remove opt_manage, provide assure_irg_properties
[libfirm] / ir / ana / cdep.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Implementation of cdep
23  * @author  Christoph Mallon
24  */
25 #include <assert.h>
26 #include <stdlib.h>
27 #include "irdom_t.h"
28 #include "irgraph.h"
29 #include "irgwalk.h"
30 #include "irnode.h"
31 #include "pmap.h"
32 #include "obst.h"
33 #include "xmalloc.h"
34 #include "cdep_t.h"
35 #include "irprintf.h"
36 #include "irdump.h"
37
38 typedef struct cdep_info {
39         pmap   *cdep_map;     /**< A map to find the list of all control dependence nodes for a block. */
40         struct obstack obst;  /**< An obstack where all cdep data lives on. */
41 } cdep_info;
42
43 static cdep_info *cdep_data;
44
45 ir_node *(get_cdep_node)(const ir_cdep *cdep)
46 {
47         return _get_cdep_node(cdep);
48 }
49
50 ir_cdep *(get_cdep_next)(const ir_cdep *cdep)
51 {
52         return _get_cdep_next(cdep);
53 }
54
55 ir_cdep *find_cdep(const ir_node *block)
56 {
57         assert(is_Block(block));
58         return (ir_cdep*) pmap_get(cdep_data->cdep_map, block);
59 }
60
61 void exchange_cdep(ir_node *old, const ir_node *nw)
62 {
63         ir_cdep *cdep = find_cdep(nw);
64         assert(is_Block(old));
65         pmap_insert(cdep_data->cdep_map, old, cdep);
66 }
67
68 /**
69  * Adds a control dependence from node to dep_on.
70  */
71 static void add_cdep(ir_node *node, ir_node *dep_on)
72 {
73         ir_cdep *dep = find_cdep(node);
74
75         assert(is_Block(dep_on));
76         if (dep == NULL) {
77                 ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
78
79                 newdep->node = dep_on;
80                 newdep->next = NULL;
81                 pmap_insert(cdep_data->cdep_map, node, newdep);
82         } else {
83                 ir_cdep *newdep;
84
85                 for (;;) {
86                         if (get_cdep_node(dep) == dep_on) return;
87                         if (dep->next == NULL) break;
88                         dep = dep->next;
89                 }
90                 newdep = OALLOC(&cdep_data->obst, ir_cdep);
91                 newdep->node = dep_on;
92                 newdep->next = NULL;
93                 dep->next = newdep;
94         }
95 }
96
97 typedef struct cdep_env {
98         ir_node *start_block;
99         ir_node *end_block;
100 } cdep_env;
101
102 /**
103  * Pre-block-walker: calculate the control dependence
104  */
105 static void cdep_pre(ir_node *node, void *ctx)
106 {
107         cdep_env *env = (cdep_env*) ctx;
108         int i;
109
110         /* special case:
111          * start and end block have no control dependency
112          */
113         if (node == env->start_block) return;
114         if (node == env->end_block) return;
115
116         for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
117                 ir_node *pred = get_Block_cfgpred_block(node, i);
118                 ir_node *pdom;
119                 ir_node *dependee;
120
121                 if (is_Bad(pred)) continue;
122
123                 pdom = get_Block_ipostdom(pred);
124                 for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
125                         assert(!is_Bad(pdom));
126                         add_cdep(dependee, pred);
127                 }
128         }
129 }
130
131
132 /**
133  * A block edge hook: add all cdep edges of block.
134  */
135 static int cdep_edge_hook(FILE *F, ir_node *block)
136 {
137         ir_cdep *cd;
138
139         for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
140                 fprintf(
141                         F,
142                         "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
143                         "linestyle:dashed color:gold}\n",
144                         get_irn_node_nr(block), get_irn_node_nr(cd->node)
145                 );
146         }
147
148         return 0;
149 }
150
151 void compute_cdep(ir_graph *irg)
152 {
153         ir_node *rem;
154         cdep_env env;
155
156         free_cdep(irg);
157         cdep_data = XMALLOC(cdep_info);
158         obstack_init(&cdep_data->obst);
159
160         cdep_data->cdep_map = pmap_create();
161
162         assure_postdoms(irg);
163
164         /* we must temporary change the post dominator relation:
165            the ipdom of the startblock is the end block.
166            Firm does NOT add the phantom edge from Start to End.
167          */
168         env.start_block = get_irg_start_block(irg);
169         env.end_block   = get_irg_end_block(irg);
170
171         rem = get_Block_ipostdom(env.start_block);
172         set_Block_ipostdom(env.start_block, env.end_block);
173
174         irg_block_walk_graph(irg, cdep_pre, NULL, &env);
175
176 #if 0
177         set_dump_block_edge_hook(cdep_edge_hook);
178         dump_ir_block_graph(irg, "_cdep");
179         set_dump_block_edge_hook(NULL);
180 #else
181         (void) cdep_edge_hook;
182 #endif
183
184         /* restore the post dominator relation */
185         set_Block_ipostdom(env.start_block, rem);
186 }
187
188 void free_cdep(ir_graph *irg)
189 {
190         (void) irg;
191         if (cdep_data != NULL) {
192                 pmap_destroy(cdep_data->cdep_map);
193                 obstack_free(&cdep_data->obst, NULL);
194                 xfree(cdep_data);
195                 cdep_data = NULL;
196         }
197 }
198
199 int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
200 {
201         const ir_cdep *dep;
202
203         for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
204                 if (get_cdep_node(dep) == candidate) return 1;
205         }
206         return 0;
207 }
208
209 ir_node *get_unique_cdep(const ir_node *block)
210 {
211         ir_cdep *cdep = find_cdep(block);
212
213         return cdep != NULL && cdep->next == NULL ? get_cdep_node(cdep) : NULL;
214 }
215
216 int has_multiple_cdep(const ir_node *block)
217 {
218         ir_cdep *cdep = find_cdep(block);
219
220         return cdep != NULL && cdep->next != NULL;
221 }