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 *funcrUsed; /* registers used in a function */
53 /* Shared with gen.c */
54 int ds390_ptrRegReq; /* one byte pointer register required */
60 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
61 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
62 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
63 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
64 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
65 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
66 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
67 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
68 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
69 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
70 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
71 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
72 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
73 {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, 1},
76 static void spillThis (symbol *);
78 /*-----------------------------------------------------------------*/
79 /* allocReg - allocates register of given type */
80 /*-----------------------------------------------------------------*/
86 for (i = 0; i < ds390_nRegs; i++)
89 /* if type is given as 0 then any
90 free register will do */
94 regs390[i].isFree = 0;
97 bitVectSetBit (currFunc->regsUsed, i);
100 /* other wise look for specific type
102 if (regs390[i].isFree &&
103 regs390[i].type == type)
105 regs390[i].isFree = 0;
108 bitVectSetBit (currFunc->regsUsed, i);
115 /*-----------------------------------------------------------------*/
116 /* ds390_regWithIdx - returns pointer to register wit index number */
117 /*-----------------------------------------------------------------*/
119 ds390_regWithIdx (int idx)
123 for (i = 0; i < ds390_nRegs; i++)
124 if (regs390[i].rIdx == idx)
127 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
128 "regWithIdx not found");
132 /*-----------------------------------------------------------------*/
133 /* freeReg - frees a register */
134 /*-----------------------------------------------------------------*/
142 /*-----------------------------------------------------------------*/
143 /* nFreeRegs - returns number of free registers */
144 /*-----------------------------------------------------------------*/
151 for (i = 0; i < ds390_nRegs; i++)
152 if (regs390[i].isFree && regs390[i].type == type)
157 /*-----------------------------------------------------------------*/
158 /* nfreeRegsType - free registers with type */
159 /*-----------------------------------------------------------------*/
161 nfreeRegsType (int type)
166 if ((nfr = nFreeRegs (type)) == 0)
167 return nFreeRegs (REG_GPR);
170 return nFreeRegs (type);
174 /*-----------------------------------------------------------------*/
175 /* allDefsOutOfRange - all definitions are out of a range */
176 /*-----------------------------------------------------------------*/
178 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
185 for (i = 0; i < defs->size; i++)
189 if (bitVectBitValue (defs, i) &&
190 (ic = hTabItemWithKey (iCodehTab, i)) &&
191 (ic->seq >= fseq && ic->seq <= toseq))
200 /*-----------------------------------------------------------------*/
201 /* computeSpillable - given a point find the spillable live ranges */
202 /*-----------------------------------------------------------------*/
204 computeSpillable (iCode * ic)
208 /* spillable live ranges are those that are live at this
209 point . the following categories need to be subtracted
211 a) - those that are already spilt
212 b) - if being used by this one
213 c) - defined by this one */
215 spillable = bitVectCopy (ic->rlive);
217 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
219 bitVectCplAnd (spillable, ic->uses); /* used in this one */
220 bitVectUnSetBit (spillable, ic->defKey);
221 spillable = bitVectIntersect (spillable, _G.regAssigned);
226 /*-----------------------------------------------------------------*/
227 /* noSpilLoc - return true if a variable has no spil location */
228 /*-----------------------------------------------------------------*/
230 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
232 return (sym->usl.spillLoc ? 0 : 1);
235 /*-----------------------------------------------------------------*/
236 /* hasSpilLoc - will return 1 if the symbol has spil location */
237 /*-----------------------------------------------------------------*/
239 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
241 return (sym->usl.spillLoc ? 1 : 0);
244 /*-----------------------------------------------------------------*/
245 /* directSpilLoc - will return 1 if the splilocation is in direct */
246 /*-----------------------------------------------------------------*/
248 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
250 if (sym->usl.spillLoc &&
251 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
257 /*-----------------------------------------------------------------*/
258 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
259 /* but is not used as a pointer */
260 /*-----------------------------------------------------------------*/
262 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
264 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
267 /*-----------------------------------------------------------------*/
268 /* rematable - will return 1 if the remat flag is set */
269 /*-----------------------------------------------------------------*/
271 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
276 /*-----------------------------------------------------------------*/
277 /* notUsedInBlock - not used in this block */
278 /*-----------------------------------------------------------------*/
280 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
282 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
283 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
284 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
287 /*-----------------------------------------------------------------*/
288 /* notUsedInRemaining - not used or defined in remain of the block */
289 /*-----------------------------------------------------------------*/
291 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
293 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
294 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
297 /*-----------------------------------------------------------------*/
298 /* allLRs - return true for all */
299 /*-----------------------------------------------------------------*/
301 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
306 /*-----------------------------------------------------------------*/
307 /* liveRangesWith - applies function to a given set of live range */
308 /*-----------------------------------------------------------------*/
310 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
311 eBBlock * ebp, iCode * ic)
316 if (!lrs || !lrs->size)
319 for (i = 1; i < lrs->size; i++)
322 if (!bitVectBitValue (lrs, i))
325 /* if we don't find it in the live range
326 hash table we are in serious trouble */
327 if (!(sym = hTabItemWithKey (liveRanges, i)))
329 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
330 "liveRangesWith could not find liveRange");
334 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
335 addSetHead (&rset, sym);
342 /*-----------------------------------------------------------------*/
343 /* leastUsedLR - given a set determines which is the least used */
344 /*-----------------------------------------------------------------*/
346 leastUsedLR (set * sset)
348 symbol *sym = NULL, *lsym = NULL;
350 sym = lsym = setFirstItem (sset);
355 for (; lsym; lsym = setNextItem (sset))
358 /* if usage is the same then prefer
359 the spill the smaller of the two */
360 if (lsym->used == sym->used)
361 if (getSize (lsym->type) < getSize (sym->type))
365 if (lsym->used < sym->used)
370 setToNull ((void **) &sset);
375 /*-----------------------------------------------------------------*/
376 /* noOverLap - will iterate through the list looking for over lap */
377 /*-----------------------------------------------------------------*/
379 noOverLap (set * itmpStack, symbol * fsym)
383 for (sym = setFirstItem (itmpStack); sym;
384 sym = setNextItem (itmpStack))
386 // if sym starts before (or on) our end point
387 // and ends after (or on) our start point,
389 if (sym->liveFrom <= fsym->liveTo &&
390 sym->liveTo >= fsym->liveFrom)
398 /*-----------------------------------------------------------------*/
399 /* isFree - will return 1 if the a free spil location is found */
400 /*-----------------------------------------------------------------*/
405 V_ARG (symbol **, sloc);
406 V_ARG (symbol *, fsym);
408 /* if already found */
412 /* if it is free && and the itmp assigned to
413 this does not have any overlapping live ranges
414 with the one currently being assigned and
415 the size can be accomodated */
417 noOverLap (sym->usl.itmpStack, fsym) &&
418 getSize (sym->type) >= getSize (fsym->type))
427 /*-----------------------------------------------------------------*/
428 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
429 /*-----------------------------------------------------------------*/
431 spillLRWithPtrReg (symbol * forSym)
437 if (!_G.regAssigned ||
438 bitVectIsZero (_G.regAssigned))
441 r0 = ds390_regWithIdx (R0_IDX);
442 r1 = ds390_regWithIdx (R1_IDX);
444 /* for all live ranges */
445 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
446 lrsym = hTabNextItem (liveRanges, &k))
450 /* if no registers assigned to it or
452 /* if it does not overlap with this then
453 not need to spill it */
455 if (lrsym->isspilt || !lrsym->nRegs ||
456 (lrsym->liveTo < forSym->liveFrom))
459 /* go thru the registers : if it is either
460 r0 or r1 then spil it */
461 for (j = 0; j < lrsym->nRegs; j++)
462 if (lrsym->regs[j] == r0 ||
463 lrsym->regs[j] == r1)
472 /*-----------------------------------------------------------------*/
473 /* createStackSpil - create a location on the stack to spil */
474 /*-----------------------------------------------------------------*/
476 createStackSpil (symbol * sym)
479 int useXstack, model, noOverlay;
483 /* first go try and find a free one that is already
484 existing on the stack */
485 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
487 /* found a free one : just update & return */
488 sym->usl.spillLoc = sloc;
491 addSetHead (&sloc->usl.itmpStack, sym);
495 /* could not then have to create one , this is the hard part
496 we need to allocate this on the stack : this is really a
497 hack!! but cannot think of anything better at this time */
499 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
501 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
506 sloc = newiTemp (slocBuffer);
508 /* set the type to the spilling symbol */
509 sloc->type = copyLinkChain (sym->type);
510 sloc->etype = getSpec (sloc->type);
511 SPEC_SCLS (sloc->etype) = options.model ? S_XDATA : S_DATA;
512 SPEC_EXTR (sloc->etype) = 0;
514 /* we don't allow it to be allocated`
515 onto the external stack since : so we
516 temporarily turn it off ; we also
517 turn off memory model to prevent
518 the spil from going to the external storage
519 and turn off overlaying
522 useXstack = options.useXstack;
523 model = options.model;
524 noOverlay = options.noOverlay;
525 options.noOverlay = 1;
527 /* options.model = options.useXstack = 0; */
531 options.useXstack = useXstack;
532 options.model = model;
533 options.noOverlay = noOverlay;
534 sloc->isref = 1; /* to prevent compiler warning */
536 /* if it is on the stack then update the stack */
537 if (IN_STACK (sloc->etype))
539 currFunc->stack += getSize (sloc->type);
540 _G.stackExtend += getSize (sloc->type);
543 _G.dataExtend += getSize (sloc->type);
545 /* add it to the _G.stackSpil set */
546 addSetHead (&_G.stackSpil, sloc);
547 sym->usl.spillLoc = sloc;
550 /* add it to the set of itempStack set
551 of the spill location */
552 addSetHead (&sloc->usl.itmpStack, sym);
556 /*-----------------------------------------------------------------*/
557 /* isSpiltOnStack - returns true if the spil location is on stack */
558 /*-----------------------------------------------------------------*/
560 isSpiltOnStack (symbol * sym)
570 /* if (sym->_G.stackSpil) */
573 if (!sym->usl.spillLoc)
576 etype = getSpec (sym->usl.spillLoc->type);
577 if (IN_STACK (etype))
583 /*-----------------------------------------------------------------*/
584 /* spillThis - spils a specific operand */
585 /*-----------------------------------------------------------------*/
587 spillThis (symbol * sym)
590 /* if this is rematerializable or has a spillLocation
591 we are okay, else we need to create a spillLocation
593 if (!(sym->remat || sym->usl.spillLoc))
594 createStackSpil (sym);
597 /* mark it has spilt & put it in the spilt set */
599 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
601 bitVectUnSetBit (_G.regAssigned, sym->key);
603 for (i = 0; i < sym->nRegs; i++)
607 freeReg (sym->regs[i]);
611 /* if spilt on stack then free up r0 & r1
612 if they could have been assigned to some
614 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
617 spillLRWithPtrReg (sym);
620 if (sym->usl.spillLoc && !sym->remat)
621 sym->usl.spillLoc->allocreq = 1;
625 /*-----------------------------------------------------------------*/
626 /* selectSpil - select a iTemp to spil : rather a simple procedure */
627 /*-----------------------------------------------------------------*/
629 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
631 bitVect *lrcs = NULL;
635 /* get the spillable live ranges */
636 lrcs = computeSpillable (ic);
638 /* get all live ranges that are rematerizable */
639 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
642 /* return the least used of these */
643 return leastUsedLR (selectS);
646 /* get live ranges with spillLocations in direct space */
647 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
649 sym = leastUsedLR (selectS);
650 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
651 sym->usl.spillLoc->rname :
652 sym->usl.spillLoc->name));
654 /* mark it as allocation required */
655 sym->usl.spillLoc->allocreq = 1;
659 /* if the symbol is local to the block then */
660 if (forSym->liveTo < ebp->lSeq)
663 /* check if there are any live ranges allocated
664 to registers that are not used in this block */
665 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
667 sym = leastUsedLR (selectS);
668 /* if this is not rematerializable */
677 /* check if there are any live ranges that not
678 used in the remainder of the block */
679 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
681 sym = leastUsedLR (selectS);
694 /* find live ranges with spillocation && not used as pointers */
695 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
698 sym = leastUsedLR (selectS);
699 /* mark this as allocation required */
700 sym->usl.spillLoc->allocreq = 1;
704 /* find live ranges with spillocation */
705 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
708 sym = leastUsedLR (selectS);
709 sym->usl.spillLoc->allocreq = 1;
713 /* couldn't find then we need to create a spil
714 location on the stack , for which one? the least
716 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
719 /* return a created spil location */
720 sym = createStackSpil (leastUsedLR (selectS));
721 sym->usl.spillLoc->allocreq = 1;
725 /* this is an extreme situation we will spill
726 this one : happens very rarely but it does happen */
732 /*-----------------------------------------------------------------*/
733 /* spilSomething - spil some variable & mark registers as free */
734 /*-----------------------------------------------------------------*/
736 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
741 /* get something we can spil */
742 ssym = selectSpil (ic, ebp, forSym);
744 /* mark it as spilt */
746 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
748 /* mark it as not register assigned &
749 take it away from the set */
750 bitVectUnSetBit (_G.regAssigned, ssym->key);
752 /* mark the registers as free */
753 for (i = 0; i < ssym->nRegs; i++)
755 freeReg (ssym->regs[i]);
757 /* if spilt on stack then free up r0 & r1
758 if they could have been assigned to as gprs */
759 if (!ds390_ptrRegReq && isSpiltOnStack (ssym))
762 spillLRWithPtrReg (ssym);
765 /* if this was a block level spil then insert push & pop
766 at the start & end of block respectively */
769 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
770 /* add push to the start of the block */
771 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
772 ebp->sch->next : ebp->sch));
773 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
774 /* add pop to the end of the block */
775 addiCodeToeBBlock (ebp, nic, NULL);
778 /* if spilt because not used in the remainder of the
779 block then add a push before this instruction and
780 a pop at the end of the block */
781 if (ssym->remainSpil)
784 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
785 /* add push just before this instruction */
786 addiCodeToeBBlock (ebp, nic, ic);
788 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
789 /* add pop to the end of the block */
790 addiCodeToeBBlock (ebp, nic, NULL);
799 /*-----------------------------------------------------------------*/
800 /* getRegPtr - will try for PTR if not a GPR type if not spil */
801 /*-----------------------------------------------------------------*/
803 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
808 /* try for a ptr type */
809 if ((reg = allocReg (REG_PTR)))
812 /* try for gpr type */
813 if ((reg = allocReg (REG_GPR)))
816 /* we have to spil */
817 if (!spilSomething (ic, ebp, sym))
820 /* this looks like an infinite loop but
821 in really selectSpil will abort */
825 /*-----------------------------------------------------------------*/
826 /* getRegGpr - will try for GPR if not spil */
827 /*-----------------------------------------------------------------*/
829 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
834 /* try for gpr type */
835 if ((reg = allocReg (REG_GPR)))
838 if (!ds390_ptrRegReq)
839 if ((reg = allocReg (REG_PTR)))
842 /* we have to spil */
843 if (!spilSomething (ic, ebp, sym))
846 /* this looks like an infinite loop but
847 in really selectSpil will abort */
851 /*-----------------------------------------------------------------*/
852 /* symHasReg - symbol has a given register */
853 /*-----------------------------------------------------------------*/
855 symHasReg (symbol * sym, regs * reg)
859 for (i = 0; i < sym->nRegs; i++)
860 if (sym->regs[i] == reg)
866 /*-----------------------------------------------------------------*/
867 /* deassignLRs - check the live to and if they have registers & are */
868 /* not spilt then free up the registers */
869 /*-----------------------------------------------------------------*/
871 deassignLRs (iCode * ic, eBBlock * ebp)
877 for (sym = hTabFirstItem (liveRanges, &k); sym;
878 sym = hTabNextItem (liveRanges, &k))
882 /* if it does not end here */
883 if (sym->liveTo > ic->seq)
886 /* if it was spilt on stack then we can
887 mark the stack spil location as free */
892 sym->usl.spillLoc->isFree = 1;
898 if (!bitVectBitValue (_G.regAssigned, sym->key))
901 /* special case check if this is an IFX &
902 the privious one was a pop and the
903 previous one was not spilt then keep track
905 if (ic->op == IFX && ic->prev &&
906 ic->prev->op == IPOP &&
907 !ic->prev->parmPush &&
908 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
909 psym = OP_SYMBOL (IC_LEFT (ic->prev));
915 bitVectUnSetBit (_G.regAssigned, sym->key);
917 /* if the result of this one needs registers
918 and does not have it then assign it right
920 if (IC_RESULT (ic) &&
921 !(SKIP_IC2 (ic) || /* not a special icode */
922 ic->op == JUMPTABLE ||
928 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
929 result->liveTo > ic->seq && /* and will live beyond this */
930 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
931 result->regType == sym->regType && /* same register types */
932 result->nRegs && /* which needs registers */
933 !result->isspilt && /* and does not already have them */
935 !bitVectBitValue (_G.regAssigned, result->key) &&
936 /* the number of free regs + number of regs in this LR
937 can accomodate the what result Needs */
938 ((nfreeRegsType (result->regType) +
939 sym->nRegs) >= result->nRegs)
943 for (i = 0; i < result->nRegs; i++)
945 result->regs[i] = sym->regs[i];
947 result->regs[i] = getRegGpr (ic, ebp, result);
949 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
953 /* free the remaining */
954 for (; i < sym->nRegs; i++)
958 if (!symHasReg (psym, sym->regs[i]))
959 freeReg (sym->regs[i]);
962 freeReg (sym->regs[i]);
969 /*-----------------------------------------------------------------*/
970 /* reassignLR - reassign this to registers */
971 /*-----------------------------------------------------------------*/
973 reassignLR (operand * op)
975 symbol *sym = OP_SYMBOL (op);
978 /* not spilt any more */
979 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
980 bitVectUnSetBit (_G.spiltSet, sym->key);
982 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
986 for (i = 0; i < sym->nRegs; i++)
987 sym->regs[i]->isFree = 0;
990 /*-----------------------------------------------------------------*/
991 /* willCauseSpill - determines if allocating will cause a spill */
992 /*-----------------------------------------------------------------*/
994 willCauseSpill (int nr, int rt)
996 /* first check if there are any avlb registers
997 of te type required */
1000 /* special case for pointer type
1001 if pointer type not avlb then
1002 check for type gpr */
1003 if (nFreeRegs (rt) >= nr)
1005 if (nFreeRegs (REG_GPR) >= nr)
1010 if (ds390_ptrRegReq)
1012 if (nFreeRegs (rt) >= nr)
1017 if (nFreeRegs (REG_PTR) +
1018 nFreeRegs (REG_GPR) >= nr)
1023 /* it will cause a spil */
1027 /*-----------------------------------------------------------------*/
1028 /* positionRegs - the allocator can allocate same registers to res- */
1029 /* ult and operand, if this happens make sure they are in the same */
1030 /* position as the operand otherwise chaos results */
1031 /*-----------------------------------------------------------------*/
1033 positionRegs (symbol * result, symbol * opsym, int lineno)
1035 int count = min (result->nRegs, opsym->nRegs);
1036 int i, j = 0, shared = 0;
1038 /* if the result has been spilt then cannot share */
1043 /* first make sure that they actually share */
1044 for (i = 0; i < count; i++)
1046 for (j = 0; j < count; j++)
1048 if (result->regs[i] == opsym->regs[j] && i != j)
1058 regs *tmp = result->regs[i];
1059 result->regs[i] = result->regs[j];
1060 result->regs[j] = tmp;
1065 /*-----------------------------------------------------------------*/
1066 /* serialRegAssign - serially allocate registers to the variables */
1067 /*-----------------------------------------------------------------*/
1069 serialRegAssign (eBBlock ** ebbs, int count)
1073 /* for all blocks */
1074 for (i = 0; i < count; i++)
1079 if (ebbs[i]->noPath &&
1080 (ebbs[i]->entryLabel != entryLabel &&
1081 ebbs[i]->entryLabel != returnLabel))
1084 /* of all instructions do */
1085 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1088 /* if this is an ipop that means some live
1089 range will have to be assigned again */
1091 reassignLR (IC_LEFT (ic));
1093 /* if result is present && is a true symbol */
1094 if (IC_RESULT (ic) && ic->op != IFX &&
1095 IS_TRUE_SYMOP (IC_RESULT (ic)))
1096 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1098 /* take away registers from live
1099 ranges that end at this instruction */
1100 deassignLRs (ic, ebbs[i]);
1102 /* some don't need registers */
1103 if (SKIP_IC2 (ic) ||
1104 ic->op == JUMPTABLE ||
1108 (IC_RESULT (ic) && POINTER_SET (ic)))
1111 /* now we need to allocate registers
1112 only for the result */
1115 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1121 /* if it does not need or is spilt
1122 or is already assigned to registers
1123 or will not live beyond this instructions */
1126 bitVectBitValue (_G.regAssigned, sym->key) ||
1127 sym->liveTo <= ic->seq)
1130 /* if some liverange has been spilt at the block level
1131 and this one live beyond this block then spil this
1133 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1138 /* if trying to allocate this will cause
1139 a spill and there is nothing to spill
1140 or this one is rematerializable then
1142 willCS = willCauseSpill (sym->nRegs, sym->regType);
1143 spillable = computeSpillable (ic);
1145 (willCS && bitVectIsZero (spillable)))
1153 /* if it has a spillocation & is used less than
1154 all other live ranges then spill this */
1156 if (sym->usl.spillLoc) {
1157 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1158 allLRs, ebbs[i], ic));
1159 if (leastUsed && leastUsed->used > sym->used) {
1164 /* if none of the liveRanges have a spillLocation then better
1165 to spill this one than anything else already assigned to registers */
1166 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1173 /* if we need ptr regs for the right side
1175 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1176 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1177 <= (unsigned) PTRSIZE)
1182 /* else we assign registers to it */
1183 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1185 for (j = 0; j < sym->nRegs; j++)
1187 if (sym->regType == REG_PTR)
1188 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1190 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1192 /* if the allocation falied which means
1193 this was spilt then break */
1197 /* if it shares registers with operands make sure
1198 that they are in the same position */
1199 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1200 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1201 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1202 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1203 /* do the same for the right operand */
1204 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1205 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1206 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1207 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1220 /*-----------------------------------------------------------------*/
1221 /* rUmaskForOp :- returns register mask for an operand */
1222 /*-----------------------------------------------------------------*/
1224 rUmaskForOp (operand * op)
1230 /* only temporaries are assigned registers */
1234 sym = OP_SYMBOL (op);
1236 /* if spilt or no registers assigned to it
1238 if (sym->isspilt || !sym->nRegs)
1241 rumask = newBitVect (ds390_nRegs);
1243 for (j = 0; j < sym->nRegs; j++)
1245 rumask = bitVectSetBit (rumask,
1246 sym->regs[j]->rIdx);
1252 /*-----------------------------------------------------------------*/
1253 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1254 /*-----------------------------------------------------------------*/
1256 regsUsedIniCode (iCode * ic)
1258 bitVect *rmask = newBitVect (ds390_nRegs);
1260 /* do the special cases first */
1263 rmask = bitVectUnion (rmask,
1264 rUmaskForOp (IC_COND (ic)));
1268 /* for the jumptable */
1269 if (ic->op == JUMPTABLE)
1271 rmask = bitVectUnion (rmask,
1272 rUmaskForOp (IC_JTCOND (ic)));
1277 /* of all other cases */
1279 rmask = bitVectUnion (rmask,
1280 rUmaskForOp (IC_LEFT (ic)));
1284 rmask = bitVectUnion (rmask,
1285 rUmaskForOp (IC_RIGHT (ic)));
1288 rmask = bitVectUnion (rmask,
1289 rUmaskForOp (IC_RESULT (ic)));
1295 /*-----------------------------------------------------------------*/
1296 /* createRegMask - for each instruction will determine the regsUsed */
1297 /*-----------------------------------------------------------------*/
1299 createRegMask (eBBlock ** ebbs, int count)
1303 /* for all blocks */
1304 for (i = 0; i < count; i++)
1308 if (ebbs[i]->noPath &&
1309 (ebbs[i]->entryLabel != entryLabel &&
1310 ebbs[i]->entryLabel != returnLabel))
1313 /* for all instructions */
1314 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1319 if (SKIP_IC2 (ic) || !ic->rlive)
1322 /* first mark the registers used in this
1324 ic->rUsed = regsUsedIniCode (ic);
1325 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1327 /* now create the register mask for those
1328 registers that are in use : this is a
1329 super set of ic->rUsed */
1330 ic->rMask = newBitVect (ds390_nRegs + 1);
1332 /* for all live Ranges alive at this point */
1333 for (j = 1; j < ic->rlive->size; j++)
1338 /* if not alive then continue */
1339 if (!bitVectBitValue (ic->rlive, j))
1342 /* find the live range we are interested in */
1343 if (!(sym = hTabItemWithKey (liveRanges, j)))
1345 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1346 "createRegMask cannot find live range");
1350 /* if no register assigned to it */
1351 if (!sym->nRegs || sym->isspilt)
1354 /* for all the registers allocated to it */
1355 for (k = 0; k < sym->nRegs; k++)
1358 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1364 /*-----------------------------------------------------------------*/
1365 /* rematStr - returns the rematerialized string for a remat var */
1366 /*-----------------------------------------------------------------*/
1368 rematStr (symbol * sym)
1371 iCode *ic = sym->rematiCode;
1376 /* if plus or minus print the right hand side */
1377 if (ic->op == '+' || ic->op == '-')
1379 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1382 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1386 /* we reached the end */
1387 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1394 /*-----------------------------------------------------------------*/
1395 /* regTypeNum - computes the type & number of registers required */
1396 /*-----------------------------------------------------------------*/
1404 /* for each live range do */
1405 for (sym = hTabFirstItem (liveRanges, &k); sym;
1406 sym = hTabNextItem (liveRanges, &k))
1409 /* if used zero times then no registers needed */
1410 if ((sym->liveTo - sym->liveFrom) == 0)
1414 /* if the live range is a temporary */
1418 /* if the type is marked as a conditional */
1419 if (sym->regType == REG_CND)
1422 /* if used in return only then we don't
1424 if (sym->ruonly || sym->accuse)
1426 if (IS_AGGREGATE (sym->type) || sym->isptr)
1427 sym->type = aggrToPtr (sym->type, FALSE);
1431 /* if the symbol has only one definition &
1432 that definition is a get_pointer and the
1433 pointer we are getting is rematerializable and
1436 if (bitVectnBitsOn (sym->defs) == 1 &&
1437 (ic = hTabItemWithKey (iCodehTab,
1438 bitVectFirstBit (sym->defs))) &&
1440 !IS_BITVAR (sym->etype))
1444 /* if remat in data space */
1445 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1447 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1450 /* create a psuedo symbol & force a spil */
1451 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1452 psym->type = sym->type;
1453 psym->etype = sym->etype;
1454 strcpy (psym->rname, psym->name);
1456 sym->usl.spillLoc = psym;
1460 /* if in data space or idata space then try to
1461 allocate pointer register */
1465 /* if not then we require registers */
1466 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1467 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1468 getSize (sym->type));
1472 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1473 printTypeChain (sym->type, stderr);
1474 fprintf (stderr, "\n");
1477 /* determine the type of register required */
1478 if (sym->nRegs == 1 &&
1479 IS_PTR (sym->type) &&
1481 sym->regType = REG_PTR;
1483 sym->regType = REG_GPR;
1487 /* for the first run we don't provide */
1488 /* registers for true symbols we will */
1489 /* see how things go */
1495 /*-----------------------------------------------------------------*/
1496 /* freeAllRegs - mark all registers as free */
1497 /*-----------------------------------------------------------------*/
1503 for (i = 0; i < ds390_nRegs; i++)
1504 regs390[i].isFree = 1;
1507 /*-----------------------------------------------------------------*/
1508 /* deallocStackSpil - this will set the stack pointer back */
1509 /*-----------------------------------------------------------------*/
1511 DEFSETFUNC (deallocStackSpil)
1519 /*-----------------------------------------------------------------*/
1520 /* farSpacePackable - returns the packable icode for far variables */
1521 /*-----------------------------------------------------------------*/
1523 farSpacePackable (iCode * ic)
1527 /* go thru till we find a definition for the
1528 symbol on the right */
1529 for (dic = ic->prev; dic; dic = dic->prev)
1532 /* if the definition is a call then no */
1533 if ((dic->op == CALL || dic->op == PCALL) &&
1534 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1539 /* if shift by unknown amount then not */
1540 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1541 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1544 /* if pointer get and size > 1 */
1545 if (POINTER_GET (dic) &&
1546 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1549 if (POINTER_SET (dic) &&
1550 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1553 /* if any three is a true symbol in far space */
1554 if (IC_RESULT (dic) &&
1555 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1556 isOperandInFarSpace (IC_RESULT (dic)))
1559 if (IC_RIGHT (dic) &&
1560 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1561 isOperandInFarSpace (IC_RIGHT (dic)) &&
1562 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1565 if (IC_LEFT (dic) &&
1566 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1567 isOperandInFarSpace (IC_LEFT (dic)) &&
1568 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1571 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1573 if ((dic->op == LEFT_OP ||
1574 dic->op == RIGHT_OP ||
1576 IS_OP_LITERAL (IC_RIGHT (dic)))
1586 /*-----------------------------------------------------------------*/
1587 /* packRegsForAssign - register reduction for assignment */
1588 /*-----------------------------------------------------------------*/
1590 packRegsForAssign (iCode * ic, eBBlock * ebp)
1594 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1595 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1596 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1601 /* if the true symbol is defined in far space or on stack
1602 then we should not since this will increase register pressure */
1604 if (isOperandInFarSpace (IC_RESULT (ic)))
1606 if ((dic = farSpacePackable (ic)))
1612 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1617 /* find the definition of iTempNN scanning backwards if we find a
1618 a use of the true symbol in before we find the definition then
1620 for (dic = ic->prev; dic; dic = dic->prev)
1622 #if 0 // jwk 20010410, the JanVanBelle case
1623 /* if there is a function call and this is
1624 a parameter & not my parameter then don't pack it */
1625 if ((dic->op == CALL || dic->op == PCALL) &&
1626 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1627 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1633 /* if there is a function call then don't pack it */
1634 if ((dic->op == CALL || dic->op == PCALL))
1644 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1645 IS_OP_VOLATILE (IC_RESULT (dic)))
1651 if (IS_SYMOP (IC_RESULT (dic)) &&
1652 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1654 if (POINTER_SET (dic))
1660 if (IS_SYMOP (IC_RIGHT (dic)) &&
1661 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1662 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1668 if (IS_SYMOP (IC_LEFT (dic)) &&
1669 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1670 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1676 if (POINTER_SET (dic) &&
1677 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1685 return 0; /* did not find */
1687 /* if the result is on stack or iaccess then it must be
1688 the same atleast one of the operands */
1689 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1690 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1693 /* the operation has only one symbol
1694 operator then we can pack */
1695 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1696 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1699 if (!((IC_LEFT (dic) &&
1700 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1702 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1706 /* found the definition */
1707 /* replace the result with the result of */
1708 /* this assignment and remove this assignment */
1709 IC_RESULT (dic) = IC_RESULT (ic);
1711 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1713 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1715 /* delete from liverange table also
1716 delete from all the points inbetween and the new
1718 for (sic = dic; sic != ic; sic = sic->next)
1720 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1721 if (IS_ITEMP (IC_RESULT (dic)))
1722 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1725 remiCodeFromeBBlock (ebp, ic);
1726 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1727 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1732 /*-----------------------------------------------------------------*/
1733 /* findAssignToSym : scanning backwards looks for first assig found */
1734 /*-----------------------------------------------------------------*/
1736 findAssignToSym (operand * op, iCode * ic)
1740 for (dic = ic->prev; dic; dic = dic->prev)
1743 /* if definition by assignment */
1744 if (dic->op == '=' &&
1745 !POINTER_SET (dic) &&
1746 IC_RESULT (dic)->key == op->key
1747 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1751 /* we are interested only if defined in far space */
1752 /* or in stack space in case of + & - */
1754 /* if assigned to a non-symbol then return
1756 if (!IS_SYMOP (IC_RIGHT (dic)))
1759 /* if the symbol is in far space then
1761 if (isOperandInFarSpace (IC_RIGHT (dic)))
1764 /* for + & - operations make sure that
1765 if it is on the stack it is the same
1766 as one of the three operands */
1767 if ((ic->op == '+' || ic->op == '-') &&
1768 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1771 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1772 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1773 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1781 /* if we find an usage then we cannot delete it */
1782 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1785 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1788 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1792 /* now make sure that the right side of dic
1793 is not defined between ic & dic */
1796 iCode *sic = dic->next;
1798 for (; sic != ic; sic = sic->next)
1799 if (IC_RESULT (sic) &&
1800 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1809 /*-----------------------------------------------------------------*/
1810 /* packRegsForSupport :- reduce some registers for support calls */
1811 /*-----------------------------------------------------------------*/
1813 packRegsForSupport (iCode * ic, eBBlock * ebp)
1816 /* for the left & right operand :- look to see if the
1817 left was assigned a true symbol in far space in that
1818 case replace them */
1819 if (IS_ITEMP (IC_LEFT (ic)) &&
1820 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1822 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1828 /* found it we need to remove it from the
1830 for (sic = dic; sic != ic; sic = sic->next)
1831 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1833 IC_LEFT (ic)->operand.symOperand =
1834 IC_RIGHT (dic)->operand.symOperand;
1835 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1836 remiCodeFromeBBlock (ebp, dic);
1837 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1841 /* do the same for the right operand */
1844 IS_ITEMP (IC_RIGHT (ic)) &&
1845 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1847 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1853 /* if this is a subtraction & the result
1854 is a true symbol in far space then don't pack */
1855 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1857 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1858 if (IN_FARSPACE (SPEC_OCLS (etype)))
1861 /* found it we need to remove it from the
1863 for (sic = dic; sic != ic; sic = sic->next)
1864 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1866 IC_RIGHT (ic)->operand.symOperand =
1867 IC_RIGHT (dic)->operand.symOperand;
1868 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1870 remiCodeFromeBBlock (ebp, dic);
1871 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1878 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1881 /*-----------------------------------------------------------------*/
1882 /* packRegsForOneuse : - will reduce some registers for single Use */
1883 /*-----------------------------------------------------------------*/
1885 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1889 /* I can't figure out how to make this safe yet. */
1890 if ((int)ic+(int)op+(int)ebp) {
1901 /* if returning a literal then do nothing */
1905 /* only upto 2 bytes since we cannot predict
1906 the usage of b, & acc */
1907 if (getSize (operandType (op)) > (fReturnSizeDS390 - 2) &&
1910 !POINTER_SET (ic) &&
1914 /* this routine will mark the a symbol as used in one
1915 instruction use only && if the defintion is local
1916 (ie. within the basic block) && has only one definition &&
1917 that definiion is either a return value from a
1918 function or does not contain any variables in
1920 uses = bitVectCopy (OP_USES (op));
1921 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1922 if (!bitVectIsZero (uses)) /* has other uses */
1925 /* if it has only one defintion */
1926 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1927 return NULL; /* has more than one definition */
1929 /* get the that definition */
1931 hTabItemWithKey (iCodehTab,
1932 bitVectFirstBit (OP_DEFS (op)))))
1935 /* found the definition now check if it is local */
1936 if (dic->seq < ebp->fSeq ||
1937 dic->seq > ebp->lSeq)
1938 return NULL; /* non-local */
1940 /* now check if it is the return from
1942 if (dic->op == CALL || dic->op == PCALL)
1944 if (ic->op != SEND && ic->op != RETURN)
1946 OP_SYMBOL (op)->ruonly = 1;
1953 /* otherwise check that the definition does
1954 not contain any symbols in far space */
1955 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1956 isOperandInFarSpace (IC_RIGHT (dic)) ||
1957 IS_OP_RUONLY (IC_LEFT (ic)) ||
1958 IS_OP_RUONLY (IC_RIGHT (ic)))
1963 /* if pointer set then make sure the pointer
1965 if (POINTER_SET (dic) &&
1966 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1969 if (POINTER_GET (dic) &&
1970 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1975 /* also make sure the intervenening instructions
1976 don't have any thing in far space */
1977 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1980 /* if there is an intervening function call then no */
1981 if (dic->op == CALL || dic->op == PCALL)
1983 /* if pointer set then make sure the pointer
1985 if (POINTER_SET (dic) &&
1986 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1989 if (POINTER_GET (dic) &&
1990 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1993 /* if address of & the result is remat the okay */
1994 if (dic->op == ADDRESS_OF &&
1995 OP_SYMBOL (IC_RESULT (dic))->remat)
1998 /* if operand has size of three or more & this
1999 operation is a '*','/' or '%' then 'b' may
2001 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2002 getSize (operandType (op)) >= 3)
2005 /* if left or right or result is in far space */
2006 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2007 isOperandInFarSpace (IC_RIGHT (dic)) ||
2008 isOperandInFarSpace (IC_RESULT (dic)) ||
2009 IS_OP_RUONLY (IC_LEFT (dic)) ||
2010 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2011 IS_OP_RUONLY (IC_RESULT (dic)))
2017 OP_SYMBOL (op)->ruonly = 1;
2022 /*-----------------------------------------------------------------*/
2023 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2024 /*-----------------------------------------------------------------*/
2026 isBitwiseOptimizable (iCode * ic)
2028 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2029 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2031 /* bitwise operations are considered optimizable
2032 under the following conditions (Jean-Louis VERN)
2044 if ( IS_LITERAL (rtype) ||
2045 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2051 /*-----------------------------------------------------------------*/
2052 /* packRegsForAccUse - pack registers for acc use */
2053 /*-----------------------------------------------------------------*/
2055 packRegsForAccUse (iCode * ic)
2059 /* if + or - then it has to be one byte result */
2060 if ((ic->op == '+' || ic->op == '-')
2061 && getSize (operandType (IC_RESULT (ic))) > 1)
2064 /* if shift operation make sure right side is not a literal */
2065 if (ic->op == RIGHT_OP &&
2066 (isOperandLiteral (IC_RIGHT (ic)) ||
2067 getSize (operandType (IC_RESULT (ic))) > 1))
2070 if (ic->op == LEFT_OP &&
2071 (isOperandLiteral (IC_RIGHT (ic)) ||
2072 getSize (operandType (IC_RESULT (ic))) > 1))
2075 if (IS_BITWISE_OP (ic) &&
2076 getSize (operandType (IC_RESULT (ic))) > 1)
2080 /* has only one definition */
2081 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2084 /* has only one use */
2085 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2088 /* and the usage immediately follows this iCode */
2089 if (!(uic = hTabItemWithKey (iCodehTab,
2090 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2093 if (ic->next != uic)
2096 /* if it is a conditional branch then we definitely can */
2100 if (uic->op == JUMPTABLE)
2103 /* if the usage is not is an assignment
2104 or an arithmetic / bitwise / shift operation then not */
2105 if (POINTER_SET (uic) &&
2106 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2109 if (uic->op != '=' &&
2110 !IS_ARITHMETIC_OP (uic) &&
2111 !IS_BITWISE_OP (uic) &&
2112 uic->op != LEFT_OP &&
2113 uic->op != RIGHT_OP)
2116 /* if used in ^ operation then make sure right is not a
2118 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2121 /* if shift operation make sure right side is not a literal */
2122 if (uic->op == RIGHT_OP &&
2123 (isOperandLiteral (IC_RIGHT (uic)) ||
2124 getSize (operandType (IC_RESULT (uic))) > 1))
2127 if (uic->op == LEFT_OP &&
2128 (isOperandLiteral (IC_RIGHT (uic)) ||
2129 getSize (operandType (IC_RESULT (uic))) > 1))
2132 /* make sure that the result of this icode is not on the
2133 stack, since acc is used to compute stack offset */
2134 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2135 OP_SYMBOL (IC_RESULT (uic))->onStack)
2138 /* if either one of them in far space then we cannot */
2139 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2140 isOperandInFarSpace (IC_LEFT (uic))) ||
2141 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2142 isOperandInFarSpace (IC_RIGHT (uic))))
2145 /* if the usage has only one operand then we can */
2146 if (IC_LEFT (uic) == NULL ||
2147 IC_RIGHT (uic) == NULL)
2150 /* make sure this is on the left side if not
2151 a '+' since '+' is commutative */
2152 if (ic->op != '+' &&
2153 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2156 /* if one of them is a literal then we can */
2157 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2158 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2160 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2164 /* if the other one is not on stack then we can */
2165 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2166 (IS_ITEMP (IC_RIGHT (uic)) ||
2167 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2168 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2171 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2172 (IS_ITEMP (IC_LEFT (uic)) ||
2173 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2174 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2180 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2185 /*-----------------------------------------------------------------*/
2186 /* packForPush - hueristics to reduce iCode for pushing */
2187 /*-----------------------------------------------------------------*/
2189 packForPush (iCode * ic, eBBlock * ebp)
2194 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2197 /* must have only definition & one usage */
2198 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2199 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2202 /* find the definition */
2203 if (!(dic = hTabItemWithKey (iCodehTab,
2204 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2207 if (dic->op != '=' || POINTER_SET (dic))
2210 /* make sure the right side does not have any definitions
2212 dbv = OP_DEFS(IC_RIGHT(dic));
2213 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2214 if (bitVectBitValue(dbv,lic->key)) return ;
2216 /* make sure they have the same type */
2218 sym_link *itype=operandType(IC_LEFT(ic));
2219 sym_link *ditype=operandType(IC_RIGHT(dic));
2221 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2222 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2225 /* extend the live range of replaced operand if needed */
2226 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2227 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2229 /* we now we know that it has one & only one def & use
2230 and the that the definition is an assignment */
2231 IC_LEFT (ic) = IC_RIGHT (dic);
2233 remiCodeFromeBBlock (ebp, dic);
2234 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2237 /*-----------------------------------------------------------------*/
2238 /* packRegisters - does some transformations to reduce register */
2240 /*-----------------------------------------------------------------*/
2242 packRegisters (eBBlock * ebp)
2252 /* look for assignments of the form */
2253 /* iTempNN = TRueSym (someoperation) SomeOperand */
2255 /* TrueSym := iTempNN:1 */
2256 for (ic = ebp->sch; ic; ic = ic->next)
2260 /* find assignment of the form TrueSym := iTempNN:1 */
2261 if (ic->op == '=' && !POINTER_SET (ic))
2262 change += packRegsForAssign (ic, ebp);
2269 for (ic = ebp->sch; ic; ic = ic->next)
2272 /* if this is an itemp & result of a address of a true sym
2273 then mark this as rematerialisable */
2274 if (ic->op == ADDRESS_OF &&
2275 IS_ITEMP (IC_RESULT (ic)) &&
2276 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2277 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2278 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2281 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2282 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2283 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2287 /* if straight assignment then carry remat flag if
2288 this is the only definition */
2289 if (ic->op == '=' &&
2290 !POINTER_SET (ic) &&
2291 IS_SYMOP (IC_RIGHT (ic)) &&
2292 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2293 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2296 OP_SYMBOL (IC_RESULT (ic))->remat =
2297 OP_SYMBOL (IC_RIGHT (ic))->remat;
2298 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2299 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2302 /* if this is a +/- operation with a rematerizable
2303 then mark this as rematerializable as well */
2304 if ((ic->op == '+' || ic->op == '-') &&
2305 (IS_SYMOP (IC_LEFT (ic)) &&
2306 IS_ITEMP (IC_RESULT (ic)) &&
2307 OP_SYMBOL (IC_LEFT (ic))->remat &&
2308 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2309 IS_OP_LITERAL (IC_RIGHT (ic))))
2312 //int i = operandLitValue(IC_RIGHT(ic));
2313 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2314 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2315 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2318 /* mark the pointer usages */
2319 if (POINTER_SET (ic))
2320 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2322 if (POINTER_GET (ic))
2323 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2327 /* if we are using a symbol on the stack
2328 then we should say ds390_ptrRegReq */
2329 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2330 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2331 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2332 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2333 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2334 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2337 if (IS_SYMOP (IC_LEFT (ic)))
2338 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2339 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2340 if (IS_SYMOP (IC_RIGHT (ic)))
2341 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2342 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2343 if (IS_SYMOP (IC_RESULT (ic)))
2344 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2345 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2350 /* if the condition of an if instruction
2351 is defined in the previous instruction then
2352 mark the itemp as a conditional */
2353 if ((IS_CONDITIONAL (ic) ||
2354 (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2355 ic->next && ic->next->op == IFX &&
2356 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2357 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2360 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2364 /* if the condition of an if instruction
2365 is defined in the previous instruction and
2366 this is the only usage then
2367 mark the itemp as a conditional */
2368 if ((IS_CONDITIONAL (ic) ||
2369 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2370 ic->next && ic->next->op == IFX &&
2371 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2372 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2373 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2375 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2380 /* reduce for support function calls */
2381 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2382 packRegsForSupport (ic, ebp);
2384 /* some cases the redundant moves can
2385 can be eliminated for return statements */
2386 if ((ic->op == RETURN || ic->op == SEND) &&
2387 !isOperandInFarSpace (IC_LEFT (ic)) &&
2389 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2391 /* if pointer set & left has a size more than
2392 one and right is not in far space */
2393 if (POINTER_SET (ic) &&
2394 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2395 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2396 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2397 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2399 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2401 /* if pointer get */
2402 if (POINTER_GET (ic) &&
2403 !isOperandInFarSpace (IC_RESULT (ic)) &&
2404 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2405 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2406 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2408 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2411 /* if this is cast for intergral promotion then
2412 check if only use of the definition of the
2413 operand being casted/ if yes then replace
2414 the result of that arithmetic operation with
2415 this result and get rid of the cast */
2418 sym_link *fromType = operandType (IC_RIGHT (ic));
2419 sym_link *toType = operandType (IC_LEFT (ic));
2421 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2422 getSize (fromType) != getSize (toType) &&
2423 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2426 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2429 if (IS_ARITHMETIC_OP (dic))
2431 IC_RESULT (dic) = IC_RESULT (ic);
2432 remiCodeFromeBBlock (ebp, ic);
2433 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2434 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2438 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2444 /* if the type from and type to are the same
2445 then if this is the only use then packit */
2446 if (compareType (operandType (IC_RIGHT (ic)),
2447 operandType (IC_LEFT (ic))) == 1)
2449 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2452 IC_RESULT (dic) = IC_RESULT (ic);
2453 remiCodeFromeBBlock (ebp, ic);
2454 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2455 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2463 iTempNN := (some variable in farspace) V1
2468 if (ic->op == IPUSH)
2470 packForPush (ic, ebp);
2474 /* pack registers for accumulator use, when the
2475 result of an arithmetic or bit wise operation
2476 has only one use, that use is immediately following
2477 the defintion and the using iCode has only one
2478 operand or has two operands but one is literal &
2479 the result of that operation is not on stack then
2480 we can leave the result of this operation in acc:b
2482 if ((IS_ARITHMETIC_OP (ic)
2484 || IS_BITWISE_OP (ic)
2486 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2489 IS_ITEMP (IC_RESULT (ic)) &&
2490 getSize (operandType (IC_RESULT (ic))) <= 2)
2492 packRegsForAccUse (ic);
2497 /*-----------------------------------------------------------------*/
2498 /* assignRegisters - assigns registers to each live range as need */
2499 /*-----------------------------------------------------------------*/
2501 ds390_assignRegisters (eBBlock ** ebbs, int count)
2506 setToNull ((void *) &_G.funcrUsed);
2507 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2508 /* if not register extentions then reduce number
2510 if (options.regExtend)
2515 /* change assignments this will remove some
2516 live ranges reducing some register pressure */
2517 for (i = 0; i < count; i++)
2518 packRegisters (ebbs[i]);
2520 if (options.dump_pack)
2521 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2523 /* first determine for each live range the number of
2524 registers & the type of registers required for each */
2527 /* and serially allocate registers */
2528 serialRegAssign (ebbs, count);
2530 /* if stack was extended then tell the user */
2533 /* werror(W_TOOMANY_SPILS,"stack", */
2534 /* _G.stackExtend,currFunc->name,""); */
2540 /* werror(W_TOOMANY_SPILS,"data space", */
2541 /* _G.dataExtend,currFunc->name,""); */
2545 /* after that create the register mask
2546 for each of the instruction */
2547 createRegMask (ebbs, count);
2549 /* redo that offsets for stacked automatic variables */
2550 redoStackOffsets ();
2552 if (options.dump_rassgn)
2553 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2555 /* do the overlaysegment stuff SDCCmem.c */
2556 doOverlays (ebbs, count);
2558 /* now get back the chain */
2559 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2564 /* free up any _G.stackSpil locations allocated */
2565 applyToSet (_G.stackSpil, deallocStackSpil);
2567 setToNull ((void **) &_G.stackSpil);
2568 setToNull ((void **) &_G.spiltSet);
2569 /* mark all registers as free */