1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
47 bitVect *totRegAssigned; /* final set of LRs that got into registers */
50 bitVect *funcrUsed; /* registers used in a function */
56 /* Shared with gen.c */
57 int ds390_ptrRegReq; /* one byte pointer register required */
63 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
64 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
65 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
66 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
67 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
68 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
69 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
70 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
71 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
72 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
73 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
74 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
75 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
76 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
77 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
78 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
79 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
80 {REG_CND, CND_IDX, REG_GPR, "C", "C", "xreg", 0, 0, 0},
83 static void spillThis (symbol *);
84 static void freeAllRegs ();
85 static iCode * packRegsDPTRuse (operand *);
86 static int packRegsDPTRnuse (operand *,int);
88 /*-----------------------------------------------------------------*/
89 /* allocReg - allocates register of given type */
90 /*-----------------------------------------------------------------*/
96 for (i = 0; i < ds390_nRegs; i++)
99 /* if type is given as 0 then any
100 free register will do */
104 regs390[i].isFree = 0;
107 bitVectSetBit (currFunc->regsUsed, i);
110 /* other wise look for specific type
112 if (regs390[i].isFree &&
113 regs390[i].type == type)
115 regs390[i].isFree = 0;
118 bitVectSetBit (currFunc->regsUsed, i);
125 /*-----------------------------------------------------------------*/
126 /* ds390_regWithIdx - returns pointer to register wit index number */
127 /*-----------------------------------------------------------------*/
129 ds390_regWithIdx (int idx)
133 for (i = 0; i < ds390_nRegs; i++)
134 if (regs390[i].rIdx == idx)
137 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
138 "regWithIdx not found");
142 /*-----------------------------------------------------------------*/
143 /* freeReg - frees a register */
144 /*-----------------------------------------------------------------*/
151 /*-----------------------------------------------------------------*/
152 /* useReg - marks a register as used */
153 /*-----------------------------------------------------------------*/
161 /*-----------------------------------------------------------------*/
162 /* nFreeRegs - returns number of free registers */
163 /*-----------------------------------------------------------------*/
170 for (i = 0; i < ds390_nRegs; i++)
171 if (regs390[i].isFree && regs390[i].type == type)
176 /*-----------------------------------------------------------------*/
177 /* nfreeRegsType - free registers with type */
178 /*-----------------------------------------------------------------*/
180 nfreeRegsType (int type)
185 if ((nfr = nFreeRegs (type)) == 0)
186 return nFreeRegs (REG_GPR);
189 return nFreeRegs (type);
193 /*-----------------------------------------------------------------*/
194 /* allDefsOutOfRange - all definitions are out of a range */
195 /*-----------------------------------------------------------------*/
197 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
204 for (i = 0; i < defs->size; i++)
208 if (bitVectBitValue (defs, i) &&
209 (ic = hTabItemWithKey (iCodehTab, i)) &&
210 (ic->seq >= fseq && ic->seq <= toseq))
219 /*-----------------------------------------------------------------*/
220 /* isOperandInReg - returns true if operand is currently in regs */
221 /*-----------------------------------------------------------------*/
222 static int isOperandInReg(operand *op)
224 if (!IS_SYMOP(op)) return 0;
225 if (OP_SYMBOL(op)->ruonly) return 1;
226 if (OP_SYMBOL(op)->accuse) return 1;
227 if (OP_SYMBOL(op)->dptr) return 1;
228 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
231 /*-----------------------------------------------------------------*/
232 /* computeSpillable - given a point find the spillable live ranges */
233 /*-----------------------------------------------------------------*/
235 computeSpillable (iCode * ic)
239 /* spillable live ranges are those that are live at this
240 point . the following categories need to be subtracted
242 a) - those that are already spilt
243 b) - if being used by this one
244 c) - defined by this one */
246 spillable = bitVectCopy (ic->rlive);
248 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
250 bitVectCplAnd (spillable, ic->uses); /* used in this one */
251 bitVectUnSetBit (spillable, ic->defKey);
252 spillable = bitVectIntersect (spillable, _G.regAssigned);
257 /*-----------------------------------------------------------------*/
258 /* noSpilLoc - return true if a variable has no spil location */
259 /*-----------------------------------------------------------------*/
261 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
263 return (sym->usl.spillLoc ? 0 : 1);
266 /*-----------------------------------------------------------------*/
267 /* hasSpilLoc - will return 1 if the symbol has spil location */
268 /*-----------------------------------------------------------------*/
270 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
272 return (sym->usl.spillLoc ? 1 : 0);
275 /*-----------------------------------------------------------------*/
276 /* directSpilLoc - will return 1 if the splilocation is in direct */
277 /*-----------------------------------------------------------------*/
279 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
281 if (sym->usl.spillLoc &&
282 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
288 /*-----------------------------------------------------------------*/
289 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
290 /* but is not used as a pointer */
291 /*-----------------------------------------------------------------*/
293 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
295 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
298 /*-----------------------------------------------------------------*/
299 /* rematable - will return 1 if the remat flag is set */
300 /*-----------------------------------------------------------------*/
302 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
307 /*-----------------------------------------------------------------*/
308 /* notUsedInBlock - not used in this block */
309 /*-----------------------------------------------------------------*/
311 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
313 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
314 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq) &&
315 allDefsOutOfRange (sym->uses, ebp->fSeq, ebp->lSeq));
316 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
319 /*-----------------------------------------------------------------*/
320 /* notUsedInRemaining - not used or defined in remain of the block */
321 /*-----------------------------------------------------------------*/
323 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
325 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
326 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
329 /*-----------------------------------------------------------------*/
330 /* allLRs - return true for all */
331 /*-----------------------------------------------------------------*/
333 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
338 /*-----------------------------------------------------------------*/
339 /* liveRangesWith - applies function to a given set of live range */
340 /*-----------------------------------------------------------------*/
342 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
343 eBBlock * ebp, iCode * ic)
348 if (!lrs || !lrs->size)
351 for (i = 1; i < lrs->size; i++)
354 if (!bitVectBitValue (lrs, i))
357 /* if we don't find it in the live range
358 hash table we are in serious trouble */
359 if (!(sym = hTabItemWithKey (liveRanges, i)))
361 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
362 "liveRangesWith could not find liveRange");
366 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
367 addSetHead (&rset, sym);
374 /*-----------------------------------------------------------------*/
375 /* leastUsedLR - given a set determines which is the least used */
376 /*-----------------------------------------------------------------*/
378 leastUsedLR (set * sset)
380 symbol *sym = NULL, *lsym = NULL;
382 sym = lsym = setFirstItem (sset);
387 for (; lsym; lsym = setNextItem (sset))
390 /* if usage is the same then prefer
391 the spill the smaller of the two */
392 if (lsym->used == sym->used)
393 if (getSize (lsym->type) < getSize (sym->type))
397 if (lsym->used < sym->used)
402 setToNull ((void **) &sset);
407 /*-----------------------------------------------------------------*/
408 /* noOverLap - will iterate through the list looking for over lap */
409 /*-----------------------------------------------------------------*/
411 noOverLap (set * itmpStack, symbol * fsym)
415 for (sym = setFirstItem (itmpStack); sym;
416 sym = setNextItem (itmpStack))
418 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
423 /*-----------------------------------------------------------------*/
424 /* isFree - will return 1 if the a free spil location is found */
425 /*-----------------------------------------------------------------*/
430 V_ARG (symbol **, sloc);
431 V_ARG (symbol *, fsym);
433 /* if already found */
437 /* if it is free && and the itmp assigned to
438 this does not have any overlapping live ranges
439 with the one currently being assigned and
440 the size can be accomodated */
442 noOverLap (sym->usl.itmpStack, fsym) &&
443 getSize (sym->type) >= getSize (fsym->type))
452 /*-----------------------------------------------------------------*/
453 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
454 /*-----------------------------------------------------------------*/
456 spillLRWithPtrReg (symbol * forSym)
462 if (!_G.regAssigned ||
463 bitVectIsZero (_G.regAssigned))
466 r0 = ds390_regWithIdx (R0_IDX);
467 r1 = ds390_regWithIdx (R1_IDX);
469 /* for all live ranges */
470 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
471 lrsym = hTabNextItem (liveRanges, &k))
475 /* if no registers assigned to it or
477 /* if it does not overlap with this then
478 not need to spill it */
480 if (lrsym->isspilt || !lrsym->nRegs ||
481 (lrsym->liveTo < forSym->liveFrom))
484 /* go thru the registers : if it is either
485 r0 or r1 then spil it */
486 for (j = 0; j < lrsym->nRegs; j++)
487 if (lrsym->regs[j] == r0 ||
488 lrsym->regs[j] == r1)
497 /*-----------------------------------------------------------------*/
498 /* createStackSpil - create a location on the stack to spil */
499 /*-----------------------------------------------------------------*/
501 createStackSpil (symbol * sym)
504 int useXstack, model, noOverlay;
508 /* first go try and find a free one that is already
509 existing on the stack */
510 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
512 /* found a free one : just update & return */
513 sym->usl.spillLoc = sloc;
516 addSetHead (&sloc->usl.itmpStack, sym);
520 /* could not then have to create one , this is the hard part
521 we need to allocate this on the stack : this is really a
522 hack!! but cannot think of anything better at this time */
524 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
526 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
531 sloc = newiTemp (slocBuffer);
533 /* set the type to the spilling symbol */
534 sloc->type = copyLinkChain (sym->type);
535 sloc->etype = getSpec (sloc->type);
536 if (options.model == MODEL_SMALL) {
537 SPEC_SCLS (sloc->etype) = S_DATA;
539 SPEC_SCLS (sloc->etype) = S_XDATA;
541 SPEC_EXTR (sloc->etype) = 0;
542 SPEC_STAT (sloc->etype) = 0;
543 SPEC_VOLATILE(sloc->etype) = 0;
544 SPEC_ABSA(sloc->etype) = 0;
546 /* we don't allow it to be allocated`
547 onto the external stack since : so we
548 temporarily turn it off ; we also
549 turn off memory model to prevent
550 the spil from going to the external storage
551 and turn off overlaying
554 useXstack = options.useXstack;
555 model = options.model;
556 noOverlay = options.noOverlay;
557 options.noOverlay = 1;
559 /* options.model = options.useXstack = 0; */
563 options.useXstack = useXstack;
564 options.model = model;
565 options.noOverlay = noOverlay;
566 sloc->isref = 1; /* to prevent compiler warning */
568 /* if it is on the stack then update the stack */
569 if (IN_STACK (sloc->etype))
571 currFunc->stack += getSize (sloc->type);
572 _G.stackExtend += getSize (sloc->type);
575 _G.dataExtend += getSize (sloc->type);
577 /* add it to the _G.stackSpil set */
578 addSetHead (&_G.stackSpil, sloc);
579 sym->usl.spillLoc = sloc;
582 /* add it to the set of itempStack set
583 of the spill location */
584 addSetHead (&sloc->usl.itmpStack, sym);
588 /*-----------------------------------------------------------------*/
589 /* isSpiltOnStack - returns true if the spil location is on stack */
590 /*-----------------------------------------------------------------*/
592 isSpiltOnStack (symbol * sym)
602 /* if (sym->_G.stackSpil) */
605 if (!sym->usl.spillLoc)
608 etype = getSpec (sym->usl.spillLoc->type);
609 if (IN_STACK (etype))
615 /*-----------------------------------------------------------------*/
616 /* spillThis - spils a specific operand */
617 /*-----------------------------------------------------------------*/
619 spillThis (symbol * sym)
622 /* if this is rematerializable or has a spillLocation
623 we are okay, else we need to create a spillLocation
625 if (!(sym->remat || sym->usl.spillLoc))
626 createStackSpil (sym);
629 /* mark it has spilt & put it in the spilt set */
630 sym->isspilt = sym->spillA = 1;
631 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
633 bitVectUnSetBit (_G.regAssigned, sym->key);
634 bitVectUnSetBit (_G.totRegAssigned, sym->key);
636 for (i = 0; i < sym->nRegs; i++)
640 freeReg (sym->regs[i]);
644 /* if spilt on stack then free up r0 & r1
645 if they could have been assigned to some
647 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
649 ds390_ptrRegReq += !options.stack10bit;
650 spillLRWithPtrReg (sym);
653 if (sym->usl.spillLoc && !sym->remat)
654 sym->usl.spillLoc->allocreq++;
658 /*-----------------------------------------------------------------*/
659 /* selectSpil - select a iTemp to spil : rather a simple procedure */
660 /*-----------------------------------------------------------------*/
662 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
664 bitVect *lrcs = NULL;
668 /* get the spillable live ranges */
669 lrcs = computeSpillable (ic);
671 /* get all live ranges that are rematerizable */
672 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
675 /* return the least used of these */
676 return leastUsedLR (selectS);
679 /* get live ranges with spillLocations in direct space */
680 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
682 sym = leastUsedLR (selectS);
683 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
684 sym->usl.spillLoc->rname :
685 sym->usl.spillLoc->name));
687 /* mark it as allocation required */
688 sym->usl.spillLoc->allocreq++;
692 /* if the symbol is local to the block then */
693 if (forSym->liveTo < ebp->lSeq)
696 /* check if there are any live ranges allocated
697 to registers that are not used in this block */
698 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
700 sym = leastUsedLR (selectS);
701 /* if this is not rematerializable */
710 /* check if there are any live ranges that not
711 used in the remainder of the block */
712 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
714 sym = leastUsedLR (selectS);
727 /* find live ranges with spillocation && not used as pointers */
728 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
731 sym = leastUsedLR (selectS);
732 /* mark this as allocation required */
733 sym->usl.spillLoc->allocreq++;
737 /* find live ranges with spillocation */
738 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
741 sym = leastUsedLR (selectS);
742 sym->usl.spillLoc->allocreq++;
746 /* couldn't find then we need to create a spil
747 location on the stack , for which one? the least
749 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
752 /* return a created spil location */
753 sym = createStackSpil (leastUsedLR (selectS));
754 sym->usl.spillLoc->allocreq++;
758 /* this is an extreme situation we will spill
759 this one : happens very rarely but it does happen */
765 /*-----------------------------------------------------------------*/
766 /* spilSomething - spil some variable & mark registers as free */
767 /*-----------------------------------------------------------------*/
769 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
774 /* get something we can spil */
775 ssym = selectSpil (ic, ebp, forSym);
777 /* mark it as spilt */
778 ssym->isspilt = ssym->spillA = 1;
779 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
781 /* mark it as not register assigned &
782 take it away from the set */
783 bitVectUnSetBit (_G.regAssigned, ssym->key);
784 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
786 /* mark the registers as free */
787 for (i = 0; i < ssym->nRegs; i++)
789 freeReg (ssym->regs[i]);
791 /* if spilt on stack then free up r0 & r1
792 if they could have been assigned to as gprs */
793 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
796 spillLRWithPtrReg (ssym);
799 /* if this was a block level spil then insert push & pop
800 at the start & end of block respectively */
803 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
804 /* add push to the start of the block */
805 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
806 ebp->sch->next : ebp->sch));
807 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
808 /* add pop to the end of the block */
809 addiCodeToeBBlock (ebp, nic, NULL);
812 /* if spilt because not used in the remainder of the
813 block then add a push before this instruction and
814 a pop at the end of the block */
815 if (ssym->remainSpil)
818 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
819 /* add push just before this instruction */
820 addiCodeToeBBlock (ebp, nic, ic);
822 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
823 /* add pop to the end of the block */
824 addiCodeToeBBlock (ebp, nic, NULL);
833 /*-----------------------------------------------------------------*/
834 /* getRegPtr - will try for PTR if not a GPR type if not spil */
835 /*-----------------------------------------------------------------*/
837 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
842 /* try for a ptr type */
843 if ((reg = allocReg (REG_PTR)))
846 /* try for gpr type */
847 if ((reg = allocReg (REG_GPR)))
850 /* we have to spil */
851 if (!spilSomething (ic, ebp, sym))
854 /* this looks like an infinite loop but
855 in really selectSpil will abort */
859 /*-----------------------------------------------------------------*/
860 /* getRegGpr - will try for GPR if not spil */
861 /*-----------------------------------------------------------------*/
863 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
868 /* try for gpr type */
869 if ((reg = allocReg (REG_GPR)))
872 if (!ds390_ptrRegReq)
873 if ((reg = allocReg (REG_PTR)))
876 /* we have to spil */
877 if (!spilSomething (ic, ebp, sym))
880 /* this looks like an infinite loop but
881 in really selectSpil will abort */
885 /*-----------------------------------------------------------------*/
886 /* getRegPtrNoSpil - get it cannot split */
887 /*-----------------------------------------------------------------*/
888 static regs *getRegPtrNoSpil()
892 /* try for a ptr type */
893 if ((reg = allocReg (REG_PTR)))
896 /* try for gpr type */
897 if ((reg = allocReg (REG_GPR)))
903 /*-----------------------------------------------------------------*/
904 /* getRegGprNoSpil - get it cannot split */
905 /*-----------------------------------------------------------------*/
906 static regs *getRegGprNoSpil()
910 if ((reg = allocReg (REG_GPR)))
913 if (!ds390_ptrRegReq)
914 if ((reg = allocReg (REG_PTR)))
920 /*-----------------------------------------------------------------*/
921 /* symHasReg - symbol has a given register */
922 /*-----------------------------------------------------------------*/
924 symHasReg (symbol * sym, regs * reg)
928 for (i = 0; i < sym->nRegs; i++)
929 if (sym->regs[i] == reg)
935 /*-----------------------------------------------------------------*/
936 /* deassignLRs - check the live to and if they have registers & are */
937 /* not spilt then free up the registers */
938 /*-----------------------------------------------------------------*/
940 deassignLRs (iCode * ic, eBBlock * ebp)
946 for (sym = hTabFirstItem (liveRanges, &k); sym;
947 sym = hTabNextItem (liveRanges, &k))
951 /* if it does not end here */
952 if (sym->liveTo > ic->seq)
955 /* if it was spilt on stack then we can
956 mark the stack spil location as free */
961 sym->usl.spillLoc->isFree = 1;
967 if (!bitVectBitValue (_G.regAssigned, sym->key))
970 /* special case check if this is an IFX &
971 the privious one was a pop and the
972 previous one was not spilt then keep track
974 if (ic->op == IFX && ic->prev &&
975 ic->prev->op == IPOP &&
976 !ic->prev->parmPush &&
977 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
978 psym = OP_SYMBOL (IC_LEFT (ic->prev));
984 bitVectUnSetBit (_G.regAssigned, sym->key);
986 /* if the result of this one needs registers
987 and does not have it then assign it right
989 if (IC_RESULT (ic) &&
990 !(SKIP_IC2 (ic) || /* not a special icode */
991 ic->op == JUMPTABLE ||
997 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
998 result->liveTo > ic->seq && /* and will live beyond this */
999 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1000 result->regType == sym->regType && /* same register types */
1001 result->nRegs && /* which needs registers */
1002 !result->isspilt && /* and does not already have them */
1004 !bitVectBitValue (_G.regAssigned, result->key) &&
1005 /* the number of free regs + number of regs in this LR
1006 can accomodate the what result Needs */
1007 ((nfreeRegsType (result->regType) +
1008 sym->nRegs) >= result->nRegs)
1012 for (i = 0; i < result->nRegs; i++)
1014 result->regs[i] = sym->regs[i];
1016 result->regs[i] = getRegGpr (ic, ebp, result);
1018 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1019 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1023 /* free the remaining */
1024 for (; i < sym->nRegs; i++)
1028 if (!symHasReg (psym, sym->regs[i]))
1029 freeReg (sym->regs[i]);
1032 freeReg (sym->regs[i]);
1039 /*-----------------------------------------------------------------*/
1040 /* reassignLR - reassign this to registers */
1041 /*-----------------------------------------------------------------*/
1043 reassignLR (operand * op)
1045 symbol *sym = OP_SYMBOL (op);
1048 /* not spilt any more */
1049 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1050 bitVectUnSetBit (_G.spiltSet, sym->key);
1052 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1053 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1057 for (i = 0; i < sym->nRegs; i++)
1058 sym->regs[i]->isFree = 0;
1061 /*-----------------------------------------------------------------*/
1062 /* willCauseSpill - determines if allocating will cause a spill */
1063 /*-----------------------------------------------------------------*/
1065 willCauseSpill (int nr, int rt)
1067 /* first check if there are any avlb registers
1068 of te type required */
1071 /* special case for pointer type
1072 if pointer type not avlb then
1073 check for type gpr */
1074 if (nFreeRegs (rt) >= nr)
1076 if (nFreeRegs (REG_GPR) >= nr)
1081 if (ds390_ptrRegReq)
1083 if (nFreeRegs (rt) >= nr)
1088 if (nFreeRegs (REG_PTR) +
1089 nFreeRegs (REG_GPR) >= nr)
1094 /* it will cause a spil */
1098 /*-----------------------------------------------------------------*/
1099 /* positionRegs - the allocator can allocate same registers to res- */
1100 /* ult and operand, if this happens make sure they are in the same */
1101 /* position as the operand otherwise chaos results */
1102 /*-----------------------------------------------------------------*/
1104 positionRegs (symbol * result, symbol * opsym)
1106 int count = min (result->nRegs, opsym->nRegs);
1107 int i, j = 0, shared = 0;
1110 /* if the result has been spilt then cannot share */
1115 /* first make sure that they actually share */
1116 for (i = 0; i < count; i++)
1118 for (j = 0; j < count; j++)
1120 if (result->regs[i] == opsym->regs[j] && i != j)
1130 regs *tmp = result->regs[i];
1131 result->regs[i] = result->regs[j];
1132 result->regs[j] = tmp;
1139 /*-----------------------------------------------------------------*/
1140 /* serialRegAssign - serially allocate registers to the variables */
1141 /*-----------------------------------------------------------------*/
1143 serialRegAssign (eBBlock ** ebbs, int count)
1147 /* for all blocks */
1148 for (i = 0; i < count; i++)
1153 if (ebbs[i]->noPath &&
1154 (ebbs[i]->entryLabel != entryLabel &&
1155 ebbs[i]->entryLabel != returnLabel))
1158 /* if (returnsAtEnd(ebbs[i])) */
1160 /* of all instructions do */
1161 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1164 /* if this is an ipop that means some live
1165 range will have to be assigned again */
1167 reassignLR (IC_LEFT (ic));
1169 /* if result is present && is a true symbol */
1170 if (IC_RESULT (ic) && ic->op != IFX &&
1171 IS_TRUE_SYMOP (IC_RESULT (ic)))
1172 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1174 /* take away registers from live
1175 ranges that end at this instruction */
1176 deassignLRs (ic, ebbs[i]);
1178 /* some don't need registers */
1179 if (SKIP_IC2 (ic) ||
1180 ic->op == JUMPTABLE ||
1184 (IC_RESULT (ic) && POINTER_SET (ic)))
1187 /* now we need to allocate registers
1188 only for the result */
1191 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1197 /* if it does not need or is spilt
1198 or is already assigned to registers
1199 or will not live beyond this instructions */
1202 bitVectBitValue (_G.regAssigned, sym->key) ||
1203 sym->liveTo <= ic->seq)
1206 /* if some liverange has been spilt at the block level
1207 and this one live beyond this block then spil this
1209 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1214 /* if trying to allocate this will cause
1215 a spill and there is nothing to spill
1216 or this one is rematerializable then
1218 willCS = willCauseSpill (sym->nRegs, sym->regType);
1219 spillable = computeSpillable (ic);
1221 (willCS && bitVectIsZero (spillable)))
1229 /* if it has a spillocation & is used less than
1230 all other live ranges then spill this */
1232 if (sym->usl.spillLoc) {
1233 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1234 allLRs, ebbs[i], ic));
1235 if (leastUsed && leastUsed->used > sym->used) {
1240 /* if none of the liveRanges have a spillLocation then better
1241 to spill this one than anything else already assigned to registers */
1242 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1243 /* if this is local to this block then we might find a block spil */
1244 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1252 /* if we need ptr regs for the right side
1254 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1255 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1256 <= (unsigned) PTRSIZE)
1261 /* else we assign registers to it */
1262 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1263 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1265 for (j = 0; j < sym->nRegs; j++)
1267 if (sym->regType == REG_PTR)
1268 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1270 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1272 /* if the allocation falied which means
1273 this was spilt then break */
1277 /* if it shares registers with operands make sure
1278 that they are in the same position */
1279 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1280 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1281 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1282 OP_SYMBOL (IC_LEFT (ic)));
1283 /* do the same for the right operand */
1284 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1285 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1286 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1287 OP_SYMBOL (IC_RIGHT (ic)));
1300 /*-----------------------------------------------------------------*/
1301 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1302 /*-----------------------------------------------------------------*/
1303 static void fillGaps()
1307 int loop = 0, change;
1309 if (getenv("DISABLE_FILL_GAPS")) return;
1311 /* First try to do DPTRuse once more since now we know what got into
1314 while (loop++ < 10) {
1317 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1318 sym = hTabNextItem(liveRanges,&key)) {
1319 int size = getSize(sym->type);
1321 if (sym->liveFrom == sym->liveTo) continue;
1323 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1324 size < 4 && size > 1) {
1326 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1328 /* if this was ssigned to registers then */
1329 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1330 /* take it out of the register assigned set */
1331 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1332 } else if (sym->usl.spillLoc) {
1333 sym->usl.spillLoc->allocreq--;
1334 sym->usl.spillLoc = NULL;
1338 sym->isspilt = sym->spillA = 0;
1342 /* try assigning other dptrs */
1343 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1344 /* if this was ssigned to registers then */
1345 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1346 /* take it out of the register assigned set */
1347 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1348 } else if (sym->usl.spillLoc) {
1349 sym->usl.spillLoc->allocreq--;
1350 sym->usl.spillLoc = NULL;
1352 /* printf("Allocated %s in function %s to DPTR1\n",sym->name,currFunc->name); */
1354 sym->isspilt = sym->spillA = 0;
1359 /* look for livernages that was spilt by the allocator */
1360 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1361 sym = hTabNextItem(liveRanges,&key)) {
1366 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1367 if (!sym->uses || !sym->defs) continue ;
1368 /* find the liveRanges this one clashes with, that are
1369 still assigned to registers & mark the registers as used*/
1370 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1374 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1375 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1378 assert (clr = hTabItemWithKey(liveRanges,i));
1380 /* mark these registers as used */
1381 for (k = 0 ; k < clr->nRegs ; k++ )
1382 useReg(clr->regs[k]);
1385 if (willCauseSpill(sym->nRegs,sym->regType)) {
1386 /* NOPE :( clear all registers & and continue */
1391 /* THERE IS HOPE !!!! */
1392 for (i=0; i < sym->nRegs ; i++ ) {
1393 if (sym->regType == REG_PTR)
1394 sym->regs[i] = getRegPtrNoSpil ();
1396 sym->regs[i] = getRegGprNoSpil ();
1399 /* for all its definitions & uses check if the registers
1400 allocated needs positioning NOTE: we can position
1401 only ONCE if more than One positioning required
1404 for (i = 0 ; i < sym->defs->size ; i++ ) {
1405 if (bitVectBitValue(sym->defs,i)) {
1407 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1408 if (SKIP_IC(ic)) continue;
1409 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1410 /* if left is assigned to registers */
1411 if (IS_SYMOP(IC_LEFT(ic)) &&
1412 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1413 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1415 if (IS_SYMOP(IC_RIGHT(ic)) &&
1416 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1417 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1419 if (pdone > 1) break;
1422 for (i = 0 ; i < sym->uses->size ; i++ ) {
1423 if (bitVectBitValue(sym->uses,i)) {
1425 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1426 if (SKIP_IC(ic)) continue;
1427 if (!IS_ASSIGN_ICODE(ic)) continue ;
1429 /* if result is assigned to registers */
1430 if (IS_SYMOP(IC_RESULT(ic)) &&
1431 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1432 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1434 if (pdone > 1) break;
1437 /* had to position more than once GIVE UP */
1439 /* UNDO all the changes we made to try this */
1441 for (i=0; i < sym->nRegs ; i++ ) {
1442 sym->regs[i] = NULL;
1445 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1446 "%s in function %s\n",
1447 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1450 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1451 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1452 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1453 sym->isspilt = sym->spillA = 0 ;
1454 sym->usl.spillLoc->allocreq--;
1455 sym->usl.spillLoc = NULL;
1463 /*-----------------------------------------------------------------*/
1464 /* rUmaskForOp :- returns register mask for an operand */
1465 /*-----------------------------------------------------------------*/
1467 ds390_rUmaskForOp (operand * op)
1473 /* only temporaries are assigned registers */
1477 sym = OP_SYMBOL (op);
1479 /* if spilt or no registers assigned to it
1481 if (sym->isspilt || !sym->nRegs)
1484 rumask = newBitVect (ds390_nRegs);
1486 for (j = 0; j < sym->nRegs; j++)
1488 rumask = bitVectSetBit (rumask,
1489 sym->regs[j]->rIdx);
1495 /*-----------------------------------------------------------------*/
1496 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1497 /*-----------------------------------------------------------------*/
1499 regsUsedIniCode (iCode * ic)
1501 bitVect *rmask = newBitVect (ds390_nRegs);
1503 /* do the special cases first */
1506 rmask = bitVectUnion (rmask,
1507 ds390_rUmaskForOp (IC_COND (ic)));
1511 /* for the jumptable */
1512 if (ic->op == JUMPTABLE)
1514 rmask = bitVectUnion (rmask,
1515 ds390_rUmaskForOp (IC_JTCOND (ic)));
1520 /* of all other cases */
1522 rmask = bitVectUnion (rmask,
1523 ds390_rUmaskForOp (IC_LEFT (ic)));
1527 rmask = bitVectUnion (rmask,
1528 ds390_rUmaskForOp (IC_RIGHT (ic)));
1531 rmask = bitVectUnion (rmask,
1532 ds390_rUmaskForOp (IC_RESULT (ic)));
1538 /*-----------------------------------------------------------------*/
1539 /* createRegMask - for each instruction will determine the regsUsed */
1540 /*-----------------------------------------------------------------*/
1542 createRegMask (eBBlock ** ebbs, int count)
1546 /* for all blocks */
1547 for (i = 0; i < count; i++)
1551 if (ebbs[i]->noPath &&
1552 (ebbs[i]->entryLabel != entryLabel &&
1553 ebbs[i]->entryLabel != returnLabel))
1556 /* for all instructions */
1557 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1562 if (SKIP_IC2 (ic) || !ic->rlive)
1565 /* first mark the registers used in this
1567 ic->rUsed = regsUsedIniCode (ic);
1568 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1570 /* now create the register mask for those
1571 registers that are in use : this is a
1572 super set of ic->rUsed */
1573 ic->rMask = newBitVect (ds390_nRegs + 1);
1575 /* for all live Ranges alive at this point */
1576 for (j = 1; j < ic->rlive->size; j++)
1581 /* if not alive then continue */
1582 if (!bitVectBitValue (ic->rlive, j))
1585 /* find the live range we are interested in */
1586 if (!(sym = hTabItemWithKey (liveRanges, j)))
1588 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1589 "createRegMask cannot find live range");
1593 /* special case for ruonly */
1594 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1595 int size = getSize(sym->type);
1597 for (k = 0 ; k < size; k++ )
1598 ic->rMask = bitVectSetBit (ic->rMask, j++);
1601 /* if no register assigned to it */
1602 if (!sym->nRegs || sym->isspilt)
1605 /* for all the registers allocated to it */
1606 for (k = 0; k < sym->nRegs; k++)
1609 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1615 /*-----------------------------------------------------------------*/
1616 /* rematStr - returns the rematerialized string for a remat var */
1617 /*-----------------------------------------------------------------*/
1619 rematStr (symbol * sym)
1622 iCode *ic = sym->rematiCode;
1627 /* if plus or minus print the right hand side */
1628 if (ic->op == '+' || ic->op == '-')
1630 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1633 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1636 /* cast then continue */
1637 if (IS_CAST_ICODE(ic)) {
1638 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1641 /* we reached the end */
1642 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1649 /*-----------------------------------------------------------------*/
1650 /* regTypeNum - computes the type & number of registers required */
1651 /*-----------------------------------------------------------------*/
1659 /* for each live range do */
1660 for (sym = hTabFirstItem (liveRanges, &k); sym;
1661 sym = hTabNextItem (liveRanges, &k))
1664 /* if used zero times then no registers needed */
1665 if ((sym->liveTo - sym->liveFrom) == 0)
1669 /* if the live range is a temporary */
1673 /* if the type is marked as a conditional */
1674 if (sym->regType == REG_CND)
1677 /* if used in return only then we don't
1679 if (sym->ruonly || sym->accuse)
1681 if (IS_AGGREGATE (sym->type) || sym->isptr)
1682 sym->type = aggrToPtr (sym->type, FALSE);
1686 /* if the symbol has only one definition &
1687 that definition is a get_pointer */
1688 if (bitVectnBitsOn (sym->defs) == 1 &&
1689 (ic = hTabItemWithKey (iCodehTab,
1690 bitVectFirstBit (sym->defs))) &&
1693 !IS_BITVAR (sym->etype))
1695 /* and that pointer is remat in data space */
1696 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1697 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1698 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1701 /* create a psuedo symbol & force a spil */
1702 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1703 psym->type = sym->type;
1704 psym->etype = sym->etype;
1705 strcpy (psym->rname, psym->name);
1707 sym->usl.spillLoc = psym;
1711 /* if in data space or idata space then try to
1712 allocate pointer register */
1716 /* if not then we require registers */
1717 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1718 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1719 getSize (sym->type));
1723 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1724 printTypeChain (sym->type, stderr);
1725 fprintf (stderr, "\n");
1728 /* determine the type of register required */
1729 if (sym->nRegs == 1 &&
1730 IS_PTR (sym->type) &&
1732 sym->regType = REG_PTR;
1734 sym->regType = REG_GPR;
1738 /* for the first run we don't provide */
1739 /* registers for true symbols we will */
1740 /* see how things go */
1746 /*-----------------------------------------------------------------*/
1747 /* freeAllRegs - mark all registers as free */
1748 /*-----------------------------------------------------------------*/
1754 for (i = 0; i < ds390_nRegs; i++)
1755 regs390[i].isFree = 1;
1758 /*-----------------------------------------------------------------*/
1759 /* deallocStackSpil - this will set the stack pointer back */
1760 /*-----------------------------------------------------------------*/
1762 DEFSETFUNC (deallocStackSpil)
1770 /*-----------------------------------------------------------------*/
1771 /* farSpacePackable - returns the packable icode for far variables */
1772 /*-----------------------------------------------------------------*/
1774 farSpacePackable (iCode * ic)
1778 /* go thru till we find a definition for the
1779 symbol on the right */
1780 for (dic = ic->prev; dic; dic = dic->prev)
1783 /* if the definition is a call then no */
1784 if ((dic->op == CALL || dic->op == PCALL) &&
1785 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1790 /* if shift by unknown amount then not */
1791 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1792 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1795 /* if pointer get and size > 1 */
1796 if (POINTER_GET (dic) &&
1797 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1800 if (POINTER_SET (dic) &&
1801 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1804 /* if any three is a true symbol in far space */
1805 if (IC_RESULT (dic) &&
1806 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1807 isOperandInFarSpace (IC_RESULT (dic)))
1810 if (IC_RIGHT (dic) &&
1811 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1812 isOperandInFarSpace (IC_RIGHT (dic)) &&
1813 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1816 if (IC_LEFT (dic) &&
1817 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1818 isOperandInFarSpace (IC_LEFT (dic)) &&
1819 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1822 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1824 if ((dic->op == LEFT_OP ||
1825 dic->op == RIGHT_OP ||
1827 IS_OP_LITERAL (IC_RIGHT (dic)))
1837 /*-----------------------------------------------------------------*/
1838 /* packRegsForAssign - register reduction for assignment */
1839 /*-----------------------------------------------------------------*/
1841 packRegsForAssign (iCode * ic, eBBlock * ebp)
1845 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1846 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1847 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1852 /* if the true symbol is defined in far space or on stack
1853 then we should not since this will increase register pressure */
1855 if (isOperandInFarSpace (IC_RESULT (ic)))
1857 if ((dic = farSpacePackable (ic)))
1863 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1868 /* find the definition of iTempNN scanning backwards if we find a
1869 a use of the true symbol in before we find the definition then
1871 for (dic = ic->prev; dic; dic = dic->prev)
1873 /* if there is a function call then don't pack it */
1874 if ((dic->op == CALL || dic->op == PCALL))
1883 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1884 IS_OP_VOLATILE (IC_RESULT (dic)))
1890 if (IS_SYMOP (IC_RESULT (dic)) &&
1891 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1893 if (POINTER_SET (dic))
1899 if (IS_SYMOP (IC_RIGHT (dic)) &&
1900 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1901 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1907 if (IS_SYMOP (IC_LEFT (dic)) &&
1908 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1909 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1915 if (POINTER_SET (dic) &&
1916 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1924 return 0; /* did not find */
1926 /* if the result is on stack or iaccess then it must be
1927 the same atleast one of the operands */
1928 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1929 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1932 /* the operation has only one symbol
1933 operator then we can pack */
1934 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1935 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1938 if (!((IC_LEFT (dic) &&
1939 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1941 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1945 /* found the definition */
1946 /* replace the result with the result of */
1947 /* this assignment and remove this assignment */
1948 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1950 IC_RESULT (dic) = IC_RESULT (ic);
1952 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1954 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1956 /* delete from liverange table also
1957 delete from all the points inbetween and the new
1959 for (sic = dic; sic != ic; sic = sic->next)
1961 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1962 if (IS_ITEMP (IC_RESULT (dic)))
1963 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1966 remiCodeFromeBBlock (ebp, ic);
1967 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1968 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1969 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1974 /*-----------------------------------------------------------------*/
1975 /* findAssignToSym : scanning backwards looks for first assig found */
1976 /*-----------------------------------------------------------------*/
1978 findAssignToSym (operand * op, iCode * ic)
1982 for (dic = ic->prev; dic; dic = dic->prev)
1985 /* if definition by assignment */
1986 if (dic->op == '=' &&
1987 !POINTER_SET (dic) &&
1988 IC_RESULT (dic)->key == op->key
1989 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1993 /* we are interested only if defined in far space */
1994 /* or in stack space in case of + & - */
1996 /* if assigned to a non-symbol then return
1998 if (!IS_SYMOP (IC_RIGHT (dic)))
2001 /* if the symbol is in far space then
2003 if (isOperandInFarSpace (IC_RIGHT (dic)))
2006 /* for + & - operations make sure that
2007 if it is on the stack it is the same
2008 as one of the three operands */
2009 if ((ic->op == '+' || ic->op == '-') &&
2010 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2013 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2014 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2015 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2023 /* if we find an usage then we cannot delete it */
2024 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2027 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2030 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2034 /* now make sure that the right side of dic
2035 is not defined between ic & dic */
2038 iCode *sic = dic->next;
2040 for (; sic != ic; sic = sic->next)
2041 if (IC_RESULT (sic) &&
2042 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2051 /*-----------------------------------------------------------------*/
2052 /* packRegsForSupport :- reduce some registers for support calls */
2053 /*-----------------------------------------------------------------*/
2055 packRegsForSupport (iCode * ic, eBBlock * ebp)
2059 /* for the left & right operand :- look to see if the
2060 left was assigned a true symbol in far space in that
2061 case replace them */
2062 if (IS_ITEMP (IC_LEFT (ic)) &&
2063 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2065 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2071 /* found it we need to remove it from the
2073 for (sic = dic; sic != ic; sic = sic->next) {
2074 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2075 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2078 IC_LEFT (ic)->operand.symOperand =
2079 IC_RIGHT (dic)->operand.symOperand;
2080 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2081 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2082 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2083 remiCodeFromeBBlock (ebp, dic);
2084 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2088 /* do the same for the right operand */
2091 IS_ITEMP (IC_RIGHT (ic)) &&
2092 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2094 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2100 /* if this is a subtraction & the result
2101 is a true symbol in far space then don't pack */
2102 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2104 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2105 if (IN_FARSPACE (SPEC_OCLS (etype)))
2108 /* found it we need to remove it from the
2110 for (sic = dic; sic != ic; sic = sic->next) {
2111 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2112 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2115 IC_RIGHT (ic)->operand.symOperand =
2116 IC_RIGHT (dic)->operand.symOperand;
2117 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2118 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2119 remiCodeFromeBBlock (ebp, dic);
2120 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2121 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2128 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2131 /*-----------------------------------------------------------------*/
2132 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2133 /*-----------------------------------------------------------------*/
2134 static int packRegsDPTRnuse( operand *op , int dptr)
2139 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2140 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2143 /* first check if any overlapping liverange has already been
2144 assigned to this DPTR */
2145 if (OP_SYMBOL(op)->clashes) {
2146 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2148 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2149 sym = hTabItemWithKey(liveRanges,i);
2150 if (sym->dptr == dptr) return 0;
2155 /* future for more dptrs */
2157 OP_SYMBOL(op)->dptr = dptr;
2161 /* DPTR1 is special since it is also used as a scratch by the backend .
2162 so we walk thru the entire live range of this operand and make sure
2163 DPTR1 will not be used by the backed . The logic here is to find out if
2164 more than one operand in an icode is in far space then we give up : we
2165 don't keep it live across functions for now
2168 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2169 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2170 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2173 if (ic->op == CALL || ic->op == PCALL) return 0;
2175 /* single operand icode are ok */
2176 if (ic->op == IFX || ic->op == IPUSH)
2179 if (ic->op == SEND ) {
2180 if (ic->argreg != 1) return 0;
2183 /* two special cases first */
2184 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2185 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2186 (isOperandInFarSpace(IC_RESULT(ic)) &&
2187 !isOperandInReg(IC_RESULT(ic)))) {
2191 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2192 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2193 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2194 !isOperandInReg(IC_RIGHT(ic)))) {
2198 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2199 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2200 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2201 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2202 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2206 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2207 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2208 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2209 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2210 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2213 /* same for right */
2214 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2215 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2216 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2217 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2218 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2222 if (nfs > 1) return 0;
2224 OP_SYMBOL(op)->dptr = dptr;
2228 /*-----------------------------------------------------------------*/
2229 /* packRegsDPTRuse : - will reduce some registers for single Use */
2230 /*-----------------------------------------------------------------*/
2232 packRegsDPTRuse (operand * op)
2234 /* go thru entire liveRange of this variable & check for
2235 other possible usage of DPTR , if we don't find it the
2236 assign this to DPTR (ruonly)
2241 sym_link *type, *etype;
2243 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2244 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2246 /* first check if any overlapping liverange has already been
2248 if (OP_SYMBOL(op)->clashes) {
2249 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2250 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2251 sym = hTabItemWithKey(liveRanges,i);
2252 if (sym->ruonly) return NULL ;
2257 /* no then go thru this guys live range */
2258 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2259 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2260 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2262 if (SKIP_IC3(ic)) continue;
2264 /* if PCALL cannot be sure give up */
2265 if (ic->op == PCALL) return NULL;
2267 /* if SEND & not the first parameter then giveup */
2268 if (ic->op == SEND && ic->argreg != 1) return NULL;
2270 /* if CALL then make sure it is VOID || return value not used
2271 or the return value is assigned to this one */
2272 if (ic->op == CALL) {
2273 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2274 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2275 etype = getSpec(type = operandType(IC_RESULT(ic)));
2276 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2281 /* special case of add with a [remat] */
2282 if (ic->op == '+' &&
2283 OP_SYMBOL(IC_LEFT(ic))->remat &&
2284 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2285 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2289 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2290 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2293 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2294 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2296 /* conditionals can destroy 'b' - make sure B wont
2297 be used in this one*/
2298 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2299 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2300 getSize(operandType(op)) > 3) return NULL;
2302 /* if this is a cast to a bigger type */
2304 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2305 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2306 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2312 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2313 !isOperandEqual(IC_RESULT(ic),op) &&
2314 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2315 !isOperandInReg(IC_RESULT(ic))) ||
2316 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2318 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2319 !isOperandEqual(IC_RIGHT(ic),op) &&
2320 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2321 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2322 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2323 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2324 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2326 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2327 !isOperandEqual(IC_LEFT(ic),op) &&
2328 (OP_SYMBOL(IC_LEFT(ic))->liveTo > ic->seq ||
2329 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2330 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2331 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2332 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2334 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2335 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2336 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2337 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2340 OP_SYMBOL(op)->ruonly = 1;
2344 /*-----------------------------------------------------------------*/
2345 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2346 /*-----------------------------------------------------------------*/
2348 isBitwiseOptimizable (iCode * ic)
2350 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2351 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2353 /* bitwise operations are considered optimizable
2354 under the following conditions (Jean-Louis VERN)
2366 if ( IS_LITERAL (rtype) ||
2367 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2373 /*-----------------------------------------------------------------*/
2374 /* packRegsForAccUse - pack registers for acc use */
2375 /*-----------------------------------------------------------------*/
2377 packRegsForAccUse (iCode * ic)
2381 /* if this is an aggregate, e.g. a one byte char array */
2382 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2386 /* if we are calling a reentrant function that has stack parameters */
2387 if (ic->op == CALL &&
2388 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2389 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2392 if (ic->op == PCALL &&
2393 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2394 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2397 /* if + or - then it has to be one byte result */
2398 if ((ic->op == '+' || ic->op == '-')
2399 && getSize (operandType (IC_RESULT (ic))) > 1)
2402 /* if shift operation make sure right side is not a literal */
2403 if (ic->op == RIGHT_OP &&
2404 (isOperandLiteral (IC_RIGHT (ic)) ||
2405 getSize (operandType (IC_RESULT (ic))) > 1))
2408 if (ic->op == LEFT_OP &&
2409 (isOperandLiteral (IC_RIGHT (ic)) ||
2410 getSize (operandType (IC_RESULT (ic))) > 1))
2413 if (IS_BITWISE_OP (ic) &&
2414 getSize (operandType (IC_RESULT (ic))) > 1)
2418 /* has only one definition */
2419 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2422 /* has only one use */
2423 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2426 /* and the usage immediately follows this iCode */
2427 if (!(uic = hTabItemWithKey (iCodehTab,
2428 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2431 if (ic->next != uic)
2434 /* if it is a conditional branch then we definitely can */
2438 if (uic->op == JUMPTABLE)
2441 /* if the usage is not is an assignment
2442 or an arithmetic / bitwise / shift operation then not */
2443 if (POINTER_SET (uic) &&
2444 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2447 if (uic->op != '=' &&
2448 !IS_ARITHMETIC_OP (uic) &&
2449 !IS_BITWISE_OP (uic) &&
2450 uic->op != LEFT_OP &&
2451 uic->op != RIGHT_OP)
2454 /* if used in ^ operation then make sure right is not a
2456 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2459 /* if shift operation make sure right side is not a literal */
2460 if (uic->op == RIGHT_OP &&
2461 (isOperandLiteral (IC_RIGHT (uic)) ||
2462 getSize (operandType (IC_RESULT (uic))) > 1))
2465 if (uic->op == LEFT_OP &&
2466 (isOperandLiteral (IC_RIGHT (uic)) ||
2467 getSize (operandType (IC_RESULT (uic))) > 1))
2470 /* make sure that the result of this icode is not on the
2471 stack, since acc is used to compute stack offset */
2472 if (isOperandOnStack(IC_RESULT(uic)))
2475 /* if either one of them in far space then we cannot */
2476 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2477 isOperandInFarSpace (IC_LEFT (uic))) ||
2478 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2479 isOperandInFarSpace (IC_RIGHT (uic))))
2482 /* if the usage has only one operand then we can */
2483 if (IC_LEFT (uic) == NULL ||
2484 IC_RIGHT (uic) == NULL)
2487 /* make sure this is on the left side if not
2488 a '+' since '+' is commutative */
2489 if (ic->op != '+' &&
2490 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2493 /* if the other one is not on stack then we can */
2494 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2495 (IS_ITEMP (IC_RIGHT (uic)) ||
2496 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2497 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2500 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2501 (IS_ITEMP (IC_LEFT (uic)) ||
2502 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2503 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2509 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2514 /*-----------------------------------------------------------------*/
2515 /* packForPush - hueristics to reduce iCode for pushing */
2516 /*-----------------------------------------------------------------*/
2518 packForPush (iCode * ic, eBBlock * ebp)
2523 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2526 /* must have only definition & one usage */
2527 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2528 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2531 /* find the definition */
2532 if (!(dic = hTabItemWithKey (iCodehTab,
2533 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2536 if (dic->op != '=' || POINTER_SET (dic))
2539 /* make sure the right side does not have any definitions
2541 dbv = OP_DEFS(IC_RIGHT(dic));
2542 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2543 if (bitVectBitValue(dbv,lic->key)) return ;
2545 /* make sure they have the same type */
2547 sym_link *itype=operandType(IC_LEFT(ic));
2548 sym_link *ditype=operandType(IC_RIGHT(dic));
2550 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2551 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2554 /* extend the live range of replaced operand if needed */
2555 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2556 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2558 for (lic = ic; lic && lic != dic; lic = lic->prev)
2560 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2561 if (IS_ITEMP (IC_RIGHT (dic)))
2562 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2564 /* we now we know that it has one & only one def & use
2565 and the that the definition is an assignment */
2566 IC_LEFT (ic) = IC_RIGHT (dic);
2568 remiCodeFromeBBlock (ebp, dic);
2569 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2570 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2573 /*-----------------------------------------------------------------*/
2574 /* packRegisters - does some transformations to reduce register */
2576 /*-----------------------------------------------------------------*/
2578 packRegisters (eBBlock * ebp)
2588 /* look for assignments of the form */
2589 /* iTempNN = TRueSym (someoperation) SomeOperand */
2591 /* TrueSym := iTempNN:1 */
2592 for (ic = ebp->sch; ic; ic = ic->next)
2596 /* find assignment of the form TrueSym := iTempNN:1 */
2597 if (ic->op == '=' && !POINTER_SET (ic))
2598 change += packRegsForAssign (ic, ebp);
2605 for (ic = ebp->sch; ic; ic = ic->next)
2608 /* if this is an itemp & result of a address of a true sym
2609 then mark this as rematerialisable */
2610 if (ic->op == ADDRESS_OF &&
2611 IS_ITEMP (IC_RESULT (ic)) &&
2612 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2613 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2614 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2617 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2618 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2619 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2623 /* if this is an itemp & used as a pointer
2624 & assigned to a literal then remat */
2625 if (IS_ASSIGN_ICODE(ic) &&
2626 IS_ITEMP(IC_RESULT(ic)) &&
2627 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2628 isOperandLiteral(IC_RIGHT(ic)))
2630 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2631 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2632 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2635 /* if straight assignment then carry remat flag if
2636 this is the only definition */
2637 if (ic->op == '=' &&
2638 !POINTER_SET (ic) &&
2639 IS_SYMOP (IC_RIGHT (ic)) &&
2640 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2641 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2642 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2645 OP_SYMBOL (IC_RESULT (ic))->remat =
2646 OP_SYMBOL (IC_RIGHT (ic))->remat;
2647 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2648 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2651 /* if cast to a generic pointer & the pointer being
2652 cast is remat, then we can remat this cast as well */
2653 if (ic->op == CAST &&
2654 IS_SYMOP(IC_RIGHT(ic)) &&
2655 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2656 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2657 sym_link *to_type = operandType(IC_LEFT(ic));
2658 sym_link *from_type = operandType(IC_RIGHT(ic));
2659 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2660 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2661 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2662 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2666 /* if this is a +/- operation with a rematerizable
2667 then mark this as rematerializable as well */
2668 if ((ic->op == '+' || ic->op == '-') &&
2669 (IS_SYMOP (IC_LEFT (ic)) &&
2670 IS_ITEMP (IC_RESULT (ic)) &&
2671 OP_SYMBOL (IC_LEFT (ic))->remat &&
2672 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2673 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2674 IS_OP_LITERAL (IC_RIGHT (ic))))
2677 //int i = operandLitValue(IC_RIGHT(ic));
2678 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2679 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2680 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2683 /* mark the pointer usages */
2684 if (POINTER_SET (ic))
2685 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2687 if (POINTER_GET (ic))
2688 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2690 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2691 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2693 if (ic->op == RECEIVE && ic->argreg == 1 &&
2694 getSize (operandType(IC_RESULT(ic))) <= 3)
2695 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2699 /* if we are using a symbol on the stack
2700 then we should say ds390_ptrRegReq */
2701 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2702 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2703 OP_SYMBOL (IC_COND (ic))->iaccess);
2704 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2705 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2706 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2709 if (IS_SYMOP (IC_LEFT (ic)))
2710 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2711 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2712 if (IS_SYMOP (IC_RIGHT (ic)))
2713 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2714 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2715 if (IS_SYMOP (IC_RESULT (ic)))
2716 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2717 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2721 /* if the condition of an if instruction
2722 is defined in the previous instruction and
2723 this is the only usage then
2724 mark the itemp as a conditional */
2725 if ((IS_CONDITIONAL (ic) ||
2726 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2727 ic->next && ic->next->op == IFX &&
2728 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2729 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2730 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2732 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2736 /* reduce for support function calls */
2737 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2738 packRegsForSupport (ic, ebp);
2740 /* some cases the redundant moves can
2741 can be eliminated for return statements . Can be elminated for the first SEND */
2742 if ((ic->op == RETURN ||
2743 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
2744 !isOperandInFarSpace (IC_LEFT (ic)) &&
2747 packRegsDPTRuse (IC_LEFT (ic));
2750 if (ic->op == CALL) {
2751 sym_link *ftype = operandType(IC_LEFT(ic));
2752 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2753 !IFFUNC_ISBUILTIN(ftype)) {
2754 packRegsDPTRuse (IC_RESULT (ic));
2758 /* if pointer set & left has a size more than
2759 one and right is not in far space */
2760 if (POINTER_SET (ic) &&
2761 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2762 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2763 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2764 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2766 packRegsDPTRuse (IC_RESULT (ic));
2769 /* if pointer get */
2770 if (POINTER_GET (ic) &&
2771 !isOperandInFarSpace (IC_RESULT (ic)) &&
2772 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2773 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2774 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2776 packRegsDPTRuse (IC_LEFT (ic));
2779 /* if this is cast for intergral promotion then
2780 check if only use of the definition of the
2781 operand being casted/ if yes then replace
2782 the result of that arithmetic operation with
2783 this result and get rid of the cast */
2786 sym_link *fromType = operandType (IC_RIGHT (ic));
2787 sym_link *toType = operandType (IC_LEFT (ic));
2789 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2790 getSize (fromType) != getSize (toType) &&
2791 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2794 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2797 if (IS_ARITHMETIC_OP (dic))
2799 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2800 IC_RESULT (dic) = IC_RESULT (ic);
2801 remiCodeFromeBBlock (ebp, ic);
2802 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2803 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2804 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2808 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2814 /* if the type from and type to are the same
2815 then if this is the only use then packit */
2816 if (compareType (operandType (IC_RIGHT (ic)),
2817 operandType (IC_LEFT (ic))) == 1)
2819 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2822 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2823 IC_RESULT (dic) = IC_RESULT (ic);
2824 remiCodeFromeBBlock (ebp, ic);
2825 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2826 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2827 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2835 iTempNN := (some variable in farspace) V1
2840 if (ic->op == IPUSH || ic->op == SEND)
2842 packForPush (ic, ebp);
2846 /* pack registers for accumulator use, when the
2847 result of an arithmetic or bit wise operation
2848 has only one use, that use is immediately following
2849 the defintion and the using iCode has only one
2850 operand or has two operands but one is literal &
2851 the result of that operation is not on stack then
2852 we can leave the result of this operation in acc:b
2854 if ((IS_ARITHMETIC_OP (ic)
2855 || IS_CONDITIONAL(ic)
2856 || IS_BITWISE_OP (ic)
2857 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2858 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2860 IS_ITEMP (IC_RESULT (ic)) &&
2861 getSize (operandType (IC_RESULT (ic))) <= 2)
2863 packRegsForAccUse (ic);
2868 /*-----------------------------------------------------------------*/
2869 /* assignRegisters - assigns registers to each live range as need */
2870 /*-----------------------------------------------------------------*/
2872 ds390_assignRegisters (eBBlock ** ebbs, int count)
2877 setToNull ((void *) &_G.funcrUsed);
2878 setToNull ((void *) &_G.regAssigned);
2879 setToNull ((void *) &_G.totRegAssigned);
2880 setToNull ((void *) &_G.funcrUsed);
2881 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2883 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2884 /* change assignments this will remove some
2885 live ranges reducing some register pressure */
2886 for (i = 0; i < count; i++)
2887 packRegisters (ebbs[i]);
2889 if (options.dump_pack)
2890 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2892 /* first determine for each live range the number of
2893 registers & the type of registers required for each */
2896 /* and serially allocate registers */
2897 serialRegAssign (ebbs, count);
2904 /* if stack was extended then tell the user */
2907 /* werror(W_TOOMANY_SPILS,"stack", */
2908 /* _G.stackExtend,currFunc->name,""); */
2914 /* werror(W_TOOMANY_SPILS,"data space", */
2915 /* _G.dataExtend,currFunc->name,""); */
2919 /* after that create the register mask
2920 for each of the instruction */
2921 createRegMask (ebbs, count);
2923 /* redo that offsets for stacked automatic variables */
2924 redoStackOffsets ();
2926 if (options.dump_rassgn) {
2927 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2928 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2931 /* do the overlaysegment stuff SDCCmem.c */
2932 doOverlays (ebbs, count);
2934 /* now get back the chain */
2935 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2940 /* free up any _G.stackSpil locations allocated */
2941 applyToSet (_G.stackSpil, deallocStackSpil);
2943 setToNull ((void **) &_G.stackSpil);
2944 setToNull ((void **) &_G.spiltSet);
2945 /* mark all registers as free */