lower_switch.c 14.7 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
18
#include "irgwalk.h"
#include "irnode_t.h"
19
#include "irouts_t.h"
20
#include "lowering.h"
21
#include "error.h"
22
#include "irnodeset.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;

32
typedef struct case_data_t {
Matthias Braun's avatar
Matthias Braun committed
33
34
	const ir_switch_table_entry *entry;
	ir_node                     *target;
35
36
} case_data_t;

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

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

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

70
		++num_cases;
71
72
	}

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

static int casecmp(const void *a, const void *b)
{
Matthias Braun's avatar
Matthias Braun committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	const case_data_t           *cda = (const case_data_t*)a;
	const case_data_t           *cdb = (const case_data_t*)b;
	const ir_switch_table_entry *ea  = cda->entry;
	const ir_switch_table_entry *eb  = cdb->entry;

	if (ea == eb)
		return 0;

	if (tarval_cmp(ea->max, eb->min) == ir_relation_less)
		return -1;
	/* cases must be non overlapping, so the only remaining case is greater */
	assert(tarval_cmp(ea->min, eb->max) == ir_relation_greater);
	return 1;
}

/**
 * Analyse the stuff that anayse_switch0() left out
 */
static void analyse_switch1(switch_info_t *info)
{
	const ir_node         *switchn   = info->switchn;
	const ir_switch_table *table     = get_Switch_table(switchn);
	size_t                 n_entries = ir_switch_table_get_n_entries(table);
	unsigned               n_outs    = get_Switch_n_outs(switchn);
	ir_node              **targets   = XMALLOCNZ(ir_node*, n_outs);
	unsigned               num_cases = info->num_cases;
	case_data_t           *cases     = XMALLOCN(case_data_t, num_cases);
	unsigned               c         = 0;
	size_t                 e;

111
	foreach_irn_out_r(switchn, i, proj) {
Matthias Braun's avatar
Matthias Braun committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		long     pn     = get_Proj_proj(proj);
		ir_node *target = get_irn_out(proj, 0);

		assert((unsigned)pn < n_outs);
		assert(targets[(unsigned)pn] == NULL);
		targets[(unsigned)pn] = target;
	}

	for (e = 0; e < n_entries; ++e) {
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
		if (entry->pn == 0)
			continue;

		cases[c].entry  = entry;
		cases[c].target = targets[entry->pn];
		++c;
	}
	assert(c == num_cases);
131
132

	/*
Matthias Braun's avatar
Matthias Braun committed
133
134
	 * Switch should be transformed into an if cascade.
	 * So first order the cases, so we can do a binary search on them.
135
	 */
Matthias Braun's avatar
Matthias Braun committed
136
137
138
139
	qsort(cases, num_cases, sizeof(cases[0]), casecmp);

	info->default_block = targets[pn_Switch_default];
	info->cases         = cases;
Matthias Braun's avatar
Matthias Braun committed
140
	free(targets);
Matthias Braun's avatar
Matthias Braun committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
}

static void normalize_table(ir_node *switchn, ir_mode *new_mode,
                            ir_tarval *delta)
{
	ir_switch_table *table     = get_Switch_table(switchn);
	size_t           n_entries = ir_switch_table_get_n_entries(table);
	size_t           e;
	/* adapt switch_table */
	for (e = 0; e < n_entries; ++e) {
		ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
		ir_tarval *min = entry->min;

		if (entry->pn == 0)
			continue;

		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;
		}
	}
}

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
static void create_out_of_bounds_check(switch_info_t *info)
{
	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   **default_preds = NEW_ARR_F(ir_node*, 0);
	ir_node    *default_block = NULL;
	ir_node    *max_const;
	ir_node    *proj_true;
	ir_node    *proj_false;
	ir_node    *cmp;
	ir_node    *oob_cond;
	ir_node    *in[1];
	ir_node    *new_block;
	size_t      n_default_preds;

	assert(tarval_is_null(info->switch_min));

	/* check for out-of-bounds */
	max_const  = new_r_Const(irg, info->switch_max);
	cmp        = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
	oob_cond   = new_rd_Cond(dbgi, block, cmp);
	proj_true  = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
	proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);

	ARR_APP1(ir_node*, default_preds, proj_false);

	/* create new block containing the switch */
	in[0] = proj_true;
	new_block = new_r_Block(irg, 1, in);
	set_nodes_block(switchn, new_block);

	/* adjust projs */
