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)
384 for (sym = setFirstItem (itmpStack); sym;
385 sym = setNextItem (itmpStack))
387 if (sym->liveTo > fsym->liveFrom)
395 /*-----------------------------------------------------------------*/
396 /* isFree - will return 1 if the a free spil location is found */
397 /*-----------------------------------------------------------------*/
402 V_ARG (symbol **, sloc);
403 V_ARG (symbol *, fsym);
405 /* if already found */
409 /* if it is free && and the itmp assigned to
410 this does not have any overlapping live ranges
411 with the one currently being assigned and
412 the size can be accomodated */
414 noOverLap (sym->usl.itmpStack, fsym) &&
415 getSize (sym->type) >= getSize (fsym->type))
424 /*-----------------------------------------------------------------*/
425 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
426 /*-----------------------------------------------------------------*/
428 spillLRWithPtrReg (symbol * forSym)
434 if (!_G.regAssigned ||
435 bitVectIsZero (_G.regAssigned))
438 r0 = ds390_regWithIdx (R0_IDX);
439 r1 = ds390_regWithIdx (R1_IDX);
441 /* for all live ranges */
442 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
443 lrsym = hTabNextItem (liveRanges, &k))
447 /* if no registers assigned to it or
449 /* if it does not overlap with this then
450 not need to spill it */
452 if (lrsym->isspilt || !lrsym->nRegs ||
453 (lrsym->liveTo < forSym->liveFrom))
456 /* go thru the registers : if it is either
457 r0 or r1 then spil it */
458 for (j = 0; j < lrsym->nRegs; j++)
459 if (lrsym->regs[j] == r0 ||
460 lrsym->regs[j] == r1)
469 /*-----------------------------------------------------------------*/
470 /* createStackSpil - create a location on the stack to spil */
471 /*-----------------------------------------------------------------*/
473 createStackSpil (symbol * sym)
476 int useXstack, model, noOverlay;
480 /* first go try and find a free one that is already
481 existing on the stack */
482 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
484 /* found a free one : just update & return */
485 sym->usl.spillLoc = sloc;
488 addSetHead (&sloc->usl.itmpStack, sym);
492 /* could not then have to create one , this is the hard part
493 we need to allocate this on the stack : this is really a
494 hack!! but cannot think of anything better at this time */
496 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
498 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
503 sloc = newiTemp (slocBuffer);
505 /* set the type to the spilling symbol */
506 sloc->type = copyLinkChain (sym->type);
507 sloc->etype = getSpec (sloc->type);
508 SPEC_SCLS (sloc->etype) = options.model ? S_XDATA : S_DATA;
509 SPEC_EXTR (sloc->etype) = 0;
511 /* we don't allow it to be allocated`
512 onto the external stack since : so we
513 temporarily turn it off ; we also
514 turn off memory model to prevent
515 the spil from going to the external storage
516 and turn off overlaying
519 useXstack = options.useXstack;
520 model = options.model;
521 noOverlay = options.noOverlay;
522 options.noOverlay = 1;
524 /* options.model = options.useXstack = 0; */
528 options.useXstack = useXstack;
529 options.model = model;
530 options.noOverlay = noOverlay;
531 sloc->isref = 1; /* to prevent compiler warning */
533 /* if it is on the stack then update the stack */
534 if (IN_STACK (sloc->etype))
536 currFunc->stack += getSize (sloc->type);
537 _G.stackExtend += getSize (sloc->type);
540 _G.dataExtend += getSize (sloc->type);
542 /* add it to the _G.stackSpil set */
543 addSetHead (&_G.stackSpil, sloc);
544 sym->usl.spillLoc = sloc;
547 /* add it to the set of itempStack set
548 of the spill location */
549 addSetHead (&sloc->usl.itmpStack, sym);
553 /*-----------------------------------------------------------------*/
554 /* isSpiltOnStack - returns true if the spil location is on stack */
555 /*-----------------------------------------------------------------*/
557 isSpiltOnStack (symbol * sym)
567 /* if (sym->_G.stackSpil) */
570 if (!sym->usl.spillLoc)
573 etype = getSpec (sym->usl.spillLoc->type);
574 if (IN_STACK (etype))
580 /*-----------------------------------------------------------------*/
581 /* spillThis - spils a specific operand */
582 /*-----------------------------------------------------------------*/
584 spillThis (symbol * sym)
587 /* if this is rematerializable or has a spillLocation
588 we are okay, else we need to create a spillLocation
590 if (!(sym->remat || sym->usl.spillLoc))
591 createStackSpil (sym);
594 /* mark it has spilt & put it in the spilt set */
596 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
598 bitVectUnSetBit (_G.regAssigned, sym->key);
600 for (i = 0; i < sym->nRegs; i++)
604 freeReg (sym->regs[i]);
608 /* if spilt on stack then free up r0 & r1
609 if they could have been assigned to some
611 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
614 spillLRWithPtrReg (sym);
617 if (sym->usl.spillLoc && !sym->remat)
618 sym->usl.spillLoc->allocreq = 1;
622 /*-----------------------------------------------------------------*/
623 /* selectSpil - select a iTemp to spil : rather a simple procedure */
624 /*-----------------------------------------------------------------*/
626 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
628 bitVect *lrcs = NULL;
632 /* get the spillable live ranges */
633 lrcs = computeSpillable (ic);
635 /* get all live ranges that are rematerizable */
636 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
639 /* return the least used of these */
640 return leastUsedLR (selectS);
643 /* get live ranges with spillLocations in direct space */
644 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
646 sym = leastUsedLR (selectS);
647 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
648 sym->usl.spillLoc->rname :
649 sym->usl.spillLoc->name));
651 /* mark it as allocation required */
652 sym->usl.spillLoc->allocreq = 1;
656 /* if the symbol is local to the block then */
657 if (forSym->liveTo < ebp->lSeq)
660 /* check if there are any live ranges allocated
661 to registers that are not used in this block */
662 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
664 sym = leastUsedLR (selectS);
665 /* if this is not rematerializable */
674 /* check if there are any live ranges that not
675 used in the remainder of the block */
676 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
678 sym = leastUsedLR (selectS);
691 /* find live ranges with spillocation && not used as pointers */
692 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
695 sym = leastUsedLR (selectS);
696 /* mark this as allocation required */
697 sym->usl.spillLoc->allocreq = 1;
701 /* find live ranges with spillocation */
702 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
705 sym = leastUsedLR (selectS);
706 sym->usl.spillLoc->allocreq = 1;
710 /* couldn't find then we need to create a spil
711 location on the stack , for which one? the least
713 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
716 /* return a created spil location */
717 sym = createStackSpil (leastUsedLR (selectS));
718 sym->usl.spillLoc->allocreq = 1;
722 /* this is an extreme situation we will spill
723 this one : happens very rarely but it does happen */
729 /*-----------------------------------------------------------------*/
730 /* spilSomething - spil some variable & mark registers as free */
731 /*-----------------------------------------------------------------*/
733 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
738 /* get something we can spil */
739 ssym = selectSpil (ic, ebp, forSym);
741 /* mark it as spilt */
743 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
745 /* mark it as not register assigned &
746 take it away from the set */
747 bitVectUnSetBit (_G.regAssigned, ssym->key);
749 /* mark the registers as free */
750 for (i = 0; i < ssym->nRegs; i++)
752 freeReg (ssym->regs[i]);
754 /* if spilt on stack then free up r0 & r1
755 if they could have been assigned to as gprs */
756 if (!ds390_ptrRegReq && isSpiltOnStack (ssym))
759 spillLRWithPtrReg (ssym);
762 /* if this was a block level spil then insert push & pop
763 at the start & end of block respectively */
766 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
767 /* add push to the start of the block */
768 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
769 ebp->sch->next : ebp->sch));
770 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
771 /* add pop to the end of the block */
772 addiCodeToeBBlock (ebp, nic, NULL);
775 /* if spilt because not used in the remainder of the
776 block then add a push before this instruction and
777 a pop at the end of the block */
778 if (ssym->remainSpil)
781 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
782 /* add push just before this instruction */
783 addiCodeToeBBlock (ebp, nic, ic);
785 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
786 /* add pop to the end of the block */
787 addiCodeToeBBlock (ebp, nic, NULL);
796 /*-----------------------------------------------------------------*/
797 /* getRegPtr - will try for PTR if not a GPR type if not spil */
798 /*-----------------------------------------------------------------*/
800 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
805 /* try for a ptr type */
806 if ((reg = allocReg (REG_PTR)))
809 /* try for gpr type */
810 if ((reg = allocReg (REG_GPR)))
813 /* we have to spil */
814 if (!spilSomething (ic, ebp, sym))
817 /* this looks like an infinite loop but
818 in really selectSpil will abort */
822 /*-----------------------------------------------------------------*/
823 /* getRegGpr - will try for GPR if not spil */
824 /*-----------------------------------------------------------------*/
826 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
831 /* try for gpr type */
832 if ((reg = allocReg (REG_GPR)))
835 if (!ds390_ptrRegReq)
836 if ((reg = allocReg (REG_PTR)))
839 /* we have to spil */
840 if (!spilSomething (ic, ebp, sym))
843 /* this looks like an infinite loop but
844 in really selectSpil will abort */
848 /*-----------------------------------------------------------------*/
849 /* symHasReg - symbol has a given register */
850 /*-----------------------------------------------------------------*/
852 symHasReg (symbol * sym, regs * reg)
856 for (i = 0; i < sym->nRegs; i++)
857 if (sym->regs[i] == reg)
863 /*-----------------------------------------------------------------*/
864 /* deassignLRs - check the live to and if they have registers & are */
865 /* not spilt then free up the registers */
866 /*-----------------------------------------------------------------*/
868 deassignLRs (iCode * ic, eBBlock * ebp)
874 for (sym = hTabFirstItem (liveRanges, &k); sym;
875 sym = hTabNextItem (liveRanges, &k))
879 /* if it does not end here */
880 if (sym->liveTo > ic->seq)
883 /* if it was spilt on stack then we can
884 mark the stack spil location as free */
889 sym->usl.spillLoc->isFree = 1;
895 if (!bitVectBitValue (_G.regAssigned, sym->key))
898 /* special case check if this is an IFX &
899 the privious one was a pop and the
900 previous one was not spilt then keep track
902 if (ic->op == IFX && ic->prev &&
903 ic->prev->op == IPOP &&
904 !ic->prev->parmPush &&
905 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
906 psym = OP_SYMBOL (IC_LEFT (ic->prev));
912 bitVectUnSetBit (_G.regAssigned, sym->key);
914 /* if the result of this one needs registers
915 and does not have it then assign it right
917 if (IC_RESULT (ic) &&
918 !(SKIP_IC2 (ic) || /* not a special icode */
919 ic->op == JUMPTABLE ||
925 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
926 result->liveTo > ic->seq && /* and will live beyond this */
927 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
928 result->regType == sym->regType && /* same register types */
929 result->nRegs && /* which needs registers */
930 !result->isspilt && /* and does not already have them */
932 !bitVectBitValue (_G.regAssigned, result->key) &&
933 /* the number of free regs + number of regs in this LR
934 can accomodate the what result Needs */
935 ((nfreeRegsType (result->regType) +
936 sym->nRegs) >= result->nRegs)
940 for (i = 0; i < result->nRegs; i++)
942 result->regs[i] = sym->regs[i];
944 result->regs[i] = getRegGpr (ic, ebp, result);
946 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
950 /* free the remaining */
951 for (; i < sym->nRegs; i++)
955 if (!symHasReg (psym, sym->regs[i]))
956 freeReg (sym->regs[i]);
959 freeReg (sym->regs[i]);
966 /*-----------------------------------------------------------------*/
967 /* reassignLR - reassign this to registers */
968 /*-----------------------------------------------------------------*/
970 reassignLR (operand * op)
972 symbol *sym = OP_SYMBOL (op);
975 /* not spilt any more */
976 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
977 bitVectUnSetBit (_G.spiltSet, sym->key);
979 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
983 for (i = 0; i < sym->nRegs; i++)
984 sym->regs[i]->isFree = 0;
987 /*-----------------------------------------------------------------*/
988 /* willCauseSpill - determines if allocating will cause a spill */
989 /*-----------------------------------------------------------------*/
991 willCauseSpill (int nr, int rt)
993 /* first check if there are any avlb registers
994 of te type required */
997 /* special case for pointer type
998 if pointer type not avlb then
999 check for type gpr */
1000 if (nFreeRegs (rt) >= nr)
1002 if (nFreeRegs (REG_GPR) >= nr)
1007 if (ds390_ptrRegReq)
1009 if (nFreeRegs (rt) >= nr)
1014 if (nFreeRegs (REG_PTR) +
1015 nFreeRegs (REG_GPR) >= nr)
1020 /* it will cause a spil */
1024 /*-----------------------------------------------------------------*/
1025 /* positionRegs - the allocator can allocate same registers to res- */
1026 /* ult and operand, if this happens make sure they are in the same */
1027 /* position as the operand otherwise chaos results */
1028 /*-----------------------------------------------------------------*/
1030 positionRegs (symbol * result, symbol * opsym, int lineno)
1032 int count = min (result->nRegs, opsym->nRegs);
1033 int i, j = 0, shared = 0;
1035 /* if the result has been spilt then cannot share */
1040 /* first make sure that they actually share */
1041 for (i = 0; i < count; i++)
1043 for (j = 0; j < count; j++)
1045 if (result->regs[i] == opsym->regs[j] && i != j)
1055 regs *tmp = result->regs[i];
1056 result->regs[i] = result->regs[j];
1057 result->regs[j] = tmp;
1062 /*-----------------------------------------------------------------*/
1063 /* serialRegAssign - serially allocate registers to the variables */
1064 /*-----------------------------------------------------------------*/
1066 serialRegAssign (eBBlock ** ebbs, int count)
1070 /* for all blocks */
1071 for (i = 0; i < count; i++)
1076 if (ebbs[i]->noPath &&
1077 (ebbs[i]->entryLabel != entryLabel &&
1078 ebbs[i]->entryLabel != returnLabel))
1081 /* of all instructions do */
1082 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1085 /* if this is an ipop that means some live
1086 range will have to be assigned again */
1088 reassignLR (IC_LEFT (ic));
1090 /* if result is present && is a true symbol */
1091 if (IC_RESULT (ic) && ic->op != IFX &&
1092 IS_TRUE_SYMOP (IC_RESULT (ic)))
1093 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1095 /* take away registers from live
1096 ranges that end at this instruction */
1097 deassignLRs (ic, ebbs[i]);
1099 /* some don't need registers */
1100 if (SKIP_IC2 (ic) ||
1101 ic->op == JUMPTABLE ||
1105 (IC_RESULT (ic) && POINTER_SET (ic)))
1108 /* now we need to allocate registers
1109 only for the result */
1112 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1118 /* if it does not need or is spilt
1119 or is already assigned to registers
1120 or will not live beyond this instructions */
1123 bitVectBitValue (_G.regAssigned, sym->key) ||
1124 sym->liveTo <= ic->seq)
1127 /* if some liverange has been spilt at the block level
1128 and this one live beyond this block then spil this
1130 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1135 /* if trying to allocate this will cause
1136 a spill and there is nothing to spill
1137 or this one is rematerializable then
1139 willCS = willCauseSpill (sym->nRegs, sym->regType);
1140 spillable = computeSpillable (ic);
1142 (willCS && bitVectIsZero (spillable)))
1150 /* if it has a spillocation & is used less than
1151 all other live ranges then spill this */
1152 if (willCS && sym->usl.spillLoc)
1156 leastUsedLR (liveRangesWith (spillable,
1161 leastUsed->used > sym->used)
1168 /* if we need ptr regs for the right side
1170 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1171 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1177 /* else we assign registers to it */
1178 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1180 for (j = 0; j < sym->nRegs; j++)
1182 if (sym->regType == REG_PTR)
1183 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1185 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1187 /* if the allocation falied which means
1188 this was spilt then break */
1192 /* if it shares registers with operands make sure
1193 that they are in the same position */
1194 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1195 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1196 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1197 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1198 /* do the same for the right operand */
1199 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1200 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1201 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1202 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1215 /*-----------------------------------------------------------------*/
1216 /* rUmaskForOp :- returns register mask for an operand */
1217 /*-----------------------------------------------------------------*/
1219 rUmaskForOp (operand * op)
1225 /* only temporaries are assigned registers */
1229 sym = OP_SYMBOL (op);
1231 /* if spilt or no registers assigned to it
1233 if (sym->isspilt || !sym->nRegs)
1236 rumask = newBitVect (ds390_nRegs);
1238 for (j = 0; j < sym->nRegs; j++)
1240 rumask = bitVectSetBit (rumask,
1241 sym->regs[j]->rIdx);
1247 /*-----------------------------------------------------------------*/
1248 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1249 /*-----------------------------------------------------------------*/
1251 regsUsedIniCode (iCode * ic)
1253 bitVect *rmask = newBitVect (ds390_nRegs);
1255 /* do the special cases first */
1258 rmask = bitVectUnion (rmask,
1259 rUmaskForOp (IC_COND (ic)));
1263 /* for the jumptable */
1264 if (ic->op == JUMPTABLE)
1266 rmask = bitVectUnion (rmask,
1267 rUmaskForOp (IC_JTCOND (ic)));
1272 /* of all other cases */
1274 rmask = bitVectUnion (rmask,
1275 rUmaskForOp (IC_LEFT (ic)));
1279 rmask = bitVectUnion (rmask,
1280 rUmaskForOp (IC_RIGHT (ic)));
1283 rmask = bitVectUnion (rmask,
1284 rUmaskForOp (IC_RESULT (ic)));
1290 /*-----------------------------------------------------------------*/
1291 /* createRegMask - for each instruction will determine the regsUsed */
1292 /*-----------------------------------------------------------------*/
1294 createRegMask (eBBlock ** ebbs, int count)
1298 /* for all blocks */
1299 for (i = 0; i < count; i++)
1303 if (ebbs[i]->noPath &&
1304 (ebbs[i]->entryLabel != entryLabel &&
1305 ebbs[i]->entryLabel != returnLabel))
1308 /* for all instructions */
1309 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1314 if (SKIP_IC2 (ic) || !ic->rlive)
1317 /* first mark the registers used in this
1319 ic->rUsed = regsUsedIniCode (ic);
1320 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1322 /* now create the register mask for those
1323 registers that are in use : this is a
1324 super set of ic->rUsed */
1325 ic->rMask = newBitVect (ds390_nRegs + 1);
1327 /* for all live Ranges alive at this point */
1328 for (j = 1; j < ic->rlive->size; j++)
1333 /* if not alive then continue */
1334 if (!bitVectBitValue (ic->rlive, j))
1337 /* find the live range we are interested in */
1338 if (!(sym = hTabItemWithKey (liveRanges, j)))
1340 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1341 "createRegMask cannot find live range");
1345 /* if no register assigned to it */
1346 if (!sym->nRegs || sym->isspilt)
1349 /* for all the registers allocated to it */
1350 for (k = 0; k < sym->nRegs; k++)
1353 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1359 /*-----------------------------------------------------------------*/
1360 /* rematStr - returns the rematerialized string for a remat var */
1361 /*-----------------------------------------------------------------*/
1363 rematStr (symbol * sym)
1366 iCode *ic = sym->rematiCode;
1371 /* if plus or minus print the right hand side */
1372 if (ic->op == '+' || ic->op == '-')
1374 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1377 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1381 /* we reached the end */
1382 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1389 /*-----------------------------------------------------------------*/
1390 /* regTypeNum - computes the type & number of registers required */
1391 /*-----------------------------------------------------------------*/
1399 /* for each live range do */
1400 for (sym = hTabFirstItem (liveRanges, &k); sym;
1401 sym = hTabNextItem (liveRanges, &k))
1404 /* if used zero times then no registers needed */
1405 if ((sym->liveTo - sym->liveFrom) == 0)
1409 /* if the live range is a temporary */
1413 /* if the type is marked as a conditional */
1414 if (sym->regType == REG_CND)
1417 /* if used in return only then we don't
1419 if (sym->ruonly || sym->accuse)
1421 if (IS_AGGREGATE (sym->type) || sym->isptr)
1422 sym->type = aggrToPtr (sym->type, FALSE);
1426 /* if the symbol has only one definition &
1427 that definition is a get_pointer and the
1428 pointer we are getting is rematerializable and
1431 if (bitVectnBitsOn (sym->defs) == 1 &&
1432 (ic = hTabItemWithKey (iCodehTab,
1433 bitVectFirstBit (sym->defs))) &&
1435 !IS_BITVAR (sym->etype))
1439 /* if remat in data space */
1440 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1442 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1445 /* create a psuedo symbol & force a spil */
1446 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1447 psym->type = sym->type;
1448 psym->etype = sym->etype;
1449 strcpy (psym->rname, psym->name);
1451 sym->usl.spillLoc = psym;
1455 /* if in data space or idata space then try to
1456 allocate pointer register */
1460 /* if not then we require registers */
1461 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1462 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1463 getSize (sym->type));
1467 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1468 printTypeChain (sym->type, stderr);
1469 fprintf (stderr, "\n");
1472 /* determine the type of register required */
1473 if (sym->nRegs == 1 &&
1474 IS_PTR (sym->type) &&
1476 sym->regType = REG_PTR;
1478 sym->regType = REG_GPR;
1482 /* for the first run we don't provide */
1483 /* registers for true symbols we will */
1484 /* see how things go */
1490 /*-----------------------------------------------------------------*/
1491 /* freeAllRegs - mark all registers as free */
1492 /*-----------------------------------------------------------------*/
1498 for (i = 0; i < ds390_nRegs; i++)
1499 regs390[i].isFree = 1;
1502 /*-----------------------------------------------------------------*/
1503 /* deallocStackSpil - this will set the stack pointer back */
1504 /*-----------------------------------------------------------------*/
1506 DEFSETFUNC (deallocStackSpil)
1514 /*-----------------------------------------------------------------*/
1515 /* farSpacePackable - returns the packable icode for far variables */
1516 /*-----------------------------------------------------------------*/
1518 farSpacePackable (iCode * ic)
1522 /* go thru till we find a definition for the
1523 symbol on the right */
1524 for (dic = ic->prev; dic; dic = dic->prev)
1527 /* if the definition is a call then no */
1528 if ((dic->op == CALL || dic->op == PCALL) &&
1529 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1534 /* if shift by unknown amount then not */
1535 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1536 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1539 /* if pointer get and size > 1 */
1540 if (POINTER_GET (dic) &&
1541 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1544 if (POINTER_SET (dic) &&
1545 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1548 /* if any three is a true symbol in far space */
1549 if (IC_RESULT (dic) &&
1550 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1551 isOperandInFarSpace (IC_RESULT (dic)))
1554 if (IC_RIGHT (dic) &&
1555 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1556 isOperandInFarSpace (IC_RIGHT (dic)) &&
1557 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1560 if (IC_LEFT (dic) &&
1561 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1562 isOperandInFarSpace (IC_LEFT (dic)) &&
1563 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1566 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1568 if ((dic->op == LEFT_OP ||
1569 dic->op == RIGHT_OP ||
1571 IS_OP_LITERAL (IC_RIGHT (dic)))
1581 /*-----------------------------------------------------------------*/
1582 /* packRegsForAssign - register reduction for assignment */
1583 /*-----------------------------------------------------------------*/
1585 packRegsForAssign (iCode * ic, eBBlock * ebp)
1589 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1590 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1591 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1596 /* if the true symbol is defined in far space or on stack
1597 then we should not since this will increase register pressure */
1599 if (isOperandInFarSpace (IC_RESULT (ic)))
1601 if ((dic = farSpacePackable (ic)))
1607 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1612 /* find the definition of iTempNN scanning backwards if we find a
1613 a use of the true symbol in before we find the definition then
1615 for (dic = ic->prev; dic; dic = dic->prev)
1618 /* if there is a function call and this is
1619 a parameter & not my parameter then don't pack it */
1620 if ((dic->op == CALL || dic->op == PCALL) &&
1621 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1622 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1631 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1632 IS_OP_VOLATILE (IC_RESULT (dic)))
1638 if (IS_SYMOP (IC_RESULT (dic)) &&
1639 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1641 if (POINTER_SET (dic))
1647 if (IS_SYMOP (IC_RIGHT (dic)) &&
1648 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1649 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1655 if (IS_SYMOP (IC_LEFT (dic)) &&
1656 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1657 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1663 if (POINTER_SET (dic) &&
1664 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1672 return 0; /* did not find */
1674 /* if the result is on stack or iaccess then it must be
1675 the same atleast one of the operands */
1676 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1677 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1680 /* the operation has only one symbol
1681 operator then we can pack */
1682 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1683 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1686 if (!((IC_LEFT (dic) &&
1687 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1689 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1693 /* found the definition */
1694 /* replace the result with the result of */
1695 /* this assignment and remove this assignment */
1696 IC_RESULT (dic) = IC_RESULT (ic);
1698 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1700 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1702 /* delete from liverange table also
1703 delete from all the points inbetween and the new
1705 for (sic = dic; sic != ic; sic = sic->next)
1707 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1708 if (IS_ITEMP (IC_RESULT (dic)))
1709 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1712 remiCodeFromeBBlock (ebp, ic);
1713 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1714 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1719 /*-----------------------------------------------------------------*/
1720 /* findAssignToSym : scanning backwards looks for first assig found */
1721 /*-----------------------------------------------------------------*/
1723 findAssignToSym (operand * op, iCode * ic)
1727 for (dic = ic->prev; dic; dic = dic->prev)
1730 /* if definition by assignment */
1731 if (dic->op == '=' &&
1732 !POINTER_SET (dic) &&
1733 IC_RESULT (dic)->key == op->key
1734 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1738 /* we are interested only if defined in far space */
1739 /* or in stack space in case of + & - */
1741 /* if assigned to a non-symbol then return
1743 if (!IS_SYMOP (IC_RIGHT (dic)))
1746 /* if the symbol is in far space then
1748 if (isOperandInFarSpace (IC_RIGHT (dic)))
1751 /* for + & - operations make sure that
1752 if it is on the stack it is the same
1753 as one of the three operands */
1754 if ((ic->op == '+' || ic->op == '-') &&
1755 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1758 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1759 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1760 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1768 /* if we find an usage then we cannot delete it */
1769 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1772 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1775 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1779 /* now make sure that the right side of dic
1780 is not defined between ic & dic */
1783 iCode *sic = dic->next;
1785 for (; sic != ic; sic = sic->next)
1786 if (IC_RESULT (sic) &&
1787 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1796 /*-----------------------------------------------------------------*/
1797 /* packRegsForSupport :- reduce some registers for support calls */
1798 /*-----------------------------------------------------------------*/
1800 packRegsForSupport (iCode * ic, eBBlock * ebp)
1803 /* for the left & right operand :- look to see if the
1804 left was assigned a true symbol in far space in that
1805 case replace them */
1806 if (IS_ITEMP (IC_LEFT (ic)) &&
1807 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1809 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1815 /* found it we need to remove it from the
1817 for (sic = dic; sic != ic; sic = sic->next)
1818 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1820 IC_LEFT (ic)->operand.symOperand =
1821 IC_RIGHT (dic)->operand.symOperand;
1822 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1823 remiCodeFromeBBlock (ebp, dic);
1824 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1828 /* do the same for the right operand */
1831 IS_ITEMP (IC_RIGHT (ic)) &&
1832 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1834 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1840 /* if this is a subtraction & the result
1841 is a true symbol in far space then don't pack */
1842 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1844 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1845 if (IN_FARSPACE (SPEC_OCLS (etype)))
1848 /* found it we need to remove it from the
1850 for (sic = dic; sic != ic; sic = sic->next)
1851 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1853 IC_RIGHT (ic)->operand.symOperand =
1854 IC_RIGHT (dic)->operand.symOperand;
1855 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1857 remiCodeFromeBBlock (ebp, dic);
1858 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1865 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1868 /*-----------------------------------------------------------------*/
1869 /* packRegsForOneuse : - will reduce some registers for single Use */
1870 /*-----------------------------------------------------------------*/
1872 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1876 /* I can't figure out how to make this safe yet. */
1877 if ((int)ic+(int)op+(int)ebp) {
1888 /* if returning a literal then do nothing */
1892 /* only upto 2 bytes since we cannot predict
1893 the usage of b, & acc */
1894 if (getSize (operandType (op)) > (fReturnSizeDS390 - 2) &&
1897 !POINTER_SET (ic) &&
1901 /* this routine will mark the a symbol as used in one
1902 instruction use only && if the defintion is local
1903 (ie. within the basic block) && has only one definition &&
1904 that definiion is either a return value from a
1905 function or does not contain any variables in
1907 uses = bitVectCopy (OP_USES (op));
1908 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1909 if (!bitVectIsZero (uses)) /* has other uses */
1912 /* if it has only one defintion */
1913 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1914 return NULL; /* has more than one definition */
1916 /* get the that definition */
1918 hTabItemWithKey (iCodehTab,
1919 bitVectFirstBit (OP_DEFS (op)))))
1922 /* found the definition now check if it is local */
1923 if (dic->seq < ebp->fSeq ||
1924 dic->seq > ebp->lSeq)
1925 return NULL; /* non-local */
1927 /* now check if it is the return from
1929 if (dic->op == CALL || dic->op == PCALL)
1931 if (ic->op != SEND && ic->op != RETURN)
1933 OP_SYMBOL (op)->ruonly = 1;
1940 /* otherwise check that the definition does
1941 not contain any symbols in far space */
1942 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1943 isOperandInFarSpace (IC_RIGHT (dic)) ||
1944 IS_OP_RUONLY (IC_LEFT (ic)) ||
1945 IS_OP_RUONLY (IC_RIGHT (ic)))
1950 /* if pointer set then make sure the pointer
1952 if (POINTER_SET (dic) &&
1953 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1956 if (POINTER_GET (dic) &&
1957 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1962 /* also make sure the intervenening instructions
1963 don't have any thing in far space */
1964 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1967 /* if there is an intervening function call then no */
1968 if (dic->op == CALL || dic->op == PCALL)
1970 /* if pointer set then make sure the pointer
1972 if (POINTER_SET (dic) &&
1973 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1976 if (POINTER_GET (dic) &&
1977 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1980 /* if address of & the result is remat the okay */
1981 if (dic->op == ADDRESS_OF &&
1982 OP_SYMBOL (IC_RESULT (dic))->remat)
1985 /* if operand has size of three or more & this
1986 operation is a '*','/' or '%' then 'b' may
1988 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
1989 getSize (operandType (op)) >= 3)
1992 /* if left or right or result is in far space */
1993 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1994 isOperandInFarSpace (IC_RIGHT (dic)) ||
1995 isOperandInFarSpace (IC_RESULT (dic)) ||
1996 IS_OP_RUONLY (IC_LEFT (dic)) ||
1997 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1998 IS_OP_RUONLY (IC_RESULT (dic)))
2004 OP_SYMBOL (op)->ruonly = 1;
2009 /*-----------------------------------------------------------------*/
2010 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2011 /*-----------------------------------------------------------------*/
2013 isBitwiseOptimizable (iCode * ic)
2015 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2016 // jwk sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2018 /* bitwise operations are considered optimizable
2019 under the following conditions (Jean-Louis VERN)
2021 x & lit <== jwk: should be x && lit
2026 x ^ lit <== jwk: should be x ^^ lit
2027 x | lit <== jwk: should be x || lit
2031 if ( /* jwk IS_LITERAL (rtype) || */
2032 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2038 /*-----------------------------------------------------------------*/
2039 /* packRegsForAccUse - pack registers for acc use */
2040 /*-----------------------------------------------------------------*/
2042 packRegsForAccUse (iCode * ic)
2046 /* if + or - then it has to be one byte result */
2047 if ((ic->op == '+' || ic->op == '-')
2048 && getSize (operandType (IC_RESULT (ic))) > 1)
2051 /* if shift operation make sure right side is not a literal */
2052 if (ic->op == RIGHT_OP &&
2053 (isOperandLiteral (IC_RIGHT (ic)) ||
2054 getSize (operandType (IC_RESULT (ic))) > 1))
2057 if (ic->op == LEFT_OP &&
2058 (isOperandLiteral (IC_RIGHT (ic)) ||
2059 getSize (operandType (IC_RESULT (ic))) > 1))
2062 if (IS_BITWISE_OP (ic) &&
2063 getSize (operandType (IC_RESULT (ic))) > 1)
2067 /* has only one definition */
2068 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2071 /* has only one use */
2072 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2075 /* and the usage immediately follows this iCode */
2076 if (!(uic = hTabItemWithKey (iCodehTab,
2077 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2080 if (ic->next != uic)
2083 /* if it is a conditional branch then we definitely can */
2087 if (uic->op == JUMPTABLE)
2090 /* if the usage is not is an assignment
2091 or an arithmetic / bitwise / shift operation then not */
2092 if (POINTER_SET (uic) &&
2093 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2096 if (uic->op != '=' &&
2097 !IS_ARITHMETIC_OP (uic) &&
2098 !IS_BITWISE_OP (uic) &&
2099 uic->op != LEFT_OP &&
2100 uic->op != RIGHT_OP)
2103 /* if used in ^ operation then make sure right is not a
2105 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2108 /* if shift operation make sure right side is not a literal */
2109 if (uic->op == RIGHT_OP &&
2110 (isOperandLiteral (IC_RIGHT (uic)) ||
2111 getSize (operandType (IC_RESULT (uic))) > 1))
2114 if (uic->op == LEFT_OP &&
2115 (isOperandLiteral (IC_RIGHT (uic)) ||
2116 getSize (operandType (IC_RESULT (uic))) > 1))
2119 /* make sure that the result of this icode is not on the
2120 stack, since acc is used to compute stack offset */
2121 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2122 OP_SYMBOL (IC_RESULT (uic))->onStack)
2125 /* if either one of them in far space then we cannot */
2126 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2127 isOperandInFarSpace (IC_LEFT (uic))) ||
2128 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2129 isOperandInFarSpace (IC_RIGHT (uic))))
2132 /* if the usage has only one operand then we can */
2133 if (IC_LEFT (uic) == NULL ||
2134 IC_RIGHT (uic) == NULL)
2137 /* make sure this is on the left side if not
2138 a '+' since '+' is commutative */
2139 if (ic->op != '+' &&
2140 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2143 /* if one of them is a literal then we can */
2144 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2145 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2147 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2151 /* if the other one is not on stack then we can */
2152 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2153 (IS_ITEMP (IC_RIGHT (uic)) ||
2154 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2155 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2158 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2159 (IS_ITEMP (IC_LEFT (uic)) ||
2160 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2161 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2167 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2172 /*-----------------------------------------------------------------*/
2173 /* packForPush - hueristics to reduce iCode for pushing */
2174 /*-----------------------------------------------------------------*/
2176 packForPush (iCode * ic, eBBlock * ebp)
2181 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2184 /* must have only definition & one usage */
2185 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2186 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2189 /* find the definition */
2190 if (!(dic = hTabItemWithKey (iCodehTab,
2191 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2194 if (dic->op != '=' || POINTER_SET (dic))
2197 /* make sure the right side does not have any definitions
2199 dbv = OP_DEFS(IC_RIGHT(dic));
2200 for (lic = ic; lic != dic ; lic = lic->prev) {
2201 if (bitVectBitValue(dbv,lic->key)) return ;
2204 /* we now we know that it has one & only one def & use
2205 and the that the definition is an assignment */
2206 IC_LEFT (ic) = IC_RIGHT (dic);
2208 remiCodeFromeBBlock (ebp, dic);
2209 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2212 /*-----------------------------------------------------------------*/
2213 /* packRegisters - does some transformations to reduce register */
2215 /*-----------------------------------------------------------------*/
2217 packRegisters (eBBlock * ebp)
2227 /* look for assignments of the form */
2228 /* iTempNN = TRueSym (someoperation) SomeOperand */
2230 /* TrueSym := iTempNN:1 */
2231 for (ic = ebp->sch; ic; ic = ic->next)
2235 /* find assignment of the form TrueSym := iTempNN:1 */
2236 if (ic->op == '=' && !POINTER_SET (ic))
2237 change += packRegsForAssign (ic, ebp);
2244 for (ic = ebp->sch; ic; ic = ic->next)
2247 /* if this is an itemp & result of a address of a true sym
2248 then mark this as rematerialisable */
2249 if (ic->op == ADDRESS_OF &&
2250 IS_ITEMP (IC_RESULT (ic)) &&
2251 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2252 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2253 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2256 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2257 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2258 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2262 /* if straight assignment then carry remat flag if
2263 this is the only definition */
2264 if (ic->op == '=' &&
2265 !POINTER_SET (ic) &&
2266 IS_SYMOP (IC_RIGHT (ic)) &&
2267 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2268 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2271 OP_SYMBOL (IC_RESULT (ic))->remat =
2272 OP_SYMBOL (IC_RIGHT (ic))->remat;
2273 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2274 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2277 /* if this is a +/- operation with a rematerizable
2278 then mark this as rematerializable as well */
2279 if ((ic->op == '+' || ic->op == '-') &&
2280 (IS_SYMOP (IC_LEFT (ic)) &&
2281 IS_ITEMP (IC_RESULT (ic)) &&
2282 OP_SYMBOL (IC_LEFT (ic))->remat &&
2283 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2284 IS_OP_LITERAL (IC_RIGHT (ic))))
2287 //int i = operandLitValue(IC_RIGHT(ic));
2288 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2289 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2290 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2293 /* mark the pointer usages */
2294 if (POINTER_SET (ic))
2295 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2297 if (POINTER_GET (ic))
2298 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2302 /* if we are using a symbol on the stack
2303 then we should say ds390_ptrRegReq */
2304 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2305 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2306 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2307 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2308 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2309 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2312 if (IS_SYMOP (IC_LEFT (ic)))
2313 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2314 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2315 if (IS_SYMOP (IC_RIGHT (ic)))
2316 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2317 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2318 if (IS_SYMOP (IC_RESULT (ic)))
2319 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2320 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2324 /* if the condition of an if instruction
2325 is defined in the previous instruction then
2326 mark the itemp as a conditional */
2327 if ((IS_CONDITIONAL (ic) ||
2328 (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2329 ic->next && ic->next->op == IFX &&
2330 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2331 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2334 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2338 /* reduce for support function calls */
2339 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2340 packRegsForSupport (ic, ebp);
2342 /* some cases the redundant moves can
2343 can be eliminated for return statements */
2344 if ((ic->op == RETURN || ic->op == SEND) &&
2345 !isOperandInFarSpace (IC_LEFT (ic)) &&
2347 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2349 /* if pointer set & left has a size more than
2350 one and right is not in far space */
2351 if (POINTER_SET (ic) &&
2352 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2353 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2354 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2355 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2357 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2359 /* if pointer get */
2360 if (POINTER_GET (ic) &&
2361 !isOperandInFarSpace (IC_RESULT (ic)) &&
2362 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2363 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2364 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2366 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2369 /* if this is cast for intergral promotion then
2370 check if only use of the definition of the
2371 operand being casted/ if yes then replace
2372 the result of that arithmetic operation with
2373 this result and get rid of the cast */
2376 sym_link *fromType = operandType (IC_RIGHT (ic));
2377 sym_link *toType = operandType (IC_LEFT (ic));
2379 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2380 getSize (fromType) != getSize (toType) &&
2381 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2384 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2387 if (IS_ARITHMETIC_OP (dic))
2389 IC_RESULT (dic) = IC_RESULT (ic);
2390 remiCodeFromeBBlock (ebp, ic);
2391 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2392 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2396 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2402 /* if the type from and type to are the same
2403 then if this is the only use then packit */
2404 if (checkType (operandType (IC_RIGHT (ic)),
2405 operandType (IC_LEFT (ic))) == 1)
2407 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2410 IC_RESULT (dic) = IC_RESULT (ic);
2411 remiCodeFromeBBlock (ebp, ic);
2412 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2413 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2421 iTempNN := (some variable in farspace) V1
2426 if (ic->op == IPUSH)
2428 packForPush (ic, ebp);
2432 /* pack registers for accumulator use, when the
2433 result of an arithmetic or bit wise operation
2434 has only one use, that use is immediately following
2435 the defintion and the using iCode has only one
2436 operand or has two operands but one is literal &
2437 the result of that operation is not on stack then
2438 we can leave the result of this operation in acc:b
2440 if ((IS_ARITHMETIC_OP (ic)
2442 || IS_BITWISE_OP (ic)
2444 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2447 IS_ITEMP (IC_RESULT (ic)) &&
2448 getSize (operandType (IC_RESULT (ic))) <= 2)
2450 packRegsForAccUse (ic);
2455 /*-----------------------------------------------------------------*/
2456 /* assignRegisters - assigns registers to each live range as need */
2457 /*-----------------------------------------------------------------*/
2459 ds390_assignRegisters (eBBlock ** ebbs, int count)
2464 setToNull ((void *) &_G.funcrUsed);
2465 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2466 /* if not register extentions then reduce number
2468 if (options.regExtend)
2473 /* change assignments this will remove some
2474 live ranges reducing some register pressure */
2475 for (i = 0; i < count; i++)
2476 packRegisters (ebbs[i]);
2478 if (options.dump_pack)
2479 dumpEbbsToFileExt (".dumppack", ebbs, count);
2481 /* first determine for each live range the number of
2482 registers & the type of registers required for each */
2485 /* and serially allocate registers */
2486 serialRegAssign (ebbs, count);
2488 /* if stack was extended then tell the user */
2491 /* werror(W_TOOMANY_SPILS,"stack", */
2492 /* _G.stackExtend,currFunc->name,""); */
2498 /* werror(W_TOOMANY_SPILS,"data space", */
2499 /* _G.dataExtend,currFunc->name,""); */
2503 /* after that create the register mask
2504 for each of the instruction */
2505 createRegMask (ebbs, count);
2507 /* redo that offsets for stacked automatic variables */
2508 redoStackOffsets ();
2510 if (options.dump_rassgn)
2511 dumpEbbsToFileExt (".dumprassgn", ebbs, count);
2513 /* do the overlaysegment stuff SDCCmem.c */
2514 doOverlays (ebbs, count);
2516 /* now get back the chain */
2517 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2522 /* free up any _G.stackSpil locations allocated */
2523 applyToSet (_G.stackSpil, deallocStackSpil);
2525 setToNull ((void **) &_G.stackSpil);
2526 setToNull ((void **) &_G.spiltSet);
2527 /* mark all registers as free */