2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Implementation of cdep
23 * @author Christoph Mallon
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. */
44 static cdep_info *cdep_data;
46 /* Return a list of all control dependences of a block. */
47 ir_cdep *find_cdep(const ir_node *block)
49 return pmap_get(cdep_data->cdep_map, block);
52 /* Replace the control dependence info of old by the info of nw. */
53 void exchange_cdep(ir_node *old, const ir_node *nw)
55 ir_cdep *cdep = find_cdep(nw);
56 pmap_insert(cdep_data->cdep_map, old, cdep);
60 * Adds a control dependence from node to dep_on.
62 static void add_cdep(ir_node *node, ir_node *dep_on)
64 ir_cdep *dep = find_cdep(node);
66 ir_fprintf(stderr, "Adding cdep of %+F on %+F\n", node, dep_on);
70 ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
72 newdep->node = dep_on;
74 pmap_insert(cdep_data->cdep_map, node, newdep);
79 if (dep->node == dep_on) return;
80 if (dep->next == NULL) break;
83 newdep = OALLOC(&cdep_data->obst, ir_cdep);
84 newdep->node = dep_on;
90 typedef struct cdep_env {
96 * Pre-block-walker: calculate the control dependence
98 static void cdep_pre(ir_node *node, void *ctx)
104 * start and end block have no control dependency
106 if (node == env->start_block) return;
107 if (node == env->end_block) return;
109 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
110 ir_node *pred = get_Block_cfgpred_block(node, i);
114 if (is_Bad(pred)) continue;
116 pdom = get_Block_ipostdom(pred);
117 for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
118 assert(!is_Bad(pdom));
119 add_cdep(dependee, pred);
126 * A block edge hook: add all cdep edges of block.
128 static int cdep_edge_hook(FILE *F, ir_node *block)
133 ir_node *pdom = get_Block_ipostdom(block);
137 "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" color:gold}\n",
138 get_irn_node_nr(pdom), get_irn_node_nr(block)
143 for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
146 "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
147 "linestyle:dashed color:gold}\n",
148 get_irn_node_nr(block), get_irn_node_nr(cd->node)
155 /* Compute the control dependence graph for a graph. */
156 void compute_cdep(ir_graph *irg)
162 cdep_data = XMALLOC(cdep_info);
163 obstack_init(&cdep_data->obst);
165 cdep_data->cdep_map = pmap_create();
167 assure_postdoms(irg);
169 /* we must temporary change the post dominator relation:
170 the ipdom of the startblock is the end block.
171 Firm does NOT add the phantom edge from Start to End.
173 env.start_block = get_irg_start_block(irg);
174 env.end_block = get_irg_end_block(irg);
176 rem = get_Block_ipostdom(env.start_block);
177 set_Block_ipostdom(env.start_block, env.end_block);
179 irg_block_walk_graph(irg, cdep_pre, NULL, &env);
182 set_dump_block_edge_hook(cdep_edge_hook);
183 dump_ir_block_graph(irg, "_cdep");
184 set_dump_block_edge_hook(NULL);
186 (void) cdep_edge_hook;
189 /* restore the post dominator relation */
190 set_Block_ipostdom(env.start_block, rem);
193 /* Free the control dependence info. */
194 void free_cdep(ir_graph *irg)
197 if (cdep_data != NULL) {
198 pmap_destroy(cdep_data->cdep_map);
199 obstack_free(&cdep_data->obst, NULL);
205 /* Check whether dependee is (directly) control dependent on candidate. */
206 int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
210 for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
211 if (dep->node == candidate) return 1;
216 /* Check whether dependee is (possible iterated) control dependent on candidate. */
217 int is_iterated_cdep_on(ir_node *dependee, ir_node *candidate)
221 while ((dep = find_cdep(dependee)) != NULL) {
222 if (dep->next != NULL) return 0;
223 if (dep->node == candidate) return 1;
224 dependee = dep->node;
229 /* If block is control dependent on exactly one node, return this node, else NULL. */
230 ir_node *get_unique_cdep(const ir_node *block)
232 ir_cdep *cdep = find_cdep(block);
234 return cdep != NULL && cdep->next == NULL ? cdep->node : NULL;
237 /* Check if the given block is control dependent of more than one node. */
238 int has_multiple_cdep(const ir_node *block)
240 ir_cdep *cdep = find_cdep(block);
242 return cdep != NULL && cdep->next != NULL;