lower_switch.c 15.1 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
 */

/**
 * @file
 * @brief   Lowering of Switches if necessary or advantageous.
 * @author  Moritz Kroll
 */
11
#include <limits.h>
12
#include <stdbool.h>
13

14
#include "array.h"
15
#include "ircons.h"
Moritz Kroll's avatar
Moritz Kroll committed
16
#include "irgopt.h"
17
#include "irgwalk.h"
18
#include "irnodeset.h"
19
#include "irnode_t.h"
20
#include "irouts_t.h"
21
#include "lowering.h"
Matthias Braun's avatar
Matthias Braun committed
22
#include "panic.h"
23
#include "util.h"
24

25
typedef struct walk_env_t {
26
27
	ir_nodeset_t  processed;
	ir_mode      *selector_mode;
28
	unsigned      spare_size; /**< the allowed spare size for table switches */
Matthias Braun's avatar
Matthias Braun committed
29
	unsigned      small_switch;
30
	bool          changed;    /**< indicates whether a change was performed */
31
32
} walk_env_t;

Matthias Braun's avatar
Matthias Braun committed
33
34
35
36
37
typedef struct target_t {
	ir_node *block;     /**< block that is targetted */
	uint16_t n_entries; /**< number of table entries targetting this block */
	uint16_t i;
} target_t;
38

Matthias Braun's avatar
Matthias Braun committed
39
40
typedef struct switch_info_t {
	ir_node     *switchn;
41
42
	ir_tarval   *switch_min;
	ir_tarval   *switch_max;
Matthias Braun's avatar
Matthias Braun committed
43
44
	ir_node     *default_block;
	unsigned     num_cases;
Matthias Braun's avatar
Matthias Braun committed
45
	target_t    *targets;
Matthias Braun's avatar
Matthias Braun committed
46
47
	ir_node    **defusers;    /**< the Projs pointing to the default case */
} switch_info_t;
48

Matthias Braun's avatar
Matthias Braun committed
49
50
51
/**
 * analyze enough to decide if we should lower the switch
 */
52
static void analyse_switch0(switch_info_t *info, ir_node *switchn)
53
{
54
55
56
57
58
59
60
61
	const ir_switch_table *table      = get_Switch_table(switchn);
	size_t                 n_entries  = ir_switch_table_get_n_entries(table);
	ir_mode               *mode       = get_irn_mode(get_Switch_selector(switchn));
	ir_tarval             *switch_min = get_mode_max(mode);
	ir_tarval             *switch_max = get_mode_min(mode);
	unsigned               num_cases  = 0;

	for (size_t e = 0; e < n_entries; ++e) {
Matthias Braun's avatar
Matthias Braun committed
62
63
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
64
		if (entry->pn == pn_Switch_default)
65
66
			continue;

67
68
69
70
71
		if (tarval_cmp(entry->min, switch_min) == ir_relation_less)
			switch_min = entry->min;
		if (tarval_cmp(entry->max, switch_max) == ir_relation_greater)
			switch_max = entry->max;

72
		++num_cases;
73
74
	}

Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
	info->switchn    = switchn;
	info->switch_min = switch_min;
	info->switch_max = switch_max;
	info->num_cases  = num_cases;
79
80
}

Matthias Braun's avatar
Matthias Braun committed
81
82
83
84
85
/**
 * Analyse the stuff that anayse_switch0() left out
 */
static void analyse_switch1(switch_info_t *info)
{
86
87
	const ir_node  *switchn   = info->switchn;
	unsigned        n_outs    = get_Switch_n_outs(switchn);
Matthias Braun's avatar
Matthias Braun committed
88
	target_t       *targets   = XMALLOCNZ(target_t, n_outs);
89
	foreach_irn_out_r(switchn, i, proj) {
Matthias Braun's avatar
Matthias Braun committed
90
91
92
93
		long     pn     = get_Proj_proj(proj);
		ir_node *target = get_irn_out(proj, 0);

		assert((unsigned)pn < n_outs);
Matthias Braun's avatar
Matthias Braun committed
94
95
		assert(targets[(unsigned)pn].block == NULL);
		targets[(unsigned)pn].block = target;
Matthias Braun's avatar
Matthias Braun committed
96
97
	}

Matthias Braun's avatar
Matthias Braun committed
98
	const ir_switch_table *table = get_Switch_table(switchn);
99
100
	for (size_t e = 0, n_entries = ir_switch_table_get_n_entries(table);
	     e < n_entries; ++e) {
Matthias Braun's avatar
Matthias Braun committed
101
102
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
103
		if (entry->pn == pn_Switch_default)
Matthias Braun's avatar
Matthias Braun committed
104
105
			continue;

Matthias Braun's avatar
Matthias Braun committed
106
107
		target_t *target = &targets[entry->pn];
		++target->n_entries;
Matthias Braun's avatar
Matthias Braun committed
108
	}
109

Matthias Braun's avatar
Matthias Braun committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	info->default_block = targets[pn_Switch_default].block;
	info->targets       = targets;
}

