summaryrefslogtreecommitdiffhomepage
path: root/ir/opt/opt_osr.c
blob: e833d5c7ffc957757d65c57bca380055bdd97089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
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
210
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   Operator Strength Reduction.
 * @date    12.5.2006
 * @author  Michael Beck
 * @brief
 *  Implementation of the Operator Strength Reduction algorithm
 *  by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
 *  Extended version.
 */
#include "array.h"
#include "debug.h"
#include "hashptr.h"
#include "ircons.h"
#include "irdom.h"
#include "iredges_t.h"
#include "irflag_t.h"
#include "irgmod.h"
#include "irgwalk.h"
#include "irhooks.h"
#include "irloop_t.h"
#include "irop_t.h"
#include "iroptimize.h"
#include "irouts.h"
#include "irtools.h"
#include "obst.h"
#include "panic.h"
#include "pdeq.h"
#include "set.h"
#include "tv.h"
#include "util.h"
#include <stdbool.h>

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

/** A scc. */
typedef struct scc {
	ir_node   *head; /**< the head of the list */
	ir_tarval *init; /**< the init value iff only one exists. */
	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
	                         analyzed, iro_Bad else */
} scc;

/** A node entry */
typedef struct node_entry {
	unsigned  DFSnum;   /**< the DFS number of this node */
	unsigned  low;      /**< the low number of this node */
	ir_node  *header;   /**< the header of this node */
	bool      in_stack; /**< flag, set if the node is on the stack */
	ir_node  *next;     /**< link to the next node the same SCC */
	scc      *pscc;     /**< the SCC of this node */
	unsigned  POnum;    /**< the post order number for blocks */
} node_entry;

/** The environment. */
typedef struct iv_env {
	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 */
	/** Function called to process a SCC. */
	void (*process_scc)(scc *pscc, struct iv_env *env);
} iv_env;

/**
 * An entry in the (op, node, node) -> node map.
 */
typedef struct quadruple_t {
	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 */
} quadruple_t;

/**
 * A LFTR edge.
 */
typedef struct ldtr_edge_t {
	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 */
} ldtr_edge_t;

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

/**
 * Compare two LFTR edges.
 */
static int lftr_cmp(const void *e1, const void *e2, size_t size)
{
	(void)size;
	const ldtr_edge_t *l1 = (const ldtr_edge_t*)e1;
	const ldtr_edge_t *l2 = (const ldtr_edge_t*)e2;
	return l1->src != l2->src;
}

/**
 * Find a LFTR edge.
 *
 * @param src  the source node of the transition
 */
static ldtr_edge_t *lftr_find(ir_node *src, iv_env *env)
{
	ldtr_edge_t key = { .src = src };
	return set_find(ldtr_edge_t, env->lftr_edges, &key, sizeof(key),
	                hash_ptr(src));
}

/**
 * Add a LFTR edge.
 *
 * @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
 */
static void lftr_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc,
                     iv_env *env)
{
	ldtr_edge_t key = { .src = src, .dst = dst, .code = code, .rc = rc };

	/*
	 * There might be more than one edge here. This is rather bad
	 * because we currently store only one.
	 */
	(void)set_insert(ldtr_edge_t, env->lftr_edges, &key, sizeof(key),
	                 hash_ptr(src));
}

/**
 * Gets the node_entry of a node.
 *
 * @param irn  the node
 * @param env  the environment
 */
static node_entry *get_irn_ne(ir_node *irn, iv_env *env)
{
	node_entry *e = (node_entry*)get_irn_link(irn);

	if (e == NULL) {
		e = OALLOCZ(&env->obst, node_entry);
		set_irn_link(irn, e);
	}
	return e;
}

/**
 * Gets the SCC from an induction variable.
 *
 * @param iv   any node of the induction variable
 * @param env  the environment
 */
static scc *get_iv_scc(ir_node *iv, iv_env *env)
{
	node_entry *e = get_irn_ne(iv, env);
	return e->pscc;
}

/**
 * Check if irn is an IV.
 *
 * @param irn  the node to check
 * @param env  the environment
 *
 * @returns the header if it is one, NULL else
 */
static ir_node *is_iv(ir_node *irn, iv_env *env)
{
	return get_irn_ne(irn, env)->header;
}

