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", "C", "xreg", 0, 1},
80 static void spillThis (symbol *);
81 static void freeAllRegs ();
83 /*-----------------------------------------------------------------*/
84 /* allocReg - allocates register of given type */
85 /*-----------------------------------------------------------------*/
91 for (i = 0; i < mcs51_nRegs; i++)
94 /* if type is given as 0 then any
95 free register will do */
99 regs8051[i].isFree = 0;
102 bitVectSetBit (currFunc->regsUsed, i);
105 /* other wise look for specific type
107 if (regs8051[i].isFree &&
108 regs8051[i].type == type)
110 regs8051[i].isFree = 0;
113 bitVectSetBit (currFunc->regsUsed, i);
120 /*-----------------------------------------------------------------*/
121 /* mcs51_regWithIdx - returns pointer to register wit index number */
122 /*-----------------------------------------------------------------*/
124 mcs51_regWithIdx (int idx)
128 for (i = 0; i < mcs51_nRegs; i++)
129 if (regs8051[i].rIdx == idx)
132 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
133 "regWithIdx not found");
137 /*-----------------------------------------------------------------*/
138 /* freeReg - frees a register */
139 /*-----------------------------------------------------------------*/
145 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
146 "freeReg - Freeing NULL register");
154 /*-----------------------------------------------------------------*/
155 /* nFreeRegs - returns number of free registers */
156 /*-----------------------------------------------------------------*/
163 for (i = 0; i < mcs51_nRegs; i++)
164 if (regs8051[i].isFree && regs8051[i].type == type)
169 /*-----------------------------------------------------------------*/
170 /* nfreeRegsType - free registers with type */
171 /*-----------------------------------------------------------------*/
173 nfreeRegsType (int type)
178 if ((nfr = nFreeRegs (type)) == 0)
179 return nFreeRegs (REG_GPR);
182 return nFreeRegs (type);
185 /*-----------------------------------------------------------------*/
186 /* useReg - marks a register as used */
187 /*-----------------------------------------------------------------*/
194 /*-----------------------------------------------------------------*/
195 /* allDefsOutOfRange - all definitions are out of a range */
196 /*-----------------------------------------------------------------*/
198 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
205 for (i = 0; i < defs->size; i++)
209 if (bitVectBitValue (defs, i) &&
210 (ic = hTabItemWithKey (iCodehTab, i)) &&
211 (ic->seq >= fseq && ic->seq <= toseq))
220 /*-----------------------------------------------------------------*/
221 /* computeSpillable - given a point find the spillable live ranges */
222 /*-----------------------------------------------------------------*/
224 computeSpillable (iCode * ic)
228 /* spillable live ranges are those that are live at this
229 point . the following categories need to be subtracted
231 a) - those that are already spilt
232 b) - if being used by this one
233 c) - defined by this one */
235 spillable = bitVectCopy (ic->rlive);
237 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
239 bitVectCplAnd (spillable, ic->uses); /* used in this one */
240 bitVectUnSetBit (spillable, ic->defKey);
241 spillable = bitVectIntersect (spillable, _G.regAssigned);
246 /*-----------------------------------------------------------------*/
247 /* noSpilLoc - return true if a variable has no spil location */
248 /*-----------------------------------------------------------------*/
250 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
252 return (sym->usl.spillLoc ? 0 : 1);
255 /*-----------------------------------------------------------------*/
256 /* hasSpilLoc - will return 1 if the symbol has spil location */
257 /*-----------------------------------------------------------------*/
259 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
261 return (sym->usl.spillLoc ? 1 : 0);
264 /*-----------------------------------------------------------------*/
265 /* directSpilLoc - will return 1 if the splilocation is in direct */
266 /*-----------------------------------------------------------------*/
268 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
270 if (sym->usl.spillLoc &&
271 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
277 /*-----------------------------------------------------------------*/
278 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
279 /* but is not used as a pointer */
280 /*-----------------------------------------------------------------*/
282 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
284 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
287 /*-----------------------------------------------------------------*/
288 /* rematable - will return 1 if the remat flag is set */
289 /*-----------------------------------------------------------------*/
291 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
296 /*-----------------------------------------------------------------*/
297 /* notUsedInBlock - not used in this block */
298 /*-----------------------------------------------------------------*/
300 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
302 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
303 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
304 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
307 /*-----------------------------------------------------------------*/
308 /* notUsedInRemaining - not used or defined in remain of the block */
309 /*-----------------------------------------------------------------*/
311 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
313 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
314 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
317 /*-----------------------------------------------------------------*/
318 /* allLRs - return true for all */
319 /*-----------------------------------------------------------------*/
321 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
326 /*-----------------------------------------------------------------*/
327 /* liveRangesWith - applies function to a given set of live range */
328 /*-----------------------------------------------------------------*/
330 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
331 eBBlock * ebp, iCode * ic)
336 if (!lrs || !lrs->size)
339 for (i = 1; i < lrs->size; i++)
342 if (!bitVectBitValue (lrs, i))
345 /* if we don't find it in the live range
346 hash table we are in serious trouble */
347 if (!(sym = hTabItemWithKey (liveRanges, i)))
349 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
350 "liveRangesWith could not find liveRange");
354 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
355 addSetHead (&rset, sym);
362 /*-----------------------------------------------------------------*/
363 /* leastUsedLR - given a set determines which is the least used */
364 /*-----------------------------------------------------------------*/
366 leastUsedLR (set * sset)
368 symbol *sym = NULL, *lsym = NULL;
370 sym = lsym = setFirstItem (sset);
375 for (; lsym; lsym = setNextItem (sset))
378 /* if usage is the same then prefer
379 the spill the smaller of the two */
380 if (lsym->used == sym->used)
381 if (getSize (lsym->type) < getSize (sym->type))
385 if (lsym->used < sym->used)
390 setToNull ((void **) &sset);
395 /*-----------------------------------------------------------------*/
396 /* noOverLap - will iterate through the list looking for over lap */
397 /*-----------------------------------------------------------------*/
399 noOverLap (set * itmpStack, symbol * fsym)
404 for (sym = setFirstItem (itmpStack); sym;
405 sym = setNextItem (itmpStack))
407 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
413 /*-----------------------------------------------------------------*/
414 /* isFree - will return 1 if the a free spil location is found */
415 /*-----------------------------------------------------------------*/
420 V_ARG (symbol **, sloc);
421 V_ARG (symbol *, fsym);
423 /* if already found */
427 /* if it is free && and the itmp assigned to
428 this does not have any overlapping live ranges
429 with the one currently being assigned and
430 the size can be accomodated */
432 noOverLap (sym->usl.itmpStack, fsym) &&
433 getSize (sym->type) >= getSize (fsym->type))
442 /*-----------------------------------------------------------------*/
443 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
444 /*-----------------------------------------------------------------*/
446 spillLRWithPtrReg (symbol * forSym)
452 if (!_G.regAssigned ||
453 bitVectIsZero (_G.regAssigned))
456 r0 = mcs51_regWithIdx (R0_IDX);
457 r1 = mcs51_regWithIdx (R1_IDX);
459 /* for all live ranges */
460 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
461 lrsym = hTabNextItem (liveRanges, &k))
465 /* if no registers assigned to it or spilt */
466 /* if it does not overlap with this then
467 not need to spill it */
469 if (lrsym->isspilt || !lrsym->nRegs ||
470 (lrsym->liveTo < forSym->liveFrom))
473 /* go thru the registers : if it is either
474 r0 or r1 then spil it */
475 for (j = 0; j < lrsym->nRegs; j++)
476 if (lrsym->regs[j] == r0 ||
477 lrsym->regs[j] == r1)
486 /*-----------------------------------------------------------------*/
487 /* createStackSpil - create a location on the stack to spil */
488 /*-----------------------------------------------------------------*/
490 createStackSpil (symbol * sym)
493 int useXstack, model;
497 /* first go try and find a free one that is already
498 existing on the stack */
499 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
501 /* found a free one : just update & return */
502 sym->usl.spillLoc = sloc;
505 addSetHead (&sloc->usl.itmpStack, sym);
509 /* could not then have to create one , this is the hard part
510 we need to allocate this on the stack : this is really a
511 hack!! but cannot think of anything better at this time */
513 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
515 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
520 sloc = newiTemp (slocBuffer);
522 /* set the type to the spilling symbol */
523 sloc->type = copyLinkChain (sym->type);
524 sloc->etype = getSpec (sloc->type);
525 SPEC_SCLS (sloc->etype) = S_DATA;
526 SPEC_EXTR (sloc->etype) = 0;
527 SPEC_STAT (sloc->etype) = 0;
528 SPEC_VOLATILE(sloc->etype) = 0;
530 /* we don't allow it to be allocated`
531 onto the external stack since : so we
532 temporarily turn it off ; we also
533 turn off memory model to prevent
534 the spil from going to the external storage
537 useXstack = options.useXstack;
538 model = options.model;
539 /* noOverlay = options.noOverlay; */
540 /* options.noOverlay = 1; */
541 options.model = options.useXstack = 0;
545 options.useXstack = useXstack;
546 options.model = model;
547 /* options.noOverlay = noOverlay; */
548 sloc->isref = 1; /* to prevent compiler warning */
550 /* if it is on the stack then update the stack */
551 if (IN_STACK (sloc->etype))
553 currFunc->stack += getSize (sloc->type);
554 _G.stackExtend += getSize (sloc->type);
557 _G.dataExtend += getSize (sloc->type);
559 /* add it to the _G.stackSpil set */
560 addSetHead (&_G.stackSpil, sloc);
561 sym->usl.spillLoc = sloc;
564 /* add it to the set of itempStack set
565 of the spill location */
566 addSetHead (&sloc->usl.itmpStack, sym);
570 /*-----------------------------------------------------------------*/
571 /* isSpiltOnStack - returns true if the spil location is on stack */
572 /*-----------------------------------------------------------------*/
574 isSpiltOnStack (symbol * sym)
584 /* if (sym->_G.stackSpil) */
587 if (!sym->usl.spillLoc)
590 etype = getSpec (sym->usl.spillLoc->type);
591 if (IN_STACK (etype))
597 /*-----------------------------------------------------------------*/
598 /* spillThis - spils a specific operand */
599 /*-----------------------------------------------------------------*/
601 spillThis (symbol * sym)
604 /* if this is rematerializable or has a spillLocation
605 we are okay, else we need to create a spillLocation
607 if (!(sym->remat || sym->usl.spillLoc))
608 createStackSpil (sym);
611 /* mark it has spilt & put it in the spilt set */
612 sym->isspilt = sym->spillA = 1;
613 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
615 bitVectUnSetBit (_G.regAssigned, sym->key);
616 bitVectUnSetBit (_G.totRegAssigned, sym->key);
618 for (i = 0; i < sym->nRegs; i++)
622 freeReg (sym->regs[i]);
626 /* if spilt on stack then free up r0 & r1
627 if they could have been assigned to some
629 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
632 spillLRWithPtrReg (sym);
635 if (sym->usl.spillLoc && !sym->remat)
636 sym->usl.spillLoc->allocreq++;
640 /*-----------------------------------------------------------------*/
641 /* selectSpil - select a iTemp to spil : rather a simple procedure */
642 /*-----------------------------------------------------------------*/
644 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
646 bitVect *lrcs = NULL;
650 /* get the spillable live ranges */
651 lrcs = computeSpillable (ic);
653 /* get all live ranges that are rematerizable */
654 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
657 /* return the least used of these */
658 return leastUsedLR (selectS);
661 /* get live ranges with spillLocations in direct space */
662 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
664 sym = leastUsedLR (selectS);
665 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
666 sym->usl.spillLoc->rname :
667 sym->usl.spillLoc->name));
669 /* mark it as allocation required */
670 sym->usl.spillLoc->allocreq++;
674 /* if the symbol is local to the block then */
675 if (forSym->liveTo < ebp->lSeq)
678 /* check if there are any live ranges allocated
679 to registers that are not used in this block */
680 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
682 sym = leastUsedLR (selectS);
683 /* if this is not rematerializable */
692 /* check if there are any live ranges that not
693 used in the remainder of the block */
694 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
696 sym = leastUsedLR (selectS);
709 /* find live ranges with spillocation && not used as pointers */
710 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
713 sym = leastUsedLR (selectS);
714 /* mark this as allocation required */
715 sym->usl.spillLoc->allocreq++;
719 /* find live ranges with spillocation */
720 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
723 sym = leastUsedLR (selectS);
724 sym->usl.spillLoc->allocreq++;
728 /* couldn't find then we need to create a spil
729 location on the stack , for which one? the least
731 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
734 /* return a created spil location */
735 sym = createStackSpil (leastUsedLR (selectS));
736 sym->usl.spillLoc->allocreq++;
740 /* this is an extreme situation we will spill
741 this one : happens very rarely but it does happen */
747 /*-----------------------------------------------------------------*/
748 /* spilSomething - spil some variable & mark registers as free */
749 /*-----------------------------------------------------------------*/
751 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
756 /* get something we can spil */
757 ssym = selectSpil (ic, ebp, forSym);
759 /* mark it as spilt */
760 ssym->isspilt = ssym->spillA = 1;
761 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
763 /* mark it as not register assigned &
764 take it away from the set */
765 bitVectUnSetBit (_G.regAssigned, ssym->key);
766 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
768 /* mark the registers as free */
769 for (i = 0; i < ssym->nRegs; i++)
771 freeReg (ssym->regs[i]);
773 /* if spilt on stack then free up r0 & r1
774 if they could have been assigned to as gprs */
775 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
778 spillLRWithPtrReg (ssym);
781 /* if this was a block level spil then insert push & pop
782 at the start & end of block respectively */
785 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
786 /* add push to the start of the block */
787 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
788 ebp->sch->next : ebp->sch));
789 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
790 /* add pop to the end of the block */
791 addiCodeToeBBlock (ebp, nic, NULL);
794 /* if spilt because not used in the remainder of the
795 block then add a push before this instruction and
796 a pop at the end of the block */
797 if (ssym->remainSpil)
800 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
801 /* add push just before this instruction */
802 addiCodeToeBBlock (ebp, nic, ic);
804 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
805 /* add pop to the end of the block */
806 addiCodeToeBBlock (ebp, nic, NULL);
815 /*-----------------------------------------------------------------*/
816 /* getRegPtr - will try for PTR if not a GPR type if not spil */
817 /*-----------------------------------------------------------------*/
819 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
824 /* try for a ptr type */
825 if ((reg = allocReg (REG_PTR)))
828 /* try for gpr type */
829 if ((reg = allocReg (REG_GPR)))
832 /* we have to spil */
833 if (!spilSomething (ic, ebp, sym))
836 /* this looks like an infinite loop but
837 in really selectSpil will abort */
841 /*-----------------------------------------------------------------*/
842 /* getRegGpr - will try for GPR if not spil */
843 /*-----------------------------------------------------------------*/
845 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
850 /* try for gpr type */
851 if ((reg = allocReg (REG_GPR)))
854 if (!mcs51_ptrRegReq)
855 if ((reg = allocReg (REG_PTR)))
858 /* we have to spil */
859 if (!spilSomething (ic, ebp, sym))
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)))
885 /*-----------------------------------------------------------------*/
886 /* getRegGprNoSpil - get it cannot split */
887 /*-----------------------------------------------------------------*/
888 static regs *getRegGprNoSpil()
892 if ((reg = allocReg (REG_GPR)))
895 if (!mcs51_ptrRegReq)
896 if ((reg = allocReg (REG_PTR)))
902 /*-----------------------------------------------------------------*/
903 /* symHasReg - symbol has a given register */
904 /*-----------------------------------------------------------------*/
906 symHasReg (symbol * sym, regs * reg)
910 for (i = 0; i < sym->nRegs; i++)
911 if (sym->regs[i] == reg)
917 /*-----------------------------------------------------------------*/
918 /* deassignLRs - check the live to and if they have registers & are */
919 /* not spilt then free up the registers */
920 /*-----------------------------------------------------------------*/
922 deassignLRs (iCode * ic, eBBlock * ebp)
928 for (sym = hTabFirstItem (liveRanges, &k); sym;
929 sym = hTabNextItem (liveRanges, &k))
933 /* if it does not end here */
934 if (sym->liveTo > ic->seq)
937 /* if it was spilt on stack then we can
938 mark the stack spil location as free */
943 sym->usl.spillLoc->isFree = 1;
949 if (!bitVectBitValue (_G.regAssigned, sym->key))
952 /* special case check if this is an IFX &
953 the privious one was a pop and the
954 previous one was not spilt then keep track
956 if (ic->op == IFX && ic->prev &&
957 ic->prev->op == IPOP &&
958 !ic->prev->parmPush &&
959 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
960 psym = OP_SYMBOL (IC_LEFT (ic->prev));
966 bitVectUnSetBit (_G.regAssigned, sym->key);
968 /* if the result of this one needs registers
969 and does not have it then assign it right
971 if (IC_RESULT (ic) &&
972 !(SKIP_IC2 (ic) || /* not a special icode */
973 ic->op == JUMPTABLE ||
979 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
980 result->liveTo > ic->seq && /* and will live beyond this */
981 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
982 result->regType == sym->regType && /* same register types */
983 result->nRegs && /* which needs registers */
984 !result->isspilt && /* and does not already have them */
986 !bitVectBitValue (_G.regAssigned, result->key) &&
987 /* the number of free regs + number of regs in this LR
988 can accomodate the what result Needs */
989 ((nfreeRegsType (result->regType) +
990 sym->nRegs) >= result->nRegs)
994 for (i = 0; i < result->nRegs; i++)
996 result->regs[i] = sym->regs[i];
998 result->regs[i] = getRegGpr (ic, ebp, result);
1000 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1001 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1005 /* free the remaining */
1006 for (; i < sym->nRegs; i++)
1010 if (!symHasReg (psym, sym->regs[i]))
1011 freeReg (sym->regs[i]);
1014 freeReg (sym->regs[i]);
1021 /*-----------------------------------------------------------------*/
1022 /* reassignLR - reassign this to registers */
1023 /*-----------------------------------------------------------------*/
1025 reassignLR (operand * op)
1027 symbol *sym = OP_SYMBOL (op);
1030 /* not spilt any more */
1031 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1032 bitVectUnSetBit (_G.spiltSet, sym->key);
1034 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1035 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1039 for (i = 0; i < sym->nRegs; i++)
1040 sym->regs[i]->isFree = 0;
1043 /*-----------------------------------------------------------------*/
1044 /* willCauseSpill - determines if allocating will cause a spill */
1045 /*-----------------------------------------------------------------*/
1047 willCauseSpill (int nr, int rt)
1049 /* first check if there are any avlb registers
1050 of te type required */
1053 /* special case for pointer type
1054 if pointer type not avlb then
1055 check for type gpr */
1056 if (nFreeRegs (rt) >= nr)
1058 if (nFreeRegs (REG_GPR) >= nr)
1063 if (mcs51_ptrRegReq)
1065 if (nFreeRegs (rt) >= nr)
1070 if (nFreeRegs (REG_PTR) +
1071 nFreeRegs (REG_GPR) >= nr)
1076 /* it will cause a spil */
1080 /*-----------------------------------------------------------------*/
1081 /* positionRegs - the allocator can allocate same registers to res- */
1082 /* ult and operand, if this happens make sure they are in the same */
1083 /* position as the operand otherwise chaos results */
1084 /*-----------------------------------------------------------------*/
1086 positionRegs (symbol * result, symbol * opsym)
1088 int count = min (result->nRegs, opsym->nRegs);
1089 int i, j = 0, shared = 0;
1092 /* if the result has been spilt then cannot share */
1097 /* first make sure that they actually share */
1098 for (i = 0; i < count; i++)
1100 for (j = 0; j < count; j++)
1102 if (result->regs[i] == opsym->regs[j] && i != j)
1112 regs *tmp = result->regs[i];
1113 result->regs[i] = result->regs[j];
1114 result->regs[j] = tmp;
1121 /*-----------------------------------------------------------------*/
1122 /* serialRegAssign - serially allocate registers to the variables */
1123 /*-----------------------------------------------------------------*/
1125 serialRegAssign (eBBlock ** ebbs, int count)
1129 /* for all blocks */
1130 for (i = 0; i < count; i++) {
1134 if (ebbs[i]->noPath &&
1135 (ebbs[i]->entryLabel != entryLabel &&
1136 ebbs[i]->entryLabel != returnLabel))
1139 /* of all instructions do */
1140 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1142 /* if this is an ipop that means some live
1143 range will have to be assigned again */
1145 reassignLR (IC_LEFT (ic));
1147 /* if result is present && is a true symbol */
1148 if (IC_RESULT (ic) && ic->op != IFX &&
1149 IS_TRUE_SYMOP (IC_RESULT (ic)))
1150 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1152 /* take away registers from live
1153 ranges that end at this instruction */
1154 deassignLRs (ic, ebbs[i]);
1156 /* some don't need registers */
1157 if (SKIP_IC2 (ic) ||
1158 ic->op == JUMPTABLE ||
1162 (IC_RESULT (ic) && POINTER_SET (ic)))
1165 /* now we need to allocate registers
1166 only for the result */
1167 if (IC_RESULT (ic)) {
1168 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1174 /* if it does not need or is spilt
1175 or is already assigned to registers
1176 or will not live beyond this instructions */
1179 bitVectBitValue (_G.regAssigned, sym->key) ||
1180 sym->liveTo <= ic->seq)
1183 /* if some liverange has been spilt at the block level
1184 and this one live beyond this block then spil this
1186 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1190 /* if trying to allocate this will cause
1191 a spill and there is nothing to spill
1192 or this one is rematerializable then
1194 willCS = willCauseSpill (sym->nRegs, sym->regType);
1195 spillable = computeSpillable (ic);
1196 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1201 /* if it has a spillocation & is used less than
1202 all other live ranges then spill this */
1204 if (sym->usl.spillLoc) {
1205 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1206 allLRs, ebbs[i], ic));
1207 if (leastUsed && leastUsed->used > sym->used) {
1212 /* if none of the liveRanges have a spillLocation then better
1213 to spill this one than anything else already assigned to registers */
1214 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1215 /* if this is local to this block then we might find a block spil */
1216 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1223 /* if we need ptr regs for the right side
1225 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1226 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1230 /* else we assign registers to it */
1231 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1232 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1234 for (j = 0; j < sym->nRegs; j++) {
1235 if (sym->regType == REG_PTR)
1236 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1238 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1240 /* if the allocation failed which means
1241 this was spilt then break */
1242 if (!sym->regs[j]) {
1244 fprintf (stderr, "%d reg(s) lost in %s:%d\n",
1245 j, __FILE__,__LINE__);
1251 /* if it shares registers with operands make sure
1252 that they are in the same position */
1253 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1254 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1255 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1256 OP_SYMBOL (IC_LEFT (ic)));
1258 /* do the same for the right operand */
1259 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1260 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1261 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1262 OP_SYMBOL (IC_RIGHT (ic)));
1275 /*-----------------------------------------------------------------*/
1276 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1277 /*-----------------------------------------------------------------*/
1278 static void fillGaps()
1283 if (getenv("DISABLE_FILL_GAPS")) return;
1285 /* look for livernages that was spilt by the allocator */
1286 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1287 sym = hTabNextItem(liveRanges,&key)) {
1292 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1294 /* find the liveRanges this one clashes with, that are
1295 still assigned to registers & mark the registers as used*/
1296 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1300 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1301 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1304 assert (clr = hTabItemWithKey(liveRanges,i));
1306 /* mark these registers as used */
1307 for (k = 0 ; k < clr->nRegs ; k++ )
1308 useReg(clr->regs[k]);
1311 if (willCauseSpill(sym->nRegs,sym->regType)) {
1312 /* NOPE :( clear all registers & and continue */
1317 /* THERE IS HOPE !!!! */
1318 for (i=0; i < sym->nRegs ; i++ ) {
1319 sym->regs[i] = getRegGprNoSpil ();
1322 /* for all its definitions check if the registers
1323 allocated needs positioning NOTE: we can position
1324 only ONCE if more than One positioning required
1327 for (i = 0 ; i < sym->defs->size ; i++ ) {
1328 if (bitVectBitValue(sym->defs,i)) {
1330 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1331 if (SKIP_IC(ic)) continue;
1332 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1333 /* if left is assigned to registers */
1334 if (IS_SYMOP(IC_LEFT(ic)) &&
1335 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1336 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1338 if (IS_SYMOP(IC_RIGHT(ic)) &&
1339 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1340 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1342 if (pdone > 1) break;
1345 /* had to position more than once GIVE UP */
1347 /* UNDO all the changes we made to try this */
1349 for (i=0; i < sym->nRegs ; i++ ) {
1350 if (sym->regType == REG_PTR)
1351 sym->regs[i] = getRegPtrNoSpil ();
1353 sym->regs[i] = getRegGprNoSpil ();
1356 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1359 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1360 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1361 sym->isspilt = sym->spillA = 0 ;
1362 sym->usl.spillLoc->allocreq--;
1367 /*-----------------------------------------------------------------*/
1368 /* rUmaskForOp :- returns register mask for an operand */
1369 /*-----------------------------------------------------------------*/
1371 mcs51_rUmaskForOp (operand * op)
1377 /* only temporaries are assigned registers */
1381 sym = OP_SYMBOL (op);
1383 /* if spilt or no registers assigned to it
1385 if (sym->isspilt || !sym->nRegs)
1388 rumask = newBitVect (mcs51_nRegs);
1390 for (j = 0; j < sym->nRegs; j++)
1392 rumask = bitVectSetBit (rumask,
1393 sym->regs[j]->rIdx);
1399 /*-----------------------------------------------------------------*/
1400 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1401 /*-----------------------------------------------------------------*/
1403 regsUsedIniCode (iCode * ic)
1405 bitVect *rmask = newBitVect (mcs51_nRegs);
1407 /* do the special cases first */
1410 rmask = bitVectUnion (rmask,
1411 mcs51_rUmaskForOp (IC_COND (ic)));
1415 /* for the jumptable */
1416 if (ic->op == JUMPTABLE)
1418 rmask = bitVectUnion (rmask,
1419 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1424 /* of all other cases */
1426 rmask = bitVectUnion (rmask,
1427 mcs51_rUmaskForOp (IC_LEFT (ic)));
1431 rmask = bitVectUnion (rmask,
1432 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1435 rmask = bitVectUnion (rmask,
1436 mcs51_rUmaskForOp (IC_RESULT (ic)));
1442 /*-----------------------------------------------------------------*/
1443 /* createRegMask - for each instruction will determine the regsUsed */
1444 /*-----------------------------------------------------------------*/
1446 createRegMask (eBBlock ** ebbs, int count)
1450 /* for all blocks */
1451 for (i = 0; i < count; i++)
1455 if (ebbs[i]->noPath &&
1456 (ebbs[i]->entryLabel != entryLabel &&
1457 ebbs[i]->entryLabel != returnLabel))
1460 /* for all instructions */
1461 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1466 if (SKIP_IC2 (ic) || !ic->rlive)
1469 /* first mark the registers used in this
1471 ic->rUsed = regsUsedIniCode (ic);
1472 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1474 /* now create the register mask for those
1475 registers that are in use : this is a
1476 super set of ic->rUsed */
1477 ic->rMask = newBitVect (mcs51_nRegs + 1);
1479 /* for all live Ranges alive at this point */
1480 for (j = 1; j < ic->rlive->size; j++)
1485 /* if not alive then continue */
1486 if (!bitVectBitValue (ic->rlive, j))
1489 /* find the live range we are interested in */
1490 if (!(sym = hTabItemWithKey (liveRanges, j)))
1492 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1493 "createRegMask cannot find live range");
1497 /* if no register assigned to it */
1498 if (!sym->nRegs || sym->isspilt)
1501 /* for all the registers allocated to it */
1502 for (k = 0; k < sym->nRegs; k++)
1505 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1511 /*-----------------------------------------------------------------*/
1512 /* rematStr - returns the rematerialized string for a remat var */
1513 /*-----------------------------------------------------------------*/
1515 rematStr (symbol * sym)
1518 iCode *ic = sym->rematiCode;
1523 /* if plus or minus print the right hand side */
1524 if (ic->op == '+' || ic->op == '-')
1526 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1529 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1533 /* cast then continue */
1534 if (IS_CAST_ICODE(ic)) {
1535 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1538 /* we reached the end */
1539 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1546 /*-----------------------------------------------------------------*/
1547 /* regTypeNum - computes the type & number of registers required */
1548 /*-----------------------------------------------------------------*/
1550 regTypeNum (eBBlock *ebbs)
1556 /* for each live range do */
1557 for (sym = hTabFirstItem (liveRanges, &k); sym;
1558 sym = hTabNextItem (liveRanges, &k))
1561 /* if used zero times then no registers needed */
1562 if ((sym->liveTo - sym->liveFrom) == 0)
1566 /* if the live range is a temporary */
1570 /* if the type is marked as a conditional */
1571 if (sym->regType == REG_CND)
1574 /* if used in return only then we don't
1576 if (sym->ruonly || sym->accuse)
1578 if (IS_AGGREGATE (sym->type) || sym->isptr)
1579 sym->type = aggrToPtr (sym->type, FALSE);
1583 /* if the symbol has only one definition &
1584 that definition is a get_pointer and the
1585 pointer we are getting is rematerializable and
1588 if (bitVectnBitsOn (sym->defs) == 1 &&
1589 (ic = hTabItemWithKey (iCodehTab,
1590 bitVectFirstBit (sym->defs))) &&
1593 !IS_BITVAR (sym->etype))
1597 /* if remat in data space */
1598 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1599 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1600 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1602 /* create a psuedo symbol & force a spil */
1603 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1604 psym->type = sym->type;
1605 psym->etype = sym->etype;
1606 strcpy (psym->rname, psym->name);
1608 sym->usl.spillLoc = psym;
1609 #if 0 // an alternative fix for bug #480076
1610 /* now this is a useless assignment to itself */
1611 remiCodeFromeBBlock (ebbs, ic);
1613 /* now this really is an assignment to itself, make it so;
1614 it will be optimized out later */
1616 IC_RIGHT(ic)=IC_RESULT(ic);
1622 /* if in data space or idata space then try to
1623 allocate pointer register */
1627 /* if not then we require registers */
1628 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1629 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1630 getSize (sym->type));
1634 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1635 printTypeChain (sym->type, stderr);
1636 fprintf (stderr, "\n");
1639 /* determine the type of register required */
1640 if (sym->nRegs == 1 &&
1641 IS_PTR (sym->type) &&
1643 sym->regType = REG_PTR;
1645 sym->regType = REG_GPR;
1649 /* for the first run we don't provide */
1650 /* registers for true symbols we will */
1651 /* see how things go */
1657 /*-----------------------------------------------------------------*/
1658 /* freeAllRegs - mark all registers as free */
1659 /*-----------------------------------------------------------------*/
1665 for (i = 0; i < mcs51_nRegs; i++)
1666 regs8051[i].isFree = 1;
1669 /*-----------------------------------------------------------------*/
1670 /* deallocStackSpil - this will set the stack pointer back */
1671 /*-----------------------------------------------------------------*/
1673 DEFSETFUNC (deallocStackSpil)
1681 /*-----------------------------------------------------------------*/
1682 /* farSpacePackable - returns the packable icode for far variables */
1683 /*-----------------------------------------------------------------*/
1685 farSpacePackable (iCode * ic)
1689 /* go thru till we find a definition for the
1690 symbol on the right */
1691 for (dic = ic->prev; dic; dic = dic->prev)
1693 /* if the definition is a call then no */
1694 if ((dic->op == CALL || dic->op == PCALL) &&
1695 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1700 /* if shift by unknown amount then not */
1701 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1702 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1705 /* if pointer get and size > 1 */
1706 if (POINTER_GET (dic) &&
1707 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1710 if (POINTER_SET (dic) &&
1711 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1714 /* if any three is a true symbol in far space */
1715 if (IC_RESULT (dic) &&
1716 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1717 isOperandInFarSpace (IC_RESULT (dic)))
1720 if (IC_RIGHT (dic) &&
1721 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1722 isOperandInFarSpace (IC_RIGHT (dic)) &&
1723 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1726 if (IC_LEFT (dic) &&
1727 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1728 isOperandInFarSpace (IC_LEFT (dic)) &&
1729 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1732 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1734 if ((dic->op == LEFT_OP ||
1735 dic->op == RIGHT_OP ||
1737 IS_OP_LITERAL (IC_RIGHT (dic)))
1747 /*-----------------------------------------------------------------*/
1748 /* packRegsForAssign - register reduction for assignment */
1749 /*-----------------------------------------------------------------*/
1751 packRegsForAssign (iCode * ic, eBBlock * ebp)
1754 //sym_link *etype = operandType (IC_RIGHT (ic));
1756 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1757 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1758 OP_LIVETO (IC_RIGHT (ic)) > ic->seq
1759 /* why? || IS_BITFIELD (etype) */ )
1764 /* if the true symbol is defined in far space or on stack
1765 then we should not since this will increase register pressure */
1766 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1770 /* find the definition of iTempNN scanning backwards if we find a
1771 a use of the true symbol in before we find the definition then
1773 for (dic = ic->prev; dic; dic = dic->prev)
1775 /* if there is a function call then don't pack it */
1776 if ((dic->op == CALL || dic->op == PCALL))
1785 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1786 IS_OP_VOLATILE (IC_RESULT (dic)))
1792 if (IS_SYMOP (IC_RESULT (dic)) &&
1793 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1795 if (POINTER_SET (dic))
1801 if (IS_SYMOP (IC_RIGHT (dic)) &&
1802 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1803 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1809 if (IS_SYMOP (IC_LEFT (dic)) &&
1810 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1811 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1817 if (POINTER_SET (dic) &&
1818 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1826 return 0; /* did not find */
1828 /* if assignment then check that right is not a bit */
1829 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1831 sym_link *etype = operandType (IC_RIGHT (dic));
1832 if (IS_BITFIELD (etype))
1835 /* if the result is on stack or iaccess then it must be
1836 the same atleast one of the operands */
1837 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1838 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1841 /* the operation has only one symbol
1842 operator then we can pack */
1843 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1844 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1847 if (!((IC_LEFT (dic) &&
1848 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1850 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1854 /* found the definition */
1855 /* replace the result with the result of */
1856 /* this assignment and remove this assignment */
1857 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1858 IC_RESULT (dic) = IC_RESULT (ic);
1860 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1862 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1864 /* delete from liverange table also
1865 delete from all the points inbetween and the new
1867 for (sic = dic; sic != ic; sic = sic->next)
1869 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1870 if (IS_ITEMP (IC_RESULT (dic)))
1871 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1874 remiCodeFromeBBlock (ebp, ic);
1875 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1876 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1877 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1882 /*-----------------------------------------------------------------*/
1883 /* findAssignToSym : scanning backwards looks for first assig found */
1884 /*-----------------------------------------------------------------*/
1886 findAssignToSym (operand * op, iCode * ic)
1890 for (dic = ic->prev; dic; dic = dic->prev)
1893 /* if definition by assignment */
1894 if (dic->op == '=' &&
1895 !POINTER_SET (dic) &&
1896 IC_RESULT (dic)->key == op->key
1897 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1901 /* we are interested only if defined in far space */
1902 /* or in stack space in case of + & - */
1904 /* if assigned to a non-symbol then return
1906 if (!IS_SYMOP (IC_RIGHT (dic)))
1909 /* if the symbol is in far space then
1911 if (isOperandInFarSpace (IC_RIGHT (dic)))
1914 /* for + & - operations make sure that
1915 if it is on the stack it is the same
1916 as one of the three operands */
1917 if ((ic->op == '+' || ic->op == '-') &&
1918 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1921 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1922 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1923 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1931 /* if we find an usage then we cannot delete it */
1932 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1935 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1938 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1942 /* now make sure that the right side of dic
1943 is not defined between ic & dic */
1946 iCode *sic = dic->next;
1948 for (; sic != ic; sic = sic->next)
1949 if (IC_RESULT (sic) &&
1950 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1959 /*-----------------------------------------------------------------*/
1960 /* packRegsForSupport :- reduce some registers for support calls */
1961 /*-----------------------------------------------------------------*/
1963 packRegsForSupport (iCode * ic, eBBlock * ebp)
1968 /* for the left & right operand :- look to see if the
1969 left was assigned a true symbol in far space in that
1970 case replace them */
1972 if (IS_ITEMP (IC_LEFT (ic)) &&
1973 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1975 dic = findAssignToSym (IC_LEFT (ic), ic);
1980 /* found it we need to remove it from the
1982 for (sic = dic; sic != ic; sic = sic->next)
1983 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1985 OP_SYMBOL(IC_LEFT (ic))=OP_SYMBOL(IC_RIGHT (dic));
1986 IC_LEFT (ic)->key = OP_SYMBOL(IC_RIGHT (dic))->key;
1987 remiCodeFromeBBlock (ebp, dic);
1988 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1989 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1993 /* do the same for the right operand */
1996 IS_ITEMP (IC_RIGHT (ic)) &&
1997 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1999 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2005 /* if this is a subtraction & the result
2006 is a true symbol in far space then don't pack */
2007 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2009 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2010 if (IN_FARSPACE (SPEC_OCLS (etype)))
2013 /* found it we need to remove it from the
2015 for (sic = dic; sic != ic; sic = sic->next)
2016 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2018 IC_RIGHT (ic)->operand.symOperand =
2019 IC_RIGHT (dic)->operand.symOperand;
2020 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2022 remiCodeFromeBBlock (ebp, dic);
2023 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2024 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2031 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2034 /*-----------------------------------------------------------------*/
2035 /* packRegsForOneuse : - will reduce some registers for single Use */
2036 /*-----------------------------------------------------------------*/
2038 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2043 /* if returning a literal then do nothing */
2047 /* only upto 2 bytes since we cannot predict
2048 the usage of b, & acc */
2049 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2052 if (ic->op != RETURN &&
2054 !POINTER_SET (ic) &&
2058 /* this routine will mark the a symbol as used in one
2059 instruction use only && if the defintion is local
2060 (ie. within the basic block) && has only one definition &&
2061 that definiion is either a return value from a
2062 function or does not contain any variables in
2064 uses = bitVectCopy (OP_USES (op));
2065 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2066 if (!bitVectIsZero (uses)) /* has other uses */
2069 /* if it has only one defintion */
2070 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2071 return NULL; /* has more than one definition */
2073 /* get that definition */
2075 hTabItemWithKey (iCodehTab,
2076 bitVectFirstBit (OP_DEFS (op)))))
2079 /* if that only usage is a cast */
2080 if (dic->op == CAST) {
2081 /* to a bigger type */
2082 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2083 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2084 /* than we can not, since we cannot predict the usage of b & acc */
2089 /* found the definition now check if it is local */
2090 if (dic->seq < ebp->fSeq ||
2091 dic->seq > ebp->lSeq)
2092 return NULL; /* non-local */
2094 /* now check if it is the return from
2096 if (dic->op == CALL || dic->op == PCALL)
2098 if (ic->op != SEND && ic->op != RETURN &&
2099 !POINTER_SET(ic) && !POINTER_GET(ic))
2101 OP_SYMBOL (op)->ruonly = 1;
2108 /* otherwise check that the definition does
2109 not contain any symbols in far space */
2110 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2111 isOperandInFarSpace (IC_RIGHT (dic)) ||
2112 IS_OP_RUONLY (IC_LEFT (ic)) ||
2113 IS_OP_RUONLY (IC_RIGHT (ic)))
2118 /* if pointer set then make sure the pointer
2120 if (POINTER_SET (dic) &&
2121 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2124 if (POINTER_GET (dic) &&
2125 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2130 /* also make sure the intervenening instructions
2131 don't have any thing in far space */
2132 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2135 /* if there is an intervening function call then no */
2136 if (dic->op == CALL || dic->op == PCALL)
2138 /* if pointer set then make sure the pointer
2140 if (POINTER_SET (dic) &&
2141 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2144 if (POINTER_GET (dic) &&
2145 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2148 /* if address of & the result is remat the okay */
2149 if (dic->op == ADDRESS_OF &&
2150 OP_SYMBOL (IC_RESULT (dic))->remat)
2153 /* if operand has size of three or more & this
2154 operation is a '*','/' or '%' then 'b' may
2156 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2157 getSize (operandType (op)) >= 3)
2160 /* if left or right or result is in far space */
2161 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2162 isOperandInFarSpace (IC_RIGHT (dic)) ||
2163 isOperandInFarSpace (IC_RESULT (dic)) ||
2164 IS_OP_RUONLY (IC_LEFT (dic)) ||
2165 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2166 IS_OP_RUONLY (IC_RESULT (dic)))
2170 /* if left or right or result is on stack */
2171 if (isOperandOnStack(IC_LEFT(dic)) ||
2172 isOperandOnStack(IC_RIGHT(dic)) ||
2173 isOperandOnStack(IC_RESULT(dic))) {
2178 OP_SYMBOL (op)->ruonly = 1;
2183 /*-----------------------------------------------------------------*/
2184 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2185 /*-----------------------------------------------------------------*/
2187 isBitwiseOptimizable (iCode * ic)
2189 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2190 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2192 /* bitwise operations are considered optimizable
2193 under the following conditions (Jean-Louis VERN)
2205 if (IS_LITERAL(rtype) ||
2206 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2212 /*-----------------------------------------------------------------*/
2213 /* packRegsForAccUse - pack registers for acc use */
2214 /*-----------------------------------------------------------------*/
2216 packRegsForAccUse (iCode * ic)
2220 /* if this is an aggregate, e.g. a one byte char array */
2221 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2225 /* if + or - then it has to be one byte result */
2226 if ((ic->op == '+' || ic->op == '-')
2227 && getSize (operandType (IC_RESULT (ic))) > 1)
2230 /* if shift operation make sure right side is not a literal */
2231 if (ic->op == RIGHT_OP &&
2232 (isOperandLiteral (IC_RIGHT (ic)) ||
2233 getSize (operandType (IC_RESULT (ic))) > 1))
2236 if (ic->op == LEFT_OP &&
2237 (isOperandLiteral (IC_RIGHT (ic)) ||
2238 getSize (operandType (IC_RESULT (ic))) > 1))
2241 if (IS_BITWISE_OP (ic) &&
2242 getSize (operandType (IC_RESULT (ic))) > 1)
2246 /* has only one definition */
2247 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2250 /* has only one use */
2251 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2254 /* and the usage immediately follows this iCode */
2255 if (!(uic = hTabItemWithKey (iCodehTab,
2256 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2259 if (ic->next != uic)
2262 /* if it is a conditional branch then we definitely can */
2266 if (uic->op == JUMPTABLE)
2269 /* if the usage is not is an assignment
2270 or an arithmetic / bitwise / shift operation then not */
2271 if (POINTER_SET (uic) &&
2272 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2275 if (uic->op != '=' &&
2276 !IS_ARITHMETIC_OP (uic) &&
2277 !IS_BITWISE_OP (uic) &&
2278 uic->op != LEFT_OP &&
2279 uic->op != RIGHT_OP)
2282 /* if used in ^ operation then make sure right is not a
2284 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2287 /* if shift operation make sure right side is not a literal */
2288 if (uic->op == RIGHT_OP &&
2289 (isOperandLiteral (IC_RIGHT (uic)) ||
2290 getSize (operandType (IC_RESULT (uic))) > 1))
2293 if (uic->op == LEFT_OP &&
2294 (isOperandLiteral (IC_RIGHT (uic)) ||
2295 getSize (operandType (IC_RESULT (uic))) > 1))
2298 /* make sure that the result of this icode is not on the
2299 stack, since acc is used to compute stack offset */
2301 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2302 OP_SYMBOL (IC_RESULT (uic))->onStack)
2305 if (isOperandOnStack(IC_RESULT(uic)))
2309 /* if either one of them in far space then we cannot */
2310 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2311 isOperandInFarSpace (IC_LEFT (uic))) ||
2312 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2313 isOperandInFarSpace (IC_RIGHT (uic))))
2316 /* if the usage has only one operand then we can */
2317 if (IC_LEFT (uic) == NULL ||
2318 IC_RIGHT (uic) == NULL)
2321 /* make sure this is on the left side if not
2322 a '+' since '+' is commutative */
2323 if (ic->op != '+' &&
2324 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2328 // this is too dangerous and need further restrictions
2331 /* if one of them is a literal then we can */
2332 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2333 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2335 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2340 /* if the other one is not on stack then we can */
2341 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2342 (IS_ITEMP (IC_RIGHT (uic)) ||
2343 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2344 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2347 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2348 (IS_ITEMP (IC_LEFT (uic)) ||
2349 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2350 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2356 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2361 /*-----------------------------------------------------------------*/
2362 /* packForPush - hueristics to reduce iCode for pushing */
2363 /*-----------------------------------------------------------------*/
2365 packForPush (iCode * ic, eBBlock * ebp)
2370 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2373 /* must have only definition & one usage */
2374 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2375 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2378 /* find the definition */
2379 if (!(dic = hTabItemWithKey (iCodehTab,
2380 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2383 if (dic->op != '=' || POINTER_SET (dic))
2386 /* make sure the right side does not have any definitions
2388 dbv = OP_DEFS(IC_RIGHT(dic));
2389 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2390 if (bitVectBitValue(dbv,lic->key))
2393 /* make sure they have the same type */
2395 sym_link *itype=operandType(IC_LEFT(ic));
2396 sym_link *ditype=operandType(IC_RIGHT(dic));
2398 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2399 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2402 /* extend the live range of replaced operand if needed */
2403 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2404 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2406 /* we now we know that it has one & only one def & use
2407 and the that the definition is an assignment */
2408 IC_LEFT (ic) = IC_RIGHT (dic);
2410 remiCodeFromeBBlock (ebp, dic);
2411 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2412 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2415 /*-----------------------------------------------------------------*/
2416 /* packRegisters - does some transformations to reduce register */
2418 /*-----------------------------------------------------------------*/
2420 packRegisters (eBBlock * ebp)
2430 /* look for assignments of the form */
2431 /* iTempNN = TRueSym (someoperation) SomeOperand */
2433 /* TrueSym := iTempNN:1 */
2434 for (ic = ebp->sch; ic; ic = ic->next)
2436 /* find assignment of the form TrueSym := iTempNN:1 */
2437 if (ic->op == '=' && !POINTER_SET (ic))
2438 change += packRegsForAssign (ic, ebp);
2445 for (ic = ebp->sch; ic; ic = ic->next)
2447 /* if this is an itemp & result of an address of a true sym
2448 then mark this as rematerialisable */
2449 if (ic->op == ADDRESS_OF &&
2450 IS_ITEMP (IC_RESULT (ic)) &&
2451 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2452 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2453 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2456 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2457 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2458 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2462 /* if straight assignment then carry remat flag if
2463 this is the only definition */
2464 if (ic->op == '=' &&
2465 !POINTER_SET (ic) &&
2466 IS_SYMOP (IC_RIGHT (ic)) &&
2467 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2468 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2469 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2472 OP_SYMBOL (IC_RESULT (ic))->remat =
2473 OP_SYMBOL (IC_RIGHT (ic))->remat;
2474 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2475 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2478 /* if cast to a generic pointer & the pointer being
2479 cast is remat, then we can remat this cast as well */
2480 if (ic->op == CAST &&
2481 IS_SYMOP(IC_RIGHT(ic)) &&
2482 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2483 sym_link *to_type = operandType(IC_LEFT(ic));
2484 sym_link *from_type = operandType(IC_RIGHT(ic));
2485 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2486 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2487 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2488 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2492 /* if this is a +/- operation with a rematerizable
2493 then mark this as rematerializable as well */
2494 if ((ic->op == '+' || ic->op == '-') &&
2495 (IS_SYMOP (IC_LEFT (ic)) &&
2496 IS_ITEMP (IC_RESULT (ic)) &&
2497 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2498 OP_SYMBOL (IC_LEFT (ic))->remat &&
2499 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2500 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2502 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2503 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2504 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2507 /* mark the pointer usages */
2508 if (POINTER_SET (ic))
2509 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2511 if (POINTER_GET (ic))
2512 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2516 /* if we are using a symbol on the stack
2517 then we should say mcs51_ptrRegReq */
2518 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2519 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2520 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2521 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2522 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2523 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2526 if (IS_SYMOP (IC_LEFT (ic)))
2527 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2528 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2529 if (IS_SYMOP (IC_RIGHT (ic)))
2530 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2531 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2532 if (IS_SYMOP (IC_RESULT (ic)))
2533 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2534 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2538 /* if the condition of an if instruction
2539 is defined in the previous instruction and
2540 this is the only usage then
2541 mark the itemp as a conditional */
2542 if ((IS_CONDITIONAL (ic) ||
2543 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2544 ic->next && ic->next->op == IFX &&
2545 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2546 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2547 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2549 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2553 /* reduce for support function calls */
2554 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2555 packRegsForSupport (ic, ebp);
2557 /* some cases the redundant moves can
2558 can be eliminated for return statements */
2559 if ((ic->op == RETURN || ic->op == SEND) &&
2560 !isOperandInFarSpace (IC_LEFT (ic)) &&
2561 options.model == MODEL_SMALL) {
2562 if (0 && options.stackAuto) {
2563 /* we should check here if acc will be clobbered for stack
2564 offset calculations */
2566 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2570 /* if pointer set & left has a size more than
2571 one and right is not in far space */
2572 if (POINTER_SET (ic) &&
2573 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2574 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2575 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2576 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2578 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2580 /* if pointer get */
2581 if (POINTER_GET (ic) &&
2582 !isOperandInFarSpace (IC_RESULT (ic)) &&
2583 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2584 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2585 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2587 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2590 /* if this is cast for intergral promotion then
2591 check if only use of the definition of the
2592 operand being casted/ if yes then replace
2593 the result of that arithmetic operation with
2594 this result and get rid of the cast */
2597 sym_link *fromType = operandType (IC_RIGHT (ic));
2598 sym_link *toType = operandType (IC_LEFT (ic));
2600 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2601 getSize (fromType) != getSize (toType) &&
2602 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2605 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2608 if (IS_ARITHMETIC_OP (dic))
2610 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2611 IC_RESULT (dic) = IC_RESULT (ic);
2612 remiCodeFromeBBlock (ebp, ic);
2613 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2614 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2615 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2619 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2625 /* if the type from and type to are the same
2626 then if this is the only use then packit */
2627 if (compareType (operandType (IC_RIGHT (ic)),
2628 operandType (IC_LEFT (ic))) == 1)
2630 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2633 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2634 IC_RESULT (dic) = IC_RESULT (ic);
2635 remiCodeFromeBBlock (ebp, ic);
2636 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2637 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2638 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2646 iTempNN := (some variable in farspace) V1
2651 if (ic->op == IPUSH)
2653 packForPush (ic, ebp);
2657 /* pack registers for accumulator use, when the
2658 result of an arithmetic or bit wise operation
2659 has only one use, that use is immediately following
2660 the defintion and the using iCode has only one
2661 operand or has two operands but one is literal &
2662 the result of that operation is not on stack then
2663 we can leave the result of this operation in acc:b
2665 if ((IS_ARITHMETIC_OP (ic)
2666 || IS_CONDITIONAL(ic)
2667 || IS_BITWISE_OP (ic)
2668 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2669 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2671 IS_ITEMP (IC_RESULT (ic)) &&
2672 getSize (operandType (IC_RESULT (ic))) <= 2)
2674 packRegsForAccUse (ic);
2678 /*-----------------------------------------------------------------*/
2679 /* assignRegisters - assigns registers to each live range as need */
2680 /*-----------------------------------------------------------------*/
2682 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2687 setToNull ((void *) &_G.funcrUsed);
2688 setToNull ((void *) &_G.totRegAssigned);
2689 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2692 /* change assignments this will remove some
2693 live ranges reducing some register pressure */
2694 for (i = 0; i < count; i++)
2695 packRegisters (ebbs[i]);
2697 if (options.dump_pack)
2698 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2700 /* first determine for each live range the number of
2701 registers & the type of registers required for each */
2704 /* and serially allocate registers */
2705 serialRegAssign (ebbs, count);
2710 /* if stack was extended then tell the user */
2713 /* werror(W_TOOMANY_SPILS,"stack", */
2714 /* _G.stackExtend,currFunc->name,""); */
2720 /* werror(W_TOOMANY_SPILS,"data space", */
2721 /* _G.dataExtend,currFunc->name,""); */
2725 /* after that create the register mask
2726 for each of the instruction */
2727 createRegMask (ebbs, count);
2729 /* redo that offsets for stacked automatic variables */
2730 redoStackOffsets ();
2732 if (options.dump_rassgn)
2734 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2735 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2738 /* do the overlaysegment stuff SDCCmem.c */
2739 doOverlays (ebbs, count);
2741 /* now get back the chain */
2742 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2746 /* free up any _G.stackSpil locations allocated */
2747 applyToSet (_G.stackSpil, deallocStackSpil);
2749 setToNull ((void **) &_G.stackSpil);
2750 setToNull ((void **) &_G.spiltSet);
2751 /* mark all registers as free */