2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Operator Strength Reduction.
24 * @author Michael Beck
26 * Implementation of the Operator Strength Reduction algorithm
27 * by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
33 #include "iroptimize.h"
52 #include "firmstat_t.h"
56 /** The debug handle. */
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
61 ir_node *head; /**< the head of the list */
62 ir_tarval *init; /**< the init value iff only one exists. */
63 ir_tarval *incr; /**< the induction variable increment if only a single const exists. */
64 unsigned code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
68 typedef struct node_entry {
69 unsigned DFSnum; /**< the DFS number of this node */
70 unsigned low; /**< the low number of this node */
71 ir_node *header; /**< the header of this node */
72 int in_stack; /**< flag, set if the node is on the stack */
73 ir_node *next; /**< link to the next node the the same scc */
74 scc *pscc; /**< the scc of this node */
75 unsigned POnum; /**< the post order number for blocks */
78 /** The environment. */
79 typedef struct iv_env {
80 struct obstack obst; /**< an obstack for allocations */
81 ir_node **stack; /**< the node stack */
82 size_t tos; /**< tos index */
83 unsigned nextDFSnum; /**< the current DFS number */
84 unsigned POnum; /**< current post order number */
85 set *quad_map; /**< a map from (op, iv, rc) to node */
86 set *lftr_edges; /**< the set of lftr edges */
87 unsigned replaced; /**< number of replaced ops */
88 unsigned lftr_replaced; /**< number of applied linear function test replacements */
89 unsigned osr_flags; /**< additional flags steering the transformation */
90 unsigned need_postpass; /**< set, if a post pass is needed to fix Add and Sub nodes */
91 /** Function called to process a SCC. */
92 void (*process_scc)(scc *pscc, struct iv_env *env);
96 * An entry in the (op, node, node) -> node map.
98 typedef struct quadruple_t {
99 unsigned code; /**< the opcode of the reduced operation */
100 ir_node *op1; /**< the first operand the reduced operation */
101 ir_node *op2; /**< the second operand of the reduced operation */
103 ir_node *res; /**< the reduced operation */
109 typedef struct LFTR_edge {
110 ir_node *src; /**< the source node */
111 ir_node *dst; /**< the destination node */
112 unsigned code; /**< the opcode that must be applied */
113 ir_node *rc; /**< the region const that must be applied */
117 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
120 * Compare two LFTR edges.
122 static int LFTR_cmp(const void *e1, const void *e2, size_t size)
124 const LFTR_edge *l1 = (const LFTR_edge*)e1;
125 const LFTR_edge *l2 = (const LFTR_edge*)e2;
128 return l1->src != l2->src;
134 * @param src the source node of the transition
136 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env)
142 return set_find(LFTR_edge, env->lftr_edges, &key, sizeof(key), hash_ptr(src));
148 * @param src the source node of the edge
149 * @param dst the destination node of the edge
150 * @param code the opcode of the transformed transition
151 * @param rc the region const used in the transition
152 * @param env the environment
154 static void LFTR_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc, iv_env *env)
164 * There might be more than one edge here. This is rather bad
165 * because we currently store only one.
167 (void)set_insert(LFTR_edge, env->lftr_edges, &key, sizeof(key), hash_ptr(src));
171 * Gets the node_entry of a node.
173 * @param irn the node
174 * @param env the environment
176 static node_entry *get_irn_ne(ir_node *irn, iv_env *env)
178 node_entry *e = (node_entry*)get_irn_link(irn);
181 e = OALLOCZ(&env->obst, node_entry);
182 set_irn_link(irn, e);
188 * Gets the scc from an induction variable.
190 * @param iv any node of the induction variable
191 * @param env the environment
193 static scc *get_iv_scc(ir_node *iv, iv_env *env)
195 node_entry *e = get_irn_ne(iv, env);
200 * Check if irn is an IV.
202 * @param irn the node to check
203 * @param env the environment
205 * @returns the header if it is one, NULL else
207 static ir_node *is_iv(ir_node *irn, iv_env *env)
209 return get_irn_ne(irn, env)->header;
213 * Check if irn is a region constant.
214 * The block or irn must strictly dominate the header block.
216 * @param irn the node to check
217 * @param header_block the header block of the induction variable
219 static int is_rc(ir_node *irn, ir_node *header_block)
221 ir_node *block = get_nodes_block(irn);
223 return (block != header_block) && block_dominates(block, header_block);
227 * Set compare function for the quad set.
229 static int quad_cmp(const void *e1, const void *e2, size_t size)
231 const quadruple_t *c1 = (const quadruple_t*)e1;
232 const quadruple_t *c2 = (const quadruple_t*)e2;
235 return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
239 * Check if an reduced operation was already calculated.
241 * @param code the opcode of the operation
242 * @param op1 the first operand of the operation
243 * @param op2 the second operand of the operation
244 * @param env the environment
246 * @return the already reduced node or NULL if this operation is not yet reduced
248 static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env)
250 quadruple_t key, *entry;
256 entry = set_find(quadruple_t, env->quad_map, &key, sizeof(key), (code * 9) ^ hash_ptr(op1) ^ hash_ptr(op2));
263 * Add an reduced operation.
265 * @param code the opcode of the operation
266 * @param op1 the first operand of the operation
267 * @param op2 the second operand of the operation
268 * @param result the result of the reduced operation
269 * @param env the environment
271 static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env)
280 (void)set_insert(quadruple_t, env->quad_map, &key, sizeof(key), (code * 9) ^ hash_ptr(op1) ^ hash_ptr(op2));
284 * Find a location where to place a bin-op whose operands are in
287 * @param block1 the block of the first operand
288 * @param block2 the block of the second operand
290 * Note that we know here that such a place must exists. Moreover, this means
291 * that either block1 dominates block2 or vice versa. So, just return
294 static ir_node *find_location(ir_node *block1, ir_node *block2)
296 if (block_dominates(block1, block2))
298 assert(block_dominates(block2, block1));
303 * Create a node that executes an op1 code op1 operation.
305 * @param code the opcode to execute
306 * @param db debug info to add to the new node
307 * @param op1 the first operand
308 * @param op2 the second operand
309 * @param mode the mode of the new operation
311 * @return the newly created node
313 static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode)
316 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
320 result = new_rd_Mul(db, block, op1, op2, mode);
323 result = new_rd_Add(db, block, op1, op2, mode);
326 result = new_rd_Sub(db, block, op1, op2, mode);
329 panic("Unsupported opcode");
335 * The Apply operation.
337 * @param orig the node that represent the original operation and determines
338 * the opcode, debug-info and mode of a newly created one
339 * @param op1 the first operand
340 * @param op2 the second operand
341 * @param env the environment
343 * @return the newly created node
345 static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env)
347 unsigned code = get_irn_opcode(orig);
348 ir_node *result = search(code, op1, op2, env);
350 if (result == NULL) {
351 dbg_info *db = get_irn_dbg_info(orig);
352 ir_node *op1_header = get_irn_ne(op1, env)->header;
353 ir_node *op2_header = get_irn_ne(op2, env)->header;
355 if (op1_header == header && is_rc(op2, op1_header)) {
356 result = reduce(orig, op1, op2, env);
358 else if (op2_header == header && is_rc(op1, op2_header)) {
359 result = reduce(orig, op2, op1, env);
362 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
363 get_irn_ne(result, env)->header = NULL;
370 * The Reduce operation.
372 * @param orig the node that represent the original operation and determines
373 * the opcode, debug-info and mode of a newly created one
374 * @param iv the induction variable
375 * @param rc the region constant
376 * @param env the environment
378 * @return the reduced node
380 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env)
382 unsigned code = get_irn_opcode(orig);
383 ir_node *result = search(code, iv, rc, env);
385 /* check if we have already done this operation on the iv */
386 if (result == NULL) {
387 node_entry *e, *iv_e;
389 ir_mode *mode = get_irn_mode(orig);
391 result = exact_copy(iv);
393 if (get_irn_mode(result) != mode) {
395 * Beware: we must always create a new induction variable with the same mode
396 * as the node we are replacing. Especially this means the mode might be changed
397 * from P to I and back. This is always possible, because we have only Phi, Add
399 * However, this might lead to AddIs(Iu,Is) which we must fix. The best way to do this
400 * seems to be a post-pass, or we might end with useless Conv's.
402 set_irn_mode(result, mode);
403 env->need_postpass = 1;
405 add(code, iv, rc, result, env);
406 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
407 get_irn_opname(orig), rc));
409 iv_e = get_irn_ne(iv, env);
410 e = get_irn_ne(result, env);
411 e->header = iv_e->header;
413 /* create the LFTR edge */
414 LFTR_add(iv, result, code, rc, env);
416 for (i = get_irn_arity(result) - 1; i >= 0; --i) {
417 ir_node *o = get_irn_n(result, i);
419 e = get_irn_ne(o, env);
420 if (e->header == iv_e->header)
421 o = reduce(orig, o, rc, env);
422 else if (is_Phi(result) || code == iro_Mul)
423 o = apply(iv_e->header, orig, o, rc, env);
424 set_irn_n(result, i, o);
427 DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
428 get_irn_opname(orig), rc));
434 * Update the scc for a newly created IV.
436 static void update_scc(ir_node *iv, node_entry *e, iv_env *env)
439 ir_node *header = e->header;
440 waitq *wq = new_waitq();
442 DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n "));
446 ir_node *irn = (ir_node*)waitq_get(wq);
447 node_entry *ne = get_irn_ne(irn, env);
451 ne->next = pscc->head;
453 DB((dbg, LEVEL_2, " %+F,", irn));
455 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
456 ir_node *pred = get_irn_n(irn, i);
457 node_entry *pe = get_irn_ne(pred, env);
459 if (pe->header == header && pe->pscc == NULL) {
460 /* set the pscc here to ensure that the node is NOT enqueued another time */
465 } while (! waitq_empty(wq));
467 DB((dbg, LEVEL_2, "\n"));
471 * The Replace operation. We found a node representing iv (+,-,*) rc
472 * that can be removed by replacing the induction variable iv by a new
473 * one that 'applies' the operation 'irn'.
475 * @param irn the node that will be replaced
476 * @param iv the induction variable
477 * @param rc the region constant
478 * @param env the environment
480 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env)
484 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
486 result = reduce(irn, iv, rc, env);
490 hook_strength_red(get_irn_irg(irn), irn);
491 exchange(irn, result);
492 e = get_irn_ne(result, env);
493 if (e->pscc == NULL) {
494 e->pscc = OALLOCZ(&env->obst, scc);
495 update_scc(result, e, env);
504 * Check if an IV represents a counter with constant limits.
506 * @param iv any node of the induction variable
507 * @param env the environment
509 static int is_counter_iv(ir_node *iv, iv_env *env)
511 node_entry *e = get_irn_ne(iv, env);
513 ir_node *have_init = NULL;
514 ir_node *have_incr = NULL;
515 ir_opcode code = iro_Bad;
518 if (pscc->code != 0) {
519 /* already analysed */
520 return pscc->code != iro_Bad;
523 pscc->code = iro_Bad;
524 for (irn = pscc->head; irn != NULL; irn = e->next) {
526 if (have_incr != NULL)
529 have_incr = get_Add_right(irn);
530 if (! is_Const(have_incr)) {
531 have_incr = get_Add_left(irn);
532 if (! is_Const(have_incr))
536 } else if (is_Sub(irn)) {
537 if (have_incr != NULL)
540 have_incr = get_Sub_right(irn);
541 if (! is_Const(have_incr))
544 } else if (is_Phi(irn)) {
547 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
548 ir_node *pred = get_Phi_pred(irn, i);
549 node_entry *ne = get_irn_ne(pred, env);
551 if (ne->header == e->header)
553 if (have_init != NULL)
556 if (! is_Const(pred))
561 e = get_irn_ne(irn, env);
563 pscc->init = get_Const_tarval(have_init);
564 pscc->incr = get_Const_tarval(have_incr);
566 return code != iro_Bad;
570 * Check the users of an induction variable for register pressure.
572 * @param iv any node of the induction variable
573 * @param env the environment
575 * @return non-zero if the register pressure is estimated
576 * to not increase, zero else
578 static int check_users_for_reg_pressure(ir_node *iv, iv_env *env)
581 ir_node *have_user = NULL;
582 ir_node *have_cmp = NULL;
583 node_entry *e = get_irn_ne(iv, env);
586 for (irn = pscc->head; irn != NULL; irn = e->next) {
587 foreach_out_edge(irn, edge) {
588 ir_node *user = get_edge_src_irn(edge);
589 node_entry *ne = get_irn_ne(user, env);
591 if (e->header == ne->header) {
592 /* found user from the same IV */
596 if (have_cmp != NULL) {
597 /* more than one cmp, for now end here */
602 /* user is a real user of the IV */
603 if (have_user != NULL) {
604 /* found the second user */
610 e = get_irn_ne(irn, env);
613 if (have_user == NULL) {
614 /* no user, ignore */
618 if (have_cmp == NULL) {
619 /* fine, only one user, try to reduce */
623 * We found one user AND at least one cmp.
624 * We should check here if we can transform the Cmp.
626 * For now our capabilities for doing linear function test
627 * are limited, so check if the iv has the right form: Only ONE
628 * Phi, only one Add/Sub with a Const.
630 if (! is_counter_iv(iv, env))
634 * Ok, we have only one increment AND it is a Const, we might be able
635 * to do a linear function test replacement, so go on.
641 * Check if a node can be replaced (+, -, *).
643 * @param irn the node to check
644 * @param env the environment
646 * @return non-zero if irn should be Replace'd
648 static int check_replace(ir_node *irn, iv_env *env)
650 ir_node *left, *right, *iv, *rc;
651 ir_op *op = get_irn_op(irn);
652 unsigned code = get_op_code(op);
661 left = get_binop_left(irn);
662 right = get_binop_right(irn);
664 liv = is_iv(left, env);
665 riv = is_iv(right, env);
666 if (liv && is_rc(right, liv)) {
667 iv = left; rc = right;
669 else if (riv && is_op_commutative(op) &&
671 iv = right; rc = left;
675 if (env->osr_flags & osr_flag_keep_reg_pressure) {
676 if (! check_users_for_reg_pressure(iv, env))
679 return replace(irn, iv, rc, env);
689 * Check which SCC's are induction variables.
692 * @param env the environment
694 static void classify_iv(scc *pscc, iv_env *env)
696 ir_node *irn, *next, *header = NULL;
697 node_entry *b, *h = NULL;
698 int j, only_phi, num_outside;
701 /* find the header block for this scc */
702 for (irn = pscc->head; irn; irn = next) {
703 node_entry *e = (node_entry*)get_irn_link(irn);
704 ir_node *block = get_nodes_block(irn);
707 b = get_irn_ne(block, env);
710 if (h->POnum < b->POnum) {
721 /* check if this scc contains only Phi, Add or Sub nodes */
725 for (irn = pscc->head; irn; irn = next) {
726 node_entry *e = get_irn_ne(irn, env);
729 switch (get_irn_opcode(irn)) {
733 ir_node *left = get_Sub_left(irn);
734 node_entry *left_entry = get_irn_ne(left, env);
735 ir_node *right = get_Sub_right(irn);
736 node_entry *right_entry = get_irn_ne(right, env);
738 if (left_entry->pscc != e->pscc ||
739 (right_entry->pscc != e->pscc && !is_rc(right, header))) {
741 * Not an induction variable.
742 * Region constant are only allowed on right hand side.
753 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
754 ir_node *pred = get_irn_n(irn, j);
755 node_entry *pe = get_irn_ne(pred, env);
757 if (pe->pscc != e->pscc) {
758 /* not in the same SCC, must be a region const */
759 if (! is_rc(pred, header)) {
760 /* not an induction variable */
766 } else if (out_rc != pred) {
773 /* not an induction variable */
777 /* found an induction variable */
778 DB((dbg, LEVEL_2, " Found an induction variable:\n "));
779 if (only_phi && num_outside == 1) {
780 /* a phi cycle with only one real predecessor can be collapsed */
781 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
783 for (irn = pscc->head; irn; irn = next) {
784 node_entry *e = get_irn_ne(irn, env);
787 exchange(irn, out_rc);
793 /* set the header for every node in this scc */
794 for (irn = pscc->head; irn; irn = next) {
795 node_entry *e = get_irn_ne(irn, env);
798 DB((dbg, LEVEL_2, " %+F,", irn));
800 DB((dbg, LEVEL_2, "\n"));
804 for (irn = pscc->head; irn; irn = next) {
805 node_entry *e = get_irn_ne(irn, env);
813 * Process an SCC for the operator strength reduction.
815 * @param pscc the SCC
816 * @param env the environment
818 static void process_scc(scc *pscc, iv_env *env)
820 ir_node *head = pscc->head;
821 node_entry *e = (node_entry*)get_irn_link(head);
827 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
828 for (irn = pscc->head; irn != NULL; irn = next) {
829 node_entry *e = (node_entry*)get_irn_link(irn);
833 DB((dbg, LEVEL_4, " %+F,", irn));
835 DB((dbg, LEVEL_4, "\n"));
839 if (e->next == NULL) {
840 /* this SCC has only a single member */
841 check_replace(head, env);
843 classify_iv(pscc, env);
848 * If an SCC is a Phi only cycle, remove it.
850 * @param pscc an SCC that consists of Phi nodes only
851 * @param env the environment
853 static void remove_phi_cycle(scc *pscc, iv_env *env)
859 /* check if this scc contains only Phi nodes */
861 for (irn = pscc->head; irn; irn = next) {
862 node_entry *e = get_irn_ne(irn, env);
868 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
869 ir_node *pred = get_irn_n(irn, j);
870 node_entry *pe = get_irn_ne(pred, env);
872 if (pe->pscc != e->pscc) {
873 /* not in the same SCC, must be the only input */
876 } else if (out_rc != pred) {
882 /* found a Phi cycle */
883 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
885 for (irn = pscc->head; irn; irn = next) {
886 node_entry *e = get_irn_ne(irn, env);
889 exchange(irn, out_rc);
895 * Process a SCC for the Phi cycle remove.
897 * @param pscc the SCC
898 * @param env the environment
900 static void process_phi_only_scc(scc *pscc, iv_env *env)
902 ir_node *head = pscc->head;
903 node_entry *e = (node_entry*)get_irn_link(head);
909 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
910 for (irn = pscc->head; irn; irn = next) {
911 node_entry *e = (node_entry*)get_irn_link(irn);
915 DB((dbg, LEVEL_4, " %+F,", irn));
917 DB((dbg, LEVEL_4, "\n"));
922 remove_phi_cycle(pscc, env);
927 * Push a node onto the stack.
929 * @param env the environment
930 * @param n the node to push
932 static void push(iv_env *env, ir_node *n)
936 if (env->tos == ARR_LEN(env->stack)) {
937 size_t nlen = ARR_LEN(env->stack) * 2;
938 ARR_RESIZE(ir_node *, env->stack, nlen);
940 env->stack[env->tos++] = n;
941 e = get_irn_ne(n, env);
946 * Pop a node from the stack.
948 * @param env the environment
950 * @return The topmost node
952 static ir_node *pop(iv_env *env)
954 ir_node *n = env->stack[--env->tos];
955 node_entry *e = get_irn_ne(n, env);
962 * Do Tarjan's SCC algorithm and drive OSR.
964 * @param irn start at this node
965 * @param env the environment
967 static void dfs(ir_node *irn, iv_env *env)
970 node_entry *node = get_irn_ne(irn, env);
972 mark_irn_visited(irn);
974 /* do not put blocks into the scc */
976 n = get_irn_arity(irn);
977 for (i = 0; i < n; ++i) {
978 ir_node *pred = get_irn_n(irn, i);
980 if (!irn_visited(pred))
984 ir_node *block = get_nodes_block(irn);
986 node->DFSnum = env->nextDFSnum++;
987 node->low = node->DFSnum;
990 /* handle the block */
991 if (!irn_visited(block))
994 n = get_irn_arity(irn);
995 for (i = 0; i < n; ++i) {
996 ir_node *pred = get_irn_n(irn, i);
997 node_entry *o = get_irn_ne(pred, env);
999 if (!irn_visited(pred)) {
1001 node->low = MIN(node->low, o->low);
1003 if (o->DFSnum < node->DFSnum && o->in_stack)
1004 node->low = MIN(o->DFSnum, node->low);
1006 if (node->low == node->DFSnum) {
1007 scc *pscc = OALLOCZ(&env->obst, scc);
1014 e = get_irn_ne(x, env);
1016 e->next = pscc->head;
1020 env->process_scc(pscc, env);
1026 * Do the DFS by starting at the End node of a graph.
1028 * @param irg the graph to process
1029 * @param env the environment
1031 static void do_dfs(ir_graph *irg, iv_env *env)
1033 ir_node *end = get_irg_end(irg);
1036 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1038 inc_irg_visited(irg);
1040 /* visit all visible nodes */
1043 /* visit the keep-alives */
1044 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
1045 ir_node *ka = get_End_keepalive(end, i);
1047 if (!irn_visited(ka))
1051 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1055 * Post-block-walker: assign the post-order number.
1057 static void assign_po(ir_node *block, void *ctx)
1059 iv_env *env = (iv_env*)ctx;
1060 node_entry *e = get_irn_ne(block, env);
1062 e->POnum = env->POnum++;
1066 * Apply one LFTR edge operation.
1067 * Return NULL if the transformation cannot be done safely without
1070 * @param iv the induction variable
1071 * @param rc the constant that should be translated
1072 * @param e the LFTR edge
1073 * @param env the IV environment
1075 * @return the translated region constant or NULL
1076 * if the translation was not possible
1079 * In the current implementation only the last edge is stored, so
1080 * only one chain exists. That's why we might miss some opportunities.
1082 static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env)
1084 if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1085 ir_tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end;
1086 tarval_int_overflow_mode_t ovmode;
1090 if (! is_counter_iv(iv, env)) {
1091 DB((dbg, LEVEL_4, " not counter IV"));
1095 /* overflow can only be decided for Consts */
1096 if (! is_Const(e->rc)) {
1097 if (e->code == iro_Add && mode_is_reference(get_irn_mode(e->rc))) {
1098 /* However we allow ONE Pointer Add, as pointer arithmetic with wrap
1099 around is undefined anyway */
1100 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->rc));
1102 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
1106 tv_l = get_Const_tarval(rc);
1107 tv_r = get_Const_tarval(e->rc);
1109 ovmode = tarval_get_integer_overflow_mode();
1110 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1112 pscc = get_iv_scc(iv, env);
1113 tv_incr = pscc->incr;
1114 tv_init = pscc->init;
1117 * Check that no overflow occurs:
1118 * init must be transformed without overflow
1119 * the new rc must be transformed without overflow
1120 * rc +/- incr must be possible without overflow
1124 tv = tarval_mul(tv_l, tv_r);
1125 tv_init = tarval_mul(tv_init, tv_r);
1126 tv_incr = tarval_mul(tv_incr, tv_r);
1127 DB((dbg, LEVEL_4, " * %+F", tv_r));
1130 tv = tarval_add(tv_l, tv_r);
1131 tv_init = tarval_add(tv_init, tv_r);
1132 DB((dbg, LEVEL_4, " + %+F", tv_r));
1135 tv = tarval_sub(tv_l, tv_r, NULL);
1136 tv_init = tarval_sub(tv_init, tv_r, NULL);
1137 DB((dbg, LEVEL_4, " - %+F", tv_r));
1140 panic("Unsupported opcode");
1143 if (tv == tarval_bad || tv_init == tarval_bad) {
1144 tarval_set_integer_overflow_mode(ovmode);
1145 DB((dbg, LEVEL_4, " = OVERFLOW"));
1149 if (pscc->code == iro_Add) {
1150 tv_end = tarval_add(tv, tv_incr);
1152 assert(pscc->code == iro_Sub);
1153 tv_end = tarval_sub(tv, tv_incr, NULL);
1156 tarval_set_integer_overflow_mode(ovmode);
1158 if (tv_end == tarval_bad) {
1159 DB((dbg, LEVEL_4, " = OVERFLOW"));
1162 irg = get_irn_irg(iv);
1163 return new_r_Const(irg, tv);
1165 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->dst));
1169 * Applies the operations represented by the LFTR edges to a
1170 * region constant and returns the value.
1171 * Return NULL if the transformation cannot be done safely without
1174 * @param pIV points to the IV node that starts the LFTR edge chain
1175 * after translation points to the new IV
1176 * @param rc the region constant that should be translated
1177 * @param env the IV environment
1179 * @return the translated region constant or NULL
1180 * if the translation was not possible
1182 static ir_node *applyEdges(ir_node **pIV, ir_node *rc, iv_env *env)
1185 if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1186 /* overflow can only be decided for Consts */
1187 if (! is_counter_iv(iv, env)) {
1188 DB((dbg, LEVEL_4, "not counter IV\n", rc));
1191 if (! is_Const(rc)) {
1192 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
1195 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
1199 LFTR_edge *e = LFTR_find(iv, env);
1201 rc = applyOneEdge(iv, rc, e, env);
1206 DB((dbg, LEVEL_3, "\n"));
1212 * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
1213 * and tries to optimize them.
1215 static void do_lftr(ir_node *cmp, void *ctx)
1217 iv_env *env = (iv_env*)ctx;
1218 ir_node *left, *right, *liv, *riv;
1220 ir_node *nleft = NULL, *nright = NULL;
1225 left = get_Cmp_left(cmp);
1226 right = get_Cmp_right(cmp);
1228 liv = is_iv(left, env);
1229 riv = is_iv(right, env);
1230 if (liv && is_rc(right, liv)) {
1231 iv = left; rc = right;
1233 nright = applyEdges(&iv, rc, env);
1236 else if (riv && is_rc(left, riv)) {
1237 iv = right; rc = left;
1239 nleft = applyEdges(&iv, rc, env);
1243 if (nleft && nright) {
1244 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
1245 set_Cmp_left(cmp, nleft);
1246 set_Cmp_right(cmp, nright);
1247 ++env->lftr_replaced;
1252 * do linear function test replacement.
1254 * @param irg the graph that should be optimized
1255 * @param env the IV environment
1257 static void lftr(ir_graph *irg, iv_env *env)
1259 irg_walk_graph(irg, NULL, do_lftr, env);
1262 /* Remove any Phi cycles with only one real input. */
1263 void remove_phi_cycles(ir_graph *irg)
1267 assure_irg_properties(irg,
1268 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
1269 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
1270 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
1272 FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
1274 DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));
1276 obstack_init(&env.obst);
1277 env.stack = NEW_ARR_F(ir_node *, 128);
1281 env.quad_map = NULL;
1282 env.lftr_edges = NULL;
1284 env.lftr_replaced = 0;
1286 env.need_postpass = 0;
1287 env.process_scc = process_phi_only_scc;
1289 /* Clear all links and move Proj nodes into the
1290 * the same block as their predecessors.
1291 * This can improve the placement of new nodes.
1293 irg_walk_graph(irg, NULL, firm_clear_link, NULL);
1295 /* calculate the post order number for blocks. */
1296 irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1298 /* calculate the SCC's and drive OSR. */
1299 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1301 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1304 DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n",
1308 DEL_ARR_F(env.stack);
1309 obstack_free(&env.obst, NULL);
1311 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
1314 ir_graph_pass_t *remove_phi_cycles_pass(const char *name)
1316 return def_graph_pass(name ? name : "remove_phi_cycles", remove_phi_cycles);
1320 * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
1322 static void fix_adds_and_subs(ir_node *irn, void *ctx)
1327 ir_mode *mode = get_irn_mode(irn);
1329 if (mode_is_int(mode)) {
1332 pred = get_Add_left(irn);
1333 if (get_irn_mode(pred) != mode) {
1334 ir_node *block = get_nodes_block(pred);
1336 pred = new_r_Conv(block, pred, mode);
1337 set_Add_left(irn, pred);
1339 pred = get_Add_right(irn);
1340 if (get_irn_mode(pred) != mode) {
1341 ir_node *block = get_nodes_block(pred);
1343 pred = new_r_Conv(block, pred, mode);
1344 set_Add_right(irn, pred);
1347 } else if (is_Sub(irn)) {
1348 ir_mode *mode = get_irn_mode(irn);
1350 if (mode_is_int(mode)) {
1351 ir_node *left = get_Sub_left(irn);
1352 ir_node *right = get_Sub_right(irn);
1353 ir_mode *l_mode = get_irn_mode(left);
1354 ir_mode *r_mode = get_irn_mode(right);
1356 if (mode_is_int(l_mode) && mode_is_int(r_mode)) {
1357 if (l_mode != mode) {
1358 ir_node *block = get_nodes_block(left);
1360 left = new_r_Conv(block, left, mode);
1361 set_Sub_left(irn, left);
1363 if (r_mode != mode) {
1364 ir_node *block = get_nodes_block(right);
1366 right = new_r_Conv(block, right, mode);
1367 set_Sub_right(irn, right);
1370 } else if (mode_is_reference(mode)) {
1371 ir_node *left = get_Sub_left(irn);
1372 ir_node *right = get_Sub_right(irn);
1373 ir_mode *l_mode = get_irn_mode(left);
1374 ir_mode *r_mode = get_irn_mode(right);
1375 if (mode_is_int(l_mode)) {
1376 /* Usually, Sub(I*,P) is an error, hence the verifier rejects it.
1377 * However, it is correct in this case, so add Conv to make verifier happy. */
1378 ir_node *block = get_nodes_block(right);
1379 ir_node *lconv = new_r_Conv(block, left, r_mode);
1380 assert(mode_is_reference(r_mode));
1381 set_Sub_left(irn, lconv);
1387 /* Performs Operator Strength Reduction for the passed graph. */
1388 void opt_osr(ir_graph *irg, unsigned flags)
1392 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
1394 assure_irg_properties(irg,
1395 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
1396 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
1397 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
1399 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
1401 obstack_init(&env.obst);
1402 env.stack = NEW_ARR_F(ir_node *, 128);
1406 env.quad_map = new_set(quad_cmp, 64);
1407 env.lftr_edges = new_set(LFTR_cmp, 64);
1409 env.lftr_replaced = 0;
1410 env.osr_flags = flags;
1411 env.need_postpass = 0;
1412 env.process_scc = process_scc;
1414 /* Clear all links and move Proj nodes into the
1415 * the same block as its predecessors.
1416 * This can improve the placement of new nodes.
1418 irg_walk_graph(irg, NULL, firm_clear_link, NULL);
1420 irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1422 /* calculate the SCC's and drive OSR. */
1423 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1427 if (env.need_postpass)
1428 irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);
1430 /* try linear function test replacements */
1434 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
1436 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1438 del_set(env.lftr_edges);
1439 del_set(env.quad_map);
1440 DEL_ARR_F(env.stack);
1441 obstack_free(&env.obst, NULL);
1443 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
1446 typedef struct pass_t {
1447 ir_graph_pass_t pass;
1452 * Wrapper for running opt_osr() as an ir_graph pass.
1454 static int pass_wrapper(ir_graph *irg, void *context)
1456 pass_t *pass = (pass_t*)context;
1457 opt_osr(irg, pass->flags);
1461 ir_graph_pass_t *opt_osr_pass(const char *name, unsigned flags)
1463 pass_t *pass = XMALLOCZ(pass_t);
1465 pass->flags = flags;
1466 return def_graph_pass_constructor(
1467 &pass->pass, name ? name : "osr", pass_wrapper);