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,
74 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
79 #define DISABLE_PACKREGSFORSUPPORT 1
80 #define DISABLE_PACKREGSFORACCUSE 1
82 extern void genZ80Code (iCode *);
84 /** Local static variables */
90 bitVect *totRegAssigned; /* final set of LRs that got into registers */
93 /* registers used in a function */
100 static regs _gbz80_regs[] =
102 {REG_GPR, C_IDX, "c", 1},
103 {REG_GPR, B_IDX, "b", 1},
104 {REG_CND, CND_IDX, "c", 1}
107 static regs _z80_regs[] =
109 {REG_GPR, C_IDX, "c", 1},
110 {REG_GPR, B_IDX, "b", 1},
111 {REG_GPR, E_IDX, "e", 1},
112 {REG_GPR, D_IDX, "d", 1},
113 {REG_CND, CND_IDX, "c", 1}
118 /** Number of usable registers (all but C) */
119 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
120 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
122 static void spillThis (symbol *);
123 static void freeAllRegs ();
125 /** Allocates register of given type.
126 'type' is not used on the z80 version. It was used to select
127 between pointer and general purpose registers on the mcs51 version.
129 @return Pointer to the newly allocated register.
132 allocReg (short type)
136 for (i = 0; i < _G.nRegs; i++)
138 /* For now we allocate from any free */
139 if (regsZ80[i].isFree)
141 regsZ80[i].isFree = 0;
144 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
146 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
150 D (D_ALLOC, ("allocReg: No free.\n"));
154 /** Returns pointer to register wit index number
161 for (i = 0; i < _G.nRegs; i++)
163 if (regsZ80[i].rIdx == idx)
169 wassertl (0, "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 < _G.nRegs; i++)
194 /* For now only one reg type */
195 if (regsZ80[i].isFree)
203 /** Free registers with type.
206 nfreeRegsType (int type)
211 if ((nfr = nFreeRegs (type)) == 0)
213 return nFreeRegs (REG_GPR);
217 return nFreeRegs (type);
220 /*-----------------------------------------------------------------*/
221 /* useReg - marks a register as used */
222 /*-----------------------------------------------------------------*/
230 /*-----------------------------------------------------------------*/
231 /* allDefsOutOfRange - all definitions are out of a range */
232 /*-----------------------------------------------------------------*/
234 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
241 for (i = 0; i < defs->size; i++)
245 if (bitVectBitValue (defs, i) &&
246 (ic = hTabItemWithKey (iCodehTab, i)) &&
247 (ic->seq >= fseq && ic->seq <= toseq))
257 /*-----------------------------------------------------------------*/
258 /* computeSpillable - given a point find the spillable live ranges */
259 /*-----------------------------------------------------------------*/
261 computeSpillable (iCode * ic)
265 /* spillable live ranges are those that are live at this
266 point . the following categories need to be subtracted
268 a) - those that are already spilt
269 b) - if being used by this one
270 c) - defined by this one */
272 spillable = bitVectCopy (ic->rlive);
274 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
276 bitVectCplAnd (spillable, ic->uses); /* used in this one */
277 bitVectUnSetBit (spillable, ic->defKey);
278 spillable = bitVectIntersect (spillable, _G.regAssigned);
283 /*-----------------------------------------------------------------*/
284 /* noSpilLoc - return true if a variable has no spil location */
285 /*-----------------------------------------------------------------*/
287 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
289 return (sym->usl.spillLoc ? 0 : 1);
292 /*-----------------------------------------------------------------*/
293 /* hasSpilLoc - will return 1 if the symbol has spil location */
294 /*-----------------------------------------------------------------*/
296 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
298 return (sym->usl.spillLoc ? 1 : 0);
301 /** Will return 1 if the remat flag is set.
302 A symbol is rematerialisable if it doesnt need to be allocated
303 into registers at creation as it can be re-created at any time -
304 i.e. it's constant in some way.
307 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
312 /*-----------------------------------------------------------------*/
313 /* allLRs - return true for all */
314 /*-----------------------------------------------------------------*/
316 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
321 /** liveRangesWith - applies function to a given set of live range
324 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
325 eBBlock * ebp, iCode * ic)
330 if (!lrs || !lrs->size)
333 for (i = 1; i < lrs->size; i++)
336 if (!bitVectBitValue (lrs, i))
339 /* if we don't find it in the live range
340 hash table we are in serious trouble */
341 if (!(sym = hTabItemWithKey (liveRanges, i)))
343 wassertl (0, "liveRangesWith could not find liveRange");
347 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
349 addSetHead (&rset, sym);
357 /** leastUsedLR - given a set determines which is the least used
360 leastUsedLR (set * sset)
362 symbol *sym = NULL, *lsym = NULL;
364 sym = lsym = setFirstItem (sset);
369 for (; lsym; lsym = setNextItem (sset))
372 /* if usage is the same then prefer
373 the spill the smaller of the two */
374 if (lsym->used == sym->used)
375 if (getSize (lsym->type) < getSize (sym->type))
379 if (lsym->used < sym->used)
384 setToNull ((void **) &sset);
389 /** noOverLap - will iterate through the list looking for over lap
392 noOverLap (set * itmpStack, symbol * fsym)
396 for (sym = setFirstItem (itmpStack); sym;
397 sym = setNextItem (itmpStack))
399 if (bitVectBitValue(sym->clashes,fsym->key))
402 // if sym starts before (or on) our end point
403 // and ends after (or on) our start point,
405 if (sym->liveFrom <= fsym->liveTo &&
406 sym->liveTo >= fsym->liveFrom)
415 /*-----------------------------------------------------------------*/
416 /* isFree - will return 1 if the a free spil location is found */
417 /*-----------------------------------------------------------------*/
421 V_ARG (symbol **, sloc);
422 V_ARG (symbol *, fsym);
424 /* if already found */
428 /* if it is free && and the itmp assigned to
429 this does not have any overlapping live ranges
430 with the one currently being assigned and
431 the size can be accomodated */
433 noOverLap (sym->usl.itmpStack, fsym) &&
434 getSize (sym->type) >= getSize (fsym->type))
443 /*-----------------------------------------------------------------*/
444 /* createStackSpil - create a location on the stack to spil */
445 /*-----------------------------------------------------------------*/
447 createStackSpil (symbol * sym)
451 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
453 /* first go try and find a free one that is already
454 existing on the stack */
455 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
457 /* found a free one : just update & return */
458 sym->usl.spillLoc = sloc;
461 addSetHead (&sloc->usl.itmpStack, sym);
462 D (D_ALLOC, ("createStackSpil: found existing\n"));
466 /* could not then have to create one , this is the hard part
467 we need to allocate this on the stack : this is really a
468 hack!! but cannot think of anything better at this time */
470 sprintf (buffer, "sloc%d", _G.slocNum++);
471 sloc = newiTemp (buffer);
473 /* set the type to the spilling symbol */
474 sloc->type = copyLinkChain (sym->type);
475 sloc->etype = getSpec (sloc->type);
476 SPEC_SCLS (sloc->etype) = S_AUTO;
477 SPEC_EXTR (sloc->etype) = 0;
478 SPEC_STAT (sloc->etype) = 0;
479 SPEC_VOLATILE(sloc->etype) = 0;
483 sloc->isref = 1; /* to prevent compiler warning */
485 /* if it is on the stack then update the stack */
486 if (IN_STACK (sloc->etype))
488 currFunc->stack += getSize (sloc->type);
489 _G.stackExtend += getSize (sloc->type);
493 _G.dataExtend += getSize (sloc->type);
496 /* add it to the stackSpil set */
497 addSetHead (&_G.stackSpil, sloc);
498 sym->usl.spillLoc = sloc;
501 /* add it to the set of itempStack set
502 of the spill location */
503 addSetHead (&sloc->usl.itmpStack, sym);
505 D (D_ALLOC, ("createStackSpil: created new\n"));
509 /*-----------------------------------------------------------------*/
510 /* spillThis - spils a specific operand */
511 /*-----------------------------------------------------------------*/
513 spillThis (symbol * sym)
517 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
519 /* if this is rematerializable or has a spillLocation
520 we are okay, else we need to create a spillLocation
522 if (!(sym->remat || sym->usl.spillLoc))
524 createStackSpil (sym);
527 /* mark it has spilt & put it in the spilt set */
528 sym->isspilt = sym->spillA = 1;
529 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
531 bitVectUnSetBit (_G.regAssigned, sym->key);
532 bitVectUnSetBit (_G.totRegAssigned, sym->key);
534 for (i = 0; i < sym->nRegs; i++)
538 freeReg (sym->regs[i]);
543 if (sym->usl.spillLoc && !sym->remat)
545 sym->usl.spillLoc->allocreq++;
551 /*-----------------------------------------------------------------*/
552 /* allDefsOutOfRange - all definitions are out of a range */
553 /*-----------------------------------------------------------------*/
555 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
562 for (i = 0; i < defs->size; i++)
566 if (bitVectBitValue (defs, i) &&
567 (ic = hTabItemWithKey (iCodehTab, i)) &&
568 (ic->seq >= fseq && ic->seq <= toseq))
577 /*-----------------------------------------------------------------*/
578 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
579 /* but is not used as a pointer */
580 /*-----------------------------------------------------------------*/
582 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
584 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
587 /*-----------------------------------------------------------------*/
588 /* notUsedInBlock - not used in this block */
589 /*-----------------------------------------------------------------*/
591 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
593 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
594 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
595 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
598 /*-----------------------------------------------------------------*/
599 /* notUsedInRemaining - not used or defined in remain of the block */
600 /*-----------------------------------------------------------------*/
602 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
604 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
605 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
609 /** Select a iTemp to spil : rather a simple procedure.
612 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
614 bitVect *lrcs = NULL;
618 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
619 /* get the spillable live ranges */
620 lrcs = computeSpillable (ic);
622 /* get all live ranges that are rematerizable */
623 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
625 D (D_ALLOC, ("selectSpil: using remat.\n"));
626 /* return the least used of these */
627 return leastUsedLR (selectS);
631 /* get live ranges with spillLocations in direct space */
632 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
634 sym = leastUsedLR (selectS);
635 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
636 sym->usl.spillLoc->rname :
637 sym->usl.spillLoc->name));
639 /* mark it as allocation required */
640 sym->usl.spillLoc->allocreq++;
644 /* if the symbol is local to the block then */
645 if (forSym->liveTo < ebp->lSeq)
648 /* check if there are any live ranges allocated
649 to registers that are not used in this block */
650 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
652 sym = leastUsedLR (selectS);
653 /* if this is not rematerializable */
657 wassertl (0, "Attempted to do an unsupported block spill");
663 /* check if there are any live ranges that not
664 used in the remainder of the block */
665 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
667 sym = leastUsedLR (selectS);
672 wassertl (0, "Attempted to do an unsupported remain spill");
680 /* find live ranges with spillocation && not used as pointers */
681 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
684 sym = leastUsedLR (selectS);
685 /* mark this as allocation required */
686 sym->usl.spillLoc->allocreq++;
691 /* find live ranges with spillocation */
692 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
694 D (D_ALLOC, ("selectSpil: using with spill.\n"));
695 sym = leastUsedLR (selectS);
696 sym->usl.spillLoc->allocreq++;
700 /* couldn't find then we need to create a spil
701 location on the stack , for which one? the least
703 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
705 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
706 /* return a created spil location */
707 sym = createStackSpil (leastUsedLR (selectS));
708 sym->usl.spillLoc->allocreq++;
712 /* this is an extreme situation we will spill
713 this one : happens very rarely but it does happen */
714 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
720 /** Spil some variable & mark registers as free.
721 A spill occurs when an iTemp wont fit into the available registers.
724 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
729 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
731 /* get something we can spil */
732 ssym = selectSpil (ic, ebp, forSym);
734 /* mark it as spilt */
735 ssym->isspilt = ssym->spillA = 1;
736 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
738 /* mark it as not register assigned &
739 take it away from the set */
740 bitVectUnSetBit (_G.regAssigned, ssym->key);
741 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
743 /* mark the registers as free */
744 for (i = 0; i < ssym->nRegs; i++)
746 freeReg (ssym->regs[i]);
748 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
749 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
751 /* if spilt on stack then free up r0 & r1
752 if they could have been assigned to as gprs */
753 if (!ptrRegReq && isSpiltOnStack (ssym))
756 spillLRWithPtrReg (ssym);
759 /* if this was a block level spil then insert push & pop
760 at the start & end of block respectively */
763 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
764 /* add push to the start of the block */
765 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
766 ebp->sch->next : ebp->sch));
767 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
768 /* add pop to the end of the block */
769 addiCodeToeBBlock (ebp, nic, NULL);
772 /* if spilt because not used in the remainder of the
773 block then add a push before this instruction and
774 a pop at the end of the block */
775 if (ssym->remainSpil)
778 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
779 /* add push just before this instruction */
780 addiCodeToeBBlock (ebp, nic, ic);
782 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
783 /* add pop to the end of the block */
784 addiCodeToeBBlock (ebp, nic, NULL);
788 D (D_ALLOC, ("spilSomething: done.\n"));
796 /** Will try for GPR if not spil.
799 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
803 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
805 /* try for gpr type */
806 if ((reg = allocReg (REG_GPR)))
808 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
812 /* we have to spil */
813 if (!spilSomething (ic, ebp, sym))
815 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
819 /* this looks like an infinite loop but
820 in really selectSpil will abort */
824 static regs *getRegGprNoSpil()
828 /* try for gpr type */
829 if ((reg = allocReg (REG_GPR)))
831 D (D_ALLOC, ("getRegGprNoSpil: got a reg.\n"));
837 /** Symbol has a given register.
840 symHasReg (symbol * sym, regs * reg)
844 for (i = 0; i < sym->nRegs; i++)
845 if (sym->regs[i] == reg)
851 /** Check the live to and if they have registers & are not spilt then
852 free up the registers
855 deassignLRs (iCode * ic, eBBlock * ebp)
861 for (sym = hTabFirstItem (liveRanges, &k); sym;
862 sym = hTabNextItem (liveRanges, &k))
866 /* if it does not end here */
867 if (sym->liveTo > ic->seq)
870 /* if it was spilt on stack then we can
871 mark the stack spil location as free */
876 sym->usl.spillLoc->isFree = 1;
882 if (!bitVectBitValue (_G.regAssigned, sym->key))
885 /* special case check if this is an IFX &
886 the privious one was a pop and the
887 previous one was not spilt then keep track
889 if (ic->op == IFX && ic->prev &&
890 ic->prev->op == IPOP &&
891 !ic->prev->parmPush &&
892 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
893 psym = OP_SYMBOL (IC_LEFT (ic->prev));
895 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
901 bitVectUnSetBit (_G.regAssigned, sym->key);
903 /* if the result of this one needs registers
904 and does not have it then assign it right
906 if (IC_RESULT (ic) &&
907 !(SKIP_IC2 (ic) || /* not a special icode */
908 ic->op == JUMPTABLE ||
913 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
914 result->liveTo > ic->seq && /* and will live beyond this */
915 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
916 result->regType == sym->regType && /* same register types */
917 result->nRegs && /* which needs registers */
918 !result->isspilt && /* and does not already have them */
920 !bitVectBitValue (_G.regAssigned, result->key) &&
921 /* the number of free regs + number of regs in this LR
922 can accomodate the what result Needs */
923 ((nfreeRegsType (result->regType) +
924 sym->nRegs) >= result->nRegs)
927 for (i = 0; i < result->nRegs; i++)
930 result->regs[i] = sym->regs[i];
932 result->regs[i] = getRegGpr (ic, ebp, result);
934 /* if the allocation falied which means
935 this was spilt then break */
936 if (!result->regs[i])
944 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
945 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
948 /* free the remaining */
949 for (; i < sym->nRegs; i++)
953 if (!symHasReg (psym, sym->regs[i]))
954 freeReg (sym->regs[i]);
957 freeReg (sym->regs[i]);
958 // sym->regs[i] = NULL;
965 /** Reassign this to registers.
968 reassignLR (operand * op)
970 symbol *sym = OP_SYMBOL (op);
973 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
975 /* not spilt any more */
976 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
977 bitVectUnSetBit (_G.spiltSet, sym->key);
979 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
980 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
984 for (i = 0; i < sym->nRegs; i++)
985 sym->regs[i]->isFree = 0;
988 /** Determines if allocating will cause a spill.
991 willCauseSpill (int nr, int rt)
993 /* first check if there are any avlb registers
994 of te type required */
995 if (nFreeRegs (0) >= nr)
998 /* it will cause a spil */
1002 /** The allocator can allocate same registers to result and operand,
1003 if this happens make sure they are in the same position as the operand
1004 otherwise chaos results.
1007 positionRegs (symbol * result, symbol * opsym)
1009 int count = min (result->nRegs, opsym->nRegs);
1010 int i, j = 0, shared = 0;
1013 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
1015 /* if the result has been spilt then cannot share */
1020 /* first make sure that they actually share */
1021 for (i = 0; i < count; i++)
1023 for (j = 0; j < count; j++)
1025 if (result->regs[i] == opsym->regs[j] && i != j)
1035 regs *tmp = result->regs[i];
1036 result->regs[i] = result->regs[j];
1037 result->regs[j] = tmp;
1044 /** Try to allocate a pair of registers to the symbol.
1047 tryAllocatingRegPair (symbol * sym)
1050 wassert (sym->nRegs == 2);
1051 for (i = 0; i < _G.nRegs; i += 2)
1053 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1055 regsZ80[i].isFree = 0;
1056 sym->regs[0] = ®sZ80[i];
1057 regsZ80[i + 1].isFree = 0;
1058 sym->regs[1] = ®sZ80[i + 1];
1059 sym->regType = REG_PAIR;
1063 currFunc->regsUsed =
1064 bitVectSetBit (currFunc->regsUsed, i);
1065 currFunc->regsUsed =
1066 bitVectSetBit (currFunc->regsUsed, i + 1);
1068 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1072 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1076 /** Serially allocate registers to the variables.
1077 This is the main register allocation function. It is called after
1081 serialRegAssign (eBBlock ** ebbs, int count)
1085 /* for all blocks */
1086 for (i = 0; i < count; i++)
1091 if (ebbs[i]->noPath &&
1092 (ebbs[i]->entryLabel != entryLabel &&
1093 ebbs[i]->entryLabel != returnLabel))
1096 /* of all instructions do */
1097 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1100 /* if this is an ipop that means some live
1101 range will have to be assigned again */
1105 reassignLR (IC_LEFT (ic));
1108 /* if result is present && is a true symbol */
1109 if (IC_RESULT (ic) && ic->op != IFX &&
1110 IS_TRUE_SYMOP (IC_RESULT (ic)))
1111 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1113 /* take away registers from live
1114 ranges that end at this instruction */
1115 deassignLRs (ic, ebbs[i]);
1117 /* some don't need registers */
1118 /* MLH: removed RESULT and POINTER_SET condition */
1119 if (SKIP_IC2 (ic) ||
1120 ic->op == JUMPTABLE ||
1126 /* now we need to allocate registers only for the result */
1129 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1134 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1136 /* if it does not need or is spilt
1137 or is already assigned to registers
1138 or will not live beyond this instructions */
1141 bitVectBitValue (_G.regAssigned, sym->key) ||
1142 sym->liveTo <= ic->seq)
1144 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1148 /* if some liverange has been spilt at the block level
1149 and this one live beyond this block then spil this
1151 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1153 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1157 /* if trying to allocate this will cause
1158 a spill and there is nothing to spill
1159 or this one is rematerializable then
1161 willCS = willCauseSpill (sym->nRegs, sym->regType);
1162 spillable = computeSpillable (ic);
1164 (willCS && bitVectIsZero (spillable)))
1167 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1173 /* if it has a spillocation & is used less than
1174 all other live ranges then spill this */
1176 if (sym->usl.spillLoc) {
1177 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1178 allLRs, ebbs[i], ic));
1179 if (leastUsed && leastUsed->used > sym->used) {
1184 /* if none of the liveRanges have a spillLocation then better
1185 to spill this one than anything else already assigned to registers */
1186 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1187 /* if this is local to this block then we might find a block spil */
1188 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1196 /* else we assign registers to it */
1197 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1198 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1200 /* Special case: Try to fit into a reg pair if
1202 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1203 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1208 for (j = 0; j < sym->nRegs; j++)
1210 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1212 /* if the allocation falied which means
1213 this was spilt then break */
1216 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1221 /* if it shares registers with operands make sure
1222 that they are in the same position */
1223 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1224 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1225 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1226 OP_SYMBOL (IC_LEFT (ic)));
1227 /* do the same for the right operand */
1228 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1229 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1230 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1231 OP_SYMBOL (IC_RIGHT (ic)));
1238 /*-----------------------------------------------------------------*/
1239 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1240 /*-----------------------------------------------------------------*/
1241 static void fillGaps()
1246 if (getenv("DISABLE_FILL_GAPS")) return;
1248 /* look for livernages that was spilt by the allocator */
1249 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1250 sym = hTabNextItem(liveRanges,&key)) {
1255 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1257 /* find the liveRanges this one clashes with, that are
1258 still assigned to registers & mark the registers as used*/
1259 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1263 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1264 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1267 assert (clr = hTabItemWithKey(liveRanges,i));
1269 /* mark these registers as used */
1270 for (k = 0 ; k < clr->nRegs ; k++ )
1271 useReg(clr->regs[k]);
1274 if (willCauseSpill(sym->nRegs,sym->regType)) {
1275 /* NOPE :( clear all registers & and continue */
1280 /* THERE IS HOPE !!!! */
1281 for (i=0; i < sym->nRegs ; i++ ) {
1282 sym->regs[i] = getRegGprNoSpil ();
1285 /* for all its definitions check if the registers
1286 allocated needs positioning NOTE: we can position
1287 only ONCE if more than One positioning required
1290 for (i = 0 ; i < sym->defs->size ; i++ ) {
1291 if (bitVectBitValue(sym->defs,i)) {
1293 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1294 if (SKIP_IC(ic)) continue;
1295 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1296 /* if left is assigned to registers */
1297 if (IS_SYMOP(IC_LEFT(ic)) &&
1298 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1299 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1301 if (IS_SYMOP(IC_RIGHT(ic)) &&
1302 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1303 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1305 if (pdone > 1) break;
1308 /* had to position more than once GIVE UP */
1310 /* UNDO all the changes we made to try this */
1312 for (i=0; i < sym->nRegs ; i++ ) {
1313 sym->regs[i] = NULL;
1316 D(D_FILL_GAPS,("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1319 D(D_FILL_GAPS,("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1320 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1321 sym->isspilt = sym->spillA = 0 ;
1322 sym->usl.spillLoc->allocreq--;
1327 /*-----------------------------------------------------------------*/
1328 /* rUmaskForOp :- returns register mask for an operand */
1329 /*-----------------------------------------------------------------*/
1331 rUmaskForOp (operand * op)
1337 /* only temporaries are assigned registers */
1341 sym = OP_SYMBOL (op);
1343 /* if spilt or no registers assigned to it
1345 if (sym->isspilt || !sym->nRegs)
1348 rumask = newBitVect (_G.nRegs);
1350 for (j = 0; j < sym->nRegs; j++)
1352 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1359 z80_rUmaskForOp (operand * op)
1361 return rUmaskForOp (op);
1364 /** Returns bit vector of registers used in iCode.
1367 regsUsedIniCode (iCode * ic)
1369 bitVect *rmask = newBitVect (_G.nRegs);
1371 /* do the special cases first */
1374 rmask = bitVectUnion (rmask,
1375 rUmaskForOp (IC_COND (ic)));
1379 /* for the jumptable */
1380 if (ic->op == JUMPTABLE)
1382 rmask = bitVectUnion (rmask,
1383 rUmaskForOp (IC_JTCOND (ic)));
1388 /* of all other cases */
1390 rmask = bitVectUnion (rmask,
1391 rUmaskForOp (IC_LEFT (ic)));
1395 rmask = bitVectUnion (rmask,
1396 rUmaskForOp (IC_RIGHT (ic)));
1399 rmask = bitVectUnion (rmask,
1400 rUmaskForOp (IC_RESULT (ic)));
1406 /** For each instruction will determine the regsUsed.
1409 createRegMask (eBBlock ** ebbs, int count)
1413 /* for all blocks */
1414 for (i = 0; i < count; i++)
1418 if (ebbs[i]->noPath &&
1419 (ebbs[i]->entryLabel != entryLabel &&
1420 ebbs[i]->entryLabel != returnLabel))
1423 /* for all instructions */
1424 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1429 if (SKIP_IC2 (ic) || !ic->rlive)
1432 /* first mark the registers used in this
1434 ic->rUsed = regsUsedIniCode (ic);
1435 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1437 /* now create the register mask for those
1438 registers that are in use : this is a
1439 super set of ic->rUsed */
1440 ic->rMask = newBitVect (_G.nRegs + 1);
1442 /* for all live Ranges alive at this point */
1443 for (j = 1; j < ic->rlive->size; j++)
1448 /* if not alive then continue */
1449 if (!bitVectBitValue (ic->rlive, j))
1452 /* find the live range we are interested in */
1453 if (!(sym = hTabItemWithKey (liveRanges, j)))
1455 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1456 "createRegMask cannot find live range");
1460 /* if no register assigned to it */
1461 if (!sym->nRegs || sym->isspilt)
1464 /* for all the registers allocated to it */
1465 for (k = 0; k < sym->nRegs; k++)
1468 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1474 /** Returns the rematerialized string for a remat var.
1477 rematStr (symbol * sym)
1480 iCode *ic = sym->rematiCode;
1485 /* if plus or minus print the right hand side */
1486 if (ic->op == '+' || ic->op == '-')
1488 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1491 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1494 /* we reached the end */
1495 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1502 /*-----------------------------------------------------------------*/
1503 /* regTypeNum - computes the type & number of registers required */
1504 /*-----------------------------------------------------------------*/
1511 /* for each live range do */
1512 for (sym = hTabFirstItem (liveRanges, &k); sym;
1513 sym = hTabNextItem (liveRanges, &k))
1516 /* if used zero times then no registers needed */
1517 if ((sym->liveTo - sym->liveFrom) == 0)
1520 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1522 /* if the live range is a temporary */
1526 /* if the type is marked as a conditional */
1527 if (sym->regType == REG_CND)
1530 /* if used in return only then we don't
1532 if (sym->ruonly || sym->accuse)
1534 if (IS_AGGREGATE (sym->type) || sym->isptr)
1535 sym->type = aggrToPtr (sym->type, FALSE);
1539 /* if not then we require registers */
1540 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1541 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1542 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1543 getSize (sym->type));
1544 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1546 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1550 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1551 printTypeChain (sym->type, stderr);
1552 fprintf (stderr, "\n");
1555 /* determine the type of register required */
1556 /* Always general purpose */
1557 sym->regType = REG_GPR;
1562 /* for the first run we don't provide */
1563 /* registers for true symbols we will */
1564 /* see how things go */
1565 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1572 /** Mark all registers as free.
1579 D (D_ALLOC, ("freeAllRegs: running.\n"));
1581 for (i = 0; i < _G.nRegs; i++)
1582 regsZ80[i].isFree = 1;
1585 /*-----------------------------------------------------------------*/
1586 /* deallocStackSpil - this will set the stack pointer back */
1587 /*-----------------------------------------------------------------*/
1588 DEFSETFUNC (deallocStackSpil)
1596 /** Register reduction for assignment.
1599 packRegsForAssign (iCode * ic, eBBlock * ebp)
1603 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1605 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1606 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1607 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1612 /* find the definition of iTempNN scanning backwards if we find a
1613 a use of the true symbol in before we find the definition then
1615 for (dic = ic->prev; dic; dic = dic->prev)
1617 /* PENDING: Don't pack across function calls. */
1618 if (dic->op == CALL || dic->op == PCALL)
1627 if (IS_SYMOP (IC_RESULT (dic)) &&
1628 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1633 if (IS_SYMOP (IC_RIGHT (dic)) &&
1634 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1635 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1641 if (IS_SYMOP (IC_LEFT (dic)) &&
1642 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1643 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1651 return 0; /* did not find */
1653 /* if the result is on stack or iaccess then it must be
1654 the same atleast one of the operands */
1655 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1656 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1658 /* the operation has only one symbol
1659 operator then we can pack */
1660 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1661 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1664 if (!((IC_LEFT (dic) &&
1665 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1667 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1671 /* found the definition */
1672 /* replace the result with the result of */
1673 /* this assignment and remove this assignment */
1674 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1675 IC_RESULT (dic) = IC_RESULT (ic);
1677 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1679 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1681 /* delete from liverange table also
1682 delete from all the points inbetween and the new
1684 for (sic = dic; sic != ic; sic = sic->next)
1686 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1687 if (IS_ITEMP (IC_RESULT (dic)))
1688 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1691 remiCodeFromeBBlock (ebp, ic);
1692 // PENDING: Check vs mcs51
1693 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1694 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1695 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1699 /** Scanning backwards looks for first assig found.
1702 findAssignToSym (operand * op, iCode * ic)
1706 for (dic = ic->prev; dic; dic = dic->prev)
1709 /* if definition by assignment */
1710 if (dic->op == '=' &&
1711 !POINTER_SET (dic) &&
1712 IC_RESULT (dic)->key == op->key)
1713 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1716 /* we are interested only if defined in far space */
1717 /* or in stack space in case of + & - */
1719 /* if assigned to a non-symbol then return
1721 if (!IS_SYMOP (IC_RIGHT (dic)))
1724 /* if the symbol is in far space then
1726 if (isOperandInFarSpace (IC_RIGHT (dic)))
1729 /* for + & - operations make sure that
1730 if it is on the stack it is the same
1731 as one of the three operands */
1732 if ((ic->op == '+' || ic->op == '-') &&
1733 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1736 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1737 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1738 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1746 /* if we find an usage then we cannot delete it */
1747 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1750 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1753 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1757 /* now make sure that the right side of dic
1758 is not defined between ic & dic */
1761 iCode *sic = dic->next;
1763 for (; sic != ic; sic = sic->next)
1764 if (IC_RESULT (sic) &&
1765 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1774 #if !DISABLE_PACKREGSFORSUPPORT
1777 /*-----------------------------------------------------------------*/
1778 /* packRegsForSupport :- reduce some registers for support calls */
1779 /*-----------------------------------------------------------------*/
1781 packRegsForSupport (iCode * ic, eBBlock * ebp)
1784 /* for the left & right operand :- look to see if the
1785 left was assigned a true symbol in far space in that
1786 case replace them */
1787 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1789 if (IS_ITEMP (IC_LEFT (ic)) &&
1790 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1792 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1798 /* found it we need to remove it from the
1800 for (sic = dic; sic != ic; sic = sic->next)
1801 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1803 IC_LEFT (ic)->operand.symOperand =
1804 IC_RIGHT (dic)->operand.symOperand;
1805 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1806 remiCodeFromeBBlock (ebp, dic);
1807 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1808 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1809 // PENDING: Check vs mcs51
1813 /* do the same for the right operand */
1816 IS_ITEMP (IC_RIGHT (ic)) &&
1817 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1819 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1825 /* found it we need to remove it from the block */
1826 for (sic = dic; sic != ic; sic = sic->next)
1827 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1829 IC_RIGHT (ic)->operand.symOperand =
1830 IC_RIGHT (dic)->operand.symOperand;
1831 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1833 remiCodeFromeBBlock (ebp, dic);
1834 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1835 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1836 // PENDING: vs mcs51
1844 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1846 /** Will reduce some registers for single use.
1849 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1855 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1857 /* if returning a literal then do nothing */
1861 /* only upto 2 bytes since we cannot predict
1862 the usage of b, & acc */
1863 if (getSize (operandType (op)) > 2)
1866 if (ic->op != RETURN &&
1870 /* this routine will mark the a symbol as used in one
1871 instruction use only && if the defintion is local
1872 (ie. within the basic block) && has only one definition &&
1873 that definiion is either a return value from a
1874 function or does not contain any variables in
1876 uses = bitVectCopy (OP_USES (op));
1877 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1878 if (!bitVectIsZero (uses)) /* has other uses */
1881 /* if it has only one defintion */
1882 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1883 return NULL; /* has more than one definition */
1885 /* get the that definition */
1887 hTabItemWithKey (iCodehTab,
1888 bitVectFirstBit (OP_DEFS (op)))))
1891 /* found the definition now check if it is local */
1892 if (dic->seq < ebp->fSeq ||
1893 dic->seq > ebp->lSeq)
1894 return NULL; /* non-local */
1896 /* now check if it is the return from a function call */
1897 if (dic->op == CALL || dic->op == PCALL)
1899 if (ic->op != SEND && ic->op != RETURN)
1901 OP_SYMBOL (op)->ruonly = 1;
1907 /* otherwise check that the definition does
1908 not contain any symbols in far space */
1909 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1910 isOperandInFarSpace (IC_RIGHT (dic)) ||
1911 IS_OP_RUONLY (IC_LEFT (ic)) ||
1912 IS_OP_RUONLY (IC_RIGHT (ic)))
1917 /* if pointer set then make sure the pointer is one byte */
1918 if (POINTER_SET (dic))
1921 if (POINTER_GET (dic))
1926 /* also make sure the intervenening instructions
1927 don't have any thing in far space */
1928 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1930 /* if there is an intervening function call then no */
1931 if (dic->op == CALL || dic->op == PCALL)
1933 /* if pointer set then make sure the pointer
1935 if (POINTER_SET (dic))
1938 if (POINTER_GET (dic))
1941 /* if address of & the result is remat the okay */
1942 if (dic->op == ADDRESS_OF &&
1943 OP_SYMBOL (IC_RESULT (dic))->remat)
1946 /* if left or right or result is in far space */
1947 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1948 isOperandInFarSpace (IC_RIGHT (dic)) ||
1949 isOperandInFarSpace (IC_RESULT (dic)) ||
1950 IS_OP_RUONLY (IC_LEFT (dic)) ||
1951 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1952 IS_OP_RUONLY (IC_RESULT (dic)))
1958 OP_SYMBOL (op)->ruonly = 1;
1962 /*-----------------------------------------------------------------*/
1963 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1964 /*-----------------------------------------------------------------*/
1966 isBitwiseOptimizable (iCode * ic)
1968 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1970 /* bitwise operations are considered optimizable
1971 under the following conditions (Jean-Louis VERN)
1983 if (IS_LITERAL (rtype))
1989 Certian assignments involving pointers can be temporarly stored
2000 #if !DISABLE_PACKREGSFORACCUSE
2003 /** Pack registers for acc use.
2004 When the result of this operation is small and short lived it may
2005 be able to be stored in the accumelator.
2008 packRegsForAccUse (iCode * ic)
2012 /* if this is an aggregate, e.g. a one byte char array */
2013 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2017 /* if + or - then it has to be one byte result */
2018 if ((ic->op == '+' || ic->op == '-')
2019 && getSize (operandType (IC_RESULT (ic))) > 1)
2022 /* if shift operation make sure right side is not a literal */
2023 if (ic->op == RIGHT_OP &&
2024 (isOperandLiteral (IC_RIGHT (ic)) ||
2025 getSize (operandType (IC_RESULT (ic))) > 1))
2028 if (ic->op == LEFT_OP &&
2029 (isOperandLiteral (IC_RIGHT (ic)) ||
2030 getSize (operandType (IC_RESULT (ic))) > 1))
2033 /* has only one definition */
2034 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2037 /* has only one use */
2038 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2041 /* and the usage immediately follows this iCode */
2042 if (!(uic = hTabItemWithKey (iCodehTab,
2043 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2046 if (ic->next != uic)
2049 /* if it is a conditional branch then we definitely can */
2053 if (uic->op == JUMPTABLE)
2057 /* if the usage is not is an assignment or an
2058 arithmetic / bitwise / shift operation then not */
2059 if (POINTER_SET (uic) &&
2060 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2064 if (uic->op != '=' &&
2065 !IS_ARITHMETIC_OP (uic) &&
2066 !IS_BITWISE_OP (uic) &&
2067 uic->op != LEFT_OP &&
2068 uic->op != RIGHT_OP)
2071 /* if used in ^ operation then make sure right is not a
2073 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2076 /* if shift operation make sure right side is not a literal */
2077 if (uic->op == RIGHT_OP &&
2078 (isOperandLiteral (IC_RIGHT (uic)) ||
2079 getSize (operandType (IC_RESULT (uic))) > 1))
2082 if (uic->op == LEFT_OP &&
2083 (isOperandLiteral (IC_RIGHT (uic)) ||
2084 getSize (operandType (IC_RESULT (uic))) > 1))
2088 /* make sure that the result of this icode is not on the
2089 stack, since acc is used to compute stack offset */
2090 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2091 OP_SYMBOL (IC_RESULT (uic))->onStack)
2096 /* if either one of them in far space then we cannot */
2097 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2098 isOperandInFarSpace (IC_LEFT (uic))) ||
2099 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2100 isOperandInFarSpace (IC_RIGHT (uic))))
2104 /* if the usage has only one operand then we can */
2105 if (IC_LEFT (uic) == NULL ||
2106 IC_RIGHT (uic) == NULL)
2109 /* make sure this is on the left side if not
2110 a '+' since '+' is commutative */
2111 if (ic->op != '+' &&
2112 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2115 // See mcs51 ralloc for reasoning
2117 /* if one of them is a literal then we can */
2118 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2119 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2126 /** This is confusing :) Guess for now */
2127 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2128 (IS_ITEMP (IC_RIGHT (uic)) ||
2129 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2132 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2133 (IS_ITEMP (IC_LEFT (uic)) ||
2134 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2138 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2143 packRegsForHLUse (iCode * ic)
2147 /* PENDING: Could do IFX */
2153 /* has only one definition */
2154 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2156 D (D_HLUSE, (" + Dropping as has more than one def\n"));
2160 /* has only one use */
2161 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2163 D (D_HLUSE, (" + Dropping as has more than one use\n"));
2167 /* and the usage immediately follows this iCode */
2168 if (!(uic = hTabItemWithKey (iCodehTab,
2169 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2171 D (D_HLUSE, (" + Dropping as usage isn't in this block\n"));
2175 if (ic->next != uic)
2177 D (D_HLUSE, (" + Dropping as usage doesn't follow this\n"));
2186 if (getSize (operandType (IC_RESULT (ic))) != 2 ||
2187 (IC_LEFT(uic) && getSize (operandType (IC_LEFT (uic))) != 2) ||
2188 (IC_RIGHT(uic) && getSize (operandType (IC_RIGHT (uic))) != 2))
2190 D (D_HLUSE, (" + Dropping as the result size is not 2\n"));
2196 if (ic->op == CAST && uic->op == IPUSH)
2198 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2200 if (ic->op == ADDRESS_OF && POINTER_GET (uic) && IS_ITEMP( IC_RESULT (uic)))
2202 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2207 /* Case of assign a constant to offset in a static array. */
2208 if (ic->op == '+' && IS_VALOP (IC_RIGHT (ic)))
2210 if (uic->op == '=' && POINTER_SET (uic))
2214 else if (uic->op == IPUSH && getSize (operandType (IC_LEFT (uic))) == 2)
2221 D (D_HLUSE, (" + Dropping as it's a bad op\n"));
2224 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_SCRATCH;
2228 packRegsForHLUse3 (iCode * lic, operand * op, eBBlock * ebp)
2233 bool isFirst = TRUE;
2236 printf("Checking:\n");
2240 if ( OP_SYMBOL(op)->accuse)
2245 if (OP_SYMBOL(op)->remat)
2250 /* Only defined once */
2251 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2254 if (getSize (operandType (op)) > 2)
2257 /* And this is the definition */
2258 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2261 /* first check if any overlapping liverange has already been
2263 if (OP_SYMBOL(op)->clashes)
2265 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2267 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2269 sym = hTabItemWithKey(liveRanges,i);
2270 if (sym->accuse == ACCUSE_SCRATCH)
2278 /* Nothing else that clashes with this is using the scratch
2279 register. Scan through all of the intermediate instructions and
2280 see if any of them could nuke HL.
2282 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2284 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2285 ic = hTabNextItem(iCodeSeqhTab,&key))
2289 printf("(op: %u)\n", ic->op);
2294 if (ic->op == ADDRESS_OF)
2296 if (POINTER_GET (ic))
2300 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))
2301 && isOperandInDirSpace (IC_RESULT (ic)))
2304 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))
2305 && isOperandInDirSpace (IC_LEFT (ic)))
2308 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))
2309 && isOperandInDirSpace (IC_RIGHT (ic)))
2312 /* Handle the non left/right/result ones first */
2315 if (ic->op == JUMPTABLE)
2324 if (ic->op == IPUSH && isOperandEqual (op, IC_LEFT (ic)))
2327 if (ic->op == SEND && isOperandEqual (op, IC_LEFT (ic)))
2330 if (ic->op == CALL && isOperandEqual (op, IC_RESULT (ic)))
2333 if ((ic->op == '=' && !POINTER_SET(ic)) ||
2334 ic->op == UNARYMINUS ||
2340 if (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic)))
2343 if (IS_VALOP (IC_RIGHT (ic)) &&
2350 /* By default give up */
2355 printf("Succeeded!\n");
2357 OP_SYMBOL (op)->accuse = ACCUSE_SCRATCH;
2363 packRegsForIYUse (iCode * lic, operand * op, eBBlock * ebp)
2371 printf("Checking IY on %p lic key %u first def %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)));
2375 if ( OP_SYMBOL(op)->accuse)
2380 if (OP_SYMBOL(op)->remat)
2385 /* Only defined once */
2386 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2389 /* And this is the definition */
2390 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2393 /* first check if any overlapping liverange has already been
2395 if (OP_SYMBOL(op)->clashes)
2397 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2399 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2401 sym = hTabItemWithKey(liveRanges,i);
2402 if (sym->accuse == ACCUSE_IY)
2410 /* Only a few instructions can load into IY */
2416 /* Nothing else that clashes with this is using the scratch
2417 register. Scan through all of the intermediate instructions and
2418 see if any of them could nuke HL.
2420 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2423 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2424 ic = hTabNextItem(iCodeSeqhTab,&key))
2428 printf("(op: %u uses %u)\n", ic->op, bitVectBitValue(uses, ic->key));
2431 if (ic->op == PCALL ||
2440 /* Only certain rules will work against IY. Check if this iCode uses
2442 if (bitVectBitValue(uses, ic->key) != 0)
2447 if (ic->op == '=' &&
2448 isOperandEqual(IC_RESULT(ic), op))
2451 if (ic->op == GET_VALUE_AT_ADDRESS &&
2452 isOperandEqual(IC_LEFT(ic), op))
2455 if (isOperandEqual(IC_RESULT(ic), IC_LEFT(ic)) == FALSE)
2458 if (IC_RIGHT (ic) && IS_VALOP (IC_RIGHT (ic)))
2460 if (ic->op == '+' ||
2463 /* Only works if the constant is small */
2464 if (operandLitValue (IC_RIGHT (ic)) < 4)
2476 /* This iCode doesn't use the sym. See if this iCode preserves IY.
2478 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2479 isOperandInDirSpace(IC_RESULT(ic)))
2482 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2483 isOperandInFarSpace(IC_RIGHT(ic)))
2486 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2487 isOperandInFarSpace(IC_LEFT(ic)))
2493 /* By default give up */
2498 printf("Succeeded IY!\n");
2500 OP_SYMBOL (op)->accuse = ACCUSE_IY;
2505 /** Returns TRUE if this operation can use acc and if it preserves the value.
2508 opPreservesA (iCode * uic)
2512 /* If we've gotten this far then the thing to compare must be
2513 small enough and must be in A.
2518 if (uic->op == JUMPTABLE)
2520 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2524 /* A pointer assign preserves A if A is the left value. */
2525 if (uic->op == '=' && POINTER_SET (uic))
2530 /* if the usage has only one operand then we can */
2531 /* PENDING: check */
2532 if (IC_LEFT (uic) == NULL ||
2533 IC_RIGHT (uic) == NULL)
2535 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2539 /* PENDING: check this rule */
2540 if (getSize (operandType (IC_RESULT (uic))) > 1)
2542 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2547 /* Disabled all of the old rules as they weren't verified and have
2548 caused at least one problem.
2553 /** Returns true if this operand preserves the value of A.
2556 opIgnoresA (iCode * ic, iCode * uic)
2558 /* A increment of an iTemp by a constant is OK. */
2559 if ( uic->op == '+' &&
2560 IS_ITEMP (IC_LEFT (uic)) &&
2561 IS_ITEMP (IC_RESULT (uic)) &&
2562 IS_OP_LITERAL (IC_RIGHT (uic)))
2564 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2566 /* Being an ITEMP means that we're already a symbol. */
2568 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2574 else if (uic->op == '=' && !POINTER_SET (uic))
2576 /* If they are equal and get optimised out then things are OK. */
2577 if (isOperandEqual (IC_RESULT (uic), IC_RIGHT (uic)))
2579 /* Straight assign is OK. */
2588 /* Some optimisation cases:
2600 ; genAssign (pointer)
2604 want to optimise down to:
2610 So genPointer get is OK
2611 genPlus where the right is constant, left is iTemp, and result is same as left
2612 genAssign (pointer) is OK
2623 ; genAssign (pointer)
2624 ; AOP_STK for _strcpy_to_1_1
2629 want to optimise down to:
2635 So genIfx where IC_COND has size of 1 and is a constant.
2638 /** Pack registers for acc use.
2639 When the result of this operation is small and short lived it may
2640 be able to be stored in the accumulator.
2642 Note that the 'A preserving' list is currently emperical :)
2645 packRegsForAccUse2 (iCode * ic)
2649 D (D_ACCUSE2, ("packRegsForAccUse2: running on ic %p line %u\n", ic, ic->lineno));
2651 /* Filter out all but those 'good' commands */
2653 !POINTER_GET (ic) &&
2655 !IS_BITWISE_OP (ic) &&
2661 ic->op != GETHBIT &&
2664 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2668 /* if + or - then it has to be one byte result.
2671 if ((ic->op == '+' || ic->op == '-')
2672 && getSize (operandType (IC_RESULT (ic))) > 1)
2674 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2678 /* has only one definition */
2679 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2681 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2685 /* Right. We may be able to propagate it through if:
2686 For each in the chain of uses the intermediate is OK.
2688 /* Get next with 'uses result' bit on
2689 If this->next == next
2690 Validate use of next
2691 If OK, increase count
2693 /* and the usage immediately follows this iCode */
2694 if (!(uic = hTabItemWithKey (iCodehTab,
2695 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2697 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2702 /* Create a copy of the OP_USES bit vect */
2703 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2705 iCode *scan = ic, *next;
2709 setBit = bitVectFirstBit (uses);
2710 next = hTabItemWithKey (iCodehTab, setBit);
2711 if (scan->next == next)
2713 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2715 bitVectUnSetBit (uses, setBit);
2716 /* Still contigous. */
2717 if (!opPreservesA (next))
2719 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2722 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2725 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2727 if (next->prev == NULL)
2729 if (!opPreservesA (next))
2731 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2734 bitVectUnSetBit (uses, setBit);
2739 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2743 else if (scan->next == NULL)
2745 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2746 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2751 if (opIgnoresA (ic, scan->next))
2755 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2759 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2764 while (!bitVectIsZero (uses));
2766 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2771 /** Does some transformations to reduce register pressure.
2774 packRegisters (eBBlock * ebp)
2779 D (D_ALLOC, ("packRegisters: entered.\n"));
2781 while (1 && !DISABLE_PACK_ASSIGN)
2784 /* look for assignments of the form */
2785 /* iTempNN = TRueSym (someoperation) SomeOperand */
2787 /* TrueSym := iTempNN:1 */
2788 for (ic = ebp->sch; ic; ic = ic->next)
2790 /* find assignment of the form TrueSym := iTempNN:1 */
2791 if (ic->op == '=' && !POINTER_SET (ic))
2792 change += packRegsForAssign (ic, ebp);
2798 for (ic = ebp->sch; ic; ic = ic->next)
2800 /* Safe: address of a true sym is always constant. */
2801 /* if this is an itemp & result of a address of a true sym
2802 then mark this as rematerialisable */
2803 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2805 if (ic->op == ADDRESS_OF &&
2806 IS_ITEMP (IC_RESULT (ic)) &&
2807 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2808 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2809 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2812 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2813 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2814 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2817 /* Safe: just propagates the remat flag */
2818 /* if straight assignment then carry remat flag if this is the
2820 if (ic->op == '=' &&
2821 !POINTER_SET (ic) &&
2822 IS_SYMOP (IC_RIGHT (ic)) &&
2823 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2824 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2827 OP_SYMBOL (IC_RESULT (ic))->remat =
2828 OP_SYMBOL (IC_RIGHT (ic))->remat;
2829 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2830 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2833 /* if the condition of an if instruction is defined in the
2834 previous instruction then mark the itemp as a conditional */
2835 if ((IS_CONDITIONAL (ic) ||
2836 ((ic->op == BITWISEAND ||
2839 isBitwiseOptimizable (ic))) &&
2840 ic->next && ic->next->op == IFX &&
2841 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2842 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2843 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2846 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2851 /* reduce for support function calls */
2852 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2853 packRegsForSupport (ic, ebp);
2856 /* some cases the redundant moves can
2857 can be eliminated for return statements */
2858 if (ic->op == RETURN || ic->op == SEND)
2860 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2863 /* if pointer set & left has a size more than
2864 one and right is not in far space */
2865 if (!DISABLE_PACK_ONE_USE &&
2867 /* MLH: no such thing.
2868 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2869 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2870 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2871 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2874 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2877 /* if pointer get */
2878 if (!DISABLE_PACK_ONE_USE &&
2880 /* MLH: dont have far space
2881 !isOperandInFarSpace(IC_RESULT(ic))&& */
2882 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2883 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2884 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2887 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2890 /* pack registers for accumulator use, when the result of an
2891 arithmetic or bit wise operation has only one use, that use is
2892 immediately following the defintion and the using iCode has
2893 only one operand or has two operands but one is literal & the
2894 result of that operation is not on stack then we can leave the
2895 result of this operation in acc:b combination */
2897 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2900 packRegsForHLUse (ic);
2902 packRegsForHLUse3 (ic, IC_RESULT (ic), ebp);
2905 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)) && IS_Z80)
2907 packRegsForIYUse (ic, IC_RESULT (ic), ebp);
2910 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2911 getSize (operandType (IC_RESULT (ic))) == 1)
2913 packRegsForAccUse2 (ic);
2918 /** Joins together two byte constant pushes into one word push.
2921 joinPushes (iCode *lic)
2925 for (ic = lic; ic; ic = ic->next)
2932 /* Anything past this? */
2937 /* This and the next pushes? */
2938 if (ic->op != IPUSH || uic->op != IPUSH)
2942 /* Both literals? */
2943 if ( !IS_OP_LITERAL (IC_LEFT (ic)) || !IS_OP_LITERAL (IC_LEFT (uic)))
2947 /* Both characters? */
2948 if ( getSize (operandType (IC_LEFT (ic))) != 1 || getSize (operandType (IC_LEFT (uic))) != 1)
2952 /* Pull out the values, make a new type, and create the new iCode for it.
2954 first = (int)operandLitValue ( IC_LEFT (ic));
2955 second = (int)operandLitValue ( IC_LEFT (uic));
2957 sprintf (buffer, "%u", ((first << 8) | (second & 0xFF)) & 0xFFFFU);
2958 val = constVal (buffer);
2959 SPEC_NOUN (val->type) = V_INT;
2960 IC_LEFT (ic)->operand.valOperand = val;
2962 /* Now remove the second one from the list. */
2963 ic->next = uic->next;
2966 /* Patch up the reverse link */
2967 uic->next->prev = ic;
2974 /*-----------------------------------------------------------------*/
2975 /* assignRegisters - assigns registers to each live range as need */
2976 /*-----------------------------------------------------------------*/
2978 z80_assignRegisters (eBBlock ** ebbs, int count)
2983 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2985 setToNull ((void *) &_G.funcrUsed);
2986 setToNull ((void *) &_G.totRegAssigned);
2987 _G.stackExtend = _G.dataExtend = 0;
2991 /* DE is required for the code gen. */
2992 _G.nRegs = GBZ80_MAX_REGS;
2993 regsZ80 = _gbz80_regs;
2997 _G.nRegs = Z80_MAX_REGS;
2998 regsZ80 = _z80_regs;
3001 /* change assignments this will remove some
3002 live ranges reducing some register pressure */
3003 for (i = 0; i < count; i++)
3004 packRegisters (ebbs[i]);
3006 if (options.dump_pack)
3007 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3009 /* first determine for each live range the number of
3010 registers & the type of registers required for each */
3013 /* and serially allocate registers */
3014 serialRegAssign (ebbs, count);
3019 /* if stack was extended then tell the user */
3022 /* werror(W_TOOMANY_SPILS,"stack", */
3023 /* _G.stackExtend,currFunc->name,""); */
3029 /* werror(W_TOOMANY_SPILS,"data space", */
3030 /* _G.dataExtend,currFunc->name,""); */
3034 if (options.dump_rassgn) {
3035 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3036 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3039 /* after that create the register mask
3040 for each of the instruction */
3041 createRegMask (ebbs, count);
3043 /* now get back the chain */
3044 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3046 ic = joinPushes (ic);
3048 /* redo that offsets for stacked automatic variables */
3049 redoStackOffsets ();
3053 /* free up any stackSpil locations allocated */
3054 applyToSet (_G.stackSpil, deallocStackSpil);
3056 setToNull ((void **) &_G.stackSpil);
3057 setToNull ((void **) &_G.spiltSet);
3058 /* mark all registers as free */