Good day and welcome to the FIRM XMALLOC*() macros. These macros are provided for...
[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  * @version $Id$
25  */
26 #include <assert.h>
27 #include <stdlib.h>
28 #include "irdom.h"
29 #include "irgraph.h"
30 #include "irgwalk.h"
31 #include "irnode.h"
32 #include "pmap.h"
33 #include "obst.h"
34 #include "xmalloc.h"
35 #include "cdep.h"
36 #include "irprintf.h"
37 #include "irdump.h"
38
39 typedef struct cdep_info {
40         pmap   *cdep_map;     /**< A map to find the list of all control dependence nodes for a block. */
41         struct obstack obst;  /**< An obstack where all cdep data lives on. */
42 } cdep_info;
43
44 static cdep_info *cdep_data;
45
46 /* Return a list of all control dependences of a block. */
47 ir_cdep *find_cdep(const ir_node *block) {
48         return pmap_get(cdep_data->cdep_map, (void *)block);
49 }
50
51 /* Replace the control dependence info of old by the info of nw. */
52 void exchange_cdep(ir_node *old, const ir_node *nw) {
53         ir_cdep *cdep = find_cdep(nw);
54         pmap_insert(cdep_data->cdep_map, old, cdep);
55 }
56
57 /**
58  * Adds a control dependence from node to dep_on.
59  */
60 static void add_cdep(ir_node *node, ir_node *dep_on) {
61         ir_cdep *dep = find_cdep(node);
62 #if 0
63         ir_fprintf(stderr, "Adding cdep of %+F on %+F\n", node, dep_on);
64 #endif
65
66         if (dep == NULL) {
67                 ir_cdep *newdep = obstack_alloc(&cdep_data->obst, sizeof(*newdep));
68
69                 newdep->node = dep_on;
70                 newdep->next = NULL;
71                 pmap_insert(cdep_data->cdep_map, node, newdep);
72         } else {
73                 ir_cdep *newdep;
74
75                 for (;;) {
76                         if (dep->node == dep_on) return;
77                         if (dep->next == NULL) break;
78                         dep = dep->next;
79                 }
80                 newdep = obstack_alloc(&cdep_data->obst, sizeof(*newdep));
81                 newdep->node = dep_on;
82                 newdep->next = NULL;
83                 dep->next = newdep;
84         }
85 }
86
87 typedef struct cdep_env {
88         ir_node *start_block;
89         ir_node *end_block;
90 } cdep_env;
91
92 /**
93  * Pre-block-walker: calculate the control dependence
94  */
95 static void cdep_pre(ir_node *node, void *ctx) {
96         cdep_env *env = ctx;
97         unsigned int n;
98         unsigned int i;
99
100         /* special case:
101          * start and end block have no control dependency
102          */
103         if (node == env->start_block) return;
104         if (node == env->end_block) return;
105
106         n = get_Block_n_cfgpreds(node);
107         for (i = 0; i < n; i++) {
108                 ir_node *pred = get_Block_cfgpred_block(node, i);
109                 ir_node *pdom;
110                 ir_node *dependee;
111
112                 if (is_Bad(pred)) continue;
113
114                 pdom = get_Block_ipostdom(pred);
115                 for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
116                         assert(!is_Bad(pdom));
117                         add_cdep(dependee, pred);
118                 }
119         }
120 }
121
122
123 /**
124  * A block edge hook: add all cdep edges of block.
125  */
126 static int cdep_edge_hook(FILE *F, ir_node *block)
127 {
128         ir_cdep *cd;
129
130 #if 0
131         ir_node *pdom = get_Block_ipostdom(block);
132         if (pdom != NULL) {
133                 fprintf(
134                         F,
135                         "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" color:gold}\n",
136                         get_irn_node_nr(pdom), get_irn_node_nr(block)
137                 );
138         }
139 #endif
140
141         for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
142                 fprintf(
143                         F,
144                         "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
145                         "linestyle:dashed color:gold}\n",
146                         get_irn_node_nr(block), get_irn_node_nr(cd->node)
147                 );
148         }
149
150         return 0;
151 }
152
153 /* Compute the control dependence graph for a graph. */
154 void compute_cdep(ir_graph *irg) {
155         ir_node *start_block, *rem;
156         cdep_env env;
157
158         free_cdep(irg);
159         cdep_data = XMALLOC(cdep_info);
160         obstack_init(&cdep_data->obst);
161
162         cdep_data->cdep_map = pmap_create();
163
164         assure_postdoms(irg);
165
166         /* we must temporary change the post dominator relation:
167            the ipdom of the startblock is the end block.
168            Firm does NOT add the phantom edge from Start to End.
169          */
170         start_block = get_irg_start_block(irg);
171         rem = get_Block_ipostdom(start_block);
172         set_Block_ipostdom(start_block, get_irg_end_block(irg));
173
174         env.start_block = get_irg_start_block(irg);
175         env.end_block   = get_irg_end_block(irg);
176         irg_block_walk_graph(irg, cdep_pre, NULL, &env);
177
178 #if 0
179         set_dump_block_edge_hook(cdep_edge_hook);
180         dump_ir_block_graph(irg, "_cdep");
181         set_dump_block_edge_hook(NULL);
182 #else
183         (void) cdep_edge_hook;
184 #endif
185
186         /* restore the post dominator relation */
187         set_Block_ipostdom(start_block, rem);
188 }
189
190 /* Free the control dependence info. */
191 void free_cdep(ir_graph *irg) {
192         (void) irg;
193         if (cdep_data != NULL) {
194                 pmap_destroy(cdep_data->cdep_map);
195                 obstack_free(&cdep_data->obst, NULL);
196                 xfree(cdep_data);
197                 cdep_data = NULL;
198         }
199 }
200
201 /* Check whether dependee is (directly) control dependent on candidate. */
202 int is_cdep_on(const ir_node *dependee, const ir_node *candidate) {
203         const ir_cdep *dep;
204
205         for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
206                 if (dep->node == candidate) return 1;
207         }
208         return 0;
209 }
210
211 /* Check whether dependee is (possible iterated) control dependent on candidate. */
212 int is_iterated_cdep_on(ir_node *dependee, ir_node *candidate) {
213         const ir_cdep *dep;
214
215         while ((dep = find_cdep(dependee)) != NULL) {
216                 if (dep->next != NULL) return 0;
217                 if (dep->node == candidate) return 1;
218                 dependee = dep->node;
219         }
220         return 0;
221 }
222
223 /* If block is control dependent on exactly one node, return this node, else NULL. */
224 ir_node *get_unique_cdep(const ir_node *block) {
225         ir_cdep *cdep = find_cdep(block);
226
227         return cdep != NULL && cdep->next == NULL ? cdep->node : NULL;
228 }
229
230 /* Check if the given block is control dependent of more than one node. */
231 int has_multiple_cdep(const ir_node *block) {
232         ir_cdep *cdep = find_cdep(block);
233
234         return cdep != NULL && cdep->next != NULL;
235 }