static int compare_entries(const void *a, const void *b)
{
	const ir_switch_table_entry *entry0 = (const ir_switch_table_entry*)a;
	const ir_switch_table_entry *entry1 = (const ir_switch_table_entry*)b;

	/* sort default targets to the end */
	if (entry0->pn == pn_Switch_default)
		return 1;
	if (entry1->pn == pn_Switch_default)
		return -1;
Matthias Braun's avatar
Matthias Braun committed
124

Matthias Braun's avatar
Matthias Braun committed
125
126
127
128
129
	if (tarval_cmp(entry0->max, entry1->min) == ir_relation_less)
		return -1;
	/* cases must be non overlapping, so the only remaining case is greater */
	assert(tarval_cmp(entry0->min, entry1->max) == ir_relation_greater);
	return 1;
Matthias Braun's avatar
Matthias Braun committed
130
131
132
133
134
}

static void normalize_table(ir_node *switchn, ir_mode *new_mode,
                            ir_tarval *delta)
{
135
	ir_switch_table *table = get_Switch_table(switchn);
Matthias Braun's avatar
Matthias Braun committed
136
137
138
	QSORT(table->entries, table->n_entries, compare_entries);

	/* subtract delta from min/max values and remove default pn entries */
139
140
	for (size_t e = 0, n_entries = ir_switch_table_get_n_entries(table);
	     e < n_entries; ++e) {
Matthias Braun's avatar
Matthias Braun committed
141
		ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
Matthias Braun's avatar
Matthias Braun committed
142
143
144
145
146
147

		/* default entries have been sorted to the end, cut off list there */
		if (entry->pn == pn_Switch_default) {
			table->n_entries = e;
			break;
		}
Matthias Braun's avatar
Matthias Braun committed
148

149
		ir_tarval *min = entry->min;
Matthias Braun's avatar
Matthias Braun committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
		min = tarval_convert_to(min, new_mode);
		if (delta != NULL)
			min = tarval_sub(min, delta, NULL);

		if (entry->min == entry->max) {
			entry->min = min;
			entry->max = min;
		} else {
			ir_tarval *max = entry->max;
			max = tarval_convert_to(max, new_mode);
			if (delta != NULL)
				max = tarval_sub(max, delta, NULL);
			entry->min = min;
			entry->max = max;
		}
	}
}

168
169
static void create_out_of_bounds_check(switch_info_t *info)
{
170
	/* should already be normalized to zero */
171
172
	assert(tarval_is_null(info->switch_min));

173
174
175
176
177
178
179
180
181
182
183
184
185
186
	/* create out-of-bounds check */
	ir_node  *switchn    = info->switchn;
	ir_graph *irg        = get_irn_irg(switchn);
	dbg_info *dbgi       = get_irn_dbg_info(switchn);
	ir_node  *selector   = get_Switch_selector(switchn);
	ir_node  *block      = get_nodes_block(switchn);
	ir_node  *max_const  = new_r_Const(irg, info->switch_max);
	ir_node  *cmp        = new_rd_Cmp(dbgi, block, selector, max_const,
	                                  ir_relation_less_equal);
	ir_node  *oob_cond   = new_rd_Cond(dbgi, block, cmp);
	ir_node  *proj_true  = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
	ir_node  *proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);

	ir_node **default_preds = NEW_ARR_F(ir_node*, 0);
187
188
189
	ARR_APP1(ir_node*, default_preds, proj_false);

	/* create new block containing the switch */
190
191
	ir_node *in[]      = { proj_true };
	ir_node *new_block = new_r_Block(irg, ARRAY_SIZE(in), in);
192
193
194
	set_nodes_block(switchn, new_block);

	/* adjust projs */
195
	ir_node *default_block = NULL;
196
	foreach_irn_out_r(switchn, i, proj) {
197
198
199
200
201
202
203
204
205
206
		long pn = get_Proj_proj(proj);
		if (pn == pn_Switch_default) {
			assert(default_block == NULL);
			default_block = get_irn_out(proj, 0);
			ARR_APP1(ir_node*, default_preds, proj);
		}
		set_nodes_block(proj, new_block);
	}

	/* adapt default block */
207
	size_t n_default_preds = ARR_LEN(default_preds);
