1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
39 extern void gen51Code (iCode *);
48 bitVect *totRegAssigned; /* final set of LRs that got into registers */
51 bitVect *funcrUsed; /* registers used in a function */
57 /* Shared with gen.c */
58 int mcs51_ptrRegReq; /* one byte pointer register required */
64 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
65 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
66 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
67 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
68 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
69 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
70 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
71 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
72 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
73 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
74 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
75 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
76 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
77 {REG_CND, CND_IDX, REG_CND, "C", "psw", "0xd0", 0, 1},
78 {0, DPL_IDX, 0, "dpl", "dpl", "0x82", 0, 0},
79 {0, DPH_IDX, 0, "dph", "dph", "0x83", 0, 0},
80 {0, B_IDX, 0, "b", "b", "0xf0", 0, 0},
81 {0, A_IDX, 0, "a", "acc", "0xe0", 0, 0},
84 static void spillThis (symbol *);
85 static void freeAllRegs ();
87 /*-----------------------------------------------------------------*/
88 /* allocReg - allocates register of given type */
89 /*-----------------------------------------------------------------*/
95 for (i = 0; i < mcs51_nRegs; i++)
98 /* if type is given as 0 then any
99 free register will do */
103 regs8051[i].isFree = 0;
106 bitVectSetBit (currFunc->regsUsed, i);
109 /* other wise look for specific type
111 if (regs8051[i].isFree &&
112 regs8051[i].type == type)
114 regs8051[i].isFree = 0;
117 bitVectSetBit (currFunc->regsUsed, i);
124 /*-----------------------------------------------------------------*/
125 /* allocThisReg - allocates a particular register (if free) */
126 /*-----------------------------------------------------------------*/
128 allocThisReg (regs * reg)
135 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, reg->rIdx);
141 /*-----------------------------------------------------------------*/
142 /* mcs51_regWithIdx - returns pointer to register with index number*/
143 /*-----------------------------------------------------------------*/
145 mcs51_regWithIdx (int idx)
149 for (i = 0; i < sizeof(regs8051)/sizeof(regs); i++)
150 if (regs8051[i].rIdx == idx)
153 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
154 "regWithIdx not found");
158 /*-----------------------------------------------------------------*/
159 /* freeReg - frees a register */
160 /*-----------------------------------------------------------------*/
166 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
167 "freeReg - Freeing NULL register");
175 /*-----------------------------------------------------------------*/
176 /* nFreeRegs - returns number of free registers */
177 /*-----------------------------------------------------------------*/
184 for (i = 0; i < mcs51_nRegs; i++)
185 if (regs8051[i].isFree && regs8051[i].type == type)
190 /*-----------------------------------------------------------------*/
191 /* nfreeRegsType - free registers with type */
192 /*-----------------------------------------------------------------*/
194 nfreeRegsType (int type)
199 if ((nfr = nFreeRegs (type)) == 0)
200 return nFreeRegs (REG_GPR);
203 return nFreeRegs (type);
206 /*-----------------------------------------------------------------*/
207 /* useReg - marks a register as used */
208 /*-----------------------------------------------------------------*/
215 /*-----------------------------------------------------------------*/
216 /* computeSpillable - given a point find the spillable live ranges */
217 /*-----------------------------------------------------------------*/
219 computeSpillable (iCode * ic)
223 /* spillable live ranges are those that are live at this
224 point . the following categories need to be subtracted
226 a) - those that are already spilt
227 b) - if being used by this one
228 c) - defined by this one */
230 spillable = bitVectCopy (ic->rlive);
232 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
234 bitVectCplAnd (spillable, ic->uses); /* used in this one */
235 bitVectUnSetBit (spillable, ic->defKey);
236 spillable = bitVectIntersect (spillable, _G.regAssigned);
241 /*-----------------------------------------------------------------*/
242 /* noSpilLoc - return true if a variable has no spil location */
243 /*-----------------------------------------------------------------*/
245 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
247 return (sym->usl.spillLoc ? 0 : 1);
250 /*-----------------------------------------------------------------*/
251 /* hasSpilLoc - will return 1 if the symbol has spil location */
252 /*-----------------------------------------------------------------*/
254 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
256 return (sym->usl.spillLoc ? 1 : 0);
259 /*-----------------------------------------------------------------*/
260 /* directSpilLoc - will return 1 if the splilocation is in direct */
261 /*-----------------------------------------------------------------*/
263 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
265 if (sym->usl.spillLoc &&
266 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
272 /*-----------------------------------------------------------------*/
273 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
274 /* but is not used as a pointer */
275 /*-----------------------------------------------------------------*/
277 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
279 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
282 /*-----------------------------------------------------------------*/
283 /* rematable - will return 1 if the remat flag is set */
284 /*-----------------------------------------------------------------*/
286 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
291 /*-----------------------------------------------------------------*/
292 /* notUsedInRemaining - not used or defined in remain of the block */
293 /*-----------------------------------------------------------------*/
295 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
297 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
298 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
301 /*-----------------------------------------------------------------*/
302 /* allLRs - return true for all */
303 /*-----------------------------------------------------------------*/
305 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
310 /*-----------------------------------------------------------------*/
311 /* liveRangesWith - applies function to a given set of live range */
312 /*-----------------------------------------------------------------*/
314 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
315 eBBlock * ebp, iCode * ic)
320 if (!lrs || !lrs->size)
323 for (i = 1; i < lrs->size; i++)
326 if (!bitVectBitValue (lrs, i))
329 /* if we don't find it in the live range
330 hash table we are in serious trouble */
331 if (!(sym = hTabItemWithKey (liveRanges, i)))
333 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
334 "liveRangesWith could not find liveRange");
338 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
339 addSetHead (&rset, sym);
346 /*-----------------------------------------------------------------*/
347 /* leastUsedLR - given a set determines which is the least used */
348 /*-----------------------------------------------------------------*/
350 leastUsedLR (set * sset)
352 symbol *sym = NULL, *lsym = NULL;
354 sym = lsym = setFirstItem (sset);
359 for (; lsym; lsym = setNextItem (sset))
362 /* if usage is the same then prefer
363 the spill the smaller of the two */
364 if (lsym->used == sym->used)
365 if (getSize (lsym->type) < getSize (sym->type))
369 if (lsym->used < sym->used)
374 setToNull ((void *) &sset);
379 /*-----------------------------------------------------------------*/
380 /* noOverLap - will iterate through the list looking for over lap */
381 /*-----------------------------------------------------------------*/
383 noOverLap (set * itmpStack, symbol * fsym)
387 for (sym = setFirstItem (itmpStack); sym;
388 sym = setNextItem (itmpStack))
390 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
395 /*-----------------------------------------------------------------*/
396 /* isFree - will return 1 if the a free spil location is found */
397 /*-----------------------------------------------------------------*/
402 V_ARG (symbol **, sloc);
403 V_ARG (symbol *, fsym);
405 /* if already found */
409 /* if it is free && and the itmp assigned to
410 this does not have any overlapping live ranges
411 with the one currently being assigned and
412 the size can be accomodated */
414 noOverLap (sym->usl.itmpStack, fsym) &&
415 getSize (sym->type) >= getSize (fsym->type))
424 /*-----------------------------------------------------------------*/
425 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
426 /*-----------------------------------------------------------------*/
428 spillLRWithPtrReg (symbol * forSym)
434 if (!_G.regAssigned ||
435 bitVectIsZero (_G.regAssigned))
438 r0 = mcs51_regWithIdx (R0_IDX);
439 r1 = mcs51_regWithIdx (R1_IDX);
441 /* for all live ranges */
442 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
443 lrsym = hTabNextItem (liveRanges, &k))
447 /* if no registers assigned to it or spilt */
448 /* if it does not overlap this then
449 no need to spill it */
451 if (lrsym->isspilt || !lrsym->nRegs ||
452 (lrsym->liveTo < forSym->liveFrom))
455 /* go thru the registers : if it is either
456 r0 or r1 then spill it */
457 for (j = 0; j < lrsym->nRegs; j++)
458 if (lrsym->regs[j] == r0 ||
459 lrsym->regs[j] == r1)
468 /*-----------------------------------------------------------------*/
469 /* createStackSpil - create a location on the stack to spil */
470 /*-----------------------------------------------------------------*/
472 createStackSpil (symbol * sym)
475 int useXstack, model;
479 /* first go try and find a free one that is already
480 existing on the stack */
481 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
483 /* found a free one : just update & return */
484 sym->usl.spillLoc = sloc;
487 addSetHead (&sloc->usl.itmpStack, sym);
491 /* could not then have to create one , this is the hard part
492 we need to allocate this on the stack : this is really a
493 hack!! but cannot think of anything better at this time */
495 if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
496 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
498 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
503 sloc = newiTemp (slocBuffer);
505 /* set the type to the spilling symbol */
506 sloc->type = copyLinkChain (sym->type);
507 sloc->etype = getSpec (sloc->type);
508 if (SPEC_SCLS (sloc->etype) != S_BIT)
510 SPEC_SCLS (sloc->etype) = S_DATA;
512 SPEC_EXTR (sloc->etype) = 0;
513 SPEC_STAT (sloc->etype) = 0;
514 SPEC_VOLATILE(sloc->etype) = 0;
515 SPEC_ABSA(sloc->etype) = 0;
517 /* we don't allow it to be allocated`
518 onto the external stack since : so we
519 temporarily turn it off ; we also
520 turn off memory model to prevent
521 the spil from going to the external storage
524 useXstack = options.useXstack;
525 model = options.model;
526 /* noOverlay = options.noOverlay; */
527 /* options.noOverlay = 1; */
528 options.model = options.useXstack = 0;
532 options.useXstack = useXstack;
533 options.model = model;
534 /* options.noOverlay = noOverlay; */
535 sloc->isref = 1; /* to prevent compiler warning */
537 /* if it is on the stack then update the stack */
538 if (IN_STACK (sloc->etype))
540 currFunc->stack += getSize (sloc->type);
541 _G.stackExtend += getSize (sloc->type);
544 _G.dataExtend += getSize (sloc->type);
546 /* add it to the _G.stackSpil set */
547 addSetHead (&_G.stackSpil, sloc);
548 sym->usl.spillLoc = sloc;
551 /* add it to the set of itempStack set
552 of the spill location */
553 addSetHead (&sloc->usl.itmpStack, sym);
557 /*-----------------------------------------------------------------*/
558 /* isSpiltOnStack - returns true if the spil location is on stack */
559 /*-----------------------------------------------------------------*/
561 isSpiltOnStack (symbol * sym)
571 /* if (sym->_G.stackSpil) */
574 if (!sym->usl.spillLoc)
577 etype = getSpec (sym->usl.spillLoc->type);
578 if (IN_STACK (etype))
584 /*-----------------------------------------------------------------*/
585 /* spillThis - spils a specific operand */
586 /*-----------------------------------------------------------------*/
588 spillThis (symbol * sym)
591 /* if this is rematerializable or has a spillLocation
592 we are okay, else we need to create a spillLocation
594 if (!(sym->remat || sym->usl.spillLoc))
595 createStackSpil (sym);
597 /* mark it has spilt & put it in the spilt set */
598 sym->isspilt = sym->spillA = 1;
599 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
601 bitVectUnSetBit (_G.regAssigned, sym->key);
602 bitVectUnSetBit (_G.totRegAssigned, sym->key);
604 for (i = 0; i < sym->nRegs; i++)
608 freeReg (sym->regs[i]);
612 /* if spilt on stack then free up r0 & r1
613 if they could have been assigned to some
615 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
618 spillLRWithPtrReg (sym);
621 if (sym->usl.spillLoc && !sym->remat)
622 sym->usl.spillLoc->allocreq++;
626 /*-----------------------------------------------------------------*/
627 /* selectSpil - select a iTemp to spil : rather a simple procedure */
628 /*-----------------------------------------------------------------*/
630 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
632 bitVect *lrcs = NULL;
636 /* get the spillable live ranges */
637 lrcs = computeSpillable (ic);
639 /* get all live ranges that are rematerizable */
640 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
643 /* return the least used of these */
644 return leastUsedLR (selectS);
647 /* get live ranges with spillLocations in direct space */
648 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
650 sym = leastUsedLR (selectS);
651 strncpyz (sym->rname,
652 sym->usl.spillLoc->rname[0] ?
653 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
656 /* mark it as allocation required */
657 sym->usl.spillLoc->allocreq++;
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 */
682 !isiCodeInFunctionCall (ic) &&
683 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
685 sym = leastUsedLR (selectS);
698 /* find live ranges with spillocation && not used as pointers */
699 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
702 sym = leastUsedLR (selectS);
703 /* mark this as allocation required */
704 sym->usl.spillLoc->allocreq++;
708 /* find live ranges with spillocation */
709 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
712 sym = leastUsedLR (selectS);
713 sym->usl.spillLoc->allocreq++;
717 /* couldn't find then we need to create a spil
718 location on the stack , for which one? the least
720 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
723 /* return a created spil location */
724 sym = createStackSpil (leastUsedLR (selectS));
725 sym->usl.spillLoc->allocreq++;
729 /* this is an extreme situation we will spill
730 this one : happens very rarely but it does happen */
736 /*-----------------------------------------------------------------*/
737 /* spilSomething - spil some variable & mark registers as free */
738 /*-----------------------------------------------------------------*/
740 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
745 /* get something we can spil */
746 ssym = selectSpil (ic, ebp, forSym);
748 /* mark it as spilt */
749 ssym->isspilt = ssym->spillA = 1;
750 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
752 /* mark it as not register assigned &
753 take it away from the set */
754 bitVectUnSetBit (_G.regAssigned, ssym->key);
755 bitVectUnSetBit (_G.totRegAssigned, 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)
814 /* try for a ptr type */
815 if ((reg = allocReg (REG_PTR)))
818 /* try for gpr type */
819 if ((reg = allocReg (REG_GPR)))
822 /* we have to spil */
823 if (!spilSomething (ic, ebp, sym))
826 /* make sure partially assigned registers aren't reused */
827 for (j=0; j<=sym->nRegs; j++)
829 sym->regs[j]->isFree = 0;
831 /* this looks like an infinite loop but
832 in really selectSpil will abort */
836 /*-----------------------------------------------------------------*/
837 /* getRegGpr - will try for GPR if not spil */
838 /*-----------------------------------------------------------------*/
840 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
846 /* try for gpr type */
847 if ((reg = allocReg (REG_GPR)))
850 if (!mcs51_ptrRegReq)
851 if ((reg = allocReg (REG_PTR)))
854 /* we have to spil */
855 if (!spilSomething (ic, ebp, sym))
858 /* make sure partially assigned registers aren't reused */
859 for (j=0; j<=sym->nRegs; j++)
861 sym->regs[j]->isFree = 0;
863 /* this looks like an infinite loop but
864 in really selectSpil will abort */
868 /*-----------------------------------------------------------------*/
869 /* getRegPtrNoSpil - get it cannot be spilt */
870 /*-----------------------------------------------------------------*/
871 static regs *getRegPtrNoSpil()
875 /* try for a ptr type */
876 if ((reg = allocReg (REG_PTR)))
879 /* try for gpr type */
880 if ((reg = allocReg (REG_GPR)))
885 /* just to make the compiler happy */
889 /*-----------------------------------------------------------------*/
890 /* getRegGprNoSpil - get it cannot be spilt */
891 /*-----------------------------------------------------------------*/
892 static regs *getRegGprNoSpil()
896 if ((reg = allocReg (REG_GPR)))
899 if (!mcs51_ptrRegReq)
900 if ((reg = allocReg (REG_PTR)))
905 /* just to make the compiler happy */
909 /*-----------------------------------------------------------------*/
910 /* symHasReg - symbol has a given register */
911 /*-----------------------------------------------------------------*/
913 symHasReg (symbol * sym, regs * reg)
917 for (i = 0; i < sym->nRegs; i++)
918 if (sym->regs[i] == reg)
924 /*-----------------------------------------------------------------*/
925 /* deassignLRs - check the live to and if they have registers & are */
926 /* not spilt then free up the registers */
927 /*-----------------------------------------------------------------*/
929 deassignLRs (iCode * ic, eBBlock * ebp)
935 for (sym = hTabFirstItem (liveRanges, &k); sym;
936 sym = hTabNextItem (liveRanges, &k))
940 /* if it does not end here */
941 if (sym->liveTo > ic->seq)
944 /* if it was spilt on stack then we can
945 mark the stack spil location as free */
950 sym->usl.spillLoc->isFree = 1;
956 if (!bitVectBitValue (_G.regAssigned, sym->key))
959 /* special case check if this is an IFX &
960 the privious one was a pop and the
961 previous one was not spilt then keep track
963 if (ic->op == IFX && ic->prev &&
964 ic->prev->op == IPOP &&
965 !ic->prev->parmPush &&
966 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
967 psym = OP_SYMBOL (IC_LEFT (ic->prev));
973 bitVectUnSetBit (_G.regAssigned, sym->key);
975 /* if the result of this one needs registers
976 and does not have it then assign it right
978 if (IC_RESULT (ic) &&
979 !(SKIP_IC2 (ic) || /* not a special icode */
980 ic->op == JUMPTABLE ||
986 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
987 result->liveTo > ic->seq && /* and will live beyond this */
988 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
989 result->liveFrom == ic->seq && /* does not start before here */
990 result->regType == sym->regType && /* same register types */
991 result->nRegs && /* which needs registers */
992 !result->isspilt && /* and does not already have them */
994 !bitVectBitValue (_G.regAssigned, result->key) &&
995 /* the number of free regs + number of regs in this LR
996 can accomodate the what result Needs */
997 ((nfreeRegsType (result->regType) +
998 sym->nRegs) >= result->nRegs)
1002 for (i = 0; i < result->nRegs; i++)
1004 result->regs[i] = sym->regs[i];
1006 result->regs[i] = getRegGpr (ic, ebp, result);
1008 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1009 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1013 /* free the remaining */
1014 for (; i < sym->nRegs; i++)
1018 if (!symHasReg (psym, sym->regs[i]))
1019 freeReg (sym->regs[i]);
1022 freeReg (sym->regs[i]);
1029 /*-----------------------------------------------------------------*/
1030 /* reassignLR - reassign this to registers */
1031 /*-----------------------------------------------------------------*/
1033 reassignLR (operand * op)
1035 symbol *sym = OP_SYMBOL (op);
1038 /* not spilt any more */
1039 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1040 bitVectUnSetBit (_G.spiltSet, sym->key);
1042 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1043 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1047 for (i = 0; i < sym->nRegs; i++)
1048 sym->regs[i]->isFree = 0;
1051 /*-----------------------------------------------------------------*/
1052 /* willCauseSpill - determines if allocating will cause a spill */
1053 /*-----------------------------------------------------------------*/
1055 willCauseSpill (int nr, int rt)
1057 /* first check if there are any available registers
1058 of the type required */
1061 /* special case for pointer type
1062 if pointer type not avlb then
1063 check for type gpr */
1064 if (nFreeRegs (rt) >= nr)
1066 if (nFreeRegs (REG_GPR) >= nr)
1069 else if (rt == REG_BIT)
1071 if (nFreeRegs (rt) >= nr)
1076 if (mcs51_ptrRegReq)
1078 if (nFreeRegs (rt) >= nr)
1083 if (nFreeRegs (REG_PTR) +
1084 nFreeRegs (REG_GPR) >= nr)
1089 /* it will cause a spil */
1093 /*-----------------------------------------------------------------*/
1094 /* positionRegs - the allocator can allocate same registers to res- */
1095 /* ult and operand, if this happens make sure they are in the same */
1096 /* position as the operand otherwise chaos results */
1097 /*-----------------------------------------------------------------*/
1099 positionRegs (symbol * result, symbol * opsym)
1101 int count = min (result->nRegs, opsym->nRegs);
1102 int i, j = 0, shared = 0;
1105 /* if the result has been spilt then cannot share */
1110 /* first make sure that they actually share */
1111 for (i = 0; i < count; i++)
1113 for (j = 0; j < count; j++)
1115 if (result->regs[i] == opsym->regs[j] && i != j)
1125 regs *tmp = result->regs[i];
1126 result->regs[i] = result->regs[j];
1127 result->regs[j] = tmp;
1134 /*------------------------------------------------------------------*/
1135 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1136 /* it should either have registers or have beed spilled. Otherwise, */
1137 /* there was an uninitialized variable, so just spill this to get */
1138 /* the operand in a valid state. */
1139 /*------------------------------------------------------------------*/
1141 verifyRegsAssigned (operand *op, iCode * ic)
1146 if (!IS_ITEMP (op)) return;
1148 sym = OP_SYMBOL (op);
1149 if (sym->isspilt) return;
1150 if (!sym->nRegs) return;
1151 if (sym->regs[0]) return;
1153 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1154 sym->prereqv ? sym->prereqv->name : sym->name);
1159 /*-----------------------------------------------------------------*/
1160 /* serialRegAssign - serially allocate registers to the variables */
1161 /*-----------------------------------------------------------------*/
1163 serialRegAssign (eBBlock ** ebbs, int count)
1167 /* for all blocks */
1168 for (i = 0; i < count; i++)
1173 if (ebbs[i]->noPath &&
1174 (ebbs[i]->entryLabel != entryLabel &&
1175 ebbs[i]->entryLabel != returnLabel))
1178 /* for all instructions do */
1179 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1184 // update the registers in use at the start of this icode
1185 for (reg=0; reg<mcs51_nRegs; reg++) {
1186 if (regs8051[reg].isFree) {
1187 ic->riu &= ~(1<<regs8051[reg].offset);
1189 ic->riu |= (1<<regs8051[reg].offset);
1193 /* if this is an ipop that means some live
1194 range will have to be assigned again */
1196 reassignLR (IC_LEFT (ic));
1198 /* if result is present && is a true symbol */
1199 if (IC_RESULT (ic) && ic->op != IFX &&
1200 IS_TRUE_SYMOP (IC_RESULT (ic)))
1201 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1203 /* take away registers from live
1204 ranges that end at this instruction */
1205 deassignLRs (ic, ebbs[i]);
1207 /* some don't need registers */
1208 if (SKIP_IC2 (ic) ||
1209 ic->op == JUMPTABLE ||
1213 (IC_RESULT (ic) && POINTER_SET (ic)))
1216 /* now we need to allocate registers
1217 only for the result */
1218 if (IC_RESULT (ic)) {
1219 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1225 /* Make sure any spill location is definitely allocated */
1226 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1227 !sym->usl.spillLoc->allocreq)
1229 sym->usl.spillLoc->allocreq++;
1232 /* if it does not need or is spilt
1233 or is already assigned to registers
1234 or will not live beyond this instructions */
1237 bitVectBitValue (_G.regAssigned, sym->key) ||
1238 sym->liveTo <= ic->seq)
1241 /* if some liverange has been spilt at the block level
1242 and this one live beyond this block then spil this
1244 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1249 /* if this is a bit variable then don't use precious registers
1250 along with expensive bit-to-char conversions but just spill
1252 if (SPEC_NOUN(sym->etype) == V_BIT) {
1257 /* if trying to allocate this will cause
1258 a spill and there is nothing to spill
1259 or this one is rematerializable then
1261 willCS = willCauseSpill (sym->nRegs, sym->regType);
1262 spillable = computeSpillable (ic);
1263 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1268 /* If the live range preceeds the point of definition
1269 then ideally we must take into account registers that
1270 have been allocated after sym->liveFrom but freed
1271 before ic->seq. This is complicated, so spill this
1272 symbol instead and let fillGaps handle the allocation. */
1273 if (sym->liveFrom < ic->seq) {
1278 /* if it has a spillocation & is used less than
1279 all other live ranges then spill this */
1281 if (sym->usl.spillLoc) {
1282 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1283 allLRs, ebbs[i], ic));
1284 if (leastUsed && leastUsed->used > sym->used) {
1289 /* if none of the liveRanges have a spillLocation then better
1290 to spill this one than anything else already assigned to registers */
1291 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1292 /* if this is local to this block then we might find a block spil */
1293 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1300 /* if we need ptr regs for the right side
1302 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1303 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1307 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1308 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1312 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1313 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1318 /* else we assign registers to it */
1319 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1320 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1322 for (j = 0; j < sym->nRegs; j++) {
1323 sym->regs[j] = NULL;
1324 if (sym->regType == REG_PTR)
1325 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1328 if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1330 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1333 sym->regs[j] = allocThisReg (right->regs[j]);
1336 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1339 /* if the allocation failed which means
1340 this was spilt then break */
1343 for (i=0; i < sym->nRegs ; i++ )
1344 sym->regs[i] = NULL;
1349 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1350 /* if it shares registers with operands make sure
1351 that they are in the same position */
1352 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1353 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1354 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1355 OP_SYMBOL (IC_LEFT (ic)));
1357 /* do the same for the right operand */
1358 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1359 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1360 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1361 OP_SYMBOL (IC_RIGHT (ic)));
1374 /* Check for and fix any problems with uninitialized operands */
1375 for (i = 0; i < count; i++)
1379 if (ebbs[i]->noPath &&
1380 (ebbs[i]->entryLabel != entryLabel &&
1381 ebbs[i]->entryLabel != returnLabel))
1384 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1391 verifyRegsAssigned (IC_COND (ic), ic);
1395 if (ic->op == JUMPTABLE)
1397 verifyRegsAssigned (IC_JTCOND (ic), ic);
1401 verifyRegsAssigned (IC_RESULT (ic), ic);
1402 verifyRegsAssigned (IC_LEFT (ic), ic);
1403 verifyRegsAssigned (IC_RIGHT (ic), ic);
1408 /*-----------------------------------------------------------------*/
1409 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1410 /*-----------------------------------------------------------------*/
1411 static void fillGaps()
1418 if (getenv("DISABLE_FILL_GAPS")) return;
1420 /* look for liveranges that were spilt by the allocator */
1421 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1422 sym = hTabNextItem(liveRanges,&key)) {
1427 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1429 /* find the liveRanges this one clashes with, that are
1430 still assigned to registers & mark the registers as used*/
1431 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1435 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1436 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1439 clr = hTabItemWithKey(liveRanges,i);
1442 /* mark these registers as used */
1443 for (k = 0 ; k < clr->nRegs ; k++ )
1444 useReg(clr->regs[k]);
1447 if (willCauseSpill(sym->nRegs,sym->regType)) {
1448 /* NOPE :( clear all registers & and continue */
1454 for (i = 0 ; i < sym->defs->size ; i++ )
1456 if (bitVectBitValue(sym->defs,i))
1458 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1465 D(printf("Attempting fillGaps on %s: [",sym->name));
1466 /* THERE IS HOPE !!!! */
1467 for (i=0; i < sym->nRegs ; i++ ) {
1468 if (sym->regType == REG_PTR)
1469 sym->regs[i] = getRegPtrNoSpil ();
1472 sym->regs[i] = NULL;
1473 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1475 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1478 sym->regs[i] = allocThisReg (right->regs[i]);
1481 sym->regs[i] = getRegGprNoSpil ();
1483 D(printf("%s ", sym->regs[i]->name));
1487 /* For all its definitions check if the registers
1488 allocated needs positioning NOTE: we can position
1489 only ONCE if more than One positioning required
1491 We may need to perform the checks twice; once to
1492 position the registers as needed, the second to
1493 verify any register repositioning is still
1497 for (pass=0; pass<2; pass++) {
1498 D(printf(" checking definitions\n"));
1499 for (i = 0 ; i < sym->defs->size ; i++ ) {
1500 if (bitVectBitValue(sym->defs,i)) {
1501 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1502 D(printf(" ic->seq = %d\n", ic->seq));
1503 if (SKIP_IC(ic)) continue;
1504 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1505 /* if left is assigned to registers */
1506 if (IS_SYMOP(IC_LEFT(ic)))
1508 D(printf(" left = "));
1509 D(printOperand(IC_LEFT(ic),NULL));
1511 if (IS_SYMOP(IC_LEFT(ic)) &&
1512 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1513 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1515 if (IS_SYMOP(IC_RIGHT(ic)))
1517 D(printf(" right = "));
1518 D(printOperand(IC_RIGHT(ic),NULL));
1520 if (IS_SYMOP(IC_RIGHT(ic)) &&
1521 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1522 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1524 D(printf(" pdone = %d\n", pdone));
1525 if (pdone > 1) break;
1528 D(printf(" checking uses\n"));
1529 for (i = 0 ; i < sym->uses->size ; i++ ) {
1530 if (bitVectBitValue(sym->uses,i)) {
1532 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1533 D(printf(" ic->seq = %d\n", ic->seq));
1534 if (SKIP_IC(ic)) continue;
1535 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1537 /* if result is assigned to registers */
1538 if (IS_SYMOP(IC_RESULT(ic)))
1540 D(printf(" result = "));
1541 D(printOperand(IC_RESULT(ic),NULL));
1543 if (IS_SYMOP(IC_RESULT(ic)) &&
1544 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1545 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1547 D(printf(" pdone = %d\n", pdone));
1548 if (pdone > 1) break;
1551 if (pdone == 0) break; /* second pass only if regs repositioned */
1552 if (pdone > 1) break;
1554 D(printf(" sym->regs = ["));
1555 for (i=0; i < sym->nRegs ; i++ )
1556 D(printf("%s ", sym->regs[i]->name));
1558 /* had to position more than once GIVE UP */
1560 /* UNDO all the changes we made to try this */
1562 for (i=0; i < sym->nRegs ; i++ ) {
1563 sym->regs[i] = NULL;
1566 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1569 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1571 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1572 sym->isspilt = sym->spillA = 0 ;
1573 sym->usl.spillLoc->allocreq--;
1578 /*-----------------------------------------------------------------*/
1579 /* rUmaskForOp :- returns register mask for an operand */
1580 /*-----------------------------------------------------------------*/
1582 mcs51_rUmaskForOp (operand * op)
1588 /* only temporaries are assigned registers */
1592 sym = OP_SYMBOL (op);
1594 /* if spilt or no registers assigned to it
1596 if (sym->isspilt || !sym->nRegs)
1599 rumask = newBitVect (mcs51_nRegs);
1601 for (j = 0; j < sym->nRegs; j++)
1603 if (sym->regs[j]) /* EEP - debug */
1604 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1610 /*-----------------------------------------------------------------*/
1611 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1612 /*-----------------------------------------------------------------*/
1614 regsUsedIniCode (iCode * ic)
1616 bitVect *rmask = newBitVect (mcs51_nRegs);
1618 /* do the special cases first */
1621 rmask = bitVectUnion (rmask,
1622 mcs51_rUmaskForOp (IC_COND (ic)));
1626 /* for the jumptable */
1627 if (ic->op == JUMPTABLE)
1629 rmask = bitVectUnion (rmask,
1630 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1635 /* of all other cases */
1637 rmask = bitVectUnion (rmask,
1638 mcs51_rUmaskForOp (IC_LEFT (ic)));
1642 rmask = bitVectUnion (rmask,
1643 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1646 rmask = bitVectUnion (rmask,
1647 mcs51_rUmaskForOp (IC_RESULT (ic)));
1653 /*-----------------------------------------------------------------*/
1654 /* createRegMask - for each instruction will determine the regsUsed */
1655 /*-----------------------------------------------------------------*/
1657 createRegMask (eBBlock ** ebbs, int count)
1661 /* for all blocks */
1662 for (i = 0; i < count; i++)
1666 if (ebbs[i]->noPath &&
1667 (ebbs[i]->entryLabel != entryLabel &&
1668 ebbs[i]->entryLabel != returnLabel))
1671 /* for all instructions */
1672 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1677 if (SKIP_IC2 (ic) || !ic->rlive)
1680 /* first mark the registers used in this
1682 ic->rUsed = regsUsedIniCode (ic);
1683 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1685 /* now create the register mask for those
1686 registers that are in use : this is a
1687 super set of ic->rUsed */
1688 ic->rMask = newBitVect (mcs51_nRegs + 1);
1690 /* for all live Ranges alive at this point */
1691 for (j = 1; j < ic->rlive->size; j++)
1696 /* if not alive then continue */
1697 if (!bitVectBitValue (ic->rlive, j))
1700 /* find the live range we are interested in */
1701 if (!(sym = hTabItemWithKey (liveRanges, j)))
1703 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1704 "createRegMask cannot find live range");
1705 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1709 /* if no register assigned to it */
1710 if (!sym->nRegs || sym->isspilt)
1713 /* for all the registers allocated to it */
1714 for (k = 0; k < sym->nRegs; k++)
1717 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1723 /*-----------------------------------------------------------------*/
1724 /* rematStr - returns the rematerialized string for a remat var */
1725 /*-----------------------------------------------------------------*/
1727 rematStr (symbol * sym)
1730 iCode *ic = sym->rematiCode;
1737 /* if plus or minus print the right hand side */
1738 if (ic->op == '+' || ic->op == '-')
1740 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1741 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1744 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1748 /* cast then continue */
1749 if (IS_CAST_ICODE(ic)) {
1750 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1753 /* we reached the end */
1754 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1755 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1762 /*-----------------------------------------------------------------*/
1763 /* regTypeNum - computes the type & number of registers required */
1764 /*-----------------------------------------------------------------*/
1766 regTypeNum (eBBlock *ebbs)
1772 /* for each live range do */
1773 for (sym = hTabFirstItem (liveRanges, &k); sym;
1774 sym = hTabNextItem (liveRanges, &k))
1777 /* if used zero times then no registers needed */
1778 if ((sym->liveTo - sym->liveFrom) == 0)
1782 /* if the live range is a temporary */
1786 /* if the type is marked as a conditional */
1787 if (sym->regType == REG_CND)
1790 /* if used in return only then we don't
1792 if (sym->ruonly || sym->accuse)
1794 if (IS_AGGREGATE (sym->type) || sym->isptr)
1795 sym->type = aggrToPtr (sym->type, FALSE);
1799 /* if the symbol has only one definition &
1800 that definition is a get_pointer */
1801 if (bitVectnBitsOn (sym->defs) == 1 &&
1802 (ic = hTabItemWithKey (iCodehTab,
1803 bitVectFirstBit (sym->defs))) &&
1805 !IS_BITVAR (sym->etype) &&
1806 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1809 if (ptrPseudoSymSafe (sym, ic))
1811 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1815 /* if in data space or idata space then try to
1816 allocate pointer register */
1820 /* if not then we require registers */
1821 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1822 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1823 getSize (sym->type));
1827 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1828 printTypeChain (sym->type, stderr);
1829 fprintf (stderr, "\n");
1832 /* determine the type of register required */
1833 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
1834 sym->regType = REG_PTR;
1835 else if (IS_BIT(sym->type))
1836 sym->regType = REG_BIT;
1838 sym->regType = REG_GPR;
1841 /* for the first run we don't provide */
1842 /* registers for true symbols we will */
1843 /* see how things go */
1849 /*-----------------------------------------------------------------*/
1850 /* freeAllRegs - mark all registers as free */
1851 /*-----------------------------------------------------------------*/
1857 for (i = 0; i < mcs51_nRegs; i++)
1858 regs8051[i].isFree = 1;
1861 /*-----------------------------------------------------------------*/
1862 /* deallocStackSpil - this will set the stack pointer back */
1863 /*-----------------------------------------------------------------*/
1865 DEFSETFUNC (deallocStackSpil)
1873 /*-----------------------------------------------------------------*/
1874 /* farSpacePackable - returns the packable icode for far variables */
1875 /*-----------------------------------------------------------------*/
1877 farSpacePackable (iCode * ic)
1881 /* go thru till we find a definition for the
1882 symbol on the right */
1883 for (dic = ic->prev; dic; dic = dic->prev)
1885 /* if the definition is a call then no */
1886 if ((dic->op == CALL || dic->op == PCALL) &&
1887 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1892 /* if shift by unknown amount then not */
1893 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1894 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1897 /* if pointer get and size > 1 */
1898 if (POINTER_GET (dic) &&
1899 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1902 if (POINTER_SET (dic) &&
1903 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1908 if (IC_COND (dic) &&
1909 IS_TRUE_SYMOP (IC_COND (dic)) &&
1910 isOperandInFarSpace (IC_COND (dic)))
1913 else if (dic->op == JUMPTABLE)
1915 if (IC_JTCOND (dic) &&
1916 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1917 isOperandInFarSpace (IC_JTCOND (dic)))
1922 /* if any tree is a true symbol in far space */
1923 if (IC_RESULT (dic) &&
1924 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1925 isOperandInFarSpace (IC_RESULT (dic)))
1928 if (IC_RIGHT (dic) &&
1929 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1930 isOperandInFarSpace (IC_RIGHT (dic)) &&
1931 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1934 if (IC_LEFT (dic) &&
1935 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1936 isOperandInFarSpace (IC_LEFT (dic)) &&
1937 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1941 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1943 if ((dic->op == LEFT_OP ||
1944 dic->op == RIGHT_OP ||
1946 IS_OP_LITERAL (IC_RIGHT (dic)))
1956 /*-----------------------------------------------------------------*/
1957 /* packRegsForAssign - register reduction for assignment */
1958 /*-----------------------------------------------------------------*/
1960 packRegsForAssign (iCode * ic, eBBlock * ebp)
1964 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1965 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1966 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1971 /* if the true symbol is defined in far space or on stack
1972 then we should not since this will increase register pressure */
1973 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1977 /* find the definition of iTempNN scanning backwards if we find a
1978 a use of the true symbol in before we find the definition then
1980 for (dic = ic->prev; dic; dic = dic->prev)
1982 int crossedCall = 0;
1984 /* We can pack across a function call only if it's a local */
1985 /* variable or our parameter. Never pack global variables */
1986 /* or parameters to a function we call. */
1987 if ((dic->op == CALL || dic->op == PCALL))
1989 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
1990 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
2001 if (IS_SYMOP (IC_COND (dic)) &&
2002 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
2003 IC_COND (dic)->key == IC_RIGHT (ic)->key))
2011 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2012 IS_OP_VOLATILE (IC_RESULT (dic)))
2018 if (IS_SYMOP (IC_RESULT (dic)) &&
2019 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2021 if (POINTER_SET (dic))
2027 if (IS_SYMOP (IC_RIGHT (dic)) &&
2028 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2029 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2035 if (IS_SYMOP (IC_LEFT (dic)) &&
2036 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2037 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2043 if (IS_SYMOP (IC_RESULT (dic)) &&
2044 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2060 return 0; /* did not find */
2062 /* if assignment then check that right is not a bit */
2063 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2065 sym_link *etype = operandType (IC_RESULT (dic));
2066 if (IS_BITFIELD (etype))
2068 /* if result is a bit too then it's ok */
2069 etype = operandType (IC_RESULT (ic));
2070 if (!IS_BITFIELD (etype))
2077 /* if assignment then check that right is not a bit */
2078 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2080 sym_link *etype = operandType (IC_RIGHT (dic));
2081 if (IS_BITFIELD (etype))
2083 /* if result is a bit too then it's ok */
2084 etype = operandType (IC_RESULT (dic));
2085 if (!IS_BITFIELD (etype))
2090 /* if the result is on stack or iaccess then it must be
2091 the same atleast one of the operands */
2092 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2093 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2096 /* the operation has only one symbol
2097 operator then we can pack */
2098 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2099 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2102 if (!((IC_LEFT (dic) &&
2103 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2105 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2109 /* found the definition */
2110 /* replace the result with the result of */
2111 /* this assignment and remove this assignment */
2112 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2113 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2115 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2117 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2119 // TODO: and the otherway around?
2121 /* delete from liverange table also
2122 delete from all the points inbetween and the new
2124 for (sic = dic; sic != ic; sic = sic->next)
2126 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2127 if (IS_ITEMP (IC_RESULT (dic)))
2128 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2131 remiCodeFromeBBlock (ebp, ic);
2132 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2133 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2134 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2138 /*------------------------------------------------------------------*/
2139 /* findAssignToSym : scanning backwards looks for first assig found */
2140 /*------------------------------------------------------------------*/
2142 findAssignToSym (operand * op, iCode * ic)
2146 /* This routine is used to find sequences like
2148 ...; (intervening ops don't use iTempAA or modify FOO)
2149 blah = blah + iTempAA;
2151 and eliminate the use of iTempAA, freeing up its register for
2155 for (dic = ic->prev; dic; dic = dic->prev)
2158 /* if definition by assignment */
2159 if (dic->op == '=' &&
2160 !POINTER_SET (dic) &&
2161 IC_RESULT (dic)->key == op->key
2162 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2164 break; /* found where this temp was defined */
2166 /* if we find an usage then we cannot delete it */
2170 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2173 else if (dic->op == JUMPTABLE)
2175 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2180 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2183 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2186 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2192 return NULL; /* didn't find any assignment to op */
2194 /* we are interested only if defined in far space */
2195 /* or in stack space in case of + & - */
2197 /* if assigned to a non-symbol then don't repack regs */
2198 if (!IS_SYMOP (IC_RIGHT (dic)))
2201 /* if the symbol is volatile then we should not */
2202 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2204 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2205 What does it mean for an iTemp to be volatile, anyway? Passing
2206 TRUE is more cautious but may prevent possible optimizations */
2208 /* if the symbol is in far space then we should not */
2209 if (isOperandInFarSpace (IC_RIGHT (dic)))
2212 /* for + & - operations make sure that
2213 if it is on the stack it is the same
2214 as one of the three operands */
2215 if ((ic->op == '+' || ic->op == '-') &&
2216 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2219 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2220 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2221 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2225 /* now make sure that the right side of dic
2226 is not defined between ic & dic */
2229 iCode *sic = dic->next;
2231 for (; sic != ic; sic = sic->next)
2232 if (IC_RESULT (sic) &&
2233 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2240 /*-----------------------------------------------------------------*/
2241 /* reassignAliasedSym - used by packRegsForSupport to replace */
2242 /* redundant iTemp with equivalent symbol */
2243 /*-----------------------------------------------------------------*/
2245 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2248 unsigned oldSymKey, newSymKey;
2250 oldSymKey = op->key;
2251 newSymKey = IC_RIGHT(assignment)->key;
2253 /* only track live ranges of compiler-generated temporaries */
2254 if (!IS_ITEMP(IC_RIGHT(assignment)))
2257 /* update the live-value bitmaps */
2258 for (ic = assignment; ic != use; ic = ic->next) {
2259 bitVectUnSetBit (ic->rlive, oldSymKey);
2261 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2264 /* update the sym of the used operand */
2265 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2266 op->key = OP_SYMBOL(op)->key;
2267 OP_SYMBOL(op)->accuse = 0;
2269 /* update the sym's liverange */
2270 if ( OP_LIVETO(op) < ic->seq )
2271 setToRange(op, ic->seq, FALSE);
2273 /* remove the assignment iCode now that its result is unused */
2274 remiCodeFromeBBlock (ebp, assignment);
2275 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2276 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2280 /*-----------------------------------------------------------------*/
2281 /* packRegsForSupport :- reduce some registers for support calls */
2282 /*-----------------------------------------------------------------*/
2284 packRegsForSupport (iCode * ic, eBBlock * ebp)
2288 /* for the left & right operand :- look to see if the
2289 left was assigned a true symbol in far space in that
2290 case replace them */
2292 if (IS_ITEMP (IC_LEFT (ic)) &&
2293 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2295 dic = findAssignToSym (IC_LEFT (ic), ic);
2299 /* found it we need to remove it from the block */
2300 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2305 /* do the same for the right operand */
2306 if (IS_ITEMP (IC_RIGHT (ic)) &&
2307 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2309 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2313 /* if this is a subtraction & the result
2314 is a true symbol in far space then don't pack */
2315 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2317 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2318 if (IN_FARSPACE (SPEC_OCLS (etype)))
2321 /* found it we need to remove it from the
2323 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2332 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2335 /*-----------------------------------------------------------------*/
2336 /* packRegsForOneuse : - will reduce some registers for single Use */
2337 /*-----------------------------------------------------------------*/
2339 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2343 /* if returning a literal then do nothing */
2347 /* if rematerializable or already return use then do nothing */
2348 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly)
2351 /* only upto 2 bytes since we cannot predict
2352 the usage of b, & acc */
2353 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2356 if (ic->op != RETURN &&
2358 !POINTER_SET (ic) &&
2362 if (ic->op == SEND && ic->argreg != 1) return NULL;
2364 /* this routine will mark the a symbol as used in one
2365 instruction use only && if the defintion is local
2366 (ie. within the basic block) && has only one definition &&
2367 that definiion is either a return value from a
2368 function or does not contain any variables in
2370 if (bitVectnBitsOn (OP_USES (op)) > 1)
2373 /* if it has only one defintion */
2374 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2375 return NULL; /* has more than one definition */
2377 /* get that definition */
2379 hTabItemWithKey (iCodehTab,
2380 bitVectFirstBit (OP_DEFS (op)))))
2383 /* if that only usage is a cast */
2384 if (dic->op == CAST) {
2385 /* to a bigger type */
2386 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2387 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2388 /* than we can not, since we cannot predict the usage of b & acc */
2393 /* found the definition now check if it is local */
2394 if (dic->seq < ebp->fSeq ||
2395 dic->seq > ebp->lSeq)
2396 return NULL; /* non-local */
2398 /* now check if it is the return from
2400 if (dic->op == CALL || dic->op == PCALL)
2402 if (ic->op != SEND && ic->op != RETURN &&
2403 !POINTER_SET(ic) && !POINTER_GET(ic))
2405 OP_SYMBOL (op)->ruonly = 1;
2411 /* otherwise check that the definition does
2412 not contain any symbols in far space */
2413 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2414 isOperandInFarSpace (IC_RIGHT (dic)) ||
2415 IS_OP_RUONLY (IC_LEFT (ic)) ||
2416 IS_OP_RUONLY (IC_RIGHT (ic)))
2421 /* if pointer set then make sure the pointer
2423 if (POINTER_SET (dic) &&
2424 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2427 if (POINTER_GET (dic) &&
2428 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2432 /* Make sure no overlapping liverange is already assigned to DPTR */
2433 if (OP_SYMBOL(op)->clashes)
2438 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2440 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2442 sym = hTabItemWithKey(liveRanges,i);
2451 /* also make sure the intervenening instructions
2452 don't have any thing in far space */
2453 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2456 /* if there is an intervening function call then no */
2457 if (dic->op == CALL || dic->op == PCALL)
2459 /* if pointer set then make sure the pointer
2461 if (POINTER_SET (dic) &&
2462 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2465 if (POINTER_GET (dic) &&
2466 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2469 /* if address of & the result is remat the okay */
2470 if (dic->op == ADDRESS_OF &&
2471 OP_SYMBOL (IC_RESULT (dic))->remat)
2474 /* if operand has size of three or more & this
2475 operation is a '*','/' or '%' then 'b' may
2477 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2478 getSize (operandType (op)) >= 3)
2481 /* if left or right or result is in far space */
2482 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2483 isOperandInFarSpace (IC_RIGHT (dic)) ||
2484 isOperandInFarSpace (IC_RESULT (dic)) ||
2485 IS_OP_RUONLY (IC_LEFT (dic)) ||
2486 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2487 IS_OP_RUONLY (IC_RESULT (dic)))
2491 /* if left or right or result is on stack */
2492 if (isOperandOnStack(IC_LEFT(dic)) ||
2493 isOperandOnStack(IC_RIGHT(dic)) ||
2494 isOperandOnStack(IC_RESULT(dic))) {
2499 OP_SYMBOL (op)->ruonly = 1;
2503 /*-----------------------------------------------------------------*/
2504 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2505 /*-----------------------------------------------------------------*/
2507 isBitwiseOptimizable (iCode * ic)
2509 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2510 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2512 /* bitwise operations are considered optimizable
2513 under the following conditions (Jean-Louis VERN)
2525 if (IS_LITERAL(rtype) ||
2526 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2532 /*-----------------------------------------------------------------*/
2533 /* isCommutativeOp - tests whether this op cares what order its */
2534 /* operands are in */
2535 /*-----------------------------------------------------------------*/
2536 bool isCommutativeOp(unsigned int op)
2538 if (op == '+' || op == '*' || op == EQ_OP ||
2539 op == '^' || op == '|' || op == BITWISEAND)
2545 /*-----------------------------------------------------------------*/
2546 /* operandUsesAcc - determines whether the code generated for this */
2547 /* operand will have to use the accumulator */
2548 /*-----------------------------------------------------------------*/
2549 bool operandUsesAcc(operand *op)
2555 symbol *sym = OP_SYMBOL(op);
2559 return TRUE; /* duh! */
2561 if (IN_STACK(sym->etype) || sym->onStack ||
2562 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2563 return TRUE; /* acc is used to calc stack offset */
2568 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2570 return FALSE; /* more checks? */
2574 symspace = SPEC_OCLS(sym->etype);
2576 if (sym->iaccess && symspace->paged)
2577 return TRUE; /* must fetch paged indirect sym via accumulator */
2579 if (IN_BITSPACE(symspace))
2580 return TRUE; /* fetching bit vars uses the accumulator */
2582 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2583 return TRUE; /* fetched via accumulator and dptr */
2589 /*-----------------------------------------------------------------*/
2590 /* packRegsForAccUse - pack registers for acc use */
2591 /*-----------------------------------------------------------------*/
2593 packRegsForAccUse (iCode * ic)
2597 /* if this is an aggregate, e.g. a one byte char array */
2598 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2602 /* if we are calling a reentrant function that has stack parameters */
2603 if (ic->op == CALL &&
2604 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2605 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2608 if (ic->op == PCALL &&
2609 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2610 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2613 /* if + or - then it has to be one byte result */
2614 if ((ic->op == '+' || ic->op == '-')
2615 && getSize (operandType (IC_RESULT (ic))) > 1)
2618 /* if shift operation make sure right side is not a literal */
2619 if (ic->op == RIGHT_OP &&
2620 (isOperandLiteral (IC_RIGHT (ic)) ||
2621 getSize (operandType (IC_RESULT (ic))) > 1))
2624 if (ic->op == LEFT_OP &&
2625 (isOperandLiteral (IC_RIGHT (ic)) ||
2626 getSize (operandType (IC_RESULT (ic))) > 1))
2629 if (IS_BITWISE_OP (ic) &&
2630 getSize (operandType (IC_RESULT (ic))) > 1)
2634 /* has only one definition */
2635 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2638 /* has only one use */
2639 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2642 /* and the usage immediately follows this iCode */
2643 if (!(uic = hTabItemWithKey (iCodehTab,
2644 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2647 if (ic->next != uic)
2650 /* if it is a conditional branch then we definitely can */
2654 if (uic->op == JUMPTABLE)
2657 if (POINTER_SET (uic) &&
2658 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2661 /* if the usage is not is an assignment
2662 or an arithmetic / bitwise / shift operation then not */
2663 if (uic->op != '=' &&
2664 !IS_ARITHMETIC_OP (uic) &&
2665 !IS_BITWISE_OP (uic) &&
2666 uic->op != LEFT_OP &&
2667 uic->op != RIGHT_OP)
2670 /* if used in ^ operation then make sure right is not a
2671 literal (WIML: Why is this?) */
2672 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2675 /* if shift operation make sure right side is not a literal */
2676 /* WIML: Why is this? */
2677 if (uic->op == RIGHT_OP &&
2678 (isOperandLiteral (IC_RIGHT (uic)) ||
2679 getSize (operandType (IC_RESULT (uic))) > 1))
2681 if (uic->op == LEFT_OP &&
2682 (isOperandLiteral (IC_RIGHT (uic)) ||
2683 getSize (operandType (IC_RESULT (uic))) > 1))
2686 /* make sure that the result of this icode is not on the
2687 stack, since acc is used to compute stack offset */
2689 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2690 OP_SYMBOL (IC_RESULT (uic))->onStack)
2693 if (isOperandOnStack(IC_RESULT(uic)))
2697 /* if the usage has only one operand then we can */
2698 if (IC_LEFT (uic) == NULL ||
2699 IC_RIGHT (uic) == NULL)
2702 /* if the other operand uses the accumulator then we cannot */
2703 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2704 operandUsesAcc(IC_RIGHT(uic))) ||
2705 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2706 operandUsesAcc(IC_LEFT(uic))) )
2709 /* make sure this is on the left side if not commutative */
2710 /* except for '-', which has been written to be able to
2711 handle reversed operands */
2712 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2713 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2717 // this is too dangerous and need further restrictions
2720 /* if one of them is a literal then we can */
2721 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2722 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2724 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2730 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2734 /*-----------------------------------------------------------------*/
2735 /* packForPush - heuristics to reduce iCode for pushing */
2736 /*-----------------------------------------------------------------*/
2738 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2742 struct eBBlock * ebp=ebpp[blockno];
2744 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2747 /* must have only definition & one usage */
2748 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2749 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2752 /* find the definition */
2753 if (!(dic = hTabItemWithKey (iCodehTab,
2754 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2757 if (dic->op != '=' || POINTER_SET (dic))
2760 if (dic->seq < ebp->fSeq) { // Evelyn did this
2762 for (i=0; i<blockno; i++) {
2763 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2768 wassert (i!=blockno); // no way to recover from here
2771 if (IS_SYMOP(IC_RIGHT(dic))) {
2772 /* make sure the right side does not have any definitions
2774 dbv = OP_DEFS(IC_RIGHT(dic));
2775 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2776 if (bitVectBitValue(dbv,lic->key))
2779 /* make sure they have the same type */
2780 if (IS_SPEC(operandType(IC_LEFT(ic))))
2782 sym_link *itype=operandType(IC_LEFT(ic));
2783 sym_link *ditype=operandType(IC_RIGHT(dic));
2785 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2786 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2789 /* extend the live range of replaced operand if needed */
2790 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2791 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2793 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2796 /* we now we know that it has one & only one def & use
2797 and the that the definition is an assignment */
2798 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2799 remiCodeFromeBBlock (ebp, dic);
2800 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2803 /*-----------------------------------------------------------------*/
2804 /* packRegisters - does some transformations to reduce register */
2806 /*-----------------------------------------------------------------*/
2808 packRegisters (eBBlock ** ebpp, int blockno)
2812 eBBlock *ebp=ebpp[blockno];
2818 /* look for assignments of the form */
2819 /* iTempNN = TRueSym (someoperation) SomeOperand */
2821 /* TrueSym := iTempNN:1 */
2822 for (ic = ebp->sch; ic; ic = ic->next)
2824 /* find assignment of the form TrueSym := iTempNN:1 */
2825 if (ic->op == '=' && !POINTER_SET (ic))
2826 change += packRegsForAssign (ic, ebp);
2833 for (ic = ebp->sch; ic; ic = ic->next)
2835 /* Fix for bug #979599: */
2838 /* Look for two subsequent iCodes with */
2840 /* _c = iTemp & op; */
2841 /* and replace them by */
2844 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2846 ic->prev->op == '=' &&
2847 IS_ITEMP (IC_LEFT (ic)) &&
2848 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2849 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
2851 iCode* ic_prev = ic->prev;
2852 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
2854 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
2855 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
2857 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
2858 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
2859 prev_result_sym->liveTo == ic->seq)
2861 prev_result_sym->liveTo = ic_prev->seq;
2864 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
2866 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
2868 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
2870 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
2871 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
2872 remiCodeFromeBBlock (ebp, ic_prev);
2873 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
2877 /* if this is an itemp & result of an address of a true sym
2878 then mark this as rematerialisable */
2879 if (ic->op == ADDRESS_OF &&
2880 IS_ITEMP (IC_RESULT (ic)) &&
2881 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2882 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2883 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2885 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2886 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2887 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2890 /* if straight assignment then carry remat flag if
2891 this is the only definition */
2892 if (ic->op == '=' &&
2893 !POINTER_SET (ic) &&
2894 IS_SYMOP (IC_RIGHT (ic)) &&
2895 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2896 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2897 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2899 OP_SYMBOL (IC_RESULT (ic))->remat =
2900 OP_SYMBOL (IC_RIGHT (ic))->remat;
2901 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2902 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2905 /* if cast to a generic pointer & the pointer being
2906 cast is remat, then we can remat this cast as well */
2907 if (ic->op == CAST &&
2908 IS_SYMOP(IC_RIGHT(ic)) &&
2909 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2910 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2912 sym_link *to_type = operandType(IC_LEFT(ic));
2913 sym_link *from_type = operandType(IC_RIGHT(ic));
2914 if (IS_GENPTR(to_type) && IS_PTR(from_type))
2916 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2917 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2918 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2922 /* if this is a +/- operation with a rematerizable
2923 then mark this as rematerializable as well */
2924 if ((ic->op == '+' || ic->op == '-') &&
2925 (IS_SYMOP (IC_LEFT (ic)) &&
2926 IS_ITEMP (IC_RESULT (ic)) &&
2927 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2928 OP_SYMBOL (IC_LEFT (ic))->remat &&
2929 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2930 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2932 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2933 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2934 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2937 /* mark the pointer usages */
2938 if (POINTER_SET (ic))
2939 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2941 if (POINTER_GET (ic) &&
2942 IS_SYMOP(IC_LEFT (ic)))
2943 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2947 /* if we are using a symbol on the stack
2948 then we should say mcs51_ptrRegReq */
2949 if (options.useXstack && ic->parmPush
2950 && (ic->op == IPUSH || ic->op == IPOP))
2952 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2953 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2954 OP_SYMBOL (IC_COND (ic))->iaccess ||
2955 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
2956 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2957 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2958 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
2959 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
2962 if (IS_SYMOP (IC_LEFT (ic)))
2963 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2964 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
2965 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
2966 if (IS_SYMOP (IC_RIGHT (ic)))
2967 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2968 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
2969 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
2970 if (IS_SYMOP (IC_RESULT (ic)))
2971 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2972 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
2973 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
2974 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
2975 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
2977 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
2978 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
2983 /* if the condition of an if instruction
2984 is defined in the previous instruction and
2985 this is the only usage then
2986 mark the itemp as a conditional */
2987 if ((IS_CONDITIONAL (ic) ||
2988 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2989 ic->next && ic->next->op == IFX &&
2990 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2991 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2992 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2994 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2998 /* if the condition of an if instruction
2999 is defined in the previous GET_POINTER instruction and
3000 this is the only usage then
3001 mark the itemp as accumulator use */
3002 if ((POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1) &&
3003 ic->next && ic->next->op == IFX &&
3004 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3005 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3006 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3008 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3012 /* reduce for support function calls */
3013 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3014 packRegsForSupport (ic, ebp);
3016 /* some cases the redundant moves can
3017 can be eliminated for return statements */
3018 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
3019 !isOperandInFarSpace (IC_LEFT (ic)) &&
3020 options.model == MODEL_SMALL) {
3021 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3024 /* if pointer set & left has a size more than
3025 one and right is not in far space */
3026 if (POINTER_SET (ic) &&
3027 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3028 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3029 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3030 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3031 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3033 /* if pointer get */
3034 if (POINTER_GET (ic) &&
3035 IS_SYMOP (IC_LEFT (ic)) &&
3036 !isOperandInFarSpace (IC_RESULT (ic)) &&
3037 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3038 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3039 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3040 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3043 /* if this is a cast for intergral promotion then
3044 check if it's the only use of the definition of the
3045 operand being casted/ if yes then replace
3046 the result of that arithmetic operation with
3047 this result and get rid of the cast */
3050 sym_link *fromType = operandType (IC_RIGHT (ic));
3051 sym_link *toType = operandType (IC_LEFT (ic));
3053 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3054 getSize (fromType) != getSize (toType) &&
3055 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3058 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3061 if (IS_ARITHMETIC_OP (dic))
3063 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3064 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3065 remiCodeFromeBBlock (ebp, ic);
3066 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3067 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3068 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3072 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3078 /* if the type from and type to are the same
3079 then if this is the only use then packit */
3080 if (compareType (operandType (IC_RIGHT (ic)),
3081 operandType (IC_LEFT (ic))) == 1)
3083 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3086 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3087 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3088 remiCodeFromeBBlock (ebp, ic);
3089 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3090 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3091 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3099 iTempNN := (some variable in farspace) V1
3104 if (ic->op == IPUSH)
3106 packForPush (ic, ebpp, blockno);
3110 /* pack registers for accumulator use, when the
3111 result of an arithmetic or bit wise operation
3112 has only one use, that use is immediately following
3113 the defintion and the using iCode has only one
3114 operand or has two operands but one is literal &
3115 the result of that operation is not on stack then
3116 we can leave the result of this operation in acc:b
3118 if ((IS_ARITHMETIC_OP (ic)
3119 || IS_CONDITIONAL(ic)
3120 || IS_BITWISE_OP (ic)
3121 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3122 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3124 IS_ITEMP (IC_RESULT (ic)) &&
3125 getSize (operandType (IC_RESULT (ic))) <= 2)
3127 packRegsForAccUse (ic);
3131 /*-----------------------------------------------------------------*/
3132 /* assignRegisters - assigns registers to each live range as need */
3133 /*-----------------------------------------------------------------*/
3135 mcs51_assignRegisters (ebbIndex * ebbi)
3137 eBBlock ** ebbs = ebbi->bbOrder;
3138 int count = ebbi->count;
3142 setToNull ((void *) &_G.funcrUsed);
3143 setToNull ((void *) &_G.regAssigned);
3144 setToNull ((void *) &_G.totRegAssigned);
3145 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3148 /* change assignments this will remove some
3149 live ranges reducing some register pressure */
3151 for (i = 0; i < count; i++)
3152 packRegisters (ebbs, i);
3154 /* liveranges probably changed by register packing
3155 so we compute them again */
3156 recomputeLiveRanges (ebbs, count);
3158 if (options.dump_pack)
3159 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3161 /* first determine for each live range the number of
3162 registers & the type of registers required for each */
3165 /* and serially allocate registers */
3166 serialRegAssign (ebbs, count);
3169 //setToNull ((void *) &_G.regAssigned);
3170 //setToNull ((void *) &_G.totRegAssigned);
3173 /* if stack was extended then tell the user */
3176 /* werror(W_TOOMANY_SPILS,"stack", */
3177 /* _G.stackExtend,currFunc->name,""); */
3183 /* werror(W_TOOMANY_SPILS,"data space", */
3184 /* _G.dataExtend,currFunc->name,""); */
3188 /* after that create the register mask
3189 for each of the instruction */
3190 createRegMask (ebbs, count);
3192 /* redo that offsets for stacked automatic variables */
3194 redoStackOffsets ();
3197 /* make sure r0 & r1 are flagged as used if they might be used */
3199 if (currFunc && mcs51_ptrRegReq)
3201 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3202 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3205 if (options.dump_rassgn)
3207 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3208 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3211 /* do the overlaysegment stuff SDCCmem.c */
3212 doOverlays (ebbs, count);
3214 /* now get back the chain */
3215 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3219 /* free up any _G.stackSpil locations allocated */
3220 applyToSet (_G.stackSpil, deallocStackSpil);
3222 setToNull ((void *) &_G.stackSpil);
3223 setToNull ((void *) &_G.spiltSet);
3224 /* mark all registers as free */