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 (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
489 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
494 sloc = newiTemp (slocBuffer);
496 /* set the type to the spilling symbol */
497 sloc->type = copyLinkChain (sym->type);
498 sloc->etype = getSpec (sloc->type);
499 if (options.model == MODEL_SMALL) {
500 SPEC_SCLS (sloc->etype) = S_DATA;
502 SPEC_SCLS (sloc->etype) = S_XDATA;
504 SPEC_EXTR (sloc->etype) = 0;
505 SPEC_STAT (sloc->etype) = 0;
506 SPEC_VOLATILE(sloc->etype) = 0;
507 SPEC_ABSA(sloc->etype) = 0;
509 /* we don't allow it to be allocated`
510 onto the external stack since : so we
511 temporarily turn it off ; we also
512 turn off memory model to prevent
513 the spil from going to the external storage
514 and turn off overlaying
517 useXstack = options.useXstack;
518 model = options.model;
519 noOverlay = options.noOverlay;
520 options.noOverlay = 1;
522 /* options.model = options.useXstack = 0; */
526 options.useXstack = useXstack;
527 options.model = model;
528 options.noOverlay = noOverlay;
529 sloc->isref = 1; /* to prevent compiler warning */
531 /* if it is on the stack then update the stack */
532 if (IN_STACK (sloc->etype))
534 currFunc->stack += getSize (sloc->type);
535 _G.stackExtend += getSize (sloc->type);
538 _G.dataExtend += getSize (sloc->type);
540 /* add it to the _G.stackSpil set */
541 addSetHead (&_G.stackSpil, sloc);
542 sym->usl.spillLoc = sloc;
545 /* add it to the set of itempStack set
546 of the spill location */
547 addSetHead (&sloc->usl.itmpStack, sym);
551 /*-----------------------------------------------------------------*/
552 /* isSpiltOnStack - returns true if the spil location is on stack */
553 /*-----------------------------------------------------------------*/
555 isSpiltOnStack (symbol * sym)
565 /* if (sym->_G.stackSpil) */
568 if (!sym->usl.spillLoc)
571 etype = getSpec (sym->usl.spillLoc->type);
572 if (IN_STACK (etype))
578 /*-----------------------------------------------------------------*/
579 /* spillThis - spils a specific operand */
580 /*-----------------------------------------------------------------*/
582 spillThis (symbol * sym)
585 /* if this is rematerializable or has a spillLocation
586 we are okay, else we need to create a spillLocation
588 if (!(sym->remat || sym->usl.spillLoc))
589 createStackSpil (sym);
592 /* mark it has spilt & put it in the spilt set */
593 sym->isspilt = sym->spillA = 1;
594 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
596 bitVectUnSetBit (_G.regAssigned, sym->key);
597 bitVectUnSetBit (_G.totRegAssigned, sym->key);
599 for (i = 0; i < sym->nRegs; i++)
603 freeReg (sym->regs[i]);
607 /* if spilt on stack then free up r0 & r1
608 if they could have been assigned to some
610 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
612 ds390_ptrRegReq += !options.stack10bit;
613 spillLRWithPtrReg (sym);
616 if (sym->usl.spillLoc && !sym->remat)
617 sym->usl.spillLoc->allocreq++;
621 /*-----------------------------------------------------------------*/
622 /* selectSpil - select a iTemp to spil : rather a simple procedure */
623 /*-----------------------------------------------------------------*/
625 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
627 bitVect *lrcs = NULL;
631 /* get the spillable live ranges */
632 lrcs = computeSpillable (ic);
634 /* get all live ranges that are rematerizable */
635 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
638 /* return the least used of these */
639 return leastUsedLR (selectS);
642 /* get live ranges with spillLocations in direct space */
643 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
645 sym = leastUsedLR (selectS);
646 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
647 sym->usl.spillLoc->rname :
648 sym->usl.spillLoc->name));
650 /* mark it as allocation required */
651 sym->usl.spillLoc->allocreq++;
655 /* if the symbol is local to the block then */
656 if (forSym->liveTo < ebp->lSeq)
659 /* check if there are any live ranges allocated
660 to registers that are not used in this block */
661 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
663 sym = leastUsedLR (selectS);
664 /* if this is not rematerializable */
673 /* check if there are any live ranges that not
674 used in the remainder of the block */
675 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
677 sym = leastUsedLR (selectS);
690 /* find live ranges with spillocation && not used as pointers */
691 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
694 sym = leastUsedLR (selectS);
695 /* mark this as allocation required */
696 sym->usl.spillLoc->allocreq++;
700 /* find live ranges with spillocation */
701 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
704 sym = leastUsedLR (selectS);
705 sym->usl.spillLoc->allocreq++;
709 /* couldn't find then we need to create a spil
710 location on the stack , for which one? the least
712 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
715 /* return a created spil location */
716 sym = createStackSpil (leastUsedLR (selectS));
717 sym->usl.spillLoc->allocreq++;
721 /* this is an extreme situation we will spill
722 this one : happens very rarely but it does happen */
728 /*-----------------------------------------------------------------*/
729 /* spilSomething - spil some variable & mark registers as free */
730 /*-----------------------------------------------------------------*/
732 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
737 /* get something we can spil */
738 ssym = selectSpil (ic, ebp, forSym);
740 /* mark it as spilt */
741 ssym->isspilt = ssym->spillA = 1;
742 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
744 /* mark it as not register assigned &
745 take it away from the set */
746 bitVectUnSetBit (_G.regAssigned, ssym->key);
747 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
749 /* mark the registers as free */
750 for (i = 0; i < ssym->nRegs; i++)
752 freeReg (ssym->regs[i]);
754 /* if spilt on stack then free up r0 & r1
755 if they could have been assigned to as gprs */
756 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
759 spillLRWithPtrReg (ssym);
762 /* if this was a block level spil then insert push & pop
763 at the start & end of block respectively */
766 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
767 /* add push to the start of the block */
768 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
769 ebp->sch->next : ebp->sch));
770 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
771 /* add pop to the end of the block */
772 addiCodeToeBBlock (ebp, nic, NULL);
775 /* if spilt because not used in the remainder of the
776 block then add a push before this instruction and
777 a pop at the end of the block */
778 if (ssym->remainSpil)
781 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
782 /* add push just before this instruction */
783 addiCodeToeBBlock (ebp, nic, ic);
785 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
786 /* add pop to the end of the block */
787 addiCodeToeBBlock (ebp, nic, NULL);
796 /*-----------------------------------------------------------------*/
797 /* getRegPtr - will try for PTR if not a GPR type if not spil */
798 /*-----------------------------------------------------------------*/
800 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
805 /* try for a ptr type */
806 if ((reg = allocReg (REG_PTR)))
809 /* try for gpr type */
810 if ((reg = allocReg (REG_GPR)))
813 /* we have to spil */
814 if (!spilSomething (ic, ebp, sym))
817 /* this looks like an infinite loop but
818 in really selectSpil will abort */
822 /*-----------------------------------------------------------------*/
823 /* getRegGpr - will try for GPR if not spil */
824 /*-----------------------------------------------------------------*/
826 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
831 /* try for gpr type */
832 if ((reg = allocReg (REG_GPR)))
835 if (!ds390_ptrRegReq)
836 if ((reg = allocReg (REG_PTR)))
839 /* we have to spil */
840 if (!spilSomething (ic, ebp, sym))
843 /* this looks like an infinite loop but
844 in really selectSpil will abort */
848 /*-----------------------------------------------------------------*/
849 /* getRegPtrNoSpil - get it cannot split */
850 /*-----------------------------------------------------------------*/
851 static regs *getRegPtrNoSpil()
855 /* try for a ptr type */
856 if ((reg = allocReg (REG_PTR)))
859 /* try for gpr type */
860 if ((reg = allocReg (REG_GPR)))
866 /*-----------------------------------------------------------------*/
867 /* getRegGprNoSpil - get it cannot split */
868 /*-----------------------------------------------------------------*/
869 static regs *getRegGprNoSpil()
873 if ((reg = allocReg (REG_GPR)))
876 if (!ds390_ptrRegReq)
877 if ((reg = allocReg (REG_PTR)))
883 /*-----------------------------------------------------------------*/
884 /* symHasReg - symbol has a given register */
885 /*-----------------------------------------------------------------*/
887 symHasReg (symbol * sym, regs * reg)
891 for (i = 0; i < sym->nRegs; i++)
892 if (sym->regs[i] == reg)
898 /*-----------------------------------------------------------------*/
899 /* deassignLRs - check the live to and if they have registers & are */
900 /* not spilt then free up the registers */
901 /*-----------------------------------------------------------------*/
903 deassignLRs (iCode * ic, eBBlock * ebp)
909 for (sym = hTabFirstItem (liveRanges, &k); sym;
910 sym = hTabNextItem (liveRanges, &k))
914 /* if it does not end here */
915 if (sym->liveTo > ic->seq)
918 /* if it was spilt on stack then we can
919 mark the stack spil location as free */
924 sym->usl.spillLoc->isFree = 1;
930 if (!bitVectBitValue (_G.regAssigned, sym->key))
933 /* special case check if this is an IFX &
934 the privious one was a pop and the
935 previous one was not spilt then keep track
937 if (ic->op == IFX && ic->prev &&
938 ic->prev->op == IPOP &&
939 !ic->prev->parmPush &&
940 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
941 psym = OP_SYMBOL (IC_LEFT (ic->prev));
947 bitVectUnSetBit (_G.regAssigned, sym->key);
949 /* if the result of this one needs registers
950 and does not have it then assign it right
952 if (IC_RESULT (ic) &&
953 !(SKIP_IC2 (ic) || /* not a special icode */
954 ic->op == JUMPTABLE ||
960 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
961 result->liveTo > ic->seq && /* and will live beyond this */
962 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
963 result->regType == sym->regType && /* same register types */
964 result->nRegs && /* which needs registers */
965 !result->isspilt && /* and does not already have them */
967 !bitVectBitValue (_G.regAssigned, result->key) &&
968 /* the number of free regs + number of regs in this LR
969 can accomodate the what result Needs */
970 ((nfreeRegsType (result->regType) +
971 sym->nRegs) >= result->nRegs)
975 for (i = 0; i < result->nRegs; i++)
977 result->regs[i] = sym->regs[i];
979 result->regs[i] = getRegGpr (ic, ebp, result);
981 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
982 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
986 /* free the remaining */
987 for (; i < sym->nRegs; i++)
991 if (!symHasReg (psym, sym->regs[i]))
992 freeReg (sym->regs[i]);
995 freeReg (sym->regs[i]);
1002 /*-----------------------------------------------------------------*/
1003 /* reassignLR - reassign this to registers */
1004 /*-----------------------------------------------------------------*/
1006 reassignLR (operand * op)
1008 symbol *sym = OP_SYMBOL (op);
1011 /* not spilt any more */
1012 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1013 bitVectUnSetBit (_G.spiltSet, sym->key);
1015 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1016 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1020 for (i = 0; i < sym->nRegs; i++)
1021 sym->regs[i]->isFree = 0;
1024 /*-----------------------------------------------------------------*/
1025 /* willCauseSpill - determines if allocating will cause a spill */
1026 /*-----------------------------------------------------------------*/
1028 willCauseSpill (int nr, int rt)
1030 /* first check if there are any avlb registers
1031 of te type required */
1034 /* special case for pointer type
1035 if pointer type not avlb then
1036 check for type gpr */
1037 if (nFreeRegs (rt) >= nr)
1039 if (nFreeRegs (REG_GPR) >= nr)
1044 if (ds390_ptrRegReq)
1046 if (nFreeRegs (rt) >= nr)
1051 if (nFreeRegs (REG_PTR) +
1052 nFreeRegs (REG_GPR) >= nr)
1057 /* it will cause a spil */
1061 /*-----------------------------------------------------------------*/
1062 /* positionRegs - the allocator can allocate same registers to res- */
1063 /* ult and operand, if this happens make sure they are in the same */
1064 /* position as the operand otherwise chaos results */
1065 /*-----------------------------------------------------------------*/
1067 positionRegs (symbol * result, symbol * opsym)
1069 int count = min (result->nRegs, opsym->nRegs);
1070 int i, j = 0, shared = 0;
1073 /* if the result has been spilt then cannot share */
1078 /* first make sure that they actually share */
1079 for (i = 0; i < count; i++)
1081 for (j = 0; j < count; j++)
1083 if (result->regs[i] == opsym->regs[j] && i != j)
1093 regs *tmp = result->regs[i];
1094 result->regs[i] = result->regs[j];
1095 result->regs[j] = tmp;
1102 /*-----------------------------------------------------------------*/
1103 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1104 /*-----------------------------------------------------------------*/
1105 bitVect *unusedLRs (eBBlock *ebp)
1107 bitVect *ret = NULL;
1111 if (!ebp) return NULL;
1112 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1113 sym = hTabNextItem(liveRanges,&key)) {
1115 if (notUsedInBlock(sym,ebp,NULL)) {
1116 ret = bitVectSetBit(ret,sym->key);
1123 /*-----------------------------------------------------------------*/
1124 /* deassignUnsedLRs - if this baisc block ends in a return then */
1125 /* deassign symbols not used in this block */
1126 /*-----------------------------------------------------------------*/
1127 bitVect *deassignUnsedLRs(eBBlock *ebp)
1129 bitVect *unused = NULL;
1132 switch (returnAtEnd(ebp)) {
1133 case 2: /* successor block ends in a return */
1134 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1136 case 1: /* this block ends in a return */
1137 unused = bitVectIntersect(unused,unusedLRs(ebp));
1142 for (i = 0 ; i < unused->size ; i++ ) {
1145 if (bitVectBitValue(unused,i)) {
1147 /* if assigned to registers */
1148 if (bitVectBitValue(_G.regAssigned,i)) {
1152 sym = hTabItemWithKey(liveRanges,i);
1153 /* remove it from regassigned & mark the
1155 bitVectUnSetBit(_G.regAssigned,i);
1156 for (j = 0 ; j < sym->nRegs; j++)
1157 freeReg(sym->regs[j]);
1159 /* not assigned to registers : remove from set*/
1160 bitVectUnSetBit(unused,i);
1168 /*-----------------------------------------------------------------*/
1169 /* reassignUnusedLRs - put registers to unused Live ranges */
1170 /*-----------------------------------------------------------------*/
1171 void reassignUnusedLRs (bitVect *unused)
1174 if (!unused) return ;
1176 for (i = 0 ; i < unused->size ; i++ ) {
1177 /* if unused : means it was assigned to registers before */
1178 if (bitVectBitValue(unused,i)) {
1182 /* put it back into reg set*/
1183 bitVectSetBit(_G.regAssigned,i) ;
1185 sym = hTabItemWithKey(liveRanges,i);
1186 /* makr registers busy */
1187 for (j = 0 ; j < sym->nRegs; j++)
1188 sym->regs[j]->isFree = 0;
1193 /*-----------------------------------------------------------------*/
1194 /* serialRegAssign - serially allocate registers to the variables */
1195 /*-----------------------------------------------------------------*/
1197 serialRegAssign (eBBlock ** ebbs, int count)
1201 /* for all blocks */
1202 for (i = 0; i < count; i++)
1206 bitVect *unusedLRs = NULL;
1208 if (ebbs[i]->noPath &&
1209 (ebbs[i]->entryLabel != entryLabel &&
1210 ebbs[i]->entryLabel != returnLabel))
1213 unusedLRs = deassignUnsedLRs(ebbs[i]);
1215 /* of all instructions do */
1216 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1219 /* if this is an ipop that means some live
1220 range will have to be assigned again */
1222 reassignLR (IC_LEFT (ic));
1224 /* if result is present && is a true symbol */
1225 if (IC_RESULT (ic) && ic->op != IFX &&
1226 IS_TRUE_SYMOP (IC_RESULT (ic)))
1227 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1229 /* take away registers from live
1230 ranges that end at this instruction */
1231 deassignLRs (ic, ebbs[i]);
1233 /* some don't need registers */
1234 if (SKIP_IC2 (ic) ||
1235 ic->op == JUMPTABLE ||
1239 (IC_RESULT (ic) && POINTER_SET (ic)))
1242 /* now we need to allocate registers
1243 only for the result */
1246 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1252 /* if it does not need or is spilt
1253 or is already assigned to registers
1254 or will not live beyond this instructions */
1257 bitVectBitValue (_G.regAssigned, sym->key) ||
1258 sym->liveTo <= ic->seq)
1261 /* if some liverange has been spilt at the block level
1262 and this one live beyond this block then spil this
1264 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1269 /* if trying to allocate this will cause
1270 a spill and there is nothing to spill
1271 or this one is rematerializable then
1273 willCS = willCauseSpill (sym->nRegs, sym->regType);
1274 spillable = computeSpillable (ic);
1276 (willCS && bitVectIsZero (spillable)))
1284 /* if it has a spillocation & is used less than
1285 all other live ranges then spill this */
1287 if (sym->usl.spillLoc) {
1288 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1289 allLRs, ebbs[i], ic));
1290 if (leastUsed && leastUsed->used > sym->used) {
1295 /* if none of the liveRanges have a spillLocation then better
1296 to spill this one than anything else already assigned to registers */
1297 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1298 /* if this is local to this block then we might find a block spil */
1299 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1307 /* if we need ptr regs for the right side
1309 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1310 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1311 <= (unsigned) PTRSIZE)
1316 /* else we assign registers to it */
1317 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1318 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1320 for (j = 0; j < sym->nRegs; j++)
1322 if (sym->regType == REG_PTR)
1323 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1325 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1327 /* if the allocation falied which means
1328 this was spilt then break */
1332 /* if it shares registers with operands make sure
1333 that they are in the same position */
1334 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1335 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1336 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1337 OP_SYMBOL (IC_LEFT (ic)));
1338 /* do the same for the right operand */
1339 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1340 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1341 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1342 OP_SYMBOL (IC_RIGHT (ic)));
1352 reassignUnusedLRs(unusedLRs);
1356 /*-----------------------------------------------------------------*/
1357 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1358 /*-----------------------------------------------------------------*/
1359 static void fillGaps()
1363 int loop = 0, change;
1365 if (getenv("DISABLE_FILL_GAPS")) return;
1367 /* First try to do DPTRuse once more since now we know what got into
1370 while (loop++ < 10) {
1373 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1374 sym = hTabNextItem(liveRanges,&key)) {
1375 int size = getSize(sym->type);
1377 if (sym->liveFrom == sym->liveTo) continue;
1379 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1380 size < 4 && size > 1) {
1382 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1384 /* if this was ssigned to registers then */
1385 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1386 /* take it out of the register assigned set */
1387 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1388 } else if (sym->usl.spillLoc) {
1389 sym->usl.spillLoc->allocreq--;
1390 sym->usl.spillLoc = NULL;
1394 sym->isspilt = sym->spillA = 0;
1398 /* try assigning other dptrs */
1399 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1400 /* if this was ssigned to registers then */
1401 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1402 /* take it out of the register assigned set */
1403 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1404 } else if (sym->usl.spillLoc) {
1405 sym->usl.spillLoc->allocreq--;
1406 sym->usl.spillLoc = NULL;
1409 sym->isspilt = sym->spillA = 0;
1414 /* look for livernages that was spilt by the allocator */
1415 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1416 sym = hTabNextItem(liveRanges,&key)) {
1421 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1422 if (!sym->uses || !sym->defs) continue ;
1423 /* find the liveRanges this one clashes with, that are
1424 still assigned to registers & mark the registers as used*/
1425 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1429 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1430 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1433 assert (clr = hTabItemWithKey(liveRanges,i));
1435 /* mark these registers as used */
1436 for (k = 0 ; k < clr->nRegs ; k++ )
1437 useReg(clr->regs[k]);
1440 if (willCauseSpill(sym->nRegs,sym->regType)) {
1441 /* NOPE :( clear all registers & and continue */
1446 /* THERE IS HOPE !!!! */
1447 for (i=0; i < sym->nRegs ; i++ ) {
1448 if (sym->regType == REG_PTR)
1449 sym->regs[i] = getRegPtrNoSpil ();
1451 sym->regs[i] = getRegGprNoSpil ();
1454 /* for all its definitions & uses check if the registers
1455 allocated needs positioning NOTE: we can position
1456 only ONCE if more than One positioning required
1459 for (i = 0 ; i < sym->defs->size ; i++ ) {
1460 if (bitVectBitValue(sym->defs,i)) {
1462 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1463 if (SKIP_IC(ic)) continue;
1464 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1465 /* if left is assigned to registers */
1466 if (IS_SYMOP(IC_LEFT(ic)) &&
1467 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1468 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1470 if (IS_SYMOP(IC_RIGHT(ic)) &&
1471 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1472 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1474 if (pdone > 1) break;
1477 for (i = 0 ; i < sym->uses->size ; i++ ) {
1478 if (bitVectBitValue(sym->uses,i)) {
1480 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1481 if (SKIP_IC(ic)) continue;
1482 if (!IS_ASSIGN_ICODE(ic)) continue ;
1484 /* if result is assigned to registers */
1485 if (IS_SYMOP(IC_RESULT(ic)) &&
1486 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1487 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1489 if (pdone > 1) break;
1492 /* had to position more than once GIVE UP */
1494 /* UNDO all the changes we made to try this */
1496 for (i=0; i < sym->nRegs ; i++ ) {
1497 sym->regs[i] = NULL;
1500 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1501 "%s in function %s\n",
1502 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1505 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1506 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1507 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1508 sym->isspilt = sym->spillA = 0 ;
1509 sym->usl.spillLoc->allocreq--;
1510 sym->usl.spillLoc = NULL;
1518 /*-----------------------------------------------------------------*/
1519 /* rUmaskForOp :- returns register mask for an operand */
1520 /*-----------------------------------------------------------------*/
1522 ds390_rUmaskForOp (operand * op)
1528 /* only temporaries are assigned registers */
1532 sym = OP_SYMBOL (op);
1534 /* if spilt or no registers assigned to it
1536 if (sym->isspilt || !sym->nRegs)
1539 rumask = newBitVect (ds390_nRegs);
1541 for (j = 0; j < sym->nRegs; j++)
1543 rumask = bitVectSetBit (rumask,
1544 sym->regs[j]->rIdx);
1550 /*-----------------------------------------------------------------*/
1551 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1552 /*-----------------------------------------------------------------*/
1554 regsUsedIniCode (iCode * ic)
1556 bitVect *rmask = newBitVect (ds390_nRegs);
1558 /* do the special cases first */
1561 rmask = bitVectUnion (rmask,
1562 ds390_rUmaskForOp (IC_COND (ic)));
1566 /* for the jumptable */
1567 if (ic->op == JUMPTABLE)
1569 rmask = bitVectUnion (rmask,
1570 ds390_rUmaskForOp (IC_JTCOND (ic)));
1575 /* of all other cases */
1577 rmask = bitVectUnion (rmask,
1578 ds390_rUmaskForOp (IC_LEFT (ic)));
1582 rmask = bitVectUnion (rmask,
1583 ds390_rUmaskForOp (IC_RIGHT (ic)));
1586 rmask = bitVectUnion (rmask,
1587 ds390_rUmaskForOp (IC_RESULT (ic)));
1593 /*-----------------------------------------------------------------*/
1594 /* createRegMask - for each instruction will determine the regsUsed */
1595 /*-----------------------------------------------------------------*/
1597 createRegMask (eBBlock ** ebbs, int count)
1601 /* for all blocks */
1602 for (i = 0; i < count; i++)
1606 if (ebbs[i]->noPath &&
1607 (ebbs[i]->entryLabel != entryLabel &&
1608 ebbs[i]->entryLabel != returnLabel))
1611 /* for all instructions */
1612 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1617 if (SKIP_IC2 (ic) || !ic->rlive)
1620 /* first mark the registers used in this
1622 ic->rUsed = regsUsedIniCode (ic);
1623 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1625 /* now create the register mask for those
1626 registers that are in use : this is a
1627 super set of ic->rUsed */
1628 ic->rMask = newBitVect (ds390_nRegs + 1);
1630 /* for all live Ranges alive at this point */
1631 for (j = 1; j < ic->rlive->size; j++)
1636 /* if not alive then continue */
1637 if (!bitVectBitValue (ic->rlive, j))
1640 /* find the live range we are interested in */
1641 if (!(sym = hTabItemWithKey (liveRanges, j)))
1643 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1644 "createRegMask cannot find live range");
1648 /* special case for ruonly */
1649 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1650 int size = getSize(sym->type);
1652 for (k = 0 ; k < size; k++ )
1653 ic->rMask = bitVectSetBit (ic->rMask, j++);
1656 /* if no register assigned to it */
1657 if (!sym->nRegs || sym->isspilt)
1660 /* for all the registers allocated to it */
1661 for (k = 0; k < sym->nRegs; k++)
1664 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1670 /*-----------------------------------------------------------------*/
1671 /* rematStr - returns the rematerialized string for a remat var */
1672 /*-----------------------------------------------------------------*/
1674 rematStr (symbol * sym)
1677 iCode *ic = sym->rematiCode;
1682 /* if plus or minus print the right hand side */
1683 if (ic->op == '+' || ic->op == '-')
1685 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1688 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1691 /* cast then continue */
1692 if (IS_CAST_ICODE(ic)) {
1693 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1696 /* we reached the end */
1697 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1704 /*-----------------------------------------------------------------*/
1705 /* regTypeNum - computes the type & number of registers required */
1706 /*-----------------------------------------------------------------*/
1714 /* for each live range do */
1715 for (sym = hTabFirstItem (liveRanges, &k); sym;
1716 sym = hTabNextItem (liveRanges, &k))
1719 /* if used zero times then no registers needed */
1720 if ((sym->liveTo - sym->liveFrom) == 0)
1724 /* if the live range is a temporary */
1728 /* if the type is marked as a conditional */
1729 if (sym->regType == REG_CND)
1732 /* if used in return only then we don't
1734 if (sym->ruonly || sym->accuse)
1736 if (IS_AGGREGATE (sym->type) || sym->isptr)
1737 sym->type = aggrToPtr (sym->type, FALSE);
1741 /* if the symbol has only one definition &
1742 that definition is a get_pointer */
1743 if (bitVectnBitsOn (sym->defs) == 1 &&
1744 (ic = hTabItemWithKey (iCodehTab,
1745 bitVectFirstBit (sym->defs))) &&
1748 !IS_BITVAR (sym->etype))
1750 /* and that pointer is remat in data space */
1751 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1752 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1753 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1756 /* create a psuedo symbol & force a spil */
1757 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1758 psym->type = sym->type;
1759 psym->etype = sym->etype;
1760 strcpy (psym->rname, psym->name);
1762 sym->usl.spillLoc = psym;
1766 /* if in data space or idata space then try to
1767 allocate pointer register */
1771 /* if not then we require registers */
1772 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1773 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1774 getSize (sym->type));
1778 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1779 printTypeChain (sym->type, stderr);
1780 fprintf (stderr, "\n");
1783 /* determine the type of register required */
1784 if (sym->nRegs == 1 &&
1785 IS_PTR (sym->type) &&
1787 sym->regType = REG_PTR;
1789 sym->regType = REG_GPR;
1793 /* for the first run we don't provide */
1794 /* registers for true symbols we will */
1795 /* see how things go */
1801 /*-----------------------------------------------------------------*/
1802 /* freeAllRegs - mark all registers as free */
1803 /*-----------------------------------------------------------------*/
1809 for (i = 0; i < ds390_nRegs; i++)
1810 regs390[i].isFree = 1;
1813 /*-----------------------------------------------------------------*/
1814 /* deallocStackSpil - this will set the stack pointer back */
1815 /*-----------------------------------------------------------------*/
1817 DEFSETFUNC (deallocStackSpil)
1825 /*-----------------------------------------------------------------*/
1826 /* farSpacePackable - returns the packable icode for far variables */
1827 /*-----------------------------------------------------------------*/
1829 farSpacePackable (iCode * ic)
1833 /* go thru till we find a definition for the
1834 symbol on the right */
1835 for (dic = ic->prev; dic; dic = dic->prev)
1838 /* if the definition is a call then no */
1839 if ((dic->op == CALL || dic->op == PCALL) &&
1840 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1845 /* if shift by unknown amount then not */
1846 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1847 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1850 /* if pointer get and size > 1 */
1851 if (POINTER_GET (dic) &&
1852 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1855 if (POINTER_SET (dic) &&
1856 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1859 /* if any three is a true symbol in far space */
1860 if (IC_RESULT (dic) &&
1861 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1862 isOperandInFarSpace (IC_RESULT (dic)))
1865 if (IC_RIGHT (dic) &&
1866 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1867 isOperandInFarSpace (IC_RIGHT (dic)) &&
1868 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1871 if (IC_LEFT (dic) &&
1872 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1873 isOperandInFarSpace (IC_LEFT (dic)) &&
1874 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1877 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1879 if ((dic->op == LEFT_OP ||
1880 dic->op == RIGHT_OP ||
1882 IS_OP_LITERAL (IC_RIGHT (dic)))
1892 /*-----------------------------------------------------------------*/
1893 /* packRegsForAssign - register reduction for assignment */
1894 /*-----------------------------------------------------------------*/
1896 packRegsForAssign (iCode * ic, eBBlock * ebp)
1900 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1901 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1902 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1907 /* if the true symbol is defined in far space or on stack
1908 then we should not since this will increase register pressure */
1910 if (isOperandInFarSpace (IC_RESULT (ic)))
1912 if ((dic = farSpacePackable (ic)))
1918 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1923 /* find the definition of iTempNN scanning backwards if we find a
1924 a use of the true symbol in before we find the definition then
1926 for (dic = ic->prev; dic; dic = dic->prev)
1928 /* if there is a function call then don't pack it */
1929 if ((dic->op == CALL || dic->op == PCALL))
1938 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1939 IS_OP_VOLATILE (IC_RESULT (dic)))
1945 if (IS_SYMOP (IC_RESULT (dic)) &&
1946 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1948 if (POINTER_SET (dic))
1954 if (IS_SYMOP (IC_RIGHT (dic)) &&
1955 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1956 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1962 if (IS_SYMOP (IC_LEFT (dic)) &&
1963 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1964 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1970 if (POINTER_SET (dic) &&
1971 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1979 return 0; /* did not find */
1981 /* if the result is on stack or iaccess then it must be
1982 the same atleast one of the operands */
1983 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1984 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1987 /* the operation has only one symbol
1988 operator then we can pack */
1989 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1990 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1993 if (!((IC_LEFT (dic) &&
1994 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1996 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2000 /* found the definition */
2001 /* replace the result with the result of */
2002 /* this assignment and remove this assignment */
2003 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2005 IC_RESULT (dic) = IC_RESULT (ic);
2007 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2009 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2011 /* delete from liverange table also
2012 delete from all the points inbetween and the new
2014 for (sic = dic; sic != ic; sic = sic->next)
2016 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2017 if (IS_ITEMP (IC_RESULT (dic)))
2018 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2021 remiCodeFromeBBlock (ebp, ic);
2022 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2023 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2024 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2029 /*-----------------------------------------------------------------*/
2030 /* findAssignToSym : scanning backwards looks for first assig found */
2031 /*-----------------------------------------------------------------*/
2033 findAssignToSym (operand * op, iCode * ic)
2037 for (dic = ic->prev; dic; dic = dic->prev)
2040 /* if definition by assignment */
2041 if (dic->op == '=' &&
2042 !POINTER_SET (dic) &&
2043 IC_RESULT (dic)->key == op->key
2044 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2048 /* we are interested only if defined in far space */
2049 /* or in stack space in case of + & - */
2051 /* if assigned to a non-symbol then return
2053 if (!IS_SYMOP (IC_RIGHT (dic)))
2056 /* if the symbol is in far space then
2058 if (isOperandInFarSpace (IC_RIGHT (dic)))
2061 /* for + & - operations make sure that
2062 if it is on the stack it is the same
2063 as one of the three operands */
2064 if ((ic->op == '+' || ic->op == '-') &&
2065 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2068 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2069 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2070 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2078 /* if we find an usage then we cannot delete it */
2079 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2082 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2085 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2089 /* now make sure that the right side of dic
2090 is not defined between ic & dic */
2093 iCode *sic = dic->next;
2095 for (; sic != ic; sic = sic->next)
2096 if (IC_RESULT (sic) &&
2097 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2106 /*-----------------------------------------------------------------*/
2107 /* packRegsForSupport :- reduce some registers for support calls */
2108 /*-----------------------------------------------------------------*/
2110 packRegsForSupport (iCode * ic, eBBlock * ebp)
2114 /* for the left & right operand :- look to see if the
2115 left was assigned a true symbol in far space in that
2116 case replace them */
2117 if (IS_ITEMP (IC_LEFT (ic)) &&
2118 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2120 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2126 /* found it we need to remove it from the
2128 for (sic = dic; sic != ic; sic = sic->next) {
2129 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2130 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2133 IC_LEFT (ic)->operand.symOperand =
2134 IC_RIGHT (dic)->operand.symOperand;
2135 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2136 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2137 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2138 remiCodeFromeBBlock (ebp, dic);
2139 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2143 /* do the same for the right operand */
2146 IS_ITEMP (IC_RIGHT (ic)) &&
2147 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2149 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2155 /* if this is a subtraction & the result
2156 is a true symbol in far space then don't pack */
2157 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2159 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2160 if (IN_FARSPACE (SPEC_OCLS (etype)))
2163 /* found it we need to remove it from the
2165 for (sic = dic; sic != ic; sic = sic->next) {
2166 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2167 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2170 IC_RIGHT (ic)->operand.symOperand =
2171 IC_RIGHT (dic)->operand.symOperand;
2172 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2173 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2174 remiCodeFromeBBlock (ebp, dic);
2175 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2176 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2183 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2186 /*-----------------------------------------------------------------*/
2187 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2188 /*-----------------------------------------------------------------*/
2189 static int packRegsDPTRnuse( operand *op , int dptr)
2194 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2195 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2198 /* first check if any overlapping liverange has already been
2199 assigned to this DPTR */
2200 if (OP_SYMBOL(op)->clashes) {
2201 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2203 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2204 sym = hTabItemWithKey(liveRanges,i);
2205 if (sym->dptr == dptr) return 0;
2210 /* future for more dptrs */
2212 OP_SYMBOL(op)->dptr = dptr;
2216 /* DPTR1 is special since it is also used as a scratch by the backend .
2217 so we walk thru the entire live range of this operand and make sure
2218 DPTR1 will not be used by the backed . The logic here is to find out if
2219 more than one operand in an icode is in far space then we give up : we
2220 don't keep it live across functions for now
2223 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2224 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2225 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2228 if (ic->op == CALL || ic->op == PCALL) return 0;
2230 /* single operand icode are ok */
2231 if (ic->op == IFX || ic->op == IPUSH)
2234 if (ic->op == SEND ) {
2235 if (ic->argreg != 1 ) return 0;
2238 /* two special cases first */
2239 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2240 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2241 (isOperandInFarSpace(IC_RESULT(ic)) &&
2242 !isOperandInReg(IC_RESULT(ic)))) {
2246 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2247 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2248 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2249 !isOperandInReg(IC_RIGHT(ic)))) {
2253 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2254 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2255 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2256 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2257 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2261 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2262 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2263 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2264 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2265 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2268 /* same for right */
2269 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2270 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2271 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2272 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2273 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2277 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2278 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2280 if (nfs > 1) return 0;
2282 OP_SYMBOL(op)->dptr = dptr;
2286 /*-----------------------------------------------------------------*/
2287 /* packRegsDPTRuse : - will reduce some registers for single Use */
2288 /*-----------------------------------------------------------------*/
2290 packRegsDPTRuse (operand * op)
2292 /* go thru entire liveRange of this variable & check for
2293 other possible usage of DPTR , if we don't find it the
2294 assign this to DPTR (ruonly)
2299 sym_link *type, *etype;
2301 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2302 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2304 /* first check if any overlapping liverange has already been
2306 if (OP_SYMBOL(op)->clashes) {
2307 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2308 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2309 sym = hTabItemWithKey(liveRanges,i);
2310 if (sym->ruonly) return NULL ;
2315 /* no then go thru this guys live range */
2316 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2317 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2318 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2320 if (SKIP_IC3(ic)) continue;
2322 /* if PCALL cannot be sure give up */
2323 if (ic->op == PCALL) return NULL;
2325 /* if SEND & not the first parameter then giveup */
2326 if (ic->op == SEND && ic->argreg != 1 &&
2327 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2328 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2330 /* if CALL then make sure it is VOID || return value not used
2331 or the return value is assigned to this one */
2332 if (ic->op == CALL) {
2333 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2334 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2335 etype = getSpec(type = operandType(IC_RESULT(ic)));
2336 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2341 /* special case of add with a [remat] */
2342 if (ic->op == '+' &&
2343 OP_SYMBOL(IC_LEFT(ic))->remat &&
2344 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2345 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2349 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2350 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2353 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2354 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2356 /* conditionals can destroy 'b' - make sure B wont
2357 be used in this one*/
2358 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2359 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2360 getSize(operandType(op)) > 3) return NULL;
2362 /* if this is a cast to a bigger type */
2364 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2365 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2366 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2372 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2373 !isOperandEqual(IC_RESULT(ic),op) &&
2374 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2375 !isOperandInReg(IC_RESULT(ic))) ||
2376 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2378 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2379 !isOperandEqual(IC_RIGHT(ic),op) &&
2380 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2381 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2382 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2383 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2384 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2386 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2387 !isOperandEqual(IC_LEFT(ic),op) &&
2388 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2389 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2390 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2391 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2392 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2394 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2395 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2396 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2397 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2400 OP_SYMBOL(op)->ruonly = 1;
2401 if (OP_SYMBOL(op)->usl.spillLoc) {
2402 if (OP_SYMBOL(op)->spillA)
2403 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2404 OP_SYMBOL(op)->usl.spillLoc = NULL;
2409 /*-----------------------------------------------------------------*/
2410 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2411 /*-----------------------------------------------------------------*/
2413 isBitwiseOptimizable (iCode * ic)
2415 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2416 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2418 /* bitwise operations are considered optimizable
2419 under the following conditions (Jean-Louis VERN)
2431 if ( IS_LITERAL (rtype) ||
2432 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2438 /*-----------------------------------------------------------------*/
2439 /* packRegsForAccUse - pack registers for acc use */
2440 /*-----------------------------------------------------------------*/
2442 packRegsForAccUse (iCode * ic)
2446 /* if this is an aggregate, e.g. a one byte char array */
2447 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2451 /* if we are calling a reentrant function that has stack parameters */
2452 if (ic->op == CALL &&
2453 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2454 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2457 if (ic->op == PCALL &&
2458 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2459 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2462 /* if + or - then it has to be one byte result */
2463 if ((ic->op == '+' || ic->op == '-')
2464 && getSize (operandType (IC_RESULT (ic))) > 1)
2467 /* if shift operation make sure right side is not a literal */
2468 if (ic->op == RIGHT_OP &&
2469 (isOperandLiteral (IC_RIGHT (ic)) ||
2470 getSize (operandType (IC_RESULT (ic))) > 1))
2473 if (ic->op == LEFT_OP &&
2474 (isOperandLiteral (IC_RIGHT (ic)) ||
2475 getSize (operandType (IC_RESULT (ic))) > 1))
2478 if (IS_BITWISE_OP (ic) &&
2479 getSize (operandType (IC_RESULT (ic))) > 1)
2483 /* has only one definition */
2484 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2487 /* has only one use */
2488 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2491 /* and the usage immediately follows this iCode */
2492 if (!(uic = hTabItemWithKey (iCodehTab,
2493 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2496 if (ic->next != uic)
2499 /* if it is a conditional branch then we definitely can */
2503 if (uic->op == JUMPTABLE)
2506 /* if the usage is not is an assignment
2507 or an arithmetic / bitwise / shift operation then not */
2508 if (POINTER_SET (uic) &&
2509 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2512 if (uic->op != '=' &&
2513 !IS_ARITHMETIC_OP (uic) &&
2514 !IS_BITWISE_OP (uic) &&
2515 uic->op != LEFT_OP &&
2516 uic->op != RIGHT_OP)
2519 /* if used in ^ operation then make sure right is not a
2521 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2524 /* if shift operation make sure right side is not a literal */
2525 if (uic->op == RIGHT_OP &&
2526 (isOperandLiteral (IC_RIGHT (uic)) ||
2527 getSize (operandType (IC_RESULT (uic))) > 1))
2530 if (uic->op == LEFT_OP &&
2531 (isOperandLiteral (IC_RIGHT (uic)) ||
2532 getSize (operandType (IC_RESULT (uic))) > 1))
2535 /* make sure that the result of this icode is not on the
2536 stack, since acc is used to compute stack offset */
2537 if (isOperandOnStack(IC_RESULT(uic)))
2540 /* if either one of them in far space then we cannot */
2541 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2542 isOperandInFarSpace (IC_LEFT (uic))) ||
2543 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2544 isOperandInFarSpace (IC_RIGHT (uic))))
2547 /* if the usage has only one operand then we can */
2548 if (IC_LEFT (uic) == NULL ||
2549 IC_RIGHT (uic) == NULL)
2552 /* make sure this is on the left side if not
2553 a '+' since '+' is commutative */
2554 if (ic->op != '+' &&
2555 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2558 /* if the other one is not on stack then we can */
2559 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2560 (IS_ITEMP (IC_RIGHT (uic)) ||
2561 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2562 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2565 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2566 (IS_ITEMP (IC_LEFT (uic)) ||
2567 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2568 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2574 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2579 /*-----------------------------------------------------------------*/
2580 /* packForPush - hueristics to reduce iCode for pushing */
2581 /*-----------------------------------------------------------------*/
2583 packForPush (iCode * ic, eBBlock * ebp)
2588 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2591 /* must have only definition & one usage */
2592 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2593 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2596 /* find the definition */
2597 if (!(dic = hTabItemWithKey (iCodehTab,
2598 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2601 if (dic->op != '=' || POINTER_SET (dic))
2604 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2606 /* make sure the right side does not have any definitions
2608 dbv = OP_DEFS(IC_RIGHT(dic));
2609 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2610 if (bitVectBitValue(dbv,lic->key)) return ;
2612 /* make sure they have the same type */
2614 sym_link *itype=operandType(IC_LEFT(ic));
2615 sym_link *ditype=operandType(IC_RIGHT(dic));
2617 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2618 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2621 /* extend the live range of replaced operand if needed */
2622 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2623 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2624 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2625 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2626 OP_SYMBOL(IC_LEFT(ic))->clashes);
2628 for (lic = ic; lic && lic != dic; lic = lic->prev)
2630 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2631 if (IS_ITEMP (IC_RIGHT (dic)))
2632 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2634 /* we now we know that it has one & only one def & use
2635 and the that the definition is an assignment */
2636 IC_LEFT (ic) = IC_RIGHT (dic);
2638 remiCodeFromeBBlock (ebp, dic);
2639 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2640 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2643 /*-----------------------------------------------------------------*/
2644 /* packRegisters - does some transformations to reduce register */
2646 /*-----------------------------------------------------------------*/
2648 packRegisters (eBBlock * ebp)
2658 /* look for assignments of the form */
2659 /* iTempNN = TRueSym (someoperation) SomeOperand */
2661 /* TrueSym := iTempNN:1 */
2662 for (ic = ebp->sch; ic; ic = ic->next)
2666 /* find assignment of the form TrueSym := iTempNN:1 */
2667 if (ic->op == '=' && !POINTER_SET (ic))
2668 change += packRegsForAssign (ic, ebp);
2675 for (ic = ebp->sch; ic; ic = ic->next)
2678 /* if this is an itemp & result of a address of a true sym
2679 then mark this as rematerialisable */
2680 if (ic->op == ADDRESS_OF &&
2681 IS_ITEMP (IC_RESULT (ic)) &&
2682 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2683 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2684 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2687 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2688 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2689 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2693 /* if this is an itemp & used as a pointer
2694 & assigned to a literal then remat */
2695 if (IS_ASSIGN_ICODE(ic) &&
2696 IS_ITEMP(IC_RESULT(ic)) &&
2697 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2698 isOperandLiteral(IC_RIGHT(ic)))
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;
2705 /* if straight assignment then carry remat flag if
2706 this is the only definition */
2707 if (ic->op == '=' &&
2708 !POINTER_SET (ic) &&
2709 IS_SYMOP (IC_RIGHT (ic)) &&
2710 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2711 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2712 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2715 OP_SYMBOL (IC_RESULT (ic))->remat =
2716 OP_SYMBOL (IC_RIGHT (ic))->remat;
2717 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2718 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2721 /* if cast to a generic pointer & the pointer being
2722 cast is remat, then we can remat this cast as well */
2723 if (ic->op == CAST &&
2724 IS_SYMOP(IC_RIGHT(ic)) &&
2725 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2726 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2727 sym_link *to_type = operandType(IC_LEFT(ic));
2728 sym_link *from_type = operandType(IC_RIGHT(ic));
2729 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2730 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2731 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2732 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2736 /* if this is a +/- operation with a rematerizable
2737 then mark this as rematerializable as well */
2738 if ((ic->op == '+' || ic->op == '-') &&
2739 (IS_SYMOP (IC_LEFT (ic)) &&
2740 IS_ITEMP (IC_RESULT (ic)) &&
2741 OP_SYMBOL (IC_LEFT (ic))->remat &&
2742 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2743 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2744 IS_OP_LITERAL (IC_RIGHT (ic))))
2747 //int i = operandLitValue(IC_RIGHT(ic));
2748 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2749 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2750 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2753 /* mark the pointer usages */
2754 if (POINTER_SET (ic))
2755 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2757 if (POINTER_GET (ic))
2758 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2760 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2761 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2763 if (ic->op == RECEIVE && ic->argreg == 1 &&
2764 getSize (operandType(IC_RESULT(ic))) <= 3)
2765 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2767 if (ic->op == SEND && ic->argreg == 1 &&
2768 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2769 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2773 /* if we are using a symbol on the stack
2774 then we should say ds390_ptrRegReq */
2775 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2776 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2777 OP_SYMBOL (IC_COND (ic))->iaccess);
2778 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2779 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2780 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2783 if (IS_SYMOP (IC_LEFT (ic)))
2784 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2785 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2786 if (IS_SYMOP (IC_RIGHT (ic)))
2787 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2788 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2789 if (IS_SYMOP (IC_RESULT (ic)))
2790 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2791 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2795 /* if the condition of an if instruction
2796 is defined in the previous instruction and
2797 this is the only usage then
2798 mark the itemp as a conditional */
2799 if ((IS_CONDITIONAL (ic) ||
2800 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2801 ic->next && ic->next->op == IFX &&
2802 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2803 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2804 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2806 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2810 /* reduce for support function calls */
2811 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2812 packRegsForSupport (ic, ebp);
2814 /* some cases the redundant moves can
2815 can be eliminated for return statements . Can be elminated for the first SEND */
2816 if ((ic->op == RETURN ||
2817 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
2818 !isOperandInFarSpace (IC_LEFT (ic)) &&
2821 packRegsDPTRuse (IC_LEFT (ic));
2824 if (ic->op == CALL) {
2825 sym_link *ftype = operandType(IC_LEFT(ic));
2826 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2827 !IFFUNC_ISBUILTIN(ftype)) {
2828 packRegsDPTRuse (IC_RESULT (ic));
2832 /* if pointer set & left has a size more than
2833 one and right is not in far space */
2834 if (POINTER_SET (ic) &&
2835 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2836 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2837 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2838 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2840 packRegsDPTRuse (IC_RESULT (ic));
2843 /* if pointer get */
2844 if (POINTER_GET (ic) &&
2845 !isOperandInFarSpace (IC_RESULT (ic)) &&
2846 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2847 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2848 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2850 packRegsDPTRuse (IC_LEFT (ic));
2853 /* if this is cast for intergral promotion then
2854 check if only use of the definition of the
2855 operand being casted/ if yes then replace
2856 the result of that arithmetic operation with
2857 this result and get rid of the cast */
2860 sym_link *fromType = operandType (IC_RIGHT (ic));
2861 sym_link *toType = operandType (IC_LEFT (ic));
2863 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2864 getSize (fromType) != getSize (toType) &&
2865 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2868 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2871 if (IS_ARITHMETIC_OP (dic))
2873 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2874 IC_RESULT (dic) = IC_RESULT (ic);
2875 remiCodeFromeBBlock (ebp, ic);
2876 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2877 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2878 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2882 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2888 /* if the type from and type to are the same
2889 then if this is the only use then packit */
2890 if (compareType (operandType (IC_RIGHT (ic)),
2891 operandType (IC_LEFT (ic))) == 1)
2893 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2896 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2897 IC_RESULT (dic) = IC_RESULT (ic);
2898 remiCodeFromeBBlock (ebp, ic);
2899 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2900 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2901 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2909 iTempNN := (some variable in farspace) V1
2914 if (ic->op == IPUSH || ic->op == SEND)
2916 packForPush (ic, ebp);
2920 /* pack registers for accumulator use, when the
2921 result of an arithmetic or bit wise operation
2922 has only one use, that use is immediately following
2923 the defintion and the using iCode has only one
2924 operand or has two operands but one is literal &
2925 the result of that operation is not on stack then
2926 we can leave the result of this operation in acc:b
2928 if ((IS_ARITHMETIC_OP (ic)
2929 || IS_CONDITIONAL(ic)
2930 || IS_BITWISE_OP (ic)
2931 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2932 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2934 IS_ITEMP (IC_RESULT (ic)) &&
2935 getSize (operandType (IC_RESULT (ic))) <= 2)
2937 packRegsForAccUse (ic);
2942 /*-----------------------------------------------------------------*/
2943 /* assignRegisters - assigns registers to each live range as need */
2944 /*-----------------------------------------------------------------*/
2946 ds390_assignRegisters (eBBlock ** ebbs, int count)
2951 setToNull ((void *) &_G.funcrUsed);
2952 setToNull ((void *) &_G.regAssigned);
2953 setToNull ((void *) &_G.totRegAssigned);
2954 setToNull ((void *) &_G.funcrUsed);
2955 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2957 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2958 /* change assignments this will remove some
2959 live ranges reducing some register pressure */
2960 for (i = 0; i < count; i++)
2961 packRegisters (ebbs[i]);
2963 if (options.dump_pack)
2964 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2966 /* first determine for each live range the number of
2967 registers & the type of registers required for each */
2970 /* and serially allocate registers */
2971 serialRegAssign (ebbs, count);
2978 /* if stack was extended then tell the user */
2981 /* werror(W_TOOMANY_SPILS,"stack", */
2982 /* _G.stackExtend,currFunc->name,""); */
2988 /* werror(W_TOOMANY_SPILS,"data space", */
2989 /* _G.dataExtend,currFunc->name,""); */
2993 /* after that create the register mask
2994 for each of the instruction */
2995 createRegMask (ebbs, count);
2997 /* redo that offsets for stacked automatic variables */
2998 redoStackOffsets ();
3000 if (options.dump_rassgn) {
3001 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3002 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3005 /* do the overlaysegment stuff SDCCmem.c */
3006 doOverlays (ebbs, count);
3008 /* now get back the chain */
3009 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3014 /* free up any _G.stackSpil locations allocated */
3015 applyToSet (_G.stackSpil, deallocStackSpil);
3017 setToNull ((void **) &_G.stackSpil);
3018 setToNull ((void **) &_G.spiltSet);
3019 /* mark all registers as free */