opt_osr.c 34.9 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
/**
Michael Beck's avatar
Michael Beck committed
7
8
9
10
 * @file
 * @brief   Operator Strength Reduction.
 * @date    12.5.2006
 * @author  Michael Beck
11
 * @brief
Michael Beck's avatar
Michael Beck committed
12
13
 *  Implementation of the Operator Strength Reduction algorithm
 *  by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
14
 *  Extended version.
15
 */
Matthias Braun's avatar
Matthias Braun committed
16
17
#include <stdbool.h>

18
#include "adt/pdeq.h"
Matthias Braun's avatar
Matthias Braun committed
19
20
#include "array.h"
#include "debug.h"
Matthias Braun's avatar
Matthias Braun committed
21
#include "panic.h"
Matthias Braun's avatar
Matthias Braun committed
22
#include "hashptr.h"
23
#include "ircons.h"
24
#include "irdom.h"
25
#include "iredges_t.h"
26
#include "irflag_t.h"
Matthias Braun's avatar
Matthias Braun committed
27
#include "irgmod.h"
28
#include "irgwalk.h"
Matthias Braun's avatar
Matthias Braun committed
29
#include "irhooks.h"
Matthias Braun's avatar
Matthias Braun committed
30
31
32
#include "irloop_t.h"
#include "irop_t.h"
#include "iroptimize.h"
Michael Beck's avatar
Michael Beck committed
33
#include "irouts.h"
Matthias Braun's avatar
Matthias Braun committed
34
#include "irtools.h"
35
#include "obst.h"
36
37
#include "set.h"
#include "tv.h"
38
#include "util.h"
39
40
41
42
43
44

/** The debug handle. */
DEBUG_ONLY(static firm_dbg_module_t *dbg;)

/** A scc. */
typedef struct scc {
Matthias Braun's avatar
Matthias Braun committed
45
46
	ir_node   *head; /**< the head of the list */
	ir_tarval *init; /**< the init value iff only one exists. */
Matthias Braun's avatar
Matthias Braun committed
47
48
49
	ir_tarval *incr; /**< the induction variable increment if only a single
	                      const exists. */
	unsigned   code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
50
	                         analyzed, iro_Bad else */
51
52
53
54
} scc;

/** A node entry */
typedef struct node_entry {
Matthias Braun's avatar
Matthias Braun committed
55
56
	unsigned  DFSnum;   /**< the DFS number of this node */
	unsigned  low;      /**< the low number of this node */
57
	ir_node  *header;   /**< the header of this node */
Matthias Braun's avatar
Matthias Braun committed
58
	bool      in_stack; /**< flag, set if the node is on the stack */
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
59
60
	ir_node  *next;     /**< link to the next node the same SCC */
	scc      *pscc;     /**< the SCC of this node */
Matthias Braun's avatar
Matthias Braun committed
61
	unsigned  POnum;    /**< the post order number for blocks */
62
63
64
65
} node_entry;

/** The environment. */
typedef struct iv_env {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
66
67
68
69
70
71
72
73
74
75
76
77
78
	struct     obstack obst;  /**< an obstack for allocations */
	ir_node  **stack;         /**< the node stack */
	size_t     tos;           /**< tos index */
	unsigned   nextDFSnum;    /**< the current DFS number */
	unsigned   POnum;         /**< current post order number */
	set       *quad_map;      /**< a map from (op, iv, rc) to node */
	set       *lftr_edges;    /**< the set of lftr edges */
	unsigned   replaced;      /**< number of replaced ops */
	unsigned   lftr_replaced; /**< number of applied linear function test
	                               replacements */
	unsigned   osr_flags;     /**< additional flags steering the transformation */
	bool       need_postpass; /**< set, if a post pass is needed to fix Add and
	                               Sub nodes */
79
80
	/** Function called to process a SCC. */
	void (*process_scc)(scc *pscc, struct iv_env *env);
81
82
83
} iv_env;

/**
84
85
 * An entry in the (op, node, node) -> node map.
 */
86
typedef struct quadruple_t {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
87
88
89
90
	unsigned  code; /**< the opcode of the reduced operation */
	ir_node  *op1;  /**< the first operand the reduced operation */
	ir_node  *op2;  /**< the second operand of the reduced operation */
	ir_node  *res;  /**< the reduced operation */
91
} quadruple_t;
92
93
94
95

/**
 * A LFTR edge.
 */
Matthias Braun's avatar
Matthias Braun committed
96
typedef struct ldtr_edge_t {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
97
98
99
100
	ir_node  *src;  /**< the source node */
	ir_node  *dst;  /**< the destination node */
	unsigned  code; /**< the opcode that must be applied */
	ir_node  *rc;   /**< the region const that must be applied */
Matthias Braun's avatar
Matthias Braun committed
101
} ldtr_edge_t;
102
103
104
105
106
107
108

