beflags.c 8.33 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
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       modifies schedule so flags dependencies are respected.
 * @author      Matthias Braun, Christoph Mallon
 * @version     $Id: besched.h 14693 2007-06-21 15:35:49Z beck $
Matthias Braun's avatar
Matthias Braun committed
25
26
27
28
29
30
31
32
33
34
35
36
 *
 * Fixup schedule to respect flag constraints by moving and rematerialisation of
 * nodes.
 *
 * Flags are modeled as register classes with ignore registers. However to avoid
 * bloating the graph, only flag-consumer -> producer dependencies are
 * explicitely modeled in the graph. Nodes that just change the flags are only
 * marked with the arch_irn_flags_modify_flags flag.
 *
 * Flags are usually a limited resource that can't (or at least shouldn't) be
 * spilled. So in some situations (for example 2 adc-nodes that use the flags of
 * a single add node on x86) operations have to be repeated to work correctly.
Matthias Braun's avatar
Matthias Braun committed
37
38
39
 */
#include "config.h"

40
41
#include <stdbool.h>

Matthias Braun's avatar
Matthias Braun committed
42
43
44
45
46
47
48
49
50
#include "irgwalk.h"
#include "irnode_t.h"
#include "irtools.h"
#include "ircons.h"
#include "iredges_t.h"
#include "irprintf.h"
#include "error.h"

#include "beflags.h"
51
52
53
#include "bearch.h"
#include "beirg.h"
#include "besched.h"
54
#include "benode.h"
55
#include "belive.h"
Matthias Braun's avatar
Matthias Braun committed
56

57
58
59
60
static const arch_register_class_t *flag_class;
static const arch_register_t       *flags_reg;
static func_rematerialize           remat;
static check_modifies_flags         check_modify;
61
static int                          changed;
Matthias Braun's avatar
Matthias Braun committed
62
63
64

static ir_node *default_remat(ir_node *node, ir_node *after)
{
Michael Beck's avatar
Michael Beck committed
65
	ir_node *block, *copy;
66
	if (is_Block(after))
67
68
69
70
		block = after;
	else
		block = get_nodes_block(after);

Michael Beck's avatar
Michael Beck committed
71
	copy = exact_copy(node);
72
	set_nodes_block(copy, block);
Matthias Braun's avatar
Matthias Braun committed
73
74
75
76
77
	sched_add_after(after, copy);

	return copy;
}

78
79
80
81
82
static bool default_check_modifies(const ir_node *node)
{
	return arch_irn_is(node, modify_flags);
}

Matthias Braun's avatar
Matthias Braun committed
83
/**
Michael Beck's avatar
Michael Beck committed
84
 * tests whether we can legally move node node after node after
Matthias Braun's avatar
Matthias Braun committed
85
86
 * (only works for nodes in same block)
 */
87
static bool can_move(ir_node *node, ir_node *after)
Matthias Braun's avatar
Matthias Braun committed
88
89
90
91
92
93
94
95
96
97
{
	const ir_edge_t *edge;
	assert(get_nodes_block(node) == get_nodes_block(after));

	/* TODO respect dep edges */
	assert(get_irn_n_edges_kind(node, EDGE_KIND_DEP) == 0);

	/** all users have to be after the after node */
	foreach_out_edge(node, edge) {
		ir_node *out = get_edge_src_irn(edge);
98
		if (is_Proj(out)) {
Matthias Braun's avatar
Matthias Braun committed
99
100
101
102
			const ir_edge_t *edge2;
			assert(get_irn_n_edges_kind(out, EDGE_KIND_DEP) == 0);
			foreach_out_edge(out, edge2) {
				ir_node *out2 = get_edge_src_irn(edge2);
yb9976's avatar
yb9976 committed
103
				/* Phi or End represents a usage at block end. */
104
				if (is_Phi(out2) || is_End(out2))
Christoph Mallon's avatar
Christoph Mallon committed
105
					continue;
106
				if (is_Sync(out2)) {
107
108
109
					const ir_edge_t *edge3;
					foreach_out_edge(out2, edge3) {
						ir_node *out3 = get_edge_src_irn(edge3);
yb9976's avatar
yb9976 committed
110
						/* Phi or End represents a usage at block end. */
111
						if (is_Phi(out3) || is_End(out3))
112
113
							continue;
						assert(!is_Sync(out3));
114
						if (sched_get_time_step(out3) <= sched_get_time_step(after)) {
115
							return false;
116
117
						}
					}
118
				} else if (sched_get_time_step(out2) <= sched_get_time_step(after)) {
119
					return false;
Matthias Braun's avatar
Matthias Braun committed
120
121
122
123
				}
			}
		} else {
			/* phi represents a usage at block end */
124
			if (is_Phi(out))
Matthias Braun's avatar
Matthias Braun committed
125
				continue;
126
			if (sched_get_time_step(out) <= sched_get_time_step(after)) {
127
				return false;
Matthias Braun's avatar
Matthias Braun committed
128
129
130
131
			}
		}
	}

132
	return true;
Matthias Braun's avatar
Matthias Braun committed
133
134
135
}