/**
 * Check if irn is a region constant.
 * 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
 */
static bool is_rc(ir_node *irn, ir_node *header_block)
{
	ir_node *block = get_nodes_block(irn);

	return (block != header_block) && block_dominates(block, header_block);
}

/**
 * Set compare function for the quad set.
 */
static int quad_cmp(const void *e1, const void *e2, size_t size)
{
	(void)size;
	const quadruple_t *c1 = (const quadruple_t*)e1;
	const quadruple_t *c2 = (const quadruple_t*)e2;
	return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
}

/**
 * Check if an reduced operation was already calculated.
 *
 * @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
 */
static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env)
{
	quadruple_t  key   = { .code = code, .op1 = op1, .op2 = op2 };
	unsigned     hash  = (code*9) ^ hash_ptr(op1) ^ hash_ptr(op2);
	quadruple_t *entry = set_find(quadruple_t, env->quad_map, &key, sizeof(key),
	                              hash);
	if (entry != NULL)
		return entry->res;
	return NULL;
}

/**
 * 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
 */
static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result,
                iv_env *env)
{
	quadruple_t key  = { .code = code, .op1 = op1, .op2 = op2, .res = result };
	unsigned    hash = (code*9) ^ hash_ptr(op1) ^ hash_ptr(op2);
	(void)set_insert(quadruple_t, env->quad_map, &key, sizeof(key), hash);
}

/**
 * Find a location where to place a bin-op whose operands are in
 * block1 and block2.
 *
 * @param block1  the block of the first operand
 * @param block2  the block of the second operand
 *
 * 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.
 */
static ir_node *find_location(ir_node *block1, ir_node *block2)
{
	if (block_dominates(block1, block2))
		return block2;
	assert(block_dominates(block2, block1));
	return block1;
}

/**
 * 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
 */
static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2)
{
	ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
	switch (code) {
	case iro_Mul:
		return new_rd_Mul(db, block, op1, op2);
	case iro_Add:
		return new_rd_Add(db, block, op1, op2);
	case iro_Sub:
		return new_rd_Sub(db, block, op1, op2);
	default:
		panic("unsupported opcode");
	}
}

/**
 * The Apply operation.
 *
 * @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
 * @param env    the environment
 *
 * @return the newly created node
 */
static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1,
                      ir_node *op2, iv_env *env)
{
	unsigned  code   = get_irn_opcode(orig);
	ir_node  *result = search(code, op1, op2, env);

	if (result == NULL) {
		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;

		if (op1_header == header && is_rc(op2, op1_header)) {
			result = reduce(orig, op1, op2, env);
		} else if (op2_header == header && is_rc(op1, op2_header)) {
			result = reduce(orig, op2, op1, env);
		} else {
			result = do_apply(code, db, op1, op2);
			get_irn_ne(result, env)->header = NULL;
		}
	}
	return result;
}

/**
 * The Reduce operation.
 *
 * @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
 */
static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env)
{
	unsigned  code   = get_irn_opcode(orig);
	ir_node  *result = search(code, iv, rc, env);

	/* check if we have already done this operation on the iv */
	if (result == NULL) {
		result = exact_copy(iv);

		ir_mode *mode = get_irn_mode(orig);
		if (get_irn_mode(result) != mode) {
			/*
			 * 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.
			 */
			set_irn_mode(result, mode);
			env->need_postpass = true;
		}
		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));

		node_entry *iv_e = get_irn_ne(iv, env);
		node_entry *e    = get_irn_ne(result, env);
		e->header = iv_e->header;

		/* create the LFTR edge */
		lftr_add(iv, result, code, rc, env);

		foreach_irn_in_r(result, i, pred) {
			ir_node *o = pred;
			e = get_irn_ne(o, env);
			if (e->header == iv_e->header)
				o = reduce(orig, o, rc, env);
			else if (is_Phi(result) || code == iro_Mul)
				o = apply(iv_e->header, orig, o, rc, env);
			set_irn_n(result, i, o);
		}
	} else {
		DB((dbg, LEVEL_3, "   Already Created %+F for %+F (%s %+F)\n", result, iv,
			get_irn_opname(orig), rc));
	}
	return result;
}

