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 == ADDRESS_OF && uic->op == IPUSH)
2040 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2044 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2048 opPreservesA (iCode * ic, iCode * uic)
2052 /* If we've gotten this far then the thing to compare must be
2053 small enough and must be in A.
2058 if (uic->op == JUMPTABLE)
2060 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2064 /* A pointer assign preserves A if A is the left value. */
2065 if (uic->op == '=' && POINTER_SET (uic))
2070 /* if the usage has only one operand then we can */
2071 /* PENDING: check */
2072 if (IC_LEFT (uic) == NULL ||
2073 IC_RIGHT (uic) == NULL)
2075 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2079 /* PENDING: check this rule */
2080 if (getSize (operandType (IC_RESULT (uic))) > 1)
2082 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2087 /* Disabled all of the old rules as they weren't verified and have
2088 caused at least one problem.
2094 opIgnoresA (iCode * ic, iCode * uic)
2096 /* A increment of an iTemp by a constant is OK. */
2097 if ( uic->op == '+' &&
2098 IS_ITEMP (IC_LEFT (uic)) &&
2099 IS_ITEMP (IC_RESULT (uic)) &&
2100 IS_OP_LITERAL (IC_RIGHT (uic)))
2102 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2104 /* Being an ITEMP means that we're already a symbol. */
2106 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2117 /* Some optimisation cases:
2129 ; genAssign (pointer)
2133 want to optimise down to:
2139 So genPointer get is OK
2140 genPlus where the right is constant, left is iTemp, and result is same as left
2141 genAssign (pointer) is OK
2152 ; genAssign (pointer)
2153 ; AOP_STK for _strcpy_to_1_1
2158 want to optimise down to:
2164 So genIfx where IC_COND has size of 1 and is a constant.
2167 /** Pack registers for acc use.
2168 When the result of this operation is small and short lived it may
2169 be able to be stored in the accumulator.
2171 Note that the 'A preserving' list is currently emperical :)
2174 packRegsForAccUse2 (iCode * ic)
2178 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2180 /* Filter out all but those 'good' commands */
2182 !POINTER_GET (ic) &&
2184 !IS_BITWISE_OP (ic) &&
2190 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2194 /* if + or - then it has to be one byte result.
2197 if ((ic->op == '+' || ic->op == '-')
2198 && getSize (operandType (IC_RESULT (ic))) > 1)
2200 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2204 /* has only one definition */
2205 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2207 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2211 /* Right. We may be able to propagate it through if:
2212 For each in the chain of uses the intermediate is OK.
2214 /* Get next with 'uses result' bit on
2215 If this->next == next
2216 Validate use of next
2217 If OK, increase count
2219 /* and the usage immediately follows this iCode */
2220 if (!(uic = hTabItemWithKey (iCodehTab,
2221 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2223 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2228 /* Create a copy of the OP_USES bit vect */
2229 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2231 iCode *scan = ic, *next;
2235 setBit = bitVectFirstBit (uses);
2236 next = hTabItemWithKey (iCodehTab, setBit);
2237 if (scan->next == next)
2239 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2241 bitVectUnSetBit (uses, setBit);
2242 /* Still contigous. */
2243 if (!opPreservesA (ic, next))
2245 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2248 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2251 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2253 if (next->prev == NULL)
2255 if (!opPreservesA (ic, next))
2257 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2260 bitVectUnSetBit (uses, setBit);
2265 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2269 else if (scan->next == NULL)
2271 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2272 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2277 if (opIgnoresA (ic, scan->next))
2281 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2285 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2290 while (!bitVectIsZero (uses));
2292 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2296 /* OLD CODE FOLLOWS */
2297 /* if it is a conditional branch then we definitely can
2305 if (uic->op == JUMPTABLE)
2309 /* if the usage is not is an assignment or an
2310 arithmetic / bitwise / shift operation then not.
2311 MLH: Pending: Invalid. Our pointer sets are always peechy.
2314 if (POINTER_SET (uic) &&
2315 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2317 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2323 if (uic->op != '=' &&
2324 !IS_ARITHMETIC_OP (uic) &&
2325 !IS_BITWISE_OP (uic) &&
2326 uic->op != LEFT_OP &&
2327 uic->op != RIGHT_OP)
2333 /* if used in ^ operation then make sure right is not a
2335 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2338 /* if shift operation make sure right side is not a literal */
2339 if (uic->op == RIGHT_OP &&
2340 (isOperandLiteral (IC_RIGHT (uic)) ||
2341 getSize (operandType (IC_RESULT (uic))) > 1))
2344 if (uic->op == LEFT_OP &&
2345 (isOperandLiteral (IC_RIGHT (uic)) ||
2346 getSize (operandType (IC_RESULT (uic))) > 1))
2350 /* make sure that the result of this icode is not on the
2351 stack, since acc is used to compute stack offset */
2352 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2353 OP_SYMBOL (IC_RESULT (uic))->onStack)
2358 /* if either one of them in far space then we cannot */
2359 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2360 isOperandInFarSpace (IC_LEFT (uic))) ||
2361 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2362 isOperandInFarSpace (IC_RIGHT (uic))))
2366 /* if the usage has only one operand then we can */
2367 if (IC_LEFT (uic) == NULL ||
2368 IC_RIGHT (uic) == NULL)
2371 /* make sure this is on the left side if not
2372 a '+' since '+' is commutative */
2373 if (ic->op != '+' &&
2374 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2377 /* if one of them is a literal then we can */
2378 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2379 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2385 /** This is confusing :) Guess for now */
2386 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2387 (IS_ITEMP (IC_RIGHT (uic)) ||
2388 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2391 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2392 (IS_ITEMP (IC_LEFT (uic)) ||
2393 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2397 printf ("acc ok!\n");
2398 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2401 /** Does some transformations to reduce register pressure.
2404 packRegisters (eBBlock * ebp)
2409 D (D_ALLOC, ("packRegisters: entered.\n"));
2411 while (1 && !DISABLE_PACK_ASSIGN)
2414 /* look for assignments of the form */
2415 /* iTempNN = TRueSym (someoperation) SomeOperand */
2417 /* TrueSym := iTempNN:1 */
2418 for (ic = ebp->sch; ic; ic = ic->next)
2420 /* find assignment of the form TrueSym := iTempNN:1 */
2421 if (ic->op == '=' && !POINTER_SET (ic))
2422 change += packRegsForAssign (ic, ebp);
2428 for (ic = ebp->sch; ic; ic = ic->next)
2430 /* Safe: address of a true sym is always constant. */
2431 /* if this is an itemp & result of a address of a true sym
2432 then mark this as rematerialisable */
2433 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2435 if (ic->op == ADDRESS_OF &&
2436 IS_ITEMP (IC_RESULT (ic)) &&
2437 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2438 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2439 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2442 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2443 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2444 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2447 /* Safe: just propagates the remat flag */
2448 /* if straight assignment then carry remat flag if this is the
2450 if (ic->op == '=' &&
2451 !POINTER_SET (ic) &&
2452 IS_SYMOP (IC_RIGHT (ic)) &&
2453 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2454 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2457 OP_SYMBOL (IC_RESULT (ic))->remat =
2458 OP_SYMBOL (IC_RIGHT (ic))->remat;
2459 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2460 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2463 /* if the condition of an if instruction is defined in the
2464 previous instruction then mark the itemp as a conditional */
2465 if ((IS_CONDITIONAL (ic) ||
2466 ((ic->op == BITWISEAND ||
2469 isBitwiseOptimizable (ic))) &&
2470 ic->next && ic->next->op == IFX &&
2471 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2472 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2473 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2476 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2481 /* reduce for support function calls */
2482 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2483 packRegsForSupport (ic, ebp);
2487 /* some cases the redundant moves can
2488 can be eliminated for return statements */
2489 if ((ic->op == RETURN || ic->op == SEND) &&
2490 !isOperandInFarSpace (IC_LEFT (ic)) &&
2492 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2494 /* if pointer set & left has a size more than
2495 one and right is not in far space */
2496 if (!DISABLE_PACK_ONE_USE &&
2498 /* MLH: no such thing.
2499 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2500 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2501 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2502 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2505 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2508 /* if pointer get */
2509 if (!DISABLE_PACK_ONE_USE &&
2511 /* MLH: dont have far space
2512 !isOperandInFarSpace(IC_RESULT(ic))&& */
2513 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2514 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2515 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2518 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2520 /* pack registers for accumulator use, when the result of an
2521 arithmetic or bit wise operation has only one use, that use is
2522 immediately following the defintion and the using iCode has
2523 only one operand or has two operands but one is literal & the
2524 result of that operation is not on stack then we can leave the
2525 result of this operation in acc:b combination */
2527 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2529 packRegsForHLUse (ic);
2532 if ((IS_ARITHMETIC_OP (ic)
2533 || IS_BITWISE_OP (ic)
2534 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2536 IS_ITEMP (IC_RESULT (ic)) &&
2537 getSize (operandType (IC_RESULT (ic))) <= 2)
2538 packRegsForAccUse (ic);
2540 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2541 getSize (operandType (IC_RESULT (ic))) == 1)
2543 packRegsForAccUse2 (ic);
2549 /*-----------------------------------------------------------------*/
2550 /* assignRegisters - assigns registers to each live range as need */
2551 /*-----------------------------------------------------------------*/
2553 z80_assignRegisters (eBBlock ** ebbs, int count)
2558 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2560 setToNull ((void *) &_G.funcrUsed);
2561 _G.stackExtend = _G.dataExtend = 0;
2565 /* DE is required for the code gen. */
2566 _G.nRegs = GBZ80_MAX_REGS;
2567 regsZ80 = _gbz80_regs;
2571 _G.nRegs = Z80_MAX_REGS;
2572 regsZ80 = _z80_regs;
2575 /* change assignments this will remove some
2576 live ranges reducing some register pressure */
2577 for (i = 0; i < count; i++)
2578 packRegisters (ebbs[i]);
2580 if (options.dump_pack)
2581 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2583 /* first determine for each live range the number of
2584 registers & the type of registers required for each */
2587 /* and serially allocate registers */
2588 serialRegAssign (ebbs, count);
2590 /* if stack was extended then tell the user */
2593 /* werror(W_TOOMANY_SPILS,"stack", */
2594 /* _G.stackExtend,currFunc->name,""); */
2600 /* werror(W_TOOMANY_SPILS,"data space", */
2601 /* _G.dataExtend,currFunc->name,""); */
2605 if (options.dump_rassgn) {
2606 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2607 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2610 /* after that create the register mask
2611 for each of the instruction */
2612 createRegMask (ebbs, count);
2614 /* now get back the chain */
2615 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2617 /* redo that offsets for stacked automatic variables */
2618 redoStackOffsets ();
2622 /* free up any stackSpil locations allocated */
2623 applyToSet (_G.stackSpil, deallocStackSpil);
2625 setToNull ((void **) &_G.stackSpil);
2626 setToNull ((void **) &_G.spiltSet);
2627 /* mark all registers as free */