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 *);
48 bitVect *funcrUsed; /* registers used in a function */
53 /* Shared with gen.c */
54 int avr_ptrRegReq; /* pointer register required */
58 {REG_GPR|REG_PAIR, R0_IDX, REG_GPR|REG_PAIR, "r0", "r0", "", 0, 0, 0}, /* scratch */
59 {REG_GPR, R1_IDX, REG_GPR , "r1", "r1", "", 0, 0, 0}, /* scratch */
60 {REG_GPR|REG_PAIR, R2_IDX, REG_GPR|REG_PAIR, "r2", "r2", "", 0, 1, 1}, /* gpr */
61 {REG_GPR, R3_IDX, REG_GPR , "r3", "r3", "", 0, 1, 1}, /* gpr */
62 {REG_GPR|REG_PAIR, R4_IDX, REG_GPR|REG_PAIR, "r4", "r4", "", 0, 1, 1}, /* gpr */
63 {REG_GPR, R5_IDX, REG_GPR , "r5", "r5", "", 0, 1, 1}, /* gpr */
64 {REG_GPR|REG_PAIR, R6_IDX, REG_GPR|REG_PAIR, "r6", "r6", "", 0, 1, 1}, /* gpr */
65 {REG_GPR, R7_IDX, REG_GPR , "r7", "r7", "", 0, 1, 1}, /* gpr */
66 {REG_GPR|REG_PAIR, R8_IDX, REG_GPR|REG_PAIR, "r8", "r8", "", 0, 1, 1}, /* gpr */
67 {REG_GPR, R9_IDX, REG_GPR , "r9", "r9", "", 0, 1, 1}, /* gpr */
68 {REG_GPR|REG_PAIR, R10_IDX,REG_GPR|REG_PAIR, "r10", "r10","",0, 1, 1}, /* gpr */
69 {REG_GPR, R11_IDX,REG_GPR , "r11", "r11","",0, 1, 1}, /* gpr */
70 {REG_GPR|REG_PAIR, R12_IDX,REG_GPR|REG_PAIR, "r12", "r12","",0, 1, 1}, /* gpr */
71 {REG_GPR, R13_IDX,REG_GPR , "r13", "r13","",0, 1, 1}, /* gpr */
72 {REG_GPR|REG_PAIR, R14_IDX,REG_GPR|REG_PAIR, "r14", "r14","",0, 1, 1}, /* gpr */
73 {REG_GPR, R15_IDX,REG_GPR , "r15", "r15","",0, 1, 1}, /* gpr */
74 {REG_GPR|REG_PAIR, R16_IDX,REG_GPR|REG_PAIR, "r16", "r16","",0, 1, 0}, /* parm/gpr */
75 {REG_GPR, R17_IDX,REG_GPR , "r17", "r17","",0, 1, 0}, /* parm/gpr */
76 {REG_GPR|REG_PAIR, R18_IDX,REG_GPR|REG_PAIR, "r18", "r18","",0, 1, 0}, /* parm/gpr */
77 {REG_GPR, R19_IDX,REG_GPR , "r19", "r19","",0, 1, 0}, /* parm/gpr */
78 {REG_GPR|REG_PAIR, R20_IDX,REG_GPR|REG_PAIR, "r20", "r20","",0, 1, 0}, /* parm/gpr */
79 {REG_GPR, R21_IDX,REG_GPR , "r21", "r21","",0, 1, 0}, /* parm/gpr */
80 {REG_GPR|REG_PAIR, R22_IDX,REG_GPR|REG_PAIR, "r22", "r22","",0, 1, 0}, /* parm/gpr */
81 {REG_GPR, R23_IDX,REG_GPR , "r23", "r23","",0, 1, 0}, /* parm/gpr */
82 {REG_GPR|REG_PAIR, R24_IDX,REG_GPR|REG_PAIR, "r24", "r24","",0, 0, 0}, /* scratch */
83 {REG_GPR, R25_IDX,REG_GPR , "r25", "r25","",0, 0, 0}, /* scratch */
84 {REG_GPR|REG_PAIR, R26_IDX,REG_GPR|REG_PAIR, "r26", "r26","",0, 1, 1}, /* used as pointer reg X */
85 {REG_GPR, R27_IDX,REG_GPR , "r27", "r27","",0, 1, 1}, /* used as pointer reg X */
86 {REG_GPR|REG_PAIR, R28_IDX,REG_GPR|REG_PAIR, "r28", "r28","",0, 1, 0}, /* stack frame Y */
87 {REG_GPR, R29_IDX,REG_GPR , "r29", "r29","",0, 1, 0}, /* stack frame Y */
88 {REG_GPR|REG_PAIR, R30_IDX,REG_GPR|REG_PAIR, "r30", "r30","",0, 1, 1}, /* used as pointer reg Z */
89 {REG_GPR, R31_IDX,REG_GPR , "r31", "r31","",0, 1, 1}, /* used as pointer reg Z */
90 {REG_PTR, X_IDX, REG_PTR, "X", "X", "", 0, 1, 0},
91 {REG_PTR, Z_IDX, REG_PTR, "Z", "Z", "", 0, 1, 0},
94 int avr_fReg = 0; /* first allocatable register */
96 static void spillThis (symbol *);
100 /*-----------------------------------------------------------------*/
101 /* findAssignToSym : scanning backwards looks for first assig found */
102 /*-----------------------------------------------------------------*/
104 findAssignToSym (operand * op, iCode * ic)
108 for (dic = ic->prev; dic; dic = dic->prev) {
110 /* if definition by assignment */
111 if (dic->op == '=' &&
112 !POINTER_SET (dic) && IC_RESULT (dic)->key == op->key
113 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
116 /* we are interested only if defined in far space */
117 /* or in stack space in case of + & - */
119 /* if assigned to a non-symbol then return
121 if (!IS_SYMOP (IC_RIGHT (dic)))
124 /* if the symbol is in far space then
126 if (isOperandInFarSpace (IC_RIGHT (dic)))
129 /* for + & - operations make sure that
130 if it is on the stack it is the same
131 as one of the three operands */
132 if ((ic->op == '+' || ic->op == '-') &&
133 OP_SYMBOL (IC_RIGHT (dic))->onStack) {
135 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key
136 && IC_LEFT (ic)->key !=
138 && IC_RIGHT (ic)->key !=
139 IC_RIGHT (dic)->key) return NULL;
146 /* if we find an usage then we cannot delete it */
147 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
150 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
153 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
157 /* now make sure that the right side of dic
158 is not defined between ic & dic */
160 iCode *sic = dic->next;
162 for (; sic != ic; sic = sic->next)
163 if (IC_RESULT (sic) &&
164 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
173 /*-----------------------------------------------------------------*/
174 /* packForPush - hueristics to reduce iCode for pushing */
175 /*-----------------------------------------------------------------*/
177 packForPush (iCode * ic, eBBlock * ebp)
181 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
184 /* must have only definition & one usage */
185 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
186 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
189 /* find the definition */
190 if (!(dic = hTabItemWithKey (iCodehTab,
191 bitVectFirstBit (OP_DEFS
195 if (dic->op != '=' || POINTER_SET (dic))
198 /* we now we know that it has one & only one def & use
199 and the that the definition is an assignment */
200 IC_LEFT (ic) = IC_RIGHT (dic);
202 remiCodeFromeBBlock (ebp, dic);
203 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
206 /*-----------------------------------------------------------------*/
207 /* packRegsForSupport :- reduce some registers for support calls */
208 /*-----------------------------------------------------------------*/
210 packRegsForSupport (iCode * ic, eBBlock * ebp)
213 /* for the left & right operand :- look to see if the
214 left was assigned a true symbol in far space in that
216 if (IS_ITEMP (IC_LEFT (ic)) &&
217 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq) {
218 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
224 /* found it we need to remove it from the
226 for (sic = dic; sic != ic; sic = sic->next)
227 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
229 IC_LEFT (ic)->operand.symOperand =
230 IC_RIGHT (dic)->operand.symOperand;
231 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
232 remiCodeFromeBBlock (ebp, dic);
233 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
237 /* do the same for the right operand */
240 IS_ITEMP (IC_RIGHT (ic)) &&
241 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq) {
242 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
248 /* if this is a subtraction & the result
249 is a true symbol in far space then don't pack */
250 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic))) {
252 getSpec (operandType (IC_RESULT (dic)));
253 if (IN_FARSPACE (SPEC_OCLS (etype)))
256 /* found it we need to remove it from the
258 for (sic = dic; sic != ic; sic = sic->next)
259 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
261 IC_RIGHT (ic)->operand.symOperand =
262 IC_RIGHT (dic)->operand.symOperand;
263 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
265 remiCodeFromeBBlock (ebp, dic);
266 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
273 /*-----------------------------------------------------------------*/
274 /* farSpacePackable - returns the packable icode for far variables */
275 /*-----------------------------------------------------------------*/
277 farSpacePackable (iCode * ic)
281 /* go thru till we find a definition for the
282 symbol on the right */
283 for (dic = ic->prev; dic; dic = dic->prev) {
285 /* if the definition is a call then no */
286 if ((dic->op == CALL || dic->op == PCALL) &&
287 IC_RESULT (dic)->key == IC_RIGHT (ic)->key) {
291 /* if shift by unknown amount then not */
292 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
293 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
296 /* if pointer get and size > 1 */
297 if (POINTER_GET (dic) &&
298 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) >
301 if (POINTER_SET (dic) &&
302 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE))
306 /* if any three is a true symbol in far space */
307 if (IC_RESULT (dic) &&
308 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
309 isOperandInFarSpace (IC_RESULT (dic)))
312 if (IC_RIGHT (dic) &&
313 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
314 isOperandInFarSpace (IC_RIGHT (dic)) &&
315 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
319 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
320 isOperandInFarSpace (IC_LEFT (dic)) &&
321 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
324 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic))) {
325 if ((dic->op == LEFT_OP ||
326 dic->op == RIGHT_OP ||
328 IS_OP_LITERAL (IC_RIGHT (dic))) return NULL;
337 /*-----------------------------------------------------------------*/
338 /* rematStr - returns the rematerialized string for a remat var */
339 /*-----------------------------------------------------------------*/
341 rematStr (symbol * sym)
344 iCode *ic = sym->rematiCode;
348 /* if plus or minus print the right hand side */
349 if (ic->op == '+' || ic->op == '-') {
350 sprintf (s, "0x%04x %c ",
351 (int) operandLitValue (IC_RIGHT (ic)),
354 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
358 /* we reached the end */
359 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
366 /*-----------------------------------------------------------------*/
367 /* isSpiltOnStack - returns true if the spil location is on stack */
368 /*-----------------------------------------------------------------*/
370 isSpiltOnStack (symbol * sym)
381 if (!sym->usl.spillLoc)
384 etype = getSpec (sym->usl.spillLoc->type);
385 if (IN_STACK (etype))
391 /*-----------------------------------------------------------------*/
392 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
393 /*-----------------------------------------------------------------*/
395 spillLRWithPtrReg (symbol * forSym)
398 regs *X, *Z, *X1, *Z1;
401 if (!_G.regAssigned || bitVectIsZero (_G.regAssigned))
404 X = avr_regWithIdx (R26_IDX);
405 X1= avr_regWithIdx (R27_IDX);
406 Z = avr_regWithIdx (R30_IDX);
407 Z1= avr_regWithIdx (R31_IDX);
409 /* for all live ranges */
410 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
411 lrsym = hTabNextItem (liveRanges, &k)) {
414 /* if no registers assigned to it or
416 /* if it does not overlap with this then
417 not need to spill it */
419 if (lrsym->isspilt || !lrsym->nRegs ||
420 (lrsym->liveTo < forSym->liveFrom)) continue;
422 /* go thru the registers : if it is either
423 r0 or r1 then spil it */
424 for (j = 0; j < lrsym->nRegs; j++)
425 if (lrsym->regs[j] == X || lrsym->regs[j] == Z ||
426 lrsym->regs[j] == X1 || lrsym->regs[j] == Z1) {
435 /*-----------------------------------------------------------------*/
436 /* allocReg - allocates register of given type */
437 /*-----------------------------------------------------------------*/
439 allocReg (short type)
443 for (i = avr_fReg; i < avr_nRegs; i++) {
445 /* if type is given as 0 then any
446 free register will do */
447 if (!type && regsAVR[i].isFree) {
448 regsAVR[i].isFree = 0;
451 bitVectSetBit (currFunc->regsUsed, i);
455 /* other wise look for specific type
457 if (regsAVR[i].isFree && (regsAVR[i].type & type)) {
458 regsAVR[i].isFree = 0;
461 bitVectSetBit (currFunc->regsUsed, i);
468 /*-----------------------------------------------------------------*/
469 /* allocRegPair - allocates register pair of given */
470 /*-----------------------------------------------------------------*/
472 allocRegPair (short type)
476 for (i = avr_fReg; i < avr_nRegs; i++) {
478 /* look for specific type of register pair */
479 if (regsAVR[i].isFree && (regsAVR[i].type & type)
480 && (regsAVR[i].type & REG_PAIR) && regsAVR[i+1].isFree) {
482 regsAVR[i].isFree = 0;
483 regsAVR[i+1].isFree = 0;
486 bitVectSetBit (currFunc->regsUsed, i);
488 bitVectSetBit (currFunc->regsUsed, i+1);
496 /*-----------------------------------------------------------------*/
497 /* avr_regWithIdx - returns pointer to register wit index number */
498 /*-----------------------------------------------------------------*/
500 avr_regWithIdx (int idx)
504 for (i = 0; i < avr_nRegs; i++)
505 if (regsAVR[i].rIdx == idx)
508 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "regWithIdx not found");
512 /*-----------------------------------------------------------------*/
513 /* freeReg - frees a register */
514 /*-----------------------------------------------------------------*/
522 /*-----------------------------------------------------------------*/
523 /* nFreeRegs - returns number of free registers */
524 /*-----------------------------------------------------------------*/
531 for (i = avr_fReg; i < avr_nRegs; i++)
532 if (regsAVR[i].isFree && regsAVR[i].type & type)
537 /*-----------------------------------------------------------------*/
538 /* nfreeRegsType - free registers with type */
539 /*-----------------------------------------------------------------*/
541 nfreeRegsType (int type)
544 if (type == REG_PTR) {
545 if ((nfr = nFreeRegs (type)) == 0)
546 return nFreeRegs (REG_GPR);
549 return nFreeRegs (type);
552 /*-----------------------------------------------------------------*/
553 /* computeSpillable - given a point find the spillable live ranges */
554 /*-----------------------------------------------------------------*/
556 computeSpillable (iCode * ic)
560 /* spillable live ranges are those that are live at this
561 point . the following categories need to be subtracted
563 a) - those that are already spilt
564 b) - if being used by this one
565 c) - defined by this one */
567 spillable = bitVectCopy (ic->rlive);
568 spillable = bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
569 spillable = bitVectCplAnd (spillable, ic->uses); /* used in this one */
570 bitVectUnSetBit (spillable, ic->defKey);
571 spillable = bitVectIntersect (spillable, _G.regAssigned);
576 /*-----------------------------------------------------------------*/
577 /* noSpilLoc - return true if a variable has no spil location */
578 /*-----------------------------------------------------------------*/
580 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
582 return (sym->usl.spillLoc ? 0 : 1);
585 /*-----------------------------------------------------------------*/
586 /* hasSpilLoc - will return 1 if the symbol has spil location */
587 /*-----------------------------------------------------------------*/
589 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
591 return (sym->usl.spillLoc ? 1 : 0);
594 /*-----------------------------------------------------------------*/
595 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
596 /* but is not used as a pointer */
597 /*-----------------------------------------------------------------*/
599 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
601 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
604 /*-----------------------------------------------------------------*/
605 /* rematable - will return 1 if the remat flag is set */
606 /*-----------------------------------------------------------------*/
608 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
613 /*-----------------------------------------------------------------*/
614 /* notUsedInRemaining - not used or defined in remain of the block */
615 /*-----------------------------------------------------------------*/
617 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
619 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
620 allDefsOutOfRange (sym->defs, ic->seq, ebp->lSeq));
623 /*-----------------------------------------------------------------*/
624 /* allLRs - return true for all */
625 /*-----------------------------------------------------------------*/
627 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
632 /*-----------------------------------------------------------------*/
633 /* liveRangesWith - applies function to a given set of live range */
634 /*-----------------------------------------------------------------*/
636 liveRangesWith (bitVect * lrs,
637 int (func) (symbol *, eBBlock *, iCode *),
638 eBBlock * ebp, iCode * ic)
643 if (!lrs || !lrs->size)
646 for (i = 1; i < lrs->size; i++) {
648 if (!bitVectBitValue (lrs, i))
651 /* if we don't find it in the live range
652 hash table we are in serious trouble */
653 if (!(sym = hTabItemWithKey (liveRanges, i))) {
654 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
655 "liveRangesWith could not find liveRange");
659 if (func (sym, ebp, ic)
660 && bitVectBitValue (_G.regAssigned,
661 sym->key)) addSetHead (&rset, sym);
668 /*-----------------------------------------------------------------*/
669 /* leastUsedLR - given a set determines which is the least used */
670 /*-----------------------------------------------------------------*/
672 leastUsedLR (set * sset)
674 symbol *sym = NULL, *lsym = NULL;
676 sym = lsym = setFirstItem (sset);
681 for (; lsym; lsym = setNextItem (sset)) {
683 /* if usage is the same then prefer
684 the spill the smaller of the two */
685 if (lsym->used == sym->used)
686 if (getSize (lsym->type) < getSize (sym->type))
690 if (lsym->used < sym->used)
695 setToNull ((void *) &sset);
700 /*-----------------------------------------------------------------*/
701 /* noOverLap - will iterate through the list looking for over lap */
702 /*-----------------------------------------------------------------*/
704 noOverLap (set * itmpStack, symbol * fsym)
709 for (sym = setFirstItem (itmpStack); sym;
710 sym = setNextItem (itmpStack)) {
711 if (sym->liveTo > fsym->liveFrom)
719 /*-----------------------------------------------------------------*/
720 /* isFree - will return 1 if the a free spil location is found */
721 /*-----------------------------------------------------------------*/
726 V_ARG (symbol **, sloc);
727 V_ARG (symbol *, fsym);
729 /* if already found */
733 /* if it is free && and the itmp assigned to
734 this does not have any overlapping live ranges
735 with the one currently being assigned and
736 the size can be accomodated */
738 noOverLap (sym->usl.itmpStack, fsym) &&
739 getSize (sym->type) >= getSize (fsym->type)) {
747 /*-----------------------------------------------------------------*/
748 /* createStackSpil - create a location on the stack to spil */
749 /*-----------------------------------------------------------------*/
751 createStackSpil (symbol * sym)
754 int useXstack, model, noOverlay;
759 /* first go try and find a free one that is already
760 existing on the stack */
761 if (applyToSet (_G.stackSpil, isFree, &sloc, sym)) {
762 /* found a free one : just update & return */
763 sym->usl.spillLoc = sloc;
766 addSetHead (&sloc->usl.itmpStack, sym);
770 /* could not then have to create one , this is the hard part
771 we need to allocate this on the stack : this is really a
772 hack!! but cannot think of anything better at this time */
774 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >=
775 sizeof (slocBuffer)) {
777 "***Internal error: slocBuffer overflowed: %s:%d\n",
782 sloc = newiTemp (slocBuffer);
784 /* set the type to the spilling symbol */
785 sloc->type = copyLinkChain (sym->type);
786 sloc->etype = getSpec (sloc->type);
787 SPEC_SCLS (sloc->etype) = S_AUTO;
788 SPEC_EXTR (sloc->etype) = 0;
790 /* we don't allow it to be allocated`
791 onto the external stack since : so we
792 temporarily turn it off ; we also
793 turn off memory model to prevent
794 the spil from going to the external storage
795 and turn off overlaying
798 useXstack = options.useXstack;
799 model = options.model;
800 noOverlay = options.noOverlay;
801 stackAuto = options.stackAuto;
802 options.noOverlay = 1;
803 options.model = options.useXstack = 0;
807 options.useXstack = useXstack;
808 options.model = model;
809 options.noOverlay = noOverlay;
810 options.stackAuto = stackAuto;
811 sloc->isref = 1; /* to prevent compiler warning */
813 /* if it is on the stack then update the stack */
814 if (IN_STACK (sloc->etype)) {
815 currFunc->stack += getSize (sloc->type);
816 _G.stackExtend += getSize (sloc->type);
819 _G.dataExtend += getSize (sloc->type);
821 /* add it to the _G.stackSpil set */
822 addSetHead (&_G.stackSpil, sloc);
823 sym->usl.spillLoc = sloc;
826 /* add it to the set of itempStack set
827 of the spill location */
828 addSetHead (&sloc->usl.itmpStack, sym);
832 /*-----------------------------------------------------------------*/
833 /* spillThis - spils a specific operand */
834 /*-----------------------------------------------------------------*/
836 spillThis (symbol * sym)
839 /* if this is rematerializable or has a spillLocation
840 we are okay, else we need to create a spillLocation
842 if (!(sym->remat || sym->usl.spillLoc))
843 createStackSpil (sym);
846 /* mark it has spilt & put it in the spilt set */
848 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
850 bitVectUnSetBit (_G.regAssigned, sym->key);
852 for (i = 0; i < sym->nRegs; i++)
855 freeReg (sym->regs[i]);
859 if (sym->usl.spillLoc && !sym->remat)
860 sym->usl.spillLoc->allocreq = 1;
864 /*-----------------------------------------------------------------*/
865 /* selectSpil - select a iTemp to spil : rather a simple procedure */
866 /*-----------------------------------------------------------------*/
868 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
870 bitVect *lrcs = NULL;
874 /* get the spillable live ranges */
875 lrcs = computeSpillable (ic);
877 /* get all live ranges that are rematerizable */
878 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic))) {
880 /* return the least used of these */
881 return leastUsedLR (selectS);
884 /* if the symbol is local to the block then */
885 if (forSym->liveTo < ebp->lSeq) {
887 /* check if there are any live ranges allocated
888 to registers that are not used in this block */
891 liveRangesWith (lrcs, notUsedInBlock, ebp, ic))) {
892 sym = leastUsedLR (selectS);
893 /* if this is not rematerializable */
901 /* check if there are any live ranges that not
902 used in the remainder of the block */
905 liveRangesWith (lrcs, notUsedInRemaining, ebp, ic))) {
906 sym = leastUsedLR (selectS);
917 /* find live ranges with spillocation && not used as pointers */
918 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic))) {
920 sym = leastUsedLR (selectS);
921 /* mark this as allocation required */
922 sym->usl.spillLoc->allocreq = 1;
926 /* find live ranges with spillocation */
927 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic))) {
929 sym = leastUsedLR (selectS);
930 sym->usl.spillLoc->allocreq = 1;
934 /* couldn't find then we need to create a spil
935 location on the stack , for which one? the least
937 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic))) {
939 /* return a created spil location */
940 sym = createStackSpil (leastUsedLR (selectS));
941 sym->usl.spillLoc->allocreq = 1;
945 /* this is an extreme situation we will spill
946 this one : happens very rarely but it does happen */
952 /*-----------------------------------------------------------------*/
953 /* spilSomething - spil some variable & mark registers as free */
954 /*-----------------------------------------------------------------*/
956 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
961 /* get something we can spil */
962 ssym = selectSpil (ic, ebp, forSym);
964 /* mark it as spilt */
966 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
968 /* mark it as not register assigned &
969 take it away from the set */
970 bitVectUnSetBit (_G.regAssigned, ssym->key);
972 /* mark the registers as free */
973 for (i = 0; i < ssym->nRegs; i++)
975 freeReg (ssym->regs[i]);
977 /* if this was a block level spil then insert push & pop
978 at the start & end of block respectively */
979 if (ssym->blockSpil) {
980 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
981 /* add push to the start of the block */
982 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
983 ebp->sch->next : ebp->sch));
984 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
985 /* add pop to the end of the block */
986 addiCodeToeBBlock (ebp, nic, NULL);
989 /* if spilt because not used in the remainder of the
990 block then add a push before this instruction and
991 a pop at the end of the block */
992 if (ssym->remainSpil) {
994 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
995 /* add push just before this instruction */
996 addiCodeToeBBlock (ebp, nic, ic);
998 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
999 /* add pop to the end of the block */
1000 addiCodeToeBBlock (ebp, nic, NULL);
1009 /*-----------------------------------------------------------------*/
1010 /* getRegPtr - will try for PTR if not a GPR type if not spil */
1011 /*-----------------------------------------------------------------*/
1013 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
1018 /* try for a ptr type */
1019 if ((reg = allocReg (REG_PTR|REG_PAIR)))
1022 /* try for gpr type / pair */
1023 if ((reg = allocReg (REG_GPR|REG_PAIR)))
1026 /* try for gpr type */
1027 if ((reg = allocReg (REG_GPR)))
1030 /* we have to spil */
1031 if (!spilSomething (ic, ebp, sym))
1034 /* this looks like an infinite loop but
1035 in reality selectSpil will abort */
1039 /*-----------------------------------------------------------------*/
1040 /* getRegScr - will try for SCR if not a GPR type if not spil */
1041 /*-----------------------------------------------------------------*/
1043 getRegScr (iCode * ic, eBBlock * ebp, symbol * sym)
1049 /* try for a scratch non-pair */
1050 if ((reg = allocReg (REG_SCR)))
1053 if ((reg = allocReg (REG_GPR)))
1056 /* we have to spil */
1057 if (!spilSomething (ic, ebp, sym))
1060 /* this looks like an infinite loop but
1061 in really selectSpil will abort */
1065 /*-----------------------------------------------------------------*/
1066 /* getRegGpr - will try for GPR if not spil */
1067 /*-----------------------------------------------------------------*/
1069 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym )
1074 /* try for gpr type */
1075 if ((reg = allocReg (REG_GPR)))
1079 if ((reg = allocReg (REG_PTR)))
1082 /* we have to spil */
1083 if (!spilSomething (ic, ebp, sym))
1086 /* this looks like an infinite loop but
1087 in reality selectSpil will abort */
1091 /*-----------------------------------------------------------------*/
1092 /* symHasReg - symbol has a given register */
1093 /*-----------------------------------------------------------------*/
1095 symHasReg (symbol * sym, regs * reg)
1099 for (i = 0; i < sym->nRegs; i++)
1100 if (sym->regs[i] == reg)
1106 /*-----------------------------------------------------------------*/
1107 /* deassignLRs - check the live to and if they have registers & are */
1108 /* not spilt then free up the registers */
1109 /*-----------------------------------------------------------------*/
1111 deassignLRs (iCode * ic, eBBlock * ebp)
1117 for (sym = hTabFirstItem (liveRanges, &k); sym;
1118 sym = hTabNextItem (liveRanges, &k)) {
1120 symbol *psym = NULL;
1121 /* if it does not end here */
1122 if (sym->liveTo > ic->seq)
1125 /* if it was spilt on stack then we can
1126 mark the stack spil location as free */
1128 if (sym->stackSpil) {
1129 sym->usl.spillLoc->isFree = 1;
1135 if (!bitVectBitValue (_G.regAssigned, sym->key))
1138 /* special case check if this is an IFX &
1139 the privious one was a pop and the
1140 previous one was not spilt then keep track
1142 if (ic->op == IFX && ic->prev &&
1143 ic->prev->op == IPOP &&
1144 !ic->prev->parmPush &&
1145 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1146 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1151 bitVectUnSetBit (_G.regAssigned, sym->key);
1153 /* if the result of this one needs registers
1154 and does not have it then assign it right
1156 if (IC_RESULT (ic) && !(SKIP_IC2 (ic) || /* not a special icode */
1157 ic->op == JUMPTABLE ||
1162 POINTER_SET (ic)) &&
1163 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1164 result->liveTo > ic->seq && /* and will live beyond this */
1165 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1166 result->regType == sym->regType && /* same register types */
1167 result->nRegs && /* which needs registers */
1168 !result->isspilt && /* and does not already have them */
1170 !bitVectBitValue (_G.regAssigned, result->key) &&
1171 /* the number of free regs + number of regs in this LR
1172 can accomodate the what result Needs */
1173 ((nfreeRegsType (result->regType) + sym->nRegs) >= result->nRegs)) {
1175 for (i = 0; i < result->nRegs; i++) {
1177 result->regs[i] = sym->regs[i];
1178 else if (result->regType == REG_SCR)
1179 result->regs[i] = getRegScr (ic, ebp, result);
1181 result->regs[i] = getRegGpr (ic, ebp, result);
1183 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1187 /* free the remaining */
1188 for (; i < sym->nRegs; i++) {
1190 if (!symHasReg (psym, sym->regs[i]))
1191 freeReg (sym->regs[i]);
1193 else freeReg (sym->regs[i]);
1200 /*-----------------------------------------------------------------*/
1201 /* reassignLR - reassign this to registers */
1202 /*-----------------------------------------------------------------*/
1204 reassignLR (operand * op)
1206 symbol *sym = OP_SYMBOL (op);
1209 /* not spilt any more */
1210 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
1211 bitVectUnSetBit (_G.spiltSet, sym->key);
1213 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1217 for (i = 0; i < sym->nRegs; i++)
1218 sym->regs[i]->isFree = 0;
1221 /*-----------------------------------------------------------------*/
1222 /* willCauseSpill - determines if allocating will cause a spill */
1223 /*-----------------------------------------------------------------*/
1225 willCauseSpill (int nr, int rt)
1227 /* first check if there are any avlb registers
1228 of te type required */
1229 if (rt == REG_PTR) {
1230 /* special case for pointer type
1231 if pointer type not avlb then
1232 check for type gpr */
1233 if (nFreeRegs (rt) >= nr)
1235 if (nFreeRegs (REG_GPR) >= nr)
1239 if (avr_ptrRegReq) {
1240 if (nFreeRegs (rt) >= nr)
1244 if (nFreeRegs (REG_PTR) + nFreeRegs (REG_GPR) >= nr)
1249 /* it will cause a spil */
1253 /*-----------------------------------------------------------------*/
1254 /* positionRegs - the allocator can allocate same registers to res- */
1255 /* ult and operand, if this happens make sure they are in the same */
1256 /* position as the operand otherwise chaos results */
1257 /*-----------------------------------------------------------------*/
1259 positionRegs (symbol * result, symbol * opsym, int lineno)
1261 int count = min (result->nRegs, opsym->nRegs);
1262 int i, j = 0, shared = 0;
1264 /* if the result has been spilt then cannot share */
1269 /* first make sure that they actually share */
1270 for (i = 0; i < count; i++) {
1271 for (j = 0; j < count; j++) {
1272 if (result->regs[i] == opsym->regs[j] && i != j) {
1280 regs *tmp = result->regs[i];
1281 result->regs[i] = result->regs[j];
1282 result->regs[j] = tmp;
1287 /*-----------------------------------------------------------------*/
1288 /* needsPair - heuristic to determine if a pair would be good */
1289 /*-----------------------------------------------------------------*/
1290 static int needsPair (iCode *ic)
1292 symbol *sym = OP_SYMBOL(IC_RESULT(ic));
1293 bitVect *uses_defs =
1294 bitVectUnion(OP_USES (IC_RESULT(ic)),OP_DEFS(IC_RESULT(ic)));
1296 /* if size is less than 2 then NO */
1297 if (sym->nRegs < 2) return 0;
1298 /* if type Pointer then YES */
1299 if (IS_PTR(sym->type)) return 1;
1301 /* go thru the usages of this operand if used with
1302 a constant then yes */
1303 while (!bitVectIsZero(uses_defs)) {
1304 int ikey = bitVectFirstBit(uses_defs);
1305 iCode *uic = hTabItemWithKey(iCodehTab,ikey);
1306 sym_link *otype = NULL;
1307 bitVectUnSetBit(uses_defs,ikey);
1309 otype = (IC_RIGHT(uic) ? operandType(IC_RIGHT(uic)) : NULL);
1310 if (otype && IS_LITERAL(otype)) return 1;
1315 /*-----------------------------------------------------------------*/
1316 /* serialRegAssign - serially allocate registers to the variables */
1317 /*-----------------------------------------------------------------*/
1319 serialRegAssign (eBBlock ** ebbs, int count)
1323 /* for all blocks */
1324 for (i = 0; i < count; i++) {
1328 if (ebbs[i]->noPath &&
1329 (ebbs[i]->entryLabel != entryLabel &&
1330 ebbs[i]->entryLabel != returnLabel))
1333 /* of all instructions do */
1334 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1336 /* if this is an ipop that means some live
1337 range will have to be assigned again */
1339 reassignLR (IC_LEFT (ic));
1341 /* if result is present && is a true symbol */
1342 if (IC_RESULT (ic) && ic->op != IFX &&
1343 IS_TRUE_SYMOP (IC_RESULT (ic)))
1344 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1346 /* take away registers from live
1347 ranges that end at this instruction */
1348 deassignLRs (ic, ebbs[i]);
1350 /* some don't need registers */
1351 if (SKIP_IC2 (ic) ||
1352 ic->op == JUMPTABLE ||
1356 (IC_RESULT (ic) && POINTER_SET (ic))) continue;
1358 /* now we need to allocate registers
1359 only for the result */
1360 if (IC_RESULT (ic)) {
1361 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1366 /* if it does not need or is spilt
1367 or is already assigned to registers
1368 or will not live beyond this instructions */
1371 bitVectBitValue (_G.regAssigned, sym->key)
1372 || sym->liveTo <= ic->seq)
1375 /* if some liverange has been spilt at the block level
1376 and this one live beyond this block then spil this
1379 && sym->liveTo > ebbs[i]->lSeq) {
1383 /* if trying to allocate this will cause
1384 a spill and there is nothing to spill
1385 or this one is rematerializable then
1388 willCauseSpill (sym->nRegs,
1390 spillable = computeSpillable (ic);
1391 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1396 /* If the live range preceeds the point of definition
1397 then ideally we must take into account registers that
1398 have been allocated after sym->liveFrom but freed
1399 before ic->seq. This is complicated, so spill this
1400 symbol instead and let fillGaps handle the allocation. */
1401 if (sym->liveFrom < ic->seq)
1407 /* if it has a spillocation & is used less than
1408 all other live ranges then spill this */
1410 if (sym->usl.spillLoc) {
1411 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1412 allLRs, ebbs[i], ic));
1413 if (leastUsed && leastUsed->used > sym->used) {
1418 /* if none of the liveRanges have a spillLocation then better
1419 to spill this one than anything else already assigned to registers */
1420 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1427 /* we assign registers to it */
1428 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1429 if (needsPair(ic)) {
1432 if (sym->regType == REG_PTR) regtype = REG_PTR;
1433 else if (sym->regType == REG_SCR) regtype = REG_SCR;
1434 else regtype = REG_GPR;
1435 preg = allocRegPair(regtype);
1437 sym->regs[j++] = preg;
1438 sym->regs[j++] = ®sAVR[preg->rIdx+1];
1441 for (; j < sym->nRegs; j++) {
1442 if (sym->regType == REG_PTR)
1443 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1444 else if (sym->regType == REG_SCR)
1445 sym->regs[j] = getRegScr (ic, ebbs[i], sym);
1447 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1448 /* if the allocation falied which means
1449 this was spilt then break */
1450 if (!sym->regs[j]) break;
1453 /* if it shares registers with operands make sure
1454 that they are in the same position */
1455 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1456 OP_SYMBOL (IC_LEFT (ic))->nRegs
1458 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1459 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1460 /* do the same for the right operand */
1461 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1462 && OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1463 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1464 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1471 /*-----------------------------------------------------------------*/
1472 /* rUmaskForOp :- returns register mask for an operand */
1473 /*-----------------------------------------------------------------*/
1475 rUmaskForOp (operand * op)
1481 /* only temporaries are assigned registers */
1485 sym = OP_SYMBOL (op);
1487 /* if spilt or no registers assigned to it
1489 if (sym->isspilt || !sym->nRegs)
1492 rumask = newBitVect (avr_nRegs);
1494 for (j = 0; j < sym->nRegs; j++) {
1495 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1501 /*-----------------------------------------------------------------*/
1502 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1503 /*-----------------------------------------------------------------*/
1505 regsUsedIniCode (iCode * ic)
1507 bitVect *rmask = newBitVect (avr_nRegs);
1509 /* do the special cases first */
1510 if (ic->op == IFX) {
1511 rmask = bitVectUnion (rmask, rUmaskForOp (IC_COND (ic)));
1515 /* for the jumptable */
1516 if (ic->op == JUMPTABLE) {
1517 rmask = bitVectUnion (rmask, rUmaskForOp (IC_JTCOND (ic)));
1522 /* of all other cases */
1524 rmask = bitVectUnion (rmask, rUmaskForOp (IC_LEFT (ic)));
1528 rmask = bitVectUnion (rmask, rUmaskForOp (IC_RIGHT (ic)));
1531 rmask = bitVectUnion (rmask, rUmaskForOp (IC_RESULT (ic)));
1537 /*-----------------------------------------------------------------*/
1538 /* createRegMask - for each instruction will determine the regsUsed */
1539 /*-----------------------------------------------------------------*/
1541 createRegMask (eBBlock ** ebbs, int count)
1545 /* for all blocks */
1546 for (i = 0; i < count; i++) {
1549 if (ebbs[i]->noPath &&
1550 (ebbs[i]->entryLabel != entryLabel &&
1551 ebbs[i]->entryLabel != returnLabel))
1554 /* for all instructions */
1555 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1559 if (SKIP_IC2 (ic) || !ic->rlive)
1562 /* first mark the registers used in this
1564 ic->rUsed = regsUsedIniCode (ic);
1565 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1567 /* now create the register mask for those
1568 registers that are in use : this is a
1569 super set of ic->rUsed */
1570 ic->rMask = newBitVect (avr_nRegs + 1);
1572 /* for all live Ranges alive at this point */
1573 for (j = 1; j < ic->rlive->size; j++) {
1577 /* if not alive then continue */
1578 if (!bitVectBitValue (ic->rlive, j))
1581 /* find the live range we are interested in */
1582 if (!(sym = hTabItemWithKey (liveRanges, j))) {
1583 werror (E_INTERNAL_ERROR, __FILE__,
1585 "createRegMask cannot find live range");
1589 /* if no register assigned to it */
1590 if (!sym->nRegs || sym->isspilt)
1593 /* for all the registers allocated to it */
1594 for (k = 0; k < sym->nRegs; k++) {
1596 int rIdx = sym->regs[k]->rIdx;
1597 ic->rMask = bitVectSetBit (ic-> rMask,rIdx);
1598 /* special case for X & Z registers */
1599 if (rIdx == R26_IDX || rIdx == R27_IDX)
1600 ic->rMask = bitVectSetBit (ic->rMask, X_IDX);
1601 if (rIdx == R30_IDX || rIdx == R31_IDX)
1602 ic->rMask = bitVectSetBit (ic->rMask, Z_IDX);
1611 /*-----------------------------------------------------------------*/
1612 /* regTypeNum - computes the type & number of registers required */
1613 /*-----------------------------------------------------------------*/
1621 /* for each live range do */
1622 for (sym = hTabFirstItem (liveRanges, &k); sym;
1623 sym = hTabNextItem (liveRanges, &k)) {
1625 /* if used zero times then no registers needed */
1626 if ((sym->liveTo - sym->liveFrom) == 0)
1630 /* if the live range is a temporary */
1633 /* if the type is marked as a conditional */
1634 if (sym->regType == REG_CND)
1637 /* if used in return only then we don't
1639 if (sym->ruonly || sym->accuse) {
1640 if (IS_AGGREGATE (sym->type) || sym->isptr)
1642 aggrToPtr (sym->type, FALSE);
1646 /* if the symbol has only one definition &
1647 that definition is a get_pointer and the
1648 pointer we are getting is rematerializable and
1651 if (bitVectnBitsOn (sym->defs) == 1 &&
1652 (ic = hTabItemWithKey (iCodehTab, bitVectFirstBit (sym-> defs)))
1653 && POINTER_GET (ic) && !IS_BITVAR (sym->etype)) {
1655 /* if in data space or idata space then try to
1656 allocate pointer register */
1660 /* if not then we require registers */
1662 ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1663 getSize (sym->type =
1664 aggrToPtr (sym->type,
1665 FALSE)) : getSize (sym->
1668 if (sym->nRegs > 4) {
1670 "allocated more than 4 or 0 registers for type ");
1671 printTypeChain (sym->type, stderr);
1672 fprintf (stderr, "\n");
1675 /* determine the type of register required */
1676 if (sym->nRegs == 2 && /* size is two */
1677 IS_PTR (sym->type) && /* is a pointer */
1678 sym->uptr) { /* has pointer usage i.e. get/set pointer */
1679 sym->regType = REG_PTR;
1683 /* live accross a function call then gpr else scratch */
1684 if (sym->isLiveFcall)
1685 sym->regType = REG_GPR;
1687 sym->regType = REG_SCR;
1691 /* for the first run we don't provide */
1692 /* registers for true symbols we will */
1693 /* see how things go */
1699 /*-----------------------------------------------------------------*/
1700 /* deallocStackSpil - this will set the stack pointer back */
1701 /*-----------------------------------------------------------------*/
1703 DEFSETFUNC (deallocStackSpil)
1711 /*-----------------------------------------------------------------*/
1712 /* packRegsForAssign - register reduction for assignment */
1713 /*-----------------------------------------------------------------*/
1715 packRegsForAssign (iCode * ic, eBBlock * ebp)
1719 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1720 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1721 OP_LIVETO (IC_RIGHT (ic)) > ic->seq) {
1725 /* find the definition of iTempNN scanning backwards if we find a
1726 a use of the true symbol in before we find the definition then
1728 for (dic = ic->prev; dic; dic = dic->prev) {
1730 /* if there is a function call and this is
1731 a parameter & not my parameter then don't pack it */
1732 if ((dic->op == CALL || dic->op == PCALL) &&
1733 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1734 !OP_SYMBOL (IC_RESULT (ic))->ismyparm)) {
1742 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1743 IS_OP_VOLATILE (IC_RESULT (dic))) {
1748 if (IS_SYMOP (IC_RESULT (dic)) &&
1749 IC_RESULT (dic)->key == IC_RIGHT (ic)->key) {
1750 if (POINTER_SET (dic))
1756 if (IS_SYMOP (IC_RIGHT (dic)) &&
1757 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1758 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key)) {
1763 if (IS_SYMOP (IC_LEFT (dic)) &&
1764 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1765 IC_LEFT (dic)->key == IC_RIGHT (ic)->key)) {
1770 if (POINTER_SET (dic) &&
1771 IC_RESULT (dic)->key == IC_RESULT (ic)->key) {
1778 return 0; /* did not find */
1780 /* if the result is on stack or iaccess then it must be
1781 the same atleast one of the operands */
1782 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1783 OP_SYMBOL (IC_RESULT (ic))->iaccess) {
1785 /* the operation has only one symbol
1786 operator then we can pack */
1787 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1788 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1791 if (!((IC_LEFT (dic) &&
1792 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1794 IC_RESULT (ic)->key == IC_RIGHT (dic)->key))) return 0;
1797 /* if in far space & tru symbol then don't */
1798 if ((IS_TRUE_SYMOP (IC_RESULT (ic)))
1799 && isOperandInFarSpace (IC_RESULT (ic))) return 0;
1800 /* found the definition */
1801 /* replace the result with the result of */
1802 /* this assignment and remove this assignment */
1803 IC_RESULT (dic) = IC_RESULT (ic);
1805 if (IS_ITEMP (IC_RESULT (dic))
1806 && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq) {
1807 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1809 /* delete from liverange table also
1810 delete from all the points inbetween and the new
1812 for (sic = dic; sic != ic; sic = sic->next) {
1813 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1814 if (IS_ITEMP (IC_RESULT (dic)))
1815 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1818 remiCodeFromeBBlock (ebp, ic);
1819 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1824 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1827 /*-----------------------------------------------------------------*/
1828 /* packRegsForOneuse : - will reduce some registers for single Use */
1829 /*-----------------------------------------------------------------*/
1831 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1836 /* if returning a literal then do nothing */
1841 if (ic->op != RETURN)
1844 /* this routine will mark the a symbol as used in one
1845 instruction use only && if the defintion is local
1846 (ie. within the basic block) && has only one definition &&
1847 that definiion is either a return value from a
1848 function or does not contain any variables in
1850 uses = bitVectCopy (OP_USES (op));
1851 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1852 if (!bitVectIsZero (uses)) /* has other uses */
1855 /* if it has only one defintion */
1856 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1857 return NULL; /* has more than one definition */
1859 /* get the that definition */
1861 hTabItemWithKey (iCodehTab,
1862 bitVectFirstBit (OP_DEFS (op))))) return NULL;
1864 /* found the definition now check if it is local */
1865 if (dic->seq < ebp->fSeq || dic->seq > ebp->lSeq)
1866 return NULL; /* non-local */
1868 /* now check if it is the return from
1870 if (dic->op == CALL || dic->op == PCALL) {
1871 if (ic->op != SEND && ic->op != RETURN &&
1872 !POINTER_SET(ic) && !POINTER_GET(ic)) {
1873 OP_SYMBOL (op)->ruonly = 1;
1880 /* otherwise check that the definition does
1881 not contain any symbols in far space */
1882 if (IS_OP_RUONLY (IC_LEFT (ic)) || IS_OP_RUONLY (IC_RIGHT (ic))) {
1886 /* if pointer set then make sure the pointer
1888 if (POINTER_SET (dic) &&
1889 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1892 if (POINTER_GET (dic) &&
1893 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1898 /* also make sure the intervenening instructions
1899 don't have any thing in far space */
1900 for (dic = dic->next; dic && dic != ic; dic = dic->next) {
1902 /* if there is an intervening function call then no */
1903 if (dic->op == CALL || dic->op == PCALL)
1905 /* if pointer set then make sure the pointer
1907 if (POINTER_SET (dic) &&
1908 !IS_DATA_PTR (aggrToPtr
1909 (operandType (IC_RESULT (dic)),
1910 FALSE))) return NULL;
1912 if (POINTER_GET (dic) &&
1913 !IS_DATA_PTR (aggrToPtr
1914 (operandType (IC_LEFT (dic)),
1915 FALSE))) return NULL;
1917 /* if address of & the result is remat the okay */
1918 if (dic->op == ADDRESS_OF &&
1919 OP_SYMBOL (IC_RESULT (dic))->remat) continue;
1921 /* if operand has size of three or more & this
1922 operation is a '*','/' or '%' then 'b' may
1924 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1925 getSize (operandType (op)) >= 3)
1928 /* if left or right or result is in far space */
1929 if (IS_OP_RUONLY (IC_LEFT (dic)) ||
1930 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1931 IS_OP_RUONLY (IC_RESULT (dic))) {
1936 OP_SYMBOL (op)->ruonly = 1;
1941 /*-----------------------------------------------------------------*/
1942 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1943 /*-----------------------------------------------------------------*/
1945 isBitwiseOptimizable (iCode * ic)
1947 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
1948 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1950 /* bitwise operations are considered optimizable
1951 under the following conditions (Jean-Louis VERN)
1963 if (IS_LITERAL (rtype) ||
1964 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
1970 /*-----------------------------------------------------------------*/
1971 /* packRegisters - does some transformations to reduce register */
1973 /*-----------------------------------------------------------------*/
1975 packRegisters (eBBlock * ebp)
1984 /* look for assignments of the form */
1985 /* iTempNN = TRueSym (someoperation) SomeOperand */
1987 /* TrueSym := iTempNN:1 */
1988 for (ic = ebp->sch; ic; ic = ic->next) {
1991 /* find assignment of the form TrueSym := iTempNN:1 */
1992 if (ic->op == '=' && !POINTER_SET (ic))
1993 change += packRegsForAssign (ic, ebp);
2000 for (ic = ebp->sch; ic; ic = ic->next) {
2002 /* if this is an itemp & result of a address of a true sym
2003 then mark this as rematerialisable */
2004 if (ic->op == ADDRESS_OF &&
2005 IS_ITEMP (IC_RESULT (ic)) &&
2006 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2007 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2008 !OP_SYMBOL (IC_LEFT (ic))->onStack) {
2010 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2011 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2012 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2016 /* if straight assignment then carry remat flag if
2017 this is the only definition */
2018 if (ic->op == '=' &&
2019 !POINTER_SET (ic) &&
2020 IS_SYMOP (IC_RIGHT (ic)) &&
2021 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2022 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1) {
2024 OP_SYMBOL (IC_RESULT (ic))->remat =
2025 OP_SYMBOL (IC_RIGHT (ic))->remat;
2026 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2027 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2030 /* if this is a +/- operation with a rematerizable
2031 then mark this as rematerializable as well only
2032 if the literal value is within the range -255 and + 255
2033 the assembler cannot handle it other wise */
2034 if ((ic->op == '+' || ic->op == '-') &&
2035 (IS_SYMOP (IC_LEFT (ic)) &&
2036 IS_ITEMP (IC_RESULT (ic)) &&
2037 OP_SYMBOL (IC_LEFT (ic))->remat &&
2038 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2039 IS_OP_LITERAL (IC_RIGHT (ic)))) {
2041 int i = (int) operandLitValue (IC_RIGHT (ic));
2042 if (i < 255 && i > -255) {
2043 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2044 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2045 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc =
2050 /* mark the pointer usages */
2051 if (POINTER_SET (ic))
2052 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2054 if (POINTER_GET (ic)) {
2055 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2056 if (OP_SYMBOL (IC_LEFT(ic))->remat)
2057 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2060 /* if the condition of an if instruction
2061 is defined in the previous instruction then
2062 mark the itemp as a conditional */
2063 if ((IS_CONDITIONAL (ic) ||
2064 ((ic->op == BITWISEAND ||
2067 isBitwiseOptimizable (ic))) &&
2068 ic->next && ic->next->op == IFX &&
2069 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2070 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq) {
2072 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2076 /* some cases the redundant moves can
2077 can be eliminated for return statements */
2078 if ((ic->op == RETURN || ic->op == SEND))
2079 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2081 /* if this is cast for intergral promotion then
2082 check if only use of the definition of the
2083 operand being casted/ if yes then replace
2084 the result of that arithmetic operation with
2085 this result and get rid of the cast */
2086 if (ic->op == CAST) {
2087 sym_link *fromType = operandType (IC_RIGHT (ic));
2088 sym_link *toType = operandType (IC_LEFT (ic));
2090 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2091 getSize (fromType) != getSize (toType) &&
2092 SPEC_USIGN (fromType) == SPEC_USIGN (toType)) {
2095 packRegsForOneuse (ic, IC_RIGHT (ic),
2098 if (IS_ARITHMETIC_OP (dic)) {
2101 remiCodeFromeBBlock (ebp, ic);
2102 hTabDeleteItem (&iCodehTab,
2109 OP_SYMBOL (IC_RIGHT (ic))->
2115 /* if the type from and type to are the same
2116 then if this is the only use then packit */
2117 if (compareType (operandType (IC_RIGHT (ic)),
2118 operandType (IC_LEFT (ic))) ==
2121 packRegsForOneuse (ic,
2127 remiCodeFromeBBlock (ebp, ic);
2128 hTabDeleteItem (&iCodehTab,
2140 /*-----------------------------------------------------------------*/
2141 /* preAssignParms - we have a leaf function preassign registers */
2142 /*-----------------------------------------------------------------*/
2144 preAssignParms (iCode * ic)
2147 /* look for receives and assign registers
2148 to the result of the receives */
2150 /* if it is a receive */
2151 if (ic->op == RECEIVE) {
2152 symbol *r = OP_SYMBOL (IC_RESULT (ic));
2153 int size = getSize (r->type);
2154 if (r->regType == REG_GPR || r->regType == REG_SCR) {
2157 r->regs[j++] = ®sAVR[i++];
2158 regsAVR[i - 1].isFree = 0;
2160 /* put in the regassigned vector */
2162 bitVectSetBit (_G.regAssigned,
2166 /* not a GPR then we should mark as free */
2168 regsAVR[i++].isFree = 1;
2174 /* mark anything remaining as free */
2175 while (i <= R23_IDX)
2176 regsAVR[i++].isFree = 1;
2179 /*-----------------------------------------------------------------*/
2180 /* setdefaultRegs - do setup stuff for register allocation */
2181 /*-----------------------------------------------------------------*/
2183 setDefaultRegs (eBBlock ** ebbs, int count)
2187 /* if no pointer registers required in this function
2188 then mark r26-27 & r30-r31 as GPR & free */
2189 regsAVR[R26_IDX].isFree =
2190 regsAVR[R27_IDX].isFree =
2191 regsAVR[R30_IDX].isFree = regsAVR[R31_IDX].isFree = 1;
2193 if (!avr_ptrRegReq) {
2194 regsAVR[R26_IDX].type = (regsAVR[R26_IDX].type & ~REG_MASK) | REG_GPR;
2195 regsAVR[R27_IDX].type = (regsAVR[R27_IDX].type & ~REG_MASK) | REG_GPR;
2196 regsAVR[R28_IDX].type = (regsAVR[R28_IDX].type & ~REG_MASK) | REG_GPR;
2197 regsAVR[R29_IDX].type = (regsAVR[R29_IDX].type & ~REG_MASK) | REG_GPR;
2200 regsAVR[R26_IDX].type = (regsAVR[R26_IDX].type & ~REG_MASK) | REG_PTR;
2201 regsAVR[R27_IDX].type = (regsAVR[R27_IDX].type & ~REG_MASK) | REG_PTR;
2202 regsAVR[R30_IDX].type = (regsAVR[R30_IDX].type & ~REG_MASK) | REG_PTR;
2203 regsAVR[R31_IDX].type = (regsAVR[R31_IDX].type & ~REG_MASK) | REG_PTR;
2206 /* registers 0-1 / 24-25 used as scratch */
2207 regsAVR[R0_IDX].isFree =
2208 regsAVR[R1_IDX].isFree =
2209 regsAVR[R24_IDX].isFree = regsAVR[R25_IDX].isFree = 0;
2211 /* if this has no function calls then we need
2212 to do something special
2213 a) pre-assign registers to parameters RECEIVE
2214 b) mark the remaining parameter regs as free */
2215 /* mark the parameter regs as SCRACH */
2216 for (i = R16_IDX; i <= R23_IDX; i++) {
2217 regsAVR[i].type = (regsAVR[i].type & ~REG_MASK) | REG_SCR;
2218 regsAVR[i].isFree = 1;
2220 if (!IFFUNC_HASFCALL(currFunc->type)) {
2221 preAssignParms (ebbs[0]->sch);
2223 /* Y - is not allocated (it is the stack frame) */
2224 regsAVR[R28_IDX].isFree = regsAVR[R28_IDX].isFree = 0;
2227 /*-----------------------------------------------------------------*/
2228 /* assignRegisters - assigns registers to each live range as need */
2229 /*-----------------------------------------------------------------*/
2231 avr_assignRegisters (eBBlock ** ebbs, int count)
2236 setToNull ((void *) &_G.funcrUsed);
2237 avr_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2239 /* change assignments this will remove some
2240 live ranges reducing some register pressure */
2241 for (i = 0; i < count; i++)
2242 packRegisters (ebbs[i]);
2244 /* liveranges probably changed by register packing
2245 so we compute them again */
2246 recomputeLiveRanges (ebbs, count);
2248 if (options.dump_pack)
2249 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2251 /* first determine for each live range the number of
2252 registers & the type of registers required for each */
2255 /* setup the default registers */
2256 setDefaultRegs (ebbs, count);
2258 /* and serially allocate registers */
2259 serialRegAssign (ebbs, count);
2261 /* if stack was extended then tell the user */
2262 if (_G.stackExtend) {
2263 /* werror(W_TOOMANY_SPILS,"stack", */
2264 /* _G.stackExtend,currFunc->name,""); */
2268 if (_G.dataExtend) {
2269 /* werror(W_TOOMANY_SPILS,"data space", */
2270 /* _G.dataExtend,currFunc->name,""); */
2274 /* after that create the register mask
2275 for each of the instruction */
2276 createRegMask (ebbs, count);
2278 /* redo that offsets for stacked automatic variables */
2279 redoStackOffsets ();
2281 if (options.dump_rassgn)
2282 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2284 /* now get back the chain */
2285 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2289 /* for (; ic ; ic = ic->next) */
2290 /* piCode(ic,stdout); */
2291 /* free up any _G.stackSpil locations allocated */
2292 applyToSet (_G.stackSpil, deallocStackSpil);
2294 setToNull ((void *) &_G.stackSpil);
2295 setToNull ((void *) &_G.spiltSet);
2296 /* mark all registers as free */