/**
 * Update the SCC for a newly created IV.
 */
static void update_scc(ir_node *iv, node_entry *e, iv_env *env)
{
	DB((dbg, LEVEL_2, "  Creating SCC for new an induction variable:\n  "));
	scc     *pscc   = e->pscc;
	ir_node *header = e->header;
	pscc->head = NULL;
	deq_t    wq;
	deq_init(&wq);
	deq_push_pointer_right(&wq, iv);
	do {
		ir_node    *irn = deq_pop_pointer_left(ir_node, &wq);
		node_entry *ne  = get_irn_ne(irn, env);

		ne->pscc   = pscc;
		ne->next   = pscc->head;
		pscc->head = irn;
		DB((dbg, LEVEL_2, " %+F,", irn));

		foreach_irn_in_r(irn, i, pred) {
			node_entry *const pe = get_irn_ne(pred, env);

			if (pe->header == header && pe->pscc == NULL) {
				/* set the pscc here to ensure that the node is NOT enqueued another time */
				pe->pscc = pscc;
				deq_push_pointer_right(&wq, pred);
			}
		}
	} while (!deq_empty(&wq));
	deq_free(&wq);
	DB((dbg, LEVEL_2, "\n"));
}

/**
 * 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'.
 *
 * @param irn   the node that will be replaced
 * @param iv    the induction variable
 * @param rc    the region constant
 * @param env   the environment
 */
static bool replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env)
{
	DB((dbg, LEVEL_2, "  Replacing %+F\n", irn));

	ir_node *result = reduce(irn, iv, rc, env);
	if (result != irn) {
		exchange(irn, result);
		node_entry *e = get_irn_ne(result, env);
		if (e->pscc == NULL) {
			e->pscc = OALLOCZ(&env->obst, scc);
			update_scc(result, e, env);
		}
		++env->replaced;
		return true;
	}
	return false;
}

/**
 * Check if an IV represents a counter with constant limits.
 *
 * @param iv    any node of the induction variable
 * @param env   the environment
 */
static bool is_counter_iv(ir_node *iv, iv_env *env)
{
	node_entry *e    = get_irn_ne(iv, env);
	scc        *pscc = e->pscc;

	if (pscc->code != 0) {
		/* already analyzed */
		return pscc->code != iro_Bad;
	}

	ir_node   *have_init = NULL;
	ir_node   *have_incr = NULL;
	ir_opcode  code      = iro_Bad;
	pscc->code = iro_Bad;
	for (ir_node *irn = pscc->head; irn != NULL; irn = e->next) {
		if (is_Add(irn) || is_Sub(irn)) {
			if (have_incr != NULL)
				return false;

			have_incr = get_binop_right(irn);
			if (!is_Const(have_incr)) {
				return false;
			}
			code = get_irn_opcode(irn);
		} else if (is_Phi(irn)) {
			for (int i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
				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)
					return false;
				have_init = pred;
				if (!is_Const(pred))
					return false;
			}
		} else {
			return false;
		}
		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;
}

/**
 * Check the users of an induction variable for register pressure.
 *
 * @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
 */
static bool check_users_for_reg_pressure(ir_node *iv, iv_env *env)
{
	ir_node    *have_user = NULL;
	ir_node    *have_cmp  = NULL;
	node_entry *e         = get_irn_ne(iv, env);
	scc        *pscc      = e->pscc;
	for (ir_node *irn = pscc->head; irn != NULL; irn = e->next) {
		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 */
					return false;
				}
				have_cmp = user;
			} else {
				/* user is a real user of the IV */
				if (have_user != NULL) {
					/* found the second user */
					return false;
				}
				have_user = user;
			}
		}
		e = get_irn_ne(irn, env);
	}

	if (have_user == NULL) {
		/* no user, ignore */
		return true;
	}

	if (have_cmp == NULL) {
		/* fine, only one user, try to reduce */
		return true;
	}
	/*
	 * We found one user AND at least one cmp.
	 * We should check here if we can transform the Cmp.
	 *
	 * For now our capabilities for doing linear function test
	 * are limited, so check if the iv has the right form: Only ONE
	 * Phi, only one Add/Sub with a Const.
	 */
	return is_counter_iv(iv, env);
}

