3 * File name: ir/opt/opt_osr.
4 * Purpose: Operator Strength Reduction,
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.
41 /** The debug handle. */
42 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
46 ir_node *head; /**< the head of the list */
50 typedef struct node_entry {
51 unsigned DFSnum; /**< the DFS number of this node */
52 unsigned low; /**< the low number of this node */
53 ir_node *header; /**< the header of this node */
54 int in_stack; /**< flag, set if the node is on the stack */
55 ir_node *next; /**< link to the next node the the same scc */
56 scc *pscc; /**< the scc of this node */
57 unsigned POnum; /**< the post order number for blocks */
60 /** The environment. */
61 typedef struct iv_env {
62 struct obstack obst; /**< an obstack for allocations */
63 ir_node **stack; /**< the node stack */
64 int tos; /**< tos index */
65 unsigned nextDFSnum; /**< the current DFS number */
66 unsigned POnum; /**< current post order number */
67 set *quad_map; /**< a map from (op, iv, rc) to node */
68 set *lftr_edges; /**< the set of lftr edges */
69 unsigned replaced; /**< number of replaced ops */
70 unsigned lftr_replaced; /**< number of applied linear function test replacements */
71 unsigned flags; /**< additional flags */
75 * An entry in the (op, node, node) -> node map.
77 typedef struct quad_t {
78 opcode code; /**< the opcode of the reduced operation */
79 ir_node *op1; /**< the first operand the reduced operation */
80 ir_node *op2; /**< the second operand of the reduced operation */
82 ir_node *res; /**< the reduced operation */
88 typedef struct LFTR_edge {
89 ir_node *src; /**< the source node */
90 ir_node *dst; /**< the destination node */
91 opcode code; /**< the opcode that must be applied */
92 ir_node *rc; /**< the region const that must be applied */
96 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
99 * Compare two LFTR edges.
101 static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
102 const LFTR_edge *l1 = e1;
103 const LFTR_edge *l2 = e2;
105 return l1->src != l2->src;
111 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
116 return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
122 static void LFTR_add(ir_node *src, ir_node *dst, opcode code, ir_node *rc, iv_env *env) {
130 assert(LFTR_find(src, env) == NULL);
131 set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
135 * Gets the node_entry of a node
137 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
138 node_entry *e = get_irn_link(irn);
141 e = obstack_alloc(&env->obst, sizeof(*e));
142 memset(e, 0, sizeof(*e));
143 set_irn_link(irn, e);
149 * Check if irn is an IV.
151 * @returns the header if it is one, NULL else
153 static ir_node *is_iv(ir_node *irn, iv_env *env) {
154 return get_irn_ne(irn, env)->header;
158 * Check if irn is a region constant.
160 static int is_rc(ir_node *irn, ir_node *header_block) {
161 ir_node *block = get_nodes_block(irn);
163 return block_dominates(block, header_block);
167 * Set compare function for the quad set.
169 static int quad_cmp(const void *e1, const void *e2, size_t size) {
170 const quad_t *c1 = e1;
171 const quad_t *c2 = e2;
173 return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
177 * Check if an reduced operation was already calculated.
179 static ir_node *search(opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
186 entry = set_find(env->quad_map, &key, sizeof(key),
187 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
194 * Add an reduced operation was already calculated.
196 static void add(opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
204 set_insert(env->quad_map, &key, sizeof(key),
205 (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
209 * Find a location where to place a bin-op whose operands are in
212 * Note that we know here that such a place must exists. Moreover, this means
213 * that either block1 dominates block2 or vice versa. So, just return
216 static ir_node *find_location(ir_node *block1, ir_node *block2) {
217 if (block_dominates(block1, block2))
219 assert(block_dominates(block2, block1));
224 * create an op1 code op1 operation.
226 static ir_node *do_apply(opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
227 ir_graph *irg = current_ir_graph;
229 ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
233 result = new_rd_Mul(db, irg, block, op1, op2, mode);
236 result = new_rd_Add(db, irg, block, op1, op2, mode);
239 result = new_rd_Sub(db, irg, block, op1, op2, mode);
249 * The Apply operation.
251 static ir_node *apply(ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
252 opcode code = get_irn_opcode(orig);
253 ir_node *result = search(code, op1, op2, env);
256 dbg_info *db = get_irn_dbg_info(orig);
257 ir_node *op1_header = get_irn_ne(op1, env)->header;
258 ir_node *op2_header = get_irn_ne(op2, env)->header;
260 if (op1_header != NULL && is_rc(op2, op1_header)) {
261 result = reduce(orig, op1, op2, env);
263 else if (op2_header != NULL && is_rc(op1, op2_header)) {
264 result = reduce(orig, op2, op1, env);
267 result = do_apply(code, db, op1, op2, get_irn_mode(orig));
268 get_irn_ne(result, env)->header = NULL;
275 * The Reduce operation.
277 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
278 opcode code = get_irn_opcode(orig);
279 ir_node *result = search(code, iv, rc, env);
282 node_entry *e, *iv_e;
284 ir_mode *mode = get_irn_mode(orig);
286 result = exact_copy(iv);
287 if (mode_is_reference(mode)) {
288 /* bad case: we replace a reference mode calculation.
289 assure that the new IV will be a reference one */
290 set_irn_mode(result, mode);
292 add(code, iv, rc, result, env);
293 DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
294 get_irn_opname(orig), rc));
296 iv_e = get_irn_ne(iv, env);
297 e = get_irn_ne(result, env);
298 e->header = iv_e->header;
300 /* create the LFTR edge */
301 LFTR_add(iv, result, code, rc, env);
303 n = get_irn_arity(result);
304 for (i = 0; i < n; ++i) {
305 ir_node *o = get_irn_n(result, i);
307 e = get_irn_ne(o, env);
308 if (e->header == iv_e->header)
309 o = reduce(orig, o, rc, env);
310 else if (is_Phi(result))
311 o = apply(orig, o, rc, env);
315 o = apply(orig, o, rc, env);
319 set_irn_n(result, i, o);
326 * Do the replacement operation.
328 * @param irn the node that will be replaced
329 * @param iv the induction variable
330 * @param rc the region constant
331 * @param env the environment
333 static void replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
336 DB((dbg, LEVEL_2, " Replacing %+F\n", irn));
338 result = reduce(irn, iv, rc, env);
339 if (result && result != irn) {
340 node_entry *e, *iv_e;
342 exchange(irn, result);
343 e = get_irn_ne(result, env);
344 iv_e = get_irn_ne(iv, env);
345 e->header = iv_e->header;
350 * check if a node can be replaced.
352 static int check_replace(ir_node *irn, iv_env *env) {
353 ir_node *left, *right, *iv, *rc;
354 ir_op *op = get_irn_op(irn);
355 opcode code = get_op_code(op);
364 left = get_binop_left(irn);
365 right = get_binop_right(irn);
367 liv = is_iv(left, env);
368 riv = is_iv(right, env);
369 if (liv && is_rc(right, liv)) {
370 iv = left; rc = right;
372 else if (is_op_commutative(op) &&
373 riv && is_rc(left, riv)) {
374 iv = right; rc = left;
378 replace(irn, iv, rc, env);
388 * check which SCC's are induction variables
390 static void classify_iv(scc *pscc, iv_env *env) {
391 ir_node *irn, *next, *header = NULL;
395 /* find the header block for this scc */
396 for (irn = pscc->head; irn; irn = next) {
397 node_entry *e = get_irn_link(irn);
398 ir_node *block = get_nodes_block(irn);
401 b = get_irn_ne(block, env);
404 if (h->POnum < b->POnum) {
415 /* check if this scc contains only Phi, Add or Sub nodes */
416 for (irn = pscc->head; irn; irn = next) {
417 node_entry *e = get_irn_ne(irn, env);
420 switch (get_irn_opcode(irn)) {
424 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
425 ir_node *pred = get_irn_n(irn, j);
426 node_entry *pe = get_irn_ne(pred, env);
428 if (pe->pscc != e->pscc) {
429 /* not in the same SCC, must be a region const */
430 if (! is_rc(pred, header)) {
431 /* not an induction variable */
438 /* not an induction variable */
442 /* found an induction variable */
443 DB((dbg, LEVEL_2, " Found an induction variable in %+F\n", pscc->head));
445 /* set the header for every node in this scc */
446 for (irn = pscc->head; irn; irn = next) {
447 node_entry *e = get_irn_ne(irn, env);
454 for (irn = pscc->head; irn; irn = next) {
455 node_entry *e = get_irn_ne(irn, env);
458 if (! check_replace(irn, env))
464 * Process a SCC given as a list.
466 static void process_scc(scc *pscc, iv_env *env) {
467 ir_node *head = pscc->head;
468 node_entry *e = get_irn_link(head);
474 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
475 for (irn = pscc->head; irn; irn = next) {
476 node_entry *e = get_irn_link(irn);
480 DB((dbg, LEVEL_4, " %+F,", irn));
482 DB((dbg, LEVEL_4, "\n"));
486 if (e->next == NULL) {
487 /* this SCC has only a single member */
488 check_replace(head, env);
491 classify_iv(pscc, env);
496 * Push a node onto the stack.
498 static void push(iv_env *env, ir_node *n) {
501 if (env->tos == ARR_LEN(env->stack)) {
502 int nlen = ARR_LEN(env->stack) * 2;
503 ARR_RESIZE(ir_node *, env->stack, nlen);
505 env->stack[env->tos++] = n;
506 e = get_irn_ne(n, env);
511 * pop a node from the stack
513 * @return The topmost node
515 static ir_node *pop(iv_env *env)
517 ir_node *n = env->stack[--env->tos];
518 node_entry *e = get_irn_ne(n, env);
525 * Do Tarjan's SCC algorithm and drive OSR
527 * @param irn start at this node
528 * @param env the environment
530 static void dfs(ir_node *irn, iv_env *env)
533 node_entry *node = get_irn_ne(irn, env);
535 mark_irn_visited(irn);
537 /* do not put blocks into the scc */
539 n = get_irn_arity(irn);
540 for (i = 0; i < n; ++i) {
541 ir_node *pred = get_irn_n(irn, i);
543 if (irn_not_visited(pred))
548 ir_node *block = get_nodes_block(irn);
550 node->DFSnum = env->nextDFSnum++;
551 node->low = node->DFSnum;
554 /* handle the block */
555 if (irn_not_visited(block))
558 n = get_irn_arity(irn);
559 for (i = 0; i < n; ++i) {
560 ir_node *pred = get_irn_n(irn, i);
561 node_entry *o = get_irn_ne(pred, env);
563 if (irn_not_visited(pred)) {
565 node->low = MIN(node->low, o->low);
567 if (o->DFSnum < node->DFSnum && o->in_stack)
568 node->low = MIN(o->DFSnum, node->low);
570 if (node->low == node->DFSnum) {
571 scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
579 e = get_irn_ne(x, env);
581 e->next = pscc->head;
585 process_scc(pscc, env);
591 * Do the DFS by starting end the End node
593 static void do_dfs(ir_graph *irg, iv_env *env) {
594 ir_graph *rem = current_ir_graph;
595 ir_node *end = get_irg_end(irg);
598 current_ir_graph = irg;
599 inc_irg_visited(irg);
601 /* visit all visible nodes */
604 /* visit the keep-alives */
605 n = get_End_n_keepalives(end);
606 for (i = 0; i < n; ++i) {
607 ir_node *ka = get_End_keepalive(end, i);
609 if (irn_not_visited(ka))
613 current_ir_graph = rem;
617 * Post-block-walker: assign the post-order number.
619 static void assign_po(ir_node *block, void *ctx) {
621 node_entry *e = get_irn_ne(block, env);
623 e->POnum = env->POnum++;
627 * follows the LFTR edges and return the last node in the chain.
629 * @param irn the node that should be followed
630 * @param env the IV environment
632 static ir_node *followEdges(ir_node *irn, iv_env *env) {
634 LFTR_edge *e = LFTR_find(irn, env);
643 * Apply one LFTR edge operation.
644 * Return NULL if the transformation cannot be done safely without
647 * @param rc the IV node that should be translated
648 * @param e the LFTR edge
649 * @param env the IV environment
651 static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) {
652 if (env->flags & osr_flag_lftr_with_ov_check) {
653 tarval *tv_l, *tv_r, *tv;
654 tarval_int_overflow_mode_t ovmode;
656 /* overflow can only be decided for Consts */
657 if (! is_Const(e->rc)) {
658 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
662 tv_l = get_Const_tarval(rc);
663 tv_r = get_Const_tarval(e->rc);
665 ovmode = tarval_get_integer_overflow_mode();
666 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
670 tv = tarval_mul(tv_l, tv_r);
671 DB((dbg, LEVEL_4, " * %+F", tv_r));
674 tv = tarval_add(tv_l, tv_r);
675 DB((dbg, LEVEL_4, " + %+F", tv_r));
678 tv = tarval_sub(tv_l, tv_r);
679 DB((dbg, LEVEL_4, " - %+F", tv_r));
685 tarval_set_integer_overflow_mode(ovmode);
687 if (tv == tarval_bad) {
688 DB((dbg, LEVEL_4, " = OVERFLOW"));
691 return new_r_Const(current_ir_graph, get_irn_n(rc, -1), get_tarval_mode(tv), tv);
693 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
697 * Applies the operations represented by the LFTR edges to a
698 * region constant and returns the value.
699 * Return NULL if the transformation cannot be done safely without
702 * @param iv the IV node that starts the LFTR edge chain
703 * @param rc the region constant that should be translated
704 * @param env the IV environment
706 static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
709 if (env->flags & osr_flag_lftr_with_ov_check) {
710 /* overflow can only be decided for Consts */
711 if (! is_Const(rc)) {
712 DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
715 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
718 for (irn = iv; rc;) {
719 LFTR_edge *e = LFTR_find(irn, env);
721 rc = applyOneEdge(rc, e, env);
727 DB((dbg, LEVEL_3, "\n"));
732 * Walker; find Cmp(iv, rc) or Cmp(rc, iv)
734 static void do_lftr(ir_node *cmp, void *ctx) {
736 ir_node *left, *right, *liv, *riv;
738 ir_node *nleft = NULL, *nright = NULL;
740 if (get_irn_op(cmp) != op_Cmp)
743 left = get_Cmp_left(cmp);
744 right = get_Cmp_right(cmp);
746 liv = is_iv(left, env);
747 riv = is_iv(right, env);
748 if (liv && is_rc(right, liv)) {
749 iv = left; rc = right;
751 nright = applyEdges(iv, rc, env);
753 nleft = followEdges(iv, env);
756 else if (riv && is_rc(left, riv)) {
757 iv = right; rc = left;
759 nleft = applyEdges(iv, rc, env);
761 nright = followEdges(iv, env);
765 if (nleft && nright) {
766 DB((dbg, LEVEL_2, " LFTR for %+F\n", cmp));
767 set_Cmp_left(cmp, nleft);
768 set_Cmp_right(cmp, nright);
769 ++env->lftr_replaced;
774 * do linear function test replacement.
776 static void lftr(ir_graph *irg, iv_env *env) {
777 irg_walk_graph(irg, NULL, do_lftr, env);
780 /* Performs Operator Strength Reduction for the passed graph. */
781 void opt_osr(ir_graph *irg, unsigned flags) {
784 if (! get_opt_strength_red())
787 FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
788 // firm_dbg_set_mask(dbg, SET_LEVEL_3);
790 /* and dominance as well */
793 DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
795 obstack_init(&env.obst);
796 env.stack = NEW_ARR_F(ir_node *, 128);
800 env.quad_map = new_set(quad_cmp, 64);
801 env.lftr_edges = new_set(LFTR_cmp, 64);
803 env.lftr_replaced = 0;
806 /* clear all links */
807 irg_walk_graph(irg, NULL, firm_clear_link, NULL);
809 /* calculate the post order number */
810 irg_block_walk_graph(irg, NULL, assign_po, &env);
812 /* calculate the SCC's and drive OSR */
816 /* try linear function test replacements */
819 set_irg_outs_inconsistent(irg);
820 set_irg_loopinfo_inconsistent(irg);
822 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
824 del_set(env.lftr_edges);
825 del_set(env.quad_map);
826 DEL_ARR_F(env.stack);
827 obstack_free(&env.obst, NULL);