cdep.c 4.77 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
26
#include <assert.h>
#include <stdlib.h>
27
#include "irdom_t.h"
28
29
30
31
#include "irgraph.h"
#include "irgwalk.h"
#include "irnode.h"
#include "pmap.h"
Michael Beck's avatar
Michael Beck committed
32
#include "obst.h"
Michael Beck's avatar
Michael Beck committed
33
#include "xmalloc.h"
34
#include "cdep_t.h"
35
#include "irprintf.h"
Michael Beck's avatar
Michael Beck committed
36
37
#include "irdump.h"

Michael Beck's avatar
Michael Beck committed
38
39
40
41
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;
42

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

45
46
47
48
49
50
51
52
53
54
ir_node *(get_cdep_node)(const ir_cdep *cdep)
{
	return _get_cdep_node(cdep);
}

ir_cdep *(get_cdep_next)(const ir_cdep *cdep)
{
	return _get_cdep_next(cdep);
}

55
56
ir_cdep *find_cdep(const ir_node *block)
{
57
	assert(is_Block(block));
58
	return pmap_get(ir_cdep, cdep_data->cdep_map, block);
59
60
}

61
62
void exchange_cdep(ir_node *old, const ir_node *nw)
{
63
	ir_cdep *cdep = find_cdep(nw);
64
	assert(is_Block(old));
Michael Beck's avatar
Michael Beck committed
65
	pmap_insert(cdep_data->cdep_map, old, cdep);
Christoph Mallon's avatar
Christoph Mallon committed
66
67
}

Michael Beck's avatar
Michael Beck committed
68
69
70
/**
 * Adds a control dependence from node to dep_on.
 */
71
72
static void add_cdep(ir_node *node, ir_node *dep_on)
{
73
	ir_cdep *dep = find_cdep(node);
74

75
	assert(is_Block(dep_on));
76
	if (dep == NULL) {
77
		ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
78
79
80

		newdep->node = dep_on;
		newdep->next = NULL;
Michael Beck's avatar
Michael Beck committed
81
		pmap_insert(cdep_data->cdep_map, node, newdep);
82
	} else {
83
		ir_cdep *newdep;
84
85

		for (;;) {
86
			if (get_cdep_node(dep) == dep_on) return;
87
88
89
			if (dep->next == NULL) break;
			dep = dep->next;
		}
90
		newdep = OALLOC(&cdep_data->obst, ir_cdep);
91
92
93
94
		newdep->node = dep_on;
		newdep->next = NULL;
		dep->next = newdep;
	}
95
96
}

97
98
99
100
typedef struct cdep_env {
	ir_node *start_block;
	ir_node *end_block;
} cdep_env;
101

102
103
104
/**
 * Pre-block-walker: calculate the control dependence
 */
105
106
static void cdep_pre(ir_node *node, void *ctx)
{
107
	cdep_env *env = (cdep_env*) ctx;
Michael Beck's avatar
Michael Beck committed
108
	int i;
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;

Michael Beck's avatar
Michael Beck committed
116
	for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
117
118
119
120
121
122
123
124
125
126
127
128
		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);
		}
	}
129
130
131
}


132
133
134
135
/**
 * A block edge hook: add all cdep edges of block.
 */
static int cdep_edge_hook(FILE *F, ir_node *block)
136
{
137
	ir_cdep *cd;
138

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

	return 0;
}

151
152
void compute_cdep(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
153
	ir_node *rem;
154
155
	cdep_env env;

Michael Beck's avatar
Michael Beck committed
156
	free_cdep(irg);
157
	cdep_data = XMALLOC(cdep_info);
Michael Beck's avatar
Michael Beck committed
158
159
160
	obstack_init(&cdep_data->obst);

	cdep_data->cdep_map = pmap_create();
161

162
	assure_postdoms(irg);
163

Michael Beck's avatar
Michael Beck committed
164
165
166
167
	/* 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.
	 */
168
169
	env.start_block = get_irg_start_block(irg);
	env.end_block   = get_irg_end_block(irg);
Michael Beck's avatar
Michael Beck committed
170
171
172
173

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

174
175
	irg_block_walk_graph(irg, cdep_pre, NULL, &env);

Matthias Braun's avatar
Matthias Braun committed
176
	(void) cdep_edge_hook;
177

178
	/* restore the post dominator relation */
Michael Beck's avatar
Michael Beck committed
179
	set_Block_ipostdom(env.start_block, rem);
180
181
}

182
183
void free_cdep(ir_graph *irg)
{
Matthias Braun's avatar
Matthias Braun committed
184
	(void) irg;
Michael Beck's avatar
Michael Beck committed
185
186
187
188
189
190
	if (cdep_data != NULL) {
		pmap_destroy(cdep_data->cdep_map);
		obstack_free(&cdep_data->obst, NULL);
		xfree(cdep_data);
		cdep_data = NULL;
	}
191
192
}

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

	for (dep = find_cdep(dependee); dep != NULL; dep = dep->next) {
198
		if (get_cdep_node(dep) == candidate) return 1;
Christoph Mallon's avatar
Christoph Mallon committed
199
200
201
202
	}
	return 0;
}

203
204
ir_node *get_unique_cdep(const ir_node *block)
{
205
	ir_cdep *cdep = find_cdep(block);
206

207
	return cdep != NULL && cdep->next == NULL ? get_cdep_node(cdep) : NULL;
208
209
}

210
211
int has_multiple_cdep(const ir_node *block)
{
212
	ir_cdep *cdep = find_cdep(block);
213
214
215

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