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 *);
49 bitVect *funcrUsed; /* registers used in a function */
55 /* Shared with gen.c */
56 int mcs51_ptrRegReq; /* one byte pointer register required */
62 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
63 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
64 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
65 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
66 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
67 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
68 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
69 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
70 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
71 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
72 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
73 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
74 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
75 {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, 1},
78 static void spillThis (symbol *);
80 /*-----------------------------------------------------------------*/
81 /* allocReg - allocates register of given type */
82 /*-----------------------------------------------------------------*/
88 for (i = 0; i < mcs51_nRegs; i++)
91 /* if type is given as 0 then any
92 free register will do */
96 regs8051[i].isFree = 0;
99 bitVectSetBit (currFunc->regsUsed, i);
102 /* other wise look for specific type
104 if (regs8051[i].isFree &&
105 regs8051[i].type == type)
107 regs8051[i].isFree = 0;
110 bitVectSetBit (currFunc->regsUsed, i);
117 /*-----------------------------------------------------------------*/
118 /* mcs51_regWithIdx - returns pointer to register wit index number */
119 /*-----------------------------------------------------------------*/
121 mcs51_regWithIdx (int idx)
125 for (i = 0; i < mcs51_nRegs; i++)
126 if (regs8051[i].rIdx == idx)
129 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
130 "regWithIdx not found");
134 /*-----------------------------------------------------------------*/
135 /* freeReg - frees a register */
136 /*-----------------------------------------------------------------*/
142 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
143 "freeReg - Freeing NULL register");
151 /*-----------------------------------------------------------------*/
152 /* nFreeRegs - returns number of free registers */
153 /*-----------------------------------------------------------------*/
160 for (i = 0; i < mcs51_nRegs; i++)
161 if (regs8051[i].isFree && regs8051[i].type == type)
166 /*-----------------------------------------------------------------*/
167 /* nfreeRegsType - free registers with type */
168 /*-----------------------------------------------------------------*/
170 nfreeRegsType (int type)
175 if ((nfr = nFreeRegs (type)) == 0)
176 return nFreeRegs (REG_GPR);
179 return nFreeRegs (type);
183 /*-----------------------------------------------------------------*/
184 /* allDefsOutOfRange - all definitions are out of a range */
185 /*-----------------------------------------------------------------*/
187 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
194 for (i = 0; i < defs->size; i++)
198 if (bitVectBitValue (defs, i) &&
199 (ic = hTabItemWithKey (iCodehTab, i)) &&
200 (ic->seq >= fseq && ic->seq <= toseq))
209 /*-----------------------------------------------------------------*/
210 /* computeSpillable - given a point find the spillable live ranges */
211 /*-----------------------------------------------------------------*/
213 computeSpillable (iCode * ic)
217 /* spillable live ranges are those that are live at this
218 point . the following categories need to be subtracted
220 a) - those that are already spilt
221 b) - if being used by this one
222 c) - defined by this one */
224 spillable = bitVectCopy (ic->rlive);
226 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
228 bitVectCplAnd (spillable, ic->uses); /* used in this one */
229 bitVectUnSetBit (spillable, ic->defKey);
230 spillable = bitVectIntersect (spillable, _G.regAssigned);
235 /*-----------------------------------------------------------------*/
236 /* noSpilLoc - return true if a variable has no spil location */
237 /*-----------------------------------------------------------------*/
239 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
241 return (sym->usl.spillLoc ? 0 : 1);
244 /*-----------------------------------------------------------------*/
245 /* hasSpilLoc - will return 1 if the symbol has spil location */
246 /*-----------------------------------------------------------------*/
248 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
250 return (sym->usl.spillLoc ? 1 : 0);
253 /*-----------------------------------------------------------------*/
254 /* directSpilLoc - will return 1 if the splilocation is in direct */
255 /*-----------------------------------------------------------------*/
257 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
259 if (sym->usl.spillLoc &&
260 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
266 /*-----------------------------------------------------------------*/
267 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
268 /* but is not used as a pointer */
269 /*-----------------------------------------------------------------*/
271 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
273 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
276 /*-----------------------------------------------------------------*/
277 /* rematable - will return 1 if the remat flag is set */
278 /*-----------------------------------------------------------------*/
280 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
285 /*-----------------------------------------------------------------*/
286 /* notUsedInBlock - not used in this block */
287 /*-----------------------------------------------------------------*/
289 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
291 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
292 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
293 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
296 /*-----------------------------------------------------------------*/
297 /* notUsedInRemaining - not used or defined in remain of the block */
298 /*-----------------------------------------------------------------*/
300 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
302 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
303 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
306 /*-----------------------------------------------------------------*/
307 /* allLRs - return true for all */
308 /*-----------------------------------------------------------------*/
310 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
315 /*-----------------------------------------------------------------*/
316 /* liveRangesWith - applies function to a given set of live range */
317 /*-----------------------------------------------------------------*/
319 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
320 eBBlock * ebp, iCode * ic)
325 if (!lrs || !lrs->size)
328 for (i = 1; i < lrs->size; i++)
331 if (!bitVectBitValue (lrs, i))
334 /* if we don't find it in the live range
335 hash table we are in serious trouble */
336 if (!(sym = hTabItemWithKey (liveRanges, i)))
338 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
339 "liveRangesWith could not find liveRange");
343 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
344 addSetHead (&rset, sym);
351 /*-----------------------------------------------------------------*/
352 /* leastUsedLR - given a set determines which is the least used */
353 /*-----------------------------------------------------------------*/
355 leastUsedLR (set * sset)
357 symbol *sym = NULL, *lsym = NULL;
359 sym = lsym = setFirstItem (sset);
364 for (; lsym; lsym = setNextItem (sset))
367 /* if usage is the same then prefer
368 the spill the smaller of the two */
369 if (lsym->used == sym->used)
370 if (getSize (lsym->type) < getSize (sym->type))
374 if (lsym->used < sym->used)
379 setToNull ((void **) &sset);
384 /*-----------------------------------------------------------------*/
385 /* noOverLap - will iterate through the list looking for over lap */
386 /*-----------------------------------------------------------------*/
388 noOverLap (set * itmpStack, symbol * fsym)
393 for (sym = setFirstItem (itmpStack); sym;
394 sym = setNextItem (itmpStack))
396 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
402 /*-----------------------------------------------------------------*/
403 /* isFree - will return 1 if the a free spil location is found */
404 /*-----------------------------------------------------------------*/
409 V_ARG (symbol **, sloc);
410 V_ARG (symbol *, fsym);
412 /* if already found */
416 /* if it is free && and the itmp assigned to
417 this does not have any overlapping live ranges
418 with the one currently being assigned and
419 the size can be accomodated */
421 noOverLap (sym->usl.itmpStack, fsym) &&
422 getSize (sym->type) >= getSize (fsym->type))
431 /*-----------------------------------------------------------------*/
432 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
433 /*-----------------------------------------------------------------*/
435 spillLRWithPtrReg (symbol * forSym)
441 if (!_G.regAssigned ||
442 bitVectIsZero (_G.regAssigned))
445 r0 = mcs51_regWithIdx (R0_IDX);
446 r1 = mcs51_regWithIdx (R1_IDX);
448 /* for all live ranges */
449 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
450 lrsym = hTabNextItem (liveRanges, &k))
454 /* if no registers assigned to it or spilt */
455 /* if it does not overlap with this then
456 not need to spill it */
458 if (lrsym->isspilt || !lrsym->nRegs ||
459 (lrsym->liveTo < forSym->liveFrom))
462 /* go thru the registers : if it is either
463 r0 or r1 then spil it */
464 for (j = 0; j < lrsym->nRegs; j++)
465 if (lrsym->regs[j] == r0 ||
466 lrsym->regs[j] == r1)
475 /*-----------------------------------------------------------------*/
476 /* createStackSpil - create a location on the stack to spil */
477 /*-----------------------------------------------------------------*/
479 createStackSpil (symbol * sym)
482 int useXstack, model;
486 /* first go try and find a free one that is already
487 existing on the stack */
488 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
490 /* found a free one : just update & return */
491 sym->usl.spillLoc = sloc;
494 addSetHead (&sloc->usl.itmpStack, sym);
498 /* could not then have to create one , this is the hard part
499 we need to allocate this on the stack : this is really a
500 hack!! but cannot think of anything better at this time */
502 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
504 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
509 sloc = newiTemp (slocBuffer);
511 /* set the type to the spilling symbol */
512 sloc->type = copyLinkChain (sym->type);
513 sloc->etype = getSpec (sloc->type);
514 SPEC_SCLS (sloc->etype) = S_DATA;
515 SPEC_EXTR (sloc->etype) = 0;
516 SPEC_STAT (sloc->etype) = 0;
518 /* we don't allow it to be allocated`
519 onto the external stack since : so we
520 temporarily turn it off ; we also
521 turn off memory model to prevent
522 the spil from going to the external storage
525 useXstack = options.useXstack;
526 model = options.model;
527 /* noOverlay = options.noOverlay; */
528 /* options.noOverlay = 1; */
529 options.model = options.useXstack = 0;
533 options.useXstack = useXstack;
534 options.model = model;
535 /* options.noOverlay = noOverlay; */
536 sloc->isref = 1; /* to prevent compiler warning */
538 /* if it is on the stack then update the stack */
539 if (IN_STACK (sloc->etype))
541 currFunc->stack += getSize (sloc->type);
542 _G.stackExtend += getSize (sloc->type);
545 _G.dataExtend += getSize (sloc->type);
547 /* add it to the _G.stackSpil set */
548 addSetHead (&_G.stackSpil, sloc);
549 sym->usl.spillLoc = sloc;
552 /* add it to the set of itempStack set
553 of the spill location */
554 addSetHead (&sloc->usl.itmpStack, sym);
558 /*-----------------------------------------------------------------*/
559 /* isSpiltOnStack - returns true if the spil location is on stack */
560 /*-----------------------------------------------------------------*/
562 isSpiltOnStack (symbol * sym)
572 /* if (sym->_G.stackSpil) */
575 if (!sym->usl.spillLoc)
578 etype = getSpec (sym->usl.spillLoc->type);
579 if (IN_STACK (etype))
585 /*-----------------------------------------------------------------*/
586 /* spillThis - spils a specific operand */
587 /*-----------------------------------------------------------------*/
589 spillThis (symbol * sym)
592 /* if this is rematerializable or has a spillLocation
593 we are okay, else we need to create a spillLocation
595 if (!(sym->remat || sym->usl.spillLoc))
596 createStackSpil (sym);
599 /* mark it has spilt & put it in the spilt set */
601 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
603 bitVectUnSetBit (_G.regAssigned, sym->key);
605 for (i = 0; i < sym->nRegs; i++)
609 freeReg (sym->regs[i]);
613 /* if spilt on stack then free up r0 & r1
614 if they could have been assigned to some
616 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
619 spillLRWithPtrReg (sym);
622 if (sym->usl.spillLoc && !sym->remat)
623 sym->usl.spillLoc->allocreq = 1;
627 /*-----------------------------------------------------------------*/
628 /* selectSpil - select a iTemp to spil : rather a simple procedure */
629 /*-----------------------------------------------------------------*/
631 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
633 bitVect *lrcs = NULL;
637 /* get the spillable live ranges */
638 lrcs = computeSpillable (ic);
640 /* get all live ranges that are rematerizable */
641 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
644 /* return the least used of these */
645 return leastUsedLR (selectS);
648 /* get live ranges with spillLocations in direct space */
649 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
651 sym = leastUsedLR (selectS);
652 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
653 sym->usl.spillLoc->rname :
654 sym->usl.spillLoc->name));
656 /* mark it as allocation required */
657 sym->usl.spillLoc->allocreq = 1;
661 /* if the symbol is local to the block then */
662 if (forSym->liveTo < ebp->lSeq)
665 /* check if there are any live ranges allocated
666 to registers that are not used in this block */
667 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
669 sym = leastUsedLR (selectS);
670 /* if this is not rematerializable */
679 /* check if there are any live ranges that not
680 used in the remainder of the block */
681 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
683 sym = leastUsedLR (selectS);
696 /* find live ranges with spillocation && not used as pointers */
697 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
700 sym = leastUsedLR (selectS);
701 /* mark this as allocation required */
702 sym->usl.spillLoc->allocreq = 1;
706 /* find live ranges with spillocation */
707 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
710 sym = leastUsedLR (selectS);
711 sym->usl.spillLoc->allocreq = 1;
715 /* couldn't find then we need to create a spil
716 location on the stack , for which one? the least
718 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
721 /* return a created spil location */
722 sym = createStackSpil (leastUsedLR (selectS));
723 sym->usl.spillLoc->allocreq = 1;
727 /* this is an extreme situation we will spill
728 this one : happens very rarely but it does happen */
734 /*-----------------------------------------------------------------*/
735 /* spilSomething - spil some variable & mark registers as free */
736 /*-----------------------------------------------------------------*/
738 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
743 /* get something we can spil */
744 ssym = selectSpil (ic, ebp, forSym);
746 /* mark it as spilt */
748 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
750 /* mark it as not register assigned &
751 take it away from the set */
752 bitVectUnSetBit (_G.regAssigned, ssym->key);
754 /* mark the registers as free */
755 for (i = 0; i < ssym->nRegs; i++)
757 freeReg (ssym->regs[i]);
759 /* if spilt on stack then free up r0 & r1
760 if they could have been assigned to as gprs */
761 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
764 spillLRWithPtrReg (ssym);
767 /* if this was a block level spil then insert push & pop
768 at the start & end of block respectively */
771 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
772 /* add push to the start of the block */
773 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
774 ebp->sch->next : ebp->sch));
775 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
776 /* add pop to the end of the block */
777 addiCodeToeBBlock (ebp, nic, NULL);
780 /* if spilt because not used in the remainder of the
781 block then add a push before this instruction and
782 a pop at the end of the block */
783 if (ssym->remainSpil)
786 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
787 /* add push just before this instruction */
788 addiCodeToeBBlock (ebp, nic, ic);
790 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
791 /* add pop to the end of the block */
792 addiCodeToeBBlock (ebp, nic, NULL);
801 /*-----------------------------------------------------------------*/
802 /* getRegPtr - will try for PTR if not a GPR type if not spil */
803 /*-----------------------------------------------------------------*/
805 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
810 /* try for a ptr type */
811 if ((reg = allocReg (REG_PTR)))
814 /* try for gpr type */
815 if ((reg = allocReg (REG_GPR)))
818 /* we have to spil */
819 if (!spilSomething (ic, ebp, sym))
822 /* this looks like an infinite loop but
823 in really selectSpil will abort */
827 /*-----------------------------------------------------------------*/
828 /* getRegGpr - will try for GPR if not spil */
829 /*-----------------------------------------------------------------*/
831 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
836 /* try for gpr type */
837 if ((reg = allocReg (REG_GPR)))
840 if (!mcs51_ptrRegReq)
841 if ((reg = allocReg (REG_PTR)))
844 /* we have to spil */
845 if (!spilSomething (ic, ebp, sym))
848 /* this looks like an infinite loop but
849 in really selectSpil will abort */
853 /*-----------------------------------------------------------------*/
854 /* symHasReg - symbol has a given register */
855 /*-----------------------------------------------------------------*/
857 symHasReg (symbol * sym, regs * reg)
861 for (i = 0; i < sym->nRegs; i++)
862 if (sym->regs[i] == reg)
868 /*-----------------------------------------------------------------*/
869 /* deassignLRs - check the live to and if they have registers & are */
870 /* not spilt then free up the registers */
871 /*-----------------------------------------------------------------*/
873 deassignLRs (iCode * ic, eBBlock * ebp)
879 for (sym = hTabFirstItem (liveRanges, &k); sym;
880 sym = hTabNextItem (liveRanges, &k))
884 /* if it does not end here */
885 if (sym->liveTo > ic->seq)
888 /* if it was spilt on stack then we can
889 mark the stack spil location as free */
894 sym->usl.spillLoc->isFree = 1;
900 if (!bitVectBitValue (_G.regAssigned, sym->key))
903 /* special case check if this is an IFX &
904 the privious one was a pop and the
905 previous one was not spilt then keep track
907 if (ic->op == IFX && ic->prev &&
908 ic->prev->op == IPOP &&
909 !ic->prev->parmPush &&
910 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
911 psym = OP_SYMBOL (IC_LEFT (ic->prev));
917 bitVectUnSetBit (_G.regAssigned, sym->key);
919 /* if the result of this one needs registers
920 and does not have it then assign it right
922 if (IC_RESULT (ic) &&
923 !(SKIP_IC2 (ic) || /* not a special icode */
924 ic->op == JUMPTABLE ||
930 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
931 result->liveTo > ic->seq && /* and will live beyond this */
932 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
933 result->regType == sym->regType && /* same register types */
934 result->nRegs && /* which needs registers */
935 !result->isspilt && /* and does not already have them */
937 !bitVectBitValue (_G.regAssigned, result->key) &&
938 /* the number of free regs + number of regs in this LR
939 can accomodate the what result Needs */
940 ((nfreeRegsType (result->regType) +
941 sym->nRegs) >= result->nRegs)
945 for (i = 0; i < result->nRegs; i++)
947 result->regs[i] = sym->regs[i];
949 result->regs[i] = getRegGpr (ic, ebp, result);
951 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
955 /* free the remaining */
956 for (; i < sym->nRegs; i++)
960 if (!symHasReg (psym, sym->regs[i]))
961 freeReg (sym->regs[i]);
964 freeReg (sym->regs[i]);
971 /*-----------------------------------------------------------------*/
972 /* reassignLR - reassign this to registers */
973 /*-----------------------------------------------------------------*/
975 reassignLR (operand * op)
977 symbol *sym = OP_SYMBOL (op);
980 /* not spilt any more */
981 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
982 bitVectUnSetBit (_G.spiltSet, sym->key);
984 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
988 for (i = 0; i < sym->nRegs; i++)
989 sym->regs[i]->isFree = 0;
992 /*-----------------------------------------------------------------*/
993 /* willCauseSpill - determines if allocating will cause a spill */
994 /*-----------------------------------------------------------------*/
996 willCauseSpill (int nr, int rt)
998 /* first check if there are any avlb registers
999 of te type required */
1002 /* special case for pointer type
1003 if pointer type not avlb then
1004 check for type gpr */
1005 if (nFreeRegs (rt) >= nr)
1007 if (nFreeRegs (REG_GPR) >= nr)
1012 if (mcs51_ptrRegReq)
1014 if (nFreeRegs (rt) >= nr)
1019 if (nFreeRegs (REG_PTR) +
1020 nFreeRegs (REG_GPR) >= nr)
1025 /* it will cause a spil */
1029 /*-----------------------------------------------------------------*/
1030 /* positionRegs - the allocator can allocate same registers to res- */
1031 /* ult and operand, if this happens make sure they are in the same */
1032 /* position as the operand otherwise chaos results */
1033 /*-----------------------------------------------------------------*/
1035 positionRegs (symbol * result, symbol * opsym, int lineno)
1037 int count = min (result->nRegs, opsym->nRegs);
1038 int i, j = 0, shared = 0;
1040 /* if the result has been spilt then cannot share */
1045 /* first make sure that they actually share */
1046 for (i = 0; i < count; i++)
1048 for (j = 0; j < count; j++)
1050 if (result->regs[i] == opsym->regs[j] && i != j)
1060 regs *tmp = result->regs[i];
1061 result->regs[i] = result->regs[j];
1062 result->regs[j] = tmp;
1067 /*-----------------------------------------------------------------*/
1068 /* serialRegAssign - serially allocate registers to the variables */
1069 /*-----------------------------------------------------------------*/
1071 serialRegAssign (eBBlock ** ebbs, int count)
1075 /* for all blocks */
1076 for (i = 0; i < count; i++) {
1080 if (ebbs[i]->noPath &&
1081 (ebbs[i]->entryLabel != entryLabel &&
1082 ebbs[i]->entryLabel != returnLabel))
1085 /* of all instructions do */
1086 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1088 /* if this is an ipop that means some live
1089 range will have to be assigned again */
1091 reassignLR (IC_LEFT (ic));
1093 /* if result is present && is a true symbol */
1094 if (IC_RESULT (ic) && ic->op != IFX &&
1095 IS_TRUE_SYMOP (IC_RESULT (ic)))
1096 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1098 /* take away registers from live
1099 ranges that end at this instruction */
1100 deassignLRs (ic, ebbs[i]);
1102 /* some don't need registers */
1103 if (SKIP_IC2 (ic) ||
1104 ic->op == JUMPTABLE ||
1108 (IC_RESULT (ic) && POINTER_SET (ic)))
1111 /* now we need to allocate registers
1112 only for the result */
1113 if (IC_RESULT (ic)) {
1114 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1120 /* if it does not need or is spilt
1121 or is already assigned to registers
1122 or will not live beyond this instructions */
1125 bitVectBitValue (_G.regAssigned, sym->key) ||
1126 sym->liveTo <= ic->seq)
1129 /* if some liverange has been spilt at the block level
1130 and this one live beyond this block then spil this
1132 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1136 /* if trying to allocate this will cause
1137 a spill and there is nothing to spill
1138 or this one is rematerializable then
1140 willCS = willCauseSpill (sym->nRegs, sym->regType);
1141 spillable = computeSpillable (ic);
1142 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1147 /* if it has a spillocation & is used less than
1148 all other live ranges then spill this */
1150 if (sym->usl.spillLoc) {
1151 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1152 allLRs, ebbs[i], ic));
1153 if (leastUsed && leastUsed->used > sym->used) {
1158 /* if none of the liveRanges have a spillLocation then better
1159 to spill this one than anything else already assigned to registers */
1160 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1161 /* if this is local to this block then we might find a block spil */
1162 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1169 /* if we need ptr regs for the right side
1171 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1172 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1176 /* else we assign registers to it */
1177 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1179 for (j = 0; j < sym->nRegs; j++) {
1180 if (sym->regType == REG_PTR)
1181 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1183 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1185 /* if the allocation failed which means
1186 this was spilt then break */
1187 if (!sym->regs[j]) {
1189 fprintf (stderr, "%d reg(s) lost in %s:%d\n",
1190 j, __FILE__,__LINE__);
1196 /* if it shares registers with operands make sure
1197 that they are in the same position */
1198 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1199 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1200 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1201 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1203 /* do the same for the right operand */
1204 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1205 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1206 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1207 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1220 /*-----------------------------------------------------------------*/
1221 /* rUmaskForOp :- returns register mask for an operand */
1222 /*-----------------------------------------------------------------*/
1224 mcs51_rUmaskForOp (operand * op)
1230 /* only temporaries are assigned registers */
1234 sym = OP_SYMBOL (op);
1236 /* if spilt or no registers assigned to it
1238 if (sym->isspilt || !sym->nRegs)
1241 rumask = newBitVect (mcs51_nRegs);
1243 for (j = 0; j < sym->nRegs; j++)
1245 rumask = bitVectSetBit (rumask,
1246 sym->regs[j]->rIdx);
1252 /*-----------------------------------------------------------------*/
1253 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1254 /*-----------------------------------------------------------------*/
1256 regsUsedIniCode (iCode * ic)
1258 bitVect *rmask = newBitVect (mcs51_nRegs);
1260 /* do the special cases first */
1263 rmask = bitVectUnion (rmask,
1264 mcs51_rUmaskForOp (IC_COND (ic)));
1268 /* for the jumptable */
1269 if (ic->op == JUMPTABLE)
1271 rmask = bitVectUnion (rmask,
1272 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1277 /* of all other cases */
1279 rmask = bitVectUnion (rmask,
1280 mcs51_rUmaskForOp (IC_LEFT (ic)));
1284 rmask = bitVectUnion (rmask,
1285 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1288 rmask = bitVectUnion (rmask,
1289 mcs51_rUmaskForOp (IC_RESULT (ic)));
1295 /*-----------------------------------------------------------------*/
1296 /* createRegMask - for each instruction will determine the regsUsed */
1297 /*-----------------------------------------------------------------*/
1299 createRegMask (eBBlock ** ebbs, int count)
1303 /* for all blocks */
1304 for (i = 0; i < count; i++)
1308 if (ebbs[i]->noPath &&
1309 (ebbs[i]->entryLabel != entryLabel &&
1310 ebbs[i]->entryLabel != returnLabel))
1313 /* for all instructions */
1314 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1319 if (SKIP_IC2 (ic) || !ic->rlive)
1322 /* first mark the registers used in this
1324 ic->rUsed = regsUsedIniCode (ic);
1325 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1327 /* now create the register mask for those
1328 registers that are in use : this is a
1329 super set of ic->rUsed */
1330 ic->rMask = newBitVect (mcs51_nRegs + 1);
1332 /* for all live Ranges alive at this point */
1333 for (j = 1; j < ic->rlive->size; j++)
1338 /* if not alive then continue */
1339 if (!bitVectBitValue (ic->rlive, j))
1342 /* find the live range we are interested in */
1343 if (!(sym = hTabItemWithKey (liveRanges, j)))
1345 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1346 "createRegMask cannot find live range");
1350 /* if no register assigned to it */
1351 if (!sym->nRegs || sym->isspilt)
1354 /* for all the registers allocated to it */
1355 for (k = 0; k < sym->nRegs; k++)
1358 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1364 /*-----------------------------------------------------------------*/
1365 /* rematStr - returns the rematerialized string for a remat var */
1366 /*-----------------------------------------------------------------*/
1368 rematStr (symbol * sym)
1371 iCode *ic = sym->rematiCode;
1376 /* if plus or minus print the right hand side */
1377 if (ic->op == '+' || ic->op == '-')
1379 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1382 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1386 /* cast then continue */
1387 if (IS_CAST_ICODE(ic)) {
1388 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1391 /* we reached the end */
1392 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1399 /*-----------------------------------------------------------------*/
1400 /* regTypeNum - computes the type & number of registers required */
1401 /*-----------------------------------------------------------------*/
1409 /* for each live range do */
1410 for (sym = hTabFirstItem (liveRanges, &k); sym;
1411 sym = hTabNextItem (liveRanges, &k))
1414 /* if used zero times then no registers needed */
1415 if ((sym->liveTo - sym->liveFrom) == 0)
1419 /* if the live range is a temporary */
1423 /* if the type is marked as a conditional */
1424 if (sym->regType == REG_CND)
1427 /* if used in return only then we don't
1429 if (sym->ruonly || sym->accuse)
1431 if (IS_AGGREGATE (sym->type) || sym->isptr)
1432 sym->type = aggrToPtr (sym->type, FALSE);
1436 /* if the symbol has only one definition &
1437 that definition is a get_pointer and the
1438 pointer we are getting is rematerializable and
1441 if (bitVectnBitsOn (sym->defs) == 1 &&
1442 (ic = hTabItemWithKey (iCodehTab,
1443 bitVectFirstBit (sym->defs))) &&
1446 !IS_BITVAR (sym->etype))
1450 /* if remat in data space */
1451 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1452 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1453 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1456 /* create a psuedo symbol & force a spil */
1457 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1458 psym->type = sym->type;
1459 psym->etype = sym->etype;
1460 strcpy (psym->rname, psym->name);
1462 sym->usl.spillLoc = psym;
1466 /* if in data space or idata space then try to
1467 allocate pointer register */
1471 /* if not then we require registers */
1472 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1473 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1474 getSize (sym->type));
1478 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1479 printTypeChain (sym->type, stderr);
1480 fprintf (stderr, "\n");
1483 /* determine the type of register required */
1484 if (sym->nRegs == 1 &&
1485 IS_PTR (sym->type) &&
1487 sym->regType = REG_PTR;
1489 sym->regType = REG_GPR;
1493 /* for the first run we don't provide */
1494 /* registers for true symbols we will */
1495 /* see how things go */
1501 /*-----------------------------------------------------------------*/
1502 /* freeAllRegs - mark all registers as free */
1503 /*-----------------------------------------------------------------*/
1509 for (i = 0; i < mcs51_nRegs; i++)
1510 regs8051[i].isFree = 1;
1513 /*-----------------------------------------------------------------*/
1514 /* deallocStackSpil - this will set the stack pointer back */
1515 /*-----------------------------------------------------------------*/
1517 DEFSETFUNC (deallocStackSpil)
1525 /*-----------------------------------------------------------------*/
1526 /* farSpacePackable - returns the packable icode for far variables */
1527 /*-----------------------------------------------------------------*/
1529 farSpacePackable (iCode * ic)
1533 /* go thru till we find a definition for the
1534 symbol on the right */
1535 for (dic = ic->prev; dic; dic = dic->prev)
1537 /* if the definition is a call then no */
1538 if ((dic->op == CALL || dic->op == PCALL) &&
1539 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1544 /* if shift by unknown amount then not */
1545 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1546 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1549 /* if pointer get and size > 1 */
1550 if (POINTER_GET (dic) &&
1551 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1554 if (POINTER_SET (dic) &&
1555 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1558 /* if any three is a true symbol in far space */
1559 if (IC_RESULT (dic) &&
1560 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1561 isOperandInFarSpace (IC_RESULT (dic)))
1564 if (IC_RIGHT (dic) &&
1565 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1566 isOperandInFarSpace (IC_RIGHT (dic)) &&
1567 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1570 if (IC_LEFT (dic) &&
1571 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1572 isOperandInFarSpace (IC_LEFT (dic)) &&
1573 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1576 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1578 if ((dic->op == LEFT_OP ||
1579 dic->op == RIGHT_OP ||
1581 IS_OP_LITERAL (IC_RIGHT (dic)))
1591 /*-----------------------------------------------------------------*/
1592 /* packRegsForAssign - register reduction for assignment */
1593 /*-----------------------------------------------------------------*/
1595 packRegsForAssign (iCode * ic, eBBlock * ebp)
1598 //sym_link *etype = operandType (IC_RIGHT (ic));
1600 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1601 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1602 OP_LIVETO (IC_RIGHT (ic)) > ic->seq
1603 /* why? || IS_BITFIELD (etype) */ )
1608 /* if the true symbol is defined in far space or on stack
1609 then we should not since this will increase register pressure */
1610 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1614 /* find the definition of iTempNN scanning backwards if we find a
1615 a use of the true symbol in before we find the definition then
1617 for (dic = ic->prev; dic; dic = dic->prev)
1619 /* if there is a function call then don't pack it */
1620 if ((dic->op == CALL || dic->op == PCALL))
1629 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1630 IS_OP_VOLATILE (IC_RESULT (dic)))
1636 if (IS_SYMOP (IC_RESULT (dic)) &&
1637 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1639 if (POINTER_SET (dic))
1645 if (IS_SYMOP (IC_RIGHT (dic)) &&
1646 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1647 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1653 if (IS_SYMOP (IC_LEFT (dic)) &&
1654 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1655 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1661 if (POINTER_SET (dic) &&
1662 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1670 return 0; /* did not find */
1672 /* if assignment then check that right is not a bit */
1673 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1675 sym_link *etype = operandType (IC_RIGHT (dic));
1676 if (IS_BITFIELD (etype))
1679 /* if the result is on stack or iaccess then it must be
1680 the same atleast one of the operands */
1681 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1682 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1685 /* the operation has only one symbol
1686 operator then we can pack */
1687 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1688 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1691 if (!((IC_LEFT (dic) &&
1692 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1694 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1698 /* found the definition */
1699 /* replace the result with the result of */
1700 /* this assignment and remove this assignment */
1701 IC_RESULT (dic) = IC_RESULT (ic);
1703 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1705 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1707 /* delete from liverange table also
1708 delete from all the points inbetween and the new
1710 for (sic = dic; sic != ic; sic = sic->next)
1712 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1713 if (IS_ITEMP (IC_RESULT (dic)))
1714 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1717 remiCodeFromeBBlock (ebp, ic);
1718 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1719 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1724 /*-----------------------------------------------------------------*/
1725 /* findAssignToSym : scanning backwards looks for first assig found */
1726 /*-----------------------------------------------------------------*/
1728 findAssignToSym (operand * op, iCode * ic)
1732 for (dic = ic->prev; dic; dic = dic->prev)
1735 /* if definition by assignment */
1736 if (dic->op == '=' &&
1737 !POINTER_SET (dic) &&
1738 IC_RESULT (dic)->key == op->key
1739 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1743 /* we are interested only if defined in far space */
1744 /* or in stack space in case of + & - */
1746 /* if assigned to a non-symbol then return
1748 if (!IS_SYMOP (IC_RIGHT (dic)))
1751 /* if the symbol is in far space then
1753 if (isOperandInFarSpace (IC_RIGHT (dic)))
1756 /* for + & - operations make sure that
1757 if it is on the stack it is the same
1758 as one of the three operands */
1759 if ((ic->op == '+' || ic->op == '-') &&
1760 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1763 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1764 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1765 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1773 /* if we find an usage then we cannot delete it */
1774 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1777 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1780 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1784 /* now make sure that the right side of dic
1785 is not defined between ic & dic */
1788 iCode *sic = dic->next;
1790 for (; sic != ic; sic = sic->next)
1791 if (IC_RESULT (sic) &&
1792 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1801 /*-----------------------------------------------------------------*/
1802 /* packRegsForSupport :- reduce some registers for support calls */
1803 /*-----------------------------------------------------------------*/
1805 packRegsForSupport (iCode * ic, eBBlock * ebp)
1810 /* for the left & right operand :- look to see if the
1811 left was assigned a true symbol in far space in that
1812 case replace them */
1814 if (IS_ITEMP (IC_LEFT (ic)) &&
1815 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1817 dic = findAssignToSym (IC_LEFT (ic), ic);
1822 /* found it we need to remove it from the
1824 for (sic = dic; sic != ic; sic = sic->next)
1825 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1827 OP_SYMBOL(IC_LEFT (ic))=OP_SYMBOL(IC_RIGHT (dic));
1828 IC_LEFT (ic)->key = OP_SYMBOL(IC_RIGHT (dic))->key;
1829 remiCodeFromeBBlock (ebp, dic);
1830 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1834 /* do the same for the right operand */
1837 IS_ITEMP (IC_RIGHT (ic)) &&
1838 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1840 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1846 /* if this is a subtraction & the result
1847 is a true symbol in far space then don't pack */
1848 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1850 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1851 if (IN_FARSPACE (SPEC_OCLS (etype)))
1854 /* found it we need to remove it from the
1856 for (sic = dic; sic != ic; sic = sic->next)
1857 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1859 IC_RIGHT (ic)->operand.symOperand =
1860 IC_RIGHT (dic)->operand.symOperand;
1861 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1863 remiCodeFromeBBlock (ebp, dic);
1864 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1871 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1874 /*-----------------------------------------------------------------*/
1875 /* packRegsForOneuse : - will reduce some registers for single Use */
1876 /*-----------------------------------------------------------------*/
1878 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1883 /* if returning a literal then do nothing */
1887 /* only upto 2 bytes since we cannot predict
1888 the usage of b, & acc */
1889 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
1892 if (ic->op != RETURN &&
1894 !POINTER_SET (ic) &&
1898 /* this routine will mark the a symbol as used in one
1899 instruction use only && if the defintion is local
1900 (ie. within the basic block) && has only one definition &&
1901 that definiion is either a return value from a
1902 function or does not contain any variables in
1904 uses = bitVectCopy (OP_USES (op));
1905 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1906 if (!bitVectIsZero (uses)) /* has other uses */
1909 /* if it has only one defintion */
1910 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1911 return NULL; /* has more than one definition */
1913 /* get that definition */
1915 hTabItemWithKey (iCodehTab,
1916 bitVectFirstBit (OP_DEFS (op)))))
1919 /* if that only usage is a cast */
1920 if (dic->op == CAST) {
1921 /* to a bigger type */
1922 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
1923 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
1924 /* than we can not, since we cannot predict the usage of b & acc */
1929 /* found the definition now check if it is local */
1930 if (dic->seq < ebp->fSeq ||
1931 dic->seq > ebp->lSeq)
1932 return NULL; /* non-local */
1934 /* now check if it is the return from
1936 if (dic->op == CALL || dic->op == PCALL)
1938 if (ic->op != SEND && ic->op != RETURN)
1940 OP_SYMBOL (op)->ruonly = 1;
1947 /* otherwise check that the definition does
1948 not contain any symbols in far space */
1949 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1950 isOperandInFarSpace (IC_RIGHT (dic)) ||
1951 IS_OP_RUONLY (IC_LEFT (ic)) ||
1952 IS_OP_RUONLY (IC_RIGHT (ic)))
1957 /* if pointer set then make sure the pointer
1959 if (POINTER_SET (dic) &&
1960 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1963 if (POINTER_GET (dic) &&
1964 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1969 /* also make sure the intervenening instructions
1970 don't have any thing in far space */
1971 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
1974 /* if there is an intervening function call then no */
1975 if (dic->op == CALL || dic->op == PCALL)
1977 /* if pointer set then make sure the pointer
1979 if (POINTER_SET (dic) &&
1980 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1983 if (POINTER_GET (dic) &&
1984 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1987 /* if address of & the result is remat the okay */
1988 if (dic->op == ADDRESS_OF &&
1989 OP_SYMBOL (IC_RESULT (dic))->remat)
1992 /* if operand has size of three or more & this
1993 operation is a '*','/' or '%' then 'b' may
1995 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1996 getSize (operandType (op)) >= 3)
1999 /* if left or right or result is in far space */
2000 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2001 isOperandInFarSpace (IC_RIGHT (dic)) ||
2002 isOperandInFarSpace (IC_RESULT (dic)) ||
2003 IS_OP_RUONLY (IC_LEFT (dic)) ||
2004 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2005 IS_OP_RUONLY (IC_RESULT (dic)))
2009 /* if left or right or result is on stack */
2010 if (isOperandOnStack(IC_LEFT(dic)) ||
2011 isOperandOnStack(IC_RIGHT(dic)) ||
2012 isOperandOnStack(IC_RESULT(dic))) {
2017 OP_SYMBOL (op)->ruonly = 1;
2022 /*-----------------------------------------------------------------*/
2023 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2024 /*-----------------------------------------------------------------*/
2026 isBitwiseOptimizable (iCode * ic)
2028 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2029 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2031 /* bitwise operations are considered optimizable
2032 under the following conditions (Jean-Louis VERN)
2044 if (IS_LITERAL(rtype) ||
2045 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2051 /*-----------------------------------------------------------------*/
2052 /* packRegsForAccUse - pack registers for acc use */
2053 /*-----------------------------------------------------------------*/
2055 packRegsForAccUse (iCode * ic)
2059 /* if this is an aggregate, e.g. a one byte char array */
2060 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2064 /* if + or - then it has to be one byte result */
2065 if ((ic->op == '+' || ic->op == '-')
2066 && getSize (operandType (IC_RESULT (ic))) > 1)
2069 /* if shift operation make sure right side is not a literal */
2070 if (ic->op == RIGHT_OP &&
2071 (isOperandLiteral (IC_RIGHT (ic)) ||
2072 getSize (operandType (IC_RESULT (ic))) > 1))
2075 if (ic->op == LEFT_OP &&
2076 (isOperandLiteral (IC_RIGHT (ic)) ||
2077 getSize (operandType (IC_RESULT (ic))) > 1))
2080 if (IS_BITWISE_OP (ic) &&
2081 getSize (operandType (IC_RESULT (ic))) > 1)
2085 /* has only one definition */
2086 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2089 /* has only one use */
2090 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2093 /* and the usage immediately follows this iCode */
2094 if (!(uic = hTabItemWithKey (iCodehTab,
2095 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2098 if (ic->next != uic)
2101 /* if it is a conditional branch then we definitely can */
2105 if (uic->op == JUMPTABLE)
2108 /* if the usage is not is an assignment
2109 or an arithmetic / bitwise / shift operation then not */
2110 if (POINTER_SET (uic) &&
2111 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2114 if (uic->op != '=' &&
2115 !IS_ARITHMETIC_OP (uic) &&
2116 !IS_BITWISE_OP (uic) &&
2117 uic->op != LEFT_OP &&
2118 uic->op != RIGHT_OP)
2121 /* if used in ^ operation then make sure right is not a
2123 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2126 /* if shift operation make sure right side is not a literal */
2127 if (uic->op == RIGHT_OP &&
2128 (isOperandLiteral (IC_RIGHT (uic)) ||
2129 getSize (operandType (IC_RESULT (uic))) > 1))
2132 if (uic->op == LEFT_OP &&
2133 (isOperandLiteral (IC_RIGHT (uic)) ||
2134 getSize (operandType (IC_RESULT (uic))) > 1))
2137 /* make sure that the result of this icode is not on the
2138 stack, since acc is used to compute stack offset */
2139 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2140 OP_SYMBOL (IC_RESULT (uic))->onStack)
2143 /* if either one of them in far space then we cannot */
2144 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2145 isOperandInFarSpace (IC_LEFT (uic))) ||
2146 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2147 isOperandInFarSpace (IC_RIGHT (uic))))
2150 /* if the usage has only one operand then we can */
2151 if (IC_LEFT (uic) == NULL ||
2152 IC_RIGHT (uic) == NULL)
2155 /* make sure this is on the left side if not
2156 a '+' since '+' is commutative */
2157 if (ic->op != '+' &&
2158 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2162 // this is too dangerous and need further restrictions
2165 /* if one of them is a literal then we can */
2166 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2167 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2169 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2174 /* if the other one is not on stack then we can */
2175 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2176 (IS_ITEMP (IC_RIGHT (uic)) ||
2177 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2178 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2181 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2182 (IS_ITEMP (IC_LEFT (uic)) ||
2183 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2184 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2190 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2195 /*-----------------------------------------------------------------*/
2196 /* packForPush - hueristics to reduce iCode for pushing */
2197 /*-----------------------------------------------------------------*/
2199 packForPush (iCode * ic, eBBlock * ebp)
2204 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2207 /* must have only definition & one usage */
2208 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2209 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2212 /* find the definition */
2213 if (!(dic = hTabItemWithKey (iCodehTab,
2214 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2217 if (dic->op != '=' || POINTER_SET (dic))
2220 /* make sure the right side does not have any definitions
2222 dbv = OP_DEFS(IC_RIGHT(dic));
2223 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2224 if (bitVectBitValue(dbv,lic->key))
2227 /* make sure they have the same type */
2229 sym_link *itype=operandType(IC_LEFT(ic));
2230 sym_link *ditype=operandType(IC_RIGHT(dic));
2232 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2233 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2236 /* extend the live range of replaced operand if needed */
2237 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2238 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2240 /* we now we know that it has one & only one def & use
2241 and the that the definition is an assignment */
2242 IC_LEFT (ic) = IC_RIGHT (dic);
2244 remiCodeFromeBBlock (ebp, dic);
2245 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2248 /*-----------------------------------------------------------------*/
2249 /* packRegisters - does some transformations to reduce register */
2251 /*-----------------------------------------------------------------*/
2253 packRegisters (eBBlock * ebp)
2263 /* look for assignments of the form */
2264 /* iTempNN = TRueSym (someoperation) SomeOperand */
2266 /* TrueSym := iTempNN:1 */
2267 for (ic = ebp->sch; ic; ic = ic->next)
2269 /* find assignment of the form TrueSym := iTempNN:1 */
2270 if (ic->op == '=' && !POINTER_SET (ic))
2271 change += packRegsForAssign (ic, ebp);
2278 for (ic = ebp->sch; ic; ic = ic->next)
2280 /* if this is an itemp & result of an address of a true sym
2281 then mark this as rematerialisable */
2282 if (ic->op == ADDRESS_OF &&
2283 IS_ITEMP (IC_RESULT (ic)) &&
2284 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2285 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2286 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2289 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2290 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2291 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2295 /* if straight assignment then carry remat flag if
2296 this is the only definition */
2297 if (ic->op == '=' &&
2298 !POINTER_SET (ic) &&
2299 IS_SYMOP (IC_RIGHT (ic)) &&
2300 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2301 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2302 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2305 OP_SYMBOL (IC_RESULT (ic))->remat =
2306 OP_SYMBOL (IC_RIGHT (ic))->remat;
2307 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2308 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2311 /* if cast to a generic pointer & the pointer being
2312 cast is remat, then we can remat this cast as well */
2313 if (ic->op == CAST &&
2314 IS_SYMOP(IC_RIGHT(ic)) &&
2315 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2316 sym_link *to_type = operandType(IC_LEFT(ic));
2317 sym_link *from_type = operandType(IC_RIGHT(ic));
2318 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2319 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2320 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2321 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2325 /* if this is a +/- operation with a rematerizable
2326 then mark this as rematerializable as well */
2327 if ((ic->op == '+' || ic->op == '-') &&
2328 (IS_SYMOP (IC_LEFT (ic)) &&
2329 IS_ITEMP (IC_RESULT (ic)) &&
2330 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2331 OP_SYMBOL (IC_LEFT (ic))->remat &&
2332 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2333 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2335 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2336 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2337 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2340 /* mark the pointer usages */
2341 if (POINTER_SET (ic))
2342 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2344 if (POINTER_GET (ic))
2345 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2349 /* if we are using a symbol on the stack
2350 then we should say mcs51_ptrRegReq */
2351 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2352 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2353 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2354 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2355 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2356 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2359 if (IS_SYMOP (IC_LEFT (ic)))
2360 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2361 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2362 if (IS_SYMOP (IC_RIGHT (ic)))
2363 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2364 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2365 if (IS_SYMOP (IC_RESULT (ic)))
2366 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2367 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2371 /* if the condition of an if instruction
2372 is defined in the previous instruction and
2373 this is the only usage then
2374 mark the itemp as a conditional */
2375 if ((IS_CONDITIONAL (ic) ||
2376 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2377 ic->next && ic->next->op == IFX &&
2378 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2379 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2380 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2382 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2386 /* reduce for support function calls */
2387 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2388 packRegsForSupport (ic, ebp);
2390 /* some cases the redundant moves can
2391 can be eliminated for return statements */
2392 if ((ic->op == RETURN || ic->op == SEND) &&
2393 !isOperandInFarSpace (IC_LEFT (ic)) &&
2394 options.model == MODEL_SMALL) {
2395 if (0 && options.stackAuto) {
2396 /* we should check here if acc will be clobbered for stack
2397 offset calculations */
2399 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2403 /* if pointer set & left has a size more than
2404 one and right is not in far space */
2405 if (POINTER_SET (ic) &&
2406 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2407 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2408 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2409 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2411 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2413 /* if pointer get */
2414 if (POINTER_GET (ic) &&
2415 !isOperandInFarSpace (IC_RESULT (ic)) &&
2416 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2417 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2418 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2420 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2423 /* if this is cast for intergral promotion then
2424 check if only use of the definition of the
2425 operand being casted/ if yes then replace
2426 the result of that arithmetic operation with
2427 this result and get rid of the cast */
2430 sym_link *fromType = operandType (IC_RIGHT (ic));
2431 sym_link *toType = operandType (IC_LEFT (ic));
2433 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2434 getSize (fromType) != getSize (toType) &&
2435 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2438 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2441 if (IS_ARITHMETIC_OP (dic))
2443 IC_RESULT (dic) = IC_RESULT (ic);
2444 remiCodeFromeBBlock (ebp, ic);
2445 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2446 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2450 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2456 /* if the type from and type to are the same
2457 then if this is the only use then packit */
2458 if (compareType (operandType (IC_RIGHT (ic)),
2459 operandType (IC_LEFT (ic))) == 1)
2461 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2464 IC_RESULT (dic) = IC_RESULT (ic);
2465 remiCodeFromeBBlock (ebp, ic);
2466 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2467 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2475 iTempNN := (some variable in farspace) V1
2480 if (ic->op == IPUSH)
2482 packForPush (ic, ebp);
2486 /* pack registers for accumulator use, when the
2487 result of an arithmetic or bit wise operation
2488 has only one use, that use is immediately following
2489 the defintion and the using iCode has only one
2490 operand or has two operands but one is literal &
2491 the result of that operation is not on stack then
2492 we can leave the result of this operation in acc:b
2494 if ((IS_ARITHMETIC_OP (ic)
2495 || IS_CONDITIONAL(ic)
2496 || IS_BITWISE_OP (ic)
2497 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2498 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2500 IS_ITEMP (IC_RESULT (ic)) &&
2501 getSize (operandType (IC_RESULT (ic))) <= 2)
2503 packRegsForAccUse (ic);
2507 /*-----------------------------------------------------------------*/
2508 /* assignRegisters - assigns registers to each live range as need */
2509 /*-----------------------------------------------------------------*/
2511 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2516 setToNull ((void *) &_G.funcrUsed);
2517 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2520 /* change assignments this will remove some
2521 live ranges reducing some register pressure */
2522 for (i = 0; i < count; i++)
2523 packRegisters (ebbs[i]);
2525 if (options.dump_pack)
2526 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2528 /* first determine for each live range the number of
2529 registers & the type of registers required for each */
2532 /* and serially allocate registers */
2533 serialRegAssign (ebbs, count);
2535 /* if stack was extended then tell the user */
2538 /* werror(W_TOOMANY_SPILS,"stack", */
2539 /* _G.stackExtend,currFunc->name,""); */
2545 /* werror(W_TOOMANY_SPILS,"data space", */
2546 /* _G.dataExtend,currFunc->name,""); */
2550 /* after that create the register mask
2551 for each of the instruction */
2552 createRegMask (ebbs, count);
2554 /* redo that offsets for stacked automatic variables */
2555 redoStackOffsets ();
2557 if (options.dump_rassgn)
2559 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2560 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2563 /* do the overlaysegment stuff SDCCmem.c */
2564 doOverlays (ebbs, count);
2566 /* now get back the chain */
2567 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2571 /* free up any _G.stackSpil locations allocated */
2572 applyToSet (_G.stackSpil, deallocStackSpil);
2574 setToNull ((void **) &_G.stackSpil);
2575 setToNull ((void **) &_G.spiltSet);
2576 /* mark all registers as free */