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, ix, iy, and a are reserved for the code generator,
11 leaving bc and de for allocation. The extra register pressure
12 from reserving hl is made up for by how much easier the sub
13 operations become. You could swap hl for iy if the undocumented
14 iyl/iyh instructions are available.
16 The stack frame is the common ix-bp style. Basically:
21 ix+0: calling functions ix
24 sp: end of local varibles
26 There is currently no support for bit spaces or banked functions.
28 This program is free software; you can redistribute it and/or
29 modify it under the terms of the GNU General Public License as
30 published by the Free Software Foundation; either version 2, or (at
31 your option) any later version. This program is distributed in the
32 hope that it will be useful, but WITHOUT ANY WARRANTY; without even
33 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
34 PURPOSE. See the GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
39 USA. In other words, you are welcome to use, share and improve
40 this program. You are forbidden to forbid anyone else to use,
41 share and improve what you give them. Help stamp out
49 DISABLE_PACK_ASSIGN = 0,
53 /*-----------------------------------------------------------------*/
54 /* At this point we start getting processor specific although */
55 /* some routines are non-processor specific & can be reused when */
56 /* targetting other processors. The decision for this will have */
57 /* to be made on a routine by routine basis */
58 /* routines used to pack registers are most definitely not reusable*/
59 /* since the pack the registers depending strictly on the MCU */
60 /*-----------------------------------------------------------------*/
62 bitVect *spiltSet = NULL ;
63 set *stackSpil = NULL;
64 bitVect *regAssigned = NULL;
67 extern void genZ80Code(iCode *);
68 bitVect *funcrUsed = NULL; /* registers used in a function */
73 /** Set to help debug register pressure related problems */
74 #define DEBUG_FAKE_EXTRA_REGS 0
76 static regs _gbz80_regs[] = {
77 { REG_GPR, C_IDX , "c", 1 },
78 { REG_GPR, B_IDX , "b", 1 },
79 { REG_CND, CND_IDX, "c", 1}
82 static regs _z80_regs[] = {
83 { REG_GPR, C_IDX , "c", 1 },
84 { REG_GPR, B_IDX , "b", 1 },
85 { REG_GPR, E_IDX , "e", 1 },
86 { REG_GPR, D_IDX , "d", 1 },
87 /* { REG_GPR, L_IDX , "l", 1 },
88 { REG_GPR, H_IDX , "h", 1 },*/
89 #if DEBUG_FAKE_EXTRA_REGS
90 { REG_GPR, M_IDX , "m", 1 },
91 { REG_GPR, N_IDX , "n", 1 },
92 { REG_GPR, O_IDX , "o", 1 },
93 { REG_GPR, P_IDX , "p", 1 },
94 { REG_GPR, Q_IDX , "q", 1 },
95 { REG_GPR, R_IDX , "r", 1 },
96 { REG_GPR, S_IDX , "s", 1 },
97 { REG_GPR, T_IDX , "t", 1 },
99 { REG_CND, CND_IDX, "c", 1}
104 /** Number of usable registers (all but C) */
105 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
106 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
108 static void spillThis (symbol *);
110 /** Allocates register of given type.
111 'type' is not used on the z80 version. It was used to select
112 between pointer and general purpose registers on the mcs51 version.
114 @return Pointer to the newly allocated register.
116 static regs *allocReg (short type)
120 for ( i = 0 ; i < _nRegs ; i++ ) {
121 /* For now we allocate from any free */
122 if (regsZ80[i].isFree ) {
123 regsZ80[i].isFree = 0;
126 bitVectSetBit(currFunc->regsUsed,i);
133 /** Returns pointer to register wit index number
135 regs *regWithIdx (int idx)
139 for (i=0;i < _nRegs;i++)
140 if (regsZ80[i].rIdx == idx)
143 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
144 "regWithIdx not found");
148 /** Frees a register.
150 static void freeReg (regs *reg)
152 wassert(!reg->isFree);
157 /** Returns number of free registers.
159 static int nFreeRegs (int type)
164 for (i = 0 ; i < _nRegs; i++ ) {
165 /* For now only one reg type */
166 if (regsZ80[i].isFree)
172 /** Free registers with type.
174 static int nfreeRegsType (int type)
177 if (type == REG_PTR) {
178 if ((nfr = nFreeRegs(type)) == 0)
179 return nFreeRegs(REG_GPR);
182 return nFreeRegs(type);
187 /*-----------------------------------------------------------------*/
188 /* allDefsOutOfRange - all definitions are out of a range */
189 /*-----------------------------------------------------------------*/
190 static bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
197 for ( i = 0 ;i < defs->size ; i++ ) {
200 if (bitVectBitValue(defs,i) &&
201 (ic = hTabItemWithKey(iCodehTab,i)) &&
202 ( ic->seq >= fseq && ic->seq <= toseq))
212 /*-----------------------------------------------------------------*/
213 /* computeSpillable - given a point find the spillable live ranges */
214 /*-----------------------------------------------------------------*/
215 static bitVect *computeSpillable (iCode *ic)
219 /* spillable live ranges are those that are live at this
220 point . the following categories need to be subtracted
222 a) - those that are already spilt
223 b) - if being used by this one
224 c) - defined by this one */
226 spillable = bitVectCopy(ic->rlive);
228 bitVectCplAnd(spillable,spiltSet); /* those already spilt */
230 bitVectCplAnd(spillable,ic->uses); /* used in this one */
231 bitVectUnSetBit(spillable,ic->defKey);
232 spillable = bitVectIntersect(spillable,regAssigned);
237 /*-----------------------------------------------------------------*/
238 /* noSpilLoc - return true if a variable has no spil location */
239 /*-----------------------------------------------------------------*/
240 static int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
242 return (sym->usl.spillLoc ? 0 : 1);
245 /*-----------------------------------------------------------------*/
246 /* hasSpilLoc - will return 1 if the symbol has spil location */
247 /*-----------------------------------------------------------------*/
248 static int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
250 return (sym->usl.spillLoc ? 1 : 0);
253 /** Will return 1 if the remat flag is set.
254 A symbol is rematerialisable if it doesnt need to be allocated
255 into registers at creation as it can be re-created at any time -
256 i.e. it's constant in some way.
258 static int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
263 /*-----------------------------------------------------------------*/
264 /* allLRs - return true for all */
265 /*-----------------------------------------------------------------*/
266 static int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
271 /*-----------------------------------------------------------------*/
272 /* liveRangesWith - applies function to a given set of live range */
273 /*-----------------------------------------------------------------*/
274 set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
275 eBBlock *ebp, iCode *ic)
280 if (!lrs || !lrs->size)
283 for ( i = 1 ; i < lrs->size ; i++ ) {
285 if (!bitVectBitValue(lrs,i))
288 /* if we don't find it in the live range
289 hash table we are in serious trouble */
290 if (!(sym = hTabItemWithKey(liveRanges,i))) {
291 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
292 "liveRangesWith could not find liveRange");
296 if (func(sym,ebp,ic) && bitVectBitValue(regAssigned,sym->key))
297 addSetHead(&rset,sym);
304 /*-----------------------------------------------------------------*/
305 /* leastUsedLR - given a set determines which is the least used */
306 /*-----------------------------------------------------------------*/
307 symbol *leastUsedLR (set *sset)
309 symbol *sym = NULL, *lsym = NULL ;
311 sym = lsym = setFirstItem(sset);
316 for (; lsym; lsym = setNextItem(sset)) {
318 /* if usage is the same then prefer
319 the spill the smaller of the two */
320 if ( lsym->used == sym->used )
321 if (getSize(lsym->type) < getSize(sym->type))
325 if (lsym->used < sym->used )
330 setToNull((void **)&sset);
335 /*-----------------------------------------------------------------*/
336 /* noOverLap - will iterate through the list looking for over lap */
337 /*-----------------------------------------------------------------*/
338 static int noOverLap (set *itmpStack, symbol *fsym)
343 for (sym = setFirstItem(itmpStack); sym;
344 sym = setNextItem(itmpStack)) {
345 if (sym->liveTo > fsym->liveFrom )
353 /*-----------------------------------------------------------------*/
354 /* isFree - will return 1 if the a free spil location is found */
355 /*-----------------------------------------------------------------*/
359 V_ARG(symbol **,sloc);
360 V_ARG(symbol *,fsym);
362 /* if already found */
366 /* if it is free && and the itmp assigned to
367 this does not have any overlapping live ranges
368 with the one currently being assigned and
369 the size can be accomodated */
371 noOverLap(sym->usl.itmpStack,fsym) &&
372 getSize(sym->type) >= getSize(fsym->type)) {
380 /*-----------------------------------------------------------------*/
381 /* createStackSpil - create a location on the stack to spil */
382 /*-----------------------------------------------------------------*/
383 symbol *createStackSpil (symbol *sym)
387 /* first go try and find a free one that is already
388 existing on the stack */
389 if (applyToSet(stackSpil,isFree,&sloc, sym)) {
390 /* found a free one : just update & return */
391 sym->usl.spillLoc = sloc;
394 addSetHead(&sloc->usl.itmpStack,sym);
398 /* could not then have to create one , this is the hard part
399 we need to allocate this on the stack : this is really a
400 hack!! but cannot think of anything better at this time */
402 sprintf(buffer,"sloc%d",slocNum++);
403 sloc = newiTemp(buffer);
405 /* set the type to the spilling symbol */
406 sloc->type = copyLinkChain(sym->type);
407 sloc->etype = getSpec(sloc->type);
408 SPEC_SCLS(sloc->etype) = S_AUTO ;
410 /* we don't allow it to be allocated`
411 onto the external stack since : so we
412 temporarily turn it off ; we also
413 turn off memory model to prevent
414 the spil from going to the external storage
415 and turn off overlaying
419 sloc->isref = 1; /* to prevent compiler warning */
421 /* if it is on the stack then update the stack */
422 if (IN_STACK(sloc->etype)) {
423 currFunc->stack += getSize(sloc->type);
424 stackExtend += getSize(sloc->type);
426 dataExtend += getSize(sloc->type);
428 /* add it to the stackSpil set */
429 addSetHead(&stackSpil,sloc);
430 sym->usl.spillLoc = sloc;
433 /* add it to the set of itempStack set
434 of the spill location */
435 addSetHead(&sloc->usl.itmpStack,sym);
439 /*-----------------------------------------------------------------*/
440 /* isSpiltOnStack - returns true if the spil location is on stack */
441 /*-----------------------------------------------------------------*/
442 bool isSpiltOnStack (symbol *sym)
452 /* if (sym->stackSpil) */
455 if (!sym->usl.spillLoc)
458 etype = getSpec(sym->usl.spillLoc->type);
465 /*-----------------------------------------------------------------*/
466 /* spillThis - spils a specific operand */
467 /*-----------------------------------------------------------------*/
468 static void spillThis (symbol *sym)
471 /* if this is rematerializable or has a spillLocation
472 we are okay, else we need to create a spillLocation
474 if (!(sym->remat || sym->usl.spillLoc))
475 createStackSpil (sym);
477 /* mark it has spilt & put it in the spilt set */
479 spiltSet = bitVectSetBit(spiltSet,sym->key);
481 bitVectUnSetBit(regAssigned,sym->key);
483 for (i = 0 ; i < sym->nRegs ; i++) {
485 freeReg(sym->regs[i]);
490 /* if spilt on stack then free up r0 & r1
491 if they could have been assigned to some
493 if (sym->usl.spillLoc && !sym->remat)
494 sym->usl.spillLoc->allocreq = 1;
498 /** Select a iTemp to spil : rather a simple procedure.
500 symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
502 bitVect *lrcs= NULL ;
506 /* get the spillable live ranges */
507 lrcs = computeSpillable (ic);
509 /* get all live ranges that are rematerizable */
510 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
512 /* return the least used of these */
513 return leastUsedLR(selectS);
517 /* get live ranges with spillLocations in direct space */
518 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
519 sym = leastUsedLR(selectS);
520 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
521 sym->usl.spillLoc->rname :
522 sym->usl.spillLoc->name));
524 /* mark it as allocation required */
525 sym->usl.spillLoc->allocreq = 1;
529 /* if the symbol is local to the block then */
530 if (forSym->liveTo < ebp->lSeq ) {
532 /* check if there are any live ranges allocated
533 to registers that are not used in this block */
534 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
535 sym = leastUsedLR(selectS);
536 /* if this is not rematerializable */
544 /* check if there are any live ranges that not
545 used in the remainder of the block */
546 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
547 sym = leastUsedLR (selectS);
555 /* find live ranges with spillocation && not used as pointers */
556 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
558 sym = leastUsedLR(selectS);
559 /* mark this as allocation required */
560 sym->usl.spillLoc->allocreq = 1;
565 /* find live ranges with spillocation */
566 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
568 sym = leastUsedLR(selectS);
569 sym->usl.spillLoc->allocreq = 1;
573 /* couldn't find then we need to create a spil
574 location on the stack , for which one? the least
576 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
577 /* return a created spil location */
578 sym = createStackSpil(leastUsedLR(selectS));
579 sym->usl.spillLoc->allocreq = 1;
583 /* this is an extreme situation we will spill
584 this one : happens very rarely but it does happen */
585 spillThis ( forSym );
590 /** Spil some variable & mark registers as free.
591 A spill occurs when an iTemp wont fit into the available registers.
593 bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
598 /* get something we can spil */
599 ssym = selectSpil(ic,ebp,forSym);
601 /* mark it as spilt */
603 spiltSet = bitVectSetBit(spiltSet,ssym->key);
605 /* mark it as not register assigned &
606 take it away from the set */
607 bitVectUnSetBit(regAssigned,ssym->key);
609 /* mark the registers as free */
610 for (i = 0 ; i < ssym->nRegs ;i++ )
612 freeReg(ssym->regs[i]);
614 /* if spilt on stack then free up r0 & r1
615 if they could have been assigned to as gprs */
616 if (!ptrRegReq && isSpiltOnStack(ssym) ) {
618 spillLRWithPtrReg(ssym);
621 /* if this was a block level spil then insert push & pop
622 at the start & end of block respectively */
623 if (ssym->blockSpil) {
624 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
625 /* add push to the start of the block */
626 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
627 ebp->sch->next : ebp->sch));
628 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
629 /* add pop to the end of the block */
630 addiCodeToeBBlock(ebp,nic,NULL);
633 /* if spilt because not used in the remainder of the
634 block then add a push before this instruction and
635 a pop at the end of the block */
636 if (ssym->remainSpil) {
638 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
639 /* add push just before this instruction */
640 addiCodeToeBBlock(ebp,nic,ic);
642 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
643 /* add pop to the end of the block */
644 addiCodeToeBBlock(ebp,nic,NULL);
654 /** Will try for GPR if not spil.
656 regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
661 /* try for gpr type */
662 if ((reg = allocReg(REG_GPR)))
665 /* we have to spil */
666 if (!spilSomething (ic,ebp,sym))
669 /* this looks like an infinite loop but
670 in really selectSpil will abort */
674 /** Symbol has a given register.
676 static bool symHasReg(symbol *sym,regs *reg)
680 for ( i = 0 ; i < sym->nRegs ; i++)
681 if (sym->regs[i] == reg)
687 /** Check the live to and if they have registers & are not spilt then
688 free up the registers
690 static void deassignLRs (iCode *ic, eBBlock *ebp)
696 for (sym = hTabFirstItem(liveRanges,&k); sym;
697 sym = hTabNextItem(liveRanges,&k)) {
700 /* if it does not end here */
701 if (sym->liveTo > ic->seq )
704 /* if it was spilt on stack then we can
705 mark the stack spil location as free */
707 if (sym->stackSpil) {
708 sym->usl.spillLoc->isFree = 1;
714 if (!bitVectBitValue(regAssigned,sym->key))
717 /* special case check if this is an IFX &
718 the privious one was a pop and the
719 previous one was not spilt then keep track
721 if (ic->op == IFX && ic->prev &&
722 ic->prev->op == IPOP &&
723 !ic->prev->parmPush &&
724 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
725 psym = OP_SYMBOL(IC_LEFT(ic->prev));
730 bitVectUnSetBit(regAssigned,sym->key);
732 /* if the result of this one needs registers
733 and does not have it then assign it right
736 ! (SKIP_IC2(ic) || /* not a special icode */
737 ic->op == JUMPTABLE ||
742 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
743 result->liveTo > ic->seq && /* and will live beyond this */
744 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
745 result->regType == sym->regType && /* same register types */
746 result->nRegs && /* which needs registers */
747 ! result->isspilt && /* and does not already have them */
749 ! bitVectBitValue(regAssigned,result->key) &&
750 /* the number of free regs + number of regs in this LR
751 can accomodate the what result Needs */
752 ((nfreeRegsType(result->regType) +
753 sym->nRegs) >= result->nRegs)
755 for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++) {
757 result->regs[i] = sym->regs[i] ;
759 result->regs[i] = getRegGpr (ic,ebp,result);
761 /* if the allocation falied which means
762 this was spilt then break */
763 if (!result->regs[i]) {
770 regAssigned = bitVectSetBit(regAssigned,result->key);
773 /* free the remaining */
774 for (; i < sym->nRegs ; i++) {
776 if (!symHasReg(psym,sym->regs[i]))
777 freeReg(sym->regs[i]);
779 freeReg(sym->regs[i]);
780 // sym->regs[i] = NULL;
787 /** Reassign this to registers.
789 static void reassignLR (operand *op)
791 symbol *sym = OP_SYMBOL(op);
794 /* not spilt any more */
795 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
796 bitVectUnSetBit(spiltSet,sym->key);
798 regAssigned = bitVectSetBit(regAssigned,sym->key);
802 for (i=0;i<sym->nRegs;i++)
803 sym->regs[i]->isFree = 0;
806 /** Determines if allocating will cause a spill.
808 static int willCauseSpill ( int nr, int rt)
810 /* first check if there are any avlb registers
811 of te type required */
812 if (nFreeRegs(0) >= nr)
815 /* it will cause a spil */
819 /** The allocator can allocate same registers to result and operand,
820 if this happens make sure they are in the same position as the operand
821 otherwise chaos results.
823 static void positionRegs (symbol *result, symbol *opsym, int lineno)
825 int count = min(result->nRegs,opsym->nRegs);
826 int i , j = 0, shared = 0;
828 /* if the result has been spilt then cannot share */
833 /* first make sure that they actually share */
834 for ( i = 0 ; i < count; i++ ) {
835 for (j = 0 ; j < count ; j++ ) {
836 if (result->regs[i] == opsym->regs[j] && i !=j) {
844 regs *tmp = result->regs[i];
845 result->regs[i] = result->regs[j];
846 result->regs[j] = tmp;
851 /** Try to allocate a pair of registers to the symbol.
853 bool tryAllocatingRegPair(symbol *sym)
856 wassert(sym->nRegs == 2);
857 for ( i = 0 ; i < _nRegs ; i+=2 ) {
858 if ((regsZ80[i].isFree)&&(regsZ80[i+1].isFree)) {
859 regsZ80[i].isFree = 0;
860 sym->regs[0] = ®sZ80[i];
861 regsZ80[i+1].isFree = 0;
862 sym->regs[1] = ®sZ80[i+1];
865 bitVectSetBit(currFunc->regsUsed,i);
867 bitVectSetBit(currFunc->regsUsed,i+1);
875 /** Serially allocate registers to the variables.
876 This is the main register allocation function. It is called after
879 static void serialRegAssign (eBBlock **ebbs, int count)
884 for (i = 0; i < count ; i++ ) {
888 if (ebbs[i]->noPath &&
889 (ebbs[i]->entryLabel != entryLabel &&
890 ebbs[i]->entryLabel != returnLabel ))
893 /* of all instructions do */
894 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
896 /* if this is an ipop that means some live
897 range will have to be assigned again */
899 reassignLR (IC_LEFT(ic));
901 /* if result is present && is a true symbol */
902 if (IC_RESULT(ic) && ic->op != IFX &&
903 IS_TRUE_SYMOP(IC_RESULT(ic)))
904 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
906 /* take away registers from live
907 ranges that end at this instruction */
908 deassignLRs (ic, ebbs[i]) ;
910 /* some don't need registers */
911 /* MLH: removed RESULT and POINTER_SET condition */
913 ic->op == JUMPTABLE ||
919 /* now we need to allocate registers only for the result */
921 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
926 /* if it does not need or is spilt
927 or is already assigned to registers
928 or will not live beyond this instructions */
931 bitVectBitValue(regAssigned,sym->key) ||
932 sym->liveTo <= ic->seq)
935 /* if some liverange has been spilt at the block level
936 and this one live beyond this block then spil this
938 if (blockSpil && sym->liveTo > ebbs[i]->lSeq) {
942 /* if trying to allocate this will cause
943 a spill and there is nothing to spill
944 or this one is rematerializable then
946 willCS = willCauseSpill(sym->nRegs,sym->regType);
947 spillable = computeSpillable(ic);
949 (willCS && bitVectIsZero(spillable) ) ) {
956 /* if it has a spillocation & is used less than
957 all other live ranges then spill this */
958 if ( willCS && sym->usl.spillLoc ) {
961 leastUsedLR(liveRangesWith (spillable ,
966 leastUsed->used > sym->used) {
972 /* else we assign registers to it */
973 regAssigned = bitVectSetBit(regAssigned,sym->key);
975 /* Special case: Try to fit into a reg pair if
977 if ((sym->nRegs == 2)&&tryAllocatingRegPair(sym)) {
980 for (j = 0 ; j < sym->nRegs ;j++ ) {
981 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
983 /* if the allocation falied which means
984 this was spilt then break */
990 /* if it shares registers with operands make sure
991 that they are in the same position */
992 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
993 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
994 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
995 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
996 /* do the same for the right operand */
997 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
998 OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
999 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1000 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1007 /*-----------------------------------------------------------------*/
1008 /* rUmaskForOp :- returns register mask for an operand */
1009 /*-----------------------------------------------------------------*/
1010 bitVect *rUmaskForOp (operand *op)
1016 /* only temporaries are assigned registers */
1020 sym = OP_SYMBOL(op);
1022 /* if spilt or no registers assigned to it
1024 if (sym->isspilt || !sym->nRegs)
1027 rumask = newBitVect(_nRegs);
1029 for (j = 0; j < sym->nRegs; j++) {
1030 rumask = bitVectSetBit(rumask,
1031 sym->regs[j]->rIdx);
1037 /** Returns bit vector of registers used in iCode.
1039 bitVect *regsUsedIniCode (iCode *ic)
1041 bitVect *rmask = newBitVect(_nRegs);
1043 /* do the special cases first */
1044 if (ic->op == IFX ) {
1045 rmask = bitVectUnion(rmask,
1046 rUmaskForOp(IC_COND(ic)));
1050 /* for the jumptable */
1051 if (ic->op == JUMPTABLE) {
1052 rmask = bitVectUnion(rmask,
1053 rUmaskForOp(IC_JTCOND(ic)));
1058 /* of all other cases */
1060 rmask = bitVectUnion(rmask,
1061 rUmaskForOp(IC_LEFT(ic)));
1065 rmask = bitVectUnion(rmask,
1066 rUmaskForOp(IC_RIGHT(ic)));
1069 rmask = bitVectUnion(rmask,
1070 rUmaskForOp(IC_RESULT(ic)));
1076 /** For each instruction will determine the regsUsed.
1078 static void createRegMask (eBBlock **ebbs, int count)
1082 /* for all blocks */
1083 for (i = 0; i < count ; i++ ) {
1086 if ( ebbs[i]->noPath &&
1087 ( ebbs[i]->entryLabel != entryLabel &&
1088 ebbs[i]->entryLabel != returnLabel ))
1091 /* for all instructions */
1092 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1096 if (SKIP_IC2(ic) || !ic->rlive)
1099 /* first mark the registers used in this
1101 ic->rUsed = regsUsedIniCode(ic);
1102 funcrUsed = bitVectUnion(funcrUsed,ic->rUsed);
1104 /* now create the register mask for those
1105 registers that are in use : this is a
1106 super set of ic->rUsed */
1107 ic->rMask = newBitVect(_nRegs+1);
1109 /* for all live Ranges alive at this point */
1110 for (j = 1; j < ic->rlive->size; j++ ) {
1114 /* if not alive then continue */
1115 if (!bitVectBitValue(ic->rlive,j))
1118 /* find the live range we are interested in */
1119 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1120 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1121 "createRegMask cannot find live range");
1125 /* if no register assigned to it */
1126 if (!sym->nRegs || sym->isspilt)
1129 /* for all the registers allocated to it */
1130 for (k = 0 ; k < sym->nRegs ;k++)
1133 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1139 /** Returns the rematerialized string for a remat var.
1141 char *rematStr (symbol *sym)
1144 iCode *ic = sym->rematiCode;
1148 /* if plus or minus print the right hand side */
1149 if (ic->op == '+' || ic->op == '-') {
1150 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1153 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1156 /* we reached the end */
1157 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1164 /*-----------------------------------------------------------------*/
1165 /* regTypeNum - computes the type & number of registers required */
1166 /*-----------------------------------------------------------------*/
1167 static void regTypeNum ()
1172 /* for each live range do */
1173 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1174 sym = hTabNextItem(liveRanges,&k)) {
1176 /* if used zero times then no registers needed */
1177 if ((sym->liveTo - sym->liveFrom) == 0)
1180 /* if the live range is a temporary */
1183 /* if the type is marked as a conditional */
1184 if (sym->regType == REG_CND)
1187 /* if used in return only then we don't
1189 if (sym->ruonly || sym->accuse) {
1190 if (IS_AGGREGATE(sym->type) || sym->isptr)
1191 sym->type = aggrToPtr(sym->type,FALSE);
1195 /* if not then we require registers */
1196 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1197 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1198 getSize(sym->type));
1200 if (sym->nRegs > 4) {
1201 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1202 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1205 /* determine the type of register required */
1206 /* Always general purpose */
1207 sym->regType = REG_GPR ;
1210 /* for the first run we don't provide */
1211 /* registers for true symbols we will */
1212 /* see how things go */
1218 /** Mark all registers as free.
1220 static void freeAllRegs()
1224 for (i=0;i< _nRegs;i++ )
1225 regsZ80[i].isFree = 1;
1228 /*-----------------------------------------------------------------*/
1229 /* deallocStackSpil - this will set the stack pointer back */
1230 /*-----------------------------------------------------------------*/
1231 DEFSETFUNC(deallocStackSpil)
1239 /** Register reduction for assignment.
1241 static int packRegsForAssign (iCode *ic,eBBlock *ebp)
1246 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) ||*/
1247 !IS_ITEMP(IC_RIGHT(ic)) ||
1248 OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
1249 OP_SYMBOL(IC_RIGHT(ic))->isind)
1253 /* if the true symbol is defined in far space or on stack
1254 then we should not since this will increase register pressure */
1255 if (isOperandInFarSpace(IC_RESULT(ic))) {
1256 if ((dic = farSpacePackable(ic)))
1263 /* find the definition of iTempNN scanning backwards if we find a
1264 a use of the true symbol in before we find the definition then
1266 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1267 /* if there is a function call and this is
1268 a parameter & not my parameter then don't pack it */
1269 if ( (dic->op == CALL || dic->op == PCALL) &&
1270 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1271 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1279 if (IS_SYMOP(IC_RESULT(dic)) &&
1280 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1284 if (IS_SYMOP(IC_RIGHT(dic)) &&
1285 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1286 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1291 if (IS_SYMOP(IC_LEFT(dic)) &&
1292 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1293 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1298 if (POINTER_SET(dic) &&
1299 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1307 return 0 ; /* did not find */
1309 /* if the result is on stack or iaccess then it must be
1310 the same atleast one of the operands */
1311 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1312 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1314 /* the operation has only one symbol
1315 operator then we can pack */
1316 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1317 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1320 if (!((IC_LEFT(dic) &&
1321 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1323 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1327 /* found the definition */
1328 /* replace the result with the result of */
1329 /* this assignment and remove this assignment */
1330 IC_RESULT(dic) = IC_RESULT(ic) ;
1332 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1333 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1335 /* delete from liverange table also
1336 delete from all the points inbetween and the new
1338 for ( sic = dic; sic != ic ; sic = sic->next ) {
1339 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1340 if (IS_ITEMP(IC_RESULT(dic)))
1341 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1344 remiCodeFromeBBlock(ebp,ic);
1349 /** Scanning backwards looks for first assig found.
1351 iCode *findAssignToSym (operand *op,iCode *ic)
1355 for (dic = ic->prev ; dic ; dic = dic->prev) {
1357 /* if definition by assignment */
1358 if (dic->op == '=' &&
1359 !POINTER_SET(dic) &&
1360 IC_RESULT(dic)->key == op->key)
1361 /* && IS_TRUE_SYMOP(IC_RIGHT(dic))*/
1364 /* we are interested only if defined in far space */
1365 /* or in stack space in case of + & - */
1367 /* if assigned to a non-symbol then return
1369 if (!IS_SYMOP(IC_RIGHT(dic)))
1372 /* if the symbol is in far space then
1374 if (isOperandInFarSpace(IC_RIGHT(dic)))
1377 /* for + & - operations make sure that
1378 if it is on the stack it is the same
1379 as one of the three operands */
1380 if ((ic->op == '+' || ic->op == '-') &&
1381 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1383 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1384 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1385 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1393 /* if we find an usage then we cannot delete it */
1394 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1397 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1400 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1404 /* now make sure that the right side of dic
1405 is not defined between ic & dic */
1407 iCode *sic = dic->next ;
1409 for (; sic != ic ; sic = sic->next)
1410 if (IC_RESULT(sic) &&
1411 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1420 /*-----------------------------------------------------------------*/
1421 /* packRegsForSupport :- reduce some registers for support calls */
1422 /*-----------------------------------------------------------------*/
1423 static int packRegsForSupport (iCode *ic, eBBlock *ebp)
1426 /* for the left & right operand :- look to see if the
1427 left was assigned a true symbol in far space in that
1428 case replace them */
1429 if (IS_ITEMP(IC_LEFT(ic)) &&
1430 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1431 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1437 /* found it we need to remove it from the
1439 for ( sic = dic; sic != ic ; sic = sic->next )
1440 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1442 IC_LEFT(ic)->operand.symOperand =
1443 IC_RIGHT(dic)->operand.symOperand;
1444 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1445 remiCodeFromeBBlock(ebp,dic);
1449 /* do the same for the right operand */
1452 IS_ITEMP(IC_RIGHT(ic)) &&
1453 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1454 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1460 /* found it we need to remove it from the block */
1461 for ( sic = dic; sic != ic ; sic = sic->next )
1462 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1464 IC_RIGHT(ic)->operand.symOperand =
1465 IC_RIGHT(dic)->operand.symOperand;
1466 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1468 remiCodeFromeBBlock(ebp,dic);
1475 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1477 /** Will reduce some registers for single use.
1479 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1484 /* if returning a literal then do nothing */
1488 /* only upto 2 bytes since we cannot predict
1489 the usage of b, & acc */
1490 if (getSize(operandType(op)) > 2 &&
1495 /* this routine will mark the a symbol as used in one
1496 instruction use only && if the defintion is local
1497 (ie. within the basic block) && has only one definition &&
1498 that definiion is either a return value from a
1499 function or does not contain any variables in
1501 uses = bitVectCopy(OP_USES(op));
1502 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1503 if (!bitVectIsZero(uses)) /* has other uses */
1506 /* if it has only one defintion */
1507 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1508 return NULL ; /* has more than one definition */
1510 /* get the that definition */
1512 hTabItemWithKey(iCodehTab,
1513 bitVectFirstBit(OP_DEFS(op)))))
1516 /* found the definition now check if it is local */
1517 if (dic->seq < ebp->fSeq ||
1518 dic->seq > ebp->lSeq)
1519 return NULL ; /* non-local */
1521 /* now check if it is the return from a function call */
1522 if (dic->op == CALL || dic->op == PCALL ) {
1523 if (ic->op != SEND && ic->op != RETURN) {
1524 OP_SYMBOL(op)->ruonly = 1;
1530 /* otherwise check that the definition does
1531 not contain any symbols in far space */
1532 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1533 isOperandInFarSpace(IC_RIGHT(dic)) ||
1534 IS_OP_RUONLY(IC_LEFT(ic)) ||
1535 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1539 /* if pointer set then make sure the pointer is one byte */
1540 if (POINTER_SET(dic))
1543 if (POINTER_GET(dic))
1548 /* also make sure the intervenening instructions
1549 don't have any thing in far space */
1550 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1551 /* if there is an intervening function call then no */
1552 if (dic->op == CALL || dic->op == PCALL)
1554 /* if pointer set then make sure the pointer
1556 if (POINTER_SET(dic))
1559 if (POINTER_GET(dic))
1562 /* if address of & the result is remat the okay */
1563 if (dic->op == ADDRESS_OF &&
1564 OP_SYMBOL(IC_RESULT(dic))->remat)
1567 /* if left or right or result is in far space */
1568 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1569 isOperandInFarSpace(IC_RIGHT(dic)) ||
1570 isOperandInFarSpace(IC_RESULT(dic)) ||
1571 IS_OP_RUONLY(IC_LEFT(dic)) ||
1572 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1573 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1578 OP_SYMBOL(op)->ruonly = 1;
1582 /*-----------------------------------------------------------------*/
1583 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1584 /*-----------------------------------------------------------------*/
1585 static bool isBitwiseOptimizable (iCode *ic)
1587 link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1589 /* bitwise operations are considered optimizable
1590 under the following conditions (Jean-Louis VERN)
1602 if (IS_LITERAL(rtype))
1608 Certian assignments involving pointers can be temporarly stored
1619 /** Pack registers for acc use.
1620 When the result of this operation is small and short lived it may
1621 be able to be stored in the accumelator.
1623 static void packRegsForAccUse (iCode *ic)
1627 /* if + or - then it has to be one byte result */
1628 if ((ic->op == '+' || ic->op == '-')
1629 && getSize(operandType(IC_RESULT(ic))) > 1)
1632 /* if shift operation make sure right side is not a literal */
1633 if (ic->op == RIGHT_OP &&
1634 (isOperandLiteral(IC_RIGHT(ic)) ||
1635 getSize(operandType(IC_RESULT(ic))) > 1))
1638 if (ic->op == LEFT_OP &&
1639 ( isOperandLiteral(IC_RIGHT(ic)) ||
1640 getSize(operandType(IC_RESULT(ic))) > 1))
1643 /* has only one definition */
1644 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1647 /* has only one use */
1648 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1651 /* and the usage immediately follows this iCode */
1652 if (!(uic = hTabItemWithKey(iCodehTab,
1653 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1656 if (ic->next != uic)
1659 /* if it is a conditional branch then we definitely can */
1660 if (uic->op == IFX )
1663 if ( uic->op == JUMPTABLE )
1667 /* if the usage is not is an assignment or an
1668 arithmetic / bitwise / shift operation then not */
1669 if (POINTER_SET(uic) &&
1670 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1674 if (uic->op != '=' &&
1675 !IS_ARITHMETIC_OP(uic) &&
1676 !IS_BITWISE_OP(uic) &&
1677 uic->op != LEFT_OP &&
1678 uic->op != RIGHT_OP )
1681 /* if used in ^ operation then make sure right is not a
1683 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1686 /* if shift operation make sure right side is not a literal */
1687 if (uic->op == RIGHT_OP &&
1688 ( isOperandLiteral(IC_RIGHT(uic)) ||
1689 getSize(operandType(IC_RESULT(uic))) > 1))
1692 if (uic->op == LEFT_OP &&
1693 ( isOperandLiteral(IC_RIGHT(uic)) ||
1694 getSize(operandType(IC_RESULT(uic))) > 1))
1698 /* make sure that the result of this icode is not on the
1699 stack, since acc is used to compute stack offset */
1700 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1701 OP_SYMBOL(IC_RESULT(uic))->onStack)
1706 /* if either one of them in far space then we cannot */
1707 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1708 isOperandInFarSpace(IC_LEFT(uic))) ||
1709 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1710 isOperandInFarSpace(IC_RIGHT(uic))))
1714 /* if the usage has only one operand then we can */
1715 if (IC_LEFT(uic) == NULL ||
1716 IC_RIGHT(uic) == NULL)
1719 /* make sure this is on the left side if not
1720 a '+' since '+' is commutative */
1721 if (ic->op != '+' &&
1722 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1725 /* if one of them is a literal then we can */
1726 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1727 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1728 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1732 /** This is confusing :) Guess for now */
1733 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1734 (IS_ITEMP(IC_RIGHT(uic)) ||
1735 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1738 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1739 (IS_ITEMP(IC_LEFT(uic)) ||
1740 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1744 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1747 bool opPreservesA(iCode *ic, iCode *uic)
1749 /* if it is a conditional branch then we definitely can */
1750 if (uic->op == IFX )
1753 if ( uic->op == JUMPTABLE )
1756 /* if the usage has only one operand then we can */
1757 /* PENDING: check */
1758 if (IC_LEFT(uic) == NULL ||
1759 IC_RIGHT(uic) == NULL)
1762 /* PENDING: check this rule */
1763 if (getSize(operandType(IC_RESULT(uic))) > 1) {
1769 !IS_ARITHMETIC_OP(uic) (sub requires A)
1773 !IS_BITWISE_OP(uic) &&
1777 uic->op != LEFT_OP &&
1778 !POINTER_GET(uic) &&
1779 uic->op != RIGHT_OP &&*/
1786 if (!IC_LEFT(uic) || !IC_RESULT(ic))
1789 /** This is confusing :) Guess for now */
1790 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1791 (IS_ITEMP(IC_RIGHT(uic)) ||
1792 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1795 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1796 (IS_ITEMP(IC_LEFT(uic)) ||
1797 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1803 /** Pack registers for acc use.
1804 When the result of this operation is small and short lived it may
1805 be able to be stored in the accumelator.
1807 Note that the 'A preserving' list is currently emperical :)e
1809 static void packRegsForAccUse2(iCode *ic)
1813 /* Filter out all but those 'good' commands */
1816 !IS_BITWISE_OP(ic) &&
1823 /* if + or - then it has to be one byte result.
1826 if ((ic->op == '+' || ic->op == '-')
1827 && getSize(operandType(IC_RESULT(ic))) > 1)
1830 /* if shift operation make sure right side is not a literal.
1834 if (ic->op == RIGHT_OP &&
1835 (isOperandLiteral(IC_RIGHT(ic)) ||
1836 getSize(operandType(IC_RESULT(ic))) > 1))
1839 if (ic->op == LEFT_OP &&
1840 ( isOperandLiteral(IC_RIGHT(ic)) ||
1841 getSize(operandType(IC_RESULT(ic))) > 1))
1845 /* has only one definition */
1846 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1) {
1850 /* Right. We may be able to propagate it through if:
1851 For each in the chain of uses the intermediate is OK.
1853 /* Get next with 'uses result' bit on
1854 If this->next == next
1855 Validate use of next
1856 If OK, increase count
1858 /* and the usage immediately follows this iCode */
1859 if (!(uic = hTabItemWithKey(iCodehTab,
1860 bitVectFirstBit(OP_USES(IC_RESULT(ic)))))) {
1865 /* Create a copy of the OP_USES bit vect */
1866 bitVect *uses = bitVectCopy(OP_USES(IC_RESULT(ic)));
1868 iCode *scan = ic, *next;
1871 setBit = bitVectFirstBit(uses);
1872 next = hTabItemWithKey(iCodehTab, setBit);
1873 if (scan->next == next) {
1874 bitVectUnSetBit(uses, setBit);
1875 /* Still contigous. */
1876 if (!opPreservesA(ic, next)) {
1884 } while (!bitVectIsZero(uses));
1885 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1889 /* OLD CODE FOLLOWS */
1890 /* if it is a conditional branch then we definitely can
1894 if (uic->op == IFX )
1898 if ( uic->op == JUMPTABLE )
1902 /* if the usage is not is an assignment or an
1903 arithmetic / bitwise / shift operation then not.
1904 MLH: Pending: Invalid. Our pointer sets are always peechy.
1907 if (POINTER_SET(uic) &&
1908 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1) {
1909 printf("e5 %u\n", getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)));
1915 if (uic->op != '=' &&
1916 !IS_ARITHMETIC_OP(uic) &&
1917 !IS_BITWISE_OP(uic) &&
1918 uic->op != LEFT_OP &&
1919 uic->op != RIGHT_OP ) {
1924 /* if used in ^ operation then make sure right is not a
1926 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1929 /* if shift operation make sure right side is not a literal */
1930 if (uic->op == RIGHT_OP &&
1931 ( isOperandLiteral(IC_RIGHT(uic)) ||
1932 getSize(operandType(IC_RESULT(uic))) > 1))
1935 if (uic->op == LEFT_OP &&
1936 ( isOperandLiteral(IC_RIGHT(uic)) ||
1937 getSize(operandType(IC_RESULT(uic))) > 1))
1941 /* make sure that the result of this icode is not on the
1942 stack, since acc is used to compute stack offset */
1943 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1944 OP_SYMBOL(IC_RESULT(uic))->onStack)
1949 /* if either one of them in far space then we cannot */
1950 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1951 isOperandInFarSpace(IC_LEFT(uic))) ||
1952 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1953 isOperandInFarSpace(IC_RIGHT(uic))))
1957 /* if the usage has only one operand then we can */
1958 if (IC_LEFT(uic) == NULL ||
1959 IC_RIGHT(uic) == NULL)
1962 /* make sure this is on the left side if not
1963 a '+' since '+' is commutative */
1964 if (ic->op != '+' &&
1965 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1968 /* if one of them is a literal then we can */
1969 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1970 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1971 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1975 /** This is confusing :) Guess for now */
1976 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1977 (IS_ITEMP(IC_RIGHT(uic)) ||
1978 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1981 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1982 (IS_ITEMP(IC_LEFT(uic)) ||
1983 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1987 printf("acc ok!\n");
1988 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1991 /** Does some transformations to reduce register pressure.
1993 static void packRegisters (eBBlock *ebp)
1998 while (1 && !DISABLE_PACK_ASSIGN) {
2000 /* look for assignments of the form */
2001 /* iTempNN = TRueSym (someoperation) SomeOperand */
2003 /* TrueSym := iTempNN:1 */
2004 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2005 /* find assignment of the form TrueSym := iTempNN:1 */
2006 if (ic->op == '=' && !POINTER_SET(ic))
2007 change += packRegsForAssign(ic,ebp);
2013 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2014 /* Safe: address of a true sym is always constant. */
2015 /* if this is an itemp & result of a address of a true sym
2016 then mark this as rematerialisable */
2017 if (ic->op == ADDRESS_OF &&
2018 IS_ITEMP(IC_RESULT(ic)) &&
2019 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
2020 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2021 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
2023 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2024 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2025 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2028 /* Safe: just propagates the remat flag */
2029 /* if straight assignment then carry remat flag if this is the
2031 if (ic->op == '=' &&
2033 IS_SYMOP(IC_RIGHT(ic)) &&
2034 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2035 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
2037 OP_SYMBOL(IC_RESULT(ic))->remat =
2038 OP_SYMBOL(IC_RIGHT(ic))->remat;
2039 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
2040 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
2043 /* if the condition of an if instruction is defined in the
2044 previous instruction then mark the itemp as a conditional */
2045 if ((IS_CONDITIONAL(ic) ||
2046 ( ( ic->op == BITWISEAND ||
2049 isBitwiseOptimizable(ic))) &&
2050 ic->next && ic->next->op == IFX &&
2051 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
2052 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
2054 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
2059 /* reduce for support function calls */
2060 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
2061 packRegsForSupport(ic,ebp);
2065 /* some cases the redundant moves can
2066 can be eliminated for return statements */
2067 if ((ic->op == RETURN || ic->op == SEND) &&
2068 !isOperandInFarSpace(IC_LEFT(ic)) &&
2070 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2072 /* if pointer set & left has a size more than
2073 one and right is not in far space */
2074 if (POINTER_SET(ic) &&
2075 /* MLH: no such thing.
2076 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2077 !OP_SYMBOL(IC_RESULT(ic))->remat &&
2078 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
2079 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 )
2081 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
2083 /* if pointer get */
2084 if (POINTER_GET(ic) &&
2085 /* MLH: dont have far space
2086 !isOperandInFarSpace(IC_RESULT(ic))&& */
2087 !OP_SYMBOL(IC_LEFT(ic))->remat &&
2088 !IS_OP_RUONLY(IC_RESULT(ic)) &&
2089 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 )
2090 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2091 /* pack registers for accumulator use, when the result of an
2092 arithmetic or bit wise operation has only one use, that use is
2093 immediately following the defintion and the using iCode has
2094 only one operand or has two operands but one is literal & the
2095 result of that operation is not on stack then we can leave the
2096 result of this operation in acc:b combination */
2098 if ((IS_ARITHMETIC_OP(ic)
2099 || IS_BITWISE_OP(ic)
2100 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2102 IS_ITEMP(IC_RESULT(ic)) &&
2103 getSize(operandType(IC_RESULT(ic))) <= 2)
2104 packRegsForAccUse (ic);
2106 if (!DISABLE_PACK_ACC && IS_ITEMP(IC_RESULT(ic)) &&
2107 getSize(operandType(IC_RESULT(ic))) == 1)
2108 packRegsForAccUse2(ic);
2113 /*-----------------------------------------------------------------*/
2114 /* assignRegisters - assigns registers to each live range as need */
2115 /*-----------------------------------------------------------------*/
2116 void z80_assignRegisters (eBBlock **ebbs, int count)
2121 setToNull((void *)&funcrUsed);
2122 stackExtend = dataExtend = 0;
2125 /* DE is required for the code gen. */
2126 _nRegs = GBZ80_MAX_REGS;
2127 regsZ80 = _gbz80_regs;
2130 _nRegs = Z80_MAX_REGS;
2131 regsZ80 = _z80_regs;
2134 /* change assignments this will remove some
2135 live ranges reducing some register pressure */
2136 for (i = 0 ; i < count ;i++ )
2137 packRegisters (ebbs[i]);
2139 if (options.dump_pack)
2140 dumpEbbsToFileExt(".dumppack",ebbs,count);
2142 /* first determine for each live range the number of
2143 registers & the type of registers required for each */
2146 /* and serially allocate registers */
2147 serialRegAssign(ebbs,count);
2149 /* if stack was extended then tell the user */
2151 /* werror(W_TOOMANY_SPILS,"stack", */
2152 /* stackExtend,currFunc->name,""); */
2157 /* werror(W_TOOMANY_SPILS,"data space", */
2158 /* dataExtend,currFunc->name,""); */
2162 if (options.dump_rassgn)
2163 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
2165 /* after that create the register mask
2166 for each of the instruction */
2167 createRegMask (ebbs,count);
2169 /* now get back the chain */
2170 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
2172 /* redo that offsets for stacked automatic variables */
2173 redoStackOffsets ();
2177 /* free up any stackSpil locations allocated */
2178 applyToSet(stackSpil,deallocStackSpil);
2180 setToNull((void **)&stackSpil);
2181 setToNull((void **)&spiltSet);
2182 /* mark all registers as free */