208
209
	if (n_default_preds > 1) {
		/* create new intermediate blocks so we don't have critical edges */
210
211
212
213
		for (size_t p = 0; p < n_default_preds; ++p) {
			ir_node *pred        = default_preds[p];
			ir_node *bin[]       = { pred };
			ir_node *split_block = new_r_Block(irg, ARRAY_SIZE(bin), bin);
214
215
216
217
218
219
220
221
222

			default_preds[p] = new_r_Jmp(split_block);
		}
	}
	set_irn_in(default_block, n_default_preds, default_preds);

	DEL_ARR_F(default_preds);
}

Matthias Braun's avatar
Matthias Braun committed
223
224
225
/**
 * normalize switch to work on an unsigned input with the first case at 0
 */
226
static bool normalize_switch(switch_info_t *info, ir_mode *selector_mode)
Matthias Braun's avatar
Matthias Braun committed
227
{
Matthias Braun's avatar
Matthias Braun committed
228
229
230
231
232
	ir_node   *switchn  = info->switchn;
	ir_node   *block    = get_nodes_block(switchn);
	ir_node   *selector = get_Switch_selector(switchn);
	ir_mode   *mode     = get_irn_mode(selector);
	ir_tarval *min      = info->switch_min;
Matthias Braun's avatar
Matthias Braun committed
233
	if (mode_is_signed(mode)) {
234
235
236
		mode             = find_unsigned_mode(mode);
		selector         = new_r_Conv(block, selector, mode);
		min              = tarval_convert_to(min, mode);
237
		info->switch_min = min;
238
		info->switch_max = tarval_convert_to(info->switch_max, mode);
Matthias Braun's avatar
Matthias Braun committed
239
240
241
	}

	/* normalize so switch_min is at 0 */
Matthias Braun's avatar
Matthias Braun committed
242
	ir_tarval *delta = NULL;
243
	if (min != get_mode_null(mode)) {
244
		ir_graph *irg       = get_irn_irg(switchn);
245
246
247
		ir_node  *min_const = new_r_Const(irg, min);
		dbg_info *dbgi      = get_irn_dbg_info(switchn);
		selector = new_rd_Sub(dbgi, block, selector, min_const, mode);
Matthias Braun's avatar
Matthias Braun committed
248

249
250
251
		info->switch_max = tarval_sub(info->switch_max, min, mode);
		info->switch_min = get_mode_null(mode);
		delta            = min;
Matthias Braun's avatar
Matthias Braun committed
252
253
	}

254
255
256
257
258
259
260
261
262
263
264
265
266
	/* if we have a selector_mode set, then the we will have a switch node,
	 * we have to construct an out-of-bounds check then and after that convert
	 * the switch/selector to the backends desired switch mode */
	if (selector_mode != NULL) {
		set_Switch_selector(switchn, selector);
		create_out_of_bounds_check(info);

		selector = new_r_Conv(block, selector, selector_mode);
		mode     = selector_mode;
		info->switch_min = tarval_convert_to(info->switch_min, mode);
		info->switch_max = tarval_convert_to(info->switch_max, mode);
		if (delta != NULL)
			delta = tarval_convert_to(delta, mode);
Matthias Braun's avatar
Matthias Braun committed
267
		set_Switch_selector(switchn, selector);
268
269
	}

270
271
	normalize_table(switchn, mode, delta);
	return true;
Matthias Braun's avatar
Matthias Braun committed
272
273
274
275
276
277
278
279
280
281
282
283
284
}

/**
 * Create an if (selector == caseval) Cond node (and handle the special case
 * of ranged cases)
 */
static ir_node *create_case_cond(const ir_switch_table_entry *entry,
                                 dbg_info *dbgi, ir_node *block,
                                 ir_node *selector)
{
	ir_graph *irg      = get_irn_irg(block);
	ir_node  *minconst = new_r_Const(irg, entry->min);

285
	ir_node  *cmp;
Matthias Braun's avatar
Matthias Braun committed
286
287
288
289
290
291
292
293
294
295
	if (entry->min == entry->max) {
		cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
	} else {
		ir_tarval *adjusted_max = tarval_sub(entry->max, entry->min, NULL);
		ir_node   *sub          = new_rd_Sub(dbgi, block, selector, minconst,
		                                     get_tarval_mode(adjusted_max));
		ir_node   *maxconst     = new_r_Const(irg, adjusted_max);
		cmp = new_rd_Cmp(dbgi, block, sub, maxconst, ir_relation_less_equal);
	}
	return new_rd_Cond(dbgi, block, cmp);
296
297
}

