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
51 DISABLE_PACK_ASSIGN = 0,
52 DISABLE_PACK_ONE_USE = 0,
66 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
71 #define DISABLE_PACKREGSFORSUPPORT 1
72 #define DISABLE_PACKREGSFORACCUSE 1
74 extern void genZ80Code (iCode *);
76 /** Local static variables */
84 /* registers used in a function */
91 static regs _gbz80_regs[] =
93 {REG_GPR, C_IDX, "c", 1},
94 {REG_GPR, B_IDX, "b", 1},
95 {REG_CND, CND_IDX, "c", 1}
98 static regs _z80_regs[] =
100 {REG_GPR, C_IDX, "c", 1},
101 {REG_GPR, B_IDX, "b", 1},
102 {REG_GPR, E_IDX, "e", 1},
103 {REG_GPR, D_IDX, "d", 1},
104 {REG_CND, CND_IDX, "c", 1}
109 /** Number of usable registers (all but C) */
110 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
111 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
113 static void spillThis (symbol *);
115 /** Allocates register of given type.
116 'type' is not used on the z80 version. It was used to select
117 between pointer and general purpose registers on the mcs51 version.
119 @return Pointer to the newly allocated register.
122 allocReg (short type)
126 for (i = 0; i < _G.nRegs; i++)
128 /* For now we allocate from any free */
129 if (regsZ80[i].isFree)
131 regsZ80[i].isFree = 0;
134 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
136 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
140 D (D_ALLOC, ("allocReg: No free.\n"));
144 /** Returns pointer to register wit index number
151 for (i = 0; i < _G.nRegs; i++)
153 if (regsZ80[i].rIdx == idx)
159 wassertl (0, "regWithIdx not found");
163 /** Frees a register.
168 wassert (!reg->isFree);
170 D (D_ALLOC, ("freeReg: freed %p\n", reg));
174 /** Returns number of free registers.
182 for (i = 0; i < _G.nRegs; i++)
184 /* For now only one reg type */
185 if (regsZ80[i].isFree)
193 /** Free registers with type.
196 nfreeRegsType (int type)
201 if ((nfr = nFreeRegs (type)) == 0)
203 return nFreeRegs (REG_GPR);
207 return nFreeRegs (type);
212 /*-----------------------------------------------------------------*/
213 /* allDefsOutOfRange - all definitions are out of a range */
214 /*-----------------------------------------------------------------*/
216 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
223 for (i = 0; i < defs->size; i++)
227 if (bitVectBitValue (defs, i) &&
228 (ic = hTabItemWithKey (iCodehTab, i)) &&
229 (ic->seq >= fseq && ic->seq <= toseq))
239 /*-----------------------------------------------------------------*/
240 /* computeSpillable - given a point find the spillable live ranges */
241 /*-----------------------------------------------------------------*/
243 computeSpillable (iCode * ic)
247 /* spillable live ranges are those that are live at this
248 point . the following categories need to be subtracted
250 a) - those that are already spilt
251 b) - if being used by this one
252 c) - defined by this one */
254 spillable = bitVectCopy (ic->rlive);
256 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
258 bitVectCplAnd (spillable, ic->uses); /* used in this one */
259 bitVectUnSetBit (spillable, ic->defKey);
260 spillable = bitVectIntersect (spillable, _G.regAssigned);
265 /*-----------------------------------------------------------------*/
266 /* noSpilLoc - return true if a variable has no spil location */
267 /*-----------------------------------------------------------------*/
269 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
271 return (sym->usl.spillLoc ? 0 : 1);
274 /*-----------------------------------------------------------------*/
275 /* hasSpilLoc - will return 1 if the symbol has spil location */
276 /*-----------------------------------------------------------------*/
278 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
280 return (sym->usl.spillLoc ? 1 : 0);
283 /** Will return 1 if the remat flag is set.
284 A symbol is rematerialisable if it doesnt need to be allocated
285 into registers at creation as it can be re-created at any time -
286 i.e. it's constant in some way.
289 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
294 /*-----------------------------------------------------------------*/
295 /* allLRs - return true for all */
296 /*-----------------------------------------------------------------*/
298 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
303 /** liveRangesWith - applies function to a given set of live range
306 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
307 eBBlock * ebp, iCode * ic)
312 if (!lrs || !lrs->size)
315 for (i = 1; i < lrs->size; i++)
318 if (!bitVectBitValue (lrs, i))
321 /* if we don't find it in the live range
322 hash table we are in serious trouble */
323 if (!(sym = hTabItemWithKey (liveRanges, i)))
325 wassertl (0, "liveRangesWith could not find liveRange");
329 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
331 addSetHead (&rset, sym);
339 /** leastUsedLR - given a set determines which is the least used
342 leastUsedLR (set * sset)
344 symbol *sym = NULL, *lsym = NULL;
346 sym = lsym = setFirstItem (sset);
351 for (; lsym; lsym = setNextItem (sset))
354 /* if usage is the same then prefer
355 the spill the smaller of the two */
356 if (lsym->used == sym->used)
357 if (getSize (lsym->type) < getSize (sym->type))
361 if (lsym->used < sym->used)
366 setToNull ((void **) &sset);
371 /** noOverLap - will iterate through the list looking for over lap
374 noOverLap (set * itmpStack, symbol * fsym)
378 for (sym = setFirstItem (itmpStack); sym;
379 sym = setNextItem (itmpStack))
381 // if sym starts before (or on) our end point
382 // and ends after (or on) our start point,
384 if (sym->liveFrom <= fsym->liveTo &&
385 sym->liveTo >= fsym->liveFrom)
393 /*-----------------------------------------------------------------*/
394 /* isFree - will return 1 if the a free spil location is found */
395 /*-----------------------------------------------------------------*/
399 V_ARG (symbol **, sloc);
400 V_ARG (symbol *, fsym);
402 /* if already found */
406 /* if it is free && and the itmp assigned to
407 this does not have any overlapping live ranges
408 with the one currently being assigned and
409 the size can be accomodated */
411 noOverLap (sym->usl.itmpStack, fsym) &&
412 getSize (sym->type) >= getSize (fsym->type))
421 /*-----------------------------------------------------------------*/
422 /* createStackSpil - create a location on the stack to spil */
423 /*-----------------------------------------------------------------*/
425 createStackSpil (symbol * sym)
429 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
431 /* first go try and find a free one that is already
432 existing on the stack */
433 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
435 /* found a free one : just update & return */
436 sym->usl.spillLoc = sloc;
439 addSetHead (&sloc->usl.itmpStack, sym);
440 D (D_ALLOC, ("createStackSpil: found existing\n"));
444 /* could not then have to create one , this is the hard part
445 we need to allocate this on the stack : this is really a
446 hack!! but cannot think of anything better at this time */
448 sprintf (buffer, "sloc%d", _G.slocNum++);
449 sloc = newiTemp (buffer);
451 /* set the type to the spilling symbol */
452 sloc->type = copyLinkChain (sym->type);
453 sloc->etype = getSpec (sloc->type);
454 SPEC_SCLS (sloc->etype) = S_AUTO;
458 sloc->isref = 1; /* to prevent compiler warning */
460 /* if it is on the stack then update the stack */
461 if (IN_STACK (sloc->etype))
463 currFunc->stack += getSize (sloc->type);
464 _G.stackExtend += getSize (sloc->type);
468 _G.dataExtend += getSize (sloc->type);
471 /* add it to the stackSpil set */
472 addSetHead (&_G.stackSpil, sloc);
473 sym->usl.spillLoc = sloc;
476 /* add it to the set of itempStack set
477 of the spill location */
478 addSetHead (&sloc->usl.itmpStack, sym);
480 D (D_ALLOC, ("createStackSpil: created new\n"));
484 /*-----------------------------------------------------------------*/
485 /* spillThis - spils a specific operand */
486 /*-----------------------------------------------------------------*/
488 spillThis (symbol * sym)
492 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
494 /* if this is rematerializable or has a spillLocation
495 we are okay, else we need to create a spillLocation
497 if (!(sym->remat || sym->usl.spillLoc))
499 createStackSpil (sym);
502 /* mark it has spilt & put it in the spilt set */
504 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
506 bitVectUnSetBit (_G.regAssigned, sym->key);
508 for (i = 0; i < sym->nRegs; i++)
512 freeReg (sym->regs[i]);
517 if (sym->usl.spillLoc && !sym->remat)
519 sym->usl.spillLoc->allocreq = 1;
525 /*-----------------------------------------------------------------*/
526 /* allDefsOutOfRange - all definitions are out of a range */
527 /*-----------------------------------------------------------------*/
529 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
536 for (i = 0; i < defs->size; i++)
540 if (bitVectBitValue (defs, i) &&
541 (ic = hTabItemWithKey (iCodehTab, i)) &&
542 (ic->seq >= fseq && ic->seq <= toseq))
551 /*-----------------------------------------------------------------*/
552 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
553 /* but is not used as a pointer */
554 /*-----------------------------------------------------------------*/
556 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
558 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
561 /*-----------------------------------------------------------------*/
562 /* notUsedInBlock - not used in this block */
563 /*-----------------------------------------------------------------*/
565 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
567 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
568 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
569 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
572 /*-----------------------------------------------------------------*/
573 /* notUsedInRemaining - not used or defined in remain of the block */
574 /*-----------------------------------------------------------------*/
576 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
578 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
579 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
583 /** Select a iTemp to spil : rather a simple procedure.
586 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
588 bitVect *lrcs = NULL;
592 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
593 /* get the spillable live ranges */
594 lrcs = computeSpillable (ic);
596 /* get all live ranges that are rematerizable */
597 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
599 D (D_ALLOC, ("selectSpil: using remat.\n"));
600 /* return the least used of these */
601 return leastUsedLR (selectS);
605 /* get live ranges with spillLocations in direct space */
606 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
608 sym = leastUsedLR (selectS);
609 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
610 sym->usl.spillLoc->rname :
611 sym->usl.spillLoc->name));
613 /* mark it as allocation required */
614 sym->usl.spillLoc->allocreq = 1;
618 /* if the symbol is local to the block then */
619 if (forSym->liveTo < ebp->lSeq)
622 /* check if there are any live ranges allocated
623 to registers that are not used in this block */
624 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
626 sym = leastUsedLR (selectS);
627 /* if this is not rematerializable */
631 wassertl (0, "Attempted to do an unsupported block spill");
637 /* check if there are any live ranges that not
638 used in the remainder of the block */
639 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
641 sym = leastUsedLR (selectS);
646 wassertl (0, "Attempted to do an unsupported remain spill");
654 /* find live ranges with spillocation && not used as pointers */
655 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
658 sym = leastUsedLR (selectS);
659 /* mark this as allocation required */
660 sym->usl.spillLoc->allocreq = 1;
665 /* find live ranges with spillocation */
666 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
668 D (D_ALLOC, ("selectSpil: using with spill.\n"));
669 sym = leastUsedLR (selectS);
670 sym->usl.spillLoc->allocreq = 1;
674 /* couldn't find then we need to create a spil
675 location on the stack , for which one? the least
677 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
679 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
680 /* return a created spil location */
681 sym = createStackSpil (leastUsedLR (selectS));
682 sym->usl.spillLoc->allocreq = 1;
686 /* this is an extreme situation we will spill
687 this one : happens very rarely but it does happen */
688 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
694 /** Spil some variable & mark registers as free.
695 A spill occurs when an iTemp wont fit into the available registers.
698 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
703 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
705 /* get something we can spil */
706 ssym = selectSpil (ic, ebp, forSym);
708 /* mark it as spilt */
710 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
712 /* mark it as not register assigned &
713 take it away from the set */
714 bitVectUnSetBit (_G.regAssigned, ssym->key);
716 /* mark the registers as free */
717 for (i = 0; i < ssym->nRegs; i++)
719 freeReg (ssym->regs[i]);
721 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
722 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
724 /* if spilt on stack then free up r0 & r1
725 if they could have been assigned to as gprs */
726 if (!ptrRegReq && isSpiltOnStack (ssym))
729 spillLRWithPtrReg (ssym);
732 /* if this was a block level spil then insert push & pop
733 at the start & end of block respectively */
736 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
737 /* add push to the start of the block */
738 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
739 ebp->sch->next : ebp->sch));
740 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
741 /* add pop to the end of the block */
742 addiCodeToeBBlock (ebp, nic, NULL);
745 /* if spilt because not used in the remainder of the
746 block then add a push before this instruction and
747 a pop at the end of the block */
748 if (ssym->remainSpil)
751 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
752 /* add push just before this instruction */
753 addiCodeToeBBlock (ebp, nic, ic);
755 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
756 /* add pop to the end of the block */
757 addiCodeToeBBlock (ebp, nic, NULL);
761 D (D_ALLOC, ("spilSomething: done.\n"));
769 /** Will try for GPR if not spil.
772 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
776 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
778 /* try for gpr type */
779 if ((reg = allocReg (REG_GPR)))
781 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
785 /* we have to spil */
786 if (!spilSomething (ic, ebp, sym))
788 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
792 /* this looks like an infinite loop but
793 in really selectSpil will abort */
797 /** Symbol has a given register.
800 symHasReg (symbol * sym, regs * reg)
804 for (i = 0; i < sym->nRegs; i++)
805 if (sym->regs[i] == reg)
811 /** Check the live to and if they have registers & are not spilt then
812 free up the registers
815 deassignLRs (iCode * ic, eBBlock * ebp)
821 for (sym = hTabFirstItem (liveRanges, &k); sym;
822 sym = hTabNextItem (liveRanges, &k))
826 /* if it does not end here */
827 if (sym->liveTo > ic->seq)
830 /* if it was spilt on stack then we can
831 mark the stack spil location as free */
836 sym->usl.spillLoc->isFree = 1;
842 if (!bitVectBitValue (_G.regAssigned, sym->key))
845 /* special case check if this is an IFX &
846 the privious one was a pop and the
847 previous one was not spilt then keep track
849 if (ic->op == IFX && ic->prev &&
850 ic->prev->op == IPOP &&
851 !ic->prev->parmPush &&
852 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
853 psym = OP_SYMBOL (IC_LEFT (ic->prev));
855 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
861 bitVectUnSetBit (_G.regAssigned, sym->key);
863 /* if the result of this one needs registers
864 and does not have it then assign it right
866 if (IC_RESULT (ic) &&
867 !(SKIP_IC2 (ic) || /* not a special icode */
868 ic->op == JUMPTABLE ||
873 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
874 result->liveTo > ic->seq && /* and will live beyond this */
875 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
876 result->regType == sym->regType && /* same register types */
877 result->nRegs && /* which needs registers */
878 !result->isspilt && /* and does not already have them */
880 !bitVectBitValue (_G.regAssigned, result->key) &&
881 /* the number of free regs + number of regs in this LR
882 can accomodate the what result Needs */
883 ((nfreeRegsType (result->regType) +
884 sym->nRegs) >= result->nRegs)
887 for (i = 0; i < result->nRegs; i++)
890 result->regs[i] = sym->regs[i];
892 result->regs[i] = getRegGpr (ic, ebp, result);
894 /* if the allocation falied which means
895 this was spilt then break */
896 if (!result->regs[i])
904 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
907 /* free the remaining */
908 for (; i < sym->nRegs; i++)
912 if (!symHasReg (psym, sym->regs[i]))
913 freeReg (sym->regs[i]);
916 freeReg (sym->regs[i]);
917 // sym->regs[i] = NULL;
924 /** Reassign this to registers.
927 reassignLR (operand * op)
929 symbol *sym = OP_SYMBOL (op);
932 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
934 /* not spilt any more */
935 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
936 bitVectUnSetBit (_G.spiltSet, sym->key);
938 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
942 for (i = 0; i < sym->nRegs; i++)
943 sym->regs[i]->isFree = 0;
946 /** Determines if allocating will cause a spill.
949 willCauseSpill (int nr, int rt)
951 /* first check if there are any avlb registers
952 of te type required */
953 if (nFreeRegs (0) >= nr)
956 /* it will cause a spil */
960 /** The allocator can allocate same registers to result and operand,
961 if this happens make sure they are in the same position as the operand
962 otherwise chaos results.
965 positionRegs (symbol * result, symbol * opsym, int lineno)
967 int count = min (result->nRegs, opsym->nRegs);
968 int i, j = 0, shared = 0;
970 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
972 /* if the result has been spilt then cannot share */
977 /* first make sure that they actually share */
978 for (i = 0; i < count; i++)
980 for (j = 0; j < count; j++)
982 if (result->regs[i] == opsym->regs[j] && i != j)
992 regs *tmp = result->regs[i];
993 result->regs[i] = result->regs[j];
994 result->regs[j] = tmp;
999 /** Try to allocate a pair of registers to the symbol.
1002 tryAllocatingRegPair (symbol * sym)
1005 wassert (sym->nRegs == 2);
1006 for (i = 0; i < _G.nRegs; i += 2)
1008 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1010 regsZ80[i].isFree = 0;
1011 sym->regs[0] = ®sZ80[i];
1012 regsZ80[i + 1].isFree = 0;
1013 sym->regs[1] = ®sZ80[i + 1];
1016 currFunc->regsUsed =
1017 bitVectSetBit (currFunc->regsUsed, i);
1018 currFunc->regsUsed =
1019 bitVectSetBit (currFunc->regsUsed, i + 1);
1021 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1025 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1029 /** Serially allocate registers to the variables.
1030 This is the main register allocation function. It is called after
1034 serialRegAssign (eBBlock ** ebbs, int count)
1038 /* for all blocks */
1039 for (i = 0; i < count; i++)
1044 if (ebbs[i]->noPath &&
1045 (ebbs[i]->entryLabel != entryLabel &&
1046 ebbs[i]->entryLabel != returnLabel))
1049 /* of all instructions do */
1050 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1053 /* if this is an ipop that means some live
1054 range will have to be assigned again */
1058 reassignLR (IC_LEFT (ic));
1061 /* if result is present && is a true symbol */
1062 if (IC_RESULT (ic) && ic->op != IFX &&
1063 IS_TRUE_SYMOP (IC_RESULT (ic)))
1064 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1066 /* take away registers from live
1067 ranges that end at this instruction */
1068 deassignLRs (ic, ebbs[i]);
1070 /* some don't need registers */
1071 /* MLH: removed RESULT and POINTER_SET condition */
1072 if (SKIP_IC2 (ic) ||
1073 ic->op == JUMPTABLE ||
1079 /* now we need to allocate registers only for the result */
1082 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1087 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1089 /* if it does not need or is spilt
1090 or is already assigned to registers
1091 or will not live beyond this instructions */
1094 bitVectBitValue (_G.regAssigned, sym->key) ||
1095 sym->liveTo <= ic->seq)
1097 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1101 /* if some liverange has been spilt at the block level
1102 and this one live beyond this block then spil this
1104 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1106 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1110 /* if trying to allocate this will cause
1111 a spill and there is nothing to spill
1112 or this one is rematerializable then
1114 willCS = willCauseSpill (sym->nRegs, sym->regType);
1115 spillable = computeSpillable (ic);
1117 (willCS && bitVectIsZero (spillable)))
1120 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1126 /* if it has a spillocation & is used less than
1127 all other live ranges then spill this */
1129 if (sym->usl.spillLoc) {
1130 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1131 allLRs, ebbs[i], ic));
1132 if (leastUsed && leastUsed->used > sym->used) {
1137 /* if none of the liveRanges have a spillLocation then better
1138 to spill this one than anything else already assigned to registers */
1139 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1146 /* else we assign registers to it */
1147 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1149 /* Special case: Try to fit into a reg pair if
1151 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1152 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1157 for (j = 0; j < sym->nRegs; j++)
1159 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1161 /* if the allocation falied which means
1162 this was spilt then break */
1165 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1170 /* if it shares registers with operands make sure
1171 that they are in the same position */
1172 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1173 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1174 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1175 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1176 /* do the same for the right operand */
1177 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1178 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1179 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1180 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1187 /*-----------------------------------------------------------------*/
1188 /* rUmaskForOp :- returns register mask for an operand */
1189 /*-----------------------------------------------------------------*/
1191 rUmaskForOp (operand * op)
1197 /* only temporaries are assigned registers */
1201 sym = OP_SYMBOL (op);
1203 /* if spilt or no registers assigned to it
1205 if (sym->isspilt || !sym->nRegs)
1208 rumask = newBitVect (_G.nRegs);
1210 for (j = 0; j < sym->nRegs; j++)
1212 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1219 z80_rUmaskForOp (operand * op)
1221 return rUmaskForOp (op);
1224 /** Returns bit vector of registers used in iCode.
1227 regsUsedIniCode (iCode * ic)
1229 bitVect *rmask = newBitVect (_G.nRegs);
1231 /* do the special cases first */
1234 rmask = bitVectUnion (rmask,
1235 rUmaskForOp (IC_COND (ic)));
1239 /* for the jumptable */
1240 if (ic->op == JUMPTABLE)
1242 rmask = bitVectUnion (rmask,
1243 rUmaskForOp (IC_JTCOND (ic)));
1248 /* of all other cases */
1250 rmask = bitVectUnion (rmask,
1251 rUmaskForOp (IC_LEFT (ic)));
1255 rmask = bitVectUnion (rmask,
1256 rUmaskForOp (IC_RIGHT (ic)));
1259 rmask = bitVectUnion (rmask,
1260 rUmaskForOp (IC_RESULT (ic)));
1266 /** For each instruction will determine the regsUsed.
1269 createRegMask (eBBlock ** ebbs, int count)
1273 /* for all blocks */
1274 for (i = 0; i < count; i++)
1278 if (ebbs[i]->noPath &&
1279 (ebbs[i]->entryLabel != entryLabel &&
1280 ebbs[i]->entryLabel != returnLabel))
1283 /* for all instructions */
1284 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1289 if (SKIP_IC2 (ic) || !ic->rlive)
1292 /* first mark the registers used in this
1294 ic->rUsed = regsUsedIniCode (ic);
1295 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1297 /* now create the register mask for those
1298 registers that are in use : this is a
1299 super set of ic->rUsed */
1300 ic->rMask = newBitVect (_G.nRegs + 1);
1302 /* for all live Ranges alive at this point */
1303 for (j = 1; j < ic->rlive->size; j++)
1308 /* if not alive then continue */
1309 if (!bitVectBitValue (ic->rlive, j))
1312 /* find the live range we are interested in */
1313 if (!(sym = hTabItemWithKey (liveRanges, j)))
1315 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1316 "createRegMask cannot find live range");
1320 /* if no register assigned to it */
1321 if (!sym->nRegs || sym->isspilt)
1324 /* for all the registers allocated to it */
1325 for (k = 0; k < sym->nRegs; k++)
1328 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1334 /** Returns the rematerialized string for a remat var.
1337 rematStr (symbol * sym)
1340 iCode *ic = sym->rematiCode;
1345 /* if plus or minus print the right hand side */
1346 if (ic->op == '+' || ic->op == '-')
1348 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1351 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1354 /* we reached the end */
1355 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1362 /*-----------------------------------------------------------------*/
1363 /* regTypeNum - computes the type & number of registers required */
1364 /*-----------------------------------------------------------------*/
1371 /* for each live range do */
1372 for (sym = hTabFirstItem (liveRanges, &k); sym;
1373 sym = hTabNextItem (liveRanges, &k))
1376 /* if used zero times then no registers needed */
1377 if ((sym->liveTo - sym->liveFrom) == 0)
1380 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1382 /* if the live range is a temporary */
1386 /* if the type is marked as a conditional */
1387 if (sym->regType == REG_CND)
1390 /* if used in return only then we don't
1392 if (sym->ruonly || sym->accuse)
1394 if (IS_AGGREGATE (sym->type) || sym->isptr)
1395 sym->type = aggrToPtr (sym->type, FALSE);
1399 /* if not then we require registers */
1400 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1401 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1402 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1403 getSize (sym->type));
1404 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1406 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1410 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1411 printTypeChain (sym->type, stderr);
1412 fprintf (stderr, "\n");
1415 /* determine the type of register required */
1416 /* Always general purpose */
1417 sym->regType = REG_GPR;
1422 /* for the first run we don't provide */
1423 /* registers for true symbols we will */
1424 /* see how things go */
1425 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1432 /** Mark all registers as free.
1439 D (D_ALLOC, ("freeAllRegs: running.\n"));
1441 for (i = 0; i < _G.nRegs; i++)
1442 regsZ80[i].isFree = 1;
1445 /*-----------------------------------------------------------------*/
1446 /* deallocStackSpil - this will set the stack pointer back */
1447 /*-----------------------------------------------------------------*/
1448 DEFSETFUNC (deallocStackSpil)
1456 /** Register reduction for assignment.
1459 packRegsForAssign (iCode * ic, eBBlock * ebp)
1463 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1465 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1466 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1467 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1473 /* if the true symbol is defined in far space or on stack
1474 then we should not since this will increase register pressure */
1475 if (isOperandInFarSpace (IC_RESULT (ic)))
1477 if ((dic = farSpacePackable (ic)))
1484 /* find the definition of iTempNN scanning backwards if we find a
1485 a use of the true symbol in before we find the definition then
1487 for (dic = ic->prev; dic; dic = dic->prev)
1489 /* if there is a function call and this is
1490 a parameter & not my parameter then don't pack it */
1491 if ((dic->op == CALL || dic->op == PCALL) &&
1492 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1493 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1502 if (IS_SYMOP (IC_RESULT (dic)) &&
1503 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1508 if (IS_SYMOP (IC_RIGHT (dic)) &&
1509 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1510 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1516 if (IS_SYMOP (IC_LEFT (dic)) &&
1517 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1518 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1524 if (POINTER_SET (dic) &&
1525 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1534 return 0; /* did not find */
1536 /* if the result is on stack or iaccess then it must be
1537 the same atleast one of the operands */
1538 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1539 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1542 /* the operation has only one symbol
1543 operator then we can pack */
1544 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1545 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1548 if (!((IC_LEFT (dic) &&
1549 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1551 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1555 /* found the definition */
1556 /* replace the result with the result of */
1557 /* this assignment and remove this assignment */
1558 IC_RESULT (dic) = IC_RESULT (ic);
1560 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1562 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1564 /* delete from liverange table also
1565 delete from all the points inbetween and the new
1567 for (sic = dic; sic != ic; sic = sic->next)
1569 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1570 if (IS_ITEMP (IC_RESULT (dic)))
1571 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1574 remiCodeFromeBBlock (ebp, ic);
1575 // PENDING: Check vs mcs51
1579 /** Scanning backwards looks for first assig found.
1582 findAssignToSym (operand * op, iCode * ic)
1586 for (dic = ic->prev; dic; dic = dic->prev)
1589 /* if definition by assignment */
1590 if (dic->op == '=' &&
1591 !POINTER_SET (dic) &&
1592 IC_RESULT (dic)->key == op->key)
1593 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1596 /* we are interested only if defined in far space */
1597 /* or in stack space in case of + & - */
1599 /* if assigned to a non-symbol then return
1601 if (!IS_SYMOP (IC_RIGHT (dic)))
1604 /* if the symbol is in far space then
1606 if (isOperandInFarSpace (IC_RIGHT (dic)))
1609 /* for + & - operations make sure that
1610 if it is on the stack it is the same
1611 as one of the three operands */
1612 if ((ic->op == '+' || ic->op == '-') &&
1613 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1616 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1617 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1618 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1626 /* if we find an usage then we cannot delete it */
1627 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1630 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1633 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1637 /* now make sure that the right side of dic
1638 is not defined between ic & dic */
1641 iCode *sic = dic->next;
1643 for (; sic != ic; sic = sic->next)
1644 if (IC_RESULT (sic) &&
1645 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1654 #if !DISABLE_PACKREGSFORSUPPORT
1657 /*-----------------------------------------------------------------*/
1658 /* packRegsForSupport :- reduce some registers for support calls */
1659 /*-----------------------------------------------------------------*/
1661 packRegsForSupport (iCode * ic, eBBlock * ebp)
1664 /* for the left & right operand :- look to see if the
1665 left was assigned a true symbol in far space in that
1666 case replace them */
1667 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1669 if (IS_ITEMP (IC_LEFT (ic)) &&
1670 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1672 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1678 /* found it we need to remove it from the
1680 for (sic = dic; sic != ic; sic = sic->next)
1681 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1683 IC_LEFT (ic)->operand.symOperand =
1684 IC_RIGHT (dic)->operand.symOperand;
1685 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1686 remiCodeFromeBBlock (ebp, dic);
1687 // PENDING: Check vs mcs51
1691 /* do the same for the right operand */
1694 IS_ITEMP (IC_RIGHT (ic)) &&
1695 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1697 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1703 /* found it we need to remove it from the block */
1704 for (sic = dic; sic != ic; sic = sic->next)
1705 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1707 IC_RIGHT (ic)->operand.symOperand =
1708 IC_RIGHT (dic)->operand.symOperand;
1709 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1711 remiCodeFromeBBlock (ebp, dic);
1712 // PENDING: vs mcs51
1720 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1722 /** Will reduce some registers for single use.
1725 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1731 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1733 /* if returning a literal then do nothing */
1737 /* only upto 2 bytes since we cannot predict
1738 the usage of b, & acc */
1739 if (getSize (operandType (op)) > 2 &&
1744 /* this routine will mark the a symbol as used in one
1745 instruction use only && if the defintion is local
1746 (ie. within the basic block) && has only one definition &&
1747 that definiion is either a return value from a
1748 function or does not contain any variables in
1750 uses = bitVectCopy (OP_USES (op));
1751 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1752 if (!bitVectIsZero (uses)) /* has other uses */
1755 /* if it has only one defintion */
1756 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1757 return NULL; /* has more than one definition */
1759 /* get the that definition */
1761 hTabItemWithKey (iCodehTab,
1762 bitVectFirstBit (OP_DEFS (op)))))
1765 /* found the definition now check if it is local */
1766 if (dic->seq < ebp->fSeq ||
1767 dic->seq > ebp->lSeq)
1768 return NULL; /* non-local */
1770 /* now check if it is the return from a function call */
1771 if (dic->op == CALL || dic->op == PCALL)
1773 if (ic->op != SEND && ic->op != RETURN)
1775 OP_SYMBOL (op)->ruonly = 1;
1781 /* otherwise check that the definition does
1782 not contain any symbols in far space */
1783 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1784 isOperandInFarSpace (IC_RIGHT (dic)) ||
1785 IS_OP_RUONLY (IC_LEFT (ic)) ||
1786 IS_OP_RUONLY (IC_RIGHT (ic)))
1791 /* if pointer set then make sure the pointer is one byte */
1792 if (POINTER_SET (dic))
1795 if (POINTER_GET (dic))
1800 /* also make sure the intervenening instructions
1801 don't have any thing in far space */
1802 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1804 /* if there is an intervening function call then no */
1805 if (dic->op == CALL || dic->op == PCALL)
1807 /* if pointer set then make sure the pointer
1809 if (POINTER_SET (dic))
1812 if (POINTER_GET (dic))
1815 /* if address of & the result is remat the okay */
1816 if (dic->op == ADDRESS_OF &&
1817 OP_SYMBOL (IC_RESULT (dic))->remat)
1820 /* if left or right or result is in far space */
1821 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1822 isOperandInFarSpace (IC_RIGHT (dic)) ||
1823 isOperandInFarSpace (IC_RESULT (dic)) ||
1824 IS_OP_RUONLY (IC_LEFT (dic)) ||
1825 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1826 IS_OP_RUONLY (IC_RESULT (dic)))
1832 OP_SYMBOL (op)->ruonly = 1;
1836 /*-----------------------------------------------------------------*/
1837 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1838 /*-----------------------------------------------------------------*/
1840 isBitwiseOptimizable (iCode * ic)
1842 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1844 /* bitwise operations are considered optimizable
1845 under the following conditions (Jean-Louis VERN)
1857 if (IS_LITERAL (rtype))
1863 Certian assignments involving pointers can be temporarly stored
1874 #if !DISABLE_PACKREGSFORACCUSE
1877 /** Pack registers for acc use.
1878 When the result of this operation is small and short lived it may
1879 be able to be stored in the accumelator.
1882 packRegsForAccUse (iCode * ic)
1886 /* if + or - then it has to be one byte result */
1887 if ((ic->op == '+' || ic->op == '-')
1888 && getSize (operandType (IC_RESULT (ic))) > 1)
1891 /* if shift operation make sure right side is not a literal */
1892 if (ic->op == RIGHT_OP &&
1893 (isOperandLiteral (IC_RIGHT (ic)) ||
1894 getSize (operandType (IC_RESULT (ic))) > 1))
1897 if (ic->op == LEFT_OP &&
1898 (isOperandLiteral (IC_RIGHT (ic)) ||
1899 getSize (operandType (IC_RESULT (ic))) > 1))
1902 /* has only one definition */
1903 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1906 /* has only one use */
1907 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1910 /* and the usage immediately follows this iCode */
1911 if (!(uic = hTabItemWithKey (iCodehTab,
1912 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1915 if (ic->next != uic)
1918 /* if it is a conditional branch then we definitely can */
1922 if (uic->op == JUMPTABLE)
1926 /* if the usage is not is an assignment or an
1927 arithmetic / bitwise / shift operation then not */
1928 if (POINTER_SET (uic) &&
1929 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1933 if (uic->op != '=' &&
1934 !IS_ARITHMETIC_OP (uic) &&
1935 !IS_BITWISE_OP (uic) &&
1936 uic->op != LEFT_OP &&
1937 uic->op != RIGHT_OP)
1940 /* if used in ^ operation then make sure right is not a
1942 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1945 /* if shift operation make sure right side is not a literal */
1946 if (uic->op == RIGHT_OP &&
1947 (isOperandLiteral (IC_RIGHT (uic)) ||
1948 getSize (operandType (IC_RESULT (uic))) > 1))
1951 if (uic->op == LEFT_OP &&
1952 (isOperandLiteral (IC_RIGHT (uic)) ||
1953 getSize (operandType (IC_RESULT (uic))) > 1))
1957 /* make sure that the result of this icode is not on the
1958 stack, since acc is used to compute stack offset */
1959 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1960 OP_SYMBOL (IC_RESULT (uic))->onStack)
1965 /* if either one of them in far space then we cannot */
1966 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1967 isOperandInFarSpace (IC_LEFT (uic))) ||
1968 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1969 isOperandInFarSpace (IC_RIGHT (uic))))
1973 /* if the usage has only one operand then we can */
1974 if (IC_LEFT (uic) == NULL ||
1975 IC_RIGHT (uic) == NULL)
1978 /* make sure this is on the left side if not
1979 a '+' since '+' is commutative */
1980 if (ic->op != '+' &&
1981 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1984 // See mcs51 ralloc for reasoning
1986 /* if one of them is a literal then we can */
1987 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1988 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1995 /** This is confusing :) Guess for now */
1996 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1997 (IS_ITEMP (IC_RIGHT (uic)) ||
1998 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2001 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2002 (IS_ITEMP (IC_LEFT (uic)) ||
2003 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2007 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2012 packRegsForHLUse (iCode * ic)
2019 /* has only one definition */
2020 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2023 /* has only one use */
2024 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2027 /* and the usage immediately follows this iCode */
2028 if (!(uic = hTabItemWithKey (iCodehTab,
2029 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2032 if (ic->next != uic)
2035 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2037 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2041 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2045 opPreservesA (iCode * ic, iCode * uic)
2051 if (getSize (operandType (IC_COND (uic))) == 1 &&
2052 IS_OP_LITERAL (IC_COND (uic)))
2057 D (D_ACCUSE2, (" + Dropping as operation is an IFX\n"));
2061 if (uic->op == JUMPTABLE)
2063 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2067 /* A pointer assign preserves A if A is the left value. */
2068 if (uic->op == '=' && POINTER_SET (uic))
2073 /* if the usage has only one operand then we can */
2074 /* PENDING: check */
2075 if (IC_LEFT (uic) == NULL ||
2076 IC_RIGHT (uic) == NULL)
2078 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2082 /* PENDING: check this rule */
2083 if (getSize (operandType (IC_RESULT (uic))) > 1)
2085 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2091 !IS_ARITHMETIC_OP(uic) (sub requires A)
2095 !IS_BITWISE_OP (uic) &&
2098 !POINTER_GET (uic) &&
2100 uic->op != LEFT_OP &&
2101 uic->op != RIGHT_OP && */
2105 D (D_ACCUSE2, (" + Dropping as 'its a bad op'\n"));
2110 if (!IC_LEFT (uic) || !IC_RESULT (ic))
2112 D (D_ACCUSE2, (" + Dropping for some reason #1\n"));
2116 /** This is confusing :) Guess for now */
2117 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2118 (IS_ITEMP (IC_RIGHT (uic)) ||
2119 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2124 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2125 (IS_ITEMP (IC_LEFT (uic)) ||
2126 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2131 D (D_ACCUSE2, (" + Dropping as hit default case\n"));
2137 opIgnoresA (iCode * ic, iCode * uic)
2139 /* A increment of an iTemp by a constant is OK. */
2140 if ( uic->op == '+' &&
2141 IS_ITEMP (IC_LEFT (uic)) &&
2142 IS_ITEMP (IC_RESULT (uic)) &&
2143 IS_OP_LITERAL (IC_RIGHT (uic)))
2145 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2147 /* Being an ITEMP means that we're already a symbol. */
2149 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2160 /* Some optimisation cases:
2172 ; genAssign (pointer)
2176 want to optimise down to:
2182 So genPointer get is OK
2183 genPlus where the right is constant, left is iTemp, and result is same as left
2184 genAssign (pointer) is OK
2195 ; genAssign (pointer)
2196 ; AOP_STK for _strcpy_to_1_1
2201 want to optimise down to:
2207 So genIfx where IC_COND has size of 1 and is a constant.
2210 /** Pack registers for acc use.
2211 When the result of this operation is small and short lived it may
2212 be able to be stored in the accumulator.
2214 Note that the 'A preserving' list is currently emperical :)
2217 packRegsForAccUse2 (iCode * ic)
2221 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2223 /* Filter out all but those 'good' commands */
2225 !POINTER_GET (ic) &&
2227 !IS_BITWISE_OP (ic) &&
2233 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2237 /* if + or - then it has to be one byte result.
2240 if ((ic->op == '+' || ic->op == '-')
2241 && getSize (operandType (IC_RESULT (ic))) > 1)
2243 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2247 /* if shift operation make sure right side is not a literal.
2251 if (ic->op == RIGHT_OP &&
2252 (isOperandLiteral (IC_RIGHT (ic)) ||
2253 getSize (operandType (IC_RESULT (ic))) > 1))
2256 if (ic->op == LEFT_OP &&
2257 (isOperandLiteral (IC_RIGHT (ic)) ||
2258 getSize (operandType (IC_RESULT (ic))) > 1))
2262 /* has only one definition */
2263 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2265 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2269 /* Right. We may be able to propagate it through if:
2270 For each in the chain of uses the intermediate is OK.
2272 /* Get next with 'uses result' bit on
2273 If this->next == next
2274 Validate use of next
2275 If OK, increase count
2277 /* and the usage immediately follows this iCode */
2278 if (!(uic = hTabItemWithKey (iCodehTab,
2279 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2281 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2286 /* Create a copy of the OP_USES bit vect */
2287 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2289 iCode *scan = ic, *next;
2293 setBit = bitVectFirstBit (uses);
2294 next = hTabItemWithKey (iCodehTab, setBit);
2295 if (scan->next == next)
2297 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2299 bitVectUnSetBit (uses, setBit);
2300 /* Still contigous. */
2301 if (!opPreservesA (ic, next))
2303 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2306 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2309 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2311 if (next->prev == NULL)
2313 if (!opPreservesA (ic, next))
2315 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2318 bitVectUnSetBit (uses, setBit);
2323 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2327 else if (scan->next == NULL)
2329 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2330 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2335 if (opIgnoresA (ic, scan->next))
2339 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2343 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2348 while (!bitVectIsZero (uses));
2350 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2354 /* OLD CODE FOLLOWS */
2355 /* if it is a conditional branch then we definitely can
2363 if (uic->op == JUMPTABLE)
2367 /* if the usage is not is an assignment or an
2368 arithmetic / bitwise / shift operation then not.
2369 MLH: Pending: Invalid. Our pointer sets are always peechy.
2372 if (POINTER_SET (uic) &&
2373 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2375 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2381 if (uic->op != '=' &&
2382 !IS_ARITHMETIC_OP (uic) &&
2383 !IS_BITWISE_OP (uic) &&
2384 uic->op != LEFT_OP &&
2385 uic->op != RIGHT_OP)
2391 /* if used in ^ operation then make sure right is not a
2393 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2396 /* if shift operation make sure right side is not a literal */
2397 if (uic->op == RIGHT_OP &&
2398 (isOperandLiteral (IC_RIGHT (uic)) ||
2399 getSize (operandType (IC_RESULT (uic))) > 1))
2402 if (uic->op == LEFT_OP &&
2403 (isOperandLiteral (IC_RIGHT (uic)) ||
2404 getSize (operandType (IC_RESULT (uic))) > 1))
2408 /* make sure that the result of this icode is not on the
2409 stack, since acc is used to compute stack offset */
2410 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2411 OP_SYMBOL (IC_RESULT (uic))->onStack)
2416 /* if either one of them in far space then we cannot */
2417 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2418 isOperandInFarSpace (IC_LEFT (uic))) ||
2419 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2420 isOperandInFarSpace (IC_RIGHT (uic))))
2424 /* if the usage has only one operand then we can */
2425 if (IC_LEFT (uic) == NULL ||
2426 IC_RIGHT (uic) == NULL)
2429 /* make sure this is on the left side if not
2430 a '+' since '+' is commutative */
2431 if (ic->op != '+' &&
2432 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2435 /* if one of them is a literal then we can */
2436 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2437 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2443 /** This is confusing :) Guess for now */
2444 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2445 (IS_ITEMP (IC_RIGHT (uic)) ||
2446 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2449 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2450 (IS_ITEMP (IC_LEFT (uic)) ||
2451 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2455 printf ("acc ok!\n");
2456 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2459 /** Does some transformations to reduce register pressure.
2462 packRegisters (eBBlock * ebp)
2467 D (D_ALLOC, ("packRegisters: entered.\n"));
2469 while (1 && !DISABLE_PACK_ASSIGN)
2472 /* look for assignments of the form */
2473 /* iTempNN = TRueSym (someoperation) SomeOperand */
2475 /* TrueSym := iTempNN:1 */
2476 for (ic = ebp->sch; ic; ic = ic->next)
2478 /* find assignment of the form TrueSym := iTempNN:1 */
2479 if (ic->op == '=' && !POINTER_SET (ic))
2480 change += packRegsForAssign (ic, ebp);
2486 for (ic = ebp->sch; ic; ic = ic->next)
2488 /* Safe: address of a true sym is always constant. */
2489 /* if this is an itemp & result of a address of a true sym
2490 then mark this as rematerialisable */
2491 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2493 if (ic->op == ADDRESS_OF &&
2494 IS_ITEMP (IC_RESULT (ic)) &&
2495 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2496 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2497 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2500 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2501 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2502 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2505 /* Safe: just propagates the remat flag */
2506 /* if straight assignment then carry remat flag if this is the
2508 if (ic->op == '=' &&
2509 !POINTER_SET (ic) &&
2510 IS_SYMOP (IC_RIGHT (ic)) &&
2511 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2512 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2515 OP_SYMBOL (IC_RESULT (ic))->remat =
2516 OP_SYMBOL (IC_RIGHT (ic))->remat;
2517 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2518 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2521 /* if the condition of an if instruction is defined in the
2522 previous instruction then mark the itemp as a conditional */
2523 if ((IS_CONDITIONAL (ic) ||
2524 ((ic->op == BITWISEAND ||
2527 isBitwiseOptimizable (ic))) &&
2528 ic->next && ic->next->op == IFX &&
2529 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2530 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2531 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2534 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2539 /* reduce for support function calls */
2540 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2541 packRegsForSupport (ic, ebp);
2545 /* some cases the redundant moves can
2546 can be eliminated for return statements */
2547 if ((ic->op == RETURN || ic->op == SEND) &&
2548 !isOperandInFarSpace (IC_LEFT (ic)) &&
2550 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2552 /* if pointer set & left has a size more than
2553 one and right is not in far space */
2554 if (!DISABLE_PACK_ONE_USE &&
2556 /* MLH: no such thing.
2557 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2558 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2559 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2560 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2563 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2566 /* if pointer get */
2567 if (!DISABLE_PACK_ONE_USE &&
2569 /* MLH: dont have far space
2570 !isOperandInFarSpace(IC_RESULT(ic))&& */
2571 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2572 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2573 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2576 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2578 /* pack registers for accumulator use, when the result of an
2579 arithmetic or bit wise operation has only one use, that use is
2580 immediately following the defintion and the using iCode has
2581 only one operand or has two operands but one is literal & the
2582 result of that operation is not on stack then we can leave the
2583 result of this operation in acc:b combination */
2585 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2587 packRegsForHLUse (ic);
2590 if ((IS_ARITHMETIC_OP (ic)
2591 || IS_BITWISE_OP (ic)
2592 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2594 IS_ITEMP (IC_RESULT (ic)) &&
2595 getSize (operandType (IC_RESULT (ic))) <= 2)
2596 packRegsForAccUse (ic);
2598 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2599 getSize (operandType (IC_RESULT (ic))) == 1)
2601 packRegsForAccUse2 (ic);
2607 /*-----------------------------------------------------------------*/
2608 /* assignRegisters - assigns registers to each live range as need */
2609 /*-----------------------------------------------------------------*/
2611 z80_assignRegisters (eBBlock ** ebbs, int count)
2616 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2618 setToNull ((void *) &_G.funcrUsed);
2619 _G.stackExtend = _G.dataExtend = 0;
2623 /* DE is required for the code gen. */
2624 _G.nRegs = GBZ80_MAX_REGS;
2625 regsZ80 = _gbz80_regs;
2629 _G.nRegs = Z80_MAX_REGS;
2630 regsZ80 = _z80_regs;
2633 /* change assignments this will remove some
2634 live ranges reducing some register pressure */
2635 for (i = 0; i < count; i++)
2636 packRegisters (ebbs[i]);
2638 if (options.dump_pack)
2639 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2641 /* first determine for each live range the number of
2642 registers & the type of registers required for each */
2645 /* and serially allocate registers */
2646 serialRegAssign (ebbs, count);
2648 /* if stack was extended then tell the user */
2651 /* werror(W_TOOMANY_SPILS,"stack", */
2652 /* _G.stackExtend,currFunc->name,""); */
2658 /* werror(W_TOOMANY_SPILS,"data space", */
2659 /* _G.dataExtend,currFunc->name,""); */
2663 if (options.dump_rassgn)
2664 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2666 /* after that create the register mask
2667 for each of the instruction */
2668 createRegMask (ebbs, count);
2670 /* now get back the chain */
2671 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2673 /* redo that offsets for stacked automatic variables */
2674 redoStackOffsets ();
2678 /* free up any stackSpil locations allocated */
2679 applyToSet (_G.stackSpil, deallocStackSpil);
2681 setToNull ((void **) &_G.stackSpil);
2682 setToNull ((void **) &_G.spiltSet);
2683 /* mark all registers as free */