static void rematerialize_or_move(ir_node *flags_needed, ir_node *node,
136
                                  ir_node *flag_consumers, int pn)
Matthias Braun's avatar
Matthias Braun committed
137
138
139
140
141
{
	ir_node *n;
	ir_node *copy;
	ir_node *value;

142
	if (!is_Block(node) &&
143
144
			get_nodes_block(flags_needed) == get_nodes_block(node) &&
			can_move(flags_needed, node)) {
Matthias Braun's avatar
Matthias Braun committed
145
146
147
		/* move it */
		sched_remove(flags_needed);
		sched_add_after(node, flags_needed);
148
		/* No need to update liveness, because the node stays in the same block */
Matthias Braun's avatar
Matthias Braun committed
149
150
151
		return;
	}

152
153
	changed = 1;
	copy    = remat(flags_needed, node);
Matthias Braun's avatar
Matthias Braun committed
154

155
	if (get_irn_mode(copy) == mode_T) {
156
157
		ir_mode *mode = flag_class->mode;
		value = new_rd_Proj(NULL, copy, mode, pn);
Matthias Braun's avatar
Matthias Braun committed
158
159
160
161
162
163
164
165
	} else {
		value = copy;
	}

	n = flag_consumers;
	do {
		int i;
		int arity = get_irn_arity(n);
166
		for (i = 0; i < arity; ++i) {
Matthias Braun's avatar
Matthias Braun committed
167
168
			ir_node *in = get_irn_n(n, i);
			in = skip_Proj(in);
169
			if (in == flags_needed) {
Matthias Braun's avatar
Matthias Braun committed
170
171
172
173
174
				set_irn_n(n, i, value);
				break;
			}
		}
		n = get_irn_link(n);
175
	} while (n != NULL);
176
177
178
179
180

	/* No need to introduce the copy, because it only lives in this block, but
	 * we have to update the liveness of all operands */
	if (is_Block(node) ||
			get_nodes_block(node) != get_nodes_block(flags_needed)) {
181
182
183
		ir_graph *irg = get_irn_irg(node);
		be_lv_t  *lv  = be_get_irg_liveness(irg);
		int       i;
184

185
186
187
188
		if (lv != NULL) {
			for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
				be_liveness_update(lv, get_irn_n(copy, i));
			}
189
190
		}
	}
Matthias Braun's avatar
Matthias Braun committed
191
192
}

Matthias Braun's avatar
Matthias Braun committed
193
194
195
196
197
198
199
/**
 * walks up the schedule and makes sure there are no flag-destroying nodes
 * between a flag-consumer -> flag-producer chain. Fixes problematic situations
 * by moving and/or rematerialisation of the flag-producers.
 * (This can be extended in the future to do some register allocation on targets
 *  like ppc32 where we conceptually have 8 flag registers)
 */
