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 if (options.model == MODEL_SMALL) {
512 SPEC_SCLS (sloc->etype) = S_DATA;
514 SPEC_SCLS (sloc->etype) = S_XDATA;
516 SPEC_EXTR (sloc->etype) = 0;
518 /* we don't allow it to be allocated`
519 onto the external stack since : so we
520 temporarily turn it off ; we also
521 turn off memory model to prevent
522 the spil from going to the external storage
523 and turn off overlaying
526 useXstack = options.useXstack;
527 model = options.model;
528 noOverlay = options.noOverlay;
529 options.noOverlay = 1;
531 /* options.model = options.useXstack = 0; */
535 options.useXstack = useXstack;
536 options.model = model;
537 options.noOverlay = noOverlay;
538 sloc->isref = 1; /* to prevent compiler warning */
540 /* if it is on the stack then update the stack */
541 if (IN_STACK (sloc->etype))
543 currFunc->stack += getSize (sloc->type);
544 _G.stackExtend += getSize (sloc->type);
547 _G.dataExtend += getSize (sloc->type);
549 /* add it to the _G.stackSpil set */
550 addSetHead (&_G.stackSpil, sloc);
551 sym->usl.spillLoc = sloc;
554 /* add it to the set of itempStack set
555 of the spill location */
556 addSetHead (&sloc->usl.itmpStack, sym);
560 /*-----------------------------------------------------------------*/
561 /* isSpiltOnStack - returns true if the spil location is on stack */
562 /*-----------------------------------------------------------------*/
564 isSpiltOnStack (symbol * sym)
574 /* if (sym->_G.stackSpil) */
577 if (!sym->usl.spillLoc)
580 etype = getSpec (sym->usl.spillLoc->type);
581 if (IN_STACK (etype))
587 /*-----------------------------------------------------------------*/
588 /* spillThis - spils a specific operand */
589 /*-----------------------------------------------------------------*/
591 spillThis (symbol * sym)
594 /* if this is rematerializable or has a spillLocation
595 we are okay, else we need to create a spillLocation
597 if (!(sym->remat || sym->usl.spillLoc))
598 createStackSpil (sym);
601 /* mark it has spilt & put it in the spilt set */
603 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
605 bitVectUnSetBit (_G.regAssigned, sym->key);
607 for (i = 0; i < sym->nRegs; i++)
611 freeReg (sym->regs[i]);
615 /* if spilt on stack then free up r0 & r1
616 if they could have been assigned to some
618 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
620 ds390_ptrRegReq += !options.stack10bit;
621 spillLRWithPtrReg (sym);
624 if (sym->usl.spillLoc && !sym->remat)
625 sym->usl.spillLoc->allocreq = 1;
629 /*-----------------------------------------------------------------*/
630 /* selectSpil - select a iTemp to spil : rather a simple procedure */
631 /*-----------------------------------------------------------------*/
633 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
635 bitVect *lrcs = NULL;
639 /* get the spillable live ranges */
640 lrcs = computeSpillable (ic);
642 /* get all live ranges that are rematerizable */
643 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
646 /* return the least used of these */
647 return leastUsedLR (selectS);
650 /* get live ranges with spillLocations in direct space */
651 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
653 sym = leastUsedLR (selectS);
654 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
655 sym->usl.spillLoc->rname :
656 sym->usl.spillLoc->name));
658 /* mark it as allocation required */
659 sym->usl.spillLoc->allocreq = 1;
663 /* if the symbol is local to the block then */
664 if (forSym->liveTo < ebp->lSeq)
667 /* check if there are any live ranges allocated
668 to registers that are not used in this block */
669 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
671 sym = leastUsedLR (selectS);
672 /* if this is not rematerializable */
681 /* check if there are any live ranges that not
682 used in the remainder of the block */
683 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
685 sym = leastUsedLR (selectS);
698 /* find live ranges with spillocation && not used as pointers */
699 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
702 sym = leastUsedLR (selectS);
703 /* mark this as allocation required */
704 sym->usl.spillLoc->allocreq = 1;
708 /* find live ranges with spillocation */
709 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
712 sym = leastUsedLR (selectS);
713 sym->usl.spillLoc->allocreq = 1;
717 /* couldn't find then we need to create a spil
718 location on the stack , for which one? the least
720 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
723 /* return a created spil location */
724 sym = createStackSpil (leastUsedLR (selectS));
725 sym->usl.spillLoc->allocreq = 1;
729 /* this is an extreme situation we will spill
730 this one : happens very rarely but it does happen */
736 /*-----------------------------------------------------------------*/
737 /* spilSomething - spil some variable & mark registers as free */
738 /*-----------------------------------------------------------------*/
740 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
745 /* get something we can spil */
746 ssym = selectSpil (ic, ebp, forSym);
748 /* mark it as spilt */
750 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
752 /* mark it as not register assigned &
753 take it away from the set */
754 bitVectUnSetBit (_G.regAssigned, ssym->key);
756 /* mark the registers as free */
757 for (i = 0; i < ssym->nRegs; i++)
759 freeReg (ssym->regs[i]);
761 /* if spilt on stack then free up r0 & r1
762 if they could have been assigned to as gprs */
763 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
766 spillLRWithPtrReg (ssym);
769 /* if this was a block level spil then insert push & pop
770 at the start & end of block respectively */
773 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
774 /* add push to the start of the block */
775 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
776 ebp->sch->next : ebp->sch));
777 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
778 /* add pop to the end of the block */
779 addiCodeToeBBlock (ebp, nic, NULL);
782 /* if spilt because not used in the remainder of the
783 block then add a push before this instruction and
784 a pop at the end of the block */
785 if (ssym->remainSpil)
788 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
789 /* add push just before this instruction */
790 addiCodeToeBBlock (ebp, nic, ic);
792 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
793 /* add pop to the end of the block */
794 addiCodeToeBBlock (ebp, nic, NULL);
803 /*-----------------------------------------------------------------*/
804 /* getRegPtr - will try for PTR if not a GPR type if not spil */
805 /*-----------------------------------------------------------------*/
807 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
812 /* try for a ptr type */
813 if ((reg = allocReg (REG_PTR)))
816 /* try for gpr type */
817 if ((reg = allocReg (REG_GPR)))
820 /* we have to spil */
821 if (!spilSomething (ic, ebp, sym))
824 /* this looks like an infinite loop but
825 in really selectSpil will abort */
829 /*-----------------------------------------------------------------*/
830 /* getRegGpr - will try for GPR if not spil */
831 /*-----------------------------------------------------------------*/
833 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
838 /* try for gpr type */
839 if ((reg = allocReg (REG_GPR)))
842 if (!ds390_ptrRegReq)
843 if ((reg = allocReg (REG_PTR)))
846 /* we have to spil */
847 if (!spilSomething (ic, ebp, sym))
850 /* this looks like an infinite loop but
851 in really selectSpil will abort */
855 /*-----------------------------------------------------------------*/
856 /* symHasReg - symbol has a given register */
857 /*-----------------------------------------------------------------*/
859 symHasReg (symbol * sym, regs * reg)
863 for (i = 0; i < sym->nRegs; i++)
864 if (sym->regs[i] == reg)
870 /*-----------------------------------------------------------------*/
871 /* deassignLRs - check the live to and if they have registers & are */
872 /* not spilt then free up the registers */
873 /*-----------------------------------------------------------------*/
875 deassignLRs (iCode * ic, eBBlock * ebp)
881 for (sym = hTabFirstItem (liveRanges, &k); sym;
882 sym = hTabNextItem (liveRanges, &k))
886 /* if it does not end here */
887 if (sym->liveTo > ic->seq)
890 /* if it was spilt on stack then we can
891 mark the stack spil location as free */
896 sym->usl.spillLoc->isFree = 1;
902 if (!bitVectBitValue (_G.regAssigned, sym->key))
905 /* special case check if this is an IFX &
906 the privious one was a pop and the
907 previous one was not spilt then keep track
909 if (ic->op == IFX && ic->prev &&
910 ic->prev->op == IPOP &&
911 !ic->prev->parmPush &&
912 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
913 psym = OP_SYMBOL (IC_LEFT (ic->prev));
919 bitVectUnSetBit (_G.regAssigned, sym->key);
921 /* if the result of this one needs registers
922 and does not have it then assign it right
924 if (IC_RESULT (ic) &&
925 !(SKIP_IC2 (ic) || /* not a special icode */
926 ic->op == JUMPTABLE ||
932 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
933 result->liveTo > ic->seq && /* and will live beyond this */
934 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
935 result->regType == sym->regType && /* same register types */
936 result->nRegs && /* which needs registers */
937 !result->isspilt && /* and does not already have them */
939 !bitVectBitValue (_G.regAssigned, result->key) &&
940 /* the number of free regs + number of regs in this LR
941 can accomodate the what result Needs */
942 ((nfreeRegsType (result->regType) +
943 sym->nRegs) >= result->nRegs)
947 for (i = 0; i < result->nRegs; i++)
949 result->regs[i] = sym->regs[i];
951 result->regs[i] = getRegGpr (ic, ebp, result);
953 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
957 /* free the remaining */
958 for (; i < sym->nRegs; i++)
962 if (!symHasReg (psym, sym->regs[i]))
963 freeReg (sym->regs[i]);
966 freeReg (sym->regs[i]);
973 /*-----------------------------------------------------------------*/
974 /* reassignLR - reassign this to registers */
975 /*-----------------------------------------------------------------*/
977 reassignLR (operand * op)
979 symbol *sym = OP_SYMBOL (op);
982 /* not spilt any more */
983 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
984 bitVectUnSetBit (_G.spiltSet, sym->key);
986 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
990 for (i = 0; i < sym->nRegs; i++)
991 sym->regs[i]->isFree = 0;
994 /*-----------------------------------------------------------------*/
995 /* willCauseSpill - determines if allocating will cause a spill */
996 /*-----------------------------------------------------------------*/
998 willCauseSpill (int nr, int rt)
1000 /* first check if there are any avlb registers
1001 of te type required */
1004 /* special case for pointer type
1005 if pointer type not avlb then
1006 check for type gpr */
1007 if (nFreeRegs (rt) >= nr)
1009 if (nFreeRegs (REG_GPR) >= nr)
1014 if (ds390_ptrRegReq)
1016 if (nFreeRegs (rt) >= nr)
1021 if (nFreeRegs (REG_PTR) +
1022 nFreeRegs (REG_GPR) >= nr)
1027 /* it will cause a spil */
1031 /*-----------------------------------------------------------------*/
1032 /* positionRegs - the allocator can allocate same registers to res- */
1033 /* ult and operand, if this happens make sure they are in the same */
1034 /* position as the operand otherwise chaos results */
1035 /*-----------------------------------------------------------------*/
1037 positionRegs (symbol * result, symbol * opsym, int lineno)
1039 int count = min (result->nRegs, opsym->nRegs);
1040 int i, j = 0, shared = 0;
1042 /* if the result has been spilt then cannot share */
1047 /* first make sure that they actually share */
1048 for (i = 0; i < count; i++)
1050 for (j = 0; j < count; j++)
1052 if (result->regs[i] == opsym->regs[j] && i != j)
1062 regs *tmp = result->regs[i];
1063 result->regs[i] = result->regs[j];
1064 result->regs[j] = tmp;
1069 /*-----------------------------------------------------------------*/
1070 /* serialRegAssign - serially allocate registers to the variables */
1071 /*-----------------------------------------------------------------*/
1073 serialRegAssign (eBBlock ** ebbs, int count)
1077 /* for all blocks */
1078 for (i = 0; i < count; i++)
1083 if (ebbs[i]->noPath &&
1084 (ebbs[i]->entryLabel != entryLabel &&
1085 ebbs[i]->entryLabel != returnLabel))
1088 /* of all instructions do */
1089 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1092 /* if this is an ipop that means some live
1093 range will have to be assigned again */
1095 reassignLR (IC_LEFT (ic));
1097 /* if result is present && is a true symbol */
1098 if (IC_RESULT (ic) && ic->op != IFX &&
1099 IS_TRUE_SYMOP (IC_RESULT (ic)))
1100 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1102 /* take away registers from live
1103 ranges that end at this instruction */
1104 deassignLRs (ic, ebbs[i]);
1106 /* some don't need registers */
1107 if (SKIP_IC2 (ic) ||
1108 ic->op == JUMPTABLE ||
1112 (IC_RESULT (ic) && POINTER_SET (ic)))
1115 /* now we need to allocate registers
1116 only for the result */
1119 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1125 /* if it does not need or is spilt
1126 or is already assigned to registers
1127 or will not live beyond this instructions */
1130 bitVectBitValue (_G.regAssigned, sym->key) ||
1131 sym->liveTo <= ic->seq)
1134 /* if some liverange has been spilt at the block level
1135 and this one live beyond this block then spil this
1137 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1142 /* if trying to allocate this will cause
1143 a spill and there is nothing to spill
1144 or this one is rematerializable then
1146 willCS = willCauseSpill (sym->nRegs, sym->regType);
1147 spillable = computeSpillable (ic);
1149 (willCS && bitVectIsZero (spillable)))
1157 /* if it has a spillocation & is used less than
1158 all other live ranges then spill this */
1160 if (sym->usl.spillLoc) {
1161 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1162 allLRs, ebbs[i], ic));
1163 if (leastUsed && leastUsed->used > sym->used) {
1168 /* if none of the liveRanges have a spillLocation then better
1169 to spill this one than anything else already assigned to registers */
1170 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1177 /* if we need ptr regs for the right side
1179 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1180 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1181 <= (unsigned) PTRSIZE)
1186 /* else we assign registers to it */
1187 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1189 for (j = 0; j < sym->nRegs; j++)
1191 if (sym->regType == REG_PTR)
1192 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1194 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1196 /* if the allocation falied which means
1197 this was spilt then break */
1201 /* if it shares registers with operands make sure
1202 that they are in the same position */
1203 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1204 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1205 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1206 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1207 /* do the same for the right operand */
1208 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1209 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1210 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1211 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1224 /*-----------------------------------------------------------------*/
1225 /* rUmaskForOp :- returns register mask for an operand */
1226 /*-----------------------------------------------------------------*/
1228 rUmaskForOp (operand * op)
1234 /* only temporaries are assigned registers */
1238 sym = OP_SYMBOL (op);
1240 /* if spilt or no registers assigned to it
1242 if (sym->isspilt || !sym->nRegs)
1245 rumask = newBitVect (ds390_nRegs);
1247 for (j = 0; j < sym->nRegs; j++)
1249 rumask = bitVectSetBit (rumask,
1250 sym->regs[j]->rIdx);
1256 /*-----------------------------------------------------------------*/
1257 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1258 /*-----------------------------------------------------------------*/
1260 regsUsedIniCode (iCode * ic)
1262 bitVect *rmask = newBitVect (ds390_nRegs);
1264 /* do the special cases first */
1267 rmask = bitVectUnion (rmask,
1268 rUmaskForOp (IC_COND (ic)));
1272 /* for the jumptable */
1273 if (ic->op == JUMPTABLE)
1275 rmask = bitVectUnion (rmask,
1276 rUmaskForOp (IC_JTCOND (ic)));
1281 /* of all other cases */
1283 rmask = bitVectUnion (rmask,
1284 rUmaskForOp (IC_LEFT (ic)));
1288 rmask = bitVectUnion (rmask,
1289 rUmaskForOp (IC_RIGHT (ic)));
1292 rmask = bitVectUnion (rmask,
1293 rUmaskForOp (IC_RESULT (ic)));
1299 /*-----------------------------------------------------------------*/
1300 /* createRegMask - for each instruction will determine the regsUsed */
1301 /*-----------------------------------------------------------------*/
1303 createRegMask (eBBlock ** ebbs, int count)
1307 /* for all blocks */
1308 for (i = 0; i < count; i++)
1312 if (ebbs[i]->noPath &&
1313 (ebbs[i]->entryLabel != entryLabel &&
1314 ebbs[i]->entryLabel != returnLabel))
1317 /* for all instructions */
1318 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1323 if (SKIP_IC2 (ic) || !ic->rlive)
1326 /* first mark the registers used in this
1328 ic->rUsed = regsUsedIniCode (ic);
1329 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1331 /* now create the register mask for those
1332 registers that are in use : this is a
1333 super set of ic->rUsed */
1334 ic->rMask = newBitVect (ds390_nRegs + 1);
1336 /* for all live Ranges alive at this point */
1337 for (j = 1; j < ic->rlive->size; j++)
1342 /* if not alive then continue */
1343 if (!bitVectBitValue (ic->rlive, j))
1346 /* find the live range we are interested in */
1347 if (!(sym = hTabItemWithKey (liveRanges, j)))
1349 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1350 "createRegMask cannot find live range");
1354 /* if no register assigned to it */
1355 if (!sym->nRegs || sym->isspilt)
1358 /* for all the registers allocated to it */
1359 for (k = 0; k < sym->nRegs; k++)
1362 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1368 /*-----------------------------------------------------------------*/
1369 /* rematStr - returns the rematerialized string for a remat var */
1370 /*-----------------------------------------------------------------*/
1372 rematStr (symbol * sym)
1375 iCode *ic = sym->rematiCode;
1380 /* if plus or minus print the right hand side */
1381 if (ic->op == '+' || ic->op == '-')
1383 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1386 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1389 /* cast then continue */
1390 if (IS_CAST_ICODE(ic)) {
1391 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1394 /* we reached the end */
1395 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1402 /*-----------------------------------------------------------------*/
1403 /* regTypeNum - computes the type & number of registers required */
1404 /*-----------------------------------------------------------------*/
1412 /* for each live range do */
1413 for (sym = hTabFirstItem (liveRanges, &k); sym;
1414 sym = hTabNextItem (liveRanges, &k))
1417 /* if used zero times then no registers needed */
1418 if ((sym->liveTo - sym->liveFrom) == 0)
1422 /* if the live range is a temporary */
1426 /* if the type is marked as a conditional */
1427 if (sym->regType == REG_CND)
1430 /* if used in return only then we don't
1432 if (sym->ruonly || sym->accuse)
1434 if (IS_AGGREGATE (sym->type) || sym->isptr)
1435 sym->type = aggrToPtr (sym->type, FALSE);
1439 /* if the symbol has only one definition &
1440 that definition is a get_pointer and the
1441 pointer we are getting is rematerializable and
1444 if (bitVectnBitsOn (sym->defs) == 1 &&
1445 (ic = hTabItemWithKey (iCodehTab,
1446 bitVectFirstBit (sym->defs))) &&
1449 !IS_BITVAR (sym->etype))
1453 /* if remat in data space */
1454 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1455 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1456 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1459 /* create a psuedo symbol & force a spil */
1460 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1461 psym->type = sym->type;
1462 psym->etype = sym->etype;
1463 strcpy (psym->rname, psym->name);
1465 sym->usl.spillLoc = psym;
1469 /* if in data space or idata space then try to
1470 allocate pointer register */
1474 /* if not then we require registers */
1475 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1476 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1477 getSize (sym->type));
1481 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1482 printTypeChain (sym->type, stderr);
1483 fprintf (stderr, "\n");
1486 /* determine the type of register required */
1487 if (sym->nRegs == 1 &&
1488 IS_PTR (sym->type) &&
1490 sym->regType = REG_PTR;
1492 sym->regType = REG_GPR;
1496 /* for the first run we don't provide */
1497 /* registers for true symbols we will */
1498 /* see how things go */
1504 /*-----------------------------------------------------------------*/
1505 /* freeAllRegs - mark all registers as free */
1506 /*-----------------------------------------------------------------*/
1512 for (i = 0; i < ds390_nRegs; i++)
1513 regs390[i].isFree = 1;
1516 /*-----------------------------------------------------------------*/
1517 /* deallocStackSpil - this will set the stack pointer back */
1518 /*-----------------------------------------------------------------*/
1520 DEFSETFUNC (deallocStackSpil)
1528 /*-----------------------------------------------------------------*/
1529 /* farSpacePackable - returns the packable icode for far variables */
1530 /*-----------------------------------------------------------------*/
1532 farSpacePackable (iCode * ic)
1536 /* go thru till we find a definition for the
1537 symbol on the right */
1538 for (dic = ic->prev; dic; dic = dic->prev)
1541 /* if the definition is a call then no */
1542 if ((dic->op == CALL || dic->op == PCALL) &&
1543 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1548 /* if shift by unknown amount then not */
1549 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1550 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1553 /* if pointer get and size > 1 */
1554 if (POINTER_GET (dic) &&
1555 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1558 if (POINTER_SET (dic) &&
1559 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1562 /* if any three is a true symbol in far space */
1563 if (IC_RESULT (dic) &&
1564 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1565 isOperandInFarSpace (IC_RESULT (dic)))
1568 if (IC_RIGHT (dic) &&
1569 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1570 isOperandInFarSpace (IC_RIGHT (dic)) &&
1571 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1574 if (IC_LEFT (dic) &&
1575 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1576 isOperandInFarSpace (IC_LEFT (dic)) &&
1577 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1580 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1582 if ((dic->op == LEFT_OP ||
1583 dic->op == RIGHT_OP ||
1585 IS_OP_LITERAL (IC_RIGHT (dic)))
1595 /*-----------------------------------------------------------------*/
1596 /* packRegsForAssign - register reduction for assignment */
1597 /*-----------------------------------------------------------------*/
1599 packRegsForAssign (iCode * ic, eBBlock * ebp)
1603 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1604 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1605 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1610 /* if the true symbol is defined in far space or on stack
1611 then we should not since this will increase register pressure */
1613 if (isOperandInFarSpace (IC_RESULT (ic)))
1615 if ((dic = farSpacePackable (ic)))
1621 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1626 /* find the definition of iTempNN scanning backwards if we find a
1627 a use of the true symbol in before we find the definition then
1629 for (dic = ic->prev; dic; dic = dic->prev)
1631 /* if there is a function call then don't pack it */
1632 if ((dic->op == CALL || dic->op == PCALL))
1641 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1642 IS_OP_VOLATILE (IC_RESULT (dic)))
1648 if (IS_SYMOP (IC_RESULT (dic)) &&
1649 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1651 if (POINTER_SET (dic))
1657 if (IS_SYMOP (IC_RIGHT (dic)) &&
1658 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1659 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1665 if (IS_SYMOP (IC_LEFT (dic)) &&
1666 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1667 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1673 if (POINTER_SET (dic) &&
1674 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1682 return 0; /* did not find */
1684 /* if the result is on stack or iaccess then it must be
1685 the same atleast one of the operands */
1686 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1687 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1690 /* the operation has only one symbol
1691 operator then we can pack */
1692 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1693 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1696 if (!((IC_LEFT (dic) &&
1697 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1699 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1703 /* found the definition */
1704 /* replace the result with the result of */
1705 /* this assignment and remove this assignment */
1706 IC_RESULT (dic) = IC_RESULT (ic);
1708 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1710 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1712 /* delete from liverange table also
1713 delete from all the points inbetween and the new
1715 for (sic = dic; sic != ic; sic = sic->next)
1717 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1718 if (IS_ITEMP (IC_RESULT (dic)))
1719 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1722 remiCodeFromeBBlock (ebp, ic);
1723 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1724 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1729 /*-----------------------------------------------------------------*/
1730 /* findAssignToSym : scanning backwards looks for first assig found */
1731 /*-----------------------------------------------------------------*/
1733 findAssignToSym (operand * op, iCode * ic)
1737 for (dic = ic->prev; dic; dic = dic->prev)
1740 /* if definition by assignment */
1741 if (dic->op == '=' &&
1742 !POINTER_SET (dic) &&
1743 IC_RESULT (dic)->key == op->key
1744 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1748 /* we are interested only if defined in far space */
1749 /* or in stack space in case of + & - */
1751 /* if assigned to a non-symbol then return
1753 if (!IS_SYMOP (IC_RIGHT (dic)))
1756 /* if the symbol is in far space then
1758 if (isOperandInFarSpace (IC_RIGHT (dic)))
1761 /* for + & - operations make sure that
1762 if it is on the stack it is the same
1763 as one of the three operands */
1764 if ((ic->op == '+' || ic->op == '-') &&
1765 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1768 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1769 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1770 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1778 /* if we find an usage then we cannot delete it */
1779 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1782 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1785 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1789 /* now make sure that the right side of dic
1790 is not defined between ic & dic */
1793 iCode *sic = dic->next;
1795 for (; sic != ic; sic = sic->next)
1796 if (IC_RESULT (sic) &&
1797 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1806 /*-----------------------------------------------------------------*/
1807 /* packRegsForSupport :- reduce some registers for support calls */
1808 /*-----------------------------------------------------------------*/
1810 packRegsForSupport (iCode * ic, eBBlock * ebp)
1813 /* for the left & right operand :- look to see if the
1814 left was assigned a true symbol in far space in that
1815 case replace them */
1816 if (IS_ITEMP (IC_LEFT (ic)) &&
1817 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1819 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1825 /* found it we need to remove it from the
1827 for (sic = dic; sic != ic; sic = sic->next)
1828 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1830 IC_LEFT (ic)->operand.symOperand =
1831 IC_RIGHT (dic)->operand.symOperand;
1832 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1833 remiCodeFromeBBlock (ebp, dic);
1834 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1838 /* do the same for the right operand */
1841 IS_ITEMP (IC_RIGHT (ic)) &&
1842 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1844 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1850 /* if this is a subtraction & the result
1851 is a true symbol in far space then don't pack */
1852 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1854 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1855 if (IN_FARSPACE (SPEC_OCLS (etype)))
1858 /* found it we need to remove it from the
1860 for (sic = dic; sic != ic; sic = sic->next)
1861 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1863 IC_RIGHT (ic)->operand.symOperand =
1864 IC_RIGHT (dic)->operand.symOperand;
1865 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1867 remiCodeFromeBBlock (ebp, dic);
1868 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1875 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1878 /*-----------------------------------------------------------------*/
1879 /* packRegsForOneuse : - will reduce some registers for single Use */
1880 /*-----------------------------------------------------------------*/
1882 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1886 /* I can't figure out how to make this safe yet. */
1887 if ((int)ic+(int)op+(int)ebp) {
1898 /* if returning a literal then do nothing */
1902 /* only upto 2 bytes since we cannot predict
1903 the usage of b, & acc */
1904 if (getSize (operandType (op)) > (fReturnSizeDS390 - 2))
1907 if (ic->op != RETURN &&
1909 !POINTER_SET (ic) &&
1913 /* this routine will mark the a symbol as used in one
1914 instruction use only && if the defintion is local
1915 (ie. within the basic block) && has only one definition &&
1916 that definiion is either a return value from a
1917 function or does not contain any variables in
1919 uses = bitVectCopy (OP_USES (op));
1920 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1921 if (!bitVectIsZero (uses)) /* has other uses */
1924 /* if it has only one defintion */
1925 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1926 return NULL; /* has more than one definition */
1928 /* get the that definition */
1930 hTabItemWithKey (iCodehTab,
1931 bitVectFirstBit (OP_DEFS (op)))))
1934 /* if that only usage is a cast */
1935 if (dic->op == CAST) {
1936 /* to a bigger type */
1937 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
1938 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
1939 /* than we can not, since we cannot predict the usage of b & acc */
1944 /* found the definition now check if it is local */
1945 if (dic->seq < ebp->fSeq ||
1946 dic->seq > ebp->lSeq)
1947 return NULL; /* non-local */
1949 /* now check if it is the return from
1951 if (dic->op == CALL || dic->op == PCALL)
1953 if (ic->op != SEND && ic->op != RETURN)
1955 OP_SYMBOL (op)->ruonly = 1;
1962 /* otherwise check that the definition does
1963 not contain any symbols in far space */
1964 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1965 isOperandInFarSpace (IC_RIGHT (dic)) ||
1966 IS_OP_RUONLY (IC_LEFT (ic)) ||
1967 IS_OP_RUONLY (IC_RIGHT (ic)))
1972 /* if pointer set then make sure the pointer
1974 if (POINTER_SET (dic) &&
1975 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1978 if (POINTER_GET (dic) &&
1979 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1984 /* also make sure the intervenening instructions
1985 don't have any thing in far space */
1986 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1989 /* if there is an intervening function call then no */
1990 if (dic->op == CALL || dic->op == PCALL)
1992 /* if pointer set then make sure the pointer
1994 if (POINTER_SET (dic) &&
1995 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1998 if (POINTER_GET (dic) &&
1999 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2002 /* if address of & the result is remat the okay */
2003 if (dic->op == ADDRESS_OF &&
2004 OP_SYMBOL (IC_RESULT (dic))->remat)
2007 /* if operand has size of three or more & this
2008 operation is a '*','/' or '%' then 'b' may
2010 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2011 getSize (operandType (op)) >= 3)
2014 /* if left or right or result is in far space */
2015 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2016 isOperandInFarSpace (IC_RIGHT (dic)) ||
2017 isOperandInFarSpace (IC_RESULT (dic)) ||
2018 IS_OP_RUONLY (IC_LEFT (dic)) ||
2019 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2020 IS_OP_RUONLY (IC_RESULT (dic)))
2026 OP_SYMBOL (op)->ruonly = 1;
2031 /*-----------------------------------------------------------------*/
2032 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2033 /*-----------------------------------------------------------------*/
2035 isBitwiseOptimizable (iCode * ic)
2037 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2038 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2040 /* bitwise operations are considered optimizable
2041 under the following conditions (Jean-Louis VERN)
2053 if ( IS_LITERAL (rtype) ||
2054 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2060 /*-----------------------------------------------------------------*/
2061 /* packRegsForAccUse - pack registers for acc use */
2062 /*-----------------------------------------------------------------*/
2064 packRegsForAccUse (iCode * ic)
2068 /* if + or - then it has to be one byte result */
2069 if ((ic->op == '+' || ic->op == '-')
2070 && getSize (operandType (IC_RESULT (ic))) > 1)
2073 /* if shift operation make sure right side is not a literal */
2074 if (ic->op == RIGHT_OP &&
2075 (isOperandLiteral (IC_RIGHT (ic)) ||
2076 getSize (operandType (IC_RESULT (ic))) > 1))
2079 if (ic->op == LEFT_OP &&
2080 (isOperandLiteral (IC_RIGHT (ic)) ||
2081 getSize (operandType (IC_RESULT (ic))) > 1))
2084 if (IS_BITWISE_OP (ic) &&
2085 getSize (operandType (IC_RESULT (ic))) > 1)
2089 /* has only one definition */
2090 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2093 /* has only one use */
2094 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2097 /* and the usage immediately follows this iCode */
2098 if (!(uic = hTabItemWithKey (iCodehTab,
2099 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2102 if (ic->next != uic)
2105 /* if it is a conditional branch then we definitely can */
2109 if (uic->op == JUMPTABLE)
2112 /* if the usage is not is an assignment
2113 or an arithmetic / bitwise / shift operation then not */
2114 if (POINTER_SET (uic) &&
2115 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2118 if (uic->op != '=' &&
2119 !IS_ARITHMETIC_OP (uic) &&
2120 !IS_BITWISE_OP (uic) &&
2121 uic->op != LEFT_OP &&
2122 uic->op != RIGHT_OP)
2125 /* if used in ^ operation then make sure right is not a
2127 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2130 /* if shift operation make sure right side is not a literal */
2131 if (uic->op == RIGHT_OP &&
2132 (isOperandLiteral (IC_RIGHT (uic)) ||
2133 getSize (operandType (IC_RESULT (uic))) > 1))
2136 if (uic->op == LEFT_OP &&
2137 (isOperandLiteral (IC_RIGHT (uic)) ||
2138 getSize (operandType (IC_RESULT (uic))) > 1))
2141 /* make sure that the result of this icode is not on the
2142 stack, since acc is used to compute stack offset */
2143 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2144 OP_SYMBOL (IC_RESULT (uic))->onStack)
2147 /* if either one of them in far space then we cannot */
2148 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2149 isOperandInFarSpace (IC_LEFT (uic))) ||
2150 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2151 isOperandInFarSpace (IC_RIGHT (uic))))
2154 /* if the usage has only one operand then we can */
2155 if (IC_LEFT (uic) == NULL ||
2156 IC_RIGHT (uic) == NULL)
2159 /* make sure this is on the left side if not
2160 a '+' since '+' is commutative */
2161 if (ic->op != '+' &&
2162 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2166 // this is too dangerous and need further restrictions
2169 /* if one of them is a literal then we can */
2170 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2171 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2173 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2178 /* if the other one is not on stack then we can */
2179 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2180 (IS_ITEMP (IC_RIGHT (uic)) ||
2181 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2182 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2185 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2186 (IS_ITEMP (IC_LEFT (uic)) ||
2187 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2188 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2194 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2199 /*-----------------------------------------------------------------*/
2200 /* packForPush - hueristics to reduce iCode for pushing */
2201 /*-----------------------------------------------------------------*/
2203 packForPush (iCode * ic, eBBlock * ebp)
2208 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2211 /* must have only definition & one usage */
2212 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2213 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2216 /* find the definition */
2217 if (!(dic = hTabItemWithKey (iCodehTab,
2218 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2221 if (dic->op != '=' || POINTER_SET (dic))
2224 /* make sure the right side does not have any definitions
2226 dbv = OP_DEFS(IC_RIGHT(dic));
2227 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2228 if (bitVectBitValue(dbv,lic->key)) return ;
2230 /* make sure they have the same type */
2232 sym_link *itype=operandType(IC_LEFT(ic));
2233 sym_link *ditype=operandType(IC_RIGHT(dic));
2235 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2236 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2239 /* extend the live range of replaced operand if needed */
2240 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2241 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2243 /* we now we know that it has one & only one def & use
2244 and the that the definition is an assignment */
2245 IC_LEFT (ic) = IC_RIGHT (dic);
2247 remiCodeFromeBBlock (ebp, dic);
2248 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2251 /*-----------------------------------------------------------------*/
2252 /* packRegisters - does some transformations to reduce register */
2254 /*-----------------------------------------------------------------*/
2256 packRegisters (eBBlock * ebp)
2266 /* look for assignments of the form */
2267 /* iTempNN = TRueSym (someoperation) SomeOperand */
2269 /* TrueSym := iTempNN:1 */
2270 for (ic = ebp->sch; ic; ic = ic->next)
2274 /* find assignment of the form TrueSym := iTempNN:1 */
2275 if (ic->op == '=' && !POINTER_SET (ic))
2276 change += packRegsForAssign (ic, ebp);
2283 for (ic = ebp->sch; ic; ic = ic->next)
2286 /* if this is an itemp & result of a address of a true sym
2287 then mark this as rematerialisable */
2288 if (ic->op == ADDRESS_OF &&
2289 IS_ITEMP (IC_RESULT (ic)) &&
2290 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2291 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2292 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2295 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2296 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2297 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2301 /* if straight assignment then carry remat flag if
2302 this is the only definition */
2303 if (ic->op == '=' &&
2304 !POINTER_SET (ic) &&
2305 IS_SYMOP (IC_RIGHT (ic)) &&
2306 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2307 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2308 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2311 OP_SYMBOL (IC_RESULT (ic))->remat =
2312 OP_SYMBOL (IC_RIGHT (ic))->remat;
2313 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2314 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2317 /* if cast to a generic pointer & the pointer being
2318 cast is remat, then we can remat this cast as well */
2319 if (ic->op == CAST &&
2320 IS_SYMOP(IC_RIGHT(ic)) &&
2321 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2322 sym_link *to_type = operandType(IC_LEFT(ic));
2323 sym_link *from_type = operandType(IC_RIGHT(ic));
2324 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2325 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2326 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2327 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2331 /* if this is a +/- operation with a rematerizable
2332 then mark this as rematerializable as well */
2333 if ((ic->op == '+' || ic->op == '-') &&
2334 (IS_SYMOP (IC_LEFT (ic)) &&
2335 IS_ITEMP (IC_RESULT (ic)) &&
2336 OP_SYMBOL (IC_LEFT (ic))->remat &&
2337 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
2338 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2339 IS_OP_LITERAL (IC_RIGHT (ic))))
2342 //int i = operandLitValue(IC_RIGHT(ic));
2343 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2344 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2345 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2348 /* mark the pointer usages */
2349 if (POINTER_SET (ic))
2350 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2352 if (POINTER_GET (ic))
2353 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2357 /* if we are using a symbol on the stack
2358 then we should say ds390_ptrRegReq */
2359 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2360 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2361 OP_SYMBOL (IC_COND (ic))->iaccess);
2362 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2363 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2364 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2367 if (IS_SYMOP (IC_LEFT (ic)))
2368 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2369 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2370 if (IS_SYMOP (IC_RIGHT (ic)))
2371 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2372 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2373 if (IS_SYMOP (IC_RESULT (ic)))
2374 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2375 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2380 /* if the condition of an if instruction
2381 is defined in the previous instruction then
2382 mark the itemp as a conditional */
2383 if ((IS_CONDITIONAL (ic) ||
2384 (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2385 ic->next && ic->next->op == IFX &&
2386 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2387 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2390 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2394 /* if the condition of an if instruction
2395 is defined in the previous instruction and
2396 this is the only usage then
2397 mark the itemp as a conditional */
2398 if ((IS_CONDITIONAL (ic) ||
2399 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2400 ic->next && ic->next->op == IFX &&
2401 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2402 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2403 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2405 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2410 /* reduce for support function calls */
2411 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2412 packRegsForSupport (ic, ebp);
2414 /* some cases the redundant moves can
2415 can be eliminated for return statements */
2416 if ((ic->op == RETURN || ic->op == SEND) &&
2417 !isOperandInFarSpace (IC_LEFT (ic)) &&
2419 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2421 /* if pointer set & left has a size more than
2422 one and right is not in far space */
2423 if (POINTER_SET (ic) &&
2424 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2425 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2426 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2427 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2429 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2431 /* if pointer get */
2432 if (POINTER_GET (ic) &&
2433 !isOperandInFarSpace (IC_RESULT (ic)) &&
2434 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2435 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2436 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2438 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2441 /* if this is cast for intergral promotion then
2442 check if only use of the definition of the
2443 operand being casted/ if yes then replace
2444 the result of that arithmetic operation with
2445 this result and get rid of the cast */
2448 sym_link *fromType = operandType (IC_RIGHT (ic));
2449 sym_link *toType = operandType (IC_LEFT (ic));
2451 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2452 getSize (fromType) != getSize (toType) &&
2453 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2456 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2459 if (IS_ARITHMETIC_OP (dic))
2461 IC_RESULT (dic) = IC_RESULT (ic);
2462 remiCodeFromeBBlock (ebp, ic);
2463 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2464 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2468 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2474 /* if the type from and type to are the same
2475 then if this is the only use then packit */
2476 if (compareType (operandType (IC_RIGHT (ic)),
2477 operandType (IC_LEFT (ic))) == 1)
2479 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2482 IC_RESULT (dic) = IC_RESULT (ic);
2483 remiCodeFromeBBlock (ebp, ic);
2484 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2485 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2493 iTempNN := (some variable in farspace) V1
2498 if (ic->op == IPUSH)
2500 packForPush (ic, ebp);
2504 /* pack registers for accumulator use, when the
2505 result of an arithmetic or bit wise operation
2506 has only one use, that use is immediately following
2507 the defintion and the using iCode has only one
2508 operand or has two operands but one is literal &
2509 the result of that operation is not on stack then
2510 we can leave the result of this operation in acc:b
2512 if ((IS_ARITHMETIC_OP (ic)
2513 || IS_CONDITIONAL(ic)
2514 || IS_BITWISE_OP (ic)
2515 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2516 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2518 IS_ITEMP (IC_RESULT (ic)) &&
2519 getSize (operandType (IC_RESULT (ic))) <= 2)
2521 packRegsForAccUse (ic);
2526 /*-----------------------------------------------------------------*/
2527 /* assignRegisters - assigns registers to each live range as need */
2528 /*-----------------------------------------------------------------*/
2530 ds390_assignRegisters (eBBlock ** ebbs, int count)
2535 setToNull ((void *) &_G.funcrUsed);
2536 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2538 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2539 /* change assignments this will remove some
2540 live ranges reducing some register pressure */
2541 for (i = 0; i < count; i++)
2542 packRegisters (ebbs[i]);
2544 if (options.dump_pack)
2545 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2547 /* first determine for each live range the number of
2548 registers & the type of registers required for each */
2551 /* and serially allocate registers */
2552 serialRegAssign (ebbs, count);
2554 /* if stack was extended then tell the user */
2557 /* werror(W_TOOMANY_SPILS,"stack", */
2558 /* _G.stackExtend,currFunc->name,""); */
2564 /* werror(W_TOOMANY_SPILS,"data space", */
2565 /* _G.dataExtend,currFunc->name,""); */
2569 /* after that create the register mask
2570 for each of the instruction */
2571 createRegMask (ebbs, count);
2573 /* redo that offsets for stacked automatic variables */
2574 redoStackOffsets ();
2576 if (options.dump_rassgn)
2577 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2579 /* do the overlaysegment stuff SDCCmem.c */
2580 doOverlays (ebbs, count);
2582 /* now get back the chain */
2583 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2588 /* free up any _G.stackSpil locations allocated */
2589 applyToSet (_G.stackSpil, deallocStackSpil);
2591 setToNull ((void **) &_G.stackSpil);
2592 setToNull ((void **) &_G.spiltSet);
2593 /* mark all registers as free */