/**
 * Check if a node can be replaced (+, -, *).
 *
 * @param irn   the node to check
 * @param env   the environment
 *
 * @return non-zero if irn should be replaced
 */
static bool check_replace(ir_node *irn, iv_env *env)
{
	ir_op    *op   = get_irn_op(irn);
	unsigned  code = get_op_code(op);
	switch (code) {
	case iro_Mul:
	case iro_Add:
	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;
		if (liv && is_rc(right, liv)) {
			iv = left; rc = right;
		} else if (riv && is_op_commutative(op) &&
			            is_rc(left, riv)) {
			iv = right; rc = left;
		} else {
			return false;
		}

		if (env->osr_flags & osr_flag_keep_reg_pressure
		    && !check_users_for_reg_pressure(iv, env))
			return false;
		return replace(irn, iv, rc, env);
	}
	default:
		return false;
	}
}

/**
 * Check which SCC's are induction variables.
 *
 * @param pscc  a SCC
 * @param env   the environment
 */
static void classify_iv(scc *pscc, iv_env *env)
{
	/* find the header block for this SCC */
	ir_node    *header = NULL;
	node_entry *h      = NULL;
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = (node_entry*)get_irn_link(irn);
		ir_node *block = get_nodes_block(irn);

		next = e->next;
		node_entry *b = get_irn_ne(block, env);

		if (header) {
			if (h->POnum < b->POnum) {
				header = block;
				h      = b;
			}
		} else {
			header = block;
			h      = b;
		}
	}

	/* check if this SCC contains only Phi, Add or Sub nodes */
	bool      only_phi    = true;
	unsigned  num_outside = 0;
	ir_node  *out_rc      = NULL;
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = get_irn_ne(irn, env);

		next = e->next;
		switch (get_irn_opcode(irn)) {
		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;
			}
			break;
		}

		case iro_Add:
			only_phi = false;
			/* fall through */
		case iro_Phi:
			foreach_irn_in_r(irn, i, pred) {
				node_entry *pe = get_irn_ne(pred, env);

				if (pe->pscc != e->pscc) {
					/* not in the same SCC, must be a region const */
					if (!is_rc(pred, header)) {
						/* not an induction variable */
						goto fail;
					}
					if (!out_rc) {
						out_rc = pred;
						++num_outside;
					} else if (out_rc != pred) {
						++num_outside;
					}
				}
			}
			break;
		default:
			/* not an induction variable */
			goto fail;
		}
	}
	/* found an induction variable */
	DB((dbg, LEVEL_2, "  Found an induction variable:\n  "));
	if (only_phi && num_outside == 1 && get_irn_mode(pscc->head) != mode_M) {
		/* a phi cycle with only one real predecessor can be collapsed */
		DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));

		for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
			node_entry *e = get_irn_ne(irn, env);
			next = e->next;
			e->header = NULL;
			exchange(irn, out_rc);
		}
		++env->replaced;
		return;
	}

	/* set the header for every node in this SCC */
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = get_irn_ne(irn, env);
		e->header = header;
		next = e->next;
		DB((dbg, LEVEL_2, " %+F,", irn));
	}
	DB((dbg, LEVEL_2, "\n"));
	return;

fail:
	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = get_irn_ne(irn, env);

		next = e->next;
		e->header = NULL;
	}
}

/**
 * Process an SCC for the operator strength reduction.
 *
 * @param pscc  the SCC
 * @param env   the environment
 */
static void process_scc(scc *pscc, iv_env *env)
{
	ir_node *head = pscc->head;
	node_entry *e = (node_entry*)get_irn_link(head);

#ifdef DEBUG_libfirm
	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);

		next = e->next;

		DB((dbg, LEVEL_4, " %+F,", irn));
	}
	DB((dbg, LEVEL_4, "\n"));
#endif

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

/**
 * If an SCC is a Phi only cycle, remove it.
 *
 * @param pscc  an SCC that consists of Phi nodes only
 * @param env   the environment
 */
