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);
987 BitBankUsed |= (reg >= 8);
992 /*-----------------------------------------------------------------*/
993 /* deassignLRs - check the live to and if they have registers & are */
994 /* not spilt then free up the registers */
995 /*-----------------------------------------------------------------*/
997 deassignLRs (iCode * ic, eBBlock * ebp)
1003 for (sym = hTabFirstItem (liveRanges, &k); sym;
1004 sym = hTabNextItem (liveRanges, &k))
1007 symbol *psym = NULL;
1008 /* if it does not end here */
1009 if (sym->liveTo > ic->seq)
1012 /* if it was spilt on stack then we can
1013 mark the stack spil location as free */
1018 sym->usl.spillLoc->isFree = 1;
1024 if (!bitVectBitValue (_G.regAssigned, sym->key))
1027 /* special case check if this is an IFX &
1028 the privious one was a pop and the
1029 previous one was not spilt then keep track
1031 if (ic->op == IFX && ic->prev &&
1032 ic->prev->op == IPOP &&
1033 !ic->prev->parmPush &&
1034 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1035 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1041 bitVectUnSetBit (_G.regAssigned, sym->key);
1043 /* if the result of this one needs registers
1044 and does not have it then assign it right
1046 if (IC_RESULT (ic) &&
1047 !(SKIP_IC2 (ic) || /* not a special icode */
1048 ic->op == JUMPTABLE ||
1053 POINTER_SET (ic)) &&
1054 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1055 result->liveTo > ic->seq && /* and will live beyond this */
1056 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1057 result->liveFrom == ic->seq && /* does not start before here */
1058 result->regType == sym->regType && /* same register types */
1059 result->nRegs && /* which needs registers */
1060 !result->isspilt && /* and does not already have them */
1062 !bitVectBitValue (_G.regAssigned, result->key) &&
1063 /* the number of free regs + number of regs in this LR
1064 can accomodate the what result Needs */
1065 ((nfreeRegsType (result->regType) +
1066 sym->nRegs) >= result->nRegs)
1070 for (i = 0; i < result->nRegs; i++)
1072 result->regs[i] = sym->regs[i];
1074 result->regs[i] = getRegGpr (ic, ebp, result);
1076 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1077 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1081 /* free the remaining */
1082 for (; i < sym->nRegs; i++)
1086 if (!symHasReg (psym, sym->regs[i]))
1087 freeReg (sym->regs[i]);
1090 freeReg (sym->regs[i]);
1097 /*-----------------------------------------------------------------*/
1098 /* reassignLR - reassign this to registers */
1099 /*-----------------------------------------------------------------*/
1101 reassignLR (operand * op)
1103 symbol *sym = OP_SYMBOL (op);
1106 /* not spilt any more */
1107 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1108 bitVectUnSetBit (_G.spiltSet, sym->key);
1110 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1111 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1115 for (i = 0; i < sym->nRegs; i++)
1116 sym->regs[i]->isFree = 0;
1119 /*-----------------------------------------------------------------*/
1120 /* willCauseSpill - determines if allocating will cause a spill */
1121 /*-----------------------------------------------------------------*/
1123 willCauseSpill (int nr, int rt)
1125 /* first check if there are any available registers
1126 of the type required */
1129 /* special case for pointer type
1130 if pointer type not avlb then
1131 check for type gpr */
1132 if (nFreeRegs (rt) >= nr)
1134 if (nFreeRegs (REG_GPR) >= nr)
1137 else if (rt == REG_BIT)
1139 if (nFreeRegs (rt) >= nr)
1144 if (mcs51_ptrRegReq)
1146 if (nFreeRegs (rt) >= nr)
1151 if (nFreeRegs (REG_PTR) +
1152 nFreeRegs (REG_GPR) >= nr)
1157 /* it will cause a spil */
1161 /*-----------------------------------------------------------------*/
1162 /* positionRegs - the allocator can allocate same registers to res- */
1163 /* ult and operand, if this happens make sure they are in the same */
1164 /* position as the operand otherwise chaos results */
1165 /*-----------------------------------------------------------------*/
1167 positionRegs (symbol * result, symbol * opsym)
1169 int count = min (result->nRegs, opsym->nRegs);
1170 int i, j = 0, shared = 0;
1173 /* if the result has been spilt then cannot share */
1178 /* first make sure that they actually share */
1179 for (i = 0; i < count; i++)
1181 for (j = 0; j < count; j++)
1183 if (result->regs[i] == opsym->regs[j] && i != j)
1193 regs *tmp = result->regs[i];
1194 result->regs[i] = result->regs[j];
1195 result->regs[j] = tmp;
1202 /*------------------------------------------------------------------*/
1203 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1204 /* it should either have registers or have beed spilled. Otherwise, */
1205 /* there was an uninitialized variable, so just spill this to get */
1206 /* the operand in a valid state. */
1207 /*------------------------------------------------------------------*/
1209 verifyRegsAssigned (operand *op, iCode * ic)
1214 if (!IS_ITEMP (op)) return;
1216 sym = OP_SYMBOL (op);
1217 if (sym->isspilt) return;
1218 if (!sym->nRegs) return;
1219 if (sym->regs[0]) return;
1221 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1222 sym->prereqv ? sym->prereqv->name : sym->name);
1227 /*-----------------------------------------------------------------*/
1228 /* serialRegAssign - serially allocate registers to the variables */
1229 /*-----------------------------------------------------------------*/
1231 serialRegAssign (eBBlock ** ebbs, int count)
1235 /* for all blocks */
1236 for (i = 0; i < count; i++)
1241 if (ebbs[i]->noPath &&
1242 (ebbs[i]->entryLabel != entryLabel &&
1243 ebbs[i]->entryLabel != returnLabel))
1246 /* for all instructions do */
1247 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1251 /* if this is an ipop that means some live
1252 range will have to be assigned again */
1254 reassignLR (IC_LEFT (ic));
1256 /* if result is present && is a true symbol */
1257 if (IC_RESULT (ic) && ic->op != IFX &&
1258 IS_TRUE_SYMOP (IC_RESULT (ic)))
1259 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1261 /* take away registers from live
1262 ranges that end at this instruction */
1263 deassignLRs (ic, ebbs[i]);
1265 /* some don't need registers */
1266 if (SKIP_IC2 (ic) ||
1267 ic->op == JUMPTABLE ||
1271 (IC_RESULT (ic) && POINTER_SET (ic)))
1274 /* now we need to allocate registers
1275 only for the result */
1276 if (IC_RESULT (ic)) {
1277 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1283 /* Make sure any spill location is definitely allocated */
1284 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1285 !sym->usl.spillLoc->allocreq)
1287 sym->usl.spillLoc->allocreq++;
1290 /* if it does not need or is spilt
1291 or is already assigned to registers
1292 or will not live beyond this instructions */
1295 bitVectBitValue (_G.regAssigned, sym->key) ||
1296 sym->liveTo <= ic->seq)
1299 /* if some liverange has been spilt at the block level
1300 and this one live beyond this block then spil this
1302 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1307 willCS = willCauseSpill (sym->nRegs, sym->regType);
1308 /* if this is a bit variable then don't use precious registers
1309 along with expensive bit-to-char conversions but just spill
1311 if (willCS && SPEC_NOUN(sym->etype) == V_BIT) {
1316 /* if trying to allocate this will cause
1317 a spill and there is nothing to spill
1318 or this one is rematerializable then
1320 spillable = computeSpillable (ic);
1321 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1326 /* If the live range preceeds the point of definition
1327 then ideally we must take into account registers that
1328 have been allocated after sym->liveFrom but freed
1329 before ic->seq. This is complicated, so spill this
1330 symbol instead and let fillGaps handle the allocation. */
1331 if (sym->liveFrom < ic->seq) {
1336 /* if it has a spillocation & is used less than
1337 all other live ranges then spill this */
1339 if (sym->usl.spillLoc) {
1340 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1341 allLRs, ebbs[i], ic));
1342 if (leastUsed && leastUsed->used > sym->used) {
1347 /* if none of the liveRanges have a spillLocation then better
1348 to spill this one than anything else already assigned to registers */
1349 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1350 /* if this is local to this block then we might find a block spil */
1351 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1358 /* if we need ptr regs for the right side
1360 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1361 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1365 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1366 && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1370 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1371 && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1376 /* else we assign registers to it */
1377 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1378 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1380 for (j = 0; j < sym->nRegs; j++) {
1381 sym->regs[j] = NULL;
1382 if (sym->regType == REG_PTR)
1383 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1384 else if (sym->regType == REG_BIT)
1385 sym->regs[j] = getRegBit (sym);
1388 if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1390 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1392 if (right->regs[j] && (right->regType != REG_BIT))
1393 sym->regs[j] = allocThisReg (right->regs[j]);
1396 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1399 /* if the allocation failed which means
1400 this was spilt then break */
1404 for (i=0; i < sym->nRegs ; i++ )
1405 sym->regs[i] = NULL;
1410 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1411 /* if it shares registers with operands make sure
1412 that they are in the same position */
1413 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1414 OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1415 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1416 OP_SYMBOL (IC_LEFT (ic)));
1418 /* do the same for the right operand */
1419 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1420 OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1421 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1422 OP_SYMBOL (IC_RIGHT (ic)));
1435 /* Check for and fix any problems with uninitialized operands */
1436 for (i = 0; i < count; i++)
1440 if (ebbs[i]->noPath &&
1441 (ebbs[i]->entryLabel != entryLabel &&
1442 ebbs[i]->entryLabel != returnLabel))
1445 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1452 verifyRegsAssigned (IC_COND (ic), ic);
1456 if (ic->op == JUMPTABLE)
1458 verifyRegsAssigned (IC_JTCOND (ic), ic);
1462 verifyRegsAssigned (IC_RESULT (ic), ic);
1463 verifyRegsAssigned (IC_LEFT (ic), ic);
1464 verifyRegsAssigned (IC_RIGHT (ic), ic);
1469 /*-----------------------------------------------------------------*/
1470 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1471 /*-----------------------------------------------------------------*/
1472 static void fillGaps()
1479 if (getenv("DISABLE_FILL_GAPS")) return;
1481 /* look for liveranges that were spilt by the allocator */
1482 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1483 sym = hTabNextItem(liveRanges,&key)) {
1488 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1490 /* find the liveRanges this one clashes with, that are
1491 still assigned to registers & mark the registers as used*/
1492 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1496 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1497 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1500 clr = hTabItemWithKey(liveRanges,i);
1503 /* mark these registers as used */
1504 for (k = 0 ; k < clr->nRegs ; k++ )
1505 useReg(clr->regs[k]);
1508 if (willCauseSpill(sym->nRegs,sym->regType)) {
1509 /* NOPE :( clear all registers & and continue */
1515 for (i = 0 ; i < sym->defs->size ; i++ )
1517 if (bitVectBitValue(sym->defs,i))
1519 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1526 D(printf("Attempting fillGaps on %s: [",sym->name));
1527 /* THERE IS HOPE !!!! */
1528 for (i=0; i < sym->nRegs ; i++ ) {
1529 if (sym->regType == REG_PTR)
1530 sym->regs[i] = getRegPtrNoSpil ();
1531 else if (sym->regType == REG_BIT)
1532 sym->regs[i] = getRegBitNoSpil ();
1535 sym->regs[i] = NULL;
1536 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1538 symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1541 sym->regs[i] = allocThisReg (right->regs[i]);
1544 sym->regs[i] = getRegGprNoSpil ();
1546 D(printf("%s ", sym->regs[i]->name));
1550 /* For all its definitions check if the registers
1551 allocated needs positioning NOTE: we can position
1552 only ONCE if more than One positioning required
1554 We may need to perform the checks twice; once to
1555 position the registers as needed, the second to
1556 verify any register repositioning is still
1560 for (pass=0; pass<2; pass++) {
1561 D(printf(" checking definitions\n"));
1562 for (i = 0 ; i < sym->defs->size ; i++ ) {
1563 if (bitVectBitValue(sym->defs,i)) {
1564 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1565 D(printf(" ic->seq = %d\n", ic->seq));
1566 if (SKIP_IC(ic)) continue;
1567 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1568 /* if left is assigned to registers */
1569 if (IS_SYMOP(IC_LEFT(ic)))
1571 D(printf(" left = "));
1572 D(printOperand(IC_LEFT(ic),NULL));
1574 if (IS_SYMOP(IC_LEFT(ic)) &&
1575 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1576 pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1578 if (IS_SYMOP(IC_RIGHT(ic)))
1580 D(printf(" right = "));
1581 D(printOperand(IC_RIGHT(ic),NULL));
1583 if (IS_SYMOP(IC_RIGHT(ic)) &&
1584 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1585 pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1587 D(printf(" pdone = %d\n", pdone));
1588 if (pdone > 1) break;
1591 D(printf(" checking uses\n"));
1592 for (i = 0 ; i < sym->uses->size ; i++ ) {
1593 if (bitVectBitValue(sym->uses,i)) {
1595 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1596 D(printf(" ic->seq = %d\n", ic->seq));
1597 if (SKIP_IC(ic)) continue;
1598 if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1600 /* if result is assigned to registers */
1601 if (IS_SYMOP(IC_RESULT(ic)))
1603 D(printf(" result = "));
1604 D(printOperand(IC_RESULT(ic),NULL));
1606 if (IS_SYMOP(IC_RESULT(ic)) &&
1607 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1608 pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1610 D(printf(" pdone = %d\n", pdone));
1611 if (pdone > 1) break;
1614 if (pdone == 0) break; /* second pass only if regs repositioned */
1615 if (pdone > 1) break;
1617 D(printf(" sym->regs = ["));
1618 for (i=0; i < sym->nRegs ; i++ )
1619 D(printf("%s ", sym->regs[i]->name));
1621 /* had to position more than once GIVE UP */
1623 /* UNDO all the changes we made to try this */
1625 for (i=0; i < sym->nRegs ; i++ ) {
1626 sym->regs[i] = NULL;
1629 D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1632 D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1634 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1635 sym->isspilt = sym->spillA = 0 ;
1636 sym->usl.spillLoc->allocreq--;
1641 /*-----------------------------------------------------------------*/
1642 /* findAllBitregs :- returns bit vector of all bit registers */
1643 /*-----------------------------------------------------------------*/
1645 findAllBitregs (void)
1647 bitVect *rmask = newBitVect (mcs51_nRegs);
1650 for (j = 0; j < mcs51_nRegs; j++)
1652 if (regs8051[j].type == REG_BIT)
1653 rmask = bitVectSetBit (rmask, regs8051[j].rIdx);
1659 /*-----------------------------------------------------------------*/
1660 /* mcs51_allBitregs :- returns bit vector of all bit registers */
1661 /*-----------------------------------------------------------------*/
1663 mcs51_allBitregs (void)
1665 return _G.allBitregs;
1668 /*-----------------------------------------------------------------*/
1669 /* rUmaskForOp :- returns register mask for an operand */
1670 /*-----------------------------------------------------------------*/
1672 mcs51_rUmaskForOp (operand * op)
1678 /* only temporaries are assigned registers */
1682 sym = OP_SYMBOL (op);
1684 /* if spilt or no registers assigned to it
1686 if (sym->isspilt || !sym->nRegs)
1689 rumask = newBitVect (mcs51_nRegs);
1691 for (j = 0; j < sym->nRegs; j++)
1693 if (sym->regs[j]) /* EEP - debug */
1694 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1700 /*-----------------------------------------------------------------*/
1701 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1702 /*-----------------------------------------------------------------*/
1704 regsUsedIniCode (iCode * ic)
1706 bitVect *rmask = newBitVect (mcs51_nRegs);
1708 /* do the special cases first */
1711 rmask = bitVectUnion (rmask,
1712 mcs51_rUmaskForOp (IC_COND (ic)));
1716 /* for the jumptable */
1717 if (ic->op == JUMPTABLE)
1719 rmask = bitVectUnion (rmask,
1720 mcs51_rUmaskForOp (IC_JTCOND (ic)));
1725 /* of all other cases */
1727 rmask = bitVectUnion (rmask,
1728 mcs51_rUmaskForOp (IC_LEFT (ic)));
1732 rmask = bitVectUnion (rmask,
1733 mcs51_rUmaskForOp (IC_RIGHT (ic)));
1736 rmask = bitVectUnion (rmask,
1737 mcs51_rUmaskForOp (IC_RESULT (ic)));
1743 /*-----------------------------------------------------------------*/
1744 /* createRegMask - for each instruction will determine the regsUsed */
1745 /*-----------------------------------------------------------------*/
1747 createRegMask (eBBlock ** ebbs, int count)
1751 /* for all blocks */
1752 for (i = 0; i < count; i++)
1756 if (ebbs[i]->noPath &&
1757 (ebbs[i]->entryLabel != entryLabel &&
1758 ebbs[i]->entryLabel != returnLabel))
1761 /* for all instructions */
1762 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1767 if (SKIP_IC2 (ic) || !ic->rlive)
1770 /* first mark the registers used in this
1772 ic->rUsed = regsUsedIniCode (ic);
1773 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1775 /* now create the register mask for those
1776 registers that are in use : this is a
1777 super set of ic->rUsed */
1778 ic->rMask = newBitVect (mcs51_nRegs + 1);
1780 /* for all live Ranges alive at this point */
1781 for (j = 1; j < ic->rlive->size; j++)
1786 /* if not alive then continue */
1787 if (!bitVectBitValue (ic->rlive, j))
1790 /* find the live range we are interested in */
1791 if (!(sym = hTabItemWithKey (liveRanges, j)))
1793 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1794 "createRegMask cannot find live range");
1795 fprintf(stderr, "\tmissing live range: key=%d\n", j);
1799 /* if no register assigned to it */
1800 if (!sym->nRegs || sym->isspilt)
1803 /* for all the registers allocated to it */
1804 for (k = 0; k < sym->nRegs; k++)
1807 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1813 /*-----------------------------------------------------------------*/
1814 /* rematStr - returns the rematerialized string for a remat var */
1815 /*-----------------------------------------------------------------*/
1817 rematStr (symbol * sym)
1820 iCode *ic = sym->rematiCode;
1827 /* if plus or minus print the right hand side */
1828 if (ic->op == '+' || ic->op == '-')
1830 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1831 "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1834 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1838 /* cast then continue */
1839 if (IS_CAST_ICODE(ic)) {
1840 ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1843 /* we reached the end */
1844 SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1845 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1852 /*-----------------------------------------------------------------*/
1853 /* regTypeNum - computes the type & number of registers required */
1854 /*-----------------------------------------------------------------*/
1856 regTypeNum (eBBlock *ebbs)
1862 /* for each live range do */
1863 for (sym = hTabFirstItem (liveRanges, &k); sym;
1864 sym = hTabNextItem (liveRanges, &k))
1867 /* if used zero times then no registers needed */
1868 if ((sym->liveTo - sym->liveFrom) == 0)
1872 /* if the live range is a temporary */
1876 /* if the type is marked as a conditional */
1877 if (sym->regType == REG_CND)
1880 /* if used in return only then we don't
1882 if (sym->ruonly || sym->accuse)
1884 if (IS_AGGREGATE (sym->type) || sym->isptr)
1885 sym->type = aggrToPtr (sym->type, FALSE);
1889 /* if the symbol has only one definition &
1890 that definition is a get_pointer */
1891 if (bitVectnBitsOn (sym->defs) == 1 &&
1892 (ic = hTabItemWithKey (iCodehTab,
1893 bitVectFirstBit (sym->defs))) &&
1895 !IS_BITVAR (sym->etype) &&
1896 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1899 if (ptrPseudoSymSafe (sym, ic))
1901 ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1905 /* if in data space or idata space then try to
1906 allocate pointer register */
1910 /* if not then we require registers */
1911 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1912 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1913 getSize (sym->type));
1917 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1918 printTypeChain (sym->type, stderr);
1919 fprintf (stderr, "\n");
1922 /* determine the type of register required */
1923 if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
1924 sym->regType = REG_PTR;
1925 else if (IS_BIT(sym->type))
1926 sym->regType = REG_BIT;
1928 sym->regType = REG_GPR;
1931 /* for the first run we don't provide */
1932 /* registers for true symbols we will */
1933 /* see how things go */
1939 /*-----------------------------------------------------------------*/
1940 /* freeAllRegs - mark all registers as free */
1941 /*-----------------------------------------------------------------*/
1947 for (i = 0; i < mcs51_nRegs; i++)
1948 regs8051[i].isFree = 1;
1951 /*-----------------------------------------------------------------*/
1952 /* deallocStackSpil - this will set the stack pointer back */
1953 /*-----------------------------------------------------------------*/
1955 DEFSETFUNC (deallocStackSpil)
1963 /*-----------------------------------------------------------------*/
1964 /* farSpacePackable - returns the packable icode for far variables */
1965 /*-----------------------------------------------------------------*/
1967 farSpacePackable (iCode * ic)
1971 /* go thru till we find a definition for the
1972 symbol on the right */
1973 for (dic = ic->prev; dic; dic = dic->prev)
1975 /* if the definition is a call then no */
1976 if ((dic->op == CALL || dic->op == PCALL) &&
1977 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1982 /* if shift by unknown amount then not */
1983 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1984 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1987 /* if pointer get and size > 1 */
1988 if (POINTER_GET (dic) &&
1989 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1992 if (POINTER_SET (dic) &&
1993 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1998 if (IC_COND (dic) &&
1999 IS_TRUE_SYMOP (IC_COND (dic)) &&
2000 isOperandInFarSpace (IC_COND (dic)))
2003 else if (dic->op == JUMPTABLE)
2005 if (IC_JTCOND (dic) &&
2006 IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
2007 isOperandInFarSpace (IC_JTCOND (dic)))
2012 /* if any tree is a true symbol in far space */
2013 if (IC_RESULT (dic) &&
2014 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2015 isOperandInFarSpace (IC_RESULT (dic)))
2018 if (IC_RIGHT (dic) &&
2019 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2020 isOperandInFarSpace (IC_RIGHT (dic)) &&
2021 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2024 if (IC_LEFT (dic) &&
2025 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2026 isOperandInFarSpace (IC_LEFT (dic)) &&
2027 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2031 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2033 if ((dic->op == LEFT_OP ||
2034 dic->op == RIGHT_OP ||
2036 IS_OP_LITERAL (IC_RIGHT (dic)))
2046 /*-----------------------------------------------------------------*/
2047 /* packRegsForAssign - register reduction for assignment */
2048 /*-----------------------------------------------------------------*/
2050 packRegsForAssign (iCode * ic, eBBlock * ebp)
2054 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2055 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2056 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2061 /* if the true symbol is defined in far space or on stack
2062 then we should not since this will increase register pressure */
2063 if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2067 /* find the definition of iTempNN scanning backwards if we find a
2068 a use of the true symbol in before we find the definition then
2070 for (dic = ic->prev; dic; dic = dic->prev)
2072 int crossedCall = 0;
2074 /* We can pack across a function call only if it's a local */
2075 /* variable or our parameter. Never pack global variables */
2076 /* or parameters to a function we call. */
2077 if ((dic->op == CALL || dic->op == PCALL))
2079 if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
2080 && !OP_SYMBOL (IC_RESULT (ic))->islocal)
2091 if (IS_SYMOP (IC_COND (dic)) &&
2092 (IC_COND (dic)->key == IC_RESULT (ic)->key ||
2093 IC_COND (dic)->key == IC_RIGHT (ic)->key))
2101 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2102 IS_OP_VOLATILE (IC_RESULT (dic)))
2108 if (IS_SYMOP (IC_RESULT (dic)) &&
2109 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2111 if (POINTER_SET (dic))
2117 if (IS_SYMOP (IC_RIGHT (dic)) &&
2118 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2119 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2125 if (IS_SYMOP (IC_LEFT (dic)) &&
2126 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2127 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2133 if (IS_SYMOP (IC_RESULT (dic)) &&
2134 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2150 return 0; /* did not find */
2152 /* if assignment then check that right is not a bit */
2153 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2155 sym_link *etype = operandType (IC_RESULT (dic));
2156 if (IS_BITFIELD (etype))
2158 /* if result is a bit too then it's ok */
2159 etype = operandType (IC_RESULT (ic));
2160 if (!IS_BITFIELD (etype))
2167 /* if assignment then check that right is not a bit */
2168 if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2170 sym_link *etype = operandType (IC_RIGHT (dic));
2171 if (IS_BITFIELD (etype))
2173 /* if result is a bit too then it's ok */
2174 etype = operandType (IC_RESULT (dic));
2175 if (!IS_BITFIELD (etype))
2180 /* if the result is on stack or iaccess then it must be
2181 the same atleast one of the operands */
2182 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2183 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2186 /* the operation has only one symbol
2187 operator then we can pack */
2188 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2189 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2192 if (!((IC_LEFT (dic) &&
2193 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2195 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2199 /* found the definition */
2200 /* replace the result with the result of */
2201 /* this assignment and remove this assignment */
2202 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2203 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2205 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2207 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2209 // TODO: and the otherway around?
2211 /* delete from liverange table also
2212 delete from all the points inbetween and the new
2214 for (sic = dic; sic != ic; sic = sic->next)
2216 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2217 if (IS_ITEMP (IC_RESULT (dic)))
2218 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2221 remiCodeFromeBBlock (ebp, ic);
2222 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2223 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2224 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2228 /*------------------------------------------------------------------*/
2229 /* findAssignToSym : scanning backwards looks for first assig found */
2230 /*------------------------------------------------------------------*/
2232 findAssignToSym (operand * op, iCode * ic)
2236 /* This routine is used to find sequences like
2238 ...; (intervening ops don't use iTempAA or modify FOO)
2239 blah = blah + iTempAA;
2241 and eliminate the use of iTempAA, freeing up its register for
2245 for (dic = ic->prev; dic; dic = dic->prev)
2248 /* if definition by assignment */
2249 if (dic->op == '=' &&
2250 !POINTER_SET (dic) &&
2251 IC_RESULT (dic)->key == op->key
2252 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2254 break; /* found where this temp was defined */
2256 /* if we find an usage then we cannot delete it */
2260 if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2263 else if (dic->op == JUMPTABLE)
2265 if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2270 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2273 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2276 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2282 return NULL; /* didn't find any assignment to op */
2284 /* we are interested only if defined in far space */
2285 /* or in stack space in case of + & - */
2287 /* if assigned to a non-symbol then don't repack regs */
2288 if (!IS_SYMOP (IC_RIGHT (dic)))
2291 /* if the symbol is volatile then we should not */
2292 if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2294 /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2295 What does it mean for an iTemp to be volatile, anyway? Passing
2296 TRUE is more cautious but may prevent possible optimizations */
2298 /* if the symbol is in far space then we should not */
2299 if (isOperandInFarSpace (IC_RIGHT (dic)))
2302 /* for + & - operations make sure that
2303 if it is on the stack it is the same
2304 as one of the three operands */
2305 if ((ic->op == '+' || ic->op == '-') &&
2306 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2309 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2310 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2311 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2315 /* now make sure that the right side of dic
2316 is not defined between ic & dic */
2319 iCode *sic = dic->next;
2321 for (; sic != ic; sic = sic->next)
2322 if (IC_RESULT (sic) &&
2323 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2330 /*-----------------------------------------------------------------*/
2331 /* reassignAliasedSym - used by packRegsForSupport to replace */
2332 /* redundant iTemp with equivalent symbol */
2333 /*-----------------------------------------------------------------*/
2335 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2338 unsigned oldSymKey, newSymKey;
2340 oldSymKey = op->key;
2341 newSymKey = IC_RIGHT(assignment)->key;
2343 /* only track live ranges of compiler-generated temporaries */
2344 if (!IS_ITEMP(IC_RIGHT(assignment)))
2347 /* update the live-value bitmaps */
2348 for (ic = assignment; ic != use; ic = ic->next) {
2349 bitVectUnSetBit (ic->rlive, oldSymKey);
2351 ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2354 /* update the sym of the used operand */
2355 OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2356 op->key = OP_SYMBOL(op)->key;
2357 OP_SYMBOL(op)->accuse = 0;
2359 /* update the sym's liverange */
2360 if ( OP_LIVETO(op) < ic->seq )
2361 setToRange(op, ic->seq, FALSE);
2363 /* remove the assignment iCode now that its result is unused */
2364 remiCodeFromeBBlock (ebp, assignment);
2365 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2366 hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2370 /*-----------------------------------------------------------------*/
2371 /* packRegsForSupport :- reduce some registers for support calls */
2372 /*-----------------------------------------------------------------*/
2374 packRegsForSupport (iCode * ic, eBBlock * ebp)
2378 /* for the left & right operand :- look to see if the
2379 left was assigned a true symbol in far space in that
2380 case replace them */
2382 if (IS_ITEMP (IC_LEFT (ic)) &&
2383 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2385 dic = findAssignToSym (IC_LEFT (ic), ic);
2389 /* found it we need to remove it from the block */
2390 reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2395 /* do the same for the right operand */
2396 if (IS_ITEMP (IC_RIGHT (ic)) &&
2397 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2399 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2403 /* if this is a subtraction & the result
2404 is a true symbol in far space then don't pack */
2405 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2407 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2408 if (IN_FARSPACE (SPEC_OCLS (etype)))
2411 /* found it we need to remove it from the
2413 reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2422 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2425 /*-----------------------------------------------------------------*/
2426 /* packRegsForOneuse : - will reduce some registers for single Use */
2427 /*-----------------------------------------------------------------*/
2429 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2433 /* if returning a literal then do nothing */
2437 /* if rematerializable or already return use then do nothing */
2438 if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly)
2441 /* only upto 2 bytes since we cannot predict
2442 the usage of b, & acc */
2443 if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2446 if (ic->op != RETURN &&
2448 !POINTER_SET (ic) &&
2452 if (ic->op == SEND && ic->argreg != 1) return NULL;
2454 /* this routine will mark the a symbol as used in one
2455 instruction use only && if the defintion is local
2456 (ie. within the basic block) && has only one definition &&
2457 that definition is either a return value from a
2458 function or does not contain any variables in
2460 if (bitVectnBitsOn (OP_USES (op)) > 1)
2463 /* if it has only one defintion */
2464 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2465 return NULL; /* has more than one definition */
2467 /* get that definition */
2469 hTabItemWithKey (iCodehTab,
2470 bitVectFirstBit (OP_DEFS (op)))))
2473 /* if that only usage is a cast */
2474 if (dic->op == CAST) {
2475 /* to a bigger type */
2476 if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2477 getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2478 /* than we can not, since we cannot predict the usage of b & acc */
2483 /* found the definition now check if it is local */
2484 if (dic->seq < ebp->fSeq ||
2485 dic->seq > ebp->lSeq)
2486 return NULL; /* non-local */
2488 /* now check if it is the return from
2490 if (dic->op == CALL || dic->op == PCALL)
2492 if (ic->op != SEND && ic->op != RETURN &&
2493 !POINTER_SET(ic) && !POINTER_GET(ic))
2495 OP_SYMBOL (op)->ruonly = 1;
2501 /* otherwise check that the definition does
2502 not contain any symbols in far space */
2503 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2504 isOperandInFarSpace (IC_RIGHT (dic)) ||
2505 IS_OP_RUONLY (IC_LEFT (ic)) ||
2506 IS_OP_RUONLY (IC_RIGHT (ic)))
2511 /* if pointer set then make sure the pointer
2513 if (POINTER_SET (dic) &&
2514 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2517 if (POINTER_GET (dic) &&
2518 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2522 /* Make sure no overlapping liverange is already assigned to DPTR */
2523 if (OP_SYMBOL(op)->clashes)
2528 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2530 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2532 sym = hTabItemWithKey(liveRanges,i);
2541 /* also make sure the intervenening instructions
2542 don't have any thing in far space */
2543 for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2546 /* if there is an intervening function call then no */
2547 if (dic->op == CALL || dic->op == PCALL)
2549 /* if pointer set then make sure the pointer
2551 if (POINTER_SET (dic) &&
2552 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2555 if (POINTER_GET (dic) &&
2556 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2559 /* if address of & the result is remat the okay */
2560 if (dic->op == ADDRESS_OF &&
2561 OP_SYMBOL (IC_RESULT (dic))->remat)
2564 /* if operand has size of three or more & this
2565 operation is a '*','/' or '%' then 'b' may
2567 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2568 getSize (operandType (op)) >= 3)
2571 /* if left or right or result is in far space */
2572 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2573 isOperandInFarSpace (IC_RIGHT (dic)) ||
2574 isOperandInFarSpace (IC_RESULT (dic)) ||
2575 IS_OP_RUONLY (IC_LEFT (dic)) ||
2576 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2577 IS_OP_RUONLY (IC_RESULT (dic)))
2581 /* if left or right or result is on stack */
2582 if (isOperandOnStack(IC_LEFT(dic)) ||
2583 isOperandOnStack(IC_RIGHT(dic)) ||
2584 isOperandOnStack(IC_RESULT(dic))) {
2589 OP_SYMBOL (op)->ruonly = 1;
2593 /*-----------------------------------------------------------------*/
2594 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2595 /*-----------------------------------------------------------------*/
2597 isBitwiseOptimizable (iCode * ic)
2599 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2600 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2602 /* bitwise operations are considered optimizable
2603 under the following conditions (Jean-Louis VERN)
2615 if (IS_LITERAL(rtype) ||
2616 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2622 /*-----------------------------------------------------------------*/
2623 /* isCommutativeOp - tests whether this op cares what order its */
2624 /* operands are in */
2625 /*-----------------------------------------------------------------*/
2626 bool isCommutativeOp(unsigned int op)
2628 if (op == '+' || op == '*' || op == EQ_OP ||
2629 op == '^' || op == '|' || op == BITWISEAND)
2635 /*-----------------------------------------------------------------*/
2636 /* operandUsesAcc - determines whether the code generated for this */
2637 /* operand will have to use the accumulator */
2638 /*-----------------------------------------------------------------*/
2639 bool operandUsesAcc(operand *op)
2645 symbol *sym = OP_SYMBOL(op);
2649 return TRUE; /* duh! */
2651 if (IN_STACK(sym->etype) || sym->onStack ||
2652 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2653 return TRUE; /* acc is used to calc stack offset */
2658 sym = SPIL_LOC(op); /* if spilled, look at spill location */
2660 return FALSE; /* more checks? */
2664 symspace = SPEC_OCLS(sym->etype);
2666 if (sym->iaccess && symspace->paged)
2667 return TRUE; /* must fetch paged indirect sym via accumulator */
2669 if (IN_BITSPACE(symspace))
2670 return TRUE; /* fetching bit vars uses the accumulator */
2672 if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2673 return TRUE; /* fetched via accumulator and dptr */
2679 /*-----------------------------------------------------------------*/
2680 /* packRegsForAccUse - pack registers for acc use */
2681 /*-----------------------------------------------------------------*/
2683 packRegsForAccUse (iCode * ic)
2687 /* if this is an aggregate, e.g. a one byte char array */
2688 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2692 /* if we are calling a reentrant function that has stack parameters */
2693 if (ic->op == CALL &&
2694 IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2695 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2698 if (ic->op == PCALL &&
2699 IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2700 FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2703 /* if + or - then it has to be one byte result */
2704 if ((ic->op == '+' || ic->op == '-')
2705 && getSize (operandType (IC_RESULT (ic))) > 1)
2708 /* if shift operation make sure right side is not a literal */
2709 if (ic->op == RIGHT_OP &&
2710 (isOperandLiteral (IC_RIGHT (ic)) ||
2711 getSize (operandType (IC_RESULT (ic))) > 1))
2714 if (ic->op == LEFT_OP &&
2715 (isOperandLiteral (IC_RIGHT (ic)) ||
2716 getSize (operandType (IC_RESULT (ic))) > 1))
2719 if (IS_BITWISE_OP (ic) &&
2720 getSize (operandType (IC_RESULT (ic))) > 1)
2724 /* has only one definition */
2725 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2728 /* has only one use */
2729 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2732 /* and the usage immediately follows this iCode */
2733 if (!(uic = hTabItemWithKey (iCodehTab,
2734 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2737 if (ic->next != uic)
2740 /* if it is a conditional branch then we definitely can */
2744 if (uic->op == JUMPTABLE)
2747 if (POINTER_SET (uic) &&
2748 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2751 /* if the usage is not is an assignment
2752 or an arithmetic / bitwise / shift operation then not */
2753 if (uic->op != '=' &&
2754 !IS_ARITHMETIC_OP (uic) &&
2755 !IS_BITWISE_OP (uic) &&
2756 uic->op != LEFT_OP &&
2757 uic->op != RIGHT_OP)
2760 /* if used in ^ operation then make sure right is not a
2761 literal (WIML: Why is this?) */
2762 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2765 /* if shift operation make sure right side is not a literal */
2766 /* WIML: Why is this? */
2767 if (uic->op == RIGHT_OP &&
2768 (isOperandLiteral (IC_RIGHT (uic)) ||
2769 getSize (operandType (IC_RESULT (uic))) > 1))
2771 if (uic->op == LEFT_OP &&
2772 (isOperandLiteral (IC_RIGHT (uic)) ||
2773 getSize (operandType (IC_RESULT (uic))) > 1))
2776 /* make sure that the result of this icode is not on the
2777 stack, since acc is used to compute stack offset */
2779 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2780 OP_SYMBOL (IC_RESULT (uic))->onStack)
2783 if (isOperandOnStack(IC_RESULT(uic)))
2787 /* if the usage has only one operand then we can */
2788 if (IC_LEFT (uic) == NULL ||
2789 IC_RIGHT (uic) == NULL)
2792 /* if the other operand uses the accumulator then we cannot */
2793 if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2794 operandUsesAcc(IC_RIGHT(uic))) ||
2795 (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2796 operandUsesAcc(IC_LEFT(uic))) )
2799 /* make sure this is on the left side if not commutative */
2800 /* except for '-', which has been written to be able to
2801 handle reversed operands */
2802 if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2803 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2807 // this is too dangerous and need further restrictions
2810 /* if one of them is a literal then we can */
2811 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2812 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2814 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2820 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2824 /*-----------------------------------------------------------------*/
2825 /* packForPush - heuristics to reduce iCode for pushing */
2826 /*-----------------------------------------------------------------*/
2828 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2832 struct eBBlock * ebp=ebpp[blockno];
2834 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2837 /* must have only definition & one usage */
2838 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2839 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2842 /* find the definition */
2843 if (!(dic = hTabItemWithKey (iCodehTab,
2844 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2847 if (dic->op != '=' || POINTER_SET (dic))
2850 if (dic->seq < ebp->fSeq) { // Evelyn did this
2852 for (i=0; i<blockno; i++) {
2853 if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2858 wassert (i!=blockno); // no way to recover from here
2861 if (IS_SYMOP(IC_RIGHT(dic))) {
2862 /* make sure the right side does not have any definitions
2864 dbv = OP_DEFS(IC_RIGHT(dic));
2865 for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2866 if (bitVectBitValue(dbv,lic->key))
2869 /* make sure they have the same type */
2870 if (IS_SPEC(operandType(IC_LEFT(ic))))
2872 sym_link *itype=operandType(IC_LEFT(ic));
2873 sym_link *ditype=operandType(IC_RIGHT(dic));
2875 if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2876 SPEC_LONG(itype)!=SPEC_LONG(ditype))
2879 /* extend the live range of replaced operand if needed */
2880 if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2881 OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2883 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2886 /* we now we know that it has one & only one def & use
2887 and the that the definition is an assignment */
2888 ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2889 remiCodeFromeBBlock (ebp, dic);
2890 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2893 /*-----------------------------------------------------------------*/
2894 /* packRegisters - does some transformations to reduce register */
2896 /*-----------------------------------------------------------------*/
2898 packRegisters (eBBlock ** ebpp, int blockno)
2902 eBBlock *ebp=ebpp[blockno];
2908 /* look for assignments of the form */
2909 /* iTempNN = TRueSym (someoperation) SomeOperand */
2911 /* TrueSym := iTempNN:1 */
2912 for (ic = ebp->sch; ic; ic = ic->next)
2914 /* find assignment of the form TrueSym := iTempNN:1 */
2915 if (ic->op == '=' && !POINTER_SET (ic))
2916 change += packRegsForAssign (ic, ebp);
2923 for (ic = ebp->sch; ic; ic = ic->next)
2925 /* Fix for bug #979599: */
2928 /* Look for two subsequent iCodes with */
2930 /* _c = iTemp & op; */
2931 /* and replace them by */
2934 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2936 ic->prev->op == '=' &&
2937 IS_ITEMP (IC_LEFT (ic)) &&
2938 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2939 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
2941 iCode* ic_prev = ic->prev;
2942 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
2944 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
2945 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
2947 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
2948 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
2949 prev_result_sym->liveTo == ic->seq)
2951 prev_result_sym->liveTo = ic_prev->seq;
2954 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
2956 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
2958 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
2960 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
2961 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
2962 remiCodeFromeBBlock (ebp, ic_prev);
2963 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
2967 /* if this is an itemp & result of an address of a true sym
2968 then mark this as rematerialisable */
2969 if (ic->op == ADDRESS_OF &&
2970 IS_ITEMP (IC_RESULT (ic)) &&
2971 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2972 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2973 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2975 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2976 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2977 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2980 /* if straight assignment then carry remat flag if
2981 this is the only definition */
2982 if (ic->op == '=' &&
2983 !POINTER_SET (ic) &&
2984 IS_SYMOP (IC_RIGHT (ic)) &&
2985 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2986 !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2987 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2989 OP_SYMBOL (IC_RESULT (ic))->remat =
2990 OP_SYMBOL (IC_RIGHT (ic))->remat;
2991 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2992 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2995 /* if cast to a generic pointer & the pointer being
2996 cast is remat, then we can remat this cast as well */
2997 if (ic->op == CAST &&
2998 IS_SYMOP(IC_RIGHT(ic)) &&
2999 OP_SYMBOL(IC_RIGHT(ic))->remat &&
3000 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3002 sym_link *to_type = operandType(IC_LEFT(ic));
3003 sym_link *from_type = operandType(IC_RIGHT(ic));
3004 if (IS_GENPTR(to_type) && IS_PTR(from_type))
3006 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3007 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3008 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3012 /* if this is a +/- operation with a rematerizable
3013 then mark this as rematerializable as well */
3014 if ((ic->op == '+' || ic->op == '-') &&
3015 (IS_SYMOP (IC_LEFT (ic)) &&
3016 IS_ITEMP (IC_RESULT (ic)) &&
3017 IS_OP_LITERAL (IC_RIGHT (ic))) &&
3018 OP_SYMBOL (IC_LEFT (ic))->remat &&
3019 (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3020 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3022 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3023 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3024 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3027 /* mark the pointer usages */
3028 if (POINTER_SET (ic))
3029 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3031 if (POINTER_GET (ic) &&
3032 IS_SYMOP(IC_LEFT (ic)))
3033 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3037 /* if we are using a symbol on the stack
3038 then we should say mcs51_ptrRegReq */
3039 if (options.useXstack && ic->parmPush
3040 && (ic->op == IPUSH || ic->op == IPOP))
3042 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3043 mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
3044 OP_SYMBOL (IC_COND (ic))->iaccess ||
3045 SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
3046 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3047 mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
3048 OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
3049 SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
3052 if (IS_SYMOP (IC_LEFT (ic)))
3053 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
3054 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
3055 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
3056 if (IS_SYMOP (IC_RIGHT (ic)))
3057 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
3058 OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
3059 SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
3060 if (IS_SYMOP (IC_RESULT (ic)))
3061 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
3062 OP_SYMBOL (IC_RESULT (ic))->iaccess ||
3063 SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
3064 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
3065 && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
3067 if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
3068 && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
3073 /* if the condition of an if instruction
3074 is defined in the previous instruction and
3075 this is the only usage then
3076 mark the itemp as a conditional */
3077 if ((IS_CONDITIONAL (ic) ||
3078 (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3079 ic->next && ic->next->op == IFX &&
3080 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3081 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3082 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3084 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3088 /* if the condition of an if instruction
3089 is defined in the previous GET_POINTER instruction and
3090 this is the only usage then
3091 mark the itemp as accumulator use */
3092 if ((POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1) &&
3093 ic->next && ic->next->op == IFX &&
3094 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3095 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3096 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3098 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3102 /* reduce for support function calls */
3103 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3104 packRegsForSupport (ic, ebp);
3106 /* some cases the redundant moves can
3107 can be eliminated for return statements */
3108 if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
3109 !isOperandInFarSpace (IC_LEFT (ic)) &&
3110 options.model == MODEL_SMALL) {
3111 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3114 /* if pointer set & left has a size more than
3115 one and right is not in far space */
3116 if (POINTER_SET (ic) &&
3117 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3118 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3119 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3120 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3121 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3123 /* if pointer get */
3124 if (POINTER_GET (ic) &&
3125 IS_SYMOP (IC_LEFT (ic)) &&
3126 !isOperandInFarSpace (IC_RESULT (ic)) &&
3127 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3128 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3129 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3130 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3133 /* if this is a cast for intergral promotion then
3134 check if it's the only use of the definition of the
3135 operand being casted/ if yes then replace
3136 the result of that arithmetic operation with
3137 this result and get rid of the cast */
3140 sym_link *fromType = operandType (IC_RIGHT (ic));
3141 sym_link *toType = operandType (IC_LEFT (ic));
3143 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3144 getSize (fromType) != getSize (toType) &&
3145 SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3148 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3151 if (IS_ARITHMETIC_OP (dic))
3153 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3154 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3155 remiCodeFromeBBlock (ebp, ic);
3156 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3157 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3158 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3162 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3168 /* if the type from and type to are the same
3169 then if this is the only use then packit */
3170 if (compareType (operandType (IC_RIGHT (ic)),
3171 operandType (IC_LEFT (ic))) == 1)
3173 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3176 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3177 ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3178 remiCodeFromeBBlock (ebp, ic);
3179 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3180 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3181 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3189 iTempNN := (some variable in farspace) V1
3194 if (ic->op == IPUSH)
3196 packForPush (ic, ebpp, blockno);
3200 /* pack registers for accumulator use, when the
3201 result of an arithmetic or bit wise operation
3202 has only one use, that use is immediately following
3203 the defintion and the using iCode has only one
3204 operand or has two operands but one is literal &
3205 the result of that operation is not on stack then
3206 we can leave the result of this operation in acc:b
3208 if ((IS_ARITHMETIC_OP (ic)
3209 || IS_CONDITIONAL(ic)
3210 || IS_BITWISE_OP (ic)
3211 || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3212 || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3214 IS_ITEMP (IC_RESULT (ic)) &&
3215 getSize (operandType (IC_RESULT (ic))) <= 2)
3217 packRegsForAccUse (ic);
3221 /*-----------------------------------------------------------------*/
3222 /* assignRegisters - assigns registers to each live range as need */
3223 /*-----------------------------------------------------------------*/
3225 mcs51_assignRegisters (ebbIndex * ebbi)
3227 eBBlock ** ebbs = ebbi->bbOrder;
3228 int count = ebbi->count;
3232 setToNull ((void *) &_G.funcrUsed);
3233 setToNull ((void *) &_G.regAssigned);
3234 setToNull ((void *) &_G.totRegAssigned);
3235 mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3236 if ((currFunc && IFFUNC_ISREENT (currFunc->type)) || 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 */