fixed pointer post increment problem
[fw/sdcc] / src / ds390 / ralloc.c
1 /*------------------------------------------------------------------------
2
3   SDCCralloc.c - source file for register allocation. (8051) 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 /* Global data */
40 static struct
41   {
42     bitVect *spiltSet;
43     set *stackSpil;
44     bitVect *regAssigned;
45     short blockSpil;
46     int slocNum;
47     bitVect *funcrUsed;         /* registers used in a function */
48     int stackExtend;
49     int dataExtend;
50   }
51 _G;
52
53 /* Shared with gen.c */
54 int ds390_ptrRegReq;            /* one byte pointer register required */
55
56 /* 8051 registers */
57 regs regs390[] =
58 {
59
60   {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
61   {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
62   {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
63   {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
64   {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
65   {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
66   {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
67   {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
68   {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
69   {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
70   {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
71   {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
72   {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
73   {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, 1},
74 };
75 int ds390_nRegs = 13;
76 static void spillThis (symbol *);
77
78 /*-----------------------------------------------------------------*/
79 /* allocReg - allocates register of given type                     */
80 /*-----------------------------------------------------------------*/
81 static regs *
82 allocReg (short type)
83 {
84   int i;
85
86   for (i = 0; i < ds390_nRegs; i++)
87     {
88
89       /* if type is given as 0 then any
90          free register will do */
91       if (!type &&
92           regs390[i].isFree)
93         {
94           regs390[i].isFree = 0;
95           if (currFunc)
96             currFunc->regsUsed =
97               bitVectSetBit (currFunc->regsUsed, i);
98           return &regs390[i];
99         }
100       /* other wise look for specific type
101          of register */
102       if (regs390[i].isFree &&
103           regs390[i].type == type)
104         {
105           regs390[i].isFree = 0;
106           if (currFunc)
107             currFunc->regsUsed =
108               bitVectSetBit (currFunc->regsUsed, i);
109           return &regs390[i];
110         }
111     }
112   return NULL;
113 }
114
115 /*-----------------------------------------------------------------*/
116 /* ds390_regWithIdx - returns pointer to register wit index number       */
117 /*-----------------------------------------------------------------*/
118 regs *
119 ds390_regWithIdx (int idx)
120 {
121   int i;
122
123   for (i = 0; i < ds390_nRegs; i++)
124     if (regs390[i].rIdx == idx)
125       return &regs390[i];
126
127   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
128           "regWithIdx not found");
129   exit (1);
130 }
131
132 /*-----------------------------------------------------------------*/
133 /* freeReg - frees a register                                      */
134 /*-----------------------------------------------------------------*/
135 static void
136 freeReg (regs * reg)
137 {
138   reg->isFree = 1;
139 }
140
141
142 /*-----------------------------------------------------------------*/
143 /* nFreeRegs - returns number of free registers                    */
144 /*-----------------------------------------------------------------*/
145 static int
146 nFreeRegs (int type)
147 {
148   int i;
149   int nfr = 0;
150
151   for (i = 0; i < ds390_nRegs; i++)
152     if (regs390[i].isFree && regs390[i].type == type)
153       nfr++;
154   return nfr;
155 }
156
157 /*-----------------------------------------------------------------*/
158 /* nfreeRegsType - free registers with type                         */
159 /*-----------------------------------------------------------------*/
160 static int
161 nfreeRegsType (int type)
162 {
163   int nfr;
164   if (type == REG_PTR)
165     {
166       if ((nfr = nFreeRegs (type)) == 0)
167         return nFreeRegs (REG_GPR);
168     }
169
170   return nFreeRegs (type);
171 }
172
173
174 /*-----------------------------------------------------------------*/
175 /* allDefsOutOfRange - all definitions are out of a range          */
176 /*-----------------------------------------------------------------*/
177 static bool
178 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
179 {
180   int i;
181
182   if (!defs)
183     return TRUE;
184
185   for (i = 0; i < defs->size; i++)
186     {
187       iCode *ic;
188
189       if (bitVectBitValue (defs, i) &&
190           (ic = hTabItemWithKey (iCodehTab, i)) &&
191           (ic->seq >= fseq && ic->seq <= toseq))
192
193         return FALSE;
194
195     }
196
197   return TRUE;
198 }
199
200 /*-----------------------------------------------------------------*/
201 /* computeSpillable - given a point find the spillable live ranges */
202 /*-----------------------------------------------------------------*/
203 static bitVect *
204 computeSpillable (iCode * ic)
205 {
206   bitVect *spillable;
207
208   /* spillable live ranges are those that are live at this 
209      point . the following categories need to be subtracted
210      from this set. 
211      a) - those that are already spilt
212      b) - if being used by this one
213      c) - defined by this one */
214
215   spillable = bitVectCopy (ic->rlive);
216   spillable =
217     bitVectCplAnd (spillable, _G.spiltSet);     /* those already spilt */
218   spillable =
219     bitVectCplAnd (spillable, ic->uses);        /* used in this one */
220   bitVectUnSetBit (spillable, ic->defKey);
221   spillable = bitVectIntersect (spillable, _G.regAssigned);
222   return spillable;
223
224 }
225
226 /*-----------------------------------------------------------------*/
227 /* noSpilLoc - return true if a variable has no spil location      */
228 /*-----------------------------------------------------------------*/
229 static int
230 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
231 {
232   return (sym->usl.spillLoc ? 0 : 1);
233 }
234
235 /*-----------------------------------------------------------------*/
236 /* hasSpilLoc - will return 1 if the symbol has spil location      */
237 /*-----------------------------------------------------------------*/
238 static int
239 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
240 {
241   return (sym->usl.spillLoc ? 1 : 0);
242 }
243
244 /*-----------------------------------------------------------------*/
245 /* directSpilLoc - will return 1 if the splilocation is in direct  */
246 /*-----------------------------------------------------------------*/
247 static int
248 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
249 {
250   if (sym->usl.spillLoc &&
251       (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
252     return 1;
253   else
254     return 0;
255 }
256
257 /*-----------------------------------------------------------------*/
258 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
259 /*                    but is not used as a pointer                 */
260 /*-----------------------------------------------------------------*/
261 static int
262 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
263 {
264   return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
265 }
266
267 /*-----------------------------------------------------------------*/
268 /* rematable - will return 1 if the remat flag is set              */
269 /*-----------------------------------------------------------------*/
270 static int
271 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
272 {
273   return sym->remat;
274 }
275
276 /*-----------------------------------------------------------------*/
277 /* notUsedInBlock - not used in this block                         */
278 /*-----------------------------------------------------------------*/
279 static int
280 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
281 {
282   return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
283           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
284 /*     return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
285 }
286
287 /*-----------------------------------------------------------------*/
288 /* notUsedInRemaining - not used or defined in remain of the block */
289 /*-----------------------------------------------------------------*/
290 static int
291 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
292 {
293   return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
294           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
295 }
296
297 /*-----------------------------------------------------------------*/
298 /* allLRs - return true for all                                    */
299 /*-----------------------------------------------------------------*/
300 static int
301 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
302 {
303   return 1;
304 }
305
306 /*-----------------------------------------------------------------*/
307 /* liveRangesWith - applies function to a given set of live range  */
308 /*-----------------------------------------------------------------*/
309 static set *
310 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
311                 eBBlock * ebp, iCode * ic)
312 {
313   set *rset = NULL;
314   int i;
315
316   if (!lrs || !lrs->size)
317     return NULL;
318
319   for (i = 1; i < lrs->size; i++)
320     {
321       symbol *sym;
322       if (!bitVectBitValue (lrs, i))
323         continue;
324
325       /* if we don't find it in the live range 
326          hash table we are in serious trouble */
327       if (!(sym = hTabItemWithKey (liveRanges, i)))
328         {
329           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
330                   "liveRangesWith could not find liveRange");
331           exit (1);
332         }
333
334       if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
335         addSetHead (&rset, sym);
336     }
337
338   return rset;
339 }
340
341
342 /*-----------------------------------------------------------------*/
343 /* leastUsedLR - given a set determines which is the least used    */
344 /*-----------------------------------------------------------------*/
345 static symbol *
346 leastUsedLR (set * sset)
347 {
348   symbol *sym = NULL, *lsym = NULL;
349
350   sym = lsym = setFirstItem (sset);
351
352   if (!lsym)
353     return NULL;
354
355   for (; lsym; lsym = setNextItem (sset))
356     {
357
358       /* if usage is the same then prefer
359          the spill the smaller of the two */
360       if (lsym->used == sym->used)
361         if (getSize (lsym->type) < getSize (sym->type))
362           sym = lsym;
363
364       /* if less usage */
365       if (lsym->used < sym->used)
366         sym = lsym;
367
368     }
369
370   setToNull ((void **) &sset);
371   sym->blockSpil = 0;
372   return sym;
373 }
374
375 /*-----------------------------------------------------------------*/
376 /* noOverLap - will iterate through the list looking for over lap  */
377 /*-----------------------------------------------------------------*/
378 static int
379 noOverLap (set * itmpStack, symbol * fsym)
380 {
381   symbol *sym;
382
383   for (sym = setFirstItem (itmpStack); sym;
384        sym = setNextItem (itmpStack))
385     {
386             // if sym starts before (or on) our end point
387             // and ends after (or on) our start point, 
388             // it is an overlap.
389             if (sym->liveFrom <= fsym->liveTo &&
390                 sym->liveTo   >= fsym->liveFrom)
391             {
392                 return 0;
393             }
394     }
395   return 1;
396 }
397
398 /*-----------------------------------------------------------------*/
399 /* isFree - will return 1 if the a free spil location is found     */
400 /*-----------------------------------------------------------------*/
401 static
402 DEFSETFUNC (isFree)
403 {
404   symbol *sym = item;
405   V_ARG (symbol **, sloc);
406   V_ARG (symbol *, fsym);
407
408   /* if already found */
409   if (*sloc)
410     return 0;
411
412   /* if it is free && and the itmp assigned to
413      this does not have any overlapping live ranges
414      with the one currently being assigned and
415      the size can be accomodated  */
416   if (sym->isFree &&
417       noOverLap (sym->usl.itmpStack, fsym) &&
418       getSize (sym->type) >= getSize (fsym->type))
419     {
420       *sloc = sym;
421       return 1;
422     }
423
424   return 0;
425 }
426
427 /*-----------------------------------------------------------------*/
428 /* spillLRWithPtrReg :- will spil those live ranges which use PTR  */
429 /*-----------------------------------------------------------------*/
430 static void
431 spillLRWithPtrReg (symbol * forSym)
432 {
433   symbol *lrsym;
434   regs *r0, *r1;
435   int k;
436
437   if (!_G.regAssigned ||
438       bitVectIsZero (_G.regAssigned))
439     return;
440
441   r0 = ds390_regWithIdx (R0_IDX);
442   r1 = ds390_regWithIdx (R1_IDX);
443
444   /* for all live ranges */
445   for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
446        lrsym = hTabNextItem (liveRanges, &k))
447     {
448       int j;
449
450       /* if no registers assigned to it or
451          spilt */
452       /* if it does not overlap with this then 
453          not 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 spil 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 (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
500     {
501       fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
502                __FILE__, __LINE__);
503       exit (1);
504     }
505
506   sloc = newiTemp (slocBuffer);
507
508   /* set the type to the spilling symbol */
509   sloc->type = copyLinkChain (sym->type);
510   sloc->etype = getSpec (sloc->type);
511   SPEC_SCLS (sloc->etype) = options.model ? S_XDATA : S_DATA;
512   SPEC_EXTR (sloc->etype) = 0;
513
514   /* we don't allow it to be allocated`
515      onto the external stack since : so we
516      temporarily turn it off ; we also
517      turn off memory model to prevent
518      the spil from going to the external storage
519      and turn off overlaying 
520    */
521
522   useXstack = options.useXstack;
523   model = options.model;
524   noOverlay = options.noOverlay;
525   options.noOverlay = 1;
526
527   /* options.model = options.useXstack = 0; */
528
529   allocLocal (sloc);
530
531   options.useXstack = useXstack;
532   options.model = model;
533   options.noOverlay = noOverlay;
534   sloc->isref = 1;              /* to prevent compiler warning */
535
536   /* if it is on the stack then update the stack */
537   if (IN_STACK (sloc->etype))
538     {
539       currFunc->stack += getSize (sloc->type);
540       _G.stackExtend += getSize (sloc->type);
541     }
542   else
543     _G.dataExtend += getSize (sloc->type);
544
545   /* add it to the _G.stackSpil set */
546   addSetHead (&_G.stackSpil, sloc);
547   sym->usl.spillLoc = sloc;
548   sym->stackSpil = 1;
549
550   /* add it to the set of itempStack set 
551      of the spill location */
552   addSetHead (&sloc->usl.itmpStack, sym);
553   return sym;
554 }
555
556 /*-----------------------------------------------------------------*/
557 /* isSpiltOnStack - returns true if the spil location is on stack  */
558 /*-----------------------------------------------------------------*/
559 static bool
560 isSpiltOnStack (symbol * sym)
561 {
562   sym_link *etype;
563
564   if (!sym)
565     return FALSE;
566
567   if (!sym->isspilt)
568     return FALSE;
569
570 /*     if (sym->_G.stackSpil) */
571 /*      return TRUE; */
572
573   if (!sym->usl.spillLoc)
574     return FALSE;
575
576   etype = getSpec (sym->usl.spillLoc->type);
577   if (IN_STACK (etype))
578     return TRUE;
579
580   return FALSE;
581 }
582
583 /*-----------------------------------------------------------------*/
584 /* spillThis - spils a specific operand                            */
585 /*-----------------------------------------------------------------*/
586 static void
587 spillThis (symbol * sym)
588 {
589   int i;
590   /* if this is rematerializable or has a spillLocation
591      we are okay, else we need to create a spillLocation
592      for it */
593   if (!(sym->remat || sym->usl.spillLoc))
594     createStackSpil (sym);
595
596
597   /* mark it has spilt & put it in the spilt set */
598   sym->isspilt = 1;
599   _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
600
601   bitVectUnSetBit (_G.regAssigned, sym->key);
602
603   for (i = 0; i < sym->nRegs; i++)
604
605     if (sym->regs[i])
606       {
607         freeReg (sym->regs[i]);
608         sym->regs[i] = NULL;
609       }
610
611   /* if spilt on stack then free up r0 & r1 
612      if they could have been assigned to some
613      LIVE ranges */
614   if (!ds390_ptrRegReq && isSpiltOnStack (sym))
615     {
616       ds390_ptrRegReq++;
617       spillLRWithPtrReg (sym);
618     }
619
620   if (sym->usl.spillLoc && !sym->remat)
621     sym->usl.spillLoc->allocreq = 1;
622   return;
623 }
624
625 /*-----------------------------------------------------------------*/
626 /* selectSpil - select a iTemp to spil : rather a simple procedure */
627 /*-----------------------------------------------------------------*/
628 static symbol *
629 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
630 {
631   bitVect *lrcs = NULL;
632   set *selectS;
633   symbol *sym;
634
635   /* get the spillable live ranges */
636   lrcs = computeSpillable (ic);
637
638   /* get all live ranges that are rematerizable */
639   if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
640     {
641
642       /* return the least used of these */
643       return leastUsedLR (selectS);
644     }
645
646   /* get live ranges with spillLocations in direct space */
647   if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
648     {
649       sym = leastUsedLR (selectS);
650       strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
651                            sym->usl.spillLoc->rname :
652                            sym->usl.spillLoc->name));
653       sym->spildir = 1;
654       /* mark it as allocation required */
655       sym->usl.spillLoc->allocreq = 1;
656       return sym;
657     }
658
659   /* if the symbol is local to the block then */
660   if (forSym->liveTo < ebp->lSeq)
661     {
662
663       /* check if there are any live ranges allocated
664          to registers that are not used in this block */
665       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
666         {
667           sym = leastUsedLR (selectS);
668           /* if this is not rematerializable */
669           if (!sym->remat)
670             {
671               _G.blockSpil++;
672               sym->blockSpil = 1;
673             }
674           return sym;
675         }
676
677       /* check if there are any live ranges that not
678          used in the remainder of the block */
679       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
680         {
681           sym = leastUsedLR (selectS);
682           if (sym != forSym)
683             {
684               if (!sym->remat)
685                 {
686                   sym->remainSpil = 1;
687                   _G.blockSpil++;
688                 }
689               return sym;
690             }
691         }
692     }
693
694   /* find live ranges with spillocation && not used as pointers */
695   if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
696     {
697
698       sym = leastUsedLR (selectS);
699       /* mark this as allocation required */
700       sym->usl.spillLoc->allocreq = 1;
701       return sym;
702     }
703
704   /* find live ranges with spillocation */
705   if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
706     {
707
708       sym = leastUsedLR (selectS);
709       sym->usl.spillLoc->allocreq = 1;
710       return sym;
711     }
712
713   /* couldn't find then we need to create a spil
714      location on the stack , for which one? the least
715      used ofcourse */
716   if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
717     {
718
719       /* return a created spil location */
720       sym = createStackSpil (leastUsedLR (selectS));
721       sym->usl.spillLoc->allocreq = 1;
722       return sym;
723     }
724
725   /* this is an extreme situation we will spill
726      this one : happens very rarely but it does happen */
727   spillThis (forSym);
728   return forSym;
729
730 }
731
732 /*-----------------------------------------------------------------*/
733 /* spilSomething - spil some variable & mark registers as free     */
734 /*-----------------------------------------------------------------*/
735 static bool
736 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
737 {
738   symbol *ssym;
739   int i;
740
741   /* get something we can spil */
742   ssym = selectSpil (ic, ebp, forSym);
743
744   /* mark it as spilt */
745   ssym->isspilt = 1;
746   _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
747
748   /* mark it as not register assigned &
749      take it away from the set */
750   bitVectUnSetBit (_G.regAssigned, ssym->key);
751
752   /* mark the registers as free */
753   for (i = 0; i < ssym->nRegs; i++)
754     if (ssym->regs[i])
755       freeReg (ssym->regs[i]);
756
757   /* if spilt on stack then free up r0 & r1 
758      if they could have been assigned to as gprs */
759   if (!ds390_ptrRegReq && isSpiltOnStack (ssym))
760     {
761       ds390_ptrRegReq++;
762       spillLRWithPtrReg (ssym);
763     }
764
765   /* if this was a block level spil then insert push & pop 
766      at the start & end of block respectively */
767   if (ssym->blockSpil)
768     {
769       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
770       /* add push to the start of the block */
771       addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
772                                     ebp->sch->next : ebp->sch));
773       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
774       /* add pop to the end of the block */
775       addiCodeToeBBlock (ebp, nic, NULL);
776     }
777
778   /* if spilt because not used in the remainder of the
779      block then add a push before this instruction and
780      a pop at the end of the block */
781   if (ssym->remainSpil)
782     {
783
784       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
785       /* add push just before this instruction */
786       addiCodeToeBBlock (ebp, nic, ic);
787
788       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
789       /* add pop to the end of the block */
790       addiCodeToeBBlock (ebp, nic, NULL);
791     }
792
793   if (ssym == forSym)
794     return FALSE;
795   else
796     return TRUE;
797 }
798
799 /*-----------------------------------------------------------------*/
800 /* getRegPtr - will try for PTR if not a GPR type if not spil      */
801 /*-----------------------------------------------------------------*/
802 static regs *
803 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
804 {
805   regs *reg;
806
807 tryAgain:
808   /* try for a ptr type */
809   if ((reg = allocReg (REG_PTR)))
810     return reg;
811
812   /* try for gpr type */
813   if ((reg = allocReg (REG_GPR)))
814     return reg;
815
816   /* we have to spil */
817   if (!spilSomething (ic, ebp, sym))
818     return NULL;
819
820   /* this looks like an infinite loop but 
821      in really selectSpil will abort  */
822   goto tryAgain;
823 }
824
825 /*-----------------------------------------------------------------*/
826 /* getRegGpr - will try for GPR if not spil                        */
827 /*-----------------------------------------------------------------*/
828 static regs *
829 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
830 {
831   regs *reg;
832
833 tryAgain:
834   /* try for gpr type */
835   if ((reg = allocReg (REG_GPR)))
836     return reg;
837
838   if (!ds390_ptrRegReq)
839     if ((reg = allocReg (REG_PTR)))
840       return reg;
841
842   /* we have to spil */
843   if (!spilSomething (ic, ebp, sym))
844     return NULL;
845
846   /* this looks like an infinite loop but 
847      in really selectSpil will abort  */
848   goto tryAgain;
849 }
850
851 /*-----------------------------------------------------------------*/
852 /* symHasReg - symbol has a given register                         */
853 /*-----------------------------------------------------------------*/
854 static bool
855 symHasReg (symbol * sym, regs * reg)
856 {
857   int i;
858
859   for (i = 0; i < sym->nRegs; i++)
860     if (sym->regs[i] == reg)
861       return TRUE;
862
863   return FALSE;
864 }
865
866 /*-----------------------------------------------------------------*/
867 /* deassignLRs - check the live to and if they have registers & are */
868 /*               not spilt then free up the registers              */
869 /*-----------------------------------------------------------------*/
870 static void
871 deassignLRs (iCode * ic, eBBlock * ebp)
872 {
873   symbol *sym;
874   int k;
875   symbol *result;
876
877   for (sym = hTabFirstItem (liveRanges, &k); sym;
878        sym = hTabNextItem (liveRanges, &k))
879     {
880
881       symbol *psym = NULL;
882       /* if it does not end here */
883       if (sym->liveTo > ic->seq)
884         continue;
885
886       /* if it was spilt on stack then we can 
887          mark the stack spil location as free */
888       if (sym->isspilt)
889         {
890           if (sym->stackSpil)
891             {
892               sym->usl.spillLoc->isFree = 1;
893               sym->stackSpil = 0;
894             }
895           continue;
896         }
897
898       if (!bitVectBitValue (_G.regAssigned, sym->key))
899         continue;
900
901       /* special case check if this is an IFX &
902          the privious one was a pop and the 
903          previous one was not spilt then keep track
904          of the symbol */
905       if (ic->op == IFX && ic->prev &&
906           ic->prev->op == IPOP &&
907           !ic->prev->parmPush &&
908           !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
909         psym = OP_SYMBOL (IC_LEFT (ic->prev));
910
911       if (sym->nRegs)
912         {
913           int i = 0;
914
915           bitVectUnSetBit (_G.regAssigned, sym->key);
916
917           /* if the result of this one needs registers
918              and does not have it then assign it right
919              away */
920           if (IC_RESULT (ic) &&
921               !(SKIP_IC2 (ic) ||        /* not a special icode */
922                 ic->op == JUMPTABLE ||
923                 ic->op == IFX ||
924                 ic->op == IPUSH ||
925                 ic->op == IPOP ||
926                 ic->op == RETURN ||
927                 POINTER_SET (ic)) &&
928               (result = OP_SYMBOL (IC_RESULT (ic))) &&  /* has a result */
929               result->liveTo > ic->seq &&       /* and will live beyond this */
930               result->liveTo <= ebp->lSeq &&    /* does not go beyond this block */
931               result->regType == sym->regType &&        /* same register types */
932               result->nRegs &&  /* which needs registers */
933               !result->isspilt &&       /* and does not already have them */
934               !result->remat &&
935               !bitVectBitValue (_G.regAssigned, result->key) &&
936           /* the number of free regs + number of regs in this LR
937              can accomodate the what result Needs */
938               ((nfreeRegsType (result->regType) +
939                 sym->nRegs) >= result->nRegs)
940             )
941             {
942
943               for (i = 0; i < result->nRegs; i++)
944                 if (i < sym->nRegs)
945                   result->regs[i] = sym->regs[i];
946                 else
947                   result->regs[i] = getRegGpr (ic, ebp, result);
948
949               _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
950
951             }
952
953           /* free the remaining */
954           for (; i < sym->nRegs; i++)
955             {
956               if (psym)
957                 {
958                   if (!symHasReg (psym, sym->regs[i]))
959                     freeReg (sym->regs[i]);
960                 }
961               else
962                 freeReg (sym->regs[i]);
963             }
964         }
965     }
966 }
967
968
969 /*-----------------------------------------------------------------*/
970 /* reassignLR - reassign this to registers                         */
971 /*-----------------------------------------------------------------*/
972 static void
973 reassignLR (operand * op)
974 {
975   symbol *sym = OP_SYMBOL (op);
976   int i;
977
978   /* not spilt any more */
979   sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
980   bitVectUnSetBit (_G.spiltSet, sym->key);
981
982   _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
983
984   _G.blockSpil--;
985
986   for (i = 0; i < sym->nRegs; i++)
987     sym->regs[i]->isFree = 0;
988 }
989
990 /*-----------------------------------------------------------------*/
991 /* willCauseSpill - determines if allocating will cause a spill    */
992 /*-----------------------------------------------------------------*/
993 static int
994 willCauseSpill (int nr, int rt)
995 {
996   /* first check if there are any avlb registers
997      of te type required */
998   if (rt == REG_PTR)
999     {
1000       /* special case for pointer type 
1001          if pointer type not avlb then 
1002          check for type gpr */
1003       if (nFreeRegs (rt) >= nr)
1004         return 0;
1005       if (nFreeRegs (REG_GPR) >= nr)
1006         return 0;
1007     }
1008   else
1009     {
1010       if (ds390_ptrRegReq)
1011         {
1012           if (nFreeRegs (rt) >= nr)
1013             return 0;
1014         }
1015       else
1016         {
1017           if (nFreeRegs (REG_PTR) +
1018               nFreeRegs (REG_GPR) >= nr)
1019             return 0;
1020         }
1021     }
1022
1023   /* it will cause a spil */
1024   return 1;
1025 }
1026
1027 /*-----------------------------------------------------------------*/
1028 /* positionRegs - the allocator can allocate same registers to res- */
1029 /* ult and operand, if this happens make sure they are in the same */
1030 /* position as the operand otherwise chaos results                 */
1031 /*-----------------------------------------------------------------*/
1032 static void
1033 positionRegs (symbol * result, symbol * opsym, int lineno)
1034 {
1035   int count = min (result->nRegs, opsym->nRegs);
1036   int i, j = 0, shared = 0;
1037
1038   /* if the result has been spilt then cannot share */
1039   if (opsym->isspilt)
1040     return;
1041 again:
1042   shared = 0;
1043   /* first make sure that they actually share */
1044   for (i = 0; i < count; i++)
1045     {
1046       for (j = 0; j < count; j++)
1047         {
1048           if (result->regs[i] == opsym->regs[j] && i != j)
1049             {
1050               shared = 1;
1051               goto xchgPositions;
1052             }
1053         }
1054     }
1055 xchgPositions:
1056   if (shared)
1057     {
1058       regs *tmp = result->regs[i];
1059       result->regs[i] = result->regs[j];
1060       result->regs[j] = tmp;
1061       goto again;
1062     }
1063 }
1064
1065 /*-----------------------------------------------------------------*/
1066 /* serialRegAssign - serially allocate registers to the variables  */
1067 /*-----------------------------------------------------------------*/
1068 static void
1069 serialRegAssign (eBBlock ** ebbs, int count)
1070 {
1071   int i;
1072
1073   /* for all blocks */
1074   for (i = 0; i < count; i++)
1075     {
1076
1077       iCode *ic;
1078
1079       if (ebbs[i]->noPath &&
1080           (ebbs[i]->entryLabel != entryLabel &&
1081            ebbs[i]->entryLabel != returnLabel))
1082         continue;
1083
1084       /* of all instructions do */
1085       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1086         {
1087
1088           /* if this is an ipop that means some live
1089              range will have to be assigned again */
1090           if (ic->op == IPOP)
1091             reassignLR (IC_LEFT (ic));
1092
1093           /* if result is present && is a true symbol */
1094           if (IC_RESULT (ic) && ic->op != IFX &&
1095               IS_TRUE_SYMOP (IC_RESULT (ic)))
1096             OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1097
1098           /* take away registers from live
1099              ranges that end at this instruction */
1100           deassignLRs (ic, ebbs[i]);
1101
1102           /* some don't need registers */
1103           if (SKIP_IC2 (ic) ||
1104               ic->op == JUMPTABLE ||
1105               ic->op == IFX ||
1106               ic->op == IPUSH ||
1107               ic->op == IPOP ||
1108               (IC_RESULT (ic) && POINTER_SET (ic)))
1109             continue;
1110
1111           /* now we need to allocate registers
1112              only for the result */
1113           if (IC_RESULT (ic))
1114             {
1115               symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1116               bitVect *spillable;
1117               int willCS;
1118               int j;
1119               int ptrRegSet = 0;
1120
1121               /* if it does not need or is spilt 
1122                  or is already assigned to registers
1123                  or will not live beyond this instructions */
1124               if (!sym->nRegs ||
1125                   sym->isspilt ||
1126                   bitVectBitValue (_G.regAssigned, sym->key) ||
1127                   sym->liveTo <= ic->seq)
1128                 continue;
1129
1130               /* if some liverange has been spilt at the block level
1131                  and this one live beyond this block then spil this
1132                  to be safe */
1133               if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1134                 {
1135                   spillThis (sym);
1136                   continue;
1137                 }
1138               /* if trying to allocate this will cause
1139                  a spill and there is nothing to spill 
1140                  or this one is rematerializable then
1141                  spill this one */
1142               willCS = willCauseSpill (sym->nRegs, sym->regType);
1143               spillable = computeSpillable (ic);
1144               if (sym->remat ||
1145                   (willCS && bitVectIsZero (spillable)))
1146                 {
1147
1148                   spillThis (sym);
1149                   continue;
1150
1151                 }
1152
1153               /* if it has a spillocation & is used less than
1154                  all other live ranges then spill this */
1155                 if (willCS) {
1156                     if (sym->usl.spillLoc) {
1157                         symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1158                                                                          allLRs, ebbs[i], ic));
1159                         if (leastUsed && leastUsed->used > sym->used) {
1160                             spillThis (sym);
1161                             continue;
1162                         }
1163                     } else {
1164                         /* if none of the liveRanges have a spillLocation then better
1165                            to spill this one than anything else already assigned to registers */
1166                         if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1167                             spillThis (sym);
1168                             continue;
1169                         }
1170                     }
1171                 }
1172
1173               /* if we need ptr regs for the right side
1174                  then mark it */
1175               if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1176                   && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1177                   <= (unsigned) PTRSIZE)
1178                 {
1179                   ds390_ptrRegReq++;
1180                   ptrRegSet = 1;
1181                 }
1182               /* else we assign registers to it */
1183               _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1184
1185               for (j = 0; j < sym->nRegs; j++)
1186                 {
1187                   if (sym->regType == REG_PTR)
1188                     sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1189                   else
1190                     sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1191
1192                   /* if the allocation falied which means
1193                      this was spilt then break */
1194                   if (!sym->regs[j])
1195                     break;
1196                 }
1197               /* if it shares registers with operands make sure
1198                  that they are in the same position */
1199               if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1200                   OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1201                 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1202                               OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1203               /* do the same for the right operand */
1204               if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1205                   OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1206                 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1207                               OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1208
1209               if (ptrRegSet)
1210                 {
1211                   ds390_ptrRegReq--;
1212                   ptrRegSet = 0;
1213                 }
1214
1215             }
1216         }
1217     }
1218 }
1219
1220 /*-----------------------------------------------------------------*/
1221 /* rUmaskForOp :- returns register mask for an operand             */
1222 /*-----------------------------------------------------------------*/
1223 static bitVect *
1224 rUmaskForOp (operand * op)
1225 {
1226   bitVect *rumask;
1227   symbol *sym;
1228   int j;
1229
1230   /* only temporaries are assigned registers */
1231   if (!IS_ITEMP (op))
1232     return NULL;
1233
1234   sym = OP_SYMBOL (op);
1235
1236   /* if spilt or no registers assigned to it
1237      then nothing */
1238   if (sym->isspilt || !sym->nRegs)
1239     return NULL;
1240
1241   rumask = newBitVect (ds390_nRegs);
1242
1243   for (j = 0; j < sym->nRegs; j++)
1244     {
1245       rumask = bitVectSetBit (rumask,
1246                               sym->regs[j]->rIdx);
1247     }
1248
1249   return rumask;
1250 }
1251
1252 /*-----------------------------------------------------------------*/
1253 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1254 /*-----------------------------------------------------------------*/
1255 static bitVect *
1256 regsUsedIniCode (iCode * ic)
1257 {
1258   bitVect *rmask = newBitVect (ds390_nRegs);
1259
1260   /* do the special cases first */
1261   if (ic->op == IFX)
1262     {
1263       rmask = bitVectUnion (rmask,
1264                             rUmaskForOp (IC_COND (ic)));
1265       goto ret;
1266     }
1267
1268   /* for the jumptable */
1269   if (ic->op == JUMPTABLE)
1270     {
1271       rmask = bitVectUnion (rmask,
1272                             rUmaskForOp (IC_JTCOND (ic)));
1273
1274       goto ret;
1275     }
1276
1277   /* of all other cases */
1278   if (IC_LEFT (ic))
1279     rmask = bitVectUnion (rmask,
1280                           rUmaskForOp (IC_LEFT (ic)));
1281
1282
1283   if (IC_RIGHT (ic))
1284     rmask = bitVectUnion (rmask,
1285                           rUmaskForOp (IC_RIGHT (ic)));
1286
1287   if (IC_RESULT (ic))
1288     rmask = bitVectUnion (rmask,
1289                           rUmaskForOp (IC_RESULT (ic)));
1290
1291 ret:
1292   return rmask;
1293 }
1294
1295 /*-----------------------------------------------------------------*/
1296 /* createRegMask - for each instruction will determine the regsUsed */
1297 /*-----------------------------------------------------------------*/
1298 static void
1299 createRegMask (eBBlock ** ebbs, int count)
1300 {
1301   int i;
1302
1303   /* for all blocks */
1304   for (i = 0; i < count; i++)
1305     {
1306       iCode *ic;
1307
1308       if (ebbs[i]->noPath &&
1309           (ebbs[i]->entryLabel != entryLabel &&
1310            ebbs[i]->entryLabel != returnLabel))
1311         continue;
1312
1313       /* for all instructions */
1314       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1315         {
1316
1317           int j;
1318
1319           if (SKIP_IC2 (ic) || !ic->rlive)
1320             continue;
1321
1322           /* first mark the registers used in this
1323              instruction */
1324           ic->rUsed = regsUsedIniCode (ic);
1325           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1326
1327           /* now create the register mask for those 
1328              registers that are in use : this is a
1329              super set of ic->rUsed */
1330           ic->rMask = newBitVect (ds390_nRegs + 1);
1331
1332           /* for all live Ranges alive at this point */
1333           for (j = 1; j < ic->rlive->size; j++)
1334             {
1335               symbol *sym;
1336               int k;
1337
1338               /* if not alive then continue */
1339               if (!bitVectBitValue (ic->rlive, j))
1340                 continue;
1341
1342               /* find the live range we are interested in */
1343               if (!(sym = hTabItemWithKey (liveRanges, j)))
1344                 {
1345                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1346                           "createRegMask cannot find live range");
1347                   exit (0);
1348                 }
1349
1350               /* if no register assigned to it */
1351               if (!sym->nRegs || sym->isspilt)
1352                 continue;
1353
1354               /* for all the registers allocated to it */
1355               for (k = 0; k < sym->nRegs; k++)
1356                 if (sym->regs[k])
1357                   ic->rMask =
1358                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1359             }
1360         }
1361     }
1362 }
1363
1364 /*-----------------------------------------------------------------*/
1365 /* rematStr - returns the rematerialized string for a remat var    */
1366 /*-----------------------------------------------------------------*/
1367 static char *
1368 rematStr (symbol * sym)
1369 {
1370   char *s = buffer;
1371   iCode *ic = sym->rematiCode;
1372
1373   while (1)
1374     {
1375
1376       /* if plus or minus print the right hand side */
1377       if (ic->op == '+' || ic->op == '-')
1378         {
1379           sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1380                    ic->op);
1381           s += strlen (s);
1382           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1383           continue;
1384         }
1385
1386       /* we reached the end */
1387       sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1388       break;
1389     }
1390
1391   return buffer;
1392 }
1393
1394 /*-----------------------------------------------------------------*/
1395 /* regTypeNum - computes the type & number of registers required   */
1396 /*-----------------------------------------------------------------*/
1397 static void
1398 regTypeNum ()
1399 {
1400   symbol *sym;
1401   int k;
1402   iCode *ic;
1403
1404   /* for each live range do */
1405   for (sym = hTabFirstItem (liveRanges, &k); sym;
1406        sym = hTabNextItem (liveRanges, &k))
1407     {
1408
1409       /* if used zero times then no registers needed */
1410       if ((sym->liveTo - sym->liveFrom) == 0)
1411         continue;
1412
1413
1414       /* if the live range is a temporary */
1415       if (sym->isitmp)
1416         {
1417
1418           /* if the type is marked as a conditional */
1419           if (sym->regType == REG_CND)
1420             continue;
1421
1422           /* if used in return only then we don't 
1423              need registers */
1424           if (sym->ruonly || sym->accuse)
1425             {
1426               if (IS_AGGREGATE (sym->type) || sym->isptr)
1427                 sym->type = aggrToPtr (sym->type, FALSE);
1428               continue;
1429             }
1430
1431           /* if the symbol has only one definition &
1432              that definition is a get_pointer and the
1433              pointer we are getting is rematerializable and
1434              in "data" space */
1435
1436           if (bitVectnBitsOn (sym->defs) == 1 &&
1437               (ic = hTabItemWithKey (iCodehTab,
1438                                      bitVectFirstBit (sym->defs))) &&
1439               POINTER_GET (ic) &&
1440               !sym->noSpilLoc &&
1441               !IS_BITVAR (sym->etype))
1442             {
1443
1444
1445               /* if remat in data space */
1446               if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1447               // sym->type &&
1448                   DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1449                 {
1450
1451                   /* create a psuedo symbol & force a spil */
1452                   symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1453                   psym->type = sym->type;
1454                   psym->etype = sym->etype;
1455                   strcpy (psym->rname, psym->name);
1456                   sym->isspilt = 1;
1457                   sym->usl.spillLoc = psym;
1458                   continue;
1459                 }
1460
1461               /* if in data space or idata space then try to
1462                  allocate pointer register */
1463
1464             }
1465
1466           /* if not then we require registers */
1467           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1468                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1469                         getSize (sym->type));
1470
1471           if (sym->nRegs > 4)
1472             {
1473               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1474               printTypeChain (sym->type, stderr);
1475               fprintf (stderr, "\n");
1476             }
1477
1478           /* determine the type of register required */
1479           if (sym->nRegs == 1 &&
1480               IS_PTR (sym->type) &&
1481               sym->uptr)
1482             sym->regType = REG_PTR;
1483           else
1484             sym->regType = REG_GPR;
1485
1486         }
1487       else
1488         /* for the first run we don't provide */
1489         /* registers for true symbols we will */
1490         /* see how things go                  */
1491         sym->nRegs = 0;
1492     }
1493
1494 }
1495
1496 /*-----------------------------------------------------------------*/
1497 /* freeAllRegs - mark all registers as free                        */
1498 /*-----------------------------------------------------------------*/
1499 static void
1500 freeAllRegs ()
1501 {
1502   int i;
1503
1504   for (i = 0; i < ds390_nRegs; i++)
1505     regs390[i].isFree = 1;
1506 }
1507
1508 /*-----------------------------------------------------------------*/
1509 /* deallocStackSpil - this will set the stack pointer back         */
1510 /*-----------------------------------------------------------------*/
1511 static
1512 DEFSETFUNC (deallocStackSpil)
1513 {
1514   symbol *sym = item;
1515
1516   deallocLocal (sym);
1517   return 0;
1518 }
1519
1520 /*-----------------------------------------------------------------*/
1521 /* farSpacePackable - returns the packable icode for far variables */
1522 /*-----------------------------------------------------------------*/
1523 static iCode *
1524 farSpacePackable (iCode * ic)
1525 {
1526   iCode *dic;
1527
1528   /* go thru till we find a definition for the
1529      symbol on the right */
1530   for (dic = ic->prev; dic; dic = dic->prev)
1531     {
1532
1533       /* if the definition is a call then no */
1534       if ((dic->op == CALL || dic->op == PCALL) &&
1535           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1536         {
1537           return NULL;
1538         }
1539
1540       /* if shift by unknown amount then not */
1541       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1542           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1543         return NULL;
1544
1545       /* if pointer get and size > 1 */
1546       if (POINTER_GET (dic) &&
1547           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1548         return NULL;
1549
1550       if (POINTER_SET (dic) &&
1551           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1552         return NULL;
1553
1554       /* if any three is a true symbol in far space */
1555       if (IC_RESULT (dic) &&
1556           IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1557           isOperandInFarSpace (IC_RESULT (dic)))
1558         return NULL;
1559
1560       if (IC_RIGHT (dic) &&
1561           IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1562           isOperandInFarSpace (IC_RIGHT (dic)) &&
1563           !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1564         return NULL;
1565
1566       if (IC_LEFT (dic) &&
1567           IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1568           isOperandInFarSpace (IC_LEFT (dic)) &&
1569           !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1570         return NULL;
1571
1572       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1573         {
1574           if ((dic->op == LEFT_OP ||
1575                dic->op == RIGHT_OP ||
1576                dic->op == '-') &&
1577               IS_OP_LITERAL (IC_RIGHT (dic)))
1578             return NULL;
1579           else
1580             return dic;
1581         }
1582     }
1583
1584   return NULL;
1585 }
1586
1587 /*-----------------------------------------------------------------*/
1588 /* packRegsForAssign - register reduction for assignment           */
1589 /*-----------------------------------------------------------------*/
1590 static int
1591 packRegsForAssign (iCode * ic, eBBlock * ebp)
1592 {
1593   iCode *dic, *sic;
1594
1595   if (!IS_ITEMP (IC_RIGHT (ic)) ||
1596       OP_SYMBOL (IC_RIGHT (ic))->isind ||
1597       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1598     {
1599       return 0;
1600     }
1601
1602   /* if the true symbol is defined in far space or on stack
1603      then we should not since this will increase register pressure */
1604 #if 0
1605   if (isOperandInFarSpace (IC_RESULT (ic)))
1606     {
1607       if ((dic = farSpacePackable (ic)))
1608         goto pack;
1609       else
1610         return 0;
1611     }
1612 #else
1613   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1614     return 0;
1615   }
1616 #endif
1617
1618   /* find the definition of iTempNN scanning backwards if we find a 
1619      a use of the true symbol in before we find the definition then 
1620      we cannot */
1621   for (dic = ic->prev; dic; dic = dic->prev)
1622     {
1623       /* if there is a function call then don't pack it */
1624       if ((dic->op == CALL || dic->op == PCALL))
1625         {
1626           dic = NULL;
1627           break;
1628         }
1629
1630       if (SKIP_IC2 (dic))
1631         continue;
1632
1633       if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1634           IS_OP_VOLATILE (IC_RESULT (dic)))
1635         {
1636           dic = NULL;
1637           break;
1638         }
1639
1640       if (IS_SYMOP (IC_RESULT (dic)) &&
1641           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1642         {
1643           if (POINTER_SET (dic))
1644             dic = NULL;
1645
1646           break;
1647         }
1648
1649       if (IS_SYMOP (IC_RIGHT (dic)) &&
1650           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1651            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1652         {
1653           dic = NULL;
1654           break;
1655         }
1656
1657       if (IS_SYMOP (IC_LEFT (dic)) &&
1658           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1659            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1660         {
1661           dic = NULL;
1662           break;
1663         }
1664
1665       if (POINTER_SET (dic) &&
1666           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1667         {
1668           dic = NULL;
1669           break;
1670         }
1671     }
1672
1673   if (!dic)
1674     return 0;                   /* did not find */
1675
1676   /* if the result is on stack or iaccess then it must be
1677      the same atleast one of the operands */
1678   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1679       OP_SYMBOL (IC_RESULT (ic))->iaccess)
1680     {
1681
1682       /* the operation has only one symbol
1683          operator then we can pack */
1684       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1685           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1686         goto pack;
1687
1688       if (!((IC_LEFT (dic) &&
1689              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1690             (IC_RIGHT (dic) &&
1691              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1692         return 0;
1693     }
1694 pack:
1695   /* found the definition */
1696   /* replace the result with the result of */
1697   /* this assignment and remove this assignment */
1698   IC_RESULT (dic) = IC_RESULT (ic);
1699
1700   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1701     {
1702       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1703     }
1704   /* delete from liverange table also 
1705      delete from all the points inbetween and the new
1706      one */
1707   for (sic = dic; sic != ic; sic = sic->next)
1708     {
1709       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1710       if (IS_ITEMP (IC_RESULT (dic)))
1711         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1712     }
1713
1714   remiCodeFromeBBlock (ebp, ic);
1715   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1716   OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1717   return 1;
1718
1719 }
1720
1721 /*-----------------------------------------------------------------*/
1722 /* findAssignToSym : scanning backwards looks for first assig found */
1723 /*-----------------------------------------------------------------*/
1724 static iCode *
1725 findAssignToSym (operand * op, iCode * ic)
1726 {
1727   iCode *dic;
1728
1729   for (dic = ic->prev; dic; dic = dic->prev)
1730     {
1731
1732       /* if definition by assignment */
1733       if (dic->op == '=' &&
1734           !POINTER_SET (dic) &&
1735           IC_RESULT (dic)->key == op->key
1736 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1737         )
1738         {
1739
1740           /* we are interested only if defined in far space */
1741           /* or in stack space in case of + & - */
1742
1743           /* if assigned to a non-symbol then return
1744              true */
1745           if (!IS_SYMOP (IC_RIGHT (dic)))
1746             break;
1747
1748           /* if the symbol is in far space then
1749              we should not */
1750           if (isOperandInFarSpace (IC_RIGHT (dic)))
1751             return NULL;
1752
1753           /* for + & - operations make sure that
1754              if it is on the stack it is the same
1755              as one of the three operands */
1756           if ((ic->op == '+' || ic->op == '-') &&
1757               OP_SYMBOL (IC_RIGHT (dic))->onStack)
1758             {
1759
1760               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1761                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1762                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1763                 return NULL;
1764             }
1765
1766           break;
1767
1768         }
1769
1770       /* if we find an usage then we cannot delete it */
1771       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1772         return NULL;
1773
1774       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1775         return NULL;
1776
1777       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1778         return NULL;
1779     }
1780
1781   /* now make sure that the right side of dic
1782      is not defined between ic & dic */
1783   if (dic)
1784     {
1785       iCode *sic = dic->next;
1786
1787       for (; sic != ic; sic = sic->next)
1788         if (IC_RESULT (sic) &&
1789             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1790           return NULL;
1791     }
1792
1793   return dic;
1794
1795
1796 }
1797
1798 /*-----------------------------------------------------------------*/
1799 /* packRegsForSupport :- reduce some registers for support calls   */
1800 /*-----------------------------------------------------------------*/
1801 static int
1802 packRegsForSupport (iCode * ic, eBBlock * ebp)
1803 {
1804   int change = 0;
1805   /* for the left & right operand :- look to see if the
1806      left was assigned a true symbol in far space in that
1807      case replace them */
1808   if (IS_ITEMP (IC_LEFT (ic)) &&
1809       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1810     {
1811       iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1812       iCode *sic;
1813
1814       if (!dic)
1815         goto right;
1816
1817       /* found it we need to remove it from the
1818          block */
1819       for (sic = dic; sic != ic; sic = sic->next)
1820         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1821
1822       IC_LEFT (ic)->operand.symOperand =
1823         IC_RIGHT (dic)->operand.symOperand;
1824       IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1825       remiCodeFromeBBlock (ebp, dic);
1826       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1827       change++;
1828     }
1829
1830   /* do the same for the right operand */
1831 right:
1832   if (!change &&
1833       IS_ITEMP (IC_RIGHT (ic)) &&
1834       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1835     {
1836       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1837       iCode *sic;
1838
1839       if (!dic)
1840         return change;
1841
1842       /* if this is a subtraction & the result
1843          is a true symbol in far space then don't pack */
1844       if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
1845         {
1846           sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
1847           if (IN_FARSPACE (SPEC_OCLS (etype)))
1848             return change;
1849         }
1850       /* found it we need to remove it from the
1851          block */
1852       for (sic = dic; sic != ic; sic = sic->next)
1853         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1854
1855       IC_RIGHT (ic)->operand.symOperand =
1856         IC_RIGHT (dic)->operand.symOperand;
1857       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1858
1859       remiCodeFromeBBlock (ebp, dic);
1860       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1861       change++;
1862     }
1863
1864   return change;
1865 }
1866
1867 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1868
1869
1870 /*-----------------------------------------------------------------*/
1871 /* packRegsForOneuse : - will reduce some registers for single Use */
1872 /*-----------------------------------------------------------------*/
1873 static iCode *
1874 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1875 {
1876 #if 1
1877
1878   /* I can't figure out how to make this safe yet. */
1879   if ((int)ic+(int)op+(int)ebp) {
1880     return 0;
1881   } else {
1882     return 0;
1883   }
1884   return NULL;
1885
1886 #else
1887   bitVect *uses;
1888   iCode *dic, *sic;
1889
1890   /* if returning a literal then do nothing */
1891   if (!IS_SYMOP (op))
1892     return NULL;
1893
1894   /* only upto 2 bytes since we cannot predict
1895      the usage of b, & acc */
1896   if (getSize (operandType (op)) > (fReturnSizeDS390 - 2) &&
1897       ic->op != RETURN &&
1898       ic->op != SEND &&
1899       !POINTER_SET (ic) &&
1900       !POINTER_GET (ic))
1901     return NULL;
1902
1903   /* this routine will mark the a symbol as used in one 
1904      instruction use only && if the defintion is local 
1905      (ie. within the basic block) && has only one definition &&
1906      that definiion is either a return value from a 
1907      function or does not contain any variables in
1908      far space */
1909   uses = bitVectCopy (OP_USES (op));
1910   bitVectUnSetBit (uses, ic->key);      /* take away this iCode */
1911   if (!bitVectIsZero (uses))    /* has other uses */
1912     return NULL;
1913
1914   /* if it has only one defintion */
1915   if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1916     return NULL;                /* has more than one definition */
1917
1918   /* get the that definition */
1919   if (!(dic =
1920         hTabItemWithKey (iCodehTab,
1921                          bitVectFirstBit (OP_DEFS (op)))))
1922     return NULL;
1923
1924   /* if that only usage is a cast */
1925   if (dic->op == CAST) {
1926     /* to a bigger type */
1927     if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) > 
1928         getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
1929       /* than we can not, since we cannot predict the usage of b & acc */
1930       return NULL;
1931     }
1932   }
1933
1934   /* found the definition now check if it is local */
1935   if (dic->seq < ebp->fSeq ||
1936       dic->seq > ebp->lSeq)
1937     return NULL;                /* non-local */
1938
1939   /* now check if it is the return from
1940      a function call */
1941   if (dic->op == CALL || dic->op == PCALL)
1942     {
1943       if (ic->op != SEND && ic->op != RETURN)
1944         {
1945           OP_SYMBOL (op)->ruonly = 1;
1946           return dic;
1947         }
1948       dic = dic->next;
1949     }
1950
1951
1952   /* otherwise check that the definition does
1953      not contain any symbols in far space */
1954   if (isOperandInFarSpace (IC_LEFT (dic)) ||
1955       isOperandInFarSpace (IC_RIGHT (dic)) ||
1956       IS_OP_RUONLY (IC_LEFT (ic)) ||
1957       IS_OP_RUONLY (IC_RIGHT (ic)))
1958     {
1959       return NULL;
1960     }
1961
1962   /* if pointer set then make sure the pointer
1963      is one byte */
1964   if (POINTER_SET (dic) &&
1965       !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1966     return NULL;
1967
1968   if (POINTER_GET (dic) &&
1969       !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1970     return NULL;
1971
1972   sic = dic;
1973
1974   /* also make sure the intervenening instructions
1975      don't have any thing in far space */
1976   for (dic = dic->next; dic && dic != ic; dic = dic->next)
1977     {
1978
1979       /* if there is an intervening function call then no */
1980       if (dic->op == CALL || dic->op == PCALL)
1981         return NULL;
1982       /* if pointer set then make sure the pointer
1983          is one byte */
1984       if (POINTER_SET (dic) &&
1985           !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
1986         return NULL;
1987
1988       if (POINTER_GET (dic) &&
1989           !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
1990         return NULL;
1991
1992       /* if address of & the result is remat the okay */
1993       if (dic->op == ADDRESS_OF &&
1994           OP_SYMBOL (IC_RESULT (dic))->remat)
1995         continue;
1996
1997       /* if operand has size of three or more & this
1998          operation is a '*','/' or '%' then 'b' may
1999          cause a problem */
2000       if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2001           getSize (operandType (op)) >= 3)
2002         return NULL;
2003
2004       /* if left or right or result is in far space */
2005       if (isOperandInFarSpace (IC_LEFT (dic)) ||
2006           isOperandInFarSpace (IC_RIGHT (dic)) ||
2007           isOperandInFarSpace (IC_RESULT (dic)) ||
2008           IS_OP_RUONLY (IC_LEFT (dic)) ||
2009           IS_OP_RUONLY (IC_RIGHT (dic)) ||
2010           IS_OP_RUONLY (IC_RESULT (dic)))
2011         {
2012           return NULL;
2013         }
2014     }
2015
2016   OP_SYMBOL (op)->ruonly = 1;
2017   return sic;
2018 #endif
2019 }
2020
2021 /*-----------------------------------------------------------------*/
2022 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2023 /*-----------------------------------------------------------------*/
2024 static bool
2025 isBitwiseOptimizable (iCode * ic)
2026 {
2027   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2028   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2029
2030   /* bitwise operations are considered optimizable
2031      under the following conditions (Jean-Louis VERN) 
2032
2033      x & lit
2034      bit & bit
2035      bit & x
2036      bit ^ bit
2037      bit ^ x
2038      x   ^ lit
2039      x   | lit
2040      bit | bit
2041      bit | x
2042    */
2043   if ( IS_LITERAL (rtype) ||
2044       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2045     return TRUE;
2046   else
2047     return FALSE;
2048 }
2049
2050 /*-----------------------------------------------------------------*/
2051 /* packRegsForAccUse - pack registers for acc use                  */
2052 /*-----------------------------------------------------------------*/
2053 static void
2054 packRegsForAccUse (iCode * ic)
2055 {
2056   iCode *uic;
2057
2058   /* if + or - then it has to be one byte result */
2059   if ((ic->op == '+' || ic->op == '-')
2060       && getSize (operandType (IC_RESULT (ic))) > 1)
2061     return;
2062
2063   /* if shift operation make sure right side is not a literal */
2064   if (ic->op == RIGHT_OP &&
2065       (isOperandLiteral (IC_RIGHT (ic)) ||
2066        getSize (operandType (IC_RESULT (ic))) > 1))
2067     return;
2068
2069   if (ic->op == LEFT_OP &&
2070       (isOperandLiteral (IC_RIGHT (ic)) ||
2071        getSize (operandType (IC_RESULT (ic))) > 1))
2072     return;
2073
2074   if (IS_BITWISE_OP (ic) &&
2075       getSize (operandType (IC_RESULT (ic))) > 1)
2076     return;
2077
2078
2079   /* has only one definition */
2080   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2081     return;
2082
2083   /* has only one use */
2084   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2085     return;
2086
2087   /* and the usage immediately follows this iCode */
2088   if (!(uic = hTabItemWithKey (iCodehTab,
2089                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2090     return;
2091
2092   if (ic->next != uic)
2093     return;
2094
2095   /* if it is a conditional branch then we definitely can */
2096   if (uic->op == IFX)
2097     goto accuse;
2098
2099   if (uic->op == JUMPTABLE)
2100     return;
2101
2102   /* if the usage is not is an assignment
2103      or an arithmetic / bitwise / shift operation then not */
2104   if (POINTER_SET (uic) &&
2105       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2106     return;
2107
2108   if (uic->op != '=' &&
2109       !IS_ARITHMETIC_OP (uic) &&
2110       !IS_BITWISE_OP (uic) &&
2111       uic->op != LEFT_OP &&
2112       uic->op != RIGHT_OP)
2113     return;
2114
2115   /* if used in ^ operation then make sure right is not a 
2116      literl */
2117   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2118     return;
2119
2120   /* if shift operation make sure right side is not a literal */
2121   if (uic->op == RIGHT_OP &&
2122       (isOperandLiteral (IC_RIGHT (uic)) ||
2123        getSize (operandType (IC_RESULT (uic))) > 1))
2124     return;
2125
2126   if (uic->op == LEFT_OP &&
2127       (isOperandLiteral (IC_RIGHT (uic)) ||
2128        getSize (operandType (IC_RESULT (uic))) > 1))
2129     return;
2130
2131   /* make sure that the result of this icode is not on the
2132      stack, since acc is used to compute stack offset */
2133   if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2134       OP_SYMBOL (IC_RESULT (uic))->onStack)
2135     return;
2136
2137   /* if either one of them in far space then we cannot */
2138   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2139        isOperandInFarSpace (IC_LEFT (uic))) ||
2140       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2141        isOperandInFarSpace (IC_RIGHT (uic))))
2142     return;
2143
2144   /* if the usage has only one operand then we can */
2145   if (IC_LEFT (uic) == NULL ||
2146       IC_RIGHT (uic) == NULL)
2147     goto accuse;
2148
2149   /* make sure this is on the left side if not
2150      a '+' since '+' is commutative */
2151   if (ic->op != '+' &&
2152       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2153     return;
2154
2155 #if 0
2156   // this is too dangerous and need further restrictions
2157   // see bug #447547
2158
2159   /* if one of them is a literal then we can */
2160   if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2161       (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2162     {
2163       OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2164       return;
2165     }
2166 #endif
2167
2168   /* if the other one is not on stack then we can */
2169   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2170       (IS_ITEMP (IC_RIGHT (uic)) ||
2171        (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2172         !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2173     goto accuse;
2174
2175   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2176       (IS_ITEMP (IC_LEFT (uic)) ||
2177        (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2178         !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2179     goto accuse;
2180
2181   return;
2182
2183 accuse:
2184   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2185
2186
2187 }
2188
2189 /*-----------------------------------------------------------------*/
2190 /* packForPush - hueristics to reduce iCode for pushing            */
2191 /*-----------------------------------------------------------------*/
2192 static void
2193 packForPush (iCode * ic, eBBlock * ebp)
2194 {
2195   iCode *dic, *lic;
2196   bitVect *dbv;
2197
2198   if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2199     return;
2200
2201   /* must have only definition & one usage */
2202   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2203       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2204     return;
2205
2206   /* find the definition */
2207   if (!(dic = hTabItemWithKey (iCodehTab,
2208                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2209     return;
2210
2211   if (dic->op != '=' || POINTER_SET (dic))
2212     return;
2213
2214   /* make sure the right side does not have any definitions
2215      inbetween */
2216   dbv = OP_DEFS(IC_RIGHT(dic));
2217   for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2218           if (bitVectBitValue(dbv,lic->key)) return ;
2219   }
2220   /* make sure they have the same type */
2221   {
2222     sym_link *itype=operandType(IC_LEFT(ic));
2223     sym_link *ditype=operandType(IC_RIGHT(dic));
2224
2225     if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2226         SPEC_LONG(itype)!=SPEC_LONG(ditype))
2227       return;
2228   }
2229   /* extend the live range of replaced operand if needed */
2230   if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2231           OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2232   }
2233   /* we now we know that it has one & only one def & use
2234      and the that the definition is an assignment */
2235   IC_LEFT (ic) = IC_RIGHT (dic);
2236
2237   remiCodeFromeBBlock (ebp, dic);
2238   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2239 }
2240
2241 /*-----------------------------------------------------------------*/
2242 /* packRegisters - does some transformations to reduce register    */
2243 /*                   pressure                                      */
2244 /*-----------------------------------------------------------------*/
2245 static void
2246 packRegisters (eBBlock * ebp)
2247 {
2248   iCode *ic;
2249   int change = 0;
2250
2251   while (1)
2252     {
2253
2254       change = 0;
2255
2256       /* look for assignments of the form */
2257       /* iTempNN = TRueSym (someoperation) SomeOperand */
2258       /*       ....                       */
2259       /* TrueSym := iTempNN:1             */
2260       for (ic = ebp->sch; ic; ic = ic->next)
2261         {
2262
2263
2264           /* find assignment of the form TrueSym := iTempNN:1 */
2265           if (ic->op == '=' && !POINTER_SET (ic))
2266             change += packRegsForAssign (ic, ebp);
2267         }
2268
2269       if (!change)
2270         break;
2271     }
2272
2273   for (ic = ebp->sch; ic; ic = ic->next)
2274     {
2275
2276       /* if this is an itemp & result of a address of a true sym 
2277          then mark this as rematerialisable   */
2278       if (ic->op == ADDRESS_OF &&
2279           IS_ITEMP (IC_RESULT (ic)) &&
2280           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2281           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2282           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2283         {
2284
2285           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2286           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2287           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2288
2289         }
2290
2291       /* if straight assignment then carry remat flag if
2292          this is the only definition */
2293       if (ic->op == '=' &&
2294           !POINTER_SET (ic) &&
2295           IS_SYMOP (IC_RIGHT (ic)) &&
2296           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2297           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2298         {
2299
2300           OP_SYMBOL (IC_RESULT (ic))->remat =
2301             OP_SYMBOL (IC_RIGHT (ic))->remat;
2302           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2303             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2304         }
2305
2306       /* if this is a +/- operation with a rematerizable 
2307          then mark this as rematerializable as well */
2308       if ((ic->op == '+' || ic->op == '-') &&
2309           (IS_SYMOP (IC_LEFT (ic)) &&
2310            IS_ITEMP (IC_RESULT (ic)) &&
2311            OP_SYMBOL (IC_LEFT (ic))->remat &&
2312            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2313            IS_OP_LITERAL (IC_RIGHT (ic))))
2314         {
2315
2316           //int i = operandLitValue(IC_RIGHT(ic));
2317           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2318           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2319           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2320         }
2321
2322       /* mark the pointer usages */
2323       if (POINTER_SET (ic))
2324         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2325
2326       if (POINTER_GET (ic))
2327         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2328
2329       if (!SKIP_IC2 (ic))
2330         {
2331           /* if we are using a symbol on the stack
2332              then we should say ds390_ptrRegReq */
2333           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2334             ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2335                                  OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2336           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2337             ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2338                               OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2339           else
2340             {
2341               if (IS_SYMOP (IC_LEFT (ic)))
2342                 ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2343                                 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2344               if (IS_SYMOP (IC_RIGHT (ic)))
2345                 ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2346                                OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2347               if (IS_SYMOP (IC_RESULT (ic)))
2348                 ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2349                               OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2350             }
2351         }
2352
2353 #if 0
2354       /* if the condition of an if instruction
2355          is defined in the previous instruction then
2356          mark the itemp as a conditional */
2357       if ((IS_CONDITIONAL (ic) ||
2358            (IS_BITWISE_OP(ic) && isBitwiseOptimizable(ic))) &&
2359           ic->next && ic->next->op == IFX &&
2360           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2361           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2362         {
2363
2364           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2365           continue;
2366         }
2367 #else
2368       /* if the condition of an if instruction
2369          is defined in the previous instruction and
2370          this is the only usage then
2371          mark the itemp as a conditional */
2372       if ((IS_CONDITIONAL (ic) ||
2373            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2374           ic->next && ic->next->op == IFX &&
2375           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2376           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2377           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2378         {
2379           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2380           continue;
2381         }
2382 #endif
2383
2384       /* reduce for support function calls */
2385       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2386         packRegsForSupport (ic, ebp);
2387
2388       /* some cases the redundant moves can
2389          can be eliminated for return statements */
2390       if ((ic->op == RETURN || ic->op == SEND) &&
2391           !isOperandInFarSpace (IC_LEFT (ic)) &&
2392           !options.model)
2393         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2394
2395       /* if pointer set & left has a size more than
2396          one and right is not in far space */
2397       if (POINTER_SET (ic) &&
2398           !isOperandInFarSpace (IC_RIGHT (ic)) &&
2399           !OP_SYMBOL (IC_RESULT (ic))->remat &&
2400           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2401           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2402
2403         packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2404
2405       /* if pointer get */
2406       if (POINTER_GET (ic) &&
2407           !isOperandInFarSpace (IC_RESULT (ic)) &&
2408           !OP_SYMBOL (IC_LEFT (ic))->remat &&
2409           !IS_OP_RUONLY (IC_RESULT (ic)) &&
2410           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2411
2412         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2413
2414
2415       /* if this is cast for intergral promotion then
2416          check if only use of  the definition of the 
2417          operand being casted/ if yes then replace
2418          the result of that arithmetic operation with 
2419          this result and get rid of the cast */
2420       if (ic->op == CAST)
2421         {
2422           sym_link *fromType = operandType (IC_RIGHT (ic));
2423           sym_link *toType = operandType (IC_LEFT (ic));
2424
2425           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2426               getSize (fromType) != getSize (toType) &&
2427               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2428             {
2429
2430               iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2431               if (dic)
2432                 {
2433                   if (IS_ARITHMETIC_OP (dic))
2434                     {
2435                       IC_RESULT (dic) = IC_RESULT (ic);
2436                       remiCodeFromeBBlock (ebp, ic);
2437                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2438                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2439                       ic = ic->prev;
2440                     }
2441                   else
2442                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2443                 }
2444             }
2445           else
2446             {
2447
2448               /* if the type from and type to are the same
2449                  then if this is the only use then packit */
2450               if (compareType (operandType (IC_RIGHT (ic)),
2451                              operandType (IC_LEFT (ic))) == 1)
2452                 {
2453                   iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2454                   if (dic)
2455                     {
2456                       IC_RESULT (dic) = IC_RESULT (ic);
2457                       remiCodeFromeBBlock (ebp, ic);
2458                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2459                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2460                       ic = ic->prev;
2461                     }
2462                 }
2463             }
2464         }
2465
2466       /* pack for PUSH 
2467          iTempNN := (some variable in farspace) V1
2468          push iTempNN ;
2469          -------------
2470          push V1
2471        */
2472       if (ic->op == IPUSH)
2473         {
2474           packForPush (ic, ebp);
2475         }
2476
2477
2478       /* pack registers for accumulator use, when the
2479          result of an arithmetic or bit wise operation
2480          has only one use, that use is immediately following
2481          the defintion and the using iCode has only one
2482          operand or has two operands but one is literal &
2483          the result of that operation is not on stack then
2484          we can leave the result of this operation in acc:b
2485          combination */
2486       if ((IS_ARITHMETIC_OP (ic)
2487
2488            || IS_BITWISE_OP (ic)
2489
2490            || ic->op == LEFT_OP || ic->op == RIGHT_OP
2491
2492           ) &&
2493           IS_ITEMP (IC_RESULT (ic)) &&
2494           getSize (operandType (IC_RESULT (ic))) <= 2)
2495
2496         packRegsForAccUse (ic);
2497
2498     }
2499 }
2500
2501 /*-----------------------------------------------------------------*/
2502 /* assignRegisters - assigns registers to each live range as need  */
2503 /*-----------------------------------------------------------------*/
2504 void
2505 ds390_assignRegisters (eBBlock ** ebbs, int count)
2506 {
2507   iCode *ic;
2508   int i;
2509
2510   setToNull ((void *) &_G.funcrUsed);
2511   ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2512   ds390_nRegs = 8;
2513
2514   /* change assignments this will remove some
2515      live ranges reducing some register pressure */
2516   for (i = 0; i < count; i++)
2517     packRegisters (ebbs[i]);
2518
2519   if (options.dump_pack)
2520     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2521
2522   /* first determine for each live range the number of 
2523      registers & the type of registers required for each */
2524   regTypeNum ();
2525
2526   /* and serially allocate registers */
2527   serialRegAssign (ebbs, count);
2528
2529   /* if stack was extended then tell the user */
2530   if (_G.stackExtend)
2531     {
2532 /*      werror(W_TOOMANY_SPILS,"stack", */
2533 /*             _G.stackExtend,currFunc->name,""); */
2534       _G.stackExtend = 0;
2535     }
2536
2537   if (_G.dataExtend)
2538     {
2539 /*      werror(W_TOOMANY_SPILS,"data space", */
2540 /*             _G.dataExtend,currFunc->name,""); */
2541       _G.dataExtend = 0;
2542     }
2543
2544   /* after that create the register mask
2545      for each of the instruction */
2546   createRegMask (ebbs, count);
2547
2548   /* redo that offsets for stacked automatic variables */
2549   redoStackOffsets ();
2550
2551   if (options.dump_rassgn)
2552     dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2553
2554   /* do the overlaysegment stuff SDCCmem.c */
2555   doOverlays (ebbs, count);
2556
2557   /* now get back the chain */
2558   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2559
2560
2561   gen390Code (ic);
2562
2563   /* free up any _G.stackSpil locations allocated */
2564   applyToSet (_G.stackSpil, deallocStackSpil);
2565   _G.slocNum = 0;
2566   setToNull ((void **) &_G.stackSpil);
2567   setToNull ((void **) &_G.spiltSet);
2568   /* mark all registers as free */
2569   freeAllRegs ();
2570
2571   return;
2572 }