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 -------------------------------------------------------------------------*/
27 #include "SDCCglobl.h"
31 #include "SDCChasht.h"
34 #include "SDCCicode.h"
35 #include "SDCClabel.h"
36 #include "SDCCBBlock.h"
39 #include "SDCCcflow.h"
40 #include "SDCCdflow.h"
41 #include "SDCClrange.h"
42 #include "SDCCralloc.h"
44 /*-----------------------------------------------------------------*/
45 /* At this point we start getting processor specific although */
46 /* some routines are non-processor specific & can be reused when */
47 /* targetting other processors. The decision for this will have */
48 /* to be made on a routine by routine basis */
49 /* routines used to pack registers are most definitely not reusable*/
50 /* since the pack the registers depending strictly on the MCU */
51 /*-----------------------------------------------------------------*/
53 extern void gen51Code(iCode *);
62 bitVect *funcrUsed; /* registers used in a function */
67 /* Shared with gen.c */
68 int mcs51_ptrRegReq; /* one byte pointer register required */
74 { REG_GPR ,R2_IDX , REG_GPR , "r2", "ar2", "0", 2, 1 },
75 { REG_GPR ,R3_IDX , REG_GPR , "r3", "ar3", "0", 3, 1 },
76 { REG_GPR ,R4_IDX , REG_GPR , "r4", "ar4", "0", 4, 1 },
77 { REG_GPR ,R5_IDX , REG_GPR , "r5", "ar5", "0", 5, 1 },
78 { REG_GPR ,R6_IDX , REG_GPR , "r6", "ar6", "0", 6, 1 },
79 { REG_GPR ,R7_IDX , REG_GPR , "r7", "ar7", "0", 7, 1 },
80 { REG_PTR ,R0_IDX , REG_PTR , "r0" , "ar0", "0", 0, 1 },
81 { REG_PTR ,R1_IDX , REG_PTR , "r1" , "ar1", "0", 1, 1 },
82 { REG_GPR ,X8_IDX , REG_GPR , "x8", "x8" , "xreg", 0, 1 },
83 { REG_GPR ,X9_IDX , REG_GPR , "x9", "x9" , "xreg", 1, 1 },
84 { REG_GPR ,X10_IDX,REG_GPR , "x10", "x10", "xreg", 2, 1 },
85 { REG_GPR ,X11_IDX,REG_GPR , "x11", "x11", "xreg", 3, 1 },
86 { REG_GPR ,X12_IDX,REG_GPR , "x12", "x12", "xreg", 4, 1 },
87 { REG_CND ,CND_IDX,REG_CND , "C" , "C" , "xreg", 0, 1 },
90 void spillThis (symbol *);
92 /*-----------------------------------------------------------------*/
93 /* allocReg - allocates register of given type */
94 /*-----------------------------------------------------------------*/
95 regs *allocReg (short type)
99 for ( i = 0 ; i < mcs51_nRegs ; i++ ) {
101 /* if type is given as 0 then any
102 free register will do */
104 regs8051[i].isFree ) {
105 regs8051[i].isFree = 0;
108 bitVectSetBit(currFunc->regsUsed,i);
111 /* other wise look for specific type
113 if (regs8051[i].isFree &&
114 regs8051[i].type == type) {
115 regs8051[i].isFree = 0;
118 bitVectSetBit(currFunc->regsUsed,i);
125 /*-----------------------------------------------------------------*/
126 /* regWithIdx - returns pointer to register wit index number */
127 /*-----------------------------------------------------------------*/
128 regs *regWithIdx (int idx)
132 for (i=0;i < mcs51_nRegs;i++)
133 if (regs8051[i].rIdx == idx)
136 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
137 "regWithIdx not found");
141 /*-----------------------------------------------------------------*/
142 /* freeReg - frees a register */
143 /*-----------------------------------------------------------------*/
144 void freeReg (regs *reg)
150 /*-----------------------------------------------------------------*/
151 /* nFreeRegs - returns number of free registers */
152 /*-----------------------------------------------------------------*/
153 int nFreeRegs (int type)
158 for (i = 0 ; i < mcs51_nRegs; i++ )
159 if (regs8051[i].isFree && regs8051[i].type == type)
164 /*-----------------------------------------------------------------*/
165 /* nfreeRegsType - free registers with type */
166 /*-----------------------------------------------------------------*/
167 int nfreeRegsType (int type)
170 if (type == REG_PTR) {
171 if ((nfr = nFreeRegs(type)) == 0)
172 return nFreeRegs(REG_GPR);
175 return nFreeRegs(type);
179 /*-----------------------------------------------------------------*/
180 /* allDefsOutOfRange - all definitions are out of a range */
181 /*-----------------------------------------------------------------*/
182 bool allDefsOutOfRange (bitVect *defs,int fseq, int toseq)
189 for ( i = 0 ;i < defs->size ; i++ ) {
192 if (bitVectBitValue(defs,i) &&
193 (ic = hTabItemWithKey(iCodehTab,i)) &&
194 ( ic->seq >= fseq && ic->seq <= toseq))
203 /*-----------------------------------------------------------------*/
204 /* computeSpillable - given a point find the spillable live ranges */
205 /*-----------------------------------------------------------------*/
206 bitVect *computeSpillable (iCode *ic)
210 /* spillable live ranges are those that are live at this
211 point . the following categories need to be subtracted
213 a) - those that are already spilt
214 b) - if being used by this one
215 c) - defined by this one */
217 spillable = bitVectCopy(ic->rlive);
219 bitVectCplAnd(spillable,_G.spiltSet); /* those already spilt */
221 bitVectCplAnd(spillable,ic->uses); /* used in this one */
222 bitVectUnSetBit(spillable,ic->defKey);
223 spillable = bitVectIntersect(spillable,_G.regAssigned);
228 /*-----------------------------------------------------------------*/
229 /* noSpilLoc - return true if a variable has no spil location */
230 /*-----------------------------------------------------------------*/
231 int noSpilLoc (symbol *sym, eBBlock *ebp,iCode *ic)
233 return (sym->usl.spillLoc ? 0 : 1);
236 /*-----------------------------------------------------------------*/
237 /* hasSpilLoc - will return 1 if the symbol has spil location */
238 /*-----------------------------------------------------------------*/
239 int hasSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
241 return (sym->usl.spillLoc ? 1 : 0);
244 /*-----------------------------------------------------------------*/
245 /* directSpilLoc - will return 1 if the splilocation is in direct */
246 /*-----------------------------------------------------------------*/
247 int directSpilLoc (symbol *sym, eBBlock *ebp, iCode *ic)
249 if ( sym->usl.spillLoc &&
250 (IN_DIRSPACE(SPEC_OCLS(sym->usl.spillLoc->etype))))
256 /*-----------------------------------------------------------------*/
257 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location*/
258 /* but is not used as a pointer */
259 /*-----------------------------------------------------------------*/
260 int hasSpilLocnoUptr (symbol *sym, eBBlock *ebp, iCode *ic)
262 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
265 /*-----------------------------------------------------------------*/
266 /* rematable - will return 1 if the remat flag is set */
267 /*-----------------------------------------------------------------*/
268 int rematable (symbol *sym, eBBlock *ebp, iCode *ic)
273 /*-----------------------------------------------------------------*/
274 /* notUsedInBlock - not used in this block */
275 /*-----------------------------------------------------------------*/
276 int notUsedInBlock (symbol *sym, eBBlock *ebp, iCode *ic)
278 return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs) &&
279 allDefsOutOfRange (sym->defs,ebp->fSeq,ebp->lSeq));
280 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
283 /*-----------------------------------------------------------------*/
284 /* notUsedInRemaining - not used or defined in remain of the block */
285 /*-----------------------------------------------------------------*/
286 int notUsedInRemaining (symbol *sym, eBBlock *ebp, iCode *ic)
288 return ((usedInRemaining (operandFromSymbol(sym),ic) ? 0 : 1) &&
289 allDefsOutOfRange (sym->defs,ic->seq,ebp->lSeq));
292 /*-----------------------------------------------------------------*/
293 /* allLRs - return true for all */
294 /*-----------------------------------------------------------------*/
295 int allLRs (symbol *sym, eBBlock *ebp, iCode *ic)
300 /*-----------------------------------------------------------------*/
301 /* liveRangesWith - applies function to a given set of live range */
302 /*-----------------------------------------------------------------*/
303 set *liveRangesWith (bitVect *lrs, int (func)(symbol *,eBBlock *, iCode *),
304 eBBlock *ebp, iCode *ic)
309 if (!lrs || !lrs->size)
312 for ( i = 1 ; i < lrs->size ; i++ ) {
314 if (!bitVectBitValue(lrs,i))
317 /* if we don't find it in the live range
318 hash table we are in serious trouble */
319 if (!(sym = hTabItemWithKey(liveRanges,i))) {
320 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
321 "liveRangesWith could not find liveRange");
325 if (func(sym,ebp,ic) && bitVectBitValue(_G.regAssigned,sym->key))
326 addSetHead(&rset,sym);
333 /*-----------------------------------------------------------------*/
334 /* leastUsedLR - given a set determines which is the least used */
335 /*-----------------------------------------------------------------*/
336 symbol *leastUsedLR (set *sset)
338 symbol *sym = NULL, *lsym = NULL ;
340 sym = lsym = setFirstItem(sset);
345 for (; lsym; lsym = setNextItem(sset)) {
347 /* if usage is the same then prefer
348 the spill the smaller of the two */
349 if ( lsym->used == sym->used )
350 if (getSize(lsym->type) < getSize(sym->type))
354 if (lsym->used < sym->used )
359 setToNull((void **)&sset);
364 /*-----------------------------------------------------------------*/
365 /* noOverLap - will iterate through the list looking for over lap */
366 /*-----------------------------------------------------------------*/
367 int noOverLap (set *itmpStack, symbol *fsym)
372 for (sym = setFirstItem(itmpStack); sym;
373 sym = setNextItem(itmpStack)) {
374 if (sym->liveTo > fsym->liveFrom )
382 /*-----------------------------------------------------------------*/
383 /* isFree - will return 1 if the a free spil location is found */
384 /*-----------------------------------------------------------------*/
388 V_ARG(symbol **,sloc);
389 V_ARG(symbol *,fsym);
391 /* if already found */
395 /* if it is free && and the itmp assigned to
396 this does not have any overlapping live ranges
397 with the one currently being assigned and
398 the size can be accomodated */
400 noOverLap(sym->usl.itmpStack,fsym) &&
401 getSize(sym->type) >= getSize(fsym->type)) {
409 /*-----------------------------------------------------------------*/
410 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
411 /*-----------------------------------------------------------------*/
412 void spillLRWithPtrReg (symbol *forSym)
418 if (!_G.regAssigned ||
419 bitVectIsZero(_G.regAssigned))
422 r0 = regWithIdx(R0_IDX);
423 r1 = regWithIdx(R1_IDX);
425 /* for all live ranges */
426 for (lrsym = hTabFirstItem(liveRanges,&k) ; lrsym ;
427 lrsym = hTabNextItem(liveRanges,&k) ) {
430 /* if no registers assigned to it or
432 /* if it does not overlap with this then
433 not need to spill it */
435 if (lrsym->isspilt || !lrsym->nRegs ||
436 (lrsym->liveTo < forSym->liveFrom))
439 /* go thru the registers : if it is either
440 r0 or r1 then spil it */
441 for (j = 0 ; j < lrsym->nRegs ; j++ )
442 if (lrsym->regs[j] == r0 ||
443 lrsym->regs[j] == r1 ) {
451 /*-----------------------------------------------------------------*/
452 /* createStackSpil - create a location on the stack to spil */
453 /*-----------------------------------------------------------------*/
454 symbol *createStackSpil (symbol *sym)
457 int useXstack, model, noOverlay;
460 /* first go try and find a free one that is already
461 existing on the stack */
462 if (applyToSet(_G.stackSpil,isFree,&sloc, sym)) {
463 /* found a free one : just update & return */
464 sym->usl.spillLoc = sloc;
467 addSetHead(&sloc->usl.itmpStack,sym);
471 /* could not then have to create one , this is the hard part
472 we need to allocate this on the stack : this is really a
473 hack!! but cannot think of anything better at this time */
475 sprintf(buffer,"sloc%d",_G.slocNum++);
476 sloc = newiTemp(buffer);
478 /* set the type to the spilling symbol */
479 sloc->type = copyLinkChain(sym->type);
480 sloc->etype = getSpec(sloc->type);
481 SPEC_SCLS(sloc->etype) = S_AUTO ;
482 SPEC_EXTR(sloc->etype) = 0;
484 /* we don't allow it to be allocated`
485 onto the external stack since : so we
486 temporarily turn it off ; we also
487 turn off memory model to prevent
488 the spil from going to the external storage
489 and turn off overlaying
492 useXstack = options.useXstack;
493 model = options.model;
494 noOverlay = options.noOverlay;
495 stackAuto = options.stackAuto;
496 options.noOverlay = 1;
497 options.model = options.useXstack = 0;
501 options.useXstack = useXstack;
502 options.model = model;
503 options.noOverlay = noOverlay;
504 options.stackAuto = stackAuto;
505 sloc->isref = 1; /* to prevent compiler warning */
507 /* if it is on the stack then update the stack */
508 if (IN_STACK(sloc->etype)) {
509 currFunc->stack += getSize(sloc->type);
510 _G.stackExtend += getSize(sloc->type);
512 _G.dataExtend += getSize(sloc->type);
514 /* add it to the _G.stackSpil set */
515 addSetHead(&_G.stackSpil,sloc);
516 sym->usl.spillLoc = sloc;
519 /* add it to the set of itempStack set
520 of the spill location */
521 addSetHead(&sloc->usl.itmpStack,sym);
525 /*-----------------------------------------------------------------*/
526 /* isSpiltOnStack - returns true if the spil location is on stack */
527 /*-----------------------------------------------------------------*/
528 bool isSpiltOnStack (symbol *sym)
538 /* if (sym->_G.stackSpil) */
541 if (!sym->usl.spillLoc)
544 etype = getSpec(sym->usl.spillLoc->type);
551 /*-----------------------------------------------------------------*/
552 /* spillThis - spils a specific operand */
553 /*-----------------------------------------------------------------*/
554 void spillThis (symbol *sym)
557 /* if this is rematerializable or has a spillLocation
558 we are okay, else we need to create a spillLocation
560 if (!(sym->remat || sym->usl.spillLoc))
561 createStackSpil (sym);
564 /* mark it has spilt & put it in the spilt set */
566 _G.spiltSet = bitVectSetBit(_G.spiltSet,sym->key);
568 bitVectUnSetBit(_G.regAssigned,sym->key);
570 for (i = 0 ; i < sym->nRegs ; i++)
573 freeReg(sym->regs[i]);
577 /* if spilt on stack then free up r0 & r1
578 if they could have been assigned to some
580 if (!mcs51_ptrRegReq && isSpiltOnStack(sym)) {
582 spillLRWithPtrReg(sym);
585 if (sym->usl.spillLoc && !sym->remat)
586 sym->usl.spillLoc->allocreq = 1;
590 /*-----------------------------------------------------------------*/
591 /* selectSpil - select a iTemp to spil : rather a simple procedure */
592 /*-----------------------------------------------------------------*/
593 symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
595 bitVect *lrcs= NULL ;
599 /* get the spillable live ranges */
600 lrcs = computeSpillable (ic);
602 /* get all live ranges that are rematerizable */
603 if ((selectS = liveRangesWith(lrcs,rematable,ebp,ic))) {
605 /* return the least used of these */
606 return leastUsedLR(selectS);
609 /* get live ranges with spillLocations in direct space */
610 if ((selectS = liveRangesWith(lrcs,directSpilLoc,ebp,ic))) {
611 sym = leastUsedLR(selectS);
612 strcpy(sym->rname,(sym->usl.spillLoc->rname[0] ?
613 sym->usl.spillLoc->rname :
614 sym->usl.spillLoc->name));
616 /* mark it as allocation required */
617 sym->usl.spillLoc->allocreq = 1;
621 /* if the symbol is local to the block then */
622 if (forSym->liveTo < ebp->lSeq ) {
624 /* check if there are any live ranges allocated
625 to registers that are not used in this block */
626 if (!_G.blockSpil && (selectS = liveRangesWith(lrcs,notUsedInBlock,ebp,ic))) {
627 sym = leastUsedLR(selectS);
628 /* if this is not rematerializable */
636 /* check if there are any live ranges that not
637 used in the remainder of the block */
638 if (!_G.blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
639 sym = leastUsedLR (selectS);
648 /* find live ranges with spillocation && not used as pointers */
649 if ((selectS = liveRangesWith(lrcs,hasSpilLocnoUptr,ebp,ic))) {
651 sym = leastUsedLR(selectS);
652 /* mark this as allocation required */
653 sym->usl.spillLoc->allocreq = 1;
657 /* find live ranges with spillocation */
658 if ((selectS = liveRangesWith(lrcs,hasSpilLoc,ebp,ic))) {
660 sym = leastUsedLR(selectS);
661 sym->usl.spillLoc->allocreq = 1;
665 /* couldn't find then we need to create a spil
666 location on the stack , for which one? the least
668 if ((selectS = liveRangesWith(lrcs,noSpilLoc,ebp,ic))) {
670 /* return a created spil location */
671 sym = createStackSpil(leastUsedLR(selectS));
672 sym->usl.spillLoc->allocreq = 1;
676 /* this is an extreme situation we will spill
677 this one : happens very rarely but it does happen */
678 spillThis ( forSym );
683 /*-----------------------------------------------------------------*/
684 /* spilSomething - spil some variable & mark registers as free */
685 /*-----------------------------------------------------------------*/
686 bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
691 /* get something we can spil */
692 ssym = selectSpil(ic,ebp,forSym);
694 /* mark it as spilt */
696 _G.spiltSet = bitVectSetBit(_G.spiltSet,ssym->key);
698 /* mark it as not register assigned &
699 take it away from the set */
700 bitVectUnSetBit(_G.regAssigned,ssym->key);
702 /* mark the registers as free */
703 for (i = 0 ; i < ssym->nRegs ;i++ )
705 freeReg(ssym->regs[i]);
707 /* if spilt on stack then free up r0 & r1
708 if they could have been assigned to as gprs */
709 if (!mcs51_ptrRegReq && isSpiltOnStack(ssym) ) {
711 spillLRWithPtrReg(ssym);
714 /* if this was a block level spil then insert push & pop
715 at the start & end of block respectively */
716 if (ssym->blockSpil) {
717 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
718 /* add push to the start of the block */
719 addiCodeToeBBlock(ebp,nic,( ebp->sch->op == LABEL ?
720 ebp->sch->next : ebp->sch));
721 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
722 /* add pop to the end of the block */
723 addiCodeToeBBlock(ebp,nic,NULL);
726 /* if spilt because not used in the remainder of the
727 block then add a push before this instruction and
728 a pop at the end of the block */
729 if (ssym->remainSpil) {
731 iCode *nic = newiCode(IPUSH,operandFromSymbol(ssym),NULL);
732 /* add push just before this instruction */
733 addiCodeToeBBlock(ebp,nic,ic);
735 nic = newiCode(IPOP,operandFromSymbol(ssym),NULL);
736 /* add pop to the end of the block */
737 addiCodeToeBBlock(ebp,nic,NULL);
746 /*-----------------------------------------------------------------*/
747 /* getRegPtr - will try for PTR if not a GPR type if not spil */
748 /*-----------------------------------------------------------------*/
749 regs *getRegPtr (iCode *ic, eBBlock *ebp, symbol *sym)
754 /* try for a ptr type */
755 if ((reg = allocReg(REG_PTR)))
758 /* try for gpr type */
759 if ((reg = allocReg(REG_GPR)))
762 /* we have to spil */
763 if (!spilSomething (ic,ebp,sym))
766 /* this looks like an infinite loop but
767 in really selectSpil will abort */
771 /*-----------------------------------------------------------------*/
772 /* getRegGpr - will try for GPR if not spil */
773 /*-----------------------------------------------------------------*/
774 regs *getRegGpr (iCode *ic, eBBlock *ebp,symbol *sym)
779 /* try for gpr type */
780 if ((reg = allocReg(REG_GPR)))
783 if (!mcs51_ptrRegReq)
784 if ((reg = allocReg(REG_PTR)))
787 /* we have to spil */
788 if (!spilSomething (ic,ebp,sym))
791 /* this looks like an infinite loop but
792 in really selectSpil will abort */
796 /*-----------------------------------------------------------------*/
797 /* symHasReg - symbol has a given register */
798 /*-----------------------------------------------------------------*/
799 static bool symHasReg(symbol *sym,regs *reg)
803 for ( i = 0 ; i < sym->nRegs ; i++)
804 if (sym->regs[i] == reg)
810 /*-----------------------------------------------------------------*/
811 /* deassignLRs - check the live to and if they have registers & are*/
812 /* not spilt then free up the registers */
813 /*-----------------------------------------------------------------*/
814 void deassignLRs (iCode *ic, eBBlock *ebp)
820 for (sym = hTabFirstItem(liveRanges,&k); sym;
821 sym = hTabNextItem(liveRanges,&k)) {
824 /* if it does not end here */
825 if (sym->liveTo > ic->seq )
828 /* if it was spilt on stack then we can
829 mark the stack spil location as free */
831 if (sym->stackSpil) {
832 sym->usl.spillLoc->isFree = 1;
838 if (!bitVectBitValue(_G.regAssigned,sym->key))
841 /* special case check if this is an IFX &
842 the privious one was a pop and the
843 previous one was not spilt then keep track
845 if (ic->op == IFX && ic->prev &&
846 ic->prev->op == IPOP &&
847 !ic->prev->parmPush &&
848 !OP_SYMBOL(IC_LEFT(ic->prev))->isspilt)
849 psym = OP_SYMBOL(IC_LEFT(ic->prev));
854 bitVectUnSetBit(_G.regAssigned,sym->key);
856 /* if the result of this one needs registers
857 and does not have it then assign it right
860 ! (SKIP_IC2(ic) || /* not a special icode */
861 ic->op == JUMPTABLE ||
867 (result = OP_SYMBOL(IC_RESULT(ic))) && /* has a result */
868 result->liveTo > ic->seq && /* and will live beyond this */
869 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
870 result->regType == sym->regType && /* same register types */
871 result->nRegs && /* which needs registers */
872 ! result->isspilt && /* and does not already have them */
874 ! bitVectBitValue(_G.regAssigned,result->key) &&
875 /* the number of free regs + number of regs in this LR
876 can accomodate the what result Needs */
877 ((nfreeRegsType(result->regType) +
878 sym->nRegs) >= result->nRegs)
881 for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++)
883 result->regs[i] = sym->regs[i] ;
885 result->regs[i] = getRegGpr (ic,ebp,result);
887 _G.regAssigned = bitVectSetBit(_G.regAssigned,result->key);
891 /* free the remaining */
892 for (; i < sym->nRegs ; i++) {
894 if (!symHasReg(psym,sym->regs[i]))
895 freeReg(sym->regs[i]);
897 freeReg(sym->regs[i]);
904 /*-----------------------------------------------------------------*/
905 /* reassignLR - reassign this to registers */
906 /*-----------------------------------------------------------------*/
907 void reassignLR (operand *op)
909 symbol *sym = OP_SYMBOL(op);
912 /* not spilt any more */
913 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
914 bitVectUnSetBit(_G.spiltSet,sym->key);
916 _G.regAssigned = bitVectSetBit(_G.regAssigned,sym->key);
920 for (i=0;i<sym->nRegs;i++)
921 sym->regs[i]->isFree = 0;
924 /*-----------------------------------------------------------------*/
925 /* willCauseSpill - determines if allocating will cause a spill */
926 /*-----------------------------------------------------------------*/
927 int willCauseSpill ( int nr, int rt)
929 /* first check if there are any avlb registers
930 of te type required */
932 /* special case for pointer type
933 if pointer type not avlb then
934 check for type gpr */
935 if (nFreeRegs(rt) >= nr)
937 if (nFreeRegs(REG_GPR) >= nr)
940 if (mcs51_ptrRegReq) {
941 if (nFreeRegs(rt) >= nr)
944 if (nFreeRegs(REG_PTR) +
945 nFreeRegs(REG_GPR) >= nr)
950 /* it will cause a spil */
954 /*-----------------------------------------------------------------*/
955 /* positionRegs - the allocator can allocate same registers to res-*/
956 /* ult and operand, if this happens make sure they are in the same */
957 /* position as the operand otherwise chaos results */
958 /*-----------------------------------------------------------------*/
959 static void positionRegs (symbol *result, symbol *opsym, int lineno)
961 int count = min(result->nRegs,opsym->nRegs);
962 int i , j = 0, shared = 0;
964 /* if the result has been spilt then cannot share */
969 /* first make sure that they actually share */
970 for ( i = 0 ; i < count; i++ ) {
971 for (j = 0 ; j < count ; j++ ) {
972 if (result->regs[i] == opsym->regs[j] && i !=j) {
980 regs *tmp = result->regs[i];
981 result->regs[i] = result->regs[j];
982 result->regs[j] = tmp;
987 /*-----------------------------------------------------------------*/
988 /* serialRegAssign - serially allocate registers to the variables */
989 /*-----------------------------------------------------------------*/
990 void serialRegAssign (eBBlock **ebbs, int count)
995 for (i = 0; i < count ; i++ ) {
999 if (ebbs[i]->noPath &&
1000 (ebbs[i]->entryLabel != entryLabel &&
1001 ebbs[i]->entryLabel != returnLabel ))
1004 /* of all instructions do */
1005 for (ic = ebbs[i]->sch ; ic ; ic = ic->next) {
1007 /* if this is an ipop that means some live
1008 range will have to be assigned again */
1010 reassignLR (IC_LEFT(ic));
1012 /* if result is present && is a true symbol */
1013 if (IC_RESULT(ic) && ic->op != IFX &&
1014 IS_TRUE_SYMOP(IC_RESULT(ic)))
1015 OP_SYMBOL(IC_RESULT(ic))->allocreq = 1;
1017 /* take away registers from live
1018 ranges that end at this instruction */
1019 deassignLRs (ic, ebbs[i]) ;
1021 /* some don't need registers */
1023 ic->op == JUMPTABLE ||
1027 (IC_RESULT(ic) &&POINTER_SET(ic)) )
1030 /* now we need to allocate registers
1031 only for the result */
1032 if (IC_RESULT(ic)) {
1033 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
1039 /* if it does not need or is spilt
1040 or is already assigned to registers
1041 or will not live beyond this instructions */
1044 bitVectBitValue(_G.regAssigned,sym->key) ||
1045 sym->liveTo <= ic->seq)
1048 /* if some liverange has been spilt at the block level
1049 and this one live beyond this block then spil this
1051 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1055 /* if trying to allocate this will cause
1056 a spill and there is nothing to spill
1057 or this one is rematerializable then
1059 willCS = willCauseSpill(sym->nRegs,sym->regType);
1060 spillable = computeSpillable(ic);
1062 (willCS && bitVectIsZero(spillable) ) ) {
1069 /* if it has a spillocation & is used less than
1070 all other live ranges then spill this */
1071 if ( willCS && sym->usl.spillLoc ) {
1074 leastUsedLR(liveRangesWith (spillable ,
1079 leastUsed->used > sym->used) {
1085 /* if we need ptr regs for the right side
1087 if (POINTER_GET(ic) && getSize(OP_SYMBOL(IC_LEFT(ic))->type) < 2) {
1091 /* else we assign registers to it */
1092 _G.regAssigned = bitVectSetBit(_G.regAssigned,sym->key);
1094 for (j = 0 ; j < sym->nRegs ;j++ ) {
1095 if (sym->regType == REG_PTR)
1096 sym->regs[j] = getRegPtr(ic,ebbs[i],sym);
1098 sym->regs[j] = getRegGpr(ic,ebbs[i],sym);
1100 /* if the allocation falied which means
1101 this was spilt then break */
1105 /* if it shares registers with operands make sure
1106 that they are in the same position */
1107 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
1108 OP_SYMBOL(IC_LEFT(ic))->nRegs && ic->op != '=')
1109 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1110 OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
1111 /* do the same for the right operand */
1112 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
1113 OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
1114 positionRegs(OP_SYMBOL(IC_RESULT(ic)),
1115 OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
1127 /*-----------------------------------------------------------------*/
1128 /* rUmaskForOp :- returns register mask for an operand */
1129 /*-----------------------------------------------------------------*/
1130 bitVect *rUmaskForOp (operand *op)
1136 /* only temporaries are assigned registers */
1140 sym = OP_SYMBOL(op);
1142 /* if spilt or no registers assigned to it
1144 if (sym->isspilt || !sym->nRegs)
1147 rumask = newBitVect(mcs51_nRegs);
1149 for (j = 0; j < sym->nRegs; j++) {
1150 rumask = bitVectSetBit(rumask,
1151 sym->regs[j]->rIdx);
1157 /*-----------------------------------------------------------------*/
1158 /* regsUsedIniCode :- returns bit vector of registers used in iCode*/
1159 /*-----------------------------------------------------------------*/
1160 bitVect *regsUsedIniCode (iCode *ic)
1162 bitVect *rmask = newBitVect(mcs51_nRegs);
1164 /* do the special cases first */
1165 if (ic->op == IFX ) {
1166 rmask = bitVectUnion(rmask,
1167 rUmaskForOp(IC_COND(ic)));
1171 /* for the jumptable */
1172 if (ic->op == JUMPTABLE) {
1173 rmask = bitVectUnion(rmask,
1174 rUmaskForOp(IC_JTCOND(ic)));
1179 /* of all other cases */
1181 rmask = bitVectUnion(rmask,
1182 rUmaskForOp(IC_LEFT(ic)));
1186 rmask = bitVectUnion(rmask,
1187 rUmaskForOp(IC_RIGHT(ic)));
1190 rmask = bitVectUnion(rmask,
1191 rUmaskForOp(IC_RESULT(ic)));
1197 /*-----------------------------------------------------------------*/
1198 /* createRegMask - for each instruction will determine the regsUsed*/
1199 /*-----------------------------------------------------------------*/
1200 void createRegMask (eBBlock **ebbs, int count)
1204 /* for all blocks */
1205 for (i = 0; i < count ; i++ ) {
1208 if ( ebbs[i]->noPath &&
1209 ( ebbs[i]->entryLabel != entryLabel &&
1210 ebbs[i]->entryLabel != returnLabel ))
1213 /* for all instructions */
1214 for ( ic = ebbs[i]->sch ; ic ; ic = ic->next ) {
1218 if (SKIP_IC2(ic) || !ic->rlive)
1221 /* first mark the registers used in this
1223 ic->rUsed = regsUsedIniCode(ic);
1224 _G.funcrUsed = bitVectUnion(_G.funcrUsed,ic->rUsed);
1226 /* now create the register mask for those
1227 registers that are in use : this is a
1228 super set of ic->rUsed */
1229 ic->rMask = newBitVect(mcs51_nRegs+1);
1231 /* for all live Ranges alive at this point */
1232 for (j = 1; j < ic->rlive->size; j++ ) {
1236 /* if not alive then continue */
1237 if (!bitVectBitValue(ic->rlive,j))
1240 /* find the live range we are interested in */
1241 if (!(sym = hTabItemWithKey(liveRanges,j))) {
1242 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1243 "createRegMask cannot find live range");
1247 /* if no register assigned to it */
1248 if (!sym->nRegs || sym->isspilt)
1251 /* for all the registers allocated to it */
1252 for (k = 0 ; k < sym->nRegs ;k++)
1255 bitVectSetBit(ic->rMask,sym->regs[k]->rIdx);
1261 /*-----------------------------------------------------------------*/
1262 /* rematStr - returns the rematerialized string for a remat var */
1263 /*-----------------------------------------------------------------*/
1264 char *rematStr (symbol *sym)
1267 iCode *ic = sym->rematiCode;
1271 /* if plus or minus print the right hand side */
1272 if (ic->op == '+' || ic->op == '-') {
1273 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1276 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1280 /* we reached the end */
1281 sprintf(s,"%s",OP_SYMBOL(IC_LEFT(ic))->rname);
1288 /*-----------------------------------------------------------------*/
1289 /* regTypeNum - computes the type & number of registers required */
1290 /*-----------------------------------------------------------------*/
1297 /* for each live range do */
1298 for ( sym = hTabFirstItem(liveRanges,&k); sym ;
1299 sym = hTabNextItem(liveRanges,&k)) {
1301 /* if used zero times then no registers needed */
1302 if ((sym->liveTo - sym->liveFrom) == 0)
1306 /* if the live range is a temporary */
1309 /* if the type is marked as a conditional */
1310 if (sym->regType == REG_CND)
1313 /* if used in return only then we don't
1315 if (sym->ruonly || sym->accuse) {
1316 if (IS_AGGREGATE(sym->type) || sym->isptr)
1317 sym->type = aggrToPtr(sym->type,FALSE);
1321 /* if the symbol has only one definition &
1322 that definition is a get_pointer and the
1323 pointer we are getting is rematerializable and
1326 if (bitVectnBitsOn(sym->defs) == 1 &&
1327 (ic = hTabItemWithKey(iCodehTab,
1328 bitVectFirstBit(sym->defs))) &&
1330 !IS_BITVAR(sym->etype)) {
1333 /* if remat in data space */
1334 if (OP_SYMBOL(IC_LEFT(ic))->remat &&
1335 DCL_TYPE(aggrToPtr(sym->type,FALSE)) == POINTER) {
1337 /* create a psuedo symbol & force a spil */
1338 symbol *psym = newSymbol(rematStr(OP_SYMBOL(IC_LEFT(ic))),1);
1339 psym->type = sym->type;
1340 psym->etype = sym->etype;
1341 strcpy(psym->rname,psym->name);
1343 sym->usl.spillLoc = psym;
1347 /* if in data space or idata space then try to
1348 allocate pointer register */
1352 /* if not then we require registers */
1353 sym->nRegs = ((IS_AGGREGATE(sym->type) || sym->isptr ) ?
1354 getSize(sym->type = aggrToPtr(sym->type,FALSE)) :
1355 getSize(sym->type));
1357 if (sym->nRegs > 4) {
1358 fprintf(stderr,"allocated more than 4 or 0 registers for type ");
1359 printTypeChain(sym->type,stderr);fprintf(stderr,"\n");
1362 /* determine the type of register required */
1363 if (sym->nRegs == 1 &&
1364 IS_PTR(sym->type) &&
1366 sym->regType = REG_PTR ;
1368 sym->regType = REG_GPR ;
1371 /* for the first run we don't provide */
1372 /* registers for true symbols we will */
1373 /* see how things go */
1379 /*-----------------------------------------------------------------*/
1380 /* freeAllRegs - mark all registers as free */
1381 /*-----------------------------------------------------------------*/
1386 for (i=0;i< mcs51_nRegs;i++ )
1387 regs8051[i].isFree = 1;
1390 /*-----------------------------------------------------------------*/
1391 /* deallocStackSpil - this will set the stack pointer back */
1392 /*-----------------------------------------------------------------*/
1393 DEFSETFUNC(deallocStackSpil)
1401 /*-----------------------------------------------------------------*/
1402 /* farSpacePackable - returns the packable icode for far variables */
1403 /*-----------------------------------------------------------------*/
1404 static iCode *farSpacePackable (iCode *ic)
1408 /* go thru till we find a definition for the
1409 symbol on the right */
1410 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1412 /* if the definition is a call then no */
1413 if ((dic->op == CALL || dic->op == PCALL) &&
1414 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1418 /* if shift by unknown amount then not */
1419 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1420 IC_RESULT(dic)->key == IC_RIGHT(ic)->key)
1423 /* if pointer get and size > 1 */
1424 if (POINTER_GET(dic) &&
1425 getSize(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)) > 1)
1428 if (POINTER_SET(dic) &&
1429 getSize(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)) > 1)
1432 /* if any three is a true symbol in far space */
1433 if (IC_RESULT(dic) &&
1434 IS_TRUE_SYMOP(IC_RESULT(dic)) &&
1435 isOperandInFarSpace(IC_RESULT(dic)))
1438 if (IC_RIGHT(dic) &&
1439 IS_TRUE_SYMOP(IC_RIGHT(dic)) &&
1440 isOperandInFarSpace(IC_RIGHT(dic)) &&
1441 !isOperandEqual(IC_RIGHT(dic),IC_RESULT(ic)))
1445 IS_TRUE_SYMOP(IC_LEFT(dic)) &&
1446 isOperandInFarSpace(IC_LEFT(dic)) &&
1447 !isOperandEqual(IC_LEFT(dic),IC_RESULT(ic)))
1450 if (isOperandEqual(IC_RIGHT(ic),IC_RESULT(dic))) {
1451 if ( (dic->op == LEFT_OP ||
1452 dic->op == RIGHT_OP ||
1454 IS_OP_LITERAL(IC_RIGHT(dic)))
1464 /*-----------------------------------------------------------------*/
1465 /* packRegsForAssign - register reduction for assignment */
1466 /*-----------------------------------------------------------------*/
1467 int packRegsForAssign (iCode *ic,eBBlock *ebp)
1472 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1473 !IS_ITEMP(IC_RIGHT(ic)) ||
1474 OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
1475 OP_SYMBOL(IC_RIGHT(ic))->isind)
1478 /* if the true symbol is defined in far space or on stack
1479 then we should not since this will increase register pressure */
1480 if (isOperandInFarSpace(IC_RESULT(ic))) {
1481 if ((dic = farSpacePackable(ic)))
1487 /* find the definition of iTempNN scanning backwards if we find a
1488 a use of the true symbol in before we find the definition then
1490 for ( dic = ic->prev ; dic ; dic = dic->prev) {
1492 /* if there is a function call and this is
1493 a parameter & not my parameter then don't pack it */
1494 if ( (dic->op == CALL || dic->op == PCALL) &&
1495 (OP_SYMBOL(IC_RESULT(ic))->_isparm &&
1496 !OP_SYMBOL(IC_RESULT(ic))->ismyparm)) {
1504 if (IS_SYMOP(IC_RESULT(dic)) &&
1505 IC_RESULT(dic)->key == IC_RIGHT(ic)->key) {
1506 if (POINTER_SET(dic))
1512 if (IS_SYMOP(IC_RIGHT(dic)) &&
1513 (IC_RIGHT(dic)->key == IC_RESULT(ic)->key ||
1514 IC_RIGHT(dic)->key == IC_RIGHT(ic)->key)) {
1519 if (IS_SYMOP(IC_LEFT(dic)) &&
1520 (IC_LEFT(dic)->key == IC_RESULT(ic)->key ||
1521 IC_LEFT(dic)->key == IC_RIGHT(ic)->key)) {
1526 if (POINTER_SET(dic) &&
1527 IC_RESULT(dic)->key == IC_RESULT(ic)->key ) {
1534 return 0 ; /* did not find */
1536 /* if the result is on stack or iaccess then it must be
1537 the same atleast one of the operands */
1538 if (OP_SYMBOL(IC_RESULT(ic))->onStack ||
1539 OP_SYMBOL(IC_RESULT(ic))->iaccess ) {
1541 /* the operation has only one symbol
1542 operator then we can pack */
1543 if ((IC_LEFT(dic) && !IS_SYMOP(IC_LEFT(dic))) ||
1544 (IC_RIGHT(dic) && !IS_SYMOP(IC_RIGHT(dic))))
1547 if (!((IC_LEFT(dic) &&
1548 IC_RESULT(ic)->key == IC_LEFT(dic)->key) ||
1550 IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
1554 /* found the definition */
1555 /* replace the result with the result of */
1556 /* this assignment and remove this assignment */
1557 IC_RESULT(dic) = IC_RESULT(ic) ;
1559 if (IS_ITEMP(IC_RESULT(dic)) && OP_SYMBOL(IC_RESULT(dic))->liveFrom > dic->seq) {
1560 OP_SYMBOL(IC_RESULT(dic))->liveFrom = dic->seq;
1562 /* delete from liverange table also
1563 delete from all the points inbetween and the new
1565 for ( sic = dic; sic != ic ; sic = sic->next ) {
1566 bitVectUnSetBit(sic->rlive,IC_RESULT(ic)->key);
1567 if (IS_ITEMP(IC_RESULT(dic)))
1568 bitVectSetBit(sic->rlive,IC_RESULT(dic)->key);
1571 remiCodeFromeBBlock(ebp,ic);
1576 /*-----------------------------------------------------------------*/
1577 /* findAssignToSym : scanning backwards looks for first assig found*/
1578 /*-----------------------------------------------------------------*/
1579 iCode *findAssignToSym (operand *op,iCode *ic)
1583 for (dic = ic->prev ; dic ; dic = dic->prev) {
1585 /* if definition by assignment */
1586 if (dic->op == '=' &&
1587 !POINTER_SET(dic) &&
1588 IC_RESULT(dic)->key == op->key
1589 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1592 /* we are interested only if defined in far space */
1593 /* or in stack space in case of + & - */
1595 /* if assigned to a non-symbol then return
1597 if (!IS_SYMOP(IC_RIGHT(dic)))
1600 /* if the symbol is in far space then
1602 if (isOperandInFarSpace(IC_RIGHT(dic)))
1605 /* for + & - operations make sure that
1606 if it is on the stack it is the same
1607 as one of the three operands */
1608 if ((ic->op == '+' || ic->op == '-') &&
1609 OP_SYMBOL(IC_RIGHT(dic))->onStack) {
1611 if ( IC_RESULT(ic)->key != IC_RIGHT(dic)->key &&
1612 IC_LEFT(ic)->key != IC_RIGHT(dic)->key &&
1613 IC_RIGHT(ic)->key != IC_RIGHT(dic)->key)
1621 /* if we find an usage then we cannot delete it */
1622 if (IC_LEFT(dic) && IC_LEFT(dic)->key == op->key)
1625 if (IC_RIGHT(dic) && IC_RIGHT(dic)->key == op->key)
1628 if (POINTER_SET(dic) && IC_RESULT(dic)->key == op->key)
1632 /* now make sure that the right side of dic
1633 is not defined between ic & dic */
1635 iCode *sic = dic->next ;
1637 for (; sic != ic ; sic = sic->next)
1638 if (IC_RESULT(sic) &&
1639 IC_RESULT(sic)->key == IC_RIGHT(dic)->key)
1648 /*-----------------------------------------------------------------*/
1649 /* packRegsForSupport :- reduce some registers for support calls */
1650 /*-----------------------------------------------------------------*/
1651 int packRegsForSupport (iCode *ic, eBBlock *ebp)
1654 /* for the left & right operand :- look to see if the
1655 left was assigned a true symbol in far space in that
1656 case replace them */
1657 if (IS_ITEMP(IC_LEFT(ic)) &&
1658 OP_SYMBOL(IC_LEFT(ic))->liveTo <= ic->seq) {
1659 iCode *dic = findAssignToSym(IC_LEFT(ic),ic);
1665 /* found it we need to remove it from the
1667 for ( sic = dic; sic != ic ; sic = sic->next )
1668 bitVectUnSetBit(sic->rlive,IC_LEFT(ic)->key);
1670 IC_LEFT(ic)->operand.symOperand =
1671 IC_RIGHT(dic)->operand.symOperand;
1672 IC_LEFT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1673 remiCodeFromeBBlock(ebp,dic);
1677 /* do the same for the right operand */
1680 IS_ITEMP(IC_RIGHT(ic)) &&
1681 OP_SYMBOL(IC_RIGHT(ic))->liveTo <= ic->seq) {
1682 iCode *dic = findAssignToSym(IC_RIGHT(ic),ic);
1688 /* if this is a subtraction & the result
1689 is a true symbol in far space then don't pack */
1690 if (ic->op == '-' && IS_TRUE_SYMOP(IC_RESULT(dic))) {
1691 link *etype =getSpec(operandType(IC_RESULT(dic)));
1692 if (IN_FARSPACE(SPEC_OCLS(etype)))
1695 /* found it we need to remove it from the
1697 for ( sic = dic; sic != ic ; sic = sic->next )
1698 bitVectUnSetBit(sic->rlive,IC_RIGHT(ic)->key);
1700 IC_RIGHT(ic)->operand.symOperand =
1701 IC_RIGHT(dic)->operand.symOperand;
1702 IC_RIGHT(ic)->key = IC_RIGHT(dic)->operand.symOperand->key;
1704 remiCodeFromeBBlock(ebp,dic);
1711 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1714 /*-----------------------------------------------------------------*/
1715 /* packRegsForOneuse : - will reduce some registers for single Use */
1716 /*-----------------------------------------------------------------*/
1717 static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
1722 /* if returning a literal then do nothing */
1726 /* only upto 2 bytes since we cannot predict
1727 the usage of b, & acc */
1728 if (getSize(operandType(op)) > 2 &&
1733 /* this routine will mark the a symbol as used in one
1734 instruction use only && if the defintion is local
1735 (ie. within the basic block) && has only one definition &&
1736 that definiion is either a return value from a
1737 function or does not contain any variables in
1739 uses = bitVectCopy(OP_USES(op));
1740 bitVectUnSetBit(uses,ic->key); /* take away this iCode */
1741 if (!bitVectIsZero(uses)) /* has other uses */
1744 /* if it has only one defintion */
1745 if (bitVectnBitsOn(OP_DEFS(op)) > 1)
1746 return NULL ; /* has more than one definition */
1748 /* get the that definition */
1750 hTabItemWithKey(iCodehTab,
1751 bitVectFirstBit(OP_DEFS(op)))))
1754 /* found the definition now check if it is local */
1755 if (dic->seq < ebp->fSeq ||
1756 dic->seq > ebp->lSeq)
1757 return NULL ; /* non-local */
1759 /* now check if it is the return from
1761 if (dic->op == CALL || dic->op == PCALL ) {
1762 if (ic->op != SEND && ic->op != RETURN) {
1763 OP_SYMBOL(op)->ruonly = 1;
1770 /* otherwise check that the definition does
1771 not contain any symbols in far space */
1772 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1773 isOperandInFarSpace(IC_RIGHT(dic)) ||
1774 IS_OP_RUONLY(IC_LEFT(ic)) ||
1775 IS_OP_RUONLY(IC_RIGHT(ic)) ) {
1779 /* if pointer set then make sure the pointer
1781 if (POINTER_SET(dic) &&
1782 !IS_DATA_PTR(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)))
1785 if (POINTER_GET(dic) &&
1786 !IS_DATA_PTR(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)))
1791 /* also make sure the intervenening instructions
1792 don't have any thing in far space */
1793 for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
1795 /* if there is an intervening function call then no */
1796 if (dic->op == CALL || dic->op == PCALL)
1798 /* if pointer set then make sure the pointer
1800 if (POINTER_SET(dic) &&
1801 !IS_DATA_PTR(aggrToPtr(operandType(IC_RESULT(dic)),FALSE)))
1804 if (POINTER_GET(dic) &&
1805 !IS_DATA_PTR(aggrToPtr(operandType(IC_LEFT(dic)),FALSE)))
1808 /* if address of & the result is remat the okay */
1809 if (dic->op == ADDRESS_OF &&
1810 OP_SYMBOL(IC_RESULT(dic))->remat)
1813 /* if operand has size of three or more & this
1814 operation is a '*','/' or '%' then 'b' may
1816 if (( dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1817 getSize(aggrToPtr(operandType(op),FALSE)) >= 3)
1820 /* if left or right or result is in far space */
1821 if (isOperandInFarSpace(IC_LEFT(dic)) ||
1822 isOperandInFarSpace(IC_RIGHT(dic)) ||
1823 isOperandInFarSpace(IC_RESULT(dic)) ||
1824 IS_OP_RUONLY(IC_LEFT(dic)) ||
1825 IS_OP_RUONLY(IC_RIGHT(dic)) ||
1826 IS_OP_RUONLY(IC_RESULT(dic)) ) {
1831 OP_SYMBOL(op)->ruonly = 1;
1836 /*-----------------------------------------------------------------*/
1837 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1838 /*-----------------------------------------------------------------*/
1839 static bool isBitwiseOptimizable (iCode *ic)
1841 link *ltype = getSpec(operandType(IC_LEFT(ic)));
1842 link *rtype = getSpec(operandType(IC_RIGHT(ic)));
1844 /* bitwise operations are considered optimizable
1845 under the following conditions (Jean-Louis VERN)
1857 if ( IS_LITERAL(rtype) ||
1858 (IS_BITVAR(ltype) && IN_BITSPACE(SPEC_OCLS(ltype))))
1864 /*-----------------------------------------------------------------*/
1865 /* packRegsForAccUse - pack registers for acc use */
1866 /*-----------------------------------------------------------------*/
1867 void packRegsForAccUse (iCode *ic)
1871 /* if + or - then it has to be one byte result */
1872 if ((ic->op == '+' || ic->op == '-')
1873 && getSize(operandType(IC_RESULT(ic))) > 1)
1876 /* if shift operation make sure right side is not a literal */
1877 if (ic->op == RIGHT_OP &&
1878 ( isOperandLiteral(IC_RIGHT(ic)) ||
1879 getSize(operandType(IC_RESULT(ic))) > 1))
1882 if (ic->op == LEFT_OP &&
1883 ( isOperandLiteral(IC_RIGHT(ic)) ||
1884 getSize(operandType(IC_RESULT(ic))) > 1))
1888 /* has only one definition */
1889 if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
1892 /* has only one use */
1893 if (bitVectnBitsOn(OP_USES(IC_RESULT(ic))) > 1)
1896 /* and the usage immediately follows this iCode */
1897 if (!(uic = hTabItemWithKey(iCodehTab,
1898 bitVectFirstBit(OP_USES(IC_RESULT(ic))))))
1901 if (ic->next != uic)
1904 /* if it is a conditional branch then we definitely can */
1905 if (uic->op == IFX )
1908 if ( uic->op == JUMPTABLE )
1911 /* if the usage is not is an assignment
1912 or an arithmetic / bitwise / shift operation then not */
1913 if (POINTER_SET(uic) &&
1914 getSize(aggrToPtr(operandType(IC_RESULT(uic)),FALSE)) > 1)
1917 if (uic->op != '=' &&
1918 !IS_ARITHMETIC_OP(uic) &&
1919 !IS_BITWISE_OP(uic) &&
1920 uic->op != LEFT_OP &&
1921 uic->op != RIGHT_OP )
1924 /* if used in ^ operation then make sure right is not a
1926 if (uic->op == '^' && isOperandLiteral(IC_RIGHT(uic)))
1929 /* if shift operation make sure right side is not a literal */
1930 if (uic->op == RIGHT_OP &&
1931 ( isOperandLiteral(IC_RIGHT(uic)) ||
1932 getSize(operandType(IC_RESULT(uic))) > 1))
1935 if (uic->op == LEFT_OP &&
1936 ( isOperandLiteral(IC_RIGHT(uic)) ||
1937 getSize(operandType(IC_RESULT(uic))) > 1))
1940 /* make sure that the result of this icode is not on the
1941 stack, since acc is used to compute stack offset */
1942 if (IS_TRUE_SYMOP(IC_RESULT(uic)) &&
1943 OP_SYMBOL(IC_RESULT(uic))->onStack)
1946 /* if either one of them in far space then we cannot */
1947 if ((IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1948 isOperandInFarSpace(IC_LEFT(uic))) ||
1949 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1950 isOperandInFarSpace(IC_RIGHT(uic))))
1953 /* if the usage has only one operand then we can */
1954 if (IC_LEFT(uic) == NULL ||
1955 IC_RIGHT(uic) == NULL)
1958 /* make sure this is on the left side if not
1959 a '+' since '+' is commutative */
1960 if (ic->op != '+' &&
1961 IC_LEFT(uic)->key != IC_RESULT(ic)->key)
1964 /* if one of them is a literal then we can */
1965 if ((IC_LEFT(uic) && IS_OP_LITERAL(IC_LEFT(uic))) ||
1966 (IC_RIGHT(uic) && IS_OP_LITERAL(IC_RIGHT(uic)))) {
1967 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1971 /* if the other one is not on stack then we can */
1972 if (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
1973 (IS_ITEMP(IC_RIGHT(uic)) ||
1974 (IS_TRUE_SYMOP(IC_RIGHT(uic)) &&
1975 !OP_SYMBOL(IC_RIGHT(uic))->onStack)))
1978 if (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
1979 (IS_ITEMP(IC_LEFT(uic)) ||
1980 (IS_TRUE_SYMOP(IC_LEFT(uic)) &&
1981 !OP_SYMBOL(IC_LEFT(uic))->onStack)))
1987 OP_SYMBOL(IC_RESULT(ic))->accuse = 1;
1992 /*-----------------------------------------------------------------*/
1993 /* packForPush - hueristics to reduce iCode for pushing */
1994 /*-----------------------------------------------------------------*/
1995 static void packForPush(iCode *ic, eBBlock *ebp)
1999 if (ic->op != IPUSH || !IS_ITEMP(IC_LEFT(ic)))
2002 /* must have only definition & one usage */
2003 if (bitVectnBitsOn(OP_DEFS(IC_LEFT(ic))) != 1 ||
2004 bitVectnBitsOn(OP_USES(IC_LEFT(ic))) != 1 )
2007 /* find the definition */
2008 if (!(dic = hTabItemWithKey(iCodehTab,
2009 bitVectFirstBit(OP_DEFS(IC_LEFT(ic))))))
2012 if (dic->op != '=' || POINTER_SET(dic) || IS_ITEMP(IC_RESULT(dic)))
2015 /* we now we know that it has one & only one def & use
2016 and the that the definition is an assignment */
2017 IC_LEFT(ic) = IC_RIGHT(dic);
2019 remiCodeFromeBBlock(ebp,dic);
2022 /*-----------------------------------------------------------------*/
2023 /* packRegisters - does some transformations to reduce register */
2025 /*-----------------------------------------------------------------*/
2026 static void packRegisters (eBBlock *ebp)
2035 /* look for assignments of the form */
2036 /* iTempNN = TRueSym (someoperation) SomeOperand */
2038 /* TrueSym := iTempNN:1 */
2039 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2042 /* find assignment of the form TrueSym := iTempNN:1 */
2043 if (ic->op == '=' && !POINTER_SET(ic))
2044 change += packRegsForAssign(ic,ebp);
2051 for ( ic = ebp->sch ; ic ; ic = ic->next ) {
2053 /* if this is an itemp & result of a address of a true sym
2054 then mark this as rematerialisable */
2055 if (ic->op == ADDRESS_OF &&
2056 IS_ITEMP(IC_RESULT(ic)) &&
2057 IS_TRUE_SYMOP(IC_LEFT(ic)) &&
2058 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2059 !OP_SYMBOL(IC_LEFT(ic))->onStack ) {
2061 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2062 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2063 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2067 /* if straight assignment then carry remat flag if
2068 this is the only definition */
2069 if (ic->op == '=' &&
2071 IS_SYMOP(IC_RIGHT(ic)) &&
2072 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2073 bitVectnBitsOn(OP_SYMBOL(IC_RESULT(ic))->defs) <= 1) {
2075 OP_SYMBOL(IC_RESULT(ic))->remat =
2076 OP_SYMBOL(IC_RIGHT(ic))->remat;
2077 OP_SYMBOL(IC_RESULT(ic))->rematiCode =
2078 OP_SYMBOL(IC_RIGHT(ic))->rematiCode ;
2081 /* if this is a +/- operation with a rematerizable
2082 then mark this as rematerializable as well only
2083 if the literal value is within the range -255 and + 255
2084 the assembler cannot handle it other wise */
2085 if ((ic->op == '+' || ic->op == '-') &&
2087 (IS_SYMOP(IC_LEFT(ic)) &&
2088 IS_ITEMP(IC_RESULT(ic)) &&
2089 OP_SYMBOL(IC_LEFT(ic))->remat &&
2090 bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) == 1 &&
2091 IS_OP_LITERAL(IC_RIGHT(ic))) ) {
2093 int i = operandLitValue(IC_RIGHT(ic));
2094 if ( i < 255 && i > -255) {
2095 OP_SYMBOL(IC_RESULT(ic))->remat = 1;
2096 OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
2097 OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
2101 /* mark the pointer usages */
2102 if (POINTER_SET(ic))
2103 OP_SYMBOL(IC_RESULT(ic))->uptr = 1;
2105 if (POINTER_GET(ic))
2106 OP_SYMBOL(IC_LEFT(ic))->uptr = 1;
2108 if (!SKIP_IC2(ic)) {
2109 /* if we are using a symbol on the stack
2110 then we should say mcs51_ptrRegReq */
2111 if (ic->op == IFX && IS_SYMOP(IC_COND(ic)))
2112 mcs51_ptrRegReq += ((OP_SYMBOL(IC_COND(ic))->onStack ||
2113 OP_SYMBOL(IC_COND(ic))->iaccess) ? 1 : 0);
2115 if (ic->op == JUMPTABLE && IS_SYMOP(IC_JTCOND(ic)))
2116 mcs51_ptrRegReq += ((OP_SYMBOL(IC_JTCOND(ic))->onStack ||
2117 OP_SYMBOL(IC_JTCOND(ic))->iaccess) ? 1 : 0);
2119 if (IS_SYMOP(IC_LEFT(ic)))
2120 mcs51_ptrRegReq += ((OP_SYMBOL(IC_LEFT(ic))->onStack ||
2121 OP_SYMBOL(IC_LEFT(ic))->iaccess) ? 1 : 0);
2122 if (IS_SYMOP(IC_RIGHT(ic)))
2123 mcs51_ptrRegReq += ((OP_SYMBOL(IC_RIGHT(ic))->onStack ||
2124 OP_SYMBOL(IC_RIGHT(ic))->iaccess) ? 1 : 0);
2125 if (IS_SYMOP(IC_RESULT(ic)))
2126 mcs51_ptrRegReq += ((OP_SYMBOL(IC_RESULT(ic))->onStack ||
2127 OP_SYMBOL(IC_RESULT(ic))->iaccess) ? 1 : 0);
2131 /* if the condition of an if instruction
2132 is defined in the previous instruction then
2133 mark the itemp as a conditional */
2134 if ((IS_CONDITIONAL(ic) ||
2135 ( ( ic->op == BITWISEAND ||
2138 isBitwiseOptimizable(ic))) &&
2139 ic->next && ic->next->op == IFX &&
2140 isOperandEqual(IC_RESULT(ic),IC_COND(ic->next)) &&
2141 OP_SYMBOL(IC_RESULT(ic))->liveTo <= ic->next->seq) {
2143 OP_SYMBOL(IC_RESULT(ic))->regType = REG_CND;
2147 /* reduce for support function calls */
2148 if (ic->supportRtn || ic->op == '+' || ic->op == '-' )
2149 packRegsForSupport (ic,ebp);
2151 /* some cases the redundant moves can
2152 can be eliminated for return statements */
2153 if ((ic->op == RETURN || ic->op == SEND) &&
2154 !isOperandInFarSpace(IC_LEFT(ic)) &&
2156 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2158 /* if pointer set & left has a size more than
2159 one and right is not in far space */
2160 if (POINTER_SET(ic) &&
2161 !isOperandInFarSpace(IC_RIGHT(ic)) &&
2162 !OP_SYMBOL(IC_RESULT(ic))->remat &&
2163 !IS_OP_RUONLY(IC_RIGHT(ic)) &&
2164 getSize(aggrToPtr(operandType(IC_RESULT(ic)),FALSE)) > 1 )
2166 packRegsForOneuse (ic,IC_RESULT(ic),ebp);
2168 /* if pointer get */
2169 if (POINTER_GET(ic) &&
2170 !isOperandInFarSpace(IC_RESULT(ic))&&
2171 !OP_SYMBOL(IC_LEFT(ic))->remat &&
2172 !IS_OP_RUONLY(IC_RESULT(ic)) &&
2173 getSize(aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) > 1 )
2175 packRegsForOneuse (ic,IC_LEFT(ic),ebp);
2178 /* if this is cast for intergral promotion then
2179 check if only use of the definition of the
2180 operand being casted/ if yes then replace
2181 the result of that arithmetic operation with
2182 this result and get rid of the cast */
2183 if (ic->op == CAST) {
2184 link *fromType = operandType(IC_RIGHT(ic));
2185 link *toType = operandType(IC_LEFT(ic));
2187 if (IS_INTEGRAL(fromType) && IS_INTEGRAL(toType) &&
2188 getSize(fromType) != getSize(toType) ) {
2190 iCode *dic = packRegsForOneuse(ic,IC_RIGHT(ic),ebp);
2192 if (IS_ARITHMETIC_OP(dic)) {
2193 IC_RESULT(dic) = IC_RESULT(ic);
2194 remiCodeFromeBBlock(ebp,ic);
2197 OP_SYMBOL(IC_RIGHT(ic))->ruonly = 0;
2201 /* if the type from and type to are the same
2202 then if this is the only use then packit */
2203 if (checkType(operandType(IC_RIGHT(ic)),
2204 operandType(IC_LEFT(ic))) == 1) {
2205 iCode *dic = packRegsForOneuse (ic,IC_RIGHT(ic),ebp);
2207 IC_RESULT(dic) = IC_RESULT(ic);
2208 remiCodeFromeBBlock(ebp,ic);
2216 iTempNN := (some variable in farspace) V1
2221 if (ic->op == IPUSH ) {
2222 packForPush(ic,ebp);
2226 /* pack registers for accumulator use, when the
2227 result of an arithmetic or bit wise operation
2228 has only one use, that use is immediately following
2229 the defintion and the using iCode has only one
2230 operand or has two operands but one is literal &
2231 the result of that operation is not on stack then
2232 we can leave the result of this operation in acc:b
2234 if ((IS_ARITHMETIC_OP(ic)
2236 || IS_BITWISE_OP(ic)
2238 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2241 IS_ITEMP(IC_RESULT(ic)) &&
2242 getSize(operandType(IC_RESULT(ic))) <= 2)
2244 packRegsForAccUse (ic);
2249 /*-----------------------------------------------------------------*/
2250 /* assignRegisters - assigns registers to each live range as need */
2251 /*-----------------------------------------------------------------*/
2252 void mcs51_assignRegisters (eBBlock **ebbs, int count)
2257 setToNull((void *)&_G.funcrUsed);
2258 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2259 /* if not register extentions then reduce number
2261 if (options.regExtend)
2266 /* change assignments this will remove some
2267 live ranges reducing some register pressure */
2268 for (i = 0 ; i < count ;i++ )
2269 packRegisters (ebbs[i]);
2271 if (options.dump_pack)
2272 dumpEbbsToFileExt(".dumppack",ebbs,count);
2274 /* first determine for each live range the number of
2275 registers & the type of registers required for each */
2278 /* and serially allocate registers */
2279 serialRegAssign(ebbs,count);
2281 /* if stack was extended then tell the user */
2282 if (_G.stackExtend) {
2283 /* werror(W_TOOMANY_SPILS,"stack", */
2284 /* _G.stackExtend,currFunc->name,""); */
2285 _G.stackExtend = 0 ;
2288 if (_G.dataExtend) {
2289 /* werror(W_TOOMANY_SPILS,"data space", */
2290 /* _G.dataExtend,currFunc->name,""); */
2294 /* after that create the register mask
2295 for each of the instruction */
2296 createRegMask (ebbs,count);
2298 /* redo that offsets for stacked automatic variables */
2299 redoStackOffsets ();
2301 if (options.dump_rassgn)
2302 dumpEbbsToFileExt(".dumprassgn",ebbs,count);
2304 /* now get back the chain */
2305 ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));
2310 /* free up any _G.stackSpil locations allocated */
2311 applyToSet(_G.stackSpil,deallocStackSpil);
2313 setToNull((void **)&_G.stackSpil);
2314 setToNull((void **)&_G.spiltSet);
2315 /* mark all registers as free */