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"
56 /** The debug handle. */
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
61 ir_node *head; /**< the head of the list */
62 tarval *init; /**< the init value iff only one exists. */
63 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 int 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 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 * @param src the source node of the transition
135 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
140 return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
146 * @param src the source node of the edge
147 * @param dst the destination node of the edge
148 * @param code the opcode of the transformed transition
149 * @param rc the region const used in the transition
150 * @param env the environment
152 static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv_env *env) {
161 * There might be more than one edge here. This is rather bad
162 * because we currently store only one.
164 // assert(LFTR_find(src, env) == NULL);
165 set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
169 * Gets the node_entry of a node.
171 * @param irn the node
172 * @param env the environment
174 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
175 node_entry *e = get_irn_link(irn);
178 e = OALLOCZ(&env->obst, node_entry);
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) {
307 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
311 result = new_rd_Mul(db, block, op1, op2, mode);
314 result = new_rd_Add(db, block, op1, op2, mode);
317 result = new_rd_Sub(db, block, op1, op2, mode);
320 panic("Unsupported opcode");
327 * The Apply operation.
329 * @param orig the node that represent the original operation and determines
330 * the opcode, debug-info and mode of a newly created one
331 * @param op1 the first operand
332 * @param op2 the second operand
333 * @param env the environment
335 * @return the newly created node
337 static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
338 ir_opcode code = get_irn_opcode(orig);
339 ir_node *result = search(code, op1, op2, env);
341 if (result == NULL) {
342 dbg_info *db = get_irn_dbg_info(orig);
343 ir_node *op1_header = get_irn_ne(op1, env)->header;
344 ir_node *op2_header = get_irn_ne(op2, env)->header;
346 if (op1_header == header && is_rc(op2, op1_header)) {
347 result = reduce(orig, op1, op2, env);
349 else if (op2_header == header && is_rc(op1, op2_header)) {
350 result = reduce(orig, op2, op1, env);
353 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
354 get_irn_ne(result, env)->header = NULL;
361 * The Reduce operation.
363 * @param orig the node that represent the original operation and determines
364 * the opcode, debug-info and mode of a newly created one
365 * @param iv the induction variable
366 * @param rc the region constant
367 * @param env the environment
369 * @return the reduced node
371 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
372 ir_opcode code = get_irn_opcode(orig);
373 ir_node *result = search(code, iv, rc, env);
375 /* check if we have already done this operation on the iv */
376 if (result == NULL) {
377 node_entry *e, *iv_e;
379 ir_mode *mode = get_irn_mode(orig);
381 result = exact_copy(iv);
383 if (get_irn_mode(result) != mode) {
385 * Beware: we must always create a new induction variable with the same mode
386 * as the node we are replacing. Especially this means the mode might be changed
387 * from P to I and back. This is always possible, because we have only Phi, Add
389 * However, this might lead to AddIs(Iu,Is) which we must fix. The best way to do this
390 * seems to be a post-pass, or we might end with useless Conv's.
392 set_irn_mode(result, mode);
393 env->need_postpass = 1;
395 add(code, iv, rc, result, env);
396 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
397 get_irn_opname(orig), rc));
399 iv_e = get_irn_ne(iv, env);
400 e = get_irn_ne(result, env);
401 e->header = iv_e->header;
403 /* create the LFTR edge */
404 LFTR_add(iv, result, code, rc, env);
406 for (i = get_irn_arity(result) - 1; i >= 0; --i) {
407 ir_node *o = get_irn_n(result, i);
409 e = get_irn_ne(o, env);
410 if (e->header == iv_e->header)
411 o = reduce(orig, o, rc, env);
412 else if (is_Phi(result) || code == iro_Mul)
413 o = apply(iv_e->header, orig, o, rc, env);
414 set_irn_n(result, i, o);
417 DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
418 get_irn_opname(orig), rc));
424 * Update the scc for a newly created IV.
426 static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
428 ir_node *header = e->header;
429 waitq *wq = new_waitq();
431 DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n "));
435 ir_node *irn = waitq_get(wq);
436 node_entry *ne = get_irn_ne(irn, env);
440 ne->next = pscc->head;
442 DB((dbg, LEVEL_2, " %+F,", irn));
444 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
445 ir_node *pred = get_irn_n(irn, i);
446 node_entry *pe = get_irn_ne(pred, env);
448 if (pe->header == header && pe->pscc == NULL) {
449 /* set the pscc here to ensure that the node is NOT enqueued another time */
454 } while (! waitq_empty(wq));
456 DB((dbg, LEVEL_2, "\n"));
460 * The Replace operation. We found a node representing iv (+,-,*) rc
461 * that can be removed by replacing the induction variable iv by a new
462 * one that 'applies' the operation 'irn'.
464 * @param irn the node that will be replaced
465 * @param iv the induction variable
466 * @param rc the region constant
467 * @param env the environment
469 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
472 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
474 result = reduce(irn, iv, rc, env);
478 hook_strength_red(current_ir_graph, irn);
479 exchange(irn, result);
480 e = get_irn_ne(result, env);
481 if (e->pscc == NULL) {
482 e->pscc = OALLOCZ(&env->obst, scc);
483 update_scc(result, e, env);
493 * check if a given node is a mul with 2, 4, 8
495 static int is_x86_shift_const(ir_node *mul) {
501 /* normalization put constants on the right side */
502 rc = get_Mul_right(mul);
504 tarval *tv = get_Const_tarval(rc);
506 if (tarval_is_long(tv)) {
507 long value = get_tarval_long(tv);
509 if (value == 2 || value == 4 || value == 8) {
510 /* do not reduce multiplications by 2, 4, 8 */
516 } /* is_x86_shift_const */
520 * Check if an IV represents a counter with constant limits.
522 * @param iv any node of the induction variable
523 * @param env the environment
525 static int is_counter_iv(ir_node *iv, iv_env *env) {
526 node_entry *e = get_irn_ne(iv, env);
528 ir_node *have_init = NULL;
529 ir_node *have_incr = NULL;
530 ir_opcode code = iro_Bad;
533 if (pscc->code != 0) {
534 /* already analysed */
535 return pscc->code != iro_Bad;
538 pscc->code = iro_Bad;
539 for (irn = pscc->head; irn != NULL; irn = e->next) {
541 if (have_incr != NULL)
544 have_incr = get_Add_right(irn);
545 if (! is_Const(have_incr)) {
546 have_incr = get_Add_left(irn);
547 if (! is_Const(have_incr))
551 } else if (is_Sub(irn)) {
552 if (have_incr != NULL)
555 have_incr = get_Sub_right(irn);
556 if (! is_Const(have_incr))
559 } else if (is_Phi(irn)) {
562 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
563 ir_node *pred = get_Phi_pred(irn, i);
564 node_entry *ne = get_irn_ne(pred, env);
566 if (ne->header == e->header)
568 if (have_init != NULL)
571 if (! is_Const(pred))
576 e = get_irn_ne(irn, env);
578 pscc->init = get_Const_tarval(have_init);
579 pscc->incr = get_Const_tarval(have_incr);
581 return code != iro_Bad;
582 } /* is_counter_iv */
585 * Check the users of an induction variable for register pressure.
587 * @param iv any node of the induction variable
588 * @param env the environment
590 * @return non-zero if the register pressure is estimated
591 * to not increase, zero else
593 static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
594 ir_node *irn, *header;
595 ir_node *have_user = NULL;
596 ir_node *have_cmp = NULL;
597 node_entry *e = get_irn_ne(iv, env);
601 for (irn = pscc->head; irn != NULL; irn = e->next) {
602 const ir_edge_t *edge;
604 foreach_out_edge(irn, edge) {
605 ir_node *user = get_edge_src_irn(edge);
606 node_entry *ne = get_irn_ne(user, env);
608 if (e->header == ne->header) {
609 /* found user from the same IV */
613 if (have_cmp != NULL) {
614 /* more than one cmp, for now end here */
619 /* user is a real user of the IV */
620 if (have_user != NULL) {
621 /* found the second user */
627 e = get_irn_ne(irn, env);
630 if (have_user == NULL) {
631 /* no user, ignore */
635 if (have_cmp == NULL) {
636 /* fine, only one user, try to reduce */
640 * We found one user AND at least one cmp.
641 * We should check here if we can transform the Cmp.
643 * For now our capabilities for doing linear function test
644 * are limited, so check if the iv has the right form: Only ONE
645 * Phi, only one Add/Sub with a Const.
647 if (! is_counter_iv(iv, env))
651 * Ok, we have only one increment AND it is a Const, we might be able
652 * to do a linear function test replacement, so go on.
655 } /* check_users_for_reg_pressure */
658 * Check if a node can be replaced (+, -, *).
660 * @param irn the node to check
661 * @param env the environment
663 * @return non-zero if irn should be Replace'd
665 static int check_replace(ir_node *irn, iv_env *env) {
666 ir_node *left, *right, *iv, *rc;
667 ir_op *op = get_irn_op(irn);
668 ir_opcode code = get_op_code(op);
677 left = get_binop_left(irn);
678 right = get_binop_right(irn);
680 liv = is_iv(left, env);
681 riv = is_iv(right, env);
682 if (liv && is_rc(right, liv)) {
683 iv = left; rc = right;
685 else if (riv && is_op_commutative(op) &&
687 iv = right; rc = left;
691 if (env->osr_flags & osr_flag_keep_reg_pressure) {
692 if (! check_users_for_reg_pressure(iv, env))
695 return replace(irn, iv, rc, env);
702 } /* check_replace */
705 * Check which SCC's are induction variables.
708 * @param env the environment
710 static void classify_iv(scc *pscc, iv_env *env) {
711 ir_node *irn, *next, *header = NULL;
712 node_entry *b, *h = NULL;
713 int j, only_phi, num_outside;
716 /* find the header block for this scc */
717 for (irn = pscc->head; irn; irn = next) {
718 node_entry *e = get_irn_link(irn);
719 ir_node *block = get_nodes_block(irn);
722 b = get_irn_ne(block, env);
725 if (h->POnum < b->POnum) {
736 /* check if this scc contains only Phi, Add or Sub nodes */
740 for (irn = pscc->head; irn; irn = next) {
741 node_entry *e = get_irn_ne(irn, env);
744 switch (get_irn_opcode(irn)) {
750 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
751 ir_node *pred = get_irn_n(irn, j);
752 node_entry *pe = get_irn_ne(pred, env);
754 if (pe->pscc != e->pscc) {
755 /* not in the same SCC, must be a region const */
756 if (! is_rc(pred, header)) {
757 /* not an induction variable */
763 } else if (out_rc != pred) {
770 /* not an induction variable */
774 /* found an induction variable */
775 DB((dbg, LEVEL_2, " Found an induction variable:\n "));
776 if (only_phi && num_outside == 1) {
777 /* a phi cycle with only one real predecessor can be collapsed */
778 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
780 for (irn = pscc->head; irn; irn = next) {
781 node_entry *e = get_irn_ne(irn, env);
784 exchange(irn, out_rc);
790 /* set the header for every node in this scc */
791 for (irn = pscc->head; irn; irn = next) {
792 node_entry *e = get_irn_ne(irn, env);
795 DB((dbg, LEVEL_2, " %+F,", irn));
797 DB((dbg, LEVEL_2, "\n"));
801 for (irn = pscc->head; irn; irn = next) {
802 node_entry *e = get_irn_ne(irn, env);
810 * Process an SCC for the operator strength reduction.
812 * @param pscc the SCC
813 * @param env the environment
815 static void process_scc(scc *pscc, iv_env *env) {
816 ir_node *head = pscc->head;
817 node_entry *e = get_irn_link(head);
823 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
824 for (irn = pscc->head; irn != NULL; irn = next) {
825 node_entry *e = get_irn_link(irn);
829 DB((dbg, LEVEL_4, " %+F,", irn));
831 DB((dbg, LEVEL_4, "\n"));
835 if (e->next == NULL) {
836 /* this SCC has only a single member */
837 check_replace(head, env);
839 classify_iv(pscc, env);
844 * If an SCC is a Phi only cycle, remove it.
846 * @param pscc an SCC that consists of Phi nodes only
847 * @param env the environment
849 static void remove_phi_cycle(scc *pscc, iv_env *env) {
854 /* check if this scc contains only Phi, Add or Sub nodes */
856 for (irn = pscc->head; irn; irn = next) {
857 node_entry *e = get_irn_ne(irn, env);
863 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
864 ir_node *pred = get_irn_n(irn, j);
865 node_entry *pe = get_irn_ne(pred, env);
867 if (pe->pscc != e->pscc) {
868 /* not in the same SCC, must be the only input */
871 } else if (out_rc != pred) {
877 /* found a Phi cycle */
878 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
880 for (irn = pscc->head; irn; irn = next) {
881 node_entry *e = get_irn_ne(irn, env);
884 exchange(irn, out_rc);
887 } /* remove_phi_cycle */
890 * Process a SCC for the Phi cycle remove.
892 * @param pscc the SCC
893 * @param env the environment
895 static void process_phi_only_scc(scc *pscc, iv_env *env) {
896 ir_node *head = pscc->head;
897 node_entry *e = get_irn_link(head);
903 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
904 for (irn = pscc->head; irn; irn = next) {
905 node_entry *e = get_irn_link(irn);
909 DB((dbg, LEVEL_4, " %+F,", irn));
911 DB((dbg, LEVEL_4, "\n"));
916 remove_phi_cycle(pscc, env);
917 } /* process_phi_only_scc */
921 * Push a node onto the stack.
923 * @param env the environment
924 * @param n the node to push
926 static void push(iv_env *env, ir_node *n) {
929 if (env->tos == ARR_LEN(env->stack)) {
930 int nlen = ARR_LEN(env->stack) * 2;
931 ARR_RESIZE(ir_node *, env->stack, nlen);
933 env->stack[env->tos++] = n;
934 e = get_irn_ne(n, env);
939 * Pop a node from the stack.
941 * @param env the environment
943 * @return The topmost node
945 static ir_node *pop(iv_env *env) {
946 ir_node *n = env->stack[--env->tos];
947 node_entry *e = get_irn_ne(n, env);
954 * Do Tarjan's SCC algorithm and drive OSR.
956 * @param irn start at this node
957 * @param env the environment
959 static void dfs(ir_node *irn, iv_env *env) {
961 node_entry *node = get_irn_ne(irn, env);
963 mark_irn_visited(irn);
965 /* do not put blocks into the scc */
967 n = get_irn_arity(irn);
968 for (i = 0; i < n; ++i) {
969 ir_node *pred = get_irn_n(irn, i);
971 if (!irn_visited(pred))
975 ir_node *block = get_nodes_block(irn);
977 node->DFSnum = env->nextDFSnum++;
978 node->low = node->DFSnum;
981 /* handle the block */
982 if (!irn_visited(block))
985 n = get_irn_arity(irn);
986 for (i = 0; i < n; ++i) {
987 ir_node *pred = get_irn_n(irn, i);
988 node_entry *o = get_irn_ne(pred, env);
990 if (!irn_visited(pred)) {
992 node->low = MIN(node->low, o->low);
994 if (o->DFSnum < node->DFSnum && o->in_stack)
995 node->low = MIN(o->DFSnum, node->low);
997 if (node->low == node->DFSnum) {
998 scc *pscc = OALLOCZ(&env->obst, scc);
1005 e = get_irn_ne(x, env);
1007 e->next = pscc->head;
1011 env->process_scc(pscc, env);
1017 * Do the DFS by starting at the End node of a graph.
1019 * @param irg the graph to process
1020 * @param env the environment
1022 static void do_dfs(ir_graph *irg, iv_env *env) {
1023 ir_graph *rem = current_ir_graph;
1024 ir_node *end = get_irg_end(irg);
1027 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1029 current_ir_graph = irg;
1030 inc_irg_visited(irg);
1032 /* visit all visible nodes */
1035 /* visit the keep-alives */
1036 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
1037 ir_node *ka = get_End_keepalive(end, i);
1039 if (!irn_visited(ka))
1043 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1045 current_ir_graph = rem;
1049 * Post-block-walker: assign the post-order number.
1051 static void assign_po(ir_node *block, void *ctx) {
1053 node_entry *e = get_irn_ne(block, env);
1055 e->POnum = env->POnum++;
1059 * Apply one LFTR edge operation.
1060 * Return NULL if the transformation cannot be done safely without
1063 * @param iv the induction variable
1064 * @param rc the constant that should be translated
1065 * @param e the LFTR edge
1066 * @param env the IV environment
1068 * @return the translated region constant or NULL
1069 * if the translation was not possible
1072 * In the current implementation only the last edge is stored, so
1073 * only one chain exists. That's why we might miss some opportunities.
1075 static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) {
1076 if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1077 tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end;
1078 tarval_int_overflow_mode_t ovmode;
1081 if (! is_counter_iv(iv, env)) {
1082 DB((dbg, LEVEL_4, " not counter IV"));
1086 /* overflow can only be decided for Consts */
1087 if (! is_Const(e->rc)) {
1088 if (e->code == iro_Add && mode_is_reference(get_irn_mode(e->rc))) {
1089 /* However we allow ONE Pointer Add, as pointer arithmetic with wrap
1090 around is undefined anyway */
1091 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->rc));
1093 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
1097 tv_l = get_Const_tarval(rc);
1098 tv_r = get_Const_tarval(e->rc);
1100 ovmode = tarval_get_integer_overflow_mode();
1101 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1103 pscc = get_iv_scc(iv, env);
1104 tv_incr = pscc->incr;
1105 tv_init = pscc->init;
1108 * Check that no overflow occurs:
1109 * init must be transformed without overflow
1110 * the new rc must be transformed without overflow
1111 * rc +/- incr must be possible without overflow
1115 tv = tarval_mul(tv_l, tv_r);
1116 tv_init = tarval_mul(tv_init, tv_r);
1117 tv_incr = tarval_mul(tv_incr, tv_r);
1118 DB((dbg, LEVEL_4, " * %+F", tv_r));
1121 tv = tarval_add(tv_l, tv_r);
1122 tv_init = tarval_add(tv_init, tv_r);
1123 DB((dbg, LEVEL_4, " + %+F", tv_r));
1126 tv = tarval_sub(tv_l, tv_r, NULL);
1127 tv_init = tarval_sub(tv_init, tv_r, NULL);
1128 DB((dbg, LEVEL_4, " - %+F", tv_r));
1131 panic("Unsupported opcode");
1135 if (pscc->code == iro_Add) {
1136 tv_end = tarval_add(tv, tv_incr);
1138 assert(pscc->code == iro_Sub);
1139 tv_end = tarval_sub(tv, tv_incr, NULL);
1142 tarval_set_integer_overflow_mode(ovmode);
1144 if (tv == tarval_bad || tv_init == tarval_bad || tv_end == tarval_bad) {
1145 DB((dbg, LEVEL_4, " = OVERFLOW"));
1148 return new_Const(tv);
1150 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->dst));
1151 } /* applyOneEdge */
1154 * Applies the operations represented by the LFTR edges to a
1155 * region constant and returns the value.
1156 * Return NULL if the transformation cannot be done safely without
1159 * @param pIV points to the IV node that starts the LFTR edge chain
1160 * after translation points to the new IV
1161 * @param rc the region constant that should be translated
1162 * @param env the IV environment
1164 * @return the translated region constant or NULL
1165 * if the translation was not possible
1167 static ir_node *applyEdges(ir_node **pIV, ir_node *rc, iv_env *env) {
1169 if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1170 /* overflow can only be decided for Consts */
1171 if (! is_counter_iv(iv, env)) {
1172 DB((dbg, LEVEL_4, "not counter IV\n", rc));
1175 if (! is_Const(rc)) {
1176 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
1179 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
1183 LFTR_edge *e = LFTR_find(iv, env);
1185 rc = applyOneEdge(iv, rc, e, env);
1190 DB((dbg, LEVEL_3, "\n"));
1196 * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
1197 * and tries to optimize them.
1199 static void do_lftr(ir_node *cmp, void *ctx) {
1201 ir_node *left, *right, *liv, *riv;
1203 ir_node *nleft = NULL, *nright = NULL;
1208 left = get_Cmp_left(cmp);
1209 right = get_Cmp_right(cmp);
1211 liv = is_iv(left, env);
1212 riv = is_iv(right, env);
1213 if (liv && is_rc(right, liv)) {
1214 iv = left; rc = right;
1216 nright = applyEdges(&iv, rc, env);
1219 else if (riv && is_rc(left, riv)) {
1220 iv = right; rc = left;
1222 nleft = applyEdges(&iv, rc, env);
1226 if (nleft && nright) {
1227 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
1228 set_Cmp_left(cmp, nleft);
1229 set_Cmp_right(cmp, nright);
1230 ++env->lftr_replaced;
1235 * do linear function test replacement.
1237 * @param irg the graph that should be optimized
1238 * @param env the IV environment
1240 static void lftr(ir_graph *irg, iv_env *env) {
1241 irg_walk_graph(irg, NULL, do_lftr, env);
1245 * Pre-walker: set all node links to NULL and fix the
1246 * block of Proj nodes.
1248 static void clear_and_fix(ir_node *irn, void *env) {
1250 set_irn_link(irn, NULL);
1253 ir_node *pred = get_Proj_pred(irn);
1254 ir_node *pred_block = get_nodes_block(pred);
1256 if (get_nodes_block(irn) != pred_block) {
1257 set_nodes_block(irn, pred_block);
1261 } /* clear_and_fix */
1264 /* Remove any Phi cycles with only one real input. */
1265 void remove_phi_cycles(ir_graph *irg) {
1270 rem = current_ir_graph;
1271 current_ir_graph = irg;
1273 FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
1275 DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));
1277 obstack_init(&env.obst);
1278 env.stack = NEW_ARR_F(ir_node *, 128);
1282 env.quad_map = NULL;
1283 env.lftr_edges = NULL;
1285 env.lftr_replaced = 0;
1287 env.need_postpass = 0;
1288 env.process_scc = process_phi_only_scc;
1290 /* Clear all links and move Proj nodes into the
1291 the same block as it's predecessors.
1292 This can improve the placement of new nodes.
1295 irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
1297 set_irg_outs_inconsistent(irg);
1299 /* we need outs for calculating the post order */
1300 assure_irg_outs(irg);
1302 /* calculate the post order number for blocks. */
1303 irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1305 /* calculate the SCC's and drive OSR. */
1306 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1308 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1311 set_irg_outs_inconsistent(irg);
1312 DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n", env.replaced));
1315 DEL_ARR_F(env.stack);
1316 obstack_free(&env.obst, NULL);
1318 current_ir_graph = rem;
1319 } /* remove_phi_cycles */
1321 ir_graph_pass_t *remove_phi_cycles_pass(const char *name)
1323 return def_graph_pass(name ? name : "remove_phi_cycles", remove_phi_cycles);
1324 } /* remove_phi_cycles_pass */
1327 * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
1329 static void fix_adds_and_subs(ir_node *irn, void *ctx) {
1333 ir_mode *mode = get_irn_mode(irn);
1335 if (mode_is_int(mode)) {
1338 pred = get_Add_left(irn);
1339 if (get_irn_mode(pred) != mode) {
1340 ir_node *block = get_nodes_block(pred);
1342 pred = new_r_Conv(block, pred, mode);
1343 set_Add_left(irn, pred);
1345 pred = get_Add_right(irn);
1346 if (get_irn_mode(pred) != mode) {
1347 ir_node *block = get_nodes_block(pred);
1349 pred = new_r_Conv(block, pred, mode);
1350 set_Add_right(irn, pred);
1353 } else if (is_Sub(irn)) {
1354 ir_mode *mode = get_irn_mode(irn);
1356 if (mode_is_int(mode)) {
1357 ir_node *left = get_Sub_left(irn);
1358 ir_node *right = get_Sub_right(irn);
1359 ir_mode *l_mode = get_irn_mode(left);
1360 ir_mode *r_mode = get_irn_mode(right);
1362 if (mode_is_int(l_mode) && mode_is_int(r_mode)) {
1363 if (l_mode != mode) {
1364 ir_node *block = get_nodes_block(left);
1366 left = new_r_Conv(block, left, mode);
1367 set_Sub_left(irn, left);
1369 if (r_mode != mode) {
1370 ir_node *block = get_nodes_block(right);
1372 right = new_r_Conv(block, right, mode);
1373 set_Sub_right(irn, right);
1378 } /* fix_adds_and_subs */
1380 /* Performs Operator Strength Reduction for the passed graph. */
1381 void opt_osr(ir_graph *irg, unsigned flags) {
1387 if (! get_opt_strength_red()) {
1388 /* only kill Phi cycles */
1389 remove_phi_cycles(irg);
1393 rem = current_ir_graph;
1394 current_ir_graph = irg;
1396 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
1398 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
1400 obstack_init(&env.obst);
1401 env.stack = NEW_ARR_F(ir_node *, 128);
1405 env.quad_map = new_set(quad_cmp, 64);
1406 env.lftr_edges = new_set(LFTR_cmp, 64);
1408 env.lftr_replaced = 0;
1409 env.osr_flags = flags;
1410 env.need_postpass = 0;
1411 env.process_scc = process_scc;
1413 /* Clear all links and move Proj nodes into the
1414 the same block as it's predecessors.
1415 This can improve the placement of new nodes.
1418 irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
1420 set_irg_outs_inconsistent(irg);
1422 /* we need dominance */
1425 edges = edges_assure(irg);
1427 /* calculate the post order number for blocks by walking the out edges. */
1428 assure_irg_outs(irg);
1429 irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1431 /* calculate the SCC's and drive OSR. */
1432 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1436 if (env.need_postpass)
1437 irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);
1439 /* try linear function test replacements */
1443 set_irg_outs_inconsistent(irg);
1444 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
1446 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1448 del_set(env.lftr_edges);
1449 del_set(env.quad_map);
1450 DEL_ARR_F(env.stack);
1451 obstack_free(&env.obst, NULL);
1454 edges_deactivate(irg);
1456 current_ir_graph = rem;
1460 ir_graph_pass_t pass;
1465 * Wrapper for running opt_osr() as an ir_graph pass.
1467 static int pass_wrapper(ir_graph *irg, void *context) {
1468 struct pass_t *pass = context;
1469 opt_osr(irg, pass->flags);
1471 } /* pass_wrapper */
1473 ir_graph_pass_t *opt_osr_pass(const char *name, unsigned flags)
1475 struct pass_t *pass = XMALLOCZ(struct pass_t);
1477 pass->flags = flags;
1478 return def_graph_pass_constructor(
1479 &pass->pass, name ? name : "osr", pass_wrapper);
1480 } /* opt_osr_pass */