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,
60 /* Flags to turn on debugging code.
67 D_ACCUSE2_VERBOSE = 0,
77 #define D(_a, _s) if (_a) { printf _s; fflush(stdout); }
82 #define DISABLE_PACKREGSFORSUPPORT 1
83 #define DISABLE_PACKREGSFORACCUSE 1
85 extern void genZ80Code (iCode *);
87 /** Local static variables */
93 bitVect *totRegAssigned; /* final set of LRs that got into registers */
96 /* registers used in a function */
103 static regs _gbz80_regs[] =
105 {REG_GPR, C_IDX, "c", 1},
106 {REG_GPR, B_IDX, "b", 1},
107 {REG_CND, CND_IDX, "c", 1}
110 static regs _z80_regs[] =
112 {REG_GPR, C_IDX, "c", 1},
113 {REG_GPR, B_IDX, "b", 1},
114 {REG_GPR, E_IDX, "e", 1},
115 {REG_GPR, D_IDX, "d", 1},
116 {REG_CND, CND_IDX, "c", 1}
121 /** Number of usable registers (all but C) */
122 #define Z80_MAX_REGS ((sizeof(_z80_regs)/sizeof(_z80_regs[0]))-1)
123 #define GBZ80_MAX_REGS ((sizeof(_gbz80_regs)/sizeof(_gbz80_regs[0]))-1)
125 static void spillThis (symbol *);
126 static void freeAllRegs ();
128 /** Allocates register of given type.
129 'type' is not used on the z80 version. It was used to select
130 between pointer and general purpose registers on the mcs51 version.
132 @return Pointer to the newly allocated register.
135 allocReg (short type)
139 for (i = 0; i < _G.nRegs; i++)
141 /* For now we allocate from any free */
142 if (regsZ80[i].isFree)
144 regsZ80[i].isFree = 0;
147 currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, i);
149 D (D_ALLOC, ("allocReg: alloced %p\n", ®sZ80[i]));
153 D (D_ALLOC, ("allocReg: No free.\n"));
157 /** Returns pointer to register wit index number
164 for (i = 0; i < _G.nRegs; i++)
166 if (regsZ80[i].rIdx == idx)
172 wassertl (0, "regWithIdx not found");
176 /** Frees a register.
181 wassert (!reg->isFree);
183 D (D_ALLOC, ("freeReg: freed %p\n", reg));
187 /** Returns number of free registers.
195 for (i = 0; i < _G.nRegs; i++)
197 /* For now only one reg type */
198 if (regsZ80[i].isFree)
206 /** Free registers with type.
209 nfreeRegsType (int type)
214 if ((nfr = nFreeRegs (type)) == 0)
216 return nFreeRegs (REG_GPR);
220 return nFreeRegs (type);
223 /*-----------------------------------------------------------------*/
224 /* useReg - marks a register as used */
225 /*-----------------------------------------------------------------*/
233 /*-----------------------------------------------------------------*/
234 /* allDefsOutOfRange - all definitions are out of a range */
235 /*-----------------------------------------------------------------*/
237 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
244 for (i = 0; i < defs->size; i++)
248 if (bitVectBitValue (defs, i) &&
249 (ic = hTabItemWithKey (iCodehTab, i)) &&
250 (ic->seq >= fseq && ic->seq <= toseq))
260 /*-----------------------------------------------------------------*/
261 /* computeSpillable - given a point find the spillable live ranges */
262 /*-----------------------------------------------------------------*/
264 computeSpillable (iCode * ic)
268 /* spillable live ranges are those that are live at this
269 point . the following categories need to be subtracted
271 a) - those that are already spilt
272 b) - if being used by this one
273 c) - defined by this one */
275 spillable = bitVectCopy (ic->rlive);
277 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
279 bitVectCplAnd (spillable, ic->uses); /* used in this one */
280 bitVectUnSetBit (spillable, ic->defKey);
281 spillable = bitVectIntersect (spillable, _G.regAssigned);
286 /*-----------------------------------------------------------------*/
287 /* noSpilLoc - return true if a variable has no spil location */
288 /*-----------------------------------------------------------------*/
290 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
292 return (sym->usl.spillLoc ? 0 : 1);
295 /*-----------------------------------------------------------------*/
296 /* hasSpilLoc - will return 1 if the symbol has spil location */
297 /*-----------------------------------------------------------------*/
299 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
301 return (sym->usl.spillLoc ? 1 : 0);
304 /** Will return 1 if the remat flag is set.
305 A symbol is rematerialisable if it doesnt need to be allocated
306 into registers at creation as it can be re-created at any time -
307 i.e. it's constant in some way.
310 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
315 /*-----------------------------------------------------------------*/
316 /* allLRs - return true for all */
317 /*-----------------------------------------------------------------*/
319 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
324 /** liveRangesWith - applies function to a given set of live range
327 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
328 eBBlock * ebp, iCode * ic)
333 if (!lrs || !lrs->size)
336 for (i = 1; i < lrs->size; i++)
339 if (!bitVectBitValue (lrs, i))
342 /* if we don't find it in the live range
343 hash table we are in serious trouble */
344 if (!(sym = hTabItemWithKey (liveRanges, i)))
346 wassertl (0, "liveRangesWith could not find liveRange");
350 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
352 addSetHead (&rset, sym);
360 /** leastUsedLR - given a set determines which is the least used
363 leastUsedLR (set * sset)
365 symbol *sym = NULL, *lsym = NULL;
367 sym = lsym = setFirstItem (sset);
372 for (; lsym; lsym = setNextItem (sset))
375 /* if usage is the same then prefer
376 the spill the smaller of the two */
377 if (lsym->used == sym->used)
378 if (getSize (lsym->type) < getSize (sym->type))
382 if (lsym->used < sym->used)
387 setToNull ((void **) &sset);
392 /** noOverLap - will iterate through the list looking for over lap
395 noOverLap (set * itmpStack, symbol * fsym)
399 for (sym = setFirstItem (itmpStack); sym;
400 sym = setNextItem (itmpStack))
402 if (bitVectBitValue(sym->clashes,fsym->key))
405 // if sym starts before (or on) our end point
406 // and ends after (or on) our start point,
408 if (sym->liveFrom <= fsym->liveTo &&
409 sym->liveTo >= fsym->liveFrom)
418 /*-----------------------------------------------------------------*/
419 /* isFree - will return 1 if the a free spil location is found */
420 /*-----------------------------------------------------------------*/
424 V_ARG (symbol **, sloc);
425 V_ARG (symbol *, fsym);
427 /* if already found */
431 /* if it is free && and the itmp assigned to
432 this does not have any overlapping live ranges
433 with the one currently being assigned and
434 the size can be accomodated */
436 noOverLap (sym->usl.itmpStack, fsym) &&
437 getSize (sym->type) >= getSize (fsym->type))
446 /*-----------------------------------------------------------------*/
447 /* createStackSpil - create a location on the stack to spil */
448 /*-----------------------------------------------------------------*/
450 createStackSpil (symbol * sym)
454 D (D_ALLOC, ("createStackSpil: for sym %p\n", sym));
456 /* first go try and find a free one that is already
457 existing on the stack */
458 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
460 /* found a free one : just update & return */
461 sym->usl.spillLoc = sloc;
464 addSetHead (&sloc->usl.itmpStack, sym);
465 D (D_ALLOC, ("createStackSpil: found existing\n"));
469 /* could not then have to create one , this is the hard part
470 we need to allocate this on the stack : this is really a
471 hack!! but cannot think of anything better at this time */
473 sprintf (buffer, "sloc%d", _G.slocNum++);
474 sloc = newiTemp (buffer);
476 /* set the type to the spilling symbol */
477 sloc->type = copyLinkChain (sym->type);
478 sloc->etype = getSpec (sloc->type);
479 SPEC_SCLS (sloc->etype) = S_AUTO;
480 SPEC_EXTR (sloc->etype) = 0;
481 SPEC_STAT (sloc->etype) = 0;
482 SPEC_VOLATILE(sloc->etype) = 0;
486 sloc->isref = 1; /* to prevent compiler warning */
488 /* if it is on the stack then update the stack */
489 if (IN_STACK (sloc->etype))
491 currFunc->stack += getSize (sloc->type);
492 _G.stackExtend += getSize (sloc->type);
496 _G.dataExtend += getSize (sloc->type);
499 /* add it to the stackSpil set */
500 addSetHead (&_G.stackSpil, sloc);
501 sym->usl.spillLoc = sloc;
504 /* add it to the set of itempStack set
505 of the spill location */
506 addSetHead (&sloc->usl.itmpStack, sym);
508 D (D_ALLOC, ("createStackSpil: created new\n"));
512 /*-----------------------------------------------------------------*/
513 /* spillThis - spils a specific operand */
514 /*-----------------------------------------------------------------*/
516 spillThis (symbol * sym)
520 D (D_ALLOC, ("spillThis: spilling %p\n", sym));
522 /* if this is rematerializable or has a spillLocation
523 we are okay, else we need to create a spillLocation
525 if (!(sym->remat || sym->usl.spillLoc))
527 createStackSpil (sym);
530 /* mark it has spilt & put it in the spilt set */
531 sym->isspilt = sym->spillA = 1;
532 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
534 bitVectUnSetBit (_G.regAssigned, sym->key);
535 bitVectUnSetBit (_G.totRegAssigned, sym->key);
537 for (i = 0; i < sym->nRegs; i++)
541 freeReg (sym->regs[i]);
546 if (sym->usl.spillLoc && !sym->remat)
548 sym->usl.spillLoc->allocreq++;
554 /*-----------------------------------------------------------------*/
555 /* allDefsOutOfRange - all definitions are out of a range */
556 /*-----------------------------------------------------------------*/
558 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
565 for (i = 0; i < defs->size; i++)
569 if (bitVectBitValue (defs, i) &&
570 (ic = hTabItemWithKey (iCodehTab, i)) &&
571 (ic->seq >= fseq && ic->seq <= toseq))
580 /*-----------------------------------------------------------------*/
581 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
582 /* but is not used as a pointer */
583 /*-----------------------------------------------------------------*/
585 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
587 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
590 /*-----------------------------------------------------------------*/
591 /* notUsedInBlock - not used in this block */
592 /*-----------------------------------------------------------------*/
594 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
596 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
597 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
598 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
601 /*-----------------------------------------------------------------*/
602 /* notUsedInRemaining - not used or defined in remain of the block */
603 /*-----------------------------------------------------------------*/
605 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
607 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
608 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
612 /** Select a iTemp to spil : rather a simple procedure.
615 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
617 bitVect *lrcs = NULL;
621 D (D_ALLOC, ("selectSpil: finding spill for ic %p\n", ic));
622 /* get the spillable live ranges */
623 lrcs = computeSpillable (ic);
625 /* get all live ranges that are rematerizable */
626 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
628 D (D_ALLOC, ("selectSpil: using remat.\n"));
629 /* return the least used of these */
630 return leastUsedLR (selectS);
634 /* get live ranges with spillLocations in direct space */
635 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
637 sym = leastUsedLR (selectS);
638 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
639 sym->usl.spillLoc->rname :
640 sym->usl.spillLoc->name));
642 /* mark it as allocation required */
643 sym->usl.spillLoc->allocreq++;
647 /* if the symbol is local to the block then */
648 if (forSym->liveTo < ebp->lSeq)
651 /* check if there are any live ranges allocated
652 to registers that are not used in this block */
653 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
655 sym = leastUsedLR (selectS);
656 /* if this is not rematerializable */
660 wassertl (0, "Attempted to do an unsupported block spill");
666 /* check if there are any live ranges that not
667 used in the remainder of the block */
668 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
670 sym = leastUsedLR (selectS);
675 wassertl (0, "Attempted to do an unsupported remain spill");
683 /* find live ranges with spillocation && not used as pointers */
684 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
687 sym = leastUsedLR (selectS);
688 /* mark this as allocation required */
689 sym->usl.spillLoc->allocreq++;
694 /* find live ranges with spillocation */
695 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
697 D (D_ALLOC, ("selectSpil: using with spill.\n"));
698 sym = leastUsedLR (selectS);
699 sym->usl.spillLoc->allocreq++;
703 /* couldn't find then we need to create a spil
704 location on the stack , for which one? the least
706 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
708 D (D_ALLOC, ("selectSpil: creating new spill.\n"));
709 /* return a created spil location */
710 sym = createStackSpil (leastUsedLR (selectS));
711 sym->usl.spillLoc->allocreq++;
715 /* this is an extreme situation we will spill
716 this one : happens very rarely but it does happen */
717 D (D_ALLOC, ("selectSpil: using spillThis.\n"));
723 /** Spil some variable & mark registers as free.
724 A spill occurs when an iTemp wont fit into the available registers.
727 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
732 D (D_ALLOC, ("spilSomething: spilling on ic %p\n", ic));
734 /* get something we can spil */
735 ssym = selectSpil (ic, ebp, forSym);
737 /* mark it as spilt */
738 ssym->isspilt = ssym->spillA = 1;
739 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
741 /* mark it as not register assigned &
742 take it away from the set */
743 bitVectUnSetBit (_G.regAssigned, ssym->key);
744 bitVectUnSetBit (_G.totRegAssigned, ssym->key);
746 /* mark the registers as free */
747 for (i = 0; i < ssym->nRegs; i++)
749 freeReg (ssym->regs[i]);
751 wassertl (ssym->blockSpil == 0, "Encountered a sym with a block spill");
752 wassertl (ssym->remainSpil == 0, "Encountered a sym with a remain spill");
754 /* if spilt on stack then free up r0 & r1
755 if they could have been assigned to as gprs */
756 if (!ptrRegReq && isSpiltOnStack (ssym))
759 spillLRWithPtrReg (ssym);
762 /* if this was a block level spil then insert push & pop
763 at the start & end of block respectively */
766 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
767 /* add push to the start of the block */
768 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
769 ebp->sch->next : ebp->sch));
770 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
771 /* add pop to the end of the block */
772 addiCodeToeBBlock (ebp, nic, NULL);
775 /* if spilt because not used in the remainder of the
776 block then add a push before this instruction and
777 a pop at the end of the block */
778 if (ssym->remainSpil)
781 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
782 /* add push just before this instruction */
783 addiCodeToeBBlock (ebp, nic, ic);
785 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
786 /* add pop to the end of the block */
787 addiCodeToeBBlock (ebp, nic, NULL);
791 D (D_ALLOC, ("spilSomething: done.\n"));
799 /** Will try for GPR if not spil.
802 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
806 D (D_ALLOC, ("getRegGpr: on ic %p\n", ic));
808 /* try for gpr type */
809 if ((reg = allocReg (REG_GPR)))
811 D (D_ALLOC, ("getRegGpr: got a reg.\n"));
815 /* we have to spil */
816 if (!spilSomething (ic, ebp, sym))
818 D (D_ALLOC, ("getRegGpr: have to spill.\n"));
822 /* this looks like an infinite loop but
823 in really selectSpil will abort */
827 static regs *getRegGprNoSpil()
831 /* try for gpr type */
832 if ((reg = allocReg (REG_GPR)))
834 D (D_ALLOC, ("getRegGprNoSpil: got a reg.\n"));
840 /** Symbol has a given register.
843 symHasReg (symbol * sym, regs * reg)
847 for (i = 0; i < sym->nRegs; i++)
848 if (sym->regs[i] == reg)
854 /** Check the live to and if they have registers & are not spilt then
855 free up the registers
858 deassignLRs (iCode * ic, eBBlock * ebp)
864 for (sym = hTabFirstItem (liveRanges, &k); sym;
865 sym = hTabNextItem (liveRanges, &k))
869 /* if it does not end here */
870 if (sym->liveTo > ic->seq)
873 /* if it was spilt on stack then we can
874 mark the stack spil location as free */
879 sym->usl.spillLoc->isFree = 1;
885 if (!bitVectBitValue (_G.regAssigned, sym->key))
888 /* special case check if this is an IFX &
889 the privious one was a pop and the
890 previous one was not spilt then keep track
892 if (ic->op == IFX && ic->prev &&
893 ic->prev->op == IPOP &&
894 !ic->prev->parmPush &&
895 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
896 psym = OP_SYMBOL (IC_LEFT (ic->prev));
898 D (D_ALLOC, ("deassignLRs: in loop on sym %p nregs %u\n", sym, sym->nRegs));
904 bitVectUnSetBit (_G.regAssigned, sym->key);
906 /* if the result of this one needs registers
907 and does not have it then assign it right
909 if (IC_RESULT (ic) &&
910 !(SKIP_IC2 (ic) || /* not a special icode */
911 ic->op == JUMPTABLE ||
916 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
917 result->liveTo > ic->seq && /* and will live beyond this */
918 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
919 result->regType == sym->regType && /* same register types */
920 result->nRegs && /* which needs registers */
921 !result->isspilt && /* and does not already have them */
923 !bitVectBitValue (_G.regAssigned, result->key) &&
924 /* the number of free regs + number of regs in this LR
925 can accomodate the what result Needs */
926 ((nfreeRegsType (result->regType) +
927 sym->nRegs) >= result->nRegs)
930 for (i = 0; i < result->nRegs; i++)
933 result->regs[i] = sym->regs[i];
935 result->regs[i] = getRegGpr (ic, ebp, result);
937 /* if the allocation falied which means
938 this was spilt then break */
939 if (!result->regs[i])
947 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
948 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
951 /* free the remaining */
952 for (; i < sym->nRegs; i++)
956 if (!symHasReg (psym, sym->regs[i]))
957 freeReg (sym->regs[i]);
960 freeReg (sym->regs[i]);
961 // sym->regs[i] = NULL;
968 /** Reassign this to registers.
971 reassignLR (operand * op)
973 symbol *sym = OP_SYMBOL (op);
976 D (D_ALLOC, ("reassingLR: on sym %p\n", sym));
978 /* not spilt any more */
979 sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
980 bitVectUnSetBit (_G.spiltSet, sym->key);
982 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
983 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
987 for (i = 0; i < sym->nRegs; i++)
988 sym->regs[i]->isFree = 0;
991 /** Determines if allocating will cause a spill.
994 willCauseSpill (int nr, int rt)
996 /* first check if there are any avlb registers
997 of te type required */
998 if (nFreeRegs (0) >= nr)
1001 /* it will cause a spil */
1005 /** The allocator can allocate same registers to result and operand,
1006 if this happens make sure they are in the same position as the operand
1007 otherwise chaos results.
1010 positionRegs (symbol * result, symbol * opsym)
1012 int count = min (result->nRegs, opsym->nRegs);
1013 int i, j = 0, shared = 0;
1016 D (D_ALLOC, ("positionRegs: on result %p opsum %p line %u\n", result, opsym, lineno));
1018 /* if the result has been spilt then cannot share */
1023 /* first make sure that they actually share */
1024 for (i = 0; i < count; i++)
1026 for (j = 0; j < count; j++)
1028 if (result->regs[i] == opsym->regs[j] && i != j)
1038 regs *tmp = result->regs[i];
1039 result->regs[i] = result->regs[j];
1040 result->regs[j] = tmp;
1047 /** Try to allocate a pair of registers to the symbol.
1050 tryAllocatingRegPair (symbol * sym)
1053 wassert (sym->nRegs == 2);
1054 for (i = 0; i < _G.nRegs; i += 2)
1056 if ((regsZ80[i].isFree) && (regsZ80[i + 1].isFree))
1058 regsZ80[i].isFree = 0;
1059 sym->regs[0] = ®sZ80[i];
1060 regsZ80[i + 1].isFree = 0;
1061 sym->regs[1] = ®sZ80[i + 1];
1062 sym->regType = REG_PAIR;
1066 currFunc->regsUsed =
1067 bitVectSetBit (currFunc->regsUsed, i);
1068 currFunc->regsUsed =
1069 bitVectSetBit (currFunc->regsUsed, i + 1);
1071 D (D_ALLOC, ("tryAllocRegPair: succeded for sym %p\n", sym));
1075 D (D_ALLOC, ("tryAllocRegPair: failed on sym %p\n", sym));
1079 /** Serially allocate registers to the variables.
1080 This is the main register allocation function. It is called after
1084 serialRegAssign (eBBlock ** ebbs, int count)
1088 /* for all blocks */
1089 for (i = 0; i < count; i++)
1094 if (ebbs[i]->noPath &&
1095 (ebbs[i]->entryLabel != entryLabel &&
1096 ebbs[i]->entryLabel != returnLabel))
1099 /* of all instructions do */
1100 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1103 /* if this is an ipop that means some live
1104 range will have to be assigned again */
1108 reassignLR (IC_LEFT (ic));
1111 /* if result is present && is a true symbol */
1112 if (IC_RESULT (ic) && ic->op != IFX &&
1113 IS_TRUE_SYMOP (IC_RESULT (ic)))
1114 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1116 /* take away registers from live
1117 ranges that end at this instruction */
1118 deassignLRs (ic, ebbs[i]);
1120 /* some don't need registers */
1121 /* MLH: removed RESULT and POINTER_SET condition */
1122 if (SKIP_IC2 (ic) ||
1123 ic->op == JUMPTABLE ||
1129 /* now we need to allocate registers only for the result */
1132 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1137 D (D_ALLOC, ("serialRegAssign: in loop on result %p\n", sym));
1139 /* if it does not need or is spilt
1140 or is already assigned to registers
1141 or will not live beyond this instructions */
1144 bitVectBitValue (_G.regAssigned, sym->key) ||
1145 sym->liveTo <= ic->seq)
1147 D (D_ALLOC, ("serialRegAssign: wont live long enough.\n"));
1151 /* if some liverange has been spilt at the block level
1152 and this one live beyond this block then spil this
1154 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1156 D (D_ALLOC, ("serialRegAssign: \"spilling to be safe.\"\n"));
1160 /* if trying to allocate this will cause
1161 a spill and there is nothing to spill
1162 or this one is rematerializable then
1164 willCS = willCauseSpill (sym->nRegs, sym->regType);
1165 spillable = computeSpillable (ic);
1167 (willCS && bitVectIsZero (spillable)))
1170 D (D_ALLOC, ("serialRegAssign: \"remat spill\"\n"));
1176 /* if it has a spillocation & is used less than
1177 all other live ranges then spill this */
1179 if (sym->usl.spillLoc) {
1180 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1181 allLRs, ebbs[i], ic));
1182 if (leastUsed && leastUsed->used > sym->used) {
1187 /* if none of the liveRanges have a spillLocation then better
1188 to spill this one than anything else already assigned to registers */
1189 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1190 /* if this is local to this block then we might find a block spil */
1191 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1199 /* else we assign registers to it */
1200 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1201 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1203 /* Special case: Try to fit into a reg pair if
1205 D (D_ALLOC, ("serialRegAssign: actually allocing regs!\n"));
1206 if ((sym->nRegs == 2) && tryAllocatingRegPair (sym))
1211 for (j = 0; j < sym->nRegs; j++)
1213 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1215 /* if the allocation falied which means
1216 this was spilt then break */
1219 D (D_ALLOC, ("Couldnt alloc (spill)\n"))
1224 /* if it shares registers with operands make sure
1225 that they are in the same position */
1226 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1227 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1228 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1229 OP_SYMBOL (IC_LEFT (ic)));
1230 /* do the same for the right operand */
1231 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1232 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1233 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1234 OP_SYMBOL (IC_RIGHT (ic)));
1241 /*-----------------------------------------------------------------*/
1242 /* fillGaps - Try to fill in the Gaps left by Pass1 */
1243 /*-----------------------------------------------------------------*/
1244 static void fillGaps()
1249 if (getenv("DISABLE_FILL_GAPS")) return;
1251 /* look for livernages that was spilt by the allocator */
1252 for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1253 sym = hTabNextItem(liveRanges,&key)) {
1258 if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1260 /* find the liveRanges this one clashes with, that are
1261 still assigned to registers & mark the registers as used*/
1262 for ( i = 0 ; i < sym->clashes->size ; i ++) {
1266 if (bitVectBitValue(sym->clashes,i) == 0 || /* those that clash with this */
1267 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1270 assert (clr = hTabItemWithKey(liveRanges,i));
1272 /* mark these registers as used */
1273 for (k = 0 ; k < clr->nRegs ; k++ )
1274 useReg(clr->regs[k]);
1277 if (willCauseSpill(sym->nRegs,sym->regType)) {
1278 /* NOPE :( clear all registers & and continue */
1283 /* THERE IS HOPE !!!! */
1284 for (i=0; i < sym->nRegs ; i++ ) {
1285 sym->regs[i] = getRegGprNoSpil ();
1288 /* for all its definitions check if the registers
1289 allocated needs positioning NOTE: we can position
1290 only ONCE if more than One positioning required
1293 for (i = 0 ; i < sym->defs->size ; i++ ) {
1294 if (bitVectBitValue(sym->defs,i)) {
1296 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1297 if (SKIP_IC(ic)) continue;
1298 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1299 /* if left is assigned to registers */
1300 if (IS_SYMOP(IC_LEFT(ic)) &&
1301 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1302 pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1304 if (IS_SYMOP(IC_RIGHT(ic)) &&
1305 bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1306 pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1308 if (pdone > 1) break;
1311 /* had to position more than once GIVE UP */
1313 /* UNDO all the changes we made to try this */
1315 for (i=0; i < sym->nRegs ; i++ ) {
1316 sym->regs[i] = NULL;
1319 D(D_FILL_GAPS,("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1322 D(D_FILL_GAPS,("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1323 _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1324 sym->isspilt = sym->spillA = 0 ;
1325 sym->usl.spillLoc->allocreq--;
1330 /*-----------------------------------------------------------------*/
1331 /* rUmaskForOp :- returns register mask for an operand */
1332 /*-----------------------------------------------------------------*/
1334 rUmaskForOp (operand * op)
1340 /* only temporaries are assigned registers */
1344 sym = OP_SYMBOL (op);
1346 /* if spilt or no registers assigned to it
1348 if (sym->isspilt || !sym->nRegs)
1351 rumask = newBitVect (_G.nRegs);
1353 for (j = 0; j < sym->nRegs; j++)
1355 rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1362 z80_rUmaskForOp (operand * op)
1364 return rUmaskForOp (op);
1367 /** Returns bit vector of registers used in iCode.
1370 regsUsedIniCode (iCode * ic)
1372 bitVect *rmask = newBitVect (_G.nRegs);
1374 /* do the special cases first */
1377 rmask = bitVectUnion (rmask,
1378 rUmaskForOp (IC_COND (ic)));
1382 /* for the jumptable */
1383 if (ic->op == JUMPTABLE)
1385 rmask = bitVectUnion (rmask,
1386 rUmaskForOp (IC_JTCOND (ic)));
1391 /* of all other cases */
1393 rmask = bitVectUnion (rmask,
1394 rUmaskForOp (IC_LEFT (ic)));
1398 rmask = bitVectUnion (rmask,
1399 rUmaskForOp (IC_RIGHT (ic)));
1402 rmask = bitVectUnion (rmask,
1403 rUmaskForOp (IC_RESULT (ic)));
1409 /** For each instruction will determine the regsUsed.
1412 createRegMask (eBBlock ** ebbs, int count)
1416 /* for all blocks */
1417 for (i = 0; i < count; i++)
1421 if (ebbs[i]->noPath &&
1422 (ebbs[i]->entryLabel != entryLabel &&
1423 ebbs[i]->entryLabel != returnLabel))
1426 /* for all instructions */
1427 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1432 if (SKIP_IC2 (ic) || !ic->rlive)
1435 /* first mark the registers used in this
1437 ic->rUsed = regsUsedIniCode (ic);
1438 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1440 /* now create the register mask for those
1441 registers that are in use : this is a
1442 super set of ic->rUsed */
1443 ic->rMask = newBitVect (_G.nRegs + 1);
1445 /* for all live Ranges alive at this point */
1446 for (j = 1; j < ic->rlive->size; j++)
1451 /* if not alive then continue */
1452 if (!bitVectBitValue (ic->rlive, j))
1455 /* find the live range we are interested in */
1456 if (!(sym = hTabItemWithKey (liveRanges, j)))
1458 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1459 "createRegMask cannot find live range");
1463 /* if no register assigned to it */
1464 if (!sym->nRegs || sym->isspilt)
1467 /* for all the registers allocated to it */
1468 for (k = 0; k < sym->nRegs; k++)
1471 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1477 /** Returns the rematerialized string for a remat var.
1480 rematStr (symbol * sym)
1483 iCode *ic = sym->rematiCode;
1488 /* if plus or minus print the right hand side */
1489 if (ic->op == '+' || ic->op == '-')
1491 sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1494 ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1497 /* we reached the end */
1498 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1505 /*-----------------------------------------------------------------*/
1506 /* regTypeNum - computes the type & number of registers required */
1507 /*-----------------------------------------------------------------*/
1514 /* for each live range do */
1515 for (sym = hTabFirstItem (liveRanges, &k); sym;
1516 sym = hTabNextItem (liveRanges, &k))
1519 /* if used zero times then no registers needed */
1520 if ((sym->liveTo - sym->liveFrom) == 0)
1523 D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1525 /* if the live range is a temporary */
1529 /* if the type is marked as a conditional */
1530 if (sym->regType == REG_CND)
1533 /* if used in return only then we don't
1535 if (sym->ruonly || sym->accuse)
1537 if (IS_AGGREGATE (sym->type) || sym->isptr)
1538 sym->type = aggrToPtr (sym->type, FALSE);
1542 /* if not then we require registers */
1543 D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1544 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1545 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1546 getSize (sym->type));
1547 D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1549 D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1553 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1554 printTypeChain (sym->type, stderr);
1555 fprintf (stderr, "\n");
1558 /* determine the type of register required */
1559 /* Always general purpose */
1560 sym->regType = REG_GPR;
1565 /* for the first run we don't provide */
1566 /* registers for true symbols we will */
1567 /* see how things go */
1568 D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1575 /** Mark all registers as free.
1582 D (D_ALLOC, ("freeAllRegs: running.\n"));
1584 for (i = 0; i < _G.nRegs; i++)
1585 regsZ80[i].isFree = 1;
1588 /*-----------------------------------------------------------------*/
1589 /* deallocStackSpil - this will set the stack pointer back */
1590 /*-----------------------------------------------------------------*/
1591 DEFSETFUNC (deallocStackSpil)
1599 /** Register reduction for assignment.
1602 packRegsForAssign (iCode * ic, eBBlock * ebp)
1606 D (D_ALLOC, ("packRegsForAssign: running on ic %p\n", ic));
1608 if (!IS_ITEMP (IC_RIGHT (ic)) ||
1609 OP_SYMBOL (IC_RIGHT (ic))->isind ||
1610 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1615 /* find the definition of iTempNN scanning backwards if we find a
1616 a use of the true symbol in before we find the definition then
1618 for (dic = ic->prev; dic; dic = dic->prev)
1620 /* PENDING: Don't pack across function calls. */
1621 if (dic->op == CALL || dic->op == PCALL)
1630 if (IS_SYMOP (IC_RESULT (dic)) &&
1631 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1636 if (IS_SYMOP (IC_RIGHT (dic)) &&
1637 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1638 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1644 if (IS_SYMOP (IC_LEFT (dic)) &&
1645 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1646 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1654 return 0; /* did not find */
1656 /* if the result is on stack or iaccess then it must be
1657 the same atleast one of the operands */
1658 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1659 OP_SYMBOL (IC_RESULT (ic))->iaccess)
1661 /* the operation has only one symbol
1662 operator then we can pack */
1663 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1664 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1667 if (!((IC_LEFT (dic) &&
1668 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1670 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1674 /* found the definition */
1675 /* replace the result with the result of */
1676 /* this assignment and remove this assignment */
1677 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1678 IC_RESULT (dic) = IC_RESULT (ic);
1680 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1682 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1684 /* delete from liverange table also
1685 delete from all the points inbetween and the new
1687 for (sic = dic; sic != ic; sic = sic->next)
1689 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1690 if (IS_ITEMP (IC_RESULT (dic)))
1691 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1694 remiCodeFromeBBlock (ebp, ic);
1695 // PENDING: Check vs mcs51
1696 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1697 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1698 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1702 /** Scanning backwards looks for first assig found.
1705 findAssignToSym (operand * op, iCode * ic)
1709 for (dic = ic->prev; dic; dic = dic->prev)
1712 /* if definition by assignment */
1713 if (dic->op == '=' &&
1714 !POINTER_SET (dic) &&
1715 IC_RESULT (dic)->key == op->key)
1716 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1719 /* we are interested only if defined in far space */
1720 /* or in stack space in case of + & - */
1722 /* if assigned to a non-symbol then return
1724 if (!IS_SYMOP (IC_RIGHT (dic)))
1727 /* if the symbol is in far space then
1729 if (isOperandInFarSpace (IC_RIGHT (dic)))
1732 /* for + & - operations make sure that
1733 if it is on the stack it is the same
1734 as one of the three operands */
1735 if ((ic->op == '+' || ic->op == '-') &&
1736 OP_SYMBOL (IC_RIGHT (dic))->onStack)
1739 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1740 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1741 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1749 /* if we find an usage then we cannot delete it */
1750 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1753 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1756 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1760 /* now make sure that the right side of dic
1761 is not defined between ic & dic */
1764 iCode *sic = dic->next;
1766 for (; sic != ic; sic = sic->next)
1767 if (IC_RESULT (sic) &&
1768 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1777 #if !DISABLE_PACKREGSFORSUPPORT
1780 /*-----------------------------------------------------------------*/
1781 /* packRegsForSupport :- reduce some registers for support calls */
1782 /*-----------------------------------------------------------------*/
1784 packRegsForSupport (iCode * ic, eBBlock * ebp)
1787 /* for the left & right operand :- look to see if the
1788 left was assigned a true symbol in far space in that
1789 case replace them */
1790 D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1792 if (IS_ITEMP (IC_LEFT (ic)) &&
1793 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1795 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1801 /* found it we need to remove it from the
1803 for (sic = dic; sic != ic; sic = sic->next)
1804 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1806 IC_LEFT (ic)->operand.symOperand =
1807 IC_RIGHT (dic)->operand.symOperand;
1808 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1809 remiCodeFromeBBlock (ebp, dic);
1810 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1811 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1812 // PENDING: Check vs mcs51
1816 /* do the same for the right operand */
1819 IS_ITEMP (IC_RIGHT (ic)) &&
1820 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1822 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1828 /* found it we need to remove it from the block */
1829 for (sic = dic; sic != ic; sic = sic->next)
1830 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1832 IC_RIGHT (ic)->operand.symOperand =
1833 IC_RIGHT (dic)->operand.symOperand;
1834 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1836 remiCodeFromeBBlock (ebp, dic);
1837 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1838 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1839 // PENDING: vs mcs51
1847 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1849 /** Will reduce some registers for single use.
1852 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1858 D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1860 /* if returning a literal then do nothing */
1864 /* only upto 2 bytes since we cannot predict
1865 the usage of b, & acc */
1866 if (getSize (operandType (op)) > 2)
1869 if (ic->op != RETURN &&
1873 /* this routine will mark the a symbol as used in one
1874 instruction use only && if the defintion is local
1875 (ie. within the basic block) && has only one definition &&
1876 that definiion is either a return value from a
1877 function or does not contain any variables in
1879 uses = bitVectCopy (OP_USES (op));
1880 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
1881 if (!bitVectIsZero (uses)) /* has other uses */
1884 /* if it has only one defintion */
1885 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1886 return NULL; /* has more than one definition */
1888 /* get the that definition */
1890 hTabItemWithKey (iCodehTab,
1891 bitVectFirstBit (OP_DEFS (op)))))
1894 /* found the definition now check if it is local */
1895 if (dic->seq < ebp->fSeq ||
1896 dic->seq > ebp->lSeq)
1897 return NULL; /* non-local */
1899 /* now check if it is the return from a function call */
1900 if (dic->op == CALL || dic->op == PCALL)
1902 if (ic->op != SEND && ic->op != RETURN &&
1903 !POINTER_SET(ic) && !POINTER_GET(ic))
1905 OP_SYMBOL (op)->ruonly = 1;
1911 /* otherwise check that the definition does
1912 not contain any symbols in far space */
1913 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1914 isOperandInFarSpace (IC_RIGHT (dic)) ||
1915 IS_OP_RUONLY (IC_LEFT (ic)) ||
1916 IS_OP_RUONLY (IC_RIGHT (ic)))
1921 /* if pointer set then make sure the pointer is one byte */
1922 if (POINTER_SET (dic))
1925 if (POINTER_GET (dic))
1930 /* also make sure the intervenening instructions
1931 don't have any thing in far space */
1932 for (dic = dic->next; dic && dic != ic; dic = dic->next)
1934 /* if there is an intervening function call then no */
1935 if (dic->op == CALL || dic->op == PCALL)
1937 /* if pointer set then make sure the pointer
1939 if (POINTER_SET (dic))
1942 if (POINTER_GET (dic))
1945 /* if address of & the result is remat the okay */
1946 if (dic->op == ADDRESS_OF &&
1947 OP_SYMBOL (IC_RESULT (dic))->remat)
1950 /* if left or right or result is in far space */
1951 if (isOperandInFarSpace (IC_LEFT (dic)) ||
1952 isOperandInFarSpace (IC_RIGHT (dic)) ||
1953 isOperandInFarSpace (IC_RESULT (dic)) ||
1954 IS_OP_RUONLY (IC_LEFT (dic)) ||
1955 IS_OP_RUONLY (IC_RIGHT (dic)) ||
1956 IS_OP_RUONLY (IC_RESULT (dic)))
1962 OP_SYMBOL (op)->ruonly = 1;
1966 /*-----------------------------------------------------------------*/
1967 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
1968 /*-----------------------------------------------------------------*/
1970 isBitwiseOptimizable (iCode * ic)
1972 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1974 /* bitwise operations are considered optimizable
1975 under the following conditions (Jean-Louis VERN)
1987 if (IS_LITERAL (rtype))
1993 Certian assignments involving pointers can be temporarly stored
2004 #if !DISABLE_PACKREGSFORACCUSE
2007 /** Pack registers for acc use.
2008 When the result of this operation is small and short lived it may
2009 be able to be stored in the accumelator.
2012 packRegsForAccUse (iCode * ic)
2016 /* if this is an aggregate, e.g. a one byte char array */
2017 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2021 /* if + or - then it has to be one byte result */
2022 if ((ic->op == '+' || ic->op == '-')
2023 && getSize (operandType (IC_RESULT (ic))) > 1)
2026 /* if shift operation make sure right side is not a literal */
2027 if (ic->op == RIGHT_OP &&
2028 (isOperandLiteral (IC_RIGHT (ic)) ||
2029 getSize (operandType (IC_RESULT (ic))) > 1))
2032 if (ic->op == LEFT_OP &&
2033 (isOperandLiteral (IC_RIGHT (ic)) ||
2034 getSize (operandType (IC_RESULT (ic))) > 1))
2037 /* has only one definition */
2038 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2041 /* has only one use */
2042 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2045 /* and the usage immediately follows this iCode */
2046 if (!(uic = hTabItemWithKey (iCodehTab,
2047 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2050 if (ic->next != uic)
2053 /* if it is a conditional branch then we definitely can */
2057 if (uic->op == JUMPTABLE)
2061 /* if the usage is not is an assignment or an
2062 arithmetic / bitwise / shift operation then not */
2063 if (POINTER_SET (uic) &&
2064 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2068 if (uic->op != '=' &&
2069 !IS_ARITHMETIC_OP (uic) &&
2070 !IS_BITWISE_OP (uic) &&
2071 uic->op != LEFT_OP &&
2072 uic->op != RIGHT_OP)
2075 /* if used in ^ operation then make sure right is not a
2077 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2080 /* if shift operation make sure right side is not a literal */
2081 if (uic->op == RIGHT_OP &&
2082 (isOperandLiteral (IC_RIGHT (uic)) ||
2083 getSize (operandType (IC_RESULT (uic))) > 1))
2086 if (uic->op == LEFT_OP &&
2087 (isOperandLiteral (IC_RIGHT (uic)) ||
2088 getSize (operandType (IC_RESULT (uic))) > 1))
2092 /* make sure that the result of this icode is not on the
2093 stack, since acc is used to compute stack offset */
2094 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2095 OP_SYMBOL (IC_RESULT (uic))->onStack)
2100 /* if either one of them in far space then we cannot */
2101 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2102 isOperandInFarSpace (IC_LEFT (uic))) ||
2103 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2104 isOperandInFarSpace (IC_RIGHT (uic))))
2108 /* if the usage has only one operand then we can */
2109 if (IC_LEFT (uic) == NULL ||
2110 IC_RIGHT (uic) == NULL)
2113 /* make sure this is on the left side if not
2114 a '+' since '+' is commutative */
2115 if (ic->op != '+' &&
2116 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2119 // See mcs51 ralloc for reasoning
2121 /* if one of them is a literal then we can */
2122 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2123 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2130 /** This is confusing :) Guess for now */
2131 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2132 (IS_ITEMP (IC_RIGHT (uic)) ||
2133 (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2136 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2137 (IS_ITEMP (IC_LEFT (uic)) ||
2138 (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2142 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2147 packRegsForHLUse (iCode * ic)
2151 /* PENDING: Could do IFX */
2157 /* has only one definition */
2158 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2160 D (D_HLUSE, (" + Dropping as has more than one def\n"));
2164 /* has only one use */
2165 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2167 D (D_HLUSE, (" + Dropping as has more than one use\n"));
2171 /* and the usage immediately follows this iCode */
2172 if (!(uic = hTabItemWithKey (iCodehTab,
2173 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2175 D (D_HLUSE, (" + Dropping as usage isn't in this block\n"));
2179 if (ic->next != uic)
2181 D (D_HLUSE, (" + Dropping as usage doesn't follow this\n"));
2190 if (getSize (operandType (IC_RESULT (ic))) != 2 ||
2191 (IC_LEFT(uic) && getSize (operandType (IC_LEFT (uic))) != 2) ||
2192 (IC_RIGHT(uic) && getSize (operandType (IC_RIGHT (uic))) != 2))
2194 D (D_HLUSE, (" + Dropping as the result size is not 2\n"));
2200 if (ic->op == CAST && uic->op == IPUSH)
2202 if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2204 if (ic->op == ADDRESS_OF && POINTER_GET (uic) && IS_ITEMP( IC_RESULT (uic)))
2206 if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2211 /* Case of assign a constant to offset in a static array. */
2212 if (ic->op == '+' && IS_VALOP (IC_RIGHT (ic)))
2214 if (uic->op == '=' && POINTER_SET (uic))
2218 else if (uic->op == IPUSH && getSize (operandType (IC_LEFT (uic))) == 2)
2225 D (D_HLUSE, (" + Dropping as it's a bad op\n"));
2228 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_SCRATCH;
2232 packRegsForHLUse3 (iCode * lic, operand * op, eBBlock * ebp)
2237 bool isFirst = TRUE;
2239 D (D_PACK_HLUSE3, ("Checking HL on %p lic key %u first def %u line %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)), lic->lineno));
2243 if ( OP_SYMBOL(op)->accuse)
2248 if (OP_SYMBOL(op)->remat)
2253 /* Only defined once */
2254 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2257 if (getSize (operandType (op)) > 2)
2260 /* And this is the definition */
2261 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2264 /* first check if any overlapping liverange has already been
2266 if (OP_SYMBOL(op)->clashes)
2268 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2270 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2272 sym = hTabItemWithKey(liveRanges,i);
2273 if (sym->accuse == ACCUSE_SCRATCH)
2281 /* Nothing else that clashes with this is using the scratch
2282 register. Scan through all of the intermediate instructions and
2283 see if any of them could nuke HL.
2285 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2287 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2288 ic = hTabNextItem(iCodeSeqhTab, &key))
2292 D (D_PACK_HLUSE3, ("(On %p: op: %u next: %p)\n", ic, ic->op, ic->next));
2297 if (ic->op == ADDRESS_OF)
2299 if (POINTER_GET (ic))
2301 if (ic->op == '=' && !POINTER_SET(ic))
2305 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))
2306 && isOperandInDirSpace (IC_RESULT (ic)))
2309 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))
2310 && isOperandInDirSpace (IC_LEFT (ic)))
2313 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))
2314 && isOperandInDirSpace (IC_RIGHT (ic)))
2317 /* Handle the non left/right/result ones first */
2320 if (ic->op == JUMPTABLE)
2329 if (ic->op == IPUSH && isOperandEqual (op, IC_LEFT (ic)))
2332 if (ic->op == SEND && isOperandEqual (op, IC_LEFT (ic)))
2335 if (ic->op == CALL && isOperandEqual (op, IC_RESULT (ic)))
2338 if (ic->op == LEFT_OP && isOperandLiteral (IC_RIGHT (ic)))
2341 if ((ic->op == '=' && !POINTER_SET(ic)) ||
2342 ic->op == UNARYMINUS ||
2351 if (ic->op == '*' && isOperandEqual (op, IC_LEFT (ic)))
2354 if (POINTER_SET (ic) && isOperandEqual (op, IC_RESULT (ic)))
2357 if (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic)))
2360 if (IS_VALOP (IC_RIGHT (ic)) &&
2367 /* By default give up */
2371 D (D_PACK_HLUSE3, ("Succeeded!\n"))
2373 OP_SYMBOL (op)->accuse = ACCUSE_SCRATCH;
2378 packRegsForIYUse (iCode * lic, operand * op, eBBlock * ebp)
2385 D (D_PACK_IY, ("Checking IY on %p lic key %u first def %u line %u:\n", OP_SYMBOL(op), lic->key, bitVectFirstBit(OP_DEFS(op)), lic->lineno));
2389 if ( OP_SYMBOL(op)->accuse)
2394 if (OP_SYMBOL(op)->remat)
2399 /* Only defined once */
2400 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2403 /* And this is the definition */
2404 if (bitVectFirstBit (OP_DEFS (op)) != lic->key)
2407 /* first check if any overlapping liverange has already been
2409 if (OP_SYMBOL(op)->clashes)
2411 for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2413 if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2415 sym = hTabItemWithKey(liveRanges,i);
2416 if (sym->accuse == ACCUSE_IY)
2424 /* Only a few instructions can load into IY */
2430 /* Nothing else that clashes with this is using the scratch
2431 register. Scan through all of the intermediate instructions and
2432 see if any of them could nuke HL.
2434 dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2437 for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2438 ic = hTabNextItem(iCodeSeqhTab,&key))
2443 if (ic->op == PCALL ||
2452 /* Be pessamistic. */
2456 D (D_PACK_IY, (" op: %u uses %u result: %d left: %d right: %d\n", ic->op, bitVectBitValue(uses, ic->key),
2457 IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) ? isOperandInDirSpace(IC_RESULT(ic)) : -1,
2458 IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) ? isOperandInDirSpace(IC_LEFT(ic)) : -1,
2459 IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) ? isOperandInDirSpace(IC_RIGHT(ic)) : -1
2462 if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2463 isOperandInDirSpace(IC_RESULT(ic)))
2466 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2467 isOperandInDirSpace(IC_RIGHT(ic)))
2470 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2471 isOperandInDirSpace(IC_LEFT(ic)))
2474 /* Only certain rules will work against IY. Check if this iCode uses
2476 if (bitVectBitValue(uses, ic->key) != 0)
2478 if (ic->op == '=' &&
2479 isOperandEqual(IC_RESULT(ic), op))
2482 if (ic->op == GET_VALUE_AT_ADDRESS &&
2483 isOperandEqual(IC_LEFT(ic), op))
2486 if (isOperandEqual(IC_RESULT(ic), IC_LEFT(ic)) == FALSE)
2489 if (IC_RIGHT (ic) && IS_VALOP (IC_RIGHT (ic)))
2491 if (ic->op == '+' ||
2494 /* Only works if the constant is small */
2495 if (operandLitValue (IC_RIGHT (ic)) < 4)
2504 /* This iCode doesn't use the sym. See if this iCode preserves IY.
2509 /* By default give up */
2513 D (D_PACK_IY, ("Succeeded IY!\n"));
2515 OP_SYMBOL (op)->accuse = ACCUSE_IY;
2519 /** Returns TRUE if this operation can use acc and if it preserves the value.
2522 opPreservesA (iCode * uic)
2526 /* If we've gotten this far then the thing to compare must be
2527 small enough and must be in A.
2532 if (uic->op == JUMPTABLE)
2534 D (D_ACCUSE2, (" + Dropping as operation is a Jumptable\n"));
2538 /* A pointer assign preserves A if A is the left value. */
2539 if (uic->op == '=' && POINTER_SET (uic))
2544 /* if the usage has only one operand then we can */
2545 /* PENDING: check */
2546 if (IC_LEFT (uic) == NULL ||
2547 IC_RIGHT (uic) == NULL)
2549 D (D_ACCUSE2, (" + Dropping as operation has only one operand\n"));
2553 /* PENDING: check this rule */
2554 if (getSize (operandType (IC_RESULT (uic))) > 1)
2556 D (D_ACCUSE2, (" + Dropping as operation has size is too big\n"));
2561 /* Disabled all of the old rules as they weren't verified and have
2562 caused at least one problem.
2567 /** Returns true if this operand preserves the value of A.
2570 opIgnoresA (iCode * ic, iCode * uic)
2572 /* A increment of an iTemp by a constant is OK. */
2573 if ( uic->op == '+' &&
2574 IS_ITEMP (IC_LEFT (uic)) &&
2575 IS_ITEMP (IC_RESULT (uic)) &&
2576 IS_OP_LITERAL (IC_RIGHT (uic)))
2578 unsigned int icount = (unsigned int) floatFromVal (IC_RIGHT (uic)->operand.valOperand);
2580 /* Being an ITEMP means that we're already a symbol. */
2582 IC_RESULT (uic)->operand.symOperand->key == IC_LEFT (uic)->operand.symOperand->key
2588 else if (uic->op == '=' && !POINTER_SET (uic))
2590 /* If they are equal and get optimised out then things are OK. */
2591 if (isOperandEqual (IC_RESULT (uic), IC_RIGHT (uic)))
2593 /* Straight assign is OK. */
2602 /* Some optimisation cases:
2614 ; genAssign (pointer)
2618 want to optimise down to:
2624 So genPointer get is OK
2625 genPlus where the right is constant, left is iTemp, and result is same as left
2626 genAssign (pointer) is OK
2637 ; genAssign (pointer)
2638 ; AOP_STK for _strcpy_to_1_1
2643 want to optimise down to:
2649 So genIfx where IC_COND has size of 1 and is a constant.
2652 /** Pack registers for acc use.
2653 When the result of this operation is small and short lived it may
2654 be able to be stored in the accumulator.
2656 Note that the 'A preserving' list is currently emperical :)
2659 packRegsForAccUse2 (iCode * ic)
2663 D (D_ACCUSE2, ("packRegsForAccUse2: running on ic %p line %u\n", ic, ic->lineno));
2667 /* Filter out all but those 'good' commands */
2669 !POINTER_GET (ic) &&
2672 !IS_BITWISE_OP (ic) &&
2678 ic->op != GETHBIT &&
2681 D (D_ACCUSE2, (" + Dropping as not a 'good' source command\n"));
2685 /* if + or - then it has to be one byte result.
2688 if ((ic->op == '+' || ic->op == '-')
2689 && getSize (operandType (IC_RESULT (ic))) > 1)
2691 D (D_ACCUSE2, (" + Dropping as it's a big + or -\n"));
2695 /* has only one definition */
2696 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2698 D (D_ACCUSE2, (" + Dropping as it has more than one definition\n"));
2702 /* Right. We may be able to propagate it through if:
2703 For each in the chain of uses the intermediate is OK.
2705 /* Get next with 'uses result' bit on
2706 If this->next == next
2707 Validate use of next
2708 If OK, increase count
2710 /* and the usage immediately follows this iCode */
2711 if (!(uic = hTabItemWithKey (iCodehTab,
2712 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2714 D (D_ACCUSE2, (" + Dropping as usage does not follow first\n"));
2719 /* Create a copy of the OP_USES bit vect */
2720 bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2722 iCode *scan = ic, *next;
2726 setBit = bitVectFirstBit (uses);
2727 next = hTabItemWithKey (iCodehTab, setBit);
2728 if (scan->next == next)
2730 D (D_ACCUSE2_VERBOSE, (" ! Is next in line\n"));
2732 bitVectUnSetBit (uses, setBit);
2733 /* Still contigous. */
2734 if (!opPreservesA (next))
2736 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A\n"));
2739 D (D_ACCUSE2_VERBOSE, (" ! Preserves A, so continue scanning\n"));
2742 else if (scan->next == NULL && bitVectnBitsOn (uses) == 1 && next != NULL)
2744 if (next->prev == NULL)
2746 if (!opPreservesA (next))
2748 D (D_ACCUSE2, (" + Dropping as operation doesn't preserve A #2\n"));
2751 bitVectUnSetBit (uses, setBit);
2756 D (D_ACCUSE2, (" + Dropping as last in list and next doesn't start a block\n"));
2760 else if (scan->next == NULL)
2762 D (D_ACCUSE2, (" + Dropping as hit the end of the list\n"));
2763 D (D_ACCUSE2, (" + Next in htab: %p\n", next));
2768 if (opIgnoresA (ic, scan->next))
2772 D (D_ACCUSE2_VERBOSE, (" ! Op ignores A, so continue scanning\n"));
2776 D (D_ACCUSE2, (" + Dropping as parts are not consecuitive and intermediate might use A\n"));
2781 while (!bitVectIsZero (uses));
2783 OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2788 /** Does some transformations to reduce register pressure.
2791 packRegisters (eBBlock * ebp)
2796 D (D_ALLOC, ("packRegisters: entered.\n"));
2798 while (1 && !DISABLE_PACK_ASSIGN)
2801 /* look for assignments of the form */
2802 /* iTempNN = TRueSym (someoperation) SomeOperand */
2804 /* TrueSym := iTempNN:1 */
2805 for (ic = ebp->sch; ic; ic = ic->next)
2807 /* find assignment of the form TrueSym := iTempNN:1 */
2808 if (ic->op == '=' && !POINTER_SET (ic))
2809 change += packRegsForAssign (ic, ebp);
2815 for (ic = ebp->sch; ic; ic = ic->next)
2817 /* Safe: address of a true sym is always constant. */
2818 /* if this is an itemp & result of a address of a true sym
2819 then mark this as rematerialisable */
2820 D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2822 if (ic->op == ADDRESS_OF &&
2823 IS_ITEMP (IC_RESULT (ic)) &&
2824 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2825 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2826 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2829 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2830 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2831 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2834 /* Safe: just propagates the remat flag */
2835 /* if straight assignment then carry remat flag if this is the
2837 if (ic->op == '=' &&
2838 !POINTER_SET (ic) &&
2839 IS_SYMOP (IC_RIGHT (ic)) &&
2840 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2841 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2844 OP_SYMBOL (IC_RESULT (ic))->remat =
2845 OP_SYMBOL (IC_RIGHT (ic))->remat;
2846 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2847 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2850 /* if the condition of an if instruction is defined in the
2851 previous instruction then mark the itemp as a conditional */
2852 if ((IS_CONDITIONAL (ic) ||
2853 ((ic->op == BITWISEAND ||
2856 isBitwiseOptimizable (ic))) &&
2857 ic->next && ic->next->op == IFX &&
2858 bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2859 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2860 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2863 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2868 /* reduce for support function calls */
2869 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2870 packRegsForSupport (ic, ebp);
2873 /* some cases the redundant moves can
2874 can be eliminated for return statements */
2875 if (ic->op == RETURN || ic->op == SEND)
2877 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2880 /* if pointer set & left has a size more than
2881 one and right is not in far space */
2882 if (!DISABLE_PACK_ONE_USE &&
2884 /* MLH: no such thing.
2885 !isOperandInFarSpace(IC_RIGHT(ic)) && */
2886 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2887 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2888 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2891 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2894 /* if pointer get */
2895 if (!DISABLE_PACK_ONE_USE &&
2897 /* MLH: dont have far space
2898 !isOperandInFarSpace(IC_RESULT(ic))&& */
2899 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2900 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2901 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2904 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2907 /* pack registers for accumulator use, when the result of an
2908 arithmetic or bit wise operation has only one use, that use is
2909 immediately following the defintion and the using iCode has
2910 only one operand or has two operands but one is literal & the
2911 result of that operation is not on stack then we can leave the
2912 result of this operation in acc:b combination */
2914 if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2917 packRegsForHLUse (ic);
2919 packRegsForHLUse3 (ic, IC_RESULT (ic), ebp);
2922 if (!DISABLE_PACK_IY && IS_ITEMP (IC_RESULT (ic)) && IS_Z80)
2924 packRegsForIYUse (ic, IC_RESULT (ic), ebp);
2927 if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2928 getSize (operandType (IC_RESULT (ic))) == 1)
2930 packRegsForAccUse2 (ic);
2935 /** Joins together two byte constant pushes into one word push.
2938 joinPushes (iCode *lic)
2942 for (ic = lic; ic; ic = ic->next)
2949 /* Anything past this? */
2954 /* This and the next pushes? */
2955 if (ic->op != IPUSH || uic->op != IPUSH)
2959 /* Both literals? */
2960 if ( !IS_OP_LITERAL (IC_LEFT (ic)) || !IS_OP_LITERAL (IC_LEFT (uic)))
2964 /* Both characters? */
2965 if ( getSize (operandType (IC_LEFT (ic))) != 1 || getSize (operandType (IC_LEFT (uic))) != 1)
2969 /* Pull out the values, make a new type, and create the new iCode for it.
2971 first = (int)operandLitValue ( IC_LEFT (ic));
2972 second = (int)operandLitValue ( IC_LEFT (uic));
2974 sprintf (buffer, "%u", ((first << 8) | (second & 0xFF)) & 0xFFFFU);
2975 val = constVal (buffer);
2976 SPEC_NOUN (val->type) = V_INT;
2977 IC_LEFT (ic)->operand.valOperand = val;
2979 /* Now remove the second one from the list. */
2980 ic->next = uic->next;
2983 /* Patch up the reverse link */
2984 uic->next->prev = ic;
2991 /*-----------------------------------------------------------------*/
2992 /* assignRegisters - assigns registers to each live range as need */
2993 /*-----------------------------------------------------------------*/
2995 z80_assignRegisters (eBBlock ** ebbs, int count)
3000 D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
3002 setToNull ((void *) &_G.funcrUsed);
3003 setToNull ((void *) &_G.totRegAssigned);
3004 _G.stackExtend = _G.dataExtend = 0;
3008 /* DE is required for the code gen. */
3009 _G.nRegs = GBZ80_MAX_REGS;
3010 regsZ80 = _gbz80_regs;
3014 _G.nRegs = Z80_MAX_REGS;
3015 regsZ80 = _z80_regs;
3018 /* change assignments this will remove some
3019 live ranges reducing some register pressure */
3020 for (i = 0; i < count; i++)
3021 packRegisters (ebbs[i]);
3023 if (options.dump_pack)
3024 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3026 /* first determine for each live range the number of
3027 registers & the type of registers required for each */
3030 /* and serially allocate registers */
3031 serialRegAssign (ebbs, count);
3036 /* if stack was extended then tell the user */
3039 /* werror(W_TOOMANY_SPILS,"stack", */
3040 /* _G.stackExtend,currFunc->name,""); */
3046 /* werror(W_TOOMANY_SPILS,"data space", */
3047 /* _G.dataExtend,currFunc->name,""); */
3051 if (options.dump_rassgn) {
3052 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3053 dumpLiveRanges (DUMP_LRANGE, liveRanges);
3056 /* after that create the register mask
3057 for each of the instruction */
3058 createRegMask (ebbs, count);
3060 /* now get back the chain */
3061 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3063 ic = joinPushes (ic);
3065 /* redo that offsets for stacked automatic variables */
3066 redoStackOffsets ();
3070 /* free up any stackSpil locations allocated */
3071 applyToSet (_G.stackSpil, deallocStackSpil);
3073 setToNull ((void **) &_G.stackSpil);
3074 setToNull ((void **) &_G.spiltSet);
3075 /* mark all registers as free */