Matthias Braun's avatar
Matthias Braun committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
static void connect_to_target(target_t *target, ir_node *cf)
{
	ir_node *block     = target->block;
	unsigned n_entries = target->n_entries;
	if (get_Block_n_cfgpreds(block) != (int)n_entries) {
		ir_node **new_in = ALLOCAN(ir_node*, n_entries);
		ir_graph *irg    = get_Block_irg(block);
		ir_node  *dummy  = new_r_Dummy(irg, mode_X);
		for (unsigned i = 0; i < n_entries; ++i) {
			new_in[i] = dummy;
		}
		set_irn_in(block, n_entries, new_in);
		assert(target->i == 0);
	}
	assert(target->i < n_entries);
	set_Block_cfgpred(target->block, target->i++, cf);
}

316
317
318
/**
 * Creates an if cascade realizing binary search.
 */
Matthias Braun's avatar
Matthias Braun committed
319
static void create_if_cascade(switch_info_t *info, ir_node *block,
Matthias Braun's avatar
Matthias Braun committed
320
321
                              ir_switch_table_entry *curcases,
                              unsigned numcases)
322
{
Matthias Braun's avatar
Matthias Braun committed
323
324
325
326
	ir_graph      *irg      = get_irn_irg(block);
	const ir_node *switchn  = info->switchn;
	dbg_info      *dbgi     = get_irn_dbg_info(switchn);
	ir_node       *selector = get_Switch_selector(switchn);
327

328
329
	if (numcases == 0) {
		/* zero cases: "goto default;" */
Matthias Braun's avatar
Matthias Braun committed
330
		ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
331
	} else if (numcases == 1) {
Matthias Braun's avatar
Matthias Braun committed
332
		/*only one case: "if (sel == val) goto target else goto default;"*/
Matthias Braun's avatar
Matthias Braun committed
333
		const ir_switch_table_entry *entry = &curcases[0];
Matthias Braun's avatar
Matthias Braun committed
334
		ir_node *cond      = create_case_cond(entry, dbgi, block, selector);
335
336
337
		ir_node *trueproj  = new_r_Proj(cond, mode_X, pn_Cond_true);
		ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);

Matthias Braun's avatar
Matthias Braun committed
338
		connect_to_target(&info->targets[entry->pn], trueproj);
Matthias Braun's avatar
Matthias Braun committed
339
		ARR_APP1(ir_node*, info->defusers, falseproj);
340
	} else if (numcases == 2) {
341
		/* only two cases: "if (sel == val[0]) goto target[0];" */
Matthias Braun's avatar
Matthias Braun committed
342
343
		const ir_switch_table_entry *entry0 = &curcases[0];
		const ir_switch_table_entry *entry1 = &curcases[1];
Matthias Braun's avatar
Matthias Braun committed
344
		ir_node *cond      = create_case_cond(entry0, dbgi, block, selector);
345
346
		ir_node *trueproj  = new_r_Proj(cond, mode_X, pn_Cond_true);
		ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
Matthias Braun's avatar
Matthias Braun committed
347
		connect_to_target(&info->targets[entry0->pn], trueproj);
348

349
350
		ir_node *in[]    = { falseproj };
		ir_node *neblock = new_r_Block(irg, ARRAY_SIZE(in), in);
351

352
		/* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
353
354
355
		ir_node *cond1      = create_case_cond(entry1, dbgi, neblock, selector);
		ir_node *trueproj1  = new_r_Proj(cond1, mode_X, pn_Cond_true);
		ir_node *falseproj1 = new_r_Proj(cond1, mode_X, pn_Cond_false);
Matthias Braun's avatar
Matthias Braun committed
356
		connect_to_target(&info->targets[entry1->pn], trueproj1);
357
		ARR_APP1(ir_node*, info->defusers, falseproj1);
358
359
	} else {
		/* recursive case: split cases in the middle */
Matthias Braun's avatar
Matthias Braun committed
360
		unsigned midcase = numcases / 2;
Matthias Braun's avatar
Matthias Braun committed
361
		const ir_switch_table_entry *entry = &curcases[midcase];
Matthias Braun's avatar
Matthias Braun committed
362
363
		ir_node *val = new_r_Const(irg, entry->min);
		ir_node *cmp = new_rd_Cmp(dbgi, block, selector, val, ir_relation_less);
364
		ir_node *cond = new_rd_Cond(dbgi, block, cmp);
365

366
367
		ir_node *ltin[]  = { new_r_Proj(cond, mode_X, pn_Cond_true) };
		ir_node *ltblock = new_r_Block(irg, ARRAY_SIZE(ltin), ltin);
368

369
370
		ir_node *gein[]  = { new_r_Proj(cond, mode_X, pn_Cond_false) };
		ir_node *geblock = new_r_Block(irg, ARRAY_SIZE(gein), gein);
371

Matthias Braun's avatar
Matthias Braun committed
372
373
		create_if_cascade(info, ltblock, curcases, midcase);
		create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
374
375
376
377
	}
}

