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)
788 /* try for a ptr type */
789 if ((reg = allocReg (REG_PTR)))
792 /* try for gpr type */
793 if ((reg = allocReg (REG_GPR)))
796 /* we have to spil */
797 if (!spilSomething (ic, ebp, sym))
800 /* make sure partially assigned registers aren't reused */
801 for (j=0; j<=sym->nRegs; j++)
803 sym->regs[j]->isFree = 0;
805 /* this looks like an infinite loop but
806 in really selectSpil will abort */
810 /*-----------------------------------------------------------------*/
811 /* getRegGpr - will try for GPR if not spil */
812 /*-----------------------------------------------------------------*/
814 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
820 /* try for gpr type */
821 if ((reg = allocReg (REG_GPR)))
824 if (!mcs51_ptrRegReq)
825 if ((reg = allocReg (REG_PTR)))
828 /* we have to spil */
829 if (!spilSomething (ic, ebp, sym))
832 /* make sure partially assigned registers aren't reused */
833 for (j=0; j<=sym->nRegs; j++)
835 sym->regs[j]->isFree = 0;
837 /* this looks like an infinite loop but
838 in really selectSpil will abort */
842 /*-----------------------------------------------------------------*/
843 /* getRegPtrNoSpil - get it cannot split */
844 /*-----------------------------------------------------------------*/
845 static regs *getRegPtrNoSpil()
849 /* try for a ptr type */
850 if ((reg = allocReg (REG_PTR)))
853 /* try for gpr type */
854 if ((reg = allocReg (REG_GPR)))
859 /* just to make the compiler happy */
863 /*-----------------------------------------------------------------*/
864 /* getRegGprNoSpil - get it cannot split */
865 /*-----------------------------------------------------------------*/
866 static regs *getRegGprNoSpil()
870 if ((reg = allocReg (REG_GPR)))
873 if (!mcs51_ptrRegReq)
874 if ((reg = allocReg (REG_PTR)))
879 /* just to make the compiler happy */
883 /*-----------------------------------------------------------------*/
884 /* symHasReg - symbol has a given register */
885 /*-----------------------------------------------------------------*/
887 symHasReg (symbol * sym, regs * reg)
891 for (i = 0; i < sym->nRegs; i++)
892 if (sym->regs[i] == reg)
898 /*-----------------------------------------------------------------*/
899 /* deassignLRs - check the live to and if they have registers & are */
900 /* not spilt then free up the registers */
901 /*-----------------------------------------------------------------*/
903 deassignLRs (iCode * ic, eBBlock * ebp)
909 for (sym = hTabFirstItem (liveRanges, &k); sym;
910 sym = hTabNextItem (liveRanges, &k))
914 /* if it does not end here */
915 if (sym->liveTo > ic->seq)
918 /* if it was spilt on stack then we can
919 mark the stack spil location as free */
924 sym->usl.spillLoc->isFree = 1;
930 if (!bitVectBitValue (_G.regAssigned, sym->key))
933 /* special case check if this is an IFX &
934 the privious one was a pop and the
935 previous one was not spilt then keep track
937 if (ic->op == IFX && ic->prev &&
938 ic->prev->op == IPOP &&
939 !ic->prev->parmPush &&
940 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
941 psym = OP_SYMBOL (IC_LEFT (ic->prev));
947 bitVectUnSetBit (_G.regAssigned, sym->key);
949 /* if the result of this one needs registers
950 and does not have it then assign it right
952 if (IC_RESULT (ic) &&
953 !(SKIP_IC2 (ic) || /* not a special icode */
954 ic->op == JUMPTABLE ||
960 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
961 result->liveTo > ic->seq && /* and will live beyond this */
962 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
963 result->regType == sym->regType && /* same register types */
964 result->nRegs && /* which needs registers */
965 !result->isspilt && /* and does not already have them */
967 !bitVectBitValue (_G.regAssigned, result->key) &&
968 /* the number of free regs + number of regs in this LR
969 can accomodate the what result Needs */
970 ((nfreeRegsType (result->regType) +
971 sym->nRegs) >= result->nRegs)
975 for (i = 0; i < result->nRegs; i++)
977 result->regs[i] = sym->regs[i];
979 result->regs[i] = getRegGpr (ic, ebp, result);
981 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
982 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
986 /* free the remaining */
987 for (; i < sym->nRegs; i++)
991 if (!symHasReg (psym, sym->regs[i]))
992 freeReg (sym->regs[i]);
995 freeReg (sym->regs[i]);
1002 /*-----------------------------------------------------------------*/
1003 /* reassignLR - reassign this to registers */
1004 /*-----------------------------------------------------------------*/
1006 reassignLR (operand * op)
1008 symbol *sym = OP_SYMBOL (op);
1011 /* not spilt any more */
1012 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1013 bitVectUnSetBit (_G.spiltSet, sym->key);
1015 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1016 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1020 for (i = 0; i < sym->nRegs; i++)
1021 sym->regs[i]->isFree = 0;
1024 /*-----------------------------------------------------------------*/
1025 /* willCauseSpill - determines if allocating will cause a spill */
1026 /*-----------------------------------------------------------------*/
1028 willCauseSpill (int nr, int rt)
1030 /* first check if there are any avlb registers
1031 of te type required */
1034 /* special case for pointer type
1035 if pointer type not avlb then
1036 check for type gpr */
1037 if (nFreeRegs (rt) >= nr)
1039 if (nFreeRegs (REG_GPR) >= nr)
1044 if (mcs51_ptrRegReq)
1046 if (nFreeRegs (rt) >= nr)
1051 if (nFreeRegs (REG_PTR) +
1052 nFreeRegs (REG_GPR) >= nr)
1057 /* it will cause a spil */
1061 /*-----------------------------------------------------------------*/
1062 /* positionRegs - the allocator can allocate same registers to res- */
1063 /* ult and operand, if this happens make sure they are in the same */
1064 /* position as the operand otherwise chaos results */
1065 /*-----------------------------------------------------------------*/
1067 positionRegs (symbol * result, symbol * opsym)
1069 int count = min (result->nRegs, opsym->nRegs);
1070 int i, j = 0, shared = 0;
1073 /* if the result has been spilt then cannot share */
1078 /* first make sure that they actually share */
1079 for (i = 0; i < count; i++)
1081 for (j = 0; j < count; j++)
1083 if (result->regs[i] == opsym->regs[j] && i != j)
1093 regs *tmp = result->regs[i];
1094 result->regs[i] = result->regs[j];
1095 result->regs[j] = tmp;
1103 /*------------------------------------------------------------------*/
1104 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1105 /* it should either have registers or have beed spilled. Otherwise, */
1106 /* there was an uninitialized variable, so just spill this to get */
1107 /* the operand in a valid state. */
1108 /*------------------------------------------------------------------*/
1110 verifyRegsAssigned (operand *op, iCode * ic)
1115 if (!IS_ITEMP (op)) return;
1117 sym = OP_SYMBOL (op);
1118 if (sym->isspilt) return;
1119 if (!sym->nRegs) return;
1120 if (sym->regs[0]) return;
1122 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1123 sym->prereqv ? sym->prereqv->name : sym->name);
1129 /*-----------------------------------------------------------------*/
1130 /* serialRegAssign - serially allocate registers to the variables */
1131 /*-----------------------------------------------------------------*/
1133 serialRegAssign (eBBlock ** ebbs, int count)
1137 /* for all blocks */
1138 for (i = 0; i < count; i++) {
1142 if (ebbs[i]->noPath &&
1143 (ebbs[i]->entryLabel != entryLabel &&
1144 ebbs[i]->entryLabel != returnLabel))
1147 /* of all instructions do */
1148 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1152 // update the registers in use at the start of this icode
1153 for (reg=0; reg<mcs51_nRegs; reg++) {
1154 if (regs8051[reg].isFree) {
1155 ic->riu &= ~(1<<regs8051[reg].offset);
1157 ic->riu |= (1<<regs8051[reg].offset);
1162 /* if this is an ipop that means some live
1163 range will have to be assigned again */
1165 reassignLR (IC_LEFT (ic));
1167 /* if result is present && is a true symbol */
1168 if (IC_RESULT (ic) && ic->op != IFX &&
1169 IS_TRUE_SYMOP (IC_RESULT (ic)))
1170 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1172 /* take away registers from live
1173 ranges that end at this instruction */
1174 deassignLRs (ic, ebbs[i]);
1176 /* some don't need registers */
1177 if (SKIP_IC2 (ic) ||
1178 ic->op == JUMPTABLE ||
1182 (IC_RESULT (ic) && POINTER_SET (ic)))
1185 /* now we need to allocate registers
1186 only for the result */
1187 if (IC_RESULT (ic)) {
1188 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1194 /* if it does not need or is spilt
1195 or is already assigned to registers
1196 or will not live beyond this instructions */
1199 bitVectBitValue (_G.regAssigned, sym->key) ||
1200 sym->liveTo <= ic->seq)
1203 /* if some liverange has been spilt at the block level
1204 and this one live beyond this block then spil this
1206 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1210 /* if trying to allocate this will cause
1211 a spill and there is nothing to spill
1212 or this one is rematerializable then
1214 willCS = willCauseSpill (sym->nRegs, sym->regType);
1215 spillable = computeSpillable (ic);
1216 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1221 /* if it has a spillocation & is used less than
1222 all other live ranges then spill this */
1224 if (sym->usl.spillLoc) {
1225 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1226 allLRs, ebbs[i], ic));
1227 if (leastUsed && leastUsed->used > sym->used) {
1232 /* if none of the liveRanges have a spillLocation then better
1233 to spill this one than anything else already assigned to registers */
1234 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1235 /* if this is local to this block then we might find a block spil */
1236 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1243 /* if we need ptr regs for the right side
1245 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1246 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1250 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1251 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1255 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1256 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1261 /* else we assign registers to it */
1262 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1263 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1265 for (j = 0; j < sym->nRegs; j++) {
1266 if (sym->regType == REG_PTR)
1267 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1269 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1271 /* if the allocation failed which means
1272 this was spilt then break */
1273 if (!sym->regs[j]) {
1278 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1279 /* if it shares registers with operands make sure
1280 that they are in the same position */
1281 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1282 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1283 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1284 OP_SYMBOL (IC_LEFT (ic)));
1286 /* do the same for the right operand */
1287 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1288 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1289 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1290 OP_SYMBOL (IC_RIGHT (ic)));
1303 /* Check for and fix any problems with uninitialized operands */
1304 for (i = 0; i < count; i++)
1308 if (ebbs[i]->noPath &&
1309 (ebbs[i]->entryLabel != entryLabel &&
1310 ebbs[i]->entryLabel != returnLabel))
1313 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1320 verifyRegsAssigned (IC_COND (ic), ic);
1324 if (ic->op == JUMPTABLE)
1326 verifyRegsAssigned (IC_JTCOND (ic), ic);
1330 verifyRegsAssigned (IC_RESULT (ic), ic);
1331 verifyRegsAssigned (IC_LEFT (ic), ic);
1332 verifyRegsAssigned (IC_RIGHT (ic), ic);
1337 /*-----------------------------------------------------------------*/
1338 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1339 /*-----------------------------------------------------------------*/
1340 static void fillGaps()
1346 if (getenv("DISABLE_FILL_GAPS")) return;
1348 /* look for livernages that was spilt by the allocator */
1349 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1350 sym = hTabNextItem(liveRanges,&key)) {
1355 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1357 /* find the liveRanges this one clashes with, that are
1358 still assigned to registers & mark the registers as used*/
1359 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1363 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1364 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1367 clr = hTabItemWithKey(liveRanges,i);
1370 /* mark these registers as used */
1371 for (k = 0 ; k < clr->nRegs ; k++ )
1372 useReg(clr->regs[k]);
1375 if (willCauseSpill(sym->nRegs,sym->regType)) {
1376 /* NOPE :( clear all registers & and continue */
1381 D(printf("Atemping fillGaps on %s: [",sym->name));
1382 /* THERE IS HOPE !!!! */
1383 for (i=0; i < sym->nRegs ; i++ ) {
1384 if (sym->regType == REG_PTR)
1385 sym->regs[i] = getRegPtrNoSpil ();
1387 sym->regs[i] = getRegGprNoSpil ();
1388 D(printf("%s ", sym->regs[i]->name));
1392 /* For all its definitions check if the registers
1393 allocated needs positioning NOTE: we can position
1394 only ONCE if more than One positioning required
1396 We may need to perform the checks twice; once to
1397 position the registers as needed, the second to
1398 verify any register repositioning is still
1402 for (pass=0; pass<2; pass++) {
1403 D(printf(" checking definitions\n"));
1404 for (i = 0 ; i < sym->defs->size ; i++ ) {
1405 if (bitVectBitValue(sym->defs,i)) {
1407 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1408 D(printf(" ic->seq = %d\n", ic->seq));
1409 if (SKIP_IC(ic)) continue;
1410 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1411 /* if left is assigned to registers */
1412 if (IS_SYMOP(IC_LEFT(ic)))
1414 D(printf(" left = "));
1415 D(printOperand(IC_LEFT(ic),NULL));
1417 if (IS_SYMOP(IC_LEFT(ic)) &&
1418 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1419 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1421 if (IS_SYMOP(IC_RIGHT(ic)))
1423 D(printf(" right = "));
1424 D(printOperand(IC_RIGHT(ic),NULL));
1426 if (IS_SYMOP(IC_RIGHT(ic)) &&
1427 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1428 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1430 D(printf(" pdone = %d\n", pdone));
1431 if (pdone > 1) break;
1434 D(printf(" checking uses\n"));
1435 for (i = 0 ; i < sym->uses->size ; i++ ) {
1436 if (bitVectBitValue(sym->uses,i)) {
1438 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1439 D(printf(" ic->seq = %d\n", ic->seq));
1440 if (SKIP_IC(ic)) continue;
1441 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1443 /* if result is assigned to registers */
1444 if (IS_SYMOP(IC_RESULT(ic)))
1446 D(printf(" result = "));
1447 D(printOperand(IC_RESULT(ic),NULL));
1449 if (IS_SYMOP(IC_RESULT(ic)) &&
1450 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1451 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1453 D(printf(" pdone = %d\n", pdone));
1454 if (pdone > 1) break;
1457 if (pdone == 0) break; /* second pass only if regs repositioned */
1458 if (pdone > 1) break;
1460 D(printf(" sym->regs = ["));
1461 for (i=0; i < sym->nRegs ; i++ )
1462 D(printf("%s ", sym->regs[i]->name));
1464 /* had to position more than once GIVE UP */
1466 /* UNDO all the changes we made to try this */
1468 for (i=0; i < sym->nRegs ; i++ ) {
1469 sym->regs[i] = NULL;
1472 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1475 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1476 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1477 sym->isspilt = sym->spillA = 0 ;
1478 sym->usl.spillLoc->allocreq--;
1483 /*-----------------------------------------------------------------*/
1484 /* rUmaskForOp :- returns register mask for an operand */
1485 /*-----------------------------------------------------------------*/
1487 mcs51_rUmaskForOp (operand * op)
1493 /* only temporaries are assigned registers */
1497 sym = OP_SYMBOL (op);
1499 /* if spilt or no registers assigned to it
1501 if (sym->isspilt || !sym->nRegs)
1504 rumask = newBitVect (mcs51_nRegs);
1506 for (j = 0; j < sym->nRegs; j++)
1508 if (sym->regs[j]) /* EEP - debug */
1509 rumask = bitVectSetBit (rumask,
1510 sym->regs[j]->rIdx);
1516 /*-----------------------------------------------------------------*/
1517 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1518 /*-----------------------------------------------------------------*/
1520 regsUsedIniCode (iCode * ic)
1522 bitVect *rmask = newBitVect (mcs51_nRegs);
1524 /* do the special cases first */
1527 rmask = bitVectUnion (rmask,
1528 mcs51_rUmaskForOp (IC_COND (ic)));
1532 /* for the jumptable */
1533 if (ic->op == JUMPTABLE)
1535 rmask = bitVectUnion (rmask,
1536 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1541 /* of all other cases */
1543 rmask = bitVectUnion (rmask,
1544 mcs51_rUmaskForOp (IC_LEFT (ic)));
1548 rmask = bitVectUnion (rmask,
1549 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1552 rmask = bitVectUnion (rmask,
1553 mcs51_rUmaskForOp (IC_RESULT (ic)));
1559 /*-----------------------------------------------------------------*/
1560 /* createRegMask - for each instruction will determine the regsUsed */
1561 /*-----------------------------------------------------------------*/
1563 createRegMask (eBBlock ** ebbs, int count)
1567 /* for all blocks */
1568 for (i = 0; i < count; i++)
1572 if (ebbs[i]->noPath &&
1573 (ebbs[i]->entryLabel != entryLabel &&
1574 ebbs[i]->entryLabel != returnLabel))
1577 /* for all instructions */
1578 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1583 if (SKIP_IC2 (ic) || !ic->rlive)
1586 /* first mark the registers used in this
1588 ic->rUsed = regsUsedIniCode (ic);
1589 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1591 /* now create the register mask for those
1592 registers that are in use : this is a
1593 super set of ic->rUsed */
1594 ic->rMask = newBitVect (mcs51_nRegs + 1);
1596 /* for all live Ranges alive at this point */
1597 for (j = 1; j < ic->rlive->size; j++)
1602 /* if not alive then continue */
1603 if (!bitVectBitValue (ic->rlive, j))
1606 /* find the live range we are interested in */
1607 if (!(sym = hTabItemWithKey (liveRanges, j)))
1609 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1610 "createRegMask cannot find live range");
1611 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1615 /* if no register assigned to it */
1616 if (!sym->nRegs || sym->isspilt)
1619 /* for all the registers allocated to it */
1620 for (k = 0; k < sym->nRegs; k++)
1623 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1629 /*-----------------------------------------------------------------*/
1630 /* rematStr - returns the rematerialized string for a remat var */
1631 /*-----------------------------------------------------------------*/
1633 rematStr (symbol * sym)
1636 iCode *ic = sym->rematiCode;
1641 /* if plus or minus print the right hand side */
1642 if (ic->op == '+' || ic->op == '-')
1644 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1647 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1651 /* cast then continue */
1652 if (IS_CAST_ICODE(ic)) {
1653 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1656 /* we reached the end */
1657 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1664 /*-----------------------------------------------------------------*/
1665 /* regTypeNum - computes the type & number of registers required */
1666 /*-----------------------------------------------------------------*/
1668 regTypeNum (eBBlock *ebbs)
1674 /* for each live range do */
1675 for (sym = hTabFirstItem (liveRanges, &k); sym;
1676 sym = hTabNextItem (liveRanges, &k))
1679 /* if used zero times then no registers needed */
1680 if ((sym->liveTo - sym->liveFrom) == 0)
1684 /* if the live range is a temporary */
1688 /* if the type is marked as a conditional */
1689 if (sym->regType == REG_CND)
1692 /* if used in return only then we don't
1694 if (sym->ruonly || sym->accuse)
1696 if (IS_AGGREGATE (sym->type) || sym->isptr)
1697 sym->type = aggrToPtr (sym->type, FALSE);
1701 /* if the symbol has only one definition &
1702 that definition is a get_pointer */
1703 if (bitVectnBitsOn (sym->defs) == 1 &&
1704 (ic = hTabItemWithKey (iCodehTab,
1705 bitVectFirstBit (sym->defs))) &&
1708 !IS_BITVAR (sym->etype))
1712 /* and that pointer is remat in data space */
1713 if (IS_SYMOP (IC_LEFT (ic)) &&
1714 OP_SYMBOL (IC_LEFT (ic))->remat &&
1715 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1716 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1718 /* create a psuedo symbol & force a spil */
1719 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1720 psym->type = sym->type;
1721 psym->etype = sym->etype;
1723 strcpy (psym->rname, psym->name);
1725 sym->usl.spillLoc = psym;
1726 #if 0 // an alternative fix for bug #480076
1727 /* now this is a useless assignment to itself */
1728 remiCodeFromeBBlock (ebbs, ic);
1730 /* now this really is an assignment to itself, make it so;
1731 it will be optimized out later */
1733 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), IC_RESULT(ic));
1739 /* if in data space or idata space then try to
1740 allocate pointer register */
1744 /* if not then we require registers */
1745 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1746 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1747 getSize (sym->type));
1751 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1752 printTypeChain (sym->type, stderr);
1753 fprintf (stderr, "\n");
1756 /* determine the type of register required */
1757 if (sym->nRegs == 1 &&
1758 IS_PTR (sym->type) &&
1760 sym->regType = REG_PTR;
1762 sym->regType = REG_GPR;
1766 /* for the first run we don't provide */
1767 /* registers for true symbols we will */
1768 /* see how things go */
1774 /*-----------------------------------------------------------------*/
1775 /* freeAllRegs - mark all registers as free */
1776 /*-----------------------------------------------------------------*/
1782 for (i = 0; i < mcs51_nRegs; i++)
1783 regs8051[i].isFree = 1;
1786 /*-----------------------------------------------------------------*/
1787 /* deallocStackSpil - this will set the stack pointer back */
1788 /*-----------------------------------------------------------------*/
1790 DEFSETFUNC (deallocStackSpil)
1798 /*-----------------------------------------------------------------*/
1799 /* farSpacePackable - returns the packable icode for far variables */
1800 /*-----------------------------------------------------------------*/
1802 farSpacePackable (iCode * ic)
1806 /* go thru till we find a definition for the
1807 symbol on the right */
1808 for (dic = ic->prev; dic; dic = dic->prev)
1810 /* if the definition is a call then no */
1811 if ((dic->op == CALL || dic->op == PCALL) &&
1812 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1817 /* if shift by unknown amount then not */
1818 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1819 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1822 /* if pointer get and size > 1 */
1823 if (POINTER_GET (dic) &&
1824 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1827 if (POINTER_SET (dic) &&
1828 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1833 if (IC_COND (dic) &&
1834 IS_TRUE_SYMOP (IC_COND (dic)) &&
1835 isOperandInFarSpace (IC_COND (dic)))
1838 else if (dic->op == JUMPTABLE)
1840 if (IC_JTCOND (dic) &&
1841 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
1842 isOperandInFarSpace (IC_JTCOND (dic)))
1847 /* if any three is a true symbol in far space */
1848 if (IC_RESULT (dic) &&
1849 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1850 isOperandInFarSpace (IC_RESULT (dic)))
1853 if (IC_RIGHT (dic) &&
1854 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1855 isOperandInFarSpace (IC_RIGHT (dic)) &&
1856 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1859 if (IC_LEFT (dic) &&
1860 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1861 isOperandInFarSpace (IC_LEFT (dic)) &&
1862 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1866 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1868 if ((dic->op == LEFT_OP ||
1869 dic->op == RIGHT_OP ||
1871 IS_OP_LITERAL (IC_RIGHT (dic)))
1881 /*-----------------------------------------------------------------*/
1882 /* packRegsForAssign - register reduction for assignment */
1883 /*-----------------------------------------------------------------*/
1885 packRegsForAssign (iCode * ic, eBBlock * ebp)
1889 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1890 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1891 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1897 /* if the true symbol is defined in far space or on stack
1898 then we should not since this will increase register pressure */
1899 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1903 /* find the definition of iTempNN scanning backwards if we find a
1904 a use of the true symbol in before we find the definition then
1906 for (dic = ic->prev; dic; dic = dic->prev)
1909 #if 0 /* jwk: This collides with 1.43 but I really see no need for
1910 this anymore. It fixes bug #716790 and substantially improves
1911 redundant register usage around function calls.
1914 /* if there is a function call then don't pack it */
1915 if ((dic->op == CALL || dic->op == PCALL))
1927 if (IS_SYMOP (IC_COND (dic)) &&
1928 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
1929 IC_COND (dic)->key == IC_RIGHT (ic)->key))
1937 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1938 IS_OP_VOLATILE (IC_RESULT (dic)))
1944 if (IS_SYMOP (IC_RESULT (dic)) &&
1945 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1947 if (POINTER_SET (dic))
1953 if (IS_SYMOP (IC_RIGHT (dic)) &&
1954 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1955 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1961 if (IS_SYMOP (IC_LEFT (dic)) &&
1962 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1963 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1969 if (POINTER_SET (dic) &&
1970 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1979 return 0; /* did not find */
1981 /* if assignment then check that right is not a bit */
1982 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1984 sym_link *etype = operandType (IC_RIGHT (dic));
1985 if (IS_BITFIELD (etype))
1987 /* if result is a bit too then it's ok */
1988 etype = operandType (IC_RESULT (dic));
1989 if (!IS_BITFIELD (etype))
1993 /* if the result is on stack or iaccess then it must be
1994 the same atleast one of the operands */
1995 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1996 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1999 /* the operation has only one symbol
2000 operator then we can pack */
2001 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2002 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2005 if (!((IC_LEFT (dic) &&
2006 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2008 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2012 /* found the definition */
2013 /* replace the result with the result of */
2014 /* this assignment and remove this assignment */
2015 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2016 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2018 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2020 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2022 // TODO: and the otherway around?
2024 /* delete from liverange table also
2025 delete from all the points inbetween and the new
2027 for (sic = dic; sic != ic; sic = sic->next)
2029 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2030 if (IS_ITEMP (IC_RESULT (dic)))
2031 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2034 remiCodeFromeBBlock (ebp, ic);
2035 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2036 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2037 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2041 /*------------------------------------------------------------------*/
2042 /* findAssignToSym : scanning backwards looks for first assig found */
2043 /*------------------------------------------------------------------*/
2045 findAssignToSym (operand * op, iCode * ic)
2049 /* This routine is used to find sequences like
2051 ...; (intervening ops don't use iTempAA or modify FOO)
2052 blah = blah + iTempAA;
2054 and eliminate the use of iTempAA, freeing up its register for
2059 for (dic = ic->prev; dic; dic = dic->prev)
2062 /* if definition by assignment */
2063 if (dic->op == '=' &&
2064 !POINTER_SET (dic) &&
2065 IC_RESULT (dic)->key == op->key
2066 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2068 break; /* found where this temp was defined */
2070 /* if we find an usage then we cannot delete it */
2074 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2077 else if (dic->op == JUMPTABLE)
2079 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2084 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2087 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2090 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2096 return NULL; /* didn't find any assignment to op */
2098 /* we are interested only if defined in far space */
2099 /* or in stack space in case of + & - */
2101 /* if assigned to a non-symbol then don't repack regs */
2102 if (!IS_SYMOP (IC_RIGHT (dic)))
2105 /* if the symbol is volatile then we should not */
2106 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2108 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2109 What does it mean for an iTemp to be volatile, anyway? Passing
2110 TRUE is more cautious but may prevent possible optimizations */
2112 /* if the symbol is in far space then we should not */
2113 if (isOperandInFarSpace (IC_RIGHT (dic)))
2116 /* for + & - operations make sure that
2117 if it is on the stack it is the same
2118 as one of the three operands */
2119 if ((ic->op == '+' || ic->op == '-') &&
2120 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2123 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2124 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2125 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2129 /* now make sure that the right side of dic
2130 is not defined between ic & dic */
2133 iCode *sic = dic->next;
2135 for (; sic != ic; sic = sic->next)
2136 if (IC_RESULT (sic) &&
2137 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2144 /*-----------------------------------------------------------------*/
2145 /* reassignAliasedSym - used by packRegsForSupport to replace */
2146 /* redundant iTemp with equivalent symbol */
2147 /*-----------------------------------------------------------------*/
2149 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2152 unsigned oldSymKey, newSymKey;
2154 oldSymKey = op->key;
2155 newSymKey = IC_RIGHT(assignment)->key;
2157 /* only track live ranges of compiler-generated temporaries */
2158 if (!IS_ITEMP(IC_RIGHT(assignment)))
2161 /* update the live-value bitmaps */
2162 for (ic = assignment; ic != use; ic = ic->next) {
2163 bitVectUnSetBit (ic->rlive, oldSymKey);
2165 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2168 /* update the sym of the used operand */
2169 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2170 op->key = OP_SYMBOL(op)->key;
2172 /* update the sym's liverange */
2173 if ( OP_LIVETO(op) < ic->seq )
2174 setToRange(op, ic->seq, FALSE);
2176 /* remove the assignment iCode now that its result is unused */
2177 remiCodeFromeBBlock (ebp, assignment);
2178 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2179 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2183 /*-----------------------------------------------------------------*/
2184 /* packRegsForSupport :- reduce some registers for support calls */
2185 /*-----------------------------------------------------------------*/
2187 packRegsForSupport (iCode * ic, eBBlock * ebp)
2191 /* for the left & right operand :- look to see if the
2192 left was assigned a true symbol in far space in that
2193 case replace them */
2195 if (IS_ITEMP (IC_LEFT (ic)) &&
2196 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2198 dic = findAssignToSym (IC_LEFT (ic), ic);
2202 /* found it we need to remove it from the block */
2203 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2208 /* do the same for the right operand */
2209 if (IS_ITEMP (IC_RIGHT (ic)) &&
2210 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2212 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2216 /* if this is a subtraction & the result
2217 is a true symbol in far space then don't pack */
2218 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2220 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2221 if (IN_FARSPACE (SPEC_OCLS (etype)))
2224 /* found it we need to remove it from the
2226 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2235 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2238 /*-----------------------------------------------------------------*/
2239 /* packRegsForOneuse : - will reduce some registers for single Use */
2240 /*-----------------------------------------------------------------*/
2242 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2247 /* if returning a literal then do nothing */
2251 /* only upto 2 bytes since we cannot predict
2252 the usage of b, & acc */
2253 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2256 if (ic->op != RETURN &&
2258 !POINTER_SET (ic) &&
2262 if (ic->op == SEND && ic->argreg != 1) return NULL;
2264 /* this routine will mark the a symbol as used in one
2265 instruction use only && if the defintion is local
2266 (ie. within the basic block) && has only one definition &&
2267 that definiion is either a return value from a
2268 function or does not contain any variables in
2270 uses = bitVectCopy (OP_USES (op));
2271 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2272 if (!bitVectIsZero (uses)) /* has other uses */
2275 /* if it has only one defintion */
2276 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2277 return NULL; /* has more than one definition */
2279 /* get that definition */
2281 hTabItemWithKey (iCodehTab,
2282 bitVectFirstBit (OP_DEFS (op)))))
2285 /* if that only usage is a cast */
2286 if (dic->op == CAST) {
2287 /* to a bigger type */
2288 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2289 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2290 /* than we can not, since we cannot predict the usage of b & acc */
2295 /* found the definition now check if it is local */
2296 if (dic->seq < ebp->fSeq ||
2297 dic->seq > ebp->lSeq)
2298 return NULL; /* non-local */
2300 /* now check if it is the return from
2302 if (dic->op == CALL || dic->op == PCALL)
2304 if (ic->op != SEND && ic->op != RETURN &&
2305 !POINTER_SET(ic) && !POINTER_GET(ic))
2307 OP_SYMBOL (op)->ruonly = 1;
2314 /* otherwise check that the definition does
2315 not contain any symbols in far space */
2316 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2317 isOperandInFarSpace (IC_RIGHT (dic)) ||
2318 IS_OP_RUONLY (IC_LEFT (ic)) ||
2319 IS_OP_RUONLY (IC_RIGHT (ic)))
2324 /* if pointer set then make sure the pointer
2326 if (POINTER_SET (dic) &&
2327 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2330 if (POINTER_GET (dic) &&
2331 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2336 /* also make sure the intervenening instructions
2337 don't have any thing in far space */
2338 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2341 /* if there is an intervening function call then no */
2342 if (dic->op == CALL || dic->op == PCALL)
2344 /* if pointer set then make sure the pointer
2346 if (POINTER_SET (dic) &&
2347 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2350 if (POINTER_GET (dic) &&
2351 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2354 /* if address of & the result is remat the okay */
2355 if (dic->op == ADDRESS_OF &&
2356 OP_SYMBOL (IC_RESULT (dic))->remat)
2359 /* if operand has size of three or more & this
2360 operation is a '*','/' or '%' then 'b' may
2362 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2363 getSize (operandType (op)) >= 3)
2366 /* if left or right or result is in far space */
2367 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2368 isOperandInFarSpace (IC_RIGHT (dic)) ||
2369 isOperandInFarSpace (IC_RESULT (dic)) ||
2370 IS_OP_RUONLY (IC_LEFT (dic)) ||
2371 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2372 IS_OP_RUONLY (IC_RESULT (dic)))
2376 /* if left or right or result is on stack */
2377 if (isOperandOnStack(IC_LEFT(dic)) ||
2378 isOperandOnStack(IC_RIGHT(dic)) ||
2379 isOperandOnStack(IC_RESULT(dic))) {
2384 OP_SYMBOL (op)->ruonly = 1;
2388 /*-----------------------------------------------------------------*/
2389 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2390 /*-----------------------------------------------------------------*/
2392 isBitwiseOptimizable (iCode * ic)
2394 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2395 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2397 /* bitwise operations are considered optimizable
2398 under the following conditions (Jean-Louis VERN)
2410 if (IS_LITERAL(rtype) ||
2411 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2417 /*-----------------------------------------------------------------*/
2418 /* isCommutativeOp - tests whether this op cares what order its */
2419 /* operands are in */
2420 /*-----------------------------------------------------------------*/
2421 bool isCommutativeOp(unsigned int op)
2423 if (op == '+' || op == '*' || op == EQ_OP ||
2424 op == '^' || op == '|' || op == BITWISEAND)
2430 /*-----------------------------------------------------------------*/
2431 /* operandUsesAcc - determines whether the code generated for this */
2432 /* operand will have to use the accumulator */
2433 /*-----------------------------------------------------------------*/
2434 bool operandUsesAcc(operand *op)
2440 symbol *sym = OP_SYMBOL(op);
2444 return TRUE; /* duh! */
2446 if (IN_STACK(sym->etype) || sym->onStack ||
2447 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2448 return TRUE; /* acc is used to calc stack offset */
2453 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2455 return FALSE; /* more checks? */
2459 symspace = SPEC_OCLS(sym->etype);
2461 if (sym->iaccess && symspace->paged)
2462 return TRUE; /* must fetch paged indirect sym via accumulator */
2464 if (IN_BITSPACE(symspace))
2465 return TRUE; /* fetching bit vars uses the accumulator */
2467 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2468 return TRUE; /* fetched via accumulator and dptr */
2474 /*-----------------------------------------------------------------*/
2475 /* packRegsForAccUse - pack registers for acc use */
2476 /*-----------------------------------------------------------------*/
2478 packRegsForAccUse (iCode * ic)
2482 /* if this is an aggregate, e.g. a one byte char array */
2483 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2487 /* if we are calling a reentrant function that has stack parameters */
2488 if (ic->op == CALL &&
2489 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2490 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2493 if (ic->op == PCALL &&
2494 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2495 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2498 /* if + or - then it has to be one byte result */
2499 if ((ic->op == '+' || ic->op == '-')
2500 && getSize (operandType (IC_RESULT (ic))) > 1)
2503 /* if shift operation make sure right side is not a literal */
2504 if (ic->op == RIGHT_OP &&
2505 (isOperandLiteral (IC_RIGHT (ic)) ||
2506 getSize (operandType (IC_RESULT (ic))) > 1))
2509 if (ic->op == LEFT_OP &&
2510 (isOperandLiteral (IC_RIGHT (ic)) ||
2511 getSize (operandType (IC_RESULT (ic))) > 1))
2514 if (IS_BITWISE_OP (ic) &&
2515 getSize (operandType (IC_RESULT (ic))) > 1)
2519 /* has only one definition */
2520 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2523 /* has only one use */
2524 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2527 /* and the usage immediately follows this iCode */
2528 if (!(uic = hTabItemWithKey (iCodehTab,
2529 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2532 if (ic->next != uic)
2535 /* if it is a conditional branch then we definitely can */
2539 if (uic->op == JUMPTABLE)
2542 if (POINTER_SET (uic) &&
2543 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2546 /* if the usage is not is an assignment
2547 or an arithmetic / bitwise / shift operation then not */
2548 if (uic->op != '=' &&
2549 !IS_ARITHMETIC_OP (uic) &&
2550 !IS_BITWISE_OP (uic) &&
2551 uic->op != LEFT_OP &&
2552 uic->op != RIGHT_OP)
2555 /* if used in ^ operation then make sure right is not a
2556 literal (WIML: Why is this?) */
2557 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2560 /* if shift operation make sure right side is not a literal */
2561 /* WIML: Why is this? */
2562 if (uic->op == RIGHT_OP &&
2563 (isOperandLiteral (IC_RIGHT (uic)) ||
2564 getSize (operandType (IC_RESULT (uic))) > 1))
2566 if (uic->op == LEFT_OP &&
2567 (isOperandLiteral (IC_RIGHT (uic)) ||
2568 getSize (operandType (IC_RESULT (uic))) > 1))
2571 /* make sure that the result of this icode is not on the
2572 stack, since acc is used to compute stack offset */
2574 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2575 OP_SYMBOL (IC_RESULT (uic))->onStack)
2578 if (isOperandOnStack(IC_RESULT(uic)))
2582 /* if the usage has only one operand then we can */
2583 if (IC_LEFT (uic) == NULL ||
2584 IC_RIGHT (uic) == NULL)
2587 /* if the other operand uses the accumulator then we cannot */
2588 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2589 operandUsesAcc(IC_RIGHT(uic))) ||
2590 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2591 operandUsesAcc(IC_LEFT(uic))) )
2594 /* make sure this is on the left side if not commutative */
2595 /* except for '-', which has been written to be able to
2596 handle reversed operands */
2597 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2598 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2602 // this is too dangerous and need further restrictions
2605 /* if one of them is a literal then we can */
2606 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2607 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2609 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2615 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2619 /*-----------------------------------------------------------------*/
2620 /* packForPush - hueristics to reduce iCode for pushing */
2621 /*-----------------------------------------------------------------*/
2623 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2627 struct eBBlock * ebp=ebpp[blockno];
2629 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2632 /* must have only definition & one usage */
2633 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2634 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2637 /* find the definition */
2638 if (!(dic = hTabItemWithKey (iCodehTab,
2639 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2642 if (dic->op != '=' || POINTER_SET (dic))
2645 if (dic->seq < ebp->fSeq) { // Evelyn did this
2647 for (i=0; i<blockno; i++) {
2648 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2653 wassert (i!=blockno); // no way to recover from here
2656 if (IS_SYMOP(IC_RIGHT(dic))) {
2657 /* make sure the right side does not have any definitions
2659 dbv = OP_DEFS(IC_RIGHT(dic));
2660 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2661 if (bitVectBitValue(dbv,lic->key))
2664 /* make sure they have the same type */
2665 if (IS_SPEC(operandType(IC_LEFT(ic))))
2667 sym_link *itype=operandType(IC_LEFT(ic));
2668 sym_link *ditype=operandType(IC_RIGHT(dic));
2670 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2671 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2674 /* extend the live range of replaced operand if needed */
2675 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2676 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2678 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2681 /* we now we know that it has one & only one def & use
2682 and the that the definition is an assignment */
2683 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2684 remiCodeFromeBBlock (ebp, dic);
2685 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2688 /*-----------------------------------------------------------------*/
2689 /* packRegisters - does some transformations to reduce register */
2691 /*-----------------------------------------------------------------*/
2693 packRegisters (eBBlock ** ebpp, int blockno)
2697 eBBlock *ebp=ebpp[blockno];
2704 /* look for assignments of the form */
2705 /* iTempNN = TRueSym (someoperation) SomeOperand */
2707 /* TrueSym := iTempNN:1 */
2708 for (ic = ebp->sch; ic; ic = ic->next)
2710 /* find assignment of the form TrueSym := iTempNN:1 */
2711 if (ic->op == '=' && !POINTER_SET (ic))
2712 change += packRegsForAssign (ic, ebp);
2719 for (ic = ebp->sch; ic; ic = ic->next)
2721 /* if this is an itemp & result of an address of a true sym
2722 then mark this as rematerialisable */
2723 if (ic->op == ADDRESS_OF &&
2724 IS_ITEMP (IC_RESULT (ic)) &&
2725 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2726 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2727 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2730 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2731 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2732 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2736 /* if straight assignment then carry remat flag if
2737 this is the only definition */
2738 if (ic->op == '=' &&
2739 !POINTER_SET (ic) &&
2740 IS_SYMOP (IC_RIGHT (ic)) &&
2741 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2742 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2743 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2746 OP_SYMBOL (IC_RESULT (ic))->remat =
2747 OP_SYMBOL (IC_RIGHT (ic))->remat;
2748 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2749 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2752 /* if cast to a generic pointer & the pointer being
2753 cast is remat, then we can remat this cast as well */
2754 if (ic->op == CAST &&
2755 IS_SYMOP(IC_RIGHT(ic)) &&
2756 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2757 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1) {
2758 sym_link *to_type = operandType(IC_LEFT(ic));
2759 sym_link *from_type = operandType(IC_RIGHT(ic));
2760 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2761 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2762 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2763 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2767 /* if this is a +/- operation with a rematerizable
2768 then mark this as rematerializable as well */
2769 if ((ic->op == '+' || ic->op == '-') &&
2770 (IS_SYMOP (IC_LEFT (ic)) &&
2771 IS_ITEMP (IC_RESULT (ic)) &&
2772 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2773 OP_SYMBOL (IC_LEFT (ic))->remat &&
2774 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2775 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2777 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2778 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2779 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2782 /* mark the pointer usages */
2783 if (POINTER_SET (ic))
2784 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2786 if (POINTER_GET (ic) &&
2787 IS_SYMOP(IC_LEFT (ic)))
2788 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2792 /* if we are using a symbol on the stack
2793 then we should say mcs51_ptrRegReq */
2794 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2795 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2796 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2797 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2798 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2799 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2802 if (IS_SYMOP (IC_LEFT (ic)))
2803 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2804 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2805 if (IS_SYMOP (IC_RIGHT (ic)))
2806 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2807 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2808 if (IS_SYMOP (IC_RESULT (ic)))
2809 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2810 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2814 /* if the condition of an if instruction
2815 is defined in the previous instruction and
2816 this is the only usage then
2817 mark the itemp as a conditional */
2818 if ((IS_CONDITIONAL (ic) ||
2819 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2820 ic->next && ic->next->op == IFX &&
2821 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2822 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2823 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2825 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2829 /* reduce for support function calls */
2830 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2831 packRegsForSupport (ic, ebp);
2833 /* some cases the redundant moves can
2834 can be eliminated for return statements */
2835 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2836 !isOperandInFarSpace (IC_LEFT (ic)) &&
2837 options.model == MODEL_SMALL) {
2838 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2841 /* if pointer set & left has a size more than
2842 one and right is not in far space */
2843 if (POINTER_SET (ic) &&
2844 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2845 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2846 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2847 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2848 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2850 /* if pointer get */
2851 if (POINTER_GET (ic) &&
2852 IS_SYMOP (IC_LEFT (ic)) &&
2853 !isOperandInFarSpace (IC_RESULT (ic)) &&
2854 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2855 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2856 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2857 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2860 /* if this is cast for intergral promotion then
2861 check if only use of the definition of the
2862 operand being casted/ if yes then replace
2863 the result of that arithmetic operation with
2864 this result and get rid of the cast */
2867 sym_link *fromType = operandType (IC_RIGHT (ic));
2868 sym_link *toType = operandType (IC_LEFT (ic));
2870 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2871 getSize (fromType) != getSize (toType) &&
2872 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2875 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2878 if (IS_ARITHMETIC_OP (dic))
2880 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2881 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2882 remiCodeFromeBBlock (ebp, ic);
2883 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2884 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2885 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2889 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2895 /* if the type from and type to are the same
2896 then if this is the only use then packit */
2897 if (compareType (operandType (IC_RIGHT (ic)),
2898 operandType (IC_LEFT (ic))) == 1)
2900 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2903 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2904 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2905 remiCodeFromeBBlock (ebp, ic);
2906 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2907 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2908 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2916 iTempNN := (some variable in farspace) V1
2921 if (ic->op == IPUSH)
2923 packForPush (ic, ebpp, blockno);
2927 /* pack registers for accumulator use, when the
2928 result of an arithmetic or bit wise operation
2929 has only one use, that use is immediately following
2930 the defintion and the using iCode has only one
2931 operand or has two operands but one is literal &
2932 the result of that operation is not on stack then
2933 we can leave the result of this operation in acc:b
2935 if ((IS_ARITHMETIC_OP (ic)
2936 || IS_CONDITIONAL(ic)
2937 || IS_BITWISE_OP (ic)
2938 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2939 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2941 IS_ITEMP (IC_RESULT (ic)) &&
2942 getSize (operandType (IC_RESULT (ic))) <= 2)
2944 packRegsForAccUse (ic);
2948 /*-----------------------------------------------------------------*/
2949 /* assignRegisters - assigns registers to each live range as need */
2950 /*-----------------------------------------------------------------*/
2952 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2957 setToNull ((void *) &_G.funcrUsed);
2958 setToNull ((void *) &_G.regAssigned);
2959 setToNull ((void *) &_G.totRegAssigned);
2960 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2963 /* change assignments this will remove some
2964 live ranges reducing some register pressure */
2966 for (i = 0; i < count; i++)
2967 packRegisters (ebbs, i);
2969 /* liveranges probably changed by register packing
2970 so we compute them again */
2971 recomputeLiveRanges (ebbs, count);
2973 if (options.dump_pack)
2974 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2976 /* first determine for each live range the number of
2977 registers & the type of registers required for each */
2980 /* and serially allocate registers */
2981 serialRegAssign (ebbs, count);
2984 //setToNull ((void *) &_G.regAssigned);
2985 //setToNull ((void *) &_G.totRegAssigned);
2988 /* if stack was extended then tell the user */
2991 /* werror(W_TOOMANY_SPILS,"stack", */
2992 /* _G.stackExtend,currFunc->name,""); */
2998 /* werror(W_TOOMANY_SPILS,"data space", */
2999 /* _G.dataExtend,currFunc->name,""); */
3003 /* after that create the register mask
3004 for each of the instruction */
3005 createRegMask (ebbs, count);
3007 /* redo that offsets for stacked automatic variables */
3009 redoStackOffsets ();
3012 if (options.dump_rassgn)
3014 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3015 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3018 /* do the overlaysegment stuff SDCCmem.c */
3019 doOverlays (ebbs, count);
3021 /* now get back the chain */
3022 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3026 /* free up any _G.stackSpil locations allocated */
3027 applyToSet (_G.stackSpil, deallocStackSpil);
3029 setToNull ((void *) &_G.stackSpil);
3030 setToNull ((void *) &_G.spiltSet);
3031 /* mark all registers as free */