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);
194 /*-----------------------------------------------------------------*/
195 /* isOperandInReg - returns true if operand is currently in regs */
196 /*-----------------------------------------------------------------*/
197 static int isOperandInReg(operand *op)
199 if (!IS_SYMOP(op)) return 0;
200 if (OP_SYMBOL(op)->ruonly) return 1;
201 if (OP_SYMBOL(op)->accuse) return 1;
202 if (OP_SYMBOL(op)->dptr) return 1;
203 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
206 /*-----------------------------------------------------------------*/
207 /* computeSpillable - given a point find the spillable live ranges */
208 /*-----------------------------------------------------------------*/
210 computeSpillable (iCode * ic)
214 /* spillable live ranges are those that are live at this
215 point . the following categories need to be subtracted
217 a) - those that are already spilt
218 b) - if being used by this one
219 c) - defined by this one */
221 spillable = bitVectCopy (ic->rlive);
223 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
225 bitVectCplAnd (spillable, ic->uses); /* used in this one */
226 bitVectUnSetBit (spillable, ic->defKey);
227 spillable = bitVectIntersect (spillable, _G.regAssigned);
232 /*-----------------------------------------------------------------*/
233 /* noSpilLoc - return true if a variable has no spil location */
234 /*-----------------------------------------------------------------*/
236 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
238 return (sym->usl.spillLoc ? 0 : 1);
241 /*-----------------------------------------------------------------*/
242 /* hasSpilLoc - will return 1 if the symbol has spil location */
243 /*-----------------------------------------------------------------*/
245 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
247 return (sym->usl.spillLoc ? 1 : 0);
250 /*-----------------------------------------------------------------*/
251 /* directSpilLoc - will return 1 if the splilocation is in direct */
252 /*-----------------------------------------------------------------*/
254 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
256 if (sym->usl.spillLoc &&
257 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
263 /*-----------------------------------------------------------------*/
264 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
265 /* but is not used as a pointer */
266 /*-----------------------------------------------------------------*/
268 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
270 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
273 /*-----------------------------------------------------------------*/
274 /* rematable - will return 1 if the remat flag is set */
275 /*-----------------------------------------------------------------*/
277 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
282 /*-----------------------------------------------------------------*/
283 /* notUsedInRemaining - not used or defined in remain of the block */
284 /*-----------------------------------------------------------------*/
286 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
288 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
289 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
292 /*-----------------------------------------------------------------*/
293 /* allLRs - return true for all */
294 /*-----------------------------------------------------------------*/
296 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
301 /*-----------------------------------------------------------------*/
302 /* liveRangesWith - applies function to a given set of live range */
303 /*-----------------------------------------------------------------*/
305 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
306 eBBlock * ebp, iCode * ic)
311 if (!lrs || !lrs->size)
314 for (i = 1; i < lrs->size; i++)
317 if (!bitVectBitValue (lrs, i))
320 /* if we don't find it in the live range
321 hash table we are in serious trouble */
322 if (!(sym = hTabItemWithKey (liveRanges, i)))
324 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
325 "liveRangesWith could not find liveRange");
329 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
330 addSetHead (&rset, sym);
337 /*-----------------------------------------------------------------*/
338 /* leastUsedLR - given a set determines which is the least used */
339 /*-----------------------------------------------------------------*/
341 leastUsedLR (set * sset)
343 symbol *sym = NULL, *lsym = NULL;
345 sym = lsym = setFirstItem (sset);
350 for (; lsym; lsym = setNextItem (sset))
353 /* if usage is the same then prefer
354 the spill the smaller of the two */
355 if (lsym->used == sym->used)
356 if (getSize (lsym->type) < getSize (sym->type))
360 if (lsym->used < sym->used)
365 setToNull ((void **) &sset);
370 /*-----------------------------------------------------------------*/
371 /* noOverLap - will iterate through the list looking for over lap */
372 /*-----------------------------------------------------------------*/
374 noOverLap (set * itmpStack, symbol * fsym)
378 for (sym = setFirstItem (itmpStack); sym;
379 sym = setNextItem (itmpStack))
381 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
386 /*-----------------------------------------------------------------*/
387 /* isFree - will return 1 if the a free spil location is found */
388 /*-----------------------------------------------------------------*/
393 V_ARG (symbol **, sloc);
394 V_ARG (symbol *, fsym);
396 /* if already found */
400 /* if it is free && and the itmp assigned to
401 this does not have any overlapping live ranges
402 with the one currently being assigned and
403 the size can be accomodated */
405 noOverLap (sym->usl.itmpStack, fsym) &&
406 getSize (sym->type) >= getSize (fsym->type))
415 /*-----------------------------------------------------------------*/
416 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
417 /*-----------------------------------------------------------------*/
419 spillLRWithPtrReg (symbol * forSym)
425 if (!_G.regAssigned ||
426 bitVectIsZero (_G.regAssigned))
429 r0 = ds390_regWithIdx (R0_IDX);
430 r1 = ds390_regWithIdx (R1_IDX);
432 /* for all live ranges */
433 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
434 lrsym = hTabNextItem (liveRanges, &k))
438 /* if no registers assigned to it or
440 /* if it does not overlap with this then
441 not need to spill it */
443 if (lrsym->isspilt || !lrsym->nRegs ||
444 (lrsym->liveTo < forSym->liveFrom))
447 /* go thru the registers : if it is either
448 r0 or r1 then spil it */
449 for (j = 0; j < lrsym->nRegs; j++)
450 if (lrsym->regs[j] == r0 ||
451 lrsym->regs[j] == r1)
460 /*-----------------------------------------------------------------*/
461 /* createStackSpil - create a location on the stack to spil */
462 /*-----------------------------------------------------------------*/
464 createStackSpil (symbol * sym)
467 int useXstack, model, noOverlay;
471 /* first go try and find a free one that is already
472 existing on the stack */
473 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
475 /* found a free one : just update & return */
476 sym->usl.spillLoc = sloc;
479 addSetHead (&sloc->usl.itmpStack, sym);
483 /* could not then have to create one , this is the hard part
484 we need to allocate this on the stack : this is really a
485 hack!! but cannot think of anything better at this time */
487 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
489 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
494 sloc = newiTemp (slocBuffer);
496 /* set the type to the spilling symbol */
497 sloc->type = copyLinkChain (sym->type);
498 sloc->etype = getSpec (sloc->type);
499 if (options.model == MODEL_SMALL) {
500 SPEC_SCLS (sloc->etype) = S_DATA;
502 SPEC_SCLS (sloc->etype) = S_XDATA;
504 SPEC_EXTR (sloc->etype) = 0;
505 SPEC_STAT (sloc->etype) = 0;
506 SPEC_VOLATILE(sloc->etype) = 0;
507 SPEC_ABSA(sloc->etype) = 0;
509 /* we don't allow it to be allocated`
510 onto the external stack since : so we
511 temporarily turn it off ; we also
512 turn off memory model to prevent
513 the spil from going to the external storage
514 and turn off overlaying
517 useXstack = options.useXstack;
518 model = options.model;
519 noOverlay = options.noOverlay;
520 options.noOverlay = 1;
522 /* options.model = options.useXstack = 0; */
526 options.useXstack = useXstack;
527 options.model = model;
528 options.noOverlay = noOverlay;
529 sloc->isref = 1; /* to prevent compiler warning */
531 /* if it is on the stack then update the stack */
532 if (IN_STACK (sloc->etype))
534 currFunc->stack += getSize (sloc->type);
535 _G.stackExtend += getSize (sloc->type);
538 _G.dataExtend += getSize (sloc->type);
540 /* add it to the _G.stackSpil set */
541 addSetHead (&_G.stackSpil, sloc);
542 sym->usl.spillLoc = sloc;
545 /* add it to the set of itempStack set
546 of the spill location */
547 addSetHead (&sloc->usl.itmpStack, sym);
551 /*-----------------------------------------------------------------*/
552 /* isSpiltOnStack - returns true if the spil location is on stack */
553 /*-----------------------------------------------------------------*/
555 isSpiltOnStack (symbol * sym)
565 /* if (sym->_G.stackSpil) */
568 if (!sym->usl.spillLoc)
571 etype = getSpec (sym->usl.spillLoc->type);
572 if (IN_STACK (etype))
578 /*-----------------------------------------------------------------*/
579 /* spillThis - spils a specific operand */
580 /*-----------------------------------------------------------------*/
582 spillThis (symbol * sym)
585 /* if this is rematerializable or has a spillLocation
586 we are okay, else we need to create a spillLocation
588 if (!(sym->remat || sym->usl.spillLoc))
589 createStackSpil (sym);
592 /* mark it has spilt & put it in the spilt set */
593 sym->isspilt = sym->spillA = 1;
594 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
596 bitVectUnSetBit (_G.regAssigned, sym->key);
597 bitVectUnSetBit (_G.totRegAssigned, sym->key);
599 for (i = 0; i < sym->nRegs; i++)
603 freeReg (sym->regs[i]);
607 /* if spilt on stack then free up r0 & r1
608 if they could have been assigned to some
610 if (!ds390_ptrRegReq && isSpiltOnStack (sym))
612 ds390_ptrRegReq += !options.stack10bit;
613 spillLRWithPtrReg (sym);
616 if (sym->usl.spillLoc && !sym->remat)
617 sym->usl.spillLoc->allocreq++;
621 /*-----------------------------------------------------------------*/
622 /* selectSpil - select a iTemp to spil : rather a simple procedure */
623 /*-----------------------------------------------------------------*/
625 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
627 bitVect *lrcs = NULL;
631 /* get the spillable live ranges */
632 lrcs = computeSpillable (ic);
634 /* get all live ranges that are rematerizable */
635 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
638 /* return the least used of these */
639 return leastUsedLR (selectS);
642 /* get live ranges with spillLocations in direct space */
643 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
645 sym = leastUsedLR (selectS);
646 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
647 sym->usl.spillLoc->rname :
648 sym->usl.spillLoc->name));
650 /* mark it as allocation required */
651 sym->usl.spillLoc->allocreq++;
655 /* if the symbol is local to the block then */
656 if (forSym->liveTo < ebp->lSeq)
659 /* check if there are any live ranges allocated
660 to registers that are not used in this block */
661 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
663 sym = leastUsedLR (selectS);
664 /* if this is not rematerializable */
673 /* check if there are any live ranges that not
674 used in the remainder of the block */
675 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
677 sym = leastUsedLR (selectS);
690 /* find live ranges with spillocation && not used as pointers */
691 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
694 sym = leastUsedLR (selectS);
695 /* mark this as allocation required */
696 sym->usl.spillLoc->allocreq++;
700 /* find live ranges with spillocation */
701 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
704 sym = leastUsedLR (selectS);
705 sym->usl.spillLoc->allocreq++;
709 /* couldn't find then we need to create a spil
710 location on the stack , for which one? the least
712 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
715 /* return a created spil location */
716 sym = createStackSpil (leastUsedLR (selectS));
717 sym->usl.spillLoc->allocreq++;
721 /* this is an extreme situation we will spill
722 this one : happens very rarely but it does happen */
728 /*-----------------------------------------------------------------*/
729 /* spilSomething - spil some variable & mark registers as free */
730 /*-----------------------------------------------------------------*/
732 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
737 /* get something we can spil */
738 ssym = selectSpil (ic, ebp, forSym);
740 /* mark it as spilt */
741 ssym->isspilt = ssym->spillA = 1;
742 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
744 /* mark it as not register assigned &
745 take it away from the set */
746 bitVectUnSetBit (_G.regAssigned, ssym->key);
747 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
749 /* mark the registers as free */
750 for (i = 0; i < ssym->nRegs; i++)
752 freeReg (ssym->regs[i]);
754 /* if spilt on stack then free up r0 & r1
755 if they could have been assigned to as gprs */
756 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
759 spillLRWithPtrReg (ssym);
762 /* if this was a block level spil then insert push & pop
763 at the start & end of block respectively */
766 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
767 /* add push to the start of the block */
768 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
769 ebp->sch->next : ebp->sch));
770 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
771 /* add pop to the end of the block */
772 addiCodeToeBBlock (ebp, nic, NULL);
775 /* if spilt because not used in the remainder of the
776 block then add a push before this instruction and
777 a pop at the end of the block */
778 if (ssym->remainSpil)
781 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
782 /* add push just before this instruction */
783 addiCodeToeBBlock (ebp, nic, ic);
785 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
786 /* add pop to the end of the block */
787 addiCodeToeBBlock (ebp, nic, NULL);
796 /*-----------------------------------------------------------------*/
797 /* getRegPtr - will try for PTR if not a GPR type if not spil */
798 /*-----------------------------------------------------------------*/
800 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
805 /* try for a ptr type */
806 if ((reg = allocReg (REG_PTR)))
809 /* try for gpr type */
810 if ((reg = allocReg (REG_GPR)))
813 /* we have to spil */
814 if (!spilSomething (ic, ebp, sym))
817 /* this looks like an infinite loop but
818 in really selectSpil will abort */
822 /*-----------------------------------------------------------------*/
823 /* getRegGpr - will try for GPR if not spil */
824 /*-----------------------------------------------------------------*/
826 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
831 /* try for gpr type */
832 if ((reg = allocReg (REG_GPR)))
835 if (!ds390_ptrRegReq)
836 if ((reg = allocReg (REG_PTR)))
839 /* we have to spil */
840 if (!spilSomething (ic, ebp, sym))
843 /* this looks like an infinite loop but
844 in really selectSpil will abort */
848 /*-----------------------------------------------------------------*/
849 /* getRegPtrNoSpil - get it cannot split */
850 /*-----------------------------------------------------------------*/
851 static regs *getRegPtrNoSpil()
855 /* try for a ptr type */
856 if ((reg = allocReg (REG_PTR)))
859 /* try for gpr type */
860 if ((reg = allocReg (REG_GPR)))
865 /* just to make the compiler happy */
869 /*-----------------------------------------------------------------*/
870 /* getRegGprNoSpil - get it cannot split */
871 /*-----------------------------------------------------------------*/
872 static regs *getRegGprNoSpil()
876 if ((reg = allocReg (REG_GPR)))
879 if (!ds390_ptrRegReq)
880 if ((reg = allocReg (REG_PTR)))
885 /* just to make the compiler happy */
889 /*-----------------------------------------------------------------*/
890 /* symHasReg - symbol has a given register */
891 /*-----------------------------------------------------------------*/
893 symHasReg (symbol * sym, regs * reg)
897 for (i = 0; i < sym->nRegs; i++)
898 if (sym->regs[i] == reg)
904 /*-----------------------------------------------------------------*/
905 /* deassignLRs - check the live to and if they have registers & are */
906 /* not spilt then free up the registers */
907 /*-----------------------------------------------------------------*/
909 deassignLRs (iCode * ic, eBBlock * ebp)
915 for (sym = hTabFirstItem (liveRanges, &k); sym;
916 sym = hTabNextItem (liveRanges, &k))
920 /* if it does not end here */
921 if (sym->liveTo > ic->seq)
924 /* if it was spilt on stack then we can
925 mark the stack spil location as free */
930 sym->usl.spillLoc->isFree = 1;
936 if (!bitVectBitValue (_G.regAssigned, sym->key))
939 /* special case check if this is an IFX &
940 the privious one was a pop and the
941 previous one was not spilt then keep track
943 if (ic->op == IFX && ic->prev &&
944 ic->prev->op == IPOP &&
945 !ic->prev->parmPush &&
946 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
947 psym = OP_SYMBOL (IC_LEFT (ic->prev));
953 bitVectUnSetBit (_G.regAssigned, sym->key);
955 /* if the result of this one needs registers
956 and does not have it then assign it right
958 if (IC_RESULT (ic) &&
959 !(SKIP_IC2 (ic) || /* not a special icode */
960 ic->op == JUMPTABLE ||
966 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
967 result->liveTo > ic->seq && /* and will live beyond this */
968 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
969 result->regType == sym->regType && /* same register types */
970 result->nRegs && /* which needs registers */
971 !result->isspilt && /* and does not already have them */
973 !bitVectBitValue (_G.regAssigned, result->key) &&
974 /* the number of free regs + number of regs in this LR
975 can accomodate the what result Needs */
976 ((nfreeRegsType (result->regType) +
977 sym->nRegs) >= result->nRegs)
981 for (i = 0; i < result->nRegs; i++)
983 result->regs[i] = sym->regs[i];
985 result->regs[i] = getRegGpr (ic, ebp, result);
987 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
988 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
992 /* free the remaining */
993 for (; i < sym->nRegs; i++)
997 if (!symHasReg (psym, sym->regs[i]))
998 freeReg (sym->regs[i]);
1001 freeReg (sym->regs[i]);
1008 /*-----------------------------------------------------------------*/
1009 /* reassignLR - reassign this to registers */
1010 /*-----------------------------------------------------------------*/
1012 reassignLR (operand * op)
1014 symbol *sym = OP_SYMBOL (op);
1017 /* not spilt any more */
1018 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1019 bitVectUnSetBit (_G.spiltSet, sym->key);
1021 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1022 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1026 for (i = 0; i < sym->nRegs; i++)
1027 sym->regs[i]->isFree = 0;
1030 /*-----------------------------------------------------------------*/
1031 /* willCauseSpill - determines if allocating will cause a spill */
1032 /*-----------------------------------------------------------------*/
1034 willCauseSpill (int nr, int rt)
1036 /* first check if there are any avlb registers
1037 of te type required */
1040 /* special case for pointer type
1041 if pointer type not avlb then
1042 check for type gpr */
1043 if (nFreeRegs (rt) >= nr)
1045 if (nFreeRegs (REG_GPR) >= nr)
1050 if (ds390_ptrRegReq)
1052 if (nFreeRegs (rt) >= nr)
1057 if (nFreeRegs (REG_PTR) +
1058 nFreeRegs (REG_GPR) >= nr)
1063 /* it will cause a spil */
1067 /*-----------------------------------------------------------------*/
1068 /* positionRegs - the allocator can allocate same registers to res- */
1069 /* ult and operand, if this happens make sure they are in the same */
1070 /* position as the operand otherwise chaos results */
1071 /*-----------------------------------------------------------------*/
1073 positionRegs (symbol * result, symbol * opsym)
1075 int count = min (result->nRegs, opsym->nRegs);
1076 int i, j = 0, shared = 0;
1079 /* if the result has been spilt then cannot share */
1084 /* first make sure that they actually share */
1085 for (i = 0; i < count; i++)
1087 for (j = 0; j < count; j++)
1089 if (result->regs[i] == opsym->regs[j] && i != j)
1099 regs *tmp = result->regs[i];
1100 result->regs[i] = result->regs[j];
1101 result->regs[j] = tmp;
1108 /*-----------------------------------------------------------------*/
1109 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1110 /*-----------------------------------------------------------------*/
1111 bitVect *unusedLRs (eBBlock *ebp)
1113 bitVect *ret = NULL;
1117 if (!ebp) return NULL;
1118 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1119 sym = hTabNextItem(liveRanges,&key)) {
1121 if (notUsedInBlock(sym,ebp,NULL)) {
1122 ret = bitVectSetBit(ret,sym->key);
1129 /*-----------------------------------------------------------------*/
1130 /* deassignUnsedLRs - if this baisc block ends in a return then */
1131 /* deassign symbols not used in this block */
1132 /*-----------------------------------------------------------------*/
1133 bitVect *deassignUnsedLRs(eBBlock *ebp)
1135 bitVect *unused = NULL;
1138 switch (returnAtEnd(ebp)) {
1139 case 2: /* successor block ends in a return */
1140 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1142 case 1: /* this block ends in a return */
1143 unused = bitVectIntersect(unused,unusedLRs(ebp));
1148 for (i = 0 ; i < unused->size ; i++ ) {
1151 if (bitVectBitValue(unused,i)) {
1153 /* if assigned to registers */
1154 if (bitVectBitValue(_G.regAssigned,i)) {
1158 sym = hTabItemWithKey(liveRanges,i);
1159 /* remove it from regassigned & mark the
1161 bitVectUnSetBit(_G.regAssigned,i);
1162 for (j = 0 ; j < sym->nRegs; j++)
1163 freeReg(sym->regs[j]);
1165 /* not assigned to registers : remove from set*/
1166 bitVectUnSetBit(unused,i);
1174 /*-----------------------------------------------------------------*/
1175 /* reassignUnusedLRs - put registers to unused Live ranges */
1176 /*-----------------------------------------------------------------*/
1177 void reassignUnusedLRs (bitVect *unused)
1180 if (!unused) return ;
1182 for (i = 0 ; i < unused->size ; i++ ) {
1183 /* if unused : means it was assigned to registers before */
1184 if (bitVectBitValue(unused,i)) {
1188 /* put it back into reg set*/
1189 bitVectSetBit(_G.regAssigned,i) ;
1191 sym = hTabItemWithKey(liveRanges,i);
1192 /* makr registers busy */
1193 for (j = 0 ; j < sym->nRegs; j++)
1194 sym->regs[j]->isFree = 0;
1199 /*-----------------------------------------------------------------*/
1200 /* serialRegAssign - serially allocate registers to the variables */
1201 /*-----------------------------------------------------------------*/
1203 serialRegAssign (eBBlock ** ebbs, int count)
1207 /* for all blocks */
1208 for (i = 0; i < count; i++)
1212 bitVect *unusedLRs = NULL;
1214 if (ebbs[i]->noPath &&
1215 (ebbs[i]->entryLabel != entryLabel &&
1216 ebbs[i]->entryLabel != returnLabel))
1219 unusedLRs = deassignUnsedLRs(ebbs[i]);
1221 /* of all instructions do */
1222 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1225 /* if this is an ipop that means some live
1226 range will have to be assigned again */
1228 reassignLR (IC_LEFT (ic));
1230 /* if result is present && is a true symbol */
1231 if (IC_RESULT (ic) && ic->op != IFX &&
1232 IS_TRUE_SYMOP (IC_RESULT (ic)))
1233 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1235 /* take away registers from live
1236 ranges that end at this instruction */
1237 deassignLRs (ic, ebbs[i]);
1239 /* some don't need registers */
1240 if (SKIP_IC2 (ic) ||
1241 ic->op == JUMPTABLE ||
1245 (IC_RESULT (ic) && POINTER_SET (ic)))
1248 /* now we need to allocate registers
1249 only for the result */
1252 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1258 /* if it does not need or is spilt
1259 or is already assigned to registers
1260 or will not live beyond this instructions */
1263 bitVectBitValue (_G.regAssigned, sym->key) ||
1264 sym->liveTo <= ic->seq)
1267 /* if some liverange has been spilt at the block level
1268 and this one live beyond this block then spil this
1270 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1275 /* if trying to allocate this will cause
1276 a spill and there is nothing to spill
1277 or this one is rematerializable then
1279 willCS = willCauseSpill (sym->nRegs, sym->regType);
1280 spillable = computeSpillable (ic);
1282 (willCS && bitVectIsZero (spillable)))
1290 /* if it has a spillocation & is used less than
1291 all other live ranges then spill this */
1293 if (sym->usl.spillLoc) {
1294 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1295 allLRs, ebbs[i], ic));
1296 if (leastUsed && leastUsed->used > sym->used) {
1301 /* if none of the liveRanges have a spillLocation then better
1302 to spill this one than anything else already assigned to registers */
1303 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1304 /* if this is local to this block then we might find a block spil */
1305 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1313 /* if we need ptr regs for the right side
1315 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1316 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1317 <= (unsigned) PTRSIZE)
1322 /* else we assign registers to it */
1323 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1324 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1326 for (j = 0; j < sym->nRegs; j++)
1328 if (sym->regType == REG_PTR)
1329 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1331 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1333 /* if the allocation falied which means
1334 this was spilt then break */
1338 /* if it shares registers with operands make sure
1339 that they are in the same position */
1340 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1341 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1342 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1343 OP_SYMBOL (IC_LEFT (ic)));
1344 /* do the same for the right operand */
1345 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1346 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1347 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1348 OP_SYMBOL (IC_RIGHT (ic)));
1358 reassignUnusedLRs(unusedLRs);
1362 /*-----------------------------------------------------------------*/
1363 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1364 /*-----------------------------------------------------------------*/
1365 static void fillGaps()
1369 int loop = 0, change;
1371 if (getenv("DISABLE_FILL_GAPS")) return;
1373 /* First try to do DPTRuse once more since now we know what got into
1376 while (loop++ < 10) {
1379 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1380 sym = hTabNextItem(liveRanges,&key)) {
1381 int size = getSize(sym->type);
1383 if (sym->liveFrom == sym->liveTo) continue;
1385 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1386 size < 4 && size > 1) {
1388 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1390 /* if this was ssigned to registers then */
1391 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1392 /* take it out of the register assigned set */
1393 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1394 } else if (sym->usl.spillLoc) {
1395 sym->usl.spillLoc->allocreq--;
1396 sym->usl.spillLoc = NULL;
1400 sym->isspilt = sym->spillA = 0;
1404 /* try assigning other dptrs */
1405 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1406 /* if this was ssigned to registers then */
1407 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1408 /* take it out of the register assigned set */
1409 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1410 } else if (sym->usl.spillLoc) {
1411 sym->usl.spillLoc->allocreq--;
1412 sym->usl.spillLoc = NULL;
1415 sym->isspilt = sym->spillA = 0;
1420 /* look for livernages that was spilt by the allocator */
1421 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1422 sym = hTabNextItem(liveRanges,&key)) {
1427 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1428 if (!sym->uses || !sym->defs) continue ;
1429 /* find the liveRanges this one clashes with, that are
1430 still assigned to registers & mark the registers as used*/
1431 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1435 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1436 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1439 clr = hTabItemWithKey(liveRanges,i);
1442 /* mark these registers as used */
1443 for (k = 0 ; k < clr->nRegs ; k++ )
1444 useReg(clr->regs[k]);
1447 if (willCauseSpill(sym->nRegs,sym->regType)) {
1448 /* NOPE :( clear all registers & and continue */
1453 /* THERE IS HOPE !!!! */
1454 for (i=0; i < sym->nRegs ; i++ ) {
1455 if (sym->regType == REG_PTR)
1456 sym->regs[i] = getRegPtrNoSpil ();
1458 sym->regs[i] = getRegGprNoSpil ();
1461 /* for all its definitions & uses check if the registers
1462 allocated needs positioning NOTE: we can position
1463 only ONCE if more than One positioning required
1466 for (i = 0 ; i < sym->defs->size ; i++ ) {
1467 if (bitVectBitValue(sym->defs,i)) {
1469 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1470 if (SKIP_IC(ic)) continue;
1471 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1472 /* if left is assigned to registers */
1473 if (IS_SYMOP(IC_LEFT(ic)) &&
1474 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1475 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1477 if (IS_SYMOP(IC_RIGHT(ic)) &&
1478 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1479 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1481 if (pdone > 1) break;
1484 for (i = 0 ; i < sym->uses->size ; i++ ) {
1485 if (bitVectBitValue(sym->uses,i)) {
1487 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1488 if (SKIP_IC(ic)) continue;
1489 if (!IS_ASSIGN_ICODE(ic)) continue ;
1491 /* if result is assigned to registers */
1492 if (IS_SYMOP(IC_RESULT(ic)) &&
1493 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1494 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1496 if (pdone > 1) break;
1499 /* had to position more than once GIVE UP */
1501 /* UNDO all the changes we made to try this */
1503 for (i=0; i < sym->nRegs ; i++ ) {
1504 sym->regs[i] = NULL;
1507 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1508 "%s in function %s\n",
1509 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1512 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1513 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1514 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1515 sym->isspilt = sym->spillA = 0 ;
1516 sym->usl.spillLoc->allocreq--;
1517 sym->usl.spillLoc = NULL;
1525 /*-----------------------------------------------------------------*/
1526 /* rUmaskForOp :- returns register mask for an operand */
1527 /*-----------------------------------------------------------------*/
1529 ds390_rUmaskForOp (operand * op)
1535 /* only temporaries are assigned registers */
1539 sym = OP_SYMBOL (op);
1541 /* if spilt or no registers assigned to it
1543 if (sym->isspilt || !sym->nRegs)
1546 rumask = newBitVect (ds390_nRegs);
1548 for (j = 0; j < sym->nRegs; j++)
1550 rumask = bitVectSetBit (rumask,
1551 sym->regs[j]->rIdx);
1557 /*-----------------------------------------------------------------*/
1558 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1559 /*-----------------------------------------------------------------*/
1561 regsUsedIniCode (iCode * ic)
1563 bitVect *rmask = newBitVect (ds390_nRegs);
1565 /* do the special cases first */
1568 rmask = bitVectUnion (rmask,
1569 ds390_rUmaskForOp (IC_COND (ic)));
1573 /* for the jumptable */
1574 if (ic->op == JUMPTABLE)
1576 rmask = bitVectUnion (rmask,
1577 ds390_rUmaskForOp (IC_JTCOND (ic)));
1582 /* of all other cases */
1584 rmask = bitVectUnion (rmask,
1585 ds390_rUmaskForOp (IC_LEFT (ic)));
1589 rmask = bitVectUnion (rmask,
1590 ds390_rUmaskForOp (IC_RIGHT (ic)));
1593 rmask = bitVectUnion (rmask,
1594 ds390_rUmaskForOp (IC_RESULT (ic)));
1600 /*-----------------------------------------------------------------*/
1601 /* createRegMask - for each instruction will determine the regsUsed */
1602 /*-----------------------------------------------------------------*/
1604 createRegMask (eBBlock ** ebbs, int count)
1608 /* for all blocks */
1609 for (i = 0; i < count; i++)
1613 if (ebbs[i]->noPath &&
1614 (ebbs[i]->entryLabel != entryLabel &&
1615 ebbs[i]->entryLabel != returnLabel))
1618 /* for all instructions */
1619 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1624 if (SKIP_IC2 (ic) || !ic->rlive)
1627 /* first mark the registers used in this
1629 ic->rUsed = regsUsedIniCode (ic);
1630 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1632 /* now create the register mask for those
1633 registers that are in use : this is a
1634 super set of ic->rUsed */
1635 ic->rMask = newBitVect (ds390_nRegs + 1);
1637 /* for all live Ranges alive at this point */
1638 for (j = 1; j < ic->rlive->size; j++)
1643 /* if not alive then continue */
1644 if (!bitVectBitValue (ic->rlive, j))
1647 /* find the live range we are interested in */
1648 if (!(sym = hTabItemWithKey (liveRanges, j)))
1650 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1651 "createRegMask cannot find live range");
1655 /* special case for ruonly */
1656 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1657 int size = getSize(sym->type);
1659 for (k = 0 ; k < size; k++ )
1660 ic->rMask = bitVectSetBit (ic->rMask, j++);
1663 /* if no register assigned to it */
1664 if (!sym->nRegs || sym->isspilt)
1667 /* for all the registers allocated to it */
1668 for (k = 0; k < sym->nRegs; k++)
1671 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1677 /*-----------------------------------------------------------------*/
1678 /* rematStr - returns the rematerialized string for a remat var */
1679 /*-----------------------------------------------------------------*/
1681 rematStr (symbol * sym)
1684 iCode *ic = sym->rematiCode;
1689 /* if plus or minus print the right hand side */
1690 if (ic->op == '+' || ic->op == '-')
1692 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1695 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1698 /* cast then continue */
1699 if (IS_CAST_ICODE(ic)) {
1700 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1703 /* we reached the end */
1704 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1711 /*-----------------------------------------------------------------*/
1712 /* regTypeNum - computes the type & number of registers required */
1713 /*-----------------------------------------------------------------*/
1721 /* for each live range do */
1722 for (sym = hTabFirstItem (liveRanges, &k); sym;
1723 sym = hTabNextItem (liveRanges, &k))
1726 /* if used zero times then no registers needed */
1727 if ((sym->liveTo - sym->liveFrom) == 0)
1731 /* if the live range is a temporary */
1735 /* if the type is marked as a conditional */
1736 if (sym->regType == REG_CND)
1739 /* if used in return only then we don't
1741 if (sym->ruonly || sym->accuse)
1743 if (IS_AGGREGATE (sym->type) || sym->isptr)
1744 sym->type = aggrToPtr (sym->type, FALSE);
1748 /* if the symbol has only one definition &
1749 that definition is a get_pointer */
1750 if (bitVectnBitsOn (sym->defs) == 1 &&
1751 (ic = hTabItemWithKey (iCodehTab,
1752 bitVectFirstBit (sym->defs))) &&
1755 !IS_BITVAR (sym->etype))
1757 /* and that pointer is remat in data space */
1758 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1759 !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1760 DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1763 /* create a psuedo symbol & force a spil */
1764 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1765 psym->type = sym->type;
1766 psym->etype = sym->etype;
1767 strcpy (psym->rname, psym->name);
1769 sym->usl.spillLoc = psym;
1773 /* if in data space or idata space then try to
1774 allocate pointer register */
1778 /* if not then we require registers */
1779 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1780 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1781 getSize (sym->type));
1785 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1786 printTypeChain (sym->type, stderr);
1787 fprintf (stderr, "\n");
1790 /* determine the type of register required */
1791 if (sym->nRegs == 1 &&
1792 IS_PTR (sym->type) &&
1794 sym->regType = REG_PTR;
1796 sym->regType = REG_GPR;
1800 /* for the first run we don't provide */
1801 /* registers for true symbols we will */
1802 /* see how things go */
1808 /*-----------------------------------------------------------------*/
1809 /* freeAllRegs - mark all registers as free */
1810 /*-----------------------------------------------------------------*/
1816 for (i = 0; i < ds390_nRegs; i++)
1817 regs390[i].isFree = 1;
1820 /*-----------------------------------------------------------------*/
1821 /* deallocStackSpil - this will set the stack pointer back */
1822 /*-----------------------------------------------------------------*/
1824 DEFSETFUNC (deallocStackSpil)
1832 /*-----------------------------------------------------------------*/
1833 /* farSpacePackable - returns the packable icode for far variables */
1834 /*-----------------------------------------------------------------*/
1836 farSpacePackable (iCode * ic)
1840 /* go thru till we find a definition for the
1841 symbol on the right */
1842 for (dic = ic->prev; dic; dic = dic->prev)
1845 /* if the definition is a call then no */
1846 if ((dic->op == CALL || dic->op == PCALL) &&
1847 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1852 /* if shift by unknown amount then not */
1853 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1854 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1857 /* if pointer get and size > 1 */
1858 if (POINTER_GET (dic) &&
1859 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1862 if (POINTER_SET (dic) &&
1863 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1866 /* if any three is a true symbol in far space */
1867 if (IC_RESULT (dic) &&
1868 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1869 isOperandInFarSpace (IC_RESULT (dic)))
1872 if (IC_RIGHT (dic) &&
1873 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1874 isOperandInFarSpace (IC_RIGHT (dic)) &&
1875 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1878 if (IC_LEFT (dic) &&
1879 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1880 isOperandInFarSpace (IC_LEFT (dic)) &&
1881 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1884 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1886 if ((dic->op == LEFT_OP ||
1887 dic->op == RIGHT_OP ||
1889 IS_OP_LITERAL (IC_RIGHT (dic)))
1899 /*-----------------------------------------------------------------*/
1900 /* packRegsForAssign - register reduction for assignment */
1901 /*-----------------------------------------------------------------*/
1903 packRegsForAssign (iCode * ic, eBBlock * ebp)
1907 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1908 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1909 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1914 /* if the true symbol is defined in far space or on stack
1915 then we should not since this will increase register pressure */
1917 if (isOperandInFarSpace (IC_RESULT (ic)))
1919 if ((dic = farSpacePackable (ic)))
1925 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1930 /* find the definition of iTempNN scanning backwards if we find a
1931 a use of the true symbol in before we find the definition then
1933 for (dic = ic->prev; dic; dic = dic->prev)
1935 /* if there is a function call then don't pack it */
1936 if ((dic->op == CALL || dic->op == PCALL))
1945 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1946 IS_OP_VOLATILE (IC_RESULT (dic)))
1952 if (IS_SYMOP (IC_RESULT (dic)) &&
1953 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1955 if (POINTER_SET (dic))
1961 if (IS_SYMOP (IC_RIGHT (dic)) &&
1962 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1963 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1969 if (IS_SYMOP (IC_LEFT (dic)) &&
1970 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1971 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1977 if (POINTER_SET (dic) &&
1978 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1986 return 0; /* did not find */
1988 /* if the result is on stack or iaccess then it must be
1989 the same atleast one of the operands */
1990 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1991 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1994 /* the operation has only one symbol
1995 operator then we can pack */
1996 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1997 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2000 if (!((IC_LEFT (dic) &&
2001 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2003 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2007 /* found the definition */
2008 /* replace the result with the result of */
2009 /* this assignment and remove this assignment */
2010 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2012 IC_RESULT (dic) = IC_RESULT (ic);
2014 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2016 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2018 /* delete from liverange table also
2019 delete from all the points inbetween and the new
2021 for (sic = dic; sic != ic; sic = sic->next)
2023 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2024 if (IS_ITEMP (IC_RESULT (dic)))
2025 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2028 remiCodeFromeBBlock (ebp, ic);
2029 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2030 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2031 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2036 /*-----------------------------------------------------------------*/
2037 /* findAssignToSym : scanning backwards looks for first assig found */
2038 /*-----------------------------------------------------------------*/
2040 findAssignToSym (operand * op, iCode * ic)
2044 for (dic = ic->prev; dic; dic = dic->prev)
2047 /* if definition by assignment */
2048 if (dic->op == '=' &&
2049 !POINTER_SET (dic) &&
2050 IC_RESULT (dic)->key == op->key
2051 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2055 /* we are interested only if defined in far space */
2056 /* or in stack space in case of + & - */
2058 /* if assigned to a non-symbol then return
2060 if (!IS_SYMOP (IC_RIGHT (dic)))
2063 /* if the symbol is in far space then
2065 if (isOperandInFarSpace (IC_RIGHT (dic)))
2068 /* for + & - operations make sure that
2069 if it is on the stack it is the same
2070 as one of the three operands */
2071 if ((ic->op == '+' || ic->op == '-') &&
2072 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2075 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2076 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2077 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2085 /* if we find an usage then we cannot delete it */
2086 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2089 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2092 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2096 /* now make sure that the right side of dic
2097 is not defined between ic & dic */
2100 iCode *sic = dic->next;
2102 for (; sic != ic; sic = sic->next)
2103 if (IC_RESULT (sic) &&
2104 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2113 /*-----------------------------------------------------------------*/
2114 /* packRegsForSupport :- reduce some registers for support calls */
2115 /*-----------------------------------------------------------------*/
2117 packRegsForSupport (iCode * ic, eBBlock * ebp)
2121 /* for the left & right operand :- look to see if the
2122 left was assigned a true symbol in far space in that
2123 case replace them */
2124 if (IS_ITEMP (IC_LEFT (ic)) &&
2125 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2127 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2133 /* found it we need to remove it from the
2135 for (sic = dic; sic != ic; sic = sic->next) {
2136 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2137 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2140 IC_LEFT (ic)->operand.symOperand =
2141 IC_RIGHT (dic)->operand.symOperand;
2142 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2143 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2144 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2145 remiCodeFromeBBlock (ebp, dic);
2146 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2150 /* do the same for the right operand */
2153 IS_ITEMP (IC_RIGHT (ic)) &&
2154 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2156 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2162 /* if this is a subtraction & the result
2163 is a true symbol in far space then don't pack */
2164 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2166 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2167 if (IN_FARSPACE (SPEC_OCLS (etype)))
2170 /* found it we need to remove it from the
2172 for (sic = dic; sic != ic; sic = sic->next) {
2173 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2174 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2177 IC_RIGHT (ic)->operand.symOperand =
2178 IC_RIGHT (dic)->operand.symOperand;
2179 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2180 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2181 remiCodeFromeBBlock (ebp, dic);
2182 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2183 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2190 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2193 /*-----------------------------------------------------------------*/
2194 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2195 /*-----------------------------------------------------------------*/
2196 static int packRegsDPTRnuse( operand *op , int dptr)
2201 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2202 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2205 /* first check if any overlapping liverange has already been
2206 assigned to this DPTR */
2207 if (OP_SYMBOL(op)->clashes) {
2208 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2210 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2211 sym = hTabItemWithKey(liveRanges,i);
2212 if (sym->dptr == dptr) return 0;
2217 /* future for more dptrs */
2219 OP_SYMBOL(op)->dptr = dptr;
2223 /* DPTR1 is special since it is also used as a scratch by the backend .
2224 so we walk thru the entire live range of this operand and make sure
2225 DPTR1 will not be used by the backed . The logic here is to find out if
2226 more than one operand in an icode is in far space then we give up : we
2227 don't keep it live across functions for now
2230 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2231 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2232 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2235 if (ic->op == CALL || ic->op == PCALL) return 0;
2237 /* single operand icode are ok */
2238 if (ic->op == IFX || ic->op == IPUSH)
2241 if (ic->op == SEND ) {
2242 if (ic->argreg != 1 ) return 0;
2245 /* two special cases first */
2246 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2247 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2248 (isOperandInFarSpace(IC_RESULT(ic)) &&
2249 !isOperandInReg(IC_RESULT(ic)))) {
2253 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2254 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2255 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2256 !isOperandInReg(IC_RIGHT(ic)))) {
2260 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2261 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2262 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2263 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2264 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2268 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2269 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2270 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2271 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2272 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2275 /* same for right */
2276 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2277 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2278 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2279 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2280 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2284 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2285 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2287 if (nfs > 1) return 0;
2289 OP_SYMBOL(op)->dptr = dptr;
2293 /*-----------------------------------------------------------------*/
2294 /* packRegsDPTRuse : - will reduce some registers for single Use */
2295 /*-----------------------------------------------------------------*/
2297 packRegsDPTRuse (operand * op)
2299 /* go thru entire liveRange of this variable & check for
2300 other possible usage of DPTR , if we don't find it the
2301 assign this to DPTR (ruonly)
2306 sym_link *type, *etype;
2308 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2309 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2311 /* first check if any overlapping liverange has already been
2313 if (OP_SYMBOL(op)->clashes) {
2314 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2315 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2316 sym = hTabItemWithKey(liveRanges,i);
2317 if (sym->ruonly) return NULL ;
2322 /* no then go thru this guys live range */
2323 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2324 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2325 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2327 if (SKIP_IC3(ic)) continue;
2329 /* if PCALL cannot be sure give up */
2330 if (ic->op == PCALL) return NULL;
2332 /* if SEND & not the first parameter then giveup */
2333 if (ic->op == SEND && ic->argreg != 1 &&
2334 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2335 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2337 /* if CALL then make sure it is VOID || return value not used
2338 or the return value is assigned to this one */
2339 if (ic->op == CALL) {
2340 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2341 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2342 etype = getSpec(type = operandType(IC_RESULT(ic)));
2343 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2348 /* special case of add with a [remat] */
2349 if (ic->op == '+' &&
2350 OP_SYMBOL(IC_LEFT(ic))->remat &&
2351 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2352 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2356 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2357 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2360 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2361 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2363 /* conditionals can destroy 'b' - make sure B wont
2364 be used in this one*/
2365 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2366 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2367 getSize(operandType(op)) > 3) return NULL;
2369 /* if this is a cast to a bigger type */
2371 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2372 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2373 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2379 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2380 !isOperandEqual(IC_RESULT(ic),op) &&
2381 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2382 !isOperandInReg(IC_RESULT(ic))) ||
2383 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2385 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2386 !isOperandEqual(IC_RIGHT(ic),op) &&
2387 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2388 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2389 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2390 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2391 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2393 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2394 !isOperandEqual(IC_LEFT(ic),op) &&
2395 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2396 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2397 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2398 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2399 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2401 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2402 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2403 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2404 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2407 OP_SYMBOL(op)->ruonly = 1;
2408 if (OP_SYMBOL(op)->usl.spillLoc) {
2409 if (OP_SYMBOL(op)->spillA)
2410 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2411 OP_SYMBOL(op)->usl.spillLoc = NULL;
2416 /*-----------------------------------------------------------------*/
2417 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2418 /*-----------------------------------------------------------------*/
2420 isBitwiseOptimizable (iCode * ic)
2422 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2423 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2425 /* bitwise operations are considered optimizable
2426 under the following conditions (Jean-Louis VERN)
2438 if ( IS_LITERAL (rtype) ||
2439 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2445 /*-----------------------------------------------------------------*/
2446 /* packRegsForAccUse - pack registers for acc use */
2447 /*-----------------------------------------------------------------*/
2449 packRegsForAccUse (iCode * ic)
2453 /* if this is an aggregate, e.g. a one byte char array */
2454 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2458 /* if we are calling a reentrant function that has stack parameters */
2459 if (ic->op == CALL &&
2460 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2461 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2464 if (ic->op == PCALL &&
2465 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2466 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2469 /* if + or - then it has to be one byte result */
2470 if ((ic->op == '+' || ic->op == '-')
2471 && getSize (operandType (IC_RESULT (ic))) > 1)
2474 /* if shift operation make sure right side is not a literal */
2475 if (ic->op == RIGHT_OP &&
2476 (isOperandLiteral (IC_RIGHT (ic)) ||
2477 getSize (operandType (IC_RESULT (ic))) > 1))
2480 if (ic->op == LEFT_OP &&
2481 (isOperandLiteral (IC_RIGHT (ic)) ||
2482 getSize (operandType (IC_RESULT (ic))) > 1))
2485 if (IS_BITWISE_OP (ic) &&
2486 getSize (operandType (IC_RESULT (ic))) > 1)
2490 /* has only one definition */
2491 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2494 /* has only one use */
2495 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2498 /* and the usage immediately follows this iCode */
2499 if (!(uic = hTabItemWithKey (iCodehTab,
2500 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2503 if (ic->next != uic)
2506 /* if it is a conditional branch then we definitely can */
2510 if (uic->op == JUMPTABLE)
2513 /* if the usage is not is an assignment
2514 or an arithmetic / bitwise / shift operation then not */
2515 if (POINTER_SET (uic) &&
2516 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2519 if (uic->op != '=' &&
2520 !IS_ARITHMETIC_OP (uic) &&
2521 !IS_BITWISE_OP (uic) &&
2522 uic->op != LEFT_OP &&
2523 uic->op != RIGHT_OP)
2526 /* if used in ^ operation then make sure right is not a
2528 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2531 /* if shift operation make sure right side is not a literal */
2532 if (uic->op == RIGHT_OP &&
2533 (isOperandLiteral (IC_RIGHT (uic)) ||
2534 getSize (operandType (IC_RESULT (uic))) > 1))
2537 if (uic->op == LEFT_OP &&
2538 (isOperandLiteral (IC_RIGHT (uic)) ||
2539 getSize (operandType (IC_RESULT (uic))) > 1))
2542 /* make sure that the result of this icode is not on the
2543 stack, since acc is used to compute stack offset */
2544 if (isOperandOnStack(IC_RESULT(uic)))
2547 /* if either one of them in far space then we cannot */
2548 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2549 isOperandInFarSpace (IC_LEFT (uic))) ||
2550 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2551 isOperandInFarSpace (IC_RIGHT (uic))))
2554 /* if the usage has only one operand then we can */
2555 if (IC_LEFT (uic) == NULL ||
2556 IC_RIGHT (uic) == NULL)
2559 /* make sure this is on the left side if not
2560 a '+' since '+' is commutative */
2561 if (ic->op != '+' &&
2562 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2565 /* if the other one is not on stack then we can */
2566 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2567 (IS_ITEMP (IC_RIGHT (uic)) ||
2568 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2569 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2572 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2573 (IS_ITEMP (IC_LEFT (uic)) ||
2574 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2575 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2581 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2586 /*-----------------------------------------------------------------*/
2587 /* packForPush - hueristics to reduce iCode for pushing */
2588 /*-----------------------------------------------------------------*/
2590 packForPush (iCode * ic, eBBlock * ebp)
2595 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2598 /* must have only definition & one usage */
2599 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2600 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2603 /* find the definition */
2604 if (!(dic = hTabItemWithKey (iCodehTab,
2605 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2608 if (dic->op != '=' || POINTER_SET (dic))
2611 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2613 /* make sure the right side does not have any definitions
2615 dbv = OP_DEFS(IC_RIGHT(dic));
2616 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2617 if (bitVectBitValue(dbv,lic->key)) return ;
2619 /* make sure they have the same type */
2621 sym_link *itype=operandType(IC_LEFT(ic));
2622 sym_link *ditype=operandType(IC_RIGHT(dic));
2624 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2625 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2628 /* extend the live range of replaced operand if needed */
2629 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2630 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2631 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2632 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2633 OP_SYMBOL(IC_LEFT(ic))->clashes);
2635 for (lic = ic; lic && lic != dic; lic = lic->prev)
2637 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2638 if (IS_ITEMP (IC_RIGHT (dic)))
2639 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2641 /* we now we know that it has one & only one def & use
2642 and the that the definition is an assignment */
2643 IC_LEFT (ic) = IC_RIGHT (dic);
2645 remiCodeFromeBBlock (ebp, dic);
2646 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2647 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2650 /*-----------------------------------------------------------------*/
2651 /* packRegisters - does some transformations to reduce register */
2653 /*-----------------------------------------------------------------*/
2655 packRegisters (eBBlock * ebp)
2665 /* look for assignments of the form */
2666 /* iTempNN = TRueSym (someoperation) SomeOperand */
2668 /* TrueSym := iTempNN:1 */
2669 for (ic = ebp->sch; ic; ic = ic->next)
2673 /* find assignment of the form TrueSym := iTempNN:1 */
2674 if (ic->op == '=' && !POINTER_SET (ic))
2675 change += packRegsForAssign (ic, ebp);
2682 for (ic = ebp->sch; ic; ic = ic->next)
2685 /* if this is an itemp & result of a address of a true sym
2686 then mark this as rematerialisable */
2687 if (ic->op == ADDRESS_OF &&
2688 IS_ITEMP (IC_RESULT (ic)) &&
2689 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2690 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2691 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2694 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2695 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2696 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2700 /* if this is an itemp & used as a pointer
2701 & assigned to a literal then remat */
2702 if (IS_ASSIGN_ICODE(ic) &&
2703 IS_ITEMP(IC_RESULT(ic)) &&
2704 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2705 isOperandLiteral(IC_RIGHT(ic)))
2707 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2708 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2709 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2712 /* if straight assignment then carry remat flag if
2713 this is the only definition */
2714 if (ic->op == '=' &&
2715 !POINTER_SET (ic) &&
2716 IS_SYMOP (IC_RIGHT (ic)) &&
2717 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2718 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2719 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2722 OP_SYMBOL (IC_RESULT (ic))->remat =
2723 OP_SYMBOL (IC_RIGHT (ic))->remat;
2724 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2725 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2728 /* if cast to a generic pointer & the pointer being
2729 cast is remat, then we can remat this cast as well */
2730 if (ic->op == CAST &&
2731 IS_SYMOP(IC_RIGHT(ic)) &&
2732 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2733 OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2734 sym_link *to_type = operandType(IC_LEFT(ic));
2735 sym_link *from_type = operandType(IC_RIGHT(ic));
2736 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2737 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2738 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2739 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2743 /* if this is a +/- operation with a rematerizable
2744 then mark this as rematerializable as well */
2745 if ((ic->op == '+' || ic->op == '-') &&
2746 (IS_SYMOP (IC_LEFT (ic)) &&
2747 IS_ITEMP (IC_RESULT (ic)) &&
2748 OP_SYMBOL (IC_LEFT (ic))->remat &&
2749 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2750 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2751 IS_OP_LITERAL (IC_RIGHT (ic))))
2754 //int i = operandLitValue(IC_RIGHT(ic));
2755 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2756 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2757 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2760 /* mark the pointer usages */
2761 if (POINTER_SET (ic))
2762 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2764 if (POINTER_GET (ic))
2765 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2767 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2768 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2770 if (ic->op == RECEIVE && ic->argreg == 1 &&
2771 getSize (operandType(IC_RESULT(ic))) <= 3)
2772 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2774 if (ic->op == SEND && ic->argreg == 1 &&
2775 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2776 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2780 /* if we are using a symbol on the stack
2781 then we should say ds390_ptrRegReq */
2782 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2783 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2784 OP_SYMBOL (IC_COND (ic))->iaccess);
2785 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2786 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2787 OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2790 if (IS_SYMOP (IC_LEFT (ic)))
2791 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2792 OP_SYMBOL (IC_LEFT (ic))->iaccess);
2793 if (IS_SYMOP (IC_RIGHT (ic)))
2794 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2795 OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2796 if (IS_SYMOP (IC_RESULT (ic)))
2797 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2798 OP_SYMBOL (IC_RESULT (ic))->iaccess);
2802 /* if the condition of an if instruction
2803 is defined in the previous instruction and
2804 this is the only usage then
2805 mark the itemp as a conditional */
2806 if ((IS_CONDITIONAL (ic) ||
2807 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2808 ic->next && ic->next->op == IFX &&
2809 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2810 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2811 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2813 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2817 /* reduce for support function calls */
2818 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2819 packRegsForSupport (ic, ebp);
2821 /* some cases the redundant moves can
2822 can be eliminated for return statements . Can be elminated for the first SEND */
2823 if ((ic->op == RETURN ||
2824 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
2825 !isOperandInFarSpace (IC_LEFT (ic)) &&
2828 packRegsDPTRuse (IC_LEFT (ic));
2831 if (ic->op == CALL) {
2832 sym_link *ftype = operandType(IC_LEFT(ic));
2833 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2834 !IFFUNC_ISBUILTIN(ftype)) {
2835 packRegsDPTRuse (IC_RESULT (ic));
2839 /* if pointer set & left has a size more than
2840 one and right is not in far space */
2841 if (POINTER_SET (ic) &&
2842 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2843 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2844 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2845 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2847 packRegsDPTRuse (IC_RESULT (ic));
2850 /* if pointer get */
2851 if (POINTER_GET (ic) &&
2852 !isOperandInFarSpace (IC_RESULT (ic)) &&
2853 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2854 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2855 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2857 packRegsDPTRuse (IC_LEFT (ic));
2860 /* if this is cast for intergral promotion then
2861 check if only use of the definition of the
2862 operand being casted/ if yes then replace
2863 the result of that arithmetic operation with
2864 this result and get rid of the cast */
2867 sym_link *fromType = operandType (IC_RIGHT (ic));
2868 sym_link *toType = operandType (IC_LEFT (ic));
2870 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2871 getSize (fromType) != getSize (toType) &&
2872 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2875 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2878 if (IS_ARITHMETIC_OP (dic))
2880 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2881 IC_RESULT (dic) = IC_RESULT (ic);
2882 remiCodeFromeBBlock (ebp, ic);
2883 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2884 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2885 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2889 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2895 /* if the type from and type to are the same
2896 then if this is the only use then packit */
2897 if (compareType (operandType (IC_RIGHT (ic)),
2898 operandType (IC_LEFT (ic))) == 1)
2900 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2903 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2904 IC_RESULT (dic) = IC_RESULT (ic);
2905 remiCodeFromeBBlock (ebp, ic);
2906 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2907 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2908 OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2916 iTempNN := (some variable in farspace) V1
2921 if (ic->op == IPUSH || ic->op == SEND)
2923 packForPush (ic, ebp);
2927 /* pack registers for accumulator use, when the
2928 result of an arithmetic or bit wise operation
2929 has only one use, that use is immediately following
2930 the defintion and the using iCode has only one
2931 operand or has two operands but one is literal &
2932 the result of that operation is not on stack then
2933 we can leave the result of this operation in acc:b
2935 if ((IS_ARITHMETIC_OP (ic)
2936 || IS_CONDITIONAL(ic)
2937 || IS_BITWISE_OP (ic)
2938 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2939 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2941 IS_ITEMP (IC_RESULT (ic)) &&
2942 getSize (operandType (IC_RESULT (ic))) <= 2)
2944 packRegsForAccUse (ic);
2949 /*-----------------------------------------------------------------*/
2950 /* assignRegisters - assigns registers to each live range as need */
2951 /*-----------------------------------------------------------------*/
2953 ds390_assignRegisters (eBBlock ** ebbs, int count)
2958 setToNull ((void *) &_G.funcrUsed);
2959 setToNull ((void *) &_G.regAssigned);
2960 setToNull ((void *) &_G.totRegAssigned);
2961 setToNull ((void *) &_G.funcrUsed);
2962 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2964 if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2965 /* change assignments this will remove some
2966 live ranges reducing some register pressure */
2967 for (i = 0; i < count; i++)
2968 packRegisters (ebbs[i]);
2970 if (options.dump_pack)
2971 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2973 /* first determine for each live range the number of
2974 registers & the type of registers required for each */
2977 /* and serially allocate registers */
2978 serialRegAssign (ebbs, count);
2985 /* if stack was extended then tell the user */
2988 /* werror(W_TOOMANY_SPILS,"stack", */
2989 /* _G.stackExtend,currFunc->name,""); */
2995 /* werror(W_TOOMANY_SPILS,"data space", */
2996 /* _G.dataExtend,currFunc->name,""); */
3000 /* after that create the register mask
3001 for each of the instruction */
3002 createRegMask (ebbs, count);
3004 /* redo that offsets for stacked automatic variables */
3005 redoStackOffsets ();
3007 if (options.dump_rassgn) {
3008 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3009 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3012 /* do the overlaysegment stuff SDCCmem.c */
3013 doOverlays (ebbs, count);
3015 /* now get back the chain */
3016 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3021 /* free up any _G.stackSpil locations allocated */
3022 applyToSet (_G.stackSpil, deallocStackSpil);
3024 setToNull ((void **) &_G.stackSpil);
3025 setToNull ((void **) &_G.spiltSet);
3026 /* mark all registers as free */