/* forward */
static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);

/**
 * Compare two LFTR edges.
 */
Matthias Braun's avatar
Matthias Braun committed
109
static int lftr_cmp(const void *e1, const void *e2, size_t size)
110
{
Matthias Braun's avatar
Matthias Braun committed
111
112
113
	(void)size;
	const ldtr_edge_t *l1 = (const ldtr_edge_t*)e1;
	const ldtr_edge_t *l2 = (const ldtr_edge_t*)e2;
114
	return l1->src != l2->src;
115
}
116
117
118

/**
 * Find a LFTR edge.
119
120
 *
 * @param src  the source node of the transition
121
 */
Matthias Braun's avatar
Matthias Braun committed
122
static ldtr_edge_t *lftr_find(ir_node *src, iv_env *env)
123
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
124
	ldtr_edge_t key = { .src = src };
Matthias Braun's avatar
Matthias Braun committed
125
126
	return set_find(ldtr_edge_t, env->lftr_edges, &key, sizeof(key),
	                hash_ptr(src));
127
}
128
129
130

/**
 * Add a LFTR edge.
131
132
133
134
135
136
 *
 * @param src   the source node of the edge
 * @param dst   the destination node of the edge
 * @param code  the opcode of the transformed transition
 * @param rc    the region const used in the transition
 * @param env   the environment
137
 */
Matthias Braun's avatar
Matthias Braun committed
138
139
static void lftr_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc,
                     iv_env *env)
140
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
141
	ldtr_edge_t key = { .src = src, .dst = dst, .code = code, .rc = rc };
142

Michael Beck's avatar
Michael Beck committed
143
144
145
146
	/*
	 * There might be more than one edge here. This is rather bad
	 * because we currently store only one.
	 */
Matthias Braun's avatar
Matthias Braun committed
147
148
	(void)set_insert(ldtr_edge_t, env->lftr_edges, &key, sizeof(key),
	                 hash_ptr(src));
149
}
150
151

/**
152
153
154
155
 * Gets the node_entry of a node.
 *
 * @param irn  the node
 * @param env  the environment
156
 */
157
158
static node_entry *get_irn_ne(ir_node *irn, iv_env *env)
{
159
	node_entry *e = (node_entry*)get_irn_link(irn);
160

161
	if (e == NULL) {
162
		e = OALLOCZ(&env->obst, node_entry);
163
164
165
		set_irn_link(irn, e);
	}
	return e;
166
}
167

Michael Beck's avatar
Michael Beck committed
168
/**
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
169
 * Gets the SCC from an induction variable.
170
171
172
 *
 * @param iv   any node of the induction variable
 * @param env  the environment
Michael Beck's avatar
Michael Beck committed
173
 */
174
175
static scc *get_iv_scc(ir_node *iv, iv_env *env)
{
Michael Beck's avatar
Michael Beck committed
176
177
	node_entry *e = get_irn_ne(iv, env);
	return e->pscc;
178
}
Michael Beck's avatar
Michael Beck committed
179

180
181
182
/**
 * Check if irn is an IV.
 *
Michael Beck's avatar
Michael Beck committed
183
184
185
 * @param irn  the node to check
 * @param env  the environment
 *
186
187
 * @returns the header if it is one, NULL else
 */
188
189
static ir_node *is_iv(ir_node *irn, iv_env *env)
{
190
	return get_irn_ne(irn, env)->header;
191
}
192
193
194

/**
 * Check if irn is a region constant.
Michael Beck's avatar
Michael Beck committed
195
196
197
198
 * The block or irn must strictly dominate the header block.
 *
 * @param irn           the node to check
 * @param header_block  the header block of the induction variable
199
 */
Matthias Braun's avatar
Matthias Braun committed
200
static bool is_rc(ir_node *irn, ir_node *header_block)
201
{
202
203
	ir_node *block = get_nodes_block(irn);

Michael Beck's avatar
Michael Beck committed
204
	return (block != header_block) && block_dominates(block, header_block);
205
}
206

207
208
209
/**
 * Set compare function for the quad set.
 */
210
211
static int quad_cmp(const void *e1, const void *e2, size_t size)
{
Matthias Braun's avatar
Matthias Braun committed
212
	(void)size;
213
214
	const quadruple_t *c1 = (const quadruple_t*)e1;
	const quadruple_t *c2 = (const quadruple_t*)e2;
215
	return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
216
}
217
218
219

/**
 * Check if an reduced operation was already calculated.
Michael Beck's avatar
Michael Beck committed
220
221
222
223
224
225
226
 *
 * @param code  the opcode of the operation
 * @param op1   the first operand of the operation
 * @param op2   the second operand of the operation
 * @param env   the environment
 *
 * @return the already reduced node or NULL if this operation is not yet reduced
227
 */
