Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050721150029

Diff

Diff from 1.32 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/mip/optim2.c

Annotated File View

ragge
1.32
1 /*      $Id: optim2.c,v 1.32 2005/07/21 15:00:29 ragge Exp $    */
ragge
1.1
2 /*
3  * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "pass2.h"
30
ragge
1.3
31 #include <string.h>
pj
1.4
32 #include <stdlib.h>
33
34 #ifndef MIN
35 #define MIN(a,b) (((a)<(b))?(a):(b))
36 #endif
37
38 #ifndef MAX
39 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
40 #endif
ragge
1.3
41
ragge
1.30
42 #define BDEBUG(x)       if (b2debug) printf x
43
pj
1.8
44 static int dfsnum;
ragge
1.1
45
46 void saveip(struct interpass *ip);
47 void deljumps(void);
48 void deltemp(NODE *p);
49 void optdump(struct interpass *ip);
ragge
1.19
50 void printip(struct interpass *pole);
ragge
1.1
51
pj
1.21
52 static struct varinfo defsites;
ragge
1.11
53 static struct interpass ipole;
ragge
1.19
54 struct interpass *storesave;
ragge
1.30
55 static struct interpass_prolog *ipp, *epp/* prolog/epilog */
ragge
1.1
56
ragge
1.30
57 void bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo);
pj
1.4
58 void cfg_build(struct labelinfo *labinfo);
pj
1.8
59 void cfg_dfs(struct basicblock *bbunsigned int parent
60              struct bblockinfo *bbinfo);
61 void dominators(struct bblockinfo *bbinfo);
pj
1.9
62 struct basicblock *
63 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
64 void link(struct basicblock *parentstruct basicblock *child);
65 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.21
66 void findTemps(struct interpass *ip);
pj
1.20
67 void placePhiFunctions(struct bblockinfo *bbinfo);
pj
1.15
68 void remunreach(void);
pj
1.4
69
ragge
1.11
70 static struct basicblock bblocks;
pj
1.4
71
ragge
1.1
72 void
73 saveip(struct interpass *ip)
74 {
ragge
1.23
75         static int inftn;
ragge
1.5
76
pj
1.4
77 #if 0
ragge
1.2
78         int regs;
pj
1.4
79 #endif
80         struct labelinfo labinfo;
pj
1.8
81         struct bblockinfo bbinfo;
ragge
1.1
82
ragge
1.23
83         if (ip->type == IP_PROLOG) {
ragge
1.11
84                 DLIST_INIT(&ipoleqelem);
ragge
1.23
85                 inftn = 1;
86         } else if (inftn == 0)
87                 comperr("saveip");
ragge
1.11
88
89         DLIST_INSERT_BEFORE(&ipoleipqelem);
ragge
1.1
90
91         if (ip->type != IP_EPILOG)
92                 return;
ragge
1.23
93         inftn = 0;
ragge
1.30
94         ipp = (struct interpass_prolog *)DLIST_NEXT(&ipoleqelem);
ragge
1.5
95         epp = (struct interpass_prolog *)ip;
ragge
1.1
96
ragge
1.30
97         if (b2debug) {
98                 printf("initial links\n");
99                 printip(&ipole);
100         }
101
ragge
1.26
102         if (xdeljumps)
103                 deljumps();     /* Delete redundant jumps and dead code */
ragge
1.28
104
ragge
1.30
105         if (b2debug) {
106                 printf("links after deljumps\n");
107                 printip(&ipole);
108         }
pj
1.4
109         if (xssaflag) {
ragge
1.11
110                 DLIST_INIT(&bblocksbbelem);
ragge
1.30
111                 bblocks_build(&labinfo, &bbinfo);
ragge
1.32
112                 BDEBUG(("Calling cfg_build\n"));
ragge
1.30
113                 cfg_build(&labinfo);
ragge
1.32
114                 BDEBUG(("Calling dominators\n"));
ragge
1.30
115                 dominators(&bbinfo);
ragge
1.32
116                 BDEBUG(("Calling computeDF\n"));
ragge
1.30
117                 computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
ragge
1.32
118                 BDEBUG(("Calling remunreach\n"));
ragge
1.30
119                 remunreach();
pj
1.4
120 #if 0
ragge
1.30
121                 dfg = dfg_build(cfg);
122                 ssa = ssa_build(cfgdfg);
pj
1.4
123 #endif
124         }
ragge
1.2
125
126 #ifdef PCC_DEBUG
ragge
1.5
127         if (epp->ipp_regs != MAXRVAR)
ragge
1.2
128                 comperr("register error");
129 #endif
ragge
1.1
130
ragge
1.5
131         ipp->ipp_autos = epp->ipp_autos;
132         ipp->ipp_regs = epp->ipp_regs// = regs;
ragge
1.1
133
134 #ifdef MYOPTIM
pj
1.6
135         myoptim((struct interpass *)ipp);
ragge
1.1
136 #endif
137
ragge
1.19
138 if (xnewreg == 0) {
139         int tmpautooff;
140         NODE *p;
141
142         p2autooff = p2maxautooff = AUTOINIT;
143         /* Must verify stack usage first */
144         DLIST_FOREACH(ip, &ipoleqelem) {
145                 if (ip->type == IP_STKOFF) {
146                         p2autooff = ip->ip_off;
147                         if (p2autooff > p2maxautooff)
148                                 p2maxautooff = p2autooff;
149                 }
150         }
151         p2autooff = p2maxautooff/* don't have live range analysis yet */
152
153         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.23
154                 if (ip->type == IPSTK) {
ragge
1.19
155                         struct interpass *ip3;
156                         p2autooff = ip->ip_off;
157                         ip3 = ip;
158                         ip = DLIST_NEXT(ipqelem);
159                         DLIST_REMOVE(ip3qelem);
160                 }
161                         
162                 if (ip->type != IP_NODE)
163                         continue;
164
165                 p = ip->ip_node;
166                 walkf(pdeltemp);
167
168                 tmpautooff = p2autooff;
169
170                 geninsn(pFOREFF);
171                 if (sucomp(p) < 0) {
172                         /* Create STKOFF entry */
173                         struct interpass *ip3;
174                         DLIST_INSERT_BEFORE(ipstoresaveqelem);
175                         ip3 = ipnode(NULL);
ragge
1.23
176                         ip3->type = IPSTK;
ragge
1.19
177                         ip3->ip_off = tmpautooff;
178                         DLIST_INSERT_AFTER(ipip3qelem);
179                         ip = DLIST_PREV(storesaveqelem);
180                         continue;
181                 }
182
183                 p2autooff = tmpautooff;
184
185                 genregs(p);
186                 mygenregs(p);
187         }
188
189 else {
190         /*
191          * Loop over instruction assignment until the register assignment
192          * code is satisfied.
193          */
ragge
1.25
194 #if 0
195         extern int tempmintempmax;
196
197         tempmin = ip->ip_tmpnum;
198         tempmin = ie->ip_tmpnum;
199 #endif
ragge
1.19
200         do {
201                 /* do instruction assignment */
202                 DLIST_FOREACH(ip, &ipoleqelem) {
203                         if (ip->type != IP_NODE)
204                                 continue;
205                         geninsn(ip->ip_nodeFOREFF);
206                         nsucomp(ip->ip_node);
207                 }
ragge
1.28
208         } while (ngenregs(&ipole));
ragge
1.19
209 }
210
211         DLIST_FOREACH(ip, &ipoleqelem) {
212                 emit(ip);
213         }
ragge
1.11
214         DLIST_INIT(&ipoleqelem);
ragge
1.1
215 }
216
ragge
1.26
217 /*
218  * Delete unused labels, excess of labels, gotos to gotos.
219  * This routine can be made much more efficient.
220  */
ragge
1.1
221 void
222 deljumps()
223 {
ragge
1.28
224         struct interpass *ip, *n, *ip2;
ragge
1.26
225         int gotone,lowhigh;
ragge
1.28
226         int *lblaryszoi;
ragge
1.26
227
228         low = ipp->ip_lblnum;
229         high = epp->ip_lblnum;
230
231 #ifdef notyet
232         mark = tmpmark(); /* temporary used memory */
233 #endif
ragge
1.28
234
ragge
1.26
235         sz = (high-low) * sizeof(int);
236         lblary = tmpalloc(sz);
237
238 again:  gotone = 0;
ragge
1.28
239         memset(lblary0sz);
ragge
1.26
240
ragge
1.28
241         /* refcount and coalesce  all labels */
ragge
1.27
242         DLIST_FOREACH(ip, &ipoleqelem) {
243                 if (ip->type == IP_DEFLAB) {
244                         n = DLIST_NEXT(ipqelem);
ragge
1.28
245                         while (n->type == IP_DEFLAB || n->type == IP_STKOFF) {
246                                 if (n->type == IP_DEFLAB &&
247                                     lblary[n->ip_lbl-low] >= 0)
248                                         lblary[n->ip_lbl-low] = -ip->ip_lbl;
ragge
1.27
249                                 n = DLIST_NEXT(nqelem);
250                         }
251                 }
ragge
1.28
252                 if (ip->type != IP_NODE)
253                         continue;
254                 o = ip->ip_node->n_op;
255                 if (o == GOTO)
256                         i = ip->ip_node->n_left->n_lval;
257                 else if (o == CBRANCH)
258                         i = ip->ip_node->n_right->n_lval;
259                 else
260                         continue;
261                 lblary[i-low] |= 1;
ragge
1.27
262         }
263
ragge
1.28
264         /* delete coalesced/unused labels and rename gotos */
265         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.27
266                 n = DLIST_NEXT(ipqelem);
ragge
1.28
267                 if (n->type == IP_DEFLAB) {
268                         if (lblary[n->ip_lbl-low] <= 0) {
269                                 DLIST_REMOVE(nqelem);
270                                 gotone = 1;
ragge
1.26
271                         }
272                         continue;
273                 }
274                 if (n->type != IP_NODE)
275                         continue;
276                 o = n->ip_node->n_op;
277                 if (o == GOTO)
278                         i = n->ip_node->n_left->n_lval;
279                 else if (o == CBRANCH)
280                         i = n->ip_node->n_right->n_lval;
281                 else
282                         continue;
283                 if (lblary[i-low] < 0) {
284                         if (o == GOTO)
285                                 n->ip_node->n_left->n_lval = -lblary[i-low];
286                         else
287                                 n->ip_node->n_right->n_lval = -lblary[i-low];
288                 }
289         }
ragge
1.27
290
ragge
1.28
291         /* Delete gotos to the next statement */
ragge
1.26
292         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.28
293                 n = DLIST_NEXT(ipqelem);
294                 if (n->type != IP_NODE)
ragge
1.26
295                         continue;
ragge
1.28
296                 o = n->ip_node->n_op;
ragge
1.26
297                 if (o == GOTO)
ragge
1.28
298                         i = n->ip_node->n_left->n_lval;
ragge
1.26
299                 else if (o == CBRANCH)
ragge
1.28
300                         i = n->ip_node->n_right->n_lval;
ragge
1.26
301                 else
302                         continue;
ragge
1.28
303                 ip2 = DLIST_NEXT(nqelem);
304                 if (ip2->type != IP_DEFLAB)
ragge
1.1
305                         continue;
ragge
1.28
306                 if (ip2->ip_lbl == i) {
ragge
1.1
307                         tfree(n->ip_node);
ragge
1.11
308                         DLIST_REMOVE(nqelem);
ragge
1.28
309                         gotone = 1;
ragge
1.1
310                 }
311         }
ragge
1.28
312
ragge
1.1
313         if (gotone)
314                 goto again;
ragge
1.28
315
316 #ifdef notyet
317         tmpfree(mark);
ragge
1.26
318 #endif
ragge
1.1
319 }
320
321 void
322 optdump(struct interpass *ip)
323 {
324         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
325                 "deflab""defnam""asm" };
326         printf("type %s\n"nm[ip->type-1]);
327         switch (ip->type) {
328         case IP_NODE:
329                 fwalk(ip->ip_nodee2print0);
330                 break;
331         case IP_DEFLAB:
332                 printf("label " LABFMT "\n"ip->ip_lbl);
333                 break;
334         case IP_ASM:
335                 printf(": %s\n"ip->ip_asm);
336                 break;
337         }
338 }
pj
1.4
339
340 /*
341  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
342  *
343  * Also fills the labelinfo struct with information about which bblocks
344  * that contain which label.
345  */
346
ragge
1.30
347 void
pj
1.8
348 bblocks_build(struct labelinfo *labinfostruct bblockinfo *bbinfo)
pj
1.4
349 {
350         struct interpass *ip;
351         struct basicblock *bb = NULL;
ragge
1.30
352         int lowhigh;
pj
1.8
353         int count = 0;
pj
1.4
354         int i;
355
ragge
1.30
356         BDEBUG(("bblocks_build (%p, %p)\n"labinfobbinfo));
357         low = ipp->ip_lblnum;
358         high = epp->ip_lblnum;
359
pj
1.4
360         /* 
361          * First statement is a leader.
362          * Any statement that is target of a jump is a leader.
363          * Any statement that immediately follows a jump is a leader.
364          */
ragge
1.31
365         DLIST_FOREACH(ip, &ipoleqelem) {
ragge
1.32
366                 /* ignore stackoff in beginning or end of bblocks */
367                 if (ip->type == IP_STKOFF && bb == NULL)
368                         continue;
369
ragge
1.31
370                 if (bb == NULL || (ip->type == IP_EPILOG) ||
371                     (ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) {
372                         bb = tmpalloc(sizeof(struct basicblock));
373                         bb->first = ip;
374                         SLIST_INIT(&bb->children);
375                         SLIST_INIT(&bb->parents);
376                         bb->dfnum = 0;
377                         bb->dfparent = 0;
378                         bb->semi = 0;
379                         bb->ancestor = 0;
380                         bb->idom = 0;
381                         bb->samedom = 0;
382                         bb->bucket = NULL;
383                         bb->df = NULL;
384                         bb->dfchildren = NULL;
385                         bb->Aorig = NULL;
386                         bb->Aphi = NULL;
387                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
388                         count++;
389                 }
390                 bb->last = ip;
391                 if ((ip->type == IP_NODE) && (ip->ip_node->n_op == GOTO || 
392                     ip->ip_node->n_op == CBRANCH))
393                         bb = NULL;
394                 if (ip->type == IP_PROLOG)
395                         bb = NULL;
396         }
ragge
1.30
397
ragge
1.31
398         if (b2debug) {
ragge
1.32
399                 printf("Basic blocks in func: %d, low %d, high %d\n",
400                     countlowhigh);
ragge
1.31
401                 DLIST_FOREACH(bb, &bblocksbbelem) {
402                         printf("bb %p: first %p last %p\n"bb,
403                             bb->firstbb->last);
404                 }
405         }
pj
1.4
406
407         labinfo->low = low;
408         labinfo->size = high - low + 1;
409         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
pj
1.8
410         for (i = 0i <= labinfo->sizei++) {
pj
1.4
411                 labinfo->arr[i] = NULL;
412         }
pj
1.8
413         
414         bbinfo->size = count + 1;
415         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
416         for (i = 0i <= bbinfo->sizei++) {
417                 bbinfo->arr[i] = NULL;
418         }
pj
1.4
419
ragge
1.32
420         /* Build the label table */
ragge
1.11
421         DLIST_FOREACH(bb, &bblocksbbelem) {
ragge
1.32
422                 if (bb->first->type == IP_DEFLAB)
pj
1.4
423                         labinfo->arr[bb->first->ip_lbl - low] = bb;
ragge
1.32
424         }
425
426         if (b2debug) {
427                 printf("Label table:\n");
428                 for (i = 0i < labinfo->sizei++)
429                         if (labinfo->arr[i])
430                                 printf("Label %d bblock %p\n"i+low,
431                                     labinfo->arr[i]);
pj
1.4
432         }
433 }
434
435 /*
436  * Build the control flow graph.
437  */
438
439 void
440 cfg_build(struct labelinfo *labinfo)
441 {
442         /* Child and parent nodes */
443         struct cfgnode *cnode
444         struct cfgnode *pnode;
445         struct basicblock *bb;
446         
ragge
1.11
447         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
448
449                 if (bb->first->type == IP_EPILOG) {
450                         break;
451                 }
452
453                 cnode = tmpalloc(sizeof(struct cfgnode));
454                 pnode = tmpalloc(sizeof(struct cfgnode));
455                 pnode->bblock = bb;
456
457                 if ((bb->last->type == IP_NODE) && 
458                     (bb->last->ip_node->n_op == GOTO)) {
459                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
460                             labinfo->size) {
461                                 comperr("Label out of range: %d, base %d"
462                                         bb->last->ip_node->n_left->n_lval
463                                         labinfo->low);
464                         }
465                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
466                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
467                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
468                         continue;
469                 }
470                 if ((bb->last->type == IP_NODE) && 
471                     (bb->last->ip_node->n_op == CBRANCH)) {
472                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
473                             labinfo->size
474                                 comperr("Label out of range: %d"
475                                         bb->last->ip_node->n_left->n_lval);
476                         
477                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
478                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
479                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
480                         cnode = tmpalloc(sizeof(struct cfgnode));
481                         pnode = tmpalloc(sizeof(struct cfgnode));
482                         pnode->bblock = bb;
483                 }
484
ragge
1.11
485                 cnode->bblock = DLIST_NEXT(bbbbelem);
486                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
487                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
488         }
489 }
pj
1.6
490
pj
1.8
491 void
492 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
493 {
494         struct cfgnode *cnode;
495         
496         if (bb->dfnum != 0)
497                 return;
498
499         bb->dfnum = ++dfsnum;
500         bb->dfparent = parent;
501         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
502         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
503                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
504         }
pj
1.13
505         /* Don't bring in unreachable nodes in the future */
pj
1.20
506         bbinfo->size = dfsnum + 1;
pj
1.8
507 }
508
ragge
1.11
509 static bittype *
510 setalloc(int nelem)
511 {
512         bittype *b;
513         int sz = (nelem+NUMBITS-1)/NUMBITS;
514
515         b = tmpalloc(sz * sizeof(bittype));
516         memset(b0sz * sizeof(bittype));
517         return b;
518 }
519
pj
1.8
520 /*
521  * Algorithm 19.9, pp 414 from Appel.
522  */
523
524 void
525 dominators(struct bblockinfo *bbinfo)
526 {
527         struct cfgnode *cnode;
528         struct basicblock *bb, *y, *v;
529         struct basicblock *s, *sprime, *p;
pj
1.10
530         int hi;
pj
1.8
531
ragge
1.11
532         DLIST_FOREACH(bb, &bblocksbbelem) {
533                 bb->bucket = setalloc(bbinfo->size);
534                 bb->df = setalloc(bbinfo->size);
535                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
536         }
537
538         dfsnum = 0;
ragge
1.11
539         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
540
ragge
1.30
541         if (b2debug) {
542                 struct basicblock *bbb;
543                 struct cfgnode *ccnode;
544
545                 DLIST_FOREACH(bbb, &bblocksbbelem) {
546                         printf("Basic block %d, parents: "bbb->dfnum);
547                         SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
548                                 printf("%d, "ccnode->bblock->dfnum);
549                         }
550                         printf("\nChildren: ");
551                         SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
552                                 printf("%d, "ccnode->bblock->dfnum);
553                         }
554                         printf("\n");
pj
1.20
555                 }
556         }
557
pj
1.10
558         for(h = bbinfo->size - 1h > 1h--) {
559                 bb = bbinfo->arr[h];
pj
1.8
560                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
561                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
562                         if (cnode->bblock->dfnum <= bb->dfnum
563                                 sprime = cnode->bblock;
pj
1.10
564                         else 
565                                 sprime = bbinfo->arr[ancestorwithlowestsemi
566                                               (cnode->bblockbbinfo)->semi];
pj
1.8
567                         if (sprime->dfnum < s->dfnum)
568                                 s = sprime;
569                 }
570                 bb->semi = s->dfnum;
pj
1.10
571                 BITSET(s->bucketbb->dfnum);
pj
1.8
572                 link(pbb);
573                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
574                         if(TESTBIT(p->bucketi)) {
pj
1.8
575                                 v = bbinfo->arr[i];
pj
1.10
576                                 y = ancestorwithlowestsemi(vbbinfo);
577                                 if (y->semi == v->semi
pj
1.8
578                                         v->idom = p->dfnum;
579                                 else
580                                         v->samedom = y->dfnum;
581                         }
582                 }
583                 memset(p->bucket0, (bbinfo->size + 7)/8);
584         }
ragge
1.30
585
586         if (b2debug) {
587                 printf("Num\tSemi\tAncest\tidom\n");
588                 DLIST_FOREACH(bb, &bblocksbbelem) {
589                         printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semi,
590                             bb->ancestorbb->idom);
591                 }
pj
1.10
592         }
ragge
1.30
593
pj
1.10
594         for(h = 2h < bbinfo->sizeh++) {
595                 bb = bbinfo->arr[h];
596                 if (bb->samedom != 0) {
pj
1.8
597                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
598                 }
599         }
ragge
1.11
600         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
601                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
ragge
1.30
602                         BDEBUG(("Setting child %d of %d\n",
603                             bb->dfnumbbinfo->arr[bb->idom]->dfnum));
pj
1.10
604                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
605                 }
pj
1.8
606         }
607 }
608
609
610 struct basicblock *
611 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
612 {
613         struct basicblock *u = bblock;
614         struct basicblock *v = bblock;
615
616         while (v->ancestor != 0) {
617                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
618                     bbinfo->arr[u->semi]->dfnum
pj
1.8
619                         u = v;
620                 v = bbinfo->arr[v->ancestor];
621         }
622         return u;
623 }
624
625 void
626 link(struct basicblock *parentstruct basicblock *child)
627 {
628         child->ancestor = parent->dfnum;
629 }
630
631 void
632 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
633 {
634         struct cfgnode *cnode;
pj
1.10
635         int hi;
pj
1.8
636         
ragge
1.11
637         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
638                 if (cnode->bblock->idom != bblock->dfnum)
639                         BITSET(bblock->dfcnode->bblock->dfnum);
640         }
pj
1.10
641         for (h = 1h < bbinfo->sizeh++) {
642                 if (!TESTBIT(bblock->dfchildrenh))
643                         continue;
644                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
645                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
646                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
647                             (bbinfo->arr[h] == bblock ||
648                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
649                             BITSET(bblock->dfi);
650                 }
651         }
652 }
pj
1.15
653
pj
1.21
654 static struct basicblock *currbb;
655 static struct interpass *currip;
656
657 /* Helper function for findTemps, Find assignment nodes. */
658 static void
659 findasg(NODE *p)
660 {
661         struct pvarinfo *pv;
662
663         if (p->n_op != ASSIGN)
664                 return;
665
666         if (p->n_left->n_op != TEMP)
667                 return;
668
669         pv = tmpcalloc(sizeof(struct pvarinfo));
670         pv->next = defsites.arr[p->n_left->n_lval];
671         pv->bb = currbb;
672         pv->top = currip->ip_node;
673         pv->n = p->n_left;
pj
1.22
674         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
675
676         defsites.arr[p->n_left->n_lval] = pv;
677 }
678
679 /* Walk the interpass looking for assignment nodes. */
680 void findTemps(struct interpass *ip)
pj
1.20
681 {
682         if (ip->type != IP_NODE)
683                 return;
684
pj
1.21
685         currip = ip;
686
687         walkf(ip->ip_nodefindasg);
pj
1.20
688 }
689
690 /*
691  * Algorithm 19.6 from Appel.
692  */
693
694 void
695 placePhiFunctions(struct bblockinfo *bbinfo)
696 {
697         struct basicblock *bb;
698         struct interpass *ip;
699         int maxtmpijkl;
700         struct pvarinfo *n;
701         struct cfgnode *cnode;
702         TWORD ntype;
703         NODE *p;
pj
1.22
704         struct pvarinfo *pv;
pj
1.20
705
706         bb = DLIST_NEXT(&bblocksbbelem);
707         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
708         bb = DLIST_PREV(&bblocksbbelem);
709         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
710         defsites.size = maxtmp - defsites.low + 1;
711         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
712
pj
1.21
713         /* Find all defsites */
pj
1.20
714         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
715                 currbb = bb;
pj
1.20
716                 ip = bb->first;
pj
1.22
717                 bb->Aorig = setalloc(defsites.size);
718                 bb->Aphi = setalloc(defsites.size);
719                 
pj
1.20
720
721                 while (ip != bb->last) {
pj
1.21
722                         findTemps(ip);
pj
1.20
723                         ip = DLIST_NEXT(ipqelem);
724                 }
725                 /* Make sure we get the last statement in the bblock */
pj
1.21
726                 findTemps(ip);
pj
1.20
727         }
pj
1.21
728         /* For each variable */
pj
1.20
729         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
730                 /* While W not empty */
pj
1.20
731                 while (defsites.arr[i] != NULL) {
pj
1.21
732                         /* Remove some node n from W */
pj
1.20
733                         n = defsites.arr[i];
734                         defsites.arr[i] = n->next;
pj
1.21
735                         /* For each y in n->bb->df */
pj
1.20
736                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
737                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
738                                         continue;
pj
1.22
739                                 
740                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
741                                         continue;
742
pj
1.20
743                                 ntype = n->n->n_type;
744                                 k = 0;
pj
1.21
745                                 /* Amount of predecessors for y */
pj
1.20
746                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
747                                         k++;
pj
1.21
748                                 /* Construct phi(...) */
pj
1.20
749                                 p = mklnode(TEMPi0ntype);
750                                 for (l = 0l < k-1l++)
751                                         p = mkbinode(PHIp,
752                                             mklnode(TEMPi0ntype), ntype);
753                                 ip = ipnode(mkbinode(ASSIGN,
754                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
755                                 /* Insert phi at top of basic block */
pj
1.20
756                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
757                                 n->bb->first = ip;
pj
1.22
758                                 BITSET(bbinfo->arr[j]->Aphii);
759                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
760                                         pv = tmpalloc(sizeof(struct pvarinfo));
761                                         // XXXpj Ej fullständig information.
762                                         pv->bb = bbinfo->arr[j];
763                                         pv->next = defsites.arr[i]->next;
764                                         defsites.arr[i] = pv;
765                                 }
766                                         
pj
1.20
767
768                         }
769                 }
770         }
771 }
772
pj
1.15
773 /*
774  * Remove unreachable nodes in the CFG.
775  */ 
776
777 void
778 remunreach(void)
779 {
780         struct basicblock *bb, *nbb;
781         struct interpass *next, *ctree;
782
783         bb = DLIST_NEXT(&bblocksbbelem);
784         while (bb != &bblocks) {
785                 nbb = DLIST_NEXT(bbbbelem);
786
787                 /* Code with dfnum 0 is unreachable */
788                 if (bb->dfnum != 0) {
789                         bb = nbb;
790                         continue;
791                 }
792
793                 /* Need the epilogue node for other parts of the
794                    compiler, set its label to 0 and backend will
795                    handle it. */ 
796                 if (bb->first->type == IP_EPILOG) {
797                         bb->first->ip_lbl = 0;
798                         bb = nbb;
799                         continue;
800                 }
801
802                 next = bb->first;
803                 do {
804                         ctree = next;
805                         next = DLIST_NEXT(ctreeqelem);
806                         
807                         if (ctree->type == IP_NODE)
pj
1.16
808                                 tfree(ctree->ip_node);
pj
1.15
809                         DLIST_REMOVE(ctreeqelem);
810                 } while (ctree != bb->last);
811                         
812                 DLIST_REMOVE(bbbbelem);
813                 bb = nbb;
814         }
815 }
ragge
1.19
816
817 void
818 printip(struct interpass *pole)
819 {
820         static char *foo[] = {
821            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
822         struct interpass *ip;
823
824         DLIST_FOREACH(ippoleqelem) {
825                 if (ip->type > MAXIP)
826                         printf("IP(%d) (%p): "ip->typeip);
827                 else
828                         printf("%s (%p): "foo[ip->type], ip);
829                 switch (ip->type) {
830                 case IP_NODEprintf("\n");
831                         fwalk(ip->ip_nodee2print0); break;
832                 case IP_PROLOG:
833                         printf("%s\n",
834                             ((struct interpass_prolog *)ip)->ipp_name); break;
835                 case IP_STKOFFprintf("%d\n"ip->ip_off); break;
836                 case IP_EPILOGprintf("\n"); break;
837                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
838                 }
839         }
840 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-08-23 01:37 +0200