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.
34 #include "iroptimize.h"
55 /** The debug handle. */
56 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
60 ir_node *head; /**< the head of the list */
61 tarval *init; /**< the init value iff only one exists. */
62 tarval *incr; /**< the induction variable increment if only a single const exists. */
63 unsigned code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
67 typedef struct node_entry {
68 unsigned DFSnum; /**< the DFS number of this node */
69 unsigned low; /**< the low number of this node */
70 ir_node *header; /**< the header of this node */
71 int in_stack; /**< flag, set if the node is on the stack */
72 ir_node *next; /**< link to the next node the the same scc */
73 scc *pscc; /**< the scc of this node */
74 unsigned POnum; /**< the post order number for blocks */
77 /** The environment. */
78 typedef struct iv_env {
79 struct obstack obst; /**< an obstack for allocations */
80 ir_node **stack; /**< the node stack */
81 int tos; /**< tos index */
82 unsigned nextDFSnum; /**< the current DFS number */
83 unsigned POnum; /**< current post order number */
84 set *quad_map; /**< a map from (op, iv, rc) to node */
85 set *lftr_edges; /**< the set of lftr edges */
86 unsigned replaced; /**< number of replaced ops */
87 unsigned lftr_replaced; /**< number of applied linear function test replacements */
88 unsigned osr_flags; /**< additional flags steering the transformation */
89 unsigned need_postpass; /**< set, if a post pass is needed to fix Add and Sub nodes */
90 /** Function called to process a SCC. */
91 void (*process_scc)(scc *pscc, struct iv_env *env);
95 * An entry in the (op, node, node) -> node map.
97 typedef struct quadruple_t {
98 ir_opcode code; /**< the opcode of the reduced operation */
99 ir_node *op1; /**< the first operand the reduced operation */
100 ir_node *op2; /**< the second operand of the reduced operation */
102 ir_node *res; /**< the reduced operation */
108 typedef struct LFTR_edge {
109 ir_node *src; /**< the source node */
110 ir_node *dst; /**< the destination node */
111 ir_opcode code; /**< the opcode that must be applied */
112 ir_node *rc; /**< the region const that must be applied */
116 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
119 * Compare two LFTR edges.
121 static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
122 const LFTR_edge *l1 = e1;
123 const LFTR_edge *l2 = e2;
126 return l1->src != l2->src;
132 * @param src the source node of the transition
134 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
139 return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
145 * @param src the source node of the edge
146 * @param dst the destination node of the edge
147 * @param code the opcode of the transformed transition
148 * @param rc the region const used in the transition
149 * @param env the environment
151 static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv_env *env) {
160 * There might be more than one edge here. This is rather bad
161 * because we currently store only one.
163 // assert(LFTR_find(src, env) == NULL);
164 set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
168 * Gets the node_entry of a node.
170 * @param irn the node
171 * @param env the environment
173 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
174 node_entry *e = get_irn_link(irn);
177 e = obstack_alloc(&env->obst, sizeof(*e));
178 memset(e, 0, sizeof(*e));
179 set_irn_link(irn, e);
185 * Gets the scc from an induction variable.
187 * @param iv any node of the induction variable
188 * @param env the environment
190 static scc *get_iv_scc(ir_node *iv, iv_env *env) {
191 node_entry *e = get_irn_ne(iv, env);
196 * Check if irn is an IV.
198 * @param irn the node to check
199 * @param env the environment
201 * @returns the header if it is one, NULL else
203 static ir_node *is_iv(ir_node *irn, iv_env *env) {
204 return get_irn_ne(irn, env)->header;
208 * Check if irn is a region constant.
209 * The block or irn must strictly dominate the header block.
211 * @param irn the node to check
212 * @param header_block the header block of the induction variable
214 static int is_rc(ir_node *irn, ir_node *header_block) {
215 ir_node *block = get_nodes_block(irn);
217 return (block != header_block) && block_dominates(block, header_block);
221 * Set compare function for the quad set.
223 static int quad_cmp(const void *e1, const void *e2, size_t size) {
224 const quadruple_t *c1 = e1;
225 const quadruple_t *c2 = e2;
228 return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
232 * Check if an reduced operation was already calculated.
234 * @param code the opcode of the operation
235 * @param op1 the first operand of the operation
236 * @param op2 the second operand of the operation
237 * @param env the environment
239 * @return the already reduced node or NULL if this operation is not yet reduced
241 static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
242 quadruple_t key, *entry;
248 entry = set_find(env->quad_map, &key, sizeof(key),
249 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
256 * Add an reduced operation.
258 * @param code the opcode of the operation
259 * @param op1 the first operand of the operation
260 * @param op2 the second operand of the operation
261 * @param result the result of the reduced operation
262 * @param env the environment
264 static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
272 set_insert(env->quad_map, &key, sizeof(key),
273 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
277 * Find a location where to place a bin-op whose operands are in
280 * @param block1 the block of the first operand
281 * @param block2 the block of the second operand
283 * Note that we know here that such a place must exists. Moreover, this means
284 * that either block1 dominates block2 or vice versa. So, just return
287 static ir_node *find_location(ir_node *block1, ir_node *block2) {
288 if (block_dominates(block1, block2))
290 assert(block_dominates(block2, block1));
292 } /* find_location */
295 * Create a node that executes an op1 code op1 operation.
297 * @param code the opcode to execute
298 * @param db debug info to add to the new node
299 * @param op1 the first operand
300 * @param op2 the second operand
301 * @param mode the mode of the new operation
303 * @return the newly created node
305 static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
306 ir_graph *irg = current_ir_graph;
308 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
312 result = new_rd_Mul(db, irg, block, op1, op2, mode);
315 result = new_rd_Add(db, irg, block, op1, op2, mode);
318 result = new_rd_Sub(db, irg, block, op1, op2, mode);
321 panic("Unsupported opcode");
328 * The Apply operation.
330 * @param orig the node that represent the original operation and determines
331 * the opcode, debug-info and mode of a newly created one
332 * @param op1 the first operand
333 * @param op2 the second operand
334 * @param env the environment
336 * @return the newly created node
338 static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
339 ir_opcode code = get_irn_opcode(orig);
340 ir_node *result = search(code, op1, op2, env);
342 if (result == NULL) {
343 dbg_info *db = get_irn_dbg_info(orig);
344 ir_node *op1_header = get_irn_ne(op1, env)->header;
345 ir_node *op2_header = get_irn_ne(op2, env)->header;
347 if (op1_header == header && is_rc(op2, op1_header)) {
348 result = reduce(orig, op1, op2, env);
350 else if (op2_header == header && is_rc(op1, op2_header)) {
351 result = reduce(orig, op2, op1, env);
354 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
355 get_irn_ne(result, env)->header = NULL;
362 * The Reduce operation.
364 * @param orig the node that represent the original operation and determines
365 * the opcode, debug-info and mode of a newly created one
366 * @param iv the induction variable
367 * @param rc the region constant
368 * @param env the environment
370 * @return the reduced node
372 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
373 ir_opcode code = get_irn_opcode(orig);
374 ir_node *result = search(code, iv, rc, env);
376 /* check if we have already done this operation on the iv */
377 if (result == NULL) {
378 node_entry *e, *iv_e;
380 ir_mode *mode = get_irn_mode(orig);
382 result = exact_copy(iv);
384 if (get_irn_mode(result) != mode) {
386 * Beware: we must always create a new induction variable with the same mode
387 * as the node we are replacing. Especially this means the mode might be changed
388 * from P to I and back. This is always possible, because we have only Phi, Add
390 * However, this might lead to AddIs(Iu,Is) which we must fix. The best way to do this
391 * seems to be a post-pass, or we might end with useless Conv's.
393 set_irn_mode(result, mode);
394 env->need_postpass = 1;
396 add(code, iv, rc, result, env);
397 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
398 get_irn_opname(orig), rc));
400 iv_e = get_irn_ne(iv, env);
401 e = get_irn_ne(result, env);
402 e->header = iv_e->header;
404 /* create the LFTR edge */
405 LFTR_add(iv, result, code, rc, env);
407 for (i = get_irn_arity(result) - 1; i >= 0; --i) {
408 ir_node *o = get_irn_n(result, i);
410 e = get_irn_ne(o, env);
411 if (e->header == iv_e->header)
412 o = reduce(orig, o, rc, env);
413 else if (is_Phi(result) || code == iro_Mul)
414 o = apply(iv_e->header, orig, o, rc, env);
415 set_irn_n(result, i, o);
418 DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
419 get_irn_opname(orig), rc));
425 * Update the scc for a newly created IV.
427 static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
429 ir_node *header = e->header;
430 waitq *wq = new_waitq();
432 DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n "));
436 ir_node *irn = waitq_get(wq);
437 node_entry *ne = get_irn_ne(irn, env);
441 ne->next = pscc->head;
443 DB((dbg, LEVEL_2, " %+F,", irn));
445 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
446 ir_node *pred = get_irn_n(irn, i);
447 node_entry *pe = get_irn_ne(pred, env);
449 if (pe->header == header && pe->pscc == NULL) {
450 /* set the pscc here to ensure that the node is NOT enqueued another time */
455 } while (! waitq_empty(wq));
457 DB((dbg, LEVEL_2, "\n"));
461 * The Replace operation. We found a node representing iv (+,-,*) rc
462 * that can be removed by replacing the induction variable iv by a new
463 * one that 'applies' the operation 'irn'.
465 * @param irn the node that will be replaced
466 * @param iv the induction variable
467 * @param rc the region constant
468 * @param env the environment
470 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
473 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
475 result = reduce(irn, iv, rc, env);
479 hook_strength_red(current_ir_graph, irn);
480 exchange(irn, result);
481 e = get_irn_ne(result, env);
482 if (e->pscc == NULL) {
483 e->pscc = obstack_alloc(&env->obst, sizeof(*e->pscc));
484 memset(e->pscc, 0, sizeof(*e->pscc));
485 update_scc(result, e, env);
495 * check if a given node is a mul with 2, 4, 8
497 static int is_x86_shift_const(ir_node *mul) {
503 /* normalization put constants on the right side */
504 rc = get_Mul_right(mul);
506 tarval *tv = get_Const_tarval(rc);
508 if (tarval_is_long(tv)) {
509 long value = get_tarval_long(tv);
511 if (value == 2 || value == 4 || value == 8) {
512 /* do not reduce multiplications by 2, 4, 8 */
518 } /* is_x86_shift_const */
522 * Check if an IV represents a counter with constant limits.
524 * @param iv any node of the induction variable
525 * @param env the environment
527 static int is_counter_iv(ir_node *iv, iv_env *env) {
528 node_entry *e = get_irn_ne(iv, env);
530 ir_node *have_init = NULL;
531 ir_node *have_incr = NULL;
532 ir_opcode code = iro_Bad;
535 if (pscc->code != 0) {
536 /* already analysed */
537 return pscc->code != iro_Bad;
540 pscc->code = iro_Bad;
541 for (irn = pscc->head; irn != NULL; irn = e->next) {
543 if (have_incr != NULL)
546 have_incr = get_Add_right(irn);
547 if (! is_Const(have_incr)) {
548 have_incr = get_Add_left(irn);
549 if (! is_Const(have_incr))
553 } else if (is_Sub(irn)) {
554 if (have_incr != NULL)
557 have_incr = get_Sub_right(irn);
558 if (! is_Const(have_incr))
561 } else if (is_Phi(irn)) {
564 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
565 ir_node *pred = get_Phi_pred(irn, i);
566 node_entry *ne = get_irn_ne(pred, env);
568 if (ne->header == e->header)
570 if (have_init != NULL)
573 if (! is_Const(pred))
578 e = get_irn_ne(irn, env);
580 pscc->init = get_Const_tarval(have_init);
581 pscc->incr = get_Const_tarval(have_incr);
583 return code != iro_Bad;
584 } /* is_counter_iv */
587 * Check the users of an induction variable for register pressure.
589 * @param iv any node of the induction variable
590 * @param env the environment
592 * @return non-zero if the register pressure is estimated
593 * to not increase, zero else
595 static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
596 ir_node *irn, *header;
597 ir_node *have_user = NULL;
598 ir_node *have_cmp = NULL;
599 node_entry *e = get_irn_ne(iv, env);
603 for (irn = pscc->head; irn != NULL; irn = e->next) {
604 const ir_edge_t *edge;
606 foreach_out_edge(irn, edge) {
607 ir_node *user = get_edge_src_irn(edge);
608 node_entry *ne = get_irn_ne(user, env);
610 if (e->header == ne->header) {
611 /* found user from the same IV */
615 if (have_cmp != NULL) {
616 /* more than one cmp, for now end here */
621 /* user is a real user of the IV */
622 if (have_user != NULL) {
623 /* found the second user */
629 e = get_irn_ne(irn, env);
632 if (have_user == NULL) {
633 /* no user, ignore */
637 if (have_cmp == NULL) {
638 /* fine, only one user, try to reduce */
642 * We found one user AND at least one cmp.
643 * We should check here if we can transform the Cmp.
645 * For now our capabilities for doing linear function test
646 * are limited, so check if the iv has the right form: Only ONE
647 * phi, only one Add/Sub with a Const
649 if (! is_counter_iv(iv, env))
653 * Ok, we have only one increment AND it is a Const, we might be able
654 * to do a linear function test replacement, so go on.
657 } /* check_users_for_reg_pressure */
660 * Check if a node can be replaced (+, -, *).
662 * @param irn the node to check
663 * @param env the environment
665 * @return non-zero if irn should be Replace'd
667 static int check_replace(ir_node *irn, iv_env *env) {
668 ir_node *left, *right, *iv, *rc;
669 ir_op *op = get_irn_op(irn);
670 ir_opcode code = get_op_code(op);
679 left = get_binop_left(irn);
680 right = get_binop_right(irn);
682 liv = is_iv(left, env);
683 riv = is_iv(right, env);
684 if (liv && is_rc(right, liv)) {
685 iv = left; rc = right;
687 else if (riv && is_op_commutative(op) &&
689 iv = right; rc = left;
693 if (env->osr_flags & osr_flag_keep_reg_pressure) {
694 if (! check_users_for_reg_pressure(iv, env))
697 return replace(irn, iv, rc, env);
704 } /* check_replace */
707 * Check which SCC's are induction variables.
710 * @param env the environment
712 static void classify_iv(scc *pscc, iv_env *env) {
713 ir_node *irn, *next, *header = NULL;
714 node_entry *b, *h = NULL;
715 int j, only_phi, num_outside;
718 /* find the header block for this scc */
719 for (irn = pscc->head; irn; irn = next) {
720 node_entry *e = get_irn_link(irn);
721 ir_node *block = get_nodes_block(irn);
724 b = get_irn_ne(block, env);
727 if (h->POnum < b->POnum) {
738 /* check if this scc contains only Phi, Add or Sub nodes */
742 for (irn = pscc->head; irn; irn = next) {
743 node_entry *e = get_irn_ne(irn, env);
746 switch (get_irn_opcode(irn)) {
752 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
753 ir_node *pred = get_irn_n(irn, j);
754 node_entry *pe = get_irn_ne(pred, env);
756 if (pe->pscc != e->pscc) {
757 /* not in the same SCC, must be a region const */
758 if (! is_rc(pred, header)) {
759 /* not an induction variable */
765 } else if (out_rc != pred) {
772 /* not an induction variable */
776 /* found an induction variable */
777 DB((dbg, LEVEL_2, " Found an induction variable:\n "));
778 if (only_phi && num_outside == 1) {
779 /* a phi cycle with only one real predecessor can be collapsed */
780 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
782 for (irn = pscc->head; irn; irn = next) {
783 node_entry *e = get_irn_ne(irn, env);
786 exchange(irn, out_rc);
792 /* set the header for every node in this scc */
793 for (irn = pscc->head; irn; irn = next) {
794 node_entry *e = get_irn_ne(irn, env);
797 DB((dbg, LEVEL_2, " %+F,", irn));
799 DB((dbg, LEVEL_2, "\n"));
803 for (irn = pscc->head; irn; irn = next) {
804 node_entry *e = get_irn_ne(irn, env);
812 * Process an SCC for the operator strength reduction.
814 * @param pscc the SCC
815 * @param env the environment
817 static void process_scc(scc *pscc, iv_env *env) {
818 ir_node *head = pscc->head;
819 node_entry *e = get_irn_link(head);
825 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
826 for (irn = pscc->head; irn != NULL; irn = next) {
827 node_entry *e = get_irn_link(irn);
831 DB((dbg, LEVEL_4, " %+F,", irn));
833 DB((dbg, LEVEL_4, "\n"));
837 if (e->next == NULL) {
838 /* this SCC has only a single member */
839 check_replace(head, env);
841 classify_iv(pscc, env);
846 * If an SCC is a Phi only cycle, remove it.
848 * @param pscc an SCC that consists of Phi nodes only
849 * @param env the environment
851 static void remove_phi_cycle(scc *pscc, iv_env *env) {
856 /* check if this scc contains only Phi, Add or Sub nodes */
858 for (irn = pscc->head; irn; irn = next) {
859 node_entry *e = get_irn_ne(irn, env);
865 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
866 ir_node *pred = get_irn_n(irn, j);
867 node_entry *pe = get_irn_ne(pred, env);
869 if (pe->pscc != e->pscc) {
870 /* not in the same SCC, must be the only input */
873 } else if (out_rc != pred) {
879 /* found a Phi cycle */
880 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
882 for (irn = pscc->head; irn; irn = next) {
883 node_entry *e = get_irn_ne(irn, env);
886 exchange(irn, out_rc);
889 } /* remove_phi_cycle */
892 * Process a SCC for the Phi cycle remove.
894 * @param pscc the SCC
895 * @param env the environment
897 static void process_phi_only_scc(scc *pscc, iv_env *env) {
898 ir_node *head = pscc->head;
899 node_entry *e = get_irn_link(head);
905 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
906 for (irn = pscc->head; irn; irn = next) {
907 node_entry *e = get_irn_link(irn);
911 DB((dbg, LEVEL_4, " %+F,", irn));
913 DB((dbg, LEVEL_4, "\n"));
918 remove_phi_cycle(pscc, env);
919 } /* process_phi_only_scc */
923 * Push a node onto the stack.
925 * @param env the environment
926 * @param n the node to push
928 static void push(iv_env *env, ir_node *n) {
931 if (env->tos == ARR_LEN(env->stack)) {
932 int nlen = ARR_LEN(env->stack) * 2;
933 ARR_RESIZE(ir_node *, env->stack, nlen);
935 env->stack[env->tos++] = n;
936 e = get_irn_ne(n, env);
941 * Pop a node from the stack.
943 * @param env the environment
945 * @return The topmost node
947 static ir_node *pop(iv_env *env) {
948 ir_node *n = env->stack[--env->tos];
949 node_entry *e = get_irn_ne(n, env);
956 * Do Tarjan's SCC algorithm and drive OSR.
958 * @param irn start at this node
959 * @param env the environment
961 static void dfs(ir_node *irn, iv_env *env) {
963 node_entry *node = get_irn_ne(irn, env);
965 mark_irn_visited(irn);
967 /* do not put blocks into the scc */
969 n = get_irn_arity(irn);
970 for (i = 0; i < n; ++i) {
971 ir_node *pred = get_irn_n(irn, i);
973 if (!irn_visited(pred))
977 ir_node *block = get_nodes_block(irn);
979 node->DFSnum = env->nextDFSnum++;
980 node->low = node->DFSnum;
983 /* handle the block */
984 if (!irn_visited(block))
987 n = get_irn_arity(irn);
988 for (i = 0; i < n; ++i) {
989 ir_node *pred = get_irn_n(irn, i);
990 node_entry *o = get_irn_ne(pred, env);
992 if (!irn_visited(pred)) {
994 node->low = MIN(node->low, o->low);
996 if (o->DFSnum < node->DFSnum && o->in_stack)
997 node->low = MIN(o->DFSnum, node->low);
999 if (node->low == node->DFSnum) {
1000 scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
1003 memset(pscc, 0, sizeof(*pscc));
1008 e = get_irn_ne(x, env);
1010 e->next = pscc->head;
1014 env->process_scc(pscc, env);
1020 * Do the DFS by starting at the End node of a graph.
1022 * @param irg the graph to process
1023 * @param env the environment
1025 static void do_dfs(ir_graph *irg, iv_env *env) {
1026 ir_graph *rem = current_ir_graph;
1027 ir_node *end = get_irg_end(irg);
1030 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1032 current_ir_graph = irg;
1033 inc_irg_visited(irg);
1035 /* visit all visible nodes */
1038 /* visit the keep-alives */
1039 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
1040 ir_node *ka = get_End_keepalive(end, i);
1042 if (!irn_visited(ka))
1046 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1048 current_ir_graph = rem;
1052 * Post-block-walker: assign the post-order number.
1054 static void assign_po(ir_node *block, void *ctx) {
1056 node_entry *e = get_irn_ne(block, env);
1058 e->POnum = env->POnum++;
1062 * Follows the LFTR edges and return the last node in the chain.
1064 * @param irn the node that should be followed
1065 * @param env the IV environment
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 *followEdges(ir_node *irn, iv_env *env) {
1073 LFTR_edge *e = LFTR_find(irn, env);
1082 * Apply one LFTR edge operation.
1083 * Return NULL if the transformation cannot be done safely without
1086 * @param iv the induction variable
1087 * @param rc the constant that should be translated
1088 * @param e the LFTR edge
1089 * @param env the IV environment
1091 * @return the translated region constant or NULL
1092 * if the translation was not possible
1095 * In the current implementation only the last edge is stored, so
1096 * only one chain exists. That's why we might miss some opportunities.
1098 static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) {
1099 if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1100 tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr;
1101 tarval_int_overflow_mode_t ovmode;
1104 if (! is_counter_iv(iv, env)) {
1105 DB((dbg, LEVEL_4, " not counter IV"));
1109 /* overflow can only be decided for Consts */
1110 if (! is_Const(e->rc)) {
1111 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
1115 tv_l = get_Const_tarval(rc);
1116 tv_r = get_Const_tarval(e->rc);
1118 ovmode = tarval_get_integer_overflow_mode();
1119 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1121 pscc = get_iv_scc(iv, env);
1122 tv_incr = pscc->incr;
1123 tv_init = pscc->init;
1126 * Check that no overflow occurs:
1127 * init must be transformed without overflow
1128 * the new rc must be transformed without overflow
1129 * rc +/- incr must be possible without overflow
1133 tv = tarval_mul(tv_l, tv_r);
1134 tv_init = tarval_mul(tv_init, tv_r);
1135 tv_incr = tarval_mul(tv_incr, tv_r);
1136 DB((dbg, LEVEL_4, " * %+F", tv_r));
1139 tv = tarval_add(tv_l, tv_r);
1140 tv_init = tarval_add(tv_init, tv_r);
1141 DB((dbg, LEVEL_4, " + %+F", tv_r));
1144 tv = tarval_sub(tv_l, tv_r, NULL);
1145 tv_init = tarval_sub(tv_init, tv_r, NULL);
1146 DB((dbg, LEVEL_4, " - %+F", tv_r));
1149 panic("Unsupported opcode");
1153 if (pscc->code == iro_Add) {
1154 tv = tarval_add(tv, tv_incr);
1156 assert(pscc->code == iro_Sub);
1157 tv = tarval_sub(tv, tv_incr, NULL);
1160 tarval_set_integer_overflow_mode(ovmode);
1162 if (tv == tarval_bad || tv_init == tarval_bad) {
1163 DB((dbg, LEVEL_4, " = OVERFLOW"));
1166 return new_Const(tv);
1168 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
1169 } /* applyOneEdge */
1172 * Applies the operations represented by the LFTR edges to a
1173 * region constant and returns the value.
1174 * Return NULL if the transformation cannot be done safely without
1177 * @param iv the IV node that starts the LFTR edge chain
1178 * @param rc the region constant that should be translated
1179 * @param env the IV environment
1181 * @return the translated region constant or NULL
1182 * if the translation was not possible
1184 static ir_node *applyEdges(ir_node *iv, 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);
1207 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 ir_node *left, *right, *liv, *riv;
1219 ir_node *nleft = NULL, *nright = NULL;
1224 left = get_Cmp_left(cmp);
1225 right = get_Cmp_right(cmp);
1227 liv = is_iv(left, env);
1228 riv = is_iv(right, env);
1229 if (liv && is_rc(right, liv)) {
1230 iv = left; rc = right;
1232 nright = applyEdges(iv, rc, env);
1233 if (nright && nright != rc) {
1234 nleft = followEdges(iv, env);
1237 else if (riv && is_rc(left, riv)) {
1238 iv = right; rc = left;
1240 nleft = applyEdges(iv, rc, env);
1241 if (nleft && nleft != rc) {
1242 nright = followEdges(iv, env);
1246 if (nleft && nright) {
1247 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
1248 set_Cmp_left(cmp, nleft);
1249 set_Cmp_right(cmp, nright);
1250 ++env->lftr_replaced;
1255 * do linear function test replacement.
1257 * @param irg the graph that should be optimized
1258 * @param env the IV environment
1260 static void lftr(ir_graph *irg, iv_env *env) {
1261 irg_walk_graph(irg, NULL, do_lftr, env);
1265 * Pre-walker: set all node links to NULL and fix the
1266 * block of Proj nodes.
1268 static void clear_and_fix(ir_node *irn, void *env) {
1270 set_irn_link(irn, NULL);
1273 ir_node *pred = get_Proj_pred(irn);
1274 ir_node *pred_block = get_nodes_block(pred);
1276 if (get_nodes_block(irn) != pred_block) {
1277 set_nodes_block(irn, pred_block);
1281 } /* clear_and_fix */
1284 /* Remove any Phi cycles with only one real input. */
1285 void remove_phi_cycles(ir_graph *irg) {
1290 rem = current_ir_graph;
1291 current_ir_graph = irg;
1293 FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
1295 DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));
1297 obstack_init(&env.obst);
1298 env.stack = NEW_ARR_F(ir_node *, 128);
1302 env.quad_map = NULL;
1303 env.lftr_edges = NULL;
1305 env.lftr_replaced = 0;
1307 env.need_postpass = 0;
1308 env.process_scc = process_phi_only_scc;
1310 /* Clear all links and move Proj nodes into the
1311 the same block as it's predecessors.
1312 This can improve the placement of new nodes.
1315 irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
1317 set_irg_outs_inconsistent(irg);
1319 /* we need outs for calculating the post order */
1320 assure_irg_outs(irg);
1322 /* calculate the post order number for blocks. */
1323 irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1325 /* calculate the SCC's and drive OSR. */
1326 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1328 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1331 set_irg_outs_inconsistent(irg);
1332 DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n", env.replaced));
1335 DEL_ARR_F(env.stack);
1336 obstack_free(&env.obst, NULL);
1338 current_ir_graph = rem;
1339 } /* remove_phi_cycles */
1342 * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
1344 static void fix_adds_and_subs(ir_node *irn, void *ctx) {
1346 ir_mode *mode = get_irn_mode(irn);
1348 if (mode_is_int(mode)) {
1351 pred = get_Add_left(irn);
1352 if (get_irn_mode(pred) != mode) {
1353 ir_node *block = get_nodes_block(pred);
1355 pred = new_r_Conv(current_ir_graph, block, pred, mode);
1356 set_Add_left(irn, pred);
1358 pred = get_Add_right(irn);
1359 if (get_irn_mode(pred) != mode) {
1360 ir_node *block = get_nodes_block(pred);
1362 pred = new_r_Conv(current_ir_graph, block, pred, mode);
1363 set_Add_right(irn, pred);
1366 } else if (is_Sub(irn)) {
1367 ir_mode *mode = get_irn_mode(irn);
1369 if (mode_is_int(mode)) {
1370 ir_node *left = get_Sub_left(irn);
1371 ir_node *right = get_Sub_right(irn);
1372 ir_mode *l_mode = get_irn_mode(left);
1373 ir_mode *r_mode = get_irn_mode(right);
1375 if (mode_is_int(l_mode) && mode_is_int(r_mode)) {
1376 if (l_mode != mode) {
1377 ir_node *block = get_nodes_block(left);
1379 left = new_r_Conv(current_ir_graph, block, left, mode);
1380 set_Sub_left(irn, left);
1382 if (r_mode != mode) {
1383 ir_node *block = get_nodes_block(right);
1385 right = new_r_Conv(current_ir_graph, block, right, mode);
1386 set_Sub_right(irn, right);
1391 } /* fix_adds_and_subs */
1393 /* Performs Operator Strength Reduction for the passed graph. */
1394 void opt_osr(ir_graph *irg, unsigned flags) {
1400 if (! get_opt_strength_red()) {
1401 /* only kill Phi cycles */
1402 remove_phi_cycles(irg);
1406 rem = current_ir_graph;
1407 current_ir_graph = irg;
1409 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
1410 firm_dbg_set_mask(dbg, SET_LEVEL_4);
1412 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
1414 obstack_init(&env.obst);
1415 env.stack = NEW_ARR_F(ir_node *, 128);
1419 env.quad_map = new_set(quad_cmp, 64);
1420 env.lftr_edges = new_set(LFTR_cmp, 64);
1422 env.lftr_replaced = 0;
1423 env.osr_flags = flags;
1424 env.need_postpass = 0;
1425 env.process_scc = process_scc;
1427 /* Clear all links and move Proj nodes into the
1428 the same block as it's predecessors.
1429 This can improve the placement of new nodes.
1432 irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
1434 set_irg_outs_inconsistent(irg);
1436 /* we need dominance */
1439 edges = edges_assure(irg);
1441 /* calculate the post order number for blocks by walking the out edges. */
1442 assure_irg_outs(irg);
1443 irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1445 /* calculate the SCC's and drive OSR. */
1446 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1450 /* try linear function test replacements */
1451 //lftr(irg, &env); // currently buggy :-(
1454 if (env.need_postpass)
1455 irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);
1457 set_irg_outs_inconsistent(irg);
1458 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
1460 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1462 del_set(env.lftr_edges);
1463 del_set(env.quad_map);
1464 DEL_ARR_F(env.stack);
1465 obstack_free(&env.obst, NULL);
1468 edges_deactivate(irg);
1470 current_ir_graph = rem;