228
static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env)
229
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
230
231
	quadruple_t  key   = { .code = code, .op1 = op1, .op2 = op2 };
	unsigned     hash  = (code*9) ^ hash_ptr(op1) ^ hash_ptr(op2);
Matthias Braun's avatar
Matthias Braun committed
232
233
234
	quadruple_t *entry = set_find(quadruple_t, env->quad_map, &key, sizeof(key),
	                              hash);
	if (entry != NULL)
235
236
		return entry->res;
	return NULL;
237
}
238
239

/**
Michael Beck's avatar
Michael Beck committed
240
241
242
243
244
245
246
 * Add an reduced operation.
 *
 * @param code    the opcode of the operation
 * @param op1     the first operand of the operation
 * @param op2     the second operand of the operation
 * @param result  the result of the reduced operation
 * @param env     the environment
247
 */
Matthias Braun's avatar
Matthias Braun committed
248
249
static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result,
                iv_env *env)
250
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
251
252
	quadruple_t key  = { .code = code, .op1 = op1, .op2 = op2, .res = result };
	unsigned    hash = (code*9) ^ hash_ptr(op1) ^ hash_ptr(op2);
Matthias Braun's avatar
Matthias Braun committed
253
	(void)set_insert(quadruple_t, env->quad_map, &key, sizeof(key), hash);
254
}
255
256
257
258
259

/**
 * Find a location where to place a bin-op whose operands are in
 * block1 and block2.
 *
Michael Beck's avatar
Michael Beck committed
260
261
262
 * @param block1  the block of the first operand
 * @param block2  the block of the second operand
 *
263
264
265
266
 * Note that we know here that such a place must exists. Moreover, this means
 * that either block1 dominates block2 or vice versa. So, just return
 * the "smaller" one.
 */
267
268
static ir_node *find_location(ir_node *block1, ir_node *block2)
{
269
	if (block_dominates(block1, block2))
Michael Beck's avatar
Michael Beck committed
270
		return block2;
271
	assert(block_dominates(block2, block1));
Michael Beck's avatar
Michael Beck committed
272
	return block1;
273
}
274
275

/**
Michael Beck's avatar
Michael Beck committed
276
277
278
279
280
281
282
283
 * Create a node that executes an op1 code op1 operation.
 *
 * @param code   the opcode to execute
 * @param db     debug info to add to the new node
 * @param op1    the first operand
 * @param op2    the second operand
 *
 * @return the newly created node
284
 */
285
static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2)
286
{
287
288
289
	ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
	switch (code) {
	case iro_Mul:
290
		return new_rd_Mul(db, block, op1, op2);
291
	case iro_Add:
292
		return new_rd_Add(db, block, op1, op2);
293
	case iro_Sub:
294
		return new_rd_Sub(db, block, op1, op2);
295
	default:
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
296
		panic("unsupported opcode");
297
	}
298
}
299
300
301

/**
 * The Apply operation.
Michael Beck's avatar
Michael Beck committed
302
303
304
305
306
 *
 * @param orig   the node that represent the original operation and determines
 *               the opcode, debug-info and mode of a newly created one
 * @param op1    the first operand
 * @param op2    the second operand
307
 * @param env    the environment
Michael Beck's avatar
Michael Beck committed
308
309
 *
 * @return the newly created node
310
 */
Matthias Braun's avatar
Matthias Braun committed
311
312
static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1,
                      ir_node *op2, iv_env *env)
313
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
314
315
	unsigned  code   = get_irn_opcode(orig);
	ir_node  *result = search(code, op1, op2, env);
316

Michael Beck's avatar
Bugfix:    
Michael Beck committed
317
	if (result == NULL) {
Matthias Braun's avatar
Matthias Braun committed
318
319
320
		dbg_info *db         = get_irn_dbg_info(orig);
		ir_node  *op1_header = get_irn_ne(op1, env)->header;
		ir_node  *op2_header = get_irn_ne(op2, env)->header;
321

Michael Beck's avatar
Bugfix:    
Michael Beck committed
322
		if (op1_header == header && is_rc(op2, op1_header)) {
323
			result = reduce(orig, op1, op2, env);
Matthias Braun's avatar
Matthias Braun committed
324
		} else if (op2_header == header && is_rc(op1, op2_header)) {
325
			result = reduce(orig, op2, op1, env);
Matthias Braun's avatar
Matthias Braun committed
326
		} else {
327
			result = do_apply(code, db, op1, op2);
Michael Beck's avatar
Bugfix:    
Michael Beck committed
328
329
			get_irn_ne(result, env)->header = NULL;
		}
330
331
	}
	return result;
332
}
333
334
335

