cdep.c 4.66 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Matthias Braun's avatar
Matthias Braun committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief   Implementation of cdep
Matthias Braun's avatar
Matthias Braun committed
23
 * @author  Christoph Mallon
Matthias Braun's avatar
Matthias Braun committed
24
25
 * @version $Id$
 */
26
27
28
29
30
31
32
#include <assert.h>
#include <stdlib.h>
#include "irdom.h"
#include "irgraph.h"
#include "irgwalk.h"
#include "irnode.h"
#include "pmap.h"
Michael Beck's avatar
Michael Beck committed
33
#include "obst.h"
Michael Beck's avatar
Michael Beck committed
34
#include "xmalloc.h"
35
36
#include "cdep.h"
#include "irprintf.h"
Michael Beck's avatar
Michael Beck committed
37
38
#include "irdump.h"

Michael Beck's avatar
Michael Beck committed
39
40
41
42
typedef struct cdep_info {
	pmap   *cdep_map;     /**< A map to find the list of all control dependence nodes for a block. */
	struct obstack obst;  /**< An obstack where all cdep data lives on. */
} cdep_info;
43

Michael Beck's avatar
Michael Beck committed
44
static cdep_info *cdep_data;
45

46
47
ir_cdep *find_cdep(const ir_node *block)
{
48
	return (ir_cdep*) pmap_get(cdep_data->cdep_map, block);
49
50
}

51
52
void exchange_cdep(ir_node *old, const ir_node *nw)
{
53
	ir_cdep *cdep = find_cdep(nw);
Michael Beck's avatar
Michael Beck committed
54
	pmap_insert(cdep_data->cdep_map, old, cdep);
Christoph Mallon's avatar
Christoph Mallon committed
55
56
}

Michael Beck's avatar
Michael Beck committed
57
58
59
/**
 * Adds a control dependence from node to dep_on.
 */
60
61
static void add_cdep(ir_node *node, ir_node *dep_on)
{
62
	ir_cdep *dep = find_cdep(node);
63

64
	if (dep == NULL) {
65
		ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
66
67
68

		newdep->node = dep_on;
		newdep->next = NULL;
Michael Beck's avatar
Michael Beck committed
69
		pmap_insert(cdep_data->cdep_map, node, newdep);
70
	} else {
71
		ir_cdep *newdep;
72
73
74
75
76
77

		for (;;) {
			if (dep->node == dep_on) return;
			if (dep->next == NULL) break;
			dep = dep->next;
		}
78
		newdep = OALLOC(&cdep_data->obst, ir_cdep);
79
80
81
82
		newdep->node = dep_on;
		newdep->next = NULL;
		dep->next = newdep;
	}
83
84
}

85
86
87
88
typedef struct cdep_env {
	ir_node *start_block;
	ir_node *end_block;
} cdep_env;
89

90
91
92
/**
 * Pre-block-walker: calculate the control dependence
 */
93
94
static void cdep_pre(ir_node *node, void *ctx)
{
95
	cdep_env *env = (cdep_env*) ctx;
Michael Beck's avatar
Michael Beck committed
96
	int i;
97
98
99
100
101
102
103

	/* special case:
	 * start and end block have no control dependency
	 */
	if (node == env->start_block) return;
	if (node == env->end_block) return;

Michael Beck's avatar
Michael Beck committed
104
	for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
105
106
107
108
109
110
111
112
113
114
115
116
		ir_node *pred = get_Block_cfgpred_block(node, i);
		ir_node *pdom;
		ir_node *dependee;

		if (is_Bad(pred)) continue;

		pdom = get_Block_ipostdom(pred);
		for (dependee = node; dependee != pdom; dependee = get_Block_ipostdom(dependee)) {
			assert(!is_Bad(pdom));
			add_cdep(dependee, pred);
		}
	}
117
118
119
}


120
121
122
123
/**
 * A block edge hook: add all cdep edges of block.
 */
static int cdep_edge_hook(FILE *F, ir_node *block)
124
{
125
	ir_cdep *cd;
126

127
128
129
	for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
		fprintf(
			F,
130
131
132
133
			"edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
			"linestyle:dashed color:gold}\n",
			get_irn_node_nr(block), get_irn_node_nr(cd->node)
		);
134
	}
135
136
137
138

	return 0;
}

139
140
void compute_cdep(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
141
	ir_node *rem;
142
143
	cdep_env env;

Michael Beck's avatar
Michael Beck committed
144
	free_cdep(irg);
145
	cdep_data = XMALLOC(cdep_info);
Michael Beck's avatar
Michael Beck committed
146
147
148
	obstack_init(&cdep_data->obst);

	cdep_data->cdep_map = pmap_create();
149

150
	assure_postdoms(irg);
151

Michael Beck's avatar
Michael Beck committed
152
153
154
155
	/* we must temporary change the post dominator relation:
	   the ipdom of the startblock is the end block.
	   Firm does NOT add the phantom edge from Start to End.
	 */
156
157
	env.start_block = get_irg_start_block(irg);
	env.end_block   = get_irg_end_block(irg);
Michael Beck's avatar
Michael Beck committed
158
159
160
161

	rem = get_Block_ipostdom(env.start_block);
	set_Block_ipostdom(env.start_block, env.end_block);

162
163
164
	irg_block_walk_graph(irg, cdep_pre, NULL, &env);

#if 0
165
	set_dump_block_edge_hook(cdep_edge_hook);
166
	dump_ir_block_graph(irg, "_cdep");
167
	set_dump_block_edge_hook(NULL);
Matthias Braun's avatar
Matthias Braun committed
168
169
#else
	(void) cdep_edge_hook;
170
171
#endif

172
	/* restore the post dominator relation */
Michael Beck's avatar
Michael Beck committed
173
	set_Block_ipostdom(env.start_block, rem);
174
175
}

176
177
void free_cdep(ir_graph *irg)
{
Matthias Braun's avatar
Matthias Braun committed
178
	(void) irg;
Michael Beck's avatar
Michael Beck committed
179
180
181
182
183
184
	if (cdep_data != NULL) {
		pmap_destroy(cdep_data->cdep_map);
		obstack_free(&cdep_data->obst, NULL);
		xfree(cdep_data);
		cdep_data = NULL;
	}
185
186
}

187
188
int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
{
189
	const ir_cdep *dep;
Christoph Mallon's avatar
Christoph Mallon committed
190
191
192
193
194
195
196

	for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
		if (dep->node == candidate) return 1;
	}
	return 0;
}

197
198
ir_node *get_unique_cdep(const ir_node *block)
{
199
	ir_cdep *cdep = find_cdep(block);
200
201
202
203

	return cdep != NULL && cdep->next == NULL ? cdep->node : NULL;
}

204
205
int has_multiple_cdep(const ir_node *block)
{
206
	ir_cdep *cdep = find_cdep(block);
207
208
209

	return cdep != NULL && cdep->next != NULL;
}