1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
47 bitVect *totRegAssigned; /* final set of LRs that got into registers */
50 bitVect *funcrUsed; /* registers used in a function */
56 /* Shared with gen.c */
57 int ds390_ptrRegReq; /* one byte pointer register required */
63 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
64 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
65 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
66 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
67 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
68 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
69 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
70 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
71 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
72 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
73 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
74 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
75 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
76 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
77 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
78 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
79 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
80 {REG_CND, CND_IDX, REG_GPR, "C", "C", "xreg", 0, 0, 0},
83 static void spillThis (symbol *);
84 static void freeAllRegs ();
85 static iCode * packRegsDPTRuse (operand *);
87 /*-----------------------------------------------------------------*/
88 /* allocReg - allocates register of given type */
89 /*-----------------------------------------------------------------*/
95 for (i = 0; i < ds390_nRegs; i++)
98 /* if type is given as 0 then any
99 free register will do */
103 regs390[i].isFree = 0;
106 bitVectSetBit (currFunc->regsUsed, i);
109 /* other wise look for specific type
111 if (regs390[i].isFree &&
112 regs390[i].type == type)
114 regs390[i].isFree = 0;
117 bitVectSetBit (currFunc->regsUsed, i);
124 /*-----------------------------------------------------------------*/
125 /* ds390_regWithIdx - returns pointer to register wit index number */
126 /*-----------------------------------------------------------------*/
128 ds390_regWithIdx (int idx)
132 for (i = 0; i < ds390_nRegs; i++)
133 if (regs390[i].rIdx == idx)
136 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
137 "regWithIdx not found");
141 /*-----------------------------------------------------------------*/
142 /* freeReg - frees a register */
143 /*-----------------------------------------------------------------*/
150 /*-----------------------------------------------------------------*/
151 /* useReg - marks a register as used */
152 /*-----------------------------------------------------------------*/
160 /*-----------------------------------------------------------------*/
161 /* nFreeRegs - returns number of free registers */
162 /*-----------------------------------------------------------------*/
169 for (i = 0; i < ds390_nRegs; i++)
170 if (regs390[i].isFree && regs390[i].type == type)
175 /*-----------------------------------------------------------------*/
176 /* nfreeRegsType - free registers with type */
177 /*-----------------------------------------------------------------*/
179 nfreeRegsType (int type)
184 if ((nfr = nFreeRegs (type)) == 0)
185 return nFreeRegs (REG_GPR);
188 return nFreeRegs (type);
192 /*-----------------------------------------------------------------*/
193 /* allDefsOutOfRange - all definitions are out of a range */
194 /*-----------------------------------------------------------------*/
196 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
203 for (i = 0; i < defs->size; i++)
207 if (bitVectBitValue (defs, i) &&
208 (ic = hTabItemWithKey (iCodehTab, i)) &&
209 (ic->seq >= fseq && ic->seq <= toseq))
218 /*-----------------------------------------------------------------*/
219 /* isOperandInReg - returns true if operand is currently in regs */
220 /*-----------------------------------------------------------------*/
221 static int isOperandInReg(operand *op)
223 if (!IS_SYMOP(op)) return 0;
225 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
228 /*-----------------------------------------------------------------*/
229 /* computeSpillable - given a point find the spillable live ranges */
230 /*-----------------------------------------------------------------*/
232 computeSpillable (iCode * ic)
236 /* spillable live ranges are those that are live at this
237 point . the following categories need to be subtracted
239 a) - those that are already spilt
240 b) - if being used by this one
241 c) - defined by this one */
243 spillable = bitVectCopy (ic->rlive);
245 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
247 bitVectCplAnd (spillable, ic->uses); /* used in this one */
248 bitVectUnSetBit (spillable, ic->defKey);
249 spillable = bitVectIntersect (spillable, _G.regAssigned);
254 /*-----------------------------------------------------------------*/
255 /* noSpilLoc - return true if a variable has no spil location */
256 /*-----------------------------------------------------------------*/
258 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
260 return (sym->usl.spillLoc ? 0 : 1);
263 /*-----------------------------------------------------------------*/
264 /* hasSpilLoc - will return 1 if the symbol has spil location */
265 /*-----------------------------------------------------------------*/
267 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
269 return (sym->usl.spillLoc ? 1 : 0);
272 /*-----------------------------------------------------------------*/
273 /* directSpilLoc - will return 1 if the splilocation is in direct */
274 /*-----------------------------------------------------------------*/
276 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
278 if (sym->usl.spillLoc &&
279 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
285 /*-----------------------------------------------------------------*/
286 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
287 /* but is not used as a pointer */
288 /*-----------------------------------------------------------------*/
290 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
292 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
295 /*-----------------------------------------------------------------*/
296 /* rematable - will return 1 if the remat flag is set */
297 /*-----------------------------------------------------------------*/
299 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
304 /*-----------------------------------------------------------------*/
305 /* notUsedInBlock - not used in this block */
306 /*-----------------------------------------------------------------*/
308 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
310 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
311 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
312 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
315 /*-----------------------------------------------------------------*/
316 /* notUsedInRemaining - not used or defined in remain of the block */
317 /*-----------------------------------------------------------------*/
319 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
321 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
322 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
325 /*-----------------------------------------------------------------*/
326 /* allLRs - return true for all */
327 /*-----------------------------------------------------------------*/
329 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
334 /*-----------------------------------------------------------------*/
335 /* liveRangesWith - applies function to a given set of live range */
336 /*-----------------------------------------------------------------*/
338 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
339 eBBlock * ebp, iCode * ic)
344 if (!lrs || !lrs->size)
347 for (i = 1; i < lrs->size; i++)
350 if (!bitVectBitValue (lrs, i))
353 /* if we don't find it in the live range
354 hash table we are in serious trouble */
355 if (!(sym = hTabItemWithKey (liveRanges, i)))
357 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
358 "liveRangesWith could not find liveRange");
362 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
363 addSetHead (&rset, sym);
370 /*-----------------------------------------------------------------*/
371 /* leastUsedLR - given a set determines which is the least used */
372 /*-----------------------------------------------------------------*/
374 leastUsedLR (set * sset)
376 symbol *sym = NULL, *lsym = NULL;
378 sym = lsym = setFirstItem (sset);
383 for (; lsym; lsym = setNextItem (sset))
386 /* if usage is the same then prefer
387 the spill the smaller of the two */
388 if (lsym->used == sym->used)
389 if (getSize (lsym->type) < getSize (sym->type))
393 if (lsym->used < sym->used)
398 setToNull ((void **) &sset);
403 /*-----------------------------------------------------------------*/
404 /* noOverLap - will iterate through the list looking for over lap */
405 /*-----------------------------------------------------------------*/
407 noOverLap (set * itmpStack, symbol * fsym)
411 for (sym = setFirstItem (itmpStack); sym;
412 sym = setNextItem (itmpStack))
414 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
419 /*-----------------------------------------------------------------*/
420 /* isFree - will return 1 if the a free spil location is found */
421 /*-----------------------------------------------------------------*/
426 V_ARG (symbol **, sloc);
427 V_ARG (symbol *, fsym);
429 /* if already found */
433 /* if it is free && and the itmp assigned to
434 this does not have any overlapping live ranges
435 with the one currently being assigned and
436 the size can be accomodated */
438 noOverLap (sym->usl.itmpStack, fsym) &&
439 getSize (sym->type) >= getSize (fsym->type))
448 /*-----------------------------------------------------------------*/
449 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
450 /*-----------------------------------------------------------------*/
452 spillLRWithPtrReg (symbol * forSym)
458 if (!_G.regAssigned ||
459 bitVectIsZero (_G.regAssigned))
462 r0 = ds390_regWithIdx (R0_IDX);
463 r1 = ds390_regWithIdx (R1_IDX);
465 /* for all live ranges */
466 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
467 lrsym = hTabNextItem (liveRanges, &k))
471 /* if no registers assigned to it or
473 /* if it does not overlap with this then
474 not need to spill it */
476 if (lrsym->isspilt || !lrsym->nRegs ||
477 (lrsym->liveTo < forSym->liveFrom))
480 /* go thru the registers : if it is either
481 r0 or r1 then spil it */
482 for (j = 0; j < lrsym->nRegs; j++)
483 if (lrsym->regs[j] == r0 ||
484 lrsym->regs[j] == r1)
493 /*-----------------------------------------------------------------*/
494 /* createStackSpil - create a location on the stack to spil */
495 /*-----------------------------------------------------------------*/
497 createStackSpil (symbol * sym)
500 int useXstack, model, noOverlay;
504 /* first go try and find a free one that is already
505 existing on the stack */
506 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
508 /* found a free one : just update & return */
509 sym->usl.spillLoc = sloc;
512 addSetHead (&sloc->usl.itmpStack, sym);
516 /* could not then have to create one , this is the hard part
517 we need to allocate this on the stack : this is really a
518 hack!! but cannot think of anything better at this time */
520 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
522 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
527 sloc = newiTemp (slocBuffer);
529 /* set the type to the spilling symbol */
530 sloc->type = copyLinkChain (sym->type);
531 sloc->etype = getSpec (sloc->type);
532 if (options.model == MODEL_SMALL) {
533 SPEC_SCLS (sloc->etype) = S_DATA;
535 SPEC_SCLS (sloc->etype) = S_XDATA;
537 SPEC_EXTR (sloc->etype) = 0;
538 SPEC_STAT (sloc->etype) = 0;
539 SPEC_VOLATILE(sloc->etype) = 0;
541 /* we don't allow it to be allocated`
542 onto the external stack since : so we
543 temporarily turn it off ; we also
544 turn off memory model to prevent
545 the spil from going to the external storage
546 and turn off overlaying
549 useXstack = options.useXstack;
550 model = options.model;
551 noOverlay = options.noOverlay;
552 options.noOverlay = 1;
554 /* options.model = options.useXstack = 0; */
558 options.useXstack = useXstack;
559 options.model = model;
560 options.noOverlay = noOverlay;
561 sloc->isref = 1; /* to prevent compiler warning */
563 /* if it is on the stack then update the stack */
564 if (IN_STACK (sloc->etype))
566 currFunc->stack += getSize (sloc->type);
567 _G.stackExtend += getSize (sloc->type);
570 _G.dataExtend += getSize (sloc->type);
572 /* add it to the _G.stackSpil set */
573 addSetHead (&_G.stackSpil, sloc);
574 sym->usl.spillLoc = sloc;
577 /* add it to the set of itempStack set
578 of the spill location */
579 addSetHead (&sloc->usl.itmpStack, sym);
583 /*-----------------------------------------------------------------*/
584 /* isSpiltOnStack - returns true if the spil location is on stack */
585 /*-----------------------------------------------------------------*/
587 isSpiltOnStack (symbol * sym)
597 /* if (sym->_G.stackSpil) */
600 if (!sym->usl.spillLoc)
603 etype = getSpec (sym->usl.spillLoc->type);
604 if (IN_STACK (etype))
610 /*-----------------------------------------------------------------*/
611 /* spillThis - spils a specific operand */
612 /*-----------------------------------------------------------------*/
614 spillThis (symbol * sym)
617 /* if this is rematerializable or has a spillLocation
618 we are okay, else we need to create a spillLocation
620 if (!(sym->remat || sym->usl.spillLoc))
621 createStackSpil (sym);
624 /* mark it has spilt & put it in the spilt set */
625 sym->isspilt = sym->spillA = 1;
626 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
628 bitVectUnSetBit (_G.regAssigned, sym->key);
629 bitVectUnSetBit (_G.totRegAssigned, sym->key);
631 for (i = 0; i < sym->nRegs; i++)
635 freeReg (sym->regs[i]);
639 /* if spilt on stack then free up r0 & r1
640 if they could have been assigned to some
642 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
644 ds390_ptrRegReq += !options.stack10bit;
645 spillLRWithPtrReg (sym);
648 if (sym->usl.spillLoc && !sym->remat)
649 sym->usl.spillLoc->allocreq++;
653 /*-----------------------------------------------------------------*/
654 /* selectSpil - select a iTemp to spil : rather a simple procedure */
655 /*-----------------------------------------------------------------*/
657 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
659 bitVect *lrcs = NULL;
663 /* get the spillable live ranges */
664 lrcs = computeSpillable (ic);
666 /* get all live ranges that are rematerizable */
667 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
670 /* return the least used of these */
671 return leastUsedLR (selectS);
674 /* get live ranges with spillLocations in direct space */
675 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
677 sym = leastUsedLR (selectS);
678 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
679 sym->usl.spillLoc->rname :
680 sym->usl.spillLoc->name));
682 /* mark it as allocation required */
683 sym->usl.spillLoc->allocreq++;
687 /* if the symbol is local to the block then */
688 if (forSym->liveTo < ebp->lSeq)
691 /* check if there are any live ranges allocated
692 to registers that are not used in this block */
693 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
695 sym = leastUsedLR (selectS);
696 /* if this is not rematerializable */
705 /* check if there are any live ranges that not
706 used in the remainder of the block */
707 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
709 sym = leastUsedLR (selectS);
722 /* find live ranges with spillocation && not used as pointers */
723 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
726 sym = leastUsedLR (selectS);
727 /* mark this as allocation required */
728 sym->usl.spillLoc->allocreq++;
732 /* find live ranges with spillocation */
733 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
736 sym = leastUsedLR (selectS);
737 sym->usl.spillLoc->allocreq++;
741 /* couldn't find then we need to create a spil
742 location on the stack , for which one? the least
744 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
747 /* return a created spil location */
748 sym = createStackSpil (leastUsedLR (selectS));
749 sym->usl.spillLoc->allocreq++;
753 /* this is an extreme situation we will spill
754 this one : happens very rarely but it does happen */
760 /*-----------------------------------------------------------------*/
761 /* spilSomething - spil some variable & mark registers as free */
762 /*-----------------------------------------------------------------*/
764 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
769 /* get something we can spil */
770 ssym = selectSpil (ic, ebp, forSym);
772 /* mark it as spilt */
773 ssym->isspilt = ssym->spillA = 1;
774 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
776 /* mark it as not register assigned &
777 take it away from the set */
778 bitVectUnSetBit (_G.regAssigned, ssym->key);
779 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
781 /* mark the registers as free */
782 for (i = 0; i < ssym->nRegs; i++)
784 freeReg (ssym->regs[i]);
786 /* if spilt on stack then free up r0 & r1
787 if they could have been assigned to as gprs */
788 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
791 spillLRWithPtrReg (ssym);
794 /* if this was a block level spil then insert push & pop
795 at the start & end of block respectively */
798 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
799 /* add push to the start of the block */
800 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
801 ebp->sch->next : ebp->sch));
802 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
803 /* add pop to the end of the block */
804 addiCodeToeBBlock (ebp, nic, NULL);
807 /* if spilt because not used in the remainder of the
808 block then add a push before this instruction and
809 a pop at the end of the block */
810 if (ssym->remainSpil)
813 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
814 /* add push just before this instruction */
815 addiCodeToeBBlock (ebp, nic, ic);
817 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
818 /* add pop to the end of the block */
819 addiCodeToeBBlock (ebp, nic, NULL);
828 /*-----------------------------------------------------------------*/
829 /* getRegPtr - will try for PTR if not a GPR type if not spil */
830 /*-----------------------------------------------------------------*/
832 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
837 /* try for a ptr type */
838 if ((reg = allocReg (REG_PTR)))
841 /* try for gpr type */
842 if ((reg = allocReg (REG_GPR)))
845 /* we have to spil */
846 if (!spilSomething (ic, ebp, sym))
849 /* this looks like an infinite loop but
850 in really selectSpil will abort */
854 /*-----------------------------------------------------------------*/
855 /* getRegGpr - will try for GPR if not spil */
856 /*-----------------------------------------------------------------*/
858 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
863 /* try for gpr type */
864 if ((reg = allocReg (REG_GPR)))
867 if (!ds390_ptrRegReq)
868 if ((reg = allocReg (REG_PTR)))
871 /* we have to spil */
872 if (!spilSomething (ic, ebp, sym))
875 /* this looks like an infinite loop but
876 in really selectSpil will abort */
880 /*-----------------------------------------------------------------*/
881 /* getRegPtrNoSpil - get it cannot split */
882 /*-----------------------------------------------------------------*/
883 static regs *getRegPtrNoSpil()
887 /* try for a ptr type */
888 if ((reg = allocReg (REG_PTR)))
891 /* try for gpr type */
892 if ((reg = allocReg (REG_GPR)))
898 /*-----------------------------------------------------------------*/
899 /* getRegGprNoSpil - get it cannot split */
900 /*-----------------------------------------------------------------*/
901 static regs *getRegGprNoSpil()
905 if ((reg = allocReg (REG_GPR)))
908 if (!ds390_ptrRegReq)
909 if ((reg = allocReg (REG_PTR)))
915 /*-----------------------------------------------------------------*/
916 /* symHasReg - symbol has a given register */
917 /*-----------------------------------------------------------------*/
919 symHasReg (symbol * sym, regs * reg)
923 for (i = 0; i < sym->nRegs; i++)
924 if (sym->regs[i] == reg)
930 /*-----------------------------------------------------------------*/
931 /* deassignLRs - check the live to and if they have registers & are */
932 /* not spilt then free up the registers */
933 /*-----------------------------------------------------------------*/
935 deassignLRs (iCode * ic, eBBlock * ebp)
941 for (sym = hTabFirstItem (liveRanges, &k); sym;
942 sym = hTabNextItem (liveRanges, &k))
946 /* if it does not end here */
947 if (sym->liveTo > ic->seq)
950 /* if it was spilt on stack then we can
951 mark the stack spil location as free */
956 sym->usl.spillLoc->isFree = 1;
962 if (!bitVectBitValue (_G.regAssigned, sym->key))
965 /* special case check if this is an IFX &
966 the privious one was a pop and the
967 previous one was not spilt then keep track
969 if (ic->op == IFX && ic->prev &&
970 ic->prev->op == IPOP &&
971 !ic->prev->parmPush &&
972 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
973 psym = OP_SYMBOL (IC_LEFT (ic->prev));
979 bitVectUnSetBit (_G.regAssigned, sym->key);
981 /* if the result of this one needs registers
982 and does not have it then assign it right
984 if (IC_RESULT (ic) &&
985 !(SKIP_IC2 (ic) || /* not a special icode */
986 ic->op == JUMPTABLE ||
992 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
993 result->liveTo > ic->seq && /* and will live beyond this */
994 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
995 result->regType == sym->regType && /* same register types */
996 result->nRegs && /* which needs registers */
997 !result->isspilt && /* and does not already have them */
999 !bitVectBitValue (_G.regAssigned, result->key) &&
1000 /* the number of free regs + number of regs in this LR
1001 can accomodate the what result Needs */
1002 ((nfreeRegsType (result->regType) +
1003 sym->nRegs) >= result->nRegs)
1007 for (i = 0; i < result->nRegs; i++)
1009 result->regs[i] = sym->regs[i];
1011 result->regs[i] = getRegGpr (ic, ebp, result);
1013 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1014 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1018 /* free the remaining */
1019 for (; i < sym->nRegs; i++)
1023 if (!symHasReg (psym, sym->regs[i]))
1024 freeReg (sym->regs[i]);
1027 freeReg (sym->regs[i]);
1034 /*-----------------------------------------------------------------*/
1035 /* reassignLR - reassign this to registers */
1036 /*-----------------------------------------------------------------*/
1038 reassignLR (operand * op)
1040 symbol *sym = OP_SYMBOL (op);
1043 /* not spilt any more */
1044 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1045 bitVectUnSetBit (_G.spiltSet, sym->key);
1047 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1048 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1052 for (i = 0; i < sym->nRegs; i++)
1053 sym->regs[i]->isFree = 0;
1056 /*-----------------------------------------------------------------*/
1057 /* willCauseSpill - determines if allocating will cause a spill */
1058 /*-----------------------------------------------------------------*/
1060 willCauseSpill (int nr, int rt)
1062 /* first check if there are any avlb registers
1063 of te type required */
1066 /* special case for pointer type
1067 if pointer type not avlb then
1068 check for type gpr */
1069 if (nFreeRegs (rt) >= nr)
1071 if (nFreeRegs (REG_GPR) >= nr)
1076 if (ds390_ptrRegReq)
1078 if (nFreeRegs (rt) >= nr)
1083 if (nFreeRegs (REG_PTR) +
1084 nFreeRegs (REG_GPR) >= nr)
1089 /* it will cause a spil */
1093 /*-----------------------------------------------------------------*/
1094 /* positionRegs - the allocator can allocate same registers to res- */
1095 /* ult and operand, if this happens make sure they are in the same */
1096 /* position as the operand otherwise chaos results */
1097 /*-----------------------------------------------------------------*/
1099 positionRegs (symbol * result, symbol * opsym)
1101 int count = min (result->nRegs, opsym->nRegs);
1102 int i, j = 0, shared = 0;
1105 /* if the result has been spilt then cannot share */
1110 /* first make sure that they actually share */
1111 for (i = 0; i < count; i++)
1113 for (j = 0; j < count; j++)
1115 if (result->regs[i] == opsym->regs[j] && i != j)
1125 regs *tmp = result->regs[i];
1126 result->regs[i] = result->regs[j];
1127 result->regs[j] = tmp;
1134 /*-----------------------------------------------------------------*/
1135 /* serialRegAssign - serially allocate registers to the variables */
1136 /*-----------------------------------------------------------------*/
1138 serialRegAssign (eBBlock ** ebbs, int count)
1142 /* for all blocks */
1143 for (i = 0; i < count; i++)
1148 if (ebbs[i]->noPath &&
1149 (ebbs[i]->entryLabel != entryLabel &&
1150 ebbs[i]->entryLabel != returnLabel))
1153 /* of all instructions do */
1154 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1157 /* if this is an ipop that means some live
1158 range will have to be assigned again */
1160 reassignLR (IC_LEFT (ic));
1162 /* if result is present && is a true symbol */
1163 if (IC_RESULT (ic) && ic->op != IFX &&
1164 IS_TRUE_SYMOP (IC_RESULT (ic)))
1165 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1167 /* take away registers from live
1168 ranges that end at this instruction */
1169 deassignLRs (ic, ebbs[i]);
1171 /* some don't need registers */
1172 if (SKIP_IC2 (ic) ||
1173 ic->op == JUMPTABLE ||
1177 (IC_RESULT (ic) && POINTER_SET (ic)))
1180 /* now we need to allocate registers
1181 only for the result */
1184 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1190 /* if it does not need or is spilt
1191 or is already assigned to registers
1192 or will not live beyond this instructions */
1195 bitVectBitValue (_G.regAssigned, sym->key) ||
1196 sym->liveTo <= ic->seq)
1199 /* if some liverange has been spilt at the block level
1200 and this one live beyond this block then spil this
1202 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1207 /* if trying to allocate this will cause
1208 a spill and there is nothing to spill
1209 or this one is rematerializable then
1211 willCS = willCauseSpill (sym->nRegs, sym->regType);
1212 spillable = computeSpillable (ic);
1214 (willCS && bitVectIsZero (spillable)))
1222 /* if it has a spillocation & is used less than
1223 all other live ranges then spill this */
1225 if (sym->usl.spillLoc) {
1226 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1227 allLRs, ebbs[i], ic));
1228 if (leastUsed && leastUsed->used > sym->used) {
1233 /* if none of the liveRanges have a spillLocation then better
1234 to spill this one than anything else already assigned to registers */
1235 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1236 /* if this is local to this block then we might find a block spil */
1237 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1245 /* if we need ptr regs for the right side
1247 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1248 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1249 <= (unsigned) PTRSIZE)
1254 /* else we assign registers to it */
1255 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1256 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1258 for (j = 0; j < sym->nRegs; j++)
1260 if (sym->regType == REG_PTR)
1261 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1263 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1265 /* if the allocation falied which means
1266 this was spilt then break */
1270 /* if it shares registers with operands make sure
1271 that they are in the same position */
1272 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1273 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1274 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1275 OP_SYMBOL (IC_LEFT (ic)));
1276 /* do the same for the right operand */
1277 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1278 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1279 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1280 OP_SYMBOL (IC_RIGHT (ic)));
1293 /*-----------------------------------------------------------------*/
1294 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1295 /*-----------------------------------------------------------------*/
1296 static void fillGaps()
1301 if (getenv("DISABLE_FILL_GAPS")) return;
1303 /* First try to do DPTRuse once more since now we know what got into
1306 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1307 sym = hTabNextItem(liveRanges,&key)) {
1309 if (sym->uptr && !sym->ruonly && getSize(sym->type) < 4) {
1310 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1312 D (printf ("FILL GAPS: found more DPTR use for %s in func %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1313 /* if this was ssigned to registers then */
1314 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1316 /* take it out of the register assigned set */
1317 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1319 } else if (sym->usl.spillLoc) sym->usl.spillLoc->allocreq--;
1321 sym->isspilt = sym->spillA = 0;
1326 /* look for livernages that was spilt by the allocator */
1327 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1328 sym = hTabNextItem(liveRanges,&key)) {
1333 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1335 /* find the liveRanges this one clashes with, that are
1336 still assigned to registers & mark the registers as used*/
1337 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1341 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1342 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1345 assert (clr = hTabItemWithKey(liveRanges,i));
1347 /* mark these registers as used */
1348 for (k = 0 ; k < clr->nRegs ; k++ )
1349 useReg(clr->regs[k]);
1352 if (willCauseSpill(sym->nRegs,sym->regType)) {
1353 /* NOPE :( clear all registers & and continue */
1358 /* THERE IS HOPE !!!! */
1359 for (i=0; i < sym->nRegs ; i++ ) {
1360 if (sym->regType == REG_PTR)
1361 sym->regs[i] = getRegPtrNoSpil ();
1363 sym->regs[i] = getRegGprNoSpil ();
1366 /* for all its definitions check if the registers
1367 allocated needs positioning NOTE: we can position
1368 only ONCE if more than One positioning required
1371 for (i = 0 ; i < sym->defs->size ; i++ ) {
1372 if (bitVectBitValue(sym->defs,i)) {
1374 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1375 if (SKIP_IC(ic)) continue;
1376 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1377 /* if left is assigned to registers */
1378 if (IS_SYMOP(IC_LEFT(ic)) &&
1379 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1380 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1382 if (IS_SYMOP(IC_RIGHT(ic)) &&
1383 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1384 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1386 if (pdone > 1) break;
1389 /* had to position more than once GIVE UP */
1391 /* UNDO all the changes we made to try this */
1393 for (i=0; i < sym->nRegs ; i++ ) {
1394 sym->regs[i] = NULL;
1397 D (printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1400 D (printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1401 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1402 sym->isspilt = sym->spillA = 0 ;
1403 sym->usl.spillLoc->allocreq--;
1408 /*-----------------------------------------------------------------*/
1409 /* rUmaskForOp :- returns register mask for an operand */
1410 /*-----------------------------------------------------------------*/
1412 ds390_rUmaskForOp (operand * op)
1418 /* only temporaries are assigned registers */
1422 sym = OP_SYMBOL (op);
1424 /* if spilt or no registers assigned to it
1426 if (sym->isspilt || !sym->nRegs)
1429 rumask = newBitVect (ds390_nRegs);
1431 for (j = 0; j < sym->nRegs; j++)
1433 rumask = bitVectSetBit (rumask,
1434 sym->regs[j]->rIdx);
1440 /*-----------------------------------------------------------------*/
1441 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1442 /*-----------------------------------------------------------------*/
1444 regsUsedIniCode (iCode * ic)
1446 bitVect *rmask = newBitVect (ds390_nRegs);
1448 /* do the special cases first */
1451 rmask = bitVectUnion (rmask,
1452 ds390_rUmaskForOp (IC_COND (ic)));
1456 /* for the jumptable */
1457 if (ic->op == JUMPTABLE)
1459 rmask = bitVectUnion (rmask,
1460 ds390_rUmaskForOp (IC_JTCOND (ic)));
1465 /* of all other cases */
1467 rmask = bitVectUnion (rmask,
1468 ds390_rUmaskForOp (IC_LEFT (ic)));
1472 rmask = bitVectUnion (rmask,
1473 ds390_rUmaskForOp (IC_RIGHT (ic)));
1476 rmask = bitVectUnion (rmask,
1477 ds390_rUmaskForOp (IC_RESULT (ic)));
1483 /*-----------------------------------------------------------------*/
1484 /* createRegMask - for each instruction will determine the regsUsed */
1485 /*-----------------------------------------------------------------*/
1487 createRegMask (eBBlock ** ebbs, int count)
1491 /* for all blocks */
1492 for (i = 0; i < count; i++)
1496 if (ebbs[i]->noPath &&
1497 (ebbs[i]->entryLabel != entryLabel &&
1498 ebbs[i]->entryLabel != returnLabel))
1501 /* for all instructions */
1502 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1507 if (SKIP_IC2 (ic) || !ic->rlive)
1510 /* first mark the registers used in this
1512 ic->rUsed = regsUsedIniCode (ic);
1513 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1515 /* now create the register mask for those
1516 registers that are in use : this is a
1517 super set of ic->rUsed */
1518 ic->rMask = newBitVect (ds390_nRegs + 1);
1520 /* for all live Ranges alive at this point */
1521 for (j = 1; j < ic->rlive->size; j++)
1526 /* if not alive then continue */
1527 if (!bitVectBitValue (ic->rlive, j))
1530 /* find the live range we are interested in */
1531 if (!(sym = hTabItemWithKey (liveRanges, j)))
1533 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1534 "createRegMask cannot find live range");
1538 /* special case for ruonly */
1539 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1540 int size = getSize(sym->type);
1542 for (k = 0 ; k < size; k++ )
1543 ic->rMask = bitVectSetBit (ic->rMask, j++);
1546 /* if no register assigned to it */
1547 if (!sym->nRegs || sym->isspilt)
1550 /* for all the registers allocated to it */
1551 for (k = 0; k < sym->nRegs; k++)
1554 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1560 /*-----------------------------------------------------------------*/
1561 /* rematStr - returns the rematerialized string for a remat var */
1562 /*-----------------------------------------------------------------*/
1564 rematStr (symbol * sym)
1567 iCode *ic = sym->rematiCode;
1572 /* if plus or minus print the right hand side */
1573 if (ic->op == '+' || ic->op == '-')
1575 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1578 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1581 /* cast then continue */
1582 if (IS_CAST_ICODE(ic)) {
1583 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1586 /* we reached the end */
1587 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1594 /*-----------------------------------------------------------------*/
1595 /* regTypeNum - computes the type & number of registers required */
1596 /*-----------------------------------------------------------------*/
1604 /* for each live range do */
1605 for (sym = hTabFirstItem (liveRanges, &k); sym;
1606 sym = hTabNextItem (liveRanges, &k))
1609 /* if used zero times then no registers needed */
1610 if ((sym->liveTo - sym->liveFrom) == 0)
1614 /* if the live range is a temporary */
1618 /* if the type is marked as a conditional */
1619 if (sym->regType == REG_CND)
1622 /* if used in return only then we don't
1624 if (sym->ruonly || sym->accuse)
1626 if (IS_AGGREGATE (sym->type) || sym->isptr)
1627 sym->type = aggrToPtr (sym->type, FALSE);
1631 /* if the symbol has only one definition &
1632 that definition is a get_pointer and the
1633 pointer we are getting is rematerializable and
1636 if (bitVectnBitsOn (sym->defs) == 1 &&
1637 (ic = hTabItemWithKey (iCodehTab,
1638 bitVectFirstBit (sym->defs))) &&
1641 !IS_BITVAR (sym->etype))
1645 /* if remat in data space */
1646 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1647 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1648 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1651 /* create a psuedo symbol & force a spil */
1652 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1653 psym->type = sym->type;
1654 psym->etype = sym->etype;
1655 strcpy (psym->rname, psym->name);
1657 sym->usl.spillLoc = psym;
1661 /* if in data space or idata space then try to
1662 allocate pointer register */
1666 /* if not then we require registers */
1667 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1668 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1669 getSize (sym->type));
1673 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1674 printTypeChain (sym->type, stderr);
1675 fprintf (stderr, "\n");
1678 /* determine the type of register required */
1679 if (sym->nRegs == 1 &&
1680 IS_PTR (sym->type) &&
1682 sym->regType = REG_PTR;
1684 sym->regType = REG_GPR;
1688 /* for the first run we don't provide */
1689 /* registers for true symbols we will */
1690 /* see how things go */
1696 /*-----------------------------------------------------------------*/
1697 /* freeAllRegs - mark all registers as free */
1698 /*-----------------------------------------------------------------*/
1704 for (i = 0; i < ds390_nRegs; i++)
1705 regs390[i].isFree = 1;
1708 /*-----------------------------------------------------------------*/
1709 /* deallocStackSpil - this will set the stack pointer back */
1710 /*-----------------------------------------------------------------*/
1712 DEFSETFUNC (deallocStackSpil)
1720 /*-----------------------------------------------------------------*/
1721 /* farSpacePackable - returns the packable icode for far variables */
1722 /*-----------------------------------------------------------------*/
1724 farSpacePackable (iCode * ic)
1728 /* go thru till we find a definition for the
1729 symbol on the right */
1730 for (dic = ic->prev; dic; dic = dic->prev)
1733 /* if the definition is a call then no */
1734 if ((dic->op == CALL || dic->op == PCALL) &&
1735 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1740 /* if shift by unknown amount then not */
1741 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1742 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1745 /* if pointer get and size > 1 */
1746 if (POINTER_GET (dic) &&
1747 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1750 if (POINTER_SET (dic) &&
1751 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1754 /* if any three is a true symbol in far space */
1755 if (IC_RESULT (dic) &&
1756 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1757 isOperandInFarSpace (IC_RESULT (dic)))
1760 if (IC_RIGHT (dic) &&
1761 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1762 isOperandInFarSpace (IC_RIGHT (dic)) &&
1763 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1766 if (IC_LEFT (dic) &&
1767 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1768 isOperandInFarSpace (IC_LEFT (dic)) &&
1769 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1772 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1774 if ((dic->op == LEFT_OP ||
1775 dic->op == RIGHT_OP ||
1777 IS_OP_LITERAL (IC_RIGHT (dic)))
1787 /*-----------------------------------------------------------------*/
1788 /* packRegsForAssign - register reduction for assignment */
1789 /*-----------------------------------------------------------------*/
1791 packRegsForAssign (iCode * ic, eBBlock * ebp)
1795 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1796 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1797 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1802 /* if the true symbol is defined in far space or on stack
1803 then we should not since this will increase register pressure */
1805 if (isOperandInFarSpace (IC_RESULT (ic)))
1807 if ((dic = farSpacePackable (ic)))
1813 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1818 /* find the definition of iTempNN scanning backwards if we find a
1819 a use of the true symbol in before we find the definition then
1821 for (dic = ic->prev; dic; dic = dic->prev)
1823 /* if there is a function call then don't pack it */
1824 if ((dic->op == CALL || dic->op == PCALL))
1833 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1834 IS_OP_VOLATILE (IC_RESULT (dic)))
1840 if (IS_SYMOP (IC_RESULT (dic)) &&
1841 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1843 if (POINTER_SET (dic))
1849 if (IS_SYMOP (IC_RIGHT (dic)) &&
1850 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1851 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1857 if (IS_SYMOP (IC_LEFT (dic)) &&
1858 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1859 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1865 if (POINTER_SET (dic) &&
1866 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1874 return 0; /* did not find */
1876 /* if the result is on stack or iaccess then it must be
1877 the same atleast one of the operands */
1878 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1879 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1882 /* the operation has only one symbol
1883 operator then we can pack */
1884 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1885 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1888 if (!((IC_LEFT (dic) &&
1889 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1891 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1895 /* found the definition */
1896 /* replace the result with the result of */
1897 /* this assignment and remove this assignment */
1898 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1899 IC_RESULT (dic) = IC_RESULT (ic);
1901 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1903 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1905 /* delete from liverange table also
1906 delete from all the points inbetween and the new
1908 for (sic = dic; sic != ic; sic = sic->next)
1910 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1911 if (IS_ITEMP (IC_RESULT (dic)))
1912 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1915 remiCodeFromeBBlock (ebp, ic);
1916 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1917 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1918 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1923 /*-----------------------------------------------------------------*/
1924 /* findAssignToSym : scanning backwards looks for first assig found */
1925 /*-----------------------------------------------------------------*/
1927 findAssignToSym (operand * op, iCode * ic)
1931 for (dic = ic->prev; dic; dic = dic->prev)
1934 /* if definition by assignment */
1935 if (dic->op == '=' &&
1936 !POINTER_SET (dic) &&
1937 IC_RESULT (dic)->key == op->key
1938 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1942 /* we are interested only if defined in far space */
1943 /* or in stack space in case of + & - */
1945 /* if assigned to a non-symbol then return
1947 if (!IS_SYMOP (IC_RIGHT (dic)))
1950 /* if the symbol is in far space then
1952 if (isOperandInFarSpace (IC_RIGHT (dic)))
1955 /* for + & - operations make sure that
1956 if it is on the stack it is the same
1957 as one of the three operands */
1958 if ((ic->op == '+' || ic->op == '-') &&
1959 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1962 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1963 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1964 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1972 /* if we find an usage then we cannot delete it */
1973 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1976 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1979 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1983 /* now make sure that the right side of dic
1984 is not defined between ic & dic */
1987 iCode *sic = dic->next;
1989 for (; sic != ic; sic = sic->next)
1990 if (IC_RESULT (sic) &&
1991 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2000 /*-----------------------------------------------------------------*/
2001 /* packRegsForSupport :- reduce some registers for support calls */
2002 /*-----------------------------------------------------------------*/
2004 packRegsForSupport (iCode * ic, eBBlock * ebp)
2007 /* for the left & right operand :- look to see if the
2008 left was assigned a true symbol in far space in that
2009 case replace them */
2010 if (IS_ITEMP (IC_LEFT (ic)) &&
2011 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2013 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2019 /* found it we need to remove it from the
2021 for (sic = dic; sic != ic; sic = sic->next)
2022 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2024 IC_LEFT (ic)->operand.symOperand =
2025 IC_RIGHT (dic)->operand.symOperand;
2026 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2027 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2028 remiCodeFromeBBlock (ebp, dic);
2029 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2033 /* do the same for the right operand */
2036 IS_ITEMP (IC_RIGHT (ic)) &&
2037 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2039 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2045 /* if this is a subtraction & the result
2046 is a true symbol in far space then don't pack */
2047 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2049 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2050 if (IN_FARSPACE (SPEC_OCLS (etype)))
2053 /* found it we need to remove it from the
2055 for (sic = dic; sic != ic; sic = sic->next)
2056 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2058 IC_RIGHT (ic)->operand.symOperand =
2059 IC_RIGHT (dic)->operand.symOperand;
2060 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2062 remiCodeFromeBBlock (ebp, dic);
2063 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2064 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2071 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2074 /*-----------------------------------------------------------------*/
2075 /* packRegsDPTRuse : - will reduce some registers for single Use */
2076 /*-----------------------------------------------------------------*/
2078 packRegsDPTRuse (operand * op)
2080 /* go thru entire liveRange of this variable & check for
2081 other possible usage of DPTR , if we don't find it the
2082 assign this to DPTR (ruonly)
2087 sym_link *type, *etype;
2089 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2090 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2092 /* first check if any overlapping liverange has already been
2094 if (OP_SYMBOL(op)->clashes) {
2095 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2096 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2097 sym = hTabItemWithKey(liveRanges,i);
2098 if (sym->ruonly) return NULL ;
2103 /* no then go thru this guys live range */
2104 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2105 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2106 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2108 if (SKIP_IC3(ic)) continue;
2110 /* if PCALL cannot be sure give up */
2111 if (ic->op == PCALL) return NULL;
2113 /* if CALL then make sure it is VOID || return value not used
2114 or the return value is assigned to this one */
2115 if (ic->op == CALL) {
2116 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2117 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2118 etype = getSpec(type = operandType(IC_RESULT(ic)));
2120 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2123 if (getSize(type)==0)
2129 /* special case of add with a [remat] */
2130 if (ic->op == '+' &&
2131 OP_SYMBOL(IC_LEFT(ic))->remat &&
2132 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2133 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2137 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2138 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2141 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2142 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2144 /* conditionals can destroy 'b' - make sure B wont be used in this one*/
2145 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ) &&
2146 getSize(operandType(op)) > 3) return NULL;
2149 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2150 !isOperandEqual(IC_RESULT(ic),op) &&
2151 ((isOperandInFarSpace(IC_RESULT(ic)) && !isOperandInReg(IC_RESULT(ic))) ||
2152 OP_SYMBOL(IC_RESULT(ic))->ruonly ||
2153 OP_SYMBOL(IC_RESULT(ic))->onStack)) return NULL;
2155 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2156 !isOperandEqual(IC_RIGHT(ic),op) &&
2157 (OP_SYMBOL(IC_RIGHT(ic))->liveTo > ic->seq ||
2158 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2159 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2160 ((isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic)))||
2161 OP_SYMBOL(IC_RIGHT(ic))->onStack)) return NULL;
2163 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2164 !isOperandEqual(IC_LEFT(ic),op) &&
2165 (OP_SYMBOL(IC_LEFT(ic))->liveTo > ic->seq ||
2166 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2167 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2168 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic)))||
2169 OP_SYMBOL(IC_LEFT(ic))->onStack)) return NULL;
2171 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2172 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2173 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2174 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2177 OP_SYMBOL(op)->ruonly = 1;
2181 /*-----------------------------------------------------------------*/
2182 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2183 /*-----------------------------------------------------------------*/
2185 isBitwiseOptimizable (iCode * ic)
2187 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2188 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2190 /* bitwise operations are considered optimizable
2191 under the following conditions (Jean-Louis VERN)
2203 if ( IS_LITERAL (rtype) ||
2204 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2210 /*-----------------------------------------------------------------*/
2211 /* packRegsForAccUse - pack registers for acc use */
2212 /*-----------------------------------------------------------------*/
2214 packRegsForAccUse (iCode * ic)
2218 /* if this is an aggregate, e.g. a one byte char array */
2219 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2223 /* if + or - then it has to be one byte result */
2224 if ((ic->op == '+' || ic->op == '-')
2225 && getSize (operandType (IC_RESULT (ic))) > 1)
2228 /* if shift operation make sure right side is not a literal */
2229 if (ic->op == RIGHT_OP &&
2230 (isOperandLiteral (IC_RIGHT (ic)) ||
2231 getSize (operandType (IC_RESULT (ic))) > 1))
2234 if (ic->op == LEFT_OP &&
2235 (isOperandLiteral (IC_RIGHT (ic)) ||
2236 getSize (operandType (IC_RESULT (ic))) > 1))
2239 if (IS_BITWISE_OP (ic) &&
2240 getSize (operandType (IC_RESULT (ic))) > 1)
2244 /* has only one definition */
2245 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2248 /* has only one use */
2249 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2252 /* and the usage immediately follows this iCode */
2253 if (!(uic = hTabItemWithKey (iCodehTab,
2254 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2257 if (ic->next != uic)
2260 /* if it is a conditional branch then we definitely can */
2264 if (uic->op == JUMPTABLE)
2267 /* if the usage is not is an assignment
2268 or an arithmetic / bitwise / shift operation then not */
2269 if (POINTER_SET (uic) &&
2270 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2273 if (uic->op != '=' &&
2274 !IS_ARITHMETIC_OP (uic) &&
2275 !IS_BITWISE_OP (uic) &&
2276 uic->op != LEFT_OP &&
2277 uic->op != RIGHT_OP)
2280 /* if used in ^ operation then make sure right is not a
2282 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2285 /* if shift operation make sure right side is not a literal */
2286 if (uic->op == RIGHT_OP &&
2287 (isOperandLiteral (IC_RIGHT (uic)) ||
2288 getSize (operandType (IC_RESULT (uic))) > 1))
2291 if (uic->op == LEFT_OP &&
2292 (isOperandLiteral (IC_RIGHT (uic)) ||
2293 getSize (operandType (IC_RESULT (uic))) > 1))
2296 /* make sure that the result of this icode is not on the
2297 stack, since acc is used to compute stack offset */
2299 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2300 OP_SYMBOL (IC_RESULT (uic))->onStack)
2303 if (isOperandOnStack(IC_RESULT(uic)))
2307 /* if either one of them in far space then we cannot */
2308 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2309 isOperandInFarSpace (IC_LEFT (uic))) ||
2310 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2311 isOperandInFarSpace (IC_RIGHT (uic))))
2314 /* if the usage has only one operand then we can */
2315 if (IC_LEFT (uic) == NULL ||
2316 IC_RIGHT (uic) == NULL)
2319 /* make sure this is on the left side if not
2320 a '+' since '+' is commutative */
2321 if (ic->op != '+' &&
2322 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2326 // this is too dangerous and need further restrictions
2329 /* if one of them is a literal then we can */
2330 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2331 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2333 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2338 /* if the other one is not on stack then we can */
2339 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2340 (IS_ITEMP (IC_RIGHT (uic)) ||
2341 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2342 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2345 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2346 (IS_ITEMP (IC_LEFT (uic)) ||
2347 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2348 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2354 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2359 /*-----------------------------------------------------------------*/
2360 /* packForPush - hueristics to reduce iCode for pushing */
2361 /*-----------------------------------------------------------------*/
2363 packForPush (iCode * ic, eBBlock * ebp)
2368 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2371 /* must have only definition & one usage */
2372 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2373 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2376 /* find the definition */
2377 if (!(dic = hTabItemWithKey (iCodehTab,
2378 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2381 if (dic->op != '=' || POINTER_SET (dic))
2384 /* make sure the right side does not have any definitions
2386 dbv = OP_DEFS(IC_RIGHT(dic));
2387 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2388 if (bitVectBitValue(dbv,lic->key)) return ;
2390 /* make sure they have the same type */
2392 sym_link *itype=operandType(IC_LEFT(ic));
2393 sym_link *ditype=operandType(IC_RIGHT(dic));
2395 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2396 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2399 /* extend the live range of replaced operand if needed */
2400 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2401 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2403 /* we now we know that it has one & only one def & use
2404 and the that the definition is an assignment */
2405 IC_LEFT (ic) = IC_RIGHT (dic);
2407 remiCodeFromeBBlock (ebp, dic);
2408 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2409 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2412 /*-----------------------------------------------------------------*/
2413 /* packRegisters - does some transformations to reduce register */
2415 /*-----------------------------------------------------------------*/
2417 packRegisters (eBBlock * ebp)
2427 /* look for assignments of the form */
2428 /* iTempNN = TRueSym (someoperation) SomeOperand */
2430 /* TrueSym := iTempNN:1 */
2431 for (ic = ebp->sch; ic; ic = ic->next)
2435 /* find assignment of the form TrueSym := iTempNN:1 */
2436 if (ic->op == '=' && !POINTER_SET (ic))
2437 change += packRegsForAssign (ic, ebp);
2444 for (ic = ebp->sch; ic; ic = ic->next)
2447 /* if this is an itemp & result of a address of a true sym
2448 then mark this as rematerialisable */
2449 if (ic->op == ADDRESS_OF &&
2450 IS_ITEMP (IC_RESULT (ic)) &&
2451 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2452 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2453 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2456 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2457 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2458 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2462 /* if straight assignment then carry remat flag if
2463 this is the only definition */
2464 if (ic->op == '=' &&
2465 !POINTER_SET (ic) &&
2466 IS_SYMOP (IC_RIGHT (ic)) &&
2467 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2468 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2469 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2472 OP_SYMBOL (IC_RESULT (ic))->remat =
2473 OP_SYMBOL (IC_RIGHT (ic))->remat;
2474 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2475 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2478 /* if cast to a generic pointer & the pointer being
2479 cast is remat, then we can remat this cast as well */
2480 if (ic->op == CAST &&
2481 IS_SYMOP(IC_RIGHT(ic)) &&
2482 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2483 sym_link *to_type = operandType(IC_LEFT(ic));
2484 sym_link *from_type = operandType(IC_RIGHT(ic));
2485 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2486 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2487 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2488 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2492 /* if this is a +/- operation with a rematerizable
2493 then mark this as rematerializable as well */
2494 if ((ic->op == '+' || ic->op == '-') &&
2495 (IS_SYMOP (IC_LEFT (ic)) &&
2496 IS_ITEMP (IC_RESULT (ic)) &&
2497 OP_SYMBOL (IC_LEFT (ic))->remat &&
2498 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2499 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2500 IS_OP_LITERAL (IC_RIGHT (ic))))
2503 //int i = operandLitValue(IC_RIGHT(ic));
2504 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2505 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2506 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2509 /* mark the pointer usages */
2510 if (POINTER_SET (ic))
2511 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2513 if (POINTER_GET (ic))
2514 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2516 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2517 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2521 /* if we are using a symbol on the stack
2522 then we should say ds390_ptrRegReq */
2523 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2524 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2525 OP_SYMBOL (IC_COND (ic))->iaccess);
2526 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2527 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2528 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2531 if (IS_SYMOP (IC_LEFT (ic)))
2532 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2533 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2534 if (IS_SYMOP (IC_RIGHT (ic)))
2535 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2536 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2537 if (IS_SYMOP (IC_RESULT (ic)))
2538 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2539 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2544 /* if the condition of an if instruction
2545 is defined in the previous instruction then
2546 mark the itemp as a conditional */
2547 if ((IS_CONDITIONAL (ic) ||
2548 (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2549 ic->next && ic->next->op == IFX &&
2550 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2551 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2554 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2558 /* if the condition of an if instruction
2559 is defined in the previous instruction and
2560 this is the only usage then
2561 mark the itemp as a conditional */
2562 if ((IS_CONDITIONAL (ic) ||
2563 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2564 ic->next && ic->next->op == IFX &&
2565 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2566 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2567 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2569 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2574 /* reduce for support function calls */
2575 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2576 packRegsForSupport (ic, ebp);
2578 /* some cases the redundant moves can
2579 can be eliminated for return statements */
2580 if ((ic->op == RETURN || ic->op == SEND) &&
2581 !isOperandInFarSpace (IC_LEFT (ic)) &&
2584 packRegsDPTRuse (IC_LEFT (ic));
2587 if ((ic->op == CALL && getSize(operandType(IC_RESULT(ic))) <= 4)) {
2588 packRegsDPTRuse (IC_RESULT (ic));
2591 /* if pointer set & left has a size more than
2592 one and right is not in far space */
2593 if (POINTER_SET (ic) &&
2594 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2595 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2596 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2597 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2599 packRegsDPTRuse (IC_RESULT (ic));
2602 /* if pointer get */
2603 if (POINTER_GET (ic) &&
2604 !isOperandInFarSpace (IC_RESULT (ic)) &&
2605 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2606 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2607 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2609 packRegsDPTRuse (IC_LEFT (ic));
2612 /* if this is cast for intergral promotion then
2613 check if only use of the definition of the
2614 operand being casted/ if yes then replace
2615 the result of that arithmetic operation with
2616 this result and get rid of the cast */
2619 sym_link *fromType = operandType (IC_RIGHT (ic));
2620 sym_link *toType = operandType (IC_LEFT (ic));
2622 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2623 getSize (fromType) != getSize (toType) &&
2624 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2627 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2630 if (IS_ARITHMETIC_OP (dic))
2632 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2633 IC_RESULT (dic) = IC_RESULT (ic);
2634 remiCodeFromeBBlock (ebp, ic);
2635 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2636 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2637 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2641 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2647 /* if the type from and type to are the same
2648 then if this is the only use then packit */
2649 if (compareType (operandType (IC_RIGHT (ic)),
2650 operandType (IC_LEFT (ic))) == 1)
2652 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2655 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2656 IC_RESULT (dic) = IC_RESULT (ic);
2657 remiCodeFromeBBlock (ebp, ic);
2658 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2659 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2660 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2668 iTempNN := (some variable in farspace) V1
2673 if (ic->op == IPUSH)
2675 packForPush (ic, ebp);
2679 /* pack registers for accumulator use, when the
2680 result of an arithmetic or bit wise operation
2681 has only one use, that use is immediately following
2682 the defintion and the using iCode has only one
2683 operand or has two operands but one is literal &
2684 the result of that operation is not on stack then
2685 we can leave the result of this operation in acc:b
2687 if ((IS_ARITHMETIC_OP (ic)
2688 || IS_CONDITIONAL(ic)
2689 || IS_BITWISE_OP (ic)
2690 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2691 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2693 IS_ITEMP (IC_RESULT (ic)) &&
2694 getSize (operandType (IC_RESULT (ic))) <= 2)
2696 packRegsForAccUse (ic);
2701 /*-----------------------------------------------------------------*/
2702 /* assignRegisters - assigns registers to each live range as need */
2703 /*-----------------------------------------------------------------*/
2705 ds390_assignRegisters (eBBlock ** ebbs, int count)
2710 setToNull ((void *) &_G.funcrUsed);
2711 setToNull ((void *) &_G.regAssigned);
2712 setToNull ((void *) &_G.totRegAssigned);
2713 setToNull ((void *) &_G.funcrUsed);
2714 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2716 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2717 /* change assignments this will remove some
2718 live ranges reducing some register pressure */
2719 for (i = 0; i < count; i++)
2720 packRegisters (ebbs[i]);
2722 if (options.dump_pack)
2723 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2725 /* first determine for each live range the number of
2726 registers & the type of registers required for each */
2729 /* and serially allocate registers */
2730 serialRegAssign (ebbs, count);
2737 /* if stack was extended then tell the user */
2740 /* werror(W_TOOMANY_SPILS,"stack", */
2741 /* _G.stackExtend,currFunc->name,""); */
2747 /* werror(W_TOOMANY_SPILS,"data space", */
2748 /* _G.dataExtend,currFunc->name,""); */
2752 /* after that create the register mask
2753 for each of the instruction */
2754 createRegMask (ebbs, count);
2756 /* redo that offsets for stacked automatic variables */
2757 redoStackOffsets ();
2759 if (options.dump_rassgn) {
2760 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2761 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2764 /* do the overlaysegment stuff SDCCmem.c */
2765 doOverlays (ebbs, count);
2767 /* now get back the chain */
2768 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2773 /* free up any _G.stackSpil locations allocated */
2774 applyToSet (_G.stackSpil, deallocStackSpil);
2776 setToNull ((void **) &_G.stackSpil);
2777 setToNull ((void **) &_G.spiltSet);
2778 /* mark all registers as free */