static void remove_phi_cycle(scc *pscc, iv_env *env)
{
	/* check if this SCC contains only Phi nodes */
	ir_node *out_rc = NULL;
	for (ir_node *irn = pscc->head, *next; irn; irn = next) {
		node_entry *e = get_irn_ne(irn, env);

		next = e->next;
		if (!is_Phi(irn))
			return;

		foreach_irn_in_r(irn, i, pred) {
			node_entry *pe = get_irn_ne(pred, env);

			if (pe->pscc != e->pscc) {
				/* not in the same SCC, must be the only input */
				if (!out_rc) {
					out_rc = pred;
				} else if (out_rc != pred) {
					return;
				}
			}
		}
	}
	/* found a Phi cycle */
	DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));

	for (ir_node *irn = pscc->head, *next; irn != NULL; irn = next) {
		node_entry *e = get_irn_ne(irn, env);
		next = e->next;
		e->header = NULL;
		exchange(irn, out_rc);
	}
	++env->replaced;
}

/**
 * Process a SCC for the Phi cycle remove.
 *
 * @param pscc  the SCC
 * @param env   the environment
 */
static void process_phi_only_scc(scc *pscc, iv_env *env)
{
	ir_node    *head = pscc->head;
	node_entry *e    = (node_entry*)get_irn_link(head);

#ifdef DEBUG_libfirm
	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;

		DB((dbg, LEVEL_4, " %+F,", irn));
	}
	DB((dbg, LEVEL_4, "\n"));
#endif

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


/**
 * Push a node onto the stack.
 *
 * @param env   the environment
 * @param n     the node to push
 */
static void push(iv_env *env, ir_node *n)
{
	if (env->tos == ARR_LEN(env->stack)) {
		size_t nlen = ARR_LEN(env->stack) * 2;
		ARR_RESIZE(ir_node *, env->stack, nlen);
	}
	env->stack[env->tos++] = n;
	node_entry *e = get_irn_ne(n, env);
	e->in_stack = true;
}

/**
 * Pop a node from the stack.
 *
 * @param env   the environment
 *
 * @return  The topmost node
 */
static ir_node *pop(iv_env *env)
{
	ir_node    *n = env->stack[--env->tos];
	node_entry *e = get_irn_ne(n, env);

	e->in_stack = false;
	return n;
}

/**
 * Do Tarjan's SCC algorithm and drive OSR.
 *
 * @param irn  start at this node
 * @param env  the environment
 */
static void dfs(ir_node *irn, iv_env *env)
{
	node_entry *node = get_irn_ne(irn, env);

	mark_irn_visited(irn);

	/* do not put blocks into the scc */
	if (is_Block(irn)) {
		foreach_irn_in(irn, i, pred) {
			if (!irn_visited(pred))
				dfs(pred, env);
		}
	} else {
		ir_node *block = get_nodes_block(irn);

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

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

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

			if (!irn_visited(pred)) {
				dfs(pred, env);
				node->low = MIN(node->low, o->low);
			}
			if (o->DFSnum < node->DFSnum && o->in_stack)
				node->low = MIN(o->DFSnum, node->low);
		}
		if (node->low == node->DFSnum) {
			scc *pscc = OALLOCZ(&env->obst, scc);
			ir_node *x;
			do {
				x = pop(env);
				node_entry *e = get_irn_ne(x, env);
				e->pscc    = pscc;
				e->next    = pscc->head;
				pscc->head = x;
			} while (x != irn);

			env->process_scc(pscc, env);
		}
	}
}

/**
 * Do the DFS by starting at the End node of a graph.
 *
 * @param irg  the graph to process
 * @param env  the environment
 */
static void do_dfs(ir_graph *irg, iv_env *env)
{
	ir_node *end = get_irg_end(irg);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);

	inc_irg_visited(irg);

	/* visit all visible nodes */
	dfs(end, env);

	/* visit the keep-alives */
	for (int i = get_End_n_keepalives(end); i-- > 0; ) {
		ir_node *ka = get_End_keepalive(end, i);
		if (!irn_visited(ka))
			dfs(ka, env);
	}

	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
}

/**
 * Post-block-walker: assign the post-order number.
 */
static void assign_po(ir_node *block, void *ctx)
{
	iv_env     *env = (iv_env*)ctx;
	node_entry *e   = get_irn_ne(block, env);
	e->POnum = env->POnum++;
}

