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
47 /*-----------------------------------------------------------------*/
48 /* At this point we start getting processor specific although */
49 /* some routines are non-processor specific & can be reused when */
50 /* targetting other processors. The decision for this will have */
51 /* to be made on a routine by routine basis */
52 /* routines used to pack registers are most definitely not reusable*/
53 /* since the pack the registers depending strictly on the MCU */
54 /*-----------------------------------------------------------------*/
56 bitVect *spiltSet = NULL ;
57 set *stackSpil = NULL;
58 bitVect *regAssigned = NULL;
61 extern void genZ80Code(iCode *);
62 int ptrRegReq = 0; /* one byte pointer register required */
63 bitVect *funcrUsed = NULL; /* registers used in a function */
68 /** Set to help debug register pressure related problems */
69 #define DEBUG_FAKE_EXTRA_REGS 0
71 static regs _gbz80_regs[] = {
72 { REG_GPR, C_IDX , "c", 1 },
73 { REG_GPR, B_IDX , "b", 1 },
74 { REG_CND, CND_IDX, "c", 1}
77 static regs _z80_regs[] = {
78 { REG_GPR, C_IDX , "c", 1 },
79 { REG_GPR, B_IDX , "b", 1 },
80 { REG_GPR, E_IDX , "e", 1 },
81 { REG_GPR, D_IDX , "d", 1 },
82 /* { REG_GPR, L_IDX , "l", 1 },
83 { REG_GPR, H_IDX , "h", 1 },*/
84 #if DEBUG_FAKE_EXTRA_REGS
85 { REG_GPR, M_IDX , "m", 1 },
86 { REG_GPR, N_IDX , "n", 1 },
87 { REG_GPR, O_IDX , "o", 1 },
88 { REG_GPR, P_IDX , "p", 1 },
89 { REG_GPR, Q_IDX , "q", 1 },
90 { REG_GPR, R_IDX , "r", 1 },
91 { REG_GPR, S_IDX , "s", 1 },
92 { REG_GPR, T_IDX , "t", 1 },
94 { REG_CND, CND_IDX, "c", 1}
99 /** Number of usable registers (all but C) */
100 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
101 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
103 static void spillThis (symbol *);
105 /** Allocates register of given type.
106 'type' is not used on the z80 version. It was used to select
107 between pointer and general purpose registers on the mcs51 version.
109 @return Pointer to the newly allocated register.
111 static regs *allocReg (short type)
115 for ( i = 0 ; i < _nRegs ; i++ ) {
116 /* For now we allocate from any free */
117 if (regsZ80[i].isFree ) {
118 regsZ80[i].isFree = 0;
121 bitVectSetBit(currFunc->regsUsed,i);
128 /** Returns pointer to register wit index number
130 regs *regWithIdx (int idx)
134 for (i=0;i < _nRegs;i++)
135 if (regsZ80[i].rIdx == idx)
138 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
139 "regWithIdx not found");
143 /** Frees a register.
145 static void freeReg (regs *reg)
147 assert(!reg->isFree);
152 /** Returns number of free registers.
154 static int nFreeRegs (int type)
159 for (i = 0 ; i < _nRegs; i++ ) {
160 /* For now only one reg type */
161 if (regsZ80[i].isFree)
167 /** Free registers with type.
169 static int nfreeRegsType (int type)
172 if (type == REG_PTR) {
173 if ((nfr = nFreeRegs(type)) == 0)
174 return nFreeRegs(REG_GPR);
177 return nFreeRegs(type);
182 /*-----------------------------------------------------------------*/
183 /* allDefsOutOfRange - all definitions are out of a range */
184 /*-----------------------------------------------------------------*/
185 static bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
192 for ( i = 0 ;i < defs->size ; i++ ) {
195 if (bitVectBitValue(defs,i) &&
196 (ic = hTabItemWithKey(iCodehTab,i)) &&
197 ( ic->seq >= fseq && ic->seq <= toseq))
207 /*-----------------------------------------------------------------*/
208 /* computeSpillable - given a point find the spillable live ranges */
209 /*-----------------------------------------------------------------*/
210 static bitVect *computeSpillable (iCode *ic)
214 /* spillable live ranges are those that are live at this
215 point . the following categories need to be subtracted
217 a) - those that are already spilt
218 b) - if being used by this one
219 c) - defined by this one */
221 spillable = bitVectCopy(ic->rlive);
223 bitVectCplAnd(spillable,spiltSet); /* those already spilt */
225 bitVectCplAnd(spillable,ic->uses); /* used in this one */
226 bitVectUnSetBit(spillable,ic->defKey);
227 spillable = bitVectIntersect(spillable,regAssigned);
232 /*-----------------------------------------------------------------*/
233 /* noSpilLoc - return true if a variable has no spil location */
234 /*-----------------------------------------------------------------*/
235 static int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
237 return (sym->usl.spillLoc ? 0 : 1);
240 /*-----------------------------------------------------------------*/
241 /* hasSpilLoc - will return 1 if the symbol has spil location */
242 /*-----------------------------------------------------------------*/
243 static int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
245 return (sym->usl.spillLoc ? 1 : 0);
248 /** Will return 1 if the remat flag is set.
249 A symbol is rematerialisable if it doesnt need to be allocated
250 into registers at creation as it can be re-created at any time -
251 i.e. it's constant in some way.
253 static int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
258 /*-----------------------------------------------------------------*/
259 /* allLRs - return true for all */
260 /*-----------------------------------------------------------------*/
261 static int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
266 /*-----------------------------------------------------------------*/
267 /* liveRangesWith - applies function to a given set of live range */
268 /*-----------------------------------------------------------------*/
269 set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
270 eBBlock *ebp, iCode *ic)
275 if (!lrs || !lrs->size)
278 for ( i = 1 ; i < lrs->size ; i++ ) {
280 if (!bitVectBitValue(lrs,i))
283 /* if we don't find it in the live range
284 hash table we are in serious trouble */
285 if (!(sym = hTabItemWithKey(liveRanges,i))) {
286 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
287 "liveRangesWith could not find liveRange");
291 if (func(sym,ebp,ic) && bitVectBitValue(regAssigned,sym->key))
292 addSetHead(&rset,sym);
299 /*-----------------------------------------------------------------*/
300 /* leastUsedLR - given a set determines which is the least used */
301 /*-----------------------------------------------------------------*/
302 symbol *leastUsedLR (set *sset)
304 symbol *sym = NULL, *lsym = NULL ;
306 sym = lsym = setFirstItem(sset);
311 for (; lsym; lsym = setNextItem(sset)) {
313 /* if usage is the same then prefer
314 the spill the smaller of the two */
315 if ( lsym->used == sym->used )
316 if (getSize(lsym->type) < getSize(sym->type))
320 if (lsym->used < sym->used )
325 setToNull((void **)&sset);
330 /*-----------------------------------------------------------------*/
331 /* noOverLap - will iterate through the list looking for over lap */
332 /*-----------------------------------------------------------------*/
333 static int noOverLap (set *itmpStack, symbol *fsym)
338 for (sym = setFirstItem(itmpStack); sym;
339 sym = setNextItem(itmpStack)) {
340 if (sym->liveTo > fsym->liveFrom )
348 /*-----------------------------------------------------------------*/
349 /* isFree - will return 1 if the a free spil location is found */
350 /*-----------------------------------------------------------------*/
354 V_ARG(symbol **,sloc);
355 V_ARG(symbol *,fsym);
357 /* if already found */
361 /* if it is free && and the itmp assigned to
362 this does not have any overlapping live ranges
363 with the one currently being assigned and
364 the size can be accomodated */
366 noOverLap(sym->usl.itmpStack,fsym) &&
367 getSize(sym->type) >= getSize(fsym->type)) {
375 /*-----------------------------------------------------------------*/
376 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
377 /*-----------------------------------------------------------------*/
378 static void spillLRWithPtrReg (symbol *forSym)
380 /* Always just return */
383 /*-----------------------------------------------------------------*/
384 /* createStackSpil - create a location on the stack to spil */
385 /*-----------------------------------------------------------------*/
386 symbol *createStackSpil (symbol *sym)
390 /* first go try and find a free one that is already
391 existing on the stack */
392 if (applyToSet(stackSpil,isFree,&sloc, sym)) {
393 /* found a free one : just update & return */
394 sym->usl.spillLoc = sloc;
397 addSetHead(&sloc->usl.itmpStack,sym);
401 /* could not then have to create one , this is the hard part
402 we need to allocate this on the stack : this is really a
403 hack!! but cannot think of anything better at this time */
405 sprintf(buffer,"sloc%d",slocNum++);
406 sloc = newiTemp(buffer);
408 /* set the type to the spilling symbol */
409 sloc->type = copyLinkChain(sym->type);
410 sloc->etype = getSpec(sloc->type);
411 SPEC_SCLS(sloc->etype) = S_AUTO ;
413 /* we don't allow it to be allocated`
414 onto the external stack since : so we
415 temporarily turn it off ; we also
416 turn off memory model to prevent
417 the spil from going to the external storage
418 and turn off overlaying
422 sloc->isref = 1; /* to prevent compiler warning */
424 /* if it is on the stack then update the stack */
425 if (IN_STACK(sloc->etype)) {
426 currFunc->stack += getSize(sloc->type);
427 stackExtend += getSize(sloc->type);
429 dataExtend += getSize(sloc->type);
431 /* add it to the stackSpil set */
432 addSetHead(&stackSpil,sloc);
433 sym->usl.spillLoc = sloc;
436 /* add it to the set of itempStack set
437 of the spill location */
438 addSetHead(&sloc->usl.itmpStack,sym);
442 /*-----------------------------------------------------------------*/
443 /* isSpiltOnStack - returns true if the spil location is on stack */
444 /*-----------------------------------------------------------------*/
445 bool isSpiltOnStack (symbol *sym)
455 /* if (sym->stackSpil) */
458 if (!sym->usl.spillLoc)
461 etype = getSpec(sym->usl.spillLoc->type);
468 /*-----------------------------------------------------------------*/
469 /* spillThis - spils a specific operand */
470 /*-----------------------------------------------------------------*/
471 static void spillThis (symbol *sym)
474 /* if this is rematerializable or has a spillLocation
475 we are okay, else we need to create a spillLocation
477 if (!(sym->remat || sym->usl.spillLoc))
478 createStackSpil (sym);
481 /* mark it has spilt & put it in the spilt set */
483 spiltSet = bitVectSetBit(spiltSet,sym->key);
485 bitVectUnSetBit(regAssigned,sym->key);
487 for (i = 0 ; i < sym->nRegs ; i++)
490 freeReg(sym->regs[i]);
494 /* if spilt on stack then free up r0 & r1
495 if they could have been assigned to some
497 if (!ptrRegReq && isSpiltOnStack(sym)) {
499 spillLRWithPtrReg(sym);
502 if (sym->usl.spillLoc && !sym->remat)
503 sym->usl.spillLoc->allocreq = 1;
507 /** Select a iTemp to spil : rather a simple procedure.
509 symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
511 bitVect *lrcs= NULL ;
515 /* get the spillable live ranges */
516 lrcs = computeSpillable (ic);
518 /* get all live ranges that are rematerizable */
519 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
521 /* return the least used of these */
522 return leastUsedLR(selectS);
526 /* get live ranges with spillLocations in direct space */
527 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
528 sym = leastUsedLR(selectS);
529 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
530 sym->usl.spillLoc->rname :
531 sym->usl.spillLoc->name));
533 /* mark it as allocation required */
534 sym->usl.spillLoc->allocreq = 1;
538 /* if the symbol is local to the block then */
539 if (forSym->liveTo < ebp->lSeq ) {
541 /* check if there are any live ranges allocated
542 to registers that are not used in this block */
543 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
544 sym = leastUsedLR(selectS);
545 /* if this is not rematerializable */
553 /* check if there are any live ranges that not
554 used in the remainder of the block */
555 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
556 sym = leastUsedLR (selectS);
564 /* find live ranges with spillocation && not used as pointers */
565 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
567 sym = leastUsedLR(selectS);
568 /* mark this as allocation required */
569 sym->usl.spillLoc->allocreq = 1;
574 /* find live ranges with spillocation */
575 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
577 sym = leastUsedLR(selectS);
578 sym->usl.spillLoc->allocreq = 1;
582 /* couldn't find then we need to create a spil
583 location on the stack , for which one? the least
585 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
586 /* return a created spil location */
587 sym = createStackSpil(leastUsedLR(selectS));
588 sym->usl.spillLoc->allocreq = 1;
592 /* this is an extreme situation we will spill
593 this one : happens very rarely but it does happen */
594 spillThis ( forSym );
599 /** Spil some variable & mark registers as free.
600 A spill occurs when an iTemp wont fit into the available registers.
602 bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
607 /* get something we can spil */
608 ssym = selectSpil(ic,ebp,forSym);
610 /* mark it as spilt */
612 spiltSet = bitVectSetBit(spiltSet,ssym->key);
614 /* mark it as not register assigned &
615 take it away from the set */
616 bitVectUnSetBit(regAssigned,ssym->key);
618 /* mark the registers as free */
619 for (i = 0 ; i < ssym->nRegs ;i++ )
621 freeReg(ssym->regs[i]);
624 /* if spilt on stack then free up r0 & r1
625 if they could have been assigned to as gprs */
626 if (!ptrRegReq && isSpiltOnStack(ssym) ) {
628 spillLRWithPtrReg(ssym);
631 /* if this was a block level spil then insert push & pop
632 at the start & end of block respectively */
633 if (ssym->blockSpil) {
634 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
635 /* add push to the start of the block */
636 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
637 ebp->sch->next : ebp->sch));
638 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
639 /* add pop to the end of the block */
640 addiCodeToeBBlock(ebp,nic,NULL);
643 /* if spilt because not used in the remainder of the
644 block then add a push before this instruction and
645 a pop at the end of the block */
646 if (ssym->remainSpil) {
648 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
649 /* add push just before this instruction */
650 addiCodeToeBBlock(ebp,nic,ic);
652 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
653 /* add pop to the end of the block */
654 addiCodeToeBBlock(ebp,nic,NULL);
664 /** Will try for GPR if not spil.
666 regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
671 /* try for gpr type */
672 if ((reg = allocReg(REG_GPR)))
676 if ((reg = allocReg(REG_PTR)))
679 /* we have to spil */
680 if (!spilSomething (ic,ebp,sym))
683 /* this looks like an infinite loop but
684 in really selectSpil will abort */
688 /** Symbol has a given register.
690 static bool symHasReg(symbol *sym,regs *reg)
694 for ( i = 0 ; i < sym->nRegs ; i++)
695 if (sym->regs[i] == reg)
701 /** Check the live to and if they have registers & are not spilt then
702 free up the registers
704 static void deassignLRs (iCode *ic, eBBlock *ebp)
710 for (sym = hTabFirstItem(liveRanges,&k); sym;
711 sym = hTabNextItem(liveRanges,&k)) {
714 /* if it does not end here */
715 if (sym->liveTo > ic->seq )
718 /* if it was spilt on stack then we can
719 mark the stack spil location as free */
721 if (sym->stackSpil) {
722 sym->usl.spillLoc->isFree = 1;
728 if (!bitVectBitValue(regAssigned,sym->key))
731 /* special case check if this is an IFX &
732 the privious one was a pop and the
733 previous one was not spilt then keep track
735 if (ic->op == IFX && ic->prev &&
736 ic->prev->op == IPOP &&
737 !ic->prev->parmPush &&
738 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
739 psym = OP_SYMBOL(IC_LEFT(ic->prev));
744 bitVectUnSetBit(regAssigned,sym->key);
746 /* if the result of this one needs registers
747 and does not have it then assign it right
750 ! (SKIP_IC2(ic) || /* not a special icode */
751 ic->op == JUMPTABLE ||
756 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
757 result->liveTo > ic->seq && /* and will live beyond this */
758 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
759 result->regType == sym->regType && /* same register types */
760 result->nRegs && /* which needs registers */
761 ! result->isspilt && /* and does not already have them */
763 ! bitVectBitValue(regAssigned,result->key) &&
764 /* the number of free regs + number of regs in this LR
765 can accomodate the what result Needs */
766 ((nfreeRegsType(result->regType) +
767 sym->nRegs) >= result->nRegs)
770 for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++)
772 result->regs[i] = sym->regs[i] ;
774 result->regs[i] = getRegGpr (ic,ebp,result);
776 regAssigned = bitVectSetBit(regAssigned,result->key);
779 /* free the remaining */
780 for (; i < sym->nRegs ; i++) {
782 if (!symHasReg(psym,sym->regs[i]))
783 freeReg(sym->regs[i]);
785 freeReg(sym->regs[i]);
792 /** Reassign this to registers.
794 static void reassignLR (operand *op)
796 symbol *sym = OP_SYMBOL(op);
799 /* not spilt any more */
800 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
801 bitVectUnSetBit(spiltSet,sym->key);
803 regAssigned = bitVectSetBit(regAssigned,sym->key);
807 for (i=0;i<sym->nRegs;i++)
808 sym->regs[i]->isFree = 0;
811 /** Determines if allocating will cause a spill.
813 static int willCauseSpill ( int nr, int rt)
815 /* first check if there are any avlb registers
816 of te type required */
817 if (nFreeRegs(0) >= nr)
820 /* it will cause a spil */
824 /** The allocator can allocate same registers to result and operand,
825 if this happens make sure they are in the same position as the operand
826 otherwise chaos results.
828 static void positionRegs (symbol *result, symbol *opsym, int lineno)
830 int count = min(result->nRegs,opsym->nRegs);
831 int i , j = 0, shared = 0;
833 /* if the result has been spilt then cannot share */
838 /* first make sure that they actually share */
839 for ( i = 0 ; i < count; i++ ) {
840 for (j = 0 ; j < count ; j++ ) {
841 if (result->regs[i] == opsym->regs[j] && i !=j) {
849 regs *tmp = result->regs[i];
850 result->regs[i] = result->regs[j];
851 result->regs[j] = tmp;
856 /** Try to allocate a pair of registers to the symbol.
858 bool tryAllocatingRegPair(symbol *sym)
861 assert(sym->nRegs == 2);
862 for ( i = 0 ; i < _nRegs ; i+=2 ) {
863 if ((regsZ80[i].isFree)&&(regsZ80[i+1].isFree)) {
864 regsZ80[i].isFree = 0;
865 sym->regs[0] = ®sZ80[i];
866 regsZ80[i+1].isFree = 0;
867 sym->regs[1] = ®sZ80[i+1];
870 bitVectSetBit(currFunc->regsUsed,i);
872 bitVectSetBit(currFunc->regsUsed,i+1);
880 /** Serially allocate registers to the variables.
881 This is the main register allocation function. It is called after
884 static void serialRegAssign (eBBlock **ebbs, int count)
889 for (i = 0; i < count ; i++ ) {
893 if (ebbs[i]->noPath &&
894 (ebbs[i]->entryLabel != entryLabel &&
895 ebbs[i]->entryLabel != returnLabel ))
898 /* of all instructions do */
899 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
901 /* if this is an ipop that means some live
902 range will have to be assigned again */
904 reassignLR (IC_LEFT(ic));
906 /* if result is present && is a true symbol */
907 if (IC_RESULT(ic) && ic->op != IFX &&
908 IS_TRUE_SYMOP(IC_RESULT(ic)))
909 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
911 /* take away registers from live
912 ranges that end at this instruction */
913 deassignLRs (ic, ebbs[i]) ;
915 /* some don't need registers */
916 /* MLH: removed RESULT and POINTER_SET condition */
918 ic->op == JUMPTABLE ||
924 /* now we need to allocate registers only for the result */
926 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
931 /* if it does not need or is spilt
932 or is already assigned to registers
933 or will not live beyond this instructions */
936 bitVectBitValue(regAssigned,sym->key) ||
937 sym->liveTo <= ic->seq)
940 /* if some liverange has been spilt at the block level
941 and this one live beyond this block then spil this
943 if (blockSpil && sym->liveTo > ebbs[i]->lSeq) {
947 /* if trying to allocate this will cause
948 a spill and there is nothing to spill
949 or this one is rematerializable then
951 willCS = willCauseSpill(sym->nRegs,sym->regType);
952 spillable = computeSpillable(ic);
954 (willCS && bitVectIsZero(spillable) ) ) {
961 /* if it has a spillocation & is used less than
962 all other live ranges then spill this */
963 if ( willCS && sym->usl.spillLoc ) {
966 leastUsedLR(liveRangesWith (spillable ,
971 leastUsed->used > sym->used) {
977 /* else we assign registers to it */
978 regAssigned = bitVectSetBit(regAssigned,sym->key);
980 /* Special case: Try to fit into a reg pair if
982 if ((sym->nRegs == 2)&&tryAllocatingRegPair(sym)) {
985 for (j = 0 ; j < sym->nRegs ;j++ ) {
986 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
988 /* if the allocation falied which means
989 this was spilt then break */
995 /* if it shares registers with operands make sure
996 that they are in the same position */
997 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
998 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
999 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1000 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
1001 /* do the same for the right operand */
1002 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
1003 OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
1004 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1005 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1012 /*-----------------------------------------------------------------*/
1013 /* rUmaskForOp :- returns register mask for an operand */
1014 /*-----------------------------------------------------------------*/
1015 bitVect *rUmaskForOp (operand *op)
1021 /* only temporaries are assigned registers */
1025 sym = OP_SYMBOL(op);
1027 /* if spilt or no registers assigned to it
1029 if (sym->isspilt || !sym->nRegs)
1032 rumask = newBitVect(_nRegs);
1034 for (j = 0; j < sym->nRegs; j++) {
1035 rumask = bitVectSetBit(rumask,
1036 sym->regs[j]->rIdx);
1042 /** Returns bit vector of registers used in iCode.
1044 bitVect *regsUsedIniCode (iCode *ic)
1046 bitVect *rmask = newBitVect(_nRegs);
1048 /* do the special cases first */
1049 if (ic->op == IFX ) {
1050 rmask = bitVectUnion(rmask,
1051 rUmaskForOp(IC_COND(ic)));
1055 /* for the jumptable */
1056 if (ic->op == JUMPTABLE) {
1057 rmask = bitVectUnion(rmask,
1058 rUmaskForOp(IC_JTCOND(ic)));
1063 /* of all other cases */
1065 rmask = bitVectUnion(rmask,
1066 rUmaskForOp(IC_LEFT(ic)));
1070 rmask = bitVectUnion(rmask,
1071 rUmaskForOp(IC_RIGHT(ic)));
1074 rmask = bitVectUnion(rmask,
1075 rUmaskForOp(IC_RESULT(ic)));
1081 /** For each instruction will determine the regsUsed.
1083 static void createRegMask (eBBlock **ebbs, int count)
1087 /* for all blocks */
1088 for (i = 0; i < count ; i++ ) {
1091 if ( ebbs[i]->noPath &&
1092 ( ebbs[i]->entryLabel != entryLabel &&
1093 ebbs[i]->entryLabel != returnLabel ))
1096 /* for all instructions */
1097 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1101 if (SKIP_IC2(ic) || !ic->rlive)
1104 /* first mark the registers used in this
1106 ic->rUsed = regsUsedIniCode(ic);
1107 funcrUsed = bitVectUnion(funcrUsed,ic->rUsed);
1109 /* now create the register mask for those
1110 registers that are in use : this is a
1111 super set of ic->rUsed */
1112 ic->rMask = newBitVect(_nRegs+1);
1114 /* for all live Ranges alive at this point */
1115 for (j = 1; j < ic->rlive->size; j++ ) {
1119 /* if not alive then continue */
1120 if (!bitVectBitValue(ic->rlive,j))
1123 /* find the live range we are interested in */
1124 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1125 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1126 "createRegMask cannot find live range");
1130 /* if no register assigned to it */
1131 if (!sym->nRegs || sym->isspilt)
1134 /* for all the registers allocated to it */
1135 for (k = 0 ; k < sym->nRegs ;k++)
1138 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1144 /** Returns the rematerialized string for a remat var.
1146 char *rematStr (symbol *sym)
1149 iCode *ic = sym->rematiCode;
1153 /* if plus or minus print the right hand side */
1154 if (ic->op == '+' || ic->op == '-') {
1155 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1158 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1161 /* we reached the end */
1162 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1169 /*-----------------------------------------------------------------*/
1170 /* regTypeNum - computes the type & number of registers required */
1171 /*-----------------------------------------------------------------*/
1172 static void regTypeNum ()
1177 /* for each live range do */
1178 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1179 sym = hTabNextItem(liveRanges,&k)) {
1181 /* if used zero times then no registers needed */
1182 if ((sym->liveTo - sym->liveFrom) == 0)
1185 /* if the live range is a temporary */
1188 /* if the type is marked as a conditional */
1189 if (sym->regType == REG_CND)
1192 /* if used in return only then we don't
1194 if (sym->ruonly || sym->accuse) {
1195 if (IS_AGGREGATE(sym->type) || sym->isptr)
1196 sym->type = aggrToPtr(sym->type,FALSE);
1200 /* if not then we require registers */
1201 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1202 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1203 getSize(sym->type));
1205 if (sym->nRegs > 4) {
1206 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1207 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1210 /* determine the type of register required */
1211 /* Always general purpose */
1212 sym->regType = REG_GPR ;
1215 /* for the first run we don't provide */
1216 /* registers for true symbols we will */
1217 /* see how things go */
1223 /** Mark all registers as free.
1225 static void freeAllRegs()
1229 for (i=0;i< _nRegs;i++ )
1230 regsZ80[i].isFree = 1;
1233 /*-----------------------------------------------------------------*/
1234 /* deallocStackSpil - this will set the stack pointer back */
1235 /*-----------------------------------------------------------------*/
1236 DEFSETFUNC(deallocStackSpil)
1244 /** Register reduction for assignment.
1246 static int packRegsForAssign (iCode *ic,eBBlock *ebp)
1251 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) ||*/
1252 !IS_ITEMP(IC_RIGHT(ic)) ||
1253 OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
1254 OP_SYMBOL(IC_RIGHT(ic))->isind)
1258 /* if the true symbol is defined in far space or on stack
1259 then we should not since this will increase register pressure */
1260 if (isOperandInFarSpace(IC_RESULT(ic))) {
1261 if ((dic = farSpacePackable(ic)))
1268 /* find the definition of iTempNN scanning backwards if we find a
1269 a use of the true symbol in before we find the definition then
1271 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1272 /* if there is a function call and this is
1273 a parameter & not my parameter then don't pack it */
1274 if ( (dic->op == CALL || dic->op == PCALL) &&
1275 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1276 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1284 if (IS_SYMOP(IC_RESULT(dic)) &&
1285 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1289 if (IS_SYMOP(IC_RIGHT(dic)) &&
1290 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1291 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1296 if (IS_SYMOP(IC_LEFT(dic)) &&
1297 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1298 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1303 if (POINTER_SET(dic) &&
1304 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1312 return 0 ; /* did not find */
1314 /* if the result is on stack or iaccess then it must be
1315 the same atleast one of the operands */
1316 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1317 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1319 /* the operation has only one symbol
1320 operator then we can pack */
1321 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1322 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1325 if (!((IC_LEFT(dic) &&
1326 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1328 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1332 /* found the definition */
1333 /* replace the result with the result of */
1334 /* this assignment and remove this assignment */
1335 IC_RESULT(dic) = IC_RESULT(ic) ;
1337 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1338 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1340 /* delete from liverange table also
1341 delete from all the points inbetween and the new
1343 for ( sic = dic; sic != ic ; sic = sic->next ) {
1344 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1345 if (IS_ITEMP(IC_RESULT(dic)))
1346 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1349 remiCodeFromeBBlock(ebp,ic);
1354 /** Scanning backwards looks for first assig found.
1356 iCode *findAssignToSym (operand *op,iCode *ic)
1360 for (dic = ic->prev ; dic ; dic = dic->prev) {
1362 /* if definition by assignment */
1363 if (dic->op == '=' &&
1364 !POINTER_SET(dic) &&
1365 IC_RESULT(dic)->key == op->key)
1366 /* && IS_TRUE_SYMOP(IC_RIGHT(dic))*/
1369 /* we are interested only if defined in far space */
1370 /* or in stack space in case of + & - */
1372 /* if assigned to a non-symbol then return
1374 if (!IS_SYMOP(IC_RIGHT(dic)))
1377 /* if the symbol is in far space then
1379 if (isOperandInFarSpace(IC_RIGHT(dic)))
1382 /* for + & - operations make sure that
1383 if it is on the stack it is the same
1384 as one of the three operands */
1385 if ((ic->op == '+' || ic->op == '-') &&
1386 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1388 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1389 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1390 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1398 /* if we find an usage then we cannot delete it */
1399 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1402 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1405 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1409 /* now make sure that the right side of dic
1410 is not defined between ic & dic */
1412 iCode *sic = dic->next ;
1414 for (; sic != ic ; sic = sic->next)
1415 if (IC_RESULT(sic) &&
1416 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1425 /*-----------------------------------------------------------------*/
1426 /* packRegsForSupport :- reduce some registers for support calls */
1427 /*-----------------------------------------------------------------*/
1428 static int packRegsForSupport (iCode *ic, eBBlock *ebp)
1431 /* for the left & right operand :- look to see if the
1432 left was assigned a true symbol in far space in that
1433 case replace them */
1434 if (IS_ITEMP(IC_LEFT(ic)) &&
1435 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1436 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1442 /* found it we need to remove it from the
1444 for ( sic = dic; sic != ic ; sic = sic->next )
1445 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1447 IC_LEFT(ic)->operand.symOperand =
1448 IC_RIGHT(dic)->operand.symOperand;
1449 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1450 remiCodeFromeBBlock(ebp,dic);
1454 /* do the same for the right operand */
1457 IS_ITEMP(IC_RIGHT(ic)) &&
1458 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1459 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1465 /* found it we need to remove it from the block */
1466 for ( sic = dic; sic != ic ; sic = sic->next )
1467 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1469 IC_RIGHT(ic)->operand.symOperand =
1470 IC_RIGHT(dic)->operand.symOperand;
1471 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1473 remiCodeFromeBBlock(ebp,dic);
1480 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1482 /** Will reduce some registers for single use.
1484 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1489 /* if returning a literal then do nothing */
1493 /* only upto 2 bytes since we cannot predict
1494 the usage of b, & acc */
1495 if (getSize(operandType(op)) > 2 &&
1500 /* this routine will mark the a symbol as used in one
1501 instruction use only && if the defintion is local
1502 (ie. within the basic block) && has only one definition &&
1503 that definiion is either a return value from a
1504 function or does not contain any variables in
1506 uses = bitVectCopy(OP_USES(op));
1507 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1508 if (!bitVectIsZero(uses)) /* has other uses */
1511 /* if it has only one defintion */
1512 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1513 return NULL ; /* has more than one definition */
1515 /* get the that definition */
1517 hTabItemWithKey(iCodehTab,
1518 bitVectFirstBit(OP_DEFS(op)))))
1521 /* found the definition now check if it is local */
1522 if (dic->seq < ebp->fSeq ||
1523 dic->seq > ebp->lSeq)
1524 return NULL ; /* non-local */
1526 /* now check if it is the return from a function call */
1527 if (dic->op == CALL || dic->op == PCALL ) {
1528 if (ic->op != SEND && ic->op != RETURN) {
1529 OP_SYMBOL(op)->ruonly = 1;
1535 /* otherwise check that the definition does
1536 not contain any symbols in far space */
1537 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1538 isOperandInFarSpace(IC_RIGHT(dic)) ||
1539 IS_OP_RUONLY(IC_LEFT(ic)) ||
1540 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1544 /* if pointer set then make sure the pointer is one byte */
1545 if (POINTER_SET(dic))
1548 if (POINTER_GET(dic))
1553 /* also make sure the intervenening instructions
1554 don't have any thing in far space */
1555 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1556 /* if there is an intervening function call then no */
1557 if (dic->op == CALL || dic->op == PCALL)
1559 /* if pointer set then make sure the pointer
1561 if (POINTER_SET(dic))
1564 if (POINTER_GET(dic))
1567 /* if address of & the result is remat the okay */
1568 if (dic->op == ADDRESS_OF &&
1569 OP_SYMBOL(IC_RESULT(dic))->remat)
1572 /* if left or right or result is in far space */
1573 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1574 isOperandInFarSpace(IC_RIGHT(dic)) ||
1575 isOperandInFarSpace(IC_RESULT(dic)) ||
1576 IS_OP_RUONLY(IC_LEFT(dic)) ||
1577 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1578 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1583 OP_SYMBOL(op)->ruonly = 1;
1587 /*-----------------------------------------------------------------*/
1588 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1589 /*-----------------------------------------------------------------*/
1590 static bool isBitwiseOptimizable (iCode *ic)
1592 link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1594 /* bitwise operations are considered optimizable
1595 under the following conditions (Jean-Louis VERN)
1607 if (IS_LITERAL(rtype))
1613 Certian assignments involving pointers can be temporarly stored
1624 /** Pack registers for acc use.
1625 When the result of this operation is small and short lived it may
1626 be able to be stored in the accumelator.
1628 static void packRegsForAccUse (iCode *ic)
1632 /* if + or - then it has to be one byte result */
1633 if ((ic->op == '+' || ic->op == '-')
1634 && getSize(operandType(IC_RESULT(ic))) > 1)
1637 /* if shift operation make sure right side is not a literal */
1638 if (ic->op == RIGHT_OP &&
1639 (isOperandLiteral(IC_RIGHT(ic)) ||
1640 getSize(operandType(IC_RESULT(ic))) > 1))
1643 if (ic->op == LEFT_OP &&
1644 ( isOperandLiteral(IC_RIGHT(ic)) ||
1645 getSize(operandType(IC_RESULT(ic))) > 1))
1648 /* has only one definition */
1649 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1652 /* has only one use */
1653 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1656 /* and the usage immediately follows this iCode */
1657 if (!(uic = hTabItemWithKey(iCodehTab,
1658 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1661 if (ic->next != uic)
1664 /* if it is a conditional branch then we definitely can */
1665 if (uic->op == IFX )
1668 if ( uic->op == JUMPTABLE )
1672 /* if the usage is not is an assignment or an
1673 arithmetic / bitwise / shift operation then not */
1674 if (POINTER_SET(uic) &&
1675 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1679 if (uic->op != '=' &&
1680 !IS_ARITHMETIC_OP(uic) &&
1681 !IS_BITWISE_OP(uic) &&
1682 uic->op != LEFT_OP &&
1683 uic->op != RIGHT_OP )
1686 /* if used in ^ operation then make sure right is not a
1688 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1691 /* if shift operation make sure right side is not a literal */
1692 if (uic->op == RIGHT_OP &&
1693 ( isOperandLiteral(IC_RIGHT(uic)) ||
1694 getSize(operandType(IC_RESULT(uic))) > 1))
1697 if (uic->op == LEFT_OP &&
1698 ( isOperandLiteral(IC_RIGHT(uic)) ||
1699 getSize(operandType(IC_RESULT(uic))) > 1))
1703 /* make sure that the result of this icode is not on the
1704 stack, since acc is used to compute stack offset */
1705 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1706 OP_SYMBOL(IC_RESULT(uic))->onStack)
1711 /* if either one of them in far space then we cannot */
1712 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1713 isOperandInFarSpace(IC_LEFT(uic))) ||
1714 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1715 isOperandInFarSpace(IC_RIGHT(uic))))
1719 /* if the usage has only one operand then we can */
1720 if (IC_LEFT(uic) == NULL ||
1721 IC_RIGHT(uic) == NULL)
1724 /* make sure this is on the left side if not
1725 a '+' since '+' is commutative */
1726 if (ic->op != '+' &&
1727 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1730 /* if one of them is a literal then we can */
1731 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1732 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1733 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1737 /** This is confusing :) Guess for now */
1738 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1739 (IS_ITEMP(IC_RIGHT(uic)) ||
1740 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1743 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1744 (IS_ITEMP(IC_LEFT(uic)) ||
1745 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1749 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1752 bool opPreservesA(iCode *ic, iCode *uic)
1754 /* if the usage has only one operand then we can */
1755 if (IC_LEFT(uic) == NULL ||
1756 IC_RIGHT(uic) == NULL)
1759 if (getSize(operandType(IC_RESULT(uic))) > 1) {
1763 if (uic->op != '=' &&
1764 !IS_ARITHMETIC_OP(uic) &&
1765 !IS_BITWISE_OP(uic) &&
1767 uic->op != LEFT_OP &&
1768 !POINTER_GET(uic) &&
1769 uic->op != RIGHT_OP ) {
1774 if (!IC_LEFT(uic) || !IC_RESULT(ic))
1777 /** This is confusing :) Guess for now */
1778 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1779 (IS_ITEMP(IC_RIGHT(uic)) ||
1780 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1783 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1784 (IS_ITEMP(IC_LEFT(uic)) ||
1785 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1791 /** Pack registers for acc use.
1792 When the result of this operation is small and short lived it may
1793 be able to be stored in the accumelator.
1795 static void packRegsForAccUse2(iCode *ic)
1799 /* if + or - then it has to be one byte result.
1802 if ((ic->op == '+' || ic->op == '-')
1803 && getSize(operandType(IC_RESULT(ic))) > 1)
1806 /* if shift operation make sure right side is not a literal.
1810 if (ic->op == RIGHT_OP &&
1811 (isOperandLiteral(IC_RIGHT(ic)) ||
1812 getSize(operandType(IC_RESULT(ic))) > 1))
1815 if (ic->op == LEFT_OP &&
1816 ( isOperandLiteral(IC_RIGHT(ic)) ||
1817 getSize(operandType(IC_RESULT(ic))) > 1))
1821 /* has only one definition */
1822 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1) {
1826 /* Right. We may be able to propagate it through if:
1827 For each in the chain of uses the intermediate is OK.
1829 /* Get next with 'uses result' bit on
1830 If this->next == next
1831 Validate use of next
1832 If OK, increase count
1834 /* and the usage immediately follows this iCode */
1835 if (!(uic = hTabItemWithKey(iCodehTab,
1836 bitVectFirstBit(OP_USES(IC_RESULT(ic)))))) {
1841 /* Create a copy of the OP_USES bit vect */
1842 bitVect *uses = bitVectCopy(OP_USES(IC_RESULT(ic)));
1844 iCode *scan = ic, *next;
1847 setBit = bitVectFirstBit(uses);
1848 next = hTabItemWithKey(iCodehTab, setBit);
1849 if (scan->next == next) {
1850 bitVectUnSetBit(uses, setBit);
1851 /* Still contigous. */
1852 if (!opPreservesA(ic, next)) {
1860 } while (!bitVectIsZero(uses));
1861 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1865 /* OLD CODE FOLLOWS */
1866 /* if it is a conditional branch then we definitely can
1870 if (uic->op == IFX )
1874 if ( uic->op == JUMPTABLE )
1878 /* if the usage is not is an assignment or an
1879 arithmetic / bitwise / shift operation then not.
1880 MLH: Pending: Invalid. Our pointer sets are always peechy.
1883 if (POINTER_SET(uic) &&
1884 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1) {
1885 printf("e5 %u\n", getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)));
1891 if (uic->op != '=' &&
1892 !IS_ARITHMETIC_OP(uic) &&
1893 !IS_BITWISE_OP(uic) &&
1894 uic->op != LEFT_OP &&
1895 uic->op != RIGHT_OP ) {
1900 /* if used in ^ operation then make sure right is not a
1902 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1905 /* if shift operation make sure right side is not a literal */
1906 if (uic->op == RIGHT_OP &&
1907 ( isOperandLiteral(IC_RIGHT(uic)) ||
1908 getSize(operandType(IC_RESULT(uic))) > 1))
1911 if (uic->op == LEFT_OP &&
1912 ( isOperandLiteral(IC_RIGHT(uic)) ||
1913 getSize(operandType(IC_RESULT(uic))) > 1))
1917 /* make sure that the result of this icode is not on the
1918 stack, since acc is used to compute stack offset */
1919 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1920 OP_SYMBOL(IC_RESULT(uic))->onStack)
1925 /* if either one of them in far space then we cannot */
1926 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1927 isOperandInFarSpace(IC_LEFT(uic))) ||
1928 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1929 isOperandInFarSpace(IC_RIGHT(uic))))
1933 /* if the usage has only one operand then we can */
1934 if (IC_LEFT(uic) == NULL ||
1935 IC_RIGHT(uic) == NULL)
1938 /* make sure this is on the left side if not
1939 a '+' since '+' is commutative */
1940 if (ic->op != '+' &&
1941 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1944 /* if one of them is a literal then we can */
1945 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1946 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1947 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1951 /** This is confusing :) Guess for now */
1952 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1953 (IS_ITEMP(IC_RIGHT(uic)) ||
1954 (IS_TRUE_SYMOP(IC_RIGHT(uic)))))
1957 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1958 (IS_ITEMP(IC_LEFT(uic)) ||
1959 (IS_TRUE_SYMOP(IC_LEFT(uic)))))
1963 printf("acc ok!\n");
1964 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1967 /** Does some transformations to reduce register pressure.
1969 static void packRegisters (eBBlock *ebp)
1976 /* look for assignments of the form */
1977 /* iTempNN = TRueSym (someoperation) SomeOperand */
1979 /* TrueSym := iTempNN:1 */
1980 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
1981 /* find assignment of the form TrueSym := iTempNN:1 */
1982 if (ic->op == '=' && !POINTER_SET(ic))
1983 change += packRegsForAssign(ic,ebp);
1989 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
1990 /* Safe: address of a true sym is always constant. */
1991 /* if this is an itemp & result of a address of a true sym
1992 then mark this as rematerialisable */
1993 if (ic->op == ADDRESS_OF &&
1994 IS_ITEMP(IC_RESULT(ic)) &&
1995 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
1996 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
1997 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
1999 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2000 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2001 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2004 /* Safe: just propagates the remat flag */
2005 /* if straight assignment then carry remat flag if this is the
2007 if (ic->op == '=' &&
2009 IS_SYMOP(IC_RIGHT(ic)) &&
2010 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2011 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
2013 OP_SYMBOL(IC_RESULT(ic))->remat =
2014 OP_SYMBOL(IC_RIGHT(ic))->remat;
2015 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
2016 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
2019 /* if the condition of an if instruction is defined in the
2020 previous instruction then mark the itemp as a conditional */
2021 if ((IS_CONDITIONAL(ic) ||
2022 ( ( ic->op == BITWISEAND ||
2025 isBitwiseOptimizable(ic))) &&
2026 ic->next && ic->next->op == IFX &&
2027 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
2028 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
2030 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
2035 /* reduce for support function calls */
2036 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
2037 packRegsForSupport(ic,ebp);
2041 /* some cases the redundant moves can
2042 can be eliminated for return statements */
2043 if ((ic->op == RETURN || ic->op == SEND) &&
2044 !isOperandInFarSpace(IC_LEFT(ic)) &&
2046 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2048 /* if pointer set & left has a size more than
2049 one and right is not in far space */
2050 if (POINTER_SET(ic) &&
2051 /* MLH: no such thing.
2052 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2053 !OP_SYMBOL(IC_RESULT(ic))->remat &&
2054 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
2055 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 )
2057 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
2059 /* if pointer get */
2060 if (POINTER_GET(ic) &&
2061 /* MLH: dont have far space
2062 !isOperandInFarSpace(IC_RESULT(ic))&& */
2063 !OP_SYMBOL(IC_LEFT(ic))->remat &&
2064 !IS_OP_RUONLY(IC_RESULT(ic)) &&
2065 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 )
2066 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2067 /* pack registers for accumulator use, when the result of an
2068 arithmetic or bit wise operation has only one use, that use is
2069 immediately following the defintion and the using iCode has
2070 only one operand or has two operands but one is literal & the
2071 result of that operation is not on stack then we can leave the
2072 result of this operation in acc:b combination */
2075 if ((IS_ARITHMETIC_OP(ic)
2076 || IS_BITWISE_OP(ic)
2077 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2079 IS_ITEMP(IC_RESULT(ic)) &&
2080 getSize(operandType(IC_RESULT(ic))) <= 2)
2081 packRegsForAccUse (ic);
2083 if (IS_ITEMP(IC_RESULT(ic)) &&
2084 getSize(operandType(IC_RESULT(ic))) == 1)
2085 packRegsForAccUse2(ic);
2091 /*-----------------------------------------------------------------*/
2092 /* assignRegisters - assigns registers to each live range as need */
2093 /*-----------------------------------------------------------------*/
2094 void z80_assignRegisters (eBBlock **ebbs, int count)
2099 setToNull((void *)&funcrUsed);
2100 ptrRegReq = stackExtend = dataExtend = 0;
2103 /* DE is required for the code gen. */
2104 _nRegs = GBZ80_MAX_REGS;
2105 regsZ80 = _gbz80_regs;
2108 _nRegs = Z80_MAX_REGS;
2109 regsZ80 = _z80_regs;
2112 /* change assignments this will remove some
2113 live ranges reducing some register pressure */
2114 for (i = 0 ; i < count ;i++ )
2115 packRegisters (ebbs[i]);
2117 if (options.dump_pack)
2118 dumpEbbsToFileExt(".dumppack",ebbs,count);
2120 /* first determine for each live range the number of
2121 registers & the type of registers required for each */
2124 /* and serially allocate registers */
2125 serialRegAssign(ebbs,count);
2127 /* if stack was extended then tell the user */
2129 /* werror(W_TOOMANY_SPILS,"stack", */
2130 /* stackExtend,currFunc->name,""); */
2135 /* werror(W_TOOMANY_SPILS,"data space", */
2136 /* dataExtend,currFunc->name,""); */
2140 if (options.dump_rassgn)
2141 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
2143 /* after that create the register mask
2144 for each of the instruction */
2145 createRegMask (ebbs,count);
2147 /* now get back the chain */
2148 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
2150 /* redo that offsets for stacked automatic variables */
2151 redoStackOffsets ();
2155 /* free up any stackSpil locations allocated */
2156 applyToSet(stackSpil,deallocStackSpil);
2158 setToNull((void **)&stackSpil);
2159 setToNull((void **)&spiltSet);
2160 /* mark all registers as free */