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