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 /*-----------------------------------------------------------------*/
45 bitVect *totRegAssigned; /* final set of LRs that got into registers */
48 bitVect *funcrUsed; /* registers used in a function */
54 /* Shared with gen.c */
55 int ds390_ptrRegReq; /* one byte pointer register required */
61 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
62 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
63 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
64 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
65 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
66 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
67 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
68 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
69 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
70 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
71 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
72 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
73 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
74 {REG_CND, CND_IDX, REG_GPR, "C", "C", "xreg", 0, 0, 0},
75 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
76 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
77 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
78 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
81 static void spillThis (symbol *);
82 static void freeAllRegs ();
83 static iCode * packRegsDPTRuse (operand *);
85 /*-----------------------------------------------------------------*/
86 /* allocReg - allocates register of given type */
87 /*-----------------------------------------------------------------*/
93 for (i = 0; i < ds390_nRegs; i++)
96 /* if type is given as 0 then any
97 free register will do */
101 regs390[i].isFree = 0;
104 bitVectSetBit (currFunc->regsUsed, i);
107 /* other wise look for specific type
109 if (regs390[i].isFree &&
110 regs390[i].type == type)
112 regs390[i].isFree = 0;
115 bitVectSetBit (currFunc->regsUsed, i);
122 /*-----------------------------------------------------------------*/
123 /* ds390_regWithIdx - returns pointer to register wit index number */
124 /*-----------------------------------------------------------------*/
126 ds390_regWithIdx (int idx)
130 for (i = 0; i < ds390_nRegs; i++)
131 if (regs390[i].rIdx == idx)
134 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
135 "regWithIdx not found");
139 /*-----------------------------------------------------------------*/
140 /* freeReg - frees a register */
141 /*-----------------------------------------------------------------*/
148 /*-----------------------------------------------------------------*/
149 /* useReg - marks a register as used */
150 /*-----------------------------------------------------------------*/
158 /*-----------------------------------------------------------------*/
159 /* nFreeRegs - returns number of free registers */
160 /*-----------------------------------------------------------------*/
167 for (i = 0; i < ds390_nRegs; i++)
168 if (regs390[i].isFree && regs390[i].type == type)
173 /*-----------------------------------------------------------------*/
174 /* nfreeRegsType - free registers with type */
175 /*-----------------------------------------------------------------*/
177 nfreeRegsType (int type)
182 if ((nfr = nFreeRegs (type)) == 0)
183 return nFreeRegs (REG_GPR);
186 return nFreeRegs (type);
190 /*-----------------------------------------------------------------*/
191 /* allDefsOutOfRange - all definitions are out of a range */
192 /*-----------------------------------------------------------------*/
194 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
201 for (i = 0; i < defs->size; i++)
205 if (bitVectBitValue (defs, i) &&
206 (ic = hTabItemWithKey (iCodehTab, i)) &&
207 (ic->seq >= fseq && ic->seq <= toseq))
216 /*-----------------------------------------------------------------*/
217 /* isOperandInReg - returns true if operand is currently in regs */
218 /*-----------------------------------------------------------------*/
219 static int isOperandInReg(operand *op)
221 if (!IS_SYMOP(op)) return 0;
223 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
226 /*-----------------------------------------------------------------*/
227 /* computeSpillable - given a point find the spillable live ranges */
228 /*-----------------------------------------------------------------*/
230 computeSpillable (iCode * ic)
234 /* spillable live ranges are those that are live at this
235 point . the following categories need to be subtracted
237 a) - those that are already spilt
238 b) - if being used by this one
239 c) - defined by this one */
241 spillable = bitVectCopy (ic->rlive);
243 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
245 bitVectCplAnd (spillable, ic->uses); /* used in this one */
246 bitVectUnSetBit (spillable, ic->defKey);
247 spillable = bitVectIntersect (spillable, _G.regAssigned);
252 /*-----------------------------------------------------------------*/
253 /* noSpilLoc - return true if a variable has no spil location */
254 /*-----------------------------------------------------------------*/
256 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
258 return (sym->usl.spillLoc ? 0 : 1);
261 /*-----------------------------------------------------------------*/
262 /* hasSpilLoc - will return 1 if the symbol has spil location */
263 /*-----------------------------------------------------------------*/
265 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
267 return (sym->usl.spillLoc ? 1 : 0);
270 /*-----------------------------------------------------------------*/
271 /* directSpilLoc - will return 1 if the splilocation is in direct */
272 /*-----------------------------------------------------------------*/
274 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
276 if (sym->usl.spillLoc &&
277 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
283 /*-----------------------------------------------------------------*/
284 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
285 /* but is not used as a pointer */
286 /*-----------------------------------------------------------------*/
288 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
290 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
293 /*-----------------------------------------------------------------*/
294 /* rematable - will return 1 if the remat flag is set */
295 /*-----------------------------------------------------------------*/
297 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
302 /*-----------------------------------------------------------------*/
303 /* notUsedInBlock - not used in this block */
304 /*-----------------------------------------------------------------*/
306 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
308 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
309 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
310 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
313 /*-----------------------------------------------------------------*/
314 /* notUsedInRemaining - not used or defined in remain of the block */
315 /*-----------------------------------------------------------------*/
317 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
319 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
320 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
323 /*-----------------------------------------------------------------*/
324 /* allLRs - return true for all */
325 /*-----------------------------------------------------------------*/
327 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
332 /*-----------------------------------------------------------------*/
333 /* liveRangesWith - applies function to a given set of live range */
334 /*-----------------------------------------------------------------*/
336 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
337 eBBlock * ebp, iCode * ic)
342 if (!lrs || !lrs->size)
345 for (i = 1; i < lrs->size; i++)
348 if (!bitVectBitValue (lrs, i))
351 /* if we don't find it in the live range
352 hash table we are in serious trouble */
353 if (!(sym = hTabItemWithKey (liveRanges, i)))
355 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
356 "liveRangesWith could not find liveRange");
360 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
361 addSetHead (&rset, sym);
368 /*-----------------------------------------------------------------*/
369 /* leastUsedLR - given a set determines which is the least used */
370 /*-----------------------------------------------------------------*/
372 leastUsedLR (set * sset)
374 symbol *sym = NULL, *lsym = NULL;
376 sym = lsym = setFirstItem (sset);
381 for (; lsym; lsym = setNextItem (sset))
384 /* if usage is the same then prefer
385 the spill the smaller of the two */
386 if (lsym->used == sym->used)
387 if (getSize (lsym->type) < getSize (sym->type))
391 if (lsym->used < sym->used)
396 setToNull ((void **) &sset);
401 /*-----------------------------------------------------------------*/
402 /* noOverLap - will iterate through the list looking for over lap */
403 /*-----------------------------------------------------------------*/
405 noOverLap (set * itmpStack, symbol * fsym)
409 for (sym = setFirstItem (itmpStack); sym;
410 sym = setNextItem (itmpStack))
412 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
417 /*-----------------------------------------------------------------*/
418 /* isFree - will return 1 if the a free spil location is found */
419 /*-----------------------------------------------------------------*/
424 V_ARG (symbol **, sloc);
425 V_ARG (symbol *, fsym);
427 /* if already found */
431 /* if it is free && and the itmp assigned to
432 this does not have any overlapping live ranges
433 with the one currently being assigned and
434 the size can be accomodated */
436 noOverLap (sym->usl.itmpStack, fsym) &&
437 getSize (sym->type) >= getSize (fsym->type))
446 /*-----------------------------------------------------------------*/
447 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
448 /*-----------------------------------------------------------------*/
450 spillLRWithPtrReg (symbol * forSym)
456 if (!_G.regAssigned ||
457 bitVectIsZero (_G.regAssigned))
460 r0 = ds390_regWithIdx (R0_IDX);
461 r1 = ds390_regWithIdx (R1_IDX);
463 /* for all live ranges */
464 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
465 lrsym = hTabNextItem (liveRanges, &k))
469 /* if no registers assigned to it or
471 /* if it does not overlap with this then
472 not need to spill it */
474 if (lrsym->isspilt || !lrsym->nRegs ||
475 (lrsym->liveTo < forSym->liveFrom))
478 /* go thru the registers : if it is either
479 r0 or r1 then spil it */
480 for (j = 0; j < lrsym->nRegs; j++)
481 if (lrsym->regs[j] == r0 ||
482 lrsym->regs[j] == r1)
491 /*-----------------------------------------------------------------*/
492 /* createStackSpil - create a location on the stack to spil */
493 /*-----------------------------------------------------------------*/
495 createStackSpil (symbol * sym)
498 int useXstack, model, noOverlay;
502 /* first go try and find a free one that is already
503 existing on the stack */
504 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
506 /* found a free one : just update & return */
507 sym->usl.spillLoc = sloc;
510 addSetHead (&sloc->usl.itmpStack, sym);
514 /* could not then have to create one , this is the hard part
515 we need to allocate this on the stack : this is really a
516 hack!! but cannot think of anything better at this time */
518 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
520 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
525 sloc = newiTemp (slocBuffer);
527 /* set the type to the spilling symbol */
528 sloc->type = copyLinkChain (sym->type);
529 sloc->etype = getSpec (sloc->type);
530 if (options.model == MODEL_SMALL) {
531 SPEC_SCLS (sloc->etype) = S_DATA;
533 SPEC_SCLS (sloc->etype) = S_XDATA;
535 SPEC_EXTR (sloc->etype) = 0;
536 SPEC_STAT (sloc->etype) = 0;
537 SPEC_VOLATILE(sloc->etype) = 0;
539 /* we don't allow it to be allocated`
540 onto the external stack since : so we
541 temporarily turn it off ; we also
542 turn off memory model to prevent
543 the spil from going to the external storage
544 and turn off overlaying
547 useXstack = options.useXstack;
548 model = options.model;
549 noOverlay = options.noOverlay;
550 options.noOverlay = 1;
552 /* options.model = options.useXstack = 0; */
556 options.useXstack = useXstack;
557 options.model = model;
558 options.noOverlay = noOverlay;
559 sloc->isref = 1; /* to prevent compiler warning */
561 /* if it is on the stack then update the stack */
562 if (IN_STACK (sloc->etype))
564 currFunc->stack += getSize (sloc->type);
565 _G.stackExtend += getSize (sloc->type);
568 _G.dataExtend += getSize (sloc->type);
570 /* add it to the _G.stackSpil set */
571 addSetHead (&_G.stackSpil, sloc);
572 sym->usl.spillLoc = sloc;
575 /* add it to the set of itempStack set
576 of the spill location */
577 addSetHead (&sloc->usl.itmpStack, sym);
581 /*-----------------------------------------------------------------*/
582 /* isSpiltOnStack - returns true if the spil location is on stack */
583 /*-----------------------------------------------------------------*/
585 isSpiltOnStack (symbol * sym)
595 /* if (sym->_G.stackSpil) */
598 if (!sym->usl.spillLoc)
601 etype = getSpec (sym->usl.spillLoc->type);
602 if (IN_STACK (etype))
608 /*-----------------------------------------------------------------*/
609 /* spillThis - spils a specific operand */
610 /*-----------------------------------------------------------------*/
612 spillThis (symbol * sym)
615 /* if this is rematerializable or has a spillLocation
616 we are okay, else we need to create a spillLocation
618 if (!(sym->remat || sym->usl.spillLoc))
619 createStackSpil (sym);
622 /* mark it has spilt & put it in the spilt set */
623 sym->isspilt = sym->spillA = 1;
624 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
626 bitVectUnSetBit (_G.regAssigned, sym->key);
627 bitVectUnSetBit (_G.totRegAssigned, sym->key);
629 for (i = 0; i < sym->nRegs; i++)
633 freeReg (sym->regs[i]);
637 /* if spilt on stack then free up r0 & r1
638 if they could have been assigned to some
640 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
642 ds390_ptrRegReq += !options.stack10bit;
643 spillLRWithPtrReg (sym);
646 if (sym->usl.spillLoc && !sym->remat)
647 sym->usl.spillLoc->allocreq++;
651 /*-----------------------------------------------------------------*/
652 /* selectSpil - select a iTemp to spil : rather a simple procedure */
653 /*-----------------------------------------------------------------*/
655 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
657 bitVect *lrcs = NULL;
661 /* get the spillable live ranges */
662 lrcs = computeSpillable (ic);
664 /* get all live ranges that are rematerizable */
665 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
668 /* return the least used of these */
669 return leastUsedLR (selectS);
672 /* get live ranges with spillLocations in direct space */
673 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
675 sym = leastUsedLR (selectS);
676 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
677 sym->usl.spillLoc->rname :
678 sym->usl.spillLoc->name));
680 /* mark it as allocation required */
681 sym->usl.spillLoc->allocreq++;
685 /* if the symbol is local to the block then */
686 if (forSym->liveTo < ebp->lSeq)
689 /* check if there are any live ranges allocated
690 to registers that are not used in this block */
691 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
693 sym = leastUsedLR (selectS);
694 /* if this is not rematerializable */
703 /* check if there are any live ranges that not
704 used in the remainder of the block */
705 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
707 sym = leastUsedLR (selectS);
720 /* find live ranges with spillocation && not used as pointers */
721 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
724 sym = leastUsedLR (selectS);
725 /* mark this as allocation required */
726 sym->usl.spillLoc->allocreq++;
730 /* find live ranges with spillocation */
731 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
734 sym = leastUsedLR (selectS);
735 sym->usl.spillLoc->allocreq++;
739 /* couldn't find then we need to create a spil
740 location on the stack , for which one? the least
742 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
745 /* return a created spil location */
746 sym = createStackSpil (leastUsedLR (selectS));
747 sym->usl.spillLoc->allocreq++;
751 /* this is an extreme situation we will spill
752 this one : happens very rarely but it does happen */
758 /*-----------------------------------------------------------------*/
759 /* spilSomething - spil some variable & mark registers as free */
760 /*-----------------------------------------------------------------*/
762 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
767 /* get something we can spil */
768 ssym = selectSpil (ic, ebp, forSym);
770 /* mark it as spilt */
771 ssym->isspilt = ssym->spillA = 1;
772 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
774 /* mark it as not register assigned &
775 take it away from the set */
776 bitVectUnSetBit (_G.regAssigned, ssym->key);
777 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
779 /* mark the registers as free */
780 for (i = 0; i < ssym->nRegs; i++)
782 freeReg (ssym->regs[i]);
784 /* if spilt on stack then free up r0 & r1
785 if they could have been assigned to as gprs */
786 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
789 spillLRWithPtrReg (ssym);
792 /* if this was a block level spil then insert push & pop
793 at the start & end of block respectively */
796 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
797 /* add push to the start of the block */
798 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
799 ebp->sch->next : ebp->sch));
800 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
801 /* add pop to the end of the block */
802 addiCodeToeBBlock (ebp, nic, NULL);
805 /* if spilt because not used in the remainder of the
806 block then add a push before this instruction and
807 a pop at the end of the block */
808 if (ssym->remainSpil)
811 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
812 /* add push just before this instruction */
813 addiCodeToeBBlock (ebp, nic, ic);
815 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
816 /* add pop to the end of the block */
817 addiCodeToeBBlock (ebp, nic, NULL);
826 /*-----------------------------------------------------------------*/
827 /* getRegPtr - will try for PTR if not a GPR type if not spil */
828 /*-----------------------------------------------------------------*/
830 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
835 /* try for a ptr type */
836 if ((reg = allocReg (REG_PTR)))
839 /* try for gpr type */
840 if ((reg = allocReg (REG_GPR)))
843 /* we have to spil */
844 if (!spilSomething (ic, ebp, sym))
847 /* this looks like an infinite loop but
848 in really selectSpil will abort */
852 /*-----------------------------------------------------------------*/
853 /* getRegGpr - will try for GPR if not spil */
854 /*-----------------------------------------------------------------*/
856 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
861 /* try for gpr type */
862 if ((reg = allocReg (REG_GPR)))
865 if (!ds390_ptrRegReq)
866 if ((reg = allocReg (REG_PTR)))
869 /* we have to spil */
870 if (!spilSomething (ic, ebp, sym))
873 /* this looks like an infinite loop but
874 in really selectSpil will abort */
878 /*-----------------------------------------------------------------*/
879 /* getRegPtrNoSpil - get it cannot split */
880 /*-----------------------------------------------------------------*/
881 static regs *getRegPtrNoSpil()
885 /* try for a ptr type */
886 if ((reg = allocReg (REG_PTR)))
889 /* try for gpr type */
890 if ((reg = allocReg (REG_GPR)))
896 /*-----------------------------------------------------------------*/
897 /* getRegGprNoSpil - get it cannot split */
898 /*-----------------------------------------------------------------*/
899 static regs *getRegGprNoSpil()
903 if ((reg = allocReg (REG_GPR)))
906 if (!ds390_ptrRegReq)
907 if ((reg = allocReg (REG_PTR)))
913 /*-----------------------------------------------------------------*/
914 /* symHasReg - symbol has a given register */
915 /*-----------------------------------------------------------------*/
917 symHasReg (symbol * sym, regs * reg)
921 for (i = 0; i < sym->nRegs; i++)
922 if (sym->regs[i] == reg)
928 /*-----------------------------------------------------------------*/
929 /* deassignLRs - check the live to and if they have registers & are */
930 /* not spilt then free up the registers */
931 /*-----------------------------------------------------------------*/
933 deassignLRs (iCode * ic, eBBlock * ebp)
939 for (sym = hTabFirstItem (liveRanges, &k); sym;
940 sym = hTabNextItem (liveRanges, &k))
944 /* if it does not end here */
945 if (sym->liveTo > ic->seq)
948 /* if it was spilt on stack then we can
949 mark the stack spil location as free */
954 sym->usl.spillLoc->isFree = 1;
960 if (!bitVectBitValue (_G.regAssigned, sym->key))
963 /* special case check if this is an IFX &
964 the privious one was a pop and the
965 previous one was not spilt then keep track
967 if (ic->op == IFX && ic->prev &&
968 ic->prev->op == IPOP &&
969 !ic->prev->parmPush &&
970 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
971 psym = OP_SYMBOL (IC_LEFT (ic->prev));
977 bitVectUnSetBit (_G.regAssigned, sym->key);
979 /* if the result of this one needs registers
980 and does not have it then assign it right
982 if (IC_RESULT (ic) &&
983 !(SKIP_IC2 (ic) || /* not a special icode */
984 ic->op == JUMPTABLE ||
990 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
991 result->liveTo > ic->seq && /* and will live beyond this */
992 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
993 result->regType == sym->regType && /* same register types */
994 result->nRegs && /* which needs registers */
995 !result->isspilt && /* and does not already have them */
997 !bitVectBitValue (_G.regAssigned, result->key) &&
998 /* the number of free regs + number of regs in this LR
999 can accomodate the what result Needs */
1000 ((nfreeRegsType (result->regType) +
1001 sym->nRegs) >= result->nRegs)
1005 for (i = 0; i < result->nRegs; i++)
1007 result->regs[i] = sym->regs[i];
1009 result->regs[i] = getRegGpr (ic, ebp, result);
1011 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1012 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1016 /* free the remaining */
1017 for (; i < sym->nRegs; i++)
1021 if (!symHasReg (psym, sym->regs[i]))
1022 freeReg (sym->regs[i]);
1025 freeReg (sym->regs[i]);
1032 /*-----------------------------------------------------------------*/
1033 /* reassignLR - reassign this to registers */
1034 /*-----------------------------------------------------------------*/
1036 reassignLR (operand * op)
1038 symbol *sym = OP_SYMBOL (op);
1041 /* not spilt any more */
1042 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1043 bitVectUnSetBit (_G.spiltSet, sym->key);
1045 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1046 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1050 for (i = 0; i < sym->nRegs; i++)
1051 sym->regs[i]->isFree = 0;
1054 /*-----------------------------------------------------------------*/
1055 /* willCauseSpill - determines if allocating will cause a spill */
1056 /*-----------------------------------------------------------------*/
1058 willCauseSpill (int nr, int rt)
1060 /* first check if there are any avlb registers
1061 of te type required */
1064 /* special case for pointer type
1065 if pointer type not avlb then
1066 check for type gpr */
1067 if (nFreeRegs (rt) >= nr)
1069 if (nFreeRegs (REG_GPR) >= nr)
1074 if (ds390_ptrRegReq)
1076 if (nFreeRegs (rt) >= nr)
1081 if (nFreeRegs (REG_PTR) +
1082 nFreeRegs (REG_GPR) >= nr)
1087 /* it will cause a spil */
1091 /*-----------------------------------------------------------------*/
1092 /* positionRegs - the allocator can allocate same registers to res- */
1093 /* ult and operand, if this happens make sure they are in the same */
1094 /* position as the operand otherwise chaos results */
1095 /*-----------------------------------------------------------------*/
1097 positionRegs (symbol * result, symbol * opsym)
1099 int count = min (result->nRegs, opsym->nRegs);
1100 int i, j = 0, shared = 0;
1103 /* if the result has been spilt then cannot share */
1108 /* first make sure that they actually share */
1109 for (i = 0; i < count; i++)
1111 for (j = 0; j < count; j++)
1113 if (result->regs[i] == opsym->regs[j] && i != j)
1123 regs *tmp = result->regs[i];
1124 result->regs[i] = result->regs[j];
1125 result->regs[j] = tmp;
1132 /*-----------------------------------------------------------------*/
1133 /* serialRegAssign - serially allocate registers to the variables */
1134 /*-----------------------------------------------------------------*/
1136 serialRegAssign (eBBlock ** ebbs, int count)
1140 /* for all blocks */
1141 for (i = 0; i < count; i++)
1146 if (ebbs[i]->noPath &&
1147 (ebbs[i]->entryLabel != entryLabel &&
1148 ebbs[i]->entryLabel != returnLabel))
1151 /* of all instructions do */
1152 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1155 /* if this is an ipop that means some live
1156 range will have to be assigned again */
1158 reassignLR (IC_LEFT (ic));
1160 /* if result is present && is a true symbol */
1161 if (IC_RESULT (ic) && ic->op != IFX &&
1162 IS_TRUE_SYMOP (IC_RESULT (ic)))
1163 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1165 /* take away registers from live
1166 ranges that end at this instruction */
1167 deassignLRs (ic, ebbs[i]);
1169 /* some don't need registers */
1170 if (SKIP_IC2 (ic) ||
1171 ic->op == JUMPTABLE ||
1175 (IC_RESULT (ic) && POINTER_SET (ic)))
1178 /* now we need to allocate registers
1179 only for the result */
1182 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1188 /* if it does not need or is spilt
1189 or is already assigned to registers
1190 or will not live beyond this instructions */
1193 bitVectBitValue (_G.regAssigned, sym->key) ||
1194 sym->liveTo <= ic->seq)
1197 /* if some liverange has been spilt at the block level
1198 and this one live beyond this block then spil this
1200 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1205 /* if trying to allocate this will cause
1206 a spill and there is nothing to spill
1207 or this one is rematerializable then
1209 willCS = willCauseSpill (sym->nRegs, sym->regType);
1210 spillable = computeSpillable (ic);
1212 (willCS && bitVectIsZero (spillable)))
1220 /* if it has a spillocation & is used less than
1221 all other live ranges then spill this */
1223 if (sym->usl.spillLoc) {
1224 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1225 allLRs, ebbs[i], ic));
1226 if (leastUsed && leastUsed->used > sym->used) {
1231 /* if none of the liveRanges have a spillLocation then better
1232 to spill this one than anything else already assigned to registers */
1233 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1234 /* if this is local to this block then we might find a block spil */
1235 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1243 /* if we need ptr regs for the right side
1245 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1246 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1247 <= (unsigned) PTRSIZE)
1252 /* else we assign registers to it */
1253 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1254 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1256 for (j = 0; j < sym->nRegs; j++)
1258 if (sym->regType == REG_PTR)
1259 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1261 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1263 /* if the allocation falied which means
1264 this was spilt then break */
1268 /* if it shares registers with operands make sure
1269 that they are in the same position */
1270 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1271 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1272 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1273 OP_SYMBOL (IC_LEFT (ic)));
1274 /* do the same for the right operand */
1275 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1276 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1277 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1278 OP_SYMBOL (IC_RIGHT (ic)));
1291 /*-----------------------------------------------------------------*/
1292 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1293 /*-----------------------------------------------------------------*/
1294 static void fillGaps()
1299 if (getenv("DISABLE_FILL_GAPS")) return;
1301 /* First try to do DPTRuse once more since now we know what got into
1304 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1305 sym = hTabNextItem(liveRanges,&key)) {
1307 if (sym->uptr && !sym->ruonly && getSize(sym->type) < 4) {
1308 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1310 printf("FILL GAPS: found more DPTR use for %s in func %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN");
1311 /* if this was ssigned to registers then */
1312 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1314 /* take it out of the register assigned set */
1315 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1317 } else if (sym->usl.spillLoc) sym->usl.spillLoc->allocreq--;
1319 sym->isspilt = sym->spillA = 0;
1324 /* look for livernages that was spilt by the allocator */
1325 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1326 sym = hTabNextItem(liveRanges,&key)) {
1331 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1333 /* find the liveRanges this one clashes with, that are
1334 still assigned to registers & mark the registers as used*/
1335 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1339 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1340 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1343 assert (clr = hTabItemWithKey(liveRanges,i));
1345 /* mark these registers as used */
1346 for (k = 0 ; k < clr->nRegs ; k++ )
1347 useReg(clr->regs[k]);
1350 if (willCauseSpill(sym->nRegs,sym->regType)) {
1351 /* NOPE :( clear all registers & and continue */
1356 /* THERE IS HOPE !!!! */
1357 for (i=0; i < sym->nRegs ; i++ ) {
1358 if (sym->regType == REG_PTR)
1359 sym->regs[i] = getRegPtrNoSpil ();
1361 sym->regs[i] = getRegGprNoSpil ();
1364 /* for all its definitions check if the registers
1365 allocated needs positioning NOTE: we can position
1366 only ONCE if more than One positioning required
1369 for (i = 0 ; i < sym->defs->size ; i++ ) {
1370 if (bitVectBitValue(sym->defs,i)) {
1372 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1373 if (SKIP_IC(ic)) continue;
1374 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1375 /* if left is assigned to registers */
1376 if (IS_SYMOP(IC_LEFT(ic)) &&
1377 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1378 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1380 if (IS_SYMOP(IC_RIGHT(ic)) &&
1381 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1382 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1384 if (pdone > 1) break;
1387 /* had to position more than once GIVE UP */
1389 /* UNDO all the changes we made to try this */
1391 for (i=0; i < sym->nRegs ; i++ ) {
1392 sym->regs[i] = NULL;
1395 printf("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN");
1398 printf("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN");
1399 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1400 sym->isspilt = sym->spillA = 0 ;
1401 sym->usl.spillLoc->allocreq--;
1406 /*-----------------------------------------------------------------*/
1407 /* rUmaskForOp :- returns register mask for an operand */
1408 /*-----------------------------------------------------------------*/
1410 rUmaskForOp (operand * op)
1416 /* only temporaries are assigned registers */
1420 sym = OP_SYMBOL (op);
1422 /* if spilt or no registers assigned to it
1424 if (sym->isspilt || !sym->nRegs)
1427 rumask = newBitVect (ds390_nRegs);
1429 for (j = 0; j < sym->nRegs; j++)
1431 rumask = bitVectSetBit (rumask,
1432 sym->regs[j]->rIdx);
1438 /*-----------------------------------------------------------------*/
1439 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1440 /*-----------------------------------------------------------------*/
1442 regsUsedIniCode (iCode * ic)
1444 bitVect *rmask = newBitVect (ds390_nRegs);
1446 /* do the special cases first */
1449 rmask = bitVectUnion (rmask,
1450 rUmaskForOp (IC_COND (ic)));
1454 /* for the jumptable */
1455 if (ic->op == JUMPTABLE)
1457 rmask = bitVectUnion (rmask,
1458 rUmaskForOp (IC_JTCOND (ic)));
1463 /* of all other cases */
1465 rmask = bitVectUnion (rmask,
1466 rUmaskForOp (IC_LEFT (ic)));
1470 rmask = bitVectUnion (rmask,
1471 rUmaskForOp (IC_RIGHT (ic)));
1474 rmask = bitVectUnion (rmask,
1475 rUmaskForOp (IC_RESULT (ic)));
1481 /*-----------------------------------------------------------------*/
1482 /* createRegMask - for each instruction will determine the regsUsed */
1483 /*-----------------------------------------------------------------*/
1485 createRegMask (eBBlock ** ebbs, int count)
1489 /* for all blocks */
1490 for (i = 0; i < count; i++)
1494 if (ebbs[i]->noPath &&
1495 (ebbs[i]->entryLabel != entryLabel &&
1496 ebbs[i]->entryLabel != returnLabel))
1499 /* for all instructions */
1500 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1505 if (SKIP_IC2 (ic) || !ic->rlive)
1508 /* first mark the registers used in this
1510 ic->rUsed = regsUsedIniCode (ic);
1511 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1513 /* now create the register mask for those
1514 registers that are in use : this is a
1515 super set of ic->rUsed */
1516 ic->rMask = newBitVect (ds390_nRegs + 1);
1518 /* for all live Ranges alive at this point */
1519 for (j = 1; j < ic->rlive->size; j++)
1524 /* if not alive then continue */
1525 if (!bitVectBitValue (ic->rlive, j))
1528 /* find the live range we are interested in */
1529 if (!(sym = hTabItemWithKey (liveRanges, j)))
1531 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1532 "createRegMask cannot find live range");
1536 /* special case for ruonly */
1537 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1538 int size = getSize(sym->type);
1540 for (k = 0 ; k < size; k++ )
1541 ic->rMask = bitVectSetBit (ic->rMask, j++);
1544 /* if no register assigned to it */
1545 if (!sym->nRegs || sym->isspilt)
1548 /* for all the registers allocated to it */
1549 for (k = 0; k < sym->nRegs; k++)
1552 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1558 /*-----------------------------------------------------------------*/
1559 /* rematStr - returns the rematerialized string for a remat var */
1560 /*-----------------------------------------------------------------*/
1562 rematStr (symbol * sym)
1565 iCode *ic = sym->rematiCode;
1570 /* if plus or minus print the right hand side */
1571 if (ic->op == '+' || ic->op == '-')
1573 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1576 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1579 /* cast then continue */
1580 if (IS_CAST_ICODE(ic)) {
1581 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1584 /* we reached the end */
1585 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1592 /*-----------------------------------------------------------------*/
1593 /* regTypeNum - computes the type & number of registers required */
1594 /*-----------------------------------------------------------------*/
1602 /* for each live range do */
1603 for (sym = hTabFirstItem (liveRanges, &k); sym;
1604 sym = hTabNextItem (liveRanges, &k))
1607 /* if used zero times then no registers needed */
1608 if ((sym->liveTo - sym->liveFrom) == 0)
1612 /* if the live range is a temporary */
1616 /* if the type is marked as a conditional */
1617 if (sym->regType == REG_CND)
1620 /* if used in return only then we don't
1622 if (sym->ruonly || sym->accuse)
1624 if (IS_AGGREGATE (sym->type) || sym->isptr)
1625 sym->type = aggrToPtr (sym->type, FALSE);
1629 /* if the symbol has only one definition &
1630 that definition is a get_pointer and the
1631 pointer we are getting is rematerializable and
1634 if (bitVectnBitsOn (sym->defs) == 1 &&
1635 (ic = hTabItemWithKey (iCodehTab,
1636 bitVectFirstBit (sym->defs))) &&
1639 !IS_BITVAR (sym->etype))
1643 /* if remat in data space */
1644 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1645 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1646 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1649 /* create a psuedo symbol & force a spil */
1650 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1651 psym->type = sym->type;
1652 psym->etype = sym->etype;
1653 strcpy (psym->rname, psym->name);
1655 sym->usl.spillLoc = psym;
1659 /* if in data space or idata space then try to
1660 allocate pointer register */
1664 /* if not then we require registers */
1665 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1666 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1667 getSize (sym->type));
1671 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1672 printTypeChain (sym->type, stderr);
1673 fprintf (stderr, "\n");
1676 /* determine the type of register required */
1677 if (sym->nRegs == 1 &&
1678 IS_PTR (sym->type) &&
1680 sym->regType = REG_PTR;
1682 sym->regType = REG_GPR;
1686 /* for the first run we don't provide */
1687 /* registers for true symbols we will */
1688 /* see how things go */
1694 /*-----------------------------------------------------------------*/
1695 /* freeAllRegs - mark all registers as free */
1696 /*-----------------------------------------------------------------*/
1702 for (i = 0; i < ds390_nRegs; i++)
1703 regs390[i].isFree = 1;
1706 /*-----------------------------------------------------------------*/
1707 /* deallocStackSpil - this will set the stack pointer back */
1708 /*-----------------------------------------------------------------*/
1710 DEFSETFUNC (deallocStackSpil)
1718 /*-----------------------------------------------------------------*/
1719 /* farSpacePackable - returns the packable icode for far variables */
1720 /*-----------------------------------------------------------------*/
1722 farSpacePackable (iCode * ic)
1726 /* go thru till we find a definition for the
1727 symbol on the right */
1728 for (dic = ic->prev; dic; dic = dic->prev)
1731 /* if the definition is a call then no */
1732 if ((dic->op == CALL || dic->op == PCALL) &&
1733 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1738 /* if shift by unknown amount then not */
1739 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1740 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1743 /* if pointer get and size > 1 */
1744 if (POINTER_GET (dic) &&
1745 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1748 if (POINTER_SET (dic) &&
1749 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1752 /* if any three is a true symbol in far space */
1753 if (IC_RESULT (dic) &&
1754 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1755 isOperandInFarSpace (IC_RESULT (dic)))
1758 if (IC_RIGHT (dic) &&
1759 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1760 isOperandInFarSpace (IC_RIGHT (dic)) &&
1761 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1764 if (IC_LEFT (dic) &&
1765 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1766 isOperandInFarSpace (IC_LEFT (dic)) &&
1767 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1770 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1772 if ((dic->op == LEFT_OP ||
1773 dic->op == RIGHT_OP ||
1775 IS_OP_LITERAL (IC_RIGHT (dic)))
1785 /*-----------------------------------------------------------------*/
1786 /* packRegsForAssign - register reduction for assignment */
1787 /*-----------------------------------------------------------------*/
1789 packRegsForAssign (iCode * ic, eBBlock * ebp)
1793 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1794 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1795 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1800 /* if the true symbol is defined in far space or on stack
1801 then we should not since this will increase register pressure */
1803 if (isOperandInFarSpace (IC_RESULT (ic)))
1805 if ((dic = farSpacePackable (ic)))
1811 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1816 /* find the definition of iTempNN scanning backwards if we find a
1817 a use of the true symbol in before we find the definition then
1819 for (dic = ic->prev; dic; dic = dic->prev)
1821 /* if there is a function call then don't pack it */
1822 if ((dic->op == CALL || dic->op == PCALL))
1831 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1832 IS_OP_VOLATILE (IC_RESULT (dic)))
1838 if (IS_SYMOP (IC_RESULT (dic)) &&
1839 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1841 if (POINTER_SET (dic))
1847 if (IS_SYMOP (IC_RIGHT (dic)) &&
1848 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1849 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1855 if (IS_SYMOP (IC_LEFT (dic)) &&
1856 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1857 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1863 if (POINTER_SET (dic) &&
1864 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1872 return 0; /* did not find */
1874 /* if the result is on stack or iaccess then it must be
1875 the same atleast one of the operands */
1876 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1877 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1880 /* the operation has only one symbol
1881 operator then we can pack */
1882 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1883 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1886 if (!((IC_LEFT (dic) &&
1887 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1889 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1893 /* found the definition */
1894 /* replace the result with the result of */
1895 /* this assignment and remove this assignment */
1896 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1897 IC_RESULT (dic) = IC_RESULT (ic);
1899 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1901 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1903 /* delete from liverange table also
1904 delete from all the points inbetween and the new
1906 for (sic = dic; sic != ic; sic = sic->next)
1908 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1909 if (IS_ITEMP (IC_RESULT (dic)))
1910 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1913 remiCodeFromeBBlock (ebp, ic);
1914 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1915 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1916 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1921 /*-----------------------------------------------------------------*/
1922 /* findAssignToSym : scanning backwards looks for first assig found */
1923 /*-----------------------------------------------------------------*/
1925 findAssignToSym (operand * op, iCode * ic)
1929 for (dic = ic->prev; dic; dic = dic->prev)
1932 /* if definition by assignment */
1933 if (dic->op == '=' &&
1934 !POINTER_SET (dic) &&
1935 IC_RESULT (dic)->key == op->key
1936 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1940 /* we are interested only if defined in far space */
1941 /* or in stack space in case of + & - */
1943 /* if assigned to a non-symbol then return
1945 if (!IS_SYMOP (IC_RIGHT (dic)))
1948 /* if the symbol is in far space then
1950 if (isOperandInFarSpace (IC_RIGHT (dic)))
1953 /* for + & - operations make sure that
1954 if it is on the stack it is the same
1955 as one of the three operands */
1956 if ((ic->op == '+' || ic->op == '-') &&
1957 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1960 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1961 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1962 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1970 /* if we find an usage then we cannot delete it */
1971 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1974 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1977 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1981 /* now make sure that the right side of dic
1982 is not defined between ic & dic */
1985 iCode *sic = dic->next;
1987 for (; sic != ic; sic = sic->next)
1988 if (IC_RESULT (sic) &&
1989 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1998 /*-----------------------------------------------------------------*/
1999 /* packRegsForSupport :- reduce some registers for support calls */
2000 /*-----------------------------------------------------------------*/
2002 packRegsForSupport (iCode * ic, eBBlock * ebp)
2005 /* for the left & right operand :- look to see if the
2006 left was assigned a true symbol in far space in that
2007 case replace them */
2008 if (IS_ITEMP (IC_LEFT (ic)) &&
2009 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2011 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2017 /* found it we need to remove it from the
2019 for (sic = dic; sic != ic; sic = sic->next)
2020 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2022 IC_LEFT (ic)->operand.symOperand =
2023 IC_RIGHT (dic)->operand.symOperand;
2024 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2025 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2026 remiCodeFromeBBlock (ebp, dic);
2027 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2031 /* do the same for the right operand */
2034 IS_ITEMP (IC_RIGHT (ic)) &&
2035 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2037 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2043 /* if this is a subtraction & the result
2044 is a true symbol in far space then don't pack */
2045 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2047 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2048 if (IN_FARSPACE (SPEC_OCLS (etype)))
2051 /* found it we need to remove it from the
2053 for (sic = dic; sic != ic; sic = sic->next)
2054 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2056 IC_RIGHT (ic)->operand.symOperand =
2057 IC_RIGHT (dic)->operand.symOperand;
2058 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2060 remiCodeFromeBBlock (ebp, dic);
2061 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2062 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2069 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2072 /*-----------------------------------------------------------------*/
2073 /* packRegsDPTRuse : - will reduce some registers for single Use */
2074 /*-----------------------------------------------------------------*/
2076 packRegsDPTRuse (operand * op)
2078 /* go thru entire liveRange of this variable & check for
2079 other possible usage of DPTR , if we don't find it the
2080 assign this to DPTR (ruonly)
2085 sym_link *type, *etype;
2087 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2088 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2090 /* first check if any overlapping liverange has already been
2092 if (OP_SYMBOL(op)->clashes) {
2093 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2094 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2095 sym = hTabItemWithKey(liveRanges,i);
2096 if (sym->ruonly) return NULL ;
2101 /* no then go thru this guys live range */
2102 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2103 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2104 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2106 if (SKIP_IC3(ic)) continue;
2108 /* if PCALL cannot be sure give up */
2109 if (ic->op == PCALL) return NULL;
2111 /* if CALL then make sure it is VOID || return value not used */
2112 if (ic->op == CALL) {
2113 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2114 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2115 etype = getSpec(type = operandType(IC_RESULT(ic)));
2116 if (getSize(type) == 0) continue ;
2120 /* special case of add with a [remat] */
2121 if (ic->op == '+' &&
2122 OP_SYMBOL(IC_LEFT(ic))->remat &&
2123 (!isOperandInFarSpace(IC_RIGHT(ic)) ||
2124 isOperandInReg(IC_RIGHT(ic)))) continue ;
2128 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2129 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2132 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2133 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2135 /* conditionals can destroy 'b' - make sure B wont be used in this one*/
2136 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ) &&
2137 getSize(operandType(op)) > 3) return NULL;
2140 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2141 !isOperandEqual(IC_RESULT(ic),op) &&
2142 ((isOperandInFarSpace(IC_RESULT(ic)) && !isOperandInReg(IC_RESULT(ic))) ||
2143 OP_SYMBOL(IC_RESULT(ic))->ruonly ||
2144 OP_SYMBOL(IC_RESULT(ic))->onStack)) return NULL;
2146 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2147 !isOperandEqual(IC_RIGHT(ic),op) &&
2148 (OP_SYMBOL(IC_RIGHT(ic))->liveTo > ic->seq ||
2149 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2150 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2151 ((isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic)))||
2152 OP_SYMBOL(IC_RIGHT(ic))->onStack)) return NULL;
2154 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2155 !isOperandEqual(IC_LEFT(ic),op) &&
2156 (OP_SYMBOL(IC_LEFT(ic))->liveTo > ic->seq ||
2157 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2158 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2159 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic)))||
2160 OP_SYMBOL(IC_LEFT(ic))->onStack)) return NULL;
2162 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2163 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2164 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2165 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2168 OP_SYMBOL(op)->ruonly = 1;
2172 /*-----------------------------------------------------------------*/
2173 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2174 /*-----------------------------------------------------------------*/
2176 isBitwiseOptimizable (iCode * ic)
2178 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2179 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2181 /* bitwise operations are considered optimizable
2182 under the following conditions (Jean-Louis VERN)
2194 if ( IS_LITERAL (rtype) ||
2195 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2201 /*-----------------------------------------------------------------*/
2202 /* packRegsForAccUse - pack registers for acc use */
2203 /*-----------------------------------------------------------------*/
2205 packRegsForAccUse (iCode * ic)
2209 /* if this is an aggregate, e.g. a one byte char array */
2210 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2214 /* if + or - then it has to be one byte result */
2215 if ((ic->op == '+' || ic->op == '-')
2216 && getSize (operandType (IC_RESULT (ic))) > 1)
2219 /* if shift operation make sure right side is not a literal */
2220 if (ic->op == RIGHT_OP &&
2221 (isOperandLiteral (IC_RIGHT (ic)) ||
2222 getSize (operandType (IC_RESULT (ic))) > 1))
2225 if (ic->op == LEFT_OP &&
2226 (isOperandLiteral (IC_RIGHT (ic)) ||
2227 getSize (operandType (IC_RESULT (ic))) > 1))
2230 if (IS_BITWISE_OP (ic) &&
2231 getSize (operandType (IC_RESULT (ic))) > 1)
2235 /* has only one definition */
2236 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2239 /* has only one use */
2240 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2243 /* and the usage immediately follows this iCode */
2244 if (!(uic = hTabItemWithKey (iCodehTab,
2245 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2248 if (ic->next != uic)
2251 /* if it is a conditional branch then we definitely can */
2255 if (uic->op == JUMPTABLE)
2258 /* if the usage is not is an assignment
2259 or an arithmetic / bitwise / shift operation then not */
2260 if (POINTER_SET (uic) &&
2261 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2264 if (uic->op != '=' &&
2265 !IS_ARITHMETIC_OP (uic) &&
2266 !IS_BITWISE_OP (uic) &&
2267 uic->op != LEFT_OP &&
2268 uic->op != RIGHT_OP)
2271 /* if used in ^ operation then make sure right is not a
2273 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2276 /* if shift operation make sure right side is not a literal */
2277 if (uic->op == RIGHT_OP &&
2278 (isOperandLiteral (IC_RIGHT (uic)) ||
2279 getSize (operandType (IC_RESULT (uic))) > 1))
2282 if (uic->op == LEFT_OP &&
2283 (isOperandLiteral (IC_RIGHT (uic)) ||
2284 getSize (operandType (IC_RESULT (uic))) > 1))
2287 /* make sure that the result of this icode is not on the
2288 stack, since acc is used to compute stack offset */
2290 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2291 OP_SYMBOL (IC_RESULT (uic))->onStack)
2294 if (isOperandOnStack(IC_RESULT(uic)))
2298 /* if either one of them in far space then we cannot */
2299 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2300 isOperandInFarSpace (IC_LEFT (uic))) ||
2301 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2302 isOperandInFarSpace (IC_RIGHT (uic))))
2305 /* if the usage has only one operand then we can */
2306 if (IC_LEFT (uic) == NULL ||
2307 IC_RIGHT (uic) == NULL)
2310 /* make sure this is on the left side if not
2311 a '+' since '+' is commutative */
2312 if (ic->op != '+' &&
2313 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2317 // this is too dangerous and need further restrictions
2320 /* if one of them is a literal then we can */
2321 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2322 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2324 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2329 /* if the other one is not on stack then we can */
2330 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2331 (IS_ITEMP (IC_RIGHT (uic)) ||
2332 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2333 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2336 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2337 (IS_ITEMP (IC_LEFT (uic)) ||
2338 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2339 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2345 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2350 /*-----------------------------------------------------------------*/
2351 /* packForPush - hueristics to reduce iCode for pushing */
2352 /*-----------------------------------------------------------------*/
2354 packForPush (iCode * ic, eBBlock * ebp)
2359 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2362 /* must have only definition & one usage */
2363 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2364 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2367 /* find the definition */
2368 if (!(dic = hTabItemWithKey (iCodehTab,
2369 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2372 if (dic->op != '=' || POINTER_SET (dic))
2375 /* make sure the right side does not have any definitions
2377 dbv = OP_DEFS(IC_RIGHT(dic));
2378 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2379 if (bitVectBitValue(dbv,lic->key)) return ;
2381 /* make sure they have the same type */
2383 sym_link *itype=operandType(IC_LEFT(ic));
2384 sym_link *ditype=operandType(IC_RIGHT(dic));
2386 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2387 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2390 /* extend the live range of replaced operand if needed */
2391 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2392 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2394 /* we now we know that it has one & only one def & use
2395 and the that the definition is an assignment */
2396 IC_LEFT (ic) = IC_RIGHT (dic);
2398 remiCodeFromeBBlock (ebp, dic);
2399 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2400 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2403 /*-----------------------------------------------------------------*/
2404 /* packRegisters - does some transformations to reduce register */
2406 /*-----------------------------------------------------------------*/
2408 packRegisters (eBBlock * ebp)
2418 /* look for assignments of the form */
2419 /* iTempNN = TRueSym (someoperation) SomeOperand */
2421 /* TrueSym := iTempNN:1 */
2422 for (ic = ebp->sch; ic; ic = ic->next)
2426 /* find assignment of the form TrueSym := iTempNN:1 */
2427 if (ic->op == '=' && !POINTER_SET (ic))
2428 change += packRegsForAssign (ic, ebp);
2435 for (ic = ebp->sch; ic; ic = ic->next)
2438 /* if this is an itemp & result of a address of a true sym
2439 then mark this as rematerialisable */
2440 if (ic->op == ADDRESS_OF &&
2441 IS_ITEMP (IC_RESULT (ic)) &&
2442 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2443 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2444 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2447 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2448 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2449 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2453 /* if straight assignment then carry remat flag if
2454 this is the only definition */
2455 if (ic->op == '=' &&
2456 !POINTER_SET (ic) &&
2457 IS_SYMOP (IC_RIGHT (ic)) &&
2458 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2459 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2460 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2463 OP_SYMBOL (IC_RESULT (ic))->remat =
2464 OP_SYMBOL (IC_RIGHT (ic))->remat;
2465 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2466 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2469 /* if cast to a generic pointer & the pointer being
2470 cast is remat, then we can remat this cast as well */
2471 if (ic->op == CAST &&
2472 IS_SYMOP(IC_RIGHT(ic)) &&
2473 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2474 sym_link *to_type = operandType(IC_LEFT(ic));
2475 sym_link *from_type = operandType(IC_RIGHT(ic));
2476 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2477 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2478 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2479 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2483 /* if this is a +/- operation with a rematerizable
2484 then mark this as rematerializable as well */
2485 if ((ic->op == '+' || ic->op == '-') &&
2486 (IS_SYMOP (IC_LEFT (ic)) &&
2487 IS_ITEMP (IC_RESULT (ic)) &&
2488 OP_SYMBOL (IC_LEFT (ic))->remat &&
2489 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2490 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2491 IS_OP_LITERAL (IC_RIGHT (ic))))
2494 //int i = operandLitValue(IC_RIGHT(ic));
2495 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2496 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2497 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2500 /* mark the pointer usages */
2501 if (POINTER_SET (ic))
2502 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2504 if (POINTER_GET (ic))
2505 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2509 /* if we are using a symbol on the stack
2510 then we should say ds390_ptrRegReq */
2511 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2512 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2513 OP_SYMBOL (IC_COND (ic))->iaccess);
2514 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2515 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2516 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2519 if (IS_SYMOP (IC_LEFT (ic)))
2520 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2521 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2522 if (IS_SYMOP (IC_RIGHT (ic)))
2523 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2524 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2525 if (IS_SYMOP (IC_RESULT (ic)))
2526 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2527 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2532 /* if the condition of an if instruction
2533 is defined in the previous instruction then
2534 mark the itemp as a conditional */
2535 if ((IS_CONDITIONAL (ic) ||
2536 (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2537 ic->next && ic->next->op == IFX &&
2538 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2539 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2542 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2546 /* if the condition of an if instruction
2547 is defined in the previous instruction and
2548 this is the only usage then
2549 mark the itemp as a conditional */
2550 if ((IS_CONDITIONAL (ic) ||
2551 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2552 ic->next && ic->next->op == IFX &&
2553 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2554 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2555 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2557 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2562 /* reduce for support function calls */
2563 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2564 packRegsForSupport (ic, ebp);
2566 /* some cases the redundant moves can
2567 can be eliminated for return statements */
2568 if ((ic->op == RETURN || ic->op == SEND) &&
2569 !isOperandInFarSpace (IC_LEFT (ic)) &&
2572 packRegsDPTRuse (IC_LEFT (ic));
2575 if ((ic->op == CALL && getSize(operandType(IC_RESULT(ic))) <= 4)) {
2576 packRegsDPTRuse (IC_RESULT (ic));
2579 /* if pointer set & left has a size more than
2580 one and right is not in far space */
2581 if (POINTER_SET (ic) &&
2582 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2583 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2584 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2585 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2587 packRegsDPTRuse (IC_RESULT (ic));
2590 /* if pointer get */
2591 if (POINTER_GET (ic) &&
2592 !isOperandInFarSpace (IC_RESULT (ic)) &&
2593 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2594 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2595 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2597 packRegsDPTRuse (IC_LEFT (ic));
2600 /* if this is cast for intergral promotion then
2601 check if only use of the definition of the
2602 operand being casted/ if yes then replace
2603 the result of that arithmetic operation with
2604 this result and get rid of the cast */
2607 sym_link *fromType = operandType (IC_RIGHT (ic));
2608 sym_link *toType = operandType (IC_LEFT (ic));
2610 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2611 getSize (fromType) != getSize (toType) &&
2612 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2615 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2618 if (IS_ARITHMETIC_OP (dic))
2620 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2621 IC_RESULT (dic) = IC_RESULT (ic);
2622 remiCodeFromeBBlock (ebp, ic);
2623 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2624 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2625 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2629 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2635 /* if the type from and type to are the same
2636 then if this is the only use then packit */
2637 if (compareType (operandType (IC_RIGHT (ic)),
2638 operandType (IC_LEFT (ic))) == 1)
2640 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2643 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2644 IC_RESULT (dic) = IC_RESULT (ic);
2645 remiCodeFromeBBlock (ebp, ic);
2646 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2647 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2648 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2656 iTempNN := (some variable in farspace) V1
2661 if (ic->op == IPUSH)
2663 packForPush (ic, ebp);
2667 /* pack registers for accumulator use, when the
2668 result of an arithmetic or bit wise operation
2669 has only one use, that use is immediately following
2670 the defintion and the using iCode has only one
2671 operand or has two operands but one is literal &
2672 the result of that operation is not on stack then
2673 we can leave the result of this operation in acc:b
2675 if ((IS_ARITHMETIC_OP (ic)
2676 || IS_CONDITIONAL(ic)
2677 || IS_BITWISE_OP (ic)
2678 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2679 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2681 IS_ITEMP (IC_RESULT (ic)) &&
2682 getSize (operandType (IC_RESULT (ic))) <= 2)
2684 packRegsForAccUse (ic);
2689 /*-----------------------------------------------------------------*/
2690 /* assignRegisters - assigns registers to each live range as need */
2691 /*-----------------------------------------------------------------*/
2693 ds390_assignRegisters (eBBlock ** ebbs, int count)
2698 setToNull ((void *) &_G.funcrUsed);
2699 setToNull ((void *) &_G.regAssigned);
2700 setToNull ((void *) &_G.totRegAssigned);
2701 setToNull ((void *) &_G.funcrUsed);
2702 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2704 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2705 /* change assignments this will remove some
2706 live ranges reducing some register pressure */
2707 for (i = 0; i < count; i++)
2708 packRegisters (ebbs[i]);
2710 if (options.dump_pack)
2711 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2713 /* first determine for each live range the number of
2714 registers & the type of registers required for each */
2717 /* and serially allocate registers */
2718 serialRegAssign (ebbs, count);
2725 /* if stack was extended then tell the user */
2728 /* werror(W_TOOMANY_SPILS,"stack", */
2729 /* _G.stackExtend,currFunc->name,""); */
2735 /* werror(W_TOOMANY_SPILS,"data space", */
2736 /* _G.dataExtend,currFunc->name,""); */
2740 /* after that create the register mask
2741 for each of the instruction */
2742 createRegMask (ebbs, count);
2744 /* redo that offsets for stacked automatic variables */
2745 redoStackOffsets ();
2747 if (options.dump_rassgn) {
2748 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2749 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2752 /* do the overlaysegment stuff SDCCmem.c */
2753 doOverlays (ebbs, count);
2755 /* now get back the chain */
2756 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2761 /* free up any _G.stackSpil locations allocated */
2762 applyToSet (_G.stackSpil, deallocStackSpil);
2764 setToNull ((void **) &_G.stackSpil);
2765 setToNull ((void **) &_G.spiltSet);
2766 /* mark all registers as free */