Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20071029004011

Diff

Diff from 1.47 to:

Annotations

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

Annotated File View

gmcgarry
1.47
1 /*      $Id: optim2.c,v 1.47 2007/10/29 00:40:11 gmcgarry 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);
ragge
1.38
47 void deljumps(struct interpass *);
ragge
1.1
48 void optdump(struct interpass *ip);
ragge
1.19
49 void printip(struct interpass *pole);
ragge
1.1
50
pj
1.21
51 static struct varinfo defsites;
ragge
1.19
52 struct interpass *storesave;
ragge
1.30
53 static struct interpass_prolog *ipp, *epp/* prolog/epilog */
ragge
1.1
54
ragge
1.38
55 void bblocks_build(struct interpass *, struct labelinfo *, struct bblockinfo *);
pj
1.4
56 void cfg_build(struct labelinfo *labinfo);
pj
1.8
57 void cfg_dfs(struct basicblock *bbunsigned int parent
58              struct bblockinfo *bbinfo);
59 void dominators(struct bblockinfo *bbinfo);
pj
1.9
60 struct basicblock *
61 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo);
62 void link(struct basicblock *parentstruct basicblock *child);
63 void computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo);
pj
1.21
64 void findTemps(struct interpass *ip);
pj
1.20
65 void placePhiFunctions(struct bblockinfo *bbinfo);
pj
1.15
66 void remunreach(void);
pj
1.4
67
ragge
1.34
68 struct basicblock bblocks;
69 int nbblocks;
ragge
1.44
70 static struct interpass *cvpole;
pj
1.4
71
ragge
1.38
72 struct addrof {
73         struct addrof *next;
74         int tempnum;
75         int oregoff;
76 } *otlink;
77
78 static int
79 getoff(int num)
80 {
81         struct addrof *w;
82
83         for (w = otlinkww = w->next)
84                 if (w->tempnum == num)
85                         return w->oregoff;
86         return 0;
87 }
88
89 /*
ragge
1.44
90  * Use stack argument addresses instead of copying if & is used on a var.
91  */
92 static int
93 setargs(int tvalstruct addrof *w)
94 {
95         struct interpass *ip;
96         NODE *p;
97
98         ip = DLIST_NEXT(cvpoleqelem); /* PROLOG */
99         ip = DLIST_NEXT(ipqelem); /* first DEFLAB */
100         ip = DLIST_NEXT(ipqelem); /* first NODE */
101         for (; ip->type != IP_DEFLABip = DLIST_NEXT(ipqelem)) {
102                 p = ip->ip_node;
103 #ifdef PCC_DEBUG
104                 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
105                         comperr("temparg");
106 #endif
107                 if (p->n_right->n_op != OREG)
108                         continue/* arg in register */
109                 if (tval != p->n_left->n_lval)
110                         continue/* wrong assign */
111                 w->oregoff = p->n_right->n_lval;
112                 tfree(p);
113                 DLIST_REMOVE(ipqelem);
114                 return 1;
115         }
116         return 0;
117 }
118
119 /*
ragge
1.38
120  * Search for ADDROF elements and, if found, record them.
121  */
122 static void
123 findaddrof(NODE *p)
124 {
125         struct addrof *w;
126
127         if (p->n_op != ADDROF)
128                 return;
129         if (getoff(p->n_left->n_lval))
130                 return;
131         w = tmpalloc(sizeof(struct addrof));
132         w->tempnum = p->n_left->n_lval;
ragge
1.44
133         if (setargs(p->n_left->n_lvalw) == 0)
134                 w->oregoff = BITOOR(freetemp(szty(p->n_left->n_type)));
ragge
1.38
135         w->next = otlink;
136         otlink = w;
137 }
138
ragge
1.44
139
ragge
1.38
140 /*
141  * Convert address-taken temps to OREGs.
142  */
143 static void
144 cvtaddrof(NODE *p)
145 {
146         NODE *l;
147         int n;
148
149         if (p->n_op != ADDROF && p->n_op != TEMP)
150                 return;
151         if (p->n_op == TEMP) {
152                 n = getoff(p->n_lval);
153                 if (n == 0)
154                         return;
155                 p->n_op = OREG;
156                 p->n_lval = n;
157                 p->n_rval = FPREG;
158         } else {
159                 l = p->n_left;
ragge
1.41
160                 l->n_type = p->n_type;
ragge
1.38
161                 p->n_right = mklnode(ICONl->n_lval0l->n_type);
162                 p->n_op = PLUS;
163                 l->n_op = REG;
164                 l->n_lval = 0;
165                 l->n_rval = FPREG;
166                 
167         }
168 }
169
170 void
171 optimize(struct interpass *ipole)
172 {
173         struct interpass *ip;
174         struct labelinfo labinfo;
175         struct bblockinfo bbinfo;
176
177         ipp = (struct interpass_prolog *)DLIST_NEXT(ipoleqelem);
178         epp = (struct interpass_prolog *)DLIST_PREV(ipoleqelem);
179
180         if (b2debug) {
181                 printf("initial links\n");
182                 printip(ipole);
183         }
184
185         /*
186          * Convert ADDROF TEMP to OREGs.
187          */
188         if (xtemps) {
189                 otlink = NULL;
ragge
1.44
190                 cvpole = ipole;
ragge
1.38
191                 DLIST_FOREACH(ipipoleqelem) {
192                         if (ip->type != IP_NODE)
193                                 continue;
194                         walkf(ip->ip_nodefindaddrof);
195                 }
196                 if (otlink) {
197                         DLIST_FOREACH(ipipoleqelem) {
198                                 if (ip->type != IP_NODE)
199                                         continue;
200                                 walkf(ip->ip_nodecvtaddrof);
201                         }
202                 }
203         }
204                 
205         if (xdeljumps)
206                 deljumps(ipole); /* Delete redundant jumps and dead code */
207
208 #ifdef PCC_DEBUG
209         if (b2debug) {
210                 printf("links after deljumps\n");
211                 printip(ipole);
212         }
213 #endif
214         if (xssaflag || xtemps) {
215                 DLIST_INIT(&bblocksbbelem);
216                 bblocks_build(ipole, &labinfo, &bbinfo);
217                 BDEBUG(("Calling cfg_build\n"));
218                 cfg_build(&labinfo);
219         }
220         if (xssaflag) {
221                 BDEBUG(("Calling dominators\n"));
222                 dominators(&bbinfo);
223                 BDEBUG(("Calling computeDF\n"));
224                 computeDF(DLIST_NEXT(&bblocksbbelem), &bbinfo);
225                 BDEBUG(("Calling remunreach\n"));
226                 remunreach();
227 #if 0
228                 dfg = dfg_build(cfg);
229                 ssa = ssa_build(cfgdfg);
230 #endif
231         }
232
233 #ifdef PCC_DEBUG
234         if (epp->ipp_regs != 0)
235                 comperr("register error");
236 #endif
237
238 #ifdef MYOPTIM
239         myoptim((struct interpass *)ipp);
240 #endif
241 }
ragge
1.1
242
ragge
1.26
243 /*
244  * Delete unused labels, excess of labels, gotos to gotos.
245  * This routine can be made much more efficient.
246  */
ragge
1.1
247 void
ragge
1.38
248 deljumps(struct interpass *ipole)
ragge
1.1
249 {
ragge
1.45
250         struct interpass *ip, *n, *ip2, *start;
ragge
1.26
251         int gotone,lowhigh;
ragge
1.45
252         int *lblary, *jmparyszoijlab1lab2;
253         int del;
ragge
1.46
254         extern int negrel[];
255         extern size_t negrelsize;
ragge
1.26
256
257         low = ipp->ip_lblnum;
258         high = epp->ip_lblnum;
259
260 #ifdef notyet
261         mark = tmpmark(); /* temporary used memory */
262 #endif
ragge
1.28
263
ragge
1.26
264         sz = (high-low) * sizeof(int);
265         lblary = tmpalloc(sz);
ragge
1.45
266         jmpary = tmpalloc(sz);
267
268         /*
269          * XXX: Find the first two labels. They may not be deleted,
270          * because the register allocator expects them to be there.
271          * These will not be coalesced with any other label.
272          */
273         lab1 = lab2 = -1;
274         start = NULL;
275         DLIST_FOREACH(ipipoleqelem) {
276                 if (ip->type != IP_DEFLAB)
277                         continue;
278                 if (lab1 < 0)
279                         lab1 = ip->ip_lbl;
280                 else if (lab2 < 0) {
281                         lab2 = ip->ip_lbl;
282                         start = ip;
283                 } else  /* lab1 >= 0 && lab2 >= 0, we're done. */
284                         break;
285         }
286         if (lab1 < 0 || lab2 < 0)
287                 comperr("deljumps");
ragge
1.26
288
289 again:  gotone = 0;
ragge
1.28
290         memset(lblary0sz);
ragge
1.45
291         lblary[lab1 - low] = lblary[lab2 - low] = 1;
292         memset(jmpary0sz);
ragge
1.26
293
ragge
1.33
294         /* refcount and coalesce all labels */
ragge
1.38
295         DLIST_FOREACH(ipipoleqelem) {
ragge
1.45
296                 if (ip->type == IP_DEFLAB && ip->ip_lbl != lab1 &&
297                     ip->ip_lbl != lab2) {
ragge
1.27
298                         n = DLIST_NEXT(ipqelem);
ragge
1.45
299
300                         /*
301                          * Find unconditional jumps directly following a
302                          * label.
303                          */
304                         if (n->type == IP_NODE && n->ip_node->n_op == GOTO) {
305                                 i = n->ip_node->n_left->n_lval;
306                                 jmpary[ip->ip_lbl - low] = i;
307                         }
308
ragge
1.38
309                         while (n->type == IP_DEFLAB) {
ragge
1.45
310                                 if (n->ip_lbl != lab1 && n->ip_lbl != lab2 &&
311                                     lblary[n->ip_lbl-low] >= 0) {
312                                         /*
313                                          * If the label is used, mark the
314                                          * label to be coalesced with as
315                                          * used, too.
316                                          */
317                                         if (lblary[n->ip_lbl - low] > 0 &&
318                                             lblary[ip->ip_lbl - low] == 0)
319                                                 lblary[ip->ip_lbl - low] = 1;
320                                         lblary[n->ip_lbl - low] = -ip->ip_lbl;
321                                 }
ragge
1.27
322                                 n = DLIST_NEXT(nqelem);
323                         }
324                 }
ragge
1.28
325                 if (ip->type != IP_NODE)
326                         continue;
327                 o = ip->ip_node->n_op;
328                 if (o == GOTO)
329                         i = ip->ip_node->n_left->n_lval;
330                 else if (o == CBRANCH)
331                         i = ip->ip_node->n_right->n_lval;
332                 else
333                         continue;
ragge
1.45
334
335                 /*
336                  * Mark destination label i as used, if it is not already.
337                  * If i is to be merged with label j, mark j as used, too.
338                  */
339                 if (lblary[i - low] == 0)
340                         lblary[i-low] = 1;
341                 else if ((j = lblary[i - low]) < 0 && lblary[-j - low] == 0)
342                         lblary[-j - low] = 1;
ragge
1.27
343         }
344
ragge
1.28
345         /* delete coalesced/unused labels and rename gotos */
ragge
1.38
346         DLIST_FOREACH(ipipoleqelem) {
ragge
1.27
347                 n = DLIST_NEXT(ipqelem);
ragge
1.28
348                 if (n->type == IP_DEFLAB) {
349                         if (lblary[n->ip_lbl-low] <= 0) {
350                                 DLIST_REMOVE(nqelem);
351                                 gotone = 1;
ragge
1.26
352                         }
353                 }
ragge
1.45
354                 if (ip->type != IP_NODE)
ragge
1.26
355                         continue;
ragge
1.45
356                 o = ip->ip_node->n_op;
ragge
1.26
357                 if (o == GOTO)
ragge
1.45
358                         i = ip->ip_node->n_left->n_lval;
ragge
1.26
359                 else if (o == CBRANCH)
ragge
1.45
360                         i = ip->ip_node->n_right->n_lval;
ragge
1.26
361                 else
362                         continue;
ragge
1.45
363
364                 /* Simplify (un-)conditional jumps to unconditional jumps. */
365                 if (jmpary[i - low] > 0) {
366                         gotone = 1;
367                         i = jmpary[i - low];
368                         if (o == GOTO)
369                                 ip->ip_node->n_left->n_lval = i;
370                         else
371                                 ip->ip_node->n_right->n_lval = i;
372                 }
373
374                 /* Fixup for coalesced labels. */
ragge
1.26
375                 if (lblary[i-low] < 0) {
376                         if (o == GOTO)
ragge
1.45
377                                 ip->ip_node->n_left->n_lval = -lblary[i-low];
ragge
1.26
378                         else
ragge
1.45
379                                 ip->ip_node->n_right->n_lval = -lblary[i-low];
ragge
1.26
380                 }
381         }
ragge
1.27
382
ragge
1.28
383         /* Delete gotos to the next statement */
ragge
1.38
384         DLIST_FOREACH(ipipoleqelem) {
ragge
1.28
385                 n = DLIST_NEXT(ipqelem);
386                 if (n->type != IP_NODE)
ragge
1.26
387                         continue;
ragge
1.28
388                 o = n->ip_node->n_op;
ragge
1.26
389                 if (o == GOTO)
ragge
1.28
390                         i = n->ip_node->n_left->n_lval;
ragge
1.26
391                 else if (o == CBRANCH)
ragge
1.28
392                         i = n->ip_node->n_right->n_lval;
ragge
1.26
393                 else
394                         continue;
ragge
1.33
395
396                 ip2 = n;
ragge
1.38
397                 ip2 = DLIST_NEXT(ip2qelem);
ragge
1.33
398
ragge
1.28
399                 if (ip2->type != IP_DEFLAB)
ragge
1.1
400                         continue;
ragge
1.45
401                 if (ip2->ip_lbl == i && i != lab1 && i != lab2) {
ragge
1.1
402                         tfree(n->ip_node);
ragge
1.11
403                         DLIST_REMOVE(nqelem);
ragge
1.28
404                         gotone = 1;
ragge
1.1
405                 }
406         }
ragge
1.28
407
ragge
1.45
408         /*
409          * Transform cbranch cond, 1; goto 2; 1: ... into
410          * cbranch !cond, 2; 1: ...
411          */
412         DLIST_FOREACH(ipipoleqelem) {
413                 n = DLIST_NEXT(ipqelem);
414                 ip2 = DLIST_NEXT(nqelem);
415                 if (ip->type != IP_NODE || ip->ip_node->n_op != CBRANCH)
416                         continue;
417                 if (n->type != IP_NODE || n->ip_node->n_op != GOTO)
418                         continue;
419                 if (ip2->type != IP_DEFLAB)
420                         continue;
421                 i = ip->ip_node->n_right->n_lval;
422                 j = n->ip_node->n_left->n_lval;
423                 if (j == lab1 || j == lab2)
424                         continue;
425                 if (i != ip2->ip_lbl || i == lab1 || i == lab2)
426                         continue;
427                 ip->ip_node->n_right->n_lval = j;
ragge
1.46
428                 i = ip->ip_node->n_left->n_op;
429                 if (i < EQ || i - EQ >= negrelsize)
ragge
1.45
430                         comperr("deljumps: unexpected op");
ragge
1.46
431                 ip->ip_node->n_left->n_op = negrel[i - EQ];
ragge
1.45
432                 tfree(n->ip_node);
433                 DLIST_REMOVE(nqelem);
434                 gotone = 1;
435         }
436
437         /* Delete everything after a goto up to the next label. */
438         for (ip = startdel = 0ip != DLIST_ENDMARK(ipole);
439              ip = DLIST_NEXT(ipqelem)) {
440 loop:
441                 if ((n = DLIST_NEXT(ipqelem)) == DLIST_ENDMARK(ipole))
442                         break;
443                 if (n->type != IP_NODE) {
444                         del = 0;
445                         continue;
446                 }
447                 if (del) {
448                         tfree(n->ip_node);
449                         DLIST_REMOVE(nqelem);
450                         gotone = 1;
451                         goto loop;
452                 } else if (n->ip_node->n_op == GOTO)
453                         del = 1;
454         }
455
ragge
1.1
456         if (gotone)
457                 goto again;
ragge
1.28
458
459 #ifdef notyet
460         tmpfree(mark);
ragge
1.26
461 #endif
ragge
1.1
462 }
463
464 void
465 optdump(struct interpass *ip)
466 {
467         static char *nm[] = { "node""prolog""newblk""epilog""locctr",
468                 "deflab""defnam""asm" };
469         printf("type %s\n"nm[ip->type-1]);
470         switch (ip->type) {
471         case IP_NODE:
472                 fwalk(ip->ip_nodee2print0);
473                 break;
474         case IP_DEFLAB:
475                 printf("label " LABFMT "\n"ip->ip_lbl);
476                 break;
477         case IP_ASM:
478                 printf(": %s\n"ip->ip_asm);
479                 break;
480         }
481 }
pj
1.4
482
483 /*
484  * Build the basic blocks, algorithm 9.1, pp 529 in Compilers.
485  *
486  * Also fills the labelinfo struct with information about which bblocks
487  * that contain which label.
488  */
489
ragge
1.30
490 void
ragge
1.38
491 bblocks_build(struct interpass *ipolestruct labelinfo *labinfo,
492     struct bblockinfo *bbinfo)
pj
1.4
493 {
494         struct interpass *ip;
495         struct basicblock *bb = NULL;
ragge
1.30
496         int lowhigh;
pj
1.8
497         int count = 0;
pj
1.4
498         int i;
499
ragge
1.30
500         BDEBUG(("bblocks_build (%p, %p)\n"labinfobbinfo));
501         low = ipp->ip_lblnum;
502         high = epp->ip_lblnum;
503
pj
1.4
504         /* 
505          * First statement is a leader.
506          * Any statement that is target of a jump is a leader.
507          * Any statement that immediately follows a jump is a leader.
508          */
ragge
1.38
509         DLIST_FOREACH(ipipoleqelem) {
ragge
1.31
510                 if (bb == NULL || (ip->type == IP_EPILOG) ||
511                     (ip->type == IP_DEFLAB) || (ip->type == IP_DEFNAM)) {
512                         bb = tmpalloc(sizeof(struct basicblock));
513                         bb->first = ip;
514                         SLIST_INIT(&bb->children);
515                         SLIST_INIT(&bb->parents);
516                         bb->dfnum = 0;
517                         bb->dfparent = 0;
518                         bb->semi = 0;
519                         bb->ancestor = 0;
520                         bb->idom = 0;
521                         bb->samedom = 0;
522                         bb->bucket = NULL;
523                         bb->df = NULL;
524                         bb->dfchildren = NULL;
525                         bb->Aorig = NULL;
526                         bb->Aphi = NULL;
ragge
1.34
527                         bb->bbnum = count;
ragge
1.31
528                         DLIST_INSERT_BEFORE(&bblocksbbbbelem);
529                         count++;
530                 }
531                 bb->last = ip;
532                 if ((ip->type == IP_NODE) && (ip->ip_node->n_op == GOTO || 
533                     ip->ip_node->n_op == CBRANCH))
534                         bb = NULL;
535                 if (ip->type == IP_PROLOG)
536                         bb = NULL;
537         }
ragge
1.34
538         nbblocks = count;
ragge
1.30
539
ragge
1.31
540         if (b2debug) {
ragge
1.32
541                 printf("Basic blocks in func: %d, low %d, high %d\n",
542                     countlowhigh);
ragge
1.31
543                 DLIST_FOREACH(bb, &bblocksbbelem) {
544                         printf("bb %p: first %p last %p\n"bb,
545                             bb->firstbb->last);
546                 }
547         }
pj
1.4
548
549         labinfo->low = low;
550         labinfo->size = high - low + 1;
551         labinfo->arr = tmpalloc(labinfo->size * sizeof(struct basicblock *));
ragge
1.42
552         for (i = 0i < labinfo->sizei++) {
pj
1.4
553                 labinfo->arr[i] = NULL;
554         }
pj
1.8
555         
556         bbinfo->size = count + 1;
557         bbinfo->arr = tmpalloc(bbinfo->size * sizeof(struct basicblock *));
ragge
1.42
558         for (i = 0i < bbinfo->sizei++) {
pj
1.8
559                 bbinfo->arr[i] = NULL;
560         }
pj
1.4
561
ragge
1.32
562         /* Build the label table */
ragge
1.11
563         DLIST_FOREACH(bb, &bblocksbbelem) {
ragge
1.32
564                 if (bb->first->type == IP_DEFLAB)
pj
1.4
565                         labinfo->arr[bb->first->ip_lbl - low] = bb;
ragge
1.32
566         }
567
568         if (b2debug) {
569                 printf("Label table:\n");
570                 for (i = 0i < labinfo->sizei++)
571                         if (labinfo->arr[i])
572                                 printf("Label %d bblock %p\n"i+low,
573                                     labinfo->arr[i]);
pj
1.4
574         }
575 }
576
577 /*
578  * Build the control flow graph.
579  */
580
581 void
582 cfg_build(struct labelinfo *labinfo)
583 {
584         /* Child and parent nodes */
585         struct cfgnode *cnode
586         struct cfgnode *pnode;
587         struct basicblock *bb;
588         
ragge
1.11
589         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.4
590
591                 if (bb->first->type == IP_EPILOG) {
592                         break;
593                 }
594
595                 cnode = tmpalloc(sizeof(struct cfgnode));
596                 pnode = tmpalloc(sizeof(struct cfgnode));
597                 pnode->bblock = bb;
598
599                 if ((bb->last->type == IP_NODE) && 
gmcgarry
1.47
600                     (bb->last->ip_node->n_op == GOTO) &&
601                     (bb->last->ip_node->n_left->n_op == ICON))  {
pj
1.4
602                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
603                             labinfo->size) {
604                                 comperr("Label out of range: %d, base %d"
605                                         bb->last->ip_node->n_left->n_lval
606                                         labinfo->low);
607                         }
608                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
609                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
610                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
611                         continue;
612                 }
613                 if ((bb->last->type == IP_NODE) && 
614                     (bb->last->ip_node->n_op == CBRANCH)) {
615                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
616                             labinfo->size
617                                 comperr("Label out of range: %d"
618                                         bb->last->ip_node->n_left->n_lval);
619                         
620                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
621                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
622                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
623                         cnode = tmpalloc(sizeof(struct cfgnode));
624                         pnode = tmpalloc(sizeof(struct cfgnode));
625                         pnode->bblock = bb;
626                 }
627
ragge
1.11
628                 cnode->bblock = DLIST_NEXT(bbbbelem);
629                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
630                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
631         }
632 }
pj
1.6
633
pj
1.8
634 void
635 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
636 {
637         struct cfgnode *cnode;
638         
639         if (bb->dfnum != 0)
640                 return;
641
642         bb->dfnum = ++dfsnum;
643         bb->dfparent = parent;
644         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
645         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
646                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
647         }
pj
1.13
648         /* Don't bring in unreachable nodes in the future */
pj
1.20
649         bbinfo->size = dfsnum + 1;
pj
1.8
650 }
651
ragge
1.11
652 static bittype *
653 setalloc(int nelem)
654 {
655         bittype *b;
656         int sz = (nelem+NUMBITS-1)/NUMBITS;
657
658         b = tmpalloc(sz * sizeof(bittype));
659         memset(b0sz * sizeof(bittype));
660         return b;
661 }
662
pj
1.8
663 /*
664  * Algorithm 19.9, pp 414 from Appel.
665  */
666
667 void
668 dominators(struct bblockinfo *bbinfo)
669 {
670         struct cfgnode *cnode;
671         struct basicblock *bb, *y, *v;
672         struct basicblock *s, *sprime, *p;
pj
1.10
673         int hi;
pj
1.8
674
ragge
1.11
675         DLIST_FOREACH(bb, &bblocksbbelem) {
676                 bb->bucket = setalloc(bbinfo->size);
677                 bb->df = setalloc(bbinfo->size);
678                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
679         }
680
681         dfsnum = 0;
ragge
1.11
682         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
683
ragge
1.30
684         if (b2debug) {
685                 struct basicblock *bbb;
686                 struct cfgnode *ccnode;
687
688                 DLIST_FOREACH(bbb, &bblocksbbelem) {
689                         printf("Basic block %d, parents: "bbb->dfnum);
690                         SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
691                                 printf("%d, "ccnode->bblock->dfnum);
692                         }
693                         printf("\nChildren: ");
694                         SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
695                                 printf("%d, "ccnode->bblock->dfnum);
696                         }
697                         printf("\n");
pj
1.20
698                 }
699         }
700
pj
1.10
701         for(h = bbinfo->size - 1h > 1h--) {
702                 bb = bbinfo->arr[h];
pj
1.8
703                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
704                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
705                         if (cnode->bblock->dfnum <= bb->dfnum
706                                 sprime = cnode->bblock;
pj
1.10
707                         else 
708                                 sprime = bbinfo->arr[ancestorwithlowestsemi
709                                               (cnode->bblockbbinfo)->semi];
pj
1.8
710                         if (sprime->dfnum < s->dfnum)
711                                 s = sprime;
712                 }
713                 bb->semi = s->dfnum;
pj
1.10
714                 BITSET(s->bucketbb->dfnum);
pj
1.8
715                 link(pbb);
716                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
717                         if(TESTBIT(p->bucketi)) {
pj
1.8
718                                 v = bbinfo->arr[i];
pj
1.10
719                                 y = ancestorwithlowestsemi(vbbinfo);
720                                 if (y->semi == v->semi
pj
1.8
721                                         v->idom = p->dfnum;
722                                 else
723                                         v->samedom = y->dfnum;
724                         }
725                 }
726                 memset(p->bucket0, (bbinfo->size + 7)/8);
727         }
ragge
1.30
728
729         if (b2debug) {
730                 printf("Num\tSemi\tAncest\tidom\n");
731                 DLIST_FOREACH(bb, &bblocksbbelem) {
732                         printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semi,
733                             bb->ancestorbb->idom);
734                 }
pj
1.10
735         }
ragge
1.30
736
pj
1.10
737         for(h = 2h < bbinfo->sizeh++) {
738                 bb = bbinfo->arr[h];
739                 if (bb->samedom != 0) {
pj
1.8
740                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
741                 }
742         }
ragge
1.11
743         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
744                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
ragge
1.30
745                         BDEBUG(("Setting child %d of %d\n",
746                             bb->dfnumbbinfo->arr[bb->idom]->dfnum));
pj
1.10
747                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
748                 }
pj
1.8
749         }
750 }
751
752
753 struct basicblock *
754 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
755 {
756         struct basicblock *u = bblock;
757         struct basicblock *v = bblock;
758
759         while (v->ancestor != 0) {
760                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
761                     bbinfo->arr[u->semi]->dfnum
pj
1.8
762                         u = v;
763                 v = bbinfo->arr[v->ancestor];
764         }
765         return u;
766 }
767
768 void
769 link(struct basicblock *parentstruct basicblock *child)
770 {
771         child->ancestor = parent->dfnum;
772 }
773
774 void
775 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
776 {
777         struct cfgnode *cnode;
pj
1.10
778         int hi;
pj
1.8
779         
ragge
1.11
780         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
781                 if (cnode->bblock->idom != bblock->dfnum)
782                         BITSET(bblock->dfcnode->bblock->dfnum);
783         }
pj
1.10
784         for (h = 1h < bbinfo->sizeh++) {
785                 if (!TESTBIT(bblock->dfchildrenh))
786                         continue;
787                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
788                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
789                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
790                             (bbinfo->arr[h] == bblock ||
791                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
792                             BITSET(bblock->dfi);
793                 }
794         }
795 }
pj
1.15
796
pj
1.21
797 static struct basicblock *currbb;
798 static struct interpass *currip;
799
800 /* Helper function for findTemps, Find assignment nodes. */
801 static void
ragge
1.39
802 searchasg(NODE *p)
pj
1.21
803 {
804         struct pvarinfo *pv;
805
806         if (p->n_op != ASSIGN)
807                 return;
808
809         if (p->n_left->n_op != TEMP)
810                 return;
811
812         pv = tmpcalloc(sizeof(struct pvarinfo));
813         pv->next = defsites.arr[p->n_left->n_lval];
814         pv->bb = currbb;
815         pv->top = currip->ip_node;
816         pv->n = p->n_left;
pj
1.22
817         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
818
819         defsites.arr[p->n_left->n_lval] = pv;
820 }
821
822 /* Walk the interpass looking for assignment nodes. */
823 void findTemps(struct interpass *ip)
pj
1.20
824 {
825         if (ip->type != IP_NODE)
826                 return;
827
pj
1.21
828         currip = ip;
829
ragge
1.39
830         walkf(ip->ip_nodesearchasg);
pj
1.20
831 }
832
833 /*
834  * Algorithm 19.6 from Appel.
835  */
836
837 void
838 placePhiFunctions(struct bblockinfo *bbinfo)
839 {
840         struct basicblock *bb;
841         struct interpass *ip;
842         int maxtmpijkl;
843         struct pvarinfo *n;
844         struct cfgnode *cnode;
845         TWORD ntype;
846         NODE *p;
pj
1.22
847         struct pvarinfo *pv;
pj
1.20
848
849         bb = DLIST_NEXT(&bblocksbbelem);
850         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
851         bb = DLIST_PREV(&bblocksbbelem);
852         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
853         defsites.size = maxtmp - defsites.low + 1;
854         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
855
pj
1.21
856         /* Find all defsites */
pj
1.20
857         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
858                 currbb = bb;
pj
1.20
859                 ip = bb->first;
pj
1.22
860                 bb->Aorig = setalloc(defsites.size);
861                 bb->Aphi = setalloc(defsites.size);
862                 
pj
1.20
863
864                 while (ip != bb->last) {
pj
1.21
865                         findTemps(ip);
pj
1.20
866                         ip = DLIST_NEXT(ipqelem);
867                 }
868                 /* Make sure we get the last statement in the bblock */
pj
1.21
869                 findTemps(ip);
pj
1.20
870         }
pj
1.21
871         /* For each variable */
pj
1.20
872         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
873                 /* While W not empty */
pj
1.20
874                 while (defsites.arr[i] != NULL) {
pj
1.21
875                         /* Remove some node n from W */
pj
1.20
876                         n = defsites.arr[i];
877                         defsites.arr[i] = n->next;
pj
1.21
878                         /* For each y in n->bb->df */
pj
1.20
879                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
880                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
881                                         continue;
pj
1.22
882                                 
883                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
884                                         continue;
885
pj
1.20
886                                 ntype = n->n->n_type;
887                                 k = 0;
pj
1.21
888                                 /* Amount of predecessors for y */
pj
1.20
889                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
890                                         k++;
pj
1.21
891                                 /* Construct phi(...) */
pj
1.20
892                                 p = mklnode(TEMPi0ntype);
893                                 for (l = 0l < k-1l++)
894                                         p = mkbinode(PHIp,
895                                             mklnode(TEMPi0ntype), ntype);
896                                 ip = ipnode(mkbinode(ASSIGN,
897                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
898                                 /* Insert phi at top of basic block */
pj
1.20
899                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
900                                 n->bb->first = ip;
pj
1.22
901                                 BITSET(bbinfo->arr[j]->Aphii);
902                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
903                                         pv = tmpalloc(sizeof(struct pvarinfo));
904                                         // XXXpj Ej fullständig information.
905                                         pv->bb = bbinfo->arr[j];
906                                         pv->next = defsites.arr[i]->next;
907                                         defsites.arr[i] = pv;
908                                 }
909                                         
pj
1.20
910
911                         }
912                 }
913         }
914 }
915
pj
1.15
916 /*
917  * Remove unreachable nodes in the CFG.
918  */ 
919
920 void
921 remunreach(void)
922 {
923         struct basicblock *bb, *nbb;
924         struct interpass *next, *ctree;
925
926         bb = DLIST_NEXT(&bblocksbbelem);
927         while (bb != &bblocks) {
928                 nbb = DLIST_NEXT(bbbbelem);
929
930                 /* Code with dfnum 0 is unreachable */
931                 if (bb->dfnum != 0) {
932                         bb = nbb;
933                         continue;
934                 }
935
936                 /* Need the epilogue node for other parts of the
937                    compiler, set its label to 0 and backend will
938                    handle it. */ 
939                 if (bb->first->type == IP_EPILOG) {
940                         bb->first->ip_lbl = 0;
941                         bb = nbb;
942                         continue;
943                 }
944
945                 next = bb->first;
946                 do {
947                         ctree = next;
948                         next = DLIST_NEXT(ctreeqelem);
949                         
950                         if (ctree->type == IP_NODE)
pj
1.16
951                                 tfree(ctree->ip_node);
pj
1.15
952                         DLIST_REMOVE(ctreeqelem);
953                 } while (ctree != bb->last);
954                         
955                 DLIST_REMOVE(bbbbelem);
956                 bb = nbb;
957         }
958 }
ragge
1.19
959
960 void
961 printip(struct interpass *pole)
962 {
963         static char *foo[] = {
964            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
965         struct interpass *ip;
ragge
1.38
966         struct interpass_prolog *ipp, *epp;
ragge
1.19
967
968         DLIST_FOREACH(ippoleqelem) {
ragge
1.43
969                 if (ip->type > MAXIP)
ragge
1.19
970                         printf("IP(%d) (%p): "ip->typeip);
971                 else
972                         printf("%s (%p): "foo[ip->type], ip);
973                 switch (ip->type) {
974                 case IP_NODEprintf("\n");
975                         fwalk(ip->ip_nodee2print0); break;
976                 case IP_PROLOG:
ragge
1.38
977                         ipp = (struct interpass_prolog *)ip;
978                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
979                             ipp->ipp_nameipp->ipp_vis ? "(local)" : "",
980                             ipp->ipp_regsipp->ipp_autosipp->ip_tmpnum,
981                             ipp->ip_lblnum);
982                         break;
983                 case IP_EPILOG:
984                         epp = (struct interpass_prolog *)ip;
985                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
986                             epp->ipp_nameepp->ipp_vis ? "(local)" : "",
987                             epp->ipp_regsepp->ipp_autosepp->ip_tmpnum,
988                             epp->ip_lblnum);
989                         break;
ragge
1.19
990                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
ragge
1.38
991                 case IP_DEFNAMprintf("\n"); break;
992                 case IP_ASMprintf("%s\n"ip->ip_asm); break;
993                 default:
994                         break;
ragge
1.19
995                 }
996         }
997 }
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-09-15 04:04 +0200