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 *);
47 bitVect *totRegAssigned; /* final set of LRs that got into registers */
50 bitVect *funcrUsed; /* registers used in a function */
56 /* Shared with gen.c */
57 int mcs51_ptrRegReq; /* one byte pointer register required */
63 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
64 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
65 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
66 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
67 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
68 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
69 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
70 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
71 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
72 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
73 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
74 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
75 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
76 {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, 1},
79 static void spillThis (symbol *);
80 static void freeAllRegs ();
82 /*-----------------------------------------------------------------*/
83 /* allocReg - allocates register of given type */
84 /*-----------------------------------------------------------------*/
90 for (i = 0; i < mcs51_nRegs; i++)
93 /* if type is given as 0 then any
94 free register will do */
98 regs8051[i].isFree = 0;
101 bitVectSetBit (currFunc->regsUsed, i);
104 /* other wise look for specific type
106 if (regs8051[i].isFree &&
107 regs8051[i].type == type)
109 regs8051[i].isFree = 0;
112 bitVectSetBit (currFunc->regsUsed, i);
119 /*-----------------------------------------------------------------*/
120 /* mcs51_regWithIdx - returns pointer to register wit index number */
121 /*-----------------------------------------------------------------*/
123 mcs51_regWithIdx (int idx)
127 for (i = 0; i < mcs51_nRegs; i++)
128 if (regs8051[i].rIdx == idx)
131 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
132 "regWithIdx not found");
136 /*-----------------------------------------------------------------*/
137 /* freeReg - frees a register */
138 /*-----------------------------------------------------------------*/
144 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
145 "freeReg - Freeing NULL register");
153 /*-----------------------------------------------------------------*/
154 /* nFreeRegs - returns number of free registers */
155 /*-----------------------------------------------------------------*/
162 for (i = 0; i < mcs51_nRegs; i++)
163 if (regs8051[i].isFree && regs8051[i].type == type)
168 /*-----------------------------------------------------------------*/
169 /* nfreeRegsType - free registers with type */
170 /*-----------------------------------------------------------------*/
172 nfreeRegsType (int type)
177 if ((nfr = nFreeRegs (type)) == 0)
178 return nFreeRegs (REG_GPR);
181 return nFreeRegs (type);
184 /*-----------------------------------------------------------------*/
185 /* useReg - marks a register as used */
186 /*-----------------------------------------------------------------*/
193 /*-----------------------------------------------------------------*/
194 /* allDefsOutOfRange - all definitions are out of a range */
195 /*-----------------------------------------------------------------*/
197 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
204 for (i = 0; i < defs->size; i++)
208 if (bitVectBitValue (defs, i) &&
209 (ic = hTabItemWithKey (iCodehTab, i)) &&
210 (ic->seq >= fseq && ic->seq <= toseq))
219 /*-----------------------------------------------------------------*/
220 /* computeSpillable - given a point find the spillable live ranges */
221 /*-----------------------------------------------------------------*/
223 computeSpillable (iCode * ic)
227 /* spillable live ranges are those that are live at this
228 point . the following categories need to be subtracted
230 a) - those that are already spilt
231 b) - if being used by this one
232 c) - defined by this one */
234 spillable = bitVectCopy (ic->rlive);
236 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
238 bitVectCplAnd (spillable, ic->uses); /* used in this one */
239 bitVectUnSetBit (spillable, ic->defKey);
240 spillable = bitVectIntersect (spillable, _G.regAssigned);
245 /*-----------------------------------------------------------------*/
246 /* noSpilLoc - return true if a variable has no spil location */
247 /*-----------------------------------------------------------------*/
249 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
251 return (sym->usl.spillLoc ? 0 : 1);
254 /*-----------------------------------------------------------------*/
255 /* hasSpilLoc - will return 1 if the symbol has spil location */
256 /*-----------------------------------------------------------------*/
258 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
260 return (sym->usl.spillLoc ? 1 : 0);
263 /*-----------------------------------------------------------------*/
264 /* directSpilLoc - will return 1 if the splilocation is in direct */
265 /*-----------------------------------------------------------------*/
267 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
269 if (sym->usl.spillLoc &&
270 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
276 /*-----------------------------------------------------------------*/
277 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
278 /* but is not used as a pointer */
279 /*-----------------------------------------------------------------*/
281 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
283 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
286 /*-----------------------------------------------------------------*/
287 /* rematable - will return 1 if the remat flag is set */
288 /*-----------------------------------------------------------------*/
290 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
295 /*-----------------------------------------------------------------*/
296 /* notUsedInBlock - not used in this block */
297 /*-----------------------------------------------------------------*/
299 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
301 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
302 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
303 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
306 /*-----------------------------------------------------------------*/
307 /* notUsedInRemaining - not used or defined in remain of the block */
308 /*-----------------------------------------------------------------*/
310 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
312 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
313 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
316 /*-----------------------------------------------------------------*/
317 /* allLRs - return true for all */
318 /*-----------------------------------------------------------------*/
320 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
325 /*-----------------------------------------------------------------*/
326 /* liveRangesWith - applies function to a given set of live range */
327 /*-----------------------------------------------------------------*/
329 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
330 eBBlock * ebp, iCode * ic)
335 if (!lrs || !lrs->size)
338 for (i = 1; i < lrs->size; i++)
341 if (!bitVectBitValue (lrs, i))
344 /* if we don't find it in the live range
345 hash table we are in serious trouble */
346 if (!(sym = hTabItemWithKey (liveRanges, i)))
348 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
349 "liveRangesWith could not find liveRange");
353 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
354 addSetHead (&rset, sym);
361 /*-----------------------------------------------------------------*/
362 /* leastUsedLR - given a set determines which is the least used */
363 /*-----------------------------------------------------------------*/
365 leastUsedLR (set * sset)
367 symbol *sym = NULL, *lsym = NULL;
369 sym = lsym = setFirstItem (sset);
374 for (; lsym; lsym = setNextItem (sset))
377 /* if usage is the same then prefer
378 the spill the smaller of the two */
379 if (lsym->used == sym->used)
380 if (getSize (lsym->type) < getSize (sym->type))
384 if (lsym->used < sym->used)
389 setToNull ((void **) &sset);
394 /*-----------------------------------------------------------------*/
395 /* noOverLap - will iterate through the list looking for over lap */
396 /*-----------------------------------------------------------------*/
398 noOverLap (set * itmpStack, symbol * fsym)
403 for (sym = setFirstItem (itmpStack); sym;
404 sym = setNextItem (itmpStack))
406 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
412 /*-----------------------------------------------------------------*/
413 /* isFree - will return 1 if the a free spil location is found */
414 /*-----------------------------------------------------------------*/
419 V_ARG (symbol **, sloc);
420 V_ARG (symbol *, fsym);
422 /* if already found */
426 /* if it is free && and the itmp assigned to
427 this does not have any overlapping live ranges
428 with the one currently being assigned and
429 the size can be accomodated */
431 noOverLap (sym->usl.itmpStack, fsym) &&
432 getSize (sym->type) >= getSize (fsym->type))
441 /*-----------------------------------------------------------------*/
442 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
443 /*-----------------------------------------------------------------*/
445 spillLRWithPtrReg (symbol * forSym)
451 if (!_G.regAssigned ||
452 bitVectIsZero (_G.regAssigned))
455 r0 = mcs51_regWithIdx (R0_IDX);
456 r1 = mcs51_regWithIdx (R1_IDX);
458 /* for all live ranges */
459 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
460 lrsym = hTabNextItem (liveRanges, &k))
464 /* if no registers assigned to it or spilt */
465 /* if it does not overlap with this then
466 not need to spill it */
468 if (lrsym->isspilt || !lrsym->nRegs ||
469 (lrsym->liveTo < forSym->liveFrom))
472 /* go thru the registers : if it is either
473 r0 or r1 then spil it */
474 for (j = 0; j < lrsym->nRegs; j++)
475 if (lrsym->regs[j] == r0 ||
476 lrsym->regs[j] == r1)
485 /*-----------------------------------------------------------------*/
486 /* createStackSpil - create a location on the stack to spil */
487 /*-----------------------------------------------------------------*/
489 createStackSpil (symbol * sym)
492 int useXstack, model;
496 /* first go try and find a free one that is already
497 existing on the stack */
498 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
500 /* found a free one : just update & return */
501 sym->usl.spillLoc = sloc;
504 addSetHead (&sloc->usl.itmpStack, sym);
508 /* could not then have to create one , this is the hard part
509 we need to allocate this on the stack : this is really a
510 hack!! but cannot think of anything better at this time */
512 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
514 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
519 sloc = newiTemp (slocBuffer);
521 /* set the type to the spilling symbol */
522 sloc->type = copyLinkChain (sym->type);
523 sloc->etype = getSpec (sloc->type);
524 SPEC_SCLS (sloc->etype) = S_DATA;
525 SPEC_EXTR (sloc->etype) = 0;
526 SPEC_STAT (sloc->etype) = 0;
527 SPEC_VOLATILE(sloc->etype) = 0;
529 /* we don't allow it to be allocated`
530 onto the external stack since : so we
531 temporarily turn it off ; we also
532 turn off memory model to prevent
533 the spil from going to the external storage
536 useXstack = options.useXstack;
537 model = options.model;
538 /* noOverlay = options.noOverlay; */
539 /* options.noOverlay = 1; */
540 options.model = options.useXstack = 0;
544 options.useXstack = useXstack;
545 options.model = model;
546 /* options.noOverlay = noOverlay; */
547 sloc->isref = 1; /* to prevent compiler warning */
549 /* if it is on the stack then update the stack */
550 if (IN_STACK (sloc->etype))
552 currFunc->stack += getSize (sloc->type);
553 _G.stackExtend += getSize (sloc->type);
556 _G.dataExtend += getSize (sloc->type);
558 /* add it to the _G.stackSpil set */
559 addSetHead (&_G.stackSpil, sloc);
560 sym->usl.spillLoc = sloc;
563 /* add it to the set of itempStack set
564 of the spill location */
565 addSetHead (&sloc->usl.itmpStack, sym);
569 /*-----------------------------------------------------------------*/
570 /* isSpiltOnStack - returns true if the spil location is on stack */
571 /*-----------------------------------------------------------------*/
573 isSpiltOnStack (symbol * sym)
583 /* if (sym->_G.stackSpil) */
586 if (!sym->usl.spillLoc)
589 etype = getSpec (sym->usl.spillLoc->type);
590 if (IN_STACK (etype))
596 /*-----------------------------------------------------------------*/
597 /* spillThis - spils a specific operand */
598 /*-----------------------------------------------------------------*/
600 spillThis (symbol * sym)
603 /* if this is rematerializable or has a spillLocation
604 we are okay, else we need to create a spillLocation
606 if (!(sym->remat || sym->usl.spillLoc))
607 createStackSpil (sym);
610 /* mark it has spilt & put it in the spilt set */
611 sym->isspilt = sym->spillA = 1;
612 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
614 bitVectUnSetBit (_G.regAssigned, sym->key);
615 bitVectUnSetBit (_G.totRegAssigned, sym->key);
617 for (i = 0; i < sym->nRegs; i++)
621 freeReg (sym->regs[i]);
625 /* if spilt on stack then free up r0 & r1
626 if they could have been assigned to some
628 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
631 spillLRWithPtrReg (sym);
634 if (sym->usl.spillLoc && !sym->remat)
635 sym->usl.spillLoc->allocreq++;
639 /*-----------------------------------------------------------------*/
640 /* selectSpil - select a iTemp to spil : rather a simple procedure */
641 /*-----------------------------------------------------------------*/
643 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
645 bitVect *lrcs = NULL;
649 /* get the spillable live ranges */
650 lrcs = computeSpillable (ic);
652 /* get all live ranges that are rematerizable */
653 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
656 /* return the least used of these */
657 return leastUsedLR (selectS);
660 /* get live ranges with spillLocations in direct space */
661 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
663 sym = leastUsedLR (selectS);
664 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
665 sym->usl.spillLoc->rname :
666 sym->usl.spillLoc->name));
668 /* mark it as allocation required */
669 sym->usl.spillLoc->allocreq++;
673 /* if the symbol is local to the block then */
674 if (forSym->liveTo < ebp->lSeq)
677 /* check if there are any live ranges allocated
678 to registers that are not used in this block */
679 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
681 sym = leastUsedLR (selectS);
682 /* if this is not rematerializable */
691 /* check if there are any live ranges that not
692 used in the remainder of the block */
693 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
695 sym = leastUsedLR (selectS);
708 /* find live ranges with spillocation && not used as pointers */
709 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
712 sym = leastUsedLR (selectS);
713 /* mark this as allocation required */
714 sym->usl.spillLoc->allocreq++;
718 /* find live ranges with spillocation */
719 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
722 sym = leastUsedLR (selectS);
723 sym->usl.spillLoc->allocreq++;
727 /* couldn't find then we need to create a spil
728 location on the stack , for which one? the least
730 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
733 /* return a created spil location */
734 sym = createStackSpil (leastUsedLR (selectS));
735 sym->usl.spillLoc->allocreq++;
739 /* this is an extreme situation we will spill
740 this one : happens very rarely but it does happen */
746 /*-----------------------------------------------------------------*/
747 /* spilSomething - spil some variable & mark registers as free */
748 /*-----------------------------------------------------------------*/
750 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
755 /* get something we can spil */
756 ssym = selectSpil (ic, ebp, forSym);
758 /* mark it as spilt */
759 ssym->isspilt = ssym->spillA = 1;
760 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
762 /* mark it as not register assigned &
763 take it away from the set */
764 bitVectUnSetBit (_G.regAssigned, ssym->key);
765 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
767 /* mark the registers as free */
768 for (i = 0; i < ssym->nRegs; i++)
770 freeReg (ssym->regs[i]);
772 /* if spilt on stack then free up r0 & r1
773 if they could have been assigned to as gprs */
774 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
777 spillLRWithPtrReg (ssym);
780 /* if this was a block level spil then insert push & pop
781 at the start & end of block respectively */
784 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
785 /* add push to the start of the block */
786 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
787 ebp->sch->next : ebp->sch));
788 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
789 /* add pop to the end of the block */
790 addiCodeToeBBlock (ebp, nic, NULL);
793 /* if spilt because not used in the remainder of the
794 block then add a push before this instruction and
795 a pop at the end of the block */
796 if (ssym->remainSpil)
799 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
800 /* add push just before this instruction */
801 addiCodeToeBBlock (ebp, nic, ic);
803 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
804 /* add pop to the end of the block */
805 addiCodeToeBBlock (ebp, nic, NULL);
814 /*-----------------------------------------------------------------*/
815 /* getRegPtr - will try for PTR if not a GPR type if not spil */
816 /*-----------------------------------------------------------------*/
818 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
823 /* try for a ptr type */
824 if ((reg = allocReg (REG_PTR)))
827 /* try for gpr type */
828 if ((reg = allocReg (REG_GPR)))
831 /* we have to spil */
832 if (!spilSomething (ic, ebp, sym))
835 /* this looks like an infinite loop but
836 in really selectSpil will abort */
840 /*-----------------------------------------------------------------*/
841 /* getRegGpr - will try for GPR if not spil */
842 /*-----------------------------------------------------------------*/
844 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
849 /* try for gpr type */
850 if ((reg = allocReg (REG_GPR)))
853 if (!mcs51_ptrRegReq)
854 if ((reg = allocReg (REG_PTR)))
857 /* we have to spil */
858 if (!spilSomething (ic, ebp, sym))
861 /* this looks like an infinite loop but
862 in really selectSpil will abort */
866 /*-----------------------------------------------------------------*/
867 /* getRegPtrNoSpil - get it cannot split */
868 /*-----------------------------------------------------------------*/
869 static regs *getRegPtrNoSpil()
873 /* try for a ptr type */
874 if ((reg = allocReg (REG_PTR)))
877 /* try for gpr type */
878 if ((reg = allocReg (REG_GPR)))
884 /*-----------------------------------------------------------------*/
885 /* getRegGprNoSpil - get it cannot split */
886 /*-----------------------------------------------------------------*/
887 static regs *getRegGprNoSpil()
891 if ((reg = allocReg (REG_GPR)))
894 if (!mcs51_ptrRegReq)
895 if ((reg = allocReg (REG_PTR)))
901 /*-----------------------------------------------------------------*/
902 /* symHasReg - symbol has a given register */
903 /*-----------------------------------------------------------------*/
905 symHasReg (symbol * sym, regs * reg)
909 for (i = 0; i < sym->nRegs; i++)
910 if (sym->regs[i] == reg)
916 /*-----------------------------------------------------------------*/
917 /* deassignLRs - check the live to and if they have registers & are */
918 /* not spilt then free up the registers */
919 /*-----------------------------------------------------------------*/
921 deassignLRs (iCode * ic, eBBlock * ebp)
927 for (sym = hTabFirstItem (liveRanges, &k); sym;
928 sym = hTabNextItem (liveRanges, &k))
932 /* if it does not end here */
933 if (sym->liveTo > ic->seq)
936 /* if it was spilt on stack then we can
937 mark the stack spil location as free */
942 sym->usl.spillLoc->isFree = 1;
948 if (!bitVectBitValue (_G.regAssigned, sym->key))
951 /* special case check if this is an IFX &
952 the privious one was a pop and the
953 previous one was not spilt then keep track
955 if (ic->op == IFX && ic->prev &&
956 ic->prev->op == IPOP &&
957 !ic->prev->parmPush &&
958 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
959 psym = OP_SYMBOL (IC_LEFT (ic->prev));
965 bitVectUnSetBit (_G.regAssigned, sym->key);
967 /* if the result of this one needs registers
968 and does not have it then assign it right
970 if (IC_RESULT (ic) &&
971 !(SKIP_IC2 (ic) || /* not a special icode */
972 ic->op == JUMPTABLE ||
978 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
979 result->liveTo > ic->seq && /* and will live beyond this */
980 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
981 result->regType == sym->regType && /* same register types */
982 result->nRegs && /* which needs registers */
983 !result->isspilt && /* and does not already have them */
985 !bitVectBitValue (_G.regAssigned, result->key) &&
986 /* the number of free regs + number of regs in this LR
987 can accomodate the what result Needs */
988 ((nfreeRegsType (result->regType) +
989 sym->nRegs) >= result->nRegs)
993 for (i = 0; i < result->nRegs; i++)
995 result->regs[i] = sym->regs[i];
997 result->regs[i] = getRegGpr (ic, ebp, result);
999 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1000 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1004 /* free the remaining */
1005 for (; i < sym->nRegs; i++)
1009 if (!symHasReg (psym, sym->regs[i]))
1010 freeReg (sym->regs[i]);
1013 freeReg (sym->regs[i]);
1020 /*-----------------------------------------------------------------*/
1021 /* reassignLR - reassign this to registers */
1022 /*-----------------------------------------------------------------*/
1024 reassignLR (operand * op)
1026 symbol *sym = OP_SYMBOL (op);
1029 /* not spilt any more */
1030 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1031 bitVectUnSetBit (_G.spiltSet, sym->key);
1033 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1034 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1038 for (i = 0; i < sym->nRegs; i++)
1039 sym->regs[i]->isFree = 0;
1042 /*-----------------------------------------------------------------*/
1043 /* willCauseSpill - determines if allocating will cause a spill */
1044 /*-----------------------------------------------------------------*/
1046 willCauseSpill (int nr, int rt)
1048 /* first check if there are any avlb registers
1049 of te type required */
1052 /* special case for pointer type
1053 if pointer type not avlb then
1054 check for type gpr */
1055 if (nFreeRegs (rt) >= nr)
1057 if (nFreeRegs (REG_GPR) >= nr)
1062 if (mcs51_ptrRegReq)
1064 if (nFreeRegs (rt) >= nr)
1069 if (nFreeRegs (REG_PTR) +
1070 nFreeRegs (REG_GPR) >= nr)
1075 /* it will cause a spil */
1079 /*-----------------------------------------------------------------*/
1080 /* positionRegs - the allocator can allocate same registers to res- */
1081 /* ult and operand, if this happens make sure they are in the same */
1082 /* position as the operand otherwise chaos results */
1083 /*-----------------------------------------------------------------*/
1085 positionRegs (symbol * result, symbol * opsym)
1087 int count = min (result->nRegs, opsym->nRegs);
1088 int i, j = 0, shared = 0;
1091 /* if the result has been spilt then cannot share */
1096 /* first make sure that they actually share */
1097 for (i = 0; i < count; i++)
1099 for (j = 0; j < count; j++)
1101 if (result->regs[i] == opsym->regs[j] && i != j)
1111 regs *tmp = result->regs[i];
1112 result->regs[i] = result->regs[j];
1113 result->regs[j] = tmp;
1120 /*-----------------------------------------------------------------*/
1121 /* serialRegAssign - serially allocate registers to the variables */
1122 /*-----------------------------------------------------------------*/
1124 serialRegAssign (eBBlock ** ebbs, int count)
1128 /* for all blocks */
1129 for (i = 0; i < count; i++) {
1133 if (ebbs[i]->noPath &&
1134 (ebbs[i]->entryLabel != entryLabel &&
1135 ebbs[i]->entryLabel != returnLabel))
1138 /* of all instructions do */
1139 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1141 /* if this is an ipop that means some live
1142 range will have to be assigned again */
1144 reassignLR (IC_LEFT (ic));
1146 /* if result is present && is a true symbol */
1147 if (IC_RESULT (ic) && ic->op != IFX &&
1148 IS_TRUE_SYMOP (IC_RESULT (ic)))
1149 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1151 /* take away registers from live
1152 ranges that end at this instruction */
1153 deassignLRs (ic, ebbs[i]);
1155 /* some don't need registers */
1156 if (SKIP_IC2 (ic) ||
1157 ic->op == JUMPTABLE ||
1161 (IC_RESULT (ic) && POINTER_SET (ic)))
1164 /* now we need to allocate registers
1165 only for the result */
1166 if (IC_RESULT (ic)) {
1167 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1173 /* if it does not need or is spilt
1174 or is already assigned to registers
1175 or will not live beyond this instructions */
1178 bitVectBitValue (_G.regAssigned, sym->key) ||
1179 sym->liveTo <= ic->seq)
1182 /* if some liverange has been spilt at the block level
1183 and this one live beyond this block then spil this
1185 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1189 /* if trying to allocate this will cause
1190 a spill and there is nothing to spill
1191 or this one is rematerializable then
1193 willCS = willCauseSpill (sym->nRegs, sym->regType);
1194 spillable = computeSpillable (ic);
1195 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1200 /* if it has a spillocation & is used less than
1201 all other live ranges then spill this */
1203 if (sym->usl.spillLoc) {
1204 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1205 allLRs, ebbs[i], ic));
1206 if (leastUsed && leastUsed->used > sym->used) {
1211 /* if none of the liveRanges have a spillLocation then better
1212 to spill this one than anything else already assigned to registers */
1213 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1214 /* if this is local to this block then we might find a block spil */
1215 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1222 /* if we need ptr regs for the right side
1224 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1225 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1229 /* else we assign registers to it */
1230 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1231 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1233 for (j = 0; j < sym->nRegs; j++) {
1234 if (sym->regType == REG_PTR)
1235 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1237 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1239 /* if the allocation failed which means
1240 this was spilt then break */
1241 if (!sym->regs[j]) {
1243 fprintf (stderr, "%d reg(s) lost in %s:%d\n",
1244 j, __FILE__,__LINE__);
1250 /* if it shares registers with operands make sure
1251 that they are in the same position */
1252 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1253 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1254 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1255 OP_SYMBOL (IC_LEFT (ic)));
1257 /* do the same for the right operand */
1258 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1259 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1260 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1261 OP_SYMBOL (IC_RIGHT (ic)));
1274 /*-----------------------------------------------------------------*/
1275 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1276 /*-----------------------------------------------------------------*/
1277 static void fillGaps()
1282 if (getenv("DISABLE_FILL_GAPS")) return;
1284 /* look for livernages that was spilt by the allocator */
1285 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1286 sym = hTabNextItem(liveRanges,&key)) {
1291 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1293 /* find the liveRanges this one clashes with, that are
1294 still assigned to registers & mark the registers as used*/
1295 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1299 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1300 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1303 assert (clr = hTabItemWithKey(liveRanges,i));
1305 /* mark these registers as used */
1306 for (k = 0 ; k < clr->nRegs ; k++ )
1307 useReg(clr->regs[k]);
1310 if (willCauseSpill(sym->nRegs,sym->regType)) {
1311 /* NOPE :( clear all registers & and continue */
1316 /* THERE IS HOPE !!!! */
1317 for (i=0; i < sym->nRegs ; i++ ) {
1318 sym->regs[i] = getRegGprNoSpil ();
1321 /* for all its definitions check if the registers
1322 allocated needs positioning NOTE: we can position
1323 only ONCE if more than One positioning required
1326 for (i = 0 ; i < sym->defs->size ; i++ ) {
1327 if (bitVectBitValue(sym->defs,i)) {
1329 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1330 if (SKIP_IC(ic)) continue;
1331 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1332 /* if left is assigned to registers */
1333 if (IS_SYMOP(IC_LEFT(ic)) &&
1334 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1335 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1337 if (IS_SYMOP(IC_RIGHT(ic)) &&
1338 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1339 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1341 if (pdone > 1) break;
1344 /* had to position more than once GIVE UP */
1346 /* UNDO all the changes we made to try this */
1348 for (i=0; i < sym->nRegs ; i++ ) {
1349 if (sym->regType == REG_PTR)
1350 sym->regs[i] = getRegPtrNoSpil ();
1352 sym->regs[i] = getRegGprNoSpil ();
1355 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1358 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1359 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1360 sym->isspilt = sym->spillA = 0 ;
1361 sym->usl.spillLoc->allocreq--;
1366 /*-----------------------------------------------------------------*/
1367 /* rUmaskForOp :- returns register mask for an operand */
1368 /*-----------------------------------------------------------------*/
1370 mcs51_rUmaskForOp (operand * op)
1376 /* only temporaries are assigned registers */
1380 sym = OP_SYMBOL (op);
1382 /* if spilt or no registers assigned to it
1384 if (sym->isspilt || !sym->nRegs)
1387 rumask = newBitVect (mcs51_nRegs);
1389 for (j = 0; j < sym->nRegs; j++)
1391 rumask = bitVectSetBit (rumask,
1392 sym->regs[j]->rIdx);
1398 /*-----------------------------------------------------------------*/
1399 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1400 /*-----------------------------------------------------------------*/
1402 regsUsedIniCode (iCode * ic)
1404 bitVect *rmask = newBitVect (mcs51_nRegs);
1406 /* do the special cases first */
1409 rmask = bitVectUnion (rmask,
1410 mcs51_rUmaskForOp (IC_COND (ic)));
1414 /* for the jumptable */
1415 if (ic->op == JUMPTABLE)
1417 rmask = bitVectUnion (rmask,
1418 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1423 /* of all other cases */
1425 rmask = bitVectUnion (rmask,
1426 mcs51_rUmaskForOp (IC_LEFT (ic)));
1430 rmask = bitVectUnion (rmask,
1431 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1434 rmask = bitVectUnion (rmask,
1435 mcs51_rUmaskForOp (IC_RESULT (ic)));
1441 /*-----------------------------------------------------------------*/
1442 /* createRegMask - for each instruction will determine the regsUsed */
1443 /*-----------------------------------------------------------------*/
1445 createRegMask (eBBlock ** ebbs, int count)
1449 /* for all blocks */
1450 for (i = 0; i < count; i++)
1454 if (ebbs[i]->noPath &&
1455 (ebbs[i]->entryLabel != entryLabel &&
1456 ebbs[i]->entryLabel != returnLabel))
1459 /* for all instructions */
1460 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1465 if (SKIP_IC2 (ic) || !ic->rlive)
1468 /* first mark the registers used in this
1470 ic->rUsed = regsUsedIniCode (ic);
1471 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1473 /* now create the register mask for those
1474 registers that are in use : this is a
1475 super set of ic->rUsed */
1476 ic->rMask = newBitVect (mcs51_nRegs + 1);
1478 /* for all live Ranges alive at this point */
1479 for (j = 1; j < ic->rlive->size; j++)
1484 /* if not alive then continue */
1485 if (!bitVectBitValue (ic->rlive, j))
1488 /* find the live range we are interested in */
1489 if (!(sym = hTabItemWithKey (liveRanges, j)))
1491 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1492 "createRegMask cannot find live range");
1496 /* if no register assigned to it */
1497 if (!sym->nRegs || sym->isspilt)
1500 /* for all the registers allocated to it */
1501 for (k = 0; k < sym->nRegs; k++)
1504 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1510 /*-----------------------------------------------------------------*/
1511 /* rematStr - returns the rematerialized string for a remat var */
1512 /*-----------------------------------------------------------------*/
1514 rematStr (symbol * sym)
1517 iCode *ic = sym->rematiCode;
1522 /* if plus or minus print the right hand side */
1523 if (ic->op == '+' || ic->op == '-')
1525 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1528 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1532 /* cast then continue */
1533 if (IS_CAST_ICODE(ic)) {
1534 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1537 /* we reached the end */
1538 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1545 /*-----------------------------------------------------------------*/
1546 /* regTypeNum - computes the type & number of registers required */
1547 /*-----------------------------------------------------------------*/
1549 regTypeNum (eBBlock *ebbs)
1555 /* for each live range do */
1556 for (sym = hTabFirstItem (liveRanges, &k); sym;
1557 sym = hTabNextItem (liveRanges, &k))
1560 /* if used zero times then no registers needed */
1561 if ((sym->liveTo - sym->liveFrom) == 0)
1565 /* if the live range is a temporary */
1569 /* if the type is marked as a conditional */
1570 if (sym->regType == REG_CND)
1573 /* if used in return only then we don't
1575 if (sym->ruonly || sym->accuse)
1577 if (IS_AGGREGATE (sym->type) || sym->isptr)
1578 sym->type = aggrToPtr (sym->type, FALSE);
1582 /* if the symbol has only one definition &
1583 that definition is a get_pointer and the
1584 pointer we are getting is rematerializable and
1587 if (bitVectnBitsOn (sym->defs) == 1 &&
1588 (ic = hTabItemWithKey (iCodehTab,
1589 bitVectFirstBit (sym->defs))) &&
1592 !IS_BITVAR (sym->etype))
1596 /* if remat in data space */
1597 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1598 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1599 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1601 /* create a psuedo symbol & force a spil */
1602 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1603 psym->type = sym->type;
1604 psym->etype = sym->etype;
1605 strcpy (psym->rname, psym->name);
1607 sym->usl.spillLoc = psym;
1608 #if 0 // an alternative fix for bug #480076
1609 /* now this is a useless assignment to itself */
1610 remiCodeFromeBBlock (ebbs, ic);
1612 /* now this really is an assignment to itself, make it so;
1613 it will be optimized out later */
1615 IC_RIGHT(ic)=IC_RESULT(ic);
1621 /* if in data space or idata space then try to
1622 allocate pointer register */
1626 /* if not then we require registers */
1627 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1628 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1629 getSize (sym->type));
1633 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1634 printTypeChain (sym->type, stderr);
1635 fprintf (stderr, "\n");
1638 /* determine the type of register required */
1639 if (sym->nRegs == 1 &&
1640 IS_PTR (sym->type) &&
1642 sym->regType = REG_PTR;
1644 sym->regType = REG_GPR;
1648 /* for the first run we don't provide */
1649 /* registers for true symbols we will */
1650 /* see how things go */
1656 /*-----------------------------------------------------------------*/
1657 /* freeAllRegs - mark all registers as free */
1658 /*-----------------------------------------------------------------*/
1664 for (i = 0; i < mcs51_nRegs; i++)
1665 regs8051[i].isFree = 1;
1668 /*-----------------------------------------------------------------*/
1669 /* deallocStackSpil - this will set the stack pointer back */
1670 /*-----------------------------------------------------------------*/
1672 DEFSETFUNC (deallocStackSpil)
1680 /*-----------------------------------------------------------------*/
1681 /* farSpacePackable - returns the packable icode for far variables */
1682 /*-----------------------------------------------------------------*/
1684 farSpacePackable (iCode * ic)
1688 /* go thru till we find a definition for the
1689 symbol on the right */
1690 for (dic = ic->prev; dic; dic = dic->prev)
1692 /* if the definition is a call then no */
1693 if ((dic->op == CALL || dic->op == PCALL) &&
1694 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1699 /* if shift by unknown amount then not */
1700 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1701 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1704 /* if pointer get and size > 1 */
1705 if (POINTER_GET (dic) &&
1706 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1709 if (POINTER_SET (dic) &&
1710 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1713 /* if any three is a true symbol in far space */
1714 if (IC_RESULT (dic) &&
1715 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1716 isOperandInFarSpace (IC_RESULT (dic)))
1719 if (IC_RIGHT (dic) &&
1720 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1721 isOperandInFarSpace (IC_RIGHT (dic)) &&
1722 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1725 if (IC_LEFT (dic) &&
1726 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1727 isOperandInFarSpace (IC_LEFT (dic)) &&
1728 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1731 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1733 if ((dic->op == LEFT_OP ||
1734 dic->op == RIGHT_OP ||
1736 IS_OP_LITERAL (IC_RIGHT (dic)))
1746 /*-----------------------------------------------------------------*/
1747 /* packRegsForAssign - register reduction for assignment */
1748 /*-----------------------------------------------------------------*/
1750 packRegsForAssign (iCode * ic, eBBlock * ebp)
1753 //sym_link *etype = operandType (IC_RIGHT (ic));
1755 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1756 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1757 OP_LIVETO (IC_RIGHT (ic)) > ic->seq
1758 /* why? || IS_BITFIELD (etype) */ )
1763 /* if the true symbol is defined in far space or on stack
1764 then we should not since this will increase register pressure */
1765 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1769 /* find the definition of iTempNN scanning backwards if we find a
1770 a use of the true symbol in before we find the definition then
1772 for (dic = ic->prev; dic; dic = dic->prev)
1774 /* if there is a function call then don't pack it */
1775 if ((dic->op == CALL || dic->op == PCALL))
1784 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1785 IS_OP_VOLATILE (IC_RESULT (dic)))
1791 if (IS_SYMOP (IC_RESULT (dic)) &&
1792 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1794 if (POINTER_SET (dic))
1800 if (IS_SYMOP (IC_RIGHT (dic)) &&
1801 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1802 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1808 if (IS_SYMOP (IC_LEFT (dic)) &&
1809 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1810 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1816 if (POINTER_SET (dic) &&
1817 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1825 return 0; /* did not find */
1827 /* if assignment then check that right is not a bit */
1828 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1830 sym_link *etype = operandType (IC_RIGHT (dic));
1831 if (IS_BITFIELD (etype))
1834 /* if the result is on stack or iaccess then it must be
1835 the same atleast one of the operands */
1836 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1837 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1840 /* the operation has only one symbol
1841 operator then we can pack */
1842 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1843 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1846 if (!((IC_LEFT (dic) &&
1847 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1849 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1853 /* found the definition */
1854 /* replace the result with the result of */
1855 /* this assignment and remove this assignment */
1856 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1857 IC_RESULT (dic) = IC_RESULT (ic);
1859 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1861 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1863 /* delete from liverange table also
1864 delete from all the points inbetween and the new
1866 for (sic = dic; sic != ic; sic = sic->next)
1868 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1869 if (IS_ITEMP (IC_RESULT (dic)))
1870 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1873 remiCodeFromeBBlock (ebp, ic);
1874 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1875 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1876 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1881 /*-----------------------------------------------------------------*/
1882 /* findAssignToSym : scanning backwards looks for first assig found */
1883 /*-----------------------------------------------------------------*/
1885 findAssignToSym (operand * op, iCode * ic)
1889 for (dic = ic->prev; dic; dic = dic->prev)
1892 /* if definition by assignment */
1893 if (dic->op == '=' &&
1894 !POINTER_SET (dic) &&
1895 IC_RESULT (dic)->key == op->key
1896 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1900 /* we are interested only if defined in far space */
1901 /* or in stack space in case of + & - */
1903 /* if assigned to a non-symbol then return
1905 if (!IS_SYMOP (IC_RIGHT (dic)))
1908 /* if the symbol is in far space then
1910 if (isOperandInFarSpace (IC_RIGHT (dic)))
1913 /* for + & - operations make sure that
1914 if it is on the stack it is the same
1915 as one of the three operands */
1916 if ((ic->op == '+' || ic->op == '-') &&
1917 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1920 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1921 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1922 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1930 /* if we find an usage then we cannot delete it */
1931 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1934 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1937 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1941 /* now make sure that the right side of dic
1942 is not defined between ic & dic */
1945 iCode *sic = dic->next;
1947 for (; sic != ic; sic = sic->next)
1948 if (IC_RESULT (sic) &&
1949 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1958 /*-----------------------------------------------------------------*/
1959 /* packRegsForSupport :- reduce some registers for support calls */
1960 /*-----------------------------------------------------------------*/
1962 packRegsForSupport (iCode * ic, eBBlock * ebp)
1967 /* for the left & right operand :- look to see if the
1968 left was assigned a true symbol in far space in that
1969 case replace them */
1971 if (IS_ITEMP (IC_LEFT (ic)) &&
1972 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1974 dic = findAssignToSym (IC_LEFT (ic), ic);
1979 /* found it we need to remove it from the
1981 for (sic = dic; sic != ic; sic = sic->next)
1982 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1984 OP_SYMBOL(IC_LEFT (ic))=OP_SYMBOL(IC_RIGHT (dic));
1985 IC_LEFT (ic)->key = OP_SYMBOL(IC_RIGHT (dic))->key;
1986 remiCodeFromeBBlock (ebp, dic);
1987 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1988 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1992 /* do the same for the right operand */
1995 IS_ITEMP (IC_RIGHT (ic)) &&
1996 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1998 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2004 /* if this is a subtraction & the result
2005 is a true symbol in far space then don't pack */
2006 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2008 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2009 if (IN_FARSPACE (SPEC_OCLS (etype)))
2012 /* found it we need to remove it from the
2014 for (sic = dic; sic != ic; sic = sic->next)
2015 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2017 IC_RIGHT (ic)->operand.symOperand =
2018 IC_RIGHT (dic)->operand.symOperand;
2019 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2021 remiCodeFromeBBlock (ebp, dic);
2022 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2023 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2030 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2033 /*-----------------------------------------------------------------*/
2034 /* packRegsForOneuse : - will reduce some registers for single Use */
2035 /*-----------------------------------------------------------------*/
2037 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2042 /* if returning a literal then do nothing */
2046 /* only upto 2 bytes since we cannot predict
2047 the usage of b, & acc */
2048 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2051 if (ic->op != RETURN &&
2053 !POINTER_SET (ic) &&
2057 /* this routine will mark the a symbol as used in one
2058 instruction use only && if the defintion is local
2059 (ie. within the basic block) && has only one definition &&
2060 that definiion is either a return value from a
2061 function or does not contain any variables in
2063 uses = bitVectCopy (OP_USES (op));
2064 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2065 if (!bitVectIsZero (uses)) /* has other uses */
2068 /* if it has only one defintion */
2069 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2070 return NULL; /* has more than one definition */
2072 /* get that definition */
2074 hTabItemWithKey (iCodehTab,
2075 bitVectFirstBit (OP_DEFS (op)))))
2078 /* if that only usage is a cast */
2079 if (dic->op == CAST) {
2080 /* to a bigger type */
2081 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2082 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2083 /* than we can not, since we cannot predict the usage of b & acc */
2088 /* found the definition now check if it is local */
2089 if (dic->seq < ebp->fSeq ||
2090 dic->seq > ebp->lSeq)
2091 return NULL; /* non-local */
2093 /* now check if it is the return from
2095 if (dic->op == CALL || dic->op == PCALL)
2097 if (ic->op != SEND && ic->op != RETURN)
2099 OP_SYMBOL (op)->ruonly = 1;
2106 /* otherwise check that the definition does
2107 not contain any symbols in far space */
2108 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2109 isOperandInFarSpace (IC_RIGHT (dic)) ||
2110 IS_OP_RUONLY (IC_LEFT (ic)) ||
2111 IS_OP_RUONLY (IC_RIGHT (ic)))
2116 /* if pointer set then make sure the pointer
2118 if (POINTER_SET (dic) &&
2119 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2122 if (POINTER_GET (dic) &&
2123 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2128 /* also make sure the intervenening instructions
2129 don't have any thing in far space */
2130 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2133 /* if there is an intervening function call then no */
2134 if (dic->op == CALL || dic->op == PCALL)
2136 /* if pointer set then make sure the pointer
2138 if (POINTER_SET (dic) &&
2139 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2142 if (POINTER_GET (dic) &&
2143 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2146 /* if address of & the result is remat the okay */
2147 if (dic->op == ADDRESS_OF &&
2148 OP_SYMBOL (IC_RESULT (dic))->remat)
2151 /* if operand has size of three or more & this
2152 operation is a '*','/' or '%' then 'b' may
2154 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2155 getSize (operandType (op)) >= 3)
2158 /* if left or right or result is in far space */
2159 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2160 isOperandInFarSpace (IC_RIGHT (dic)) ||
2161 isOperandInFarSpace (IC_RESULT (dic)) ||
2162 IS_OP_RUONLY (IC_LEFT (dic)) ||
2163 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2164 IS_OP_RUONLY (IC_RESULT (dic)))
2168 /* if left or right or result is on stack */
2169 if (isOperandOnStack(IC_LEFT(dic)) ||
2170 isOperandOnStack(IC_RIGHT(dic)) ||
2171 isOperandOnStack(IC_RESULT(dic))) {
2176 OP_SYMBOL (op)->ruonly = 1;
2181 /*-----------------------------------------------------------------*/
2182 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2183 /*-----------------------------------------------------------------*/
2185 isBitwiseOptimizable (iCode * ic)
2187 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2188 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2190 /* bitwise operations are considered optimizable
2191 under the following conditions (Jean-Louis VERN)
2203 if (IS_LITERAL(rtype) ||
2204 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2210 /*-----------------------------------------------------------------*/
2211 /* packRegsForAccUse - pack registers for acc use */
2212 /*-----------------------------------------------------------------*/
2214 packRegsForAccUse (iCode * ic)
2218 /* if this is an aggregate, e.g. a one byte char array */
2219 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2223 /* if + or - then it has to be one byte result */
2224 if ((ic->op == '+' || ic->op == '-')
2225 && getSize (operandType (IC_RESULT (ic))) > 1)
2228 /* if shift operation make sure right side is not a literal */
2229 if (ic->op == RIGHT_OP &&
2230 (isOperandLiteral (IC_RIGHT (ic)) ||
2231 getSize (operandType (IC_RESULT (ic))) > 1))
2234 if (ic->op == LEFT_OP &&
2235 (isOperandLiteral (IC_RIGHT (ic)) ||
2236 getSize (operandType (IC_RESULT (ic))) > 1))
2239 if (IS_BITWISE_OP (ic) &&
2240 getSize (operandType (IC_RESULT (ic))) > 1)
2244 /* has only one definition */
2245 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2248 /* has only one use */
2249 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2252 /* and the usage immediately follows this iCode */
2253 if (!(uic = hTabItemWithKey (iCodehTab,
2254 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2257 if (ic->next != uic)
2260 /* if it is a conditional branch then we definitely can */
2264 if (uic->op == JUMPTABLE)
2267 /* if the usage is not is an assignment
2268 or an arithmetic / bitwise / shift operation then not */
2269 if (POINTER_SET (uic) &&
2270 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2273 if (uic->op != '=' &&
2274 !IS_ARITHMETIC_OP (uic) &&
2275 !IS_BITWISE_OP (uic) &&
2276 uic->op != LEFT_OP &&
2277 uic->op != RIGHT_OP)
2280 /* if used in ^ operation then make sure right is not a
2282 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2285 /* if shift operation make sure right side is not a literal */
2286 if (uic->op == RIGHT_OP &&
2287 (isOperandLiteral (IC_RIGHT (uic)) ||
2288 getSize (operandType (IC_RESULT (uic))) > 1))
2291 if (uic->op == LEFT_OP &&
2292 (isOperandLiteral (IC_RIGHT (uic)) ||
2293 getSize (operandType (IC_RESULT (uic))) > 1))
2296 /* make sure that the result of this icode is not on the
2297 stack, since acc is used to compute stack offset */
2299 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2300 OP_SYMBOL (IC_RESULT (uic))->onStack)
2303 if (isOperandOnStack(IC_RESULT(uic)))
2307 /* if either one of them in far space then we cannot */
2308 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2309 isOperandInFarSpace (IC_LEFT (uic))) ||
2310 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2311 isOperandInFarSpace (IC_RIGHT (uic))))
2314 /* if the usage has only one operand then we can */
2315 if (IC_LEFT (uic) == NULL ||
2316 IC_RIGHT (uic) == NULL)
2319 /* make sure this is on the left side if not
2320 a '+' since '+' is commutative */
2321 if (ic->op != '+' &&
2322 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2326 // this is too dangerous and need further restrictions
2329 /* if one of them is a literal then we can */
2330 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2331 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2333 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2338 /* if the other one is not on stack then we can */
2339 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2340 (IS_ITEMP (IC_RIGHT (uic)) ||
2341 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2342 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2345 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2346 (IS_ITEMP (IC_LEFT (uic)) ||
2347 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2348 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2354 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2359 /*-----------------------------------------------------------------*/
2360 /* packForPush - hueristics to reduce iCode for pushing */
2361 /*-----------------------------------------------------------------*/
2363 packForPush (iCode * ic, eBBlock * ebp)
2368 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2371 /* must have only definition & one usage */
2372 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2373 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2376 /* find the definition */
2377 if (!(dic = hTabItemWithKey (iCodehTab,
2378 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2381 if (dic->op != '=' || POINTER_SET (dic))
2384 /* make sure the right side does not have any definitions
2386 dbv = OP_DEFS(IC_RIGHT(dic));
2387 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2388 if (bitVectBitValue(dbv,lic->key))
2391 /* make sure they have the same type */
2393 sym_link *itype=operandType(IC_LEFT(ic));
2394 sym_link *ditype=operandType(IC_RIGHT(dic));
2396 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2397 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2400 /* extend the live range of replaced operand if needed */
2401 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2402 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2404 /* we now we know that it has one & only one def & use
2405 and the that the definition is an assignment */
2406 IC_LEFT (ic) = IC_RIGHT (dic);
2408 remiCodeFromeBBlock (ebp, dic);
2409 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2410 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2413 /*-----------------------------------------------------------------*/
2414 /* packRegisters - does some transformations to reduce register */
2416 /*-----------------------------------------------------------------*/
2418 packRegisters (eBBlock * ebp)
2428 /* look for assignments of the form */
2429 /* iTempNN = TRueSym (someoperation) SomeOperand */
2431 /* TrueSym := iTempNN:1 */
2432 for (ic = ebp->sch; ic; ic = ic->next)
2434 /* find assignment of the form TrueSym := iTempNN:1 */
2435 if (ic->op == '=' && !POINTER_SET (ic))
2436 change += packRegsForAssign (ic, ebp);
2443 for (ic = ebp->sch; ic; ic = ic->next)
2445 /* if this is an itemp & result of an address of a true sym
2446 then mark this as rematerialisable */
2447 if (ic->op == ADDRESS_OF &&
2448 IS_ITEMP (IC_RESULT (ic)) &&
2449 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2450 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2451 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2454 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2455 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2456 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2460 /* if straight assignment then carry remat flag if
2461 this is the only definition */
2462 if (ic->op == '=' &&
2463 !POINTER_SET (ic) &&
2464 IS_SYMOP (IC_RIGHT (ic)) &&
2465 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2466 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2467 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2470 OP_SYMBOL (IC_RESULT (ic))->remat =
2471 OP_SYMBOL (IC_RIGHT (ic))->remat;
2472 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2473 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2476 /* if cast to a generic pointer & the pointer being
2477 cast is remat, then we can remat this cast as well */
2478 if (ic->op == CAST &&
2479 IS_SYMOP(IC_RIGHT(ic)) &&
2480 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2481 sym_link *to_type = operandType(IC_LEFT(ic));
2482 sym_link *from_type = operandType(IC_RIGHT(ic));
2483 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2484 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2485 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2486 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2490 /* if this is a +/- operation with a rematerizable
2491 then mark this as rematerializable as well */
2492 if ((ic->op == '+' || ic->op == '-') &&
2493 (IS_SYMOP (IC_LEFT (ic)) &&
2494 IS_ITEMP (IC_RESULT (ic)) &&
2495 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2496 OP_SYMBOL (IC_LEFT (ic))->remat &&
2497 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2498 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2500 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2501 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2502 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2505 /* mark the pointer usages */
2506 if (POINTER_SET (ic))
2507 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2509 if (POINTER_GET (ic))
2510 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2514 /* if we are using a symbol on the stack
2515 then we should say mcs51_ptrRegReq */
2516 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2517 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2518 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2519 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2520 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2521 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2524 if (IS_SYMOP (IC_LEFT (ic)))
2525 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2526 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2527 if (IS_SYMOP (IC_RIGHT (ic)))
2528 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2529 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2530 if (IS_SYMOP (IC_RESULT (ic)))
2531 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2532 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2536 /* if the condition of an if instruction
2537 is defined in the previous instruction and
2538 this is the only usage then
2539 mark the itemp as a conditional */
2540 if ((IS_CONDITIONAL (ic) ||
2541 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2542 ic->next && ic->next->op == IFX &&
2543 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2544 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2545 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2547 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2551 /* reduce for support function calls */
2552 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2553 packRegsForSupport (ic, ebp);
2555 /* some cases the redundant moves can
2556 can be eliminated for return statements */
2557 if ((ic->op == RETURN || ic->op == SEND) &&
2558 !isOperandInFarSpace (IC_LEFT (ic)) &&
2559 options.model == MODEL_SMALL) {
2560 if (0 && options.stackAuto) {
2561 /* we should check here if acc will be clobbered for stack
2562 offset calculations */
2564 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2568 /* if pointer set & left has a size more than
2569 one and right is not in far space */
2570 if (POINTER_SET (ic) &&
2571 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2572 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2573 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2574 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2576 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2578 /* if pointer get */
2579 if (POINTER_GET (ic) &&
2580 !isOperandInFarSpace (IC_RESULT (ic)) &&
2581 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2582 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2583 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2585 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2588 /* if this is cast for intergral promotion then
2589 check if only use of the definition of the
2590 operand being casted/ if yes then replace
2591 the result of that arithmetic operation with
2592 this result and get rid of the cast */
2595 sym_link *fromType = operandType (IC_RIGHT (ic));
2596 sym_link *toType = operandType (IC_LEFT (ic));
2598 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2599 getSize (fromType) != getSize (toType) &&
2600 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2603 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2606 if (IS_ARITHMETIC_OP (dic))
2608 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2609 IC_RESULT (dic) = IC_RESULT (ic);
2610 remiCodeFromeBBlock (ebp, ic);
2611 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2612 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2613 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2617 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2623 /* if the type from and type to are the same
2624 then if this is the only use then packit */
2625 if (compareType (operandType (IC_RIGHT (ic)),
2626 operandType (IC_LEFT (ic))) == 1)
2628 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2631 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2632 IC_RESULT (dic) = IC_RESULT (ic);
2633 remiCodeFromeBBlock (ebp, ic);
2634 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2635 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2636 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2644 iTempNN := (some variable in farspace) V1
2649 if (ic->op == IPUSH)
2651 packForPush (ic, ebp);
2655 /* pack registers for accumulator use, when the
2656 result of an arithmetic or bit wise operation
2657 has only one use, that use is immediately following
2658 the defintion and the using iCode has only one
2659 operand or has two operands but one is literal &
2660 the result of that operation is not on stack then
2661 we can leave the result of this operation in acc:b
2663 if ((IS_ARITHMETIC_OP (ic)
2664 || IS_CONDITIONAL(ic)
2665 || IS_BITWISE_OP (ic)
2666 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2667 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2669 IS_ITEMP (IC_RESULT (ic)) &&
2670 getSize (operandType (IC_RESULT (ic))) <= 2)
2672 packRegsForAccUse (ic);
2676 /*-----------------------------------------------------------------*/
2677 /* assignRegisters - assigns registers to each live range as need */
2678 /*-----------------------------------------------------------------*/
2680 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2685 setToNull ((void *) &_G.funcrUsed);
2686 setToNull ((void *) &_G.totRegAssigned);
2687 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2690 /* change assignments this will remove some
2691 live ranges reducing some register pressure */
2692 for (i = 0; i < count; i++)
2693 packRegisters (ebbs[i]);
2695 if (options.dump_pack)
2696 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2698 /* first determine for each live range the number of
2699 registers & the type of registers required for each */
2702 /* and serially allocate registers */
2703 serialRegAssign (ebbs, count);
2708 /* if stack was extended then tell the user */
2711 /* werror(W_TOOMANY_SPILS,"stack", */
2712 /* _G.stackExtend,currFunc->name,""); */
2718 /* werror(W_TOOMANY_SPILS,"data space", */
2719 /* _G.dataExtend,currFunc->name,""); */
2723 /* after that create the register mask
2724 for each of the instruction */
2725 createRegMask (ebbs, count);
2727 /* redo that offsets for stacked automatic variables */
2728 redoStackOffsets ();
2730 if (options.dump_rassgn)
2732 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2733 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2736 /* do the overlaysegment stuff SDCCmem.c */
2737 doOverlays (ebbs, count);
2739 /* now get back the chain */
2740 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2744 /* free up any _G.stackSpil locations allocated */
2745 applyToSet (_G.stackSpil, deallocStackSpil);
2747 setToNull ((void **) &_G.stackSpil);
2748 setToNull ((void **) &_G.spiltSet);
2749 /* mark all registers as free */