fbb65ed8174bbdffb2a4db8bf59433602d00f963
[fw/sdcc] / src / ds390 / ralloc.c
1 /*------------------------------------------------------------------------
2
3   SDCCralloc.c - source file for register allocation. (DS80C390) specific
4
5                 Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
25
26 #include "common.h"
27 #include "ralloc.h"
28 #include "gen.h"
29
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although      */
32 /* some routines are non-processor specific & can be reused when   */
33 /* targetting other processors. The decision for this will have    */
34 /* to be made on a routine by routine basis                        */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU      */
37 /*-----------------------------------------------------------------*/
38
39 #define D(x)
40
41 /* Global data */
42 static struct
43   {
44     bitVect *spiltSet;
45     set *stackSpil;
46     bitVect *regAssigned;
47     bitVect *totRegAssigned;    /* final set of LRs that got into registers */
48     short blockSpil;
49     int slocNum;
50     bitVect *funcrUsed;         /* registers used in a function */
51     int stackExtend;
52     int dataExtend;
53   }
54 _G;
55
56 /* Shared with gen.c */
57 int ds390_ptrRegReq;            /* one byte pointer register required */
58
59 /* 8051 registers */
60 regs regs390[] =
61 {
62
63   {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1, 1},
64   {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1, 1},
65   {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1, 1},
66   {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1, 1},
67   {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1, 1},
68   {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1, 1},
69   {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1, 1},
70   {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1, 1},
71   {REG_GPR, DPL_IDX, REG_GPR, "dpl", "dpl", "dpl", 0, 0, 0},
72   {REG_GPR, DPH_IDX, REG_GPR, "dph", "dph", "dph", 0, 0, 0},
73   {REG_GPR, DPX_IDX, REG_GPR, "dpx", "dpx", "dpx", 0, 0, 0},
74   {REG_GPR, B_IDX, REG_GPR, "b", "b", "b", 0, 0, 0},
75   {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 0, 0},
76   {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 0, 0},
77   {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 0, 0},
78   {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 0, 0},
79   {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 0, 0},
80   {REG_CND, CND_IDX, REG_GPR, "C", "psw", "xreg", 0, 0, 0},
81   {0, DPL1_IDX, 0, "dpl1", "dpl1", "dpl1", 0, 0, 0},
82   {0, DPH1_IDX, 0, "dph1", "dph1", "dph1", 0, 0, 0},
83   {0, DPX1_IDX, 0, "dpx1", "dpx1", "dpx1", 0, 0, 0},
84   {0, DPS_IDX, 0, "dps", "dps", "dps", 0, 0, 0},
85   {0, A_IDX, 0, "a", "acc", "acc", 0, 0, 0},
86   {0, AP_IDX, 0, "ap", "ap", "ap", 0, 0, 0},
87 };
88 int ds390_nRegs = 13;
89 static void spillThis (symbol *);
90 static void freeAllRegs ();
91 static iCode * packRegsDPTRuse (operand *);
92 static int packRegsDPTRnuse (operand *,unsigned);
93
94 /*-----------------------------------------------------------------*/
95 /* allocReg - allocates register of given type                     */
96 /*-----------------------------------------------------------------*/
97 static regs *
98 allocReg (short type)
99 {
100   int i;
101
102   for (i = 0; i < ds390_nRegs; i++)
103     {
104
105       /* if type is given as 0 then any
106          free register will do */
107       if (!type &&
108           regs390[i].isFree)
109         {
110           regs390[i].isFree = 0;
111           if (currFunc)
112             currFunc->regsUsed =
113               bitVectSetBit (currFunc->regsUsed, i);
114           return &regs390[i];
115         }
116       /* other wise look for specific type
117          of register */
118       if (regs390[i].isFree &&
119           regs390[i].type == type)
120         {
121           regs390[i].isFree = 0;
122           if (currFunc)
123             currFunc->regsUsed =
124               bitVectSetBit (currFunc->regsUsed, i);
125           return &regs390[i];
126         }
127     }
128   return NULL;
129 }
130
131 /*-----------------------------------------------------------------*/
132 /* ds390_regWithIdx - returns pointer to register with index number*/
133 /*-----------------------------------------------------------------*/
134 regs *
135 ds390_regWithIdx (int idx)
136 {
137   int i;
138
139   for (i = 0; i < sizeof(regs390)/sizeof(regs); i++)
140     if (regs390[i].rIdx == idx)
141       return &regs390[i];
142
143   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
144           "regWithIdx not found");
145   exit (1);
146 }
147
148 /*-----------------------------------------------------------------*/
149 /* freeReg - frees a register                                      */
150 /*-----------------------------------------------------------------*/
151 static void
152 freeReg (regs * reg)
153 {
154   if (!reg)
155     {
156       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
157               "freeReg - Freeing NULL register");
158       exit (1);
159     }
160
161   reg->isFree = 1;
162 }
163
164 /*-----------------------------------------------------------------*/
165 /* useReg - marks a register  as used                              */
166 /*-----------------------------------------------------------------*/
167 static void
168 useReg (regs * reg)
169 {
170   reg->isFree = 0;
171 }
172
173
174 /*-----------------------------------------------------------------*/
175 /* nFreeRegs - returns number of free registers                    */
176 /*-----------------------------------------------------------------*/
177 static int
178 nFreeRegs (int type)
179 {
180   int i;
181   int nfr = 0;
182
183   for (i = 0; i < ds390_nRegs; i++)
184     if (regs390[i].isFree && regs390[i].type == type)
185       nfr++;
186   return nfr;
187 }
188
189 /*-----------------------------------------------------------------*/
190 /* nfreeRegsType - free registers with type                         */
191 /*-----------------------------------------------------------------*/
192 static int
193 nfreeRegsType (int type)
194 {
195   int nfr;
196   if (type == REG_PTR)
197     {
198       if ((nfr = nFreeRegs (type)) == 0)
199         return nFreeRegs (REG_GPR);
200     }
201
202   return nFreeRegs (type);
203 }
204
205
206
207 /*-----------------------------------------------------------------*/
208 /* isOperandInReg - returns true if operand is currently in regs   */
209 /*-----------------------------------------------------------------*/
210 static int isOperandInReg(operand *op)
211 {
212     if (!IS_SYMOP(op)) return 0;
213     if (OP_SYMBOL(op)->ruonly) return 1;
214     if (OP_SYMBOL(op)->accuse) return 1;
215     if (OP_SYMBOL(op)->dptr) return 1;
216     return bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(op)->key);
217 }
218
219 /*-----------------------------------------------------------------*/
220 /* computeSpillable - given a point find the spillable live ranges */
221 /*-----------------------------------------------------------------*/
222 static bitVect *
223 computeSpillable (iCode * ic)
224 {
225   bitVect *spillable;
226
227   /* spillable live ranges are those that are live at this
228      point . the following categories need to be subtracted
229      from this set.
230      a) - those that are already spilt
231      b) - if being used by this one
232      c) - defined by this one */
233
234   spillable = bitVectCopy (ic->rlive);
235   spillable =
236     bitVectCplAnd (spillable, _G.spiltSet);     /* those already spilt */
237   spillable =
238     bitVectCplAnd (spillable, ic->uses);        /* used in this one */
239   bitVectUnSetBit (spillable, ic->defKey);
240   spillable = bitVectIntersect (spillable, _G.regAssigned);
241   return spillable;
242
243 }
244
245 /*-----------------------------------------------------------------*/
246 /* noSpilLoc - return true if a variable has no spil location      */
247 /*-----------------------------------------------------------------*/
248 static int
249 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
250 {
251   return (sym->usl.spillLoc ? 0 : 1);
252 }
253
254 /*-----------------------------------------------------------------*/
255 /* hasSpilLoc - will return 1 if the symbol has spil location      */
256 /*-----------------------------------------------------------------*/
257 static int
258 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
259 {
260   return (sym->usl.spillLoc ? 1 : 0);
261 }
262
263 /*-----------------------------------------------------------------*/
264 /* directSpilLoc - will return 1 if the splilocation is in direct  */
265 /*-----------------------------------------------------------------*/
266 static int
267 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
268 {
269   if (sym->usl.spillLoc &&
270       (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
271     return 1;
272   else
273     return 0;
274 }
275
276 /*-----------------------------------------------------------------*/
277 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
278 /*                    but is not used as a pointer                 */
279 /*-----------------------------------------------------------------*/
280 static int
281 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
282 {
283   return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
284 }
285
286 /*-----------------------------------------------------------------*/
287 /* rematable - will return 1 if the remat flag is set              */
288 /*-----------------------------------------------------------------*/
289 static int
290 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
291 {
292   return sym->remat;
293 }
294
295 /*-----------------------------------------------------------------*/
296 /* notUsedInRemaining - not used or defined in remain of the block */
297 /*-----------------------------------------------------------------*/
298 static int
299 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
300 {
301   return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
302           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
303 }
304
305 /*-----------------------------------------------------------------*/
306 /* allLRs - return true for all                                    */
307 /*-----------------------------------------------------------------*/
308 static int
309 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
310 {
311   return 1;
312 }
313
314 /*-----------------------------------------------------------------*/
315 /* liveRangesWith - applies function to a given set of live range  */
316 /*-----------------------------------------------------------------*/
317 static set *
318 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
319                 eBBlock * ebp, iCode * ic)
320 {
321   set *rset = NULL;
322   int i;
323
324   if (!lrs || !lrs->size)
325     return NULL;
326
327   for (i = 1; i < lrs->size; i++)
328     {
329       symbol *sym;
330       if (!bitVectBitValue (lrs, i))
331         continue;
332
333       /* if we don't find it in the live range
334          hash table we are in serious trouble */
335       if (!(sym = hTabItemWithKey (liveRanges, i)))
336         {
337           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
338                   "liveRangesWith could not find liveRange");
339           exit (1);
340         }
341
342       if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
343         addSetHead (&rset, sym);
344     }
345
346   return rset;
347 }
348
349
350 /*-----------------------------------------------------------------*/
351 /* leastUsedLR - given a set determines which is the least used    */
352 /*-----------------------------------------------------------------*/
353 static symbol *
354 leastUsedLR (set * sset)
355 {
356   symbol *sym = NULL, *lsym = NULL;
357
358   sym = lsym = setFirstItem (sset);
359
360   if (!lsym)
361     return NULL;
362
363   for (; lsym; lsym = setNextItem (sset))
364     {
365
366       /* if usage is the same then prefer
367          the spill the smaller of the two */
368       if (lsym->used == sym->used)
369         if (getSize (lsym->type) < getSize (sym->type))
370           sym = lsym;
371
372       /* if less usage */
373       if (lsym->used < sym->used)
374         sym = lsym;
375
376     }
377
378   setToNull ((void *) &sset);
379   sym->blockSpil = 0;
380   return sym;
381 }
382
383 /*-----------------------------------------------------------------*/
384 /* noOverLap - will iterate through the list looking for over lap  */
385 /*-----------------------------------------------------------------*/
386 static int
387 noOverLap (set * itmpStack, symbol * fsym)
388 {
389   symbol *sym;
390
391   for (sym = setFirstItem (itmpStack); sym;
392        sym = setNextItem (itmpStack))
393     {
394         if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
395     }
396   return 1;
397 }
398
399 /*-----------------------------------------------------------------*/
400 /* isFree - will return 1 if the a free spil location is found     */
401 /*-----------------------------------------------------------------*/
402 static
403 DEFSETFUNC (isFree)
404 {
405   symbol *sym = item;
406   V_ARG (symbol **, sloc);
407   V_ARG (symbol *, fsym);
408
409   /* if already found */
410   if (*sloc)
411     return 0;
412
413   /* if it is free && and the itmp assigned to
414      this does not have any overlapping live ranges
415      with the one currently being assigned and
416      the size can be accomodated  */
417   if (sym->isFree &&
418       noOverLap (sym->usl.itmpStack, fsym) &&
419       getSize (sym->type) >= getSize (fsym->type))
420     {
421       *sloc = sym;
422       return 1;
423     }
424
425   return 0;
426 }
427
428 /*-----------------------------------------------------------------*/
429 /* spillLRWithPtrReg :- will spil those live ranges which use PTR  */
430 /*-----------------------------------------------------------------*/
431 static void
432 spillLRWithPtrReg (symbol * forSym)
433 {
434   symbol *lrsym;
435   regs *r0, *r1;
436   int k;
437
438   if (!_G.regAssigned ||
439       bitVectIsZero (_G.regAssigned))
440     return;
441
442   r0 = ds390_regWithIdx (R0_IDX);
443   r1 = ds390_regWithIdx (R1_IDX);
444
445   /* for all live ranges */
446   for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
447        lrsym = hTabNextItem (liveRanges, &k))
448     {
449       int j;
450
451       /* if no registers assigned to it or spilt */
452       /* if it does not overlap this then
453          no need to spill it */
454
455       if (lrsym->isspilt || !lrsym->nRegs ||
456           (lrsym->liveTo < forSym->liveFrom))
457         continue;
458
459       /* go thru the registers : if it is either
460          r0 or r1 then spill it */
461       for (j = 0; j < lrsym->nRegs; j++)
462         if (lrsym->regs[j] == r0 ||
463             lrsym->regs[j] == r1)
464           {
465             spillThis (lrsym);
466             break;
467           }
468     }
469
470 }
471
472 /*-----------------------------------------------------------------*/
473 /* createStackSpil - create a location on the stack to spil        */
474 /*-----------------------------------------------------------------*/
475 static symbol *
476 createStackSpil (symbol * sym)
477 {
478   symbol *sloc = NULL;
479   int useXstack, model, noOverlay;
480
481   char slocBuffer[30];
482
483   /* first go try and find a free one that is already
484      existing on the stack */
485   if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
486     {
487       /* found a free one : just update & return */
488       sym->usl.spillLoc = sloc;
489       sym->stackSpil = 1;
490       sloc->isFree = 0;
491       addSetHead (&sloc->usl.itmpStack, sym);
492       return sym;
493     }
494
495   /* could not then have to create one , this is the hard part
496      we need to allocate this on the stack : this is really a
497      hack!! but cannot think of anything better at this time */
498
499   if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
500                 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
501     {
502       fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
503                __FILE__, __LINE__);
504       exit (1);
505     }
506
507   sloc = newiTemp (slocBuffer);
508
509   /* set the type to the spilling symbol */
510   sloc->type = copyLinkChain (sym->type);
511   sloc->etype = getSpec (sloc->type);
512   if (options.model == MODEL_SMALL) {
513     SPEC_SCLS (sloc->etype) = S_DATA;
514   } else {
515     SPEC_SCLS (sloc->etype) = S_XDATA;
516   }
517   SPEC_EXTR (sloc->etype) = 0;
518   SPEC_STAT (sloc->etype) = 0;
519   SPEC_VOLATILE(sloc->etype) = 0;
520   SPEC_ABSA(sloc->etype) = 0;
521
522   /* we don't allow it to be allocated`
523      onto the external stack since : so we
524      temporarily turn it off ; we also
525      turn off memory model to prevent
526      the spil from going to the external storage
527      and turn off overlaying
528    */
529
530   useXstack = options.useXstack;
531   model = options.model;
532   noOverlay = options.noOverlay;
533   options.noOverlay = 1;
534
535   /* options.model = options.useXstack = 0; */
536
537   allocLocal (sloc);
538
539   options.useXstack = useXstack;
540   options.model = model;
541   options.noOverlay = noOverlay;
542   sloc->isref = 1;              /* to prevent compiler warning */
543
544   /* if it is on the stack then update the stack */
545   if (IN_STACK (sloc->etype))
546     {
547       currFunc->stack += getSize (sloc->type);
548       _G.stackExtend += getSize (sloc->type);
549     }
550   else
551     _G.dataExtend += getSize (sloc->type);
552
553   /* add it to the _G.stackSpil set */
554   addSetHead (&_G.stackSpil, sloc);
555   sym->usl.spillLoc = sloc;
556   sym->stackSpil = 1;
557
558   /* add it to the set of itempStack set
559      of the spill location */
560   addSetHead (&sloc->usl.itmpStack, sym);
561   return sym;
562 }
563
564 /*-----------------------------------------------------------------*/
565 /* isSpiltOnStack - returns true if the spil location is on stack  */
566 /*-----------------------------------------------------------------*/
567 static bool
568 isSpiltOnStack (symbol * sym)
569 {
570   sym_link *etype;
571
572   if (!sym)
573     return FALSE;
574
575   if (!sym->isspilt)
576     return FALSE;
577
578 /*     if (sym->_G.stackSpil) */
579 /*      return TRUE; */
580
581   if (!sym->usl.spillLoc)
582     return FALSE;
583
584   etype = getSpec (sym->usl.spillLoc->type);
585   if (IN_STACK (etype))
586     return TRUE;
587
588   return FALSE;
589 }
590
591 /*-----------------------------------------------------------------*/
592 /* spillThis - spils a specific operand                            */
593 /*-----------------------------------------------------------------*/
594 static void
595 spillThis (symbol * sym)
596 {
597   int i;
598   /* if this is rematerializable or has a spillLocation
599      we are okay, else we need to create a spillLocation
600      for it */
601   if (!(sym->remat || sym->usl.spillLoc))
602     createStackSpil (sym);
603
604   /* mark it has spilt & put it in the spilt set */
605   sym->isspilt = sym->spillA = 1;
606   _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
607
608   bitVectUnSetBit (_G.regAssigned, sym->key);
609   bitVectUnSetBit (_G.totRegAssigned, sym->key);
610
611   for (i = 0; i < sym->nRegs; i++)
612
613     if (sym->regs[i])
614       {
615         freeReg (sym->regs[i]);
616         sym->regs[i] = NULL;
617       }
618
619   /* if spilt on stack then free up r0 & r1
620      if they could have been assigned to some
621      LIVE ranges */
622   if (!ds390_ptrRegReq && isSpiltOnStack (sym) && !options.stack10bit)
623     {
624       ds390_ptrRegReq ++;
625       spillLRWithPtrReg (sym);
626     }
627
628   if (sym->usl.spillLoc && !sym->remat)
629     sym->usl.spillLoc->allocreq++;
630   return;
631 }
632
633 /*-----------------------------------------------------------------*/
634 /* selectSpil - select a iTemp to spil : rather a simple procedure */
635 /*-----------------------------------------------------------------*/
636 static symbol *
637 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
638 {
639   bitVect *lrcs = NULL;
640   set *selectS;
641   symbol *sym;
642
643   /* get the spillable live ranges */
644   lrcs = computeSpillable (ic);
645
646   /* get all live ranges that are rematerizable */
647   if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
648     {
649
650       /* return the least used of these */
651       return leastUsedLR (selectS);
652     }
653
654   /* get live ranges with spillLocations in direct space */
655   if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
656     {
657       sym = leastUsedLR (selectS);
658       strncpyz (sym->rname,
659                 sym->usl.spillLoc->rname[0] ?
660                    sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
661                 sizeof(sym->rname));
662       sym->spildir = 1;
663       /* mark it as allocation required */
664       sym->usl.spillLoc->allocreq++;
665       return sym;
666     }
667
668   /* if the symbol is local to the block then */
669   if (forSym->liveTo < ebp->lSeq)
670     {
671
672       /* check if there are any live ranges allocated
673          to registers that are not used in this block */
674       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
675         {
676           sym = leastUsedLR (selectS);
677           /* if this is not rematerializable */
678           if (!sym->remat)
679             {
680               _G.blockSpil++;
681               sym->blockSpil = 1;
682             }
683           return sym;
684         }
685
686       /* check if there are any live ranges that not
687          used in the remainder of the block */
688       if (!_G.blockSpil &&
689           !isiCodeInFunctionCall (ic) &&       
690           (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
691         {
692           sym = leastUsedLR (selectS);
693           if (sym != forSym)
694             {
695               if (!sym->remat)
696                 {
697                   sym->remainSpil = 1;
698                   _G.blockSpil++;
699                 }
700               return sym;
701             }
702         }
703     }
704
705   /* find live ranges with spillocation && not used as pointers */
706   if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
707     {
708
709       sym = leastUsedLR (selectS);
710       /* mark this as allocation required */
711       sym->usl.spillLoc->allocreq++;
712       return sym;
713     }
714
715   /* find live ranges with spillocation */
716   if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
717     {
718
719       sym = leastUsedLR (selectS);
720       sym->usl.spillLoc->allocreq++;
721       return sym;
722     }
723
724   /* couldn't find then we need to create a spil
725      location on the stack , for which one? the least
726      used ofcourse */
727   if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
728     {
729
730       /* return a created spil location */
731       sym = createStackSpil (leastUsedLR (selectS));
732       sym->usl.spillLoc->allocreq++;
733       return sym;
734     }
735
736   /* this is an extreme situation we will spill
737      this one : happens very rarely but it does happen */
738   spillThis (forSym);
739   return forSym;
740
741 }
742
743 /*-----------------------------------------------------------------*/
744 /* spilSomething - spil some variable & mark registers as free     */
745 /*-----------------------------------------------------------------*/
746 static bool
747 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
748 {
749   symbol *ssym;
750   int i;
751
752   /* get something we can spil */
753   ssym = selectSpil (ic, ebp, forSym);
754
755   /* mark it as spilt */
756   ssym->isspilt = ssym->spillA = 1;
757   _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
758
759   /* mark it as not register assigned &
760      take it away from the set */
761   bitVectUnSetBit (_G.regAssigned, ssym->key);
762   bitVectUnSetBit (_G.totRegAssigned, ssym->key);
763
764   /* mark the registers as free */
765   for (i = 0; i < ssym->nRegs; i++)
766     if (ssym->regs[i])
767       freeReg (ssym->regs[i]);
768
769   /* if spilt on stack then free up r0 & r1
770      if they could have been assigned to as gprs */
771   if (!ds390_ptrRegReq && isSpiltOnStack (ssym) && !options.stack10bit)
772     {
773             ds390_ptrRegReq++;
774       spillLRWithPtrReg (ssym);
775     }
776
777   /* if this was a block level spil then insert push & pop
778      at the start & end of block respectively */
779   if (ssym->blockSpil)
780     {
781       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
782       /* add push to the start of the block */
783       addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
784                                     ebp->sch->next : ebp->sch));
785       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
786       /* add pop to the end of the block */
787       addiCodeToeBBlock (ebp, nic, NULL);
788     }
789
790   /* if spilt because not used in the remainder of the
791      block then add a push before this instruction and
792      a pop at the end of the block */
793   if (ssym->remainSpil)
794     {
795
796       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
797       /* add push just before this instruction */
798       addiCodeToeBBlock (ebp, nic, ic);
799
800       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
801       /* add pop to the end of the block */
802       addiCodeToeBBlock (ebp, nic, NULL);
803     }
804
805   if (ssym == forSym)
806     return FALSE;
807   else
808     return TRUE;
809 }
810
811 /*-----------------------------------------------------------------*/
812 /* getRegPtr - will try for PTR if not a GPR type if not spil      */
813 /*-----------------------------------------------------------------*/
814 static regs *
815 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
816 {
817   regs *reg;
818   int j;
819
820 tryAgain:
821   /* try for a ptr type */
822   if ((reg = allocReg (REG_PTR)))
823     return reg;
824
825   /* try for gpr type */
826   if ((reg = allocReg (REG_GPR)))
827     return reg;
828
829   /* we have to spil */
830   if (!spilSomething (ic, ebp, sym))
831     return NULL;
832
833   /* make sure partially assigned registers aren't reused */
834   for (j=0; j<=sym->nRegs; j++)
835     if (sym->regs[j])
836       sym->regs[j]->isFree = 0;
837
838   /* this looks like an infinite loop but
839      in really selectSpil will abort  */
840   goto tryAgain;
841 }
842
843 /*-----------------------------------------------------------------*/
844 /* getRegGpr - will try for GPR if not spil                        */
845 /*-----------------------------------------------------------------*/
846 static regs *
847 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
848 {
849   regs *reg;
850   int j;
851
852 tryAgain:
853   /* try for gpr type */
854   if ((reg = allocReg (REG_GPR)))
855     return reg;
856
857   if (!ds390_ptrRegReq)
858     if ((reg = allocReg (REG_PTR)))
859       return reg;
860
861   /* we have to spil */
862   if (!spilSomething (ic, ebp, sym))
863     return NULL;
864
865   /* make sure partially assigned registers aren't reused */
866   for (j=0; j<=sym->nRegs; j++)
867     if (sym->regs[j])
868       sym->regs[j]->isFree = 0;
869
870   /* this looks like an infinite loop but
871      in really selectSpil will abort  */
872   goto tryAgain;
873 }
874
875 /*-----------------------------------------------------------------*/
876 /* getRegPtrNoSpil - get it cannot split                           */
877 /*-----------------------------------------------------------------*/
878 static regs *getRegPtrNoSpil()
879 {
880   regs *reg;
881
882   /* try for a ptr type */
883   if ((reg = allocReg (REG_PTR)))
884     return reg;
885
886   /* try for gpr type */
887   if ((reg = allocReg (REG_GPR)))
888     return reg;
889
890   assert(0);
891
892   /* just to make the compiler happy */
893   return 0;
894 }
895
896 /*-----------------------------------------------------------------*/
897 /* getRegGprNoSpil - get it cannot split                           */
898 /*-----------------------------------------------------------------*/
899 static regs *getRegGprNoSpil()
900 {
901
902   regs *reg;
903   if ((reg = allocReg (REG_GPR)))
904     return reg;
905
906   if (!ds390_ptrRegReq)
907     if ((reg = allocReg (REG_PTR)))
908       return reg;
909
910   assert(0);
911
912   /* just to make the compiler happy */
913   return 0;
914 }
915
916 /*-----------------------------------------------------------------*/
917 /* symHasReg - symbol has a given register                         */
918 /*-----------------------------------------------------------------*/
919 static bool
920 symHasReg (symbol * sym, regs * reg)
921 {
922   int i;
923
924   for (i = 0; i < sym->nRegs; i++)
925     if (sym->regs[i] == reg)
926       return TRUE;
927
928   return FALSE;
929 }
930
931 /*-----------------------------------------------------------------*/
932 /* deassignLRs - check the live to and if they have registers & are */
933 /*               not spilt then free up the registers              */
934 /*-----------------------------------------------------------------*/
935 static void
936 deassignLRs (iCode * ic, eBBlock * ebp)
937 {
938   symbol *sym;
939   int k;
940   symbol *result;
941
942   for (sym = hTabFirstItem (liveRanges, &k); sym;
943        sym = hTabNextItem (liveRanges, &k))
944     {
945
946       symbol *psym = NULL;
947       /* if it does not end here */
948       if (sym->liveTo > ic->seq)
949         continue;
950
951       /* if it was spilt on stack then we can
952          mark the stack spil location as free */
953       if (sym->isspilt)
954         {
955           if (sym->stackSpil)
956             {
957               sym->usl.spillLoc->isFree = 1;
958               sym->stackSpil = 0;
959             }
960           continue;
961         }
962
963       if (!bitVectBitValue (_G.regAssigned, sym->key))
964         continue;
965
966       /* special case check if this is an IFX &
967          the privious one was a pop and the
968          previous one was not spilt then keep track
969          of the symbol */
970       if (ic->op == IFX && ic->prev &&
971           ic->prev->op == IPOP &&
972           !ic->prev->parmPush &&
973           !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
974         psym = OP_SYMBOL (IC_LEFT (ic->prev));
975
976       if (sym->nRegs)
977         {
978           int i = 0;
979
980           bitVectUnSetBit (_G.regAssigned, sym->key);
981
982           /* if the result of this one needs registers
983              and does not have it then assign it right
984              away */
985           if (IC_RESULT (ic) &&
986               !(SKIP_IC2 (ic) ||        /* not a special icode */
987                 ic->op == JUMPTABLE ||
988                 ic->op == IFX ||
989                 ic->op == IPUSH ||
990                 ic->op == IPOP ||
991                 ic->op == RETURN ||
992                 POINTER_SET (ic)) &&
993               (result = OP_SYMBOL (IC_RESULT (ic))) &&  /* has a result */
994               result->liveTo > ic->seq &&       /* and will live beyond this */
995               result->liveTo <= ebp->lSeq &&    /* does not go beyond this block */
996               result->liveFrom == ic->seq &&    /* does not start before here */
997               result->regType == sym->regType &&        /* same register types */
998               result->nRegs &&  /* which needs registers */
999               !result->isspilt &&       /* and does not already have them */
1000               !result->remat &&
1001               !bitVectBitValue (_G.regAssigned, result->key) &&
1002           /* the number of free regs + number of regs in this LR
1003              can accomodate the what result Needs */
1004               ((nfreeRegsType (result->regType) +
1005                 sym->nRegs) >= result->nRegs)
1006             )
1007             {
1008
1009               for (i = 0; i < result->nRegs; i++)
1010                 if (i < sym->nRegs)
1011                   result->regs[i] = sym->regs[i];
1012                 else
1013                   result->regs[i] = getRegGpr (ic, ebp, result);
1014
1015               _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1016               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1017
1018             }
1019
1020           /* free the remaining */
1021           for (; i < sym->nRegs; i++)
1022             {
1023               if (psym)
1024                 {
1025                   if (!symHasReg (psym, sym->regs[i]))
1026                     freeReg (sym->regs[i]);
1027                 }
1028               else
1029                 freeReg (sym->regs[i]);
1030             }
1031         }
1032     }
1033 }
1034
1035
1036 /*-----------------------------------------------------------------*/
1037 /* reassignLR - reassign this to registers                         */
1038 /*-----------------------------------------------------------------*/
1039 static void
1040 reassignLR (operand * op)
1041 {
1042   symbol *sym = OP_SYMBOL (op);
1043   int i;
1044
1045   /* not spilt any more */
1046   sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1047   bitVectUnSetBit (_G.spiltSet, sym->key);
1048
1049   _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1050   _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1051
1052   _G.blockSpil--;
1053
1054   for (i = 0; i < sym->nRegs; i++)
1055     sym->regs[i]->isFree = 0;
1056 }
1057
1058 /*-----------------------------------------------------------------*/
1059 /* willCauseSpill - determines if allocating will cause a spill    */
1060 /*-----------------------------------------------------------------*/
1061 static int
1062 willCauseSpill (int nr, int rt)
1063 {
1064   /* first check if there are any avlb registers
1065      of te type required */
1066   if (rt == REG_PTR)
1067     {
1068       /* special case for pointer type
1069          if pointer type not avlb then
1070          check for type gpr */
1071       if (nFreeRegs (rt) >= nr)
1072         return 0;
1073       if (nFreeRegs (REG_GPR) >= nr)
1074         return 0;
1075     }
1076   else
1077     {
1078       if (ds390_ptrRegReq)
1079         {
1080           if (nFreeRegs (rt) >= nr)
1081             return 0;
1082         }
1083       else
1084         {
1085           if (nFreeRegs (REG_PTR) +
1086               nFreeRegs (REG_GPR) >= nr)
1087             return 0;
1088         }
1089     }
1090
1091   /* it will cause a spil */
1092   return 1;
1093 }
1094
1095 /*-----------------------------------------------------------------*/
1096 /* positionRegs - the allocator can allocate same registers to res- */
1097 /* ult and operand, if this happens make sure they are in the same */
1098 /* position as the operand otherwise chaos results                 */
1099 /*-----------------------------------------------------------------*/
1100 static int
1101 positionRegs (symbol * result, symbol * opsym)
1102 {
1103   int count = min (result->nRegs, opsym->nRegs);
1104   int i, j = 0, shared = 0;
1105   int change = 0;
1106
1107   /* if the result has been spilt then cannot share */
1108   if (opsym->isspilt)
1109     return 0;
1110 again:
1111   shared = 0;
1112   /* first make sure that they actually share */
1113   for (i = 0; i < count; i++)
1114     {
1115       for (j = 0; j < count; j++)
1116         {
1117           if (result->regs[i] == opsym->regs[j] && i != j)
1118             {
1119               shared = 1;
1120               goto xchgPositions;
1121             }
1122         }
1123     }
1124 xchgPositions:
1125   if (shared)
1126     {
1127       regs *tmp = result->regs[i];
1128       result->regs[i] = result->regs[j];
1129       result->regs[j] = tmp;
1130       change ++;
1131       goto again;
1132     }
1133   return change;
1134 }
1135
1136 /*-----------------------------------------------------------------*/
1137 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1138 /*-----------------------------------------------------------------*/
1139 bitVect *unusedLRs (eBBlock *ebp)
1140 {
1141     bitVect *ret = NULL;
1142     symbol *sym;
1143     int key;
1144
1145     if (!ebp) return NULL;
1146     for (sym = hTabFirstItem(liveRanges,&key); sym ;
1147          sym = hTabNextItem(liveRanges,&key)) {
1148
1149         if (notUsedInBlock(sym,ebp,NULL)) {
1150             ret = bitVectSetBit(ret,sym->key);
1151         }
1152     }
1153
1154     return ret;
1155 }
1156
1157 /*-----------------------------------------------------------------*/
1158 /* deassignUnsedLRs - if this baisc block ends in a return then    */
1159 /*                    deassign symbols not used in this block      */
1160 /*-----------------------------------------------------------------*/
1161 bitVect *deassignUnsedLRs(eBBlock *ebp)
1162 {
1163     bitVect *unused = NULL;
1164     int i;
1165
1166     switch (returnAtEnd(ebp)) {
1167     case 2: /* successor block ends in a return */
1168         unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1169         /* fall thru */
1170     case 1: /* this block ends in a return */
1171         unused = bitVectIntersect(unused,unusedLRs(ebp));
1172         break;
1173     }
1174
1175     if (unused) {
1176         for (i = 0 ; i < unused->size ; i++ ) {
1177
1178             /* if unused  */
1179             if (bitVectBitValue(unused,i)) {
1180
1181                 /* if assigned to registers */
1182                 if (bitVectBitValue(_G.regAssigned,i)) {
1183                     symbol *sym;
1184                     int j;
1185
1186                     sym = hTabItemWithKey(liveRanges,i);
1187                     /* remove it from regassigned & mark the
1188                        register free */
1189                     bitVectUnSetBit(_G.regAssigned,i);
1190                     for (j = 0 ; j < sym->nRegs; j++)
1191                         freeReg(sym->regs[j]);
1192                 } else {
1193                     /* not assigned to registers : remove from set*/
1194                     bitVectUnSetBit(unused,i);
1195                 }
1196             }
1197         }
1198     }
1199     return unused;
1200 }
1201
1202 /*-----------------------------------------------------------------*/
1203 /* reassignUnusedLRs - put registers to unused Live ranges         */
1204 /*-----------------------------------------------------------------*/
1205 void reassignUnusedLRs (bitVect *unused)
1206 {
1207     int i;
1208     if (!unused) return ;
1209
1210     for (i = 0 ; i < unused->size ; i++ ) {
1211         /* if unused : means it was assigned to registers before */
1212         if (bitVectBitValue(unused,i)) {
1213             symbol *sym;
1214             int j;
1215
1216             /* put it back into reg set*/
1217             bitVectSetBit(_G.regAssigned,i) ;
1218
1219             sym = hTabItemWithKey(liveRanges,i);
1220             /* makr registers busy */
1221             for (j = 0 ; j < sym->nRegs; j++)
1222                 sym->regs[j]->isFree = 0;
1223         }
1224     }
1225 }
1226
1227 /*------------------------------------------------------------------*/
1228 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1229 /* it should either have registers or have beed spilled. Otherwise, */
1230 /* there was an uninitialized variable, so just spill this to get   */
1231 /* the operand in a valid state.                                    */
1232 /*------------------------------------------------------------------*/
1233 static void
1234 verifyRegsAssigned (operand *op, iCode * ic)
1235 {
1236   symbol * sym;
1237
1238   if (!op) return;
1239   if (!IS_ITEMP (op)) return;
1240
1241   sym = OP_SYMBOL (op);
1242   if (sym->isspilt) return;
1243   if (!sym->nRegs) return;
1244   if (sym->regs[0]) return;
1245
1246   werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1247             sym->prereqv ? sym->prereqv->name : sym->name);
1248   spillThis (sym);
1249 }
1250
1251
1252 /*-----------------------------------------------------------------*/
1253 /* serialRegAssign - serially allocate registers to the variables  */
1254 /*-----------------------------------------------------------------*/
1255 static void
1256 serialRegAssign (eBBlock ** ebbs, int count)
1257 {
1258   int i;
1259
1260   /* for all blocks */
1261   for (i = 0; i < count; i++)
1262       { /* ebbs */
1263
1264       iCode *ic;
1265       bitVect *unusedLRs = NULL;
1266
1267       if (ebbs[i]->noPath &&
1268           (ebbs[i]->entryLabel != entryLabel &&
1269            ebbs[i]->entryLabel != returnLabel))
1270         continue;
1271
1272       unusedLRs = deassignUnsedLRs(ebbs[i]);
1273
1274       /* for all instructions do */
1275       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1276         {
1277
1278           /* if this is an ipop that means some live
1279              range will have to be assigned again */
1280           if (ic->op == IPOP)
1281             reassignLR (IC_LEFT (ic));
1282
1283           /* if result is present && is a true symbol */
1284           if (IC_RESULT (ic) && ic->op != IFX &&
1285               IS_TRUE_SYMOP (IC_RESULT (ic)))
1286             OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1287
1288           /* take away registers from live
1289              ranges that end at this instruction */
1290           deassignLRs (ic, ebbs[i]);
1291
1292           /* some don't need registers */
1293           if (SKIP_IC2 (ic) ||
1294               ic->op == JUMPTABLE ||
1295               ic->op == IFX ||
1296               ic->op == IPUSH ||
1297               ic->op == IPOP ||
1298               (IC_RESULT (ic) && POINTER_SET (ic)))
1299             continue;
1300
1301           /* now we need to allocate registers
1302              only for the result */
1303           if (IC_RESULT (ic))
1304             {
1305               symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1306               bitVect *spillable;
1307               int willCS;
1308               int j;
1309               int ptrRegSet = 0;
1310
1311               /* if it does not need or is spilt
1312                  or is already assigned to registers
1313                  or will not live beyond this instructions */
1314               if (!sym->nRegs ||
1315                   sym->isspilt ||
1316                   bitVectBitValue (_G.regAssigned, sym->key) ||
1317                   sym->liveTo <= ic->seq)
1318                 continue;
1319
1320               /* if some liverange has been spilt at the block level
1321                  and this one live beyond this block then spil this
1322                  to be safe */
1323               if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1324                 {
1325                   spillThis (sym);
1326                   continue;
1327                 }
1328
1329               /* if this is a bit variable then don't use precious registers
1330                  along with expensive bit-to-char conversions but just spill
1331                  it */
1332               if (SPEC_NOUN(sym->etype) == V_BIT)
1333                 {
1334                   spillThis (sym);
1335                   continue;
1336                 }
1337
1338               /* if trying to allocate this will cause
1339                  a spill and there is nothing to spill
1340                  or this one is rematerializable then
1341                  spill this one */
1342               willCS = willCauseSpill (sym->nRegs, sym->regType);
1343               spillable = computeSpillable (ic);
1344               if (sym->remat ||
1345                   (willCS && bitVectIsZero (spillable)))
1346                 {
1347
1348                   spillThis (sym);
1349                   continue;
1350
1351                 }
1352
1353               /* If the live range preceeds the point of definition
1354                  then ideally we must take into account registers that
1355                  have been allocated after sym->liveFrom but freed
1356                  before ic->seq. This is complicated, so spill this
1357                  symbol instead and let fillGaps handle the allocation. */
1358               if (sym->liveFrom < ic->seq)
1359                 {
1360                     spillThis (sym);
1361                     continue;
1362                 }
1363
1364               /* if it has a spillocation & is used less than
1365                  all other live ranges then spill this */
1366                 if (willCS) {
1367                     if (sym->usl.spillLoc) {
1368                         symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1369                                                                          allLRs, ebbs[i], ic));
1370                         if (leastUsed && leastUsed->used > sym->used) {
1371                             spillThis (sym);
1372                             continue;
1373                         }
1374                     } else {
1375                         /* if none of the liveRanges have a spillLocation then better
1376                            to spill this one than anything else already assigned to registers */
1377                         if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1378                             /* if this is local to this block then we might find a block spil */
1379                             if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1380                                 spillThis (sym);
1381                                 continue;
1382                             }
1383                         }
1384                     }
1385                 }
1386
1387               /* if we need ptr regs for the right side
1388                  then mark it */
1389               if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1390                   && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1391                   <= (unsigned) PTRSIZE)
1392                 {
1393                   ds390_ptrRegReq++;
1394                   ptrRegSet = 1;
1395                 }
1396               /* else we assign registers to it */
1397               _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1398               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1399
1400               for (j = 0; j < sym->nRegs; j++)
1401                 {
1402                   if (sym->regType == REG_PTR)
1403                     sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1404                   else
1405                     sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1406
1407                   /* if the allocation falied which means
1408                      this was spilt then break */
1409                   if (!sym->regs[j])
1410                     break;
1411                 }
1412
1413               /* if it shares registers with operands make sure
1414                  that they are in the same position */
1415               if (!POINTER_SET(ic) && !POINTER_GET(ic))
1416                 {
1417                   if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1418                       OP_SYMBOL (IC_LEFT (ic))->nRegs)
1419                     {
1420                       positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1421                                     OP_SYMBOL (IC_LEFT (ic)));
1422                     }
1423                   /* do the same for the right operand */
1424                   if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1425                       OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1426                     {
1427                       positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1428                                     OP_SYMBOL (IC_RIGHT (ic)));
1429                     }
1430                 }
1431
1432               if (ptrRegSet)
1433                 {
1434                   ds390_ptrRegReq--;
1435                   ptrRegSet = 0;
1436                 }
1437
1438             }
1439         }
1440       reassignUnusedLRs(unusedLRs);
1441     }
1442
1443     /* Check for and fix any problems with uninitialized operands */
1444     for (i = 0; i < count; i++)
1445       {
1446         iCode *ic;
1447
1448         if (ebbs[i]->noPath &&
1449             (ebbs[i]->entryLabel != entryLabel &&
1450              ebbs[i]->entryLabel != returnLabel))
1451             continue;
1452
1453         for (ic = ebbs[i]->sch; ic; ic = ic->next)
1454           {
1455             if (SKIP_IC2 (ic))
1456               continue;
1457
1458             if (ic->op == IFX)
1459               {
1460                 verifyRegsAssigned (IC_COND (ic), ic);
1461                 continue;
1462               }
1463
1464             if (ic->op == JUMPTABLE)
1465               {
1466                 verifyRegsAssigned (IC_JTCOND (ic), ic);
1467                 continue;
1468               }
1469
1470             verifyRegsAssigned (IC_RESULT (ic), ic);
1471             verifyRegsAssigned (IC_LEFT (ic), ic);
1472             verifyRegsAssigned (IC_RIGHT (ic), ic);
1473           }
1474       }
1475 }
1476
1477 /*-----------------------------------------------------------------*/
1478 /* fillGaps - Try to fill in the Gaps left by Pass1                */
1479 /*-----------------------------------------------------------------*/
1480 static void fillGaps()
1481 {
1482     symbol *sym =NULL;
1483     int key =0;
1484     int loop = 0, change;
1485     int pass;
1486
1487     if (getenv("DISABLE_FILL_GAPS")) return;
1488
1489     /* First try to do DPTRuse once more since now we know what got into
1490        registers */
1491
1492     while (loop++ < 10) {
1493         change = 0;
1494
1495         for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1496              sym = hTabNextItem(liveRanges,&key)) {
1497             int size = getSize(sym->type);
1498
1499             if (sym->liveFrom == sym->liveTo) continue;
1500
1501             if (sym->uptr && sym->dptr==0 && !sym->ruonly &&
1502                 size < 4 && size > 1) {
1503
1504                 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1505
1506                     /* if this was assigned to registers then */
1507                     if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1508                         /* take it out of the register assigned set */
1509                         bitVectUnSetBit(_G.totRegAssigned,sym->key);
1510                     } else if (sym->usl.spillLoc) {
1511                         sym->usl.spillLoc->allocreq--;
1512                         sym->usl.spillLoc = NULL;
1513                     }
1514
1515                     sym->nRegs = 0;
1516                     sym->isspilt = sym->spillA = 0;
1517                     continue ;
1518                 }
1519
1520                 /* try assigning other dptrs */
1521                 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1522                     /* if this was ssigned to registers then */
1523                     if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1524                         /* take it out of the register assigned set */
1525                         bitVectUnSetBit(_G.totRegAssigned,sym->key);
1526                     } else if (sym->usl.spillLoc) {
1527                         sym->usl.spillLoc->allocreq--;
1528                         sym->usl.spillLoc = NULL;
1529                     }
1530                     sym->nRegs = 0;
1531                     sym->isspilt = sym->spillA = 0;
1532                 }
1533             }
1534         }
1535
1536         /* look for livernages that was spilt by the allocator */
1537         for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1538              sym = hTabNextItem(liveRanges,&key)) {
1539
1540             int i;
1541             int pdone = 0;
1542
1543             if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1544             if (!sym->uses || !sym->defs) continue ;
1545             /* find the liveRanges this one clashes with, that are
1546                still assigned to registers & mark the registers as used*/
1547             for ( i = 0 ; i < sym->clashes->size ; i ++) {
1548                 int k;
1549                 symbol *clr;
1550
1551                 if (bitVectBitValue(sym->clashes,i) == 0 ||    /* those that clash with this */
1552                     bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1553                     continue ;
1554
1555                 clr = hTabItemWithKey(liveRanges,i);
1556                 assert(clr);
1557
1558                 /* mark these registers as used */
1559                 for (k = 0 ; k < clr->nRegs ; k++ )
1560                     useReg(clr->regs[k]);
1561             }
1562
1563             if (willCauseSpill(sym->nRegs,sym->regType)) {
1564                 /* NOPE :( clear all registers & and continue */
1565                 freeAllRegs();
1566                 continue ;
1567             }
1568
1569             /* THERE IS HOPE !!!! */
1570             for (i=0; i < sym->nRegs ; i++ ) {
1571                 if (sym->regType == REG_PTR)
1572                     sym->regs[i] = getRegPtrNoSpil ();
1573                 else
1574                     sym->regs[i] = getRegGprNoSpil ();
1575             }
1576
1577             /* For all its definitions check if the registers
1578                allocated needs positioning NOTE: we can position
1579                only ONCE if more than One positioning required
1580                then give up.
1581                We may need to perform the checks twice; once to
1582                position the registers as needed, the second to
1583                verify any register repositioning is still
1584                compatible.
1585               */
1586             sym->isspilt = 0;
1587             for (pass=0; pass<2; pass++) {
1588                 for (i = 0 ; i < sym->defs->size ; i++ ) {
1589                     if (bitVectBitValue(sym->defs,i)) {
1590                         iCode *ic;
1591                         if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1592                         if (SKIP_IC(ic)) continue;
1593                         assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1594                         /* if left is assigned to registers */
1595                         if (IS_SYMOP(IC_LEFT(ic)) &&
1596                           bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1597                             pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1598                         }
1599                         if (IS_SYMOP(IC_RIGHT(ic)) &&
1600                           bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1601                             pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1602                         }
1603                         if (pdone > 1) break;
1604                     }
1605                 }
1606                 for (i = 0 ; i < sym->uses->size ; i++ ) {
1607                     if (bitVectBitValue(sym->uses,i)) {
1608                         iCode *ic;
1609                         if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1610                         if (SKIP_IC(ic)) continue;
1611                         if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1612
1613                         /* if result is assigned to registers */
1614                         if (IS_SYMOP(IC_RESULT(ic)) &&
1615                           bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1616                             pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1617                         }
1618                         if (pdone > 1) break;
1619                     }
1620                 }
1621                 if (pdone == 0) break; /* second pass only if regs repositioned */
1622                 if (pdone > 1) break;
1623             }
1624             /* had to position more than once GIVE UP */
1625             if (pdone > 1) {
1626                 /* UNDO all the changes we made to try this */
1627                 sym->isspilt = 1;
1628                 for (i=0; i < sym->nRegs ; i++ ) {
1629                     sym->regs[i] = NULL;
1630                 }
1631                 freeAllRegs();
1632                 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1633                             "%s in function %s\n",
1634                             sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1635                 continue ;
1636             }
1637             D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1638                         sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1639             _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1640             sym->isspilt = sym->spillA = 0 ;
1641             sym->usl.spillLoc->allocreq--;
1642             sym->usl.spillLoc = NULL;
1643             freeAllRegs();
1644             change ++;
1645         }
1646         if (!change) break;
1647     }
1648 }
1649
1650 /*-----------------------------------------------------------------*/
1651 /* rUmaskForOp :- returns register mask for an operand             */
1652 /*-----------------------------------------------------------------*/
1653 bitVect *
1654 ds390_rUmaskForOp (operand * op)
1655 {
1656   bitVect *rumask;
1657   symbol *sym;
1658   int j;
1659
1660   /* only temporaries are assigned registers */
1661   if (!IS_ITEMP (op))
1662     return NULL;
1663
1664   sym = OP_SYMBOL (op);
1665
1666   /* if spilt or no registers assigned to it
1667      then nothing */
1668   if (sym->isspilt || !sym->nRegs)
1669     return NULL;
1670
1671   rumask = newBitVect (ds390_nRegs);
1672
1673   for (j = 0; j < sym->nRegs; j++)
1674     {
1675       rumask = bitVectSetBit (rumask,
1676                               sym->regs[j]->rIdx);
1677     }
1678
1679   return rumask;
1680 }
1681
1682 /*-----------------------------------------------------------------*/
1683 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1684 /*-----------------------------------------------------------------*/
1685 static bitVect *
1686 regsUsedIniCode (iCode * ic)
1687 {
1688   bitVect *rmask = newBitVect (ds390_nRegs);
1689
1690   /* do the special cases first */
1691   if (ic->op == IFX)
1692     {
1693       rmask = bitVectUnion (rmask,
1694                             ds390_rUmaskForOp (IC_COND (ic)));
1695       goto ret;
1696     }
1697
1698   /* for the jumptable */
1699   if (ic->op == JUMPTABLE)
1700     {
1701       rmask = bitVectUnion (rmask,
1702                             ds390_rUmaskForOp (IC_JTCOND (ic)));
1703
1704       goto ret;
1705     }
1706
1707   /* of all other cases */
1708   if (IC_LEFT (ic))
1709     rmask = bitVectUnion (rmask,
1710                           ds390_rUmaskForOp (IC_LEFT (ic)));
1711
1712
1713   if (IC_RIGHT (ic))
1714     rmask = bitVectUnion (rmask,
1715                           ds390_rUmaskForOp (IC_RIGHT (ic)));
1716
1717   if (IC_RESULT (ic))
1718     rmask = bitVectUnion (rmask,
1719                           ds390_rUmaskForOp (IC_RESULT (ic)));
1720
1721 ret:
1722   return rmask;
1723 }
1724
1725 /*-----------------------------------------------------------------*/
1726 /* createRegMask - for each instruction will determine the regsUsed */
1727 /*-----------------------------------------------------------------*/
1728 static void
1729 createRegMask (eBBlock ** ebbs, int count)
1730 {
1731   int i;
1732
1733   /* for all blocks */
1734   for (i = 0; i < count; i++)
1735     {
1736       iCode *ic;
1737
1738       if (ebbs[i]->noPath &&
1739           (ebbs[i]->entryLabel != entryLabel &&
1740            ebbs[i]->entryLabel != returnLabel))
1741         continue;
1742
1743       /* for all instructions */
1744       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1745         {
1746
1747           int j;
1748
1749           if (SKIP_IC2 (ic) || !ic->rlive)
1750             continue;
1751
1752           /* first mark the registers used in this
1753              instruction */
1754           ic->rUsed = regsUsedIniCode (ic);
1755           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1756
1757           /* now create the register mask for those
1758              registers that are in use : this is a
1759              super set of ic->rUsed */
1760           ic->rMask = newBitVect (ds390_nRegs + 1);
1761
1762           /* for all live Ranges alive at this point */
1763           for (j = 1; j < ic->rlive->size; j++)
1764             {
1765               symbol *sym;
1766               int k;
1767
1768               /* if not alive then continue */
1769               if (!bitVectBitValue (ic->rlive, j))
1770                 continue;
1771
1772               /* find the live range we are interested in */
1773               if (!(sym = hTabItemWithKey (liveRanges, j)))
1774                 {
1775                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1776                           "createRegMask cannot find live range");
1777                   fprintf(stderr, "\tmissing live range: key=%d\n", j);
1778                   exit (0);
1779                 }
1780 #if 0
1781               /* special case for ruonly */
1782               if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1783                   int size = getSize(sym->type);
1784                   int j = DPL_IDX;
1785                   for (k = 0 ; k < size; k++ )
1786                       ic->rMask = bitVectSetBit (ic->rMask, j++);
1787                   continue ;
1788               }
1789 #endif
1790               /* if no register assigned to it */
1791               if (!sym->nRegs || sym->isspilt)
1792                 continue;
1793
1794               /* for all the registers allocated to it */
1795               for (k = 0; k < sym->nRegs; k++)
1796                 if (sym->regs[k])
1797                   ic->rMask =
1798                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1799             }
1800         }
1801     }
1802 }
1803
1804 /*-----------------------------------------------------------------*/
1805 /* rematStr - returns the rematerialized string for a remat var    */
1806 /*-----------------------------------------------------------------*/
1807 static char *
1808 rematStr (symbol * sym)
1809 {
1810   char *s = buffer;
1811   iCode *ic = sym->rematiCode;
1812
1813   *s = 0;
1814
1815   while (1)
1816     {
1817
1818       /* if plus or minus print the right hand side */
1819       if (ic->op == '+' || ic->op == '-')
1820         {
1821           SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1822                     "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1823                     ic->op);
1824           s += strlen (s);
1825           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1826           continue;
1827         }
1828
1829       /* cast then continue */
1830       if (IS_CAST_ICODE(ic)) {
1831           ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1832           continue;
1833       }
1834       /* we reached the end */
1835       SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1836                 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1837       break;
1838     }
1839
1840   return buffer;
1841 }
1842
1843 /*-----------------------------------------------------------------*/
1844 /* regTypeNum - computes the type & number of registers required   */
1845 /*-----------------------------------------------------------------*/
1846 static void
1847 regTypeNum ()
1848 {
1849   symbol *sym;
1850   int k;
1851   iCode *ic;
1852
1853   /* for each live range do */
1854   for (sym = hTabFirstItem (liveRanges, &k); sym;
1855        sym = hTabNextItem (liveRanges, &k))
1856     {
1857
1858       /* if used zero times then no registers needed */
1859       if ((sym->liveTo - sym->liveFrom) == 0)
1860         continue;
1861
1862
1863       /* if the live range is a temporary */
1864       if (sym->isitmp)
1865         {
1866
1867           /* if the type is marked as a conditional */
1868           if (sym->regType == REG_CND)
1869             continue;
1870
1871           /* if used in return only then we don't
1872              need registers */
1873           if (sym->ruonly || sym->accuse)
1874             {
1875               if (IS_AGGREGATE (sym->type) || sym->isptr)
1876                 sym->type = aggrToPtr (sym->type, FALSE);
1877               continue;
1878             }
1879
1880           /* if the symbol has only one definition &
1881              that definition is a get_pointer */
1882           if (bitVectnBitsOn (sym->defs) == 1 &&
1883               (ic = hTabItemWithKey (iCodehTab,
1884                                      bitVectFirstBit (sym->defs))) &&
1885               POINTER_GET (ic) &&
1886               !IS_BITVAR (sym->etype) &&
1887               (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1888             {
1889
1890               if (ptrPseudoSymSafe (sym, ic))
1891                 {
1892                   ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1893                   continue;
1894                 }
1895
1896               /* if in data space or idata space then try to
1897                  allocate pointer register */
1898
1899             }
1900
1901           /* if not then we require registers */
1902           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1903                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1904                         getSize (sym->type));
1905
1906           if (sym->nRegs > 4)
1907             {
1908               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1909               printTypeChain (sym->type, stderr);
1910               fprintf (stderr, "\n");
1911             }
1912
1913           /* determine the type of register required */
1914           if (sym->nRegs == 1 &&
1915               IS_PTR (sym->type) &&
1916               sym->uptr)
1917             sym->regType = REG_PTR;
1918           else
1919             sym->regType = REG_GPR;
1920
1921         }
1922       else
1923         /* for the first run we don't provide */
1924         /* registers for true symbols we will */
1925         /* see how things go                  */
1926         sym->nRegs = 0;
1927     }
1928
1929 }
1930
1931 /*-----------------------------------------------------------------*/
1932 /* freeAllRegs - mark all registers as free                        */
1933 /*-----------------------------------------------------------------*/
1934 static void
1935 freeAllRegs ()
1936 {
1937   int i;
1938
1939   for (i = 0; i < ds390_nRegs; i++)
1940     regs390[i].isFree = 1;
1941 }
1942
1943 /*-----------------------------------------------------------------*/
1944 /* deallocStackSpil - this will set the stack pointer back         */
1945 /*-----------------------------------------------------------------*/
1946 static
1947 DEFSETFUNC (deallocStackSpil)
1948 {
1949   symbol *sym = item;
1950
1951   deallocLocal (sym);
1952   return 0;
1953 }
1954
1955 /*-----------------------------------------------------------------*/
1956 /* farSpacePackable - returns the packable icode for far variables */
1957 /*-----------------------------------------------------------------*/
1958 static iCode *
1959 farSpacePackable (iCode * ic)
1960 {
1961   iCode *dic;
1962
1963   /* go thru till we find a definition for the
1964      symbol on the right */
1965   for (dic = ic->prev; dic; dic = dic->prev)
1966     {
1967       /* if the definition is a call then no */
1968       if ((dic->op == CALL || dic->op == PCALL) &&
1969           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1970         {
1971           return NULL;
1972         }
1973
1974       /* if shift by unknown amount then not */
1975       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1976           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1977         return NULL;
1978
1979       /* if pointer get and size > 1 */
1980       if (POINTER_GET (dic) &&
1981           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1982         return NULL;
1983
1984       if (POINTER_SET (dic) &&
1985           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1986         return NULL;
1987
1988       /* if any tree is a true symbol in far space */
1989       if (IC_RESULT (dic) &&
1990           IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1991           isOperandInFarSpace (IC_RESULT (dic)))
1992         return NULL;
1993
1994       if (IC_RIGHT (dic) &&
1995           IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1996           isOperandInFarSpace (IC_RIGHT (dic)) &&
1997           !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1998         return NULL;
1999
2000       if (IC_LEFT (dic) &&
2001           IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2002           isOperandInFarSpace (IC_LEFT (dic)) &&
2003           !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2004         return NULL;
2005
2006       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2007         {
2008           if ((dic->op == LEFT_OP ||
2009                dic->op == RIGHT_OP ||
2010                dic->op == '-') &&
2011               IS_OP_LITERAL (IC_RIGHT (dic)))
2012             return NULL;
2013           else
2014             return dic;
2015         }
2016     }
2017
2018   return NULL;
2019 }
2020
2021 /*-----------------------------------------------------------------*/
2022 /* packRegsForAssign - register reduction for assignment           */
2023 /*-----------------------------------------------------------------*/
2024 static int
2025 packRegsForAssign (iCode * ic, eBBlock * ebp)
2026 {
2027   iCode *dic, *sic;
2028
2029   if (!IS_ITEMP (IC_RIGHT (ic)) ||
2030       OP_SYMBOL (IC_RIGHT (ic))->isind ||
2031       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2032     {
2033       return 0;
2034     }
2035
2036   /* if the true symbol is defined in far space or on stack
2037      then we should not since this will increase register pressure */
2038 #if 0
2039   if (isOperandInFarSpace (IC_RESULT (ic)))
2040     {
2041       if ((dic = farSpacePackable (ic)))
2042         goto pack;
2043       else
2044         return 0;
2045     }
2046 #else
2047   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2048     return 0;
2049   }
2050 #endif
2051
2052   /* find the definition of iTempNN scanning backwards if we find a
2053      a use of the true symbol in before we find the definition then
2054      we cannot */
2055   for (dic = ic->prev; dic; dic = dic->prev)
2056     {
2057       /* if there is a function call then don't pack it */
2058       if ((dic->op == CALL || dic->op == PCALL))
2059         {
2060           dic = NULL;
2061           break;
2062         }
2063
2064       if (SKIP_IC2 (dic))
2065         continue;
2066
2067       if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2068           IS_OP_VOLATILE (IC_RESULT (dic)))
2069         {
2070           dic = NULL;
2071           break;
2072         }
2073
2074       if (IS_SYMOP (IC_RESULT (dic)) &&
2075           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2076         {
2077           if (POINTER_SET (dic))
2078             dic = NULL;
2079
2080           break;
2081         }
2082
2083       if (IS_SYMOP (IC_RIGHT (dic)) &&
2084           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2085            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2086         {
2087           dic = NULL;
2088           break;
2089         }
2090
2091       if (IS_SYMOP (IC_LEFT (dic)) &&
2092           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2093            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2094         {
2095           dic = NULL;
2096           break;
2097         }
2098
2099       if (POINTER_SET (dic) &&
2100           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2101         {
2102           dic = NULL;
2103           break;
2104         }
2105     }
2106
2107   if (!dic)
2108     return 0;                   /* did not find */
2109
2110   /* if the result is on stack or iaccess then it must be
2111      the same atleast one of the operands */
2112   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2113       OP_SYMBOL (IC_RESULT (ic))->iaccess)
2114     {
2115
2116       /* the operation has only one symbol
2117          operator then we can pack */
2118       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2119           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2120         goto pack;
2121
2122       if (!((IC_LEFT (dic) &&
2123              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2124             (IC_RIGHT (dic) &&
2125              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2126         return 0;
2127     }
2128 pack:
2129   /* found the definition */
2130   /* replace the result with the result of */
2131   /* this assignment and remove this assignment */
2132   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2133
2134   IC_RESULT (dic) = IC_RESULT (ic);
2135
2136   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2137     {
2138       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2139     }
2140   /* delete from liverange table also
2141      delete from all the points inbetween and the new
2142      one */
2143   for (sic = dic; sic != ic; sic = sic->next)
2144     {
2145       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2146       if (IS_ITEMP (IC_RESULT (dic)))
2147         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2148     }
2149
2150   remiCodeFromeBBlock (ebp, ic);
2151   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2152   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2153   OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2154   return 1;
2155 }
2156
2157 /*------------------------------------------------------------------*/
2158 /* findAssignToSym : scanning backwards looks for first assig found */
2159 /*------------------------------------------------------------------*/
2160 static iCode *
2161 findAssignToSym (operand * op, iCode * ic)
2162 {
2163   iCode *dic;
2164
2165   /* This routine is used to find sequences like
2166      iTempAA = FOO;
2167      ...;  (intervening ops don't use iTempAA or modify FOO)
2168      blah = blah + iTempAA;
2169
2170      and eliminate the use of iTempAA, freeing up its register for
2171      other uses.
2172   */
2173
2174   for (dic = ic->prev; dic; dic = dic->prev)
2175     {
2176
2177       /* if definition by assignment */
2178       if (dic->op == '=' &&
2179           !POINTER_SET (dic) &&
2180           IC_RESULT (dic)->key == op->key
2181 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2182         )
2183         {
2184
2185           /* we are interested only if defined in far space */
2186           /* or in stack space in case of + & - */
2187
2188           /* if assigned to a non-symbol then return
2189              FALSE */
2190           if (!IS_SYMOP (IC_RIGHT (dic)))
2191             return NULL;
2192
2193           /* if the symbol is in far space then
2194              we should not */
2195           if (isOperandInFarSpace (IC_RIGHT (dic)))
2196             return NULL;
2197
2198           /* for + & - operations make sure that
2199              if it is on the stack it is the same
2200              as one of the three operands */
2201           if ((ic->op == '+' || ic->op == '-') &&
2202               OP_SYMBOL (IC_RIGHT (dic))->onStack)
2203             {
2204
2205               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2206                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2207                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2208                 return NULL;
2209             }
2210
2211           break;
2212
2213         }
2214
2215       /* if we find an usage then we cannot delete it */
2216       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2217         return NULL;
2218
2219       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2220         return NULL;
2221
2222       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2223         return NULL;
2224     }
2225
2226   /* now make sure that the right side of dic
2227      is not defined between ic & dic */
2228   if (dic)
2229     {
2230       iCode *sic = dic->next;
2231
2232       for (; sic != ic; sic = sic->next)
2233         if (IC_RESULT (sic) &&
2234             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2235           return NULL;
2236     }
2237
2238   return dic;
2239
2240
2241 }
2242
2243 /*-----------------------------------------------------------------*/
2244 /* packRegsForSupport :- reduce some registers for support calls   */
2245 /*-----------------------------------------------------------------*/
2246 static int
2247 packRegsForSupport (iCode * ic, eBBlock * ebp)
2248 {
2249   int change = 0;
2250
2251   /* for the left & right operand :- look to see if the
2252      left was assigned a true symbol in far space in that
2253      case replace them */
2254   if (IS_ITEMP (IC_LEFT (ic)) &&
2255       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2256     {
2257       iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2258       iCode *sic;
2259
2260       if (!dic)
2261         goto right;
2262
2263       /* found it we need to remove it from the
2264          block */
2265       for (sic = dic; sic != ic; sic = sic->next) {
2266         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2267         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2268       }
2269
2270       wassert(IS_SYMOP(IC_LEFT (ic)));
2271       wassert(IS_SYMOP(IC_RIGHT (dic)));
2272       IC_LEFT (ic)->operand.symOperand =
2273         IC_RIGHT (dic)->operand.symOperand;
2274       OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2275       IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2276       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2277       remiCodeFromeBBlock (ebp, dic);
2278       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2279       change++;
2280     }
2281
2282   /* do the same for the right operand */
2283 right:
2284   if (!change &&
2285       IS_ITEMP (IC_RIGHT (ic)) &&
2286       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2287     {
2288       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2289       iCode *sic;
2290
2291       if (!dic)
2292         return change;
2293
2294       /* if this is a subtraction & the result
2295          is a true symbol in far space then don't pack */
2296       if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2297         {
2298           sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2299           if (IN_FARSPACE (SPEC_OCLS (etype)))
2300             return change;
2301         }
2302       /* found it we need to remove it from the
2303          block */
2304       for (sic = dic; sic != ic; sic = sic->next) {
2305         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2306         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2307       }
2308
2309       wassert(IS_SYMOP(IC_RIGHT (ic)));
2310       wassert(IS_SYMOP(IC_RIGHT (dic)));
2311       IC_RIGHT (ic)->operand.symOperand =
2312         IC_RIGHT (dic)->operand.symOperand;
2313       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2314       OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2315       remiCodeFromeBBlock (ebp, dic);
2316       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2317       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2318       change++;
2319     }
2320
2321   return change;
2322 }
2323
2324 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2325
2326
2327 /*-----------------------------------------------------------------*/
2328 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2329 /*-----------------------------------------------------------------*/
2330 static int packRegsDPTRnuse( operand *op , unsigned dptr)
2331 {
2332     int i,key;
2333     iCode *ic;
2334
2335     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2336     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr)
2337         return 0;
2338
2339     /* first check if any overlapping liverange has already been
2340        assigned to this DPTR */
2341     if (OP_SYMBOL(op)->clashes) {
2342         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2343             symbol *sym;
2344             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2345                 sym = hTabItemWithKey(liveRanges,i);
2346                 if (sym->dptr == dptr) return 0;
2347             }
2348         }
2349     }
2350
2351     /* future for more dptrs */
2352     if (dptr > 1) {
2353         OP_SYMBOL(op)->dptr = dptr;
2354         return 1;
2355     }
2356
2357     /* DPTR1 is special since it is also used as a scratch by the backend .
2358        so we walk thru the entire live range of this operand and make sure
2359        DPTR1 will not be used by the backed . The logic here is to find out if
2360        more than one operand in an icode is in far space then we give up : we
2361        don't keep it live across functions for now
2362     */
2363
2364     ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2365     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2366          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2367         int nfs =0;
2368
2369         if (ic->op == CALL || ic->op == PCALL) return 0;
2370
2371         /* single operand icode are ok */
2372         if (ic->op == IFX || ic->op == IPUSH)
2373             continue ;
2374
2375         if (ic->op == SEND ) {
2376             if (ic->argreg != 1 ) return 0;
2377             else continue ;
2378         }
2379         /* two special cases first */
2380         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op)  && /* pointer get */
2381             !OP_SYMBOL(IC_LEFT(ic))->ruonly                     && /* with result in far space */
2382             (isOperandInFarSpace(IC_RESULT(ic)) &&
2383              !isOperandInReg(IC_RESULT(ic)))) {
2384             return 0;
2385         }
2386
2387         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op)        && /* pointer set */
2388             !OP_SYMBOL(IC_RESULT(ic))->ruonly                           && /* with right in far space */
2389             (isOperandInFarSpace(IC_RIGHT(ic)) &&
2390              !isOperandInReg(IC_RIGHT(ic)))) {
2391             return 0;
2392         }
2393
2394         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))    && /* if symbol operand */
2395             !isOperandEqual(IC_RESULT(ic),op)           && /* not the same as this */
2396             ((isOperandInFarSpace(IC_RESULT(ic)) ||        /* in farspace or */
2397               OP_SYMBOL(IC_RESULT(ic))->onStack)        && /* on the stack   */
2398              !isOperandInReg(IC_RESULT(ic)))) {            /* and not in register */
2399             nfs++;
2400         }
2401         /* same for left */
2402         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))        && /* if symbol operand */
2403             !isOperandEqual(IC_LEFT(ic),op)             && /* not the same as this */
2404             ((isOperandInFarSpace(IC_LEFT(ic)) ||          /* in farspace or */
2405               OP_SYMBOL(IC_LEFT(ic))->onStack)          && /* on the stack   */
2406              !isOperandInReg(IC_LEFT(ic)))) {              /* and not in register */
2407             nfs++;
2408         }
2409         /* same for right */
2410         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))      && /* if symbol operand */
2411             !isOperandEqual(IC_RIGHT(ic),op)            && /* not the same as this */
2412             ((isOperandInFarSpace(IC_RIGHT(ic)) ||         /* in farspace or */
2413               OP_SYMBOL(IC_RIGHT(ic))->onStack)         && /* on the stack   */
2414              !isOperandInReg(IC_RIGHT(ic)))) {             /* and not in register */
2415             nfs++;
2416         }
2417
2418         // Check that no other ops in this range have been assigned to dptr1.
2419         // I don't understand why this is not caught by the first check, above.
2420         // But it isn't always, see bug 769624.
2421         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2422             (OP_SYMBOL(IC_RESULT(ic))->dptr == 1))
2423         {
2424             //fprintf(stderr, "dptr1 already in use in live range #1\n");
2425             return 0;
2426         }
2427
2428         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2429             (OP_SYMBOL(IC_LEFT(ic))->dptr == 1))
2430         {
2431             //fprintf(stderr, "dptr1 already in use in live range # 2\n");
2432             return 0;
2433         }
2434
2435         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2436             (OP_SYMBOL(IC_RIGHT(ic))->dptr == 1))
2437         {
2438             //fprintf(stderr, "dptr1 already in use in live range # 3\n");
2439             return 0;
2440         }
2441
2442         if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2443             OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2444
2445         if (nfs > 1) return 0;
2446     }
2447     OP_SYMBOL(op)->dptr = dptr;
2448     return 1;
2449 }
2450
2451 /*-----------------------------------------------------------------*/
2452 /* packRegsDPTRuse : - will reduce some registers for single Use */
2453 /*-----------------------------------------------------------------*/
2454 static iCode *
2455 packRegsDPTRuse (operand * op)
2456 {
2457     /* go thru entire liveRange of this variable & check for
2458        other possible usage of DPTR , if we don't find it the
2459        assign this to DPTR (ruonly)
2460     */
2461     int i, key;
2462     symbol *sym;
2463     iCode *ic, *dic;
2464     sym_link *type, *etype;
2465
2466     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2467     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL;
2468
2469     /* first check if any overlapping liverange has already been
2470        assigned to DPTR */
2471     if (OP_SYMBOL(op)->clashes) {
2472         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2473             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2474                 sym = hTabItemWithKey(liveRanges,i);
2475                 if (sym->ruonly) return NULL ;
2476             }
2477         }
2478     }
2479
2480     /* no then go thru this guys live range */
2481     dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2482     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2483          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2484
2485         if (SKIP_IC3(ic)) continue;
2486
2487         /* if PCALL cannot be sure give up */
2488         if (ic->op == PCALL) return NULL;
2489
2490         /* if SEND & not the first parameter then giveup */
2491         if (ic->op == SEND && ic->argreg != 1 &&
2492             ((isOperandInFarSpace(IC_LEFT(ic))  && !isOperandInReg(IC_LEFT(ic))) ||
2493              isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2494
2495         /* if CALL then make sure it is VOID || return value not used
2496            or the return value is assigned to this one */
2497         if (ic->op == CALL) {
2498             if (OP_SYMBOL(IC_RESULT(ic))->liveTo ==
2499                 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2500             etype = getSpec(type = operandType(IC_RESULT(ic)));
2501             if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic)))
2502                 continue ;
2503             return NULL ;
2504         }
2505
2506         /* special case of add with a [remat] */
2507         if (ic->op == '+' &&
2508             OP_SYMBOL(IC_LEFT(ic))->remat &&
2509             (isOperandInFarSpace(IC_RIGHT(ic)) &&
2510              !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2511
2512         /* special cases  */
2513         /* pointerGet */
2514         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2515             getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2516
2517         /* pointerSet */
2518         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2519             getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2520
2521         /* conditionals can destroy 'b' - make sure B wont
2522            be used in this one*/
2523         if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/'  ||
2524              ic->op == LEFT_OP || ic->op == RIGHT_OP ) &&
2525             getSize(operandType(op)) > 3) return NULL;
2526
2527         /* if this is a cast to a bigger type */
2528         if (ic->op==CAST) {
2529           if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) &&
2530               getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2531               getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2532             return 0;
2533           }
2534         }
2535
2536         /* general case */
2537         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2538             !isOperandEqual(IC_RESULT(ic),op) &&
2539             ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) &&
2540                 !isOperandInReg(IC_RESULT(ic))) ||
2541              OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2542
2543         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
2544             !isOperandEqual(IC_RIGHT(ic),op) &&
2545             (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq ||
2546              IS_TRUE_SYMOP(IC_RIGHT(ic))               ||
2547              OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2548             ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) &&
2549                 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2550
2551         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) &&
2552             !isOperandEqual(IC_LEFT(ic),op) &&
2553             (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq ||
2554              IS_TRUE_SYMOP(IC_LEFT(ic))               ||
2555              OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2556             ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) &&
2557                 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2558
2559         if (IC_LEFT(ic) && IC_RIGHT(ic) &&
2560             IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2561             (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) &&
2562             (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2563             return NULL;
2564     }
2565     OP_SYMBOL(op)->ruonly = 1;
2566     if (OP_SYMBOL(op)->usl.spillLoc) {
2567         if (OP_SYMBOL(op)->spillA)
2568             OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2569         OP_SYMBOL(op)->usl.spillLoc = NULL;
2570     }
2571     return dic;
2572 }
2573
2574 /*-----------------------------------------------------------------*/
2575 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2576 /*-----------------------------------------------------------------*/
2577 static bool
2578 isBitwiseOptimizable (iCode * ic)
2579 {
2580   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2581   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2582
2583   /* bitwise operations are considered optimizable
2584      under the following conditions (Jean-Louis VERN)
2585
2586      x & lit
2587      bit & bit
2588      bit & x
2589      bit ^ bit
2590      bit ^ x
2591      x   ^ lit
2592      x   | lit
2593      bit | bit
2594      bit | x
2595    */
2596   if (IS_LITERAL (rtype) ||
2597       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2598     return TRUE;
2599   else
2600     return FALSE;
2601 }
2602
2603 /*-----------------------------------------------------------------*/
2604 /* packRegsForAccUse - pack registers for acc use                  */
2605 /*-----------------------------------------------------------------*/
2606 static void
2607 packRegsForAccUse (iCode * ic)
2608 {
2609   iCode *uic;
2610
2611   /* if this is an aggregate, e.g. a one byte char array */
2612   if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2613     return;
2614   }
2615
2616   /* if we are calling a reentrant function that has stack parameters */
2617   if (ic->op == CALL &&
2618        IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2619        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2620       return;
2621
2622   if (ic->op == PCALL &&
2623        IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2624        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2625       return;
2626
2627   /* if + or - then it has to be one byte result */
2628   if ((ic->op == '+' || ic->op == '-')
2629       && getSize (operandType (IC_RESULT (ic))) > 1)
2630     return;
2631
2632   /* if shift operation make sure right side is not a literal */
2633   if (ic->op == RIGHT_OP &&
2634       (isOperandLiteral (IC_RIGHT (ic)) ||
2635        getSize (operandType (IC_RESULT (ic))) > 1))
2636     return;
2637
2638   if (ic->op == LEFT_OP &&
2639       (isOperandLiteral (IC_RIGHT (ic)) ||
2640        getSize (operandType (IC_RESULT (ic))) > 1))
2641     return;
2642
2643   if (IS_BITWISE_OP (ic) &&
2644       getSize (operandType (IC_RESULT (ic))) > 1)
2645     return;
2646
2647
2648   /* has only one definition */
2649   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2650     return;
2651
2652   /* has only one use */
2653   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2654     return;
2655
2656   /* and the usage immediately follows this iCode */
2657   if (!(uic = hTabItemWithKey (iCodehTab,
2658                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2659     return;
2660
2661   if (ic->next != uic)
2662     return;
2663
2664   /* if it is a conditional branch then we definitely can */
2665   if (uic->op == IFX)
2666     goto accuse;
2667
2668   if (uic->op == JUMPTABLE)
2669     return;
2670
2671   /* if the usage is not is an assignment
2672      or an arithmetic / bitwise / shift operation then not */
2673   if (POINTER_SET (uic) &&
2674       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2675     return;
2676
2677   if (uic->op != '=' &&
2678       !IS_ARITHMETIC_OP (uic) &&
2679       !IS_BITWISE_OP (uic) &&
2680       uic->op != LEFT_OP &&
2681       uic->op != RIGHT_OP)
2682     return;
2683
2684   /* if used in ^ operation then make sure right is not a
2685      literl */
2686   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2687     return;
2688
2689   /* if shift operation make sure right side is not a literal */
2690   if (uic->op == RIGHT_OP &&
2691       (isOperandLiteral (IC_RIGHT (uic)) ||
2692        getSize (operandType (IC_RESULT (uic))) > 1))
2693     return;
2694
2695   if (uic->op == LEFT_OP &&
2696       (isOperandLiteral (IC_RIGHT (uic)) ||
2697        getSize (operandType (IC_RESULT (uic))) > 1))
2698     return;
2699
2700   /* make sure that the result of this icode is not on the
2701      stack, since acc is used to compute stack offset */
2702   if (isOperandOnStack(IC_RESULT(uic)))
2703     return;
2704
2705   /* if either one of them in far space then we cannot */
2706   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2707        isOperandInFarSpace (IC_LEFT (uic))) ||
2708       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2709        isOperandInFarSpace (IC_RIGHT (uic))))
2710     return;
2711
2712   /* if the usage has only one operand then we can */
2713   if (IC_LEFT (uic) == NULL ||
2714       IC_RIGHT (uic) == NULL)
2715     goto accuse;
2716
2717   /* make sure this is on the left side if not
2718      a '+' since '+' is commutative */
2719   if (ic->op != '+' &&
2720       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2721     return;
2722
2723   /* if the other one is not on stack then we can */
2724   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2725       (IS_ITEMP (IC_RIGHT (uic)) ||
2726        (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2727         !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2728     goto accuse;
2729
2730   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2731       (IS_ITEMP (IC_LEFT (uic)) ||
2732        (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2733         !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2734     goto accuse;
2735
2736   return;
2737
2738 accuse:
2739   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2740
2741 }
2742
2743 /*-----------------------------------------------------------------*/
2744 /* packForPush - hueristics to reduce iCode for pushing            */
2745 /*-----------------------------------------------------------------*/
2746 static void
2747 packForPush (iCode * ic, eBBlock * ebp)
2748 {
2749   iCode *dic, *lic;
2750   bitVect *dbv;
2751
2752   if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2753     return;
2754
2755   /* must have only definition & one usage */
2756   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2757       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2758     return;
2759
2760   /* find the definition */
2761   if (!(dic = hTabItemWithKey (iCodehTab,
2762                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2763     return;
2764
2765   if (dic->op != '=' || POINTER_SET (dic))
2766     return;
2767
2768   if (dic->eBBlockNum != ic->eBBlockNum) return ;
2769
2770   /* make sure the right side does not have any definitions
2771      inbetween */
2772   dbv = OP_DEFS(IC_RIGHT(dic));
2773   for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2774           if (bitVectBitValue(dbv,lic->key)) return ;
2775   }
2776   /* make sure they have the same type */
2777   if (IS_SPEC(operandType(IC_LEFT(ic))))
2778   {
2779     sym_link *itype=operandType(IC_LEFT(ic));
2780     sym_link *ditype=operandType(IC_RIGHT(dic));
2781
2782     if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2783         SPEC_LONG(itype)!=SPEC_LONG(ditype))
2784       return;
2785   }
2786   /* extend the live range of replaced operand if needed */
2787   if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2788           OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2789           OP_SYMBOL(IC_RIGHT(dic))->clashes =
2790               bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2791                            OP_SYMBOL(IC_LEFT(ic))->clashes);
2792   }
2793   for (lic = ic; lic && lic != dic; lic = lic->prev)
2794     {
2795       bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2796       if (IS_ITEMP (IC_RIGHT (dic)))
2797         bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2798     }
2799   /* we now we know that it has one & only one def & use
2800      and the that the definition is an assignment */
2801   IC_LEFT (ic) = IC_RIGHT (dic);
2802
2803   remiCodeFromeBBlock (ebp, dic);
2804   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2805   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2806 }
2807
2808 /*-----------------------------------------------------------------*/
2809 /* packRegisters - does some transformations to reduce register    */
2810 /*                   pressure                                      */
2811 /*-----------------------------------------------------------------*/
2812 static void
2813 packRegisters (eBBlock * ebp)
2814 {
2815   iCode *ic;
2816   int change = 0;
2817
2818   while (1)
2819     {
2820
2821       change = 0;
2822
2823       /* look for assignments of the form */
2824       /* iTempNN = TRueSym (someoperation) SomeOperand */
2825       /*       ....                       */
2826       /* TrueSym := iTempNN:1             */
2827       for (ic = ebp->sch; ic; ic = ic->next)
2828         {
2829           /* find assignment of the form TrueSym := iTempNN:1 */
2830           if (ic->op == '=' && !POINTER_SET (ic))
2831             change += packRegsForAssign (ic, ebp);
2832         }
2833
2834       if (!change)
2835         break;
2836     }
2837
2838   for (ic = ebp->sch; ic; ic = ic->next)
2839     {
2840       /* if this is an itemp & result of an address of a true sym
2841          then mark this as rematerialisable   */
2842       if (ic->op == ADDRESS_OF &&
2843           IS_ITEMP (IC_RESULT (ic)) &&
2844           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2845           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2846           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2847         {
2848
2849           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2850           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2851           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2852
2853         }
2854
2855         /* if this is an itemp & used as a pointer
2856            & assigned to a literal then remat */
2857         if (IS_ASSIGN_ICODE(ic) &&
2858             IS_ITEMP(IC_RESULT(ic)) &&
2859             bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2860             isOperandLiteral(IC_RIGHT(ic)))
2861         {
2862           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2863           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2864           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2865         }
2866
2867       /* if straight assignment then carry remat flag if
2868          this is the only definition */
2869       if (ic->op == '=' &&
2870           !POINTER_SET (ic) &&
2871           IS_SYMOP (IC_RIGHT (ic)) &&
2872           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2873           !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2874           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2875         {
2876
2877           OP_SYMBOL (IC_RESULT (ic))->remat =
2878             OP_SYMBOL (IC_RIGHT (ic))->remat;
2879           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2880             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2881         }
2882
2883       /* if cast to a generic pointer & the pointer being
2884          cast is remat, then we can remat this cast as well */
2885       if (ic->op == CAST &&
2886           IS_SYMOP(IC_RIGHT(ic)) &&
2887           !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2888           OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2889               sym_link *to_type = operandType(IC_LEFT(ic));
2890               sym_link *from_type = operandType(IC_RIGHT(ic));
2891               if (IS_GENPTR(to_type) && IS_PTR(from_type)) {
2892                       OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2893                       OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2894                       OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2895               }
2896       }
2897
2898       /* if this is a +/- operation with a rematerizable
2899          then mark this as rematerializable as well */
2900       if ((ic->op == '+' || ic->op == '-') &&
2901           (IS_SYMOP (IC_LEFT (ic)) &&
2902            IS_ITEMP (IC_RESULT (ic)) &&
2903            OP_SYMBOL (IC_LEFT (ic))->remat &&
2904            (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2905            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2906            IS_OP_LITERAL (IC_RIGHT (ic))))
2907         {
2908
2909           //int i = operandLitValue(IC_RIGHT(ic));
2910           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2911           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2912           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2913         }
2914
2915       /* mark the pointer usages */
2916       if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic)))
2917         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2918
2919       if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic)))
2920         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2921
2922       if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2923           OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2924
2925       if (ic->op == RECEIVE && ic->argreg == 1 &&
2926           IS_SYMOP (IC_RESULT (ic)) &&
2927           getSize (operandType(IC_RESULT(ic))) <= 3)
2928           OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2929
2930       if (ic->op == SEND && ic->argreg == 1 &&
2931           IS_SYMOP(IC_LEFT(ic)) &&
2932           getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2933           OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2934
2935       if (!SKIP_IC2 (ic))
2936         {
2937           /* if we are using a symbol on the stack
2938              then we should say ds390_ptrRegReq */
2939           if (options.useXstack && ic->parmPush
2940               && (ic->op == IPUSH || ic->op == IPOP))
2941             ds390_ptrRegReq++;
2942           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2943                   ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2944                                       OP_SYMBOL (IC_COND (ic))->iaccess +
2945                                       (SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata));
2946           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2947                   ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2948                                       OP_SYMBOL (IC_JTCOND (ic))->iaccess +
2949                                       (SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata));
2950           else
2951             {
2952               if (IS_SYMOP (IC_LEFT (ic)))
2953                       ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2954                                           OP_SYMBOL (IC_LEFT (ic))->iaccess +
2955                                           (SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata));
2956               if (IS_SYMOP (IC_RIGHT (ic)))
2957                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2958                                           OP_SYMBOL (IC_RIGHT (ic))->iaccess +
2959                                           (SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata));
2960               if (IS_SYMOP (IC_RESULT (ic)))
2961                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2962                                           OP_SYMBOL (IC_RESULT (ic))->iaccess +
2963                                           (SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata));
2964             }
2965         }
2966
2967       /* if the condition of an if instruction
2968          is defined in the previous instruction and
2969          this is the only usage then
2970          mark the itemp as a conditional */
2971       if ((IS_CONDITIONAL (ic) ||
2972            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2973           ic->next && ic->next->op == IFX &&
2974           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2975           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2976           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2977         {
2978           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2979           continue;
2980         }
2981 #if 1
2982       /* reduce for support function calls */
2983       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2984         packRegsForSupport (ic, ebp);
2985 #endif
2986       /* some cases the redundant moves can
2987          can be eliminated for return statements . Can be elminated for the first SEND */
2988       if ((ic->op == RETURN ||
2989            ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&
2990           !isOperandInFarSpace (IC_LEFT (ic)) &&
2991           !options.model) {
2992
2993           packRegsDPTRuse (IC_LEFT (ic));
2994       }
2995
2996       if (ic->op == CALL) {
2997           sym_link *ftype = operandType(IC_LEFT(ic));
2998           if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2999               !IFFUNC_ISBUILTIN(ftype)) {
3000               packRegsDPTRuse (IC_RESULT (ic));
3001           }
3002       }
3003
3004       /* if pointer set & left has a size more than
3005          one and right is not in far space */
3006       if (POINTER_SET (ic) &&
3007           !isOperandInFarSpace (IC_RIGHT (ic)) &&
3008           IS_SYMOP (IC_RESULT (ic)) &&
3009           !OP_SYMBOL (IC_RESULT (ic))->remat &&
3010           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3011           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
3012
3013           packRegsDPTRuse (IC_RESULT (ic));
3014       }
3015
3016       /* if pointer get */
3017       if (POINTER_GET (ic) &&
3018           !isOperandInFarSpace (IC_RESULT (ic)) &&
3019           IS_SYMOP (IC_LEFT (ic)) &&
3020           !OP_SYMBOL (IC_LEFT (ic))->remat &&
3021           !IS_OP_RUONLY (IC_RESULT (ic)) &&
3022           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
3023
3024           packRegsDPTRuse (IC_LEFT (ic));
3025       }
3026
3027       /* if this is cast for intergral promotion then
3028          check if only use of  the definition of the
3029          operand being casted/ if yes then replace
3030          the result of that arithmetic operation with
3031          this result and get rid of the cast */
3032       if (ic->op == CAST)
3033         {
3034           sym_link *fromType = operandType (IC_RIGHT (ic));
3035           sym_link *toType = operandType (IC_LEFT (ic));
3036
3037           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3038               getSize (fromType) != getSize (toType) &&
3039               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3040             {
3041
3042               iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3043               if (dic)
3044                 {
3045                   if (IS_ARITHMETIC_OP (dic))
3046                     {
3047                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3048                       IC_RESULT (dic) = IC_RESULT (ic);
3049                       remiCodeFromeBBlock (ebp, ic);
3050                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3051                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3052                       OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3053                       ic = ic->prev;
3054                     }
3055                   else
3056                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3057                 }
3058             }
3059           else
3060             {
3061
3062               /* if the type from and type to are the same
3063                  then if this is the only use then packit */
3064               if (compareType (operandType (IC_RIGHT (ic)),
3065                              operandType (IC_LEFT (ic))) == 1)
3066                 {
3067                   iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
3068                   if (dic)
3069                     {
3070                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3071                       IC_RESULT (dic) = IC_RESULT (ic);
3072                       remiCodeFromeBBlock (ebp, ic);
3073                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3074                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3075                       OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3076                       ic = ic->prev;
3077                     }
3078                 }
3079             }
3080         }
3081
3082       /* pack for PUSH
3083          iTempNN := (some variable in farspace) V1
3084          push iTempNN ;
3085          -------------
3086          push V1
3087        */
3088       if (ic->op == IPUSH || ic->op == SEND)
3089         {
3090           packForPush (ic, ebp);
3091         }
3092
3093
3094       /* pack registers for accumulator use, when the
3095          result of an arithmetic or bit wise operation
3096          has only one use, that use is immediately following
3097          the defintion and the using iCode has only one
3098          operand or has two operands but one is literal &
3099          the result of that operation is not on stack then
3100          we can leave the result of this operation in acc:b
3101          combination */
3102       if ((IS_ARITHMETIC_OP (ic)
3103            || IS_CONDITIONAL(ic)
3104            || IS_BITWISE_OP (ic)
3105            || ic->op == LEFT_OP || ic->op == RIGHT_OP
3106            || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3107           ) &&
3108           IS_ITEMP (IC_RESULT (ic)) &&
3109           getSize (operandType (IC_RESULT (ic))) <= 2)
3110
3111         packRegsForAccUse (ic);
3112
3113     }
3114 }
3115
3116 /*-----------------------------------------------------------------*/
3117 /* assignRegisters - assigns registers to each live range as need  */
3118 /*-----------------------------------------------------------------*/
3119 void
3120 ds390_assignRegisters (eBBlock ** ebbs, int count)
3121 {
3122   iCode *ic;
3123   int i;
3124
3125   setToNull ((void *) &_G.funcrUsed);
3126   setToNull ((void *) &_G.regAssigned);
3127   setToNull ((void *) &_G.totRegAssigned);
3128   setToNull ((void *) &_G.funcrUsed);
3129   ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3130   ds390_nRegs = 12;
3131   if (options.model != MODEL_FLAT24) options.stack10bit = 0;
3132   /* change assignments this will remove some
3133      live ranges reducing some register pressure */
3134   for (i = 0; i < count; i++)
3135     packRegisters (ebbs[i]);
3136
3137   /* liveranges probably changed by register packing
3138      so we compute them again */
3139   recomputeLiveRanges (ebbs, count);
3140
3141   if (options.dump_pack)
3142     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3143
3144   /* first determine for each live range the number of
3145      registers & the type of registers required for each */
3146   regTypeNum ();
3147
3148   /* and serially allocate registers */
3149   serialRegAssign (ebbs, count);
3150
3151   ds390_nRegs = 8;
3152   freeAllRegs ();
3153   fillGaps();
3154   ds390_nRegs = 12;
3155
3156   /* if stack was extended then tell the user */
3157   if (_G.stackExtend)
3158     {
3159 /*      werror(W_TOOMANY_SPILS,"stack", */
3160 /*             _G.stackExtend,currFunc->name,""); */
3161       _G.stackExtend = 0;
3162     }
3163
3164   if (_G.dataExtend)
3165     {
3166 /*      werror(W_TOOMANY_SPILS,"data space", */
3167 /*             _G.dataExtend,currFunc->name,""); */
3168       _G.dataExtend = 0;
3169     }
3170
3171   /* after that create the register mask
3172      for each of the instruction */
3173   createRegMask (ebbs, count);
3174
3175   /* redo that offsets for stacked automatic variables */
3176   if (currFunc)
3177     redoStackOffsets ();
3178
3179   /* make sure r0 & r1 are flagged as used if they might be used */
3180   /* as pointers */
3181   if (currFunc && ds390_ptrRegReq)
3182     {
3183       currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3184       currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3185     }
3186
3187   if (options.dump_rassgn) {
3188     dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3189     dumpLiveRanges (DUMP_LRANGE, liveRanges);
3190   }
3191
3192   /* do the overlaysegment stuff SDCCmem.c */
3193   doOverlays (ebbs, count);
3194
3195   /* now get back the chain */
3196   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3197
3198   gen390Code (ic);
3199
3200   /* free up any _G.stackSpil locations allocated */
3201   applyToSet (_G.stackSpil, deallocStackSpil);
3202   _G.slocNum = 0;
3203   setToNull ((void *) &_G.stackSpil);
3204   setToNull ((void *) &_G.spiltSet);
3205   /* mark all registers as free */
3206   ds390_nRegs = 8;
3207   freeAllRegs ();
3208
3209   return;
3210 }