/**
 * The Reduce operation.
Michael Beck's avatar
Michael Beck committed
336
337
338
339
340
341
342
343
 *
 * @param orig   the node that represent the original operation and determines
 *               the opcode, debug-info and mode of a newly created one
 * @param iv     the induction variable
 * @param rc     the region constant
 * @param env    the environment
 *
 * @return the reduced node
344
 */
345
346
static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env)
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
347
348
	unsigned  code   = get_irn_opcode(orig);
	ir_node  *result = search(code, iv, rc, env);
349

350
	/* check if we have already done this operation on the iv */
Michael Beck's avatar
Bugfix:    
Michael Beck committed
351
	if (result == NULL) {
352
		result = exact_copy(iv);
353

Matthias Braun's avatar
Matthias Braun committed
354
		ir_mode *mode = get_irn_mode(orig);
355
356
		if (get_irn_mode(result) != mode) {
			/*
Matthias Braun's avatar
Matthias Braun committed
357
358
359
360
361
362
363
			 * Beware: we must always create a new induction variable with the
			 * same mode as the node we are replacing. Especially this means
			 * the mode might be changed from P to I and back. This is always
			 * possible, because we have only Phi, Add and Sub nodes.
			 * However, this might lead to AddIs(Iu,Is) which we must fix. The
			 * best way to do this seems to be a post-pass, or we might end
			 * with useless Conv's.
364
365
			 */
			set_irn_mode(result, mode);
Matthias Braun's avatar
Matthias Braun committed
366
			env->need_postpass = true;
367
		}
368
369
370
371
		add(code, iv, rc, result, env);
		DB((dbg, LEVEL_3, "   Created new %+F for %+F (%s %+F)\n", result, iv,
			get_irn_opname(orig), rc));

Matthias Braun's avatar
Matthias Braun committed
372
373
		node_entry *iv_e = get_irn_ne(iv, env);
		node_entry *e    = get_irn_ne(result, env);
374
375
376
		e->header = iv_e->header;

		/* create the LFTR edge */
Matthias Braun's avatar
Matthias Braun committed
377
		lftr_add(iv, result, code, rc, env);
378

379
380
		foreach_irn_in_r(result, i, pred) {
			ir_node *o = pred;
381
382
383
			e = get_irn_ne(o, env);
			if (e->header == iv_e->header)
				o = reduce(orig, o, rc, env);
384
			else if (is_Phi(result) || code == iro_Mul)
Michael Beck's avatar
Bugfix:    
Michael Beck committed
385
				o = apply(iv_e->header, orig, o, rc, env);
386
387
			set_irn_n(result, i, o);
		}
388
	} else {
Michael Beck's avatar
Michael Beck committed
389
390
391
		DB((dbg, LEVEL_3, "   Already Created %+F for %+F (%s %+F)\n", result, iv,
			get_irn_opname(orig), rc));
	}
392
	return result;
393
}
394

Michael Beck's avatar
Michael Beck committed
395
/**
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
396
 * Update the SCC for a newly created IV.
Michael Beck's avatar
Michael Beck committed
397
 */
398
399
static void update_scc(ir_node *iv, node_entry *e, iv_env *env)
{
Matthias Braun's avatar
Matthias Braun committed
400
	DB((dbg, LEVEL_2, "  Creating SCC for new an induction variable:\n  "));
Michael Beck's avatar
Michael Beck committed
401
402
403
	scc     *pscc   = e->pscc;
	ir_node *header = e->header;
	pscc->head = NULL;
404
405
	pdeq    *wq = new_pdeq();
	pdeq_putr(wq, iv);
Michael Beck's avatar
Michael Beck committed
406
	do {
407
		ir_node    *irn = (ir_node*)pdeq_getl(wq);
Michael Beck's avatar
Michael Beck committed
408
409
410
411
412
		node_entry *ne  = get_irn_ne(irn, env);

		ne->pscc   = pscc;
		ne->next   = pscc->head;
		pscc->head = irn;
413
		DB((dbg, LEVEL_2, " %+F,", irn));
Michael Beck's avatar
Michael Beck committed
414

415
416
		foreach_irn_in_r(irn, i, pred) {
			node_entry *const pe = get_irn_ne(pred, env);
Michael Beck's avatar
Michael Beck committed
417

418
			if (pe->header == header && pe->pscc == NULL) {
Michael Beck's avatar
Bugfix:    
Michael Beck committed
419
				/* set the pscc here to ensure that the node is NOT enqueued another time */
420
				pe->pscc = pscc;
421
				pdeq_putr(wq, pred);
422
			}
Michael Beck's avatar
Michael Beck committed
423
		}
424
425
	} while (!pdeq_empty(wq));
	del_pdeq(wq);
426
	DB((dbg, LEVEL_2, "\n"));
427
}
Michael Beck's avatar
Michael Beck committed
428

