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
47 #include "SDCCicode.h"
49 /* Flags to turn off optimisations.
54 DISABLE_PACK_ASSIGN = 0,
55 DISABLE_PACK_ONE_USE = 0,
59 /* Flags to turn on debugging code.
66 D_ACCUSE2_VERBOSE = 0,
73 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
78 #define DISABLE_PACKREGSFORSUPPORT 1
79 #define DISABLE_PACKREGSFORACCUSE 1
81 extern void genZ80Code (iCode *);
83 /** Local static variables */
91 /* registers used in a function */
98 static regs _gbz80_regs[] =
100 {REG_GPR, C_IDX, "c", 1},
101 {REG_GPR, B_IDX, "b", 1},
102 {REG_CND, CND_IDX, "c", 1}
105 static regs _z80_regs[] =
107 {REG_GPR, C_IDX, "c", 1},
108 {REG_GPR, B_IDX, "b", 1},
109 {REG_GPR, E_IDX, "e", 1},
110 {REG_GPR, D_IDX, "d", 1},
111 {REG_CND, CND_IDX, "c", 1}
116 /** Number of usable registers (all but C) */
117 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
118 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
120 static void spillThis (symbol *);
122 /** Allocates register of given type.
123 'type' is not used on the z80 version. It was used to select
124 between pointer and general purpose registers on the mcs51 version.
126 @return Pointer to the newly allocated register.
129 allocReg (short type)
133 for (i = 0; i < _G.nRegs; i++)
135 /* For now we allocate from any free */
136 if (regsZ80[i].isFree)
138 regsZ80[i].isFree = 0;
141 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
143 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
147 D (D_ALLOC, ("allocReg: No free.\n"));
151 /** Returns pointer to register wit index number
158 for (i = 0; i < _G.nRegs; i++)
160 if (regsZ80[i].rIdx == idx)
166 wassertl (0, "regWithIdx not found");
170 /** Frees a register.
175 wassert (!reg->isFree);
177 D (D_ALLOC, ("freeReg: freed %p\n", reg));
181 /** Returns number of free registers.
189 for (i = 0; i < _G.nRegs; i++)
191 /* For now only one reg type */
192 if (regsZ80[i].isFree)
200 /** Free registers with type.
203 nfreeRegsType (int type)
208 if ((nfr = nFreeRegs (type)) == 0)
210 return nFreeRegs (REG_GPR);
214 return nFreeRegs (type);
219 /*-----------------------------------------------------------------*/
220 /* allDefsOutOfRange - all definitions are out of a range */
221 /*-----------------------------------------------------------------*/
223 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
230 for (i = 0; i < defs->size; i++)
234 if (bitVectBitValue (defs, i) &&
235 (ic = hTabItemWithKey (iCodehTab, i)) &&
236 (ic->seq >= fseq && ic->seq <= toseq))
246 /*-----------------------------------------------------------------*/
247 /* computeSpillable - given a point find the spillable live ranges */
248 /*-----------------------------------------------------------------*/
250 computeSpillable (iCode * ic)
254 /* spillable live ranges are those that are live at this
255 point . the following categories need to be subtracted
257 a) - those that are already spilt
258 b) - if being used by this one
259 c) - defined by this one */
261 spillable = bitVectCopy (ic->rlive);
263 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
265 bitVectCplAnd (spillable, ic->uses); /* used in this one */
266 bitVectUnSetBit (spillable, ic->defKey);
267 spillable = bitVectIntersect (spillable, _G.regAssigned);
272 /*-----------------------------------------------------------------*/
273 /* noSpilLoc - return true if a variable has no spil location */
274 /*-----------------------------------------------------------------*/
276 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
278 return (sym->usl.spillLoc ? 0 : 1);
281 /*-----------------------------------------------------------------*/
282 /* hasSpilLoc - will return 1 if the symbol has spil location */
283 /*-----------------------------------------------------------------*/
285 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
287 return (sym->usl.spillLoc ? 1 : 0);
290 /** Will return 1 if the remat flag is set.
291 A symbol is rematerialisable if it doesnt need to be allocated
292 into registers at creation as it can be re-created at any time -
293 i.e. it's constant in some way.
296 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
301 /*-----------------------------------------------------------------*/
302 /* allLRs - return true for all */
303 /*-----------------------------------------------------------------*/
305 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
310 /** liveRangesWith - applies function to a given set of live range
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 wassertl (0, "liveRangesWith could not find liveRange");
336 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
338 addSetHead (&rset, sym);
346 /** leastUsedLR - given a set determines which is the least used
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 /** noOverLap - will iterate through the list looking for over lap
381 noOverLap (set * itmpStack, symbol * fsym)
385 for (sym = setFirstItem (itmpStack); sym;
386 sym = setNextItem (itmpStack))
388 if (bitVectBitValue(sym->clashes,fsym->key))
391 // if sym starts before (or on) our end point
392 // and ends after (or on) our start point,
394 if (sym->liveFrom <= fsym->liveTo &&
395 sym->liveTo >= fsym->liveFrom)
404 /*-----------------------------------------------------------------*/
405 /* isFree - will return 1 if the a free spil location is found */
406 /*-----------------------------------------------------------------*/
410 V_ARG (symbol **, sloc);
411 V_ARG (symbol *, fsym);
413 /* if already found */
417 /* if it is free && and the itmp assigned to
418 this does not have any overlapping live ranges
419 with the one currently being assigned and
420 the size can be accomodated */
422 noOverLap (sym->usl.itmpStack, fsym) &&
423 getSize (sym->type) >= getSize (fsym->type))
432 /*-----------------------------------------------------------------*/
433 /* createStackSpil - create a location on the stack to spil */
434 /*-----------------------------------------------------------------*/
436 createStackSpil (symbol * sym)
440 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
442 /* first go try and find a free one that is already
443 existing on the stack */
444 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
446 /* found a free one : just update & return */
447 sym->usl.spillLoc = sloc;
450 addSetHead (&sloc->usl.itmpStack, sym);
451 D (D_ALLOC, ("createStackSpil: found existing\n"));
455 /* could not then have to create one , this is the hard part
456 we need to allocate this on the stack : this is really a
457 hack!! but cannot think of anything better at this time */
459 sprintf (buffer, "sloc%d", _G.slocNum++);
460 sloc = newiTemp (buffer);
462 /* set the type to the spilling symbol */
463 sloc->type = copyLinkChain (sym->type);
464 sloc->etype = getSpec (sloc->type);
465 SPEC_SCLS (sloc->etype) = S_AUTO;
466 SPEC_STAT (sloc->etype) = 0;
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 _G.stackExtend += getSize (sloc->type);
480 _G.dataExtend += getSize (sloc->type);
483 /* add it to the stackSpil set */
484 addSetHead (&_G.stackSpil, sloc);
485 sym->usl.spillLoc = sloc;
488 /* add it to the set of itempStack set
489 of the spill location */
490 addSetHead (&sloc->usl.itmpStack, sym);
492 D (D_ALLOC, ("createStackSpil: created new\n"));
496 /*-----------------------------------------------------------------*/
497 /* spillThis - spils a specific operand */
498 /*-----------------------------------------------------------------*/
500 spillThis (symbol * sym)
504 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
506 /* if this is rematerializable or has a spillLocation
507 we are okay, else we need to create a spillLocation
509 if (!(sym->remat || sym->usl.spillLoc))
511 createStackSpil (sym);
514 /* mark it has spilt & put it in the spilt set */
516 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
518 bitVectUnSetBit (_G.regAssigned, sym->key);
520 for (i = 0; i < sym->nRegs; i++)
524 freeReg (sym->regs[i]);
529 if (sym->usl.spillLoc && !sym->remat)
531 sym->usl.spillLoc->allocreq = 1;
537 /*-----------------------------------------------------------------*/
538 /* allDefsOutOfRange - all definitions are out of a range */
539 /*-----------------------------------------------------------------*/
541 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
548 for (i = 0; i < defs->size; i++)
552 if (bitVectBitValue (defs, i) &&
553 (ic = hTabItemWithKey (iCodehTab, i)) &&
554 (ic->seq >= fseq && ic->seq <= toseq))
563 /*-----------------------------------------------------------------*/
564 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
565 /* but is not used as a pointer */
566 /*-----------------------------------------------------------------*/
568 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
570 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
573 /*-----------------------------------------------------------------*/
574 /* notUsedInBlock - not used in this block */
575 /*-----------------------------------------------------------------*/
577 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
579 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
580 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
581 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
584 /*-----------------------------------------------------------------*/
585 /* notUsedInRemaining - not used or defined in remain of the block */
586 /*-----------------------------------------------------------------*/
588 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
590 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
591 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
595 /** Select a iTemp to spil : rather a simple procedure.
598 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
600 bitVect *lrcs = NULL;
604 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
605 /* get the spillable live ranges */
606 lrcs = computeSpillable (ic);
608 /* get all live ranges that are rematerizable */
609 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
611 D (D_ALLOC, ("selectSpil: using remat.\n"));
612 /* return the least used of these */
613 return leastUsedLR (selectS);
617 /* get live ranges with spillLocations in direct space */
618 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
620 sym = leastUsedLR (selectS);
621 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
622 sym->usl.spillLoc->rname :
623 sym->usl.spillLoc->name));
625 /* mark it as allocation required */
626 sym->usl.spillLoc->allocreq = 1;
630 /* if the symbol is local to the block then */
631 if (forSym->liveTo < ebp->lSeq)
634 /* check if there are any live ranges allocated
635 to registers that are not used in this block */
636 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
638 sym = leastUsedLR (selectS);
639 /* if this is not rematerializable */
643 wassertl (0, "Attempted to do an unsupported block spill");
649 /* check if there are any live ranges that not
650 used in the remainder of the block */
651 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
653 sym = leastUsedLR (selectS);
658 wassertl (0, "Attempted to do an unsupported remain spill");
666 /* find live ranges with spillocation && not used as pointers */
667 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
670 sym = leastUsedLR (selectS);
671 /* mark this as allocation required */
672 sym->usl.spillLoc->allocreq = 1;
677 /* find live ranges with spillocation */
678 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
680 D (D_ALLOC, ("selectSpil: using with spill.\n"));
681 sym = leastUsedLR (selectS);
682 sym->usl.spillLoc->allocreq = 1;
686 /* couldn't find then we need to create a spil
687 location on the stack , for which one? the least
689 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
691 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
692 /* return a created spil location */
693 sym = createStackSpil (leastUsedLR (selectS));
694 sym->usl.spillLoc->allocreq = 1;
698 /* this is an extreme situation we will spill
699 this one : happens very rarely but it does happen */
700 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
706 /** Spil some variable & mark registers as free.
707 A spill occurs when an iTemp wont fit into the available registers.
710 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
715 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
717 /* get something we can spil */
718 ssym = selectSpil (ic, ebp, forSym);
720 /* mark it as spilt */
722 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
724 /* mark it as not register assigned &
725 take it away from the set */
726 bitVectUnSetBit (_G.regAssigned, ssym->key);
728 /* mark the registers as free */
729 for (i = 0; i < ssym->nRegs; i++)
731 freeReg (ssym->regs[i]);
733 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
734 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
736 /* if spilt on stack then free up r0 & r1
737 if they could have been assigned to as gprs */
738 if (!ptrRegReq && isSpiltOnStack (ssym))
741 spillLRWithPtrReg (ssym);
744 /* if this was a block level spil then insert push & pop
745 at the start & end of block respectively */
748 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
749 /* add push to the start of the block */
750 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
751 ebp->sch->next : ebp->sch));
752 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
753 /* add pop to the end of the block */
754 addiCodeToeBBlock (ebp, nic, NULL);
757 /* if spilt because not used in the remainder of the
758 block then add a push before this instruction and
759 a pop at the end of the block */
760 if (ssym->remainSpil)
763 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
764 /* add push just before this instruction */
765 addiCodeToeBBlock (ebp, nic, ic);
767 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
768 /* add pop to the end of the block */
769 addiCodeToeBBlock (ebp, nic, NULL);
773 D (D_ALLOC, ("spilSomething: done.\n"));
781 /** Will try for GPR if not spil.
784 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
788 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
790 /* try for gpr type */
791 if ((reg = allocReg (REG_GPR)))
793 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
797 /* we have to spil */
798 if (!spilSomething (ic, ebp, sym))
800 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
804 /* this looks like an infinite loop but
805 in really selectSpil will abort */
809 /** Symbol has a given register.
812 symHasReg (symbol * sym, regs * reg)
816 for (i = 0; i < sym->nRegs; i++)
817 if (sym->regs[i] == reg)
823 /** Check the live to and if they have registers & are not spilt then
824 free up the registers
827 deassignLRs (iCode * ic, eBBlock * ebp)
833 for (sym = hTabFirstItem (liveRanges, &k); sym;
834 sym = hTabNextItem (liveRanges, &k))
838 /* if it does not end here */
839 if (sym->liveTo > ic->seq)
842 /* if it was spilt on stack then we can
843 mark the stack spil location as free */
848 sym->usl.spillLoc->isFree = 1;
854 if (!bitVectBitValue (_G.regAssigned, sym->key))
857 /* special case check if this is an IFX &
858 the privious one was a pop and the
859 previous one was not spilt then keep track
861 if (ic->op == IFX && ic->prev &&
862 ic->prev->op == IPOP &&
863 !ic->prev->parmPush &&
864 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
865 psym = OP_SYMBOL (IC_LEFT (ic->prev));
867 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
873 bitVectUnSetBit (_G.regAssigned, sym->key);
875 /* if the result of this one needs registers
876 and does not have it then assign it right
878 if (IC_RESULT (ic) &&
879 !(SKIP_IC2 (ic) || /* not a special icode */
880 ic->op == JUMPTABLE ||
885 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
886 result->liveTo > ic->seq && /* and will live beyond this */
887 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
888 result->regType == sym->regType && /* same register types */
889 result->nRegs && /* which needs registers */
890 !result->isspilt && /* and does not already have them */
892 !bitVectBitValue (_G.regAssigned, result->key) &&
893 /* the number of free regs + number of regs in this LR
894 can accomodate the what result Needs */
895 ((nfreeRegsType (result->regType) +
896 sym->nRegs) >= result->nRegs)
899 for (i = 0; i < result->nRegs; i++)
902 result->regs[i] = sym->regs[i];
904 result->regs[i] = getRegGpr (ic, ebp, result);
906 /* if the allocation falied which means
907 this was spilt then break */
908 if (!result->regs[i])
916 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
919 /* free the remaining */
920 for (; i < sym->nRegs; i++)
924 if (!symHasReg (psym, sym->regs[i]))
925 freeReg (sym->regs[i]);
928 freeReg (sym->regs[i]);
929 // sym->regs[i] = NULL;
936 /** Reassign this to registers.
939 reassignLR (operand * op)
941 symbol *sym = OP_SYMBOL (op);
944 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
946 /* not spilt any more */
947 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
948 bitVectUnSetBit (_G.spiltSet, sym->key);
950 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
954 for (i = 0; i < sym->nRegs; i++)
955 sym->regs[i]->isFree = 0;
958 /** Determines if allocating will cause a spill.
961 willCauseSpill (int nr, int rt)
963 /* first check if there are any avlb registers
964 of te type required */
965 if (nFreeRegs (0) >= nr)
968 /* it will cause a spil */
972 /** The allocator can allocate same registers to result and operand,
973 if this happens make sure they are in the same position as the operand
974 otherwise chaos results.
977 positionRegs (symbol * result, symbol * opsym, int lineno)
979 int count = min (result->nRegs, opsym->nRegs);
980 int i, j = 0, shared = 0;
982 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
984 /* if the result has been spilt then cannot share */
989 /* first make sure that they actually share */
990 for (i = 0; i < count; i++)
992 for (j = 0; j < count; j++)
994 if (result->regs[i] == opsym->regs[j] && i != j)
1004 regs *tmp = result->regs[i];
1005 result->regs[i] = result->regs[j];
1006 result->regs[j] = tmp;
1011 /** Try to allocate a pair of registers to the symbol.
1014 tryAllocatingRegPair (symbol * sym)
1017 wassert (sym->nRegs == 2);
1018 for (i = 0; i < _G.nRegs; i += 2)
1020 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1022 regsZ80[i].isFree = 0;
1023 sym->regs[0] = ®sZ80[i];
1024 regsZ80[i + 1].isFree = 0;
1025 sym->regs[1] = ®sZ80[i + 1];
1026 sym->regType = REG_PAIR;
1030 currFunc->regsUsed =
1031 bitVectSetBit (currFunc->regsUsed, i);
1032 currFunc->regsUsed =
1033 bitVectSetBit (currFunc->regsUsed, i + 1);
1035 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1039 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1043 /** Serially allocate registers to the variables.
1044 This is the main register allocation function. It is called after
1048 serialRegAssign (eBBlock ** ebbs, int count)
1052 /* for all blocks */
1053 for (i = 0; i < count; i++)
1058 if (ebbs[i]->noPath &&
1059 (ebbs[i]->entryLabel != entryLabel &&
1060 ebbs[i]->entryLabel != returnLabel))
1063 /* of all instructions do */
1064 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1067 /* if this is an ipop that means some live
1068 range will have to be assigned again */
1072 reassignLR (IC_LEFT (ic));
1075 /* if result is present && is a true symbol */
1076 if (IC_RESULT (ic) && ic->op != IFX &&
1077 IS_TRUE_SYMOP (IC_RESULT (ic)))
1078 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1080 /* take away registers from live
1081 ranges that end at this instruction */
1082 deassignLRs (ic, ebbs[i]);
1084 /* some don't need registers */
1085 /* MLH: removed RESULT and POINTER_SET condition */
1086 if (SKIP_IC2 (ic) ||
1087 ic->op == JUMPTABLE ||
1093 /* now we need to allocate registers only for the result */
1096 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1101 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1103 /* if it does not need or is spilt
1104 or is already assigned to registers
1105 or will not live beyond this instructions */
1108 bitVectBitValue (_G.regAssigned, sym->key) ||
1109 sym->liveTo <= ic->seq)
1111 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1115 /* if some liverange has been spilt at the block level
1116 and this one live beyond this block then spil this
1118 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1120 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1124 /* if trying to allocate this will cause
1125 a spill and there is nothing to spill
1126 or this one is rematerializable then
1128 willCS = willCauseSpill (sym->nRegs, sym->regType);
1129 spillable = computeSpillable (ic);
1131 (willCS && bitVectIsZero (spillable)))
1134 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1140 /* if it has a spillocation & is used less than
1141 all other live ranges then spill this */
1143 if (sym->usl.spillLoc) {
1144 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1145 allLRs, ebbs[i], ic));
1146 if (leastUsed && leastUsed->used > sym->used) {
1151 /* if none of the liveRanges have a spillLocation then better
1152 to spill this one than anything else already assigned to registers */
1153 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1154 /* if this is local to this block then we might find a block spil */
1155 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1163 /* else we assign registers to it */
1164 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1166 /* Special case: Try to fit into a reg pair if
1168 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1169 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1174 for (j = 0; j < sym->nRegs; j++)
1176 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1178 /* if the allocation falied which means
1179 this was spilt then break */
1182 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1187 /* if it shares registers with operands make sure
1188 that they are in the same position */
1189 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1190 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1191 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1192 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1193 /* do the same for the right operand */
1194 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1195 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1196 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1197 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1204 /*-----------------------------------------------------------------*/
1205 /* rUmaskForOp :- returns register mask for an operand */
1206 /*-----------------------------------------------------------------*/
1208 rUmaskForOp (operand * op)
1214 /* only temporaries are assigned registers */
1218 sym = OP_SYMBOL (op);
1220 /* if spilt or no registers assigned to it
1222 if (sym->isspilt || !sym->nRegs)
1225 rumask = newBitVect (_G.nRegs);
1227 for (j = 0; j < sym->nRegs; j++)
1229 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1236 z80_rUmaskForOp (operand * op)
1238 return rUmaskForOp (op);
1241 /** Returns bit vector of registers used in iCode.
1244 regsUsedIniCode (iCode * ic)
1246 bitVect *rmask = newBitVect (_G.nRegs);
1248 /* do the special cases first */
1251 rmask = bitVectUnion (rmask,
1252 rUmaskForOp (IC_COND (ic)));
1256 /* for the jumptable */
1257 if (ic->op == JUMPTABLE)
1259 rmask = bitVectUnion (rmask,
1260 rUmaskForOp (IC_JTCOND (ic)));
1265 /* of all other cases */
1267 rmask = bitVectUnion (rmask,
1268 rUmaskForOp (IC_LEFT (ic)));
1272 rmask = bitVectUnion (rmask,
1273 rUmaskForOp (IC_RIGHT (ic)));
1276 rmask = bitVectUnion (rmask,
1277 rUmaskForOp (IC_RESULT (ic)));
1283 /** For each instruction will determine the regsUsed.
1286 createRegMask (eBBlock ** ebbs, int count)
1290 /* for all blocks */
1291 for (i = 0; i < count; i++)
1295 if (ebbs[i]->noPath &&
1296 (ebbs[i]->entryLabel != entryLabel &&
1297 ebbs[i]->entryLabel != returnLabel))
1300 /* for all instructions */
1301 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1306 if (SKIP_IC2 (ic) || !ic->rlive)
1309 /* first mark the registers used in this
1311 ic->rUsed = regsUsedIniCode (ic);
1312 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1314 /* now create the register mask for those
1315 registers that are in use : this is a
1316 super set of ic->rUsed */
1317 ic->rMask = newBitVect (_G.nRegs + 1);
1319 /* for all live Ranges alive at this point */
1320 for (j = 1; j < ic->rlive->size; j++)
1325 /* if not alive then continue */
1326 if (!bitVectBitValue (ic->rlive, j))
1329 /* find the live range we are interested in */
1330 if (!(sym = hTabItemWithKey (liveRanges, j)))
1332 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1333 "createRegMask cannot find live range");
1337 /* if no register assigned to it */
1338 if (!sym->nRegs || sym->isspilt)
1341 /* for all the registers allocated to it */
1342 for (k = 0; k < sym->nRegs; k++)
1345 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1351 /** Returns the rematerialized string for a remat var.
1354 rematStr (symbol * sym)
1357 iCode *ic = sym->rematiCode;
1362 /* if plus or minus print the right hand side */
1363 if (ic->op == '+' || ic->op == '-')
1365 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1368 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1371 /* we reached the end */
1372 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1379 /*-----------------------------------------------------------------*/
1380 /* regTypeNum - computes the type & number of registers required */
1381 /*-----------------------------------------------------------------*/
1388 /* for each live range do */
1389 for (sym = hTabFirstItem (liveRanges, &k); sym;
1390 sym = hTabNextItem (liveRanges, &k))
1393 /* if used zero times then no registers needed */
1394 if ((sym->liveTo - sym->liveFrom) == 0)
1397 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1399 /* if the live range is a temporary */
1403 /* if the type is marked as a conditional */
1404 if (sym->regType == REG_CND)
1407 /* if used in return only then we don't
1409 if (sym->ruonly || sym->accuse)
1411 if (IS_AGGREGATE (sym->type) || sym->isptr)
1412 sym->type = aggrToPtr (sym->type, FALSE);
1416 /* if not then we require registers */
1417 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1418 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1419 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1420 getSize (sym->type));
1421 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1423 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1427 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1428 printTypeChain (sym->type, stderr);
1429 fprintf (stderr, "\n");
1432 /* determine the type of register required */
1433 /* Always general purpose */
1434 sym->regType = REG_GPR;
1439 /* for the first run we don't provide */
1440 /* registers for true symbols we will */
1441 /* see how things go */
1442 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1449 /** Mark all registers as free.
1456 D (D_ALLOC, ("freeAllRegs: running.\n"));
1458 for (i = 0; i < _G.nRegs; i++)
1459 regsZ80[i].isFree = 1;
1462 /*-----------------------------------------------------------------*/
1463 /* deallocStackSpil - this will set the stack pointer back */
1464 /*-----------------------------------------------------------------*/
1465 DEFSETFUNC (deallocStackSpil)
1473 /** Register reduction for assignment.
1476 packRegsForAssign (iCode * ic, eBBlock * ebp)
1480 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1482 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1483 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1484 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1489 /* find the definition of iTempNN scanning backwards if we find a
1490 a use of the true symbol in before we find the definition then
1492 for (dic = ic->prev; dic; dic = dic->prev)
1494 /* PENDING: Don't pack across function calls. */
1495 if (dic->op == CALL || dic->op == PCALL)
1504 if (IS_SYMOP (IC_RESULT (dic)) &&
1505 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1510 if (IS_SYMOP (IC_RIGHT (dic)) &&
1511 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1512 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1518 if (IS_SYMOP (IC_LEFT (dic)) &&
1519 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1520 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1528 return 0; /* did not find */
1530 /* if the result is on stack or iaccess then it must be
1531 the same atleast one of the operands */
1532 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1533 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1535 /* the operation has only one symbol
1536 operator then we can pack */
1537 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1538 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1541 if (!((IC_LEFT (dic) &&
1542 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1544 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1548 /* found the definition */
1549 /* replace the result with the result of */
1550 /* this assignment and remove this assignment */
1551 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1552 IC_RESULT (dic) = IC_RESULT (ic);
1554 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1556 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1558 /* delete from liverange table also
1559 delete from all the points inbetween and the new
1561 for (sic = dic; sic != ic; sic = sic->next)
1563 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1564 if (IS_ITEMP (IC_RESULT (dic)))
1565 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1568 remiCodeFromeBBlock (ebp, ic);
1569 // PENDING: Check vs mcs51
1570 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1571 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1572 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1576 /** Scanning backwards looks for first assig found.
1579 findAssignToSym (operand * op, iCode * ic)
1583 for (dic = ic->prev; dic; dic = dic->prev)
1586 /* if definition by assignment */
1587 if (dic->op == '=' &&
1588 !POINTER_SET (dic) &&
1589 IC_RESULT (dic)->key == op->key)
1590 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1593 /* we are interested only if defined in far space */
1594 /* or in stack space in case of + & - */
1596 /* if assigned to a non-symbol then return
1598 if (!IS_SYMOP (IC_RIGHT (dic)))
1601 /* if the symbol is in far space then
1603 if (isOperandInFarSpace (IC_RIGHT (dic)))
1606 /* for + & - operations make sure that
1607 if it is on the stack it is the same
1608 as one of the three operands */
1609 if ((ic->op == '+' || ic->op == '-') &&
1610 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1613 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1614 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1615 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1623 /* if we find an usage then we cannot delete it */
1624 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1627 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1630 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1634 /* now make sure that the right side of dic
1635 is not defined between ic & dic */
1638 iCode *sic = dic->next;
1640 for (; sic != ic; sic = sic->next)
1641 if (IC_RESULT (sic) &&
1642 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1651 #if !DISABLE_PACKREGSFORSUPPORT
1654 /*-----------------------------------------------------------------*/
1655 /* packRegsForSupport :- reduce some registers for support calls */
1656 /*-----------------------------------------------------------------*/
1658 packRegsForSupport (iCode * ic, eBBlock * ebp)
1661 /* for the left & right operand :- look to see if the
1662 left was assigned a true symbol in far space in that
1663 case replace them */
1664 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1666 if (IS_ITEMP (IC_LEFT (ic)) &&
1667 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1669 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1675 /* found it we need to remove it from the
1677 for (sic = dic; sic != ic; sic = sic->next)
1678 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1680 IC_LEFT (ic)->operand.symOperand =
1681 IC_RIGHT (dic)->operand.symOperand;
1682 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1683 remiCodeFromeBBlock (ebp, dic);
1684 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1685 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1686 // PENDING: Check vs mcs51
1690 /* do the same for the right operand */
1693 IS_ITEMP (IC_RIGHT (ic)) &&
1694 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1696 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1702 /* found it we need to remove it from the block */
1703 for (sic = dic; sic != ic; sic = sic->next)
1704 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1706 IC_RIGHT (ic)->operand.symOperand =
1707 IC_RIGHT (dic)->operand.symOperand;
1708 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1710 remiCodeFromeBBlock (ebp, dic);
1711 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1712 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1713 // PENDING: vs mcs51
1721 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1723 /** Will reduce some registers for single use.
1726 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1732 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1734 /* if returning a literal then do nothing */
1738 /* only upto 2 bytes since we cannot predict
1739 the usage of b, & acc */
1740 if (getSize (operandType (op)) > 2)
1743 if (ic->op != RETURN &&
1747 /* this routine will mark the a symbol as used in one
1748 instruction use only && if the defintion is local
1749 (ie. within the basic block) && has only one definition &&
1750 that definiion is either a return value from a
1751 function or does not contain any variables in
1753 uses = bitVectCopy (OP_USES (op));
1754 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1755 if (!bitVectIsZero (uses)) /* has other uses */
1758 /* if it has only one defintion */
1759 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1760 return NULL; /* has more than one definition */
1762 /* get the that definition */
1764 hTabItemWithKey (iCodehTab,
1765 bitVectFirstBit (OP_DEFS (op)))))
1768 /* found the definition now check if it is local */
1769 if (dic->seq < ebp->fSeq ||
1770 dic->seq > ebp->lSeq)
1771 return NULL; /* non-local */
1773 /* now check if it is the return from a function call */
1774 if (dic->op == CALL || dic->op == PCALL)
1776 if (ic->op != SEND && ic->op != RETURN)
1778 OP_SYMBOL (op)->ruonly = 1;
1784 /* otherwise check that the definition does
1785 not contain any symbols in far space */
1786 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1787 isOperandInFarSpace (IC_RIGHT (dic)) ||
1788 IS_OP_RUONLY (IC_LEFT (ic)) ||
1789 IS_OP_RUONLY (IC_RIGHT (ic)))
1794 /* if pointer set then make sure the pointer is one byte */
1795 if (POINTER_SET (dic))
1798 if (POINTER_GET (dic))
1803 /* also make sure the intervenening instructions
1804 don't have any thing in far space */
1805 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1807 /* if there is an intervening function call then no */
1808 if (dic->op == CALL || dic->op == PCALL)
1810 /* if pointer set then make sure the pointer
1812 if (POINTER_SET (dic))
1815 if (POINTER_GET (dic))
1818 /* if address of & the result is remat the okay */
1819 if (dic->op == ADDRESS_OF &&
1820 OP_SYMBOL (IC_RESULT (dic))->remat)
1823 /* if left or right or result is in far space */
1824 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1825 isOperandInFarSpace (IC_RIGHT (dic)) ||
1826 isOperandInFarSpace (IC_RESULT (dic)) ||
1827 IS_OP_RUONLY (IC_LEFT (dic)) ||
1828 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1829 IS_OP_RUONLY (IC_RESULT (dic)))
1835 OP_SYMBOL (op)->ruonly = 1;
1839 /*-----------------------------------------------------------------*/
1840 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1841 /*-----------------------------------------------------------------*/
1843 isBitwiseOptimizable (iCode * ic)
1845 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1847 /* bitwise operations are considered optimizable
1848 under the following conditions (Jean-Louis VERN)
1860 if (IS_LITERAL (rtype))
1866 Certian assignments involving pointers can be temporarly stored
1877 #if !DISABLE_PACKREGSFORACCUSE
1880 /** Pack registers for acc use.
1881 When the result of this operation is small and short lived it may
1882 be able to be stored in the accumelator.
1885 packRegsForAccUse (iCode * ic)
1889 /* if this is an aggregate, e.g. a one byte char array */
1890 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
1894 /* if + or - then it has to be one byte result */
1895 if ((ic->op == '+' || ic->op == '-')
1896 && getSize (operandType (IC_RESULT (ic))) > 1)
1899 /* if shift operation make sure right side is not a literal */
1900 if (ic->op == RIGHT_OP &&
1901 (isOperandLiteral (IC_RIGHT (ic)) ||
1902 getSize (operandType (IC_RESULT (ic))) > 1))
1905 if (ic->op == LEFT_OP &&
1906 (isOperandLiteral (IC_RIGHT (ic)) ||
1907 getSize (operandType (IC_RESULT (ic))) > 1))
1910 /* has only one definition */
1911 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1914 /* has only one use */
1915 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1918 /* and the usage immediately follows this iCode */
1919 if (!(uic = hTabItemWithKey (iCodehTab,
1920 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1923 if (ic->next != uic)
1926 /* if it is a conditional branch then we definitely can */
1930 if (uic->op == JUMPTABLE)
1934 /* if the usage is not is an assignment or an
1935 arithmetic / bitwise / shift operation then not */
1936 if (POINTER_SET (uic) &&
1937 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1941 if (uic->op != '=' &&
1942 !IS_ARITHMETIC_OP (uic) &&
1943 !IS_BITWISE_OP (uic) &&
1944 uic->op != LEFT_OP &&
1945 uic->op != RIGHT_OP)
1948 /* if used in ^ operation then make sure right is not a
1950 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1953 /* if shift operation make sure right side is not a literal */
1954 if (uic->op == RIGHT_OP &&
1955 (isOperandLiteral (IC_RIGHT (uic)) ||
1956 getSize (operandType (IC_RESULT (uic))) > 1))
1959 if (uic->op == LEFT_OP &&
1960 (isOperandLiteral (IC_RIGHT (uic)) ||
1961 getSize (operandType (IC_RESULT (uic))) > 1))
1965 /* make sure that the result of this icode is not on the
1966 stack, since acc is used to compute stack offset */
1967 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1968 OP_SYMBOL (IC_RESULT (uic))->onStack)
1973 /* if either one of them in far space then we cannot */
1974 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1975 isOperandInFarSpace (IC_LEFT (uic))) ||
1976 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1977 isOperandInFarSpace (IC_RIGHT (uic))))
1981 /* if the usage has only one operand then we can */
1982 if (IC_LEFT (uic) == NULL ||
1983 IC_RIGHT (uic) == NULL)
1986 /* make sure this is on the left side if not
1987 a '+' since '+' is commutative */
1988 if (ic->op != '+' &&
1989 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1992 // See mcs51 ralloc for reasoning
1994 /* if one of them is a literal then we can */
1995 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1996 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2003 /** This is confusing :) Guess for now */
2004 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2005 (IS_ITEMP (IC_RIGHT (uic)) ||
2006 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2009 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2010 (IS_ITEMP (IC_LEFT (uic)) ||
2011 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2015 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2020 packRegsForHLUse (iCode * ic)
2024 /* PENDING: Could do IFX */
2030 /* has only one definition */
2031 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2033 D (D_HLUSE, (" + Dropping as has more than one def\n"));
2037 /* has only one use */
2038 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2040 D (D_HLUSE, (" + Dropping as has more than one use\n"));
2044 /* and the usage immediately follows this iCode */
2045 if (!(uic = hTabItemWithKey (iCodehTab,
2046 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2048 D (D_HLUSE, (" + Dropping as usage isn't in this block\n"));
2052 if (ic->next != uic)
2054 D (D_HLUSE, (" + Dropping as usage doesn't follow this\n"));
2063 if (getSize (operandType (IC_RESULT (ic))) != 2 ||
2064 (IC_LEFT(uic) && getSize (operandType (IC_LEFT (uic))) != 2) ||
2065 (IC_RIGHT(uic) && getSize (operandType (IC_RIGHT (uic))) != 2))
2067 D (D_HLUSE, (" + Dropping as the result size is not 2\n"));
2073 if (ic->op == CAST && uic->op == IPUSH)
2075 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2077 if (ic->op == ADDRESS_OF && POINTER_GET (uic) && IS_ITEMP( IC_RESULT (uic)))
2079 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2084 /* Case of assign a constant to offset in a static array. */
2085 if (ic->op == '+' && IS_VALOP (IC_RIGHT (ic)))
2087 if (uic->op == '=' && POINTER_SET (uic))
2091 else if (uic->op == IPUSH && getSize (operandType (IC_LEFT (uic))) == 2)
2098 D (D_HLUSE, (" + Dropping as it's a bad op\n"));
2101 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_SCRATCH;
2105 packRegsForHLUse3 (iCode * lic, operand * op, eBBlock * ebp)
2110 bool isFirst = TRUE;
2113 printf("Checking:\n");
2117 if ( OP_SYMBOL(op)->accuse)
2122 if (OP_SYMBOL(op)->remat)
2127 /* Only defined once */
2128 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2131 if (getSize (operandType (op)) != 2)
2134 /* And this is the definition */
2135 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2138 /* first check if any overlapping liverange has already been
2140 if (OP_SYMBOL(op)->clashes)
2142 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2144 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2146 sym = hTabItemWithKey(liveRanges,i);
2147 if (sym->accuse == ACCUSE_SCRATCH)
2155 /* Nothing else that clashes with this is using the scratch
2156 register. Scan through all of the intermediate instructions and
2157 see if any of them could nuke HL.
2159 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2161 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2162 ic = hTabNextItem(iCodeSeqhTab,&key))
2166 printf("(op: %u)\n", ic->op);
2171 if (ic->op == ADDRESS_OF)
2173 if (POINTER_GET (ic))
2177 /* Handle the non left/right/result ones first */
2180 if (ic->op == JUMPTABLE)
2186 if (ic->op == IPUSH && isOperandEqual (op, IC_LEFT (ic)))
2189 if ((ic->op == '=' && !POINTER_SET(ic)) ||
2190 ic->op == UNARYMINUS ||
2196 if (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic)))
2199 if (IS_VALOP (IC_RIGHT (ic)) &&
2206 /* By default give up */
2211 printf("Succeeded!\n");
2213 OP_SYMBOL (op)->accuse = ACCUSE_SCRATCH;
2219 packRegsForIYUse (iCode * lic, operand * op, eBBlock * ebp)
2227 printf("Checking IY on %p lic key %u first def %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)));
2231 if ( OP_SYMBOL(op)->accuse)
2236 if (OP_SYMBOL(op)->remat)
2241 /* Only defined once */
2242 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2245 /* And this is the definition */
2246 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2249 /* first check if any overlapping liverange has already been
2251 if (OP_SYMBOL(op)->clashes)
2253 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2255 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2257 sym = hTabItemWithKey(liveRanges,i);
2258 if (sym->accuse == ACCUSE_IY)
2266 /* Only a few instructions can load into IY */
2272 /* Nothing else that clashes with this is using the scratch
2273 register. Scan through all of the intermediate instructions and
2274 see if any of them could nuke HL.
2276 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2279 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2280 ic = hTabNextItem(iCodeSeqhTab,&key))
2284 printf("(op: %u uses %u)\n", ic->op, bitVectBitValue(uses, ic->key));
2287 if (ic->op == PCALL ||
2296 /* Only certain rules will work against IY. Check if this iCode uses
2298 if (bitVectBitValue(uses, ic->key) != 0)
2303 if (ic->op == '=' &&
2304 isOperandEqual(IC_RESULT(ic), op))
2307 if (ic->op == GET_VALUE_AT_ADDRESS &&
2308 isOperandEqual(IC_LEFT(ic), op))
2311 if (isOperandEqual(IC_RESULT(ic), IC_LEFT(ic)) == FALSE)
2314 if (IC_RIGHT (ic) && IS_VALOP (IC_RIGHT (ic)))
2316 if (ic->op == '+' ||
2319 /* Only works if the constant is small */
2320 if (operandLitValue (IC_RIGHT (ic)) < 4)
2332 /* This iCode doesn't use the sym. See if this iCode preserves IY.
2334 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2335 isOperandInDirSpace(IC_RESULT(ic)))
2338 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2339 isOperandInFarSpace(IC_RIGHT(ic)))
2342 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2343 isOperandInFarSpace(IC_LEFT(ic)))
2349 /* By default give up */
2354 printf("Succeeded IY!\n");
2356 OP_SYMBOL (op)->accuse = ACCUSE_IY;
2361 /** Returns TRUE if this operation can use acc and if it preserves the value.
2364 opPreservesA (iCode * uic)
2368 /* If we've gotten this far then the thing to compare must be
2369 small enough and must be in A.
2374 if (uic->op == JUMPTABLE)
2376 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2380 /* A pointer assign preserves A if A is the left value. */
2381 if (uic->op == '=' && POINTER_SET (uic))
2386 /* if the usage has only one operand then we can */
2387 /* PENDING: check */
2388 if (IC_LEFT (uic) == NULL ||
2389 IC_RIGHT (uic) == NULL)
2391 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2395 /* PENDING: check this rule */
2396 if (getSize (operandType (IC_RESULT (uic))) > 1)
2398 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2403 /* Disabled all of the old rules as they weren't verified and have
2404 caused at least one problem.
2409 /** Returns true if this operand preserves the value of A.
2412 opIgnoresA (iCode * ic, iCode * uic)
2414 /* A increment of an iTemp by a constant is OK. */
2415 if ( uic->op == '+' &&
2416 IS_ITEMP (IC_LEFT (uic)) &&
2417 IS_ITEMP (IC_RESULT (uic)) &&
2418 IS_OP_LITERAL (IC_RIGHT (uic)))
2420 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2422 /* Being an ITEMP means that we're already a symbol. */
2424 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2430 else if (uic->op == '=' && !POINTER_SET (uic))
2432 /* If they are equal and get optimised out then things are OK. */
2433 if (isOperandEqual (IC_RESULT (uic), IC_RIGHT (uic)))
2435 /* Straight assign is OK. */
2444 /* Some optimisation cases:
2456 ; genAssign (pointer)
2460 want to optimise down to:
2466 So genPointer get is OK
2467 genPlus where the right is constant, left is iTemp, and result is same as left
2468 genAssign (pointer) is OK
2479 ; genAssign (pointer)
2480 ; AOP_STK for _strcpy_to_1_1
2485 want to optimise down to:
2491 So genIfx where IC_COND has size of 1 and is a constant.
2494 /** Pack registers for acc use.
2495 When the result of this operation is small and short lived it may
2496 be able to be stored in the accumulator.
2498 Note that the 'A preserving' list is currently emperical :)
2501 packRegsForAccUse2 (iCode * ic)
2505 D (D_ACCUSE2, ("packRegsForAccUse2: running on ic %p line %u\n", ic, ic->lineno));
2507 /* Filter out all but those 'good' commands */
2509 !POINTER_GET (ic) &&
2511 !IS_BITWISE_OP (ic) &&
2517 ic->op != GETHBIT &&
2520 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2524 /* if + or - then it has to be one byte result.
2527 if ((ic->op == '+' || ic->op == '-')
2528 && getSize (operandType (IC_RESULT (ic))) > 1)
2530 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2534 /* has only one definition */
2535 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2537 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2541 /* Right. We may be able to propagate it through if:
2542 For each in the chain of uses the intermediate is OK.
2544 /* Get next with 'uses result' bit on
2545 If this->next == next
2546 Validate use of next
2547 If OK, increase count
2549 /* and the usage immediately follows this iCode */
2550 if (!(uic = hTabItemWithKey (iCodehTab,
2551 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2553 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2558 /* Create a copy of the OP_USES bit vect */
2559 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2561 iCode *scan = ic, *next;
2565 setBit = bitVectFirstBit (uses);
2566 next = hTabItemWithKey (iCodehTab, setBit);
2567 if (scan->next == next)
2569 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2571 bitVectUnSetBit (uses, setBit);
2572 /* Still contigous. */
2573 if (!opPreservesA (next))
2575 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2578 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2581 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2583 if (next->prev == NULL)
2585 if (!opPreservesA (next))
2587 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2590 bitVectUnSetBit (uses, setBit);
2595 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2599 else if (scan->next == NULL)
2601 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2602 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2607 if (opIgnoresA (ic, scan->next))
2611 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2615 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2620 while (!bitVectIsZero (uses));
2622 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2627 /** Does some transformations to reduce register pressure.
2630 packRegisters (eBBlock * ebp)
2635 D (D_ALLOC, ("packRegisters: entered.\n"));
2637 while (1 && !DISABLE_PACK_ASSIGN)
2640 /* look for assignments of the form */
2641 /* iTempNN = TRueSym (someoperation) SomeOperand */
2643 /* TrueSym := iTempNN:1 */
2644 for (ic = ebp->sch; ic; ic = ic->next)
2646 /* find assignment of the form TrueSym := iTempNN:1 */
2647 if (ic->op == '=' && !POINTER_SET (ic))
2648 change += packRegsForAssign (ic, ebp);
2654 for (ic = ebp->sch; ic; ic = ic->next)
2656 /* Safe: address of a true sym is always constant. */
2657 /* if this is an itemp & result of a address of a true sym
2658 then mark this as rematerialisable */
2659 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2661 if (ic->op == ADDRESS_OF &&
2662 IS_ITEMP (IC_RESULT (ic)) &&
2663 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2664 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2665 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2668 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2669 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2670 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2673 /* Safe: just propagates the remat flag */
2674 /* if straight assignment then carry remat flag if this is the
2676 if (ic->op == '=' &&
2677 !POINTER_SET (ic) &&
2678 IS_SYMOP (IC_RIGHT (ic)) &&
2679 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2680 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2683 OP_SYMBOL (IC_RESULT (ic))->remat =
2684 OP_SYMBOL (IC_RIGHT (ic))->remat;
2685 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2686 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2689 /* if the condition of an if instruction is defined in the
2690 previous instruction then mark the itemp as a conditional */
2691 if ((IS_CONDITIONAL (ic) ||
2692 ((ic->op == BITWISEAND ||
2695 isBitwiseOptimizable (ic))) &&
2696 ic->next && ic->next->op == IFX &&
2697 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2698 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2699 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2702 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2707 /* reduce for support function calls */
2708 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2709 packRegsForSupport (ic, ebp);
2712 /* some cases the redundant moves can
2713 can be eliminated for return statements */
2714 if (ic->op == RETURN || ic->op == SEND)
2716 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2719 /* if pointer set & left has a size more than
2720 one and right is not in far space */
2721 if (!DISABLE_PACK_ONE_USE &&
2723 /* MLH: no such thing.
2724 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2725 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2726 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2727 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2730 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2733 /* if pointer get */
2734 if (!DISABLE_PACK_ONE_USE &&
2736 /* MLH: dont have far space
2737 !isOperandInFarSpace(IC_RESULT(ic))&& */
2738 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2739 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2740 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2743 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2746 /* pack registers for accumulator use, when the result of an
2747 arithmetic or bit wise operation has only one use, that use is
2748 immediately following the defintion and the using iCode has
2749 only one operand or has two operands but one is literal & the
2750 result of that operation is not on stack then we can leave the
2751 result of this operation in acc:b combination */
2753 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2756 packRegsForHLUse (ic);
2758 packRegsForHLUse3 (ic, IC_RESULT (ic), ebp);
2761 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)) && IS_Z80)
2763 packRegsForIYUse (ic, IC_RESULT (ic), ebp);
2766 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2767 getSize (operandType (IC_RESULT (ic))) == 1)
2769 packRegsForAccUse2 (ic);
2774 /** Joins together two byte constant pushes into one word push.
2777 joinPushes (iCode *lic)
2781 for (ic = lic; ic; ic = ic->next)
2788 /* Anything past this? */
2793 /* This and the next pushes? */
2794 if (ic->op != IPUSH || uic->op != IPUSH)
2798 /* Both literals? */
2799 if ( !IS_OP_LITERAL (IC_LEFT (ic)) || !IS_OP_LITERAL (IC_LEFT (uic)))
2803 /* Both characters? */
2804 if ( getSize (operandType (IC_LEFT (ic))) != 1 || getSize (operandType (IC_LEFT (uic))) != 1)
2808 /* Pull out the values, make a new type, and create the new iCode for it.
2810 first = (int)operandLitValue ( IC_LEFT (ic));
2811 second = (int)operandLitValue ( IC_LEFT (uic));
2813 sprintf (buffer, "%u", ((first << 8) | (second & 0xFF)) & 0xFFFFU);
2814 val = constVal (buffer);
2815 SPEC_NOUN (val->type) = V_INT;
2816 IC_LEFT (ic)->operand.valOperand = val;
2818 /* Now remove the second one from the list. */
2819 ic->next = uic->next;
2822 /* Patch up the reverse link */
2823 uic->next->prev = ic;
2830 /*-----------------------------------------------------------------*/
2831 /* assignRegisters - assigns registers to each live range as need */
2832 /*-----------------------------------------------------------------*/
2834 z80_assignRegisters (eBBlock ** ebbs, int count)
2839 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2841 setToNull ((void *) &_G.funcrUsed);
2842 _G.stackExtend = _G.dataExtend = 0;
2846 /* DE is required for the code gen. */
2847 _G.nRegs = GBZ80_MAX_REGS;
2848 regsZ80 = _gbz80_regs;
2852 _G.nRegs = Z80_MAX_REGS;
2853 regsZ80 = _z80_regs;
2856 /* change assignments this will remove some
2857 live ranges reducing some register pressure */
2858 for (i = 0; i < count; i++)
2859 packRegisters (ebbs[i]);
2861 if (options.dump_pack)
2862 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2864 /* first determine for each live range the number of
2865 registers & the type of registers required for each */
2868 /* and serially allocate registers */
2869 serialRegAssign (ebbs, count);
2871 /* if stack was extended then tell the user */
2874 /* werror(W_TOOMANY_SPILS,"stack", */
2875 /* _G.stackExtend,currFunc->name,""); */
2881 /* werror(W_TOOMANY_SPILS,"data space", */
2882 /* _G.dataExtend,currFunc->name,""); */
2886 if (options.dump_rassgn) {
2887 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2888 dumpLiveRanges (DUMP_LRANGE, liveRanges);
2891 /* after that create the register mask
2892 for each of the instruction */
2893 createRegMask (ebbs, count);
2895 /* now get back the chain */
2896 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2898 ic = joinPushes (ic);
2900 /* redo that offsets for stacked automatic variables */
2901 redoStackOffsets ();
2905 /* free up any stackSpil locations allocated */
2906 applyToSet (_G.stackSpil, deallocStackSpil);
2908 setToNull ((void **) &_G.stackSpil);
2909 setToNull ((void **) &_G.spiltSet);
2910 /* mark all registers as free */