1 /* Reassociation for trees.
2 Copyright (C) 2005-2020 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)
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/>. */
23 #include "coretypes.h"
30 #include "alloc-pool.h"
31 #include "tree-pass.h"
35 #include "optabs-tree.h"
36 #include "gimple-pretty-print.h"
37 #include "diagnostic-core.h"
38 #include "fold-const.h"
39 #include "stor-layout.h"
41 #include "gimple-fold.h"
43 #include "gimple-iterator.h"
44 #include "gimplify-me.h"
46 #include "tree-ssa-loop.h"
49 #include "langhooks.h"
53 #include "case-cfn-macros.h"
55 /* This is a simple global reassociation pass. It is, in part, based
56 on the LLVM pass of the same name (They do some things more/less
57 than we do, in different orders, etc).
59 It consists of five steps:
61 1. Breaking up subtract operations into addition + negate, where
62 it would promote the reassociation of adds.
64 2. Left linearization of the expression trees, so that (A+B)+(C+D)
65 becomes (((A+B)+C)+D), which is easier for us to rewrite later.
66 During linearization, we place the operands of the binary
67 expressions into a vector of operand_entry_*
69 3. Optimization of the operand lists, eliminating things like a +
72 3a. Combine repeated factors with the same occurrence counts
73 into a __builtin_powi call that will later be optimized into
74 an optimal number of multiplies.
76 4. Rewrite the expression trees we linearized and optimized so
77 they are in proper rank order.
79 5. Repropagate negates, as nothing else will clean it up ATM.
81 A bit of theory on #4, since nobody seems to write anything down
82 about why it makes sense to do it the way they do it:
84 We could do this much nicer theoretically, but don't (for reasons
85 explained after how to do it theoretically nice :P).
87 In order to promote the most redundancy elimination, you want
88 binary expressions whose operands are the same rank (or
89 preferably, the same value) exposed to the redundancy eliminator,
90 for possible elimination.
92 So the way to do this if we really cared, is to build the new op
93 tree from the leaves to the roots, merging as you go, and putting the
94 new op on the end of the worklist, until you are left with one
95 thing on the worklist.
97 IE if you have to rewrite the following set of operands (listed with
98 rank in parentheses), with opcode PLUS_EXPR:
100 a (1), b (1), c (1), d (2), e (2)
103 We start with our merge worklist empty, and the ops list with all of
106 You want to first merge all leaves of the same rank, as much as
109 So first build a binary op of
111 mergetmp = a + b, and put "mergetmp" on the merge worklist.
113 Because there is no three operand form of PLUS_EXPR, c is not going to
114 be exposed to redundancy elimination as a rank 1 operand.
116 So you might as well throw it on the merge worklist (you could also
117 consider it to now be a rank two operand, and merge it with d and e,
118 but in this case, you then have evicted e from a binary op. So at
119 least in this situation, you can't win.)
121 Then build a binary op of d + e
124 and put mergetmp2 on the merge worklist.
126 so merge worklist = {mergetmp, c, mergetmp2}
128 Continue building binary ops of these operations until you have only
129 one operation left on the worklist.
134 mergetmp3 = mergetmp + c
136 worklist = {mergetmp2, mergetmp3}
138 mergetmp4 = mergetmp2 + mergetmp3
140 worklist = {mergetmp4}
142 because we have one operation left, we can now just set the original
143 statement equal to the result of that operation.
145 This will at least expose a + b and d + e to redundancy elimination
146 as binary operations.
148 For extra points, you can reuse the old statements to build the
149 mergetmps, since you shouldn't run out.
151 So why don't we do this?
153 Because it's expensive, and rarely will help. Most trees we are
154 reassociating have 3 or less ops. If they have 2 ops, they already
155 will be written into a nice single binary op. If you have 3 ops, a
156 single simple check suffices to tell you whether the first two are of the
157 same rank. If so, you know to order it
160 newstmt = mergetmp + op3
164 newstmt = mergetmp + op1
166 If all three are of the same rank, you can't expose them all in a
167 single binary operator anyway, so the above is *still* the best you
170 Thus, this is what we do. When we have three ops left, we check to see
171 what order to put them in, and call it a day. As a nod to vector sum
172 reduction, we check if any of the ops are really a phi node that is a
173 destructive update for the associating op, and keep the destructive
174 update together for vector sum reduction recognition. */
176 /* Enable insertion of __builtin_powi calls during execute_reassoc. See
177 point 3a in the pass header comment. */
178 static bool reassoc_insert_powi_p
;
184 int constants_eliminated
;
187 int pows_encountered
;
191 /* Operator, rank pair. */
198 gimple
*stmt_to_insert
;
201 static object_allocator
<operand_entry
> operand_entry_pool
202 ("operand entry pool");
204 /* This is used to assign a unique ID to each struct operand_entry
205 so that qsort results are identical on different hosts. */
206 static unsigned int next_operand_entry_id
;
208 /* Starting rank number for a given basic block, so that we can rank
209 operations using unmovable instructions in that BB based on the bb
211 static long *bb_rank
;
213 /* Operand->rank hashtable. */
214 static hash_map
<tree
, long> *operand_rank
;
216 /* Vector of SSA_NAMEs on which after reassociate_bb is done with
217 all basic blocks the CFG should be adjusted - basic blocks
218 split right after that SSA_NAME's definition statement and before
219 the only use, which must be a bit ior. */
220 static vec
<tree
> reassoc_branch_fixups
;
223 static long get_rank (tree
);
224 static bool reassoc_stmt_dominates_stmt_p (gimple
*, gimple
*);
226 /* Wrapper around gsi_remove, which adjusts gimple_uid of debug stmts
227 possibly added by gsi_remove. */
230 reassoc_remove_stmt (gimple_stmt_iterator
*gsi
)
232 gimple
*stmt
= gsi_stmt (*gsi
);
234 if (!MAY_HAVE_DEBUG_BIND_STMTS
|| gimple_code (stmt
) == GIMPLE_PHI
)
235 return gsi_remove (gsi
, true);
237 gimple_stmt_iterator prev
= *gsi
;
239 unsigned uid
= gimple_uid (stmt
);
240 basic_block bb
= gimple_bb (stmt
);
241 bool ret
= gsi_remove (gsi
, true);
242 if (!gsi_end_p (prev
))
245 prev
= gsi_start_bb (bb
);
246 gimple
*end_stmt
= gsi_stmt (*gsi
);
247 while ((stmt
= gsi_stmt (prev
)) != end_stmt
)
249 gcc_assert (stmt
&& is_gimple_debug (stmt
) && gimple_uid (stmt
) == 0);
250 gimple_set_uid (stmt
, uid
);
256 /* Bias amount for loop-carried phis. We want this to be larger than
257 the depth of any reassociation tree we can see, but not larger than
258 the rank difference between two blocks. */
259 #define PHI_LOOP_BIAS (1 << 15)
261 /* Rank assigned to a phi statement. If STMT is a loop-carried phi of
262 an innermost loop, and the phi has only a single use which is inside
263 the loop, then the rank is the block rank of the loop latch plus an
264 extra bias for the loop-carried dependence. This causes expressions
265 calculated into an accumulator variable to be independent for each
266 iteration of the loop. If STMT is some other phi, the rank is the
267 block rank of its containing block. */
269 phi_rank (gimple
*stmt
)
271 basic_block bb
= gimple_bb (stmt
);
272 class loop
*father
= bb
->loop_father
;
278 /* We only care about real loops (those with a latch). */
280 return bb_rank
[bb
->index
];
282 /* Interesting phis must be in headers of innermost loops. */
283 if (bb
!= father
->header
285 return bb_rank
[bb
->index
];
287 /* Ignore virtual SSA_NAMEs. */
288 res
= gimple_phi_result (stmt
);
289 if (virtual_operand_p (res
))
290 return bb_rank
[bb
->index
];
292 /* The phi definition must have a single use, and that use must be
293 within the loop. Otherwise this isn't an accumulator pattern. */
294 if (!single_imm_use (res
, &use
, &use_stmt
)
295 || gimple_bb (use_stmt
)->loop_father
!= father
)
296 return bb_rank
[bb
->index
];
298 /* Look for phi arguments from within the loop. If found, bias this phi. */
299 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
301 tree arg
= gimple_phi_arg_def (stmt
, i
);
302 if (TREE_CODE (arg
) == SSA_NAME
303 && !SSA_NAME_IS_DEFAULT_DEF (arg
))
305 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
306 if (gimple_bb (def_stmt
)->loop_father
== father
)
307 return bb_rank
[father
->latch
->index
] + PHI_LOOP_BIAS
;
311 /* Must be an uninteresting phi. */
312 return bb_rank
[bb
->index
];
315 /* If EXP is an SSA_NAME defined by a PHI statement that represents a
316 loop-carried dependence of an innermost loop, return TRUE; else
319 loop_carried_phi (tree exp
)
324 if (TREE_CODE (exp
) != SSA_NAME
325 || SSA_NAME_IS_DEFAULT_DEF (exp
))
328 phi_stmt
= SSA_NAME_DEF_STMT (exp
);
330 if (gimple_code (SSA_NAME_DEF_STMT (exp
)) != GIMPLE_PHI
)
333 /* Non-loop-carried phis have block rank. Loop-carried phis have
334 an additional bias added in. If this phi doesn't have block rank,
335 it's biased and should not be propagated. */
336 block_rank
= bb_rank
[gimple_bb (phi_stmt
)->index
];
338 if (phi_rank (phi_stmt
) != block_rank
)
344 /* Return the maximum of RANK and the rank that should be propagated
345 from expression OP. For most operands, this is just the rank of OP.
346 For loop-carried phis, the value is zero to avoid undoing the bias
347 in favor of the phi. */
349 propagate_rank (long rank
, tree op
)
353 if (loop_carried_phi (op
))
356 op_rank
= get_rank (op
);
358 return MAX (rank
, op_rank
);
361 /* Look up the operand rank structure for expression E. */
364 find_operand_rank (tree e
)
366 long *slot
= operand_rank
->get (e
);
367 return slot
? *slot
: -1;
370 /* Insert {E,RANK} into the operand rank hashtable. */
373 insert_operand_rank (tree e
, long rank
)
375 gcc_assert (rank
> 0);
376 gcc_assert (!operand_rank
->put (e
, rank
));
379 /* Given an expression E, return the rank of the expression. */
384 /* SSA_NAME's have the rank of the expression they are the result
386 For globals and uninitialized values, the rank is 0.
387 For function arguments, use the pre-setup rank.
388 For PHI nodes, stores, asm statements, etc, we use the rank of
390 For simple operations, the rank is the maximum rank of any of
391 its operands, or the bb_rank, whichever is less.
392 I make no claims that this is optimal, however, it gives good
395 /* We make an exception to the normal ranking system to break
396 dependences of accumulator variables in loops. Suppose we
397 have a simple one-block loop containing:
404 As shown, each iteration of the calculation into x is fully
405 dependent upon the iteration before it. We would prefer to
406 see this in the form:
413 If the loop is unrolled, the calculations of b and c from
414 different iterations can be interleaved.
416 To obtain this result during reassociation, we bias the rank
417 of the phi definition x_1 upward, when it is recognized as an
418 accumulator pattern. The artificial rank causes it to be
419 added last, providing the desired independence. */
421 if (TREE_CODE (e
) == SSA_NAME
)
428 if (SSA_NAME_IS_DEFAULT_DEF (e
))
429 return find_operand_rank (e
);
431 stmt
= SSA_NAME_DEF_STMT (e
);
432 if (gimple_code (stmt
) == GIMPLE_PHI
)
433 return phi_rank (stmt
);
435 if (!is_gimple_assign (stmt
))
436 return bb_rank
[gimple_bb (stmt
)->index
];
438 /* If we already have a rank for this expression, use that. */
439 rank
= find_operand_rank (e
);
443 /* Otherwise, find the maximum rank for the operands. As an
444 exception, remove the bias from loop-carried phis when propagating
445 the rank so that dependent operations are not also biased. */
446 /* Simply walk over all SSA uses - this takes advatage of the
447 fact that non-SSA operands are is_gimple_min_invariant and
450 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
451 rank
= propagate_rank (rank
, op
);
453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
455 fprintf (dump_file
, "Rank for ");
456 print_generic_expr (dump_file
, e
);
457 fprintf (dump_file
, " is %ld\n", (rank
+ 1));
460 /* Note the rank in the hashtable so we don't recompute it. */
461 insert_operand_rank (e
, (rank
+ 1));
465 /* Constants, globals, etc., are rank 0 */
470 /* We want integer ones to end up last no matter what, since they are
471 the ones we can do the most with. */
472 #define INTEGER_CONST_TYPE 1 << 4
473 #define FLOAT_ONE_CONST_TYPE 1 << 3
474 #define FLOAT_CONST_TYPE 1 << 2
475 #define OTHER_CONST_TYPE 1 << 1
477 /* Classify an invariant tree into integer, float, or other, so that
478 we can sort them to be near other constants of the same type. */
480 constant_type (tree t
)
482 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
483 return INTEGER_CONST_TYPE
;
484 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
)))
486 /* Sort -1.0 and 1.0 constants last, while in some cases
487 const_binop can't optimize some inexact operations, multiplication
488 by -1.0 or 1.0 can be always merged with others. */
489 if (real_onep (t
) || real_minus_onep (t
))
490 return FLOAT_ONE_CONST_TYPE
;
491 return FLOAT_CONST_TYPE
;
494 return OTHER_CONST_TYPE
;
497 /* qsort comparison function to sort operand entries PA and PB by rank
498 so that the sorted array is ordered by rank in decreasing order. */
500 sort_by_operand_rank (const void *pa
, const void *pb
)
502 const operand_entry
*oea
= *(const operand_entry
*const *)pa
;
503 const operand_entry
*oeb
= *(const operand_entry
*const *)pb
;
505 if (oeb
->rank
!= oea
->rank
)
506 return oeb
->rank
> oea
->rank
? 1 : -1;
508 /* It's nicer for optimize_expression if constants that are likely
509 to fold when added/multiplied/whatever are put next to each
510 other. Since all constants have rank 0, order them by type. */
513 if (constant_type (oeb
->op
) != constant_type (oea
->op
))
514 return constant_type (oea
->op
) - constant_type (oeb
->op
);
516 /* To make sorting result stable, we use unique IDs to determine
518 return oeb
->id
> oea
->id
? 1 : -1;
521 if (TREE_CODE (oea
->op
) != SSA_NAME
)
523 if (TREE_CODE (oeb
->op
) != SSA_NAME
)
524 return oeb
->id
> oea
->id
? 1 : -1;
528 else if (TREE_CODE (oeb
->op
) != SSA_NAME
)
531 /* Lastly, make sure the versions that are the same go next to each
533 if (SSA_NAME_VERSION (oeb
->op
) != SSA_NAME_VERSION (oea
->op
))
535 /* As SSA_NAME_VERSION is assigned pretty randomly, because we reuse
536 versions of removed SSA_NAMEs, so if possible, prefer to sort
537 based on basic block and gimple_uid of the SSA_NAME_DEF_STMT.
539 gimple
*stmta
= SSA_NAME_DEF_STMT (oea
->op
);
540 gimple
*stmtb
= SSA_NAME_DEF_STMT (oeb
->op
);
541 basic_block bba
= gimple_bb (stmta
);
542 basic_block bbb
= gimple_bb (stmtb
);
545 /* One of the SSA_NAMEs can be defined in oeN->stmt_to_insert
546 but the other might not. */
551 /* If neither is, compare bb_rank. */
552 if (bb_rank
[bbb
->index
] != bb_rank
[bba
->index
])
553 return (bb_rank
[bbb
->index
] >> 16) - (bb_rank
[bba
->index
] >> 16);
556 bool da
= reassoc_stmt_dominates_stmt_p (stmta
, stmtb
);
557 bool db
= reassoc_stmt_dominates_stmt_p (stmtb
, stmta
);
561 return SSA_NAME_VERSION (oeb
->op
) > SSA_NAME_VERSION (oea
->op
) ? 1 : -1;
564 return oeb
->id
> oea
->id
? 1 : -1;
567 /* Add an operand entry to *OPS for the tree operand OP. */
570 add_to_ops_vec (vec
<operand_entry
*> *ops
, tree op
, gimple
*stmt_to_insert
= NULL
)
572 operand_entry
*oe
= operand_entry_pool
.allocate ();
575 oe
->rank
= get_rank (op
);
576 oe
->id
= next_operand_entry_id
++;
578 oe
->stmt_to_insert
= stmt_to_insert
;
582 /* Add an operand entry to *OPS for the tree operand OP with repeat
586 add_repeat_to_ops_vec (vec
<operand_entry
*> *ops
, tree op
,
587 HOST_WIDE_INT repeat
)
589 operand_entry
*oe
= operand_entry_pool
.allocate ();
592 oe
->rank
= get_rank (op
);
593 oe
->id
= next_operand_entry_id
++;
595 oe
->stmt_to_insert
= NULL
;
598 reassociate_stats
.pows_encountered
++;
601 /* Return true if STMT is reassociable operation containing a binary
602 operation with tree code CODE, and is inside LOOP. */
605 is_reassociable_op (gimple
*stmt
, enum tree_code code
, class loop
*loop
)
607 basic_block bb
= gimple_bb (stmt
);
609 if (gimple_bb (stmt
) == NULL
)
612 if (!flow_bb_inside_loop_p (loop
, bb
))
615 if (is_gimple_assign (stmt
)
616 && gimple_assign_rhs_code (stmt
) == code
617 && has_single_use (gimple_assign_lhs (stmt
)))
619 tree rhs1
= gimple_assign_rhs1 (stmt
);
620 tree rhs2
= gimple_assign_rhs2 (stmt
);
621 if (TREE_CODE (rhs1
) == SSA_NAME
622 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
))
625 && TREE_CODE (rhs2
) == SSA_NAME
626 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2
))
635 /* Return true if STMT is a nop-conversion. */
638 gimple_nop_conversion_p (gimple
*stmt
)
640 if (gassign
*ass
= dyn_cast
<gassign
*> (stmt
))
642 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass
))
643 && tree_nop_conversion_p (TREE_TYPE (gimple_assign_lhs (ass
)),
644 TREE_TYPE (gimple_assign_rhs1 (ass
))))
650 /* Given NAME, if NAME is defined by a unary operation OPCODE, return the
651 operand of the negate operation. Otherwise, return NULL. */
654 get_unary_op (tree name
, enum tree_code opcode
)
656 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
658 /* Look through nop conversions (sign changes). */
659 if (gimple_nop_conversion_p (stmt
)
660 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
661 stmt
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
663 if (!is_gimple_assign (stmt
))
666 if (gimple_assign_rhs_code (stmt
) == opcode
)
667 return gimple_assign_rhs1 (stmt
);
671 /* Return true if OP1 and OP2 have the same value if casted to either type. */
674 ops_equal_values_p (tree op1
, tree op2
)
680 if (TREE_CODE (op1
) == SSA_NAME
)
682 gimple
*stmt
= SSA_NAME_DEF_STMT (op1
);
683 if (gimple_nop_conversion_p (stmt
))
685 op1
= gimple_assign_rhs1 (stmt
);
691 if (TREE_CODE (op2
) == SSA_NAME
)
693 gimple
*stmt
= SSA_NAME_DEF_STMT (op2
);
694 if (gimple_nop_conversion_p (stmt
))
696 op2
= gimple_assign_rhs1 (stmt
);
707 /* If CURR and LAST are a pair of ops that OPCODE allows us to
708 eliminate through equivalences, do so, remove them from OPS, and
709 return true. Otherwise, return false. */
712 eliminate_duplicate_pair (enum tree_code opcode
,
713 vec
<operand_entry
*> *ops
,
720 /* If we have two of the same op, and the opcode is & |, min, or max,
721 we can eliminate one of them.
722 If we have two of the same op, and the opcode is ^, we can
723 eliminate both of them. */
725 if (last
&& last
->op
== curr
->op
)
733 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
735 fprintf (dump_file
, "Equivalence: ");
736 print_generic_expr (dump_file
, curr
->op
);
737 fprintf (dump_file
, " [&|minmax] ");
738 print_generic_expr (dump_file
, last
->op
);
739 fprintf (dump_file
, " -> ");
740 print_generic_stmt (dump_file
, last
->op
);
743 ops
->ordered_remove (i
);
744 reassociate_stats
.ops_eliminated
++;
749 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
751 fprintf (dump_file
, "Equivalence: ");
752 print_generic_expr (dump_file
, curr
->op
);
753 fprintf (dump_file
, " ^ ");
754 print_generic_expr (dump_file
, last
->op
);
755 fprintf (dump_file
, " -> nothing\n");
758 reassociate_stats
.ops_eliminated
+= 2;
760 if (ops
->length () == 2)
763 add_to_ops_vec (ops
, build_zero_cst (TREE_TYPE (last
->op
)));
768 ops
->ordered_remove (i
-1);
769 ops
->ordered_remove (i
-1);
781 static vec
<tree
> plus_negates
;
783 /* If OPCODE is PLUS_EXPR, CURR->OP is a negate expression or a bitwise not
784 expression, look in OPS for a corresponding positive operation to cancel
785 it out. If we find one, remove the other from OPS, replace
786 OPS[CURRINDEX] with 0 or -1, respectively, and return true. Otherwise,
790 eliminate_plus_minus_pair (enum tree_code opcode
,
791 vec
<operand_entry
*> *ops
,
792 unsigned int currindex
,
800 if (opcode
!= PLUS_EXPR
|| TREE_CODE (curr
->op
) != SSA_NAME
)
803 negateop
= get_unary_op (curr
->op
, NEGATE_EXPR
);
804 notop
= get_unary_op (curr
->op
, BIT_NOT_EXPR
);
805 if (negateop
== NULL_TREE
&& notop
== NULL_TREE
)
808 /* Any non-negated version will have a rank that is one less than
809 the current rank. So once we hit those ranks, if we don't find
812 for (i
= currindex
+ 1;
813 ops
->iterate (i
, &oe
)
814 && oe
->rank
>= curr
->rank
- 1 ;
818 && ops_equal_values_p (oe
->op
, negateop
))
820 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
822 fprintf (dump_file
, "Equivalence: ");
823 print_generic_expr (dump_file
, negateop
);
824 fprintf (dump_file
, " + -");
825 print_generic_expr (dump_file
, oe
->op
);
826 fprintf (dump_file
, " -> 0\n");
829 ops
->ordered_remove (i
);
830 add_to_ops_vec (ops
, build_zero_cst (TREE_TYPE (oe
->op
)));
831 ops
->ordered_remove (currindex
);
832 reassociate_stats
.ops_eliminated
++;
837 && ops_equal_values_p (oe
->op
, notop
))
839 tree op_type
= TREE_TYPE (oe
->op
);
841 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
843 fprintf (dump_file
, "Equivalence: ");
844 print_generic_expr (dump_file
, notop
);
845 fprintf (dump_file
, " + ~");
846 print_generic_expr (dump_file
, oe
->op
);
847 fprintf (dump_file
, " -> -1\n");
850 ops
->ordered_remove (i
);
851 add_to_ops_vec (ops
, build_all_ones_cst (op_type
));
852 ops
->ordered_remove (currindex
);
853 reassociate_stats
.ops_eliminated
++;
859 /* If CURR->OP is a negate expr without nop conversion in a plus expr:
860 save it for later inspection in repropagate_negates(). */
861 if (negateop
!= NULL_TREE
862 && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (curr
->op
)) == NEGATE_EXPR
)
863 plus_negates
.safe_push (curr
->op
);
868 /* If OPCODE is BIT_IOR_EXPR, BIT_AND_EXPR, and, CURR->OP is really a
869 bitwise not expression, look in OPS for a corresponding operand to
870 cancel it out. If we find one, remove the other from OPS, replace
871 OPS[CURRINDEX] with 0, and return true. Otherwise, return
875 eliminate_not_pairs (enum tree_code opcode
,
876 vec
<operand_entry
*> *ops
,
877 unsigned int currindex
,
884 if ((opcode
!= BIT_IOR_EXPR
&& opcode
!= BIT_AND_EXPR
)
885 || TREE_CODE (curr
->op
) != SSA_NAME
)
888 notop
= get_unary_op (curr
->op
, BIT_NOT_EXPR
);
889 if (notop
== NULL_TREE
)
892 /* Any non-not version will have a rank that is one less than
893 the current rank. So once we hit those ranks, if we don't find
896 for (i
= currindex
+ 1;
897 ops
->iterate (i
, &oe
)
898 && oe
->rank
>= curr
->rank
- 1;
903 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
905 fprintf (dump_file
, "Equivalence: ");
906 print_generic_expr (dump_file
, notop
);
907 if (opcode
== BIT_AND_EXPR
)
908 fprintf (dump_file
, " & ~");
909 else if (opcode
== BIT_IOR_EXPR
)
910 fprintf (dump_file
, " | ~");
911 print_generic_expr (dump_file
, oe
->op
);
912 if (opcode
== BIT_AND_EXPR
)
913 fprintf (dump_file
, " -> 0\n");
914 else if (opcode
== BIT_IOR_EXPR
)
915 fprintf (dump_file
, " -> -1\n");
918 if (opcode
== BIT_AND_EXPR
)
919 oe
->op
= build_zero_cst (TREE_TYPE (oe
->op
));
920 else if (opcode
== BIT_IOR_EXPR
)
921 oe
->op
= build_all_ones_cst (TREE_TYPE (oe
->op
));
923 reassociate_stats
.ops_eliminated
+= ops
->length () - 1;
925 ops
->quick_push (oe
);
933 /* Use constant value that may be present in OPS to try to eliminate
934 operands. Note that this function is only really used when we've
935 eliminated ops for other reasons, or merged constants. Across
936 single statements, fold already does all of this, plus more. There
937 is little point in duplicating logic, so I've only included the
938 identities that I could ever construct testcases to trigger. */
941 eliminate_using_constants (enum tree_code opcode
,
942 vec
<operand_entry
*> *ops
)
944 operand_entry
*oelast
= ops
->last ();
945 tree type
= TREE_TYPE (oelast
->op
);
947 if (oelast
->rank
== 0
948 && (ANY_INTEGRAL_TYPE_P (type
) || FLOAT_TYPE_P (type
)))
953 if (integer_zerop (oelast
->op
))
955 if (ops
->length () != 1)
957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
958 fprintf (dump_file
, "Found & 0, removing all other ops\n");
960 reassociate_stats
.ops_eliminated
+= ops
->length () - 1;
963 ops
->quick_push (oelast
);
967 else if (integer_all_onesp (oelast
->op
))
969 if (ops
->length () != 1)
971 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
972 fprintf (dump_file
, "Found & -1, removing\n");
974 reassociate_stats
.ops_eliminated
++;
979 if (integer_all_onesp (oelast
->op
))
981 if (ops
->length () != 1)
983 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
984 fprintf (dump_file
, "Found | -1, removing all other ops\n");
986 reassociate_stats
.ops_eliminated
+= ops
->length () - 1;
989 ops
->quick_push (oelast
);
993 else if (integer_zerop (oelast
->op
))
995 if (ops
->length () != 1)
997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
998 fprintf (dump_file
, "Found | 0, removing\n");
1000 reassociate_stats
.ops_eliminated
++;
1005 if (integer_zerop (oelast
->op
)
1006 || (FLOAT_TYPE_P (type
)
1007 && !HONOR_NANS (type
)
1008 && !HONOR_SIGNED_ZEROS (type
)
1009 && real_zerop (oelast
->op
)))
1011 if (ops
->length () != 1)
1013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1014 fprintf (dump_file
, "Found * 0, removing all other ops\n");
1016 reassociate_stats
.ops_eliminated
+= ops
->length () - 1;
1018 ops
->quick_push (oelast
);
1022 else if (integer_onep (oelast
->op
)
1023 || (FLOAT_TYPE_P (type
)
1024 && !HONOR_SNANS (type
)
1025 && real_onep (oelast
->op
)))
1027 if (ops
->length () != 1)
1029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1030 fprintf (dump_file
, "Found * 1, removing\n");
1032 reassociate_stats
.ops_eliminated
++;
1040 if (integer_zerop (oelast
->op
)
1041 || (FLOAT_TYPE_P (type
)
1042 && (opcode
== PLUS_EXPR
|| opcode
== MINUS_EXPR
)
1043 && fold_real_zero_addition_p (type
, oelast
->op
,
1044 opcode
== MINUS_EXPR
)))
1046 if (ops
->length () != 1)
1048 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1049 fprintf (dump_file
, "Found [|^+] 0, removing\n");
1051 reassociate_stats
.ops_eliminated
++;
1063 static void linearize_expr_tree (vec
<operand_entry
*> *, gimple
*,
1066 /* Structure for tracking and counting operands. */
1070 enum tree_code oecode
;
1075 /* The heap for the oecount hashtable and the sorted list of operands. */
1076 static vec
<oecount
> cvec
;
1079 /* Oecount hashtable helpers. */
1081 struct oecount_hasher
: int_hash
<int, 0, 1>
1083 static inline hashval_t
hash (int);
1084 static inline bool equal (int, int);
1087 /* Hash function for oecount. */
1090 oecount_hasher::hash (int p
)
1092 const oecount
*c
= &cvec
[p
- 42];
1093 return htab_hash_pointer (c
->op
) ^ (hashval_t
)c
->oecode
;
1096 /* Comparison function for oecount. */
1099 oecount_hasher::equal (int p1
, int p2
)
1101 const oecount
*c1
= &cvec
[p1
- 42];
1102 const oecount
*c2
= &cvec
[p2
- 42];
1103 return c1
->oecode
== c2
->oecode
&& c1
->op
== c2
->op
;
1106 /* Comparison function for qsort sorting oecount elements by count. */
1109 oecount_cmp (const void *p1
, const void *p2
)
1111 const oecount
*c1
= (const oecount
*)p1
;
1112 const oecount
*c2
= (const oecount
*)p2
;
1113 if (c1
->cnt
!= c2
->cnt
)
1114 return c1
->cnt
> c2
->cnt
? 1 : -1;
1116 /* If counts are identical, use unique IDs to stabilize qsort. */
1117 return c1
->id
> c2
->id
? 1 : -1;
1120 /* Return TRUE iff STMT represents a builtin call that raises OP
1121 to some exponent. */
1124 stmt_is_power_of_op (gimple
*stmt
, tree op
)
1126 if (!is_gimple_call (stmt
))
1129 switch (gimple_call_combined_fn (stmt
))
1133 return (operand_equal_p (gimple_call_arg (stmt
, 0), op
, 0));
1140 /* Given STMT which is a __builtin_pow* call, decrement its exponent
1141 in place and return the result. Assumes that stmt_is_power_of_op
1142 was previously called for STMT and returned TRUE. */
1144 static HOST_WIDE_INT
1145 decrement_power (gimple
*stmt
)
1147 REAL_VALUE_TYPE c
, cint
;
1148 HOST_WIDE_INT power
;
1151 switch (gimple_call_combined_fn (stmt
))
1154 arg1
= gimple_call_arg (stmt
, 1);
1155 c
= TREE_REAL_CST (arg1
);
1156 power
= real_to_integer (&c
) - 1;
1157 real_from_integer (&cint
, VOIDmode
, power
, SIGNED
);
1158 gimple_call_set_arg (stmt
, 1, build_real (TREE_TYPE (arg1
), cint
));
1162 arg1
= gimple_call_arg (stmt
, 1);
1163 power
= TREE_INT_CST_LOW (arg1
) - 1;
1164 gimple_call_set_arg (stmt
, 1, build_int_cst (TREE_TYPE (arg1
), power
));
1172 /* Replace SSA defined by STMT and replace all its uses with new
1173 SSA. Also return the new SSA. */
1176 make_new_ssa_for_def (gimple
*stmt
, enum tree_code opcode
, tree op
)
1180 imm_use_iterator iter
;
1181 tree new_lhs
, new_debug_lhs
= NULL_TREE
;
1182 tree lhs
= gimple_get_lhs (stmt
);
1184 new_lhs
= make_ssa_name (TREE_TYPE (lhs
));
1185 gimple_set_lhs (stmt
, new_lhs
);
1187 /* Also need to update GIMPLE_DEBUGs. */
1188 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
1190 tree repl
= new_lhs
;
1191 if (is_gimple_debug (use_stmt
))
1193 if (new_debug_lhs
== NULL_TREE
)
1195 new_debug_lhs
= make_node (DEBUG_EXPR_DECL
);
1197 = gimple_build_debug_bind (new_debug_lhs
,
1198 build2 (opcode
, TREE_TYPE (lhs
),
1201 DECL_ARTIFICIAL (new_debug_lhs
) = 1;
1202 TREE_TYPE (new_debug_lhs
) = TREE_TYPE (lhs
);
1203 SET_DECL_MODE (new_debug_lhs
, TYPE_MODE (TREE_TYPE (lhs
)));
1204 gimple_set_uid (def_temp
, gimple_uid (stmt
));
1205 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1206 gsi_insert_after (&gsi
, def_temp
, GSI_SAME_STMT
);
1208 repl
= new_debug_lhs
;
1210 FOR_EACH_IMM_USE_ON_STMT (use
, iter
)
1211 SET_USE (use
, repl
);
1212 update_stmt (use_stmt
);
1217 /* Replace all SSAs defined in STMTS_TO_FIX and replace its
1218 uses with new SSAs. Also do this for the stmt that defines DEF
1219 if *DEF is not OP. */
1222 make_new_ssa_for_all_defs (tree
*def
, enum tree_code opcode
, tree op
,
1223 vec
<gimple
*> &stmts_to_fix
)
1229 && TREE_CODE (*def
) == SSA_NAME
1230 && (stmt
= SSA_NAME_DEF_STMT (*def
))
1231 && gimple_code (stmt
) != GIMPLE_NOP
)
1232 *def
= make_new_ssa_for_def (stmt
, opcode
, op
);
1234 FOR_EACH_VEC_ELT (stmts_to_fix
, i
, stmt
)
1235 make_new_ssa_for_def (stmt
, opcode
, op
);
1238 /* Find the single immediate use of STMT's LHS, and replace it
1239 with OP. Remove STMT. If STMT's LHS is the same as *DEF,
1240 replace *DEF with OP as well. */
1243 propagate_op_to_single_use (tree op
, gimple
*stmt
, tree
*def
)
1248 gimple_stmt_iterator gsi
;
1250 if (is_gimple_call (stmt
))
1251 lhs
= gimple_call_lhs (stmt
);
1253 lhs
= gimple_assign_lhs (stmt
);
1255 gcc_assert (has_single_use (lhs
));
1256 single_imm_use (lhs
, &use
, &use_stmt
);
1260 if (TREE_CODE (op
) != SSA_NAME
)
1261 update_stmt (use_stmt
);
1262 gsi
= gsi_for_stmt (stmt
);
1263 unlink_stmt_vdef (stmt
);
1264 reassoc_remove_stmt (&gsi
);
1265 release_defs (stmt
);
1268 /* Walks the linear chain with result *DEF searching for an operation
1269 with operand OP and code OPCODE removing that from the chain. *DEF
1270 is updated if there is only one operand but no operation left. */
1273 zero_one_operation (tree
*def
, enum tree_code opcode
, tree op
)
1275 tree orig_def
= *def
;
1276 gimple
*stmt
= SSA_NAME_DEF_STMT (*def
);
1277 /* PR72835 - Record the stmt chain that has to be updated such that
1278 we dont use the same LHS when the values computed are different. */
1279 auto_vec
<gimple
*, 64> stmts_to_fix
;
1285 if (opcode
== MULT_EXPR
)
1287 if (stmt_is_power_of_op (stmt
, op
))
1289 if (decrement_power (stmt
) == 1)
1291 if (stmts_to_fix
.length () > 0)
1292 stmts_to_fix
.pop ();
1293 propagate_op_to_single_use (op
, stmt
, def
);
1297 else if (gimple_assign_rhs_code (stmt
) == NEGATE_EXPR
)
1299 if (gimple_assign_rhs1 (stmt
) == op
)
1301 tree cst
= build_minus_one_cst (TREE_TYPE (op
));
1302 if (stmts_to_fix
.length () > 0)
1303 stmts_to_fix
.pop ();
1304 propagate_op_to_single_use (cst
, stmt
, def
);
1307 else if (integer_minus_onep (op
)
1308 || real_minus_onep (op
))
1310 gimple_assign_set_rhs_code
1311 (stmt
, TREE_CODE (gimple_assign_rhs1 (stmt
)));
1317 name
= gimple_assign_rhs1 (stmt
);
1319 /* If this is the operation we look for and one of the operands
1320 is ours simply propagate the other operand into the stmts
1322 if (gimple_assign_rhs_code (stmt
) == opcode
1324 || gimple_assign_rhs2 (stmt
) == op
))
1327 name
= gimple_assign_rhs2 (stmt
);
1328 if (stmts_to_fix
.length () > 0)
1329 stmts_to_fix
.pop ();
1330 propagate_op_to_single_use (name
, stmt
, def
);
1334 /* We might have a multiply of two __builtin_pow* calls, and
1335 the operand might be hiding in the rightmost one. Likewise
1336 this can happen for a negate. */
1337 if (opcode
== MULT_EXPR
1338 && gimple_assign_rhs_code (stmt
) == opcode
1339 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
1340 && has_single_use (gimple_assign_rhs2 (stmt
)))
1342 gimple
*stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt
));
1343 if (stmt_is_power_of_op (stmt2
, op
))
1345 if (decrement_power (stmt2
) == 1)
1346 propagate_op_to_single_use (op
, stmt2
, def
);
1348 stmts_to_fix
.safe_push (stmt2
);
1351 else if (is_gimple_assign (stmt2
)
1352 && gimple_assign_rhs_code (stmt2
) == NEGATE_EXPR
)
1354 if (gimple_assign_rhs1 (stmt2
) == op
)
1356 tree cst
= build_minus_one_cst (TREE_TYPE (op
));
1357 propagate_op_to_single_use (cst
, stmt2
, def
);
1360 else if (integer_minus_onep (op
)
1361 || real_minus_onep (op
))
1363 stmts_to_fix
.safe_push (stmt2
);
1364 gimple_assign_set_rhs_code
1365 (stmt2
, TREE_CODE (gimple_assign_rhs1 (stmt2
)));
1371 /* Continue walking the chain. */
1372 gcc_assert (name
!= op
1373 && TREE_CODE (name
) == SSA_NAME
);
1374 stmt
= SSA_NAME_DEF_STMT (name
);
1375 stmts_to_fix
.safe_push (stmt
);
1379 if (stmts_to_fix
.length () > 0 || *def
== orig_def
)
1380 make_new_ssa_for_all_defs (def
, opcode
, op
, stmts_to_fix
);
1383 /* Returns true if statement S1 dominates statement S2. Like
1384 stmt_dominates_stmt_p, but uses stmt UIDs to optimize. */
1387 reassoc_stmt_dominates_stmt_p (gimple
*s1
, gimple
*s2
)
1389 basic_block bb1
= gimple_bb (s1
), bb2
= gimple_bb (s2
);
1391 /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
1392 SSA_NAME. Assume it lives at the beginning of function and
1393 thus dominates everything. */
1394 if (!bb1
|| s1
== s2
)
1397 /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
1403 /* PHIs in the same basic block are assumed to be
1404 executed all in parallel, if only one stmt is a PHI,
1405 it dominates the other stmt in the same basic block. */
1406 if (gimple_code (s1
) == GIMPLE_PHI
)
1409 if (gimple_code (s2
) == GIMPLE_PHI
)
1412 gcc_assert (gimple_uid (s1
) && gimple_uid (s2
));
1414 if (gimple_uid (s1
) < gimple_uid (s2
))
1417 if (gimple_uid (s1
) > gimple_uid (s2
))
1420 gimple_stmt_iterator gsi
= gsi_for_stmt (s1
);
1421 unsigned int uid
= gimple_uid (s1
);
1422 for (gsi_next (&gsi
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1424 gimple
*s
= gsi_stmt (gsi
);
1425 if (gimple_uid (s
) != uid
)
1434 return dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
);
1437 /* Insert STMT after INSERT_POINT. */
1440 insert_stmt_after (gimple
*stmt
, gimple
*insert_point
)
1442 gimple_stmt_iterator gsi
;
1445 if (gimple_code (insert_point
) == GIMPLE_PHI
)
1446 bb
= gimple_bb (insert_point
);
1447 else if (!stmt_ends_bb_p (insert_point
))
1449 gsi
= gsi_for_stmt (insert_point
);
1450 gimple_set_uid (stmt
, gimple_uid (insert_point
));
1451 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
1455 /* We assume INSERT_POINT is a SSA_NAME_DEF_STMT of some SSA_NAME,
1456 thus if it must end a basic block, it should be a call that can
1457 throw, or some assignment that can throw. If it throws, the LHS
1458 of it will not be initialized though, so only valid places using
1459 the SSA_NAME should be dominated by the fallthru edge. */
1460 bb
= find_fallthru_edge (gimple_bb (insert_point
)->succs
)->dest
;
1461 gsi
= gsi_after_labels (bb
);
1462 if (gsi_end_p (gsi
))
1464 gimple_stmt_iterator gsi2
= gsi_last_bb (bb
);
1465 gimple_set_uid (stmt
,
1466 gsi_end_p (gsi2
) ? 1 : gimple_uid (gsi_stmt (gsi2
)));
1469 gimple_set_uid (stmt
, gimple_uid (gsi_stmt (gsi
)));
1470 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
1473 /* Builds one statement performing OP1 OPCODE OP2 using TMPVAR for
1474 the result. Places the statement after the definition of either
1475 OP1 or OP2. Returns the new statement. */
1478 build_and_add_sum (tree type
, tree op1
, tree op2
, enum tree_code opcode
)
1480 gimple
*op1def
= NULL
, *op2def
= NULL
;
1481 gimple_stmt_iterator gsi
;
1485 /* Create the addition statement. */
1486 op
= make_ssa_name (type
);
1487 sum
= gimple_build_assign (op
, opcode
, op1
, op2
);
1489 /* Find an insertion place and insert. */
1490 if (TREE_CODE (op1
) == SSA_NAME
)
1491 op1def
= SSA_NAME_DEF_STMT (op1
);
1492 if (TREE_CODE (op2
) == SSA_NAME
)
1493 op2def
= SSA_NAME_DEF_STMT (op2
);
1494 if ((!op1def
|| gimple_nop_p (op1def
))
1495 && (!op2def
|| gimple_nop_p (op2def
)))
1497 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1498 if (gsi_end_p (gsi
))
1500 gimple_stmt_iterator gsi2
1501 = gsi_last_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1502 gimple_set_uid (sum
,
1503 gsi_end_p (gsi2
) ? 1 : gimple_uid (gsi_stmt (gsi2
)));
1506 gimple_set_uid (sum
, gimple_uid (gsi_stmt (gsi
)));
1507 gsi_insert_before (&gsi
, sum
, GSI_NEW_STMT
);
1511 gimple
*insert_point
;
1512 if ((!op1def
|| gimple_nop_p (op1def
))
1513 || (op2def
&& !gimple_nop_p (op2def
)
1514 && reassoc_stmt_dominates_stmt_p (op1def
, op2def
)))
1515 insert_point
= op2def
;
1517 insert_point
= op1def
;
1518 insert_stmt_after (sum
, insert_point
);
1525 /* Perform un-distribution of divisions and multiplications.
1526 A * X + B * X is transformed into (A + B) * X and A / X + B / X
1527 to (A + B) / X for real X.
1529 The algorithm is organized as follows.
1531 - First we walk the addition chain *OPS looking for summands that
1532 are defined by a multiplication or a real division. This results
1533 in the candidates bitmap with relevant indices into *OPS.
1535 - Second we build the chains of multiplications or divisions for
1536 these candidates, counting the number of occurrences of (operand, code)
1537 pairs in all of the candidates chains.
1539 - Third we sort the (operand, code) pairs by number of occurrence and
1540 process them starting with the pair with the most uses.
1542 * For each such pair we walk the candidates again to build a
1543 second candidate bitmap noting all multiplication/division chains
1544 that have at least one occurrence of (operand, code).
1546 * We build an alternate addition chain only covering these
1547 candidates with one (operand, code) operation removed from their
1548 multiplication/division chain.
1550 * The first candidate gets replaced by the alternate addition chain
1551 multiplied/divided by the operand.
1553 * All candidate chains get disabled for further processing and
1554 processing of (operand, code) pairs continues.
1556 The alternate addition chains built are re-processed by the main
1557 reassociation algorithm which allows optimizing a * x * y + b * y * x
1558 to (a + b ) * x * y in one invocation of the reassociation pass. */
1561 undistribute_ops_list (enum tree_code opcode
,
1562 vec
<operand_entry
*> *ops
, class loop
*loop
)
1564 unsigned int length
= ops
->length ();
1567 unsigned nr_candidates
, nr_candidates2
;
1568 sbitmap_iterator sbi0
;
1569 vec
<operand_entry
*> *subops
;
1570 bool changed
= false;
1571 unsigned int next_oecount_id
= 0;
1574 || opcode
!= PLUS_EXPR
)
1577 /* Build a list of candidates to process. */
1578 auto_sbitmap
candidates (length
);
1579 bitmap_clear (candidates
);
1581 FOR_EACH_VEC_ELT (*ops
, i
, oe1
)
1583 enum tree_code dcode
;
1586 if (TREE_CODE (oe1
->op
) != SSA_NAME
)
1588 oe1def
= SSA_NAME_DEF_STMT (oe1
->op
);
1589 if (!is_gimple_assign (oe1def
))
1591 dcode
= gimple_assign_rhs_code (oe1def
);
1592 if ((dcode
!= MULT_EXPR
1593 && dcode
!= RDIV_EXPR
)
1594 || !is_reassociable_op (oe1def
, dcode
, loop
))
1597 bitmap_set_bit (candidates
, i
);
1601 if (nr_candidates
< 2)
1604 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1606 fprintf (dump_file
, "searching for un-distribute opportunities ");
1607 print_generic_expr (dump_file
,
1608 (*ops
)[bitmap_first_set_bit (candidates
)]->op
, TDF_NONE
);
1609 fprintf (dump_file
, " %d\n", nr_candidates
);
1612 /* Build linearized sub-operand lists and the counting table. */
1615 hash_table
<oecount_hasher
> ctable (15);
1617 /* ??? Macro arguments cannot have multi-argument template types in
1618 them. This typedef is needed to workaround that limitation. */
1619 typedef vec
<operand_entry
*> vec_operand_entry_t_heap
;
1620 subops
= XCNEWVEC (vec_operand_entry_t_heap
, ops
->length ());
1621 EXECUTE_IF_SET_IN_BITMAP (candidates
, 0, i
, sbi0
)
1624 enum tree_code oecode
;
1627 oedef
= SSA_NAME_DEF_STMT ((*ops
)[i
]->op
);
1628 oecode
= gimple_assign_rhs_code (oedef
);
1629 linearize_expr_tree (&subops
[i
], oedef
,
1630 associative_tree_code (oecode
), false);
1632 FOR_EACH_VEC_ELT (subops
[i
], j
, oe1
)
1639 c
.id
= next_oecount_id
++;
1642 idx
= cvec
.length () + 41;
1643 slot
= ctable
.find_slot (idx
, INSERT
);
1651 cvec
[*slot
- 42].cnt
++;
1656 /* Sort the counting table. */
1657 cvec
.qsort (oecount_cmp
);
1659 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1662 fprintf (dump_file
, "Candidates:\n");
1663 FOR_EACH_VEC_ELT (cvec
, j
, c
)
1665 fprintf (dump_file
, " %u %s: ", c
->cnt
,
1666 c
->oecode
== MULT_EXPR
1667 ? "*" : c
->oecode
== RDIV_EXPR
? "/" : "?");
1668 print_generic_expr (dump_file
, c
->op
);
1669 fprintf (dump_file
, "\n");
1673 /* Process the (operand, code) pairs in order of most occurrence. */
1674 auto_sbitmap
candidates2 (length
);
1675 while (!cvec
.is_empty ())
1677 oecount
*c
= &cvec
.last ();
1681 /* Now collect the operands in the outer chain that contain
1682 the common operand in their inner chain. */
1683 bitmap_clear (candidates2
);
1685 EXECUTE_IF_SET_IN_BITMAP (candidates
, 0, i
, sbi0
)
1688 enum tree_code oecode
;
1690 tree op
= (*ops
)[i
]->op
;
1692 /* If we undistributed in this chain already this may be
1694 if (TREE_CODE (op
) != SSA_NAME
)
1697 oedef
= SSA_NAME_DEF_STMT (op
);
1698 oecode
= gimple_assign_rhs_code (oedef
);
1699 if (oecode
!= c
->oecode
)
1702 FOR_EACH_VEC_ELT (subops
[i
], j
, oe1
)
1704 if (oe1
->op
== c
->op
)
1706 bitmap_set_bit (candidates2
, i
);
1713 if (nr_candidates2
>= 2)
1715 operand_entry
*oe1
, *oe2
;
1717 int first
= bitmap_first_set_bit (candidates2
);
1719 /* Build the new addition chain. */
1720 oe1
= (*ops
)[first
];
1721 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1723 fprintf (dump_file
, "Building (");
1724 print_generic_expr (dump_file
, oe1
->op
);
1726 zero_one_operation (&oe1
->op
, c
->oecode
, c
->op
);
1727 EXECUTE_IF_SET_IN_BITMAP (candidates2
, first
+1, i
, sbi0
)
1731 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1733 fprintf (dump_file
, " + ");
1734 print_generic_expr (dump_file
, oe2
->op
);
1736 zero_one_operation (&oe2
->op
, c
->oecode
, c
->op
);
1737 sum
= build_and_add_sum (TREE_TYPE (oe1
->op
),
1738 oe1
->op
, oe2
->op
, opcode
);
1739 oe2
->op
= build_zero_cst (TREE_TYPE (oe2
->op
));
1741 oe1
->op
= gimple_get_lhs (sum
);
1744 /* Apply the multiplication/division. */
1745 prod
= build_and_add_sum (TREE_TYPE (oe1
->op
),
1746 oe1
->op
, c
->op
, c
->oecode
);
1747 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1749 fprintf (dump_file
, ") %s ", c
->oecode
== MULT_EXPR
? "*" : "/");
1750 print_generic_expr (dump_file
, c
->op
);
1751 fprintf (dump_file
, "\n");
1754 /* Record it in the addition chain and disable further
1755 undistribution with this op. */
1756 oe1
->op
= gimple_assign_lhs (prod
);
1757 oe1
->rank
= get_rank (oe1
->op
);
1758 subops
[first
].release ();
1766 for (i
= 0; i
< ops
->length (); ++i
)
1767 subops
[i
].release ();
1774 /* Pair to hold the information of one specific VECTOR_TYPE SSA_NAME:
1775 first: element index for each relevant BIT_FIELD_REF.
1776 second: the index of vec ops* for each relevant BIT_FIELD_REF. */
1777 typedef std::pair
<unsigned, unsigned> v_info_elem
;
1780 auto_vec
<v_info_elem
, 32> vec
;
1782 typedef v_info
*v_info_ptr
;
1784 /* Comparison function for qsort on VECTOR SSA_NAME trees by machine mode. */
1786 sort_by_mach_mode (const void *p_i
, const void *p_j
)
1788 const tree tr1
= *((const tree
*) p_i
);
1789 const tree tr2
= *((const tree
*) p_j
);
1790 unsigned int mode1
= TYPE_MODE (TREE_TYPE (tr1
));
1791 unsigned int mode2
= TYPE_MODE (TREE_TYPE (tr2
));
1794 else if (mode1
< mode2
)
1796 if (SSA_NAME_VERSION (tr1
) < SSA_NAME_VERSION (tr2
))
1798 else if (SSA_NAME_VERSION (tr1
) > SSA_NAME_VERSION (tr2
))
1803 /* Cleanup hash map for VECTOR information. */
1805 cleanup_vinfo_map (hash_map
<tree
, v_info_ptr
> &info_map
)
1807 for (hash_map
<tree
, v_info_ptr
>::iterator it
= info_map
.begin ();
1808 it
!= info_map
.end (); ++it
)
1810 v_info_ptr info
= (*it
).second
;
1812 (*it
).second
= NULL
;
1816 /* Perform un-distribution of BIT_FIELD_REF on VECTOR_TYPE.
1817 V1[0] + V1[1] + ... + V1[k] + V2[0] + V2[1] + ... + V2[k] + ... Vn[k]
1819 Vs = (V1 + V2 + ... + Vn)
1820 Vs[0] + Vs[1] + ... + Vs[k]
1822 The basic steps are listed below:
1824 1) Check the addition chain *OPS by looking those summands coming from
1825 VECTOR bit_field_ref on VECTOR type. Put the information into
1826 v_info_map for each satisfied summand, using VECTOR SSA_NAME as key.
1828 2) For each key (VECTOR SSA_NAME), validate all its BIT_FIELD_REFs are
1829 continuous, they can cover the whole VECTOR perfectly without any holes.
1830 Obtain one VECTOR list which contain candidates to be transformed.
1832 3) Sort the VECTOR list by machine mode of VECTOR type, for each group of
1833 candidates with same mode, build the addition statements for them and
1834 generate BIT_FIELD_REFs accordingly.
1837 The current implementation requires the whole VECTORs should be fully
1838 covered, but it can be extended to support partial, checking adjacent
1839 but not fill the whole, it may need some cost model to define the
1840 boundary to do or not.
1843 undistribute_bitref_for_vector (enum tree_code opcode
,
1844 vec
<operand_entry
*> *ops
, struct loop
*loop
)
1846 if (ops
->length () <= 1)
1849 if (opcode
!= PLUS_EXPR
1850 && opcode
!= MULT_EXPR
1851 && opcode
!= BIT_XOR_EXPR
1852 && opcode
!= BIT_IOR_EXPR
1853 && opcode
!= BIT_AND_EXPR
)
1856 hash_map
<tree
, v_info_ptr
> v_info_map
;
1860 /* Find those summands from VECTOR BIT_FIELD_REF in addition chain, put the
1861 information into map. */
1862 FOR_EACH_VEC_ELT (*ops
, i
, oe1
)
1864 enum tree_code dcode
;
1867 if (TREE_CODE (oe1
->op
) != SSA_NAME
)
1869 oe1def
= SSA_NAME_DEF_STMT (oe1
->op
);
1870 if (!is_gimple_assign (oe1def
))
1872 dcode
= gimple_assign_rhs_code (oe1def
);
1873 if (dcode
!= BIT_FIELD_REF
|| !is_reassociable_op (oe1def
, dcode
, loop
))
1876 tree rhs
= gimple_assign_rhs1 (oe1def
);
1877 tree vec
= TREE_OPERAND (rhs
, 0);
1878 tree vec_type
= TREE_TYPE (vec
);
1880 if (TREE_CODE (vec
) != SSA_NAME
|| !VECTOR_TYPE_P (vec_type
))
1883 /* Ignore it if target machine can't support this VECTOR type. */
1884 if (!VECTOR_MODE_P (TYPE_MODE (vec_type
)))
1887 /* Check const vector type, constrain BIT_FIELD_REF offset and size. */
1888 if (!TYPE_VECTOR_SUBPARTS (vec_type
).is_constant ())
1891 if (VECTOR_TYPE_P (TREE_TYPE (rhs
))
1892 || !is_a
<scalar_mode
> (TYPE_MODE (TREE_TYPE (rhs
))))
1895 /* The type of BIT_FIELD_REF might not be equal to the element type of
1896 the vector. We want to use a vector type with element type the
1897 same as the BIT_FIELD_REF and size the same as TREE_TYPE (vec). */
1898 if (!useless_type_conversion_p (TREE_TYPE (rhs
), TREE_TYPE (vec_type
)))
1900 machine_mode simd_mode
;
1901 unsigned HOST_WIDE_INT size
, nunits
;
1902 unsigned HOST_WIDE_INT elem_size
1903 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs
)));
1904 if (!GET_MODE_BITSIZE (TYPE_MODE (vec_type
)).is_constant (&size
))
1906 if (size
<= elem_size
|| (size
% elem_size
) != 0)
1908 nunits
= size
/ elem_size
;
1909 if (!mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (rhs
)),
1910 nunits
).exists (&simd_mode
))
1912 vec_type
= build_vector_type_for_mode (TREE_TYPE (rhs
), simd_mode
);
1914 /* Ignore it if target machine can't support this VECTOR type. */
1915 if (!VECTOR_MODE_P (TYPE_MODE (vec_type
)))
1918 /* Check const vector type, constrain BIT_FIELD_REF offset and
1920 if (!TYPE_VECTOR_SUBPARTS (vec_type
).is_constant ())
1923 if (maybe_ne (GET_MODE_SIZE (TYPE_MODE (vec_type
)),
1924 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (vec
)))))
1928 tree elem_type
= TREE_TYPE (vec_type
);
1929 unsigned HOST_WIDE_INT elem_size
= tree_to_uhwi (TYPE_SIZE (elem_type
));
1930 if (maybe_ne (bit_field_size (rhs
), elem_size
))
1934 if (!constant_multiple_p (bit_field_offset (rhs
), elem_size
, &idx
))
1937 /* Ignore it if target machine can't support this type of VECTOR
1939 optab op_tab
= optab_for_tree_code (opcode
, vec_type
, optab_vector
);
1940 if (optab_handler (op_tab
, TYPE_MODE (vec_type
)) == CODE_FOR_nothing
)
1944 v_info_ptr
&info
= v_info_map
.get_or_insert (vec
, &existed
);
1948 info
->vec_type
= vec_type
;
1950 else if (!types_compatible_p (vec_type
, info
->vec_type
))
1952 info
->vec
.safe_push (std::make_pair (idx
, i
));
1955 /* At least two VECTOR to combine. */
1956 if (v_info_map
.elements () <= 1)
1958 cleanup_vinfo_map (v_info_map
);
1962 /* Verify all VECTOR candidates by checking two conditions:
1963 1) sorted offsets are adjacent, no holes.
1964 2) can fill the whole VECTOR perfectly.
1965 And add the valid candidates to a vector for further handling. */
1966 auto_vec
<tree
> valid_vecs (v_info_map
.elements ());
1967 for (hash_map
<tree
, v_info_ptr
>::iterator it
= v_info_map
.begin ();
1968 it
!= v_info_map
.end (); ++it
)
1970 tree cand_vec
= (*it
).first
;
1971 v_info_ptr cand_info
= (*it
).second
;
1972 unsigned int num_elems
1973 = TYPE_VECTOR_SUBPARTS (cand_info
->vec_type
).to_constant ();
1974 if (cand_info
->vec
.length () != num_elems
)
1976 sbitmap holes
= sbitmap_alloc (num_elems
);
1977 bitmap_ones (holes
);
1980 FOR_EACH_VEC_ELT (cand_info
->vec
, i
, curr
)
1982 if (!bitmap_bit_p (holes
, curr
->first
))
1988 bitmap_clear_bit (holes
, curr
->first
);
1990 if (valid
&& bitmap_empty_p (holes
))
1991 valid_vecs
.quick_push (cand_vec
);
1992 sbitmap_free (holes
);
1995 /* At least two VECTOR to combine. */
1996 if (valid_vecs
.length () <= 1)
1998 cleanup_vinfo_map (v_info_map
);
2002 valid_vecs
.qsort (sort_by_mach_mode
);
2003 /* Go through all candidates by machine mode order, query the mode_to_total
2004 to get the total number for each mode and skip the single one. */
2005 for (unsigned i
= 0; i
< valid_vecs
.length () - 1; ++i
)
2007 tree tvec
= valid_vecs
[i
];
2008 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (tvec
));
2010 /* Skip modes with only a single candidate. */
2011 if (TYPE_MODE (TREE_TYPE (valid_vecs
[i
+ 1])) != mode
)
2014 unsigned int idx
, j
;
2016 tree sum_vec
= tvec
;
2017 v_info_ptr info_ptr
= *(v_info_map
.get (tvec
));
2019 tree vec_type
= info_ptr
->vec_type
;
2021 /* Build the sum for all candidates with same mode. */
2024 sum
= build_and_add_sum (vec_type
, sum_vec
,
2025 valid_vecs
[i
+ 1], opcode
);
2026 if (!useless_type_conversion_p (vec_type
,
2027 TREE_TYPE (valid_vecs
[i
+ 1])))
2029 /* Update the operands only after build_and_add_sum,
2030 so that we don't have to repeat the placement algorithm
2031 of build_and_add_sum. */
2032 gimple_stmt_iterator gsi
= gsi_for_stmt (sum
);
2033 tree vce
= build1 (VIEW_CONVERT_EXPR
, vec_type
,
2035 tree lhs
= make_ssa_name (vec_type
);
2036 gimple
*g
= gimple_build_assign (lhs
, VIEW_CONVERT_EXPR
, vce
);
2037 gimple_set_uid (g
, gimple_uid (sum
));
2038 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
2039 gimple_assign_set_rhs2 (sum
, lhs
);
2040 if (sum_vec
== tvec
)
2042 vce
= build1 (VIEW_CONVERT_EXPR
, vec_type
, sum_vec
);
2043 lhs
= make_ssa_name (vec_type
);
2044 g
= gimple_build_assign (lhs
, VIEW_CONVERT_EXPR
, vce
);
2045 gimple_set_uid (g
, gimple_uid (sum
));
2046 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
2047 gimple_assign_set_rhs1 (sum
, lhs
);
2051 sum_vec
= gimple_get_lhs (sum
);
2052 info_ptr
= *(v_info_map
.get (valid_vecs
[i
+ 1]));
2053 gcc_assert (types_compatible_p (vec_type
, info_ptr
->vec_type
));
2054 /* Update those related ops of current candidate VECTOR. */
2055 FOR_EACH_VEC_ELT (info_ptr
->vec
, j
, elem
)
2058 gimple
*def
= SSA_NAME_DEF_STMT ((*ops
)[idx
]->op
);
2059 /* Set this then op definition will get DCEd later. */
2060 gimple_set_visited (def
, true);
2061 if (opcode
== PLUS_EXPR
2062 || opcode
== BIT_XOR_EXPR
2063 || opcode
== BIT_IOR_EXPR
)
2064 (*ops
)[idx
]->op
= build_zero_cst (TREE_TYPE ((*ops
)[idx
]->op
));
2065 else if (opcode
== MULT_EXPR
)
2066 (*ops
)[idx
]->op
= build_one_cst (TREE_TYPE ((*ops
)[idx
]->op
));
2069 gcc_assert (opcode
== BIT_AND_EXPR
);
2071 = build_all_ones_cst (TREE_TYPE ((*ops
)[idx
]->op
));
2073 (*ops
)[idx
]->rank
= 0;
2075 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2077 fprintf (dump_file
, "Generating addition -> ");
2078 print_gimple_stmt (dump_file
, sum
, 0);
2082 while ((i
< valid_vecs
.length () - 1)
2083 && TYPE_MODE (TREE_TYPE (valid_vecs
[i
+ 1])) == mode
);
2085 /* Referring to first valid VECTOR with this mode, generate the
2086 BIT_FIELD_REF statements accordingly. */
2087 info_ptr
= *(v_info_map
.get (tvec
));
2089 tree elem_type
= TREE_TYPE (vec_type
);
2090 FOR_EACH_VEC_ELT (info_ptr
->vec
, j
, elem
)
2093 tree dst
= make_ssa_name (elem_type
);
2094 tree pos
= bitsize_int (elem
->first
2095 * tree_to_uhwi (TYPE_SIZE (elem_type
)));
2096 tree bfr
= build3 (BIT_FIELD_REF
, elem_type
, sum_vec
,
2097 TYPE_SIZE (elem_type
), pos
);
2098 gimple
*gs
= gimple_build_assign (dst
, BIT_FIELD_REF
, bfr
);
2099 insert_stmt_after (gs
, sum
);
2100 gimple
*def
= SSA_NAME_DEF_STMT ((*ops
)[idx
]->op
);
2101 /* Set this then op definition will get DCEd later. */
2102 gimple_set_visited (def
, true);
2103 (*ops
)[idx
]->op
= gimple_assign_lhs (gs
);
2104 (*ops
)[idx
]->rank
= get_rank ((*ops
)[idx
]->op
);
2105 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2107 fprintf (dump_file
, "Generating bit_field_ref -> ");
2108 print_gimple_stmt (dump_file
, gs
, 0);
2113 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2114 fprintf (dump_file
, "undistributiong bit_field_ref for vector done.\n");
2116 cleanup_vinfo_map (v_info_map
);
2121 /* If OPCODE is BIT_IOR_EXPR or BIT_AND_EXPR and CURR is a comparison
2122 expression, examine the other OPS to see if any of them are comparisons
2123 of the same values, which we may be able to combine or eliminate.
2124 For example, we can rewrite (a < b) | (a == b) as (a <= b). */
2127 eliminate_redundant_comparison (enum tree_code opcode
,
2128 vec
<operand_entry
*> *ops
,
2129 unsigned int currindex
,
2130 operand_entry
*curr
)
2133 enum tree_code lcode
, rcode
;
2134 gimple
*def1
, *def2
;
2138 if (opcode
!= BIT_IOR_EXPR
&& opcode
!= BIT_AND_EXPR
)
2141 /* Check that CURR is a comparison. */
2142 if (TREE_CODE (curr
->op
) != SSA_NAME
)
2144 def1
= SSA_NAME_DEF_STMT (curr
->op
);
2145 if (!is_gimple_assign (def1
))
2147 lcode
= gimple_assign_rhs_code (def1
);
2148 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
)
2150 op1
= gimple_assign_rhs1 (def1
);
2151 op2
= gimple_assign_rhs2 (def1
);
2153 /* Now look for a similar comparison in the remaining OPS. */
2154 for (i
= currindex
+ 1; ops
->iterate (i
, &oe
); i
++)
2158 if (TREE_CODE (oe
->op
) != SSA_NAME
)
2160 def2
= SSA_NAME_DEF_STMT (oe
->op
);
2161 if (!is_gimple_assign (def2
))
2163 rcode
= gimple_assign_rhs_code (def2
);
2164 if (TREE_CODE_CLASS (rcode
) != tcc_comparison
)
2167 /* If we got here, we have a match. See if we can combine the
2169 tree type
= TREE_TYPE (gimple_assign_lhs (def1
));
2170 if (opcode
== BIT_IOR_EXPR
)
2171 t
= maybe_fold_or_comparisons (type
,
2173 rcode
, gimple_assign_rhs1 (def2
),
2174 gimple_assign_rhs2 (def2
));
2176 t
= maybe_fold_and_comparisons (type
,
2178 rcode
, gimple_assign_rhs1 (def2
),
2179 gimple_assign_rhs2 (def2
));
2183 /* maybe_fold_and_comparisons and maybe_fold_or_comparisons
2184 always give us a boolean_type_node value back. If the original
2185 BIT_AND_EXPR or BIT_IOR_EXPR was of a wider integer type,
2186 we need to convert. */
2187 if (!useless_type_conversion_p (TREE_TYPE (curr
->op
), TREE_TYPE (t
)))
2188 t
= fold_convert (TREE_TYPE (curr
->op
), t
);
2190 if (TREE_CODE (t
) != INTEGER_CST
2191 && !operand_equal_p (t
, curr
->op
, 0))
2193 enum tree_code subcode
;
2194 tree newop1
, newop2
;
2195 if (!COMPARISON_CLASS_P (t
))
2197 extract_ops_from_tree (t
, &subcode
, &newop1
, &newop2
);
2198 STRIP_USELESS_TYPE_CONVERSION (newop1
);
2199 STRIP_USELESS_TYPE_CONVERSION (newop2
);
2200 if (!is_gimple_val (newop1
) || !is_gimple_val (newop2
))
2204 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2206 fprintf (dump_file
, "Equivalence: ");
2207 print_generic_expr (dump_file
, curr
->op
);
2208 fprintf (dump_file
, " %s ", op_symbol_code (opcode
));
2209 print_generic_expr (dump_file
, oe
->op
);
2210 fprintf (dump_file
, " -> ");
2211 print_generic_expr (dump_file
, t
);
2212 fprintf (dump_file
, "\n");
2215 /* Now we can delete oe, as it has been subsumed by the new combined
2217 ops
->ordered_remove (i
);
2218 reassociate_stats
.ops_eliminated
++;
2220 /* If t is the same as curr->op, we're done. Otherwise we must
2221 replace curr->op with t. Special case is if we got a constant
2222 back, in which case we add it to the end instead of in place of
2223 the current entry. */
2224 if (TREE_CODE (t
) == INTEGER_CST
)
2226 ops
->ordered_remove (currindex
);
2227 add_to_ops_vec (ops
, t
);
2229 else if (!operand_equal_p (t
, curr
->op
, 0))
2232 enum tree_code subcode
;
2235 gcc_assert (COMPARISON_CLASS_P (t
));
2236 extract_ops_from_tree (t
, &subcode
, &newop1
, &newop2
);
2237 STRIP_USELESS_TYPE_CONVERSION (newop1
);
2238 STRIP_USELESS_TYPE_CONVERSION (newop2
);
2239 gcc_checking_assert (is_gimple_val (newop1
)
2240 && is_gimple_val (newop2
));
2241 sum
= build_and_add_sum (TREE_TYPE (t
), newop1
, newop2
, subcode
);
2242 curr
->op
= gimple_get_lhs (sum
);
2251 /* Transform repeated addition of same values into multiply with
2254 transform_add_to_multiply (vec
<operand_entry
*> *ops
)
2257 tree op
= NULL_TREE
;
2259 int i
, start
= -1, end
= 0, count
= 0;
2260 auto_vec
<std::pair
<int, int> > indxs
;
2261 bool changed
= false;
2263 if (!INTEGRAL_TYPE_P (TREE_TYPE ((*ops
)[0]->op
))
2264 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE ((*ops
)[0]->op
))
2265 || !flag_unsafe_math_optimizations
))
2268 /* Look for repeated operands. */
2269 FOR_EACH_VEC_ELT (*ops
, i
, oe
)
2277 else if (operand_equal_p (oe
->op
, op
, 0))
2285 indxs
.safe_push (std::make_pair (start
, end
));
2293 indxs
.safe_push (std::make_pair (start
, end
));
2295 for (j
= indxs
.length () - 1; j
>= 0; --j
)
2297 /* Convert repeated operand addition to multiplication. */
2298 start
= indxs
[j
].first
;
2299 end
= indxs
[j
].second
;
2300 op
= (*ops
)[start
]->op
;
2301 count
= end
- start
+ 1;
2302 for (i
= end
; i
>= start
; --i
)
2303 ops
->unordered_remove (i
);
2304 tree tmp
= make_ssa_name (TREE_TYPE (op
));
2305 tree cst
= build_int_cst (integer_type_node
, count
);
2307 = gimple_build_assign (tmp
, MULT_EXPR
,
2308 op
, fold_convert (TREE_TYPE (op
), cst
));
2309 gimple_set_visited (mul_stmt
, true);
2310 add_to_ops_vec (ops
, tmp
, mul_stmt
);
2318 /* Perform various identities and other optimizations on the list of
2319 operand entries, stored in OPS. The tree code for the binary
2320 operation between all the operands is OPCODE. */
2323 optimize_ops_list (enum tree_code opcode
,
2324 vec
<operand_entry
*> *ops
)
2326 unsigned int length
= ops
->length ();
2329 operand_entry
*oelast
= NULL
;
2330 bool iterate
= false;
2335 oelast
= ops
->last ();
2337 /* If the last two are constants, pop the constants off, merge them
2338 and try the next two. */
2339 if (oelast
->rank
== 0 && is_gimple_min_invariant (oelast
->op
))
2341 operand_entry
*oelm1
= (*ops
)[length
- 2];
2343 if (oelm1
->rank
== 0
2344 && is_gimple_min_invariant (oelm1
->op
)
2345 && useless_type_conversion_p (TREE_TYPE (oelm1
->op
),
2346 TREE_TYPE (oelast
->op
)))
2348 tree folded
= fold_binary (opcode
, TREE_TYPE (oelm1
->op
),
2349 oelm1
->op
, oelast
->op
);
2351 if (folded
&& is_gimple_min_invariant (folded
))
2353 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2354 fprintf (dump_file
, "Merging constants\n");
2359 add_to_ops_vec (ops
, folded
);
2360 reassociate_stats
.constants_eliminated
++;
2362 optimize_ops_list (opcode
, ops
);
2368 eliminate_using_constants (opcode
, ops
);
2371 for (i
= 0; ops
->iterate (i
, &oe
);)
2375 if (eliminate_not_pairs (opcode
, ops
, i
, oe
))
2377 if (eliminate_duplicate_pair (opcode
, ops
, &done
, i
, oe
, oelast
)
2378 || (!done
&& eliminate_plus_minus_pair (opcode
, ops
, i
, oe
))
2379 || (!done
&& eliminate_redundant_comparison (opcode
, ops
, i
, oe
)))
2392 optimize_ops_list (opcode
, ops
);
2395 /* The following functions are subroutines to optimize_range_tests and allow
2396 it to try to change a logical combination of comparisons into a range
2400 X == 2 || X == 5 || X == 3 || X == 4
2404 (unsigned) (X - 2) <= 3
2406 For more information see comments above fold_test_range in fold-const.c,
2407 this implementation is for GIMPLE. */
2415 bool strict_overflow_p
;
2416 unsigned int idx
, next
;
2419 void dump_range_entry (FILE *file
, struct range_entry
*r
);
2420 void debug_range_entry (struct range_entry
*r
);
2422 /* Dump the range entry R to FILE, skipping its expression if SKIP_EXP. */
2425 dump_range_entry (FILE *file
, struct range_entry
*r
, bool skip_exp
)
2428 print_generic_expr (file
, r
->exp
);
2429 fprintf (file
, " %c[", r
->in_p
? '+' : '-');
2430 print_generic_expr (file
, r
->low
);
2432 print_generic_expr (file
, r
->high
);
2436 /* Dump the range entry R to STDERR. */
2439 debug_range_entry (struct range_entry
*r
)
2441 dump_range_entry (stderr
, r
, false);
2442 fputc ('\n', stderr
);
2445 /* This is similar to make_range in fold-const.c, but on top of
2446 GIMPLE instead of trees. If EXP is non-NULL, it should be
2447 an SSA_NAME and STMT argument is ignored, otherwise STMT
2448 argument should be a GIMPLE_COND. */
2451 init_range_entry (struct range_entry
*r
, tree exp
, gimple
*stmt
)
2455 bool is_bool
, strict_overflow_p
;
2459 r
->strict_overflow_p
= false;
2461 r
->high
= NULL_TREE
;
2462 if (exp
!= NULL_TREE
2463 && (TREE_CODE (exp
) != SSA_NAME
|| !INTEGRAL_TYPE_P (TREE_TYPE (exp
))))
2466 /* Start with simply saying "EXP != 0" and then look at the code of EXP
2467 and see if we can refine the range. Some of the cases below may not
2468 happen, but it doesn't seem worth worrying about this. We "continue"
2469 the outer loop when we've changed something; otherwise we "break"
2470 the switch, which will "break" the while. */
2471 low
= exp
? build_int_cst (TREE_TYPE (exp
), 0) : boolean_false_node
;
2474 strict_overflow_p
= false;
2476 if (exp
== NULL_TREE
)
2478 else if (TYPE_PRECISION (TREE_TYPE (exp
)) == 1)
2480 if (TYPE_UNSIGNED (TREE_TYPE (exp
)))
2485 else if (TREE_CODE (TREE_TYPE (exp
)) == BOOLEAN_TYPE
)
2490 enum tree_code code
;
2491 tree arg0
, arg1
, exp_type
;
2495 if (exp
!= NULL_TREE
)
2497 if (TREE_CODE (exp
) != SSA_NAME
2498 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp
))
2501 stmt
= SSA_NAME_DEF_STMT (exp
);
2502 if (!is_gimple_assign (stmt
))
2505 code
= gimple_assign_rhs_code (stmt
);
2506 arg0
= gimple_assign_rhs1 (stmt
);
2507 arg1
= gimple_assign_rhs2 (stmt
);
2508 exp_type
= TREE_TYPE (exp
);
2512 code
= gimple_cond_code (stmt
);
2513 arg0
= gimple_cond_lhs (stmt
);
2514 arg1
= gimple_cond_rhs (stmt
);
2515 exp_type
= boolean_type_node
;
2518 if (TREE_CODE (arg0
) != SSA_NAME
2519 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0
))
2521 loc
= gimple_location (stmt
);
2525 if (TREE_CODE (TREE_TYPE (exp
)) == BOOLEAN_TYPE
2526 /* Ensure the range is either +[-,0], +[0,0],
2527 -[-,0], -[0,0] or +[1,-], +[1,1], -[1,-] or
2528 -[1,1]. If it is e.g. +[-,-] or -[-,-]
2529 or similar expression of unconditional true or
2530 false, it should not be negated. */
2531 && ((high
&& integer_zerop (high
))
2532 || (low
&& integer_onep (low
))))
2545 if ((TYPE_PRECISION (exp_type
) == 1
2546 || TREE_CODE (exp_type
) == BOOLEAN_TYPE
)
2547 && TYPE_PRECISION (TREE_TYPE (arg0
)) > 1)
2550 else if (TYPE_PRECISION (TREE_TYPE (arg0
)) == 1)
2552 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)))
2557 else if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
)
2572 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
,
2574 &strict_overflow_p
);
2575 if (nexp
!= NULL_TREE
)
2578 gcc_assert (TREE_CODE (exp
) == SSA_NAME
);
2591 r
->strict_overflow_p
= strict_overflow_p
;
2595 /* Comparison function for qsort. Sort entries
2596 without SSA_NAME exp first, then with SSA_NAMEs sorted
2597 by increasing SSA_NAME_VERSION, and for the same SSA_NAMEs
2598 by increasing ->low and if ->low is the same, by increasing
2599 ->high. ->low == NULL_TREE means minimum, ->high == NULL_TREE
2603 range_entry_cmp (const void *a
, const void *b
)
2605 const struct range_entry
*p
= (const struct range_entry
*) a
;
2606 const struct range_entry
*q
= (const struct range_entry
*) b
;
2608 if (p
->exp
!= NULL_TREE
&& TREE_CODE (p
->exp
) == SSA_NAME
)
2610 if (q
->exp
!= NULL_TREE
&& TREE_CODE (q
->exp
) == SSA_NAME
)
2612 /* Group range_entries for the same SSA_NAME together. */
2613 if (SSA_NAME_VERSION (p
->exp
) < SSA_NAME_VERSION (q
->exp
))
2615 else if (SSA_NAME_VERSION (p
->exp
) > SSA_NAME_VERSION (q
->exp
))
2617 /* If ->low is different, NULL low goes first, then by
2619 if (p
->low
!= NULL_TREE
)
2621 if (q
->low
!= NULL_TREE
)
2623 tree tem
= fold_binary (LT_EXPR
, boolean_type_node
,
2625 if (tem
&& integer_onep (tem
))
2627 tem
= fold_binary (GT_EXPR
, boolean_type_node
,
2629 if (tem
&& integer_onep (tem
))
2635 else if (q
->low
!= NULL_TREE
)
2637 /* If ->high is different, NULL high goes last, before that by
2639 if (p
->high
!= NULL_TREE
)
2641 if (q
->high
!= NULL_TREE
)
2643 tree tem
= fold_binary (LT_EXPR
, boolean_type_node
,
2645 if (tem
&& integer_onep (tem
))
2647 tem
= fold_binary (GT_EXPR
, boolean_type_node
,
2649 if (tem
&& integer_onep (tem
))
2655 else if (q
->high
!= NULL_TREE
)
2657 /* If both ranges are the same, sort below by ascending idx. */
2662 else if (q
->exp
!= NULL_TREE
&& TREE_CODE (q
->exp
) == SSA_NAME
)
2665 if (p
->idx
< q
->idx
)
2669 gcc_checking_assert (p
->idx
> q
->idx
);
2674 /* Helper function for update_range_test. Force EXPR into an SSA_NAME,
2675 insert needed statements BEFORE or after GSI. */
2678 force_into_ssa_name (gimple_stmt_iterator
*gsi
, tree expr
, bool before
)
2680 enum gsi_iterator_update m
= before
? GSI_SAME_STMT
: GSI_CONTINUE_LINKING
;
2681 tree ret
= force_gimple_operand_gsi (gsi
, expr
, true, NULL_TREE
, before
, m
);
2682 if (TREE_CODE (ret
) != SSA_NAME
)
2684 gimple
*g
= gimple_build_assign (make_ssa_name (TREE_TYPE (ret
)), ret
);
2686 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2688 gsi_insert_after (gsi
, g
, GSI_CONTINUE_LINKING
);
2689 ret
= gimple_assign_lhs (g
);
2694 /* Helper routine of optimize_range_test.
2695 [EXP, IN_P, LOW, HIGH, STRICT_OVERFLOW_P] is a merged range for
2696 RANGE and OTHERRANGE through OTHERRANGE + COUNT - 1 ranges,
2697 OPCODE and OPS are arguments of optimize_range_tests. If OTHERRANGE
2698 is NULL, OTHERRANGEP should not be and then OTHERRANGEP points to
2699 an array of COUNT pointers to other ranges. Return
2700 true if the range merge has been successful.
2701 If OPCODE is ERROR_MARK, this is called from within
2702 maybe_optimize_range_tests and is performing inter-bb range optimization.
2703 In that case, whether an op is BIT_AND_EXPR or BIT_IOR_EXPR is found in
2707 update_range_test (struct range_entry
*range
, struct range_entry
*otherrange
,
2708 struct range_entry
**otherrangep
,
2709 unsigned int count
, enum tree_code opcode
,
2710 vec
<operand_entry
*> *ops
, tree exp
, gimple_seq seq
,
2711 bool in_p
, tree low
, tree high
, bool strict_overflow_p
)
2713 operand_entry
*oe
= (*ops
)[range
->idx
];
2715 gimple
*stmt
= op
? SSA_NAME_DEF_STMT (op
)
2716 : last_stmt (BASIC_BLOCK_FOR_FN (cfun
, oe
->id
));
2717 location_t loc
= gimple_location (stmt
);
2718 tree optype
= op
? TREE_TYPE (op
) : boolean_type_node
;
2719 tree tem
= build_range_check (loc
, optype
, unshare_expr (exp
),
2721 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
2722 gimple_stmt_iterator gsi
;
2723 unsigned int i
, uid
;
2725 if (tem
== NULL_TREE
)
2728 /* If op is default def SSA_NAME, there is no place to insert the
2729 new comparison. Give up, unless we can use OP itself as the
2731 if (op
&& SSA_NAME_IS_DEFAULT_DEF (op
))
2733 if (op
== range
->exp
2734 && ((TYPE_PRECISION (optype
) == 1 && TYPE_UNSIGNED (optype
))
2735 || TREE_CODE (optype
) == BOOLEAN_TYPE
)
2737 || (TREE_CODE (tem
) == EQ_EXPR
2738 && TREE_OPERAND (tem
, 0) == op
2739 && integer_onep (TREE_OPERAND (tem
, 1))))
2740 && opcode
!= BIT_IOR_EXPR
2741 && (opcode
!= ERROR_MARK
|| oe
->rank
!= BIT_IOR_EXPR
))
2750 if (strict_overflow_p
&& issue_strict_overflow_warning (wc
))
2751 warning_at (loc
, OPT_Wstrict_overflow
,
2752 "assuming signed overflow does not occur "
2753 "when simplifying range test");
2755 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2757 struct range_entry
*r
;
2758 fprintf (dump_file
, "Optimizing range tests ");
2759 dump_range_entry (dump_file
, range
, false);
2760 for (i
= 0; i
< count
; i
++)
2767 && r
->exp
!= range
->exp
2768 && TREE_CODE (r
->exp
) == SSA_NAME
)
2770 fprintf (dump_file
, " and ");
2771 dump_range_entry (dump_file
, r
, false);
2775 fprintf (dump_file
, " and");
2776 dump_range_entry (dump_file
, r
, true);
2779 fprintf (dump_file
, "\n into ");
2780 print_generic_expr (dump_file
, tem
);
2781 fprintf (dump_file
, "\n");
2784 if (opcode
== BIT_IOR_EXPR
2785 || (opcode
== ERROR_MARK
&& oe
->rank
== BIT_IOR_EXPR
))
2786 tem
= invert_truthvalue_loc (loc
, tem
);
2788 tem
= fold_convert_loc (loc
, optype
, tem
);
2791 gsi
= gsi_for_stmt (stmt
);
2792 uid
= gimple_uid (stmt
);
2800 gcc_checking_assert (tem
== op
);
2801 /* In rare cases range->exp can be equal to lhs of stmt.
2802 In that case we have to insert after the stmt rather then before
2803 it. If stmt is a PHI, insert it at the start of the basic block. */
2804 else if (op
!= range
->exp
)
2806 gsi_insert_seq_before (&gsi
, seq
, GSI_SAME_STMT
);
2807 tem
= force_into_ssa_name (&gsi
, tem
, true);
2810 else if (gimple_code (stmt
) != GIMPLE_PHI
)
2812 gsi_insert_seq_after (&gsi
, seq
, GSI_CONTINUE_LINKING
);
2813 tem
= force_into_ssa_name (&gsi
, tem
, false);
2817 gsi
= gsi_after_labels (gimple_bb (stmt
));
2818 if (!gsi_end_p (gsi
))
2819 uid
= gimple_uid (gsi_stmt (gsi
));
2822 gsi
= gsi_start_bb (gimple_bb (stmt
));
2824 while (!gsi_end_p (gsi
))
2826 uid
= gimple_uid (gsi_stmt (gsi
));
2830 gsi_insert_seq_before (&gsi
, seq
, GSI_SAME_STMT
);
2831 tem
= force_into_ssa_name (&gsi
, tem
, true);
2832 if (gsi_end_p (gsi
))
2833 gsi
= gsi_last_bb (gimple_bb (stmt
));
2837 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
2838 if (gimple_uid (gsi_stmt (gsi
)))
2841 gimple_set_uid (gsi_stmt (gsi
), uid
);
2848 range
->strict_overflow_p
= false;
2850 for (i
= 0; i
< count
; i
++)
2853 range
= otherrange
+ i
;
2855 range
= otherrangep
[i
];
2856 oe
= (*ops
)[range
->idx
];
2857 /* Now change all the other range test immediate uses, so that
2858 those tests will be optimized away. */
2859 if (opcode
== ERROR_MARK
)
2862 oe
->op
= build_int_cst (TREE_TYPE (oe
->op
),
2863 oe
->rank
== BIT_IOR_EXPR
? 0 : 1);
2865 oe
->op
= (oe
->rank
== BIT_IOR_EXPR
2866 ? boolean_false_node
: boolean_true_node
);
2869 oe
->op
= error_mark_node
;
2870 range
->exp
= NULL_TREE
;
2871 range
->low
= NULL_TREE
;
2872 range
->high
= NULL_TREE
;
2877 /* Optimize X == CST1 || X == CST2
2878 if popcount (CST1 ^ CST2) == 1 into
2879 (X & ~(CST1 ^ CST2)) == (CST1 & ~(CST1 ^ CST2)).
2880 Similarly for ranges. E.g.
2881 X != 2 && X != 3 && X != 10 && X != 11
2882 will be transformed by the previous optimization into
2883 !((X - 2U) <= 1U || (X - 10U) <= 1U)
2884 and this loop can transform that into
2885 !(((X & ~8) - 2U) <= 1U). */
2888 optimize_range_tests_xor (enum tree_code opcode
, tree type
,
2889 tree lowi
, tree lowj
, tree highi
, tree highj
,
2890 vec
<operand_entry
*> *ops
,
2891 struct range_entry
*rangei
,
2892 struct range_entry
*rangej
)
2894 tree lowxor
, highxor
, tem
, exp
;
2895 /* Check lowi ^ lowj == highi ^ highj and
2896 popcount (lowi ^ lowj) == 1. */
2897 lowxor
= fold_binary (BIT_XOR_EXPR
, type
, lowi
, lowj
);
2898 if (lowxor
== NULL_TREE
|| TREE_CODE (lowxor
) != INTEGER_CST
)
2900 if (!integer_pow2p (lowxor
))
2902 highxor
= fold_binary (BIT_XOR_EXPR
, type
, highi
, highj
);
2903 if (!tree_int_cst_equal (lowxor
, highxor
))
2907 scalar_int_mode mode
= as_a
<scalar_int_mode
> (TYPE_MODE (type
));
2908 int prec
= GET_MODE_PRECISION (mode
);
2909 if (TYPE_PRECISION (type
) < prec
2910 || (wi::to_wide (TYPE_MIN_VALUE (type
))
2911 != wi::min_value (prec
, TYPE_SIGN (type
)))
2912 || (wi::to_wide (TYPE_MAX_VALUE (type
))
2913 != wi::max_value (prec
, TYPE_SIGN (type
))))
2915 type
= build_nonstandard_integer_type (prec
, TYPE_UNSIGNED (type
));
2916 exp
= fold_convert (type
, exp
);
2917 lowxor
= fold_convert (type
, lowxor
);
2918 lowi
= fold_convert (type
, lowi
);
2919 highi
= fold_convert (type
, highi
);
2921 tem
= fold_build1 (BIT_NOT_EXPR
, type
, lowxor
);
2922 exp
= fold_build2 (BIT_AND_EXPR
, type
, exp
, tem
);
2923 lowj
= fold_build2 (BIT_AND_EXPR
, type
, lowi
, tem
);
2924 highj
= fold_build2 (BIT_AND_EXPR
, type
, highi
, tem
);
2925 if (update_range_test (rangei
, rangej
, NULL
, 1, opcode
, ops
, exp
,
2926 NULL
, rangei
->in_p
, lowj
, highj
,
2927 rangei
->strict_overflow_p
2928 || rangej
->strict_overflow_p
))
2933 /* Optimize X == CST1 || X == CST2
2934 if popcount (CST2 - CST1) == 1 into
2935 ((X - CST1) & ~(CST2 - CST1)) == 0.
2936 Similarly for ranges. E.g.
2937 X == 43 || X == 76 || X == 44 || X == 78 || X == 77 || X == 46
2938 || X == 75 || X == 45
2939 will be transformed by the previous optimization into
2940 (X - 43U) <= 3U || (X - 75U) <= 3U
2941 and this loop can transform that into
2942 ((X - 43U) & ~(75U - 43U)) <= 3U. */
2944 optimize_range_tests_diff (enum tree_code opcode
, tree type
,
2945 tree lowi
, tree lowj
, tree highi
, tree highj
,
2946 vec
<operand_entry
*> *ops
,
2947 struct range_entry
*rangei
,
2948 struct range_entry
*rangej
)
2950 tree tem1
, tem2
, mask
;
2951 /* Check highi - lowi == highj - lowj. */
2952 tem1
= fold_binary (MINUS_EXPR
, type
, highi
, lowi
);
2953 if (tem1
== NULL_TREE
|| TREE_CODE (tem1
) != INTEGER_CST
)
2955 tem2
= fold_binary (MINUS_EXPR
, type
, highj
, lowj
);
2956 if (!tree_int_cst_equal (tem1
, tem2
))
2958 /* Check popcount (lowj - lowi) == 1. */
2959 tem1
= fold_binary (MINUS_EXPR
, type
, lowj
, lowi
);
2960 if (tem1
== NULL_TREE
|| TREE_CODE (tem1
) != INTEGER_CST
)
2962 if (!integer_pow2p (tem1
))
2965 scalar_int_mode mode
= as_a
<scalar_int_mode
> (TYPE_MODE (type
));
2966 int prec
= GET_MODE_PRECISION (mode
);
2967 if (TYPE_PRECISION (type
) < prec
2968 || (wi::to_wide (TYPE_MIN_VALUE (type
))
2969 != wi::min_value (prec
, TYPE_SIGN (type
)))
2970 || (wi::to_wide (TYPE_MAX_VALUE (type
))
2971 != wi::max_value (prec
, TYPE_SIGN (type
))))
2972 type
= build_nonstandard_integer_type (prec
, 1);
2974 type
= unsigned_type_for (type
);
2975 tem1
= fold_convert (type
, tem1
);
2976 tem2
= fold_convert (type
, tem2
);
2977 lowi
= fold_convert (type
, lowi
);
2978 mask
= fold_build1 (BIT_NOT_EXPR
, type
, tem1
);
2979 tem1
= fold_build2 (MINUS_EXPR
, type
,
2980 fold_convert (type
, rangei
->exp
), lowi
);
2981 tem1
= fold_build2 (BIT_AND_EXPR
, type
, tem1
, mask
);
2982 lowj
= build_int_cst (type
, 0);
2983 if (update_range_test (rangei
, rangej
, NULL
, 1, opcode
, ops
, tem1
,
2984 NULL
, rangei
->in_p
, lowj
, tem2
,
2985 rangei
->strict_overflow_p
2986 || rangej
->strict_overflow_p
))
2991 /* It does some common checks for function optimize_range_tests_xor and
2992 optimize_range_tests_diff.
2993 If OPTIMIZE_XOR is TRUE, it calls optimize_range_tests_xor.
2994 Else it calls optimize_range_tests_diff. */
2997 optimize_range_tests_1 (enum tree_code opcode
, int first
, int length
,
2998 bool optimize_xor
, vec
<operand_entry
*> *ops
,
2999 struct range_entry
*ranges
)
3002 bool any_changes
= false;
3003 for (i
= first
; i
< length
; i
++)
3005 tree lowi
, highi
, lowj
, highj
, type
, tem
;
3007 if (ranges
[i
].exp
== NULL_TREE
|| ranges
[i
].in_p
)
3009 type
= TREE_TYPE (ranges
[i
].exp
);
3010 if (!INTEGRAL_TYPE_P (type
))
3012 lowi
= ranges
[i
].low
;
3013 if (lowi
== NULL_TREE
)
3014 lowi
= TYPE_MIN_VALUE (type
);
3015 highi
= ranges
[i
].high
;
3016 if (highi
== NULL_TREE
)
3018 for (j
= i
+ 1; j
< length
&& j
< i
+ 64; j
++)
3021 if (ranges
[i
].exp
!= ranges
[j
].exp
|| ranges
[j
].in_p
)
3023 lowj
= ranges
[j
].low
;
3024 if (lowj
== NULL_TREE
)
3026 highj
= ranges
[j
].high
;
3027 if (highj
== NULL_TREE
)
3028 highj
= TYPE_MAX_VALUE (type
);
3029 /* Check lowj > highi. */
3030 tem
= fold_binary (GT_EXPR
, boolean_type_node
,
3032 if (tem
== NULL_TREE
|| !integer_onep (tem
))
3035 changes
= optimize_range_tests_xor (opcode
, type
, lowi
, lowj
,
3037 ranges
+ i
, ranges
+ j
);
3039 changes
= optimize_range_tests_diff (opcode
, type
, lowi
, lowj
,
3041 ranges
+ i
, ranges
+ j
);
3052 /* Helper function of optimize_range_tests_to_bit_test. Handle a single
3053 range, EXP, LOW, HIGH, compute bit mask of bits to test and return
3054 EXP on success, NULL otherwise. */
3057 extract_bit_test_mask (tree exp
, int prec
, tree totallow
, tree low
, tree high
,
3058 wide_int
*mask
, tree
*totallowp
)
3060 tree tem
= int_const_binop (MINUS_EXPR
, high
, low
);
3061 if (tem
== NULL_TREE
3062 || TREE_CODE (tem
) != INTEGER_CST
3063 || TREE_OVERFLOW (tem
)
3064 || tree_int_cst_sgn (tem
) == -1
3065 || compare_tree_int (tem
, prec
) != -1)
3068 unsigned HOST_WIDE_INT max
= tree_to_uhwi (tem
) + 1;
3069 *mask
= wi::shifted_mask (0, max
, false, prec
);
3070 if (TREE_CODE (exp
) == BIT_AND_EXPR
3071 && TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
3073 widest_int msk
= wi::to_widest (TREE_OPERAND (exp
, 1));
3074 msk
= wi::zext (~msk
, TYPE_PRECISION (TREE_TYPE (exp
)));
3075 if (wi::popcount (msk
) == 1
3076 && wi::ltu_p (msk
, prec
- max
))
3078 *mask
|= wi::shifted_mask (msk
.to_uhwi (), max
, false, prec
);
3079 max
+= msk
.to_uhwi ();
3080 exp
= TREE_OPERAND (exp
, 0);
3081 if (integer_zerop (low
)
3082 && TREE_CODE (exp
) == PLUS_EXPR
3083 && TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
3085 tree ret
= TREE_OPERAND (exp
, 0);
3088 = wi::neg (wi::sext (wi::to_widest (TREE_OPERAND (exp
, 1)),
3089 TYPE_PRECISION (TREE_TYPE (low
))));
3090 tree tbias
= wide_int_to_tree (TREE_TYPE (ret
), bias
);
3096 else if (!tree_int_cst_lt (totallow
, tbias
))
3098 bias
= wi::to_widest (tbias
);
3099 bias
-= wi::to_widest (totallow
);
3100 if (bias
>= 0 && bias
< prec
- max
)
3102 *mask
= wi::lshift (*mask
, bias
);
3110 if (!tree_int_cst_lt (totallow
, low
))
3112 tem
= int_const_binop (MINUS_EXPR
, low
, totallow
);
3113 if (tem
== NULL_TREE
3114 || TREE_CODE (tem
) != INTEGER_CST
3115 || TREE_OVERFLOW (tem
)
3116 || compare_tree_int (tem
, prec
- max
) == 1)
3119 *mask
= wi::lshift (*mask
, wi::to_widest (tem
));
3123 /* Attempt to optimize small range tests using bit test.
3125 X != 43 && X != 76 && X != 44 && X != 78 && X != 49
3126 && X != 77 && X != 46 && X != 75 && X != 45 && X != 82
3127 has been by earlier optimizations optimized into:
3128 ((X - 43U) & ~32U) > 3U && X != 49 && X != 82
3129 As all the 43 through 82 range is less than 64 numbers,
3130 for 64-bit word targets optimize that into:
3131 (X - 43U) > 40U && ((1 << (X - 43U)) & 0x8F0000004FULL) == 0 */
3134 optimize_range_tests_to_bit_test (enum tree_code opcode
, int first
, int length
,
3135 vec
<operand_entry
*> *ops
,
3136 struct range_entry
*ranges
)
3139 bool any_changes
= false;
3140 int prec
= GET_MODE_BITSIZE (word_mode
);
3141 auto_vec
<struct range_entry
*, 64> candidates
;
3143 for (i
= first
; i
< length
- 1; i
++)
3145 tree lowi
, highi
, lowj
, highj
, type
;
3147 if (ranges
[i
].exp
== NULL_TREE
|| ranges
[i
].in_p
)
3149 type
= TREE_TYPE (ranges
[i
].exp
);
3150 if (!INTEGRAL_TYPE_P (type
))
3152 lowi
= ranges
[i
].low
;
3153 if (lowi
== NULL_TREE
)
3154 lowi
= TYPE_MIN_VALUE (type
);
3155 highi
= ranges
[i
].high
;
3156 if (highi
== NULL_TREE
)
3159 tree exp
= extract_bit_test_mask (ranges
[i
].exp
, prec
, lowi
, lowi
,
3160 highi
, &mask
, &lowi
);
3161 if (exp
== NULL_TREE
)
3163 bool strict_overflow_p
= ranges
[i
].strict_overflow_p
;
3164 candidates
.truncate (0);
3165 int end
= MIN (i
+ 64, length
);
3166 for (j
= i
+ 1; j
< end
; j
++)
3169 if (ranges
[j
].exp
== NULL_TREE
|| ranges
[j
].in_p
)
3171 if (ranges
[j
].exp
== exp
)
3173 else if (TREE_CODE (ranges
[j
].exp
) == BIT_AND_EXPR
)
3175 exp2
= TREE_OPERAND (ranges
[j
].exp
, 0);
3178 else if (TREE_CODE (exp2
) == PLUS_EXPR
)
3180 exp2
= TREE_OPERAND (exp2
, 0);
3190 lowj
= ranges
[j
].low
;
3191 if (lowj
== NULL_TREE
)
3193 highj
= ranges
[j
].high
;
3194 if (highj
== NULL_TREE
)
3195 highj
= TYPE_MAX_VALUE (type
);
3197 exp2
= extract_bit_test_mask (ranges
[j
].exp
, prec
, lowi
, lowj
,
3198 highj
, &mask2
, NULL
);
3202 strict_overflow_p
|= ranges
[j
].strict_overflow_p
;
3203 candidates
.safe_push (&ranges
[j
]);
3206 /* If every possible relative value of the expression is a valid shift
3207 amount, then we can merge the entry test in the bit test. In this
3208 case, if we would need otherwise 2 or more comparisons, then use
3209 the bit test; in the other cases, the threshold is 3 comparisons. */
3211 bool entry_test_needed
;
3212 if (TREE_CODE (exp
) == SSA_NAME
3213 && get_range_info (exp
, &min
, &max
) == VR_RANGE
3214 && wi::leu_p (max
- min
, prec
- 1))
3216 wide_int ilowi
= wi::to_wide (lowi
);
3217 if (wi::lt_p (min
, ilowi
, TYPE_SIGN (TREE_TYPE (lowi
))))
3219 lowi
= wide_int_to_tree (TREE_TYPE (lowi
), min
);
3220 mask
= wi::lshift (mask
, ilowi
- min
);
3222 else if (wi::gt_p (min
, ilowi
, TYPE_SIGN (TREE_TYPE (lowi
))))
3224 lowi
= wide_int_to_tree (TREE_TYPE (lowi
), min
);
3225 mask
= wi::lrshift (mask
, min
- ilowi
);
3227 entry_test_needed
= false;
3230 entry_test_needed
= true;
3231 if (candidates
.length () >= (entry_test_needed
? 2 : 1))
3233 tree high
= wide_int_to_tree (TREE_TYPE (lowi
),
3234 wi::to_widest (lowi
)
3235 + prec
- 1 - wi::clz (mask
));
3236 operand_entry
*oe
= (*ops
)[ranges
[i
].idx
];
3238 gimple
*stmt
= op
? SSA_NAME_DEF_STMT (op
)
3239 : last_stmt (BASIC_BLOCK_FOR_FN (cfun
, oe
->id
));
3240 location_t loc
= gimple_location (stmt
);
3241 tree optype
= op
? TREE_TYPE (op
) : boolean_type_node
;
3243 /* See if it isn't cheaper to pretend the minimum value of the
3244 range is 0, if maximum value is small enough.
3245 We can avoid then subtraction of the minimum value, but the
3246 mask constant could be perhaps more expensive. */
3247 if (compare_tree_int (lowi
, 0) > 0
3248 && compare_tree_int (high
, prec
) < 0)
3251 HOST_WIDE_INT m
= tree_to_uhwi (lowi
);
3252 rtx reg
= gen_raw_REG (word_mode
, 10000);
3253 bool speed_p
= optimize_bb_for_speed_p (gimple_bb (stmt
));
3254 cost_diff
= set_src_cost (gen_rtx_PLUS (word_mode
, reg
,
3256 word_mode
, speed_p
);
3257 rtx r
= immed_wide_int_const (mask
, word_mode
);
3258 cost_diff
+= set_src_cost (gen_rtx_AND (word_mode
, reg
, r
),
3259 word_mode
, speed_p
);
3260 r
= immed_wide_int_const (wi::lshift (mask
, m
), word_mode
);
3261 cost_diff
-= set_src_cost (gen_rtx_AND (word_mode
, reg
, r
),
3262 word_mode
, speed_p
);
3265 mask
= wi::lshift (mask
, m
);
3266 lowi
= build_zero_cst (TREE_TYPE (lowi
));
3271 if (entry_test_needed
)
3273 tem
= build_range_check (loc
, optype
, unshare_expr (exp
),
3275 if (tem
== NULL_TREE
|| is_gimple_val (tem
))
3280 tree etype
= unsigned_type_for (TREE_TYPE (exp
));
3281 exp
= fold_build2_loc (loc
, MINUS_EXPR
, etype
,
3282 fold_convert_loc (loc
, etype
, exp
),
3283 fold_convert_loc (loc
, etype
, lowi
));
3284 exp
= fold_convert_loc (loc
, integer_type_node
, exp
);
3285 tree word_type
= lang_hooks
.types
.type_for_mode (word_mode
, 1);
3286 exp
= fold_build2_loc (loc
, LSHIFT_EXPR
, word_type
,
3287 build_int_cst (word_type
, 1), exp
);
3288 exp
= fold_build2_loc (loc
, BIT_AND_EXPR
, word_type
, exp
,
3289 wide_int_to_tree (word_type
, mask
));
3290 exp
= fold_build2_loc (loc
, EQ_EXPR
, optype
, exp
,
3291 build_zero_cst (word_type
));
3292 if (is_gimple_val (exp
))
3295 /* The shift might have undefined behavior if TEM is true,
3296 but reassociate_bb isn't prepared to have basic blocks
3297 split when it is running. So, temporarily emit a code
3298 with BIT_IOR_EXPR instead of &&, and fix it up in
3300 gimple_seq seq
= NULL
;
3303 tem
= force_gimple_operand (tem
, &seq
, true, NULL_TREE
);
3304 gcc_assert (TREE_CODE (tem
) == SSA_NAME
);
3305 gimple_set_visited (SSA_NAME_DEF_STMT (tem
), true);
3308 exp
= force_gimple_operand (exp
, &seq2
, true, NULL_TREE
);
3309 gimple_seq_add_seq_without_update (&seq
, seq2
);
3310 gcc_assert (TREE_CODE (exp
) == SSA_NAME
);
3311 gimple_set_visited (SSA_NAME_DEF_STMT (exp
), true);
3314 gimple
*g
= gimple_build_assign (make_ssa_name (optype
),
3315 BIT_IOR_EXPR
, tem
, exp
);
3316 gimple_set_location (g
, loc
);
3317 gimple_seq_add_stmt_without_update (&seq
, g
);
3318 exp
= gimple_assign_lhs (g
);
3320 tree val
= build_zero_cst (optype
);
3321 if (update_range_test (&ranges
[i
], NULL
, candidates
.address (),
3322 candidates
.length (), opcode
, ops
, exp
,
3323 seq
, false, val
, val
, strict_overflow_p
))
3327 reassoc_branch_fixups
.safe_push (tem
);
3330 gimple_seq_discard (seq
);
3336 /* Optimize x != 0 && y != 0 && z != 0 into (x | y | z) != 0
3337 and similarly x != -1 && y != -1 && y != -1 into (x & y & z) != -1. */
3340 optimize_range_tests_cmp_bitwise (enum tree_code opcode
, int first
, int length
,
3341 vec
<operand_entry
*> *ops
,
3342 struct range_entry
*ranges
)
3346 bool any_changes
= false;
3347 auto_vec
<int, 128> buckets
;
3348 auto_vec
<int, 32> chains
;
3349 auto_vec
<struct range_entry
*, 32> candidates
;
3351 for (i
= first
; i
< length
; i
++)
3353 if (ranges
[i
].exp
== NULL_TREE
3354 || TREE_CODE (ranges
[i
].exp
) != SSA_NAME
3356 || TYPE_PRECISION (TREE_TYPE (ranges
[i
].exp
)) <= 1
3357 || TREE_CODE (TREE_TYPE (ranges
[i
].exp
)) == BOOLEAN_TYPE
3358 || ranges
[i
].low
== NULL_TREE
3359 || ranges
[i
].low
!= ranges
[i
].high
)
3362 bool zero_p
= integer_zerop (ranges
[i
].low
);
3363 if (!zero_p
&& !integer_all_onesp (ranges
[i
].low
))
3366 b
= TYPE_PRECISION (TREE_TYPE (ranges
[i
].exp
)) * 2 + !zero_p
;
3367 if (buckets
.length () <= b
)
3368 buckets
.safe_grow_cleared (b
+ 1);
3369 if (chains
.length () <= (unsigned) i
)
3370 chains
.safe_grow (i
+ 1);
3371 chains
[i
] = buckets
[b
];
3375 FOR_EACH_VEC_ELT (buckets
, b
, i
)
3376 if (i
&& chains
[i
- 1])
3379 for (j
= chains
[i
- 1]; j
; j
= chains
[j
- 1])
3381 gimple
*gk
= SSA_NAME_DEF_STMT (ranges
[k
- 1].exp
);
3382 gimple
*gj
= SSA_NAME_DEF_STMT (ranges
[j
- 1].exp
);
3383 if (reassoc_stmt_dominates_stmt_p (gk
, gj
))
3386 tree type1
= TREE_TYPE (ranges
[k
- 1].exp
);
3387 tree type2
= NULL_TREE
;
3388 bool strict_overflow_p
= false;
3389 candidates
.truncate (0);
3390 for (j
= i
; j
; j
= chains
[j
- 1])
3392 tree type
= TREE_TYPE (ranges
[j
- 1].exp
);
3393 strict_overflow_p
|= ranges
[j
- 1].strict_overflow_p
;
3395 || useless_type_conversion_p (type1
, type
))
3397 else if (type2
== NULL_TREE
3398 || useless_type_conversion_p (type2
, type
))
3400 if (type2
== NULL_TREE
)
3402 candidates
.safe_push (&ranges
[j
- 1]);
3405 unsigned l
= candidates
.length ();
3406 for (j
= i
; j
; j
= chains
[j
- 1])
3408 tree type
= TREE_TYPE (ranges
[j
- 1].exp
);
3411 if (useless_type_conversion_p (type1
, type
))
3413 else if (type2
== NULL_TREE
3414 || useless_type_conversion_p (type2
, type
))
3416 candidates
.safe_push (&ranges
[j
- 1]);
3418 gimple_seq seq
= NULL
;
3419 tree op
= NULL_TREE
;
3421 struct range_entry
*r
;
3422 candidates
.safe_push (&ranges
[k
- 1]);
3423 FOR_EACH_VEC_ELT (candidates
, id
, r
)
3433 g
= gimple_build_assign (make_ssa_name (type1
), NOP_EXPR
, op
);
3434 gimple_seq_add_stmt_without_update (&seq
, g
);
3435 op
= gimple_assign_lhs (g
);
3437 tree type
= TREE_TYPE (r
->exp
);
3439 if (id
>= l
&& !useless_type_conversion_p (type1
, type
))
3441 g
= gimple_build_assign (make_ssa_name (type1
), NOP_EXPR
, exp
);
3442 gimple_seq_add_stmt_without_update (&seq
, g
);
3443 exp
= gimple_assign_lhs (g
);
3445 g
= gimple_build_assign (make_ssa_name (id
>= l
? type1
: type2
),
3446 (b
& 1) ? BIT_AND_EXPR
: BIT_IOR_EXPR
,
3448 gimple_seq_add_stmt_without_update (&seq
, g
);
3449 op
= gimple_assign_lhs (g
);
3452 if (update_range_test (&ranges
[k
- 1], NULL
, candidates
.address (),
3453 candidates
.length (), opcode
, ops
, op
,
3454 seq
, true, ranges
[k
- 1].low
,
3455 ranges
[k
- 1].low
, strict_overflow_p
))
3458 gimple_seq_discard (seq
);
3464 /* Attempt to optimize for signed a and b where b is known to be >= 0:
3465 a >= 0 && a < b into (unsigned) a < (unsigned) b
3466 a >= 0 && a <= b into (unsigned) a <= (unsigned) b */
3469 optimize_range_tests_var_bound (enum tree_code opcode
, int first
, int length
,
3470 vec
<operand_entry
*> *ops
,
3471 struct range_entry
*ranges
,
3472 basic_block first_bb
)
3475 bool any_changes
= false;
3476 hash_map
<tree
, int> *map
= NULL
;
3478 for (i
= first
; i
< length
; i
++)
3480 if (ranges
[i
].exp
== NULL_TREE
3481 || TREE_CODE (ranges
[i
].exp
) != SSA_NAME
3485 tree type
= TREE_TYPE (ranges
[i
].exp
);
3486 if (!INTEGRAL_TYPE_P (type
)
3487 || TYPE_UNSIGNED (type
)
3488 || ranges
[i
].low
== NULL_TREE
3489 || !integer_zerop (ranges
[i
].low
)
3490 || ranges
[i
].high
!= NULL_TREE
)
3492 /* EXP >= 0 here. */
3494 map
= new hash_map
<tree
, int>;
3495 map
->put (ranges
[i
].exp
, i
);
3501 for (i
= 0; i
< length
; i
++)
3503 bool in_p
= ranges
[i
].in_p
;
3504 if (ranges
[i
].low
== NULL_TREE
3505 || ranges
[i
].high
== NULL_TREE
)
3507 if (!integer_zerop (ranges
[i
].low
)
3508 || !integer_zerop (ranges
[i
].high
))
3511 && TYPE_PRECISION (TREE_TYPE (ranges
[i
].exp
)) == 1
3512 && TYPE_UNSIGNED (TREE_TYPE (ranges
[i
].exp
))
3513 && integer_onep (ranges
[i
].low
)
3514 && integer_onep (ranges
[i
].high
))
3525 if (TREE_CODE (ranges
[i
].exp
) != SSA_NAME
)
3527 stmt
= SSA_NAME_DEF_STMT (ranges
[i
].exp
);
3528 if (!is_gimple_assign (stmt
))
3530 ccode
= gimple_assign_rhs_code (stmt
);
3531 rhs1
= gimple_assign_rhs1 (stmt
);
3532 rhs2
= gimple_assign_rhs2 (stmt
);
3536 operand_entry
*oe
= (*ops
)[ranges
[i
].idx
];
3537 stmt
= last_stmt (BASIC_BLOCK_FOR_FN (cfun
, oe
->id
));
3538 if (gimple_code (stmt
) != GIMPLE_COND
)
3540 ccode
= gimple_cond_code (stmt
);
3541 rhs1
= gimple_cond_lhs (stmt
);
3542 rhs2
= gimple_cond_rhs (stmt
);
3545 if (TREE_CODE (rhs1
) != SSA_NAME
3546 || rhs2
== NULL_TREE
3547 || TREE_CODE (rhs2
) != SSA_NAME
)
3561 ccode
= invert_tree_comparison (ccode
, false);
3566 std::swap (rhs1
, rhs2
);
3567 ccode
= swap_tree_comparison (ccode
);
3576 int *idx
= map
->get (rhs1
);
3580 /* maybe_optimize_range_tests allows statements without side-effects
3581 in the basic blocks as long as they are consumed in the same bb.
3582 Make sure rhs2's def stmt is not among them, otherwise we can't
3583 use safely get_nonzero_bits on it. E.g. in:
3584 # RANGE [-83, 1] NONZERO 173
3585 # k_32 = PHI <k_47(13), k_12(9)>
3588 goto <bb 5>; [26.46%]
3590 goto <bb 9>; [73.54%]
3592 <bb 5> [local count: 140323371]:
3593 # RANGE [0, 1] NONZERO 1
3595 # RANGE [0, 4] NONZERO 4
3597 # RANGE [0, 4] NONZERO 4
3598 iftmp.0_44 = (char) _21;
3599 if (k_32 < iftmp.0_44)
3600 goto <bb 6>; [84.48%]
3602 goto <bb 9>; [15.52%]
3603 the ranges on _5/_21/iftmp.0_44 are flow sensitive, assume that
3604 k_32 >= 0. If we'd optimize k_32 >= 0 to true and k_32 < iftmp.0_44
3605 to (unsigned) k_32 < (unsigned) iftmp.0_44, then we would execute
3606 those stmts even for negative k_32 and the value ranges would be no
3607 longer guaranteed and so the optimization would be invalid. */
3608 while (opcode
== ERROR_MARK
)
3610 gimple
*g
= SSA_NAME_DEF_STMT (rhs2
);
3611 basic_block bb2
= gimple_bb (g
);
3614 && dominated_by_p (CDI_DOMINATORS
, bb2
, first_bb
))
3616 /* As an exception, handle a few common cases. */
3617 if (gimple_assign_cast_p (g
)
3618 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (g
))))
3620 tree op0
= gimple_assign_rhs1 (g
);
3621 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
3622 && (TYPE_PRECISION (TREE_TYPE (rhs2
))
3623 > TYPE_PRECISION (TREE_TYPE (op0
))))
3624 /* Zero-extension is always ok. */
3626 else if (TYPE_PRECISION (TREE_TYPE (rhs2
))
3627 == TYPE_PRECISION (TREE_TYPE (op0
))
3628 && TREE_CODE (op0
) == SSA_NAME
)
3630 /* Cast from signed to unsigned or vice versa. Retry
3631 with the op0 as new rhs2. */
3636 else if (is_gimple_assign (g
)
3637 && gimple_assign_rhs_code (g
) == BIT_AND_EXPR
3638 && TREE_CODE (gimple_assign_rhs2 (g
)) == INTEGER_CST
3639 && !wi::neg_p (wi::to_wide (gimple_assign_rhs2 (g
))))
3640 /* Masking with INTEGER_CST with MSB clear is always ok
3647 if (rhs2
== NULL_TREE
)
3650 wide_int nz
= get_nonzero_bits (rhs2
);
3654 /* We have EXP < RHS2 or EXP <= RHS2 where EXP >= 0
3655 and RHS2 is known to be RHS2 >= 0. */
3656 tree utype
= unsigned_type_for (TREE_TYPE (rhs1
));
3658 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
3659 if ((ranges
[*idx
].strict_overflow_p
3660 || ranges
[i
].strict_overflow_p
)
3661 && issue_strict_overflow_warning (wc
))
3662 warning_at (gimple_location (stmt
), OPT_Wstrict_overflow
,
3663 "assuming signed overflow does not occur "
3664 "when simplifying range test");
3666 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3668 struct range_entry
*r
= &ranges
[*idx
];
3669 fprintf (dump_file
, "Optimizing range test ");
3670 print_generic_expr (dump_file
, r
->exp
);
3671 fprintf (dump_file
, " +[");
3672 print_generic_expr (dump_file
, r
->low
);
3673 fprintf (dump_file
, ", ");
3674 print_generic_expr (dump_file
, r
->high
);
3675 fprintf (dump_file
, "] and comparison ");
3676 print_generic_expr (dump_file
, rhs1
);
3677 fprintf (dump_file
, " %s ", op_symbol_code (ccode
));
3678 print_generic_expr (dump_file
, rhs2
);
3679 fprintf (dump_file
, "\n into (");
3680 print_generic_expr (dump_file
, utype
);
3681 fprintf (dump_file
, ") ");
3682 print_generic_expr (dump_file
, rhs1
);
3683 fprintf (dump_file
, " %s (", op_symbol_code (ccode
));
3684 print_generic_expr (dump_file
, utype
);
3685 fprintf (dump_file
, ") ");
3686 print_generic_expr (dump_file
, rhs2
);
3687 fprintf (dump_file
, "\n");
3690 operand_entry
*oe
= (*ops
)[ranges
[i
].idx
];
3692 if (opcode
== BIT_IOR_EXPR
3693 || (opcode
== ERROR_MARK
&& oe
->rank
== BIT_IOR_EXPR
))
3696 ccode
= invert_tree_comparison (ccode
, false);
3699 unsigned int uid
= gimple_uid (stmt
);
3700 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
3701 gimple
*g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, rhs1
);
3702 gimple_set_uid (g
, uid
);
3703 rhs1
= gimple_assign_lhs (g
);
3704 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3705 if (!useless_type_conversion_p (utype
, TREE_TYPE (rhs2
)))
3707 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, rhs2
);
3708 gimple_set_uid (g
, uid
);
3709 rhs2
= gimple_assign_lhs (g
);
3710 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3712 if (tree_swap_operands_p (rhs1
, rhs2
))
3714 std::swap (rhs1
, rhs2
);
3715 ccode
= swap_tree_comparison (ccode
);
3717 if (gimple_code (stmt
) == GIMPLE_COND
)
3719 gcond
*c
= as_a
<gcond
*> (stmt
);
3720 gimple_cond_set_code (c
, ccode
);
3721 gimple_cond_set_lhs (c
, rhs1
);
3722 gimple_cond_set_rhs (c
, rhs2
);
3727 tree ctype
= oe
->op
? TREE_TYPE (oe
->op
) : boolean_type_node
;
3728 if (!INTEGRAL_TYPE_P (ctype
)
3729 || (TREE_CODE (ctype
) != BOOLEAN_TYPE
3730 && TYPE_PRECISION (ctype
) != 1))
3731 ctype
= boolean_type_node
;
3732 g
= gimple_build_assign (make_ssa_name (ctype
), ccode
, rhs1
, rhs2
);
3733 gimple_set_uid (g
, uid
);
3734 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3735 if (oe
->op
&& ctype
!= TREE_TYPE (oe
->op
))
3737 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (oe
->op
)),
3738 NOP_EXPR
, gimple_assign_lhs (g
));
3739 gimple_set_uid (g
, uid
);
3740 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3742 ranges
[i
].exp
= gimple_assign_lhs (g
);
3743 oe
->op
= ranges
[i
].exp
;
3744 ranges
[i
].low
= build_zero_cst (TREE_TYPE (ranges
[i
].exp
));
3745 ranges
[i
].high
= ranges
[i
].low
;
3747 ranges
[i
].strict_overflow_p
= false;
3748 oe
= (*ops
)[ranges
[*idx
].idx
];
3749 /* Now change all the other range test immediate uses, so that
3750 those tests will be optimized away. */
3751 if (opcode
== ERROR_MARK
)
3754 oe
->op
= build_int_cst (TREE_TYPE (oe
->op
),
3755 oe
->rank
== BIT_IOR_EXPR
? 0 : 1);
3757 oe
->op
= (oe
->rank
== BIT_IOR_EXPR
3758 ? boolean_false_node
: boolean_true_node
);
3761 oe
->op
= error_mark_node
;
3762 ranges
[*idx
].exp
= NULL_TREE
;
3763 ranges
[*idx
].low
= NULL_TREE
;
3764 ranges
[*idx
].high
= NULL_TREE
;
3772 /* Optimize range tests, similarly how fold_range_test optimizes
3773 it on trees. The tree code for the binary
3774 operation between all the operands is OPCODE.
3775 If OPCODE is ERROR_MARK, optimize_range_tests is called from within
3776 maybe_optimize_range_tests for inter-bb range optimization.
3777 In that case if oe->op is NULL, oe->id is bb->index whose
3778 GIMPLE_COND is && or ||ed into the test, and oe->rank says
3780 FIRST_BB is the first basic block if OPCODE is ERROR_MARK. */
3783 optimize_range_tests (enum tree_code opcode
,
3784 vec
<operand_entry
*> *ops
, basic_block first_bb
)
3786 unsigned int length
= ops
->length (), i
, j
, first
;
3788 struct range_entry
*ranges
;
3789 bool any_changes
= false;
3794 ranges
= XNEWVEC (struct range_entry
, length
);
3795 for (i
= 0; i
< length
; i
++)
3799 init_range_entry (ranges
+ i
, oe
->op
,
3802 : last_stmt (BASIC_BLOCK_FOR_FN (cfun
, oe
->id
)));
3803 /* For | invert it now, we will invert it again before emitting
3804 the optimized expression. */
3805 if (opcode
== BIT_IOR_EXPR
3806 || (opcode
== ERROR_MARK
&& oe
->rank
== BIT_IOR_EXPR
))
3807 ranges
[i
].in_p
= !ranges
[i
].in_p
;
3810 qsort (ranges
, length
, sizeof (*ranges
), range_entry_cmp
);
3811 for (i
= 0; i
< length
; i
++)
3812 if (ranges
[i
].exp
!= NULL_TREE
&& TREE_CODE (ranges
[i
].exp
) == SSA_NAME
)
3815 /* Try to merge ranges. */
3816 for (first
= i
; i
< length
; i
++)
3818 tree low
= ranges
[i
].low
;
3819 tree high
= ranges
[i
].high
;
3820 int in_p
= ranges
[i
].in_p
;
3821 bool strict_overflow_p
= ranges
[i
].strict_overflow_p
;
3822 int update_fail_count
= 0;
3824 for (j
= i
+ 1; j
< length
; j
++)
3826 if (ranges
[i
].exp
!= ranges
[j
].exp
)
3828 if (!merge_ranges (&in_p
, &low
, &high
, in_p
, low
, high
,
3829 ranges
[j
].in_p
, ranges
[j
].low
, ranges
[j
].high
))
3831 strict_overflow_p
|= ranges
[j
].strict_overflow_p
;
3837 if (update_range_test (ranges
+ i
, ranges
+ i
+ 1, NULL
, j
- i
- 1,
3838 opcode
, ops
, ranges
[i
].exp
, NULL
, in_p
,
3839 low
, high
, strict_overflow_p
))
3844 /* Avoid quadratic complexity if all merge_ranges calls would succeed,
3845 while update_range_test would fail. */
3846 else if (update_fail_count
== 64)
3849 ++update_fail_count
;
3852 any_changes
|= optimize_range_tests_1 (opcode
, first
, length
, true,
3855 if (BRANCH_COST (optimize_function_for_speed_p (cfun
), false) >= 2)
3856 any_changes
|= optimize_range_tests_1 (opcode
, first
, length
, false,
3858 if (lshift_cheap_p (optimize_function_for_speed_p (cfun
)))
3859 any_changes
|= optimize_range_tests_to_bit_test (opcode
, first
, length
,
3861 any_changes
|= optimize_range_tests_cmp_bitwise (opcode
, first
, length
,
3863 any_changes
|= optimize_range_tests_var_bound (opcode
, first
, length
, ops
,
3866 if (any_changes
&& opcode
!= ERROR_MARK
)
3869 FOR_EACH_VEC_ELT (*ops
, i
, oe
)
3871 if (oe
->op
== error_mark_node
)
3880 XDELETEVEC (ranges
);
3884 /* A subroutine of optimize_vec_cond_expr to extract and canonicalize
3885 the operands of the VEC_COND_EXPR. Returns ERROR_MARK on failure,
3886 otherwise the comparison code. TYPE is a return value that is set
3887 to type of comparison. */
3890 ovce_extract_ops (tree var
, gassign
**rets
, bool *reti
, tree
*type
,
3891 tree
*lhs
, tree
*rhs
, gassign
**vcond
)
3893 if (TREE_CODE (var
) != SSA_NAME
)
3896 gassign
*stmt
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (var
));
3902 /* ??? If we start creating more COND_EXPR, we could perform
3903 this same optimization with them. For now, simplify. */
3904 if (gimple_assign_rhs_code (stmt
) != VEC_COND_EXPR
)
3907 tree cond
= gimple_assign_rhs1 (stmt
);
3908 tree_code cmp
= TREE_CODE (cond
);
3909 if (cmp
!= SSA_NAME
)
3912 gassign
*assign
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (cond
));
3914 || TREE_CODE_CLASS (gimple_assign_rhs_code (assign
)) != tcc_comparison
)
3917 cmp
= gimple_assign_rhs_code (assign
);
3919 *lhs
= gimple_assign_rhs1 (assign
);
3921 *rhs
= gimple_assign_rhs2 (assign
);
3923 /* ??? For now, allow only canonical true and false result vectors.
3924 We could expand this to other constants should the need arise,
3925 but at the moment we don't create them. */
3926 tree t
= gimple_assign_rhs2 (stmt
);
3927 tree f
= gimple_assign_rhs3 (stmt
);
3929 if (integer_all_onesp (t
))
3931 else if (integer_all_onesp (f
))
3933 cmp
= invert_tree_comparison (cmp
, false);
3938 if (!integer_zerop (f
))
3947 *type
= TREE_TYPE (cond
);
3951 /* Optimize the condition of VEC_COND_EXPRs which have been combined
3952 with OPCODE (either BIT_AND_EXPR or BIT_IOR_EXPR). */
3955 optimize_vec_cond_expr (tree_code opcode
, vec
<operand_entry
*> *ops
)
3957 unsigned int length
= ops
->length (), i
, j
;
3958 bool any_changes
= false;
3963 for (i
= 0; i
< length
; ++i
)
3965 tree elt0
= (*ops
)[i
]->op
;
3967 gassign
*stmt0
, *vcond0
;
3969 tree type
, lhs0
, rhs0
;
3970 tree_code cmp0
= ovce_extract_ops (elt0
, &stmt0
, &invert
, &type
, &lhs0
,
3972 if (cmp0
== ERROR_MARK
)
3975 for (j
= i
+ 1; j
< length
; ++j
)
3977 tree
&elt1
= (*ops
)[j
]->op
;
3979 gassign
*stmt1
, *vcond1
;
3981 tree_code cmp1
= ovce_extract_ops (elt1
, &stmt1
, NULL
, NULL
, &lhs1
,
3983 if (cmp1
== ERROR_MARK
)
3987 if (opcode
== BIT_AND_EXPR
)
3988 comb
= maybe_fold_and_comparisons (type
, cmp0
, lhs0
, rhs0
,
3990 else if (opcode
== BIT_IOR_EXPR
)
3991 comb
= maybe_fold_or_comparisons (type
, cmp0
, lhs0
, rhs0
,
3999 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4001 fprintf (dump_file
, "Transforming ");
4002 print_generic_expr (dump_file
, gimple_assign_lhs (stmt0
));
4003 fprintf (dump_file
, " %c ", opcode
== BIT_AND_EXPR
? '&' : '|');
4004 print_generic_expr (dump_file
, gimple_assign_lhs (stmt1
));
4005 fprintf (dump_file
, " into ");
4006 print_generic_expr (dump_file
, comb
);
4007 fputc ('\n', dump_file
);
4010 gimple_stmt_iterator gsi
= gsi_for_stmt (vcond0
);
4011 tree exp
= force_gimple_operand_gsi (&gsi
, comb
, true, NULL_TREE
,
4012 true, GSI_SAME_STMT
);
4014 swap_ssa_operands (vcond0
, gimple_assign_rhs2_ptr (vcond0
),
4015 gimple_assign_rhs3_ptr (vcond0
));
4016 gimple_assign_set_rhs1 (vcond0
, exp
);
4017 update_stmt (vcond0
);
4019 elt1
= error_mark_node
;
4028 FOR_EACH_VEC_ELT (*ops
, i
, oe
)
4030 if (oe
->op
== error_mark_node
)
4042 /* Return true if STMT is a cast like:
4048 # _345 = PHI <_123(N), 1(...), 1(...)>
4049 where _234 has bool type, _123 has single use and
4050 bb N has a single successor M. This is commonly used in
4051 the last block of a range test.
4053 Also Return true if STMT is tcc_compare like:
4059 # _345 = PHI <_234(N), 1(...), 1(...)>
4061 where _234 has booltype, single use and
4062 bb N has a single successor M. This is commonly used in
4063 the last block of a range test. */
4066 final_range_test_p (gimple
*stmt
)
4068 basic_block bb
, rhs_bb
, lhs_bb
;
4071 use_operand_p use_p
;
4074 if (!gimple_assign_cast_p (stmt
)
4075 && (!is_gimple_assign (stmt
)
4076 || (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
4077 != tcc_comparison
)))
4079 bb
= gimple_bb (stmt
);
4080 if (!single_succ_p (bb
))
4082 e
= single_succ_edge (bb
);
4083 if (e
->flags
& EDGE_COMPLEX
)
4086 lhs
= gimple_assign_lhs (stmt
);
4087 rhs
= gimple_assign_rhs1 (stmt
);
4088 if (gimple_assign_cast_p (stmt
)
4089 && (!INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4090 || TREE_CODE (rhs
) != SSA_NAME
4091 || TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
))
4094 if (!gimple_assign_cast_p (stmt
)
4095 && (TREE_CODE (TREE_TYPE (lhs
)) != BOOLEAN_TYPE
))
4098 /* Test whether lhs is consumed only by a PHI in the only successor bb. */
4099 if (!single_imm_use (lhs
, &use_p
, &use_stmt
))
4102 if (gimple_code (use_stmt
) != GIMPLE_PHI
4103 || gimple_bb (use_stmt
) != e
->dest
)
4106 /* And that the rhs is defined in the same loop. */
4107 if (gimple_assign_cast_p (stmt
))
4109 if (TREE_CODE (rhs
) != SSA_NAME
4110 || !(rhs_bb
= gimple_bb (SSA_NAME_DEF_STMT (rhs
)))
4111 || !flow_bb_inside_loop_p (loop_containing_stmt (stmt
), rhs_bb
))
4116 if (TREE_CODE (lhs
) != SSA_NAME
4117 || !(lhs_bb
= gimple_bb (SSA_NAME_DEF_STMT (lhs
)))
4118 || !flow_bb_inside_loop_p (loop_containing_stmt (stmt
), lhs_bb
))
4125 /* Return true if BB is suitable basic block for inter-bb range test
4126 optimization. If BACKWARD is true, BB should be the only predecessor
4127 of TEST_BB, and *OTHER_BB is either NULL and filled by the routine,
4128 or compared with to find a common basic block to which all conditions
4129 branch to if true resp. false. If BACKWARD is false, TEST_BB should
4130 be the only predecessor of BB. */
4133 suitable_cond_bb (basic_block bb
, basic_block test_bb
, basic_block
*other_bb
,
4136 edge_iterator ei
, ei2
;
4140 bool other_edge_seen
= false;
4145 /* Check last stmt first. */
4146 stmt
= last_stmt (bb
);
4148 || (gimple_code (stmt
) != GIMPLE_COND
4149 && (backward
|| !final_range_test_p (stmt
)))
4150 || gimple_visited_p (stmt
)
4151 || stmt_could_throw_p (cfun
, stmt
)
4154 is_cond
= gimple_code (stmt
) == GIMPLE_COND
;
4157 /* If last stmt is GIMPLE_COND, verify that one of the succ edges
4158 goes to the next bb (if BACKWARD, it is TEST_BB), and the other
4159 to *OTHER_BB (if not set yet, try to find it out). */
4160 if (EDGE_COUNT (bb
->succs
) != 2)
4162 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4164 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
4166 if (e
->dest
== test_bb
)
4175 if (*other_bb
== NULL
)
4177 FOR_EACH_EDGE (e2
, ei2
, test_bb
->succs
)
4178 if (!(e2
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
4180 else if (e
->dest
== e2
->dest
)
4181 *other_bb
= e
->dest
;
4182 if (*other_bb
== NULL
)
4185 if (e
->dest
== *other_bb
)
4186 other_edge_seen
= true;
4190 if (*other_bb
== NULL
|| !other_edge_seen
)
4193 else if (single_succ (bb
) != *other_bb
)
4196 /* Now check all PHIs of *OTHER_BB. */
4197 e
= find_edge (bb
, *other_bb
);
4198 e2
= find_edge (test_bb
, *other_bb
);
4199 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4201 gphi
*phi
= gsi
.phi ();
4202 /* If both BB and TEST_BB end with GIMPLE_COND, all PHI arguments
4203 corresponding to BB and TEST_BB predecessor must be the same. */
4204 if (!operand_equal_p (gimple_phi_arg_def (phi
, e
->dest_idx
),
4205 gimple_phi_arg_def (phi
, e2
->dest_idx
), 0))
4207 /* Otherwise, if one of the blocks doesn't end with GIMPLE_COND,
4208 one of the PHIs should have the lhs of the last stmt in
4209 that block as PHI arg and that PHI should have 0 or 1
4210 corresponding to it in all other range test basic blocks
4214 if (gimple_phi_arg_def (phi
, e
->dest_idx
)
4215 == gimple_assign_lhs (stmt
)
4216 && (integer_zerop (gimple_phi_arg_def (phi
, e2
->dest_idx
))
4217 || integer_onep (gimple_phi_arg_def (phi
,
4223 gimple
*test_last
= last_stmt (test_bb
);
4224 if (gimple_code (test_last
) != GIMPLE_COND
4225 && gimple_phi_arg_def (phi
, e2
->dest_idx
)
4226 == gimple_assign_lhs (test_last
)
4227 && (integer_zerop (gimple_phi_arg_def (phi
, e
->dest_idx
))
4228 || integer_onep (gimple_phi_arg_def (phi
, e
->dest_idx
))))
4238 /* Return true if BB doesn't have side-effects that would disallow
4239 range test optimization, all SSA_NAMEs set in the bb are consumed
4240 in the bb and there are no PHIs. */
4243 no_side_effect_bb (basic_block bb
)
4245 gimple_stmt_iterator gsi
;
4248 if (!gimple_seq_empty_p (phi_nodes (bb
)))
4250 last
= last_stmt (bb
);
4251 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4253 gimple
*stmt
= gsi_stmt (gsi
);
4255 imm_use_iterator imm_iter
;
4256 use_operand_p use_p
;
4258 if (is_gimple_debug (stmt
))
4260 if (gimple_has_side_effects (stmt
))
4264 if (!is_gimple_assign (stmt
))
4266 lhs
= gimple_assign_lhs (stmt
);
4267 if (TREE_CODE (lhs
) != SSA_NAME
)
4269 if (gimple_assign_rhs_could_trap_p (stmt
))
4271 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
4273 gimple
*use_stmt
= USE_STMT (use_p
);
4274 if (is_gimple_debug (use_stmt
))
4276 if (gimple_bb (use_stmt
) != bb
)
4283 /* If VAR is set by CODE (BIT_{AND,IOR}_EXPR) which is reassociable,
4284 return true and fill in *OPS recursively. */
4287 get_ops (tree var
, enum tree_code code
, vec
<operand_entry
*> *ops
,
4290 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
4294 if (!is_reassociable_op (stmt
, code
, loop
))
4297 rhs
[0] = gimple_assign_rhs1 (stmt
);
4298 rhs
[1] = gimple_assign_rhs2 (stmt
);
4299 gimple_set_visited (stmt
, true);
4300 for (i
= 0; i
< 2; i
++)
4301 if (TREE_CODE (rhs
[i
]) == SSA_NAME
4302 && !get_ops (rhs
[i
], code
, ops
, loop
)
4303 && has_single_use (rhs
[i
]))
4305 operand_entry
*oe
= operand_entry_pool
.allocate ();
4311 oe
->stmt_to_insert
= NULL
;
4312 ops
->safe_push (oe
);
4317 /* Find the ops that were added by get_ops starting from VAR, see if
4318 they were changed during update_range_test and if yes, create new
4322 update_ops (tree var
, enum tree_code code
, vec
<operand_entry
*> ops
,
4323 unsigned int *pidx
, class loop
*loop
)
4325 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
4329 if (!is_reassociable_op (stmt
, code
, loop
))
4332 rhs
[0] = gimple_assign_rhs1 (stmt
);
4333 rhs
[1] = gimple_assign_rhs2 (stmt
);
4336 for (i
= 0; i
< 2; i
++)
4337 if (TREE_CODE (rhs
[i
]) == SSA_NAME
)
4339 rhs
[2 + i
] = update_ops (rhs
[i
], code
, ops
, pidx
, loop
);
4340 if (rhs
[2 + i
] == NULL_TREE
)
4342 if (has_single_use (rhs
[i
]))
4343 rhs
[2 + i
] = ops
[(*pidx
)++]->op
;
4345 rhs
[2 + i
] = rhs
[i
];
4348 if ((rhs
[2] != rhs
[0] || rhs
[3] != rhs
[1])
4349 && (rhs
[2] != rhs
[1] || rhs
[3] != rhs
[0]))
4351 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
4352 var
= make_ssa_name (TREE_TYPE (var
));
4353 gassign
*g
= gimple_build_assign (var
, gimple_assign_rhs_code (stmt
),
4355 gimple_set_uid (g
, gimple_uid (stmt
));
4356 gimple_set_visited (g
, true);
4357 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
4362 /* Structure to track the initial value passed to get_ops and
4363 the range in the ops vector for each basic block. */
4365 struct inter_bb_range_test_entry
4368 unsigned int first_idx
, last_idx
;
4371 /* Inter-bb range test optimization.
4373 Returns TRUE if a gimple conditional is optimized to a true/false,
4374 otherwise return FALSE.
4376 This indicates to the caller that it should run a CFG cleanup pass
4377 once reassociation is completed. */
4380 maybe_optimize_range_tests (gimple
*stmt
)
4382 basic_block first_bb
= gimple_bb (stmt
);
4383 basic_block last_bb
= first_bb
;
4384 basic_block other_bb
= NULL
;
4388 auto_vec
<operand_entry
*> ops
;
4389 auto_vec
<inter_bb_range_test_entry
> bbinfo
;
4390 bool any_changes
= false;
4391 bool cfg_cleanup_needed
= false;
4393 /* Consider only basic blocks that end with GIMPLE_COND or
4394 a cast statement satisfying final_range_test_p. All
4395 but the last bb in the first_bb .. last_bb range
4396 should end with GIMPLE_COND. */
4397 if (gimple_code (stmt
) == GIMPLE_COND
)
4399 if (EDGE_COUNT (first_bb
->succs
) != 2)
4400 return cfg_cleanup_needed
;
4402 else if (final_range_test_p (stmt
))
4403 other_bb
= single_succ (first_bb
);
4405 return cfg_cleanup_needed
;
4407 if (stmt_could_throw_p (cfun
, stmt
))
4408 return cfg_cleanup_needed
;
4410 /* As relative ordering of post-dominator sons isn't fixed,
4411 maybe_optimize_range_tests can be called first on any
4412 bb in the range we want to optimize. So, start searching
4413 backwards, if first_bb can be set to a predecessor. */
4414 while (single_pred_p (first_bb
))
4416 basic_block pred_bb
= single_pred (first_bb
);
4417 if (!suitable_cond_bb (pred_bb
, first_bb
, &other_bb
, true))
4419 if (!no_side_effect_bb (first_bb
))
4423 /* If first_bb is last_bb, other_bb hasn't been computed yet.
4424 Before starting forward search in last_bb successors, find
4425 out the other_bb. */
4426 if (first_bb
== last_bb
)
4429 /* As non-GIMPLE_COND last stmt always terminates the range,
4430 if forward search didn't discover anything, just give up. */
4431 if (gimple_code (stmt
) != GIMPLE_COND
)
4432 return cfg_cleanup_needed
;
4433 /* Look at both successors. Either it ends with a GIMPLE_COND
4434 and satisfies suitable_cond_bb, or ends with a cast and
4435 other_bb is that cast's successor. */
4436 FOR_EACH_EDGE (e
, ei
, first_bb
->succs
)
4437 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
))
4438 || e
->dest
== first_bb
)
4439 return cfg_cleanup_needed
;
4440 else if (single_pred_p (e
->dest
))
4442 stmt
= last_stmt (e
->dest
);
4444 && gimple_code (stmt
) == GIMPLE_COND
4445 && EDGE_COUNT (e
->dest
->succs
) == 2)
4447 if (suitable_cond_bb (first_bb
, e
->dest
, &other_bb
, true))
4453 && final_range_test_p (stmt
)
4454 && find_edge (first_bb
, single_succ (e
->dest
)))
4456 other_bb
= single_succ (e
->dest
);
4457 if (other_bb
== first_bb
)
4461 if (other_bb
== NULL
)
4462 return cfg_cleanup_needed
;
4464 /* Now do the forward search, moving last_bb to successor bbs
4465 that aren't other_bb. */
4466 while (EDGE_COUNT (last_bb
->succs
) == 2)
4468 FOR_EACH_EDGE (e
, ei
, last_bb
->succs
)
4469 if (e
->dest
!= other_bb
)
4473 if (!single_pred_p (e
->dest
))
4475 if (!suitable_cond_bb (e
->dest
, last_bb
, &other_bb
, false))
4477 if (!no_side_effect_bb (e
->dest
))
4481 if (first_bb
== last_bb
)
4482 return cfg_cleanup_needed
;
4483 /* Here basic blocks first_bb through last_bb's predecessor
4484 end with GIMPLE_COND, all of them have one of the edges to
4485 other_bb and another to another block in the range,
4486 all blocks except first_bb don't have side-effects and
4487 last_bb ends with either GIMPLE_COND, or cast satisfying
4488 final_range_test_p. */
4489 for (bb
= last_bb
; ; bb
= single_pred (bb
))
4491 enum tree_code code
;
4493 inter_bb_range_test_entry bb_ent
;
4495 bb_ent
.op
= NULL_TREE
;
4496 bb_ent
.first_idx
= ops
.length ();
4497 bb_ent
.last_idx
= bb_ent
.first_idx
;
4498 e
= find_edge (bb
, other_bb
);
4499 stmt
= last_stmt (bb
);
4500 gimple_set_visited (stmt
, true);
4501 if (gimple_code (stmt
) != GIMPLE_COND
)
4503 use_operand_p use_p
;
4508 lhs
= gimple_assign_lhs (stmt
);
4509 rhs
= gimple_assign_rhs1 (stmt
);
4510 gcc_assert (bb
== last_bb
);
4519 # _345 = PHI <_123(N), 1(...), 1(...)>
4521 or 0 instead of 1. If it is 0, the _234
4522 range test is anded together with all the
4523 other range tests, if it is 1, it is ored with
4525 single_imm_use (lhs
, &use_p
, &phi
);
4526 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
4527 e2
= find_edge (first_bb
, other_bb
);
4529 gcc_assert (gimple_phi_arg_def (phi
, e
->dest_idx
) == lhs
);
4530 if (integer_zerop (gimple_phi_arg_def (phi
, d
)))
4531 code
= BIT_AND_EXPR
;
4534 gcc_checking_assert (integer_onep (gimple_phi_arg_def (phi
, d
)));
4535 code
= BIT_IOR_EXPR
;
4538 /* If _234 SSA_NAME_DEF_STMT is
4540 (or &, corresponding to 1/0 in the phi arguments,
4541 push into ops the individual range test arguments
4542 of the bitwise or resp. and, recursively. */
4543 if (TREE_CODE (rhs
) == SSA_NAME
4544 && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
4546 && !get_ops (rhs
, code
, &ops
,
4547 loop_containing_stmt (stmt
))
4548 && has_single_use (rhs
))
4550 /* Otherwise, push the _234 range test itself. */
4551 operand_entry
*oe
= operand_entry_pool
.allocate ();
4557 oe
->stmt_to_insert
= NULL
;
4562 else if (is_gimple_assign (stmt
)
4563 && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
4565 && !get_ops (lhs
, code
, &ops
,
4566 loop_containing_stmt (stmt
))
4567 && has_single_use (lhs
))
4569 operand_entry
*oe
= operand_entry_pool
.allocate ();
4580 bb_ent
.last_idx
= ops
.length ();
4583 bbinfo
.safe_push (bb_ent
);
4586 /* Otherwise stmt is GIMPLE_COND. */
4587 code
= gimple_cond_code (stmt
);
4588 lhs
= gimple_cond_lhs (stmt
);
4589 rhs
= gimple_cond_rhs (stmt
);
4590 if (TREE_CODE (lhs
) == SSA_NAME
4591 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4592 && ((code
!= EQ_EXPR
&& code
!= NE_EXPR
)
4593 || rhs
!= boolean_false_node
4594 /* Either push into ops the individual bitwise
4595 or resp. and operands, depending on which
4596 edge is other_bb. */
4597 || !get_ops (lhs
, (((e
->flags
& EDGE_TRUE_VALUE
) == 0)
4598 ^ (code
== EQ_EXPR
))
4599 ? BIT_AND_EXPR
: BIT_IOR_EXPR
, &ops
,
4600 loop_containing_stmt (stmt
))))
4602 /* Or push the GIMPLE_COND stmt itself. */
4603 operand_entry
*oe
= operand_entry_pool
.allocate ();
4606 oe
->rank
= (e
->flags
& EDGE_TRUE_VALUE
)
4607 ? BIT_IOR_EXPR
: BIT_AND_EXPR
;
4608 /* oe->op = NULL signs that there is no SSA_NAME
4609 for the range test, and oe->id instead is the
4610 basic block number, at which's end the GIMPLE_COND
4614 oe
->stmt_to_insert
= NULL
;
4619 else if (ops
.length () > bb_ent
.first_idx
)
4622 bb_ent
.last_idx
= ops
.length ();
4624 bbinfo
.safe_push (bb_ent
);
4628 if (ops
.length () > 1)
4629 any_changes
= optimize_range_tests (ERROR_MARK
, &ops
, first_bb
);
4632 unsigned int idx
, max_idx
= 0;
4633 /* update_ops relies on has_single_use predicates returning the
4634 same values as it did during get_ops earlier. Additionally it
4635 never removes statements, only adds new ones and it should walk
4636 from the single imm use and check the predicate already before
4637 making those changes.
4638 On the other side, the handling of GIMPLE_COND directly can turn
4639 previously multiply used SSA_NAMEs into single use SSA_NAMEs, so
4640 it needs to be done in a separate loop afterwards. */
4641 for (bb
= last_bb
, idx
= 0; ; bb
= single_pred (bb
), idx
++)
4643 if (bbinfo
[idx
].first_idx
< bbinfo
[idx
].last_idx
4644 && bbinfo
[idx
].op
!= NULL_TREE
)
4649 stmt
= last_stmt (bb
);
4650 new_op
= update_ops (bbinfo
[idx
].op
,
4652 ops
[bbinfo
[idx
].first_idx
]->rank
,
4653 ops
, &bbinfo
[idx
].first_idx
,
4654 loop_containing_stmt (stmt
));
4655 if (new_op
== NULL_TREE
)
4657 gcc_assert (bb
== last_bb
);
4658 new_op
= ops
[bbinfo
[idx
].first_idx
++]->op
;
4660 if (bbinfo
[idx
].op
!= new_op
)
4662 imm_use_iterator iter
;
4663 use_operand_p use_p
;
4664 gimple
*use_stmt
, *cast_or_tcc_cmp_stmt
= NULL
;
4666 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, bbinfo
[idx
].op
)
4667 if (is_gimple_debug (use_stmt
))
4669 else if (gimple_code (use_stmt
) == GIMPLE_COND
4670 || gimple_code (use_stmt
) == GIMPLE_PHI
)
4671 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4672 SET_USE (use_p
, new_op
);
4673 else if ((is_gimple_assign (use_stmt
)
4675 (gimple_assign_rhs_code (use_stmt
))
4676 == tcc_comparison
)))
4677 cast_or_tcc_cmp_stmt
= use_stmt
;
4678 else if (gimple_assign_cast_p (use_stmt
))
4679 cast_or_tcc_cmp_stmt
= use_stmt
;
4683 if (cast_or_tcc_cmp_stmt
)
4685 gcc_assert (bb
== last_bb
);
4686 tree lhs
= gimple_assign_lhs (cast_or_tcc_cmp_stmt
);
4687 tree new_lhs
= make_ssa_name (TREE_TYPE (lhs
));
4688 enum tree_code rhs_code
4689 = gimple_assign_cast_p (cast_or_tcc_cmp_stmt
)
4690 ? gimple_assign_rhs_code (cast_or_tcc_cmp_stmt
)
4693 if (is_gimple_min_invariant (new_op
))
4695 new_op
= fold_convert (TREE_TYPE (lhs
), new_op
);
4696 g
= gimple_build_assign (new_lhs
, new_op
);
4699 g
= gimple_build_assign (new_lhs
, rhs_code
, new_op
);
4700 gimple_stmt_iterator gsi
4701 = gsi_for_stmt (cast_or_tcc_cmp_stmt
);
4702 gimple_set_uid (g
, gimple_uid (cast_or_tcc_cmp_stmt
));
4703 gimple_set_visited (g
, true);
4704 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
4705 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4706 if (is_gimple_debug (use_stmt
))
4708 else if (gimple_code (use_stmt
) == GIMPLE_COND
4709 || gimple_code (use_stmt
) == GIMPLE_PHI
)
4710 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4711 SET_USE (use_p
, new_lhs
);
4720 for (bb
= last_bb
, idx
= 0; ; bb
= single_pred (bb
), idx
++)
4722 if (bbinfo
[idx
].first_idx
< bbinfo
[idx
].last_idx
4723 && bbinfo
[idx
].op
== NULL_TREE
4724 && ops
[bbinfo
[idx
].first_idx
]->op
!= NULL_TREE
)
4726 gcond
*cond_stmt
= as_a
<gcond
*> (last_stmt (bb
));
4731 /* If we collapse the conditional to a true/false
4732 condition, then bubble that knowledge up to our caller. */
4733 if (integer_zerop (ops
[bbinfo
[idx
].first_idx
]->op
))
4735 gimple_cond_make_false (cond_stmt
);
4736 cfg_cleanup_needed
= true;
4738 else if (integer_onep (ops
[bbinfo
[idx
].first_idx
]->op
))
4740 gimple_cond_make_true (cond_stmt
);
4741 cfg_cleanup_needed
= true;
4745 gimple_cond_set_code (cond_stmt
, NE_EXPR
);
4746 gimple_cond_set_lhs (cond_stmt
,
4747 ops
[bbinfo
[idx
].first_idx
]->op
);
4748 gimple_cond_set_rhs (cond_stmt
, boolean_false_node
);
4750 update_stmt (cond_stmt
);
4756 /* The above changes could result in basic blocks after the first
4757 modified one, up to and including last_bb, to be executed even if
4758 they would not be in the original program. If the value ranges of
4759 assignment lhs' in those bbs were dependent on the conditions
4760 guarding those basic blocks which now can change, the VRs might
4761 be incorrect. As no_side_effect_bb should ensure those SSA_NAMEs
4762 are only used within the same bb, it should be not a big deal if
4763 we just reset all the VRs in those bbs. See PR68671. */
4764 for (bb
= last_bb
, idx
= 0; idx
< max_idx
; bb
= single_pred (bb
), idx
++)
4765 reset_flow_sensitive_info_in_bb (bb
);
4767 return cfg_cleanup_needed
;
4770 /* Return true if OPERAND is defined by a PHI node which uses the LHS
4771 of STMT in it's operands. This is also known as a "destructive
4772 update" operation. */
4775 is_phi_for_stmt (gimple
*stmt
, tree operand
)
4780 use_operand_p arg_p
;
4783 if (TREE_CODE (operand
) != SSA_NAME
)
4786 lhs
= gimple_assign_lhs (stmt
);
4788 def_stmt
= SSA_NAME_DEF_STMT (operand
);
4789 def_phi
= dyn_cast
<gphi
*> (def_stmt
);
4793 FOR_EACH_PHI_ARG (arg_p
, def_phi
, i
, SSA_OP_USE
)
4794 if (lhs
== USE_FROM_PTR (arg_p
))
4799 /* Remove def stmt of VAR if VAR has zero uses and recurse
4800 on rhs1 operand if so. */
4803 remove_visited_stmt_chain (tree var
)
4806 gimple_stmt_iterator gsi
;
4810 if (TREE_CODE (var
) != SSA_NAME
|| !has_zero_uses (var
))
4812 stmt
= SSA_NAME_DEF_STMT (var
);
4813 if (is_gimple_assign (stmt
) && gimple_visited_p (stmt
))
4815 var
= gimple_assign_rhs1 (stmt
);
4816 gsi
= gsi_for_stmt (stmt
);
4817 reassoc_remove_stmt (&gsi
);
4818 release_defs (stmt
);
4825 /* This function checks three consequtive operands in
4826 passed operands vector OPS starting from OPINDEX and
4827 swaps two operands if it is profitable for binary operation
4828 consuming OPINDEX + 1 abnd OPINDEX + 2 operands.
4830 We pair ops with the same rank if possible.
4832 The alternative we try is to see if STMT is a destructive
4833 update style statement, which is like:
4836 In that case, we want to use the destructive update form to
4837 expose the possible vectorizer sum reduction opportunity.
4838 In that case, the third operand will be the phi node. This
4839 check is not performed if STMT is null.
4841 We could, of course, try to be better as noted above, and do a
4842 lot of work to try to find these opportunities in >3 operand
4843 cases, but it is unlikely to be worth it. */
4846 swap_ops_for_binary_stmt (vec
<operand_entry
*> ops
,
4847 unsigned int opindex
, gimple
*stmt
)
4849 operand_entry
*oe1
, *oe2
, *oe3
;
4852 oe2
= ops
[opindex
+ 1];
4853 oe3
= ops
[opindex
+ 2];
4855 if ((oe1
->rank
== oe2
->rank
4856 && oe2
->rank
!= oe3
->rank
)
4857 || (stmt
&& is_phi_for_stmt (stmt
, oe3
->op
)
4858 && !is_phi_for_stmt (stmt
, oe1
->op
)
4859 && !is_phi_for_stmt (stmt
, oe2
->op
)))
4860 std::swap (*oe1
, *oe3
);
4861 else if ((oe1
->rank
== oe3
->rank
4862 && oe2
->rank
!= oe3
->rank
)
4863 || (stmt
&& is_phi_for_stmt (stmt
, oe2
->op
)
4864 && !is_phi_for_stmt (stmt
, oe1
->op
)
4865 && !is_phi_for_stmt (stmt
, oe3
->op
)))
4866 std::swap (*oe1
, *oe2
);
4869 /* If definition of RHS1 or RHS2 dominates STMT, return the later of those
4870 two definitions, otherwise return STMT. */
4872 static inline gimple
*
4873 find_insert_point (gimple
*stmt
, tree rhs1
, tree rhs2
)
4875 if (TREE_CODE (rhs1
) == SSA_NAME
4876 && reassoc_stmt_dominates_stmt_p (stmt
, SSA_NAME_DEF_STMT (rhs1
)))
4877 stmt
= SSA_NAME_DEF_STMT (rhs1
);
4878 if (TREE_CODE (rhs2
) == SSA_NAME
4879 && reassoc_stmt_dominates_stmt_p (stmt
, SSA_NAME_DEF_STMT (rhs2
)))
4880 stmt
= SSA_NAME_DEF_STMT (rhs2
);
4884 /* If the stmt that defines operand has to be inserted, insert it
4887 insert_stmt_before_use (gimple
*stmt
, gimple
*stmt_to_insert
)
4889 gcc_assert (is_gimple_assign (stmt_to_insert
));
4890 tree rhs1
= gimple_assign_rhs1 (stmt_to_insert
);
4891 tree rhs2
= gimple_assign_rhs2 (stmt_to_insert
);
4892 gimple
*insert_point
= find_insert_point (stmt
, rhs1
, rhs2
);
4893 gimple_stmt_iterator gsi
= gsi_for_stmt (insert_point
);
4894 gimple_set_uid (stmt_to_insert
, gimple_uid (insert_point
));
4896 /* If the insert point is not stmt, then insert_point would be
4897 the point where operand rhs1 or rhs2 is defined. In this case,
4898 stmt_to_insert has to be inserted afterwards. This would
4899 only happen when the stmt insertion point is flexible. */
4900 if (stmt
== insert_point
)
4901 gsi_insert_before (&gsi
, stmt_to_insert
, GSI_NEW_STMT
);
4903 insert_stmt_after (stmt_to_insert
, insert_point
);
4907 /* Recursively rewrite our linearized statements so that the operators
4908 match those in OPS[OPINDEX], putting the computation in rank
4909 order. Return new lhs.
4910 CHANGED is true if we shouldn't reuse the lhs SSA_NAME both in
4911 the current stmt and during recursive invocations.
4912 NEXT_CHANGED is true if we shouldn't reuse the lhs SSA_NAME in
4913 recursive invocations. */
4916 rewrite_expr_tree (gimple
*stmt
, unsigned int opindex
,
4917 vec
<operand_entry
*> ops
, bool changed
, bool next_changed
)
4919 tree rhs1
= gimple_assign_rhs1 (stmt
);
4920 tree rhs2
= gimple_assign_rhs2 (stmt
);
4921 tree lhs
= gimple_assign_lhs (stmt
);
4924 /* The final recursion case for this function is that you have
4925 exactly two operations left.
4926 If we had exactly one op in the entire list to start with, we
4927 would have never called this function, and the tail recursion
4928 rewrites them one at a time. */
4929 if (opindex
+ 2 == ops
.length ())
4931 operand_entry
*oe1
, *oe2
;
4934 oe2
= ops
[opindex
+ 1];
4936 if (rhs1
!= oe1
->op
|| rhs2
!= oe2
->op
)
4938 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
4939 unsigned int uid
= gimple_uid (stmt
);
4941 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4943 fprintf (dump_file
, "Transforming ");
4944 print_gimple_stmt (dump_file
, stmt
, 0);
4947 /* If the stmt that defines operand has to be inserted, insert it
4949 if (oe1
->stmt_to_insert
)
4950 insert_stmt_before_use (stmt
, oe1
->stmt_to_insert
);
4951 if (oe2
->stmt_to_insert
)
4952 insert_stmt_before_use (stmt
, oe2
->stmt_to_insert
);
4953 /* Even when changed is false, reassociation could have e.g. removed
4954 some redundant operations, so unless we are just swapping the
4955 arguments or unless there is no change at all (then we just
4956 return lhs), force creation of a new SSA_NAME. */
4957 if (changed
|| ((rhs1
!= oe2
->op
|| rhs2
!= oe1
->op
) && opindex
))
4959 gimple
*insert_point
4960 = find_insert_point (stmt
, oe1
->op
, oe2
->op
);
4961 lhs
= make_ssa_name (TREE_TYPE (lhs
));
4963 = gimple_build_assign (lhs
, gimple_assign_rhs_code (stmt
),
4965 gimple_set_uid (stmt
, uid
);
4966 gimple_set_visited (stmt
, true);
4967 if (insert_point
== gsi_stmt (gsi
))
4968 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
4970 insert_stmt_after (stmt
, insert_point
);
4974 gcc_checking_assert (find_insert_point (stmt
, oe1
->op
, oe2
->op
)
4976 gimple_assign_set_rhs1 (stmt
, oe1
->op
);
4977 gimple_assign_set_rhs2 (stmt
, oe2
->op
);
4981 if (rhs1
!= oe1
->op
&& rhs1
!= oe2
->op
)
4982 remove_visited_stmt_chain (rhs1
);
4984 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4986 fprintf (dump_file
, " into ");
4987 print_gimple_stmt (dump_file
, stmt
, 0);
4993 /* If we hit here, we should have 3 or more ops left. */
4994 gcc_assert (opindex
+ 2 < ops
.length ());
4996 /* Rewrite the next operator. */
4999 /* If the stmt that defines operand has to be inserted, insert it
5001 if (oe
->stmt_to_insert
)
5002 insert_stmt_before_use (stmt
, oe
->stmt_to_insert
);
5004 /* Recurse on the LHS of the binary operator, which is guaranteed to
5005 be the non-leaf side. */
5007 = rewrite_expr_tree (SSA_NAME_DEF_STMT (rhs1
), opindex
+ 1, ops
,
5008 changed
|| oe
->op
!= rhs2
|| next_changed
,
5011 if (oe
->op
!= rhs2
|| new_rhs1
!= rhs1
)
5013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5015 fprintf (dump_file
, "Transforming ");
5016 print_gimple_stmt (dump_file
, stmt
, 0);
5019 /* If changed is false, this is either opindex == 0
5020 or all outer rhs2's were equal to corresponding oe->op,
5021 and powi_result is NULL.
5022 That means lhs is equivalent before and after reassociation.
5023 Otherwise ensure the old lhs SSA_NAME is not reused and
5024 create a new stmt as well, so that any debug stmts will be
5025 properly adjusted. */
5028 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
5029 unsigned int uid
= gimple_uid (stmt
);
5030 gimple
*insert_point
= find_insert_point (stmt
, new_rhs1
, oe
->op
);
5032 lhs
= make_ssa_name (TREE_TYPE (lhs
));
5033 stmt
= gimple_build_assign (lhs
, gimple_assign_rhs_code (stmt
),
5035 gimple_set_uid (stmt
, uid
);
5036 gimple_set_visited (stmt
, true);
5037 if (insert_point
== gsi_stmt (gsi
))
5038 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
5040 insert_stmt_after (stmt
, insert_point
);
5044 gcc_checking_assert (find_insert_point (stmt
, new_rhs1
, oe
->op
)
5046 gimple_assign_set_rhs1 (stmt
, new_rhs1
);
5047 gimple_assign_set_rhs2 (stmt
, oe
->op
);
5051 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5053 fprintf (dump_file
, " into ");
5054 print_gimple_stmt (dump_file
, stmt
, 0);
5060 /* Find out how many cycles we need to compute statements chain.
5061 OPS_NUM holds number os statements in a chain. CPU_WIDTH is a
5062 maximum number of independent statements we may execute per cycle. */
5065 get_required_cycles (int ops_num
, int cpu_width
)
5071 /* While we have more than 2 * cpu_width operands
5072 we may reduce number of operands by cpu_width
5074 res
= ops_num
/ (2 * cpu_width
);
5076 /* Remained operands count may be reduced twice per cycle
5077 until we have only one operand. */
5078 rest
= (unsigned)(ops_num
- res
* cpu_width
);
5079 elog
= exact_log2 (rest
);
5083 res
+= floor_log2 (rest
) + 1;
5088 /* Returns an optimal number of registers to use for computation of
5089 given statements. */
5092 get_reassociation_width (int ops_num
, enum tree_code opc
,
5095 int param_width
= param_tree_reassoc_width
;
5100 if (param_width
> 0)
5101 width
= param_width
;
5103 width
= targetm
.sched
.reassociation_width (opc
, mode
);
5108 /* Get the minimal time required for sequence computation. */
5109 cycles_best
= get_required_cycles (ops_num
, width
);
5111 /* Check if we may use less width and still compute sequence for
5112 the same time. It will allow us to reduce registers usage.
5113 get_required_cycles is monotonically increasing with lower width
5114 so we can perform a binary search for the minimal width that still
5115 results in the optimal cycle count. */
5117 while (width
> width_min
)
5119 int width_mid
= (width
+ width_min
) / 2;
5121 if (get_required_cycles (ops_num
, width_mid
) == cycles_best
)
5123 else if (width_min
< width_mid
)
5124 width_min
= width_mid
;
5132 /* Recursively rewrite our linearized statements so that the operators
5133 match those in OPS[OPINDEX], putting the computation in rank
5134 order and trying to allow operations to be executed in
5138 rewrite_expr_tree_parallel (gassign
*stmt
, int width
,
5139 vec
<operand_entry
*> ops
)
5141 enum tree_code opcode
= gimple_assign_rhs_code (stmt
);
5142 int op_num
= ops
.length ();
5143 gcc_assert (op_num
> 0);
5144 int stmt_num
= op_num
- 1;
5145 gimple
**stmts
= XALLOCAVEC (gimple
*, stmt_num
);
5146 int op_index
= op_num
- 1;
5148 int ready_stmts_end
= 0;
5150 gimple
*stmt1
= NULL
, *stmt2
= NULL
;
5151 tree last_rhs1
= gimple_assign_rhs1 (stmt
);
5153 /* We start expression rewriting from the top statements.
5154 So, in this loop we create a full list of statements
5155 we will work with. */
5156 stmts
[stmt_num
- 1] = stmt
;
5157 for (i
= stmt_num
- 2; i
>= 0; i
--)
5158 stmts
[i
] = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmts
[i
+1]));
5160 for (i
= 0; i
< stmt_num
; i
++)
5164 /* Determine whether we should use results of
5165 already handled statements or not. */
5166 if (ready_stmts_end
== 0
5167 && (i
- stmt_index
>= width
|| op_index
< 1))
5168 ready_stmts_end
= i
;
5170 /* Now we choose operands for the next statement. Non zero
5171 value in ready_stmts_end means here that we should use
5172 the result of already generated statements as new operand. */
5173 if (ready_stmts_end
> 0)
5175 op1
= gimple_assign_lhs (stmts
[stmt_index
++]);
5176 if (ready_stmts_end
> stmt_index
)
5177 op2
= gimple_assign_lhs (stmts
[stmt_index
++]);
5178 else if (op_index
>= 0)
5180 operand_entry
*oe
= ops
[op_index
--];
5181 stmt2
= oe
->stmt_to_insert
;
5186 gcc_assert (stmt_index
< i
);
5187 op2
= gimple_assign_lhs (stmts
[stmt_index
++]);
5190 if (stmt_index
>= ready_stmts_end
)
5191 ready_stmts_end
= 0;
5196 swap_ops_for_binary_stmt (ops
, op_index
- 2, NULL
);
5197 operand_entry
*oe2
= ops
[op_index
--];
5198 operand_entry
*oe1
= ops
[op_index
--];
5200 stmt2
= oe2
->stmt_to_insert
;
5202 stmt1
= oe1
->stmt_to_insert
;
5205 /* If we emit the last statement then we should put
5206 operands into the last statement. It will also
5208 if (op_index
< 0 && stmt_index
== i
)
5211 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5213 fprintf (dump_file
, "Transforming ");
5214 print_gimple_stmt (dump_file
, stmts
[i
], 0);
5217 /* If the stmt that defines operand has to be inserted, insert it
5220 insert_stmt_before_use (stmts
[i
], stmt1
);
5222 insert_stmt_before_use (stmts
[i
], stmt2
);
5223 stmt1
= stmt2
= NULL
;
5225 /* We keep original statement only for the last one. All
5226 others are recreated. */
5227 if (i
== stmt_num
- 1)
5229 gimple_assign_set_rhs1 (stmts
[i
], op1
);
5230 gimple_assign_set_rhs2 (stmts
[i
], op2
);
5231 update_stmt (stmts
[i
]);
5235 stmts
[i
] = build_and_add_sum (TREE_TYPE (last_rhs1
), op1
, op2
, opcode
);
5236 gimple_set_visited (stmts
[i
], true);
5238 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5240 fprintf (dump_file
, " into ");
5241 print_gimple_stmt (dump_file
, stmts
[i
], 0);
5245 remove_visited_stmt_chain (last_rhs1
);
5248 /* Transform STMT, which is really (A +B) + (C + D) into the left
5249 linear form, ((A+B)+C)+D.
5250 Recurse on D if necessary. */
5253 linearize_expr (gimple
*stmt
)
5255 gimple_stmt_iterator gsi
;
5256 gimple
*binlhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
5257 gimple
*binrhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt
));
5258 gimple
*oldbinrhs
= binrhs
;
5259 enum tree_code rhscode
= gimple_assign_rhs_code (stmt
);
5260 gimple
*newbinrhs
= NULL
;
5261 class loop
*loop
= loop_containing_stmt (stmt
);
5262 tree lhs
= gimple_assign_lhs (stmt
);
5264 gcc_assert (is_reassociable_op (binlhs
, rhscode
, loop
)
5265 && is_reassociable_op (binrhs
, rhscode
, loop
));
5267 gsi
= gsi_for_stmt (stmt
);
5269 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs1 (binrhs
));
5270 binrhs
= gimple_build_assign (make_ssa_name (TREE_TYPE (lhs
)),
5271 gimple_assign_rhs_code (binrhs
),
5272 gimple_assign_lhs (binlhs
),
5273 gimple_assign_rhs2 (binrhs
));
5274 gimple_assign_set_rhs1 (stmt
, gimple_assign_lhs (binrhs
));
5275 gsi_insert_before (&gsi
, binrhs
, GSI_SAME_STMT
);
5276 gimple_set_uid (binrhs
, gimple_uid (stmt
));
5278 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
)
5279 newbinrhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt
));
5281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5283 fprintf (dump_file
, "Linearized: ");
5284 print_gimple_stmt (dump_file
, stmt
, 0);
5287 reassociate_stats
.linearized
++;
5290 gsi
= gsi_for_stmt (oldbinrhs
);
5291 reassoc_remove_stmt (&gsi
);
5292 release_defs (oldbinrhs
);
5294 gimple_set_visited (stmt
, true);
5295 gimple_set_visited (binlhs
, true);
5296 gimple_set_visited (binrhs
, true);
5298 /* Tail recurse on the new rhs if it still needs reassociation. */
5299 if (newbinrhs
&& is_reassociable_op (newbinrhs
, rhscode
, loop
))
5300 /* ??? This should probably be linearize_expr (newbinrhs) but I don't
5301 want to change the algorithm while converting to tuples. */
5302 linearize_expr (stmt
);
5305 /* If LHS has a single immediate use that is a GIMPLE_ASSIGN statement, return
5306 it. Otherwise, return NULL. */
5309 get_single_immediate_use (tree lhs
)
5311 use_operand_p immuse
;
5314 if (TREE_CODE (lhs
) == SSA_NAME
5315 && single_imm_use (lhs
, &immuse
, &immusestmt
)
5316 && is_gimple_assign (immusestmt
))
5322 /* Recursively negate the value of TONEGATE, and return the SSA_NAME
5323 representing the negated value. Insertions of any necessary
5324 instructions go before GSI.
5325 This function is recursive in that, if you hand it "a_5" as the
5326 value to negate, and a_5 is defined by "a_5 = b_3 + b_4", it will
5327 transform b_3 + b_4 into a_5 = -b_3 + -b_4. */
5330 negate_value (tree tonegate
, gimple_stmt_iterator
*gsip
)
5332 gimple
*negatedefstmt
= NULL
;
5333 tree resultofnegate
;
5334 gimple_stmt_iterator gsi
;
5337 /* If we are trying to negate a name, defined by an add, negate the
5338 add operands instead. */
5339 if (TREE_CODE (tonegate
) == SSA_NAME
)
5340 negatedefstmt
= SSA_NAME_DEF_STMT (tonegate
);
5341 if (TREE_CODE (tonegate
) == SSA_NAME
5342 && is_gimple_assign (negatedefstmt
)
5343 && TREE_CODE (gimple_assign_lhs (negatedefstmt
)) == SSA_NAME
5344 && has_single_use (gimple_assign_lhs (negatedefstmt
))
5345 && gimple_assign_rhs_code (negatedefstmt
) == PLUS_EXPR
)
5347 tree rhs1
= gimple_assign_rhs1 (negatedefstmt
);
5348 tree rhs2
= gimple_assign_rhs2 (negatedefstmt
);
5349 tree lhs
= gimple_assign_lhs (negatedefstmt
);
5352 gsi
= gsi_for_stmt (negatedefstmt
);
5353 rhs1
= negate_value (rhs1
, &gsi
);
5355 gsi
= gsi_for_stmt (negatedefstmt
);
5356 rhs2
= negate_value (rhs2
, &gsi
);
5358 gsi
= gsi_for_stmt (negatedefstmt
);
5359 lhs
= make_ssa_name (TREE_TYPE (lhs
));
5360 gimple_set_visited (negatedefstmt
, true);
5361 g
= gimple_build_assign (lhs
, PLUS_EXPR
, rhs1
, rhs2
);
5362 gimple_set_uid (g
, gimple_uid (negatedefstmt
));
5363 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
5367 tonegate
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (tonegate
), tonegate
);
5368 resultofnegate
= force_gimple_operand_gsi (gsip
, tonegate
, true,
5369 NULL_TREE
, true, GSI_SAME_STMT
);
5371 uid
= gimple_uid (gsi_stmt (gsi
));
5372 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
5374 gimple
*stmt
= gsi_stmt (gsi
);
5375 if (gimple_uid (stmt
) != 0)
5377 gimple_set_uid (stmt
, uid
);
5379 return resultofnegate
;
5382 /* Return true if we should break up the subtract in STMT into an add
5383 with negate. This is true when we the subtract operands are really
5384 adds, or the subtract itself is used in an add expression. In
5385 either case, breaking up the subtract into an add with negate
5386 exposes the adds to reassociation. */
5389 should_break_up_subtract (gimple
*stmt
)
5391 tree lhs
= gimple_assign_lhs (stmt
);
5392 tree binlhs
= gimple_assign_rhs1 (stmt
);
5393 tree binrhs
= gimple_assign_rhs2 (stmt
);
5395 class loop
*loop
= loop_containing_stmt (stmt
);
5397 if (TREE_CODE (binlhs
) == SSA_NAME
5398 && is_reassociable_op (SSA_NAME_DEF_STMT (binlhs
), PLUS_EXPR
, loop
))
5401 if (TREE_CODE (binrhs
) == SSA_NAME
5402 && is_reassociable_op (SSA_NAME_DEF_STMT (binrhs
), PLUS_EXPR
, loop
))
5405 if (TREE_CODE (lhs
) == SSA_NAME
5406 && (immusestmt
= get_single_immediate_use (lhs
))
5407 && is_gimple_assign (immusestmt
)
5408 && (gimple_assign_rhs_code (immusestmt
) == PLUS_EXPR
5409 || (gimple_assign_rhs_code (immusestmt
) == MINUS_EXPR
5410 && gimple_assign_rhs1 (immusestmt
) == lhs
)
5411 || gimple_assign_rhs_code (immusestmt
) == MULT_EXPR
))
5416 /* Transform STMT from A - B into A + -B. */
5419 break_up_subtract (gimple
*stmt
, gimple_stmt_iterator
*gsip
)
5421 tree rhs1
= gimple_assign_rhs1 (stmt
);
5422 tree rhs2
= gimple_assign_rhs2 (stmt
);
5424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5426 fprintf (dump_file
, "Breaking up subtract ");
5427 print_gimple_stmt (dump_file
, stmt
, 0);
5430 rhs2
= negate_value (rhs2
, gsip
);
5431 gimple_assign_set_rhs_with_ops (gsip
, PLUS_EXPR
, rhs1
, rhs2
);
5435 /* Determine whether STMT is a builtin call that raises an SSA name
5436 to an integer power and has only one use. If so, and this is early
5437 reassociation and unsafe math optimizations are permitted, place
5438 the SSA name in *BASE and the exponent in *EXPONENT, and return TRUE.
5439 If any of these conditions does not hold, return FALSE. */
5442 acceptable_pow_call (gcall
*stmt
, tree
*base
, HOST_WIDE_INT
*exponent
)
5445 REAL_VALUE_TYPE c
, cint
;
5447 switch (gimple_call_combined_fn (stmt
))
5450 if (flag_errno_math
)
5453 *base
= gimple_call_arg (stmt
, 0);
5454 arg1
= gimple_call_arg (stmt
, 1);
5456 if (TREE_CODE (arg1
) != REAL_CST
)
5459 c
= TREE_REAL_CST (arg1
);
5461 if (REAL_EXP (&c
) > HOST_BITS_PER_WIDE_INT
)
5464 *exponent
= real_to_integer (&c
);
5465 real_from_integer (&cint
, VOIDmode
, *exponent
, SIGNED
);
5466 if (!real_identical (&c
, &cint
))
5472 *base
= gimple_call_arg (stmt
, 0);
5473 arg1
= gimple_call_arg (stmt
, 1);
5475 if (!tree_fits_shwi_p (arg1
))
5478 *exponent
= tree_to_shwi (arg1
);
5485 /* Expanding negative exponents is generally unproductive, so we don't
5486 complicate matters with those. Exponents of zero and one should
5487 have been handled by expression folding. */
5488 if (*exponent
< 2 || TREE_CODE (*base
) != SSA_NAME
)
5494 /* Try to derive and add operand entry for OP to *OPS. Return false if
5498 try_special_add_to_ops (vec
<operand_entry
*> *ops
,
5499 enum tree_code code
,
5500 tree op
, gimple
* def_stmt
)
5502 tree base
= NULL_TREE
;
5503 HOST_WIDE_INT exponent
= 0;
5505 if (TREE_CODE (op
) != SSA_NAME
5506 || ! has_single_use (op
))
5509 if (code
== MULT_EXPR
5510 && reassoc_insert_powi_p
5511 && flag_unsafe_math_optimizations
5512 && is_gimple_call (def_stmt
)
5513 && acceptable_pow_call (as_a
<gcall
*> (def_stmt
), &base
, &exponent
))
5515 add_repeat_to_ops_vec (ops
, base
, exponent
);
5516 gimple_set_visited (def_stmt
, true);
5519 else if (code
== MULT_EXPR
5520 && is_gimple_assign (def_stmt
)
5521 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
5522 && !HONOR_SNANS (TREE_TYPE (op
))
5523 && (!HONOR_SIGNED_ZEROS (TREE_TYPE (op
))
5524 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (op
))))
5526 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
5527 tree cst
= build_minus_one_cst (TREE_TYPE (op
));
5528 add_to_ops_vec (ops
, rhs1
);
5529 add_to_ops_vec (ops
, cst
);
5530 gimple_set_visited (def_stmt
, true);
5537 /* Recursively linearize a binary expression that is the RHS of STMT.
5538 Place the operands of the expression tree in the vector named OPS. */
5541 linearize_expr_tree (vec
<operand_entry
*> *ops
, gimple
*stmt
,
5542 bool is_associative
, bool set_visited
)
5544 tree binlhs
= gimple_assign_rhs1 (stmt
);
5545 tree binrhs
= gimple_assign_rhs2 (stmt
);
5546 gimple
*binlhsdef
= NULL
, *binrhsdef
= NULL
;
5547 bool binlhsisreassoc
= false;
5548 bool binrhsisreassoc
= false;
5549 enum tree_code rhscode
= gimple_assign_rhs_code (stmt
);
5550 class loop
*loop
= loop_containing_stmt (stmt
);
5553 gimple_set_visited (stmt
, true);
5555 if (TREE_CODE (binlhs
) == SSA_NAME
)
5557 binlhsdef
= SSA_NAME_DEF_STMT (binlhs
);
5558 binlhsisreassoc
= (is_reassociable_op (binlhsdef
, rhscode
, loop
)
5559 && !stmt_could_throw_p (cfun
, binlhsdef
));
5562 if (TREE_CODE (binrhs
) == SSA_NAME
)
5564 binrhsdef
= SSA_NAME_DEF_STMT (binrhs
);
5565 binrhsisreassoc
= (is_reassociable_op (binrhsdef
, rhscode
, loop
)
5566 && !stmt_could_throw_p (cfun
, binrhsdef
));
5569 /* If the LHS is not reassociable, but the RHS is, we need to swap
5570 them. If neither is reassociable, there is nothing we can do, so
5571 just put them in the ops vector. If the LHS is reassociable,
5572 linearize it. If both are reassociable, then linearize the RHS
5575 if (!binlhsisreassoc
)
5577 /* If this is not a associative operation like division, give up. */
5578 if (!is_associative
)
5580 add_to_ops_vec (ops
, binrhs
);
5584 if (!binrhsisreassoc
)
5586 if (!try_special_add_to_ops (ops
, rhscode
, binrhs
, binrhsdef
))
5587 add_to_ops_vec (ops
, binrhs
);
5589 if (!try_special_add_to_ops (ops
, rhscode
, binlhs
, binlhsdef
))
5590 add_to_ops_vec (ops
, binlhs
);
5595 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5597 fprintf (dump_file
, "swapping operands of ");
5598 print_gimple_stmt (dump_file
, stmt
, 0);
5601 swap_ssa_operands (stmt
,
5602 gimple_assign_rhs1_ptr (stmt
),
5603 gimple_assign_rhs2_ptr (stmt
));
5606 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5608 fprintf (dump_file
, " is now ");
5609 print_gimple_stmt (dump_file
, stmt
, 0);
5612 /* We want to make it so the lhs is always the reassociative op,
5614 std::swap (binlhs
, binrhs
);
5616 else if (binrhsisreassoc
)
5618 linearize_expr (stmt
);
5619 binlhs
= gimple_assign_rhs1 (stmt
);
5620 binrhs
= gimple_assign_rhs2 (stmt
);
5623 gcc_assert (TREE_CODE (binrhs
) != SSA_NAME
5624 || !is_reassociable_op (SSA_NAME_DEF_STMT (binrhs
),
5626 linearize_expr_tree (ops
, SSA_NAME_DEF_STMT (binlhs
),
5627 is_associative
, set_visited
);
5629 if (!try_special_add_to_ops (ops
, rhscode
, binrhs
, binrhsdef
))
5630 add_to_ops_vec (ops
, binrhs
);
5633 /* Repropagate the negates back into subtracts, since no other pass
5634 currently does it. */
5637 repropagate_negates (void)
5642 FOR_EACH_VEC_ELT (plus_negates
, i
, negate
)
5644 gimple
*user
= get_single_immediate_use (negate
);
5646 if (!user
|| !is_gimple_assign (user
))
5649 /* The negate operand can be either operand of a PLUS_EXPR
5650 (it can be the LHS if the RHS is a constant for example).
5652 Force the negate operand to the RHS of the PLUS_EXPR, then
5653 transform the PLUS_EXPR into a MINUS_EXPR. */
5654 if (gimple_assign_rhs_code (user
) == PLUS_EXPR
)
5656 /* If the negated operand appears on the LHS of the
5657 PLUS_EXPR, exchange the operands of the PLUS_EXPR
5658 to force the negated operand to the RHS of the PLUS_EXPR. */
5659 if (gimple_assign_rhs1 (user
) == negate
)
5661 swap_ssa_operands (user
,
5662 gimple_assign_rhs1_ptr (user
),
5663 gimple_assign_rhs2_ptr (user
));
5666 /* Now transform the PLUS_EXPR into a MINUS_EXPR and replace
5667 the RHS of the PLUS_EXPR with the operand of the NEGATE_EXPR. */
5668 if (gimple_assign_rhs2 (user
) == negate
)
5670 tree rhs1
= gimple_assign_rhs1 (user
);
5671 tree rhs2
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (negate
));
5672 gimple_stmt_iterator gsi
= gsi_for_stmt (user
);
5673 gimple_assign_set_rhs_with_ops (&gsi
, MINUS_EXPR
, rhs1
, rhs2
);
5677 else if (gimple_assign_rhs_code (user
) == MINUS_EXPR
)
5679 if (gimple_assign_rhs1 (user
) == negate
)
5684 which we transform into
5687 This pushes down the negate which we possibly can merge
5688 into some other operation, hence insert it into the
5689 plus_negates vector. */
5690 gimple
*feed
= SSA_NAME_DEF_STMT (negate
);
5691 tree a
= gimple_assign_rhs1 (feed
);
5692 tree b
= gimple_assign_rhs2 (user
);
5693 gimple_stmt_iterator gsi
= gsi_for_stmt (feed
);
5694 gimple_stmt_iterator gsi2
= gsi_for_stmt (user
);
5695 tree x
= make_ssa_name (TREE_TYPE (gimple_assign_lhs (feed
)));
5696 gimple
*g
= gimple_build_assign (x
, PLUS_EXPR
, a
, b
);
5697 gsi_insert_before (&gsi2
, g
, GSI_SAME_STMT
);
5698 gimple_assign_set_rhs_with_ops (&gsi2
, NEGATE_EXPR
, x
);
5699 user
= gsi_stmt (gsi2
);
5701 reassoc_remove_stmt (&gsi
);
5702 release_defs (feed
);
5703 plus_negates
.safe_push (gimple_assign_lhs (user
));
5707 /* Transform "x = -a; y = b - x" into "y = b + a", getting
5708 rid of one operation. */
5709 gimple
*feed
= SSA_NAME_DEF_STMT (negate
);
5710 tree a
= gimple_assign_rhs1 (feed
);
5711 tree rhs1
= gimple_assign_rhs1 (user
);
5712 gimple_stmt_iterator gsi
= gsi_for_stmt (user
);
5713 gimple_assign_set_rhs_with_ops (&gsi
, PLUS_EXPR
, rhs1
, a
);
5714 update_stmt (gsi_stmt (gsi
));
5720 /* Returns true if OP is of a type for which we can do reassociation.
5721 That is for integral or non-saturating fixed-point types, and for
5722 floating point type when associative-math is enabled. */
5725 can_reassociate_p (tree op
)
5727 tree type
= TREE_TYPE (op
);
5728 if (TREE_CODE (op
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
5730 if ((ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
5731 || NON_SAT_FIXED_POINT_TYPE_P (type
)
5732 || (flag_associative_math
&& FLOAT_TYPE_P (type
)))
5737 /* Break up subtract operations in block BB.
5739 We do this top down because we don't know whether the subtract is
5740 part of a possible chain of reassociation except at the top.
5749 we want to break up k = t - q, but we won't until we've transformed q
5750 = b - r, which won't be broken up until we transform b = c - d.
5752 En passant, clear the GIMPLE visited flag on every statement
5753 and set UIDs within each basic block. */
5756 break_up_subtract_bb (basic_block bb
)
5758 gimple_stmt_iterator gsi
;
5760 unsigned int uid
= 1;
5762 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
5764 gimple
*stmt
= gsi_stmt (gsi
);
5765 gimple_set_visited (stmt
, false);
5766 gimple_set_uid (stmt
, uid
++);
5768 if (!is_gimple_assign (stmt
)
5769 || !can_reassociate_p (gimple_assign_lhs (stmt
)))
5772 /* Look for simple gimple subtract operations. */
5773 if (gimple_assign_rhs_code (stmt
) == MINUS_EXPR
)
5775 if (!can_reassociate_p (gimple_assign_rhs1 (stmt
))
5776 || !can_reassociate_p (gimple_assign_rhs2 (stmt
)))
5779 /* Check for a subtract used only in an addition. If this
5780 is the case, transform it into add of a negate for better
5781 reassociation. IE transform C = A-B into C = A + -B if C
5782 is only used in an addition. */
5783 if (should_break_up_subtract (stmt
))
5784 break_up_subtract (stmt
, &gsi
);
5786 else if (gimple_assign_rhs_code (stmt
) == NEGATE_EXPR
5787 && can_reassociate_p (gimple_assign_rhs1 (stmt
)))
5788 plus_negates
.safe_push (gimple_assign_lhs (stmt
));
5790 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
5792 son
= next_dom_son (CDI_DOMINATORS
, son
))
5793 break_up_subtract_bb (son
);
5796 /* Used for repeated factor analysis. */
5797 struct repeat_factor
5799 /* An SSA name that occurs in a multiply chain. */
5802 /* Cached rank of the factor. */
5805 /* Number of occurrences of the factor in the chain. */
5806 HOST_WIDE_INT count
;
5808 /* An SSA name representing the product of this factor and
5809 all factors appearing later in the repeated factor vector. */
5814 static vec
<repeat_factor
> repeat_factor_vec
;
5816 /* Used for sorting the repeat factor vector. Sort primarily by
5817 ascending occurrence count, secondarily by descending rank. */
5820 compare_repeat_factors (const void *x1
, const void *x2
)
5822 const repeat_factor
*rf1
= (const repeat_factor
*) x1
;
5823 const repeat_factor
*rf2
= (const repeat_factor
*) x2
;
5825 if (rf1
->count
!= rf2
->count
)
5826 return rf1
->count
- rf2
->count
;
5828 return rf2
->rank
- rf1
->rank
;
5831 /* Look for repeated operands in OPS in the multiply tree rooted at
5832 STMT. Replace them with an optimal sequence of multiplies and powi
5833 builtin calls, and remove the used operands from OPS. Return an
5834 SSA name representing the value of the replacement sequence. */
5837 attempt_builtin_powi (gimple
*stmt
, vec
<operand_entry
*> *ops
)
5839 unsigned i
, j
, vec_len
;
5842 repeat_factor
*rf1
, *rf2
;
5843 repeat_factor rfnew
;
5844 tree result
= NULL_TREE
;
5845 tree target_ssa
, iter_result
;
5846 tree type
= TREE_TYPE (gimple_get_lhs (stmt
));
5847 tree powi_fndecl
= mathfn_built_in (type
, BUILT_IN_POWI
);
5848 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
5849 gimple
*mul_stmt
, *pow_stmt
;
5851 /* Nothing to do if BUILT_IN_POWI doesn't exist for this type and
5856 /* Allocate the repeated factor vector. */
5857 repeat_factor_vec
.create (10);
5859 /* Scan the OPS vector for all SSA names in the product and build
5860 up a vector of occurrence counts for each factor. */
5861 FOR_EACH_VEC_ELT (*ops
, i
, oe
)
5863 if (TREE_CODE (oe
->op
) == SSA_NAME
)
5865 FOR_EACH_VEC_ELT (repeat_factor_vec
, j
, rf1
)
5867 if (rf1
->factor
== oe
->op
)
5869 rf1
->count
+= oe
->count
;
5874 if (j
>= repeat_factor_vec
.length ())
5876 rfnew
.factor
= oe
->op
;
5877 rfnew
.rank
= oe
->rank
;
5878 rfnew
.count
= oe
->count
;
5879 rfnew
.repr
= NULL_TREE
;
5880 repeat_factor_vec
.safe_push (rfnew
);
5885 /* Sort the repeated factor vector by (a) increasing occurrence count,
5886 and (b) decreasing rank. */
5887 repeat_factor_vec
.qsort (compare_repeat_factors
);
5889 /* It is generally best to combine as many base factors as possible
5890 into a product before applying __builtin_powi to the result.
5891 However, the sort order chosen for the repeated factor vector
5892 allows us to cache partial results for the product of the base
5893 factors for subsequent use. When we already have a cached partial
5894 result from a previous iteration, it is best to make use of it
5895 before looking for another __builtin_pow opportunity.
5897 As an example, consider x * x * y * y * y * z * z * z * z.
5898 We want to first compose the product x * y * z, raise it to the
5899 second power, then multiply this by y * z, and finally multiply
5900 by z. This can be done in 5 multiplies provided we cache y * z
5901 for use in both expressions:
5909 If we instead ignored the cached y * z and first multiplied by
5910 the __builtin_pow opportunity z * z, we would get the inferior:
5919 vec_len
= repeat_factor_vec
.length ();
5921 /* Repeatedly look for opportunities to create a builtin_powi call. */
5924 HOST_WIDE_INT power
;
5926 /* First look for the largest cached product of factors from
5927 preceding iterations. If found, create a builtin_powi for
5928 it if the minimum occurrence count for its factors is at
5929 least 2, or just use this cached product as our next
5930 multiplicand if the minimum occurrence count is 1. */
5931 FOR_EACH_VEC_ELT (repeat_factor_vec
, j
, rf1
)
5933 if (rf1
->repr
&& rf1
->count
> 0)
5943 iter_result
= rf1
->repr
;
5945 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5949 fputs ("Multiplying by cached product ", dump_file
);
5950 for (elt
= j
; elt
< vec_len
; elt
++)
5952 rf
= &repeat_factor_vec
[elt
];
5953 print_generic_expr (dump_file
, rf
->factor
);
5954 if (elt
< vec_len
- 1)
5955 fputs (" * ", dump_file
);
5957 fputs ("\n", dump_file
);
5962 iter_result
= make_temp_ssa_name (type
, NULL
, "reassocpow");
5963 pow_stmt
= gimple_build_call (powi_fndecl
, 2, rf1
->repr
,
5964 build_int_cst (integer_type_node
,
5966 gimple_call_set_lhs (pow_stmt
, iter_result
);
5967 gimple_set_location (pow_stmt
, gimple_location (stmt
));
5968 gimple_set_uid (pow_stmt
, gimple_uid (stmt
));
5969 gsi_insert_before (&gsi
, pow_stmt
, GSI_SAME_STMT
);
5971 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5975 fputs ("Building __builtin_pow call for cached product (",
5977 for (elt
= j
; elt
< vec_len
; elt
++)
5979 rf
= &repeat_factor_vec
[elt
];
5980 print_generic_expr (dump_file
, rf
->factor
);
5981 if (elt
< vec_len
- 1)
5982 fputs (" * ", dump_file
);
5984 fprintf (dump_file
, ")^" HOST_WIDE_INT_PRINT_DEC
"\n",
5991 /* Otherwise, find the first factor in the repeated factor
5992 vector whose occurrence count is at least 2. If no such
5993 factor exists, there are no builtin_powi opportunities
5995 FOR_EACH_VEC_ELT (repeat_factor_vec
, j
, rf1
)
5997 if (rf1
->count
>= 2)
6006 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6010 fputs ("Building __builtin_pow call for (", dump_file
);
6011 for (elt
= j
; elt
< vec_len
; elt
++)
6013 rf
= &repeat_factor_vec
[elt
];
6014 print_generic_expr (dump_file
, rf
->factor
);
6015 if (elt
< vec_len
- 1)
6016 fputs (" * ", dump_file
);
6018 fprintf (dump_file
, ")^" HOST_WIDE_INT_PRINT_DEC
"\n", power
);
6021 reassociate_stats
.pows_created
++;
6023 /* Visit each element of the vector in reverse order (so that
6024 high-occurrence elements are visited first, and within the
6025 same occurrence count, lower-ranked elements are visited
6026 first). Form a linear product of all elements in this order
6027 whose occurrencce count is at least that of element J.
6028 Record the SSA name representing the product of each element
6029 with all subsequent elements in the vector. */
6030 if (j
== vec_len
- 1)
6031 rf1
->repr
= rf1
->factor
;
6034 for (ii
= vec_len
- 2; ii
>= (int)j
; ii
--)
6038 rf1
= &repeat_factor_vec
[ii
];
6039 rf2
= &repeat_factor_vec
[ii
+ 1];
6041 /* Init the last factor's representative to be itself. */
6043 rf2
->repr
= rf2
->factor
;
6048 target_ssa
= make_temp_ssa_name (type
, NULL
, "reassocpow");
6049 mul_stmt
= gimple_build_assign (target_ssa
, MULT_EXPR
,
6051 gimple_set_location (mul_stmt
, gimple_location (stmt
));
6052 gimple_set_uid (mul_stmt
, gimple_uid (stmt
));
6053 gsi_insert_before (&gsi
, mul_stmt
, GSI_SAME_STMT
);
6054 rf1
->repr
= target_ssa
;
6056 /* Don't reprocess the multiply we just introduced. */
6057 gimple_set_visited (mul_stmt
, true);
6061 /* Form a call to __builtin_powi for the maximum product
6062 just formed, raised to the power obtained earlier. */
6063 rf1
= &repeat_factor_vec
[j
];
6064 iter_result
= make_temp_ssa_name (type
, NULL
, "reassocpow");
6065 pow_stmt
= gimple_build_call (powi_fndecl
, 2, rf1
->repr
,
6066 build_int_cst (integer_type_node
,
6068 gimple_call_set_lhs (pow_stmt
, iter_result
);
6069 gimple_set_location (pow_stmt
, gimple_location (stmt
));
6070 gimple_set_uid (pow_stmt
, gimple_uid (stmt
));
6071 gsi_insert_before (&gsi
, pow_stmt
, GSI_SAME_STMT
);
6074 /* If we previously formed at least one other builtin_powi call,
6075 form the product of this one and those others. */
6078 tree new_result
= make_temp_ssa_name (type
, NULL
, "reassocpow");
6079 mul_stmt
= gimple_build_assign (new_result
, MULT_EXPR
,
6080 result
, iter_result
);
6081 gimple_set_location (mul_stmt
, gimple_location (stmt
));
6082 gimple_set_uid (mul_stmt
, gimple_uid (stmt
));
6083 gsi_insert_before (&gsi
, mul_stmt
, GSI_SAME_STMT
);
6084 gimple_set_visited (mul_stmt
, true);
6085 result
= new_result
;
6088 result
= iter_result
;
6090 /* Decrement the occurrence count of each element in the product
6091 by the count found above, and remove this many copies of each
6093 for (i
= j
; i
< vec_len
; i
++)
6098 rf1
= &repeat_factor_vec
[i
];
6099 rf1
->count
-= power
;
6101 FOR_EACH_VEC_ELT_REVERSE (*ops
, n
, oe
)
6103 if (oe
->op
== rf1
->factor
)
6107 ops
->ordered_remove (n
);
6123 /* At this point all elements in the repeated factor vector have a
6124 remaining occurrence count of 0 or 1, and those with a count of 1
6125 don't have cached representatives. Re-sort the ops vector and
6127 ops
->qsort (sort_by_operand_rank
);
6128 repeat_factor_vec
.release ();
6130 /* Return the final product computed herein. Note that there may
6131 still be some elements with single occurrence count left in OPS;
6132 those will be handled by the normal reassociation logic. */
6136 /* Attempt to optimize
6137 CST1 * copysign (CST2, y) -> copysign (CST1 * CST2, y) if CST1 > 0, or
6138 CST1 * copysign (CST2, y) -> -copysign (CST1 * CST2, y) if CST1 < 0. */
6141 attempt_builtin_copysign (vec
<operand_entry
*> *ops
)
6145 unsigned int length
= ops
->length ();
6146 tree cst
= ops
->last ()->op
;
6148 if (length
== 1 || TREE_CODE (cst
) != REAL_CST
)
6151 FOR_EACH_VEC_ELT (*ops
, i
, oe
)
6153 if (TREE_CODE (oe
->op
) == SSA_NAME
6154 && has_single_use (oe
->op
))
6156 gimple
*def_stmt
= SSA_NAME_DEF_STMT (oe
->op
);
6157 if (gcall
*old_call
= dyn_cast
<gcall
*> (def_stmt
))
6160 switch (gimple_call_combined_fn (old_call
))
6163 CASE_CFN_COPYSIGN_FN
:
6164 arg0
= gimple_call_arg (old_call
, 0);
6165 arg1
= gimple_call_arg (old_call
, 1);
6166 /* The first argument of copysign must be a constant,
6167 otherwise there's nothing to do. */
6168 if (TREE_CODE (arg0
) == REAL_CST
)
6170 tree type
= TREE_TYPE (arg0
);
6171 tree mul
= const_binop (MULT_EXPR
, type
, cst
, arg0
);
6172 /* If we couldn't fold to a single constant, skip it.
6173 That happens e.g. for inexact multiplication when
6175 if (mul
== NULL_TREE
)
6177 /* Instead of adjusting OLD_CALL, let's build a new
6178 call to not leak the LHS and prevent keeping bogus
6179 debug statements. DCE will clean up the old call. */
6181 if (gimple_call_internal_p (old_call
))
6182 new_call
= gimple_build_call_internal
6183 (IFN_COPYSIGN
, 2, mul
, arg1
);
6185 new_call
= gimple_build_call
6186 (gimple_call_fndecl (old_call
), 2, mul
, arg1
);
6187 tree lhs
= make_ssa_name (type
);
6188 gimple_call_set_lhs (new_call
, lhs
);
6189 gimple_set_location (new_call
,
6190 gimple_location (old_call
));
6191 insert_stmt_after (new_call
, old_call
);
6192 /* We've used the constant, get rid of it. */
6194 bool cst1_neg
= real_isneg (TREE_REAL_CST_PTR (cst
));
6195 /* Handle the CST1 < 0 case by negating the result. */
6198 tree negrhs
= make_ssa_name (TREE_TYPE (lhs
));
6200 = gimple_build_assign (negrhs
, NEGATE_EXPR
, lhs
);
6201 insert_stmt_after (negate_stmt
, new_call
);
6206 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6208 fprintf (dump_file
, "Optimizing copysign: ");
6209 print_generic_expr (dump_file
, cst
);
6210 fprintf (dump_file
, " * COPYSIGN (");
6211 print_generic_expr (dump_file
, arg0
);
6212 fprintf (dump_file
, ", ");
6213 print_generic_expr (dump_file
, arg1
);
6214 fprintf (dump_file
, ") into %sCOPYSIGN (",
6215 cst1_neg
? "-" : "");
6216 print_generic_expr (dump_file
, mul
);
6217 fprintf (dump_file
, ", ");
6218 print_generic_expr (dump_file
, arg1
);
6219 fprintf (dump_file
, "\n");
6232 /* Transform STMT at *GSI into a copy by replacing its rhs with NEW_RHS. */
6235 transform_stmt_to_copy (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree new_rhs
)
6239 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6241 fprintf (dump_file
, "Transforming ");
6242 print_gimple_stmt (dump_file
, stmt
, 0);
6245 rhs1
= gimple_assign_rhs1 (stmt
);
6246 gimple_assign_set_rhs_from_tree (gsi
, new_rhs
);
6248 remove_visited_stmt_chain (rhs1
);
6250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6252 fprintf (dump_file
, " into ");
6253 print_gimple_stmt (dump_file
, stmt
, 0);
6257 /* Transform STMT at *GSI into a multiply of RHS1 and RHS2. */
6260 transform_stmt_to_multiply (gimple_stmt_iterator
*gsi
, gimple
*stmt
,
6261 tree rhs1
, tree rhs2
)
6263 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6265 fprintf (dump_file
, "Transforming ");
6266 print_gimple_stmt (dump_file
, stmt
, 0);
6269 gimple_assign_set_rhs_with_ops (gsi
, MULT_EXPR
, rhs1
, rhs2
);
6270 update_stmt (gsi_stmt (*gsi
));
6271 remove_visited_stmt_chain (rhs1
);
6273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6275 fprintf (dump_file
, " into ");
6276 print_gimple_stmt (dump_file
, stmt
, 0);
6280 /* Reassociate expressions in basic block BB and its post-dominator as
6283 Bubble up return status from maybe_optimize_range_tests. */
6286 reassociate_bb (basic_block bb
)
6288 gimple_stmt_iterator gsi
;
6290 gimple
*stmt
= last_stmt (bb
);
6291 bool cfg_cleanup_needed
= false;
6293 if (stmt
&& !gimple_visited_p (stmt
))
6294 cfg_cleanup_needed
|= maybe_optimize_range_tests (stmt
);
6296 bool do_prev
= false;
6297 for (gsi
= gsi_last_bb (bb
);
6298 !gsi_end_p (gsi
); do_prev
? gsi_prev (&gsi
) : (void) 0)
6301 stmt
= gsi_stmt (gsi
);
6303 if (is_gimple_assign (stmt
)
6304 && !stmt_could_throw_p (cfun
, stmt
))
6306 tree lhs
, rhs1
, rhs2
;
6307 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
6309 /* If this was part of an already processed statement,
6310 we don't need to touch it again. */
6311 if (gimple_visited_p (stmt
))
6313 /* This statement might have become dead because of previous
6315 if (has_zero_uses (gimple_get_lhs (stmt
)))
6317 reassoc_remove_stmt (&gsi
);
6318 release_defs (stmt
);
6319 /* We might end up removing the last stmt above which
6320 places the iterator to the end of the sequence.
6321 Reset it to the last stmt in this case and make sure
6322 we don't do gsi_prev in that case. */
6323 if (gsi_end_p (gsi
))
6325 gsi
= gsi_last_bb (bb
);
6332 /* If this is not a gimple binary expression, there is
6333 nothing for us to do with it. */
6334 if (get_gimple_rhs_class (rhs_code
) != GIMPLE_BINARY_RHS
)
6337 lhs
= gimple_assign_lhs (stmt
);
6338 rhs1
= gimple_assign_rhs1 (stmt
);
6339 rhs2
= gimple_assign_rhs2 (stmt
);
6341 /* For non-bit or min/max operations we can't associate
6342 all types. Verify that here. */
6343 if (rhs_code
!= BIT_IOR_EXPR
6344 && rhs_code
!= BIT_AND_EXPR
6345 && rhs_code
!= BIT_XOR_EXPR
6346 && rhs_code
!= MIN_EXPR
6347 && rhs_code
!= MAX_EXPR
6348 && (!can_reassociate_p (lhs
)
6349 || !can_reassociate_p (rhs1
)
6350 || !can_reassociate_p (rhs2
)))
6353 if (associative_tree_code (rhs_code
))
6355 auto_vec
<operand_entry
*> ops
;
6356 tree powi_result
= NULL_TREE
;
6357 bool is_vector
= VECTOR_TYPE_P (TREE_TYPE (lhs
));
6359 /* There may be no immediate uses left by the time we
6360 get here because we may have eliminated them all. */
6361 if (TREE_CODE (lhs
) == SSA_NAME
&& has_zero_uses (lhs
))
6364 gimple_set_visited (stmt
, true);
6365 linearize_expr_tree (&ops
, stmt
, true, true);
6366 ops
.qsort (sort_by_operand_rank
);
6367 int orig_len
= ops
.length ();
6368 optimize_ops_list (rhs_code
, &ops
);
6369 if (undistribute_ops_list (rhs_code
, &ops
,
6370 loop_containing_stmt (stmt
)))
6372 ops
.qsort (sort_by_operand_rank
);
6373 optimize_ops_list (rhs_code
, &ops
);
6375 if (undistribute_bitref_for_vector (rhs_code
, &ops
,
6376 loop_containing_stmt (stmt
)))
6378 ops
.qsort (sort_by_operand_rank
);
6379 optimize_ops_list (rhs_code
, &ops
);
6381 if (rhs_code
== PLUS_EXPR
6382 && transform_add_to_multiply (&ops
))
6383 ops
.qsort (sort_by_operand_rank
);
6385 if (rhs_code
== BIT_IOR_EXPR
|| rhs_code
== BIT_AND_EXPR
)
6388 optimize_vec_cond_expr (rhs_code
, &ops
);
6390 optimize_range_tests (rhs_code
, &ops
, NULL
);
6393 if (rhs_code
== MULT_EXPR
&& !is_vector
)
6395 attempt_builtin_copysign (&ops
);
6397 if (reassoc_insert_powi_p
6398 && flag_unsafe_math_optimizations
)
6399 powi_result
= attempt_builtin_powi (stmt
, &ops
);
6402 operand_entry
*last
;
6403 bool negate_result
= false;
6404 if (ops
.length () > 1
6405 && rhs_code
== MULT_EXPR
)
6408 if ((integer_minus_onep (last
->op
)
6409 || real_minus_onep (last
->op
))
6410 && !HONOR_SNANS (TREE_TYPE (lhs
))
6411 && (!HONOR_SIGNED_ZEROS (TREE_TYPE (lhs
))
6412 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (lhs
))))
6415 negate_result
= true;
6420 /* If the operand vector is now empty, all operands were
6421 consumed by the __builtin_powi optimization. */
6422 if (ops
.length () == 0)
6423 transform_stmt_to_copy (&gsi
, stmt
, powi_result
);
6424 else if (ops
.length () == 1)
6426 tree last_op
= ops
.last ()->op
;
6428 /* If the stmt that defines operand has to be inserted, insert it
6430 if (ops
.last ()->stmt_to_insert
)
6431 insert_stmt_before_use (stmt
, ops
.last ()->stmt_to_insert
);
6433 transform_stmt_to_multiply (&gsi
, stmt
, last_op
,
6436 transform_stmt_to_copy (&gsi
, stmt
, last_op
);
6440 machine_mode mode
= TYPE_MODE (TREE_TYPE (lhs
));
6441 int ops_num
= ops
.length ();
6444 /* For binary bit operations, if there are at least 3
6445 operands and the last operand in OPS is a constant,
6446 move it to the front. This helps ensure that we generate
6447 (X & Y) & C rather than (X & C) & Y. The former will
6448 often match a canonical bit test when we get to RTL. */
6449 if (ops
.length () > 2
6450 && (rhs_code
== BIT_AND_EXPR
6451 || rhs_code
== BIT_IOR_EXPR
6452 || rhs_code
== BIT_XOR_EXPR
)
6453 && TREE_CODE (ops
.last ()->op
) == INTEGER_CST
)
6454 std::swap (*ops
[0], *ops
[ops_num
- 1]);
6456 /* Only rewrite the expression tree to parallel in the
6457 last reassoc pass to avoid useless work back-and-forth
6458 with initial linearization. */
6459 if (!reassoc_insert_powi_p
6460 && ops
.length () > 3
6461 && (width
= get_reassociation_width (ops_num
, rhs_code
,
6464 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6466 "Width = %d was chosen for reassociation\n",
6468 rewrite_expr_tree_parallel (as_a
<gassign
*> (stmt
),
6473 /* When there are three operands left, we want
6474 to make sure the ones that get the double
6475 binary op are chosen wisely. */
6476 int len
= ops
.length ();
6478 swap_ops_for_binary_stmt (ops
, len
- 3, stmt
);
6480 new_lhs
= rewrite_expr_tree (stmt
, 0, ops
,
6486 /* If we combined some repeated factors into a
6487 __builtin_powi call, multiply that result by the
6488 reassociated operands. */
6491 gimple
*mul_stmt
, *lhs_stmt
= SSA_NAME_DEF_STMT (lhs
);
6492 tree type
= TREE_TYPE (lhs
);
6493 tree target_ssa
= make_temp_ssa_name (type
, NULL
,
6495 gimple_set_lhs (lhs_stmt
, target_ssa
);
6496 update_stmt (lhs_stmt
);
6499 target_ssa
= new_lhs
;
6502 mul_stmt
= gimple_build_assign (lhs
, MULT_EXPR
,
6503 powi_result
, target_ssa
);
6504 gimple_set_location (mul_stmt
, gimple_location (stmt
));
6505 gimple_set_uid (mul_stmt
, gimple_uid (stmt
));
6506 gsi_insert_after (&gsi
, mul_stmt
, GSI_NEW_STMT
);
6512 stmt
= SSA_NAME_DEF_STMT (lhs
);
6513 tree tmp
= make_ssa_name (TREE_TYPE (lhs
));
6514 gimple_set_lhs (stmt
, tmp
);
6517 gassign
*neg_stmt
= gimple_build_assign (lhs
, NEGATE_EXPR
,
6519 gimple_set_uid (neg_stmt
, gimple_uid (stmt
));
6520 gsi_insert_after (&gsi
, neg_stmt
, GSI_NEW_STMT
);
6526 for (son
= first_dom_son (CDI_POST_DOMINATORS
, bb
);
6528 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
6529 cfg_cleanup_needed
|= reassociate_bb (son
);
6531 return cfg_cleanup_needed
;
6534 /* Add jumps around shifts for range tests turned into bit tests.
6535 For each SSA_NAME VAR we have code like:
6536 VAR = ...; // final stmt of range comparison
6537 // bit test here...;
6538 OTHERVAR = ...; // final stmt of the bit test sequence
6539 RES = VAR | OTHERVAR;
6540 Turn the above into:
6547 // bit test here...;
6550 # RES = PHI<1(l1), OTHERVAR(l2)>; */
6558 FOR_EACH_VEC_ELT (reassoc_branch_fixups
, i
, var
)
6560 gimple
*def_stmt
= SSA_NAME_DEF_STMT (var
);
6563 bool ok
= single_imm_use (var
, &use
, &use_stmt
);
6565 && is_gimple_assign (use_stmt
)
6566 && gimple_assign_rhs_code (use_stmt
) == BIT_IOR_EXPR
6567 && gimple_bb (def_stmt
) == gimple_bb (use_stmt
));
6569 basic_block cond_bb
= gimple_bb (def_stmt
);
6570 basic_block then_bb
= split_block (cond_bb
, def_stmt
)->dest
;
6571 basic_block merge_bb
= split_block (then_bb
, use_stmt
)->dest
;
6573 gimple_stmt_iterator gsi
= gsi_for_stmt (def_stmt
);
6574 gimple
*g
= gimple_build_cond (NE_EXPR
, var
,
6575 build_zero_cst (TREE_TYPE (var
)),
6576 NULL_TREE
, NULL_TREE
);
6577 location_t loc
= gimple_location (use_stmt
);
6578 gimple_set_location (g
, loc
);
6579 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
6581 edge etrue
= make_edge (cond_bb
, merge_bb
, EDGE_TRUE_VALUE
);
6582 etrue
->probability
= profile_probability::even ();
6583 edge efalse
= find_edge (cond_bb
, then_bb
);
6584 efalse
->flags
= EDGE_FALSE_VALUE
;
6585 efalse
->probability
-= etrue
->probability
;
6586 then_bb
->count
-= etrue
->count ();
6588 tree othervar
= NULL_TREE
;
6589 if (gimple_assign_rhs1 (use_stmt
) == var
)
6590 othervar
= gimple_assign_rhs2 (use_stmt
);
6591 else if (gimple_assign_rhs2 (use_stmt
) == var
)
6592 othervar
= gimple_assign_rhs1 (use_stmt
);
6595 tree lhs
= gimple_assign_lhs (use_stmt
);
6596 gphi
*phi
= create_phi_node (lhs
, merge_bb
);
6597 add_phi_arg (phi
, build_one_cst (TREE_TYPE (lhs
)), etrue
, loc
);
6598 add_phi_arg (phi
, othervar
, single_succ_edge (then_bb
), loc
);
6599 gsi
= gsi_for_stmt (use_stmt
);
6600 gsi_remove (&gsi
, true);
6602 set_immediate_dominator (CDI_DOMINATORS
, merge_bb
, cond_bb
);
6603 set_immediate_dominator (CDI_POST_DOMINATORS
, cond_bb
, merge_bb
);
6605 reassoc_branch_fixups
.release ();
6608 void dump_ops_vector (FILE *file
, vec
<operand_entry
*> ops
);
6609 void debug_ops_vector (vec
<operand_entry
*> ops
);
6611 /* Dump the operand entry vector OPS to FILE. */
6614 dump_ops_vector (FILE *file
, vec
<operand_entry
*> ops
)
6619 FOR_EACH_VEC_ELT (ops
, i
, oe
)
6621 fprintf (file
, "Op %d -> rank: %d, tree: ", i
, oe
->rank
);
6622 print_generic_expr (file
, oe
->op
);
6623 fprintf (file
, "\n");
6627 /* Dump the operand entry vector OPS to STDERR. */
6630 debug_ops_vector (vec
<operand_entry
*> ops
)
6632 dump_ops_vector (stderr
, ops
);
6635 /* Bubble up return status from reassociate_bb. */
6640 break_up_subtract_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
6641 return reassociate_bb (EXIT_BLOCK_PTR_FOR_FN (cfun
));
6644 /* Initialize the reassociation pass. */
6651 int *bbs
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
);
6653 /* Find the loops, so that we can prevent moving calculations in
6655 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
6657 memset (&reassociate_stats
, 0, sizeof (reassociate_stats
));
6659 next_operand_entry_id
= 0;
6661 /* Reverse RPO (Reverse Post Order) will give us something where
6662 deeper loops come later. */
6663 pre_and_rev_post_order_compute (NULL
, bbs
, false);
6664 bb_rank
= XCNEWVEC (long, last_basic_block_for_fn (cfun
));
6665 operand_rank
= new hash_map
<tree
, long>;
6667 /* Give each default definition a distinct rank. This includes
6668 parameters and the static chain. Walk backwards over all
6669 SSA names so that we get proper rank ordering according
6670 to tree_swap_operands_p. */
6671 for (i
= num_ssa_names
- 1; i
> 0; --i
)
6673 tree name
= ssa_name (i
);
6674 if (name
&& SSA_NAME_IS_DEFAULT_DEF (name
))
6675 insert_operand_rank (name
, ++rank
);
6678 /* Set up rank for each BB */
6679 for (i
= 0; i
< n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
; i
++)
6680 bb_rank
[bbs
[i
]] = ++rank
<< 16;
6683 calculate_dominance_info (CDI_POST_DOMINATORS
);
6684 plus_negates
= vNULL
;
6687 /* Cleanup after the reassociation pass, and print stats if
6693 statistics_counter_event (cfun
, "Linearized",
6694 reassociate_stats
.linearized
);
6695 statistics_counter_event (cfun
, "Constants eliminated",
6696 reassociate_stats
.constants_eliminated
);
6697 statistics_counter_event (cfun
, "Ops eliminated",
6698 reassociate_stats
.ops_eliminated
);
6699 statistics_counter_event (cfun
, "Statements rewritten",
6700 reassociate_stats
.rewritten
);
6701 statistics_counter_event (cfun
, "Built-in pow[i] calls encountered",
6702 reassociate_stats
.pows_encountered
);
6703 statistics_counter_event (cfun
, "Built-in powi calls created",
6704 reassociate_stats
.pows_created
);
6706 delete operand_rank
;
6707 operand_entry_pool
.release ();
6709 plus_negates
.release ();
6710 free_dominance_info (CDI_POST_DOMINATORS
);
6711 loop_optimizer_finalize ();
6714 /* Gate and execute functions for Reassociation. If INSERT_POWI_P, enable
6715 insertion of __builtin_powi calls.
6717 Returns TODO_cfg_cleanup if a CFG cleanup pass is desired due to
6718 optimization of a gimple conditional. Otherwise returns zero. */
6721 execute_reassoc (bool insert_powi_p
)
6723 reassoc_insert_powi_p
= insert_powi_p
;
6727 bool cfg_cleanup_needed
;
6728 cfg_cleanup_needed
= do_reassoc ();
6729 repropagate_negates ();
6733 return cfg_cleanup_needed
? TODO_cleanup_cfg
: 0;
6738 const pass_data pass_data_reassoc
=
6740 GIMPLE_PASS
, /* type */
6741 "reassoc", /* name */
6742 OPTGROUP_NONE
, /* optinfo_flags */
6743 TV_TREE_REASSOC
, /* tv_id */
6744 ( PROP_cfg
| PROP_ssa
), /* properties_required */
6745 0, /* properties_provided */
6746 0, /* properties_destroyed */
6747 0, /* todo_flags_start */
6748 TODO_update_ssa_only_virtuals
, /* todo_flags_finish */
6751 class pass_reassoc
: public gimple_opt_pass
6754 pass_reassoc (gcc::context
*ctxt
)
6755 : gimple_opt_pass (pass_data_reassoc
, ctxt
), insert_powi_p (false)
6758 /* opt_pass methods: */
6759 opt_pass
* clone () { return new pass_reassoc (m_ctxt
); }
6760 void set_pass_param (unsigned int n
, bool param
)
6762 gcc_assert (n
== 0);
6763 insert_powi_p
= param
;
6765 virtual bool gate (function
*) { return flag_tree_reassoc
!= 0; }
6766 virtual unsigned int execute (function
*)
6767 { return execute_reassoc (insert_powi_p
); }
6770 /* Enable insertion of __builtin_powi calls during execute_reassoc. See
6771 point 3a in the pass header comment. */
6773 }; // class pass_reassoc
6778 make_pass_reassoc (gcc::context
*ctxt
)
6780 return new pass_reassoc (ctxt
);