2 * Copyright (C) 1995-2008 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
27 * Implementation of the Operator Strength Reduction algorithm
28 * by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
36 #include "iroptimize.h"
57 /** The debug handle. */
58 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
62 ir_node *head; /**< the head of the list */
63 tarval *init; /**< the init value iff only one exists. */
64 tarval *incr; /**< the induction variable increment if only a single const exists. */
65 unsigned code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
69 typedef struct node_entry {
70 unsigned DFSnum; /**< the DFS number of this node */
71 unsigned low; /**< the low number of this node */
72 ir_node *header; /**< the header of this node */
73 int in_stack; /**< flag, set if the node is on the stack */
74 ir_node *next; /**< link to the next node the the same scc */
75 scc *pscc; /**< the scc of this node */
76 unsigned POnum; /**< the post order number for blocks */
79 /** The environment. */
80 typedef struct iv_env {
81 struct obstack obst; /**< an obstack for allocations */
82 ir_node **stack; /**< the node stack */
83 int tos; /**< tos index */
84 unsigned nextDFSnum; /**< the current DFS number */
85 unsigned POnum; /**< current post order number */
86 set *quad_map; /**< a map from (op, iv, rc) to node */
87 set *lftr_edges; /**< the set of lftr edges */
88 unsigned replaced; /**< number of replaced ops */
89 unsigned lftr_replaced; /**< number of applied linear function test replacements */
90 unsigned flags; /**< additional flags */
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 ir_opcode 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 ir_opcode 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) {
123 const LFTR_edge *l1 = e1;
124 const LFTR_edge *l2 = e2;
127 return l1->src != l2->src;
133 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
138 return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
144 static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv_env *env) {
153 * There might be more than one edge here. This is rather bad
154 * because we currently store only one.
156 // assert(LFTR_find(src, env) == NULL);
157 set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
161 * Gets the node_entry of a node
163 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
164 node_entry *e = get_irn_link(irn);
167 e = obstack_alloc(&env->obst, sizeof(*e));
168 memset(e, 0, sizeof(*e));
169 set_irn_link(irn, e);
175 * Gets the scc from an IV.
177 static scc *get_iv_scc(ir_node *iv, iv_env *env) {
178 node_entry *e = get_irn_ne(iv, env);
183 * Check if irn is an IV.
185 * @param irn the node to check
186 * @param env the environment
188 * @returns the header if it is one, NULL else
190 static ir_node *is_iv(ir_node *irn, iv_env *env) {
191 return get_irn_ne(irn, env)->header;
195 * Check if irn is a region constant.
196 * The block or irn must strictly dominate the header block.
198 * @param irn the node to check
199 * @param header_block the header block of the induction variable
201 static int is_rc(ir_node *irn, ir_node *header_block) {
202 ir_node *block = get_nodes_block(irn);
204 return (block != header_block) && block_dominates(block, header_block);
208 * Set compare function for the quad set.
210 static int quad_cmp(const void *e1, const void *e2, size_t size) {
211 const quadruple_t *c1 = e1;
212 const quadruple_t *c2 = e2;
215 return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
219 * Check if an reduced operation was already calculated.
221 * @param code the opcode of the operation
222 * @param op1 the first operand of the operation
223 * @param op2 the second operand of the operation
224 * @param env the environment
226 * @return the already reduced node or NULL if this operation is not yet reduced
228 static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
229 quadruple_t key, *entry;
235 entry = set_find(env->quad_map, &key, sizeof(key),
236 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
243 * Add an reduced operation.
245 * @param code the opcode of the operation
246 * @param op1 the first operand of the operation
247 * @param op2 the second operand of the operation
248 * @param result the result of the reduced operation
249 * @param env the environment
251 static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
259 set_insert(env->quad_map, &key, sizeof(key),
260 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
264 * Find a location where to place a bin-op whose operands are in
267 * @param block1 the block of the first operand
268 * @param block2 the block of the second operand
270 * Note that we know here that such a place must exists. Moreover, this means
271 * that either block1 dominates block2 or vice versa. So, just return
274 static ir_node *find_location(ir_node *block1, ir_node *block2) {
275 if (block_dominates(block1, block2))
277 assert(block_dominates(block2, block1));
282 * Create a node that executes an op1 code op1 operation.
284 * @param code the opcode to execute
285 * @param db debug info to add to the new node
286 * @param op1 the first operand
287 * @param op2 the second operand
288 * @param mode the mode of the new operation
290 * @return the newly created node
292 static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
293 ir_graph *irg = current_ir_graph;
295 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
299 result = new_rd_Mul(db, irg, block, op1, op2, mode);
302 result = new_rd_Add(db, irg, block, op1, op2, mode);
305 result = new_rd_Sub(db, irg, block, op1, op2, mode);
315 * The Apply operation.
317 * @param orig the node that represent the original operation and determines
318 * the opcode, debug-info and mode of a newly created one
319 * @param op1 the first operand
320 * @param op2 the second operand
321 * @param env the environment
323 * @return the newly created node
325 static ir_node *apply(ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
326 ir_opcode code = get_irn_opcode(orig);
327 ir_node *result = search(code, op1, op2, env);
330 dbg_info *db = get_irn_dbg_info(orig);
331 ir_node *op1_header = get_irn_ne(op1, env)->header;
332 ir_node *op2_header = get_irn_ne(op2, env)->header;
334 if (op1_header != NULL && is_rc(op2, op1_header)) {
335 result = reduce(orig, op1, op2, env);
337 else if (op2_header != NULL && is_rc(op1, op2_header)) {
338 result = reduce(orig, op2, op1, env);
341 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
342 get_irn_ne(result, env)->header = NULL; }
348 * The Reduce operation.
350 * @param orig the node that represent the original operation and determines
351 * the opcode, debug-info and mode of a newly created one
352 * @param iv the induction variable
353 * @param rc the region constant
354 * @param env the environment
356 * @return the reduced node
358 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
359 ir_opcode code = get_irn_opcode(orig);
360 ir_node *result = search(code, iv, rc, env);
363 node_entry *e, *iv_e;
365 ir_mode *mode = get_irn_mode(orig);
367 result = exact_copy(iv);
369 /* Beware: we must always create a new induction variable with the same mode
370 as the node we are replacing. Especially this means the mode might be changed
371 from P to I and back. This is always possible, because we have only Phi, Add
373 set_irn_mode(result, mode);
374 add(code, iv, rc, result, env);
375 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
376 get_irn_opname(orig), rc));
378 iv_e = get_irn_ne(iv, env);
379 e = get_irn_ne(result, env);
380 e->header = iv_e->header;
382 /* create the LFTR edge */
383 LFTR_add(iv, result, code, rc, env);
385 n = get_irn_arity(result);
386 for (i = 0; i < n; ++i) {
387 ir_node *o = get_irn_n(result, i);
389 e = get_irn_ne(o, env);
390 if (e->header == iv_e->header)
391 o = reduce(orig, o, rc, env);
392 else if (is_Phi(result) || code == iro_Mul)
393 o = apply(orig, o, rc, env);
394 set_irn_n(result, i, o);
398 DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
399 get_irn_opname(orig), rc));
405 * Update the scc for a newly created IV.
407 static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
409 ir_node *header = e->header;
410 waitq *wq = new_waitq();
412 DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n "));
416 ir_node *irn = waitq_get(wq);
417 node_entry *ne = get_irn_ne(irn, env);
421 ne->next = pscc->head;
423 DB((dbg, LEVEL_2, " %+F,", irn));
425 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
426 ir_node *pred = get_irn_n(irn, i);
427 node_entry *pe = get_irn_ne(pred, env);
429 if (pe->header == header && pe->pscc == NULL) {
430 /* set the psc here to ensure that the node is NOT enqueued another time */
435 } while (! waitq_empty(wq));
437 DB((dbg, LEVEL_2, "\n"));
441 * The Replace operation.
443 * @param irn the node that will be replaced
444 * @param iv the induction variable
445 * @param rc the region constant
446 * @param env the environment
448 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
451 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
453 result = reduce(irn, iv, rc, env);
457 hook_strength_red(current_ir_graph, irn);
458 exchange(irn, result);
459 e = get_irn_ne(result, env);
460 if (e->pscc == NULL) {
461 e->pscc = obstack_alloc(&env->obst, sizeof(*e->pscc));
462 memset(e->pscc, 0, sizeof(*e->pscc));
463 update_scc(result, e, env);
472 * check if a given node is a mul with 2, 4, 8
474 static int is_x86_shift_const(ir_node *mul) {
480 /* normalization put constants on the right side */
481 rc = get_Mul_right(mul);
483 tarval *tv = get_Const_tarval(rc);
485 if (tarval_is_long(tv)) {
486 long value = get_tarval_long(tv);
488 if (value == 2 || value == 4 || value == 8) {
489 /* do not reduce multiplications by 2, 4, 8 */
498 * Check if an IV represents a counter with constant limits.
500 static int is_counter_iv(ir_node *iv, iv_env *env) {
501 node_entry *e = get_irn_ne(iv, env);
503 ir_node *have_init = NULL;
504 ir_node *have_incr = NULL;
505 ir_opcode code = iro_Bad;
508 if (pscc->code != 0) {
509 /* already analysed */
510 return pscc->code != iro_Bad;
513 pscc->code = iro_Bad;
514 for (irn = pscc->head; irn != NULL; irn = e->next) {
516 if (have_incr != NULL)
519 have_incr = get_Add_right(irn);
520 if (! is_Const(have_incr)) {
521 have_incr = get_Add_left(irn);
522 if (! is_Const(have_incr))
526 } else if (is_Sub(irn)) {
527 if (have_incr != NULL)
530 have_incr = get_Sub_right(irn);
531 if (! is_Const(have_incr))
534 } else if (is_Phi(irn)) {
537 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
538 ir_node *pred = get_Phi_pred(irn, i);
539 node_entry *ne = get_irn_ne(pred, env);
541 if (ne->header == e->header)
543 if (have_init != NULL)
546 if (! is_Const(pred))
551 e = get_irn_ne(irn, env);
553 pscc->init = get_Const_tarval(have_init);
554 pscc->incr = get_Const_tarval(have_incr);
556 return code != iro_Bad;
560 * Check the users of an induction variable for register pressure.
562 static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
563 ir_node *irn, *header;
564 ir_node *have_user = NULL;
565 ir_node *have_cmp = NULL;
566 node_entry *e = get_irn_ne(iv, env);
570 for (irn = pscc->head; irn != NULL; irn = e->next) {
571 const ir_edge_t *edge;
573 foreach_out_edge(irn, edge) {
574 ir_node *user = get_edge_src_irn(edge);
575 node_entry *ne = get_irn_ne(user, env);
577 if (e->header == ne->header) {
578 /* found user from the same IV */
582 if (have_cmp != NULL) {
583 /* more than one cmp, for now end here */
588 /* user is a real user of the IV */
589 if (have_user != NULL) {
590 /* found the second user */
596 e = get_irn_ne(irn, env);
599 if (have_user == NULL) {
600 /* no user, ignore */
604 if (have_cmp == NULL) {
605 /* fine, only one user, try to reduce */
609 * We found one user AND at least one cmp.
610 * We should check here if we can transform the Cmp.
612 * For now our capabilities for doing linear function test
613 * are limited, so check if the iv has the right form: Only ONE
614 * phi, only one Add/Sub with a Const
616 if (! is_counter_iv(iv, env))
620 * Ok, we have only one increment AND it is a Const, we might be able
621 * to do a linear function test replacement, so go on.
627 * Check if a node can be replaced (+, -, *).
629 * @param irn the node to check
630 * @param env the environment
632 * @return non-zero if irn should be Replace'd
634 static int check_replace(ir_node *irn, iv_env *env) {
635 ir_node *left, *right, *iv, *rc;
636 ir_op *op = get_irn_op(irn);
637 ir_opcode code = get_op_code(op);
646 left = get_binop_left(irn);
647 right = get_binop_right(irn);
649 liv = is_iv(left, env);
650 riv = is_iv(right, env);
651 if (liv && is_rc(right, liv)) {
652 iv = left; rc = right;
654 else if (riv && is_op_commutative(op) &&
656 iv = right; rc = left;
660 if (env->flags & osr_flag_keep_reg_pressure) {
661 if (! check_users_for_reg_pressure(iv, env))
664 /* check for x86 constants */
665 if (env->flags & osr_flag_ignore_x86_shift)
666 if (is_x86_shift_const(irn))
669 return replace(irn, iv, rc, env);
679 * Check which SCC's are induction variables.
682 * @param env the environment
684 static void classify_iv(scc *pscc, iv_env *env) {
685 ir_node *irn, *next, *header = NULL;
686 node_entry *b, *h = NULL;
687 int j, only_phi, num_outside;
690 /* find the header block for this scc */
691 for (irn = pscc->head; irn; irn = next) {
692 node_entry *e = get_irn_link(irn);
693 ir_node *block = get_nodes_block(irn);
696 b = get_irn_ne(block, env);
699 if (h->POnum < b->POnum) {
710 /* check if this scc contains only Phi, Add or Sub nodes */
714 for (irn = pscc->head; irn; irn = next) {
715 node_entry *e = get_irn_ne(irn, env);
718 switch (get_irn_opcode(irn)) {
724 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
725 ir_node *pred = get_irn_n(irn, j);
726 node_entry *pe = get_irn_ne(pred, env);
728 if (pe->pscc != e->pscc) {
729 /* not in the same SCC, must be a region const */
730 if (! is_rc(pred, header)) {
731 /* not an induction variable */
737 } else if (out_rc != pred) {
744 /* not an induction variable */
748 /* found an induction variable */
749 DB((dbg, LEVEL_2, " Found an induction variable:\n "));
750 if (only_phi && num_outside == 1) {
751 /* a phi cycle with only one real predecessor can be collapsed */
752 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
754 for (irn = pscc->head; irn; irn = next) {
755 node_entry *e = get_irn_ne(irn, env);
758 exchange(irn, out_rc);
764 /* set the header for every node in this scc */
765 for (irn = pscc->head; irn; irn = next) {
766 node_entry *e = get_irn_ne(irn, env);
769 DB((dbg, LEVEL_2, " %+F,", irn));
771 DB((dbg, LEVEL_2, "\n"));
775 for (irn = pscc->head; irn; irn = next) {
776 node_entry *e = get_irn_ne(irn, env);
784 * Process a SCC for the operator strength reduction.
786 * @param pscc the SCC
787 * @param env the environment
789 static void process_scc(scc *pscc, iv_env *env) {
790 ir_node *head = pscc->head;
791 node_entry *e = get_irn_link(head);
797 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
798 for (irn = pscc->head; irn; irn = next) {
799 node_entry *e = get_irn_link(irn);
803 DB((dbg, LEVEL_4, " %+F,", irn));
805 DB((dbg, LEVEL_4, "\n"));
809 if (e->next == NULL) {
810 /* this SCC has only a single member */
811 check_replace(head, env);
813 classify_iv(pscc, env);
818 * If an SCC is a Phi only cycle, remove it.
820 static void remove_phi_cycle(scc *pscc, iv_env *env) {
825 /* check if this scc contains only Phi, Add or Sub nodes */
827 for (irn = pscc->head; irn; irn = next) {
828 node_entry *e = get_irn_ne(irn, env);
834 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
835 ir_node *pred = get_irn_n(irn, j);
836 node_entry *pe = get_irn_ne(pred, env);
838 if (pe->pscc != e->pscc) {
839 /* not in the same SCC, must be the only input */
842 } else if (out_rc != pred) {
848 /* found a Phi cycle */
849 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
851 for (irn = pscc->head; irn; irn = next) {
852 node_entry *e = get_irn_ne(irn, env);
855 exchange(irn, out_rc);
861 * Process a SCC for the Phi cycle removement.
863 * @param pscc the SCC
864 * @param env the environment
866 static void process_phi_only_scc(scc *pscc, iv_env *env) {
867 ir_node *head = pscc->head;
868 node_entry *e = get_irn_link(head);
874 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
875 for (irn = pscc->head; irn; irn = next) {
876 node_entry *e = get_irn_link(irn);
880 DB((dbg, LEVEL_4, " %+F,", irn));
882 DB((dbg, LEVEL_4, "\n"));
887 remove_phi_cycle(pscc, env);
892 * Push a node onto the stack.
894 * @param env the environment
895 * @param n the node to push
897 static void push(iv_env *env, ir_node *n) {
900 if (env->tos == ARR_LEN(env->stack)) {
901 int nlen = ARR_LEN(env->stack) * 2;
902 ARR_RESIZE(ir_node *, env->stack, nlen);
904 env->stack[env->tos++] = n;
905 e = get_irn_ne(n, env);
910 * pop a node from the stack
912 * @param env the environment
914 * @return The topmost node
916 static ir_node *pop(iv_env *env)
918 ir_node *n = env->stack[--env->tos];
919 node_entry *e = get_irn_ne(n, env);
926 * Do Tarjan's SCC algorithm and drive OSR.
928 * @param irn start at this node
929 * @param env the environment
931 static void dfs(ir_node *irn, iv_env *env)
934 node_entry *node = get_irn_ne(irn, env);
936 mark_irn_visited(irn);
938 /* do not put blocks into the scc */
940 n = get_irn_arity(irn);
941 for (i = 0; i < n; ++i) {
942 ir_node *pred = get_irn_n(irn, i);
944 if (irn_not_visited(pred))
949 ir_node *block = get_nodes_block(irn);
951 node->DFSnum = env->nextDFSnum++;
952 node->low = node->DFSnum;
955 /* handle the block */
956 if (irn_not_visited(block))
959 n = get_irn_arity(irn);
960 for (i = 0; i < n; ++i) {
961 ir_node *pred = get_irn_n(irn, i);
962 node_entry *o = get_irn_ne(pred, env);
964 if (irn_not_visited(pred)) {
966 node->low = MIN(node->low, o->low);
968 if (o->DFSnum < node->DFSnum && o->in_stack)
969 node->low = MIN(o->DFSnum, node->low);
971 if (node->low == node->DFSnum) {
972 scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
975 memset(pscc, 0, sizeof(*pscc));
980 e = get_irn_ne(x, env);
982 e->next = pscc->head;
986 env->process_scc(pscc, env);
992 * Do the DFS by starting at the End node of a graph.
994 * @param irg the graph to process
995 * @param env the environment
997 static void do_dfs(ir_graph *irg, iv_env *env) {
998 ir_graph *rem = current_ir_graph;
999 ir_node *end = get_irg_end(irg);
1002 set_using_irn_visited(irg);
1004 current_ir_graph = irg;
1005 inc_irg_visited(irg);
1007 /* visit all visible nodes */
1010 /* visit the keep-alives */
1011 n = get_End_n_keepalives(end);
1012 for (i = 0; i < n; ++i) {
1013 ir_node *ka = get_End_keepalive(end, i);
1015 if (irn_not_visited(ka))
1019 clear_using_irn_visited(irg);
1021 current_ir_graph = rem;
1025 * Post-block-walker: assign the post-order number.
1027 static void assign_po(ir_node *block, void *ctx) {
1029 node_entry *e = get_irn_ne(block, env);
1031 e->POnum = env->POnum++;
1035 * Follows the LFTR edges and return the last node in the chain.
1037 * @param irn the node that should be followed
1038 * @param env the IV environment
1041 * In the current implementation only the last edge is stored, so
1042 * only one chain exists. That's why we might miss some opportunities.
1044 static ir_node *followEdges(ir_node *irn, iv_env *env) {
1046 LFTR_edge *e = LFTR_find(irn, env);
1055 * Apply one LFTR edge operation.
1056 * Return NULL if the transformation cannot be done safely without
1059 * @param iv the induction variable
1060 * @param rc the constant that should be translated
1061 * @param e the LFTR edge
1062 * @param env the IV environment
1064 * @return the translated region constant or NULL
1065 * if the translation was not possible
1068 * In the current implementation only the last edge is stored, so
1069 * only one chain exists. That's why we might miss some opportunities.
1071 static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) {
1072 if (env->flags & osr_flag_lftr_with_ov_check) {
1073 tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr;
1074 tarval_int_overflow_mode_t ovmode;
1077 if (! is_counter_iv(iv, env)) {
1078 DB((dbg, LEVEL_4, " not counter IV"));
1082 /* overflow can only be decided for Consts */
1083 if (! is_Const(e->rc)) {
1084 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
1088 tv_l = get_Const_tarval(rc);
1089 tv_r = get_Const_tarval(e->rc);
1091 ovmode = tarval_get_integer_overflow_mode();
1092 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1094 pscc = get_iv_scc(iv, env);
1095 tv_incr = pscc->incr;
1096 tv_init = pscc->init;
1099 * Check that no overflow occurs:
1100 * init must be transformed without overflow
1101 * the new rc must be transformed without overflow
1102 * rc +/- incr must be possible without overflow
1106 tv = tarval_mul(tv_l, tv_r);
1107 tv_init = tarval_mul(tv_init, tv_r);
1108 tv_incr = tarval_mul(tv_incr, tv_r);
1109 DB((dbg, LEVEL_4, " * %+F", tv_r));
1112 tv = tarval_add(tv_l, tv_r);
1113 tv_init = tarval_add(tv_init, tv_r);
1114 DB((dbg, LEVEL_4, " + %+F", tv_r));
1117 tv = tarval_sub(tv_l, tv_r);
1118 tv_init = tarval_sub(tv_init, tv_r);
1119 DB((dbg, LEVEL_4, " - %+F", tv_r));
1126 if (pscc->code == iro_Add) {
1127 tv = tarval_add(tv, tv_incr);
1129 assert(pscc->code == iro_Sub);
1130 tv = tarval_sub(tv, tv_incr);
1133 tarval_set_integer_overflow_mode(ovmode);
1135 if (tv == tarval_bad || tv_init == tarval_bad) {
1136 DB((dbg, LEVEL_4, " = OVERFLOW"));
1139 return new_r_Const(current_ir_graph, get_irn_n(rc, -1), get_tarval_mode(tv), tv);
1141 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
1145 * Applies the operations represented by the LFTR edges to a
1146 * region constant and returns the value.
1147 * Return NULL if the transformation cannot be done safely without
1150 * @param iv the IV node that starts the LFTR edge chain
1151 * @param rc the region constant that should be translated
1152 * @param env the IV environment
1154 * @return the translated region constant or NULL
1155 * if the translation was not possible
1157 static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
1158 if (env->flags & osr_flag_lftr_with_ov_check) {
1159 /* overflow can only be decided for Consts */
1160 if (! is_counter_iv(iv, env)) {
1161 DB((dbg, LEVEL_4, "not counter IV\n", rc));
1164 if (! is_Const(rc)) {
1165 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
1168 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
1172 LFTR_edge *e = LFTR_find(iv, env);
1174 rc = applyOneEdge(iv, rc, e, env);
1180 DB((dbg, LEVEL_3, "\n"));
1185 * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
1186 * and tries to optimize them.
1188 static void do_lftr(ir_node *cmp, void *ctx) {
1190 ir_node *left, *right, *liv, *riv;
1192 ir_node *nleft = NULL, *nright = NULL;
1194 if (get_irn_op(cmp) != op_Cmp)
1197 left = get_Cmp_left(cmp);
1198 right = get_Cmp_right(cmp);
1200 liv = is_iv(left, env);
1201 riv = is_iv(right, env);
1202 if (liv && is_rc(right, liv)) {
1203 iv = left; rc = right;
1205 nright = applyEdges(iv, rc, env);
1206 if (nright && nright != rc) {
1207 nleft = followEdges(iv, env);
1210 else if (riv && is_rc(left, riv)) {
1211 iv = right; rc = left;
1213 nleft = applyEdges(iv, rc, env);
1214 if (nleft && nleft != rc) {
1215 nright = followEdges(iv, env);
1219 if (nleft && nright) {
1220 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
1221 set_Cmp_left(cmp, nleft);
1222 set_Cmp_right(cmp, nright);
1223 ++env->lftr_replaced;
1228 * do linear function test replacement.
1230 * @param irg the graph that should be optimized
1231 * @param env the IV environment
1233 static void lftr(ir_graph *irg, iv_env *env) {
1234 irg_walk_graph(irg, NULL, do_lftr, env);
1238 * Pre-walker: set all node links to NULL and fix the
1239 * block of Proj nodes.
1241 static void clear_and_fix(ir_node *irn, void *env)
1244 set_irn_link(irn, NULL);
1247 ir_node *pred = get_Proj_pred(irn);
1248 set_nodes_block(irn, get_nodes_block(pred));
1252 /* Performs Operator Strength Reduction for the passed graph. */
1253 void opt_osr(ir_graph *irg, unsigned flags) {
1258 if (! get_opt_strength_red()) {
1259 /* only kill Phi cycles */
1260 remove_phi_cycles(irg);
1264 rem = current_ir_graph;
1265 current_ir_graph = irg;
1267 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
1269 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
1271 obstack_init(&env.obst);
1272 env.stack = NEW_ARR_F(ir_node *, 128);
1276 env.quad_map = new_set(quad_cmp, 64);
1277 env.lftr_edges = new_set(LFTR_cmp, 64);
1279 env.lftr_replaced = 0;
1281 env.process_scc = process_scc;
1283 /* Clear all links and move Proj nodes into the
1284 the same block as it's predecessors.
1285 This can improve the placement of new nodes.
1287 irg_walk_graph(irg, NULL, clear_and_fix, NULL);
1289 /* we need dominance */
1292 edges = edges_assure(irg);
1294 /* calculate the post order number for blocks. */
1295 irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1297 /* calculate the SCC's and drive OSR. */
1301 /* try linear function test replacements */
1304 set_irg_outs_inconsistent(irg);
1305 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
1308 del_set(env.lftr_edges);
1309 del_set(env.quad_map);
1310 DEL_ARR_F(env.stack);
1311 obstack_free(&env.obst, NULL);
1314 edges_deactivate(irg);
1316 current_ir_graph = rem;
1319 /* Remove any Phi cycles with only one real input. */
1320 void remove_phi_cycles(ir_graph *irg) {
1324 rem = current_ir_graph;
1325 current_ir_graph = irg;
1327 FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
1329 DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));
1331 obstack_init(&env.obst);
1332 env.stack = NEW_ARR_F(ir_node *, 128);
1336 env.quad_map = NULL;
1337 env.lftr_edges = NULL;
1339 env.lftr_replaced = 0;
1341 env.process_scc = process_phi_only_scc;
1343 /* Clear all links and move Proj nodes into the
1344 the same block as it's predecessors.
1345 This can improve the placement of new nodes.
1347 irg_walk_graph(irg, NULL, clear_and_fix, NULL);
1349 /* we need outs for calculating the post order */
1350 assure_irg_outs(irg);
1352 /* calculate the post order number for blocks. */
1353 irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1355 /* calculate the SCC's and drive OSR. */
1359 set_irg_outs_inconsistent(irg);
1360 DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n", env.replaced));
1363 DEL_ARR_F(env.stack);
1364 obstack_free(&env.obst, NULL);
1366 current_ir_graph = rem;