/**
Matthias Braun's avatar
Matthias Braun committed
378
 * Block-Walker: searches for Switch nodes
379
 */
Matthias Braun's avatar
Matthias Braun committed
380
static void find_switch_nodes(ir_node *block, void *ctx)
381
{
382
	walk_env_t *env = (walk_env_t*)ctx;
383

384
385
	/* because we split critical blocks only blocks with 1 predecessors may
	 * contain Proj->Cond nodes */
386
	if (get_Block_n_cfgpreds(block) != 1)
387
388
		return;

389
	ir_node *projx = get_Block_cfgpred(block, 0);
390
	if (!is_Proj(projx))
391
392
393
		return;
	assert(get_irn_mode(projx) == mode_X);

394
	ir_node *switchn = get_Proj_pred(projx);
Matthias Braun's avatar
Matthias Braun committed
395
	if (!is_Switch(switchn))
396
		return;
397
	if (!ir_nodeset_insert(&env->processed, switchn))
398
399
		return;

400
	switch_info_t info;
401
	analyse_switch0(&info, switchn);
402

403
404
405
406
	/*
	 * Here we have: num_cases and [switch_min, switch_max] interval.
	 * We do an if-cascade if there are too many spare numbers.
	 */
Matthias Braun's avatar
Matthias Braun committed
407
408
409
	ir_mode   *selector_mode = get_irn_mode(get_Switch_selector(switchn));
	ir_tarval *spare = tarval_sub(info.switch_max, info.switch_min, selector_mode);
	ir_mode   *mode  = find_unsigned_mode(selector_mode);
410
411
412
413
414
	spare = tarval_convert_to(spare, mode);
	ir_tarval *num_cases_minus_one
		= new_tarval_from_long(info.num_cases-1, mode);
	spare = tarval_sub(spare, num_cases_minus_one, mode);
	ir_tarval *spare_size = new_tarval_from_long(env->spare_size, mode);
415
416
	bool lower_switch = info.num_cases <= env->small_switch
		|| (tarval_cmp(spare, spare_size) & ir_relation_greater_equal);
Matthias Braun's avatar
Matthias Braun committed
417
418

	if (!lower_switch) {
419
420
		/* we won't decompose the switch. But we must add an out-of-bounds
		 * check */
421
		env->changed |= normalize_switch(&info, env->selector_mode);
422
		return;
423
	}
424

Matthias Braun's avatar
Matthias Braun committed
425
	normalize_table(switchn, selector_mode, NULL);
Matthias Braun's avatar
Matthias Braun committed
426
	analyse_switch1(&info);
427
428

	/* Now create the if cascade */
429
	env->changed  = true;
Matthias Braun's avatar
Matthias Braun committed
430
	info.defusers = NEW_ARR_F(ir_node*, 0);
431
	block         = get_nodes_block(switchn);
Matthias Braun's avatar
Matthias Braun committed
432
433
	ir_switch_table *table = get_Switch_table(switchn);
	create_if_cascade(&info, block, table->entries, table->n_entries);
Moritz Kroll's avatar
Moritz Kroll committed
434
435

	/* Connect new default case users */
Matthias Braun's avatar
Matthias Braun committed
436
	set_irn_in(info.default_block, ARR_LEN(info.defusers), info.defusers);
437

Matthias Braun's avatar
Matthias Braun committed
438
	DEL_ARR_F(info.defusers);
Matthias Braun's avatar
Matthias Braun committed
439
	free(info.targets);
440
441
}

442
443
void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
                  ir_mode *selector_mode)
444
{
445
446
447
	if (mode_is_signed(selector_mode))
		panic("expected unsigned mode for switch selector");

448
	walk_env_t env;
449
	env.selector_mode       = selector_mode;
450
	env.spare_size          = spare_size;
Matthias Braun's avatar
Matthias Braun committed
451
	env.small_switch        = small_switch;
452
	env.changed             = false;
453
	ir_nodeset_init(&env.processed);
454

455
456
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
	                         | IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
457

Matthias Braun's avatar
Matthias Braun committed
458
	irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
459
	ir_nodeset_destroy(&env.processed);
460

461
462
	confirm_irg_properties(irg, env.changed ? IR_GRAPH_PROPERTIES_NONE
	                                        : IR_GRAPH_PROPERTIES_ALL);
463
}