cdep.c 4.73 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
/*
2
 * Copyright (C) 1995-2007 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
23
24
 *
 * 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
 * @version $Id$
 */
25
26
27
28
29
30
31
#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
32
#include "xmalloc.h"
33
34
#include "cdep.h"
#include "irprintf.h"
Michael Beck's avatar
Michael Beck committed
35
36
#include "irdump.h"

37

38
static pmap *cdep_map;
39

40
ir_cdep *find_cdep(const ir_node *block)
41
{
42
	return pmap_get(cdep_map, (void *)block);
43
44
45
}


46
void exchange_cdep(ir_node *old, const ir_node *nw)
Christoph Mallon's avatar
Christoph Mallon committed
47
{
48
	ir_cdep *cdep = find_cdep(nw);
Christoph Mallon's avatar
Christoph Mallon committed
49
50
51
52
53

	pmap_insert(cdep_map, old, cdep);
}


54
55
static void add_cdep(ir_node* node, ir_node* dep_on)
{
56
	ir_cdep *dep = find_cdep(node);
57
58
59
60
#if 0
	ir_fprintf(stderr, "Adding cdep of %+F on %+F\n", node, dep_on);
#endif

61
	if (dep == NULL) {
62
		ir_cdep *newdep = xmalloc(sizeof(*newdep));
63
64
65
66
67

		newdep->node = dep_on;
		newdep->next = NULL;
		pmap_insert(cdep_map, node, newdep);
	} else {
68
		ir_cdep *newdep;
69
70
71
72
73
74
75
76
77
78
79

		for (;;) {
			if (dep->node == dep_on) return;
			if (dep->next == NULL) break;
			dep = dep->next;
		}
		newdep = xmalloc(sizeof(*newdep));
		newdep->node = dep_on;
		newdep->next = NULL;
		dep->next = newdep;
	}
80
81
}

82
83
84
85
typedef struct cdep_env {
	ir_node *start_block;
	ir_node *end_block;
} cdep_env;
86

87
88
89
90
/**
 * Pre-block-walker: calculate the control dependence
 */
static void cdep_pre(ir_node *node, void *ctx)
91
{
92
	cdep_env *env = ctx;
Matthias Braun's avatar
Matthias Braun committed
93
94
	unsigned int n;
	unsigned int i;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

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

	n = get_Block_n_cfgpreds(node);
	for (i = 0; i < n; i++) {
		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);
		}
	}
116
117
118
}


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

#if 0
127
	ir_node *pdom = get_Block_ipostdom(block);
128
129
130
131
132
133
134
135
136
	if (pdom != NULL) {
		fprintf(
			F,
			"edge:{sourcename:\"n%ld\" targetname:\"n%ld\" color:gold}\n",
			get_irn_node_nr(pdom), get_irn_node_nr(block)
		);
	}
#endif

137
138
139
	for (cd = find_cdep(block); cd != NULL; cd = cd->next) {
		fprintf(
			F,
140
141
142
143
			"edge:{sourcename:\"n%ld\" targetname:\"n%ld\" "
			"linestyle:dashed color:gold}\n",
			get_irn_node_nr(block), get_irn_node_nr(cd->node)
		);
144
	}
145
146
147
148
149

	return 0;
}


150
void compute_cdep(ir_graph *irg)
151
{
152
153
154
155
	ir_node *start_block, *rem;
	cdep_env env;

	cdep_map = pmap_create();
156

157
	assure_postdoms(irg);
158

Michael Beck's avatar
Michael Beck committed
159
160
161
162
	/* 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.
	 */
163
164
165
	start_block = get_irg_start_block(irg);
	rem = get_Block_ipostdom(start_block);
	set_Block_ipostdom(start_block, get_irg_end_block(irg));
166

167
168
169
170
171
	env.start_block = get_irg_start_block(irg);
	env.end_block   = get_irg_end_block(irg);
	irg_block_walk_graph(irg, cdep_pre, NULL, &env);

#if 0
172
	set_dump_block_edge_hook(cdep_edge_hook);
173
	dump_ir_block_graph(irg, "_cdep");
174
	set_dump_block_edge_hook(NULL);
Matthias Braun's avatar
Matthias Braun committed
175
176
#else
	(void) cdep_edge_hook;
177
178
#endif

179
180
	/* restore the post dominator relation */
	set_Block_ipostdom(start_block, rem);
181
182
183
}


184
void free_cdep(ir_graph *irg)
185
{
Matthias Braun's avatar
Matthias Braun committed
186
	(void) irg;
187
	// TODO atm leaking more memory than a small memory leaking animal
188
189
190
}


191
int is_cdep_on(const ir_node *dependee, const ir_node *candidate)
Christoph Mallon's avatar
Christoph Mallon committed
192
{
193
	const ir_cdep *dep;
Christoph Mallon's avatar
Christoph Mallon committed
194
195
196
197
198
199
200
201

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


202
int is_iterated_cdep_on(ir_node *dependee, ir_node *candidate)
203
{
204
	const ir_cdep *dep;
205
206
207
208
209
210
211
212
213
214

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


215
ir_node *get_unique_cdep(const ir_node *block)
216
{
217
	ir_cdep *cdep = find_cdep(block);
218
219
220
221
222

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


223
int has_multiple_cdep(const ir_node *block)
224
{
225
	ir_cdep *cdep = find_cdep(block);
226
227
228

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