429
/**
430
431
432
 * The Replace operation. We found a node representing iv (+,-,*) rc
 * that can be removed by replacing the induction variable iv by a new
 * one that 'applies' the operation 'irn'.
433
434
435
436
437
438
 *
 * @param irn   the node that will be replaced
 * @param iv    the induction variable
 * @param rc    the region constant
 * @param env   the environment
 */
Matthias Braun's avatar
Matthias Braun committed
439
static bool replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env)
440
{
441
442
	DB((dbg, LEVEL_2, "  Replacing %+F\n", irn));

Matthias Braun's avatar
Matthias Braun committed
443
	ir_node *result = reduce(irn, iv, rc, env);
444
445
	if (result != irn) {
		exchange(irn, result);
Matthias Braun's avatar
Matthias Braun committed
446
		node_entry *e = get_irn_ne(result, env);
Michael Beck's avatar
Michael Beck committed
447
		if (e->pscc == NULL) {
448
			e->pscc = OALLOCZ(&env->obst, scc);
Michael Beck's avatar
Michael Beck committed
449
450
			update_scc(result, e, env);
		}
451
		++env->replaced;
Matthias Braun's avatar
Matthias Braun committed
452
		return true;
453
	}
Matthias Braun's avatar
Matthias Braun committed
454
	return false;
455
}
456

Michael Beck's avatar
Michael Beck committed
457
458
/**
 * Check if an IV represents a counter with constant limits.
459
460
461
 *
 * @param iv    any node of the induction variable
 * @param env   the environment
Michael Beck's avatar
Michael Beck committed
462
 */
Matthias Braun's avatar
Matthias Braun committed
463
static bool is_counter_iv(ir_node *iv, iv_env *env)
464
{
Matthias Braun's avatar
Matthias Braun committed
465
466
	node_entry *e    = get_irn_ne(iv, env);
	scc        *pscc = e->pscc;
Michael Beck's avatar
Michael Beck committed
467
468

	if (pscc->code != 0) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
469
		/* already analyzed */
Michael Beck's avatar
Michael Beck committed
470
471
472
		return pscc->code != iro_Bad;
	}

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
473
474
475
	ir_node   *have_init = NULL;
	ir_node   *have_incr = NULL;
	ir_opcode  code      = iro_Bad;
Michael Beck's avatar
Michael Beck committed
476
	pscc->code = iro_Bad;
Matthias Braun's avatar
Matthias Braun committed
477
	for (ir_node *irn = pscc->head; irn != NULL; irn = e->next) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
478
		if (is_Add(irn) || is_Sub(irn)) {
Michael Beck's avatar
Michael Beck committed
479
			if (have_incr != NULL)
Matthias Braun's avatar
Matthias Braun committed
480
				return false;
Michael Beck's avatar
Michael Beck committed
481

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
482
			have_incr = get_binop_right(irn);
Matthias Braun's avatar
Matthias Braun committed
483
			if (!is_Const(have_incr)) {
484
				return false;
Michael Beck's avatar
Michael Beck committed
485
			}
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
486
			code = get_irn_opcode(irn);
Michael Beck's avatar
Michael Beck committed
487
		} else if (is_Phi(irn)) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
488
			for (int i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
489
490
491
492
493
494
				ir_node    *pred = get_Phi_pred(irn, i);
				node_entry *ne   = get_irn_ne(pred, env);

				if (ne->header == e->header)
					continue;
				if (have_init != NULL)
Matthias Braun's avatar
Matthias Braun committed
495
					return false;
Michael Beck's avatar
Michael Beck committed
496
				have_init = pred;
Matthias Braun's avatar
Matthias Braun committed
497
498
				if (!is_Const(pred))
					return false;
Michael Beck's avatar
Michael Beck committed
499
			}
Matthias Braun's avatar
Matthias Braun committed
500
501
502
		} else {
			return false;
		}
Michael Beck's avatar
Michael Beck committed
503
504
505
506
507
508
		e = get_irn_ne(irn, env);
	}
	pscc->init = get_Const_tarval(have_init);
	pscc->incr = get_Const_tarval(have_incr);
	pscc->code = code;
	return code != iro_Bad;
509
}
Michael Beck's avatar
Michael Beck committed
510

511
512
/**
 * Check the users of an induction variable for register pressure.
513
514
515
516
517
518
 *
 * @param iv    any node of the induction variable
 * @param env   the environment
 *
 * @return non-zero if the register pressure is estimated
 *         to not increase, zero else
519
 */
