lower_mode_b.c 10.2 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
/*
2
 * Copyright (C) 1995-2011 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
 *
 * 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.
 */

/**
Matthias Braun's avatar
Matthias Braun committed
21
 * @file
22
 * @brief       lower mode_b operations to something the backend can handle
Matthias Braun's avatar
Matthias Braun committed
23
 * @author      Matthias Braun, Christoph Mallon
24
 * @version     $Id$
Matthias Braun's avatar
Matthias Braun committed
25
26
27
 */
#include "config.h"

28
29
#include "lower_mode_b.h"

Matthias Braun's avatar
Matthias Braun committed
30
#include <stdlib.h>
31
#include <stdbool.h>
Matthias Braun's avatar
Matthias Braun committed
32
33
34

#include "irnode_t.h"
#include "ircons_t.h"
35
#include "irflag.h"
Matthias Braun's avatar
Matthias Braun committed
36
37
#include "irgwalk.h"
#include "irtools.h"
38
#include "iredges.h"
Matthias Braun's avatar
Matthias Braun committed
39
#include "iropt_t.h"
40
#include "irgmod.h"
Matthias Braun's avatar
Matthias Braun committed
41
42
43
#include "tv.h"
#include "error.h"
#include "lowering.h"
44
#include "pdeq.h"
Michael Beck's avatar
Michael Beck committed
45
#include "irpass_t.h"
46
47
#include "util.h"
#include "array.h"
Matthias Braun's avatar
Matthias Braun committed
48

49
50
51
52
typedef struct needs_lowering_t {
	ir_node *node;
	int      input;
} needs_lowering_t;
53

54
55
static const lower_mode_b_config_t *config;
static needs_lowering_t            *needs_lowering;
Matthias Braun's avatar
Matthias Braun committed
56
57
58

static ir_node *create_not(dbg_info *dbgi, ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
59
60
61
62
63
	ir_node   *block  = get_nodes_block(node);
	ir_mode   *mode   = config->lowered_mode;
	ir_tarval *tv_one = get_mode_one(mode);
	ir_graph  *irg    = get_irn_irg(node);
	ir_node   *one    = new_rd_Const(dbgi, irg, tv_one);
Matthias Braun's avatar
Matthias Braun committed
64

65
	return new_rd_Eor(dbgi, block, node, one, mode);
Matthias Braun's avatar
Matthias Braun committed
66
67
}

