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 /*-----------------------------------------------------------------*/
963 static regs *getRegBitNoSpil()
967 /* try for a bit type */
968 if ((reg = allocReg (REG_BIT)))
971 /* try for gpr type */
972 if ((reg = allocReg (REG_GPR)))
977 /* just to make the compiler happy */
982 /*-----------------------------------------------------------------*/
983 /* symHasReg - symbol has a given register */
984 /*-----------------------------------------------------------------*/
986 symHasReg (symbol * sym, regs * reg)
990 for (i = 0; i < sym->nRegs; i++)
991 if (sym->regs[i] == reg)
997 /*-----------------------------------------------------------------*/
998 /* updateRegUsage - update the registers in use at the start of */
1000 /*-----------------------------------------------------------------*/
1002 updateRegUsage (iCode * ic)
1006 for (reg=0; reg<ds390_nRegs; reg++)
1008 if (regs390[reg].isFree)
1010 ic->riu &= ~(1<<regs390[reg].offset);
1014 ic->riu |= (1<<regs390[reg].offset);
1015 BitBankUsed |= (reg >= B0_IDX);
1020 /*-----------------------------------------------------------------*/
1021 /* deassignLRs - check the live to and if they have registers & are */
1022 /* not spilt then free up the registers */
1023 /*-----------------------------------------------------------------*/
1025 deassignLRs (iCode * ic, eBBlock * ebp)
1031 for (sym = hTabFirstItem (liveRanges, &k); sym;
1032 sym = hTabNextItem (liveRanges, &k))
1035 symbol *psym = NULL;
1036 /* if it does not end here */
1037 if (sym->liveTo > ic->seq)
1040 /* if it was spilt on stack then we can
1041 mark the stack spil location as free */
1046 sym->usl.spillLoc->isFree = 1;
1052 if (!bitVectBitValue (_G.regAssigned, sym->key))
1055 /* special case check if this is an IFX &
1056 the privious one was a pop and the
1057 previous one was not spilt then keep track
1059 if (ic->op == IFX && ic->prev &&
1060 ic->prev->op == IPOP &&
1061 !ic->prev->parmPush &&
1062 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1063 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1069 bitVectUnSetBit (_G.regAssigned, sym->key);
1071 /* if the result of this one needs registers
1072 and does not have it then assign it right
1074 if (IC_RESULT (ic) &&
1075 !(SKIP_IC2 (ic) || /* not a special icode */
1076 ic->op == JUMPTABLE ||
1081 POINTER_SET (ic)) &&
1082 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1083 result->liveTo > ic->seq && /* and will live beyond this */
1084 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1085 result->liveFrom == ic->seq && /* does not start before here */
1086 result->regType == sym->regType && /* same register types */
1087 result->nRegs && /* which needs registers */
1088 !result->isspilt && /* and does not already have them */
1090 !bitVectBitValue (_G.regAssigned, result->key) &&
1091 /* the number of free regs + number of regs in this LR
1092 can accomodate the what result Needs */
1093 ((nfreeRegsType (result->regType) +
1094 sym->nRegs) >= result->nRegs)
1098 for (i = 0; i < result->nRegs; i++)
1100 result->regs[i] = sym->regs[i];
1102 result->regs[i] = getRegGpr (ic, ebp, result);
1104 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1105 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1109 /* free the remaining */
1110 for (; i < sym->nRegs; i++)
1114 if (!symHasReg (psym, sym->regs[i]))
1115 freeReg (sym->regs[i]);
1118 freeReg (sym->regs[i]);
1125 /*-----------------------------------------------------------------*/
1126 /* reassignLR - reassign this to registers */
1127 /*-----------------------------------------------------------------*/
1129 reassignLR (operand * op)
1131 symbol *sym = OP_SYMBOL (op);
1134 /* not spilt any more */
1135 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1136 bitVectUnSetBit (_G.spiltSet, sym->key);
1138 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1139 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1143 for (i = 0; i < sym->nRegs; i++)
1144 sym->regs[i]->isFree = 0;
1147 /*-----------------------------------------------------------------*/
1148 /* willCauseSpill - determines if allocating will cause a spill */
1149 /*-----------------------------------------------------------------*/
1151 willCauseSpill (int nr, int rt)
1153 /* first check if there are any available registers
1154 of the type required */
1157 /* special case for pointer type
1158 if pointer type not avlb then
1159 check for type gpr */
1160 if (nFreeRegs (rt) >= nr)
1162 if (nFreeRegs (REG_GPR) >= nr)
1165 else if (rt == REG_BIT)
1167 if (nFreeRegs (rt) >= nr)
1172 if (ds390_ptrRegReq)
1174 if (nFreeRegs (rt) >= nr)
1179 if (nFreeRegs (REG_PTR) +
1180 nFreeRegs (REG_GPR) >= nr)
1185 /* it will cause a spil */
1189 /*-----------------------------------------------------------------*/
1190 /* positionRegs - the allocator can allocate same registers to res- */
1191 /* ult and operand, if this happens make sure they are in the same */
1192 /* position as the operand otherwise chaos results */
1193 /*-----------------------------------------------------------------*/
1195 positionRegs (symbol * result, symbol * opsym)
1197 int count = min (result->nRegs, opsym->nRegs);
1198 int i, j = 0, shared = 0;
1201 /* if the result has been spilt then cannot share */
1206 /* first make sure that they actually share */
1207 for (i = 0; i < count; i++)
1209 for (j = 0; j < count; j++)
1211 if (result->regs[i] == opsym->regs[j] && i != j)
1221 regs *tmp = result->regs[i];
1222 result->regs[i] = result->regs[j];
1223 result->regs[j] = tmp;
1230 /*-----------------------------------------------------------------*/
1231 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1232 /*-----------------------------------------------------------------*/
1233 bitVect *unusedLRs (eBBlock *ebp)
1235 bitVect *ret = NULL;
1239 if (!ebp) return NULL;
1240 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1241 sym = hTabNextItem(liveRanges,&key)) {
1243 if (notUsedInBlock(sym,ebp,NULL)) {
1244 ret = bitVectSetBit(ret,sym->key);
1251 /*-----------------------------------------------------------------*/
1252 /* deassignUnsedLRs - if this baisc block ends in a return then */
1253 /* deassign symbols not used in this block */
1254 /*-----------------------------------------------------------------*/
1255 bitVect *deassignUnsedLRs(eBBlock *ebp)
1257 bitVect *unused = NULL;
1260 switch (returnAtEnd(ebp)) {
1261 case 2: /* successor block ends in a return */
1262 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1264 case 1: /* this block ends in a return */
1265 unused = bitVectIntersect(unused,unusedLRs(ebp));
1270 for (i = 0 ; i < unused->size ; i++ ) {
1273 if (bitVectBitValue(unused,i)) {
1275 /* if assigned to registers */
1276 if (bitVectBitValue(_G.regAssigned,i)) {
1280 sym = hTabItemWithKey(liveRanges,i);
1281 /* remove it from regassigned & mark the
1283 bitVectUnSetBit(_G.regAssigned,i);
1284 for (j = 0 ; j < sym->nRegs; j++)
1285 freeReg(sym->regs[j]);
1287 /* not assigned to registers : remove from set*/
1288 bitVectUnSetBit(unused,i);
1296 /*-----------------------------------------------------------------*/
1297 /* reassignUnusedLRs - put registers to unused Live ranges */
1298 /*-----------------------------------------------------------------*/
1299 void reassignUnusedLRs (bitVect *unused)
1302 if (!unused) return ;
1304 for (i = 0 ; i < unused->size ; i++ ) {
1305 /* if unused : means it was assigned to registers before */
1306 if (bitVectBitValue(unused,i)) {
1310 /* put it back into reg set*/
1311 bitVectSetBit(_G.regAssigned,i) ;
1313 sym = hTabItemWithKey(liveRanges,i);
1314 /* make registers busy */
1315 for (j = 0 ; j < sym->nRegs; j++)
1316 sym->regs[j]->isFree = 0;
1321 /*------------------------------------------------------------------*/
1322 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1323 /* it should either have registers or have beed spilled. Otherwise, */
1324 /* there was an uninitialized variable, so just spill this to get */
1325 /* the operand in a valid state. */
1326 /*------------------------------------------------------------------*/
1328 verifyRegsAssigned (operand *op, iCode * ic)
1333 if (!IS_ITEMP (op)) return;
1335 sym = OP_SYMBOL (op);
1336 if (sym->isspilt) return;
1337 if (!sym->nRegs) return;
1338 if (sym->regs[0]) return;
1340 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1341 sym->prereqv ? sym->prereqv->name : sym->name);
1346 /*-----------------------------------------------------------------*/
1347 /* serialRegAssign - serially allocate registers to the variables */
1348 /*-----------------------------------------------------------------*/
1350 serialRegAssign (eBBlock ** ebbs, int count)
1354 /* for all blocks */
1355 for (i = 0; i < count; i++)
1359 bitVect *unusedLRs = NULL;
1361 if (ebbs[i]->noPath &&
1362 (ebbs[i]->entryLabel != entryLabel &&
1363 ebbs[i]->entryLabel != returnLabel))
1366 unusedLRs = deassignUnsedLRs(ebbs[i]);
1368 /* for all instructions do */
1369 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1373 /* if this is an ipop that means some live
1374 range will have to be assigned again */
1376 reassignLR (IC_LEFT (ic));
1378 /* if result is present && is a true symbol */
1379 if (IC_RESULT (ic) && ic->op != IFX &&
1380 IS_TRUE_SYMOP (IC_RESULT (ic)))
1381 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1383 /* take away registers from live
1384 ranges that end at this instruction */
1385 deassignLRs (ic, ebbs[i]);
1387 /* some don't need registers */
1388 if (SKIP_IC2 (ic) ||
1389 ic->op == JUMPTABLE ||
1393 (IC_RESULT (ic) && POINTER_SET (ic)))
1396 /* now we need to allocate registers
1397 only for the result */
1400 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1406 /* Make sure any spill location is definitely allocated */
1407 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1408 !sym->usl.spillLoc->allocreq)
1410 sym->usl.spillLoc->allocreq++;
1413 /* if it does not need or is spilt
1414 or is already assigned to registers
1415 or will not live beyond this instructions */
1418 bitVectBitValue (_G.regAssigned, sym->key) ||
1419 sym->liveTo <= ic->seq)
1422 /* if some liverange has been spilt at the block level
1423 and this one live beyond this block then spil this
1425 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1431 willCS = willCauseSpill (sym->nRegs, sym->regType);
1432 /* if this is a bit variable then don't use precious registers
1433 along with expensive bit-to-char conversions but just spill
1435 if (willCS && SPEC_NOUN(sym->etype) == V_BIT)
1441 /* if trying to allocate this will cause
1442 a spill and there is nothing to spill
1443 or this one is rematerializable then
1445 spillable = computeSpillable (ic);
1446 if (sym->remat || (willCS && bitVectIsZero (spillable)))
1452 /* If the live range preceeds the point of definition
1453 then ideally we must take into account registers that
1454 have been allocated after sym->liveFrom but freed
1455 before ic->seq. This is complicated, so spill this
1456 symbol instead and let fillGaps handle the allocation. */
1457 if (sym->liveFrom < ic->seq)
1463 /* if it has a spillocation & is used less than
1464 all other live ranges then spill this */
1466 if (sym->usl.spillLoc) {
1467 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1468 allLRs, ebbs[i], ic));
1469 if (leastUsed && leastUsed->used > sym->used) {
1474 /* if none of the liveRanges have a spillLocation then better
1475 to spill this one than anything else already assigned to registers */
1476 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1477 /* if this is local to this block then we might find a block spil */
1478 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1486 /* if we need ptr regs for the right side
1488 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1489 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned) PTRSIZE)
1494 /* else we assign registers to it */
1495 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1496 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1498 for (j = 0; j < sym->nRegs; j++)
1500 sym->regs[j] = NULL;
1501 if (sym->regType == REG_PTR)
1502 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1503 else if (sym->regType == REG_BIT)
1504 sym->regs[j] = getRegBit (sym);
1506 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1508 /* if the allocation failed which means
1509 this was spilt then break */
1514 /* if it shares registers with operands make sure
1515 that they are in the same position */
1516 if (!POINTER_SET(ic) && !POINTER_GET(ic))
1518 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1519 OP_SYMBOL (IC_LEFT (ic))->nRegs)
1521 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1522 OP_SYMBOL (IC_LEFT (ic)));
1524 /* do the same for the right operand */
1525 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1526 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1528 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1529 OP_SYMBOL (IC_RIGHT (ic)));
1541 reassignUnusedLRs(unusedLRs);
1544 /* Check for and fix any problems with uninitialized operands */
1545 for (i = 0; i < count; i++)
1549 if (ebbs[i]->noPath &&
1550 (ebbs[i]->entryLabel != entryLabel &&
1551 ebbs[i]->entryLabel != returnLabel))
1554 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1561 verifyRegsAssigned (IC_COND (ic), ic);
1565 if (ic->op == JUMPTABLE)
1567 verifyRegsAssigned (IC_JTCOND (ic), ic);
1571 verifyRegsAssigned (IC_RESULT (ic), ic);
1572 verifyRegsAssigned (IC_LEFT (ic), ic);
1573 verifyRegsAssigned (IC_RIGHT (ic), ic);
1578 /*-----------------------------------------------------------------*/
1579 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1580 /*-----------------------------------------------------------------*/
1581 static void fillGaps()
1585 int loop = 0, change;
1588 if (getenv("DISABLE_FILL_GAPS")) return;
1590 /* First try to do DPTRuse once more since now we know what got into
1593 while (loop++ < 10) {
1596 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1597 sym = hTabNextItem(liveRanges,&key)) {
1598 int size = getSize(sym->type);
1600 if (sym->liveFrom == sym->liveTo) continue;
1602 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1603 size < 4 && size > 1) {
1605 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1607 /* if this was assigned to registers then */
1608 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1609 /* take it out of the register assigned set */
1610 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1611 } else if (sym->usl.spillLoc) {
1612 sym->usl.spillLoc->allocreq--;
1613 sym->usl.spillLoc = NULL;
1617 sym->isspilt = sym->spillA = 0;
1621 /* try assigning other dptrs */
1622 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1623 /* if this was ssigned to registers then */
1624 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1625 /* take it out of the register assigned set */
1626 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1627 } else if (sym->usl.spillLoc) {
1628 sym->usl.spillLoc->allocreq--;
1629 sym->usl.spillLoc = NULL;
1632 sym->isspilt = sym->spillA = 0;
1637 /* look for liveranges that were spilt by the allocator */
1638 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1639 sym = hTabNextItem(liveRanges,&key)) {
1644 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1645 if (!sym->uses || !sym->defs) continue ;
1646 /* find the liveRanges this one clashes with, that are
1647 still assigned to registers & mark the registers as used*/
1648 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1652 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1653 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1656 clr = hTabItemWithKey(liveRanges,i);
1659 /* mark these registers as used */
1660 for (k = 0 ; k < clr->nRegs ; k++ )
1661 useReg(clr->regs[k]);
1664 if (willCauseSpill(sym->nRegs,sym->regType)) {
1665 /* NOPE :( clear all registers & and continue */
1670 /* THERE IS HOPE !!!! */
1671 for (i=0; i < sym->nRegs ; i++ ) {
1672 if (sym->regType == REG_PTR)
1673 sym->regs[i] = getRegPtrNoSpil ();
1675 sym->regs[i] = getRegGprNoSpil ();
1678 /* For all its definitions check if the registers
1679 allocated needs positioning NOTE: we can position
1680 only ONCE if more than One positioning required
1682 We may need to perform the checks twice; once to
1683 position the registers as needed, the second to
1684 verify any register repositioning is still
1688 for (pass=0; pass<2; pass++) {
1689 for (i = 0 ; i < sym->defs->size ; i++ ) {
1690 if (bitVectBitValue(sym->defs,i)) {
1692 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1693 if (SKIP_IC(ic)) continue;
1694 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1695 /* if left is assigned to registers */
1696 if (IS_SYMOP(IC_LEFT(ic)) &&
1697 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1698 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1700 if (IS_SYMOP(IC_RIGHT(ic)) &&
1701 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1702 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1704 if (pdone > 1) break;
1707 for (i = 0 ; i < sym->uses->size ; i++ ) {
1708 if (bitVectBitValue(sym->uses,i)) {
1710 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1711 if (SKIP_IC(ic)) continue;
1712 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1714 /* if result is assigned to registers */
1715 if (IS_SYMOP(IC_RESULT(ic)) &&
1716 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1717 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1719 if (pdone > 1) break;
1722 if (pdone == 0) break; /* second pass only if regs repositioned */
1723 if (pdone > 1) break;
1725 /* had to position more than once GIVE UP */
1727 /* UNDO all the changes we made to try this */
1729 for (i=0; i < sym->nRegs ; i++ ) {
1730 sym->regs[i] = NULL;
1733 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1734 "%s in function %s\n",
1735 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1738 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1739 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1740 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1741 sym->isspilt = sym->spillA = 0 ;
1742 sym->usl.spillLoc->allocreq--;
1743 sym->usl.spillLoc = NULL;
1751 /*-----------------------------------------------------------------*/
1752 /* findAllBitregs :- returns bit vector of all bit registers */
1753 /*-----------------------------------------------------------------*/
1755 findAllBitregs (void)
1757 bitVect *rmask = newBitVect (ds390_nRegs);
1760 for (j = 0; j < ds390_nRegs; j++)
1762 if (regs390[j].type == REG_BIT)
1763 rmask = bitVectSetBit (rmask, regs390[j].rIdx);
1769 /*-----------------------------------------------------------------*/
1770 /* ds390_allBitregs :- returns bit vector of all bit registers */
1771 /*-----------------------------------------------------------------*/
1773 ds390_allBitregs (void)
1775 return _G.allBitregs;
1778 /*-----------------------------------------------------------------*/
1779 /* rUmaskForOp :- returns register mask for an operand */
1780 /*-----------------------------------------------------------------*/
1782 ds390_rUmaskForOp (operand * op)
1788 /* only temporaries are assigned registers */
1792 sym = OP_SYMBOL (op);
1794 /* if spilt or no registers assigned to it
1796 if (sym->isspilt || !sym->nRegs)
1799 rumask = newBitVect (ds390_nRegs);
1801 for (j = 0; j < sym->nRegs; j++)
1803 rumask = bitVectSetBit (rumask,
1804 sym->regs[j]->rIdx);
1810 /*-----------------------------------------------------------------*/
1811 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1812 /*-----------------------------------------------------------------*/
1814 regsUsedIniCode (iCode * ic)
1816 bitVect *rmask = newBitVect (ds390_nRegs);
1818 /* do the special cases first */
1821 rmask = bitVectUnion (rmask,
1822 ds390_rUmaskForOp (IC_COND (ic)));
1826 /* for the jumptable */
1827 if (ic->op == JUMPTABLE)
1829 rmask = bitVectUnion (rmask,
1830 ds390_rUmaskForOp (IC_JTCOND (ic)));
1835 /* of all other cases */
1837 rmask = bitVectUnion (rmask,
1838 ds390_rUmaskForOp (IC_LEFT (ic)));
1842 rmask = bitVectUnion (rmask,
1843 ds390_rUmaskForOp (IC_RIGHT (ic)));
1846 rmask = bitVectUnion (rmask,
1847 ds390_rUmaskForOp (IC_RESULT (ic)));
1853 /*-----------------------------------------------------------------*/
1854 /* createRegMask - for each instruction will determine the regsUsed */
1855 /*-----------------------------------------------------------------*/
1857 createRegMask (eBBlock ** ebbs, int count)
1861 /* for all blocks */
1862 for (i = 0; i < count; i++)
1866 if (ebbs[i]->noPath &&
1867 (ebbs[i]->entryLabel != entryLabel &&
1868 ebbs[i]->entryLabel != returnLabel))
1871 /* for all instructions */
1872 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1877 if (SKIP_IC2 (ic) || !ic->rlive)
1880 /* first mark the registers used in this
1882 ic->rUsed = regsUsedIniCode (ic);
1883 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1885 /* now create the register mask for those
1886 registers that are in use : this is a
1887 super set of ic->rUsed */
1888 ic->rMask = newBitVect (ds390_nRegs + 1);
1890 /* for all live Ranges alive at this point */
1891 for (j = 1; j < ic->rlive->size; j++)
1896 /* if not alive then continue */
1897 if (!bitVectBitValue (ic->rlive, j))
1900 /* find the live range we are interested in */
1901 if (!(sym = hTabItemWithKey (liveRanges, j)))
1903 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1904 "createRegMask cannot find live range");
1905 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1909 /* special case for ruonly */
1910 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1911 int size = getSize(sym->type);
1913 for (k = 0 ; k < size; k++ )
1914 ic->rMask = bitVectSetBit (ic->rMask, j++);
1918 /* if no register assigned to it */
1919 if (!sym->nRegs || sym->isspilt)
1922 /* for all the registers allocated to it */
1923 for (k = 0; k < sym->nRegs; k++)
1926 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1932 /*-----------------------------------------------------------------*/
1933 /* rematStr - returns the rematerialized string for a remat var */
1934 /*-----------------------------------------------------------------*/
1936 rematStr (symbol * sym)
1939 iCode *ic = sym->rematiCode;
1946 /* if plus or minus print the right hand side */
1947 if (ic->op == '+' || ic->op == '-')
1949 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1950 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1953 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1957 /* cast then continue */
1958 if (IS_CAST_ICODE(ic)) {
1959 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1962 /* we reached the end */
1963 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1964 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1971 /*-----------------------------------------------------------------*/
1972 /* regTypeNum - computes the type & number of registers required */
1973 /*-----------------------------------------------------------------*/
1981 /* for each live range do */
1982 for (sym = hTabFirstItem (liveRanges, &k); sym;
1983 sym = hTabNextItem (liveRanges, &k))
1986 /* if used zero times then no registers needed */
1987 if ((sym->liveTo - sym->liveFrom) == 0)
1991 /* if the live range is a temporary */
1995 /* if the type is marked as a conditional */
1996 if (sym->regType == REG_CND)
1999 /* if used in return only then we don't
2001 if (sym->ruonly || sym->accuse)
2003 if (IS_AGGREGATE (sym->type) || sym->isptr)
2004 sym->type = aggrToPtr (sym->type, FALSE);
2008 /* if the symbol has only one definition &
2009 that definition is a get_pointer */
2010 if (bitVectnBitsOn (sym->defs) == 1 &&
2011 (ic = hTabItemWithKey (iCodehTab,
2012 bitVectFirstBit (sym->defs))) &&
2014 !IS_BITVAR (sym->etype) &&
2015 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
2018 if (ptrPseudoSymSafe (sym, ic))
2020 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
2024 /* if in data space or idata space then try to
2025 allocate pointer register */
2029 /* if not then we require registers */
2030 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
2031 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
2032 getSize (sym->type));
2036 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
2037 printTypeChain (sym->type, stderr);
2038 fprintf (stderr, "\n");
2041 /* determine the type of register required */
2042 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
2043 sym->regType = REG_PTR;
2044 else if (IS_BIT(sym->type))
2045 sym->regType = REG_BIT;
2047 sym->regType = REG_GPR;
2050 /* for the first run we don't provide */
2051 /* registers for true symbols we will */
2052 /* see how things go */
2058 /*-----------------------------------------------------------------*/
2059 /* freeAllRegs - mark all registers as free */
2060 /*-----------------------------------------------------------------*/
2066 for (i = 0; i < ds390_nRegs; i++)
2067 regs390[i].isFree = 1;
2069 for (i = B0_IDX; i < ds390_nBitRegs; i++)
2070 regs390[i].isFree = 1;
2073 /*-----------------------------------------------------------------*/
2074 /* deallocStackSpil - this will set the stack pointer back */
2075 /*-----------------------------------------------------------------*/
2077 DEFSETFUNC (deallocStackSpil)
2085 /*-----------------------------------------------------------------*/
2086 /* farSpacePackable - returns the packable icode for far variables */
2087 /*-----------------------------------------------------------------*/
2089 farSpacePackable (iCode * ic)
2093 /* go thru till we find a definition for the
2094 symbol on the right */
2095 for (dic = ic->prev; dic; dic = dic->prev)
2097 /* if the definition is a call then no */
2098 if ((dic->op == CALL || dic->op == PCALL) &&
2099 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2104 /* if shift by unknown amount then not */
2105 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2106 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2109 /* if pointer get and size > 1 */
2110 if (POINTER_GET (dic) &&
2111 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2114 if (POINTER_SET (dic) &&
2115 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2118 /* if any tree is a true symbol in far space */
2119 if (IC_RESULT (dic) &&
2120 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2121 isOperandInFarSpace (IC_RESULT (dic)))
2124 if (IC_RIGHT (dic) &&
2125 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2126 isOperandInFarSpace (IC_RIGHT (dic)) &&
2127 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2130 if (IC_LEFT (dic) &&
2131 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2132 isOperandInFarSpace (IC_LEFT (dic)) &&
2133 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2136 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2138 if ((dic->op == LEFT_OP ||
2139 dic->op == RIGHT_OP ||
2141 IS_OP_LITERAL (IC_RIGHT (dic)))
2151 /*-----------------------------------------------------------------*/
2152 /* packRegsForAssign - register reduction for assignment */
2153 /*-----------------------------------------------------------------*/
2155 packRegsForAssign (iCode * ic, eBBlock * ebp)
2159 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2160 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2161 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2166 /* if the true symbol is defined in far space or on stack
2167 then we should not since this will increase register pressure */
2169 if (isOperandInFarSpace (IC_RESULT (ic)))
2171 if ((dic = farSpacePackable (ic)))
2177 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2182 /* find the definition of iTempNN scanning backwards if we find a
2183 a use of the true symbol in before we find the definition then
2185 for (dic = ic->prev; dic; dic = dic->prev)
2187 /* if there is a function call then don't pack it */
2188 if ((dic->op == CALL || dic->op == PCALL))
2197 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2198 IS_OP_VOLATILE (IC_RESULT (dic)))
2204 if (IS_SYMOP (IC_RESULT (dic)) &&
2205 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2207 if (POINTER_SET (dic))
2213 if (IS_SYMOP (IC_RIGHT (dic)) &&
2214 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2215 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2221 if (IS_SYMOP (IC_LEFT (dic)) &&
2222 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2223 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2229 if (POINTER_SET (dic) &&
2230 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2238 return 0; /* did not find */
2240 /* if assignment then check that right is not a bit */
2241 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2243 sym_link *etype = operandType (IC_RESULT (dic));
2244 if (IS_BITFIELD (etype))
2246 /* if result is a bit too then it's ok */
2247 etype = operandType (IC_RESULT (ic));
2248 if (!IS_BITFIELD (etype))
2254 /* if the result is on stack or iaccess then it must be
2255 the same atleast one of the operands */
2256 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2257 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2260 /* the operation has only one symbol
2261 operator then we can pack */
2262 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2263 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2266 if (!((IC_LEFT (dic) &&
2267 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2269 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2273 /* found the definition */
2274 /* replace the result with the result of */
2275 /* this assignment and remove this assignment */
2276 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2278 IC_RESULT (dic) = IC_RESULT (ic);
2280 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2282 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2284 /* delete from liverange table also
2285 delete from all the points inbetween and the new
2287 for (sic = dic; sic != ic; sic = sic->next)
2289 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2290 if (IS_ITEMP (IC_RESULT (dic)))
2291 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2294 remiCodeFromeBBlock (ebp, ic);
2295 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2296 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2297 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2301 /*------------------------------------------------------------------*/
2302 /* findAssignToSym : scanning backwards looks for first assig found */
2303 /*------------------------------------------------------------------*/
2305 findAssignToSym (operand * op, iCode * ic)
2309 /* This routine is used to find sequences like
2311 ...; (intervening ops don't use iTempAA or modify FOO)
2312 blah = blah + iTempAA;
2314 and eliminate the use of iTempAA, freeing up its register for
2318 for (dic = ic->prev; dic; dic = dic->prev)
2321 /* if definition by assignment */
2322 if (dic->op == '=' &&
2323 !POINTER_SET (dic) &&
2324 IC_RESULT (dic)->key == op->key
2325 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2329 /* we are interested only if defined in far space */
2330 /* or in stack space in case of + & - */
2332 /* if assigned to a non-symbol then return
2334 if (!IS_SYMOP (IC_RIGHT (dic)))
2337 /* if the symbol is in far space then we should not */
2338 if (isOperandInFarSpace (IC_RIGHT (dic)))
2341 /* for + & - operations make sure that
2342 if it is on the stack it is the same
2343 as one of the three operands */
2344 if ((ic->op == '+' || ic->op == '-') &&
2345 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2348 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2349 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2350 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2358 /* if we find an usage then we cannot delete it */
2359 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2362 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2365 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2369 /* now make sure that the right side of dic
2370 is not defined between ic & dic */
2373 iCode *sic = dic->next;
2375 for (; sic != ic; sic = sic->next)
2376 if (IC_RESULT (sic) &&
2377 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2386 /*-----------------------------------------------------------------*/
2387 /* packRegsForSupport :- reduce some registers for support calls */
2388 /*-----------------------------------------------------------------*/
2390 packRegsForSupport (iCode * ic, eBBlock * ebp)
2394 /* for the left & right operand :- look to see if the
2395 left was assigned a true symbol in far space in that
2396 case replace them */
2397 if (IS_ITEMP (IC_LEFT (ic)) &&
2398 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2400 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2406 /* found it we need to remove it from the
2408 for (sic = dic; sic != ic; sic = sic->next) {
2409 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2410 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2413 wassert(IS_SYMOP(IC_LEFT (ic)));
2414 wassert(IS_SYMOP(IC_RIGHT (dic)));
2415 IC_LEFT (ic)->operand.symOperand =
2416 IC_RIGHT (dic)->operand.symOperand;
2417 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2418 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2419 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2420 remiCodeFromeBBlock (ebp, dic);
2421 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2425 /* do the same for the right operand */
2428 IS_ITEMP (IC_RIGHT (ic)) &&
2429 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2431 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2437 /* if this is a subtraction & the result
2438 is a true symbol in far space then don't pack */
2439 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2441 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2442 if (IN_FARSPACE (SPEC_OCLS (etype)))
2445 /* found it we need to remove it from the
2447 for (sic = dic; sic != ic; sic = sic->next) {
2448 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2449 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2452 wassert(IS_SYMOP(IC_RIGHT (ic)));
2453 wassert(IS_SYMOP(IC_RIGHT (dic)));
2454 IC_RIGHT (ic)->operand.symOperand =
2455 IC_RIGHT (dic)->operand.symOperand;
2456 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2457 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2458 remiCodeFromeBBlock (ebp, dic);
2459 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2460 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2467 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2470 /*-----------------------------------------------------------------*/
2471 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2472 /*-----------------------------------------------------------------*/
2473 static int packRegsDPTRnuse( operand *op , unsigned dptr)
2478 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2479 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2482 /* first check if any overlapping liverange has already been
2483 assigned to this DPTR */
2484 if (OP_SYMBOL(op)->clashes) {
2485 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2487 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2488 sym = hTabItemWithKey(liveRanges,i);
2489 if (sym->dptr == dptr) return 0;
2494 /* future for more dptrs */
2496 OP_SYMBOL(op)->dptr = dptr;
2500 /* DPTR1 is special since it is also used as a scratch by the backend .
2501 so we walk thru the entire live range of this operand and make sure
2502 DPTR1 will not be used by the backed . The logic here is to find out if
2503 more than one operand in an icode is in far space then we give up : we
2504 don't keep it live across functions for now
2507 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2508 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2509 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2512 if (ic->op == CALL || ic->op == PCALL) return 0;
2514 /* single operand icode are ok */
2515 if (ic->op == IFX || ic->op == IPUSH)
2518 if (ic->op == SEND ) {
2519 if (ic->argreg != 1 ) return 0;
2522 /* two special cases first */
2523 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2524 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2525 (isOperandInFarSpace(IC_RESULT(ic)) &&
2526 !isOperandInReg(IC_RESULT(ic)))) {
2530 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2531 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2532 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2533 !isOperandInReg(IC_RIGHT(ic)))) {
2537 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2538 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2539 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2540 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2541 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2545 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2546 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2547 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2548 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2549 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2552 /* same for right */
2553 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2554 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2555 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2556 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2557 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2561 // Check that no other ops in this range have been assigned to dptr1.
2562 // I don't understand why this is not caught by the first check, above.
2563 // But it isn't always, see bug 769624.
2564 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2565 (OP_SYMBOL(IC_RESULT(ic))->dptr == 1))
2567 //fprintf(stderr, "dptr1 already in use in live range #1\n");
2571 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2572 (OP_SYMBOL(IC_LEFT(ic))->dptr == 1))
2574 //fprintf(stderr, "dptr1 already in use in live range # 2\n");
2578 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2579 (OP_SYMBOL(IC_RIGHT(ic))->dptr == 1))
2581 //fprintf(stderr, "dptr1 already in use in live range # 3\n");
2585 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2586 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2588 if (nfs > 1) return 0;
2590 OP_SYMBOL(op)->dptr = dptr;
2594 /*-----------------------------------------------------------------*/
2595 /* packRegsDPTRuse : - will reduce some registers for single Use */
2596 /*-----------------------------------------------------------------*/
2598 packRegsDPTRuse (operand * op)
2600 /* go thru entire liveRange of this variable & check for
2601 other possible usage of DPTR , if we don't find it the
2602 assign this to DPTR (ruonly)
2607 sym_link *type, *etype;
2609 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2610 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2612 /* first check if any overlapping liverange has already been
2614 if (OP_SYMBOL(op)->clashes) {
2615 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2616 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2617 sym = hTabItemWithKey(liveRanges,i);
2618 if (sym->ruonly) return NULL ;
2623 /* no then go thru this guys live range */
2624 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2625 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2626 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2628 if (SKIP_IC3(ic)) continue;
2630 /* if PCALL cannot be sure give up */
2631 if (ic->op == PCALL) return NULL;
2633 /* if SEND & not the first parameter then giveup */
2634 if (ic->op == SEND && ic->argreg != 1 &&
2635 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2636 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2638 /* if CALL then make sure it is VOID || return value not used
2639 or the return value is assigned to this one */
2640 if (ic->op == CALL) {
2641 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2642 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2643 etype = getSpec(type = operandType(IC_RESULT(ic)));
2644 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2649 /* special case of add with a [remat] */
2650 if (ic->op == '+' &&
2651 OP_SYMBOL(IC_LEFT(ic))->remat &&
2652 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2653 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2657 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2658 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2661 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2662 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2664 /* conditionals can destroy 'b' - make sure B wont
2665 be used in this one*/
2666 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2667 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2668 getSize(operandType(op)) > 3) return NULL;
2670 /* if this is a cast to a bigger type */
2672 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2673 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2674 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2680 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2681 !isOperandEqual(IC_RESULT(ic),op) &&
2682 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2683 !isOperandInReg(IC_RESULT(ic))) ||
2684 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2686 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2687 !isOperandEqual(IC_RIGHT(ic),op) &&
2688 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2689 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2690 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2691 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2692 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2694 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2695 !isOperandEqual(IC_LEFT(ic),op) &&
2696 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2697 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2698 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2699 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2700 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2702 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2703 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2704 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2705 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2708 OP_SYMBOL(op)->ruonly = 1;
2709 if (OP_SYMBOL(op)->usl.spillLoc) {
2710 if (OP_SYMBOL(op)->spillA)
2711 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2712 OP_SYMBOL(op)->usl.spillLoc = NULL;
2717 /*-----------------------------------------------------------------*/
2718 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2719 /*-----------------------------------------------------------------*/
2721 isBitwiseOptimizable (iCode * ic)
2723 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2724 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2726 /* bitwise operations are considered optimizable
2727 under the following conditions (Jean-Louis VERN)
2739 if (IS_LITERAL (rtype) ||
2740 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2746 /*-----------------------------------------------------------------*/
2747 /* packRegsForAccUse - pack registers for acc use */
2748 /*-----------------------------------------------------------------*/
2750 packRegsForAccUse (iCode * ic)
2754 /* if this is an aggregate, e.g. a one byte char array */
2755 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2759 /* if we are calling a reentrant function that has stack parameters */
2760 if (ic->op == CALL &&
2761 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2762 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2765 if (ic->op == PCALL &&
2766 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2767 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2770 /* if + or - then it has to be one byte result */
2771 if ((ic->op == '+' || ic->op == '-')
2772 && getSize (operandType (IC_RESULT (ic))) > 1)
2775 /* if shift operation make sure right side is not a literal */
2776 if (ic->op == RIGHT_OP &&
2777 (isOperandLiteral (IC_RIGHT (ic)) ||
2778 getSize (operandType (IC_RESULT (ic))) > 1))
2781 if (ic->op == LEFT_OP &&
2782 (isOperandLiteral (IC_RIGHT (ic)) ||
2783 getSize (operandType (IC_RESULT (ic))) > 1))
2786 if (IS_BITWISE_OP (ic) &&
2787 getSize (operandType (IC_RESULT (ic))) > 1)
2791 /* has only one definition */
2792 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2795 /* has only one use */
2796 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2799 /* and the usage immediately follows this iCode */
2800 if (!(uic = hTabItemWithKey (iCodehTab,
2801 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2804 if (ic->next != uic)
2807 /* if it is a conditional branch then we definitely can */
2811 if (uic->op == JUMPTABLE)
2814 /* if the usage is not is an assignment
2815 or an arithmetic / bitwise / shift operation then not */
2816 if (POINTER_SET (uic) &&
2817 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2820 if (uic->op != '=' &&
2821 !IS_ARITHMETIC_OP (uic) &&
2822 !IS_BITWISE_OP (uic) &&
2823 uic->op != LEFT_OP &&
2824 uic->op != RIGHT_OP)
2827 /* if used in ^ operation then make sure right is not a
2829 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2832 /* if shift operation make sure right side is not a literal */
2833 if (uic->op == RIGHT_OP &&
2834 (isOperandLiteral (IC_RIGHT (uic)) ||
2835 getSize (operandType (IC_RESULT (uic))) > 1))
2838 if (uic->op == LEFT_OP &&
2839 (isOperandLiteral (IC_RIGHT (uic)) ||
2840 getSize (operandType (IC_RESULT (uic))) > 1))
2843 /* make sure that the result of this icode is not on the
2844 stack, since acc is used to compute stack offset */
2845 if (isOperandOnStack(IC_RESULT(uic)))
2848 /* if either one of them in far space then we cannot */
2849 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2850 isOperandInFarSpace (IC_LEFT (uic))) ||
2851 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2852 isOperandInFarSpace (IC_RIGHT (uic))))
2855 /* if the usage has only one operand then we can */
2856 if (IC_LEFT (uic) == NULL ||
2857 IC_RIGHT (uic) == NULL)
2860 /* make sure this is on the left side if not
2861 a '+' since '+' is commutative */
2862 if (ic->op != '+' &&
2863 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2866 /* if the other one is not on stack then we can */
2867 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2868 (IS_ITEMP (IC_RIGHT (uic)) ||
2869 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2870 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2873 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2874 (IS_ITEMP (IC_LEFT (uic)) ||
2875 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2876 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2882 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2886 /*-----------------------------------------------------------------*/
2887 /* packForPush - heuristics to reduce iCode for pushing */
2888 /*-----------------------------------------------------------------*/
2890 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2894 struct eBBlock * ebp = ebpp[blockno];
2896 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2899 /* must have only definition & one usage */
2900 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2901 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2904 /* find the definition */
2905 if (!(dic = hTabItemWithKey (iCodehTab,
2906 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2909 if (dic->op != '=' || POINTER_SET (dic))
2912 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2914 /* make sure the right side does not have any definitions
2916 dbv = OP_DEFS(IC_RIGHT(dic));
2917 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2918 if (bitVectBitValue (dbv, lic->key))
2921 /* make sure they have the same type */
2922 if (IS_SPEC(operandType(IC_LEFT(ic))))
2924 sym_link *itype=operandType(IC_LEFT(ic));
2925 sym_link *ditype=operandType(IC_RIGHT(dic));
2927 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2928 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2931 /* extend the live range of replaced operand if needed */
2932 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2933 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2934 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2935 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2936 OP_SYMBOL(IC_LEFT(ic))->clashes);
2938 for (lic = ic; lic && lic != dic; lic = lic->prev)
2940 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2941 if (IS_ITEMP (IC_RIGHT (dic)))
2942 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2944 /* we now we know that it has one & only one def & use
2945 and the that the definition is an assignment */
2946 IC_LEFT (ic) = IC_RIGHT (dic);
2948 remiCodeFromeBBlock (ebp, dic);
2949 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2950 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2953 /*-----------------------------------------------------------------*/
2954 /* packRegisters - does some transformations to reduce register */
2956 /*-----------------------------------------------------------------*/
2958 packRegisters (eBBlock ** ebpp, int blockno)
2962 eBBlock *ebp = ebpp[blockno];
2968 /* look for assignments of the form */
2969 /* iTempNN = TRueSym (someoperation) SomeOperand */
2971 /* TrueSym := iTempNN:1 */
2972 for (ic = ebp->sch; ic; ic = ic->next)
2974 /* find assignment of the form TrueSym := iTempNN:1 */
2975 if (ic->op == '=' && !POINTER_SET (ic))
2976 change += packRegsForAssign (ic, ebp);
2983 for (ic = ebp->sch; ic; ic = ic->next)
2985 /* Fix for bug #979599: */
2988 /* Look for two subsequent iCodes with */
2990 /* _c = iTemp & op; */
2991 /* and replace them by */
2994 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2996 ic->prev->op == '=' &&
2997 IS_ITEMP (IC_LEFT (ic)) &&
2998 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2999 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
3001 iCode* ic_prev = ic->prev;
3002 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
3004 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
3005 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
3007 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
3008 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
3009 prev_result_sym->liveTo == ic->seq)
3011 prev_result_sym->liveTo = ic_prev->seq;
3014 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
3016 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
3018 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
3020 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
3021 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
3022 remiCodeFromeBBlock (ebp, ic_prev);
3023 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
3027 /* if this is an itemp & result of an address of a true sym
3028 then mark this as rematerialisable */
3029 if (ic->op == ADDRESS_OF &&
3030 IS_ITEMP (IC_RESULT (ic)) &&
3031 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
3032 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3033 !OP_SYMBOL (IC_LEFT (ic))->onStack)
3036 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3037 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3038 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3042 /* if this is an itemp & used as a pointer
3043 & assigned to a literal then remat */
3044 if (IS_ASSIGN_ICODE(ic) &&
3045 IS_ITEMP(IC_RESULT(ic)) &&
3046 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3047 isOperandLiteral(IC_RIGHT(ic)))
3049 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3050 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3051 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3054 /* if straight assignment then carry remat flag if
3055 this is the only definition */
3056 if (ic->op == '=' &&
3057 !POINTER_SET (ic) &&
3058 IS_SYMOP (IC_RIGHT (ic)) &&
3059 OP_SYMBOL (IC_RIGHT (ic))->remat &&
3060 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
3061 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
3063 OP_SYMBOL (IC_RESULT (ic))->remat =
3064 OP_SYMBOL (IC_RIGHT (ic))->remat;
3065 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
3066 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
3069 /* if cast to a generic pointer & the pointer being
3070 cast is remat, then we can remat this cast as well */
3071 if (ic->op == CAST &&
3072 IS_SYMOP(IC_RIGHT(ic)) &&
3073 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
3074 OP_SYMBOL(IC_RIGHT(ic))->remat )
3076 sym_link *to_type = operandType(IC_LEFT(ic));
3077 sym_link *from_type = operandType(IC_RIGHT(ic));
3078 if (IS_GENPTR(to_type) && IS_PTR(from_type))
3080 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3081 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3082 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3086 /* if this is a +/- operation with a rematerizable
3087 then mark this as rematerializable as well */
3088 if ((ic->op == '+' || ic->op == '-') &&
3089 (IS_SYMOP (IC_LEFT (ic)) &&
3090 IS_ITEMP (IC_RESULT (ic)) &&
3091 OP_SYMBOL (IC_LEFT (ic))->remat &&
3092 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3093 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3094 IS_OP_LITERAL (IC_RIGHT (ic))))
3097 //int i = operandLitValue(IC_RIGHT(ic));
3098 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3099 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3100 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3103 /* mark the pointer usages */
3104 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic)))
3105 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3107 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic)))
3108 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3110 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
3111 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3113 if (ic->op == RECEIVE && ic->argreg == 1 &&
3114 IS_SYMOP (IC_RESULT (ic)) &&
3115 getSize (operandType(IC_RESULT(ic))) <= 3)
3116 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
3118 if (ic->op == SEND && ic->argreg == 1 &&
3119 IS_SYMOP(IC_LEFT(ic)) &&
3120 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
3121 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
3125 /* if we are using a symbol on the stack
3126 then we should say ds390_ptrRegReq */
3127 if (options.useXstack && ic->parmPush
3128 && (ic->op == IPUSH || ic->op == IPOP))
3130 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3131 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
3132 OP_SYMBOL (IC_COND (ic))->iaccess +
3133 (SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata));
3134 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3135 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
3136 OP_SYMBOL (IC_JTCOND (ic))->iaccess +
3137 (SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata));
3140 if (IS_SYMOP (IC_LEFT (ic)))
3141 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
3142 OP_SYMBOL (IC_LEFT (ic))->iaccess +
3143 (SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata));
3144 if (IS_SYMOP (IC_RIGHT (ic)))
3145 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
3146 OP_SYMBOL (IC_RIGHT (ic))->iaccess +
3147 (SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata));
3148 if (IS_SYMOP (IC_RESULT (ic)))
3149 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
3150 OP_SYMBOL (IC_RESULT (ic))->iaccess +
3151 (SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata));
3155 /* if the condition of an if instruction
3156 is defined in the previous instruction and
3157 this is the only usage then
3158 mark the itemp as a conditional */
3159 if ((IS_CONDITIONAL (ic) ||
3160 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3161 ic->next && ic->next->op == IFX &&
3162 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3163 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3164 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3166 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3170 /* reduce for support function calls */
3171 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3172 packRegsForSupport (ic, ebp);
3174 /* some cases the redundant moves can
3175 can be eliminated for return statements . Can be elminated for the first SEND */
3176 if ((ic->op == RETURN ||
3177 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
3178 !isOperandInFarSpace (IC_LEFT (ic)) &&
3181 packRegsDPTRuse (IC_LEFT (ic));
3184 if (ic->op == CALL) {
3185 sym_link *ftype = operandType(IC_LEFT(ic));
3186 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
3187 !IFFUNC_ISBUILTIN(ftype)) {
3188 packRegsDPTRuse (IC_RESULT (ic));
3192 /* if pointer set & left has a size more than
3193 one and right is not in far space */
3194 if (POINTER_SET (ic) &&
3195 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3196 IS_SYMOP (IC_RESULT (ic)) &&
3197 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3198 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3199 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
3201 packRegsDPTRuse (IC_RESULT (ic));
3204 /* if pointer get */
3205 if (POINTER_GET (ic) &&
3206 !isOperandInFarSpace (IC_RESULT (ic)) &&
3207 IS_SYMOP (IC_LEFT (ic)) &&
3208 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3209 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3210 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
3212 packRegsDPTRuse (IC_LEFT (ic));
3215 /* if this is a cast for intergral promotion then
3216 check if it's the only use of the definition of the
3217 operand being casted/ if yes then replace
3218 the result of that arithmetic operation with
3219 this result and get rid of the cast */
3222 sym_link *fromType = operandType (IC_RIGHT (ic));
3223 sym_link *toType = operandType (IC_LEFT (ic));
3225 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3226 getSize (fromType) != getSize (toType) &&
3227 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3230 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3233 if (IS_ARITHMETIC_OP (dic))
3235 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3236 IC_RESULT (dic) = IC_RESULT (ic);
3237 remiCodeFromeBBlock (ebp, ic);
3238 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3239 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3240 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3244 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3250 /* if the type from and type to are the same
3251 then if this is the only use then packit */
3252 if (compareType (operandType (IC_RIGHT (ic)),
3253 operandType (IC_LEFT (ic))) == 1)
3255 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3258 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3259 IC_RESULT (dic) = IC_RESULT (ic);
3260 remiCodeFromeBBlock (ebp, ic);
3261 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3262 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3263 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3271 iTempNN := (some variable in farspace) V1
3276 if (ic->op == IPUSH || ic->op == SEND)
3278 packForPush (ic, ebpp, blockno);
3282 /* pack registers for accumulator use, when the
3283 result of an arithmetic or bit wise operation
3284 has only one use, that use is immediately following
3285 the defintion and the using iCode has only one
3286 operand or has two operands but one is literal &
3287 the result of that operation is not on stack then
3288 we can leave the result of this operation in acc:b
3290 if ((IS_ARITHMETIC_OP (ic)
3291 || IS_CONDITIONAL(ic)
3292 || IS_BITWISE_OP (ic)
3293 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3294 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3296 IS_ITEMP (IC_RESULT (ic)) &&
3297 getSize (operandType (IC_RESULT (ic))) <= 2)
3299 packRegsForAccUse (ic);
3303 /*-----------------------------------------------------------------*/
3304 /* assignRegisters - assigns registers to each live range as need */
3305 /*-----------------------------------------------------------------*/
3307 ds390_assignRegisters (ebbIndex * ebbi)
3309 eBBlock ** ebbs = ebbi->bbOrder;
3310 int count = ebbi->count;
3314 setToNull ((void *) &_G.funcrUsed);
3315 setToNull ((void *) &_G.regAssigned);
3316 setToNull ((void *) &_G.totRegAssigned);
3317 setToNull ((void *) &_G.funcrUsed);
3318 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3319 if ((currFunc && IFFUNC_ISREENT (currFunc->type)) || options.stackAuto)
3327 ds390_nRegs = 12 + ds390_nBitRegs;
3328 _G.allBitregs = findAllBitregs ();
3330 if (options.model != MODEL_FLAT24)
3331 options.stack10bit = 0;
3332 /* change assignments this will remove some
3333 live ranges reducing some register pressure */
3334 for (i = 0; i < count; i++)
3335 packRegisters (ebbs, i);
3337 /* liveranges probably changed by register packing
3338 so we compute them again */
3339 recomputeLiveRanges (ebbs, count);
3341 if (options.dump_pack)
3342 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3344 /* first determine for each live range the number of
3345 registers & the type of registers required for each */
3348 /* and serially allocate registers */
3349 serialRegAssign (ebbs, count);
3354 ds390_nRegs = 12 + ds390_nBitRegs;
3356 /* if stack was extended then tell the user */
3359 /* werror(W_TOOMANY_SPILS,"stack", */
3360 /* _G.stackExtend,currFunc->name,""); */
3366 /* werror(W_TOOMANY_SPILS,"data space", */
3367 /* _G.dataExtend,currFunc->name,""); */
3371 /* after that create the register mask
3372 for each of the instruction */
3373 createRegMask (ebbs, count);
3375 /* redo that offsets for stacked automatic variables */
3377 redoStackOffsets ();
3379 /* make sure r0 & r1 are flagged as used if they might be used */
3381 if (currFunc && ds390_ptrRegReq)
3383 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3384 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3387 if (options.dump_rassgn)
3389 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3390 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3393 /* do the overlaysegment stuff SDCCmem.c */
3394 doOverlays (ebbs, count);
3396 /* now get back the chain */
3397 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3401 /* free up any _G.stackSpil locations allocated */
3402 applyToSet (_G.stackSpil, deallocStackSpil);
3404 setToNull ((void *) &_G.stackSpil);
3405 setToNull ((void *) &_G.spiltSet);
3406 /* mark all registers as free */