3 * File name: ir/opt/opt_osr.
4 * Purpose: Operator Strength Reduction, based on
5 * Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick
10 * Copyright: (c) 2006 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
44 /** The debug handle. */
45 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
49 ir_node *head; /**< the head of the list */
53 typedef struct node_entry {
54 unsigned DFSnum; /**< the DFS number of this node */
55 unsigned low; /**< the low number of this node */
56 ir_node *header; /**< the header of this node */
57 int in_stack; /**< flag, set if the node is on the stack */
58 ir_node *next; /**< link to the next node the the same scc */
59 scc *pscc; /**< the scc of this node */
60 unsigned POnum; /**< the post order number for blocks */
63 /** The environment. */
64 typedef struct iv_env {
65 struct obstack obst; /**< an obstack for allocations */
66 ir_node **stack; /**< the node stack */
67 int tos; /**< tos index */
68 unsigned nextDFSnum; /**< the current DFS number */
69 unsigned POnum; /**< current post order number */
70 set *quad_map; /**< a map from (op, iv, rc) to node */
71 set *lftr_edges; /**< the set of lftr edges */
72 unsigned replaced; /**< number of replaced ops */
73 unsigned lftr_replaced; /**< number of applied linear function test replacements */
74 unsigned flags; /**< additional flags */
78 * An entry in the (op, node, node) -> node map.
80 typedef struct quad_t {
81 opcode code; /**< the opcode of the reduced operation */
82 ir_node *op1; /**< the first operand the reduced operation */
83 ir_node *op2; /**< the second operand of the reduced operation */
85 ir_node *res; /**< the reduced operation */
91 typedef struct LFTR_edge {
92 ir_node *src; /**< the source node */
93 ir_node *dst; /**< the destination node */
94 opcode code; /**< the opcode that must be applied */
95 ir_node *rc; /**< the region const that must be applied */
99 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
102 * Compare two LFTR edges.
104 static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
105 const LFTR_edge *l1 = e1;
106 const LFTR_edge *l2 = e2;
108 return l1->src != l2->src;
114 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
119 return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
125 static void LFTR_add(ir_node *src, ir_node *dst, opcode code, ir_node *rc, iv_env *env) {
134 * There might be more than one edge here. This is rather bad
135 * because we currently store only one.
137 // assert(LFTR_find(src, env) == NULL);
138 set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
142 * Gets the node_entry of a node
144 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
145 node_entry *e = get_irn_link(irn);
148 e = obstack_alloc(&env->obst, sizeof(*e));
149 memset(e, 0, sizeof(*e));
150 set_irn_link(irn, e);
156 * Check if irn is an IV.
158 * @param irn the node to check
159 * @param env the environment
161 * @returns the header if it is one, NULL else
163 static ir_node *is_iv(ir_node *irn, iv_env *env) {
164 return get_irn_ne(irn, env)->header;
168 * Check if irn is a region constant.
169 * The block or irn must strictly dominate the header block.
171 * @param irn the node to check
172 * @param header_block the header block of the induction variable
174 static int is_rc(ir_node *irn, ir_node *header_block) {
175 ir_node *block = get_nodes_block(irn);
177 return (block != header_block) && block_dominates(block, header_block);
181 * Set compare function for the quad set.
183 static int quad_cmp(const void *e1, const void *e2, size_t size) {
184 const quad_t *c1 = e1;
185 const quad_t *c2 = e2;
187 return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
191 * Check if an reduced operation was already calculated.
193 * @param code the opcode of the operation
194 * @param op1 the first operand of the operation
195 * @param op2 the second operand of the operation
196 * @param env the environment
198 * @return the already reduced node or NULL if this operation is not yet reduced
200 static ir_node *search(opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
207 entry = set_find(env->quad_map, &key, sizeof(key),
208 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
215 * Add an reduced operation.
217 * @param code the opcode of the operation
218 * @param op1 the first operand of the operation
219 * @param op2 the second operand of the operation
220 * @param result the result of the reduced operation
221 * @param env the environment
223 static void add(opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
231 set_insert(env->quad_map, &key, sizeof(key),
232 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
236 * Find a location where to place a bin-op whose operands are in
239 * @param block1 the block of the first operand
240 * @param block2 the block of the second operand
242 * Note that we know here that such a place must exists. Moreover, this means
243 * that either block1 dominates block2 or vice versa. So, just return
246 static ir_node *find_location(ir_node *block1, ir_node *block2) {
247 if (block_dominates(block1, block2))
249 assert(block_dominates(block2, block1));
254 * Create a node that executes an op1 code op1 operation.
256 * @param code the opcode to execute
257 * @param db debug info to add to the new node
258 * @param op1 the first operand
259 * @param op2 the second operand
260 * @param mode the mode of the new operation
262 * @return the newly created node
264 static ir_node *do_apply(opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
265 ir_graph *irg = current_ir_graph;
267 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
271 result = new_rd_Mul(db, irg, block, op1, op2, mode);
274 result = new_rd_Add(db, irg, block, op1, op2, mode);
277 result = new_rd_Sub(db, irg, block, op1, op2, mode);
287 * The Apply operation.
289 * @param orig the node that represent the original operation and determines
290 * the opcode, debug-info and mode of a newly created one
291 * @param op1 the first operand
292 * @param op2 the second operand
293 * @param env the environment
295 * @return the newly created node
297 static ir_node *apply(ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
298 opcode code = get_irn_opcode(orig);
299 ir_node *result = search(code, op1, op2, env);
302 dbg_info *db = get_irn_dbg_info(orig);
303 ir_node *op1_header = get_irn_ne(op1, env)->header;
304 ir_node *op2_header = get_irn_ne(op2, env)->header;
306 if (op1_header != NULL && is_rc(op2, op1_header)) {
307 result = reduce(orig, op1, op2, env);
309 else if (op2_header != NULL && is_rc(op1, op2_header)) {
310 result = reduce(orig, op2, op1, env);
313 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
314 get_irn_ne(result, env)->header = NULL; }
320 * The Reduce operation.
322 * @param orig the node that represent the original operation and determines
323 * the opcode, debug-info and mode of a newly created one
324 * @param iv the induction variable
325 * @param rc the region constant
326 * @param env the environment
328 * @return the reduced node
330 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
331 opcode code = get_irn_opcode(orig);
332 ir_node *result = search(code, iv, rc, env);
335 node_entry *e, *iv_e;
337 ir_mode *mode = get_irn_mode(orig);
339 result = exact_copy(iv);
341 /* Beware: we must always create a new nduction variable with the same mode
342 as the node we are replacing. Espicially this means the mode might be changed
343 from P to I and back. This is always possible, because we have only Phi, Add
345 set_irn_mode(result, mode);
346 add(code, iv, rc, result, env);
347 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
348 get_irn_opname(orig), rc));
350 iv_e = get_irn_ne(iv, env);
351 e = get_irn_ne(result, env);
352 e->header = iv_e->header;
354 /* create the LFTR edge */
355 LFTR_add(iv, result, code, rc, env);
357 n = get_irn_arity(result);
358 for (i = 0; i < n; ++i) {
359 ir_node *o = get_irn_n(result, i);
361 e = get_irn_ne(o, env);
362 if (e->header == iv_e->header)
363 o = reduce(orig, o, rc, env);
364 else if (is_Phi(result))
365 o = apply(orig, o, rc, env);
368 o = apply(orig, o, rc, env);
370 set_irn_n(result, i, o);
374 DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
375 get_irn_opname(orig), rc));
381 * The Replace operation.
383 * @param irn the node that will be replaced
384 * @param iv the induction variable
385 * @param rc the region constant
386 * @param env the environment
388 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
390 ir_loop *iv_loop = get_irn_loop(get_nodes_block(iv));
391 ir_loop *irn_loop = get_irn_loop(get_nodes_block(irn));
393 /* only replace nodes that are in the same (or deeper loops) */
394 if (get_loop_depth(irn_loop) >= get_loop_depth(iv_loop)) {
395 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
397 result = reduce(irn, iv, rc, env);
399 node_entry *e, *iv_e;
401 hook_strength_red(current_ir_graph, irn);
402 exchange(irn, result);
403 e = get_irn_ne(result, env);
404 iv_e = get_irn_ne(iv, env);
405 e->header = iv_e->header;
414 * Check if a node can be replaced (+, -, *).
416 * @param irn the node to check
417 * @param env the environment
419 * @return non-zero if irn should be Replace'd
421 static int check_replace(ir_node *irn, iv_env *env) {
422 ir_node *left, *right, *iv, *rc;
423 ir_op *op = get_irn_op(irn);
424 opcode code = get_op_code(op);
433 left = get_binop_left(irn);
434 right = get_binop_right(irn);
436 liv = is_iv(left, env);
437 riv = is_iv(right, env);
438 if (liv && is_rc(right, liv)) {
439 iv = left; rc = right;
441 else if (riv && is_op_commutative(op) &&
443 iv = right; rc = left;
447 if (code == iro_Mul && env->flags & osr_flag_ignore_x86_shift) {
449 tarval *tv = get_Const_tarval(rc);
451 if (tarval_is_long(tv)) {
452 long value = get_tarval_long(tv);
454 if (value == 2 || value == 4 || value == 8) {
455 /* do not reduce multiplications by 2, 4, 8 */
462 return replace(irn, iv, rc, env);
472 * Check which SCC's are induction variables.
475 * @param env the environment
477 static void classify_iv(scc *pscc, iv_env *env) {
478 ir_node *irn, *next, *header = NULL;
479 node_entry *b, *h = NULL;
480 int j, only_phi, num_outside;
483 /* find the header block for this scc */
484 for (irn = pscc->head; irn; irn = next) {
485 node_entry *e = get_irn_link(irn);
486 ir_node *block = get_nodes_block(irn);
489 b = get_irn_ne(block, env);
492 if (h->POnum < b->POnum) {
503 /* check if this scc contains only Phi, Add or Sub nodes */
507 for (irn = pscc->head; irn; irn = next) {
508 node_entry *e = get_irn_ne(irn, env);
511 switch (get_irn_opcode(irn)) {
517 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
518 ir_node *pred = get_irn_n(irn, j);
519 node_entry *pe = get_irn_ne(pred, env);
521 if (pe->pscc != e->pscc) {
522 /* not in the same SCC, must be a region const */
523 if (! is_rc(pred, header)) {
524 /* not an induction variable */
531 else if (out_rc != pred)
537 /* not an induction variable */
541 /* found an induction variable */
542 DB((dbg, LEVEL_2, " Found an induction variable:\n "));
543 if (only_phi && num_outside == 1) {
544 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
547 /* set the header for every node in this scc */
548 for (irn = pscc->head; irn; irn = next) {
549 node_entry *e = get_irn_ne(irn, env);
552 DB((dbg, LEVEL_2, " %+F,", irn));
554 DB((dbg, LEVEL_2, "\n"));
558 for (irn = pscc->head; irn; irn = next) {
559 node_entry *e = get_irn_ne(irn, env);
562 if (! check_replace(irn, env))
570 * @param pscc the SCC
571 * @param env the environment
573 static void process_scc(scc *pscc, iv_env *env) {
574 ir_node *head = pscc->head;
575 node_entry *e = get_irn_link(head);
581 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
582 for (irn = pscc->head; irn; irn = next) {
583 node_entry *e = get_irn_link(irn);
587 DB((dbg, LEVEL_4, " %+F,", irn));
589 DB((dbg, LEVEL_4, "\n"));
593 if (e->next == NULL) {
594 /* this SCC has only a single member */
595 check_replace(head, env);
598 classify_iv(pscc, env);
603 * Push a node onto the stack.
605 * @param env the environment
606 * @param n the node to push
608 static void push(iv_env *env, ir_node *n) {
611 if (env->tos == ARR_LEN(env->stack)) {
612 int nlen = ARR_LEN(env->stack) * 2;
613 ARR_RESIZE(ir_node *, env->stack, nlen);
615 env->stack[env->tos++] = n;
616 e = get_irn_ne(n, env);
621 * pop a node from the stack
623 * @param env the environment
625 * @return The topmost node
627 static ir_node *pop(iv_env *env)
629 ir_node *n = env->stack[--env->tos];
630 node_entry *e = get_irn_ne(n, env);
637 * Do Tarjan's SCC algorithm and drive OSR.
639 * @param irn start at this node
640 * @param env the environment
642 static void dfs(ir_node *irn, iv_env *env)
645 node_entry *node = get_irn_ne(irn, env);
647 mark_irn_visited(irn);
649 /* do not put blocks into the scc */
651 n = get_irn_arity(irn);
652 for (i = 0; i < n; ++i) {
653 ir_node *pred = get_irn_n(irn, i);
655 if (irn_not_visited(pred))
660 ir_node *block = get_nodes_block(irn);
662 node->DFSnum = env->nextDFSnum++;
663 node->low = node->DFSnum;
666 /* handle the block */
667 if (irn_not_visited(block))
670 n = get_irn_arity(irn);
671 for (i = 0; i < n; ++i) {
672 ir_node *pred = get_irn_n(irn, i);
673 node_entry *o = get_irn_ne(pred, env);
675 if (irn_not_visited(pred)) {
677 node->low = MIN(node->low, o->low);
679 if (o->DFSnum < node->DFSnum && o->in_stack)
680 node->low = MIN(o->DFSnum, node->low);
682 if (node->low == node->DFSnum) {
683 scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
691 e = get_irn_ne(x, env);
693 e->next = pscc->head;
697 process_scc(pscc, env);
703 * Do the DFS by starting at the End node of a graph.
705 * @param irg the graph to process
706 * @param env the environment
708 static void do_dfs(ir_graph *irg, iv_env *env) {
709 ir_graph *rem = current_ir_graph;
710 ir_node *end = get_irg_end(irg);
713 current_ir_graph = irg;
714 inc_irg_visited(irg);
716 /* visit all visible nodes */
719 /* visit the keep-alives */
720 n = get_End_n_keepalives(end);
721 for (i = 0; i < n; ++i) {
722 ir_node *ka = get_End_keepalive(end, i);
724 if (irn_not_visited(ka))
728 current_ir_graph = rem;
732 * Post-block-walker: assign the post-order number.
734 static void assign_po(ir_node *block, void *ctx) {
736 node_entry *e = get_irn_ne(block, env);
738 e->POnum = env->POnum++;
742 * Follows the LFTR edges and return the last node in the chain.
744 * @param irn the node that should be followed
745 * @param env the IV environment
748 * In the current implementation only the last edge is stored, so
749 * only one chain exists. That's why we might miss some opportunities.
751 static ir_node *followEdges(ir_node *irn, iv_env *env) {
753 LFTR_edge *e = LFTR_find(irn, env);
762 * Apply one LFTR edge operation.
763 * Return NULL if the transformation cannot be done safely without
766 * @param rc the IV node that should be translated
767 * @param e the LFTR edge
768 * @param env the IV environment
770 * @return the translated region constant or NULL
771 * if the translation was not possible
774 * In the current implementation only the last edge is stored, so
775 * only one chain exists. That's why we might miss some opportunities.
777 static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) {
778 if (env->flags & osr_flag_lftr_with_ov_check) {
779 tarval *tv_l, *tv_r, *tv;
780 tarval_int_overflow_mode_t ovmode;
782 /* overflow can only be decided for Consts */
783 if (! is_Const(e->rc)) {
784 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
788 tv_l = get_Const_tarval(rc);
789 tv_r = get_Const_tarval(e->rc);
791 ovmode = tarval_get_integer_overflow_mode();
792 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
796 tv = tarval_mul(tv_l, tv_r);
797 DB((dbg, LEVEL_4, " * %+F", tv_r));
800 tv = tarval_add(tv_l, tv_r);
801 DB((dbg, LEVEL_4, " + %+F", tv_r));
804 tv = tarval_sub(tv_l, tv_r);
805 DB((dbg, LEVEL_4, " - %+F", tv_r));
811 tarval_set_integer_overflow_mode(ovmode);
813 if (tv == tarval_bad) {
814 DB((dbg, LEVEL_4, " = OVERFLOW"));
817 return new_r_Const(current_ir_graph, get_irn_n(rc, -1), get_tarval_mode(tv), tv);
819 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
823 * Applies the operations represented by the LFTR edges to a
824 * region constant and returns the value.
825 * Return NULL if the transformation cannot be done safely without
828 * @param iv the IV node that starts the LFTR edge chain
829 * @param rc the region constant that should be translated
830 * @param env the IV environment
832 * @return the translated region constant or NULL
833 * if the translation was not possible
835 static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
838 if (env->flags & osr_flag_lftr_with_ov_check) {
839 /* overflow can only be decided for Consts */
840 if (! is_Const(rc)) {
841 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
844 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
847 for (irn = iv; rc;) {
848 LFTR_edge *e = LFTR_find(irn, env);
850 rc = applyOneEdge(rc, e, env);
856 DB((dbg, LEVEL_3, "\n"));
861 * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
862 * and tries to optimize them.
864 static void do_lftr(ir_node *cmp, void *ctx) {
866 ir_node *left, *right, *liv, *riv;
868 ir_node *nleft = NULL, *nright = NULL;
870 if (get_irn_op(cmp) != op_Cmp)
873 left = get_Cmp_left(cmp);
874 right = get_Cmp_right(cmp);
876 liv = is_iv(left, env);
877 riv = is_iv(right, env);
878 if (liv && is_rc(right, liv)) {
879 iv = left; rc = right;
881 nright = applyEdges(iv, rc, env);
882 if (nright && nright != rc) {
883 nleft = followEdges(iv, env);
886 else if (riv && is_rc(left, riv)) {
887 iv = right; rc = left;
889 nleft = applyEdges(iv, rc, env);
890 if (nleft && nleft != rc) {
891 nright = followEdges(iv, env);
895 if (nleft && nright) {
896 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
897 set_Cmp_left(cmp, nleft);
898 set_Cmp_right(cmp, nright);
899 ++env->lftr_replaced;
904 * do linear function test replacement.
906 * @param irg the graph that should be optimized
907 * @param env the IV environment
909 static void lftr(ir_graph *irg, iv_env *env) {
910 irg_walk_graph(irg, NULL, do_lftr, env);
914 * Pre-walker: set all node links to NULL and fix the
915 * block of Proj nodes.
917 static void clear_and_fix(ir_node *irn, void *env)
919 set_irn_link(irn, NULL);
922 ir_node *pred = get_Proj_pred(irn);
923 set_irn_n(irn, -1, get_irn_n(pred, -1));
927 /* Performs Operator Strength Reduction for the passed graph. */
928 void opt_osr(ir_graph *irg, unsigned flags) {
932 if (! get_opt_strength_red())
935 rem = current_ir_graph;
936 current_ir_graph = irg;
938 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
940 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
942 obstack_init(&env.obst);
943 env.stack = NEW_ARR_F(ir_node *, 128);
947 env.quad_map = new_set(quad_cmp, 64);
948 env.lftr_edges = new_set(LFTR_cmp, 64);
950 env.lftr_replaced = 0;
953 /* we need control flow loop information to decide whether
954 * we should do a replacement or not. */
955 construct_cf_backedges(irg);
957 /* Clear all links and move Proj nodes into the
958 the same block as it's predecessors.
959 This can improve the placement of new nodes.
961 irg_walk_graph(irg, NULL, clear_and_fix, NULL);
963 /* we need dominance */
965 assure_irg_outs(irg);
967 /* calculate the post order number for blocks. */
968 irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
970 /* calculate the SCC's and drive OSR. */
974 /* try linear function test replacements */
978 set_irg_outs_inconsistent(irg);
979 /* cfg loop still valid */
981 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
984 del_set(env.lftr_edges);
985 del_set(env.quad_map);
986 DEL_ARR_F(env.stack);
987 obstack_free(&env.obst, NULL);
989 current_ir_graph = rem;