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 /* computeSpillable - given a point find the spillable live ranges */
196 /*-----------------------------------------------------------------*/
198 computeSpillable (iCode * ic)
202 /* spillable live ranges are those that are live at this
203 point . the following categories need to be subtracted
205 a) - those that are already spilt
206 b) - if being used by this one
207 c) - defined by this one */
209 spillable = bitVectCopy (ic->rlive);
211 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
213 bitVectCplAnd (spillable, ic->uses); /* used in this one */
214 bitVectUnSetBit (spillable, ic->defKey);
215 spillable = bitVectIntersect (spillable, _G.regAssigned);
220 /*-----------------------------------------------------------------*/
221 /* noSpilLoc - return true if a variable has no spil location */
222 /*-----------------------------------------------------------------*/
224 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
226 return (sym->usl.spillLoc ? 0 : 1);
229 /*-----------------------------------------------------------------*/
230 /* hasSpilLoc - will return 1 if the symbol has spil location */
231 /*-----------------------------------------------------------------*/
233 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
235 return (sym->usl.spillLoc ? 1 : 0);
238 /*-----------------------------------------------------------------*/
239 /* directSpilLoc - will return 1 if the splilocation is in direct */
240 /*-----------------------------------------------------------------*/
242 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
244 if (sym->usl.spillLoc &&
245 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
251 /*-----------------------------------------------------------------*/
252 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
253 /* but is not used as a pointer */
254 /*-----------------------------------------------------------------*/
256 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
258 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
261 /*-----------------------------------------------------------------*/
262 /* rematable - will return 1 if the remat flag is set */
263 /*-----------------------------------------------------------------*/
265 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
270 /*-----------------------------------------------------------------*/
271 /* notUsedInRemaining - not used or defined in remain of the block */
272 /*-----------------------------------------------------------------*/
274 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
276 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
277 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
280 /*-----------------------------------------------------------------*/
281 /* allLRs - return true for all */
282 /*-----------------------------------------------------------------*/
284 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
289 /*-----------------------------------------------------------------*/
290 /* liveRangesWith - applies function to a given set of live range */
291 /*-----------------------------------------------------------------*/
293 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
294 eBBlock * ebp, iCode * ic)
299 if (!lrs || !lrs->size)
302 for (i = 1; i < lrs->size; i++)
305 if (!bitVectBitValue (lrs, i))
308 /* if we don't find it in the live range
309 hash table we are in serious trouble */
310 if (!(sym = hTabItemWithKey (liveRanges, i)))
312 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
313 "liveRangesWith could not find liveRange");
317 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
318 addSetHead (&rset, sym);
325 /*-----------------------------------------------------------------*/
326 /* leastUsedLR - given a set determines which is the least used */
327 /*-----------------------------------------------------------------*/
329 leastUsedLR (set * sset)
331 symbol *sym = NULL, *lsym = NULL;
333 sym = lsym = setFirstItem (sset);
338 for (; lsym; lsym = setNextItem (sset))
341 /* if usage is the same then prefer
342 the spill the smaller of the two */
343 if (lsym->used == sym->used)
344 if (getSize (lsym->type) < getSize (sym->type))
348 if (lsym->used < sym->used)
353 setToNull ((void **) &sset);
358 /*-----------------------------------------------------------------*/
359 /* noOverLap - will iterate through the list looking for over lap */
360 /*-----------------------------------------------------------------*/
362 noOverLap (set * itmpStack, symbol * fsym)
367 for (sym = setFirstItem (itmpStack); sym;
368 sym = setNextItem (itmpStack))
370 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
376 /*-----------------------------------------------------------------*/
377 /* isFree - will return 1 if the a free spil location is found */
378 /*-----------------------------------------------------------------*/
383 V_ARG (symbol **, sloc);
384 V_ARG (symbol *, fsym);
386 /* if already found */
390 /* if it is free && and the itmp assigned to
391 this does not have any overlapping live ranges
392 with the one currently being assigned and
393 the size can be accomodated */
395 noOverLap (sym->usl.itmpStack, fsym) &&
396 getSize (sym->type) >= getSize (fsym->type))
405 /*-----------------------------------------------------------------*/
406 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
407 /*-----------------------------------------------------------------*/
409 spillLRWithPtrReg (symbol * forSym)
415 if (!_G.regAssigned ||
416 bitVectIsZero (_G.regAssigned))
419 r0 = mcs51_regWithIdx (R0_IDX);
420 r1 = mcs51_regWithIdx (R1_IDX);
422 /* for all live ranges */
423 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
424 lrsym = hTabNextItem (liveRanges, &k))
428 /* if no registers assigned to it or spilt */
429 /* if it does not overlap with this then
430 not need to spill it */
432 if (lrsym->isspilt || !lrsym->nRegs ||
433 (lrsym->liveTo < forSym->liveFrom))
436 /* go thru the registers : if it is either
437 r0 or r1 then spil it */
438 for (j = 0; j < lrsym->nRegs; j++)
439 if (lrsym->regs[j] == r0 ||
440 lrsym->regs[j] == r1)
449 /*-----------------------------------------------------------------*/
450 /* createStackSpil - create a location on the stack to spil */
451 /*-----------------------------------------------------------------*/
453 createStackSpil (symbol * sym)
456 int useXstack, model;
460 /* first go try and find a free one that is already
461 existing on the stack */
462 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
464 /* found a free one : just update & return */
465 sym->usl.spillLoc = sloc;
468 addSetHead (&sloc->usl.itmpStack, sym);
472 /* could not then have to create one , this is the hard part
473 we need to allocate this on the stack : this is really a
474 hack!! but cannot think of anything better at this time */
476 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
478 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
483 sloc = newiTemp (slocBuffer);
485 /* set the type to the spilling symbol */
486 sloc->type = copyLinkChain (sym->type);
487 sloc->etype = getSpec (sloc->type);
488 SPEC_SCLS (sloc->etype) = S_DATA;
489 SPEC_EXTR (sloc->etype) = 0;
490 SPEC_STAT (sloc->etype) = 0;
491 SPEC_VOLATILE(sloc->etype) = 0;
492 SPEC_ABSA(sloc->etype) = 0;
494 /* we don't allow it to be allocated`
495 onto the external stack since : so we
496 temporarily turn it off ; we also
497 turn off memory model to prevent
498 the spil from going to the external storage
501 useXstack = options.useXstack;
502 model = options.model;
503 /* noOverlay = options.noOverlay; */
504 /* options.noOverlay = 1; */
505 options.model = options.useXstack = 0;
509 options.useXstack = useXstack;
510 options.model = model;
511 /* options.noOverlay = noOverlay; */
512 sloc->isref = 1; /* to prevent compiler warning */
514 /* if it is on the stack then update the stack */
515 if (IN_STACK (sloc->etype))
517 currFunc->stack += getSize (sloc->type);
518 _G.stackExtend += getSize (sloc->type);
521 _G.dataExtend += getSize (sloc->type);
523 /* add it to the _G.stackSpil set */
524 addSetHead (&_G.stackSpil, sloc);
525 sym->usl.spillLoc = sloc;
528 /* add it to the set of itempStack set
529 of the spill location */
530 addSetHead (&sloc->usl.itmpStack, sym);
534 /*-----------------------------------------------------------------*/
535 /* isSpiltOnStack - returns true if the spil location is on stack */
536 /*-----------------------------------------------------------------*/
538 isSpiltOnStack (symbol * sym)
548 /* if (sym->_G.stackSpil) */
551 if (!sym->usl.spillLoc)
554 etype = getSpec (sym->usl.spillLoc->type);
555 if (IN_STACK (etype))
561 /*-----------------------------------------------------------------*/
562 /* spillThis - spils a specific operand */
563 /*-----------------------------------------------------------------*/
565 spillThis (symbol * sym)
568 /* if this is rematerializable or has a spillLocation
569 we are okay, else we need to create a spillLocation
571 if (!(sym->remat || sym->usl.spillLoc))
572 createStackSpil (sym);
574 /* mark it has spilt & put it in the spilt set */
575 sym->isspilt = sym->spillA = 1;
576 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
578 bitVectUnSetBit (_G.regAssigned, sym->key);
579 bitVectUnSetBit (_G.totRegAssigned, sym->key);
581 for (i = 0; i < sym->nRegs; i++)
585 freeReg (sym->regs[i]);
589 /* if spilt on stack then free up r0 & r1
590 if they could have been assigned to some
592 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
595 spillLRWithPtrReg (sym);
598 if (sym->usl.spillLoc && !sym->remat)
599 sym->usl.spillLoc->allocreq++;
603 /*-----------------------------------------------------------------*/
604 /* selectSpil - select a iTemp to spil : rather a simple procedure */
605 /*-----------------------------------------------------------------*/
607 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
609 bitVect *lrcs = NULL;
613 /* get the spillable live ranges */
614 lrcs = computeSpillable (ic);
616 /* get all live ranges that are rematerizable */
617 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
620 /* return the least used of these */
621 return leastUsedLR (selectS);
624 /* get live ranges with spillLocations in direct space */
625 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
627 sym = leastUsedLR (selectS);
628 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
629 sym->usl.spillLoc->rname :
630 sym->usl.spillLoc->name));
632 /* mark it as allocation required */
633 sym->usl.spillLoc->allocreq++;
637 /* if the symbol is local to the block then */
638 if (forSym->liveTo < ebp->lSeq)
641 /* check if there are any live ranges allocated
642 to registers that are not used in this block */
643 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
645 sym = leastUsedLR (selectS);
646 /* if this is not rematerializable */
655 /* check if there are any live ranges that not
656 used in the remainder of the block */
657 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
659 sym = leastUsedLR (selectS);
672 /* find live ranges with spillocation && not used as pointers */
673 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
676 sym = leastUsedLR (selectS);
677 /* mark this as allocation required */
678 sym->usl.spillLoc->allocreq++;
682 /* find live ranges with spillocation */
683 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
686 sym = leastUsedLR (selectS);
687 sym->usl.spillLoc->allocreq++;
691 /* couldn't find then we need to create a spil
692 location on the stack , for which one? the least
694 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
697 /* return a created spil location */
698 sym = createStackSpil (leastUsedLR (selectS));
699 sym->usl.spillLoc->allocreq++;
703 /* this is an extreme situation we will spill
704 this one : happens very rarely but it does happen */
710 /*-----------------------------------------------------------------*/
711 /* spilSomething - spil some variable & mark registers as free */
712 /*-----------------------------------------------------------------*/
714 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
719 /* get something we can spil */
720 ssym = selectSpil (ic, ebp, forSym);
722 /* mark it as spilt */
723 ssym->isspilt = ssym->spillA = 1;
724 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
726 /* mark it as not register assigned &
727 take it away from the set */
728 bitVectUnSetBit (_G.regAssigned, ssym->key);
729 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
731 /* mark the registers as free */
732 for (i = 0; i < ssym->nRegs; i++)
734 freeReg (ssym->regs[i]);
736 /* if spilt on stack then free up r0 & r1
737 if they could have been assigned to as gprs */
738 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
741 spillLRWithPtrReg (ssym);
744 /* if this was a block level spil then insert push & pop
745 at the start & end of block respectively */
748 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
749 /* add push to the start of the block */
750 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
751 ebp->sch->next : ebp->sch));
752 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
753 /* add pop to the end of the block */
754 addiCodeToeBBlock (ebp, nic, NULL);
757 /* if spilt because not used in the remainder of the
758 block then add a push before this instruction and
759 a pop at the end of the block */
760 if (ssym->remainSpil)
763 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
764 /* add push just before this instruction */
765 addiCodeToeBBlock (ebp, nic, ic);
767 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
768 /* add pop to the end of the block */
769 addiCodeToeBBlock (ebp, nic, NULL);
778 /*-----------------------------------------------------------------*/
779 /* getRegPtr - will try for PTR if not a GPR type if not spil */
780 /*-----------------------------------------------------------------*/
782 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
787 /* try for a ptr type */
788 if ((reg = allocReg (REG_PTR)))
791 /* try for gpr type */
792 if ((reg = allocReg (REG_GPR)))
795 /* we have to spil */
796 if (!spilSomething (ic, ebp, sym))
799 /* this looks like an infinite loop but
800 in really selectSpil will abort */
804 /*-----------------------------------------------------------------*/
805 /* getRegGpr - will try for GPR if not spil */
806 /*-----------------------------------------------------------------*/
808 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
813 /* try for gpr type */
814 if ((reg = allocReg (REG_GPR)))
817 if (!mcs51_ptrRegReq)
818 if ((reg = allocReg (REG_PTR)))
821 /* we have to spil */
822 if (!spilSomething (ic, ebp, sym))
825 /* this looks like an infinite loop but
826 in really selectSpil will abort */
830 /*-----------------------------------------------------------------*/
831 /* getRegPtrNoSpil - get it cannot split */
832 /*-----------------------------------------------------------------*/
833 static regs *getRegPtrNoSpil()
837 /* try for a ptr type */
838 if ((reg = allocReg (REG_PTR)))
841 /* try for gpr type */
842 if ((reg = allocReg (REG_GPR)))
847 /* just to make the compiler happy */
851 /*-----------------------------------------------------------------*/
852 /* getRegGprNoSpil - get it cannot split */
853 /*-----------------------------------------------------------------*/
854 static regs *getRegGprNoSpil()
858 if ((reg = allocReg (REG_GPR)))
861 if (!mcs51_ptrRegReq)
862 if ((reg = allocReg (REG_PTR)))
867 /* just to make the compiler happy */
871 /*-----------------------------------------------------------------*/
872 /* symHasReg - symbol has a given register */
873 /*-----------------------------------------------------------------*/
875 symHasReg (symbol * sym, regs * reg)
879 for (i = 0; i < sym->nRegs; i++)
880 if (sym->regs[i] == reg)
886 /*-----------------------------------------------------------------*/
887 /* deassignLRs - check the live to and if they have registers & are */
888 /* not spilt then free up the registers */
889 /*-----------------------------------------------------------------*/
891 deassignLRs (iCode * ic, eBBlock * ebp)
897 for (sym = hTabFirstItem (liveRanges, &k); sym;
898 sym = hTabNextItem (liveRanges, &k))
902 /* if it does not end here */
903 if (sym->liveTo > ic->seq)
906 /* if it was spilt on stack then we can
907 mark the stack spil location as free */
912 sym->usl.spillLoc->isFree = 1;
918 if (!bitVectBitValue (_G.regAssigned, sym->key))
921 /* special case check if this is an IFX &
922 the privious one was a pop and the
923 previous one was not spilt then keep track
925 if (ic->op == IFX && ic->prev &&
926 ic->prev->op == IPOP &&
927 !ic->prev->parmPush &&
928 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
929 psym = OP_SYMBOL (IC_LEFT (ic->prev));
935 bitVectUnSetBit (_G.regAssigned, sym->key);
937 /* if the result of this one needs registers
938 and does not have it then assign it right
940 if (IC_RESULT (ic) &&
941 !(SKIP_IC2 (ic) || /* not a special icode */
942 ic->op == JUMPTABLE ||
948 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
949 result->liveTo > ic->seq && /* and will live beyond this */
950 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
951 result->regType == sym->regType && /* same register types */
952 result->nRegs && /* which needs registers */
953 !result->isspilt && /* and does not already have them */
955 !bitVectBitValue (_G.regAssigned, result->key) &&
956 /* the number of free regs + number of regs in this LR
957 can accomodate the what result Needs */
958 ((nfreeRegsType (result->regType) +
959 sym->nRegs) >= result->nRegs)
963 for (i = 0; i < result->nRegs; i++)
965 result->regs[i] = sym->regs[i];
967 result->regs[i] = getRegGpr (ic, ebp, result);
969 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
970 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
974 /* free the remaining */
975 for (; i < sym->nRegs; i++)
979 if (!symHasReg (psym, sym->regs[i]))
980 freeReg (sym->regs[i]);
983 freeReg (sym->regs[i]);
990 /*-----------------------------------------------------------------*/
991 /* reassignLR - reassign this to registers */
992 /*-----------------------------------------------------------------*/
994 reassignLR (operand * op)
996 symbol *sym = OP_SYMBOL (op);
999 /* not spilt any more */
1000 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1001 bitVectUnSetBit (_G.spiltSet, sym->key);
1003 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1004 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1008 for (i = 0; i < sym->nRegs; i++)
1009 sym->regs[i]->isFree = 0;
1012 /*-----------------------------------------------------------------*/
1013 /* willCauseSpill - determines if allocating will cause a spill */
1014 /*-----------------------------------------------------------------*/
1016 willCauseSpill (int nr, int rt)
1018 /* first check if there are any avlb registers
1019 of te type required */
1022 /* special case for pointer type
1023 if pointer type not avlb then
1024 check for type gpr */
1025 if (nFreeRegs (rt) >= nr)
1027 if (nFreeRegs (REG_GPR) >= nr)
1032 if (mcs51_ptrRegReq)
1034 if (nFreeRegs (rt) >= nr)
1039 if (nFreeRegs (REG_PTR) +
1040 nFreeRegs (REG_GPR) >= nr)
1045 /* it will cause a spil */
1049 /*-----------------------------------------------------------------*/
1050 /* positionRegs - the allocator can allocate same registers to res- */
1051 /* ult and operand, if this happens make sure they are in the same */
1052 /* position as the operand otherwise chaos results */
1053 /*-----------------------------------------------------------------*/
1055 positionRegs (symbol * result, symbol * opsym)
1057 int count = min (result->nRegs, opsym->nRegs);
1058 int i, j = 0, shared = 0;
1061 /* if the result has been spilt then cannot share */
1066 /* first make sure that they actually share */
1067 for (i = 0; i < count; i++)
1069 for (j = 0; j < count; j++)
1071 if (result->regs[i] == opsym->regs[j] && i != j)
1081 regs *tmp = result->regs[i];
1082 result->regs[i] = result->regs[j];
1083 result->regs[j] = tmp;
1090 /*-----------------------------------------------------------------*/
1091 /* serialRegAssign - serially allocate registers to the variables */
1092 /*-----------------------------------------------------------------*/
1094 serialRegAssign (eBBlock ** ebbs, int count)
1098 /* for all blocks */
1099 for (i = 0; i < count; i++) {
1103 if (ebbs[i]->noPath &&
1104 (ebbs[i]->entryLabel != entryLabel &&
1105 ebbs[i]->entryLabel != returnLabel))
1108 /* of all instructions do */
1109 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1113 // update the registers in use at the start of this icode
1114 for (reg=0; reg<mcs51_nRegs; reg++) {
1115 if (regs8051[reg].isFree) {
1116 ic->riu &= ~(1<<regs8051[reg].offset);
1118 ic->riu |= (1<<regs8051[reg].offset);
1123 /* if this is an ipop that means some live
1124 range will have to be assigned again */
1126 reassignLR (IC_LEFT (ic));
1128 /* if result is present && is a true symbol */
1129 if (IC_RESULT (ic) && ic->op != IFX &&
1130 IS_TRUE_SYMOP (IC_RESULT (ic)))
1131 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1133 /* take away registers from live
1134 ranges that end at this instruction */
1135 deassignLRs (ic, ebbs[i]);
1137 /* some don't need registers */
1138 if (SKIP_IC2 (ic) ||
1139 ic->op == JUMPTABLE ||
1143 (IC_RESULT (ic) && POINTER_SET (ic)))
1146 /* now we need to allocate registers
1147 only for the result */
1148 if (IC_RESULT (ic)) {
1149 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1155 /* if it does not need or is spilt
1156 or is already assigned to registers
1157 or will not live beyond this instructions */
1160 bitVectBitValue (_G.regAssigned, sym->key) ||
1161 sym->liveTo <= ic->seq)
1164 /* if some liverange has been spilt at the block level
1165 and this one live beyond this block then spil this
1167 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1171 /* if trying to allocate this will cause
1172 a spill and there is nothing to spill
1173 or this one is rematerializable then
1175 willCS = willCauseSpill (sym->nRegs, sym->regType);
1176 spillable = computeSpillable (ic);
1177 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1182 /* if it has a spillocation & is used less than
1183 all other live ranges then spill this */
1185 if (sym->usl.spillLoc) {
1186 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1187 allLRs, ebbs[i], ic));
1188 if (leastUsed && leastUsed->used > sym->used) {
1193 /* if none of the liveRanges have a spillLocation then better
1194 to spill this one than anything else already assigned to registers */
1195 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1196 /* if this is local to this block then we might find a block spil */
1197 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1204 /* if we need ptr regs for the right side
1206 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1207 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1211 /* else we assign registers to it */
1212 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1213 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1215 for (j = 0; j < sym->nRegs; j++) {
1216 if (sym->regType == REG_PTR)
1217 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1219 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1221 /* if the allocation failed which means
1222 this was spilt then break */
1223 if (!sym->regs[j]) {
1228 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1229 /* if it shares registers with operands make sure
1230 that they are in the same position */
1231 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1232 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1233 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1234 OP_SYMBOL (IC_LEFT (ic)));
1236 /* do the same for the right operand */
1237 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1238 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1239 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1240 OP_SYMBOL (IC_RIGHT (ic)));
1254 /*-----------------------------------------------------------------*/
1255 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1256 /*-----------------------------------------------------------------*/
1257 static void fillGaps()
1262 if (getenv("DISABLE_FILL_GAPS")) return;
1264 /* look for livernages that was spilt by the allocator */
1265 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1266 sym = hTabNextItem(liveRanges,&key)) {
1271 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1273 /* find the liveRanges this one clashes with, that are
1274 still assigned to registers & mark the registers as used*/
1275 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1279 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1280 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1283 clr = hTabItemWithKey(liveRanges,i);
1286 /* mark these registers as used */
1287 for (k = 0 ; k < clr->nRegs ; k++ )
1288 useReg(clr->regs[k]);
1291 if (willCauseSpill(sym->nRegs,sym->regType)) {
1292 /* NOPE :( clear all registers & and continue */
1297 /* THERE IS HOPE !!!! */
1298 for (i=0; i < sym->nRegs ; i++ ) {
1299 if (sym->regType == REG_PTR)
1300 sym->regs[i] = getRegPtrNoSpil ();
1302 sym->regs[i] = getRegGprNoSpil ();
1305 /* for all its definitions check if the registers
1306 allocated needs positioning NOTE: we can position
1307 only ONCE if more than One positioning required
1310 for (i = 0 ; i < sym->defs->size ; i++ ) {
1311 if (bitVectBitValue(sym->defs,i)) {
1313 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1314 if (SKIP_IC(ic)) continue;
1315 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1316 /* if left is assigned to registers */
1317 if (IS_SYMOP(IC_LEFT(ic)) &&
1318 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1319 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1321 if (IS_SYMOP(IC_RIGHT(ic)) &&
1322 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1323 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1325 if (pdone > 1) break;
1328 for (i = 0 ; i < sym->uses->size ; i++ ) {
1329 if (bitVectBitValue(sym->uses,i)) {
1331 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1332 if (SKIP_IC(ic)) continue;
1333 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1335 /* if result is assigned to registers */
1336 if (IS_SYMOP(IC_RESULT(ic)) &&
1337 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1338 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1340 if (pdone > 1) break;
1343 /* had to position more than once GIVE UP */
1345 /* UNDO all the changes we made to try this */
1347 for (i=0; i < sym->nRegs ; i++ ) {
1348 sym->regs[i] = NULL;
1351 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1354 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1355 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1356 sym->isspilt = sym->spillA = 0 ;
1357 sym->usl.spillLoc->allocreq--;
1362 /*-----------------------------------------------------------------*/
1363 /* rUmaskForOp :- returns register mask for an operand */
1364 /*-----------------------------------------------------------------*/
1366 mcs51_rUmaskForOp (operand * op)
1372 /* only temporaries are assigned registers */
1376 sym = OP_SYMBOL (op);
1378 /* if spilt or no registers assigned to it
1380 if (sym->isspilt || !sym->nRegs)
1383 rumask = newBitVect (mcs51_nRegs);
1385 for (j = 0; j < sym->nRegs; j++)
1387 rumask = bitVectSetBit (rumask,
1388 sym->regs[j]->rIdx);
1394 /*-----------------------------------------------------------------*/
1395 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1396 /*-----------------------------------------------------------------*/
1398 regsUsedIniCode (iCode * ic)
1400 bitVect *rmask = newBitVect (mcs51_nRegs);
1402 /* do the special cases first */
1405 rmask = bitVectUnion (rmask,
1406 mcs51_rUmaskForOp (IC_COND (ic)));
1410 /* for the jumptable */
1411 if (ic->op == JUMPTABLE)
1413 rmask = bitVectUnion (rmask,
1414 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1419 /* of all other cases */
1421 rmask = bitVectUnion (rmask,
1422 mcs51_rUmaskForOp (IC_LEFT (ic)));
1426 rmask = bitVectUnion (rmask,
1427 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1430 rmask = bitVectUnion (rmask,
1431 mcs51_rUmaskForOp (IC_RESULT (ic)));
1437 /*-----------------------------------------------------------------*/
1438 /* createRegMask - for each instruction will determine the regsUsed */
1439 /*-----------------------------------------------------------------*/
1441 createRegMask (eBBlock ** ebbs, int count)
1445 /* for all blocks */
1446 for (i = 0; i < count; i++)
1450 if (ebbs[i]->noPath &&
1451 (ebbs[i]->entryLabel != entryLabel &&
1452 ebbs[i]->entryLabel != returnLabel))
1455 /* for all instructions */
1456 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1461 if (SKIP_IC2 (ic) || !ic->rlive)
1464 /* first mark the registers used in this
1466 ic->rUsed = regsUsedIniCode (ic);
1467 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1469 /* now create the register mask for those
1470 registers that are in use : this is a
1471 super set of ic->rUsed */
1472 ic->rMask = newBitVect (mcs51_nRegs + 1);
1474 /* for all live Ranges alive at this point */
1475 for (j = 1; j < ic->rlive->size; j++)
1480 /* if not alive then continue */
1481 if (!bitVectBitValue (ic->rlive, j))
1484 /* find the live range we are interested in */
1485 if (!(sym = hTabItemWithKey (liveRanges, j)))
1487 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1488 "createRegMask cannot find live range");
1489 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1493 /* if no register assigned to it */
1494 if (!sym->nRegs || sym->isspilt)
1497 /* for all the registers allocated to it */
1498 for (k = 0; k < sym->nRegs; k++)
1501 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1507 /*-----------------------------------------------------------------*/
1508 /* rematStr - returns the rematerialized string for a remat var */
1509 /*-----------------------------------------------------------------*/
1511 rematStr (symbol * sym)
1514 iCode *ic = sym->rematiCode;
1519 /* if plus or minus print the right hand side */
1520 if (ic->op == '+' || ic->op == '-')
1522 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1525 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1529 /* cast then continue */
1530 if (IS_CAST_ICODE(ic)) {
1531 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1534 /* we reached the end */
1535 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1542 /*-----------------------------------------------------------------*/
1543 /* regTypeNum - computes the type & number of registers required */
1544 /*-----------------------------------------------------------------*/
1546 regTypeNum (eBBlock *ebbs)
1552 /* for each live range do */
1553 for (sym = hTabFirstItem (liveRanges, &k); sym;
1554 sym = hTabNextItem (liveRanges, &k))
1557 /* if used zero times then no registers needed */
1558 if ((sym->liveTo - sym->liveFrom) == 0)
1562 /* if the live range is a temporary */
1566 /* if the type is marked as a conditional */
1567 if (sym->regType == REG_CND)
1570 /* if used in return only then we don't
1572 if (sym->ruonly || sym->accuse)
1574 if (IS_AGGREGATE (sym->type) || sym->isptr)
1575 sym->type = aggrToPtr (sym->type, FALSE);
1579 /* if the symbol has only one definition &
1580 that definition is a get_pointer */
1581 if (bitVectnBitsOn (sym->defs) == 1 &&
1582 (ic = hTabItemWithKey (iCodehTab,
1583 bitVectFirstBit (sym->defs))) &&
1586 !IS_BITVAR (sym->etype))
1590 /* and that pointer is remat in data space */
1591 if (IS_SYMOP (IC_LEFT (ic)) &&
1592 OP_SYMBOL (IC_LEFT (ic))->remat &&
1593 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1594 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1596 /* create a psuedo symbol & force a spil */
1597 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1598 psym->type = sym->type;
1599 psym->etype = sym->etype;
1601 strcpy (psym->rname, psym->name);
1603 sym->usl.spillLoc = psym;
1604 #if 0 // an alternative fix for bug #480076
1605 /* now this is a useless assignment to itself */
1606 remiCodeFromeBBlock (ebbs, ic);
1608 /* now this really is an assignment to itself, make it so;
1609 it will be optimized out later */
1611 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), IC_RESULT(ic));
1617 /* if in data space or idata space then try to
1618 allocate pointer register */
1622 /* if not then we require registers */
1623 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1624 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1625 getSize (sym->type));
1629 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1630 printTypeChain (sym->type, stderr);
1631 fprintf (stderr, "\n");
1634 /* determine the type of register required */
1635 if (sym->nRegs == 1 &&
1636 IS_PTR (sym->type) &&
1638 sym->regType = REG_PTR;
1640 sym->regType = REG_GPR;
1644 /* for the first run we don't provide */
1645 /* registers for true symbols we will */
1646 /* see how things go */
1652 /*-----------------------------------------------------------------*/
1653 /* freeAllRegs - mark all registers as free */
1654 /*-----------------------------------------------------------------*/
1660 for (i = 0; i < mcs51_nRegs; i++)
1661 regs8051[i].isFree = 1;
1664 /*-----------------------------------------------------------------*/
1665 /* deallocStackSpil - this will set the stack pointer back */
1666 /*-----------------------------------------------------------------*/
1668 DEFSETFUNC (deallocStackSpil)
1676 /*-----------------------------------------------------------------*/
1677 /* farSpacePackable - returns the packable icode for far variables */
1678 /*-----------------------------------------------------------------*/
1680 farSpacePackable (iCode * ic)
1684 /* go thru till we find a definition for the
1685 symbol on the right */
1686 for (dic = ic->prev; dic; dic = dic->prev)
1688 /* if the definition is a call then no */
1689 if ((dic->op == CALL || dic->op == PCALL) &&
1690 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1695 /* if shift by unknown amount then not */
1696 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1697 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1700 /* if pointer get and size > 1 */
1701 if (POINTER_GET (dic) &&
1702 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1705 if (POINTER_SET (dic) &&
1706 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1711 if (IC_COND (dic) &&
1712 IS_TRUE_SYMOP (IC_COND (dic)) &&
1713 isOperandInFarSpace (IC_COND (dic)))
1716 else if (dic->op == JUMPTABLE)
1718 if (IC_JTCOND (dic) &&
1719 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1720 isOperandInFarSpace (IC_JTCOND (dic)))
1725 /* if any three is a true symbol in far space */
1726 if (IC_RESULT (dic) &&
1727 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1728 isOperandInFarSpace (IC_RESULT (dic)))
1731 if (IC_RIGHT (dic) &&
1732 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1733 isOperandInFarSpace (IC_RIGHT (dic)) &&
1734 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1737 if (IC_LEFT (dic) &&
1738 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1739 isOperandInFarSpace (IC_LEFT (dic)) &&
1740 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1744 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1746 if ((dic->op == LEFT_OP ||
1747 dic->op == RIGHT_OP ||
1749 IS_OP_LITERAL (IC_RIGHT (dic)))
1759 /*-----------------------------------------------------------------*/
1760 /* packRegsForAssign - register reduction for assignment */
1761 /*-----------------------------------------------------------------*/
1763 packRegsForAssign (iCode * ic, eBBlock * ebp)
1767 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1768 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1769 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1775 /* if the true symbol is defined in far space or on stack
1776 then we should not since this will increase register pressure */
1777 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1781 /* find the definition of iTempNN scanning backwards if we find a
1782 a use of the true symbol in before we find the definition then
1784 for (dic = ic->prev; dic; dic = dic->prev)
1787 #if 0 /* jwk: This collides with 1.43 but I really see no need for
1788 this anymore. It fixes bug #716790 and substantially improves
1789 redundant register usage around function calls.
1792 /* if there is a function call then don't pack it */
1793 if ((dic->op == CALL || dic->op == PCALL))
1805 if (IS_SYMOP (IC_COND (dic)) &&
1806 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1807 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1815 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1816 IS_OP_VOLATILE (IC_RESULT (dic)))
1822 if (IS_SYMOP (IC_RESULT (dic)) &&
1823 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1825 if (POINTER_SET (dic))
1831 if (IS_SYMOP (IC_RIGHT (dic)) &&
1832 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1833 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1839 if (IS_SYMOP (IC_LEFT (dic)) &&
1840 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1841 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1847 if (POINTER_SET (dic) &&
1848 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1857 return 0; /* did not find */
1859 /* if assignment then check that right is not a bit */
1860 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1862 sym_link *etype = operandType (IC_RIGHT (dic));
1863 if (IS_BITFIELD (etype))
1865 /* if result is a bit too then it's ok */
1866 etype = operandType (IC_RESULT (dic));
1867 if (!IS_BITFIELD (etype))
1871 /* if the result is on stack or iaccess then it must be
1872 the same atleast one of the operands */
1873 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1874 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1877 /* the operation has only one symbol
1878 operator then we can pack */
1879 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1880 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1883 if (!((IC_LEFT (dic) &&
1884 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1886 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1890 /* found the definition */
1891 /* replace the result with the result of */
1892 /* this assignment and remove this assignment */
1893 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1894 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
1896 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1898 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1900 // TODO: and the otherway around?
1902 /* delete from liverange table also
1903 delete from all the points inbetween and the new
1905 for (sic = dic; sic != ic; sic = sic->next)
1907 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1908 if (IS_ITEMP (IC_RESULT (dic)))
1909 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1912 remiCodeFromeBBlock (ebp, ic);
1913 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1914 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1915 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1919 /*------------------------------------------------------------------*/
1920 /* findAssignToSym : scanning backwards looks for first assig found */
1921 /*------------------------------------------------------------------*/
1923 findAssignToSym (operand * op, iCode * ic)
1927 /* This routine is used to find sequences like
1929 ...; (intervening ops don't use iTempAA or modify FOO)
1930 blah = blah + iTempAA;
1932 and eliminate the use of iTempAA, freeing up its register for
1937 for (dic = ic->prev; dic; dic = dic->prev)
1940 /* if definition by assignment */
1941 if (dic->op == '=' &&
1942 !POINTER_SET (dic) &&
1943 IC_RESULT (dic)->key == op->key
1944 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1946 break; /* found where this temp was defined */
1948 /* if we find an usage then we cannot delete it */
1952 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
1955 else if (dic->op == JUMPTABLE)
1957 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
1962 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1965 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1968 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1974 return NULL; /* didn't find any assignment to op */
1976 /* we are interested only if defined in far space */
1977 /* or in stack space in case of + & - */
1979 /* if assigned to a non-symbol then don't repack regs */
1980 if (!IS_SYMOP (IC_RIGHT (dic)))
1983 /* if the symbol is volatile then we should not */
1984 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
1986 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
1987 What does it mean for an iTemp to be volatile, anyway? Passing
1988 TRUE is more cautious but may prevent possible optimizations */
1990 /* if the symbol is in far space then we should not */
1991 if (isOperandInFarSpace (IC_RIGHT (dic)))
1994 /* for + & - operations make sure that
1995 if it is on the stack it is the same
1996 as one of the three operands */
1997 if ((ic->op == '+' || ic->op == '-') &&
1998 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2001 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2002 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2003 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2007 /* now make sure that the right side of dic
2008 is not defined between ic & dic */
2011 iCode *sic = dic->next;
2013 for (; sic != ic; sic = sic->next)
2014 if (IC_RESULT (sic) &&
2015 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2022 /*-----------------------------------------------------------------*/
2023 /* reassignAliasedSym - used by packRegsForSupport to replace */
2024 /* redundant iTemp with equivalent symbol */
2025 /*-----------------------------------------------------------------*/
2027 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2030 unsigned oldSymKey, newSymKey;
2032 oldSymKey = op->key;
2033 newSymKey = IC_RIGHT(assignment)->key;
2035 /* only track live ranges of compiler-generated temporaries */
2036 if (!IS_ITEMP(IC_RIGHT(assignment)))
2039 /* update the live-value bitmaps */
2040 for (ic = assignment; ic != use; ic = ic->next) {
2041 bitVectUnSetBit (ic->rlive, oldSymKey);
2043 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2046 /* update the sym of the used operand */
2047 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2048 op->key = OP_SYMBOL(op)->key;
2050 /* update the sym's liverange */
2051 if ( OP_LIVETO(op) < ic->seq )
2052 setToRange(op, ic->seq, FALSE);
2054 /* remove the assignment iCode now that its result is unused */
2055 remiCodeFromeBBlock (ebp, assignment);
2056 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2057 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2061 /*-----------------------------------------------------------------*/
2062 /* packRegsForSupport :- reduce some registers for support calls */
2063 /*-----------------------------------------------------------------*/
2065 packRegsForSupport (iCode * ic, eBBlock * ebp)
2069 /* for the left & right operand :- look to see if the
2070 left was assigned a true symbol in far space in that
2071 case replace them */
2073 if (IS_ITEMP (IC_LEFT (ic)) &&
2074 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2076 dic = findAssignToSym (IC_LEFT (ic), ic);
2080 /* found it we need to remove it from the block */
2081 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2086 /* do the same for the right operand */
2087 if (IS_ITEMP (IC_RIGHT (ic)) &&
2088 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2090 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2094 /* if this is a subtraction & the result
2095 is a true symbol in far space then don't pack */
2096 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2098 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2099 if (IN_FARSPACE (SPEC_OCLS (etype)))
2102 /* found it we need to remove it from the
2104 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2113 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2116 /*-----------------------------------------------------------------*/
2117 /* packRegsForOneuse : - will reduce some registers for single Use */
2118 /*-----------------------------------------------------------------*/
2120 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2125 /* if returning a literal then do nothing */
2129 /* only upto 2 bytes since we cannot predict
2130 the usage of b, & acc */
2131 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2134 if (ic->op != RETURN &&
2136 !POINTER_SET (ic) &&
2140 if (ic->op == SEND && ic->argreg != 1) return NULL;
2142 /* this routine will mark the a symbol as used in one
2143 instruction use only && if the defintion is local
2144 (ie. within the basic block) && has only one definition &&
2145 that definiion is either a return value from a
2146 function or does not contain any variables in
2148 uses = bitVectCopy (OP_USES (op));
2149 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2150 if (!bitVectIsZero (uses)) /* has other uses */
2153 /* if it has only one defintion */
2154 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2155 return NULL; /* has more than one definition */
2157 /* get that definition */
2159 hTabItemWithKey (iCodehTab,
2160 bitVectFirstBit (OP_DEFS (op)))))
2163 /* if that only usage is a cast */
2164 if (dic->op == CAST) {
2165 /* to a bigger type */
2166 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2167 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2168 /* than we can not, since we cannot predict the usage of b & acc */
2173 /* found the definition now check if it is local */
2174 if (dic->seq < ebp->fSeq ||
2175 dic->seq > ebp->lSeq)
2176 return NULL; /* non-local */
2178 /* now check if it is the return from
2180 if (dic->op == CALL || dic->op == PCALL)
2182 if (ic->op != SEND && ic->op != RETURN &&
2183 !POINTER_SET(ic) && !POINTER_GET(ic))
2185 OP_SYMBOL (op)->ruonly = 1;
2192 /* otherwise check that the definition does
2193 not contain any symbols in far space */
2194 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2195 isOperandInFarSpace (IC_RIGHT (dic)) ||
2196 IS_OP_RUONLY (IC_LEFT (ic)) ||
2197 IS_OP_RUONLY (IC_RIGHT (ic)))
2202 /* if pointer set then make sure the pointer
2204 if (POINTER_SET (dic) &&
2205 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2208 if (POINTER_GET (dic) &&
2209 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2214 /* also make sure the intervenening instructions
2215 don't have any thing in far space */
2216 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2219 /* if there is an intervening function call then no */
2220 if (dic->op == CALL || dic->op == PCALL)
2222 /* if pointer set then make sure the pointer
2224 if (POINTER_SET (dic) &&
2225 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2228 if (POINTER_GET (dic) &&
2229 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2232 /* if address of & the result is remat the okay */
2233 if (dic->op == ADDRESS_OF &&
2234 OP_SYMBOL (IC_RESULT (dic))->remat)
2237 /* if operand has size of three or more & this
2238 operation is a '*','/' or '%' then 'b' may
2240 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2241 getSize (operandType (op)) >= 3)
2244 /* if left or right or result is in far space */
2245 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2246 isOperandInFarSpace (IC_RIGHT (dic)) ||
2247 isOperandInFarSpace (IC_RESULT (dic)) ||
2248 IS_OP_RUONLY (IC_LEFT (dic)) ||
2249 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2250 IS_OP_RUONLY (IC_RESULT (dic)))
2254 /* if left or right or result is on stack */
2255 if (isOperandOnStack(IC_LEFT(dic)) ||
2256 isOperandOnStack(IC_RIGHT(dic)) ||
2257 isOperandOnStack(IC_RESULT(dic))) {
2262 OP_SYMBOL (op)->ruonly = 1;
2266 /*-----------------------------------------------------------------*/
2267 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2268 /*-----------------------------------------------------------------*/
2270 isBitwiseOptimizable (iCode * ic)
2272 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2273 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2275 /* bitwise operations are considered optimizable
2276 under the following conditions (Jean-Louis VERN)
2288 if (IS_LITERAL(rtype) ||
2289 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2295 /*-----------------------------------------------------------------*/
2296 /* isCommutativeOp - tests whether this op cares what order its */
2297 /* operands are in */
2298 /*-----------------------------------------------------------------*/
2299 bool isCommutativeOp(unsigned int op)
2301 if (op == '+' || op == '*' || op == EQ_OP ||
2302 op == '^' || op == '|' || op == BITWISEAND)
2308 /*-----------------------------------------------------------------*/
2309 /* operandUsesAcc - determines whether the code generated for this */
2310 /* operand will have to use the accumulator */
2311 /*-----------------------------------------------------------------*/
2312 bool operandUsesAcc(operand *op)
2318 symbol *sym = OP_SYMBOL(op);
2322 return TRUE; /* duh! */
2324 if (IN_STACK(sym->etype) || sym->onStack ||
2325 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2326 return TRUE; /* acc is used to calc stack offset */
2331 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2333 return FALSE; /* more checks? */
2337 symspace = SPEC_OCLS(sym->etype);
2339 if (sym->iaccess && symspace->paged)
2340 return TRUE; /* must fetch paged indirect sym via accumulator */
2342 if (IN_BITSPACE(symspace))
2343 return TRUE; /* fetching bit vars uses the accumulator */
2345 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2346 return TRUE; /* fetched via accumulator and dptr */
2352 /*-----------------------------------------------------------------*/
2353 /* packRegsForAccUse - pack registers for acc use */
2354 /*-----------------------------------------------------------------*/
2356 packRegsForAccUse (iCode * ic)
2360 /* if this is an aggregate, e.g. a one byte char array */
2361 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2365 /* if we are calling a reentrant function that has stack parameters */
2366 if (ic->op == CALL &&
2367 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2368 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2371 if (ic->op == PCALL &&
2372 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2373 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2376 /* if + or - then it has to be one byte result */
2377 if ((ic->op == '+' || ic->op == '-')
2378 && getSize (operandType (IC_RESULT (ic))) > 1)
2381 /* if shift operation make sure right side is not a literal */
2382 if (ic->op == RIGHT_OP &&
2383 (isOperandLiteral (IC_RIGHT (ic)) ||
2384 getSize (operandType (IC_RESULT (ic))) > 1))
2387 if (ic->op == LEFT_OP &&
2388 (isOperandLiteral (IC_RIGHT (ic)) ||
2389 getSize (operandType (IC_RESULT (ic))) > 1))
2392 if (IS_BITWISE_OP (ic) &&
2393 getSize (operandType (IC_RESULT (ic))) > 1)
2397 /* has only one definition */
2398 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2401 /* has only one use */
2402 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2405 /* and the usage immediately follows this iCode */
2406 if (!(uic = hTabItemWithKey (iCodehTab,
2407 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2410 if (ic->next != uic)
2413 /* if it is a conditional branch then we definitely can */
2417 if (uic->op == JUMPTABLE)
2420 if (POINTER_SET (uic) &&
2421 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2424 /* if the usage is not is an assignment
2425 or an arithmetic / bitwise / shift operation then not */
2426 if (uic->op != '=' &&
2427 !IS_ARITHMETIC_OP (uic) &&
2428 !IS_BITWISE_OP (uic) &&
2429 uic->op != LEFT_OP &&
2430 uic->op != RIGHT_OP)
2433 /* if used in ^ operation then make sure right is not a
2434 literal (WIML: Why is this?) */
2435 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2438 /* if shift operation make sure right side is not a literal */
2439 /* WIML: Why is this? */
2440 if (uic->op == RIGHT_OP &&
2441 (isOperandLiteral (IC_RIGHT (uic)) ||
2442 getSize (operandType (IC_RESULT (uic))) > 1))
2444 if (uic->op == LEFT_OP &&
2445 (isOperandLiteral (IC_RIGHT (uic)) ||
2446 getSize (operandType (IC_RESULT (uic))) > 1))
2449 /* make sure that the result of this icode is not on the
2450 stack, since acc is used to compute stack offset */
2452 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2453 OP_SYMBOL (IC_RESULT (uic))->onStack)
2456 if (isOperandOnStack(IC_RESULT(uic)))
2460 /* if the usage has only one operand then we can */
2461 if (IC_LEFT (uic) == NULL ||
2462 IC_RIGHT (uic) == NULL)
2465 /* if the other operand uses the accumulator then we cannot */
2466 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2467 operandUsesAcc(IC_RIGHT(uic))) ||
2468 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2469 operandUsesAcc(IC_LEFT(uic))) )
2472 /* make sure this is on the left side if not commutative */
2473 /* except for '-', which has been written to be able to
2474 handle reversed operands */
2475 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2476 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2480 // this is too dangerous and need further restrictions
2483 /* if one of them is a literal then we can */
2484 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2485 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2487 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2493 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2497 /*-----------------------------------------------------------------*/
2498 /* packForPush - hueristics to reduce iCode for pushing */
2499 /*-----------------------------------------------------------------*/
2501 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2505 struct eBBlock * ebp=ebpp[blockno];
2507 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2510 /* must have only definition & one usage */
2511 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2512 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2515 /* find the definition */
2516 if (!(dic = hTabItemWithKey (iCodehTab,
2517 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2520 if (dic->op != '=' || POINTER_SET (dic))
2523 if (dic->seq < ebp->fSeq) { // Evelyn did this
2525 for (i=0; i<blockno; i++) {
2526 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2531 wassert (i!=blockno); // no way to recover from here
2534 if (IS_SYMOP(IC_RIGHT(dic))) {
2535 /* make sure the right side does not have any definitions
2537 dbv = OP_DEFS(IC_RIGHT(dic));
2538 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2539 if (bitVectBitValue(dbv,lic->key))
2542 /* make sure they have the same type */
2543 if (IS_SPEC(operandType(IC_LEFT(ic))))
2545 sym_link *itype=operandType(IC_LEFT(ic));
2546 sym_link *ditype=operandType(IC_RIGHT(dic));
2548 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2549 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2552 /* extend the live range of replaced operand if needed */
2553 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2554 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2556 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2559 /* we now we know that it has one & only one def & use
2560 and the that the definition is an assignment */
2561 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2562 remiCodeFromeBBlock (ebp, dic);
2563 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2566 /*-----------------------------------------------------------------*/
2567 /* packRegisters - does some transformations to reduce register */
2569 /*-----------------------------------------------------------------*/
2571 packRegisters (eBBlock ** ebpp, int blockno)
2575 eBBlock *ebp=ebpp[blockno];
2582 /* look for assignments of the form */
2583 /* iTempNN = TRueSym (someoperation) SomeOperand */
2585 /* TrueSym := iTempNN:1 */
2586 for (ic = ebp->sch; ic; ic = ic->next)
2588 /* find assignment of the form TrueSym := iTempNN:1 */
2589 if (ic->op == '=' && !POINTER_SET (ic))
2590 change += packRegsForAssign (ic, ebp);
2597 for (ic = ebp->sch; ic; ic = ic->next)
2599 /* if this is an itemp & result of an address of a true sym
2600 then mark this as rematerialisable */
2601 if (ic->op == ADDRESS_OF &&
2602 IS_ITEMP (IC_RESULT (ic)) &&
2603 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2604 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2605 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2608 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2609 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2610 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2614 /* if straight assignment then carry remat flag if
2615 this is the only definition */
2616 if (ic->op == '=' &&
2617 !POINTER_SET (ic) &&
2618 IS_SYMOP (IC_RIGHT (ic)) &&
2619 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2620 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2621 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2624 OP_SYMBOL (IC_RESULT (ic))->remat =
2625 OP_SYMBOL (IC_RIGHT (ic))->remat;
2626 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2627 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2630 /* if cast to a generic pointer & the pointer being
2631 cast is remat, then we can remat this cast as well */
2632 if (ic->op == CAST &&
2633 IS_SYMOP(IC_RIGHT(ic)) &&
2634 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2635 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1) {
2636 sym_link *to_type = operandType(IC_LEFT(ic));
2637 sym_link *from_type = operandType(IC_RIGHT(ic));
2638 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2639 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2640 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2641 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2645 /* if this is a +/- operation with a rematerizable
2646 then mark this as rematerializable as well */
2647 if ((ic->op == '+' || ic->op == '-') &&
2648 (IS_SYMOP (IC_LEFT (ic)) &&
2649 IS_ITEMP (IC_RESULT (ic)) &&
2650 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2651 OP_SYMBOL (IC_LEFT (ic))->remat &&
2652 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2653 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2655 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2656 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2657 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2660 /* mark the pointer usages */
2661 if (POINTER_SET (ic))
2662 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2664 if (POINTER_GET (ic) &&
2665 IS_SYMOP(IC_LEFT (ic)))
2666 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2670 /* if we are using a symbol on the stack
2671 then we should say mcs51_ptrRegReq */
2672 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2673 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2674 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2675 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2676 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2677 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2680 if (IS_SYMOP (IC_LEFT (ic)))
2681 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2682 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2683 if (IS_SYMOP (IC_RIGHT (ic)))
2684 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2685 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2686 if (IS_SYMOP (IC_RESULT (ic)))
2687 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2688 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2692 /* if the condition of an if instruction
2693 is defined in the previous instruction and
2694 this is the only usage then
2695 mark the itemp as a conditional */
2696 if ((IS_CONDITIONAL (ic) ||
2697 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2698 ic->next && ic->next->op == IFX &&
2699 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2700 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2701 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2703 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2707 /* reduce for support function calls */
2708 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2709 packRegsForSupport (ic, ebp);
2711 /* some cases the redundant moves can
2712 can be eliminated for return statements */
2713 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2714 !isOperandInFarSpace (IC_LEFT (ic)) &&
2715 options.model == MODEL_SMALL) {
2716 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2719 /* if pointer set & left has a size more than
2720 one and right is not in far space */
2721 if (POINTER_SET (ic) &&
2722 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2723 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2724 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2725 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2726 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2728 /* if pointer get */
2729 if (POINTER_GET (ic) &&
2730 IS_SYMOP (IC_LEFT (ic)) &&
2731 !isOperandInFarSpace (IC_RESULT (ic)) &&
2732 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2733 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2734 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2735 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2738 /* if this is cast for intergral promotion then
2739 check if only use of the definition of the
2740 operand being casted/ if yes then replace
2741 the result of that arithmetic operation with
2742 this result and get rid of the cast */
2745 sym_link *fromType = operandType (IC_RIGHT (ic));
2746 sym_link *toType = operandType (IC_LEFT (ic));
2748 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2749 getSize (fromType) != getSize (toType) &&
2750 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2753 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2756 if (IS_ARITHMETIC_OP (dic))
2758 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2759 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2760 remiCodeFromeBBlock (ebp, ic);
2761 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2762 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2763 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2767 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2773 /* if the type from and type to are the same
2774 then if this is the only use then packit */
2775 if (compareType (operandType (IC_RIGHT (ic)),
2776 operandType (IC_LEFT (ic))) == 1)
2778 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2781 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2782 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2783 remiCodeFromeBBlock (ebp, ic);
2784 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2785 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2786 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2794 iTempNN := (some variable in farspace) V1
2799 if (ic->op == IPUSH)
2801 packForPush (ic, ebpp, blockno);
2805 /* pack registers for accumulator use, when the
2806 result of an arithmetic or bit wise operation
2807 has only one use, that use is immediately following
2808 the defintion and the using iCode has only one
2809 operand or has two operands but one is literal &
2810 the result of that operation is not on stack then
2811 we can leave the result of this operation in acc:b
2813 if ((IS_ARITHMETIC_OP (ic)
2814 || IS_CONDITIONAL(ic)
2815 || IS_BITWISE_OP (ic)
2816 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2817 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2819 IS_ITEMP (IC_RESULT (ic)) &&
2820 getSize (operandType (IC_RESULT (ic))) <= 2)
2822 packRegsForAccUse (ic);
2826 /*-----------------------------------------------------------------*/
2827 /* assignRegisters - assigns registers to each live range as need */
2828 /*-----------------------------------------------------------------*/
2830 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2835 setToNull ((void *) &_G.funcrUsed);
2836 setToNull ((void *) &_G.regAssigned);
2837 setToNull ((void *) &_G.totRegAssigned);
2838 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2841 /* change assignments this will remove some
2842 live ranges reducing some register pressure */
2844 for (i = 0; i < count; i++)
2845 packRegisters (ebbs, i);
2848 if (options.dump_pack)
2849 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2851 /* first determine for each live range the number of
2852 registers & the type of registers required for each */
2855 /* and serially allocate registers */
2856 serialRegAssign (ebbs, count);
2859 //setToNull ((void *) &_G.regAssigned);
2860 //setToNull ((void *) &_G.totRegAssigned);
2863 /* if stack was extended then tell the user */
2866 /* werror(W_TOOMANY_SPILS,"stack", */
2867 /* _G.stackExtend,currFunc->name,""); */
2873 /* werror(W_TOOMANY_SPILS,"data space", */
2874 /* _G.dataExtend,currFunc->name,""); */
2878 /* after that create the register mask
2879 for each of the instruction */
2880 createRegMask (ebbs, count);
2882 /* redo that offsets for stacked automatic variables */
2884 redoStackOffsets ();
2887 if (options.dump_rassgn)
2889 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2890 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2893 /* do the overlaysegment stuff SDCCmem.c */
2894 doOverlays (ebbs, count);
2896 /* now get back the chain */
2897 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2901 /* free up any _G.stackSpil locations allocated */
2902 applyToSet (_G.stackSpil, deallocStackSpil);
2904 setToNull ((void **) &_G.stackSpil);
2905 setToNull ((void **) &_G.spiltSet);
2906 /* mark all registers as free */