1 /** @name Z80 Register allocation functions.
4 Note: much of this is ripped straight from Sandeep's mcs51 code.
6 This code maps the virtual symbols and code onto the real
7 hardware. It allocates based on usage and how long the varible
8 lives into registers or temporary memory on the stack.
10 On the Z80 hl and ix and a are reserved for the code generator,
11 leaving bc and de for allocation. iy is unusable due to currently
12 as it's only adressable as a pair. The extra register pressure
13 from reserving hl is made up for by how much easier the sub
14 operations become. You could swap hl for iy if the undocumented
15 iyl/iyh instructions are available.
17 The stack frame is the common ix-bp style. Basically:
22 ix+0: calling functions ix
25 sp: end of local varibles
27 There is currently no support for bit spaces or banked functions.
29 This program is free software; you can redistribute it and/or
30 modify it under the terms of the GNU General Public License as
31 published by the Free Software Foundation; either version 2, or (at
32 your option) any later version. This program is distributed in the
33 hope that it will be useful, but WITHOUT ANY WARRANTY; without even
34 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
35 PURPOSE. See the GNU General Public License for more details.
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
40 USA. In other words, you are welcome to use, share and improve
41 this program. You are forbidden to forbid anyone else to use,
42 share and improve what you give them. Help stamp out
47 #include "SDCCicode.h"
49 /* Flags to turn off optimisations.
54 DISABLE_PACK_ASSIGN = 0,
55 DISABLE_PACK_ONE_USE = 0,
60 /* Flags to turn on debugging code.
67 D_ACCUSE2_VERBOSE = 0,
77 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
82 #define DISABLE_PACKREGSFORSUPPORT 1
83 #define DISABLE_PACKREGSFORACCUSE 1
85 extern void genZ80Code (iCode *);
87 /** Local static variables */
93 bitVect *totRegAssigned; /* final set of LRs that got into registers */
96 /* registers used in a function */
103 static regs _gbz80_regs[] =
105 {REG_GPR, C_IDX, "c", 1},
106 {REG_GPR, B_IDX, "b", 1},
107 {REG_CND, CND_IDX, "c", 1}
110 static regs _z80_regs[] =
112 {REG_GPR, C_IDX, "c", 1},
113 {REG_GPR, B_IDX, "b", 1},
114 {REG_GPR, E_IDX, "e", 1},
115 {REG_GPR, D_IDX, "d", 1},
116 {REG_CND, CND_IDX, "c", 1}
121 /** Number of usable registers (all but C) */
122 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
123 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
125 static void spillThis (symbol *);
126 static void freeAllRegs ();
128 /** Allocates register of given type.
129 'type' is not used on the z80 version. It was used to select
130 between pointer and general purpose registers on the mcs51 version.
132 @return Pointer to the newly allocated register.
135 allocReg (short type)
139 for (i = 0; i < _G.nRegs; i++)
141 /* For now we allocate from any free */
142 if (regsZ80[i].isFree)
144 regsZ80[i].isFree = 0;
147 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
149 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
153 D (D_ALLOC, ("allocReg: No free.\n"));
157 /** Returns pointer to register wit index number
164 for (i = 0; i < _G.nRegs; i++)
166 if (regsZ80[i].rIdx == idx)
172 wassertl (0, "regWithIdx not found");
176 /** Frees a register.
181 wassert (!reg->isFree);
183 D (D_ALLOC, ("freeReg: freed %p\n", reg));
187 /** Returns number of free registers.
195 for (i = 0; i < _G.nRegs; i++)
197 /* For now only one reg type */
198 if (regsZ80[i].isFree)
206 /** Free registers with type.
209 nfreeRegsType (int type)
214 if ((nfr = nFreeRegs (type)) == 0)
216 return nFreeRegs (REG_GPR);
220 return nFreeRegs (type);
223 /*-----------------------------------------------------------------*/
224 /* useReg - marks a register as used */
225 /*-----------------------------------------------------------------*/
232 /*-----------------------------------------------------------------*/
233 /* computeSpillable - given a point find the spillable live ranges */
234 /*-----------------------------------------------------------------*/
236 computeSpillable (iCode * ic)
240 /* spillable live ranges are those that are live at this
241 point . the following categories need to be subtracted
243 a) - those that are already spilt
244 b) - if being used by this one
245 c) - defined by this one */
247 spillable = bitVectCopy (ic->rlive);
249 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
251 bitVectCplAnd (spillable, ic->uses); /* used in this one */
252 bitVectUnSetBit (spillable, ic->defKey);
253 spillable = bitVectIntersect (spillable, _G.regAssigned);
258 /*-----------------------------------------------------------------*/
259 /* noSpilLoc - return true if a variable has no spil location */
260 /*-----------------------------------------------------------------*/
262 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
264 return (sym->usl.spillLoc ? 0 : 1);
267 /*-----------------------------------------------------------------*/
268 /* hasSpilLoc - will return 1 if the symbol has spil location */
269 /*-----------------------------------------------------------------*/
271 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
273 return (sym->usl.spillLoc ? 1 : 0);
276 /** Will return 1 if the remat flag is set.
277 A symbol is rematerialisable if it doesnt need to be allocated
278 into registers at creation as it can be re-created at any time -
279 i.e. it's constant in some way.
282 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
287 /*-----------------------------------------------------------------*/
288 /* allLRs - return true for all */
289 /*-----------------------------------------------------------------*/
291 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
296 /** liveRangesWith - applies function to a given set of live range
299 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
300 eBBlock * ebp, iCode * ic)
305 if (!lrs || !lrs->size)
308 for (i = 1; i < lrs->size; i++)
311 if (!bitVectBitValue (lrs, i))
314 /* if we don't find it in the live range
315 hash table we are in serious trouble */
316 if (!(sym = hTabItemWithKey (liveRanges, i)))
318 wassertl (0, "liveRangesWith could not find liveRange");
322 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
324 addSetHead (&rset, sym);
332 /** leastUsedLR - given a set determines which is the least used
335 leastUsedLR (set * sset)
337 symbol *sym = NULL, *lsym = NULL;
339 sym = lsym = setFirstItem (sset);
344 for (; lsym; lsym = setNextItem (sset))
347 /* if usage is the same then prefer
348 the spill the smaller of the two */
349 if (lsym->used == sym->used)
350 if (getSize (lsym->type) < getSize (sym->type))
354 if (lsym->used < sym->used)
359 setToNull ((void *) &sset);
364 /** noOverLap - will iterate through the list looking for over lap
367 noOverLap (set * itmpStack, symbol * fsym)
371 for (sym = setFirstItem (itmpStack); sym;
372 sym = setNextItem (itmpStack))
374 if (bitVectBitValue(sym->clashes,fsym->key))
377 // if sym starts before (or on) our end point
378 // and ends after (or on) our start point,
380 if (sym->liveFrom <= fsym->liveTo &&
381 sym->liveTo >= fsym->liveFrom)
390 /*-----------------------------------------------------------------*/
391 /* isFree - will return 1 if the a free spil location is found */
392 /*-----------------------------------------------------------------*/
396 V_ARG (symbol **, sloc);
397 V_ARG (symbol *, fsym);
399 /* if already found */
403 /* if it is free && and the itmp assigned to
404 this does not have any overlapping live ranges
405 with the one currently being assigned and
406 the size can be accomodated */
408 noOverLap (sym->usl.itmpStack, fsym) &&
409 getSize (sym->type) >= getSize (fsym->type))
418 /*-----------------------------------------------------------------*/
419 /* createStackSpil - create a location on the stack to spil */
420 /*-----------------------------------------------------------------*/
422 createStackSpil (symbol * sym)
426 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
428 /* first go try and find a free one that is already
429 existing on the stack */
430 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
432 /* found a free one : just update & return */
433 sym->usl.spillLoc = sloc;
436 addSetHead (&sloc->usl.itmpStack, sym);
437 D (D_ALLOC, ("createStackSpil: found existing\n"));
441 /* could not then have to create one , this is the hard part
442 we need to allocate this on the stack : this is really a
443 hack!! but cannot think of anything better at this time */
445 sprintf (buffer, "sloc%d", _G.slocNum++);
446 sloc = newiTemp (buffer);
448 /* set the type to the spilling symbol */
449 sloc->type = copyLinkChain (sym->type);
450 sloc->etype = getSpec (sloc->type);
451 SPEC_SCLS (sloc->etype) = S_AUTO;
452 SPEC_EXTR (sloc->etype) = 0;
453 SPEC_STAT (sloc->etype) = 0;
454 SPEC_VOLATILE(sloc->etype) = 0;
458 sloc->isref = 1; /* to prevent compiler warning */
460 /* if it is on the stack then update the stack */
461 if (IN_STACK (sloc->etype))
463 currFunc->stack += getSize (sloc->type);
464 _G.stackExtend += getSize (sloc->type);
468 _G.dataExtend += getSize (sloc->type);
471 /* add it to the stackSpil set */
472 addSetHead (&_G.stackSpil, sloc);
473 sym->usl.spillLoc = sloc;
476 /* add it to the set of itempStack set
477 of the spill location */
478 addSetHead (&sloc->usl.itmpStack, sym);
480 D (D_ALLOC, ("createStackSpil: created new\n"));
484 /*-----------------------------------------------------------------*/
485 /* spillThis - spils a specific operand */
486 /*-----------------------------------------------------------------*/
488 spillThis (symbol * sym)
492 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
494 /* if this is rematerializable or has a spillLocation
495 we are okay, else we need to create a spillLocation
497 if (!(sym->remat || sym->usl.spillLoc))
499 createStackSpil (sym);
502 /* mark it has spilt & put it in the spilt set */
503 sym->isspilt = sym->spillA = 1;
504 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
506 bitVectUnSetBit (_G.regAssigned, sym->key);
507 bitVectUnSetBit (_G.totRegAssigned, sym->key);
509 for (i = 0; i < sym->nRegs; i++)
513 freeReg (sym->regs[i]);
518 if (sym->usl.spillLoc && !sym->remat)
520 sym->usl.spillLoc->allocreq++;
526 /*-----------------------------------------------------------------*/
527 /* allDefsOutOfRange - all definitions are out of a range */
528 /*-----------------------------------------------------------------*/
530 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
537 for (i = 0; i < defs->size; i++)
541 if (bitVectBitValue (defs, i) &&
542 (ic = hTabItemWithKey (iCodehTab, i)) &&
543 (ic->seq >= fseq && ic->seq <= toseq))
552 /*-----------------------------------------------------------------*/
553 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
554 /* but is not used as a pointer */
555 /*-----------------------------------------------------------------*/
557 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
559 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
562 /*-----------------------------------------------------------------*/
563 /* notUsedInRemaining - not used or defined in remain of the block */
564 /*-----------------------------------------------------------------*/
566 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
568 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
569 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
573 /** Select a iTemp to spil : rather a simple procedure.
576 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
578 bitVect *lrcs = NULL;
582 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
583 /* get the spillable live ranges */
584 lrcs = computeSpillable (ic);
586 /* get all live ranges that are rematerizable */
587 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
589 D (D_ALLOC, ("selectSpil: using remat.\n"));
590 /* return the least used of these */
591 return leastUsedLR (selectS);
595 /* get live ranges with spillLocations in direct space */
596 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
598 sym = leastUsedLR (selectS);
599 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
600 sym->usl.spillLoc->rname :
601 sym->usl.spillLoc->name));
603 /* mark it as allocation required */
604 sym->usl.spillLoc->allocreq++;
608 /* if the symbol is local to the block then */
609 if (forSym->liveTo < ebp->lSeq)
612 /* check if there are any live ranges allocated
613 to registers that are not used in this block */
614 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
616 sym = leastUsedLR (selectS);
617 /* if this is not rematerializable */
621 wassertl (0, "Attempted to do an unsupported block spill");
627 /* check if there are any live ranges that not
628 used in the remainder of the block */
629 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
631 sym = leastUsedLR (selectS);
636 wassertl (0, "Attempted to do an unsupported remain spill");
644 /* find live ranges with spillocation && not used as pointers */
645 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
648 sym = leastUsedLR (selectS);
649 /* mark this as allocation required */
650 sym->usl.spillLoc->allocreq++;
655 /* find live ranges with spillocation */
656 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
658 D (D_ALLOC, ("selectSpil: using with spill.\n"));
659 sym = leastUsedLR (selectS);
660 sym->usl.spillLoc->allocreq++;
664 /* couldn't find then we need to create a spil
665 location on the stack , for which one? the least
667 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
669 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
670 /* return a created spil location */
671 sym = createStackSpil (leastUsedLR (selectS));
672 sym->usl.spillLoc->allocreq++;
676 /* this is an extreme situation we will spill
677 this one : happens very rarely but it does happen */
678 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
684 /** Spil some variable & mark registers as free.
685 A spill occurs when an iTemp wont fit into the available registers.
688 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
693 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
695 /* get something we can spil */
696 ssym = selectSpil (ic, ebp, forSym);
698 /* mark it as spilt */
699 ssym->isspilt = ssym->spillA = 1;
700 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
702 /* mark it as not register assigned &
703 take it away from the set */
704 bitVectUnSetBit (_G.regAssigned, ssym->key);
705 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
707 /* mark the registers as free */
708 for (i = 0; i < ssym->nRegs; i++)
710 freeReg (ssym->regs[i]);
712 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
713 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
715 /* if spilt on stack then free up r0 & r1
716 if they could have been assigned to as gprs */
717 if (!ptrRegReq && isSpiltOnStack (ssym))
720 spillLRWithPtrReg (ssym);
723 /* if this was a block level spil then insert push & pop
724 at the start & end of block respectively */
727 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
728 /* add push to the start of the block */
729 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
730 ebp->sch->next : ebp->sch));
731 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
732 /* add pop to the end of the block */
733 addiCodeToeBBlock (ebp, nic, NULL);
736 /* if spilt because not used in the remainder of the
737 block then add a push before this instruction and
738 a pop at the end of the block */
739 if (ssym->remainSpil)
742 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
743 /* add push just before this instruction */
744 addiCodeToeBBlock (ebp, nic, ic);
746 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
747 /* add pop to the end of the block */
748 addiCodeToeBBlock (ebp, nic, NULL);
752 D (D_ALLOC, ("spilSomething: done.\n"));
760 /** Will try for GPR if not spil.
763 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
768 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
770 /* try for gpr type */
771 if ((reg = allocReg (REG_GPR)))
773 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
777 /* we have to spil */
778 if (!spilSomething (ic, ebp, sym))
780 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
784 /* make sure partially assigned registers aren't reused */
785 for (j=0; j<=sym->nRegs; j++)
787 sym->regs[j]->isFree = 0;
789 /* this looks like an infinite loop but
790 in really selectSpil will abort */
794 static regs *getRegGprNoSpil()
798 /* try for gpr type */
799 if ((reg = allocReg (REG_GPR)))
801 D (D_ALLOC, ("getRegGprNoSpil: got a reg.\n"));
806 /* just to make the compiler happy */
810 /** Symbol has a given register.
813 symHasReg (symbol * sym, regs * reg)
817 for (i = 0; i < sym->nRegs; i++)
818 if (sym->regs[i] == reg)
824 /** Check the live to and if they have registers & are not spilt then
825 free up the registers
828 deassignLRs (iCode * ic, eBBlock * ebp)
834 for (sym = hTabFirstItem (liveRanges, &k); sym;
835 sym = hTabNextItem (liveRanges, &k))
839 /* if it does not end here */
840 if (sym->liveTo > ic->seq)
843 /* if it was spilt on stack then we can
844 mark the stack spil location as free */
849 sym->usl.spillLoc->isFree = 1;
855 if (!bitVectBitValue (_G.regAssigned, sym->key))
858 /* special case check if this is an IFX &
859 the privious one was a pop and the
860 previous one was not spilt then keep track
862 if (ic->op == IFX && ic->prev &&
863 ic->prev->op == IPOP &&
864 !ic->prev->parmPush &&
865 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
866 psym = OP_SYMBOL (IC_LEFT (ic->prev));
868 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
874 bitVectUnSetBit (_G.regAssigned, sym->key);
876 /* if the result of this one needs registers
877 and does not have it then assign it right
879 if (IC_RESULT (ic) &&
880 !(SKIP_IC2 (ic) || /* not a special icode */
881 ic->op == JUMPTABLE ||
886 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
887 result->liveTo > ic->seq && /* and will live beyond this */
888 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
889 result->liveFrom == ic->seq && /* does not start before here */
890 result->regType == sym->regType && /* same register types */
891 result->nRegs && /* which needs registers */
892 !result->isspilt && /* and does not already have them */
894 !bitVectBitValue (_G.regAssigned, result->key) &&
895 /* the number of free regs + number of regs in this LR
896 can accomodate the what result Needs */
897 ((nfreeRegsType (result->regType) +
898 sym->nRegs) >= result->nRegs)
901 for (i = 0; i < result->nRegs; i++)
904 result->regs[i] = sym->regs[i];
906 result->regs[i] = getRegGpr (ic, ebp, result);
908 /* if the allocation falied which means
909 this was spilt then break */
910 if (!result->regs[i])
918 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
919 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
922 /* free the remaining */
923 for (; i < sym->nRegs; i++)
927 if (!symHasReg (psym, sym->regs[i]))
928 freeReg (sym->regs[i]);
931 freeReg (sym->regs[i]);
932 // sym->regs[i] = NULL;
939 /** Reassign this to registers.
942 reassignLR (operand * op)
944 symbol *sym = OP_SYMBOL (op);
947 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
949 /* not spilt any more */
950 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
951 bitVectUnSetBit (_G.spiltSet, sym->key);
953 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
954 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
958 for (i = 0; i < sym->nRegs; i++)
959 sym->regs[i]->isFree = 0;
962 /** Determines if allocating will cause a spill.
965 willCauseSpill (int nr, int rt)
967 /* first check if there are any avlb registers
968 of te type required */
969 if (nFreeRegs (0) >= nr)
972 /* it will cause a spil */
976 /** The allocator can allocate same registers to result and operand,
977 if this happens make sure they are in the same position as the operand
978 otherwise chaos results.
981 positionRegs (symbol * result, symbol * opsym)
983 int count = min (result->nRegs, opsym->nRegs);
984 int i, j = 0, shared = 0;
987 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
989 /* if the result has been spilt then cannot share */
994 /* first make sure that they actually share */
995 for (i = 0; i < count; i++)
997 for (j = 0; j < count; j++)
999 if (result->regs[i] == opsym->regs[j] && i != j)
1009 regs *tmp = result->regs[i];
1010 result->regs[i] = result->regs[j];
1011 result->regs[j] = tmp;
1018 /** Try to allocate a pair of registers to the symbol.
1021 tryAllocatingRegPair (symbol * sym)
1024 wassert (sym->nRegs == 2);
1025 for (i = 0; i < _G.nRegs; i += 2)
1027 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1029 regsZ80[i].isFree = 0;
1030 sym->regs[0] = ®sZ80[i];
1031 regsZ80[i + 1].isFree = 0;
1032 sym->regs[1] = ®sZ80[i + 1];
1033 sym->regType = REG_PAIR;
1037 currFunc->regsUsed =
1038 bitVectSetBit (currFunc->regsUsed, i);
1039 currFunc->regsUsed =
1040 bitVectSetBit (currFunc->regsUsed, i + 1);
1042 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1046 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1050 /*------------------------------------------------------------------*/
1051 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1052 /* it should either have registers or have beed spilled. Otherwise, */
1053 /* there was an uninitialized variable, so just spill this to get */
1054 /* the operand in a valid state. */
1055 /*------------------------------------------------------------------*/
1057 verifyRegsAssigned (operand *op, iCode * ic)
1062 if (!IS_ITEMP (op)) return;
1064 sym = OP_SYMBOL (op);
1065 if (sym->isspilt) return;
1066 if (!sym->nRegs) return;
1067 if (sym->regs[0]) return;
1069 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1070 sym->prereqv ? sym->prereqv->name : sym->name);
1075 /** Serially allocate registers to the variables.
1076 This is the main register allocation function. It is called after
1080 serialRegAssign (eBBlock ** ebbs, int count)
1084 /* for all blocks */
1085 for (i = 0; i < count; i++)
1090 if (ebbs[i]->noPath &&
1091 (ebbs[i]->entryLabel != entryLabel &&
1092 ebbs[i]->entryLabel != returnLabel))
1095 /* of all instructions do */
1096 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1099 /* if this is an ipop that means some live
1100 range will have to be assigned again */
1104 reassignLR (IC_LEFT (ic));
1107 /* if result is present && is a true symbol */
1108 if (IC_RESULT (ic) && ic->op != IFX &&
1109 IS_TRUE_SYMOP (IC_RESULT (ic)))
1110 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1112 /* take away registers from live
1113 ranges that end at this instruction */
1114 deassignLRs (ic, ebbs[i]);
1116 /* some don't need registers */
1117 /* MLH: removed RESULT and POINTER_SET condition */
1118 if (SKIP_IC2 (ic) ||
1119 ic->op == JUMPTABLE ||
1125 /* now we need to allocate registers only for the result */
1128 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1133 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1135 /* Make sure any spill location is definately allocated */
1136 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1137 !sym->usl.spillLoc->allocreq)
1139 sym->usl.spillLoc->allocreq++;
1142 /* if it does not need or is spilt
1143 or is already assigned to registers
1144 or will not live beyond this instructions */
1147 bitVectBitValue (_G.regAssigned, sym->key) ||
1148 sym->liveTo <= ic->seq)
1150 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1154 /* if some liverange has been spilt at the block level
1155 and this one live beyond this block then spil this
1157 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1159 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1163 /* if trying to allocate this will cause
1164 a spill and there is nothing to spill
1165 or this one is rematerializable then
1167 willCS = willCauseSpill (sym->nRegs, sym->regType);
1168 spillable = computeSpillable (ic);
1170 (willCS && bitVectIsZero (spillable)))
1173 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1179 /* If the live range preceeds the point of definition
1180 then ideally we must take into account registers that
1181 have been allocated after sym->liveFrom but freed
1182 before ic->seq. This is complicated, so spill this
1183 symbol instead and let fillGaps handle the allocation. */
1184 if (sym->liveFrom < ic->seq)
1190 /* if it has a spillocation & is used less than
1191 all other live ranges then spill this */
1193 if (sym->usl.spillLoc) {
1194 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1195 allLRs, ebbs[i], ic));
1196 if (leastUsed && leastUsed->used > sym->used) {
1201 /* if none of the liveRanges have a spillLocation then better
1202 to spill this one than anything else already assigned to registers */
1203 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1204 /* if this is local to this block then we might find a block spil */
1205 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1213 /* else we assign registers to it */
1214 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1215 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1217 /* Special case: Try to fit into a reg pair if
1219 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1220 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1225 for (j = 0; j < sym->nRegs; j++)
1227 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1229 /* if the allocation falied which means
1230 this was spilt then break */
1233 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1238 /* if it shares registers with operands make sure
1239 that they are in the same position */
1240 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1241 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1242 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1243 OP_SYMBOL (IC_LEFT (ic)));
1244 /* do the same for the right operand */
1245 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1246 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1247 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1248 OP_SYMBOL (IC_RIGHT (ic)));
1254 /* Check for and fix any problems with uninitialized operands */
1255 for (i = 0; i < count; i++)
1259 if (ebbs[i]->noPath &&
1260 (ebbs[i]->entryLabel != entryLabel &&
1261 ebbs[i]->entryLabel != returnLabel))
1264 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1271 verifyRegsAssigned (IC_COND (ic), ic);
1275 if (ic->op == JUMPTABLE)
1277 verifyRegsAssigned (IC_JTCOND (ic), ic);
1281 verifyRegsAssigned (IC_RESULT (ic), ic);
1282 verifyRegsAssigned (IC_LEFT (ic), ic);
1283 verifyRegsAssigned (IC_RIGHT (ic), ic);
1289 /*-----------------------------------------------------------------*/
1290 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1291 /*-----------------------------------------------------------------*/
1292 static void fillGaps()
1297 if (getenv("DISABLE_FILL_GAPS")) return;
1299 /* look for livernages that was spilt by the allocator */
1300 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1301 sym = hTabNextItem(liveRanges,&key)) {
1306 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1308 /* find the liveRanges this one clashes with, that are
1309 still assigned to registers & mark the registers as used*/
1310 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1314 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1315 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1318 clr = hTabItemWithKey(liveRanges,i);
1321 /* mark these registers as used */
1322 for (k = 0 ; k < clr->nRegs ; k++ )
1323 useReg(clr->regs[k]);
1326 if (willCauseSpill(sym->nRegs,sym->regType)) {
1327 /* NOPE :( clear all registers & and continue */
1332 /* THERE IS HOPE !!!! */
1333 for (i=0; i < sym->nRegs ; i++ ) {
1334 sym->regs[i] = getRegGprNoSpil ();
1337 /* for all its definitions check if the registers
1338 allocated needs positioning NOTE: we can position
1339 only ONCE if more than One positioning required
1342 for (i = 0 ; i < sym->defs->size ; i++ ) {
1343 if (bitVectBitValue(sym->defs,i)) {
1345 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1346 if (SKIP_IC(ic)) continue;
1347 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1348 /* if left is assigned to registers */
1349 if (IS_SYMOP(IC_LEFT(ic)) &&
1350 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1351 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1353 if (IS_SYMOP(IC_RIGHT(ic)) &&
1354 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1355 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1357 if (pdone > 1) break;
1360 for (i = 0 ; i < sym->uses->size ; i++ ) {
1361 if (bitVectBitValue(sym->uses,i)) {
1363 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1364 if (SKIP_IC(ic)) continue;
1365 if (!IS_ASSIGN_ICODE(ic)) continue ;
1367 /* if result is assigned to registers */
1368 if (IS_SYMOP(IC_RESULT(ic)) &&
1369 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1370 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1372 if (pdone > 1) break;
1375 /* had to position more than once GIVE UP */
1377 /* UNDO all the changes we made to try this */
1379 for (i=0; i < sym->nRegs ; i++ ) {
1380 sym->regs[i] = NULL;
1383 D(D_FILL_GAPS,("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1386 D(D_FILL_GAPS,("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1387 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1388 sym->isspilt = sym->spillA = 0 ;
1389 sym->usl.spillLoc->allocreq--;
1394 /*-----------------------------------------------------------------*/
1395 /* rUmaskForOp :- returns register mask for an operand */
1396 /*-----------------------------------------------------------------*/
1398 rUmaskForOp (operand * op)
1404 /* only temporaries are assigned registers */
1408 sym = OP_SYMBOL (op);
1410 /* if spilt or no registers assigned to it
1412 if (sym->isspilt || !sym->nRegs)
1415 rumask = newBitVect (_G.nRegs);
1417 for (j = 0; j < sym->nRegs; j++)
1419 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1426 z80_rUmaskForOp (operand * op)
1428 return rUmaskForOp (op);
1431 /** Returns bit vector of registers used in iCode.
1434 regsUsedIniCode (iCode * ic)
1436 bitVect *rmask = newBitVect (_G.nRegs);
1438 /* do the special cases first */
1441 rmask = bitVectUnion (rmask,
1442 rUmaskForOp (IC_COND (ic)));
1446 /* for the jumptable */
1447 if (ic->op == JUMPTABLE)
1449 rmask = bitVectUnion (rmask,
1450 rUmaskForOp (IC_JTCOND (ic)));
1455 /* of all other cases */
1457 rmask = bitVectUnion (rmask,
1458 rUmaskForOp (IC_LEFT (ic)));
1462 rmask = bitVectUnion (rmask,
1463 rUmaskForOp (IC_RIGHT (ic)));
1466 rmask = bitVectUnion (rmask,
1467 rUmaskForOp (IC_RESULT (ic)));
1473 /** For each instruction will determine the regsUsed.
1476 createRegMask (eBBlock ** ebbs, int count)
1480 /* for all blocks */
1481 for (i = 0; i < count; i++)
1485 if (ebbs[i]->noPath &&
1486 (ebbs[i]->entryLabel != entryLabel &&
1487 ebbs[i]->entryLabel != returnLabel))
1490 /* for all instructions */
1491 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1496 if (SKIP_IC2 (ic) || !ic->rlive)
1499 /* first mark the registers used in this
1501 ic->rUsed = regsUsedIniCode (ic);
1502 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1504 /* now create the register mask for those
1505 registers that are in use : this is a
1506 super set of ic->rUsed */
1507 ic->rMask = newBitVect (_G.nRegs + 1);
1509 /* for all live Ranges alive at this point */
1510 for (j = 1; j < ic->rlive->size; j++)
1515 /* if not alive then continue */
1516 if (!bitVectBitValue (ic->rlive, j))
1519 /* find the live range we are interested in */
1520 if (!(sym = hTabItemWithKey (liveRanges, j)))
1522 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1523 "createRegMask cannot find live range");
1527 /* if no register assigned to it */
1528 if (!sym->nRegs || sym->isspilt)
1531 /* for all the registers allocated to it */
1532 for (k = 0; k < sym->nRegs; k++)
1535 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1542 /** Returns the rematerialized string for a remat var.
1545 rematStr (symbol * sym)
1547 iCode *ic = sym->rematiCode;
1552 /* if plus adjust offset to right hand side */
1555 offset += (int) operandLitValue (IC_RIGHT (ic));
1556 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1560 /* if minus adjust offset to right hand side */
1563 offset -= (int) operandLitValue (IC_RIGHT (ic));
1564 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1568 /* cast then continue */
1569 if (IS_CAST_ICODE(ic)) {
1570 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1573 /* we reached the end */
1579 SNPRINTF (buffer, sizeof(buffer),
1581 OP_SYMBOL (IC_LEFT (ic))->rname,
1582 offset >= 0 ? '+' : '-',
1583 abs (offset) & 0xffff);
1587 strncpyz (buffer, OP_SYMBOL (IC_LEFT (ic))->rname, sizeof(buffer));
1593 /*-----------------------------------------------------------------*/
1594 /* regTypeNum - computes the type & number of registers required */
1595 /*-----------------------------------------------------------------*/
1602 /* for each live range do */
1603 for (sym = hTabFirstItem (liveRanges, &k); sym;
1604 sym = hTabNextItem (liveRanges, &k))
1607 /* if used zero times then no registers needed */
1608 if ((sym->liveTo - sym->liveFrom) == 0)
1611 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1613 /* if the live range is a temporary */
1617 /* if the type is marked as a conditional */
1618 if (sym->regType == REG_CND)
1621 /* if used in return only then we don't
1623 if (sym->ruonly || sym->accuse)
1625 if (IS_AGGREGATE (sym->type) || sym->isptr)
1626 sym->type = aggrToPtr (sym->type, FALSE);
1630 /* if not then we require registers */
1631 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1632 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1633 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1634 getSize (sym->type));
1635 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1637 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1641 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1642 printTypeChain (sym->type, stderr);
1643 fprintf (stderr, "\n");
1646 /* determine the type of register required */
1647 /* Always general purpose */
1648 sym->regType = REG_GPR;
1653 /* for the first run we don't provide */
1654 /* registers for true symbols we will */
1655 /* see how things go */
1656 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1663 /** Mark all registers as free.
1670 D (D_ALLOC, ("freeAllRegs: running.\n"));
1672 for (i = 0; i < _G.nRegs; i++)
1673 regsZ80[i].isFree = 1;
1676 /*-----------------------------------------------------------------*/
1677 /* deallocStackSpil - this will set the stack pointer back */
1678 /*-----------------------------------------------------------------*/
1679 DEFSETFUNC (deallocStackSpil)
1687 /** Register reduction for assignment.
1690 packRegsForAssign (iCode * ic, eBBlock * ebp)
1694 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1696 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1697 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1698 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1703 /* find the definition of iTempNN scanning backwards if we find a
1704 a use of the true symbol in before we find the definition then
1706 for (dic = ic->prev; dic; dic = dic->prev)
1708 /* PENDING: Don't pack across function calls. */
1709 if (dic->op == CALL || dic->op == PCALL)
1720 if (IS_SYMOP (IC_COND (dic)) &&
1721 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1722 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1730 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1731 IS_OP_VOLATILE (IC_RESULT (dic)))
1737 if (IS_SYMOP (IC_RESULT (dic)) &&
1738 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1740 if (POINTER_SET (dic))
1746 if (IS_SYMOP (IC_RIGHT (dic)) &&
1747 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1748 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1754 if (IS_SYMOP (IC_LEFT (dic)) &&
1755 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1756 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1762 if (IS_SYMOP (IC_RESULT (dic)) &&
1763 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1773 return 0; /* did not find */
1775 /* if assignment then check that right is not a bit */
1776 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
1778 sym_link *etype = operandType (IC_RESULT (dic));
1779 if (IS_BITFIELD (etype))
1781 /* if result is a bit too then it's ok */
1782 etype = operandType (IC_RESULT (ic));
1783 if (!IS_BITFIELD (etype))
1790 /* if the result is on stack or iaccess then it must be
1791 the same atleast one of the operands */
1792 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1793 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1795 /* the operation has only one symbol
1796 operator then we can pack */
1797 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1798 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1801 if (!((IC_LEFT (dic) &&
1802 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1804 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1808 /* found the definition */
1809 /* replace the result with the result of */
1810 /* this assignment and remove this assignment */
1811 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1812 IC_RESULT (dic) = IC_RESULT (ic);
1814 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1816 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1818 /* delete from liverange table also
1819 delete from all the points inbetween and the new
1821 for (sic = dic; sic != ic; sic = sic->next)
1823 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1824 if (IS_ITEMP (IC_RESULT (dic)))
1825 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1828 remiCodeFromeBBlock (ebp, ic);
1829 // PENDING: Check vs mcs51
1830 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1831 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1832 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1836 /** Scanning backwards looks for first assig found.
1839 findAssignToSym (operand * op, iCode * ic)
1843 for (dic = ic->prev; dic; dic = dic->prev)
1846 /* if definition by assignment */
1847 if (dic->op == '=' &&
1848 !POINTER_SET (dic) &&
1849 IC_RESULT (dic)->key == op->key)
1850 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1853 /* we are interested only if defined in far space */
1854 /* or in stack space in case of + & - */
1856 /* if assigned to a non-symbol then return
1858 if (!IS_SYMOP (IC_RIGHT (dic)))
1861 /* if the symbol is in far space then
1863 if (isOperandInFarSpace (IC_RIGHT (dic)))
1866 /* for + & - operations make sure that
1867 if it is on the stack it is the same
1868 as one of the three operands */
1869 if ((ic->op == '+' || ic->op == '-') &&
1870 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1873 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1874 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1875 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1883 /* if we find an usage then we cannot delete it */
1884 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1887 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1890 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1894 /* now make sure that the right side of dic
1895 is not defined between ic & dic */
1898 iCode *sic = dic->next;
1900 for (; sic != ic; sic = sic->next)
1901 if (IC_RESULT (sic) &&
1902 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1911 #if !DISABLE_PACKREGSFORSUPPORT
1914 /*-----------------------------------------------------------------*/
1915 /* packRegsForSupport :- reduce some registers for support calls */
1916 /*-----------------------------------------------------------------*/
1918 packRegsForSupport (iCode * ic, eBBlock * ebp)
1921 /* for the left & right operand :- look to see if the
1922 left was assigned a true symbol in far space in that
1923 case replace them */
1924 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1926 if (IS_ITEMP (IC_LEFT (ic)) &&
1927 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1929 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1935 /* found it we need to remove it from the
1937 for (sic = dic; sic != ic; sic = sic->next)
1938 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1940 IC_LEFT (ic)->operand.symOperand =
1941 IC_RIGHT (dic)->operand.symOperand;
1942 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1943 remiCodeFromeBBlock (ebp, dic);
1944 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1945 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1946 // PENDING: Check vs mcs51
1950 /* do the same for the right operand */
1953 IS_ITEMP (IC_RIGHT (ic)) &&
1954 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1956 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1962 /* found it we need to remove it from the block */
1963 for (sic = dic; sic != ic; sic = sic->next)
1964 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1966 IC_RIGHT (ic)->operand.symOperand =
1967 IC_RIGHT (dic)->operand.symOperand;
1968 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1970 remiCodeFromeBBlock (ebp, dic);
1971 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1972 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1973 // PENDING: vs mcs51
1981 /** Will reduce some registers for single use.
1984 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1990 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1992 /* if returning a literal then do nothing */
1996 /* only upto 2 bytes since we cannot predict
1997 the usage of b, & acc */
1998 if (getSize (operandType (op)) > 2)
2001 if (ic->op != RETURN &&
2005 /* this routine will mark the a symbol as used in one
2006 instruction use only && if the defintion is local
2007 (ie. within the basic block) && has only one definition &&
2008 that definiion is either a return value from a
2009 function or does not contain any variables in
2011 uses = bitVectCopy (OP_USES (op));
2012 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2013 if (!bitVectIsZero (uses)) /* has other uses */
2016 /* if it has only one defintion */
2017 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2018 return NULL; /* has more than one definition */
2020 /* get the that definition */
2022 hTabItemWithKey (iCodehTab,
2023 bitVectFirstBit (OP_DEFS (op)))))
2026 /* found the definition now check if it is local */
2027 if (dic->seq < ebp->fSeq ||
2028 dic->seq > ebp->lSeq)
2029 return NULL; /* non-local */
2031 /* now check if it is the return from a function call */
2032 if (dic->op == CALL || dic->op == PCALL)
2034 if (ic->op != SEND && ic->op != RETURN &&
2035 !POINTER_SET(ic) && !POINTER_GET(ic))
2037 OP_SYMBOL (op)->ruonly = 1;
2043 /* otherwise check that the definition does
2044 not contain any symbols in far space */
2045 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2046 isOperandInFarSpace (IC_RIGHT (dic)) ||
2047 IS_OP_RUONLY (IC_LEFT (ic)) ||
2048 IS_OP_RUONLY (IC_RIGHT (ic)))
2053 /* if pointer set then make sure the pointer is one byte */
2054 if (POINTER_SET (dic))
2057 if (POINTER_GET (dic))
2062 /* also make sure the intervenening instructions
2063 don't have any thing in far space */
2064 for (dic = dic->next; dic && dic != ic; dic = dic->next)
2066 /* if there is an intervening function call then no */
2067 if (dic->op == CALL || dic->op == PCALL)
2069 /* if pointer set then make sure the pointer
2071 if (POINTER_SET (dic))
2074 if (POINTER_GET (dic))
2077 /* if address of & the result is remat the okay */
2078 if (dic->op == ADDRESS_OF &&
2079 OP_SYMBOL (IC_RESULT (dic))->remat)
2082 /* if left or right or result is in far space */
2083 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2084 isOperandInFarSpace (IC_RIGHT (dic)) ||
2085 isOperandInFarSpace (IC_RESULT (dic)) ||
2086 IS_OP_RUONLY (IC_LEFT (dic)) ||
2087 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2088 IS_OP_RUONLY (IC_RESULT (dic)))
2094 OP_SYMBOL (op)->ruonly = 1;
2098 /*-----------------------------------------------------------------*/
2099 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2100 /*-----------------------------------------------------------------*/
2102 isBitwiseOptimizable (iCode * ic)
2104 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2106 /* bitwise operations are considered optimizable
2107 under the following conditions (Jean-Louis VERN)
2119 if (IS_LITERAL (rtype))
2125 Certian assignments involving pointers can be temporarly stored
2136 #if !DISABLE_PACKREGSFORACCUSE
2139 /** Pack registers for acc use.
2140 When the result of this operation is small and short lived it may
2141 be able to be stored in the accumelator.
2144 packRegsForAccUse (iCode * ic)
2148 /* if this is an aggregate, e.g. a one byte char array */
2149 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2153 /* if + or - then it has to be one byte result */
2154 if ((ic->op == '+' || ic->op == '-')
2155 && getSize (operandType (IC_RESULT (ic))) > 1)
2158 /* if shift operation make sure right side is not a literal */
2159 if (ic->op == RIGHT_OP &&
2160 (isOperandLiteral (IC_RIGHT (ic)) ||
2161 getSize (operandType (IC_RESULT (ic))) > 1))
2164 if (ic->op == LEFT_OP &&
2165 (isOperandLiteral (IC_RIGHT (ic)) ||
2166 getSize (operandType (IC_RESULT (ic))) > 1))
2169 /* has only one definition */
2170 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2173 /* has only one use */
2174 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2177 /* and the usage immediately follows this iCode */
2178 if (!(uic = hTabItemWithKey (iCodehTab,
2179 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2182 if (ic->next != uic)
2185 /* if it is a conditional branch then we definitely can */
2189 if (uic->op == JUMPTABLE)
2193 /* if the usage is not is an assignment or an
2194 arithmetic / bitwise / shift operation then not */
2195 if (POINTER_SET (uic) &&
2196 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2200 if (uic->op != '=' &&
2201 !IS_ARITHMETIC_OP (uic) &&
2202 !IS_BITWISE_OP (uic) &&
2203 uic->op != LEFT_OP &&
2204 uic->op != RIGHT_OP)
2207 /* if used in ^ operation then make sure right is not a
2209 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2212 /* if shift operation make sure right side is not a literal */
2213 if (uic->op == RIGHT_OP &&
2214 (isOperandLiteral (IC_RIGHT (uic)) ||
2215 getSize (operandType (IC_RESULT (uic))) > 1))
2218 if (uic->op == LEFT_OP &&
2219 (isOperandLiteral (IC_RIGHT (uic)) ||
2220 getSize (operandType (IC_RESULT (uic))) > 1))
2224 /* make sure that the result of this icode is not on the
2225 stack, since acc is used to compute stack offset */
2226 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2227 OP_SYMBOL (IC_RESULT (uic))->onStack)
2232 /* if either one of them in far space then we cannot */
2233 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2234 isOperandInFarSpace (IC_LEFT (uic))) ||
2235 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2236 isOperandInFarSpace (IC_RIGHT (uic))))
2240 /* if the usage has only one operand then we can */
2241 if (IC_LEFT (uic) == NULL ||
2242 IC_RIGHT (uic) == NULL)
2245 /* make sure this is on the left side if not
2246 a '+' since '+' is commutative */
2247 if (ic->op != '+' &&
2248 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2251 // See mcs51 ralloc for reasoning
2253 /* if one of them is a literal then we can */
2254 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2255 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2262 /** This is confusing :) Guess for now */
2263 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2264 (IS_ITEMP (IC_RIGHT (uic)) ||
2265 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2268 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2269 (IS_ITEMP (IC_LEFT (uic)) ||
2270 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2274 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2279 packRegsForHLUse (iCode * ic)
2283 /* PENDING: Could do IFX */
2289 /* has only one definition */
2290 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2292 D (D_HLUSE, (" + Dropping as has more than one def\n"));
2296 /* has only one use */
2297 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2299 D (D_HLUSE, (" + Dropping as has more than one use\n"));
2303 /* and the usage immediately follows this iCode */
2304 if (!(uic = hTabItemWithKey (iCodehTab,
2305 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2307 D (D_HLUSE, (" + Dropping as usage isn't in this block\n"));
2311 if (ic->next != uic)
2313 D (D_HLUSE, (" + Dropping as usage doesn't follow this\n"));
2322 if (getSize (operandType (IC_RESULT (ic))) != 2 ||
2323 (IC_LEFT(uic) && getSize (operandType (IC_LEFT (uic))) != 2) ||
2324 (IC_RIGHT(uic) && getSize (operandType (IC_RIGHT (uic))) != 2))
2326 D (D_HLUSE, (" + Dropping as the result size is not 2\n"));
2332 if (ic->op == CAST && uic->op == IPUSH)
2334 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2336 if (ic->op == ADDRESS_OF && POINTER_GET (uic) && IS_ITEMP( IC_RESULT (uic)))
2338 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2343 /* Case of assign a constant to offset in a static array. */
2344 if (ic->op == '+' && IS_VALOP (IC_RIGHT (ic)))
2346 if (uic->op == '=' && POINTER_SET (uic))
2350 else if (uic->op == IPUSH && getSize (operandType (IC_LEFT (uic))) == 2)
2357 D (D_HLUSE, (" + Dropping as it's a bad op\n"));
2360 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_SCRATCH;
2364 packRegsForHLUse3 (iCode * lic, operand * op, eBBlock * ebp)
2369 bool isFirst = TRUE;
2371 D (D_PACK_HLUSE3, ("Checking HL on %p lic key %u first def %u line %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)), lic->lineno));
2375 if ( OP_SYMBOL(op)->accuse)
2380 if (OP_SYMBOL(op)->remat)
2385 /* Only defined once */
2386 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2389 if (getSize (operandType (op)) > 2)
2392 /* And this is the definition */
2393 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2396 /* first check if any overlapping liverange has already been
2398 if (OP_SYMBOL(op)->clashes)
2400 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2402 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2404 sym = hTabItemWithKey(liveRanges,i);
2405 if (sym->accuse == ACCUSE_SCRATCH)
2413 /* Nothing else that clashes with this is using the scratch
2414 register. Scan through all of the intermediate instructions and
2415 see if any of them could nuke HL.
2417 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2419 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2420 ic = hTabNextItem(iCodeSeqhTab, &key))
2424 D (D_PACK_HLUSE3, ("(On %p: op: %u next: %p)\n", ic, ic->op, ic->next));
2429 if (ic->op == ADDRESS_OF)
2431 if (POINTER_GET (ic))
2433 if (ic->op == '=' && !POINTER_SET(ic))
2437 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))
2438 && isOperandInDirSpace (IC_RESULT (ic)))
2441 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))
2442 && isOperandInDirSpace (IC_LEFT (ic)))
2445 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))
2446 && isOperandInDirSpace (IC_RIGHT (ic)))
2449 /* Handle the non left/right/result ones first */
2452 if (ic->op == JUMPTABLE)
2461 if (ic->op == IPUSH && isOperandEqual (op, IC_LEFT (ic)))
2464 if (ic->op == SEND && isOperandEqual (op, IC_LEFT (ic)))
2467 if (ic->op == CALL && isOperandEqual (op, IC_RESULT (ic)))
2470 if (ic->op == LEFT_OP && isOperandLiteral (IC_RIGHT (ic)))
2473 if ((ic->op == '=' && !POINTER_SET(ic)) ||
2474 ic->op == UNARYMINUS ||
2483 if (ic->op == '*' && isOperandEqual (op, IC_LEFT (ic)))
2486 if (POINTER_SET (ic) && isOperandEqual (op, IC_RESULT (ic)))
2489 if (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic)))
2492 if (IS_VALOP (IC_RIGHT (ic)) &&
2499 /* By default give up */
2503 D (D_PACK_HLUSE3, ("Succeeded!\n"))
2505 OP_SYMBOL (op)->accuse = ACCUSE_SCRATCH;
2510 packRegsForIYUse (iCode * lic, operand * op, eBBlock * ebp)
2517 D (D_PACK_IY, ("Checking IY on %p lic key %u first def %u line %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)), lic->lineno));
2521 if ( OP_SYMBOL(op)->accuse)
2526 if (OP_SYMBOL(op)->remat)
2531 /* Only defined once */
2532 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2535 /* And this is the definition */
2536 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2539 /* first check if any overlapping liverange has already been
2541 if (OP_SYMBOL(op)->clashes)
2543 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2545 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2547 sym = hTabItemWithKey(liveRanges,i);
2548 if (sym->accuse == ACCUSE_IY)
2556 /* Only a few instructions can load into IY */
2562 if (getSize (operandType (op)) != 2)
2564 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2568 /* Nothing else that clashes with this is using the scratch
2569 register. Scan through all of the intermediate instructions and
2570 see if any of them could nuke HL.
2572 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2575 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2576 ic = hTabNextItem(iCodeSeqhTab,&key))
2581 if (ic->op == PCALL ||
2590 /* Be pessamistic. */
2594 D (D_PACK_IY, (" op: %u uses %u result: %d left: %d right: %d\n", ic->op, bitVectBitValue(uses, ic->key),
2595 IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) ? isOperandInDirSpace(IC_RESULT(ic)) : -1,
2596 IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) ? isOperandInDirSpace(IC_LEFT(ic)) : -1,
2597 IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) ? isOperandInDirSpace(IC_RIGHT(ic)) : -1
2600 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2601 isOperandInDirSpace(IC_RESULT(ic)))
2604 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2605 isOperandInDirSpace(IC_RIGHT(ic)))
2608 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2609 isOperandInDirSpace(IC_LEFT(ic)))
2612 /* Only certain rules will work against IY. Check if this iCode uses
2614 if (bitVectBitValue(uses, ic->key) != 0)
2616 if (ic->op == '=' &&
2617 isOperandEqual(IC_RESULT(ic), op))
2620 if (ic->op == GET_VALUE_AT_ADDRESS &&
2621 isOperandEqual(IC_LEFT(ic), op))
2624 if (isOperandEqual(IC_RESULT(ic), IC_LEFT(ic)) == FALSE)
2627 if (IC_RIGHT (ic) && IS_VALOP (IC_RIGHT (ic)))
2629 if (ic->op == '+' ||
2632 /* Only works if the constant is small */
2633 if (operandLitValue (IC_RIGHT (ic)) < 4)
2642 /* This iCode doesn't use the sym. See if this iCode preserves IY.
2647 /* By default give up */
2651 D (D_PACK_IY, ("Succeeded IY!\n"));
2653 OP_SYMBOL (op)->accuse = ACCUSE_IY;
2657 /** Returns TRUE if this operation can use acc and if it preserves the value.
2660 opPreservesA (iCode * uic)
2664 /* If we've gotten this far then the thing to compare must be
2665 small enough and must be in A.
2670 if (uic->op == JUMPTABLE)
2672 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2676 /* A pointer assign preserves A if A is the left value. */
2677 if (uic->op == '=' && POINTER_SET (uic))
2682 /* if the usage has only one operand then we can */
2683 /* PENDING: check */
2684 if (IC_LEFT (uic) == NULL ||
2685 IC_RIGHT (uic) == NULL)
2687 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2691 /* PENDING: check this rule */
2692 if (getSize (operandType (IC_RESULT (uic))) > 1)
2694 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2699 /* Disabled all of the old rules as they weren't verified and have
2700 caused at least one problem.
2705 /** Returns true if this operand preserves the value of A.
2708 opIgnoresA (iCode * ic, iCode * uic)
2710 /* A increment of an iTemp by a constant is OK. */
2711 if ( uic->op == '+' &&
2712 IS_ITEMP (IC_LEFT (uic)) &&
2713 IS_ITEMP (IC_RESULT (uic)) &&
2714 IS_OP_LITERAL (IC_RIGHT (uic)))
2716 unsigned int icount = (unsigned int) ulFromVal (IC_RIGHT (uic)->operand.valOperand);
2718 /* Being an ITEMP means that we're already a symbol. */
2720 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2726 else if (uic->op == '=' && !POINTER_SET (uic))
2728 /* If they are equal and get optimised out then things are OK. */
2729 if (isOperandEqual (IC_RESULT (uic), IC_RIGHT (uic)))
2731 /* Straight assign is OK. */
2740 /* Some optimisation cases:
2752 ; genAssign (pointer)
2756 want to optimise down to:
2762 So genPointer get is OK
2763 genPlus where the right is constant, left is iTemp, and result is same as left
2764 genAssign (pointer) is OK
2775 ; genAssign (pointer)
2776 ; AOP_STK for _strcpy_to_1_1
2781 want to optimise down to:
2787 So genIfx where IC_COND has size of 1 and is a constant.
2790 /** Pack registers for acc use.
2791 When the result of this operation is small and short lived it may
2792 be able to be stored in the accumulator.
2794 Note that the 'A preserving' list is currently emperical :)
2797 packRegsForAccUse2 (iCode * ic)
2801 D (D_ACCUSE2, ("packRegsForAccUse2: running on ic %p line %u\n", ic, ic->lineno));
2805 /* Filter out all but those 'good' commands */
2807 !POINTER_GET (ic) &&
2810 !IS_BITWISE_OP (ic) &&
2816 ic->op != GETHBIT &&
2819 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2823 /* if + or - then it has to be one byte result.
2826 if ((ic->op == '+' || ic->op == '-')
2827 && getSize (operandType (IC_RESULT (ic))) > 1)
2829 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2833 /* has only one definition */
2834 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2836 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2840 /* Right. We may be able to propagate it through if:
2841 For each in the chain of uses the intermediate is OK.
2843 /* Get next with 'uses result' bit on
2844 If this->next == next
2845 Validate use of next
2846 If OK, increase count
2848 /* and the usage immediately follows this iCode */
2849 if (!(uic = hTabItemWithKey (iCodehTab,
2850 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2852 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2857 /* Create a copy of the OP_USES bit vect */
2858 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2860 iCode *scan = ic, *next;
2864 setBit = bitVectFirstBit (uses);
2865 next = hTabItemWithKey (iCodehTab, setBit);
2866 if (scan->next == next)
2868 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2870 bitVectUnSetBit (uses, setBit);
2871 /* Still contigous. */
2872 if (!opPreservesA (next))
2874 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2877 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2880 /*else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2882 if (next->prev == NULL)
2884 if (!opPreservesA (next))
2886 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2889 bitVectUnSetBit (uses, setBit);
2894 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2897 } //This caused bug #1292721 */
2898 else if (scan->next == NULL)
2900 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2901 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2906 if (opIgnoresA (ic, scan->next))
2910 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2914 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2919 while (!bitVectIsZero (uses));
2921 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2926 /** Does some transformations to reduce register pressure.
2929 packRegisters (eBBlock * ebp)
2934 D (D_ALLOC, ("packRegisters: entered.\n"));
2936 while (1 && !DISABLE_PACK_ASSIGN)
2939 /* look for assignments of the form */
2940 /* iTempNN = TRueSym (someoperation) SomeOperand */
2942 /* TrueSym := iTempNN:1 */
2943 for (ic = ebp->sch; ic; ic = ic->next)
2945 /* find assignment of the form TrueSym := iTempNN:1 */
2946 if (ic->op == '=' && !POINTER_SET (ic))
2947 change += packRegsForAssign (ic, ebp);
2953 for (ic = ebp->sch; ic; ic = ic->next)
2955 /* Safe: address of a true sym is always constant. */
2956 /* if this is an itemp & result of a address of a true sym
2957 then mark this as rematerialisable */
2958 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2960 if (ic->op == ADDRESS_OF &&
2961 IS_ITEMP (IC_RESULT (ic)) &&
2962 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2963 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2964 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2967 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2968 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2969 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2972 /* Safe: just propagates the remat flag */
2973 /* if straight assignment then carry remat flag if this is the
2975 if (ic->op == '=' &&
2976 !POINTER_SET (ic) &&
2977 IS_SYMOP (IC_RIGHT (ic)) &&
2978 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2979 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2980 !isOperandGlobal(IC_RESULT(ic)) && /* due to bug 1618050 */
2981 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2983 OP_SYMBOL (IC_RESULT (ic))->remat =
2984 OP_SYMBOL (IC_RIGHT (ic))->remat;
2985 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2986 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2989 /* if the condition of an if instruction is defined in the
2990 previous instruction then mark the itemp as a conditional */
2991 if ((IS_CONDITIONAL (ic) ||
2992 ((ic->op == BITWISEAND ||
2995 isBitwiseOptimizable (ic))) &&
2996 ic->next && ic->next->op == IFX &&
2997 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2998 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2999 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3002 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3007 /* reduce for support function calls */
3008 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3009 packRegsForSupport (ic, ebp);
3012 /* some cases the redundant moves can
3013 can be eliminated for return statements */
3014 if (ic->op == RETURN || ic->op == SEND)
3016 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3019 /* if pointer set & left has a size more than
3020 one and right is not in far space */
3021 if (!DISABLE_PACK_ONE_USE &&
3023 /* MLH: no such thing.
3024 !isOperandInFarSpace(IC_RIGHT(ic)) && */
3025 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3026 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3027 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3030 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3033 /* if pointer get */
3034 if (!DISABLE_PACK_ONE_USE &&
3036 IS_SYMOP (IC_LEFT (ic)) &&
3037 /* MLH: dont have far space
3038 !isOperandInFarSpace(IC_RESULT(ic))&& */
3039 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3040 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3041 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3044 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3047 /* pack registers for accumulator use, when the result of an
3048 arithmetic or bit wise operation has only one use, that use is
3049 immediately following the defintion and the using iCode has
3050 only one operand or has two operands but one is literal & the
3051 result of that operation is not on stack then we can leave the
3052 result of this operation in acc:b combination */
3054 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
3060 packRegsForHLUse (ic);
3064 packRegsForHLUse3 (ic, IC_RESULT (ic), ebp);
3068 if (!DISABLE_PACK_IY && IS_ITEMP (IC_RESULT (ic)) && IS_Z80)
3070 packRegsForIYUse (ic, IC_RESULT (ic), ebp);
3073 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
3074 getSize (operandType (IC_RESULT (ic))) == 1)
3076 packRegsForAccUse2 (ic);
3081 /** Joins together two byte constant pushes into one word push.
3084 joinPushes (iCode *lic)
3088 for (ic = lic; ic; ic = ic->next)
3095 /* Anything past this? */
3100 /* This and the next pushes? */
3101 if (ic->op != IPUSH || uic->op != IPUSH)
3105 /* Both literals? */
3106 if ( !IS_OP_LITERAL (IC_LEFT (ic)) || !IS_OP_LITERAL (IC_LEFT (uic)))
3110 /* Both characters? */
3111 if ( getSize (operandType (IC_LEFT (ic))) != 1 || getSize (operandType (IC_LEFT (uic))) != 1)
3115 /* Pull out the values, make a new type, and create the new iCode for it.
3117 first = (int)operandLitValue ( IC_LEFT (ic));
3118 second = (int)operandLitValue ( IC_LEFT (uic));
3120 sprintf (buffer, "%uu", ((first << 8) | (second & 0xFF)) & 0xFFFFU);
3121 val = constVal (buffer);
3122 SPEC_NOUN (val->type) = V_INT;
3123 IC_LEFT (ic) = operandFromOperand (IC_LEFT (ic));
3124 IC_LEFT (ic)->operand.valOperand = val;
3126 /* Now remove the second one from the list. */
3127 ic->next = uic->next;
3130 /* Patch up the reverse link */
3131 uic->next->prev = ic;
3138 /*-----------------------------------------------------------------*/
3139 /* assignRegisters - assigns registers to each live range as need */
3140 /*-----------------------------------------------------------------*/
3142 z80_assignRegisters (ebbIndex * ebbi)
3144 eBBlock ** ebbs = ebbi->bbOrder;
3145 int count = ebbi->count;
3149 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
3151 setToNull ((void *) &_G.funcrUsed);
3152 setToNull ((void *) &_G.totRegAssigned);
3153 _G.stackExtend = _G.dataExtend = 0;
3157 /* DE is required for the code gen. */
3158 _G.nRegs = GBZ80_MAX_REGS;
3159 regsZ80 = _gbz80_regs;
3163 _G.nRegs = Z80_MAX_REGS;
3164 regsZ80 = _z80_regs;
3167 /* change assignments this will remove some
3168 live ranges reducing some register pressure */
3169 for (i = 0; i < count; i++)
3170 packRegisters (ebbs[i]);
3172 /* liveranges probably changed by register packing
3173 so we compute them again */
3174 recomputeLiveRanges (ebbs, count);
3176 if (options.dump_pack)
3177 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3179 /* first determine for each live range the number of
3180 registers & the type of registers required for each */
3183 /* and serially allocate registers */
3184 serialRegAssign (ebbs, count);
3189 /* if stack was extended then tell the user */
3192 /* werror(W_TOOMANY_SPILS,"stack", */
3193 /* _G.stackExtend,currFunc->name,""); */
3199 /* werror(W_TOOMANY_SPILS,"data space", */
3200 /* _G.dataExtend,currFunc->name,""); */
3204 if (options.dump_rassgn) {
3205 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3206 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3209 /* after that create the register mask
3210 for each of the instruction */
3211 createRegMask (ebbs, count);
3213 /* now get back the chain */
3214 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3216 ic = joinPushes (ic);
3218 /* redo that offsets for stacked automatic variables */
3219 redoStackOffsets ();
3223 /* free up any stackSpil locations allocated */
3224 applyToSet (_G.stackSpil, deallocStackSpil);
3226 setToNull ((void *) &_G.stackSpil);
3227 setToNull ((void *) &_G.spiltSet);
3228 /* mark all registers as free */