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 SPEC_SCLS (sloc->etype) = S_DATA;
509 SPEC_EXTR (sloc->etype) = 0;
510 SPEC_STAT (sloc->etype) = 0;
511 SPEC_VOLATILE(sloc->etype) = 0;
512 SPEC_ABSA(sloc->etype) = 0;
514 /* we don't allow it to be allocated`
515 onto the external stack since : so we
516 temporarily turn it off ; we also
517 turn off memory model to prevent
518 the spil from going to the external storage
521 useXstack = options.useXstack;
522 model = options.model;
523 /* noOverlay = options.noOverlay; */
524 /* options.noOverlay = 1; */
525 options.model = options.useXstack = 0;
529 options.useXstack = useXstack;
530 options.model = model;
531 /* options.noOverlay = noOverlay; */
532 sloc->isref = 1; /* to prevent compiler warning */
534 /* if it is on the stack then update the stack */
535 if (IN_STACK (sloc->etype))
537 currFunc->stack += getSize (sloc->type);
538 _G.stackExtend += getSize (sloc->type);
541 _G.dataExtend += getSize (sloc->type);
543 /* add it to the _G.stackSpil set */
544 addSetHead (&_G.stackSpil, sloc);
545 sym->usl.spillLoc = sloc;
548 /* add it to the set of itempStack set
549 of the spill location */
550 addSetHead (&sloc->usl.itmpStack, sym);
554 /*-----------------------------------------------------------------*/
555 /* isSpiltOnStack - returns true if the spil location is on stack */
556 /*-----------------------------------------------------------------*/
558 isSpiltOnStack (symbol * sym)
568 /* if (sym->_G.stackSpil) */
571 if (!sym->usl.spillLoc)
574 etype = getSpec (sym->usl.spillLoc->type);
575 if (IN_STACK (etype))
581 /*-----------------------------------------------------------------*/
582 /* spillThis - spils a specific operand */
583 /*-----------------------------------------------------------------*/
585 spillThis (symbol * sym)
588 /* if this is rematerializable or has a spillLocation
589 we are okay, else we need to create a spillLocation
591 if (!(sym->remat || sym->usl.spillLoc))
592 createStackSpil (sym);
594 /* mark it has spilt & put it in the spilt set */
595 sym->isspilt = sym->spillA = 1;
596 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
598 bitVectUnSetBit (_G.regAssigned, sym->key);
599 bitVectUnSetBit (_G.totRegAssigned, sym->key);
601 for (i = 0; i < sym->nRegs; i++)
605 freeReg (sym->regs[i]);
609 /* if spilt on stack then free up r0 & r1
610 if they could have been assigned to some
612 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
615 spillLRWithPtrReg (sym);
618 if (sym->usl.spillLoc && !sym->remat)
619 sym->usl.spillLoc->allocreq++;
625 /*-----------------------------------------------------------------*/
626 /* selectSpil - select a iTemp to spil : rather a simple procedure */
627 /*-----------------------------------------------------------------*/
629 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
631 bitVect *lrcs = NULL;
635 /* get the spillable live ranges */
636 lrcs = computeSpillable (ic);
638 /* get all live ranges that are rematerizable */
639 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
642 /* return the least used of these */
643 return leastUsedLR (selectS);
646 /* get live ranges with spillLocations in direct space */
647 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
649 sym = leastUsedLR (selectS);
650 strncpyz (sym->rname,
651 sym->usl.spillLoc->rname[0] ?
652 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
655 /* mark it as allocation required */
656 sym->usl.spillLoc->allocreq++;
660 /* if the symbol is local to the block then */
661 if (forSym->liveTo < ebp->lSeq)
664 /* check if there are any live ranges allocated
665 to registers that are not used in this block */
666 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
668 sym = leastUsedLR (selectS);
669 /* if this is not rematerializable */
678 /* check if there are any live ranges that not
679 used in the remainder of the block */
681 !isiCodeInFunctionCall (ic) &&
682 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
684 sym = leastUsedLR (selectS);
697 /* find live ranges with spillocation && not used as pointers */
698 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
701 sym = leastUsedLR (selectS);
702 /* mark this as allocation required */
703 sym->usl.spillLoc->allocreq++;
707 /* find live ranges with spillocation */
708 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
711 sym = leastUsedLR (selectS);
712 sym->usl.spillLoc->allocreq++;
716 /* couldn't find then we need to create a spil
717 location on the stack , for which one? the least
719 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
722 /* return a created spil location */
723 sym = createStackSpil (leastUsedLR (selectS));
724 sym->usl.spillLoc->allocreq++;
728 /* this is an extreme situation we will spill
729 this one : happens very rarely but it does happen */
735 /*-----------------------------------------------------------------*/
736 /* spilSomething - spil some variable & mark registers as free */
737 /*-----------------------------------------------------------------*/
739 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
744 /* get something we can spil */
745 ssym = selectSpil (ic, ebp, forSym);
747 /* mark it as spilt */
748 ssym->isspilt = ssym->spillA = 1;
749 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
751 /* mark it as not register assigned &
752 take it away from the set */
753 bitVectUnSetBit (_G.regAssigned, ssym->key);
754 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
756 /* mark the registers as free */
757 for (i = 0; i < ssym->nRegs; i++)
759 freeReg (ssym->regs[i]);
761 /* if spilt on stack then free up r0 & r1
762 if they could have been assigned to as gprs */
763 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
766 spillLRWithPtrReg (ssym);
769 /* if this was a block level spil then insert push & pop
770 at the start & end of block respectively */
773 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
774 /* add push to the start of the block */
775 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
776 ebp->sch->next : ebp->sch));
777 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
778 /* add pop to the end of the block */
779 addiCodeToeBBlock (ebp, nic, NULL);
782 /* if spilt because not used in the remainder of the
783 block then add a push before this instruction and
784 a pop at the end of the block */
785 if (ssym->remainSpil)
788 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
789 /* add push just before this instruction */
790 addiCodeToeBBlock (ebp, nic, ic);
792 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
793 /* add pop to the end of the block */
794 addiCodeToeBBlock (ebp, nic, NULL);
803 /*-----------------------------------------------------------------*/
804 /* getRegPtr - will try for PTR if not a GPR type if not spil */
805 /*-----------------------------------------------------------------*/
807 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 /* make sure partially assigned registers aren't reused */
826 for (j=0; j<=sym->nRegs; j++)
828 sym->regs[j]->isFree = 0;
830 /* this looks like an infinite loop but
831 in really selectSpil will abort */
835 /*-----------------------------------------------------------------*/
836 /* getRegGpr - will try for GPR if not spil */
837 /*-----------------------------------------------------------------*/
839 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
845 /* try for gpr type */
846 if ((reg = allocReg (REG_GPR)))
849 if (!mcs51_ptrRegReq)
850 if ((reg = allocReg (REG_PTR)))
853 /* we have to spil */
854 if (!spilSomething (ic, ebp, sym))
857 /* make sure partially assigned registers aren't reused */
858 for (j=0; j<=sym->nRegs; j++)
860 sym->regs[j]->isFree = 0;
862 /* this looks like an infinite loop but
863 in really selectSpil will abort */
867 /*-----------------------------------------------------------------*/
868 /* getRegPtrNoSpil - get it cannot split */
869 /*-----------------------------------------------------------------*/
870 static regs *getRegPtrNoSpil()
874 /* try for a ptr type */
875 if ((reg = allocReg (REG_PTR)))
878 /* try for gpr type */
879 if ((reg = allocReg (REG_GPR)))
884 /* just to make the compiler happy */
888 /*-----------------------------------------------------------------*/
889 /* getRegGprNoSpil - get it cannot split */
890 /*-----------------------------------------------------------------*/
891 static regs *getRegGprNoSpil()
895 if ((reg = allocReg (REG_GPR)))
898 if (!mcs51_ptrRegReq)
899 if ((reg = allocReg (REG_PTR)))
904 /* just to make the compiler happy */
908 /*-----------------------------------------------------------------*/
909 /* symHasReg - symbol has a given register */
910 /*-----------------------------------------------------------------*/
912 symHasReg (symbol * sym, regs * reg)
916 for (i = 0; i < sym->nRegs; i++)
917 if (sym->regs[i] == reg)
923 /*-----------------------------------------------------------------*/
924 /* deassignLRs - check the live to and if they have registers & are */
925 /* not spilt then free up the registers */
926 /*-----------------------------------------------------------------*/
928 deassignLRs (iCode * ic, eBBlock * ebp)
934 for (sym = hTabFirstItem (liveRanges, &k); sym;
935 sym = hTabNextItem (liveRanges, &k))
939 /* if it does not end here */
940 if (sym->liveTo > ic->seq)
943 /* if it was spilt on stack then we can
944 mark the stack spil location as free */
949 sym->usl.spillLoc->isFree = 1;
955 if (!bitVectBitValue (_G.regAssigned, sym->key))
958 /* special case check if this is an IFX &
959 the privious one was a pop and the
960 previous one was not spilt then keep track
962 if (ic->op == IFX && ic->prev &&
963 ic->prev->op == IPOP &&
964 !ic->prev->parmPush &&
965 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
966 psym = OP_SYMBOL (IC_LEFT (ic->prev));
972 bitVectUnSetBit (_G.regAssigned, sym->key);
974 /* if the result of this one needs registers
975 and does not have it then assign it right
977 if (IC_RESULT (ic) &&
978 !(SKIP_IC2 (ic) || /* not a special icode */
979 ic->op == JUMPTABLE ||
985 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
986 result->liveTo > ic->seq && /* and will live beyond this */
987 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
988 result->liveFrom == ic->seq && /* does not start before here */
989 result->regType == sym->regType && /* same register types */
990 result->nRegs && /* which needs registers */
991 !result->isspilt && /* and does not already have them */
993 !bitVectBitValue (_G.regAssigned, result->key) &&
994 /* the number of free regs + number of regs in this LR
995 can accomodate the what result Needs */
996 ((nfreeRegsType (result->regType) +
997 sym->nRegs) >= result->nRegs)
1001 for (i = 0; i < result->nRegs; i++)
1003 result->regs[i] = sym->regs[i];
1005 result->regs[i] = getRegGpr (ic, ebp, result);
1007 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1008 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1012 /* free the remaining */
1013 for (; i < sym->nRegs; i++)
1017 if (!symHasReg (psym, sym->regs[i]))
1018 freeReg (sym->regs[i]);
1021 freeReg (sym->regs[i]);
1028 /*-----------------------------------------------------------------*/
1029 /* reassignLR - reassign this to registers */
1030 /*-----------------------------------------------------------------*/
1032 reassignLR (operand * op)
1034 symbol *sym = OP_SYMBOL (op);
1037 /* not spilt any more */
1038 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1039 bitVectUnSetBit (_G.spiltSet, sym->key);
1041 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1042 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1046 for (i = 0; i < sym->nRegs; i++)
1047 sym->regs[i]->isFree = 0;
1050 /*-----------------------------------------------------------------*/
1051 /* willCauseSpill - determines if allocating will cause a spill */
1052 /*-----------------------------------------------------------------*/
1054 willCauseSpill (int nr, int rt)
1056 /* first check if there are any avlb registers
1057 of te type required */
1060 /* special case for pointer type
1061 if pointer type not avlb then
1062 check for type gpr */
1063 if (nFreeRegs (rt) >= nr)
1065 if (nFreeRegs (REG_GPR) >= nr)
1070 if (mcs51_ptrRegReq)
1072 if (nFreeRegs (rt) >= nr)
1077 if (nFreeRegs (REG_PTR) +
1078 nFreeRegs (REG_GPR) >= nr)
1083 /* it will cause a spil */
1087 /*-----------------------------------------------------------------*/
1088 /* positionRegs - the allocator can allocate same registers to res- */
1089 /* ult and operand, if this happens make sure they are in the same */
1090 /* position as the operand otherwise chaos results */
1091 /*-----------------------------------------------------------------*/
1093 positionRegs (symbol * result, symbol * opsym)
1095 int count = min (result->nRegs, opsym->nRegs);
1096 int i, j = 0, shared = 0;
1099 /* if the result has been spilt then cannot share */
1104 /* first make sure that they actually share */
1105 for (i = 0; i < count; i++)
1107 for (j = 0; j < count; j++)
1109 if (result->regs[i] == opsym->regs[j] && i != j)
1119 regs *tmp = result->regs[i];
1120 result->regs[i] = result->regs[j];
1121 result->regs[j] = tmp;
1128 /*------------------------------------------------------------------*/
1129 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1130 /* it should either have registers or have beed spilled. Otherwise, */
1131 /* there was an uninitialized variable, so just spill this to get */
1132 /* the operand in a valid state. */
1133 /*------------------------------------------------------------------*/
1135 verifyRegsAssigned (operand *op, iCode * ic)
1140 if (!IS_ITEMP (op)) return;
1142 sym = OP_SYMBOL (op);
1143 if (sym->isspilt) return;
1144 if (!sym->nRegs) return;
1145 if (sym->regs[0]) return;
1147 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1148 sym->prereqv ? sym->prereqv->name : sym->name);
1153 /*-----------------------------------------------------------------*/
1154 /* serialRegAssign - serially allocate registers to the variables */
1155 /*-----------------------------------------------------------------*/
1157 serialRegAssign (eBBlock ** ebbs, int count)
1161 /* for all blocks */
1162 for (i = 0; i < count; i++)
1167 if (ebbs[i]->noPath &&
1168 (ebbs[i]->entryLabel != entryLabel &&
1169 ebbs[i]->entryLabel != returnLabel))
1172 /* of all instructions do */
1173 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1178 // update the registers in use at the start of this icode
1179 for (reg=0; reg<mcs51_nRegs; reg++) {
1180 if (regs8051[reg].isFree) {
1181 ic->riu &= ~(1<<regs8051[reg].offset);
1183 ic->riu |= (1<<regs8051[reg].offset);
1187 /* if this is an ipop that means some live
1188 range will have to be assigned again */
1190 reassignLR (IC_LEFT (ic));
1192 /* if result is present && is a true symbol */
1193 if (IC_RESULT (ic) && ic->op != IFX &&
1194 IS_TRUE_SYMOP (IC_RESULT (ic)))
1195 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1197 /* take away registers from live
1198 ranges that end at this instruction */
1199 deassignLRs (ic, ebbs[i]);
1201 /* some don't need registers */
1202 if (SKIP_IC2 (ic) ||
1203 ic->op == JUMPTABLE ||
1207 (IC_RESULT (ic) && POINTER_SET (ic)))
1210 /* now we need to allocate registers
1211 only for the result */
1212 if (IC_RESULT (ic)) {
1213 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1219 /* if it does not need or is spilt
1220 or is already assigned to registers
1221 or will not live beyond this instructions */
1224 bitVectBitValue (_G.regAssigned, sym->key) ||
1225 sym->liveTo <= ic->seq)
1228 /* if some liverange has been spilt at the block level
1229 and this one live beyond this block then spil this
1231 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1235 /* if trying to allocate this will cause
1236 a spill and there is nothing to spill
1237 or this one is rematerializable then
1239 willCS = willCauseSpill (sym->nRegs, sym->regType);
1240 spillable = computeSpillable (ic);
1241 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1246 /* If the live range preceeds the point of definition
1247 then ideally we must take into account registers that
1248 have been allocated after sym->liveFrom but freed
1249 before ic->seq. This is complicated, so spill this
1250 symbol instead and let fillGaps handle the allocation. */
1251 if (sym->liveFrom < ic->seq) {
1256 /* if it has a spillocation & is used less than
1257 all other live ranges then spill this */
1259 if (sym->usl.spillLoc) {
1260 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1261 allLRs, ebbs[i], ic));
1262 if (leastUsed && leastUsed->used > sym->used) {
1267 /* if none of the liveRanges have a spillLocation then better
1268 to spill this one than anything else already assigned to registers */
1269 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1270 /* if this is local to this block then we might find a block spil */
1271 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1278 /* if we need ptr regs for the right side
1280 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1281 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1285 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1286 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1290 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1291 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1296 /* else we assign registers to it */
1297 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1298 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1300 for (j = 0; j < sym->nRegs; j++) {
1301 sym->regs[j] = NULL;
1302 if (sym->regType == REG_PTR)
1303 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1306 if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1308 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1311 sym->regs[j] = allocThisReg (right->regs[j]);
1314 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1317 /* if the allocation failed which means
1318 this was spilt then break */
1321 for (i=0; i < sym->nRegs ; i++ )
1322 sym->regs[i] = NULL;
1327 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1328 /* if it shares registers with operands make sure
1329 that they are in the same position */
1330 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1331 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1332 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1333 OP_SYMBOL (IC_LEFT (ic)));
1335 /* do the same for the right operand */
1336 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1337 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1338 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1339 OP_SYMBOL (IC_RIGHT (ic)));
1352 /* Check for and fix any problems with uninitialized operands */
1353 for (i = 0; i < count; i++)
1357 if (ebbs[i]->noPath &&
1358 (ebbs[i]->entryLabel != entryLabel &&
1359 ebbs[i]->entryLabel != returnLabel))
1362 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1369 verifyRegsAssigned (IC_COND (ic), ic);
1373 if (ic->op == JUMPTABLE)
1375 verifyRegsAssigned (IC_JTCOND (ic), ic);
1379 verifyRegsAssigned (IC_RESULT (ic), ic);
1380 verifyRegsAssigned (IC_LEFT (ic), ic);
1381 verifyRegsAssigned (IC_RIGHT (ic), ic);
1386 /*-----------------------------------------------------------------*/
1387 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1388 /*-----------------------------------------------------------------*/
1389 static void fillGaps()
1396 if (getenv("DISABLE_FILL_GAPS")) return;
1398 /* look for liveranges that were spilt by the allocator */
1399 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1400 sym = hTabNextItem(liveRanges,&key)) {
1405 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1407 /* find the liveRanges this one clashes with, that are
1408 still assigned to registers & mark the registers as used*/
1409 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1413 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1414 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1417 clr = hTabItemWithKey(liveRanges,i);
1420 /* mark these registers as used */
1421 for (k = 0 ; k < clr->nRegs ; k++ )
1422 useReg(clr->regs[k]);
1425 if (willCauseSpill(sym->nRegs,sym->regType)) {
1426 /* NOPE :( clear all registers & and continue */
1432 for (i = 0 ; i < sym->defs->size ; i++ )
1434 if (bitVectBitValue(sym->defs,i))
1436 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1443 D(printf("Atemping fillGaps on %s: [",sym->name));
1444 /* THERE IS HOPE !!!! */
1445 for (i=0; i < sym->nRegs ; i++ ) {
1446 if (sym->regType == REG_PTR)
1447 sym->regs[i] = getRegPtrNoSpil ();
1450 sym->regs[i] = NULL;
1451 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1453 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1456 sym->regs[i] = allocThisReg (right->regs[i]);
1459 sym->regs[i] = getRegGprNoSpil ();
1461 D(printf("%s ", sym->regs[i]->name));
1465 /* For all its definitions check if the registers
1466 allocated needs positioning NOTE: we can position
1467 only ONCE if more than One positioning required
1469 We may need to perform the checks twice; once to
1470 position the registers as needed, the second to
1471 verify any register repositioning is still
1475 for (pass=0; pass<2; pass++) {
1476 D(printf(" checking definitions\n"));
1477 for (i = 0 ; i < sym->defs->size ; i++ ) {
1478 if (bitVectBitValue(sym->defs,i)) {
1479 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1480 D(printf(" ic->seq = %d\n", ic->seq));
1481 if (SKIP_IC(ic)) continue;
1482 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1483 /* if left is assigned to registers */
1484 if (IS_SYMOP(IC_LEFT(ic)))
1486 D(printf(" left = "));
1487 D(printOperand(IC_LEFT(ic),NULL));
1489 if (IS_SYMOP(IC_LEFT(ic)) &&
1490 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1491 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1493 if (IS_SYMOP(IC_RIGHT(ic)))
1495 D(printf(" right = "));
1496 D(printOperand(IC_RIGHT(ic),NULL));
1498 if (IS_SYMOP(IC_RIGHT(ic)) &&
1499 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1500 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1502 D(printf(" pdone = %d\n", pdone));
1503 if (pdone > 1) break;
1506 D(printf(" checking uses\n"));
1507 for (i = 0 ; i < sym->uses->size ; i++ ) {
1508 if (bitVectBitValue(sym->uses,i)) {
1510 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1511 D(printf(" ic->seq = %d\n", ic->seq));
1512 if (SKIP_IC(ic)) continue;
1513 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1515 /* if result is assigned to registers */
1516 if (IS_SYMOP(IC_RESULT(ic)))
1518 D(printf(" result = "));
1519 D(printOperand(IC_RESULT(ic),NULL));
1521 if (IS_SYMOP(IC_RESULT(ic)) &&
1522 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1523 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1525 D(printf(" pdone = %d\n", pdone));
1526 if (pdone > 1) break;
1529 if (pdone == 0) break; /* second pass only if regs repositioned */
1530 if (pdone > 1) break;
1532 D(printf(" sym->regs = ["));
1533 for (i=0; i < sym->nRegs ; i++ )
1534 D(printf("%s ", sym->regs[i]->name));
1536 /* had to position more than once GIVE UP */
1538 /* UNDO all the changes we made to try this */
1540 for (i=0; i < sym->nRegs ; i++ ) {
1541 sym->regs[i] = NULL;
1544 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1547 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1549 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1550 sym->isspilt = sym->spillA = 0 ;
1551 sym->usl.spillLoc->allocreq--;
1556 /*-----------------------------------------------------------------*/
1557 /* rUmaskForOp :- returns register mask for an operand */
1558 /*-----------------------------------------------------------------*/
1560 mcs51_rUmaskForOp (operand * op)
1566 /* only temporaries are assigned registers */
1570 sym = OP_SYMBOL (op);
1572 /* if spilt or no registers assigned to it
1574 if (sym->isspilt || !sym->nRegs)
1577 rumask = newBitVect (mcs51_nRegs);
1579 for (j = 0; j < sym->nRegs; j++)
1581 if (sym->regs[j]) /* EEP - debug */
1582 rumask = bitVectSetBit (rumask,
1583 sym->regs[j]->rIdx);
1589 /*-----------------------------------------------------------------*/
1590 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1591 /*-----------------------------------------------------------------*/
1593 regsUsedIniCode (iCode * ic)
1595 bitVect *rmask = newBitVect (mcs51_nRegs);
1597 /* do the special cases first */
1600 rmask = bitVectUnion (rmask,
1601 mcs51_rUmaskForOp (IC_COND (ic)));
1605 /* for the jumptable */
1606 if (ic->op == JUMPTABLE)
1608 rmask = bitVectUnion (rmask,
1609 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1614 /* of all other cases */
1616 rmask = bitVectUnion (rmask,
1617 mcs51_rUmaskForOp (IC_LEFT (ic)));
1621 rmask = bitVectUnion (rmask,
1622 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1625 rmask = bitVectUnion (rmask,
1626 mcs51_rUmaskForOp (IC_RESULT (ic)));
1632 /*-----------------------------------------------------------------*/
1633 /* createRegMask - for each instruction will determine the regsUsed */
1634 /*-----------------------------------------------------------------*/
1636 createRegMask (eBBlock ** ebbs, int count)
1640 /* for all blocks */
1641 for (i = 0; i < count; i++)
1645 if (ebbs[i]->noPath &&
1646 (ebbs[i]->entryLabel != entryLabel &&
1647 ebbs[i]->entryLabel != returnLabel))
1650 /* for all instructions */
1651 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1656 if (SKIP_IC2 (ic) || !ic->rlive)
1659 /* first mark the registers used in this
1661 ic->rUsed = regsUsedIniCode (ic);
1662 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1664 /* now create the register mask for those
1665 registers that are in use : this is a
1666 super set of ic->rUsed */
1667 ic->rMask = newBitVect (mcs51_nRegs + 1);
1669 /* for all live Ranges alive at this point */
1670 for (j = 1; j < ic->rlive->size; j++)
1675 /* if not alive then continue */
1676 if (!bitVectBitValue (ic->rlive, j))
1679 /* find the live range we are interested in */
1680 if (!(sym = hTabItemWithKey (liveRanges, j)))
1682 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1683 "createRegMask cannot find live range");
1684 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1688 /* if no register assigned to it */
1689 if (!sym->nRegs || sym->isspilt)
1692 /* for all the registers allocated to it */
1693 for (k = 0; k < sym->nRegs; k++)
1696 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1702 /*-----------------------------------------------------------------*/
1703 /* rematStr - returns the rematerialized string for a remat var */
1704 /*-----------------------------------------------------------------*/
1706 rematStr (symbol * sym)
1709 iCode *ic = sym->rematiCode;
1716 /* if plus or minus print the right hand side */
1717 if (ic->op == '+' || ic->op == '-')
1719 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1720 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1723 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1727 /* cast then continue */
1728 if (IS_CAST_ICODE(ic)) {
1729 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1732 /* we reached the end */
1733 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1734 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1741 /*-----------------------------------------------------------------*/
1742 /* regTypeNum - computes the type & number of registers required */
1743 /*-----------------------------------------------------------------*/
1745 regTypeNum (eBBlock *ebbs)
1751 /* for each live range do */
1752 for (sym = hTabFirstItem (liveRanges, &k); sym;
1753 sym = hTabNextItem (liveRanges, &k))
1756 /* if used zero times then no registers needed */
1757 if ((sym->liveTo - sym->liveFrom) == 0)
1761 /* if the live range is a temporary */
1765 /* if the type is marked as a conditional */
1766 if (sym->regType == REG_CND)
1769 /* if used in return only then we don't
1771 if (sym->ruonly || sym->accuse)
1773 if (IS_AGGREGATE (sym->type) || sym->isptr)
1774 sym->type = aggrToPtr (sym->type, FALSE);
1778 /* if the symbol has only one definition &
1779 that definition is a get_pointer */
1780 if (bitVectnBitsOn (sym->defs) == 1 &&
1781 (ic = hTabItemWithKey (iCodehTab,
1782 bitVectFirstBit (sym->defs))) &&
1784 !IS_BITVAR (sym->etype) &&
1785 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1788 if (ptrPseudoSymSafe (sym, ic))
1790 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1794 /* if in data space or idata space then try to
1795 allocate pointer register */
1799 /* if not then we require registers */
1800 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1801 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1802 getSize (sym->type));
1806 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1807 printTypeChain (sym->type, stderr);
1808 fprintf (stderr, "\n");
1811 /* determine the type of register required */
1812 if (sym->nRegs == 1 &&
1813 IS_PTR (sym->type) &&
1815 sym->regType = REG_PTR;
1817 sym->regType = REG_GPR;
1821 /* for the first run we don't provide */
1822 /* registers for true symbols we will */
1823 /* see how things go */
1829 /*-----------------------------------------------------------------*/
1830 /* freeAllRegs - mark all registers as free */
1831 /*-----------------------------------------------------------------*/
1837 for (i = 0; i < mcs51_nRegs; i++)
1838 regs8051[i].isFree = 1;
1841 /*-----------------------------------------------------------------*/
1842 /* deallocStackSpil - this will set the stack pointer back */
1843 /*-----------------------------------------------------------------*/
1845 DEFSETFUNC (deallocStackSpil)
1853 /*-----------------------------------------------------------------*/
1854 /* farSpacePackable - returns the packable icode for far variables */
1855 /*-----------------------------------------------------------------*/
1857 farSpacePackable (iCode * ic)
1861 /* go thru till we find a definition for the
1862 symbol on the right */
1863 for (dic = ic->prev; dic; dic = dic->prev)
1865 /* if the definition is a call then no */
1866 if ((dic->op == CALL || dic->op == PCALL) &&
1867 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1872 /* if shift by unknown amount then not */
1873 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1874 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1877 /* if pointer get and size > 1 */
1878 if (POINTER_GET (dic) &&
1879 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1882 if (POINTER_SET (dic) &&
1883 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1888 if (IC_COND (dic) &&
1889 IS_TRUE_SYMOP (IC_COND (dic)) &&
1890 isOperandInFarSpace (IC_COND (dic)))
1893 else if (dic->op == JUMPTABLE)
1895 if (IC_JTCOND (dic) &&
1896 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1897 isOperandInFarSpace (IC_JTCOND (dic)))
1902 /* if any tree is a true symbol in far space */
1903 if (IC_RESULT (dic) &&
1904 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1905 isOperandInFarSpace (IC_RESULT (dic)))
1908 if (IC_RIGHT (dic) &&
1909 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1910 isOperandInFarSpace (IC_RIGHT (dic)) &&
1911 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1914 if (IC_LEFT (dic) &&
1915 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1916 isOperandInFarSpace (IC_LEFT (dic)) &&
1917 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1921 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1923 if ((dic->op == LEFT_OP ||
1924 dic->op == RIGHT_OP ||
1926 IS_OP_LITERAL (IC_RIGHT (dic)))
1936 /*-----------------------------------------------------------------*/
1937 /* packRegsForAssign - register reduction for assignment */
1938 /*-----------------------------------------------------------------*/
1940 packRegsForAssign (iCode * ic, eBBlock * ebp)
1944 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1945 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1946 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1951 /* if the true symbol is defined in far space or on stack
1952 then we should not since this will increase register pressure */
1953 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1957 /* find the definition of iTempNN scanning backwards if we find a
1958 a use of the true symbol in before we find the definition then
1960 for (dic = ic->prev; dic; dic = dic->prev)
1962 int crossedCall = 0;
1964 /* We can pack across a function call only if it's a local */
1965 /* variable or our parameter. Never pack global variables */
1966 /* or parameters to a function we call. */
1967 if ((dic->op == CALL || dic->op == PCALL))
1969 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
1970 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
1981 if (IS_SYMOP (IC_COND (dic)) &&
1982 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1983 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1991 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1992 IS_OP_VOLATILE (IC_RESULT (dic)))
1998 if (IS_SYMOP (IC_RESULT (dic)) &&
1999 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2001 if (POINTER_SET (dic))
2007 if (IS_SYMOP (IC_RIGHT (dic)) &&
2008 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2009 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2015 if (IS_SYMOP (IC_LEFT (dic)) &&
2016 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2017 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2023 if (IS_SYMOP (IC_RESULT (dic)) &&
2024 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2040 return 0; /* did not find */
2042 /* if assignment then check that right is not a bit */
2043 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2045 sym_link *etype = operandType (IC_RESULT (dic));
2046 if (IS_BITFIELD (etype))
2048 /* if result is a bit too then it's ok */
2049 etype = operandType (IC_RESULT (ic));
2050 if (!IS_BITFIELD (etype))
2057 /* if assignment then check that right is not a bit */
2058 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2060 sym_link *etype = operandType (IC_RIGHT (dic));
2061 if (IS_BITFIELD (etype))
2063 /* if result is a bit too then it's ok */
2064 etype = operandType (IC_RESULT (dic));
2065 if (!IS_BITFIELD (etype))
2070 /* if the result is on stack or iaccess then it must be
2071 the same atleast one of the operands */
2072 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2073 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2076 /* the operation has only one symbol
2077 operator then we can pack */
2078 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2079 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2082 if (!((IC_LEFT (dic) &&
2083 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2085 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2089 /* found the definition */
2090 /* replace the result with the result of */
2091 /* this assignment and remove this assignment */
2092 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2093 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2095 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2097 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2099 // TODO: and the otherway around?
2101 /* delete from liverange table also
2102 delete from all the points inbetween and the new
2104 for (sic = dic; sic != ic; sic = sic->next)
2106 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2107 if (IS_ITEMP (IC_RESULT (dic)))
2108 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2111 remiCodeFromeBBlock (ebp, ic);
2112 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2113 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2114 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2118 /*------------------------------------------------------------------*/
2119 /* findAssignToSym : scanning backwards looks for first assig found */
2120 /*------------------------------------------------------------------*/
2122 findAssignToSym (operand * op, iCode * ic)
2126 /* This routine is used to find sequences like
2128 ...; (intervening ops don't use iTempAA or modify FOO)
2129 blah = blah + iTempAA;
2131 and eliminate the use of iTempAA, freeing up its register for
2135 for (dic = ic->prev; dic; dic = dic->prev)
2138 /* if definition by assignment */
2139 if (dic->op == '=' &&
2140 !POINTER_SET (dic) &&
2141 IC_RESULT (dic)->key == op->key
2142 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2144 break; /* found where this temp was defined */
2146 /* if we find an usage then we cannot delete it */
2150 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2153 else if (dic->op == JUMPTABLE)
2155 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2160 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2163 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2166 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2172 return NULL; /* didn't find any assignment to op */
2174 /* we are interested only if defined in far space */
2175 /* or in stack space in case of + & - */
2177 /* if assigned to a non-symbol then don't repack regs */
2178 if (!IS_SYMOP (IC_RIGHT (dic)))
2181 /* if the symbol is volatile then we should not */
2182 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2184 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2185 What does it mean for an iTemp to be volatile, anyway? Passing
2186 TRUE is more cautious but may prevent possible optimizations */
2188 /* if the symbol is in far space then we should not */
2189 if (isOperandInFarSpace (IC_RIGHT (dic)))
2192 /* for + & - operations make sure that
2193 if it is on the stack it is the same
2194 as one of the three operands */
2195 if ((ic->op == '+' || ic->op == '-') &&
2196 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2199 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2200 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2201 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2205 /* now make sure that the right side of dic
2206 is not defined between ic & dic */
2209 iCode *sic = dic->next;
2211 for (; sic != ic; sic = sic->next)
2212 if (IC_RESULT (sic) &&
2213 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2220 /*-----------------------------------------------------------------*/
2221 /* reassignAliasedSym - used by packRegsForSupport to replace */
2222 /* redundant iTemp with equivalent symbol */
2223 /*-----------------------------------------------------------------*/
2225 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2228 unsigned oldSymKey, newSymKey;
2230 oldSymKey = op->key;
2231 newSymKey = IC_RIGHT(assignment)->key;
2233 /* only track live ranges of compiler-generated temporaries */
2234 if (!IS_ITEMP(IC_RIGHT(assignment)))
2237 /* update the live-value bitmaps */
2238 for (ic = assignment; ic != use; ic = ic->next) {
2239 bitVectUnSetBit (ic->rlive, oldSymKey);
2241 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2244 /* update the sym of the used operand */
2245 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2246 op->key = OP_SYMBOL(op)->key;
2247 OP_SYMBOL(op)->accuse = 0;
2249 /* update the sym's liverange */
2250 if ( OP_LIVETO(op) < ic->seq )
2251 setToRange(op, ic->seq, FALSE);
2253 /* remove the assignment iCode now that its result is unused */
2254 remiCodeFromeBBlock (ebp, assignment);
2255 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2256 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2260 /*-----------------------------------------------------------------*/
2261 /* packRegsForSupport :- reduce some registers for support calls */
2262 /*-----------------------------------------------------------------*/
2264 packRegsForSupport (iCode * ic, eBBlock * ebp)
2268 /* for the left & right operand :- look to see if the
2269 left was assigned a true symbol in far space in that
2270 case replace them */
2272 if (IS_ITEMP (IC_LEFT (ic)) &&
2273 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2275 dic = findAssignToSym (IC_LEFT (ic), ic);
2279 /* found it we need to remove it from the block */
2280 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2285 /* do the same for the right operand */
2286 if (IS_ITEMP (IC_RIGHT (ic)) &&
2287 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2289 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2293 /* if this is a subtraction & the result
2294 is a true symbol in far space then don't pack */
2295 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2297 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2298 if (IN_FARSPACE (SPEC_OCLS (etype)))
2301 /* found it we need to remove it from the
2303 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2312 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2315 /*-----------------------------------------------------------------*/
2316 /* packRegsForOneuse : - will reduce some registers for single Use */
2317 /*-----------------------------------------------------------------*/
2319 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2323 /* if returning a literal then do nothing */
2327 /* if rematerializable or already return use then do nothing */
2328 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly)
2331 /* only upto 2 bytes since we cannot predict
2332 the usage of b, & acc */
2333 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2336 if (ic->op != RETURN &&
2338 !POINTER_SET (ic) &&
2342 if (ic->op == SEND && ic->argreg != 1) return NULL;
2344 /* this routine will mark the a symbol as used in one
2345 instruction use only && if the defintion is local
2346 (ie. within the basic block) && has only one definition &&
2347 that definiion is either a return value from a
2348 function or does not contain any variables in
2350 if (bitVectnBitsOn (OP_USES (op)) > 1)
2353 /* if it has only one defintion */
2354 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2355 return NULL; /* has more than one definition */
2357 /* get that definition */
2359 hTabItemWithKey (iCodehTab,
2360 bitVectFirstBit (OP_DEFS (op)))))
2363 /* if that only usage is a cast */
2364 if (dic->op == CAST) {
2365 /* to a bigger type */
2366 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2367 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2368 /* than we can not, since we cannot predict the usage of b & acc */
2373 /* found the definition now check if it is local */
2374 if (dic->seq < ebp->fSeq ||
2375 dic->seq > ebp->lSeq)
2376 return NULL; /* non-local */
2378 /* now check if it is the return from
2380 if (dic->op == CALL || dic->op == PCALL)
2382 if (ic->op != SEND && ic->op != RETURN &&
2383 !POINTER_SET(ic) && !POINTER_GET(ic))
2385 OP_SYMBOL (op)->ruonly = 1;
2391 /* otherwise check that the definition does
2392 not contain any symbols in far space */
2393 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2394 isOperandInFarSpace (IC_RIGHT (dic)) ||
2395 IS_OP_RUONLY (IC_LEFT (ic)) ||
2396 IS_OP_RUONLY (IC_RIGHT (ic)))
2401 /* if pointer set then make sure the pointer
2403 if (POINTER_SET (dic) &&
2404 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2407 if (POINTER_GET (dic) &&
2408 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2412 /* Make sure no overlapping liverange is already assigned to DPTR */
2413 if (OP_SYMBOL(op)->clashes)
2418 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2420 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2422 sym = hTabItemWithKey(liveRanges,i);
2431 /* also make sure the intervenening instructions
2432 don't have any thing in far space */
2433 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2436 /* if there is an intervening function call then no */
2437 if (dic->op == CALL || dic->op == PCALL)
2439 /* if pointer set then make sure the pointer
2441 if (POINTER_SET (dic) &&
2442 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2445 if (POINTER_GET (dic) &&
2446 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2449 /* if address of & the result is remat the okay */
2450 if (dic->op == ADDRESS_OF &&
2451 OP_SYMBOL (IC_RESULT (dic))->remat)
2454 /* if operand has size of three or more & this
2455 operation is a '*','/' or '%' then 'b' may
2457 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2458 getSize (operandType (op)) >= 3)
2461 /* if left or right or result is in far space */
2462 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2463 isOperandInFarSpace (IC_RIGHT (dic)) ||
2464 isOperandInFarSpace (IC_RESULT (dic)) ||
2465 IS_OP_RUONLY (IC_LEFT (dic)) ||
2466 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2467 IS_OP_RUONLY (IC_RESULT (dic)))
2471 /* if left or right or result is on stack */
2472 if (isOperandOnStack(IC_LEFT(dic)) ||
2473 isOperandOnStack(IC_RIGHT(dic)) ||
2474 isOperandOnStack(IC_RESULT(dic))) {
2479 OP_SYMBOL (op)->ruonly = 1;
2483 /*-----------------------------------------------------------------*/
2484 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2485 /*-----------------------------------------------------------------*/
2487 isBitwiseOptimizable (iCode * ic)
2489 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2490 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2492 /* bitwise operations are considered optimizable
2493 under the following conditions (Jean-Louis VERN)
2505 if (IS_LITERAL(rtype) ||
2506 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2512 /*-----------------------------------------------------------------*/
2513 /* isCommutativeOp - tests whether this op cares what order its */
2514 /* operands are in */
2515 /*-----------------------------------------------------------------*/
2516 bool isCommutativeOp(unsigned int op)
2518 if (op == '+' || op == '*' || op == EQ_OP ||
2519 op == '^' || op == '|' || op == BITWISEAND)
2525 /*-----------------------------------------------------------------*/
2526 /* operandUsesAcc - determines whether the code generated for this */
2527 /* operand will have to use the accumulator */
2528 /*-----------------------------------------------------------------*/
2529 bool operandUsesAcc(operand *op)
2535 symbol *sym = OP_SYMBOL(op);
2539 return TRUE; /* duh! */
2541 if (IN_STACK(sym->etype) || sym->onStack ||
2542 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2543 return TRUE; /* acc is used to calc stack offset */
2548 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2550 return FALSE; /* more checks? */
2554 symspace = SPEC_OCLS(sym->etype);
2556 if (sym->iaccess && symspace->paged)
2557 return TRUE; /* must fetch paged indirect sym via accumulator */
2559 if (IN_BITSPACE(symspace))
2560 return TRUE; /* fetching bit vars uses the accumulator */
2562 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2563 return TRUE; /* fetched via accumulator and dptr */
2569 /*-----------------------------------------------------------------*/
2570 /* packRegsForAccUse - pack registers for acc use */
2571 /*-----------------------------------------------------------------*/
2573 packRegsForAccUse (iCode * ic)
2577 /* if this is an aggregate, e.g. a one byte char array */
2578 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2582 /* if we are calling a reentrant function that has stack parameters */
2583 if (ic->op == CALL &&
2584 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2585 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2588 if (ic->op == PCALL &&
2589 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2590 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2593 /* if + or - then it has to be one byte result */
2594 if ((ic->op == '+' || ic->op == '-')
2595 && getSize (operandType (IC_RESULT (ic))) > 1)
2598 /* if shift operation make sure right side is not a literal */
2599 if (ic->op == RIGHT_OP &&
2600 (isOperandLiteral (IC_RIGHT (ic)) ||
2601 getSize (operandType (IC_RESULT (ic))) > 1))
2604 if (ic->op == LEFT_OP &&
2605 (isOperandLiteral (IC_RIGHT (ic)) ||
2606 getSize (operandType (IC_RESULT (ic))) > 1))
2609 if (IS_BITWISE_OP (ic) &&
2610 getSize (operandType (IC_RESULT (ic))) > 1)
2614 /* has only one definition */
2615 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2618 /* has only one use */
2619 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2622 /* and the usage immediately follows this iCode */
2623 if (!(uic = hTabItemWithKey (iCodehTab,
2624 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2627 if (ic->next != uic)
2630 /* if it is a conditional branch then we definitely can */
2634 if (uic->op == JUMPTABLE)
2637 if (POINTER_SET (uic) &&
2638 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2641 /* if the usage is not is an assignment
2642 or an arithmetic / bitwise / shift operation then not */
2643 if (uic->op != '=' &&
2644 !IS_ARITHMETIC_OP (uic) &&
2645 !IS_BITWISE_OP (uic) &&
2646 uic->op != LEFT_OP &&
2647 uic->op != RIGHT_OP)
2650 /* if used in ^ operation then make sure right is not a
2651 literal (WIML: Why is this?) */
2652 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2655 /* if shift operation make sure right side is not a literal */
2656 /* WIML: Why is this? */
2657 if (uic->op == RIGHT_OP &&
2658 (isOperandLiteral (IC_RIGHT (uic)) ||
2659 getSize (operandType (IC_RESULT (uic))) > 1))
2661 if (uic->op == LEFT_OP &&
2662 (isOperandLiteral (IC_RIGHT (uic)) ||
2663 getSize (operandType (IC_RESULT (uic))) > 1))
2666 /* make sure that the result of this icode is not on the
2667 stack, since acc is used to compute stack offset */
2669 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2670 OP_SYMBOL (IC_RESULT (uic))->onStack)
2673 if (isOperandOnStack(IC_RESULT(uic)))
2677 /* if the usage has only one operand then we can */
2678 if (IC_LEFT (uic) == NULL ||
2679 IC_RIGHT (uic) == NULL)
2682 /* if the other operand uses the accumulator then we cannot */
2683 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2684 operandUsesAcc(IC_RIGHT(uic))) ||
2685 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2686 operandUsesAcc(IC_LEFT(uic))) )
2689 /* make sure this is on the left side if not commutative */
2690 /* except for '-', which has been written to be able to
2691 handle reversed operands */
2692 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2693 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2697 // this is too dangerous and need further restrictions
2700 /* if one of them is a literal then we can */
2701 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2702 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2704 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2710 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2714 /*-----------------------------------------------------------------*/
2715 /* packForPush - hueristics to reduce iCode for pushing */
2716 /*-----------------------------------------------------------------*/
2718 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2722 struct eBBlock * ebp=ebpp[blockno];
2724 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2727 /* must have only definition & one usage */
2728 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2729 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2732 /* find the definition */
2733 if (!(dic = hTabItemWithKey (iCodehTab,
2734 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2737 if (dic->op != '=' || POINTER_SET (dic))
2740 if (dic->seq < ebp->fSeq) { // Evelyn did this
2742 for (i=0; i<blockno; i++) {
2743 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2748 wassert (i!=blockno); // no way to recover from here
2751 if (IS_SYMOP(IC_RIGHT(dic))) {
2752 /* make sure the right side does not have any definitions
2754 dbv = OP_DEFS(IC_RIGHT(dic));
2755 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2756 if (bitVectBitValue(dbv,lic->key))
2759 /* make sure they have the same type */
2760 if (IS_SPEC(operandType(IC_LEFT(ic))))
2762 sym_link *itype=operandType(IC_LEFT(ic));
2763 sym_link *ditype=operandType(IC_RIGHT(dic));
2765 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2766 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2769 /* extend the live range of replaced operand if needed */
2770 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2771 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2773 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2776 /* we now we know that it has one & only one def & use
2777 and the that the definition is an assignment */
2778 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2779 remiCodeFromeBBlock (ebp, dic);
2780 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2783 /*-----------------------------------------------------------------*/
2784 /* packRegisters - does some transformations to reduce register */
2786 /*-----------------------------------------------------------------*/
2788 packRegisters (eBBlock ** ebpp, int blockno)
2792 eBBlock *ebp=ebpp[blockno];
2798 /* look for assignments of the form */
2799 /* iTempNN = TRueSym (someoperation) SomeOperand */
2801 /* TrueSym := iTempNN:1 */
2802 for (ic = ebp->sch; ic; ic = ic->next)
2804 /* find assignment of the form TrueSym := iTempNN:1 */
2805 if (ic->op == '=' && !POINTER_SET (ic))
2806 change += packRegsForAssign (ic, ebp);
2813 for (ic = ebp->sch; ic; ic = ic->next)
2816 /* if this is an itemp & result of an address of a true sym
2817 then mark this as rematerialisable */
2818 if (ic->op == ADDRESS_OF &&
2819 IS_ITEMP (IC_RESULT (ic)) &&
2820 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2821 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2822 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2824 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2825 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2826 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2829 /* if straight assignment then carry remat flag if
2830 this is the only definition */
2831 if (ic->op == '=' &&
2832 !POINTER_SET (ic) &&
2833 IS_SYMOP (IC_RIGHT (ic)) &&
2834 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2835 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2836 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2838 OP_SYMBOL (IC_RESULT (ic))->remat =
2839 OP_SYMBOL (IC_RIGHT (ic))->remat;
2840 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2841 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2844 /* if cast to a generic pointer & the pointer being
2845 cast is remat, then we can remat this cast as well */
2846 if (ic->op == CAST &&
2847 IS_SYMOP(IC_RIGHT(ic)) &&
2848 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2849 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2851 sym_link *to_type = operandType(IC_LEFT(ic));
2852 sym_link *from_type = operandType(IC_RIGHT(ic));
2853 if (IS_GENPTR(to_type) && IS_PTR(from_type))
2855 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2856 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2857 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2861 /* if this is a +/- operation with a rematerizable
2862 then mark this as rematerializable as well */
2863 if ((ic->op == '+' || ic->op == '-') &&
2864 (IS_SYMOP (IC_LEFT (ic)) &&
2865 IS_ITEMP (IC_RESULT (ic)) &&
2866 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2867 OP_SYMBOL (IC_LEFT (ic))->remat &&
2868 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2869 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2871 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2872 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2873 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2876 /* mark the pointer usages */
2877 if (POINTER_SET (ic))
2878 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2880 if (POINTER_GET (ic) &&
2881 IS_SYMOP(IC_LEFT (ic)))
2882 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2886 /* if we are using a symbol on the stack
2887 then we should say mcs51_ptrRegReq */
2888 if (options.useXstack && ic->parmPush
2889 && (ic->op == IPUSH || ic->op == IPOP))
2891 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2892 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2893 OP_SYMBOL (IC_COND (ic))->iaccess ||
2894 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
2895 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2896 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2897 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
2898 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
2901 if (IS_SYMOP (IC_LEFT (ic)))
2902 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2903 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
2904 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
2905 if (IS_SYMOP (IC_RIGHT (ic)))
2906 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2907 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
2908 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
2909 if (IS_SYMOP (IC_RESULT (ic)))
2910 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2911 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
2912 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
2913 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
2914 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
2916 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
2917 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
2922 /* if the condition of an if instruction
2923 is defined in the previous instruction and
2924 this is the only usage then
2925 mark the itemp as a conditional */
2926 if ((IS_CONDITIONAL (ic) ||
2927 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic)) ||
2928 (POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1)) &&
2929 ic->next && ic->next->op == IFX &&
2930 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2931 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2932 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2934 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2938 /* if the condition of an if instruction
2939 is defined in the previous GET_POINTER instruction and
2940 this is the only usage then
2941 mark the itemp as accumulator use */
2942 if ((POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1) &&
2943 ic->next && ic->next->op == IFX &&
2944 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2945 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2946 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2948 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2952 /* reduce for support function calls */
2953 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2954 packRegsForSupport (ic, ebp);
2956 /* some cases the redundant moves can
2957 can be eliminated for return statements */
2958 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2959 !isOperandInFarSpace (IC_LEFT (ic)) &&
2960 options.model == MODEL_SMALL) {
2961 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2964 /* if pointer set & left has a size more than
2965 one and right is not in far space */
2966 if (POINTER_SET (ic) &&
2967 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2968 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2969 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2970 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2971 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2973 /* if pointer get */
2974 if (POINTER_GET (ic) &&
2975 IS_SYMOP (IC_LEFT (ic)) &&
2976 !isOperandInFarSpace (IC_RESULT (ic)) &&
2977 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2978 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2979 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2980 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2983 /* if this is cast for intergral promotion then
2984 check if only use of the definition of the
2985 operand being casted/ if yes then replace
2986 the result of that arithmetic operation with
2987 this result and get rid of the cast */
2990 sym_link *fromType = operandType (IC_RIGHT (ic));
2991 sym_link *toType = operandType (IC_LEFT (ic));
2993 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2994 getSize (fromType) != getSize (toType) &&
2995 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2998 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3001 if (IS_ARITHMETIC_OP (dic))
3003 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3004 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3005 remiCodeFromeBBlock (ebp, ic);
3006 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3007 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3008 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3012 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3018 /* if the type from and type to are the same
3019 then if this is the only use then packit */
3020 if (compareType (operandType (IC_RIGHT (ic)),
3021 operandType (IC_LEFT (ic))) == 1)
3023 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3026 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3027 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3028 remiCodeFromeBBlock (ebp, ic);
3029 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3030 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3031 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3039 iTempNN := (some variable in farspace) V1
3044 if (ic->op == IPUSH)
3046 packForPush (ic, ebpp, blockno);
3050 /* pack registers for accumulator use, when the
3051 result of an arithmetic or bit wise operation
3052 has only one use, that use is immediately following
3053 the defintion and the using iCode has only one
3054 operand or has two operands but one is literal &
3055 the result of that operation is not on stack then
3056 we can leave the result of this operation in acc:b
3058 if ((IS_ARITHMETIC_OP (ic)
3059 || IS_CONDITIONAL(ic)
3060 || IS_BITWISE_OP (ic)
3061 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3062 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3064 IS_ITEMP (IC_RESULT (ic)) &&
3065 getSize (operandType (IC_RESULT (ic))) <= 2)
3067 packRegsForAccUse (ic);
3071 /*-----------------------------------------------------------------*/
3072 /* assignRegisters - assigns registers to each live range as need */
3073 /*-----------------------------------------------------------------*/
3075 mcs51_assignRegisters (eBBlock ** ebbs, int count)
3080 setToNull ((void *) &_G.funcrUsed);
3081 setToNull ((void *) &_G.regAssigned);
3082 setToNull ((void *) &_G.totRegAssigned);
3083 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3086 /* change assignments this will remove some
3087 live ranges reducing some register pressure */
3089 for (i = 0; i < count; i++)
3090 packRegisters (ebbs, i);
3092 /* liveranges probably changed by register packing
3093 so we compute them again */
3094 recomputeLiveRanges (ebbs, count);
3096 if (options.dump_pack)
3097 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3099 /* first determine for each live range the number of
3100 registers & the type of registers required for each */
3103 /* and serially allocate registers */
3104 serialRegAssign (ebbs, count);
3107 //setToNull ((void *) &_G.regAssigned);
3108 //setToNull ((void *) &_G.totRegAssigned);
3111 /* if stack was extended then tell the user */
3114 /* werror(W_TOOMANY_SPILS,"stack", */
3115 /* _G.stackExtend,currFunc->name,""); */
3121 /* werror(W_TOOMANY_SPILS,"data space", */
3122 /* _G.dataExtend,currFunc->name,""); */
3126 /* after that create the register mask
3127 for each of the instruction */
3128 createRegMask (ebbs, count);
3130 /* redo that offsets for stacked automatic variables */
3132 redoStackOffsets ();
3135 /* make sure r0 & r1 are flagged as used if they might be used */
3137 if (currFunc && mcs51_ptrRegReq)
3139 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3140 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3143 if (options.dump_rassgn)
3145 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3146 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3149 /* do the overlaysegment stuff SDCCmem.c */
3150 doOverlays (ebbs, count);
3152 /* now get back the chain */
3153 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3157 /* free up any _G.stackSpil locations allocated */
3158 applyToSet (_G.stackSpil, deallocStackSpil);
3160 setToNull ((void *) &_G.stackSpil);
3161 setToNull ((void *) &_G.spiltSet);
3162 /* mark all registers as free */