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
50 DISABLE_PACK_ASSIGN = 0,
51 DISABLE_PACK_ONE_USE = 0,
62 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
67 /*-----------------------------------------------------------------*/
68 /* At this point we start getting processor specific although */
69 /* some routines are non-processor specific & can be reused when */
70 /* targetting other processors. The decision for this will have */
71 /* to be made on a routine by routine basis */
72 /* routines used to pack registers are most definitely not reusable*/
73 /* since the pack the registers depending strictly on the MCU */
74 /*-----------------------------------------------------------------*/
76 bitVect *spiltSet = NULL ;
77 set *stackSpil = NULL;
78 bitVect *regAssigned = NULL;
81 extern void genZ80Code(iCode *);
82 bitVect *funcrUsed = NULL; /* registers used in a function */
87 /** Set to help debug register pressure related problems */
88 #define DEBUG_FAKE_EXTRA_REGS 0
90 static regs _gbz80_regs[] = {
91 { REG_GPR, C_IDX , "c", 1 },
92 { REG_GPR, B_IDX , "b", 1 },
93 { REG_CND, CND_IDX, "c", 1}
96 static regs _z80_regs[] = {
97 { REG_GPR, C_IDX , "c", 1 },
98 { REG_GPR, B_IDX , "b", 1 },
99 { REG_GPR, E_IDX , "e", 1 },
100 { REG_GPR, D_IDX , "d", 1 },
101 /* { REG_GPR, L_IDX , "l", 1 },
102 { REG_GPR, H_IDX , "h", 1 },*/
103 #if DEBUG_FAKE_EXTRA_REGS
104 { REG_GPR, M_IDX , "m", 1 },
105 { REG_GPR, N_IDX , "n", 1 },
106 { REG_GPR, O_IDX , "o", 1 },
107 { REG_GPR, P_IDX , "p", 1 },
108 { REG_GPR, Q_IDX , "q", 1 },
109 { REG_GPR, R_IDX , "r", 1 },
110 { REG_GPR, S_IDX , "s", 1 },
111 { REG_GPR, T_IDX , "t", 1 },
113 { REG_CND, CND_IDX, "c", 1}
118 /** Number of usable registers (all but C) */
119 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
120 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
122 static void spillThis (symbol *);
124 /** Allocates register of given type.
125 'type' is not used on the z80 version. It was used to select
126 between pointer and general purpose registers on the mcs51 version.
128 @return Pointer to the newly allocated register.
130 static regs *allocReg (short type)
134 for ( i = 0 ; i < _nRegs ; i++ ) {
135 /* For now we allocate from any free */
136 if (regsZ80[i].isFree ) {
137 regsZ80[i].isFree = 0;
140 bitVectSetBit(currFunc->regsUsed,i);
141 D(D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
145 D(D_ALLOC, ("allocReg: No free.\n"));
149 /** Returns pointer to register wit index number
151 regs *regWithIdx (int idx)
155 for (i=0;i < _nRegs;i++)
156 if (regsZ80[i].rIdx == idx)
159 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
160 "regWithIdx not found");
164 /** Frees a register.
166 static void freeReg (regs *reg)
168 wassert(!reg->isFree);
170 D(D_ALLOC, ("freeReg: freed %p\n", reg));
174 /** Returns number of free registers.
176 static int nFreeRegs (int type)
181 for (i = 0 ; i < _nRegs; i++ ) {
182 /* For now only one reg type */
183 if (regsZ80[i].isFree)
189 /** Free registers with type.
191 static int nfreeRegsType (int type)
194 if (type == REG_PTR) {
195 if ((nfr = nFreeRegs(type)) == 0)
196 return nFreeRegs(REG_GPR);
199 return nFreeRegs(type);
204 /*-----------------------------------------------------------------*/
205 /* allDefsOutOfRange - all definitions are out of a range */
206 /*-----------------------------------------------------------------*/
207 static bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
214 for ( i = 0 ;i < defs->size ; i++ ) {
217 if (bitVectBitValue(defs,i) &&
218 (ic = hTabItemWithKey(iCodehTab,i)) &&
219 ( ic->seq >= fseq && ic->seq <= toseq))
229 /*-----------------------------------------------------------------*/
230 /* computeSpillable - given a point find the spillable live ranges */
231 /*-----------------------------------------------------------------*/
232 static bitVect *computeSpillable (iCode *ic)
236 /* spillable live ranges are those that are live at this
237 point . the following categories need to be subtracted
239 a) - those that are already spilt
240 b) - if being used by this one
241 c) - defined by this one */
243 spillable = bitVectCopy(ic->rlive);
245 bitVectCplAnd(spillable,spiltSet); /* those already spilt */
247 bitVectCplAnd(spillable,ic->uses); /* used in this one */
248 bitVectUnSetBit(spillable,ic->defKey);
249 spillable = bitVectIntersect(spillable,regAssigned);
254 /*-----------------------------------------------------------------*/
255 /* noSpilLoc - return true if a variable has no spil location */
256 /*-----------------------------------------------------------------*/
257 static int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
259 return (sym->usl.spillLoc ? 0 : 1);
262 /*-----------------------------------------------------------------*/
263 /* hasSpilLoc - will return 1 if the symbol has spil location */
264 /*-----------------------------------------------------------------*/
265 static int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
267 return (sym->usl.spillLoc ? 1 : 0);
270 /** Will return 1 if the remat flag is set.
271 A symbol is rematerialisable if it doesnt need to be allocated
272 into registers at creation as it can be re-created at any time -
273 i.e. it's constant in some way.
275 static int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
280 /*-----------------------------------------------------------------*/
281 /* allLRs - return true for all */
282 /*-----------------------------------------------------------------*/
283 static int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
288 /*-----------------------------------------------------------------*/
289 /* liveRangesWith - applies function to a given set of live range */
290 /*-----------------------------------------------------------------*/
291 set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
292 eBBlock *ebp, iCode *ic)
297 if (!lrs || !lrs->size)
300 for ( i = 1 ; i < lrs->size ; i++ ) {
302 if (!bitVectBitValue(lrs,i))
305 /* if we don't find it in the live range
306 hash table we are in serious trouble */
307 if (!(sym = hTabItemWithKey(liveRanges,i))) {
308 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
309 "liveRangesWith could not find liveRange");
313 if (func(sym,ebp,ic) && bitVectBitValue(regAssigned,sym->key))
314 addSetHead(&rset,sym);
321 /*-----------------------------------------------------------------*/
322 /* leastUsedLR - given a set determines which is the least used */
323 /*-----------------------------------------------------------------*/
324 symbol *leastUsedLR (set *sset)
326 symbol *sym = NULL, *lsym = NULL ;
328 sym = lsym = setFirstItem(sset);
333 for (; lsym; lsym = setNextItem(sset)) {
335 /* if usage is the same then prefer
336 the spill the smaller of the two */
337 if ( lsym->used == sym->used )
338 if (getSize(lsym->type) < getSize(sym->type))
342 if (lsym->used < sym->used )
347 setToNull((void **)&sset);
352 /*-----------------------------------------------------------------*/
353 /* noOverLap - will iterate through the list looking for over lap */
354 /*-----------------------------------------------------------------*/
355 static int noOverLap (set *itmpStack, symbol *fsym)
359 for (sym = setFirstItem(itmpStack); sym;
360 sym = setNextItem(itmpStack)) {
361 if (sym->liveTo > fsym->liveFrom )
368 /*-----------------------------------------------------------------*/
369 /* isFree - will return 1 if the a free spil location is found */
370 /*-----------------------------------------------------------------*/
374 V_ARG(symbol **,sloc);
375 V_ARG(symbol *,fsym);
377 /* if already found */
381 /* if it is free && and the itmp assigned to
382 this does not have any overlapping live ranges
383 with the one currently being assigned and
384 the size can be accomodated */
386 noOverLap(sym->usl.itmpStack,fsym) &&
387 getSize(sym->type) >= getSize(fsym->type)) {
395 /*-----------------------------------------------------------------*/
396 /* createStackSpil - create a location on the stack to spil */
397 /*-----------------------------------------------------------------*/
398 symbol *createStackSpil (symbol *sym)
402 D(D_ALLOC, ("createStackSpil: for sym %p\n", sym));
404 /* first go try and find a free one that is already
405 existing on the stack */
406 if (applyToSet(stackSpil,isFree,&sloc, sym)) {
407 /* found a free one : just update & return */
408 sym->usl.spillLoc = sloc;
411 addSetHead(&sloc->usl.itmpStack,sym);
412 D(D_ALLOC, ("createStackSpil: found existing\n"));
416 /* could not then have to create one , this is the hard part
417 we need to allocate this on the stack : this is really a
418 hack!! but cannot think of anything better at this time */
420 sprintf(buffer,"sloc%d",slocNum++);
421 sloc = newiTemp(buffer);
423 /* set the type to the spilling symbol */
424 sloc->type = copyLinkChain(sym->type);
425 sloc->etype = getSpec(sloc->type);
426 SPEC_SCLS(sloc->etype) = S_AUTO ;
428 /* we don't allow it to be allocated`
429 onto the external stack since : so we
430 temporarily turn it off ; we also
431 turn off memory model to prevent
432 the spil from going to the external storage
433 and turn off overlaying
437 sloc->isref = 1; /* to prevent compiler warning */
439 /* if it is on the stack then update the stack */
440 if (IN_STACK(sloc->etype)) {
441 currFunc->stack += getSize(sloc->type);
442 stackExtend += getSize(sloc->type);
444 dataExtend += getSize(sloc->type);
446 /* add it to the stackSpil set */
447 addSetHead(&stackSpil,sloc);
448 sym->usl.spillLoc = sloc;
451 /* add it to the set of itempStack set
452 of the spill location */
453 addSetHead(&sloc->usl.itmpStack,sym);
455 D(D_ALLOC, ("createStackSpil: created new\n"));
459 /*-----------------------------------------------------------------*/
460 /* isSpiltOnStack - returns true if the spil location is on stack */
461 /*-----------------------------------------------------------------*/
462 bool isSpiltOnStack (symbol *sym)
472 /* if (sym->stackSpil) */
475 if (!sym->usl.spillLoc)
478 etype = getSpec(sym->usl.spillLoc->type);
485 /*-----------------------------------------------------------------*/
486 /* spillThis - spils a specific operand */
487 /*-----------------------------------------------------------------*/
488 static void 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))
498 createStackSpil (sym);
500 /* mark it has spilt & put it in the spilt set */
502 spiltSet = bitVectSetBit(spiltSet,sym->key);
504 bitVectUnSetBit(regAssigned,sym->key);
506 for (i = 0 ; i < sym->nRegs ; i++) {
508 freeReg(sym->regs[i]);
513 /* if spilt on stack then free up r0 & r1
514 if they could have been assigned to some
516 if (sym->usl.spillLoc && !sym->remat)
517 sym->usl.spillLoc->allocreq = 1;
521 /** Select a iTemp to spil : rather a simple procedure.
523 symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
525 bitVect *lrcs= NULL ;
529 D(D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
530 /* get the spillable live ranges */
531 lrcs = computeSpillable (ic);
533 /* get all live ranges that are rematerizable */
534 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
535 D(D_ALLOC, ("selectSpil: using remat.\n"));
536 /* return the least used of these */
537 return leastUsedLR(selectS);
541 /* get live ranges with spillLocations in direct space */
542 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
543 sym = leastUsedLR(selectS);
544 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
545 sym->usl.spillLoc->rname :
546 sym->usl.spillLoc->name));
548 /* mark it as allocation required */
549 sym->usl.spillLoc->allocreq = 1;
553 /* if the symbol is local to the block then */
554 if (forSym->liveTo < ebp->lSeq ) {
556 /* check if there are any live ranges allocated
557 to registers that are not used in this block */
558 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
559 sym = leastUsedLR(selectS);
560 /* if this is not rematerializable */
568 /* check if there are any live ranges that not
569 used in the remainder of the block */
570 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
571 sym = leastUsedLR (selectS);
581 /* find live ranges with spillocation && not used as pointers */
582 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
584 sym = leastUsedLR(selectS);
585 /* mark this as allocation required */
586 sym->usl.spillLoc->allocreq = 1;
591 /* find live ranges with spillocation */
592 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
593 D(D_ALLOC, ("selectSpil: using with spill.\n"));
594 sym = leastUsedLR(selectS);
595 sym->usl.spillLoc->allocreq = 1;
599 /* couldn't find then we need to create a spil
600 location on the stack , for which one? the least
602 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
603 D(D_ALLOC, ("selectSpil: creating new spill.\n"));
604 /* return a created spil location */
605 sym = createStackSpil(leastUsedLR(selectS));
606 sym->usl.spillLoc->allocreq = 1;
610 /* this is an extreme situation we will spill
611 this one : happens very rarely but it does happen */
612 D(D_ALLOC, ("selectSpil: using spillThis.\n"));
613 spillThis ( forSym );
618 /** Spil some variable & mark registers as free.
619 A spill occurs when an iTemp wont fit into the available registers.
621 bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
626 D(D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
628 /* get something we can spil */
629 ssym = selectSpil(ic,ebp,forSym);
631 /* mark it as spilt */
633 spiltSet = bitVectSetBit(spiltSet,ssym->key);
635 /* mark it as not register assigned &
636 take it away from the set */
637 bitVectUnSetBit(regAssigned,ssym->key);
639 /* mark the registers as free */
640 for (i = 0 ; i < ssym->nRegs ;i++ )
642 freeReg(ssym->regs[i]);
644 /* if spilt on stack then free up r0 & r1
645 if they could have been assigned to as gprs */
646 if (!ptrRegReq && isSpiltOnStack(ssym) ) {
648 spillLRWithPtrReg(ssym);
651 /* if this was a block level spil then insert push & pop
652 at the start & end of block respectively */
653 if (ssym->blockSpil) {
654 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
655 /* add push to the start of the block */
656 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
657 ebp->sch->next : ebp->sch));
658 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
659 /* add pop to the end of the block */
660 addiCodeToeBBlock(ebp,nic,NULL);
663 /* if spilt because not used in the remainder of the
664 block then add a push before this instruction and
665 a pop at the end of the block */
666 if (ssym->remainSpil) {
668 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
669 /* add push just before this instruction */
670 addiCodeToeBBlock(ebp,nic,ic);
672 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
673 /* add pop to the end of the block */
674 addiCodeToeBBlock(ebp,nic,NULL);
678 D(D_ALLOC, ("spilSomething: done.\n"));
686 /** Will try for GPR if not spil.
688 regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
692 D(D_ALLOC, ("getRegGpr: on ic %p\n", ic));
694 /* try for gpr type */
695 if ((reg = allocReg(REG_GPR))) {
696 D(D_ALLOC, ("getRegGpr: got a reg.\n"));
700 /* we have to spil */
701 if (!spilSomething (ic,ebp,sym)) {
702 D(D_ALLOC, ("getRegGpr: have to spill.\n"));
706 /* this looks like an infinite loop but
707 in really selectSpil will abort */
711 /** Symbol has a given register.
713 static bool symHasReg(symbol *sym,regs *reg)
717 for ( i = 0 ; i < sym->nRegs ; i++)
718 if (sym->regs[i] == reg)
724 /** Check the live to and if they have registers & are not spilt then
725 free up the registers
727 static void deassignLRs (iCode *ic, eBBlock *ebp)
733 for (sym = hTabFirstItem(liveRanges,&k); sym;
734 sym = hTabNextItem(liveRanges,&k)) {
737 /* if it does not end here */
738 if (sym->liveTo > ic->seq )
741 /* if it was spilt on stack then we can
742 mark the stack spil location as free */
744 if (sym->stackSpil) {
745 sym->usl.spillLoc->isFree = 1;
751 if (!bitVectBitValue(regAssigned,sym->key))
754 /* special case check if this is an IFX &
755 the privious one was a pop and the
756 previous one was not spilt then keep track
758 if (ic->op == IFX && ic->prev &&
759 ic->prev->op == IPOP &&
760 !ic->prev->parmPush &&
761 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
762 psym = OP_SYMBOL(IC_LEFT(ic->prev));
764 D(D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
769 bitVectUnSetBit(regAssigned,sym->key);
771 /* if the result of this one needs registers
772 and does not have it then assign it right
775 ! (SKIP_IC2(ic) || /* not a special icode */
776 ic->op == JUMPTABLE ||
781 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
782 result->liveTo > ic->seq && /* and will live beyond this */
783 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
784 result->regType == sym->regType && /* same register types */
785 result->nRegs && /* which needs registers */
786 ! result->isspilt && /* and does not already have them */
788 ! bitVectBitValue(regAssigned,result->key) &&
789 /* the number of free regs + number of regs in this LR
790 can accomodate the what result Needs */
791 ((nfreeRegsType(result->regType) +
792 sym->nRegs) >= result->nRegs)
794 for (i = 0 ; i < result->nRegs ; i++) {
796 result->regs[i] = sym->regs[i] ;
798 result->regs[i] = getRegGpr (ic,ebp,result);
800 /* if the allocation falied which means
801 this was spilt then break */
802 if (!result->regs[i]) {
809 regAssigned = bitVectSetBit(regAssigned,result->key);
812 /* free the remaining */
813 for (; i < sym->nRegs ; i++) {
815 if (!symHasReg(psym,sym->regs[i]))
816 freeReg(sym->regs[i]);
818 freeReg(sym->regs[i]);
819 // sym->regs[i] = NULL;
826 /** Reassign this to registers.
828 static void reassignLR (operand *op)
830 symbol *sym = OP_SYMBOL(op);
833 D(D_ALLOC, ("reassingLR: on sym %p\n", sym));
835 /* not spilt any more */
836 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
837 bitVectUnSetBit(spiltSet,sym->key);
839 regAssigned = bitVectSetBit(regAssigned,sym->key);
843 for (i=0;i<sym->nRegs;i++)
844 sym->regs[i]->isFree = 0;
847 /** Determines if allocating will cause a spill.
849 static int willCauseSpill ( int nr, int rt)
851 /* first check if there are any avlb registers
852 of te type required */
853 if (nFreeRegs(0) >= nr)
856 /* it will cause a spil */
860 /** The allocator can allocate same registers to result and operand,
861 if this happens make sure they are in the same position as the operand
862 otherwise chaos results.
864 static void positionRegs (symbol *result, symbol *opsym, int lineno)
866 int count = min(result->nRegs,opsym->nRegs);
867 int i , j = 0, shared = 0;
869 D(D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
871 /* if the result has been spilt then cannot share */
876 /* first make sure that they actually share */
877 for ( i = 0 ; i < count; i++ ) {
878 for (j = 0 ; j < count ; j++ ) {
879 if (result->regs[i] == opsym->regs[j] && i !=j) {
887 regs *tmp = result->regs[i];
888 result->regs[i] = result->regs[j];
889 result->regs[j] = tmp;
894 /** Try to allocate a pair of registers to the symbol.
896 bool tryAllocatingRegPair(symbol *sym)
899 wassert(sym->nRegs == 2);
900 for ( i = 0 ; i < _nRegs ; i+=2 ) {
901 if ((regsZ80[i].isFree)&&(regsZ80[i+1].isFree)) {
902 regsZ80[i].isFree = 0;
903 sym->regs[0] = ®sZ80[i];
904 regsZ80[i+1].isFree = 0;
905 sym->regs[1] = ®sZ80[i+1];
908 bitVectSetBit(currFunc->regsUsed,i);
910 bitVectSetBit(currFunc->regsUsed,i+1);
912 D(D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
916 D(D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
920 /** Serially allocate registers to the variables.
921 This is the main register allocation function. It is called after
924 static void serialRegAssign (eBBlock **ebbs, int count)
929 for (i = 0; i < count ; i++ ) {
933 if (ebbs[i]->noPath &&
934 (ebbs[i]->entryLabel != entryLabel &&
935 ebbs[i]->entryLabel != returnLabel ))
938 /* of all instructions do */
939 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
941 /* if this is an ipop that means some live
942 range will have to be assigned again */
943 if (ic->op == IPOP) {
945 reassignLR (IC_LEFT(ic));
948 /* if result is present && is a true symbol */
949 if (IC_RESULT(ic) && ic->op != IFX &&
950 IS_TRUE_SYMOP(IC_RESULT(ic)))
951 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
953 /* take away registers from live
954 ranges that end at this instruction */
955 deassignLRs (ic, ebbs[i]) ;
957 /* some don't need registers */
958 /* MLH: removed RESULT and POINTER_SET condition */
960 ic->op == JUMPTABLE ||
966 /* now we need to allocate registers only for the result */
968 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
973 D(D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
975 /* if it does not need or is spilt
976 or is already assigned to registers
977 or will not live beyond this instructions */
980 bitVectBitValue(regAssigned,sym->key) ||
981 sym->liveTo <= ic->seq) {
982 D(D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
986 /* if some liverange has been spilt at the block level
987 and this one live beyond this block then spil this
989 if (blockSpil && sym->liveTo > ebbs[i]->lSeq) {
990 D(D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
994 /* if trying to allocate this will cause
995 a spill and there is nothing to spill
996 or this one is rematerializable then
998 willCS = willCauseSpill(sym->nRegs,sym->regType);
999 spillable = computeSpillable(ic);
1001 (willCS && bitVectIsZero(spillable) ) ) {
1003 D(D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1009 /* if it has a spillocation & is used less than
1010 all other live ranges then spill this */
1011 if ( willCS && sym->usl.spillLoc ) {
1014 leastUsedLR(liveRangesWith (spillable ,
1019 leastUsed->used > sym->used) {
1025 /* else we assign registers to it */
1026 regAssigned = bitVectSetBit(regAssigned,sym->key);
1028 /* Special case: Try to fit into a reg pair if
1030 D(D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1031 if ((sym->nRegs == 2)&&tryAllocatingRegPair(sym)) {
1034 for (j = 0 ; j < sym->nRegs ;j++ ) {
1035 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
1037 /* if the allocation falied which means
1038 this was spilt then break */
1039 if (!sym->regs[j]) {
1040 D(D_ALLOC, ("Couldnt alloc (spill)\n"))
1045 /* if it shares registers with operands make sure
1046 that they are in the same position */
1047 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
1048 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
1049 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1050 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
1051 /* do the same for the right operand */
1052 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
1053 OP_SYMBOL(IC_RIGHT(ic))->nRegs)
1054 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1055 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1062 /*-----------------------------------------------------------------*/
1063 /* rUmaskForOp :- returns register mask for an operand */
1064 /*-----------------------------------------------------------------*/
1065 bitVect *rUmaskForOp (operand *op)
1071 /* only temporaries are assigned registers */
1075 sym = OP_SYMBOL(op);
1077 /* if spilt or no registers assigned to it
1079 if (sym->isspilt || !sym->nRegs)
1082 rumask = newBitVect(_nRegs);
1084 for (j = 0; j < sym->nRegs; j++) {
1085 rumask = bitVectSetBit(rumask, sym->regs[j]->rIdx);
1091 /** Returns bit vector of registers used in iCode.
1093 bitVect *regsUsedIniCode (iCode *ic)
1095 bitVect *rmask = newBitVect(_nRegs);
1097 /* do the special cases first */
1098 if (ic->op == IFX ) {
1099 rmask = bitVectUnion(rmask,
1100 rUmaskForOp(IC_COND(ic)));
1104 /* for the jumptable */
1105 if (ic->op == JUMPTABLE) {
1106 rmask = bitVectUnion(rmask,
1107 rUmaskForOp(IC_JTCOND(ic)));
1112 /* of all other cases */
1114 rmask = bitVectUnion(rmask,
1115 rUmaskForOp(IC_LEFT(ic)));
1119 rmask = bitVectUnion(rmask,
1120 rUmaskForOp(IC_RIGHT(ic)));
1123 rmask = bitVectUnion(rmask,
1124 rUmaskForOp(IC_RESULT(ic)));
1130 /** For each instruction will determine the regsUsed.
1132 static void createRegMask (eBBlock **ebbs, int count)
1136 /* for all blocks */
1137 for (i = 0; i < count ; i++ ) {
1140 if ( ebbs[i]->noPath &&
1141 ( ebbs[i]->entryLabel != entryLabel &&
1142 ebbs[i]->entryLabel != returnLabel ))
1145 /* for all instructions */
1146 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1150 if (SKIP_IC2(ic) || !ic->rlive)
1153 /* first mark the registers used in this
1155 ic->rUsed = regsUsedIniCode(ic);
1156 funcrUsed = bitVectUnion(funcrUsed,ic->rUsed);
1158 /* now create the register mask for those
1159 registers that are in use : this is a
1160 super set of ic->rUsed */
1161 ic->rMask = newBitVect(_nRegs+1);
1163 /* for all live Ranges alive at this point */
1164 for (j = 1; j < ic->rlive->size; j++ ) {
1168 /* if not alive then continue */
1169 if (!bitVectBitValue(ic->rlive,j))
1172 /* find the live range we are interested in */
1173 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1174 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1175 "createRegMask cannot find live range");
1179 /* if no register assigned to it */
1180 if (!sym->nRegs || sym->isspilt)
1183 /* for all the registers allocated to it */
1184 for (k = 0 ; k < sym->nRegs ;k++)
1187 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1193 /** Returns the rematerialized string for a remat var.
1195 char *rematStr (symbol *sym)
1198 iCode *ic = sym->rematiCode;
1202 /* if plus or minus print the right hand side */
1203 if (ic->op == '+' || ic->op == '-') {
1204 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1207 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1210 /* we reached the end */
1211 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1218 /*-----------------------------------------------------------------*/
1219 /* regTypeNum - computes the type & number of registers required */
1220 /*-----------------------------------------------------------------*/
1221 static void regTypeNum (void)
1226 /* for each live range do */
1227 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1228 sym = hTabNextItem(liveRanges,&k)) {
1230 /* if used zero times then no registers needed */
1231 if ((sym->liveTo - sym->liveFrom) == 0)
1234 D(D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1236 /* if the live range is a temporary */
1239 /* if the type is marked as a conditional */
1240 if (sym->regType == REG_CND)
1243 /* if used in return only then we don't
1245 if (sym->ruonly || sym->accuse) {
1246 if (IS_AGGREGATE(sym->type) || sym->isptr)
1247 sym->type = aggrToPtr(sym->type,FALSE);
1251 /* if not then we require registers */
1252 D(D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE(sym->type) || sym->isptr, sym->nRegs, sym->type));
1253 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1254 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1255 getSize(sym->type));
1256 D(D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1258 D(D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1260 if (sym->nRegs > 4) {
1261 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1262 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1265 /* determine the type of register required */
1266 /* Always general purpose */
1267 sym->regType = REG_GPR ;
1270 /* for the first run we don't provide */
1271 /* registers for true symbols we will */
1272 /* see how things go */
1273 D(D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1280 /** Mark all registers as free.
1282 static void freeAllRegs()
1286 D(D_ALLOC, ("freeAllRegs: running.\n"));
1288 for (i=0;i< _nRegs;i++ )
1289 regsZ80[i].isFree = 1;
1292 /*-----------------------------------------------------------------*/
1293 /* deallocStackSpil - this will set the stack pointer back */
1294 /*-----------------------------------------------------------------*/
1295 DEFSETFUNC(deallocStackSpil)
1303 /** Register reduction for assignment.
1305 static int packRegsForAssign (iCode *ic,eBBlock *ebp)
1309 D(D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1312 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) ||*/
1313 !IS_ITEMP(IC_RIGHT(ic)) ||
1314 OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
1315 OP_SYMBOL(IC_RIGHT(ic))->isind)
1319 /* if the true symbol is defined in far space or on stack
1320 then we should not since this will increase register pressure */
1321 if (isOperandInFarSpace(IC_RESULT(ic))) {
1322 if ((dic = farSpacePackable(ic)))
1329 /* find the definition of iTempNN scanning backwards if we find a
1330 a use of the true symbol in before we find the definition then
1332 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1333 /* if there is a function call and this is
1334 a parameter & not my parameter then don't pack it */
1335 if ( (dic->op == CALL || dic->op == PCALL) &&
1336 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1337 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1345 if (IS_SYMOP(IC_RESULT(dic)) &&
1346 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1350 if (IS_SYMOP(IC_RIGHT(dic)) &&
1351 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1352 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1357 if (IS_SYMOP(IC_LEFT(dic)) &&
1358 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1359 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1364 if (POINTER_SET(dic) &&
1365 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1373 return 0 ; /* did not find */
1375 /* if the result is on stack or iaccess then it must be
1376 the same atleast one of the operands */
1377 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1378 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1380 /* the operation has only one symbol
1381 operator then we can pack */
1382 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1383 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1386 if (!((IC_LEFT(dic) &&
1387 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1389 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1393 /* found the definition */
1394 /* replace the result with the result of */
1395 /* this assignment and remove this assignment */
1396 IC_RESULT(dic) = IC_RESULT(ic) ;
1398 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1399 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1401 /* delete from liverange table also
1402 delete from all the points inbetween and the new
1404 for ( sic = dic; sic != ic ; sic = sic->next ) {
1405 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1406 if (IS_ITEMP(IC_RESULT(dic)))
1407 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1410 remiCodeFromeBBlock(ebp,ic);
1414 /** Scanning backwards looks for first assig found.
1416 iCode *findAssignToSym (operand *op,iCode *ic)
1420 for (dic = ic->prev ; dic ; dic = dic->prev) {
1422 /* if definition by assignment */
1423 if (dic->op == '=' &&
1424 !POINTER_SET(dic) &&
1425 IC_RESULT(dic)->key == op->key)
1426 /* && IS_TRUE_SYMOP(IC_RIGHT(dic))*/
1429 /* we are interested only if defined in far space */
1430 /* or in stack space in case of + & - */
1432 /* if assigned to a non-symbol then return
1434 if (!IS_SYMOP(IC_RIGHT(dic)))
1437 /* if the symbol is in far space then
1439 if (isOperandInFarSpace(IC_RIGHT(dic)))
1442 /* for + & - operations make sure that
1443 if it is on the stack it is the same
1444 as one of the three operands */
1445 if ((ic->op == '+' || ic->op == '-') &&
1446 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1448 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1449 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1450 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1458 /* if we find an usage then we cannot delete it */
1459 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1462 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1465 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1469 /* now make sure that the right side of dic
1470 is not defined between ic & dic */
1472 iCode *sic = dic->next ;
1474 for (; sic != ic ; sic = sic->next)
1475 if (IC_RESULT(sic) &&
1476 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1485 /*-----------------------------------------------------------------*/
1486 /* packRegsForSupport :- reduce some registers for support calls */
1487 /*-----------------------------------------------------------------*/
1488 static int packRegsForSupport (iCode *ic, eBBlock *ebp)
1491 /* for the left & right operand :- look to see if the
1492 left was assigned a true symbol in far space in that
1493 case replace them */
1494 D(D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1496 if (IS_ITEMP(IC_LEFT(ic)) &&
1497 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1498 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1504 /* found it we need to remove it from the
1506 for ( sic = dic; sic != ic ; sic = sic->next )
1507 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1509 IC_LEFT(ic)->operand.symOperand =
1510 IC_RIGHT(dic)->operand.symOperand;
1511 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1512 remiCodeFromeBBlock(ebp,dic);
1516 /* do the same for the right operand */
1519 IS_ITEMP(IC_RIGHT(ic)) &&
1520 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1521 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1527 /* found it we need to remove it from the block */
1528 for ( sic = dic; sic != ic ; sic = sic->next )
1529 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1531 IC_RIGHT(ic)->operand.symOperand =
1532 IC_RIGHT(dic)->operand.symOperand;
1533 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1535 remiCodeFromeBBlock(ebp,dic);
1542 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1544 /** Will reduce some registers for single use.
1546 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1551 D(D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1553 /* if returning a literal then do nothing */
1557 /* only upto 2 bytes since we cannot predict
1558 the usage of b, & acc */
1559 if (getSize(operandType(op)) > 2 &&
1564 /* this routine will mark the a symbol as used in one
1565 instruction use only && if the defintion is local
1566 (ie. within the basic block) && has only one definition &&
1567 that definiion is either a return value from a
1568 function or does not contain any variables in
1570 uses = bitVectCopy(OP_USES(op));
1571 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1572 if (!bitVectIsZero(uses)) /* has other uses */
1575 /* if it has only one defintion */
1576 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1577 return NULL ; /* has more than one definition */
1579 /* get the that definition */
1581 hTabItemWithKey(iCodehTab,
1582 bitVectFirstBit(OP_DEFS(op)))))
1585 /* found the definition now check if it is local */
1586 if (dic->seq < ebp->fSeq ||
1587 dic->seq > ebp->lSeq)
1588 return NULL ; /* non-local */
1590 /* now check if it is the return from a function call */
1591 if (dic->op == CALL || dic->op == PCALL ) {
1592 if (ic->op != SEND && ic->op != RETURN) {
1593 OP_SYMBOL(op)->ruonly = 1;
1599 /* otherwise check that the definition does
1600 not contain any symbols in far space */
1601 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1602 isOperandInFarSpace(IC_RIGHT(dic)) ||
1603 IS_OP_RUONLY(IC_LEFT(ic)) ||
1604 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1608 /* if pointer set then make sure the pointer is one byte */
1609 if (POINTER_SET(dic))
1612 if (POINTER_GET(dic))
1617 /* also make sure the intervenening instructions
1618 don't have any thing in far space */
1619 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1620 /* if there is an intervening function call then no */
1621 if (dic->op == CALL || dic->op == PCALL)
1623 /* if pointer set then make sure the pointer
1625 if (POINTER_SET(dic))
1628 if (POINTER_GET(dic))
1631 /* if address of & the result is remat the okay */
1632 if (dic->op == ADDRESS_OF &&
1633 OP_SYMBOL(IC_RESULT(dic))->remat)
1636 /* if left or right or result is in far space */
1637 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1638 isOperandInFarSpace(IC_RIGHT(dic)) ||
1639 isOperandInFarSpace(IC_RESULT(dic)) ||
1640 IS_OP_RUONLY(IC_LEFT(dic)) ||
1641 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1642 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1647 OP_SYMBOL(op)->ruonly = 1;
1651 /*-----------------------------------------------------------------*/
1652 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1653 /*-----------------------------------------------------------------*/
1654 static bool isBitwiseOptimizable (iCode *ic)
1656 sym_link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1658 /* bitwise operations are considered optimizable
1659 under the following conditions (Jean-Louis VERN)
1671 if (IS_LITERAL(rtype))
1677 Certian assignments involving pointers can be temporarly stored
1688 /** Pack registers for acc use.
1689 When the result of this operation is small and short lived it may
1690 be able to be stored in the accumelator.
1692 static void packRegsForAccUse (iCode *ic)
1696 /* if + or - then it has to be one byte result */
1697 if ((ic->op == '+' || ic->op == '-')
1698 && getSize(operandType(IC_RESULT(ic))) > 1)
1701 /* if shift operation make sure right side is not a literal */
1702 if (ic->op == RIGHT_OP &&
1703 (isOperandLiteral(IC_RIGHT(ic)) ||
1704 getSize(operandType(IC_RESULT(ic))) > 1))
1707 if (ic->op == LEFT_OP &&
1708 ( isOperandLiteral(IC_RIGHT(ic)) ||
1709 getSize(operandType(IC_RESULT(ic))) > 1))
1712 /* has only one definition */
1713 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1716 /* has only one use */
1717 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1720 /* and the usage immediately follows this iCode */
1721 if (!(uic = hTabItemWithKey(iCodehTab,
1722 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1725 if (ic->next != uic)
1728 /* if it is a conditional branch then we definitely can */
1729 if (uic->op == IFX )
1732 if ( uic->op == JUMPTABLE )
1736 /* if the usage is not is an assignment or an
1737 arithmetic / bitwise / shift operation then not */
1738 if (POINTER_SET(uic) &&
1739 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1743 if (uic->op != '=' &&
1744 !IS_ARITHMETIC_OP(uic) &&
1745 !IS_BITWISE_OP(uic) &&
1746 uic->op != LEFT_OP &&
1747 uic->op != RIGHT_OP )
1750 /* if used in ^ operation then make sure right is not a
1752 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1755 /* if shift operation make sure right side is not a literal */
1756 if (uic->op == RIGHT_OP &&
1757 ( isOperandLiteral(IC_RIGHT(uic)) ||
1758 getSize(operandType(IC_RESULT(uic))) > 1))
1761 if (uic->op == LEFT_OP &&
1762 ( isOperandLiteral(IC_RIGHT(uic)) ||
1763 getSize(operandType(IC_RESULT(uic))) > 1))
1767 /* make sure that the result of this icode is not on the
1768 stack, since acc is used to compute stack offset */
1769 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1770 OP_SYMBOL(IC_RESULT(uic))->onStack)
1775 /* if either one of them in far space then we cannot */
1776 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1777 isOperandInFarSpace(IC_LEFT(uic))) ||
1778 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1779 isOperandInFarSpace(IC_RIGHT(uic))))
1783 /* if the usage has only one operand then we can */
1784 if (IC_LEFT(uic) == NULL ||
1785 IC_RIGHT(uic) == NULL)
1788 /* make sure this is on the left side if not
1789 a '+' since '+' is commutative */
1790 if (ic->op != '+' &&
1791 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1794 /* if one of them is a literal then we can */
1795 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1796 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1801 /** This is confusing :) Guess for now */
1802 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1803 (IS_ITEMP(IC_RIGHT(uic)) ||
1804 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1807 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1808 (IS_ITEMP(IC_LEFT(uic)) ||
1809 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1813 OP_SYMBOL(IC_RESULT(ic))->accuse = ACCUSE_A;
1816 static void packRegsForHLUse (iCode *ic)
1823 /* has only one definition */
1824 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1827 /* has only one use */
1828 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1831 /* and the usage immediately follows this iCode */
1832 if (!(uic = hTabItemWithKey(iCodehTab,
1833 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1836 if (ic->next != uic)
1839 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
1841 if (ic->op == CALL && IC_LEFT(ic)->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
1845 OP_SYMBOL(IC_RESULT(ic))->accuse = ACCUSE_HL;
1848 bool opPreservesA(iCode *ic, iCode *uic)
1850 /* if it is a conditional branch then we definitely can */
1851 if (uic->op == IFX )
1854 if ( uic->op == JUMPTABLE )
1857 /* if the usage has only one operand then we can */
1858 /* PENDING: check */
1859 if (IC_LEFT(uic) == NULL ||
1860 IC_RIGHT(uic) == NULL)
1863 /* PENDING: check this rule */
1864 if (getSize(operandType(IC_RESULT(uic))) > 1) {
1870 !IS_ARITHMETIC_OP(uic) (sub requires A)
1874 !IS_BITWISE_OP(uic) &&
1877 !POINTER_GET(uic) &&
1879 uic->op != LEFT_OP &&
1880 uic->op != RIGHT_OP &&*/
1887 if (!IC_LEFT(uic) || !IC_RESULT(ic))
1890 /** This is confusing :) Guess for now */
1891 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1892 (IS_ITEMP(IC_RIGHT(uic)) ||
1893 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1896 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1897 (IS_ITEMP(IC_LEFT(uic)) ||
1898 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1904 static void joinPushes(iCode *ic)
1907 if (ic->op == IPUSH &&
1908 isOperandLiteral(IC_LEFT(ic)) &&
1909 getSize(operandType(IC_LEFT(ic))) == 1 &&
1910 ic->next->op == IPUSH &&
1911 isOperandLiteral(IC_LEFT(ic->next)) &&
1912 getSize(operandType(IC_LEFT(ic->next))) == 1) {
1913 /* This is a bit tricky as michaelh doesnt know what he's doing.
1915 /* First upgrade the size of (first) to int */
1916 SPEC_NOUN(operandType(IC_LEFT(ic))) = V_INT;
1917 SPEC_SHORT(operandType(IC_LEFT(ic))) = 0;
1920 /* Now get and join the values */
1921 value * val = aop->aopu.aop_lit;
1922 /* if it is a float then it gets tricky */
1923 /* otherwise it is fairly simple */
1924 if (!IS_FLOAT(val->type)) {
1925 unsigned long v = floatFromVal(val);
1928 printf("Size %u\n", getSize(operandType(IC_LEFT(ic))));
1929 ic->next = ic->next->next;
1934 /** Pack registers for acc use.
1935 When the result of this operation is small and short lived it may
1936 be able to be stored in the accumulator.
1938 Note that the 'A preserving' list is currently emperical :)e
1940 static void packRegsForAccUse2(iCode *ic)
1944 D(D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
1946 /* Filter out all but those 'good' commands */
1950 !IS_BITWISE_OP(ic) &&
1957 /* if + or - then it has to be one byte result.
1960 if ((ic->op == '+' || ic->op == '-')
1961 && getSize(operandType(IC_RESULT(ic))) > 1)
1964 /* if shift operation make sure right side is not a literal.
1968 if (ic->op == RIGHT_OP &&
1969 (isOperandLiteral(IC_RIGHT(ic)) ||
1970 getSize(operandType(IC_RESULT(ic))) > 1))
1973 if (ic->op == LEFT_OP &&
1974 ( isOperandLiteral(IC_RIGHT(ic)) ||
1975 getSize(operandType(IC_RESULT(ic))) > 1))
1979 /* has only one definition */
1980 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1) {
1984 /* Right. We may be able to propagate it through if:
1985 For each in the chain of uses the intermediate is OK.
1987 /* Get next with 'uses result' bit on
1988 If this->next == next
1989 Validate use of next
1990 If OK, increase count
1992 /* and the usage immediately follows this iCode */
1993 if (!(uic = hTabItemWithKey(iCodehTab,
1994 bitVectFirstBit(OP_USES(IC_RESULT(ic)))))) {
1999 /* Create a copy of the OP_USES bit vect */
2000 bitVect *uses = bitVectCopy(OP_USES(IC_RESULT(ic)));
2002 iCode *scan = ic, *next;
2005 setBit = bitVectFirstBit(uses);
2006 next = hTabItemWithKey(iCodehTab, setBit);
2007 if (scan->next == next) {
2008 bitVectUnSetBit(uses, setBit);
2009 /* Still contigous. */
2010 if (!opPreservesA(ic, next)) {
2018 } while (!bitVectIsZero(uses));
2019 OP_SYMBOL(IC_RESULT(ic))->accuse = ACCUSE_A;
2023 /* OLD CODE FOLLOWS */
2024 /* if it is a conditional branch then we definitely can
2028 if (uic->op == IFX )
2032 if ( uic->op == JUMPTABLE )
2036 /* if the usage is not is an assignment or an
2037 arithmetic / bitwise / shift operation then not.
2038 MLH: Pending: Invalid. Our pointer sets are always peechy.
2041 if (POINTER_SET(uic) &&
2042 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1) {
2043 printf("e5 %u\n", getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)));
2049 if (uic->op != '=' &&
2050 !IS_ARITHMETIC_OP(uic) &&
2051 !IS_BITWISE_OP(uic) &&
2052 uic->op != LEFT_OP &&
2053 uic->op != RIGHT_OP ) {
2058 /* if used in ^ operation then make sure right is not a
2060 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
2063 /* if shift operation make sure right side is not a literal */
2064 if (uic->op == RIGHT_OP &&
2065 ( isOperandLiteral(IC_RIGHT(uic)) ||
2066 getSize(operandType(IC_RESULT(uic))) > 1))
2069 if (uic->op == LEFT_OP &&
2070 ( isOperandLiteral(IC_RIGHT(uic)) ||
2071 getSize(operandType(IC_RESULT(uic))) > 1))
2075 /* make sure that the result of this icode is not on the
2076 stack, since acc is used to compute stack offset */
2077 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
2078 OP_SYMBOL(IC_RESULT(uic))->onStack)
2083 /* if either one of them in far space then we cannot */
2084 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
2085 isOperandInFarSpace(IC_LEFT(uic))) ||
2086 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
2087 isOperandInFarSpace(IC_RIGHT(uic))))
2091 /* if the usage has only one operand then we can */
2092 if (IC_LEFT(uic) == NULL ||
2093 IC_RIGHT(uic) == NULL)
2096 /* make sure this is on the left side if not
2097 a '+' since '+' is commutative */
2098 if (ic->op != '+' &&
2099 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
2102 /* if one of them is a literal then we can */
2103 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
2104 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
2109 /** This is confusing :) Guess for now */
2110 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2111 (IS_ITEMP(IC_RIGHT(uic)) ||
2112 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
2115 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2116 (IS_ITEMP(IC_LEFT(uic)) ||
2117 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
2121 printf("acc ok!\n");
2122 OP_SYMBOL(IC_RESULT(ic))->accuse = ACCUSE_A;
2125 /** Does some transformations to reduce register pressure.
2127 static void packRegisters (eBBlock *ebp)
2132 D(D_ALLOC, ("packRegisters: entered.\n"));
2134 while (1 && !DISABLE_PACK_ASSIGN) {
2136 /* look for assignments of the form */
2137 /* iTempNN = TRueSym (someoperation) SomeOperand */
2139 /* TrueSym := iTempNN:1 */
2140 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2141 /* find assignment of the form TrueSym := iTempNN:1 */
2142 if (ic->op == '=' && !POINTER_SET(ic))
2143 change += packRegsForAssign(ic,ebp);
2149 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2150 /* Safe: address of a true sym is always constant. */
2151 /* if this is an itemp & result of a address of a true sym
2152 then mark this as rematerialisable */
2153 D(D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2155 if (ic->op == ADDRESS_OF &&
2156 IS_ITEMP(IC_RESULT(ic)) &&
2157 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
2158 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2159 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
2161 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2162 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2163 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2166 /* Safe: just propagates the remat flag */
2167 /* if straight assignment then carry remat flag if this is the
2169 if (ic->op == '=' &&
2171 IS_SYMOP(IC_RIGHT(ic)) &&
2172 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2173 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
2175 OP_SYMBOL(IC_RESULT(ic))->remat =
2176 OP_SYMBOL(IC_RIGHT(ic))->remat;
2177 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
2178 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
2181 /* if the condition of an if instruction is defined in the
2182 previous instruction then mark the itemp as a conditional */
2183 if ((IS_CONDITIONAL(ic) ||
2184 ( ( ic->op == BITWISEAND ||
2187 isBitwiseOptimizable(ic))) &&
2188 ic->next && ic->next->op == IFX &&
2189 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
2190 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
2192 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
2197 /* reduce for support function calls */
2198 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
2199 packRegsForSupport(ic,ebp);
2203 /* some cases the redundant moves can
2204 can be eliminated for return statements */
2205 if ((ic->op == RETURN || ic->op == SEND) &&
2206 !isOperandInFarSpace(IC_LEFT(ic)) &&
2208 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2210 /* if pointer set & left has a size more than
2211 one and right is not in far space */
2212 if (POINTER_SET(ic) &&
2213 /* MLH: no such thing.
2214 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2215 !OP_SYMBOL(IC_RESULT(ic))->remat &&
2216 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
2217 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 ) {
2219 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
2222 /* if pointer get */
2223 if (!DISABLE_PACK_ONE_USE &&
2225 /* MLH: dont have far space
2226 !isOperandInFarSpace(IC_RESULT(ic))&& */
2227 !OP_SYMBOL(IC_LEFT(ic))->remat &&
2228 !IS_OP_RUONLY(IC_RESULT(ic)) &&
2229 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 ) {
2231 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2233 /* pack registers for accumulator use, when the result of an
2234 arithmetic or bit wise operation has only one use, that use is
2235 immediately following the defintion and the using iCode has
2236 only one operand or has two operands but one is literal & the
2237 result of that operation is not on stack then we can leave the
2238 result of this operation in acc:b combination */
2240 if (!DISABLE_PACK_HL && IS_ITEMP(IC_RESULT(ic))) {
2241 packRegsForHLUse(ic);
2244 if ((IS_ARITHMETIC_OP(ic)
2245 || IS_BITWISE_OP(ic)
2246 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2248 IS_ITEMP(IC_RESULT(ic)) &&
2249 getSize(operandType(IC_RESULT(ic))) <= 2)
2250 packRegsForAccUse (ic);
2252 if (!DISABLE_PACK_ACC && IS_ITEMP(IC_RESULT(ic)) &&
2253 getSize(operandType(IC_RESULT(ic))) == 1) {
2254 packRegsForAccUse2(ic);
2261 /*-----------------------------------------------------------------*/
2262 /* assignRegisters - assigns registers to each live range as need */
2263 /*-----------------------------------------------------------------*/
2264 void z80_assignRegisters (eBBlock **ebbs, int count)
2269 D(D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2271 setToNull((void *)&funcrUsed);
2272 stackExtend = dataExtend = 0;
2275 /* DE is required for the code gen. */
2276 _nRegs = GBZ80_MAX_REGS;
2277 regsZ80 = _gbz80_regs;
2280 _nRegs = Z80_MAX_REGS;
2281 regsZ80 = _z80_regs;
2284 /* change assignments this will remove some
2285 live ranges reducing some register pressure */
2286 for (i = 0 ; i < count ;i++ )
2287 packRegisters (ebbs[i]);
2289 if (options.dump_pack)
2290 dumpEbbsToFileExt(".dumppack",ebbs,count);
2292 /* first determine for each live range the number of
2293 registers & the type of registers required for each */
2296 /* and serially allocate registers */
2297 serialRegAssign(ebbs,count);
2299 /* if stack was extended then tell the user */
2301 /* werror(W_TOOMANY_SPILS,"stack", */
2302 /* stackExtend,currFunc->name,""); */
2307 /* werror(W_TOOMANY_SPILS,"data space", */
2308 /* dataExtend,currFunc->name,""); */
2312 if (options.dump_rassgn)
2313 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
2315 /* after that create the register mask
2316 for each of the instruction */
2317 createRegMask (ebbs,count);
2319 /* now get back the chain */
2320 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
2322 /* redo that offsets for stacked automatic variables */
2323 redoStackOffsets ();
2327 /* free up any stackSpil locations allocated */
2328 applyToSet(stackSpil,deallocStackSpil);
2330 setToNull((void **)&stackSpil);
2331 setToNull((void **)&spiltSet);
2332 /* mark all registers as free */