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,
64 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
69 /*-----------------------------------------------------------------*/
70 /* At this point we start getting processor specific although */
71 /* some routines are non-processor specific & can be reused when */
72 /* targetting other processors. The decision for this will have */
73 /* to be made on a routine by routine basis */
74 /* routines used to pack registers are most definitely not reusable */
75 /* since the pack the registers depending strictly on the MCU */
76 /*-----------------------------------------------------------------*/
78 bitVect *spiltSet = NULL;
79 set *stackSpil = NULL;
80 bitVect *regAssigned = NULL;
83 extern void genZ80Code (iCode *);
84 bitVect *funcrUsed = NULL; /* registers used in a function */
89 /** Set to help debug register pressure related problems */
90 #define DEBUG_FAKE_EXTRA_REGS 0
92 static regs _gbz80_regs[] =
94 {REG_GPR, C_IDX, "c", 1},
95 {REG_GPR, B_IDX, "b", 1},
96 {REG_CND, CND_IDX, "c", 1}
99 static regs _z80_regs[] =
101 {REG_GPR, C_IDX, "c", 1},
102 {REG_GPR, B_IDX, "b", 1},
103 {REG_GPR, E_IDX, "e", 1},
104 {REG_GPR, D_IDX, "d", 1},
105 /* { REG_GPR, L_IDX , "l", 1 },
106 { REG_GPR, H_IDX , "h", 1 }, */
107 #if DEBUG_FAKE_EXTRA_REGS
108 {REG_GPR, M_IDX, "m", 1},
109 {REG_GPR, N_IDX, "n", 1},
110 {REG_GPR, O_IDX, "o", 1},
111 {REG_GPR, P_IDX, "p", 1},
112 {REG_GPR, Q_IDX, "q", 1},
113 {REG_GPR, R_IDX, "r", 1},
114 {REG_GPR, S_IDX, "s", 1},
115 {REG_GPR, T_IDX, "t", 1},
117 {REG_CND, CND_IDX, "c", 1}
122 /** Number of usable registers (all but C) */
123 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
124 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
126 static void spillThis (symbol *);
128 /** Allocates register of given type.
129 'type' is not used on the z80 version. It was used to select
130 between pointer and general purpose registers on the mcs51 version.
132 @return Pointer to the newly allocated register.
135 allocReg (short type)
139 for (i = 0; i < _nRegs; i++)
141 /* For now we allocate from any free */
142 if (regsZ80[i].isFree)
144 regsZ80[i].isFree = 0;
147 bitVectSetBit (currFunc->regsUsed, i);
148 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
152 D (D_ALLOC, ("allocReg: No free.\n"));
156 /** Returns pointer to register wit index number
163 for (i = 0; i < _nRegs; i++)
164 if (regsZ80[i].rIdx == idx)
167 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
168 "regWithIdx not found");
172 /** Frees a register.
177 wassert (!reg->isFree);
179 D (D_ALLOC, ("freeReg: freed %p\n", reg));
183 /** Returns number of free registers.
191 for (i = 0; i < _nRegs; i++)
193 /* For now only one reg type */
194 if (regsZ80[i].isFree)
200 /** Free registers with type.
203 nfreeRegsType (int type)
208 if ((nfr = nFreeRegs (type)) == 0)
209 return nFreeRegs (REG_GPR);
212 return nFreeRegs (type);
217 /*-----------------------------------------------------------------*/
218 /* allDefsOutOfRange - all definitions are out of a range */
219 /*-----------------------------------------------------------------*/
221 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
228 for (i = 0; i < defs->size; i++)
232 if (bitVectBitValue (defs, i) &&
233 (ic = hTabItemWithKey (iCodehTab, i)) &&
234 (ic->seq >= fseq && ic->seq <= toseq))
244 /*-----------------------------------------------------------------*/
245 /* computeSpillable - given a point find the spillable live ranges */
246 /*-----------------------------------------------------------------*/
248 computeSpillable (iCode * ic)
252 /* spillable live ranges are those that are live at this
253 point . the following categories need to be subtracted
255 a) - those that are already spilt
256 b) - if being used by this one
257 c) - defined by this one */
259 spillable = bitVectCopy (ic->rlive);
261 bitVectCplAnd (spillable, spiltSet); /* those already spilt */
263 bitVectCplAnd (spillable, ic->uses); /* used in this one */
264 bitVectUnSetBit (spillable, ic->defKey);
265 spillable = bitVectIntersect (spillable, regAssigned);
270 /*-----------------------------------------------------------------*/
271 /* noSpilLoc - return true if a variable has no spil location */
272 /*-----------------------------------------------------------------*/
274 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
276 return (sym->usl.spillLoc ? 0 : 1);
279 /*-----------------------------------------------------------------*/
280 /* hasSpilLoc - will return 1 if the symbol has spil location */
281 /*-----------------------------------------------------------------*/
283 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
285 return (sym->usl.spillLoc ? 1 : 0);
288 /** Will return 1 if the remat flag is set.
289 A symbol is rematerialisable if it doesnt need to be allocated
290 into registers at creation as it can be re-created at any time -
291 i.e. it's constant in some way.
294 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
299 /*-----------------------------------------------------------------*/
300 /* allLRs - return true for all */
301 /*-----------------------------------------------------------------*/
303 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
308 /*-----------------------------------------------------------------*/
309 /* liveRangesWith - applies function to a given set of live range */
310 /*-----------------------------------------------------------------*/
312 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
313 eBBlock * ebp, iCode * ic)
318 if (!lrs || !lrs->size)
321 for (i = 1; i < lrs->size; i++)
324 if (!bitVectBitValue (lrs, i))
327 /* if we don't find it in the live range
328 hash table we are in serious trouble */
329 if (!(sym = hTabItemWithKey (liveRanges, i)))
331 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
332 "liveRangesWith could not find liveRange");
336 if (func (sym, ebp, ic) && bitVectBitValue (regAssigned, sym->key))
337 addSetHead (&rset, sym);
344 /*-----------------------------------------------------------------*/
345 /* leastUsedLR - given a set determines which is the least used */
346 /*-----------------------------------------------------------------*/
348 leastUsedLR (set * sset)
350 symbol *sym = NULL, *lsym = NULL;
352 sym = lsym = setFirstItem (sset);
357 for (; lsym; lsym = setNextItem (sset))
360 /* if usage is the same then prefer
361 the spill the smaller of the two */
362 if (lsym->used == sym->used)
363 if (getSize (lsym->type) < getSize (sym->type))
367 if (lsym->used < sym->used)
372 setToNull ((void **) &sset);
377 /*-----------------------------------------------------------------*/
378 /* noOverLap - will iterate through the list looking for over lap */
379 /*-----------------------------------------------------------------*/
381 noOverLap (set * itmpStack, symbol * fsym)
385 for (sym = setFirstItem (itmpStack); sym;
386 sym = setNextItem (itmpStack))
388 if (sym->liveTo > fsym->liveFrom)
395 /*-----------------------------------------------------------------*/
396 /* isFree - will return 1 if the a free spil location is found */
397 /*-----------------------------------------------------------------*/
401 V_ARG (symbol **, sloc);
402 V_ARG (symbol *, fsym);
404 /* if already found */
408 /* if it is free && and the itmp assigned to
409 this does not have any overlapping live ranges
410 with the one currently being assigned and
411 the size can be accomodated */
413 noOverLap (sym->usl.itmpStack, fsym) &&
414 getSize (sym->type) >= getSize (fsym->type))
423 /*-----------------------------------------------------------------*/
424 /* createStackSpil - create a location on the stack to spil */
425 /*-----------------------------------------------------------------*/
427 createStackSpil (symbol * sym)
431 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
433 /* first go try and find a free one that is already
434 existing on the stack */
435 if (applyToSet (stackSpil, isFree, &sloc, sym))
437 /* found a free one : just update & return */
438 sym->usl.spillLoc = sloc;
441 addSetHead (&sloc->usl.itmpStack, sym);
442 D (D_ALLOC, ("createStackSpil: found existing\n"));
446 /* could not then have to create one , this is the hard part
447 we need to allocate this on the stack : this is really a
448 hack!! but cannot think of anything better at this time */
450 sprintf (buffer, "sloc%d", slocNum++);
451 sloc = newiTemp (buffer);
453 /* set the type to the spilling symbol */
454 sloc->type = copyLinkChain (sym->type);
455 sloc->etype = getSpec (sloc->type);
456 SPEC_SCLS (sloc->etype) = S_AUTO;
458 /* we don't allow it to be allocated`
459 onto the external stack since : so we
460 temporarily turn it off ; we also
461 turn off memory model to prevent
462 the spil from going to the external storage
463 and turn off overlaying
467 sloc->isref = 1; /* to prevent compiler warning */
469 /* if it is on the stack then update the stack */
470 if (IN_STACK (sloc->etype))
472 currFunc->stack += getSize (sloc->type);
473 stackExtend += getSize (sloc->type);
476 dataExtend += getSize (sloc->type);
478 /* add it to the stackSpil set */
479 addSetHead (&stackSpil, sloc);
480 sym->usl.spillLoc = sloc;
483 /* add it to the set of itempStack set
484 of the spill location */
485 addSetHead (&sloc->usl.itmpStack, sym);
487 D (D_ALLOC, ("createStackSpil: created new\n"));
491 /*-----------------------------------------------------------------*/
492 /* isSpiltOnStack - returns true if the spil location is on stack */
493 /*-----------------------------------------------------------------*/
495 isSpiltOnStack (symbol * sym)
505 /* if (sym->stackSpil) */
508 if (!sym->usl.spillLoc)
511 etype = getSpec (sym->usl.spillLoc->type);
512 if (IN_STACK (etype))
518 /*-----------------------------------------------------------------*/
519 /* spillThis - spils a specific operand */
520 /*-----------------------------------------------------------------*/
522 spillThis (symbol * sym)
526 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
528 /* if this is rematerializable or has a spillLocation
529 we are okay, else we need to create a spillLocation
531 if (!(sym->remat || sym->usl.spillLoc))
532 createStackSpil (sym);
534 /* mark it has spilt & put it in the spilt set */
536 spiltSet = bitVectSetBit (spiltSet, sym->key);
538 bitVectUnSetBit (regAssigned, sym->key);
540 for (i = 0; i < sym->nRegs; i++)
544 freeReg (sym->regs[i]);
549 /* if spilt on stack then free up r0 & r1
550 if they could have been assigned to some
552 if (sym->usl.spillLoc && !sym->remat)
553 sym->usl.spillLoc->allocreq = 1;
557 /** Select a iTemp to spil : rather a simple procedure.
560 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
562 bitVect *lrcs = NULL;
566 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
567 /* get the spillable live ranges */
568 lrcs = computeSpillable (ic);
570 /* get all live ranges that are rematerizable */
571 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
573 D (D_ALLOC, ("selectSpil: using remat.\n"));
574 /* return the least used of these */
575 return leastUsedLR (selectS);
579 /* get live ranges with spillLocations in direct space */
580 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
582 sym = leastUsedLR (selectS);
583 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
584 sym->usl.spillLoc->rname :
585 sym->usl.spillLoc->name));
587 /* mark it as allocation required */
588 sym->usl.spillLoc->allocreq = 1;
592 /* if the symbol is local to the block then */
593 if (forSym->liveTo < ebp->lSeq)
596 /* check if there are any live ranges allocated
597 to registers that are not used in this block */
598 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
600 sym = leastUsedLR (selectS);
601 /* if this is not rematerializable */
610 /* check if there are any live ranges that not
611 used in the remainder of the block */
612 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
614 sym = leastUsedLR (selectS);
626 /* find live ranges with spillocation && not used as pointers */
627 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
630 sym = leastUsedLR (selectS);
631 /* mark this as allocation required */
632 sym->usl.spillLoc->allocreq = 1;
637 /* find live ranges with spillocation */
638 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
640 D (D_ALLOC, ("selectSpil: using with spill.\n"));
641 sym = leastUsedLR (selectS);
642 sym->usl.spillLoc->allocreq = 1;
646 /* couldn't find then we need to create a spil
647 location on the stack , for which one? the least
649 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
651 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
652 /* return a created spil location */
653 sym = createStackSpil (leastUsedLR (selectS));
654 sym->usl.spillLoc->allocreq = 1;
658 /* this is an extreme situation we will spill
659 this one : happens very rarely but it does happen */
660 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
666 /** Spil some variable & mark registers as free.
667 A spill occurs when an iTemp wont fit into the available registers.
670 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
675 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
677 /* get something we can spil */
678 ssym = selectSpil (ic, ebp, forSym);
680 /* mark it as spilt */
682 spiltSet = bitVectSetBit (spiltSet, ssym->key);
684 /* mark it as not register assigned &
685 take it away from the set */
686 bitVectUnSetBit (regAssigned, ssym->key);
688 /* mark the registers as free */
689 for (i = 0; i < ssym->nRegs; i++)
691 freeReg (ssym->regs[i]);
693 /* if spilt on stack then free up r0 & r1
694 if they could have been assigned to as gprs */
695 if (!ptrRegReq && isSpiltOnStack (ssym))
698 spillLRWithPtrReg (ssym);
701 /* if this was a block level spil then insert push & pop
702 at the start & end of block respectively */
705 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
706 /* add push to the start of the block */
707 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
708 ebp->sch->next : ebp->sch));
709 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
710 /* add pop to the end of the block */
711 addiCodeToeBBlock (ebp, nic, NULL);
714 /* if spilt because not used in the remainder of the
715 block then add a push before this instruction and
716 a pop at the end of the block */
717 if (ssym->remainSpil)
720 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
721 /* add push just before this instruction */
722 addiCodeToeBBlock (ebp, nic, ic);
724 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
725 /* add pop to the end of the block */
726 addiCodeToeBBlock (ebp, nic, NULL);
730 D (D_ALLOC, ("spilSomething: done.\n"));
738 /** Will try for GPR if not spil.
741 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
745 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
747 /* try for gpr type */
748 if ((reg = allocReg (REG_GPR)))
750 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
754 /* we have to spil */
755 if (!spilSomething (ic, ebp, sym))
757 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
761 /* this looks like an infinite loop but
762 in really selectSpil will abort */
766 /** Symbol has a given register.
769 symHasReg (symbol * sym, regs * reg)
773 for (i = 0; i < sym->nRegs; i++)
774 if (sym->regs[i] == reg)
780 /** Check the live to and if they have registers & are not spilt then
781 free up the registers
784 deassignLRs (iCode * ic, eBBlock * ebp)
790 for (sym = hTabFirstItem (liveRanges, &k); sym;
791 sym = hTabNextItem (liveRanges, &k))
795 /* if it does not end here */
796 if (sym->liveTo > ic->seq)
799 /* if it was spilt on stack then we can
800 mark the stack spil location as free */
805 sym->usl.spillLoc->isFree = 1;
811 if (!bitVectBitValue (regAssigned, sym->key))
814 /* special case check if this is an IFX &
815 the privious one was a pop and the
816 previous one was not spilt then keep track
818 if (ic->op == IFX && ic->prev &&
819 ic->prev->op == IPOP &&
820 !ic->prev->parmPush &&
821 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
822 psym = OP_SYMBOL (IC_LEFT (ic->prev));
824 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
830 bitVectUnSetBit (regAssigned, sym->key);
832 /* if the result of this one needs registers
833 and does not have it then assign it right
835 if (IC_RESULT (ic) &&
836 !(SKIP_IC2 (ic) || /* not a special icode */
837 ic->op == JUMPTABLE ||
842 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
843 result->liveTo > ic->seq && /* and will live beyond this */
844 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
845 result->regType == sym->regType && /* same register types */
846 result->nRegs && /* which needs registers */
847 !result->isspilt && /* and does not already have them */
849 !bitVectBitValue (regAssigned, result->key) &&
850 /* the number of free regs + number of regs in this LR
851 can accomodate the what result Needs */
852 ((nfreeRegsType (result->regType) +
853 sym->nRegs) >= result->nRegs)
856 for (i = 0; i < result->nRegs; i++)
859 result->regs[i] = sym->regs[i];
861 result->regs[i] = getRegGpr (ic, ebp, result);
863 /* if the allocation falied which means
864 this was spilt then break */
865 if (!result->regs[i])
873 regAssigned = bitVectSetBit (regAssigned, result->key);
876 /* free the remaining */
877 for (; i < sym->nRegs; i++)
881 if (!symHasReg (psym, sym->regs[i]))
882 freeReg (sym->regs[i]);
885 freeReg (sym->regs[i]);
886 // sym->regs[i] = NULL;
893 /** Reassign this to registers.
896 reassignLR (operand * op)
898 symbol *sym = OP_SYMBOL (op);
901 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
903 /* not spilt any more */
904 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
905 bitVectUnSetBit (spiltSet, sym->key);
907 regAssigned = bitVectSetBit (regAssigned, sym->key);
911 for (i = 0; i < sym->nRegs; i++)
912 sym->regs[i]->isFree = 0;
915 /** Determines if allocating will cause a spill.
918 willCauseSpill (int nr, int rt)
920 /* first check if there are any avlb registers
921 of te type required */
922 if (nFreeRegs (0) >= nr)
925 /* it will cause a spil */
929 /** The allocator can allocate same registers to result and operand,
930 if this happens make sure they are in the same position as the operand
931 otherwise chaos results.
934 positionRegs (symbol * result, symbol * opsym, int lineno)
936 int count = min (result->nRegs, opsym->nRegs);
937 int i, j = 0, shared = 0;
939 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
941 /* if the result has been spilt then cannot share */
946 /* first make sure that they actually share */
947 for (i = 0; i < count; i++)
949 for (j = 0; j < count; j++)
951 if (result->regs[i] == opsym->regs[j] && i != j)
961 regs *tmp = result->regs[i];
962 result->regs[i] = result->regs[j];
963 result->regs[j] = tmp;
968 /** Try to allocate a pair of registers to the symbol.
971 tryAllocatingRegPair (symbol * sym)
974 wassert (sym->nRegs == 2);
975 for (i = 0; i < _nRegs; i += 2)
977 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
979 regsZ80[i].isFree = 0;
980 sym->regs[0] = ®sZ80[i];
981 regsZ80[i + 1].isFree = 0;
982 sym->regs[1] = ®sZ80[i + 1];
986 bitVectSetBit (currFunc->regsUsed, i);
988 bitVectSetBit (currFunc->regsUsed, i + 1);
990 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
994 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
998 /** Serially allocate registers to the variables.
999 This is the main register allocation function. It is called after
1003 serialRegAssign (eBBlock ** ebbs, int count)
1007 /* for all blocks */
1008 for (i = 0; i < count; i++)
1013 if (ebbs[i]->noPath &&
1014 (ebbs[i]->entryLabel != entryLabel &&
1015 ebbs[i]->entryLabel != returnLabel))
1018 /* of all instructions do */
1019 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1022 /* if this is an ipop that means some live
1023 range will have to be assigned again */
1027 reassignLR (IC_LEFT (ic));
1030 /* if result is present && is a true symbol */
1031 if (IC_RESULT (ic) && ic->op != IFX &&
1032 IS_TRUE_SYMOP (IC_RESULT (ic)))
1033 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1035 /* take away registers from live
1036 ranges that end at this instruction */
1037 deassignLRs (ic, ebbs[i]);
1039 /* some don't need registers */
1040 /* MLH: removed RESULT and POINTER_SET condition */
1041 if (SKIP_IC2 (ic) ||
1042 ic->op == JUMPTABLE ||
1048 /* now we need to allocate registers only for the result */
1051 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1056 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1058 /* if it does not need or is spilt
1059 or is already assigned to registers
1060 or will not live beyond this instructions */
1063 bitVectBitValue (regAssigned, sym->key) ||
1064 sym->liveTo <= ic->seq)
1066 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1070 /* if some liverange has been spilt at the block level
1071 and this one live beyond this block then spil this
1073 if (blockSpil && sym->liveTo > ebbs[i]->lSeq)
1075 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1079 /* if trying to allocate this will cause
1080 a spill and there is nothing to spill
1081 or this one is rematerializable then
1083 willCS = willCauseSpill (sym->nRegs, sym->regType);
1084 spillable = computeSpillable (ic);
1086 (willCS && bitVectIsZero (spillable)))
1089 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1095 /* if it has a spillocation & is used less than
1096 all other live ranges then spill this */
1098 if (sym->usl.spillLoc) {
1099 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1100 allLRs, ebbs[i], ic));
1101 if (leastUsed && leastUsed->used > sym->used) {
1106 /* if none of the liveRanges have a spillLocation then better
1107 to spill this one than anything else already assigned to registers */
1108 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1115 /* else we assign registers to it */
1116 regAssigned = bitVectSetBit (regAssigned, sym->key);
1118 /* Special case: Try to fit into a reg pair if
1120 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1121 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1126 for (j = 0; j < sym->nRegs; j++)
1128 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1130 /* if the allocation falied which means
1131 this was spilt then break */
1134 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1139 /* if it shares registers with operands make sure
1140 that they are in the same position */
1141 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1142 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1143 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1144 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1145 /* do the same for the right operand */
1146 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1147 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1148 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1149 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1156 /*-----------------------------------------------------------------*/
1157 /* rUmaskForOp :- returns register mask for an operand */
1158 /*-----------------------------------------------------------------*/
1160 rUmaskForOp (operand * op)
1166 /* only temporaries are assigned registers */
1170 sym = OP_SYMBOL (op);
1172 /* if spilt or no registers assigned to it
1174 if (sym->isspilt || !sym->nRegs)
1177 rumask = newBitVect (_nRegs);
1179 for (j = 0; j < sym->nRegs; j++)
1181 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1187 /** Returns bit vector of registers used in iCode.
1190 regsUsedIniCode (iCode * ic)
1192 bitVect *rmask = newBitVect (_nRegs);
1194 /* do the special cases first */
1197 rmask = bitVectUnion (rmask,
1198 rUmaskForOp (IC_COND (ic)));
1202 /* for the jumptable */
1203 if (ic->op == JUMPTABLE)
1205 rmask = bitVectUnion (rmask,
1206 rUmaskForOp (IC_JTCOND (ic)));
1211 /* of all other cases */
1213 rmask = bitVectUnion (rmask,
1214 rUmaskForOp (IC_LEFT (ic)));
1218 rmask = bitVectUnion (rmask,
1219 rUmaskForOp (IC_RIGHT (ic)));
1222 rmask = bitVectUnion (rmask,
1223 rUmaskForOp (IC_RESULT (ic)));
1229 /** For each instruction will determine the regsUsed.
1232 createRegMask (eBBlock ** ebbs, int count)
1236 /* for all blocks */
1237 for (i = 0; i < count; i++)
1241 if (ebbs[i]->noPath &&
1242 (ebbs[i]->entryLabel != entryLabel &&
1243 ebbs[i]->entryLabel != returnLabel))
1246 /* for all instructions */
1247 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1252 if (SKIP_IC2 (ic) || !ic->rlive)
1255 /* first mark the registers used in this
1257 ic->rUsed = regsUsedIniCode (ic);
1258 funcrUsed = bitVectUnion (funcrUsed, ic->rUsed);
1260 /* now create the register mask for those
1261 registers that are in use : this is a
1262 super set of ic->rUsed */
1263 ic->rMask = newBitVect (_nRegs + 1);
1265 /* for all live Ranges alive at this point */
1266 for (j = 1; j < ic->rlive->size; j++)
1271 /* if not alive then continue */
1272 if (!bitVectBitValue (ic->rlive, j))
1275 /* find the live range we are interested in */
1276 if (!(sym = hTabItemWithKey (liveRanges, j)))
1278 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1279 "createRegMask cannot find live range");
1283 /* if no register assigned to it */
1284 if (!sym->nRegs || sym->isspilt)
1287 /* for all the registers allocated to it */
1288 for (k = 0; k < sym->nRegs; k++)
1291 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1297 /** Returns the rematerialized string for a remat var.
1300 rematStr (symbol * sym)
1303 iCode *ic = sym->rematiCode;
1308 /* if plus or minus print the right hand side */
1309 if (ic->op == '+' || ic->op == '-')
1311 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1314 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1317 /* we reached the end */
1318 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1325 /*-----------------------------------------------------------------*/
1326 /* regTypeNum - computes the type & number of registers required */
1327 /*-----------------------------------------------------------------*/
1334 /* for each live range do */
1335 for (sym = hTabFirstItem (liveRanges, &k); sym;
1336 sym = hTabNextItem (liveRanges, &k))
1339 /* if used zero times then no registers needed */
1340 if ((sym->liveTo - sym->liveFrom) == 0)
1343 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1345 /* if the live range is a temporary */
1349 /* if the type is marked as a conditional */
1350 if (sym->regType == REG_CND)
1353 /* if used in return only then we don't
1355 if (sym->ruonly || sym->accuse)
1357 if (IS_AGGREGATE (sym->type) || sym->isptr)
1358 sym->type = aggrToPtr (sym->type, FALSE);
1362 /* if not then we require registers */
1363 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1364 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1365 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1366 getSize (sym->type));
1367 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1369 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1373 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1374 printTypeChain (sym->type, stderr);
1375 fprintf (stderr, "\n");
1378 /* determine the type of register required */
1379 /* Always general purpose */
1380 sym->regType = REG_GPR;
1385 /* for the first run we don't provide */
1386 /* registers for true symbols we will */
1387 /* see how things go */
1388 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1395 /** Mark all registers as free.
1402 D (D_ALLOC, ("freeAllRegs: running.\n"));
1404 for (i = 0; i < _nRegs; i++)
1405 regsZ80[i].isFree = 1;
1408 /*-----------------------------------------------------------------*/
1409 /* deallocStackSpil - this will set the stack pointer back */
1410 /*-----------------------------------------------------------------*/
1411 DEFSETFUNC (deallocStackSpil)
1419 /** Register reduction for assignment.
1422 packRegsForAssign (iCode * ic, eBBlock * ebp)
1426 D (D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1429 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1430 !IS_ITEMP (IC_RIGHT (ic)) ||
1431 OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1432 OP_SYMBOL (IC_RIGHT (ic))->isind)
1436 /* if the true symbol is defined in far space or on stack
1437 then we should not since this will increase register pressure */
1438 if (isOperandInFarSpace (IC_RESULT (ic)))
1440 if ((dic = farSpacePackable (ic)))
1447 /* find the definition of iTempNN scanning backwards if we find a
1448 a use of the true symbol in before we find the definition then
1450 for (dic = ic->prev; dic; dic = dic->prev)
1452 /* if there is a function call and this is
1453 a parameter & not my parameter then don't pack it */
1454 if ((dic->op == CALL || dic->op == PCALL) &&
1455 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1456 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1465 if (IS_SYMOP (IC_RESULT (dic)) &&
1466 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1471 if (IS_SYMOP (IC_RIGHT (dic)) &&
1472 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1473 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1479 if (IS_SYMOP (IC_LEFT (dic)) &&
1480 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1481 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1487 if (POINTER_SET (dic) &&
1488 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1497 return 0; /* did not find */
1499 /* if the result is on stack or iaccess then it must be
1500 the same atleast one of the operands */
1501 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1502 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1505 /* the operation has only one symbol
1506 operator then we can pack */
1507 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1508 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1511 if (!((IC_LEFT (dic) &&
1512 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1514 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1518 /* found the definition */
1519 /* replace the result with the result of */
1520 /* this assignment and remove this assignment */
1521 IC_RESULT (dic) = IC_RESULT (ic);
1523 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1525 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1527 /* delete from liverange table also
1528 delete from all the points inbetween and the new
1530 for (sic = dic; sic != ic; sic = sic->next)
1532 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1533 if (IS_ITEMP (IC_RESULT (dic)))
1534 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1537 remiCodeFromeBBlock (ebp, ic);
1541 /** Scanning backwards looks for first assig found.
1544 findAssignToSym (operand * op, iCode * ic)
1548 for (dic = ic->prev; dic; dic = dic->prev)
1551 /* if definition by assignment */
1552 if (dic->op == '=' &&
1553 !POINTER_SET (dic) &&
1554 IC_RESULT (dic)->key == op->key)
1555 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1558 /* we are interested only if defined in far space */
1559 /* or in stack space in case of + & - */
1561 /* if assigned to a non-symbol then return
1563 if (!IS_SYMOP (IC_RIGHT (dic)))
1566 /* if the symbol is in far space then
1568 if (isOperandInFarSpace (IC_RIGHT (dic)))
1571 /* for + & - operations make sure that
1572 if it is on the stack it is the same
1573 as one of the three operands */
1574 if ((ic->op == '+' || ic->op == '-') &&
1575 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1578 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1579 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1580 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1588 /* if we find an usage then we cannot delete it */
1589 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1592 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1595 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1599 /* now make sure that the right side of dic
1600 is not defined between ic & dic */
1603 iCode *sic = dic->next;
1605 for (; sic != ic; sic = sic->next)
1606 if (IC_RESULT (sic) &&
1607 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1616 /*-----------------------------------------------------------------*/
1617 /* packRegsForSupport :- reduce some registers for support calls */
1618 /*-----------------------------------------------------------------*/
1620 packRegsForSupport (iCode * ic, eBBlock * ebp)
1623 /* for the left & right operand :- look to see if the
1624 left was assigned a true symbol in far space in that
1625 case replace them */
1626 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1628 if (IS_ITEMP (IC_LEFT (ic)) &&
1629 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1631 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1637 /* found it we need to remove it from the
1639 for (sic = dic; sic != ic; sic = sic->next)
1640 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1642 IC_LEFT (ic)->operand.symOperand =
1643 IC_RIGHT (dic)->operand.symOperand;
1644 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1645 remiCodeFromeBBlock (ebp, dic);
1649 /* do the same for the right operand */
1652 IS_ITEMP (IC_RIGHT (ic)) &&
1653 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1655 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1661 /* found it we need to remove it from the block */
1662 for (sic = dic; sic != ic; sic = sic->next)
1663 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1665 IC_RIGHT (ic)->operand.symOperand =
1666 IC_RIGHT (dic)->operand.symOperand;
1667 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1669 remiCodeFromeBBlock (ebp, dic);
1676 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1678 /** Will reduce some registers for single use.
1681 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1686 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1688 /* if returning a literal then do nothing */
1692 /* only upto 2 bytes since we cannot predict
1693 the usage of b, & acc */
1694 if (getSize (operandType (op)) > 2 &&
1699 /* this routine will mark the a symbol as used in one
1700 instruction use only && if the defintion is local
1701 (ie. within the basic block) && has only one definition &&
1702 that definiion is either a return value from a
1703 function or does not contain any variables in
1705 uses = bitVectCopy (OP_USES (op));
1706 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1707 if (!bitVectIsZero (uses)) /* has other uses */
1710 /* if it has only one defintion */
1711 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1712 return NULL; /* has more than one definition */
1714 /* get the that definition */
1716 hTabItemWithKey (iCodehTab,
1717 bitVectFirstBit (OP_DEFS (op)))))
1720 /* found the definition now check if it is local */
1721 if (dic->seq < ebp->fSeq ||
1722 dic->seq > ebp->lSeq)
1723 return NULL; /* non-local */
1725 /* now check if it is the return from a function call */
1726 if (dic->op == CALL || dic->op == PCALL)
1728 if (ic->op != SEND && ic->op != RETURN)
1730 OP_SYMBOL (op)->ruonly = 1;
1736 /* otherwise check that the definition does
1737 not contain any symbols in far space */
1738 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1739 isOperandInFarSpace (IC_RIGHT (dic)) ||
1740 IS_OP_RUONLY (IC_LEFT (ic)) ||
1741 IS_OP_RUONLY (IC_RIGHT (ic)))
1746 /* if pointer set then make sure the pointer is one byte */
1747 if (POINTER_SET (dic))
1750 if (POINTER_GET (dic))
1755 /* also make sure the intervenening instructions
1756 don't have any thing in far space */
1757 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1759 /* if there is an intervening function call then no */
1760 if (dic->op == CALL || dic->op == PCALL)
1762 /* if pointer set then make sure the pointer
1764 if (POINTER_SET (dic))
1767 if (POINTER_GET (dic))
1770 /* if address of & the result is remat the okay */
1771 if (dic->op == ADDRESS_OF &&
1772 OP_SYMBOL (IC_RESULT (dic))->remat)
1775 /* if left or right or result is in far space */
1776 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1777 isOperandInFarSpace (IC_RIGHT (dic)) ||
1778 isOperandInFarSpace (IC_RESULT (dic)) ||
1779 IS_OP_RUONLY (IC_LEFT (dic)) ||
1780 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1781 IS_OP_RUONLY (IC_RESULT (dic)))
1787 OP_SYMBOL (op)->ruonly = 1;
1791 /*-----------------------------------------------------------------*/
1792 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1793 /*-----------------------------------------------------------------*/
1795 isBitwiseOptimizable (iCode * ic)
1797 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1799 /* bitwise operations are considered optimizable
1800 under the following conditions (Jean-Louis VERN)
1812 if (IS_LITERAL (rtype))
1818 Certian assignments involving pointers can be temporarly stored
1829 /** Pack registers for acc use.
1830 When the result of this operation is small and short lived it may
1831 be able to be stored in the accumelator.
1834 packRegsForAccUse (iCode * ic)
1838 /* if + or - then it has to be one byte result */
1839 if ((ic->op == '+' || ic->op == '-')
1840 && getSize (operandType (IC_RESULT (ic))) > 1)
1843 /* if shift operation make sure right side is not a literal */
1844 if (ic->op == RIGHT_OP &&
1845 (isOperandLiteral (IC_RIGHT (ic)) ||
1846 getSize (operandType (IC_RESULT (ic))) > 1))
1849 if (ic->op == LEFT_OP &&
1850 (isOperandLiteral (IC_RIGHT (ic)) ||
1851 getSize (operandType (IC_RESULT (ic))) > 1))
1854 /* has only one definition */
1855 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1858 /* has only one use */
1859 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1862 /* and the usage immediately follows this iCode */
1863 if (!(uic = hTabItemWithKey (iCodehTab,
1864 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1867 if (ic->next != uic)
1870 /* if it is a conditional branch then we definitely can */
1874 if (uic->op == JUMPTABLE)
1878 /* if the usage is not is an assignment or an
1879 arithmetic / bitwise / shift operation then not */
1880 if (POINTER_SET (uic) &&
1881 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1885 if (uic->op != '=' &&
1886 !IS_ARITHMETIC_OP (uic) &&
1887 !IS_BITWISE_OP (uic) &&
1888 uic->op != LEFT_OP &&
1889 uic->op != RIGHT_OP)
1892 /* if used in ^ operation then make sure right is not a
1894 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1897 /* if shift operation make sure right side is not a literal */
1898 if (uic->op == RIGHT_OP &&
1899 (isOperandLiteral (IC_RIGHT (uic)) ||
1900 getSize (operandType (IC_RESULT (uic))) > 1))
1903 if (uic->op == LEFT_OP &&
1904 (isOperandLiteral (IC_RIGHT (uic)) ||
1905 getSize (operandType (IC_RESULT (uic))) > 1))
1909 /* make sure that the result of this icode is not on the
1910 stack, since acc is used to compute stack offset */
1911 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1912 OP_SYMBOL (IC_RESULT (uic))->onStack)
1917 /* if either one of them in far space then we cannot */
1918 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1919 isOperandInFarSpace (IC_LEFT (uic))) ||
1920 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1921 isOperandInFarSpace (IC_RIGHT (uic))))
1925 /* if the usage has only one operand then we can */
1926 if (IC_LEFT (uic) == NULL ||
1927 IC_RIGHT (uic) == NULL)
1930 /* make sure this is on the left side if not
1931 a '+' since '+' is commutative */
1932 if (ic->op != '+' &&
1933 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1936 /* if one of them is a literal then we can */
1937 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1938 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1944 /** This is confusing :) Guess for now */
1945 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1946 (IS_ITEMP (IC_RIGHT (uic)) ||
1947 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
1950 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
1951 (IS_ITEMP (IC_LEFT (uic)) ||
1952 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
1956 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
1960 packRegsForHLUse (iCode * ic)
1967 /* has only one definition */
1968 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1971 /* has only one use */
1972 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1975 /* and the usage immediately follows this iCode */
1976 if (!(uic = hTabItemWithKey (iCodehTab,
1977 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1980 if (ic->next != uic)
1983 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
1985 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
1989 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
1993 opPreservesA (iCode * ic, iCode * uic)
1995 /* if it is a conditional branch then we definitely can */
1999 if (uic->op == JUMPTABLE)
2002 /* if the usage has only one operand then we can */
2003 /* PENDING: check */
2004 if (IC_LEFT (uic) == NULL ||
2005 IC_RIGHT (uic) == NULL)
2008 /* PENDING: check this rule */
2009 if (getSize (operandType (IC_RESULT (uic))) > 1)
2016 !IS_ARITHMETIC_OP(uic) (sub requires A)
2020 !IS_BITWISE_OP (uic) &&
2023 !POINTER_GET (uic) &&
2025 uic->op != LEFT_OP &&
2026 uic->op != RIGHT_OP && */
2034 if (!IC_LEFT (uic) || !IC_RESULT (ic))
2037 /** This is confusing :) Guess for now */
2038 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2039 (IS_ITEMP (IC_RIGHT (uic)) ||
2040 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2043 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2044 (IS_ITEMP (IC_LEFT (uic)) ||
2045 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2052 joinPushes (iCode * ic)
2055 if (ic->op == IPUSH &&
2056 isOperandLiteral (IC_LEFT (ic)) &&
2057 getSize (operandType (IC_LEFT (ic))) == 1 &&
2058 ic->next->op == IPUSH &&
2059 isOperandLiteral (IC_LEFT (ic->next)) &&
2060 getSize (operandType (IC_LEFT (ic->next))) == 1)
2062 /* This is a bit tricky as michaelh doesnt know what he's doing.
2064 /* First upgrade the size of (first) to int */
2065 SPEC_NOUN (operandType (IC_LEFT (ic))) = V_INT;
2067 floatFromVal (AOP /* need some sleep ... */ );
2068 /* Now get and join the values */
2069 value *val = aop->aopu.aop_lit;
2070 /* if it is a float then it gets tricky */
2071 /* otherwise it is fairly simple */
2072 if (!IS_FLOAT (val->type))
2074 unsigned long v = floatFromVal (val);
2076 floatFrom ( /* need some sleep ... */ );
2077 printf ("Size %u\n", getSize (operandType (IC_LEFT (ic))));
2078 ic->next = ic->next->next;
2084 /** Pack registers for acc use.
2085 When the result of this operation is small and short lived it may
2086 be able to be stored in the accumulator.
2088 Note that the 'A preserving' list is currently emperical :)e
2091 packRegsForAccUse2 (iCode * ic)
2095 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2097 /* Filter out all but those 'good' commands */
2099 !POINTER_GET (ic) &&
2101 !IS_BITWISE_OP (ic) &&
2108 /* if + or - then it has to be one byte result.
2111 if ((ic->op == '+' || ic->op == '-')
2112 && getSize (operandType (IC_RESULT (ic))) > 1)
2115 /* if shift operation make sure right side is not a literal.
2119 if (ic->op == RIGHT_OP &&
2120 (isOperandLiteral (IC_RIGHT (ic)) ||
2121 getSize (operandType (IC_RESULT (ic))) > 1))
2124 if (ic->op == LEFT_OP &&
2125 (isOperandLiteral (IC_RIGHT (ic)) ||
2126 getSize (operandType (IC_RESULT (ic))) > 1))
2130 /* has only one definition */
2131 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2136 /* Right. We may be able to propagate it through if:
2137 For each in the chain of uses the intermediate is OK.
2139 /* Get next with 'uses result' bit on
2140 If this->next == next
2141 Validate use of next
2142 If OK, increase count
2144 /* and the usage immediately follows this iCode */
2145 if (!(uic = hTabItemWithKey (iCodehTab,
2146 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2152 /* Create a copy of the OP_USES bit vect */
2153 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2155 iCode *scan = ic, *next;
2159 setBit = bitVectFirstBit (uses);
2160 next = hTabItemWithKey (iCodehTab, setBit);
2161 if (scan->next == next)
2163 bitVectUnSetBit (uses, setBit);
2164 /* Still contigous. */
2165 if (!opPreservesA (ic, next))
2176 while (!bitVectIsZero (uses));
2177 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2181 /* OLD CODE FOLLOWS */
2182 /* if it is a conditional branch then we definitely can
2190 if (uic->op == JUMPTABLE)
2194 /* if the usage is not is an assignment or an
2195 arithmetic / bitwise / shift operation then not.
2196 MLH: Pending: Invalid. Our pointer sets are always peechy.
2199 if (POINTER_SET (uic) &&
2200 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2202 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2208 if (uic->op != '=' &&
2209 !IS_ARITHMETIC_OP (uic) &&
2210 !IS_BITWISE_OP (uic) &&
2211 uic->op != LEFT_OP &&
2212 uic->op != RIGHT_OP)
2218 /* if used in ^ operation then make sure right is not a
2220 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2223 /* if shift operation make sure right side is not a literal */
2224 if (uic->op == RIGHT_OP &&
2225 (isOperandLiteral (IC_RIGHT (uic)) ||
2226 getSize (operandType (IC_RESULT (uic))) > 1))
2229 if (uic->op == LEFT_OP &&
2230 (isOperandLiteral (IC_RIGHT (uic)) ||
2231 getSize (operandType (IC_RESULT (uic))) > 1))
2235 /* make sure that the result of this icode is not on the
2236 stack, since acc is used to compute stack offset */
2237 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2238 OP_SYMBOL (IC_RESULT (uic))->onStack)
2243 /* if either one of them in far space then we cannot */
2244 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2245 isOperandInFarSpace (IC_LEFT (uic))) ||
2246 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2247 isOperandInFarSpace (IC_RIGHT (uic))))
2251 /* if the usage has only one operand then we can */
2252 if (IC_LEFT (uic) == NULL ||
2253 IC_RIGHT (uic) == NULL)
2256 /* make sure this is on the left side if not
2257 a '+' since '+' is commutative */
2258 if (ic->op != '+' &&
2259 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2262 /* if one of them is a literal then we can */
2263 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2264 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2270 /** This is confusing :) Guess for now */
2271 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2272 (IS_ITEMP (IC_RIGHT (uic)) ||
2273 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2276 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2277 (IS_ITEMP (IC_LEFT (uic)) ||
2278 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2282 printf ("acc ok!\n");
2283 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2286 /** Does some transformations to reduce register pressure.
2289 packRegisters (eBBlock * ebp)
2294 D (D_ALLOC, ("packRegisters: entered.\n"));
2296 while (1 && !DISABLE_PACK_ASSIGN)
2299 /* look for assignments of the form */
2300 /* iTempNN = TRueSym (someoperation) SomeOperand */
2302 /* TrueSym := iTempNN:1 */
2303 for (ic = ebp->sch; ic; ic = ic->next)
2305 /* find assignment of the form TrueSym := iTempNN:1 */
2306 if (ic->op == '=' && !POINTER_SET (ic))
2307 change += packRegsForAssign (ic, ebp);
2313 for (ic = ebp->sch; ic; ic = ic->next)
2315 /* Safe: address of a true sym is always constant. */
2316 /* if this is an itemp & result of a address of a true sym
2317 then mark this as rematerialisable */
2318 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2320 if (ic->op == ADDRESS_OF &&
2321 IS_ITEMP (IC_RESULT (ic)) &&
2322 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2323 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2324 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2327 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2328 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2329 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2332 /* Safe: just propagates the remat flag */
2333 /* if straight assignment then carry remat flag if this is the
2335 if (ic->op == '=' &&
2336 !POINTER_SET (ic) &&
2337 IS_SYMOP (IC_RIGHT (ic)) &&
2338 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2339 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2342 OP_SYMBOL (IC_RESULT (ic))->remat =
2343 OP_SYMBOL (IC_RIGHT (ic))->remat;
2344 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2345 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2348 /* if the condition of an if instruction is defined in the
2349 previous instruction then mark the itemp as a conditional */
2350 if ((IS_CONDITIONAL (ic) ||
2351 ((ic->op == BITWISEAND ||
2354 isBitwiseOptimizable (ic))) &&
2355 ic->next && ic->next->op == IFX &&
2356 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2357 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2360 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2365 /* reduce for support function calls */
2366 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2367 packRegsForSupport (ic, ebp);
2371 /* some cases the redundant moves can
2372 can be eliminated for return statements */
2373 if ((ic->op == RETURN || ic->op == SEND) &&
2374 !isOperandInFarSpace (IC_LEFT (ic)) &&
2376 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2378 /* if pointer set & left has a size more than
2379 one and right is not in far space */
2380 if (POINTER_SET (ic) &&
2381 /* MLH: no such thing.
2382 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2383 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2384 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2385 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2388 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2391 /* if pointer get */
2392 if (!DISABLE_PACK_ONE_USE &&
2394 /* MLH: dont have far space
2395 !isOperandInFarSpace(IC_RESULT(ic))&& */
2396 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2397 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2398 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2401 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2403 /* pack registers for accumulator use, when the result of an
2404 arithmetic or bit wise operation has only one use, that use is
2405 immediately following the defintion and the using iCode has
2406 only one operand or has two operands but one is literal & the
2407 result of that operation is not on stack then we can leave the
2408 result of this operation in acc:b combination */
2410 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2412 packRegsForHLUse (ic);
2415 if ((IS_ARITHMETIC_OP (ic)
2416 || IS_BITWISE_OP (ic)
2417 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2419 IS_ITEMP (IC_RESULT (ic)) &&
2420 getSize (operandType (IC_RESULT (ic))) <= 2)
2421 packRegsForAccUse (ic);
2423 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2424 getSize (operandType (IC_RESULT (ic))) == 1)
2426 packRegsForAccUse2 (ic);
2433 /*-----------------------------------------------------------------*/
2434 /* assignRegisters - assigns registers to each live range as need */
2435 /*-----------------------------------------------------------------*/
2437 z80_assignRegisters (eBBlock ** ebbs, int count)
2442 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2444 setToNull ((void *) &funcrUsed);
2445 stackExtend = dataExtend = 0;
2449 /* DE is required for the code gen. */
2450 _nRegs = GBZ80_MAX_REGS;
2451 regsZ80 = _gbz80_regs;
2455 _nRegs = Z80_MAX_REGS;
2456 regsZ80 = _z80_regs;
2459 /* change assignments this will remove some
2460 live ranges reducing some register pressure */
2461 for (i = 0; i < count; i++)
2462 packRegisters (ebbs[i]);
2464 if (options.dump_pack)
2465 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2467 /* first determine for each live range the number of
2468 registers & the type of registers required for each */
2471 /* and serially allocate registers */
2472 serialRegAssign (ebbs, count);
2474 /* if stack was extended then tell the user */
2477 /* werror(W_TOOMANY_SPILS,"stack", */
2478 /* stackExtend,currFunc->name,""); */
2484 /* werror(W_TOOMANY_SPILS,"data space", */
2485 /* dataExtend,currFunc->name,""); */
2489 if (options.dump_rassgn)
2490 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2492 /* after that create the register mask
2493 for each of the instruction */
2494 createRegMask (ebbs, count);
2496 /* now get back the chain */
2497 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2499 /* redo that offsets for stacked automatic variables */
2500 redoStackOffsets ();
2504 /* free up any stackSpil locations allocated */
2505 applyToSet (stackSpil, deallocStackSpil);
2507 setToNull ((void **) &stackSpil);
2508 setToNull ((void **) &spiltSet);
2509 /* mark all registers as free */