1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (ATMEL AVR) 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 genAVRCode (iCode *);
49 bitVect *funcrUsed; /* registers used in a function */
55 /* Shared with gen.c */
56 int avr_ptrRegReq; /* pointer register required */
61 {REG_GPR, R0_IDX, REG_GPR, "r0", "r0", "", 0, 0, 0}, /* used as scratch */
62 {REG_GPR, R1_IDX, REG_GPR, "r1", "r1", "", 0, 0, 0}, /* used as scratch */
63 {REG_GPR, R2_IDX, REG_GPR, "r2", "r2", "", 0, 1, 1}, /* gpr */
64 {REG_GPR, R3_IDX, REG_GPR, "r3", "r3", "", 0, 1, 1}, /* gpr */
65 {REG_GPR, R4_IDX, REG_GPR, "r4", "r4", "", 0, 1, 1}, /* gpr */
66 {REG_GPR, R5_IDX, REG_GPR, "r5", "r5", "", 0, 1, 1}, /* gpr */
67 {REG_GPR, R6_IDX, REG_GPR, "r6", "r6", "", 0, 1, 1}, /* gpr */
68 {REG_GPR, R7_IDX, REG_GPR, "r7", "r7", "", 0, 1, 1}, /* gpr */
69 {REG_GPR, R8_IDX, REG_GPR, "r8", "r8", "", 0, 1, 1}, /* gpr */
70 {REG_GPR, R9_IDX, REG_GPR, "r9", "r9", "", 0, 1, 1}, /* gpr */
71 {REG_GPR, R10_IDX, REG_GPR, "r10", "r10", "", 0, 1, 1}, /* gpr */
72 {REG_GPR, R11_IDX, REG_GPR, "r11", "r11", "", 0, 1, 1}, /* gpr */
73 {REG_GPR, R12_IDX, REG_GPR, "r12", "r12", "", 0, 1, 1}, /* gpr */
74 {REG_GPR, R13_IDX, REG_GPR, "r13", "r13", "", 0, 1, 1}, /* gpr */
75 {REG_GPR, R14_IDX, REG_GPR, "r14", "r14", "", 0, 1, 1}, /* gpr */
76 {REG_GPR, R15_IDX, REG_GPR, "r15", "r15", "", 0, 1, 1}, /* gpr */
77 {REG_GPR, R16_IDX, REG_GPR, "r16", "r16", "", 0, 1, 0}, /* parm/gpr */
78 {REG_GPR, R17_IDX, REG_GPR, "r17", "r17", "", 0, 1, 0}, /* parm/gpr */
79 {REG_GPR, R18_IDX, REG_GPR, "r18", "r18", "", 0, 1, 0}, /* parm/gpr */
80 {REG_GPR, R19_IDX, REG_GPR, "r19", "r19", "", 0, 1, 0}, /* parm/gpr */
81 {REG_GPR, R20_IDX, REG_GPR, "r20", "r20", "", 0, 1, 0}, /* parm/gpr */
82 {REG_GPR, R21_IDX, REG_GPR, "r21", "r21", "", 0, 1, 0}, /* parm/gpr */
83 {REG_GPR, R22_IDX, REG_GPR, "r22", "r22", "", 0, 1, 0}, /* parm/gpr */
84 {REG_GPR, R23_IDX, REG_GPR, "r23", "r23", "", 0, 1, 0}, /* parm/gpr */
85 {REG_GPR, R24_IDX, REG_GPR, "r24", "r24", "", 0, 0, 0}, /* scratch */
86 {REG_GPR, R25_IDX, REG_GPR, "r25", "r25", "", 0, 0, 0}, /* scratch */
87 {REG_GPR, R26_IDX, REG_GPR, "r26", "r26", "", 0, 1, 1}, /* used as pointer reg X */
88 {REG_GPR, R27_IDX, REG_GPR, "r27", "r27", "", 0, 1, 1}, /* used as pointer reg X */
89 {REG_GPR, R28_IDX, REG_GPR, "r28", "r28", "", 0, 1, 0}, /* stack frame Y */
90 {REG_GPR, R29_IDX, REG_GPR, "r29", "r29", "", 0, 1, 0}, /* stack frame Y */
91 {REG_GPR, R30_IDX, REG_GPR, "r30", "r30", "", 0, 1, 1}, /* used as pointer reg Z */
92 {REG_GPR, R31_IDX, REG_GPR, "r31", "r31", "", 0, 1, 1}, /* used as pointer reg Z */
93 {REG_PTR, X_IDX, REG_PTR, "X", "X", "", 0, 1, 0},
94 {REG_PTR, Z_IDX, REG_PTR, "Z", "Z", "", 0, 1, 0},
97 int avr_fReg = 0; /* first allocatable register */
99 static void spillThis (symbol *);
101 /*-----------------------------------------------------------------*/
102 /* allocReg - allocates register of given type */
103 /*-----------------------------------------------------------------*/
105 allocReg (short type)
109 for (i = avr_fReg; i < avr_nRegs; i++)
112 /* if type is given as 0 then any
113 free register will do */
117 regsAVR[i].isFree = 0;
119 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
122 /* other wise look for specific type
124 if (regsAVR[i].isFree &&
125 regsAVR[i].type == type)
127 regsAVR[i].isFree = 0;
130 bitVectSetBit (currFunc->regsUsed, i);
137 /*-----------------------------------------------------------------*/
138 /* avr_regWithIdx - returns pointer to register wit index number */
139 /*-----------------------------------------------------------------*/
141 avr_regWithIdx (int idx)
145 for (i = 0; i < avr_nRegs; i++)
146 if (regsAVR[i].rIdx == idx)
149 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
150 "regWithIdx not found");
154 /*-----------------------------------------------------------------*/
155 /* freeReg - frees a register */
156 /*-----------------------------------------------------------------*/
164 /*-----------------------------------------------------------------*/
165 /* nFreeRegs - returns number of free registers */
166 /*-----------------------------------------------------------------*/
173 for (i = avr_fReg; i < avr_nRegs; i++)
174 if (regsAVR[i].isFree && regsAVR[i].type == type)
179 /*-----------------------------------------------------------------*/
180 /* nfreeRegsType - free registers with type */
181 /*-----------------------------------------------------------------*/
183 nfreeRegsType (int type)
188 if ((nfr = nFreeRegs (type)) == 0)
189 return nFreeRegs (REG_GPR);
192 return nFreeRegs (type);
196 /*-----------------------------------------------------------------*/
197 /* allDefsOutOfRange - all definitions are out of a range */
198 /*-----------------------------------------------------------------*/
200 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
207 for (i = 0; i < defs->size; i++)
211 if (bitVectBitValue (defs, i) &&
212 (ic = hTabItemWithKey (iCodehTab, i)) &&
213 (ic->seq >= fseq && ic->seq <= toseq))
221 /*-----------------------------------------------------------------*/
222 /* computeSpillable - given a point find the spillable live ranges */
223 /*-----------------------------------------------------------------*/
225 computeSpillable (iCode * ic)
229 /* spillable live ranges are those that are live at this
230 point . the following categories need to be subtracted
232 a) - those that are already spilt
233 b) - if being used by this one
234 c) - defined by this one */
236 spillable = bitVectCopy (ic->rlive);
238 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
240 bitVectCplAnd (spillable, ic->uses); /* used in this one */
241 bitVectUnSetBit (spillable, ic->defKey);
242 spillable = bitVectIntersect (spillable, _G.regAssigned);
247 /*-----------------------------------------------------------------*/
248 /* noSpilLoc - return true if a variable has no spil location */
249 /*-----------------------------------------------------------------*/
251 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
253 return (sym->usl.spillLoc ? 0 : 1);
256 /*-----------------------------------------------------------------*/
257 /* hasSpilLoc - will return 1 if the symbol has spil location */
258 /*-----------------------------------------------------------------*/
260 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
262 return (sym->usl.spillLoc ? 1 : 0);
265 /*-----------------------------------------------------------------*/
266 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
267 /* but is not used as a pointer */
268 /*-----------------------------------------------------------------*/
270 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
272 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
275 /*-----------------------------------------------------------------*/
276 /* rematable - will return 1 if the remat flag is set */
277 /*-----------------------------------------------------------------*/
279 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
284 /*-----------------------------------------------------------------*/
285 /* notUsedInBlock - not used in this block */
286 /*-----------------------------------------------------------------*/
288 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
290 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
291 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
294 /*-----------------------------------------------------------------*/
295 /* notUsedInRemaining - not used or defined in remain of the block */
296 /*-----------------------------------------------------------------*/
298 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
300 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
301 allDefsOutOfRange (sym->defs, ic->seq, ebp->lSeq));
304 /*-----------------------------------------------------------------*/
305 /* allLRs - return true for all */
306 /*-----------------------------------------------------------------*/
308 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
313 /*-----------------------------------------------------------------*/
314 /* liveRangesWith - applies function to a given set of live range */
315 /*-----------------------------------------------------------------*/
317 liveRangesWith (bitVect * lrs,
318 int (func) (symbol *, eBBlock *, iCode *),
319 eBBlock * ebp, iCode * ic)
324 if (!lrs || !lrs->size)
327 for (i = 1; i < lrs->size; i++)
330 if (!bitVectBitValue (lrs, i))
333 /* if we don't find it in the live range
334 hash table we are in serious trouble */
335 if (!(sym = hTabItemWithKey (liveRanges, i)))
337 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
338 "liveRangesWith could not find liveRange");
342 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
343 addSetHead (&rset, sym);
350 /*-----------------------------------------------------------------*/
351 /* leastUsedLR - given a set determines which is the least used */
352 /*-----------------------------------------------------------------*/
354 leastUsedLR (set * sset)
356 symbol *sym = NULL, *lsym = NULL;
358 sym = lsym = setFirstItem (sset);
363 for (; lsym; lsym = setNextItem (sset))
366 /* if usage is the same then prefer
367 the spill the smaller of the two */
368 if (lsym->used == sym->used)
369 if (getSize (lsym->type) < getSize (sym->type))
373 if (lsym->used < sym->used)
378 setToNull ((void **) &sset);
383 /*-----------------------------------------------------------------*/
384 /* noOverLap - will iterate through the list looking for over lap */
385 /*-----------------------------------------------------------------*/
387 noOverLap (set * itmpStack, symbol * fsym)
392 for (sym = setFirstItem (itmpStack); sym;
393 sym = setNextItem (itmpStack))
395 if (sym->liveTo > fsym->liveFrom)
403 /*-----------------------------------------------------------------*/
404 /* isFree - will return 1 if the a free spil location is found */
405 /*-----------------------------------------------------------------*/
410 V_ARG (symbol **, sloc);
411 V_ARG (symbol *, fsym);
413 /* if already found */
417 /* if it is free && and the itmp assigned to
418 this does not have any overlapping live ranges
419 with the one currently being assigned and
420 the size can be accomodated */
422 noOverLap (sym->usl.itmpStack, fsym) &&
423 getSize (sym->type) >= getSize (fsym->type))
432 /*-----------------------------------------------------------------*/
433 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
434 /*-----------------------------------------------------------------*/
436 spillLRWithPtrReg (symbol * forSym)
442 if (!_G.regAssigned ||
443 bitVectIsZero (_G.regAssigned))
446 X = avr_regWithIdx (X_IDX);
447 Z = avr_regWithIdx (Z_IDX);
449 /* for all live ranges */
450 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
451 lrsym = hTabNextItem (liveRanges, &k))
455 /* if no registers assigned to it or
457 /* if it does not overlap with this then
458 not need to spill it */
460 if (lrsym->isspilt || !lrsym->nRegs ||
461 (lrsym->liveTo < forSym->liveFrom))
464 /* go thru the registers : if it is either
465 r0 or r1 then spil it */
466 for (j = 0; j < lrsym->nRegs; j++)
467 if (lrsym->regs[j] == X || lrsym->regs[j] == Z)
476 /*-----------------------------------------------------------------*/
477 /* createStackSpil - create a location on the stack to spil */
478 /*-----------------------------------------------------------------*/
480 createStackSpil (symbol * sym)
483 int useXstack, model, noOverlay;
488 /* first go try and find a free one that is already
489 existing on the stack */
490 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
492 /* found a free one : just update & return */
493 sym->usl.spillLoc = sloc;
496 addSetHead (&sloc->usl.itmpStack, sym);
500 /* could not then have to create one , this is the hard part
501 we need to allocate this on the stack : this is really a
502 hack!! but cannot think of anything better at this time */
504 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
506 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
511 sloc = newiTemp (slocBuffer);
513 /* set the type to the spilling symbol */
514 sloc->type = copyLinkChain (sym->type);
515 sloc->etype = getSpec (sloc->type);
516 SPEC_SCLS (sloc->etype) = S_AUTO;
517 SPEC_EXTR (sloc->etype) = 0;
519 /* we don't allow it to be allocated`
520 onto the external stack since : so we
521 temporarily turn it off ; we also
522 turn off memory model to prevent
523 the spil from going to the external storage
524 and turn off overlaying
527 useXstack = options.useXstack;
528 model = options.model;
529 noOverlay = options.noOverlay;
530 stackAuto = options.stackAuto;
531 options.noOverlay = 1;
532 options.model = options.useXstack = 0;
536 options.useXstack = useXstack;
537 options.model = model;
538 options.noOverlay = noOverlay;
539 options.stackAuto = stackAuto;
540 sloc->isref = 1; /* to prevent compiler warning */
542 /* if it is on the stack then update the stack */
543 if (IN_STACK (sloc->etype))
545 currFunc->stack += getSize (sloc->type);
546 _G.stackExtend += getSize (sloc->type);
549 _G.dataExtend += getSize (sloc->type);
551 /* add it to the _G.stackSpil set */
552 addSetHead (&_G.stackSpil, sloc);
553 sym->usl.spillLoc = sloc;
556 /* add it to the set of itempStack set
557 of the spill location */
558 addSetHead (&sloc->usl.itmpStack, sym);
562 /*-----------------------------------------------------------------*/
563 /* isSpiltOnStack - returns true if the spil location is on stack */
564 /*-----------------------------------------------------------------*/
566 isSpiltOnStack (symbol * sym)
577 if (!sym->usl.spillLoc)
580 etype = getSpec (sym->usl.spillLoc->type);
581 if (IN_STACK (etype))
587 /*-----------------------------------------------------------------*/
588 /* spillThis - spils a specific operand */
589 /*-----------------------------------------------------------------*/
591 spillThis (symbol * sym)
594 /* if this is rematerializable or has a spillLocation
595 we are okay, else we need to create a spillLocation
597 if (!(sym->remat || sym->usl.spillLoc))
598 createStackSpil (sym);
601 /* mark it has spilt & put it in the spilt set */
603 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
605 bitVectUnSetBit (_G.regAssigned, sym->key);
607 for (i = 0; i < sym->nRegs; i++)
611 freeReg (sym->regs[i]);
615 if (sym->usl.spillLoc && !sym->remat)
616 sym->usl.spillLoc->allocreq = 1;
620 /*-----------------------------------------------------------------*/
621 /* selectSpil - select a iTemp to spil : rather a simple procedure */
622 /*-----------------------------------------------------------------*/
624 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
626 bitVect *lrcs = NULL;
630 /* get the spillable live ranges */
631 lrcs = computeSpillable (ic);
633 /* get all live ranges that are rematerizable */
634 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
637 /* return the least used of these */
638 return leastUsedLR (selectS);
641 /* if the symbol is local to the block then */
642 if (forSym->liveTo < ebp->lSeq)
645 /* check if there are any live ranges allocated
646 to registers that are not used in this block */
648 (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
650 sym = leastUsedLR (selectS);
651 /* if this is not rematerializable */
660 /* check if there are any live ranges that not
661 used in the remainder of the block */
663 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
665 sym = leastUsedLR (selectS);
678 /* find live ranges with spillocation && not used as pointers */
679 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
682 sym = leastUsedLR (selectS);
683 /* mark this as allocation required */
684 sym->usl.spillLoc->allocreq = 1;
688 /* find live ranges with spillocation */
689 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
692 sym = leastUsedLR (selectS);
693 sym->usl.spillLoc->allocreq = 1;
697 /* couldn't find then we need to create a spil
698 location on the stack , for which one? the least
700 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
703 /* return a created spil location */
704 sym = createStackSpil (leastUsedLR (selectS));
705 sym->usl.spillLoc->allocreq = 1;
709 /* this is an extreme situation we will spill
710 this one : happens very rarely but it does happen */
716 /*-----------------------------------------------------------------*/
717 /* spilSomething - spil some variable & mark registers as free */
718 /*-----------------------------------------------------------------*/
720 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
725 /* get something we can spil */
726 ssym = selectSpil (ic, ebp, forSym);
728 /* mark it as spilt */
730 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
732 /* mark it as not register assigned &
733 take it away from the set */
734 bitVectUnSetBit (_G.regAssigned, ssym->key);
736 /* mark the registers as free */
737 for (i = 0; i < ssym->nRegs; i++)
739 freeReg (ssym->regs[i]);
741 /* if this was a block level spil then insert push & pop
742 at the start & end of block respectively */
745 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
746 /* add push to the start of the block */
747 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
748 ebp->sch->next : ebp->sch));
749 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
750 /* add pop to the end of the block */
751 addiCodeToeBBlock (ebp, nic, NULL);
754 /* if spilt because not used in the remainder of the
755 block then add a push before this instruction and
756 a pop at the end of the block */
757 if (ssym->remainSpil)
760 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
761 /* add push just before this instruction */
762 addiCodeToeBBlock (ebp, nic, ic);
764 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
765 /* add pop to the end of the block */
766 addiCodeToeBBlock (ebp, nic, NULL);
775 /*-----------------------------------------------------------------*/
776 /* getRegPtr - will try for PTR if not a GPR type if not spil */
777 /*-----------------------------------------------------------------*/
779 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
784 /* try for a ptr type */
785 if ((reg = allocReg (REG_PTR)))
788 /* try for gpr type */
789 if ((reg = allocReg (REG_GPR)))
792 /* we have to spil */
793 if (!spilSomething (ic, ebp, sym))
796 /* this looks like an infinite loop but
797 in really selectSpil will abort */
801 /*-----------------------------------------------------------------*/
802 /* getRegScr - will try for SCR if not a GPR type if not spil */
803 /*-----------------------------------------------------------------*/
805 getRegScr (iCode * ic, eBBlock * ebp, symbol * sym)
810 /* try for a ptr type */
811 if ((reg = allocReg (REG_SCR)))
814 /* try for gpr type */
815 if ((reg = allocReg (REG_GPR)))
818 /* we have to spil */
819 if (!spilSomething (ic, ebp, sym))
822 /* this looks like an infinite loop but
823 in really selectSpil will abort */
827 /*-----------------------------------------------------------------*/
828 /* getRegGpr - will try for GPR if not spil */
829 /*-----------------------------------------------------------------*/
831 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
836 /* try for gpr type */
837 if ((reg = allocReg (REG_GPR)))
841 if ((reg = allocReg (REG_PTR)))
844 /* we have to spil */
845 if (!spilSomething (ic, ebp, sym))
848 /* this looks like an infinite loop but
849 in reality selectSpil will abort */
853 /*-----------------------------------------------------------------*/
854 /* symHasReg - symbol has a given register */
855 /*-----------------------------------------------------------------*/
857 symHasReg (symbol * sym, regs * reg)
861 for (i = 0; i < sym->nRegs; i++)
862 if (sym->regs[i] == reg)
868 /*-----------------------------------------------------------------*/
869 /* deassignLRs - check the live to and if they have registers & are */
870 /* not spilt then free up the registers */
871 /*-----------------------------------------------------------------*/
873 deassignLRs (iCode * ic, eBBlock * ebp)
879 for (sym = hTabFirstItem (liveRanges, &k); sym;
880 sym = hTabNextItem (liveRanges, &k))
884 /* if it does not end here */
885 if (sym->liveTo > ic->seq)
888 /* if it was spilt on stack then we can
889 mark the stack spil location as free */
894 sym->usl.spillLoc->isFree = 1;
900 if (!bitVectBitValue (_G.regAssigned, sym->key))
903 /* special case check if this is an IFX &
904 the privious one was a pop and the
905 previous one was not spilt then keep track
907 if (ic->op == IFX && ic->prev &&
908 ic->prev->op == IPOP &&
909 !ic->prev->parmPush &&
910 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
911 psym = OP_SYMBOL (IC_LEFT (ic->prev));
917 bitVectUnSetBit (_G.regAssigned, sym->key);
919 /* if the result of this one needs registers
920 and does not have it then assign it right
922 if (IC_RESULT (ic) &&
923 !(SKIP_IC2 (ic) || /* not a special icode */
924 ic->op == JUMPTABLE ||
930 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
931 result->liveTo > ic->seq && /* and will live beyond this */
932 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
933 result->regType == sym->regType && /* same register types */
934 result->nRegs && /* which needs registers */
935 !result->isspilt && /* and does not already have them */
937 !bitVectBitValue (_G.regAssigned, result->key) &&
938 /* the number of free regs + number of regs in this LR
939 can accomodate the what result Needs */
940 ((nfreeRegsType (result->regType) +
941 sym->nRegs) >= result->nRegs)
945 for (i = 0; i < result->nRegs; i++)
947 result->regs[i] = sym->regs[i];
948 else if (result->regType == REG_SCR)
949 result->regs[i] = getRegScr (ic, ebp, result);
951 result->regs[i] = getRegGpr (ic, ebp, result);
953 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
957 /* free the remaining */
958 for (; i < sym->nRegs; i++)
962 if (!symHasReg (psym, sym->regs[i]))
963 freeReg (sym->regs[i]);
966 freeReg (sym->regs[i]);
973 /*-----------------------------------------------------------------*/
974 /* reassignLR - reassign this to registers */
975 /*-----------------------------------------------------------------*/
977 reassignLR (operand * op)
979 symbol *sym = OP_SYMBOL (op);
982 /* not spilt any more */
983 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
984 bitVectUnSetBit (_G.spiltSet, sym->key);
986 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
990 for (i = 0; i < sym->nRegs; i++)
991 sym->regs[i]->isFree = 0;
994 /*-----------------------------------------------------------------*/
995 /* willCauseSpill - determines if allocating will cause a spill */
996 /*-----------------------------------------------------------------*/
998 willCauseSpill (int nr, int rt)
1000 /* first check if there are any avlb registers
1001 of te type required */
1004 /* special case for pointer type
1005 if pointer type not avlb then
1006 check for type gpr */
1007 if (nFreeRegs (rt) >= nr)
1009 if (nFreeRegs (REG_GPR) >= nr)
1016 if (nFreeRegs (rt) >= nr)
1021 if (nFreeRegs (REG_PTR) +
1022 nFreeRegs (REG_GPR) >= nr)
1027 /* it will cause a spil */
1031 /*-----------------------------------------------------------------*/
1032 /* positionRegs - the allocator can allocate same registers to res- */
1033 /* ult and operand, if this happens make sure they are in the same */
1034 /* position as the operand otherwise chaos results */
1035 /*-----------------------------------------------------------------*/
1037 positionRegs (symbol * result, symbol * opsym, int lineno)
1039 int count = min (result->nRegs, opsym->nRegs);
1040 int i, j = 0, shared = 0;
1042 /* if the result has been spilt then cannot share */
1047 /* first make sure that they actually share */
1048 for (i = 0; i < count; i++)
1050 for (j = 0; j < count; j++)
1052 if (result->regs[i] == opsym->regs[j] && i != j)
1062 regs *tmp = result->regs[i];
1063 result->regs[i] = result->regs[j];
1064 result->regs[j] = tmp;
1069 /*-----------------------------------------------------------------*/
1070 /* serialRegAssign - serially allocate registers to the variables */
1071 /*-----------------------------------------------------------------*/
1073 serialRegAssign (eBBlock ** ebbs, int count)
1077 /* for all blocks */
1078 for (i = 0; i < count; i++)
1083 if (ebbs[i]->noPath &&
1084 (ebbs[i]->entryLabel != entryLabel &&
1085 ebbs[i]->entryLabel != returnLabel))
1088 /* of all instructions do */
1089 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1092 /* if this is an ipop that means some live
1093 range will have to be assigned again */
1095 reassignLR (IC_LEFT (ic));
1097 /* if result is present && is a true symbol */
1098 if (IC_RESULT (ic) && ic->op != IFX &&
1099 IS_TRUE_SYMOP (IC_RESULT (ic)))
1100 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1102 /* take away registers from live
1103 ranges that end at this instruction */
1104 deassignLRs (ic, ebbs[i]);
1106 /* some don't need registers */
1107 if (SKIP_IC2 (ic) ||
1108 ic->op == JUMPTABLE ||
1112 (IC_RESULT (ic) && POINTER_SET (ic)))
1115 /* now we need to allocate registers
1116 only for the result */
1119 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1124 /* if it does not need or is spilt
1125 or is already assigned to registers
1126 or will not live beyond this instructions */
1129 bitVectBitValue (_G.regAssigned, sym->key) ||
1130 sym->liveTo <= ic->seq)
1133 /* if some liverange has been spilt at the block level
1134 and this one live beyond this block then spil this
1136 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1141 /* if trying to allocate this will cause
1142 a spill and there is nothing to spill
1143 or this one is rematerializable then
1145 willCS = willCauseSpill (sym->nRegs, sym->regType);
1146 spillable = computeSpillable (ic);
1148 (willCS && bitVectIsZero (spillable)))
1156 /* if it has a spillocation & is used less than
1157 all other live ranges then spill this */
1158 if (willCS && sym->usl.spillLoc)
1162 leastUsedLR (liveRangesWith (spillable,
1167 leastUsed->used > sym->used)
1174 /* we assign registers to it */
1175 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1177 for (j = 0; j < sym->nRegs; j++)
1179 if (sym->regType == REG_PTR)
1180 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1181 else if (sym->regType == REG_SCR)
1182 sym->regs[j] = getRegScr (ic, ebbs[i], sym);
1184 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1186 /* if the allocation falied which means
1187 this was spilt then break */
1191 /* if it shares registers with operands make sure
1192 that they are in the same position */
1193 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1194 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1195 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1196 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1197 /* do the same for the right operand */
1198 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1199 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1200 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1201 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1208 /*-----------------------------------------------------------------*/
1209 /* rUmaskForOp :- returns register mask for an operand */
1210 /*-----------------------------------------------------------------*/
1212 rUmaskForOp (operand * op)
1218 /* only temporaries are assigned registers */
1222 sym = OP_SYMBOL (op);
1224 /* if spilt or no registers assigned to it
1226 if (sym->isspilt || !sym->nRegs)
1229 rumask = newBitVect (avr_nRegs);
1231 for (j = 0; j < sym->nRegs; j++)
1233 rumask = bitVectSetBit (rumask,
1234 sym->regs[j]->rIdx);
1240 /*-----------------------------------------------------------------*/
1241 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1242 /*-----------------------------------------------------------------*/
1244 regsUsedIniCode (iCode * ic)
1246 bitVect *rmask = newBitVect (avr_nRegs);
1248 /* do the special cases first */
1251 rmask = bitVectUnion (rmask,
1252 rUmaskForOp (IC_COND (ic)));
1256 /* for the jumptable */
1257 if (ic->op == JUMPTABLE)
1259 rmask = bitVectUnion (rmask,
1260 rUmaskForOp (IC_JTCOND (ic)));
1265 /* of all other cases */
1267 rmask = bitVectUnion (rmask,
1268 rUmaskForOp (IC_LEFT (ic)));
1272 rmask = bitVectUnion (rmask,
1273 rUmaskForOp (IC_RIGHT (ic)));
1276 rmask = bitVectUnion (rmask,
1277 rUmaskForOp (IC_RESULT (ic)));
1283 /*-----------------------------------------------------------------*/
1284 /* createRegMask - for each instruction will determine the regsUsed */
1285 /*-----------------------------------------------------------------*/
1287 createRegMask (eBBlock ** ebbs, int count)
1291 /* for all blocks */
1292 for (i = 0; i < count; i++)
1296 if (ebbs[i]->noPath &&
1297 (ebbs[i]->entryLabel != entryLabel &&
1298 ebbs[i]->entryLabel != returnLabel))
1301 /* for all instructions */
1302 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1307 if (SKIP_IC2 (ic) || !ic->rlive)
1310 /* first mark the registers used in this
1312 ic->rUsed = regsUsedIniCode (ic);
1313 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1315 /* now create the register mask for those
1316 registers that are in use : this is a
1317 super set of ic->rUsed */
1318 ic->rMask = newBitVect (avr_nRegs + 1);
1320 /* for all live Ranges alive at this point */
1321 for (j = 1; j < ic->rlive->size; j++)
1326 /* if not alive then continue */
1327 if (!bitVectBitValue (ic->rlive, j))
1330 /* find the live range we are interested in */
1331 if (!(sym = hTabItemWithKey (liveRanges, j)))
1333 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1334 "createRegMask cannot find live range");
1338 /* if no register assigned to it */
1339 if (!sym->nRegs || sym->isspilt)
1342 /* for all the registers allocated to it */
1343 for (k = 0; k < sym->nRegs; k++)
1348 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1349 /* special case for X & Z registers */
1350 if (k == R26_IDX || k == R27_IDX)
1351 ic->rMask = bitVectSetBit (ic->rMask, X_IDX);
1352 if (k == R30_IDX || k == R31_IDX)
1353 ic->rMask = bitVectSetBit (ic->rMask, Z_IDX);
1361 /*-----------------------------------------------------------------*/
1362 /* rematStr - returns the rematerialized string for a remat var */
1363 /*-----------------------------------------------------------------*/
1365 rematStr (symbol * sym)
1368 iCode *ic = sym->rematiCode;
1373 /* if plus or minus print the right hand side */
1374 if (ic->op == '+' || ic->op == '-')
1376 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1379 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1383 /* we reached the end */
1384 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1391 /*-----------------------------------------------------------------*/
1392 /* regTypeNum - computes the type & number of registers required */
1393 /*-----------------------------------------------------------------*/
1401 /* for each live range do */
1402 for (sym = hTabFirstItem (liveRanges, &k); sym;
1403 sym = hTabNextItem (liveRanges, &k))
1406 /* if used zero times then no registers needed */
1407 if ((sym->liveTo - sym->liveFrom) == 0)
1411 /* if the live range is a temporary */
1415 /* if the type is marked as a conditional */
1416 if (sym->regType == REG_CND)
1419 /* if used in return only then we don't
1421 if (sym->ruonly || sym->accuse)
1423 if (IS_AGGREGATE (sym->type) || sym->isptr)
1424 sym->type = aggrToPtr (sym->type, FALSE);
1428 /* if the symbol has only one definition &
1429 that definition is a get_pointer and the
1430 pointer we are getting is rematerializable and
1433 if (bitVectnBitsOn (sym->defs) == 1 &&
1434 (ic = hTabItemWithKey (iCodehTab,
1435 bitVectFirstBit (sym->defs))) &&
1437 !IS_BITVAR (sym->etype))
1440 /* if in data space or idata space then try to
1441 allocate pointer register */
1445 /* if not then we require registers */
1446 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1447 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1448 getSize (sym->type));
1452 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1453 printTypeChain (sym->type, stderr);
1454 fprintf (stderr, "\n");
1457 /* determine the type of register required */
1458 if (sym->nRegs == 2 && /* size is two */
1459 IS_PTR (sym->type) && /* is a pointer */
1461 { /* has pointer usage i.e. get/set pointer */
1462 sym->regType = REG_PTR;
1467 /* live accross a function call then gpr else scratch */
1468 if (sym->isLiveFcall)
1469 sym->regType = REG_GPR;
1471 sym->regType = REG_SCR;
1475 /* for the first run we don't provide */
1476 /* registers for true symbols we will */
1477 /* see how things go */
1483 /*-----------------------------------------------------------------*/
1484 /* deallocStackSpil - this will set the stack pointer back */
1485 /*-----------------------------------------------------------------*/
1487 DEFSETFUNC (deallocStackSpil)
1495 /*-----------------------------------------------------------------*/
1496 /* farSpacePackable - returns the packable icode for far variables */
1497 /*-----------------------------------------------------------------*/
1499 farSpacePackable (iCode * ic)
1503 /* go thru till we find a definition for the
1504 symbol on the right */
1505 for (dic = ic->prev; dic; dic = dic->prev)
1508 /* if the definition is a call then no */
1509 if ((dic->op == CALL || dic->op == PCALL) &&
1510 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1515 /* if shift by unknown amount then not */
1516 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1517 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1520 /* if pointer get and size > 1 */
1521 if (POINTER_GET (dic) &&
1522 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1525 if (POINTER_SET (dic) &&
1526 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1529 /* if any three is a true symbol in far space */
1530 if (IC_RESULT (dic) &&
1531 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1532 isOperandInFarSpace (IC_RESULT (dic)))
1535 if (IC_RIGHT (dic) &&
1536 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1537 isOperandInFarSpace (IC_RIGHT (dic)) &&
1538 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1541 if (IC_LEFT (dic) &&
1542 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1543 isOperandInFarSpace (IC_LEFT (dic)) &&
1544 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1547 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1549 if ((dic->op == LEFT_OP ||
1550 dic->op == RIGHT_OP ||
1552 IS_OP_LITERAL (IC_RIGHT (dic)))
1562 /*-----------------------------------------------------------------*/
1563 /* packRegsForAssign - register reduction for assignment */
1564 /*-----------------------------------------------------------------*/
1566 packRegsForAssign (iCode * ic, eBBlock * ebp)
1570 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1571 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1572 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1577 /* find the definition of iTempNN scanning backwards if we find a
1578 a use of the true symbol in before we find the definition then
1580 for (dic = ic->prev; dic; dic = dic->prev)
1583 /* if there is a function call and this is
1584 a parameter & not my parameter then don't pack it */
1585 if ((dic->op == CALL || dic->op == PCALL) &&
1586 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1587 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1596 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1597 IS_OP_VOLATILE (IC_RESULT (dic)))
1603 if (IS_SYMOP (IC_RESULT (dic)) &&
1604 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1606 if (POINTER_SET (dic))
1612 if (IS_SYMOP (IC_RIGHT (dic)) &&
1613 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1614 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1620 if (IS_SYMOP (IC_LEFT (dic)) &&
1621 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1622 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1628 if (POINTER_SET (dic) &&
1629 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1637 return 0; /* did not find */
1639 /* if the result is on stack or iaccess then it must be
1640 the same atleast one of the operands */
1641 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1642 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1645 /* the operation has only one symbol
1646 operator then we can pack */
1647 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1648 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1651 if (!((IC_LEFT (dic) &&
1652 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1654 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1658 /* if in far space & tru symbol then don't */
1659 if ((IS_TRUE_SYMOP (IC_RESULT (ic))) && isOperandInFarSpace (IC_RESULT (ic)))
1661 /* found the definition */
1662 /* replace the result with the result of */
1663 /* this assignment and remove this assignment */
1664 IC_RESULT (dic) = IC_RESULT (ic);
1666 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1668 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1670 /* delete from liverange table also
1671 delete from all the points inbetween and the new
1673 for (sic = dic; sic != ic; sic = sic->next)
1675 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1676 if (IS_ITEMP (IC_RESULT (dic)))
1677 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1680 remiCodeFromeBBlock (ebp, ic);
1681 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1686 /*-----------------------------------------------------------------*/
1687 /* findAssignToSym : scanning backwards looks for first assig found */
1688 /*-----------------------------------------------------------------*/
1690 findAssignToSym (operand * op, iCode * ic)
1694 for (dic = ic->prev; dic; dic = dic->prev)
1697 /* if definition by assignment */
1698 if (dic->op == '=' &&
1699 !POINTER_SET (dic) &&
1700 IC_RESULT (dic)->key == op->key
1701 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1705 /* we are interested only if defined in far space */
1706 /* or in stack space in case of + & - */
1708 /* if assigned to a non-symbol then return
1710 if (!IS_SYMOP (IC_RIGHT (dic)))
1713 /* if the symbol is in far space then
1715 if (isOperandInFarSpace (IC_RIGHT (dic)))
1718 /* for + & - operations make sure that
1719 if it is on the stack it is the same
1720 as one of the three operands */
1721 if ((ic->op == '+' || ic->op == '-') &&
1722 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1725 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1726 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1727 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1735 /* if we find an usage then we cannot delete it */
1736 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1739 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1742 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1746 /* now make sure that the right side of dic
1747 is not defined between ic & dic */
1750 iCode *sic = dic->next;
1752 for (; sic != ic; sic = sic->next)
1753 if (IC_RESULT (sic) &&
1754 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1763 /*-----------------------------------------------------------------*/
1764 /* packRegsForSupport :- reduce some registers for support calls */
1765 /*-----------------------------------------------------------------*/
1767 packRegsForSupport (iCode * ic, eBBlock * ebp)
1770 /* for the left & right operand :- look to see if the
1771 left was assigned a true symbol in far space in that
1772 case replace them */
1773 if (IS_ITEMP (IC_LEFT (ic)) &&
1774 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1776 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1782 /* found it we need to remove it from the
1784 for (sic = dic; sic != ic; sic = sic->next)
1785 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1787 IC_LEFT (ic)->operand.symOperand =
1788 IC_RIGHT (dic)->operand.symOperand;
1789 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1790 remiCodeFromeBBlock (ebp, dic);
1791 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1795 /* do the same for the right operand */
1798 IS_ITEMP (IC_RIGHT (ic)) &&
1799 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1801 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1807 /* if this is a subtraction & the result
1808 is a true symbol in far space then don't pack */
1809 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1811 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1812 if (IN_FARSPACE (SPEC_OCLS (etype)))
1815 /* found it we need to remove it from the
1817 for (sic = dic; sic != ic; sic = sic->next)
1818 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1820 IC_RIGHT (ic)->operand.symOperand =
1821 IC_RIGHT (dic)->operand.symOperand;
1822 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1824 remiCodeFromeBBlock (ebp, dic);
1825 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1832 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1835 /*-----------------------------------------------------------------*/
1836 /* packRegsForOneuse : - will reduce some registers for single Use */
1837 /*-----------------------------------------------------------------*/
1839 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1844 /* if returning a literal then do nothing */
1849 if (ic->op != RETURN)
1852 /* this routine will mark the a symbol as used in one
1853 instruction use only && if the defintion is local
1854 (ie. within the basic block) && has only one definition &&
1855 that definiion is either a return value from a
1856 function or does not contain any variables in
1858 uses = bitVectCopy (OP_USES (op));
1859 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1860 if (!bitVectIsZero (uses)) /* has other uses */
1863 /* if it has only one defintion */
1864 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1865 return NULL; /* has more than one definition */
1867 /* get the that definition */
1869 hTabItemWithKey (iCodehTab,
1870 bitVectFirstBit (OP_DEFS (op)))))
1873 /* found the definition now check if it is local */
1874 if (dic->seq < ebp->fSeq ||
1875 dic->seq > ebp->lSeq)
1876 return NULL; /* non-local */
1878 /* now check if it is the return from
1880 if (dic->op == CALL || dic->op == PCALL)
1882 if (ic->op != SEND && ic->op != RETURN)
1884 OP_SYMBOL (op)->ruonly = 1;
1891 /* otherwise check that the definition does
1892 not contain any symbols in far space */
1893 if (IS_OP_RUONLY (IC_LEFT (ic)) ||
1894 IS_OP_RUONLY (IC_RIGHT (ic)))
1899 /* if pointer set then make sure the pointer
1901 if (POINTER_SET (dic) &&
1902 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1905 if (POINTER_GET (dic) &&
1906 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1911 /* also make sure the intervenening instructions
1912 don't have any thing in far space */
1913 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1916 /* if there is an intervening function call then no */
1917 if (dic->op == CALL || dic->op == PCALL)
1919 /* if pointer set then make sure the pointer
1921 if (POINTER_SET (dic) &&
1922 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1925 if (POINTER_GET (dic) &&
1926 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1929 /* if address of & the result is remat the okay */
1930 if (dic->op == ADDRESS_OF &&
1931 OP_SYMBOL (IC_RESULT (dic))->remat)
1934 /* if operand has size of three or more & this
1935 operation is a '*','/' or '%' then 'b' may
1937 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1938 getSize (operandType (op)) >= 3)
1941 /* if left or right or result is in far space */
1942 if (IS_OP_RUONLY (IC_LEFT (dic)) ||
1943 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1944 IS_OP_RUONLY (IC_RESULT (dic)))
1950 OP_SYMBOL (op)->ruonly = 1;
1955 /*-----------------------------------------------------------------*/
1956 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1957 /*-----------------------------------------------------------------*/
1959 isBitwiseOptimizable (iCode * ic)
1961 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
1962 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1964 /* bitwise operations are considered optimizable
1965 under the following conditions (Jean-Louis VERN)
1977 if (IS_LITERAL (rtype) ||
1978 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
1984 /*-----------------------------------------------------------------*/
1985 /* packForPush - hueristics to reduce iCode for pushing */
1986 /*-----------------------------------------------------------------*/
1988 packForPush (iCode * ic, eBBlock * ebp)
1992 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
1995 /* must have only definition & one usage */
1996 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
1997 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2000 /* find the definition */
2001 if (!(dic = hTabItemWithKey (iCodehTab,
2002 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2005 if (dic->op != '=' || POINTER_SET (dic))
2008 /* we now we know that it has one & only one def & use
2009 and the that the definition is an assignment */
2010 IC_LEFT (ic) = IC_RIGHT (dic);
2012 remiCodeFromeBBlock (ebp, dic);
2013 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2016 /*-----------------------------------------------------------------*/
2017 /* packRegisters - does some transformations to reduce register */
2019 /*-----------------------------------------------------------------*/
2021 packRegisters (eBBlock * ebp)
2031 /* look for assignments of the form */
2032 /* iTempNN = TRueSym (someoperation) SomeOperand */
2034 /* TrueSym := iTempNN:1 */
2035 for (ic = ebp->sch; ic; ic = ic->next)
2039 /* find assignment of the form TrueSym := iTempNN:1 */
2040 if (ic->op == '=' && !POINTER_SET (ic))
2041 change += packRegsForAssign (ic, ebp);
2048 for (ic = ebp->sch; ic; ic = ic->next)
2051 /* if this is an itemp & result of a address of a true sym
2052 then mark this as rematerialisable */
2053 if (ic->op == ADDRESS_OF &&
2054 IS_ITEMP (IC_RESULT (ic)) &&
2055 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2056 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2057 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2060 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2061 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2062 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2066 /* if straight assignment then carry remat flag if
2067 this is the only definition */
2068 if (ic->op == '=' &&
2069 !POINTER_SET (ic) &&
2070 IS_SYMOP (IC_RIGHT (ic)) &&
2071 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2072 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2075 OP_SYMBOL (IC_RESULT (ic))->remat =
2076 OP_SYMBOL (IC_RIGHT (ic))->remat;
2077 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2078 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2081 /* if this is a +/- operation with a rematerizable
2082 then mark this as rematerializable as well only
2083 if the literal value is within the range -255 and + 255
2084 the assembler cannot handle it other wise */
2085 if ((ic->op == '+' || ic->op == '-') &&
2087 (IS_SYMOP (IC_LEFT (ic)) &&
2088 IS_ITEMP (IC_RESULT (ic)) &&
2089 OP_SYMBOL (IC_LEFT (ic))->remat &&
2090 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2091 IS_OP_LITERAL (IC_RIGHT (ic))))
2094 int i = (int) operandLitValue (IC_RIGHT (ic));
2095 if (i < 255 && i > -255)
2097 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2098 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2099 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2103 /* mark the pointer usages */
2104 if (POINTER_SET (ic))
2105 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2107 if (POINTER_GET (ic))
2108 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2110 /* if the condition of an if instruction
2111 is defined in the previous instruction then
2112 mark the itemp as a conditional */
2113 if ((IS_CONDITIONAL (ic) ||
2114 ((ic->op == BITWISEAND ||
2117 isBitwiseOptimizable (ic))) &&
2118 ic->next && ic->next->op == IFX &&
2119 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2120 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2123 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2127 /* some cases the redundant moves can
2128 can be eliminated for return statements */
2129 if ((ic->op == RETURN || ic->op == SEND))
2130 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2132 /* if this is cast for intergral promotion then
2133 check if only use of the definition of the
2134 operand being casted/ if yes then replace
2135 the result of that arithmetic operation with
2136 this result and get rid of the cast */
2139 sym_link *fromType = operandType (IC_RIGHT (ic));
2140 sym_link *toType = operandType (IC_LEFT (ic));
2142 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2143 getSize (fromType) != getSize (toType) &&
2144 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2147 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2150 if (IS_ARITHMETIC_OP (dic))
2152 IC_RESULT (dic) = IC_RESULT (ic);
2153 remiCodeFromeBBlock (ebp, ic);
2154 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2158 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2164 /* if the type from and type to are the same
2165 then if this is the only use then packit */
2166 if (checkType (operandType (IC_RIGHT (ic)),
2167 operandType (IC_LEFT (ic))) == 1)
2169 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2172 IC_RESULT (dic) = IC_RESULT (ic);
2173 remiCodeFromeBBlock (ebp, ic);
2174 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2183 /*-----------------------------------------------------------------*/
2184 /* preAssignParms - we have a leaf function preassign registers */
2185 /*-----------------------------------------------------------------*/
2187 preAssignParms (iCode * ic)
2190 /* look for receives and assign registers
2191 to the result of the receives */
2194 /* if it is a receive */
2195 if (ic->op == RECEIVE)
2197 symbol *r = OP_SYMBOL (IC_RESULT (ic));
2198 int size = getSize (r->type);
2199 if (r->regType == REG_GPR || r->regType == REG_SCR)
2204 r->regs[j++] = ®sAVR[i++];
2205 regsAVR[i - 1].isFree = 0;
2207 /* put in the regassigned vector */
2208 _G.regAssigned = bitVectSetBit (_G.regAssigned, r->key);
2212 /* not a GPR then we should mark as free */
2215 regsAVR[i++].isFree = 1;
2221 /* mark anything remaining as free */
2222 while (i <= R23_IDX)
2223 regsAVR[i++].isFree = 1;
2226 /*-----------------------------------------------------------------*/
2227 /* setdefaultRegs - do setup stuff for register allocation */
2228 /*-----------------------------------------------------------------*/
2230 setDefaultRegs (eBBlock ** ebbs, int count)
2234 /* if no pointer registers required in this function
2235 then mark r26-27 & r30-r31 as GPR & free */
2236 regsAVR[R26_IDX].isFree =
2237 regsAVR[R27_IDX].isFree =
2238 regsAVR[R30_IDX].isFree =
2239 regsAVR[R31_IDX].isFree = 1;
2243 regsAVR[R26_IDX].type =
2244 regsAVR[R27_IDX].type =
2245 regsAVR[R30_IDX].type =
2246 regsAVR[R31_IDX].type = REG_GPR;
2250 regsAVR[R26_IDX].type =
2251 regsAVR[R27_IDX].type =
2252 regsAVR[R30_IDX].type =
2253 regsAVR[R31_IDX].type = REG_PTR;
2256 /* registers 0-1 / 24-25 used as scratch */
2257 regsAVR[R0_IDX].isFree =
2258 regsAVR[R1_IDX].isFree =
2259 regsAVR[R24_IDX].isFree =
2260 regsAVR[R25_IDX].isFree = 0;
2262 /* if this has no function calls then we need
2263 to do something special
2264 a) pre-assign registers to parameters RECEIVE
2265 b) mark the remaining parameter regs as free */
2266 if (!currFunc->hasFcall)
2268 /* mark the parameter regs as GPR */
2269 for (i = R16_IDX; i <= R23_IDX; i++)
2271 regsAVR[i].type = REG_SCR;
2272 regsAVR[i].isFree = 1;
2274 preAssignParms (ebbs[0]->sch);
2279 /* otherwise mark them as free scratch */
2280 for (i = R16_IDX; i <= R23_IDX; i++)
2282 regsAVR[i].type = REG_SCR;
2283 regsAVR[i].isFree = 1;
2287 /* Y - is not allocated (it is the stack frame) */
2288 regsAVR[R28_IDX].isFree =
2289 regsAVR[R28_IDX].isFree = 0;
2292 /*-----------------------------------------------------------------*/
2293 /* assignRegisters - assigns registers to each live range as need */
2294 /*-----------------------------------------------------------------*/
2296 avr_assignRegisters (eBBlock ** ebbs, int count)
2301 setToNull ((void *) &_G.funcrUsed);
2302 avr_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2304 /* change assignments this will remove some
2305 live ranges reducing some register pressure */
2306 for (i = 0; i < count; i++)
2307 packRegisters (ebbs[i]);
2309 if (options.dump_pack)
2310 dumpEbbsToFileExt (".dumppack", ebbs, count);
2312 /* first determine for each live range the number of
2313 registers & the type of registers required for each */
2316 /* setup the default registers */
2317 setDefaultRegs (ebbs, count);
2319 /* and serially allocate registers */
2320 serialRegAssign (ebbs, count);
2322 /* if stack was extended then tell the user */
2325 /* werror(W_TOOMANY_SPILS,"stack", */
2326 /* _G.stackExtend,currFunc->name,""); */
2332 /* werror(W_TOOMANY_SPILS,"data space", */
2333 /* _G.dataExtend,currFunc->name,""); */
2337 /* after that create the register mask
2338 for each of the instruction */
2339 createRegMask (ebbs, count);
2341 /* redo that offsets for stacked automatic variables */
2342 redoStackOffsets ();
2344 if (options.dump_rassgn)
2345 dumpEbbsToFileExt (".dumprassgn", ebbs, count);
2347 /* now get back the chain */
2348 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2352 /* for (; ic ; ic = ic->next) */
2353 /* piCode(ic,stdout); */
2354 /* free up any _G.stackSpil locations allocated */
2355 applyToSet (_G.stackSpil, deallocStackSpil);
2357 setToNull ((void **) &_G.stackSpil);
2358 setToNull ((void **) &_G.spiltSet);
2359 /* mark all registers as free */