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 #include "SDCCglobl.h"
51 #include "SDCChasht.h"
54 #include "SDCCicode.h"
55 #include "SDCClabel.h"
56 #include "SDCCBBlock.h"
59 #include "SDCCcflow.h"
60 #include "SDCCdflow.h"
61 #include "SDCClrange.h"
64 /*-----------------------------------------------------------------*/
65 /* At this point we start getting processor specific although */
66 /* some routines are non-processor specific & can be reused when */
67 /* targetting other processors. The decision for this will have */
68 /* to be made on a routine by routine basis */
69 /* routines used to pack registers are most definitely not reusable*/
70 /* since the pack the registers depending strictly on the MCU */
71 /*-----------------------------------------------------------------*/
73 bitVect *spiltSet = NULL ;
74 set *stackSpil = NULL;
75 bitVect *regAssigned = NULL;
78 extern void genZ80Code(iCode *);
79 int ptrRegReq = 0; /* one byte pointer register required */
80 bitVect *funcrUsed = NULL; /* registers used in a function */
84 /** Set to help debug register pressure related problems */
85 #define DEBUG_FAKE_EXTRA_REGS 0
89 { REG_GPR, C_IDX , "c", 1 },
90 { REG_GPR, B_IDX , "b", 1 },
91 { REG_GPR, E_IDX , "e", 1 },
92 { REG_GPR, D_IDX , "d", 1 },
93 /* { REG_GPR, L_IDX , "l", 1 },
94 { REG_GPR, H_IDX , "h", 1 },*/
95 #if DEBUG_FAKE_EXTRA_REGS
96 { REG_GPR, M_IDX , "m", 1 },
97 { REG_GPR, N_IDX , "n", 1 },
98 { REG_GPR, O_IDX , "o", 1 },
99 { REG_GPR, P_IDX , "p", 1 },
100 { REG_GPR, Q_IDX , "q", 1 },
101 { REG_GPR, R_IDX , "r", 1 },
102 { REG_GPR, S_IDX , "s", 1 },
103 { REG_GPR, T_IDX , "t", 1 },
105 { REG_CND, CND_IDX, "c", 1}
108 /** Number of usable registers (all but C) */
109 #define MAX_REGS ((sizeof(regsZ80)/sizeof(regs))-1)
111 int nRegs = MAX_REGS;
113 void spillThis (symbol *);
115 /** Allocates register of given type.
116 'type' is not used on the z80 version. It was used to select
117 between pointer and general purpose registers on the mcs51 version.
119 @return Pointer to the newly allocated register.
121 regs *allocReg (short type)
125 for ( i = 0 ; i < nRegs ; i++ ) {
126 /* For now we allocate from any free */
127 if (regsZ80[i].isFree ) {
128 regsZ80[i].isFree = 0;
131 bitVectSetBit(currFunc->regsUsed,i);
138 /** Returns pointer to register wit index number
140 regs *regWithIdx (int idx)
144 for (i=0;i < nRegs;i++)
145 if (regsZ80[i].rIdx == idx)
148 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
149 "regWithIdx not found");
153 /** Frees a register.
155 void freeReg (regs *reg)
157 assert(!reg->isFree);
162 /** Returns number of free registers.
164 int nFreeRegs (int type)
169 for (i = 0 ; i < nRegs; i++ ) {
170 /* For now only one reg type */
171 if (regsZ80[i].isFree)
177 /** Free registers with type.
179 int nfreeRegsType (int type)
182 if (type == REG_PTR) {
183 if ((nfr = nFreeRegs(type)) == 0)
184 return nFreeRegs(REG_GPR);
187 return nFreeRegs(type);
191 /*-----------------------------------------------------------------*/
192 /* allDefsOutOfRange - all definitions are out of a range */
193 /*-----------------------------------------------------------------*/
194 bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
201 for ( i = 0 ;i < defs->size ; i++ ) {
204 if (bitVectBitValue(defs,i) &&
205 (ic = hTabItemWithKey(iCodehTab,i)) &&
206 ( ic->seq >= fseq && ic->seq <= toseq))
215 /*-----------------------------------------------------------------*/
216 /* computeSpillable - given a point find the spillable live ranges */
217 /*-----------------------------------------------------------------*/
218 bitVect *computeSpillable (iCode *ic)
222 /* spillable live ranges are those that are live at this
223 point . the following categories need to be subtracted
225 a) - those that are already spilt
226 b) - if being used by this one
227 c) - defined by this one */
229 spillable = bitVectCopy(ic->rlive);
231 bitVectCplAnd(spillable,spiltSet); /* those already spilt */
233 bitVectCplAnd(spillable,ic->uses); /* used in this one */
234 bitVectUnSetBit(spillable,ic->defKey);
235 spillable = bitVectIntersect(spillable,regAssigned);
240 /*-----------------------------------------------------------------*/
241 /* noSpilLoc - return true if a variable has no spil location */
242 /*-----------------------------------------------------------------*/
243 int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
245 return (sym->usl.spillLoc ? 0 : 1);
248 /*-----------------------------------------------------------------*/
249 /* hasSpilLoc - will return 1 if the symbol has spil location */
250 /*-----------------------------------------------------------------*/
251 int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
253 return (sym->usl.spillLoc ? 1 : 0);
256 /*-----------------------------------------------------------------*/
257 /* directSpilLoc - will return 1 if the splilocation is in direct */
258 /*-----------------------------------------------------------------*/
259 int directSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
261 /* No such thing as direct space */
265 /*-----------------------------------------------------------------*/
266 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location*/
267 /* but is not used as a pointer */
268 /*-----------------------------------------------------------------*/
269 int hasSpilLocnoUptr (symbol *sym, eBBlock *ebp, iCode *ic)
272 return sym->usl.spillLoc ? 1:0;
274 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
278 /** Will return 1 if the remat flag is set.
279 A symbol is rematerialisable if it doesnt need to be allocated
280 into registers at creation as it can be re-created at any time -
281 i.e. it's constant in some way.
283 int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
288 /*-----------------------------------------------------------------*/
289 /* notUsedInBlock - not used in this block */
290 /*-----------------------------------------------------------------*/
291 int notUsedInBlock (symbol *sym, eBBlock *ebp, iCode *ic)
293 return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs) &&
294 allDefsOutOfRange (sym->defs,ebp->fSeq,ebp->lSeq));
295 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
298 /*-----------------------------------------------------------------*/
299 /* notUsedInRemaining - not used or defined in remain of the block */
300 /*-----------------------------------------------------------------*/
301 int notUsedInRemaining (symbol *sym, eBBlock *ebp, iCode *ic)
303 return ((usedInRemaining (operandFromSymbol(sym),ic) ? 0 : 1) &&
304 allDefsOutOfRange (sym->defs,ic->seq,ebp->lSeq));
307 /*-----------------------------------------------------------------*/
308 /* allLRs - return true for all */
309 /*-----------------------------------------------------------------*/
310 int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
315 /*-----------------------------------------------------------------*/
316 /* liveRangesWith - applies function to a given set of live range */
317 /*-----------------------------------------------------------------*/
318 set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
319 eBBlock *ebp, iCode *ic)
324 if (!lrs || !lrs->size)
327 for ( i = 1 ; i < lrs->size ; i++ ) {
329 if (!bitVectBitValue(lrs,i))
332 /* if we don't find it in the live range
333 hash table we are in serious trouble */
334 if (!(sym = hTabItemWithKey(liveRanges,i))) {
335 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
336 "liveRangesWith could not find liveRange");
340 if (func(sym,ebp,ic) && bitVectBitValue(regAssigned,sym->key))
341 addSetHead(&rset,sym);
348 /*-----------------------------------------------------------------*/
349 /* leastUsedLR - given a set determines which is the least used */
350 /*-----------------------------------------------------------------*/
351 symbol *leastUsedLR (set *sset)
353 symbol *sym = NULL, *lsym = NULL ;
355 sym = lsym = setFirstItem(sset);
360 for (; lsym; lsym = setNextItem(sset)) {
362 /* if usage is the same then prefer
363 the spill the smaller of the two */
364 if ( lsym->used == sym->used )
365 if (getSize(lsym->type) < getSize(sym->type))
369 if (lsym->used < sym->used )
374 setToNull((void **)&sset);
379 /*-----------------------------------------------------------------*/
380 /* noOverLap - will iterate through the list looking for over lap */
381 /*-----------------------------------------------------------------*/
382 int noOverLap (set *itmpStack, symbol *fsym)
387 for (sym = setFirstItem(itmpStack); sym;
388 sym = setNextItem(itmpStack)) {
389 if (sym->liveTo > fsym->liveFrom )
397 /*-----------------------------------------------------------------*/
398 /* isFree - will return 1 if the a free spil location is found */
399 /*-----------------------------------------------------------------*/
403 V_ARG(symbol **,sloc);
404 V_ARG(symbol *,fsym);
406 /* if already found */
410 /* if it is free && and the itmp assigned to
411 this does not have any overlapping live ranges
412 with the one currently being assigned and
413 the size can be accomodated */
415 noOverLap(sym->usl.itmpStack,fsym) &&
416 getSize(sym->type) >= getSize(fsym->type)) {
424 /*-----------------------------------------------------------------*/
425 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
426 /*-----------------------------------------------------------------*/
427 void spillLRWithPtrReg (symbol *forSym)
429 /* Always just return */
432 /*-----------------------------------------------------------------*/
433 /* createStackSpil - create a location on the stack to spil */
434 /*-----------------------------------------------------------------*/
435 symbol *createStackSpil (symbol *sym)
439 /* first go try and find a free one that is already
440 existing on the stack */
441 if (applyToSet(stackSpil,isFree,&sloc, sym)) {
442 /* found a free one : just update & return */
443 sym->usl.spillLoc = sloc;
446 addSetHead(&sloc->usl.itmpStack,sym);
450 /* could not then have to create one , this is the hard part
451 we need to allocate this on the stack : this is really a
452 hack!! but cannot think of anything better at this time */
454 sprintf(buffer,"sloc%d",slocNum++);
455 sloc = newiTemp(buffer);
457 /* set the type to the spilling symbol */
458 sloc->type = copyLinkChain(sym->type);
459 sloc->etype = getSpec(sloc->type);
460 SPEC_SCLS(sloc->etype) = S_AUTO ;
462 /* we don't allow it to be allocated`
463 onto the external stack since : so we
464 temporarily turn it off ; we also
465 turn off memory model to prevent
466 the spil from going to the external storage
467 and turn off overlaying
471 sloc->isref = 1; /* to prevent compiler warning */
473 /* if it is on the stack then update the stack */
474 if (IN_STACK(sloc->etype)) {
475 currFunc->stack += getSize(sloc->type);
476 stackExtend += getSize(sloc->type);
478 dataExtend += getSize(sloc->type);
480 /* add it to the stackSpil set */
481 addSetHead(&stackSpil,sloc);
482 sym->usl.spillLoc = sloc;
485 /* add it to the set of itempStack set
486 of the spill location */
487 addSetHead(&sloc->usl.itmpStack,sym);
491 /*-----------------------------------------------------------------*/
492 /* isSpiltOnStack - returns true if the spil location is on stack */
493 /*-----------------------------------------------------------------*/
494 bool isSpiltOnStack (symbol *sym)
504 /* if (sym->stackSpil) */
507 if (!sym->usl.spillLoc)
510 etype = getSpec(sym->usl.spillLoc->type);
517 /*-----------------------------------------------------------------*/
518 /* spillThis - spils a specific operand */
519 /*-----------------------------------------------------------------*/
520 void spillThis (symbol *sym)
523 /* if this is rematerializable or has a spillLocation
524 we are okay, else we need to create a spillLocation
526 if (!(sym->remat || sym->usl.spillLoc))
527 createStackSpil (sym);
530 /* mark it has spilt & put it in the spilt set */
532 spiltSet = bitVectSetBit(spiltSet,sym->key);
534 bitVectUnSetBit(regAssigned,sym->key);
536 for (i = 0 ; i < sym->nRegs ; i++)
539 freeReg(sym->regs[i]);
543 /* if spilt on stack then free up r0 & r1
544 if they could have been assigned to some
546 if (!ptrRegReq && isSpiltOnStack(sym)) {
548 spillLRWithPtrReg(sym);
551 if (sym->usl.spillLoc && !sym->remat)
552 sym->usl.spillLoc->allocreq = 1;
556 /** Select a iTemp to spil : rather a simple procedure.
558 symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
560 bitVect *lrcs= NULL ;
564 /* get the spillable live ranges */
565 lrcs = computeSpillable (ic);
567 /* get all live ranges that are rematerizable */
568 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
570 /* return the least used of these */
571 return leastUsedLR(selectS);
575 /* get live ranges with spillLocations in direct space */
576 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
577 sym = leastUsedLR(selectS);
578 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
579 sym->usl.spillLoc->rname :
580 sym->usl.spillLoc->name));
582 /* mark it as allocation required */
583 sym->usl.spillLoc->allocreq = 1;
587 /* if the symbol is local to the block then */
588 if (forSym->liveTo < ebp->lSeq ) {
590 /* check if there are any live ranges allocated
591 to registers that are not used in this block */
592 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
593 sym = leastUsedLR(selectS);
594 /* if this is not rematerializable */
602 /* check if there are any live ranges that not
603 used in the remainder of the block */
604 if (!blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
605 sym = leastUsedLR (selectS);
613 /* find live ranges with spillocation && not used as pointers */
614 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
616 sym = leastUsedLR(selectS);
617 /* mark this as allocation required */
618 sym->usl.spillLoc->allocreq = 1;
623 /* find live ranges with spillocation */
624 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
626 sym = leastUsedLR(selectS);
627 sym->usl.spillLoc->allocreq = 1;
631 /* couldn't find then we need to create a spil
632 location on the stack , for which one? the least
634 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
635 /* return a created spil location */
636 sym = createStackSpil(leastUsedLR(selectS));
637 sym->usl.spillLoc->allocreq = 1;
641 /* this is an extreme situation we will spill
642 this one : happens very rarely but it does happen */
643 spillThis ( forSym );
648 /** Spil some variable & mark registers as free.
649 A spill occurs when an iTemp wont fit into the available registers.
651 bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
656 /* get something we can spil */
657 ssym = selectSpil(ic,ebp,forSym);
659 /* mark it as spilt */
661 spiltSet = bitVectSetBit(spiltSet,ssym->key);
663 /* mark it as not register assigned &
664 take it away from the set */
665 bitVectUnSetBit(regAssigned,ssym->key);
667 /* mark the registers as free */
668 for (i = 0 ; i < ssym->nRegs ;i++ )
670 freeReg(ssym->regs[i]);
673 /* if spilt on stack then free up r0 & r1
674 if they could have been assigned to as gprs */
675 if (!ptrRegReq && isSpiltOnStack(ssym) ) {
677 spillLRWithPtrReg(ssym);
680 /* if this was a block level spil then insert push & pop
681 at the start & end of block respectively */
682 if (ssym->blockSpil) {
683 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
684 /* add push to the start of the block */
685 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
686 ebp->sch->next : ebp->sch));
687 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
688 /* add pop to the end of the block */
689 addiCodeToeBBlock(ebp,nic,NULL);
692 /* if spilt because not used in the remainder of the
693 block then add a push before this instruction and
694 a pop at the end of the block */
695 if (ssym->remainSpil) {
697 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
698 /* add push just before this instruction */
699 addiCodeToeBBlock(ebp,nic,ic);
701 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
702 /* add pop to the end of the block */
703 addiCodeToeBBlock(ebp,nic,NULL);
713 /** Will try for GPR if not spil.
715 regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
720 /* try for gpr type */
721 if ((reg = allocReg(REG_GPR)))
725 if ((reg = allocReg(REG_PTR)))
728 /* we have to spil */
729 if (!spilSomething (ic,ebp,sym))
732 /* this looks like an infinite loop but
733 in really selectSpil will abort */
737 /** Symbol has a given register.
739 static bool symHasReg(symbol *sym,regs *reg)
743 for ( i = 0 ; i < sym->nRegs ; i++)
744 if (sym->regs[i] == reg)
750 /** Check the live to and if they have registers & are not spilt then
751 free up the registers
753 void deassignLRs (iCode *ic, eBBlock *ebp)
759 for (sym = hTabFirstItem(liveRanges,&k); sym;
760 sym = hTabNextItem(liveRanges,&k)) {
763 /* if it does not end here */
764 if (sym->liveTo > ic->seq )
767 /* if it was spilt on stack then we can
768 mark the stack spil location as free */
770 if (sym->stackSpil) {
771 sym->usl.spillLoc->isFree = 1;
777 if (!bitVectBitValue(regAssigned,sym->key))
780 /* special case check if this is an IFX &
781 the privious one was a pop and the
782 previous one was not spilt then keep track
784 if (ic->op == IFX && ic->prev &&
785 ic->prev->op == IPOP &&
786 !ic->prev->parmPush &&
787 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
788 psym = OP_SYMBOL(IC_LEFT(ic->prev));
793 bitVectUnSetBit(regAssigned,sym->key);
795 /* if the result of this one needs registers
796 and does not have it then assign it right
799 ! (SKIP_IC2(ic) || /* not a special icode */
800 ic->op == JUMPTABLE ||
805 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
806 result->liveTo > ic->seq && /* and will live beyond this */
807 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
808 result->regType == sym->regType && /* same register types */
809 result->nRegs && /* which needs registers */
810 ! result->isspilt && /* and does not already have them */
812 ! bitVectBitValue(regAssigned,result->key) &&
813 /* the number of free regs + number of regs in this LR
814 can accomodate the what result Needs */
815 ((nfreeRegsType(result->regType) +
816 sym->nRegs) >= result->nRegs)
819 for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++)
821 result->regs[i] = sym->regs[i] ;
823 result->regs[i] = getRegGpr (ic,ebp,result);
825 regAssigned = bitVectSetBit(regAssigned,result->key);
828 /* free the remaining */
829 for (; i < sym->nRegs ; i++) {
831 if (!symHasReg(psym,sym->regs[i]))
832 freeReg(sym->regs[i]);
834 freeReg(sym->regs[i]);
841 /** Reassign this to registers.
843 void reassignLR (operand *op)
845 symbol *sym = OP_SYMBOL(op);
848 /* not spilt any more */
849 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
850 bitVectUnSetBit(spiltSet,sym->key);
852 regAssigned = bitVectSetBit(regAssigned,sym->key);
856 for (i=0;i<sym->nRegs;i++)
857 sym->regs[i]->isFree = 0;
860 /** Determines if allocating will cause a spill.
862 int willCauseSpill ( int nr, int rt)
864 /* first check if there are any avlb registers
865 of te type required */
866 if (nFreeRegs(0) >= nr)
869 /* it will cause a spil */
873 /** The allocator can allocate same registers to result and operand,
874 if this happens make sure they are in the same position as the operand
875 otherwise chaos results.
877 static void positionRegs (symbol *result, symbol *opsym, int lineno)
879 int count = min(result->nRegs,opsym->nRegs);
880 int i , j = 0, shared = 0;
882 /* if the result has been spilt then cannot share */
887 /* first make sure that they actually share */
888 for ( i = 0 ; i < count; i++ ) {
889 for (j = 0 ; j < count ; j++ ) {
890 if (result->regs[i] == opsym->regs[j] && i !=j) {
898 regs *tmp = result->regs[i];
899 result->regs[i] = result->regs[j];
900 result->regs[j] = tmp;
905 /** Try to allocate a pair of registers to the symbol.
907 bool tryAllocatingRegPair(symbol *sym)
910 assert(sym->nRegs == 2);
911 for ( i = 0 ; i < nRegs ; i+=2 ) {
912 if ((regsZ80[i].isFree)&&(regsZ80[i+1].isFree)) {
913 regsZ80[i].isFree = 0;
914 sym->regs[0] = ®sZ80[i];
915 regsZ80[i+1].isFree = 0;
916 sym->regs[1] = ®sZ80[i+1];
919 bitVectSetBit(currFunc->regsUsed,i);
921 bitVectSetBit(currFunc->regsUsed,i+1);
929 /** Serially allocate registers to the variables.
930 This is the main register allocation function. It is called after
933 void serialRegAssign (eBBlock **ebbs, int count)
938 for (i = 0; i < count ; i++ ) {
942 if (ebbs[i]->noPath &&
943 (ebbs[i]->entryLabel != entryLabel &&
944 ebbs[i]->entryLabel != returnLabel ))
947 /* of all instructions do */
948 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
950 /* if this is an ipop that means some live
951 range will have to be assigned again */
953 reassignLR (IC_LEFT(ic));
955 /* if result is present && is a true symbol */
956 if (IC_RESULT(ic) && ic->op != IFX &&
957 IS_TRUE_SYMOP(IC_RESULT(ic)))
958 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
960 /* take away registers from live
961 ranges that end at this instruction */
962 deassignLRs (ic, ebbs[i]) ;
964 /* some don't need registers */
965 /* MLH: removed RESULT and POINTER_SET condition */
967 ic->op == JUMPTABLE ||
973 /* now we need to allocate registers only for the result */
975 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
980 /* if it does not need or is spilt
981 or is already assigned to registers
982 or will not live beyond this instructions */
985 bitVectBitValue(regAssigned,sym->key) ||
986 sym->liveTo <= ic->seq)
989 /* if some liverange has been spilt at the block level
990 and this one live beyond this block then spil this
992 if (blockSpil && sym->liveTo > ebbs[i]->lSeq) {
996 /* if trying to allocate this will cause
997 a spill and there is nothing to spill
998 or this one is rematerializable then
1000 willCS = willCauseSpill(sym->nRegs,sym->regType);
1001 spillable = computeSpillable(ic);
1003 (willCS && bitVectIsZero(spillable) ) ) {
1010 /* if it has a spillocation & is used less than
1011 all other live ranges then spill this */
1012 if ( willCS && sym->usl.spillLoc ) {
1015 leastUsedLR(liveRangesWith (spillable ,
1020 leastUsed->used > sym->used) {
1026 /* else we assign registers to it */
1027 regAssigned = bitVectSetBit(regAssigned,sym->key);
1029 /* Special case: Try to fit into a reg pair if
1031 if ((sym->nRegs == 2)&&tryAllocatingRegPair(sym)) {
1034 for (j = 0 ; j < sym->nRegs ;j++ ) {
1035 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
1037 /* if the allocation falied which means
1038 this was spilt then break */
1039 if (!sym->regs[j]) {
1044 /* if it shares registers with operands make sure
1045 that they are in the same position */
1046 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
1047 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
1048 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1049 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
1050 /* do the same for the right operand */
1051 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
1052 OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
1053 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1054 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1061 /*-----------------------------------------------------------------*/
1062 /* rUmaskForOp :- returns register mask for an operand */
1063 /*-----------------------------------------------------------------*/
1064 bitVect *rUmaskForOp (operand *op)
1070 /* only temporaries are assigned registers */
1074 sym = OP_SYMBOL(op);
1076 /* if spilt or no registers assigned to it
1078 if (sym->isspilt || !sym->nRegs)
1081 rumask = newBitVect(nRegs);
1083 for (j = 0; j < sym->nRegs; j++) {
1084 rumask = bitVectSetBit(rumask,
1085 sym->regs[j]->rIdx);
1091 /** Returns bit vector of registers used in iCode.
1093 bitVect *regsUsedIniCode (iCode *ic)
1095 bitVect *rmask = newBitVect(nRegs);
1097 /* do the special cases first */
1098 if (ic->op == IFX ) {
1099 rmask = bitVectUnion(rmask,
1100 rUmaskForOp(IC_COND(ic)));
1104 /* for the jumptable */
1105 if (ic->op == JUMPTABLE) {
1106 rmask = bitVectUnion(rmask,
1107 rUmaskForOp(IC_JTCOND(ic)));
1112 /* of all other cases */
1114 rmask = bitVectUnion(rmask,
1115 rUmaskForOp(IC_LEFT(ic)));
1119 rmask = bitVectUnion(rmask,
1120 rUmaskForOp(IC_RIGHT(ic)));
1123 rmask = bitVectUnion(rmask,
1124 rUmaskForOp(IC_RESULT(ic)));
1130 /** For each instruction will determine the regsUsed.
1132 void createRegMask (eBBlock **ebbs, int count)
1136 /* for all blocks */
1137 for (i = 0; i < count ; i++ ) {
1140 if ( ebbs[i]->noPath &&
1141 ( ebbs[i]->entryLabel != entryLabel &&
1142 ebbs[i]->entryLabel != returnLabel ))
1145 /* for all instructions */
1146 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1150 if (SKIP_IC2(ic) || !ic->rlive)
1153 /* first mark the registers used in this
1155 ic->rUsed = regsUsedIniCode(ic);
1156 funcrUsed = bitVectUnion(funcrUsed,ic->rUsed);
1158 /* now create the register mask for those
1159 registers that are in use : this is a
1160 super set of ic->rUsed */
1161 ic->rMask = newBitVect(nRegs+1);
1163 /* for all live Ranges alive at this point */
1164 for (j = 1; j < ic->rlive->size; j++ ) {
1168 /* if not alive then continue */
1169 if (!bitVectBitValue(ic->rlive,j))
1172 /* find the live range we are interested in */
1173 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1174 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1175 "createRegMask cannot find live range");
1179 /* if no register assigned to it */
1180 if (!sym->nRegs || sym->isspilt)
1183 /* for all the registers allocated to it */
1184 for (k = 0 ; k < sym->nRegs ;k++)
1187 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1193 /** Returns the rematerialized string for a remat var.
1195 char *rematStr (symbol *sym)
1198 iCode *ic = sym->rematiCode;
1202 /* if plus or minus print the right hand side */
1203 if (ic->op == '+' || ic->op == '-') {
1204 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1207 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1210 /* we reached the end */
1211 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1218 /*-----------------------------------------------------------------*/
1219 /* regTypeNum - computes the type & number of registers required */
1220 /*-----------------------------------------------------------------*/
1226 /* for each live range do */
1227 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1228 sym = hTabNextItem(liveRanges,&k)) {
1230 /* if used zero times then no registers needed */
1231 if ((sym->liveTo - sym->liveFrom) == 0)
1234 /* if the live range is a temporary */
1237 /* if the type is marked as a conditional */
1238 if (sym->regType == REG_CND)
1241 /* if used in return only then we don't
1243 if (sym->ruonly || sym->accuse) {
1244 if (IS_AGGREGATE(sym->type) || sym->isptr)
1245 sym->type = aggrToPtr(sym->type,FALSE);
1249 /* if not then we require registers */
1250 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1251 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1252 getSize(sym->type));
1254 if (sym->nRegs > 4) {
1255 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1256 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1259 /* determine the type of register required */
1260 /* Always general purpose */
1261 sym->regType = REG_GPR ;
1264 /* for the first run we don't provide */
1265 /* registers for true symbols we will */
1266 /* see how things go */
1272 /** Mark all registers as free.
1278 for (i=0;i< nRegs;i++ )
1279 regsZ80[i].isFree = 1;
1282 /*-----------------------------------------------------------------*/
1283 /* deallocStackSpil - this will set the stack pointer back */
1284 /*-----------------------------------------------------------------*/
1285 DEFSETFUNC(deallocStackSpil)
1293 /** Register reduction for assignment.
1295 int packRegsForAssign (iCode *ic,eBBlock *ebp)
1300 !IS_TRUE_SYMOP(IC_RESULT(ic)) ||
1301 !IS_ITEMP(IC_RIGHT(ic)) ||
1302 OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
1303 OP_SYMBOL(IC_RIGHT(ic))->isind)
1307 /* if the true symbol is defined in far space or on stack
1308 then we should not since this will increase register pressure */
1309 if (isOperandInFarSpace(IC_RESULT(ic))) {
1310 if ((dic = farSpacePackable(ic)))
1317 /* find the definition of iTempNN scanning backwards if we find a
1318 a use of the true symbol in before we find the definition then
1320 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1322 /* if there is a function call and this is
1323 a parameter & not my parameter then don't pack it */
1324 if ( (dic->op == CALL || dic->op == PCALL) &&
1325 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1326 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1335 if (IS_SYMOP(IC_RESULT(dic)) &&
1336 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1337 if (POINTER_SET(dic))
1342 if (IS_SYMOP(IC_RIGHT(dic)) &&
1343 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1344 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1349 if (IS_SYMOP(IC_LEFT(dic)) &&
1350 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1351 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1355 if (POINTER_SET(dic) &&
1356 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1364 return 0 ; /* did not find */
1366 /* if the result is on stack or iaccess then it must be
1367 the same atleast one of the operands */
1368 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1369 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1371 /* the operation has only one symbol
1372 operator then we can pack */
1373 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1374 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1377 if (!((IC_LEFT(dic) &&
1378 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1380 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1384 /* found the definition */
1385 /* replace the result with the result of */
1386 /* this assignment and remove this assignment */
1387 IC_RESULT(dic) = IC_RESULT(ic) ;
1389 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1390 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1392 /* delete from liverange table also
1393 delete from all the points inbetween and the new
1395 for ( sic = dic; sic != ic ; sic = sic->next ) {
1396 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1397 if (IS_ITEMP(IC_RESULT(dic)))
1398 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1401 remiCodeFromeBBlock(ebp,ic);
1406 /** Scanning backwards looks for first assig found.
1408 iCode *findAssignToSym (operand *op,iCode *ic)
1412 for (dic = ic->prev ; dic ; dic = dic->prev) {
1414 /* if definition by assignment */
1415 if (dic->op == '=' &&
1416 !POINTER_SET(dic) &&
1417 IC_RESULT(dic)->key == op->key)
1418 /* && IS_TRUE_SYMOP(IC_RIGHT(dic))*/
1421 /* we are interested only if defined in far space */
1422 /* or in stack space in case of + & - */
1424 /* if assigned to a non-symbol then return
1426 if (!IS_SYMOP(IC_RIGHT(dic)))
1429 /* if the symbol is in far space then
1431 if (isOperandInFarSpace(IC_RIGHT(dic)))
1434 /* for + & - operations make sure that
1435 if it is on the stack it is the same
1436 as one of the three operands */
1437 if ((ic->op == '+' || ic->op == '-') &&
1438 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1440 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1441 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1442 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1450 /* if we find an usage then we cannot delete it */
1451 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1454 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1457 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1461 /* now make sure that the right side of dic
1462 is not defined between ic & dic */
1464 iCode *sic = dic->next ;
1466 for (; sic != ic ; sic = sic->next)
1467 if (IC_RESULT(sic) &&
1468 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1477 /*-----------------------------------------------------------------*/
1478 /* packRegsForSupport :- reduce some registers for support calls */
1479 /*-----------------------------------------------------------------*/
1480 int packRegsForSupport (iCode *ic, eBBlock *ebp)
1483 /* for the left & right operand :- look to see if the
1484 left was assigned a true symbol in far space in that
1485 case replace them */
1486 if (IS_ITEMP(IC_LEFT(ic)) &&
1487 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1488 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1494 /* found it we need to remove it from the
1496 for ( sic = dic; sic != ic ; sic = sic->next )
1497 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1499 IC_LEFT(ic)->operand.symOperand =
1500 IC_RIGHT(dic)->operand.symOperand;
1501 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1502 remiCodeFromeBBlock(ebp,dic);
1506 /* do the same for the right operand */
1509 IS_ITEMP(IC_RIGHT(ic)) &&
1510 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1511 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1517 /* found it we need to remove it from the block */
1518 for ( sic = dic; sic != ic ; sic = sic->next )
1519 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1521 IC_RIGHT(ic)->operand.symOperand =
1522 IC_RIGHT(dic)->operand.symOperand;
1523 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1525 remiCodeFromeBBlock(ebp,dic);
1532 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1534 /** Will reduce some registers for single use.
1536 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1541 /* if returning a literal then do nothing */
1545 /* only upto 2 bytes since we cannot predict
1546 the usage of b, & acc */
1547 if (getSize(operandType(op)) > 2 &&
1552 /* this routine will mark the a symbol as used in one
1553 instruction use only && if the defintion is local
1554 (ie. within the basic block) && has only one definition &&
1555 that definiion is either a return value from a
1556 function or does not contain any variables in
1558 uses = bitVectCopy(OP_USES(op));
1559 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1560 if (!bitVectIsZero(uses)) /* has other uses */
1563 /* if it has only one defintion */
1564 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1565 return NULL ; /* has more than one definition */
1567 /* get the that definition */
1569 hTabItemWithKey(iCodehTab,
1570 bitVectFirstBit(OP_DEFS(op)))))
1573 /* found the definition now check if it is local */
1574 if (dic->seq < ebp->fSeq ||
1575 dic->seq > ebp->lSeq)
1576 return NULL ; /* non-local */
1578 /* now check if it is the return from a function call */
1579 if (dic->op == CALL || dic->op == PCALL ) {
1580 if (ic->op != SEND && ic->op != RETURN) {
1581 OP_SYMBOL(op)->ruonly = 1;
1587 /* otherwise check that the definition does
1588 not contain any symbols in far space */
1589 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1590 isOperandInFarSpace(IC_RIGHT(dic)) ||
1591 IS_OP_RUONLY(IC_LEFT(ic)) ||
1592 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1596 /* if pointer set then make sure the pointer is one byte */
1597 if (POINTER_SET(dic))
1600 if (POINTER_GET(dic))
1605 /* also make sure the intervenening instructions
1606 don't have any thing in far space */
1607 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1608 /* if there is an intervening function call then no */
1609 if (dic->op == CALL || dic->op == PCALL)
1611 /* if pointer set then make sure the pointer
1613 if (POINTER_SET(dic))
1616 if (POINTER_GET(dic))
1619 /* if address of & the result is remat the okay */
1620 if (dic->op == ADDRESS_OF &&
1621 OP_SYMBOL(IC_RESULT(dic))->remat)
1624 /* if left or right or result is in far space */
1625 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1626 isOperandInFarSpace(IC_RIGHT(dic)) ||
1627 isOperandInFarSpace(IC_RESULT(dic)) ||
1628 IS_OP_RUONLY(IC_LEFT(dic)) ||
1629 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1630 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1635 OP_SYMBOL(op)->ruonly = 1;
1639 /*-----------------------------------------------------------------*/
1640 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1641 /*-----------------------------------------------------------------*/
1642 static bool isBitwiseOptimizable (iCode *ic)
1644 link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1646 /* bitwise operations are considered optimizable
1647 under the following conditions (Jean-Louis VERN)
1659 if (IS_LITERAL(rtype))
1664 /** Pack registers for acc use.
1665 When the result of this operation is small and short lived it may
1666 be able to be stored in the accumelator.
1668 void packRegsForAccUse (iCode *ic)
1672 /* if + or - then it has to be one byte result */
1673 if ((ic->op == '+' || ic->op == '-')
1674 && getSize(operandType(IC_RESULT(ic))) > 1)
1677 /* if shift operation make sure right side is not a literal */
1678 if (ic->op == RIGHT_OP &&
1679 (isOperandLiteral(IC_RIGHT(ic)) ||
1680 getSize(operandType(IC_RESULT(ic))) > 1))
1683 if (ic->op == LEFT_OP &&
1684 ( isOperandLiteral(IC_RIGHT(ic)) ||
1685 getSize(operandType(IC_RESULT(ic))) > 1))
1688 /* has only one definition */
1689 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1692 /* has only one use */
1693 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1696 /* and the usage immediately follows this iCode */
1697 if (!(uic = hTabItemWithKey(iCodehTab,
1698 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1701 if (ic->next != uic)
1704 /* if it is a conditional branch then we definitely can */
1705 if (uic->op == IFX )
1708 if ( uic->op == JUMPTABLE )
1712 /* if the usage is not is an assignment or an
1713 arithmetic / bitwise / shift operation then not */
1714 if (POINTER_SET(uic) &&
1715 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1719 if (uic->op != '=' &&
1720 !IS_ARITHMETIC_OP(uic) &&
1721 !IS_BITWISE_OP(uic) &&
1722 uic->op != LEFT_OP &&
1723 uic->op != RIGHT_OP )
1726 /* if used in ^ operation then make sure right is not a
1728 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1731 /* if shift operation make sure right side is not a literal */
1732 if (uic->op == RIGHT_OP &&
1733 ( isOperandLiteral(IC_RIGHT(uic)) ||
1734 getSize(operandType(IC_RESULT(uic))) > 1))
1737 if (uic->op == LEFT_OP &&
1738 ( isOperandLiteral(IC_RIGHT(uic)) ||
1739 getSize(operandType(IC_RESULT(uic))) > 1))
1743 /* make sure that the result of this icode is not on the
1744 stack, since acc is used to compute stack offset */
1745 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1746 OP_SYMBOL(IC_RESULT(uic))->onStack)
1751 /* if either one of them in far space then we cannot */
1752 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1753 isOperandInFarSpace(IC_LEFT(uic))) ||
1754 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1755 isOperandInFarSpace(IC_RIGHT(uic))))
1759 /* if the usage has only one operand then we can */
1760 if (IC_LEFT(uic) == NULL ||
1761 IC_RIGHT(uic) == NULL)
1764 /* make sure this is on the left side if not
1765 a '+' since '+' is commutative */
1766 if (ic->op != '+' &&
1767 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1770 /* if one of them is a literal then we can */
1771 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1772 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1773 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
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)))))
1789 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1792 /** Does some transformations to reduce register pressure.
1794 void packRegisters (eBBlock *ebp)
1801 /* look for assignments of the form */
1802 /* iTempNN = TRueSym (someoperation) SomeOperand */
1804 /* TrueSym := iTempNN:1 */
1805 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
1806 /* find assignment of the form TrueSym := iTempNN:1 */
1807 if (ic->op == '=' && !POINTER_SET(ic))
1808 change += packRegsForAssign(ic,ebp);
1814 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
1815 /* Safe: address of a true sym is always constant. */
1816 /* if this is an itemp & result of a address of a true sym
1817 then mark this as rematerialisable */
1818 if (ic->op == ADDRESS_OF &&
1819 IS_ITEMP(IC_RESULT(ic)) &&
1820 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
1821 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
1822 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
1824 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
1825 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
1826 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
1829 /* Safe: just propagates the remat flag */
1830 /* if straight assignment then carry remat flag if this is the
1832 if (ic->op == '=' &&
1834 IS_SYMOP(IC_RIGHT(ic)) &&
1835 OP_SYMBOL(IC_RIGHT(ic))->remat &&
1836 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
1838 OP_SYMBOL(IC_RESULT(ic))->remat =
1839 OP_SYMBOL(IC_RIGHT(ic))->remat;
1840 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
1841 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
1844 /* if the condition of an if instruction is defined in the
1845 previous instruction then mark the itemp as a conditional */
1846 if ((IS_CONDITIONAL(ic) ||
1847 ( ( ic->op == BITWISEAND ||
1850 isBitwiseOptimizable(ic))) &&
1851 ic->next && ic->next->op == IFX &&
1852 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
1853 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
1855 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
1860 /* reduce for support function calls */
1861 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
1862 packRegsForSupport(ic,ebp);
1866 /* some cases the redundant moves can
1867 can be eliminated for return statements */
1868 if ((ic->op == RETURN || ic->op == SEND) &&
1869 !isOperandInFarSpace(IC_LEFT(ic)) &&
1871 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
1875 /* if pointer set & left has a size more than
1876 one and right is not in far space */
1877 if (POINTER_SET(ic) &&
1878 !isOperandInFarSpace(IC_RIGHT(ic)) &&
1879 !OP_SYMBOL(IC_RESULT(ic))->remat &&
1880 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
1881 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 )
1883 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
1887 /* if pointer get */
1888 if (POINTER_GET(ic) &&
1889 !isOperandInFarSpace(IC_RESULT(ic))&&
1890 !OP_SYMBOL(IC_LEFT(ic))->remat &&
1891 !IS_OP_RUONLY(IC_RESULT(ic)) &&
1892 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 )
1894 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
1897 /* pack registers for accumulator use, when the result of an
1898 arithmetic or bit wise operation has only one use, that use is
1899 immediately following the defintion and the using iCode has
1900 only one operand or has two operands but one is literal & the
1901 result of that operation is not on stack then we can leave the
1902 result of this operation in acc:b combination */
1903 if ((IS_ARITHMETIC_OP(ic)
1904 || IS_BITWISE_OP(ic)
1905 || ic->op == LEFT_OP || ic->op == RIGHT_OP
1907 IS_ITEMP(IC_RESULT(ic)) &&
1908 getSize(operandType(IC_RESULT(ic))) <= 2)
1909 packRegsForAccUse (ic);
1913 /*-----------------------------------------------------------------*/
1914 /* assignRegisters - assigns registers to each live range as need */
1915 /*-----------------------------------------------------------------*/
1916 void z80_assignRegisters (eBBlock **ebbs, int count)
1921 setToNull((void *)&funcrUsed);
1922 ptrRegReq = stackExtend = dataExtend = 0;
1923 /* if not register extentions then reduce number
1927 /* change assignments this will remove some
1928 live ranges reducing some register pressure */
1929 for (i = 0 ; i < count ;i++ )
1930 packRegisters (ebbs[i]);
1932 if (options.dump_pack)
1933 dumpEbbsToFileExt(".dumppack",ebbs,count);
1935 /* first determine for each live range the number of
1936 registers & the type of registers required for each */
1939 /* and serially allocate registers */
1940 serialRegAssign(ebbs,count);
1942 /* if stack was extended then tell the user */
1944 /* werror(W_TOOMANY_SPILS,"stack", */
1945 /* stackExtend,currFunc->name,""); */
1950 /* werror(W_TOOMANY_SPILS,"data space", */
1951 /* dataExtend,currFunc->name,""); */
1955 if (options.dump_rassgn)
1956 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
1958 /* after that create the register mask
1959 for each of the instruction */
1960 createRegMask (ebbs,count);
1962 /* now get back the chain */
1963 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
1965 /* redo that offsets for stacked automatic variables */
1966 redoStackOffsets ();
1970 /* free up any stackSpil locations allocated */
1971 applyToSet(stackSpil,deallocStackSpil);
1973 setToNull((void **)&stackSpil);
1974 setToNull((void **)&spiltSet);
1975 /* mark all registers as free */