Matthias Braun's avatar
Matthias Braun committed
520
static bool check_users_for_reg_pressure(ir_node *iv, iv_env *env)
521
{
522
523
	ir_node    *have_user = NULL;
	ir_node    *have_cmp  = NULL;
Michael Beck's avatar
Michael Beck committed
524
525
	node_entry *e         = get_irn_ne(iv, env);
	scc        *pscc      = e->pscc;
Matthias Braun's avatar
Matthias Braun committed
526
	for (ir_node *irn = pscc->head; irn != NULL; irn = e->next) {
527
528
529
530
531
532
533
534
535
536
537
		foreach_out_edge(irn, edge) {
			ir_node    *user = get_edge_src_irn(edge);
			node_entry *ne = get_irn_ne(user, env);

			if (e->header == ne->header) {
				/* found user from the same IV */
				continue;
			}
			if (is_Cmp(user)) {
				if (have_cmp != NULL) {
					/* more than one cmp, for now end here */
Matthias Braun's avatar
Matthias Braun committed
538
					return false;
539
540
541
542
543
544
				}
				have_cmp = user;
			} else {
				/* user is a real user of the IV */
				if (have_user != NULL) {
					/* found the second user */
Matthias Braun's avatar
Matthias Braun committed
545
					return false;
546
547
548
549
				}
				have_user = user;
			}
		}
Michael Beck's avatar
Michael Beck committed
550
551
		e = get_irn_ne(irn, env);
	}
552
553
554

	if (have_user == NULL) {
		/* no user, ignore */
Matthias Braun's avatar
Matthias Braun committed
555
		return true;
556
557
558
559
	}

	if (have_cmp == NULL) {
		/* fine, only one user, try to reduce */
Matthias Braun's avatar
Matthias Braun committed
560
		return true;
561
	}
562
563
564
	/*
	 * We found one user AND at least one cmp.
	 * We should check here if we can transform the Cmp.
Michael Beck's avatar
Michael Beck committed
565
566
567
	 *
	 * For now our capabilities for doing linear function test
	 * are limited, so check if the iv has the right form: Only ONE
568
	 * Phi, only one Add/Sub with a Const.
Michael Beck's avatar
Michael Beck committed
569
	 */
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
570
	return is_counter_iv(iv, env);
571
}
572
573

/**
Michael Beck's avatar
Michael Beck committed
574
575
576
577
578
 * Check if a node can be replaced (+, -, *).
 *
 * @param irn   the node to check
 * @param env   the environment
 *
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
579
 * @return non-zero if irn should be replaced
580
 */
Matthias Braun's avatar
Matthias Braun committed
581
static bool check_replace(ir_node *irn, iv_env *env)
582
{
Matthias Braun's avatar
Matthias Braun committed
583
	ir_op    *op   = get_irn_op(irn);
584
	unsigned  code = get_op_code(op);
585
586
587
	switch (code) {
	case iro_Mul:
	case iro_Add:
Matthias Braun's avatar
Matthias Braun committed
588
589
590
591
592
593
594
595
	case iro_Sub: {
		ir_node *left  = get_binop_left(irn);
		ir_node *right = get_binop_right(irn);

		ir_node *liv = is_iv(left, env);
		ir_node *riv = is_iv(right, env);
		ir_node *iv  = NULL;
		ir_node *rc  = NULL;
596
		if (liv && is_rc(right, liv)) {
597
			iv = left; rc = right;
Matthias Braun's avatar
Matthias Braun committed
598
		} else if (riv && is_op_commutative(op) &&
Michael Beck's avatar
Michael Beck committed
599
			            is_rc(left, riv)) {
600
			iv = right; rc = left;
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
601
602
		} else {
			return false;
603
604
		}

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
605
606
607
608
		if (env->osr_flags & osr_flag_keep_reg_pressure
		    && !check_users_for_reg_pressure(iv, env))
			return false;
		return replace(irn, iv, rc, env);
Matthias Braun's avatar
Matthias Braun committed
609
	}
Michael Beck's avatar
Michael Beck committed
610
	default:
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
611
		return false;
612
	}
613
}
614
615

/**
Michael Beck's avatar
Michael Beck committed
616
617
618
619
 * Check which SCC's are induction variables.
 *
 * @param pscc  a SCC
 * @param env   the environment
620
 */
621
622
static void classify_iv(scc *pscc, iv_env *env)
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
623
	/* find the header block for this SCC */
Matthias Braun's avatar
Matthias Braun committed
624
625
626
	ir_node    *header = NULL;
	node_entry *h      = NULL;
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
627
		node_entry *e = (node_entry*)get_irn_link(irn);
628
629
630
		ir_node *block = get_nodes_block(irn);

		next = e->next;
Matthias Braun's avatar
Matthias Braun committed
631
		node_entry *b = get_irn_ne(block, env);
632
633
634
635
636
637

		if (header) {
			if (h->POnum < b->POnum) {
				header = block;
				h      = b;
			}
Matthias Braun's avatar
Matthias Braun committed
638
		} else {
639
640
641
642
643
			header = block;
			h      = b;
		}
	}

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
644
	/* check if this SCC contains only Phi, Add or Sub nodes */
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
645
646
647
	bool      only_phi    = true;
	unsigned  num_outside = 0;
	ir_node  *out_rc      = NULL;
