1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable*/
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
39 extern void gen51Code(iCode *);
48 bitVect *funcrUsed; /* registers used in a function */
53 /* Shared with gen.c */
54 int mcs51_ptrRegReq; /* one byte pointer register required */
60 { REG_GPR ,R2_IDX , REG_GPR , "r2", "ar2", "0", 2, 1 },
61 { REG_GPR ,R3_IDX , REG_GPR , "r3", "ar3", "0", 3, 1 },
62 { REG_GPR ,R4_IDX , REG_GPR , "r4", "ar4", "0", 4, 1 },
63 { REG_GPR ,R5_IDX , REG_GPR , "r5", "ar5", "0", 5, 1 },
64 { REG_GPR ,R6_IDX , REG_GPR , "r6", "ar6", "0", 6, 1 },
65 { REG_GPR ,R7_IDX , REG_GPR , "r7", "ar7", "0", 7, 1 },
66 { REG_PTR ,R0_IDX , REG_PTR , "r0" , "ar0", "0", 0, 1 },
67 { REG_PTR ,R1_IDX , REG_PTR , "r1" , "ar1", "0", 1, 1 },
68 { REG_GPR ,X8_IDX , REG_GPR , "x8", "x8" , "xreg", 0, 1 },
69 { REG_GPR ,X9_IDX , REG_GPR , "x9", "x9" , "xreg", 1, 1 },
70 { REG_GPR ,X10_IDX,REG_GPR , "x10", "x10", "xreg", 2, 1 },
71 { REG_GPR ,X11_IDX,REG_GPR , "x11", "x11", "xreg", 3, 1 },
72 { REG_GPR ,X12_IDX,REG_GPR , "x12", "x12", "xreg", 4, 1 },
73 { REG_CND ,CND_IDX,REG_CND , "C" , "C" , "xreg", 0, 1 },
76 static void spillThis (symbol *);
78 /*-----------------------------------------------------------------*/
79 /* allocReg - allocates register of given type */
80 /*-----------------------------------------------------------------*/
81 static regs *allocReg (short type)
85 for ( i = 0 ; i < mcs51_nRegs ; i++ ) {
87 /* if type is given as 0 then any
88 free register will do */
90 regs8051[i].isFree ) {
91 regs8051[i].isFree = 0;
94 bitVectSetBit(currFunc->regsUsed,i);
97 /* other wise look for specific type
99 if (regs8051[i].isFree &&
100 regs8051[i].type == type) {
101 regs8051[i].isFree = 0;
104 bitVectSetBit(currFunc->regsUsed,i);
111 /*-----------------------------------------------------------------*/
112 /* mcs51_regWithIdx - returns pointer to register wit index number */
113 /*-----------------------------------------------------------------*/
114 regs *mcs51_regWithIdx (int idx)
118 for (i=0;i < mcs51_nRegs;i++)
119 if (regs8051[i].rIdx == idx)
122 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
123 "regWithIdx not found");
127 /*-----------------------------------------------------------------*/
128 /* freeReg - frees a register */
129 /*-----------------------------------------------------------------*/
130 static void freeReg (regs *reg)
136 /*-----------------------------------------------------------------*/
137 /* nFreeRegs - returns number of free registers */
138 /*-----------------------------------------------------------------*/
139 static int nFreeRegs (int type)
144 for (i = 0 ; i < mcs51_nRegs; i++ )
145 if (regs8051[i].isFree && regs8051[i].type == type)
150 /*-----------------------------------------------------------------*/
151 /* nfreeRegsType - free registers with type */
152 /*-----------------------------------------------------------------*/
153 static int nfreeRegsType (int type)
156 if (type == REG_PTR) {
157 if ((nfr = nFreeRegs(type)) == 0)
158 return nFreeRegs(REG_GPR);
161 return nFreeRegs(type);
165 /*-----------------------------------------------------------------*/
166 /* allDefsOutOfRange - all definitions are out of a range */
167 /*-----------------------------------------------------------------*/
168 static bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
175 for ( i = 0 ;i < defs->size ; i++ ) {
178 if (bitVectBitValue(defs,i) &&
179 (ic = hTabItemWithKey(iCodehTab,i)) &&
180 ( ic->seq >= fseq && ic->seq <= toseq))
189 /*-----------------------------------------------------------------*/
190 /* computeSpillable - given a point find the spillable live ranges */
191 /*-----------------------------------------------------------------*/
192 static bitVect *computeSpillable (iCode *ic)
196 /* spillable live ranges are those that are live at this
197 point . the following categories need to be subtracted
199 a) - those that are already spilt
200 b) - if being used by this one
201 c) - defined by this one */
203 spillable = bitVectCopy(ic->rlive);
205 bitVectCplAnd(spillable,_G.spiltSet); /* those already spilt */
207 bitVectCplAnd(spillable,ic->uses); /* used in this one */
208 bitVectUnSetBit(spillable,ic->defKey);
209 spillable = bitVectIntersect(spillable,_G.regAssigned);
214 /*-----------------------------------------------------------------*/
215 /* noSpilLoc - return true if a variable has no spil location */
216 /*-----------------------------------------------------------------*/
217 static int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
219 return (sym->usl.spillLoc ? 0 : 1);
222 /*-----------------------------------------------------------------*/
223 /* hasSpilLoc - will return 1 if the symbol has spil location */
224 /*-----------------------------------------------------------------*/
225 static int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
227 return (sym->usl.spillLoc ? 1 : 0);
230 /*-----------------------------------------------------------------*/
231 /* directSpilLoc - will return 1 if the splilocation is in direct */
232 /*-----------------------------------------------------------------*/
233 static int directSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
235 if ( sym->usl.spillLoc &&
236 (IN_DIRSPACE(SPEC_OCLS(sym->usl.spillLoc->etype))))
242 /*-----------------------------------------------------------------*/
243 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location*/
244 /* but is not used as a pointer */
245 /*-----------------------------------------------------------------*/
246 static int hasSpilLocnoUptr (symbol *sym, eBBlock *ebp, iCode *ic)
248 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
251 /*-----------------------------------------------------------------*/
252 /* rematable - will return 1 if the remat flag is set */
253 /*-----------------------------------------------------------------*/
254 static int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
259 /*-----------------------------------------------------------------*/
260 /* notUsedInBlock - not used in this block */
261 /*-----------------------------------------------------------------*/
262 static int notUsedInBlock (symbol *sym, eBBlock *ebp, iCode *ic)
264 return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs) &&
265 allDefsOutOfRange (sym->defs,ebp->fSeq,ebp->lSeq));
266 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
269 /*-----------------------------------------------------------------*/
270 /* notUsedInRemaining - not used or defined in remain of the block */
271 /*-----------------------------------------------------------------*/
272 static int notUsedInRemaining (symbol *sym, eBBlock *ebp, iCode *ic)
274 return ((usedInRemaining (operandFromSymbol(sym),ic) ? 0 : 1) &&
275 allDefsOutOfRange (sym->defs,ic->seq,ebp->lSeq));
278 /*-----------------------------------------------------------------*/
279 /* allLRs - return true for all */
280 /*-----------------------------------------------------------------*/
281 static int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
286 /*-----------------------------------------------------------------*/
287 /* liveRangesWith - applies function to a given set of live range */
288 /*-----------------------------------------------------------------*/
289 static set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
290 eBBlock *ebp, iCode *ic)
295 if (!lrs || !lrs->size)
298 for ( i = 1 ; i < lrs->size ; i++ ) {
300 if (!bitVectBitValue(lrs,i))
303 /* if we don't find it in the live range
304 hash table we are in serious trouble */
305 if (!(sym = hTabItemWithKey(liveRanges,i))) {
306 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
307 "liveRangesWith could not find liveRange");
311 if (func(sym,ebp,ic) && bitVectBitValue(_G.regAssigned,sym->key))
312 addSetHead(&rset,sym);
319 /*-----------------------------------------------------------------*/
320 /* leastUsedLR - given a set determines which is the least used */
321 /*-----------------------------------------------------------------*/
322 static symbol *leastUsedLR (set *sset)
324 symbol *sym = NULL, *lsym = NULL ;
326 sym = lsym = setFirstItem(sset);
331 for (; lsym; lsym = setNextItem(sset)) {
333 /* if usage is the same then prefer
334 the spill the smaller of the two */
335 if ( lsym->used == sym->used )
336 if (getSize(lsym->type) < getSize(sym->type))
340 if (lsym->used < sym->used )
345 setToNull((void **)&sset);
350 /*-----------------------------------------------------------------*/
351 /* noOverLap - will iterate through the list looking for over lap */
352 /*-----------------------------------------------------------------*/
353 static int noOverLap (set *itmpStack, symbol *fsym)
358 for (sym = setFirstItem(itmpStack); sym;
359 sym = setNextItem(itmpStack)) {
360 if (sym->liveTo > fsym->liveFrom )
368 /*-----------------------------------------------------------------*/
369 /* isFree - will return 1 if the a free spil location is found */
370 /*-----------------------------------------------------------------*/
371 static DEFSETFUNC(isFree)
374 V_ARG(symbol **,sloc);
375 V_ARG(symbol *,fsym);
377 /* if already found */
381 /* if it is free && and the itmp assigned to
382 this does not have any overlapping live ranges
383 with the one currently being assigned and
384 the size can be accomodated */
386 noOverLap(sym->usl.itmpStack,fsym) &&
387 getSize(sym->type) >= getSize(fsym->type)) {
395 /*-----------------------------------------------------------------*/
396 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
397 /*-----------------------------------------------------------------*/
398 static void spillLRWithPtrReg (symbol *forSym)
404 if (!_G.regAssigned ||
405 bitVectIsZero(_G.regAssigned))
408 r0 = mcs51_regWithIdx(R0_IDX);
409 r1 = mcs51_regWithIdx(R1_IDX);
411 /* for all live ranges */
412 for (lrsym = hTabFirstItem(liveRanges,&k) ; lrsym ;
413 lrsym = hTabNextItem(liveRanges,&k) ) {
416 /* if no registers assigned to it or
418 /* if it does not overlap with this then
419 not need to spill it */
421 if (lrsym->isspilt || !lrsym->nRegs ||
422 (lrsym->liveTo < forSym->liveFrom))
425 /* go thru the registers : if it is either
426 r0 or r1 then spil it */
427 for (j = 0 ; j < lrsym->nRegs ; j++ )
428 if (lrsym->regs[j] == r0 ||
429 lrsym->regs[j] == r1 ) {
437 /*-----------------------------------------------------------------*/
438 /* createStackSpil - create a location on the stack to spil */
439 /*-----------------------------------------------------------------*/
440 static symbol *createStackSpil (symbol *sym)
443 int useXstack, model, noOverlay;
448 /* first go try and find a free one that is already
449 existing on the stack */
450 if (applyToSet(_G.stackSpil,isFree,&sloc, sym)) {
451 /* found a free one : just update & return */
452 sym->usl.spillLoc = sloc;
455 addSetHead(&sloc->usl.itmpStack,sym);
459 /* could not then have to create one , this is the hard part
460 we need to allocate this on the stack : this is really a
461 hack!! but cannot think of anything better at this time */
463 if (sprintf(slocBuffer,"sloc%d",_G.slocNum++) >= sizeof(slocBuffer))
465 fprintf(stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
470 sloc = newiTemp(slocBuffer);
472 /* set the type to the spilling symbol */
473 sloc->type = copyLinkChain(sym->type);
474 sloc->etype = getSpec(sloc->type);
475 SPEC_SCLS(sloc->etype) = S_AUTO ;
476 SPEC_EXTR(sloc->etype) = 0;
478 /* we don't allow it to be allocated`
479 onto the external stack since : so we
480 temporarily turn it off ; we also
481 turn off memory model to prevent
482 the spil from going to the external storage
483 and turn off overlaying
486 useXstack = options.useXstack;
487 model = options.model;
488 noOverlay = options.noOverlay;
489 stackAuto = options.stackAuto;
490 options.noOverlay = 1;
491 options.model = options.useXstack = 0;
495 options.useXstack = useXstack;
496 options.model = model;
497 options.noOverlay = noOverlay;
498 options.stackAuto = stackAuto;
499 sloc->isref = 1; /* to prevent compiler warning */
501 /* if it is on the stack then update the stack */
502 if (IN_STACK(sloc->etype)) {
503 currFunc->stack += getSize(sloc->type);
504 _G.stackExtend += getSize(sloc->type);
506 _G.dataExtend += getSize(sloc->type);
508 /* add it to the _G.stackSpil set */
509 addSetHead(&_G.stackSpil,sloc);
510 sym->usl.spillLoc = sloc;
513 /* add it to the set of itempStack set
514 of the spill location */
515 addSetHead(&sloc->usl.itmpStack,sym);
519 /*-----------------------------------------------------------------*/
520 /* isSpiltOnStack - returns true if the spil location is on stack */
521 /*-----------------------------------------------------------------*/
522 static bool isSpiltOnStack (symbol *sym)
532 /* if (sym->_G.stackSpil) */
535 if (!sym->usl.spillLoc)
538 etype = getSpec(sym->usl.spillLoc->type);
545 /*-----------------------------------------------------------------*/
546 /* spillThis - spils a specific operand */
547 /*-----------------------------------------------------------------*/
548 static void spillThis (symbol *sym)
551 /* if this is rematerializable or has a spillLocation
552 we are okay, else we need to create a spillLocation
554 if (!(sym->remat || sym->usl.spillLoc))
555 createStackSpil (sym);
558 /* mark it has spilt & put it in the spilt set */
560 _G.spiltSet = bitVectSetBit(_G.spiltSet,sym->key);
562 bitVectUnSetBit(_G.regAssigned,sym->key);
564 for (i = 0 ; i < sym->nRegs ; i++)
567 freeReg(sym->regs[i]);
571 /* if spilt on stack then free up r0 & r1
572 if they could have been assigned to some
574 if (!mcs51_ptrRegReq && isSpiltOnStack(sym)) {
576 spillLRWithPtrReg(sym);
579 if (sym->usl.spillLoc && !sym->remat)
580 sym->usl.spillLoc->allocreq = 1;
584 /*-----------------------------------------------------------------*/
585 /* selectSpil - select a iTemp to spil : rather a simple procedure */
586 /*-----------------------------------------------------------------*/
587 static symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
589 bitVect *lrcs= NULL ;
593 /* get the spillable live ranges */
594 lrcs = computeSpillable (ic);
596 /* get all live ranges that are rematerizable */
597 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
599 /* return the least used of these */
600 return leastUsedLR(selectS);
603 /* get live ranges with spillLocations in direct space */
604 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
605 sym = leastUsedLR(selectS);
606 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
607 sym->usl.spillLoc->rname :
608 sym->usl.spillLoc->name));
610 /* mark it as allocation required */
611 sym->usl.spillLoc->allocreq = 1;
615 /* if the symbol is local to the block then */
616 if (forSym->liveTo < ebp->lSeq ) {
618 /* check if there are any live ranges allocated
619 to registers that are not used in this block */
620 if (!_G.blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
621 sym = leastUsedLR(selectS);
622 /* if this is not rematerializable */
630 /* check if there are any live ranges that not
631 used in the remainder of the block */
632 if (!_G.blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
633 sym = leastUsedLR (selectS);
642 /* find live ranges with spillocation && not used as pointers */
643 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
645 sym = leastUsedLR(selectS);
646 /* mark this as allocation required */
647 sym->usl.spillLoc->allocreq = 1;
651 /* find live ranges with spillocation */
652 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
654 sym = leastUsedLR(selectS);
655 sym->usl.spillLoc->allocreq = 1;
659 /* couldn't find then we need to create a spil
660 location on the stack , for which one? the least
662 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
664 /* return a created spil location */
665 sym = createStackSpil(leastUsedLR(selectS));
666 sym->usl.spillLoc->allocreq = 1;
670 /* this is an extreme situation we will spill
671 this one : happens very rarely but it does happen */
672 spillThis ( forSym );
677 /*-----------------------------------------------------------------*/
678 /* spilSomething - spil some variable & mark registers as free */
679 /*-----------------------------------------------------------------*/
680 static bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
685 /* get something we can spil */
686 ssym = selectSpil(ic,ebp,forSym);
688 /* mark it as spilt */
690 _G.spiltSet = bitVectSetBit(_G.spiltSet,ssym->key);
692 /* mark it as not register assigned &
693 take it away from the set */
694 bitVectUnSetBit(_G.regAssigned,ssym->key);
696 /* mark the registers as free */
697 for (i = 0 ; i < ssym->nRegs ;i++ )
699 freeReg(ssym->regs[i]);
701 /* if spilt on stack then free up r0 & r1
702 if they could have been assigned to as gprs */
703 if (!mcs51_ptrRegReq && isSpiltOnStack(ssym) ) {
705 spillLRWithPtrReg(ssym);
708 /* if this was a block level spil then insert push & pop
709 at the start & end of block respectively */
710 if (ssym->blockSpil) {
711 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
712 /* add push to the start of the block */
713 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
714 ebp->sch->next : ebp->sch));
715 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
716 /* add pop to the end of the block */
717 addiCodeToeBBlock(ebp,nic,NULL);
720 /* if spilt because not used in the remainder of the
721 block then add a push before this instruction and
722 a pop at the end of the block */
723 if (ssym->remainSpil) {
725 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
726 /* add push just before this instruction */
727 addiCodeToeBBlock(ebp,nic,ic);
729 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
730 /* add pop to the end of the block */
731 addiCodeToeBBlock(ebp,nic,NULL);
740 /*-----------------------------------------------------------------*/
741 /* getRegPtr - will try for PTR if not a GPR type if not spil */
742 /*-----------------------------------------------------------------*/
743 regs *getRegPtr (iCode *ic, eBBlock *ebp, symbol *sym)
748 /* try for a ptr type */
749 if ((reg = allocReg(REG_PTR)))
752 /* try for gpr type */
753 if ((reg = allocReg(REG_GPR)))
756 /* we have to spil */
757 if (!spilSomething (ic,ebp,sym))
760 /* this looks like an infinite loop but
761 in really selectSpil will abort */
765 /*-----------------------------------------------------------------*/
766 /* getRegGpr - will try for GPR if not spil */
767 /*-----------------------------------------------------------------*/
768 static regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
773 /* try for gpr type */
774 if ((reg = allocReg(REG_GPR)))
777 if (!mcs51_ptrRegReq)
778 if ((reg = allocReg(REG_PTR)))
781 /* we have to spil */
782 if (!spilSomething (ic,ebp,sym))
785 /* this looks like an infinite loop but
786 in really selectSpil will abort */
790 /*-----------------------------------------------------------------*/
791 /* symHasReg - symbol has a given register */
792 /*-----------------------------------------------------------------*/
793 static bool symHasReg(symbol *sym,regs *reg)
797 for ( i = 0 ; i < sym->nRegs ; i++)
798 if (sym->regs[i] == reg)
804 /*-----------------------------------------------------------------*/
805 /* deassignLRs - check the live to and if they have registers & are*/
806 /* not spilt then free up the registers */
807 /*-----------------------------------------------------------------*/
808 static void deassignLRs (iCode *ic, eBBlock *ebp)
814 for (sym = hTabFirstItem(liveRanges,&k); sym;
815 sym = hTabNextItem(liveRanges,&k)) {
818 /* if it does not end here */
819 if (sym->liveTo > ic->seq )
822 /* if it was spilt on stack then we can
823 mark the stack spil location as free */
825 if (sym->stackSpil) {
826 sym->usl.spillLoc->isFree = 1;
832 if (!bitVectBitValue(_G.regAssigned,sym->key))
835 /* special case check if this is an IFX &
836 the privious one was a pop and the
837 previous one was not spilt then keep track
839 if (ic->op == IFX && ic->prev &&
840 ic->prev->op == IPOP &&
841 !ic->prev->parmPush &&
842 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
843 psym = OP_SYMBOL(IC_LEFT(ic->prev));
848 bitVectUnSetBit(_G.regAssigned,sym->key);
850 /* if the result of this one needs registers
851 and does not have it then assign it right
854 ! (SKIP_IC2(ic) || /* not a special icode */
855 ic->op == JUMPTABLE ||
861 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
862 result->liveTo > ic->seq && /* and will live beyond this */
863 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
864 result->regType == sym->regType && /* same register types */
865 result->nRegs && /* which needs registers */
866 ! result->isspilt && /* and does not already have them */
868 ! bitVectBitValue(_G.regAssigned,result->key) &&
869 /* the number of free regs + number of regs in this LR
870 can accomodate the what result Needs */
871 ((nfreeRegsType(result->regType) +
872 sym->nRegs) >= result->nRegs)
875 for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++)
877 result->regs[i] = sym->regs[i] ;
879 result->regs[i] = getRegGpr (ic,ebp,result);
881 _G.regAssigned = bitVectSetBit(_G.regAssigned,result->key);
885 /* free the remaining */
886 for (; i < sym->nRegs ; i++) {
888 if (!symHasReg(psym,sym->regs[i]))
889 freeReg(sym->regs[i]);
891 freeReg(sym->regs[i]);
898 /*-----------------------------------------------------------------*/
899 /* reassignLR - reassign this to registers */
900 /*-----------------------------------------------------------------*/
901 static void reassignLR (operand *op)
903 symbol *sym = OP_SYMBOL(op);
906 /* not spilt any more */
907 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
908 bitVectUnSetBit(_G.spiltSet,sym->key);
910 _G.regAssigned = bitVectSetBit(_G.regAssigned,sym->key);
914 for (i=0;i<sym->nRegs;i++)
915 sym->regs[i]->isFree = 0;
918 /*-----------------------------------------------------------------*/
919 /* willCauseSpill - determines if allocating will cause a spill */
920 /*-----------------------------------------------------------------*/
921 static int willCauseSpill ( int nr, int rt)
923 /* first check if there are any avlb registers
924 of te type required */
926 /* special case for pointer type
927 if pointer type not avlb then
928 check for type gpr */
929 if (nFreeRegs(rt) >= nr)
931 if (nFreeRegs(REG_GPR) >= nr)
934 if (mcs51_ptrRegReq) {
935 if (nFreeRegs(rt) >= nr)
938 if (nFreeRegs(REG_PTR) +
939 nFreeRegs(REG_GPR) >= nr)
944 /* it will cause a spil */
948 /*-----------------------------------------------------------------*/
949 /* positionRegs - the allocator can allocate same registers to res-*/
950 /* ult and operand, if this happens make sure they are in the same */
951 /* position as the operand otherwise chaos results */
952 /*-----------------------------------------------------------------*/
953 static void positionRegs (symbol *result, symbol *opsym, int lineno)
955 int count = min(result->nRegs,opsym->nRegs);
956 int i , j = 0, shared = 0;
958 /* if the result has been spilt then cannot share */
963 /* first make sure that they actually share */
964 for ( i = 0 ; i < count; i++ ) {
965 for (j = 0 ; j < count ; j++ ) {
966 if (result->regs[i] == opsym->regs[j] && i !=j) {
974 regs *tmp = result->regs[i];
975 result->regs[i] = result->regs[j];
976 result->regs[j] = tmp;
981 /*-----------------------------------------------------------------*/
982 /* serialRegAssign - serially allocate registers to the variables */
983 /*-----------------------------------------------------------------*/
984 static void serialRegAssign (eBBlock **ebbs, int count)
989 for (i = 0; i < count ; i++ ) {
993 if (ebbs[i]->noPath &&
994 (ebbs[i]->entryLabel != entryLabel &&
995 ebbs[i]->entryLabel != returnLabel ))
998 /* of all instructions do */
999 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
1001 /* if this is an ipop that means some live
1002 range will have to be assigned again */
1004 reassignLR (IC_LEFT(ic));
1006 /* if result is present && is a true symbol */
1007 if (IC_RESULT(ic) && ic->op != IFX &&
1008 IS_TRUE_SYMOP(IC_RESULT(ic)))
1009 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
1011 /* take away registers from live
1012 ranges that end at this instruction */
1013 deassignLRs (ic, ebbs[i]) ;
1015 /* some don't need registers */
1017 ic->op == JUMPTABLE ||
1021 (IC_RESULT(ic) &&POINTER_SET(ic)) )
1024 /* now we need to allocate registers
1025 only for the result */
1026 if (IC_RESULT(ic)) {
1027 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
1033 /* if it does not need or is spilt
1034 or is already assigned to registers
1035 or will not live beyond this instructions */
1038 bitVectBitValue(_G.regAssigned,sym->key) ||
1039 sym->liveTo <= ic->seq)
1042 /* if some liverange has been spilt at the block level
1043 and this one live beyond this block then spil this
1045 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1049 /* if trying to allocate this will cause
1050 a spill and there is nothing to spill
1051 or this one is rematerializable then
1053 willCS = willCauseSpill(sym->nRegs,sym->regType);
1054 spillable = computeSpillable(ic);
1056 (willCS && bitVectIsZero(spillable) ) ) {
1063 /* if it has a spillocation & is used less than
1064 all other live ranges then spill this */
1065 if ( willCS && sym->usl.spillLoc ) {
1068 leastUsedLR(liveRangesWith (spillable ,
1073 leastUsed->used > sym->used) {
1079 /* if we need ptr regs for the right side
1081 if (POINTER_GET(ic) && getSize(OP_SYMBOL(IC_LEFT(ic))->type)
1087 /* else we assign registers to it */
1088 _G.regAssigned = bitVectSetBit(_G.regAssigned,sym->key);
1090 for (j = 0 ; j < sym->nRegs ;j++ ) {
1091 if (sym->regType == REG_PTR)
1092 sym->regs[j] = getRegPtr(ic,ebbs[i],sym);
1094 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
1096 /* if the allocation falied which means
1097 this was spilt then break */
1101 /* if it shares registers with operands make sure
1102 that they are in the same position */
1103 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
1104 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
1105 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1106 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
1107 /* do the same for the right operand */
1108 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
1109 OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
1110 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1111 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1123 /*-----------------------------------------------------------------*/
1124 /* rUmaskForOp :- returns register mask for an operand */
1125 /*-----------------------------------------------------------------*/
1126 static bitVect *rUmaskForOp (operand *op)
1132 /* only temporaries are assigned registers */
1136 sym = OP_SYMBOL(op);
1138 /* if spilt or no registers assigned to it
1140 if (sym->isspilt || !sym->nRegs)
1143 rumask = newBitVect(mcs51_nRegs);
1145 for (j = 0; j < sym->nRegs; j++) {
1146 rumask = bitVectSetBit(rumask,
1147 sym->regs[j]->rIdx);
1153 /*-----------------------------------------------------------------*/
1154 /* regsUsedIniCode :- returns bit vector of registers used in iCode*/
1155 /*-----------------------------------------------------------------*/
1156 static bitVect *regsUsedIniCode (iCode *ic)
1158 bitVect *rmask = newBitVect(mcs51_nRegs);
1160 /* do the special cases first */
1161 if (ic->op == IFX ) {
1162 rmask = bitVectUnion(rmask,
1163 rUmaskForOp(IC_COND(ic)));
1167 /* for the jumptable */
1168 if (ic->op == JUMPTABLE) {
1169 rmask = bitVectUnion(rmask,
1170 rUmaskForOp(IC_JTCOND(ic)));
1175 /* of all other cases */
1177 rmask = bitVectUnion(rmask,
1178 rUmaskForOp(IC_LEFT(ic)));
1182 rmask = bitVectUnion(rmask,
1183 rUmaskForOp(IC_RIGHT(ic)));
1186 rmask = bitVectUnion(rmask,
1187 rUmaskForOp(IC_RESULT(ic)));
1193 /*-----------------------------------------------------------------*/
1194 /* createRegMask - for each instruction will determine the regsUsed*/
1195 /*-----------------------------------------------------------------*/
1196 static void createRegMask (eBBlock **ebbs, int count)
1200 /* for all blocks */
1201 for (i = 0; i < count ; i++ ) {
1204 if ( ebbs[i]->noPath &&
1205 ( ebbs[i]->entryLabel != entryLabel &&
1206 ebbs[i]->entryLabel != returnLabel ))
1209 /* for all instructions */
1210 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1214 if (SKIP_IC2(ic) || !ic->rlive)
1217 /* first mark the registers used in this
1219 ic->rUsed = regsUsedIniCode(ic);
1220 _G.funcrUsed = bitVectUnion(_G.funcrUsed,ic->rUsed);
1222 /* now create the register mask for those
1223 registers that are in use : this is a
1224 super set of ic->rUsed */
1225 ic->rMask = newBitVect(mcs51_nRegs+1);
1227 /* for all live Ranges alive at this point */
1228 for (j = 1; j < ic->rlive->size; j++ ) {
1232 /* if not alive then continue */
1233 if (!bitVectBitValue(ic->rlive,j))
1236 /* find the live range we are interested in */
1237 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1238 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1239 "createRegMask cannot find live range");
1243 /* if no register assigned to it */
1244 if (!sym->nRegs || sym->isspilt)
1247 /* for all the registers allocated to it */
1248 for (k = 0 ; k < sym->nRegs ;k++)
1251 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1257 /*-----------------------------------------------------------------*/
1258 /* rematStr - returns the rematerialized string for a remat var */
1259 /*-----------------------------------------------------------------*/
1260 static char *rematStr (symbol *sym)
1263 iCode *ic = sym->rematiCode;
1267 /* if plus or minus print the right hand side */
1268 if (ic->op == '+' || ic->op == '-') {
1269 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1272 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1276 /* we reached the end */
1277 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1284 /*-----------------------------------------------------------------*/
1285 /* regTypeNum - computes the type & number of registers required */
1286 /*-----------------------------------------------------------------*/
1287 static void regTypeNum ()
1293 /* for each live range do */
1294 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1295 sym = hTabNextItem(liveRanges,&k)) {
1297 /* if used zero times then no registers needed */
1298 if ((sym->liveTo - sym->liveFrom) == 0)
1302 /* if the live range is a temporary */
1305 /* if the type is marked as a conditional */
1306 if (sym->regType == REG_CND)
1309 /* if used in return only then we don't
1311 if (sym->ruonly || sym->accuse) {
1312 if (IS_AGGREGATE(sym->type) || sym->isptr)
1313 sym->type = aggrToPtr(sym->type,FALSE);
1317 /* if the symbol has only one definition &
1318 that definition is a get_pointer and the
1319 pointer we are getting is rematerializable and
1322 if (bitVectnBitsOn(sym->defs) == 1 &&
1323 (ic = hTabItemWithKey(iCodehTab,
1324 bitVectFirstBit(sym->defs))) &&
1326 !IS_BITVAR(sym->etype)) {
1329 /* if remat in data space */
1330 if (OP_SYMBOL(IC_LEFT(ic))->remat &&
1331 DCL_TYPE(aggrToPtr(sym->type,FALSE)) == POINTER) {
1333 /* create a psuedo symbol & force a spil */
1334 symbol *psym = newSymbol(rematStr(OP_SYMBOL(IC_LEFT(ic))),1);
1335 psym->type = sym->type;
1336 psym->etype = sym->etype;
1337 strcpy(psym->rname,psym->name);
1339 sym->usl.spillLoc = psym;
1343 /* if in data space or idata space then try to
1344 allocate pointer register */
1348 /* if not then we require registers */
1349 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1350 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1351 getSize(sym->type));
1353 if (sym->nRegs > 4) {
1354 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1355 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1358 /* determine the type of register required */
1359 if (sym->nRegs == 1 &&
1360 IS_PTR(sym->type) &&
1362 sym->regType = REG_PTR ;
1364 sym->regType = REG_GPR ;
1367 /* for the first run we don't provide */
1368 /* registers for true symbols we will */
1369 /* see how things go */
1375 /*-----------------------------------------------------------------*/
1376 /* freeAllRegs - mark all registers as free */
1377 /*-----------------------------------------------------------------*/
1378 static void freeAllRegs()
1382 for (i=0;i< mcs51_nRegs;i++ )
1383 regs8051[i].isFree = 1;
1386 /*-----------------------------------------------------------------*/
1387 /* deallocStackSpil - this will set the stack pointer back */
1388 /*-----------------------------------------------------------------*/
1389 static DEFSETFUNC(deallocStackSpil)
1397 /*-----------------------------------------------------------------*/
1398 /* farSpacePackable - returns the packable icode for far variables */
1399 /*-----------------------------------------------------------------*/
1400 static iCode *farSpacePackable (iCode *ic)
1404 /* go thru till we find a definition for the
1405 symbol on the right */
1406 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1408 /* if the definition is a call then no */
1409 if ((dic->op == CALL || dic->op == PCALL) &&
1410 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1414 /* if shift by unknown amount then not */
1415 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1416 IC_RESULT(dic)->key == IC_RIGHT(ic)->key)
1419 /* if pointer get and size > 1 */
1420 if (POINTER_GET(dic) &&
1421 getSize(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)) > 1)
1424 if (POINTER_SET(dic) &&
1425 getSize(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)) > 1)
1428 /* if any three is a true symbol in far space */
1429 if (IC_RESULT(dic) &&
1430 IS_TRUE_SYMOP(IC_RESULT(dic)) &&
1431 isOperandInFarSpace(IC_RESULT(dic)))
1434 if (IC_RIGHT(dic) &&
1435 IS_TRUE_SYMOP(IC_RIGHT(dic)) &&
1436 isOperandInFarSpace(IC_RIGHT(dic)) &&
1437 !isOperandEqual(IC_RIGHT(dic),IC_RESULT(ic)))
1441 IS_TRUE_SYMOP(IC_LEFT(dic)) &&
1442 isOperandInFarSpace(IC_LEFT(dic)) &&
1443 !isOperandEqual(IC_LEFT(dic),IC_RESULT(ic)))
1446 if (isOperandEqual(IC_RIGHT(ic),IC_RESULT(dic))) {
1447 if ( (dic->op == LEFT_OP ||
1448 dic->op == RIGHT_OP ||
1450 IS_OP_LITERAL(IC_RIGHT(dic)))
1460 /*-----------------------------------------------------------------*/
1461 /* packRegsForAssign - register reduction for assignment */
1462 /*-----------------------------------------------------------------*/
1463 static int packRegsForAssign (iCode *ic,eBBlock *ebp)
1467 if (!IS_ITEMP(IC_RIGHT(ic)) ||
1468 OP_SYMBOL(IC_RIGHT(ic))->isind ||
1469 OP_LIVETO(IC_RIGHT(ic)) > ic->seq) {
1473 /* if the true symbol is defined in far space or on stack
1474 then we should not since this will increase register pressure */
1475 if (isOperandInFarSpace(IC_RESULT(ic))) {
1476 if ((dic = farSpacePackable(ic)))
1482 /* find the definition of iTempNN scanning backwards if we find a
1483 a use of the true symbol in before we find the definition then
1485 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1487 /* if there is a function call and this is
1488 a parameter & not my parameter then don't pack it */
1489 if ( (dic->op == CALL || dic->op == PCALL) &&
1490 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1491 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1499 if (IS_TRUE_SYMOP(IC_RESULT(dic)) &&
1500 IS_OP_VOLATILE(IC_RESULT(dic))) {
1505 if (IS_SYMOP(IC_RESULT(dic)) &&
1506 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1507 if (POINTER_SET(dic))
1513 if (IS_SYMOP(IC_RIGHT(dic)) &&
1514 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1515 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1520 if (IS_SYMOP(IC_LEFT(dic)) &&
1521 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1522 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1527 if (POINTER_SET(dic) &&
1528 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1535 return 0 ; /* did not find */
1537 /* if the result is on stack or iaccess then it must be
1538 the same atleast one of the operands */
1539 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1540 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1542 /* the operation has only one symbol
1543 operator then we can pack */
1544 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1545 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1548 if (!((IC_LEFT(dic) &&
1549 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1551 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1555 /* found the definition */
1556 /* replace the result with the result of */
1557 /* this assignment and remove this assignment */
1558 IC_RESULT(dic) = IC_RESULT(ic) ;
1560 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1561 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1563 /* delete from liverange table also
1564 delete from all the points inbetween and the new
1566 for ( sic = dic; sic != ic ; sic = sic->next ) {
1567 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1568 if (IS_ITEMP(IC_RESULT(dic)))
1569 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1572 remiCodeFromeBBlock(ebp,ic);
1573 hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
1578 /*-----------------------------------------------------------------*/
1579 /* findAssignToSym : scanning backwards looks for first assig found*/
1580 /*-----------------------------------------------------------------*/
1581 static iCode *findAssignToSym (operand *op,iCode *ic)
1585 for (dic = ic->prev ; dic ; dic = dic->prev) {
1587 /* if definition by assignment */
1588 if (dic->op == '=' &&
1589 !POINTER_SET(dic) &&
1590 IC_RESULT(dic)->key == op->key
1591 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1594 /* we are interested only if defined in far space */
1595 /* or in stack space in case of + & - */
1597 /* if assigned to a non-symbol then return
1599 if (!IS_SYMOP(IC_RIGHT(dic)))
1602 /* if the symbol is in far space then
1604 if (isOperandInFarSpace(IC_RIGHT(dic)))
1607 /* for + & - operations make sure that
1608 if it is on the stack it is the same
1609 as one of the three operands */
1610 if ((ic->op == '+' || ic->op == '-') &&
1611 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1613 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1614 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1615 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1623 /* if we find an usage then we cannot delete it */
1624 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1627 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1630 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1634 /* now make sure that the right side of dic
1635 is not defined between ic & dic */
1637 iCode *sic = dic->next ;
1639 for (; sic != ic ; sic = sic->next)
1640 if (IC_RESULT(sic) &&
1641 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1650 /*-----------------------------------------------------------------*/
1651 /* packRegsForSupport :- reduce some registers for support calls */
1652 /*-----------------------------------------------------------------*/
1653 static int packRegsForSupport (iCode *ic, eBBlock *ebp)
1656 /* for the left & right operand :- look to see if the
1657 left was assigned a true symbol in far space in that
1658 case replace them */
1659 if (IS_ITEMP(IC_LEFT(ic)) &&
1660 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1661 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1667 /* found it we need to remove it from the
1669 for ( sic = dic; sic != ic ; sic = sic->next )
1670 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1672 IC_LEFT(ic)->operand.symOperand =
1673 IC_RIGHT(dic)->operand.symOperand;
1674 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1675 remiCodeFromeBBlock(ebp,dic);
1676 hTabDeleteItem (&iCodehTab,dic->key,dic,DELETE_ITEM,NULL);
1680 /* do the same for the right operand */
1683 IS_ITEMP(IC_RIGHT(ic)) &&
1684 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1685 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1691 /* if this is a subtraction & the result
1692 is a true symbol in far space then don't pack */
1693 if (ic->op == '-' && IS_TRUE_SYMOP(IC_RESULT(dic))) {
1694 link *etype =getSpec(operandType(IC_RESULT(dic)));
1695 if (IN_FARSPACE(SPEC_OCLS(etype)))
1698 /* found it we need to remove it from the
1700 for ( sic = dic; sic != ic ; sic = sic->next )
1701 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1703 IC_RIGHT(ic)->operand.symOperand =
1704 IC_RIGHT(dic)->operand.symOperand;
1705 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1707 remiCodeFromeBBlock(ebp,dic);
1708 hTabDeleteItem (&iCodehTab,dic->key,dic,DELETE_ITEM,NULL);
1715 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1718 /*-----------------------------------------------------------------*/
1719 /* packRegsForOneuse : - will reduce some registers for single Use */
1720 /*-----------------------------------------------------------------*/
1721 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1726 /* if returning a literal then do nothing */
1730 /* only upto 2 bytes since we cannot predict
1731 the usage of b, & acc */
1732 if (getSize(operandType(op)) > (fReturnSize - 2) &&
1737 /* this routine will mark the a symbol as used in one
1738 instruction use only && if the defintion is local
1739 (ie. within the basic block) && has only one definition &&
1740 that definiion is either a return value from a
1741 function or does not contain any variables in
1743 uses = bitVectCopy(OP_USES(op));
1744 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1745 if (!bitVectIsZero(uses)) /* has other uses */
1748 /* if it has only one defintion */
1749 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1750 return NULL ; /* has more than one definition */
1752 /* get the that definition */
1754 hTabItemWithKey(iCodehTab,
1755 bitVectFirstBit(OP_DEFS(op)))))
1758 /* found the definition now check if it is local */
1759 if (dic->seq < ebp->fSeq ||
1760 dic->seq > ebp->lSeq)
1761 return NULL ; /* non-local */
1763 /* now check if it is the return from
1765 if (dic->op == CALL || dic->op == PCALL ) {
1766 if (ic->op != SEND && ic->op != RETURN) {
1767 OP_SYMBOL(op)->ruonly = 1;
1774 /* otherwise check that the definition does
1775 not contain any symbols in far space */
1776 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1777 isOperandInFarSpace(IC_RIGHT(dic)) ||
1778 IS_OP_RUONLY(IC_LEFT(ic)) ||
1779 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1783 /* if pointer set then make sure the pointer
1785 if (POINTER_SET(dic) &&
1786 !IS_DATA_PTR(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)))
1789 if (POINTER_GET(dic) &&
1790 !IS_DATA_PTR(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)))
1795 /* also make sure the intervenening instructions
1796 don't have any thing in far space */
1797 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1799 /* if there is an intervening function call then no */
1800 if (dic->op == CALL || dic->op == PCALL)
1802 /* if pointer set then make sure the pointer
1804 if (POINTER_SET(dic) &&
1805 !IS_DATA_PTR(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)))
1808 if (POINTER_GET(dic) &&
1809 !IS_DATA_PTR(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)))
1812 /* if address of & the result is remat the okay */
1813 if (dic->op == ADDRESS_OF &&
1814 OP_SYMBOL(IC_RESULT(dic))->remat)
1817 /* if operand has size of three or more & this
1818 operation is a '*','/' or '%' then 'b' may
1820 if (( dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1821 getSize(operandType(op)) >= 3)
1824 /* if left or right or result is in far space */
1825 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1826 isOperandInFarSpace(IC_RIGHT(dic)) ||
1827 isOperandInFarSpace(IC_RESULT(dic)) ||
1828 IS_OP_RUONLY(IC_LEFT(dic)) ||
1829 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1830 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1835 OP_SYMBOL(op)->ruonly = 1;
1840 /*-----------------------------------------------------------------*/
1841 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1842 /*-----------------------------------------------------------------*/
1843 static bool isBitwiseOptimizable (iCode *ic)
1845 link *ltype = getSpec(operandType(IC_LEFT(ic)));
1846 link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1848 /* bitwise operations are considered optimizable
1849 under the following conditions (Jean-Louis VERN)
1861 if ( IS_LITERAL(rtype) ||
1862 (IS_BITVAR(ltype) && IN_BITSPACE(SPEC_OCLS(ltype))))
1868 /*-----------------------------------------------------------------*/
1869 /* packRegsForAccUse - pack registers for acc use */
1870 /*-----------------------------------------------------------------*/
1871 static void packRegsForAccUse (iCode *ic)
1875 /* if + or - then it has to be one byte result */
1876 if ((ic->op == '+' || ic->op == '-')
1877 && getSize(operandType(IC_RESULT(ic))) > 1)
1880 /* if shift operation make sure right side is not a literal */
1881 if (ic->op == RIGHT_OP &&
1882 ( isOperandLiteral(IC_RIGHT(ic)) ||
1883 getSize(operandType(IC_RESULT(ic))) > 1))
1886 if (ic->op == LEFT_OP &&
1887 ( isOperandLiteral(IC_RIGHT(ic)) ||
1888 getSize(operandType(IC_RESULT(ic))) > 1))
1891 if (IS_BITWISE_OP(ic) &&
1892 getSize(operandType(IC_RESULT(ic))) > 1)
1896 /* has only one definition */
1897 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1900 /* has only one use */
1901 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1904 /* and the usage immediately follows this iCode */
1905 if (!(uic = hTabItemWithKey(iCodehTab,
1906 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1909 if (ic->next != uic)
1912 /* if it is a conditional branch then we definitely can */
1913 if (uic->op == IFX )
1916 if ( uic->op == JUMPTABLE )
1919 /* if the usage is not is an assignment
1920 or an arithmetic / bitwise / shift operation then not */
1921 if (POINTER_SET(uic) &&
1922 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1925 if (uic->op != '=' &&
1926 !IS_ARITHMETIC_OP(uic) &&
1927 !IS_BITWISE_OP(uic) &&
1928 uic->op != LEFT_OP &&
1929 uic->op != RIGHT_OP )
1932 /* if used in ^ operation then make sure right is not a
1934 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1937 /* if shift operation make sure right side is not a literal */
1938 if (uic->op == RIGHT_OP &&
1939 ( isOperandLiteral(IC_RIGHT(uic)) ||
1940 getSize(operandType(IC_RESULT(uic))) > 1))
1943 if (uic->op == LEFT_OP &&
1944 ( isOperandLiteral(IC_RIGHT(uic)) ||
1945 getSize(operandType(IC_RESULT(uic))) > 1))
1948 /* make sure that the result of this icode is not on the
1949 stack, since acc is used to compute stack offset */
1950 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1951 OP_SYMBOL(IC_RESULT(uic))->onStack)
1954 /* if either one of them in far space then we cannot */
1955 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1956 isOperandInFarSpace(IC_LEFT(uic))) ||
1957 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1958 isOperandInFarSpace(IC_RIGHT(uic))))
1961 /* if the usage has only one operand then we can */
1962 if (IC_LEFT(uic) == NULL ||
1963 IC_RIGHT(uic) == NULL)
1966 /* make sure this is on the left side if not
1967 a '+' since '+' is commutative */
1968 if (ic->op != '+' &&
1969 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1972 /* if one of them is a literal then we can */
1973 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1974 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1975 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1979 /* if the other one is not on stack then we can */
1980 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1981 (IS_ITEMP(IC_RIGHT(uic)) ||
1982 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1983 !OP_SYMBOL(IC_RIGHT(uic))->onStack)))
1986 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1987 (IS_ITEMP(IC_LEFT(uic)) ||
1988 (IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1989 !OP_SYMBOL(IC_LEFT(uic))->onStack)))
1995 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
2000 /*-----------------------------------------------------------------*/
2001 /* packForPush - hueristics to reduce iCode for pushing */
2002 /*-----------------------------------------------------------------*/
2003 static void packForPush(iCode *ic, eBBlock *ebp)
2007 if (ic->op != IPUSH || !IS_ITEMP(IC_LEFT(ic)))
2010 /* must have only definition & one usage */
2011 if (bitVectnBitsOn(OP_DEFS(IC_LEFT(ic))) != 1 ||
2012 bitVectnBitsOn(OP_USES(IC_LEFT(ic))) != 1 )
2015 /* find the definition */
2016 if (!(dic = hTabItemWithKey(iCodehTab,
2017 bitVectFirstBit(OP_DEFS(IC_LEFT(ic))))))
2020 if (dic->op != '=' || POINTER_SET(dic))
2023 /* we now we know that it has one & only one def & use
2024 and the that the definition is an assignment */
2025 IC_LEFT(ic) = IC_RIGHT(dic);
2027 remiCodeFromeBBlock(ebp,dic);
2028 hTabDeleteItem (&iCodehTab,dic->key,dic,DELETE_ITEM,NULL);
2031 /*-----------------------------------------------------------------*/
2032 /* packRegisters - does some transformations to reduce register */
2034 /*-----------------------------------------------------------------*/
2035 static void packRegisters (eBBlock *ebp)
2044 /* look for assignments of the form */
2045 /* iTempNN = TRueSym (someoperation) SomeOperand */
2047 /* TrueSym := iTempNN:1 */
2048 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2051 /* find assignment of the form TrueSym := iTempNN:1 */
2052 if (ic->op == '=' && !POINTER_SET(ic))
2053 change += packRegsForAssign(ic,ebp);
2060 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2062 /* if this is an itemp & result of a address of a true sym
2063 then mark this as rematerialisable */
2064 if (ic->op == ADDRESS_OF &&
2065 IS_ITEMP(IC_RESULT(ic)) &&
2066 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
2067 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2068 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
2070 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2071 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2072 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2076 /* if straight assignment then carry remat flag if
2077 this is the only definition */
2078 if (ic->op == '=' &&
2080 IS_SYMOP(IC_RIGHT(ic)) &&
2081 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2082 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
2084 OP_SYMBOL(IC_RESULT(ic))->remat =
2085 OP_SYMBOL(IC_RIGHT(ic))->remat;
2086 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
2087 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
2090 /* if this is a +/- operation with a rematerizable
2091 then mark this as rematerializable as well only
2092 if the literal value is within the range -255 and + 255
2093 the assembler cannot handle it other wise */
2094 if ((ic->op == '+' || ic->op == '-') &&
2096 (IS_SYMOP(IC_LEFT(ic)) &&
2097 IS_ITEMP(IC_RESULT(ic)) &&
2098 OP_SYMBOL(IC_LEFT(ic))->remat &&
2099 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2100 IS_OP_LITERAL(IC_RIGHT(ic))) ) {
2102 int i = operandLitValue(IC_RIGHT(ic));
2103 if ( i < 255 && i > -255) {
2104 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2105 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2106 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2110 /* mark the pointer usages */
2111 if (POINTER_SET(ic))
2112 OP_SYMBOL(IC_RESULT(ic))->uptr = 1;
2114 if (POINTER_GET(ic))
2115 OP_SYMBOL(IC_LEFT(ic))->uptr = 1;
2117 if (!SKIP_IC2(ic)) {
2118 /* if we are using a symbol on the stack
2119 then we should say mcs51_ptrRegReq */
2120 if (ic->op == IFX && IS_SYMOP(IC_COND(ic)))
2121 mcs51_ptrRegReq += ((OP_SYMBOL(IC_COND(ic))->onStack ||
2122 OP_SYMBOL(IC_COND(ic))->iaccess) ? 1 : 0);
2124 if (ic->op == JUMPTABLE && IS_SYMOP(IC_JTCOND(ic)))
2125 mcs51_ptrRegReq += ((OP_SYMBOL(IC_JTCOND(ic))->onStack ||
2126 OP_SYMBOL(IC_JTCOND(ic))->iaccess) ? 1 : 0);
2128 if (IS_SYMOP(IC_LEFT(ic)))
2129 mcs51_ptrRegReq += ((OP_SYMBOL(IC_LEFT(ic))->onStack ||
2130 OP_SYMBOL(IC_LEFT(ic))->iaccess) ? 1 : 0);
2131 if (IS_SYMOP(IC_RIGHT(ic)))
2132 mcs51_ptrRegReq += ((OP_SYMBOL(IC_RIGHT(ic))->onStack ||
2133 OP_SYMBOL(IC_RIGHT(ic))->iaccess) ? 1 : 0);
2134 if (IS_SYMOP(IC_RESULT(ic)))
2135 mcs51_ptrRegReq += ((OP_SYMBOL(IC_RESULT(ic))->onStack ||
2136 OP_SYMBOL(IC_RESULT(ic))->iaccess) ? 1 : 0);
2140 /* if the condition of an if instruction
2141 is defined in the previous instruction then
2142 mark the itemp as a conditional */
2143 if ((IS_CONDITIONAL(ic) ||
2144 ( ( ic->op == BITWISEAND ||
2147 isBitwiseOptimizable(ic))) &&
2148 ic->next && ic->next->op == IFX &&
2149 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
2150 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
2152 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
2156 /* reduce for support function calls */
2157 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
2158 packRegsForSupport (ic,ebp);
2160 /* some cases the redundant moves can
2161 can be eliminated for return statements */
2162 if ((ic->op == RETURN || ic->op == SEND) &&
2163 !isOperandInFarSpace(IC_LEFT(ic)) &&
2165 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2167 /* if pointer set & left has a size more than
2168 one and right is not in far space */
2169 if (POINTER_SET(ic) &&
2170 !isOperandInFarSpace(IC_RIGHT(ic)) &&
2171 !OP_SYMBOL(IC_RESULT(ic))->remat &&
2172 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
2173 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 )
2175 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
2177 /* if pointer get */
2178 if (POINTER_GET(ic) &&
2179 !isOperandInFarSpace(IC_RESULT(ic))&&
2180 !OP_SYMBOL(IC_LEFT(ic))->remat &&
2181 !IS_OP_RUONLY(IC_RESULT(ic)) &&
2182 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 )
2184 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2187 /* if this is cast for intergral promotion then
2188 check if only use of the definition of the
2189 operand being casted/ if yes then replace
2190 the result of that arithmetic operation with
2191 this result and get rid of the cast */
2192 if (ic->op == CAST) {
2193 link *fromType = operandType(IC_RIGHT(ic));
2194 link *toType = operandType(IC_LEFT(ic));
2196 if (IS_INTEGRAL(fromType) && IS_INTEGRAL(toType) &&
2197 getSize(fromType) != getSize(toType) ) {
2199 iCode *dic = packRegsForOneuse(ic,IC_RIGHT(ic),ebp);
2201 if (IS_ARITHMETIC_OP(dic)) {
2202 IC_RESULT(dic) = IC_RESULT(ic);
2203 remiCodeFromeBBlock(ebp,ic);
2204 hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
2207 OP_SYMBOL(IC_RIGHT(ic))->ruonly = 0;
2211 /* if the type from and type to are the same
2212 then if this is the only use then packit */
2213 if (checkType(operandType(IC_RIGHT(ic)),
2214 operandType(IC_LEFT(ic))) == 1) {
2215 iCode *dic = packRegsForOneuse (ic,IC_RIGHT(ic),ebp);
2217 IC_RESULT(dic) = IC_RESULT(ic);
2218 remiCodeFromeBBlock(ebp,ic);
2219 hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
2227 iTempNN := (some variable in farspace) V1
2232 if (ic->op == IPUSH ) {
2233 packForPush(ic,ebp);
2237 /* pack registers for accumulator use, when the
2238 result of an arithmetic or bit wise operation
2239 has only one use, that use is immediately following
2240 the defintion and the using iCode has only one
2241 operand or has two operands but one is literal &
2242 the result of that operation is not on stack then
2243 we can leave the result of this operation in acc:b
2245 if ((IS_ARITHMETIC_OP(ic)
2247 || IS_BITWISE_OP(ic)
2249 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2252 IS_ITEMP(IC_RESULT(ic)) &&
2253 getSize(operandType(IC_RESULT(ic))) <= 2)
2255 packRegsForAccUse (ic);
2260 /*-----------------------------------------------------------------*/
2261 /* assignRegisters - assigns registers to each live range as need */
2262 /*-----------------------------------------------------------------*/
2263 void mcs51_assignRegisters (eBBlock **ebbs, int count)
2268 setToNull((void *)&_G.funcrUsed);
2269 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2270 /* if not register extentions then reduce number
2272 if (options.regExtend)
2277 /* change assignments this will remove some
2278 live ranges reducing some register pressure */
2279 for (i = 0 ; i < count ;i++ )
2280 packRegisters (ebbs[i]);
2282 if (options.dump_pack)
2283 dumpEbbsToFileExt(".dumppack",ebbs,count);
2285 /* first determine for each live range the number of
2286 registers & the type of registers required for each */
2289 /* and serially allocate registers */
2290 serialRegAssign(ebbs,count);
2292 /* if stack was extended then tell the user */
2293 if (_G.stackExtend) {
2294 /* werror(W_TOOMANY_SPILS,"stack", */
2295 /* _G.stackExtend,currFunc->name,""); */
2296 _G.stackExtend = 0 ;
2299 if (_G.dataExtend) {
2300 /* werror(W_TOOMANY_SPILS,"data space", */
2301 /* _G.dataExtend,currFunc->name,""); */
2305 /* after that create the register mask
2306 for each of the instruction */
2307 createRegMask (ebbs,count);
2309 /* redo that offsets for stacked automatic variables */
2310 redoStackOffsets ();
2312 if (options.dump_rassgn)
2313 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
2315 /* now get back the chain */
2316 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
2321 /* free up any _G.stackSpil locations allocated */
2322 applyToSet(_G.stackSpil,deallocStackSpil);
2324 setToNull((void **)&_G.stackSpil);
2325 setToNull((void **)&_G.spiltSet);
2326 /* mark all registers as free */