68
static ir_node *convert_to_modeb(ir_node *node)
69
{
70
71
72
73
	ir_node   *block   = get_nodes_block(node);
	ir_graph  *irg     = get_irn_irg(node);
	ir_mode   *mode    = config->lowered_mode;
	ir_tarval *tv_zero = get_mode_null(mode);
Matthias Braun's avatar
Matthias Braun committed
74
	ir_node   *zero    = new_r_Const(irg, tv_zero);
75
76
	ir_node   *cmp     = new_r_Cmp(block, node, zero, ir_relation_less_greater);
	return cmp;
77
78
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
ir_node *ir_create_cond_set(ir_node *cond_value, ir_mode *dest_mode)
{
	ir_node  *lower_block = part_block_edges(cond_value);
	ir_node  *upper_block = get_nodes_block(cond_value);
	ir_graph *irg         = get_irn_irg(cond_value);
	ir_node  *cond        = new_r_Cond(upper_block, cond_value);
	ir_node  *proj_true   = new_r_Proj(cond, mode_X, pn_Cond_true);
	ir_node  *proj_false  = new_r_Proj(cond, mode_X, pn_Cond_false);
	ir_node  *in_true[1]  = { proj_true };
	ir_node  *in_false[1] = { proj_false };
	ir_node  *true_block  = new_r_Block(irg, ARRAY_SIZE(in_true), in_true);
	ir_node  *false_block = new_r_Block(irg, ARRAY_SIZE(in_false),in_false);
	ir_node  *true_jmp    = new_r_Jmp(true_block);
	ir_node  *false_jmp   = new_r_Jmp(false_block);
	ir_node  *lower_in[2] = { true_jmp, false_jmp };
	ir_node  *one         = new_r_Const(irg, get_mode_one(dest_mode));
	ir_node  *zero        = new_r_Const(irg, get_mode_null(dest_mode));
	ir_node  *phi_in[2]   = { one, zero };
	ir_node  *phi;

	set_irn_in(lower_block, ARRAY_SIZE(lower_in), lower_in);
	phi = new_r_Phi(lower_block, ARRAY_SIZE(phi_in), phi_in, dest_mode);

	return phi;
}

Matthias Braun's avatar
Matthias Braun committed
105
106
107
108
static ir_node *lower_node(ir_node *node)
{
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_node  *block = get_nodes_block(node);
109
	ir_mode  *mode  = config->lowered_mode;
110
	ir_node  *res   = (ir_node*)get_irn_link(node);
111
	ir_graph *irg;
Matthias Braun's avatar
Matthias Braun committed
112

113
	if (res != NULL)
114
115
		return res;

Matthias Braun's avatar
Matthias Braun committed
116
117
	node = skip_Tuple(node);

118
119
	assert(get_irn_mode(node) == mode_b);

120
	irg = get_irn_irg(node);
121
122
	switch (get_irn_opcode(node)) {
	case iro_Phi: {
123
124
		int       i, arity;
		ir_node **in;
125
126
		ir_node  *dummy;
		ir_node  *new_phi;
127

128
129
130
		arity = get_irn_arity(node);
		in    = ALLOCAN(ir_node*, arity);
		dummy = new_r_Dummy(irg, mode);
131
		for (i = 0; i < arity; ++i) {
132
			in[i] = dummy;
133
		}
134
		new_phi = new_r_Phi(block, arity, in, mode);
135
		/* FIXME This does not correctly break cycles: The Phi might not be the
136
137
138
		 * first in the recursion, so the caller(s) are some yet un-lowered
		 * nodes and this Phi might have them (indirectly) as operands, so they
		 * would be replaced twice. */
139
140
		set_irn_link(node, new_phi);

141
		for (i = 0; i < arity; ++i) {
142
			ir_node *in         = get_irn_n(node, i);
Matthias Braun's avatar
Matthias Braun committed
143
			ir_node *lowered_in = lower_node(in);
Matthias Braun's avatar
Matthias Braun committed
144

145
			set_irn_n(new_phi, i, lowered_in);
146
147
148
149
150
		}

		return new_phi;
	}

151
152
153
154
155
156
157
158
159
160
161
162
	case iro_And: {
		ir_node *lowered_left  = lower_node(get_And_left(node));
		ir_node *lowered_right = lower_node(get_And_right(node));
		res = new_rd_And(dbgi, block, lowered_left, lowered_right, mode);
		break;
	}
	case iro_Or: {
		ir_node *lowered_left  = lower_node(get_Or_left(node));
		ir_node *lowered_right = lower_node(get_Or_right(node));
		res = new_rd_Or(dbgi, block, lowered_left, lowered_right, mode);
		break;
	}
163
	case iro_Eor: {
164
165
166
		ir_node *lowered_left  = lower_node(get_Eor_left(node));
		ir_node *lowered_right = lower_node(get_Eor_right(node));
		res = new_rd_Eor(dbgi, block, lowered_left, lowered_right, mode);
167
		break;
Matthias Braun's avatar
Matthias Braun committed
168
	}
169

170
	case iro_Not: {
Matthias Braun's avatar
Matthias Braun committed
171
172
173
		ir_node *op     = get_Not_op(node);
		ir_node *low_op = lower_node(op);

174
		res = create_not(dbgi, low_op);
175
		break;
Matthias Braun's avatar
Matthias Braun committed
176
	}
177

178
179
	case iro_Mux: {
		ir_node *cond        = get_Mux_sel(node);
Matthias Braun's avatar
Matthias Braun committed
180
		ir_node *low_cond    = lower_node(cond);
181
		ir_node *v_true      = get_Mux_true(node);
Matthias Braun's avatar
Matthias Braun committed
182
		ir_node *low_v_true  = lower_node(v_true);
183
		ir_node *v_false     = get_Mux_false(node);
Matthias Braun's avatar
Matthias Braun committed
184
185
		ir_node *low_v_false = lower_node(v_false);

186
		ir_node *and0     = new_rd_And(dbgi, block, low_cond, low_v_true, mode);
Matthias Braun's avatar
Matthias Braun committed
187
		ir_node *not_cond = create_not(dbgi, low_cond);
188
		ir_node *and1     = new_rd_And(dbgi, block, not_cond, low_v_false, mode);
189
190
		res = new_rd_Or(dbgi, block, and0, and1, mode);
		break;
Matthias Braun's avatar
Matthias Braun committed
191
	}
192

193
	case iro_Cmp: {
194
195
196
197
		ir_node    *left     = get_Cmp_left(node);
		ir_node    *right    = get_Cmp_right(node);
		ir_mode    *cmp_mode = get_irn_mode(left);
		ir_relation relation = get_Cmp_relation(node);
198
199
200

		if ((mode_is_int(cmp_mode) || mode_is_reference(cmp_mode)) &&
			(get_mode_size_bits(cmp_mode) < get_mode_size_bits(mode) ||
201
			(mode_is_signed(cmp_mode) && is_Const(right) && is_Const_null(right) && relation != ir_relation_greater))) {
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
			int         need_not = 0;
			ir_node    *a        = NULL;
			ir_node    *b        = NULL;
			int         bits;
			ir_tarval  *tv;
			ir_node    *shift_cnt;

			if (relation == ir_relation_less) {
				/* a < b  ->  (a - b) >> 31 */
				a = left;
				b = right;
			} else if (relation == ir_relation_less_equal) {
				/* a <= b  -> ~(a - b) >> 31 */
				a        = right;
				b        = left;
				need_not = 1;
			} else if (relation == ir_relation_greater) {
				/* a > b   -> (b - a) >> 31 */
				a = right;
				b = left;
			} else if (relation == ir_relation_greater_equal) {
				/* a >= b   -> ~(a - b) >> 31 */
				a        = left;
				b        = right;
				need_not = 1;
			} else {
				goto synth_zero_one;
			}
Matthias Braun's avatar
Matthias Braun committed
230

231
232
233
			bits      = get_mode_size_bits(mode);
			tv        = new_tarval_from_long(bits-1, mode_Iu);
			shift_cnt = new_rd_Const(dbgi, irg, tv);
234

235
236
237
238
			if (cmp_mode != mode) {
				a = new_rd_Conv(dbgi, block, a, mode);
				b = new_rd_Conv(dbgi, block, b, mode);
			}
Matthias Braun's avatar
Matthias Braun committed
239

240
241
242
			res = new_rd_Sub(dbgi, block, a, b, mode);
			if (need_not) {
				res = new_rd_Not(dbgi, block, res, mode);
243
			}
244
245
246
247
248
249
250
251
252
			res = new_rd_Shr(dbgi, block, res, shift_cnt, mode);
		} else {
			/* synthesize the 0/1 value */
synth_zero_one:
			res = config->create_set(node);
		}
		break;
	}

253
	case iro_Const: {
Matthias Braun's avatar
Matthias Braun committed
254
		ir_tarval *tv = get_Const_tarval(node);
255
		if (tv == get_tarval_b_true()) {
Matthias Braun's avatar
Matthias Braun committed
256
257
			ir_tarval *tv_one = get_mode_one(mode);
			res               = new_rd_Const(dbgi, irg, tv_one);
258
		} else if (tv == get_tarval_b_false()) {
Matthias Braun's avatar
Matthias Braun committed
259
260
			ir_tarval *tv_zero = get_mode_null(mode);
			res                = new_rd_Const(dbgi, irg, tv_zero);
Matthias Braun's avatar
Matthias Braun committed
261
262
263
		} else {
			panic("invalid boolean const %+F", node);
		}
264
		break;
Matthias Braun's avatar
Matthias Braun committed
265
	}
266

267
	case iro_Unknown:
268
		res = new_r_Unknown(irg, mode);
269
		break;
270

Matthias Braun's avatar
Matthias Braun committed
271
272
273
274
	case iro_Bad:
		res = new_r_Bad(irg, mode);
		break;

275
	default:
276
		panic("Don't know how to lower mode_b node %+F", node);
277
	}
278
279
280

	set_irn_link(node, res);
	return res;
Matthias Braun's avatar
Matthias Braun committed
281
282
}

283
static bool needs_mode_b_input(const ir_node *node, int input)
Matthias Braun's avatar
Matthias Braun committed
284
{
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	return (is_Cond(node) && input == n_Cond_selector)
	    || (is_Mux(node) && input == n_Mux_sel);
}

/**
 * Collects "roots" of a mode_b calculation. These are nodes which require a
 * mode_b input (Cond, Mux, Conv(xxx_b))
 */
static void collect_needs_lowering(ir_node *node, void *env)
{
	int arity = get_irn_arity(node);
	int i;
	(void) env;

	/* if the node produces mode_b then it is a not a root (but should be
	 * something our lower_node function can handle) */
	if (get_irn_mode(node) == mode_b) {
		assert(is_And(node) || is_Or(node) || is_Eor(node) || is_Phi(node)
		       || is_Not(node) || is_Mux(node) || is_Cmp(node)
Matthias Braun's avatar
Matthias Braun committed
304
		       || is_Const(node) || is_Unknown(node) || is_Bad(node));
305
306
		return;
	}
Matthias Braun's avatar
Matthias Braun committed
307

308
	for (i = 0; i < arity; ++i) {
309
		needs_lowering_t entry;
Matthias Braun's avatar
Matthias Braun committed
310
		ir_node *in = get_irn_n(node, i);
311
		if (get_irn_mode(in) != mode_b)
Matthias Braun's avatar
Matthias Braun committed
312
			continue;
313
314
		if (is_Cmp(in) && needs_mode_b_input(node, i))
			continue;
Matthias Braun's avatar
Matthias Braun committed
315

316
317
318
		entry.node  = node;
		entry.input = i;
		ARR_APP1(needs_lowering_t, needs_lowering, entry);
Matthias Braun's avatar
Matthias Braun committed
319
320
321
	}
}

322
void ir_lower_mode_b(ir_graph *irg, const lower_mode_b_config_t *nconfig)
Matthias Braun's avatar
Matthias Braun committed
323
{
324
325
	size_t i;
	size_t n;
326

327
	config = nconfig;
328

329
	/* edges are used by part_block_edges in the ir_create_cond_set variant. */
330
331
	edges_assure(irg);

332
	set_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED);
333
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
334

335
	needs_lowering = NEW_ARR_F(needs_lowering_t, 0);
336

337
	irg_walk_graph(irg, firm_clear_link, collect_needs_lowering, NULL);
338

339
340
341
342
343
344
345
	n = ARR_LEN(needs_lowering);
	for (i = 0; i < n; ++i) {
		const needs_lowering_t *entry   = &needs_lowering[i];
		ir_node                *node    = entry->node;
		int                     input   = entry->input;
		ir_node                *in      = get_irn_n(node, input);
		ir_node                *lowered = lower_node(in);
346

347
348
349
		if (needs_mode_b_input(node, input))
			lowered = convert_to_modeb(lowered);
		set_irn_n(node, input, lowered);
350
351
	}

352
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
353

354
355
356
	DEL_ARR_F(needs_lowering);

	if (n > 0) {
357
		/* lowering might create new blocks, so be sure to handle this */
358
359
		clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
		                   | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
360
		edges_deactivate(irg);
361
	}
Matthias Braun's avatar
Matthias Braun committed
362
}