1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
39 extern void gen51Code (iCode *);
48 bitVect *totRegAssigned; /* final set of LRs that got into registers */
51 bitVect *funcrUsed; /* registers used in a function */
57 /* Shared with gen.c */
58 int mcs51_ptrRegReq; /* one byte pointer register required */
64 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
65 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
66 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
67 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
68 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
69 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
70 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
71 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
72 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
73 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
74 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
75 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
76 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
77 {REG_CND, CND_IDX, REG_CND, "C", "psw", "0xd0", 0, 1},
78 {0, DPL_IDX, 0, "dpl", "dpl", "0x82", 0, 0},
79 {0, DPH_IDX, 0, "dph", "dph", "0x83", 0, 0},
80 {0, B_IDX, 0, "b", "b", "0xf0", 0, 0},
81 {0, A_IDX, 0, "a", "acc", "0xe0", 0, 0},
84 static void spillThis (symbol *);
85 static void freeAllRegs ();
87 /*-----------------------------------------------------------------*/
88 /* allocReg - allocates register of given type */
89 /*-----------------------------------------------------------------*/
95 for (i = 0; i < mcs51_nRegs; i++)
98 /* if type is given as 0 then any
99 free register will do */
103 regs8051[i].isFree = 0;
106 bitVectSetBit (currFunc->regsUsed, i);
109 /* other wise look for specific type
111 if (regs8051[i].isFree &&
112 regs8051[i].type == type)
114 regs8051[i].isFree = 0;
117 bitVectSetBit (currFunc->regsUsed, i);
124 /*-----------------------------------------------------------------*/
125 /* allocThisReg - allocates a particular register (if free) */
126 /*-----------------------------------------------------------------*/
128 allocThisReg (regs * reg)
135 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, reg->rIdx);
141 /*-----------------------------------------------------------------*/
142 /* mcs51_regWithIdx - returns pointer to register wit index number */
143 /*-----------------------------------------------------------------*/
145 mcs51_regWithIdx (int idx)
149 for (i = 0; i < sizeof(regs8051)/sizeof(regs); i++)
150 if (regs8051[i].rIdx == idx)
153 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
154 "regWithIdx not found");
158 /*-----------------------------------------------------------------*/
159 /* freeReg - frees a register */
160 /*-----------------------------------------------------------------*/
166 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
167 "freeReg - Freeing NULL register");
175 /*-----------------------------------------------------------------*/
176 /* nFreeRegs - returns number of free registers */
177 /*-----------------------------------------------------------------*/
184 for (i = 0; i < mcs51_nRegs; i++)
185 if (regs8051[i].isFree && regs8051[i].type == type)
190 /*-----------------------------------------------------------------*/
191 /* nfreeRegsType - free registers with type */
192 /*-----------------------------------------------------------------*/
194 nfreeRegsType (int type)
199 if ((nfr = nFreeRegs (type)) == 0)
200 return nFreeRegs (REG_GPR);
203 return nFreeRegs (type);
206 /*-----------------------------------------------------------------*/
207 /* useReg - marks a register as used */
208 /*-----------------------------------------------------------------*/
215 /*-----------------------------------------------------------------*/
216 /* computeSpillable - given a point find the spillable live ranges */
217 /*-----------------------------------------------------------------*/
219 computeSpillable (iCode * ic)
223 /* spillable live ranges are those that are live at this
224 point . the following categories need to be subtracted
226 a) - those that are already spilt
227 b) - if being used by this one
228 c) - defined by this one */
230 spillable = bitVectCopy (ic->rlive);
232 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
234 bitVectCplAnd (spillable, ic->uses); /* used in this one */
235 bitVectUnSetBit (spillable, ic->defKey);
236 spillable = bitVectIntersect (spillable, _G.regAssigned);
241 /*-----------------------------------------------------------------*/
242 /* noSpilLoc - return true if a variable has no spil location */
243 /*-----------------------------------------------------------------*/
245 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
247 return (sym->usl.spillLoc ? 0 : 1);
250 /*-----------------------------------------------------------------*/
251 /* hasSpilLoc - will return 1 if the symbol has spil location */
252 /*-----------------------------------------------------------------*/
254 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
256 return (sym->usl.spillLoc ? 1 : 0);
259 /*-----------------------------------------------------------------*/
260 /* directSpilLoc - will return 1 if the splilocation is in direct */
261 /*-----------------------------------------------------------------*/
263 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
265 if (sym->usl.spillLoc &&
266 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
272 /*-----------------------------------------------------------------*/
273 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
274 /* but is not used as a pointer */
275 /*-----------------------------------------------------------------*/
277 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
279 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
282 /*-----------------------------------------------------------------*/
283 /* rematable - will return 1 if the remat flag is set */
284 /*-----------------------------------------------------------------*/
286 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
291 /*-----------------------------------------------------------------*/
292 /* notUsedInRemaining - not used or defined in remain of the block */
293 /*-----------------------------------------------------------------*/
295 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
297 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
298 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
301 /*-----------------------------------------------------------------*/
302 /* allLRs - return true for all */
303 /*-----------------------------------------------------------------*/
305 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
310 /*-----------------------------------------------------------------*/
311 /* liveRangesWith - applies function to a given set of live range */
312 /*-----------------------------------------------------------------*/
314 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
315 eBBlock * ebp, iCode * ic)
320 if (!lrs || !lrs->size)
323 for (i = 1; i < lrs->size; i++)
326 if (!bitVectBitValue (lrs, i))
329 /* if we don't find it in the live range
330 hash table we are in serious trouble */
331 if (!(sym = hTabItemWithKey (liveRanges, i)))
333 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
334 "liveRangesWith could not find liveRange");
338 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
339 addSetHead (&rset, sym);
346 /*-----------------------------------------------------------------*/
347 /* leastUsedLR - given a set determines which is the least used */
348 /*-----------------------------------------------------------------*/
350 leastUsedLR (set * sset)
352 symbol *sym = NULL, *lsym = NULL;
354 sym = lsym = setFirstItem (sset);
359 for (; lsym; lsym = setNextItem (sset))
362 /* if usage is the same then prefer
363 the spill the smaller of the two */
364 if (lsym->used == sym->used)
365 if (getSize (lsym->type) < getSize (sym->type))
369 if (lsym->used < sym->used)
374 setToNull ((void *) &sset);
379 /*-----------------------------------------------------------------*/
380 /* noOverLap - will iterate through the list looking for over lap */
381 /*-----------------------------------------------------------------*/
383 noOverLap (set * itmpStack, symbol * fsym)
388 for (sym = setFirstItem (itmpStack); sym;
389 sym = setNextItem (itmpStack))
391 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
397 /*-----------------------------------------------------------------*/
398 /* isFree - will return 1 if the a free spil location is found */
399 /*-----------------------------------------------------------------*/
404 V_ARG (symbol **, sloc);
405 V_ARG (symbol *, fsym);
407 /* if already found */
411 /* if it is free && and the itmp assigned to
412 this does not have any overlapping live ranges
413 with the one currently being assigned and
414 the size can be accomodated */
416 noOverLap (sym->usl.itmpStack, fsym) &&
417 getSize (sym->type) >= getSize (fsym->type))
426 /*-----------------------------------------------------------------*/
427 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
428 /*-----------------------------------------------------------------*/
430 spillLRWithPtrReg (symbol * forSym)
436 if (!_G.regAssigned ||
437 bitVectIsZero (_G.regAssigned))
440 r0 = mcs51_regWithIdx (R0_IDX);
441 r1 = mcs51_regWithIdx (R1_IDX);
443 /* for all live ranges */
444 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
445 lrsym = hTabNextItem (liveRanges, &k))
449 /* if no registers assigned to it or spilt */
450 /* if it does not overlap with this then
451 not need to spill it */
453 if (lrsym->isspilt || !lrsym->nRegs ||
454 (lrsym->liveTo < forSym->liveFrom))
457 /* go thru the registers : if it is either
458 r0 or r1 then spil it */
459 for (j = 0; j < lrsym->nRegs; j++)
460 if (lrsym->regs[j] == r0 ||
461 lrsym->regs[j] == r1)
470 /*-----------------------------------------------------------------*/
471 /* createStackSpil - create a location on the stack to spil */
472 /*-----------------------------------------------------------------*/
474 createStackSpil (symbol * sym)
477 int useXstack, model;
481 /* first go try and find a free one that is already
482 existing on the stack */
483 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
485 /* found a free one : just update & return */
486 sym->usl.spillLoc = sloc;
489 addSetHead (&sloc->usl.itmpStack, sym);
493 /* could not then have to create one , this is the hard part
494 we need to allocate this on the stack : this is really a
495 hack!! but cannot think of anything better at this time */
497 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
499 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
504 sloc = newiTemp (slocBuffer);
506 /* set the type to the spilling symbol */
507 sloc->type = copyLinkChain (sym->type);
508 sloc->etype = getSpec (sloc->type);
509 SPEC_SCLS (sloc->etype) = S_DATA;
510 SPEC_EXTR (sloc->etype) = 0;
511 SPEC_STAT (sloc->etype) = 0;
512 SPEC_VOLATILE(sloc->etype) = 0;
513 SPEC_ABSA(sloc->etype) = 0;
515 /* we don't allow it to be allocated`
516 onto the external stack since : so we
517 temporarily turn it off ; we also
518 turn off memory model to prevent
519 the spil from going to the external storage
522 useXstack = options.useXstack;
523 model = options.model;
524 /* noOverlay = options.noOverlay; */
525 /* options.noOverlay = 1; */
526 options.model = options.useXstack = 0;
530 options.useXstack = useXstack;
531 options.model = model;
532 /* options.noOverlay = noOverlay; */
533 sloc->isref = 1; /* to prevent compiler warning */
535 /* if it is on the stack then update the stack */
536 if (IN_STACK (sloc->etype))
538 currFunc->stack += getSize (sloc->type);
539 _G.stackExtend += getSize (sloc->type);
542 _G.dataExtend += getSize (sloc->type);
544 /* add it to the _G.stackSpil set */
545 addSetHead (&_G.stackSpil, sloc);
546 sym->usl.spillLoc = sloc;
549 /* add it to the set of itempStack set
550 of the spill location */
551 addSetHead (&sloc->usl.itmpStack, sym);
555 /*-----------------------------------------------------------------*/
556 /* isSpiltOnStack - returns true if the spil location is on stack */
557 /*-----------------------------------------------------------------*/
559 isSpiltOnStack (symbol * sym)
569 /* if (sym->_G.stackSpil) */
572 if (!sym->usl.spillLoc)
575 etype = getSpec (sym->usl.spillLoc->type);
576 if (IN_STACK (etype))
582 /*-----------------------------------------------------------------*/
583 /* spillThis - spils a specific operand */
584 /*-----------------------------------------------------------------*/
586 spillThis (symbol * sym)
589 /* if this is rematerializable or has a spillLocation
590 we are okay, else we need to create a spillLocation
592 if (!(sym->remat || sym->usl.spillLoc))
593 createStackSpil (sym);
595 /* mark it has spilt & put it in the spilt set */
596 sym->isspilt = sym->spillA = 1;
597 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
599 bitVectUnSetBit (_G.regAssigned, sym->key);
600 bitVectUnSetBit (_G.totRegAssigned, sym->key);
602 for (i = 0; i < sym->nRegs; i++)
606 freeReg (sym->regs[i]);
610 /* if spilt on stack then free up r0 & r1
611 if they could have been assigned to some
613 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
616 spillLRWithPtrReg (sym);
619 if (sym->usl.spillLoc && !sym->remat)
620 sym->usl.spillLoc->allocreq++;
624 /*-----------------------------------------------------------------*/
625 /* selectSpil - select a iTemp to spil : rather a simple procedure */
626 /*-----------------------------------------------------------------*/
628 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
630 bitVect *lrcs = NULL;
634 /* get the spillable live ranges */
635 lrcs = computeSpillable (ic);
637 /* get all live ranges that are rematerizable */
638 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
641 /* return the least used of these */
642 return leastUsedLR (selectS);
645 /* get live ranges with spillLocations in direct space */
646 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
648 sym = leastUsedLR (selectS);
649 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
650 sym->usl.spillLoc->rname :
651 sym->usl.spillLoc->name));
653 /* mark it as allocation required */
654 sym->usl.spillLoc->allocreq++;
658 /* if the symbol is local to the block then */
659 if (forSym->liveTo < ebp->lSeq)
662 /* check if there are any live ranges allocated
663 to registers that are not used in this block */
664 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
666 sym = leastUsedLR (selectS);
667 /* if this is not rematerializable */
676 /* check if there are any live ranges that not
677 used in the remainder of the block */
678 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
680 sym = leastUsedLR (selectS);
693 /* find live ranges with spillocation && not used as pointers */
694 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
697 sym = leastUsedLR (selectS);
698 /* mark this as allocation required */
699 sym->usl.spillLoc->allocreq++;
703 /* find live ranges with spillocation */
704 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
707 sym = leastUsedLR (selectS);
708 sym->usl.spillLoc->allocreq++;
712 /* couldn't find then we need to create a spil
713 location on the stack , for which one? the least
715 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
718 /* return a created spil location */
719 sym = createStackSpil (leastUsedLR (selectS));
720 sym->usl.spillLoc->allocreq++;
724 /* this is an extreme situation we will spill
725 this one : happens very rarely but it does happen */
731 /*-----------------------------------------------------------------*/
732 /* spilSomething - spil some variable & mark registers as free */
733 /*-----------------------------------------------------------------*/
735 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
740 /* get something we can spil */
741 ssym = selectSpil (ic, ebp, forSym);
743 /* mark it as spilt */
744 ssym->isspilt = ssym->spillA = 1;
745 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
747 /* mark it as not register assigned &
748 take it away from the set */
749 bitVectUnSetBit (_G.regAssigned, ssym->key);
750 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
752 /* mark the registers as free */
753 for (i = 0; i < ssym->nRegs; i++)
755 freeReg (ssym->regs[i]);
757 /* if spilt on stack then free up r0 & r1
758 if they could have been assigned to as gprs */
759 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
762 spillLRWithPtrReg (ssym);
765 /* if this was a block level spil then insert push & pop
766 at the start & end of block respectively */
769 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
770 /* add push to the start of the block */
771 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
772 ebp->sch->next : ebp->sch));
773 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
774 /* add pop to the end of the block */
775 addiCodeToeBBlock (ebp, nic, NULL);
778 /* if spilt because not used in the remainder of the
779 block then add a push before this instruction and
780 a pop at the end of the block */
781 if (ssym->remainSpil)
784 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
785 /* add push just before this instruction */
786 addiCodeToeBBlock (ebp, nic, ic);
788 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
789 /* add pop to the end of the block */
790 addiCodeToeBBlock (ebp, nic, NULL);
799 /*-----------------------------------------------------------------*/
800 /* getRegPtr - will try for PTR if not a GPR type if not spil */
801 /*-----------------------------------------------------------------*/
803 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
809 /* try for a ptr type */
810 if ((reg = allocReg (REG_PTR)))
813 /* try for gpr type */
814 if ((reg = allocReg (REG_GPR)))
817 /* we have to spil */
818 if (!spilSomething (ic, ebp, sym))
821 /* make sure partially assigned registers aren't reused */
822 for (j=0; j<=sym->nRegs; j++)
824 sym->regs[j]->isFree = 0;
826 /* this looks like an infinite loop but
827 in really selectSpil will abort */
831 /*-----------------------------------------------------------------*/
832 /* getRegGpr - will try for GPR if not spil */
833 /*-----------------------------------------------------------------*/
835 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
841 /* try for gpr type */
842 if ((reg = allocReg (REG_GPR)))
845 if (!mcs51_ptrRegReq)
846 if ((reg = allocReg (REG_PTR)))
849 /* we have to spil */
850 if (!spilSomething (ic, ebp, sym))
853 /* make sure partially assigned registers aren't reused */
854 for (j=0; j<=sym->nRegs; j++)
856 sym->regs[j]->isFree = 0;
858 /* this looks like an infinite loop but
859 in really selectSpil will abort */
863 /*-----------------------------------------------------------------*/
864 /* getRegPtrNoSpil - get it cannot split */
865 /*-----------------------------------------------------------------*/
866 static regs *getRegPtrNoSpil()
870 /* try for a ptr type */
871 if ((reg = allocReg (REG_PTR)))
874 /* try for gpr type */
875 if ((reg = allocReg (REG_GPR)))
880 /* just to make the compiler happy */
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)))
900 /* just to make the compiler happy */
904 /*-----------------------------------------------------------------*/
905 /* symHasReg - symbol has a given register */
906 /*-----------------------------------------------------------------*/
908 symHasReg (symbol * sym, regs * reg)
912 for (i = 0; i < sym->nRegs; i++)
913 if (sym->regs[i] == reg)
919 /*-----------------------------------------------------------------*/
920 /* deassignLRs - check the live to and if they have registers & are */
921 /* not spilt then free up the registers */
922 /*-----------------------------------------------------------------*/
924 deassignLRs (iCode * ic, eBBlock * ebp)
930 for (sym = hTabFirstItem (liveRanges, &k); sym;
931 sym = hTabNextItem (liveRanges, &k))
935 /* if it does not end here */
936 if (sym->liveTo > ic->seq)
939 /* if it was spilt on stack then we can
940 mark the stack spil location as free */
945 sym->usl.spillLoc->isFree = 1;
951 if (!bitVectBitValue (_G.regAssigned, sym->key))
954 /* special case check if this is an IFX &
955 the privious one was a pop and the
956 previous one was not spilt then keep track
958 if (ic->op == IFX && ic->prev &&
959 ic->prev->op == IPOP &&
960 !ic->prev->parmPush &&
961 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
962 psym = OP_SYMBOL (IC_LEFT (ic->prev));
968 bitVectUnSetBit (_G.regAssigned, sym->key);
970 /* if the result of this one needs registers
971 and does not have it then assign it right
973 if (IC_RESULT (ic) &&
974 !(SKIP_IC2 (ic) || /* not a special icode */
975 ic->op == JUMPTABLE ||
981 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
982 result->liveTo > ic->seq && /* and will live beyond this */
983 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
984 result->regType == sym->regType && /* same register types */
985 result->nRegs && /* which needs registers */
986 !result->isspilt && /* and does not already have them */
988 !bitVectBitValue (_G.regAssigned, result->key) &&
989 /* the number of free regs + number of regs in this LR
990 can accomodate the what result Needs */
991 ((nfreeRegsType (result->regType) +
992 sym->nRegs) >= result->nRegs)
996 for (i = 0; i < result->nRegs; i++)
998 result->regs[i] = sym->regs[i];
1000 result->regs[i] = getRegGpr (ic, ebp, result);
1002 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1003 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1007 /* free the remaining */
1008 for (; i < sym->nRegs; i++)
1012 if (!symHasReg (psym, sym->regs[i]))
1013 freeReg (sym->regs[i]);
1016 freeReg (sym->regs[i]);
1023 /*-----------------------------------------------------------------*/
1024 /* reassignLR - reassign this to registers */
1025 /*-----------------------------------------------------------------*/
1027 reassignLR (operand * op)
1029 symbol *sym = OP_SYMBOL (op);
1032 /* not spilt any more */
1033 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1034 bitVectUnSetBit (_G.spiltSet, sym->key);
1036 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1037 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1041 for (i = 0; i < sym->nRegs; i++)
1042 sym->regs[i]->isFree = 0;
1045 /*-----------------------------------------------------------------*/
1046 /* willCauseSpill - determines if allocating will cause a spill */
1047 /*-----------------------------------------------------------------*/
1049 willCauseSpill (int nr, int rt)
1051 /* first check if there are any avlb registers
1052 of te type required */
1055 /* special case for pointer type
1056 if pointer type not avlb then
1057 check for type gpr */
1058 if (nFreeRegs (rt) >= nr)
1060 if (nFreeRegs (REG_GPR) >= nr)
1065 if (mcs51_ptrRegReq)
1067 if (nFreeRegs (rt) >= nr)
1072 if (nFreeRegs (REG_PTR) +
1073 nFreeRegs (REG_GPR) >= nr)
1078 /* it will cause a spil */
1082 /*-----------------------------------------------------------------*/
1083 /* positionRegs - the allocator can allocate same registers to res- */
1084 /* ult and operand, if this happens make sure they are in the same */
1085 /* position as the operand otherwise chaos results */
1086 /*-----------------------------------------------------------------*/
1088 positionRegs (symbol * result, symbol * opsym)
1090 int count = min (result->nRegs, opsym->nRegs);
1091 int i, j = 0, shared = 0;
1094 /* if the result has been spilt then cannot share */
1099 /* first make sure that they actually share */
1100 for (i = 0; i < count; i++)
1102 for (j = 0; j < count; j++)
1104 if (result->regs[i] == opsym->regs[j] && i != j)
1114 regs *tmp = result->regs[i];
1115 result->regs[i] = result->regs[j];
1116 result->regs[j] = tmp;
1124 /*------------------------------------------------------------------*/
1125 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1126 /* it should either have registers or have beed spilled. Otherwise, */
1127 /* there was an uninitialized variable, so just spill this to get */
1128 /* the operand in a valid state. */
1129 /*------------------------------------------------------------------*/
1131 verifyRegsAssigned (operand *op, iCode * ic)
1136 if (!IS_ITEMP (op)) return;
1138 sym = OP_SYMBOL (op);
1139 if (sym->isspilt) return;
1140 if (!sym->nRegs) return;
1141 if (sym->regs[0]) return;
1143 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1144 sym->prereqv ? sym->prereqv->name : sym->name);
1150 /*-----------------------------------------------------------------*/
1151 /* serialRegAssign - serially allocate registers to the variables */
1152 /*-----------------------------------------------------------------*/
1154 serialRegAssign (eBBlock ** ebbs, int count)
1158 /* for all blocks */
1159 for (i = 0; i < count; i++) {
1163 if (ebbs[i]->noPath &&
1164 (ebbs[i]->entryLabel != entryLabel &&
1165 ebbs[i]->entryLabel != returnLabel))
1168 /* of all instructions do */
1169 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1173 // update the registers in use at the start of this icode
1174 for (reg=0; reg<mcs51_nRegs; reg++) {
1175 if (regs8051[reg].isFree) {
1176 ic->riu &= ~(1<<regs8051[reg].offset);
1178 ic->riu |= (1<<regs8051[reg].offset);
1182 /* if this is an ipop that means some live
1183 range will have to be assigned again */
1185 reassignLR (IC_LEFT (ic));
1187 /* if result is present && is a true symbol */
1188 if (IC_RESULT (ic) && ic->op != IFX &&
1189 IS_TRUE_SYMOP (IC_RESULT (ic)))
1190 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1192 /* take away registers from live
1193 ranges that end at this instruction */
1194 deassignLRs (ic, ebbs[i]);
1196 /* some don't need registers */
1197 if (SKIP_IC2 (ic) ||
1198 ic->op == JUMPTABLE ||
1202 (IC_RESULT (ic) && POINTER_SET (ic)))
1205 /* now we need to allocate registers
1206 only for the result */
1207 if (IC_RESULT (ic)) {
1208 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1214 /* if it does not need or is spilt
1215 or is already assigned to registers
1216 or will not live beyond this instructions */
1219 bitVectBitValue (_G.regAssigned, sym->key) ||
1220 sym->liveTo <= ic->seq)
1223 /* if some liverange has been spilt at the block level
1224 and this one live beyond this block then spil this
1226 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1230 /* if trying to allocate this will cause
1231 a spill and there is nothing to spill
1232 or this one is rematerializable then
1234 willCS = willCauseSpill (sym->nRegs, sym->regType);
1235 spillable = computeSpillable (ic);
1236 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1241 /* if it has a spillocation & is used less than
1242 all other live ranges then spill this */
1244 if (sym->usl.spillLoc) {
1245 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1246 allLRs, ebbs[i], ic));
1247 if (leastUsed && leastUsed->used > sym->used) {
1252 /* if none of the liveRanges have a spillLocation then better
1253 to spill this one than anything else already assigned to registers */
1254 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1255 /* if this is local to this block then we might find a block spil */
1256 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1263 /* if we need ptr regs for the right side
1265 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1266 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1270 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1271 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1275 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1276 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1281 /* else we assign registers to it */
1282 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1283 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1285 for (j = 0; j < sym->nRegs; j++) {
1286 sym->regs[j] = NULL;
1287 if (sym->regType == REG_PTR)
1288 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1291 if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1293 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1296 sym->regs[j] = allocThisReg (right->regs[j]);
1298 if (!sym->regs[j]) */
1299 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1302 /* if the allocation failed which means
1303 this was spilt then break */
1306 for (i=0; i < sym->nRegs ; i++ )
1307 sym->regs[i] = NULL;
1312 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1313 /* if it shares registers with operands make sure
1314 that they are in the same position */
1315 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1316 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1317 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1318 OP_SYMBOL (IC_LEFT (ic)));
1320 /* do the same for the right operand */
1321 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1322 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1323 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1324 OP_SYMBOL (IC_RIGHT (ic)));
1337 /* Check for and fix any problems with uninitialized operands */
1338 for (i = 0; i < count; i++)
1342 if (ebbs[i]->noPath &&
1343 (ebbs[i]->entryLabel != entryLabel &&
1344 ebbs[i]->entryLabel != returnLabel))
1347 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1354 verifyRegsAssigned (IC_COND (ic), ic);
1358 if (ic->op == JUMPTABLE)
1360 verifyRegsAssigned (IC_JTCOND (ic), ic);
1364 verifyRegsAssigned (IC_RESULT (ic), ic);
1365 verifyRegsAssigned (IC_LEFT (ic), ic);
1366 verifyRegsAssigned (IC_RIGHT (ic), ic);
1371 /*-----------------------------------------------------------------*/
1372 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1373 /*-----------------------------------------------------------------*/
1374 static void fillGaps()
1381 if (getenv("DISABLE_FILL_GAPS")) return;
1383 /* look for livernages that was spilt by the allocator */
1384 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1385 sym = hTabNextItem(liveRanges,&key)) {
1390 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1392 /* find the liveRanges this one clashes with, that are
1393 still assigned to registers & mark the registers as used*/
1394 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1398 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1399 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1402 clr = hTabItemWithKey(liveRanges,i);
1405 /* mark these registers as used */
1406 for (k = 0 ; k < clr->nRegs ; k++ )
1407 useReg(clr->regs[k]);
1410 if (willCauseSpill(sym->nRegs,sym->regType)) {
1411 /* NOPE :( clear all registers & and continue */
1417 for (i = 0 ; i < sym->defs->size ; i++ )
1419 if (bitVectBitValue(sym->defs,i))
1421 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1428 D(printf("Atemping fillGaps on %s: [",sym->name));
1429 /* THERE IS HOPE !!!! */
1430 for (i=0; i < sym->nRegs ; i++ ) {
1431 if (sym->regType == REG_PTR)
1432 sym->regs[i] = getRegPtrNoSpil ();
1435 sym->regs[i] = NULL;
1436 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1438 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1441 sym->regs[i] = allocThisReg (right->regs[i]);
1444 sym->regs[i] = getRegGprNoSpil ();
1446 D(printf("%s ", sym->regs[i]->name));
1450 /* For all its definitions check if the registers
1451 allocated needs positioning NOTE: we can position
1452 only ONCE if more than One positioning required
1454 We may need to perform the checks twice; once to
1455 position the registers as needed, the second to
1456 verify any register repositioning is still
1460 for (pass=0; pass<2; pass++) {
1461 D(printf(" checking definitions\n"));
1462 for (i = 0 ; i < sym->defs->size ; i++ ) {
1463 if (bitVectBitValue(sym->defs,i)) {
1464 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1465 D(printf(" ic->seq = %d\n", ic->seq));
1466 if (SKIP_IC(ic)) continue;
1467 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1468 /* if left is assigned to registers */
1469 if (IS_SYMOP(IC_LEFT(ic)))
1471 D(printf(" left = "));
1472 D(printOperand(IC_LEFT(ic),NULL));
1474 if (IS_SYMOP(IC_LEFT(ic)) &&
1475 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1476 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1478 if (IS_SYMOP(IC_RIGHT(ic)))
1480 D(printf(" right = "));
1481 D(printOperand(IC_RIGHT(ic),NULL));
1483 if (IS_SYMOP(IC_RIGHT(ic)) &&
1484 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1485 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1487 D(printf(" pdone = %d\n", pdone));
1488 if (pdone > 1) break;
1491 D(printf(" checking uses\n"));
1492 for (i = 0 ; i < sym->uses->size ; i++ ) {
1493 if (bitVectBitValue(sym->uses,i)) {
1495 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1496 D(printf(" ic->seq = %d\n", ic->seq));
1497 if (SKIP_IC(ic)) continue;
1498 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1500 /* if result is assigned to registers */
1501 if (IS_SYMOP(IC_RESULT(ic)))
1503 D(printf(" result = "));
1504 D(printOperand(IC_RESULT(ic),NULL));
1506 if (IS_SYMOP(IC_RESULT(ic)) &&
1507 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1508 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1510 D(printf(" pdone = %d\n", pdone));
1511 if (pdone > 1) break;
1514 if (pdone == 0) break; /* second pass only if regs repositioned */
1515 if (pdone > 1) break;
1517 D(printf(" sym->regs = ["));
1518 for (i=0; i < sym->nRegs ; i++ )
1519 D(printf("%s ", sym->regs[i]->name));
1521 /* had to position more than once GIVE UP */
1523 /* UNDO all the changes we made to try this */
1525 for (i=0; i < sym->nRegs ; i++ ) {
1526 sym->regs[i] = NULL;
1529 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1532 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1534 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1535 sym->isspilt = sym->spillA = 0 ;
1536 sym->usl.spillLoc->allocreq--;
1541 /*-----------------------------------------------------------------*/
1542 /* rUmaskForOp :- returns register mask for an operand */
1543 /*-----------------------------------------------------------------*/
1545 mcs51_rUmaskForOp (operand * op)
1551 /* only temporaries are assigned registers */
1555 sym = OP_SYMBOL (op);
1557 /* if spilt or no registers assigned to it
1559 if (sym->isspilt || !sym->nRegs)
1562 rumask = newBitVect (mcs51_nRegs);
1564 for (j = 0; j < sym->nRegs; j++)
1566 if (sym->regs[j]) /* EEP - debug */
1567 rumask = bitVectSetBit (rumask,
1568 sym->regs[j]->rIdx);
1574 /*-----------------------------------------------------------------*/
1575 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1576 /*-----------------------------------------------------------------*/
1578 regsUsedIniCode (iCode * ic)
1580 bitVect *rmask = newBitVect (mcs51_nRegs);
1582 /* do the special cases first */
1585 rmask = bitVectUnion (rmask,
1586 mcs51_rUmaskForOp (IC_COND (ic)));
1590 /* for the jumptable */
1591 if (ic->op == JUMPTABLE)
1593 rmask = bitVectUnion (rmask,
1594 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1599 /* of all other cases */
1601 rmask = bitVectUnion (rmask,
1602 mcs51_rUmaskForOp (IC_LEFT (ic)));
1606 rmask = bitVectUnion (rmask,
1607 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1610 rmask = bitVectUnion (rmask,
1611 mcs51_rUmaskForOp (IC_RESULT (ic)));
1617 /*-----------------------------------------------------------------*/
1618 /* createRegMask - for each instruction will determine the regsUsed */
1619 /*-----------------------------------------------------------------*/
1621 createRegMask (eBBlock ** ebbs, int count)
1625 /* for all blocks */
1626 for (i = 0; i < count; i++)
1630 if (ebbs[i]->noPath &&
1631 (ebbs[i]->entryLabel != entryLabel &&
1632 ebbs[i]->entryLabel != returnLabel))
1635 /* for all instructions */
1636 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1641 if (SKIP_IC2 (ic) || !ic->rlive)
1644 /* first mark the registers used in this
1646 ic->rUsed = regsUsedIniCode (ic);
1647 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1649 /* now create the register mask for those
1650 registers that are in use : this is a
1651 super set of ic->rUsed */
1652 ic->rMask = newBitVect (mcs51_nRegs + 1);
1654 /* for all live Ranges alive at this point */
1655 for (j = 1; j < ic->rlive->size; j++)
1660 /* if not alive then continue */
1661 if (!bitVectBitValue (ic->rlive, j))
1664 /* find the live range we are interested in */
1665 if (!(sym = hTabItemWithKey (liveRanges, j)))
1667 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1668 "createRegMask cannot find live range");
1669 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1673 /* if no register assigned to it */
1674 if (!sym->nRegs || sym->isspilt)
1677 /* for all the registers allocated to it */
1678 for (k = 0; k < sym->nRegs; k++)
1681 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1687 /*-----------------------------------------------------------------*/
1688 /* rematStr - returns the rematerialized string for a remat var */
1689 /*-----------------------------------------------------------------*/
1691 rematStr (symbol * sym)
1694 iCode *ic = sym->rematiCode;
1699 /* if plus or minus print the right hand side */
1700 if (ic->op == '+' || ic->op == '-')
1702 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1705 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1709 /* cast then continue */
1710 if (IS_CAST_ICODE(ic)) {
1711 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1714 /* we reached the end */
1715 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1722 /*-----------------------------------------------------------------*/
1723 /* regTypeNum - computes the type & number of registers required */
1724 /*-----------------------------------------------------------------*/
1726 regTypeNum (eBBlock *ebbs)
1732 /* for each live range do */
1733 for (sym = hTabFirstItem (liveRanges, &k); sym;
1734 sym = hTabNextItem (liveRanges, &k))
1737 /* if used zero times then no registers needed */
1738 if ((sym->liveTo - sym->liveFrom) == 0)
1742 /* if the live range is a temporary */
1746 /* if the type is marked as a conditional */
1747 if (sym->regType == REG_CND)
1750 /* if used in return only then we don't
1752 if (sym->ruonly || sym->accuse)
1754 if (IS_AGGREGATE (sym->type) || sym->isptr)
1755 sym->type = aggrToPtr (sym->type, FALSE);
1759 /* if the symbol has only one definition &
1760 that definition is a get_pointer */
1761 if (bitVectnBitsOn (sym->defs) == 1 &&
1762 (ic = hTabItemWithKey (iCodehTab,
1763 bitVectFirstBit (sym->defs))) &&
1766 !IS_BITVAR (sym->etype))
1770 /* and that pointer is remat in data space */
1771 if (IS_SYMOP (IC_LEFT (ic)) &&
1772 OP_SYMBOL (IC_LEFT (ic))->remat &&
1773 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1774 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1776 /* create a psuedo symbol & force a spil */
1777 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1778 psym->type = sym->type;
1779 psym->etype = sym->etype;
1781 strcpy (psym->rname, psym->name);
1783 sym->usl.spillLoc = psym;
1784 #if 0 // an alternative fix for bug #480076
1785 /* now this is a useless assignment to itself */
1786 remiCodeFromeBBlock (ebbs, ic);
1788 /* now this really is an assignment to itself, make it so;
1789 it will be optimized out later */
1791 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), IC_RESULT(ic));
1797 /* if in data space or idata space then try to
1798 allocate pointer register */
1802 /* if not then we require registers */
1803 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1804 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1805 getSize (sym->type));
1809 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1810 printTypeChain (sym->type, stderr);
1811 fprintf (stderr, "\n");
1814 /* determine the type of register required */
1815 if (sym->nRegs == 1 &&
1816 IS_PTR (sym->type) &&
1818 sym->regType = REG_PTR;
1820 sym->regType = REG_GPR;
1824 /* for the first run we don't provide */
1825 /* registers for true symbols we will */
1826 /* see how things go */
1832 /*-----------------------------------------------------------------*/
1833 /* freeAllRegs - mark all registers as free */
1834 /*-----------------------------------------------------------------*/
1840 for (i = 0; i < mcs51_nRegs; i++)
1841 regs8051[i].isFree = 1;
1844 /*-----------------------------------------------------------------*/
1845 /* deallocStackSpil - this will set the stack pointer back */
1846 /*-----------------------------------------------------------------*/
1848 DEFSETFUNC (deallocStackSpil)
1856 /*-----------------------------------------------------------------*/
1857 /* farSpacePackable - returns the packable icode for far variables */
1858 /*-----------------------------------------------------------------*/
1860 farSpacePackable (iCode * ic)
1864 /* go thru till we find a definition for the
1865 symbol on the right */
1866 for (dic = ic->prev; dic; dic = dic->prev)
1868 /* if the definition is a call then no */
1869 if ((dic->op == CALL || dic->op == PCALL) &&
1870 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1875 /* if shift by unknown amount then not */
1876 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1877 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1880 /* if pointer get and size > 1 */
1881 if (POINTER_GET (dic) &&
1882 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1885 if (POINTER_SET (dic) &&
1886 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1891 if (IC_COND (dic) &&
1892 IS_TRUE_SYMOP (IC_COND (dic)) &&
1893 isOperandInFarSpace (IC_COND (dic)))
1896 else if (dic->op == JUMPTABLE)
1898 if (IC_JTCOND (dic) &&
1899 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1900 isOperandInFarSpace (IC_JTCOND (dic)))
1905 /* if any three is a true symbol in far space */
1906 if (IC_RESULT (dic) &&
1907 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1908 isOperandInFarSpace (IC_RESULT (dic)))
1911 if (IC_RIGHT (dic) &&
1912 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1913 isOperandInFarSpace (IC_RIGHT (dic)) &&
1914 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1917 if (IC_LEFT (dic) &&
1918 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1919 isOperandInFarSpace (IC_LEFT (dic)) &&
1920 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1924 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1926 if ((dic->op == LEFT_OP ||
1927 dic->op == RIGHT_OP ||
1929 IS_OP_LITERAL (IC_RIGHT (dic)))
1939 /*-----------------------------------------------------------------*/
1940 /* packRegsForAssign - register reduction for assignment */
1941 /*-----------------------------------------------------------------*/
1943 packRegsForAssign (iCode * ic, eBBlock * ebp)
1947 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1948 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1949 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1954 /* if the true symbol is defined in far space or on stack
1955 then we should not since this will increase register pressure */
1956 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1960 /* find the definition of iTempNN scanning backwards if we find a
1961 a use of the true symbol in before we find the definition then
1963 for (dic = ic->prev; dic; dic = dic->prev)
1965 int crossedCall = 0;
1967 /* We can pack across a function call only if it's a local */
1968 /* variable or our parameter. Never pack global variables */
1969 /* or parameters to a function we call. */
1970 if ((dic->op == CALL || dic->op == PCALL))
1972 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
1973 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
1984 if (IS_SYMOP (IC_COND (dic)) &&
1985 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1986 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1994 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1995 IS_OP_VOLATILE (IC_RESULT (dic)))
2001 if (IS_SYMOP (IC_RESULT (dic)) &&
2002 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2004 if (POINTER_SET (dic))
2010 if (IS_SYMOP (IC_RIGHT (dic)) &&
2011 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2012 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2018 if (IS_SYMOP (IC_LEFT (dic)) &&
2019 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2020 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2026 if (IS_SYMOP (IC_RESULT (dic)) &&
2027 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2043 return 0; /* did not find */
2045 /* if assignment then check that right is not a bit */
2046 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2048 sym_link *etype = operandType (IC_RESULT (dic));
2049 if (IS_BITFIELD (etype))
2051 /* if result is a bit too then it's ok */
2052 etype = operandType (IC_RESULT (ic));
2053 if (!IS_BITFIELD (etype))
2060 /* if assignment then check that right is not a bit */
2061 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2063 sym_link *etype = operandType (IC_RIGHT (dic));
2064 if (IS_BITFIELD (etype))
2066 /* if result is a bit too then it's ok */
2067 etype = operandType (IC_RESULT (dic));
2068 if (!IS_BITFIELD (etype))
2073 /* if the result is on stack or iaccess then it must be
2074 the same atleast one of the operands */
2075 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2076 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2079 /* the operation has only one symbol
2080 operator then we can pack */
2081 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2082 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2085 if (!((IC_LEFT (dic) &&
2086 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2088 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2092 /* found the definition */
2093 /* replace the result with the result of */
2094 /* this assignment and remove this assignment */
2095 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2096 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2098 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2100 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2102 // TODO: and the otherway around?
2104 /* delete from liverange table also
2105 delete from all the points inbetween and the new
2107 for (sic = dic; sic != ic; sic = sic->next)
2109 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2110 if (IS_ITEMP (IC_RESULT (dic)))
2111 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2114 remiCodeFromeBBlock (ebp, ic);
2115 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2116 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2117 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2121 /*------------------------------------------------------------------*/
2122 /* findAssignToSym : scanning backwards looks for first assig found */
2123 /*------------------------------------------------------------------*/
2125 findAssignToSym (operand * op, iCode * ic)
2129 /* This routine is used to find sequences like
2131 ...; (intervening ops don't use iTempAA or modify FOO)
2132 blah = blah + iTempAA;
2134 and eliminate the use of iTempAA, freeing up its register for
2138 for (dic = ic->prev; dic; dic = dic->prev)
2141 /* if definition by assignment */
2142 if (dic->op == '=' &&
2143 !POINTER_SET (dic) &&
2144 IC_RESULT (dic)->key == op->key
2145 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2147 break; /* found where this temp was defined */
2149 /* if we find an usage then we cannot delete it */
2153 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2156 else if (dic->op == JUMPTABLE)
2158 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2163 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2166 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2169 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2175 return NULL; /* didn't find any assignment to op */
2177 /* we are interested only if defined in far space */
2178 /* or in stack space in case of + & - */
2180 /* if assigned to a non-symbol then don't repack regs */
2181 if (!IS_SYMOP (IC_RIGHT (dic)))
2184 /* if the symbol is volatile then we should not */
2185 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2187 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2188 What does it mean for an iTemp to be volatile, anyway? Passing
2189 TRUE is more cautious but may prevent possible optimizations */
2191 /* if the symbol is in far space then we should not */
2192 if (isOperandInFarSpace (IC_RIGHT (dic)))
2195 /* for + & - operations make sure that
2196 if it is on the stack it is the same
2197 as one of the three operands */
2198 if ((ic->op == '+' || ic->op == '-') &&
2199 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2202 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2203 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2204 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2208 /* now make sure that the right side of dic
2209 is not defined between ic & dic */
2212 iCode *sic = dic->next;
2214 for (; sic != ic; sic = sic->next)
2215 if (IC_RESULT (sic) &&
2216 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2223 /*-----------------------------------------------------------------*/
2224 /* reassignAliasedSym - used by packRegsForSupport to replace */
2225 /* redundant iTemp with equivalent symbol */
2226 /*-----------------------------------------------------------------*/
2228 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2231 unsigned oldSymKey, newSymKey;
2233 oldSymKey = op->key;
2234 newSymKey = IC_RIGHT(assignment)->key;
2236 /* only track live ranges of compiler-generated temporaries */
2237 if (!IS_ITEMP(IC_RIGHT(assignment)))
2240 /* update the live-value bitmaps */
2241 for (ic = assignment; ic != use; ic = ic->next) {
2242 bitVectUnSetBit (ic->rlive, oldSymKey);
2244 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2247 /* update the sym of the used operand */
2248 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2249 op->key = OP_SYMBOL(op)->key;
2250 OP_SYMBOL(op)->accuse = 0;
2252 /* update the sym's liverange */
2253 if ( OP_LIVETO(op) < ic->seq )
2254 setToRange(op, ic->seq, FALSE);
2256 /* remove the assignment iCode now that its result is unused */
2257 remiCodeFromeBBlock (ebp, assignment);
2258 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2259 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2263 /*-----------------------------------------------------------------*/
2264 /* packRegsForSupport :- reduce some registers for support calls */
2265 /*-----------------------------------------------------------------*/
2267 packRegsForSupport (iCode * ic, eBBlock * ebp)
2271 /* for the left & right operand :- look to see if the
2272 left was assigned a true symbol in far space in that
2273 case replace them */
2275 if (IS_ITEMP (IC_LEFT (ic)) &&
2276 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2278 dic = findAssignToSym (IC_LEFT (ic), ic);
2282 /* found it we need to remove it from the block */
2283 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2288 /* do the same for the right operand */
2289 if (IS_ITEMP (IC_RIGHT (ic)) &&
2290 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2292 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2296 /* if this is a subtraction & the result
2297 is a true symbol in far space then don't pack */
2298 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2300 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2301 if (IN_FARSPACE (SPEC_OCLS (etype)))
2304 /* found it we need to remove it from the
2306 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2315 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2318 /*-----------------------------------------------------------------*/
2319 /* packRegsForOneuse : - will reduce some registers for single Use */
2320 /*-----------------------------------------------------------------*/
2322 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2327 /* if returning a literal then do nothing */
2331 /* only upto 2 bytes since we cannot predict
2332 the usage of b, & acc */
2333 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2336 if (ic->op != RETURN &&
2338 !POINTER_SET (ic) &&
2342 if (ic->op == SEND && ic->argreg != 1) return NULL;
2344 /* this routine will mark the a symbol as used in one
2345 instruction use only && if the defintion is local
2346 (ie. within the basic block) && has only one definition &&
2347 that definiion is either a return value from a
2348 function or does not contain any variables in
2350 uses = bitVectCopy (OP_USES (op));
2351 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2352 if (!bitVectIsZero (uses)) /* has other uses */
2355 /* if it has only one defintion */
2356 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2357 return NULL; /* has more than one definition */
2359 /* get that definition */
2361 hTabItemWithKey (iCodehTab,
2362 bitVectFirstBit (OP_DEFS (op)))))
2365 /* if that only usage is a cast */
2366 if (dic->op == CAST) {
2367 /* to a bigger type */
2368 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2369 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2370 /* than we can not, since we cannot predict the usage of b & acc */
2375 /* found the definition now check if it is local */
2376 if (dic->seq < ebp->fSeq ||
2377 dic->seq > ebp->lSeq)
2378 return NULL; /* non-local */
2380 /* now check if it is the return from
2382 if (dic->op == CALL || dic->op == PCALL)
2384 if (ic->op != SEND && ic->op != RETURN &&
2385 !POINTER_SET(ic) && !POINTER_GET(ic))
2387 OP_SYMBOL (op)->ruonly = 1;
2394 /* otherwise check that the definition does
2395 not contain any symbols in far space */
2396 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2397 isOperandInFarSpace (IC_RIGHT (dic)) ||
2398 IS_OP_RUONLY (IC_LEFT (ic)) ||
2399 IS_OP_RUONLY (IC_RIGHT (ic)))
2404 /* if pointer set then make sure the pointer
2406 if (POINTER_SET (dic) &&
2407 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2410 if (POINTER_GET (dic) &&
2411 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2416 /* also make sure the intervenening instructions
2417 don't have any thing in far space */
2418 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2421 /* if there is an intervening function call then no */
2422 if (dic->op == CALL || dic->op == PCALL)
2424 /* if pointer set then make sure the pointer
2426 if (POINTER_SET (dic) &&
2427 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2430 if (POINTER_GET (dic) &&
2431 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2434 /* if address of & the result is remat the okay */
2435 if (dic->op == ADDRESS_OF &&
2436 OP_SYMBOL (IC_RESULT (dic))->remat)
2439 /* if operand has size of three or more & this
2440 operation is a '*','/' or '%' then 'b' may
2442 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2443 getSize (operandType (op)) >= 3)
2446 /* if left or right or result is in far space */
2447 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2448 isOperandInFarSpace (IC_RIGHT (dic)) ||
2449 isOperandInFarSpace (IC_RESULT (dic)) ||
2450 IS_OP_RUONLY (IC_LEFT (dic)) ||
2451 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2452 IS_OP_RUONLY (IC_RESULT (dic)))
2456 /* if left or right or result is on stack */
2457 if (isOperandOnStack(IC_LEFT(dic)) ||
2458 isOperandOnStack(IC_RIGHT(dic)) ||
2459 isOperandOnStack(IC_RESULT(dic))) {
2464 OP_SYMBOL (op)->ruonly = 1;
2468 /*-----------------------------------------------------------------*/
2469 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2470 /*-----------------------------------------------------------------*/
2472 isBitwiseOptimizable (iCode * ic)
2474 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2475 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2477 /* bitwise operations are considered optimizable
2478 under the following conditions (Jean-Louis VERN)
2490 if (IS_LITERAL(rtype) ||
2491 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2497 /*-----------------------------------------------------------------*/
2498 /* isCommutativeOp - tests whether this op cares what order its */
2499 /* operands are in */
2500 /*-----------------------------------------------------------------*/
2501 bool isCommutativeOp(unsigned int op)
2503 if (op == '+' || op == '*' || op == EQ_OP ||
2504 op == '^' || op == '|' || op == BITWISEAND)
2510 /*-----------------------------------------------------------------*/
2511 /* operandUsesAcc - determines whether the code generated for this */
2512 /* operand will have to use the accumulator */
2513 /*-----------------------------------------------------------------*/
2514 bool operandUsesAcc(operand *op)
2520 symbol *sym = OP_SYMBOL(op);
2524 return TRUE; /* duh! */
2526 if (IN_STACK(sym->etype) || sym->onStack ||
2527 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2528 return TRUE; /* acc is used to calc stack offset */
2533 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2535 return FALSE; /* more checks? */
2539 symspace = SPEC_OCLS(sym->etype);
2541 if (sym->iaccess && symspace->paged)
2542 return TRUE; /* must fetch paged indirect sym via accumulator */
2544 if (IN_BITSPACE(symspace))
2545 return TRUE; /* fetching bit vars uses the accumulator */
2547 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2548 return TRUE; /* fetched via accumulator and dptr */
2554 /*-----------------------------------------------------------------*/
2555 /* packRegsForAccUse - pack registers for acc use */
2556 /*-----------------------------------------------------------------*/
2558 packRegsForAccUse (iCode * ic)
2562 /* if this is an aggregate, e.g. a one byte char array */
2563 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2567 /* if we are calling a reentrant function that has stack parameters */
2568 if (ic->op == CALL &&
2569 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2570 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2573 if (ic->op == PCALL &&
2574 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2575 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2578 /* if + or - then it has to be one byte result */
2579 if ((ic->op == '+' || ic->op == '-')
2580 && getSize (operandType (IC_RESULT (ic))) > 1)
2583 /* if shift operation make sure right side is not a literal */
2584 if (ic->op == RIGHT_OP &&
2585 (isOperandLiteral (IC_RIGHT (ic)) ||
2586 getSize (operandType (IC_RESULT (ic))) > 1))
2589 if (ic->op == LEFT_OP &&
2590 (isOperandLiteral (IC_RIGHT (ic)) ||
2591 getSize (operandType (IC_RESULT (ic))) > 1))
2594 if (IS_BITWISE_OP (ic) &&
2595 getSize (operandType (IC_RESULT (ic))) > 1)
2599 /* has only one definition */
2600 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2603 /* has only one use */
2604 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2607 /* and the usage immediately follows this iCode */
2608 if (!(uic = hTabItemWithKey (iCodehTab,
2609 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2612 if (ic->next != uic)
2615 /* if it is a conditional branch then we definitely can */
2619 if (uic->op == JUMPTABLE)
2622 if (POINTER_SET (uic) &&
2623 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2626 /* if the usage is not is an assignment
2627 or an arithmetic / bitwise / shift operation then not */
2628 if (uic->op != '=' &&
2629 !IS_ARITHMETIC_OP (uic) &&
2630 !IS_BITWISE_OP (uic) &&
2631 uic->op != LEFT_OP &&
2632 uic->op != RIGHT_OP)
2635 /* if used in ^ operation then make sure right is not a
2636 literal (WIML: Why is this?) */
2637 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2640 /* if shift operation make sure right side is not a literal */
2641 /* WIML: Why is this? */
2642 if (uic->op == RIGHT_OP &&
2643 (isOperandLiteral (IC_RIGHT (uic)) ||
2644 getSize (operandType (IC_RESULT (uic))) > 1))
2646 if (uic->op == LEFT_OP &&
2647 (isOperandLiteral (IC_RIGHT (uic)) ||
2648 getSize (operandType (IC_RESULT (uic))) > 1))
2651 /* make sure that the result of this icode is not on the
2652 stack, since acc is used to compute stack offset */
2654 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2655 OP_SYMBOL (IC_RESULT (uic))->onStack)
2658 if (isOperandOnStack(IC_RESULT(uic)))
2662 /* if the usage has only one operand then we can */
2663 if (IC_LEFT (uic) == NULL ||
2664 IC_RIGHT (uic) == NULL)
2667 /* if the other operand uses the accumulator then we cannot */
2668 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2669 operandUsesAcc(IC_RIGHT(uic))) ||
2670 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2671 operandUsesAcc(IC_LEFT(uic))) )
2674 /* make sure this is on the left side if not commutative */
2675 /* except for '-', which has been written to be able to
2676 handle reversed operands */
2677 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2678 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2682 // this is too dangerous and need further restrictions
2685 /* if one of them is a literal then we can */
2686 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2687 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2689 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2695 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2699 /*-----------------------------------------------------------------*/
2700 /* packForPush - hueristics to reduce iCode for pushing */
2701 /*-----------------------------------------------------------------*/
2703 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2707 struct eBBlock * ebp=ebpp[blockno];
2709 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2712 /* must have only definition & one usage */
2713 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2714 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2717 /* find the definition */
2718 if (!(dic = hTabItemWithKey (iCodehTab,
2719 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2722 if (dic->op != '=' || POINTER_SET (dic))
2725 if (dic->seq < ebp->fSeq) { // Evelyn did this
2727 for (i=0; i<blockno; i++) {
2728 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2733 wassert (i!=blockno); // no way to recover from here
2736 if (IS_SYMOP(IC_RIGHT(dic))) {
2737 /* make sure the right side does not have any definitions
2739 dbv = OP_DEFS(IC_RIGHT(dic));
2740 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2741 if (bitVectBitValue(dbv,lic->key))
2744 /* make sure they have the same type */
2745 if (IS_SPEC(operandType(IC_LEFT(ic))))
2747 sym_link *itype=operandType(IC_LEFT(ic));
2748 sym_link *ditype=operandType(IC_RIGHT(dic));
2750 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2751 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2754 /* extend the live range of replaced operand if needed */
2755 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2756 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2758 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2761 /* we now we know that it has one & only one def & use
2762 and the that the definition is an assignment */
2763 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2764 remiCodeFromeBBlock (ebp, dic);
2765 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2768 /*-----------------------------------------------------------------*/
2769 /* packRegisters - does some transformations to reduce register */
2771 /*-----------------------------------------------------------------*/
2773 packRegisters (eBBlock ** ebpp, int blockno)
2777 eBBlock *ebp=ebpp[blockno];
2784 /* look for assignments of the form */
2785 /* iTempNN = TRueSym (someoperation) SomeOperand */
2787 /* TrueSym := iTempNN:1 */
2788 for (ic = ebp->sch; ic; ic = ic->next)
2790 /* find assignment of the form TrueSym := iTempNN:1 */
2791 if (ic->op == '=' && !POINTER_SET (ic))
2792 change += packRegsForAssign (ic, ebp);
2799 for (ic = ebp->sch; ic; ic = ic->next)
2801 /* if this is an itemp & result of an address of a true sym
2802 then mark this as rematerialisable */
2803 if (ic->op == ADDRESS_OF &&
2804 IS_ITEMP (IC_RESULT (ic)) &&
2805 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2806 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2807 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2810 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2811 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2812 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2816 /* if straight assignment then carry remat flag if
2817 this is the only definition */
2818 if (ic->op == '=' &&
2819 !POINTER_SET (ic) &&
2820 IS_SYMOP (IC_RIGHT (ic)) &&
2821 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2822 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2823 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2826 OP_SYMBOL (IC_RESULT (ic))->remat =
2827 OP_SYMBOL (IC_RIGHT (ic))->remat;
2828 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2829 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2832 /* if cast to a generic pointer & the pointer being
2833 cast is remat, then we can remat this cast as well */
2834 if (ic->op == CAST &&
2835 IS_SYMOP(IC_RIGHT(ic)) &&
2836 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2837 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1) {
2838 sym_link *to_type = operandType(IC_LEFT(ic));
2839 sym_link *from_type = operandType(IC_RIGHT(ic));
2840 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2841 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2842 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2843 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2847 /* if this is a +/- operation with a rematerizable
2848 then mark this as rematerializable as well */
2849 if ((ic->op == '+' || ic->op == '-') &&
2850 (IS_SYMOP (IC_LEFT (ic)) &&
2851 IS_ITEMP (IC_RESULT (ic)) &&
2852 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2853 OP_SYMBOL (IC_LEFT (ic))->remat &&
2854 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2855 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2857 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2858 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2859 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2862 /* mark the pointer usages */
2863 if (POINTER_SET (ic))
2864 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2866 if (POINTER_GET (ic) &&
2867 IS_SYMOP(IC_LEFT (ic)))
2868 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2872 /* if we are using a symbol on the stack
2873 then we should say mcs51_ptrRegReq */
2874 if (options.useXstack && ic->parmPush
2875 && (ic->op == IPUSH || ic->op == IPOP))
2877 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2878 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2879 OP_SYMBOL (IC_COND (ic))->iaccess ||
2880 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
2881 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2882 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2883 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
2884 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
2887 if (IS_SYMOP (IC_LEFT (ic)))
2888 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2889 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
2890 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
2891 if (IS_SYMOP (IC_RIGHT (ic)))
2892 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2893 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
2894 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
2895 if (IS_SYMOP (IC_RESULT (ic)))
2896 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2897 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
2898 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
2899 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
2900 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
2902 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
2903 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
2908 /* if the condition of an if instruction
2909 is defined in the previous instruction and
2910 this is the only usage then
2911 mark the itemp as a conditional */
2912 if ((IS_CONDITIONAL (ic) ||
2913 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2914 ic->next && ic->next->op == IFX &&
2915 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2916 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2917 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2919 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2923 /* reduce for support function calls */
2924 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2925 packRegsForSupport (ic, ebp);
2927 /* some cases the redundant moves can
2928 can be eliminated for return statements */
2929 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2930 !isOperandInFarSpace (IC_LEFT (ic)) &&
2931 options.model == MODEL_SMALL) {
2932 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2935 /* if pointer set & left has a size more than
2936 one and right is not in far space */
2937 if (POINTER_SET (ic) &&
2938 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2939 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2940 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2941 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2942 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2944 /* if pointer get */
2945 if (POINTER_GET (ic) &&
2946 IS_SYMOP (IC_LEFT (ic)) &&
2947 !isOperandInFarSpace (IC_RESULT (ic)) &&
2948 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2949 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2950 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2951 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2954 /* if this is cast for intergral promotion then
2955 check if only use of the definition of the
2956 operand being casted/ if yes then replace
2957 the result of that arithmetic operation with
2958 this result and get rid of the cast */
2961 sym_link *fromType = operandType (IC_RIGHT (ic));
2962 sym_link *toType = operandType (IC_LEFT (ic));
2964 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2965 getSize (fromType) != getSize (toType) &&
2966 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2969 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2972 if (IS_ARITHMETIC_OP (dic))
2974 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2975 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2976 remiCodeFromeBBlock (ebp, ic);
2977 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2978 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2979 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2983 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2989 /* if the type from and type to are the same
2990 then if this is the only use then packit */
2991 if (compareType (operandType (IC_RIGHT (ic)),
2992 operandType (IC_LEFT (ic))) == 1)
2994 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2997 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2998 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2999 remiCodeFromeBBlock (ebp, ic);
3000 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3001 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3002 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3010 iTempNN := (some variable in farspace) V1
3015 if (ic->op == IPUSH)
3017 packForPush (ic, ebpp, blockno);
3021 /* pack registers for accumulator use, when the
3022 result of an arithmetic or bit wise operation
3023 has only one use, that use is immediately following
3024 the defintion and the using iCode has only one
3025 operand or has two operands but one is literal &
3026 the result of that operation is not on stack then
3027 we can leave the result of this operation in acc:b
3029 if ((IS_ARITHMETIC_OP (ic)
3030 || IS_CONDITIONAL(ic)
3031 || IS_BITWISE_OP (ic)
3032 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3033 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3035 IS_ITEMP (IC_RESULT (ic)) &&
3036 getSize (operandType (IC_RESULT (ic))) <= 2)
3038 packRegsForAccUse (ic);
3042 /*-----------------------------------------------------------------*/
3043 /* assignRegisters - assigns registers to each live range as need */
3044 /*-----------------------------------------------------------------*/
3046 mcs51_assignRegisters (eBBlock ** ebbs, int count)
3051 setToNull ((void *) &_G.funcrUsed);
3052 setToNull ((void *) &_G.regAssigned);
3053 setToNull ((void *) &_G.totRegAssigned);
3054 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3057 /* change assignments this will remove some
3058 live ranges reducing some register pressure */
3060 for (i = 0; i < count; i++)
3061 packRegisters (ebbs, i);
3063 /* liveranges probably changed by register packing
3064 so we compute them again */
3065 recomputeLiveRanges (ebbs, count);
3067 if (options.dump_pack)
3068 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3070 /* first determine for each live range the number of
3071 registers & the type of registers required for each */
3074 /* and serially allocate registers */
3075 serialRegAssign (ebbs, count);
3078 //setToNull ((void *) &_G.regAssigned);
3079 //setToNull ((void *) &_G.totRegAssigned);
3082 /* if stack was extended then tell the user */
3085 /* werror(W_TOOMANY_SPILS,"stack", */
3086 /* _G.stackExtend,currFunc->name,""); */
3092 /* werror(W_TOOMANY_SPILS,"data space", */
3093 /* _G.dataExtend,currFunc->name,""); */
3097 /* after that create the register mask
3098 for each of the instruction */
3099 createRegMask (ebbs, count);
3101 /* redo that offsets for stacked automatic variables */
3103 redoStackOffsets ();
3106 /* make sure r0 & r1 are flagged as used if they might be used */
3108 if (currFunc && mcs51_ptrRegReq)
3110 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3111 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3114 if (options.dump_rassgn)
3116 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3117 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3120 /* do the overlaysegment stuff SDCCmem.c */
3121 doOverlays (ebbs, count);
3123 /* now get back the chain */
3124 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3128 /* free up any _G.stackSpil locations allocated */
3129 applyToSet (_G.stackSpil, deallocStackSpil);
3131 setToNull ((void *) &_G.stackSpil);
3132 setToNull ((void *) &_G.spiltSet);
3133 /* mark all registers as free */