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 /* mcs51_regWithIdx - returns pointer to register wit index number */
126 /*-----------------------------------------------------------------*/
128 mcs51_regWithIdx (int idx)
132 for (i = 0; i < sizeof(regs8051)/sizeof(regs); i++)
133 if (regs8051[i].rIdx == idx)
136 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
137 "regWithIdx not found");
141 /*-----------------------------------------------------------------*/
142 /* freeReg - frees a register */
143 /*-----------------------------------------------------------------*/
149 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
150 "freeReg - Freeing NULL register");
158 /*-----------------------------------------------------------------*/
159 /* nFreeRegs - returns number of free registers */
160 /*-----------------------------------------------------------------*/
167 for (i = 0; i < mcs51_nRegs; i++)
168 if (regs8051[i].isFree && regs8051[i].type == type)
173 /*-----------------------------------------------------------------*/
174 /* nfreeRegsType - free registers with type */
175 /*-----------------------------------------------------------------*/
177 nfreeRegsType (int type)
182 if ((nfr = nFreeRegs (type)) == 0)
183 return nFreeRegs (REG_GPR);
186 return nFreeRegs (type);
189 /*-----------------------------------------------------------------*/
190 /* useReg - marks a register as used */
191 /*-----------------------------------------------------------------*/
198 /*-----------------------------------------------------------------*/
199 /* computeSpillable - given a point find the spillable live ranges */
200 /*-----------------------------------------------------------------*/
202 computeSpillable (iCode * ic)
206 /* spillable live ranges are those that are live at this
207 point . the following categories need to be subtracted
209 a) - those that are already spilt
210 b) - if being used by this one
211 c) - defined by this one */
213 spillable = bitVectCopy (ic->rlive);
215 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
217 bitVectCplAnd (spillable, ic->uses); /* used in this one */
218 bitVectUnSetBit (spillable, ic->defKey);
219 spillable = bitVectIntersect (spillable, _G.regAssigned);
224 /*-----------------------------------------------------------------*/
225 /* noSpilLoc - return true if a variable has no spil location */
226 /*-----------------------------------------------------------------*/
228 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
230 return (sym->usl.spillLoc ? 0 : 1);
233 /*-----------------------------------------------------------------*/
234 /* hasSpilLoc - will return 1 if the symbol has spil location */
235 /*-----------------------------------------------------------------*/
237 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
239 return (sym->usl.spillLoc ? 1 : 0);
242 /*-----------------------------------------------------------------*/
243 /* directSpilLoc - will return 1 if the splilocation is in direct */
244 /*-----------------------------------------------------------------*/
246 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
248 if (sym->usl.spillLoc &&
249 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
255 /*-----------------------------------------------------------------*/
256 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
257 /* but is not used as a pointer */
258 /*-----------------------------------------------------------------*/
260 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
262 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
265 /*-----------------------------------------------------------------*/
266 /* rematable - will return 1 if the remat flag is set */
267 /*-----------------------------------------------------------------*/
269 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
274 /*-----------------------------------------------------------------*/
275 /* notUsedInRemaining - not used or defined in remain of the block */
276 /*-----------------------------------------------------------------*/
278 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
280 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
281 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
284 /*-----------------------------------------------------------------*/
285 /* allLRs - return true for all */
286 /*-----------------------------------------------------------------*/
288 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
293 /*-----------------------------------------------------------------*/
294 /* liveRangesWith - applies function to a given set of live range */
295 /*-----------------------------------------------------------------*/
297 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
298 eBBlock * ebp, iCode * ic)
303 if (!lrs || !lrs->size)
306 for (i = 1; i < lrs->size; i++)
309 if (!bitVectBitValue (lrs, i))
312 /* if we don't find it in the live range
313 hash table we are in serious trouble */
314 if (!(sym = hTabItemWithKey (liveRanges, i)))
316 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
317 "liveRangesWith could not find liveRange");
321 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
322 addSetHead (&rset, sym);
329 /*-----------------------------------------------------------------*/
330 /* leastUsedLR - given a set determines which is the least used */
331 /*-----------------------------------------------------------------*/
333 leastUsedLR (set * sset)
335 symbol *sym = NULL, *lsym = NULL;
337 sym = lsym = setFirstItem (sset);
342 for (; lsym; lsym = setNextItem (sset))
345 /* if usage is the same then prefer
346 the spill the smaller of the two */
347 if (lsym->used == sym->used)
348 if (getSize (lsym->type) < getSize (sym->type))
352 if (lsym->used < sym->used)
357 setToNull ((void *) &sset);
362 /*-----------------------------------------------------------------*/
363 /* noOverLap - will iterate through the list looking for over lap */
364 /*-----------------------------------------------------------------*/
366 noOverLap (set * itmpStack, symbol * fsym)
371 for (sym = setFirstItem (itmpStack); sym;
372 sym = setNextItem (itmpStack))
374 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
380 /*-----------------------------------------------------------------*/
381 /* isFree - will return 1 if the a free spil location is found */
382 /*-----------------------------------------------------------------*/
387 V_ARG (symbol **, sloc);
388 V_ARG (symbol *, fsym);
390 /* if already found */
394 /* if it is free && and the itmp assigned to
395 this does not have any overlapping live ranges
396 with the one currently being assigned and
397 the size can be accomodated */
399 noOverLap (sym->usl.itmpStack, fsym) &&
400 getSize (sym->type) >= getSize (fsym->type))
409 /*-----------------------------------------------------------------*/
410 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
411 /*-----------------------------------------------------------------*/
413 spillLRWithPtrReg (symbol * forSym)
419 if (!_G.regAssigned ||
420 bitVectIsZero (_G.regAssigned))
423 r0 = mcs51_regWithIdx (R0_IDX);
424 r1 = mcs51_regWithIdx (R1_IDX);
426 /* for all live ranges */
427 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
428 lrsym = hTabNextItem (liveRanges, &k))
432 /* if no registers assigned to it or spilt */
433 /* if it does not overlap with this then
434 not need to spill it */
436 if (lrsym->isspilt || !lrsym->nRegs ||
437 (lrsym->liveTo < forSym->liveFrom))
440 /* go thru the registers : if it is either
441 r0 or r1 then spil it */
442 for (j = 0; j < lrsym->nRegs; j++)
443 if (lrsym->regs[j] == r0 ||
444 lrsym->regs[j] == r1)
453 /*-----------------------------------------------------------------*/
454 /* createStackSpil - create a location on the stack to spil */
455 /*-----------------------------------------------------------------*/
457 createStackSpil (symbol * sym)
460 int useXstack, model;
464 /* first go try and find a free one that is already
465 existing on the stack */
466 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
468 /* found a free one : just update & return */
469 sym->usl.spillLoc = sloc;
472 addSetHead (&sloc->usl.itmpStack, sym);
476 /* could not then have to create one , this is the hard part
477 we need to allocate this on the stack : this is really a
478 hack!! but cannot think of anything better at this time */
480 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
482 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
487 sloc = newiTemp (slocBuffer);
489 /* set the type to the spilling symbol */
490 sloc->type = copyLinkChain (sym->type);
491 sloc->etype = getSpec (sloc->type);
492 SPEC_SCLS (sloc->etype) = S_DATA;
493 SPEC_EXTR (sloc->etype) = 0;
494 SPEC_STAT (sloc->etype) = 0;
495 SPEC_VOLATILE(sloc->etype) = 0;
496 SPEC_ABSA(sloc->etype) = 0;
498 /* we don't allow it to be allocated`
499 onto the external stack since : so we
500 temporarily turn it off ; we also
501 turn off memory model to prevent
502 the spil from going to the external storage
505 useXstack = options.useXstack;
506 model = options.model;
507 /* noOverlay = options.noOverlay; */
508 /* options.noOverlay = 1; */
509 options.model = options.useXstack = 0;
513 options.useXstack = useXstack;
514 options.model = model;
515 /* options.noOverlay = noOverlay; */
516 sloc->isref = 1; /* to prevent compiler warning */
518 /* if it is on the stack then update the stack */
519 if (IN_STACK (sloc->etype))
521 currFunc->stack += getSize (sloc->type);
522 _G.stackExtend += getSize (sloc->type);
525 _G.dataExtend += getSize (sloc->type);
527 /* add it to the _G.stackSpil set */
528 addSetHead (&_G.stackSpil, sloc);
529 sym->usl.spillLoc = sloc;
532 /* add it to the set of itempStack set
533 of the spill location */
534 addSetHead (&sloc->usl.itmpStack, sym);
538 /*-----------------------------------------------------------------*/
539 /* isSpiltOnStack - returns true if the spil location is on stack */
540 /*-----------------------------------------------------------------*/
542 isSpiltOnStack (symbol * sym)
552 /* if (sym->_G.stackSpil) */
555 if (!sym->usl.spillLoc)
558 etype = getSpec (sym->usl.spillLoc->type);
559 if (IN_STACK (etype))
565 /*-----------------------------------------------------------------*/
566 /* spillThis - spils a specific operand */
567 /*-----------------------------------------------------------------*/
569 spillThis (symbol * sym)
572 /* if this is rematerializable or has a spillLocation
573 we are okay, else we need to create a spillLocation
575 if (!(sym->remat || sym->usl.spillLoc))
576 createStackSpil (sym);
578 /* mark it has spilt & put it in the spilt set */
579 sym->isspilt = sym->spillA = 1;
580 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
582 bitVectUnSetBit (_G.regAssigned, sym->key);
583 bitVectUnSetBit (_G.totRegAssigned, sym->key);
585 for (i = 0; i < sym->nRegs; i++)
589 freeReg (sym->regs[i]);
593 /* if spilt on stack then free up r0 & r1
594 if they could have been assigned to some
596 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
599 spillLRWithPtrReg (sym);
602 if (sym->usl.spillLoc && !sym->remat)
603 sym->usl.spillLoc->allocreq++;
607 /*-----------------------------------------------------------------*/
608 /* selectSpil - select a iTemp to spil : rather a simple procedure */
609 /*-----------------------------------------------------------------*/
611 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
613 bitVect *lrcs = NULL;
617 /* get the spillable live ranges */
618 lrcs = computeSpillable (ic);
620 /* get all live ranges that are rematerizable */
621 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
624 /* return the least used of these */
625 return leastUsedLR (selectS);
628 /* get live ranges with spillLocations in direct space */
629 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
631 sym = leastUsedLR (selectS);
632 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
633 sym->usl.spillLoc->rname :
634 sym->usl.spillLoc->name));
636 /* mark it as allocation required */
637 sym->usl.spillLoc->allocreq++;
641 /* if the symbol is local to the block then */
642 if (forSym->liveTo < ebp->lSeq)
645 /* check if there are any live ranges allocated
646 to registers that are not used in this block */
647 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
649 sym = leastUsedLR (selectS);
650 /* if this is not rematerializable */
659 /* check if there are any live ranges that not
660 used in the remainder of the block */
661 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
663 sym = leastUsedLR (selectS);
676 /* find live ranges with spillocation && not used as pointers */
677 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
680 sym = leastUsedLR (selectS);
681 /* mark this as allocation required */
682 sym->usl.spillLoc->allocreq++;
686 /* find live ranges with spillocation */
687 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
690 sym = leastUsedLR (selectS);
691 sym->usl.spillLoc->allocreq++;
695 /* couldn't find then we need to create a spil
696 location on the stack , for which one? the least
698 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
701 /* return a created spil location */
702 sym = createStackSpil (leastUsedLR (selectS));
703 sym->usl.spillLoc->allocreq++;
707 /* this is an extreme situation we will spill
708 this one : happens very rarely but it does happen */
714 /*-----------------------------------------------------------------*/
715 /* spilSomething - spil some variable & mark registers as free */
716 /*-----------------------------------------------------------------*/
718 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
723 /* get something we can spil */
724 ssym = selectSpil (ic, ebp, forSym);
726 /* mark it as spilt */
727 ssym->isspilt = ssym->spillA = 1;
728 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
730 /* mark it as not register assigned &
731 take it away from the set */
732 bitVectUnSetBit (_G.regAssigned, ssym->key);
733 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
735 /* mark the registers as free */
736 for (i = 0; i < ssym->nRegs; i++)
738 freeReg (ssym->regs[i]);
740 /* if spilt on stack then free up r0 & r1
741 if they could have been assigned to as gprs */
742 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
745 spillLRWithPtrReg (ssym);
748 /* if this was a block level spil then insert push & pop
749 at the start & end of block respectively */
752 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
753 /* add push to the start of the block */
754 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
755 ebp->sch->next : ebp->sch));
756 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
757 /* add pop to the end of the block */
758 addiCodeToeBBlock (ebp, nic, NULL);
761 /* if spilt because not used in the remainder of the
762 block then add a push before this instruction and
763 a pop at the end of the block */
764 if (ssym->remainSpil)
767 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
768 /* add push just before this instruction */
769 addiCodeToeBBlock (ebp, nic, ic);
771 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
772 /* add pop to the end of the block */
773 addiCodeToeBBlock (ebp, nic, NULL);
782 /*-----------------------------------------------------------------*/
783 /* getRegPtr - will try for PTR if not a GPR type if not spil */
784 /*-----------------------------------------------------------------*/
786 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
792 /* try for a ptr type */
793 if ((reg = allocReg (REG_PTR)))
796 /* try for gpr type */
797 if ((reg = allocReg (REG_GPR)))
800 /* we have to spil */
801 if (!spilSomething (ic, ebp, sym))
804 /* make sure partially assigned registers aren't reused */
805 for (j=0; j<=sym->nRegs; j++)
807 sym->regs[j]->isFree = 0;
809 /* this looks like an infinite loop but
810 in really selectSpil will abort */
814 /*-----------------------------------------------------------------*/
815 /* getRegGpr - will try for GPR if not spil */
816 /*-----------------------------------------------------------------*/
818 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
824 /* try for gpr type */
825 if ((reg = allocReg (REG_GPR)))
828 if (!mcs51_ptrRegReq)
829 if ((reg = allocReg (REG_PTR)))
832 /* we have to spil */
833 if (!spilSomething (ic, ebp, sym))
836 /* make sure partially assigned registers aren't reused */
837 for (j=0; j<=sym->nRegs; j++)
839 sym->regs[j]->isFree = 0;
841 /* this looks like an infinite loop but
842 in really selectSpil will abort */
846 /*-----------------------------------------------------------------*/
847 /* getRegPtrNoSpil - get it cannot split */
848 /*-----------------------------------------------------------------*/
849 static regs *getRegPtrNoSpil()
853 /* try for a ptr type */
854 if ((reg = allocReg (REG_PTR)))
857 /* try for gpr type */
858 if ((reg = allocReg (REG_GPR)))
863 /* just to make the compiler happy */
867 /*-----------------------------------------------------------------*/
868 /* getRegGprNoSpil - get it cannot split */
869 /*-----------------------------------------------------------------*/
870 static regs *getRegGprNoSpil()
874 if ((reg = allocReg (REG_GPR)))
877 if (!mcs51_ptrRegReq)
878 if ((reg = allocReg (REG_PTR)))
883 /* just to make the compiler happy */
887 /*-----------------------------------------------------------------*/
888 /* symHasReg - symbol has a given register */
889 /*-----------------------------------------------------------------*/
891 symHasReg (symbol * sym, regs * reg)
895 for (i = 0; i < sym->nRegs; i++)
896 if (sym->regs[i] == reg)
902 /*-----------------------------------------------------------------*/
903 /* deassignLRs - check the live to and if they have registers & are */
904 /* not spilt then free up the registers */
905 /*-----------------------------------------------------------------*/
907 deassignLRs (iCode * ic, eBBlock * ebp)
913 for (sym = hTabFirstItem (liveRanges, &k); sym;
914 sym = hTabNextItem (liveRanges, &k))
918 /* if it does not end here */
919 if (sym->liveTo > ic->seq)
922 /* if it was spilt on stack then we can
923 mark the stack spil location as free */
928 sym->usl.spillLoc->isFree = 1;
934 if (!bitVectBitValue (_G.regAssigned, sym->key))
937 /* special case check if this is an IFX &
938 the privious one was a pop and the
939 previous one was not spilt then keep track
941 if (ic->op == IFX && ic->prev &&
942 ic->prev->op == IPOP &&
943 !ic->prev->parmPush &&
944 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
945 psym = OP_SYMBOL (IC_LEFT (ic->prev));
951 bitVectUnSetBit (_G.regAssigned, sym->key);
953 /* if the result of this one needs registers
954 and does not have it then assign it right
956 if (IC_RESULT (ic) &&
957 !(SKIP_IC2 (ic) || /* not a special icode */
958 ic->op == JUMPTABLE ||
964 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
965 result->liveTo > ic->seq && /* and will live beyond this */
966 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
967 result->regType == sym->regType && /* same register types */
968 result->nRegs && /* which needs registers */
969 !result->isspilt && /* and does not already have them */
971 !bitVectBitValue (_G.regAssigned, result->key) &&
972 /* the number of free regs + number of regs in this LR
973 can accomodate the what result Needs */
974 ((nfreeRegsType (result->regType) +
975 sym->nRegs) >= result->nRegs)
979 for (i = 0; i < result->nRegs; i++)
981 result->regs[i] = sym->regs[i];
983 result->regs[i] = getRegGpr (ic, ebp, result);
985 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
986 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
990 /* free the remaining */
991 for (; i < sym->nRegs; i++)
995 if (!symHasReg (psym, sym->regs[i]))
996 freeReg (sym->regs[i]);
999 freeReg (sym->regs[i]);
1006 /*-----------------------------------------------------------------*/
1007 /* reassignLR - reassign this to registers */
1008 /*-----------------------------------------------------------------*/
1010 reassignLR (operand * op)
1012 symbol *sym = OP_SYMBOL (op);
1015 /* not spilt any more */
1016 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1017 bitVectUnSetBit (_G.spiltSet, sym->key);
1019 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1020 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1024 for (i = 0; i < sym->nRegs; i++)
1025 sym->regs[i]->isFree = 0;
1028 /*-----------------------------------------------------------------*/
1029 /* willCauseSpill - determines if allocating will cause a spill */
1030 /*-----------------------------------------------------------------*/
1032 willCauseSpill (int nr, int rt)
1034 /* first check if there are any avlb registers
1035 of te type required */
1038 /* special case for pointer type
1039 if pointer type not avlb then
1040 check for type gpr */
1041 if (nFreeRegs (rt) >= nr)
1043 if (nFreeRegs (REG_GPR) >= nr)
1048 if (mcs51_ptrRegReq)
1050 if (nFreeRegs (rt) >= nr)
1055 if (nFreeRegs (REG_PTR) +
1056 nFreeRegs (REG_GPR) >= nr)
1061 /* it will cause a spil */
1065 /*-----------------------------------------------------------------*/
1066 /* positionRegs - the allocator can allocate same registers to res- */
1067 /* ult and operand, if this happens make sure they are in the same */
1068 /* position as the operand otherwise chaos results */
1069 /*-----------------------------------------------------------------*/
1071 positionRegs (symbol * result, symbol * opsym)
1073 int count = min (result->nRegs, opsym->nRegs);
1074 int i, j = 0, shared = 0;
1077 /* if the result has been spilt then cannot share */
1082 /* first make sure that they actually share */
1083 for (i = 0; i < count; i++)
1085 for (j = 0; j < count; j++)
1087 if (result->regs[i] == opsym->regs[j] && i != j)
1097 regs *tmp = result->regs[i];
1098 result->regs[i] = result->regs[j];
1099 result->regs[j] = tmp;
1107 /*------------------------------------------------------------------*/
1108 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1109 /* it should either have registers or have beed spilled. Otherwise, */
1110 /* there was an uninitialized variable, so just spill this to get */
1111 /* the operand in a valid state. */
1112 /*------------------------------------------------------------------*/
1114 verifyRegsAssigned (operand *op, iCode * ic)
1119 if (!IS_ITEMP (op)) return;
1121 sym = OP_SYMBOL (op);
1122 if (sym->isspilt) return;
1123 if (!sym->nRegs) return;
1124 if (sym->regs[0]) return;
1126 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1127 sym->prereqv ? sym->prereqv->name : sym->name);
1133 /*-----------------------------------------------------------------*/
1134 /* serialRegAssign - serially allocate registers to the variables */
1135 /*-----------------------------------------------------------------*/
1137 serialRegAssign (eBBlock ** ebbs, int count)
1141 /* for all blocks */
1142 for (i = 0; i < count; i++) {
1146 if (ebbs[i]->noPath &&
1147 (ebbs[i]->entryLabel != entryLabel &&
1148 ebbs[i]->entryLabel != returnLabel))
1151 /* of all instructions do */
1152 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1156 // update the registers in use at the start of this icode
1157 for (reg=0; reg<mcs51_nRegs; reg++) {
1158 if (regs8051[reg].isFree) {
1159 ic->riu &= ~(1<<regs8051[reg].offset);
1161 ic->riu |= (1<<regs8051[reg].offset);
1166 /* if this is an ipop that means some live
1167 range will have to be assigned again */
1169 reassignLR (IC_LEFT (ic));
1171 /* if result is present && is a true symbol */
1172 if (IC_RESULT (ic) && ic->op != IFX &&
1173 IS_TRUE_SYMOP (IC_RESULT (ic)))
1174 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1176 /* take away registers from live
1177 ranges that end at this instruction */
1178 deassignLRs (ic, ebbs[i]);
1180 /* some don't need registers */
1181 if (SKIP_IC2 (ic) ||
1182 ic->op == JUMPTABLE ||
1186 (IC_RESULT (ic) && POINTER_SET (ic)))
1189 /* now we need to allocate registers
1190 only for the result */
1191 if (IC_RESULT (ic)) {
1192 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1198 /* if it does not need or is spilt
1199 or is already assigned to registers
1200 or will not live beyond this instructions */
1203 bitVectBitValue (_G.regAssigned, sym->key) ||
1204 sym->liveTo <= ic->seq)
1207 /* if some liverange has been spilt at the block level
1208 and this one live beyond this block then spil this
1210 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1214 /* if trying to allocate this will cause
1215 a spill and there is nothing to spill
1216 or this one is rematerializable then
1218 willCS = willCauseSpill (sym->nRegs, sym->regType);
1219 spillable = computeSpillable (ic);
1220 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1225 /* if it has a spillocation & is used less than
1226 all other live ranges then spill this */
1228 if (sym->usl.spillLoc) {
1229 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1230 allLRs, ebbs[i], ic));
1231 if (leastUsed && leastUsed->used > sym->used) {
1236 /* if none of the liveRanges have a spillLocation then better
1237 to spill this one than anything else already assigned to registers */
1238 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1239 /* if this is local to this block then we might find a block spil */
1240 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1247 /* if we need ptr regs for the right side
1249 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1250 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1254 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1255 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1259 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1260 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1265 /* else we assign registers to it */
1266 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1267 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1269 for (j = 0; j < sym->nRegs; j++) {
1270 if (sym->regType == REG_PTR)
1271 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1273 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1275 /* if the allocation failed which means
1276 this was spilt then break */
1277 if (!sym->regs[j]) {
1282 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1283 /* if it shares registers with operands make sure
1284 that they are in the same position */
1285 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1286 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1287 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1288 OP_SYMBOL (IC_LEFT (ic)));
1290 /* do the same for the right operand */
1291 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1292 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1293 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1294 OP_SYMBOL (IC_RIGHT (ic)));
1307 /* Check for and fix any problems with uninitialized operands */
1308 for (i = 0; i < count; i++)
1312 if (ebbs[i]->noPath &&
1313 (ebbs[i]->entryLabel != entryLabel &&
1314 ebbs[i]->entryLabel != returnLabel))
1317 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1324 verifyRegsAssigned (IC_COND (ic), ic);
1328 if (ic->op == JUMPTABLE)
1330 verifyRegsAssigned (IC_JTCOND (ic), ic);
1334 verifyRegsAssigned (IC_RESULT (ic), ic);
1335 verifyRegsAssigned (IC_LEFT (ic), ic);
1336 verifyRegsAssigned (IC_RIGHT (ic), ic);
1341 /*-----------------------------------------------------------------*/
1342 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1343 /*-----------------------------------------------------------------*/
1344 static void fillGaps()
1350 if (getenv("DISABLE_FILL_GAPS")) return;
1352 /* look for livernages that was spilt by the allocator */
1353 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1354 sym = hTabNextItem(liveRanges,&key)) {
1359 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1361 /* find the liveRanges this one clashes with, that are
1362 still assigned to registers & mark the registers as used*/
1363 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1367 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1368 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1371 clr = hTabItemWithKey(liveRanges,i);
1374 /* mark these registers as used */
1375 for (k = 0 ; k < clr->nRegs ; k++ )
1376 useReg(clr->regs[k]);
1379 if (willCauseSpill(sym->nRegs,sym->regType)) {
1380 /* NOPE :( clear all registers & and continue */
1385 D(printf("Atemping fillGaps on %s: [",sym->name));
1386 /* THERE IS HOPE !!!! */
1387 for (i=0; i < sym->nRegs ; i++ ) {
1388 if (sym->regType == REG_PTR)
1389 sym->regs[i] = getRegPtrNoSpil ();
1391 sym->regs[i] = getRegGprNoSpil ();
1392 D(printf("%s ", sym->regs[i]->name));
1396 /* For all its definitions check if the registers
1397 allocated needs positioning NOTE: we can position
1398 only ONCE if more than One positioning required
1400 We may need to perform the checks twice; once to
1401 position the registers as needed, the second to
1402 verify any register repositioning is still
1406 for (pass=0; pass<2; pass++) {
1407 D(printf(" checking definitions\n"));
1408 for (i = 0 ; i < sym->defs->size ; i++ ) {
1409 if (bitVectBitValue(sym->defs,i)) {
1411 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1412 D(printf(" ic->seq = %d\n", ic->seq));
1413 if (SKIP_IC(ic)) continue;
1414 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1415 /* if left is assigned to registers */
1416 if (IS_SYMOP(IC_LEFT(ic)))
1418 D(printf(" left = "));
1419 D(printOperand(IC_LEFT(ic),NULL));
1421 if (IS_SYMOP(IC_LEFT(ic)) &&
1422 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1423 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1425 if (IS_SYMOP(IC_RIGHT(ic)))
1427 D(printf(" right = "));
1428 D(printOperand(IC_RIGHT(ic),NULL));
1430 if (IS_SYMOP(IC_RIGHT(ic)) &&
1431 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1432 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1434 D(printf(" pdone = %d\n", pdone));
1435 if (pdone > 1) break;
1438 D(printf(" checking uses\n"));
1439 for (i = 0 ; i < sym->uses->size ; i++ ) {
1440 if (bitVectBitValue(sym->uses,i)) {
1442 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1443 D(printf(" ic->seq = %d\n", ic->seq));
1444 if (SKIP_IC(ic)) continue;
1445 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1447 /* if result is assigned to registers */
1448 if (IS_SYMOP(IC_RESULT(ic)))
1450 D(printf(" result = "));
1451 D(printOperand(IC_RESULT(ic),NULL));
1453 if (IS_SYMOP(IC_RESULT(ic)) &&
1454 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1455 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1457 D(printf(" pdone = %d\n", pdone));
1458 if (pdone > 1) break;
1461 if (pdone == 0) break; /* second pass only if regs repositioned */
1462 if (pdone > 1) break;
1464 D(printf(" sym->regs = ["));
1465 for (i=0; i < sym->nRegs ; i++ )
1466 D(printf("%s ", sym->regs[i]->name));
1468 /* had to position more than once GIVE UP */
1470 /* UNDO all the changes we made to try this */
1472 for (i=0; i < sym->nRegs ; i++ ) {
1473 sym->regs[i] = NULL;
1476 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1479 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1480 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1481 sym->isspilt = sym->spillA = 0 ;
1482 sym->usl.spillLoc->allocreq--;
1487 /*-----------------------------------------------------------------*/
1488 /* rUmaskForOp :- returns register mask for an operand */
1489 /*-----------------------------------------------------------------*/
1491 mcs51_rUmaskForOp (operand * op)
1497 /* only temporaries are assigned registers */
1501 sym = OP_SYMBOL (op);
1503 /* if spilt or no registers assigned to it
1505 if (sym->isspilt || !sym->nRegs)
1508 rumask = newBitVect (mcs51_nRegs);
1510 for (j = 0; j < sym->nRegs; j++)
1512 if (sym->regs[j]) /* EEP - debug */
1513 rumask = bitVectSetBit (rumask,
1514 sym->regs[j]->rIdx);
1520 /*-----------------------------------------------------------------*/
1521 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1522 /*-----------------------------------------------------------------*/
1524 regsUsedIniCode (iCode * ic)
1526 bitVect *rmask = newBitVect (mcs51_nRegs);
1528 /* do the special cases first */
1531 rmask = bitVectUnion (rmask,
1532 mcs51_rUmaskForOp (IC_COND (ic)));
1536 /* for the jumptable */
1537 if (ic->op == JUMPTABLE)
1539 rmask = bitVectUnion (rmask,
1540 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1545 /* of all other cases */
1547 rmask = bitVectUnion (rmask,
1548 mcs51_rUmaskForOp (IC_LEFT (ic)));
1552 rmask = bitVectUnion (rmask,
1553 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1556 rmask = bitVectUnion (rmask,
1557 mcs51_rUmaskForOp (IC_RESULT (ic)));
1563 /*-----------------------------------------------------------------*/
1564 /* createRegMask - for each instruction will determine the regsUsed */
1565 /*-----------------------------------------------------------------*/
1567 createRegMask (eBBlock ** ebbs, int count)
1571 /* for all blocks */
1572 for (i = 0; i < count; i++)
1576 if (ebbs[i]->noPath &&
1577 (ebbs[i]->entryLabel != entryLabel &&
1578 ebbs[i]->entryLabel != returnLabel))
1581 /* for all instructions */
1582 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1587 if (SKIP_IC2 (ic) || !ic->rlive)
1590 /* first mark the registers used in this
1592 ic->rUsed = regsUsedIniCode (ic);
1593 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1595 /* now create the register mask for those
1596 registers that are in use : this is a
1597 super set of ic->rUsed */
1598 ic->rMask = newBitVect (mcs51_nRegs + 1);
1600 /* for all live Ranges alive at this point */
1601 for (j = 1; j < ic->rlive->size; j++)
1606 /* if not alive then continue */
1607 if (!bitVectBitValue (ic->rlive, j))
1610 /* find the live range we are interested in */
1611 if (!(sym = hTabItemWithKey (liveRanges, j)))
1613 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1614 "createRegMask cannot find live range");
1615 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1619 /* if no register assigned to it */
1620 if (!sym->nRegs || sym->isspilt)
1623 /* for all the registers allocated to it */
1624 for (k = 0; k < sym->nRegs; k++)
1627 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1633 /*-----------------------------------------------------------------*/
1634 /* rematStr - returns the rematerialized string for a remat var */
1635 /*-----------------------------------------------------------------*/
1637 rematStr (symbol * sym)
1640 iCode *ic = sym->rematiCode;
1645 /* if plus or minus print the right hand side */
1646 if (ic->op == '+' || ic->op == '-')
1648 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1651 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1655 /* cast then continue */
1656 if (IS_CAST_ICODE(ic)) {
1657 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1660 /* we reached the end */
1661 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1668 /*-----------------------------------------------------------------*/
1669 /* regTypeNum - computes the type & number of registers required */
1670 /*-----------------------------------------------------------------*/
1672 regTypeNum (eBBlock *ebbs)
1678 /* for each live range do */
1679 for (sym = hTabFirstItem (liveRanges, &k); sym;
1680 sym = hTabNextItem (liveRanges, &k))
1683 /* if used zero times then no registers needed */
1684 if ((sym->liveTo - sym->liveFrom) == 0)
1688 /* if the live range is a temporary */
1692 /* if the type is marked as a conditional */
1693 if (sym->regType == REG_CND)
1696 /* if used in return only then we don't
1698 if (sym->ruonly || sym->accuse)
1700 if (IS_AGGREGATE (sym->type) || sym->isptr)
1701 sym->type = aggrToPtr (sym->type, FALSE);
1705 /* if the symbol has only one definition &
1706 that definition is a get_pointer */
1707 if (bitVectnBitsOn (sym->defs) == 1 &&
1708 (ic = hTabItemWithKey (iCodehTab,
1709 bitVectFirstBit (sym->defs))) &&
1712 !IS_BITVAR (sym->etype))
1716 /* and that pointer is remat in data space */
1717 if (IS_SYMOP (IC_LEFT (ic)) &&
1718 OP_SYMBOL (IC_LEFT (ic))->remat &&
1719 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1720 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1722 /* create a psuedo symbol & force a spil */
1723 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1724 psym->type = sym->type;
1725 psym->etype = sym->etype;
1727 strcpy (psym->rname, psym->name);
1729 sym->usl.spillLoc = psym;
1730 #if 0 // an alternative fix for bug #480076
1731 /* now this is a useless assignment to itself */
1732 remiCodeFromeBBlock (ebbs, ic);
1734 /* now this really is an assignment to itself, make it so;
1735 it will be optimized out later */
1737 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), IC_RESULT(ic));
1743 /* if in data space or idata space then try to
1744 allocate pointer register */
1748 /* if not then we require registers */
1749 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1750 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1751 getSize (sym->type));
1755 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1756 printTypeChain (sym->type, stderr);
1757 fprintf (stderr, "\n");
1760 /* determine the type of register required */
1761 if (sym->nRegs == 1 &&
1762 IS_PTR (sym->type) &&
1764 sym->regType = REG_PTR;
1766 sym->regType = REG_GPR;
1770 /* for the first run we don't provide */
1771 /* registers for true symbols we will */
1772 /* see how things go */
1778 /*-----------------------------------------------------------------*/
1779 /* freeAllRegs - mark all registers as free */
1780 /*-----------------------------------------------------------------*/
1786 for (i = 0; i < mcs51_nRegs; i++)
1787 regs8051[i].isFree = 1;
1790 /*-----------------------------------------------------------------*/
1791 /* deallocStackSpil - this will set the stack pointer back */
1792 /*-----------------------------------------------------------------*/
1794 DEFSETFUNC (deallocStackSpil)
1802 /*-----------------------------------------------------------------*/
1803 /* farSpacePackable - returns the packable icode for far variables */
1804 /*-----------------------------------------------------------------*/
1806 farSpacePackable (iCode * ic)
1810 /* go thru till we find a definition for the
1811 symbol on the right */
1812 for (dic = ic->prev; dic; dic = dic->prev)
1814 /* if the definition is a call then no */
1815 if ((dic->op == CALL || dic->op == PCALL) &&
1816 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1821 /* if shift by unknown amount then not */
1822 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1823 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1826 /* if pointer get and size > 1 */
1827 if (POINTER_GET (dic) &&
1828 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1831 if (POINTER_SET (dic) &&
1832 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1837 if (IC_COND (dic) &&
1838 IS_TRUE_SYMOP (IC_COND (dic)) &&
1839 isOperandInFarSpace (IC_COND (dic)))
1842 else if (dic->op == JUMPTABLE)
1844 if (IC_JTCOND (dic) &&
1845 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1846 isOperandInFarSpace (IC_JTCOND (dic)))
1851 /* if any three is a true symbol in far space */
1852 if (IC_RESULT (dic) &&
1853 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1854 isOperandInFarSpace (IC_RESULT (dic)))
1857 if (IC_RIGHT (dic) &&
1858 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1859 isOperandInFarSpace (IC_RIGHT (dic)) &&
1860 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1863 if (IC_LEFT (dic) &&
1864 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1865 isOperandInFarSpace (IC_LEFT (dic)) &&
1866 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1870 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1872 if ((dic->op == LEFT_OP ||
1873 dic->op == RIGHT_OP ||
1875 IS_OP_LITERAL (IC_RIGHT (dic)))
1885 /*-----------------------------------------------------------------*/
1886 /* packRegsForAssign - register reduction for assignment */
1887 /*-----------------------------------------------------------------*/
1889 packRegsForAssign (iCode * ic, eBBlock * ebp)
1893 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1894 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1895 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1900 /* if the true symbol is defined in far space or on stack
1901 then we should not since this will increase register pressure */
1902 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1906 /* find the definition of iTempNN scanning backwards if we find a
1907 a use of the true symbol in before we find the definition then
1909 for (dic = ic->prev; dic; dic = dic->prev)
1911 int crossedCall = 0;
1913 /* We can pack across a function call only if it's a local */
1914 /* variable or our parameter. Never pack global variables */
1915 /* or parameters to a function we call. */
1916 if ((dic->op == CALL || dic->op == PCALL))
1918 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
1919 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
1930 if (IS_SYMOP (IC_COND (dic)) &&
1931 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1932 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1940 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1941 IS_OP_VOLATILE (IC_RESULT (dic)))
1947 if (IS_SYMOP (IC_RESULT (dic)) &&
1948 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1950 if (POINTER_SET (dic))
1956 if (IS_SYMOP (IC_RIGHT (dic)) &&
1957 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1958 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1964 if (IS_SYMOP (IC_LEFT (dic)) &&
1965 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1966 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1972 if (IS_SYMOP (IC_RESULT (dic)) &&
1973 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1989 return 0; /* did not find */
1991 /* if assignment then check that right is not a bit */
1992 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1994 sym_link *etype = operandType (IC_RIGHT (dic));
1995 if (IS_BITFIELD (etype))
1997 /* if result is a bit too then it's ok */
1998 etype = operandType (IC_RESULT (dic));
1999 if (!IS_BITFIELD (etype))
2003 /* if the result is on stack or iaccess then it must be
2004 the same atleast one of the operands */
2005 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2006 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2009 /* the operation has only one symbol
2010 operator then we can pack */
2011 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2012 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2015 if (!((IC_LEFT (dic) &&
2016 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2018 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2022 /* found the definition */
2023 /* replace the result with the result of */
2024 /* this assignment and remove this assignment */
2025 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2026 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2028 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2030 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2032 // TODO: and the otherway around?
2034 /* delete from liverange table also
2035 delete from all the points inbetween and the new
2037 for (sic = dic; sic != ic; sic = sic->next)
2039 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2040 if (IS_ITEMP (IC_RESULT (dic)))
2041 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2044 remiCodeFromeBBlock (ebp, ic);
2045 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2046 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2047 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2051 /*------------------------------------------------------------------*/
2052 /* findAssignToSym : scanning backwards looks for first assig found */
2053 /*------------------------------------------------------------------*/
2055 findAssignToSym (operand * op, iCode * ic)
2059 /* This routine is used to find sequences like
2061 ...; (intervening ops don't use iTempAA or modify FOO)
2062 blah = blah + iTempAA;
2064 and eliminate the use of iTempAA, freeing up its register for
2068 for (dic = ic->prev; dic; dic = dic->prev)
2071 /* if definition by assignment */
2072 if (dic->op == '=' &&
2073 !POINTER_SET (dic) &&
2074 IC_RESULT (dic)->key == op->key
2075 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2077 break; /* found where this temp was defined */
2079 /* if we find an usage then we cannot delete it */
2083 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2086 else if (dic->op == JUMPTABLE)
2088 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2093 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2096 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2099 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2105 return NULL; /* didn't find any assignment to op */
2107 /* we are interested only if defined in far space */
2108 /* or in stack space in case of + & - */
2110 /* if assigned to a non-symbol then don't repack regs */
2111 if (!IS_SYMOP (IC_RIGHT (dic)))
2114 /* if the symbol is volatile then we should not */
2115 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2117 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2118 What does it mean for an iTemp to be volatile, anyway? Passing
2119 TRUE is more cautious but may prevent possible optimizations */
2121 /* if the symbol is in far space then we should not */
2122 if (isOperandInFarSpace (IC_RIGHT (dic)))
2125 /* for + & - operations make sure that
2126 if it is on the stack it is the same
2127 as one of the three operands */
2128 if ((ic->op == '+' || ic->op == '-') &&
2129 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2132 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2133 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2134 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2138 /* now make sure that the right side of dic
2139 is not defined between ic & dic */
2142 iCode *sic = dic->next;
2144 for (; sic != ic; sic = sic->next)
2145 if (IC_RESULT (sic) &&
2146 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2153 /*-----------------------------------------------------------------*/
2154 /* reassignAliasedSym - used by packRegsForSupport to replace */
2155 /* redundant iTemp with equivalent symbol */
2156 /*-----------------------------------------------------------------*/
2158 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2161 unsigned oldSymKey, newSymKey;
2163 oldSymKey = op->key;
2164 newSymKey = IC_RIGHT(assignment)->key;
2166 /* only track live ranges of compiler-generated temporaries */
2167 if (!IS_ITEMP(IC_RIGHT(assignment)))
2170 /* update the live-value bitmaps */
2171 for (ic = assignment; ic != use; ic = ic->next) {
2172 bitVectUnSetBit (ic->rlive, oldSymKey);
2174 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2177 /* update the sym of the used operand */
2178 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2179 op->key = OP_SYMBOL(op)->key;
2180 OP_SYMBOL(op)->accuse = 0;
2182 /* update the sym's liverange */
2183 if ( OP_LIVETO(op) < ic->seq )
2184 setToRange(op, ic->seq, FALSE);
2186 /* remove the assignment iCode now that its result is unused */
2187 remiCodeFromeBBlock (ebp, assignment);
2188 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2189 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2193 /*-----------------------------------------------------------------*/
2194 /* packRegsForSupport :- reduce some registers for support calls */
2195 /*-----------------------------------------------------------------*/
2197 packRegsForSupport (iCode * ic, eBBlock * ebp)
2201 /* for the left & right operand :- look to see if the
2202 left was assigned a true symbol in far space in that
2203 case replace them */
2205 if (IS_ITEMP (IC_LEFT (ic)) &&
2206 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2208 dic = findAssignToSym (IC_LEFT (ic), ic);
2212 /* found it we need to remove it from the block */
2213 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2218 /* do the same for the right operand */
2219 if (IS_ITEMP (IC_RIGHT (ic)) &&
2220 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2222 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2226 /* if this is a subtraction & the result
2227 is a true symbol in far space then don't pack */
2228 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2230 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2231 if (IN_FARSPACE (SPEC_OCLS (etype)))
2234 /* found it we need to remove it from the
2236 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2245 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2248 /*-----------------------------------------------------------------*/
2249 /* packRegsForOneuse : - will reduce some registers for single Use */
2250 /*-----------------------------------------------------------------*/
2252 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2257 /* if returning a literal then do nothing */
2261 /* only upto 2 bytes since we cannot predict
2262 the usage of b, & acc */
2263 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2266 if (ic->op != RETURN &&
2268 !POINTER_SET (ic) &&
2272 if (ic->op == SEND && ic->argreg != 1) return NULL;
2274 /* this routine will mark the a symbol as used in one
2275 instruction use only && if the defintion is local
2276 (ie. within the basic block) && has only one definition &&
2277 that definiion is either a return value from a
2278 function or does not contain any variables in
2280 uses = bitVectCopy (OP_USES (op));
2281 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2282 if (!bitVectIsZero (uses)) /* has other uses */
2285 /* if it has only one defintion */
2286 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2287 return NULL; /* has more than one definition */
2289 /* get that definition */
2291 hTabItemWithKey (iCodehTab,
2292 bitVectFirstBit (OP_DEFS (op)))))
2295 /* if that only usage is a cast */
2296 if (dic->op == CAST) {
2297 /* to a bigger type */
2298 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2299 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2300 /* than we can not, since we cannot predict the usage of b & acc */
2305 /* found the definition now check if it is local */
2306 if (dic->seq < ebp->fSeq ||
2307 dic->seq > ebp->lSeq)
2308 return NULL; /* non-local */
2310 /* now check if it is the return from
2312 if (dic->op == CALL || dic->op == PCALL)
2314 if (ic->op != SEND && ic->op != RETURN &&
2315 !POINTER_SET(ic) && !POINTER_GET(ic))
2317 OP_SYMBOL (op)->ruonly = 1;
2324 /* otherwise check that the definition does
2325 not contain any symbols in far space */
2326 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2327 isOperandInFarSpace (IC_RIGHT (dic)) ||
2328 IS_OP_RUONLY (IC_LEFT (ic)) ||
2329 IS_OP_RUONLY (IC_RIGHT (ic)))
2334 /* if pointer set then make sure the pointer
2336 if (POINTER_SET (dic) &&
2337 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2340 if (POINTER_GET (dic) &&
2341 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2346 /* also make sure the intervenening instructions
2347 don't have any thing in far space */
2348 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2351 /* if there is an intervening function call then no */
2352 if (dic->op == CALL || dic->op == PCALL)
2354 /* if pointer set then make sure the pointer
2356 if (POINTER_SET (dic) &&
2357 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2360 if (POINTER_GET (dic) &&
2361 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2364 /* if address of & the result is remat the okay */
2365 if (dic->op == ADDRESS_OF &&
2366 OP_SYMBOL (IC_RESULT (dic))->remat)
2369 /* if operand has size of three or more & this
2370 operation is a '*','/' or '%' then 'b' may
2372 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2373 getSize (operandType (op)) >= 3)
2376 /* if left or right or result is in far space */
2377 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2378 isOperandInFarSpace (IC_RIGHT (dic)) ||
2379 isOperandInFarSpace (IC_RESULT (dic)) ||
2380 IS_OP_RUONLY (IC_LEFT (dic)) ||
2381 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2382 IS_OP_RUONLY (IC_RESULT (dic)))
2386 /* if left or right or result is on stack */
2387 if (isOperandOnStack(IC_LEFT(dic)) ||
2388 isOperandOnStack(IC_RIGHT(dic)) ||
2389 isOperandOnStack(IC_RESULT(dic))) {
2394 OP_SYMBOL (op)->ruonly = 1;
2398 /*-----------------------------------------------------------------*/
2399 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2400 /*-----------------------------------------------------------------*/
2402 isBitwiseOptimizable (iCode * ic)
2404 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2405 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2407 /* bitwise operations are considered optimizable
2408 under the following conditions (Jean-Louis VERN)
2420 if (IS_LITERAL(rtype) ||
2421 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2427 /*-----------------------------------------------------------------*/
2428 /* isCommutativeOp - tests whether this op cares what order its */
2429 /* operands are in */
2430 /*-----------------------------------------------------------------*/
2431 bool isCommutativeOp(unsigned int op)
2433 if (op == '+' || op == '*' || op == EQ_OP ||
2434 op == '^' || op == '|' || op == BITWISEAND)
2440 /*-----------------------------------------------------------------*/
2441 /* operandUsesAcc - determines whether the code generated for this */
2442 /* operand will have to use the accumulator */
2443 /*-----------------------------------------------------------------*/
2444 bool operandUsesAcc(operand *op)
2450 symbol *sym = OP_SYMBOL(op);
2454 return TRUE; /* duh! */
2456 if (IN_STACK(sym->etype) || sym->onStack ||
2457 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2458 return TRUE; /* acc is used to calc stack offset */
2463 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2465 return FALSE; /* more checks? */
2469 symspace = SPEC_OCLS(sym->etype);
2471 if (sym->iaccess && symspace->paged)
2472 return TRUE; /* must fetch paged indirect sym via accumulator */
2474 if (IN_BITSPACE(symspace))
2475 return TRUE; /* fetching bit vars uses the accumulator */
2477 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2478 return TRUE; /* fetched via accumulator and dptr */
2484 /*-----------------------------------------------------------------*/
2485 /* packRegsForAccUse - pack registers for acc use */
2486 /*-----------------------------------------------------------------*/
2488 packRegsForAccUse (iCode * ic)
2492 /* if this is an aggregate, e.g. a one byte char array */
2493 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2497 /* if we are calling a reentrant function that has stack parameters */
2498 if (ic->op == CALL &&
2499 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2500 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2503 if (ic->op == PCALL &&
2504 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2505 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2508 /* if + or - then it has to be one byte result */
2509 if ((ic->op == '+' || ic->op == '-')
2510 && getSize (operandType (IC_RESULT (ic))) > 1)
2513 /* if shift operation make sure right side is not a literal */
2514 if (ic->op == RIGHT_OP &&
2515 (isOperandLiteral (IC_RIGHT (ic)) ||
2516 getSize (operandType (IC_RESULT (ic))) > 1))
2519 if (ic->op == LEFT_OP &&
2520 (isOperandLiteral (IC_RIGHT (ic)) ||
2521 getSize (operandType (IC_RESULT (ic))) > 1))
2524 if (IS_BITWISE_OP (ic) &&
2525 getSize (operandType (IC_RESULT (ic))) > 1)
2529 /* has only one definition */
2530 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2533 /* has only one use */
2534 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2537 /* and the usage immediately follows this iCode */
2538 if (!(uic = hTabItemWithKey (iCodehTab,
2539 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2542 if (ic->next != uic)
2545 /* if it is a conditional branch then we definitely can */
2549 if (uic->op == JUMPTABLE)
2552 if (POINTER_SET (uic) &&
2553 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2556 /* if the usage is not is an assignment
2557 or an arithmetic / bitwise / shift operation then not */
2558 if (uic->op != '=' &&
2559 !IS_ARITHMETIC_OP (uic) &&
2560 !IS_BITWISE_OP (uic) &&
2561 uic->op != LEFT_OP &&
2562 uic->op != RIGHT_OP)
2565 /* if used in ^ operation then make sure right is not a
2566 literal (WIML: Why is this?) */
2567 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2570 /* if shift operation make sure right side is not a literal */
2571 /* WIML: Why is this? */
2572 if (uic->op == RIGHT_OP &&
2573 (isOperandLiteral (IC_RIGHT (uic)) ||
2574 getSize (operandType (IC_RESULT (uic))) > 1))
2576 if (uic->op == LEFT_OP &&
2577 (isOperandLiteral (IC_RIGHT (uic)) ||
2578 getSize (operandType (IC_RESULT (uic))) > 1))
2581 /* make sure that the result of this icode is not on the
2582 stack, since acc is used to compute stack offset */
2584 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2585 OP_SYMBOL (IC_RESULT (uic))->onStack)
2588 if (isOperandOnStack(IC_RESULT(uic)))
2592 /* if the usage has only one operand then we can */
2593 if (IC_LEFT (uic) == NULL ||
2594 IC_RIGHT (uic) == NULL)
2597 /* if the other operand uses the accumulator then we cannot */
2598 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2599 operandUsesAcc(IC_RIGHT(uic))) ||
2600 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2601 operandUsesAcc(IC_LEFT(uic))) )
2604 /* make sure this is on the left side if not commutative */
2605 /* except for '-', which has been written to be able to
2606 handle reversed operands */
2607 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2608 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2612 // this is too dangerous and need further restrictions
2615 /* if one of them is a literal then we can */
2616 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2617 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2619 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2625 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2629 /*-----------------------------------------------------------------*/
2630 /* packForPush - hueristics to reduce iCode for pushing */
2631 /*-----------------------------------------------------------------*/
2633 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2637 struct eBBlock * ebp=ebpp[blockno];
2639 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2642 /* must have only definition & one usage */
2643 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2644 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2647 /* find the definition */
2648 if (!(dic = hTabItemWithKey (iCodehTab,
2649 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2652 if (dic->op != '=' || POINTER_SET (dic))
2655 if (dic->seq < ebp->fSeq) { // Evelyn did this
2657 for (i=0; i<blockno; i++) {
2658 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2663 wassert (i!=blockno); // no way to recover from here
2666 if (IS_SYMOP(IC_RIGHT(dic))) {
2667 /* make sure the right side does not have any definitions
2669 dbv = OP_DEFS(IC_RIGHT(dic));
2670 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2671 if (bitVectBitValue(dbv,lic->key))
2674 /* make sure they have the same type */
2675 if (IS_SPEC(operandType(IC_LEFT(ic))))
2677 sym_link *itype=operandType(IC_LEFT(ic));
2678 sym_link *ditype=operandType(IC_RIGHT(dic));
2680 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2681 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2684 /* extend the live range of replaced operand if needed */
2685 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2686 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2688 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2691 /* we now we know that it has one & only one def & use
2692 and the that the definition is an assignment */
2693 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2694 remiCodeFromeBBlock (ebp, dic);
2695 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2698 /*-----------------------------------------------------------------*/
2699 /* packRegisters - does some transformations to reduce register */
2701 /*-----------------------------------------------------------------*/
2703 packRegisters (eBBlock ** ebpp, int blockno)
2707 eBBlock *ebp=ebpp[blockno];
2714 /* look for assignments of the form */
2715 /* iTempNN = TRueSym (someoperation) SomeOperand */
2717 /* TrueSym := iTempNN:1 */
2718 for (ic = ebp->sch; ic; ic = ic->next)
2720 /* find assignment of the form TrueSym := iTempNN:1 */
2721 if (ic->op == '=' && !POINTER_SET (ic))
2722 change += packRegsForAssign (ic, ebp);
2729 for (ic = ebp->sch; ic; ic = ic->next)
2731 /* if this is an itemp & result of an address of a true sym
2732 then mark this as rematerialisable */
2733 if (ic->op == ADDRESS_OF &&
2734 IS_ITEMP (IC_RESULT (ic)) &&
2735 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2736 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2737 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2740 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2741 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2742 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2746 /* if straight assignment then carry remat flag if
2747 this is the only definition */
2748 if (ic->op == '=' &&
2749 !POINTER_SET (ic) &&
2750 IS_SYMOP (IC_RIGHT (ic)) &&
2751 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2752 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2753 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2756 OP_SYMBOL (IC_RESULT (ic))->remat =
2757 OP_SYMBOL (IC_RIGHT (ic))->remat;
2758 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2759 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2762 /* if cast to a generic pointer & the pointer being
2763 cast is remat, then we can remat this cast as well */
2764 if (ic->op == CAST &&
2765 IS_SYMOP(IC_RIGHT(ic)) &&
2766 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2767 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1) {
2768 sym_link *to_type = operandType(IC_LEFT(ic));
2769 sym_link *from_type = operandType(IC_RIGHT(ic));
2770 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2771 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2772 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2773 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2777 /* if this is a +/- operation with a rematerizable
2778 then mark this as rematerializable as well */
2779 if ((ic->op == '+' || ic->op == '-') &&
2780 (IS_SYMOP (IC_LEFT (ic)) &&
2781 IS_ITEMP (IC_RESULT (ic)) &&
2782 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2783 OP_SYMBOL (IC_LEFT (ic))->remat &&
2784 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2785 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2787 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2788 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2789 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2792 /* mark the pointer usages */
2793 if (POINTER_SET (ic))
2794 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2796 if (POINTER_GET (ic) &&
2797 IS_SYMOP(IC_LEFT (ic)))
2798 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2802 /* if we are using a symbol on the stack
2803 then we should say mcs51_ptrRegReq */
2804 if (options.useXstack && ic->parmPush
2805 && (ic->op == IPUSH || ic->op == IPOP))
2807 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2808 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2809 OP_SYMBOL (IC_COND (ic))->iaccess ||
2810 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
2811 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2812 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2813 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
2814 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
2817 if (IS_SYMOP (IC_LEFT (ic)))
2818 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2819 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
2820 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
2821 if (IS_SYMOP (IC_RIGHT (ic)))
2822 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2823 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
2824 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
2825 if (IS_SYMOP (IC_RESULT (ic)))
2826 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2827 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
2828 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
2829 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
2830 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
2832 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
2833 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
2838 /* if the condition of an if instruction
2839 is defined in the previous instruction and
2840 this is the only usage then
2841 mark the itemp as a conditional */
2842 if ((IS_CONDITIONAL (ic) ||
2843 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2844 ic->next && ic->next->op == IFX &&
2845 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2846 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2847 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2849 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2853 /* reduce for support function calls */
2854 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2855 packRegsForSupport (ic, ebp);
2857 /* some cases the redundant moves can
2858 can be eliminated for return statements */
2859 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2860 !isOperandInFarSpace (IC_LEFT (ic)) &&
2861 options.model == MODEL_SMALL) {
2862 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2865 /* if pointer set & left has a size more than
2866 one and right is not in far space */
2867 if (POINTER_SET (ic) &&
2868 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2869 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2870 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2871 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2872 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2874 /* if pointer get */
2875 if (POINTER_GET (ic) &&
2876 IS_SYMOP (IC_LEFT (ic)) &&
2877 !isOperandInFarSpace (IC_RESULT (ic)) &&
2878 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2879 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2880 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2881 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2884 /* if this is cast for intergral promotion then
2885 check if only use of the definition of the
2886 operand being casted/ if yes then replace
2887 the result of that arithmetic operation with
2888 this result and get rid of the cast */
2891 sym_link *fromType = operandType (IC_RIGHT (ic));
2892 sym_link *toType = operandType (IC_LEFT (ic));
2894 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2895 getSize (fromType) != getSize (toType) &&
2896 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2899 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2902 if (IS_ARITHMETIC_OP (dic))
2904 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2905 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2906 remiCodeFromeBBlock (ebp, ic);
2907 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2908 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2909 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2913 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2919 /* if the type from and type to are the same
2920 then if this is the only use then packit */
2921 if (compareType (operandType (IC_RIGHT (ic)),
2922 operandType (IC_LEFT (ic))) == 1)
2924 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2927 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2928 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2929 remiCodeFromeBBlock (ebp, ic);
2930 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2931 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2932 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2940 iTempNN := (some variable in farspace) V1
2945 if (ic->op == IPUSH)
2947 packForPush (ic, ebpp, blockno);
2951 /* pack registers for accumulator use, when the
2952 result of an arithmetic or bit wise operation
2953 has only one use, that use is immediately following
2954 the defintion and the using iCode has only one
2955 operand or has two operands but one is literal &
2956 the result of that operation is not on stack then
2957 we can leave the result of this operation in acc:b
2959 if ((IS_ARITHMETIC_OP (ic)
2960 || IS_CONDITIONAL(ic)
2961 || IS_BITWISE_OP (ic)
2962 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2963 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2965 IS_ITEMP (IC_RESULT (ic)) &&
2966 getSize (operandType (IC_RESULT (ic))) <= 2)
2968 packRegsForAccUse (ic);
2972 /*-----------------------------------------------------------------*/
2973 /* assignRegisters - assigns registers to each live range as need */
2974 /*-----------------------------------------------------------------*/
2976 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2981 setToNull ((void *) &_G.funcrUsed);
2982 setToNull ((void *) &_G.regAssigned);
2983 setToNull ((void *) &_G.totRegAssigned);
2984 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2987 /* change assignments this will remove some
2988 live ranges reducing some register pressure */
2990 for (i = 0; i < count; i++)
2991 packRegisters (ebbs, i);
2993 /* liveranges probably changed by register packing
2994 so we compute them again */
2995 recomputeLiveRanges (ebbs, count);
2997 if (options.dump_pack)
2998 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3000 /* first determine for each live range the number of
3001 registers & the type of registers required for each */
3004 /* and serially allocate registers */
3005 serialRegAssign (ebbs, count);
3008 //setToNull ((void *) &_G.regAssigned);
3009 //setToNull ((void *) &_G.totRegAssigned);
3012 /* if stack was extended then tell the user */
3015 /* werror(W_TOOMANY_SPILS,"stack", */
3016 /* _G.stackExtend,currFunc->name,""); */
3022 /* werror(W_TOOMANY_SPILS,"data space", */
3023 /* _G.dataExtend,currFunc->name,""); */
3027 /* after that create the register mask
3028 for each of the instruction */
3029 createRegMask (ebbs, count);
3031 /* redo that offsets for stacked automatic variables */
3033 redoStackOffsets ();
3036 /* make sure r0 & r1 are flagged as used if they might be used */
3038 if (currFunc && mcs51_ptrRegReq)
3040 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3041 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3044 if (options.dump_rassgn)
3046 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3047 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3050 /* do the overlaysegment stuff SDCCmem.c */
3051 doOverlays (ebbs, count);
3053 /* now get back the chain */
3054 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3058 /* free up any _G.stackSpil locations allocated */
3059 applyToSet (_G.stackSpil, deallocStackSpil);
3061 setToNull ((void *) &_G.stackSpil);
3062 setToNull ((void *) &_G.spiltSet);
3063 /* mark all registers as free */