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 (sym->liveFrom < fsym->liveTo &&
397 sym->liveTo > fsym->liveTo) return 0;
399 if (sym->liveFrom < fsym->liveFrom &&
400 sym->liveTo > fsym->liveFrom) return 0;
406 /*-----------------------------------------------------------------*/
407 /* isFree - will return 1 if the a free spil location is found */
408 /*-----------------------------------------------------------------*/
413 V_ARG (symbol **, sloc);
414 V_ARG (symbol *, fsym);
416 /* if already found */
420 /* if it is free && and the itmp assigned to
421 this does not have any overlapping live ranges
422 with the one currently being assigned and
423 the size can be accomodated */
425 noOverLap (sym->usl.itmpStack, fsym) &&
426 getSize (sym->type) >= getSize (fsym->type))
435 /*-----------------------------------------------------------------*/
436 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
437 /*-----------------------------------------------------------------*/
439 spillLRWithPtrReg (symbol * forSym)
445 if (!_G.regAssigned ||
446 bitVectIsZero (_G.regAssigned))
449 r0 = mcs51_regWithIdx (R0_IDX);
450 r1 = mcs51_regWithIdx (R1_IDX);
452 /* for all live ranges */
453 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
454 lrsym = hTabNextItem (liveRanges, &k))
458 /* if no registers assigned to it or spilt */
459 /* if it does not overlap with this then
460 not need to spill it */
462 if (lrsym->isspilt || !lrsym->nRegs ||
463 (lrsym->liveTo < forSym->liveFrom))
466 /* go thru the registers : if it is either
467 r0 or r1 then spil it */
468 for (j = 0; j < lrsym->nRegs; j++)
469 if (lrsym->regs[j] == r0 ||
470 lrsym->regs[j] == r1)
479 /*-----------------------------------------------------------------*/
480 /* createStackSpil - create a location on the stack to spil */
481 /*-----------------------------------------------------------------*/
483 createStackSpil (symbol * sym)
486 int useXstack, model;
490 /* first go try and find a free one that is already
491 existing on the stack */
492 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
494 /* found a free one : just update & return */
495 sym->usl.spillLoc = sloc;
498 addSetHead (&sloc->usl.itmpStack, sym);
502 /* could not then have to create one , this is the hard part
503 we need to allocate this on the stack : this is really a
504 hack!! but cannot think of anything better at this time */
506 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
508 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
513 sloc = newiTemp (slocBuffer);
515 /* set the type to the spilling symbol */
516 sloc->type = copyLinkChain (sym->type);
517 sloc->etype = getSpec (sloc->type);
518 SPEC_SCLS (sloc->etype) = S_DATA;
519 SPEC_EXTR (sloc->etype) = 0;
521 /* we don't allow it to be allocated`
522 onto the external stack since : so we
523 temporarily turn it off ; we also
524 turn off memory model to prevent
525 the spil from going to the external storage
528 useXstack = options.useXstack;
529 model = options.model;
530 /* noOverlay = options.noOverlay; */
531 /* options.noOverlay = 1; */
532 options.model = options.useXstack = 0;
536 options.useXstack = useXstack;
537 options.model = model;
538 /* options.noOverlay = noOverlay; */
539 sloc->isref = 1; /* to prevent compiler warning */
541 /* if it is on the stack then update the stack */
542 if (IN_STACK (sloc->etype))
544 currFunc->stack += getSize (sloc->type);
545 _G.stackExtend += getSize (sloc->type);
548 _G.dataExtend += getSize (sloc->type);
550 /* add it to the _G.stackSpil set */
551 addSetHead (&_G.stackSpil, sloc);
552 sym->usl.spillLoc = sloc;
555 /* add it to the set of itempStack set
556 of the spill location */
557 addSetHead (&sloc->usl.itmpStack, sym);
561 /*-----------------------------------------------------------------*/
562 /* isSpiltOnStack - returns true if the spil location is on stack */
563 /*-----------------------------------------------------------------*/
565 isSpiltOnStack (symbol * sym)
575 /* if (sym->_G.stackSpil) */
578 if (!sym->usl.spillLoc)
581 etype = getSpec (sym->usl.spillLoc->type);
582 if (IN_STACK (etype))
588 /*-----------------------------------------------------------------*/
589 /* spillThis - spils a specific operand */
590 /*-----------------------------------------------------------------*/
592 spillThis (symbol * sym)
595 /* if this is rematerializable or has a spillLocation
596 we are okay, else we need to create a spillLocation
598 if (!(sym->remat || sym->usl.spillLoc))
599 createStackSpil (sym);
602 /* mark it has spilt & put it in the spilt set */
604 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
606 bitVectUnSetBit (_G.regAssigned, sym->key);
608 for (i = 0; i < sym->nRegs; i++)
612 freeReg (sym->regs[i]);
616 /* if spilt on stack then free up r0 & r1
617 if they could have been assigned to some
619 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
622 spillLRWithPtrReg (sym);
625 if (sym->usl.spillLoc && !sym->remat)
626 sym->usl.spillLoc->allocreq = 1;
630 /*-----------------------------------------------------------------*/
631 /* selectSpil - select a iTemp to spil : rather a simple procedure */
632 /*-----------------------------------------------------------------*/
634 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
636 bitVect *lrcs = NULL;
640 /* get the spillable live ranges */
641 lrcs = computeSpillable (ic);
643 /* get all live ranges that are rematerizable */
644 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
647 /* return the least used of these */
648 return leastUsedLR (selectS);
651 /* get live ranges with spillLocations in direct space */
652 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
654 sym = leastUsedLR (selectS);
655 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
656 sym->usl.spillLoc->rname :
657 sym->usl.spillLoc->name));
659 /* mark it as allocation required */
660 sym->usl.spillLoc->allocreq = 1;
664 /* if the symbol is local to the block then */
665 if (forSym->liveTo < ebp->lSeq)
668 /* check if there are any live ranges allocated
669 to registers that are not used in this block */
670 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
672 sym = leastUsedLR (selectS);
673 /* if this is not rematerializable */
682 /* check if there are any live ranges that not
683 used in the remainder of the block */
684 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
686 sym = leastUsedLR (selectS);
699 /* find live ranges with spillocation && not used as pointers */
700 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
703 sym = leastUsedLR (selectS);
704 /* mark this as allocation required */
705 sym->usl.spillLoc->allocreq = 1;
709 /* find live ranges with spillocation */
710 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
713 sym = leastUsedLR (selectS);
714 sym->usl.spillLoc->allocreq = 1;
718 /* couldn't find then we need to create a spil
719 location on the stack , for which one? the least
721 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
724 /* return a created spil location */
725 sym = createStackSpil (leastUsedLR (selectS));
726 sym->usl.spillLoc->allocreq = 1;
730 /* this is an extreme situation we will spill
731 this one : happens very rarely but it does happen */
737 /*-----------------------------------------------------------------*/
738 /* spilSomething - spil some variable & mark registers as free */
739 /*-----------------------------------------------------------------*/
741 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
746 /* get something we can spil */
747 ssym = selectSpil (ic, ebp, forSym);
749 /* mark it as spilt */
751 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
753 /* mark it as not register assigned &
754 take it away from the set */
755 bitVectUnSetBit (_G.regAssigned, ssym->key);
757 /* mark the registers as free */
758 for (i = 0; i < ssym->nRegs; i++)
760 freeReg (ssym->regs[i]);
762 /* if spilt on stack then free up r0 & r1
763 if they could have been assigned to as gprs */
764 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
767 spillLRWithPtrReg (ssym);
770 /* if this was a block level spil then insert push & pop
771 at the start & end of block respectively */
774 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
775 /* add push to the start of the block */
776 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
777 ebp->sch->next : ebp->sch));
778 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
779 /* add pop to the end of the block */
780 addiCodeToeBBlock (ebp, nic, NULL);
783 /* if spilt because not used in the remainder of the
784 block then add a push before this instruction and
785 a pop at the end of the block */
786 if (ssym->remainSpil)
789 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
790 /* add push just before this instruction */
791 addiCodeToeBBlock (ebp, nic, ic);
793 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
794 /* add pop to the end of the block */
795 addiCodeToeBBlock (ebp, nic, NULL);
804 /*-----------------------------------------------------------------*/
805 /* getRegPtr - will try for PTR if not a GPR type if not spil */
806 /*-----------------------------------------------------------------*/
808 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
813 /* try for a ptr type */
814 if ((reg = allocReg (REG_PTR)))
817 /* try for gpr type */
818 if ((reg = allocReg (REG_GPR)))
821 /* we have to spil */
822 if (!spilSomething (ic, ebp, sym))
825 /* this looks like an infinite loop but
826 in really selectSpil will abort */
830 /*-----------------------------------------------------------------*/
831 /* getRegGpr - will try for GPR if not spil */
832 /*-----------------------------------------------------------------*/
834 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
839 /* try for gpr type */
840 if ((reg = allocReg (REG_GPR)))
843 if (!mcs51_ptrRegReq)
844 if ((reg = allocReg (REG_PTR)))
847 /* we have to spil */
848 if (!spilSomething (ic, ebp, sym))
851 /* this looks like an infinite loop but
852 in really selectSpil will abort */
856 /*-----------------------------------------------------------------*/
857 /* symHasReg - symbol has a given register */
858 /*-----------------------------------------------------------------*/
860 symHasReg (symbol * sym, regs * reg)
864 for (i = 0; i < sym->nRegs; i++)
865 if (sym->regs[i] == reg)
871 /*-----------------------------------------------------------------*/
872 /* deassignLRs - check the live to and if they have registers & are */
873 /* not spilt then free up the registers */
874 /*-----------------------------------------------------------------*/
876 deassignLRs (iCode * ic, eBBlock * ebp)
882 for (sym = hTabFirstItem (liveRanges, &k); sym;
883 sym = hTabNextItem (liveRanges, &k))
887 /* if it does not end here */
888 if (sym->liveTo > ic->seq)
891 /* if it was spilt on stack then we can
892 mark the stack spil location as free */
897 sym->usl.spillLoc->isFree = 1;
903 if (!bitVectBitValue (_G.regAssigned, sym->key))
906 /* special case check if this is an IFX &
907 the privious one was a pop and the
908 previous one was not spilt then keep track
910 if (ic->op == IFX && ic->prev &&
911 ic->prev->op == IPOP &&
912 !ic->prev->parmPush &&
913 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
914 psym = OP_SYMBOL (IC_LEFT (ic->prev));
920 bitVectUnSetBit (_G.regAssigned, sym->key);
922 /* if the result of this one needs registers
923 and does not have it then assign it right
925 if (IC_RESULT (ic) &&
926 !(SKIP_IC2 (ic) || /* not a special icode */
927 ic->op == JUMPTABLE ||
933 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
934 result->liveTo > ic->seq && /* and will live beyond this */
935 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
936 result->regType == sym->regType && /* same register types */
937 result->nRegs && /* which needs registers */
938 !result->isspilt && /* and does not already have them */
940 !bitVectBitValue (_G.regAssigned, result->key) &&
941 /* the number of free regs + number of regs in this LR
942 can accomodate the what result Needs */
943 ((nfreeRegsType (result->regType) +
944 sym->nRegs) >= result->nRegs)
948 for (i = 0; i < result->nRegs; i++)
950 result->regs[i] = sym->regs[i];
952 result->regs[i] = getRegGpr (ic, ebp, result);
954 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
958 /* free the remaining */
959 for (; i < sym->nRegs; i++)
963 if (!symHasReg (psym, sym->regs[i]))
964 freeReg (sym->regs[i]);
967 freeReg (sym->regs[i]);
974 /*-----------------------------------------------------------------*/
975 /* reassignLR - reassign this to registers */
976 /*-----------------------------------------------------------------*/
978 reassignLR (operand * op)
980 symbol *sym = OP_SYMBOL (op);
983 /* not spilt any more */
984 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
985 bitVectUnSetBit (_G.spiltSet, sym->key);
987 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
991 for (i = 0; i < sym->nRegs; i++)
992 sym->regs[i]->isFree = 0;
995 /*-----------------------------------------------------------------*/
996 /* willCauseSpill - determines if allocating will cause a spill */
997 /*-----------------------------------------------------------------*/
999 willCauseSpill (int nr, int rt)
1001 /* first check if there are any avlb registers
1002 of te type required */
1005 /* special case for pointer type
1006 if pointer type not avlb then
1007 check for type gpr */
1008 if (nFreeRegs (rt) >= nr)
1010 if (nFreeRegs (REG_GPR) >= nr)
1015 if (mcs51_ptrRegReq)
1017 if (nFreeRegs (rt) >= nr)
1022 if (nFreeRegs (REG_PTR) +
1023 nFreeRegs (REG_GPR) >= nr)
1028 /* it will cause a spil */
1032 /*-----------------------------------------------------------------*/
1033 /* positionRegs - the allocator can allocate same registers to res- */
1034 /* ult and operand, if this happens make sure they are in the same */
1035 /* position as the operand otherwise chaos results */
1036 /*-----------------------------------------------------------------*/
1038 positionRegs (symbol * result, symbol * opsym, int lineno)
1040 int count = min (result->nRegs, opsym->nRegs);
1041 int i, j = 0, shared = 0;
1043 /* if the result has been spilt then cannot share */
1048 /* first make sure that they actually share */
1049 for (i = 0; i < count; i++)
1051 for (j = 0; j < count; j++)
1053 if (result->regs[i] == opsym->regs[j] && i != j)
1063 regs *tmp = result->regs[i];
1064 result->regs[i] = result->regs[j];
1065 result->regs[j] = tmp;
1070 /*-----------------------------------------------------------------*/
1071 /* serialRegAssign - serially allocate registers to the variables */
1072 /*-----------------------------------------------------------------*/
1074 serialRegAssign (eBBlock ** ebbs, int count)
1078 /* for all blocks */
1079 for (i = 0; i < count; i++) {
1083 if (ebbs[i]->noPath &&
1084 (ebbs[i]->entryLabel != entryLabel &&
1085 ebbs[i]->entryLabel != returnLabel))
1088 /* of all instructions do */
1089 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1091 /* if this is an ipop that means some live
1092 range will have to be assigned again */
1094 reassignLR (IC_LEFT (ic));
1096 /* if result is present && is a true symbol */
1097 if (IC_RESULT (ic) && ic->op != IFX &&
1098 IS_TRUE_SYMOP (IC_RESULT (ic)))
1099 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1101 /* take away registers from live
1102 ranges that end at this instruction */
1103 deassignLRs (ic, ebbs[i]);
1105 /* some don't need registers */
1106 if (SKIP_IC2 (ic) ||
1107 ic->op == JUMPTABLE ||
1111 (IC_RESULT (ic) && POINTER_SET (ic)))
1114 /* now we need to allocate registers
1115 only for the result */
1116 if (IC_RESULT (ic)) {
1117 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1123 /* if it does not need or is spilt
1124 or is already assigned to registers
1125 or will not live beyond this instructions */
1128 bitVectBitValue (_G.regAssigned, sym->key) ||
1129 sym->liveTo <= ic->seq)
1132 /* if some liverange has been spilt at the block level
1133 and this one live beyond this block then spil this
1135 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1139 /* if trying to allocate this will cause
1140 a spill and there is nothing to spill
1141 or this one is rematerializable then
1143 willCS = willCauseSpill (sym->nRegs, sym->regType);
1144 spillable = computeSpillable (ic);
1145 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1150 /* if it has a spillocation & is used less than
1151 all other live ranges then spill this */
1153 if (sym->usl.spillLoc) {
1154 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1155 allLRs, ebbs[i], ic));
1156 if (leastUsed && leastUsed->used > sym->used) {
1161 /* if none of the liveRanges have a spillLocation then better
1162 to spill this one than anything else already assigned to registers */
1163 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
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 falied which means
1186 this was spilt then break */
1191 /* if it shares registers with operands make sure
1192 that they are in the same position */
1193 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1194 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1195 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1196 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1198 /* do the same for the right operand */
1199 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1200 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1201 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1202 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1215 /*-----------------------------------------------------------------*/
1216 /* rUmaskForOp :- returns register mask for an operand */
1217 /*-----------------------------------------------------------------*/
1219 rUmaskForOp (operand * op)
1225 /* only temporaries are assigned registers */
1229 sym = OP_SYMBOL (op);
1231 /* if spilt or no registers assigned to it
1233 if (sym->isspilt || !sym->nRegs)
1236 rumask = newBitVect (mcs51_nRegs);
1238 for (j = 0; j < sym->nRegs; j++)
1240 rumask = bitVectSetBit (rumask,
1241 sym->regs[j]->rIdx);
1247 /*-----------------------------------------------------------------*/
1248 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1249 /*-----------------------------------------------------------------*/
1251 regsUsedIniCode (iCode * ic)
1253 bitVect *rmask = newBitVect (mcs51_nRegs);
1255 /* do the special cases first */
1258 rmask = bitVectUnion (rmask,
1259 rUmaskForOp (IC_COND (ic)));
1263 /* for the jumptable */
1264 if (ic->op == JUMPTABLE)
1266 rmask = bitVectUnion (rmask,
1267 rUmaskForOp (IC_JTCOND (ic)));
1272 /* of all other cases */
1274 rmask = bitVectUnion (rmask,
1275 rUmaskForOp (IC_LEFT (ic)));
1279 rmask = bitVectUnion (rmask,
1280 rUmaskForOp (IC_RIGHT (ic)));
1283 rmask = bitVectUnion (rmask,
1284 rUmaskForOp (IC_RESULT (ic)));
1290 /*-----------------------------------------------------------------*/
1291 /* createRegMask - for each instruction will determine the regsUsed */
1292 /*-----------------------------------------------------------------*/
1294 createRegMask (eBBlock ** ebbs, int count)
1298 /* for all blocks */
1299 for (i = 0; i < count; i++)
1303 if (ebbs[i]->noPath &&
1304 (ebbs[i]->entryLabel != entryLabel &&
1305 ebbs[i]->entryLabel != returnLabel))
1308 /* for all instructions */
1309 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1314 if (SKIP_IC2 (ic) || !ic->rlive)
1317 /* first mark the registers used in this
1319 ic->rUsed = regsUsedIniCode (ic);
1320 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1322 /* now create the register mask for those
1323 registers that are in use : this is a
1324 super set of ic->rUsed */
1325 ic->rMask = newBitVect (mcs51_nRegs + 1);
1327 /* for all live Ranges alive at this point */
1328 for (j = 1; j < ic->rlive->size; j++)
1333 /* if not alive then continue */
1334 if (!bitVectBitValue (ic->rlive, j))
1337 /* find the live range we are interested in */
1338 if (!(sym = hTabItemWithKey (liveRanges, j)))
1340 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1341 "createRegMask cannot find live range");
1345 /* if no register assigned to it */
1346 if (!sym->nRegs || sym->isspilt)
1349 /* for all the registers allocated to it */
1350 for (k = 0; k < sym->nRegs; k++)
1353 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1359 /*-----------------------------------------------------------------*/
1360 /* rematStr - returns the rematerialized string for a remat var */
1361 /*-----------------------------------------------------------------*/
1363 rematStr (symbol * sym)
1366 iCode *ic = sym->rematiCode;
1371 /* if plus or minus print the right hand side */
1372 if (ic->op == '+' || ic->op == '-')
1374 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1377 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1381 /* we reached the end */
1382 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1389 /*-----------------------------------------------------------------*/
1390 /* regTypeNum - computes the type & number of registers required */
1391 /*-----------------------------------------------------------------*/
1399 /* for each live range do */
1400 for (sym = hTabFirstItem (liveRanges, &k); sym;
1401 sym = hTabNextItem (liveRanges, &k))
1404 /* if used zero times then no registers needed */
1405 if ((sym->liveTo - sym->liveFrom) == 0)
1409 /* if the live range is a temporary */
1413 /* if the type is marked as a conditional */
1414 if (sym->regType == REG_CND)
1417 /* if used in return only then we don't
1419 if (sym->ruonly || sym->accuse)
1421 if (IS_AGGREGATE (sym->type) || sym->isptr)
1422 sym->type = aggrToPtr (sym->type, FALSE);
1426 /* if the symbol has only one definition &
1427 that definition is a get_pointer and the
1428 pointer we are getting is rematerializable and
1431 if (bitVectnBitsOn (sym->defs) == 1 &&
1432 (ic = hTabItemWithKey (iCodehTab,
1433 bitVectFirstBit (sym->defs))) &&
1435 !IS_BITVAR (sym->etype))
1439 /* if remat in data space */
1440 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1441 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1444 /* create a psuedo symbol & force a spil */
1445 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1446 psym->type = sym->type;
1447 psym->etype = sym->etype;
1448 strcpy (psym->rname, psym->name);
1450 sym->usl.spillLoc = psym;
1454 /* if in data space or idata space then try to
1455 allocate pointer register */
1459 /* if not then we require registers */
1460 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1461 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1462 getSize (sym->type));
1466 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1467 printTypeChain (sym->type, stderr);
1468 fprintf (stderr, "\n");
1471 /* determine the type of register required */
1472 if (sym->nRegs == 1 &&
1473 IS_PTR (sym->type) &&
1475 sym->regType = REG_PTR;
1477 sym->regType = REG_GPR;
1481 /* for the first run we don't provide */
1482 /* registers for true symbols we will */
1483 /* see how things go */
1489 /*-----------------------------------------------------------------*/
1490 /* freeAllRegs - mark all registers as free */
1491 /*-----------------------------------------------------------------*/
1497 for (i = 0; i < mcs51_nRegs; i++)
1498 regs8051[i].isFree = 1;
1501 /*-----------------------------------------------------------------*/
1502 /* deallocStackSpil - this will set the stack pointer back */
1503 /*-----------------------------------------------------------------*/
1505 DEFSETFUNC (deallocStackSpil)
1513 /*-----------------------------------------------------------------*/
1514 /* farSpacePackable - returns the packable icode for far variables */
1515 /*-----------------------------------------------------------------*/
1517 farSpacePackable (iCode * ic)
1521 /* go thru till we find a definition for the
1522 symbol on the right */
1523 for (dic = ic->prev; dic; dic = dic->prev)
1525 /* if the definition is a call then no */
1526 if ((dic->op == CALL || dic->op == PCALL) &&
1527 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1532 /* if shift by unknown amount then not */
1533 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1534 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1537 /* if pointer get and size > 1 */
1538 if (POINTER_GET (dic) &&
1539 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1542 if (POINTER_SET (dic) &&
1543 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1546 /* if any three is a true symbol in far space */
1547 if (IC_RESULT (dic) &&
1548 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1549 isOperandInFarSpace (IC_RESULT (dic)))
1552 if (IC_RIGHT (dic) &&
1553 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1554 isOperandInFarSpace (IC_RIGHT (dic)) &&
1555 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1558 if (IC_LEFT (dic) &&
1559 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1560 isOperandInFarSpace (IC_LEFT (dic)) &&
1561 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1564 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1566 if ((dic->op == LEFT_OP ||
1567 dic->op == RIGHT_OP ||
1569 IS_OP_LITERAL (IC_RIGHT (dic)))
1579 /*-----------------------------------------------------------------*/
1580 /* packRegsForAssign - register reduction for assignment */
1581 /*-----------------------------------------------------------------*/
1583 packRegsForAssign (iCode * ic, eBBlock * ebp)
1586 sym_link *etype = operandType (IC_RIGHT (ic));
1588 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1589 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1590 OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1591 IS_BITFIELD (etype))
1596 /* if the true symbol is defined in far space or on stack
1597 then we should not since this will increase register pressure */
1599 if (isOperandInFarSpace (IC_RESULT (ic)))
1601 if ((dic = farSpacePackable (ic)))
1607 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1612 /* find the definition of iTempNN scanning backwards if we find a
1613 a use of the true symbol in before we find the definition then
1615 for (dic = ic->prev; dic; dic = dic->prev)
1617 #if 0 // jwk 20010410
1618 /* if there is a function call and this is
1619 a parameter & not my parameter then don't pack it */
1620 if ((dic->op == CALL || dic->op == PCALL) &&
1621 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1622 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1628 /* if there is a function call then don't pack it */
1629 if ((dic->op == CALL || dic->op == PCALL))
1639 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1640 IS_OP_VOLATILE (IC_RESULT (dic)))
1646 if (IS_SYMOP (IC_RESULT (dic)) &&
1647 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1649 if (POINTER_SET (dic))
1655 if (IS_SYMOP (IC_RIGHT (dic)) &&
1656 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1657 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1663 if (IS_SYMOP (IC_LEFT (dic)) &&
1664 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1665 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1671 if (POINTER_SET (dic) &&
1672 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1680 return 0; /* did not find */
1682 /* if assignment then check that right is not a bit */
1683 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1685 sym_link *etype = operandType (IC_RIGHT (dic));
1686 if (IS_BITFIELD (etype))
1689 /* if the result is on stack or iaccess then it must be
1690 the same atleast one of the operands */
1691 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1692 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1695 /* the operation has only one symbol
1696 operator then we can pack */
1697 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1698 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1701 if (!((IC_LEFT (dic) &&
1702 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1704 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1708 /* found the definition */
1709 /* replace the result with the result of */
1710 /* this assignment and remove this assignment */
1711 IC_RESULT (dic) = IC_RESULT (ic);
1713 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1715 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1717 /* delete from liverange table also
1718 delete from all the points inbetween and the new
1720 for (sic = dic; sic != ic; sic = sic->next)
1722 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1723 if (IS_ITEMP (IC_RESULT (dic)))
1724 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1727 remiCodeFromeBBlock (ebp, ic);
1728 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1729 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1734 /*-----------------------------------------------------------------*/
1735 /* findAssignToSym : scanning backwards looks for first assig found */
1736 /*-----------------------------------------------------------------*/
1738 findAssignToSym (operand * op, iCode * ic)
1742 for (dic = ic->prev; dic; dic = dic->prev)
1745 /* if definition by assignment */
1746 if (dic->op == '=' &&
1747 !POINTER_SET (dic) &&
1748 IC_RESULT (dic)->key == op->key
1749 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1753 /* we are interested only if defined in far space */
1754 /* or in stack space in case of + & - */
1756 /* if assigned to a non-symbol then return
1758 if (!IS_SYMOP (IC_RIGHT (dic)))
1761 /* if the symbol is in far space then
1763 if (isOperandInFarSpace (IC_RIGHT (dic)))
1766 /* for + & - operations make sure that
1767 if it is on the stack it is the same
1768 as one of the three operands */
1769 if ((ic->op == '+' || ic->op == '-') &&
1770 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1773 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1774 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1775 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1783 /* if we find an usage then we cannot delete it */
1784 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1787 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1790 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1794 /* now make sure that the right side of dic
1795 is not defined between ic & dic */
1798 iCode *sic = dic->next;
1800 for (; sic != ic; sic = sic->next)
1801 if (IC_RESULT (sic) &&
1802 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1811 /*-----------------------------------------------------------------*/
1812 /* packRegsForSupport :- reduce some registers for support calls */
1813 /*-----------------------------------------------------------------*/
1815 packRegsForSupport (iCode * ic, eBBlock * ebp)
1818 /* for the left & right operand :- look to see if the
1819 left was assigned a true symbol in far space in that
1820 case replace them */
1822 if (IS_ITEMP (IC_LEFT (ic)) &&
1823 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1825 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1831 /* found it we need to remove it from the
1833 for (sic = dic; sic != ic; sic = sic->next)
1834 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1836 IC_LEFT (ic)->operand.symOperand =
1837 IC_RIGHT (dic)->operand.symOperand;
1838 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1839 remiCodeFromeBBlock (ebp, dic);
1840 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1844 /* do the same for the right operand */
1847 IS_ITEMP (IC_RIGHT (ic)) &&
1848 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1850 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1856 /* if this is a subtraction & the result
1857 is a true symbol in far space then don't pack */
1858 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1860 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1861 if (IN_FARSPACE (SPEC_OCLS (etype)))
1864 /* found it we need to remove it from the
1866 for (sic = dic; sic != ic; sic = sic->next)
1867 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1869 IC_RIGHT (ic)->operand.symOperand =
1870 IC_RIGHT (dic)->operand.symOperand;
1871 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1873 remiCodeFromeBBlock (ebp, dic);
1874 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1881 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1884 /*-----------------------------------------------------------------*/
1885 /* packRegsForOneuse : - will reduce some registers for single Use */
1886 /*-----------------------------------------------------------------*/
1888 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1893 /* if returning a literal then do nothing */
1897 /* only upto 2 bytes since we cannot predict
1898 the usage of b, & acc */
1899 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2) &&
1902 !POINTER_SET (ic) &&
1906 /* this routine will mark the a symbol as used in one
1907 instruction use only && if the defintion is local
1908 (ie. within the basic block) && has only one definition &&
1909 that definiion is either a return value from a
1910 function or does not contain any variables in
1912 uses = bitVectCopy (OP_USES (op));
1913 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1914 if (!bitVectIsZero (uses)) /* has other uses */
1917 /* if it has only one defintion */
1918 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1919 return NULL; /* has more than one definition */
1921 /* get that definition */
1923 hTabItemWithKey (iCodehTab,
1924 bitVectFirstBit (OP_DEFS (op)))))
1927 /* found the definition now check if it is local */
1928 if (dic->seq < ebp->fSeq ||
1929 dic->seq > ebp->lSeq)
1930 return NULL; /* non-local */
1932 /* now check if it is the return from
1934 if (dic->op == CALL || dic->op == PCALL)
1936 if (ic->op != SEND && ic->op != RETURN)
1938 OP_SYMBOL (op)->ruonly = 1;
1945 /* otherwise check that the definition does
1946 not contain any symbols in far space */
1947 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1948 isOperandInFarSpace (IC_RIGHT (dic)) ||
1949 IS_OP_RUONLY (IC_LEFT (ic)) ||
1950 IS_OP_RUONLY (IC_RIGHT (ic)))
1955 /* if pointer set then make sure the pointer
1957 if (POINTER_SET (dic) &&
1958 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1961 if (POINTER_GET (dic) &&
1962 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1967 /* also make sure the intervenening instructions
1968 don't have any thing in far space */
1969 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
1972 /* if there is an intervening function call then no */
1973 if (dic->op == CALL || dic->op == PCALL)
1975 /* if pointer set then make sure the pointer
1977 if (POINTER_SET (dic) &&
1978 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1981 if (POINTER_GET (dic) &&
1982 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1985 /* if address of & the result is remat the okay */
1986 if (dic->op == ADDRESS_OF &&
1987 OP_SYMBOL (IC_RESULT (dic))->remat)
1990 /* if operand has size of three or more & this
1991 operation is a '*','/' or '%' then 'b' may
1993 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1994 getSize (operandType (op)) >= 3)
1997 /* if left or right or result is in far space */
1998 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1999 isOperandInFarSpace (IC_RIGHT (dic)) ||
2000 isOperandInFarSpace (IC_RESULT (dic)) ||
2001 IS_OP_RUONLY (IC_LEFT (dic)) ||
2002 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2003 IS_OP_RUONLY (IC_RESULT (dic)))
2007 /* if left or right or result is on stack */
2008 if (isOperandOnStack(IC_LEFT(dic)) ||
2009 isOperandOnStack(IC_RIGHT(dic)) ||
2010 isOperandOnStack(IC_RESULT(dic))) {
2015 OP_SYMBOL (op)->ruonly = 1;
2020 /*-----------------------------------------------------------------*/
2021 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2022 /*-----------------------------------------------------------------*/
2024 isBitwiseOptimizable (iCode * ic)
2026 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2027 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2029 /* bitwise operations are considered optimizable
2030 under the following conditions (Jean-Louis VERN)
2042 if (IS_LITERAL(rtype) ||
2043 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2049 /*-----------------------------------------------------------------*/
2050 /* packRegsForAccUse - pack registers for acc use */
2051 /*-----------------------------------------------------------------*/
2053 packRegsForAccUse (iCode * ic)
2057 /* if + or - then it has to be one byte result */
2058 if ((ic->op == '+' || ic->op == '-')
2059 && getSize (operandType (IC_RESULT (ic))) > 1)
2062 /* if shift operation make sure right side is not a literal */
2063 if (ic->op == RIGHT_OP &&
2064 (isOperandLiteral (IC_RIGHT (ic)) ||
2065 getSize (operandType (IC_RESULT (ic))) > 1))
2068 if (ic->op == LEFT_OP &&
2069 (isOperandLiteral (IC_RIGHT (ic)) ||
2070 getSize (operandType (IC_RESULT (ic))) > 1))
2073 if (IS_BITWISE_OP (ic) &&
2074 getSize (operandType (IC_RESULT (ic))) > 1)
2078 /* has only one definition */
2079 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2082 /* has only one use */
2083 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2086 /* and the usage immediately follows this iCode */
2087 if (!(uic = hTabItemWithKey (iCodehTab,
2088 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2091 if (ic->next != uic)
2094 /* if it is a conditional branch then we definitely can */
2098 if (uic->op == JUMPTABLE)
2101 /* if the usage is not is an assignment
2102 or an arithmetic / bitwise / shift operation then not */
2103 if (POINTER_SET (uic) &&
2104 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2107 if (uic->op != '=' &&
2108 !IS_ARITHMETIC_OP (uic) &&
2109 !IS_BITWISE_OP (uic) &&
2110 uic->op != LEFT_OP &&
2111 uic->op != RIGHT_OP)
2114 /* if used in ^ operation then make sure right is not a
2116 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2119 /* if shift operation make sure right side is not a literal */
2120 if (uic->op == RIGHT_OP &&
2121 (isOperandLiteral (IC_RIGHT (uic)) ||
2122 getSize (operandType (IC_RESULT (uic))) > 1))
2125 if (uic->op == LEFT_OP &&
2126 (isOperandLiteral (IC_RIGHT (uic)) ||
2127 getSize (operandType (IC_RESULT (uic))) > 1))
2130 /* make sure that the result of this icode is not on the
2131 stack, since acc is used to compute stack offset */
2132 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2133 OP_SYMBOL (IC_RESULT (uic))->onStack)
2136 /* if either one of them in far space then we cannot */
2137 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2138 isOperandInFarSpace (IC_LEFT (uic))) ||
2139 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2140 isOperandInFarSpace (IC_RIGHT (uic))))
2143 /* if the usage has only one operand then we can */
2144 if (IC_LEFT (uic) == NULL ||
2145 IC_RIGHT (uic) == NULL)
2148 /* make sure this is on the left side if not
2149 a '+' since '+' is commutative */
2150 if (ic->op != '+' &&
2151 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2154 /* if one of them is a literal then we can */
2155 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2156 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2158 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2162 /* if the other one is not on stack then we can */
2163 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2164 (IS_ITEMP (IC_RIGHT (uic)) ||
2165 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2166 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2169 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2170 (IS_ITEMP (IC_LEFT (uic)) ||
2171 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2172 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2178 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2183 /*-----------------------------------------------------------------*/
2184 /* packForPush - hueristics to reduce iCode for pushing */
2185 /*-----------------------------------------------------------------*/
2187 packForPush (iCode * ic, eBBlock * ebp)
2192 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2195 /* must have only definition & one usage */
2196 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2197 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2200 /* find the definition */
2201 if (!(dic = hTabItemWithKey (iCodehTab,
2202 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2205 if (dic->op != '=' || POINTER_SET (dic))
2208 /* make sure the right side does not have any definitions
2210 dbv = OP_DEFS(IC_RIGHT(dic));
2211 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2212 if (bitVectBitValue(dbv,lic->key))
2215 /* we now we know that it has one & only one def & use
2216 and the that the definition is an assignment */
2217 IC_LEFT (ic) = IC_RIGHT (dic);
2219 remiCodeFromeBBlock (ebp, dic);
2220 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2223 /*-----------------------------------------------------------------*/
2224 /* packRegisters - does some transformations to reduce register */
2226 /*-----------------------------------------------------------------*/
2228 packRegisters (eBBlock * ebp)
2238 /* look for assignments of the form */
2239 /* iTempNN = TRueSym (someoperation) SomeOperand */
2241 /* TrueSym := iTempNN:1 */
2242 for (ic = ebp->sch; ic; ic = ic->next)
2244 /* find assignment of the form TrueSym := iTempNN:1 */
2245 if (ic->op == '=' && !POINTER_SET (ic))
2246 change += packRegsForAssign (ic, ebp);
2253 for (ic = ebp->sch; ic; ic = ic->next)
2255 /* if this is an itemp & result of a address of a true sym
2256 then mark this as rematerialisable */
2257 if (ic->op == ADDRESS_OF &&
2258 IS_ITEMP (IC_RESULT (ic)) &&
2259 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2260 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2261 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2264 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2265 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2266 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2270 /* if straight assignment then carry remat flag if
2271 this is the only definition */
2272 if (ic->op == '=' &&
2273 !POINTER_SET (ic) &&
2274 IS_SYMOP (IC_RIGHT (ic)) &&
2275 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2276 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2279 OP_SYMBOL (IC_RESULT (ic))->remat =
2280 OP_SYMBOL (IC_RIGHT (ic))->remat;
2281 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2282 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2285 /* if this is a +/- operation with a rematerizable
2286 then mark this as rematerializable as well */
2287 if ((ic->op == '+' || ic->op == '-') &&
2288 (IS_SYMOP (IC_LEFT (ic)) &&
2289 IS_ITEMP (IC_RESULT (ic)) &&
2290 OP_SYMBOL (IC_LEFT (ic))->remat &&
2291 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2292 IS_OP_LITERAL (IC_RIGHT (ic))))
2294 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2295 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2296 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2299 /* mark the pointer usages */
2300 if (POINTER_SET (ic))
2301 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2303 if (POINTER_GET (ic))
2304 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2308 /* if we are using a symbol on the stack
2309 then we should say mcs51_ptrRegReq */
2310 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2311 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2312 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2313 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2314 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2315 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2318 if (IS_SYMOP (IC_LEFT (ic)))
2319 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2320 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2321 if (IS_SYMOP (IC_RIGHT (ic)))
2322 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2323 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2324 if (IS_SYMOP (IC_RESULT (ic)))
2325 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2326 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2331 /* if the condition of an if instruction
2332 is defined in the previous instruction then
2333 mark the itemp as a conditional */
2334 if ((IS_CONDITIONAL (ic) ||
2335 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2336 ic->next && ic->next->op == IFX &&
2337 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2338 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2340 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2344 /* if the condition of an if instruction
2345 is defined in the previous instruction and
2346 this is the only usage then
2347 mark the itemp as a conditional */
2348 if ((IS_CONDITIONAL (ic) ||
2349 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2350 ic->next && ic->next->op == IFX &&
2351 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2352 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2353 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2355 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2360 /* reduce for support function calls */
2361 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2362 packRegsForSupport (ic, ebp);
2364 /* some cases the redundant moves can
2365 can be eliminated for return statements */
2366 if ((ic->op == RETURN || ic->op == SEND) &&
2367 !isOperandInFarSpace (IC_LEFT (ic)) &&
2368 options.model == MODEL_SMALL) {
2369 if (0 && options.stackAuto) {
2370 /* we should check here if acc will be clobbered for stack
2371 offset calculations */
2373 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2377 /* if pointer set & left has a size more than
2378 one and right is not in far space */
2379 if (POINTER_SET (ic) &&
2380 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2381 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2382 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2383 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2385 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2387 /* if pointer get */
2388 if (POINTER_GET (ic) &&
2389 !isOperandInFarSpace (IC_RESULT (ic)) &&
2390 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2391 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2392 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2394 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2397 /* if this is cast for intergral promotion then
2398 check if only use of the definition of the
2399 operand being casted/ if yes then replace
2400 the result of that arithmetic operation with
2401 this result and get rid of the cast */
2404 sym_link *fromType = operandType (IC_RIGHT (ic));
2405 sym_link *toType = operandType (IC_LEFT (ic));
2407 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2408 getSize (fromType) != getSize (toType) &&
2409 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2412 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2415 if (IS_ARITHMETIC_OP (dic))
2417 IC_RESULT (dic) = IC_RESULT (ic);
2418 remiCodeFromeBBlock (ebp, ic);
2419 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2420 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2424 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2430 /* if the type from and type to are the same
2431 then if this is the only use then packit */
2432 if (checkType (operandType (IC_RIGHT (ic)),
2433 operandType (IC_LEFT (ic))) == 1)
2435 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2438 IC_RESULT (dic) = IC_RESULT (ic);
2439 remiCodeFromeBBlock (ebp, ic);
2440 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2441 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2449 iTempNN := (some variable in farspace) V1
2454 if (ic->op == IPUSH)
2456 packForPush (ic, ebp);
2460 /* pack registers for accumulator use, when the
2461 result of an arithmetic or bit wise operation
2462 has only one use, that use is immediately following
2463 the defintion and the using iCode has only one
2464 operand or has two operands but one is literal &
2465 the result of that operation is not on stack then
2466 we can leave the result of this operation in acc:b
2468 if ((IS_ARITHMETIC_OP (ic)
2469 || IS_BITWISE_OP (ic)
2470 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2471 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2473 IS_ITEMP (IC_RESULT (ic)) &&
2474 getSize (operandType (IC_RESULT (ic))) <= 2)
2476 packRegsForAccUse (ic);
2480 /*-----------------------------------------------------------------*/
2481 /* assignRegisters - assigns registers to each live range as need */
2482 /*-----------------------------------------------------------------*/
2484 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2489 setToNull ((void *) &_G.funcrUsed);
2490 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2491 /* if not register extentions then reduce number
2493 if (options.regExtend)
2498 /* change assignments this will remove some
2499 live ranges reducing some register pressure */
2500 for (i = 0; i < count; i++)
2501 packRegisters (ebbs[i]);
2503 if (options.dump_pack)
2504 dumpEbbsToFileExt (".dumppack", ebbs, count);
2506 /* first determine for each live range the number of
2507 registers & the type of registers required for each */
2510 /* and serially allocate registers */
2511 serialRegAssign (ebbs, count);
2513 /* if stack was extended then tell the user */
2516 /* werror(W_TOOMANY_SPILS,"stack", */
2517 /* _G.stackExtend,currFunc->name,""); */
2523 /* werror(W_TOOMANY_SPILS,"data space", */
2524 /* _G.dataExtend,currFunc->name,""); */
2528 /* after that create the register mask
2529 for each of the instruction */
2530 createRegMask (ebbs, count);
2532 /* redo that offsets for stacked automatic variables */
2533 redoStackOffsets ();
2535 if (options.dump_rassgn)
2537 dumpEbbsToFileExt (".dumprassgn", ebbs, count);
2538 dumpLiveRanges (".dumplrange", liveRanges);
2541 /* do the overlaysegment stuff SDCCmem.c */
2542 doOverlays (ebbs, count);
2544 /* now get back the chain */
2545 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2550 /* free up any _G.stackSpil locations allocated */
2551 applyToSet (_G.stackSpil, deallocStackSpil);
2553 setToNull ((void **) &_G.stackSpil);
2554 setToNull ((void **) &_G.spiltSet);
2555 /* mark all registers as free */