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 /*-----------------------------------------------------------------*/
39 extern void genhc08Code (iCode *);
48 bitVect *totRegAssigned; /* final set of LRs that got into registers */
51 bitVect *funcrUsed; /* registers used in a function */
57 /* Shared with gen.c */
58 int hc08_ptrRegReq; /* one byte pointer register required */
64 {REG_GPR, A_IDX, REG_GPR, "a", "a", "0", 1, NULL, 0, 1},
65 {REG_GPR, X_IDX, REG_GPR, "x", "x", "0", 2, NULL, 0, 1},
66 {REG_GPR, H_IDX, REG_GPR, "h", "h", "0", 4, NULL, 0, 1},
67 {REG_PTR, HX_IDX, REG_PTR, "hx", "hx", "0", 6, NULL, 0, 1},
68 {REG_GPR, XA_IDX, REG_GPR, "xa", "xa", "0", 3, NULL, 0, 1},
70 {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, NULL, 0, 1},
80 static void spillThis (symbol *);
81 static void freeAllRegs ();
83 /*-----------------------------------------------------------------*/
84 /* allocReg - allocates register of given type */
85 /*-----------------------------------------------------------------*/
91 if ((type==REG_PTR) && (regshc08[HX_IDX].isFree))
93 regshc08[HX_IDX].isFree = 0;
96 bitVectSetBit (currFunc->regsUsed, HX_IDX);
97 return ®shc08[HX_IDX];
102 /*-----------------------------------------------------------------*/
103 /* hc08_regWithIdx - returns pointer to register wit index number */
104 /*-----------------------------------------------------------------*/
106 hc08_regWithIdx (int idx)
110 for (i = 0; i < hc08_nRegs; i++)
111 if (regshc08[i].rIdx == idx)
114 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
115 "regWithIdx not found");
119 /*-----------------------------------------------------------------*/
120 /* hc08_freeReg - frees a register */
121 /*-----------------------------------------------------------------*/
123 hc08_freeReg (regs * reg)
127 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
128 "hc08_freeReg - Freeing NULL register");
137 if (hc08_reg_x->isFree)
138 hc08_reg_xa->isFree = 1;
141 if (hc08_reg_a->isFree)
142 hc08_reg_xa->isFree = 1;
143 if (hc08_reg_h->isFree)
144 hc08_reg_hx->isFree = 1;
147 if (hc08_reg_x->isFree)
148 hc08_reg_hx->isFree = 1;
151 hc08_reg_h->isFree = 1;
152 hc08_reg_x->isFree = 1;
153 if (hc08_reg_a->isFree)
154 hc08_reg_xa->isFree = 1;
157 hc08_reg_x->isFree = 1;
158 hc08_reg_a->isFree = 1;
159 if (hc08_reg_h->isFree)
160 hc08_reg_hx->isFree = 1;
168 /*-----------------------------------------------------------------*/
169 /* nFreeRegs - returns number of free registers */
170 /*-----------------------------------------------------------------*/
179 for (i = 0; i < hc08_nRegs; i++)
180 if (regshc08[i].isFree && regshc08[i].type == type)
185 /*-----------------------------------------------------------------*/
186 /* nfreeRegsType - free registers with type */
187 /*-----------------------------------------------------------------*/
189 nfreeRegsType (int type)
194 if ((nfr = nFreeRegs (type)) == 0)
195 return nFreeRegs (REG_GPR);
198 return nFreeRegs (type);
201 /*-----------------------------------------------------------------*/
202 /* hc08_useReg - marks a register as used */
203 /*-----------------------------------------------------------------*/
205 hc08_useReg (regs * reg)
212 hc08_reg_xa->aop = NULL;
213 hc08_reg_xa->isFree = 0;
216 hc08_reg_xa->aop = NULL;
217 hc08_reg_xa->isFree = 0;
218 hc08_reg_hx->aop = NULL;
219 hc08_reg_hx->isFree = 0;
222 hc08_reg_hx->aop = NULL;
223 hc08_reg_hx->isFree = 0;
226 hc08_reg_h->aop = NULL;
227 hc08_reg_h->isFree = 0;
228 hc08_reg_x->aop = NULL;
229 hc08_reg_x->isFree = 0;
232 hc08_reg_x->aop = NULL;
233 hc08_reg_x->isFree = 0;
234 hc08_reg_a->aop = NULL;
235 hc08_reg_a->isFree = 0;
243 /*-----------------------------------------------------------------*/
244 /* hc08_dirtyReg - marks a register as dirty */
245 /*-----------------------------------------------------------------*/
247 hc08_dirtyReg (regs * reg, bool freereg)
254 hc08_reg_xa->aop = NULL;
257 hc08_reg_xa->aop = NULL;
258 hc08_reg_hx->aop = NULL;
261 hc08_reg_hx->aop = NULL;
264 hc08_reg_h->aop = NULL;
265 hc08_reg_x->aop = NULL;
268 hc08_reg_x->aop = NULL;
269 hc08_reg_a->aop = NULL;
278 /*-----------------------------------------------------------------*/
279 /* computeSpillable - given a point find the spillable live ranges */
280 /*-----------------------------------------------------------------*/
282 computeSpillable (iCode * ic)
286 /* spillable live ranges are those that are live at this
287 point . the following categories need to be subtracted
289 a) - those that are already spilt
290 b) - if being used by this one
291 c) - defined by this one */
293 spillable = bitVectCopy (ic->rlive);
295 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
297 bitVectCplAnd (spillable, ic->uses); /* used in this one */
298 bitVectUnSetBit (spillable, ic->defKey);
299 spillable = bitVectIntersect (spillable, _G.regAssigned);
304 /*-----------------------------------------------------------------*/
305 /* noSpilLoc - return true if a variable has no spil location */
306 /*-----------------------------------------------------------------*/
308 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
310 return (sym->usl.spillLoc ? 0 : 1);
313 /*-----------------------------------------------------------------*/
314 /* hasSpilLoc - will return 1 if the symbol has spil location */
315 /*-----------------------------------------------------------------*/
317 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
319 return (sym->usl.spillLoc ? 1 : 0);
322 /*-----------------------------------------------------------------*/
323 /* directSpilLoc - will return 1 if the splilocation is in direct */
324 /*-----------------------------------------------------------------*/
326 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
328 if (sym->usl.spillLoc &&
329 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
335 /*-----------------------------------------------------------------*/
336 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
337 /* but is not used as a pointer */
338 /*-----------------------------------------------------------------*/
340 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
342 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
345 /*-----------------------------------------------------------------*/
346 /* rematable - will return 1 if the remat flag is set */
347 /*-----------------------------------------------------------------*/
349 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
354 /*-----------------------------------------------------------------*/
355 /* notUsedInRemaining - not used or defined in remain of the block */
356 /*-----------------------------------------------------------------*/
358 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
360 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
361 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
364 /*-----------------------------------------------------------------*/
365 /* allLRs - return true for all */
366 /*-----------------------------------------------------------------*/
368 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
373 /*-----------------------------------------------------------------*/
374 /* liveRangesWith - applies function to a given set of live range */
375 /*-----------------------------------------------------------------*/
377 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
378 eBBlock * ebp, iCode * ic)
383 if (!lrs || !lrs->size)
386 for (i = 1; i < lrs->size; i++)
389 if (!bitVectBitValue (lrs, i))
392 /* if we don't find it in the live range
393 hash table we are in serious trouble */
394 if (!(sym = hTabItemWithKey (liveRanges, i)))
396 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
397 "liveRangesWith could not find liveRange");
401 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
402 addSetHead (&rset, sym);
409 /*-----------------------------------------------------------------*/
410 /* leastUsedLR - given a set determines which is the least used */
411 /*-----------------------------------------------------------------*/
413 leastUsedLR (set * sset)
415 symbol *sym = NULL, *lsym = NULL;
417 sym = lsym = setFirstItem (sset);
422 for (; lsym; lsym = setNextItem (sset))
425 /* if usage is the same then prefer
426 the spill the smaller of the two */
427 if (lsym->used == sym->used)
428 if (getSize (lsym->type) < getSize (sym->type))
432 if (lsym->used < sym->used)
437 setToNull ((void *) &sset);
442 /*-----------------------------------------------------------------*/
443 /* noOverLap - will iterate through the list looking for over lap */
444 /*-----------------------------------------------------------------*/
446 noOverLap (set * itmpStack, symbol * fsym)
451 for (sym = setFirstItem (itmpStack); sym;
452 sym = setNextItem (itmpStack))
454 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
460 /*-----------------------------------------------------------------*/
461 /* isFree - will return 1 if the a free spil location is found */
462 /*-----------------------------------------------------------------*/
467 V_ARG (symbol **, sloc);
468 V_ARG (symbol *, fsym);
470 /* if already found */
474 /* if it is free && and the itmp assigned to
475 this does not have any overlapping live ranges
476 with the one currently being assigned and
477 the size can be accomodated */
479 noOverLap (sym->usl.itmpStack, fsym) &&
480 getSize (sym->type) >= getSize (fsym->type))
489 /*-----------------------------------------------------------------*/
490 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
491 /*-----------------------------------------------------------------*/
493 spillLRWithPtrReg (symbol * forSym)
499 if (!_G.regAssigned ||
500 bitVectIsZero (_G.regAssigned))
503 hx = hc08_regWithIdx (HX_IDX);
505 /* for all live ranges */
506 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
507 lrsym = hTabNextItem (liveRanges, &k))
511 /* if no registers assigned to it or spilt */
512 /* if it does not overlap with this then
513 not need to spill it */
515 if (lrsym->isspilt || !lrsym->nRegs ||
516 (lrsym->liveTo < forSym->liveFrom))
519 /* go thru the registers : if it is either
520 r0 or r1 then spil it */
521 for (j = 0; j < lrsym->nRegs; j++)
522 if (lrsym->regs[j] == hx)
531 /*-----------------------------------------------------------------*/
532 /* createStackSpil - create a location on the stack to spil */
533 /*-----------------------------------------------------------------*/
535 createStackSpil (symbol * sym)
538 int useXstack, model;
542 /* first go try and find a free one that is already
543 existing on the stack */
544 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
546 /* found a free one : just update & return */
547 sym->usl.spillLoc = sloc;
550 addSetHead (&sloc->usl.itmpStack, sym);
554 /* could not then have to create one , this is the hard part
555 we need to allocate this on the stack : this is really a
556 hack!! but cannot think of anything better at this time */
558 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
560 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
565 sloc = newiTemp (slocBuffer);
567 /* set the type to the spilling symbol */
568 sloc->type = copyLinkChain (sym->type);
569 sloc->etype = getSpec (sloc->type);
570 SPEC_SCLS (sloc->etype) = S_DATA;
571 SPEC_EXTR (sloc->etype) = 0;
572 SPEC_STAT (sloc->etype) = 0;
573 SPEC_VOLATILE(sloc->etype) = 0;
574 SPEC_ABSA(sloc->etype) = 0;
576 /* we don't allow it to be allocated`
577 onto the external stack since : so we
578 temporarily turn it off ; we also
579 turn off memory model to prevent
580 the spil from going to the external storage
583 useXstack = options.useXstack;
584 model = options.model;
585 /* noOverlay = options.noOverlay; */
586 /* options.noOverlay = 1; */
587 options.model = options.useXstack = 0;
591 options.useXstack = useXstack;
592 options.model = model;
593 /* options.noOverlay = noOverlay; */
594 sloc->isref = 1; /* to prevent compiler warning */
596 /* if it is on the stack then update the stack */
597 if (IN_STACK (sloc->etype))
599 currFunc->stack += getSize (sloc->type);
600 _G.stackExtend += getSize (sloc->type);
603 _G.dataExtend += getSize (sloc->type);
605 /* add it to the _G.stackSpil set */
606 addSetHead (&_G.stackSpil, sloc);
607 sym->usl.spillLoc = sloc;
610 /* add it to the set of itempStack set
611 of the spill location */
612 addSetHead (&sloc->usl.itmpStack, sym);
616 /*-----------------------------------------------------------------*/
617 /* isSpiltOnStack - returns true if the spil location is on stack */
618 /*-----------------------------------------------------------------*/
620 isSpiltOnStack (symbol * sym)
630 /* if (sym->_G.stackSpil) */
633 if (!sym->usl.spillLoc)
636 etype = getSpec (sym->usl.spillLoc->type);
637 if (IN_STACK (etype))
643 /*-----------------------------------------------------------------*/
644 /* spillThis - spils a specific operand */
645 /*-----------------------------------------------------------------*/
647 spillThis (symbol * sym)
650 /* if this is rematerializable or has a spillLocation
651 we are okay, else we need to create a spillLocation
653 if (!(sym->remat || sym->usl.spillLoc))
654 createStackSpil (sym);
656 /* mark it has spilt & put it in the spilt set */
657 sym->isspilt = sym->spillA = 1;
658 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
660 bitVectUnSetBit (_G.regAssigned, sym->key);
661 bitVectUnSetBit (_G.totRegAssigned, sym->key);
663 for (i = 0; i < sym->nRegs; i++)
667 hc08_freeReg (sym->regs[i]);
671 /* if spilt on stack then free up r0 & r1
672 if they could have been assigned to some
674 // if (!hc08_ptrRegReq && isSpiltOnStack (sym))
677 // spillLRWithPtrReg (sym);
680 if (sym->usl.spillLoc && !sym->remat)
681 sym->usl.spillLoc->allocreq++;
685 /*-----------------------------------------------------------------*/
686 /* selectSpil - select a iTemp to spil : rather a simple procedure */
687 /*-----------------------------------------------------------------*/
689 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
691 bitVect *lrcs = NULL;
695 /* get the spillable live ranges */
696 lrcs = computeSpillable (ic);
698 /* get all live ranges that are rematerizable */
699 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
702 /* return the least used of these */
703 return leastUsedLR (selectS);
706 /* get live ranges with spillLocations in direct space */
707 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
709 sym = leastUsedLR (selectS);
710 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
711 sym->usl.spillLoc->rname :
712 sym->usl.spillLoc->name));
714 /* mark it as allocation required */
715 sym->usl.spillLoc->allocreq++;
719 /* if the symbol is local to the block then */
720 if (forSym->liveTo < ebp->lSeq)
723 /* check if there are any live ranges allocated
724 to registers that are not used in this block */
725 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
727 sym = leastUsedLR (selectS);
728 /* if this is not rematerializable */
737 /* check if there are any live ranges that not
738 used in the remainder of the block */
739 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
741 sym = leastUsedLR (selectS);
754 /* find live ranges with spillocation && not used as pointers */
755 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
758 sym = leastUsedLR (selectS);
759 /* mark this as allocation required */
760 sym->usl.spillLoc->allocreq++;
764 /* find live ranges with spillocation */
765 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
768 sym = leastUsedLR (selectS);
769 sym->usl.spillLoc->allocreq++;
773 /* couldn't find then we need to create a spil
774 location on the stack , for which one? the least
776 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
779 /* return a created spil location */
780 sym = createStackSpil (leastUsedLR (selectS));
781 sym->usl.spillLoc->allocreq++;
785 /* this is an extreme situation we will spill
786 this one : happens very rarely but it does happen */
792 /*-----------------------------------------------------------------*/
793 /* spilSomething - spil some variable & mark registers as free */
794 /*-----------------------------------------------------------------*/
796 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
801 /* get something we can spil */
802 ssym = selectSpil (ic, ebp, forSym);
804 /* mark it as spilt */
805 ssym->isspilt = ssym->spillA = 1;
806 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
808 /* mark it as not register assigned &
809 take it away from the set */
810 bitVectUnSetBit (_G.regAssigned, ssym->key);
811 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
813 /* mark the registers as free */
814 for (i = 0; i < ssym->nRegs; i++)
816 hc08_freeReg (ssym->regs[i]);
818 /* if spilt on stack then free up hx
819 if it could have been assigned to as gprs */
820 if (!hc08_ptrRegReq && isSpiltOnStack (ssym))
823 spillLRWithPtrReg (ssym);
826 /* if this was a block level spil then insert push & pop
827 at the start & end of block respectively */
830 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
831 /* add push to the start of the block */
832 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
833 ebp->sch->next : ebp->sch));
834 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
835 /* add pop to the end of the block */
836 addiCodeToeBBlock (ebp, nic, NULL);
839 /* if spilt because not used in the remainder of the
840 block then add a push before this instruction and
841 a pop at the end of the block */
842 if (ssym->remainSpil)
845 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
846 /* add push just before this instruction */
847 addiCodeToeBBlock (ebp, nic, ic);
849 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
850 /* add pop to the end of the block */
851 addiCodeToeBBlock (ebp, nic, NULL);
860 /*-----------------------------------------------------------------*/
861 /* getRegPtr - will try for PTR if not a GPR type if not spil */
862 /*-----------------------------------------------------------------*/
864 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
869 /* try for a ptr type */
870 if ((reg = allocReg (REG_PTR)))
873 /* try for gpr type */
874 if ((reg = allocReg (REG_GPR)))
877 /* we have to spil */
878 if (!spilSomething (ic, ebp, sym))
881 /* this looks like an infinite loop but
882 in really selectSpil will abort */
886 /*-----------------------------------------------------------------*/
887 /* getRegGpr - will try for GPR if not spil */
888 /*-----------------------------------------------------------------*/
890 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
895 /* try for gpr type */
896 if ((reg = allocReg (REG_GPR)))
900 if ((reg = allocReg (REG_PTR)))
903 /* we have to spil */
904 if (!spilSomething (ic, ebp, sym))
907 /* this looks like an infinite loop but
908 in really selectSpil will abort */
912 /*-----------------------------------------------------------------*/
913 /* getRegPtrNoSpil - get it cannot split */
914 /*-----------------------------------------------------------------*/
915 static regs *getRegPtrNoSpil()
919 /* try for a ptr type */
920 if ((reg = allocReg (REG_PTR)))
923 /* try for gpr type */
924 if ((reg = allocReg (REG_GPR)))
929 /* just to make the compiler happy */
933 /*-----------------------------------------------------------------*/
934 /* getRegGprNoSpil - get it cannot split */
935 /*-----------------------------------------------------------------*/
936 static regs *getRegGprNoSpil()
940 if ((reg = allocReg (REG_GPR)))
944 if ((reg = allocReg (REG_PTR)))
949 /* just to make the compiler happy */
953 /*-----------------------------------------------------------------*/
954 /* symHasReg - symbol has a given register */
955 /*-----------------------------------------------------------------*/
957 symHasReg (symbol * sym, regs * reg)
961 for (i = 0; i < sym->nRegs; i++)
962 if (sym->regs[i] == reg)
968 /*-----------------------------------------------------------------*/
969 /* deassignLRs - check the live to and if they have registers & are */
970 /* not spilt then free up the registers */
971 /*-----------------------------------------------------------------*/
973 deassignLRs (iCode * ic, eBBlock * ebp)
979 for (sym = hTabFirstItem (liveRanges, &k); sym;
980 sym = hTabNextItem (liveRanges, &k))
984 /* if it does not end here */
985 if (sym->liveTo > ic->seq)
988 /* if it was spilt on stack then we can
989 mark the stack spil location as free */
994 sym->usl.spillLoc->isFree = 1;
1000 if (!bitVectBitValue (_G.regAssigned, sym->key))
1003 /* special case check if this is an IFX &
1004 the privious one was a pop and the
1005 previous one was not spilt then keep track
1007 if (ic->op == IFX && ic->prev &&
1008 ic->prev->op == IPOP &&
1009 !ic->prev->parmPush &&
1010 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1011 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1017 bitVectUnSetBit (_G.regAssigned, sym->key);
1019 /* if the result of this one needs registers
1020 and does not have it then assign it right
1022 if (IC_RESULT (ic) &&
1023 !(SKIP_IC2 (ic) || /* not a special icode */
1024 ic->op == JUMPTABLE ||
1029 POINTER_SET (ic)) &&
1030 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1031 result->liveTo > ic->seq && /* and will live beyond this */
1032 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1033 result->regType == sym->regType && /* same register types */
1034 result->nRegs && /* which needs registers */
1035 !result->isspilt && /* and does not already have them */
1037 !bitVectBitValue (_G.regAssigned, result->key) &&
1038 /* the number of free regs + number of regs in this LR
1039 can accomodate the what result Needs */
1040 ((nfreeRegsType (result->regType) +
1041 sym->nRegs) >= result->nRegs)
1045 for (i = 0; i < result->nRegs; i++)
1047 result->regs[i] = sym->regs[i];
1049 result->regs[i] = getRegGpr (ic, ebp, result);
1051 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1052 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1056 /* free the remaining */
1057 for (; i < sym->nRegs; i++)
1061 if (!symHasReg (psym, sym->regs[i]))
1062 hc08_freeReg (sym->regs[i]);
1065 hc08_freeReg (sym->regs[i]);
1072 /*-----------------------------------------------------------------*/
1073 /* reassignLR - reassign this to registers */
1074 /*-----------------------------------------------------------------*/
1076 reassignLR (operand * op)
1078 symbol *sym = OP_SYMBOL (op);
1081 /* not spilt any more */
1082 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1083 bitVectUnSetBit (_G.spiltSet, sym->key);
1085 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1086 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1090 for (i = 0; i < sym->nRegs; i++)
1091 sym->regs[i]->isFree = 0;
1094 /*-----------------------------------------------------------------*/
1095 /* willCauseSpill - determines if allocating will cause a spill */
1096 /*-----------------------------------------------------------------*/
1098 willCauseSpill (int nr, int rt)
1100 /* first check if there are any avlb registers
1101 of te type required */
1104 /* special case for pointer type
1105 if pointer type not avlb then
1106 check for type gpr */
1107 if (nFreeRegs (rt) >= nr)
1109 if (nFreeRegs (REG_GPR) >= nr)
1116 if (nFreeRegs (rt) >= nr)
1121 if (nFreeRegs (REG_PTR) +
1122 nFreeRegs (REG_GPR) >= nr)
1127 /* it will cause a spil */
1131 /*-----------------------------------------------------------------*/
1132 /* positionRegs - the allocator can allocate same registers to res- */
1133 /* ult and operand, if this happens make sure they are in the same */
1134 /* position as the operand otherwise chaos results */
1135 /*-----------------------------------------------------------------*/
1137 positionRegs (symbol * result, symbol * opsym)
1139 int count = min (result->nRegs, opsym->nRegs);
1140 int i, j = 0, shared = 0;
1143 /* if the result has been spilt then cannot share */
1148 /* first make sure that they actually share */
1149 for (i = 0; i < count; i++)
1151 for (j = 0; j < count; j++)
1153 if (result->regs[i] == opsym->regs[j] && i != j)
1163 regs *tmp = result->regs[i];
1164 result->regs[i] = result->regs[j];
1165 result->regs[j] = tmp;
1172 /*------------------------------------------------------------------*/
1173 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1174 /* it should either have registers or have beed spilled. Otherwise, */
1175 /* there was an uninitialized variable, so just spill this to get */
1176 /* the operand in a valid state. */
1177 /*------------------------------------------------------------------*/
1179 verifyRegsAssigned (operand *op, iCode * ic)
1184 if (!IS_ITEMP (op)) return;
1186 sym = OP_SYMBOL (op);
1187 if (sym->isspilt) return;
1188 if (!sym->nRegs) return;
1189 if (sym->regs[0]) return;
1191 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1192 sym->prereqv ? sym->prereqv->name : sym->name);
1198 /*-----------------------------------------------------------------*/
1199 /* serialRegAssign - serially allocate registers to the variables */
1200 /*-----------------------------------------------------------------*/
1202 serialRegAssign (eBBlock ** ebbs, int count)
1206 /* for all blocks */
1207 for (i = 0; i < count; i++) {
1211 if (ebbs[i]->noPath &&
1212 (ebbs[i]->entryLabel != entryLabel &&
1213 ebbs[i]->entryLabel != returnLabel))
1216 /* of all instructions do */
1217 for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1221 // update the registers in use at the start of this icode
1222 for (reg=0; reg<hc08_nRegs; reg++) {
1223 if (regshc08[reg].isFree) {
1224 ic->riu &= ~(regshc08[reg].mask);
1226 ic->riu |= (regshc08[reg].mask);
1231 /* if this is an ipop that means some live
1232 range will have to be assigned again */
1234 reassignLR (IC_LEFT (ic));
1236 /* if result is present && is a true symbol */
1237 if (IC_RESULT (ic) && ic->op != IFX &&
1238 IS_TRUE_SYMOP (IC_RESULT (ic)))
1239 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1241 /* take away registers from live
1242 ranges that end at this instruction */
1243 deassignLRs (ic, ebbs[i]);
1245 /* some don't need registers */
1246 if (SKIP_IC2 (ic) ||
1247 ic->op == JUMPTABLE ||
1251 (IC_RESULT (ic) && POINTER_SET (ic)))
1254 /* now we need to allocate registers
1255 only for the result */
1256 if (IC_RESULT (ic)) {
1257 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1263 /* if it does not need or is spilt
1264 or is already assigned to registers
1265 or will not live beyond this instructions */
1268 bitVectBitValue (_G.regAssigned, sym->key) ||
1269 sym->liveTo <= ic->seq)
1272 /* if some liverange has been spilt at the block level
1273 and this one live beyond this block then spil this
1275 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1279 /* if trying to allocate this will cause
1280 a spill and there is nothing to spill
1281 or this one is rematerializable then
1283 willCS = willCauseSpill (sym->nRegs, sym->regType);
1284 spillable = computeSpillable (ic);
1285 if (sym->remat || (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)) {
1312 /* if we need ptr regs for the right side
1314 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1315 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1319 /* else we assign registers to it */
1320 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1321 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1323 for (j = 0; j < sym->nRegs; j++) {
1324 if (sym->regType == REG_PTR)
1325 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1327 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1329 /* if the allocation failed which means
1330 this was spilt then break */
1331 if (!sym->regs[j]) {
1336 /* if it shares registers with operands make sure
1337 that they are in the same position */
1338 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1339 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1340 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1341 OP_SYMBOL (IC_LEFT (ic)));
1343 /* do the same for the right operand */
1344 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1345 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1346 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1347 OP_SYMBOL (IC_RIGHT (ic)));
1359 /* Check for and fix any problems with uninitialized operands */
1360 for (i = 0; i < count; i++)
1364 if (ebbs[i]->noPath &&
1365 (ebbs[i]->entryLabel != entryLabel &&
1366 ebbs[i]->entryLabel != returnLabel))
1369 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1376 verifyRegsAssigned (IC_COND (ic), ic);
1380 if (ic->op == JUMPTABLE)
1382 verifyRegsAssigned (IC_JTCOND (ic), ic);
1386 verifyRegsAssigned (IC_RESULT (ic), ic);
1387 verifyRegsAssigned (IC_LEFT (ic), ic);
1388 verifyRegsAssigned (IC_RIGHT (ic), ic);
1394 /*-----------------------------------------------------------------*/
1395 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1396 /*-----------------------------------------------------------------*/
1397 static void fillGaps()
1402 if (getenv("DISABLE_FILL_GAPS")) return;
1404 /* look for livernages that was spilt by the allocator */
1405 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1406 sym = hTabNextItem(liveRanges,&key)) {
1411 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1413 /* find the liveRanges this one clashes with, that are
1414 still assigned to registers & mark the registers as used*/
1415 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1419 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1420 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1423 clr = hTabItemWithKey(liveRanges,i);
1426 /* mark these registers as used */
1427 for (k = 0 ; k < clr->nRegs ; k++ )
1428 hc08_useReg(clr->regs[k]);
1431 if (willCauseSpill(sym->nRegs,sym->regType)) {
1432 /* NOPE :( clear all registers & and continue */
1437 /* THERE IS HOPE !!!! */
1438 for (i=0; i < sym->nRegs ; i++ ) {
1439 if (sym->regType == REG_PTR)
1440 sym->regs[i] = getRegPtrNoSpil ();
1442 sym->regs[i] = getRegGprNoSpil ();
1445 /* for all its definitions check if the registers
1446 allocated needs positioning NOTE: we can position
1447 only ONCE if more than One positioning required
1450 for (i = 0 ; i < sym->defs->size ; i++ ) {
1451 if (bitVectBitValue(sym->defs,i)) {
1453 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1454 if (SKIP_IC(ic)) continue;
1455 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1456 /* if left is assigned to registers */
1457 if (IS_SYMOP(IC_LEFT(ic)) &&
1458 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1459 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1461 if (IS_SYMOP(IC_RIGHT(ic)) &&
1462 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1463 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1465 if (pdone > 1) break;
1468 for (i = 0 ; i < sym->uses->size ; i++ ) {
1469 if (bitVectBitValue(sym->uses,i)) {
1471 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1472 if (SKIP_IC(ic)) continue;
1473 if (!IS_ASSIGN_ICODE(ic)) continue ;
1475 /* if result is assigned to registers */
1476 if (IS_SYMOP(IC_RESULT(ic)) &&
1477 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1478 pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1480 if (pdone > 1) break;
1483 /* had to position more than once GIVE UP */
1485 /* UNDO all the changes we made to try this */
1487 for (i=0; i < sym->nRegs ; i++ ) {
1488 sym->regs[i] = NULL;
1491 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1494 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1495 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1496 sym->isspilt = sym->spillA = 0 ;
1497 sym->usl.spillLoc->allocreq--;
1502 /*-----------------------------------------------------------------*/
1503 /* rUmaskForOp :- returns register mask for an operand */
1504 /*-----------------------------------------------------------------*/
1506 hc08_rUmaskForOp (operand * op)
1512 /* only temporaries are assigned registers */
1516 sym = OP_SYMBOL (op);
1518 /* if spilt or no registers assigned to it
1520 if (sym->isspilt || !sym->nRegs)
1523 rumask = newBitVect (hc08_nRegs);
1525 for (j = 0; j < sym->nRegs; j++)
1527 rumask = bitVectSetBit (rumask,
1528 sym->regs[j]->rIdx);
1534 /*-----------------------------------------------------------------*/
1535 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1536 /*-----------------------------------------------------------------*/
1538 regsUsedIniCode (iCode * ic)
1540 bitVect *rmask = newBitVect (hc08_nRegs);
1542 /* do the special cases first */
1545 rmask = bitVectUnion (rmask,
1546 hc08_rUmaskForOp (IC_COND (ic)));
1550 /* for the jumptable */
1551 if (ic->op == JUMPTABLE)
1553 rmask = bitVectUnion (rmask,
1554 hc08_rUmaskForOp (IC_JTCOND (ic)));
1559 /* of all other cases */
1561 rmask = bitVectUnion (rmask,
1562 hc08_rUmaskForOp (IC_LEFT (ic)));
1566 rmask = bitVectUnion (rmask,
1567 hc08_rUmaskForOp (IC_RIGHT (ic)));
1570 rmask = bitVectUnion (rmask,
1571 hc08_rUmaskForOp (IC_RESULT (ic)));
1577 /*-----------------------------------------------------------------*/
1578 /* createRegMask - for each instruction will determine the regsUsed */
1579 /*-----------------------------------------------------------------*/
1581 createRegMask (eBBlock ** ebbs, int count)
1585 /* for all blocks */
1586 for (i = 0; i < count; i++)
1590 if (ebbs[i]->noPath &&
1591 (ebbs[i]->entryLabel != entryLabel &&
1592 ebbs[i]->entryLabel != returnLabel))
1595 /* for all instructions */
1596 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1601 if (SKIP_IC2 (ic) || !ic->rlive)
1604 /* first mark the registers used in this
1606 ic->rUsed = regsUsedIniCode (ic);
1607 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1609 /* now create the register mask for those
1610 registers that are in use : this is a
1611 super set of ic->rUsed */
1612 ic->rMask = newBitVect (hc08_nRegs + 1);
1614 /* for all live Ranges alive at this point */
1615 for (j = 1; j < ic->rlive->size; j++)
1620 /* if not alive then continue */
1621 if (!bitVectBitValue (ic->rlive, j))
1624 /* find the live range we are interested in */
1625 if (!(sym = hTabItemWithKey (liveRanges, j)))
1627 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1628 "createRegMask cannot find live range");
1629 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1633 /* if no register assigned to it */
1634 if (!sym->nRegs || sym->isspilt)
1637 /* for all the registers allocated to it */
1638 for (k = 0; k < sym->nRegs; k++)
1641 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1647 /*-----------------------------------------------------------------*/
1648 /* rematStr - returns the rematerialized string for a remat var */
1649 /*-----------------------------------------------------------------*/
1651 rematStr (symbol * sym)
1654 iCode *ic = sym->rematiCode;
1659 /* if plus or minus print the right hand side */
1660 if (ic->op == '+' || ic->op == '-')
1662 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1665 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1672 offset += operandLitValue (IC_RIGHT (ic));
1673 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1678 offset -= operandLitValue (IC_RIGHT (ic));
1679 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1683 /* cast then continue */
1684 if (IS_CAST_ICODE(ic)) {
1685 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1688 /* we reached the end */
1689 if (ic->op == ADDRESS_OF)
1690 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1691 else if (ic->op == '=')
1692 sprintf (s, "0x%04x", (int) operandLitValue (IC_RIGHT (ic)) );
1699 /*-----------------------------------------------------------------*/
1700 /* regTypeNum - computes the type & number of registers required */
1701 /*-----------------------------------------------------------------*/
1703 regTypeNum (eBBlock *ebbs)
1709 /* for each live range do */
1710 for (sym = hTabFirstItem (liveRanges, &k); sym;
1711 sym = hTabNextItem (liveRanges, &k))
1714 /* if used zero times then no registers needed */
1715 if ((sym->liveTo - sym->liveFrom) == 0)
1719 /* if the live range is a temporary */
1723 /* if the type is marked as a conditional */
1724 if (sym->regType == REG_CND)
1727 /* if used in return only then we don't
1729 if (sym->ruonly || sym->accuse)
1731 if (IS_AGGREGATE (sym->type) || sym->isptr)
1732 sym->type = aggrToPtr (sym->type, FALSE);
1736 /* if the symbol has only one definition &
1737 that definition is a get_pointer */
1738 if (bitVectnBitsOn (sym->defs) == 1 &&
1739 (ic = hTabItemWithKey (iCodehTab,
1740 bitVectFirstBit (sym->defs))) &&
1742 !IS_BITVAR (sym->etype) &&
1743 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1746 if (ptrPseudoSymSafe (sym, ic))
1748 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1752 /* if in data space or idata space then try to
1753 allocate pointer register */
1757 /* if not then we require registers */
1758 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1759 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1760 getSize (sym->type));
1764 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1765 printTypeChain (sym->type, stderr);
1766 fprintf (stderr, "\n");
1769 /* determine the type of register required */
1770 if (sym->nRegs == 1 &&
1771 IS_PTR (sym->type) &&
1773 sym->regType = REG_PTR;
1775 sym->regType = REG_GPR;
1779 /* for the first run we don't provide */
1780 /* registers for true symbols we will */
1781 /* see how things go */
1787 /*-----------------------------------------------------------------*/
1788 /* freeAllRegs - mark all registers as free */
1789 /*-----------------------------------------------------------------*/
1795 for (i = 0; i < hc08_nRegs; i++) {
1796 regshc08[i].isFree = 1;
1797 regshc08[i].aop = NULL;
1801 /*-----------------------------------------------------------------*/
1802 /* deallocStackSpil - this will set the stack pointer back */
1803 /*-----------------------------------------------------------------*/
1805 DEFSETFUNC (deallocStackSpil)
1814 /*-----------------------------------------------------------------*/
1815 /* farSpacePackable - returns the packable icode for far variables */
1816 /*-----------------------------------------------------------------*/
1818 farSpacePackable (iCode * ic)
1822 /* go thru till we find a definition for the
1823 symbol on the right */
1824 for (dic = ic->prev; dic; dic = dic->prev)
1826 /* if the definition is a call then no */
1827 if ((dic->op == CALL || dic->op == PCALL) &&
1828 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1833 /* if shift by unknown amount then not */
1834 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1835 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1839 /* if pointer get and size > 1 */
1840 if (POINTER_GET (dic) &&
1841 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1844 if (POINTER_SET (dic) &&
1845 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1849 /* if any three is a true symbol in far space */
1850 if (IC_RESULT (dic) &&
1851 IS_TRUE_SYMOP (IC_RESULT (dic)) /* &&
1852 isOperandInFarSpace (IC_RESULT (dic)) */)
1855 if (IC_RIGHT (dic) &&
1856 IS_TRUE_SYMOP (IC_RIGHT (dic)) /* &&
1857 isOperandInFarSpace (IC_RIGHT (dic)) */ &&
1858 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1861 if (IC_LEFT (dic) &&
1862 IS_TRUE_SYMOP (IC_LEFT (dic)) /* &&
1863 isOperandInFarSpace (IC_LEFT (dic)) */ &&
1864 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1867 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1869 if ((dic->op == LEFT_OP ||
1870 dic->op == RIGHT_OP ||
1872 IS_OP_LITERAL (IC_RIGHT (dic)))
1885 packRegsForLiteral (iCode * ic)
1892 if (POINTER_SET (ic))
1894 if (!IS_LITERAL (getSpec (operandType (IC_RIGHT (ic)))))
1896 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1899 for (k=0; k< OP_USES (IC_RESULT (ic))->size; k++)
1900 if (bitVectBitValue (OP_USES (IC_RESULT (ic)), k))
1902 uic = hTabItemWithKey (iCodehTab, k);
1905 if (uic->op != IFX && uic->op != JUMPTABLE)
1907 if (IC_LEFT (uic) && IC_LEFT (uic)->key == IC_RESULT (ic)->key)
1908 ReplaceOpWithCheaperOp(&IC_LEFT(uic), IC_RIGHT(ic));
1909 if (IC_RIGHT (uic) && IC_RIGHT (uic)->key == IC_RESULT (ic)->key)
1910 ReplaceOpWithCheaperOp(&IC_RIGHT(uic), IC_RIGHT(ic));
1911 if (IC_RESULT (uic) && IC_RESULT (uic)->key == IC_RESULT (ic)->key)
1912 ReplaceOpWithCheaperOp(&IC_RESULT(uic), IC_RIGHT(ic));
1920 /*-----------------------------------------------------------------*/
1921 /* packRegsForAssign - register reduction for assignment */
1922 /*-----------------------------------------------------------------*/
1924 packRegsForAssign (iCode * ic, eBBlock * ebp)
1928 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1929 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1930 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1936 /* if the true symbol is defined in far space or on stack
1937 then we should not since this will increase register pressure */
1939 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1944 /* find the definition of iTempNN scanning backwards if we find a
1945 a use of the true symbol in before we find the definition then
1947 for (dic = ic->prev; dic; dic = dic->prev)
1950 #if 0 /* jwk: This collides with 1.43 but I really see no need for
1951 this anymore. It fixes bug #716790 and substantially improves
1952 redundant register usage around function calls.
1955 /* if there is a function call then don't pack it */
1956 if ((dic->op == CALL || dic->op == PCALL))
1966 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1967 IS_OP_VOLATILE (IC_RESULT (dic)))
1973 if (IS_SYMOP (IC_RESULT (dic)) &&
1974 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1976 if (POINTER_SET (dic))
1982 if (IS_SYMOP (IC_RIGHT (dic)) &&
1983 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1984 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1990 if (IS_SYMOP (IC_LEFT (dic)) &&
1991 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1992 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1998 if (POINTER_SET (dic) &&
1999 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2007 return 0; /* did not find */
2009 /* if assignment then check that right is not a bit */
2010 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2012 sym_link *etype = operandType (IC_RIGHT (dic));
2013 if (IS_BITFIELD (etype))
2015 /* if result is a bit too then it's ok */
2016 etype = operandType (IC_RESULT (dic));
2017 if (!IS_BITFIELD (etype))
2021 /* if the result is on stack or iaccess then it must be
2022 the same atleast one of the operands */
2023 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2024 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2027 /* the operation has only one symbol
2028 operator then we can pack */
2029 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2030 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2033 if (!((IC_LEFT (dic) &&
2034 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2036 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2040 /* found the definition */
2041 /* replace the result with the result of */
2042 /* this assignment and remove this assignment */
2043 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2044 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2046 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2048 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2050 // TODO: and the otherway around?
2052 /* delete from liverange table also
2053 delete from all the points inbetween and the new
2055 for (sic = dic; sic != ic; sic = sic->next)
2057 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2058 if (IS_ITEMP (IC_RESULT (dic)))
2059 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2062 remiCodeFromeBBlock (ebp, ic);
2063 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2064 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2065 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2070 /*------------------------------------------------------------------*/
2071 /* findAssignToSym : scanning backwards looks for first assig found */
2072 /*------------------------------------------------------------------*/
2074 findAssignToSym (operand * op, iCode * ic)
2078 /* This routine is used to find sequences like
2080 ...; (intervening ops don't use iTempAA or modify FOO)
2081 blah = blah + iTempAA;
2083 and eliminate the use of iTempAA, freeing up its register for
2088 for (dic = ic->prev; dic; dic = dic->prev)
2091 /* if definition by assignment */
2092 if (dic->op == '=' &&
2093 !POINTER_SET (dic) &&
2094 IC_RESULT (dic)->key == op->key
2095 && IS_TRUE_SYMOP(IC_RIGHT(dic))
2097 break; /* found where this temp was defined */
2099 /* if we find an usage then we cannot delete it */
2100 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2103 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2106 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2111 return NULL; /* didn't find any assignment to op */
2113 /* we are interested only if defined in far space */
2114 /* or in stack space in case of + & - */
2116 /* if assigned to a non-symbol then don't repack regs */
2117 if (!IS_SYMOP (IC_RIGHT (dic)))
2120 /* if the symbol is volatile then we should not */
2121 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2123 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2124 What does it mean for an iTemp to be volatile, anyway? Passing
2125 TRUE is more cautious but may prevent possible optimizations */
2127 /* if the symbol is in far space then we should not */
2128 /* if (isOperandInFarSpace (IC_RIGHT (dic)))
2131 /* for + & - operations make sure that
2132 if it is on the stack it is the same
2133 as one of the three operands */
2135 if ((ic->op == '+' || ic->op == '-') &&
2136 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2139 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2140 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2141 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2146 /* now make sure that the right side of dic
2147 is not defined between ic & dic */
2150 iCode *sic = dic->next;
2152 for (; sic != ic; sic = sic->next)
2153 if (IC_RESULT (sic) &&
2154 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2161 /*-----------------------------------------------------------------*/
2162 /* reassignAliasedSym - used by packRegsForSupport to replace */
2163 /* redundant iTemp with equivalent symbol */
2164 /*-----------------------------------------------------------------*/
2166 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2169 unsigned oldSymKey, newSymKey;
2171 oldSymKey = op->key;
2172 newSymKey = IC_RIGHT(assignment)->key;
2174 /* only track live ranges of compiler-generated temporaries */
2175 if (!IS_ITEMP(IC_RIGHT(assignment)))
2178 /* update the live-value bitmaps */
2179 for (ic = assignment; ic != use; ic = ic->next) {
2180 bitVectUnSetBit (ic->rlive, oldSymKey);
2182 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2185 /* update the sym of the used operand */
2186 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2187 op->key = OP_SYMBOL(op)->key;
2189 /* update the sym's liverange */
2190 if ( OP_LIVETO(op) < ic->seq )
2191 setToRange(op, ic->seq, FALSE);
2193 /* remove the assignment iCode now that its result is unused */
2194 remiCodeFromeBBlock (ebp, assignment);
2195 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2196 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2200 /*-----------------------------------------------------------------*/
2201 /* packRegsForSupport :- reduce some registers for support calls */
2202 /*-----------------------------------------------------------------*/
2204 packRegsForSupport (iCode * ic, eBBlock * ebp)
2209 /* for the left & right operand :- look to see if the
2210 left was assigned a true symbol in far space in that
2211 case replace them */
2213 if (IS_ITEMP (IC_LEFT (ic)) &&
2214 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2216 dic = findAssignToSym (IC_LEFT (ic), ic);
2220 /* found it we need to remove it from the block */
2221 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2226 /* do the same for the right operand */
2227 if (IS_ITEMP (IC_RIGHT (ic)) &&
2228 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2230 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2234 /* found it we need to remove it from the block */
2235 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2243 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2247 /*-----------------------------------------------------------------*/
2248 /* packRegsForOneuse : - will reduce some registers for single Use */
2249 /*-----------------------------------------------------------------*/
2251 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2256 /* if returning a literal then do nothing */
2260 /* only up to 2 bytes */
2261 if (getSize (operandType (op)) > (fReturnSizeHC08 - 2))
2266 if (ic->op != SEND //RETURN
2268 && !POINTER_SET (ic)
2269 && !POINTER_GET (ic) )
2272 if (ic->op == SEND && ic->argreg != 1) return NULL;
2274 /* this routine will mark the a symbol as used in one
2275 instruction use only && if the defintion is local
2276 (ie. within the basic block) && has only one definition &&
2277 that definiion is either a return value from a
2278 function or does not contain any variables in
2280 uses = bitVectCopy (OP_USES (op));
2281 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2282 if (!bitVectIsZero (uses)) /* has other uses */
2285 /* if it has only one defintion */
2286 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2287 return NULL; /* has more than one definition */
2289 /* get that definition */
2291 hTabItemWithKey (iCodehTab,
2292 bitVectFirstBit (OP_DEFS (op)))))
2295 /* if that only usage is a cast */
2296 if (dic->op == CAST) {
2297 /* to a bigger type */
2298 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2299 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2300 /* than we can not, since we cannot predict the usage of b & acc */
2305 /* found the definition now check if it is local */
2306 if (dic->seq < ebp->fSeq ||
2307 dic->seq > ebp->lSeq)
2308 return NULL; /* non-local */
2310 /* now check if it is the return from
2312 if (dic->op == CALL || dic->op == PCALL)
2314 if (ic->op != SEND && ic->op != RETURN &&
2315 !POINTER_SET(ic) && !POINTER_GET(ic))
2317 OP_SYMBOL (op)->ruonly = 1;
2324 /* otherwise check that the definition does
2325 not contain any symbols in far space */
2326 // if (isOperandInFarSpace (IC_LEFT (dic)) ||
2327 // isOperandInFarSpace (IC_RIGHT (dic)) ||
2328 // IS_OP_RUONLY (IC_LEFT (ic)) ||
2329 // IS_OP_RUONLY (IC_RIGHT (ic)))
2334 /* if pointer set then make sure the pointer
2337 if (POINTER_SET (dic) &&
2338 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2341 if (POINTER_GET (dic) &&
2342 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2348 /* also make sure the intervenening instructions
2349 don't have any thing in far space */
2350 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2353 /* if there is an intervening function call then no */
2354 if (dic->op == CALL || dic->op == PCALL)
2356 /* if pointer set then make sure the pointer
2359 if (POINTER_SET (dic) &&
2360 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2363 if (POINTER_GET (dic) &&
2364 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2367 /* if address of & the result is remat the okay */
2368 if (dic->op == ADDRESS_OF &&
2369 OP_SYMBOL (IC_RESULT (dic))->remat)
2372 /* if operand has size of three or more & this
2373 operation is a '*','/' or '%' then 'b' may
2376 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2377 getSize (operandType (op)) >= 3)
2381 /* if left or right or result is in far space */
2382 // if (isOperandInFarSpace (IC_LEFT (dic)) ||
2383 // isOperandInFarSpace (IC_RIGHT (dic)) ||
2384 // isOperandInFarSpace (IC_RESULT (dic)) ||
2385 // IS_OP_RUONLY (IC_LEFT (dic)) ||
2386 // IS_OP_RUONLY (IC_RIGHT (dic)) ||
2387 // IS_OP_RUONLY (IC_RESULT (dic)))
2391 // /* if left or right or result is on stack */
2392 // if (isOperandOnStack(IC_LEFT(dic)) ||
2393 // isOperandOnStack(IC_RIGHT(dic)) ||
2394 // isOperandOnStack(IC_RESULT(dic))) {
2399 OP_SYMBOL (op)->ruonly = 1;
2404 /*-----------------------------------------------------------------*/
2405 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2406 /*-----------------------------------------------------------------*/
2408 isBitwiseOptimizable (iCode * ic)
2410 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2411 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2413 /* bitwise operations are considered optimizable
2414 under the following conditions (Jean-Louis VERN)
2426 if (IS_LITERAL(rtype) ||
2427 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2433 /*-----------------------------------------------------------------*/
2434 /* isCommutativeOp - tests whether this op cares what order its */
2435 /* operands are in */
2436 /*-----------------------------------------------------------------*/
2437 bool isCommutativeOp2(unsigned int op)
2439 if (op == '+' || op == '*' || op == EQ_OP ||
2440 op == '^' || op == '|' || op == BITWISEAND)
2446 /*-----------------------------------------------------------------*/
2447 /* operandUsesAcc2 - determines whether the code generated for this */
2448 /* operand will have to use the accumulator */
2449 /*-----------------------------------------------------------------*/
2450 bool operandUsesAcc2(operand *op)
2456 symbol *sym = OP_SYMBOL(op);
2460 return TRUE; /* duh! */
2462 // if (IN_STACK(sym->etype) || sym->onStack ||
2463 // (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2464 // return TRUE; /* acc is used to calc stack offset */
2469 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2471 return FALSE; /* more checks? */
2475 symspace = SPEC_OCLS(sym->etype);
2477 // if (sym->iaccess && symspace->paged)
2478 // return TRUE; /* must fetch paged indirect sym via accumulator */
2480 if (IN_BITSPACE(symspace))
2481 return TRUE; /* fetching bit vars uses the accumulator */
2483 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2484 return TRUE; /* fetched via accumulator and dptr */
2490 /*-----------------------------------------------------------------*/
2491 /* packRegsForAccUse - pack registers for acc use */
2492 /*-----------------------------------------------------------------*/
2494 packRegsForAccUse (iCode * ic)
2498 /* if this is an aggregate, e.g. a one byte char array */
2499 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2503 /* if we are calling a reentrant function that has stack parameters */
2505 if (ic->op == CALL &&
2506 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2507 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2510 if (ic->op == PCALL &&
2511 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2512 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2516 /* if + or - then it has to be one byte result */
2517 if ((ic->op == '+' || ic->op == '-')
2518 && getSize (operandType (IC_RESULT (ic))) > 1)
2522 /* if shift operation make sure right side is a literal */
2523 if (ic->op == RIGHT_OP &&
2524 (!isOperandLiteral (IC_RIGHT (ic)) ||
2525 (getSize (operandType (IC_RESULT (ic) )) > 1)))
2528 if (ic->op == LEFT_OP &&
2529 (!isOperandLiteral (IC_RIGHT (ic)) ||
2530 (getSize (operandType (IC_RESULT (ic) )) > 1)))
2533 if (IS_BITWISE_OP (ic) &&
2534 getSize (operandType (IC_RESULT (ic))) > 1)
2538 /* has only one definition */
2539 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2542 /* has only one use */
2543 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2546 /* and the usage immediately follows this iCode */
2547 if (!(uic = hTabItemWithKey (iCodehTab,
2548 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2551 if (ic->next != uic)
2554 /* if it is a conditional branch then we definitely can */
2558 if (uic->op == JUMPTABLE)
2562 if (POINTER_SET (uic) &&
2563 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2567 /* if the usage is not is an assignment
2568 or an arithmetic / bitwise / shift operation then not */
2569 if (uic->op != '=' &&
2570 !IS_ARITHMETIC_OP (uic) &&
2571 !IS_BITWISE_OP (uic) &&
2572 (uic->op != LEFT_OP) &&
2573 (uic->op != RIGHT_OP) &&
2574 (uic->op != GETHBIT) &&
2575 (uic->op != RETURN) &&
2581 /* if used in ^ operation then make sure right is not a
2582 literal (WIML: Why is this?) */
2583 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2586 /* if shift operation make sure right side is not a literal */
2587 /* WIML: Why is this? */
2588 if (uic->op == RIGHT_OP &&
2589 (isOperandLiteral (IC_RIGHT (uic)) ||
2590 getSize (operandType (IC_RESULT (uic))) > 1))
2592 if (uic->op == LEFT_OP &&
2593 (isOperandLiteral (IC_RIGHT (uic)) ||
2594 getSize (operandType (IC_RESULT (uic))) > 1))
2598 /* make sure that the result of this icode is not on the
2599 stack, since acc is used to compute stack offset */
2601 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2602 OP_SYMBOL (IC_RESULT (uic))->onStack)
2605 // if (isOperandOnStack(IC_RESULT(uic)))
2609 /* if the usage has only one operand then we can */
2610 if (IC_LEFT (uic) == NULL ||
2611 IC_RIGHT (uic) == NULL)
2615 /* if the other operand uses the accumulator then we cannot */
2616 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2617 operandUsesAcc2(IC_RIGHT(uic))) ||
2618 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2619 operandUsesAcc2(IC_LEFT(uic))) )
2622 /* make sure this is on the left side if not commutative */
2623 /* except for '-', which has been written to be able to
2624 handle reversed operands */
2625 if (!(isCommutativeOp2(ic->op) || ic->op == '-') &&
2626 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2631 // this is too dangerous and need further restrictions
2634 /* if one of them is a literal then we can */
2635 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2636 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2638 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2645 if ((POINTER_GET(uic))
2646 || (ic->op == ADDRESS_OF && uic->op == '+' && IS_OP_LITERAL (IC_RIGHT (uic))))
2648 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HX;
2652 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_XA;
2655 /*-----------------------------------------------------------------*/
2656 /* packForPush - hueristics to reduce iCode for pushing */
2657 /*-----------------------------------------------------------------*/
2659 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2663 struct eBBlock * ebp=ebpp[blockno];
2665 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2668 /* must have only definition & one usage */
2669 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2670 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2673 /* find the definition */
2674 if (!(dic = hTabItemWithKey (iCodehTab,
2675 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2678 if (dic->op != '=' || POINTER_SET (dic))
2681 if (dic->seq < ebp->fSeq) { // Evelyn did this
2683 for (i=0; i<blockno; i++) {
2684 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2689 wassert (i!=blockno); // no way to recover from here
2692 if (IS_SYMOP(IC_RIGHT(dic))) {
2693 /* make sure the right side does not have any definitions
2695 dbv = OP_DEFS(IC_RIGHT(dic));
2696 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2697 if (bitVectBitValue(dbv,lic->key))
2700 /* make sure they have the same type */
2701 if (IS_SPEC(operandType(IC_LEFT(ic))))
2703 sym_link *itype=operandType(IC_LEFT(ic));
2704 sym_link *ditype=operandType(IC_RIGHT(dic));
2706 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2707 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2710 /* extend the live range of replaced operand if needed */
2711 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2712 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2714 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2717 /* we now we know that it has one & only one def & use
2718 and the that the definition is an assignment */
2719 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2720 remiCodeFromeBBlock (ebp, dic);
2721 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2724 /*-----------------------------------------------------------------*/
2725 /* packRegisters - does some transformations to reduce register */
2727 /*-----------------------------------------------------------------*/
2729 packRegisters (eBBlock ** ebpp, int blockno)
2733 eBBlock *ebp=ebpp[blockno];
2740 /* look for assignments of the form */
2741 /* iTempNN = TRueSym (someoperation) SomeOperand */
2743 /* TrueSym := iTempNN:1 */
2744 for (ic = ebp->sch; ic; ic = ic->next)
2746 /* find assignment of the form TrueSym := iTempNN:1 */
2747 if (ic->op == '=' && !POINTER_SET (ic) )
2748 change += packRegsForAssign (ic, ebp);
2755 for (ic = ebp->sch; ic; ic = ic->next)
2757 //packRegsForLiteral (ic);
2759 /* if this is an itemp & result of an address of a true sym
2760 then mark this as rematerialisable */
2761 if (ic->op == ADDRESS_OF &&
2762 IS_ITEMP (IC_RESULT (ic)) &&
2763 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2764 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2765 !OP_SYMBOL (IC_LEFT (ic))->onStack )
2768 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2769 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2770 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2774 if (ic->op == '=' &&
2775 !POINTER_SET (ic) &&
2776 IS_ITEMP (IC_RESULT (ic)) &&
2777 IS_VALOP (IC_RIGHT (ic)) &&
2778 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2781 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2782 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2783 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2787 /* if straight assignment then carry remat flag if
2788 this is the only definition */
2789 if (ic->op == '=' &&
2790 !POINTER_SET (ic) &&
2791 IS_SYMOP (IC_RIGHT (ic)) &&
2792 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2793 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2794 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2797 OP_SYMBOL (IC_RESULT (ic))->remat =
2798 OP_SYMBOL (IC_RIGHT (ic))->remat;
2799 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2800 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2803 /* if cast to a generic pointer & the pointer being
2804 cast is remat, then we can remat this cast as well */
2805 if (ic->op == CAST &&
2806 IS_SYMOP(IC_RIGHT(ic)) &&
2807 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2808 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1) {
2809 sym_link *to_type = operandType(IC_LEFT(ic));
2810 sym_link *from_type = operandType(IC_RIGHT(ic));
2811 if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2812 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2813 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2814 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2818 /* if this is a +/- operation with a rematerizable
2819 then mark this as rematerializable as well */
2820 if ((ic->op == '+' || ic->op == '-') &&
2821 (IS_SYMOP (IC_LEFT (ic)) &&
2822 IS_ITEMP (IC_RESULT (ic)) &&
2823 IS_OP_LITERAL (IC_RIGHT (ic))) &&
2824 OP_SYMBOL (IC_LEFT (ic))->remat &&
2825 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2826 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2828 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2829 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2830 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2833 /* mark the pointer usages */
2834 if (POINTER_SET (ic))
2835 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2837 if (POINTER_GET (ic) &&
2838 IS_SYMOP(IC_LEFT (ic)))
2839 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2844 /* if we are using a symbol on the stack
2845 then we should say hc08_ptrRegReq */
2846 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2847 hc08_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2848 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2849 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2850 hc08_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2851 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2854 if (IS_SYMOP (IC_LEFT (ic)))
2855 hc08_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2856 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2857 if (IS_SYMOP (IC_RIGHT (ic)))
2858 hc08_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2859 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2860 if (IS_SYMOP (IC_RESULT (ic)))
2861 hc08_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2862 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2867 /* if the condition of an if instruction
2868 is defined in the previous instruction and
2869 this is the only usage then
2870 mark the itemp as a conditional */
2871 if ((IS_CONDITIONAL (ic) ||
2872 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2873 ic->next && ic->next->op == IFX &&
2874 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2875 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2876 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2878 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2882 /* reduce for support function calls */
2883 if (ic->supportRtn || (ic->op != IFX && ic->op != JUMPTABLE))
2884 packRegsForSupport (ic, ebp);
2887 /* some cases the redundant moves can
2888 can be eliminated for return statements */
2889 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
2890 /* !isOperandInFarSpace (IC_LEFT (ic)) && */
2891 options.model == MODEL_SMALL) {
2892 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2895 /* if pointer set & left has a size more than
2896 one and right is not in far space */
2897 if (POINTER_SET (ic) &&
2898 /* !isOperandInFarSpace (IC_RIGHT (ic)) && */
2899 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2900 !IS_OP_RUONLY (IC_RIGHT (ic))
2901 /* && getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1 */ )
2902 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2904 /* if pointer get */
2905 if (POINTER_GET (ic) &&
2906 IS_SYMOP (IC_LEFT (ic)) &&
2907 /* !isOperandInFarSpace (IC_RESULT (ic)) && */
2908 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2909 !IS_OP_RUONLY (IC_RESULT (ic))
2910 /* && getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1 */)
2911 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2914 /* if this is cast for intergral promotion then
2915 check if only use of the definition of the
2916 operand being casted/ if yes then replace
2917 the result of that arithmetic operation with
2918 this result and get rid of the cast */
2921 sym_link *fromType = operandType (IC_RIGHT (ic));
2922 sym_link *toType = operandType (IC_LEFT (ic));
2924 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2925 getSize (fromType) != getSize (toType) &&
2926 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2929 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2932 if (IS_ARITHMETIC_OP (dic))
2934 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2935 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2936 remiCodeFromeBBlock (ebp, ic);
2937 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2938 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2939 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2943 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2949 /* if the type from and type to are the same
2950 then if this is the only use then packit */
2951 if (compareType (operandType (IC_RIGHT (ic)),
2952 operandType (IC_LEFT (ic))) == 1)
2954 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2957 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2958 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2959 remiCodeFromeBBlock (ebp, ic);
2960 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2961 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2962 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2971 iTempNN := (some variable in farspace) V1
2976 if (ic->op == IPUSH)
2978 packForPush (ic, ebpp, blockno);
2983 /* pack registers for accumulator use, when the
2984 result of an arithmetic or bit wise operation
2985 has only one use, that use is immediately following
2986 the defintion and the using iCode has only one
2987 operand or has two operands but one is literal &
2988 the result of that operation is not on stack then
2989 we can leave the result of this operation in x:a
2991 if ((IS_ARITHMETIC_OP (ic)
2992 || IS_CONDITIONAL(ic)
2993 || IS_BITWISE_OP (ic)
2997 || ic->op == GETHBIT
2998 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2999 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3000 || ic->op == RECEIVE
3002 IS_ITEMP (IC_RESULT (ic)) &&
3003 getSize (operandType (IC_RESULT (ic))) <= 1)
3005 packRegsForAccUse (ic);
3010 /*-----------------------------------------------------------------*/
3011 /* assignRegisters - assigns registers to each live range as need */
3012 /*-----------------------------------------------------------------*/
3014 hc08_assignRegisters (eBBlock ** ebbs, int count)
3019 setToNull ((void *) &_G.funcrUsed);
3020 setToNull ((void *) &_G.regAssigned);
3021 setToNull ((void *) &_G.totRegAssigned);
3022 hc08_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3024 hc08_reg_a = hc08_regWithIdx(A_IDX);
3025 hc08_reg_x = hc08_regWithIdx(X_IDX);
3026 hc08_reg_h = hc08_regWithIdx(H_IDX);
3027 hc08_reg_hx = hc08_regWithIdx(HX_IDX);
3028 hc08_reg_xa = hc08_regWithIdx(XA_IDX);
3030 /* change assignments this will remove some
3031 live ranges reducing some register pressure */
3032 for (i = 0; i < count; i++)
3033 packRegisters (ebbs, i);
3035 /* liveranges probably changed by register packing
3036 so we compute them again */
3037 recomputeLiveRanges (ebbs, count);
3039 if (options.dump_pack)
3040 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3042 /* first determine for each live range the number of
3043 registers & the type of registers required for each */
3046 /* and serially allocate registers */
3047 serialRegAssign (ebbs, count);
3050 //setToNull ((void *) &_G.regAssigned);
3051 //setToNull ((void *) &_G.totRegAssigned);
3054 /* if stack was extended then tell the user */
3057 /* werror(W_TOOMANY_SPILS,"stack", */
3058 /* _G.stackExtend,currFunc->name,""); */
3064 /* werror(W_TOOMANY_SPILS,"data space", */
3065 /* _G.dataExtend,currFunc->name,""); */
3069 /* after that create the register mask
3070 for each of the instruction */
3071 createRegMask (ebbs, count);
3073 /* redo that offsets for stacked automatic variables */
3075 redoStackOffsets ();
3078 if (options.dump_rassgn)
3080 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3081 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3084 /* do the overlaysegment stuff SDCCmem.c */
3085 doOverlays (ebbs, count);
3087 /* now get back the chain */
3088 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3092 /* free up any _G.stackSpil locations allocated */
3093 applyToSet (_G.stackSpil, deallocStackSpil);
3095 setToNull ((void **) &_G.stackSpil);
3096 setToNull ((void **) &_G.spiltSet);
3097 /* mark all registers as free */