/**
 * Apply one LFTR edge operation.
 * Return NULL if the transformation cannot be done safely without
 * an Overflow.
 *
 * @param iv   the induction variable
 * @param rc   the constant that should be translated
 * @param e    the LFTR edge
 * @param env  the IV environment
 *
 * @return the translated region constant or NULL
 *         if the translation was not possible
 *
 * @note
 * In the current implementation only the last edge is stored, so
 * only one chain exists. That's why we might miss some opportunities.
 */
static ir_node *apply_one_edge(ir_node *iv, ir_node *rc, ldtr_edge_t *e,
                               iv_env *env)
{
	if (env->osr_flags & osr_flag_lftr_with_ov_check) {
		if (!is_counter_iv(iv, env)) {
			DB((dbg, LEVEL_4, " not counter IV"));
			return NULL;
		}

		/* overflow can only be decided for Consts */
		if (!is_Const(e->rc)) {
			if (e->code == iro_Add && mode_is_reference(get_irn_mode(e->rc))) {
				/* However we allow ONE Pointer Add, as pointer arithmetic with
				 * wrap around is undefined anyway */
				return do_apply(e->code, NULL, rc, e->rc);
			}
			DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
			return NULL;
		}

		ir_tarval *tv_l = get_Const_tarval(rc);
		ir_tarval *tv_r = get_Const_tarval(e->rc);

		int old_wrap_on_overflow = tarval_get_wrap_on_overflow();
		tarval_set_wrap_on_overflow(false);

		scc       *pscc    = get_iv_scc(iv, env);
		ir_tarval *tv_incr = pscc->incr;
		ir_tarval *tv_init = pscc->init;

		/*
		 * Check that no overflow occurs:
		 * init must be transformed without overflow
		 * the new rc must be transformed without overflow
		 * rc +/- incr must be possible without overflow
		 */
		ir_tarval *tv;
		switch (e->code) {
		case iro_Mul:
			tv      = tarval_mul(tv_l, tv_r);
			tv_init = tarval_mul(tv_init, tv_r);
			tv_incr = tarval_mul(tv_incr, tv_r);
			DB((dbg, LEVEL_4, " * %+F", tv_r));
			break;
		case iro_Add:
			tv      = tarval_add(tv_l, tv_r);
			tv_init = tarval_add(tv_init, tv_r);
			DB((dbg, LEVEL_4, " + %+F", tv_r));
			break;
		case iro_Sub:
			tv      = tarval_sub(tv_l, tv_r);
			tv_init = tarval_sub(tv_init, tv_r);
			DB((dbg, LEVEL_4, " - %+F", tv_r));
			break;
		default:
			panic("unsupported opcode");
		}

		if (tv == tarval_bad || tv_init == tarval_bad) {
			tarval_set_wrap_on_overflow(old_wrap_on_overflow);
			DB((dbg, LEVEL_4, " = OVERFLOW"));
			return NULL;
		}
		/* backwards counting in unsigned modes easily leads to overflow
		 * in the increment. TODO: improve this situation */
		if (tv_incr == tarval_bad) {
			tarval_set_wrap_on_overflow(old_wrap_on_overflow);
			DB((dbg, LEVEL_4, " = OVERFLOW (incr)"));
			return NULL;
		}

		ir_tarval *tv_end;
		if (pscc->code == iro_Add) {
			tv_end = tarval_add(tv, tv_incr);
		} else {
			assert(pscc->code == iro_Sub);
			tv_end = tarval_sub(tv, tv_incr);
		}

		tarval_set_wrap_on_overflow(old_wrap_on_overflow);

		if (tv_end == tarval_bad) {
			DB((dbg, LEVEL_4, " = OVERFLOW"));
			return NULL;
		}
		ir_graph *irg = get_irn_irg(iv);
		return new_r_Const(irg, tv);
	}
	return do_apply(e->code, NULL, rc, e->rc);
}

/**
 * Applies the operations represented by the LFTR edges to a
 * region constant and returns the value.
 * Return NULL if the transformation cannot be done safely without
 * an Overflow.
 *
 * @param pIV  points to the IV node that starts the LFTR edge chain
 *             after translation points to the new IV
 * @param rc   the region constant that should be translated
 * @param env  the IV environment
 *
 * @return the translated region constant or NULL
 *         if the translation was not possible
 */
