1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
47 bitVect *totRegAssigned; /* final set of LRs that got into registers */
50 bitVect *funcrUsed; /* registers used in a function */
56 /* Shared with gen.c */
57 int ds390_ptrRegReq; /* one byte pointer register required */
63 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
64 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
65 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
66 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
67 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
68 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
69 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
70 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
71 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
72 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
73 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
74 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
75 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
76 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
77 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
78 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
79 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
80 {REG_CND, CND_IDX, REG_GPR, "C", "C", "xreg", 0, 0, 0},
83 static void spillThis (symbol *);
84 static void freeAllRegs ();
85 static iCode * packRegsDPTRuse (operand *);
86 static int packRegsDPTRnuse (operand *,int);
88 /*-----------------------------------------------------------------*/
89 /* allocReg - allocates register of given type */
90 /*-----------------------------------------------------------------*/
96 for (i = 0; i < ds390_nRegs; i++)
99 /* if type is given as 0 then any
100 free register will do */
104 regs390[i].isFree = 0;
107 bitVectSetBit (currFunc->regsUsed, i);
110 /* other wise look for specific type
112 if (regs390[i].isFree &&
113 regs390[i].type == type)
115 regs390[i].isFree = 0;
118 bitVectSetBit (currFunc->regsUsed, i);
125 /*-----------------------------------------------------------------*/
126 /* ds390_regWithIdx - returns pointer to register wit index number */
127 /*-----------------------------------------------------------------*/
129 ds390_regWithIdx (int idx)
133 for (i = 0; i < ds390_nRegs; i++)
134 if (regs390[i].rIdx == idx)
137 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
138 "regWithIdx not found");
142 /*-----------------------------------------------------------------*/
143 /* freeReg - frees a register */
144 /*-----------------------------------------------------------------*/
151 /*-----------------------------------------------------------------*/
152 /* useReg - marks a register as used */
153 /*-----------------------------------------------------------------*/
161 /*-----------------------------------------------------------------*/
162 /* nFreeRegs - returns number of free registers */
163 /*-----------------------------------------------------------------*/
170 for (i = 0; i < ds390_nRegs; i++)
171 if (regs390[i].isFree && regs390[i].type == type)
176 /*-----------------------------------------------------------------*/
177 /* nfreeRegsType - free registers with type */
178 /*-----------------------------------------------------------------*/
180 nfreeRegsType (int type)
185 if ((nfr = nFreeRegs (type)) == 0)
186 return nFreeRegs (REG_GPR);
189 return nFreeRegs (type);
194 /*-----------------------------------------------------------------*/
195 /* isOperandInReg - returns true if operand is currently in regs */
196 /*-----------------------------------------------------------------*/
197 static int isOperandInReg(operand *op)
199 if (!IS_SYMOP(op)) return 0;
200 if (OP_SYMBOL(op)->ruonly) return 1;
201 if (OP_SYMBOL(op)->accuse) return 1;
202 if (OP_SYMBOL(op)->dptr) return 1;
203 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
206 /*-----------------------------------------------------------------*/
207 /* computeSpillable - given a point find the spillable live ranges */
208 /*-----------------------------------------------------------------*/
210 computeSpillable (iCode * ic)
214 /* spillable live ranges are those that are live at this
215 point . the following categories need to be subtracted
217 a) - those that are already spilt
218 b) - if being used by this one
219 c) - defined by this one */
221 spillable = bitVectCopy (ic->rlive);
223 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
225 bitVectCplAnd (spillable, ic->uses); /* used in this one */
226 bitVectUnSetBit (spillable, ic->defKey);
227 spillable = bitVectIntersect (spillable, _G.regAssigned);
232 /*-----------------------------------------------------------------*/
233 /* noSpilLoc - return true if a variable has no spil location */
234 /*-----------------------------------------------------------------*/
236 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
238 return (sym->usl.spillLoc ? 0 : 1);
241 /*-----------------------------------------------------------------*/
242 /* hasSpilLoc - will return 1 if the symbol has spil location */
243 /*-----------------------------------------------------------------*/
245 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
247 return (sym->usl.spillLoc ? 1 : 0);
250 /*-----------------------------------------------------------------*/
251 /* directSpilLoc - will return 1 if the splilocation is in direct */
252 /*-----------------------------------------------------------------*/
254 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
256 if (sym->usl.spillLoc &&
257 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
263 /*-----------------------------------------------------------------*/
264 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
265 /* but is not used as a pointer */
266 /*-----------------------------------------------------------------*/
268 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
270 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
273 /*-----------------------------------------------------------------*/
274 /* rematable - will return 1 if the remat flag is set */
275 /*-----------------------------------------------------------------*/
277 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
282 /*-----------------------------------------------------------------*/
283 /* notUsedInRemaining - not used or defined in remain of the block */
284 /*-----------------------------------------------------------------*/
286 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
288 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
289 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
292 /*-----------------------------------------------------------------*/
293 /* allLRs - return true for all */
294 /*-----------------------------------------------------------------*/
296 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
301 /*-----------------------------------------------------------------*/
302 /* liveRangesWith - applies function to a given set of live range */
303 /*-----------------------------------------------------------------*/
305 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
306 eBBlock * ebp, iCode * ic)
311 if (!lrs || !lrs->size)
314 for (i = 1; i < lrs->size; i++)
317 if (!bitVectBitValue (lrs, i))
320 /* if we don't find it in the live range
321 hash table we are in serious trouble */
322 if (!(sym = hTabItemWithKey (liveRanges, i)))
324 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
325 "liveRangesWith could not find liveRange");
329 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
330 addSetHead (&rset, sym);
337 /*-----------------------------------------------------------------*/
338 /* leastUsedLR - given a set determines which is the least used */
339 /*-----------------------------------------------------------------*/
341 leastUsedLR (set * sset)
343 symbol *sym = NULL, *lsym = NULL;
345 sym = lsym = setFirstItem (sset);
350 for (; lsym; lsym = setNextItem (sset))
353 /* if usage is the same then prefer
354 the spill the smaller of the two */
355 if (lsym->used == sym->used)
356 if (getSize (lsym->type) < getSize (sym->type))
360 if (lsym->used < sym->used)
365 setToNull ((void **) &sset);
370 /*-----------------------------------------------------------------*/
371 /* noOverLap - will iterate through the list looking for over lap */
372 /*-----------------------------------------------------------------*/
374 noOverLap (set * itmpStack, symbol * fsym)
378 for (sym = setFirstItem (itmpStack); sym;
379 sym = setNextItem (itmpStack))
381 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
386 /*-----------------------------------------------------------------*/
387 /* isFree - will return 1 if the a free spil location is found */
388 /*-----------------------------------------------------------------*/
393 V_ARG (symbol **, sloc);
394 V_ARG (symbol *, fsym);
396 /* if already found */
400 /* if it is free && and the itmp assigned to
401 this does not have any overlapping live ranges
402 with the one currently being assigned and
403 the size can be accomodated */
405 noOverLap (sym->usl.itmpStack, fsym) &&
406 getSize (sym->type) >= getSize (fsym->type))
415 /*-----------------------------------------------------------------*/
416 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
417 /*-----------------------------------------------------------------*/
419 spillLRWithPtrReg (symbol * forSym)
425 if (!_G.regAssigned ||
426 bitVectIsZero (_G.regAssigned))
429 r0 = ds390_regWithIdx (R0_IDX);
430 r1 = ds390_regWithIdx (R1_IDX);
432 /* for all live ranges */
433 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
434 lrsym = hTabNextItem (liveRanges, &k))
438 /* if no registers assigned to it or
440 /* if it does not overlap with this then
441 not need to spill it */
443 if (lrsym->isspilt || !lrsym->nRegs ||
444 (lrsym->liveTo < forSym->liveFrom))
447 /* go thru the registers : if it is either
448 r0 or r1 then spil it */
449 for (j = 0; j < lrsym->nRegs; j++)
450 if (lrsym->regs[j] == r0 ||
451 lrsym->regs[j] == r1)
460 /*-----------------------------------------------------------------*/
461 /* createStackSpil - create a location on the stack to spil */
462 /*-----------------------------------------------------------------*/
464 createStackSpil (symbol * sym)
467 int useXstack, model, noOverlay;
471 /* first go try and find a free one that is already
472 existing on the stack */
473 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
475 /* found a free one : just update & return */
476 sym->usl.spillLoc = sloc;
479 addSetHead (&sloc->usl.itmpStack, sym);
483 /* could not then have to create one , this is the hard part
484 we need to allocate this on the stack : this is really a
485 hack!! but cannot think of anything better at this time */
487 if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
488 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
490 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
495 sloc = newiTemp (slocBuffer);
497 /* set the type to the spilling symbol */
498 sloc->type = copyLinkChain (sym->type);
499 sloc->etype = getSpec (sloc->type);
500 if (options.model == MODEL_SMALL) {
501 SPEC_SCLS (sloc->etype) = S_DATA;
503 SPEC_SCLS (sloc->etype) = S_XDATA;
505 SPEC_EXTR (sloc->etype) = 0;
506 SPEC_STAT (sloc->etype) = 0;
507 SPEC_VOLATILE(sloc->etype) = 0;
508 SPEC_ABSA(sloc->etype) = 0;
510 /* we don't allow it to be allocated`
511 onto the external stack since : so we
512 temporarily turn it off ; we also
513 turn off memory model to prevent
514 the spil from going to the external storage
515 and turn off overlaying
518 useXstack = options.useXstack;
519 model = options.model;
520 noOverlay = options.noOverlay;
521 options.noOverlay = 1;
523 /* options.model = options.useXstack = 0; */
527 options.useXstack = useXstack;
528 options.model = model;
529 options.noOverlay = noOverlay;
530 sloc->isref = 1; /* to prevent compiler warning */
532 /* if it is on the stack then update the stack */
533 if (IN_STACK (sloc->etype))
535 currFunc->stack += getSize (sloc->type);
536 _G.stackExtend += getSize (sloc->type);
539 _G.dataExtend += getSize (sloc->type);
541 /* add it to the _G.stackSpil set */
542 addSetHead (&_G.stackSpil, sloc);
543 sym->usl.spillLoc = sloc;
546 /* add it to the set of itempStack set
547 of the spill location */
548 addSetHead (&sloc->usl.itmpStack, sym);
552 /*-----------------------------------------------------------------*/
553 /* isSpiltOnStack - returns true if the spil location is on stack */
554 /*-----------------------------------------------------------------*/
556 isSpiltOnStack (symbol * sym)
566 /* if (sym->_G.stackSpil) */
569 if (!sym->usl.spillLoc)
572 etype = getSpec (sym->usl.spillLoc->type);
573 if (IN_STACK (etype))
579 /*-----------------------------------------------------------------*/
580 /* spillThis - spils a specific operand */
581 /*-----------------------------------------------------------------*/
583 spillThis (symbol * sym)
586 /* if this is rematerializable or has a spillLocation
587 we are okay, else we need to create a spillLocation
589 if (!(sym->remat || sym->usl.spillLoc))
590 createStackSpil (sym);
593 /* mark it has spilt & put it in the spilt set */
594 sym->isspilt = sym->spillA = 1;
595 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
597 bitVectUnSetBit (_G.regAssigned, sym->key);
598 bitVectUnSetBit (_G.totRegAssigned, sym->key);
600 for (i = 0; i < sym->nRegs; i++)
604 freeReg (sym->regs[i]);
608 /* if spilt on stack then free up r0 & r1
609 if they could have been assigned to some
611 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
613 ds390_ptrRegReq += !options.stack10bit;
614 spillLRWithPtrReg (sym);
617 if (sym->usl.spillLoc && !sym->remat)
618 sym->usl.spillLoc->allocreq++;
622 /*-----------------------------------------------------------------*/
623 /* selectSpil - select a iTemp to spil : rather a simple procedure */
624 /*-----------------------------------------------------------------*/
626 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
628 bitVect *lrcs = NULL;
632 /* get the spillable live ranges */
633 lrcs = computeSpillable (ic);
635 /* get all live ranges that are rematerizable */
636 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
639 /* return the least used of these */
640 return leastUsedLR (selectS);
643 /* get live ranges with spillLocations in direct space */
644 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
646 sym = leastUsedLR (selectS);
647 strncpyz (sym->rname,
648 sym->usl.spillLoc->rname[0] ?
649 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
652 /* mark it as allocation required */
653 sym->usl.spillLoc->allocreq++;
657 /* if the symbol is local to the block then */
658 if (forSym->liveTo < ebp->lSeq)
661 /* check if there are any live ranges allocated
662 to registers that are not used in this block */
663 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
665 sym = leastUsedLR (selectS);
666 /* if this is not rematerializable */
675 /* check if there are any live ranges that not
676 used in the remainder of the block */
677 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
679 sym = leastUsedLR (selectS);
692 /* find live ranges with spillocation && not used as pointers */
693 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
696 sym = leastUsedLR (selectS);
697 /* mark this as allocation required */
698 sym->usl.spillLoc->allocreq++;
702 /* find live ranges with spillocation */
703 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
706 sym = leastUsedLR (selectS);
707 sym->usl.spillLoc->allocreq++;
711 /* couldn't find then we need to create a spil
712 location on the stack , for which one? the least
714 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
717 /* return a created spil location */
718 sym = createStackSpil (leastUsedLR (selectS));
719 sym->usl.spillLoc->allocreq++;
723 /* this is an extreme situation we will spill
724 this one : happens very rarely but it does happen */
730 /*-----------------------------------------------------------------*/
731 /* spilSomething - spil some variable & mark registers as free */
732 /*-----------------------------------------------------------------*/
734 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
739 /* get something we can spil */
740 ssym = selectSpil (ic, ebp, forSym);
742 /* mark it as spilt */
743 ssym->isspilt = ssym->spillA = 1;
744 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
746 /* mark it as not register assigned &
747 take it away from the set */
748 bitVectUnSetBit (_G.regAssigned, ssym->key);
749 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
751 /* mark the registers as free */
752 for (i = 0; i < ssym->nRegs; i++)
754 freeReg (ssym->regs[i]);
756 /* if spilt on stack then free up r0 & r1
757 if they could have been assigned to as gprs */
758 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
761 spillLRWithPtrReg (ssym);
764 /* if this was a block level spil then insert push & pop
765 at the start & end of block respectively */
768 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
769 /* add push to the start of the block */
770 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
771 ebp->sch->next : ebp->sch));
772 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
773 /* add pop to the end of the block */
774 addiCodeToeBBlock (ebp, nic, NULL);
777 /* if spilt because not used in the remainder of the
778 block then add a push before this instruction and
779 a pop at the end of the block */
780 if (ssym->remainSpil)
783 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
784 /* add push just before this instruction */
785 addiCodeToeBBlock (ebp, nic, ic);
787 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
788 /* add pop to the end of the block */
789 addiCodeToeBBlock (ebp, nic, NULL);
798 /*-----------------------------------------------------------------*/
799 /* getRegPtr - will try for PTR if not a GPR type if not spil */
800 /*-----------------------------------------------------------------*/
802 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
807 /* try for a ptr type */
808 if ((reg = allocReg (REG_PTR)))
811 /* try for gpr type */
812 if ((reg = allocReg (REG_GPR)))
815 /* we have to spil */
816 if (!spilSomething (ic, ebp, sym))
819 /* this looks like an infinite loop but
820 in really selectSpil will abort */
824 /*-----------------------------------------------------------------*/
825 /* getRegGpr - will try for GPR if not spil */
826 /*-----------------------------------------------------------------*/
828 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
833 /* try for gpr type */
834 if ((reg = allocReg (REG_GPR)))
837 if (!ds390_ptrRegReq)
838 if ((reg = allocReg (REG_PTR)))
841 /* we have to spil */
842 if (!spilSomething (ic, ebp, sym))
845 /* this looks like an infinite loop but
846 in really selectSpil will abort */
850 /*-----------------------------------------------------------------*/
851 /* getRegPtrNoSpil - get it cannot split */
852 /*-----------------------------------------------------------------*/
853 static regs *getRegPtrNoSpil()
857 /* try for a ptr type */
858 if ((reg = allocReg (REG_PTR)))
861 /* try for gpr type */
862 if ((reg = allocReg (REG_GPR)))
867 /* just to make the compiler happy */
871 /*-----------------------------------------------------------------*/
872 /* getRegGprNoSpil - get it cannot split */
873 /*-----------------------------------------------------------------*/
874 static regs *getRegGprNoSpil()
878 if ((reg = allocReg (REG_GPR)))
881 if (!ds390_ptrRegReq)
882 if ((reg = allocReg (REG_PTR)))
887 /* just to make the compiler happy */
891 /*-----------------------------------------------------------------*/
892 /* symHasReg - symbol has a given register */
893 /*-----------------------------------------------------------------*/
895 symHasReg (symbol * sym, regs * reg)
899 for (i = 0; i < sym->nRegs; i++)
900 if (sym->regs[i] == reg)
906 /*-----------------------------------------------------------------*/
907 /* deassignLRs - check the live to and if they have registers & are */
908 /* not spilt then free up the registers */
909 /*-----------------------------------------------------------------*/
911 deassignLRs (iCode * ic, eBBlock * ebp)
917 for (sym = hTabFirstItem (liveRanges, &k); sym;
918 sym = hTabNextItem (liveRanges, &k))
922 /* if it does not end here */
923 if (sym->liveTo > ic->seq)
926 /* if it was spilt on stack then we can
927 mark the stack spil location as free */
932 sym->usl.spillLoc->isFree = 1;
938 if (!bitVectBitValue (_G.regAssigned, sym->key))
941 /* special case check if this is an IFX &
942 the privious one was a pop and the
943 previous one was not spilt then keep track
945 if (ic->op == IFX && ic->prev &&
946 ic->prev->op == IPOP &&
947 !ic->prev->parmPush &&
948 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
949 psym = OP_SYMBOL (IC_LEFT (ic->prev));
955 bitVectUnSetBit (_G.regAssigned, sym->key);
957 /* if the result of this one needs registers
958 and does not have it then assign it right
960 if (IC_RESULT (ic) &&
961 !(SKIP_IC2 (ic) || /* not a special icode */
962 ic->op == JUMPTABLE ||
968 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
969 result->liveTo > ic->seq && /* and will live beyond this */
970 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
971 result->regType == sym->regType && /* same register types */
972 result->nRegs && /* which needs registers */
973 !result->isspilt && /* and does not already have them */
975 !bitVectBitValue (_G.regAssigned, result->key) &&
976 /* the number of free regs + number of regs in this LR
977 can accomodate the what result Needs */
978 ((nfreeRegsType (result->regType) +
979 sym->nRegs) >= result->nRegs)
983 for (i = 0; i < result->nRegs; i++)
985 result->regs[i] = sym->regs[i];
987 result->regs[i] = getRegGpr (ic, ebp, result);
989 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
990 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
994 /* free the remaining */
995 for (; i < sym->nRegs; i++)
999 if (!symHasReg (psym, sym->regs[i]))
1000 freeReg (sym->regs[i]);
1003 freeReg (sym->regs[i]);
1010 /*-----------------------------------------------------------------*/
1011 /* reassignLR - reassign this to registers */
1012 /*-----------------------------------------------------------------*/
1014 reassignLR (operand * op)
1016 symbol *sym = OP_SYMBOL (op);
1019 /* not spilt any more */
1020 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1021 bitVectUnSetBit (_G.spiltSet, sym->key);
1023 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1024 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1028 for (i = 0; i < sym->nRegs; i++)
1029 sym->regs[i]->isFree = 0;
1032 /*-----------------------------------------------------------------*/
1033 /* willCauseSpill - determines if allocating will cause a spill */
1034 /*-----------------------------------------------------------------*/
1036 willCauseSpill (int nr, int rt)
1038 /* first check if there are any avlb registers
1039 of te type required */
1042 /* special case for pointer type
1043 if pointer type not avlb then
1044 check for type gpr */
1045 if (nFreeRegs (rt) >= nr)
1047 if (nFreeRegs (REG_GPR) >= nr)
1052 if (ds390_ptrRegReq)
1054 if (nFreeRegs (rt) >= nr)
1059 if (nFreeRegs (REG_PTR) +
1060 nFreeRegs (REG_GPR) >= nr)
1065 /* it will cause a spil */
1069 /*-----------------------------------------------------------------*/
1070 /* positionRegs - the allocator can allocate same registers to res- */
1071 /* ult and operand, if this happens make sure they are in the same */
1072 /* position as the operand otherwise chaos results */
1073 /*-----------------------------------------------------------------*/
1075 positionRegs (symbol * result, symbol * opsym)
1077 int count = min (result->nRegs, opsym->nRegs);
1078 int i, j = 0, shared = 0;
1081 /* if the result has been spilt then cannot share */
1086 /* first make sure that they actually share */
1087 for (i = 0; i < count; i++)
1089 for (j = 0; j < count; j++)
1091 if (result->regs[i] == opsym->regs[j] && i != j)
1101 regs *tmp = result->regs[i];
1102 result->regs[i] = result->regs[j];
1103 result->regs[j] = tmp;
1110 /*-----------------------------------------------------------------*/
1111 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1112 /*-----------------------------------------------------------------*/
1113 bitVect *unusedLRs (eBBlock *ebp)
1115 bitVect *ret = NULL;
1119 if (!ebp) return NULL;
1120 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1121 sym = hTabNextItem(liveRanges,&key)) {
1123 if (notUsedInBlock(sym,ebp,NULL)) {
1124 ret = bitVectSetBit(ret,sym->key);
1131 /*-----------------------------------------------------------------*/
1132 /* deassignUnsedLRs - if this baisc block ends in a return then */
1133 /* deassign symbols not used in this block */
1134 /*-----------------------------------------------------------------*/
1135 bitVect *deassignUnsedLRs(eBBlock *ebp)
1137 bitVect *unused = NULL;
1140 switch (returnAtEnd(ebp)) {
1141 case 2: /* successor block ends in a return */
1142 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1144 case 1: /* this block ends in a return */
1145 unused = bitVectIntersect(unused,unusedLRs(ebp));
1150 for (i = 0 ; i < unused->size ; i++ ) {
1153 if (bitVectBitValue(unused,i)) {
1155 /* if assigned to registers */
1156 if (bitVectBitValue(_G.regAssigned,i)) {
1160 sym = hTabItemWithKey(liveRanges,i);
1161 /* remove it from regassigned & mark the
1163 bitVectUnSetBit(_G.regAssigned,i);
1164 for (j = 0 ; j < sym->nRegs; j++)
1165 freeReg(sym->regs[j]);
1167 /* not assigned to registers : remove from set*/
1168 bitVectUnSetBit(unused,i);
1176 /*-----------------------------------------------------------------*/
1177 /* reassignUnusedLRs - put registers to unused Live ranges */
1178 /*-----------------------------------------------------------------*/
1179 void reassignUnusedLRs (bitVect *unused)
1182 if (!unused) return ;
1184 for (i = 0 ; i < unused->size ; i++ ) {
1185 /* if unused : means it was assigned to registers before */
1186 if (bitVectBitValue(unused,i)) {
1190 /* put it back into reg set*/
1191 bitVectSetBit(_G.regAssigned,i) ;
1193 sym = hTabItemWithKey(liveRanges,i);
1194 /* makr registers busy */
1195 for (j = 0 ; j < sym->nRegs; j++)
1196 sym->regs[j]->isFree = 0;
1201 /*-----------------------------------------------------------------*/
1202 /* serialRegAssign - serially allocate registers to the variables */
1203 /*-----------------------------------------------------------------*/
1205 serialRegAssign (eBBlock ** ebbs, int count)
1209 /* for all blocks */
1210 for (i = 0; i < count; i++)
1214 bitVect *unusedLRs = NULL;
1216 if (ebbs[i]->noPath &&
1217 (ebbs[i]->entryLabel != entryLabel &&
1218 ebbs[i]->entryLabel != returnLabel))
1221 unusedLRs = deassignUnsedLRs(ebbs[i]);
1223 /* of all instructions do */
1224 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1227 /* if this is an ipop that means some live
1228 range will have to be assigned again */
1230 reassignLR (IC_LEFT (ic));
1232 /* if result is present && is a true symbol */
1233 if (IC_RESULT (ic) && ic->op != IFX &&
1234 IS_TRUE_SYMOP (IC_RESULT (ic)))
1235 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1237 /* take away registers from live
1238 ranges that end at this instruction */
1239 deassignLRs (ic, ebbs[i]);
1241 /* some don't need registers */
1242 if (SKIP_IC2 (ic) ||
1243 ic->op == JUMPTABLE ||
1247 (IC_RESULT (ic) && POINTER_SET (ic)))
1250 /* now we need to allocate registers
1251 only for the result */
1254 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1260 /* if it does not need or is spilt
1261 or is already assigned to registers
1262 or will not live beyond this instructions */
1265 bitVectBitValue (_G.regAssigned, sym->key) ||
1266 sym->liveTo <= ic->seq)
1269 /* if some liverange has been spilt at the block level
1270 and this one live beyond this block then spil this
1272 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1277 /* if trying to allocate this will cause
1278 a spill and there is nothing to spill
1279 or this one is rematerializable then
1281 willCS = willCauseSpill (sym->nRegs, sym->regType);
1282 spillable = computeSpillable (ic);
1284 (willCS && bitVectIsZero (spillable)))
1292 /* if it has a spillocation & is used less than
1293 all other live ranges then spill this */
1295 if (sym->usl.spillLoc) {
1296 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1297 allLRs, ebbs[i], ic));
1298 if (leastUsed && leastUsed->used > sym->used) {
1303 /* if none of the liveRanges have a spillLocation then better
1304 to spill this one than anything else already assigned to registers */
1305 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1306 /* if this is local to this block then we might find a block spil */
1307 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1315 /* if we need ptr regs for the right side
1317 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1318 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1319 <= (unsigned) PTRSIZE)
1324 /* else we assign registers to it */
1325 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1326 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1328 for (j = 0; j < sym->nRegs; j++)
1330 if (sym->regType == REG_PTR)
1331 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1333 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1335 /* if the allocation falied which means
1336 this was spilt then break */
1340 /* if it shares registers with operands make sure
1341 that they are in the same position */
1342 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1343 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1344 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1345 OP_SYMBOL (IC_LEFT (ic)));
1346 /* do the same for the right operand */
1347 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1348 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1349 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1350 OP_SYMBOL (IC_RIGHT (ic)));
1360 reassignUnusedLRs(unusedLRs);
1364 /*-----------------------------------------------------------------*/
1365 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1366 /*-----------------------------------------------------------------*/
1367 static void fillGaps()
1371 int loop = 0, change;
1373 if (getenv("DISABLE_FILL_GAPS")) return;
1375 /* First try to do DPTRuse once more since now we know what got into
1378 while (loop++ < 10) {
1381 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1382 sym = hTabNextItem(liveRanges,&key)) {
1383 int size = getSize(sym->type);
1385 if (sym->liveFrom == sym->liveTo) continue;
1387 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1388 size < 4 && size > 1) {
1390 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1392 /* if this was ssigned to registers then */
1393 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1394 /* take it out of the register assigned set */
1395 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1396 } else if (sym->usl.spillLoc) {
1397 sym->usl.spillLoc->allocreq--;
1398 sym->usl.spillLoc = NULL;
1402 sym->isspilt = sym->spillA = 0;
1406 /* try assigning other dptrs */
1407 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1408 /* if this was ssigned to registers then */
1409 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1410 /* take it out of the register assigned set */
1411 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1412 } else if (sym->usl.spillLoc) {
1413 sym->usl.spillLoc->allocreq--;
1414 sym->usl.spillLoc = NULL;
1417 sym->isspilt = sym->spillA = 0;
1422 /* look for livernages that was spilt by the allocator */
1423 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1424 sym = hTabNextItem(liveRanges,&key)) {
1429 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1430 if (!sym->uses || !sym->defs) continue ;
1431 /* find the liveRanges this one clashes with, that are
1432 still assigned to registers & mark the registers as used*/
1433 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1437 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1438 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1441 clr = hTabItemWithKey(liveRanges,i);
1444 /* mark these registers as used */
1445 for (k = 0 ; k < clr->nRegs ; k++ )
1446 useReg(clr->regs[k]);
1449 if (willCauseSpill(sym->nRegs,sym->regType)) {
1450 /* NOPE :( clear all registers & and continue */
1455 /* THERE IS HOPE !!!! */
1456 for (i=0; i < sym->nRegs ; i++ ) {
1457 if (sym->regType == REG_PTR)
1458 sym->regs[i] = getRegPtrNoSpil ();
1460 sym->regs[i] = getRegGprNoSpil ();
1463 /* for all its definitions & uses check if the registers
1464 allocated needs positioning NOTE: we can position
1465 only ONCE if more than One positioning required
1468 for (i = 0 ; i < sym->defs->size ; i++ ) {
1469 if (bitVectBitValue(sym->defs,i)) {
1471 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1472 if (SKIP_IC(ic)) continue;
1473 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1474 /* if left is assigned to registers */
1475 if (IS_SYMOP(IC_LEFT(ic)) &&
1476 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1477 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1479 if (IS_SYMOP(IC_RIGHT(ic)) &&
1480 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1481 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1483 if (pdone > 1) break;
1486 for (i = 0 ; i < sym->uses->size ; i++ ) {
1487 if (bitVectBitValue(sym->uses,i)) {
1489 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1490 if (SKIP_IC(ic)) continue;
1491 if (!IS_ASSIGN_ICODE(ic)) continue ;
1493 /* if result is assigned to registers */
1494 if (IS_SYMOP(IC_RESULT(ic)) &&
1495 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1496 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1498 if (pdone > 1) break;
1501 /* had to position more than once GIVE UP */
1503 /* UNDO all the changes we made to try this */
1505 for (i=0; i < sym->nRegs ; i++ ) {
1506 sym->regs[i] = NULL;
1509 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1510 "%s in function %s\n",
1511 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1514 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1515 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1516 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1517 sym->isspilt = sym->spillA = 0 ;
1518 sym->usl.spillLoc->allocreq--;
1519 sym->usl.spillLoc = NULL;
1527 /*-----------------------------------------------------------------*/
1528 /* rUmaskForOp :- returns register mask for an operand */
1529 /*-----------------------------------------------------------------*/
1531 ds390_rUmaskForOp (operand * op)
1537 /* only temporaries are assigned registers */
1541 sym = OP_SYMBOL (op);
1543 /* if spilt or no registers assigned to it
1545 if (sym->isspilt || !sym->nRegs)
1548 rumask = newBitVect (ds390_nRegs);
1550 for (j = 0; j < sym->nRegs; j++)
1552 rumask = bitVectSetBit (rumask,
1553 sym->regs[j]->rIdx);
1559 /*-----------------------------------------------------------------*/
1560 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1561 /*-----------------------------------------------------------------*/
1563 regsUsedIniCode (iCode * ic)
1565 bitVect *rmask = newBitVect (ds390_nRegs);
1567 /* do the special cases first */
1570 rmask = bitVectUnion (rmask,
1571 ds390_rUmaskForOp (IC_COND (ic)));
1575 /* for the jumptable */
1576 if (ic->op == JUMPTABLE)
1578 rmask = bitVectUnion (rmask,
1579 ds390_rUmaskForOp (IC_JTCOND (ic)));
1584 /* of all other cases */
1586 rmask = bitVectUnion (rmask,
1587 ds390_rUmaskForOp (IC_LEFT (ic)));
1591 rmask = bitVectUnion (rmask,
1592 ds390_rUmaskForOp (IC_RIGHT (ic)));
1595 rmask = bitVectUnion (rmask,
1596 ds390_rUmaskForOp (IC_RESULT (ic)));
1602 /*-----------------------------------------------------------------*/
1603 /* createRegMask - for each instruction will determine the regsUsed */
1604 /*-----------------------------------------------------------------*/
1606 createRegMask (eBBlock ** ebbs, int count)
1610 /* for all blocks */
1611 for (i = 0; i < count; i++)
1615 if (ebbs[i]->noPath &&
1616 (ebbs[i]->entryLabel != entryLabel &&
1617 ebbs[i]->entryLabel != returnLabel))
1620 /* for all instructions */
1621 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1626 if (SKIP_IC2 (ic) || !ic->rlive)
1629 /* first mark the registers used in this
1631 ic->rUsed = regsUsedIniCode (ic);
1632 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1634 /* now create the register mask for those
1635 registers that are in use : this is a
1636 super set of ic->rUsed */
1637 ic->rMask = newBitVect (ds390_nRegs + 1);
1639 /* for all live Ranges alive at this point */
1640 for (j = 1; j < ic->rlive->size; j++)
1645 /* if not alive then continue */
1646 if (!bitVectBitValue (ic->rlive, j))
1649 /* find the live range we are interested in */
1650 if (!(sym = hTabItemWithKey (liveRanges, j)))
1652 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1653 "createRegMask cannot find live range");
1657 /* special case for ruonly */
1658 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1659 int size = getSize(sym->type);
1661 for (k = 0 ; k < size; k++ )
1662 ic->rMask = bitVectSetBit (ic->rMask, j++);
1665 /* if no register assigned to it */
1666 if (!sym->nRegs || sym->isspilt)
1669 /* for all the registers allocated to it */
1670 for (k = 0; k < sym->nRegs; k++)
1673 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1679 /*-----------------------------------------------------------------*/
1680 /* rematStr - returns the rematerialized string for a remat var */
1681 /*-----------------------------------------------------------------*/
1683 rematStr (symbol * sym)
1686 iCode *ic = sym->rematiCode;
1693 /* if plus or minus print the right hand side */
1694 if (ic->op == '+' || ic->op == '-')
1696 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1697 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1700 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1703 /* cast then continue */
1704 if (IS_CAST_ICODE(ic)) {
1705 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1708 /* we reached the end */
1709 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1710 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1717 /*-----------------------------------------------------------------*/
1718 /* regTypeNum - computes the type & number of registers required */
1719 /*-----------------------------------------------------------------*/
1727 /* for each live range do */
1728 for (sym = hTabFirstItem (liveRanges, &k); sym;
1729 sym = hTabNextItem (liveRanges, &k))
1732 /* if used zero times then no registers needed */
1733 if ((sym->liveTo - sym->liveFrom) == 0)
1737 /* if the live range is a temporary */
1741 /* if the type is marked as a conditional */
1742 if (sym->regType == REG_CND)
1745 /* if used in return only then we don't
1747 if (sym->ruonly || sym->accuse)
1749 if (IS_AGGREGATE (sym->type) || sym->isptr)
1750 sym->type = aggrToPtr (sym->type, FALSE);
1754 /* if the symbol has only one definition &
1755 that definition is a get_pointer */
1756 if (bitVectnBitsOn (sym->defs) == 1 &&
1757 (ic = hTabItemWithKey (iCodehTab,
1758 bitVectFirstBit (sym->defs))) &&
1761 !IS_BITVAR (sym->etype))
1763 /* and that pointer is remat in data space */
1764 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1765 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1766 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1769 /* create a psuedo symbol & force a spil */
1770 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1771 psym->type = sym->type;
1772 psym->etype = sym->etype;
1773 strncpyz (psym->rname, psym->name, sizeof(psym->rname));
1775 sym->usl.spillLoc = psym;
1779 /* if in data space or idata space then try to
1780 allocate pointer register */
1784 /* if not then we require registers */
1785 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1786 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1787 getSize (sym->type));
1791 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1792 printTypeChain (sym->type, stderr);
1793 fprintf (stderr, "\n");
1796 /* determine the type of register required */
1797 if (sym->nRegs == 1 &&
1798 IS_PTR (sym->type) &&
1800 sym->regType = REG_PTR;
1802 sym->regType = REG_GPR;
1806 /* for the first run we don't provide */
1807 /* registers for true symbols we will */
1808 /* see how things go */
1814 /*-----------------------------------------------------------------*/
1815 /* freeAllRegs - mark all registers as free */
1816 /*-----------------------------------------------------------------*/
1822 for (i = 0; i < ds390_nRegs; i++)
1823 regs390[i].isFree = 1;
1826 /*-----------------------------------------------------------------*/
1827 /* deallocStackSpil - this will set the stack pointer back */
1828 /*-----------------------------------------------------------------*/
1830 DEFSETFUNC (deallocStackSpil)
1838 /*-----------------------------------------------------------------*/
1839 /* farSpacePackable - returns the packable icode for far variables */
1840 /*-----------------------------------------------------------------*/
1842 farSpacePackable (iCode * ic)
1846 /* go thru till we find a definition for the
1847 symbol on the right */
1848 for (dic = ic->prev; dic; dic = dic->prev)
1851 /* if the definition is a call then no */
1852 if ((dic->op == CALL || dic->op == PCALL) &&
1853 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1858 /* if shift by unknown amount then not */
1859 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1860 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1863 /* if pointer get and size > 1 */
1864 if (POINTER_GET (dic) &&
1865 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1868 if (POINTER_SET (dic) &&
1869 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1872 /* if any three is a true symbol in far space */
1873 if (IC_RESULT (dic) &&
1874 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1875 isOperandInFarSpace (IC_RESULT (dic)))
1878 if (IC_RIGHT (dic) &&
1879 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1880 isOperandInFarSpace (IC_RIGHT (dic)) &&
1881 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1884 if (IC_LEFT (dic) &&
1885 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1886 isOperandInFarSpace (IC_LEFT (dic)) &&
1887 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1890 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1892 if ((dic->op == LEFT_OP ||
1893 dic->op == RIGHT_OP ||
1895 IS_OP_LITERAL (IC_RIGHT (dic)))
1905 /*-----------------------------------------------------------------*/
1906 /* packRegsForAssign - register reduction for assignment */
1907 /*-----------------------------------------------------------------*/
1909 packRegsForAssign (iCode * ic, eBBlock * ebp)
1913 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1914 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1915 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1920 /* if the true symbol is defined in far space or on stack
1921 then we should not since this will increase register pressure */
1923 if (isOperandInFarSpace (IC_RESULT (ic)))
1925 if ((dic = farSpacePackable (ic)))
1931 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1936 /* find the definition of iTempNN scanning backwards if we find a
1937 a use of the true symbol in before we find the definition then
1939 for (dic = ic->prev; dic; dic = dic->prev)
1941 /* if there is a function call then don't pack it */
1942 if ((dic->op == CALL || dic->op == PCALL))
1951 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1952 IS_OP_VOLATILE (IC_RESULT (dic)))
1958 if (IS_SYMOP (IC_RESULT (dic)) &&
1959 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1961 if (POINTER_SET (dic))
1967 if (IS_SYMOP (IC_RIGHT (dic)) &&
1968 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1969 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1975 if (IS_SYMOP (IC_LEFT (dic)) &&
1976 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1977 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1983 if (POINTER_SET (dic) &&
1984 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1992 return 0; /* did not find */
1994 /* if the result is on stack or iaccess then it must be
1995 the same atleast one of the operands */
1996 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1997 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2000 /* the operation has only one symbol
2001 operator then we can pack */
2002 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2003 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2006 if (!((IC_LEFT (dic) &&
2007 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2009 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2013 /* found the definition */
2014 /* replace the result with the result of */
2015 /* this assignment and remove this assignment */
2016 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2018 IC_RESULT (dic) = IC_RESULT (ic);
2020 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2022 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2024 /* delete from liverange table also
2025 delete from all the points inbetween and the new
2027 for (sic = dic; sic != ic; sic = sic->next)
2029 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2030 if (IS_ITEMP (IC_RESULT (dic)))
2031 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2034 remiCodeFromeBBlock (ebp, ic);
2035 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2036 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2037 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2042 /*-----------------------------------------------------------------*/
2043 /* findAssignToSym : scanning backwards looks for first assig found */
2044 /*-----------------------------------------------------------------*/
2046 findAssignToSym (operand * op, iCode * ic)
2050 for (dic = ic->prev; dic; dic = dic->prev)
2053 /* if definition by assignment */
2054 if (dic->op == '=' &&
2055 !POINTER_SET (dic) &&
2056 IC_RESULT (dic)->key == op->key
2057 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2061 /* we are interested only if defined in far space */
2062 /* or in stack space in case of + & - */
2064 /* if assigned to a non-symbol then return
2066 if (!IS_SYMOP (IC_RIGHT (dic)))
2069 /* if the symbol is in far space then
2071 if (isOperandInFarSpace (IC_RIGHT (dic)))
2074 /* for + & - operations make sure that
2075 if it is on the stack it is the same
2076 as one of the three operands */
2077 if ((ic->op == '+' || ic->op == '-') &&
2078 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2081 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2082 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2083 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2091 /* if we find an usage then we cannot delete it */
2092 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2095 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2098 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2102 /* now make sure that the right side of dic
2103 is not defined between ic & dic */
2106 iCode *sic = dic->next;
2108 for (; sic != ic; sic = sic->next)
2109 if (IC_RESULT (sic) &&
2110 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2119 /*-----------------------------------------------------------------*/
2120 /* packRegsForSupport :- reduce some registers for support calls */
2121 /*-----------------------------------------------------------------*/
2123 packRegsForSupport (iCode * ic, eBBlock * ebp)
2127 /* for the left & right operand :- look to see if the
2128 left was assigned a true symbol in far space in that
2129 case replace them */
2130 if (IS_ITEMP (IC_LEFT (ic)) &&
2131 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2133 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2139 /* found it we need to remove it from the
2141 for (sic = dic; sic != ic; sic = sic->next) {
2142 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2143 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2146 IC_LEFT (ic)->operand.symOperand =
2147 IC_RIGHT (dic)->operand.symOperand;
2148 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2149 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2150 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2151 remiCodeFromeBBlock (ebp, dic);
2152 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2156 /* do the same for the right operand */
2159 IS_ITEMP (IC_RIGHT (ic)) &&
2160 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2162 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2168 /* if this is a subtraction & the result
2169 is a true symbol in far space then don't pack */
2170 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2172 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2173 if (IN_FARSPACE (SPEC_OCLS (etype)))
2176 /* found it we need to remove it from the
2178 for (sic = dic; sic != ic; sic = sic->next) {
2179 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2180 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2183 IC_RIGHT (ic)->operand.symOperand =
2184 IC_RIGHT (dic)->operand.symOperand;
2185 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2186 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2187 remiCodeFromeBBlock (ebp, dic);
2188 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2189 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2196 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2199 /*-----------------------------------------------------------------*/
2200 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2201 /*-----------------------------------------------------------------*/
2202 static int packRegsDPTRnuse( operand *op , int dptr)
2207 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2208 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2211 /* first check if any overlapping liverange has already been
2212 assigned to this DPTR */
2213 if (OP_SYMBOL(op)->clashes) {
2214 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2216 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2217 sym = hTabItemWithKey(liveRanges,i);
2218 if (sym->dptr == dptr) return 0;
2223 /* future for more dptrs */
2225 OP_SYMBOL(op)->dptr = dptr;
2229 /* DPTR1 is special since it is also used as a scratch by the backend .
2230 so we walk thru the entire live range of this operand and make sure
2231 DPTR1 will not be used by the backed . The logic here is to find out if
2232 more than one operand in an icode is in far space then we give up : we
2233 don't keep it live across functions for now
2236 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2237 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2238 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2241 if (ic->op == CALL || ic->op == PCALL) return 0;
2243 /* single operand icode are ok */
2244 if (ic->op == IFX || ic->op == IPUSH)
2247 if (ic->op == SEND ) {
2248 if (ic->argreg != 1 ) return 0;
2251 /* two special cases first */
2252 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2253 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2254 (isOperandInFarSpace(IC_RESULT(ic)) &&
2255 !isOperandInReg(IC_RESULT(ic)))) {
2259 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2260 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2261 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2262 !isOperandInReg(IC_RIGHT(ic)))) {
2266 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2267 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2268 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2269 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2270 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2274 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2275 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2276 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2277 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2278 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2281 /* same for right */
2282 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2283 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2284 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2285 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2286 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2290 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2291 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2293 if (nfs > 1) return 0;
2295 OP_SYMBOL(op)->dptr = dptr;
2299 /*-----------------------------------------------------------------*/
2300 /* packRegsDPTRuse : - will reduce some registers for single Use */
2301 /*-----------------------------------------------------------------*/
2303 packRegsDPTRuse (operand * op)
2305 /* go thru entire liveRange of this variable & check for
2306 other possible usage of DPTR , if we don't find it the
2307 assign this to DPTR (ruonly)
2312 sym_link *type, *etype;
2314 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2315 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2317 /* first check if any overlapping liverange has already been
2319 if (OP_SYMBOL(op)->clashes) {
2320 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2321 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2322 sym = hTabItemWithKey(liveRanges,i);
2323 if (sym->ruonly) return NULL ;
2328 /* no then go thru this guys live range */
2329 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2330 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2331 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2333 if (SKIP_IC3(ic)) continue;
2335 /* if PCALL cannot be sure give up */
2336 if (ic->op == PCALL) return NULL;
2338 /* if SEND & not the first parameter then giveup */
2339 if (ic->op == SEND && ic->argreg != 1 &&
2340 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2341 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2343 /* if CALL then make sure it is VOID || return value not used
2344 or the return value is assigned to this one */
2345 if (ic->op == CALL) {
2346 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2347 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2348 etype = getSpec(type = operandType(IC_RESULT(ic)));
2349 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2354 /* special case of add with a [remat] */
2355 if (ic->op == '+' &&
2356 OP_SYMBOL(IC_LEFT(ic))->remat &&
2357 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2358 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2362 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2363 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2366 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2367 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2369 /* conditionals can destroy 'b' - make sure B wont
2370 be used in this one*/
2371 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2372 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2373 getSize(operandType(op)) > 3) return NULL;
2375 /* if this is a cast to a bigger type */
2377 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2378 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2379 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2385 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2386 !isOperandEqual(IC_RESULT(ic),op) &&
2387 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2388 !isOperandInReg(IC_RESULT(ic))) ||
2389 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2391 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2392 !isOperandEqual(IC_RIGHT(ic),op) &&
2393 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2394 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2395 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2396 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2397 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2399 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2400 !isOperandEqual(IC_LEFT(ic),op) &&
2401 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2402 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2403 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2404 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2405 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2407 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2408 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2409 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2410 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2413 OP_SYMBOL(op)->ruonly = 1;
2414 if (OP_SYMBOL(op)->usl.spillLoc) {
2415 if (OP_SYMBOL(op)->spillA)
2416 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2417 OP_SYMBOL(op)->usl.spillLoc = NULL;
2422 /*-----------------------------------------------------------------*/
2423 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2424 /*-----------------------------------------------------------------*/
2426 isBitwiseOptimizable (iCode * ic)
2428 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2429 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2431 /* bitwise operations are considered optimizable
2432 under the following conditions (Jean-Louis VERN)
2444 if ( IS_LITERAL (rtype) ||
2445 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2451 /*-----------------------------------------------------------------*/
2452 /* packRegsForAccUse - pack registers for acc use */
2453 /*-----------------------------------------------------------------*/
2455 packRegsForAccUse (iCode * ic)
2459 /* if this is an aggregate, e.g. a one byte char array */
2460 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2464 /* if we are calling a reentrant function that has stack parameters */
2465 if (ic->op == CALL &&
2466 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2467 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2470 if (ic->op == PCALL &&
2471 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2472 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2475 /* if + or - then it has to be one byte result */
2476 if ((ic->op == '+' || ic->op == '-')
2477 && getSize (operandType (IC_RESULT (ic))) > 1)
2480 /* if shift operation make sure right side is not a literal */
2481 if (ic->op == RIGHT_OP &&
2482 (isOperandLiteral (IC_RIGHT (ic)) ||
2483 getSize (operandType (IC_RESULT (ic))) > 1))
2486 if (ic->op == LEFT_OP &&
2487 (isOperandLiteral (IC_RIGHT (ic)) ||
2488 getSize (operandType (IC_RESULT (ic))) > 1))
2491 if (IS_BITWISE_OP (ic) &&
2492 getSize (operandType (IC_RESULT (ic))) > 1)
2496 /* has only one definition */
2497 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2500 /* has only one use */
2501 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2504 /* and the usage immediately follows this iCode */
2505 if (!(uic = hTabItemWithKey (iCodehTab,
2506 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2509 if (ic->next != uic)
2512 /* if it is a conditional branch then we definitely can */
2516 if (uic->op == JUMPTABLE)
2519 /* if the usage is not is an assignment
2520 or an arithmetic / bitwise / shift operation then not */
2521 if (POINTER_SET (uic) &&
2522 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2525 if (uic->op != '=' &&
2526 !IS_ARITHMETIC_OP (uic) &&
2527 !IS_BITWISE_OP (uic) &&
2528 uic->op != LEFT_OP &&
2529 uic->op != RIGHT_OP)
2532 /* if used in ^ operation then make sure right is not a
2534 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2537 /* if shift operation make sure right side is not a literal */
2538 if (uic->op == RIGHT_OP &&
2539 (isOperandLiteral (IC_RIGHT (uic)) ||
2540 getSize (operandType (IC_RESULT (uic))) > 1))
2543 if (uic->op == LEFT_OP &&
2544 (isOperandLiteral (IC_RIGHT (uic)) ||
2545 getSize (operandType (IC_RESULT (uic))) > 1))
2548 /* make sure that the result of this icode is not on the
2549 stack, since acc is used to compute stack offset */
2550 if (isOperandOnStack(IC_RESULT(uic)))
2553 /* if either one of them in far space then we cannot */
2554 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2555 isOperandInFarSpace (IC_LEFT (uic))) ||
2556 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2557 isOperandInFarSpace (IC_RIGHT (uic))))
2560 /* if the usage has only one operand then we can */
2561 if (IC_LEFT (uic) == NULL ||
2562 IC_RIGHT (uic) == NULL)
2565 /* make sure this is on the left side if not
2566 a '+' since '+' is commutative */
2567 if (ic->op != '+' &&
2568 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2571 /* if the other one is not on stack then we can */
2572 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2573 (IS_ITEMP (IC_RIGHT (uic)) ||
2574 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2575 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2578 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2579 (IS_ITEMP (IC_LEFT (uic)) ||
2580 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2581 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2587 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2592 /*-----------------------------------------------------------------*/
2593 /* packForPush - hueristics to reduce iCode for pushing */
2594 /*-----------------------------------------------------------------*/
2596 packForPush (iCode * ic, eBBlock * ebp)
2601 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2604 /* must have only definition & one usage */
2605 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2606 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2609 /* find the definition */
2610 if (!(dic = hTabItemWithKey (iCodehTab,
2611 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2614 if (dic->op != '=' || POINTER_SET (dic))
2617 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2619 /* make sure the right side does not have any definitions
2621 dbv = OP_DEFS(IC_RIGHT(dic));
2622 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2623 if (bitVectBitValue(dbv,lic->key)) return ;
2625 /* make sure they have the same type */
2627 sym_link *itype=operandType(IC_LEFT(ic));
2628 sym_link *ditype=operandType(IC_RIGHT(dic));
2630 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2631 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2634 /* extend the live range of replaced operand if needed */
2635 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2636 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2637 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2638 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2639 OP_SYMBOL(IC_LEFT(ic))->clashes);
2641 for (lic = ic; lic && lic != dic; lic = lic->prev)
2643 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2644 if (IS_ITEMP (IC_RIGHT (dic)))
2645 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2647 /* we now we know that it has one & only one def & use
2648 and the that the definition is an assignment */
2649 IC_LEFT (ic) = IC_RIGHT (dic);
2651 remiCodeFromeBBlock (ebp, dic);
2652 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2653 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2656 /*-----------------------------------------------------------------*/
2657 /* packRegisters - does some transformations to reduce register */
2659 /*-----------------------------------------------------------------*/
2661 packRegisters (eBBlock * ebp)
2671 /* look for assignments of the form */
2672 /* iTempNN = TRueSym (someoperation) SomeOperand */
2674 /* TrueSym := iTempNN:1 */
2675 for (ic = ebp->sch; ic; ic = ic->next)
2679 /* find assignment of the form TrueSym := iTempNN:1 */
2680 if (ic->op == '=' && !POINTER_SET (ic))
2681 change += packRegsForAssign (ic, ebp);
2688 for (ic = ebp->sch; ic; ic = ic->next)
2691 /* if this is an itemp & result of a address of a true sym
2692 then mark this as rematerialisable */
2693 if (ic->op == ADDRESS_OF &&
2694 IS_ITEMP (IC_RESULT (ic)) &&
2695 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2696 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2697 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2700 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2701 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2702 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2706 /* if this is an itemp & used as a pointer
2707 & assigned to a literal then remat */
2708 if (IS_ASSIGN_ICODE(ic) &&
2709 IS_ITEMP(IC_RESULT(ic)) &&
2710 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2711 isOperandLiteral(IC_RIGHT(ic)))
2713 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2714 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2715 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2718 /* if straight assignment then carry remat flag if
2719 this is the only definition */
2720 if (ic->op == '=' &&
2721 !POINTER_SET (ic) &&
2722 IS_SYMOP (IC_RIGHT (ic)) &&
2723 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2724 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2725 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2728 OP_SYMBOL (IC_RESULT (ic))->remat =
2729 OP_SYMBOL (IC_RIGHT (ic))->remat;
2730 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2731 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2734 /* if cast to a generic pointer & the pointer being
2735 cast is remat, then we can remat this cast as well */
2736 if (ic->op == CAST &&
2737 IS_SYMOP(IC_RIGHT(ic)) &&
2738 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2739 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2740 sym_link *to_type = operandType(IC_LEFT(ic));
2741 sym_link *from_type = operandType(IC_RIGHT(ic));
2742 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2743 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2744 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2745 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2749 /* if this is a +/- operation with a rematerizable
2750 then mark this as rematerializable as well */
2751 if ((ic->op == '+' || ic->op == '-') &&
2752 (IS_SYMOP (IC_LEFT (ic)) &&
2753 IS_ITEMP (IC_RESULT (ic)) &&
2754 OP_SYMBOL (IC_LEFT (ic))->remat &&
2755 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2756 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2757 IS_OP_LITERAL (IC_RIGHT (ic))))
2760 //int i = operandLitValue(IC_RIGHT(ic));
2761 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2762 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2763 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2766 /* mark the pointer usages */
2767 if (POINTER_SET (ic))
2768 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2770 if (POINTER_GET (ic))
2771 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2773 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2774 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2776 if (ic->op == RECEIVE && ic->argreg == 1 &&
2777 getSize (operandType(IC_RESULT(ic))) <= 3)
2778 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2780 if (ic->op == SEND && ic->argreg == 1 &&
2781 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2782 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2786 /* if we are using a symbol on the stack
2787 then we should say ds390_ptrRegReq */
2788 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2789 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2790 OP_SYMBOL (IC_COND (ic))->iaccess);
2791 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2792 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2793 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2796 if (IS_SYMOP (IC_LEFT (ic)))
2797 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2798 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2799 if (IS_SYMOP (IC_RIGHT (ic)))
2800 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2801 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2802 if (IS_SYMOP (IC_RESULT (ic)))
2803 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2804 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2808 /* if the condition of an if instruction
2809 is defined in the previous instruction and
2810 this is the only usage then
2811 mark the itemp as a conditional */
2812 if ((IS_CONDITIONAL (ic) ||
2813 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2814 ic->next && ic->next->op == IFX &&
2815 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2816 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2817 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2819 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2823 /* reduce for support function calls */
2824 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2825 packRegsForSupport (ic, ebp);
2827 /* some cases the redundant moves can
2828 can be eliminated for return statements . Can be elminated for the first SEND */
2829 if ((ic->op == RETURN ||
2830 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
2831 !isOperandInFarSpace (IC_LEFT (ic)) &&
2834 packRegsDPTRuse (IC_LEFT (ic));
2837 if (ic->op == CALL) {
2838 sym_link *ftype = operandType(IC_LEFT(ic));
2839 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2840 !IFFUNC_ISBUILTIN(ftype)) {
2841 packRegsDPTRuse (IC_RESULT (ic));
2845 /* if pointer set & left has a size more than
2846 one and right is not in far space */
2847 if (POINTER_SET (ic) &&
2848 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2849 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2850 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2851 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2853 packRegsDPTRuse (IC_RESULT (ic));
2856 /* if pointer get */
2857 if (POINTER_GET (ic) &&
2858 !isOperandInFarSpace (IC_RESULT (ic)) &&
2859 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2860 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2861 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2863 packRegsDPTRuse (IC_LEFT (ic));
2866 /* if this is cast for intergral promotion then
2867 check if only use of the definition of the
2868 operand being casted/ if yes then replace
2869 the result of that arithmetic operation with
2870 this result and get rid of the cast */
2873 sym_link *fromType = operandType (IC_RIGHT (ic));
2874 sym_link *toType = operandType (IC_LEFT (ic));
2876 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2877 getSize (fromType) != getSize (toType) &&
2878 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2881 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2884 if (IS_ARITHMETIC_OP (dic))
2886 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2887 IC_RESULT (dic) = IC_RESULT (ic);
2888 remiCodeFromeBBlock (ebp, ic);
2889 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2890 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2891 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2895 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2901 /* if the type from and type to are the same
2902 then if this is the only use then packit */
2903 if (compareType (operandType (IC_RIGHT (ic)),
2904 operandType (IC_LEFT (ic))) == 1)
2906 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2909 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2910 IC_RESULT (dic) = IC_RESULT (ic);
2911 remiCodeFromeBBlock (ebp, ic);
2912 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2913 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2914 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2922 iTempNN := (some variable in farspace) V1
2927 if (ic->op == IPUSH || ic->op == SEND)
2929 packForPush (ic, ebp);
2933 /* pack registers for accumulator use, when the
2934 result of an arithmetic or bit wise operation
2935 has only one use, that use is immediately following
2936 the defintion and the using iCode has only one
2937 operand or has two operands but one is literal &
2938 the result of that operation is not on stack then
2939 we can leave the result of this operation in acc:b
2941 if ((IS_ARITHMETIC_OP (ic)
2942 || IS_CONDITIONAL(ic)
2943 || IS_BITWISE_OP (ic)
2944 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2945 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2947 IS_ITEMP (IC_RESULT (ic)) &&
2948 getSize (operandType (IC_RESULT (ic))) <= 2)
2950 packRegsForAccUse (ic);
2955 /*-----------------------------------------------------------------*/
2956 /* assignRegisters - assigns registers to each live range as need */
2957 /*-----------------------------------------------------------------*/
2959 ds390_assignRegisters (eBBlock ** ebbs, int count)
2964 setToNull ((void *) &_G.funcrUsed);
2965 setToNull ((void *) &_G.regAssigned);
2966 setToNull ((void *) &_G.totRegAssigned);
2967 setToNull ((void *) &_G.funcrUsed);
2968 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2970 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2971 /* change assignments this will remove some
2972 live ranges reducing some register pressure */
2973 for (i = 0; i < count; i++)
2974 packRegisters (ebbs[i]);
2976 if (options.dump_pack)
2977 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2979 /* first determine for each live range the number of
2980 registers & the type of registers required for each */
2983 /* and serially allocate registers */
2984 serialRegAssign (ebbs, count);
2991 /* if stack was extended then tell the user */
2994 /* werror(W_TOOMANY_SPILS,"stack", */
2995 /* _G.stackExtend,currFunc->name,""); */
3001 /* werror(W_TOOMANY_SPILS,"data space", */
3002 /* _G.dataExtend,currFunc->name,""); */
3006 /* after that create the register mask
3007 for each of the instruction */
3008 createRegMask (ebbs, count);
3010 /* redo that offsets for stacked automatic variables */
3011 redoStackOffsets ();
3013 if (options.dump_rassgn) {
3014 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3015 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3018 /* do the overlaysegment stuff SDCCmem.c */
3019 doOverlays (ebbs, count);
3021 /* now get back the chain */
3022 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3027 /* free up any _G.stackSpil locations allocated */
3028 applyToSet (_G.stackSpil, deallocStackSpil);
3030 setToNull ((void **) &_G.stackSpil);
3031 setToNull ((void **) &_G.spiltSet);
3032 /* mark all registers as free */