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
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. */
43 static cdep_info *cdep_data;
45 ir_node *(get_cdep_node)(const ir_cdep *cdep)
47 return _get_cdep_node(cdep);
50 ir_cdep *(get_cdep_next)(const ir_cdep *cdep)
52 return _get_cdep_next(cdep);
55 /* Return a list of all control dependences of a block. */
56 ir_cdep *find_cdep(const ir_node *block)
58 assert(is_Block(block));
59 return (ir_cdep*) pmap_get(cdep_data->cdep_map, block);
62 void exchange_cdep(ir_node *old, const ir_node *nw)
64 ir_cdep *cdep = find_cdep(nw);
65 assert(is_Block(old));
66 pmap_insert(cdep_data->cdep_map, old, cdep);
70 * Adds a control dependence from node to dep_on.
72 static void add_cdep(ir_node *node, ir_node *dep_on)
74 ir_cdep *dep = find_cdep(node);
76 assert(is_Block(dep_on));
78 ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
80 newdep->node = dep_on;
82 pmap_insert(cdep_data->cdep_map, node, newdep);
87 if (get_cdep_node(dep) == dep_on) return;
88 if (dep->next == NULL) break;
91 newdep = OALLOC(&cdep_data->obst, ir_cdep);
92 newdep->node = dep_on;
98 typedef struct cdep_env {
104 * Pre-block-walker: calculate the control dependence
106 static void cdep_pre(ir_node *node, void *ctx)
108 cdep_env *env = (cdep_env*) ctx;
112 * start and end block have no control dependency
114 if (node == env->start_block) return;
115 if (node == env->end_block) return;
117 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
118 ir_node *pred = get_Block_cfgpred_block(node, i);
122 if (is_Bad(pred)) continue;
124 pdom = get_Block_ipostdom(pred);
125 for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
126 assert(!is_Bad(pdom));
127 add_cdep(dependee, pred);
134 * A block edge hook: add all cdep edges of block.
136 static int cdep_edge_hook(FILE *F, ir_node *block)
140 for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
143 "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
144 "linestyle:dashed color:gold}\n",
145 get_irn_node_nr(block), get_irn_node_nr(cd->node)
152 void compute_cdep(ir_graph *irg)
158 cdep_data = XMALLOC(cdep_info);
159 obstack_init(&cdep_data->obst);
161 cdep_data->cdep_map = pmap_create();
163 assure_postdoms(irg);
165 /* we must temporary change the post dominator relation:
166 the ipdom of the startblock is the end block.
167 Firm does NOT add the phantom edge from Start to End.
169 env.start_block = get_irg_start_block(irg);
170 env.end_block = get_irg_end_block(irg);
172 rem = get_Block_ipostdom(env.start_block);
173 set_Block_ipostdom(env.start_block, env.end_block);
175 irg_block_walk_graph(irg, cdep_pre, NULL, &env);
178 set_dump_block_edge_hook(cdep_edge_hook);
179 dump_ir_block_graph(irg, "_cdep");
180 set_dump_block_edge_hook(NULL);
182 (void) cdep_edge_hook;
185 /* restore the post dominator relation */
186 set_Block_ipostdom(env.start_block, rem);
189 void free_cdep(ir_graph *irg)
192 if (cdep_data != NULL) {
193 pmap_destroy(cdep_data->cdep_map);
194 obstack_free(&cdep_data->obst, NULL);
200 int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
204 for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
205 if (get_cdep_node(dep) == candidate) return 1;
210 ir_node *get_unique_cdep(const ir_node *block)
212 ir_cdep *cdep = find_cdep(block);
214 return cdep != NULL && cdep->next == NULL ? get_cdep_node(cdep) : NULL;
217 int has_multiple_cdep(const ir_node *block)
219 ir_cdep *cdep = find_cdep(block);
221 return cdep != NULL && cdep->next != NULL;