Matthias Braun's avatar
Matthias Braun committed
200
201
202
203
204
static void fix_flags_walker(ir_node *block, void *env)
{
	ir_node *node;
	ir_node *flags_needed   = NULL;
	ir_node *flag_consumers = NULL;
205
	int      pn = -1;
206
	(void) env;
Matthias Braun's avatar
Matthias Braun committed
207
208
209
210

	sched_foreach_reverse(block, node) {
		int i, arity;
		ir_node *new_flags_needed = NULL;
211
		ir_node *test;
Matthias Braun's avatar
Matthias Braun committed
212

213
		if (is_Phi(node))
214
215
			break;

216
		if (node == flags_needed) {
Matthias Braun's avatar
Matthias Braun committed
217
218
219
220
221
			/* all ok */
			flags_needed   = NULL;
			flag_consumers = NULL;
		}

222
		/* test whether node destroys the flags */
223
224
225
226
		test = node;
		if (be_is_Keep(test))
			test = sched_prev(test);

227
		if (flags_needed != NULL && check_modify(test)) {
Matthias Braun's avatar
Matthias Braun committed
228
			/* rematerialize */
229
			rematerialize_or_move(flags_needed, node, flag_consumers, pn);
Matthias Braun's avatar
Matthias Braun committed
230
231
232
233
			flags_needed   = NULL;
			flag_consumers = NULL;
		}

234
		/* test whether the current node needs flags */
Matthias Braun's avatar
Matthias Braun committed
235
		arity = get_irn_arity(node);
236
		for (i = 0; i < arity; ++i) {
237
			const arch_register_class_t *cls = arch_get_irn_reg_class(node, i);
238
			if (cls == flag_class) {
Matthias Braun's avatar
Matthias Braun committed
239
240
241
242
243
				assert(new_flags_needed == NULL);
				new_flags_needed = get_irn_n(node, i);
			}
		}

244
		if (new_flags_needed == NULL)
Matthias Braun's avatar
Matthias Braun committed
245
246
			continue;

247
		/* spiller can't (correctly) remat flag consumers at the moment */
248
		assert(!arch_irn_is(node, rematerializable));
249

250
251
		if (skip_Proj(new_flags_needed) != flags_needed) {
			if (flags_needed != NULL) {
Matthias Braun's avatar
Matthias Braun committed
252
				/* rematerialize node */
253
				rematerialize_or_move(flags_needed, node, flag_consumers, pn);
254
				flags_needed   = NULL;
Matthias Braun's avatar
Matthias Braun committed
255
256
257
				flag_consumers = NULL;
			}

258
			flags_needed = new_flags_needed;
259
			arch_set_irn_register(flags_needed, flags_reg);
260
			if (is_Proj(flags_needed)) {
261
262
				pn           = get_Proj_proj(flags_needed);
				flags_needed = get_Proj_pred(flags_needed);
Matthias Braun's avatar
Matthias Braun committed
263
			}
264
265
			flag_consumers = node;
			set_irn_link(flag_consumers, NULL);
266
			assert(arch_irn_is(flags_needed, rematerializable));
Matthias Braun's avatar
Matthias Braun committed
267
268
		} else {
			/* link all consumers in a list */
269
			set_irn_link(node, flag_consumers);
Matthias Braun's avatar
Matthias Braun committed
270
271
272
273
			flag_consumers = node;
		}
	}

274
	if (flags_needed != NULL) {
275
		assert(get_nodes_block(flags_needed) != block);
276
		rematerialize_or_move(flags_needed, node, flag_consumers, pn);
277
278
279
280
		flags_needed   = NULL;
		flag_consumers = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
281
282
283
284
	assert(flags_needed   == NULL);
	assert(flag_consumers == NULL);
}

285
void be_sched_fix_flags(ir_graph *irg, const arch_register_class_t *flag_cls,
286
287
                        func_rematerialize remat_func,
                        check_modifies_flags check_modifies_flags_func)
Matthias Braun's avatar
Matthias Braun committed
288
{
289
290
291
292
293
	flag_class   = flag_cls;
	flags_reg    = & flag_class->regs[0];
	remat        = remat_func;
	check_modify = check_modifies_flags_func;
	changed      = 0;
294
	if (remat == NULL)
Matthias Braun's avatar
Matthias Braun committed
295
		remat = &default_remat;
296
297
	if (check_modify == NULL)
		check_modify = &default_check_modifies;
Matthias Braun's avatar
Matthias Braun committed
298

299
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
300
	irg_block_walk_graph(irg, fix_flags_walker, NULL, NULL);
301
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
302

303
	if (changed) {
304
		be_remove_dead_nodes_from_schedule(irg);
305
	}
Matthias Braun's avatar
Matthias Braun committed
306
}