Matthias Braun's avatar
Matthias Braun committed
648
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
649
		node_entry *e = get_irn_ne(irn, env);
650
651
652

		next = e->next;
		switch (get_irn_opcode(irn)) {
Matthias Braun's avatar
Matthias Braun committed
653
654
655
656
657
658
659
660
661
662
663
664
665
666
		case iro_Sub: {
			only_phi = false;
			ir_node    *left        = get_Sub_left(irn);
			node_entry *left_entry  = get_irn_ne(left, env);
			ir_node    *right       = get_Sub_right(irn);
			node_entry *right_entry = get_irn_ne(right, env);

			if (left_entry->pscc != e->pscc ||
				(right_entry->pscc != e->pscc && !is_rc(right, header))) {
				/*
				 * Not an induction variable.
				 * Region constant are only allowed on right hand side.
				 */
				goto fail;
667
668
			}
			break;
Matthias Braun's avatar
Matthias Braun committed
669
		}
670
671

		case iro_Add:
Matthias Braun's avatar
Matthias Braun committed
672
			only_phi = false;
673
			/* fall through */
674
		case iro_Phi:
675
			foreach_irn_in_r(irn, i, pred) {
676
				node_entry *pe = get_irn_ne(pred, env);
677
678
679

				if (pe->pscc != e->pscc) {
					/* not in the same SCC, must be a region const */
Matthias Braun's avatar
Matthias Braun committed
680
					if (!is_rc(pred, header)) {
681
682
683
						/* not an induction variable */
						goto fail;
					}
Matthias Braun's avatar
Matthias Braun committed
684
					if (!out_rc) {
685
686
						out_rc = pred;
						++num_outside;
687
					} else if (out_rc != pred) {
688
						++num_outside;
689
					}
690
691
692
693
694
695
696
697
698
				}
			}
			break;
		default:
			/* not an induction variable */
			goto fail;
		}
	}
	/* found an induction variable */
Michael Beck's avatar
Michael Beck committed
699
	DB((dbg, LEVEL_2, "  Found an induction variable:\n  "));
700
	if (only_phi && num_outside == 1 && get_irn_mode(pscc->head) != mode_M) {
701
		/* a phi cycle with only one real predecessor can be collapsed */
702
		DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));
703

Matthias Braun's avatar
Matthias Braun committed
704
		for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
705
706
707
708
709
710
711
			node_entry *e = get_irn_ne(irn, env);
			next = e->next;
			e->header = NULL;
			exchange(irn, out_rc);
		}
		++env->replaced;
		return;
712
	}
713

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
714
	/* set the header for every node in this SCC */
Matthias Braun's avatar
Matthias Braun committed
715
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
716
717
		node_entry *e = get_irn_ne(irn, env);
		e->header = header;
718
		next = e->next;
Michael Beck's avatar
Michael Beck committed
719
		DB((dbg, LEVEL_2, " %+F,", irn));
720
	}
Michael Beck's avatar
Michael Beck committed
721
	DB((dbg, LEVEL_2, "\n"));
722
	return;
723

724
fail:
Matthias Braun's avatar
Matthias Braun committed
725
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
726
		node_entry *e = get_irn_ne(irn, env);
727

728
		next = e->next;
729
		e->header = NULL;
730
	}
731
}
732
733

/**
734
 * Process an SCC for the operator strength reduction.
Michael Beck's avatar
Michael Beck committed
735
736
737
 *
 * @param pscc  the SCC
 * @param env   the environment
738
 */
739
740
static void process_scc(scc *pscc, iv_env *env)
{
741
	ir_node *head = pscc->head;
742
	node_entry *e = (node_entry*)get_irn_link(head);
743
744

#ifdef DEBUG_libfirm
Matthias Braun's avatar
Matthias Braun committed
745
746
747
	DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = (node_entry*)get_irn_link(irn);
748

Matthias Braun's avatar
Matthias Braun committed
749
		next = e->next;
750

Matthias Braun's avatar
Matthias Braun committed
751
		DB((dbg, LEVEL_4, " %+F,", irn));
752
	}
Matthias Braun's avatar
Matthias Braun committed
753
	DB((dbg, LEVEL_4, "\n"));
754
755
756
757
758
#endif

	if (e->next == NULL) {
		/* this SCC has only a single member */
		check_replace(head, env);
759
	} else {
760
761
		classify_iv(pscc, env);
	}
762
}
763

764
765
/**
 * If an SCC is a Phi only cycle, remove it.
766
767
768
 *
 * @param pscc  an SCC that consists of Phi nodes only
 * @param env   the environment
769
 */
