1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (DS80C390) 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 */
53 bitVect *allBitregs; /* all bit registers */
57 /* Shared with gen.c */
58 int ds390_ptrRegReq; /* one byte pointer register required */
64 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
65 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
66 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
67 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
68 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
69 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
70 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
71 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
72 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
73 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
74 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
75 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
76 {REG_BIT, B0_IDX, REG_BIT, "b0", "b0", "bits", 0, 1, 0},
77 {REG_BIT, B1_IDX, REG_BIT, "b1", "b1", "bits", 1, 1, 0},
78 {REG_BIT, B2_IDX, REG_BIT, "b2", "b2", "bits", 2, 1, 0},
79 {REG_BIT, B3_IDX, REG_BIT, "b3", "b3", "bits", 3, 1, 0},
80 {REG_BIT, B4_IDX, REG_BIT, "b4", "b4", "bits", 4, 1, 0},
81 {REG_BIT, B5_IDX, REG_BIT, "b5", "b5", "bits", 5, 1, 0},
82 {REG_BIT, B6_IDX, REG_BIT, "b6", "b6", "bits", 6, 1, 0},
83 {REG_BIT, B7_IDX, REG_BIT, "b7", "b7", "bits", 7, 1, 0},
84 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
85 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
86 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
87 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
88 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
89 {REG_CND, CND_IDX, REG_GPR, "C", "psw", "xreg", 0, 0, 0},
90 {0, DPL1_IDX, 0, "dpl1", "dpl1", "dpl1", 0, 0, 0},
91 {0, DPH1_IDX, 0, "dph1", "dph1", "dph1", 0, 0, 0},
92 {0, DPX1_IDX, 0, "dpx1", "dpx1", "dpx1", 0, 0, 0},
93 {0, DPS_IDX, 0, "dps", "dps", "dps", 0, 0, 0},
94 {0, A_IDX, 0, "a", "acc", "acc", 0, 0, 0},
95 {0, AP_IDX, 0, "ap", "ap", "ap", 0, 0, 0},
99 int ds390_nBitRegs = 0;
101 static void spillThis (symbol *);
102 static void freeAllRegs ();
103 static iCode * packRegsDPTRuse (operand *);
104 static int packRegsDPTRnuse (operand *,unsigned);
106 /*-----------------------------------------------------------------*/
107 /* allocReg - allocates register of given type */
108 /*-----------------------------------------------------------------*/
110 allocReg (short type)
114 for (i = 0; i < ds390_nRegs; i++)
117 /* if type is given as 0 then any
118 free register will do */
122 regs390[i].isFree = 0;
124 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
127 /* otherwise look for specific type
129 if (regs390[i].isFree &&
130 regs390[i].type == type)
132 regs390[i].isFree = 0;
134 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
141 /*-----------------------------------------------------------------*/
142 /* ds390_regWithIdx - returns pointer to register with index number*/
143 /*-----------------------------------------------------------------*/
145 ds390_regWithIdx (int idx)
149 for (i = 0; i < sizeof(regs390)/sizeof(regs); i++)
150 if (regs390[i].rIdx == idx)
153 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
154 "regWithIdx not found");
158 /*-----------------------------------------------------------------*/
159 /* freeReg - frees a register */
160 /*-----------------------------------------------------------------*/
166 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
167 "freeReg - Freeing NULL register");
174 /*-----------------------------------------------------------------*/
175 /* useReg - marks a register as used */
176 /*-----------------------------------------------------------------*/
183 /*-----------------------------------------------------------------*/
184 /* nFreeRegs - returns number of free registers */
185 /*-----------------------------------------------------------------*/
192 for (i = 0; i < ds390_nRegs; i++)
193 if (regs390[i].isFree && regs390[i].type == type)
198 /*-----------------------------------------------------------------*/
199 /* nfreeRegsType - free registers with type */
200 /*-----------------------------------------------------------------*/
202 nfreeRegsType (int type)
207 if ((nfr = nFreeRegs (type)) == 0)
208 return nFreeRegs (REG_GPR);
211 return nFreeRegs (type);
214 /*-----------------------------------------------------------------*/
215 /* isOperandInReg - returns true if operand is currently in regs */
216 /*-----------------------------------------------------------------*/
217 static int isOperandInReg(operand *op)
219 if (!IS_SYMOP(op)) return 0;
220 if (OP_SYMBOL(op)->ruonly) return 1;
221 if (OP_SYMBOL(op)->accuse) return 1;
222 if (OP_SYMBOL(op)->dptr) return 1;
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 /* bitType - will return 1 if the symbol has type REG_BIT */
254 /*-----------------------------------------------------------------*/
256 bitType (symbol * sym, eBBlock * ebp, iCode * ic)
258 return (sym->regType == REG_BIT ? 1 : 0);
261 /*-----------------------------------------------------------------*/
262 /* noSpilLoc - return true if a variable has no spil location */
263 /*-----------------------------------------------------------------*/
265 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
267 return (sym->usl.spillLoc ? 0 : 1);
270 /*-----------------------------------------------------------------*/
271 /* hasSpilLoc - will return 1 if the symbol has spil location */
272 /*-----------------------------------------------------------------*/
274 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
276 return (sym->usl.spillLoc ? 1 : 0);
279 /*-----------------------------------------------------------------*/
280 /* directSpilLoc - will return 1 if the spillocation is in direct */
281 /*-----------------------------------------------------------------*/
283 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
285 if (sym->usl.spillLoc &&
286 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
292 /*-----------------------------------------------------------------*/
293 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
294 /* but is not used as a pointer */
295 /*-----------------------------------------------------------------*/
297 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
299 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
302 /*-----------------------------------------------------------------*/
303 /* rematable - will return 1 if the remat flag is set */
304 /*-----------------------------------------------------------------*/
306 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
311 /*-----------------------------------------------------------------*/
312 /* notUsedInRemaining - not used or defined in remain of the block */
313 /*-----------------------------------------------------------------*/
315 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
317 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
318 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
321 /*-----------------------------------------------------------------*/
322 /* allLRs - return true for all */
323 /*-----------------------------------------------------------------*/
325 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
330 /*-----------------------------------------------------------------*/
331 /* liveRangesWith - applies function to a given set of live range */
332 /*-----------------------------------------------------------------*/
334 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
335 eBBlock * ebp, iCode * ic)
340 if (!lrs || !lrs->size)
343 for (i = 1; i < lrs->size; i++)
346 if (!bitVectBitValue (lrs, i))
349 /* if we don't find it in the live range
350 hash table we are in serious trouble */
351 if (!(sym = hTabItemWithKey (liveRanges, i)))
353 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
354 "liveRangesWith could not find liveRange");
358 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
359 addSetHead (&rset, sym);
366 /*-----------------------------------------------------------------*/
367 /* leastUsedLR - given a set determines which is the least used */
368 /*-----------------------------------------------------------------*/
370 leastUsedLR (set * sset)
372 symbol *sym = NULL, *lsym = NULL;
374 sym = lsym = setFirstItem (sset);
379 for (; lsym; lsym = setNextItem (sset))
382 /* if usage is the same then prefer
383 to spill the smaller of the two */
384 if (lsym->used == sym->used)
385 if (getSize (lsym->type) < getSize (sym->type))
389 if (lsym->used < sym->used)
394 setToNull ((void *) &sset);
399 /*-----------------------------------------------------------------*/
400 /* noOverLap - will iterate through the list looking for over lap */
401 /*-----------------------------------------------------------------*/
403 noOverLap (set * itmpStack, symbol * fsym)
407 for (sym = setFirstItem (itmpStack); sym;
408 sym = setNextItem (itmpStack))
410 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
415 /*-----------------------------------------------------------------*/
416 /* isFree - will return 1 if the a free spil location is found */
417 /*-----------------------------------------------------------------*/
422 V_ARG (symbol **, sloc);
423 V_ARG (symbol *, fsym);
425 /* if already found */
429 /* if it is free && and the itmp assigned to
430 this does not have any overlapping live ranges
431 with the one currently being assigned and
432 the size can be accomodated */
434 noOverLap (sym->usl.itmpStack, fsym) &&
435 getSize (sym->type) >= getSize (fsym->type))
444 /*-----------------------------------------------------------------*/
445 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
446 /*-----------------------------------------------------------------*/
448 spillLRWithPtrReg (symbol * forSym)
454 if (!_G.regAssigned ||
455 bitVectIsZero (_G.regAssigned))
458 r0 = ds390_regWithIdx (R0_IDX);
459 r1 = ds390_regWithIdx (R1_IDX);
461 /* for all live ranges */
462 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
463 lrsym = hTabNextItem (liveRanges, &k))
467 /* if no registers assigned to it or spilt */
468 /* if it does not overlap this then
469 no need to spill it */
471 if (lrsym->isspilt || !lrsym->nRegs ||
472 (lrsym->liveTo < forSym->liveFrom))
475 /* go thru the registers : if it is either
476 r0 or r1 then spill it */
477 for (j = 0; j < lrsym->nRegs; j++)
478 if (lrsym->regs[j] == r0 ||
479 lrsym->regs[j] == r1)
488 /*-----------------------------------------------------------------*/
489 /* createStackSpil - create a location on the stack to spil */
490 /*-----------------------------------------------------------------*/
492 createStackSpil (symbol * sym)
495 int useXstack, model, noOverlay;
499 /* first go try and find a free one that is already
500 existing on the stack */
501 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
503 /* found a free one : just update & return */
504 sym->usl.spillLoc = sloc;
507 addSetHead (&sloc->usl.itmpStack, sym);
511 /* could not then have to create one , this is the hard part
512 we need to allocate this on the stack : this is really a
513 hack!! but cannot think of anything better at this time */
515 if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
516 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
518 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
523 sloc = newiTemp (slocBuffer);
525 /* set the type to the spilling symbol */
526 sloc->type = copyLinkChain (sym->type);
527 sloc->etype = getSpec (sloc->type);
528 if (!IS_BIT (sloc->etype))
530 if (options.model == MODEL_SMALL)
532 SPEC_SCLS (sloc->etype) = S_DATA;
536 SPEC_SCLS (sloc->etype) = S_XDATA;
539 SPEC_EXTR (sloc->etype) = 0;
540 SPEC_STAT (sloc->etype) = 0;
541 SPEC_VOLATILE(sloc->etype) = 0;
542 SPEC_ABSA(sloc->etype) = 0;
544 /* we don't allow it to be allocated
545 onto the external stack since : so we
546 temporarily turn it off ; we also
547 turn off memory model to prevent
548 the spil from going to the external storage
549 and turn off overlaying
552 useXstack = options.useXstack;
553 model = options.model;
554 noOverlay = options.noOverlay;
555 options.noOverlay = 1;
557 /* options.model = options.useXstack = 0; */
561 options.useXstack = useXstack;
562 options.model = model;
563 options.noOverlay = noOverlay;
564 sloc->isref = 1; /* to prevent compiler warning */
566 /* if it is on the stack then update the stack */
567 if (IN_STACK (sloc->etype))
569 currFunc->stack += getSize (sloc->type);
570 _G.stackExtend += getSize (sloc->type);
573 _G.dataExtend += getSize (sloc->type);
575 /* add it to the _G.stackSpil set */
576 addSetHead (&_G.stackSpil, sloc);
577 sym->usl.spillLoc = sloc;
580 /* add it to the set of itempStack set
581 of the spill location */
582 addSetHead (&sloc->usl.itmpStack, sym);
586 /*-----------------------------------------------------------------*/
587 /* isSpiltOnStack - returns true if the spil location is on stack */
588 /*-----------------------------------------------------------------*/
590 isSpiltOnStack (symbol * sym)
600 /* if (sym->_G.stackSpil) */
603 if (!sym->usl.spillLoc)
606 etype = getSpec (sym->usl.spillLoc->type);
607 if (IN_STACK (etype))
613 /*-----------------------------------------------------------------*/
614 /* spillThis - spils a specific operand */
615 /*-----------------------------------------------------------------*/
617 spillThis (symbol * sym)
620 /* if this is rematerializable or has a spillLocation
621 we are okay, else we need to create a spillLocation
623 if (!(sym->remat || sym->usl.spillLoc))
624 createStackSpil (sym);
626 /* mark it has spilt & put it in the spilt set */
627 sym->isspilt = sym->spillA = 1;
628 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
630 bitVectUnSetBit (_G.regAssigned, sym->key);
631 bitVectUnSetBit (_G.totRegAssigned, sym->key);
633 for (i = 0; i < sym->nRegs; i++)
637 freeReg (sym->regs[i]);
641 /* if spilt on stack then free up r0 & r1
642 if they could have been assigned to some
644 if (!ds390_ptrRegReq && isSpiltOnStack (sym) && !options.stack10bit)
647 spillLRWithPtrReg (sym);
650 if (sym->usl.spillLoc && !sym->remat)
651 sym->usl.spillLoc->allocreq++;
655 /*-----------------------------------------------------------------*/
656 /* selectSpil - select a iTemp to spil : rather a simple procedure */
657 /*-----------------------------------------------------------------*/
659 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
661 bitVect *lrcs = NULL;
665 /* get the spillable live ranges */
666 lrcs = computeSpillable (ic);
668 /* remove incompatible registers */
669 if ((forSym->regType == REG_PTR) || (forSym->regType == REG_GPR))
671 selectS = liveRangesWith (lrcs, bitType, ebp, ic);
673 for (sym = setFirstItem (selectS); sym; sym = setNextItem (selectS))
675 bitVectUnSetBit (lrcs, sym->key);
679 /* get all live ranges that are rematerializable */
680 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
682 /* return the least used of these */
683 return leastUsedLR (selectS);
686 /* get live ranges with spillLocations in direct space */
687 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
689 sym = leastUsedLR (selectS);
690 strncpyz (sym->rname,
691 sym->usl.spillLoc->rname[0] ?
692 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
695 /* mark it as allocation required */
696 sym->usl.spillLoc->allocreq++;
700 /* if the symbol is local to the block then */
701 if (forSym->liveTo < ebp->lSeq)
703 /* check if there are any live ranges allocated
704 to registers that are not used in this block */
705 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
707 sym = leastUsedLR (selectS);
708 /* if this is not rematerializable */
717 /* check if there are any live ranges that not
718 used in the remainder of the block */
720 !isiCodeInFunctionCall (ic) &&
721 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
723 sym = leastUsedLR (selectS);
736 /* find live ranges with spillocation && not used as pointers */
737 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
739 sym = leastUsedLR (selectS);
740 /* mark this as allocation required */
741 sym->usl.spillLoc->allocreq++;
745 /* find live ranges with spillocation */
746 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
748 sym = leastUsedLR (selectS);
749 sym->usl.spillLoc->allocreq++;
753 /* couldn't find then we need to create a spil
754 location on the stack , for which one? the least
756 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
758 /* return a created spil location */
759 sym = createStackSpil (leastUsedLR (selectS));
760 sym->usl.spillLoc->allocreq++;
764 /* this is an extreme situation we will spill
765 this one : happens very rarely but it does happen */
771 /*-----------------------------------------------------------------*/
772 /* spilSomething - spil some variable & mark registers as free */
773 /*-----------------------------------------------------------------*/
775 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
780 /* get something we can spil */
781 ssym = selectSpil (ic, ebp, forSym);
783 /* mark it as spilt */
784 ssym->isspilt = ssym->spillA = 1;
785 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
787 /* mark it as not register assigned &
788 take it away from the set */
789 bitVectUnSetBit (_G.regAssigned, ssym->key);
790 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
792 /* mark the registers as free */
793 for (i = 0; i < ssym->nRegs; i++)
795 freeReg (ssym->regs[i]);
797 /* if spilt on stack then free up r0 & r1
798 if they could have been assigned to as gprs */
799 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
802 spillLRWithPtrReg (ssym);
805 /* if this was a block level spil then insert push & pop
806 at the start & end of block respectively */
809 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
810 /* add push to the start of the block */
811 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
812 ebp->sch->next : ebp->sch));
813 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
814 /* add pop to the end of the block */
815 addiCodeToeBBlock (ebp, nic, NULL);
818 /* if spilt because not used in the remainder of the
819 block then add a push before this instruction and
820 a pop at the end of the block */
821 if (ssym->remainSpil)
824 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
825 /* add push just before this instruction */
826 addiCodeToeBBlock (ebp, nic, ic);
828 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
829 /* add pop to the end of the block */
830 addiCodeToeBBlock (ebp, nic, NULL);
839 /*-----------------------------------------------------------------*/
840 /* getRegPtr - will try for PTR if not a GPR type if not spil */
841 /*-----------------------------------------------------------------*/
843 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
849 /* try for a ptr type */
850 if ((reg = allocReg (REG_PTR)))
853 /* try for gpr type */
854 if ((reg = allocReg (REG_GPR)))
857 /* we have to spil */
858 if (!spilSomething (ic, ebp, sym))
861 /* make sure partially assigned registers aren't reused */
862 for (j=0; j<=sym->nRegs; j++)
864 sym->regs[j]->isFree = 0;
866 /* this looks like an infinite loop but
867 in really selectSpil will abort */
871 /*-----------------------------------------------------------------*/
872 /* getRegGpr - will try for GPR if not spil */
873 /*-----------------------------------------------------------------*/
875 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
881 /* try for gpr type */
882 if ((reg = allocReg (REG_GPR)))
885 if (!ds390_ptrRegReq)
886 if ((reg = allocReg (REG_PTR)))
889 /* we have to spil */
890 if (!spilSomething (ic, ebp, sym))
893 /* make sure partially assigned registers aren't reused */
894 for (j=0; j<=sym->nRegs; j++)
896 sym->regs[j]->isFree = 0;
898 /* this looks like an infinite loop but
899 in really selectSpil will abort */
903 /*-----------------------------------------------------------------*/
904 /* getRegBit - will try for Bit if not spill this */
905 /*-----------------------------------------------------------------*/
906 static regs *getRegBit (symbol * sym)
910 /* try for a bit type */
911 if ((reg = allocReg (REG_BIT)))
918 /*-----------------------------------------------------------------*/
919 /* getRegPtrNoSpil - get it cannot be spilt */
920 /*-----------------------------------------------------------------*/
921 static regs *getRegPtrNoSpil()
925 /* try for a ptr type */
926 if ((reg = allocReg (REG_PTR)))
929 /* try for gpr type */
930 if ((reg = allocReg (REG_GPR)))
935 /* just to make the compiler happy */
939 /*-----------------------------------------------------------------*/
940 /* getRegGprNoSpil - get it cannot be spilt */
941 /*-----------------------------------------------------------------*/
942 static regs *getRegGprNoSpil()
946 if ((reg = allocReg (REG_GPR)))
949 if (!ds390_ptrRegReq)
950 if ((reg = allocReg (REG_PTR)))
955 /* just to make the compiler happy */
959 /*-----------------------------------------------------------------*/
960 /* getRegBitNoSpil - get it cannot be spilt */
961 /*-----------------------------------------------------------------*/
962 static regs *getRegBitNoSpil()
966 /* try for a bit type */
967 if ((reg = allocReg (REG_BIT)))
970 /* try for gpr type */
971 if ((reg = allocReg (REG_GPR)))
976 /* just to make the compiler happy */
980 /*-----------------------------------------------------------------*/
981 /* symHasReg - symbol has a given register */
982 /*-----------------------------------------------------------------*/
984 symHasReg (symbol * sym, regs * reg)
988 for (i = 0; i < sym->nRegs; i++)
989 if (sym->regs[i] == reg)
995 /*-----------------------------------------------------------------*/
996 /* updateRegUsage - update the registers in use at the start of */
998 /*-----------------------------------------------------------------*/
1000 updateRegUsage (iCode * ic)
1004 for (reg=0; reg<ds390_nRegs; reg++)
1006 if (regs390[reg].isFree)
1008 ic->riu &= ~(1<<regs390[reg].offset);
1012 ic->riu |= (1<<regs390[reg].offset);
1013 BitBankUsed |= (reg >= B0_IDX);
1018 /*-----------------------------------------------------------------*/
1019 /* deassignLRs - check the live to and if they have registers & are */
1020 /* not spilt then free up the registers */
1021 /*-----------------------------------------------------------------*/
1023 deassignLRs (iCode * ic, eBBlock * ebp)
1029 for (sym = hTabFirstItem (liveRanges, &k); sym;
1030 sym = hTabNextItem (liveRanges, &k))
1033 symbol *psym = NULL;
1034 /* if it does not end here */
1035 if (sym->liveTo > ic->seq)
1038 /* if it was spilt on stack then we can
1039 mark the stack spil location as free */
1044 sym->usl.spillLoc->isFree = 1;
1050 if (!bitVectBitValue (_G.regAssigned, sym->key))
1053 /* special case check if this is an IFX &
1054 the privious one was a pop and the
1055 previous one was not spilt then keep track
1057 if (ic->op == IFX && ic->prev &&
1058 ic->prev->op == IPOP &&
1059 !ic->prev->parmPush &&
1060 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1061 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1067 bitVectUnSetBit (_G.regAssigned, sym->key);
1069 /* if the result of this one needs registers
1070 and does not have it then assign it right
1072 if (IC_RESULT (ic) &&
1073 !(SKIP_IC2 (ic) || /* not a special icode */
1074 ic->op == JUMPTABLE ||
1079 POINTER_SET (ic)) &&
1080 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1081 result->liveTo > ic->seq && /* and will live beyond this */
1082 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1083 result->liveFrom == ic->seq && /* does not start before here */
1084 result->regType == sym->regType && /* same register types */
1085 result->nRegs && /* which needs registers */
1086 !result->isspilt && /* and does not already have them */
1088 !bitVectBitValue (_G.regAssigned, result->key) &&
1089 /* the number of free regs + number of regs in this LR
1090 can accomodate the what result Needs */
1091 ((nfreeRegsType (result->regType) +
1092 sym->nRegs) >= result->nRegs)
1096 for (i = 0; i < result->nRegs; i++)
1098 result->regs[i] = sym->regs[i];
1100 result->regs[i] = getRegGpr (ic, ebp, result);
1102 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1103 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1107 /* free the remaining */
1108 for (; i < sym->nRegs; i++)
1112 if (!symHasReg (psym, sym->regs[i]))
1113 freeReg (sym->regs[i]);
1116 freeReg (sym->regs[i]);
1123 /*-----------------------------------------------------------------*/
1124 /* reassignLR - reassign this to registers */
1125 /*-----------------------------------------------------------------*/
1127 reassignLR (operand * op)
1129 symbol *sym = OP_SYMBOL (op);
1132 /* not spilt any more */
1133 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1134 bitVectUnSetBit (_G.spiltSet, sym->key);
1136 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1137 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1141 for (i = 0; i < sym->nRegs; i++)
1142 sym->regs[i]->isFree = 0;
1145 /*-----------------------------------------------------------------*/
1146 /* willCauseSpill - determines if allocating will cause a spill */
1147 /*-----------------------------------------------------------------*/
1149 willCauseSpill (int nr, int rt)
1151 /* first check if there are any available registers
1152 of the type required */
1155 /* special case for pointer type
1156 if pointer type not avlb then
1157 check for type gpr */
1158 if (nFreeRegs (rt) >= nr)
1160 if (nFreeRegs (REG_GPR) >= nr)
1163 else if (rt == REG_BIT)
1165 if (nFreeRegs (rt) >= nr)
1170 if (ds390_ptrRegReq)
1172 if (nFreeRegs (rt) >= nr)
1177 if (nFreeRegs (REG_PTR) +
1178 nFreeRegs (REG_GPR) >= nr)
1183 /* it will cause a spil */
1187 /*-----------------------------------------------------------------*/
1188 /* positionRegs - the allocator can allocate same registers to res- */
1189 /* ult and operand, if this happens make sure they are in the same */
1190 /* position as the operand otherwise chaos results */
1191 /*-----------------------------------------------------------------*/
1193 positionRegs (symbol * result, symbol * opsym)
1195 int count = min (result->nRegs, opsym->nRegs);
1196 int i, j = 0, shared = 0;
1199 /* if the result has been spilt then cannot share */
1204 /* first make sure that they actually share */
1205 for (i = 0; i < count; i++)
1207 for (j = 0; j < count; j++)
1209 if (result->regs[i] == opsym->regs[j] && i != j)
1219 regs *tmp = result->regs[i];
1220 result->regs[i] = result->regs[j];
1221 result->regs[j] = tmp;
1228 /*-----------------------------------------------------------------*/
1229 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1230 /*-----------------------------------------------------------------*/
1231 bitVect *unusedLRs (eBBlock *ebp)
1233 bitVect *ret = NULL;
1237 if (!ebp) return NULL;
1238 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1239 sym = hTabNextItem(liveRanges,&key)) {
1241 if (notUsedInBlock(sym,ebp,NULL)) {
1242 ret = bitVectSetBit(ret,sym->key);
1249 /*-----------------------------------------------------------------*/
1250 /* deassignUnsedLRs - if this baisc block ends in a return then */
1251 /* deassign symbols not used in this block */
1252 /*-----------------------------------------------------------------*/
1253 bitVect *deassignUnsedLRs(eBBlock *ebp)
1255 bitVect *unused = NULL;
1258 switch (returnAtEnd(ebp)) {
1259 case 2: /* successor block ends in a return */
1260 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1262 case 1: /* this block ends in a return */
1263 unused = bitVectIntersect(unused,unusedLRs(ebp));
1268 for (i = 0 ; i < unused->size ; i++ ) {
1271 if (bitVectBitValue(unused,i)) {
1273 /* if assigned to registers */
1274 if (bitVectBitValue(_G.regAssigned,i)) {
1278 sym = hTabItemWithKey(liveRanges,i);
1279 /* remove it from regassigned & mark the
1281 bitVectUnSetBit(_G.regAssigned,i);
1282 for (j = 0 ; j < sym->nRegs; j++)
1283 freeReg(sym->regs[j]);
1285 /* not assigned to registers : remove from set*/
1286 bitVectUnSetBit(unused,i);
1294 /*-----------------------------------------------------------------*/
1295 /* reassignUnusedLRs - put registers to unused Live ranges */
1296 /*-----------------------------------------------------------------*/
1297 void reassignUnusedLRs (bitVect *unused)
1300 if (!unused) return ;
1302 for (i = 0 ; i < unused->size ; i++ ) {
1303 /* if unused : means it was assigned to registers before */
1304 if (bitVectBitValue(unused,i)) {
1308 /* put it back into reg set*/
1309 bitVectSetBit(_G.regAssigned,i) ;
1311 sym = hTabItemWithKey(liveRanges,i);
1312 /* make registers busy */
1313 for (j = 0 ; j < sym->nRegs; j++)
1314 sym->regs[j]->isFree = 0;
1319 /*------------------------------------------------------------------*/
1320 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1321 /* it should either have registers or have beed spilled. Otherwise, */
1322 /* there was an uninitialized variable, so just spill this to get */
1323 /* the operand in a valid state. */
1324 /*------------------------------------------------------------------*/
1326 verifyRegsAssigned (operand *op, iCode * ic)
1331 if (!IS_ITEMP (op)) return;
1333 sym = OP_SYMBOL (op);
1334 if (sym->isspilt) return;
1335 if (!sym->nRegs) return;
1336 if (sym->regs[0]) return;
1338 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1339 sym->prereqv ? sym->prereqv->name : sym->name);
1344 /*-----------------------------------------------------------------*/
1345 /* serialRegAssign - serially allocate registers to the variables */
1346 /*-----------------------------------------------------------------*/
1348 serialRegAssign (eBBlock ** ebbs, int count)
1352 /* for all blocks */
1353 for (i = 0; i < count; i++)
1357 bitVect *unusedLRs = NULL;
1359 if (ebbs[i]->noPath &&
1360 (ebbs[i]->entryLabel != entryLabel &&
1361 ebbs[i]->entryLabel != returnLabel))
1364 unusedLRs = deassignUnsedLRs(ebbs[i]);
1366 /* for all instructions do */
1367 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1371 /* if this is an ipop that means some live
1372 range will have to be assigned again */
1374 reassignLR (IC_LEFT (ic));
1376 /* if result is present && is a true symbol */
1377 if (IC_RESULT (ic) && ic->op != IFX &&
1378 IS_TRUE_SYMOP (IC_RESULT (ic)))
1379 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1381 /* take away registers from live
1382 ranges that end at this instruction */
1383 deassignLRs (ic, ebbs[i]);
1385 /* some don't need registers */
1386 if (SKIP_IC2 (ic) ||
1387 ic->op == JUMPTABLE ||
1391 (IC_RESULT (ic) && POINTER_SET (ic)))
1394 /* now we need to allocate registers
1395 only for the result */
1398 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1404 /* Make sure any spill location is definitely allocated */
1405 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1406 !sym->usl.spillLoc->allocreq)
1408 sym->usl.spillLoc->allocreq++;
1411 /* if it does not need or is spilt
1412 or is already assigned to registers
1413 or will not live beyond this instructions */
1416 bitVectBitValue (_G.regAssigned, sym->key) ||
1417 sym->liveTo <= ic->seq)
1420 /* if some liverange has been spilt at the block level
1421 and this one live beyond this block then spil this
1423 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1429 willCS = willCauseSpill (sym->nRegs, sym->regType);
1430 /* if this is a bit variable then don't use precious registers
1431 along with expensive bit-to-char conversions but just spill
1433 if (willCS && SPEC_NOUN(sym->etype) == V_BIT)
1439 /* if trying to allocate this will cause
1440 a spill and there is nothing to spill
1441 or this one is rematerializable then
1443 spillable = computeSpillable (ic);
1444 if (sym->remat || (willCS && bitVectIsZero (spillable)))
1450 /* If the live range preceeds the point of definition
1451 then ideally we must take into account registers that
1452 have been allocated after sym->liveFrom but freed
1453 before ic->seq. This is complicated, so spill this
1454 symbol instead and let fillGaps handle the allocation. */
1455 if (sym->liveFrom < ic->seq)
1461 /* if it has a spillocation & is used less than
1462 all other live ranges then spill this */
1464 if (sym->usl.spillLoc) {
1465 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1466 allLRs, ebbs[i], ic));
1467 if (leastUsed && leastUsed->used > sym->used) {
1472 /* if none of the liveRanges have a spillLocation then better
1473 to spill this one than anything else already assigned to registers */
1474 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1475 /* if this is local to this block then we might find a block spil */
1476 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1484 /* if we need ptr regs for the right side
1486 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1487 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned) PTRSIZE)
1492 /* else we assign registers to it */
1493 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1494 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1496 for (j = 0; j < sym->nRegs; j++)
1498 sym->regs[j] = NULL;
1499 if (sym->regType == REG_PTR)
1500 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1501 else if (sym->regType == REG_BIT)
1502 sym->regs[j] = getRegBit (sym);
1504 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1506 /* if the allocation failed which means
1507 this was spilt then break */
1512 /* if it shares registers with operands make sure
1513 that they are in the same position */
1514 if (!POINTER_SET(ic) && !POINTER_GET(ic))
1516 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1517 OP_SYMBOL (IC_LEFT (ic))->nRegs)
1519 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1520 OP_SYMBOL (IC_LEFT (ic)));
1522 /* do the same for the right operand */
1523 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1524 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1526 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1527 OP_SYMBOL (IC_RIGHT (ic)));
1539 reassignUnusedLRs(unusedLRs);
1542 /* Check for and fix any problems with uninitialized operands */
1543 for (i = 0; i < count; i++)
1547 if (ebbs[i]->noPath &&
1548 (ebbs[i]->entryLabel != entryLabel &&
1549 ebbs[i]->entryLabel != returnLabel))
1552 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1559 verifyRegsAssigned (IC_COND (ic), ic);
1563 if (ic->op == JUMPTABLE)
1565 verifyRegsAssigned (IC_JTCOND (ic), ic);
1569 verifyRegsAssigned (IC_RESULT (ic), ic);
1570 verifyRegsAssigned (IC_LEFT (ic), ic);
1571 verifyRegsAssigned (IC_RIGHT (ic), ic);
1576 /*-----------------------------------------------------------------*/
1577 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1578 /*-----------------------------------------------------------------*/
1579 static void fillGaps()
1583 int loop = 0, change;
1586 if (getenv("DISABLE_FILL_GAPS")) return;
1588 /* First try to do DPTRuse once more since now we know what got into
1591 while (loop++ < 10) {
1594 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1595 sym = hTabNextItem(liveRanges,&key)) {
1596 int size = getSize(sym->type);
1598 if (sym->liveFrom == sym->liveTo) continue;
1600 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1601 size < 4 && size > 1) {
1603 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1605 /* if this was assigned to registers then */
1606 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1607 /* take it out of the register assigned set */
1608 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1609 } else if (sym->usl.spillLoc) {
1610 sym->usl.spillLoc->allocreq--;
1611 sym->usl.spillLoc = NULL;
1615 sym->isspilt = sym->spillA = 0;
1619 /* try assigning other dptrs */
1620 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1621 /* if this was ssigned to registers then */
1622 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1623 /* take it out of the register assigned set */
1624 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1625 } else if (sym->usl.spillLoc) {
1626 sym->usl.spillLoc->allocreq--;
1627 sym->usl.spillLoc = NULL;
1630 sym->isspilt = sym->spillA = 0;
1635 /* look for liveranges that were spilt by the allocator */
1636 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1637 sym = hTabNextItem(liveRanges,&key)) {
1642 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1643 if (!sym->uses || !sym->defs) continue ;
1644 /* find the liveRanges this one clashes with, that are
1645 still assigned to registers & mark the registers as used*/
1646 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1650 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1651 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1654 clr = hTabItemWithKey(liveRanges,i);
1657 /* mark these registers as used */
1658 for (k = 0 ; k < clr->nRegs ; k++ )
1659 useReg(clr->regs[k]);
1662 if (willCauseSpill(sym->nRegs,sym->regType)) {
1663 /* NOPE :( clear all registers & and continue */
1668 /* THERE IS HOPE !!!! */
1669 for (i=0; i < sym->nRegs ; i++ ) {
1670 if (sym->regType == REG_PTR)
1671 sym->regs[i] = getRegPtrNoSpil ();
1673 sym->regs[i] = getRegGprNoSpil ();
1676 /* For all its definitions check if the registers
1677 allocated needs positioning NOTE: we can position
1678 only ONCE if more than One positioning required
1680 We may need to perform the checks twice; once to
1681 position the registers as needed, the second to
1682 verify any register repositioning is still
1686 for (pass=0; pass<2; pass++) {
1687 for (i = 0 ; i < sym->defs->size ; i++ ) {
1688 if (bitVectBitValue(sym->defs,i)) {
1690 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1691 if (SKIP_IC(ic)) continue;
1692 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1693 /* if left is assigned to registers */
1694 if (IS_SYMOP(IC_LEFT(ic)) &&
1695 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1696 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1698 if (IS_SYMOP(IC_RIGHT(ic)) &&
1699 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1700 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1702 if (pdone > 1) break;
1705 for (i = 0 ; i < sym->uses->size ; i++ ) {
1706 if (bitVectBitValue(sym->uses,i)) {
1708 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1709 if (SKIP_IC(ic)) continue;
1710 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1712 /* if result is assigned to registers */
1713 if (IS_SYMOP(IC_RESULT(ic)) &&
1714 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1715 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1717 if (pdone > 1) break;
1720 if (pdone == 0) break; /* second pass only if regs repositioned */
1721 if (pdone > 1) break;
1723 /* had to position more than once GIVE UP */
1725 /* UNDO all the changes we made to try this */
1727 for (i=0; i < sym->nRegs ; i++ ) {
1728 sym->regs[i] = NULL;
1731 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1732 "%s in function %s\n",
1733 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1736 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1737 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1738 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1739 sym->isspilt = sym->spillA = 0 ;
1740 sym->usl.spillLoc->allocreq--;
1741 sym->usl.spillLoc = NULL;
1749 /*-----------------------------------------------------------------*/
1750 /* findAllBitregs :- returns bit vector of all bit registers */
1751 /*-----------------------------------------------------------------*/
1753 findAllBitregs (void)
1755 bitVect *rmask = newBitVect (ds390_nRegs);
1758 for (j = 0; j < ds390_nRegs; j++)
1760 if (regs390[j].type == REG_BIT)
1761 rmask = bitVectSetBit (rmask, regs390[j].rIdx);
1767 /*-----------------------------------------------------------------*/
1768 /* ds390_allBitregs :- returns bit vector of all bit registers */
1769 /*-----------------------------------------------------------------*/
1771 ds390_allBitregs (void)
1773 return _G.allBitregs;
1776 /*-----------------------------------------------------------------*/
1777 /* rUmaskForOp :- returns register mask for an operand */
1778 /*-----------------------------------------------------------------*/
1780 ds390_rUmaskForOp (operand * op)
1786 /* only temporaries are assigned registers */
1790 sym = OP_SYMBOL (op);
1792 /* if spilt or no registers assigned to it
1794 if (sym->isspilt || !sym->nRegs)
1797 rumask = newBitVect (ds390_nRegs);
1799 for (j = 0; j < sym->nRegs; j++)
1801 rumask = bitVectSetBit (rumask,
1802 sym->regs[j]->rIdx);
1808 /*-----------------------------------------------------------------*/
1809 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1810 /*-----------------------------------------------------------------*/
1812 regsUsedIniCode (iCode * ic)
1814 bitVect *rmask = newBitVect (ds390_nRegs);
1816 /* do the special cases first */
1819 rmask = bitVectUnion (rmask,
1820 ds390_rUmaskForOp (IC_COND (ic)));
1824 /* for the jumptable */
1825 if (ic->op == JUMPTABLE)
1827 rmask = bitVectUnion (rmask,
1828 ds390_rUmaskForOp (IC_JTCOND (ic)));
1833 /* of all other cases */
1835 rmask = bitVectUnion (rmask,
1836 ds390_rUmaskForOp (IC_LEFT (ic)));
1840 rmask = bitVectUnion (rmask,
1841 ds390_rUmaskForOp (IC_RIGHT (ic)));
1844 rmask = bitVectUnion (rmask,
1845 ds390_rUmaskForOp (IC_RESULT (ic)));
1851 /*-----------------------------------------------------------------*/
1852 /* createRegMask - for each instruction will determine the regsUsed */
1853 /*-----------------------------------------------------------------*/
1855 createRegMask (eBBlock ** ebbs, int count)
1859 /* for all blocks */
1860 for (i = 0; i < count; i++)
1864 if (ebbs[i]->noPath &&
1865 (ebbs[i]->entryLabel != entryLabel &&
1866 ebbs[i]->entryLabel != returnLabel))
1869 /* for all instructions */
1870 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1875 if (SKIP_IC2 (ic) || !ic->rlive)
1878 /* first mark the registers used in this
1880 ic->rUsed = regsUsedIniCode (ic);
1881 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1883 /* now create the register mask for those
1884 registers that are in use : this is a
1885 super set of ic->rUsed */
1886 ic->rMask = newBitVect (ds390_nRegs + 1);
1888 /* for all live Ranges alive at this point */
1889 for (j = 1; j < ic->rlive->size; j++)
1894 /* if not alive then continue */
1895 if (!bitVectBitValue (ic->rlive, j))
1898 /* find the live range we are interested in */
1899 if (!(sym = hTabItemWithKey (liveRanges, j)))
1901 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1902 "createRegMask cannot find live range");
1903 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1907 /* special case for ruonly */
1908 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1909 int size = getSize(sym->type);
1911 for (k = 0 ; k < size; k++ )
1912 ic->rMask = bitVectSetBit (ic->rMask, j++);
1916 /* if no register assigned to it */
1917 if (!sym->nRegs || sym->isspilt)
1920 /* for all the registers allocated to it */
1921 for (k = 0; k < sym->nRegs; k++)
1924 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1930 /*-----------------------------------------------------------------*/
1931 /* rematStr - returns the rematerialized string for a remat var */
1932 /*-----------------------------------------------------------------*/
1934 rematStr (symbol * sym)
1937 iCode *ic = sym->rematiCode;
1944 /* if plus or minus print the right hand side */
1945 if (ic->op == '+' || ic->op == '-')
1947 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1948 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1951 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1955 /* cast then continue */
1956 if (IS_CAST_ICODE(ic)) {
1957 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1960 /* we reached the end */
1961 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1962 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1969 /*-----------------------------------------------------------------*/
1970 /* regTypeNum - computes the type & number of registers required */
1971 /*-----------------------------------------------------------------*/
1979 /* for each live range do */
1980 for (sym = hTabFirstItem (liveRanges, &k); sym;
1981 sym = hTabNextItem (liveRanges, &k))
1984 /* if used zero times then no registers needed */
1985 if ((sym->liveTo - sym->liveFrom) == 0)
1989 /* if the live range is a temporary */
1993 /* if the type is marked as a conditional */
1994 if (sym->regType == REG_CND)
1997 /* if used in return only then we don't
1999 if (sym->ruonly || sym->accuse)
2001 if (IS_AGGREGATE (sym->type) || sym->isptr)
2002 sym->type = aggrToPtr (sym->type, FALSE);
2006 /* if the symbol has only one definition &
2007 that definition is a get_pointer */
2008 if (bitVectnBitsOn (sym->defs) == 1 &&
2009 (ic = hTabItemWithKey (iCodehTab,
2010 bitVectFirstBit (sym->defs))) &&
2012 !IS_BITVAR (sym->etype) &&
2013 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
2016 if (ptrPseudoSymSafe (sym, ic))
2018 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
2022 /* if in data space or idata space then try to
2023 allocate pointer register */
2027 /* if not then we require registers */
2028 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
2029 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
2030 getSize (sym->type));
2034 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
2035 printTypeChain (sym->type, stderr);
2036 fprintf (stderr, "\n");
2039 /* determine the type of register required */
2040 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
2041 sym->regType = REG_PTR;
2042 else if (IS_BIT(sym->type))
2043 sym->regType = REG_BIT;
2045 sym->regType = REG_GPR;
2048 /* for the first run we don't provide */
2049 /* registers for true symbols we will */
2050 /* see how things go */
2056 /*-----------------------------------------------------------------*/
2057 /* freeAllRegs - mark all registers as free */
2058 /*-----------------------------------------------------------------*/
2064 for (i = 0; i < ds390_nRegs; i++)
2065 regs390[i].isFree = 1;
2067 for (i = B0_IDX; i < ds390_nBitRegs; i++)
2068 regs390[i].isFree = 1;
2071 /*-----------------------------------------------------------------*/
2072 /* deallocStackSpil - this will set the stack pointer back */
2073 /*-----------------------------------------------------------------*/
2075 DEFSETFUNC (deallocStackSpil)
2083 /*-----------------------------------------------------------------*/
2084 /* farSpacePackable - returns the packable icode for far variables */
2085 /*-----------------------------------------------------------------*/
2087 farSpacePackable (iCode * ic)
2091 /* go thru till we find a definition for the
2092 symbol on the right */
2093 for (dic = ic->prev; dic; dic = dic->prev)
2095 /* if the definition is a call then no */
2096 if ((dic->op == CALL || dic->op == PCALL) &&
2097 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2102 /* if shift by unknown amount then not */
2103 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2104 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2107 /* if pointer get and size > 1 */
2108 if (POINTER_GET (dic) &&
2109 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2112 if (POINTER_SET (dic) &&
2113 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2116 /* if any tree is a true symbol in far space */
2117 if (IC_RESULT (dic) &&
2118 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2119 isOperandInFarSpace (IC_RESULT (dic)))
2122 if (IC_RIGHT (dic) &&
2123 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2124 isOperandInFarSpace (IC_RIGHT (dic)) &&
2125 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2128 if (IC_LEFT (dic) &&
2129 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2130 isOperandInFarSpace (IC_LEFT (dic)) &&
2131 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2134 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2136 if ((dic->op == LEFT_OP ||
2137 dic->op == RIGHT_OP ||
2139 IS_OP_LITERAL (IC_RIGHT (dic)))
2149 /*-----------------------------------------------------------------*/
2150 /* packRegsForAssign - register reduction for assignment */
2151 /*-----------------------------------------------------------------*/
2153 packRegsForAssign (iCode * ic, eBBlock * ebp)
2157 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2158 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2159 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2164 /* if the true symbol is defined in far space or on stack
2165 then we should not since this will increase register pressure */
2167 if (isOperandInFarSpace (IC_RESULT (ic)))
2169 if ((dic = farSpacePackable (ic)))
2175 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2180 /* find the definition of iTempNN scanning backwards if we find a
2181 a use of the true symbol in before we find the definition then
2183 for (dic = ic->prev; dic; dic = dic->prev)
2185 /* if there is a function call then don't pack it */
2186 if ((dic->op == CALL || dic->op == PCALL))
2195 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2196 IS_OP_VOLATILE (IC_RESULT (dic)))
2202 if (IS_SYMOP (IC_RESULT (dic)) &&
2203 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2205 if (POINTER_SET (dic))
2211 if (IS_SYMOP (IC_RIGHT (dic)) &&
2212 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2213 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2219 if (IS_SYMOP (IC_LEFT (dic)) &&
2220 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2221 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2227 if (POINTER_SET (dic) &&
2228 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2236 return 0; /* did not find */
2238 /* if assignment then check that right is not a bit */
2239 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2241 sym_link *etype = operandType (IC_RESULT (dic));
2242 if (IS_BITFIELD (etype))
2244 /* if result is a bit too then it's ok */
2245 etype = operandType (IC_RESULT (ic));
2246 if (!IS_BITFIELD (etype))
2252 /* if the result is on stack or iaccess then it must be
2253 the same atleast one of the operands */
2254 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2255 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2258 /* the operation has only one symbol
2259 operator then we can pack */
2260 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2261 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2264 if (!((IC_LEFT (dic) &&
2265 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2267 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2271 /* found the definition */
2272 /* replace the result with the result of */
2273 /* this assignment and remove this assignment */
2274 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2276 IC_RESULT (dic) = IC_RESULT (ic);
2278 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2280 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2282 /* delete from liverange table also
2283 delete from all the points inbetween and the new
2285 for (sic = dic; sic != ic; sic = sic->next)
2287 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2288 if (IS_ITEMP (IC_RESULT (dic)))
2289 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2292 remiCodeFromeBBlock (ebp, ic);
2293 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2294 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2295 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2299 /*------------------------------------------------------------------*/
2300 /* findAssignToSym : scanning backwards looks for first assig found */
2301 /*------------------------------------------------------------------*/
2303 findAssignToSym (operand * op, iCode * ic)
2307 /* This routine is used to find sequences like
2309 ...; (intervening ops don't use iTempAA or modify FOO)
2310 blah = blah + iTempAA;
2312 and eliminate the use of iTempAA, freeing up its register for
2316 for (dic = ic->prev; dic; dic = dic->prev)
2319 /* if definition by assignment */
2320 if (dic->op == '=' &&
2321 !POINTER_SET (dic) &&
2322 IC_RESULT (dic)->key == op->key
2323 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2327 /* we are interested only if defined in far space */
2328 /* or in stack space in case of + & - */
2330 /* if assigned to a non-symbol then return
2332 if (!IS_SYMOP (IC_RIGHT (dic)))
2335 /* if the symbol is in far space then we should not */
2336 if (isOperandInFarSpace (IC_RIGHT (dic)))
2339 /* for + & - operations make sure that
2340 if it is on the stack it is the same
2341 as one of the three operands */
2342 if ((ic->op == '+' || ic->op == '-') &&
2343 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2346 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2347 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2348 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2356 /* if we find an usage then we cannot delete it */
2357 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2360 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2363 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2367 /* now make sure that the right side of dic
2368 is not defined between ic & dic */
2371 iCode *sic = dic->next;
2373 for (; sic != ic; sic = sic->next)
2374 if (IC_RESULT (sic) &&
2375 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2384 /*-----------------------------------------------------------------*/
2385 /* packRegsForSupport :- reduce some registers for support calls */
2386 /*-----------------------------------------------------------------*/
2388 packRegsForSupport (iCode * ic, eBBlock * ebp)
2392 /* for the left & right operand :- look to see if the
2393 left was assigned a true symbol in far space in that
2394 case replace them */
2395 if (IS_ITEMP (IC_LEFT (ic)) &&
2396 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2398 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2404 /* found it we need to remove it from the
2406 for (sic = dic; sic != ic; sic = sic->next) {
2407 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2408 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2411 wassert(IS_SYMOP(IC_LEFT (ic)));
2412 wassert(IS_SYMOP(IC_RIGHT (dic)));
2413 IC_LEFT (ic)->operand.symOperand =
2414 IC_RIGHT (dic)->operand.symOperand;
2415 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2416 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2417 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2418 remiCodeFromeBBlock (ebp, dic);
2419 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2423 /* do the same for the right operand */
2426 IS_ITEMP (IC_RIGHT (ic)) &&
2427 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2429 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2435 /* if this is a subtraction & the result
2436 is a true symbol in far space then don't pack */
2437 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2439 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2440 if (IN_FARSPACE (SPEC_OCLS (etype)))
2443 /* found it we need to remove it from the
2445 for (sic = dic; sic != ic; sic = sic->next) {
2446 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2447 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2450 wassert(IS_SYMOP(IC_RIGHT (ic)));
2451 wassert(IS_SYMOP(IC_RIGHT (dic)));
2452 IC_RIGHT (ic)->operand.symOperand =
2453 IC_RIGHT (dic)->operand.symOperand;
2454 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2455 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2456 remiCodeFromeBBlock (ebp, dic);
2457 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2458 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2465 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2468 /*-----------------------------------------------------------------*/
2469 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2470 /*-----------------------------------------------------------------*/
2471 static int packRegsDPTRnuse( operand *op , unsigned dptr)
2476 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2477 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2480 /* first check if any overlapping liverange has already been
2481 assigned to this DPTR */
2482 if (OP_SYMBOL(op)->clashes) {
2483 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2485 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2486 sym = hTabItemWithKey(liveRanges,i);
2487 if (sym->dptr == dptr) return 0;
2492 /* future for more dptrs */
2494 OP_SYMBOL(op)->dptr = dptr;
2498 /* DPTR1 is special since it is also used as a scratch by the backend .
2499 so we walk thru the entire live range of this operand and make sure
2500 DPTR1 will not be used by the backed . The logic here is to find out if
2501 more than one operand in an icode is in far space then we give up : we
2502 don't keep it live across functions for now
2505 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2506 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2507 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2510 if (ic->op == CALL || ic->op == PCALL) return 0;
2512 /* single operand icode are ok */
2513 if (ic->op == IFX || ic->op == IPUSH)
2516 if (ic->op == SEND ) {
2517 if (ic->argreg != 1 ) return 0;
2520 /* two special cases first */
2521 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2522 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2523 (isOperandInFarSpace(IC_RESULT(ic)) &&
2524 !isOperandInReg(IC_RESULT(ic)))) {
2528 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2529 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2530 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2531 !isOperandInReg(IC_RIGHT(ic)))) {
2535 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2536 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2537 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2538 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2539 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2543 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2544 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2545 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2546 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2547 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2550 /* same for right */
2551 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2552 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2553 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2554 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2555 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2559 // Check that no other ops in this range have been assigned to dptr1.
2560 // I don't understand why this is not caught by the first check, above.
2561 // But it isn't always, see bug 769624.
2562 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2563 (OP_SYMBOL(IC_RESULT(ic))->dptr == 1))
2565 //fprintf(stderr, "dptr1 already in use in live range #1\n");
2569 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2570 (OP_SYMBOL(IC_LEFT(ic))->dptr == 1))
2572 //fprintf(stderr, "dptr1 already in use in live range # 2\n");
2576 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2577 (OP_SYMBOL(IC_RIGHT(ic))->dptr == 1))
2579 //fprintf(stderr, "dptr1 already in use in live range # 3\n");
2583 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2584 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2586 if (nfs > 1) return 0;
2588 OP_SYMBOL(op)->dptr = dptr;
2592 /*-----------------------------------------------------------------*/
2593 /* packRegsDPTRuse : - will reduce some registers for single Use */
2594 /*-----------------------------------------------------------------*/
2596 packRegsDPTRuse (operand * op)
2598 /* go thru entire liveRange of this variable & check for
2599 other possible usage of DPTR , if we don't find it the
2600 assign this to DPTR (ruonly)
2605 sym_link *type, *etype;
2607 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2608 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2610 /* first check if any overlapping liverange has already been
2612 if (OP_SYMBOL(op)->clashes) {
2613 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2614 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2615 sym = hTabItemWithKey(liveRanges,i);
2616 if (sym->ruonly) return NULL ;
2621 /* no then go thru this guys live range */
2622 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2623 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2624 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2626 if (SKIP_IC3(ic)) continue;
2628 /* if PCALL cannot be sure give up */
2629 if (ic->op == PCALL) return NULL;
2631 /* if SEND & not the first parameter then giveup */
2632 if (ic->op == SEND && ic->argreg != 1 &&
2633 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2634 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2636 /* if CALL then make sure it is VOID || return value not used
2637 or the return value is assigned to this one */
2638 if (ic->op == CALL) {
2639 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2640 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2641 etype = getSpec(type = operandType(IC_RESULT(ic)));
2642 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2647 /* special case of add with a [remat] */
2648 if (ic->op == '+' &&
2649 OP_SYMBOL(IC_LEFT(ic))->remat &&
2650 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2651 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2655 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2656 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2659 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2660 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2662 /* conditionals can destroy 'b' - make sure B wont
2663 be used in this one*/
2664 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2665 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2666 getSize(operandType(op)) > 3) return NULL;
2668 /* if this is a cast to a bigger type */
2670 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2671 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2672 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2678 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2679 !isOperandEqual(IC_RESULT(ic),op) &&
2680 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2681 !isOperandInReg(IC_RESULT(ic))) ||
2682 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2684 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2685 !isOperandEqual(IC_RIGHT(ic),op) &&
2686 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2687 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2688 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2689 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2690 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2692 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2693 !isOperandEqual(IC_LEFT(ic),op) &&
2694 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2695 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2696 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2697 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2698 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2700 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2701 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2702 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2703 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2706 OP_SYMBOL(op)->ruonly = 1;
2707 if (OP_SYMBOL(op)->usl.spillLoc) {
2708 if (OP_SYMBOL(op)->spillA)
2709 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2710 OP_SYMBOL(op)->usl.spillLoc = NULL;
2715 /*-----------------------------------------------------------------*/
2716 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2717 /*-----------------------------------------------------------------*/
2719 isBitwiseOptimizable (iCode * ic)
2721 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2722 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2724 /* bitwise operations are considered optimizable
2725 under the following conditions (Jean-Louis VERN)
2737 if (IS_LITERAL (rtype) ||
2738 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2744 /*-----------------------------------------------------------------*/
2745 /* packRegsForAccUse - pack registers for acc use */
2746 /*-----------------------------------------------------------------*/
2748 packRegsForAccUse (iCode * ic)
2752 /* if this is an aggregate, e.g. a one byte char array */
2753 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2757 /* if we are calling a reentrant function that has stack parameters */
2758 if (ic->op == CALL &&
2759 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2760 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2763 if (ic->op == PCALL &&
2764 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2765 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2768 /* if + or - then it has to be one byte result */
2769 if ((ic->op == '+' || ic->op == '-')
2770 && getSize (operandType (IC_RESULT (ic))) > 1)
2773 /* if shift operation make sure right side is not a literal */
2774 if (ic->op == RIGHT_OP &&
2775 (isOperandLiteral (IC_RIGHT (ic)) ||
2776 getSize (operandType (IC_RESULT (ic))) > 1))
2779 if (ic->op == LEFT_OP &&
2780 (isOperandLiteral (IC_RIGHT (ic)) ||
2781 getSize (operandType (IC_RESULT (ic))) > 1))
2784 if (IS_BITWISE_OP (ic) &&
2785 getSize (operandType (IC_RESULT (ic))) > 1)
2789 /* has only one definition */
2790 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2793 /* has only one use */
2794 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2797 /* and the usage immediately follows this iCode */
2798 if (!(uic = hTabItemWithKey (iCodehTab,
2799 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2802 if (ic->next != uic)
2805 /* if it is a conditional branch then we definitely can */
2809 if (uic->op == JUMPTABLE)
2812 /* if the usage is not is an assignment
2813 or an arithmetic / bitwise / shift operation then not */
2814 if (POINTER_SET (uic) &&
2815 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2818 if (uic->op != '=' &&
2819 !IS_ARITHMETIC_OP (uic) &&
2820 !IS_BITWISE_OP (uic) &&
2821 uic->op != LEFT_OP &&
2822 uic->op != RIGHT_OP)
2825 /* if used in ^ operation then make sure right is not a
2827 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2830 /* if shift operation make sure right side is not a literal */
2831 if (uic->op == RIGHT_OP &&
2832 (isOperandLiteral (IC_RIGHT (uic)) ||
2833 getSize (operandType (IC_RESULT (uic))) > 1))
2836 if (uic->op == LEFT_OP &&
2837 (isOperandLiteral (IC_RIGHT (uic)) ||
2838 getSize (operandType (IC_RESULT (uic))) > 1))
2841 /* make sure that the result of this icode is not on the
2842 stack, since acc is used to compute stack offset */
2843 if (isOperandOnStack(IC_RESULT(uic)))
2846 /* if either one of them in far space then we cannot */
2847 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2848 isOperandInFarSpace (IC_LEFT (uic))) ||
2849 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2850 isOperandInFarSpace (IC_RIGHT (uic))))
2853 /* if the usage has only one operand then we can */
2854 if (IC_LEFT (uic) == NULL ||
2855 IC_RIGHT (uic) == NULL)
2858 /* make sure this is on the left side if not
2859 a '+' since '+' is commutative */
2860 if (ic->op != '+' &&
2861 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2864 /* if the other one is not on stack then we can */
2865 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2866 (IS_ITEMP (IC_RIGHT (uic)) ||
2867 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2868 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2871 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2872 (IS_ITEMP (IC_LEFT (uic)) ||
2873 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2874 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2880 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2884 /*-----------------------------------------------------------------*/
2885 /* packForPush - heuristics to reduce iCode for pushing */
2886 /*-----------------------------------------------------------------*/
2888 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2892 struct eBBlock * ebp = ebpp[blockno];
2894 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2897 /* must have only definition & one usage */
2898 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2899 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2902 /* find the definition */
2903 if (!(dic = hTabItemWithKey (iCodehTab,
2904 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2907 if (dic->op != '=' || POINTER_SET (dic))
2910 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2912 /* make sure the right side does not have any definitions
2914 dbv = OP_DEFS(IC_RIGHT(dic));
2915 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2916 if (bitVectBitValue (dbv, lic->key))
2919 /* make sure they have the same type */
2920 if (IS_SPEC(operandType(IC_LEFT(ic))))
2922 sym_link *itype=operandType(IC_LEFT(ic));
2923 sym_link *ditype=operandType(IC_RIGHT(dic));
2925 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2926 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2929 /* extend the live range of replaced operand if needed */
2930 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2931 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2932 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2933 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2934 OP_SYMBOL(IC_LEFT(ic))->clashes);
2936 for (lic = ic; lic && lic != dic; lic = lic->prev)
2938 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2939 if (IS_ITEMP (IC_RIGHT (dic)))
2940 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2942 /* we now we know that it has one & only one def & use
2943 and the that the definition is an assignment */
2944 IC_LEFT (ic) = IC_RIGHT (dic);
2946 remiCodeFromeBBlock (ebp, dic);
2947 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2948 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2951 /*-----------------------------------------------------------------*/
2952 /* packRegisters - does some transformations to reduce register */
2954 /*-----------------------------------------------------------------*/
2956 packRegisters (eBBlock ** ebpp, int blockno)
2960 eBBlock *ebp = ebpp[blockno];
2966 /* look for assignments of the form */
2967 /* iTempNN = TRueSym (someoperation) SomeOperand */
2969 /* TrueSym := iTempNN:1 */
2970 for (ic = ebp->sch; ic; ic = ic->next)
2972 /* find assignment of the form TrueSym := iTempNN:1 */
2973 if (ic->op == '=' && !POINTER_SET (ic))
2974 change += packRegsForAssign (ic, ebp);
2981 for (ic = ebp->sch; ic; ic = ic->next)
2983 /* Fix for bug #979599: */
2986 /* Look for two subsequent iCodes with */
2988 /* _c = iTemp & op; */
2989 /* and replace them by */
2992 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2994 ic->prev->op == '=' &&
2995 IS_ITEMP (IC_LEFT (ic)) &&
2996 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2997 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
2999 iCode* ic_prev = ic->prev;
3000 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
3002 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
3003 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
3005 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
3006 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
3007 prev_result_sym->liveTo == ic->seq)
3009 prev_result_sym->liveTo = ic_prev->seq;
3012 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
3014 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
3016 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
3018 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
3019 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
3020 remiCodeFromeBBlock (ebp, ic_prev);
3021 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
3025 /* if this is an itemp & result of an address of a true sym
3026 then mark this as rematerialisable */
3027 if (ic->op == ADDRESS_OF &&
3028 IS_ITEMP (IC_RESULT (ic)) &&
3029 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
3030 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3031 !OP_SYMBOL (IC_LEFT (ic))->onStack)
3034 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3035 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3036 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3040 /* if this is an itemp & used as a pointer
3041 & assigned to a literal then remat */
3042 if (IS_ASSIGN_ICODE(ic) &&
3043 IS_ITEMP(IC_RESULT(ic)) &&
3044 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3045 isOperandLiteral(IC_RIGHT(ic)))
3047 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3048 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3049 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3052 /* if straight assignment then carry remat flag if
3053 this is the only definition */
3054 if (ic->op == '=' &&
3055 !POINTER_SET (ic) &&
3056 IS_SYMOP (IC_RIGHT (ic)) &&
3057 OP_SYMBOL (IC_RIGHT (ic))->remat &&
3058 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
3059 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
3061 OP_SYMBOL (IC_RESULT (ic))->remat =
3062 OP_SYMBOL (IC_RIGHT (ic))->remat;
3063 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
3064 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
3067 /* if cast to a generic pointer & the pointer being
3068 cast is remat, then we can remat this cast as well */
3069 if (ic->op == CAST &&
3070 IS_SYMOP(IC_RIGHT(ic)) &&
3071 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
3072 OP_SYMBOL(IC_RIGHT(ic))->remat )
3074 sym_link *to_type = operandType(IC_LEFT(ic));
3075 sym_link *from_type = operandType(IC_RIGHT(ic));
3076 if (IS_GENPTR(to_type) && IS_PTR(from_type))
3078 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3079 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3080 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3084 /* if this is a +/- operation with a rematerizable
3085 then mark this as rematerializable as well */
3086 if ((ic->op == '+' || ic->op == '-') &&
3087 (IS_SYMOP (IC_LEFT (ic)) &&
3088 IS_ITEMP (IC_RESULT (ic)) &&
3089 OP_SYMBOL (IC_LEFT (ic))->remat &&
3090 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3091 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3092 IS_OP_LITERAL (IC_RIGHT (ic))))
3095 //int i = operandLitValue(IC_RIGHT(ic));
3096 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3097 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3098 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3101 /* mark the pointer usages */
3102 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic)))
3103 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3105 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic)))
3106 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3108 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
3109 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3111 if (ic->op == RECEIVE && ic->argreg == 1 &&
3112 IS_SYMOP (IC_RESULT (ic)) &&
3113 getSize (operandType(IC_RESULT(ic))) <= 3)
3114 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
3116 if (ic->op == SEND && ic->argreg == 1 &&
3117 IS_SYMOP(IC_LEFT(ic)) &&
3118 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
3119 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
3123 /* if we are using a symbol on the stack
3124 then we should say ds390_ptrRegReq */
3125 if (options.useXstack && ic->parmPush
3126 && (ic->op == IPUSH || ic->op == IPOP))
3128 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3129 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
3130 OP_SYMBOL (IC_COND (ic))->iaccess +
3131 (SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata));
3132 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3133 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
3134 OP_SYMBOL (IC_JTCOND (ic))->iaccess +
3135 (SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata));
3138 if (IS_SYMOP (IC_LEFT (ic)))
3139 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
3140 OP_SYMBOL (IC_LEFT (ic))->iaccess +
3141 (SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata));
3142 if (IS_SYMOP (IC_RIGHT (ic)))
3143 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
3144 OP_SYMBOL (IC_RIGHT (ic))->iaccess +
3145 (SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata));
3146 if (IS_SYMOP (IC_RESULT (ic)))
3147 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
3148 OP_SYMBOL (IC_RESULT (ic))->iaccess +
3149 (SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata));
3153 /* if the condition of an if instruction
3154 is defined in the previous instruction and
3155 this is the only usage then
3156 mark the itemp as a conditional */
3157 if ((IS_CONDITIONAL (ic) ||
3158 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3159 ic->next && ic->next->op == IFX &&
3160 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3161 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3162 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3164 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3168 /* reduce for support function calls */
3169 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3170 packRegsForSupport (ic, ebp);
3172 /* some cases the redundant moves can
3173 can be eliminated for return statements . Can be elminated for the first SEND */
3174 if ((ic->op == RETURN ||
3175 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
3176 !isOperandInFarSpace (IC_LEFT (ic)) &&
3179 packRegsDPTRuse (IC_LEFT (ic));
3182 if (ic->op == CALL) {
3183 sym_link *ftype = operandType(IC_LEFT(ic));
3184 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
3185 !IFFUNC_ISBUILTIN(ftype)) {
3186 packRegsDPTRuse (IC_RESULT (ic));
3190 /* if pointer set & left has a size more than
3191 one and right is not in far space */
3192 if (POINTER_SET (ic) &&
3193 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3194 IS_SYMOP (IC_RESULT (ic)) &&
3195 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3196 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3197 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
3199 packRegsDPTRuse (IC_RESULT (ic));
3202 /* if pointer get */
3203 if (POINTER_GET (ic) &&
3204 !isOperandInFarSpace (IC_RESULT (ic)) &&
3205 IS_SYMOP (IC_LEFT (ic)) &&
3206 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3207 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3208 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
3210 packRegsDPTRuse (IC_LEFT (ic));
3213 /* if this is a cast for intergral promotion then
3214 check if it's the only use of the definition of the
3215 operand being casted/ if yes then replace
3216 the result of that arithmetic operation with
3217 this result and get rid of the cast */
3220 sym_link *fromType = operandType (IC_RIGHT (ic));
3221 sym_link *toType = operandType (IC_LEFT (ic));
3223 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3224 getSize (fromType) != getSize (toType) &&
3225 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3228 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3231 if (IS_ARITHMETIC_OP (dic))
3233 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3234 IC_RESULT (dic) = IC_RESULT (ic);
3235 remiCodeFromeBBlock (ebp, ic);
3236 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3237 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3238 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3242 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3248 /* if the type from and type to are the same
3249 then if this is the only use then packit */
3250 if (compareType (operandType (IC_RIGHT (ic)),
3251 operandType (IC_LEFT (ic))) == 1)
3253 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3256 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3257 IC_RESULT (dic) = IC_RESULT (ic);
3258 remiCodeFromeBBlock (ebp, ic);
3259 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3260 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3261 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3269 iTempNN := (some variable in farspace) V1
3274 if (ic->op == IPUSH || ic->op == SEND)
3276 packForPush (ic, ebpp, blockno);
3280 /* pack registers for accumulator use, when the
3281 result of an arithmetic or bit wise operation
3282 has only one use, that use is immediately following
3283 the defintion and the using iCode has only one
3284 operand or has two operands but one is literal &
3285 the result of that operation is not on stack then
3286 we can leave the result of this operation in acc:b
3288 if ((IS_ARITHMETIC_OP (ic)
3289 || IS_CONDITIONAL(ic)
3290 || IS_BITWISE_OP (ic)
3291 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3292 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3294 IS_ITEMP (IC_RESULT (ic)) &&
3295 getSize (operandType (IC_RESULT (ic))) <= 2)
3297 packRegsForAccUse (ic);
3301 /*-----------------------------------------------------------------*/
3302 /* assignRegisters - assigns registers to each live range as need */
3303 /*-----------------------------------------------------------------*/
3305 ds390_assignRegisters (ebbIndex * ebbi)
3307 eBBlock ** ebbs = ebbi->bbOrder;
3308 int count = ebbi->count;
3312 setToNull ((void *) &_G.funcrUsed);
3313 setToNull ((void *) &_G.regAssigned);
3314 setToNull ((void *) &_G.totRegAssigned);
3315 setToNull ((void *) &_G.funcrUsed);
3316 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3317 if ((currFunc && IFFUNC_ISREENT (currFunc->type)) || options.stackAuto)
3325 ds390_nRegs = 12 + ds390_nBitRegs;
3326 _G.allBitregs = findAllBitregs ();
3328 if (options.model != MODEL_FLAT24)
3329 options.stack10bit = 0;
3330 /* change assignments this will remove some
3331 live ranges reducing some register pressure */
3332 for (i = 0; i < count; i++)
3333 packRegisters (ebbs, i);
3335 /* liveranges probably changed by register packing
3336 so we compute them again */
3337 recomputeLiveRanges (ebbs, count);
3339 if (options.dump_pack)
3340 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3342 /* first determine for each live range the number of
3343 registers & the type of registers required for each */
3346 /* and serially allocate registers */
3347 serialRegAssign (ebbs, count);
3352 ds390_nRegs = 12 + ds390_nBitRegs;
3354 /* if stack was extended then tell the user */
3357 /* werror(W_TOOMANY_SPILS,"stack", */
3358 /* _G.stackExtend,currFunc->name,""); */
3364 /* werror(W_TOOMANY_SPILS,"data space", */
3365 /* _G.dataExtend,currFunc->name,""); */
3369 /* after that create the register mask
3370 for each of the instruction */
3371 createRegMask (ebbs, count);
3373 /* redo that offsets for stacked automatic variables */
3375 redoStackOffsets ();
3377 /* make sure r0 & r1 are flagged as used if they might be used */
3379 if (currFunc && ds390_ptrRegReq)
3381 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3382 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3385 if (options.dump_rassgn)
3387 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3388 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3391 /* do the overlaysegment stuff SDCCmem.c */
3392 doOverlays (ebbs, count);
3394 /* now get back the chain */
3395 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3399 /* free up any _G.stackSpil locations allocated */
3400 applyToSet (_G.stackSpil, deallocStackSpil);
3402 setToNull ((void *) &_G.stackSpil);
3403 setToNull ((void *) &_G.spiltSet);
3404 /* mark all registers as free */