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 #define DISABLE_PACKREGSFORSUPPORT 1
70 #define DISABLE_PACKREGSFORACCUSE 1
72 /*-----------------------------------------------------------------*/
73 /* At this point we start getting processor specific although */
74 /* some routines are non-processor specific & can be reused when */
75 /* targetting other processors. The decision for this will have */
76 /* to be made on a routine by routine basis */
77 /* routines used to pack registers are most definitely not reusable */
78 /* since the pack the registers depending strictly on the MCU */
79 /*-----------------------------------------------------------------*/
81 bitVect *spiltSet = NULL;
82 set *stackSpil = NULL;
83 bitVect *regAssigned = NULL;
86 extern void genZ80Code (iCode *);
87 bitVect *funcrUsed = NULL; /* registers used in a function */
92 /** Set to help debug register pressure related problems */
93 #define DEBUG_FAKE_EXTRA_REGS 0
95 static regs _gbz80_regs[] =
97 {REG_GPR, C_IDX, "c", 1},
98 {REG_GPR, B_IDX, "b", 1},
99 {REG_CND, CND_IDX, "c", 1}
102 static regs _z80_regs[] =
104 {REG_GPR, C_IDX, "c", 1},
105 {REG_GPR, B_IDX, "b", 1},
106 {REG_GPR, E_IDX, "e", 1},
107 {REG_GPR, D_IDX, "d", 1},
108 /* { REG_GPR, L_IDX , "l", 1 },
109 { REG_GPR, H_IDX , "h", 1 }, */
110 #if DEBUG_FAKE_EXTRA_REGS
111 {REG_GPR, M_IDX, "m", 1},
112 {REG_GPR, N_IDX, "n", 1},
113 {REG_GPR, O_IDX, "o", 1},
114 {REG_GPR, P_IDX, "p", 1},
115 {REG_GPR, Q_IDX, "q", 1},
116 {REG_GPR, R_IDX, "r", 1},
117 {REG_GPR, S_IDX, "s", 1},
118 {REG_GPR, T_IDX, "t", 1},
120 {REG_CND, CND_IDX, "c", 1}
125 /** Number of usable registers (all but C) */
126 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
127 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
129 static void spillThis (symbol *);
131 /** Allocates register of given type.
132 'type' is not used on the z80 version. It was used to select
133 between pointer and general purpose registers on the mcs51 version.
135 @return Pointer to the newly allocated register.
138 allocReg (short type)
142 for (i = 0; i < _nRegs; i++)
144 /* For now we allocate from any free */
145 if (regsZ80[i].isFree)
147 regsZ80[i].isFree = 0;
150 bitVectSetBit (currFunc->regsUsed, i);
151 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
155 D (D_ALLOC, ("allocReg: No free.\n"));
159 /** Returns pointer to register wit index number
166 for (i = 0; i < _nRegs; i++)
167 if (regsZ80[i].rIdx == idx)
170 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
171 "regWithIdx not found");
175 /** Frees a register.
180 wassert (!reg->isFree);
182 D (D_ALLOC, ("freeReg: freed %p\n", reg));
186 /** Returns number of free registers.
194 for (i = 0; i < _nRegs; i++)
196 /* For now only one reg type */
197 if (regsZ80[i].isFree)
203 /** Free registers with type.
206 nfreeRegsType (int type)
211 if ((nfr = nFreeRegs (type)) == 0)
212 return nFreeRegs (REG_GPR);
215 return nFreeRegs (type);
220 /*-----------------------------------------------------------------*/
221 /* allDefsOutOfRange - all definitions are out of a range */
222 /*-----------------------------------------------------------------*/
224 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
231 for (i = 0; i < defs->size; i++)
235 if (bitVectBitValue (defs, i) &&
236 (ic = hTabItemWithKey (iCodehTab, i)) &&
237 (ic->seq >= fseq && ic->seq <= toseq))
247 /*-----------------------------------------------------------------*/
248 /* computeSpillable - given a point find the spillable live ranges */
249 /*-----------------------------------------------------------------*/
251 computeSpillable (iCode * ic)
255 /* spillable live ranges are those that are live at this
256 point . the following categories need to be subtracted
258 a) - those that are already spilt
259 b) - if being used by this one
260 c) - defined by this one */
262 spillable = bitVectCopy (ic->rlive);
264 bitVectCplAnd (spillable, spiltSet); /* those already spilt */
266 bitVectCplAnd (spillable, ic->uses); /* used in this one */
267 bitVectUnSetBit (spillable, ic->defKey);
268 spillable = bitVectIntersect (spillable, regAssigned);
273 /*-----------------------------------------------------------------*/
274 /* noSpilLoc - return true if a variable has no spil location */
275 /*-----------------------------------------------------------------*/
277 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
279 return (sym->usl.spillLoc ? 0 : 1);
282 /*-----------------------------------------------------------------*/
283 /* hasSpilLoc - will return 1 if the symbol has spil location */
284 /*-----------------------------------------------------------------*/
286 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
288 return (sym->usl.spillLoc ? 1 : 0);
291 /** Will return 1 if the remat flag is set.
292 A symbol is rematerialisable if it doesnt need to be allocated
293 into registers at creation as it can be re-created at any time -
294 i.e. it's constant in some way.
297 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
302 /*-----------------------------------------------------------------*/
303 /* allLRs - return true for all */
304 /*-----------------------------------------------------------------*/
306 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
311 /*-----------------------------------------------------------------*/
312 /* liveRangesWith - applies function to a given set of live range */
313 /*-----------------------------------------------------------------*/
315 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
316 eBBlock * ebp, iCode * ic)
321 if (!lrs || !lrs->size)
324 for (i = 1; i < lrs->size; i++)
327 if (!bitVectBitValue (lrs, i))
330 /* if we don't find it in the live range
331 hash table we are in serious trouble */
332 if (!(sym = hTabItemWithKey (liveRanges, i)))
334 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
335 "liveRangesWith could not find liveRange");
339 if (func (sym, ebp, ic) && bitVectBitValue (regAssigned, sym->key))
340 addSetHead (&rset, sym);
347 /*-----------------------------------------------------------------*/
348 /* leastUsedLR - given a set determines which is the least used */
349 /*-----------------------------------------------------------------*/
351 leastUsedLR (set * sset)
353 symbol *sym = NULL, *lsym = NULL;
355 sym = lsym = setFirstItem (sset);
360 for (; lsym; lsym = setNextItem (sset))
363 /* if usage is the same then prefer
364 the spill the smaller of the two */
365 if (lsym->used == sym->used)
366 if (getSize (lsym->type) < getSize (sym->type))
370 if (lsym->used < sym->used)
375 setToNull ((void **) &sset);
380 /*-----------------------------------------------------------------*/
381 /* noOverLap - will iterate through the list looking for over lap */
382 /*-----------------------------------------------------------------*/
384 noOverLap (set * itmpStack, symbol * fsym)
388 for (sym = setFirstItem (itmpStack); sym;
389 sym = setNextItem (itmpStack))
391 if (sym->liveTo > fsym->liveFrom)
398 /*-----------------------------------------------------------------*/
399 /* isFree - will return 1 if the a free spil location is found */
400 /*-----------------------------------------------------------------*/
404 V_ARG (symbol **, sloc);
405 V_ARG (symbol *, fsym);
407 /* if already found */
411 /* if it is free && and the itmp assigned to
412 this does not have any overlapping live ranges
413 with the one currently being assigned and
414 the size can be accomodated */
416 noOverLap (sym->usl.itmpStack, fsym) &&
417 getSize (sym->type) >= getSize (fsym->type))
426 /*-----------------------------------------------------------------*/
427 /* createStackSpil - create a location on the stack to spil */
428 /*-----------------------------------------------------------------*/
430 createStackSpil (symbol * sym)
434 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
436 /* first go try and find a free one that is already
437 existing on the stack */
438 if (applyToSet (stackSpil, isFree, &sloc, sym))
440 /* found a free one : just update & return */
441 sym->usl.spillLoc = sloc;
444 addSetHead (&sloc->usl.itmpStack, sym);
445 D (D_ALLOC, ("createStackSpil: found existing\n"));
449 /* could not then have to create one , this is the hard part
450 we need to allocate this on the stack : this is really a
451 hack!! but cannot think of anything better at this time */
453 sprintf (buffer, "sloc%d", slocNum++);
454 sloc = newiTemp (buffer);
456 /* set the type to the spilling symbol */
457 sloc->type = copyLinkChain (sym->type);
458 sloc->etype = getSpec (sloc->type);
459 SPEC_SCLS (sloc->etype) = S_AUTO;
461 /* we don't allow it to be allocated`
462 onto the external stack since : so we
463 temporarily turn it off ; we also
464 turn off memory model to prevent
465 the spil from going to the external storage
466 and turn off overlaying
470 sloc->isref = 1; /* to prevent compiler warning */
472 /* if it is on the stack then update the stack */
473 if (IN_STACK (sloc->etype))
475 currFunc->stack += getSize (sloc->type);
476 stackExtend += getSize (sloc->type);
479 dataExtend += getSize (sloc->type);
481 /* add it to the stackSpil set */
482 addSetHead (&stackSpil, sloc);
483 sym->usl.spillLoc = sloc;
486 /* add it to the set of itempStack set
487 of the spill location */
488 addSetHead (&sloc->usl.itmpStack, sym);
490 D (D_ALLOC, ("createStackSpil: created new\n"));
494 /*-----------------------------------------------------------------*/
495 /* isSpiltOnStack - returns true if the spil location is on stack */
496 /*-----------------------------------------------------------------*/
498 isSpiltOnStack (symbol * sym)
508 /* if (sym->stackSpil) */
511 if (!sym->usl.spillLoc)
514 etype = getSpec (sym->usl.spillLoc->type);
515 if (IN_STACK (etype))
521 /*-----------------------------------------------------------------*/
522 /* spillThis - spils a specific operand */
523 /*-----------------------------------------------------------------*/
525 spillThis (symbol * sym)
529 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
531 /* if this is rematerializable or has a spillLocation
532 we are okay, else we need to create a spillLocation
534 if (!(sym->remat || sym->usl.spillLoc))
535 createStackSpil (sym);
537 /* mark it has spilt & put it in the spilt set */
539 spiltSet = bitVectSetBit (spiltSet, sym->key);
541 bitVectUnSetBit (regAssigned, sym->key);
543 for (i = 0; i < sym->nRegs; i++)
547 freeReg (sym->regs[i]);
552 /* if spilt on stack then free up r0 & r1
553 if they could have been assigned to some
555 if (sym->usl.spillLoc && !sym->remat)
556 sym->usl.spillLoc->allocreq = 1;
560 /** Select a iTemp to spil : rather a simple procedure.
563 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
565 bitVect *lrcs = NULL;
569 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
570 /* get the spillable live ranges */
571 lrcs = computeSpillable (ic);
573 /* get all live ranges that are rematerizable */
574 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
576 D (D_ALLOC, ("selectSpil: using remat.\n"));
577 /* return the least used of these */
578 return leastUsedLR (selectS);
582 /* get live ranges with spillLocations in direct space */
583 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
585 sym = leastUsedLR (selectS);
586 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
587 sym->usl.spillLoc->rname :
588 sym->usl.spillLoc->name));
590 /* mark it as allocation required */
591 sym->usl.spillLoc->allocreq = 1;
595 /* if the symbol is local to the block then */
596 if (forSym->liveTo < ebp->lSeq)
599 /* check if there are any live ranges allocated
600 to registers that are not used in this block */
601 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
603 sym = leastUsedLR (selectS);
604 /* if this is not rematerializable */
613 /* check if there are any live ranges that not
614 used in the remainder of the block */
615 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
617 sym = leastUsedLR (selectS);
629 /* find live ranges with spillocation && not used as pointers */
630 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
633 sym = leastUsedLR (selectS);
634 /* mark this as allocation required */
635 sym->usl.spillLoc->allocreq = 1;
640 /* find live ranges with spillocation */
641 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
643 D (D_ALLOC, ("selectSpil: using with spill.\n"));
644 sym = leastUsedLR (selectS);
645 sym->usl.spillLoc->allocreq = 1;
649 /* couldn't find then we need to create a spil
650 location on the stack , for which one? the least
652 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
654 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
655 /* return a created spil location */
656 sym = createStackSpil (leastUsedLR (selectS));
657 sym->usl.spillLoc->allocreq = 1;
661 /* this is an extreme situation we will spill
662 this one : happens very rarely but it does happen */
663 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
669 /** Spil some variable & mark registers as free.
670 A spill occurs when an iTemp wont fit into the available registers.
673 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
678 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
680 /* get something we can spil */
681 ssym = selectSpil (ic, ebp, forSym);
683 /* mark it as spilt */
685 spiltSet = bitVectSetBit (spiltSet, ssym->key);
687 /* mark it as not register assigned &
688 take it away from the set */
689 bitVectUnSetBit (regAssigned, ssym->key);
691 /* mark the registers as free */
692 for (i = 0; i < ssym->nRegs; i++)
694 freeReg (ssym->regs[i]);
696 /* if spilt on stack then free up r0 & r1
697 if they could have been assigned to as gprs */
698 if (!ptrRegReq && isSpiltOnStack (ssym))
701 spillLRWithPtrReg (ssym);
704 /* if this was a block level spil then insert push & pop
705 at the start & end of block respectively */
708 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
709 /* add push to the start of the block */
710 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
711 ebp->sch->next : ebp->sch));
712 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
713 /* add pop to the end of the block */
714 addiCodeToeBBlock (ebp, nic, NULL);
717 /* if spilt because not used in the remainder of the
718 block then add a push before this instruction and
719 a pop at the end of the block */
720 if (ssym->remainSpil)
723 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
724 /* add push just before this instruction */
725 addiCodeToeBBlock (ebp, nic, ic);
727 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
728 /* add pop to the end of the block */
729 addiCodeToeBBlock (ebp, nic, NULL);
733 D (D_ALLOC, ("spilSomething: done.\n"));
741 /** Will try for GPR if not spil.
744 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
748 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
750 /* try for gpr type */
751 if ((reg = allocReg (REG_GPR)))
753 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
757 /* we have to spil */
758 if (!spilSomething (ic, ebp, sym))
760 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
764 /* this looks like an infinite loop but
765 in really selectSpil will abort */
769 /** Symbol has a given register.
772 symHasReg (symbol * sym, regs * reg)
776 for (i = 0; i < sym->nRegs; i++)
777 if (sym->regs[i] == reg)
783 /** Check the live to and if they have registers & are not spilt then
784 free up the registers
787 deassignLRs (iCode * ic, eBBlock * ebp)
793 for (sym = hTabFirstItem (liveRanges, &k); sym;
794 sym = hTabNextItem (liveRanges, &k))
798 /* if it does not end here */
799 if (sym->liveTo > ic->seq)
802 /* if it was spilt on stack then we can
803 mark the stack spil location as free */
808 sym->usl.spillLoc->isFree = 1;
814 if (!bitVectBitValue (regAssigned, sym->key))
817 /* special case check if this is an IFX &
818 the privious one was a pop and the
819 previous one was not spilt then keep track
821 if (ic->op == IFX && ic->prev &&
822 ic->prev->op == IPOP &&
823 !ic->prev->parmPush &&
824 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
825 psym = OP_SYMBOL (IC_LEFT (ic->prev));
827 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
833 bitVectUnSetBit (regAssigned, sym->key);
835 /* if the result of this one needs registers
836 and does not have it then assign it right
838 if (IC_RESULT (ic) &&
839 !(SKIP_IC2 (ic) || /* not a special icode */
840 ic->op == JUMPTABLE ||
845 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
846 result->liveTo > ic->seq && /* and will live beyond this */
847 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
848 result->regType == sym->regType && /* same register types */
849 result->nRegs && /* which needs registers */
850 !result->isspilt && /* and does not already have them */
852 !bitVectBitValue (regAssigned, result->key) &&
853 /* the number of free regs + number of regs in this LR
854 can accomodate the what result Needs */
855 ((nfreeRegsType (result->regType) +
856 sym->nRegs) >= result->nRegs)
859 for (i = 0; i < result->nRegs; i++)
862 result->regs[i] = sym->regs[i];
864 result->regs[i] = getRegGpr (ic, ebp, result);
866 /* if the allocation falied which means
867 this was spilt then break */
868 if (!result->regs[i])
876 regAssigned = bitVectSetBit (regAssigned, result->key);
879 /* free the remaining */
880 for (; i < sym->nRegs; i++)
884 if (!symHasReg (psym, sym->regs[i]))
885 freeReg (sym->regs[i]);
888 freeReg (sym->regs[i]);
889 // sym->regs[i] = NULL;
896 /** Reassign this to registers.
899 reassignLR (operand * op)
901 symbol *sym = OP_SYMBOL (op);
904 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
906 /* not spilt any more */
907 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
908 bitVectUnSetBit (spiltSet, sym->key);
910 regAssigned = bitVectSetBit (regAssigned, sym->key);
914 for (i = 0; i < sym->nRegs; i++)
915 sym->regs[i]->isFree = 0;
918 /** Determines if allocating will cause a spill.
921 willCauseSpill (int nr, int rt)
923 /* first check if there are any avlb registers
924 of te type required */
925 if (nFreeRegs (0) >= nr)
928 /* it will cause a spil */
932 /** The allocator can allocate same registers to result and operand,
933 if this happens make sure they are in the same position as the operand
934 otherwise chaos results.
937 positionRegs (symbol * result, symbol * opsym, int lineno)
939 int count = min (result->nRegs, opsym->nRegs);
940 int i, j = 0, shared = 0;
942 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
944 /* if the result has been spilt then cannot share */
949 /* first make sure that they actually share */
950 for (i = 0; i < count; i++)
952 for (j = 0; j < count; j++)
954 if (result->regs[i] == opsym->regs[j] && i != j)
964 regs *tmp = result->regs[i];
965 result->regs[i] = result->regs[j];
966 result->regs[j] = tmp;
971 /** Try to allocate a pair of registers to the symbol.
974 tryAllocatingRegPair (symbol * sym)
977 wassert (sym->nRegs == 2);
978 for (i = 0; i < _nRegs; i += 2)
980 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
982 regsZ80[i].isFree = 0;
983 sym->regs[0] = ®sZ80[i];
984 regsZ80[i + 1].isFree = 0;
985 sym->regs[1] = ®sZ80[i + 1];
989 bitVectSetBit (currFunc->regsUsed, i);
991 bitVectSetBit (currFunc->regsUsed, i + 1);
993 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
997 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1001 /** Serially allocate registers to the variables.
1002 This is the main register allocation function. It is called after
1006 serialRegAssign (eBBlock ** ebbs, int count)
1010 /* for all blocks */
1011 for (i = 0; i < count; i++)
1016 if (ebbs[i]->noPath &&
1017 (ebbs[i]->entryLabel != entryLabel &&
1018 ebbs[i]->entryLabel != returnLabel))
1021 /* of all instructions do */
1022 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1025 /* if this is an ipop that means some live
1026 range will have to be assigned again */
1030 reassignLR (IC_LEFT (ic));
1033 /* if result is present && is a true symbol */
1034 if (IC_RESULT (ic) && ic->op != IFX &&
1035 IS_TRUE_SYMOP (IC_RESULT (ic)))
1036 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1038 /* take away registers from live
1039 ranges that end at this instruction */
1040 deassignLRs (ic, ebbs[i]);
1042 /* some don't need registers */
1043 /* MLH: removed RESULT and POINTER_SET condition */
1044 if (SKIP_IC2 (ic) ||
1045 ic->op == JUMPTABLE ||
1051 /* now we need to allocate registers only for the result */
1054 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1059 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1061 /* if it does not need or is spilt
1062 or is already assigned to registers
1063 or will not live beyond this instructions */
1066 bitVectBitValue (regAssigned, sym->key) ||
1067 sym->liveTo <= ic->seq)
1069 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1073 /* if some liverange has been spilt at the block level
1074 and this one live beyond this block then spil this
1076 if (blockSpil && sym->liveTo > ebbs[i]->lSeq)
1078 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1082 /* if trying to allocate this will cause
1083 a spill and there is nothing to spill
1084 or this one is rematerializable then
1086 willCS = willCauseSpill (sym->nRegs, sym->regType);
1087 spillable = computeSpillable (ic);
1089 (willCS && bitVectIsZero (spillable)))
1092 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1098 /* if it has a spillocation & is used less than
1099 all other live ranges then spill this */
1101 if (sym->usl.spillLoc) {
1102 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1103 allLRs, ebbs[i], ic));
1104 if (leastUsed && leastUsed->used > sym->used) {
1109 /* if none of the liveRanges have a spillLocation then better
1110 to spill this one than anything else already assigned to registers */
1111 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1118 /* else we assign registers to it */
1119 regAssigned = bitVectSetBit (regAssigned, sym->key);
1121 /* Special case: Try to fit into a reg pair if
1123 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1124 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1129 for (j = 0; j < sym->nRegs; j++)
1131 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1133 /* if the allocation falied which means
1134 this was spilt then break */
1137 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1142 /* if it shares registers with operands make sure
1143 that they are in the same position */
1144 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1145 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1146 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1147 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1148 /* do the same for the right operand */
1149 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1150 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1151 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1152 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1159 /*-----------------------------------------------------------------*/
1160 /* rUmaskForOp :- returns register mask for an operand */
1161 /*-----------------------------------------------------------------*/
1163 rUmaskForOp (operand * op)
1169 /* only temporaries are assigned registers */
1173 sym = OP_SYMBOL (op);
1175 /* if spilt or no registers assigned to it
1177 if (sym->isspilt || !sym->nRegs)
1180 rumask = newBitVect (_nRegs);
1182 for (j = 0; j < sym->nRegs; j++)
1184 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1190 /** Returns bit vector of registers used in iCode.
1193 regsUsedIniCode (iCode * ic)
1195 bitVect *rmask = newBitVect (_nRegs);
1197 /* do the special cases first */
1200 rmask = bitVectUnion (rmask,
1201 rUmaskForOp (IC_COND (ic)));
1205 /* for the jumptable */
1206 if (ic->op == JUMPTABLE)
1208 rmask = bitVectUnion (rmask,
1209 rUmaskForOp (IC_JTCOND (ic)));
1214 /* of all other cases */
1216 rmask = bitVectUnion (rmask,
1217 rUmaskForOp (IC_LEFT (ic)));
1221 rmask = bitVectUnion (rmask,
1222 rUmaskForOp (IC_RIGHT (ic)));
1225 rmask = bitVectUnion (rmask,
1226 rUmaskForOp (IC_RESULT (ic)));
1232 /** For each instruction will determine the regsUsed.
1235 createRegMask (eBBlock ** ebbs, int count)
1239 /* for all blocks */
1240 for (i = 0; i < count; i++)
1244 if (ebbs[i]->noPath &&
1245 (ebbs[i]->entryLabel != entryLabel &&
1246 ebbs[i]->entryLabel != returnLabel))
1249 /* for all instructions */
1250 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1255 if (SKIP_IC2 (ic) || !ic->rlive)
1258 /* first mark the registers used in this
1260 ic->rUsed = regsUsedIniCode (ic);
1261 funcrUsed = bitVectUnion (funcrUsed, ic->rUsed);
1263 /* now create the register mask for those
1264 registers that are in use : this is a
1265 super set of ic->rUsed */
1266 ic->rMask = newBitVect (_nRegs + 1);
1268 /* for all live Ranges alive at this point */
1269 for (j = 1; j < ic->rlive->size; j++)
1274 /* if not alive then continue */
1275 if (!bitVectBitValue (ic->rlive, j))
1278 /* find the live range we are interested in */
1279 if (!(sym = hTabItemWithKey (liveRanges, j)))
1281 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1282 "createRegMask cannot find live range");
1286 /* if no register assigned to it */
1287 if (!sym->nRegs || sym->isspilt)
1290 /* for all the registers allocated to it */
1291 for (k = 0; k < sym->nRegs; k++)
1294 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1300 /** Returns the rematerialized string for a remat var.
1303 rematStr (symbol * sym)
1306 iCode *ic = sym->rematiCode;
1311 /* if plus or minus print the right hand side */
1312 if (ic->op == '+' || ic->op == '-')
1314 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1317 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1320 /* we reached the end */
1321 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1328 /*-----------------------------------------------------------------*/
1329 /* regTypeNum - computes the type & number of registers required */
1330 /*-----------------------------------------------------------------*/
1337 /* for each live range do */
1338 for (sym = hTabFirstItem (liveRanges, &k); sym;
1339 sym = hTabNextItem (liveRanges, &k))
1342 /* if used zero times then no registers needed */
1343 if ((sym->liveTo - sym->liveFrom) == 0)
1346 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1348 /* if the live range is a temporary */
1352 /* if the type is marked as a conditional */
1353 if (sym->regType == REG_CND)
1356 /* if used in return only then we don't
1358 if (sym->ruonly || sym->accuse)
1360 if (IS_AGGREGATE (sym->type) || sym->isptr)
1361 sym->type = aggrToPtr (sym->type, FALSE);
1365 /* if not then we require registers */
1366 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1367 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1368 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1369 getSize (sym->type));
1370 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1372 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1376 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1377 printTypeChain (sym->type, stderr);
1378 fprintf (stderr, "\n");
1381 /* determine the type of register required */
1382 /* Always general purpose */
1383 sym->regType = REG_GPR;
1388 /* for the first run we don't provide */
1389 /* registers for true symbols we will */
1390 /* see how things go */
1391 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1398 /** Mark all registers as free.
1405 D (D_ALLOC, ("freeAllRegs: running.\n"));
1407 for (i = 0; i < _nRegs; i++)
1408 regsZ80[i].isFree = 1;
1411 /*-----------------------------------------------------------------*/
1412 /* deallocStackSpil - this will set the stack pointer back */
1413 /*-----------------------------------------------------------------*/
1414 DEFSETFUNC (deallocStackSpil)
1422 /** Register reduction for assignment.
1425 packRegsForAssign (iCode * ic, eBBlock * ebp)
1429 D (D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1432 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1433 !IS_ITEMP (IC_RIGHT (ic)) ||
1434 OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1435 OP_SYMBOL (IC_RIGHT (ic))->isind)
1439 /* if the true symbol is defined in far space or on stack
1440 then we should not since this will increase register pressure */
1441 if (isOperandInFarSpace (IC_RESULT (ic)))
1443 if ((dic = farSpacePackable (ic)))
1450 /* find the definition of iTempNN scanning backwards if we find a
1451 a use of the true symbol in before we find the definition then
1453 for (dic = ic->prev; dic; dic = dic->prev)
1455 /* if there is a function call and this is
1456 a parameter & not my parameter then don't pack it */
1457 if ((dic->op == CALL || dic->op == PCALL) &&
1458 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1459 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1468 if (IS_SYMOP (IC_RESULT (dic)) &&
1469 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1474 if (IS_SYMOP (IC_RIGHT (dic)) &&
1475 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1476 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1482 if (IS_SYMOP (IC_LEFT (dic)) &&
1483 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1484 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1490 if (POINTER_SET (dic) &&
1491 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1500 return 0; /* did not find */
1502 /* if the result is on stack or iaccess then it must be
1503 the same atleast one of the operands */
1504 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1505 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1508 /* the operation has only one symbol
1509 operator then we can pack */
1510 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1511 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1514 if (!((IC_LEFT (dic) &&
1515 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1517 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1521 /* found the definition */
1522 /* replace the result with the result of */
1523 /* this assignment and remove this assignment */
1524 IC_RESULT (dic) = IC_RESULT (ic);
1526 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1528 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1530 /* delete from liverange table also
1531 delete from all the points inbetween and the new
1533 for (sic = dic; sic != ic; sic = sic->next)
1535 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1536 if (IS_ITEMP (IC_RESULT (dic)))
1537 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1540 remiCodeFromeBBlock (ebp, ic);
1544 /** Scanning backwards looks for first assig found.
1547 findAssignToSym (operand * op, iCode * ic)
1551 for (dic = ic->prev; dic; dic = dic->prev)
1554 /* if definition by assignment */
1555 if (dic->op == '=' &&
1556 !POINTER_SET (dic) &&
1557 IC_RESULT (dic)->key == op->key)
1558 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1561 /* we are interested only if defined in far space */
1562 /* or in stack space in case of + & - */
1564 /* if assigned to a non-symbol then return
1566 if (!IS_SYMOP (IC_RIGHT (dic)))
1569 /* if the symbol is in far space then
1571 if (isOperandInFarSpace (IC_RIGHT (dic)))
1574 /* for + & - operations make sure that
1575 if it is on the stack it is the same
1576 as one of the three operands */
1577 if ((ic->op == '+' || ic->op == '-') &&
1578 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1581 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1582 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1583 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1591 /* if we find an usage then we cannot delete it */
1592 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1595 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1598 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1602 /* now make sure that the right side of dic
1603 is not defined between ic & dic */
1606 iCode *sic = dic->next;
1608 for (; sic != ic; sic = sic->next)
1609 if (IC_RESULT (sic) &&
1610 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1619 #if !DISABLE_PACKREGSFORSUPPORT
1622 /*-----------------------------------------------------------------*/
1623 /* packRegsForSupport :- reduce some registers for support calls */
1624 /*-----------------------------------------------------------------*/
1626 packRegsForSupport (iCode * ic, eBBlock * ebp)
1629 /* for the left & right operand :- look to see if the
1630 left was assigned a true symbol in far space in that
1631 case replace them */
1632 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1634 if (IS_ITEMP (IC_LEFT (ic)) &&
1635 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1637 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1643 /* found it we need to remove it from the
1645 for (sic = dic; sic != ic; sic = sic->next)
1646 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1648 IC_LEFT (ic)->operand.symOperand =
1649 IC_RIGHT (dic)->operand.symOperand;
1650 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1651 remiCodeFromeBBlock (ebp, dic);
1655 /* do the same for the right operand */
1658 IS_ITEMP (IC_RIGHT (ic)) &&
1659 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1661 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1667 /* found it we need to remove it from the block */
1668 for (sic = dic; sic != ic; sic = sic->next)
1669 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1671 IC_RIGHT (ic)->operand.symOperand =
1672 IC_RIGHT (dic)->operand.symOperand;
1673 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1675 remiCodeFromeBBlock (ebp, dic);
1683 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1685 /** Will reduce some registers for single use.
1688 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1693 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1695 /* if returning a literal then do nothing */
1699 /* only upto 2 bytes since we cannot predict
1700 the usage of b, & acc */
1701 if (getSize (operandType (op)) > 2 &&
1706 /* this routine will mark the a symbol as used in one
1707 instruction use only && if the defintion is local
1708 (ie. within the basic block) && has only one definition &&
1709 that definiion is either a return value from a
1710 function or does not contain any variables in
1712 uses = bitVectCopy (OP_USES (op));
1713 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1714 if (!bitVectIsZero (uses)) /* has other uses */
1717 /* if it has only one defintion */
1718 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1719 return NULL; /* has more than one definition */
1721 /* get the that definition */
1723 hTabItemWithKey (iCodehTab,
1724 bitVectFirstBit (OP_DEFS (op)))))
1727 /* found the definition now check if it is local */
1728 if (dic->seq < ebp->fSeq ||
1729 dic->seq > ebp->lSeq)
1730 return NULL; /* non-local */
1732 /* now check if it is the return from a function call */
1733 if (dic->op == CALL || dic->op == PCALL)
1735 if (ic->op != SEND && ic->op != RETURN)
1737 OP_SYMBOL (op)->ruonly = 1;
1743 /* otherwise check that the definition does
1744 not contain any symbols in far space */
1745 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1746 isOperandInFarSpace (IC_RIGHT (dic)) ||
1747 IS_OP_RUONLY (IC_LEFT (ic)) ||
1748 IS_OP_RUONLY (IC_RIGHT (ic)))
1753 /* if pointer set then make sure the pointer is one byte */
1754 if (POINTER_SET (dic))
1757 if (POINTER_GET (dic))
1762 /* also make sure the intervenening instructions
1763 don't have any thing in far space */
1764 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1766 /* if there is an intervening function call then no */
1767 if (dic->op == CALL || dic->op == PCALL)
1769 /* if pointer set then make sure the pointer
1771 if (POINTER_SET (dic))
1774 if (POINTER_GET (dic))
1777 /* if address of & the result is remat the okay */
1778 if (dic->op == ADDRESS_OF &&
1779 OP_SYMBOL (IC_RESULT (dic))->remat)
1782 /* if left or right or result is in far space */
1783 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1784 isOperandInFarSpace (IC_RIGHT (dic)) ||
1785 isOperandInFarSpace (IC_RESULT (dic)) ||
1786 IS_OP_RUONLY (IC_LEFT (dic)) ||
1787 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1788 IS_OP_RUONLY (IC_RESULT (dic)))
1794 OP_SYMBOL (op)->ruonly = 1;
1798 /*-----------------------------------------------------------------*/
1799 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1800 /*-----------------------------------------------------------------*/
1802 isBitwiseOptimizable (iCode * ic)
1804 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1806 /* bitwise operations are considered optimizable
1807 under the following conditions (Jean-Louis VERN)
1819 if (IS_LITERAL (rtype))
1825 Certian assignments involving pointers can be temporarly stored
1836 #if !DISABLE_PACKREGSFORACCUSE
1839 /** Pack registers for acc use.
1840 When the result of this operation is small and short lived it may
1841 be able to be stored in the accumelator.
1844 packRegsForAccUse (iCode * ic)
1848 /* if + or - then it has to be one byte result */
1849 if ((ic->op == '+' || ic->op == '-')
1850 && getSize (operandType (IC_RESULT (ic))) > 1)
1853 /* if shift operation make sure right side is not a literal */
1854 if (ic->op == RIGHT_OP &&
1855 (isOperandLiteral (IC_RIGHT (ic)) ||
1856 getSize (operandType (IC_RESULT (ic))) > 1))
1859 if (ic->op == LEFT_OP &&
1860 (isOperandLiteral (IC_RIGHT (ic)) ||
1861 getSize (operandType (IC_RESULT (ic))) > 1))
1864 /* has only one definition */
1865 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1868 /* has only one use */
1869 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1872 /* and the usage immediately follows this iCode */
1873 if (!(uic = hTabItemWithKey (iCodehTab,
1874 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1877 if (ic->next != uic)
1880 /* if it is a conditional branch then we definitely can */
1884 if (uic->op == JUMPTABLE)
1888 /* if the usage is not is an assignment or an
1889 arithmetic / bitwise / shift operation then not */
1890 if (POINTER_SET (uic) &&
1891 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1895 if (uic->op != '=' &&
1896 !IS_ARITHMETIC_OP (uic) &&
1897 !IS_BITWISE_OP (uic) &&
1898 uic->op != LEFT_OP &&
1899 uic->op != RIGHT_OP)
1902 /* if used in ^ operation then make sure right is not a
1904 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1907 /* if shift operation make sure right side is not a literal */
1908 if (uic->op == RIGHT_OP &&
1909 (isOperandLiteral (IC_RIGHT (uic)) ||
1910 getSize (operandType (IC_RESULT (uic))) > 1))
1913 if (uic->op == LEFT_OP &&
1914 (isOperandLiteral (IC_RIGHT (uic)) ||
1915 getSize (operandType (IC_RESULT (uic))) > 1))
1919 /* make sure that the result of this icode is not on the
1920 stack, since acc is used to compute stack offset */
1921 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1922 OP_SYMBOL (IC_RESULT (uic))->onStack)
1927 /* if either one of them in far space then we cannot */
1928 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1929 isOperandInFarSpace (IC_LEFT (uic))) ||
1930 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1931 isOperandInFarSpace (IC_RIGHT (uic))))
1935 /* if the usage has only one operand then we can */
1936 if (IC_LEFT (uic) == NULL ||
1937 IC_RIGHT (uic) == NULL)
1940 /* make sure this is on the left side if not
1941 a '+' since '+' is commutative */
1942 if (ic->op != '+' &&
1943 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1946 /* if one of them is a literal then we can */
1947 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1948 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1954 /** This is confusing :) Guess for now */
1955 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1956 (IS_ITEMP (IC_RIGHT (uic)) ||
1957 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
1960 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
1961 (IS_ITEMP (IC_LEFT (uic)) ||
1962 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
1966 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
1971 packRegsForHLUse (iCode * ic)
1978 /* has only one definition */
1979 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1982 /* has only one use */
1983 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1986 /* and the usage immediately follows this iCode */
1987 if (!(uic = hTabItemWithKey (iCodehTab,
1988 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1991 if (ic->next != uic)
1994 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
1996 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2000 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2004 opPreservesA (iCode * ic, iCode * uic)
2006 /* if it is a conditional branch then we definitely can */
2010 if (uic->op == JUMPTABLE)
2013 /* if the usage has only one operand then we can */
2014 /* PENDING: check */
2015 if (IC_LEFT (uic) == NULL ||
2016 IC_RIGHT (uic) == NULL)
2019 /* PENDING: check this rule */
2020 if (getSize (operandType (IC_RESULT (uic))) > 1)
2027 !IS_ARITHMETIC_OP(uic) (sub requires A)
2031 !IS_BITWISE_OP (uic) &&
2034 !POINTER_GET (uic) &&
2036 uic->op != LEFT_OP &&
2037 uic->op != RIGHT_OP && */
2045 if (!IC_LEFT (uic) || !IC_RESULT (ic))
2048 /** This is confusing :) Guess for now */
2049 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2050 (IS_ITEMP (IC_RIGHT (uic)) ||
2051 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2054 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2055 (IS_ITEMP (IC_LEFT (uic)) ||
2056 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2063 joinPushes (iCode * ic)
2066 if (ic->op == IPUSH &&
2067 isOperandLiteral (IC_LEFT (ic)) &&
2068 getSize (operandType (IC_LEFT (ic))) == 1 &&
2069 ic->next->op == IPUSH &&
2070 isOperandLiteral (IC_LEFT (ic->next)) &&
2071 getSize (operandType (IC_LEFT (ic->next))) == 1)
2073 /* This is a bit tricky as michaelh doesnt know what he's doing.
2075 /* First upgrade the size of (first) to int */
2076 SPEC_NOUN (operandType (IC_LEFT (ic))) = V_INT;
2078 floatFromVal (AOP /* need some sleep ... */ );
2079 /* Now get and join the values */
2080 value *val = aop->aopu.aop_lit;
2081 /* if it is a float then it gets tricky */
2082 /* otherwise it is fairly simple */
2083 if (!IS_FLOAT (val->type))
2085 unsigned long v = floatFromVal (val);
2087 floatFrom ( /* need some sleep ... */ );
2088 printf ("Size %u\n", getSize (operandType (IC_LEFT (ic))));
2089 ic->next = ic->next->next;
2095 /** Pack registers for acc use.
2096 When the result of this operation is small and short lived it may
2097 be able to be stored in the accumulator.
2099 Note that the 'A preserving' list is currently emperical :)e
2102 packRegsForAccUse2 (iCode * ic)
2106 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2108 /* Filter out all but those 'good' commands */
2110 !POINTER_GET (ic) &&
2112 !IS_BITWISE_OP (ic) &&
2119 /* if + or - then it has to be one byte result.
2122 if ((ic->op == '+' || ic->op == '-')
2123 && getSize (operandType (IC_RESULT (ic))) > 1)
2126 /* if shift operation make sure right side is not a literal.
2130 if (ic->op == RIGHT_OP &&
2131 (isOperandLiteral (IC_RIGHT (ic)) ||
2132 getSize (operandType (IC_RESULT (ic))) > 1))
2135 if (ic->op == LEFT_OP &&
2136 (isOperandLiteral (IC_RIGHT (ic)) ||
2137 getSize (operandType (IC_RESULT (ic))) > 1))
2141 /* has only one definition */
2142 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2147 /* Right. We may be able to propagate it through if:
2148 For each in the chain of uses the intermediate is OK.
2150 /* Get next with 'uses result' bit on
2151 If this->next == next
2152 Validate use of next
2153 If OK, increase count
2155 /* and the usage immediately follows this iCode */
2156 if (!(uic = hTabItemWithKey (iCodehTab,
2157 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2163 /* Create a copy of the OP_USES bit vect */
2164 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2166 iCode *scan = ic, *next;
2170 setBit = bitVectFirstBit (uses);
2171 next = hTabItemWithKey (iCodehTab, setBit);
2172 if (scan->next == next)
2174 bitVectUnSetBit (uses, setBit);
2175 /* Still contigous. */
2176 if (!opPreservesA (ic, next))
2187 while (!bitVectIsZero (uses));
2188 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2192 /* OLD CODE FOLLOWS */
2193 /* if it is a conditional branch then we definitely can
2201 if (uic->op == JUMPTABLE)
2205 /* if the usage is not is an assignment or an
2206 arithmetic / bitwise / shift operation then not.
2207 MLH: Pending: Invalid. Our pointer sets are always peechy.
2210 if (POINTER_SET (uic) &&
2211 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2213 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2219 if (uic->op != '=' &&
2220 !IS_ARITHMETIC_OP (uic) &&
2221 !IS_BITWISE_OP (uic) &&
2222 uic->op != LEFT_OP &&
2223 uic->op != RIGHT_OP)
2229 /* if used in ^ operation then make sure right is not a
2231 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2234 /* if shift operation make sure right side is not a literal */
2235 if (uic->op == RIGHT_OP &&
2236 (isOperandLiteral (IC_RIGHT (uic)) ||
2237 getSize (operandType (IC_RESULT (uic))) > 1))
2240 if (uic->op == LEFT_OP &&
2241 (isOperandLiteral (IC_RIGHT (uic)) ||
2242 getSize (operandType (IC_RESULT (uic))) > 1))
2246 /* make sure that the result of this icode is not on the
2247 stack, since acc is used to compute stack offset */
2248 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2249 OP_SYMBOL (IC_RESULT (uic))->onStack)
2254 /* if either one of them in far space then we cannot */
2255 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2256 isOperandInFarSpace (IC_LEFT (uic))) ||
2257 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2258 isOperandInFarSpace (IC_RIGHT (uic))))
2262 /* if the usage has only one operand then we can */
2263 if (IC_LEFT (uic) == NULL ||
2264 IC_RIGHT (uic) == NULL)
2267 /* make sure this is on the left side if not
2268 a '+' since '+' is commutative */
2269 if (ic->op != '+' &&
2270 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2273 /* if one of them is a literal then we can */
2274 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2275 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2281 /** This is confusing :) Guess for now */
2282 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2283 (IS_ITEMP (IC_RIGHT (uic)) ||
2284 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2287 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2288 (IS_ITEMP (IC_LEFT (uic)) ||
2289 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2293 printf ("acc ok!\n");
2294 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2297 /** Does some transformations to reduce register pressure.
2300 packRegisters (eBBlock * ebp)
2305 D (D_ALLOC, ("packRegisters: entered.\n"));
2307 while (1 && !DISABLE_PACK_ASSIGN)
2310 /* look for assignments of the form */
2311 /* iTempNN = TRueSym (someoperation) SomeOperand */
2313 /* TrueSym := iTempNN:1 */
2314 for (ic = ebp->sch; ic; ic = ic->next)
2316 /* find assignment of the form TrueSym := iTempNN:1 */
2317 if (ic->op == '=' && !POINTER_SET (ic))
2318 change += packRegsForAssign (ic, ebp);
2324 for (ic = ebp->sch; ic; ic = ic->next)
2326 /* Safe: address of a true sym is always constant. */
2327 /* if this is an itemp & result of a address of a true sym
2328 then mark this as rematerialisable */
2329 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2331 if (ic->op == ADDRESS_OF &&
2332 IS_ITEMP (IC_RESULT (ic)) &&
2333 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2334 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2335 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2338 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2339 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2340 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2343 /* Safe: just propagates the remat flag */
2344 /* if straight assignment then carry remat flag if this is the
2346 if (ic->op == '=' &&
2347 !POINTER_SET (ic) &&
2348 IS_SYMOP (IC_RIGHT (ic)) &&
2349 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2350 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2353 OP_SYMBOL (IC_RESULT (ic))->remat =
2354 OP_SYMBOL (IC_RIGHT (ic))->remat;
2355 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2356 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2359 /* if the condition of an if instruction is defined in the
2360 previous instruction then mark the itemp as a conditional */
2361 if ((IS_CONDITIONAL (ic) ||
2362 ((ic->op == BITWISEAND ||
2365 isBitwiseOptimizable (ic))) &&
2366 ic->next && ic->next->op == IFX &&
2367 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2368 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2371 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2376 /* reduce for support function calls */
2377 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2378 packRegsForSupport (ic, ebp);
2382 /* some cases the redundant moves can
2383 can be eliminated for return statements */
2384 if ((ic->op == RETURN || ic->op == SEND) &&
2385 !isOperandInFarSpace (IC_LEFT (ic)) &&
2387 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2389 /* if pointer set & left has a size more than
2390 one and right is not in far space */
2391 if (POINTER_SET (ic) &&
2392 /* MLH: no such thing.
2393 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2394 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2395 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2396 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2399 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2402 /* if pointer get */
2403 if (!DISABLE_PACK_ONE_USE &&
2405 /* MLH: dont have far space
2406 !isOperandInFarSpace(IC_RESULT(ic))&& */
2407 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2408 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2409 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2412 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2414 /* pack registers for accumulator use, when the result of an
2415 arithmetic or bit wise operation has only one use, that use is
2416 immediately following the defintion and the using iCode has
2417 only one operand or has two operands but one is literal & the
2418 result of that operation is not on stack then we can leave the
2419 result of this operation in acc:b combination */
2421 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2423 packRegsForHLUse (ic);
2426 if ((IS_ARITHMETIC_OP (ic)
2427 || IS_BITWISE_OP (ic)
2428 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2430 IS_ITEMP (IC_RESULT (ic)) &&
2431 getSize (operandType (IC_RESULT (ic))) <= 2)
2432 packRegsForAccUse (ic);
2434 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2435 getSize (operandType (IC_RESULT (ic))) == 1)
2437 packRegsForAccUse2 (ic);
2444 /*-----------------------------------------------------------------*/
2445 /* assignRegisters - assigns registers to each live range as need */
2446 /*-----------------------------------------------------------------*/
2448 z80_assignRegisters (eBBlock ** ebbs, int count)
2453 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2455 setToNull ((void *) &funcrUsed);
2456 stackExtend = dataExtend = 0;
2460 /* DE is required for the code gen. */
2461 _nRegs = GBZ80_MAX_REGS;
2462 regsZ80 = _gbz80_regs;
2466 _nRegs = Z80_MAX_REGS;
2467 regsZ80 = _z80_regs;
2470 /* change assignments this will remove some
2471 live ranges reducing some register pressure */
2472 for (i = 0; i < count; i++)
2473 packRegisters (ebbs[i]);
2475 if (options.dump_pack)
2476 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2478 /* first determine for each live range the number of
2479 registers & the type of registers required for each */
2482 /* and serially allocate registers */
2483 serialRegAssign (ebbs, count);
2485 /* if stack was extended then tell the user */
2488 /* werror(W_TOOMANY_SPILS,"stack", */
2489 /* stackExtend,currFunc->name,""); */
2495 /* werror(W_TOOMANY_SPILS,"data space", */
2496 /* dataExtend,currFunc->name,""); */
2500 if (options.dump_rassgn)
2501 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2503 /* after that create the register mask
2504 for each of the instruction */
2505 createRegMask (ebbs, count);
2507 /* now get back the chain */
2508 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2510 /* redo that offsets for stacked automatic variables */
2511 redoStackOffsets ();
2515 /* free up any stackSpil locations allocated */
2516 applyToSet (stackSpil, deallocStackSpil);
2518 setToNull ((void **) &stackSpil);
2519 setToNull ((void **) &spiltSet);
2520 /* mark all registers as free */