2 * Copyright (C) 1995-2007 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
36 static pmap *cdep_map;
38 cdep *find_cdep(const ir_node *block)
40 return pmap_get(cdep_map, (void *)block);
44 void exchange_cdep(ir_node *old, const ir_node *nw)
46 cdep *cdep = find_cdep(nw);
48 pmap_insert(cdep_map, old, cdep);
52 static void add_cdep(ir_node* node, ir_node* dep_on)
54 cdep *dep = find_cdep(node);
56 ir_fprintf(stderr, "Adding cdep of %+F on %+F\n", node, dep_on);
60 cdep *newdep = xmalloc(sizeof(*newdep));
62 newdep->node = dep_on;
64 pmap_insert(cdep_map, node, newdep);
69 if (dep->node == dep_on) return;
70 if (dep->next == NULL) break;
73 newdep = xmalloc(sizeof(*newdep));
74 newdep->node = dep_on;
80 typedef struct cdep_env {
86 * Pre-block-walker: calculate the control dependence
88 static void cdep_pre(ir_node *node, void *ctx)
95 * start and end block have no control dependency
97 if (node == env->start_block) return;
98 if (node == env->end_block) return;
100 n = get_Block_n_cfgpreds(node);
101 for (i = 0; i < n; i++) {
102 ir_node *pred = get_Block_cfgpred_block(node, i);
106 if (is_Bad(pred)) continue;
108 pdom = get_Block_ipostdom(pred);
109 for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
110 assert(!is_Bad(pdom));
111 add_cdep(dependee, pred);
120 * A block edge hook: add all cdep edges of block.
122 static int cdep_edge_hook(FILE *F, ir_node *block)
127 ir_node *pdom = get_Block_ipostdom(block);
131 "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" color:gold}\n",
132 get_irn_node_nr(pdom), get_irn_node_nr(block)
137 for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
140 "edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
141 "linestyle:dashed color:gold}\n",
142 get_irn_node_nr(block), get_irn_node_nr(cd->node)
150 void compute_cdep(ir_graph *irg)
152 ir_node *start_block, *rem;
155 cdep_map = pmap_create();
157 assure_postdoms(irg);
159 /* we must temporary change the post dominator relation */
160 start_block = get_irg_start_block(irg);
161 rem = get_Block_ipostdom(start_block);
162 set_Block_ipostdom(start_block, get_irg_end_block(irg));
164 env.start_block = get_irg_start_block(irg);
165 env.end_block = get_irg_end_block(irg);
166 irg_block_walk_graph(irg, cdep_pre, NULL, &env);
169 set_dump_block_edge_hook(cdep_edge_hook);
170 dump_ir_block_graph(irg, "_cdep");
171 set_dump_block_edge_hook(NULL);
173 (void) cdep_edge_hook;
176 /* restore the post dominator relation */
177 set_Block_ipostdom(start_block, rem);
181 void free_cdep(ir_graph *irg)
184 // TODO atm leaking more memory than a small memory leaking animal
188 int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
192 for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
193 if (dep->node == candidate) return 1;
199 int is_iterated_cdep_on(ir_node *dependee, ir_node *candidate)
203 while ((dep = find_cdep(dependee)) != NULL) {
204 if (dep->next != NULL) return 0;
205 if (dep->node == candidate) return 1;
206 dependee = dep->node;
212 ir_node *get_unique_cdep(const ir_node *block)
214 cdep *cdep = find_cdep(block);
216 return cdep != NULL && cdep->next == NULL ? cdep->node : NULL;
220 int has_multiple_cdep(const ir_node *block)
222 cdep *cdep = find_cdep(block);
224 return cdep != NULL && cdep->next != NULL;