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 #if DEBUG_FAKE_EXTRA_REGS
109 {REG_GPR, M_IDX, "m", 1},
110 {REG_GPR, N_IDX, "n", 1},
111 {REG_GPR, O_IDX, "o", 1},
112 {REG_GPR, P_IDX, "p", 1},
113 {REG_GPR, Q_IDX, "q", 1},
114 {REG_GPR, R_IDX, "r", 1},
115 {REG_GPR, S_IDX, "s", 1},
116 {REG_GPR, T_IDX, "t", 1},
118 {REG_CND, CND_IDX, "c", 1}
123 /** Number of usable registers (all but C) */
124 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
125 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
127 static void spillThis (symbol *);
129 /** Allocates register of given type.
130 'type' is not used on the z80 version. It was used to select
131 between pointer and general purpose registers on the mcs51 version.
133 @return Pointer to the newly allocated register.
136 allocReg (short type)
140 for (i = 0; i < _nRegs; i++)
142 /* For now we allocate from any free */
143 if (regsZ80[i].isFree)
145 regsZ80[i].isFree = 0;
148 bitVectSetBit (currFunc->regsUsed, i);
149 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
153 D (D_ALLOC, ("allocReg: No free.\n"));
157 /** Returns pointer to register wit index number
164 for (i = 0; i < _nRegs; i++)
165 if (regsZ80[i].rIdx == idx)
168 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
169 "regWithIdx not found");
173 /** Frees a register.
178 wassert (!reg->isFree);
180 D (D_ALLOC, ("freeReg: freed %p\n", reg));
184 /** Returns number of free registers.
192 for (i = 0; i < _nRegs; i++)
194 /* For now only one reg type */
195 if (regsZ80[i].isFree)
201 /** Free registers with type.
204 nfreeRegsType (int type)
209 if ((nfr = nFreeRegs (type)) == 0)
210 return nFreeRegs (REG_GPR);
213 return nFreeRegs (type);
218 /*-----------------------------------------------------------------*/
219 /* allDefsOutOfRange - all definitions are out of a range */
220 /*-----------------------------------------------------------------*/
222 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
229 for (i = 0; i < defs->size; i++)
233 if (bitVectBitValue (defs, i) &&
234 (ic = hTabItemWithKey (iCodehTab, i)) &&
235 (ic->seq >= fseq && ic->seq <= toseq))
245 /*-----------------------------------------------------------------*/
246 /* computeSpillable - given a point find the spillable live ranges */
247 /*-----------------------------------------------------------------*/
249 computeSpillable (iCode * ic)
253 /* spillable live ranges are those that are live at this
254 point . the following categories need to be subtracted
256 a) - those that are already spilt
257 b) - if being used by this one
258 c) - defined by this one */
260 spillable = bitVectCopy (ic->rlive);
262 bitVectCplAnd (spillable, spiltSet); /* those already spilt */
264 bitVectCplAnd (spillable, ic->uses); /* used in this one */
265 bitVectUnSetBit (spillable, ic->defKey);
266 spillable = bitVectIntersect (spillable, regAssigned);
271 /*-----------------------------------------------------------------*/
272 /* noSpilLoc - return true if a variable has no spil location */
273 /*-----------------------------------------------------------------*/
275 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
277 return (sym->usl.spillLoc ? 0 : 1);
280 /*-----------------------------------------------------------------*/
281 /* hasSpilLoc - will return 1 if the symbol has spil location */
282 /*-----------------------------------------------------------------*/
284 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
286 return (sym->usl.spillLoc ? 1 : 0);
289 /** Will return 1 if the remat flag is set.
290 A symbol is rematerialisable if it doesnt need to be allocated
291 into registers at creation as it can be re-created at any time -
292 i.e. it's constant in some way.
295 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
300 /*-----------------------------------------------------------------*/
301 /* allLRs - return true for all */
302 /*-----------------------------------------------------------------*/
304 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
309 /*-----------------------------------------------------------------*/
310 /* liveRangesWith - applies function to a given set of live range */
311 /*-----------------------------------------------------------------*/
313 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
314 eBBlock * ebp, iCode * ic)
319 if (!lrs || !lrs->size)
322 for (i = 1; i < lrs->size; i++)
325 if (!bitVectBitValue (lrs, i))
328 /* if we don't find it in the live range
329 hash table we are in serious trouble */
330 if (!(sym = hTabItemWithKey (liveRanges, i)))
332 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
333 "liveRangesWith could not find liveRange");
337 if (func (sym, ebp, ic) && bitVectBitValue (regAssigned, sym->key))
338 addSetHead (&rset, sym);
345 /*-----------------------------------------------------------------*/
346 /* leastUsedLR - given a set determines which is the least used */
347 /*-----------------------------------------------------------------*/
349 leastUsedLR (set * sset)
351 symbol *sym = NULL, *lsym = NULL;
353 sym = lsym = setFirstItem (sset);
358 for (; lsym; lsym = setNextItem (sset))
361 /* if usage is the same then prefer
362 the spill the smaller of the two */
363 if (lsym->used == sym->used)
364 if (getSize (lsym->type) < getSize (sym->type))
368 if (lsym->used < sym->used)
373 setToNull ((void **) &sset);
378 /*-----------------------------------------------------------------*/
379 /* noOverLap - will iterate through the list looking for over lap */
380 /*-----------------------------------------------------------------*/
382 noOverLap (set * itmpStack, symbol * fsym)
386 for (sym = setFirstItem (itmpStack); sym;
387 sym = setNextItem (itmpStack))
389 if (sym->liveTo > fsym->liveFrom)
396 /*-----------------------------------------------------------------*/
397 /* isFree - will return 1 if the a free spil location is found */
398 /*-----------------------------------------------------------------*/
402 V_ARG (symbol **, sloc);
403 V_ARG (symbol *, fsym);
405 /* if already found */
409 /* if it is free && and the itmp assigned to
410 this does not have any overlapping live ranges
411 with the one currently being assigned and
412 the size can be accomodated */
414 noOverLap (sym->usl.itmpStack, fsym) &&
415 getSize (sym->type) >= getSize (fsym->type))
424 /*-----------------------------------------------------------------*/
425 /* createStackSpil - create a location on the stack to spil */
426 /*-----------------------------------------------------------------*/
428 createStackSpil (symbol * sym)
432 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
434 /* first go try and find a free one that is already
435 existing on the stack */
436 if (applyToSet (stackSpil, isFree, &sloc, sym))
438 /* found a free one : just update & return */
439 sym->usl.spillLoc = sloc;
442 addSetHead (&sloc->usl.itmpStack, sym);
443 D (D_ALLOC, ("createStackSpil: found existing\n"));
447 /* could not then have to create one , this is the hard part
448 we need to allocate this on the stack : this is really a
449 hack!! but cannot think of anything better at this time */
451 sprintf (buffer, "sloc%d", slocNum++);
452 sloc = newiTemp (buffer);
454 /* set the type to the spilling symbol */
455 sloc->type = copyLinkChain (sym->type);
456 sloc->etype = getSpec (sloc->type);
457 SPEC_SCLS (sloc->etype) = S_AUTO;
459 /* we don't allow it to be allocated`
460 onto the external stack since : so we
461 temporarily turn it off ; we also
462 turn off memory model to prevent
463 the spil from going to the external storage
464 and turn off overlaying
468 sloc->isref = 1; /* to prevent compiler warning */
470 /* if it is on the stack then update the stack */
471 if (IN_STACK (sloc->etype))
473 currFunc->stack += getSize (sloc->type);
474 stackExtend += getSize (sloc->type);
477 dataExtend += getSize (sloc->type);
479 /* add it to the stackSpil set */
480 addSetHead (&stackSpil, sloc);
481 sym->usl.spillLoc = sloc;
484 /* add it to the set of itempStack set
485 of the spill location */
486 addSetHead (&sloc->usl.itmpStack, sym);
488 D (D_ALLOC, ("createStackSpil: created new\n"));
492 /*-----------------------------------------------------------------*/
493 /* isSpiltOnStack - returns true if the spil location is on stack */
494 /*-----------------------------------------------------------------*/
496 isSpiltOnStack (symbol * sym)
506 /* if (sym->stackSpil) */
509 if (!sym->usl.spillLoc)
512 etype = getSpec (sym->usl.spillLoc->type);
513 if (IN_STACK (etype))
519 /*-----------------------------------------------------------------*/
520 /* spillThis - spils a specific operand */
521 /*-----------------------------------------------------------------*/
523 spillThis (symbol * sym)
527 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
529 /* if this is rematerializable or has a spillLocation
530 we are okay, else we need to create a spillLocation
532 if (!(sym->remat || sym->usl.spillLoc))
533 createStackSpil (sym);
535 /* mark it has spilt & put it in the spilt set */
537 spiltSet = bitVectSetBit (spiltSet, sym->key);
539 bitVectUnSetBit (regAssigned, sym->key);
541 for (i = 0; i < sym->nRegs; i++)
545 freeReg (sym->regs[i]);
550 /* if spilt on stack then free up r0 & r1
551 if they could have been assigned to some
553 if (sym->usl.spillLoc && !sym->remat)
554 sym->usl.spillLoc->allocreq = 1;
558 /** Select a iTemp to spil : rather a simple procedure.
561 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
563 bitVect *lrcs = NULL;
567 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
568 /* get the spillable live ranges */
569 lrcs = computeSpillable (ic);
571 /* get all live ranges that are rematerizable */
572 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
574 D (D_ALLOC, ("selectSpil: using remat.\n"));
575 /* return the least used of these */
576 return leastUsedLR (selectS);
580 /* get live ranges with spillLocations in direct space */
581 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
583 sym = leastUsedLR (selectS);
584 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
585 sym->usl.spillLoc->rname :
586 sym->usl.spillLoc->name));
588 /* mark it as allocation required */
589 sym->usl.spillLoc->allocreq = 1;
593 /* if the symbol is local to the block then */
594 if (forSym->liveTo < ebp->lSeq)
597 /* check if there are any live ranges allocated
598 to registers that are not used in this block */
599 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
601 sym = leastUsedLR (selectS);
602 /* if this is not rematerializable */
611 /* check if there are any live ranges that not
612 used in the remainder of the block */
613 if (!blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
615 sym = leastUsedLR (selectS);
627 /* find live ranges with spillocation && not used as pointers */
628 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
631 sym = leastUsedLR (selectS);
632 /* mark this as allocation required */
633 sym->usl.spillLoc->allocreq = 1;
638 /* find live ranges with spillocation */
639 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
641 D (D_ALLOC, ("selectSpil: using with spill.\n"));
642 sym = leastUsedLR (selectS);
643 sym->usl.spillLoc->allocreq = 1;
647 /* couldn't find then we need to create a spil
648 location on the stack , for which one? the least
650 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
652 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
653 /* return a created spil location */
654 sym = createStackSpil (leastUsedLR (selectS));
655 sym->usl.spillLoc->allocreq = 1;
659 /* this is an extreme situation we will spill
660 this one : happens very rarely but it does happen */
661 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
667 /** Spil some variable & mark registers as free.
668 A spill occurs when an iTemp wont fit into the available registers.
671 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
676 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
678 /* get something we can spil */
679 ssym = selectSpil (ic, ebp, forSym);
681 /* mark it as spilt */
683 spiltSet = bitVectSetBit (spiltSet, ssym->key);
685 /* mark it as not register assigned &
686 take it away from the set */
687 bitVectUnSetBit (regAssigned, ssym->key);
689 /* mark the registers as free */
690 for (i = 0; i < ssym->nRegs; i++)
692 freeReg (ssym->regs[i]);
694 /* if spilt on stack then free up r0 & r1
695 if they could have been assigned to as gprs */
696 if (!ptrRegReq && isSpiltOnStack (ssym))
699 spillLRWithPtrReg (ssym);
702 /* if this was a block level spil then insert push & pop
703 at the start & end of block respectively */
706 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
707 /* add push to the start of the block */
708 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
709 ebp->sch->next : ebp->sch));
710 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
711 /* add pop to the end of the block */
712 addiCodeToeBBlock (ebp, nic, NULL);
715 /* if spilt because not used in the remainder of the
716 block then add a push before this instruction and
717 a pop at the end of the block */
718 if (ssym->remainSpil)
721 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
722 /* add push just before this instruction */
723 addiCodeToeBBlock (ebp, nic, ic);
725 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
726 /* add pop to the end of the block */
727 addiCodeToeBBlock (ebp, nic, NULL);
731 D (D_ALLOC, ("spilSomething: done.\n"));
739 /** Will try for GPR if not spil.
742 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
746 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
748 /* try for gpr type */
749 if ((reg = allocReg (REG_GPR)))
751 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
755 /* we have to spil */
756 if (!spilSomething (ic, ebp, sym))
758 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
762 /* this looks like an infinite loop but
763 in really selectSpil will abort */
767 /** Symbol has a given register.
770 symHasReg (symbol * sym, regs * reg)
774 for (i = 0; i < sym->nRegs; i++)
775 if (sym->regs[i] == reg)
781 /** Check the live to and if they have registers & are not spilt then
782 free up the registers
785 deassignLRs (iCode * ic, eBBlock * ebp)
791 for (sym = hTabFirstItem (liveRanges, &k); sym;
792 sym = hTabNextItem (liveRanges, &k))
796 /* if it does not end here */
797 if (sym->liveTo > ic->seq)
800 /* if it was spilt on stack then we can
801 mark the stack spil location as free */
806 sym->usl.spillLoc->isFree = 1;
812 if (!bitVectBitValue (regAssigned, sym->key))
815 /* special case check if this is an IFX &
816 the privious one was a pop and the
817 previous one was not spilt then keep track
819 if (ic->op == IFX && ic->prev &&
820 ic->prev->op == IPOP &&
821 !ic->prev->parmPush &&
822 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
823 psym = OP_SYMBOL (IC_LEFT (ic->prev));
825 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
831 bitVectUnSetBit (regAssigned, sym->key);
833 /* if the result of this one needs registers
834 and does not have it then assign it right
836 if (IC_RESULT (ic) &&
837 !(SKIP_IC2 (ic) || /* not a special icode */
838 ic->op == JUMPTABLE ||
843 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
844 result->liveTo > ic->seq && /* and will live beyond this */
845 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
846 result->regType == sym->regType && /* same register types */
847 result->nRegs && /* which needs registers */
848 !result->isspilt && /* and does not already have them */
850 !bitVectBitValue (regAssigned, result->key) &&
851 /* the number of free regs + number of regs in this LR
852 can accomodate the what result Needs */
853 ((nfreeRegsType (result->regType) +
854 sym->nRegs) >= result->nRegs)
857 for (i = 0; i < result->nRegs; i++)
860 result->regs[i] = sym->regs[i];
862 result->regs[i] = getRegGpr (ic, ebp, result);
864 /* if the allocation falied which means
865 this was spilt then break */
866 if (!result->regs[i])
874 regAssigned = bitVectSetBit (regAssigned, result->key);
877 /* free the remaining */
878 for (; i < sym->nRegs; i++)
882 if (!symHasReg (psym, sym->regs[i]))
883 freeReg (sym->regs[i]);
886 freeReg (sym->regs[i]);
887 // sym->regs[i] = NULL;
894 /** Reassign this to registers.
897 reassignLR (operand * op)
899 symbol *sym = OP_SYMBOL (op);
902 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
904 /* not spilt any more */
905 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
906 bitVectUnSetBit (spiltSet, sym->key);
908 regAssigned = bitVectSetBit (regAssigned, sym->key);
912 for (i = 0; i < sym->nRegs; i++)
913 sym->regs[i]->isFree = 0;
916 /** Determines if allocating will cause a spill.
919 willCauseSpill (int nr, int rt)
921 /* first check if there are any avlb registers
922 of te type required */
923 if (nFreeRegs (0) >= nr)
926 /* it will cause a spil */
930 /** The allocator can allocate same registers to result and operand,
931 if this happens make sure they are in the same position as the operand
932 otherwise chaos results.
935 positionRegs (symbol * result, symbol * opsym, int lineno)
937 int count = min (result->nRegs, opsym->nRegs);
938 int i, j = 0, shared = 0;
940 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
942 /* if the result has been spilt then cannot share */
947 /* first make sure that they actually share */
948 for (i = 0; i < count; i++)
950 for (j = 0; j < count; j++)
952 if (result->regs[i] == opsym->regs[j] && i != j)
962 regs *tmp = result->regs[i];
963 result->regs[i] = result->regs[j];
964 result->regs[j] = tmp;
969 /** Try to allocate a pair of registers to the symbol.
972 tryAllocatingRegPair (symbol * sym)
975 wassert (sym->nRegs == 2);
976 for (i = 0; i < _nRegs; i += 2)
978 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
980 regsZ80[i].isFree = 0;
981 sym->regs[0] = ®sZ80[i];
982 regsZ80[i + 1].isFree = 0;
983 sym->regs[1] = ®sZ80[i + 1];
987 bitVectSetBit (currFunc->regsUsed, i);
989 bitVectSetBit (currFunc->regsUsed, i + 1);
991 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
995 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
999 /** Serially allocate registers to the variables.
1000 This is the main register allocation function. It is called after
1004 serialRegAssign (eBBlock ** ebbs, int count)
1008 /* for all blocks */
1009 for (i = 0; i < count; i++)
1014 if (ebbs[i]->noPath &&
1015 (ebbs[i]->entryLabel != entryLabel &&
1016 ebbs[i]->entryLabel != returnLabel))
1019 /* of all instructions do */
1020 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1023 /* if this is an ipop that means some live
1024 range will have to be assigned again */
1028 reassignLR (IC_LEFT (ic));
1031 /* if result is present && is a true symbol */
1032 if (IC_RESULT (ic) && ic->op != IFX &&
1033 IS_TRUE_SYMOP (IC_RESULT (ic)))
1034 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1036 /* take away registers from live
1037 ranges that end at this instruction */
1038 deassignLRs (ic, ebbs[i]);
1040 /* some don't need registers */
1041 /* MLH: removed RESULT and POINTER_SET condition */
1042 if (SKIP_IC2 (ic) ||
1043 ic->op == JUMPTABLE ||
1049 /* now we need to allocate registers only for the result */
1052 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1057 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1059 /* if it does not need or is spilt
1060 or is already assigned to registers
1061 or will not live beyond this instructions */
1064 bitVectBitValue (regAssigned, sym->key) ||
1065 sym->liveTo <= ic->seq)
1067 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1071 /* if some liverange has been spilt at the block level
1072 and this one live beyond this block then spil this
1074 if (blockSpil && sym->liveTo > ebbs[i]->lSeq)
1076 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1080 /* if trying to allocate this will cause
1081 a spill and there is nothing to spill
1082 or this one is rematerializable then
1084 willCS = willCauseSpill (sym->nRegs, sym->regType);
1085 spillable = computeSpillable (ic);
1087 (willCS && bitVectIsZero (spillable)))
1090 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1096 /* if it has a spillocation & is used less than
1097 all other live ranges then spill this */
1099 if (sym->usl.spillLoc) {
1100 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1101 allLRs, ebbs[i], ic));
1102 if (leastUsed && leastUsed->used > sym->used) {
1107 /* if none of the liveRanges have a spillLocation then better
1108 to spill this one than anything else already assigned to registers */
1109 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1116 /* else we assign registers to it */
1117 regAssigned = bitVectSetBit (regAssigned, sym->key);
1119 /* Special case: Try to fit into a reg pair if
1121 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1122 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1127 for (j = 0; j < sym->nRegs; j++)
1129 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1131 /* if the allocation falied which means
1132 this was spilt then break */
1135 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1140 /* if it shares registers with operands make sure
1141 that they are in the same position */
1142 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1143 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1144 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1145 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1146 /* do the same for the right operand */
1147 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1148 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1149 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1150 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1157 /*-----------------------------------------------------------------*/
1158 /* rUmaskForOp :- returns register mask for an operand */
1159 /*-----------------------------------------------------------------*/
1161 rUmaskForOp (operand * op)
1167 /* only temporaries are assigned registers */
1171 sym = OP_SYMBOL (op);
1173 /* if spilt or no registers assigned to it
1175 if (sym->isspilt || !sym->nRegs)
1178 rumask = newBitVect (_nRegs);
1180 for (j = 0; j < sym->nRegs; j++)
1182 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1189 z80_rUmaskForOp (operand * op)
1191 return rUmaskForOp (op);
1194 /** Returns bit vector of registers used in iCode.
1197 regsUsedIniCode (iCode * ic)
1199 bitVect *rmask = newBitVect (_nRegs);
1201 /* do the special cases first */
1204 rmask = bitVectUnion (rmask,
1205 rUmaskForOp (IC_COND (ic)));
1209 /* for the jumptable */
1210 if (ic->op == JUMPTABLE)
1212 rmask = bitVectUnion (rmask,
1213 rUmaskForOp (IC_JTCOND (ic)));
1218 /* of all other cases */
1220 rmask = bitVectUnion (rmask,
1221 rUmaskForOp (IC_LEFT (ic)));
1225 rmask = bitVectUnion (rmask,
1226 rUmaskForOp (IC_RIGHT (ic)));
1229 rmask = bitVectUnion (rmask,
1230 rUmaskForOp (IC_RESULT (ic)));
1236 /** For each instruction will determine the regsUsed.
1239 createRegMask (eBBlock ** ebbs, int count)
1243 /* for all blocks */
1244 for (i = 0; i < count; i++)
1248 if (ebbs[i]->noPath &&
1249 (ebbs[i]->entryLabel != entryLabel &&
1250 ebbs[i]->entryLabel != returnLabel))
1253 /* for all instructions */
1254 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1259 if (SKIP_IC2 (ic) || !ic->rlive)
1262 /* first mark the registers used in this
1264 ic->rUsed = regsUsedIniCode (ic);
1265 funcrUsed = bitVectUnion (funcrUsed, ic->rUsed);
1267 /* now create the register mask for those
1268 registers that are in use : this is a
1269 super set of ic->rUsed */
1270 ic->rMask = newBitVect (_nRegs + 1);
1272 /* for all live Ranges alive at this point */
1273 for (j = 1; j < ic->rlive->size; j++)
1278 /* if not alive then continue */
1279 if (!bitVectBitValue (ic->rlive, j))
1282 /* find the live range we are interested in */
1283 if (!(sym = hTabItemWithKey (liveRanges, j)))
1285 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1286 "createRegMask cannot find live range");
1290 /* if no register assigned to it */
1291 if (!sym->nRegs || sym->isspilt)
1294 /* for all the registers allocated to it */
1295 for (k = 0; k < sym->nRegs; k++)
1298 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1304 /** Returns the rematerialized string for a remat var.
1307 rematStr (symbol * sym)
1310 iCode *ic = sym->rematiCode;
1315 /* if plus or minus print the right hand side */
1316 if (ic->op == '+' || ic->op == '-')
1318 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1321 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1324 /* we reached the end */
1325 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1332 /*-----------------------------------------------------------------*/
1333 /* regTypeNum - computes the type & number of registers required */
1334 /*-----------------------------------------------------------------*/
1341 /* for each live range do */
1342 for (sym = hTabFirstItem (liveRanges, &k); sym;
1343 sym = hTabNextItem (liveRanges, &k))
1346 /* if used zero times then no registers needed */
1347 if ((sym->liveTo - sym->liveFrom) == 0)
1350 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1352 /* if the live range is a temporary */
1356 /* if the type is marked as a conditional */
1357 if (sym->regType == REG_CND)
1360 /* if used in return only then we don't
1362 if (sym->ruonly || sym->accuse)
1364 if (IS_AGGREGATE (sym->type) || sym->isptr)
1365 sym->type = aggrToPtr (sym->type, FALSE);
1369 /* if not then we require registers */
1370 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1371 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1372 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1373 getSize (sym->type));
1374 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1376 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1380 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1381 printTypeChain (sym->type, stderr);
1382 fprintf (stderr, "\n");
1385 /* determine the type of register required */
1386 /* Always general purpose */
1387 sym->regType = REG_GPR;
1392 /* for the first run we don't provide */
1393 /* registers for true symbols we will */
1394 /* see how things go */
1395 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1402 /** Mark all registers as free.
1409 D (D_ALLOC, ("freeAllRegs: running.\n"));
1411 for (i = 0; i < _nRegs; i++)
1412 regsZ80[i].isFree = 1;
1415 /*-----------------------------------------------------------------*/
1416 /* deallocStackSpil - this will set the stack pointer back */
1417 /*-----------------------------------------------------------------*/
1418 DEFSETFUNC (deallocStackSpil)
1426 /** Register reduction for assignment.
1429 packRegsForAssign (iCode * ic, eBBlock * ebp)
1433 D (D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1436 /* !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1437 !IS_ITEMP (IC_RIGHT (ic)) ||
1438 OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1439 OP_SYMBOL (IC_RIGHT (ic))->isind)
1443 /* if the true symbol is defined in far space or on stack
1444 then we should not since this will increase register pressure */
1445 if (isOperandInFarSpace (IC_RESULT (ic)))
1447 if ((dic = farSpacePackable (ic)))
1454 /* find the definition of iTempNN scanning backwards if we find a
1455 a use of the true symbol in before we find the definition then
1457 for (dic = ic->prev; dic; dic = dic->prev)
1459 /* if there is a function call and this is
1460 a parameter & not my parameter then don't pack it */
1461 if ((dic->op == CALL || dic->op == PCALL) &&
1462 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1463 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1472 if (IS_SYMOP (IC_RESULT (dic)) &&
1473 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1478 if (IS_SYMOP (IC_RIGHT (dic)) &&
1479 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1480 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1486 if (IS_SYMOP (IC_LEFT (dic)) &&
1487 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1488 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1494 if (POINTER_SET (dic) &&
1495 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1504 return 0; /* did not find */
1506 /* if the result is on stack or iaccess then it must be
1507 the same atleast one of the operands */
1508 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1509 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1512 /* the operation has only one symbol
1513 operator then we can pack */
1514 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1515 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1518 if (!((IC_LEFT (dic) &&
1519 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1521 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1525 /* found the definition */
1526 /* replace the result with the result of */
1527 /* this assignment and remove this assignment */
1528 IC_RESULT (dic) = IC_RESULT (ic);
1530 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1532 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1534 /* delete from liverange table also
1535 delete from all the points inbetween and the new
1537 for (sic = dic; sic != ic; sic = sic->next)
1539 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1540 if (IS_ITEMP (IC_RESULT (dic)))
1541 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1544 remiCodeFromeBBlock (ebp, ic);
1548 /** Scanning backwards looks for first assig found.
1551 findAssignToSym (operand * op, iCode * ic)
1555 for (dic = ic->prev; dic; dic = dic->prev)
1558 /* if definition by assignment */
1559 if (dic->op == '=' &&
1560 !POINTER_SET (dic) &&
1561 IC_RESULT (dic)->key == op->key)
1562 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1565 /* we are interested only if defined in far space */
1566 /* or in stack space in case of + & - */
1568 /* if assigned to a non-symbol then return
1570 if (!IS_SYMOP (IC_RIGHT (dic)))
1573 /* if the symbol is in far space then
1575 if (isOperandInFarSpace (IC_RIGHT (dic)))
1578 /* for + & - operations make sure that
1579 if it is on the stack it is the same
1580 as one of the three operands */
1581 if ((ic->op == '+' || ic->op == '-') &&
1582 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1585 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1586 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1587 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1595 /* if we find an usage then we cannot delete it */
1596 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1599 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1602 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1606 /* now make sure that the right side of dic
1607 is not defined between ic & dic */
1610 iCode *sic = dic->next;
1612 for (; sic != ic; sic = sic->next)
1613 if (IC_RESULT (sic) &&
1614 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1623 #if !DISABLE_PACKREGSFORSUPPORT
1626 /*-----------------------------------------------------------------*/
1627 /* packRegsForSupport :- reduce some registers for support calls */
1628 /*-----------------------------------------------------------------*/
1630 packRegsForSupport (iCode * ic, eBBlock * ebp)
1633 /* for the left & right operand :- look to see if the
1634 left was assigned a true symbol in far space in that
1635 case replace them */
1636 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1638 if (IS_ITEMP (IC_LEFT (ic)) &&
1639 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1641 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1647 /* found it we need to remove it from the
1649 for (sic = dic; sic != ic; sic = sic->next)
1650 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1652 IC_LEFT (ic)->operand.symOperand =
1653 IC_RIGHT (dic)->operand.symOperand;
1654 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1655 remiCodeFromeBBlock (ebp, dic);
1659 /* do the same for the right operand */
1662 IS_ITEMP (IC_RIGHT (ic)) &&
1663 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1665 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1671 /* found it we need to remove it from the block */
1672 for (sic = dic; sic != ic; sic = sic->next)
1673 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1675 IC_RIGHT (ic)->operand.symOperand =
1676 IC_RIGHT (dic)->operand.symOperand;
1677 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1679 remiCodeFromeBBlock (ebp, dic);
1687 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1689 /** Will reduce some registers for single use.
1692 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1697 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1699 /* if returning a literal then do nothing */
1703 /* only upto 2 bytes since we cannot predict
1704 the usage of b, & acc */
1705 if (getSize (operandType (op)) > 2 &&
1710 /* this routine will mark the a symbol as used in one
1711 instruction use only && if the defintion is local
1712 (ie. within the basic block) && has only one definition &&
1713 that definiion is either a return value from a
1714 function or does not contain any variables in
1716 uses = bitVectCopy (OP_USES (op));
1717 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1718 if (!bitVectIsZero (uses)) /* has other uses */
1721 /* if it has only one defintion */
1722 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1723 return NULL; /* has more than one definition */
1725 /* get the that definition */
1727 hTabItemWithKey (iCodehTab,
1728 bitVectFirstBit (OP_DEFS (op)))))
1731 /* found the definition now check if it is local */
1732 if (dic->seq < ebp->fSeq ||
1733 dic->seq > ebp->lSeq)
1734 return NULL; /* non-local */
1736 /* now check if it is the return from a function call */
1737 if (dic->op == CALL || dic->op == PCALL)
1739 if (ic->op != SEND && ic->op != RETURN)
1741 OP_SYMBOL (op)->ruonly = 1;
1747 /* otherwise check that the definition does
1748 not contain any symbols in far space */
1749 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1750 isOperandInFarSpace (IC_RIGHT (dic)) ||
1751 IS_OP_RUONLY (IC_LEFT (ic)) ||
1752 IS_OP_RUONLY (IC_RIGHT (ic)))
1757 /* if pointer set then make sure the pointer is one byte */
1758 if (POINTER_SET (dic))
1761 if (POINTER_GET (dic))
1766 /* also make sure the intervenening instructions
1767 don't have any thing in far space */
1768 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1770 /* if there is an intervening function call then no */
1771 if (dic->op == CALL || dic->op == PCALL)
1773 /* if pointer set then make sure the pointer
1775 if (POINTER_SET (dic))
1778 if (POINTER_GET (dic))
1781 /* if address of & the result is remat the okay */
1782 if (dic->op == ADDRESS_OF &&
1783 OP_SYMBOL (IC_RESULT (dic))->remat)
1786 /* if left or right or result is in far space */
1787 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1788 isOperandInFarSpace (IC_RIGHT (dic)) ||
1789 isOperandInFarSpace (IC_RESULT (dic)) ||
1790 IS_OP_RUONLY (IC_LEFT (dic)) ||
1791 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1792 IS_OP_RUONLY (IC_RESULT (dic)))
1798 OP_SYMBOL (op)->ruonly = 1;
1802 /*-----------------------------------------------------------------*/
1803 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1804 /*-----------------------------------------------------------------*/
1806 isBitwiseOptimizable (iCode * ic)
1808 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1810 /* bitwise operations are considered optimizable
1811 under the following conditions (Jean-Louis VERN)
1823 if (IS_LITERAL (rtype))
1829 Certian assignments involving pointers can be temporarly stored
1840 #if !DISABLE_PACKREGSFORACCUSE
1843 /** Pack registers for acc use.
1844 When the result of this operation is small and short lived it may
1845 be able to be stored in the accumelator.
1848 packRegsForAccUse (iCode * ic)
1852 /* if + or - then it has to be one byte result */
1853 if ((ic->op == '+' || ic->op == '-')
1854 && getSize (operandType (IC_RESULT (ic))) > 1)
1857 /* if shift operation make sure right side is not a literal */
1858 if (ic->op == RIGHT_OP &&
1859 (isOperandLiteral (IC_RIGHT (ic)) ||
1860 getSize (operandType (IC_RESULT (ic))) > 1))
1863 if (ic->op == LEFT_OP &&
1864 (isOperandLiteral (IC_RIGHT (ic)) ||
1865 getSize (operandType (IC_RESULT (ic))) > 1))
1868 /* has only one definition */
1869 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1872 /* has only one use */
1873 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1876 /* and the usage immediately follows this iCode */
1877 if (!(uic = hTabItemWithKey (iCodehTab,
1878 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1881 if (ic->next != uic)
1884 /* if it is a conditional branch then we definitely can */
1888 if (uic->op == JUMPTABLE)
1892 /* if the usage is not is an assignment or an
1893 arithmetic / bitwise / shift operation then not */
1894 if (POINTER_SET (uic) &&
1895 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1899 if (uic->op != '=' &&
1900 !IS_ARITHMETIC_OP (uic) &&
1901 !IS_BITWISE_OP (uic) &&
1902 uic->op != LEFT_OP &&
1903 uic->op != RIGHT_OP)
1906 /* if used in ^ operation then make sure right is not a
1908 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1911 /* if shift operation make sure right side is not a literal */
1912 if (uic->op == RIGHT_OP &&
1913 (isOperandLiteral (IC_RIGHT (uic)) ||
1914 getSize (operandType (IC_RESULT (uic))) > 1))
1917 if (uic->op == LEFT_OP &&
1918 (isOperandLiteral (IC_RIGHT (uic)) ||
1919 getSize (operandType (IC_RESULT (uic))) > 1))
1923 /* make sure that the result of this icode is not on the
1924 stack, since acc is used to compute stack offset */
1925 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1926 OP_SYMBOL (IC_RESULT (uic))->onStack)
1931 /* if either one of them in far space then we cannot */
1932 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1933 isOperandInFarSpace (IC_LEFT (uic))) ||
1934 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1935 isOperandInFarSpace (IC_RIGHT (uic))))
1939 /* if the usage has only one operand then we can */
1940 if (IC_LEFT (uic) == NULL ||
1941 IC_RIGHT (uic) == NULL)
1944 /* make sure this is on the left side if not
1945 a '+' since '+' is commutative */
1946 if (ic->op != '+' &&
1947 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1950 /* if one of them is a literal then we can */
1951 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1952 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1958 /** This is confusing :) Guess for now */
1959 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1960 (IS_ITEMP (IC_RIGHT (uic)) ||
1961 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
1964 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
1965 (IS_ITEMP (IC_LEFT (uic)) ||
1966 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
1970 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
1975 packRegsForHLUse (iCode * ic)
1982 /* has only one definition */
1983 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1986 /* has only one use */
1987 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1990 /* and the usage immediately follows this iCode */
1991 if (!(uic = hTabItemWithKey (iCodehTab,
1992 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1995 if (ic->next != uic)
1998 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2000 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2004 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2008 opPreservesA (iCode * ic, iCode * uic)
2010 /* if it is a conditional branch then we definitely can */
2014 if (uic->op == JUMPTABLE)
2017 /* if the usage has only one operand then we can */
2018 /* PENDING: check */
2019 if (IC_LEFT (uic) == NULL ||
2020 IC_RIGHT (uic) == NULL)
2023 /* PENDING: check this rule */
2024 if (getSize (operandType (IC_RESULT (uic))) > 1)
2031 !IS_ARITHMETIC_OP(uic) (sub requires A)
2035 !IS_BITWISE_OP (uic) &&
2038 !POINTER_GET (uic) &&
2040 uic->op != LEFT_OP &&
2041 uic->op != RIGHT_OP && */
2049 if (!IC_LEFT (uic) || !IC_RESULT (ic))
2052 /** This is confusing :) Guess for now */
2053 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2054 (IS_ITEMP (IC_RIGHT (uic)) ||
2055 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2058 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2059 (IS_ITEMP (IC_LEFT (uic)) ||
2060 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2067 joinPushes (iCode * ic)
2070 if (ic->op == IPUSH &&
2071 isOperandLiteral (IC_LEFT (ic)) &&
2072 getSize (operandType (IC_LEFT (ic))) == 1 &&
2073 ic->next->op == IPUSH &&
2074 isOperandLiteral (IC_LEFT (ic->next)) &&
2075 getSize (operandType (IC_LEFT (ic->next))) == 1)
2077 /* This is a bit tricky as michaelh doesnt know what he's doing.
2079 /* First upgrade the size of (first) to int */
2080 SPEC_NOUN (operandType (IC_LEFT (ic))) = V_INT;
2082 floatFromVal (AOP /* need some sleep ... */ );
2083 /* Now get and join the values */
2084 value *val = aop->aopu.aop_lit;
2085 /* if it is a float then it gets tricky */
2086 /* otherwise it is fairly simple */
2087 if (!IS_FLOAT (val->type))
2089 unsigned long v = floatFromVal (val);
2091 floatFrom ( /* need some sleep ... */ );
2092 printf ("Size %u\n", getSize (operandType (IC_LEFT (ic))));
2093 ic->next = ic->next->next;
2099 /** Pack registers for acc use.
2100 When the result of this operation is small and short lived it may
2101 be able to be stored in the accumulator.
2103 Note that the 'A preserving' list is currently emperical :)e
2106 packRegsForAccUse2 (iCode * ic)
2110 D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2112 /* Filter out all but those 'good' commands */
2114 !POINTER_GET (ic) &&
2116 !IS_BITWISE_OP (ic) &&
2123 /* if + or - then it has to be one byte result.
2126 if ((ic->op == '+' || ic->op == '-')
2127 && getSize (operandType (IC_RESULT (ic))) > 1)
2130 /* if shift operation make sure right side is not a literal.
2134 if (ic->op == RIGHT_OP &&
2135 (isOperandLiteral (IC_RIGHT (ic)) ||
2136 getSize (operandType (IC_RESULT (ic))) > 1))
2139 if (ic->op == LEFT_OP &&
2140 (isOperandLiteral (IC_RIGHT (ic)) ||
2141 getSize (operandType (IC_RESULT (ic))) > 1))
2145 /* has only one definition */
2146 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2151 /* Right. We may be able to propagate it through if:
2152 For each in the chain of uses the intermediate is OK.
2154 /* Get next with 'uses result' bit on
2155 If this->next == next
2156 Validate use of next
2157 If OK, increase count
2159 /* and the usage immediately follows this iCode */
2160 if (!(uic = hTabItemWithKey (iCodehTab,
2161 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2167 /* Create a copy of the OP_USES bit vect */
2168 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2170 iCode *scan = ic, *next;
2174 setBit = bitVectFirstBit (uses);
2175 next = hTabItemWithKey (iCodehTab, setBit);
2176 if (scan->next == next)
2178 bitVectUnSetBit (uses, setBit);
2179 /* Still contigous. */
2180 if (!opPreservesA (ic, next))
2191 while (!bitVectIsZero (uses));
2192 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2196 /* OLD CODE FOLLOWS */
2197 /* if it is a conditional branch then we definitely can
2205 if (uic->op == JUMPTABLE)
2209 /* if the usage is not is an assignment or an
2210 arithmetic / bitwise / shift operation then not.
2211 MLH: Pending: Invalid. Our pointer sets are always peechy.
2214 if (POINTER_SET (uic) &&
2215 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2217 printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2223 if (uic->op != '=' &&
2224 !IS_ARITHMETIC_OP (uic) &&
2225 !IS_BITWISE_OP (uic) &&
2226 uic->op != LEFT_OP &&
2227 uic->op != RIGHT_OP)
2233 /* if used in ^ operation then make sure right is not a
2235 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2238 /* if shift operation make sure right side is not a literal */
2239 if (uic->op == RIGHT_OP &&
2240 (isOperandLiteral (IC_RIGHT (uic)) ||
2241 getSize (operandType (IC_RESULT (uic))) > 1))
2244 if (uic->op == LEFT_OP &&
2245 (isOperandLiteral (IC_RIGHT (uic)) ||
2246 getSize (operandType (IC_RESULT (uic))) > 1))
2250 /* make sure that the result of this icode is not on the
2251 stack, since acc is used to compute stack offset */
2252 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2253 OP_SYMBOL (IC_RESULT (uic))->onStack)
2258 /* if either one of them in far space then we cannot */
2259 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2260 isOperandInFarSpace (IC_LEFT (uic))) ||
2261 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2262 isOperandInFarSpace (IC_RIGHT (uic))))
2266 /* if the usage has only one operand then we can */
2267 if (IC_LEFT (uic) == NULL ||
2268 IC_RIGHT (uic) == NULL)
2271 /* make sure this is on the left side if not
2272 a '+' since '+' is commutative */
2273 if (ic->op != '+' &&
2274 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2277 /* if one of them is a literal then we can */
2278 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2279 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2285 /** This is confusing :) Guess for now */
2286 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2287 (IS_ITEMP (IC_RIGHT (uic)) ||
2288 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2291 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2292 (IS_ITEMP (IC_LEFT (uic)) ||
2293 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2297 printf ("acc ok!\n");
2298 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2301 /** Does some transformations to reduce register pressure.
2304 packRegisters (eBBlock * ebp)
2309 D (D_ALLOC, ("packRegisters: entered.\n"));
2311 while (1 && !DISABLE_PACK_ASSIGN)
2314 /* look for assignments of the form */
2315 /* iTempNN = TRueSym (someoperation) SomeOperand */
2317 /* TrueSym := iTempNN:1 */
2318 for (ic = ebp->sch; ic; ic = ic->next)
2320 /* find assignment of the form TrueSym := iTempNN:1 */
2321 if (ic->op == '=' && !POINTER_SET (ic))
2322 change += packRegsForAssign (ic, ebp);
2328 for (ic = ebp->sch; ic; ic = ic->next)
2330 /* Safe: address of a true sym is always constant. */
2331 /* if this is an itemp & result of a address of a true sym
2332 then mark this as rematerialisable */
2333 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2335 if (ic->op == ADDRESS_OF &&
2336 IS_ITEMP (IC_RESULT (ic)) &&
2337 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2338 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2339 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2342 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2343 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2344 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2347 /* Safe: just propagates the remat flag */
2348 /* if straight assignment then carry remat flag if this is the
2350 if (ic->op == '=' &&
2351 !POINTER_SET (ic) &&
2352 IS_SYMOP (IC_RIGHT (ic)) &&
2353 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2354 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2357 OP_SYMBOL (IC_RESULT (ic))->remat =
2358 OP_SYMBOL (IC_RIGHT (ic))->remat;
2359 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2360 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2363 /* if the condition of an if instruction is defined in the
2364 previous instruction then mark the itemp as a conditional */
2365 if ((IS_CONDITIONAL (ic) ||
2366 ((ic->op == BITWISEAND ||
2369 isBitwiseOptimizable (ic))) &&
2370 ic->next && ic->next->op == IFX &&
2371 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2372 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2375 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2380 /* reduce for support function calls */
2381 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2382 packRegsForSupport (ic, ebp);
2386 /* some cases the redundant moves can
2387 can be eliminated for return statements */
2388 if ((ic->op == RETURN || ic->op == SEND) &&
2389 !isOperandInFarSpace (IC_LEFT (ic)) &&
2391 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2393 /* if pointer set & left has a size more than
2394 one and right is not in far space */
2395 if (POINTER_SET (ic) &&
2396 /* MLH: no such thing.
2397 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2398 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2399 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2400 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2403 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2406 /* if pointer get */
2407 if (!DISABLE_PACK_ONE_USE &&
2409 /* MLH: dont have far space
2410 !isOperandInFarSpace(IC_RESULT(ic))&& */
2411 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2412 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2413 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2416 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2418 /* pack registers for accumulator use, when the result of an
2419 arithmetic or bit wise operation has only one use, that use is
2420 immediately following the defintion and the using iCode has
2421 only one operand or has two operands but one is literal & the
2422 result of that operation is not on stack then we can leave the
2423 result of this operation in acc:b combination */
2425 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2427 packRegsForHLUse (ic);
2430 if ((IS_ARITHMETIC_OP (ic)
2431 || IS_BITWISE_OP (ic)
2432 || ic->op == LEFT_OP || ic->op == RIGHT_OP
2434 IS_ITEMP (IC_RESULT (ic)) &&
2435 getSize (operandType (IC_RESULT (ic))) <= 2)
2436 packRegsForAccUse (ic);
2438 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2439 getSize (operandType (IC_RESULT (ic))) == 1)
2441 packRegsForAccUse2 (ic);
2448 /*-----------------------------------------------------------------*/
2449 /* assignRegisters - assigns registers to each live range as need */
2450 /*-----------------------------------------------------------------*/
2452 z80_assignRegisters (eBBlock ** ebbs, int count)
2457 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2459 setToNull ((void *) &funcrUsed);
2460 stackExtend = dataExtend = 0;
2464 /* DE is required for the code gen. */
2465 _nRegs = GBZ80_MAX_REGS;
2466 regsZ80 = _gbz80_regs;
2470 _nRegs = Z80_MAX_REGS;
2471 regsZ80 = _z80_regs;
2474 /* change assignments this will remove some
2475 live ranges reducing some register pressure */
2476 for (i = 0; i < count; i++)
2477 packRegisters (ebbs[i]);
2479 if (options.dump_pack)
2480 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2482 /* first determine for each live range the number of
2483 registers & the type of registers required for each */
2486 /* and serially allocate registers */
2487 serialRegAssign (ebbs, count);
2489 /* if stack was extended then tell the user */
2492 /* werror(W_TOOMANY_SPILS,"stack", */
2493 /* stackExtend,currFunc->name,""); */
2499 /* werror(W_TOOMANY_SPILS,"data space", */
2500 /* dataExtend,currFunc->name,""); */
2504 if (options.dump_rassgn)
2505 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2507 /* after that create the register mask
2508 for each of the instruction */
2509 createRegMask (ebbs, count);
2511 /* now get back the chain */
2512 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2514 /* redo that offsets for stacked automatic variables */
2515 redoStackOffsets ();
2519 /* free up any stackSpil locations allocated */
2520 applyToSet (stackSpil, deallocStackSpil);
2522 setToNull ((void **) &stackSpil);
2523 setToNull ((void **) &spiltSet);
2524 /* mark all registers as free */