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);
1191 z80_rUmaskForOp (operand * op)
1193 return rUmaskForOp (op);
1196 /** Returns bit vector of registers used in iCode.
1199 regsUsedIniCode (iCode * ic)
1201 bitVect *rmask = newBitVect (_nRegs);
1203 /* do the special cases first */
1206 rmask = bitVectUnion (rmask,
1207 rUmaskForOp (IC_COND (ic)));
1211 /* for the jumptable */
1212 if (ic->op == JUMPTABLE)
1214 rmask = bitVectUnion (rmask,
1215 rUmaskForOp (IC_JTCOND (ic)));
1220 /* of all other cases */
1222 rmask = bitVectUnion (rmask,
1223 rUmaskForOp (IC_LEFT (ic)));
1227 rmask = bitVectUnion (rmask,
1228 rUmaskForOp (IC_RIGHT (ic)));
1231 rmask = bitVectUnion (rmask,
1232 rUmaskForOp (IC_RESULT (ic)));
1238 /** For each instruction will determine the regsUsed.
1241 createRegMask (eBBlock ** ebbs, int count)
1245 /* for all blocks */
1246 for (i = 0; i < count; i++)
1250 if (ebbs[i]->noPath &&
1251 (ebbs[i]->entryLabel != entryLabel &&
1252 ebbs[i]->entryLabel != returnLabel))
1255 /* for all instructions */
1256 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1261 if (SKIP_IC2 (ic) || !ic->rlive)
1264 /* first mark the registers used in this
1266 ic->rUsed = regsUsedIniCode (ic);
1267 funcrUsed = bitVectUnion (funcrUsed, ic->rUsed);
1269 /* now create the register mask for those
1270 registers that are in use : this is a
1271 super set of ic->rUsed */
1272 ic->rMask = newBitVect (_nRegs + 1);
1274 /* for all live Ranges alive at this point */
1275 for (j = 1; j < ic->rlive->size; j++)
1280 /* if not alive then continue */
1281 if (!bitVectBitValue (ic->rlive, j))
1284 /* find the live range we are interested in */
1285 if (!(sym = hTabItemWithKey (liveRanges, j)))
1287 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1288 "createRegMask cannot find live range");
1292 /* if no register assigned to it */
1293 if (!sym->nRegs || sym->isspilt)
1296 /* for all the registers allocated to it */
1297 for (k = 0; k < sym->nRegs; k++)
1300 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1306 /** Returns the rematerialized string for a remat var.
1309 rematStr (symbol * sym)
1312 iCode *ic = sym->rematiCode;
1317 /* if plus or minus print the right hand side */
1318 if (ic->op == '+' || ic->op == '-')
1320 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1323 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1326 /* we reached the end */
1327 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1334 /*-----------------------------------------------------------------*/
1335 /* regTypeNum - computes the type & number of registers required */
1336 /*-----------------------------------------------------------------*/
1343 /* for each live range do */
1344 for (sym = hTabFirstItem (liveRanges, &k); sym;
1345 sym = hTabNextItem (liveRanges, &k))
1348 /* if used zero times then no registers needed */
1349 if ((sym->liveTo - sym->liveFrom) == 0)
1352 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1354 /* if the live range is a temporary */
1358 /* if the type is marked as a conditional */
1359 if (sym->regType == REG_CND)
1362 /* if used in return only then we don't
1364 if (sym->ruonly || sym->accuse)
1366 if (IS_AGGREGATE (sym->type) || sym->isptr)
1367 sym->type = aggrToPtr (sym->type, FALSE);
1371 /* if not then we require registers */
1372 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1373 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1374 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1375 getSize (sym->type));
1376 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1378 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1382 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1383 printTypeChain (sym->type, stderr);
1384 fprintf (stderr, "\n");
1387 /* determine the type of register required */
1388 /* Always general purpose */
1389 sym->regType = REG_GPR;
1394 /* for the first run we don't provide */
1395 /* registers for true symbols we will */
1396 /* see how things go */
1397 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1404 /** Mark all registers as free.
1411 D (D_ALLOC, ("freeAllRegs: running.\n"));
1413 for (i = 0; i < _nRegs; i++)
1414 regsZ80[i].isFree = 1;
1417 /*-----------------------------------------------------------------*/
1418 /* deallocStackSpil - this will set the stack pointer back */
1419 /*-----------------------------------------------------------------*/
1420 DEFSETFUNC (deallocStackSpil)
1428 /** Register reduction for assignment.
1431 packRegsForAssign (iCode * ic, eBBlock * ebp)
1435 D (D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1438 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1439 !IS_ITEMP (IC_RIGHT (ic)) ||
1440 OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1441 OP_SYMBOL (IC_RIGHT (ic))->isind)
1445 /* if the true symbol is defined in far space or on stack
1446 then we should not since this will increase register pressure */
1447 if (isOperandInFarSpace (IC_RESULT (ic)))
1449 if ((dic = farSpacePackable (ic)))
1456 /* find the definition of iTempNN scanning backwards if we find a
1457 a use of the true symbol in before we find the definition then
1459 for (dic = ic->prev; dic; dic = dic->prev)
1461 /* if there is a function call and this is
1462 a parameter & not my parameter then don't pack it */
1463 if ((dic->op == CALL || dic->op == PCALL) &&
1464 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1465 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1474 if (IS_SYMOP (IC_RESULT (dic)) &&
1475 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1480 if (IS_SYMOP (IC_RIGHT (dic)) &&
1481 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1482 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1488 if (IS_SYMOP (IC_LEFT (dic)) &&
1489 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1490 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1496 if (POINTER_SET (dic) &&
1497 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1506 return 0; /* did not find */
1508 /* if the result is on stack or iaccess then it must be
1509 the same atleast one of the operands */
1510 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1511 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1514 /* the operation has only one symbol
1515 operator then we can pack */
1516 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1517 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1520 if (!((IC_LEFT (dic) &&
1521 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1523 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1527 /* found the definition */
1528 /* replace the result with the result of */
1529 /* this assignment and remove this assignment */
1530 IC_RESULT (dic) = IC_RESULT (ic);
1532 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1534 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1536 /* delete from liverange table also
1537 delete from all the points inbetween and the new
1539 for (sic = dic; sic != ic; sic = sic->next)
1541 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1542 if (IS_ITEMP (IC_RESULT (dic)))
1543 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1546 remiCodeFromeBBlock (ebp, ic);
1550 /** Scanning backwards looks for first assig found.
1553 findAssignToSym (operand * op, iCode * ic)
1557 for (dic = ic->prev; dic; dic = dic->prev)
1560 /* if definition by assignment */
1561 if (dic->op == '=' &&
1562 !POINTER_SET (dic) &&
1563 IC_RESULT (dic)->key == op->key)
1564 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1567 /* we are interested only if defined in far space */
1568 /* or in stack space in case of + & - */
1570 /* if assigned to a non-symbol then return
1572 if (!IS_SYMOP (IC_RIGHT (dic)))
1575 /* if the symbol is in far space then
1577 if (isOperandInFarSpace (IC_RIGHT (dic)))
1580 /* for + & - operations make sure that
1581 if it is on the stack it is the same
1582 as one of the three operands */
1583 if ((ic->op == '+' || ic->op == '-') &&
1584 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1587 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1588 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1589 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1597 /* if we find an usage then we cannot delete it */
1598 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1601 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1604 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1608 /* now make sure that the right side of dic
1609 is not defined between ic & dic */
1612 iCode *sic = dic->next;
1614 for (; sic != ic; sic = sic->next)
1615 if (IC_RESULT (sic) &&
1616 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1625 #if !DISABLE_PACKREGSFORSUPPORT
1628 /*-----------------------------------------------------------------*/
1629 /* packRegsForSupport :- reduce some registers for support calls */
1630 /*-----------------------------------------------------------------*/
1632 packRegsForSupport (iCode * ic, eBBlock * ebp)
1635 /* for the left & right operand :- look to see if the
1636 left was assigned a true symbol in far space in that
1637 case replace them */
1638 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1640 if (IS_ITEMP (IC_LEFT (ic)) &&
1641 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1643 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1649 /* found it we need to remove it from the
1651 for (sic = dic; sic != ic; sic = sic->next)
1652 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1654 IC_LEFT (ic)->operand.symOperand =
1655 IC_RIGHT (dic)->operand.symOperand;
1656 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1657 remiCodeFromeBBlock (ebp, dic);
1661 /* do the same for the right operand */
1664 IS_ITEMP (IC_RIGHT (ic)) &&
1665 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1667 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1673 /* found it we need to remove it from the block */
1674 for (sic = dic; sic != ic; sic = sic->next)
1675 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1677 IC_RIGHT (ic)->operand.symOperand =
1678 IC_RIGHT (dic)->operand.symOperand;
1679 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1681 remiCodeFromeBBlock (ebp, dic);
1689 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1691 /** Will reduce some registers for single use.
1694 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1699 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1701 /* if returning a literal then do nothing */
1705 /* only upto 2 bytes since we cannot predict
1706 the usage of b, & acc */
1707 if (getSize (operandType (op)) > 2 &&
1712 /* this routine will mark the a symbol as used in one
1713 instruction use only && if the defintion is local
1714 (ie. within the basic block) && has only one definition &&
1715 that definiion is either a return value from a
1716 function or does not contain any variables in
1718 uses = bitVectCopy (OP_USES (op));
1719 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1720 if (!bitVectIsZero (uses)) /* has other uses */
1723 /* if it has only one defintion */
1724 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1725 return NULL; /* has more than one definition */
1727 /* get the that definition */
1729 hTabItemWithKey (iCodehTab,
1730 bitVectFirstBit (OP_DEFS (op)))))
1733 /* found the definition now check if it is local */
1734 if (dic->seq < ebp->fSeq ||
1735 dic->seq > ebp->lSeq)
1736 return NULL; /* non-local */
1738 /* now check if it is the return from a function call */
1739 if (dic->op == CALL || dic->op == PCALL)
1741 if (ic->op != SEND && ic->op != RETURN)
1743 OP_SYMBOL (op)->ruonly = 1;
1749 /* otherwise check that the definition does
1750 not contain any symbols in far space */
1751 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1752 isOperandInFarSpace (IC_RIGHT (dic)) ||
1753 IS_OP_RUONLY (IC_LEFT (ic)) ||
1754 IS_OP_RUONLY (IC_RIGHT (ic)))
1759 /* if pointer set then make sure the pointer is one byte */
1760 if (POINTER_SET (dic))
1763 if (POINTER_GET (dic))
1768 /* also make sure the intervenening instructions
1769 don't have any thing in far space */
1770 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1772 /* if there is an intervening function call then no */
1773 if (dic->op == CALL || dic->op == PCALL)
1775 /* if pointer set then make sure the pointer
1777 if (POINTER_SET (dic))
1780 if (POINTER_GET (dic))
1783 /* if address of & the result is remat the okay */
1784 if (dic->op == ADDRESS_OF &&
1785 OP_SYMBOL (IC_RESULT (dic))->remat)
1788 /* if left or right or result is in far space */
1789 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1790 isOperandInFarSpace (IC_RIGHT (dic)) ||
1791 isOperandInFarSpace (IC_RESULT (dic)) ||
1792 IS_OP_RUONLY (IC_LEFT (dic)) ||
1793 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1794 IS_OP_RUONLY (IC_RESULT (dic)))
1800 OP_SYMBOL (op)->ruonly = 1;
1804 /*-----------------------------------------------------------------*/
1805 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1806 /*-----------------------------------------------------------------*/
1808 isBitwiseOptimizable (iCode * ic)
1810 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1812 /* bitwise operations are considered optimizable
1813 under the following conditions (Jean-Louis VERN)
1825 if (IS_LITERAL (rtype))
1831 Certian assignments involving pointers can be temporarly stored
1842 #if !DISABLE_PACKREGSFORACCUSE
1845 /** Pack registers for acc use.
1846 When the result of this operation is small and short lived it may
1847 be able to be stored in the accumelator.
1850 packRegsForAccUse (iCode * ic)
1854 /* if + or - then it has to be one byte result */
1855 if ((ic->op == '+' || ic->op == '-')
1856 && getSize (operandType (IC_RESULT (ic))) > 1)
1859 /* if shift operation make sure right side is not a literal */
1860 if (ic->op == RIGHT_OP &&
1861 (isOperandLiteral (IC_RIGHT (ic)) ||
1862 getSize (operandType (IC_RESULT (ic))) > 1))
1865 if (ic->op == LEFT_OP &&
1866 (isOperandLiteral (IC_RIGHT (ic)) ||
1867 getSize (operandType (IC_RESULT (ic))) > 1))
1870 /* has only one definition */
1871 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1874 /* has only one use */
1875 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1878 /* and the usage immediately follows this iCode */
1879 if (!(uic = hTabItemWithKey (iCodehTab,
1880 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1883 if (ic->next != uic)
1886 /* if it is a conditional branch then we definitely can */
1890 if (uic->op == JUMPTABLE)
1894 /* if the usage is not is an assignment or an
1895 arithmetic / bitwise / shift operation then not */
1896 if (POINTER_SET (uic) &&
1897 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1901 if (uic->op != '=' &&
1902 !IS_ARITHMETIC_OP (uic) &&
1903 !IS_BITWISE_OP (uic) &&
1904 uic->op != LEFT_OP &&
1905 uic->op != RIGHT_OP)
1908 /* if used in ^ operation then make sure right is not a
1910 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1913 /* if shift operation make sure right side is not a literal */
1914 if (uic->op == RIGHT_OP &&
1915 (isOperandLiteral (IC_RIGHT (uic)) ||
1916 getSize (operandType (IC_RESULT (uic))) > 1))
1919 if (uic->op == LEFT_OP &&
1920 (isOperandLiteral (IC_RIGHT (uic)) ||
1921 getSize (operandType (IC_RESULT (uic))) > 1))
1925 /* make sure that the result of this icode is not on the
1926 stack, since acc is used to compute stack offset */
1927 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1928 OP_SYMBOL (IC_RESULT (uic))->onStack)
1933 /* if either one of them in far space then we cannot */
1934 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1935 isOperandInFarSpace (IC_LEFT (uic))) ||
1936 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1937 isOperandInFarSpace (IC_RIGHT (uic))))
1941 /* if the usage has only one operand then we can */
1942 if (IC_LEFT (uic) == NULL ||
1943 IC_RIGHT (uic) == NULL)
1946 /* make sure this is on the left side if not
1947 a '+' since '+' is commutative */
1948 if (ic->op != '+' &&
1949 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1952 /* if one of them is a literal then we can */
1953 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1954 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1960 /** This is confusing :) Guess for now */
1961 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1962 (IS_ITEMP (IC_RIGHT (uic)) ||
1963 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
1966 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
1967 (IS_ITEMP (IC_LEFT (uic)) ||
1968 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
1972 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
1977 packRegsForHLUse (iCode * ic)
1984 /* has only one definition */
1985 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1988 /* has only one use */
1989 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1992 /* and the usage immediately follows this iCode */
1993 if (!(uic = hTabItemWithKey (iCodehTab,
1994 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1997 if (ic->next != uic)
2000 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2002 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2006 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2010 opPreservesA (iCode * ic, iCode * uic)
2012 /* if it is a conditional branch then we definitely can */
2016 if (uic->op == JUMPTABLE)
2019 /* if the usage has only one operand then we can */
2020 /* PENDING: check */
2021 if (IC_LEFT (uic) == NULL ||
2022 IC_RIGHT (uic) == NULL)
2025 /* PENDING: check this rule */
2026 if (getSize (operandType (IC_RESULT (uic))) > 1)
2033 !IS_ARITHMETIC_OP(uic) (sub requires A)
2037 !IS_BITWISE_OP (uic) &&
2040 !POINTER_GET (uic) &&
2042 uic->op != LEFT_OP &&
2043 uic->op != RIGHT_OP && */
2051 if (!IC_LEFT (uic) || !IC_RESULT (ic))
2054 /** This is confusing :) Guess for now */
2055 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2056 (IS_ITEMP (IC_RIGHT (uic)) ||
2057 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2060 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2061 (IS_ITEMP (IC_LEFT (uic)) ||
2062 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2069 joinPushes (iCode * ic)
2072 if (ic->op == IPUSH &&
2073 isOperandLiteral (IC_LEFT (ic)) &&
2074 getSize (operandType (IC_LEFT (ic))) == 1 &&
2075 ic->next->op == IPUSH &&
2076 isOperandLiteral (IC_LEFT (ic->next)) &&
2077 getSize (operandType (IC_LEFT (ic->next))) == 1)
2079 /* This is a bit tricky as michaelh doesnt know what he's doing.
2081 /* First upgrade the size of (first) to int */
2082 SPEC_NOUN (operandType (IC_LEFT (ic))) = V_INT;
2084 floatFromVal (AOP /* need some sleep ... */ );
2085 /* Now get and join the values */
2086 value *val = aop->aopu.aop_lit;
2087 /* if it is a float then it gets tricky */
2088 /* otherwise it is fairly simple */
2089 if (!IS_FLOAT (val->type))
2091 unsigned long v = floatFromVal (val);
2093 floatFrom ( /* need some sleep ... */ );
2094 printf ("Size %u\n", getSize (operandType (IC_LEFT (ic))));
2095 ic->next = ic->next->next;
2101 /** Pack registers for acc use.
2102 When the result of this operation is small and short lived it may
2103 be able to be stored in the accumulator.
2105 Note that the 'A preserving' list is currently emperical :)e
2108 packRegsForAccUse2 (iCode * ic)
2112 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2114 /* Filter out all but those 'good' commands */
2116 !POINTER_GET (ic) &&
2118 !IS_BITWISE_OP (ic) &&
2125 /* if + or - then it has to be one byte result.
2128 if ((ic->op == '+' || ic->op == '-')
2129 && getSize (operandType (IC_RESULT (ic))) > 1)
2132 /* if shift operation make sure right side is not a literal.
2136 if (ic->op == RIGHT_OP &&
2137 (isOperandLiteral (IC_RIGHT (ic)) ||
2138 getSize (operandType (IC_RESULT (ic))) > 1))
2141 if (ic->op == LEFT_OP &&
2142 (isOperandLiteral (IC_RIGHT (ic)) ||
2143 getSize (operandType (IC_RESULT (ic))) > 1))
2147 /* has only one definition */
2148 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2153 /* Right. We may be able to propagate it through if:
2154 For each in the chain of uses the intermediate is OK.
2156 /* Get next with 'uses result' bit on
2157 If this->next == next
2158 Validate use of next
2159 If OK, increase count
2161 /* and the usage immediately follows this iCode */
2162 if (!(uic = hTabItemWithKey (iCodehTab,
2163 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2169 /* Create a copy of the OP_USES bit vect */
2170 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2172 iCode *scan = ic, *next;
2176 setBit = bitVectFirstBit (uses);
2177 next = hTabItemWithKey (iCodehTab, setBit);
2178 if (scan->next == next)
2180 bitVectUnSetBit (uses, setBit);
2181 /* Still contigous. */
2182 if (!opPreservesA (ic, next))
2193 while (!bitVectIsZero (uses));
2194 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2198 /* OLD CODE FOLLOWS */
2199 /* if it is a conditional branch then we definitely can
2207 if (uic->op == JUMPTABLE)
2211 /* if the usage is not is an assignment or an
2212 arithmetic / bitwise / shift operation then not.
2213 MLH: Pending: Invalid. Our pointer sets are always peechy.
2216 if (POINTER_SET (uic) &&
2217 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2219 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2225 if (uic->op != '=' &&
2226 !IS_ARITHMETIC_OP (uic) &&
2227 !IS_BITWISE_OP (uic) &&
2228 uic->op != LEFT_OP &&
2229 uic->op != RIGHT_OP)
2235 /* if used in ^ operation then make sure right is not a
2237 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2240 /* if shift operation make sure right side is not a literal */
2241 if (uic->op == RIGHT_OP &&
2242 (isOperandLiteral (IC_RIGHT (uic)) ||
2243 getSize (operandType (IC_RESULT (uic))) > 1))
2246 if (uic->op == LEFT_OP &&
2247 (isOperandLiteral (IC_RIGHT (uic)) ||
2248 getSize (operandType (IC_RESULT (uic))) > 1))
2252 /* make sure that the result of this icode is not on the
2253 stack, since acc is used to compute stack offset */
2254 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2255 OP_SYMBOL (IC_RESULT (uic))->onStack)
2260 /* if either one of them in far space then we cannot */
2261 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2262 isOperandInFarSpace (IC_LEFT (uic))) ||
2263 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2264 isOperandInFarSpace (IC_RIGHT (uic))))
2268 /* if the usage has only one operand then we can */
2269 if (IC_LEFT (uic) == NULL ||
2270 IC_RIGHT (uic) == NULL)
2273 /* make sure this is on the left side if not
2274 a '+' since '+' is commutative */
2275 if (ic->op != '+' &&
2276 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2279 /* if one of them is a literal then we can */
2280 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2281 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2287 /** This is confusing :) Guess for now */
2288 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2289 (IS_ITEMP (IC_RIGHT (uic)) ||
2290 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2293 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2294 (IS_ITEMP (IC_LEFT (uic)) ||
2295 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2299 printf ("acc ok!\n");
2300 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2303 /** Does some transformations to reduce register pressure.
2306 packRegisters (eBBlock * ebp)
2311 D (D_ALLOC, ("packRegisters: entered.\n"));
2313 while (1 && !DISABLE_PACK_ASSIGN)
2316 /* look for assignments of the form */
2317 /* iTempNN = TRueSym (someoperation) SomeOperand */
2319 /* TrueSym := iTempNN:1 */
2320 for (ic = ebp->sch; ic; ic = ic->next)
2322 /* find assignment of the form TrueSym := iTempNN:1 */
2323 if (ic->op == '=' && !POINTER_SET (ic))
2324 change += packRegsForAssign (ic, ebp);
2330 for (ic = ebp->sch; ic; ic = ic->next)
2332 /* Safe: address of a true sym is always constant. */
2333 /* if this is an itemp & result of a address of a true sym
2334 then mark this as rematerialisable */
2335 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2337 if (ic->op == ADDRESS_OF &&
2338 IS_ITEMP (IC_RESULT (ic)) &&
2339 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2340 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2341 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2344 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2345 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2346 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2349 /* Safe: just propagates the remat flag */
2350 /* if straight assignment then carry remat flag if this is the
2352 if (ic->op == '=' &&
2353 !POINTER_SET (ic) &&
2354 IS_SYMOP (IC_RIGHT (ic)) &&
2355 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2356 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2359 OP_SYMBOL (IC_RESULT (ic))->remat =
2360 OP_SYMBOL (IC_RIGHT (ic))->remat;
2361 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2362 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2365 /* if the condition of an if instruction is defined in the
2366 previous instruction then mark the itemp as a conditional */
2367 if ((IS_CONDITIONAL (ic) ||
2368 ((ic->op == BITWISEAND ||
2371 isBitwiseOptimizable (ic))) &&
2372 ic->next && ic->next->op == IFX &&
2373 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2374 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2377 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2382 /* reduce for support function calls */
2383 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2384 packRegsForSupport (ic, ebp);
2388 /* some cases the redundant moves can
2389 can be eliminated for return statements */
2390 if ((ic->op == RETURN || ic->op == SEND) &&
2391 !isOperandInFarSpace (IC_LEFT (ic)) &&
2393 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2395 /* if pointer set & left has a size more than
2396 one and right is not in far space */
2397 if (POINTER_SET (ic) &&
2398 /* MLH: no such thing.
2399 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2400 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2401 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2402 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2405 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2408 /* if pointer get */
2409 if (!DISABLE_PACK_ONE_USE &&
2411 /* MLH: dont have far space
2412 !isOperandInFarSpace(IC_RESULT(ic))&& */
2413 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2414 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2415 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2418 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2420 /* pack registers for accumulator use, when the result of an
2421 arithmetic or bit wise operation has only one use, that use is
2422 immediately following the defintion and the using iCode has
2423 only one operand or has two operands but one is literal & the
2424 result of that operation is not on stack then we can leave the
2425 result of this operation in acc:b combination */
2427 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2429 packRegsForHLUse (ic);
2432 if ((IS_ARITHMETIC_OP (ic)
2433 || IS_BITWISE_OP (ic)
2434 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2436 IS_ITEMP (IC_RESULT (ic)) &&
2437 getSize (operandType (IC_RESULT (ic))) <= 2)
2438 packRegsForAccUse (ic);
2440 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2441 getSize (operandType (IC_RESULT (ic))) == 1)
2443 packRegsForAccUse2 (ic);
2450 /*-----------------------------------------------------------------*/
2451 /* assignRegisters - assigns registers to each live range as need */
2452 /*-----------------------------------------------------------------*/
2454 z80_assignRegisters (eBBlock ** ebbs, int count)
2459 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2461 setToNull ((void *) &funcrUsed);
2462 stackExtend = dataExtend = 0;
2466 /* DE is required for the code gen. */
2467 _nRegs = GBZ80_MAX_REGS;
2468 regsZ80 = _gbz80_regs;
2472 _nRegs = Z80_MAX_REGS;
2473 regsZ80 = _z80_regs;
2476 /* change assignments this will remove some
2477 live ranges reducing some register pressure */
2478 for (i = 0; i < count; i++)
2479 packRegisters (ebbs[i]);
2481 if (options.dump_pack)
2482 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2484 /* first determine for each live range the number of
2485 registers & the type of registers required for each */
2488 /* and serially allocate registers */
2489 serialRegAssign (ebbs, count);
2491 /* if stack was extended then tell the user */
2494 /* werror(W_TOOMANY_SPILS,"stack", */
2495 /* stackExtend,currFunc->name,""); */
2501 /* werror(W_TOOMANY_SPILS,"data space", */
2502 /* dataExtend,currFunc->name,""); */
2506 if (options.dump_rassgn)
2507 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2509 /* after that create the register mask
2510 for each of the instruction */
2511 createRegMask (ebbs, count);
2513 /* now get back the chain */
2514 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2516 /* redo that offsets for stacked automatic variables */
2517 redoStackOffsets ();
2521 /* free up any stackSpil locations allocated */
2522 applyToSet (stackSpil, deallocStackSpil);
2524 setToNull ((void **) &stackSpil);
2525 setToNull ((void **) &spiltSet);
2526 /* mark all registers as free */