lower_switch.c 15 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 <stdbool.h>
12

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

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

Matthias Braun's avatar
Matthias Braun committed
32
33
34
35
36
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;
37

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

Matthias Braun's avatar
Matthias Braun committed
48
49
50
/**
 * analyze enough to decide if we should lower the switch
 */
51
static void analyse_switch0(switch_info_t *info, ir_node *switchn)
52
{
53
54
55
56
57
58
59
60
	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
61
62
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
63
		if (entry->pn == pn_Switch_default)
64
65
			continue;

66
67
68
69
70
		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;

71
		++num_cases;
72
73
	}

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

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

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

Matthias Braun's avatar
Matthias Braun committed
97
	const ir_switch_table *table = get_Switch_table(switchn);
98
99
	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
100
101
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
102
		if (entry->pn == pn_Switch_default)
Matthias Braun's avatar
Matthias Braun committed
103
104
			continue;

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

Matthias Braun's avatar
Matthias Braun committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	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
123

Matthias Braun's avatar
Matthias Braun committed
124
125
126
127
128
	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
129
130
131
132
133
}

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

	/* subtract delta from min/max values and remove default pn entries */
138
139
	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
140
		ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
Matthias Braun's avatar
Matthias Braun committed
141
142
143
144
145
146

		/* 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
147

148
		ir_tarval *min = entry->min;
Matthias Braun's avatar
Matthias Braun committed
149
150
		min = tarval_convert_to(min, new_mode);
		if (delta != NULL)
151
			min = tarval_sub(min, delta);
Matthias Braun's avatar
Matthias Braun committed
152
153
154
155
156
157
158
159

		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)
160
				max = tarval_sub(max, delta);
Matthias Braun's avatar
Matthias Braun committed
161
162
163
164
165
166
			entry->min = min;
			entry->max = max;
		}
	}
}

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

172
173
174
175
176
177
178
179
180
181
182
183
184
185
	/* 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);
186
187
188
	ARR_APP1(ir_node*, default_preds, proj_false);

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

	/* adjust projs */
194
	ir_node *default_block = NULL;
195
	foreach_irn_out_r(switchn, i, proj) {
196
		unsigned pn = get_Proj_num(proj);
197
198
199
200
201
202
203
204
205
		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 */
206
	size_t n_default_preds = ARR_LEN(default_preds);
207
208
	if (n_default_preds > 1) {
		/* create new intermediate blocks so we don't have critical edges */
209
210
211
212
		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);
213
214
215
216
217
218
219
220
221

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

	/* normalize so switch_min is at 0 */
Matthias Braun's avatar
Matthias Braun committed
241
	ir_tarval *delta = NULL;
242
	if (min != get_mode_null(mode)) {
243
		ir_graph *irg       = get_irn_irg(switchn);
244
245
246
		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
247

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

253
254
255
256
257
258
259
260
261
262
263
264
265
	/* 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
266
		set_Switch_selector(switchn, selector);
267
268
	}

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

/**
 * 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);

284
	ir_node  *cmp;
Matthias Braun's avatar
Matthias Braun committed
285
286
287
	if (entry->min == entry->max) {
		cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
	} else {
288
		ir_tarval *adjusted_max = tarval_sub(entry->max, entry->min);
Matthias Braun's avatar
Matthias Braun committed
289
290
291
292
293
294
		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);
295
296
}

Matthias Braun's avatar
Matthias Braun committed
297
298
299
300
301
302
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);
303
		ir_graph *irg    = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
304
305
306
307
308
309
310
311
312
313
314
		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);
}

315
316
317
/**
 * Creates an if cascade realizing binary search.
 */
Matthias Braun's avatar
Matthias Braun committed
318
static void create_if_cascade(switch_info_t *info, ir_node *block,
Matthias Braun's avatar
Matthias Braun committed
319
320
                              ir_switch_table_entry *curcases,
                              unsigned numcases)
321
{
Matthias Braun's avatar
Matthias Braun committed
322
323
324
325
	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);
326

327
328
	if (numcases == 0) {
		/* zero cases: "goto default;" */
Matthias Braun's avatar
Matthias Braun committed
329
		ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
330
	} else if (numcases == 1) {
Matthias Braun's avatar
Matthias Braun committed
331
		/*only one case: "if (sel == val) goto target else goto default;"*/
Matthias Braun's avatar
Matthias Braun committed
332
		const ir_switch_table_entry *entry = &curcases[0];
Matthias Braun's avatar
Matthias Braun committed
333
		ir_node *cond      = create_case_cond(entry, dbgi, block, selector);
334
335
336
		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
337
		connect_to_target(&info->targets[entry->pn], trueproj);
Matthias Braun's avatar
Matthias Braun committed
338
		ARR_APP1(ir_node*, info->defusers, falseproj);
339
	} else if (numcases == 2) {
340
		/* only two cases: "if (sel == val[0]) goto target[0];" */
Matthias Braun's avatar
Matthias Braun committed
341
342
		const ir_switch_table_entry *entry0 = &curcases[0];
		const ir_switch_table_entry *entry1 = &curcases[1];
Matthias Braun's avatar
Matthias Braun committed
343
		ir_node *cond      = create_case_cond(entry0, dbgi, block, selector);
344
345
		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
346
		connect_to_target(&info->targets[entry0->pn], trueproj);
347

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

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

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

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

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

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

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

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

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

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

402
403
404
405
	/*
	 * 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
406
	ir_mode   *selector_mode = get_irn_mode(get_Switch_selector(switchn));
407
	ir_tarval *spare = tarval_sub(info.switch_max, info.switch_min);
Matthias Braun's avatar
Matthias Braun committed
408
	ir_mode   *mode  = find_unsigned_mode(selector_mode);
409
410
411
	spare = tarval_convert_to(spare, mode);
	ir_tarval *num_cases_minus_one
		= new_tarval_from_long(info.num_cases-1, mode);
412
	spare = tarval_sub(spare, num_cases_minus_one);
413
	ir_tarval *spare_size = new_tarval_from_long(env->spare_size, mode);
414
415
	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
416
417

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

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

	/* Now create the if cascade */
428
	env->changed  = true;
Matthias Braun's avatar
Matthias Braun committed
429
	info.defusers = NEW_ARR_F(ir_node*, 0);
430
	block         = get_nodes_block(switchn);
Matthias Braun's avatar
Matthias Braun committed
431
432
	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
433
434

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

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

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

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

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

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

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