static ir_node *apply_edges(ir_node **pIV, ir_node *rc, iv_env *env)
{
	ir_node *iv = *pIV;
	if (env->osr_flags & osr_flag_lftr_with_ov_check) {
		/* overflow can only be decided for Consts */
		if (!is_counter_iv(iv, env)) {
			DB((dbg, LEVEL_4, "not counter IV\n", rc));
			return NULL;
		}
		if (!is_Const(rc)) {
			DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
			return NULL;
		}
		DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
	}

	while (rc != NULL) {
		ldtr_edge_t *e = lftr_find(iv, env);
		if (e != NULL) {
			rc = apply_one_edge(iv, rc, e, env);
			iv = e->dst;
		} else
			break;
	}
	DB((dbg, LEVEL_3, "\n"));
	*pIV = iv;
	return rc;
}

/**
 * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
 * and tries to optimize them.
 */
static void do_lftr(ir_node *cmp, void *ctx)
{
	if (!is_Cmp(cmp))
		return;

	iv_env  *env    = (iv_env*)ctx;
	ir_node *left   = get_Cmp_left(cmp);
	ir_node *right  = get_Cmp_right(cmp);
	ir_node *liv    = is_iv(left, env);
	ir_node *riv    = is_iv(right, env);
	ir_node *nleft  = NULL;
	ir_node *nright = NULL;
	ir_node *iv;
	if (liv != NULL && is_rc(right, liv)) {
		iv = left;
		ir_node *rc = right;

		nright = apply_edges(&iv, rc, env);
		nleft  = iv;
	} else if (riv != NULL && is_rc(left, riv)) {
		iv = right;
		ir_node *rc = left;

		nleft  = apply_edges(&iv, rc, env);
		nright = iv;
	}

	if (nleft && nright) {
		if (is_counter_iv(iv, env)) {
			scc         *pscc          = get_iv_scc(iv, env);
			ir_tarval   *tv_incr       = pscc->incr;
			ir_mode     *mode          = get_tarval_mode(tv_incr);
			ir_tarval   *zero          = get_mode_null(mode);
			ir_relation  incr_relation = tarval_cmp(tv_incr, zero);
			ir_relation  relation      = get_Cmp_relation(cmp);
			switch (relation) {
				case ir_relation_less:
				case ir_relation_less_equal:
					if (incr_relation == ir_relation_less)
						/* Prevent: for (i = 42; i <= 42; i--) -> for (i = 43; i >= 43; i--) */
						return;
					/* FALLTHROUGH */
				case ir_relation_greater:
				case ir_relation_greater_equal:
					if (incr_relation == ir_relation_greater)
						/* Prevent: for (i = 42; i >= 42; i++) -> for (i = 41; i >= 41; i++) */
						return;

				default:
					break;
			}
		}

		DB((dbg, LEVEL_2, "  LFTR for %+F\n", cmp));
		set_Cmp_left(cmp, nleft);
		set_Cmp_right(cmp, nright);
		++env->lftr_replaced;
	}
}

/**
 * do linear function test replacement.
 *
 * @param irg   the graph that should be optimized
 * @param env   the IV environment
 */
static void lftr(ir_graph *irg, iv_env *env)
{
	irg_walk_graph(irg, NULL, do_lftr, env);
}

/* Remove any Phi cycles with only one real input. */
void remove_phi_cycles(ir_graph *irg)
{
	assure_irg_properties(irg,
		IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
		| IR_GRAPH_PROPERTY_CONSISTENT_OUTS
		| IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);

	FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");

	DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));

	iv_env env;
	obstack_init(&env.obst);
	env.stack         = NEW_ARR_F(ir_node *, 128);
	env.tos           = 0;
	env.nextDFSnum    = 0;
	env.POnum         = 0;
	env.quad_map      = NULL;
	env.lftr_edges    = NULL;
	env.replaced      = 0;
	env.lftr_replaced = 0;
	env.osr_flags     = 0;
	env.need_postpass = false;
	env.process_scc   = process_phi_only_scc;

	/* Clear all links and move Proj nodes into the
	 * the same block as their predecessors.
	 * This can improve the placement of new nodes.
	 */
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
	irg_walk_graph(irg, NULL, firm_clear_link, NULL);

	/* calculate the post order number for blocks. */
	irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);

	/* calculate the SCC's and drive OSR. */
	do_dfs(irg, &env);
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);

	if (env.replaced) {
		DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n",
		    env.replaced));
	}

	DEL_ARR_F(env.stack);
	obstack_free(&env.obst, NULL);

	confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
}

