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 gen51Code (iCode *);
48 bitVect *totRegAssigned; /* final set of LRs that got into registers */
51 bitVect *funcrUsed; /* registers used in a function */
54 bitVect *allBitregs; /* all bit registers */
58 /* Shared with gen.c */
59 int mcs51_ptrRegReq; /* one byte pointer register required */
65 {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
66 {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
67 {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
68 {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
69 {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
70 {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
71 {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
72 {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
73 {REG_BIT, B0_IDX, REG_BIT, "b0", "b0", "bits", 0, 1},
74 {REG_BIT, B1_IDX, REG_BIT, "b1", "b1", "bits", 1, 1},
75 {REG_BIT, B2_IDX, REG_BIT, "b2", "b2", "bits", 2, 1},
76 {REG_BIT, B3_IDX, REG_BIT, "b3", "b3", "bits", 3, 1},
77 {REG_BIT, B4_IDX, REG_BIT, "b4", "b4", "bits", 4, 1},
78 {REG_BIT, B5_IDX, REG_BIT, "b5", "b5", "bits", 5, 1},
79 {REG_BIT, B6_IDX, REG_BIT, "b6", "b6", "bits", 6, 1},
80 {REG_BIT, B7_IDX, REG_BIT, "b7", "b7", "bits", 7, 1},
81 {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
82 {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
83 {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
84 {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
85 {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
86 {REG_CND, CND_IDX, REG_CND, "C", "psw", "0xd0", 0, 1},
87 {0, DPL_IDX, 0, "dpl", "dpl", "0x82", 0, 0},
88 {0, DPH_IDX, 0, "dph", "dph", "0x83", 0, 0},
89 {0, B_IDX, 0, "b", "b", "0xf0", 0, 0},
90 {0, A_IDX, 0, "a", "acc", "0xe0", 0, 0},
93 static void spillThis (symbol *);
94 static void freeAllRegs ();
96 /*-----------------------------------------------------------------*/
97 /* allocReg - allocates register of given type */
98 /*-----------------------------------------------------------------*/
100 allocReg (short type)
104 for (i = 0; i < mcs51_nRegs; i++)
107 /* if type is given as 0 then any
108 free register will do */
112 regs8051[i].isFree = 0;
115 bitVectSetBit (currFunc->regsUsed, i);
118 /* other wise look for specific type
120 if (regs8051[i].isFree &&
121 regs8051[i].type == type)
123 regs8051[i].isFree = 0;
126 bitVectSetBit (currFunc->regsUsed, i);
133 /*-----------------------------------------------------------------*/
134 /* allocThisReg - allocates a particular register (if free) */
135 /*-----------------------------------------------------------------*/
137 allocThisReg (regs * reg)
144 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, reg->rIdx);
150 /*-----------------------------------------------------------------*/
151 /* mcs51_regWithIdx - returns pointer to register with index number*/
152 /*-----------------------------------------------------------------*/
154 mcs51_regWithIdx (int idx)
158 for (i = 0; i < sizeof(regs8051)/sizeof(regs); i++)
159 if (regs8051[i].rIdx == idx)
162 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
163 "regWithIdx not found");
167 /*-----------------------------------------------------------------*/
168 /* freeReg - frees a register */
169 /*-----------------------------------------------------------------*/
175 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
176 "freeReg - Freeing NULL register");
184 /*-----------------------------------------------------------------*/
185 /* nFreeRegs - returns number of free registers */
186 /*-----------------------------------------------------------------*/
193 for (i = 0; i < mcs51_nRegs; i++)
194 if (regs8051[i].isFree && regs8051[i].type == type)
199 /*-----------------------------------------------------------------*/
200 /* nfreeRegsType - free registers with type */
201 /*-----------------------------------------------------------------*/
203 nfreeRegsType (int type)
208 if ((nfr = nFreeRegs (type)) == 0)
209 return nFreeRegs (REG_GPR);
212 return nFreeRegs (type);
215 /*-----------------------------------------------------------------*/
216 /* useReg - marks a register as used */
217 /*-----------------------------------------------------------------*/
224 /*-----------------------------------------------------------------*/
225 /* computeSpillable - given a point find the spillable live ranges */
226 /*-----------------------------------------------------------------*/
228 computeSpillable (iCode * ic)
232 /* spillable live ranges are those that are live at this
233 point . the following categories need to be subtracted
235 a) - those that are already spilt
236 b) - if being used by this one
237 c) - defined by this one */
239 spillable = bitVectCopy (ic->rlive);
241 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
243 bitVectCplAnd (spillable, ic->uses); /* used in this one */
244 bitVectUnSetBit (spillable, ic->defKey);
245 spillable = bitVectIntersect (spillable, _G.regAssigned);
250 /*-----------------------------------------------------------------*/
251 /* noSpilLoc - return true if a variable has no spil location */
252 /*-----------------------------------------------------------------*/
254 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
256 return (sym->usl.spillLoc ? 0 : 1);
259 /*-----------------------------------------------------------------*/
260 /* hasSpilLoc - will return 1 if the symbol has spil location */
261 /*-----------------------------------------------------------------*/
263 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
265 return (sym->usl.spillLoc ? 1 : 0);
268 /*-----------------------------------------------------------------*/
269 /* directSpilLoc - will return 1 if the splilocation is in direct */
270 /*-----------------------------------------------------------------*/
272 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
274 if (sym->usl.spillLoc &&
275 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
281 /*-----------------------------------------------------------------*/
282 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
283 /* but is not used as a pointer */
284 /*-----------------------------------------------------------------*/
286 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
288 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
291 /*-----------------------------------------------------------------*/
292 /* rematable - will return 1 if the remat flag is set */
293 /*-----------------------------------------------------------------*/
295 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
300 /*-----------------------------------------------------------------*/
301 /* notUsedInRemaining - not used or defined in remain of the block */
302 /*-----------------------------------------------------------------*/
304 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
306 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
307 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
310 /*-----------------------------------------------------------------*/
311 /* allLRs - return true for all */
312 /*-----------------------------------------------------------------*/
314 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
319 /*-----------------------------------------------------------------*/
320 /* liveRangesWith - applies function to a given set of live range */
321 /*-----------------------------------------------------------------*/
323 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
324 eBBlock * ebp, iCode * ic)
329 if (!lrs || !lrs->size)
332 for (i = 1; i < lrs->size; i++)
335 if (!bitVectBitValue (lrs, i))
338 /* if we don't find it in the live range
339 hash table we are in serious trouble */
340 if (!(sym = hTabItemWithKey (liveRanges, i)))
342 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
343 "liveRangesWith could not find liveRange");
347 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
348 addSetHead (&rset, sym);
355 /*-----------------------------------------------------------------*/
356 /* leastUsedLR - given a set determines which is the least used */
357 /*-----------------------------------------------------------------*/
359 leastUsedLR (set * sset)
361 symbol *sym = NULL, *lsym = NULL;
363 sym = lsym = setFirstItem (sset);
368 for (; lsym; lsym = setNextItem (sset))
371 /* if usage is the same then prefer
372 the spill the smaller of the two */
373 if (lsym->used == sym->used)
374 if (getSize (lsym->type) < getSize (sym->type))
378 if (lsym->used < sym->used)
383 setToNull ((void *) &sset);
388 /*-----------------------------------------------------------------*/
389 /* noOverLap - will iterate through the list looking for over lap */
390 /*-----------------------------------------------------------------*/
392 noOverLap (set * itmpStack, symbol * fsym)
396 for (sym = setFirstItem (itmpStack); sym;
397 sym = setNextItem (itmpStack))
399 if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
404 /*-----------------------------------------------------------------*/
405 /* isFree - will return 1 if the a free spil location is found */
406 /*-----------------------------------------------------------------*/
411 V_ARG (symbol **, sloc);
412 V_ARG (symbol *, fsym);
414 /* if already found */
418 /* if it is free && and the itmp assigned to
419 this does not have any overlapping live ranges
420 with the one currently being assigned and
421 the size can be accomodated */
423 noOverLap (sym->usl.itmpStack, fsym) &&
424 getSize (sym->type) >= getSize (fsym->type))
433 /*-----------------------------------------------------------------*/
434 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
435 /*-----------------------------------------------------------------*/
437 spillLRWithPtrReg (symbol * forSym)
443 if (!_G.regAssigned ||
444 bitVectIsZero (_G.regAssigned))
447 r0 = mcs51_regWithIdx (R0_IDX);
448 r1 = mcs51_regWithIdx (R1_IDX);
450 /* for all live ranges */
451 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
452 lrsym = hTabNextItem (liveRanges, &k))
456 /* if no registers assigned to it or spilt */
457 /* if it does not overlap this then
458 no need to spill it */
460 if (lrsym->isspilt || !lrsym->nRegs ||
461 (lrsym->liveTo < forSym->liveFrom))
464 /* go thru the registers : if it is either
465 r0 or r1 then spill it */
466 for (j = 0; j < lrsym->nRegs; j++)
467 if (lrsym->regs[j] == r0 ||
468 lrsym->regs[j] == r1)
477 /*-----------------------------------------------------------------*/
478 /* createStackSpil - create a location on the stack to spil */
479 /*-----------------------------------------------------------------*/
481 createStackSpil (symbol * sym)
484 int useXstack, model;
488 /* first go try and find a free one that is already
489 existing on the stack */
490 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
492 /* found a free one : just update & return */
493 sym->usl.spillLoc = sloc;
496 addSetHead (&sloc->usl.itmpStack, sym);
500 /* could not then have to create one , this is the hard part
501 we need to allocate this on the stack : this is really a
502 hack!! but cannot think of anything better at this time */
504 if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
505 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
507 fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
512 sloc = newiTemp (slocBuffer);
514 /* set the type to the spilling symbol */
515 sloc->type = copyLinkChain (sym->type);
516 sloc->etype = getSpec (sloc->type);
517 if (!IS_BIT (sloc->etype))
519 SPEC_SCLS (sloc->etype) = S_DATA;
521 SPEC_EXTR (sloc->etype) = 0;
522 SPEC_STAT (sloc->etype) = 0;
523 SPEC_VOLATILE(sloc->etype) = 0;
524 SPEC_ABSA(sloc->etype) = 0;
526 /* we don't allow it to be allocated`
527 onto the external stack since : so we
528 temporarily turn it off ; we also
529 turn off memory model to prevent
530 the spil from going to the external storage
533 useXstack = options.useXstack;
534 model = options.model;
535 /* noOverlay = options.noOverlay; */
536 /* options.noOverlay = 1; */
537 options.model = options.useXstack = 0;
541 options.useXstack = useXstack;
542 options.model = model;
543 /* options.noOverlay = noOverlay; */
544 sloc->isref = 1; /* to prevent compiler warning */
546 /* if it is on the stack then update the stack */
547 if (IN_STACK (sloc->etype))
549 currFunc->stack += getSize (sloc->type);
550 _G.stackExtend += getSize (sloc->type);
553 _G.dataExtend += getSize (sloc->type);
555 /* add it to the _G.stackSpil set */
556 addSetHead (&_G.stackSpil, sloc);
557 sym->usl.spillLoc = sloc;
560 /* add it to the set of itempStack set
561 of the spill location */
562 addSetHead (&sloc->usl.itmpStack, sym);
566 /*-----------------------------------------------------------------*/
567 /* isSpiltOnStack - returns true if the spil location is on stack */
568 /*-----------------------------------------------------------------*/
570 isSpiltOnStack (symbol * sym)
580 /* if (sym->_G.stackSpil) */
583 if (!sym->usl.spillLoc)
586 etype = getSpec (sym->usl.spillLoc->type);
587 if (IN_STACK (etype))
593 /*-----------------------------------------------------------------*/
594 /* spillThis - spils a specific operand */
595 /*-----------------------------------------------------------------*/
597 spillThis (symbol * sym)
600 /* if this is rematerializable or has a spillLocation
601 we are okay, else we need to create a spillLocation
603 if (!(sym->remat || sym->usl.spillLoc))
604 createStackSpil (sym);
606 /* mark it has spilt & put it in the spilt set */
607 sym->isspilt = sym->spillA = 1;
608 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
610 bitVectUnSetBit (_G.regAssigned, sym->key);
611 bitVectUnSetBit (_G.totRegAssigned, sym->key);
613 for (i = 0; i < sym->nRegs; i++)
617 freeReg (sym->regs[i]);
621 /* if spilt on stack then free up r0 & r1
622 if they could have been assigned to some
624 if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
627 spillLRWithPtrReg (sym);
630 if (sym->usl.spillLoc && !sym->remat)
631 sym->usl.spillLoc->allocreq++;
635 /*-----------------------------------------------------------------*/
636 /* selectSpil - select a iTemp to spil : rather a simple procedure */
637 /*-----------------------------------------------------------------*/
639 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
641 bitVect *lrcs = NULL;
645 /* get the spillable live ranges */
646 lrcs = computeSpillable (ic);
648 /* get all live ranges that are rematerizable */
649 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
652 /* return the least used of these */
653 return leastUsedLR (selectS);
656 /* get live ranges with spillLocations in direct space */
657 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
659 sym = leastUsedLR (selectS);
660 strncpyz (sym->rname,
661 sym->usl.spillLoc->rname[0] ?
662 sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
665 /* mark it as allocation required */
666 sym->usl.spillLoc->allocreq++;
670 /* if the symbol is local to the block then */
671 if (forSym->liveTo < ebp->lSeq)
674 /* check if there are any live ranges allocated
675 to registers that are not used in this block */
676 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
678 sym = leastUsedLR (selectS);
679 /* if this is not rematerializable */
688 /* check if there are any live ranges that not
689 used in the remainder of the block */
691 !isiCodeInFunctionCall (ic) &&
692 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
694 sym = leastUsedLR (selectS);
707 /* find live ranges with spillocation && not used as pointers */
708 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
711 sym = leastUsedLR (selectS);
712 /* mark this as allocation required */
713 sym->usl.spillLoc->allocreq++;
717 /* find live ranges with spillocation */
718 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
721 sym = leastUsedLR (selectS);
722 sym->usl.spillLoc->allocreq++;
726 /* couldn't find then we need to create a spil
727 location on the stack , for which one? the least
729 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
732 /* return a created spil location */
733 sym = createStackSpil (leastUsedLR (selectS));
734 sym->usl.spillLoc->allocreq++;
738 /* this is an extreme situation we will spill
739 this one : happens very rarely but it does happen */
745 /*-----------------------------------------------------------------*/
746 /* spilSomething - spil some variable & mark registers as free */
747 /*-----------------------------------------------------------------*/
749 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
754 /* get something we can spil */
755 ssym = selectSpil (ic, ebp, forSym);
757 /* mark it as spilt */
758 ssym->isspilt = ssym->spillA = 1;
759 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
761 /* mark it as not register assigned &
762 take it away from the set */
763 bitVectUnSetBit (_G.regAssigned, ssym->key);
764 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
766 /* mark the registers as free */
767 for (i = 0; i < ssym->nRegs; i++)
769 freeReg (ssym->regs[i]);
771 /* if spilt on stack then free up r0 & r1
772 if they could have been assigned to as gprs */
773 if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
776 spillLRWithPtrReg (ssym);
779 /* if this was a block level spil then insert push & pop
780 at the start & end of block respectively */
783 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
784 /* add push to the start of the block */
785 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
786 ebp->sch->next : ebp->sch));
787 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
788 /* add pop to the end of the block */
789 addiCodeToeBBlock (ebp, nic, NULL);
792 /* if spilt because not used in the remainder of the
793 block then add a push before this instruction and
794 a pop at the end of the block */
795 if (ssym->remainSpil)
798 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
799 /* add push just before this instruction */
800 addiCodeToeBBlock (ebp, nic, ic);
802 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
803 /* add pop to the end of the block */
804 addiCodeToeBBlock (ebp, nic, NULL);
813 /*-----------------------------------------------------------------*/
814 /* getRegPtr - will try for PTR if not a GPR type if not spil */
815 /*-----------------------------------------------------------------*/
817 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
823 /* try for a ptr type */
824 if ((reg = allocReg (REG_PTR)))
827 /* try for gpr type */
828 if ((reg = allocReg (REG_GPR)))
831 /* we have to spil */
832 if (!spilSomething (ic, ebp, sym))
835 /* make sure partially assigned registers aren't reused */
836 for (j=0; j<=sym->nRegs; j++)
838 sym->regs[j]->isFree = 0;
840 /* this looks like an infinite loop but
841 in really selectSpil will abort */
845 /*-----------------------------------------------------------------*/
846 /* getRegGpr - will try for GPR if not spil */
847 /*-----------------------------------------------------------------*/
849 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
855 /* try for gpr type */
856 if ((reg = allocReg (REG_GPR)))
859 if (!mcs51_ptrRegReq)
860 if ((reg = allocReg (REG_PTR)))
863 /* we have to spil */
864 if (!spilSomething (ic, ebp, sym))
867 /* make sure partially assigned registers aren't reused */
868 for (j=0; j<=sym->nRegs; j++)
870 sym->regs[j]->isFree = 0;
872 /* this looks like an infinite loop but
873 in really selectSpil will abort */
877 /*-----------------------------------------------------------------*/
878 /* getRegBit - will try for Bit if not spill this */
879 /*-----------------------------------------------------------------*/
880 static regs *getRegBit (symbol * sym)
884 /* try for a bit type */
885 if ((reg = allocReg (REG_BIT)))
892 /*-----------------------------------------------------------------*/
893 /* getRegPtrNoSpil - get it cannot be spilt */
894 /*-----------------------------------------------------------------*/
895 static regs *getRegPtrNoSpil()
899 /* try for a ptr type */
900 if ((reg = allocReg (REG_PTR)))
903 /* try for gpr type */
904 if ((reg = allocReg (REG_GPR)))
909 /* just to make the compiler happy */
913 /*-----------------------------------------------------------------*/
914 /* getRegGprNoSpil - get it cannot be spilt */
915 /*-----------------------------------------------------------------*/
916 static regs *getRegGprNoSpil()
920 if ((reg = allocReg (REG_GPR)))
923 if (!mcs51_ptrRegReq)
924 if ((reg = allocReg (REG_PTR)))
929 /* just to make the compiler happy */
933 /*-----------------------------------------------------------------*/
934 /* getRegBitNoSpil - get it cannot be spilt */
935 /*-----------------------------------------------------------------*/
936 static regs *getRegBitNoSpil()
940 /* try for a ptr type */
941 if ((reg = allocReg (REG_BIT)))
944 /* try for gpr type */
945 if ((reg = allocReg (REG_GPR)))
950 /* just to make the compiler happy */
954 /*-----------------------------------------------------------------*/
955 /* symHasReg - symbol has a given register */
956 /*-----------------------------------------------------------------*/
958 symHasReg (symbol * sym, regs * reg)
962 for (i = 0; i < sym->nRegs; i++)
963 if (sym->regs[i] == reg)
969 /*-----------------------------------------------------------------*/
970 /* updateRegUsage - update the registers in use at the start of */
972 /*-----------------------------------------------------------------*/
974 updateRegUsage (iCode * ic)
978 for (reg=0; reg<mcs51_nRegs; reg++)
980 if (regs8051[reg].isFree)
982 ic->riu &= ~(1<<regs8051[reg].offset);
986 ic->riu |= (1<<regs8051[reg].offset);
991 /*-----------------------------------------------------------------*/
992 /* deassignLRs - check the live to and if they have registers & are */
993 /* not spilt then free up the registers */
994 /*-----------------------------------------------------------------*/
996 deassignLRs (iCode * ic, eBBlock * ebp)
1002 for (sym = hTabFirstItem (liveRanges, &k); sym;
1003 sym = hTabNextItem (liveRanges, &k))
1006 symbol *psym = NULL;
1007 /* if it does not end here */
1008 if (sym->liveTo > ic->seq)
1011 /* if it was spilt on stack then we can
1012 mark the stack spil location as free */
1017 sym->usl.spillLoc->isFree = 1;
1023 if (!bitVectBitValue (_G.regAssigned, sym->key))
1026 /* special case check if this is an IFX &
1027 the privious one was a pop and the
1028 previous one was not spilt then keep track
1030 if (ic->op == IFX && ic->prev &&
1031 ic->prev->op == IPOP &&
1032 !ic->prev->parmPush &&
1033 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1034 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1040 bitVectUnSetBit (_G.regAssigned, sym->key);
1042 /* if the result of this one needs registers
1043 and does not have it then assign it right
1045 if (IC_RESULT (ic) &&
1046 !(SKIP_IC2 (ic) || /* not a special icode */
1047 ic->op == JUMPTABLE ||
1052 POINTER_SET (ic)) &&
1053 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1054 result->liveTo > ic->seq && /* and will live beyond this */
1055 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1056 result->liveFrom == ic->seq && /* does not start before here */
1057 result->regType == sym->regType && /* same register types */
1058 result->nRegs && /* which needs registers */
1059 !result->isspilt && /* and does not already have them */
1061 !bitVectBitValue (_G.regAssigned, result->key) &&
1062 /* the number of free regs + number of regs in this LR
1063 can accomodate the what result Needs */
1064 ((nfreeRegsType (result->regType) +
1065 sym->nRegs) >= result->nRegs)
1069 for (i = 0; i < result->nRegs; i++)
1071 result->regs[i] = sym->regs[i];
1073 result->regs[i] = getRegGpr (ic, ebp, result);
1075 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1076 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1080 /* free the remaining */
1081 for (; i < sym->nRegs; i++)
1085 if (!symHasReg (psym, sym->regs[i]))
1086 freeReg (sym->regs[i]);
1089 freeReg (sym->regs[i]);
1096 /*-----------------------------------------------------------------*/
1097 /* reassignLR - reassign this to registers */
1098 /*-----------------------------------------------------------------*/
1100 reassignLR (operand * op)
1102 symbol *sym = OP_SYMBOL (op);
1105 /* not spilt any more */
1106 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1107 bitVectUnSetBit (_G.spiltSet, sym->key);
1109 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1110 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1114 for (i = 0; i < sym->nRegs; i++)
1115 sym->regs[i]->isFree = 0;
1118 /*-----------------------------------------------------------------*/
1119 /* willCauseSpill - determines if allocating will cause a spill */
1120 /*-----------------------------------------------------------------*/
1122 willCauseSpill (int nr, int rt)
1124 /* first check if there are any available registers
1125 of the type required */
1128 /* special case for pointer type
1129 if pointer type not avlb then
1130 check for type gpr */
1131 if (nFreeRegs (rt) >= nr)
1133 if (nFreeRegs (REG_GPR) >= nr)
1136 else if (rt == REG_BIT)
1138 if (nFreeRegs (rt) >= nr)
1143 if (mcs51_ptrRegReq)
1145 if (nFreeRegs (rt) >= nr)
1150 if (nFreeRegs (REG_PTR) +
1151 nFreeRegs (REG_GPR) >= nr)
1156 /* it will cause a spil */
1160 /*-----------------------------------------------------------------*/
1161 /* positionRegs - the allocator can allocate same registers to res- */
1162 /* ult and operand, if this happens make sure they are in the same */
1163 /* position as the operand otherwise chaos results */
1164 /*-----------------------------------------------------------------*/
1166 positionRegs (symbol * result, symbol * opsym)
1168 int count = min (result->nRegs, opsym->nRegs);
1169 int i, j = 0, shared = 0;
1172 /* if the result has been spilt then cannot share */
1177 /* first make sure that they actually share */
1178 for (i = 0; i < count; i++)
1180 for (j = 0; j < count; j++)
1182 if (result->regs[i] == opsym->regs[j] && i != j)
1192 regs *tmp = result->regs[i];
1193 result->regs[i] = result->regs[j];
1194 result->regs[j] = tmp;
1201 /*------------------------------------------------------------------*/
1202 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1203 /* it should either have registers or have beed spilled. Otherwise, */
1204 /* there was an uninitialized variable, so just spill this to get */
1205 /* the operand in a valid state. */
1206 /*------------------------------------------------------------------*/
1208 verifyRegsAssigned (operand *op, iCode * ic)
1213 if (!IS_ITEMP (op)) return;
1215 sym = OP_SYMBOL (op);
1216 if (sym->isspilt) return;
1217 if (!sym->nRegs) return;
1218 if (sym->regs[0]) return;
1220 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1221 sym->prereqv ? sym->prereqv->name : sym->name);
1226 /*-----------------------------------------------------------------*/
1227 /* serialRegAssign - serially allocate registers to the variables */
1228 /*-----------------------------------------------------------------*/
1230 serialRegAssign (eBBlock ** ebbs, int count)
1234 /* for all blocks */
1235 for (i = 0; i < count; i++)
1240 if (ebbs[i]->noPath &&
1241 (ebbs[i]->entryLabel != entryLabel &&
1242 ebbs[i]->entryLabel != returnLabel))
1245 /* for all instructions do */
1246 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1250 /* if this is an ipop that means some live
1251 range will have to be assigned again */
1253 reassignLR (IC_LEFT (ic));
1255 /* if result is present && is a true symbol */
1256 if (IC_RESULT (ic) && ic->op != IFX &&
1257 IS_TRUE_SYMOP (IC_RESULT (ic)))
1258 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1260 /* take away registers from live
1261 ranges that end at this instruction */
1262 deassignLRs (ic, ebbs[i]);
1264 /* some don't need registers */
1265 if (SKIP_IC2 (ic) ||
1266 ic->op == JUMPTABLE ||
1270 (IC_RESULT (ic) && POINTER_SET (ic)))
1273 /* now we need to allocate registers
1274 only for the result */
1275 if (IC_RESULT (ic)) {
1276 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1282 /* Make sure any spill location is definitely allocated */
1283 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1284 !sym->usl.spillLoc->allocreq)
1286 sym->usl.spillLoc->allocreq++;
1289 /* if it does not need or is spilt
1290 or is already assigned to registers
1291 or will not live beyond this instructions */
1294 bitVectBitValue (_G.regAssigned, sym->key) ||
1295 sym->liveTo <= ic->seq)
1298 /* if some liverange has been spilt at the block level
1299 and this one live beyond this block then spil this
1301 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1306 willCS = willCauseSpill (sym->nRegs, sym->regType);
1307 /* if this is a bit variable then don't use precious registers
1308 along with expensive bit-to-char conversions but just spill
1310 if (willCS && SPEC_NOUN(sym->etype) == V_BIT) {
1315 /* if trying to allocate this will cause
1316 a spill and there is nothing to spill
1317 or this one is rematerializable then
1319 spillable = computeSpillable (ic);
1320 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1325 /* If the live range preceeds the point of definition
1326 then ideally we must take into account registers that
1327 have been allocated after sym->liveFrom but freed
1328 before ic->seq. This is complicated, so spill this
1329 symbol instead and let fillGaps handle the allocation. */
1330 if (sym->liveFrom < ic->seq) {
1335 /* if it has a spillocation & is used less than
1336 all other live ranges then spill this */
1338 if (sym->usl.spillLoc) {
1339 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1340 allLRs, ebbs[i], ic));
1341 if (leastUsed && leastUsed->used > sym->used) {
1346 /* if none of the liveRanges have a spillLocation then better
1347 to spill this one than anything else already assigned to registers */
1348 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1349 /* if this is local to this block then we might find a block spil */
1350 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1357 /* if we need ptr regs for the right side
1359 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1360 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1364 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1365 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1369 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1370 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1375 /* else we assign registers to it */
1376 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1377 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1379 for (j = 0; j < sym->nRegs; j++) {
1380 sym->regs[j] = NULL;
1381 if (sym->regType == REG_PTR)
1382 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1383 else if (sym->regType == REG_BIT)
1384 sym->regs[j] = getRegBit (sym);
1387 if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1389 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1391 if (right->regs[j] && (right->regType != REG_BIT))
1392 sym->regs[j] = allocThisReg (right->regs[j]);
1395 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1398 /* if the allocation failed which means
1399 this was spilt then break */
1403 for (i=0; i < sym->nRegs ; i++ )
1404 sym->regs[i] = NULL;
1409 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1410 /* if it shares registers with operands make sure
1411 that they are in the same position */
1412 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1413 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1414 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1415 OP_SYMBOL (IC_LEFT (ic)));
1417 /* do the same for the right operand */
1418 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1419 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1420 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1421 OP_SYMBOL (IC_RIGHT (ic)));
1434 /* Check for and fix any problems with uninitialized operands */
1435 for (i = 0; i < count; i++)
1439 if (ebbs[i]->noPath &&
1440 (ebbs[i]->entryLabel != entryLabel &&
1441 ebbs[i]->entryLabel != returnLabel))
1444 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1451 verifyRegsAssigned (IC_COND (ic), ic);
1455 if (ic->op == JUMPTABLE)
1457 verifyRegsAssigned (IC_JTCOND (ic), ic);
1461 verifyRegsAssigned (IC_RESULT (ic), ic);
1462 verifyRegsAssigned (IC_LEFT (ic), ic);
1463 verifyRegsAssigned (IC_RIGHT (ic), ic);
1468 /*-----------------------------------------------------------------*/
1469 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1470 /*-----------------------------------------------------------------*/
1471 static void fillGaps()
1478 if (getenv("DISABLE_FILL_GAPS")) return;
1480 /* look for liveranges that were spilt by the allocator */
1481 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1482 sym = hTabNextItem(liveRanges,&key)) {
1487 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1489 /* find the liveRanges this one clashes with, that are
1490 still assigned to registers & mark the registers as used*/
1491 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1495 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1496 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1499 clr = hTabItemWithKey(liveRanges,i);
1502 /* mark these registers as used */
1503 for (k = 0 ; k < clr->nRegs ; k++ )
1504 useReg(clr->regs[k]);
1507 if (willCauseSpill(sym->nRegs,sym->regType)) {
1508 /* NOPE :( clear all registers & and continue */
1514 for (i = 0 ; i < sym->defs->size ; i++ )
1516 if (bitVectBitValue(sym->defs,i))
1518 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1525 D(printf("Attempting fillGaps on %s: [",sym->name));
1526 /* THERE IS HOPE !!!! */
1527 for (i=0; i < sym->nRegs ; i++ ) {
1528 if (sym->regType == REG_PTR)
1529 sym->regs[i] = getRegPtrNoSpil ();
1530 else if (sym->regType == REG_BIT)
1531 sym->regs[i] = getRegBitNoSpil ();
1534 sym->regs[i] = NULL;
1535 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1537 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1540 sym->regs[i] = allocThisReg (right->regs[i]);
1543 sym->regs[i] = getRegGprNoSpil ();
1545 D(printf("%s ", sym->regs[i]->name));
1549 /* For all its definitions check if the registers
1550 allocated needs positioning NOTE: we can position
1551 only ONCE if more than One positioning required
1553 We may need to perform the checks twice; once to
1554 position the registers as needed, the second to
1555 verify any register repositioning is still
1559 for (pass=0; pass<2; pass++) {
1560 D(printf(" checking definitions\n"));
1561 for (i = 0 ; i < sym->defs->size ; i++ ) {
1562 if (bitVectBitValue(sym->defs,i)) {
1563 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1564 D(printf(" ic->seq = %d\n", ic->seq));
1565 if (SKIP_IC(ic)) continue;
1566 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1567 /* if left is assigned to registers */
1568 if (IS_SYMOP(IC_LEFT(ic)))
1570 D(printf(" left = "));
1571 D(printOperand(IC_LEFT(ic),NULL));
1573 if (IS_SYMOP(IC_LEFT(ic)) &&
1574 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1575 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1577 if (IS_SYMOP(IC_RIGHT(ic)))
1579 D(printf(" right = "));
1580 D(printOperand(IC_RIGHT(ic),NULL));
1582 if (IS_SYMOP(IC_RIGHT(ic)) &&
1583 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1584 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1586 D(printf(" pdone = %d\n", pdone));
1587 if (pdone > 1) break;
1590 D(printf(" checking uses\n"));
1591 for (i = 0 ; i < sym->uses->size ; i++ ) {
1592 if (bitVectBitValue(sym->uses,i)) {
1594 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1595 D(printf(" ic->seq = %d\n", ic->seq));
1596 if (SKIP_IC(ic)) continue;
1597 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1599 /* if result is assigned to registers */
1600 if (IS_SYMOP(IC_RESULT(ic)))
1602 D(printf(" result = "));
1603 D(printOperand(IC_RESULT(ic),NULL));
1605 if (IS_SYMOP(IC_RESULT(ic)) &&
1606 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1607 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1609 D(printf(" pdone = %d\n", pdone));
1610 if (pdone > 1) break;
1613 if (pdone == 0) break; /* second pass only if regs repositioned */
1614 if (pdone > 1) break;
1616 D(printf(" sym->regs = ["));
1617 for (i=0; i < sym->nRegs ; i++ )
1618 D(printf("%s ", sym->regs[i]->name));
1620 /* had to position more than once GIVE UP */
1622 /* UNDO all the changes we made to try this */
1624 for (i=0; i < sym->nRegs ; i++ ) {
1625 sym->regs[i] = NULL;
1628 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1631 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1633 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1634 sym->isspilt = sym->spillA = 0 ;
1635 sym->usl.spillLoc->allocreq--;
1640 /*-----------------------------------------------------------------*/
1641 /* findAllBitregs :- returns bit vector of all bit registers */
1642 /*-----------------------------------------------------------------*/
1644 findAllBitregs (void)
1646 bitVect *rmask = newBitVect (mcs51_nRegs);
1649 for (j = 0; j < mcs51_nRegs; j++)
1651 if (regs8051[j].type == REG_BIT)
1652 rmask = bitVectSetBit (rmask, regs8051[j].rIdx);
1658 /*-----------------------------------------------------------------*/
1659 /* mcs51_allBitregs :- returns bit vector of all bit registers */
1660 /*-----------------------------------------------------------------*/
1662 mcs51_allBitregs (void)
1664 return _G.allBitregs;
1667 /*-----------------------------------------------------------------*/
1668 /* rUmaskForOp :- returns register mask for an operand */
1669 /*-----------------------------------------------------------------*/
1671 mcs51_rUmaskForOp (operand * op)
1677 /* only temporaries are assigned registers */
1681 sym = OP_SYMBOL (op);
1683 /* if spilt or no registers assigned to it
1685 if (sym->isspilt || !sym->nRegs)
1688 rumask = newBitVect (mcs51_nRegs);
1690 for (j = 0; j < sym->nRegs; j++)
1692 if (sym->regs[j]) /* EEP - debug */
1693 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1699 /*-----------------------------------------------------------------*/
1700 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1701 /*-----------------------------------------------------------------*/
1703 regsUsedIniCode (iCode * ic)
1705 bitVect *rmask = newBitVect (mcs51_nRegs);
1707 /* do the special cases first */
1710 rmask = bitVectUnion (rmask,
1711 mcs51_rUmaskForOp (IC_COND (ic)));
1715 /* for the jumptable */
1716 if (ic->op == JUMPTABLE)
1718 rmask = bitVectUnion (rmask,
1719 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1724 /* of all other cases */
1726 rmask = bitVectUnion (rmask,
1727 mcs51_rUmaskForOp (IC_LEFT (ic)));
1731 rmask = bitVectUnion (rmask,
1732 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1735 rmask = bitVectUnion (rmask,
1736 mcs51_rUmaskForOp (IC_RESULT (ic)));
1742 /*-----------------------------------------------------------------*/
1743 /* createRegMask - for each instruction will determine the regsUsed */
1744 /*-----------------------------------------------------------------*/
1746 createRegMask (eBBlock ** ebbs, int count)
1750 /* for all blocks */
1751 for (i = 0; i < count; i++)
1755 if (ebbs[i]->noPath &&
1756 (ebbs[i]->entryLabel != entryLabel &&
1757 ebbs[i]->entryLabel != returnLabel))
1760 /* for all instructions */
1761 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1766 if (SKIP_IC2 (ic) || !ic->rlive)
1769 /* first mark the registers used in this
1771 ic->rUsed = regsUsedIniCode (ic);
1772 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1774 /* now create the register mask for those
1775 registers that are in use : this is a
1776 super set of ic->rUsed */
1777 ic->rMask = newBitVect (mcs51_nRegs + 1);
1779 /* for all live Ranges alive at this point */
1780 for (j = 1; j < ic->rlive->size; j++)
1785 /* if not alive then continue */
1786 if (!bitVectBitValue (ic->rlive, j))
1789 /* find the live range we are interested in */
1790 if (!(sym = hTabItemWithKey (liveRanges, j)))
1792 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1793 "createRegMask cannot find live range");
1794 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1798 /* if no register assigned to it */
1799 if (!sym->nRegs || sym->isspilt)
1802 /* for all the registers allocated to it */
1803 for (k = 0; k < sym->nRegs; k++)
1806 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1812 /*-----------------------------------------------------------------*/
1813 /* rematStr - returns the rematerialized string for a remat var */
1814 /*-----------------------------------------------------------------*/
1816 rematStr (symbol * sym)
1819 iCode *ic = sym->rematiCode;
1826 /* if plus or minus print the right hand side */
1827 if (ic->op == '+' || ic->op == '-')
1829 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1830 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1833 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1837 /* cast then continue */
1838 if (IS_CAST_ICODE(ic)) {
1839 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1842 /* we reached the end */
1843 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1844 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1851 /*-----------------------------------------------------------------*/
1852 /* regTypeNum - computes the type & number of registers required */
1853 /*-----------------------------------------------------------------*/
1855 regTypeNum (eBBlock *ebbs)
1861 /* for each live range do */
1862 for (sym = hTabFirstItem (liveRanges, &k); sym;
1863 sym = hTabNextItem (liveRanges, &k))
1866 /* if used zero times then no registers needed */
1867 if ((sym->liveTo - sym->liveFrom) == 0)
1871 /* if the live range is a temporary */
1875 /* if the type is marked as a conditional */
1876 if (sym->regType == REG_CND)
1879 /* if used in return only then we don't
1881 if (sym->ruonly || sym->accuse)
1883 if (IS_AGGREGATE (sym->type) || sym->isptr)
1884 sym->type = aggrToPtr (sym->type, FALSE);
1888 /* if the symbol has only one definition &
1889 that definition is a get_pointer */
1890 if (bitVectnBitsOn (sym->defs) == 1 &&
1891 (ic = hTabItemWithKey (iCodehTab,
1892 bitVectFirstBit (sym->defs))) &&
1894 !IS_BITVAR (sym->etype) &&
1895 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1898 if (ptrPseudoSymSafe (sym, ic))
1900 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1904 /* if in data space or idata space then try to
1905 allocate pointer register */
1909 /* if not then we require registers */
1910 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1911 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1912 getSize (sym->type));
1916 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1917 printTypeChain (sym->type, stderr);
1918 fprintf (stderr, "\n");
1921 /* determine the type of register required */
1922 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
1923 sym->regType = REG_PTR;
1924 else if (IS_BIT(sym->type))
1925 sym->regType = REG_BIT;
1927 sym->regType = REG_GPR;
1930 /* for the first run we don't provide */
1931 /* registers for true symbols we will */
1932 /* see how things go */
1938 /*-----------------------------------------------------------------*/
1939 /* freeAllRegs - mark all registers as free */
1940 /*-----------------------------------------------------------------*/
1946 for (i = 0; i < mcs51_nRegs; i++)
1947 regs8051[i].isFree = 1;
1950 /*-----------------------------------------------------------------*/
1951 /* deallocStackSpil - this will set the stack pointer back */
1952 /*-----------------------------------------------------------------*/
1954 DEFSETFUNC (deallocStackSpil)
1962 /*-----------------------------------------------------------------*/
1963 /* farSpacePackable - returns the packable icode for far variables */
1964 /*-----------------------------------------------------------------*/
1966 farSpacePackable (iCode * ic)
1970 /* go thru till we find a definition for the
1971 symbol on the right */
1972 for (dic = ic->prev; dic; dic = dic->prev)
1974 /* if the definition is a call then no */
1975 if ((dic->op == CALL || dic->op == PCALL) &&
1976 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1981 /* if shift by unknown amount then not */
1982 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1983 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1986 /* if pointer get and size > 1 */
1987 if (POINTER_GET (dic) &&
1988 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1991 if (POINTER_SET (dic) &&
1992 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1997 if (IC_COND (dic) &&
1998 IS_TRUE_SYMOP (IC_COND (dic)) &&
1999 isOperandInFarSpace (IC_COND (dic)))
2002 else if (dic->op == JUMPTABLE)
2004 if (IC_JTCOND (dic) &&
2005 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
2006 isOperandInFarSpace (IC_JTCOND (dic)))
2011 /* if any tree is a true symbol in far space */
2012 if (IC_RESULT (dic) &&
2013 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2014 isOperandInFarSpace (IC_RESULT (dic)))
2017 if (IC_RIGHT (dic) &&
2018 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2019 isOperandInFarSpace (IC_RIGHT (dic)) &&
2020 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2023 if (IC_LEFT (dic) &&
2024 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2025 isOperandInFarSpace (IC_LEFT (dic)) &&
2026 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2030 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2032 if ((dic->op == LEFT_OP ||
2033 dic->op == RIGHT_OP ||
2035 IS_OP_LITERAL (IC_RIGHT (dic)))
2045 /*-----------------------------------------------------------------*/
2046 /* packRegsForAssign - register reduction for assignment */
2047 /*-----------------------------------------------------------------*/
2049 packRegsForAssign (iCode * ic, eBBlock * ebp)
2053 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2054 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2055 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2060 /* if the true symbol is defined in far space or on stack
2061 then we should not since this will increase register pressure */
2062 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2066 /* find the definition of iTempNN scanning backwards if we find a
2067 a use of the true symbol in before we find the definition then
2069 for (dic = ic->prev; dic; dic = dic->prev)
2071 int crossedCall = 0;
2073 /* We can pack across a function call only if it's a local */
2074 /* variable or our parameter. Never pack global variables */
2075 /* or parameters to a function we call. */
2076 if ((dic->op == CALL || dic->op == PCALL))
2078 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
2079 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
2090 if (IS_SYMOP (IC_COND (dic)) &&
2091 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
2092 IC_COND (dic)->key == IC_RIGHT (ic)->key))
2100 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2101 IS_OP_VOLATILE (IC_RESULT (dic)))
2107 if (IS_SYMOP (IC_RESULT (dic)) &&
2108 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2110 if (POINTER_SET (dic))
2116 if (IS_SYMOP (IC_RIGHT (dic)) &&
2117 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2118 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2124 if (IS_SYMOP (IC_LEFT (dic)) &&
2125 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2126 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2132 if (IS_SYMOP (IC_RESULT (dic)) &&
2133 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2149 return 0; /* did not find */
2151 /* if assignment then check that right is not a bit */
2152 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2154 sym_link *etype = operandType (IC_RESULT (dic));
2155 if (IS_BITFIELD (etype))
2157 /* if result is a bit too then it's ok */
2158 etype = operandType (IC_RESULT (ic));
2159 if (!IS_BITFIELD (etype))
2166 /* if assignment then check that right is not a bit */
2167 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2169 sym_link *etype = operandType (IC_RIGHT (dic));
2170 if (IS_BITFIELD (etype))
2172 /* if result is a bit too then it's ok */
2173 etype = operandType (IC_RESULT (dic));
2174 if (!IS_BITFIELD (etype))
2179 /* if the result is on stack or iaccess then it must be
2180 the same atleast one of the operands */
2181 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2182 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2185 /* the operation has only one symbol
2186 operator then we can pack */
2187 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2188 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2191 if (!((IC_LEFT (dic) &&
2192 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2194 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2198 /* found the definition */
2199 /* replace the result with the result of */
2200 /* this assignment and remove this assignment */
2201 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2202 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2204 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2206 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2208 // TODO: and the otherway around?
2210 /* delete from liverange table also
2211 delete from all the points inbetween and the new
2213 for (sic = dic; sic != ic; sic = sic->next)
2215 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2216 if (IS_ITEMP (IC_RESULT (dic)))
2217 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2220 remiCodeFromeBBlock (ebp, ic);
2221 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2222 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2223 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2227 /*------------------------------------------------------------------*/
2228 /* findAssignToSym : scanning backwards looks for first assig found */
2229 /*------------------------------------------------------------------*/
2231 findAssignToSym (operand * op, iCode * ic)
2235 /* This routine is used to find sequences like
2237 ...; (intervening ops don't use iTempAA or modify FOO)
2238 blah = blah + iTempAA;
2240 and eliminate the use of iTempAA, freeing up its register for
2244 for (dic = ic->prev; dic; dic = dic->prev)
2247 /* if definition by assignment */
2248 if (dic->op == '=' &&
2249 !POINTER_SET (dic) &&
2250 IC_RESULT (dic)->key == op->key
2251 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2253 break; /* found where this temp was defined */
2255 /* if we find an usage then we cannot delete it */
2259 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2262 else if (dic->op == JUMPTABLE)
2264 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2269 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2272 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2275 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2281 return NULL; /* didn't find any assignment to op */
2283 /* we are interested only if defined in far space */
2284 /* or in stack space in case of + & - */
2286 /* if assigned to a non-symbol then don't repack regs */
2287 if (!IS_SYMOP (IC_RIGHT (dic)))
2290 /* if the symbol is volatile then we should not */
2291 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2293 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2294 What does it mean for an iTemp to be volatile, anyway? Passing
2295 TRUE is more cautious but may prevent possible optimizations */
2297 /* if the symbol is in far space then we should not */
2298 if (isOperandInFarSpace (IC_RIGHT (dic)))
2301 /* for + & - operations make sure that
2302 if it is on the stack it is the same
2303 as one of the three operands */
2304 if ((ic->op == '+' || ic->op == '-') &&
2305 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2308 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2309 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2310 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2314 /* now make sure that the right side of dic
2315 is not defined between ic & dic */
2318 iCode *sic = dic->next;
2320 for (; sic != ic; sic = sic->next)
2321 if (IC_RESULT (sic) &&
2322 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2329 /*-----------------------------------------------------------------*/
2330 /* reassignAliasedSym - used by packRegsForSupport to replace */
2331 /* redundant iTemp with equivalent symbol */
2332 /*-----------------------------------------------------------------*/
2334 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2337 unsigned oldSymKey, newSymKey;
2339 oldSymKey = op->key;
2340 newSymKey = IC_RIGHT(assignment)->key;
2342 /* only track live ranges of compiler-generated temporaries */
2343 if (!IS_ITEMP(IC_RIGHT(assignment)))
2346 /* update the live-value bitmaps */
2347 for (ic = assignment; ic != use; ic = ic->next) {
2348 bitVectUnSetBit (ic->rlive, oldSymKey);
2350 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2353 /* update the sym of the used operand */
2354 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2355 op->key = OP_SYMBOL(op)->key;
2356 OP_SYMBOL(op)->accuse = 0;
2358 /* update the sym's liverange */
2359 if ( OP_LIVETO(op) < ic->seq )
2360 setToRange(op, ic->seq, FALSE);
2362 /* remove the assignment iCode now that its result is unused */
2363 remiCodeFromeBBlock (ebp, assignment);
2364 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2365 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2369 /*-----------------------------------------------------------------*/
2370 /* packRegsForSupport :- reduce some registers for support calls */
2371 /*-----------------------------------------------------------------*/
2373 packRegsForSupport (iCode * ic, eBBlock * ebp)
2377 /* for the left & right operand :- look to see if the
2378 left was assigned a true symbol in far space in that
2379 case replace them */
2381 if (IS_ITEMP (IC_LEFT (ic)) &&
2382 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2384 dic = findAssignToSym (IC_LEFT (ic), ic);
2388 /* found it we need to remove it from the block */
2389 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2394 /* do the same for the right operand */
2395 if (IS_ITEMP (IC_RIGHT (ic)) &&
2396 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2398 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2402 /* if this is a subtraction & the result
2403 is a true symbol in far space then don't pack */
2404 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2406 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2407 if (IN_FARSPACE (SPEC_OCLS (etype)))
2410 /* found it we need to remove it from the
2412 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2421 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2424 /*-----------------------------------------------------------------*/
2425 /* packRegsForOneuse : - will reduce some registers for single Use */
2426 /*-----------------------------------------------------------------*/
2428 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2432 /* if returning a literal then do nothing */
2436 /* if rematerializable or already return use then do nothing */
2437 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly)
2440 /* only upto 2 bytes since we cannot predict
2441 the usage of b, & acc */
2442 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2445 if (ic->op != RETURN &&
2447 !POINTER_SET (ic) &&
2451 if (ic->op == SEND && ic->argreg != 1) return NULL;
2453 /* this routine will mark the a symbol as used in one
2454 instruction use only && if the defintion is local
2455 (ie. within the basic block) && has only one definition &&
2456 that definition is either a return value from a
2457 function or does not contain any variables in
2459 if (bitVectnBitsOn (OP_USES (op)) > 1)
2462 /* if it has only one defintion */
2463 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2464 return NULL; /* has more than one definition */
2466 /* get that definition */
2468 hTabItemWithKey (iCodehTab,
2469 bitVectFirstBit (OP_DEFS (op)))))
2472 /* if that only usage is a cast */
2473 if (dic->op == CAST) {
2474 /* to a bigger type */
2475 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2476 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2477 /* than we can not, since we cannot predict the usage of b & acc */
2482 /* found the definition now check if it is local */
2483 if (dic->seq < ebp->fSeq ||
2484 dic->seq > ebp->lSeq)
2485 return NULL; /* non-local */
2487 /* now check if it is the return from
2489 if (dic->op == CALL || dic->op == PCALL)
2491 if (ic->op != SEND && ic->op != RETURN &&
2492 !POINTER_SET(ic) && !POINTER_GET(ic))
2494 OP_SYMBOL (op)->ruonly = 1;
2500 /* otherwise check that the definition does
2501 not contain any symbols in far space */
2502 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2503 isOperandInFarSpace (IC_RIGHT (dic)) ||
2504 IS_OP_RUONLY (IC_LEFT (ic)) ||
2505 IS_OP_RUONLY (IC_RIGHT (ic)))
2510 /* if pointer set then make sure the pointer
2512 if (POINTER_SET (dic) &&
2513 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2516 if (POINTER_GET (dic) &&
2517 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2521 /* Make sure no overlapping liverange is already assigned to DPTR */
2522 if (OP_SYMBOL(op)->clashes)
2527 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2529 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2531 sym = hTabItemWithKey(liveRanges,i);
2540 /* also make sure the intervenening instructions
2541 don't have any thing in far space */
2542 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2545 /* if there is an intervening function call then no */
2546 if (dic->op == CALL || dic->op == PCALL)
2548 /* if pointer set then make sure the pointer
2550 if (POINTER_SET (dic) &&
2551 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2554 if (POINTER_GET (dic) &&
2555 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2558 /* if address of & the result is remat the okay */
2559 if (dic->op == ADDRESS_OF &&
2560 OP_SYMBOL (IC_RESULT (dic))->remat)
2563 /* if operand has size of three or more & this
2564 operation is a '*','/' or '%' then 'b' may
2566 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2567 getSize (operandType (op)) >= 3)
2570 /* if left or right or result is in far space */
2571 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2572 isOperandInFarSpace (IC_RIGHT (dic)) ||
2573 isOperandInFarSpace (IC_RESULT (dic)) ||
2574 IS_OP_RUONLY (IC_LEFT (dic)) ||
2575 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2576 IS_OP_RUONLY (IC_RESULT (dic)))
2580 /* if left or right or result is on stack */
2581 if (isOperandOnStack(IC_LEFT(dic)) ||
2582 isOperandOnStack(IC_RIGHT(dic)) ||
2583 isOperandOnStack(IC_RESULT(dic))) {
2588 OP_SYMBOL (op)->ruonly = 1;
2592 /*-----------------------------------------------------------------*/
2593 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2594 /*-----------------------------------------------------------------*/
2596 isBitwiseOptimizable (iCode * ic)
2598 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2599 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2601 /* bitwise operations are considered optimizable
2602 under the following conditions (Jean-Louis VERN)
2614 if (IS_LITERAL(rtype) ||
2615 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2621 /*-----------------------------------------------------------------*/
2622 /* isCommutativeOp - tests whether this op cares what order its */
2623 /* operands are in */
2624 /*-----------------------------------------------------------------*/
2625 bool isCommutativeOp(unsigned int op)
2627 if (op == '+' || op == '*' || op == EQ_OP ||
2628 op == '^' || op == '|' || op == BITWISEAND)
2634 /*-----------------------------------------------------------------*/
2635 /* operandUsesAcc - determines whether the code generated for this */
2636 /* operand will have to use the accumulator */
2637 /*-----------------------------------------------------------------*/
2638 bool operandUsesAcc(operand *op)
2644 symbol *sym = OP_SYMBOL(op);
2648 return TRUE; /* duh! */
2650 if (IN_STACK(sym->etype) || sym->onStack ||
2651 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2652 return TRUE; /* acc is used to calc stack offset */
2657 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2659 return FALSE; /* more checks? */
2663 symspace = SPEC_OCLS(sym->etype);
2665 if (sym->iaccess && symspace->paged)
2666 return TRUE; /* must fetch paged indirect sym via accumulator */
2668 if (IN_BITSPACE(symspace))
2669 return TRUE; /* fetching bit vars uses the accumulator */
2671 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2672 return TRUE; /* fetched via accumulator and dptr */
2678 /*-----------------------------------------------------------------*/
2679 /* packRegsForAccUse - pack registers for acc use */
2680 /*-----------------------------------------------------------------*/
2682 packRegsForAccUse (iCode * ic)
2686 /* if this is an aggregate, e.g. a one byte char array */
2687 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2691 /* if we are calling a reentrant function that has stack parameters */
2692 if (ic->op == CALL &&
2693 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2694 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2697 if (ic->op == PCALL &&
2698 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2699 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2702 /* if + or - then it has to be one byte result */
2703 if ((ic->op == '+' || ic->op == '-')
2704 && getSize (operandType (IC_RESULT (ic))) > 1)
2707 /* if shift operation make sure right side is not a literal */
2708 if (ic->op == RIGHT_OP &&
2709 (isOperandLiteral (IC_RIGHT (ic)) ||
2710 getSize (operandType (IC_RESULT (ic))) > 1))
2713 if (ic->op == LEFT_OP &&
2714 (isOperandLiteral (IC_RIGHT (ic)) ||
2715 getSize (operandType (IC_RESULT (ic))) > 1))
2718 if (IS_BITWISE_OP (ic) &&
2719 getSize (operandType (IC_RESULT (ic))) > 1)
2723 /* has only one definition */
2724 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2727 /* has only one use */
2728 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2731 /* and the usage immediately follows this iCode */
2732 if (!(uic = hTabItemWithKey (iCodehTab,
2733 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2736 if (ic->next != uic)
2739 /* if it is a conditional branch then we definitely can */
2743 if (uic->op == JUMPTABLE)
2746 if (POINTER_SET (uic) &&
2747 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2750 /* if the usage is not is an assignment
2751 or an arithmetic / bitwise / shift operation then not */
2752 if (uic->op != '=' &&
2753 !IS_ARITHMETIC_OP (uic) &&
2754 !IS_BITWISE_OP (uic) &&
2755 uic->op != LEFT_OP &&
2756 uic->op != RIGHT_OP)
2759 /* if used in ^ operation then make sure right is not a
2760 literal (WIML: Why is this?) */
2761 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2764 /* if shift operation make sure right side is not a literal */
2765 /* WIML: Why is this? */
2766 if (uic->op == RIGHT_OP &&
2767 (isOperandLiteral (IC_RIGHT (uic)) ||
2768 getSize (operandType (IC_RESULT (uic))) > 1))
2770 if (uic->op == LEFT_OP &&
2771 (isOperandLiteral (IC_RIGHT (uic)) ||
2772 getSize (operandType (IC_RESULT (uic))) > 1))
2775 /* make sure that the result of this icode is not on the
2776 stack, since acc is used to compute stack offset */
2778 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2779 OP_SYMBOL (IC_RESULT (uic))->onStack)
2782 if (isOperandOnStack(IC_RESULT(uic)))
2786 /* if the usage has only one operand then we can */
2787 if (IC_LEFT (uic) == NULL ||
2788 IC_RIGHT (uic) == NULL)
2791 /* if the other operand uses the accumulator then we cannot */
2792 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2793 operandUsesAcc(IC_RIGHT(uic))) ||
2794 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2795 operandUsesAcc(IC_LEFT(uic))) )
2798 /* make sure this is on the left side if not commutative */
2799 /* except for '-', which has been written to be able to
2800 handle reversed operands */
2801 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2802 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2806 // this is too dangerous and need further restrictions
2809 /* if one of them is a literal then we can */
2810 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2811 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2813 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2819 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2823 /*-----------------------------------------------------------------*/
2824 /* packForPush - heuristics to reduce iCode for pushing */
2825 /*-----------------------------------------------------------------*/
2827 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2831 struct eBBlock * ebp=ebpp[blockno];
2833 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2836 /* must have only definition & one usage */
2837 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2838 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2841 /* find the definition */
2842 if (!(dic = hTabItemWithKey (iCodehTab,
2843 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2846 if (dic->op != '=' || POINTER_SET (dic))
2849 if (dic->seq < ebp->fSeq) { // Evelyn did this
2851 for (i=0; i<blockno; i++) {
2852 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2857 wassert (i!=blockno); // no way to recover from here
2860 if (IS_SYMOP(IC_RIGHT(dic))) {
2861 /* make sure the right side does not have any definitions
2863 dbv = OP_DEFS(IC_RIGHT(dic));
2864 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2865 if (bitVectBitValue(dbv,lic->key))
2868 /* make sure they have the same type */
2869 if (IS_SPEC(operandType(IC_LEFT(ic))))
2871 sym_link *itype=operandType(IC_LEFT(ic));
2872 sym_link *ditype=operandType(IC_RIGHT(dic));
2874 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2875 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2878 /* extend the live range of replaced operand if needed */
2879 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2880 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2882 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2885 /* we now we know that it has one & only one def & use
2886 and the that the definition is an assignment */
2887 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2888 remiCodeFromeBBlock (ebp, dic);
2889 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2892 /*-----------------------------------------------------------------*/
2893 /* packRegisters - does some transformations to reduce register */
2895 /*-----------------------------------------------------------------*/
2897 packRegisters (eBBlock ** ebpp, int blockno)
2901 eBBlock *ebp=ebpp[blockno];
2907 /* look for assignments of the form */
2908 /* iTempNN = TRueSym (someoperation) SomeOperand */
2910 /* TrueSym := iTempNN:1 */
2911 for (ic = ebp->sch; ic; ic = ic->next)
2913 /* find assignment of the form TrueSym := iTempNN:1 */
2914 if (ic->op == '=' && !POINTER_SET (ic))
2915 change += packRegsForAssign (ic, ebp);
2922 for (ic = ebp->sch; ic; ic = ic->next)
2924 /* Fix for bug #979599: */
2927 /* Look for two subsequent iCodes with */
2929 /* _c = iTemp & op; */
2930 /* and replace them by */
2933 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2935 ic->prev->op == '=' &&
2936 IS_ITEMP (IC_LEFT (ic)) &&
2937 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2938 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
2940 iCode* ic_prev = ic->prev;
2941 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
2943 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
2944 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
2946 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
2947 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
2948 prev_result_sym->liveTo == ic->seq)
2950 prev_result_sym->liveTo = ic_prev->seq;
2953 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
2955 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
2957 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
2959 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
2960 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
2961 remiCodeFromeBBlock (ebp, ic_prev);
2962 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
2966 /* if this is an itemp & result of an address of a true sym
2967 then mark this as rematerialisable */
2968 if (ic->op == ADDRESS_OF &&
2969 IS_ITEMP (IC_RESULT (ic)) &&
2970 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2971 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2972 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2974 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2975 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2976 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2979 /* if straight assignment then carry remat flag if
2980 this is the only definition */
2981 if (ic->op == '=' &&
2982 !POINTER_SET (ic) &&
2983 IS_SYMOP (IC_RIGHT (ic)) &&
2984 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2985 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2986 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2988 OP_SYMBOL (IC_RESULT (ic))->remat =
2989 OP_SYMBOL (IC_RIGHT (ic))->remat;
2990 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2991 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2994 /* if cast to a generic pointer & the pointer being
2995 cast is remat, then we can remat this cast as well */
2996 if (ic->op == CAST &&
2997 IS_SYMOP(IC_RIGHT(ic)) &&
2998 OP_SYMBOL(IC_RIGHT(ic))->remat &&
2999 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3001 sym_link *to_type = operandType(IC_LEFT(ic));
3002 sym_link *from_type = operandType(IC_RIGHT(ic));
3003 if (IS_GENPTR(to_type) && IS_PTR(from_type))
3005 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3006 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3007 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3011 /* if this is a +/- operation with a rematerizable
3012 then mark this as rematerializable as well */
3013 if ((ic->op == '+' || ic->op == '-') &&
3014 (IS_SYMOP (IC_LEFT (ic)) &&
3015 IS_ITEMP (IC_RESULT (ic)) &&
3016 IS_OP_LITERAL (IC_RIGHT (ic))) &&
3017 OP_SYMBOL (IC_LEFT (ic))->remat &&
3018 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3019 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3021 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3022 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3023 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3026 /* mark the pointer usages */
3027 if (POINTER_SET (ic))
3028 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3030 if (POINTER_GET (ic) &&
3031 IS_SYMOP(IC_LEFT (ic)))
3032 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3036 /* if we are using a symbol on the stack
3037 then we should say mcs51_ptrRegReq */
3038 if (options.useXstack && ic->parmPush
3039 && (ic->op == IPUSH || ic->op == IPOP))
3041 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3042 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
3043 OP_SYMBOL (IC_COND (ic))->iaccess ||
3044 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
3045 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3046 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
3047 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
3048 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
3051 if (IS_SYMOP (IC_LEFT (ic)))
3052 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
3053 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
3054 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
3055 if (IS_SYMOP (IC_RIGHT (ic)))
3056 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
3057 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
3058 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
3059 if (IS_SYMOP (IC_RESULT (ic)))
3060 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
3061 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
3062 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
3063 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
3064 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
3066 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
3067 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
3072 /* if the condition of an if instruction
3073 is defined in the previous instruction and
3074 this is the only usage then
3075 mark the itemp as a conditional */
3076 if ((IS_CONDITIONAL (ic) ||
3077 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3078 ic->next && ic->next->op == IFX &&
3079 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3080 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3081 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3083 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3087 /* if the condition of an if instruction
3088 is defined in the previous GET_POINTER instruction and
3089 this is the only usage then
3090 mark the itemp as accumulator use */
3091 if ((POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1) &&
3092 ic->next && ic->next->op == IFX &&
3093 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3094 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3095 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3097 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3101 /* reduce for support function calls */
3102 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3103 packRegsForSupport (ic, ebp);
3105 /* some cases the redundant moves can
3106 can be eliminated for return statements */
3107 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
3108 !isOperandInFarSpace (IC_LEFT (ic)) &&
3109 options.model == MODEL_SMALL) {
3110 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3113 /* if pointer set & left has a size more than
3114 one and right is not in far space */
3115 if (POINTER_SET (ic) &&
3116 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3117 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3118 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3119 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3120 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3122 /* if pointer get */
3123 if (POINTER_GET (ic) &&
3124 IS_SYMOP (IC_LEFT (ic)) &&
3125 !isOperandInFarSpace (IC_RESULT (ic)) &&
3126 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3127 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3128 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3129 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3132 /* if this is a cast for intergral promotion then
3133 check if it's the only use of the definition of the
3134 operand being casted/ if yes then replace
3135 the result of that arithmetic operation with
3136 this result and get rid of the cast */
3139 sym_link *fromType = operandType (IC_RIGHT (ic));
3140 sym_link *toType = operandType (IC_LEFT (ic));
3142 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3143 getSize (fromType) != getSize (toType) &&
3144 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3147 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3150 if (IS_ARITHMETIC_OP (dic))
3152 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3153 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3154 remiCodeFromeBBlock (ebp, ic);
3155 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3156 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3157 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3161 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3167 /* if the type from and type to are the same
3168 then if this is the only use then packit */
3169 if (compareType (operandType (IC_RIGHT (ic)),
3170 operandType (IC_LEFT (ic))) == 1)
3172 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3175 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3176 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3177 remiCodeFromeBBlock (ebp, ic);
3178 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3179 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3180 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3188 iTempNN := (some variable in farspace) V1
3193 if (ic->op == IPUSH)
3195 packForPush (ic, ebpp, blockno);
3199 /* pack registers for accumulator use, when the
3200 result of an arithmetic or bit wise operation
3201 has only one use, that use is immediately following
3202 the defintion and the using iCode has only one
3203 operand or has two operands but one is literal &
3204 the result of that operation is not on stack then
3205 we can leave the result of this operation in acc:b
3207 if ((IS_ARITHMETIC_OP (ic)
3208 || IS_CONDITIONAL(ic)
3209 || IS_BITWISE_OP (ic)
3210 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3211 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3213 IS_ITEMP (IC_RESULT (ic)) &&
3214 getSize (operandType (IC_RESULT (ic))) <= 2)
3216 packRegsForAccUse (ic);
3220 /*-----------------------------------------------------------------*/
3221 /* assignRegisters - assigns registers to each live range as need */
3222 /*-----------------------------------------------------------------*/
3224 mcs51_assignRegisters (ebbIndex * ebbi)
3226 eBBlock ** ebbs = ebbi->bbOrder;
3227 int count = ebbi->count;
3231 setToNull ((void *) &_G.funcrUsed);
3232 setToNull ((void *) &_G.regAssigned);
3233 setToNull ((void *) &_G.totRegAssigned);
3234 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3235 if (options.stackAuto)
3244 _G.allBitregs = findAllBitregs ();
3247 /* change assignments this will remove some
3248 live ranges reducing some register pressure */
3250 for (i = 0; i < count; i++)
3251 packRegisters (ebbs, i);
3253 /* liveranges probably changed by register packing
3254 so we compute them again */
3255 recomputeLiveRanges (ebbs, count);
3257 if (options.dump_pack)
3258 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3260 /* first determine for each live range the number of
3261 registers & the type of registers required for each */
3264 /* and serially allocate registers */
3265 serialRegAssign (ebbs, count);
3268 //setToNull ((void *) &_G.regAssigned);
3269 //setToNull ((void *) &_G.totRegAssigned);
3272 /* if stack was extended then tell the user */
3275 /* werror(W_TOOMANY_SPILS,"stack", */
3276 /* _G.stackExtend,currFunc->name,""); */
3282 /* werror(W_TOOMANY_SPILS,"data space", */
3283 /* _G.dataExtend,currFunc->name,""); */
3287 /* after that create the register mask
3288 for each of the instruction */
3289 createRegMask (ebbs, count);
3291 /* redo that offsets for stacked automatic variables */
3293 redoStackOffsets ();
3296 /* make sure r0 & r1 are flagged as used if they might be used */
3298 if (currFunc && mcs51_ptrRegReq)
3300 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3301 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3304 if (options.dump_rassgn)
3306 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3307 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3310 /* do the overlaysegment stuff SDCCmem.c */
3311 doOverlays (ebbs, count);
3313 /* now get back the chain */
3314 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3318 /* free up any _G.stackSpil locations allocated */
3319 applyToSet (_G.stackSpil, deallocStackSpil);
3321 setToNull ((void *) &_G.stackSpil);
3322 setToNull ((void *) &_G.spiltSet);
3323 /* mark all registers as free */