* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / tree-ssa-reassoc.c
blobdf99f0dea676bda6c54d351df2807f42de681472
1 /* Reassociation for trees.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hash-table.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "tree.h"
29 #include "stor-layout.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "cfganal.h"
41 #include "basic-block.h"
42 #include "gimple-pretty-print.h"
43 #include "tree-inline.h"
44 #include "hash-map.h"
45 #include "tree-ssa-alias.h"
46 #include "internal-fn.h"
47 #include "gimple-fold.h"
48 #include "tree-eh.h"
49 #include "gimple-expr.h"
50 #include "is-a.h"
51 #include "gimple.h"
52 #include "gimple-iterator.h"
53 #include "gimplify-me.h"
54 #include "gimple-ssa.h"
55 #include "tree-cfg.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "stringpool.h"
59 #include "tree-ssanames.h"
60 #include "tree-ssa-loop-niter.h"
61 #include "tree-ssa-loop.h"
62 #include "expr.h"
63 #include "tree-dfa.h"
64 #include "tree-ssa.h"
65 #include "tree-iterator.h"
66 #include "tree-pass.h"
67 #include "alloc-pool.h"
68 #include "langhooks.h"
69 #include "cfgloop.h"
70 #include "flags.h"
71 #include "target.h"
72 #include "params.h"
73 #include "diagnostic-core.h"
74 #include "builtins.h"
75 #include "gimplify.h"
76 #include "insn-codes.h"
77 #include "optabs.h"
79 /* This is a simple global reassociation pass. It is, in part, based
80 on the LLVM pass of the same name (They do some things more/less
81 than we do, in different orders, etc).
83 It consists of five steps:
85 1. Breaking up subtract operations into addition + negate, where
86 it would promote the reassociation of adds.
88 2. Left linearization of the expression trees, so that (A+B)+(C+D)
89 becomes (((A+B)+C)+D), which is easier for us to rewrite later.
90 During linearization, we place the operands of the binary
91 expressions into a vector of operand_entry_t
93 3. Optimization of the operand lists, eliminating things like a +
94 -a, a & a, etc.
96 3a. Combine repeated factors with the same occurrence counts
97 into a __builtin_powi call that will later be optimized into
98 an optimal number of multiplies.
100 4. Rewrite the expression trees we linearized and optimized so
101 they are in proper rank order.
103 5. Repropagate negates, as nothing else will clean it up ATM.
105 A bit of theory on #4, since nobody seems to write anything down
106 about why it makes sense to do it the way they do it:
108 We could do this much nicer theoretically, but don't (for reasons
109 explained after how to do it theoretically nice :P).
111 In order to promote the most redundancy elimination, you want
112 binary expressions whose operands are the same rank (or
113 preferably, the same value) exposed to the redundancy eliminator,
114 for possible elimination.
116 So the way to do this if we really cared, is to build the new op
117 tree from the leaves to the roots, merging as you go, and putting the
118 new op on the end of the worklist, until you are left with one
119 thing on the worklist.
121 IE if you have to rewrite the following set of operands (listed with
122 rank in parentheses), with opcode PLUS_EXPR:
124 a (1), b (1), c (1), d (2), e (2)
127 We start with our merge worklist empty, and the ops list with all of
128 those on it.
130 You want to first merge all leaves of the same rank, as much as
131 possible.
133 So first build a binary op of
135 mergetmp = a + b, and put "mergetmp" on the merge worklist.
137 Because there is no three operand form of PLUS_EXPR, c is not going to
138 be exposed to redundancy elimination as a rank 1 operand.
140 So you might as well throw it on the merge worklist (you could also
141 consider it to now be a rank two operand, and merge it with d and e,
142 but in this case, you then have evicted e from a binary op. So at
143 least in this situation, you can't win.)
145 Then build a binary op of d + e
146 mergetmp2 = d + e
148 and put mergetmp2 on the merge worklist.
150 so merge worklist = {mergetmp, c, mergetmp2}
152 Continue building binary ops of these operations until you have only
153 one operation left on the worklist.
155 So we have
157 build binary op
158 mergetmp3 = mergetmp + c
160 worklist = {mergetmp2, mergetmp3}
162 mergetmp4 = mergetmp2 + mergetmp3
164 worklist = {mergetmp4}
166 because we have one operation left, we can now just set the original
167 statement equal to the result of that operation.
169 This will at least expose a + b and d + e to redundancy elimination
170 as binary operations.
172 For extra points, you can reuse the old statements to build the
173 mergetmps, since you shouldn't run out.
175 So why don't we do this?
177 Because it's expensive, and rarely will help. Most trees we are
178 reassociating have 3 or less ops. If they have 2 ops, they already
179 will be written into a nice single binary op. If you have 3 ops, a
180 single simple check suffices to tell you whether the first two are of the
181 same rank. If so, you know to order it
183 mergetmp = op1 + op2
184 newstmt = mergetmp + op3
186 instead of
187 mergetmp = op2 + op3
188 newstmt = mergetmp + op1
190 If all three are of the same rank, you can't expose them all in a
191 single binary operator anyway, so the above is *still* the best you
192 can do.
194 Thus, this is what we do. When we have three ops left, we check to see
195 what order to put them in, and call it a day. As a nod to vector sum
196 reduction, we check if any of the ops are really a phi node that is a
197 destructive update for the associating op, and keep the destructive
198 update together for vector sum reduction recognition. */
201 /* Statistics */
202 static struct
204 int linearized;
205 int constants_eliminated;
206 int ops_eliminated;
207 int rewritten;
208 int pows_encountered;
209 int pows_created;
210 } reassociate_stats;
212 /* Operator, rank pair. */
213 typedef struct operand_entry
215 unsigned int rank;
216 int id;
217 tree op;
218 unsigned int count;
219 } *operand_entry_t;
221 static alloc_pool operand_entry_pool;
223 /* This is used to assign a unique ID to each struct operand_entry
224 so that qsort results are identical on different hosts. */
225 static int next_operand_entry_id;
227 /* Starting rank number for a given basic block, so that we can rank
228 operations using unmovable instructions in that BB based on the bb
229 depth. */
230 static long *bb_rank;
232 /* Operand->rank hashtable. */
233 static hash_map<tree, long> *operand_rank;
235 /* Vector of SSA_NAMEs on which after reassociate_bb is done with
236 all basic blocks the CFG should be adjusted - basic blocks
237 split right after that SSA_NAME's definition statement and before
238 the only use, which must be a bit ior. */
239 static vec<tree> reassoc_branch_fixups;
241 /* Forward decls. */
242 static long get_rank (tree);
243 static bool reassoc_stmt_dominates_stmt_p (gimple, gimple);
245 /* Wrapper around gsi_remove, which adjusts gimple_uid of debug stmts
246 possibly added by gsi_remove. */
248 bool
249 reassoc_remove_stmt (gimple_stmt_iterator *gsi)
251 gimple stmt = gsi_stmt (*gsi);
253 if (!MAY_HAVE_DEBUG_STMTS || gimple_code (stmt) == GIMPLE_PHI)
254 return gsi_remove (gsi, true);
256 gimple_stmt_iterator prev = *gsi;
257 gsi_prev (&prev);
258 unsigned uid = gimple_uid (stmt);
259 basic_block bb = gimple_bb (stmt);
260 bool ret = gsi_remove (gsi, true);
261 if (!gsi_end_p (prev))
262 gsi_next (&prev);
263 else
264 prev = gsi_start_bb (bb);
265 gimple end_stmt = gsi_stmt (*gsi);
266 while ((stmt = gsi_stmt (prev)) != end_stmt)
268 gcc_assert (stmt && is_gimple_debug (stmt) && gimple_uid (stmt) == 0);
269 gimple_set_uid (stmt, uid);
270 gsi_next (&prev);
272 return ret;
275 /* Bias amount for loop-carried phis. We want this to be larger than
276 the depth of any reassociation tree we can see, but not larger than
277 the rank difference between two blocks. */
278 #define PHI_LOOP_BIAS (1 << 15)
280 /* Rank assigned to a phi statement. If STMT is a loop-carried phi of
281 an innermost loop, and the phi has only a single use which is inside
282 the loop, then the rank is the block rank of the loop latch plus an
283 extra bias for the loop-carried dependence. This causes expressions
284 calculated into an accumulator variable to be independent for each
285 iteration of the loop. If STMT is some other phi, the rank is the
286 block rank of its containing block. */
287 static long
288 phi_rank (gimple stmt)
290 basic_block bb = gimple_bb (stmt);
291 struct loop *father = bb->loop_father;
292 tree res;
293 unsigned i;
294 use_operand_p use;
295 gimple use_stmt;
297 /* We only care about real loops (those with a latch). */
298 if (!father->latch)
299 return bb_rank[bb->index];
301 /* Interesting phis must be in headers of innermost loops. */
302 if (bb != father->header
303 || father->inner)
304 return bb_rank[bb->index];
306 /* Ignore virtual SSA_NAMEs. */
307 res = gimple_phi_result (stmt);
308 if (virtual_operand_p (res))
309 return bb_rank[bb->index];
311 /* The phi definition must have a single use, and that use must be
312 within the loop. Otherwise this isn't an accumulator pattern. */
313 if (!single_imm_use (res, &use, &use_stmt)
314 || gimple_bb (use_stmt)->loop_father != father)
315 return bb_rank[bb->index];
317 /* Look for phi arguments from within the loop. If found, bias this phi. */
318 for (i = 0; i < gimple_phi_num_args (stmt); i++)
320 tree arg = gimple_phi_arg_def (stmt, i);
321 if (TREE_CODE (arg) == SSA_NAME
322 && !SSA_NAME_IS_DEFAULT_DEF (arg))
324 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
325 if (gimple_bb (def_stmt)->loop_father == father)
326 return bb_rank[father->latch->index] + PHI_LOOP_BIAS;
330 /* Must be an uninteresting phi. */
331 return bb_rank[bb->index];
334 /* If EXP is an SSA_NAME defined by a PHI statement that represents a
335 loop-carried dependence of an innermost loop, return TRUE; else
336 return FALSE. */
337 static bool
338 loop_carried_phi (tree exp)
340 gimple phi_stmt;
341 long block_rank;
343 if (TREE_CODE (exp) != SSA_NAME
344 || SSA_NAME_IS_DEFAULT_DEF (exp))
345 return false;
347 phi_stmt = SSA_NAME_DEF_STMT (exp);
349 if (gimple_code (SSA_NAME_DEF_STMT (exp)) != GIMPLE_PHI)
350 return false;
352 /* Non-loop-carried phis have block rank. Loop-carried phis have
353 an additional bias added in. If this phi doesn't have block rank,
354 it's biased and should not be propagated. */
355 block_rank = bb_rank[gimple_bb (phi_stmt)->index];
357 if (phi_rank (phi_stmt) != block_rank)
358 return true;
360 return false;
363 /* Return the maximum of RANK and the rank that should be propagated
364 from expression OP. For most operands, this is just the rank of OP.
365 For loop-carried phis, the value is zero to avoid undoing the bias
366 in favor of the phi. */
367 static long
368 propagate_rank (long rank, tree op)
370 long op_rank;
372 if (loop_carried_phi (op))
373 return rank;
375 op_rank = get_rank (op);
377 return MAX (rank, op_rank);
380 /* Look up the operand rank structure for expression E. */
382 static inline long
383 find_operand_rank (tree e)
385 long *slot = operand_rank->get (e);
386 return slot ? *slot : -1;
389 /* Insert {E,RANK} into the operand rank hashtable. */
391 static inline void
392 insert_operand_rank (tree e, long rank)
394 gcc_assert (rank > 0);
395 gcc_assert (!operand_rank->put (e, rank));
398 /* Given an expression E, return the rank of the expression. */
400 static long
401 get_rank (tree e)
403 /* Constants have rank 0. */
404 if (is_gimple_min_invariant (e))
405 return 0;
407 /* SSA_NAME's have the rank of the expression they are the result
409 For globals and uninitialized values, the rank is 0.
410 For function arguments, use the pre-setup rank.
411 For PHI nodes, stores, asm statements, etc, we use the rank of
412 the BB.
413 For simple operations, the rank is the maximum rank of any of
414 its operands, or the bb_rank, whichever is less.
415 I make no claims that this is optimal, however, it gives good
416 results. */
418 /* We make an exception to the normal ranking system to break
419 dependences of accumulator variables in loops. Suppose we
420 have a simple one-block loop containing:
422 x_1 = phi(x_0, x_2)
423 b = a + x_1
424 c = b + d
425 x_2 = c + e
427 As shown, each iteration of the calculation into x is fully
428 dependent upon the iteration before it. We would prefer to
429 see this in the form:
431 x_1 = phi(x_0, x_2)
432 b = a + d
433 c = b + e
434 x_2 = c + x_1
436 If the loop is unrolled, the calculations of b and c from
437 different iterations can be interleaved.
439 To obtain this result during reassociation, we bias the rank
440 of the phi definition x_1 upward, when it is recognized as an
441 accumulator pattern. The artificial rank causes it to be
442 added last, providing the desired independence. */
444 if (TREE_CODE (e) == SSA_NAME)
446 gimple stmt;
447 long rank;
448 int i, n;
449 tree op;
451 if (SSA_NAME_IS_DEFAULT_DEF (e))
452 return find_operand_rank (e);
454 stmt = SSA_NAME_DEF_STMT (e);
455 if (gimple_code (stmt) == GIMPLE_PHI)
456 return phi_rank (stmt);
458 if (!is_gimple_assign (stmt)
459 || gimple_vdef (stmt))
460 return bb_rank[gimple_bb (stmt)->index];
462 /* If we already have a rank for this expression, use that. */
463 rank = find_operand_rank (e);
464 if (rank != -1)
465 return rank;
467 /* Otherwise, find the maximum rank for the operands. As an
468 exception, remove the bias from loop-carried phis when propagating
469 the rank so that dependent operations are not also biased. */
470 rank = 0;
471 if (gimple_assign_single_p (stmt))
473 tree rhs = gimple_assign_rhs1 (stmt);
474 n = TREE_OPERAND_LENGTH (rhs);
475 if (n == 0)
476 rank = propagate_rank (rank, rhs);
477 else
479 for (i = 0; i < n; i++)
481 op = TREE_OPERAND (rhs, i);
483 if (op != NULL_TREE)
484 rank = propagate_rank (rank, op);
488 else
490 n = gimple_num_ops (stmt);
491 for (i = 1; i < n; i++)
493 op = gimple_op (stmt, i);
494 gcc_assert (op);
495 rank = propagate_rank (rank, op);
499 if (dump_file && (dump_flags & TDF_DETAILS))
501 fprintf (dump_file, "Rank for ");
502 print_generic_expr (dump_file, e, 0);
503 fprintf (dump_file, " is %ld\n", (rank + 1));
506 /* Note the rank in the hashtable so we don't recompute it. */
507 insert_operand_rank (e, (rank + 1));
508 return (rank + 1);
511 /* Globals, etc, are rank 0 */
512 return 0;
516 /* We want integer ones to end up last no matter what, since they are
517 the ones we can do the most with. */
518 #define INTEGER_CONST_TYPE 1 << 3
519 #define FLOAT_CONST_TYPE 1 << 2
520 #define OTHER_CONST_TYPE 1 << 1
522 /* Classify an invariant tree into integer, float, or other, so that
523 we can sort them to be near other constants of the same type. */
524 static inline int
525 constant_type (tree t)
527 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
528 return INTEGER_CONST_TYPE;
529 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t)))
530 return FLOAT_CONST_TYPE;
531 else
532 return OTHER_CONST_TYPE;
535 /* qsort comparison function to sort operand entries PA and PB by rank
536 so that the sorted array is ordered by rank in decreasing order. */
537 static int
538 sort_by_operand_rank (const void *pa, const void *pb)
540 const operand_entry_t oea = *(const operand_entry_t *)pa;
541 const operand_entry_t oeb = *(const operand_entry_t *)pb;
543 /* It's nicer for optimize_expression if constants that are likely
544 to fold when added/multiplied//whatever are put next to each
545 other. Since all constants have rank 0, order them by type. */
546 if (oeb->rank == 0 && oea->rank == 0)
548 if (constant_type (oeb->op) != constant_type (oea->op))
549 return constant_type (oeb->op) - constant_type (oea->op);
550 else
551 /* To make sorting result stable, we use unique IDs to determine
552 order. */
553 return oeb->id - oea->id;
556 /* Lastly, make sure the versions that are the same go next to each
557 other. */
558 if ((oeb->rank - oea->rank == 0)
559 && TREE_CODE (oea->op) == SSA_NAME
560 && TREE_CODE (oeb->op) == SSA_NAME)
562 /* As SSA_NAME_VERSION is assigned pretty randomly, because we reuse
563 versions of removed SSA_NAMEs, so if possible, prefer to sort
564 based on basic block and gimple_uid of the SSA_NAME_DEF_STMT.
565 See PR60418. */
566 if (!SSA_NAME_IS_DEFAULT_DEF (oea->op)
567 && !SSA_NAME_IS_DEFAULT_DEF (oeb->op)
568 && SSA_NAME_VERSION (oeb->op) != SSA_NAME_VERSION (oea->op))
570 gimple stmta = SSA_NAME_DEF_STMT (oea->op);
571 gimple stmtb = SSA_NAME_DEF_STMT (oeb->op);
572 basic_block bba = gimple_bb (stmta);
573 basic_block bbb = gimple_bb (stmtb);
574 if (bbb != bba)
576 if (bb_rank[bbb->index] != bb_rank[bba->index])
577 return bb_rank[bbb->index] - bb_rank[bba->index];
579 else
581 bool da = reassoc_stmt_dominates_stmt_p (stmta, stmtb);
582 bool db = reassoc_stmt_dominates_stmt_p (stmtb, stmta);
583 if (da != db)
584 return da ? 1 : -1;
588 if (SSA_NAME_VERSION (oeb->op) != SSA_NAME_VERSION (oea->op))
589 return SSA_NAME_VERSION (oeb->op) - SSA_NAME_VERSION (oea->op);
590 else
591 return oeb->id - oea->id;
594 if (oeb->rank != oea->rank)
595 return oeb->rank - oea->rank;
596 else
597 return oeb->id - oea->id;
600 /* Add an operand entry to *OPS for the tree operand OP. */
602 static void
603 add_to_ops_vec (vec<operand_entry_t> *ops, tree op)
605 operand_entry_t oe = (operand_entry_t) pool_alloc (operand_entry_pool);
607 oe->op = op;
608 oe->rank = get_rank (op);
609 oe->id = next_operand_entry_id++;
610 oe->count = 1;
611 ops->safe_push (oe);
614 /* Add an operand entry to *OPS for the tree operand OP with repeat
615 count REPEAT. */
617 static void
618 add_repeat_to_ops_vec (vec<operand_entry_t> *ops, tree op,
619 HOST_WIDE_INT repeat)
621 operand_entry_t oe = (operand_entry_t) pool_alloc (operand_entry_pool);
623 oe->op = op;
624 oe->rank = get_rank (op);
625 oe->id = next_operand_entry_id++;
626 oe->count = repeat;
627 ops->safe_push (oe);
629 reassociate_stats.pows_encountered++;
632 /* Return true if STMT is reassociable operation containing a binary
633 operation with tree code CODE, and is inside LOOP. */
635 static bool
636 is_reassociable_op (gimple stmt, enum tree_code code, struct loop *loop)
638 basic_block bb = gimple_bb (stmt);
640 if (gimple_bb (stmt) == NULL)
641 return false;
643 if (!flow_bb_inside_loop_p (loop, bb))
644 return false;
646 if (is_gimple_assign (stmt)
647 && gimple_assign_rhs_code (stmt) == code
648 && has_single_use (gimple_assign_lhs (stmt)))
649 return true;
651 return false;
655 /* Given NAME, if NAME is defined by a unary operation OPCODE, return the
656 operand of the negate operation. Otherwise, return NULL. */
658 static tree
659 get_unary_op (tree name, enum tree_code opcode)
661 gimple stmt = SSA_NAME_DEF_STMT (name);
663 if (!is_gimple_assign (stmt))
664 return NULL_TREE;
666 if (gimple_assign_rhs_code (stmt) == opcode)
667 return gimple_assign_rhs1 (stmt);
668 return NULL_TREE;
671 /* If CURR and LAST are a pair of ops that OPCODE allows us to
672 eliminate through equivalences, do so, remove them from OPS, and
673 return true. Otherwise, return false. */
675 static bool
676 eliminate_duplicate_pair (enum tree_code opcode,
677 vec<operand_entry_t> *ops,
678 bool *all_done,
679 unsigned int i,
680 operand_entry_t curr,
681 operand_entry_t last)
684 /* If we have two of the same op, and the opcode is & |, min, or max,
685 we can eliminate one of them.
686 If we have two of the same op, and the opcode is ^, we can
687 eliminate both of them. */
689 if (last && last->op == curr->op)
691 switch (opcode)
693 case MAX_EXPR:
694 case MIN_EXPR:
695 case BIT_IOR_EXPR:
696 case BIT_AND_EXPR:
697 if (dump_file && (dump_flags & TDF_DETAILS))
699 fprintf (dump_file, "Equivalence: ");
700 print_generic_expr (dump_file, curr->op, 0);
701 fprintf (dump_file, " [&|minmax] ");
702 print_generic_expr (dump_file, last->op, 0);
703 fprintf (dump_file, " -> ");
704 print_generic_stmt (dump_file, last->op, 0);
707 ops->ordered_remove (i);
708 reassociate_stats.ops_eliminated ++;
710 return true;
712 case BIT_XOR_EXPR:
713 if (dump_file && (dump_flags & TDF_DETAILS))
715 fprintf (dump_file, "Equivalence: ");
716 print_generic_expr (dump_file, curr->op, 0);
717 fprintf (dump_file, " ^ ");
718 print_generic_expr (dump_file, last->op, 0);
719 fprintf (dump_file, " -> nothing\n");
722 reassociate_stats.ops_eliminated += 2;
724 if (ops->length () == 2)
726 ops->create (0);
727 add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (last->op)));
728 *all_done = true;
730 else
732 ops->ordered_remove (i-1);
733 ops->ordered_remove (i-1);
736 return true;
738 default:
739 break;
742 return false;
745 static vec<tree> plus_negates;
747 /* If OPCODE is PLUS_EXPR, CURR->OP is a negate expression or a bitwise not
748 expression, look in OPS for a corresponding positive operation to cancel
749 it out. If we find one, remove the other from OPS, replace
750 OPS[CURRINDEX] with 0 or -1, respectively, and return true. Otherwise,
751 return false. */
753 static bool
754 eliminate_plus_minus_pair (enum tree_code opcode,
755 vec<operand_entry_t> *ops,
756 unsigned int currindex,
757 operand_entry_t curr)
759 tree negateop;
760 tree notop;
761 unsigned int i;
762 operand_entry_t oe;
764 if (opcode != PLUS_EXPR || TREE_CODE (curr->op) != SSA_NAME)
765 return false;
767 negateop = get_unary_op (curr->op, NEGATE_EXPR);
768 notop = get_unary_op (curr->op, BIT_NOT_EXPR);
769 if (negateop == NULL_TREE && notop == NULL_TREE)
770 return false;
772 /* Any non-negated version will have a rank that is one less than
773 the current rank. So once we hit those ranks, if we don't find
774 one, we can stop. */
776 for (i = currindex + 1;
777 ops->iterate (i, &oe)
778 && oe->rank >= curr->rank - 1 ;
779 i++)
781 if (oe->op == negateop)
784 if (dump_file && (dump_flags & TDF_DETAILS))
786 fprintf (dump_file, "Equivalence: ");
787 print_generic_expr (dump_file, negateop, 0);
788 fprintf (dump_file, " + -");
789 print_generic_expr (dump_file, oe->op, 0);
790 fprintf (dump_file, " -> 0\n");
793 ops->ordered_remove (i);
794 add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (oe->op)));
795 ops->ordered_remove (currindex);
796 reassociate_stats.ops_eliminated ++;
798 return true;
800 else if (oe->op == notop)
802 tree op_type = TREE_TYPE (oe->op);
804 if (dump_file && (dump_flags & TDF_DETAILS))
806 fprintf (dump_file, "Equivalence: ");
807 print_generic_expr (dump_file, notop, 0);
808 fprintf (dump_file, " + ~");
809 print_generic_expr (dump_file, oe->op, 0);
810 fprintf (dump_file, " -> -1\n");
813 ops->ordered_remove (i);
814 add_to_ops_vec (ops, build_int_cst_type (op_type, -1));
815 ops->ordered_remove (currindex);
816 reassociate_stats.ops_eliminated ++;
818 return true;
822 /* CURR->OP is a negate expr in a plus expr: save it for later
823 inspection in repropagate_negates(). */
824 if (negateop != NULL_TREE)
825 plus_negates.safe_push (curr->op);
827 return false;
830 /* If OPCODE is BIT_IOR_EXPR, BIT_AND_EXPR, and, CURR->OP is really a
831 bitwise not expression, look in OPS for a corresponding operand to
832 cancel it out. If we find one, remove the other from OPS, replace
833 OPS[CURRINDEX] with 0, and return true. Otherwise, return
834 false. */
836 static bool
837 eliminate_not_pairs (enum tree_code opcode,
838 vec<operand_entry_t> *ops,
839 unsigned int currindex,
840 operand_entry_t curr)
842 tree notop;
843 unsigned int i;
844 operand_entry_t oe;
846 if ((opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
847 || TREE_CODE (curr->op) != SSA_NAME)
848 return false;
850 notop = get_unary_op (curr->op, BIT_NOT_EXPR);
851 if (notop == NULL_TREE)
852 return false;
854 /* Any non-not version will have a rank that is one less than
855 the current rank. So once we hit those ranks, if we don't find
856 one, we can stop. */
858 for (i = currindex + 1;
859 ops->iterate (i, &oe)
860 && oe->rank >= curr->rank - 1;
861 i++)
863 if (oe->op == notop)
865 if (dump_file && (dump_flags & TDF_DETAILS))
867 fprintf (dump_file, "Equivalence: ");
868 print_generic_expr (dump_file, notop, 0);
869 if (opcode == BIT_AND_EXPR)
870 fprintf (dump_file, " & ~");
871 else if (opcode == BIT_IOR_EXPR)
872 fprintf (dump_file, " | ~");
873 print_generic_expr (dump_file, oe->op, 0);
874 if (opcode == BIT_AND_EXPR)
875 fprintf (dump_file, " -> 0\n");
876 else if (opcode == BIT_IOR_EXPR)
877 fprintf (dump_file, " -> -1\n");
880 if (opcode == BIT_AND_EXPR)
881 oe->op = build_zero_cst (TREE_TYPE (oe->op));
882 else if (opcode == BIT_IOR_EXPR)
883 oe->op = build_all_ones_cst (TREE_TYPE (oe->op));
885 reassociate_stats.ops_eliminated += ops->length () - 1;
886 ops->truncate (0);
887 ops->quick_push (oe);
888 return true;
892 return false;
895 /* Use constant value that may be present in OPS to try to eliminate
896 operands. Note that this function is only really used when we've
897 eliminated ops for other reasons, or merged constants. Across
898 single statements, fold already does all of this, plus more. There
899 is little point in duplicating logic, so I've only included the
900 identities that I could ever construct testcases to trigger. */
902 static void
903 eliminate_using_constants (enum tree_code opcode,
904 vec<operand_entry_t> *ops)
906 operand_entry_t oelast = ops->last ();
907 tree type = TREE_TYPE (oelast->op);
909 if (oelast->rank == 0
910 && (INTEGRAL_TYPE_P (type) || FLOAT_TYPE_P (type)))
912 switch (opcode)
914 case BIT_AND_EXPR:
915 if (integer_zerop (oelast->op))
917 if (ops->length () != 1)
919 if (dump_file && (dump_flags & TDF_DETAILS))
920 fprintf (dump_file, "Found & 0, removing all other ops\n");
922 reassociate_stats.ops_eliminated += ops->length () - 1;
924 ops->truncate (0);
925 ops->quick_push (oelast);
926 return;
929 else if (integer_all_onesp (oelast->op))
931 if (ops->length () != 1)
933 if (dump_file && (dump_flags & TDF_DETAILS))
934 fprintf (dump_file, "Found & -1, removing\n");
935 ops->pop ();
936 reassociate_stats.ops_eliminated++;
939 break;
940 case BIT_IOR_EXPR:
941 if (integer_all_onesp (oelast->op))
943 if (ops->length () != 1)
945 if (dump_file && (dump_flags & TDF_DETAILS))
946 fprintf (dump_file, "Found | -1, removing all other ops\n");
948 reassociate_stats.ops_eliminated += ops->length () - 1;
950 ops->truncate (0);
951 ops->quick_push (oelast);
952 return;
955 else if (integer_zerop (oelast->op))
957 if (ops->length () != 1)
959 if (dump_file && (dump_flags & TDF_DETAILS))
960 fprintf (dump_file, "Found | 0, removing\n");
961 ops->pop ();
962 reassociate_stats.ops_eliminated++;
965 break;
966 case MULT_EXPR:
967 if (integer_zerop (oelast->op)
968 || (FLOAT_TYPE_P (type)
969 && !HONOR_NANS (TYPE_MODE (type))
970 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
971 && real_zerop (oelast->op)))
973 if (ops->length () != 1)
975 if (dump_file && (dump_flags & TDF_DETAILS))
976 fprintf (dump_file, "Found * 0, removing all other ops\n");
978 reassociate_stats.ops_eliminated += ops->length () - 1;
979 ops->truncate (1);
980 ops->quick_push (oelast);
981 return;
984 else if (integer_onep (oelast->op)
985 || (FLOAT_TYPE_P (type)
986 && !HONOR_SNANS (TYPE_MODE (type))
987 && real_onep (oelast->op)))
989 if (ops->length () != 1)
991 if (dump_file && (dump_flags & TDF_DETAILS))
992 fprintf (dump_file, "Found * 1, removing\n");
993 ops->pop ();
994 reassociate_stats.ops_eliminated++;
995 return;
998 break;
999 case BIT_XOR_EXPR:
1000 case PLUS_EXPR:
1001 case MINUS_EXPR:
1002 if (integer_zerop (oelast->op)
1003 || (FLOAT_TYPE_P (type)
1004 && (opcode == PLUS_EXPR || opcode == MINUS_EXPR)
1005 && fold_real_zero_addition_p (type, oelast->op,
1006 opcode == MINUS_EXPR)))
1008 if (ops->length () != 1)
1010 if (dump_file && (dump_flags & TDF_DETAILS))
1011 fprintf (dump_file, "Found [|^+] 0, removing\n");
1012 ops->pop ();
1013 reassociate_stats.ops_eliminated++;
1014 return;
1017 break;
1018 default:
1019 break;
1025 static void linearize_expr_tree (vec<operand_entry_t> *, gimple,
1026 bool, bool);
1028 /* Structure for tracking and counting operands. */
1029 typedef struct oecount_s {
1030 int cnt;
1031 int id;
1032 enum tree_code oecode;
1033 tree op;
1034 } oecount;
1037 /* The heap for the oecount hashtable and the sorted list of operands. */
1038 static vec<oecount> cvec;
1041 /* Oecount hashtable helpers. */
1043 struct oecount_hasher
1045 typedef int value_type;
1046 typedef int compare_type;
1047 typedef int store_values_directly;
1048 static inline hashval_t hash (const value_type &);
1049 static inline bool equal (const value_type &, const compare_type &);
1050 static bool is_deleted (int &v) { return v == 1; }
1051 static void mark_deleted (int &e) { e = 1; }
1052 static bool is_empty (int &v) { return v == 0; }
1053 static void mark_empty (int &e) { e = 0; }
1054 static void remove (int &) {}
1057 /* Hash function for oecount. */
1059 inline hashval_t
1060 oecount_hasher::hash (const value_type &p)
1062 const oecount *c = &cvec[p - 42];
1063 return htab_hash_pointer (c->op) ^ (hashval_t)c->oecode;
1066 /* Comparison function for oecount. */
1068 inline bool
1069 oecount_hasher::equal (const value_type &p1, const compare_type &p2)
1071 const oecount *c1 = &cvec[p1 - 42];
1072 const oecount *c2 = &cvec[p2 - 42];
1073 return (c1->oecode == c2->oecode
1074 && c1->op == c2->op);
1077 /* Comparison function for qsort sorting oecount elements by count. */
1079 static int
1080 oecount_cmp (const void *p1, const void *p2)
1082 const oecount *c1 = (const oecount *)p1;
1083 const oecount *c2 = (const oecount *)p2;
1084 if (c1->cnt != c2->cnt)
1085 return c1->cnt - c2->cnt;
1086 else
1087 /* If counts are identical, use unique IDs to stabilize qsort. */
1088 return c1->id - c2->id;
1091 /* Return TRUE iff STMT represents a builtin call that raises OP
1092 to some exponent. */
1094 static bool
1095 stmt_is_power_of_op (gimple stmt, tree op)
1097 tree fndecl;
1099 if (!is_gimple_call (stmt))
1100 return false;
1102 fndecl = gimple_call_fndecl (stmt);
1104 if (!fndecl
1105 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
1106 return false;
1108 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
1110 CASE_FLT_FN (BUILT_IN_POW):
1111 CASE_FLT_FN (BUILT_IN_POWI):
1112 return (operand_equal_p (gimple_call_arg (stmt, 0), op, 0));
1114 default:
1115 return false;
1119 /* Given STMT which is a __builtin_pow* call, decrement its exponent
1120 in place and return the result. Assumes that stmt_is_power_of_op
1121 was previously called for STMT and returned TRUE. */
1123 static HOST_WIDE_INT
1124 decrement_power (gimple stmt)
1126 REAL_VALUE_TYPE c, cint;
1127 HOST_WIDE_INT power;
1128 tree arg1;
1130 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
1132 CASE_FLT_FN (BUILT_IN_POW):
1133 arg1 = gimple_call_arg (stmt, 1);
1134 c = TREE_REAL_CST (arg1);
1135 power = real_to_integer (&c) - 1;
1136 real_from_integer (&cint, VOIDmode, power, SIGNED);
1137 gimple_call_set_arg (stmt, 1, build_real (TREE_TYPE (arg1), cint));
1138 return power;
1140 CASE_FLT_FN (BUILT_IN_POWI):
1141 arg1 = gimple_call_arg (stmt, 1);
1142 power = TREE_INT_CST_LOW (arg1) - 1;
1143 gimple_call_set_arg (stmt, 1, build_int_cst (TREE_TYPE (arg1), power));
1144 return power;
1146 default:
1147 gcc_unreachable ();
1151 /* Find the single immediate use of STMT's LHS, and replace it
1152 with OP. Remove STMT. If STMT's LHS is the same as *DEF,
1153 replace *DEF with OP as well. */
1155 static void
1156 propagate_op_to_single_use (tree op, gimple stmt, tree *def)
1158 tree lhs;
1159 gimple use_stmt;
1160 use_operand_p use;
1161 gimple_stmt_iterator gsi;
1163 if (is_gimple_call (stmt))
1164 lhs = gimple_call_lhs (stmt);
1165 else
1166 lhs = gimple_assign_lhs (stmt);
1168 gcc_assert (has_single_use (lhs));
1169 single_imm_use (lhs, &use, &use_stmt);
1170 if (lhs == *def)
1171 *def = op;
1172 SET_USE (use, op);
1173 if (TREE_CODE (op) != SSA_NAME)
1174 update_stmt (use_stmt);
1175 gsi = gsi_for_stmt (stmt);
1176 unlink_stmt_vdef (stmt);
1177 reassoc_remove_stmt (&gsi);
1178 release_defs (stmt);
1181 /* Walks the linear chain with result *DEF searching for an operation
1182 with operand OP and code OPCODE removing that from the chain. *DEF
1183 is updated if there is only one operand but no operation left. */
1185 static void
1186 zero_one_operation (tree *def, enum tree_code opcode, tree op)
1188 gimple stmt = SSA_NAME_DEF_STMT (*def);
1192 tree name;
1194 if (opcode == MULT_EXPR
1195 && stmt_is_power_of_op (stmt, op))
1197 if (decrement_power (stmt) == 1)
1198 propagate_op_to_single_use (op, stmt, def);
1199 return;
1202 name = gimple_assign_rhs1 (stmt);
1204 /* If this is the operation we look for and one of the operands
1205 is ours simply propagate the other operand into the stmts
1206 single use. */
1207 if (gimple_assign_rhs_code (stmt) == opcode
1208 && (name == op
1209 || gimple_assign_rhs2 (stmt) == op))
1211 if (name == op)
1212 name = gimple_assign_rhs2 (stmt);
1213 propagate_op_to_single_use (name, stmt, def);
1214 return;
1217 /* We might have a multiply of two __builtin_pow* calls, and
1218 the operand might be hiding in the rightmost one. */
1219 if (opcode == MULT_EXPR
1220 && gimple_assign_rhs_code (stmt) == opcode
1221 && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
1222 && has_single_use (gimple_assign_rhs2 (stmt)))
1224 gimple stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
1225 if (stmt_is_power_of_op (stmt2, op))
1227 if (decrement_power (stmt2) == 1)
1228 propagate_op_to_single_use (op, stmt2, def);
1229 return;
1233 /* Continue walking the chain. */
1234 gcc_assert (name != op
1235 && TREE_CODE (name) == SSA_NAME);
1236 stmt = SSA_NAME_DEF_STMT (name);
1238 while (1);
1241 /* Returns true if statement S1 dominates statement S2. Like
1242 stmt_dominates_stmt_p, but uses stmt UIDs to optimize. */
1244 static bool
1245 reassoc_stmt_dominates_stmt_p (gimple s1, gimple s2)
1247 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
1249 /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
1250 SSA_NAME. Assume it lives at the beginning of function and
1251 thus dominates everything. */
1252 if (!bb1 || s1 == s2)
1253 return true;
1255 /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
1256 if (!bb2)
1257 return false;
1259 if (bb1 == bb2)
1261 /* PHIs in the same basic block are assumed to be
1262 executed all in parallel, if only one stmt is a PHI,
1263 it dominates the other stmt in the same basic block. */
1264 if (gimple_code (s1) == GIMPLE_PHI)
1265 return true;
1267 if (gimple_code (s2) == GIMPLE_PHI)
1268 return false;
1270 gcc_assert (gimple_uid (s1) && gimple_uid (s2));
1272 if (gimple_uid (s1) < gimple_uid (s2))
1273 return true;
1275 if (gimple_uid (s1) > gimple_uid (s2))
1276 return false;
1278 gimple_stmt_iterator gsi = gsi_for_stmt (s1);
1279 unsigned int uid = gimple_uid (s1);
1280 for (gsi_next (&gsi); !gsi_end_p (gsi); gsi_next (&gsi))
1282 gimple s = gsi_stmt (gsi);
1283 if (gimple_uid (s) != uid)
1284 break;
1285 if (s == s2)
1286 return true;
1289 return false;
1292 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
1295 /* Insert STMT after INSERT_POINT. */
1297 static void
1298 insert_stmt_after (gimple stmt, gimple insert_point)
1300 gimple_stmt_iterator gsi;
1301 basic_block bb;
1303 if (gimple_code (insert_point) == GIMPLE_PHI)
1304 bb = gimple_bb (insert_point);
1305 else if (!stmt_ends_bb_p (insert_point))
1307 gsi = gsi_for_stmt (insert_point);
1308 gimple_set_uid (stmt, gimple_uid (insert_point));
1309 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1310 return;
1312 else
1313 /* We assume INSERT_POINT is a SSA_NAME_DEF_STMT of some SSA_NAME,
1314 thus if it must end a basic block, it should be a call that can
1315 throw, or some assignment that can throw. If it throws, the LHS
1316 of it will not be initialized though, so only valid places using
1317 the SSA_NAME should be dominated by the fallthru edge. */
1318 bb = find_fallthru_edge (gimple_bb (insert_point)->succs)->dest;
1319 gsi = gsi_after_labels (bb);
1320 if (gsi_end_p (gsi))
1322 gimple_stmt_iterator gsi2 = gsi_last_bb (bb);
1323 gimple_set_uid (stmt,
1324 gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
1326 else
1327 gimple_set_uid (stmt, gimple_uid (gsi_stmt (gsi)));
1328 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1331 /* Builds one statement performing OP1 OPCODE OP2 using TMPVAR for
1332 the result. Places the statement after the definition of either
1333 OP1 or OP2. Returns the new statement. */
1335 static gimple
1336 build_and_add_sum (tree type, tree op1, tree op2, enum tree_code opcode)
1338 gimple op1def = NULL, op2def = NULL;
1339 gimple_stmt_iterator gsi;
1340 tree op;
1341 gassign *sum;
1343 /* Create the addition statement. */
1344 op = make_ssa_name (type, NULL);
1345 sum = gimple_build_assign_with_ops (opcode, op, op1, op2);
1347 /* Find an insertion place and insert. */
1348 if (TREE_CODE (op1) == SSA_NAME)
1349 op1def = SSA_NAME_DEF_STMT (op1);
1350 if (TREE_CODE (op2) == SSA_NAME)
1351 op2def = SSA_NAME_DEF_STMT (op2);
1352 if ((!op1def || gimple_nop_p (op1def))
1353 && (!op2def || gimple_nop_p (op2def)))
1355 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1356 if (gsi_end_p (gsi))
1358 gimple_stmt_iterator gsi2
1359 = gsi_last_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1360 gimple_set_uid (sum,
1361 gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
1363 else
1364 gimple_set_uid (sum, gimple_uid (gsi_stmt (gsi)));
1365 gsi_insert_before (&gsi, sum, GSI_NEW_STMT);
1367 else
1369 gimple insert_point;
1370 if ((!op1def || gimple_nop_p (op1def))
1371 || (op2def && !gimple_nop_p (op2def)
1372 && reassoc_stmt_dominates_stmt_p (op1def, op2def)))
1373 insert_point = op2def;
1374 else
1375 insert_point = op1def;
1376 insert_stmt_after (sum, insert_point);
1378 update_stmt (sum);
1380 return sum;
1383 /* Perform un-distribution of divisions and multiplications.
1384 A * X + B * X is transformed into (A + B) * X and A / X + B / X
1385 to (A + B) / X for real X.
1387 The algorithm is organized as follows.
1389 - First we walk the addition chain *OPS looking for summands that
1390 are defined by a multiplication or a real division. This results
1391 in the candidates bitmap with relevant indices into *OPS.
1393 - Second we build the chains of multiplications or divisions for
1394 these candidates, counting the number of occurrences of (operand, code)
1395 pairs in all of the candidates chains.
1397 - Third we sort the (operand, code) pairs by number of occurrence and
1398 process them starting with the pair with the most uses.
1400 * For each such pair we walk the candidates again to build a
1401 second candidate bitmap noting all multiplication/division chains
1402 that have at least one occurrence of (operand, code).
1404 * We build an alternate addition chain only covering these
1405 candidates with one (operand, code) operation removed from their
1406 multiplication/division chain.
1408 * The first candidate gets replaced by the alternate addition chain
1409 multiplied/divided by the operand.
1411 * All candidate chains get disabled for further processing and
1412 processing of (operand, code) pairs continues.
1414 The alternate addition chains built are re-processed by the main
1415 reassociation algorithm which allows optimizing a * x * y + b * y * x
1416 to (a + b ) * x * y in one invocation of the reassociation pass. */
1418 static bool
1419 undistribute_ops_list (enum tree_code opcode,
1420 vec<operand_entry_t> *ops, struct loop *loop)
1422 unsigned int length = ops->length ();
1423 operand_entry_t oe1;
1424 unsigned i, j;
1425 sbitmap candidates, candidates2;
1426 unsigned nr_candidates, nr_candidates2;
1427 sbitmap_iterator sbi0;
1428 vec<operand_entry_t> *subops;
1429 bool changed = false;
1430 int next_oecount_id = 0;
1432 if (length <= 1
1433 || opcode != PLUS_EXPR)
1434 return false;
1436 /* Build a list of candidates to process. */
1437 candidates = sbitmap_alloc (length);
1438 bitmap_clear (candidates);
1439 nr_candidates = 0;
1440 FOR_EACH_VEC_ELT (*ops, i, oe1)
1442 enum tree_code dcode;
1443 gimple oe1def;
1445 if (TREE_CODE (oe1->op) != SSA_NAME)
1446 continue;
1447 oe1def = SSA_NAME_DEF_STMT (oe1->op);
1448 if (!is_gimple_assign (oe1def))
1449 continue;
1450 dcode = gimple_assign_rhs_code (oe1def);
1451 if ((dcode != MULT_EXPR
1452 && dcode != RDIV_EXPR)
1453 || !is_reassociable_op (oe1def, dcode, loop))
1454 continue;
1456 bitmap_set_bit (candidates, i);
1457 nr_candidates++;
1460 if (nr_candidates < 2)
1462 sbitmap_free (candidates);
1463 return false;
1466 if (dump_file && (dump_flags & TDF_DETAILS))
1468 fprintf (dump_file, "searching for un-distribute opportunities ");
1469 print_generic_expr (dump_file,
1470 (*ops)[bitmap_first_set_bit (candidates)]->op, 0);
1471 fprintf (dump_file, " %d\n", nr_candidates);
1474 /* Build linearized sub-operand lists and the counting table. */
1475 cvec.create (0);
1477 hash_table<oecount_hasher> ctable (15);
1479 /* ??? Macro arguments cannot have multi-argument template types in
1480 them. This typedef is needed to workaround that limitation. */
1481 typedef vec<operand_entry_t> vec_operand_entry_t_heap;
1482 subops = XCNEWVEC (vec_operand_entry_t_heap, ops->length ());
1483 EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
1485 gimple oedef;
1486 enum tree_code oecode;
1487 unsigned j;
1489 oedef = SSA_NAME_DEF_STMT ((*ops)[i]->op);
1490 oecode = gimple_assign_rhs_code (oedef);
1491 linearize_expr_tree (&subops[i], oedef,
1492 associative_tree_code (oecode), false);
1494 FOR_EACH_VEC_ELT (subops[i], j, oe1)
1496 oecount c;
1497 int *slot;
1498 int idx;
1499 c.oecode = oecode;
1500 c.cnt = 1;
1501 c.id = next_oecount_id++;
1502 c.op = oe1->op;
1503 cvec.safe_push (c);
1504 idx = cvec.length () + 41;
1505 slot = ctable.find_slot (idx, INSERT);
1506 if (!*slot)
1508 *slot = idx;
1510 else
1512 cvec.pop ();
1513 cvec[*slot - 42].cnt++;
1518 /* Sort the counting table. */
1519 cvec.qsort (oecount_cmp);
1521 if (dump_file && (dump_flags & TDF_DETAILS))
1523 oecount *c;
1524 fprintf (dump_file, "Candidates:\n");
1525 FOR_EACH_VEC_ELT (cvec, j, c)
1527 fprintf (dump_file, " %u %s: ", c->cnt,
1528 c->oecode == MULT_EXPR
1529 ? "*" : c->oecode == RDIV_EXPR ? "/" : "?");
1530 print_generic_expr (dump_file, c->op, 0);
1531 fprintf (dump_file, "\n");
1535 /* Process the (operand, code) pairs in order of most occurrence. */
1536 candidates2 = sbitmap_alloc (length);
1537 while (!cvec.is_empty ())
1539 oecount *c = &cvec.last ();
1540 if (c->cnt < 2)
1541 break;
1543 /* Now collect the operands in the outer chain that contain
1544 the common operand in their inner chain. */
1545 bitmap_clear (candidates2);
1546 nr_candidates2 = 0;
1547 EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
1549 gimple oedef;
1550 enum tree_code oecode;
1551 unsigned j;
1552 tree op = (*ops)[i]->op;
1554 /* If we undistributed in this chain already this may be
1555 a constant. */
1556 if (TREE_CODE (op) != SSA_NAME)
1557 continue;
1559 oedef = SSA_NAME_DEF_STMT (op);
1560 oecode = gimple_assign_rhs_code (oedef);
1561 if (oecode != c->oecode)
1562 continue;
1564 FOR_EACH_VEC_ELT (subops[i], j, oe1)
1566 if (oe1->op == c->op)
1568 bitmap_set_bit (candidates2, i);
1569 ++nr_candidates2;
1570 break;
1575 if (nr_candidates2 >= 2)
1577 operand_entry_t oe1, oe2;
1578 gimple prod;
1579 int first = bitmap_first_set_bit (candidates2);
1581 /* Build the new addition chain. */
1582 oe1 = (*ops)[first];
1583 if (dump_file && (dump_flags & TDF_DETAILS))
1585 fprintf (dump_file, "Building (");
1586 print_generic_expr (dump_file, oe1->op, 0);
1588 zero_one_operation (&oe1->op, c->oecode, c->op);
1589 EXECUTE_IF_SET_IN_BITMAP (candidates2, first+1, i, sbi0)
1591 gimple sum;
1592 oe2 = (*ops)[i];
1593 if (dump_file && (dump_flags & TDF_DETAILS))
1595 fprintf (dump_file, " + ");
1596 print_generic_expr (dump_file, oe2->op, 0);
1598 zero_one_operation (&oe2->op, c->oecode, c->op);
1599 sum = build_and_add_sum (TREE_TYPE (oe1->op),
1600 oe1->op, oe2->op, opcode);
1601 oe2->op = build_zero_cst (TREE_TYPE (oe2->op));
1602 oe2->rank = 0;
1603 oe1->op = gimple_get_lhs (sum);
1606 /* Apply the multiplication/division. */
1607 prod = build_and_add_sum (TREE_TYPE (oe1->op),
1608 oe1->op, c->op, c->oecode);
1609 if (dump_file && (dump_flags & TDF_DETAILS))
1611 fprintf (dump_file, ") %s ", c->oecode == MULT_EXPR ? "*" : "/");
1612 print_generic_expr (dump_file, c->op, 0);
1613 fprintf (dump_file, "\n");
1616 /* Record it in the addition chain and disable further
1617 undistribution with this op. */
1618 oe1->op = gimple_assign_lhs (prod);
1619 oe1->rank = get_rank (oe1->op);
1620 subops[first].release ();
1622 changed = true;
1625 cvec.pop ();
1628 for (i = 0; i < ops->length (); ++i)
1629 subops[i].release ();
1630 free (subops);
1631 cvec.release ();
1632 sbitmap_free (candidates);
1633 sbitmap_free (candidates2);
1635 return changed;
1638 /* If OPCODE is BIT_IOR_EXPR or BIT_AND_EXPR and CURR is a comparison
1639 expression, examine the other OPS to see if any of them are comparisons
1640 of the same values, which we may be able to combine or eliminate.
1641 For example, we can rewrite (a < b) | (a == b) as (a <= b). */
1643 static bool
1644 eliminate_redundant_comparison (enum tree_code opcode,
1645 vec<operand_entry_t> *ops,
1646 unsigned int currindex,
1647 operand_entry_t curr)
1649 tree op1, op2;
1650 enum tree_code lcode, rcode;
1651 gimple def1, def2;
1652 int i;
1653 operand_entry_t oe;
1655 if (opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
1656 return false;
1658 /* Check that CURR is a comparison. */
1659 if (TREE_CODE (curr->op) != SSA_NAME)
1660 return false;
1661 def1 = SSA_NAME_DEF_STMT (curr->op);
1662 if (!is_gimple_assign (def1))
1663 return false;
1664 lcode = gimple_assign_rhs_code (def1);
1665 if (TREE_CODE_CLASS (lcode) != tcc_comparison)
1666 return false;
1667 op1 = gimple_assign_rhs1 (def1);
1668 op2 = gimple_assign_rhs2 (def1);
1670 /* Now look for a similar comparison in the remaining OPS. */
1671 for (i = currindex + 1; ops->iterate (i, &oe); i++)
1673 tree t;
1675 if (TREE_CODE (oe->op) != SSA_NAME)
1676 continue;
1677 def2 = SSA_NAME_DEF_STMT (oe->op);
1678 if (!is_gimple_assign (def2))
1679 continue;
1680 rcode = gimple_assign_rhs_code (def2);
1681 if (TREE_CODE_CLASS (rcode) != tcc_comparison)
1682 continue;
1684 /* If we got here, we have a match. See if we can combine the
1685 two comparisons. */
1686 if (opcode == BIT_IOR_EXPR)
1687 t = maybe_fold_or_comparisons (lcode, op1, op2,
1688 rcode, gimple_assign_rhs1 (def2),
1689 gimple_assign_rhs2 (def2));
1690 else
1691 t = maybe_fold_and_comparisons (lcode, op1, op2,
1692 rcode, gimple_assign_rhs1 (def2),
1693 gimple_assign_rhs2 (def2));
1694 if (!t)
1695 continue;
1697 /* maybe_fold_and_comparisons and maybe_fold_or_comparisons
1698 always give us a boolean_type_node value back. If the original
1699 BIT_AND_EXPR or BIT_IOR_EXPR was of a wider integer type,
1700 we need to convert. */
1701 if (!useless_type_conversion_p (TREE_TYPE (curr->op), TREE_TYPE (t)))
1702 t = fold_convert (TREE_TYPE (curr->op), t);
1704 if (TREE_CODE (t) != INTEGER_CST
1705 && !operand_equal_p (t, curr->op, 0))
1707 enum tree_code subcode;
1708 tree newop1, newop2;
1709 if (!COMPARISON_CLASS_P (t))
1710 continue;
1711 extract_ops_from_tree (t, &subcode, &newop1, &newop2);
1712 STRIP_USELESS_TYPE_CONVERSION (newop1);
1713 STRIP_USELESS_TYPE_CONVERSION (newop2);
1714 if (!is_gimple_val (newop1) || !is_gimple_val (newop2))
1715 continue;
1718 if (dump_file && (dump_flags & TDF_DETAILS))
1720 fprintf (dump_file, "Equivalence: ");
1721 print_generic_expr (dump_file, curr->op, 0);
1722 fprintf (dump_file, " %s ", op_symbol_code (opcode));
1723 print_generic_expr (dump_file, oe->op, 0);
1724 fprintf (dump_file, " -> ");
1725 print_generic_expr (dump_file, t, 0);
1726 fprintf (dump_file, "\n");
1729 /* Now we can delete oe, as it has been subsumed by the new combined
1730 expression t. */
1731 ops->ordered_remove (i);
1732 reassociate_stats.ops_eliminated ++;
1734 /* If t is the same as curr->op, we're done. Otherwise we must
1735 replace curr->op with t. Special case is if we got a constant
1736 back, in which case we add it to the end instead of in place of
1737 the current entry. */
1738 if (TREE_CODE (t) == INTEGER_CST)
1740 ops->ordered_remove (currindex);
1741 add_to_ops_vec (ops, t);
1743 else if (!operand_equal_p (t, curr->op, 0))
1745 gimple sum;
1746 enum tree_code subcode;
1747 tree newop1;
1748 tree newop2;
1749 gcc_assert (COMPARISON_CLASS_P (t));
1750 extract_ops_from_tree (t, &subcode, &newop1, &newop2);
1751 STRIP_USELESS_TYPE_CONVERSION (newop1);
1752 STRIP_USELESS_TYPE_CONVERSION (newop2);
1753 gcc_checking_assert (is_gimple_val (newop1)
1754 && is_gimple_val (newop2));
1755 sum = build_and_add_sum (TREE_TYPE (t), newop1, newop2, subcode);
1756 curr->op = gimple_get_lhs (sum);
1758 return true;
1761 return false;
1764 /* Perform various identities and other optimizations on the list of
1765 operand entries, stored in OPS. The tree code for the binary
1766 operation between all the operands is OPCODE. */
1768 static void
1769 optimize_ops_list (enum tree_code opcode,
1770 vec<operand_entry_t> *ops)
1772 unsigned int length = ops->length ();
1773 unsigned int i;
1774 operand_entry_t oe;
1775 operand_entry_t oelast = NULL;
1776 bool iterate = false;
1778 if (length == 1)
1779 return;
1781 oelast = ops->last ();
1783 /* If the last two are constants, pop the constants off, merge them
1784 and try the next two. */
1785 if (oelast->rank == 0 && is_gimple_min_invariant (oelast->op))
1787 operand_entry_t oelm1 = (*ops)[length - 2];
1789 if (oelm1->rank == 0
1790 && is_gimple_min_invariant (oelm1->op)
1791 && useless_type_conversion_p (TREE_TYPE (oelm1->op),
1792 TREE_TYPE (oelast->op)))
1794 tree folded = fold_binary (opcode, TREE_TYPE (oelm1->op),
1795 oelm1->op, oelast->op);
1797 if (folded && is_gimple_min_invariant (folded))
1799 if (dump_file && (dump_flags & TDF_DETAILS))
1800 fprintf (dump_file, "Merging constants\n");
1802 ops->pop ();
1803 ops->pop ();
1805 add_to_ops_vec (ops, folded);
1806 reassociate_stats.constants_eliminated++;
1808 optimize_ops_list (opcode, ops);
1809 return;
1814 eliminate_using_constants (opcode, ops);
1815 oelast = NULL;
1817 for (i = 0; ops->iterate (i, &oe);)
1819 bool done = false;
1821 if (eliminate_not_pairs (opcode, ops, i, oe))
1822 return;
1823 if (eliminate_duplicate_pair (opcode, ops, &done, i, oe, oelast)
1824 || (!done && eliminate_plus_minus_pair (opcode, ops, i, oe))
1825 || (!done && eliminate_redundant_comparison (opcode, ops, i, oe)))
1827 if (done)
1828 return;
1829 iterate = true;
1830 oelast = NULL;
1831 continue;
1833 oelast = oe;
1834 i++;
1837 length = ops->length ();
1838 oelast = ops->last ();
1840 if (iterate)
1841 optimize_ops_list (opcode, ops);
1844 /* The following functions are subroutines to optimize_range_tests and allow
1845 it to try to change a logical combination of comparisons into a range
1846 test.
1848 For example, both
1849 X == 2 || X == 5 || X == 3 || X == 4
1851 X >= 2 && X <= 5
1852 are converted to
1853 (unsigned) (X - 2) <= 3
1855 For more information see comments above fold_test_range in fold-const.c,
1856 this implementation is for GIMPLE. */
1858 struct range_entry
1860 tree exp;
1861 tree low;
1862 tree high;
1863 bool in_p;
1864 bool strict_overflow_p;
1865 unsigned int idx, next;
1868 /* This is similar to make_range in fold-const.c, but on top of
1869 GIMPLE instead of trees. If EXP is non-NULL, it should be
1870 an SSA_NAME and STMT argument is ignored, otherwise STMT
1871 argument should be a GIMPLE_COND. */
1873 static void
1874 init_range_entry (struct range_entry *r, tree exp, gimple stmt)
1876 int in_p;
1877 tree low, high;
1878 bool is_bool, strict_overflow_p;
1880 r->exp = NULL_TREE;
1881 r->in_p = false;
1882 r->strict_overflow_p = false;
1883 r->low = NULL_TREE;
1884 r->high = NULL_TREE;
1885 if (exp != NULL_TREE
1886 && (TREE_CODE (exp) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (exp))))
1887 return;
1889 /* Start with simply saying "EXP != 0" and then look at the code of EXP
1890 and see if we can refine the range. Some of the cases below may not
1891 happen, but it doesn't seem worth worrying about this. We "continue"
1892 the outer loop when we've changed something; otherwise we "break"
1893 the switch, which will "break" the while. */
1894 low = exp ? build_int_cst (TREE_TYPE (exp), 0) : boolean_false_node;
1895 high = low;
1896 in_p = 0;
1897 strict_overflow_p = false;
1898 is_bool = false;
1899 if (exp == NULL_TREE)
1900 is_bool = true;
1901 else if (TYPE_PRECISION (TREE_TYPE (exp)) == 1)
1903 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
1904 is_bool = true;
1905 else
1906 return;
1908 else if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE)
1909 is_bool = true;
1911 while (1)
1913 enum tree_code code;
1914 tree arg0, arg1, exp_type;
1915 tree nexp;
1916 location_t loc;
1918 if (exp != NULL_TREE)
1920 if (TREE_CODE (exp) != SSA_NAME
1921 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
1922 break;
1924 stmt = SSA_NAME_DEF_STMT (exp);
1925 if (!is_gimple_assign (stmt))
1926 break;
1928 code = gimple_assign_rhs_code (stmt);
1929 arg0 = gimple_assign_rhs1 (stmt);
1930 arg1 = gimple_assign_rhs2 (stmt);
1931 exp_type = TREE_TYPE (exp);
1933 else
1935 code = gimple_cond_code (stmt);
1936 arg0 = gimple_cond_lhs (stmt);
1937 arg1 = gimple_cond_rhs (stmt);
1938 exp_type = boolean_type_node;
1941 if (TREE_CODE (arg0) != SSA_NAME)
1942 break;
1943 loc = gimple_location (stmt);
1944 switch (code)
1946 case BIT_NOT_EXPR:
1947 if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE
1948 /* Ensure the range is either +[-,0], +[0,0],
1949 -[-,0], -[0,0] or +[1,-], +[1,1], -[1,-] or
1950 -[1,1]. If it is e.g. +[-,-] or -[-,-]
1951 or similar expression of unconditional true or
1952 false, it should not be negated. */
1953 && ((high && integer_zerop (high))
1954 || (low && integer_onep (low))))
1956 in_p = !in_p;
1957 exp = arg0;
1958 continue;
1960 break;
1961 case SSA_NAME:
1962 exp = arg0;
1963 continue;
1964 CASE_CONVERT:
1965 if (is_bool)
1966 goto do_default;
1967 if (TYPE_PRECISION (TREE_TYPE (arg0)) == 1)
1969 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
1970 is_bool = true;
1971 else
1972 return;
1974 else if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE)
1975 is_bool = true;
1976 goto do_default;
1977 case EQ_EXPR:
1978 case NE_EXPR:
1979 case LT_EXPR:
1980 case LE_EXPR:
1981 case GE_EXPR:
1982 case GT_EXPR:
1983 is_bool = true;
1984 /* FALLTHRU */
1985 default:
1986 if (!is_bool)
1987 return;
1988 do_default:
1989 nexp = make_range_step (loc, code, arg0, arg1, exp_type,
1990 &low, &high, &in_p,
1991 &strict_overflow_p);
1992 if (nexp != NULL_TREE)
1994 exp = nexp;
1995 gcc_assert (TREE_CODE (exp) == SSA_NAME);
1996 continue;
1998 break;
2000 break;
2002 if (is_bool)
2004 r->exp = exp;
2005 r->in_p = in_p;
2006 r->low = low;
2007 r->high = high;
2008 r->strict_overflow_p = strict_overflow_p;
2012 /* Comparison function for qsort. Sort entries
2013 without SSA_NAME exp first, then with SSA_NAMEs sorted
2014 by increasing SSA_NAME_VERSION, and for the same SSA_NAMEs
2015 by increasing ->low and if ->low is the same, by increasing
2016 ->high. ->low == NULL_TREE means minimum, ->high == NULL_TREE
2017 maximum. */
2019 static int
2020 range_entry_cmp (const void *a, const void *b)
2022 const struct range_entry *p = (const struct range_entry *) a;
2023 const struct range_entry *q = (const struct range_entry *) b;
2025 if (p->exp != NULL_TREE && TREE_CODE (p->exp) == SSA_NAME)
2027 if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
2029 /* Group range_entries for the same SSA_NAME together. */
2030 if (SSA_NAME_VERSION (p->exp) < SSA_NAME_VERSION (q->exp))
2031 return -1;
2032 else if (SSA_NAME_VERSION (p->exp) > SSA_NAME_VERSION (q->exp))
2033 return 1;
2034 /* If ->low is different, NULL low goes first, then by
2035 ascending low. */
2036 if (p->low != NULL_TREE)
2038 if (q->low != NULL_TREE)
2040 tree tem = fold_binary (LT_EXPR, boolean_type_node,
2041 p->low, q->low);
2042 if (tem && integer_onep (tem))
2043 return -1;
2044 tem = fold_binary (GT_EXPR, boolean_type_node,
2045 p->low, q->low);
2046 if (tem && integer_onep (tem))
2047 return 1;
2049 else
2050 return 1;
2052 else if (q->low != NULL_TREE)
2053 return -1;
2054 /* If ->high is different, NULL high goes last, before that by
2055 ascending high. */
2056 if (p->high != NULL_TREE)
2058 if (q->high != NULL_TREE)
2060 tree tem = fold_binary (LT_EXPR, boolean_type_node,
2061 p->high, q->high);
2062 if (tem && integer_onep (tem))
2063 return -1;
2064 tem = fold_binary (GT_EXPR, boolean_type_node,
2065 p->high, q->high);
2066 if (tem && integer_onep (tem))
2067 return 1;
2069 else
2070 return -1;
2072 else if (p->high != NULL_TREE)
2073 return 1;
2074 /* If both ranges are the same, sort below by ascending idx. */
2076 else
2077 return 1;
2079 else if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
2080 return -1;
2082 if (p->idx < q->idx)
2083 return -1;
2084 else
2086 gcc_checking_assert (p->idx > q->idx);
2087 return 1;
2091 /* Helper routine of optimize_range_test.
2092 [EXP, IN_P, LOW, HIGH, STRICT_OVERFLOW_P] is a merged range for
2093 RANGE and OTHERRANGE through OTHERRANGE + COUNT - 1 ranges,
2094 OPCODE and OPS are arguments of optimize_range_tests. If OTHERRANGE
2095 is NULL, OTHERRANGEP should not be and then OTHERRANGEP points to
2096 an array of COUNT pointers to other ranges. Return
2097 true if the range merge has been successful.
2098 If OPCODE is ERROR_MARK, this is called from within
2099 maybe_optimize_range_tests and is performing inter-bb range optimization.
2100 In that case, whether an op is BIT_AND_EXPR or BIT_IOR_EXPR is found in
2101 oe->rank. */
2103 static bool
2104 update_range_test (struct range_entry *range, struct range_entry *otherrange,
2105 struct range_entry **otherrangep,
2106 unsigned int count, enum tree_code opcode,
2107 vec<operand_entry_t> *ops, tree exp, gimple_seq seq,
2108 bool in_p, tree low, tree high, bool strict_overflow_p)
2110 operand_entry_t oe = (*ops)[range->idx];
2111 tree op = oe->op;
2112 gimple stmt = op ? SSA_NAME_DEF_STMT (op) :
2113 last_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
2114 location_t loc = gimple_location (stmt);
2115 tree optype = op ? TREE_TYPE (op) : boolean_type_node;
2116 tree tem = build_range_check (loc, optype, unshare_expr (exp),
2117 in_p, low, high);
2118 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
2119 gimple_stmt_iterator gsi;
2120 unsigned int i;
2122 if (tem == NULL_TREE)
2123 return false;
2125 if (strict_overflow_p && issue_strict_overflow_warning (wc))
2126 warning_at (loc, OPT_Wstrict_overflow,
2127 "assuming signed overflow does not occur "
2128 "when simplifying range test");
2130 if (dump_file && (dump_flags & TDF_DETAILS))
2132 struct range_entry *r;
2133 fprintf (dump_file, "Optimizing range tests ");
2134 print_generic_expr (dump_file, range->exp, 0);
2135 fprintf (dump_file, " %c[", range->in_p ? '+' : '-');
2136 print_generic_expr (dump_file, range->low, 0);
2137 fprintf (dump_file, ", ");
2138 print_generic_expr (dump_file, range->high, 0);
2139 fprintf (dump_file, "]");
2140 for (i = 0; i < count; i++)
2142 if (otherrange)
2143 r = otherrange + i;
2144 else
2145 r = otherrangep[i];
2146 fprintf (dump_file, " and %c[", r->in_p ? '+' : '-');
2147 print_generic_expr (dump_file, r->low, 0);
2148 fprintf (dump_file, ", ");
2149 print_generic_expr (dump_file, r->high, 0);
2150 fprintf (dump_file, "]");
2152 fprintf (dump_file, "\n into ");
2153 print_generic_expr (dump_file, tem, 0);
2154 fprintf (dump_file, "\n");
2157 if (opcode == BIT_IOR_EXPR
2158 || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
2159 tem = invert_truthvalue_loc (loc, tem);
2161 tem = fold_convert_loc (loc, optype, tem);
2162 gsi = gsi_for_stmt (stmt);
2163 /* In rare cases range->exp can be equal to lhs of stmt.
2164 In that case we have to insert after the stmt rather then before
2165 it. */
2166 if (op == range->exp)
2168 gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
2169 tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE, false,
2170 GSI_CONTINUE_LINKING);
2172 else
2174 gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
2175 tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE, true,
2176 GSI_SAME_STMT);
2177 gsi_prev (&gsi);
2179 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2180 if (gimple_uid (gsi_stmt (gsi)))
2181 break;
2182 else
2183 gimple_set_uid (gsi_stmt (gsi), gimple_uid (stmt));
2185 oe->op = tem;
2186 range->exp = exp;
2187 range->low = low;
2188 range->high = high;
2189 range->in_p = in_p;
2190 range->strict_overflow_p = false;
2192 for (i = 0; i < count; i++)
2194 if (otherrange)
2195 range = otherrange + i;
2196 else
2197 range = otherrangep[i];
2198 oe = (*ops)[range->idx];
2199 /* Now change all the other range test immediate uses, so that
2200 those tests will be optimized away. */
2201 if (opcode == ERROR_MARK)
2203 if (oe->op)
2204 oe->op = build_int_cst (TREE_TYPE (oe->op),
2205 oe->rank == BIT_IOR_EXPR ? 0 : 1);
2206 else
2207 oe->op = (oe->rank == BIT_IOR_EXPR
2208 ? boolean_false_node : boolean_true_node);
2210 else
2211 oe->op = error_mark_node;
2212 range->exp = NULL_TREE;
2214 return true;
2217 /* Optimize X == CST1 || X == CST2
2218 if popcount (CST1 ^ CST2) == 1 into
2219 (X & ~(CST1 ^ CST2)) == (CST1 & ~(CST1 ^ CST2)).
2220 Similarly for ranges. E.g.
2221 X != 2 && X != 3 && X != 10 && X != 11
2222 will be transformed by the previous optimization into
2223 !((X - 2U) <= 1U || (X - 10U) <= 1U)
2224 and this loop can transform that into
2225 !(((X & ~8) - 2U) <= 1U). */
2227 static bool
2228 optimize_range_tests_xor (enum tree_code opcode, tree type,
2229 tree lowi, tree lowj, tree highi, tree highj,
2230 vec<operand_entry_t> *ops,
2231 struct range_entry *rangei,
2232 struct range_entry *rangej)
2234 tree lowxor, highxor, tem, exp;
2235 /* Check lowi ^ lowj == highi ^ highj and
2236 popcount (lowi ^ lowj) == 1. */
2237 lowxor = fold_binary (BIT_XOR_EXPR, type, lowi, lowj);
2238 if (lowxor == NULL_TREE || TREE_CODE (lowxor) != INTEGER_CST)
2239 return false;
2240 if (!integer_pow2p (lowxor))
2241 return false;
2242 highxor = fold_binary (BIT_XOR_EXPR, type, highi, highj);
2243 if (!tree_int_cst_equal (lowxor, highxor))
2244 return false;
2246 tem = fold_build1 (BIT_NOT_EXPR, type, lowxor);
2247 exp = fold_build2 (BIT_AND_EXPR, type, rangei->exp, tem);
2248 lowj = fold_build2 (BIT_AND_EXPR, type, lowi, tem);
2249 highj = fold_build2 (BIT_AND_EXPR, type, highi, tem);
2250 if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, exp,
2251 NULL, rangei->in_p, lowj, highj,
2252 rangei->strict_overflow_p
2253 || rangej->strict_overflow_p))
2254 return true;
2255 return false;
2258 /* Optimize X == CST1 || X == CST2
2259 if popcount (CST2 - CST1) == 1 into
2260 ((X - CST1) & ~(CST2 - CST1)) == 0.
2261 Similarly for ranges. E.g.
2262 X == 43 || X == 76 || X == 44 || X == 78 || X == 77 || X == 46
2263 || X == 75 || X == 45
2264 will be transformed by the previous optimization into
2265 (X - 43U) <= 3U || (X - 75U) <= 3U
2266 and this loop can transform that into
2267 ((X - 43U) & ~(75U - 43U)) <= 3U. */
2268 static bool
2269 optimize_range_tests_diff (enum tree_code opcode, tree type,
2270 tree lowi, tree lowj, tree highi, tree highj,
2271 vec<operand_entry_t> *ops,
2272 struct range_entry *rangei,
2273 struct range_entry *rangej)
2275 tree tem1, tem2, mask;
2276 /* Check highi - lowi == highj - lowj. */
2277 tem1 = fold_binary (MINUS_EXPR, type, highi, lowi);
2278 if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
2279 return false;
2280 tem2 = fold_binary (MINUS_EXPR, type, highj, lowj);
2281 if (!tree_int_cst_equal (tem1, tem2))
2282 return false;
2283 /* Check popcount (lowj - lowi) == 1. */
2284 tem1 = fold_binary (MINUS_EXPR, type, lowj, lowi);
2285 if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
2286 return false;
2287 if (!integer_pow2p (tem1))
2288 return false;
2290 type = unsigned_type_for (type);
2291 tem1 = fold_convert (type, tem1);
2292 tem2 = fold_convert (type, tem2);
2293 lowi = fold_convert (type, lowi);
2294 mask = fold_build1 (BIT_NOT_EXPR, type, tem1);
2295 tem1 = fold_binary (MINUS_EXPR, type,
2296 fold_convert (type, rangei->exp), lowi);
2297 tem1 = fold_build2 (BIT_AND_EXPR, type, tem1, mask);
2298 lowj = build_int_cst (type, 0);
2299 if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, tem1,
2300 NULL, rangei->in_p, lowj, tem2,
2301 rangei->strict_overflow_p
2302 || rangej->strict_overflow_p))
2303 return true;
2304 return false;
2307 /* It does some common checks for function optimize_range_tests_xor and
2308 optimize_range_tests_diff.
2309 If OPTIMIZE_XOR is TRUE, it calls optimize_range_tests_xor.
2310 Else it calls optimize_range_tests_diff. */
2312 static bool
2313 optimize_range_tests_1 (enum tree_code opcode, int first, int length,
2314 bool optimize_xor, vec<operand_entry_t> *ops,
2315 struct range_entry *ranges)
2317 int i, j;
2318 bool any_changes = false;
2319 for (i = first; i < length; i++)
2321 tree lowi, highi, lowj, highj, type, tem;
2323 if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
2324 continue;
2325 type = TREE_TYPE (ranges[i].exp);
2326 if (!INTEGRAL_TYPE_P (type))
2327 continue;
2328 lowi = ranges[i].low;
2329 if (lowi == NULL_TREE)
2330 lowi = TYPE_MIN_VALUE (type);
2331 highi = ranges[i].high;
2332 if (highi == NULL_TREE)
2333 continue;
2334 for (j = i + 1; j < length && j < i + 64; j++)
2336 bool changes;
2337 if (ranges[i].exp != ranges[j].exp || ranges[j].in_p)
2338 continue;
2339 lowj = ranges[j].low;
2340 if (lowj == NULL_TREE)
2341 continue;
2342 highj = ranges[j].high;
2343 if (highj == NULL_TREE)
2344 highj = TYPE_MAX_VALUE (type);
2345 /* Check lowj > highi. */
2346 tem = fold_binary (GT_EXPR, boolean_type_node,
2347 lowj, highi);
2348 if (tem == NULL_TREE || !integer_onep (tem))
2349 continue;
2350 if (optimize_xor)
2351 changes = optimize_range_tests_xor (opcode, type, lowi, lowj,
2352 highi, highj, ops,
2353 ranges + i, ranges + j);
2354 else
2355 changes = optimize_range_tests_diff (opcode, type, lowi, lowj,
2356 highi, highj, ops,
2357 ranges + i, ranges + j);
2358 if (changes)
2360 any_changes = true;
2361 break;
2365 return any_changes;
2368 /* Helper function of optimize_range_tests_to_bit_test. Handle a single
2369 range, EXP, LOW, HIGH, compute bit mask of bits to test and return
2370 EXP on success, NULL otherwise. */
2372 static tree
2373 extract_bit_test_mask (tree exp, int prec, tree totallow, tree low, tree high,
2374 wide_int *mask, tree *totallowp)
2376 tree tem = int_const_binop (MINUS_EXPR, high, low);
2377 if (tem == NULL_TREE
2378 || TREE_CODE (tem) != INTEGER_CST
2379 || TREE_OVERFLOW (tem)
2380 || tree_int_cst_sgn (tem) == -1
2381 || compare_tree_int (tem, prec) != -1)
2382 return NULL_TREE;
2384 unsigned HOST_WIDE_INT max = tree_to_uhwi (tem) + 1;
2385 *mask = wi::shifted_mask (0, max, false, prec);
2386 if (TREE_CODE (exp) == BIT_AND_EXPR
2387 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
2389 widest_int msk = wi::to_widest (TREE_OPERAND (exp, 1));
2390 msk = wi::zext (~msk, TYPE_PRECISION (TREE_TYPE (exp)));
2391 if (wi::popcount (msk) == 1
2392 && wi::ltu_p (msk, prec - max))
2394 *mask |= wi::shifted_mask (msk.to_uhwi (), max, false, prec);
2395 max += msk.to_uhwi ();
2396 exp = TREE_OPERAND (exp, 0);
2397 if (integer_zerop (low)
2398 && TREE_CODE (exp) == PLUS_EXPR
2399 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
2401 widest_int bias
2402 = wi::neg (wi::sext (wi::to_widest (TREE_OPERAND (exp, 1)),
2403 TYPE_PRECISION (TREE_TYPE (low))));
2404 tree tbias = wide_int_to_tree (TREE_TYPE (low), bias);
2405 if (totallowp)
2407 *totallowp = tbias;
2408 exp = TREE_OPERAND (exp, 0);
2409 STRIP_NOPS (exp);
2410 return exp;
2412 else if (!tree_int_cst_lt (totallow, tbias))
2413 return NULL_TREE;
2414 bias -= wi::to_widest (totallow);
2415 if (wi::ges_p (bias, 0) && wi::lts_p (bias, prec - max))
2417 *mask = wi::lshift (*mask, bias);
2418 exp = TREE_OPERAND (exp, 0);
2419 STRIP_NOPS (exp);
2420 return exp;
2425 if (totallowp)
2426 return exp;
2427 if (!tree_int_cst_lt (totallow, low))
2428 return exp;
2429 tem = int_const_binop (MINUS_EXPR, low, totallow);
2430 if (tem == NULL_TREE
2431 || TREE_CODE (tem) != INTEGER_CST
2432 || TREE_OVERFLOW (tem)
2433 || compare_tree_int (tem, prec - max) == 1)
2434 return NULL_TREE;
2436 *mask = wi::lshift (*mask, wi::to_widest (tem));
2437 return exp;
2440 /* Attempt to optimize small range tests using bit test.
2441 E.g.
2442 X != 43 && X != 76 && X != 44 && X != 78 && X != 49
2443 && X != 77 && X != 46 && X != 75 && X != 45 && X != 82
2444 has been by earlier optimizations optimized into:
2445 ((X - 43U) & ~32U) > 3U && X != 49 && X != 82
2446 As all the 43 through 82 range is less than 64 numbers,
2447 for 64-bit word targets optimize that into:
2448 (X - 43U) > 40U && ((1 << (X - 43U)) & 0x8F0000004FULL) == 0 */
2450 static bool
2451 optimize_range_tests_to_bit_test (enum tree_code opcode, int first, int length,
2452 vec<operand_entry_t> *ops,
2453 struct range_entry *ranges)
2455 int i, j;
2456 bool any_changes = false;
2457 int prec = GET_MODE_BITSIZE (word_mode);
2458 auto_vec<struct range_entry *, 64> candidates;
2460 for (i = first; i < length - 2; i++)
2462 tree lowi, highi, lowj, highj, type;
2464 if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
2465 continue;
2466 type = TREE_TYPE (ranges[i].exp);
2467 if (!INTEGRAL_TYPE_P (type))
2468 continue;
2469 lowi = ranges[i].low;
2470 if (lowi == NULL_TREE)
2471 lowi = TYPE_MIN_VALUE (type);
2472 highi = ranges[i].high;
2473 if (highi == NULL_TREE)
2474 continue;
2475 wide_int mask;
2476 tree exp = extract_bit_test_mask (ranges[i].exp, prec, lowi, lowi,
2477 highi, &mask, &lowi);
2478 if (exp == NULL_TREE)
2479 continue;
2480 bool strict_overflow_p = ranges[i].strict_overflow_p;
2481 candidates.truncate (0);
2482 int end = MIN (i + 64, length);
2483 for (j = i + 1; j < end; j++)
2485 tree exp2;
2486 if (ranges[j].exp == NULL_TREE || ranges[j].in_p)
2487 continue;
2488 if (ranges[j].exp == exp)
2490 else if (TREE_CODE (ranges[j].exp) == BIT_AND_EXPR)
2492 exp2 = TREE_OPERAND (ranges[j].exp, 0);
2493 if (exp2 == exp)
2495 else if (TREE_CODE (exp2) == PLUS_EXPR)
2497 exp2 = TREE_OPERAND (exp2, 0);
2498 STRIP_NOPS (exp2);
2499 if (exp2 != exp)
2500 continue;
2502 else
2503 continue;
2505 else
2506 continue;
2507 lowj = ranges[j].low;
2508 if (lowj == NULL_TREE)
2509 continue;
2510 highj = ranges[j].high;
2511 if (highj == NULL_TREE)
2512 highj = TYPE_MAX_VALUE (type);
2513 wide_int mask2;
2514 exp2 = extract_bit_test_mask (ranges[j].exp, prec, lowi, lowj,
2515 highj, &mask2, NULL);
2516 if (exp2 != exp)
2517 continue;
2518 mask |= mask2;
2519 strict_overflow_p |= ranges[j].strict_overflow_p;
2520 candidates.safe_push (&ranges[j]);
2523 /* If we need otherwise 3 or more comparisons, use a bit test. */
2524 if (candidates.length () >= 2)
2526 tree high = wide_int_to_tree (TREE_TYPE (lowi),
2527 wi::to_widest (lowi)
2528 + prec - 1 - wi::clz (mask));
2529 operand_entry_t oe = (*ops)[ranges[i].idx];
2530 tree op = oe->op;
2531 gimple stmt = op ? SSA_NAME_DEF_STMT (op)
2532 : last_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
2533 location_t loc = gimple_location (stmt);
2534 tree optype = op ? TREE_TYPE (op) : boolean_type_node;
2536 /* See if it isn't cheaper to pretend the minimum value of the
2537 range is 0, if maximum value is small enough.
2538 We can avoid then subtraction of the minimum value, but the
2539 mask constant could be perhaps more expensive. */
2540 if (compare_tree_int (lowi, 0) > 0
2541 && compare_tree_int (high, prec) < 0)
2543 int cost_diff;
2544 HOST_WIDE_INT m = tree_to_uhwi (lowi);
2545 rtx reg = gen_raw_REG (word_mode, 10000);
2546 bool speed_p = optimize_bb_for_speed_p (gimple_bb (stmt));
2547 cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
2548 GEN_INT (-m)), speed_p);
2549 rtx r = immed_wide_int_const (mask, word_mode);
2550 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
2551 speed_p);
2552 r = immed_wide_int_const (wi::lshift (mask, m), word_mode);
2553 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
2554 speed_p);
2555 if (cost_diff > 0)
2557 mask = wi::lshift (mask, m);
2558 lowi = build_zero_cst (TREE_TYPE (lowi));
2562 tree tem = build_range_check (loc, optype, unshare_expr (exp),
2563 false, lowi, high);
2564 if (tem == NULL_TREE || is_gimple_val (tem))
2565 continue;
2566 tree etype = unsigned_type_for (TREE_TYPE (exp));
2567 exp = fold_build2_loc (loc, MINUS_EXPR, etype,
2568 fold_convert_loc (loc, etype, exp),
2569 fold_convert_loc (loc, etype, lowi));
2570 exp = fold_convert_loc (loc, integer_type_node, exp);
2571 tree word_type = lang_hooks.types.type_for_mode (word_mode, 1);
2572 exp = fold_build2_loc (loc, LSHIFT_EXPR, word_type,
2573 build_int_cst (word_type, 1), exp);
2574 exp = fold_build2_loc (loc, BIT_AND_EXPR, word_type, exp,
2575 wide_int_to_tree (word_type, mask));
2576 exp = fold_build2_loc (loc, EQ_EXPR, optype, exp,
2577 build_zero_cst (word_type));
2578 if (is_gimple_val (exp))
2579 continue;
2581 /* The shift might have undefined behavior if TEM is true,
2582 but reassociate_bb isn't prepared to have basic blocks
2583 split when it is running. So, temporarily emit a code
2584 with BIT_IOR_EXPR instead of &&, and fix it up in
2585 branch_fixup. */
2586 gimple_seq seq;
2587 tem = force_gimple_operand (tem, &seq, true, NULL_TREE);
2588 gcc_assert (TREE_CODE (tem) == SSA_NAME);
2589 gimple_set_visited (SSA_NAME_DEF_STMT (tem), true);
2590 gimple_seq seq2;
2591 exp = force_gimple_operand (exp, &seq2, true, NULL_TREE);
2592 gimple_seq_add_seq_without_update (&seq, seq2);
2593 gcc_assert (TREE_CODE (exp) == SSA_NAME);
2594 gimple_set_visited (SSA_NAME_DEF_STMT (exp), true);
2595 gimple g
2596 = gimple_build_assign_with_ops (BIT_IOR_EXPR,
2597 make_ssa_name (optype, NULL),
2598 tem, exp);
2599 gimple_set_location (g, loc);
2600 gimple_seq_add_stmt_without_update (&seq, g);
2601 exp = gimple_assign_lhs (g);
2602 tree val = build_zero_cst (optype);
2603 if (update_range_test (&ranges[i], NULL, candidates.address (),
2604 candidates.length (), opcode, ops, exp,
2605 seq, false, val, val, strict_overflow_p))
2607 any_changes = true;
2608 reassoc_branch_fixups.safe_push (tem);
2610 else
2611 gimple_seq_discard (seq);
2614 return any_changes;
2617 /* Optimize range tests, similarly how fold_range_test optimizes
2618 it on trees. The tree code for the binary
2619 operation between all the operands is OPCODE.
2620 If OPCODE is ERROR_MARK, optimize_range_tests is called from within
2621 maybe_optimize_range_tests for inter-bb range optimization.
2622 In that case if oe->op is NULL, oe->id is bb->index whose
2623 GIMPLE_COND is && or ||ed into the test, and oe->rank says
2624 the actual opcode. */
2626 static bool
2627 optimize_range_tests (enum tree_code opcode,
2628 vec<operand_entry_t> *ops)
2630 unsigned int length = ops->length (), i, j, first;
2631 operand_entry_t oe;
2632 struct range_entry *ranges;
2633 bool any_changes = false;
2635 if (length == 1)
2636 return false;
2638 ranges = XNEWVEC (struct range_entry, length);
2639 for (i = 0; i < length; i++)
2641 oe = (*ops)[i];
2642 ranges[i].idx = i;
2643 init_range_entry (ranges + i, oe->op,
2644 oe->op ? NULL :
2645 last_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id)));
2646 /* For | invert it now, we will invert it again before emitting
2647 the optimized expression. */
2648 if (opcode == BIT_IOR_EXPR
2649 || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
2650 ranges[i].in_p = !ranges[i].in_p;
2653 qsort (ranges, length, sizeof (*ranges), range_entry_cmp);
2654 for (i = 0; i < length; i++)
2655 if (ranges[i].exp != NULL_TREE && TREE_CODE (ranges[i].exp) == SSA_NAME)
2656 break;
2658 /* Try to merge ranges. */
2659 for (first = i; i < length; i++)
2661 tree low = ranges[i].low;
2662 tree high = ranges[i].high;
2663 int in_p = ranges[i].in_p;
2664 bool strict_overflow_p = ranges[i].strict_overflow_p;
2665 int update_fail_count = 0;
2667 for (j = i + 1; j < length; j++)
2669 if (ranges[i].exp != ranges[j].exp)
2670 break;
2671 if (!merge_ranges (&in_p, &low, &high, in_p, low, high,
2672 ranges[j].in_p, ranges[j].low, ranges[j].high))
2673 break;
2674 strict_overflow_p |= ranges[j].strict_overflow_p;
2677 if (j == i + 1)
2678 continue;
2680 if (update_range_test (ranges + i, ranges + i + 1, NULL, j - i - 1,
2681 opcode, ops, ranges[i].exp, NULL, in_p,
2682 low, high, strict_overflow_p))
2684 i = j - 1;
2685 any_changes = true;
2687 /* Avoid quadratic complexity if all merge_ranges calls would succeed,
2688 while update_range_test would fail. */
2689 else if (update_fail_count == 64)
2690 i = j - 1;
2691 else
2692 ++update_fail_count;
2695 any_changes |= optimize_range_tests_1 (opcode, first, length, true,
2696 ops, ranges);
2698 if (BRANCH_COST (optimize_function_for_speed_p (cfun), false) >= 2)
2699 any_changes |= optimize_range_tests_1 (opcode, first, length, false,
2700 ops, ranges);
2701 if (lshift_cheap_p (optimize_function_for_speed_p (cfun)))
2702 any_changes |= optimize_range_tests_to_bit_test (opcode, first, length,
2703 ops, ranges);
2705 if (any_changes && opcode != ERROR_MARK)
2707 j = 0;
2708 FOR_EACH_VEC_ELT (*ops, i, oe)
2710 if (oe->op == error_mark_node)
2711 continue;
2712 else if (i != j)
2713 (*ops)[j] = oe;
2714 j++;
2716 ops->truncate (j);
2719 XDELETEVEC (ranges);
2720 return any_changes;
2723 /* Return true if STMT is a cast like:
2724 <bb N>:
2726 _123 = (int) _234;
2728 <bb M>:
2729 # _345 = PHI <_123(N), 1(...), 1(...)>
2730 where _234 has bool type, _123 has single use and
2731 bb N has a single successor M. This is commonly used in
2732 the last block of a range test. */
2734 static bool
2735 final_range_test_p (gimple stmt)
2737 basic_block bb, rhs_bb;
2738 edge e;
2739 tree lhs, rhs;
2740 use_operand_p use_p;
2741 gimple use_stmt;
2743 if (!gimple_assign_cast_p (stmt))
2744 return false;
2745 bb = gimple_bb (stmt);
2746 if (!single_succ_p (bb))
2747 return false;
2748 e = single_succ_edge (bb);
2749 if (e->flags & EDGE_COMPLEX)
2750 return false;
2752 lhs = gimple_assign_lhs (stmt);
2753 rhs = gimple_assign_rhs1 (stmt);
2754 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2755 || TREE_CODE (rhs) != SSA_NAME
2756 || TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE)
2757 return false;
2759 /* Test whether lhs is consumed only by a PHI in the only successor bb. */
2760 if (!single_imm_use (lhs, &use_p, &use_stmt))
2761 return false;
2763 if (gimple_code (use_stmt) != GIMPLE_PHI
2764 || gimple_bb (use_stmt) != e->dest)
2765 return false;
2767 /* And that the rhs is defined in the same loop. */
2768 rhs_bb = gimple_bb (SSA_NAME_DEF_STMT (rhs));
2769 if (rhs_bb == NULL
2770 || !flow_bb_inside_loop_p (loop_containing_stmt (stmt), rhs_bb))
2771 return false;
2773 return true;
2776 /* Return true if BB is suitable basic block for inter-bb range test
2777 optimization. If BACKWARD is true, BB should be the only predecessor
2778 of TEST_BB, and *OTHER_BB is either NULL and filled by the routine,
2779 or compared with to find a common basic block to which all conditions
2780 branch to if true resp. false. If BACKWARD is false, TEST_BB should
2781 be the only predecessor of BB. */
2783 static bool
2784 suitable_cond_bb (basic_block bb, basic_block test_bb, basic_block *other_bb,
2785 bool backward)
2787 edge_iterator ei, ei2;
2788 edge e, e2;
2789 gimple stmt;
2790 gphi_iterator gsi;
2791 bool other_edge_seen = false;
2792 bool is_cond;
2794 if (test_bb == bb)
2795 return false;
2796 /* Check last stmt first. */
2797 stmt = last_stmt (bb);
2798 if (stmt == NULL
2799 || (gimple_code (stmt) != GIMPLE_COND
2800 && (backward || !final_range_test_p (stmt)))
2801 || gimple_visited_p (stmt)
2802 || stmt_could_throw_p (stmt)
2803 || *other_bb == bb)
2804 return false;
2805 is_cond = gimple_code (stmt) == GIMPLE_COND;
2806 if (is_cond)
2808 /* If last stmt is GIMPLE_COND, verify that one of the succ edges
2809 goes to the next bb (if BACKWARD, it is TEST_BB), and the other
2810 to *OTHER_BB (if not set yet, try to find it out). */
2811 if (EDGE_COUNT (bb->succs) != 2)
2812 return false;
2813 FOR_EACH_EDGE (e, ei, bb->succs)
2815 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
2816 return false;
2817 if (e->dest == test_bb)
2819 if (backward)
2820 continue;
2821 else
2822 return false;
2824 if (e->dest == bb)
2825 return false;
2826 if (*other_bb == NULL)
2828 FOR_EACH_EDGE (e2, ei2, test_bb->succs)
2829 if (!(e2->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
2830 return false;
2831 else if (e->dest == e2->dest)
2832 *other_bb = e->dest;
2833 if (*other_bb == NULL)
2834 return false;
2836 if (e->dest == *other_bb)
2837 other_edge_seen = true;
2838 else if (backward)
2839 return false;
2841 if (*other_bb == NULL || !other_edge_seen)
2842 return false;
2844 else if (single_succ (bb) != *other_bb)
2845 return false;
2847 /* Now check all PHIs of *OTHER_BB. */
2848 e = find_edge (bb, *other_bb);
2849 e2 = find_edge (test_bb, *other_bb);
2850 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
2852 gphi *phi = gsi.phi ();
2853 /* If both BB and TEST_BB end with GIMPLE_COND, all PHI arguments
2854 corresponding to BB and TEST_BB predecessor must be the same. */
2855 if (!operand_equal_p (gimple_phi_arg_def (phi, e->dest_idx),
2856 gimple_phi_arg_def (phi, e2->dest_idx), 0))
2858 /* Otherwise, if one of the blocks doesn't end with GIMPLE_COND,
2859 one of the PHIs should have the lhs of the last stmt in
2860 that block as PHI arg and that PHI should have 0 or 1
2861 corresponding to it in all other range test basic blocks
2862 considered. */
2863 if (!is_cond)
2865 if (gimple_phi_arg_def (phi, e->dest_idx)
2866 == gimple_assign_lhs (stmt)
2867 && (integer_zerop (gimple_phi_arg_def (phi, e2->dest_idx))
2868 || integer_onep (gimple_phi_arg_def (phi,
2869 e2->dest_idx))))
2870 continue;
2872 else
2874 gimple test_last = last_stmt (test_bb);
2875 if (gimple_code (test_last) != GIMPLE_COND
2876 && gimple_phi_arg_def (phi, e2->dest_idx)
2877 == gimple_assign_lhs (test_last)
2878 && (integer_zerop (gimple_phi_arg_def (phi, e->dest_idx))
2879 || integer_onep (gimple_phi_arg_def (phi, e->dest_idx))))
2880 continue;
2883 return false;
2886 return true;
2889 /* Return true if BB doesn't have side-effects that would disallow
2890 range test optimization, all SSA_NAMEs set in the bb are consumed
2891 in the bb and there are no PHIs. */
2893 static bool
2894 no_side_effect_bb (basic_block bb)
2896 gimple_stmt_iterator gsi;
2897 gimple last;
2899 if (!gimple_seq_empty_p (phi_nodes (bb)))
2900 return false;
2901 last = last_stmt (bb);
2902 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2904 gimple stmt = gsi_stmt (gsi);
2905 tree lhs;
2906 imm_use_iterator imm_iter;
2907 use_operand_p use_p;
2909 if (is_gimple_debug (stmt))
2910 continue;
2911 if (gimple_has_side_effects (stmt))
2912 return false;
2913 if (stmt == last)
2914 return true;
2915 if (!is_gimple_assign (stmt))
2916 return false;
2917 lhs = gimple_assign_lhs (stmt);
2918 if (TREE_CODE (lhs) != SSA_NAME)
2919 return false;
2920 if (gimple_assign_rhs_could_trap_p (stmt))
2921 return false;
2922 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
2924 gimple use_stmt = USE_STMT (use_p);
2925 if (is_gimple_debug (use_stmt))
2926 continue;
2927 if (gimple_bb (use_stmt) != bb)
2928 return false;
2931 return false;
2934 /* If VAR is set by CODE (BIT_{AND,IOR}_EXPR) which is reassociable,
2935 return true and fill in *OPS recursively. */
2937 static bool
2938 get_ops (tree var, enum tree_code code, vec<operand_entry_t> *ops,
2939 struct loop *loop)
2941 gimple stmt = SSA_NAME_DEF_STMT (var);
2942 tree rhs[2];
2943 int i;
2945 if (!is_reassociable_op (stmt, code, loop))
2946 return false;
2948 rhs[0] = gimple_assign_rhs1 (stmt);
2949 rhs[1] = gimple_assign_rhs2 (stmt);
2950 gimple_set_visited (stmt, true);
2951 for (i = 0; i < 2; i++)
2952 if (TREE_CODE (rhs[i]) == SSA_NAME
2953 && !get_ops (rhs[i], code, ops, loop)
2954 && has_single_use (rhs[i]))
2956 operand_entry_t oe = (operand_entry_t) pool_alloc (operand_entry_pool);
2958 oe->op = rhs[i];
2959 oe->rank = code;
2960 oe->id = 0;
2961 oe->count = 1;
2962 ops->safe_push (oe);
2964 return true;
2967 /* Find the ops that were added by get_ops starting from VAR, see if
2968 they were changed during update_range_test and if yes, create new
2969 stmts. */
2971 static tree
2972 update_ops (tree var, enum tree_code code, vec<operand_entry_t> ops,
2973 unsigned int *pidx, struct loop *loop)
2975 gimple stmt = SSA_NAME_DEF_STMT (var);
2976 tree rhs[4];
2977 int i;
2979 if (!is_reassociable_op (stmt, code, loop))
2980 return NULL;
2982 rhs[0] = gimple_assign_rhs1 (stmt);
2983 rhs[1] = gimple_assign_rhs2 (stmt);
2984 rhs[2] = rhs[0];
2985 rhs[3] = rhs[1];
2986 for (i = 0; i < 2; i++)
2987 if (TREE_CODE (rhs[i]) == SSA_NAME)
2989 rhs[2 + i] = update_ops (rhs[i], code, ops, pidx, loop);
2990 if (rhs[2 + i] == NULL_TREE)
2992 if (has_single_use (rhs[i]))
2993 rhs[2 + i] = ops[(*pidx)++]->op;
2994 else
2995 rhs[2 + i] = rhs[i];
2998 if ((rhs[2] != rhs[0] || rhs[3] != rhs[1])
2999 && (rhs[2] != rhs[1] || rhs[3] != rhs[0]))
3001 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3002 var = make_ssa_name (TREE_TYPE (var), NULL);
3003 gassign *g = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt),
3004 var, rhs[2], rhs[3]);
3005 gimple_set_uid (g, gimple_uid (stmt));
3006 gimple_set_visited (g, true);
3007 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3009 return var;
3012 /* Structure to track the initial value passed to get_ops and
3013 the range in the ops vector for each basic block. */
3015 struct inter_bb_range_test_entry
3017 tree op;
3018 unsigned int first_idx, last_idx;
3021 /* Inter-bb range test optimization. */
3023 static void
3024 maybe_optimize_range_tests (gimple stmt)
3026 basic_block first_bb = gimple_bb (stmt);
3027 basic_block last_bb = first_bb;
3028 basic_block other_bb = NULL;
3029 basic_block bb;
3030 edge_iterator ei;
3031 edge e;
3032 auto_vec<operand_entry_t> ops;
3033 auto_vec<inter_bb_range_test_entry> bbinfo;
3034 bool any_changes = false;
3036 /* Consider only basic blocks that end with GIMPLE_COND or
3037 a cast statement satisfying final_range_test_p. All
3038 but the last bb in the first_bb .. last_bb range
3039 should end with GIMPLE_COND. */
3040 if (gimple_code (stmt) == GIMPLE_COND)
3042 if (EDGE_COUNT (first_bb->succs) != 2)
3043 return;
3045 else if (final_range_test_p (stmt))
3046 other_bb = single_succ (first_bb);
3047 else
3048 return;
3050 if (stmt_could_throw_p (stmt))
3051 return;
3053 /* As relative ordering of post-dominator sons isn't fixed,
3054 maybe_optimize_range_tests can be called first on any
3055 bb in the range we want to optimize. So, start searching
3056 backwards, if first_bb can be set to a predecessor. */
3057 while (single_pred_p (first_bb))
3059 basic_block pred_bb = single_pred (first_bb);
3060 if (!suitable_cond_bb (pred_bb, first_bb, &other_bb, true))
3061 break;
3062 if (!no_side_effect_bb (first_bb))
3063 break;
3064 first_bb = pred_bb;
3066 /* If first_bb is last_bb, other_bb hasn't been computed yet.
3067 Before starting forward search in last_bb successors, find
3068 out the other_bb. */
3069 if (first_bb == last_bb)
3071 other_bb = NULL;
3072 /* As non-GIMPLE_COND last stmt always terminates the range,
3073 if forward search didn't discover anything, just give up. */
3074 if (gimple_code (stmt) != GIMPLE_COND)
3075 return;
3076 /* Look at both successors. Either it ends with a GIMPLE_COND
3077 and satisfies suitable_cond_bb, or ends with a cast and
3078 other_bb is that cast's successor. */
3079 FOR_EACH_EDGE (e, ei, first_bb->succs)
3080 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
3081 || e->dest == first_bb)
3082 return;
3083 else if (single_pred_p (e->dest))
3085 stmt = last_stmt (e->dest);
3086 if (stmt
3087 && gimple_code (stmt) == GIMPLE_COND
3088 && EDGE_COUNT (e->dest->succs) == 2)
3090 if (suitable_cond_bb (first_bb, e->dest, &other_bb, true))
3091 break;
3092 else
3093 other_bb = NULL;
3095 else if (stmt
3096 && final_range_test_p (stmt)
3097 && find_edge (first_bb, single_succ (e->dest)))
3099 other_bb = single_succ (e->dest);
3100 if (other_bb == first_bb)
3101 other_bb = NULL;
3104 if (other_bb == NULL)
3105 return;
3107 /* Now do the forward search, moving last_bb to successor bbs
3108 that aren't other_bb. */
3109 while (EDGE_COUNT (last_bb->succs) == 2)
3111 FOR_EACH_EDGE (e, ei, last_bb->succs)
3112 if (e->dest != other_bb)
3113 break;
3114 if (e == NULL)
3115 break;
3116 if (!single_pred_p (e->dest))
3117 break;
3118 if (!suitable_cond_bb (e->dest, last_bb, &other_bb, false))
3119 break;
3120 if (!no_side_effect_bb (e->dest))
3121 break;
3122 last_bb = e->dest;
3124 if (first_bb == last_bb)
3125 return;
3126 /* Here basic blocks first_bb through last_bb's predecessor
3127 end with GIMPLE_COND, all of them have one of the edges to
3128 other_bb and another to another block in the range,
3129 all blocks except first_bb don't have side-effects and
3130 last_bb ends with either GIMPLE_COND, or cast satisfying
3131 final_range_test_p. */
3132 for (bb = last_bb; ; bb = single_pred (bb))
3134 enum tree_code code;
3135 tree lhs, rhs;
3136 inter_bb_range_test_entry bb_ent;
3138 bb_ent.op = NULL_TREE;
3139 bb_ent.first_idx = ops.length ();
3140 bb_ent.last_idx = bb_ent.first_idx;
3141 e = find_edge (bb, other_bb);
3142 stmt = last_stmt (bb);
3143 gimple_set_visited (stmt, true);
3144 if (gimple_code (stmt) != GIMPLE_COND)
3146 use_operand_p use_p;
3147 gimple phi;
3148 edge e2;
3149 unsigned int d;
3151 lhs = gimple_assign_lhs (stmt);
3152 rhs = gimple_assign_rhs1 (stmt);
3153 gcc_assert (bb == last_bb);
3155 /* stmt is
3156 _123 = (int) _234;
3158 followed by:
3159 <bb M>:
3160 # _345 = PHI <_123(N), 1(...), 1(...)>
3162 or 0 instead of 1. If it is 0, the _234
3163 range test is anded together with all the
3164 other range tests, if it is 1, it is ored with
3165 them. */
3166 single_imm_use (lhs, &use_p, &phi);
3167 gcc_assert (gimple_code (phi) == GIMPLE_PHI);
3168 e2 = find_edge (first_bb, other_bb);
3169 d = e2->dest_idx;
3170 gcc_assert (gimple_phi_arg_def (phi, e->dest_idx) == lhs);
3171 if (integer_zerop (gimple_phi_arg_def (phi, d)))
3172 code = BIT_AND_EXPR;
3173 else
3175 gcc_checking_assert (integer_onep (gimple_phi_arg_def (phi, d)));
3176 code = BIT_IOR_EXPR;
3179 /* If _234 SSA_NAME_DEF_STMT is
3180 _234 = _567 | _789;
3181 (or &, corresponding to 1/0 in the phi arguments,
3182 push into ops the individual range test arguments
3183 of the bitwise or resp. and, recursively. */
3184 if (!get_ops (rhs, code, &ops,
3185 loop_containing_stmt (stmt))
3186 && has_single_use (rhs))
3188 /* Otherwise, push the _234 range test itself. */
3189 operand_entry_t oe
3190 = (operand_entry_t) pool_alloc (operand_entry_pool);
3192 oe->op = rhs;
3193 oe->rank = code;
3194 oe->id = 0;
3195 oe->count = 1;
3196 ops.safe_push (oe);
3197 bb_ent.last_idx++;
3199 else
3200 bb_ent.last_idx = ops.length ();
3201 bb_ent.op = rhs;
3202 bbinfo.safe_push (bb_ent);
3203 continue;
3205 /* Otherwise stmt is GIMPLE_COND. */
3206 code = gimple_cond_code (stmt);
3207 lhs = gimple_cond_lhs (stmt);
3208 rhs = gimple_cond_rhs (stmt);
3209 if (TREE_CODE (lhs) == SSA_NAME
3210 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3211 && ((code != EQ_EXPR && code != NE_EXPR)
3212 || rhs != boolean_false_node
3213 /* Either push into ops the individual bitwise
3214 or resp. and operands, depending on which
3215 edge is other_bb. */
3216 || !get_ops (lhs, (((e->flags & EDGE_TRUE_VALUE) == 0)
3217 ^ (code == EQ_EXPR))
3218 ? BIT_AND_EXPR : BIT_IOR_EXPR, &ops,
3219 loop_containing_stmt (stmt))))
3221 /* Or push the GIMPLE_COND stmt itself. */
3222 operand_entry_t oe
3223 = (operand_entry_t) pool_alloc (operand_entry_pool);
3225 oe->op = NULL;
3226 oe->rank = (e->flags & EDGE_TRUE_VALUE)
3227 ? BIT_IOR_EXPR : BIT_AND_EXPR;
3228 /* oe->op = NULL signs that there is no SSA_NAME
3229 for the range test, and oe->id instead is the
3230 basic block number, at which's end the GIMPLE_COND
3231 is. */
3232 oe->id = bb->index;
3233 oe->count = 1;
3234 ops.safe_push (oe);
3235 bb_ent.op = NULL;
3236 bb_ent.last_idx++;
3238 else if (ops.length () > bb_ent.first_idx)
3240 bb_ent.op = lhs;
3241 bb_ent.last_idx = ops.length ();
3243 bbinfo.safe_push (bb_ent);
3244 if (bb == first_bb)
3245 break;
3247 if (ops.length () > 1)
3248 any_changes = optimize_range_tests (ERROR_MARK, &ops);
3249 if (any_changes)
3251 unsigned int idx;
3252 /* update_ops relies on has_single_use predicates returning the
3253 same values as it did during get_ops earlier. Additionally it
3254 never removes statements, only adds new ones and it should walk
3255 from the single imm use and check the predicate already before
3256 making those changes.
3257 On the other side, the handling of GIMPLE_COND directly can turn
3258 previously multiply used SSA_NAMEs into single use SSA_NAMEs, so
3259 it needs to be done in a separate loop afterwards. */
3260 for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
3262 if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
3263 && bbinfo[idx].op != NULL_TREE)
3265 tree new_op;
3267 stmt = last_stmt (bb);
3268 new_op = update_ops (bbinfo[idx].op,
3269 (enum tree_code)
3270 ops[bbinfo[idx].first_idx]->rank,
3271 ops, &bbinfo[idx].first_idx,
3272 loop_containing_stmt (stmt));
3273 if (new_op == NULL_TREE)
3275 gcc_assert (bb == last_bb);
3276 new_op = ops[bbinfo[idx].first_idx++]->op;
3278 if (bbinfo[idx].op != new_op)
3280 imm_use_iterator iter;
3281 use_operand_p use_p;
3282 gimple use_stmt, cast_stmt = NULL;
3284 FOR_EACH_IMM_USE_STMT (use_stmt, iter, bbinfo[idx].op)
3285 if (is_gimple_debug (use_stmt))
3286 continue;
3287 else if (gimple_code (use_stmt) == GIMPLE_COND
3288 || gimple_code (use_stmt) == GIMPLE_PHI)
3289 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3290 SET_USE (use_p, new_op);
3291 else if (gimple_assign_cast_p (use_stmt))
3292 cast_stmt = use_stmt;
3293 else
3294 gcc_unreachable ();
3295 if (cast_stmt)
3297 gcc_assert (bb == last_bb);
3298 tree lhs = gimple_assign_lhs (cast_stmt);
3299 tree new_lhs = make_ssa_name (TREE_TYPE (lhs), NULL);
3300 enum tree_code rhs_code
3301 = gimple_assign_rhs_code (cast_stmt);
3302 gassign *g;
3303 if (is_gimple_min_invariant (new_op))
3305 new_op = fold_convert (TREE_TYPE (lhs), new_op);
3306 g = gimple_build_assign (new_lhs, new_op);
3308 else
3309 g = gimple_build_assign_with_ops (rhs_code, new_lhs,
3310 new_op);
3311 gimple_stmt_iterator gsi = gsi_for_stmt (cast_stmt);
3312 gimple_set_uid (g, gimple_uid (cast_stmt));
3313 gimple_set_visited (g, true);
3314 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3315 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3316 if (is_gimple_debug (use_stmt))
3317 continue;
3318 else if (gimple_code (use_stmt) == GIMPLE_COND
3319 || gimple_code (use_stmt) == GIMPLE_PHI)
3320 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3321 SET_USE (use_p, new_lhs);
3322 else
3323 gcc_unreachable ();
3327 if (bb == first_bb)
3328 break;
3330 for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
3332 if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
3333 && bbinfo[idx].op == NULL_TREE
3334 && ops[bbinfo[idx].first_idx]->op != NULL_TREE)
3336 gcond *cond_stmt = as_a <gcond *> (last_stmt (bb));
3337 if (integer_zerop (ops[bbinfo[idx].first_idx]->op))
3338 gimple_cond_make_false (cond_stmt);
3339 else if (integer_onep (ops[bbinfo[idx].first_idx]->op))
3340 gimple_cond_make_true (cond_stmt);
3341 else
3343 gimple_cond_set_code (cond_stmt, NE_EXPR);
3344 gimple_cond_set_lhs (cond_stmt,
3345 ops[bbinfo[idx].first_idx]->op);
3346 gimple_cond_set_rhs (cond_stmt, boolean_false_node);
3348 update_stmt (cond_stmt);
3350 if (bb == first_bb)
3351 break;
3356 /* Return true if OPERAND is defined by a PHI node which uses the LHS
3357 of STMT in it's operands. This is also known as a "destructive
3358 update" operation. */
3360 static bool
3361 is_phi_for_stmt (gimple stmt, tree operand)
3363 gimple def_stmt;
3364 gphi *def_phi;
3365 tree lhs;
3366 use_operand_p arg_p;
3367 ssa_op_iter i;
3369 if (TREE_CODE (operand) != SSA_NAME)
3370 return false;
3372 lhs = gimple_assign_lhs (stmt);
3374 def_stmt = SSA_NAME_DEF_STMT (operand);
3375 def_phi = dyn_cast <gphi *> (def_stmt);
3376 if (!def_phi)
3377 return false;
3379 FOR_EACH_PHI_ARG (arg_p, def_phi, i, SSA_OP_USE)
3380 if (lhs == USE_FROM_PTR (arg_p))
3381 return true;
3382 return false;
3385 /* Remove def stmt of VAR if VAR has zero uses and recurse
3386 on rhs1 operand if so. */
3388 static void
3389 remove_visited_stmt_chain (tree var)
3391 gimple stmt;
3392 gimple_stmt_iterator gsi;
3394 while (1)
3396 if (TREE_CODE (var) != SSA_NAME || !has_zero_uses (var))
3397 return;
3398 stmt = SSA_NAME_DEF_STMT (var);
3399 if (is_gimple_assign (stmt) && gimple_visited_p (stmt))
3401 var = gimple_assign_rhs1 (stmt);
3402 gsi = gsi_for_stmt (stmt);
3403 reassoc_remove_stmt (&gsi);
3404 release_defs (stmt);
3406 else
3407 return;
3411 /* This function checks three consequtive operands in
3412 passed operands vector OPS starting from OPINDEX and
3413 swaps two operands if it is profitable for binary operation
3414 consuming OPINDEX + 1 abnd OPINDEX + 2 operands.
3416 We pair ops with the same rank if possible.
3418 The alternative we try is to see if STMT is a destructive
3419 update style statement, which is like:
3420 b = phi (a, ...)
3421 a = c + b;
3422 In that case, we want to use the destructive update form to
3423 expose the possible vectorizer sum reduction opportunity.
3424 In that case, the third operand will be the phi node. This
3425 check is not performed if STMT is null.
3427 We could, of course, try to be better as noted above, and do a
3428 lot of work to try to find these opportunities in >3 operand
3429 cases, but it is unlikely to be worth it. */
3431 static void
3432 swap_ops_for_binary_stmt (vec<operand_entry_t> ops,
3433 unsigned int opindex, gimple stmt)
3435 operand_entry_t oe1, oe2, oe3;
3437 oe1 = ops[opindex];
3438 oe2 = ops[opindex + 1];
3439 oe3 = ops[opindex + 2];
3441 if ((oe1->rank == oe2->rank
3442 && oe2->rank != oe3->rank)
3443 || (stmt && is_phi_for_stmt (stmt, oe3->op)
3444 && !is_phi_for_stmt (stmt, oe1->op)
3445 && !is_phi_for_stmt (stmt, oe2->op)))
3447 struct operand_entry temp = *oe3;
3448 oe3->op = oe1->op;
3449 oe3->rank = oe1->rank;
3450 oe1->op = temp.op;
3451 oe1->rank= temp.rank;
3453 else if ((oe1->rank == oe3->rank
3454 && oe2->rank != oe3->rank)
3455 || (stmt && is_phi_for_stmt (stmt, oe2->op)
3456 && !is_phi_for_stmt (stmt, oe1->op)
3457 && !is_phi_for_stmt (stmt, oe3->op)))
3459 struct operand_entry temp = *oe2;
3460 oe2->op = oe1->op;
3461 oe2->rank = oe1->rank;
3462 oe1->op = temp.op;
3463 oe1->rank = temp.rank;
3467 /* If definition of RHS1 or RHS2 dominates STMT, return the later of those
3468 two definitions, otherwise return STMT. */
3470 static inline gimple
3471 find_insert_point (gimple stmt, tree rhs1, tree rhs2)
3473 if (TREE_CODE (rhs1) == SSA_NAME
3474 && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs1)))
3475 stmt = SSA_NAME_DEF_STMT (rhs1);
3476 if (TREE_CODE (rhs2) == SSA_NAME
3477 && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs2)))
3478 stmt = SSA_NAME_DEF_STMT (rhs2);
3479 return stmt;
3482 /* Recursively rewrite our linearized statements so that the operators
3483 match those in OPS[OPINDEX], putting the computation in rank
3484 order. Return new lhs. */
3486 static tree
3487 rewrite_expr_tree (gimple stmt, unsigned int opindex,
3488 vec<operand_entry_t> ops, bool changed)
3490 tree rhs1 = gimple_assign_rhs1 (stmt);
3491 tree rhs2 = gimple_assign_rhs2 (stmt);
3492 tree lhs = gimple_assign_lhs (stmt);
3493 operand_entry_t oe;
3495 /* The final recursion case for this function is that you have
3496 exactly two operations left.
3497 If we had one exactly one op in the entire list to start with, we
3498 would have never called this function, and the tail recursion
3499 rewrites them one at a time. */
3500 if (opindex + 2 == ops.length ())
3502 operand_entry_t oe1, oe2;
3504 oe1 = ops[opindex];
3505 oe2 = ops[opindex + 1];
3507 if (rhs1 != oe1->op || rhs2 != oe2->op)
3509 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3510 unsigned int uid = gimple_uid (stmt);
3512 if (dump_file && (dump_flags & TDF_DETAILS))
3514 fprintf (dump_file, "Transforming ");
3515 print_gimple_stmt (dump_file, stmt, 0, 0);
3518 if (changed)
3520 gimple insert_point = find_insert_point (stmt, oe1->op, oe2->op);
3521 lhs = make_ssa_name (TREE_TYPE (lhs), NULL);
3522 stmt
3523 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt),
3524 lhs, oe1->op, oe2->op);
3525 gimple_set_uid (stmt, uid);
3526 gimple_set_visited (stmt, true);
3527 if (insert_point == gsi_stmt (gsi))
3528 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
3529 else
3530 insert_stmt_after (stmt, insert_point);
3532 else
3534 gcc_checking_assert (find_insert_point (stmt, oe1->op, oe2->op)
3535 == stmt);
3536 gimple_assign_set_rhs1 (stmt, oe1->op);
3537 gimple_assign_set_rhs2 (stmt, oe2->op);
3538 update_stmt (stmt);
3541 if (rhs1 != oe1->op && rhs1 != oe2->op)
3542 remove_visited_stmt_chain (rhs1);
3544 if (dump_file && (dump_flags & TDF_DETAILS))
3546 fprintf (dump_file, " into ");
3547 print_gimple_stmt (dump_file, stmt, 0, 0);
3550 return lhs;
3553 /* If we hit here, we should have 3 or more ops left. */
3554 gcc_assert (opindex + 2 < ops.length ());
3556 /* Rewrite the next operator. */
3557 oe = ops[opindex];
3559 /* Recurse on the LHS of the binary operator, which is guaranteed to
3560 be the non-leaf side. */
3561 tree new_rhs1
3562 = rewrite_expr_tree (SSA_NAME_DEF_STMT (rhs1), opindex + 1, ops,
3563 changed || oe->op != rhs2);
3565 if (oe->op != rhs2 || new_rhs1 != rhs1)
3567 if (dump_file && (dump_flags & TDF_DETAILS))
3569 fprintf (dump_file, "Transforming ");
3570 print_gimple_stmt (dump_file, stmt, 0, 0);
3573 /* If changed is false, this is either opindex == 0
3574 or all outer rhs2's were equal to corresponding oe->op,
3575 and powi_result is NULL.
3576 That means lhs is equivalent before and after reassociation.
3577 Otherwise ensure the old lhs SSA_NAME is not reused and
3578 create a new stmt as well, so that any debug stmts will be
3579 properly adjusted. */
3580 if (changed)
3582 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3583 unsigned int uid = gimple_uid (stmt);
3584 gimple insert_point = find_insert_point (stmt, new_rhs1, oe->op);
3586 lhs = make_ssa_name (TREE_TYPE (lhs), NULL);
3587 stmt = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt),
3588 lhs, new_rhs1, oe->op);
3589 gimple_set_uid (stmt, uid);
3590 gimple_set_visited (stmt, true);
3591 if (insert_point == gsi_stmt (gsi))
3592 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
3593 else
3594 insert_stmt_after (stmt, insert_point);
3596 else
3598 gcc_checking_assert (find_insert_point (stmt, new_rhs1, oe->op)
3599 == stmt);
3600 gimple_assign_set_rhs1 (stmt, new_rhs1);
3601 gimple_assign_set_rhs2 (stmt, oe->op);
3602 update_stmt (stmt);
3605 if (dump_file && (dump_flags & TDF_DETAILS))
3607 fprintf (dump_file, " into ");
3608 print_gimple_stmt (dump_file, stmt, 0, 0);
3611 return lhs;
3614 /* Find out how many cycles we need to compute statements chain.
3615 OPS_NUM holds number os statements in a chain. CPU_WIDTH is a
3616 maximum number of independent statements we may execute per cycle. */
3618 static int
3619 get_required_cycles (int ops_num, int cpu_width)
3621 int res;
3622 int elog;
3623 unsigned int rest;
3625 /* While we have more than 2 * cpu_width operands
3626 we may reduce number of operands by cpu_width
3627 per cycle. */
3628 res = ops_num / (2 * cpu_width);
3630 /* Remained operands count may be reduced twice per cycle
3631 until we have only one operand. */
3632 rest = (unsigned)(ops_num - res * cpu_width);
3633 elog = exact_log2 (rest);
3634 if (elog >= 0)
3635 res += elog;
3636 else
3637 res += floor_log2 (rest) + 1;
3639 return res;
3642 /* Returns an optimal number of registers to use for computation of
3643 given statements. */
3645 static int
3646 get_reassociation_width (int ops_num, enum tree_code opc,
3647 machine_mode mode)
3649 int param_width = PARAM_VALUE (PARAM_TREE_REASSOC_WIDTH);
3650 int width;
3651 int width_min;
3652 int cycles_best;
3654 if (param_width > 0)
3655 width = param_width;
3656 else
3657 width = targetm.sched.reassociation_width (opc, mode);
3659 if (width == 1)
3660 return width;
3662 /* Get the minimal time required for sequence computation. */
3663 cycles_best = get_required_cycles (ops_num, width);
3665 /* Check if we may use less width and still compute sequence for
3666 the same time. It will allow us to reduce registers usage.
3667 get_required_cycles is monotonically increasing with lower width
3668 so we can perform a binary search for the minimal width that still
3669 results in the optimal cycle count. */
3670 width_min = 1;
3671 while (width > width_min)
3673 int width_mid = (width + width_min) / 2;
3675 if (get_required_cycles (ops_num, width_mid) == cycles_best)
3676 width = width_mid;
3677 else if (width_min < width_mid)
3678 width_min = width_mid;
3679 else
3680 break;
3683 return width;
3686 /* Recursively rewrite our linearized statements so that the operators
3687 match those in OPS[OPINDEX], putting the computation in rank
3688 order and trying to allow operations to be executed in
3689 parallel. */
3691 static void
3692 rewrite_expr_tree_parallel (gassign *stmt, int width,
3693 vec<operand_entry_t> ops)
3695 enum tree_code opcode = gimple_assign_rhs_code (stmt);
3696 int op_num = ops.length ();
3697 int stmt_num = op_num - 1;
3698 gimple *stmts = XALLOCAVEC (gimple, stmt_num);
3699 int op_index = op_num - 1;
3700 int stmt_index = 0;
3701 int ready_stmts_end = 0;
3702 int i = 0;
3703 tree last_rhs1 = gimple_assign_rhs1 (stmt);
3705 /* We start expression rewriting from the top statements.
3706 So, in this loop we create a full list of statements
3707 we will work with. */
3708 stmts[stmt_num - 1] = stmt;
3709 for (i = stmt_num - 2; i >= 0; i--)
3710 stmts[i] = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmts[i+1]));
3712 for (i = 0; i < stmt_num; i++)
3714 tree op1, op2;
3716 /* Determine whether we should use results of
3717 already handled statements or not. */
3718 if (ready_stmts_end == 0
3719 && (i - stmt_index >= width || op_index < 1))
3720 ready_stmts_end = i;
3722 /* Now we choose operands for the next statement. Non zero
3723 value in ready_stmts_end means here that we should use
3724 the result of already generated statements as new operand. */
3725 if (ready_stmts_end > 0)
3727 op1 = gimple_assign_lhs (stmts[stmt_index++]);
3728 if (ready_stmts_end > stmt_index)
3729 op2 = gimple_assign_lhs (stmts[stmt_index++]);
3730 else if (op_index >= 0)
3731 op2 = ops[op_index--]->op;
3732 else
3734 gcc_assert (stmt_index < i);
3735 op2 = gimple_assign_lhs (stmts[stmt_index++]);
3738 if (stmt_index >= ready_stmts_end)
3739 ready_stmts_end = 0;
3741 else
3743 if (op_index > 1)
3744 swap_ops_for_binary_stmt (ops, op_index - 2, NULL);
3745 op2 = ops[op_index--]->op;
3746 op1 = ops[op_index--]->op;
3749 /* If we emit the last statement then we should put
3750 operands into the last statement. It will also
3751 break the loop. */
3752 if (op_index < 0 && stmt_index == i)
3753 i = stmt_num - 1;
3755 if (dump_file && (dump_flags & TDF_DETAILS))
3757 fprintf (dump_file, "Transforming ");
3758 print_gimple_stmt (dump_file, stmts[i], 0, 0);
3761 /* We keep original statement only for the last one. All
3762 others are recreated. */
3763 if (i == stmt_num - 1)
3765 gimple_assign_set_rhs1 (stmts[i], op1);
3766 gimple_assign_set_rhs2 (stmts[i], op2);
3767 update_stmt (stmts[i]);
3769 else
3770 stmts[i] = build_and_add_sum (TREE_TYPE (last_rhs1), op1, op2, opcode);
3772 if (dump_file && (dump_flags & TDF_DETAILS))
3774 fprintf (dump_file, " into ");
3775 print_gimple_stmt (dump_file, stmts[i], 0, 0);
3779 remove_visited_stmt_chain (last_rhs1);
3782 /* Transform STMT, which is really (A +B) + (C + D) into the left
3783 linear form, ((A+B)+C)+D.
3784 Recurse on D if necessary. */
3786 static void
3787 linearize_expr (gimple stmt)
3789 gimple_stmt_iterator gsi;
3790 gimple binlhs = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3791 gimple binrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
3792 gimple oldbinrhs = binrhs;
3793 enum tree_code rhscode = gimple_assign_rhs_code (stmt);
3794 gimple newbinrhs = NULL;
3795 struct loop *loop = loop_containing_stmt (stmt);
3796 tree lhs = gimple_assign_lhs (stmt);
3798 gcc_assert (is_reassociable_op (binlhs, rhscode, loop)
3799 && is_reassociable_op (binrhs, rhscode, loop));
3801 gsi = gsi_for_stmt (stmt);
3803 gimple_assign_set_rhs2 (stmt, gimple_assign_rhs1 (binrhs));
3804 binrhs = gimple_build_assign_with_ops (gimple_assign_rhs_code (binrhs),
3805 make_ssa_name (TREE_TYPE (lhs), NULL),
3806 gimple_assign_lhs (binlhs),
3807 gimple_assign_rhs2 (binrhs));
3808 gimple_assign_set_rhs1 (stmt, gimple_assign_lhs (binrhs));
3809 gsi_insert_before (&gsi, binrhs, GSI_SAME_STMT);
3810 gimple_set_uid (binrhs, gimple_uid (stmt));
3812 if (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
3813 newbinrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
3815 if (dump_file && (dump_flags & TDF_DETAILS))
3817 fprintf (dump_file, "Linearized: ");
3818 print_gimple_stmt (dump_file, stmt, 0, 0);
3821 reassociate_stats.linearized++;
3822 update_stmt (stmt);
3824 gsi = gsi_for_stmt (oldbinrhs);
3825 reassoc_remove_stmt (&gsi);
3826 release_defs (oldbinrhs);
3828 gimple_set_visited (stmt, true);
3829 gimple_set_visited (binlhs, true);
3830 gimple_set_visited (binrhs, true);
3832 /* Tail recurse on the new rhs if it still needs reassociation. */
3833 if (newbinrhs && is_reassociable_op (newbinrhs, rhscode, loop))
3834 /* ??? This should probably be linearize_expr (newbinrhs) but I don't
3835 want to change the algorithm while converting to tuples. */
3836 linearize_expr (stmt);
3839 /* If LHS has a single immediate use that is a GIMPLE_ASSIGN statement, return
3840 it. Otherwise, return NULL. */
3842 static gimple
3843 get_single_immediate_use (tree lhs)
3845 use_operand_p immuse;
3846 gimple immusestmt;
3848 if (TREE_CODE (lhs) == SSA_NAME
3849 && single_imm_use (lhs, &immuse, &immusestmt)
3850 && is_gimple_assign (immusestmt))
3851 return immusestmt;
3853 return NULL;
3856 /* Recursively negate the value of TONEGATE, and return the SSA_NAME
3857 representing the negated value. Insertions of any necessary
3858 instructions go before GSI.
3859 This function is recursive in that, if you hand it "a_5" as the
3860 value to negate, and a_5 is defined by "a_5 = b_3 + b_4", it will
3861 transform b_3 + b_4 into a_5 = -b_3 + -b_4. */
3863 static tree
3864 negate_value (tree tonegate, gimple_stmt_iterator *gsip)
3866 gimple negatedefstmt = NULL;
3867 tree resultofnegate;
3868 gimple_stmt_iterator gsi;
3869 unsigned int uid;
3871 /* If we are trying to negate a name, defined by an add, negate the
3872 add operands instead. */
3873 if (TREE_CODE (tonegate) == SSA_NAME)
3874 negatedefstmt = SSA_NAME_DEF_STMT (tonegate);
3875 if (TREE_CODE (tonegate) == SSA_NAME
3876 && is_gimple_assign (negatedefstmt)
3877 && TREE_CODE (gimple_assign_lhs (negatedefstmt)) == SSA_NAME
3878 && has_single_use (gimple_assign_lhs (negatedefstmt))
3879 && gimple_assign_rhs_code (negatedefstmt) == PLUS_EXPR)
3881 tree rhs1 = gimple_assign_rhs1 (negatedefstmt);
3882 tree rhs2 = gimple_assign_rhs2 (negatedefstmt);
3883 tree lhs = gimple_assign_lhs (negatedefstmt);
3884 gimple g;
3886 gsi = gsi_for_stmt (negatedefstmt);
3887 rhs1 = negate_value (rhs1, &gsi);
3889 gsi = gsi_for_stmt (negatedefstmt);
3890 rhs2 = negate_value (rhs2, &gsi);
3892 gsi = gsi_for_stmt (negatedefstmt);
3893 lhs = make_ssa_name (TREE_TYPE (lhs), NULL);
3894 gimple_set_visited (negatedefstmt, true);
3895 g = gimple_build_assign_with_ops (PLUS_EXPR, lhs, rhs1, rhs2);
3896 gimple_set_uid (g, gimple_uid (negatedefstmt));
3897 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3898 return lhs;
3901 tonegate = fold_build1 (NEGATE_EXPR, TREE_TYPE (tonegate), tonegate);
3902 resultofnegate = force_gimple_operand_gsi (gsip, tonegate, true,
3903 NULL_TREE, true, GSI_SAME_STMT);
3904 gsi = *gsip;
3905 uid = gimple_uid (gsi_stmt (gsi));
3906 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3908 gimple stmt = gsi_stmt (gsi);
3909 if (gimple_uid (stmt) != 0)
3910 break;
3911 gimple_set_uid (stmt, uid);
3913 return resultofnegate;
3916 /* Return true if we should break up the subtract in STMT into an add
3917 with negate. This is true when we the subtract operands are really
3918 adds, or the subtract itself is used in an add expression. In
3919 either case, breaking up the subtract into an add with negate
3920 exposes the adds to reassociation. */
3922 static bool
3923 should_break_up_subtract (gimple stmt)
3925 tree lhs = gimple_assign_lhs (stmt);
3926 tree binlhs = gimple_assign_rhs1 (stmt);
3927 tree binrhs = gimple_assign_rhs2 (stmt);
3928 gimple immusestmt;
3929 struct loop *loop = loop_containing_stmt (stmt);
3931 if (TREE_CODE (binlhs) == SSA_NAME
3932 && is_reassociable_op (SSA_NAME_DEF_STMT (binlhs), PLUS_EXPR, loop))
3933 return true;
3935 if (TREE_CODE (binrhs) == SSA_NAME
3936 && is_reassociable_op (SSA_NAME_DEF_STMT (binrhs), PLUS_EXPR, loop))
3937 return true;
3939 if (TREE_CODE (lhs) == SSA_NAME
3940 && (immusestmt = get_single_immediate_use (lhs))
3941 && is_gimple_assign (immusestmt)
3942 && (gimple_assign_rhs_code (immusestmt) == PLUS_EXPR
3943 || gimple_assign_rhs_code (immusestmt) == MULT_EXPR))
3944 return true;
3945 return false;
3948 /* Transform STMT from A - B into A + -B. */
3950 static void
3951 break_up_subtract (gimple stmt, gimple_stmt_iterator *gsip)
3953 tree rhs1 = gimple_assign_rhs1 (stmt);
3954 tree rhs2 = gimple_assign_rhs2 (stmt);
3956 if (dump_file && (dump_flags & TDF_DETAILS))
3958 fprintf (dump_file, "Breaking up subtract ");
3959 print_gimple_stmt (dump_file, stmt, 0, 0);
3962 rhs2 = negate_value (rhs2, gsip);
3963 gimple_assign_set_rhs_with_ops (gsip, PLUS_EXPR, rhs1, rhs2);
3964 update_stmt (stmt);
3967 /* Determine whether STMT is a builtin call that raises an SSA name
3968 to an integer power and has only one use. If so, and this is early
3969 reassociation and unsafe math optimizations are permitted, place
3970 the SSA name in *BASE and the exponent in *EXPONENT, and return TRUE.
3971 If any of these conditions does not hold, return FALSE. */
3973 static bool
3974 acceptable_pow_call (gimple stmt, tree *base, HOST_WIDE_INT *exponent)
3976 tree fndecl, arg1;
3977 REAL_VALUE_TYPE c, cint;
3979 if (!first_pass_instance
3980 || !flag_unsafe_math_optimizations
3981 || !is_gimple_call (stmt)
3982 || !has_single_use (gimple_call_lhs (stmt)))
3983 return false;
3985 fndecl = gimple_call_fndecl (stmt);
3987 if (!fndecl
3988 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3989 return false;
3991 switch (DECL_FUNCTION_CODE (fndecl))
3993 CASE_FLT_FN (BUILT_IN_POW):
3994 *base = gimple_call_arg (stmt, 0);
3995 arg1 = gimple_call_arg (stmt, 1);
3997 if (TREE_CODE (arg1) != REAL_CST)
3998 return false;
4000 c = TREE_REAL_CST (arg1);
4002 if (REAL_EXP (&c) > HOST_BITS_PER_WIDE_INT)
4003 return false;
4005 *exponent = real_to_integer (&c);
4006 real_from_integer (&cint, VOIDmode, *exponent, SIGNED);
4007 if (!real_identical (&c, &cint))
4008 return false;
4010 break;
4012 CASE_FLT_FN (BUILT_IN_POWI):
4013 *base = gimple_call_arg (stmt, 0);
4014 arg1 = gimple_call_arg (stmt, 1);
4016 if (!tree_fits_shwi_p (arg1))
4017 return false;
4019 *exponent = tree_to_shwi (arg1);
4020 break;
4022 default:
4023 return false;
4026 /* Expanding negative exponents is generally unproductive, so we don't
4027 complicate matters with those. Exponents of zero and one should
4028 have been handled by expression folding. */
4029 if (*exponent < 2 || TREE_CODE (*base) != SSA_NAME)
4030 return false;
4032 return true;
4035 /* Recursively linearize a binary expression that is the RHS of STMT.
4036 Place the operands of the expression tree in the vector named OPS. */
4038 static void
4039 linearize_expr_tree (vec<operand_entry_t> *ops, gimple stmt,
4040 bool is_associative, bool set_visited)
4042 tree binlhs = gimple_assign_rhs1 (stmt);
4043 tree binrhs = gimple_assign_rhs2 (stmt);
4044 gimple binlhsdef = NULL, binrhsdef = NULL;
4045 bool binlhsisreassoc = false;
4046 bool binrhsisreassoc = false;
4047 enum tree_code rhscode = gimple_assign_rhs_code (stmt);
4048 struct loop *loop = loop_containing_stmt (stmt);
4049 tree base = NULL_TREE;
4050 HOST_WIDE_INT exponent = 0;
4052 if (set_visited)
4053 gimple_set_visited (stmt, true);
4055 if (TREE_CODE (binlhs) == SSA_NAME)
4057 binlhsdef = SSA_NAME_DEF_STMT (binlhs);
4058 binlhsisreassoc = (is_reassociable_op (binlhsdef, rhscode, loop)
4059 && !stmt_could_throw_p (binlhsdef));
4062 if (TREE_CODE (binrhs) == SSA_NAME)
4064 binrhsdef = SSA_NAME_DEF_STMT (binrhs);
4065 binrhsisreassoc = (is_reassociable_op (binrhsdef, rhscode, loop)
4066 && !stmt_could_throw_p (binrhsdef));
4069 /* If the LHS is not reassociable, but the RHS is, we need to swap
4070 them. If neither is reassociable, there is nothing we can do, so
4071 just put them in the ops vector. If the LHS is reassociable,
4072 linearize it. If both are reassociable, then linearize the RHS
4073 and the LHS. */
4075 if (!binlhsisreassoc)
4077 tree temp;
4079 /* If this is not a associative operation like division, give up. */
4080 if (!is_associative)
4082 add_to_ops_vec (ops, binrhs);
4083 return;
4086 if (!binrhsisreassoc)
4088 if (rhscode == MULT_EXPR
4089 && TREE_CODE (binrhs) == SSA_NAME
4090 && acceptable_pow_call (binrhsdef, &base, &exponent))
4092 add_repeat_to_ops_vec (ops, base, exponent);
4093 gimple_set_visited (binrhsdef, true);
4095 else
4096 add_to_ops_vec (ops, binrhs);
4098 if (rhscode == MULT_EXPR
4099 && TREE_CODE (binlhs) == SSA_NAME
4100 && acceptable_pow_call (binlhsdef, &base, &exponent))
4102 add_repeat_to_ops_vec (ops, base, exponent);
4103 gimple_set_visited (binlhsdef, true);
4105 else
4106 add_to_ops_vec (ops, binlhs);
4108 return;
4111 if (dump_file && (dump_flags & TDF_DETAILS))
4113 fprintf (dump_file, "swapping operands of ");
4114 print_gimple_stmt (dump_file, stmt, 0, 0);
4117 swap_ssa_operands (stmt,
4118 gimple_assign_rhs1_ptr (stmt),
4119 gimple_assign_rhs2_ptr (stmt));
4120 update_stmt (stmt);
4122 if (dump_file && (dump_flags & TDF_DETAILS))
4124 fprintf (dump_file, " is now ");
4125 print_gimple_stmt (dump_file, stmt, 0, 0);
4128 /* We want to make it so the lhs is always the reassociative op,
4129 so swap. */
4130 temp = binlhs;
4131 binlhs = binrhs;
4132 binrhs = temp;
4134 else if (binrhsisreassoc)
4136 linearize_expr (stmt);
4137 binlhs = gimple_assign_rhs1 (stmt);
4138 binrhs = gimple_assign_rhs2 (stmt);
4141 gcc_assert (TREE_CODE (binrhs) != SSA_NAME
4142 || !is_reassociable_op (SSA_NAME_DEF_STMT (binrhs),
4143 rhscode, loop));
4144 linearize_expr_tree (ops, SSA_NAME_DEF_STMT (binlhs),
4145 is_associative, set_visited);
4147 if (rhscode == MULT_EXPR
4148 && TREE_CODE (binrhs) == SSA_NAME
4149 && acceptable_pow_call (SSA_NAME_DEF_STMT (binrhs), &base, &exponent))
4151 add_repeat_to_ops_vec (ops, base, exponent);
4152 gimple_set_visited (SSA_NAME_DEF_STMT (binrhs), true);
4154 else
4155 add_to_ops_vec (ops, binrhs);
4158 /* Repropagate the negates back into subtracts, since no other pass
4159 currently does it. */
4161 static void
4162 repropagate_negates (void)
4164 unsigned int i = 0;
4165 tree negate;
4167 FOR_EACH_VEC_ELT (plus_negates, i, negate)
4169 gimple user = get_single_immediate_use (negate);
4171 if (!user || !is_gimple_assign (user))
4172 continue;
4174 /* The negate operand can be either operand of a PLUS_EXPR
4175 (it can be the LHS if the RHS is a constant for example).
4177 Force the negate operand to the RHS of the PLUS_EXPR, then
4178 transform the PLUS_EXPR into a MINUS_EXPR. */
4179 if (gimple_assign_rhs_code (user) == PLUS_EXPR)
4181 /* If the negated operand appears on the LHS of the
4182 PLUS_EXPR, exchange the operands of the PLUS_EXPR
4183 to force the negated operand to the RHS of the PLUS_EXPR. */
4184 if (gimple_assign_rhs1 (user) == negate)
4186 swap_ssa_operands (user,
4187 gimple_assign_rhs1_ptr (user),
4188 gimple_assign_rhs2_ptr (user));
4191 /* Now transform the PLUS_EXPR into a MINUS_EXPR and replace
4192 the RHS of the PLUS_EXPR with the operand of the NEGATE_EXPR. */
4193 if (gimple_assign_rhs2 (user) == negate)
4195 tree rhs1 = gimple_assign_rhs1 (user);
4196 tree rhs2 = get_unary_op (negate, NEGATE_EXPR);
4197 gimple_stmt_iterator gsi = gsi_for_stmt (user);
4198 gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, rhs1, rhs2);
4199 update_stmt (user);
4202 else if (gimple_assign_rhs_code (user) == MINUS_EXPR)
4204 if (gimple_assign_rhs1 (user) == negate)
4206 /* We have
4207 x = -a
4208 y = x - b
4209 which we transform into
4210 x = a + b
4211 y = -x .
4212 This pushes down the negate which we possibly can merge
4213 into some other operation, hence insert it into the
4214 plus_negates vector. */
4215 gimple feed = SSA_NAME_DEF_STMT (negate);
4216 tree a = gimple_assign_rhs1 (feed);
4217 tree b = gimple_assign_rhs2 (user);
4218 gimple_stmt_iterator gsi = gsi_for_stmt (feed);
4219 gimple_stmt_iterator gsi2 = gsi_for_stmt (user);
4220 tree x = make_ssa_name (TREE_TYPE (gimple_assign_lhs (feed)),
4221 NULL);
4222 gimple g = gimple_build_assign_with_ops (PLUS_EXPR, x, a, b);
4223 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
4224 gimple_assign_set_rhs_with_ops (&gsi2, NEGATE_EXPR, x);
4225 user = gsi_stmt (gsi2);
4226 update_stmt (user);
4227 reassoc_remove_stmt (&gsi);
4228 release_defs (feed);
4229 plus_negates.safe_push (gimple_assign_lhs (user));
4231 else
4233 /* Transform "x = -a; y = b - x" into "y = b + a", getting
4234 rid of one operation. */
4235 gimple feed = SSA_NAME_DEF_STMT (negate);
4236 tree a = gimple_assign_rhs1 (feed);
4237 tree rhs1 = gimple_assign_rhs1 (user);
4238 gimple_stmt_iterator gsi = gsi_for_stmt (user);
4239 gimple_assign_set_rhs_with_ops (&gsi, PLUS_EXPR, rhs1, a);
4240 update_stmt (gsi_stmt (gsi));
4246 /* Returns true if OP is of a type for which we can do reassociation.
4247 That is for integral or non-saturating fixed-point types, and for
4248 floating point type when associative-math is enabled. */
4250 static bool
4251 can_reassociate_p (tree op)
4253 tree type = TREE_TYPE (op);
4254 if ((INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
4255 || NON_SAT_FIXED_POINT_TYPE_P (type)
4256 || (flag_associative_math && FLOAT_TYPE_P (type)))
4257 return true;
4258 return false;
4261 /* Break up subtract operations in block BB.
4263 We do this top down because we don't know whether the subtract is
4264 part of a possible chain of reassociation except at the top.
4266 IE given
4267 d = f + g
4268 c = a + e
4269 b = c - d
4270 q = b - r
4271 k = t - q
4273 we want to break up k = t - q, but we won't until we've transformed q
4274 = b - r, which won't be broken up until we transform b = c - d.
4276 En passant, clear the GIMPLE visited flag on every statement
4277 and set UIDs within each basic block. */
4279 static void
4280 break_up_subtract_bb (basic_block bb)
4282 gimple_stmt_iterator gsi;
4283 basic_block son;
4284 unsigned int uid = 1;
4286 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4288 gimple stmt = gsi_stmt (gsi);
4289 gimple_set_visited (stmt, false);
4290 gimple_set_uid (stmt, uid++);
4292 if (!is_gimple_assign (stmt)
4293 || !can_reassociate_p (gimple_assign_lhs (stmt)))
4294 continue;
4296 /* Look for simple gimple subtract operations. */
4297 if (gimple_assign_rhs_code (stmt) == MINUS_EXPR)
4299 if (!can_reassociate_p (gimple_assign_rhs1 (stmt))
4300 || !can_reassociate_p (gimple_assign_rhs2 (stmt)))
4301 continue;
4303 /* Check for a subtract used only in an addition. If this
4304 is the case, transform it into add of a negate for better
4305 reassociation. IE transform C = A-B into C = A + -B if C
4306 is only used in an addition. */
4307 if (should_break_up_subtract (stmt))
4308 break_up_subtract (stmt, &gsi);
4310 else if (gimple_assign_rhs_code (stmt) == NEGATE_EXPR
4311 && can_reassociate_p (gimple_assign_rhs1 (stmt)))
4312 plus_negates.safe_push (gimple_assign_lhs (stmt));
4314 for (son = first_dom_son (CDI_DOMINATORS, bb);
4315 son;
4316 son = next_dom_son (CDI_DOMINATORS, son))
4317 break_up_subtract_bb (son);
4320 /* Used for repeated factor analysis. */
4321 struct repeat_factor_d
4323 /* An SSA name that occurs in a multiply chain. */
4324 tree factor;
4326 /* Cached rank of the factor. */
4327 unsigned rank;
4329 /* Number of occurrences of the factor in the chain. */
4330 HOST_WIDE_INT count;
4332 /* An SSA name representing the product of this factor and
4333 all factors appearing later in the repeated factor vector. */
4334 tree repr;
4337 typedef struct repeat_factor_d repeat_factor, *repeat_factor_t;
4338 typedef const struct repeat_factor_d *const_repeat_factor_t;
4341 static vec<repeat_factor> repeat_factor_vec;
4343 /* Used for sorting the repeat factor vector. Sort primarily by
4344 ascending occurrence count, secondarily by descending rank. */
4346 static int
4347 compare_repeat_factors (const void *x1, const void *x2)
4349 const_repeat_factor_t rf1 = (const_repeat_factor_t) x1;
4350 const_repeat_factor_t rf2 = (const_repeat_factor_t) x2;
4352 if (rf1->count != rf2->count)
4353 return rf1->count - rf2->count;
4355 return rf2->rank - rf1->rank;
4358 /* Look for repeated operands in OPS in the multiply tree rooted at
4359 STMT. Replace them with an optimal sequence of multiplies and powi
4360 builtin calls, and remove the used operands from OPS. Return an
4361 SSA name representing the value of the replacement sequence. */
4363 static tree
4364 attempt_builtin_powi (gimple stmt, vec<operand_entry_t> *ops)
4366 unsigned i, j, vec_len;
4367 int ii;
4368 operand_entry_t oe;
4369 repeat_factor_t rf1, rf2;
4370 repeat_factor rfnew;
4371 tree result = NULL_TREE;
4372 tree target_ssa, iter_result;
4373 tree type = TREE_TYPE (gimple_get_lhs (stmt));
4374 tree powi_fndecl = mathfn_built_in (type, BUILT_IN_POWI);
4375 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4376 gimple mul_stmt, pow_stmt;
4378 /* Nothing to do if BUILT_IN_POWI doesn't exist for this type and
4379 target. */
4380 if (!powi_fndecl)
4381 return NULL_TREE;
4383 /* Allocate the repeated factor vector. */
4384 repeat_factor_vec.create (10);
4386 /* Scan the OPS vector for all SSA names in the product and build
4387 up a vector of occurrence counts for each factor. */
4388 FOR_EACH_VEC_ELT (*ops, i, oe)
4390 if (TREE_CODE (oe->op) == SSA_NAME)
4392 FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
4394 if (rf1->factor == oe->op)
4396 rf1->count += oe->count;
4397 break;
4401 if (j >= repeat_factor_vec.length ())
4403 rfnew.factor = oe->op;
4404 rfnew.rank = oe->rank;
4405 rfnew.count = oe->count;
4406 rfnew.repr = NULL_TREE;
4407 repeat_factor_vec.safe_push (rfnew);
4412 /* Sort the repeated factor vector by (a) increasing occurrence count,
4413 and (b) decreasing rank. */
4414 repeat_factor_vec.qsort (compare_repeat_factors);
4416 /* It is generally best to combine as many base factors as possible
4417 into a product before applying __builtin_powi to the result.
4418 However, the sort order chosen for the repeated factor vector
4419 allows us to cache partial results for the product of the base
4420 factors for subsequent use. When we already have a cached partial
4421 result from a previous iteration, it is best to make use of it
4422 before looking for another __builtin_pow opportunity.
4424 As an example, consider x * x * y * y * y * z * z * z * z.
4425 We want to first compose the product x * y * z, raise it to the
4426 second power, then multiply this by y * z, and finally multiply
4427 by z. This can be done in 5 multiplies provided we cache y * z
4428 for use in both expressions:
4430 t1 = y * z
4431 t2 = t1 * x
4432 t3 = t2 * t2
4433 t4 = t1 * t3
4434 result = t4 * z
4436 If we instead ignored the cached y * z and first multiplied by
4437 the __builtin_pow opportunity z * z, we would get the inferior:
4439 t1 = y * z
4440 t2 = t1 * x
4441 t3 = t2 * t2
4442 t4 = z * z
4443 t5 = t3 * t4
4444 result = t5 * y */
4446 vec_len = repeat_factor_vec.length ();
4448 /* Repeatedly look for opportunities to create a builtin_powi call. */
4449 while (true)
4451 HOST_WIDE_INT power;
4453 /* First look for the largest cached product of factors from
4454 preceding iterations. If found, create a builtin_powi for
4455 it if the minimum occurrence count for its factors is at
4456 least 2, or just use this cached product as our next
4457 multiplicand if the minimum occurrence count is 1. */
4458 FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
4460 if (rf1->repr && rf1->count > 0)
4461 break;
4464 if (j < vec_len)
4466 power = rf1->count;
4468 if (power == 1)
4470 iter_result = rf1->repr;
4472 if (dump_file && (dump_flags & TDF_DETAILS))
4474 unsigned elt;
4475 repeat_factor_t rf;
4476 fputs ("Multiplying by cached product ", dump_file);
4477 for (elt = j; elt < vec_len; elt++)
4479 rf = &repeat_factor_vec[elt];
4480 print_generic_expr (dump_file, rf->factor, 0);
4481 if (elt < vec_len - 1)
4482 fputs (" * ", dump_file);
4484 fputs ("\n", dump_file);
4487 else
4489 iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
4490 pow_stmt = gimple_build_call (powi_fndecl, 2, rf1->repr,
4491 build_int_cst (integer_type_node,
4492 power));
4493 gimple_call_set_lhs (pow_stmt, iter_result);
4494 gimple_set_location (pow_stmt, gimple_location (stmt));
4495 gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
4497 if (dump_file && (dump_flags & TDF_DETAILS))
4499 unsigned elt;
4500 repeat_factor_t rf;
4501 fputs ("Building __builtin_pow call for cached product (",
4502 dump_file);
4503 for (elt = j; elt < vec_len; elt++)
4505 rf = &repeat_factor_vec[elt];
4506 print_generic_expr (dump_file, rf->factor, 0);
4507 if (elt < vec_len - 1)
4508 fputs (" * ", dump_file);
4510 fprintf (dump_file, ")^"HOST_WIDE_INT_PRINT_DEC"\n",
4511 power);
4515 else
4517 /* Otherwise, find the first factor in the repeated factor
4518 vector whose occurrence count is at least 2. If no such
4519 factor exists, there are no builtin_powi opportunities
4520 remaining. */
4521 FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
4523 if (rf1->count >= 2)
4524 break;
4527 if (j >= vec_len)
4528 break;
4530 power = rf1->count;
4532 if (dump_file && (dump_flags & TDF_DETAILS))
4534 unsigned elt;
4535 repeat_factor_t rf;
4536 fputs ("Building __builtin_pow call for (", dump_file);
4537 for (elt = j; elt < vec_len; elt++)
4539 rf = &repeat_factor_vec[elt];
4540 print_generic_expr (dump_file, rf->factor, 0);
4541 if (elt < vec_len - 1)
4542 fputs (" * ", dump_file);
4544 fprintf (dump_file, ")^"HOST_WIDE_INT_PRINT_DEC"\n", power);
4547 reassociate_stats.pows_created++;
4549 /* Visit each element of the vector in reverse order (so that
4550 high-occurrence elements are visited first, and within the
4551 same occurrence count, lower-ranked elements are visited
4552 first). Form a linear product of all elements in this order
4553 whose occurrencce count is at least that of element J.
4554 Record the SSA name representing the product of each element
4555 with all subsequent elements in the vector. */
4556 if (j == vec_len - 1)
4557 rf1->repr = rf1->factor;
4558 else
4560 for (ii = vec_len - 2; ii >= (int)j; ii--)
4562 tree op1, op2;
4564 rf1 = &repeat_factor_vec[ii];
4565 rf2 = &repeat_factor_vec[ii + 1];
4567 /* Init the last factor's representative to be itself. */
4568 if (!rf2->repr)
4569 rf2->repr = rf2->factor;
4571 op1 = rf1->factor;
4572 op2 = rf2->repr;
4574 target_ssa = make_temp_ssa_name (type, NULL, "reassocpow");
4575 mul_stmt = gimple_build_assign_with_ops (MULT_EXPR,
4576 target_ssa,
4577 op1, op2);
4578 gimple_set_location (mul_stmt, gimple_location (stmt));
4579 gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
4580 rf1->repr = target_ssa;
4582 /* Don't reprocess the multiply we just introduced. */
4583 gimple_set_visited (mul_stmt, true);
4587 /* Form a call to __builtin_powi for the maximum product
4588 just formed, raised to the power obtained earlier. */
4589 rf1 = &repeat_factor_vec[j];
4590 iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
4591 pow_stmt = gimple_build_call (powi_fndecl, 2, rf1->repr,
4592 build_int_cst (integer_type_node,
4593 power));
4594 gimple_call_set_lhs (pow_stmt, iter_result);
4595 gimple_set_location (pow_stmt, gimple_location (stmt));
4596 gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
4599 /* If we previously formed at least one other builtin_powi call,
4600 form the product of this one and those others. */
4601 if (result)
4603 tree new_result = make_temp_ssa_name (type, NULL, "reassocpow");
4604 mul_stmt = gimple_build_assign_with_ops (MULT_EXPR, new_result,
4605 result, iter_result);
4606 gimple_set_location (mul_stmt, gimple_location (stmt));
4607 gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
4608 gimple_set_visited (mul_stmt, true);
4609 result = new_result;
4611 else
4612 result = iter_result;
4614 /* Decrement the occurrence count of each element in the product
4615 by the count found above, and remove this many copies of each
4616 factor from OPS. */
4617 for (i = j; i < vec_len; i++)
4619 unsigned k = power;
4620 unsigned n;
4622 rf1 = &repeat_factor_vec[i];
4623 rf1->count -= power;
4625 FOR_EACH_VEC_ELT_REVERSE (*ops, n, oe)
4627 if (oe->op == rf1->factor)
4629 if (oe->count <= k)
4631 ops->ordered_remove (n);
4632 k -= oe->count;
4634 if (k == 0)
4635 break;
4637 else
4639 oe->count -= k;
4640 break;
4647 /* At this point all elements in the repeated factor vector have a
4648 remaining occurrence count of 0 or 1, and those with a count of 1
4649 don't have cached representatives. Re-sort the ops vector and
4650 clean up. */
4651 ops->qsort (sort_by_operand_rank);
4652 repeat_factor_vec.release ();
4654 /* Return the final product computed herein. Note that there may
4655 still be some elements with single occurrence count left in OPS;
4656 those will be handled by the normal reassociation logic. */
4657 return result;
4660 /* Transform STMT at *GSI into a copy by replacing its rhs with NEW_RHS. */
4662 static void
4663 transform_stmt_to_copy (gimple_stmt_iterator *gsi, gimple stmt, tree new_rhs)
4665 tree rhs1;
4667 if (dump_file && (dump_flags & TDF_DETAILS))
4669 fprintf (dump_file, "Transforming ");
4670 print_gimple_stmt (dump_file, stmt, 0, 0);
4673 rhs1 = gimple_assign_rhs1 (stmt);
4674 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
4675 update_stmt (stmt);
4676 remove_visited_stmt_chain (rhs1);
4678 if (dump_file && (dump_flags & TDF_DETAILS))
4680 fprintf (dump_file, " into ");
4681 print_gimple_stmt (dump_file, stmt, 0, 0);
4685 /* Transform STMT at *GSI into a multiply of RHS1 and RHS2. */
4687 static void
4688 transform_stmt_to_multiply (gimple_stmt_iterator *gsi, gimple stmt,
4689 tree rhs1, tree rhs2)
4691 if (dump_file && (dump_flags & TDF_DETAILS))
4693 fprintf (dump_file, "Transforming ");
4694 print_gimple_stmt (dump_file, stmt, 0, 0);
4697 gimple_assign_set_rhs_with_ops (gsi, MULT_EXPR, rhs1, rhs2);
4698 update_stmt (gsi_stmt (*gsi));
4699 remove_visited_stmt_chain (rhs1);
4701 if (dump_file && (dump_flags & TDF_DETAILS))
4703 fprintf (dump_file, " into ");
4704 print_gimple_stmt (dump_file, stmt, 0, 0);
4708 /* Reassociate expressions in basic block BB and its post-dominator as
4709 children. */
4711 static void
4712 reassociate_bb (basic_block bb)
4714 gimple_stmt_iterator gsi;
4715 basic_block son;
4716 gimple stmt = last_stmt (bb);
4718 if (stmt && !gimple_visited_p (stmt))
4719 maybe_optimize_range_tests (stmt);
4721 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
4723 stmt = gsi_stmt (gsi);
4725 if (is_gimple_assign (stmt)
4726 && !stmt_could_throw_p (stmt))
4728 tree lhs, rhs1, rhs2;
4729 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4731 /* If this is not a gimple binary expression, there is
4732 nothing for us to do with it. */
4733 if (get_gimple_rhs_class (rhs_code) != GIMPLE_BINARY_RHS)
4734 continue;
4736 /* If this was part of an already processed statement,
4737 we don't need to touch it again. */
4738 if (gimple_visited_p (stmt))
4740 /* This statement might have become dead because of previous
4741 reassociations. */
4742 if (has_zero_uses (gimple_get_lhs (stmt)))
4744 reassoc_remove_stmt (&gsi);
4745 release_defs (stmt);
4746 /* We might end up removing the last stmt above which
4747 places the iterator to the end of the sequence.
4748 Reset it to the last stmt in this case which might
4749 be the end of the sequence as well if we removed
4750 the last statement of the sequence. In which case
4751 we need to bail out. */
4752 if (gsi_end_p (gsi))
4754 gsi = gsi_last_bb (bb);
4755 if (gsi_end_p (gsi))
4756 break;
4759 continue;
4762 lhs = gimple_assign_lhs (stmt);
4763 rhs1 = gimple_assign_rhs1 (stmt);
4764 rhs2 = gimple_assign_rhs2 (stmt);
4766 /* For non-bit or min/max operations we can't associate
4767 all types. Verify that here. */
4768 if (rhs_code != BIT_IOR_EXPR
4769 && rhs_code != BIT_AND_EXPR
4770 && rhs_code != BIT_XOR_EXPR
4771 && rhs_code != MIN_EXPR
4772 && rhs_code != MAX_EXPR
4773 && (!can_reassociate_p (lhs)
4774 || !can_reassociate_p (rhs1)
4775 || !can_reassociate_p (rhs2)))
4776 continue;
4778 if (associative_tree_code (rhs_code))
4780 auto_vec<operand_entry_t> ops;
4781 tree powi_result = NULL_TREE;
4783 /* There may be no immediate uses left by the time we
4784 get here because we may have eliminated them all. */
4785 if (TREE_CODE (lhs) == SSA_NAME && has_zero_uses (lhs))
4786 continue;
4788 gimple_set_visited (stmt, true);
4789 linearize_expr_tree (&ops, stmt, true, true);
4790 ops.qsort (sort_by_operand_rank);
4791 optimize_ops_list (rhs_code, &ops);
4792 if (undistribute_ops_list (rhs_code, &ops,
4793 loop_containing_stmt (stmt)))
4795 ops.qsort (sort_by_operand_rank);
4796 optimize_ops_list (rhs_code, &ops);
4799 if (rhs_code == BIT_IOR_EXPR || rhs_code == BIT_AND_EXPR)
4800 optimize_range_tests (rhs_code, &ops);
4802 if (first_pass_instance
4803 && rhs_code == MULT_EXPR
4804 && flag_unsafe_math_optimizations)
4805 powi_result = attempt_builtin_powi (stmt, &ops);
4807 /* If the operand vector is now empty, all operands were
4808 consumed by the __builtin_powi optimization. */
4809 if (ops.length () == 0)
4810 transform_stmt_to_copy (&gsi, stmt, powi_result);
4811 else if (ops.length () == 1)
4813 tree last_op = ops.last ()->op;
4815 if (powi_result)
4816 transform_stmt_to_multiply (&gsi, stmt, last_op,
4817 powi_result);
4818 else
4819 transform_stmt_to_copy (&gsi, stmt, last_op);
4821 else
4823 machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
4824 int ops_num = ops.length ();
4825 int width = get_reassociation_width (ops_num, rhs_code, mode);
4826 tree new_lhs = lhs;
4828 if (dump_file && (dump_flags & TDF_DETAILS))
4829 fprintf (dump_file,
4830 "Width = %d was chosen for reassociation\n", width);
4832 if (width > 1
4833 && ops.length () > 3)
4834 rewrite_expr_tree_parallel (as_a <gassign *> (stmt),
4835 width, ops);
4836 else
4838 /* When there are three operands left, we want
4839 to make sure the ones that get the double
4840 binary op are chosen wisely. */
4841 int len = ops.length ();
4842 if (len >= 3)
4843 swap_ops_for_binary_stmt (ops, len - 3, stmt);
4845 new_lhs = rewrite_expr_tree (stmt, 0, ops,
4846 powi_result != NULL);
4849 /* If we combined some repeated factors into a
4850 __builtin_powi call, multiply that result by the
4851 reassociated operands. */
4852 if (powi_result)
4854 gimple mul_stmt, lhs_stmt = SSA_NAME_DEF_STMT (lhs);
4855 tree type = TREE_TYPE (lhs);
4856 tree target_ssa = make_temp_ssa_name (type, NULL,
4857 "reassocpow");
4858 gimple_set_lhs (lhs_stmt, target_ssa);
4859 update_stmt (lhs_stmt);
4860 if (lhs != new_lhs)
4861 target_ssa = new_lhs;
4862 mul_stmt = gimple_build_assign_with_ops (MULT_EXPR, lhs,
4863 powi_result,
4864 target_ssa);
4865 gimple_set_location (mul_stmt, gimple_location (stmt));
4866 gsi_insert_after (&gsi, mul_stmt, GSI_NEW_STMT);
4872 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
4873 son;
4874 son = next_dom_son (CDI_POST_DOMINATORS, son))
4875 reassociate_bb (son);
4878 /* Add jumps around shifts for range tests turned into bit tests.
4879 For each SSA_NAME VAR we have code like:
4880 VAR = ...; // final stmt of range comparison
4881 // bit test here...;
4882 OTHERVAR = ...; // final stmt of the bit test sequence
4883 RES = VAR | OTHERVAR;
4884 Turn the above into:
4885 VAR = ...;
4886 if (VAR != 0)
4887 goto <l3>;
4888 else
4889 goto <l2>;
4890 <l2>:
4891 // bit test here...;
4892 OTHERVAR = ...;
4893 <l3>:
4894 # RES = PHI<1(l1), OTHERVAR(l2)>; */
4896 static void
4897 branch_fixup (void)
4899 tree var;
4900 unsigned int i;
4902 FOR_EACH_VEC_ELT (reassoc_branch_fixups, i, var)
4904 gimple def_stmt = SSA_NAME_DEF_STMT (var);
4905 gimple use_stmt;
4906 use_operand_p use;
4907 bool ok = single_imm_use (var, &use, &use_stmt);
4908 gcc_assert (ok
4909 && is_gimple_assign (use_stmt)
4910 && gimple_assign_rhs_code (use_stmt) == BIT_IOR_EXPR
4911 && gimple_bb (def_stmt) == gimple_bb (use_stmt));
4913 basic_block cond_bb = gimple_bb (def_stmt);
4914 basic_block then_bb = split_block (cond_bb, def_stmt)->dest;
4915 basic_block merge_bb = split_block (then_bb, use_stmt)->dest;
4917 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
4918 gimple g = gimple_build_cond (NE_EXPR, var,
4919 build_zero_cst (TREE_TYPE (var)),
4920 NULL_TREE, NULL_TREE);
4921 location_t loc = gimple_location (use_stmt);
4922 gimple_set_location (g, loc);
4923 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
4925 edge etrue = make_edge (cond_bb, merge_bb, EDGE_TRUE_VALUE);
4926 etrue->probability = REG_BR_PROB_BASE / 2;
4927 etrue->count = cond_bb->count / 2;
4928 edge efalse = find_edge (cond_bb, then_bb);
4929 efalse->flags = EDGE_FALSE_VALUE;
4930 efalse->probability -= etrue->probability;
4931 efalse->count -= etrue->count;
4932 then_bb->count -= etrue->count;
4934 tree othervar = NULL_TREE;
4935 if (gimple_assign_rhs1 (use_stmt) == var)
4936 othervar = gimple_assign_rhs2 (use_stmt);
4937 else if (gimple_assign_rhs2 (use_stmt) == var)
4938 othervar = gimple_assign_rhs1 (use_stmt);
4939 else
4940 gcc_unreachable ();
4941 tree lhs = gimple_assign_lhs (use_stmt);
4942 gphi *phi = create_phi_node (lhs, merge_bb);
4943 add_phi_arg (phi, build_one_cst (TREE_TYPE (lhs)), etrue, loc);
4944 add_phi_arg (phi, othervar, single_succ_edge (then_bb), loc);
4945 gsi = gsi_for_stmt (use_stmt);
4946 gsi_remove (&gsi, true);
4948 set_immediate_dominator (CDI_DOMINATORS, merge_bb, cond_bb);
4949 set_immediate_dominator (CDI_POST_DOMINATORS, cond_bb, merge_bb);
4951 reassoc_branch_fixups.release ();
4954 void dump_ops_vector (FILE *file, vec<operand_entry_t> ops);
4955 void debug_ops_vector (vec<operand_entry_t> ops);
4957 /* Dump the operand entry vector OPS to FILE. */
4959 void
4960 dump_ops_vector (FILE *file, vec<operand_entry_t> ops)
4962 operand_entry_t oe;
4963 unsigned int i;
4965 FOR_EACH_VEC_ELT (ops, i, oe)
4967 fprintf (file, "Op %d -> rank: %d, tree: ", i, oe->rank);
4968 print_generic_expr (file, oe->op, 0);
4972 /* Dump the operand entry vector OPS to STDERR. */
4974 DEBUG_FUNCTION void
4975 debug_ops_vector (vec<operand_entry_t> ops)
4977 dump_ops_vector (stderr, ops);
4980 static void
4981 do_reassoc (void)
4983 break_up_subtract_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4984 reassociate_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
4987 /* Initialize the reassociation pass. */
4989 static void
4990 init_reassoc (void)
4992 int i;
4993 long rank = 2;
4994 int *bbs = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4996 /* Find the loops, so that we can prevent moving calculations in
4997 them. */
4998 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5000 memset (&reassociate_stats, 0, sizeof (reassociate_stats));
5002 operand_entry_pool = create_alloc_pool ("operand entry pool",
5003 sizeof (struct operand_entry), 30);
5004 next_operand_entry_id = 0;
5006 /* Reverse RPO (Reverse Post Order) will give us something where
5007 deeper loops come later. */
5008 pre_and_rev_post_order_compute (NULL, bbs, false);
5009 bb_rank = XCNEWVEC (long, last_basic_block_for_fn (cfun));
5010 operand_rank = new hash_map<tree, long>;
5012 /* Give each default definition a distinct rank. This includes
5013 parameters and the static chain. Walk backwards over all
5014 SSA names so that we get proper rank ordering according
5015 to tree_swap_operands_p. */
5016 for (i = num_ssa_names - 1; i > 0; --i)
5018 tree name = ssa_name (i);
5019 if (name && SSA_NAME_IS_DEFAULT_DEF (name))
5020 insert_operand_rank (name, ++rank);
5023 /* Set up rank for each BB */
5024 for (i = 0; i < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; i++)
5025 bb_rank[bbs[i]] = ++rank << 16;
5027 free (bbs);
5028 calculate_dominance_info (CDI_POST_DOMINATORS);
5029 plus_negates = vNULL;
5032 /* Cleanup after the reassociation pass, and print stats if
5033 requested. */
5035 static void
5036 fini_reassoc (void)
5038 statistics_counter_event (cfun, "Linearized",
5039 reassociate_stats.linearized);
5040 statistics_counter_event (cfun, "Constants eliminated",
5041 reassociate_stats.constants_eliminated);
5042 statistics_counter_event (cfun, "Ops eliminated",
5043 reassociate_stats.ops_eliminated);
5044 statistics_counter_event (cfun, "Statements rewritten",
5045 reassociate_stats.rewritten);
5046 statistics_counter_event (cfun, "Built-in pow[i] calls encountered",
5047 reassociate_stats.pows_encountered);
5048 statistics_counter_event (cfun, "Built-in powi calls created",
5049 reassociate_stats.pows_created);
5051 delete operand_rank;
5052 free_alloc_pool (operand_entry_pool);
5053 free (bb_rank);
5054 plus_negates.release ();
5055 free_dominance_info (CDI_POST_DOMINATORS);
5056 loop_optimizer_finalize ();
5059 /* Gate and execute functions for Reassociation. */
5061 static unsigned int
5062 execute_reassoc (void)
5064 init_reassoc ();
5066 do_reassoc ();
5067 repropagate_negates ();
5068 branch_fixup ();
5070 fini_reassoc ();
5071 return 0;
5074 namespace {
5076 const pass_data pass_data_reassoc =
5078 GIMPLE_PASS, /* type */
5079 "reassoc", /* name */
5080 OPTGROUP_NONE, /* optinfo_flags */
5081 TV_TREE_REASSOC, /* tv_id */
5082 ( PROP_cfg | PROP_ssa ), /* properties_required */
5083 0, /* properties_provided */
5084 0, /* properties_destroyed */
5085 0, /* todo_flags_start */
5086 TODO_update_ssa_only_virtuals, /* todo_flags_finish */
5089 class pass_reassoc : public gimple_opt_pass
5091 public:
5092 pass_reassoc (gcc::context *ctxt)
5093 : gimple_opt_pass (pass_data_reassoc, ctxt)
5096 /* opt_pass methods: */
5097 opt_pass * clone () { return new pass_reassoc (m_ctxt); }
5098 virtual bool gate (function *) { return flag_tree_reassoc != 0; }
5099 virtual unsigned int execute (function *) { return execute_reassoc (); }
5101 }; // class pass_reassoc
5103 } // anon namespace
5105 gimple_opt_pass *
5106 make_pass_reassoc (gcc::context *ctxt)
5108 return new pass_reassoc (ctxt);