/**
 * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
 */
static void fix_adds_and_subs(ir_node *irn, void *ctx)
{
	(void)ctx;

	if (is_Add(irn)) {
		ir_mode *mode = get_irn_mode(irn);

		if (mode_is_int(mode)) {
			ir_node *pred = get_Add_left(irn);
			if (get_irn_mode(pred) != mode) {
				ir_node *block = get_nodes_block(pred);

				pred = new_r_Conv(block, pred, mode);
				set_Add_left(irn, pred);
			}
			pred = get_Add_right(irn);
			if (get_irn_mode(pred) != mode) {
				ir_node *block = get_nodes_block(pred);

				pred = new_r_Conv(block, pred, mode);
				set_Add_right(irn, pred);
			}
		}
	} else if (is_Sub(irn)) {
		ir_mode *mode = get_irn_mode(irn);

		if (mode_is_int(mode)) {
			ir_node *left   = get_Sub_left(irn);
			ir_node *right  = get_Sub_right(irn);
			ir_mode *l_mode = get_irn_mode(left);
			ir_mode *r_mode = get_irn_mode(right);

			if (mode_is_int(l_mode) && mode_is_int(r_mode)) {
				if (l_mode != mode) {
					ir_node *block = get_nodes_block(left);

					left = new_r_Conv(block, left, mode);
					set_Sub_left(irn, left);
				}
				if (r_mode != mode) {
					ir_node *block = get_nodes_block(right);

					right = new_r_Conv(block, right, mode);
					set_Sub_right(irn, right);
				}
			}
		} else if (mode_is_reference(mode)) {
			ir_node *left   = get_Sub_left(irn);
			ir_node *right  = get_Sub_right(irn);
			ir_mode *l_mode = get_irn_mode(left);
			ir_mode *r_mode = get_irn_mode(right);
			if (mode_is_int(l_mode)) {
				/* Usually, Sub(I*,P) is an error, hence the verifier rejects it.
				 * However, it is correct in this case, so add Conv to make verifier happy. */
				ir_node *block = get_nodes_block(right);
				ir_node *lconv = new_r_Conv(block, left, r_mode);
				assert(mode_is_reference(r_mode));
				set_Sub_left(irn, lconv);
			}
		}
	}
}

/* Performs Operator Strength Reduction for the passed graph. */
void opt_osr(ir_graph *irg, unsigned flags)
{
	FIRM_DBG_REGISTER(dbg, "firm.opt.osr");

	assure_irg_properties(irg,
		IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
		| IR_GRAPH_PROPERTY_CONSISTENT_OUTS
		| IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);

	DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));

	iv_env env;
	obstack_init(&env.obst);
	env.stack         = NEW_ARR_F(ir_node *, 128);
	env.tos           = 0;
	env.nextDFSnum    = 0;
	env.POnum         = 0;
	env.quad_map      = new_set(quad_cmp, 64);
	env.lftr_edges    = new_set(lftr_cmp, 64);
	env.replaced      = 0;
	env.lftr_replaced = 0;
	env.osr_flags     = flags;
	env.need_postpass = false;
	env.process_scc   = process_scc;

	/* Clear all links and move Proj nodes into the
	 * the same block as its predecessors.
	 * This can improve the placement of new nodes.
	 */
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
	irg_walk_graph(irg, NULL, firm_clear_link, NULL);

	irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);

	/* calculate the SCC's and drive OSR. */
	do_dfs(irg, &env);

	if (env.replaced) {
		if (env.need_postpass)
			irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);

		/* try linear function test replacements */
		lftr(irg, &env);
		(void)lftr;

		DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced,
		    env.lftr_replaced));
	}
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);

	del_set(env.lftr_edges);
	del_set(env.quad_map);
	DEL_ARR_F(env.stack);
	obstack_free(&env.obst, NULL);

	confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
}