770
771
static void remove_phi_cycle(scc *pscc, iv_env *env)
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
772
	/* check if this SCC contains only Phi nodes */
Matthias Braun's avatar
Matthias Braun committed
773
774
	ir_node *out_rc = NULL;
	for (ir_node *irn = pscc->head, *next; irn; irn = next) {
775
776
777
		node_entry *e = get_irn_ne(irn, env);

		next = e->next;
Matthias Braun's avatar
Matthias Braun committed
778
		if (!is_Phi(irn))
779
780
			return;

781
		foreach_irn_in_r(irn, i, pred) {
782
783
784
785
			node_entry *pe = get_irn_ne(pred, env);

			if (pe->pscc != e->pscc) {
				/* not in the same SCC, must be the only input */
Matthias Braun's avatar
Matthias Braun committed
786
				if (!out_rc) {
787
788
789
790
791
792
793
794
795
796
					out_rc = pred;
				} else if (out_rc != pred) {
					return;
				}
			}
		}
	}
	/* found a Phi cycle */
	DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));

Matthias Braun's avatar
Matthias Braun committed
797
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
798
799
800
801
802
803
		node_entry *e = get_irn_ne(irn, env);
		next = e->next;
		e->header = NULL;
		exchange(irn, out_rc);
	}
	++env->replaced;
804
}
805
806

/**
807
 * Process a SCC for the Phi cycle remove.
808
809
810
811
 *
 * @param pscc  the SCC
 * @param env   the environment
 */
812
813
static void process_phi_only_scc(scc *pscc, iv_env *env)
{
Matthias Braun's avatar
Matthias Braun committed
814
815
	ir_node    *head = pscc->head;
	node_entry *e    = (node_entry*)get_irn_link(head);
816
817

#ifdef DEBUG_libfirm
Matthias Braun's avatar
Matthias Braun committed
818
819
820
821
	DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
	for (ir_node *irn = pscc->head, *next; irn; irn = next) {
		node_entry *e = (node_entry*)get_irn_link(irn);
		next = e->next;
822

Matthias Braun's avatar
Matthias Braun committed
823
		DB((dbg, LEVEL_4, " %+F,", irn));
824
	}
Matthias Braun's avatar
Matthias Braun committed
825
	DB((dbg, LEVEL_4, "\n"));
826
827
828
829
#endif

	if (e->next != NULL)
		remove_phi_cycle(pscc, env);
830
}
831
832


833
834
/**
 * Push a node onto the stack.
Michael Beck's avatar
Michael Beck committed
835
836
837
 *
 * @param env   the environment
 * @param n     the node to push
838
 */
839
840
static void push(iv_env *env, ir_node *n)
{
841
	if (env->tos == ARR_LEN(env->stack)) {
842
		size_t nlen = ARR_LEN(env->stack) * 2;
843
844
845
		ARR_RESIZE(ir_node *, env->stack, nlen);
	}
	env->stack[env->tos++] = n;
Matthias Braun's avatar
Matthias Braun committed
846
847
	node_entry *e = get_irn_ne(n, env);
	e->in_stack = true;
848
}
849
850

/**
851
 * Pop a node from the stack.
852
 *
Michael Beck's avatar
Michael Beck committed
853
854
 * @param env   the environment
 *
855
856
 * @return  The topmost node
 */
857
858
static ir_node *pop(iv_env *env)
{
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
859
	ir_node    *n = env->stack[--env->tos];
860
	node_entry *e = get_irn_ne(n, env);
861

Matthias Braun's avatar
Matthias Braun committed
862
	e->in_stack = false;
863
	return n;
864
}
865

866
/**
Michael Beck's avatar
Michael Beck committed
867
 * Do Tarjan's SCC algorithm and drive OSR.
868
869
870
871
 *
 * @param irn  start at this node
 * @param env  the environment
 */
872
873
static void dfs(ir_node *irn, iv_env *env)
{
874
	node_entry *node = get_irn_ne(irn, env);
875
876
877
878
879

	mark_irn_visited(irn);

	/* do not put blocks into the scc */
	if (is_Block(irn)) {
880
		foreach_irn_in(irn, i, pred) {
881
			if (!irn_visited(pred))
882
883
				dfs(pred, env);
		}
884
	} else {
885
886
887
888
889
890
891
		ir_node *block = get_nodes_block(irn);

		node->DFSnum = env->nextDFSnum++;
		node->low    = node->DFSnum;
		push(env, irn);

		/* handle the block */
892
		if (!irn_visited(block))
893
894
			dfs(block, env);

895
		foreach_irn_in(irn, i, pred) {
896
			node_entry *o = get_irn_ne(pred, env);
897

898
			if (!irn_visited(pred)) {
899
900
901
902