Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20071001152753

Diff

Diff from 1.46 to:

Annotations

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

Annotated File View

ragge
1.46
1 /*      $Id: optim2.c,v 1.46 2007/10/01 15:27:53 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);
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) && 
600                     (bb->last->ip_node->n_op == GOTO)) {
601                         if (bb->last->ip_node->n_left->n_lval - labinfo->low > 
602                             labinfo->size) {
603                                 comperr("Label out of range: %d, base %d"
604                                         bb->last->ip_node->n_left->n_lval
605                                         labinfo->low);
606                         }
607                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_left->n_lval - labinfo->low];
ragge
1.11
608                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
609                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
610                         continue;
611                 }
612                 if ((bb->last->type == IP_NODE) && 
613                     (bb->last->ip_node->n_op == CBRANCH)) {
614                         if (bb->last->ip_node->n_right->n_lval - labinfo->low > 
615                             labinfo->size
616                                 comperr("Label out of range: %d"
617                                         bb->last->ip_node->n_left->n_lval);
618                         
619                         cnode->bblock = labinfo->arr[bb->last->ip_node->n_right->n_lval - labinfo->low];
ragge
1.11
620                         SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
621                         SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
622                         cnode = tmpalloc(sizeof(struct cfgnode));
623                         pnode = tmpalloc(sizeof(struct cfgnode));
624                         pnode->bblock = bb;
625                 }
626
ragge
1.11
627                 cnode->bblock = DLIST_NEXT(bbbbelem);
628                 SLIST_INSERT_LAST(&cnode->bblock->parentspnodecfgelem);
629                 SLIST_INSERT_LAST(&bb->childrencnodecfgelem);
pj
1.4
630         }
631 }
pj
1.6
632
pj
1.8
633 void
634 cfg_dfs(struct basicblock *bbunsigned int parentstruct bblockinfo *bbinfo)
635 {
636         struct cfgnode *cnode;
637         
638         if (bb->dfnum != 0)
639                 return;
640
641         bb->dfnum = ++dfsnum;
642         bb->dfparent = parent;
643         bbinfo->arr[bb->dfnum] = bb;
ragge
1.11
644         SLIST_FOREACH(cnode, &bb->childrencfgelem) {
pj
1.8
645                 cfg_dfs(cnode->bblockbb->dfnumbbinfo);
646         }
pj
1.13
647         /* Don't bring in unreachable nodes in the future */
pj
1.20
648         bbinfo->size = dfsnum + 1;
pj
1.8
649 }
650
ragge
1.11
651 static bittype *
652 setalloc(int nelem)
653 {
654         bittype *b;
655         int sz = (nelem+NUMBITS-1)/NUMBITS;
656
657         b = tmpalloc(sz * sizeof(bittype));
658         memset(b0sz * sizeof(bittype));
659         return b;
660 }
661
pj
1.8
662 /*
663  * Algorithm 19.9, pp 414 from Appel.
664  */
665
666 void
667 dominators(struct bblockinfo *bbinfo)
668 {
669         struct cfgnode *cnode;
670         struct basicblock *bb, *y, *v;
671         struct basicblock *s, *sprime, *p;
pj
1.10
672         int hi;
pj
1.8
673
ragge
1.11
674         DLIST_FOREACH(bb, &bblocksbbelem) {
675                 bb->bucket = setalloc(bbinfo->size);
676                 bb->df = setalloc(bbinfo->size);
677                 bb->dfchildren = setalloc(bbinfo->size);
pj
1.8
678         }
679
680         dfsnum = 0;
ragge
1.11
681         cfg_dfs(DLIST_NEXT(&bblocksbbelem), 0bbinfo);
pj
1.8
682
ragge
1.30
683         if (b2debug) {
684                 struct basicblock *bbb;
685                 struct cfgnode *ccnode;
686
687                 DLIST_FOREACH(bbb, &bblocksbbelem) {
688                         printf("Basic block %d, parents: "bbb->dfnum);
689                         SLIST_FOREACH(ccnode, &bbb->parentscfgelem) {
690                                 printf("%d, "ccnode->bblock->dfnum);
691                         }
692                         printf("\nChildren: ");
693                         SLIST_FOREACH(ccnode, &bbb->childrencfgelem) {
694                                 printf("%d, "ccnode->bblock->dfnum);
695                         }
696                         printf("\n");
pj
1.20
697                 }
698         }
699
pj
1.10
700         for(h = bbinfo->size - 1h > 1h--) {
701                 bb = bbinfo->arr[h];
pj
1.8
702                 p = s = bbinfo->arr[bb->dfparent];
ragge
1.11
703                 SLIST_FOREACH(cnode, &bb->parentscfgelem) {
pj
1.8
704                         if (cnode->bblock->dfnum <= bb->dfnum
705                                 sprime = cnode->bblock;
pj
1.10
706                         else 
707                                 sprime = bbinfo->arr[ancestorwithlowestsemi
708                                               (cnode->bblockbbinfo)->semi];
pj
1.8
709                         if (sprime->dfnum < s->dfnum)
710                                 s = sprime;
711                 }
712                 bb->semi = s->dfnum;
pj
1.10
713                 BITSET(s->bucketbb->dfnum);
pj
1.8
714                 link(pbb);
715                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
716                         if(TESTBIT(p->bucketi)) {
pj
1.8
717                                 v = bbinfo->arr[i];
pj
1.10
718                                 y = ancestorwithlowestsemi(vbbinfo);
719                                 if (y->semi == v->semi
pj
1.8
720                                         v->idom = p->dfnum;
721                                 else
722                                         v->samedom = y->dfnum;
723                         }
724                 }
725                 memset(p->bucket0, (bbinfo->size + 7)/8);
726         }
ragge
1.30
727
728         if (b2debug) {
729                 printf("Num\tSemi\tAncest\tidom\n");
730                 DLIST_FOREACH(bb, &bblocksbbelem) {
731                         printf("%d\t%d\t%d\t%d\n"bb->dfnumbb->semi,
732                             bb->ancestorbb->idom);
733                 }
pj
1.10
734         }
ragge
1.30
735
pj
1.10
736         for(h = 2h < bbinfo->sizeh++) {
737                 bb = bbinfo->arr[h];
738                 if (bb->samedom != 0) {
pj
1.8
739                         bb->idom = bbinfo->arr[bb->samedom]->idom;
pj
1.10
740                 }
741         }
ragge
1.11
742         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.10
743                 if (bb->idom != 0 && bb->idom != bb->dfnum) {
ragge
1.30
744                         BDEBUG(("Setting child %d of %d\n",
745                             bb->dfnumbbinfo->arr[bb->idom]->dfnum));
pj
1.10
746                         BITSET(bbinfo->arr[bb->idom]->dfchildrenbb->dfnum);
747                 }
pj
1.8
748         }
749 }
750
751
752 struct basicblock *
753 ancestorwithlowestsemi(struct basicblock *bblockstruct bblockinfo *bbinfo)
754 {
755         struct basicblock *u = bblock;
756         struct basicblock *v = bblock;
757
758         while (v->ancestor != 0) {
759                 if (bbinfo->arr[v->semi]->dfnum < 
pj
1.10
760                     bbinfo->arr[u->semi]->dfnum
pj
1.8
761                         u = v;
762                 v = bbinfo->arr[v->ancestor];
763         }
764         return u;
765 }
766
767 void
768 link(struct basicblock *parentstruct basicblock *child)
769 {
770         child->ancestor = parent->dfnum;
771 }
772
773 void
774 computeDF(struct basicblock *bblockstruct bblockinfo *bbinfo)
775 {
776         struct cfgnode *cnode;
pj
1.10
777         int hi;
pj
1.8
778         
ragge
1.11
779         SLIST_FOREACH(cnode, &bblock->childrencfgelem) {
pj
1.8
780                 if (cnode->bblock->idom != bblock->dfnum)
781                         BITSET(bblock->dfcnode->bblock->dfnum);
782         }
pj
1.10
783         for (h = 1h < bbinfo->sizeh++) {
784                 if (!TESTBIT(bblock->dfchildrenh))
785                         continue;
786                 computeDF(bbinfo->arr[h], bbinfo);
pj
1.8
787                 for (i = 1i < bbinfo->sizei++) {
pj
1.10
788                         if (TESTBIT(bbinfo->arr[h]->dfi) && 
789                             (bbinfo->arr[h] == bblock ||
790                              (bblock->idom != bbinfo->arr[h]->dfnum))) 
pj
1.8
791                             BITSET(bblock->dfi);
792                 }
793         }
794 }
pj
1.15
795
pj
1.21
796 static struct basicblock *currbb;
797 static struct interpass *currip;
798
799 /* Helper function for findTemps, Find assignment nodes. */
800 static void
ragge
1.39
801 searchasg(NODE *p)
pj
1.21
802 {
803         struct pvarinfo *pv;
804
805         if (p->n_op != ASSIGN)
806                 return;
807
808         if (p->n_left->n_op != TEMP)
809                 return;
810
811         pv = tmpcalloc(sizeof(struct pvarinfo));
812         pv->next = defsites.arr[p->n_left->n_lval];
813         pv->bb = currbb;
814         pv->top = currip->ip_node;
815         pv->n = p->n_left;
pj
1.22
816         BITSET(currbb->Aorigp->n_left->n_lval);
pj
1.21
817
818         defsites.arr[p->n_left->n_lval] = pv;
819 }
820
821 /* Walk the interpass looking for assignment nodes. */
822 void findTemps(struct interpass *ip)
pj
1.20
823 {
824         if (ip->type != IP_NODE)
825                 return;
826
pj
1.21
827         currip = ip;
828
ragge
1.39
829         walkf(ip->ip_nodesearchasg);
pj
1.20
830 }
831
832 /*
833  * Algorithm 19.6 from Appel.
834  */
835
836 void
837 placePhiFunctions(struct bblockinfo *bbinfo)
838 {
839         struct basicblock *bb;
840         struct interpass *ip;
841         int maxtmpijkl;
842         struct pvarinfo *n;
843         struct cfgnode *cnode;
844         TWORD ntype;
845         NODE *p;
pj
1.22
846         struct pvarinfo *pv;
pj
1.20
847
848         bb = DLIST_NEXT(&bblocksbbelem);
849         defsites.low = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
850         bb = DLIST_PREV(&bblocksbbelem);
851         maxtmp = ((struct interpass_prolog *)bb->first)->ip_tmpnum;
852         defsites.size = maxtmp - defsites.low + 1;
853         defsites.arr = tmpcalloc(defsites.size*sizeof(struct pvarinfo *));
854
pj
1.21
855         /* Find all defsites */
pj
1.20
856         DLIST_FOREACH(bb, &bblocksbbelem) {
pj
1.21
857                 currbb = bb;
pj
1.20
858                 ip = bb->first;
pj
1.22
859                 bb->Aorig = setalloc(defsites.size);
860                 bb->Aphi = setalloc(defsites.size);
861                 
pj
1.20
862
863                 while (ip != bb->last) {
pj
1.21
864                         findTemps(ip);
pj
1.20
865                         ip = DLIST_NEXT(ipqelem);
866                 }
867                 /* Make sure we get the last statement in the bblock */
pj
1.21
868                 findTemps(ip);
pj
1.20
869         }
pj
1.21
870         /* For each variable */
pj
1.20
871         for (i = defsites.lowi < defsites.sizei++) {
pj
1.21
872                 /* While W not empty */
pj
1.20
873                 while (defsites.arr[i] != NULL) {
pj
1.21
874                         /* Remove some node n from W */
pj
1.20
875                         n = defsites.arr[i];
876                         defsites.arr[i] = n->next;
pj
1.21
877                         /* For each y in n->bb->df */
pj
1.20
878                         for (j = 0j < bbinfo->sizej++) {
pj
1.22
879                                 if (!TESTBIT(n->bb->dfj))
pj
1.20
880                                         continue;
pj
1.22
881                                 
882                                 if (TESTBIT(bbinfo->arr[j]->Aphii))
883                                         continue;
884
pj
1.20
885                                 ntype = n->n->n_type;
886                                 k = 0;
pj
1.21
887                                 /* Amount of predecessors for y */
pj
1.20
888                                 SLIST_FOREACH(cnode, &n->bb->parentscfgelem
889                                         k++;
pj
1.21
890                                 /* Construct phi(...) */
pj
1.20
891                                 p = mklnode(TEMPi0ntype);
892                                 for (l = 0l < k-1l++)
893                                         p = mkbinode(PHIp,
894                                             mklnode(TEMPi0ntype), ntype);
895                                 ip = ipnode(mkbinode(ASSIGN,
896                                     mklnode(TEMPi0ntype), pntype));
pj
1.21
897                                 /* Insert phi at top of basic block */
pj
1.20
898                                 DLIST_INSERT_BEFORE(((struct interpass*)&n->bb->first), ipqelem);
899                                 n->bb->first = ip;
pj
1.22
900                                 BITSET(bbinfo->arr[j]->Aphii);
901                                 if (!TESTBIT(bbinfo->arr[j]->Aorigi)) {
902                                         pv = tmpalloc(sizeof(struct pvarinfo));
903                                         // XXXpj Ej fullständig information.
904                                         pv->bb = bbinfo->arr[j];
905                                         pv->next = defsites.arr[i]->next;
906                                         defsites.arr[i] = pv;
907                                 }
908                                         
pj
1.20
909
910                         }
911                 }
912         }
913 }
914
pj
1.15
915 /*
916  * Remove unreachable nodes in the CFG.
917  */ 
918
919 void
920 remunreach(void)
921 {
922         struct basicblock *bb, *nbb;
923         struct interpass *next, *ctree;
924
925         bb = DLIST_NEXT(&bblocksbbelem);
926         while (bb != &bblocks) {
927                 nbb = DLIST_NEXT(bbbbelem);
928
929                 /* Code with dfnum 0 is unreachable */
930                 if (bb->dfnum != 0) {
931                         bb = nbb;
932                         continue;
933                 }
934
935                 /* Need the epilogue node for other parts of the
936                    compiler, set its label to 0 and backend will
937                    handle it. */ 
938                 if (bb->first->type == IP_EPILOG) {
939                         bb->first->ip_lbl = 0;
940                         bb = nbb;
941                         continue;
942                 }
943
944                 next = bb->first;
945                 do {
946                         ctree = next;
947                         next = DLIST_NEXT(ctreeqelem);
948                         
949                         if (ctree->type == IP_NODE)
pj
1.16
950                                 tfree(ctree->ip_node);
pj
1.15
951                         DLIST_REMOVE(ctreeqelem);
952                 } while (ctree != bb->last);
953                         
954                 DLIST_REMOVE(bbbbelem);
955                 bb = nbb;
956         }
957 }
ragge
1.19
958
959 void
960 printip(struct interpass *pole)
961 {
962         static char *foo[] = {
963            0"NODE""PROLOG""STKOFF""EPILOG""DEFLAB""DEFNAM""ASM" };
964         struct interpass *ip;
ragge
1.38
965         struct interpass_prolog *ipp, *epp;
ragge
1.19
966
967         DLIST_FOREACH(ippoleqelem) {
ragge
1.43
968                 if (ip->type > MAXIP)
ragge
1.19
969                         printf("IP(%d) (%p): "ip->typeip);
970                 else
971                         printf("%s (%p): "foo[ip->type], ip);
972                 switch (ip->type) {
973                 case IP_NODEprintf("\n");
974                         fwalk(ip->ip_nodee2print0); break;
975                 case IP_PROLOG:
ragge
1.38
976                         ipp = (struct interpass_prolog *)ip;
977                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
978                             ipp->ipp_nameipp->ipp_vis ? "(local)" : "",
979                             ipp->ipp_regsipp->ipp_autosipp->ip_tmpnum,
980                             ipp->ip_lblnum);
981                         break;
982                 case IP_EPILOG:
983                         epp = (struct interpass_prolog *)ip;
984                         printf("%s %s regs %x autos %d mintemp %d minlbl %d\n",
985                             epp->ipp_nameepp->ipp_vis ? "(local)" : "",
986                             epp->ipp_regsepp->ipp_autosepp->ip_tmpnum,
987                             epp->ip_lblnum);
988                         break;
ragge
1.19
989                 case IP_DEFLABprintf(LABFMT "\n"ip->ip_lbl); break;
ragge
1.38
990                 case IP_DEFNAMprintf("\n"); break;
991                 case IP_ASMprintf("%s\n"ip->ip_asm); break;
992                 default:
993                         break;
ragge
1.19
994                 }
995         }
996 }
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-23 08:23 +0200