1 /** @name Z80 Register allocation functions.
4 Note: much of this is ripped straight from Sandeep's mcs51 code.
6 This code maps the virtual symbols and code onto the real
7 hardware. It allocates based on usage and how long the varible
8 lives into registers or temporary memory on the stack.
10 On the Z80 hl and ix and a are reserved for the code generator,
11 leaving bc and de for allocation. iy is unusable due to currently
12 as it's only adressable as a pair. The extra register pressure
13 from reserving hl is made up for by how much easier the sub
14 operations become. You could swap hl for iy if the undocumented
15 iyl/iyh instructions are available.
17 The stack frame is the common ix-bp style. Basically:
22 ix+0: calling functions ix
25 sp: end of local varibles
27 There is currently no support for bit spaces or banked functions.
29 This program is free software; you can redistribute it and/or
30 modify it under the terms of the GNU General Public License as
31 published by the Free Software Foundation; either version 2, or (at
32 your option) any later version. This program is distributed in the
33 hope that it will be useful, but WITHOUT ANY WARRANTY; without even
34 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
35 PURPOSE. See the GNU General Public License for more details.
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
40 USA. In other words, you are welcome to use, share and improve
41 this program. You are forbidden to forbid anyone else to use,
42 share and improve what you give them. Help stamp out
48 /* Flags to turn off optimisations.
53 DISABLE_PACK_ASSIGN = 0,
54 DISABLE_PACK_ONE_USE = 0,
58 /* Flags to turn on debugging code.
69 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
74 #define DISABLE_PACKREGSFORSUPPORT 1
75 #define DISABLE_PACKREGSFORACCUSE 1
77 extern void genZ80Code (iCode *);
79 /** Local static variables */
87 /* registers used in a function */
94 static regs _gbz80_regs[] =
96 {REG_GPR, C_IDX, "c", 1},
97 {REG_GPR, B_IDX, "b", 1},
98 {REG_CND, CND_IDX, "c", 1}
101 static regs _z80_regs[] =
103 {REG_GPR, C_IDX, "c", 1},
104 {REG_GPR, B_IDX, "b", 1},
105 {REG_GPR, E_IDX, "e", 1},
106 {REG_GPR, D_IDX, "d", 1},
107 {REG_CND, CND_IDX, "c", 1}
112 /** Number of usable registers (all but C) */
113 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
114 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
116 static void spillThis (symbol *);
118 /** Allocates register of given type.
119 'type' is not used on the z80 version. It was used to select
120 between pointer and general purpose registers on the mcs51 version.
122 @return Pointer to the newly allocated register.
125 allocReg (short type)
129 for (i = 0; i < _G.nRegs; i++)
131 /* For now we allocate from any free */
132 if (regsZ80[i].isFree)
134 regsZ80[i].isFree = 0;
137 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
139 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
143 D (D_ALLOC, ("allocReg: No free.\n"));
147 /** Returns pointer to register wit index number
154 for (i = 0; i < _G.nRegs; i++)
156 if (regsZ80[i].rIdx == idx)
162 wassertl (0, "regWithIdx not found");
166 /** Frees a register.
171 wassert (!reg->isFree);
173 D (D_ALLOC, ("freeReg: freed %p\n", reg));
177 /** Returns number of free registers.
185 for (i = 0; i < _G.nRegs; i++)
187 /* For now only one reg type */
188 if (regsZ80[i].isFree)
196 /** Free registers with type.
199 nfreeRegsType (int type)
204 if ((nfr = nFreeRegs (type)) == 0)
206 return nFreeRegs (REG_GPR);
210 return nFreeRegs (type);
215 /*-----------------------------------------------------------------*/
216 /* allDefsOutOfRange - all definitions are out of a range */
217 /*-----------------------------------------------------------------*/
219 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
226 for (i = 0; i < defs->size; i++)
230 if (bitVectBitValue (defs, i) &&
231 (ic = hTabItemWithKey (iCodehTab, i)) &&
232 (ic->seq >= fseq && ic->seq <= toseq))
242 /*-----------------------------------------------------------------*/
243 /* computeSpillable - given a point find the spillable live ranges */
244 /*-----------------------------------------------------------------*/
246 computeSpillable (iCode * ic)
250 /* spillable live ranges are those that are live at this
251 point . the following categories need to be subtracted
253 a) - those that are already spilt
254 b) - if being used by this one
255 c) - defined by this one */
257 spillable = bitVectCopy (ic->rlive);
259 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
261 bitVectCplAnd (spillable, ic->uses); /* used in this one */
262 bitVectUnSetBit (spillable, ic->defKey);
263 spillable = bitVectIntersect (spillable, _G.regAssigned);
268 /*-----------------------------------------------------------------*/
269 /* noSpilLoc - return true if a variable has no spil location */
270 /*-----------------------------------------------------------------*/
272 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
274 return (sym->usl.spillLoc ? 0 : 1);
277 /*-----------------------------------------------------------------*/
278 /* hasSpilLoc - will return 1 if the symbol has spil location */
279 /*-----------------------------------------------------------------*/
281 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
283 return (sym->usl.spillLoc ? 1 : 0);
286 /** Will return 1 if the remat flag is set.
287 A symbol is rematerialisable if it doesnt need to be allocated
288 into registers at creation as it can be re-created at any time -
289 i.e. it's constant in some way.
292 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
297 /*-----------------------------------------------------------------*/
298 /* allLRs - return true for all */
299 /*-----------------------------------------------------------------*/
301 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
306 /** liveRangesWith - applies function to a given set of live range
309 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
310 eBBlock * ebp, iCode * ic)
315 if (!lrs || !lrs->size)
318 for (i = 1; i < lrs->size; i++)
321 if (!bitVectBitValue (lrs, i))
324 /* if we don't find it in the live range
325 hash table we are in serious trouble */
326 if (!(sym = hTabItemWithKey (liveRanges, i)))
328 wassertl (0, "liveRangesWith could not find liveRange");
332 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
334 addSetHead (&rset, sym);
342 /** leastUsedLR - given a set determines which is the least used
345 leastUsedLR (set * sset)
347 symbol *sym = NULL, *lsym = NULL;
349 sym = lsym = setFirstItem (sset);
354 for (; lsym; lsym = setNextItem (sset))
357 /* if usage is the same then prefer
358 the spill the smaller of the two */
359 if (lsym->used == sym->used)
360 if (getSize (lsym->type) < getSize (sym->type))
364 if (lsym->used < sym->used)
369 setToNull ((void **) &sset);
374 /** noOverLap - will iterate through the list looking for over lap
377 noOverLap (set * itmpStack, symbol * fsym)
381 for (sym = setFirstItem (itmpStack); sym;
382 sym = setNextItem (itmpStack))
384 // if sym starts before (or on) our end point
385 // and ends after (or on) our start point,
387 if (sym->liveFrom <= fsym->liveTo &&
388 sym->liveTo >= fsym->liveFrom)
396 /*-----------------------------------------------------------------*/
397 /* isFree - will return 1 if the a free spil location is found */
398 /*-----------------------------------------------------------------*/
402 V_ARG (symbol **, sloc);
403 V_ARG (symbol *, fsym);
405 /* if already found */
409 /* if it is free && and the itmp assigned to
410 this does not have any overlapping live ranges
411 with the one currently being assigned and
412 the size can be accomodated */
414 noOverLap (sym->usl.itmpStack, fsym) &&
415 getSize (sym->type) >= getSize (fsym->type))
424 /*-----------------------------------------------------------------*/
425 /* createStackSpil - create a location on the stack to spil */
426 /*-----------------------------------------------------------------*/
428 createStackSpil (symbol * sym)
432 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
434 /* first go try and find a free one that is already
435 existing on the stack */
436 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
438 /* found a free one : just update & return */
439 sym->usl.spillLoc = sloc;
442 addSetHead (&sloc->usl.itmpStack, sym);
443 D (D_ALLOC, ("createStackSpil: found existing\n"));
447 /* could not then have to create one , this is the hard part
448 we need to allocate this on the stack : this is really a
449 hack!! but cannot think of anything better at this time */
451 sprintf (buffer, "sloc%d", _G.slocNum++);
452 sloc = newiTemp (buffer);
454 /* set the type to the spilling symbol */
455 sloc->type = copyLinkChain (sym->type);
456 sloc->etype = getSpec (sloc->type);
457 SPEC_SCLS (sloc->etype) = S_AUTO;
461 sloc->isref = 1; /* to prevent compiler warning */
463 /* if it is on the stack then update the stack */
464 if (IN_STACK (sloc->etype))
466 currFunc->stack += getSize (sloc->type);
467 _G.stackExtend += getSize (sloc->type);
471 _G.dataExtend += getSize (sloc->type);
474 /* add it to the stackSpil set */
475 addSetHead (&_G.stackSpil, sloc);
476 sym->usl.spillLoc = sloc;
479 /* add it to the set of itempStack set
480 of the spill location */
481 addSetHead (&sloc->usl.itmpStack, sym);
483 D (D_ALLOC, ("createStackSpil: created new\n"));
487 /*-----------------------------------------------------------------*/
488 /* spillThis - spils a specific operand */
489 /*-----------------------------------------------------------------*/
491 spillThis (symbol * sym)
495 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
497 /* if this is rematerializable or has a spillLocation
498 we are okay, else we need to create a spillLocation
500 if (!(sym->remat || sym->usl.spillLoc))
502 createStackSpil (sym);
505 /* mark it has spilt & put it in the spilt set */
507 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
509 bitVectUnSetBit (_G.regAssigned, sym->key);
511 for (i = 0; i < sym->nRegs; i++)
515 freeReg (sym->regs[i]);
520 if (sym->usl.spillLoc && !sym->remat)
522 sym->usl.spillLoc->allocreq = 1;
528 /*-----------------------------------------------------------------*/
529 /* allDefsOutOfRange - all definitions are out of a range */
530 /*-----------------------------------------------------------------*/
532 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
539 for (i = 0; i < defs->size; i++)
543 if (bitVectBitValue (defs, i) &&
544 (ic = hTabItemWithKey (iCodehTab, i)) &&
545 (ic->seq >= fseq && ic->seq <= toseq))
554 /*-----------------------------------------------------------------*/
555 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
556 /* but is not used as a pointer */
557 /*-----------------------------------------------------------------*/
559 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
561 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
564 /*-----------------------------------------------------------------*/
565 /* notUsedInBlock - not used in this block */
566 /*-----------------------------------------------------------------*/
568 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
570 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
571 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
572 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
575 /*-----------------------------------------------------------------*/
576 /* notUsedInRemaining - not used or defined in remain of the block */
577 /*-----------------------------------------------------------------*/
579 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
581 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
582 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
586 /** Select a iTemp to spil : rather a simple procedure.
589 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
591 bitVect *lrcs = NULL;
595 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
596 /* get the spillable live ranges */
597 lrcs = computeSpillable (ic);
599 /* get all live ranges that are rematerizable */
600 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
602 D (D_ALLOC, ("selectSpil: using remat.\n"));
603 /* return the least used of these */
604 return leastUsedLR (selectS);
608 /* get live ranges with spillLocations in direct space */
609 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
611 sym = leastUsedLR (selectS);
612 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
613 sym->usl.spillLoc->rname :
614 sym->usl.spillLoc->name));
616 /* mark it as allocation required */
617 sym->usl.spillLoc->allocreq = 1;
621 /* if the symbol is local to the block then */
622 if (forSym->liveTo < ebp->lSeq)
625 /* check if there are any live ranges allocated
626 to registers that are not used in this block */
627 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
629 sym = leastUsedLR (selectS);
630 /* if this is not rematerializable */
634 wassertl (0, "Attempted to do an unsupported block spill");
640 /* check if there are any live ranges that not
641 used in the remainder of the block */
642 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
644 sym = leastUsedLR (selectS);
649 wassertl (0, "Attempted to do an unsupported remain spill");
657 /* find live ranges with spillocation && not used as pointers */
658 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
661 sym = leastUsedLR (selectS);
662 /* mark this as allocation required */
663 sym->usl.spillLoc->allocreq = 1;
668 /* find live ranges with spillocation */
669 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
671 D (D_ALLOC, ("selectSpil: using with spill.\n"));
672 sym = leastUsedLR (selectS);
673 sym->usl.spillLoc->allocreq = 1;
677 /* couldn't find then we need to create a spil
678 location on the stack , for which one? the least
680 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
682 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
683 /* return a created spil location */
684 sym = createStackSpil (leastUsedLR (selectS));
685 sym->usl.spillLoc->allocreq = 1;
689 /* this is an extreme situation we will spill
690 this one : happens very rarely but it does happen */
691 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
697 /** Spil some variable & mark registers as free.
698 A spill occurs when an iTemp wont fit into the available registers.
701 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
706 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
708 /* get something we can spil */
709 ssym = selectSpil (ic, ebp, forSym);
711 /* mark it as spilt */
713 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
715 /* mark it as not register assigned &
716 take it away from the set */
717 bitVectUnSetBit (_G.regAssigned, ssym->key);
719 /* mark the registers as free */
720 for (i = 0; i < ssym->nRegs; i++)
722 freeReg (ssym->regs[i]);
724 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
725 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
727 /* if spilt on stack then free up r0 & r1
728 if they could have been assigned to as gprs */
729 if (!ptrRegReq && isSpiltOnStack (ssym))
732 spillLRWithPtrReg (ssym);
735 /* if this was a block level spil then insert push & pop
736 at the start & end of block respectively */
739 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
740 /* add push to the start of the block */
741 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
742 ebp->sch->next : ebp->sch));
743 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
744 /* add pop to the end of the block */
745 addiCodeToeBBlock (ebp, nic, NULL);
748 /* if spilt because not used in the remainder of the
749 block then add a push before this instruction and
750 a pop at the end of the block */
751 if (ssym->remainSpil)
754 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
755 /* add push just before this instruction */
756 addiCodeToeBBlock (ebp, nic, ic);
758 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
759 /* add pop to the end of the block */
760 addiCodeToeBBlock (ebp, nic, NULL);
764 D (D_ALLOC, ("spilSomething: done.\n"));
772 /** Will try for GPR if not spil.
775 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
779 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
781 /* try for gpr type */
782 if ((reg = allocReg (REG_GPR)))
784 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
788 /* we have to spil */
789 if (!spilSomething (ic, ebp, sym))
791 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
795 /* this looks like an infinite loop but
796 in really selectSpil will abort */
800 /** Symbol has a given register.
803 symHasReg (symbol * sym, regs * reg)
807 for (i = 0; i < sym->nRegs; i++)
808 if (sym->regs[i] == reg)
814 /** Check the live to and if they have registers & are not spilt then
815 free up the registers
818 deassignLRs (iCode * ic, eBBlock * ebp)
824 for (sym = hTabFirstItem (liveRanges, &k); sym;
825 sym = hTabNextItem (liveRanges, &k))
829 /* if it does not end here */
830 if (sym->liveTo > ic->seq)
833 /* if it was spilt on stack then we can
834 mark the stack spil location as free */
839 sym->usl.spillLoc->isFree = 1;
845 if (!bitVectBitValue (_G.regAssigned, sym->key))
848 /* special case check if this is an IFX &
849 the privious one was a pop and the
850 previous one was not spilt then keep track
852 if (ic->op == IFX && ic->prev &&
853 ic->prev->op == IPOP &&
854 !ic->prev->parmPush &&
855 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
856 psym = OP_SYMBOL (IC_LEFT (ic->prev));
858 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
864 bitVectUnSetBit (_G.regAssigned, sym->key);
866 /* if the result of this one needs registers
867 and does not have it then assign it right
869 if (IC_RESULT (ic) &&
870 !(SKIP_IC2 (ic) || /* not a special icode */
871 ic->op == JUMPTABLE ||
876 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
877 result->liveTo > ic->seq && /* and will live beyond this */
878 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
879 result->regType == sym->regType && /* same register types */
880 result->nRegs && /* which needs registers */
881 !result->isspilt && /* and does not already have them */
883 !bitVectBitValue (_G.regAssigned, result->key) &&
884 /* the number of free regs + number of regs in this LR
885 can accomodate the what result Needs */
886 ((nfreeRegsType (result->regType) +
887 sym->nRegs) >= result->nRegs)
890 for (i = 0; i < result->nRegs; i++)
893 result->regs[i] = sym->regs[i];
895 result->regs[i] = getRegGpr (ic, ebp, result);
897 /* if the allocation falied which means
898 this was spilt then break */
899 if (!result->regs[i])
907 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
910 /* free the remaining */
911 for (; i < sym->nRegs; i++)
915 if (!symHasReg (psym, sym->regs[i]))
916 freeReg (sym->regs[i]);
919 freeReg (sym->regs[i]);
920 // sym->regs[i] = NULL;
927 /** Reassign this to registers.
930 reassignLR (operand * op)
932 symbol *sym = OP_SYMBOL (op);
935 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
937 /* not spilt any more */
938 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
939 bitVectUnSetBit (_G.spiltSet, sym->key);
941 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
945 for (i = 0; i < sym->nRegs; i++)
946 sym->regs[i]->isFree = 0;
949 /** Determines if allocating will cause a spill.
952 willCauseSpill (int nr, int rt)
954 /* first check if there are any avlb registers
955 of te type required */
956 if (nFreeRegs (0) >= nr)
959 /* it will cause a spil */
963 /** The allocator can allocate same registers to result and operand,
964 if this happens make sure they are in the same position as the operand
965 otherwise chaos results.
968 positionRegs (symbol * result, symbol * opsym, int lineno)
970 int count = min (result->nRegs, opsym->nRegs);
971 int i, j = 0, shared = 0;
973 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
975 /* if the result has been spilt then cannot share */
980 /* first make sure that they actually share */
981 for (i = 0; i < count; i++)
983 for (j = 0; j < count; j++)
985 if (result->regs[i] == opsym->regs[j] && i != j)
995 regs *tmp = result->regs[i];
996 result->regs[i] = result->regs[j];
997 result->regs[j] = tmp;
1002 /** Try to allocate a pair of registers to the symbol.
1005 tryAllocatingRegPair (symbol * sym)
1008 wassert (sym->nRegs == 2);
1009 for (i = 0; i < _G.nRegs; i += 2)
1011 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1013 regsZ80[i].isFree = 0;
1014 sym->regs[0] = ®sZ80[i];
1015 regsZ80[i + 1].isFree = 0;
1016 sym->regs[1] = ®sZ80[i + 1];
1019 currFunc->regsUsed =
1020 bitVectSetBit (currFunc->regsUsed, i);
1021 currFunc->regsUsed =
1022 bitVectSetBit (currFunc->regsUsed, i + 1);
1024 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1028 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1032 /** Serially allocate registers to the variables.
1033 This is the main register allocation function. It is called after
1037 serialRegAssign (eBBlock ** ebbs, int count)
1041 /* for all blocks */
1042 for (i = 0; i < count; i++)
1047 if (ebbs[i]->noPath &&
1048 (ebbs[i]->entryLabel != entryLabel &&
1049 ebbs[i]->entryLabel != returnLabel))
1052 /* of all instructions do */
1053 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1056 /* if this is an ipop that means some live
1057 range will have to be assigned again */
1061 reassignLR (IC_LEFT (ic));
1064 /* if result is present && is a true symbol */
1065 if (IC_RESULT (ic) && ic->op != IFX &&
1066 IS_TRUE_SYMOP (IC_RESULT (ic)))
1067 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1069 /* take away registers from live
1070 ranges that end at this instruction */
1071 deassignLRs (ic, ebbs[i]);
1073 /* some don't need registers */
1074 /* MLH: removed RESULT and POINTER_SET condition */
1075 if (SKIP_IC2 (ic) ||
1076 ic->op == JUMPTABLE ||
1082 /* now we need to allocate registers only for the result */
1085 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1090 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1092 /* if it does not need or is spilt
1093 or is already assigned to registers
1094 or will not live beyond this instructions */
1097 bitVectBitValue (_G.regAssigned, sym->key) ||
1098 sym->liveTo <= ic->seq)
1100 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1104 /* if some liverange has been spilt at the block level
1105 and this one live beyond this block then spil this
1107 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1109 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1113 /* if trying to allocate this will cause
1114 a spill and there is nothing to spill
1115 or this one is rematerializable then
1117 willCS = willCauseSpill (sym->nRegs, sym->regType);
1118 spillable = computeSpillable (ic);
1120 (willCS && bitVectIsZero (spillable)))
1123 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1129 /* if it has a spillocation & is used less than
1130 all other live ranges then spill this */
1132 if (sym->usl.spillLoc) {
1133 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1134 allLRs, ebbs[i], ic));
1135 if (leastUsed && leastUsed->used > sym->used) {
1140 /* if none of the liveRanges have a spillLocation then better
1141 to spill this one than anything else already assigned to registers */
1142 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1149 /* else we assign registers to it */
1150 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1152 /* Special case: Try to fit into a reg pair if
1154 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1155 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1160 for (j = 0; j < sym->nRegs; j++)
1162 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1164 /* if the allocation falied which means
1165 this was spilt then break */
1168 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1173 /* if it shares registers with operands make sure
1174 that they are in the same position */
1175 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1176 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1177 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1178 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1179 /* do the same for the right operand */
1180 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1181 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1182 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1183 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1190 /*-----------------------------------------------------------------*/
1191 /* rUmaskForOp :- returns register mask for an operand */
1192 /*-----------------------------------------------------------------*/
1194 rUmaskForOp (operand * op)
1200 /* only temporaries are assigned registers */
1204 sym = OP_SYMBOL (op);
1206 /* if spilt or no registers assigned to it
1208 if (sym->isspilt || !sym->nRegs)
1211 rumask = newBitVect (_G.nRegs);
1213 for (j = 0; j < sym->nRegs; j++)
1215 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1222 z80_rUmaskForOp (operand * op)
1224 return rUmaskForOp (op);
1227 /** Returns bit vector of registers used in iCode.
1230 regsUsedIniCode (iCode * ic)
1232 bitVect *rmask = newBitVect (_G.nRegs);
1234 /* do the special cases first */
1237 rmask = bitVectUnion (rmask,
1238 rUmaskForOp (IC_COND (ic)));
1242 /* for the jumptable */
1243 if (ic->op == JUMPTABLE)
1245 rmask = bitVectUnion (rmask,
1246 rUmaskForOp (IC_JTCOND (ic)));
1251 /* of all other cases */
1253 rmask = bitVectUnion (rmask,
1254 rUmaskForOp (IC_LEFT (ic)));
1258 rmask = bitVectUnion (rmask,
1259 rUmaskForOp (IC_RIGHT (ic)));
1262 rmask = bitVectUnion (rmask,
1263 rUmaskForOp (IC_RESULT (ic)));
1269 /** For each instruction will determine the regsUsed.
1272 createRegMask (eBBlock ** ebbs, int count)
1276 /* for all blocks */
1277 for (i = 0; i < count; i++)
1281 if (ebbs[i]->noPath &&
1282 (ebbs[i]->entryLabel != entryLabel &&
1283 ebbs[i]->entryLabel != returnLabel))
1286 /* for all instructions */
1287 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1292 if (SKIP_IC2 (ic) || !ic->rlive)
1295 /* first mark the registers used in this
1297 ic->rUsed = regsUsedIniCode (ic);
1298 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1300 /* now create the register mask for those
1301 registers that are in use : this is a
1302 super set of ic->rUsed */
1303 ic->rMask = newBitVect (_G.nRegs + 1);
1305 /* for all live Ranges alive at this point */
1306 for (j = 1; j < ic->rlive->size; j++)
1311 /* if not alive then continue */
1312 if (!bitVectBitValue (ic->rlive, j))
1315 /* find the live range we are interested in */
1316 if (!(sym = hTabItemWithKey (liveRanges, j)))
1318 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1319 "createRegMask cannot find live range");
1323 /* if no register assigned to it */
1324 if (!sym->nRegs || sym->isspilt)
1327 /* for all the registers allocated to it */
1328 for (k = 0; k < sym->nRegs; k++)
1331 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1337 /** Returns the rematerialized string for a remat var.
1340 rematStr (symbol * sym)
1343 iCode *ic = sym->rematiCode;
1348 /* if plus or minus print the right hand side */
1349 if (ic->op == '+' || ic->op == '-')
1351 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1354 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1357 /* we reached the end */
1358 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1365 /*-----------------------------------------------------------------*/
1366 /* regTypeNum - computes the type & number of registers required */
1367 /*-----------------------------------------------------------------*/
1374 /* for each live range do */
1375 for (sym = hTabFirstItem (liveRanges, &k); sym;
1376 sym = hTabNextItem (liveRanges, &k))
1379 /* if used zero times then no registers needed */
1380 if ((sym->liveTo - sym->liveFrom) == 0)
1383 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1385 /* if the live range is a temporary */
1389 /* if the type is marked as a conditional */
1390 if (sym->regType == REG_CND)
1393 /* if used in return only then we don't
1395 if (sym->ruonly || sym->accuse)
1397 if (IS_AGGREGATE (sym->type) || sym->isptr)
1398 sym->type = aggrToPtr (sym->type, FALSE);
1402 /* if not then we require registers */
1403 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1404 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1405 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1406 getSize (sym->type));
1407 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1409 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1413 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1414 printTypeChain (sym->type, stderr);
1415 fprintf (stderr, "\n");
1418 /* determine the type of register required */
1419 /* Always general purpose */
1420 sym->regType = REG_GPR;
1425 /* for the first run we don't provide */
1426 /* registers for true symbols we will */
1427 /* see how things go */
1428 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1435 /** Mark all registers as free.
1442 D (D_ALLOC, ("freeAllRegs: running.\n"));
1444 for (i = 0; i < _G.nRegs; i++)
1445 regsZ80[i].isFree = 1;
1448 /*-----------------------------------------------------------------*/
1449 /* deallocStackSpil - this will set the stack pointer back */
1450 /*-----------------------------------------------------------------*/
1451 DEFSETFUNC (deallocStackSpil)
1459 /** Register reduction for assignment.
1462 packRegsForAssign (iCode * ic, eBBlock * ebp)
1466 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1468 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1469 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1470 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1476 /* if the true symbol is defined in far space or on stack
1477 then we should not since this will increase register pressure */
1478 if (isOperandInFarSpace (IC_RESULT (ic)))
1480 if ((dic = farSpacePackable (ic)))
1487 /* find the definition of iTempNN scanning backwards if we find a
1488 a use of the true symbol in before we find the definition then
1490 for (dic = ic->prev; dic; dic = dic->prev)
1492 /* if there is a function call and this is
1493 a parameter & not my parameter then don't pack it */
1494 if ((dic->op == CALL || dic->op == PCALL) &&
1495 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1496 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1505 if (IS_SYMOP (IC_RESULT (dic)) &&
1506 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1511 if (IS_SYMOP (IC_RIGHT (dic)) &&
1512 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1513 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1519 if (IS_SYMOP (IC_LEFT (dic)) &&
1520 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1521 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1527 if (POINTER_SET (dic) &&
1528 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1537 return 0; /* did not find */
1539 /* if the result is on stack or iaccess then it must be
1540 the same atleast one of the operands */
1541 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1542 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1545 /* the operation has only one symbol
1546 operator then we can pack */
1547 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1548 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1551 if (!((IC_LEFT (dic) &&
1552 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1554 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1558 /* found the definition */
1559 /* replace the result with the result of */
1560 /* this assignment and remove this assignment */
1561 IC_RESULT (dic) = IC_RESULT (ic);
1563 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1565 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1567 /* delete from liverange table also
1568 delete from all the points inbetween and the new
1570 for (sic = dic; sic != ic; sic = sic->next)
1572 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1573 if (IS_ITEMP (IC_RESULT (dic)))
1574 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1577 remiCodeFromeBBlock (ebp, ic);
1578 // PENDING: Check vs mcs51
1582 /** Scanning backwards looks for first assig found.
1585 findAssignToSym (operand * op, iCode * ic)
1589 for (dic = ic->prev; dic; dic = dic->prev)
1592 /* if definition by assignment */
1593 if (dic->op == '=' &&
1594 !POINTER_SET (dic) &&
1595 IC_RESULT (dic)->key == op->key)
1596 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1599 /* we are interested only if defined in far space */
1600 /* or in stack space in case of + & - */
1602 /* if assigned to a non-symbol then return
1604 if (!IS_SYMOP (IC_RIGHT (dic)))
1607 /* if the symbol is in far space then
1609 if (isOperandInFarSpace (IC_RIGHT (dic)))
1612 /* for + & - operations make sure that
1613 if it is on the stack it is the same
1614 as one of the three operands */
1615 if ((ic->op == '+' || ic->op == '-') &&
1616 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1619 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1620 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1621 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1629 /* if we find an usage then we cannot delete it */
1630 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1633 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1636 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1640 /* now make sure that the right side of dic
1641 is not defined between ic & dic */
1644 iCode *sic = dic->next;
1646 for (; sic != ic; sic = sic->next)
1647 if (IC_RESULT (sic) &&
1648 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1657 #if !DISABLE_PACKREGSFORSUPPORT
1660 /*-----------------------------------------------------------------*/
1661 /* packRegsForSupport :- reduce some registers for support calls */
1662 /*-----------------------------------------------------------------*/
1664 packRegsForSupport (iCode * ic, eBBlock * ebp)
1667 /* for the left & right operand :- look to see if the
1668 left was assigned a true symbol in far space in that
1669 case replace them */
1670 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1672 if (IS_ITEMP (IC_LEFT (ic)) &&
1673 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1675 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1681 /* found it we need to remove it from the
1683 for (sic = dic; sic != ic; sic = sic->next)
1684 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1686 IC_LEFT (ic)->operand.symOperand =
1687 IC_RIGHT (dic)->operand.symOperand;
1688 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1689 remiCodeFromeBBlock (ebp, dic);
1690 // PENDING: Check vs mcs51
1694 /* do the same for the right operand */
1697 IS_ITEMP (IC_RIGHT (ic)) &&
1698 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1700 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1706 /* found it we need to remove it from the block */
1707 for (sic = dic; sic != ic; sic = sic->next)
1708 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1710 IC_RIGHT (ic)->operand.symOperand =
1711 IC_RIGHT (dic)->operand.symOperand;
1712 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1714 remiCodeFromeBBlock (ebp, dic);
1715 // PENDING: vs mcs51
1723 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1725 /** Will reduce some registers for single use.
1728 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1734 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1736 /* if returning a literal then do nothing */
1740 /* only upto 2 bytes since we cannot predict
1741 the usage of b, & acc */
1742 if (getSize (operandType (op)) > 2 &&
1747 /* this routine will mark the a symbol as used in one
1748 instruction use only && if the defintion is local
1749 (ie. within the basic block) && has only one definition &&
1750 that definiion is either a return value from a
1751 function or does not contain any variables in
1753 uses = bitVectCopy (OP_USES (op));
1754 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1755 if (!bitVectIsZero (uses)) /* has other uses */
1758 /* if it has only one defintion */
1759 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1760 return NULL; /* has more than one definition */
1762 /* get the that definition */
1764 hTabItemWithKey (iCodehTab,
1765 bitVectFirstBit (OP_DEFS (op)))))
1768 /* found the definition now check if it is local */
1769 if (dic->seq < ebp->fSeq ||
1770 dic->seq > ebp->lSeq)
1771 return NULL; /* non-local */
1773 /* now check if it is the return from a function call */
1774 if (dic->op == CALL || dic->op == PCALL)
1776 if (ic->op != SEND && ic->op != RETURN)
1778 OP_SYMBOL (op)->ruonly = 1;
1784 /* otherwise check that the definition does
1785 not contain any symbols in far space */
1786 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1787 isOperandInFarSpace (IC_RIGHT (dic)) ||
1788 IS_OP_RUONLY (IC_LEFT (ic)) ||
1789 IS_OP_RUONLY (IC_RIGHT (ic)))
1794 /* if pointer set then make sure the pointer is one byte */
1795 if (POINTER_SET (dic))
1798 if (POINTER_GET (dic))
1803 /* also make sure the intervenening instructions
1804 don't have any thing in far space */
1805 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1807 /* if there is an intervening function call then no */
1808 if (dic->op == CALL || dic->op == PCALL)
1810 /* if pointer set then make sure the pointer
1812 if (POINTER_SET (dic))
1815 if (POINTER_GET (dic))
1818 /* if address of & the result is remat the okay */
1819 if (dic->op == ADDRESS_OF &&
1820 OP_SYMBOL (IC_RESULT (dic))->remat)
1823 /* if left or right or result is in far space */
1824 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1825 isOperandInFarSpace (IC_RIGHT (dic)) ||
1826 isOperandInFarSpace (IC_RESULT (dic)) ||
1827 IS_OP_RUONLY (IC_LEFT (dic)) ||
1828 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1829 IS_OP_RUONLY (IC_RESULT (dic)))
1835 OP_SYMBOL (op)->ruonly = 1;
1839 /*-----------------------------------------------------------------*/
1840 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1841 /*-----------------------------------------------------------------*/
1843 isBitwiseOptimizable (iCode * ic)
1845 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1847 /* bitwise operations are considered optimizable
1848 under the following conditions (Jean-Louis VERN)
1860 if (IS_LITERAL (rtype))
1866 Certian assignments involving pointers can be temporarly stored
1877 #if !DISABLE_PACKREGSFORACCUSE
1880 /** Pack registers for acc use.
1881 When the result of this operation is small and short lived it may
1882 be able to be stored in the accumelator.
1885 packRegsForAccUse (iCode * ic)
1889 /* if + or - then it has to be one byte result */
1890 if ((ic->op == '+' || ic->op == '-')
1891 && getSize (operandType (IC_RESULT (ic))) > 1)
1894 /* if shift operation make sure right side is not a literal */
1895 if (ic->op == RIGHT_OP &&
1896 (isOperandLiteral (IC_RIGHT (ic)) ||
1897 getSize (operandType (IC_RESULT (ic))) > 1))
1900 if (ic->op == LEFT_OP &&
1901 (isOperandLiteral (IC_RIGHT (ic)) ||
1902 getSize (operandType (IC_RESULT (ic))) > 1))
1905 /* has only one definition */
1906 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1909 /* has only one use */
1910 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1913 /* and the usage immediately follows this iCode */
1914 if (!(uic = hTabItemWithKey (iCodehTab,
1915 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1918 if (ic->next != uic)
1921 /* if it is a conditional branch then we definitely can */
1925 if (uic->op == JUMPTABLE)
1929 /* if the usage is not is an assignment or an
1930 arithmetic / bitwise / shift operation then not */
1931 if (POINTER_SET (uic) &&
1932 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1936 if (uic->op != '=' &&
1937 !IS_ARITHMETIC_OP (uic) &&
1938 !IS_BITWISE_OP (uic) &&
1939 uic->op != LEFT_OP &&
1940 uic->op != RIGHT_OP)
1943 /* if used in ^ operation then make sure right is not a
1945 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1948 /* if shift operation make sure right side is not a literal */
1949 if (uic->op == RIGHT_OP &&
1950 (isOperandLiteral (IC_RIGHT (uic)) ||
1951 getSize (operandType (IC_RESULT (uic))) > 1))
1954 if (uic->op == LEFT_OP &&
1955 (isOperandLiteral (IC_RIGHT (uic)) ||
1956 getSize (operandType (IC_RESULT (uic))) > 1))
1960 /* make sure that the result of this icode is not on the
1961 stack, since acc is used to compute stack offset */
1962 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1963 OP_SYMBOL (IC_RESULT (uic))->onStack)
1968 /* if either one of them in far space then we cannot */
1969 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1970 isOperandInFarSpace (IC_LEFT (uic))) ||
1971 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1972 isOperandInFarSpace (IC_RIGHT (uic))))
1976 /* if the usage has only one operand then we can */
1977 if (IC_LEFT (uic) == NULL ||
1978 IC_RIGHT (uic) == NULL)
1981 /* make sure this is on the left side if not
1982 a '+' since '+' is commutative */
1983 if (ic->op != '+' &&
1984 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1987 // See mcs51 ralloc for reasoning
1989 /* if one of them is a literal then we can */
1990 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1991 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1998 /** This is confusing :) Guess for now */
1999 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2000 (IS_ITEMP (IC_RIGHT (uic)) ||
2001 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2004 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2005 (IS_ITEMP (IC_LEFT (uic)) ||
2006 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2010 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2015 packRegsForHLUse (iCode * ic)
2022 /* has only one definition */
2023 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2026 /* has only one use */
2027 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2030 /* and the usage immediately follows this iCode */
2031 if (!(uic = hTabItemWithKey (iCodehTab,
2032 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2035 if (ic->next != uic)
2038 if (ic->op == CAST && uic->op == IPUSH)
2040 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2042 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2046 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2050 opPreservesA (iCode * ic, iCode * uic)
2054 /* If we've gotten this far then the thing to compare must be
2055 small enough and must be in A.
2060 if (uic->op == JUMPTABLE)
2062 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2066 /* A pointer assign preserves A if A is the left value. */
2067 if (uic->op == '=' && POINTER_SET (uic))
2072 /* if the usage has only one operand then we can */
2073 /* PENDING: check */
2074 if (IC_LEFT (uic) == NULL ||
2075 IC_RIGHT (uic) == NULL)
2077 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2081 /* PENDING: check this rule */
2082 if (getSize (operandType (IC_RESULT (uic))) > 1)
2084 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2089 /* Disabled all of the old rules as they weren't verified and have
2090 caused at least one problem.
2096 opIgnoresA (iCode * ic, iCode * uic)
2098 /* A increment of an iTemp by a constant is OK. */
2099 if ( uic->op == '+' &&
2100 IS_ITEMP (IC_LEFT (uic)) &&
2101 IS_ITEMP (IC_RESULT (uic)) &&
2102 IS_OP_LITERAL (IC_RIGHT (uic)))
2104 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2106 /* Being an ITEMP means that we're already a symbol. */
2108 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2119 /* Some optimisation cases:
2131 ; genAssign (pointer)
2135 want to optimise down to:
2141 So genPointer get is OK
2142 genPlus where the right is constant, left is iTemp, and result is same as left
2143 genAssign (pointer) is OK
2154 ; genAssign (pointer)
2155 ; AOP_STK for _strcpy_to_1_1
2160 want to optimise down to:
2166 So genIfx where IC_COND has size of 1 and is a constant.
2169 /** Pack registers for acc use.
2170 When the result of this operation is small and short lived it may
2171 be able to be stored in the accumulator.
2173 Note that the 'A preserving' list is currently emperical :)
2176 packRegsForAccUse2 (iCode * ic)
2180 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2182 /* Filter out all but those 'good' commands */
2184 !POINTER_GET (ic) &&
2186 !IS_BITWISE_OP (ic) &&
2192 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2196 /* if + or - then it has to be one byte result.
2199 if ((ic->op == '+' || ic->op == '-')
2200 && getSize (operandType (IC_RESULT (ic))) > 1)
2202 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2206 /* has only one definition */
2207 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2209 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2213 /* Right. We may be able to propagate it through if:
2214 For each in the chain of uses the intermediate is OK.
2216 /* Get next with 'uses result' bit on
2217 If this->next == next
2218 Validate use of next
2219 If OK, increase count
2221 /* and the usage immediately follows this iCode */
2222 if (!(uic = hTabItemWithKey (iCodehTab,
2223 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2225 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2230 /* Create a copy of the OP_USES bit vect */
2231 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2233 iCode *scan = ic, *next;
2237 setBit = bitVectFirstBit (uses);
2238 next = hTabItemWithKey (iCodehTab, setBit);
2239 if (scan->next == next)
2241 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2243 bitVectUnSetBit (uses, setBit);
2244 /* Still contigous. */
2245 if (!opPreservesA (ic, next))
2247 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2250 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2253 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2255 if (next->prev == NULL)
2257 if (!opPreservesA (ic, next))
2259 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2262 bitVectUnSetBit (uses, setBit);
2267 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2271 else if (scan->next == NULL)
2273 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2274 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2279 if (opIgnoresA (ic, scan->next))
2283 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2287 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2292 while (!bitVectIsZero (uses));
2294 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2298 /* OLD CODE FOLLOWS */
2299 /* if it is a conditional branch then we definitely can
2307 if (uic->op == JUMPTABLE)
2311 /* if the usage is not is an assignment or an
2312 arithmetic / bitwise / shift operation then not.
2313 MLH: Pending: Invalid. Our pointer sets are always peechy.
2316 if (POINTER_SET (uic) &&
2317 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2319 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2325 if (uic->op != '=' &&
2326 !IS_ARITHMETIC_OP (uic) &&
2327 !IS_BITWISE_OP (uic) &&
2328 uic->op != LEFT_OP &&
2329 uic->op != RIGHT_OP)
2335 /* if used in ^ operation then make sure right is not a
2337 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2340 /* if shift operation make sure right side is not a literal */
2341 if (uic->op == RIGHT_OP &&
2342 (isOperandLiteral (IC_RIGHT (uic)) ||
2343 getSize (operandType (IC_RESULT (uic))) > 1))
2346 if (uic->op == LEFT_OP &&
2347 (isOperandLiteral (IC_RIGHT (uic)) ||
2348 getSize (operandType (IC_RESULT (uic))) > 1))
2352 /* make sure that the result of this icode is not on the
2353 stack, since acc is used to compute stack offset */
2354 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2355 OP_SYMBOL (IC_RESULT (uic))->onStack)
2360 /* if either one of them in far space then we cannot */
2361 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2362 isOperandInFarSpace (IC_LEFT (uic))) ||
2363 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2364 isOperandInFarSpace (IC_RIGHT (uic))))
2368 /* if the usage has only one operand then we can */
2369 if (IC_LEFT (uic) == NULL ||
2370 IC_RIGHT (uic) == NULL)
2373 /* make sure this is on the left side if not
2374 a '+' since '+' is commutative */
2375 if (ic->op != '+' &&
2376 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2379 /* if one of them is a literal then we can */
2380 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2381 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2387 /** This is confusing :) Guess for now */
2388 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2389 (IS_ITEMP (IC_RIGHT (uic)) ||
2390 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2393 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2394 (IS_ITEMP (IC_LEFT (uic)) ||
2395 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2399 printf ("acc ok!\n");
2400 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2403 /** Does some transformations to reduce register pressure.
2406 packRegisters (eBBlock * ebp)
2411 D (D_ALLOC, ("packRegisters: entered.\n"));
2413 while (1 && !DISABLE_PACK_ASSIGN)
2416 /* look for assignments of the form */
2417 /* iTempNN = TRueSym (someoperation) SomeOperand */
2419 /* TrueSym := iTempNN:1 */
2420 for (ic = ebp->sch; ic; ic = ic->next)
2422 /* find assignment of the form TrueSym := iTempNN:1 */
2423 if (ic->op == '=' && !POINTER_SET (ic))
2424 change += packRegsForAssign (ic, ebp);
2430 for (ic = ebp->sch; ic; ic = ic->next)
2432 /* Safe: address of a true sym is always constant. */
2433 /* if this is an itemp & result of a address of a true sym
2434 then mark this as rematerialisable */
2435 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2437 if (ic->op == ADDRESS_OF &&
2438 IS_ITEMP (IC_RESULT (ic)) &&
2439 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2440 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2441 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2444 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2445 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2446 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2449 /* Safe: just propagates the remat flag */
2450 /* if straight assignment then carry remat flag if this is the
2452 if (ic->op == '=' &&
2453 !POINTER_SET (ic) &&
2454 IS_SYMOP (IC_RIGHT (ic)) &&
2455 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2456 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2459 OP_SYMBOL (IC_RESULT (ic))->remat =
2460 OP_SYMBOL (IC_RIGHT (ic))->remat;
2461 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2462 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2465 /* if the condition of an if instruction is defined in the
2466 previous instruction then mark the itemp as a conditional */
2467 if ((IS_CONDITIONAL (ic) ||
2468 ((ic->op == BITWISEAND ||
2471 isBitwiseOptimizable (ic))) &&
2472 ic->next && ic->next->op == IFX &&
2473 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2474 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2475 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2478 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2483 /* reduce for support function calls */
2484 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2485 packRegsForSupport (ic, ebp);
2489 /* some cases the redundant moves can
2490 can be eliminated for return statements */
2491 if ((ic->op == RETURN || ic->op == SEND) &&
2492 !isOperandInFarSpace (IC_LEFT (ic)) &&
2494 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2496 /* if pointer set & left has a size more than
2497 one and right is not in far space */
2498 if (!DISABLE_PACK_ONE_USE &&
2500 /* MLH: no such thing.
2501 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2502 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2503 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2504 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2507 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2510 /* if pointer get */
2511 if (!DISABLE_PACK_ONE_USE &&
2513 /* MLH: dont have far space
2514 !isOperandInFarSpace(IC_RESULT(ic))&& */
2515 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2516 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2517 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2520 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2522 /* pack registers for accumulator use, when the result of an
2523 arithmetic or bit wise operation has only one use, that use is
2524 immediately following the defintion and the using iCode has
2525 only one operand or has two operands but one is literal & the
2526 result of that operation is not on stack then we can leave the
2527 result of this operation in acc:b combination */
2529 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2531 packRegsForHLUse (ic);
2534 if ((IS_ARITHMETIC_OP (ic)
2535 || IS_BITWISE_OP (ic)
2536 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2538 IS_ITEMP (IC_RESULT (ic)) &&
2539 getSize (operandType (IC_RESULT (ic))) <= 2)
2540 packRegsForAccUse (ic);
2542 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2543 getSize (operandType (IC_RESULT (ic))) == 1)
2545 packRegsForAccUse2 (ic);
2551 /*-----------------------------------------------------------------*/
2552 /* assignRegisters - assigns registers to each live range as need */
2553 /*-----------------------------------------------------------------*/
2555 z80_assignRegisters (eBBlock ** ebbs, int count)
2560 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2562 setToNull ((void *) &_G.funcrUsed);
2563 _G.stackExtend = _G.dataExtend = 0;
2567 /* DE is required for the code gen. */
2568 _G.nRegs = GBZ80_MAX_REGS;
2569 regsZ80 = _gbz80_regs;
2573 _G.nRegs = Z80_MAX_REGS;
2574 regsZ80 = _z80_regs;
2577 /* change assignments this will remove some
2578 live ranges reducing some register pressure */
2579 for (i = 0; i < count; i++)
2580 packRegisters (ebbs[i]);
2582 if (options.dump_pack)
2583 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2585 /* first determine for each live range the number of
2586 registers & the type of registers required for each */
2589 /* and serially allocate registers */
2590 serialRegAssign (ebbs, count);
2592 /* if stack was extended then tell the user */
2595 /* werror(W_TOOMANY_SPILS,"stack", */
2596 /* _G.stackExtend,currFunc->name,""); */
2602 /* werror(W_TOOMANY_SPILS,"data space", */
2603 /* _G.dataExtend,currFunc->name,""); */
2607 if (options.dump_rassgn) {
2608 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2609 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2612 /* after that create the register mask
2613 for each of the instruction */
2614 createRegMask (ebbs, count);
2616 /* now get back the chain */
2617 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2619 /* redo that offsets for stacked automatic variables */
2620 redoStackOffsets ();
2624 /* free up any stackSpil locations allocated */
2625 applyToSet (_G.stackSpil, deallocStackSpil);
2627 setToNull ((void **) &_G.stackSpil);
2628 setToNull ((void **) &_G.spiltSet);
2629 /* mark all registers as free */