210
	foreach_irn_out_r(switchn, i, proj) {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
		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 */
	n_default_preds = ARR_LEN(default_preds);
	if (n_default_preds > 1) {
		/* create new intermediate blocks so we don't have critical edges */
		size_t p;
		for (p = 0; p < n_default_preds; ++p) {
			ir_node *pred = default_preds[p];
			ir_node *split_block;
			ir_node *block_in[1];

			block_in[0] = pred;
			split_block = new_r_Block(irg, 1, block_in);

			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
241
242
243
/**
 * normalize switch to work on an unsigned input with the first case at 0
 */
244
static bool normalize_switch(switch_info_t *info, ir_mode *selector_mode)
Matthias Braun's avatar
Matthias Braun committed
245
{
246
247
248
249
	ir_node   *switchn         = info->switchn;
	ir_graph  *irg             = get_irn_irg(switchn);
	ir_node   *block           = get_nodes_block(switchn);
	ir_node   *selector        = get_Switch_selector(switchn);
250
	ir_mode   *mode            = get_irn_mode(selector);
251
252
253
254
	ir_tarval *delta           = NULL;
	bool       needs_normalize = false;

	ir_tarval *min = info->switch_min;
Matthias Braun's avatar
Matthias Braun committed
255
	if (mode_is_signed(mode)) {
256
257
258
		mode             = find_unsigned_mode(mode);
		selector         = new_r_Conv(block, selector, mode);
		min              = tarval_convert_to(min, mode);
259
		info->switch_min = min;
260
		info->switch_max = tarval_convert_to(info->switch_max, mode);
261
		needs_normalize  = true;
Matthias Braun's avatar
Matthias Braun committed
262
263
264
	}

	/* normalize so switch_min is at 0 */
265
266
267
268
	if (min != get_mode_null(mode)) {
		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
269

270
271
272
		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
273

274
		needs_normalize = true;
Matthias Braun's avatar
Matthias Braun committed
275
276
	}

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
	/* 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);
		needs_normalize = true;
	}

293
294
295
296
297
298
	if (!needs_normalize)
		return false;

	set_Switch_selector(switchn, selector);
	normalize_table(switchn, mode, delta);
	return true;
Matthias Braun's avatar
Matthias Braun committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
}

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

	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);
324
325
326
327
328
}

/**
 * Creates an if cascade realizing binary search.
 */
Matthias Braun's avatar
Matthias Braun committed
329
static void create_if_cascade(switch_info_t *info, ir_node *block,
330
                              case_data_t *curcases, unsigned numcases)
331
{
Matthias Braun's avatar
Matthias Braun committed
332
333
334
335
	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);
336

337
338
	if (numcases == 0) {
		/* zero cases: "goto default;" */
Matthias Braun's avatar
Matthias Braun committed
339
		ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
340
	} else if (numcases == 1) {
Matthias Braun's avatar
Matthias Braun committed
341
342
343
		/*only one case: "if (sel == val) goto target else goto default;"*/
		const ir_switch_table_entry *entry = curcases[0].entry;
		ir_node *cond      = create_case_cond(entry, dbgi, block, selector);
344
345
346
347
		ir_node *trueproj  = new_r_Proj(cond, mode_X, pn_Cond_true);
		ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);

		set_Block_cfgpred(curcases[0].target, 0, trueproj);
Matthias Braun's avatar
Matthias Braun committed
348
		ARR_APP1(ir_node*, info->defusers, falseproj);
349
	} else if (numcases == 2) {
350
		/* only two cases: "if (sel == val[0]) goto target[0];" */
Matthias Braun's avatar
Matthias Braun committed
351
352
353
		const ir_switch_table_entry *entry0 = curcases[0].entry;
		const ir_switch_table_entry *entry1 = curcases[1].entry;
		ir_node *cond      = create_case_cond(entry0, dbgi, block, selector);
354
355
		ir_node *trueproj  = new_r_Proj(cond, mode_X, pn_Cond_true);
		ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
356
357
358
		ir_node *in[1];
		ir_node *neblock;

359
360
361
362
		set_Block_cfgpred(curcases[0].target, 0, trueproj);

		in[0] = falseproj;
		neblock = new_r_Block(irg, 1, in);
363

364
		/* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
Matthias Braun's avatar
Matthias Braun committed
365
		cond      = create_case_cond(entry1, dbgi, neblock, selector);
366
367
368
		trueproj  = new_r_Proj(cond, mode_X, pn_Cond_true);
		falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
		set_Block_cfgpred(curcases[1].target, 0, trueproj);
Matthias Braun's avatar
Matthias Braun committed
369
		ARR_APP1(ir_node*, info->defusers, falseproj);
370
371
	} else {
		/* recursive case: split cases in the middle */
Matthias Braun's avatar
Matthias Braun committed
372
373
374
375
		unsigned midcase = numcases / 2;
		const ir_switch_table_entry *entry = curcases[midcase].entry;
		ir_node *val = new_r_Const(irg, entry->min);
		ir_node *cmp = new_rd_Cmp(dbgi, block, selector, val, ir_relation_less);
376
		ir_node *cond = new_rd_Cond(dbgi, block, cmp);
377
378
379
380
		ir_node *in[1];
		ir_node *ltblock;
		ir_node *geblock;

381
382
		in[0]   = new_r_Proj(cond, mode_X, pn_Cond_true);
		ltblock = new_r_Block(irg, 1, in);
383

384
385
		in[0]   = new_r_Proj(cond, mode_X, pn_Cond_false);
		geblock = new_r_Block(irg, 1, in);
386

Matthias Braun's avatar
Matthias Braun committed
387
388
		create_if_cascade(info, ltblock, curcases, midcase);
		create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
389
390
391
392
	}
}

/**
Matthias Braun's avatar
Matthias Braun committed
393
 * Block-Walker: searches for Switch nodes
394
 */
Matthias Braun's avatar
Matthias Braun committed
395
static void find_switch_nodes(ir_node *block, void *ctx)
396
{
Matthias Braun's avatar
Matthias Braun committed
397
398
399
400
	walk_env_t   *env = (walk_env_t *)ctx;
	ir_node      *projx;
	ir_node      *switchn;
	switch_info_t info;
401

402
403
	/* because we split critical blocks only blocks with 1 predecessors may
	 * contain Proj->Cond nodes */
404
	if (get_Block_n_cfgpreds(block) != 1)
405
406
407
		return;

	projx = get_Block_cfgpred(block, 0);
408
	if (!is_Proj(projx))
409
410
411
		return;
	assert(get_irn_mode(projx) == mode_X);

Matthias Braun's avatar
Matthias Braun committed
412
413
	switchn = get_Proj_pred(projx);
	if (!is_Switch(switchn))
414
415
		return;

416
	if (!ir_nodeset_insert(&env->processed, switchn))
417
418
		return;

419
	analyse_switch0(&info, switchn);
420

421
422
423
424
	/*
	 * Here we have: num_cases and [switch_min, switch_max] interval.
	 * We do an if-cascade if there are too many spare numbers.
	 */
425
426
427
428
429
430
431
432
433
434
	ir_mode   *mode  = get_irn_mode(get_Switch_selector(switchn));
	ir_tarval *spare = tarval_sub(info.switch_max, info.switch_min, mode);
	mode  = find_unsigned_mode(mode);
	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);
	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
435
436

	if (!lower_switch) {
437
438
		/* we won't decompose the switch. But we must add an out-of-bounds
		 * check */
439
		env->changed |= normalize_switch(&info, env->selector_mode);
440
		return;
441
	}
442

443
	normalize_switch(&info, NULL);
Matthias Braun's avatar
Matthias Braun committed
444
	analyse_switch1(&info);
445
446

	/* Now create the if cascade */
447
	env->changed  = true;
Matthias Braun's avatar
Matthias Braun committed
448
	info.defusers = NEW_ARR_F(ir_node*, 0);
449
	block         = get_nodes_block(switchn);
Matthias Braun's avatar
Matthias Braun committed
450
	create_if_cascade(&info, block, info.cases, info.num_cases);
Moritz Kroll's avatar
Moritz Kroll committed
451
452

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

Matthias Braun's avatar
Matthias Braun committed
455
	DEL_ARR_F(info.defusers);
456
	free(info.cases);
457
458
}

459
460
void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
                  ir_mode *selector_mode)
461
{
462
463
464
	if (mode_is_signed(selector_mode))
		panic("expected unsigned mode for switch selector");

465
	walk_env_t env;
466
	env.selector_mode       = selector_mode;
467
	env.spare_size          = spare_size;
Matthias Braun's avatar
Matthias Braun committed
468
	env.small_switch        = small_switch;
469
	env.changed             = false;
470
	ir_nodeset_init(&env.processed);
471

472
473
474
	assure_irg_properties(irg,
		IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
		| IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
475

Matthias Braun's avatar
Matthias Braun committed
476
	irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
477
	ir_nodeset_destroy(&env.processed);
478

479
480
	confirm_irg_properties(irg, env.changed ? IR_GRAPH_PROPERTIES_NONE
	                                        : IR_GRAPH_PROPERTIES_ALL);
481
}