1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (DS80C390) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although */
32 /* some routines are non-processor specific & can be reused when */
33 /* targetting other processors. The decision for this will have */
34 /* to be made on a routine by routine basis */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU */
37 /*-----------------------------------------------------------------*/
47 bitVect *totRegAssigned; /* final set of LRs that got into registers */
50 bitVect *funcrUsed; /* registers used in a function */
53 bitVect *allBitregs; /* all bit registers */
57 /* Shared with gen.c */
58 int ds390_ptrRegReq; /* one byte pointer register required */
64 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
65 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
66 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
67 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
68 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
69 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
70 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
71 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
72 {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
73 {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
74 {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
75 {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
76 {REG_BIT, B0_IDX, REG_BIT, "b0", "b0", "bits", 0, 1, 0},
77 {REG_BIT, B1_IDX, REG_BIT, "b1", "b1", "bits", 1, 1, 0},
78 {REG_BIT, B2_IDX, REG_BIT, "b2", "b2", "bits", 2, 1, 0},
79 {REG_BIT, B3_IDX, REG_BIT, "b3", "b3", "bits", 3, 1, 0},
80 {REG_BIT, B4_IDX, REG_BIT, "b4", "b4", "bits", 4, 1, 0},
81 {REG_BIT, B5_IDX, REG_BIT, "b5", "b5", "bits", 5, 1, 0},
82 {REG_BIT, B6_IDX, REG_BIT, "b6", "b6", "bits", 6, 1, 0},
83 {REG_BIT, B7_IDX, REG_BIT, "b7", "b7", "bits", 7, 1, 0},
84 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
85 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
86 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
87 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
88 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
89 {REG_CND, CND_IDX, REG_GPR, "C", "psw", "xreg", 0, 0, 0},
90 {0, DPL1_IDX, 0, "dpl1", "dpl1", "dpl1", 0, 0, 0},
91 {0, DPH1_IDX, 0, "dph1", "dph1", "dph1", 0, 0, 0},
92 {0, DPX1_IDX, 0, "dpx1", "dpx1", "dpx1", 0, 0, 0},
93 {0, DPS_IDX, 0, "dps", "dps", "dps", 0, 0, 0},
94 {0, A_IDX, 0, "a", "acc", "acc", 0, 0, 0},
95 {0, AP_IDX, 0, "ap", "ap", "ap", 0, 0, 0},
99 int ds390_nBitRegs = 0;
101 static void spillThis (symbol *);
102 static void freeAllRegs ();
103 static iCode * packRegsDPTRuse (operand *);
104 static int packRegsDPTRnuse (operand *,unsigned);
106 /*-----------------------------------------------------------------*/
107 /* allocReg - allocates register of given type */
108 /*-----------------------------------------------------------------*/
110 allocReg (short type)
114 for (i = 0; i < ds390_nRegs; i++)
117 /* if type is given as 0 then any
118 free register will do */
122 regs390[i].isFree = 0;
124 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
127 /* otherwise look for specific type of register */
128 if (regs390[i].isFree &&
129 regs390[i].type == type)
131 regs390[i].isFree = 0;
133 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
140 /*-----------------------------------------------------------------*/
141 /* ds390_regWithIdx - returns pointer to register with index number*/
142 /*-----------------------------------------------------------------*/
144 ds390_regWithIdx (int idx)
148 for (i = 0; i < sizeof(regs390)/sizeof(regs); i++)
149 if (regs390[i].rIdx == idx)
152 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
153 "regWithIdx not found");
157 /*-----------------------------------------------------------------*/
158 /* freeReg - frees a register */
159 /*-----------------------------------------------------------------*/
165 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
166 "freeReg - Freeing NULL register");
173 /*-----------------------------------------------------------------*/
174 /* useReg - marks a register as used */
175 /*-----------------------------------------------------------------*/
182 /*-----------------------------------------------------------------*/
183 /* nFreeRegs - returns number of free registers */
184 /*-----------------------------------------------------------------*/
191 for (i = 0; i < ds390_nRegs; i++)
192 if (regs390[i].isFree && regs390[i].type == type)
197 /*-----------------------------------------------------------------*/
198 /* nfreeRegsType - free registers with type */
199 /*-----------------------------------------------------------------*/
201 nfreeRegsType (int type)
206 if ((nfr = nFreeRegs (type)) == 0)
207 return nFreeRegs (REG_GPR);
210 return nFreeRegs (type);
213 /*-----------------------------------------------------------------*/
214 /* isOperandInReg - returns true if operand is currently in regs */
215 /*-----------------------------------------------------------------*/
216 static int isOperandInReg(operand *op)
218 if (!IS_SYMOP(op)) return 0;
219 if (OP_SYMBOL(op)->ruonly) return 1;
220 if (OP_SYMBOL(op)->accuse) return 1;
221 if (OP_SYMBOL(op)->dptr) return 1;
222 return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
225 /*-----------------------------------------------------------------*/
226 /* computeSpillable - given a point find the spillable live ranges */
227 /*-----------------------------------------------------------------*/
229 computeSpillable (iCode * ic)
233 /* spillable live ranges are those that are live at this
234 point . the following categories need to be subtracted
236 a) - those that are already spilt
237 b) - if being used by this one
238 c) - defined by this one */
240 spillable = bitVectCopy (ic->rlive);
242 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
244 bitVectCplAnd (spillable, ic->uses); /* used in this one */
245 bitVectUnSetBit (spillable, ic->defKey);
246 spillable = bitVectIntersect (spillable, _G.regAssigned);
251 /*-----------------------------------------------------------------*/
252 /* bitType - will return 1 if the symbol has type REG_BIT */
253 /*-----------------------------------------------------------------*/
255 bitType (symbol * sym, eBBlock * ebp, iCode * ic)
257 return (sym->regType == REG_BIT ? 1 : 0);
260 /*-----------------------------------------------------------------*/
261 /* noSpilLoc - return true if a variable has no spil location */
262 /*-----------------------------------------------------------------*/
264 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
266 return (sym->usl.spillLoc ? 0 : 1);
269 /*-----------------------------------------------------------------*/
270 /* hasSpilLoc - will return 1 if the symbol has spil location */
271 /*-----------------------------------------------------------------*/
273 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
275 return (sym->usl.spillLoc ? 1 : 0);
278 /*-----------------------------------------------------------------*/
279 /* directSpilLoc - will return 1 if the spillocation is in direct */
280 /*-----------------------------------------------------------------*/
282 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
284 if (sym->usl.spillLoc &&
285 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
291 /*-----------------------------------------------------------------*/
292 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
293 /* but is not used as a pointer */
294 /*-----------------------------------------------------------------*/
296 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
298 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
301 /*-----------------------------------------------------------------*/
302 /* rematable - will return 1 if the remat flag is set */
303 /*-----------------------------------------------------------------*/
305 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
310 /*-----------------------------------------------------------------*/
311 /* notUsedInRemaining - not used or defined in remain of the block */
312 /*-----------------------------------------------------------------*/
314 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
316 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
317 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
320 /*-----------------------------------------------------------------*/
321 /* allLRs - return true for all */
322 /*-----------------------------------------------------------------*/
324 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
329 /*-----------------------------------------------------------------*/
330 /* liveRangesWith - applies function to a given set of live range */
331 /*-----------------------------------------------------------------*/
333 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
334 eBBlock * ebp, iCode * ic)
339 if (!lrs || !lrs->size)
342 for (i = 1; i < lrs->size; i++)
345 if (!bitVectBitValue (lrs, i))
348 /* if we don't find it in the live range
349 hash table we are in serious trouble */
350 if (!(sym = hTabItemWithKey (liveRanges, i)))
352 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
353 "liveRangesWith could not find liveRange");
357 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
358 addSetHead (&rset, sym);
365 /*-----------------------------------------------------------------*/
366 /* leastUsedLR - given a set determines which is the least used */
367 /*-----------------------------------------------------------------*/
369 leastUsedLR (set * sset)
371 symbol *sym = NULL, *lsym = NULL;
373 sym = lsym = setFirstItem (sset);
378 for (; lsym; lsym = setNextItem (sset))
381 /* if usage is the same then prefer
382 to spill the smaller of the two */
383 if (lsym->used == sym->used)
384 if (getSize (lsym->type) < getSize (sym->type))
388 if (lsym->used < sym->used)
393 setToNull ((void *) &sset);
398 /*-----------------------------------------------------------------*/
399 /* noOverLap - will iterate through the list looking for over lap */
400 /*-----------------------------------------------------------------*/
402 noOverLap (set * itmpStack, symbol * fsym)
406 for (sym = setFirstItem (itmpStack); sym;
407 sym = setNextItem (itmpStack))
409 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
414 /*-----------------------------------------------------------------*/
415 /* isFree - will return 1 if the a free spil location is found */
416 /*-----------------------------------------------------------------*/
421 V_ARG (symbol **, sloc);
422 V_ARG (symbol *, fsym);
424 /* if already found */
428 /* if it is free && and the itmp assigned to
429 this does not have any overlapping live ranges
430 with the one currently being assigned and
431 the size can be accomodated */
433 noOverLap (sym->usl.itmpStack, fsym) &&
434 getSize (sym->type) >= getSize (fsym->type))
443 /*-----------------------------------------------------------------*/
444 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
445 /*-----------------------------------------------------------------*/
447 spillLRWithPtrReg (symbol * forSym)
453 if (!_G.regAssigned ||
454 bitVectIsZero (_G.regAssigned))
457 r0 = ds390_regWithIdx (R0_IDX);
458 r1 = ds390_regWithIdx (R1_IDX);
460 /* for all live ranges */
461 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
462 lrsym = hTabNextItem (liveRanges, &k))
466 /* if no registers assigned to it or spilt */
467 /* if it does not overlap this then
468 no need to spill it */
470 if (lrsym->isspilt || !lrsym->nRegs ||
471 (lrsym->liveTo < forSym->liveFrom))
474 /* go thru the registers : if it is either
475 r0 or r1 then spill it */
476 for (j = 0; j < lrsym->nRegs; j++)
477 if (lrsym->regs[j] == r0 ||
478 lrsym->regs[j] == r1)
487 /*-----------------------------------------------------------------*/
488 /* createStackSpil - create a location on the stack to spil */
489 /*-----------------------------------------------------------------*/
491 createStackSpil (symbol * sym)
494 int useXstack, model, noOverlay;
498 /* first go try and find a free one that is already
499 existing on the stack */
500 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
502 /* found a free one : just update & return */
503 sym->usl.spillLoc = sloc;
506 addSetHead (&sloc->usl.itmpStack, sym);
510 /* could not then have to create one , this is the hard part
511 we need to allocate this on the stack : this is really a
512 hack!! but cannot think of anything better at this time */
514 if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
515 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
517 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
522 sloc = newiTemp (slocBuffer);
524 /* set the type to the spilling symbol */
525 sloc->type = copyLinkChain (sym->type);
526 sloc->etype = getSpec (sloc->type);
527 if (!IS_BIT (sloc->etype))
529 if (options.model == MODEL_SMALL)
531 SPEC_SCLS (sloc->etype) = S_DATA;
535 SPEC_SCLS (sloc->etype) = S_XDATA;
538 SPEC_EXTR (sloc->etype) = 0;
539 SPEC_STAT (sloc->etype) = 0;
540 SPEC_VOLATILE(sloc->etype) = 0;
541 SPEC_ABSA(sloc->etype) = 0;
543 /* we don't allow it to be allocated
544 onto the external stack since : so we
545 temporarily turn it off ; we also
546 turn off memory model to prevent
547 the spil from going to the external storage
548 and turn off overlaying
551 useXstack = options.useXstack;
552 model = options.model;
553 noOverlay = options.noOverlay;
554 options.noOverlay = 1;
556 /* options.model = options.useXstack = 0; */
560 options.useXstack = useXstack;
561 options.model = model;
562 options.noOverlay = noOverlay;
563 sloc->isref = 1; /* to prevent compiler warning */
565 /* if it is on the stack then update the stack */
566 if (IN_STACK (sloc->etype))
568 currFunc->stack += getSize (sloc->type);
569 _G.stackExtend += getSize (sloc->type);
572 _G.dataExtend += getSize (sloc->type);
574 /* add it to the _G.stackSpil set */
575 addSetHead (&_G.stackSpil, sloc);
576 sym->usl.spillLoc = sloc;
579 /* add it to the set of itempStack set
580 of the spill location */
581 addSetHead (&sloc->usl.itmpStack, sym);
585 /*-----------------------------------------------------------------*/
586 /* isSpiltOnStack - returns true if the spil location is on stack */
587 /*-----------------------------------------------------------------*/
589 isSpiltOnStack (symbol * sym)
599 /* if (sym->_G.stackSpil) */
602 if (!sym->usl.spillLoc)
605 etype = getSpec (sym->usl.spillLoc->type);
606 if (IN_STACK (etype))
612 /*-----------------------------------------------------------------*/
613 /* spillThis - spils a specific operand */
614 /*-----------------------------------------------------------------*/
616 spillThis (symbol * sym)
619 /* if this is rematerializable or has a spillLocation
620 we are okay, else we need to create a spillLocation
622 if (!(sym->remat || sym->usl.spillLoc))
623 createStackSpil (sym);
625 /* mark it has spilt & put it in the spilt set */
626 sym->isspilt = sym->spillA = 1;
627 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
629 bitVectUnSetBit (_G.regAssigned, sym->key);
630 bitVectUnSetBit (_G.totRegAssigned, sym->key);
632 for (i = 0; i < sym->nRegs; i++)
636 freeReg (sym->regs[i]);
640 /* if spilt on stack then free up r0 & r1
641 if they could have been assigned to some
643 if (!ds390_ptrRegReq && isSpiltOnStack (sym) && !options.stack10bit)
646 spillLRWithPtrReg (sym);
649 if (sym->usl.spillLoc && !sym->remat)
650 sym->usl.spillLoc->allocreq++;
654 /*-----------------------------------------------------------------*/
655 /* selectSpil - select a iTemp to spil : rather a simple procedure */
656 /*-----------------------------------------------------------------*/
658 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
660 bitVect *lrcs = NULL;
664 /* get the spillable live ranges */
665 lrcs = computeSpillable (ic);
667 /* remove incompatible registers */
668 if ((forSym->regType == REG_PTR) || (forSym->regType == REG_GPR))
670 selectS = liveRangesWith (lrcs, bitType, ebp, ic);
672 for (sym = setFirstItem (selectS); sym; sym = setNextItem (selectS))
674 bitVectUnSetBit (lrcs, sym->key);
678 /* get all live ranges that are rematerializable */
679 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
681 /* return the least used of these */
682 return leastUsedLR (selectS);
685 /* get live ranges with spillLocations in direct space */
686 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
688 sym = leastUsedLR (selectS);
689 strncpyz (sym->rname,
690 sym->usl.spillLoc->rname[0] ?
691 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
694 /* mark it as allocation required */
695 sym->usl.spillLoc->allocreq++;
699 /* if the symbol is local to the block then */
700 if (forSym->liveTo < ebp->lSeq)
702 /* check if there are any live ranges allocated
703 to registers that are not used in this block */
704 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
706 sym = leastUsedLR (selectS);
707 /* if this is not rematerializable */
716 /* check if there are any live ranges that not
717 used in the remainder of the block */
719 !isiCodeInFunctionCall (ic) &&
720 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
722 sym = leastUsedLR (selectS);
735 /* find live ranges with spillocation && not used as pointers */
736 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
738 sym = leastUsedLR (selectS);
739 /* mark this as allocation required */
740 sym->usl.spillLoc->allocreq++;
744 /* find live ranges with spillocation */
745 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
747 sym = leastUsedLR (selectS);
748 sym->usl.spillLoc->allocreq++;
752 /* couldn't find then we need to create a spil
753 location on the stack , for which one? the least
755 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
757 /* return a created spil location */
758 sym = createStackSpil (leastUsedLR (selectS));
759 sym->usl.spillLoc->allocreq++;
763 /* this is an extreme situation we will spill
764 this one : happens very rarely but it does happen */
770 /*-----------------------------------------------------------------*/
771 /* spilSomething - spil some variable & mark registers as free */
772 /*-----------------------------------------------------------------*/
774 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
779 /* get something we can spil */
780 ssym = selectSpil (ic, ebp, forSym);
782 /* mark it as spilt */
783 ssym->isspilt = ssym->spillA = 1;
784 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
786 /* mark it as not register assigned &
787 take it away from the set */
788 bitVectUnSetBit (_G.regAssigned, ssym->key);
789 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
791 /* mark the registers as free */
792 for (i = 0; i < ssym->nRegs; i++)
794 freeReg (ssym->regs[i]);
796 /* if spilt on stack then free up r0 & r1
797 if they could have been assigned to as gprs */
798 if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
801 spillLRWithPtrReg (ssym);
804 /* if this was a block level spil then insert push & pop
805 at the start & end of block respectively */
808 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
809 /* add push to the start of the block */
810 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
811 ebp->sch->next : ebp->sch));
812 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
813 /* add pop to the end of the block */
814 addiCodeToeBBlock (ebp, nic, NULL);
817 /* if spilt because not used in the remainder of the
818 block then add a push before this instruction and
819 a pop at the end of the block */
820 if (ssym->remainSpil)
823 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
824 /* add push just before this instruction */
825 addiCodeToeBBlock (ebp, nic, ic);
827 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
828 /* add pop to the end of the block */
829 addiCodeToeBBlock (ebp, nic, NULL);
838 /*-----------------------------------------------------------------*/
839 /* getRegPtr - will try for PTR if not a GPR type if not spil */
840 /*-----------------------------------------------------------------*/
842 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
848 /* try for a ptr type */
849 if ((reg = allocReg (REG_PTR)))
852 /* try for gpr type */
853 if ((reg = allocReg (REG_GPR)))
856 /* we have to spil */
857 if (!spilSomething (ic, ebp, sym))
860 /* make sure partially assigned registers aren't reused */
861 for (j=0; j<=sym->nRegs; j++)
863 sym->regs[j]->isFree = 0;
865 /* this looks like an infinite loop but
866 in really selectSpil will abort */
870 /*-----------------------------------------------------------------*/
871 /* getRegGpr - will try for GPR if not spil */
872 /*-----------------------------------------------------------------*/
874 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
880 /* try for gpr type */
881 if ((reg = allocReg (REG_GPR)))
884 if (!ds390_ptrRegReq)
885 if ((reg = allocReg (REG_PTR)))
888 /* we have to spil */
889 if (!spilSomething (ic, ebp, sym))
892 /* make sure partially assigned registers aren't reused */
893 for (j=0; j<=sym->nRegs; j++)
895 sym->regs[j]->isFree = 0;
897 /* this looks like an infinite loop but
898 in really selectSpil will abort */
902 /*-----------------------------------------------------------------*/
903 /* getRegBit - will try for Bit if not spill this */
904 /*-----------------------------------------------------------------*/
905 static regs *getRegBit (symbol * sym)
909 /* try for a bit type */
910 if ((reg = allocReg (REG_BIT)))
917 /*-----------------------------------------------------------------*/
918 /* getRegPtrNoSpil - get it cannot be spilt */
919 /*-----------------------------------------------------------------*/
920 static regs *getRegPtrNoSpil()
924 /* try for a ptr type */
925 if ((reg = allocReg (REG_PTR)))
928 /* try for gpr type */
929 if ((reg = allocReg (REG_GPR)))
934 /* just to make the compiler happy */
938 /*-----------------------------------------------------------------*/
939 /* getRegGprNoSpil - get it cannot be spilt */
940 /*-----------------------------------------------------------------*/
941 static regs *getRegGprNoSpil()
945 if ((reg = allocReg (REG_GPR)))
948 if (!ds390_ptrRegReq)
949 if ((reg = allocReg (REG_PTR)))
954 /* just to make the compiler happy */
958 /*-----------------------------------------------------------------*/
959 /* getRegBitNoSpil - get it cannot be spilt */
960 /*-----------------------------------------------------------------*/
961 static regs *getRegBitNoSpil()
965 /* try for a bit type */
966 if ((reg = allocReg (REG_BIT)))
969 /* try for gpr type */
970 if ((reg = allocReg (REG_GPR)))
975 /* just to make the compiler happy */
979 /*-----------------------------------------------------------------*/
980 /* symHasReg - symbol has a given register */
981 /*-----------------------------------------------------------------*/
983 symHasReg (symbol * sym, regs * reg)
987 for (i = 0; i < sym->nRegs; i++)
988 if (sym->regs[i] == reg)
994 /*-----------------------------------------------------------------*/
995 /* updateRegUsage - update the registers in use at the start of */
997 /*-----------------------------------------------------------------*/
999 updateRegUsage (iCode * ic)
1003 for (reg=0; reg<ds390_nRegs; reg++)
1005 if (regs390[reg].isFree)
1007 ic->riu &= ~(1<<regs390[reg].offset);
1011 ic->riu |= (1<<regs390[reg].offset);
1012 BitBankUsed |= (reg >= B0_IDX);
1017 /*-----------------------------------------------------------------*/
1018 /* deassignLRs - check the live to and if they have registers & are */
1019 /* not spilt then free up the registers */
1020 /*-----------------------------------------------------------------*/
1022 deassignLRs (iCode * ic, eBBlock * ebp)
1028 for (sym = hTabFirstItem (liveRanges, &k); sym;
1029 sym = hTabNextItem (liveRanges, &k))
1032 symbol *psym = NULL;
1033 /* if it does not end here */
1034 if (sym->liveTo > ic->seq)
1037 /* if it was spilt on stack then we can
1038 mark the stack spil location as free */
1043 sym->usl.spillLoc->isFree = 1;
1049 if (!bitVectBitValue (_G.regAssigned, sym->key))
1052 /* special case check if this is an IFX &
1053 the privious one was a pop and the
1054 previous one was not spilt then keep track
1056 if (ic->op == IFX && ic->prev &&
1057 ic->prev->op == IPOP &&
1058 !ic->prev->parmPush &&
1059 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1060 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1066 bitVectUnSetBit (_G.regAssigned, sym->key);
1068 /* if the result of this one needs registers
1069 and does not have it then assign it right
1071 if (IC_RESULT (ic) &&
1072 !(SKIP_IC2 (ic) || /* not a special icode */
1073 ic->op == JUMPTABLE ||
1078 POINTER_SET (ic)) &&
1079 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1080 result->liveTo > ic->seq && /* and will live beyond this */
1081 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1082 result->liveFrom == ic->seq && /* does not start before here */
1083 result->regType == sym->regType && /* same register types */
1084 result->nRegs && /* which needs registers */
1085 !result->isspilt && /* and does not already have them */
1087 !bitVectBitValue (_G.regAssigned, result->key) &&
1088 /* the number of free regs + number of regs in this LR
1089 can accomodate the what result Needs */
1090 ((nfreeRegsType (result->regType) +
1091 sym->nRegs) >= result->nRegs)
1095 for (i = 0; i < result->nRegs; i++)
1097 result->regs[i] = sym->regs[i];
1099 result->regs[i] = getRegGpr (ic, ebp, result);
1101 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1102 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1106 /* free the remaining */
1107 for (; i < sym->nRegs; i++)
1111 if (!symHasReg (psym, sym->regs[i]))
1112 freeReg (sym->regs[i]);
1115 freeReg (sym->regs[i]);
1122 /*-----------------------------------------------------------------*/
1123 /* reassignLR - reassign this to registers */
1124 /*-----------------------------------------------------------------*/
1126 reassignLR (operand * op)
1128 symbol *sym = OP_SYMBOL (op);
1131 /* not spilt any more */
1132 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1133 bitVectUnSetBit (_G.spiltSet, sym->key);
1135 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1136 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1140 for (i = 0; i < sym->nRegs; i++)
1141 sym->regs[i]->isFree = 0;
1144 /*-----------------------------------------------------------------*/
1145 /* willCauseSpill - determines if allocating will cause a spill */
1146 /*-----------------------------------------------------------------*/
1148 willCauseSpill (int nr, int rt)
1150 /* first check if there are any available registers
1151 of the type required */
1154 /* special case for pointer type
1155 if pointer type not avlb then
1156 check for type gpr */
1157 if (nFreeRegs (rt) >= nr)
1159 if (nFreeRegs (REG_GPR) >= nr)
1162 else if (rt == REG_BIT)
1164 if (nFreeRegs (rt) >= nr)
1169 if (ds390_ptrRegReq)
1171 if (nFreeRegs (rt) >= nr)
1176 if (nFreeRegs (REG_PTR) +
1177 nFreeRegs (REG_GPR) >= nr)
1182 /* it will cause a spil */
1186 /*-----------------------------------------------------------------*/
1187 /* positionRegs - the allocator can allocate same registers to res- */
1188 /* ult and operand, if this happens make sure they are in the same */
1189 /* position as the operand otherwise chaos results */
1190 /*-----------------------------------------------------------------*/
1192 positionRegs (symbol * result, symbol * opsym)
1194 int count = min (result->nRegs, opsym->nRegs);
1195 int i, j = 0, shared = 0;
1198 /* if the result has been spilt then cannot share */
1203 /* first make sure that they actually share */
1204 for (i = 0; i < count; i++)
1206 for (j = 0; j < count; j++)
1208 if (result->regs[i] == opsym->regs[j] && i != j)
1218 regs *tmp = result->regs[i];
1219 result->regs[i] = result->regs[j];
1220 result->regs[j] = tmp;
1227 /*-----------------------------------------------------------------*/
1228 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1229 /*-----------------------------------------------------------------*/
1230 bitVect *unusedLRs (eBBlock *ebp)
1232 bitVect *ret = NULL;
1236 if (!ebp) return NULL;
1237 for (sym = hTabFirstItem(liveRanges,&key); sym ;
1238 sym = hTabNextItem(liveRanges,&key)) {
1240 if (notUsedInBlock(sym,ebp,NULL)) {
1241 ret = bitVectSetBit(ret,sym->key);
1248 /*-----------------------------------------------------------------*/
1249 /* deassignUnsedLRs - if this baisc block ends in a return then */
1250 /* deassign symbols not used in this block */
1251 /*-----------------------------------------------------------------*/
1252 bitVect *deassignUnsedLRs(eBBlock *ebp)
1254 bitVect *unused = NULL;
1257 switch (returnAtEnd(ebp)) {
1258 case 2: /* successor block ends in a return */
1259 unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1261 case 1: /* this block ends in a return */
1262 unused = bitVectIntersect(unused,unusedLRs(ebp));
1267 for (i = 0 ; i < unused->size ; i++ ) {
1270 if (bitVectBitValue(unused,i)) {
1272 /* if assigned to registers */
1273 if (bitVectBitValue(_G.regAssigned,i)) {
1277 sym = hTabItemWithKey(liveRanges,i);
1278 /* remove it from regassigned & mark the
1280 bitVectUnSetBit(_G.regAssigned,i);
1281 for (j = 0 ; j < sym->nRegs; j++)
1282 freeReg(sym->regs[j]);
1284 /* not assigned to registers : remove from set*/
1285 bitVectUnSetBit(unused,i);
1293 /*-----------------------------------------------------------------*/
1294 /* reassignUnusedLRs - put registers to unused Live ranges */
1295 /*-----------------------------------------------------------------*/
1296 void reassignUnusedLRs (bitVect *unused)
1299 if (!unused) return ;
1301 for (i = 0 ; i < unused->size ; i++ ) {
1302 /* if unused : means it was assigned to registers before */
1303 if (bitVectBitValue(unused,i)) {
1307 /* put it back into reg set*/
1308 bitVectSetBit(_G.regAssigned,i) ;
1310 sym = hTabItemWithKey(liveRanges,i);
1311 /* make registers busy */
1312 for (j = 0 ; j < sym->nRegs; j++)
1313 sym->regs[j]->isFree = 0;
1318 /*------------------------------------------------------------------*/
1319 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1320 /* it should either have registers or have beed spilled. Otherwise, */
1321 /* there was an uninitialized variable, so just spill this to get */
1322 /* the operand in a valid state. */
1323 /*------------------------------------------------------------------*/
1325 verifyRegsAssigned (operand *op, iCode * ic)
1330 if (!IS_ITEMP (op)) return;
1332 sym = OP_SYMBOL (op);
1333 if (sym->isspilt) return;
1334 if (!sym->nRegs) return;
1335 if (sym->regs[0]) return;
1337 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1338 sym->prereqv ? sym->prereqv->name : sym->name);
1343 /*-----------------------------------------------------------------*/
1344 /* serialRegAssign - serially allocate registers to the variables */
1345 /*-----------------------------------------------------------------*/
1347 serialRegAssign (eBBlock ** ebbs, int count)
1351 /* for all blocks */
1352 for (i = 0; i < count; i++)
1356 bitVect *unusedLRs = NULL;
1358 if (ebbs[i]->noPath &&
1359 (ebbs[i]->entryLabel != entryLabel &&
1360 ebbs[i]->entryLabel != returnLabel))
1363 unusedLRs = deassignUnsedLRs(ebbs[i]);
1365 /* for all instructions do */
1366 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1370 /* if this is an ipop that means some live
1371 range will have to be assigned again */
1373 reassignLR (IC_LEFT (ic));
1375 /* if result is present && is a true symbol */
1376 if (IC_RESULT (ic) && ic->op != IFX &&
1377 IS_TRUE_SYMOP (IC_RESULT (ic)))
1378 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1380 /* take away registers from live
1381 ranges that end at this instruction */
1382 deassignLRs (ic, ebbs[i]);
1384 /* some don't need registers */
1385 if (SKIP_IC2 (ic) ||
1386 ic->op == JUMPTABLE ||
1390 (IC_RESULT (ic) && POINTER_SET (ic)))
1393 /* now we need to allocate registers
1394 only for the result */
1397 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1403 /* Make sure any spill location is definitely allocated */
1404 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1405 !sym->usl.spillLoc->allocreq)
1407 sym->usl.spillLoc->allocreq++;
1410 /* if it does not need or is spilt
1411 or is already assigned to registers
1412 or will not live beyond this instructions */
1415 bitVectBitValue (_G.regAssigned, sym->key) ||
1416 sym->liveTo <= ic->seq)
1419 /* if some liverange has been spilt at the block level
1420 and this one live beyond this block then spil this
1422 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1428 willCS = willCauseSpill (sym->nRegs, sym->regType);
1429 /* if this is a bit variable then don't use precious registers
1430 along with expensive bit-to-char conversions but just spill
1432 if (willCS && SPEC_NOUN(sym->etype) == V_BIT)
1438 /* if trying to allocate this will cause
1439 a spill and there is nothing to spill
1440 or this one is rematerializable then
1442 spillable = computeSpillable (ic);
1443 if (sym->remat || (willCS && bitVectIsZero (spillable)))
1449 /* If the live range preceeds the point of definition
1450 then ideally we must take into account registers that
1451 have been allocated after sym->liveFrom but freed
1452 before ic->seq. This is complicated, so spill this
1453 symbol instead and let fillGaps handle the allocation. */
1454 if (sym->liveFrom < ic->seq)
1460 /* if it has a spillocation & is used less than
1461 all other live ranges then spill this */
1463 if (sym->usl.spillLoc) {
1464 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1465 allLRs, ebbs[i], ic));
1466 if (leastUsed && leastUsed->used > sym->used) {
1471 /* if none of the liveRanges have a spillLocation then better
1472 to spill this one than anything else already assigned to registers */
1473 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1474 /* if this is local to this block then we might find a block spil */
1475 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1483 /* if we need ptr regs for the right side
1485 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1486 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned) PTRSIZE)
1491 /* else we assign registers to it */
1492 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1493 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1495 for (j = 0; j < sym->nRegs; j++)
1497 sym->regs[j] = NULL;
1498 if (sym->regType == REG_PTR)
1499 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1500 else if (sym->regType == REG_BIT)
1501 sym->regs[j] = getRegBit (sym);
1503 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1505 /* if the allocation failed which means
1506 this was spilt then break */
1511 /* if it shares registers with operands make sure
1512 that they are in the same position */
1513 if (!POINTER_SET(ic) && !POINTER_GET(ic))
1515 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1516 OP_SYMBOL (IC_LEFT (ic))->nRegs)
1518 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1519 OP_SYMBOL (IC_LEFT (ic)));
1521 /* do the same for the right operand */
1522 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1523 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1525 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1526 OP_SYMBOL (IC_RIGHT (ic)));
1538 reassignUnusedLRs(unusedLRs);
1541 /* Check for and fix any problems with uninitialized operands */
1542 for (i = 0; i < count; i++)
1546 if (ebbs[i]->noPath &&
1547 (ebbs[i]->entryLabel != entryLabel &&
1548 ebbs[i]->entryLabel != returnLabel))
1551 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1558 verifyRegsAssigned (IC_COND (ic), ic);
1562 if (ic->op == JUMPTABLE)
1564 verifyRegsAssigned (IC_JTCOND (ic), ic);
1568 verifyRegsAssigned (IC_RESULT (ic), ic);
1569 verifyRegsAssigned (IC_LEFT (ic), ic);
1570 verifyRegsAssigned (IC_RIGHT (ic), ic);
1575 /*-----------------------------------------------------------------*/
1576 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1577 /*-----------------------------------------------------------------*/
1578 static void fillGaps()
1582 int loop = 0, change;
1585 if (getenv("DISABLE_FILL_GAPS")) return;
1587 /* First try to do DPTRuse once more since now we know what got into
1590 while (loop++ < 10) {
1593 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1594 sym = hTabNextItem(liveRanges,&key)) {
1595 int size = getSize(sym->type);
1597 if (sym->liveFrom == sym->liveTo) continue;
1599 if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1600 size < 4 && size > 1) {
1602 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1604 /* if this was assigned to registers then */
1605 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1606 /* take it out of the register assigned set */
1607 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1608 } else if (sym->usl.spillLoc) {
1609 sym->usl.spillLoc->allocreq--;
1610 sym->usl.spillLoc = NULL;
1614 sym->isspilt = sym->spillA = 0;
1618 /* try assigning other dptrs */
1619 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1620 /* if this was ssigned to registers then */
1621 if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1622 /* take it out of the register assigned set */
1623 bitVectUnSetBit(_G.totRegAssigned,sym->key);
1624 } else if (sym->usl.spillLoc) {
1625 sym->usl.spillLoc->allocreq--;
1626 sym->usl.spillLoc = NULL;
1629 sym->isspilt = sym->spillA = 0;
1634 /* look for liveranges that were spilt by the allocator */
1635 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1636 sym = hTabNextItem(liveRanges,&key)) {
1641 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1642 if (!sym->uses || !sym->defs) continue ;
1643 /* find the liveRanges this one clashes with, that are
1644 still assigned to registers & mark the registers as used*/
1645 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1649 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1650 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1653 clr = hTabItemWithKey(liveRanges,i);
1656 /* mark these registers as used */
1657 for (k = 0 ; k < clr->nRegs ; k++ )
1658 useReg(clr->regs[k]);
1661 if (willCauseSpill(sym->nRegs,sym->regType)) {
1662 /* NOPE :( clear all registers & and continue */
1667 /* THERE IS HOPE !!!! */
1668 for (i=0; i < sym->nRegs ; i++ ) {
1669 if (sym->regType == REG_PTR)
1670 sym->regs[i] = getRegPtrNoSpil ();
1671 else if (sym->regType == REG_BIT)
1672 sym->regs[i] = getRegBitNoSpil ();
1674 sym->regs[i] = getRegGprNoSpil ();
1677 /* For all its definitions check if the registers
1678 allocated needs positioning NOTE: we can position
1679 only ONCE if more than One positioning required
1681 We may need to perform the checks twice; once to
1682 position the registers as needed, the second to
1683 verify any register repositioning is still
1687 for (pass=0; pass<2; pass++) {
1688 for (i = 0 ; i < sym->defs->size ; i++ ) {
1689 if (bitVectBitValue(sym->defs,i)) {
1691 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1692 if (SKIP_IC(ic)) continue;
1693 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1694 /* if left is assigned to registers */
1695 if (IS_SYMOP(IC_LEFT(ic)) &&
1696 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1697 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1699 if (IS_SYMOP(IC_RIGHT(ic)) &&
1700 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1701 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1703 if (pdone > 1) break;
1706 for (i = 0 ; i < sym->uses->size ; i++ ) {
1707 if (bitVectBitValue(sym->uses,i)) {
1709 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1710 if (SKIP_IC(ic)) continue;
1711 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1713 /* if result is assigned to registers */
1714 if (IS_SYMOP(IC_RESULT(ic)) &&
1715 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1716 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1718 if (pdone > 1) break;
1721 if (pdone == 0) break; /* second pass only if regs repositioned */
1722 if (pdone > 1) break;
1724 /* had to position more than once GIVE UP */
1726 /* UNDO all the changes we made to try this */
1728 for (i=0; i < sym->nRegs ; i++ ) {
1729 sym->regs[i] = NULL;
1732 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1733 "%s in function %s\n",
1734 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1737 D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1738 sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1739 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1740 sym->isspilt = sym->spillA = 0 ;
1741 sym->usl.spillLoc->allocreq--;
1742 sym->usl.spillLoc = NULL;
1750 /*-----------------------------------------------------------------*/
1751 /* findAllBitregs :- returns bit vector of all bit registers */
1752 /*-----------------------------------------------------------------*/
1754 findAllBitregs (void)
1756 bitVect *rmask = newBitVect (ds390_nRegs);
1759 for (j = 0; j < ds390_nRegs; j++)
1761 if (regs390[j].type == REG_BIT)
1762 rmask = bitVectSetBit (rmask, regs390[j].rIdx);
1768 /*-----------------------------------------------------------------*/
1769 /* ds390_allBitregs :- returns bit vector of all bit registers */
1770 /*-----------------------------------------------------------------*/
1772 ds390_allBitregs (void)
1774 return _G.allBitregs;
1777 /*-----------------------------------------------------------------*/
1778 /* rUmaskForOp :- returns register mask for an operand */
1779 /*-----------------------------------------------------------------*/
1781 ds390_rUmaskForOp (operand * op)
1787 /* only temporaries are assigned registers */
1791 sym = OP_SYMBOL (op);
1793 /* if spilt or no registers assigned to it
1795 if (sym->isspilt || !sym->nRegs)
1798 rumask = newBitVect (ds390_nRegs);
1800 for (j = 0; j < sym->nRegs; j++)
1802 rumask = bitVectSetBit (rumask,
1803 sym->regs[j]->rIdx);
1809 /*-----------------------------------------------------------------*/
1810 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1811 /*-----------------------------------------------------------------*/
1813 regsUsedIniCode (iCode * ic)
1815 bitVect *rmask = newBitVect (ds390_nRegs);
1817 /* do the special cases first */
1820 rmask = bitVectUnion (rmask,
1821 ds390_rUmaskForOp (IC_COND (ic)));
1825 /* for the jumptable */
1826 if (ic->op == JUMPTABLE)
1828 rmask = bitVectUnion (rmask,
1829 ds390_rUmaskForOp (IC_JTCOND (ic)));
1834 /* of all other cases */
1836 rmask = bitVectUnion (rmask,
1837 ds390_rUmaskForOp (IC_LEFT (ic)));
1841 rmask = bitVectUnion (rmask,
1842 ds390_rUmaskForOp (IC_RIGHT (ic)));
1845 rmask = bitVectUnion (rmask,
1846 ds390_rUmaskForOp (IC_RESULT (ic)));
1852 /*-----------------------------------------------------------------*/
1853 /* createRegMask - for each instruction will determine the regsUsed */
1854 /*-----------------------------------------------------------------*/
1856 createRegMask (eBBlock ** ebbs, int count)
1860 /* for all blocks */
1861 for (i = 0; i < count; i++)
1865 if (ebbs[i]->noPath &&
1866 (ebbs[i]->entryLabel != entryLabel &&
1867 ebbs[i]->entryLabel != returnLabel))
1870 /* for all instructions */
1871 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1876 if (SKIP_IC2 (ic) || !ic->rlive)
1879 /* first mark the registers used in this
1881 ic->rUsed = regsUsedIniCode (ic);
1882 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1884 /* now create the register mask for those
1885 registers that are in use : this is a
1886 super set of ic->rUsed */
1887 ic->rMask = newBitVect (ds390_nRegs + 1);
1889 /* for all live Ranges alive at this point */
1890 for (j = 1; j < ic->rlive->size; j++)
1895 /* if not alive then continue */
1896 if (!bitVectBitValue (ic->rlive, j))
1899 /* find the live range we are interested in */
1900 if (!(sym = hTabItemWithKey (liveRanges, j)))
1902 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1903 "createRegMask cannot find live range");
1904 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1908 /* special case for ruonly */
1909 if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1910 int size = getSize(sym->type);
1912 for (k = 0 ; k < size; k++ )
1913 ic->rMask = bitVectSetBit (ic->rMask, j++);
1917 /* if no register assigned to it */
1918 if (!sym->nRegs || sym->isspilt)
1921 /* for all the registers allocated to it */
1922 for (k = 0; k < sym->nRegs; k++)
1925 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1931 /*-----------------------------------------------------------------*/
1932 /* rematStr - returns the rematerialized string for a remat var */
1933 /*-----------------------------------------------------------------*/
1935 rematStr (symbol * sym)
1937 iCode *ic = sym->rematiCode;
1942 /* if plus adjust offset to right hand side */
1945 offset += (int) operandLitValue (IC_RIGHT (ic));
1946 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1950 /* if minus adjust offset to right hand side */
1953 offset -= (int) operandLitValue (IC_RIGHT (ic));
1954 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1958 /* cast then continue */
1959 if (IS_CAST_ICODE(ic)) {
1960 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1963 /* we reached the end */
1969 SNPRINTF (buffer, sizeof(buffer),
1971 OP_SYMBOL (IC_LEFT (ic))->rname,
1972 offset >= 0 ? '+' : '-',
1973 abs (offset) & 0xffff);
1977 strncpyz (buffer, OP_SYMBOL (IC_LEFT (ic))->rname, sizeof(buffer));
1982 /*-----------------------------------------------------------------*/
1983 /* regTypeNum - computes the type & number of registers required */
1984 /*-----------------------------------------------------------------*/
1992 /* for each live range do */
1993 for (sym = hTabFirstItem (liveRanges, &k); sym;
1994 sym = hTabNextItem (liveRanges, &k))
1997 /* if used zero times then no registers needed */
1998 if ((sym->liveTo - sym->liveFrom) == 0)
2002 /* if the live range is a temporary */
2006 /* if the type is marked as a conditional */
2007 if (sym->regType == REG_CND)
2010 /* if used in return only then we don't
2012 if (sym->ruonly || sym->accuse)
2014 if (IS_AGGREGATE (sym->type) || sym->isptr)
2015 sym->type = aggrToPtr (sym->type, FALSE);
2019 /* if the symbol has only one definition &
2020 that definition is a get_pointer */
2021 if (bitVectnBitsOn (sym->defs) == 1 &&
2022 (ic = hTabItemWithKey (iCodehTab,
2023 bitVectFirstBit (sym->defs))) &&
2025 !IS_BITVAR (sym->etype) &&
2026 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
2029 if (ptrPseudoSymSafe (sym, ic))
2031 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
2035 /* if in data space or idata space then try to
2036 allocate pointer register */
2040 /* if not then we require registers */
2041 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
2042 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
2043 getSize (sym->type));
2047 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
2048 printTypeChain (sym->type, stderr);
2049 fprintf (stderr, "\n");
2052 /* determine the type of register required */
2053 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
2054 sym->regType = REG_PTR;
2055 else if (IS_BIT(sym->type))
2056 sym->regType = REG_BIT;
2058 sym->regType = REG_GPR;
2061 /* for the first run we don't provide */
2062 /* registers for true symbols we will */
2063 /* see how things go */
2069 /*-----------------------------------------------------------------*/
2070 /* freeAllRegs - mark all registers as free */
2071 /*-----------------------------------------------------------------*/
2077 for (i = 0; i < ds390_nRegs; i++)
2078 regs390[i].isFree = 1;
2080 for (i = B0_IDX; i < ds390_nBitRegs; i++)
2081 regs390[i].isFree = 1;
2084 /*-----------------------------------------------------------------*/
2085 /* deallocStackSpil - this will set the stack pointer back */
2086 /*-----------------------------------------------------------------*/
2088 DEFSETFUNC (deallocStackSpil)
2096 /*-----------------------------------------------------------------*/
2097 /* farSpacePackable - returns the packable icode for far variables */
2098 /*-----------------------------------------------------------------*/
2100 farSpacePackable (iCode * ic)
2104 /* go thru till we find a definition for the
2105 symbol on the right */
2106 for (dic = ic->prev; dic; dic = dic->prev)
2108 /* if the definition is a call then no */
2109 if ((dic->op == CALL || dic->op == PCALL) &&
2110 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2115 /* if shift by unknown amount then not */
2116 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2117 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2120 /* if pointer get and size > 1 */
2121 if (POINTER_GET (dic) &&
2122 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2125 if (POINTER_SET (dic) &&
2126 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2129 /* if any tree is a true symbol in far space */
2130 if (IC_RESULT (dic) &&
2131 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2132 isOperandInFarSpace (IC_RESULT (dic)))
2135 if (IC_RIGHT (dic) &&
2136 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2137 isOperandInFarSpace (IC_RIGHT (dic)) &&
2138 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2141 if (IC_LEFT (dic) &&
2142 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2143 isOperandInFarSpace (IC_LEFT (dic)) &&
2144 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2147 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2149 if ((dic->op == LEFT_OP ||
2150 dic->op == RIGHT_OP ||
2152 IS_OP_LITERAL (IC_RIGHT (dic)))
2162 /*-----------------------------------------------------------------*/
2163 /* packRegsForAssign - register reduction for assignment */
2164 /*-----------------------------------------------------------------*/
2166 packRegsForAssign (iCode * ic, eBBlock * ebp)
2170 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2171 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2172 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2177 /* if the true symbol is defined in far space or on stack
2178 then we should not since this will increase register pressure */
2180 if (isOperandInFarSpace (IC_RESULT (ic)))
2182 if ((dic = farSpacePackable (ic)))
2188 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2193 /* find the definition of iTempNN scanning backwards if we find a
2194 a use of the true symbol in before we find the definition then
2196 for (dic = ic->prev; dic; dic = dic->prev)
2198 /* if there is a function call then don't pack it */
2199 if ((dic->op == CALL || dic->op == PCALL))
2208 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2209 IS_OP_VOLATILE (IC_RESULT (dic)))
2215 if (IS_SYMOP (IC_RESULT (dic)) &&
2216 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2218 if (POINTER_SET (dic))
2224 if (IS_SYMOP (IC_RIGHT (dic)) &&
2225 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2226 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2232 if (IS_SYMOP (IC_LEFT (dic)) &&
2233 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2234 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2240 if (POINTER_SET (dic) &&
2241 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2249 return 0; /* did not find */
2251 /* if assignment then check that right is not a bit */
2252 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2254 sym_link *etype = operandType (IC_RESULT (dic));
2255 if (IS_BITFIELD (etype))
2257 /* if result is a bit too then it's ok */
2258 etype = operandType (IC_RESULT (ic));
2259 if (!IS_BITFIELD (etype))
2265 /* if the result is on stack or iaccess then it must be
2266 the same atleast one of the operands */
2267 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2268 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2271 /* the operation has only one symbol
2272 operator then we can pack */
2273 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2274 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2277 if (!((IC_LEFT (dic) &&
2278 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2280 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2284 /* found the definition */
2285 /* replace the result with the result of */
2286 /* this assignment and remove this assignment */
2287 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2289 IC_RESULT (dic) = IC_RESULT (ic);
2291 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2293 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2295 /* delete from liverange table also
2296 delete from all the points inbetween and the new
2298 for (sic = dic; sic != ic; sic = sic->next)
2300 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2301 if (IS_ITEMP (IC_RESULT (dic)))
2302 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2305 remiCodeFromeBBlock (ebp, ic);
2306 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2307 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2308 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2312 /*------------------------------------------------------------------*/
2313 /* findAssignToSym : scanning backwards looks for first assig found */
2314 /*------------------------------------------------------------------*/
2316 findAssignToSym (operand * op, iCode * ic)
2320 /* This routine is used to find sequences like
2322 ...; (intervening ops don't use iTempAA or modify FOO)
2323 blah = blah + iTempAA;
2325 and eliminate the use of iTempAA, freeing up its register for
2329 for (dic = ic->prev; dic; dic = dic->prev)
2332 /* if definition by assignment */
2333 if (dic->op == '=' &&
2334 !POINTER_SET (dic) &&
2335 IC_RESULT (dic)->key == op->key
2336 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2340 /* we are interested only if defined in far space */
2341 /* or in stack space in case of + & - */
2343 /* if assigned to a non-symbol then return
2345 if (!IS_SYMOP (IC_RIGHT (dic)))
2348 /* if the symbol is in far space then we should not */
2349 if (isOperandInFarSpace (IC_RIGHT (dic)))
2352 /* for + & - operations make sure that
2353 if it is on the stack it is the same
2354 as one of the three operands */
2355 if ((ic->op == '+' || ic->op == '-') &&
2356 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2359 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2360 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2361 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2369 /* if we find an usage then we cannot delete it */
2370 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2373 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2376 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2380 /* now make sure that the right side of dic
2381 is not defined between ic & dic */
2384 iCode *sic = dic->next;
2386 for (; sic != ic; sic = sic->next)
2387 if (IC_RESULT (sic) &&
2388 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2397 /*-----------------------------------------------------------------*/
2398 /* packRegsForSupport :- reduce some registers for support calls */
2399 /*-----------------------------------------------------------------*/
2401 packRegsForSupport (iCode * ic, eBBlock * ebp)
2405 /* for the left & right operand :- look to see if the
2406 left was assigned a true symbol in far space in that
2407 case replace them */
2408 if (IS_ITEMP (IC_LEFT (ic)) &&
2409 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2411 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2417 /* found it we need to remove it from the
2419 for (sic = dic; sic != ic; sic = sic->next) {
2420 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2421 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2424 wassert(IS_SYMOP(IC_LEFT (ic)));
2425 wassert(IS_SYMOP(IC_RIGHT (dic)));
2426 IC_LEFT (ic)->operand.symOperand =
2427 IC_RIGHT (dic)->operand.symOperand;
2428 OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2429 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2430 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2431 remiCodeFromeBBlock (ebp, dic);
2432 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2436 /* do the same for the right operand */
2439 IS_ITEMP (IC_RIGHT (ic)) &&
2440 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2442 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2448 /* if this is a subtraction & the result
2449 is a true symbol in far space then don't pack */
2450 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2452 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2453 if (IN_FARSPACE (SPEC_OCLS (etype)))
2456 /* found it we need to remove it from the
2458 for (sic = dic; sic != ic; sic = sic->next) {
2459 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2460 sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2463 wassert(IS_SYMOP(IC_RIGHT (ic)));
2464 wassert(IS_SYMOP(IC_RIGHT (dic)));
2465 IC_RIGHT (ic)->operand.symOperand =
2466 IC_RIGHT (dic)->operand.symOperand;
2467 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2468 OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2469 remiCodeFromeBBlock (ebp, dic);
2470 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2471 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2479 /*-----------------------------------------------------------------*/
2480 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2481 /*-----------------------------------------------------------------*/
2482 static int packRegsDPTRnuse( operand *op , unsigned dptr)
2487 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2488 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2491 /* first check if any overlapping liverange has already been
2492 assigned to this DPTR */
2493 if (OP_SYMBOL(op)->clashes) {
2494 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2496 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2497 sym = hTabItemWithKey(liveRanges,i);
2498 if (sym->dptr == dptr) return 0;
2503 /* future for more dptrs */
2505 OP_SYMBOL(op)->dptr = dptr;
2509 /* DPTR1 is special since it is also used as a scratch by the backend .
2510 so we walk thru the entire live range of this operand and make sure
2511 DPTR1 will not be used by the backed . The logic here is to find out if
2512 more than one operand in an icode is in far space then we give up : we
2513 don't keep it live across functions for now
2516 ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2517 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2518 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2521 if (ic->op == CALL || ic->op == PCALL) return 0;
2523 /* single operand icode are ok */
2524 if (ic->op == IFX || ic->op == IPUSH)
2527 if (ic->op == SEND ) {
2528 if (ic->argreg != 1 ) return 0;
2531 /* two special cases first */
2532 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) && /* pointer get */
2533 !OP_SYMBOL(IC_LEFT(ic))->ruonly && /* with result in far space */
2534 (isOperandInFarSpace(IC_RESULT(ic)) &&
2535 !isOperandInReg(IC_RESULT(ic)))) {
2539 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) && /* pointer set */
2540 !OP_SYMBOL(IC_RESULT(ic))->ruonly && /* with right in far space */
2541 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2542 !isOperandInReg(IC_RIGHT(ic)))) {
2546 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && /* if symbol operand */
2547 !isOperandEqual(IC_RESULT(ic),op) && /* not the same as this */
2548 ((isOperandInFarSpace(IC_RESULT(ic)) || /* in farspace or */
2549 OP_SYMBOL(IC_RESULT(ic))->onStack) && /* on the stack */
2550 !isOperandInReg(IC_RESULT(ic)))) { /* and not in register */
2554 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && /* if symbol operand */
2555 !isOperandEqual(IC_LEFT(ic),op) && /* not the same as this */
2556 ((isOperandInFarSpace(IC_LEFT(ic)) || /* in farspace or */
2557 OP_SYMBOL(IC_LEFT(ic))->onStack) && /* on the stack */
2558 !isOperandInReg(IC_LEFT(ic)))) { /* and not in register */
2561 /* same for right */
2562 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && /* if symbol operand */
2563 !isOperandEqual(IC_RIGHT(ic),op) && /* not the same as this */
2564 ((isOperandInFarSpace(IC_RIGHT(ic)) || /* in farspace or */
2565 OP_SYMBOL(IC_RIGHT(ic))->onStack) && /* on the stack */
2566 !isOperandInReg(IC_RIGHT(ic)))) { /* and not in register */
2570 // Check that no other ops in this range have been assigned to dptr1.
2571 // I don't understand why this is not caught by the first check, above.
2572 // But it isn't always, see bug 769624.
2573 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2574 (OP_SYMBOL(IC_RESULT(ic))->dptr == 1))
2576 //fprintf(stderr, "dptr1 already in use in live range #1\n");
2580 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2581 (OP_SYMBOL(IC_LEFT(ic))->dptr == 1))
2583 //fprintf(stderr, "dptr1 already in use in live range # 2\n");
2587 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2588 (OP_SYMBOL(IC_RIGHT(ic))->dptr == 1))
2590 //fprintf(stderr, "dptr1 already in use in live range # 3\n");
2594 if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2595 OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2597 if (nfs > 1) return 0;
2599 OP_SYMBOL(op)->dptr = dptr;
2603 /*-----------------------------------------------------------------*/
2604 /* packRegsDPTRuse : - will reduce some registers for single Use */
2605 /*-----------------------------------------------------------------*/
2607 packRegsDPTRuse (operand * op)
2609 /* go thru entire liveRange of this variable & check for
2610 other possible usage of DPTR , if we don't find it the
2611 assign this to DPTR (ruonly)
2616 sym_link *type, *etype;
2618 if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2619 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2621 /* first check if any overlapping liverange has already been
2623 if (OP_SYMBOL(op)->clashes) {
2624 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2625 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2626 sym = hTabItemWithKey(liveRanges,i);
2627 if (sym->ruonly) return NULL ;
2632 /* no then go thru this guys live range */
2633 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2634 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2635 ic = hTabNextItem(iCodeSeqhTab,&key)) {
2637 if (SKIP_IC3(ic)) continue;
2639 /* if PCALL cannot be sure give up */
2640 if (ic->op == PCALL) return NULL;
2642 /* if SEND & not the first parameter then giveup */
2643 if (ic->op == SEND && ic->argreg != 1 &&
2644 ((isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) ||
2645 isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2647 /* if CALL then make sure it is VOID || return value not used
2648 or the return value is assigned to this one */
2649 if (ic->op == CALL) {
2650 if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2651 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2652 etype = getSpec(type = operandType(IC_RESULT(ic)));
2653 if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2658 /* special case of add with a [remat] */
2659 if (ic->op == '+' &&
2660 OP_SYMBOL(IC_LEFT(ic))->remat &&
2661 (isOperandInFarSpace(IC_RIGHT(ic)) &&
2662 !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2666 if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2667 getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2670 if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2671 getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2673 /* conditionals can destroy 'b' - make sure B wont
2674 be used in this one*/
2675 if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/' ||
2676 ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2677 getSize(operandType(op)) > 3) return NULL;
2679 /* if this is a cast to a bigger type */
2681 if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2682 getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2683 getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2689 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2690 !isOperandEqual(IC_RESULT(ic),op) &&
2691 ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2692 !isOperandInReg(IC_RESULT(ic))) ||
2693 OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2695 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2696 !isOperandEqual(IC_RIGHT(ic),op) &&
2697 (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2698 IS_TRUE_SYMOP(IC_RIGHT(ic)) ||
2699 OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2700 ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2701 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2703 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2704 !isOperandEqual(IC_LEFT(ic),op) &&
2705 (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2706 IS_TRUE_SYMOP(IC_LEFT(ic)) ||
2707 OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2708 ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2709 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2711 if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2712 IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2713 (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2714 (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2717 OP_SYMBOL(op)->ruonly = 1;
2718 if (OP_SYMBOL(op)->usl.spillLoc) {
2719 if (OP_SYMBOL(op)->spillA)
2720 OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2721 OP_SYMBOL(op)->usl.spillLoc = NULL;
2726 /*-----------------------------------------------------------------*/
2727 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2728 /*-----------------------------------------------------------------*/
2730 isBitwiseOptimizable (iCode * ic)
2732 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2733 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2735 /* bitwise operations are considered optimizable
2736 under the following conditions (Jean-Louis VERN)
2748 if (IS_LITERAL (rtype) ||
2749 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2755 /*-----------------------------------------------------------------*/
2756 /* packRegsForAccUse - pack registers for acc use */
2757 /*-----------------------------------------------------------------*/
2759 packRegsForAccUse (iCode * ic)
2763 /* if this is an aggregate, e.g. a one byte char array */
2764 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2768 /* if we are calling a reentrant function that has stack parameters */
2769 if (ic->op == CALL &&
2770 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2771 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2774 if (ic->op == PCALL &&
2775 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2776 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2779 /* if + or - then it has to be one byte result */
2780 if ((ic->op == '+' || ic->op == '-')
2781 && getSize (operandType (IC_RESULT (ic))) > 1)
2784 /* if shift operation make sure right side is not a literal */
2785 if (ic->op == RIGHT_OP &&
2786 (isOperandLiteral (IC_RIGHT (ic)) ||
2787 getSize (operandType (IC_RESULT (ic))) > 1))
2790 if (ic->op == LEFT_OP &&
2791 (isOperandLiteral (IC_RIGHT (ic)) ||
2792 getSize (operandType (IC_RESULT (ic))) > 1))
2795 if (IS_BITWISE_OP (ic) &&
2796 getSize (operandType (IC_RESULT (ic))) > 1)
2800 /* has only one definition */
2801 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2804 /* has only one use */
2805 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2808 /* and the usage immediately follows this iCode */
2809 if (!(uic = hTabItemWithKey (iCodehTab,
2810 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2813 if (ic->next != uic)
2816 /* if it is a conditional branch then we definitely can */
2820 if (uic->op == JUMPTABLE)
2823 /* if the usage is not is an assignment
2824 or an arithmetic / bitwise / shift operation then not */
2825 if (POINTER_SET (uic) &&
2826 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2829 if (uic->op != '=' &&
2830 !IS_ARITHMETIC_OP (uic) &&
2831 !IS_BITWISE_OP (uic) &&
2832 uic->op != LEFT_OP &&
2833 uic->op != RIGHT_OP)
2836 /* if used in ^ operation then make sure right is not a
2838 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2841 /* if shift operation make sure right side is not a literal */
2842 if (uic->op == RIGHT_OP &&
2843 (isOperandLiteral (IC_RIGHT (uic)) ||
2844 getSize (operandType (IC_RESULT (uic))) > 1))
2847 if (uic->op == LEFT_OP &&
2848 (isOperandLiteral (IC_RIGHT (uic)) ||
2849 getSize (operandType (IC_RESULT (uic))) > 1))
2852 /* make sure that the result of this icode is not on the
2853 stack, since acc is used to compute stack offset */
2854 if (isOperandOnStack(IC_RESULT(uic)))
2857 /* if either one of them in far space then we cannot */
2858 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2859 isOperandInFarSpace (IC_LEFT (uic))) ||
2860 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2861 isOperandInFarSpace (IC_RIGHT (uic))))
2864 /* if the usage has only one operand then we can */
2865 if (IC_LEFT (uic) == NULL ||
2866 IC_RIGHT (uic) == NULL)
2869 /* make sure this is on the left side if not
2870 a '+' since '+' is commutative */
2871 if (ic->op != '+' &&
2872 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2875 /* if the other one is not on stack then we can */
2876 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2877 (IS_ITEMP (IC_RIGHT (uic)) ||
2878 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2879 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2882 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2883 (IS_ITEMP (IC_LEFT (uic)) ||
2884 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2885 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2891 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2895 /*-----------------------------------------------------------------*/
2896 /* packForPush - heuristics to reduce iCode for pushing */
2897 /*-----------------------------------------------------------------*/
2899 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2903 struct eBBlock * ebp = ebpp[blockno];
2905 if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2908 /* must have only definition & one usage */
2909 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2910 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2913 /* find the definition */
2914 if (!(dic = hTabItemWithKey (iCodehTab,
2915 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2918 if (dic->op != '=' || POINTER_SET (dic))
2921 if (dic->eBBlockNum != ic->eBBlockNum) return ;
2923 /* make sure the right side does not have any definitions
2925 dbv = OP_DEFS(IC_RIGHT(dic));
2926 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2927 if (bitVectBitValue (dbv, lic->key))
2930 /* make sure they have the same type */
2931 if (IS_SPEC(operandType(IC_LEFT(ic))))
2933 sym_link *itype=operandType(IC_LEFT(ic));
2934 sym_link *ditype=operandType(IC_RIGHT(dic));
2936 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2937 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2940 /* extend the live range of replaced operand if needed */
2941 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2942 OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2943 OP_SYMBOL(IC_RIGHT(dic))->clashes =
2944 bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2945 OP_SYMBOL(IC_LEFT(ic))->clashes);
2947 for (lic = ic; lic && lic != dic; lic = lic->prev)
2949 bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2950 if (IS_ITEMP (IC_RIGHT (dic)))
2951 bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2953 /* we now we know that it has one & only one def & use
2954 and the that the definition is an assignment */
2955 IC_LEFT (ic) = IC_RIGHT (dic);
2957 remiCodeFromeBBlock (ebp, dic);
2958 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2959 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2962 /*-----------------------------------------------------------------*/
2963 /* packRegisters - does some transformations to reduce register */
2965 /*-----------------------------------------------------------------*/
2967 packRegisters (eBBlock ** ebpp, int blockno)
2971 eBBlock *ebp = ebpp[blockno];
2977 /* look for assignments of the form */
2978 /* iTempNN = TRueSym (someoperation) SomeOperand */
2980 /* TrueSym := iTempNN:1 */
2981 for (ic = ebp->sch; ic; ic = ic->next)
2983 /* find assignment of the form TrueSym := iTempNN:1 */
2984 if (ic->op == '=' && !POINTER_SET (ic))
2985 change += packRegsForAssign (ic, ebp);
2992 for (ic = ebp->sch; ic; ic = ic->next)
2994 /* Fix for bug #979599: */
2997 /* Look for two subsequent iCodes with */
2999 /* _c = iTemp & op; */
3000 /* and replace them by */
3003 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
3005 ic->prev->op == '=' &&
3006 IS_ITEMP (IC_LEFT (ic)) &&
3007 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
3008 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
3010 iCode* ic_prev = ic->prev;
3011 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
3013 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
3014 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
3016 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
3017 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
3018 prev_result_sym->liveTo == ic->seq)
3020 prev_result_sym->liveTo = ic_prev->seq;
3023 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
3025 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
3027 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
3029 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
3030 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
3031 remiCodeFromeBBlock (ebp, ic_prev);
3032 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
3036 /* if this is an itemp & result of an address of a true sym
3037 then mark this as rematerialisable */
3038 if (ic->op == ADDRESS_OF &&
3039 IS_ITEMP (IC_RESULT (ic)) &&
3040 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
3041 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3042 !OP_SYMBOL (IC_LEFT (ic))->onStack)
3045 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3046 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3047 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3051 /* if this is an itemp & used as a pointer
3052 & assigned to a literal then remat */
3053 if (IS_ASSIGN_ICODE(ic) &&
3054 IS_ITEMP(IC_RESULT(ic)) &&
3055 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3056 isOperandLiteral(IC_RIGHT(ic)))
3058 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3059 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3060 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3063 /* if straight assignment then carry remat flag if
3064 this is the only definition */
3065 if (ic->op == '=' &&
3066 !POINTER_SET (ic) &&
3067 IS_SYMOP (IC_RIGHT (ic)) &&
3068 OP_SYMBOL (IC_RIGHT (ic))->remat &&
3069 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
3070 !isOperandGlobal(IC_RESULT(ic)) && /* due to bug 1618050 */
3071 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
3073 OP_SYMBOL (IC_RESULT (ic))->remat =
3074 OP_SYMBOL (IC_RIGHT (ic))->remat;
3075 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
3076 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
3079 /* if cast to a generic pointer & the pointer being
3080 cast is remat, then we can remat this cast as well */
3081 if (ic->op == CAST &&
3082 IS_SYMOP(IC_RIGHT(ic)) &&
3083 !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
3084 OP_SYMBOL(IC_RIGHT(ic))->remat )
3086 sym_link *to_type = operandType(IC_LEFT(ic));
3087 sym_link *from_type = operandType(IC_RIGHT(ic));
3088 if (IS_GENPTR(to_type) && IS_PTR(from_type))
3090 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3091 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3092 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3096 /* if this is a +/- operation with a rematerizable
3097 then mark this as rematerializable as well */
3098 if ((ic->op == '+' || ic->op == '-') &&
3099 (IS_SYMOP (IC_LEFT (ic)) &&
3100 IS_ITEMP (IC_RESULT (ic)) &&
3101 OP_SYMBOL (IC_LEFT (ic))->remat &&
3102 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3103 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3104 IS_OP_LITERAL (IC_RIGHT (ic))))
3107 //int i = operandLitValue(IC_RIGHT(ic));
3108 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3109 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3110 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3113 /* mark the pointer usages */
3114 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic)))
3115 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3117 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic)))
3118 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3120 if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
3121 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3123 if (ic->op == RECEIVE && ic->argreg == 1 &&
3124 IS_SYMOP (IC_RESULT (ic)) &&
3125 getSize (operandType(IC_RESULT(ic))) <= 3)
3126 OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
3128 if (ic->op == SEND && ic->argreg == 1 &&
3129 IS_SYMOP(IC_LEFT(ic)) &&
3130 getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
3131 OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
3135 /* if we are using a symbol on the stack
3136 then we should say ds390_ptrRegReq */
3137 if (options.useXstack && ic->parmPush
3138 && (ic->op == IPUSH || ic->op == IPOP))
3140 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3141 ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
3142 OP_SYMBOL (IC_COND (ic))->iaccess +
3143 (SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata));
3144 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3145 ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
3146 OP_SYMBOL (IC_JTCOND (ic))->iaccess +
3147 (SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata));
3150 if (IS_SYMOP (IC_LEFT (ic)))
3151 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
3152 OP_SYMBOL (IC_LEFT (ic))->iaccess +
3153 (SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata));
3154 if (IS_SYMOP (IC_RIGHT (ic)))
3155 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
3156 OP_SYMBOL (IC_RIGHT (ic))->iaccess +
3157 (SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata));
3158 if (IS_SYMOP (IC_RESULT (ic)))
3159 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
3160 OP_SYMBOL (IC_RESULT (ic))->iaccess +
3161 (SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata));
3165 /* if the condition of an if instruction
3166 is defined in the previous instruction and
3167 this is the only usage then
3168 mark the itemp as a conditional */
3169 if ((IS_CONDITIONAL (ic) ||
3170 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3171 ic->next && ic->next->op == IFX &&
3172 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3173 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3174 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3176 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3180 /* reduce for support function calls */
3181 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3182 packRegsForSupport (ic, ebp);
3184 /* some cases the redundant moves can
3185 can be eliminated for return statements . Can be elminated for the first SEND */
3186 if ((ic->op == RETURN ||
3187 ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
3188 !isOperandInFarSpace (IC_LEFT (ic)) &&
3191 packRegsDPTRuse (IC_LEFT (ic));
3194 if (ic->op == CALL) {
3195 sym_link *ftype = operandType(IC_LEFT(ic));
3196 if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
3197 !IFFUNC_ISBUILTIN(ftype)) {
3198 packRegsDPTRuse (IC_RESULT (ic));
3202 /* if pointer set & left has a size more than
3203 one and right is not in far space */
3204 if (POINTER_SET (ic) &&
3205 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3206 IS_SYMOP (IC_RESULT (ic)) &&
3207 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3208 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3209 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
3211 packRegsDPTRuse (IC_RESULT (ic));
3214 /* if pointer get */
3215 if (POINTER_GET (ic) &&
3216 !isOperandInFarSpace (IC_RESULT (ic)) &&
3217 IS_SYMOP (IC_LEFT (ic)) &&
3218 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3219 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3220 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
3222 packRegsDPTRuse (IC_LEFT (ic));
3225 /* if this is a cast for intergral promotion then
3226 check if it's the only use of the definition of the
3227 operand being casted/ if yes then replace
3228 the result of that arithmetic operation with
3229 this result and get rid of the cast */
3232 sym_link *fromType = operandType (IC_RIGHT (ic));
3233 sym_link *toType = operandType (IC_LEFT (ic));
3235 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3236 getSize (fromType) != getSize (toType) &&
3237 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3240 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3243 if (IS_ARITHMETIC_OP (dic))
3245 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3246 IC_RESULT (dic) = IC_RESULT (ic);
3247 remiCodeFromeBBlock (ebp, ic);
3248 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3249 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3250 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3254 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3260 /* if the type from and type to are the same
3261 then if this is the only use then packit */
3262 if (compareType (operandType (IC_RIGHT (ic)),
3263 operandType (IC_LEFT (ic))) == 1)
3265 iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3268 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3269 IC_RESULT (dic) = IC_RESULT (ic);
3270 remiCodeFromeBBlock (ebp, ic);
3271 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3272 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3273 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3281 iTempNN := (some variable in farspace) V1
3286 if (ic->op == IPUSH || ic->op == SEND)
3288 packForPush (ic, ebpp, blockno);
3292 /* pack registers for accumulator use, when the
3293 result of an arithmetic or bit wise operation
3294 has only one use, that use is immediately following
3295 the defintion and the using iCode has only one
3296 operand or has two operands but one is literal &
3297 the result of that operation is not on stack then
3298 we can leave the result of this operation in acc:b
3300 if ((IS_ARITHMETIC_OP (ic)
3301 || IS_CONDITIONAL(ic)
3302 || IS_BITWISE_OP (ic)
3303 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3304 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3306 IS_ITEMP (IC_RESULT (ic)) &&
3307 getSize (operandType (IC_RESULT (ic))) <= 2)
3309 packRegsForAccUse (ic);
3313 /*-----------------------------------------------------------------*/
3314 /* assignRegisters - assigns registers to each live range as need */
3315 /*-----------------------------------------------------------------*/
3317 ds390_assignRegisters (ebbIndex * ebbi)
3319 eBBlock ** ebbs = ebbi->bbOrder;
3320 int count = ebbi->count;
3324 setToNull ((void *) &_G.funcrUsed);
3325 setToNull ((void *) &_G.regAssigned);
3326 setToNull ((void *) &_G.totRegAssigned);
3327 setToNull ((void *) &_G.funcrUsed);
3328 ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3329 if ((currFunc && IFFUNC_ISREENT (currFunc->type)) || options.stackAuto)
3337 ds390_nRegs = 12 + ds390_nBitRegs;
3338 _G.allBitregs = findAllBitregs ();
3340 if (options.model != MODEL_FLAT24)
3341 options.stack10bit = 0;
3342 /* change assignments this will remove some
3343 live ranges reducing some register pressure */
3344 for (i = 0; i < count; i++)
3345 packRegisters (ebbs, i);
3347 /* liveranges probably changed by register packing
3348 so we compute them again */
3349 recomputeLiveRanges (ebbs, count);
3351 if (options.dump_pack)
3352 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3354 /* first determine for each live range the number of
3355 registers & the type of registers required for each */
3358 /* and serially allocate registers */
3359 serialRegAssign (ebbs, count);
3364 ds390_nRegs = 12 + ds390_nBitRegs;
3366 /* if stack was extended then tell the user */
3369 /* werror(W_TOOMANY_SPILS,"stack", */
3370 /* _G.stackExtend,currFunc->name,""); */
3376 /* werror(W_TOOMANY_SPILS,"data space", */
3377 /* _G.dataExtend,currFunc->name,""); */
3381 /* after that create the register mask
3382 for each of the instruction */
3383 createRegMask (ebbs, count);
3385 /* redo that offsets for stacked automatic variables */
3387 redoStackOffsets ();
3389 /* make sure r0 & r1 are flagged as used if they might be used */
3391 if (currFunc && ds390_ptrRegReq)
3393 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3394 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3397 if (options.dump_rassgn)
3399 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3400 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3403 /* do the overlaysegment stuff SDCCmem.c */
3404 doOverlays (ebbs, count);
3406 /* now get back the chain */
3407 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3411 /* free up any _G.stackSpil locations allocated */
3412 applyToSet (_G.stackSpil, deallocStackSpil);
3414 setToNull ((void *) &_G.stackSpil);
3415 setToNull ((void *) &_G.spiltSet);
3416 /* mark all registers as free */