Branch data Line data Source code
1 : : /* Reassociation for trees.
2 : : Copyright (C) 2005-2025 Free Software Foundation, Inc.
3 : : Contributed by Daniel Berlin <dan@dberlin.org>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify
8 : : it under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
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.
16 : :
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/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "target.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "cfghooks.h"
30 : : #include "alloc-pool.h"
31 : : #include "tree-pass.h"
32 : : #include "memmodel.h"
33 : : #include "tm_p.h"
34 : : #include "ssa.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"
40 : : #include "cfganal.h"
41 : : #include "gimple-iterator.h"
42 : : #include "gimple-fold.h"
43 : : #include "tree-eh.h"
44 : : #include "gimplify-me.h"
45 : : #include "tree-cfg.h"
46 : : #include "tree-ssa-loop.h"
47 : : #include "flags.h"
48 : : #include "tree-ssa.h"
49 : : #include "langhooks.h"
50 : : #include "cfgloop.h"
51 : : #include "builtins.h"
52 : : #include "gimplify.h"
53 : : #include "case-cfn-macros.h"
54 : : #include "tree-ssa-reassoc.h"
55 : : #include "tree-ssa-math-opts.h"
56 : : #include "gimple-range.h"
57 : : #include "internal-fn.h"
58 : :
59 : : /* This is a simple global reassociation pass. It is, in part, based
60 : : on the LLVM pass of the same name (They do some things more/less
61 : : than we do, in different orders, etc).
62 : :
63 : : It consists of five steps:
64 : :
65 : : 1. Breaking up subtract operations into addition + negate, where
66 : : it would promote the reassociation of adds.
67 : :
68 : : 2. Left linearization of the expression trees, so that (A+B)+(C+D)
69 : : becomes (((A+B)+C)+D), which is easier for us to rewrite later.
70 : : During linearization, we place the operands of the binary
71 : : expressions into a vector of operand_entry_*
72 : :
73 : : 3. Optimization of the operand lists, eliminating things like a +
74 : : -a, a & a, etc.
75 : :
76 : : 3a. Combine repeated factors with the same occurrence counts
77 : : into a __builtin_powi call that will later be optimized into
78 : : an optimal number of multiplies.
79 : :
80 : : 4. Rewrite the expression trees we linearized and optimized so
81 : : they are in proper rank order.
82 : :
83 : : 5. Repropagate negates, as nothing else will clean it up ATM.
84 : :
85 : : A bit of theory on #4, since nobody seems to write anything down
86 : : about why it makes sense to do it the way they do it:
87 : :
88 : : We could do this much nicer theoretically, but don't (for reasons
89 : : explained after how to do it theoretically nice :P).
90 : :
91 : : In order to promote the most redundancy elimination, you want
92 : : binary expressions whose operands are the same rank (or
93 : : preferably, the same value) exposed to the redundancy eliminator,
94 : : for possible elimination.
95 : :
96 : : So the way to do this if we really cared, is to build the new op
97 : : tree from the leaves to the roots, merging as you go, and putting the
98 : : new op on the end of the worklist, until you are left with one
99 : : thing on the worklist.
100 : :
101 : : IE if you have to rewrite the following set of operands (listed with
102 : : rank in parentheses), with opcode PLUS_EXPR:
103 : :
104 : : a (1), b (1), c (1), d (2), e (2)
105 : :
106 : :
107 : : We start with our merge worklist empty, and the ops list with all of
108 : : those on it.
109 : :
110 : : You want to first merge all leaves of the same rank, as much as
111 : : possible.
112 : :
113 : : So first build a binary op of
114 : :
115 : : mergetmp = a + b, and put "mergetmp" on the merge worklist.
116 : :
117 : : Because there is no three operand form of PLUS_EXPR, c is not going to
118 : : be exposed to redundancy elimination as a rank 1 operand.
119 : :
120 : : So you might as well throw it on the merge worklist (you could also
121 : : consider it to now be a rank two operand, and merge it with d and e,
122 : : but in this case, you then have evicted e from a binary op. So at
123 : : least in this situation, you can't win.)
124 : :
125 : : Then build a binary op of d + e
126 : : mergetmp2 = d + e
127 : :
128 : : and put mergetmp2 on the merge worklist.
129 : :
130 : : so merge worklist = {mergetmp, c, mergetmp2}
131 : :
132 : : Continue building binary ops of these operations until you have only
133 : : one operation left on the worklist.
134 : :
135 : : So we have
136 : :
137 : : build binary op
138 : : mergetmp3 = mergetmp + c
139 : :
140 : : worklist = {mergetmp2, mergetmp3}
141 : :
142 : : mergetmp4 = mergetmp2 + mergetmp3
143 : :
144 : : worklist = {mergetmp4}
145 : :
146 : : because we have one operation left, we can now just set the original
147 : : statement equal to the result of that operation.
148 : :
149 : : This will at least expose a + b and d + e to redundancy elimination
150 : : as binary operations.
151 : :
152 : : For extra points, you can reuse the old statements to build the
153 : : mergetmps, since you shouldn't run out.
154 : :
155 : : So why don't we do this?
156 : :
157 : : Because it's expensive, and rarely will help. Most trees we are
158 : : reassociating have 3 or less ops. If they have 2 ops, they already
159 : : will be written into a nice single binary op. If you have 3 ops, a
160 : : single simple check suffices to tell you whether the first two are of the
161 : : same rank. If so, you know to order it
162 : :
163 : : mergetmp = op1 + op2
164 : : newstmt = mergetmp + op3
165 : :
166 : : instead of
167 : : mergetmp = op2 + op3
168 : : newstmt = mergetmp + op1
169 : :
170 : : If all three are of the same rank, you can't expose them all in a
171 : : single binary operator anyway, so the above is *still* the best you
172 : : can do.
173 : :
174 : : Thus, this is what we do. When we have three ops left, we check to see
175 : : what order to put them in, and call it a day. As a nod to vector sum
176 : : reduction, we check if any of the ops are really a phi node that is a
177 : : destructive update for the associating op, and keep the destructive
178 : : update together for vector sum reduction recognition. */
179 : :
180 : : /* Enable insertion of __builtin_powi calls during execute_reassoc. See
181 : : point 3a in the pass header comment. */
182 : : static bool reassoc_insert_powi_p;
183 : :
184 : : /* Enable biasing ranks of loop accumulators. We don't want this before
185 : : vectorization, since it interferes with reduction chains. */
186 : : static bool reassoc_bias_loop_carried_phi_ranks_p;
187 : :
188 : : /* Statistics */
189 : : static struct
190 : : {
191 : : int linearized;
192 : : int constants_eliminated;
193 : : int ops_eliminated;
194 : : int rewritten;
195 : : int pows_encountered;
196 : : int pows_created;
197 : : } reassociate_stats;
198 : :
199 : :
200 : : static object_allocator<operand_entry> operand_entry_pool
201 : : ("operand entry pool");
202 : :
203 : : /* This is used to assign a unique ID to each struct operand_entry
204 : : so that qsort results are identical on different hosts. */
205 : : static unsigned int next_operand_entry_id;
206 : :
207 : : /* Starting rank number for a given basic block, so that we can rank
208 : : operations using unmovable instructions in that BB based on the bb
209 : : depth. */
210 : : static int64_t *bb_rank;
211 : :
212 : : /* Operand->rank hashtable. */
213 : : static hash_map<tree, int64_t> *operand_rank;
214 : :
215 : : /* SSA_NAMEs that are forms of loop accumulators and whose ranks need to be
216 : : biased. */
217 : : static auto_bitmap biased_names;
218 : :
219 : : /* Vector of SSA_NAMEs on which after reassociate_bb is done with
220 : : all basic blocks the CFG should be adjusted - basic blocks
221 : : split right after that SSA_NAME's definition statement and before
222 : : the only use, which must be a bit ior. */
223 : : static vec<tree> reassoc_branch_fixups;
224 : :
225 : : /* Forward decls. */
226 : : static int64_t get_rank (tree);
227 : : static bool reassoc_stmt_dominates_stmt_p (gimple *, gimple *);
228 : :
229 : : /* Wrapper around gsi_remove, which adjusts gimple_uid of debug stmts
230 : : possibly added by gsi_remove. */
231 : :
232 : : static bool
233 : 166001 : reassoc_remove_stmt (gimple_stmt_iterator *gsi)
234 : : {
235 : 166001 : gimple *stmt = gsi_stmt (*gsi);
236 : :
237 : 166001 : if (!MAY_HAVE_DEBUG_BIND_STMTS || gimple_code (stmt) == GIMPLE_PHI)
238 : 72427 : return gsi_remove (gsi, true);
239 : :
240 : 93574 : gimple_stmt_iterator prev = *gsi;
241 : 93574 : gsi_prev (&prev);
242 : 93574 : unsigned uid = gimple_uid (stmt);
243 : 93574 : basic_block bb = gimple_bb (stmt);
244 : 93574 : bool ret = gsi_remove (gsi, true);
245 : 93574 : if (!gsi_end_p (prev))
246 : 93420 : gsi_next (&prev);
247 : : else
248 : 308 : prev = gsi_start_bb (bb);
249 : 93574 : gimple *end_stmt = gsi_stmt (*gsi);
250 : 98971 : while ((stmt = gsi_stmt (prev)) != end_stmt)
251 : : {
252 : 5397 : gcc_assert (stmt && is_gimple_debug (stmt) && gimple_uid (stmt) == 0);
253 : 5397 : gimple_set_uid (stmt, uid);
254 : 5397 : gsi_next (&prev);
255 : : }
256 : : return ret;
257 : : }
258 : :
259 : : /* Bias amount for loop-carried phis. We want this to be larger than
260 : : the depth of any reassociation tree we can see, but not larger than
261 : : the rank difference between two blocks. */
262 : : #define PHI_LOOP_BIAS (1 << 15)
263 : :
264 : : /* Return TRUE iff PHI_LOOP_BIAS should be propagated from one of the STMT's
265 : : operands to the STMT's left-hand side. The goal is to preserve bias in code
266 : : like this:
267 : :
268 : : x_1 = phi(x_0, x_2)
269 : : a = x_1 | 1
270 : : b = a ^ 2
271 : : .MEM = b
272 : : c = b + d
273 : : x_2 = c + e
274 : :
275 : : That is, we need to preserve bias along single-use chains originating from
276 : : loop-carried phis. Only GIMPLE_ASSIGNs to SSA_NAMEs are considered to be
277 : : uses, because only they participate in rank propagation. */
278 : : static bool
279 : 6740879 : propagate_bias_p (gimple *stmt)
280 : : {
281 : 6740879 : use_operand_p use;
282 : 6740879 : imm_use_iterator use_iter;
283 : 6740879 : gimple *single_use_stmt = NULL;
284 : :
285 : 6740879 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_reference)
286 : : return false;
287 : :
288 : 11961320 : FOR_EACH_IMM_USE_FAST (use, use_iter, gimple_assign_lhs (stmt))
289 : : {
290 : 7527831 : gimple *current_use_stmt = USE_STMT (use);
291 : :
292 : 7527831 : if (is_gimple_assign (current_use_stmt)
293 : 7527831 : && TREE_CODE (gimple_assign_lhs (current_use_stmt)) == SSA_NAME)
294 : : {
295 : 5839077 : if (single_use_stmt != NULL && single_use_stmt != current_use_stmt)
296 : : return false;
297 : : single_use_stmt = current_use_stmt;
298 : : }
299 : : }
300 : :
301 : 4433489 : if (single_use_stmt == NULL)
302 : : return false;
303 : :
304 : 4433199 : if (gimple_bb (stmt)->loop_father
305 : 4433199 : != gimple_bb (single_use_stmt)->loop_father)
306 : : return false;
307 : :
308 : : return true;
309 : : }
310 : :
311 : : /* Rank assigned to a phi statement. If STMT is a loop-carried phi of
312 : : an innermost loop, and the phi has only a single use which is inside
313 : : the loop, then the rank is the block rank of the loop latch plus an
314 : : extra bias for the loop-carried dependence. This causes expressions
315 : : calculated into an accumulator variable to be independent for each
316 : : iteration of the loop. If STMT is some other phi, the rank is the
317 : : block rank of its containing block. */
318 : : static int64_t
319 : 1424271 : phi_rank (gimple *stmt)
320 : : {
321 : 1424271 : basic_block bb = gimple_bb (stmt);
322 : 1424271 : class loop *father = bb->loop_father;
323 : 1424271 : tree res;
324 : 1424271 : unsigned i;
325 : 1424271 : use_operand_p use;
326 : 1424271 : gimple *use_stmt;
327 : :
328 : 1424271 : if (!reassoc_bias_loop_carried_phi_ranks_p)
329 : 547209 : return bb_rank[bb->index];
330 : :
331 : : /* We only care about real loops (those with a latch). */
332 : 877062 : if (!father->latch)
333 : 88 : return bb_rank[bb->index];
334 : :
335 : : /* Interesting phis must be in headers of innermost loops. */
336 : 876974 : if (bb != father->header
337 : 684136 : || father->inner)
338 : 345303 : return bb_rank[bb->index];
339 : :
340 : : /* Ignore virtual SSA_NAMEs. */
341 : 531671 : res = gimple_phi_result (stmt);
342 : 1063342 : if (virtual_operand_p (res))
343 : 0 : return bb_rank[bb->index];
344 : :
345 : : /* The phi definition must have a single use, and that use must be
346 : : within the loop. Otherwise this isn't an accumulator pattern. */
347 : 531671 : if (!single_imm_use (res, &use, &use_stmt)
348 : 531671 : || gimple_bb (use_stmt)->loop_father != father)
349 : 465277 : return bb_rank[bb->index];
350 : :
351 : : /* Look for phi arguments from within the loop. If found, bias this phi. */
352 : 77010 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
353 : : {
354 : 76789 : tree arg = gimple_phi_arg_def (stmt, i);
355 : 76789 : if (TREE_CODE (arg) == SSA_NAME
356 : 76789 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
357 : : {
358 : 71612 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
359 : 71612 : if (gimple_bb (def_stmt)->loop_father == father)
360 : 66173 : return bb_rank[father->latch->index] + PHI_LOOP_BIAS;
361 : : }
362 : : }
363 : :
364 : : /* Must be an uninteresting phi. */
365 : 221 : return bb_rank[bb->index];
366 : : }
367 : :
368 : : /* Return the maximum of RANK and the rank that should be propagated
369 : : from expression OP. For most operands, this is just the rank of OP.
370 : : For loop-carried phis, the value is zero to avoid undoing the bias
371 : : in favor of the phi. */
372 : : static int64_t
373 : 7317598 : propagate_rank (int64_t rank, tree op, bool *maybe_biased_p)
374 : : {
375 : 7317598 : int64_t op_rank;
376 : :
377 : 7317598 : op_rank = get_rank (op);
378 : :
379 : : /* Check whether op is biased after the get_rank () call, since it might have
380 : : updated biased_names. */
381 : 7317598 : if (TREE_CODE (op) == SSA_NAME
382 : 7317598 : && bitmap_bit_p (biased_names, SSA_NAME_VERSION (op)))
383 : : {
384 : 45572 : if (maybe_biased_p == NULL)
385 : : return rank;
386 : 33382 : *maybe_biased_p = true;
387 : : }
388 : :
389 : 7305408 : return MAX (rank, op_rank);
390 : : }
391 : :
392 : : /* Look up the operand rank structure for expression E. */
393 : :
394 : : static inline int64_t
395 : 13447728 : find_operand_rank (tree e)
396 : : {
397 : 13447728 : int64_t *slot = operand_rank->get (e);
398 : 13447728 : return slot ? *slot : -1;
399 : : }
400 : :
401 : : /* Insert {E,RANK} into the operand rank hashtable. */
402 : :
403 : : static inline void
404 : 14595339 : insert_operand_rank (tree e, int64_t rank)
405 : : {
406 : 14595339 : gcc_assert (rank > 0);
407 : 14595339 : bool existed = operand_rank->put (e, rank);
408 : 14595339 : gcc_assert (!existed);
409 : 14595339 : }
410 : :
411 : : /* Given an expression E, return the rank of the expression. */
412 : :
413 : : static int64_t
414 : 16775009 : get_rank (tree e)
415 : : {
416 : : /* SSA_NAME's have the rank of the expression they are the result
417 : : of.
418 : : For globals and uninitialized values, the rank is 0.
419 : : For function arguments, use the pre-setup rank.
420 : : For PHI nodes, stores, asm statements, etc, we use the rank of
421 : : the BB.
422 : : For simple operations, the rank is the maximum rank of any of
423 : : its operands, or the bb_rank, whichever is less.
424 : : I make no claims that this is optimal, however, it gives good
425 : : results. */
426 : :
427 : : /* We make an exception to the normal ranking system to break
428 : : dependences of accumulator variables in loops. Suppose we
429 : : have a simple one-block loop containing:
430 : :
431 : : x_1 = phi(x_0, x_2)
432 : : b = a + x_1
433 : : c = b + d
434 : : x_2 = c + e
435 : :
436 : : As shown, each iteration of the calculation into x is fully
437 : : dependent upon the iteration before it. We would prefer to
438 : : see this in the form:
439 : :
440 : : x_1 = phi(x_0, x_2)
441 : : b = a + d
442 : : c = b + e
443 : : x_2 = c + x_1
444 : :
445 : : If the loop is unrolled, the calculations of b and c from
446 : : different iterations can be interleaved.
447 : :
448 : : To obtain this result during reassociation, we bias the rank
449 : : of the phi definition x_1 upward, when it is recognized as an
450 : : accumulator pattern. The artificial rank causes it to be
451 : : added last, providing the desired independence. */
452 : :
453 : 16775009 : if (TREE_CODE (e) == SSA_NAME)
454 : : {
455 : 13447728 : ssa_op_iter iter;
456 : 13447728 : gimple *stmt;
457 : 13447728 : int64_t rank;
458 : 13447728 : tree op;
459 : :
460 : : /* If we already have a rank for this expression, use that. */
461 : 13447728 : rank = find_operand_rank (e);
462 : 13447728 : if (rank != -1)
463 : : return rank;
464 : :
465 : 8428606 : stmt = SSA_NAME_DEF_STMT (e);
466 : 8428606 : if (gimple_code (stmt) == GIMPLE_PHI)
467 : : {
468 : 1424271 : rank = phi_rank (stmt);
469 : 1424271 : if (rank != bb_rank[gimple_bb (stmt)->index])
470 : 66173 : bitmap_set_bit (biased_names, SSA_NAME_VERSION (e));
471 : : }
472 : :
473 : 7004335 : else if (!is_gimple_assign (stmt))
474 : 263456 : rank = bb_rank[gimple_bb (stmt)->index];
475 : :
476 : : else
477 : : {
478 : 6740879 : bool biased_p = false;
479 : 6740879 : bool *maybe_biased_p = propagate_bias_p (stmt) ? &biased_p : NULL;
480 : :
481 : : /* Otherwise, find the maximum rank for the operands. As an
482 : : exception, remove the bias from loop-carried phis when propagating
483 : : the rank so that dependent operations are not also biased. */
484 : : /* Simply walk over all SSA uses - this takes advatage of the
485 : : fact that non-SSA operands are is_gimple_min_invariant and
486 : : thus have rank 0. */
487 : 6740879 : rank = 0;
488 : 14058477 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
489 : 7317598 : rank = propagate_rank (rank, op, maybe_biased_p);
490 : :
491 : 6740879 : rank += 1;
492 : 6740879 : if (biased_p)
493 : 31819 : bitmap_set_bit (biased_names, SSA_NAME_VERSION (e));
494 : : }
495 : :
496 : 8428606 : if (dump_file && (dump_flags & TDF_DETAILS))
497 : : {
498 : 205 : fprintf (dump_file, "Rank for ");
499 : 205 : print_generic_expr (dump_file, e);
500 : 205 : fprintf (dump_file, " is %" PRId64 "\n", rank);
501 : : }
502 : :
503 : : /* Note the rank in the hashtable so we don't recompute it. */
504 : 8428606 : insert_operand_rank (e, rank);
505 : 8428606 : return rank;
506 : : }
507 : :
508 : : /* Constants, globals, etc., are rank 0 */
509 : : return 0;
510 : : }
511 : :
512 : :
513 : : /* We want integer ones to end up last no matter what, since they are
514 : : the ones we can do the most with. */
515 : : #define INTEGER_CONST_TYPE 1 << 4
516 : : #define FLOAT_ONE_CONST_TYPE 1 << 3
517 : : #define FLOAT_CONST_TYPE 1 << 2
518 : : #define OTHER_CONST_TYPE 1 << 1
519 : :
520 : : /* Classify an invariant tree into integer, float, or other, so that
521 : : we can sort them to be near other constants of the same type. */
522 : : static inline int
523 : 314968 : constant_type (tree t)
524 : : {
525 : 314968 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
526 : : return INTEGER_CONST_TYPE;
527 : 8780 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t)))
528 : : {
529 : : /* Sort -1.0 and 1.0 constants last, while in some cases
530 : : const_binop can't optimize some inexact operations, multiplication
531 : : by -1.0 or 1.0 can be always merged with others. */
532 : 6604 : if (real_onep (t) || real_minus_onep (t))
533 : 816 : return FLOAT_ONE_CONST_TYPE;
534 : : return FLOAT_CONST_TYPE;
535 : : }
536 : : else
537 : : return OTHER_CONST_TYPE;
538 : : }
539 : :
540 : : /* qsort comparison function to sort operand entries PA and PB by rank
541 : : so that the sorted array is ordered by rank in decreasing order. */
542 : : static int
543 : 23272333 : sort_by_operand_rank (const void *pa, const void *pb)
544 : : {
545 : 23272333 : const operand_entry *oea = *(const operand_entry *const *)pa;
546 : 23272333 : const operand_entry *oeb = *(const operand_entry *const *)pb;
547 : :
548 : 23272333 : if (oeb->rank != oea->rank)
549 : 34431302 : return oeb->rank > oea->rank ? 1 : -1;
550 : :
551 : : /* It's nicer for optimize_expression if constants that are likely
552 : : to fold when added/multiplied/whatever are put next to each
553 : : other. Since all constants have rank 0, order them by type. */
554 : 2838465 : if (oea->rank == 0)
555 : : {
556 : 157426 : if (constant_type (oeb->op) != constant_type (oea->op))
557 : 58 : return constant_type (oea->op) - constant_type (oeb->op);
558 : : else
559 : : /* To make sorting result stable, we use unique IDs to determine
560 : : order. */
561 : 252565 : return oeb->id > oea->id ? 1 : -1;
562 : : }
563 : :
564 : 2681039 : if (TREE_CODE (oea->op) != SSA_NAME)
565 : : {
566 : 0 : if (TREE_CODE (oeb->op) != SSA_NAME)
567 : 0 : return oeb->id > oea->id ? 1 : -1;
568 : : else
569 : : return 1;
570 : : }
571 : 2681039 : else if (TREE_CODE (oeb->op) != SSA_NAME)
572 : : return -1;
573 : :
574 : : /* Lastly, make sure the versions that are the same go next to each
575 : : other. */
576 : 2681039 : if (SSA_NAME_VERSION (oeb->op) != SSA_NAME_VERSION (oea->op))
577 : : {
578 : : /* As SSA_NAME_VERSION is assigned pretty randomly, because we reuse
579 : : versions of removed SSA_NAMEs, so if possible, prefer to sort
580 : : based on basic block and gimple_uid of the SSA_NAME_DEF_STMT.
581 : : See PR60418. */
582 : 2631158 : gimple *stmta = SSA_NAME_DEF_STMT (oea->op);
583 : 2631158 : gimple *stmtb = SSA_NAME_DEF_STMT (oeb->op);
584 : 2631158 : basic_block bba = gimple_bb (stmta);
585 : 2631158 : basic_block bbb = gimple_bb (stmtb);
586 : 2631158 : if (bbb != bba)
587 : : {
588 : : /* One of the SSA_NAMEs can be defined in oeN->stmt_to_insert
589 : : but the other might not. */
590 : 171798 : if (!bba)
591 : : return 1;
592 : 166863 : if (!bbb)
593 : : return -1;
594 : : /* If neither is, compare bb_rank. */
595 : 160713 : if (bb_rank[bbb->index] != bb_rank[bba->index])
596 : 160713 : return (bb_rank[bbb->index] >> 16) - (bb_rank[bba->index] >> 16);
597 : : }
598 : :
599 : 2459360 : bool da = reassoc_stmt_dominates_stmt_p (stmta, stmtb);
600 : 2459360 : bool db = reassoc_stmt_dominates_stmt_p (stmtb, stmta);
601 : 2459360 : if (da != db)
602 : 3757273 : return da ? 1 : -1;
603 : :
604 : 59590 : return SSA_NAME_VERSION (oeb->op) > SSA_NAME_VERSION (oea->op) ? 1 : -1;
605 : : }
606 : :
607 : 49881 : return oeb->id > oea->id ? 1 : -1;
608 : : }
609 : :
610 : : /* Add an operand entry to *OPS for the tree operand OP. */
611 : :
612 : : static void
613 : 9457054 : add_to_ops_vec (vec<operand_entry *> *ops, tree op, gimple *stmt_to_insert = NULL)
614 : : {
615 : 9457054 : operand_entry *oe = operand_entry_pool.allocate ();
616 : :
617 : 9457054 : oe->op = op;
618 : 9457054 : oe->rank = get_rank (op);
619 : 9457054 : oe->id = next_operand_entry_id++;
620 : 9457054 : oe->count = 1;
621 : 9457054 : oe->stmt_to_insert = stmt_to_insert;
622 : 9457054 : ops->safe_push (oe);
623 : 9457054 : }
624 : :
625 : : /* Add an operand entry to *OPS for the tree operand OP with repeat
626 : : count REPEAT. */
627 : :
628 : : static void
629 : 18 : add_repeat_to_ops_vec (vec<operand_entry *> *ops, tree op,
630 : : HOST_WIDE_INT repeat)
631 : : {
632 : 18 : operand_entry *oe = operand_entry_pool.allocate ();
633 : :
634 : 18 : oe->op = op;
635 : 18 : oe->rank = get_rank (op);
636 : 18 : oe->id = next_operand_entry_id++;
637 : 18 : oe->count = repeat;
638 : 18 : oe->stmt_to_insert = NULL;
639 : 18 : ops->safe_push (oe);
640 : :
641 : 18 : reassociate_stats.pows_encountered++;
642 : 18 : }
643 : :
644 : : /* Returns true if we can associate the SSA def OP. */
645 : :
646 : : static bool
647 : 31876720 : can_reassociate_op_p (tree op)
648 : : {
649 : 31876720 : if (TREE_CODE (op) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
650 : : return false;
651 : : /* Uninitialized variables can't participate in reassociation. */
652 : 31875906 : if (TREE_CODE (op) == SSA_NAME && ssa_name_maybe_undef_p (op))
653 : : return false;
654 : : /* Make sure asm goto outputs do not participate in reassociation since
655 : : we have no way to find an insertion place after asm goto. */
656 : 31871243 : if (TREE_CODE (op) == SSA_NAME
657 : 23506281 : && gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_ASM
658 : 31892179 : && gimple_asm_nlabels (as_a <gasm *> (SSA_NAME_DEF_STMT (op))) != 0)
659 : 70 : return false;
660 : : return true;
661 : : }
662 : :
663 : : /* Returns true if we can reassociate operations of TYPE.
664 : : That is for integral or non-saturating fixed-point types, and for
665 : : floating point type when associative-math is enabled. */
666 : :
667 : : static bool
668 : 58055198 : can_reassociate_type_p (tree type)
669 : : {
670 : 58055198 : if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
671 : 35939114 : || NON_SAT_FIXED_POINT_TYPE_P (type)
672 : 93994312 : || (flag_associative_math && FLOAT_TYPE_P (type)))
673 : 22476221 : return true;
674 : : return false;
675 : : }
676 : :
677 : : /* Return true if STMT is reassociable operation containing a binary
678 : : operation with tree code CODE, and is inside LOOP. */
679 : :
680 : : static bool
681 : 7655069 : is_reassociable_op (gimple *stmt, enum tree_code code, class loop *loop)
682 : : {
683 : 7655069 : basic_block bb = gimple_bb (stmt);
684 : :
685 : 7655069 : if (gimple_bb (stmt) == NULL)
686 : : return false;
687 : :
688 : 7475498 : if (!flow_bb_inside_loop_p (loop, bb))
689 : : return false;
690 : :
691 : 7273269 : if (is_gimple_assign (stmt)
692 : 5777459 : && gimple_assign_rhs_code (stmt) == code
693 : 8132020 : && has_single_use (gimple_assign_lhs (stmt)))
694 : : {
695 : 661006 : tree rhs1 = gimple_assign_rhs1 (stmt);
696 : 661006 : tree rhs2 = gimple_assign_rhs2 (stmt);
697 : 661006 : if (!can_reassociate_op_p (rhs1)
698 : 661006 : || (rhs2 && !can_reassociate_op_p (rhs2)))
699 : : return false;
700 : : return true;
701 : : }
702 : :
703 : : return false;
704 : : }
705 : :
706 : :
707 : : /* Return true if STMT is a nop-conversion. */
708 : :
709 : : static bool
710 : 7562911 : gimple_nop_conversion_p (gimple *stmt)
711 : : {
712 : 7562911 : if (gassign *ass = dyn_cast <gassign *> (stmt))
713 : : {
714 : 8990361 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
715 : 6658359 : && tree_nop_conversion_p (TREE_TYPE (gimple_assign_lhs (ass)),
716 : 1442119 : TREE_TYPE (gimple_assign_rhs1 (ass))))
717 : : return true;
718 : : }
719 : : return false;
720 : : }
721 : :
722 : : /* Given NAME, if NAME is defined by a unary operation OPCODE, return the
723 : : operand of the negate operation. Otherwise, return NULL. */
724 : :
725 : : static tree
726 : 7477136 : get_unary_op (tree name, enum tree_code opcode)
727 : : {
728 : 7477136 : gimple *stmt = SSA_NAME_DEF_STMT (name);
729 : :
730 : : /* Look through nop conversions (sign changes). */
731 : 7477136 : if (gimple_nop_conversion_p (stmt)
732 : 7477136 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
733 : 821411 : stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
734 : :
735 : 7477136 : if (!is_gimple_assign (stmt))
736 : : return NULL_TREE;
737 : :
738 : 4779040 : if (gimple_assign_rhs_code (stmt) == opcode)
739 : 127092 : return gimple_assign_rhs1 (stmt);
740 : : return NULL_TREE;
741 : : }
742 : :
743 : : /* Return true if OP1 and OP2 have the same value if casted to either type. */
744 : :
745 : : static bool
746 : 44015 : ops_equal_values_p (tree op1, tree op2)
747 : : {
748 : 44015 : if (op1 == op2)
749 : : return true;
750 : :
751 : 43798 : tree orig_op1 = op1;
752 : 43798 : if (TREE_CODE (op1) == SSA_NAME)
753 : : {
754 : 43798 : gimple *stmt = SSA_NAME_DEF_STMT (op1);
755 : 43798 : if (gimple_nop_conversion_p (stmt))
756 : : {
757 : 15791 : op1 = gimple_assign_rhs1 (stmt);
758 : 15791 : if (op1 == op2)
759 : : return true;
760 : : }
761 : : }
762 : :
763 : 41977 : if (TREE_CODE (op2) == SSA_NAME)
764 : : {
765 : 41977 : gimple *stmt = SSA_NAME_DEF_STMT (op2);
766 : 41977 : if (gimple_nop_conversion_p (stmt))
767 : : {
768 : 14315 : op2 = gimple_assign_rhs1 (stmt);
769 : 14315 : if (op1 == op2
770 : 14315 : || orig_op1 == op2)
771 : : return true;
772 : : }
773 : : }
774 : :
775 : : return false;
776 : : }
777 : :
778 : :
779 : : /* If CURR and LAST are a pair of ops that OPCODE allows us to
780 : : eliminate through equivalences, do so, remove them from OPS, and
781 : : return true. Otherwise, return false. */
782 : :
783 : : static bool
784 : 9350355 : eliminate_duplicate_pair (enum tree_code opcode,
785 : : vec<operand_entry *> *ops,
786 : : bool *all_done,
787 : : unsigned int i,
788 : : operand_entry *curr,
789 : : operand_entry *last)
790 : : {
791 : :
792 : : /* If we have two of the same op, and the opcode is & |, min, or max,
793 : : we can eliminate one of them.
794 : : If we have two of the same op, and the opcode is ^, we can
795 : : eliminate both of them. */
796 : :
797 : 9350355 : if (last && last->op == curr->op)
798 : : {
799 : 5068 : switch (opcode)
800 : : {
801 : 30 : case MAX_EXPR:
802 : 30 : case MIN_EXPR:
803 : 30 : case BIT_IOR_EXPR:
804 : 30 : case BIT_AND_EXPR:
805 : 30 : if (dump_file && (dump_flags & TDF_DETAILS))
806 : : {
807 : 1 : fprintf (dump_file, "Equivalence: ");
808 : 1 : print_generic_expr (dump_file, curr->op);
809 : 1 : fprintf (dump_file, " [&|minmax] ");
810 : 1 : print_generic_expr (dump_file, last->op);
811 : 1 : fprintf (dump_file, " -> ");
812 : 1 : print_generic_stmt (dump_file, last->op);
813 : : }
814 : :
815 : 30 : ops->ordered_remove (i);
816 : 30 : reassociate_stats.ops_eliminated ++;
817 : :
818 : 30 : return true;
819 : :
820 : 15 : case BIT_XOR_EXPR:
821 : 15 : if (dump_file && (dump_flags & TDF_DETAILS))
822 : : {
823 : 0 : fprintf (dump_file, "Equivalence: ");
824 : 0 : print_generic_expr (dump_file, curr->op);
825 : 0 : fprintf (dump_file, " ^ ");
826 : 0 : print_generic_expr (dump_file, last->op);
827 : 0 : fprintf (dump_file, " -> nothing\n");
828 : : }
829 : :
830 : 15 : reassociate_stats.ops_eliminated += 2;
831 : :
832 : 15 : if (ops->length () == 2)
833 : : {
834 : 1 : ops->truncate (0);
835 : 1 : add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (last->op)));
836 : 1 : *all_done = true;
837 : : }
838 : : else
839 : : {
840 : 14 : ops->ordered_remove (i-1);
841 : 14 : ops->ordered_remove (i-1);
842 : : }
843 : :
844 : 15 : return true;
845 : :
846 : : default:
847 : : break;
848 : : }
849 : : }
850 : : return false;
851 : : }
852 : :
853 : : static vec<tree> plus_negates;
854 : :
855 : : /* If OPCODE is PLUS_EXPR, CURR->OP is a negate expression or a bitwise not
856 : : expression, look in OPS for a corresponding positive operation to cancel
857 : : it out. If we find one, remove the other from OPS, replace
858 : : OPS[CURRINDEX] with 0 or -1, respectively, and return true. Otherwise,
859 : : return false. */
860 : :
861 : : static bool
862 : 9350310 : eliminate_plus_minus_pair (enum tree_code opcode,
863 : : vec<operand_entry *> *ops,
864 : : unsigned int currindex,
865 : : operand_entry *curr)
866 : : {
867 : 9350310 : tree negateop;
868 : 9350310 : tree notop;
869 : 9350310 : unsigned int i;
870 : 9350310 : operand_entry *oe;
871 : :
872 : 9350310 : if (opcode != PLUS_EXPR || TREE_CODE (curr->op) != SSA_NAME)
873 : : return false;
874 : :
875 : 2945589 : negateop = get_unary_op (curr->op, NEGATE_EXPR);
876 : 2945589 : notop = get_unary_op (curr->op, BIT_NOT_EXPR);
877 : 2945589 : if (negateop == NULL_TREE && notop == NULL_TREE)
878 : : return false;
879 : :
880 : : /* Any non-negated version will have a rank that is one less than
881 : : the current rank. So once we hit those ranks, if we don't find
882 : : one, we can stop. */
883 : :
884 : 131879 : for (i = currindex + 1;
885 : 196139 : ops->iterate (i, &oe)
886 : 240154 : && oe->rank >= curr->rank - 1 ;
887 : : i++)
888 : : {
889 : 44015 : if (negateop
890 : 44015 : && ops_equal_values_p (oe->op, negateop))
891 : : {
892 : 1796 : if (dump_file && (dump_flags & TDF_DETAILS))
893 : : {
894 : 0 : fprintf (dump_file, "Equivalence: ");
895 : 0 : print_generic_expr (dump_file, negateop);
896 : 0 : fprintf (dump_file, " + -");
897 : 0 : print_generic_expr (dump_file, oe->op);
898 : 0 : fprintf (dump_file, " -> 0\n");
899 : : }
900 : :
901 : 1796 : ops->ordered_remove (i);
902 : 1796 : add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (oe->op)));
903 : 1796 : ops->ordered_remove (currindex);
904 : 1796 : reassociate_stats.ops_eliminated ++;
905 : :
906 : 1796 : return true;
907 : : }
908 : 42219 : else if (notop
909 : 42219 : && ops_equal_values_p (oe->op, notop))
910 : : {
911 : 1807 : tree op_type = TREE_TYPE (oe->op);
912 : :
913 : 1807 : if (dump_file && (dump_flags & TDF_DETAILS))
914 : : {
915 : 0 : fprintf (dump_file, "Equivalence: ");
916 : 0 : print_generic_expr (dump_file, notop);
917 : 0 : fprintf (dump_file, " + ~");
918 : 0 : print_generic_expr (dump_file, oe->op);
919 : 0 : fprintf (dump_file, " -> -1\n");
920 : : }
921 : :
922 : 1807 : ops->ordered_remove (i);
923 : 1807 : add_to_ops_vec (ops, build_all_ones_cst (op_type));
924 : 1807 : ops->ordered_remove (currindex);
925 : 1807 : reassociate_stats.ops_eliminated ++;
926 : :
927 : 1807 : return true;
928 : : }
929 : : }
930 : :
931 : : /* If CURR->OP is a negate expr without nop conversion in a plus expr:
932 : : save it for later inspection in repropagate_negates(). */
933 : 87864 : if (negateop != NULL_TREE
934 : 87864 : && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (curr->op)) == NEGATE_EXPR)
935 : 87298 : plus_negates.safe_push (curr->op);
936 : :
937 : : return false;
938 : : }
939 : :
940 : : /* If OPCODE is BIT_IOR_EXPR, BIT_AND_EXPR, and, CURR->OP is really a
941 : : bitwise not expression, look in OPS for a corresponding operand to
942 : : cancel it out. If we find one, remove the other from OPS, replace
943 : : OPS[CURRINDEX] with 0, and return true. Otherwise, return
944 : : false. */
945 : :
946 : : static bool
947 : 9350356 : eliminate_not_pairs (enum tree_code opcode,
948 : : vec<operand_entry *> *ops,
949 : : unsigned int currindex,
950 : : operand_entry *curr)
951 : : {
952 : 9350356 : tree notop;
953 : 9350356 : unsigned int i;
954 : 9350356 : operand_entry *oe;
955 : :
956 : 9350356 : if ((opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
957 : 2124334 : || TREE_CODE (curr->op) != SSA_NAME)
958 : : return false;
959 : :
960 : 1585958 : notop = get_unary_op (curr->op, BIT_NOT_EXPR);
961 : 1585958 : if (notop == NULL_TREE)
962 : : return false;
963 : :
964 : : /* Any non-not version will have a rank that is one less than
965 : : the current rank. So once we hit those ranks, if we don't find
966 : : one, we can stop. */
967 : :
968 : 42228 : for (i = currindex + 1;
969 : 9377555 : ops->iterate (i, &oe)
970 : 69427 : && oe->rank >= curr->rank - 1;
971 : : i++)
972 : : {
973 : 6604 : if (oe->op == notop)
974 : : {
975 : 1 : if (dump_file && (dump_flags & TDF_DETAILS))
976 : : {
977 : 0 : fprintf (dump_file, "Equivalence: ");
978 : 0 : print_generic_expr (dump_file, notop);
979 : 0 : if (opcode == BIT_AND_EXPR)
980 : 0 : fprintf (dump_file, " & ~");
981 : 0 : else if (opcode == BIT_IOR_EXPR)
982 : 0 : fprintf (dump_file, " | ~");
983 : 0 : print_generic_expr (dump_file, oe->op);
984 : 0 : if (opcode == BIT_AND_EXPR)
985 : 0 : fprintf (dump_file, " -> 0\n");
986 : 0 : else if (opcode == BIT_IOR_EXPR)
987 : 0 : fprintf (dump_file, " -> -1\n");
988 : : }
989 : :
990 : 1 : if (opcode == BIT_AND_EXPR)
991 : 1 : oe->op = build_zero_cst (TREE_TYPE (oe->op));
992 : 0 : else if (opcode == BIT_IOR_EXPR)
993 : 0 : oe->op = build_all_ones_cst (TREE_TYPE (oe->op));
994 : :
995 : 1 : reassociate_stats.ops_eliminated += ops->length () - 1;
996 : 1 : ops->truncate (0);
997 : 1 : ops->quick_push (oe);
998 : 1 : return true;
999 : : }
1000 : : }
1001 : :
1002 : : return false;
1003 : : }
1004 : :
1005 : : /* Use constant value that may be present in OPS to try to eliminate
1006 : : operands. Note that this function is only really used when we've
1007 : : eliminated ops for other reasons, or merged constants. Across
1008 : : single statements, fold already does all of this, plus more. There
1009 : : is little point in duplicating logic, so I've only included the
1010 : : identities that I could ever construct testcases to trigger. */
1011 : :
1012 : : static void
1013 : 4538898 : eliminate_using_constants (enum tree_code opcode,
1014 : : vec<operand_entry *> *ops)
1015 : : {
1016 : 4538898 : operand_entry *oelast = ops->last ();
1017 : 4538898 : tree type = TREE_TYPE (oelast->op);
1018 : :
1019 : 4538898 : if (oelast->rank == 0
1020 : 4538898 : && (ANY_INTEGRAL_TYPE_P (type) || FLOAT_TYPE_P (type)))
1021 : : {
1022 : 3286883 : switch (opcode)
1023 : : {
1024 : 460945 : case BIT_AND_EXPR:
1025 : 460945 : if (integer_zerop (oelast->op))
1026 : : {
1027 : 0 : if (ops->length () != 1)
1028 : : {
1029 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1030 : 0 : fprintf (dump_file, "Found & 0, removing all other ops\n");
1031 : :
1032 : 0 : reassociate_stats.ops_eliminated += ops->length () - 1;
1033 : :
1034 : 0 : ops->truncate (0);
1035 : 0 : ops->quick_push (oelast);
1036 : 1826 : return;
1037 : : }
1038 : : }
1039 : 460945 : else if (integer_all_onesp (oelast->op))
1040 : : {
1041 : 4 : if (ops->length () != 1)
1042 : : {
1043 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
1044 : 0 : fprintf (dump_file, "Found & -1, removing\n");
1045 : 4 : ops->pop ();
1046 : 4 : reassociate_stats.ops_eliminated++;
1047 : : }
1048 : : }
1049 : : break;
1050 : 76924 : case BIT_IOR_EXPR:
1051 : 76924 : if (integer_all_onesp (oelast->op))
1052 : : {
1053 : 0 : if (ops->length () != 1)
1054 : : {
1055 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1056 : 0 : fprintf (dump_file, "Found | -1, removing all other ops\n");
1057 : :
1058 : 0 : reassociate_stats.ops_eliminated += ops->length () - 1;
1059 : :
1060 : 0 : ops->truncate (0);
1061 : 0 : ops->quick_push (oelast);
1062 : 0 : return;
1063 : : }
1064 : : }
1065 : 76924 : else if (integer_zerop (oelast->op))
1066 : : {
1067 : 6 : if (ops->length () != 1)
1068 : : {
1069 : 6 : if (dump_file && (dump_flags & TDF_DETAILS))
1070 : 0 : fprintf (dump_file, "Found | 0, removing\n");
1071 : 6 : ops->pop ();
1072 : 6 : reassociate_stats.ops_eliminated++;
1073 : : }
1074 : : }
1075 : : break;
1076 : 909115 : case MULT_EXPR:
1077 : 909115 : if (integer_zerop (oelast->op)
1078 : 909115 : || (FLOAT_TYPE_P (type)
1079 : 1369 : && !HONOR_NANS (type)
1080 : 1274 : && !HONOR_SIGNED_ZEROS (type)
1081 : 1274 : && real_zerop (oelast->op)))
1082 : : {
1083 : 0 : if (ops->length () != 1)
1084 : : {
1085 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1086 : 0 : fprintf (dump_file, "Found * 0, removing all other ops\n");
1087 : :
1088 : 0 : reassociate_stats.ops_eliminated += ops->length () - 1;
1089 : 0 : ops->truncate (0);
1090 : 0 : ops->quick_push (oelast);
1091 : 0 : return;
1092 : : }
1093 : : }
1094 : 909115 : else if (integer_onep (oelast->op)
1095 : 909115 : || (FLOAT_TYPE_P (type)
1096 : 1369 : && !HONOR_SNANS (type)
1097 : 1369 : && real_onep (oelast->op)))
1098 : : {
1099 : 4 : if (ops->length () != 1)
1100 : : {
1101 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
1102 : 0 : fprintf (dump_file, "Found * 1, removing\n");
1103 : 4 : ops->pop ();
1104 : 4 : reassociate_stats.ops_eliminated++;
1105 : 4 : return;
1106 : : }
1107 : : }
1108 : : break;
1109 : 1734048 : case BIT_XOR_EXPR:
1110 : 1734048 : case PLUS_EXPR:
1111 : 1734048 : case MINUS_EXPR:
1112 : 1734048 : if (integer_zerop (oelast->op)
1113 : 1734048 : || (FLOAT_TYPE_P (type)
1114 : 687 : && (opcode == PLUS_EXPR || opcode == MINUS_EXPR)
1115 : 687 : && fold_real_zero_addition_p (type, 0, oelast->op,
1116 : : opcode == MINUS_EXPR)))
1117 : : {
1118 : 1822 : if (ops->length () != 1)
1119 : : {
1120 : 1822 : if (dump_file && (dump_flags & TDF_DETAILS))
1121 : 0 : fprintf (dump_file, "Found [|^+] 0, removing\n");
1122 : 1822 : ops->pop ();
1123 : 1822 : reassociate_stats.ops_eliminated++;
1124 : 1822 : return;
1125 : : }
1126 : : }
1127 : : break;
1128 : : default:
1129 : : break;
1130 : : }
1131 : : }
1132 : : }
1133 : :
1134 : :
1135 : : static void linearize_expr_tree (vec<operand_entry *> *, gimple *,
1136 : : bool, bool);
1137 : :
1138 : : /* Structure for tracking and counting operands. */
1139 : : struct oecount {
1140 : : unsigned int cnt;
1141 : : unsigned int id;
1142 : : enum tree_code oecode;
1143 : : tree op;
1144 : : };
1145 : :
1146 : :
1147 : : /* The heap for the oecount hashtable and the sorted list of operands. */
1148 : : static vec<oecount> cvec;
1149 : :
1150 : :
1151 : : /* Oecount hashtable helpers. */
1152 : :
1153 : : struct oecount_hasher : int_hash <int, 0, 1>
1154 : : {
1155 : : static inline hashval_t hash (int);
1156 : : static inline bool equal (int, int);
1157 : : };
1158 : :
1159 : : /* Hash function for oecount. */
1160 : :
1161 : : inline hashval_t
1162 : 152145 : oecount_hasher::hash (int p)
1163 : : {
1164 : 152145 : const oecount *c = &cvec[p - 42];
1165 : 152145 : return htab_hash_pointer (c->op) ^ (hashval_t)c->oecode;
1166 : : }
1167 : :
1168 : : /* Comparison function for oecount. */
1169 : :
1170 : : inline bool
1171 : 80899 : oecount_hasher::equal (int p1, int p2)
1172 : : {
1173 : 80899 : const oecount *c1 = &cvec[p1 - 42];
1174 : 80899 : const oecount *c2 = &cvec[p2 - 42];
1175 : 80899 : return c1->oecode == c2->oecode && c1->op == c2->op;
1176 : : }
1177 : :
1178 : : /* Comparison function for qsort sorting oecount elements by count. */
1179 : :
1180 : : static int
1181 : 603885 : oecount_cmp (const void *p1, const void *p2)
1182 : : {
1183 : 603885 : const oecount *c1 = (const oecount *)p1;
1184 : 603885 : const oecount *c2 = (const oecount *)p2;
1185 : 603885 : if (c1->cnt != c2->cnt)
1186 : 12213 : return c1->cnt > c2->cnt ? 1 : -1;
1187 : : else
1188 : : /* If counts are identical, use unique IDs to stabilize qsort. */
1189 : 872955 : return c1->id > c2->id ? 1 : -1;
1190 : : }
1191 : :
1192 : : /* Return TRUE iff STMT represents a builtin call that raises OP
1193 : : to some exponent. */
1194 : :
1195 : : static bool
1196 : 851 : stmt_is_power_of_op (gimple *stmt, tree op)
1197 : : {
1198 : 851 : if (!is_gimple_call (stmt))
1199 : : return false;
1200 : :
1201 : 11 : switch (gimple_call_combined_fn (stmt))
1202 : : {
1203 : 6 : CASE_CFN_POW:
1204 : 6 : CASE_CFN_POWI:
1205 : 6 : return (operand_equal_p (gimple_call_arg (stmt, 0), op, 0));
1206 : :
1207 : : default:
1208 : : return false;
1209 : : }
1210 : : }
1211 : :
1212 : : /* Given STMT which is a __builtin_pow* call, decrement its exponent
1213 : : in place and return the result. Assumes that stmt_is_power_of_op
1214 : : was previously called for STMT and returned TRUE. */
1215 : :
1216 : : static HOST_WIDE_INT
1217 : 6 : decrement_power (gimple *stmt)
1218 : : {
1219 : 6 : REAL_VALUE_TYPE c, cint;
1220 : 6 : HOST_WIDE_INT power;
1221 : 6 : tree arg1;
1222 : :
1223 : 6 : switch (gimple_call_combined_fn (stmt))
1224 : : {
1225 : 0 : CASE_CFN_POW:
1226 : 0 : arg1 = gimple_call_arg (stmt, 1);
1227 : 0 : c = TREE_REAL_CST (arg1);
1228 : 0 : power = real_to_integer (&c) - 1;
1229 : 0 : real_from_integer (&cint, VOIDmode, power, SIGNED);
1230 : 0 : gimple_call_set_arg (stmt, 1, build_real (TREE_TYPE (arg1), cint));
1231 : 0 : return power;
1232 : :
1233 : 6 : CASE_CFN_POWI:
1234 : 6 : arg1 = gimple_call_arg (stmt, 1);
1235 : 6 : power = TREE_INT_CST_LOW (arg1) - 1;
1236 : 6 : gimple_call_set_arg (stmt, 1, build_int_cst (TREE_TYPE (arg1), power));
1237 : 6 : return power;
1238 : :
1239 : 0 : default:
1240 : 0 : gcc_unreachable ();
1241 : : }
1242 : : }
1243 : :
1244 : : /* Replace SSA defined by STMT and replace all its uses with new
1245 : : SSA. Also return the new SSA. */
1246 : :
1247 : : static tree
1248 : 233 : make_new_ssa_for_def (gimple *stmt, enum tree_code opcode, tree op)
1249 : : {
1250 : 233 : gimple *use_stmt;
1251 : 233 : use_operand_p use;
1252 : 233 : imm_use_iterator iter;
1253 : 233 : tree new_lhs, new_debug_lhs = NULL_TREE;
1254 : 233 : tree lhs = gimple_get_lhs (stmt);
1255 : :
1256 : 233 : new_lhs = make_ssa_name (TREE_TYPE (lhs));
1257 : 233 : gimple_set_lhs (stmt, new_lhs);
1258 : :
1259 : : /* Also need to update GIMPLE_DEBUGs. */
1260 : 478 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
1261 : : {
1262 : 245 : tree repl = new_lhs;
1263 : 245 : if (is_gimple_debug (use_stmt))
1264 : : {
1265 : 12 : if (new_debug_lhs == NULL_TREE)
1266 : : {
1267 : 6 : new_debug_lhs = build_debug_expr_decl (TREE_TYPE (lhs));
1268 : 6 : gdebug *def_temp
1269 : 6 : = gimple_build_debug_bind (new_debug_lhs,
1270 : 6 : build2 (opcode, TREE_TYPE (lhs),
1271 : : new_lhs, op),
1272 : : stmt);
1273 : 6 : gimple_set_uid (def_temp, gimple_uid (stmt));
1274 : 6 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1275 : 6 : gsi_insert_after (&gsi, def_temp, GSI_SAME_STMT);
1276 : : }
1277 : : repl = new_debug_lhs;
1278 : : }
1279 : 735 : FOR_EACH_IMM_USE_ON_STMT (use, iter)
1280 : 245 : SET_USE (use, repl);
1281 : 245 : update_stmt (use_stmt);
1282 : 233 : }
1283 : 233 : return new_lhs;
1284 : : }
1285 : :
1286 : : /* Replace all SSAs defined in STMTS_TO_FIX and replace its
1287 : : uses with new SSAs. Also do this for the stmt that defines DEF
1288 : : if *DEF is not OP. */
1289 : :
1290 : : static void
1291 : 166 : make_new_ssa_for_all_defs (tree *def, enum tree_code opcode, tree op,
1292 : : vec<gimple *> &stmts_to_fix)
1293 : : {
1294 : 166 : unsigned i;
1295 : 166 : gimple *stmt;
1296 : :
1297 : 166 : if (*def != op
1298 : 166 : && TREE_CODE (*def) == SSA_NAME
1299 : 166 : && (stmt = SSA_NAME_DEF_STMT (*def))
1300 : 332 : && gimple_code (stmt) != GIMPLE_NOP)
1301 : 166 : *def = make_new_ssa_for_def (stmt, opcode, op);
1302 : :
1303 : 233 : FOR_EACH_VEC_ELT (stmts_to_fix, i, stmt)
1304 : 67 : make_new_ssa_for_def (stmt, opcode, op);
1305 : 166 : }
1306 : :
1307 : : /* Find the single immediate use of STMT's LHS, and replace it
1308 : : with OP. Remove STMT. If STMT's LHS is the same as *DEF,
1309 : : replace *DEF with OP as well. */
1310 : :
1311 : : static void
1312 : 594 : propagate_op_to_single_use (tree op, gimple *stmt, tree *def)
1313 : : {
1314 : 594 : tree lhs;
1315 : 594 : gimple *use_stmt;
1316 : 594 : use_operand_p use;
1317 : 594 : gimple_stmt_iterator gsi;
1318 : :
1319 : 594 : if (is_gimple_call (stmt))
1320 : 1 : lhs = gimple_call_lhs (stmt);
1321 : : else
1322 : 593 : lhs = gimple_assign_lhs (stmt);
1323 : :
1324 : 594 : gcc_assert (has_single_use (lhs));
1325 : 594 : single_imm_use (lhs, &use, &use_stmt);
1326 : 594 : if (lhs == *def)
1327 : 437 : *def = op;
1328 : 594 : SET_USE (use, op);
1329 : 594 : if (TREE_CODE (op) != SSA_NAME)
1330 : 39 : update_stmt (use_stmt);
1331 : 594 : gsi = gsi_for_stmt (stmt);
1332 : 594 : unlink_stmt_vdef (stmt);
1333 : 594 : reassoc_remove_stmt (&gsi);
1334 : 594 : release_defs (stmt);
1335 : 594 : }
1336 : :
1337 : : /* Walks the linear chain with result *DEF searching for an operation
1338 : : with operand OP and code OPCODE removing that from the chain. *DEF
1339 : : is updated if there is only one operand but no operation left. */
1340 : :
1341 : : static void
1342 : 603 : zero_one_operation (tree *def, enum tree_code opcode, tree op)
1343 : : {
1344 : 603 : tree orig_def = *def;
1345 : 603 : gimple *stmt = SSA_NAME_DEF_STMT (*def);
1346 : : /* PR72835 - Record the stmt chain that has to be updated such that
1347 : : we dont use the same LHS when the values computed are different. */
1348 : 603 : auto_vec<gimple *, 64> stmts_to_fix;
1349 : :
1350 : 1051 : do
1351 : : {
1352 : 827 : tree name;
1353 : :
1354 : 827 : if (opcode == MULT_EXPR)
1355 : : {
1356 : 825 : if (stmt_is_power_of_op (stmt, op))
1357 : : {
1358 : 6 : if (decrement_power (stmt) == 1)
1359 : : {
1360 : 1 : if (stmts_to_fix.length () > 0)
1361 : 1 : stmts_to_fix.pop ();
1362 : 1 : propagate_op_to_single_use (op, stmt, def);
1363 : : }
1364 : : break;
1365 : : }
1366 : 819 : else if (gimple_assign_rhs_code (stmt) == NEGATE_EXPR)
1367 : : {
1368 : 15 : if (gimple_assign_rhs1 (stmt) == op)
1369 : : {
1370 : 11 : tree cst = build_minus_one_cst (TREE_TYPE (op));
1371 : 11 : if (stmts_to_fix.length () > 0)
1372 : 11 : stmts_to_fix.pop ();
1373 : 11 : propagate_op_to_single_use (cst, stmt, def);
1374 : 11 : break;
1375 : : }
1376 : 4 : else if (integer_minus_onep (op)
1377 : 4 : || real_minus_onep (op))
1378 : : {
1379 : 4 : gimple_assign_set_rhs_code
1380 : 4 : (stmt, TREE_CODE (gimple_assign_rhs1 (stmt)));
1381 : 4 : break;
1382 : : }
1383 : : }
1384 : : }
1385 : :
1386 : 806 : name = gimple_assign_rhs1 (stmt);
1387 : :
1388 : : /* If this is the operation we look for and one of the operands
1389 : : is ours simply propagate the other operand into the stmts
1390 : : single use. */
1391 : 806 : if (gimple_assign_rhs_code (stmt) == opcode
1392 : 806 : && (name == op
1393 : 644 : || gimple_assign_rhs2 (stmt) == op))
1394 : : {
1395 : 582 : if (name == op)
1396 : 162 : name = gimple_assign_rhs2 (stmt);
1397 : 582 : if (stmts_to_fix.length () > 0)
1398 : 145 : stmts_to_fix.pop ();
1399 : 582 : propagate_op_to_single_use (name, stmt, def);
1400 : 582 : break;
1401 : : }
1402 : :
1403 : : /* We might have a multiply of two __builtin_pow* calls, and
1404 : : the operand might be hiding in the rightmost one. Likewise
1405 : : this can happen for a negate. */
1406 : 224 : if (opcode == MULT_EXPR
1407 : 224 : && gimple_assign_rhs_code (stmt) == opcode
1408 : 224 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
1409 : 378 : && has_single_use (gimple_assign_rhs2 (stmt)))
1410 : : {
1411 : 26 : gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
1412 : 26 : if (stmt_is_power_of_op (stmt2, op))
1413 : : {
1414 : 0 : if (decrement_power (stmt2) == 1)
1415 : 0 : propagate_op_to_single_use (op, stmt2, def);
1416 : : else
1417 : 0 : stmts_to_fix.safe_push (stmt2);
1418 : 0 : break;
1419 : : }
1420 : 26 : else if (is_gimple_assign (stmt2)
1421 : 26 : && gimple_assign_rhs_code (stmt2) == NEGATE_EXPR)
1422 : : {
1423 : 0 : if (gimple_assign_rhs1 (stmt2) == op)
1424 : : {
1425 : 0 : tree cst = build_minus_one_cst (TREE_TYPE (op));
1426 : 0 : propagate_op_to_single_use (cst, stmt2, def);
1427 : 0 : break;
1428 : : }
1429 : 0 : else if (integer_minus_onep (op)
1430 : 0 : || real_minus_onep (op))
1431 : : {
1432 : 0 : stmts_to_fix.safe_push (stmt2);
1433 : 0 : gimple_assign_set_rhs_code
1434 : 0 : (stmt2, TREE_CODE (gimple_assign_rhs1 (stmt2)));
1435 : 0 : break;
1436 : : }
1437 : : }
1438 : : }
1439 : :
1440 : : /* Continue walking the chain. */
1441 : 224 : gcc_assert (name != op
1442 : : && TREE_CODE (name) == SSA_NAME);
1443 : 224 : stmt = SSA_NAME_DEF_STMT (name);
1444 : 224 : stmts_to_fix.safe_push (stmt);
1445 : 224 : }
1446 : : while (1);
1447 : :
1448 : 603 : if (stmts_to_fix.length () > 0 || *def == orig_def)
1449 : 166 : make_new_ssa_for_all_defs (def, opcode, op, stmts_to_fix);
1450 : 603 : }
1451 : :
1452 : : /* Returns true if statement S1 dominates statement S2. Like
1453 : : stmt_dominates_stmt_p, but uses stmt UIDs to optimize. */
1454 : :
1455 : : static bool
1456 : 6756792 : reassoc_stmt_dominates_stmt_p (gimple *s1, gimple *s2)
1457 : : {
1458 : 6756792 : basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
1459 : :
1460 : : /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
1461 : : SSA_NAME. Assume it lives at the beginning of function and
1462 : : thus dominates everything. */
1463 : 6756792 : if (!bb1 || s1 == s2)
1464 : : return true;
1465 : :
1466 : : /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
1467 : 6753726 : if (!bb2)
1468 : : return false;
1469 : :
1470 : 6731464 : if (bb1 == bb2)
1471 : : {
1472 : : /* PHIs in the same basic block are assumed to be
1473 : : executed all in parallel, if only one stmt is a PHI,
1474 : : it dominates the other stmt in the same basic block. */
1475 : 6457378 : if (gimple_code (s1) == GIMPLE_PHI)
1476 : : return true;
1477 : :
1478 : 6334984 : if (gimple_code (s2) == GIMPLE_PHI)
1479 : : return false;
1480 : :
1481 : 6252013 : gcc_assert (gimple_uid (s1) && gimple_uid (s2));
1482 : :
1483 : 6252013 : if (gimple_uid (s1) < gimple_uid (s2))
1484 : : return true;
1485 : :
1486 : 3821951 : if (gimple_uid (s1) > gimple_uid (s2))
1487 : : return false;
1488 : :
1489 : 45761 : gimple_stmt_iterator gsi = gsi_for_stmt (s1);
1490 : 45761 : unsigned int uid = gimple_uid (s1);
1491 : 117333 : for (gsi_next (&gsi); !gsi_end_p (gsi); gsi_next (&gsi))
1492 : : {
1493 : 113789 : gimple *s = gsi_stmt (gsi);
1494 : 113789 : if (gimple_uid (s) != uid)
1495 : : break;
1496 : 74973 : if (s == s2)
1497 : : return true;
1498 : : }
1499 : :
1500 : : return false;
1501 : : }
1502 : :
1503 : 274086 : return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
1504 : : }
1505 : :
1506 : : /* Insert STMT after INSERT_POINT. */
1507 : :
1508 : : static void
1509 : 41318 : insert_stmt_after (gimple *stmt, gimple *insert_point)
1510 : : {
1511 : 41318 : gimple_stmt_iterator gsi;
1512 : 41318 : basic_block bb;
1513 : :
1514 : 41318 : if (gimple_code (insert_point) == GIMPLE_PHI)
1515 : 29 : bb = gimple_bb (insert_point);
1516 : 41289 : else if (!stmt_ends_bb_p (insert_point))
1517 : : {
1518 : 41284 : gsi = gsi_for_stmt (insert_point);
1519 : 41284 : gimple_set_uid (stmt, gimple_uid (insert_point));
1520 : 41284 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1521 : 41284 : return;
1522 : : }
1523 : 5 : else if (gimple_code (insert_point) == GIMPLE_ASM
1524 : 5 : && gimple_asm_nlabels (as_a <gasm *> (insert_point)) != 0)
1525 : : /* We have no idea where to insert - it depends on where the
1526 : : uses will be placed. */
1527 : 0 : gcc_unreachable ();
1528 : : else
1529 : : /* We assume INSERT_POINT is a SSA_NAME_DEF_STMT of some SSA_NAME,
1530 : : thus if it must end a basic block, it should be a call that can
1531 : : throw, or some assignment that can throw. If it throws, the LHS
1532 : : of it will not be initialized though, so only valid places using
1533 : : the SSA_NAME should be dominated by the fallthru edge. */
1534 : 5 : bb = find_fallthru_edge (gimple_bb (insert_point)->succs)->dest;
1535 : 34 : gsi = gsi_after_labels (bb);
1536 : 34 : if (gsi_end_p (gsi))
1537 : : {
1538 : 0 : gimple_stmt_iterator gsi2 = gsi_last_bb (bb);
1539 : 0 : gimple_set_uid (stmt,
1540 : 0 : gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
1541 : : }
1542 : : else
1543 : 34 : gimple_set_uid (stmt, gimple_uid (gsi_stmt (gsi)));
1544 : 34 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1545 : : }
1546 : :
1547 : : /* Builds one statement performing OP1 OPCODE OP2 using TMPVAR for
1548 : : the result. Places the statement after the definition of either
1549 : : OP1 or OP2. Returns the new statement. */
1550 : :
1551 : : static gimple *
1552 : 7163 : build_and_add_sum (tree type, tree op1, tree op2, enum tree_code opcode)
1553 : : {
1554 : 7163 : gimple *op1def = NULL, *op2def = NULL;
1555 : 7163 : gimple_stmt_iterator gsi;
1556 : 7163 : tree op;
1557 : 7163 : gassign *sum;
1558 : :
1559 : : /* Create the addition statement. */
1560 : 7163 : op = make_ssa_name (type);
1561 : 7163 : sum = gimple_build_assign (op, opcode, op1, op2);
1562 : :
1563 : : /* Find an insertion place and insert. */
1564 : 7163 : if (TREE_CODE (op1) == SSA_NAME)
1565 : 7163 : op1def = SSA_NAME_DEF_STMT (op1);
1566 : 7163 : if (TREE_CODE (op2) == SSA_NAME)
1567 : 6902 : op2def = SSA_NAME_DEF_STMT (op2);
1568 : 7163 : if ((!op1def || gimple_nop_p (op1def))
1569 : 7256 : && (!op2def || gimple_nop_p (op2def)))
1570 : : {
1571 : 93 : gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1572 : 93 : if (!gsi_end_p (gsi)
1573 : 93 : && is_gimple_call (gsi_stmt (gsi))
1574 : 99 : && (gimple_call_flags (gsi_stmt (gsi)) & ECF_RETURNS_TWICE))
1575 : : {
1576 : : /* Don't add statements before a returns_twice call at the start
1577 : : of a function. */
1578 : 1 : split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1579 : 1 : gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1580 : : }
1581 : 93 : if (gsi_end_p (gsi))
1582 : : {
1583 : 1 : gimple_stmt_iterator gsi2
1584 : 1 : = gsi_last_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1585 : 1 : gimple_set_uid (sum,
1586 : 1 : gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
1587 : : }
1588 : : else
1589 : 92 : gimple_set_uid (sum, gimple_uid (gsi_stmt (gsi)));
1590 : 93 : gsi_insert_before (&gsi, sum, GSI_NEW_STMT);
1591 : : }
1592 : : else
1593 : : {
1594 : 7070 : gimple *insert_point;
1595 : 7070 : if ((!op1def || gimple_nop_p (op1def))
1596 : 14140 : || (op2def && !gimple_nop_p (op2def)
1597 : 6793 : && reassoc_stmt_dominates_stmt_p (op1def, op2def)))
1598 : : insert_point = op2def;
1599 : : else
1600 : : insert_point = op1def;
1601 : 7070 : insert_stmt_after (sum, insert_point);
1602 : : }
1603 : 7163 : update_stmt (sum);
1604 : :
1605 : 7163 : return sum;
1606 : : }
1607 : :
1608 : : /* Perform un-distribution of divisions and multiplications.
1609 : : A * X + B * X is transformed into (A + B) * X and A / X + B / X
1610 : : to (A + B) / X for real X.
1611 : :
1612 : : The algorithm is organized as follows.
1613 : :
1614 : : - First we walk the addition chain *OPS looking for summands that
1615 : : are defined by a multiplication or a real division. This results
1616 : : in the candidates bitmap with relevant indices into *OPS.
1617 : :
1618 : : - Second we build the chains of multiplications or divisions for
1619 : : these candidates, counting the number of occurrences of (operand, code)
1620 : : pairs in all of the candidates chains.
1621 : :
1622 : : - Third we sort the (operand, code) pairs by number of occurrence and
1623 : : process them starting with the pair with the most uses.
1624 : :
1625 : : * For each such pair we walk the candidates again to build a
1626 : : second candidate bitmap noting all multiplication/division chains
1627 : : that have at least one occurrence of (operand, code).
1628 : :
1629 : : * We build an alternate addition chain only covering these
1630 : : candidates with one (operand, code) operation removed from their
1631 : : multiplication/division chain.
1632 : :
1633 : : * The first candidate gets replaced by the alternate addition chain
1634 : : multiplied/divided by the operand.
1635 : :
1636 : : * All candidate chains get disabled for further processing and
1637 : : processing of (operand, code) pairs continues.
1638 : :
1639 : : The alternate addition chains built are re-processed by the main
1640 : : reassociation algorithm which allows optimizing a * x * y + b * y * x
1641 : : to (a + b ) * x * y in one invocation of the reassociation pass. */
1642 : :
1643 : : static bool
1644 : 4536771 : undistribute_ops_list (enum tree_code opcode,
1645 : : vec<operand_entry *> *ops, class loop *loop)
1646 : : {
1647 : 4536771 : unsigned int length = ops->length ();
1648 : 4536771 : operand_entry *oe1;
1649 : 4536771 : unsigned i, j;
1650 : 4536771 : unsigned nr_candidates, nr_candidates2;
1651 : 4536771 : sbitmap_iterator sbi0;
1652 : 4536771 : vec<operand_entry *> *subops;
1653 : 4536771 : bool changed = false;
1654 : 4536771 : unsigned int next_oecount_id = 0;
1655 : :
1656 : 4536771 : if (length <= 1
1657 : 4536771 : || opcode != PLUS_EXPR)
1658 : : return false;
1659 : :
1660 : : /* Build a list of candidates to process. */
1661 : 2233944 : auto_sbitmap candidates (length);
1662 : 2233944 : bitmap_clear (candidates);
1663 : 2233944 : nr_candidates = 0;
1664 : 6893489 : FOR_EACH_VEC_ELT (*ops, i, oe1)
1665 : : {
1666 : 4659545 : enum tree_code dcode;
1667 : 4659545 : gimple *oe1def;
1668 : :
1669 : 4659545 : if (TREE_CODE (oe1->op) != SSA_NAME)
1670 : 1721854 : continue;
1671 : 2937691 : oe1def = SSA_NAME_DEF_STMT (oe1->op);
1672 : 2937691 : if (!is_gimple_assign (oe1def))
1673 : 1063941 : continue;
1674 : 1873750 : dcode = gimple_assign_rhs_code (oe1def);
1675 : 3508563 : if ((dcode != MULT_EXPR
1676 : 1873750 : && dcode != RDIV_EXPR)
1677 : 1873750 : || !is_reassociable_op (oe1def, dcode, loop))
1678 : 1634813 : continue;
1679 : :
1680 : 238937 : bitmap_set_bit (candidates, i);
1681 : 238937 : nr_candidates++;
1682 : : }
1683 : :
1684 : 2233944 : if (nr_candidates < 2)
1685 : : return false;
1686 : :
1687 : 16247 : if (dump_file && (dump_flags & TDF_DETAILS))
1688 : : {
1689 : 1 : fprintf (dump_file, "searching for un-distribute opportunities ");
1690 : 2 : print_generic_expr (dump_file,
1691 : 1 : (*ops)[bitmap_first_set_bit (candidates)]->op, TDF_NONE);
1692 : 1 : fprintf (dump_file, " %d\n", nr_candidates);
1693 : : }
1694 : :
1695 : : /* Build linearized sub-operand lists and the counting table. */
1696 : 16247 : cvec.create (0);
1697 : :
1698 : 16247 : hash_table<oecount_hasher> ctable (15);
1699 : :
1700 : : /* ??? Macro arguments cannot have multi-argument template types in
1701 : : them. This typedef is needed to workaround that limitation. */
1702 : 16247 : typedef vec<operand_entry *> vec_operand_entry_t_heap;
1703 : 32494 : subops = XCNEWVEC (vec_operand_entry_t_heap, ops->length ());
1704 : 72415 : EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
1705 : : {
1706 : 39921 : gimple *oedef;
1707 : 39921 : enum tree_code oecode;
1708 : 39921 : unsigned j;
1709 : :
1710 : 39921 : oedef = SSA_NAME_DEF_STMT ((*ops)[i]->op);
1711 : 39921 : oecode = gimple_assign_rhs_code (oedef);
1712 : 79842 : linearize_expr_tree (&subops[i], oedef,
1713 : 39921 : associative_tree_code (oecode), false);
1714 : :
1715 : 160351 : FOR_EACH_VEC_ELT (subops[i], j, oe1)
1716 : : {
1717 : 80509 : oecount c;
1718 : 80509 : int *slot;
1719 : 80509 : int idx;
1720 : 80509 : c.oecode = oecode;
1721 : 80509 : c.cnt = 1;
1722 : 80509 : c.id = next_oecount_id++;
1723 : 80509 : c.op = oe1->op;
1724 : 80509 : cvec.safe_push (c);
1725 : 80509 : idx = cvec.length () + 41;
1726 : 80509 : slot = ctable.find_slot (idx, INSERT);
1727 : 80509 : if (!*slot)
1728 : : {
1729 : 79639 : *slot = idx;
1730 : : }
1731 : : else
1732 : : {
1733 : 870 : cvec.pop ();
1734 : 870 : cvec[*slot - 42].cnt++;
1735 : : }
1736 : : }
1737 : : }
1738 : :
1739 : : /* Sort the counting table. */
1740 : 16247 : cvec.qsort (oecount_cmp);
1741 : :
1742 : 16247 : if (dump_file && (dump_flags & TDF_DETAILS))
1743 : : {
1744 : 1 : oecount *c;
1745 : 1 : fprintf (dump_file, "Candidates:\n");
1746 : 5 : FOR_EACH_VEC_ELT (cvec, j, c)
1747 : : {
1748 : 3 : fprintf (dump_file, " %u %s: ", c->cnt,
1749 : 3 : c->oecode == MULT_EXPR
1750 : : ? "*" : c->oecode == RDIV_EXPR ? "/" : "?");
1751 : 3 : print_generic_expr (dump_file, c->op);
1752 : 3 : fprintf (dump_file, "\n");
1753 : : }
1754 : : }
1755 : :
1756 : : /* Process the (operand, code) pairs in order of most occurrence. */
1757 : 16247 : auto_sbitmap candidates2 (length);
1758 : 16811 : while (!cvec.is_empty ())
1759 : : {
1760 : 16743 : oecount *c = &cvec.last ();
1761 : 16743 : if (c->cnt < 2)
1762 : : break;
1763 : :
1764 : : /* Now collect the operands in the outer chain that contain
1765 : : the common operand in their inner chain. */
1766 : 564 : bitmap_clear (candidates2);
1767 : 564 : nr_candidates2 = 0;
1768 : 4163 : EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
1769 : : {
1770 : 3035 : gimple *oedef;
1771 : 3035 : enum tree_code oecode;
1772 : 3035 : unsigned j;
1773 : 3035 : tree op = (*ops)[i]->op;
1774 : :
1775 : : /* If we undistributed in this chain already this may be
1776 : : a constant. */
1777 : 3035 : if (TREE_CODE (op) != SSA_NAME)
1778 : 766 : continue;
1779 : :
1780 : 2269 : oedef = SSA_NAME_DEF_STMT (op);
1781 : 2269 : oecode = gimple_assign_rhs_code (oedef);
1782 : 2269 : if (oecode != c->oecode)
1783 : 0 : continue;
1784 : :
1785 : 8615 : FOR_EACH_VEC_ELT (subops[i], j, oe1)
1786 : : {
1787 : 4242 : if (oe1->op == c->op)
1788 : : {
1789 : 931 : bitmap_set_bit (candidates2, i);
1790 : 931 : ++nr_candidates2;
1791 : 931 : break;
1792 : : }
1793 : : }
1794 : : }
1795 : :
1796 : 564 : if (nr_candidates2 >= 2)
1797 : : {
1798 : 191 : operand_entry *oe1, *oe2;
1799 : 191 : gimple *prod;
1800 : 191 : int first = bitmap_first_set_bit (candidates2);
1801 : :
1802 : : /* Build the new addition chain. */
1803 : 191 : oe1 = (*ops)[first];
1804 : 191 : if (dump_file && (dump_flags & TDF_DETAILS))
1805 : : {
1806 : 0 : fprintf (dump_file, "Building (");
1807 : 0 : print_generic_expr (dump_file, oe1->op);
1808 : : }
1809 : 191 : zero_one_operation (&oe1->op, c->oecode, c->op);
1810 : 603 : EXECUTE_IF_SET_IN_BITMAP (candidates2, first+1, i, sbi0)
1811 : : {
1812 : 412 : gimple *sum;
1813 : 412 : oe2 = (*ops)[i];
1814 : 412 : if (dump_file && (dump_flags & TDF_DETAILS))
1815 : : {
1816 : 0 : fprintf (dump_file, " + ");
1817 : 0 : print_generic_expr (dump_file, oe2->op);
1818 : : }
1819 : 412 : zero_one_operation (&oe2->op, c->oecode, c->op);
1820 : 412 : sum = build_and_add_sum (TREE_TYPE (oe1->op),
1821 : : oe1->op, oe2->op, opcode);
1822 : 412 : oe2->op = build_zero_cst (TREE_TYPE (oe2->op));
1823 : 412 : oe2->rank = 0;
1824 : 412 : oe1->op = gimple_get_lhs (sum);
1825 : : }
1826 : :
1827 : : /* Apply the multiplication/division. */
1828 : 191 : prod = build_and_add_sum (TREE_TYPE (oe1->op),
1829 : : oe1->op, c->op, c->oecode);
1830 : 191 : if (dump_file && (dump_flags & TDF_DETAILS))
1831 : : {
1832 : 0 : fprintf (dump_file, ") %s ", c->oecode == MULT_EXPR ? "*" : "/");
1833 : 0 : print_generic_expr (dump_file, c->op);
1834 : 0 : fprintf (dump_file, "\n");
1835 : : }
1836 : :
1837 : : /* Record it in the addition chain and disable further
1838 : : undistribution with this op. */
1839 : 191 : oe1->op = gimple_assign_lhs (prod);
1840 : 191 : oe1->rank = get_rank (oe1->op);
1841 : 191 : subops[first].release ();
1842 : :
1843 : 191 : changed = true;
1844 : : }
1845 : :
1846 : 564 : cvec.pop ();
1847 : : }
1848 : :
1849 : 71630 : for (i = 0; i < ops->length (); ++i)
1850 : 55383 : subops[i].release ();
1851 : 16247 : free (subops);
1852 : 16247 : cvec.release ();
1853 : :
1854 : 16247 : return changed;
1855 : 2233944 : }
1856 : :
1857 : : /* Pair to hold the information of one specific VECTOR_TYPE SSA_NAME:
1858 : : first: element index for each relevant BIT_FIELD_REF.
1859 : : second: the index of vec ops* for each relevant BIT_FIELD_REF. */
1860 : : typedef std::pair<unsigned, unsigned> v_info_elem;
1861 : 6584 : struct v_info {
1862 : : tree vec_type;
1863 : : auto_vec<v_info_elem, 32> vec;
1864 : : };
1865 : : typedef v_info *v_info_ptr;
1866 : :
1867 : : /* Comparison function for qsort on VECTOR SSA_NAME trees by machine mode. */
1868 : : static int
1869 : 10568 : sort_by_mach_mode (const void *p_i, const void *p_j)
1870 : : {
1871 : 10568 : const tree tr1 = *((const tree *) p_i);
1872 : 10568 : const tree tr2 = *((const tree *) p_j);
1873 : 10568 : unsigned int mode1 = TYPE_MODE (TREE_TYPE (tr1));
1874 : 10568 : unsigned int mode2 = TYPE_MODE (TREE_TYPE (tr2));
1875 : 10568 : if (mode1 > mode2)
1876 : : return 1;
1877 : 10529 : else if (mode1 < mode2)
1878 : : return -1;
1879 : 10484 : if (SSA_NAME_VERSION (tr1) < SSA_NAME_VERSION (tr2))
1880 : : return -1;
1881 : 5111 : else if (SSA_NAME_VERSION (tr1) > SSA_NAME_VERSION (tr2))
1882 : 5111 : return 1;
1883 : : return 0;
1884 : : }
1885 : :
1886 : : /* Cleanup hash map for VECTOR information. */
1887 : : static void
1888 : 4381655 : cleanup_vinfo_map (hash_map<tree, v_info_ptr> &info_map)
1889 : : {
1890 : 4388239 : for (hash_map<tree, v_info_ptr>::iterator it = info_map.begin ();
1891 : 4394823 : it != info_map.end (); ++it)
1892 : : {
1893 : 6584 : v_info_ptr info = (*it).second;
1894 : 6584 : delete info;
1895 : 6584 : (*it).second = NULL;
1896 : : }
1897 : 4381655 : }
1898 : :
1899 : : /* Perform un-distribution of BIT_FIELD_REF on VECTOR_TYPE.
1900 : : V1[0] + V1[1] + ... + V1[k] + V2[0] + V2[1] + ... + V2[k] + ... Vn[k]
1901 : : is transformed to
1902 : : Vs = (V1 + V2 + ... + Vn)
1903 : : Vs[0] + Vs[1] + ... + Vs[k]
1904 : :
1905 : : The basic steps are listed below:
1906 : :
1907 : : 1) Check the addition chain *OPS by looking those summands coming from
1908 : : VECTOR bit_field_ref on VECTOR type. Put the information into
1909 : : v_info_map for each satisfied summand, using VECTOR SSA_NAME as key.
1910 : :
1911 : : 2) For each key (VECTOR SSA_NAME), validate all its BIT_FIELD_REFs are
1912 : : continuous, they can cover the whole VECTOR perfectly without any holes.
1913 : : Obtain one VECTOR list which contain candidates to be transformed.
1914 : :
1915 : : 3) Sort the VECTOR list by machine mode of VECTOR type, for each group of
1916 : : candidates with same mode, build the addition statements for them and
1917 : : generate BIT_FIELD_REFs accordingly.
1918 : :
1919 : : TODO:
1920 : : The current implementation requires the whole VECTORs should be fully
1921 : : covered, but it can be extended to support partial, checking adjacent
1922 : : but not fill the whole, it may need some cost model to define the
1923 : : boundary to do or not.
1924 : : */
1925 : : static bool
1926 : 4536771 : undistribute_bitref_for_vector (enum tree_code opcode,
1927 : : vec<operand_entry *> *ops, struct loop *loop)
1928 : : {
1929 : 4536771 : if (ops->length () <= 1)
1930 : : return false;
1931 : :
1932 : 4533128 : if (opcode != PLUS_EXPR
1933 : 4533128 : && opcode != MULT_EXPR
1934 : : && opcode != BIT_XOR_EXPR
1935 : 1231787 : && opcode != BIT_IOR_EXPR
1936 : 835871 : && opcode != BIT_AND_EXPR)
1937 : : return false;
1938 : :
1939 : 4381655 : hash_map<tree, v_info_ptr> v_info_map;
1940 : 4381655 : operand_entry *oe1;
1941 : 4381655 : unsigned i;
1942 : :
1943 : : /* Find those summands from VECTOR BIT_FIELD_REF in addition chain, put the
1944 : : information into map. */
1945 : 13414671 : FOR_EACH_VEC_ELT (*ops, i, oe1)
1946 : : {
1947 : 9033016 : enum tree_code dcode;
1948 : 9033016 : gimple *oe1def;
1949 : :
1950 : 9033016 : if (TREE_CODE (oe1->op) != SSA_NAME)
1951 : 3179536 : continue;
1952 : 5853480 : oe1def = SSA_NAME_DEF_STMT (oe1->op);
1953 : 5853480 : if (!is_gimple_assign (oe1def))
1954 : 1481005 : continue;
1955 : 4372475 : dcode = gimple_assign_rhs_code (oe1def);
1956 : 4372475 : if (dcode != BIT_FIELD_REF || !is_reassociable_op (oe1def, dcode, loop))
1957 : 4294977 : continue;
1958 : :
1959 : 77498 : tree rhs = gimple_assign_rhs1 (oe1def);
1960 : 77498 : tree vec = TREE_OPERAND (rhs, 0);
1961 : 77498 : tree vec_type = TREE_TYPE (vec);
1962 : :
1963 : 77498 : if (TREE_CODE (vec) != SSA_NAME || !VECTOR_TYPE_P (vec_type))
1964 : 58236 : continue;
1965 : :
1966 : : /* Ignore it if target machine can't support this VECTOR type. */
1967 : 19262 : if (!VECTOR_MODE_P (TYPE_MODE (vec_type)))
1968 : 5346 : continue;
1969 : :
1970 : : /* Check const vector type, constrain BIT_FIELD_REF offset and size. */
1971 : 13916 : if (!TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
1972 : : continue;
1973 : :
1974 : 13916 : if (VECTOR_TYPE_P (TREE_TYPE (rhs))
1975 : 13916 : || !is_a <scalar_mode> (TYPE_MODE (TREE_TYPE (rhs))))
1976 : 5347 : continue;
1977 : :
1978 : : /* The type of BIT_FIELD_REF might not be equal to the element type of
1979 : : the vector. We want to use a vector type with element type the
1980 : : same as the BIT_FIELD_REF and size the same as TREE_TYPE (vec). */
1981 : 8569 : if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (vec_type)))
1982 : : {
1983 : 1329 : machine_mode simd_mode;
1984 : 1329 : unsigned HOST_WIDE_INT size, nunits;
1985 : 1329 : unsigned HOST_WIDE_INT elem_size
1986 : 1329 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
1987 : 2658 : if (!GET_MODE_BITSIZE (TYPE_MODE (vec_type)).is_constant (&size))
1988 : 9024895 : continue;
1989 : 1329 : if (size <= elem_size || (size % elem_size) != 0)
1990 : 0 : continue;
1991 : 1329 : nunits = size / elem_size;
1992 : 1329 : if (!mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (rhs)),
1993 : 1329 : nunits).exists (&simd_mode))
1994 : 0 : continue;
1995 : 1329 : vec_type = build_vector_type_for_mode (TREE_TYPE (rhs), simd_mode);
1996 : :
1997 : : /* Ignore it if target machine can't support this VECTOR type. */
1998 : 1329 : if (!VECTOR_MODE_P (TYPE_MODE (vec_type)))
1999 : 0 : continue;
2000 : :
2001 : : /* Check const vector type, constrain BIT_FIELD_REF offset and
2002 : : size. */
2003 : 1329 : if (!TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
2004 : : continue;
2005 : :
2006 : 2658 : if (maybe_ne (GET_MODE_SIZE (TYPE_MODE (vec_type)),
2007 : 2658 : GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (vec)))))
2008 : 0 : continue;
2009 : : }
2010 : :
2011 : 8569 : tree elem_type = TREE_TYPE (vec_type);
2012 : 8569 : unsigned HOST_WIDE_INT elem_size = tree_to_uhwi (TYPE_SIZE (elem_type));
2013 : 8569 : if (maybe_ne (bit_field_size (rhs), elem_size))
2014 : 0 : continue;
2015 : :
2016 : 8569 : unsigned idx;
2017 : 8569 : if (!constant_multiple_p (bit_field_offset (rhs), elem_size, &idx))
2018 : 0 : continue;
2019 : :
2020 : : /* Ignore it if target machine can't support this type of VECTOR
2021 : : operation. */
2022 : 8569 : optab op_tab = optab_for_tree_code (opcode, vec_type, optab_vector);
2023 : 8569 : if (optab_handler (op_tab, TYPE_MODE (vec_type)) == CODE_FOR_nothing)
2024 : 448 : continue;
2025 : :
2026 : 8121 : bool existed;
2027 : 8121 : v_info_ptr &info = v_info_map.get_or_insert (vec, &existed);
2028 : 8121 : if (!existed)
2029 : : {
2030 : 6584 : info = new v_info;
2031 : 6584 : info->vec_type = vec_type;
2032 : : }
2033 : 1537 : else if (!types_compatible_p (vec_type, info->vec_type))
2034 : 0 : continue;
2035 : 8121 : info->vec.safe_push (std::make_pair (idx, i));
2036 : : }
2037 : :
2038 : : /* At least two VECTOR to combine. */
2039 : 4381655 : if (v_info_map.elements () <= 1)
2040 : : {
2041 : 4381534 : cleanup_vinfo_map (v_info_map);
2042 : 4381534 : return false;
2043 : : }
2044 : :
2045 : : /* Verify all VECTOR candidates by checking two conditions:
2046 : : 1) sorted offsets are adjacent, no holes.
2047 : : 2) can fill the whole VECTOR perfectly.
2048 : : And add the valid candidates to a vector for further handling. */
2049 : 121 : auto_vec<tree> valid_vecs (v_info_map.elements ());
2050 : 671 : for (hash_map<tree, v_info_ptr>::iterator it = v_info_map.begin ();
2051 : 1221 : it != v_info_map.end (); ++it)
2052 : : {
2053 : 550 : tree cand_vec = (*it).first;
2054 : 550 : v_info_ptr cand_info = (*it).second;
2055 : 550 : unsigned int num_elems
2056 : 550 : = TYPE_VECTOR_SUBPARTS (cand_info->vec_type).to_constant ();
2057 : 1100 : if (cand_info->vec.length () != num_elems)
2058 : 175 : continue;
2059 : 375 : sbitmap holes = sbitmap_alloc (num_elems);
2060 : 375 : bitmap_ones (holes);
2061 : 375 : bool valid = true;
2062 : 375 : v_info_elem *curr;
2063 : 2208 : FOR_EACH_VEC_ELT (cand_info->vec, i, curr)
2064 : : {
2065 : 1458 : if (!bitmap_bit_p (holes, curr->first))
2066 : : {
2067 : : valid = false;
2068 : : break;
2069 : : }
2070 : : else
2071 : 1458 : bitmap_clear_bit (holes, curr->first);
2072 : : }
2073 : 375 : if (valid && bitmap_empty_p (holes))
2074 : 375 : valid_vecs.quick_push (cand_vec);
2075 : 375 : sbitmap_free (holes);
2076 : : }
2077 : :
2078 : : /* At least two VECTOR to combine. */
2079 : 121 : if (valid_vecs.length () <= 1)
2080 : : {
2081 : 81 : cleanup_vinfo_map (v_info_map);
2082 : 81 : return false;
2083 : : }
2084 : :
2085 : 40 : valid_vecs.qsort (sort_by_mach_mode);
2086 : : /* Go through all candidates by machine mode order, query the mode_to_total
2087 : : to get the total number for each mode and skip the single one. */
2088 : 86 : for (unsigned i = 0; i < valid_vecs.length () - 1; ++i)
2089 : : {
2090 : 46 : tree tvec = valid_vecs[i];
2091 : 46 : enum machine_mode mode = TYPE_MODE (TREE_TYPE (tvec));
2092 : :
2093 : : /* Skip modes with only a single candidate. */
2094 : 46 : if (TYPE_MODE (TREE_TYPE (valid_vecs[i + 1])) != mode)
2095 : 4 : continue;
2096 : :
2097 : 42 : unsigned int idx, j;
2098 : 42 : gimple *sum = NULL;
2099 : 42 : tree sum_vec = tvec;
2100 : 42 : v_info_ptr info_ptr = *(v_info_map.get (tvec));
2101 : 42 : v_info_elem *elem;
2102 : 42 : tree vec_type = info_ptr->vec_type;
2103 : :
2104 : : /* Build the sum for all candidates with same mode. */
2105 : 325 : do
2106 : : {
2107 : 975 : sum = build_and_add_sum (vec_type, sum_vec,
2108 : 325 : valid_vecs[i + 1], opcode);
2109 : : /* Update the operands only after build_and_add_sum,
2110 : : so that we don't have to repeat the placement algorithm
2111 : : of build_and_add_sum. */
2112 : 325 : if (sum_vec == tvec
2113 : 325 : && !useless_type_conversion_p (vec_type, TREE_TYPE (sum_vec)))
2114 : : {
2115 : 18 : gimple_stmt_iterator gsi = gsi_for_stmt (sum);
2116 : 18 : tree vce = build1 (VIEW_CONVERT_EXPR, vec_type, sum_vec);
2117 : 18 : tree lhs = make_ssa_name (vec_type);
2118 : 18 : gimple *g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, vce);
2119 : 18 : gimple_set_uid (g, gimple_uid (sum));
2120 : 18 : gsi_insert_before (&gsi, g, GSI_NEW_STMT);
2121 : 18 : gimple_assign_set_rhs1 (sum, lhs);
2122 : 18 : update_stmt (sum);
2123 : : }
2124 : 325 : if (!useless_type_conversion_p (vec_type,
2125 : 325 : TREE_TYPE (valid_vecs[i + 1])))
2126 : : {
2127 : 270 : gimple_stmt_iterator gsi = gsi_for_stmt (sum);
2128 : 810 : tree vce = build1 (VIEW_CONVERT_EXPR, vec_type,
2129 : 270 : valid_vecs[i + 1]);
2130 : 270 : tree lhs = make_ssa_name (vec_type);
2131 : 270 : gimple *g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, vce);
2132 : 270 : gimple_set_uid (g, gimple_uid (sum));
2133 : 270 : gsi_insert_before (&gsi, g, GSI_NEW_STMT);
2134 : 270 : gimple_assign_set_rhs2 (sum, lhs);
2135 : 270 : update_stmt (sum);
2136 : : }
2137 : 325 : sum_vec = gimple_get_lhs (sum);
2138 : 325 : info_ptr = *(v_info_map.get (valid_vecs[i + 1]));
2139 : 325 : gcc_assert (types_compatible_p (vec_type, info_ptr->vec_type));
2140 : : /* Update those related ops of current candidate VECTOR. */
2141 : 1575 : FOR_EACH_VEC_ELT (info_ptr->vec, j, elem)
2142 : : {
2143 : 1250 : idx = elem->second;
2144 : 1250 : gimple *def = SSA_NAME_DEF_STMT ((*ops)[idx]->op);
2145 : : /* Set this then op definition will get DCEd later. */
2146 : 1250 : gimple_set_visited (def, true);
2147 : 1250 : if (opcode == PLUS_EXPR
2148 : 1250 : || opcode == BIT_XOR_EXPR
2149 : 100 : || opcode == BIT_IOR_EXPR)
2150 : 1190 : (*ops)[idx]->op = build_zero_cst (TREE_TYPE ((*ops)[idx]->op));
2151 : 60 : else if (opcode == MULT_EXPR)
2152 : 24 : (*ops)[idx]->op = build_one_cst (TREE_TYPE ((*ops)[idx]->op));
2153 : : else
2154 : : {
2155 : 36 : gcc_assert (opcode == BIT_AND_EXPR);
2156 : 36 : (*ops)[idx]->op
2157 : 36 : = build_all_ones_cst (TREE_TYPE ((*ops)[idx]->op));
2158 : : }
2159 : 1250 : (*ops)[idx]->rank = 0;
2160 : : }
2161 : 325 : if (dump_file && (dump_flags & TDF_DETAILS))
2162 : : {
2163 : 0 : fprintf (dump_file, "Generating addition -> ");
2164 : 0 : print_gimple_stmt (dump_file, sum, 0);
2165 : : }
2166 : 325 : i++;
2167 : : }
2168 : 325 : while ((i < valid_vecs.length () - 1)
2169 : 367 : && TYPE_MODE (TREE_TYPE (valid_vecs[i + 1])) == mode);
2170 : :
2171 : : /* Referring to first valid VECTOR with this mode, generate the
2172 : : BIT_FIELD_REF statements accordingly. */
2173 : 42 : info_ptr = *(v_info_map.get (tvec));
2174 : 42 : gcc_assert (sum);
2175 : 42 : tree elem_type = TREE_TYPE (vec_type);
2176 : 232 : FOR_EACH_VEC_ELT (info_ptr->vec, j, elem)
2177 : : {
2178 : 148 : idx = elem->second;
2179 : 148 : tree dst = make_ssa_name (elem_type);
2180 : 148 : tree pos = bitsize_int (elem->first
2181 : : * tree_to_uhwi (TYPE_SIZE (elem_type)));
2182 : 148 : tree bfr = build3 (BIT_FIELD_REF, elem_type, sum_vec,
2183 : 148 : TYPE_SIZE (elem_type), pos);
2184 : 148 : gimple *gs = gimple_build_assign (dst, BIT_FIELD_REF, bfr);
2185 : 148 : insert_stmt_after (gs, sum);
2186 : 148 : gimple *def = SSA_NAME_DEF_STMT ((*ops)[idx]->op);
2187 : : /* Set this then op definition will get DCEd later. */
2188 : 148 : gimple_set_visited (def, true);
2189 : 148 : (*ops)[idx]->op = gimple_assign_lhs (gs);
2190 : 148 : (*ops)[idx]->rank = get_rank ((*ops)[idx]->op);
2191 : 148 : if (dump_file && (dump_flags & TDF_DETAILS))
2192 : : {
2193 : 0 : fprintf (dump_file, "Generating bit_field_ref -> ");
2194 : 0 : print_gimple_stmt (dump_file, gs, 0);
2195 : : }
2196 : : }
2197 : : }
2198 : :
2199 : 40 : if (dump_file && (dump_flags & TDF_DETAILS))
2200 : 0 : fprintf (dump_file, "undistributiong bit_field_ref for vector done.\n");
2201 : :
2202 : 40 : cleanup_vinfo_map (v_info_map);
2203 : :
2204 : 40 : return true;
2205 : 4381776 : }
2206 : :
2207 : : /* If OPCODE is BIT_IOR_EXPR or BIT_AND_EXPR and CURR is a comparison
2208 : : expression, examine the other OPS to see if any of them are comparisons
2209 : : of the same values, which we may be able to combine or eliminate.
2210 : : For example, we can rewrite (a < b) | (a == b) as (a <= b). */
2211 : :
2212 : : static bool
2213 : 9346707 : eliminate_redundant_comparison (enum tree_code opcode,
2214 : : vec<operand_entry *> *ops,
2215 : : unsigned int currindex,
2216 : : operand_entry *curr)
2217 : : {
2218 : 9346707 : tree op1, op2;
2219 : 9346707 : enum tree_code lcode, rcode;
2220 : 9346707 : gimple *def1, *def2;
2221 : 9346707 : int i;
2222 : 9346707 : operand_entry *oe;
2223 : :
2224 : 9346707 : if (opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
2225 : : return false;
2226 : :
2227 : : /* Check that CURR is a comparison. */
2228 : 2124304 : if (TREE_CODE (curr->op) != SSA_NAME)
2229 : : return false;
2230 : 1585928 : def1 = SSA_NAME_DEF_STMT (curr->op);
2231 : 1585928 : if (!is_gimple_assign (def1))
2232 : : return false;
2233 : 1373126 : lcode = gimple_assign_rhs_code (def1);
2234 : 1373126 : if (TREE_CODE_CLASS (lcode) != tcc_comparison)
2235 : : return false;
2236 : 556103 : op1 = gimple_assign_rhs1 (def1);
2237 : 556103 : op2 = gimple_assign_rhs2 (def1);
2238 : :
2239 : : /* Now look for a similar comparison in the remaining OPS. */
2240 : 1140977 : for (i = currindex + 1; ops->iterate (i, &oe); i++)
2241 : : {
2242 : 585155 : tree t;
2243 : :
2244 : 585155 : if (TREE_CODE (oe->op) != SSA_NAME)
2245 : 37 : continue;
2246 : 585118 : def2 = SSA_NAME_DEF_STMT (oe->op);
2247 : 585118 : if (!is_gimple_assign (def2))
2248 : 8078 : continue;
2249 : 577040 : rcode = gimple_assign_rhs_code (def2);
2250 : 577040 : if (TREE_CODE_CLASS (rcode) != tcc_comparison)
2251 : 7916 : continue;
2252 : :
2253 : : /* If we got here, we have a match. See if we can combine the
2254 : : two comparisons. */
2255 : 569124 : tree type = TREE_TYPE (gimple_assign_lhs (def1));
2256 : 569124 : if (opcode == BIT_IOR_EXPR)
2257 : 431413 : t = maybe_fold_or_comparisons (type,
2258 : : lcode, op1, op2,
2259 : : rcode, gimple_assign_rhs1 (def2),
2260 : : gimple_assign_rhs2 (def2));
2261 : : else
2262 : 137711 : t = maybe_fold_and_comparisons (type,
2263 : : lcode, op1, op2,
2264 : : rcode, gimple_assign_rhs1 (def2),
2265 : : gimple_assign_rhs2 (def2));
2266 : 569124 : if (!t)
2267 : 568815 : continue;
2268 : :
2269 : : /* maybe_fold_and_comparisons and maybe_fold_or_comparisons
2270 : : always give us a boolean_type_node value back. If the original
2271 : : BIT_AND_EXPR or BIT_IOR_EXPR was of a wider integer type,
2272 : : we need to convert. */
2273 : 309 : if (!useless_type_conversion_p (TREE_TYPE (curr->op), TREE_TYPE (t)))
2274 : : {
2275 : 1 : if (!fold_convertible_p (TREE_TYPE (curr->op), t))
2276 : 0 : continue;
2277 : 1 : t = fold_convert (TREE_TYPE (curr->op), t);
2278 : : }
2279 : :
2280 : 309 : if (TREE_CODE (t) != INTEGER_CST
2281 : 309 : && !operand_equal_p (t, curr->op, 0))
2282 : : {
2283 : 303 : enum tree_code subcode;
2284 : 303 : tree newop1, newop2;
2285 : 303 : if (!COMPARISON_CLASS_P (t))
2286 : 28 : continue;
2287 : 295 : extract_ops_from_tree (t, &subcode, &newop1, &newop2);
2288 : 295 : STRIP_USELESS_TYPE_CONVERSION (newop1);
2289 : 295 : STRIP_USELESS_TYPE_CONVERSION (newop2);
2290 : 295 : if (!is_gimple_val (newop1) || !is_gimple_val (newop2))
2291 : 0 : continue;
2292 : 295 : if (lcode == TREE_CODE (t)
2293 : 117 : && operand_equal_p (op1, newop1, 0)
2294 : 412 : && operand_equal_p (op2, newop2, 0))
2295 : 78 : t = curr->op;
2296 : 237 : else if ((TREE_CODE (newop1) == SSA_NAME
2297 : 217 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (newop1))
2298 : 414 : || (TREE_CODE (newop2) == SSA_NAME
2299 : 155 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (newop2)))
2300 : 20 : continue;
2301 : : }
2302 : :
2303 : 281 : if (dump_file && (dump_flags & TDF_DETAILS))
2304 : : {
2305 : 6 : fprintf (dump_file, "Equivalence: ");
2306 : 6 : print_generic_expr (dump_file, curr->op);
2307 : 6 : fprintf (dump_file, " %s ", op_symbol_code (opcode));
2308 : 6 : print_generic_expr (dump_file, oe->op);
2309 : 6 : fprintf (dump_file, " -> ");
2310 : 6 : print_generic_expr (dump_file, t);
2311 : 6 : fprintf (dump_file, "\n");
2312 : : }
2313 : :
2314 : : /* Now we can delete oe, as it has been subsumed by the new combined
2315 : : expression t. */
2316 : 281 : ops->ordered_remove (i);
2317 : 281 : reassociate_stats.ops_eliminated ++;
2318 : :
2319 : : /* If t is the same as curr->op, we're done. Otherwise we must
2320 : : replace curr->op with t. Special case is if we got a constant
2321 : : back, in which case we add it to the end instead of in place of
2322 : : the current entry. */
2323 : 281 : if (TREE_CODE (t) == INTEGER_CST)
2324 : : {
2325 : 6 : ops->ordered_remove (currindex);
2326 : 6 : add_to_ops_vec (ops, t);
2327 : : }
2328 : 275 : else if (!operand_equal_p (t, curr->op, 0))
2329 : : {
2330 : 197 : gimple *sum;
2331 : 197 : enum tree_code subcode;
2332 : 197 : tree newop1;
2333 : 197 : tree newop2;
2334 : 197 : gcc_assert (COMPARISON_CLASS_P (t));
2335 : 197 : extract_ops_from_tree (t, &subcode, &newop1, &newop2);
2336 : 197 : STRIP_USELESS_TYPE_CONVERSION (newop1);
2337 : 197 : STRIP_USELESS_TYPE_CONVERSION (newop2);
2338 : 197 : gcc_checking_assert (is_gimple_val (newop1)
2339 : : && is_gimple_val (newop2));
2340 : 197 : sum = build_and_add_sum (TREE_TYPE (t), newop1, newop2, subcode);
2341 : 197 : curr->op = gimple_get_lhs (sum);
2342 : : }
2343 : : return true;
2344 : : }
2345 : :
2346 : : return false;
2347 : : }
2348 : :
2349 : :
2350 : : /* Transform repeated addition of same values into multiply with
2351 : : constant. */
2352 : : static bool
2353 : 2237278 : transform_add_to_multiply (vec<operand_entry *> *ops)
2354 : : {
2355 : 2237278 : operand_entry *oe;
2356 : 2237278 : tree op = NULL_TREE;
2357 : 2237278 : int j;
2358 : 2237278 : int i, start = -1, end = 0, count = 0;
2359 : 2237278 : auto_vec<std::pair <int, int> > indxs;
2360 : 2237278 : bool changed = false;
2361 : :
2362 : 2237278 : if (!INTEGRAL_TYPE_P (TREE_TYPE ((*ops)[0]->op))
2363 : 63847 : && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE ((*ops)[0]->op))
2364 : 30858 : || !flag_unsafe_math_optimizations))
2365 : : return false;
2366 : :
2367 : : /* Look for repeated operands. */
2368 : 6791517 : FOR_EACH_VEC_ELT (*ops, i, oe)
2369 : : {
2370 : 4587287 : if (start == -1)
2371 : : {
2372 : 2204230 : count = 1;
2373 : 2204230 : op = oe->op;
2374 : 2204230 : start = i;
2375 : : }
2376 : 2383057 : else if (operand_equal_p (oe->op, op, 0))
2377 : : {
2378 : 169 : count++;
2379 : 169 : end = i;
2380 : : }
2381 : : else
2382 : : {
2383 : 2382888 : if (count > 1)
2384 : 61 : indxs.safe_push (std::make_pair (start, end));
2385 : 2382888 : count = 1;
2386 : 2382888 : op = oe->op;
2387 : 2382888 : start = i;
2388 : : }
2389 : : }
2390 : :
2391 : 2204230 : if (count > 1)
2392 : 38 : indxs.safe_push (std::make_pair (start, end));
2393 : :
2394 : 2204416 : for (j = indxs.length () - 1; j >= 0; --j)
2395 : : {
2396 : : /* Convert repeated operand addition to multiplication. */
2397 : 99 : start = indxs[j].first;
2398 : 99 : end = indxs[j].second;
2399 : 99 : op = (*ops)[start]->op;
2400 : 99 : count = end - start + 1;
2401 : 367 : for (i = end; i >= start; --i)
2402 : 268 : ops->unordered_remove (i);
2403 : 99 : tree tmp = make_ssa_name (TREE_TYPE (op));
2404 : 99 : tree cst = build_int_cst (integer_type_node, count);
2405 : 99 : gassign *mul_stmt
2406 : 99 : = gimple_build_assign (tmp, MULT_EXPR,
2407 : 99 : op, fold_convert (TREE_TYPE (op), cst));
2408 : 99 : gimple_set_visited (mul_stmt, true);
2409 : 99 : add_to_ops_vec (ops, tmp, mul_stmt);
2410 : 99 : changed = true;
2411 : : }
2412 : :
2413 : : return changed;
2414 : 2237278 : }
2415 : :
2416 : :
2417 : : /* Perform various identities and other optimizations on the list of
2418 : : operand entries, stored in OPS. The tree code for the binary
2419 : : operation between all the operands is OPCODE. */
2420 : :
2421 : : static void
2422 : 4536981 : optimize_ops_list (enum tree_code opcode,
2423 : : vec<operand_entry *> *ops)
2424 : : {
2425 : 4554560 : unsigned int length = ops->length ();
2426 : 4554560 : unsigned int i;
2427 : 4554560 : operand_entry *oe;
2428 : 9107143 : operand_entry *oelast = NULL;
2429 : 9107143 : bool iterate = false;
2430 : :
2431 : 4554560 : if (length == 1)
2432 : 4536981 : return;
2433 : :
2434 : 4552583 : oelast = ops->last ();
2435 : :
2436 : : /* If the last two are constants, pop the constants off, merge them
2437 : : and try the next two. */
2438 : 4552583 : if (oelast->rank == 0 && is_gimple_min_invariant (oelast->op))
2439 : : {
2440 : 3301176 : operand_entry *oelm1 = (*ops)[length - 2];
2441 : :
2442 : 3301176 : if (oelm1->rank == 0
2443 : 13703 : && is_gimple_min_invariant (oelm1->op)
2444 : 3314879 : && useless_type_conversion_p (TREE_TYPE (oelm1->op),
2445 : 13703 : TREE_TYPE (oelast->op)))
2446 : : {
2447 : 13703 : tree folded = fold_binary (opcode, TREE_TYPE (oelm1->op),
2448 : : oelm1->op, oelast->op);
2449 : :
2450 : 13703 : if (folded && is_gimple_min_invariant (folded))
2451 : : {
2452 : 13685 : if (dump_file && (dump_flags & TDF_DETAILS))
2453 : 0 : fprintf (dump_file, "Merging constants\n");
2454 : :
2455 : 13685 : ops->pop ();
2456 : 13685 : ops->pop ();
2457 : :
2458 : 13685 : add_to_ops_vec (ops, folded);
2459 : 13685 : reassociate_stats.constants_eliminated++;
2460 : :
2461 : 13685 : optimize_ops_list (opcode, ops);
2462 : 13685 : return;
2463 : : }
2464 : : }
2465 : : }
2466 : :
2467 : 4538898 : eliminate_using_constants (opcode, ops);
2468 : 4538898 : oelast = NULL;
2469 : :
2470 : 13889252 : for (i = 0; ops->iterate (i, &oe);)
2471 : : {
2472 : 9350356 : bool done = false;
2473 : :
2474 : 9350356 : if (eliminate_not_pairs (opcode, ops, i, oe))
2475 : 2 : return;
2476 : 9350355 : if (eliminate_duplicate_pair (opcode, ops, &done, i, oe, oelast)
2477 : 9350310 : || (!done && eliminate_plus_minus_pair (opcode, ops, i, oe))
2478 : 18697062 : || (!done && eliminate_redundant_comparison (opcode, ops, i, oe)))
2479 : : {
2480 : 3929 : if (done)
2481 : : return;
2482 : 3928 : iterate = true;
2483 : 3928 : oelast = NULL;
2484 : 3928 : continue;
2485 : : }
2486 : 9346426 : oelast = oe;
2487 : 9346426 : i++;
2488 : : }
2489 : :
2490 : 4538896 : if (iterate)
2491 : : optimize_ops_list (opcode, ops);
2492 : : }
2493 : :
2494 : : /* The following functions are subroutines to optimize_range_tests and allow
2495 : : it to try to change a logical combination of comparisons into a range
2496 : : test.
2497 : :
2498 : : For example, both
2499 : : X == 2 || X == 5 || X == 3 || X == 4
2500 : : and
2501 : : X >= 2 && X <= 5
2502 : : are converted to
2503 : : (unsigned) (X - 2) <= 3
2504 : :
2505 : : For more information see comments above fold_test_range in fold-const.cc,
2506 : : this implementation is for GIMPLE. */
2507 : :
2508 : :
2509 : :
2510 : : /* Dump the range entry R to FILE, skipping its expression if SKIP_EXP. */
2511 : :
2512 : : void
2513 : 141 : dump_range_entry (FILE *file, struct range_entry *r, bool skip_exp)
2514 : : {
2515 : 141 : if (!skip_exp)
2516 : 59 : print_generic_expr (file, r->exp);
2517 : 251 : fprintf (file, " %c[", r->in_p ? '+' : '-');
2518 : 141 : print_generic_expr (file, r->low);
2519 : 141 : fputs (", ", file);
2520 : 141 : print_generic_expr (file, r->high);
2521 : 141 : fputc (']', file);
2522 : 141 : }
2523 : :
2524 : : /* Dump the range entry R to STDERR. */
2525 : :
2526 : : DEBUG_FUNCTION void
2527 : 0 : debug_range_entry (struct range_entry *r)
2528 : : {
2529 : 0 : dump_range_entry (stderr, r, false);
2530 : 0 : fputc ('\n', stderr);
2531 : 0 : }
2532 : :
2533 : : /* This is similar to make_range in fold-const.cc, but on top of
2534 : : GIMPLE instead of trees. If EXP is non-NULL, it should be
2535 : : an SSA_NAME and STMT argument is ignored, otherwise STMT
2536 : : argument should be a GIMPLE_COND. */
2537 : :
2538 : : void
2539 : 5607375 : init_range_entry (struct range_entry *r, tree exp, gimple *stmt)
2540 : : {
2541 : 5607375 : int in_p;
2542 : 5607375 : tree low, high;
2543 : 5607375 : bool is_bool, strict_overflow_p;
2544 : :
2545 : 5607375 : r->exp = NULL_TREE;
2546 : 5607375 : r->in_p = false;
2547 : 5607375 : r->strict_overflow_p = false;
2548 : 5607375 : r->low = NULL_TREE;
2549 : 5607375 : r->high = NULL_TREE;
2550 : 5607375 : if (exp != NULL_TREE
2551 : 5607375 : && (TREE_CODE (exp) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (exp))))
2552 : 842849 : return;
2553 : :
2554 : : /* Start with simply saying "EXP != 0" and then look at the code of EXP
2555 : : and see if we can refine the range. Some of the cases below may not
2556 : : happen, but it doesn't seem worth worrying about this. We "continue"
2557 : : the outer loop when we've changed something; otherwise we "break"
2558 : : the switch, which will "break" the while. */
2559 : 5072058 : low = exp ? build_int_cst (TREE_TYPE (exp), 0) : boolean_false_node;
2560 : 5072058 : high = low;
2561 : 5072058 : in_p = 0;
2562 : 5072058 : strict_overflow_p = false;
2563 : 5072058 : is_bool = false;
2564 : 5072058 : if (exp == NULL_TREE)
2565 : : is_bool = true;
2566 : 1648381 : else if (TYPE_PRECISION (TREE_TYPE (exp)) == 1)
2567 : : {
2568 : 697298 : if (TYPE_UNSIGNED (TREE_TYPE (exp)))
2569 : : is_bool = true;
2570 : : else
2571 : : return;
2572 : : }
2573 : 951083 : else if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE)
2574 : 0 : is_bool = true;
2575 : :
2576 : 8009744 : while (1)
2577 : : {
2578 : 8009744 : enum tree_code code;
2579 : 8009744 : tree arg0, arg1, exp_type;
2580 : 8009744 : tree nexp;
2581 : 8009744 : location_t loc;
2582 : :
2583 : 8009744 : if (exp != NULL_TREE)
2584 : : {
2585 : 4586067 : if (TREE_CODE (exp) != SSA_NAME
2586 : 4586067 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
2587 : : break;
2588 : :
2589 : 4586067 : stmt = SSA_NAME_DEF_STMT (exp);
2590 : 4586067 : if (!is_gimple_assign (stmt))
2591 : : break;
2592 : :
2593 : 2841386 : code = gimple_assign_rhs_code (stmt);
2594 : 2841386 : arg0 = gimple_assign_rhs1 (stmt);
2595 : 2841386 : arg1 = gimple_assign_rhs2 (stmt);
2596 : 2841386 : exp_type = TREE_TYPE (exp);
2597 : : }
2598 : : else
2599 : : {
2600 : 3423677 : code = gimple_cond_code (stmt);
2601 : 3423677 : arg0 = gimple_cond_lhs (stmt);
2602 : 3423677 : arg1 = gimple_cond_rhs (stmt);
2603 : 3423677 : exp_type = boolean_type_node;
2604 : : }
2605 : :
2606 : 6265063 : if (TREE_CODE (arg0) != SSA_NAME
2607 : 4972987 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0)
2608 : 11237605 : || ssa_name_maybe_undef_p (arg0))
2609 : : break;
2610 : 4966001 : loc = gimple_location (stmt);
2611 : 4966001 : switch (code)
2612 : : {
2613 : 33907 : case BIT_NOT_EXPR:
2614 : 33907 : if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE
2615 : : /* Ensure the range is either +[-,0], +[0,0],
2616 : : -[-,0], -[0,0] or +[1,-], +[1,1], -[1,-] or
2617 : : -[1,1]. If it is e.g. +[-,-] or -[-,-]
2618 : : or similar expression of unconditional true or
2619 : : false, it should not be negated. */
2620 : 33907 : && ((high && integer_zerop (high))
2621 : 0 : || (low && integer_onep (low))))
2622 : : {
2623 : 6936 : in_p = !in_p;
2624 : 6936 : exp = arg0;
2625 : 6936 : continue;
2626 : : }
2627 : : break;
2628 : 2747 : case SSA_NAME:
2629 : 2747 : exp = arg0;
2630 : 2747 : continue;
2631 : 245204 : CASE_CONVERT:
2632 : 245204 : if (is_bool)
2633 : : {
2634 : 134454 : if ((TYPE_PRECISION (exp_type) == 1
2635 : 128151 : || TREE_CODE (exp_type) == BOOLEAN_TYPE)
2636 : 134454 : && TYPE_PRECISION (TREE_TYPE (arg0)) > 1)
2637 : : return;
2638 : : }
2639 : 110750 : else if (TYPE_PRECISION (TREE_TYPE (arg0)) == 1)
2640 : : {
2641 : 3960 : if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
2642 : : is_bool = true;
2643 : : else
2644 : : return;
2645 : : }
2646 : 106790 : else if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE)
2647 : 135621 : is_bool = true;
2648 : 242411 : goto do_default;
2649 : : case EQ_EXPR:
2650 : : case NE_EXPR:
2651 : : case LT_EXPR:
2652 : : case LE_EXPR:
2653 : : case GE_EXPR:
2654 : : case GT_EXPR:
2655 : : is_bool = true;
2656 : : /* FALLTHRU */
2657 : 655106 : default:
2658 : 655106 : if (!is_bool)
2659 : : return;
2660 : 350367 : do_default:
2661 : 4621815 : nexp = make_range_step (loc, code, arg0, arg1, exp_type,
2662 : : &low, &high, &in_p,
2663 : : &strict_overflow_p);
2664 : 4621815 : if (nexp != NULL_TREE)
2665 : : {
2666 : 2928003 : exp = nexp;
2667 : 2928003 : gcc_assert (TREE_CODE (exp) == SSA_NAME);
2668 : 2928003 : continue;
2669 : : }
2670 : : break;
2671 : : }
2672 : : break;
2673 : : }
2674 : 4764526 : if (is_bool)
2675 : : {
2676 : 4122142 : r->exp = exp;
2677 : 4122142 : r->in_p = in_p;
2678 : 4122142 : r->low = low;
2679 : 4122142 : r->high = high;
2680 : 4122142 : r->strict_overflow_p = strict_overflow_p;
2681 : : }
2682 : : }
2683 : :
2684 : : /* Comparison function for qsort. Sort entries
2685 : : without SSA_NAME exp first, then with SSA_NAMEs sorted
2686 : : by increasing SSA_NAME_VERSION, and for the same SSA_NAMEs
2687 : : by increasing ->low and if ->low is the same, by increasing
2688 : : ->high. ->low == NULL_TREE means minimum, ->high == NULL_TREE
2689 : : maximum. */
2690 : :
2691 : : static int
2692 : 6611717 : range_entry_cmp (const void *a, const void *b)
2693 : : {
2694 : 6611717 : const struct range_entry *p = (const struct range_entry *) a;
2695 : 6611717 : const struct range_entry *q = (const struct range_entry *) b;
2696 : :
2697 : 6611717 : if (p->exp != NULL_TREE && TREE_CODE (p->exp) == SSA_NAME)
2698 : : {
2699 : 2876679 : if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
2700 : : {
2701 : : /* Group range_entries for the same SSA_NAME together. */
2702 : 2819228 : if (SSA_NAME_VERSION (p->exp) < SSA_NAME_VERSION (q->exp))
2703 : : return -1;
2704 : 1203973 : else if (SSA_NAME_VERSION (p->exp) > SSA_NAME_VERSION (q->exp))
2705 : : return 1;
2706 : : /* If ->low is different, NULL low goes first, then by
2707 : : ascending low. */
2708 : 146172 : if (p->low != NULL_TREE)
2709 : : {
2710 : 129683 : if (q->low != NULL_TREE)
2711 : : {
2712 : 122303 : tree tem = fold_binary (LT_EXPR, boolean_type_node,
2713 : : p->low, q->low);
2714 : 122303 : if (tem && integer_onep (tem))
2715 : : return -1;
2716 : 50817 : tem = fold_binary (GT_EXPR, boolean_type_node,
2717 : : p->low, q->low);
2718 : 50817 : if (tem && integer_onep (tem))
2719 : : return 1;
2720 : : }
2721 : : else
2722 : : return 1;
2723 : : }
2724 : 16489 : else if (q->low != NULL_TREE)
2725 : : return -1;
2726 : : /* If ->high is different, NULL high goes last, before that by
2727 : : ascending high. */
2728 : 13446 : if (p->high != NULL_TREE)
2729 : : {
2730 : 13310 : if (q->high != NULL_TREE)
2731 : : {
2732 : 13043 : tree tem = fold_binary (LT_EXPR, boolean_type_node,
2733 : : p->high, q->high);
2734 : 13043 : if (tem && integer_onep (tem))
2735 : : return -1;
2736 : 4695 : tem = fold_binary (GT_EXPR, boolean_type_node,
2737 : : p->high, q->high);
2738 : 4695 : if (tem && integer_onep (tem))
2739 : : return 1;
2740 : : }
2741 : : else
2742 : : return -1;
2743 : : }
2744 : 136 : else if (q->high != NULL_TREE)
2745 : : return 1;
2746 : : /* If both ranges are the same, sort below by ascending idx. */
2747 : : }
2748 : : else
2749 : : return 1;
2750 : : }
2751 : 3735038 : else if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
2752 : : return -1;
2753 : :
2754 : 3652387 : if (p->idx < q->idx)
2755 : : return -1;
2756 : : else
2757 : : {
2758 : 1842200 : gcc_checking_assert (p->idx > q->idx);
2759 : : return 1;
2760 : : }
2761 : : }
2762 : :
2763 : : /* Helper function for update_range_test. Force EXPR into an SSA_NAME,
2764 : : insert needed statements BEFORE or after GSI. */
2765 : :
2766 : : static tree
2767 : 20164 : force_into_ssa_name (gimple_stmt_iterator *gsi, tree expr, bool before)
2768 : : {
2769 : 20164 : enum gsi_iterator_update m = before ? GSI_SAME_STMT : GSI_CONTINUE_LINKING;
2770 : 20164 : tree ret = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE, before, m);
2771 : 20164 : if (TREE_CODE (ret) != SSA_NAME)
2772 : : {
2773 : 30 : gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (ret)), ret);
2774 : 30 : if (before)
2775 : 30 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2776 : : else
2777 : 0 : gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING);
2778 : 30 : ret = gimple_assign_lhs (g);
2779 : : }
2780 : 20164 : return ret;
2781 : : }
2782 : :
2783 : : /* Helper routine of optimize_range_test.
2784 : : [EXP, IN_P, LOW, HIGH, STRICT_OVERFLOW_P] is a merged range for
2785 : : RANGE and OTHERRANGE through OTHERRANGE + COUNT - 1 ranges,
2786 : : OPCODE and OPS are arguments of optimize_range_tests. If OTHERRANGE
2787 : : is NULL, OTHERRANGEP should not be and then OTHERRANGEP points to
2788 : : an array of COUNT pointers to other ranges. Return
2789 : : true if the range merge has been successful.
2790 : : If OPCODE is ERROR_MARK, this is called from within
2791 : : maybe_optimize_range_tests and is performing inter-bb range optimization.
2792 : : In that case, whether an op is BIT_AND_EXPR or BIT_IOR_EXPR is found in
2793 : : oe->rank. */
2794 : :
2795 : : static bool
2796 : 20164 : update_range_test (struct range_entry *range, struct range_entry *otherrange,
2797 : : struct range_entry **otherrangep,
2798 : : unsigned int count, enum tree_code opcode,
2799 : : vec<operand_entry *> *ops, tree exp, gimple_seq seq,
2800 : : bool in_p, tree low, tree high, bool strict_overflow_p)
2801 : : {
2802 : 20164 : unsigned int idx = range->idx;
2803 : 20164 : struct range_entry *swap_with = NULL;
2804 : 20164 : basic_block rewrite_bb_first = NULL, rewrite_bb_last = NULL;
2805 : 20164 : if (opcode == ERROR_MARK)
2806 : : {
2807 : : /* For inter-bb range test optimization, pick from the range tests
2808 : : the one which is tested in the earliest condition (one dominating
2809 : : the others), because otherwise there could be some UB (e.g. signed
2810 : : overflow) in following bbs that we'd expose which wasn't there in
2811 : : the original program. See PR104196. */
2812 : 9110 : basic_block orig_range_bb = BASIC_BLOCK_FOR_FN (cfun, (*ops)[idx]->id);
2813 : 9110 : basic_block range_bb = orig_range_bb;
2814 : 22982 : for (unsigned int i = 0; i < count; i++)
2815 : : {
2816 : 13872 : struct range_entry *this_range;
2817 : 13872 : if (otherrange)
2818 : 5542 : this_range = otherrange + i;
2819 : : else
2820 : 8330 : this_range = otherrangep[i];
2821 : 13872 : operand_entry *oe = (*ops)[this_range->idx];
2822 : 13872 : basic_block this_bb = BASIC_BLOCK_FOR_FN (cfun, oe->id);
2823 : 13872 : if (range_bb != this_bb
2824 : 13872 : && dominated_by_p (CDI_DOMINATORS, range_bb, this_bb))
2825 : : {
2826 : 7728 : swap_with = this_range;
2827 : 7728 : range_bb = this_bb;
2828 : 7728 : idx = this_range->idx;
2829 : : }
2830 : : }
2831 : : /* If seq is non-NULL, it can contain statements that use SSA_NAMEs
2832 : : only defined in later blocks. In this case we can't move the
2833 : : merged comparison earlier, so instead check if there are any stmts
2834 : : that might trigger signed integer overflow in between and rewrite
2835 : : them. But only after we check if the optimization is possible. */
2836 : 9110 : if (seq && swap_with)
2837 : : {
2838 : 3264 : rewrite_bb_first = range_bb;
2839 : 3264 : rewrite_bb_last = orig_range_bb;
2840 : 3264 : idx = range->idx;
2841 : 3264 : swap_with = NULL;
2842 : : }
2843 : : }
2844 : 20164 : operand_entry *oe = (*ops)[idx];
2845 : 20164 : tree op = oe->op;
2846 : 20164 : gimple *stmt = op ? SSA_NAME_DEF_STMT (op)
2847 : 7350 : : last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
2848 : 20164 : location_t loc = gimple_location (stmt);
2849 : 20164 : tree optype = op ? TREE_TYPE (op) : boolean_type_node;
2850 : 20164 : tree tem = build_range_check (loc, optype, unshare_expr (exp),
2851 : : in_p, low, high);
2852 : 20164 : enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
2853 : 20164 : gimple_stmt_iterator gsi;
2854 : 20164 : unsigned int i, uid;
2855 : :
2856 : 20164 : if (tem == NULL_TREE)
2857 : : return false;
2858 : :
2859 : : /* If op is default def SSA_NAME, there is no place to insert the
2860 : : new comparison. Give up, unless we can use OP itself as the
2861 : : range test. */
2862 : 32978 : if (op && SSA_NAME_IS_DEFAULT_DEF (op))
2863 : : {
2864 : 0 : if (op == range->exp
2865 : 0 : && ((TYPE_PRECISION (optype) == 1 && TYPE_UNSIGNED (optype))
2866 : 0 : || TREE_CODE (optype) == BOOLEAN_TYPE)
2867 : 0 : && (op == tem
2868 : 0 : || (TREE_CODE (tem) == EQ_EXPR
2869 : 0 : && TREE_OPERAND (tem, 0) == op
2870 : 0 : && integer_onep (TREE_OPERAND (tem, 1))))
2871 : 0 : && opcode != BIT_IOR_EXPR
2872 : 0 : && (opcode != ERROR_MARK || oe->rank != BIT_IOR_EXPR))
2873 : : {
2874 : : stmt = NULL;
2875 : : tem = op;
2876 : : }
2877 : : else
2878 : 0 : return false;
2879 : : }
2880 : :
2881 : 20164 : if (swap_with)
2882 : 1168 : std::swap (range->idx, swap_with->idx);
2883 : :
2884 : 20164 : if (strict_overflow_p && issue_strict_overflow_warning (wc))
2885 : 0 : warning_at (loc, OPT_Wstrict_overflow,
2886 : : "assuming signed overflow does not occur "
2887 : : "when simplifying range test");
2888 : :
2889 : 20164 : if (dump_file && (dump_flags & TDF_DETAILS))
2890 : : {
2891 : 39 : struct range_entry *r;
2892 : 39 : fprintf (dump_file, "Optimizing range tests ");
2893 : 39 : dump_range_entry (dump_file, range, false);
2894 : 180 : for (i = 0; i < count; i++)
2895 : : {
2896 : 102 : if (otherrange)
2897 : 82 : r = otherrange + i;
2898 : : else
2899 : 20 : r = otherrangep[i];
2900 : 102 : if (r->exp
2901 : 102 : && r->exp != range->exp
2902 : 20 : && TREE_CODE (r->exp) == SSA_NAME)
2903 : : {
2904 : 20 : fprintf (dump_file, " and ");
2905 : 20 : dump_range_entry (dump_file, r, false);
2906 : : }
2907 : : else
2908 : : {
2909 : 82 : fprintf (dump_file, " and");
2910 : 82 : dump_range_entry (dump_file, r, true);
2911 : : }
2912 : : }
2913 : 39 : fprintf (dump_file, "\n into ");
2914 : 39 : print_generic_expr (dump_file, tem);
2915 : 39 : fprintf (dump_file, "\n");
2916 : : }
2917 : :
2918 : : /* In inter-bb range optimization mode, if we have a seq, we can't
2919 : : move the merged comparison to the earliest bb from the comparisons
2920 : : being replaced, so instead rewrite stmts that could trigger signed
2921 : : integer overflow. */
2922 : 7933 : for (basic_block bb = rewrite_bb_last;
2923 : 28097 : bb != rewrite_bb_first; bb = single_pred (bb))
2924 : 15866 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
2925 : 27381 : !gsi_end_p (gsi); gsi_next (&gsi))
2926 : : {
2927 : 19448 : gimple *stmt = gsi_stmt (gsi);
2928 : 19448 : if (gimple_needing_rewrite_undefined (stmt))
2929 : : {
2930 : 45 : gimple_stmt_iterator gsip = gsi;
2931 : 45 : gimple_stmt_iterator gsin = gsi;
2932 : 45 : gsi_prev (&gsip);
2933 : 45 : gsi_next (&gsin);
2934 : 45 : rewrite_to_defined_unconditional (&gsi);
2935 : 45 : unsigned uid = gimple_uid (stmt);
2936 : 45 : if (gsi_end_p (gsip))
2937 : 28 : gsip = gsi_after_labels (bb);
2938 : : else
2939 : 17 : gsi_next (&gsip);
2940 : 180 : for (; gsi_stmt (gsip) != gsi_stmt (gsin);
2941 : 135 : gsi_next (&gsip))
2942 : 135 : gimple_set_uid (gsi_stmt (gsip), uid);
2943 : : }
2944 : : }
2945 : :
2946 : 20164 : if (opcode == BIT_IOR_EXPR
2947 : 13915 : || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
2948 : 13420 : tem = invert_truthvalue_loc (loc, tem);
2949 : :
2950 : 20164 : tem = fold_convert_loc (loc, optype, tem);
2951 : 20164 : if (stmt)
2952 : : {
2953 : 20164 : gsi = gsi_for_stmt (stmt);
2954 : 20164 : uid = gimple_uid (stmt);
2955 : : }
2956 : : else
2957 : : {
2958 : 0 : gsi = gsi_none ();
2959 : 0 : uid = 0;
2960 : : }
2961 : 20164 : if (stmt == NULL)
2962 : 0 : gcc_checking_assert (tem == op);
2963 : : /* In rare cases range->exp can be equal to lhs of stmt.
2964 : : In that case we have to insert after the stmt rather then before
2965 : : it. If stmt is a PHI, insert it at the start of the basic block. */
2966 : 20164 : else if (op != range->exp)
2967 : : {
2968 : 20164 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
2969 : 20164 : tem = force_into_ssa_name (&gsi, tem, true);
2970 : 20164 : gsi_prev (&gsi);
2971 : : }
2972 : 0 : else if (gimple_code (stmt) != GIMPLE_PHI)
2973 : : {
2974 : 0 : gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
2975 : 0 : tem = force_into_ssa_name (&gsi, tem, false);
2976 : : }
2977 : : else
2978 : : {
2979 : 0 : gsi = gsi_after_labels (gimple_bb (stmt));
2980 : 0 : if (!gsi_end_p (gsi))
2981 : 0 : uid = gimple_uid (gsi_stmt (gsi));
2982 : : else
2983 : : {
2984 : 0 : gsi = gsi_start_bb (gimple_bb (stmt));
2985 : 0 : uid = 1;
2986 : 0 : while (!gsi_end_p (gsi))
2987 : : {
2988 : 0 : uid = gimple_uid (gsi_stmt (gsi));
2989 : 0 : gsi_next (&gsi);
2990 : : }
2991 : : }
2992 : 0 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
2993 : 0 : tem = force_into_ssa_name (&gsi, tem, true);
2994 : 0 : if (gsi_end_p (gsi))
2995 : 0 : gsi = gsi_last_bb (gimple_bb (stmt));
2996 : : else
2997 : 20164 : gsi_prev (&gsi);
2998 : : }
2999 : 136944 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3000 : 73772 : if (gimple_uid (gsi_stmt (gsi)))
3001 : : break;
3002 : : else
3003 : 58390 : gimple_set_uid (gsi_stmt (gsi), uid);
3004 : :
3005 : 20164 : oe->op = tem;
3006 : 20164 : range->exp = exp;
3007 : 20164 : range->low = low;
3008 : 20164 : range->high = high;
3009 : 20164 : range->in_p = in_p;
3010 : 20164 : range->strict_overflow_p = false;
3011 : :
3012 : 46265 : for (i = 0; i < count; i++)
3013 : : {
3014 : 26101 : if (otherrange)
3015 : 13872 : range = otherrange + i;
3016 : : else
3017 : 12229 : range = otherrangep[i];
3018 : 26101 : oe = (*ops)[range->idx];
3019 : : /* Now change all the other range test immediate uses, so that
3020 : : those tests will be optimized away. */
3021 : 26101 : if (opcode == ERROR_MARK)
3022 : : {
3023 : 13872 : if (oe->op)
3024 : 2090 : oe->op = build_int_cst (TREE_TYPE (oe->op),
3025 : 2090 : oe->rank == BIT_IOR_EXPR ? 0 : 1);
3026 : : else
3027 : 11782 : oe->op = (oe->rank == BIT_IOR_EXPR
3028 : 11782 : ? boolean_false_node : boolean_true_node);
3029 : : }
3030 : : else
3031 : 12229 : oe->op = error_mark_node;
3032 : 26101 : range->exp = NULL_TREE;
3033 : 26101 : range->low = NULL_TREE;
3034 : 26101 : range->high = NULL_TREE;
3035 : : }
3036 : : return true;
3037 : : }
3038 : :
3039 : : /* Optimize X == CST1 || X == CST2
3040 : : if popcount (CST1 ^ CST2) == 1 into
3041 : : (X & ~(CST1 ^ CST2)) == (CST1 & ~(CST1 ^ CST2)).
3042 : : Similarly for ranges. E.g.
3043 : : X != 2 && X != 3 && X != 10 && X != 11
3044 : : will be transformed by the previous optimization into
3045 : : !((X - 2U) <= 1U || (X - 10U) <= 1U)
3046 : : and this loop can transform that into
3047 : : !(((X & ~8) - 2U) <= 1U). */
3048 : :
3049 : : static bool
3050 : 24201 : optimize_range_tests_xor (enum tree_code opcode, tree type,
3051 : : tree lowi, tree lowj, tree highi, tree highj,
3052 : : vec<operand_entry *> *ops,
3053 : : struct range_entry *rangei,
3054 : : struct range_entry *rangej)
3055 : : {
3056 : 24201 : tree lowxor, highxor, tem, exp;
3057 : : /* Check lowi ^ lowj == highi ^ highj and
3058 : : popcount (lowi ^ lowj) == 1. */
3059 : 24201 : lowxor = fold_binary (BIT_XOR_EXPR, type, lowi, lowj);
3060 : 24201 : if (lowxor == NULL_TREE || TREE_CODE (lowxor) != INTEGER_CST)
3061 : : return false;
3062 : 24201 : if (!integer_pow2p (lowxor))
3063 : : return false;
3064 : 2993 : highxor = fold_binary (BIT_XOR_EXPR, type, highi, highj);
3065 : 2993 : if (!tree_int_cst_equal (lowxor, highxor))
3066 : : return false;
3067 : :
3068 : 2588 : exp = rangei->exp;
3069 : 2588 : scalar_int_mode mode = as_a <scalar_int_mode> (TYPE_MODE (type));
3070 : 2588 : int prec = GET_MODE_PRECISION (mode);
3071 : 2588 : if (TYPE_PRECISION (type) < prec
3072 : 2587 : || (wi::to_wide (TYPE_MIN_VALUE (type))
3073 : 7762 : != wi::min_value (prec, TYPE_SIGN (type)))
3074 : 7762 : || (wi::to_wide (TYPE_MAX_VALUE (type))
3075 : 7762 : != wi::max_value (prec, TYPE_SIGN (type))))
3076 : : {
3077 : 1 : type = build_nonstandard_integer_type (prec, TYPE_UNSIGNED (type));
3078 : 1 : exp = fold_convert (type, exp);
3079 : 1 : lowxor = fold_convert (type, lowxor);
3080 : 1 : lowi = fold_convert (type, lowi);
3081 : 1 : highi = fold_convert (type, highi);
3082 : : }
3083 : 2588 : tem = fold_build1 (BIT_NOT_EXPR, type, lowxor);
3084 : 2588 : exp = fold_build2 (BIT_AND_EXPR, type, exp, tem);
3085 : 2588 : lowj = fold_build2 (BIT_AND_EXPR, type, lowi, tem);
3086 : 2588 : highj = fold_build2 (BIT_AND_EXPR, type, highi, tem);
3087 : 2588 : if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, exp,
3088 : 2588 : NULL, rangei->in_p, lowj, highj,
3089 : 2588 : rangei->strict_overflow_p
3090 : 2588 : || rangej->strict_overflow_p))
3091 : : return true;
3092 : : return false;
3093 : : }
3094 : :
3095 : : /* Optimize X == CST1 || X == CST2
3096 : : if popcount (CST2 - CST1) == 1 into
3097 : : ((X - CST1) & ~(CST2 - CST1)) == 0.
3098 : : Similarly for ranges. E.g.
3099 : : X == 43 || X == 76 || X == 44 || X == 78 || X == 77 || X == 46
3100 : : || X == 75 || X == 45
3101 : : will be transformed by the previous optimization into
3102 : : (X - 43U) <= 3U || (X - 75U) <= 3U
3103 : : and this loop can transform that into
3104 : : ((X - 43U) & ~(75U - 43U)) <= 3U. */
3105 : : static bool
3106 : 19274 : optimize_range_tests_diff (enum tree_code opcode, tree type,
3107 : : tree lowi, tree lowj, tree highi, tree highj,
3108 : : vec<operand_entry *> *ops,
3109 : : struct range_entry *rangei,
3110 : : struct range_entry *rangej)
3111 : : {
3112 : 19274 : tree tem1, tem2, mask;
3113 : : /* Check highi - lowi == highj - lowj. */
3114 : 19274 : tem1 = fold_binary (MINUS_EXPR, type, highi, lowi);
3115 : 19274 : if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
3116 : : return false;
3117 : 19274 : tem2 = fold_binary (MINUS_EXPR, type, highj, lowj);
3118 : 19274 : if (!tree_int_cst_equal (tem1, tem2))
3119 : : return false;
3120 : : /* Check popcount (lowj - lowi) == 1. */
3121 : 12424 : tem1 = fold_binary (MINUS_EXPR, type, lowj, lowi);
3122 : 12424 : if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
3123 : : return false;
3124 : 12424 : if (!integer_pow2p (tem1))
3125 : : return false;
3126 : :
3127 : 1988 : scalar_int_mode mode = as_a <scalar_int_mode> (TYPE_MODE (type));
3128 : 1988 : int prec = GET_MODE_PRECISION (mode);
3129 : 1988 : if (TYPE_PRECISION (type) < prec
3130 : 1984 : || (wi::to_wide (TYPE_MIN_VALUE (type))
3131 : 5956 : != wi::min_value (prec, TYPE_SIGN (type)))
3132 : 5956 : || (wi::to_wide (TYPE_MAX_VALUE (type))
3133 : 5956 : != wi::max_value (prec, TYPE_SIGN (type))))
3134 : 4 : type = build_nonstandard_integer_type (prec, 1);
3135 : : else
3136 : 1984 : type = unsigned_type_for (type);
3137 : 1988 : tem1 = fold_convert (type, tem1);
3138 : 1988 : tem2 = fold_convert (type, tem2);
3139 : 1988 : lowi = fold_convert (type, lowi);
3140 : 1988 : mask = fold_build1 (BIT_NOT_EXPR, type, tem1);
3141 : 1988 : tem1 = fold_build2 (MINUS_EXPR, type,
3142 : : fold_convert (type, rangei->exp), lowi);
3143 : 1988 : tem1 = fold_build2 (BIT_AND_EXPR, type, tem1, mask);
3144 : 1988 : lowj = build_int_cst (type, 0);
3145 : 1988 : if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, tem1,
3146 : 1988 : NULL, rangei->in_p, lowj, tem2,
3147 : 1988 : rangei->strict_overflow_p
3148 : 1988 : || rangej->strict_overflow_p))
3149 : : return true;
3150 : : return false;
3151 : : }
3152 : :
3153 : : /* It does some common checks for function optimize_range_tests_xor and
3154 : : optimize_range_tests_diff.
3155 : : If OPTIMIZE_XOR is TRUE, it calls optimize_range_tests_xor.
3156 : : Else it calls optimize_range_tests_diff. */
3157 : :
3158 : : static bool
3159 : 2222014 : optimize_range_tests_1 (enum tree_code opcode, int first, int length,
3160 : : bool optimize_xor, vec<operand_entry *> *ops,
3161 : : struct range_entry *ranges)
3162 : : {
3163 : 2222014 : int i, j;
3164 : 2222014 : bool any_changes = false;
3165 : 3848002 : for (i = first; i < length; i++)
3166 : : {
3167 : 1625988 : tree lowi, highi, lowj, highj, type, tem;
3168 : :
3169 : 1625988 : if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
3170 : 991362 : continue;
3171 : 634626 : type = TREE_TYPE (ranges[i].exp);
3172 : 634626 : if (!INTEGRAL_TYPE_P (type))
3173 : 48926 : continue;
3174 : 585700 : lowi = ranges[i].low;
3175 : 585700 : if (lowi == NULL_TREE)
3176 : 35062 : lowi = TYPE_MIN_VALUE (type);
3177 : 585700 : highi = ranges[i].high;
3178 : 585700 : if (highi == NULL_TREE)
3179 : 7504 : continue;
3180 : 958799 : for (j = i + 1; j < length && j < i + 64; j++)
3181 : : {
3182 : 385179 : bool changes;
3183 : 385179 : if (ranges[i].exp != ranges[j].exp || ranges[j].in_p)
3184 : 341704 : continue;
3185 : 43475 : lowj = ranges[j].low;
3186 : 43475 : if (lowj == NULL_TREE)
3187 : 0 : continue;
3188 : 43475 : highj = ranges[j].high;
3189 : 43475 : if (highj == NULL_TREE)
3190 : 144 : highj = TYPE_MAX_VALUE (type);
3191 : : /* Check lowj > highi. */
3192 : 43475 : tem = fold_binary (GT_EXPR, boolean_type_node,
3193 : : lowj, highi);
3194 : 43475 : if (tem == NULL_TREE || !integer_onep (tem))
3195 : 0 : continue;
3196 : 43475 : if (optimize_xor)
3197 : 24201 : changes = optimize_range_tests_xor (opcode, type, lowi, lowj,
3198 : : highi, highj, ops,
3199 : : ranges + i, ranges + j);
3200 : : else
3201 : 19274 : changes = optimize_range_tests_diff (opcode, type, lowi, lowj,
3202 : : highi, highj, ops,
3203 : : ranges + i, ranges + j);
3204 : 43475 : if (changes)
3205 : : {
3206 : : any_changes = true;
3207 : : break;
3208 : : }
3209 : : }
3210 : : }
3211 : 2222014 : return any_changes;
3212 : : }
3213 : :
3214 : : /* Helper function of optimize_range_tests_to_bit_test. Handle a single
3215 : : range, EXP, LOW, HIGH, compute bit mask of bits to test and return
3216 : : EXP on success, NULL otherwise. */
3217 : :
3218 : : static tree
3219 : 174311 : extract_bit_test_mask (tree exp, int prec, tree totallow, tree low, tree high,
3220 : : wide_int *mask, tree *totallowp)
3221 : : {
3222 : 174311 : tree tem = int_const_binop (MINUS_EXPR, high, low);
3223 : 174311 : if (tem == NULL_TREE
3224 : 174311 : || TREE_CODE (tem) != INTEGER_CST
3225 : 174311 : || TREE_OVERFLOW (tem)
3226 : 163360 : || tree_int_cst_sgn (tem) == -1
3227 : 337671 : || compare_tree_int (tem, prec) != -1)
3228 : 15278 : return NULL_TREE;
3229 : :
3230 : 159033 : unsigned HOST_WIDE_INT max = tree_to_uhwi (tem) + 1;
3231 : 159033 : *mask = wi::shifted_mask (0, max, false, prec);
3232 : 159033 : if (TREE_CODE (exp) == BIT_AND_EXPR
3233 : 159033 : && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
3234 : : {
3235 : 4763 : widest_int msk = wi::to_widest (TREE_OPERAND (exp, 1));
3236 : 4763 : msk = wi::zext (~msk, TYPE_PRECISION (TREE_TYPE (exp)));
3237 : 4763 : if (wi::popcount (msk) == 1
3238 : 4763 : && wi::ltu_p (msk, prec - max))
3239 : : {
3240 : 4131 : *mask |= wi::shifted_mask (msk.to_uhwi (), max, false, prec);
3241 : 4131 : max += msk.to_uhwi ();
3242 : 4131 : exp = TREE_OPERAND (exp, 0);
3243 : 4131 : if (integer_zerop (low)
3244 : 2140 : && TREE_CODE (exp) == PLUS_EXPR
3245 : 5919 : && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
3246 : : {
3247 : 1788 : tree ret = TREE_OPERAND (exp, 0);
3248 : 1788 : STRIP_NOPS (ret);
3249 : 1788 : widest_int bias
3250 : 1788 : = wi::neg (wi::sext (wi::to_widest (TREE_OPERAND (exp, 1)),
3251 : 3576 : TYPE_PRECISION (TREE_TYPE (low))));
3252 : 1788 : tree tbias = wide_int_to_tree (TREE_TYPE (ret), bias);
3253 : 1788 : if (totallowp)
3254 : : {
3255 : 1756 : *totallowp = tbias;
3256 : 1756 : return ret;
3257 : : }
3258 : 32 : else if (!tree_int_cst_lt (totallow, tbias))
3259 : : return NULL_TREE;
3260 : 32 : bias = wi::to_widest (tbias);
3261 : 32 : bias -= wi::to_widest (totallow);
3262 : 32 : if (bias >= 0 && bias < prec - max)
3263 : : {
3264 : 22 : *mask = wi::lshift (*mask, bias);
3265 : 22 : return ret;
3266 : : }
3267 : 1788 : }
3268 : : }
3269 : 4763 : }
3270 : 157255 : if (totallowp)
3271 : : return exp;
3272 : 14551 : if (!tree_int_cst_lt (totallow, low))
3273 : : return exp;
3274 : 14529 : tem = int_const_binop (MINUS_EXPR, low, totallow);
3275 : 14529 : if (tem == NULL_TREE
3276 : 14529 : || TREE_CODE (tem) != INTEGER_CST
3277 : 14529 : || TREE_OVERFLOW (tem)
3278 : 28949 : || compare_tree_int (tem, prec - max) == 1)
3279 : 3911 : return NULL_TREE;
3280 : :
3281 : 10618 : *mask = wi::lshift (*mask, wi::to_widest (tem));
3282 : 10618 : return exp;
3283 : : }
3284 : :
3285 : : /* Attempt to optimize small range tests using bit test.
3286 : : E.g.
3287 : : X != 43 && X != 76 && X != 44 && X != 78 && X != 49
3288 : : && X != 77 && X != 46 && X != 75 && X != 45 && X != 82
3289 : : has been by earlier optimizations optimized into:
3290 : : ((X - 43U) & ~32U) > 3U && X != 49 && X != 82
3291 : : As all the 43 through 82 range is less than 64 numbers,
3292 : : for 64-bit word targets optimize that into:
3293 : : (X - 43U) > 40U && ((1 << (X - 43U)) & 0x8F0000004FULL) == 0 */
3294 : :
3295 : : static bool
3296 : 1111015 : optimize_range_tests_to_bit_test (enum tree_code opcode, int first, int length,
3297 : : vec<operand_entry *> *ops,
3298 : : struct range_entry *ranges)
3299 : : {
3300 : 1111015 : int i, j;
3301 : 1111015 : bool any_changes = false;
3302 : 1111015 : int prec = GET_MODE_BITSIZE (word_mode);
3303 : 1111015 : auto_vec<struct range_entry *, 64> candidates;
3304 : :
3305 : 1551539 : for (i = first; i < length - 1; i++)
3306 : : {
3307 : 440524 : tree lowi, highi, lowj, highj, type;
3308 : :
3309 : 440524 : if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
3310 : 296064 : continue;
3311 : 177893 : type = TREE_TYPE (ranges[i].exp);
3312 : 177893 : if (!INTEGRAL_TYPE_P (type))
3313 : 16151 : continue;
3314 : 161742 : lowi = ranges[i].low;
3315 : 161742 : if (lowi == NULL_TREE)
3316 : 11034 : lowi = TYPE_MIN_VALUE (type);
3317 : 161742 : highi = ranges[i].high;
3318 : 161742 : if (highi == NULL_TREE)
3319 : 2132 : continue;
3320 : 159610 : wide_int mask;
3321 : 159610 : tree exp = extract_bit_test_mask (ranges[i].exp, prec, lowi, lowi,
3322 : : highi, &mask, &lowi);
3323 : 159610 : if (exp == NULL_TREE)
3324 : 15150 : continue;
3325 : 144460 : bool strict_overflow_p = ranges[i].strict_overflow_p;
3326 : 144460 : candidates.truncate (0);
3327 : 144460 : int end = MIN (i + 64, length);
3328 : 311455 : for (j = i + 1; j < end; j++)
3329 : : {
3330 : 166995 : tree exp2;
3331 : 166995 : if (ranges[j].exp == NULL_TREE || ranges[j].in_p)
3332 : 156367 : continue;
3333 : 97227 : if (ranges[j].exp == exp)
3334 : : ;
3335 : 82792 : else if (TREE_CODE (ranges[j].exp) == BIT_AND_EXPR)
3336 : : {
3337 : 1276 : exp2 = TREE_OPERAND (ranges[j].exp, 0);
3338 : 1276 : if (exp2 == exp)
3339 : : ;
3340 : 1054 : else if (TREE_CODE (exp2) == PLUS_EXPR)
3341 : : {
3342 : 806 : exp2 = TREE_OPERAND (exp2, 0);
3343 : 806 : STRIP_NOPS (exp2);
3344 : 806 : if (exp2 != exp)
3345 : 762 : continue;
3346 : : }
3347 : : else
3348 : 248 : continue;
3349 : : }
3350 : : else
3351 : 81516 : continue;
3352 : 14701 : lowj = ranges[j].low;
3353 : 14701 : if (lowj == NULL_TREE)
3354 : 0 : continue;
3355 : 14701 : highj = ranges[j].high;
3356 : 14701 : if (highj == NULL_TREE)
3357 : 72 : highj = TYPE_MAX_VALUE (TREE_TYPE (lowj));
3358 : 14701 : wide_int mask2;
3359 : 14701 : exp2 = extract_bit_test_mask (ranges[j].exp, prec, lowi, lowj,
3360 : : highj, &mask2, NULL);
3361 : 14701 : if (exp2 != exp)
3362 : 4073 : continue;
3363 : 10628 : mask |= mask2;
3364 : 10628 : strict_overflow_p |= ranges[j].strict_overflow_p;
3365 : 10628 : candidates.safe_push (&ranges[j]);
3366 : 14701 : }
3367 : :
3368 : : /* If every possible relative value of the expression is a valid shift
3369 : : amount, then we can merge the entry test in the bit test. In this
3370 : : case, if we would need otherwise 2 or more comparisons, then use
3371 : : the bit test; in the other cases, the threshold is 3 comparisons. */
3372 : 144460 : bool entry_test_needed;
3373 : 144460 : int_range_max r;
3374 : 288920 : if (TREE_CODE (exp) == SSA_NAME
3375 : 287762 : && get_range_query (cfun)->range_of_expr (r, exp)
3376 : 143881 : && !r.undefined_p ()
3377 : 143881 : && !r.varying_p ()
3378 : 334406 : && wi::leu_p (r.upper_bound () - r.lower_bound (), prec - 1))
3379 : : {
3380 : 6230 : wide_int min = r.lower_bound ();
3381 : 6230 : wide_int ilowi = wi::to_wide (lowi);
3382 : 6230 : if (wi::lt_p (min, ilowi, TYPE_SIGN (TREE_TYPE (lowi))))
3383 : : {
3384 : 843 : lowi = wide_int_to_tree (TREE_TYPE (lowi), min);
3385 : 843 : mask = wi::lshift (mask, ilowi - min);
3386 : : }
3387 : 5387 : else if (wi::gt_p (min, ilowi, TYPE_SIGN (TREE_TYPE (lowi))))
3388 : : {
3389 : 0 : lowi = wide_int_to_tree (TREE_TYPE (lowi), min);
3390 : 0 : mask = wi::lrshift (mask, min - ilowi);
3391 : : }
3392 : 6230 : entry_test_needed = false;
3393 : 6230 : }
3394 : : else
3395 : : entry_test_needed = true;
3396 : 295150 : if (candidates.length () >= (entry_test_needed ? 2 : 1))
3397 : : {
3398 : 884 : tree high = wide_int_to_tree (TREE_TYPE (lowi),
3399 : 442 : wi::to_widest (lowi)
3400 : 1326 : + prec - 1 - wi::clz (mask));
3401 : 442 : operand_entry *oe = (*ops)[ranges[i].idx];
3402 : 442 : tree op = oe->op;
3403 : 442 : gimple *stmt = op ? SSA_NAME_DEF_STMT (op)
3404 : 28 : : last_nondebug_stmt (BASIC_BLOCK_FOR_FN
3405 : 414 : (cfun, oe->id));
3406 : 442 : location_t loc = gimple_location (stmt);
3407 : 442 : tree optype = op ? TREE_TYPE (op) : boolean_type_node;
3408 : :
3409 : : /* See if it isn't cheaper to pretend the minimum value of the
3410 : : range is 0, if maximum value is small enough.
3411 : : We can avoid then subtraction of the minimum value, but the
3412 : : mask constant could be perhaps more expensive. */
3413 : 442 : if (compare_tree_int (lowi, 0) > 0
3414 : 344 : && compare_tree_int (high, prec) < 0
3415 : 898 : && (entry_test_needed || wi::ltu_p (r.upper_bound (), prec)))
3416 : : {
3417 : 138 : int cost_diff;
3418 : 138 : HOST_WIDE_INT m = tree_to_uhwi (lowi);
3419 : 138 : rtx reg = gen_raw_REG (word_mode, 10000);
3420 : 138 : bool speed_p = optimize_bb_for_speed_p (gimple_bb (stmt));
3421 : 138 : cost_diff = set_src_cost (gen_rtx_PLUS (word_mode, reg,
3422 : : GEN_INT (-m)),
3423 : : word_mode, speed_p);
3424 : 138 : rtx r = immed_wide_int_const (mask, word_mode);
3425 : 138 : cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
3426 : : word_mode, speed_p);
3427 : 138 : r = immed_wide_int_const (wi::lshift (mask, m), word_mode);
3428 : 138 : cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
3429 : : word_mode, speed_p);
3430 : 138 : if (cost_diff > 0)
3431 : : {
3432 : 58 : mask = wi::lshift (mask, m);
3433 : 58 : lowi = build_zero_cst (TREE_TYPE (lowi));
3434 : : }
3435 : : }
3436 : :
3437 : 442 : tree tem;
3438 : 442 : if (entry_test_needed)
3439 : : {
3440 : 381 : tem = build_range_check (loc, optype, unshare_expr (exp),
3441 : : false, lowi, high);
3442 : 381 : if (tem == NULL_TREE || is_gimple_val (tem))
3443 : 0 : continue;
3444 : : }
3445 : : else
3446 : 61 : tem = NULL_TREE;
3447 : 442 : tree etype = unsigned_type_for (TREE_TYPE (exp));
3448 : 442 : exp = fold_build2_loc (loc, MINUS_EXPR, etype,
3449 : : fold_convert_loc (loc, etype, exp),
3450 : : fold_convert_loc (loc, etype, lowi));
3451 : 442 : exp = fold_convert_loc (loc, integer_type_node, exp);
3452 : 442 : tree word_type = lang_hooks.types.type_for_mode (word_mode, 1);
3453 : 442 : exp = fold_build2_loc (loc, LSHIFT_EXPR, word_type,
3454 : : build_int_cst (word_type, 1), exp);
3455 : 884 : exp = fold_build2_loc (loc, BIT_AND_EXPR, word_type, exp,
3456 : 442 : wide_int_to_tree (word_type, mask));
3457 : 442 : exp = fold_build2_loc (loc, EQ_EXPR, optype, exp,
3458 : : build_zero_cst (word_type));
3459 : 442 : if (is_gimple_val (exp))
3460 : 0 : continue;
3461 : :
3462 : : /* The shift might have undefined behavior if TEM is true,
3463 : : but reassociate_bb isn't prepared to have basic blocks
3464 : : split when it is running. So, temporarily emit a code
3465 : : with BIT_IOR_EXPR instead of &&, and fix it up in
3466 : : branch_fixup. */
3467 : 442 : gimple_seq seq = NULL;
3468 : 442 : if (tem)
3469 : : {
3470 : 381 : tem = force_gimple_operand (tem, &seq, true, NULL_TREE);
3471 : 381 : gcc_assert (TREE_CODE (tem) == SSA_NAME);
3472 : 381 : gimple_set_visited (SSA_NAME_DEF_STMT (tem), true);
3473 : : }
3474 : 442 : gimple_seq seq2;
3475 : 442 : exp = force_gimple_operand (exp, &seq2, true, NULL_TREE);
3476 : 442 : gimple_seq_add_seq_without_update (&seq, seq2);
3477 : 442 : gcc_assert (TREE_CODE (exp) == SSA_NAME);
3478 : 442 : gimple_set_visited (SSA_NAME_DEF_STMT (exp), true);
3479 : 442 : if (tem)
3480 : : {
3481 : 381 : gimple *g = gimple_build_assign (make_ssa_name (optype),
3482 : : BIT_IOR_EXPR, tem, exp);
3483 : 381 : gimple_set_location (g, loc);
3484 : 381 : gimple_seq_add_stmt_without_update (&seq, g);
3485 : 381 : exp = gimple_assign_lhs (g);
3486 : : }
3487 : 442 : tree val = build_zero_cst (optype);
3488 : 1326 : if (update_range_test (&ranges[i], NULL, candidates.address (),
3489 : : candidates.length (), opcode, ops, exp,
3490 : : seq, false, val, val, strict_overflow_p))
3491 : : {
3492 : 442 : any_changes = true;
3493 : 442 : if (tem)
3494 : 381 : reassoc_branch_fixups.safe_push (tem);
3495 : : }
3496 : : else
3497 : 0 : gimple_seq_discard (seq);
3498 : : }
3499 : 159610 : }
3500 : 1111015 : return any_changes;
3501 : 1111015 : }
3502 : :
3503 : : /* Optimize x != 0 && y != 0 && z != 0 into (x | y | z) != 0
3504 : : and similarly x != -1 && y != -1 && y != -1 into (x & y & z) != -1.
3505 : : Also, handle x < C && y < C && z < C where C is power of two as
3506 : : (x | y | z) < C. And also handle signed x < 0 && y < 0 && z < 0
3507 : : as (x | y | z) < 0. */
3508 : :
3509 : : static bool
3510 : 1111015 : optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
3511 : : vec<operand_entry *> *ops,
3512 : : struct range_entry *ranges)
3513 : : {
3514 : 1111015 : int i;
3515 : 1111015 : unsigned int b;
3516 : 1111015 : bool any_changes = false;
3517 : 1111015 : auto_vec<int, 128> buckets;
3518 : 1111015 : auto_vec<int, 32> chains;
3519 : 1111015 : auto_vec<struct range_entry *, 32> candidates;
3520 : :
3521 : 1924016 : for (i = first; i < length; i++)
3522 : : {
3523 : 813001 : int idx;
3524 : :
3525 : 1186451 : if (ranges[i].exp == NULL_TREE
3526 : 797641 : || TREE_CODE (ranges[i].exp) != SSA_NAME
3527 : 793190 : || TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) <= 1
3528 : 1252552 : || TREE_CODE (TREE_TYPE (ranges[i].exp)) == BOOLEAN_TYPE)
3529 : 373450 : continue;
3530 : :
3531 : 439551 : if (ranges[i].low != NULL_TREE
3532 : 412846 : && ranges[i].high != NULL_TREE
3533 : 357665 : && ranges[i].in_p
3534 : 639937 : && tree_int_cst_equal (ranges[i].low, ranges[i].high))
3535 : : {
3536 : 174807 : idx = !integer_zerop (ranges[i].low);
3537 : 174807 : if (idx && !integer_all_onesp (ranges[i].low))
3538 : 93194 : continue;
3539 : : }
3540 : 264744 : else if (ranges[i].high != NULL_TREE
3541 : 209533 : && TREE_CODE (ranges[i].high) == INTEGER_CST
3542 : 209533 : && ranges[i].in_p)
3543 : : {
3544 : 34753 : wide_int w = wi::to_wide (ranges[i].high);
3545 : 34753 : int prec = TYPE_PRECISION (TREE_TYPE (ranges[i].exp));
3546 : 34753 : int l = wi::clz (w);
3547 : 34753 : idx = 2;
3548 : 93409 : if (l <= 0
3549 : 34753 : || l >= prec
3550 : 63431 : || w != wi::mask (prec - l, false, prec))
3551 : 23903 : continue;
3552 : 10850 : if (!((TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
3553 : 6860 : && ranges[i].low == NULL_TREE)
3554 : 10850 : || (ranges[i].low
3555 : 8954 : && integer_zerop (ranges[i].low))))
3556 : 3455 : continue;
3557 : 34753 : }
3558 : 435793 : else if (ranges[i].high == NULL_TREE
3559 : 55211 : && ranges[i].low != NULL_TREE
3560 : : /* Perform this optimization only in the last
3561 : : reassoc pass, as it interferes with the reassociation
3562 : : itself or could also with VRP etc. which might not
3563 : : be able to virtually undo the optimization. */
3564 : 55181 : && !reassoc_insert_powi_p
3565 : 27518 : && !TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
3566 : 257495 : && integer_zerop (ranges[i].low))
3567 : : idx = 3;
3568 : : else
3569 : 205802 : continue;
3570 : :
3571 : 113197 : b = TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) * 4 + idx;
3572 : 113197 : if (buckets.length () <= b)
3573 : 96554 : buckets.safe_grow_cleared (b + 1, true);
3574 : 113197 : if (chains.length () <= (unsigned) i)
3575 : 113197 : chains.safe_grow (i + 1, true);
3576 : 113197 : chains[i] = buckets[b];
3577 : 113197 : buckets[b] = i + 1;
3578 : : }
3579 : :
3580 : 17367848 : FOR_EACH_VEC_ELT (buckets, b, i)
3581 : 16256833 : if (i && chains[i - 1])
3582 : : {
3583 : 6520 : int j, k = i;
3584 : 6520 : if ((b % 4) == 2)
3585 : : {
3586 : : /* When ranges[X - 1].high + 1 is a power of two,
3587 : : we need to process the same bucket up to
3588 : : precision - 1 times, each time split the entries
3589 : : with the same high bound into one chain and the
3590 : : rest into another one to be processed later. */
3591 : : int this_prev = i;
3592 : : int other_prev = 0;
3593 : 154 : for (j = chains[i - 1]; j; j = chains[j - 1])
3594 : : {
3595 : 83 : if (tree_int_cst_equal (ranges[i - 1].high,
3596 : 83 : ranges[j - 1].high))
3597 : : {
3598 : 68 : chains[this_prev - 1] = j;
3599 : 68 : this_prev = j;
3600 : : }
3601 : 15 : else if (other_prev == 0)
3602 : : {
3603 : 13 : buckets[b] = j;
3604 : 13 : other_prev = j;
3605 : : }
3606 : : else
3607 : : {
3608 : 2 : chains[other_prev - 1] = j;
3609 : 2 : other_prev = j;
3610 : : }
3611 : : }
3612 : 71 : chains[this_prev - 1] = 0;
3613 : 71 : if (other_prev)
3614 : 13 : chains[other_prev - 1] = 0;
3615 : 71 : if (chains[i - 1] == 0)
3616 : : {
3617 : 11 : if (other_prev)
3618 : 11 : b--;
3619 : 11 : continue;
3620 : : }
3621 : : }
3622 : 17766 : for (j = chains[i - 1]; j; j = chains[j - 1])
3623 : : {
3624 : 11257 : gimple *gk = SSA_NAME_DEF_STMT (ranges[k - 1].exp);
3625 : 11257 : gimple *gj = SSA_NAME_DEF_STMT (ranges[j - 1].exp);
3626 : 11257 : if (reassoc_stmt_dominates_stmt_p (gk, gj))
3627 : 2874 : k = j;
3628 : : }
3629 : 6509 : tree type1 = TREE_TYPE (ranges[k - 1].exp);
3630 : 6509 : tree type2 = NULL_TREE;
3631 : 6509 : bool strict_overflow_p = false;
3632 : 6509 : candidates.truncate (0);
3633 : 6509 : if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
3634 : 604 : type1 = pointer_sized_int_node;
3635 : 24275 : for (j = i; j; j = chains[j - 1])
3636 : : {
3637 : 17766 : tree type = TREE_TYPE (ranges[j - 1].exp);
3638 : 17766 : strict_overflow_p |= ranges[j - 1].strict_overflow_p;
3639 : 17766 : if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
3640 : 1210 : type = pointer_sized_int_node;
3641 : 17766 : if ((b % 4) == 3)
3642 : : {
3643 : : /* For the signed < 0 cases, the types should be
3644 : : really compatible (all signed with the same precision,
3645 : : instead put ranges that have different in_p from
3646 : : k first. */
3647 : 3435 : if (!useless_type_conversion_p (type1, type))
3648 : 0 : continue;
3649 : 3435 : if (ranges[j - 1].in_p != ranges[k - 1].in_p)
3650 : 1009 : candidates.safe_push (&ranges[j - 1]);
3651 : 3435 : type2 = type1;
3652 : 3435 : continue;
3653 : : }
3654 : 14331 : if (j == k
3655 : 14331 : || useless_type_conversion_p (type1, type))
3656 : : ;
3657 : 363 : else if (type2 == NULL_TREE
3658 : 363 : || useless_type_conversion_p (type2, type))
3659 : : {
3660 : 363 : if (type2 == NULL_TREE)
3661 : 349 : type2 = type;
3662 : 363 : candidates.safe_push (&ranges[j - 1]);
3663 : : }
3664 : : }
3665 : 6509 : unsigned l = candidates.length ();
3666 : 24275 : for (j = i; j; j = chains[j - 1])
3667 : : {
3668 : 17766 : tree type = TREE_TYPE (ranges[j - 1].exp);
3669 : 17766 : if (j == k)
3670 : 6509 : continue;
3671 : 11257 : if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
3672 : 606 : type = pointer_sized_int_node;
3673 : 11257 : if ((b % 4) == 3)
3674 : : {
3675 : 1882 : if (!useless_type_conversion_p (type1, type))
3676 : 0 : continue;
3677 : 1882 : if (ranges[j - 1].in_p == ranges[k - 1].in_p)
3678 : 873 : candidates.safe_push (&ranges[j - 1]);
3679 : 1882 : continue;
3680 : : }
3681 : 9375 : if (useless_type_conversion_p (type1, type))
3682 : : ;
3683 : 726 : else if (type2 == NULL_TREE
3684 : 363 : || useless_type_conversion_p (type2, type))
3685 : 363 : continue;
3686 : 9012 : candidates.safe_push (&ranges[j - 1]);
3687 : : }
3688 : 6509 : gimple_seq seq = NULL;
3689 : 6509 : tree op = NULL_TREE;
3690 : 6509 : unsigned int id;
3691 : 6509 : struct range_entry *r;
3692 : 6509 : candidates.safe_push (&ranges[k - 1]);
3693 : 24275 : FOR_EACH_VEC_ELT (candidates, id, r)
3694 : : {
3695 : 17766 : gimple *g;
3696 : 17766 : enum tree_code code;
3697 : 17766 : if (id == 0)
3698 : : {
3699 : 6509 : op = r->exp;
3700 : 6509 : continue;
3701 : : }
3702 : 11257 : if (id == l
3703 : 9899 : || POINTER_TYPE_P (TREE_TYPE (op))
3704 : 20659 : || TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
3705 : : {
3706 : 1860 : code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR;
3707 : 1860 : tree type3 = id >= l ? type1 : pointer_sized_int_node;
3708 : 1860 : if (code == BIT_NOT_EXPR
3709 : 1860 : && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
3710 : : {
3711 : 0 : g = gimple_build_assign (make_ssa_name (type3),
3712 : : NOP_EXPR, op);
3713 : 0 : gimple_seq_add_stmt_without_update (&seq, g);
3714 : 0 : op = gimple_assign_lhs (g);
3715 : : }
3716 : 1860 : g = gimple_build_assign (make_ssa_name (type3), code, op);
3717 : 1860 : gimple_seq_add_stmt_without_update (&seq, g);
3718 : 1860 : op = gimple_assign_lhs (g);
3719 : : }
3720 : 11257 : tree type = TREE_TYPE (r->exp);
3721 : 11257 : tree exp = r->exp;
3722 : 11257 : if (POINTER_TYPE_P (type)
3723 : 10630 : || TREE_CODE (type) == OFFSET_TYPE
3724 : 21882 : || (id >= l && !useless_type_conversion_p (type1, type)))
3725 : : {
3726 : 632 : tree type3 = id >= l ? type1 : pointer_sized_int_node;
3727 : 632 : g = gimple_build_assign (make_ssa_name (type3), NOP_EXPR, exp);
3728 : 632 : gimple_seq_add_stmt_without_update (&seq, g);
3729 : 632 : exp = gimple_assign_lhs (g);
3730 : : }
3731 : 11257 : if ((b % 4) == 3)
3732 : 3153 : code = r->in_p ? BIT_IOR_EXPR : BIT_AND_EXPR;
3733 : : else
3734 : 9375 : code = (b % 4) == 1 ? BIT_AND_EXPR : BIT_IOR_EXPR;
3735 : 22514 : g = gimple_build_assign (make_ssa_name (id >= l ? type1 : type2),
3736 : : code, op, exp);
3737 : 11257 : gimple_seq_add_stmt_without_update (&seq, g);
3738 : 11257 : op = gimple_assign_lhs (g);
3739 : : }
3740 : 6509 : type1 = TREE_TYPE (ranges[k - 1].exp);
3741 : 6509 : if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
3742 : : {
3743 : 604 : gimple *g
3744 : 604 : = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op);
3745 : 604 : gimple_seq_add_stmt_without_update (&seq, g);
3746 : 604 : op = gimple_assign_lhs (g);
3747 : : }
3748 : 6509 : candidates.pop ();
3749 : 6509 : if (update_range_test (&ranges[k - 1], NULL, candidates.address (),
3750 : : candidates.length (), opcode, ops, op,
3751 : 6509 : seq, ranges[k - 1].in_p, ranges[k - 1].low,
3752 : : ranges[k - 1].high, strict_overflow_p))
3753 : : any_changes = true;
3754 : : else
3755 : 0 : gimple_seq_discard (seq);
3756 : 6569 : if ((b % 4) == 2 && buckets[b] != i)
3757 : : /* There is more work to do for this bucket. */
3758 : 2 : b--;
3759 : : }
3760 : :
3761 : 1111015 : return any_changes;
3762 : 1111015 : }
3763 : :
3764 : : /* Attempt to optimize for signed a and b where b is known to be >= 0:
3765 : : a >= 0 && a < b into (unsigned) a < (unsigned) b
3766 : : a >= 0 && a <= b into (unsigned) a <= (unsigned) b */
3767 : :
3768 : : static bool
3769 : 1111015 : optimize_range_tests_var_bound (enum tree_code opcode, int first, int length,
3770 : : vec<operand_entry *> *ops,
3771 : : struct range_entry *ranges,
3772 : : basic_block first_bb)
3773 : : {
3774 : 1111015 : int i;
3775 : 1111015 : bool any_changes = false;
3776 : 1111015 : hash_map<tree, int> *map = NULL;
3777 : :
3778 : 1924016 : for (i = first; i < length; i++)
3779 : : {
3780 : 813001 : if (ranges[i].exp == NULL_TREE
3781 : 798157 : || TREE_CODE (ranges[i].exp) != SSA_NAME
3782 : 793706 : || !ranges[i].in_p)
3783 : 330195 : continue;
3784 : :
3785 : 482806 : tree type = TREE_TYPE (ranges[i].exp);
3786 : 919666 : if (!INTEGRAL_TYPE_P (type)
3787 : 473416 : || TYPE_UNSIGNED (type)
3788 : 174095 : || ranges[i].low == NULL_TREE
3789 : 164921 : || !integer_zerop (ranges[i].low)
3790 : 553716 : || ranges[i].high != NULL_TREE)
3791 : 436860 : continue;
3792 : : /* EXP >= 0 here. */
3793 : 45946 : if (map == NULL)
3794 : 44485 : map = new hash_map <tree, int>;
3795 : 45946 : map->put (ranges[i].exp, i);
3796 : : }
3797 : :
3798 : 1111015 : if (map == NULL)
3799 : : return false;
3800 : :
3801 : 136487 : for (i = 0; i < length; i++)
3802 : : {
3803 : 92002 : bool in_p = ranges[i].in_p;
3804 : 92002 : if (ranges[i].low == NULL_TREE
3805 : 91336 : || ranges[i].high == NULL_TREE)
3806 : 91486 : continue;
3807 : 43586 : if (!integer_zerop (ranges[i].low)
3808 : 43586 : || !integer_zerop (ranges[i].high))
3809 : : {
3810 : 8196 : if (ranges[i].exp
3811 : 4098 : && TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) == 1
3812 : 0 : && TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
3813 : 0 : && integer_onep (ranges[i].low)
3814 : 4098 : && integer_onep (ranges[i].high))
3815 : 0 : in_p = !in_p;
3816 : : else
3817 : 4098 : continue;
3818 : : }
3819 : :
3820 : 39488 : gimple *stmt;
3821 : 39488 : tree_code ccode;
3822 : 39488 : tree rhs1, rhs2;
3823 : 39488 : if (ranges[i].exp)
3824 : : {
3825 : 39005 : if (TREE_CODE (ranges[i].exp) != SSA_NAME)
3826 : 5 : continue;
3827 : 39000 : stmt = SSA_NAME_DEF_STMT (ranges[i].exp);
3828 : 39000 : if (!is_gimple_assign (stmt))
3829 : 900 : continue;
3830 : 38100 : ccode = gimple_assign_rhs_code (stmt);
3831 : 38100 : rhs1 = gimple_assign_rhs1 (stmt);
3832 : 38100 : rhs2 = gimple_assign_rhs2 (stmt);
3833 : : }
3834 : : else
3835 : : {
3836 : 483 : operand_entry *oe = (*ops)[ranges[i].idx];
3837 : 483 : stmt = last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
3838 : 483 : if (gimple_code (stmt) != GIMPLE_COND)
3839 : 0 : continue;
3840 : 483 : ccode = gimple_cond_code (stmt);
3841 : 483 : rhs1 = gimple_cond_lhs (stmt);
3842 : 483 : rhs2 = gimple_cond_rhs (stmt);
3843 : : }
3844 : :
3845 : 38583 : if (TREE_CODE (rhs1) != SSA_NAME
3846 : 38020 : || rhs2 == NULL_TREE
3847 : 37952 : || TREE_CODE (rhs2) != SSA_NAME)
3848 : 698 : continue;
3849 : :
3850 : 37885 : switch (ccode)
3851 : : {
3852 : 36904 : case GT_EXPR:
3853 : 36904 : case GE_EXPR:
3854 : 36904 : case LT_EXPR:
3855 : 36904 : case LE_EXPR:
3856 : 36904 : break;
3857 : 981 : default:
3858 : 981 : continue;
3859 : : }
3860 : 36904 : if (in_p)
3861 : 746 : ccode = invert_tree_comparison (ccode, false);
3862 : 36904 : switch (ccode)
3863 : : {
3864 : 15072 : case GT_EXPR:
3865 : 15072 : case GE_EXPR:
3866 : 15072 : std::swap (rhs1, rhs2);
3867 : 15072 : ccode = swap_tree_comparison (ccode);
3868 : 15072 : break;
3869 : : case LT_EXPR:
3870 : : case LE_EXPR:
3871 : : break;
3872 : 0 : default:
3873 : 0 : gcc_unreachable ();
3874 : : }
3875 : :
3876 : 36904 : int *idx = map->get (rhs1);
3877 : 36904 : if (idx == NULL)
3878 : 935 : continue;
3879 : :
3880 : : /* maybe_optimize_range_tests allows statements without side-effects
3881 : : in the basic blocks as long as they are consumed in the same bb.
3882 : : Make sure rhs2's def stmt is not among them, otherwise we can't
3883 : : use safely get_nonzero_bits on it. E.g. in:
3884 : : # RANGE [-83, 1] NONZERO 173
3885 : : # k_32 = PHI <k_47(13), k_12(9)>
3886 : : ...
3887 : : if (k_32 >= 0)
3888 : : goto <bb 5>; [26.46%]
3889 : : else
3890 : : goto <bb 9>; [73.54%]
3891 : :
3892 : : <bb 5> [local count: 140323371]:
3893 : : # RANGE [0, 1] NONZERO 1
3894 : : _5 = (int) k_32;
3895 : : # RANGE [0, 4] NONZERO 4
3896 : : _21 = _5 << 2;
3897 : : # RANGE [0, 4] NONZERO 4
3898 : : iftmp.0_44 = (char) _21;
3899 : : if (k_32 < iftmp.0_44)
3900 : : goto <bb 6>; [84.48%]
3901 : : else
3902 : : goto <bb 9>; [15.52%]
3903 : : the ranges on _5/_21/iftmp.0_44 are flow sensitive, assume that
3904 : : k_32 >= 0. If we'd optimize k_32 >= 0 to true and k_32 < iftmp.0_44
3905 : : to (unsigned) k_32 < (unsigned) iftmp.0_44, then we would execute
3906 : : those stmts even for negative k_32 and the value ranges would be no
3907 : : longer guaranteed and so the optimization would be invalid. */
3908 : 35969 : while (opcode == ERROR_MARK)
3909 : : {
3910 : 319 : gimple *g = SSA_NAME_DEF_STMT (rhs2);
3911 : 319 : basic_block bb2 = gimple_bb (g);
3912 : 319 : if (bb2
3913 : 319 : && bb2 != first_bb
3914 : 319 : && dominated_by_p (CDI_DOMINATORS, bb2, first_bb))
3915 : : {
3916 : : /* As an exception, handle a few common cases. */
3917 : 257 : if (gimple_assign_cast_p (g)
3918 : 257 : && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (g))))
3919 : : {
3920 : 24 : tree op0 = gimple_assign_rhs1 (g);
3921 : 24 : if (TYPE_UNSIGNED (TREE_TYPE (op0))
3922 : 24 : && (TYPE_PRECISION (TREE_TYPE (rhs2))
3923 : 10 : > TYPE_PRECISION (TREE_TYPE (op0))))
3924 : : /* Zero-extension is always ok. */
3925 : : break;
3926 : 14 : else if (TYPE_PRECISION (TREE_TYPE (rhs2))
3927 : 14 : == TYPE_PRECISION (TREE_TYPE (op0))
3928 : 14 : && TREE_CODE (op0) == SSA_NAME)
3929 : : {
3930 : : /* Cast from signed to unsigned or vice versa. Retry
3931 : : with the op0 as new rhs2. */
3932 : 0 : rhs2 = op0;
3933 : 0 : continue;
3934 : : }
3935 : : }
3936 : 233 : else if (is_gimple_assign (g)
3937 : 233 : && gimple_assign_rhs_code (g) == BIT_AND_EXPR
3938 : 0 : && TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST
3939 : 466 : && !wi::neg_p (wi::to_wide (gimple_assign_rhs2 (g))))
3940 : : /* Masking with INTEGER_CST with MSB clear is always ok
3941 : : too. */
3942 : : break;
3943 : : rhs2 = NULL_TREE;
3944 : : }
3945 : : break;
3946 : : }
3947 : 35722 : if (rhs2 == NULL_TREE)
3948 : 247 : continue;
3949 : :
3950 : 36238 : wide_int nz = get_nonzero_bits (rhs2);
3951 : 35722 : if (wi::neg_p (nz))
3952 : 35206 : continue;
3953 : :
3954 : : /* We have EXP < RHS2 or EXP <= RHS2 where EXP >= 0
3955 : : and RHS2 is known to be RHS2 >= 0. */
3956 : 516 : tree utype = unsigned_type_for (TREE_TYPE (rhs1));
3957 : :
3958 : 516 : enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
3959 : 516 : if ((ranges[*idx].strict_overflow_p
3960 : 516 : || ranges[i].strict_overflow_p)
3961 : 0 : && issue_strict_overflow_warning (wc))
3962 : 0 : warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
3963 : : "assuming signed overflow does not occur "
3964 : : "when simplifying range test");
3965 : :
3966 : 516 : if (dump_file && (dump_flags & TDF_DETAILS))
3967 : : {
3968 : 7 : struct range_entry *r = &ranges[*idx];
3969 : 7 : fprintf (dump_file, "Optimizing range test ");
3970 : 7 : print_generic_expr (dump_file, r->exp);
3971 : 7 : fprintf (dump_file, " +[");
3972 : 7 : print_generic_expr (dump_file, r->low);
3973 : 7 : fprintf (dump_file, ", ");
3974 : 7 : print_generic_expr (dump_file, r->high);
3975 : 7 : fprintf (dump_file, "] and comparison ");
3976 : 7 : print_generic_expr (dump_file, rhs1);
3977 : 7 : fprintf (dump_file, " %s ", op_symbol_code (ccode));
3978 : 7 : print_generic_expr (dump_file, rhs2);
3979 : 7 : fprintf (dump_file, "\n into (");
3980 : 7 : print_generic_expr (dump_file, utype);
3981 : 7 : fprintf (dump_file, ") ");
3982 : 7 : print_generic_expr (dump_file, rhs1);
3983 : 7 : fprintf (dump_file, " %s (", op_symbol_code (ccode));
3984 : 7 : print_generic_expr (dump_file, utype);
3985 : 7 : fprintf (dump_file, ") ");
3986 : 7 : print_generic_expr (dump_file, rhs2);
3987 : 7 : fprintf (dump_file, "\n");
3988 : : }
3989 : :
3990 : 516 : operand_entry *oe = (*ops)[ranges[i].idx];
3991 : 516 : ranges[i].in_p = 0;
3992 : 516 : if (opcode == BIT_IOR_EXPR
3993 : 501 : || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
3994 : : {
3995 : 15 : ranges[i].in_p = 1;
3996 : 15 : ccode = invert_tree_comparison (ccode, false);
3997 : : }
3998 : :
3999 : 516 : unsigned int uid = gimple_uid (stmt);
4000 : 516 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4001 : 516 : gimple *g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, rhs1);
4002 : 516 : gimple_set_uid (g, uid);
4003 : 516 : rhs1 = gimple_assign_lhs (g);
4004 : 516 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4005 : 516 : if (!useless_type_conversion_p (utype, TREE_TYPE (rhs2)))
4006 : : {
4007 : 516 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, rhs2);
4008 : 516 : gimple_set_uid (g, uid);
4009 : 516 : rhs2 = gimple_assign_lhs (g);
4010 : 516 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4011 : : }
4012 : 516 : if (tree_swap_operands_p (rhs1, rhs2))
4013 : : {
4014 : 487 : std::swap (rhs1, rhs2);
4015 : 487 : ccode = swap_tree_comparison (ccode);
4016 : : }
4017 : 516 : if (gimple_code (stmt) == GIMPLE_COND)
4018 : : {
4019 : 7 : gcond *c = as_a <gcond *> (stmt);
4020 : 7 : gimple_cond_set_code (c, ccode);
4021 : 7 : gimple_cond_set_lhs (c, rhs1);
4022 : 7 : gimple_cond_set_rhs (c, rhs2);
4023 : 7 : update_stmt (stmt);
4024 : : }
4025 : : else
4026 : : {
4027 : 509 : tree ctype = oe->op ? TREE_TYPE (oe->op) : boolean_type_node;
4028 : 509 : if (!INTEGRAL_TYPE_P (ctype)
4029 : 509 : || (TREE_CODE (ctype) != BOOLEAN_TYPE
4030 : 3 : && TYPE_PRECISION (ctype) != 1))
4031 : 3 : ctype = boolean_type_node;
4032 : 509 : g = gimple_build_assign (make_ssa_name (ctype), ccode, rhs1, rhs2);
4033 : 509 : gimple_set_uid (g, uid);
4034 : 509 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4035 : 509 : if (oe->op && ctype != TREE_TYPE (oe->op))
4036 : : {
4037 : 3 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (oe->op)),
4038 : : NOP_EXPR, gimple_assign_lhs (g));
4039 : 3 : gimple_set_uid (g, uid);
4040 : 3 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4041 : : }
4042 : 509 : ranges[i].exp = gimple_assign_lhs (g);
4043 : 509 : oe->op = ranges[i].exp;
4044 : 509 : ranges[i].low = build_zero_cst (TREE_TYPE (ranges[i].exp));
4045 : 509 : ranges[i].high = ranges[i].low;
4046 : : }
4047 : 516 : ranges[i].strict_overflow_p = false;
4048 : 516 : oe = (*ops)[ranges[*idx].idx];
4049 : : /* Now change all the other range test immediate uses, so that
4050 : : those tests will be optimized away. */
4051 : 516 : if (opcode == ERROR_MARK)
4052 : : {
4053 : 10 : if (oe->op)
4054 : 3 : oe->op = build_int_cst (TREE_TYPE (oe->op),
4055 : 3 : oe->rank == BIT_IOR_EXPR ? 0 : 1);
4056 : : else
4057 : 7 : oe->op = (oe->rank == BIT_IOR_EXPR
4058 : 7 : ? boolean_false_node : boolean_true_node);
4059 : : }
4060 : : else
4061 : 506 : oe->op = error_mark_node;
4062 : 516 : ranges[*idx].exp = NULL_TREE;
4063 : 516 : ranges[*idx].low = NULL_TREE;
4064 : 516 : ranges[*idx].high = NULL_TREE;
4065 : 516 : any_changes = true;
4066 : : }
4067 : :
4068 : 44485 : delete map;
4069 : 44485 : return any_changes;
4070 : : }
4071 : :
4072 : : /* Optimize range tests, similarly how fold_range_test optimizes
4073 : : it on trees. The tree code for the binary
4074 : : operation between all the operands is OPCODE.
4075 : : If OPCODE is ERROR_MARK, optimize_range_tests is called from within
4076 : : maybe_optimize_range_tests for inter-bb range optimization.
4077 : : In that case if oe->op is NULL, oe->id is bb->index whose
4078 : : GIMPLE_COND is && or ||ed into the test, and oe->rank says
4079 : : the actual opcode.
4080 : : FIRST_BB is the first basic block if OPCODE is ERROR_MARK. */
4081 : :
4082 : : static bool
4083 : 1111242 : optimize_range_tests (enum tree_code opcode,
4084 : : vec<operand_entry *> *ops, basic_block first_bb)
4085 : : {
4086 : 1111242 : unsigned int length = ops->length (), i, j, first;
4087 : 1111242 : operand_entry *oe;
4088 : 1111242 : struct range_entry *ranges;
4089 : 2222257 : bool any_changes = false;
4090 : :
4091 : 1111242 : if (length == 1)
4092 : : return false;
4093 : :
4094 : 1111015 : ranges = XNEWVEC (struct range_entry, length);
4095 : 4574998 : for (i = 0; i < length; i++)
4096 : : {
4097 : 2352968 : oe = (*ops)[i];
4098 : 2352968 : ranges[i].idx = i;
4099 : 2352968 : init_range_entry (ranges + i, oe->op,
4100 : 2352968 : oe->op
4101 : : ? NULL
4102 : 229724 : : last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id)));
4103 : : /* For | invert it now, we will invert it again before emitting
4104 : : the optimized expression. */
4105 : 2352968 : if (opcode == BIT_IOR_EXPR
4106 : 1631019 : || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
4107 : 892171 : ranges[i].in_p = !ranges[i].in_p;
4108 : : }
4109 : :
4110 : 1111015 : qsort (ranges, length, sizeof (*ranges), range_entry_cmp);
4111 : 3761997 : for (i = 0; i < length; i++)
4112 : 1912444 : if (ranges[i].exp != NULL_TREE && TREE_CODE (ranges[i].exp) == SSA_NAME)
4113 : : break;
4114 : :
4115 : : /* Try to merge ranges. */
4116 : 1914720 : for (first = i; i < length; i++)
4117 : : {
4118 : 803705 : tree low = ranges[i].low;
4119 : 803705 : tree high = ranges[i].high;
4120 : 803705 : int in_p = ranges[i].in_p;
4121 : 803705 : bool strict_overflow_p = ranges[i].strict_overflow_p;
4122 : 803705 : int update_fail_count = 0;
4123 : :
4124 : 813001 : for (j = i + 1; j < length; j++)
4125 : : {
4126 : 440524 : if (ranges[i].exp != ranges[j].exp)
4127 : : break;
4128 : 31812 : if (!merge_ranges (&in_p, &low, &high, in_p, low, high,
4129 : 31812 : ranges[j].in_p, ranges[j].low, ranges[j].high))
4130 : : break;
4131 : 9296 : strict_overflow_p |= ranges[j].strict_overflow_p;
4132 : : }
4133 : :
4134 : 803705 : if (j == i + 1)
4135 : 795068 : continue;
4136 : :
4137 : 8637 : if (update_range_test (ranges + i, ranges + i + 1, NULL, j - i - 1,
4138 : : opcode, ops, ranges[i].exp, NULL, in_p,
4139 : : low, high, strict_overflow_p))
4140 : : {
4141 : 8637 : i = j - 1;
4142 : 8637 : any_changes = true;
4143 : : }
4144 : : /* Avoid quadratic complexity if all merge_ranges calls would succeed,
4145 : : while update_range_test would fail. */
4146 : : else if (update_fail_count == 64)
4147 : : i = j - 1;
4148 : : else
4149 : 8637 : ++update_fail_count;
4150 : : }
4151 : :
4152 : 1111015 : any_changes |= optimize_range_tests_1 (opcode, first, length, true,
4153 : : ops, ranges);
4154 : :
4155 : 1111015 : if (BRANCH_COST (optimize_function_for_speed_p (cfun), false) >= 2)
4156 : 1110999 : any_changes |= optimize_range_tests_1 (opcode, first, length, false,
4157 : : ops, ranges);
4158 : 1111015 : if (lshift_cheap_p (optimize_function_for_speed_p (cfun)))
4159 : 1111015 : any_changes |= optimize_range_tests_to_bit_test (opcode, first, length,
4160 : : ops, ranges);
4161 : 1111015 : any_changes |= optimize_range_tests_var_bound (opcode, first, length, ops,
4162 : : ranges, first_bb);
4163 : 1111015 : any_changes |= optimize_range_tests_cmp_bitwise (opcode, first, length,
4164 : : ops, ranges);
4165 : :
4166 : 1111015 : if (any_changes && opcode != ERROR_MARK)
4167 : : {
4168 : : j = 0;
4169 : 37955 : FOR_EACH_VEC_ELT (*ops, i, oe)
4170 : : {
4171 : 26678 : if (oe->op == error_mark_node)
4172 : 12735 : continue;
4173 : 13943 : else if (i != j)
4174 : 5713 : (*ops)[j] = oe;
4175 : 13943 : j++;
4176 : : }
4177 : 11277 : ops->truncate (j);
4178 : : }
4179 : :
4180 : 1111015 : XDELETEVEC (ranges);
4181 : 1111015 : return any_changes;
4182 : : }
4183 : :
4184 : : /* A subroutine of optimize_vec_cond_expr to extract and canonicalize
4185 : : the operands of the VEC_COND_EXPR. Returns ERROR_MARK on failure,
4186 : : otherwise the comparison code. TYPE is a return value that is set
4187 : : to type of comparison. */
4188 : :
4189 : : static tree_code
4190 : 45558 : ovce_extract_ops (tree var, gassign **rets, bool *reti, tree *type,
4191 : : tree *lhs, tree *rhs, gassign **vcond)
4192 : : {
4193 : 45558 : if (TREE_CODE (var) != SSA_NAME)
4194 : : return ERROR_MARK;
4195 : :
4196 : 42003 : gassign *stmt = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (var));
4197 : 28896 : if (stmt == NULL)
4198 : : return ERROR_MARK;
4199 : 28896 : if (vcond)
4200 : 28896 : *vcond = stmt;
4201 : :
4202 : : /* ??? If we start creating more COND_EXPR, we could perform
4203 : : this same optimization with them. For now, simplify. */
4204 : 37546 : if (gimple_assign_rhs_code (stmt) != VEC_COND_EXPR)
4205 : : return ERROR_MARK;
4206 : :
4207 : 1130 : tree cond = gimple_assign_rhs1 (stmt);
4208 : 1130 : tree_code cmp = TREE_CODE (cond);
4209 : 1130 : if (cmp != SSA_NAME)
4210 : : return ERROR_MARK;
4211 : :
4212 : 46686 : gassign *assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (cond));
4213 : 1128 : if (assign == NULL
4214 : 1128 : || TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) != tcc_comparison)
4215 : : return ERROR_MARK;
4216 : :
4217 : 1064 : cmp = gimple_assign_rhs_code (assign);
4218 : 1064 : if (lhs)
4219 : 1064 : *lhs = gimple_assign_rhs1 (assign);
4220 : 1064 : if (rhs)
4221 : 2128 : *rhs = gimple_assign_rhs2 (assign);
4222 : :
4223 : : /* ??? For now, allow only canonical true and false result vectors.
4224 : : We could expand this to other constants should the need arise,
4225 : : but at the moment we don't create them. */
4226 : 1064 : tree t = gimple_assign_rhs2 (stmt);
4227 : 1064 : tree f = gimple_assign_rhs3 (stmt);
4228 : 1064 : bool inv;
4229 : 1064 : if (integer_all_onesp (t))
4230 : : inv = false;
4231 : 1064 : else if (integer_all_onesp (f))
4232 : : {
4233 : 1 : cmp = invert_tree_comparison (cmp, false);
4234 : 1 : inv = true;
4235 : : }
4236 : : else
4237 : : return ERROR_MARK;
4238 : 1 : if (!integer_zerop (f))
4239 : : return ERROR_MARK;
4240 : :
4241 : : /* Success! */
4242 : 0 : if (rets)
4243 : 0 : *rets = assign;
4244 : 0 : if (reti)
4245 : 0 : *reti = inv;
4246 : 0 : if (type)
4247 : 0 : *type = TREE_TYPE (cond);
4248 : : return cmp;
4249 : : }
4250 : :
4251 : : /* Optimize the condition of VEC_COND_EXPRs which have been combined
4252 : : with OPCODE (either BIT_AND_EXPR or BIT_IOR_EXPR). */
4253 : :
4254 : : static bool
4255 : 21247 : optimize_vec_cond_expr (tree_code opcode, vec<operand_entry *> *ops)
4256 : : {
4257 : 21247 : unsigned int length = ops->length (), i, j;
4258 : 21247 : bool any_changes = false;
4259 : :
4260 : 21247 : if (length == 1)
4261 : : return false;
4262 : :
4263 : 66797 : for (i = 0; i < length; ++i)
4264 : : {
4265 : 45558 : tree elt0 = (*ops)[i]->op;
4266 : :
4267 : 45558 : gassign *stmt0, *vcond0;
4268 : 45558 : bool invert;
4269 : 45558 : tree type, lhs0, rhs0;
4270 : 45558 : tree_code cmp0 = ovce_extract_ops (elt0, &stmt0, &invert, &type, &lhs0,
4271 : : &rhs0, &vcond0);
4272 : 45558 : if (cmp0 == ERROR_MARK)
4273 : 45558 : continue;
4274 : :
4275 : 0 : for (j = i + 1; j < length; ++j)
4276 : : {
4277 : 0 : tree &elt1 = (*ops)[j]->op;
4278 : :
4279 : 0 : gassign *stmt1, *vcond1;
4280 : 0 : tree lhs1, rhs1;
4281 : 0 : tree_code cmp1 = ovce_extract_ops (elt1, &stmt1, NULL, NULL, &lhs1,
4282 : : &rhs1, &vcond1);
4283 : 0 : if (cmp1 == ERROR_MARK)
4284 : 0 : continue;
4285 : :
4286 : 0 : tree comb;
4287 : 0 : if (opcode == BIT_AND_EXPR)
4288 : 0 : comb = maybe_fold_and_comparisons (type, cmp0, lhs0, rhs0,
4289 : : cmp1, lhs1, rhs1);
4290 : 0 : else if (opcode == BIT_IOR_EXPR)
4291 : 0 : comb = maybe_fold_or_comparisons (type, cmp0, lhs0, rhs0,
4292 : : cmp1, lhs1, rhs1);
4293 : : else
4294 : 0 : gcc_unreachable ();
4295 : 0 : if (comb == NULL)
4296 : 0 : continue;
4297 : :
4298 : : /* Success! */
4299 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4300 : : {
4301 : 0 : fprintf (dump_file, "Transforming ");
4302 : 0 : print_generic_expr (dump_file, gimple_assign_lhs (stmt0));
4303 : 0 : fprintf (dump_file, " %c ", opcode == BIT_AND_EXPR ? '&' : '|');
4304 : 0 : print_generic_expr (dump_file, gimple_assign_lhs (stmt1));
4305 : 0 : fprintf (dump_file, " into ");
4306 : 0 : print_generic_expr (dump_file, comb);
4307 : 0 : fputc ('\n', dump_file);
4308 : : }
4309 : :
4310 : 0 : gimple_stmt_iterator gsi = gsi_for_stmt (vcond0);
4311 : 0 : tree exp = force_gimple_operand_gsi (&gsi, comb, true, NULL_TREE,
4312 : : true, GSI_SAME_STMT);
4313 : 0 : if (invert)
4314 : 0 : swap_ssa_operands (vcond0, gimple_assign_rhs2_ptr (vcond0),
4315 : : gimple_assign_rhs3_ptr (vcond0));
4316 : 0 : gimple_assign_set_rhs1 (vcond0, exp);
4317 : 0 : update_stmt (vcond0);
4318 : :
4319 : 0 : elt1 = error_mark_node;
4320 : 0 : any_changes = true;
4321 : : }
4322 : : }
4323 : :
4324 : 21239 : if (any_changes)
4325 : : {
4326 : : operand_entry *oe;
4327 : : j = 0;
4328 : 0 : FOR_EACH_VEC_ELT (*ops, i, oe)
4329 : : {
4330 : 0 : if (oe->op == error_mark_node)
4331 : 0 : continue;
4332 : 0 : else if (i != j)
4333 : 0 : (*ops)[j] = oe;
4334 : 0 : j++;
4335 : : }
4336 : 0 : ops->truncate (j);
4337 : : }
4338 : :
4339 : : return any_changes;
4340 : : }
4341 : :
4342 : : /* Return true if STMT is a cast like:
4343 : : <bb N>:
4344 : : ...
4345 : : _123 = (int) _234;
4346 : :
4347 : : <bb M>:
4348 : : # _345 = PHI <_123(N), 1(...), 1(...)>
4349 : : where _234 has bool type, _123 has single use and
4350 : : bb N has a single successor M. This is commonly used in
4351 : : the last block of a range test.
4352 : :
4353 : : Also Return true if STMT is tcc_compare like:
4354 : : <bb N>:
4355 : : ...
4356 : : _234 = a_2(D) == 2;
4357 : :
4358 : : <bb M>:
4359 : : # _345 = PHI <_234(N), 1(...), 1(...)>
4360 : : _346 = (int) _345;
4361 : : where _234 has booltype, single use and
4362 : : bb N has a single successor M. This is commonly used in
4363 : : the last block of a range test. */
4364 : :
4365 : : static bool
4366 : 15812756 : final_range_test_p (gimple *stmt)
4367 : : {
4368 : 15812756 : basic_block bb, rhs_bb, lhs_bb;
4369 : 15812756 : edge e;
4370 : 15812756 : tree lhs, rhs;
4371 : 15812756 : use_operand_p use_p;
4372 : 15812756 : gimple *use_stmt;
4373 : :
4374 : 15812756 : if (!gimple_assign_cast_p (stmt)
4375 : 15812756 : && (!is_gimple_assign (stmt)
4376 : 5048220 : || (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
4377 : : != tcc_comparison)))
4378 : : return false;
4379 : 565276 : bb = gimple_bb (stmt);
4380 : 16176466 : if (!single_succ_p (bb))
4381 : : return false;
4382 : 564753 : e = single_succ_edge (bb);
4383 : 564753 : if (e->flags & EDGE_COMPLEX)
4384 : : return false;
4385 : :
4386 : 564753 : lhs = gimple_assign_lhs (stmt);
4387 : 564753 : rhs = gimple_assign_rhs1 (stmt);
4388 : 564753 : if (gimple_assign_cast_p (stmt)
4389 : 564753 : && (!INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4390 : 396189 : || TREE_CODE (rhs) != SSA_NAME
4391 : 379869 : || TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE))
4392 : : return false;
4393 : :
4394 : 209369 : if (!gimple_assign_cast_p (stmt)
4395 : 209369 : && (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE))
4396 : : return false;
4397 : :
4398 : : /* Test whether lhs is consumed only by a PHI in the only successor bb. */
4399 : 209346 : if (!single_imm_use (lhs, &use_p, &use_stmt))
4400 : : return false;
4401 : :
4402 : 202884 : if (gimple_code (use_stmt) != GIMPLE_PHI
4403 : 202884 : || gimple_bb (use_stmt) != e->dest)
4404 : : return false;
4405 : :
4406 : : /* And that the rhs is defined in the same loop. */
4407 : 201094 : if (gimple_assign_cast_p (stmt))
4408 : : {
4409 : 69543 : if (TREE_CODE (rhs) != SSA_NAME
4410 : 69543 : || !(rhs_bb = gimple_bb (SSA_NAME_DEF_STMT (rhs)))
4411 : 139080 : || !flow_bb_inside_loop_p (loop_containing_stmt (stmt), rhs_bb))
4412 : 51 : return false;
4413 : : }
4414 : : else
4415 : : {
4416 : 131551 : if (TREE_CODE (lhs) != SSA_NAME
4417 : 131551 : || !(lhs_bb = gimple_bb (SSA_NAME_DEF_STMT (lhs)))
4418 : 263102 : || !flow_bb_inside_loop_p (loop_containing_stmt (stmt), lhs_bb))
4419 : 0 : return false;
4420 : : }
4421 : :
4422 : : return true;
4423 : : }
4424 : :
4425 : : /* Return true if BB is suitable basic block for inter-bb range test
4426 : : optimization. If BACKWARD is true, BB should be the only predecessor
4427 : : of TEST_BB, and *OTHER_BB is either NULL and filled by the routine,
4428 : : or compared with to find a common basic block to which all conditions
4429 : : branch to if true resp. false. If BACKWARD is false, TEST_BB should
4430 : : be the only predecessor of BB. *TEST_SWAPPED_P is set to true if
4431 : : TEST_BB is a bb ending in condition where the edge to non-*OTHER_BB
4432 : : block points to an empty block that falls through into *OTHER_BB and
4433 : : the phi args match that path. */
4434 : :
4435 : : static bool
4436 : 11275779 : suitable_cond_bb (basic_block bb, basic_block test_bb, basic_block *other_bb,
4437 : : bool *test_swapped_p, bool backward)
4438 : : {
4439 : 11275779 : edge_iterator ei, ei2;
4440 : 11275779 : edge e, e2;
4441 : 11275779 : gimple *stmt;
4442 : 11275779 : gphi_iterator gsi;
4443 : 11275779 : bool other_edge_seen = false;
4444 : 11275779 : bool is_cond;
4445 : :
4446 : 11275779 : if (test_bb == bb)
4447 : : return false;
4448 : : /* Check last stmt first. */
4449 : 11275779 : stmt = last_nondebug_stmt (bb);
4450 : 11275779 : if (stmt == NULL
4451 : 10424124 : || (gimple_code (stmt) != GIMPLE_COND
4452 : 594343 : && (backward || !final_range_test_p (stmt)))
4453 : 9868238 : || gimple_visited_p (stmt)
4454 : 9814590 : || stmt_could_throw_p (cfun, stmt)
4455 : 21090249 : || *other_bb == bb)
4456 : 1461312 : return false;
4457 : 9814467 : is_cond = gimple_code (stmt) == GIMPLE_COND;
4458 : 9814467 : if (is_cond)
4459 : : {
4460 : : /* If last stmt is GIMPLE_COND, verify that one of the succ edges
4461 : : goes to the next bb (if BACKWARD, it is TEST_BB), and the other
4462 : : to *OTHER_BB (if not set yet, try to find it out). */
4463 : 18721049 : if (EDGE_COUNT (bb->succs) != 2)
4464 : : return false;
4465 : 19046100 : FOR_EACH_EDGE (e, ei, bb->succs)
4466 : : {
4467 : 15715617 : if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4468 : : return false;
4469 : 15715617 : if (e->dest == test_bb)
4470 : : {
4471 : 5099698 : if (backward)
4472 : 5097521 : continue;
4473 : : else
4474 : : return false;
4475 : : }
4476 : 10615919 : if (e->dest == bb)
4477 : : return false;
4478 : 10467697 : if (*other_bb == NULL)
4479 : : {
4480 : 25749729 : FOR_EACH_EDGE (e2, ei2, test_bb->succs)
4481 : 17166486 : if (!(e2->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4482 : : return false;
4483 : 17166486 : else if (e->dest == e2->dest)
4484 : 2328835 : *other_bb = e->dest;
4485 : 8583243 : if (*other_bb == NULL)
4486 : : return false;
4487 : : }
4488 : 4213289 : if (e->dest == *other_bb)
4489 : : other_edge_seen = true;
4490 : 922096 : else if (backward)
4491 : : return false;
4492 : : }
4493 : 3330483 : if (*other_bb == NULL || !other_edge_seen)
4494 : : return false;
4495 : : }
4496 : 38337 : else if (single_succ (bb) != *other_bb)
4497 : : return false;
4498 : :
4499 : : /* Now check all PHIs of *OTHER_BB. */
4500 : 3329133 : e = find_edge (bb, *other_bb);
4501 : 3329133 : e2 = find_edge (test_bb, *other_bb);
4502 : 3335737 : retry:;
4503 : 4787049 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
4504 : : {
4505 : 2456189 : gphi *phi = gsi.phi ();
4506 : : /* If both BB and TEST_BB end with GIMPLE_COND, all PHI arguments
4507 : : corresponding to BB and TEST_BB predecessor must be the same. */
4508 : 2456189 : if (!operand_equal_p (gimple_phi_arg_def (phi, e->dest_idx),
4509 : 2456189 : gimple_phi_arg_def (phi, e2->dest_idx), 0))
4510 : : {
4511 : : /* Otherwise, if one of the blocks doesn't end with GIMPLE_COND,
4512 : : one of the PHIs should have the lhs of the last stmt in
4513 : : that block as PHI arg and that PHI should have 0 or 1
4514 : : corresponding to it in all other range test basic blocks
4515 : : considered. */
4516 : 1072439 : if (!is_cond)
4517 : : {
4518 : 39922 : if (gimple_phi_arg_def (phi, e->dest_idx)
4519 : 39922 : == gimple_assign_lhs (stmt)
4520 : 39922 : && (integer_zerop (gimple_phi_arg_def (phi, e2->dest_idx))
4521 : 17847 : || integer_onep (gimple_phi_arg_def (phi,
4522 : 17847 : e2->dest_idx))))
4523 : 36415 : continue;
4524 : : }
4525 : : else
4526 : : {
4527 : 1032517 : gimple *test_last = last_nondebug_stmt (test_bb);
4528 : 1032517 : if (gimple_code (test_last) == GIMPLE_COND)
4529 : : {
4530 : 997891 : if (backward ? e2->src != test_bb : e->src != bb)
4531 : : return false;
4532 : :
4533 : : /* For last_bb, handle also:
4534 : : if (x_3(D) == 3)
4535 : : goto <bb 6>; [34.00%]
4536 : : else
4537 : : goto <bb 7>; [66.00%]
4538 : :
4539 : : <bb 6> [local count: 79512730]:
4540 : :
4541 : : <bb 7> [local count: 1073741824]:
4542 : : # prephitmp_7 = PHI <1(3), 1(4), 0(5), 1(2), 1(6)>
4543 : : where bb 7 is *OTHER_BB, but the PHI values from the
4544 : : earlier bbs match the path through the empty bb
4545 : : in between. */
4546 : 993571 : edge e3;
4547 : 993571 : if (backward)
4548 : 1313532 : e3 = EDGE_SUCC (test_bb,
4549 : : e2 == EDGE_SUCC (test_bb, 0) ? 1 : 0);
4550 : : else
4551 : 12783 : e3 = EDGE_SUCC (bb,
4552 : : e == EDGE_SUCC (bb, 0) ? 1 : 0);
4553 : 993571 : if (empty_block_p (e3->dest)
4554 : 34386 : && single_succ_p (e3->dest)
4555 : 34386 : && single_succ (e3->dest) == *other_bb
4556 : 1027269 : && single_pred_p (e3->dest)
4557 : 1026887 : && single_succ_edge (e3->dest)->flags == EDGE_FALLTHRU)
4558 : : {
4559 : 6604 : if (backward)
4560 : 5831 : e2 = single_succ_edge (e3->dest);
4561 : : else
4562 : 773 : e = single_succ_edge (e3->dest);
4563 : 6604 : if (test_swapped_p)
4564 : 357 : *test_swapped_p = true;
4565 : 6604 : goto retry;
4566 : : }
4567 : : }
4568 : 34626 : else if (gimple_phi_arg_def (phi, e2->dest_idx)
4569 : 34626 : == gimple_assign_lhs (test_last)
4570 : 66766 : && (integer_zerop (gimple_phi_arg_def (phi,
4571 : 32140 : e->dest_idx))
4572 : 14972 : || integer_onep (gimple_phi_arg_def (phi,
4573 : 14972 : e->dest_idx))))
4574 : 31147 : continue;
4575 : : }
4576 : :
4577 : 993953 : return false;
4578 : : }
4579 : : }
4580 : : return true;
4581 : : }
4582 : :
4583 : : /* Return true if BB doesn't have side-effects that would disallow
4584 : : range test optimization, all SSA_NAMEs set in the bb are consumed
4585 : : in the bb and there are no PHIs. */
4586 : :
4587 : : bool
4588 : 5245399 : no_side_effect_bb (basic_block bb)
4589 : : {
4590 : 5245399 : gimple_stmt_iterator gsi;
4591 : 5245399 : gimple *last;
4592 : :
4593 : 5245399 : if (!gimple_seq_empty_p (phi_nodes (bb)))
4594 : : return false;
4595 : 4180627 : last = last_nondebug_stmt (bb);
4596 : 13776177 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4597 : : {
4598 : 9595550 : gimple *stmt = gsi_stmt (gsi);
4599 : 9595550 : tree lhs;
4600 : 9595550 : imm_use_iterator imm_iter;
4601 : 9595550 : use_operand_p use_p;
4602 : :
4603 : 9595550 : if (is_gimple_debug (stmt))
4604 : 3961951 : continue;
4605 : 5633599 : if (gimple_has_side_effects (stmt))
4606 : 4180627 : return false;
4607 : 4805436 : if (stmt == last)
4608 : : return true;
4609 : 3776647 : if (!is_gimple_assign (stmt))
4610 : : return false;
4611 : 3111746 : lhs = gimple_assign_lhs (stmt);
4612 : 3111746 : if (TREE_CODE (lhs) != SSA_NAME)
4613 : : return false;
4614 : 2850102 : if (gimple_assign_rhs_could_trap_p (stmt))
4615 : : return false;
4616 : 3454306 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
4617 : : {
4618 : 2001334 : gimple *use_stmt = USE_STMT (use_p);
4619 : 2001334 : if (is_gimple_debug (use_stmt))
4620 : 104816 : continue;
4621 : 1896518 : if (gimple_bb (use_stmt) != bb)
4622 : : return false;
4623 : : }
4624 : : }
4625 : : return false;
4626 : : }
4627 : :
4628 : : /* If VAR is set by CODE (BIT_{AND,IOR}_EXPR) which is reassociable,
4629 : : return true and fill in *OPS recursively. */
4630 : :
4631 : : static bool
4632 : 99635 : get_ops (tree var, enum tree_code code, vec<operand_entry *> *ops,
4633 : : class loop *loop)
4634 : : {
4635 : 99635 : gimple *stmt = SSA_NAME_DEF_STMT (var);
4636 : 99635 : tree rhs[2];
4637 : 99635 : int i;
4638 : :
4639 : 99635 : if (!is_reassociable_op (stmt, code, loop))
4640 : : return false;
4641 : :
4642 : 25485 : rhs[0] = gimple_assign_rhs1 (stmt);
4643 : 25485 : rhs[1] = gimple_assign_rhs2 (stmt);
4644 : 25485 : gimple_set_visited (stmt, true);
4645 : 76455 : for (i = 0; i < 2; i++)
4646 : 50970 : if (TREE_CODE (rhs[i]) == SSA_NAME
4647 : 50970 : && !get_ops (rhs[i], code, ops, loop)
4648 : 93072 : && has_single_use (rhs[i]))
4649 : : {
4650 : 41387 : operand_entry *oe = operand_entry_pool.allocate ();
4651 : :
4652 : 41387 : oe->op = rhs[i];
4653 : 41387 : oe->rank = code;
4654 : 41387 : oe->id = 0;
4655 : 41387 : oe->count = 1;
4656 : 41387 : oe->stmt_to_insert = NULL;
4657 : 41387 : ops->safe_push (oe);
4658 : : }
4659 : : return true;
4660 : : }
4661 : :
4662 : : /* Find the ops that were added by get_ops starting from VAR, see if
4663 : : they were changed during update_range_test and if yes, create new
4664 : : stmts. */
4665 : :
4666 : : static tree
4667 : 9378 : update_ops (tree var, enum tree_code code, const vec<operand_entry *> &ops,
4668 : : unsigned int *pidx, class loop *loop)
4669 : : {
4670 : 9378 : gimple *stmt = SSA_NAME_DEF_STMT (var);
4671 : 9378 : tree rhs[4];
4672 : 9378 : int i;
4673 : :
4674 : 9378 : if (!is_reassociable_op (stmt, code, loop))
4675 : : return NULL;
4676 : :
4677 : 3107 : rhs[0] = gimple_assign_rhs1 (stmt);
4678 : 3107 : rhs[1] = gimple_assign_rhs2 (stmt);
4679 : 3107 : rhs[2] = rhs[0];
4680 : 3107 : rhs[3] = rhs[1];
4681 : 9321 : for (i = 0; i < 2; i++)
4682 : 6214 : if (TREE_CODE (rhs[i]) == SSA_NAME)
4683 : : {
4684 : 6214 : rhs[2 + i] = update_ops (rhs[i], code, ops, pidx, loop);
4685 : 6214 : if (rhs[2 + i] == NULL_TREE)
4686 : : {
4687 : 5948 : if (has_single_use (rhs[i]))
4688 : 5930 : rhs[2 + i] = ops[(*pidx)++]->op;
4689 : : else
4690 : 18 : rhs[2 + i] = rhs[i];
4691 : : }
4692 : : }
4693 : 3107 : if ((rhs[2] != rhs[0] || rhs[3] != rhs[1])
4694 : 2832 : && (rhs[2] != rhs[1] || rhs[3] != rhs[0]))
4695 : : {
4696 : 2832 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4697 : 2832 : var = make_ssa_name (TREE_TYPE (var));
4698 : 2832 : gassign *g = gimple_build_assign (var, gimple_assign_rhs_code (stmt),
4699 : : rhs[2], rhs[3]);
4700 : 2832 : gimple_set_uid (g, gimple_uid (stmt));
4701 : 2832 : gimple_set_visited (g, true);
4702 : 2832 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4703 : 2832 : gimple_stmt_iterator gsi2 = gsi_for_stmt (g);
4704 : 2832 : if (fold_stmt_inplace (&gsi2))
4705 : 1925 : update_stmt (g);
4706 : : }
4707 : : return var;
4708 : : }
4709 : :
4710 : : /* Structure to track the initial value passed to get_ops and
4711 : : the range in the ops vector for each basic block. */
4712 : :
4713 : : struct inter_bb_range_test_entry
4714 : : {
4715 : : tree op;
4716 : : unsigned int first_idx, last_idx;
4717 : : };
4718 : :
4719 : : /* Inter-bb range test optimization.
4720 : :
4721 : : Returns TRUE if a gimple conditional is optimized to a true/false,
4722 : : otherwise return FALSE.
4723 : :
4724 : : This indicates to the caller that it should run a CFG cleanup pass
4725 : : once reassociation is completed. */
4726 : :
4727 : : static bool
4728 : 19081485 : maybe_optimize_range_tests (gimple *stmt)
4729 : : {
4730 : 19081485 : basic_block first_bb = gimple_bb (stmt);
4731 : 19081485 : basic_block last_bb = first_bb;
4732 : 19081485 : basic_block other_bb = NULL;
4733 : 19081485 : basic_block bb;
4734 : 19081485 : edge_iterator ei;
4735 : 19081485 : edge e;
4736 : 19081485 : auto_vec<operand_entry *> ops;
4737 : 19081485 : auto_vec<inter_bb_range_test_entry> bbinfo;
4738 : 19081485 : bool any_changes = false;
4739 : 19081485 : bool cfg_cleanup_needed = false;
4740 : :
4741 : : /* Consider only basic blocks that end with GIMPLE_COND or
4742 : : a cast statement satisfying final_range_test_p. All
4743 : : but the last bb in the first_bb .. last_bb range
4744 : : should end with GIMPLE_COND. */
4745 : 19081485 : if (gimple_code (stmt) == GIMPLE_COND)
4746 : : {
4747 : 27772681 : if (EDGE_COUNT (first_bb->succs) != 2)
4748 : : return cfg_cleanup_needed;
4749 : : }
4750 : 10381463 : else if (final_range_test_p (stmt))
4751 : 85247 : other_bb = single_succ (first_bb);
4752 : : else
4753 : : return cfg_cleanup_needed;
4754 : :
4755 : 8785269 : if (stmt_could_throw_p (cfun, stmt))
4756 : : return cfg_cleanup_needed;
4757 : :
4758 : : /* As relative ordering of post-dominator sons isn't fixed,
4759 : : maybe_optimize_range_tests can be called first on any
4760 : : bb in the range we want to optimize. So, start searching
4761 : : backwards, if first_bb can be set to a predecessor. */
4762 : 8786588 : while (single_pred_p (first_bb))
4763 : : {
4764 : 5835857 : basic_block pred_bb = single_pred (first_bb);
4765 : 5835857 : if (!suitable_cond_bb (pred_bb, first_bb, &other_bb, NULL, true))
4766 : : break;
4767 : 640093 : if (!no_side_effect_bb (first_bb))
4768 : : break;
4769 : : first_bb = pred_bb;
4770 : : }
4771 : : /* If first_bb is last_bb, other_bb hasn't been computed yet.
4772 : : Before starting forward search in last_bb successors, find
4773 : : out the other_bb. */
4774 : 8785089 : if (first_bb == last_bb)
4775 : : {
4776 : 8783796 : other_bb = NULL;
4777 : : /* As non-GIMPLE_COND last stmt always terminates the range,
4778 : : if forward search didn't discover anything, just give up. */
4779 : 8783796 : if (gimple_code (stmt) != GIMPLE_COND)
4780 : : return cfg_cleanup_needed;
4781 : : /* Look at both successors. Either it ends with a GIMPLE_COND
4782 : : and satisfies suitable_cond_bb, or ends with a cast and
4783 : : other_bb is that cast's successor. */
4784 : 24229250 : FOR_EACH_EDGE (e, ei, first_bb->succs)
4785 : 16771288 : if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4786 : 16771288 : || e->dest == first_bb)
4787 : : return cfg_cleanup_needed;
4788 : 25489482 : else if (single_pred_p (e->dest))
4789 : : {
4790 : 9959030 : stmt = last_nondebug_stmt (e->dest);
4791 : 9959030 : if (stmt
4792 : 9745959 : && gimple_code (stmt) == GIMPLE_COND
4793 : 14349100 : && EDGE_COUNT (e->dest->succs) == 2)
4794 : : {
4795 : 4390070 : if (suitable_cond_bb (first_bb, e->dest, &other_bb,
4796 : : NULL, true))
4797 : : break;
4798 : : else
4799 : 3654427 : other_bb = NULL;
4800 : : }
4801 : 5568960 : else if (stmt
4802 : 5355889 : && final_range_test_p (stmt)
4803 : 5646299 : && find_edge (first_bb, single_succ (e->dest)))
4804 : : {
4805 : 36790 : other_bb = single_succ (e->dest);
4806 : 36790 : if (other_bb == first_bb)
4807 : 0 : other_bb = NULL;
4808 : : }
4809 : : }
4810 : 8193605 : if (other_bb == NULL)
4811 : : return cfg_cleanup_needed;
4812 : : }
4813 : : /* Now do the forward search, moving last_bb to successor bbs
4814 : : that aren't other_bb. */
4815 : 1728506 : while (EDGE_COUNT (last_bb->succs) == 2)
4816 : : {
4817 : 1611492 : FOR_EACH_EDGE (e, ei, last_bb->succs)
4818 : 1611492 : if (e->dest != other_bb)
4819 : : break;
4820 : 954780 : if (e == NULL)
4821 : : break;
4822 : 954780 : if (!single_pred_p (e->dest))
4823 : : break;
4824 : 923066 : if (!suitable_cond_bb (e->dest, last_bb, &other_bb, NULL, false))
4825 : : break;
4826 : 828338 : if (!no_side_effect_bb (e->dest))
4827 : : break;
4828 : 185747 : last_bb = e->dest;
4829 : : }
4830 : 773726 : if (first_bb == last_bb)
4831 : : return cfg_cleanup_needed;
4832 : : /* Here basic blocks first_bb through last_bb's predecessor
4833 : : end with GIMPLE_COND, all of them have one of the edges to
4834 : : other_bb and another to another block in the range,
4835 : : all blocks except first_bb don't have side-effects and
4836 : : last_bb ends with either GIMPLE_COND, or cast satisfying
4837 : : final_range_test_p. */
4838 : 187246 : for (bb = last_bb; ; bb = single_pred (bb))
4839 : : {
4840 : 318725 : enum tree_code code;
4841 : 318725 : tree lhs, rhs;
4842 : 318725 : inter_bb_range_test_entry bb_ent;
4843 : :
4844 : 318725 : bb_ent.op = NULL_TREE;
4845 : 318725 : bb_ent.first_idx = ops.length ();
4846 : 318725 : bb_ent.last_idx = bb_ent.first_idx;
4847 : 318725 : e = find_edge (bb, other_bb);
4848 : 318725 : stmt = last_nondebug_stmt (bb);
4849 : 318725 : gimple_set_visited (stmt, true);
4850 : 318725 : if (gimple_code (stmt) != GIMPLE_COND)
4851 : : {
4852 : 4693 : use_operand_p use_p;
4853 : 4693 : gimple *phi;
4854 : 4693 : edge e2;
4855 : 4693 : unsigned int d;
4856 : :
4857 : 4693 : lhs = gimple_assign_lhs (stmt);
4858 : 4693 : rhs = gimple_assign_rhs1 (stmt);
4859 : 4693 : gcc_assert (bb == last_bb);
4860 : :
4861 : : /* stmt is
4862 : : _123 = (int) _234;
4863 : : OR
4864 : : _234 = a_2(D) == 2;
4865 : :
4866 : : followed by:
4867 : : <bb M>:
4868 : : # _345 = PHI <_123(N), 1(...), 1(...)>
4869 : :
4870 : : or 0 instead of 1. If it is 0, the _234
4871 : : range test is anded together with all the
4872 : : other range tests, if it is 1, it is ored with
4873 : : them. */
4874 : 4693 : single_imm_use (lhs, &use_p, &phi);
4875 : 4693 : gcc_assert (gimple_code (phi) == GIMPLE_PHI);
4876 : 4693 : e2 = find_edge (first_bb, other_bb);
4877 : 4693 : d = e2->dest_idx;
4878 : 4693 : gcc_assert (gimple_phi_arg_def (phi, e->dest_idx) == lhs);
4879 : 4693 : if (integer_zerop (gimple_phi_arg_def (phi, d)))
4880 : : code = BIT_AND_EXPR;
4881 : : else
4882 : : {
4883 : 2408 : gcc_checking_assert (integer_onep (gimple_phi_arg_def (phi, d)));
4884 : : code = BIT_IOR_EXPR;
4885 : : }
4886 : :
4887 : : /* If _234 SSA_NAME_DEF_STMT is
4888 : : _234 = _567 | _789;
4889 : : (or &, corresponding to 1/0 in the phi arguments,
4890 : : push into ops the individual range test arguments
4891 : : of the bitwise or resp. and, recursively. */
4892 : 4693 : if (TREE_CODE (rhs) == SSA_NAME
4893 : 4693 : && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
4894 : : != tcc_comparison)
4895 : 2654 : && !get_ops (rhs, code, &ops,
4896 : : loop_containing_stmt (stmt))
4897 : 7154 : && has_single_use (rhs))
4898 : : {
4899 : : /* Otherwise, push the _234 range test itself. */
4900 : 2451 : operand_entry *oe = operand_entry_pool.allocate ();
4901 : :
4902 : 2451 : oe->op = rhs;
4903 : 2451 : oe->rank = code;
4904 : 2451 : oe->id = 0;
4905 : 2451 : oe->count = 1;
4906 : 2451 : oe->stmt_to_insert = NULL;
4907 : 2451 : ops.safe_push (oe);
4908 : 2451 : bb_ent.last_idx++;
4909 : 2451 : bb_ent.op = rhs;
4910 : : }
4911 : 2242 : else if (is_gimple_assign (stmt)
4912 : 2242 : && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
4913 : : == tcc_comparison)
4914 : 2039 : && !get_ops (lhs, code, &ops,
4915 : : loop_containing_stmt (stmt))
4916 : 4281 : && has_single_use (lhs))
4917 : : {
4918 : 2039 : operand_entry *oe = operand_entry_pool.allocate ();
4919 : 2039 : oe->op = lhs;
4920 : 2039 : oe->rank = code;
4921 : 2039 : oe->id = 0;
4922 : 2039 : oe->count = 1;
4923 : 2039 : ops.safe_push (oe);
4924 : 2039 : bb_ent.last_idx++;
4925 : 2039 : bb_ent.op = lhs;
4926 : : }
4927 : : else
4928 : : {
4929 : 203 : bb_ent.last_idx = ops.length ();
4930 : 203 : bb_ent.op = rhs;
4931 : : }
4932 : 4693 : bbinfo.safe_push (bb_ent);
4933 : 9632 : for (unsigned int i = bb_ent.first_idx; i < bb_ent.last_idx; ++i)
4934 : 4939 : ops[i]->id = bb->index;
4935 : 4693 : continue;
4936 : 4693 : }
4937 : 314032 : else if (bb == last_bb)
4938 : : {
4939 : : /* For last_bb, handle also:
4940 : : if (x_3(D) == 3)
4941 : : goto <bb 6>; [34.00%]
4942 : : else
4943 : : goto <bb 7>; [66.00%]
4944 : :
4945 : : <bb 6> [local count: 79512730]:
4946 : :
4947 : : <bb 7> [local count: 1073741824]:
4948 : : # prephitmp_7 = PHI <1(3), 1(4), 0(5), 1(2), 1(6)>
4949 : : where bb 7 is OTHER_BB, but the PHI values from the
4950 : : earlier bbs match the path through the empty bb
4951 : : in between. */
4952 : 126786 : bool test_swapped_p = false;
4953 : 126786 : bool ok = suitable_cond_bb (single_pred (last_bb), last_bb,
4954 : : &other_bb, &test_swapped_p, true);
4955 : 126786 : gcc_assert (ok);
4956 : 126786 : if (test_swapped_p)
4957 : 612 : e = EDGE_SUCC (bb, e == EDGE_SUCC (bb, 0) ? 1 : 0);
4958 : : }
4959 : : /* Otherwise stmt is GIMPLE_COND. */
4960 : 314032 : code = gimple_cond_code (stmt);
4961 : 314032 : lhs = gimple_cond_lhs (stmt);
4962 : 314032 : rhs = gimple_cond_rhs (stmt);
4963 : 314032 : if (TREE_CODE (lhs) == SSA_NAME
4964 : 313934 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4965 : 575773 : && ((code != EQ_EXPR && code != NE_EXPR)
4966 : 204492 : || rhs != boolean_false_node
4967 : : /* Either push into ops the individual bitwise
4968 : : or resp. and operands, depending on which
4969 : : edge is other_bb. */
4970 : 43972 : || !get_ops (lhs, (((e->flags & EDGE_TRUE_VALUE) == 0)
4971 : 43972 : ^ (code == EQ_EXPR))
4972 : : ? BIT_AND_EXPR : BIT_IOR_EXPR, &ops,
4973 : : loop_containing_stmt (stmt))))
4974 : : {
4975 : : /* Or push the GIMPLE_COND stmt itself. */
4976 : 245317 : operand_entry *oe = operand_entry_pool.allocate ();
4977 : :
4978 : 245317 : oe->op = NULL;
4979 : 490634 : oe->rank = (e->flags & EDGE_TRUE_VALUE)
4980 : 245317 : ? BIT_IOR_EXPR : BIT_AND_EXPR;
4981 : : /* oe->op = NULL signs that there is no SSA_NAME
4982 : : for the range test, and oe->id instead is the
4983 : : basic block number, at which's end the GIMPLE_COND
4984 : : is. */
4985 : 245317 : oe->id = bb->index;
4986 : 245317 : oe->count = 1;
4987 : 245317 : oe->stmt_to_insert = NULL;
4988 : 245317 : ops.safe_push (oe);
4989 : 245317 : bb_ent.op = NULL;
4990 : 245317 : bb_ent.last_idx++;
4991 : : }
4992 : 68715 : else if (ops.length () > bb_ent.first_idx)
4993 : : {
4994 : 16352 : bb_ent.op = lhs;
4995 : 16352 : bb_ent.last_idx = ops.length ();
4996 : : }
4997 : 314032 : bbinfo.safe_push (bb_ent);
4998 : 600287 : for (unsigned int i = bb_ent.first_idx; i < bb_ent.last_idx; ++i)
4999 : 286255 : ops[i]->id = bb->index;
5000 : 314032 : if (bb == first_bb)
5001 : : break;
5002 : 187246 : }
5003 : 19212964 : if (ops.length () > 1)
5004 : 102773 : any_changes = optimize_range_tests (ERROR_MARK, &ops, first_bb);
5005 : 102773 : if (any_changes)
5006 : : {
5007 : : unsigned int idx, max_idx = 0;
5008 : : /* update_ops relies on has_single_use predicates returning the
5009 : : same values as it did during get_ops earlier. Additionally it
5010 : : never removes statements, only adds new ones and it should walk
5011 : : from the single imm use and check the predicate already before
5012 : : making those changes.
5013 : : On the other side, the handling of GIMPLE_COND directly can turn
5014 : : previously multiply used SSA_NAMEs into single use SSA_NAMEs, so
5015 : : it needs to be done in a separate loop afterwards. */
5016 : 17099 : for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
5017 : : {
5018 : 25925 : if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
5019 : 25925 : && bbinfo[idx].op != NULL_TREE)
5020 : : {
5021 : 3164 : tree new_op;
5022 : :
5023 : 3164 : max_idx = idx;
5024 : 3164 : stmt = last_nondebug_stmt (bb);
5025 : 6328 : new_op = update_ops (bbinfo[idx].op,
5026 : : (enum tree_code)
5027 : 3164 : ops[bbinfo[idx].first_idx]->rank,
5028 : 3164 : ops, &bbinfo[idx].first_idx,
5029 : : loop_containing_stmt (stmt));
5030 : 3164 : if (new_op == NULL_TREE)
5031 : : {
5032 : 323 : gcc_assert (bb == last_bb);
5033 : 323 : new_op = ops[bbinfo[idx].first_idx++]->op;
5034 : : }
5035 : 3164 : if (bbinfo[idx].op != new_op)
5036 : : {
5037 : 2932 : imm_use_iterator iter;
5038 : 2932 : use_operand_p use_p;
5039 : 2932 : gimple *use_stmt, *cast_or_tcc_cmp_stmt = NULL;
5040 : :
5041 : 5874 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, bbinfo[idx].op)
5042 : 2942 : if (is_gimple_debug (use_stmt))
5043 : 10 : continue;
5044 : 2932 : else if (gimple_code (use_stmt) == GIMPLE_COND
5045 : 2932 : || gimple_code (use_stmt) == GIMPLE_PHI)
5046 : 7959 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
5047 : 2653 : SET_USE (use_p, new_op);
5048 : 279 : else if ((is_gimple_assign (use_stmt)
5049 : 279 : && (TREE_CODE_CLASS
5050 : : (gimple_assign_rhs_code (use_stmt))
5051 : : == tcc_comparison)))
5052 : : cast_or_tcc_cmp_stmt = use_stmt;
5053 : 279 : else if (gimple_assign_cast_p (use_stmt))
5054 : : cast_or_tcc_cmp_stmt = use_stmt;
5055 : : else
5056 : 2932 : gcc_unreachable ();
5057 : :
5058 : 2932 : if (cast_or_tcc_cmp_stmt)
5059 : : {
5060 : 279 : gcc_assert (bb == last_bb);
5061 : 279 : tree lhs = gimple_assign_lhs (cast_or_tcc_cmp_stmt);
5062 : 279 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs));
5063 : 279 : enum tree_code rhs_code
5064 : 279 : = gimple_assign_cast_p (cast_or_tcc_cmp_stmt)
5065 : 279 : ? gimple_assign_rhs_code (cast_or_tcc_cmp_stmt)
5066 : : : CONVERT_EXPR;
5067 : 279 : gassign *g;
5068 : 279 : if (is_gimple_min_invariant (new_op))
5069 : : {
5070 : 79 : new_op = fold_convert (TREE_TYPE (lhs), new_op);
5071 : 79 : g = gimple_build_assign (new_lhs, new_op);
5072 : : }
5073 : : else
5074 : 200 : g = gimple_build_assign (new_lhs, rhs_code, new_op);
5075 : 279 : gimple_stmt_iterator gsi
5076 : 279 : = gsi_for_stmt (cast_or_tcc_cmp_stmt);
5077 : 279 : gimple_set_uid (g, gimple_uid (cast_or_tcc_cmp_stmt));
5078 : 279 : gimple_set_visited (g, true);
5079 : 279 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5080 : 558 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5081 : 279 : if (is_gimple_debug (use_stmt))
5082 : 0 : continue;
5083 : 279 : else if (gimple_code (use_stmt) == GIMPLE_COND
5084 : 279 : || gimple_code (use_stmt) == GIMPLE_PHI)
5085 : 837 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
5086 : 279 : SET_USE (use_p, new_lhs);
5087 : : else
5088 : 279 : gcc_unreachable ();
5089 : : }
5090 : : }
5091 : : }
5092 : 25925 : if (bb == first_bb)
5093 : : break;
5094 : 17099 : }
5095 : 17099 : for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
5096 : : {
5097 : 25925 : if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
5098 : 22477 : && bbinfo[idx].op == NULL_TREE
5099 : 48402 : && ops[bbinfo[idx].first_idx]->op != NULL_TREE)
5100 : : {
5101 : 38278 : gcond *cond_stmt = as_a <gcond *> (*gsi_last_bb (bb));
5102 : :
5103 : 19139 : if (idx > max_idx)
5104 : : max_idx = idx;
5105 : :
5106 : : /* If we collapse the conditional to a true/false
5107 : : condition, then bubble that knowledge up to our caller. */
5108 : 19139 : if (integer_zerop (ops[bbinfo[idx].first_idx]->op))
5109 : : {
5110 : 9165 : gimple_cond_make_false (cond_stmt);
5111 : 9165 : cfg_cleanup_needed = true;
5112 : : }
5113 : 9974 : else if (integer_onep (ops[bbinfo[idx].first_idx]->op))
5114 : : {
5115 : 2628 : gimple_cond_make_true (cond_stmt);
5116 : 2628 : cfg_cleanup_needed = true;
5117 : : }
5118 : : else
5119 : : {
5120 : 7346 : gimple_cond_set_code (cond_stmt, NE_EXPR);
5121 : 7346 : gimple_cond_set_lhs (cond_stmt,
5122 : 7346 : ops[bbinfo[idx].first_idx]->op);
5123 : 7346 : gimple_cond_set_rhs (cond_stmt, boolean_false_node);
5124 : : }
5125 : 19139 : update_stmt (cond_stmt);
5126 : : }
5127 : 25925 : if (bb == first_bb)
5128 : : break;
5129 : 17099 : }
5130 : :
5131 : : /* The above changes could result in basic blocks after the first
5132 : : modified one, up to and including last_bb, to be executed even if
5133 : : they would not be in the original program. If the value ranges of
5134 : : assignment lhs' in those bbs were dependent on the conditions
5135 : : guarding those basic blocks which now can change, the VRs might
5136 : : be incorrect. As no_side_effect_bb should ensure those SSA_NAMEs
5137 : : are only used within the same bb, it should be not a big deal if
5138 : : we just reset all the VRs in those bbs. See PR68671. */
5139 : 24859 : for (bb = last_bb, idx = 0; idx < max_idx; bb = single_pred (bb), idx++)
5140 : 16033 : reset_flow_sensitive_info_in_bb (bb);
5141 : : }
5142 : : return cfg_cleanup_needed;
5143 : 19081485 : }
5144 : :
5145 : : /* Remove def stmt of VAR if VAR has zero uses and recurse
5146 : : on rhs1 operand if so. */
5147 : :
5148 : : static void
5149 : 69196 : remove_visited_stmt_chain (tree var)
5150 : : {
5151 : 92768 : gimple *stmt;
5152 : 92768 : gimple_stmt_iterator gsi;
5153 : :
5154 : 116340 : while (1)
5155 : : {
5156 : 92768 : if (TREE_CODE (var) != SSA_NAME || !has_zero_uses (var))
5157 : : return;
5158 : 34938 : stmt = SSA_NAME_DEF_STMT (var);
5159 : 34938 : if (is_gimple_assign (stmt) && gimple_visited_p (stmt))
5160 : : {
5161 : 23572 : var = gimple_assign_rhs1 (stmt);
5162 : 23572 : gsi = gsi_for_stmt (stmt);
5163 : 23572 : reassoc_remove_stmt (&gsi);
5164 : 23572 : release_defs (stmt);
5165 : : }
5166 : : else
5167 : : return;
5168 : : }
5169 : : }
5170 : :
5171 : : /* This function checks three consequtive operands in
5172 : : passed operands vector OPS starting from OPINDEX and
5173 : : swaps two operands if it is profitable for binary operation
5174 : : consuming OPINDEX + 1 abnd OPINDEX + 2 operands.
5175 : :
5176 : : We pair ops with the same rank if possible. */
5177 : :
5178 : : static void
5179 : 185407 : swap_ops_for_binary_stmt (const vec<operand_entry *> &ops,
5180 : : unsigned int opindex)
5181 : : {
5182 : 185407 : operand_entry *oe1, *oe2, *oe3;
5183 : :
5184 : 185407 : oe1 = ops[opindex];
5185 : 185407 : oe2 = ops[opindex + 1];
5186 : 185407 : oe3 = ops[opindex + 2];
5187 : :
5188 : 185407 : if (oe1->rank == oe2->rank && oe2->rank != oe3->rank)
5189 : 26565 : std::swap (*oe1, *oe3);
5190 : 158842 : else if (oe1->rank == oe3->rank && oe2->rank != oe3->rank)
5191 : 201 : std::swap (*oe1, *oe2);
5192 : 185407 : }
5193 : :
5194 : : /* If definition of RHS1 or RHS2 dominates STMT, return the later of those
5195 : : two definitions, otherwise return STMT. Sets INSERT_BEFORE to indicate
5196 : : whether RHS1 op RHS2 can be inserted before or needs to be inserted
5197 : : after the returned stmt. */
5198 : :
5199 : : static inline gimple *
5200 : 945729 : find_insert_point (gimple *stmt, tree rhs1, tree rhs2, bool &insert_before)
5201 : : {
5202 : 945729 : insert_before = true;
5203 : 945729 : if (TREE_CODE (rhs1) == SSA_NAME
5204 : 945729 : && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs1)))
5205 : : {
5206 : 15683 : stmt = SSA_NAME_DEF_STMT (rhs1);
5207 : 15683 : insert_before = false;
5208 : : }
5209 : 945729 : if (TREE_CODE (rhs2) == SSA_NAME
5210 : 945729 : && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs2)))
5211 : : {
5212 : 19279 : stmt = SSA_NAME_DEF_STMT (rhs2);
5213 : 19279 : insert_before = false;
5214 : : }
5215 : 945729 : return stmt;
5216 : : }
5217 : :
5218 : : /* If the stmt that defines operand has to be inserted, insert it
5219 : : before the use. */
5220 : : static void
5221 : 99 : insert_stmt_before_use (gimple *stmt, gimple *stmt_to_insert)
5222 : : {
5223 : 99 : gcc_assert (is_gimple_assign (stmt_to_insert));
5224 : 99 : tree rhs1 = gimple_assign_rhs1 (stmt_to_insert);
5225 : 99 : tree rhs2 = gimple_assign_rhs2 (stmt_to_insert);
5226 : 99 : bool insert_before;
5227 : 99 : gimple *insert_point = find_insert_point (stmt, rhs1, rhs2, insert_before);
5228 : 99 : gimple_stmt_iterator gsi = gsi_for_stmt (insert_point);
5229 : 99 : gimple_set_uid (stmt_to_insert, gimple_uid (insert_point));
5230 : :
5231 : : /* If the insert point is not stmt, then insert_point would be
5232 : : the point where operand rhs1 or rhs2 is defined. In this case,
5233 : : stmt_to_insert has to be inserted afterwards. This would
5234 : : only happen when the stmt insertion point is flexible. */
5235 : 99 : if (insert_before)
5236 : 98 : gsi_insert_before (&gsi, stmt_to_insert, GSI_NEW_STMT);
5237 : : else
5238 : 1 : insert_stmt_after (stmt_to_insert, insert_point);
5239 : 99 : }
5240 : :
5241 : :
5242 : : /* Recursively rewrite our linearized statements so that the operators
5243 : : match those in OPS[OPINDEX], putting the computation in rank
5244 : : order. Return new lhs.
5245 : : CHANGED is true if we shouldn't reuse the lhs SSA_NAME both in
5246 : : the current stmt and during recursive invocations.
5247 : : NEXT_CHANGED is true if we shouldn't reuse the lhs SSA_NAME in
5248 : : recursive invocations. */
5249 : :
5250 : : static tree
5251 : 4779238 : rewrite_expr_tree (gimple *stmt, enum tree_code rhs_code, unsigned int opindex,
5252 : : const vec<operand_entry *> &ops, bool changed,
5253 : : bool next_changed)
5254 : : {
5255 : 4779238 : tree rhs1 = gimple_assign_rhs1 (stmt);
5256 : 4779238 : tree rhs2 = gimple_assign_rhs2 (stmt);
5257 : 4779238 : tree lhs = gimple_assign_lhs (stmt);
5258 : 4779238 : operand_entry *oe;
5259 : :
5260 : : /* The final recursion case for this function is that you have
5261 : : exactly two operations left.
5262 : : If we had exactly one op in the entire list to start with, we
5263 : : would have never called this function, and the tail recursion
5264 : : rewrites them one at a time. */
5265 : 9558476 : if (opindex + 2 == ops.length ())
5266 : : {
5267 : 4520656 : operand_entry *oe1, *oe2;
5268 : :
5269 : 4520656 : oe1 = ops[opindex];
5270 : 4520656 : oe2 = ops[opindex + 1];
5271 : :
5272 : 4520656 : if (rhs1 != oe1->op || rhs2 != oe2->op)
5273 : : {
5274 : 815437 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5275 : 815437 : unsigned int uid = gimple_uid (stmt);
5276 : :
5277 : 815437 : if (dump_file && (dump_flags & TDF_DETAILS))
5278 : : {
5279 : 31 : fprintf (dump_file, "Transforming ");
5280 : 31 : print_gimple_stmt (dump_file, stmt, 0);
5281 : : }
5282 : :
5283 : : /* If the stmt that defines operand has to be inserted, insert it
5284 : : before the use. */
5285 : 815437 : if (oe1->stmt_to_insert)
5286 : 43 : insert_stmt_before_use (stmt, oe1->stmt_to_insert);
5287 : 815437 : if (oe2->stmt_to_insert)
5288 : 54 : insert_stmt_before_use (stmt, oe2->stmt_to_insert);
5289 : : /* Even when changed is false, reassociation could have e.g. removed
5290 : : some redundant operations, so unless we are just swapping the
5291 : : arguments or unless there is no change at all (then we just
5292 : : return lhs), force creation of a new SSA_NAME. */
5293 : 815437 : if (changed || ((rhs1 != oe2->op || rhs2 != oe1->op) && opindex))
5294 : : {
5295 : 98167 : bool insert_before;
5296 : 98167 : gimple *insert_point
5297 : 98167 : = find_insert_point (stmt, oe1->op, oe2->op, insert_before);
5298 : 98167 : lhs = make_ssa_name (TREE_TYPE (lhs));
5299 : 98167 : stmt
5300 : 98167 : = gimple_build_assign (lhs, rhs_code,
5301 : : oe1->op, oe2->op);
5302 : 98167 : gimple_set_uid (stmt, uid);
5303 : 98167 : gimple_set_visited (stmt, true);
5304 : 98167 : if (insert_before)
5305 : 77530 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
5306 : : else
5307 : 20637 : insert_stmt_after (stmt, insert_point);
5308 : 98167 : }
5309 : : else
5310 : : {
5311 : 717270 : bool insert_before;
5312 : 717270 : gcc_checking_assert (find_insert_point (stmt, oe1->op, oe2->op,
5313 : : insert_before)
5314 : : == stmt);
5315 : 717270 : gimple_assign_set_rhs1 (stmt, oe1->op);
5316 : 717270 : gimple_assign_set_rhs2 (stmt, oe2->op);
5317 : 717270 : update_stmt (stmt);
5318 : : }
5319 : :
5320 : 815437 : if (rhs1 != oe1->op && rhs1 != oe2->op)
5321 : 53081 : remove_visited_stmt_chain (rhs1);
5322 : :
5323 : 815437 : if (dump_file && (dump_flags & TDF_DETAILS))
5324 : : {
5325 : 31 : fprintf (dump_file, " into ");
5326 : 31 : print_gimple_stmt (dump_file, stmt, 0);
5327 : : }
5328 : : }
5329 : 4520656 : return lhs;
5330 : : }
5331 : :
5332 : : /* If we hit here, we should have 3 or more ops left. */
5333 : 258582 : gcc_assert (opindex + 2 < ops.length ());
5334 : :
5335 : : /* Rewrite the next operator. */
5336 : 258582 : oe = ops[opindex];
5337 : :
5338 : : /* If the stmt that defines operand has to be inserted, insert it
5339 : : before the use. */
5340 : 258582 : if (oe->stmt_to_insert)
5341 : 2 : insert_stmt_before_use (stmt, oe->stmt_to_insert);
5342 : :
5343 : : /* Recurse on the LHS of the binary operator, which is guaranteed to
5344 : : be the non-leaf side. */
5345 : 258582 : tree new_rhs1
5346 : 258582 : = rewrite_expr_tree (SSA_NAME_DEF_STMT (rhs1), rhs_code, opindex + 1, ops,
5347 : 258582 : changed || oe->op != rhs2 || next_changed,
5348 : : false);
5349 : :
5350 : 258582 : if (oe->op != rhs2 || new_rhs1 != rhs1)
5351 : : {
5352 : 130193 : if (dump_file && (dump_flags & TDF_DETAILS))
5353 : : {
5354 : 6 : fprintf (dump_file, "Transforming ");
5355 : 6 : print_gimple_stmt (dump_file, stmt, 0);
5356 : : }
5357 : :
5358 : : /* If changed is false, this is either opindex == 0
5359 : : or all outer rhs2's were equal to corresponding oe->op,
5360 : : and powi_result is NULL.
5361 : : That means lhs is equivalent before and after reassociation.
5362 : : Otherwise ensure the old lhs SSA_NAME is not reused and
5363 : : create a new stmt as well, so that any debug stmts will be
5364 : : properly adjusted. */
5365 : 130193 : if (changed)
5366 : : {
5367 : 31012 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5368 : 31012 : unsigned int uid = gimple_uid (stmt);
5369 : 31012 : bool insert_before;
5370 : 31012 : gimple *insert_point = find_insert_point (stmt, new_rhs1, oe->op,
5371 : : insert_before);
5372 : :
5373 : 31012 : lhs = make_ssa_name (TREE_TYPE (lhs));
5374 : 31012 : stmt = gimple_build_assign (lhs, rhs_code,
5375 : : new_rhs1, oe->op);
5376 : 31012 : gimple_set_uid (stmt, uid);
5377 : 31012 : gimple_set_visited (stmt, true);
5378 : 31012 : if (insert_before)
5379 : 17573 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
5380 : : else
5381 : 13439 : insert_stmt_after (stmt, insert_point);
5382 : : }
5383 : : else
5384 : : {
5385 : 99181 : bool insert_before;
5386 : 99181 : gcc_checking_assert (find_insert_point (stmt, new_rhs1, oe->op,
5387 : : insert_before)
5388 : : == stmt);
5389 : 99181 : gimple_assign_set_rhs1 (stmt, new_rhs1);
5390 : 99181 : gimple_assign_set_rhs2 (stmt, oe->op);
5391 : 99181 : update_stmt (stmt);
5392 : : }
5393 : :
5394 : 130193 : if (dump_file && (dump_flags & TDF_DETAILS))
5395 : : {
5396 : 6 : fprintf (dump_file, " into ");
5397 : 6 : print_gimple_stmt (dump_file, stmt, 0);
5398 : : }
5399 : : }
5400 : : return lhs;
5401 : : }
5402 : :
5403 : : /* Find out how many cycles we need to compute statements chain.
5404 : : OPS_NUM holds number os statements in a chain. CPU_WIDTH is a
5405 : : maximum number of independent statements we may execute per cycle. */
5406 : :
5407 : : static int
5408 : 22228 : get_required_cycles (int ops_num, int cpu_width)
5409 : : {
5410 : 22228 : int res;
5411 : 22228 : int elog;
5412 : 22228 : unsigned int rest;
5413 : :
5414 : : /* While we have more than 2 * cpu_width operands
5415 : : we may reduce number of operands by cpu_width
5416 : : per cycle. */
5417 : 22228 : res = ops_num / (2 * cpu_width);
5418 : :
5419 : : /* Remained operands count may be reduced twice per cycle
5420 : : until we have only one operand. */
5421 : 22228 : rest = (unsigned)(ops_num - res * cpu_width);
5422 : 22228 : elog = exact_log2 (rest);
5423 : 9888 : if (elog >= 0)
5424 : 9888 : res += elog;
5425 : : else
5426 : 24680 : res += floor_log2 (rest) + 1;
5427 : :
5428 : 22228 : return res;
5429 : : }
5430 : :
5431 : : /* Given that the target fully pipelines FMA instructions, return the latency
5432 : : of MULT_EXPRs that can't be hidden by the FMAs. WIDTH is the number of
5433 : : pipes. */
5434 : :
5435 : : static inline int
5436 : 0 : get_mult_latency_consider_fma (int ops_num, int mult_num, int width)
5437 : : {
5438 : 0 : gcc_checking_assert (mult_num && mult_num <= ops_num);
5439 : :
5440 : : /* For each partition, if mult_num == ops_num, there's latency(MULT)*2.
5441 : : e.g:
5442 : :
5443 : : A * B + C * D
5444 : : =>
5445 : : _1 = A * B;
5446 : : _2 = .FMA (C, D, _1);
5447 : :
5448 : : Otherwise there's latency(MULT)*1 in the first FMA. */
5449 : 0 : return CEIL (ops_num, width) == CEIL (mult_num, width) ? 2 : 1;
5450 : : }
5451 : :
5452 : : /* Returns an optimal number of registers to use for computation of
5453 : : given statements.
5454 : :
5455 : : LHS is the result ssa name of OPS. MULT_NUM is number of sub-expressions
5456 : : that are MULT_EXPRs, when OPS are PLUS_EXPRs or MINUS_EXPRs. */
5457 : :
5458 : : static int
5459 : 23647 : get_reassociation_width (vec<operand_entry *> *ops, int mult_num, tree lhs,
5460 : : enum tree_code opc, machine_mode mode)
5461 : : {
5462 : 23647 : int param_width = param_tree_reassoc_width;
5463 : 23647 : int width;
5464 : 23647 : int width_min;
5465 : 23647 : int cycles_best;
5466 : 23647 : int ops_num = ops->length ();
5467 : :
5468 : 23647 : if (param_width > 0)
5469 : : width = param_width;
5470 : : else
5471 : 23597 : width = targetm.sched.reassociation_width (opc, mode);
5472 : :
5473 : 23647 : if (width == 1)
5474 : : return width;
5475 : :
5476 : : /* Get the minimal time required for sequence computation. */
5477 : 8574 : cycles_best = get_required_cycles (ops_num, width);
5478 : :
5479 : : /* Check if we may use less width and still compute sequence for
5480 : : the same time. It will allow us to reduce registers usage.
5481 : : get_required_cycles is monotonically increasing with lower width
5482 : : so we can perform a binary search for the minimal width that still
5483 : : results in the optimal cycle count. */
5484 : 8574 : width_min = 1;
5485 : :
5486 : : /* If the target fully pipelines FMA instruction, the multiply part can start
5487 : : already if its operands are ready. Assuming symmetric pipes are used for
5488 : : FMUL/FADD/FMA, then for a sequence of FMA like:
5489 : :
5490 : : _8 = .FMA (_2, _3, _1);
5491 : : _9 = .FMA (_5, _4, _8);
5492 : : _10 = .FMA (_7, _6, _9);
5493 : :
5494 : : , if width=1, the latency is latency(MULT) + latency(ADD)*3.
5495 : : While with width=2:
5496 : :
5497 : : _8 = _4 * _5;
5498 : : _9 = .FMA (_2, _3, _1);
5499 : : _10 = .FMA (_6, _7, _8);
5500 : : _11 = _9 + _10;
5501 : :
5502 : : , it is latency(MULT)*2 + latency(ADD)*2. Assuming latency(MULT) >=
5503 : : latency(ADD), the first variant is preferred.
5504 : :
5505 : : Find out if we can get a smaller width considering FMA.
5506 : : Assume FMUL and FMA use the same units that can also do FADD.
5507 : : For other scenarios, such as when FMUL and FADD are using separated units,
5508 : : the following code may not apply. */
5509 : :
5510 : 8574 : int width_mult = targetm.sched.reassociation_width (MULT_EXPR, mode);
5511 : 8574 : if (width > 1 && mult_num && param_fully_pipelined_fma
5512 : 0 : && width_mult <= width)
5513 : : {
5514 : : /* Latency of MULT_EXPRs. */
5515 : 0 : int lat_mul
5516 : 0 : = get_mult_latency_consider_fma (ops_num, mult_num, width_mult);
5517 : :
5518 : : /* Quick search might not apply. So start from 1. */
5519 : 0 : for (int i = 1; i < width_mult; i++)
5520 : : {
5521 : 0 : int lat_mul_new
5522 : 0 : = get_mult_latency_consider_fma (ops_num, mult_num, i);
5523 : 0 : int lat_add_new = get_required_cycles (ops_num, i);
5524 : :
5525 : : /* Assume latency(MULT) >= latency(ADD). */
5526 : 0 : if (lat_mul - lat_mul_new >= lat_add_new - cycles_best)
5527 : : {
5528 : : width = i;
5529 : : break;
5530 : : }
5531 : : }
5532 : : }
5533 : : else
5534 : : {
5535 : 20650 : while (width > width_min)
5536 : : {
5537 : 13654 : int width_mid = (width + width_min) / 2;
5538 : :
5539 : 13654 : if (get_required_cycles (ops_num, width_mid) == cycles_best)
5540 : : width = width_mid;
5541 : 1749 : else if (width_min < width_mid)
5542 : : width_min = width_mid;
5543 : : else
5544 : : break;
5545 : : }
5546 : : }
5547 : :
5548 : : /* If there's loop dependent FMA result, return width=2 to avoid it. This is
5549 : : better than skipping these FMA candidates in widening_mul. */
5550 : 8574 : if (width == 1
5551 : 8574 : && maybe_le (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (lhs))),
5552 : : param_avoid_fma_max_bits))
5553 : : {
5554 : : /* Look for cross backedge dependency:
5555 : : 1. LHS is a phi argument in the same basic block it is defined.
5556 : : 2. And the result of the phi node is used in OPS. */
5557 : 6580 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (lhs));
5558 : :
5559 : 6580 : use_operand_p use_p;
5560 : 6580 : imm_use_iterator iter;
5561 : 12086 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5562 : 9472 : if (gphi *phi = dyn_cast<gphi *> (USE_STMT (use_p)))
5563 : : {
5564 : 5854 : if (gimple_phi_arg_edge (phi, phi_arg_index_from_use (use_p))->src
5565 : : != bb)
5566 : 0 : continue;
5567 : 5854 : tree phi_result = gimple_phi_result (phi);
5568 : 5854 : operand_entry *oe;
5569 : 5854 : unsigned int j;
5570 : 24956 : FOR_EACH_VEC_ELT (*ops, j, oe)
5571 : : {
5572 : 17562 : if (TREE_CODE (oe->op) != SSA_NAME)
5573 : 0 : continue;
5574 : :
5575 : : /* Result of phi is operand of PLUS_EXPR. */
5576 : 17562 : if (oe->op == phi_result)
5577 : 3966 : return 2;
5578 : :
5579 : : /* Check is result of phi is operand of MULT_EXPR. */
5580 : 13596 : gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
5581 : 13596 : if (is_gimple_assign (def_stmt)
5582 : 13596 : && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR)
5583 : : {
5584 : 2924 : tree rhs = gimple_assign_rhs1 (def_stmt);
5585 : 2924 : if (TREE_CODE (rhs) == SSA_NAME)
5586 : : {
5587 : 2924 : if (rhs == phi_result)
5588 : : return 2;
5589 : 2924 : def_stmt = SSA_NAME_DEF_STMT (rhs);
5590 : : }
5591 : : }
5592 : 13596 : if (is_gimple_assign (def_stmt)
5593 : 13596 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR)
5594 : : {
5595 : 11708 : if (gimple_assign_rhs1 (def_stmt) == phi_result
5596 : 11708 : || gimple_assign_rhs2 (def_stmt) == phi_result)
5597 : : return 2;
5598 : : }
5599 : : }
5600 : : }
5601 : : }
5602 : :
5603 : : return width;
5604 : : }
5605 : :
5606 : : #define SPECIAL_BIASED_END_STMT 0 /* It is the end stmt of all ops. */
5607 : : #define BIASED_END_STMT 1 /* It is the end stmt of normal or biased ops. */
5608 : : #define NORMAL_END_STMT 2 /* It is the end stmt of normal ops. */
5609 : :
5610 : : /* Rewrite statements with dependency chain with regard the chance to generate
5611 : : FMA.
5612 : : For the chain with FMA: Try to keep fma opportunity as much as possible.
5613 : : For the chain without FMA: Putting the computation in rank order and trying
5614 : : to allow operations to be executed in parallel.
5615 : : E.g.
5616 : : e + f + a * b + c * d;
5617 : :
5618 : : ssa1 = e + a * b;
5619 : : ssa2 = f + c * d;
5620 : : ssa3 = ssa1 + ssa2;
5621 : :
5622 : : This reassociation approach preserves the chance of fma generation as much
5623 : : as possible.
5624 : :
5625 : : Another thing is to avoid adding loop-carried ops to long chains, otherwise
5626 : : the whole chain will have dependencies across the loop iteration. Just keep
5627 : : loop-carried ops in a separate chain.
5628 : : E.g.
5629 : : x_1 = phi (x_0, x_2)
5630 : : y_1 = phi (y_0, y_2)
5631 : :
5632 : : a + b + c + d + e + x1 + y1
5633 : :
5634 : : SSA1 = a + b;
5635 : : SSA2 = c + d;
5636 : : SSA3 = SSA1 + e;
5637 : : SSA4 = SSA3 + SSA2;
5638 : : SSA5 = x1 + y1;
5639 : : SSA6 = SSA4 + SSA5;
5640 : : */
5641 : : static void
5642 : 1574 : rewrite_expr_tree_parallel (gassign *stmt, int width, bool has_fma,
5643 : : const vec<operand_entry *> &ops)
5644 : : {
5645 : 1574 : enum tree_code opcode = gimple_assign_rhs_code (stmt);
5646 : 1574 : int op_num = ops.length ();
5647 : 1574 : int op_normal_num = op_num;
5648 : 1574 : gcc_assert (op_num > 0);
5649 : 1574 : int stmt_num = op_num - 1;
5650 : 1574 : gimple **stmts = XALLOCAVEC (gimple *, stmt_num);
5651 : 1574 : int i = 0, j = 0;
5652 : 1574 : tree tmp_op[2], op1;
5653 : 1574 : operand_entry *oe;
5654 : 1574 : gimple *stmt1 = NULL;
5655 : 1574 : tree last_rhs1 = gimple_assign_rhs1 (stmt);
5656 : 1574 : int last_rhs1_stmt_index = 0, last_rhs2_stmt_index = 0;
5657 : 1574 : int width_active = 0, width_count = 0;
5658 : 1574 : bool has_biased = false, ops_changed = false;
5659 : 1574 : auto_vec<operand_entry *> ops_normal;
5660 : 1574 : auto_vec<operand_entry *> ops_biased;
5661 : 1574 : vec<operand_entry *> *ops1;
5662 : :
5663 : : /* We start expression rewriting from the top statements.
5664 : : So, in this loop we create a full list of statements
5665 : : we will work with. */
5666 : 1574 : stmts[stmt_num - 1] = stmt;
5667 : 7612 : for (i = stmt_num - 2; i >= 0; i--)
5668 : 6038 : stmts[i] = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmts[i+1]));
5669 : :
5670 : : /* Avoid adding loop-carried ops to long chains, first filter out the
5671 : : loop-carried. But we need to make sure that the length of the remainder
5672 : : is not less than 4, which is the smallest ops length we can break the
5673 : : dependency. */
5674 : 10760 : FOR_EACH_VEC_ELT (ops, i, oe)
5675 : : {
5676 : 9186 : if (TREE_CODE (oe->op) == SSA_NAME
5677 : 9041 : && bitmap_bit_p (biased_names, SSA_NAME_VERSION (oe->op))
5678 : 9431 : && op_normal_num > 4)
5679 : : {
5680 : 217 : ops_biased.safe_push (oe);
5681 : 217 : has_biased = true;
5682 : 217 : op_normal_num --;
5683 : : }
5684 : : else
5685 : 8969 : ops_normal.safe_push (oe);
5686 : : }
5687 : :
5688 : : /* Width should not be larger than ops length / 2, since we can not create
5689 : : more parallel dependency chains that exceeds such value. */
5690 : 1574 : int width_normal = op_normal_num / 2;
5691 : 1574 : int width_biased = (op_num - op_normal_num) / 2;
5692 : 1574 : width_normal = width <= width_normal ? width : width_normal;
5693 : 1574 : width_biased = width <= width_biased ? width : width_biased;
5694 : :
5695 : 1574 : ops1 = &ops_normal;
5696 : 1574 : width_count = width_active = width_normal;
5697 : :
5698 : : /* Build parallel dependency chain according to width. */
5699 : 9186 : for (i = 0; i < stmt_num; i++)
5700 : : {
5701 : 7612 : if (dump_file && (dump_flags & TDF_DETAILS))
5702 : : {
5703 : 6 : fprintf (dump_file, "Transforming ");
5704 : 6 : print_gimple_stmt (dump_file, stmts[i], 0);
5705 : : }
5706 : :
5707 : : /* When the work of normal ops is over, but the loop is not over,
5708 : : continue to do biased ops. */
5709 : 7612 : if (width_count == 0 && ops1 == &ops_normal)
5710 : : {
5711 : 213 : ops1 = &ops_biased;
5712 : 213 : width_count = width_active = width_biased;
5713 : 213 : ops_changed = true;
5714 : : }
5715 : :
5716 : : /* Swap the operands if no FMA in the chain. */
5717 : 7612 : if (ops1->length () > 2 && !has_fma)
5718 : 3630 : swap_ops_for_binary_stmt (*ops1, ops1->length () - 3);
5719 : :
5720 : 7612 : if (i < width_active
5721 : 4296 : || (ops_changed && i <= (last_rhs1_stmt_index + width_active)))
5722 : : {
5723 : 9957 : for (j = 0; j < 2; j++)
5724 : : {
5725 : 6638 : oe = ops1->pop ();
5726 : 6638 : tmp_op[j] = oe->op;
5727 : : /* If the stmt that defines operand has to be inserted, insert it
5728 : : before the use. */
5729 : 6638 : stmt1 = oe->stmt_to_insert;
5730 : 6638 : if (stmt1)
5731 : 0 : insert_stmt_before_use (stmts[i], stmt1);
5732 : 6638 : stmt1 = NULL;
5733 : : }
5734 : 3319 : stmts[i] = build_and_add_sum (TREE_TYPE (last_rhs1),
5735 : : tmp_op[1],
5736 : : tmp_op[0],
5737 : : opcode);
5738 : 3319 : gimple_set_visited (stmts[i], true);
5739 : :
5740 : : }
5741 : : else
5742 : : {
5743 : : /* We keep original statement only for the last one. All others are
5744 : : recreated. */
5745 : 4293 : if (!ops1->length ())
5746 : : {
5747 : : /* For biased length equal to 2. */
5748 : 1745 : if (width_count == BIASED_END_STMT && !last_rhs2_stmt_index)
5749 : 1 : last_rhs2_stmt_index = i - 1;
5750 : :
5751 : : /* When width_count == 2 and there is no biased, just finish. */
5752 : 1745 : if (width_count == NORMAL_END_STMT && !has_biased)
5753 : : {
5754 : 1361 : last_rhs1_stmt_index = i - 1;
5755 : 1361 : last_rhs2_stmt_index = i - 2;
5756 : : }
5757 : 1745 : if (last_rhs1_stmt_index && (last_rhs2_stmt_index || !has_biased))
5758 : : {
5759 : : /* We keep original statement only for the last one. All
5760 : : others are recreated. */
5761 : 1363 : gimple_assign_set_rhs1 (stmts[i], gimple_assign_lhs
5762 : 1363 : (stmts[last_rhs1_stmt_index]));
5763 : 1363 : gimple_assign_set_rhs2 (stmts[i], gimple_assign_lhs
5764 : 1363 : (stmts[last_rhs2_stmt_index]));
5765 : 1363 : update_stmt (stmts[i]);
5766 : : }
5767 : : else
5768 : : {
5769 : 1146 : stmts[i] =
5770 : 382 : build_and_add_sum (TREE_TYPE (last_rhs1),
5771 : 382 : gimple_assign_lhs (stmts[i-width_count]),
5772 : : gimple_assign_lhs
5773 : 382 : (stmts[i-width_count+1]),
5774 : : opcode);
5775 : 382 : gimple_set_visited (stmts[i], true);
5776 : 382 : width_count--;
5777 : :
5778 : : /* It is the end of normal or biased ops.
5779 : : last_rhs1_stmt_index used to record the last stmt index
5780 : : for normal ops. last_rhs2_stmt_index used to record the
5781 : : last stmt index for biased ops. */
5782 : 382 : if (width_count == BIASED_END_STMT)
5783 : : {
5784 : 214 : gcc_assert (has_biased);
5785 : 214 : if (ops_biased.length ())
5786 : : last_rhs1_stmt_index = i;
5787 : : else
5788 : 1 : last_rhs2_stmt_index = i;
5789 : : width_count--;
5790 : : }
5791 : : }
5792 : : }
5793 : : else
5794 : : {
5795 : : /* Attach the rest ops to the parallel dependency chain. */
5796 : 2548 : oe = ops1->pop ();
5797 : 2548 : op1 = oe->op;
5798 : 2548 : stmt1 = oe->stmt_to_insert;
5799 : 2548 : if (stmt1)
5800 : 0 : insert_stmt_before_use (stmts[i], stmt1);
5801 : 2548 : stmt1 = NULL;
5802 : :
5803 : : /* For only one biased ops. */
5804 : 2548 : if (width_count == SPECIAL_BIASED_END_STMT)
5805 : : {
5806 : : /* We keep original statement only for the last one. All
5807 : : others are recreated. */
5808 : 211 : gcc_assert (has_biased);
5809 : 211 : gimple_assign_set_rhs1 (stmts[i], gimple_assign_lhs
5810 : 211 : (stmts[last_rhs1_stmt_index]));
5811 : 211 : gimple_assign_set_rhs2 (stmts[i], op1);
5812 : 211 : update_stmt (stmts[i]);
5813 : : }
5814 : : else
5815 : : {
5816 : 2337 : stmts[i] = build_and_add_sum (TREE_TYPE (last_rhs1),
5817 : : gimple_assign_lhs
5818 : 2337 : (stmts[i-width_active]),
5819 : : op1,
5820 : : opcode);
5821 : 2337 : gimple_set_visited (stmts[i], true);
5822 : : }
5823 : : }
5824 : : }
5825 : :
5826 : 7612 : if (dump_file && (dump_flags & TDF_DETAILS))
5827 : : {
5828 : 6 : fprintf (dump_file, " into ");
5829 : 6 : print_gimple_stmt (dump_file, stmts[i], 0);
5830 : : }
5831 : : }
5832 : :
5833 : 1574 : remove_visited_stmt_chain (last_rhs1);
5834 : 1574 : }
5835 : :
5836 : : /* Transform STMT, which is really (A +B) + (C + D) into the left
5837 : : linear form, ((A+B)+C)+D.
5838 : : Recurse on D if necessary. */
5839 : :
5840 : : static void
5841 : 2301 : linearize_expr (gimple *stmt)
5842 : : {
5843 : 2301 : gimple_stmt_iterator gsi;
5844 : 2301 : gimple *binlhs = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
5845 : 2301 : gimple *binrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
5846 : 2301 : gimple *oldbinrhs = binrhs;
5847 : 2301 : enum tree_code rhscode = gimple_assign_rhs_code (stmt);
5848 : 2301 : gimple *newbinrhs = NULL;
5849 : 2301 : class loop *loop = loop_containing_stmt (stmt);
5850 : 2301 : tree lhs = gimple_assign_lhs (stmt);
5851 : :
5852 : 2301 : gcc_assert (is_reassociable_op (binlhs, rhscode, loop)
5853 : : && is_reassociable_op (binrhs, rhscode, loop));
5854 : :
5855 : 2301 : gsi = gsi_for_stmt (stmt);
5856 : :
5857 : 2301 : gimple_assign_set_rhs2 (stmt, gimple_assign_rhs1 (binrhs));
5858 : 2301 : binrhs = gimple_build_assign (make_ssa_name (TREE_TYPE (lhs)),
5859 : : gimple_assign_rhs_code (binrhs),
5860 : : gimple_assign_lhs (binlhs),
5861 : : gimple_assign_rhs2 (binrhs));
5862 : 2301 : gimple_assign_set_rhs1 (stmt, gimple_assign_lhs (binrhs));
5863 : 2301 : gsi_insert_before (&gsi, binrhs, GSI_SAME_STMT);
5864 : 2301 : gimple_set_uid (binrhs, gimple_uid (stmt));
5865 : :
5866 : 2301 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
5867 : 2294 : newbinrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
5868 : :
5869 : 2301 : if (dump_file && (dump_flags & TDF_DETAILS))
5870 : : {
5871 : 0 : fprintf (dump_file, "Linearized: ");
5872 : 0 : print_gimple_stmt (dump_file, stmt, 0);
5873 : : }
5874 : :
5875 : 2301 : reassociate_stats.linearized++;
5876 : 2301 : update_stmt (stmt);
5877 : :
5878 : 2301 : gsi = gsi_for_stmt (oldbinrhs);
5879 : 2301 : reassoc_remove_stmt (&gsi);
5880 : 2301 : release_defs (oldbinrhs);
5881 : :
5882 : 2301 : gimple_set_visited (stmt, true);
5883 : 2301 : gimple_set_visited (binlhs, true);
5884 : 2301 : gimple_set_visited (binrhs, true);
5885 : :
5886 : : /* Tail recurse on the new rhs if it still needs reassociation. */
5887 : 2301 : if (newbinrhs && is_reassociable_op (newbinrhs, rhscode, loop))
5888 : : /* ??? This should probably be linearize_expr (newbinrhs) but I don't
5889 : : want to change the algorithm while converting to tuples. */
5890 : 578 : linearize_expr (stmt);
5891 : 2301 : }
5892 : :
5893 : : /* If LHS has a single immediate use that is a GIMPLE_ASSIGN statement, return
5894 : : it. Otherwise, return NULL. */
5895 : :
5896 : : static gimple *
5897 : 417033 : get_single_immediate_use (tree lhs)
5898 : : {
5899 : 417033 : use_operand_p immuse;
5900 : 417033 : gimple *immusestmt;
5901 : :
5902 : 417033 : if (TREE_CODE (lhs) == SSA_NAME
5903 : 417033 : && single_imm_use (lhs, &immuse, &immusestmt)
5904 : 731212 : && is_gimple_assign (immusestmt))
5905 : : return immusestmt;
5906 : :
5907 : : return NULL;
5908 : : }
5909 : :
5910 : : /* Recursively negate the value of TONEGATE, and return the SSA_NAME
5911 : : representing the negated value. Insertions of any necessary
5912 : : instructions go before GSI.
5913 : : This function is recursive in that, if you hand it "a_5" as the
5914 : : value to negate, and a_5 is defined by "a_5 = b_3 + b_4", it will
5915 : : transform b_3 + b_4 into a_5 = -b_3 + -b_4. */
5916 : :
5917 : : static tree
5918 : 79181 : negate_value (tree tonegate, gimple_stmt_iterator *gsip)
5919 : : {
5920 : 79181 : gimple *negatedefstmt = NULL;
5921 : 79181 : tree resultofnegate;
5922 : 79181 : gimple_stmt_iterator gsi;
5923 : 79181 : unsigned int uid;
5924 : :
5925 : : /* If we are trying to negate a name, defined by an add, negate the
5926 : : add operands instead. */
5927 : 79181 : if (TREE_CODE (tonegate) == SSA_NAME)
5928 : 77467 : negatedefstmt = SSA_NAME_DEF_STMT (tonegate);
5929 : 79181 : if (TREE_CODE (tonegate) == SSA_NAME
5930 : 77467 : && is_gimple_assign (negatedefstmt)
5931 : 65140 : && TREE_CODE (gimple_assign_lhs (negatedefstmt)) == SSA_NAME
5932 : 65140 : && has_single_use (gimple_assign_lhs (negatedefstmt))
5933 : 126157 : && gimple_assign_rhs_code (negatedefstmt) == PLUS_EXPR)
5934 : : {
5935 : 907 : tree rhs1 = gimple_assign_rhs1 (negatedefstmt);
5936 : 907 : tree rhs2 = gimple_assign_rhs2 (negatedefstmt);
5937 : 907 : tree lhs = gimple_assign_lhs (negatedefstmt);
5938 : 907 : gimple *g;
5939 : :
5940 : 907 : gsi = gsi_for_stmt (negatedefstmt);
5941 : 907 : rhs1 = negate_value (rhs1, &gsi);
5942 : :
5943 : 907 : gsi = gsi_for_stmt (negatedefstmt);
5944 : 907 : rhs2 = negate_value (rhs2, &gsi);
5945 : :
5946 : 907 : gsi = gsi_for_stmt (negatedefstmt);
5947 : 907 : lhs = make_ssa_name (TREE_TYPE (lhs));
5948 : 907 : gimple_set_visited (negatedefstmt, true);
5949 : 907 : g = gimple_build_assign (lhs, PLUS_EXPR, rhs1, rhs2);
5950 : 907 : gimple_set_uid (g, gimple_uid (negatedefstmt));
5951 : 907 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5952 : 907 : return lhs;
5953 : : }
5954 : :
5955 : 78274 : tonegate = fold_build1 (NEGATE_EXPR, TREE_TYPE (tonegate), tonegate);
5956 : 78274 : resultofnegate = force_gimple_operand_gsi (gsip, tonegate, true,
5957 : : NULL_TREE, true, GSI_SAME_STMT);
5958 : 78274 : gsi = *gsip;
5959 : 78274 : uid = gimple_uid (gsi_stmt (gsi));
5960 : 309668 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
5961 : : {
5962 : 149416 : gimple *stmt = gsi_stmt (gsi);
5963 : 149416 : if (gimple_uid (stmt) != 0)
5964 : : break;
5965 : 76560 : gimple_set_uid (stmt, uid);
5966 : : }
5967 : : return resultofnegate;
5968 : : }
5969 : :
5970 : : /* Return true if we should break up the subtract in STMT into an add
5971 : : with negate. This is true when we the subtract operands are really
5972 : : adds, or the subtract itself is used in an add expression. In
5973 : : either case, breaking up the subtract into an add with negate
5974 : : exposes the adds to reassociation. */
5975 : :
5976 : : static bool
5977 : 302419 : should_break_up_subtract (gimple *stmt)
5978 : : {
5979 : 302419 : tree lhs = gimple_assign_lhs (stmt);
5980 : 302419 : tree binlhs = gimple_assign_rhs1 (stmt);
5981 : 302419 : tree binrhs = gimple_assign_rhs2 (stmt);
5982 : 302419 : gimple *immusestmt;
5983 : 302419 : class loop *loop = loop_containing_stmt (stmt);
5984 : :
5985 : 302419 : if (TREE_CODE (binlhs) == SSA_NAME
5986 : 302419 : && is_reassociable_op (SSA_NAME_DEF_STMT (binlhs), PLUS_EXPR, loop))
5987 : : return true;
5988 : :
5989 : 280773 : if (TREE_CODE (binrhs) == SSA_NAME
5990 : 280773 : && is_reassociable_op (SSA_NAME_DEF_STMT (binrhs), PLUS_EXPR, loop))
5991 : : return true;
5992 : :
5993 : 279971 : if (TREE_CODE (lhs) == SSA_NAME
5994 : 279971 : && (immusestmt = get_single_immediate_use (lhs))
5995 : 118650 : && is_gimple_assign (immusestmt)
5996 : 398621 : && (gimple_assign_rhs_code (immusestmt) == PLUS_EXPR
5997 : 76633 : || (gimple_assign_rhs_code (immusestmt) == MINUS_EXPR
5998 : 2317 : && gimple_assign_rhs1 (immusestmt) == lhs)
5999 : 74326 : || gimple_assign_rhs_code (immusestmt) == MULT_EXPR))
6000 : : return true;
6001 : : return false;
6002 : : }
6003 : :
6004 : : /* Transform STMT from A - B into A + -B. */
6005 : :
6006 : : static void
6007 : 77367 : break_up_subtract (gimple *stmt, gimple_stmt_iterator *gsip)
6008 : : {
6009 : 77367 : tree rhs1 = gimple_assign_rhs1 (stmt);
6010 : 77367 : tree rhs2 = gimple_assign_rhs2 (stmt);
6011 : :
6012 : 77367 : if (dump_file && (dump_flags & TDF_DETAILS))
6013 : : {
6014 : 0 : fprintf (dump_file, "Breaking up subtract ");
6015 : 0 : print_gimple_stmt (dump_file, stmt, 0);
6016 : : }
6017 : :
6018 : 77367 : rhs2 = negate_value (rhs2, gsip);
6019 : 77367 : gimple_assign_set_rhs_with_ops (gsip, PLUS_EXPR, rhs1, rhs2);
6020 : 77367 : update_stmt (stmt);
6021 : 77367 : }
6022 : :
6023 : : /* Determine whether STMT is a builtin call that raises an SSA name
6024 : : to an integer power and has only one use. If so, and this is early
6025 : : reassociation and unsafe math optimizations are permitted, place
6026 : : the SSA name in *BASE and the exponent in *EXPONENT, and return TRUE.
6027 : : If any of these conditions does not hold, return FALSE. */
6028 : :
6029 : : static bool
6030 : 125 : acceptable_pow_call (gcall *stmt, tree *base, HOST_WIDE_INT *exponent)
6031 : : {
6032 : 125 : tree arg1;
6033 : 125 : REAL_VALUE_TYPE c, cint;
6034 : :
6035 : 125 : switch (gimple_call_combined_fn (stmt))
6036 : : {
6037 : 16 : CASE_CFN_POW:
6038 : 16 : if (flag_errno_math)
6039 : : return false;
6040 : :
6041 : 16 : *base = gimple_call_arg (stmt, 0);
6042 : 16 : arg1 = gimple_call_arg (stmt, 1);
6043 : :
6044 : 16 : if (TREE_CODE (arg1) != REAL_CST)
6045 : : return false;
6046 : :
6047 : 16 : c = TREE_REAL_CST (arg1);
6048 : :
6049 : 16 : if (REAL_EXP (&c) > HOST_BITS_PER_WIDE_INT)
6050 : : return false;
6051 : :
6052 : 16 : *exponent = real_to_integer (&c);
6053 : 16 : real_from_integer (&cint, VOIDmode, *exponent, SIGNED);
6054 : 16 : if (!real_identical (&c, &cint))
6055 : : return false;
6056 : :
6057 : : break;
6058 : :
6059 : 10 : CASE_CFN_POWI:
6060 : 10 : *base = gimple_call_arg (stmt, 0);
6061 : 10 : arg1 = gimple_call_arg (stmt, 1);
6062 : :
6063 : 10 : if (!tree_fits_shwi_p (arg1))
6064 : : return false;
6065 : :
6066 : 10 : *exponent = tree_to_shwi (arg1);
6067 : 10 : break;
6068 : :
6069 : : default:
6070 : : return false;
6071 : : }
6072 : :
6073 : : /* Expanding negative exponents is generally unproductive, so we don't
6074 : : complicate matters with those. Exponents of zero and one should
6075 : : have been handled by expression folding. */
6076 : 18 : if (*exponent < 2 || TREE_CODE (*base) != SSA_NAME)
6077 : : return false;
6078 : :
6079 : : return true;
6080 : : }
6081 : :
6082 : : /* Try to derive and add operand entry for OP to *OPS. Return false if
6083 : : unsuccessful. */
6084 : :
6085 : : static bool
6086 : 9439481 : try_special_add_to_ops (vec<operand_entry *> *ops,
6087 : : enum tree_code code,
6088 : : tree op, gimple* def_stmt)
6089 : : {
6090 : 9439481 : tree base = NULL_TREE;
6091 : 9439481 : HOST_WIDE_INT exponent = 0;
6092 : :
6093 : 9439481 : if (TREE_CODE (op) != SSA_NAME
6094 : 9439481 : || ! has_single_use (op))
6095 : : return false;
6096 : :
6097 : 3596205 : if (code == MULT_EXPR
6098 : 753115 : && reassoc_insert_powi_p
6099 : 367794 : && flag_unsafe_math_optimizations
6100 : 29996 : && is_gimple_call (def_stmt)
6101 : 3596330 : && acceptable_pow_call (as_a <gcall *> (def_stmt), &base, &exponent))
6102 : : {
6103 : 18 : add_repeat_to_ops_vec (ops, base, exponent);
6104 : 18 : gimple_set_visited (def_stmt, true);
6105 : 18 : return true;
6106 : : }
6107 : 3596187 : else if (code == MULT_EXPR
6108 : 753097 : && is_gimple_assign (def_stmt)
6109 : 713807 : && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
6110 : 189 : && !HONOR_SNANS (TREE_TYPE (op))
6111 : 189 : && (!HONOR_SIGNED_ZEROS (TREE_TYPE (op))
6112 : 0 : || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (op)))
6113 : 3596376 : && (!FLOAT_TYPE_P (TREE_TYPE (op))
6114 : 51 : || !DECIMAL_FLOAT_MODE_P (element_mode (op))))
6115 : : {
6116 : 182 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
6117 : 182 : tree cst = build_minus_one_cst (TREE_TYPE (op));
6118 : 182 : add_to_ops_vec (ops, rhs1);
6119 : 182 : add_to_ops_vec (ops, cst);
6120 : 182 : gimple_set_visited (def_stmt, true);
6121 : 182 : return true;
6122 : : }
6123 : :
6124 : : return false;
6125 : : }
6126 : :
6127 : : /* Recursively linearize a binary expression that is the RHS of STMT.
6128 : : Place the operands of the expression tree in the vector named OPS. */
6129 : :
6130 : : static void
6131 : 4862819 : linearize_expr_tree (vec<operand_entry *> *ops, gimple *stmt,
6132 : : bool is_associative, bool set_visited)
6133 : : {
6134 : 4862819 : tree binlhs = gimple_assign_rhs1 (stmt);
6135 : 4862819 : tree binrhs = gimple_assign_rhs2 (stmt);
6136 : 4862819 : gimple *binlhsdef = NULL, *binrhsdef = NULL;
6137 : 4862819 : bool binlhsisreassoc = false;
6138 : 4862819 : bool binrhsisreassoc = false;
6139 : 4862819 : enum tree_code rhscode = gimple_assign_rhs_code (stmt);
6140 : 4862819 : class loop *loop = loop_containing_stmt (stmt);
6141 : :
6142 : 4862819 : if (set_visited)
6143 : 4822231 : gimple_set_visited (stmt, true);
6144 : :
6145 : 4862819 : if (TREE_CODE (binlhs) == SSA_NAME)
6146 : : {
6147 : 4860671 : binlhsdef = SSA_NAME_DEF_STMT (binlhs);
6148 : 4860671 : binlhsisreassoc = (is_reassociable_op (binlhsdef, rhscode, loop)
6149 : 4860671 : && !stmt_could_throw_p (cfun, binlhsdef));
6150 : : }
6151 : :
6152 : 4862819 : if (TREE_CODE (binrhs) == SSA_NAME)
6153 : : {
6154 : 1555166 : binrhsdef = SSA_NAME_DEF_STMT (binrhs);
6155 : 1555166 : binrhsisreassoc = (is_reassociable_op (binrhsdef, rhscode, loop)
6156 : 1555166 : && !stmt_could_throw_p (cfun, binrhsdef));
6157 : : }
6158 : :
6159 : : /* If the LHS is not reassociable, but the RHS is, we need to swap
6160 : : them. If neither is reassociable, there is nothing we can do, so
6161 : : just put them in the ops vector. If the LHS is reassociable,
6162 : : linearize it. If both are reassociable, then linearize the RHS
6163 : : and the LHS. */
6164 : :
6165 : 4862819 : if (!binlhsisreassoc)
6166 : : {
6167 : : /* If this is not a associative operation like division, give up. */
6168 : 4651072 : if (!is_associative)
6169 : : {
6170 : 15 : add_to_ops_vec (ops, binrhs);
6171 : 15 : return;
6172 : : }
6173 : :
6174 : 4651057 : if (!binrhsisreassoc)
6175 : : {
6176 : 4576677 : bool swap = false;
6177 : 4576677 : if (try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef))
6178 : : /* If we add ops for the rhs we expect to be able to recurse
6179 : : to it via the lhs during expression rewrite so swap
6180 : : operands. */
6181 : : swap = true;
6182 : : else
6183 : 4576588 : add_to_ops_vec (ops, binrhs);
6184 : :
6185 : 4576677 : if (!try_special_add_to_ops (ops, rhscode, binlhs, binlhsdef))
6186 : 4576570 : add_to_ops_vec (ops, binlhs);
6187 : :
6188 : 4576677 : if (!swap)
6189 : : return;
6190 : : }
6191 : :
6192 : 74469 : if (dump_file && (dump_flags & TDF_DETAILS))
6193 : : {
6194 : 9 : fprintf (dump_file, "swapping operands of ");
6195 : 9 : print_gimple_stmt (dump_file, stmt, 0);
6196 : : }
6197 : :
6198 : 74469 : swap_ssa_operands (stmt,
6199 : : gimple_assign_rhs1_ptr (stmt),
6200 : : gimple_assign_rhs2_ptr (stmt));
6201 : 74469 : update_stmt (stmt);
6202 : :
6203 : 74469 : if (dump_file && (dump_flags & TDF_DETAILS))
6204 : : {
6205 : 9 : fprintf (dump_file, " is now ");
6206 : 9 : print_gimple_stmt (dump_file, stmt, 0);
6207 : : }
6208 : 74469 : if (!binrhsisreassoc)
6209 : : return;
6210 : :
6211 : : /* We want to make it so the lhs is always the reassociative op,
6212 : : so swap. */
6213 : : std::swap (binlhs, binrhs);
6214 : : }
6215 : 211747 : else if (binrhsisreassoc)
6216 : : {
6217 : 1723 : linearize_expr (stmt);
6218 : 1723 : binlhs = gimple_assign_rhs1 (stmt);
6219 : 1723 : binrhs = gimple_assign_rhs2 (stmt);
6220 : : }
6221 : :
6222 : 286127 : gcc_assert (TREE_CODE (binrhs) != SSA_NAME
6223 : : || !is_reassociable_op (SSA_NAME_DEF_STMT (binrhs),
6224 : : rhscode, loop));
6225 : 286127 : linearize_expr_tree (ops, SSA_NAME_DEF_STMT (binlhs),
6226 : : is_associative, set_visited);
6227 : :
6228 : 286127 : if (!try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef))
6229 : 286123 : add_to_ops_vec (ops, binrhs);
6230 : : }
6231 : :
6232 : : /* Repropagate the negates back into subtracts, since no other pass
6233 : : currently does it. */
6234 : :
6235 : : static void
6236 : 2042992 : repropagate_negates (void)
6237 : : {
6238 : 2042992 : unsigned int i = 0;
6239 : 2042992 : tree negate;
6240 : :
6241 : 2180054 : FOR_EACH_VEC_ELT (plus_negates, i, negate)
6242 : : {
6243 : 137062 : gimple *user = get_single_immediate_use (negate);
6244 : 137062 : if (!user || !is_gimple_assign (user))
6245 : 22887 : continue;
6246 : :
6247 : 114175 : tree negateop = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (negate));
6248 : 114185 : if (TREE_CODE (negateop) == SSA_NAME
6249 : 114175 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (negateop))
6250 : 10 : continue;
6251 : :
6252 : : /* The negate operand can be either operand of a PLUS_EXPR
6253 : : (it can be the LHS if the RHS is a constant for example).
6254 : :
6255 : : Force the negate operand to the RHS of the PLUS_EXPR, then
6256 : : transform the PLUS_EXPR into a MINUS_EXPR. */
6257 : 114165 : if (gimple_assign_rhs_code (user) == PLUS_EXPR)
6258 : : {
6259 : : /* If the negated operand appears on the LHS of the
6260 : : PLUS_EXPR, exchange the operands of the PLUS_EXPR
6261 : : to force the negated operand to the RHS of the PLUS_EXPR. */
6262 : 84383 : if (gimple_assign_rhs1 (user) == negate)
6263 : : {
6264 : 35394 : swap_ssa_operands (user,
6265 : : gimple_assign_rhs1_ptr (user),
6266 : : gimple_assign_rhs2_ptr (user));
6267 : : }
6268 : :
6269 : : /* Now transform the PLUS_EXPR into a MINUS_EXPR and replace
6270 : : the RHS of the PLUS_EXPR with the operand of the NEGATE_EXPR. */
6271 : 84383 : if (gimple_assign_rhs2 (user) == negate)
6272 : : {
6273 : 84383 : tree rhs1 = gimple_assign_rhs1 (user);
6274 : 84383 : gimple_stmt_iterator gsi = gsi_for_stmt (user);
6275 : 84383 : gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, rhs1,
6276 : : negateop);
6277 : 84383 : update_stmt (user);
6278 : : }
6279 : : }
6280 : 29782 : else if (gimple_assign_rhs_code (user) == MINUS_EXPR)
6281 : : {
6282 : 2240 : if (gimple_assign_rhs1 (user) == negate)
6283 : : {
6284 : : /* We have
6285 : : x = -negateop
6286 : : y = x - b
6287 : : which we transform into
6288 : : x = negateop + b
6289 : : y = -x .
6290 : : This pushes down the negate which we possibly can merge
6291 : : into some other operation, hence insert it into the
6292 : : plus_negates vector. */
6293 : 2240 : gimple *feed = SSA_NAME_DEF_STMT (negate);
6294 : 2240 : tree b = gimple_assign_rhs2 (user);
6295 : 2240 : gimple_stmt_iterator gsi = gsi_for_stmt (feed);
6296 : 2240 : gimple_stmt_iterator gsi2 = gsi_for_stmt (user);
6297 : 2240 : tree x = make_ssa_name (TREE_TYPE (gimple_assign_lhs (feed)));
6298 : 2240 : gimple *g = gimple_build_assign (x, PLUS_EXPR, negateop, b);
6299 : 2240 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
6300 : 2240 : gimple_assign_set_rhs_with_ops (&gsi2, NEGATE_EXPR, x);
6301 : 2240 : user = gsi_stmt (gsi2);
6302 : 2240 : update_stmt (user);
6303 : 2240 : reassoc_remove_stmt (&gsi);
6304 : 2240 : release_defs (feed);
6305 : 2240 : plus_negates.safe_push (gimple_assign_lhs (user));
6306 : : }
6307 : : else
6308 : : {
6309 : : /* Transform "x = -negateop; y = b - x" into "y = b + negateop",
6310 : : getting rid of one operation. */
6311 : 0 : tree rhs1 = gimple_assign_rhs1 (user);
6312 : 0 : gimple_stmt_iterator gsi = gsi_for_stmt (user);
6313 : 0 : gimple_assign_set_rhs_with_ops (&gsi, PLUS_EXPR, rhs1, negateop);
6314 : 0 : update_stmt (gsi_stmt (gsi));
6315 : : }
6316 : : }
6317 : : }
6318 : 2042992 : }
6319 : :
6320 : : /* Break up subtract operations in block BB.
6321 : :
6322 : : We do this top down because we don't know whether the subtract is
6323 : : part of a possible chain of reassociation except at the top.
6324 : :
6325 : : IE given
6326 : : d = f + g
6327 : : c = a + e
6328 : : b = c - d
6329 : : q = b - r
6330 : : k = t - q
6331 : :
6332 : : we want to break up k = t - q, but we won't until we've transformed q
6333 : : = b - r, which won't be broken up until we transform b = c - d.
6334 : :
6335 : : En passant, clear the GIMPLE visited flag on every statement
6336 : : and set UIDs within each basic block. */
6337 : :
6338 : : static void
6339 : 19914058 : break_up_subtract_bb (basic_block bb)
6340 : : {
6341 : 19914058 : gimple_stmt_iterator gsi;
6342 : 19914058 : unsigned int uid = 1;
6343 : :
6344 : 209129897 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6345 : : {
6346 : 169301781 : gimple *stmt = gsi_stmt (gsi);
6347 : 169301781 : gimple_set_visited (stmt, false);
6348 : 169301781 : gimple_set_uid (stmt, uid++);
6349 : :
6350 : 169301781 : if (!is_gimple_assign (stmt)
6351 : 47669559 : || !can_reassociate_type_p (TREE_TYPE (gimple_assign_lhs (stmt)))
6352 : 186770056 : || !can_reassociate_op_p (gimple_assign_lhs (stmt)))
6353 : 151833836 : continue;
6354 : :
6355 : : /* Look for simple gimple subtract operations. */
6356 : 17467945 : if (gimple_assign_rhs_code (stmt) == MINUS_EXPR)
6357 : : {
6358 : 302693 : if (!can_reassociate_op_p (gimple_assign_rhs1 (stmt))
6359 : 302693 : || !can_reassociate_op_p (gimple_assign_rhs2 (stmt)))
6360 : 274 : continue;
6361 : :
6362 : : /* Check for a subtract used only in an addition. If this
6363 : : is the case, transform it into add of a negate for better
6364 : : reassociation. IE transform C = A-B into C = A + -B if C
6365 : : is only used in an addition. */
6366 : 302419 : if (should_break_up_subtract (stmt))
6367 : 77367 : break_up_subtract (stmt, &gsi);
6368 : : }
6369 : 17165252 : else if (gimple_assign_rhs_code (stmt) == NEGATE_EXPR
6370 : 17165252 : && can_reassociate_op_p (gimple_assign_rhs1 (stmt)))
6371 : 47524 : plus_negates.safe_push (gimple_assign_lhs (stmt));
6372 : : }
6373 : 19914058 : }
6374 : :
6375 : : /* Used for repeated factor analysis. */
6376 : : struct repeat_factor
6377 : : {
6378 : : /* An SSA name that occurs in a multiply chain. */
6379 : : tree factor;
6380 : :
6381 : : /* Cached rank of the factor. */
6382 : : unsigned rank;
6383 : :
6384 : : /* Number of occurrences of the factor in the chain. */
6385 : : HOST_WIDE_INT count;
6386 : :
6387 : : /* An SSA name representing the product of this factor and
6388 : : all factors appearing later in the repeated factor vector. */
6389 : : tree repr;
6390 : : };
6391 : :
6392 : :
6393 : : static vec<repeat_factor> repeat_factor_vec;
6394 : :
6395 : : /* Used for sorting the repeat factor vector. Sort primarily by
6396 : : ascending occurrence count, secondarily by descending rank. */
6397 : :
6398 : : static int
6399 : 240212 : compare_repeat_factors (const void *x1, const void *x2)
6400 : : {
6401 : 240212 : const repeat_factor *rf1 = (const repeat_factor *) x1;
6402 : 240212 : const repeat_factor *rf2 = (const repeat_factor *) x2;
6403 : :
6404 : 240212 : if (rf1->count < rf2->count)
6405 : : return -1;
6406 : 239626 : else if (rf1->count > rf2->count)
6407 : : return 1;
6408 : :
6409 : 239181 : if (rf1->rank < rf2->rank)
6410 : : return 1;
6411 : 130028 : else if (rf1->rank > rf2->rank)
6412 : 108020 : return -1;
6413 : :
6414 : : return 0;
6415 : : }
6416 : :
6417 : : /* Look for repeated operands in OPS in the multiply tree rooted at
6418 : : STMT. Replace them with an optimal sequence of multiplies and powi
6419 : : builtin calls, and remove the used operands from OPS. Return an
6420 : : SSA name representing the value of the replacement sequence. */
6421 : :
6422 : : static tree
6423 : 488409 : attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops)
6424 : : {
6425 : 488409 : unsigned i, j, vec_len;
6426 : 488409 : int ii;
6427 : 488409 : operand_entry *oe;
6428 : 488409 : repeat_factor *rf1, *rf2;
6429 : 488409 : repeat_factor rfnew;
6430 : 488409 : tree result = NULL_TREE;
6431 : 488409 : tree target_ssa, iter_result;
6432 : 488409 : tree type = TREE_TYPE (gimple_get_lhs (stmt));
6433 : 488409 : tree powi_fndecl = mathfn_built_in (type, BUILT_IN_POWI);
6434 : 488409 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6435 : 488409 : gimple *mul_stmt, *pow_stmt;
6436 : :
6437 : : /* Nothing to do if BUILT_IN_POWI doesn't exist for this type and
6438 : : target, unless type is integral. */
6439 : 488409 : if (!powi_fndecl && !INTEGRAL_TYPE_P (type))
6440 : : return NULL_TREE;
6441 : :
6442 : : /* Allocate the repeated factor vector. */
6443 : 484753 : repeat_factor_vec.create (10);
6444 : :
6445 : : /* Scan the OPS vector for all SSA names in the product and build
6446 : : up a vector of occurrence counts for each factor. */
6447 : 1943115 : FOR_EACH_VEC_ELT (*ops, i, oe)
6448 : : {
6449 : 973609 : if (TREE_CODE (oe->op) == SSA_NAME)
6450 : : {
6451 : 610444 : FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
6452 : : {
6453 : 65153 : if (rf1->factor == oe->op)
6454 : : {
6455 : 2687 : rf1->count += oe->count;
6456 : 2687 : break;
6457 : : }
6458 : : }
6459 : :
6460 : 547978 : if (j >= repeat_factor_vec.length ())
6461 : : {
6462 : 545291 : rfnew.factor = oe->op;
6463 : 545291 : rfnew.rank = oe->rank;
6464 : 545291 : rfnew.count = oe->count;
6465 : 545291 : rfnew.repr = NULL_TREE;
6466 : 545291 : repeat_factor_vec.safe_push (rfnew);
6467 : : }
6468 : : }
6469 : : }
6470 : :
6471 : : /* Sort the repeated factor vector by (a) increasing occurrence count,
6472 : : and (b) decreasing rank. */
6473 : 484753 : repeat_factor_vec.qsort (compare_repeat_factors);
6474 : :
6475 : : /* It is generally best to combine as many base factors as possible
6476 : : into a product before applying __builtin_powi to the result.
6477 : : However, the sort order chosen for the repeated factor vector
6478 : : allows us to cache partial results for the product of the base
6479 : : factors for subsequent use. When we already have a cached partial
6480 : : result from a previous iteration, it is best to make use of it
6481 : : before looking for another __builtin_pow opportunity.
6482 : :
6483 : : As an example, consider x * x * y * y * y * z * z * z * z.
6484 : : We want to first compose the product x * y * z, raise it to the
6485 : : second power, then multiply this by y * z, and finally multiply
6486 : : by z. This can be done in 5 multiplies provided we cache y * z
6487 : : for use in both expressions:
6488 : :
6489 : : t1 = y * z
6490 : : t2 = t1 * x
6491 : : t3 = t2 * t2
6492 : : t4 = t1 * t3
6493 : : result = t4 * z
6494 : :
6495 : : If we instead ignored the cached y * z and first multiplied by
6496 : : the __builtin_pow opportunity z * z, we would get the inferior:
6497 : :
6498 : : t1 = y * z
6499 : : t2 = t1 * x
6500 : : t3 = t2 * t2
6501 : : t4 = z * z
6502 : : t5 = t3 * t4
6503 : : result = t5 * y */
6504 : :
6505 : 969506 : vec_len = repeat_factor_vec.length ();
6506 : :
6507 : : /* Repeatedly look for opportunities to create a builtin_powi call. */
6508 : 486412 : while (true)
6509 : : {
6510 : 486412 : HOST_WIDE_INT power;
6511 : :
6512 : : /* First look for the largest cached product of factors from
6513 : : preceding iterations. If found, create a builtin_powi for
6514 : : it if the minimum occurrence count for its factors is at
6515 : : least 2, or just use this cached product as our next
6516 : : multiplicand if the minimum occurrence count is 1. */
6517 : 1033639 : FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
6518 : : {
6519 : 547236 : if (rf1->repr && rf1->count > 0)
6520 : : break;
6521 : : }
6522 : :
6523 : 486412 : if (j < vec_len)
6524 : : {
6525 : 9 : power = rf1->count;
6526 : :
6527 : 9 : if (power == 1)
6528 : : {
6529 : 7 : iter_result = rf1->repr;
6530 : :
6531 : 7 : if (dump_file && (dump_flags & TDF_DETAILS))
6532 : : {
6533 : 0 : unsigned elt;
6534 : 0 : repeat_factor *rf;
6535 : 0 : fputs ("Multiplying by cached product ", dump_file);
6536 : 0 : for (elt = j; elt < vec_len; elt++)
6537 : : {
6538 : 0 : rf = &repeat_factor_vec[elt];
6539 : 0 : print_generic_expr (dump_file, rf->factor);
6540 : 0 : if (elt < vec_len - 1)
6541 : 0 : fputs (" * ", dump_file);
6542 : : }
6543 : 0 : fputs ("\n", dump_file);
6544 : : }
6545 : : }
6546 : : else
6547 : : {
6548 : 2 : if (INTEGRAL_TYPE_P (type))
6549 : : {
6550 : 0 : gcc_assert (power > 1);
6551 : 0 : gimple_stmt_iterator gsip = gsi;
6552 : 0 : gsi_prev (&gsip);
6553 : 0 : iter_result = powi_as_mults (&gsi, gimple_location (stmt),
6554 : : rf1->repr, power);
6555 : 0 : gimple_stmt_iterator gsic = gsi;
6556 : 0 : while (gsi_stmt (gsic) != gsi_stmt (gsip))
6557 : : {
6558 : 0 : gimple_set_uid (gsi_stmt (gsic), gimple_uid (stmt));
6559 : 0 : gimple_set_visited (gsi_stmt (gsic), true);
6560 : 0 : gsi_prev (&gsic);
6561 : : }
6562 : : }
6563 : : else
6564 : : {
6565 : 2 : iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
6566 : 2 : pow_stmt
6567 : 2 : = gimple_build_call (powi_fndecl, 2, rf1->repr,
6568 : : build_int_cst (integer_type_node,
6569 : 2 : power));
6570 : 2 : gimple_call_set_lhs (pow_stmt, iter_result);
6571 : 2 : gimple_set_location (pow_stmt, gimple_location (stmt));
6572 : 2 : gimple_set_uid (pow_stmt, gimple_uid (stmt));
6573 : 2 : gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
6574 : : }
6575 : :
6576 : 2 : if (dump_file && (dump_flags & TDF_DETAILS))
6577 : : {
6578 : 0 : unsigned elt;
6579 : 0 : repeat_factor *rf;
6580 : 0 : fputs ("Building __builtin_pow call for cached product (",
6581 : : dump_file);
6582 : 0 : for (elt = j; elt < vec_len; elt++)
6583 : : {
6584 : 0 : rf = &repeat_factor_vec[elt];
6585 : 0 : print_generic_expr (dump_file, rf->factor);
6586 : 0 : if (elt < vec_len - 1)
6587 : 0 : fputs (" * ", dump_file);
6588 : : }
6589 : 0 : fprintf (dump_file, ")^" HOST_WIDE_INT_PRINT_DEC"\n",
6590 : : power);
6591 : : }
6592 : : }
6593 : : }
6594 : : else
6595 : : {
6596 : : /* Otherwise, find the first factor in the repeated factor
6597 : : vector whose occurrence count is at least 2. If no such
6598 : : factor exists, there are no builtin_powi opportunities
6599 : : remaining. */
6600 : 1031936 : FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
6601 : : {
6602 : 547183 : if (rf1->count >= 2)
6603 : : break;
6604 : : }
6605 : :
6606 : 486403 : if (j >= vec_len)
6607 : : break;
6608 : :
6609 : 1650 : power = rf1->count;
6610 : :
6611 : 1650 : if (dump_file && (dump_flags & TDF_DETAILS))
6612 : : {
6613 : 0 : unsigned elt;
6614 : 0 : repeat_factor *rf;
6615 : 0 : fputs ("Building __builtin_pow call for (", dump_file);
6616 : 0 : for (elt = j; elt < vec_len; elt++)
6617 : : {
6618 : 0 : rf = &repeat_factor_vec[elt];
6619 : 0 : print_generic_expr (dump_file, rf->factor);
6620 : 0 : if (elt < vec_len - 1)
6621 : 0 : fputs (" * ", dump_file);
6622 : : }
6623 : 0 : fprintf (dump_file, ")^" HOST_WIDE_INT_PRINT_DEC"\n", power);
6624 : : }
6625 : :
6626 : 1650 : reassociate_stats.pows_created++;
6627 : :
6628 : : /* Visit each element of the vector in reverse order (so that
6629 : : high-occurrence elements are visited first, and within the
6630 : : same occurrence count, lower-ranked elements are visited
6631 : : first). Form a linear product of all elements in this order
6632 : : whose occurrencce count is at least that of element J.
6633 : : Record the SSA name representing the product of each element
6634 : : with all subsequent elements in the vector. */
6635 : 1650 : if (j == vec_len - 1)
6636 : 1629 : rf1->repr = rf1->factor;
6637 : : else
6638 : : {
6639 : 50 : for (ii = vec_len - 2; ii >= (int)j; ii--)
6640 : : {
6641 : 29 : tree op1, op2;
6642 : :
6643 : 29 : rf1 = &repeat_factor_vec[ii];
6644 : 29 : rf2 = &repeat_factor_vec[ii + 1];
6645 : :
6646 : : /* Init the last factor's representative to be itself. */
6647 : 29 : if (!rf2->repr)
6648 : 21 : rf2->repr = rf2->factor;
6649 : :
6650 : 29 : op1 = rf1->factor;
6651 : 29 : op2 = rf2->repr;
6652 : :
6653 : 29 : target_ssa = make_temp_ssa_name (type, NULL, "reassocpow");
6654 : 29 : mul_stmt = gimple_build_assign (target_ssa, MULT_EXPR,
6655 : : op1, op2);
6656 : 29 : gimple_set_location (mul_stmt, gimple_location (stmt));
6657 : 29 : gimple_set_uid (mul_stmt, gimple_uid (stmt));
6658 : 29 : gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
6659 : 29 : rf1->repr = target_ssa;
6660 : :
6661 : : /* Don't reprocess the multiply we just introduced. */
6662 : 29 : gimple_set_visited (mul_stmt, true);
6663 : : }
6664 : : }
6665 : :
6666 : : /* Form a call to __builtin_powi for the maximum product
6667 : : just formed, raised to the power obtained earlier. */
6668 : 1650 : rf1 = &repeat_factor_vec[j];
6669 : 1650 : if (INTEGRAL_TYPE_P (type))
6670 : : {
6671 : 1100 : gcc_assert (power > 1);
6672 : 1100 : gimple_stmt_iterator gsip = gsi;
6673 : 1100 : gsi_prev (&gsip);
6674 : 1100 : iter_result = powi_as_mults (&gsi, gimple_location (stmt),
6675 : : rf1->repr, power);
6676 : 1100 : gimple_stmt_iterator gsic = gsi;
6677 : 1100 : while (gsi_stmt (gsic) != gsi_stmt (gsip))
6678 : : {
6679 : 2235 : gimple_set_uid (gsi_stmt (gsic), gimple_uid (stmt));
6680 : 2235 : gimple_set_visited (gsi_stmt (gsic), true);
6681 : 3335 : gsi_prev (&gsic);
6682 : : }
6683 : : }
6684 : : else
6685 : : {
6686 : 550 : iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
6687 : 550 : pow_stmt = gimple_build_call (powi_fndecl, 2, rf1->repr,
6688 : : build_int_cst (integer_type_node,
6689 : 550 : power));
6690 : 550 : gimple_call_set_lhs (pow_stmt, iter_result);
6691 : 550 : gimple_set_location (pow_stmt, gimple_location (stmt));
6692 : 550 : gimple_set_uid (pow_stmt, gimple_uid (stmt));
6693 : 550 : gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
6694 : : }
6695 : : }
6696 : :
6697 : : /* If we previously formed at least one other builtin_powi call,
6698 : : form the product of this one and those others. */
6699 : 1659 : if (result)
6700 : : {
6701 : 9 : tree new_result = make_temp_ssa_name (type, NULL, "reassocpow");
6702 : 9 : mul_stmt = gimple_build_assign (new_result, MULT_EXPR,
6703 : : result, iter_result);
6704 : 9 : gimple_set_location (mul_stmt, gimple_location (stmt));
6705 : 9 : gimple_set_uid (mul_stmt, gimple_uid (stmt));
6706 : 9 : gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
6707 : 9 : gimple_set_visited (mul_stmt, true);
6708 : 9 : result = new_result;
6709 : : }
6710 : : else
6711 : : result = iter_result;
6712 : :
6713 : : /* Decrement the occurrence count of each element in the product
6714 : : by the count found above, and remove this many copies of each
6715 : : factor from OPS. */
6716 : 3352 : for (i = j; i < vec_len; i++)
6717 : : {
6718 : 1693 : unsigned k = power;
6719 : 1693 : unsigned n;
6720 : :
6721 : 1693 : rf1 = &repeat_factor_vec[i];
6722 : 1693 : rf1->count -= power;
6723 : :
6724 : 8129 : FOR_EACH_VEC_ELT_REVERSE (*ops, n, oe)
6725 : : {
6726 : 4743 : if (oe->op == rf1->factor)
6727 : : {
6728 : 4372 : if (oe->count <= k)
6729 : : {
6730 : 4366 : ops->ordered_remove (n);
6731 : 4366 : k -= oe->count;
6732 : :
6733 : 4366 : if (k == 0)
6734 : : break;
6735 : : }
6736 : : else
6737 : : {
6738 : 6 : oe->count -= k;
6739 : 6 : break;
6740 : : }
6741 : : }
6742 : : }
6743 : : }
6744 : : }
6745 : :
6746 : : /* At this point all elements in the repeated factor vector have a
6747 : : remaining occurrence count of 0 or 1, and those with a count of 1
6748 : : don't have cached representatives. Re-sort the ops vector and
6749 : : clean up. */
6750 : 484753 : ops->qsort (sort_by_operand_rank);
6751 : 484753 : repeat_factor_vec.release ();
6752 : :
6753 : : /* Return the final product computed herein. Note that there may
6754 : : still be some elements with single occurrence count left in OPS;
6755 : : those will be handled by the normal reassociation logic. */
6756 : 484753 : return result;
6757 : : }
6758 : :
6759 : : /* Attempt to optimize
6760 : : CST1 * copysign (CST2, y) -> copysign (CST1 * CST2, y) if CST1 > 0, or
6761 : : CST1 * copysign (CST2, y) -> -copysign (CST1 * CST2, y) if CST1 < 0. */
6762 : :
6763 : : static void
6764 : 1046229 : attempt_builtin_copysign (vec<operand_entry *> *ops)
6765 : : {
6766 : 1046229 : operand_entry *oe;
6767 : 1046229 : unsigned int i;
6768 : 1046229 : unsigned int length = ops->length ();
6769 : 1046229 : tree cst = ops->last ()->op;
6770 : :
6771 : 1046229 : if (length == 1 || TREE_CODE (cst) != REAL_CST)
6772 : : return;
6773 : :
6774 : 4119 : FOR_EACH_VEC_ELT (*ops, i, oe)
6775 : : {
6776 : 2941 : if (TREE_CODE (oe->op) == SSA_NAME
6777 : 2941 : && has_single_use (oe->op))
6778 : : {
6779 : 859 : gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
6780 : 2980 : if (gcall *old_call = dyn_cast <gcall *> (def_stmt))
6781 : : {
6782 : 55 : tree arg0, arg1;
6783 : 55 : switch (gimple_call_combined_fn (old_call))
6784 : : {
6785 : 20 : CASE_CFN_COPYSIGN:
6786 : 20 : CASE_CFN_COPYSIGN_FN:
6787 : 20 : arg0 = gimple_call_arg (old_call, 0);
6788 : 20 : arg1 = gimple_call_arg (old_call, 1);
6789 : : /* The first argument of copysign must be a constant,
6790 : : otherwise there's nothing to do. */
6791 : 20 : if (TREE_CODE (arg0) == REAL_CST)
6792 : : {
6793 : 20 : tree type = TREE_TYPE (arg0);
6794 : 20 : tree mul = const_binop (MULT_EXPR, type, cst, arg0);
6795 : : /* If we couldn't fold to a single constant, skip it.
6796 : : That happens e.g. for inexact multiplication when
6797 : : -frounding-math. */
6798 : 20 : if (mul == NULL_TREE)
6799 : : break;
6800 : : /* Instead of adjusting OLD_CALL, let's build a new
6801 : : call to not leak the LHS and prevent keeping bogus
6802 : : debug statements. DCE will clean up the old call. */
6803 : 16 : gcall *new_call;
6804 : 16 : if (gimple_call_internal_p (old_call))
6805 : 0 : new_call = gimple_build_call_internal
6806 : 0 : (IFN_COPYSIGN, 2, mul, arg1);
6807 : : else
6808 : 16 : new_call = gimple_build_call
6809 : 16 : (gimple_call_fndecl (old_call), 2, mul, arg1);
6810 : 16 : tree lhs = make_ssa_name (type);
6811 : 16 : gimple_call_set_lhs (new_call, lhs);
6812 : 16 : gimple_set_location (new_call,
6813 : : gimple_location (old_call));
6814 : 16 : insert_stmt_after (new_call, old_call);
6815 : : /* We've used the constant, get rid of it. */
6816 : 16 : ops->pop ();
6817 : 16 : bool cst1_neg = real_isneg (TREE_REAL_CST_PTR (cst));
6818 : : /* Handle the CST1 < 0 case by negating the result. */
6819 : 16 : if (cst1_neg)
6820 : : {
6821 : 7 : tree negrhs = make_ssa_name (TREE_TYPE (lhs));
6822 : 7 : gimple *negate_stmt
6823 : 7 : = gimple_build_assign (negrhs, NEGATE_EXPR, lhs);
6824 : 7 : insert_stmt_after (negate_stmt, new_call);
6825 : 7 : oe->op = negrhs;
6826 : : }
6827 : : else
6828 : 9 : oe->op = lhs;
6829 : 16 : if (dump_file && (dump_flags & TDF_DETAILS))
6830 : : {
6831 : 14 : fprintf (dump_file, "Optimizing copysign: ");
6832 : 14 : print_generic_expr (dump_file, cst);
6833 : 14 : fprintf (dump_file, " * COPYSIGN (");
6834 : 14 : print_generic_expr (dump_file, arg0);
6835 : 14 : fprintf (dump_file, ", ");
6836 : 14 : print_generic_expr (dump_file, arg1);
6837 : 23 : fprintf (dump_file, ") into %sCOPYSIGN (",
6838 : : cst1_neg ? "-" : "");
6839 : 14 : print_generic_expr (dump_file, mul);
6840 : 14 : fprintf (dump_file, ", ");
6841 : 14 : print_generic_expr (dump_file, arg1);
6842 : 14 : fprintf (dump_file, "\n");
6843 : : }
6844 : 16 : return;
6845 : : }
6846 : : break;
6847 : : default:
6848 : : break;
6849 : : }
6850 : : }
6851 : : }
6852 : : }
6853 : : }
6854 : :
6855 : : /* Transform STMT at *GSI into a copy by replacing its rhs with NEW_RHS. */
6856 : :
6857 : : static void
6858 : 14367 : transform_stmt_to_copy (gimple_stmt_iterator *gsi, gimple *stmt, tree new_rhs)
6859 : : {
6860 : 14367 : tree rhs1;
6861 : :
6862 : 14367 : if (dump_file && (dump_flags & TDF_DETAILS))
6863 : : {
6864 : 28 : fprintf (dump_file, "Transforming ");
6865 : 28 : print_gimple_stmt (dump_file, stmt, 0);
6866 : : }
6867 : :
6868 : 14367 : rhs1 = gimple_assign_rhs1 (stmt);
6869 : 14367 : gimple_assign_set_rhs_from_tree (gsi, new_rhs);
6870 : 14367 : update_stmt (stmt);
6871 : 14367 : remove_visited_stmt_chain (rhs1);
6872 : :
6873 : 14367 : if (dump_file && (dump_flags & TDF_DETAILS))
6874 : : {
6875 : 28 : fprintf (dump_file, " into ");
6876 : 28 : print_gimple_stmt (dump_file, stmt, 0);
6877 : : }
6878 : 14367 : }
6879 : :
6880 : : /* Transform STMT at *GSI into a multiply of RHS1 and RHS2. */
6881 : :
6882 : : static void
6883 : 174 : transform_stmt_to_multiply (gimple_stmt_iterator *gsi, gimple *stmt,
6884 : : tree rhs1, tree rhs2)
6885 : : {
6886 : 174 : if (dump_file && (dump_flags & TDF_DETAILS))
6887 : : {
6888 : 0 : fprintf (dump_file, "Transforming ");
6889 : 0 : print_gimple_stmt (dump_file, stmt, 0);
6890 : : }
6891 : :
6892 : 174 : gimple_assign_set_rhs_with_ops (gsi, MULT_EXPR, rhs1, rhs2);
6893 : 174 : update_stmt (gsi_stmt (*gsi));
6894 : 174 : remove_visited_stmt_chain (rhs1);
6895 : :
6896 : 174 : if (dump_file && (dump_flags & TDF_DETAILS))
6897 : : {
6898 : 0 : fprintf (dump_file, " into ");
6899 : 0 : print_gimple_stmt (dump_file, stmt, 0);
6900 : : }
6901 : 174 : }
6902 : :
6903 : : /* Rearrange ops may have more FMA when the chain may has more than 2 FMAs.
6904 : : Put no-mult ops and mult ops alternately at the end of the queue, which is
6905 : : conducive to generating more FMA and reducing the loss of FMA when breaking
6906 : : the chain.
6907 : : E.g.
6908 : : a * b + c * d + e generates:
6909 : :
6910 : : _4 = c_9(D) * d_10(D);
6911 : : _12 = .FMA (a_7(D), b_8(D), _4);
6912 : : _11 = e_6(D) + _12;
6913 : :
6914 : : Rearrange ops to -> e + a * b + c * d generates:
6915 : :
6916 : : _4 = .FMA (c_7(D), d_8(D), _3);
6917 : : _11 = .FMA (a_5(D), b_6(D), _4);
6918 : :
6919 : : Return the number of MULT_EXPRs in the chain. */
6920 : : static int
6921 : 17571 : rank_ops_for_fma (vec<operand_entry *> *ops)
6922 : : {
6923 : 17571 : operand_entry *oe;
6924 : 17571 : unsigned int i;
6925 : 17571 : unsigned int ops_length = ops->length ();
6926 : 17571 : auto_vec<operand_entry *> ops_mult;
6927 : 17571 : auto_vec<operand_entry *> ops_others;
6928 : :
6929 : 60000 : FOR_EACH_VEC_ELT (*ops, i, oe)
6930 : : {
6931 : 42429 : if (TREE_CODE (oe->op) == SSA_NAME)
6932 : : {
6933 : 42397 : gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
6934 : 42397 : if (is_gimple_assign (def_stmt))
6935 : : {
6936 : 29436 : if (gimple_assign_rhs_code (def_stmt) == MULT_EXPR)
6937 : 15737 : ops_mult.safe_push (oe);
6938 : : /* A negate on the multiplication leads to FNMA. */
6939 : 13699 : else if (gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
6940 : 13699 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
6941 : : {
6942 : 3604 : gimple *neg_def_stmt
6943 : 3604 : = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
6944 : 3604 : if (is_gimple_assign (neg_def_stmt)
6945 : 3601 : && gimple_bb (neg_def_stmt) == gimple_bb (def_stmt)
6946 : 7196 : && gimple_assign_rhs_code (neg_def_stmt) == MULT_EXPR)
6947 : 3534 : ops_mult.safe_push (oe);
6948 : : else
6949 : 70 : ops_others.safe_push (oe);
6950 : : }
6951 : : else
6952 : 10095 : ops_others.safe_push (oe);
6953 : : }
6954 : : else
6955 : 12961 : ops_others.safe_push (oe);
6956 : : }
6957 : : else
6958 : 32 : ops_others.safe_push (oe);
6959 : : }
6960 : : /* 1. When ops_mult.length == 2, like the following case,
6961 : :
6962 : : a * b + c * d + e.
6963 : :
6964 : : we need to rearrange the ops.
6965 : :
6966 : : Putting ops that not def from mult in front can generate more FMAs.
6967 : :
6968 : : 2. If all ops are defined with mult, we don't need to rearrange them. */
6969 : 17571 : unsigned mult_num = ops_mult.length ();
6970 : 17571 : if (mult_num >= 2 && mult_num != ops_length)
6971 : : {
6972 : : /* Put no-mult ops and mult ops alternately at the end of the
6973 : : queue, which is conducive to generating more FMA and reducing the
6974 : : loss of FMA when breaking the chain. */
6975 : 7004 : ops->truncate (0);
6976 : 7004 : ops->splice (ops_mult);
6977 : 7004 : int j, opindex = ops->length ();
6978 : 7004 : int others_length = ops_others.length ();
6979 : 14017 : for (j = 0; j < others_length; j++)
6980 : : {
6981 : 7013 : oe = ops_others.pop ();
6982 : 7013 : ops->quick_insert (opindex, oe);
6983 : 7013 : if (opindex > 0)
6984 : 7011 : opindex--;
6985 : : }
6986 : : }
6987 : 17571 : return mult_num;
6988 : 17571 : }
6989 : : /* Reassociate expressions in basic block BB and its post-dominator as
6990 : : children.
6991 : :
6992 : : Bubble up return status from maybe_optimize_range_tests. */
6993 : :
6994 : : static bool
6995 : 19914016 : reassociate_bb (basic_block bb)
6996 : : {
6997 : 19914016 : gimple_stmt_iterator gsi;
6998 : 19914016 : gimple *stmt = last_nondebug_stmt (bb);
6999 : 19914016 : bool cfg_cleanup_needed = false;
7000 : :
7001 : 19914016 : if (stmt && !gimple_visited_p (stmt))
7002 : 19081485 : cfg_cleanup_needed |= maybe_optimize_range_tests (stmt);
7003 : :
7004 : 19914016 : bool do_prev = false;
7005 : 39828032 : for (gsi = gsi_last_bb (bb);
7006 : 189476952 : !gsi_end_p (gsi); do_prev ? gsi_prev (&gsi) : (void) 0)
7007 : : {
7008 : 169562936 : do_prev = true;
7009 : 169562936 : stmt = gsi_stmt (gsi);
7010 : :
7011 : 169562936 : if (is_gimple_assign (stmt)
7012 : 169562936 : && !stmt_could_throw_p (cfun, stmt))
7013 : : {
7014 : 45836116 : tree lhs, rhs1, rhs2;
7015 : 45836116 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
7016 : :
7017 : : /* If this was part of an already processed statement,
7018 : : we don't need to touch it again. */
7019 : 45836116 : if (gimple_visited_p (stmt))
7020 : : {
7021 : : /* This statement might have become dead because of previous
7022 : : reassociations. */
7023 : 432444 : if (has_zero_uses (gimple_get_lhs (stmt)))
7024 : : {
7025 : 137294 : reassoc_remove_stmt (&gsi);
7026 : 137294 : release_defs (stmt);
7027 : : /* We might end up removing the last stmt above which
7028 : : places the iterator to the end of the sequence.
7029 : : Reset it to the last stmt in this case and make sure
7030 : : we don't do gsi_prev in that case. */
7031 : 137294 : if (gsi_end_p (gsi))
7032 : : {
7033 : 396 : gsi = gsi_last_bb (bb);
7034 : 396 : do_prev = false;
7035 : : }
7036 : : }
7037 : 432444 : continue;
7038 : : }
7039 : :
7040 : : /* If this is not a gimple binary expression, there is
7041 : : nothing for us to do with it. */
7042 : 45403672 : if (get_gimple_rhs_class (rhs_code) != GIMPLE_BINARY_RHS)
7043 : 33768854 : continue;
7044 : :
7045 : 11634818 : lhs = gimple_assign_lhs (stmt);
7046 : 11634818 : rhs1 = gimple_assign_rhs1 (stmt);
7047 : 11634818 : rhs2 = gimple_assign_rhs2 (stmt);
7048 : :
7049 : : /* For non-bit or min/max operations we can't associate
7050 : : all types. Verify that here. */
7051 : 17016937 : if ((rhs_code != BIT_IOR_EXPR
7052 : 11634818 : && rhs_code != BIT_AND_EXPR
7053 : 10589279 : && rhs_code != BIT_XOR_EXPR
7054 : 10589279 : && rhs_code != MIN_EXPR
7055 : 10478973 : && rhs_code != MAX_EXPR
7056 : 10385639 : && !can_reassociate_type_p (TREE_TYPE (lhs)))
7057 : 6257125 : || !can_reassociate_op_p (rhs1)
7058 : 17889032 : || !can_reassociate_op_p (rhs2))
7059 : 5382119 : continue;
7060 : :
7061 : 6252699 : if (associative_tree_code (rhs_code))
7062 : : {
7063 : 4583301 : auto_vec<operand_entry *> ops;
7064 : 4583301 : tree powi_result = NULL_TREE;
7065 : 4583301 : bool is_vector = VECTOR_TYPE_P (TREE_TYPE (lhs));
7066 : :
7067 : : /* There may be no immediate uses left by the time we
7068 : : get here because we may have eliminated them all. */
7069 : 4583301 : if (TREE_CODE (lhs) == SSA_NAME && has_zero_uses (lhs))
7070 : 46530 : continue;
7071 : :
7072 : 4536771 : gimple_set_visited (stmt, true);
7073 : 4536771 : linearize_expr_tree (&ops, stmt, true, true);
7074 : 4536771 : ops.qsort (sort_by_operand_rank);
7075 : 4536771 : int orig_len = ops.length ();
7076 : 4536771 : optimize_ops_list (rhs_code, &ops);
7077 : 9073542 : if (undistribute_ops_list (rhs_code, &ops,
7078 : : loop_containing_stmt (stmt)))
7079 : : {
7080 : 170 : ops.qsort (sort_by_operand_rank);
7081 : 170 : optimize_ops_list (rhs_code, &ops);
7082 : : }
7083 : 9073542 : if (undistribute_bitref_for_vector (rhs_code, &ops,
7084 : : loop_containing_stmt (stmt)))
7085 : : {
7086 : 40 : ops.qsort (sort_by_operand_rank);
7087 : 40 : optimize_ops_list (rhs_code, &ops);
7088 : : }
7089 : 4536771 : if (rhs_code == PLUS_EXPR
7090 : 4536771 : && transform_add_to_multiply (&ops))
7091 : 87 : ops.qsort (sort_by_operand_rank);
7092 : :
7093 : 4536771 : if (rhs_code == BIT_IOR_EXPR || rhs_code == BIT_AND_EXPR)
7094 : : {
7095 : 1029716 : if (is_vector)
7096 : 21247 : optimize_vec_cond_expr (rhs_code, &ops);
7097 : : else
7098 : 1008469 : optimize_range_tests (rhs_code, &ops, NULL);
7099 : : }
7100 : :
7101 : 4536771 : if (rhs_code == MULT_EXPR && !is_vector)
7102 : : {
7103 : 1046229 : attempt_builtin_copysign (&ops);
7104 : :
7105 : 1046229 : if (reassoc_insert_powi_p
7106 : 1046229 : && (flag_unsafe_math_optimizations
7107 : 445217 : || (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))))
7108 : 488409 : powi_result = attempt_builtin_powi (stmt, &ops);
7109 : : }
7110 : :
7111 : 4536771 : operand_entry *last;
7112 : 4536771 : bool negate_result = false;
7113 : 4536771 : if (ops.length () > 1
7114 : 4536771 : && rhs_code == MULT_EXPR)
7115 : : {
7116 : 1065912 : last = ops.last ();
7117 : 1065912 : if ((integer_minus_onep (last->op)
7118 : 1065778 : || real_minus_onep (last->op))
7119 : 162 : && !HONOR_SNANS (TREE_TYPE (lhs))
7120 : 1066074 : && (!HONOR_SIGNED_ZEROS (TREE_TYPE (lhs))
7121 : 0 : || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (lhs))))
7122 : : {
7123 : 162 : ops.pop ();
7124 : 162 : negate_result = true;
7125 : : }
7126 : : }
7127 : :
7128 : 4536771 : tree new_lhs = lhs;
7129 : : /* If the operand vector is now empty, all operands were
7130 : : consumed by the __builtin_powi optimization. */
7131 : 4536771 : if (ops.length () == 0)
7132 : 1371 : transform_stmt_to_copy (&gsi, stmt, powi_result);
7133 : 4535400 : else if (ops.length () == 1)
7134 : : {
7135 : 13170 : tree last_op = ops.last ()->op;
7136 : :
7137 : : /* If the stmt that defines operand has to be inserted, insert it
7138 : : before the use. */
7139 : 13170 : if (ops.last ()->stmt_to_insert)
7140 : 0 : insert_stmt_before_use (stmt, ops.last ()->stmt_to_insert);
7141 : 13170 : if (powi_result)
7142 : 174 : transform_stmt_to_multiply (&gsi, stmt, last_op,
7143 : : powi_result);
7144 : : else
7145 : 12996 : transform_stmt_to_copy (&gsi, stmt, last_op);
7146 : : }
7147 : : else
7148 : : {
7149 : 4522230 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
7150 : 4522230 : int ops_num = ops.length ();
7151 : 4522230 : int width = 0;
7152 : 4522230 : int mult_num = 0;
7153 : :
7154 : : /* For binary bit operations, if there are at least 3
7155 : : operands and the last operand in OPS is a constant,
7156 : : move it to the front. This helps ensure that we generate
7157 : : (X & Y) & C rather than (X & C) & Y. The former will
7158 : : often match a canonical bit test when we get to RTL. */
7159 : 4522230 : if (ops.length () > 2
7160 : 186381 : && (rhs_code == BIT_AND_EXPR
7161 : : || rhs_code == BIT_IOR_EXPR
7162 : 165348 : || rhs_code == BIT_XOR_EXPR)
7163 : 4546685 : && TREE_CODE (ops.last ()->op) == INTEGER_CST)
7164 : 1524 : std::swap (*ops[0], *ops[ops_num - 1]);
7165 : :
7166 : 4522230 : optimization_type opt_type = bb_optimization_type (bb);
7167 : :
7168 : : /* If the target support FMA, rank_ops_for_fma will detect if
7169 : : the chain has fmas and rearrange the ops if so. */
7170 : 4522230 : if (direct_internal_fn_supported_p (IFN_FMA,
7171 : 4522230 : TREE_TYPE (lhs),
7172 : : opt_type)
7173 : 4522230 : && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
7174 : : {
7175 : 17571 : mult_num = rank_ops_for_fma (&ops);
7176 : : }
7177 : :
7178 : : /* Only rewrite the expression tree to parallel in the
7179 : : last reassoc pass to avoid useless work back-and-forth
7180 : : with initial linearization. */
7181 : 4522230 : bool has_fma = mult_num >= 2 && mult_num != ops_num;
7182 : 4522230 : if (!reassoc_insert_powi_p
7183 : 2747066 : && ops.length () > 3
7184 : 4538877 : && (width = get_reassociation_width (&ops, mult_num, lhs,
7185 : : rhs_code, mode))
7186 : : > 1)
7187 : : {
7188 : 1574 : if (dump_file && (dump_flags & TDF_DETAILS))
7189 : 2 : fprintf (dump_file,
7190 : : "Width = %d was chosen for reassociation\n",
7191 : : width);
7192 : 1574 : rewrite_expr_tree_parallel (as_a <gassign *> (stmt),
7193 : : width,
7194 : : has_fma,
7195 : : ops);
7196 : : }
7197 : : else
7198 : : {
7199 : : /* When there are three operands left, we want
7200 : : to make sure the ones that get the double
7201 : : binary op are chosen wisely. */
7202 : 4520656 : int len = ops.length ();
7203 : 4520656 : if (len >= 3
7204 : 4520656 : && (!has_fma
7205 : : /* width > 1 means ranking ops results in better
7206 : : parallelism. Check current value to avoid
7207 : : calling get_reassociation_width again. */
7208 : 7000 : || (width != 1
7209 : 7000 : && get_reassociation_width (
7210 : : &ops, mult_num, lhs, rhs_code, mode)
7211 : : > 1)))
7212 : 181777 : swap_ops_for_binary_stmt (ops, len - 3);
7213 : :
7214 : 4520656 : new_lhs = rewrite_expr_tree (stmt, rhs_code, 0, ops,
7215 : 4520656 : powi_result != NULL
7216 : 4520656 : || negate_result,
7217 : : len != orig_len);
7218 : : }
7219 : :
7220 : : /* If we combined some repeated factors into a
7221 : : __builtin_powi call, multiply that result by the
7222 : : reassociated operands. */
7223 : 4522230 : if (powi_result)
7224 : : {
7225 : 105 : gimple *mul_stmt, *lhs_stmt = SSA_NAME_DEF_STMT (lhs);
7226 : 105 : tree type = TREE_TYPE (lhs);
7227 : 105 : tree target_ssa = make_temp_ssa_name (type, NULL,
7228 : : "reassocpow");
7229 : 105 : gimple_set_lhs (lhs_stmt, target_ssa);
7230 : 105 : update_stmt (lhs_stmt);
7231 : 105 : if (lhs != new_lhs)
7232 : : {
7233 : 105 : target_ssa = new_lhs;
7234 : 105 : new_lhs = lhs;
7235 : : }
7236 : 105 : mul_stmt = gimple_build_assign (lhs, MULT_EXPR,
7237 : : powi_result, target_ssa);
7238 : 105 : gimple_set_location (mul_stmt, gimple_location (stmt));
7239 : 105 : gimple_set_uid (mul_stmt, gimple_uid (stmt));
7240 : 105 : gsi_insert_after (&gsi, mul_stmt, GSI_NEW_STMT);
7241 : : }
7242 : : }
7243 : :
7244 : 4536771 : if (negate_result)
7245 : : {
7246 : 162 : stmt = SSA_NAME_DEF_STMT (lhs);
7247 : 162 : tree tmp = make_ssa_name (TREE_TYPE (lhs));
7248 : 162 : gimple_set_lhs (stmt, tmp);
7249 : 162 : if (lhs != new_lhs)
7250 : 152 : tmp = new_lhs;
7251 : 162 : gassign *neg_stmt = gimple_build_assign (lhs, NEGATE_EXPR,
7252 : : tmp);
7253 : 162 : gimple_set_uid (neg_stmt, gimple_uid (stmt));
7254 : 162 : gsi_insert_after (&gsi, neg_stmt, GSI_NEW_STMT);
7255 : 162 : update_stmt (stmt);
7256 : : }
7257 : 4583301 : }
7258 : : }
7259 : : }
7260 : :
7261 : 19914016 : return cfg_cleanup_needed;
7262 : : }
7263 : :
7264 : : /* Add jumps around shifts for range tests turned into bit tests.
7265 : : For each SSA_NAME VAR we have code like:
7266 : : VAR = ...; // final stmt of range comparison
7267 : : // bit test here...;
7268 : : OTHERVAR = ...; // final stmt of the bit test sequence
7269 : : RES = VAR | OTHERVAR;
7270 : : Turn the above into:
7271 : : VAR = ...;
7272 : : if (VAR != 0)
7273 : : goto <l3>;
7274 : : else
7275 : : goto <l2>;
7276 : : <l2>:
7277 : : // bit test here...;
7278 : : OTHERVAR = ...;
7279 : : <l3>:
7280 : : # RES = PHI<1(l1), OTHERVAR(l2)>; */
7281 : :
7282 : : static void
7283 : 2042992 : branch_fixup (void)
7284 : : {
7285 : 2042992 : tree var;
7286 : 2042992 : unsigned int i;
7287 : :
7288 : 2043373 : FOR_EACH_VEC_ELT (reassoc_branch_fixups, i, var)
7289 : : {
7290 : 381 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
7291 : 381 : gimple *use_stmt;
7292 : 381 : use_operand_p use;
7293 : 381 : bool ok = single_imm_use (var, &use, &use_stmt);
7294 : 381 : gcc_assert (ok
7295 : : && is_gimple_assign (use_stmt)
7296 : : && gimple_assign_rhs_code (use_stmt) == BIT_IOR_EXPR
7297 : : && gimple_bb (def_stmt) == gimple_bb (use_stmt));
7298 : :
7299 : 381 : basic_block cond_bb = gimple_bb (def_stmt);
7300 : 381 : basic_block then_bb = split_block (cond_bb, def_stmt)->dest;
7301 : 381 : basic_block merge_bb = split_block (then_bb, use_stmt)->dest;
7302 : :
7303 : 381 : gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
7304 : 381 : gimple *g = gimple_build_cond (NE_EXPR, var,
7305 : 381 : build_zero_cst (TREE_TYPE (var)),
7306 : : NULL_TREE, NULL_TREE);
7307 : 381 : location_t loc = gimple_location (use_stmt);
7308 : 381 : gimple_set_location (g, loc);
7309 : 381 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
7310 : :
7311 : 381 : edge etrue = make_edge (cond_bb, merge_bb, EDGE_TRUE_VALUE);
7312 : 381 : etrue->probability = profile_probability::even ();
7313 : 381 : edge efalse = find_edge (cond_bb, then_bb);
7314 : 381 : efalse->flags = EDGE_FALSE_VALUE;
7315 : 381 : efalse->probability -= etrue->probability;
7316 : 381 : then_bb->count -= etrue->count ();
7317 : :
7318 : 381 : tree othervar = NULL_TREE;
7319 : 381 : if (gimple_assign_rhs1 (use_stmt) == var)
7320 : 0 : othervar = gimple_assign_rhs2 (use_stmt);
7321 : 381 : else if (gimple_assign_rhs2 (use_stmt) == var)
7322 : : othervar = gimple_assign_rhs1 (use_stmt);
7323 : : else
7324 : 0 : gcc_unreachable ();
7325 : 381 : tree lhs = gimple_assign_lhs (use_stmt);
7326 : 381 : gphi *phi = create_phi_node (lhs, merge_bb);
7327 : 381 : add_phi_arg (phi, build_one_cst (TREE_TYPE (lhs)), etrue, loc);
7328 : 381 : add_phi_arg (phi, othervar, single_succ_edge (then_bb), loc);
7329 : 381 : gsi = gsi_for_stmt (use_stmt);
7330 : 381 : gsi_remove (&gsi, true);
7331 : :
7332 : 381 : set_immediate_dominator (CDI_DOMINATORS, merge_bb, cond_bb);
7333 : 381 : set_immediate_dominator (CDI_POST_DOMINATORS, cond_bb, merge_bb);
7334 : : }
7335 : 2042992 : reassoc_branch_fixups.release ();
7336 : 2042992 : }
7337 : :
7338 : : void dump_ops_vector (FILE *file, vec<operand_entry *> ops);
7339 : : void debug_ops_vector (vec<operand_entry *> ops);
7340 : :
7341 : : /* Dump the operand entry vector OPS to FILE. */
7342 : :
7343 : : void
7344 : 0 : dump_ops_vector (FILE *file, vec<operand_entry *> ops)
7345 : : {
7346 : 0 : operand_entry *oe;
7347 : 0 : unsigned int i;
7348 : :
7349 : 0 : FOR_EACH_VEC_ELT (ops, i, oe)
7350 : : {
7351 : 0 : fprintf (file, "Op %d -> rank: %d, tree: ", i, oe->rank);
7352 : 0 : print_generic_expr (file, oe->op);
7353 : 0 : fprintf (file, "\n");
7354 : : }
7355 : 0 : }
7356 : :
7357 : : /* Dump the operand entry vector OPS to STDERR. */
7358 : :
7359 : : DEBUG_FUNCTION void
7360 : 0 : debug_ops_vector (vec<operand_entry *> ops)
7361 : : {
7362 : 0 : dump_ops_vector (stderr, ops);
7363 : 0 : }
7364 : :
7365 : : /* Bubble up return status from reassociate_bb. */
7366 : :
7367 : : static bool
7368 : 2042992 : do_reassoc ()
7369 : : {
7370 : 2042992 : bool cfg_cleanup_needed = false;
7371 : 2042992 : basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
7372 : :
7373 : 2042992 : unsigned sp = 0;
7374 : 2042992 : for (auto son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
7375 : 4085984 : son; son = next_dom_son (CDI_DOMINATORS, son))
7376 : 2042992 : worklist[sp++] = son;
7377 : 21957050 : while (sp)
7378 : : {
7379 : 19914058 : basic_block bb = worklist[--sp];
7380 : 19914058 : break_up_subtract_bb (bb);
7381 : 19914058 : for (auto son = first_dom_son (CDI_DOMINATORS, bb);
7382 : 37785124 : son; son = next_dom_son (CDI_DOMINATORS, son))
7383 : 17871066 : worklist[sp++] = son;
7384 : : }
7385 : :
7386 : 10999703 : for (auto son = first_dom_son (CDI_POST_DOMINATORS,
7387 : 2042992 : EXIT_BLOCK_PTR_FOR_FN (cfun));
7388 : 10999703 : son; son = next_dom_son (CDI_POST_DOMINATORS, son))
7389 : 8956711 : worklist[sp++] = son;
7390 : 21957008 : while (sp)
7391 : : {
7392 : 19914016 : basic_block bb = worklist[--sp];
7393 : 19914016 : cfg_cleanup_needed |= reassociate_bb (bb);
7394 : 19914016 : for (auto son = first_dom_son (CDI_POST_DOMINATORS, bb);
7395 : 30871321 : son; son = next_dom_son (CDI_POST_DOMINATORS, son))
7396 : 10957305 : worklist[sp++] = son;
7397 : : }
7398 : :
7399 : 2042992 : free (worklist);
7400 : 2042992 : return cfg_cleanup_needed;
7401 : : }
7402 : :
7403 : : /* Initialize the reassociation pass. */
7404 : :
7405 : : static void
7406 : 2042992 : init_reassoc (void)
7407 : : {
7408 : 2042992 : int i;
7409 : 2042992 : int64_t rank = 2;
7410 : 2042992 : int *bbs = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
7411 : :
7412 : : /* Find the loops, so that we can prevent moving calculations in
7413 : : them. */
7414 : 2042992 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
7415 : :
7416 : 2042992 : memset (&reassociate_stats, 0, sizeof (reassociate_stats));
7417 : :
7418 : 2042992 : next_operand_entry_id = 0;
7419 : :
7420 : : /* Reverse RPO (Reverse Post Order) will give us something where
7421 : : deeper loops come later. */
7422 : 2042992 : pre_and_rev_post_order_compute (NULL, bbs, false);
7423 : 2042992 : bb_rank = XCNEWVEC (int64_t, last_basic_block_for_fn (cfun));
7424 : 2042992 : operand_rank = new hash_map<tree, int64_t>;
7425 : :
7426 : : /* Give each default definition a distinct rank. This includes
7427 : : parameters and the static chain. Walk backwards over all
7428 : : SSA names so that we get proper rank ordering according
7429 : : to tree_swap_operands_p. */
7430 : 108457885 : for (i = num_ssa_names - 1; i > 0; --i)
7431 : : {
7432 : 104371901 : tree name = ssa_name (i);
7433 : 179731129 : if (name && SSA_NAME_IS_DEFAULT_DEF (name))
7434 : 6166733 : insert_operand_rank (name, ++rank);
7435 : : }
7436 : :
7437 : : /* Set up rank for each BB */
7438 : 21957008 : for (i = 0; i < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; i++)
7439 : 19914016 : bb_rank[bbs[i]] = ++rank << 16;
7440 : :
7441 : 2042992 : free (bbs);
7442 : 2042992 : calculate_dominance_info (CDI_POST_DOMINATORS);
7443 : 2042992 : plus_negates = vNULL;
7444 : 2042992 : mark_ssa_maybe_undefs ();
7445 : 2042992 : }
7446 : :
7447 : : /* Cleanup after the reassociation pass, and print stats if
7448 : : requested. */
7449 : :
7450 : : static void
7451 : 2042992 : fini_reassoc (void)
7452 : : {
7453 : 2042992 : statistics_counter_event (cfun, "Linearized",
7454 : : reassociate_stats.linearized);
7455 : 2042992 : statistics_counter_event (cfun, "Constants eliminated",
7456 : : reassociate_stats.constants_eliminated);
7457 : 2042992 : statistics_counter_event (cfun, "Ops eliminated",
7458 : : reassociate_stats.ops_eliminated);
7459 : 2042992 : statistics_counter_event (cfun, "Statements rewritten",
7460 : : reassociate_stats.rewritten);
7461 : 2042992 : statistics_counter_event (cfun, "Built-in pow[i] calls encountered",
7462 : : reassociate_stats.pows_encountered);
7463 : 2042992 : statistics_counter_event (cfun, "Built-in powi calls created",
7464 : : reassociate_stats.pows_created);
7465 : :
7466 : 4085984 : delete operand_rank;
7467 : 2042992 : bitmap_clear (biased_names);
7468 : 2042992 : operand_entry_pool.release ();
7469 : 2042992 : free (bb_rank);
7470 : 2042992 : plus_negates.release ();
7471 : 2042992 : free_dominance_info (CDI_POST_DOMINATORS);
7472 : 2042992 : loop_optimizer_finalize ();
7473 : 2042992 : }
7474 : :
7475 : : /* Gate and execute functions for Reassociation. If INSERT_POWI_P, enable
7476 : : insertion of __builtin_powi calls.
7477 : :
7478 : : Returns TODO_cfg_cleanup if a CFG cleanup pass is desired due to
7479 : : optimization of a gimple conditional. Otherwise returns zero. */
7480 : :
7481 : : static unsigned int
7482 : 2042992 : execute_reassoc (bool insert_powi_p, bool bias_loop_carried_phi_ranks_p)
7483 : : {
7484 : 2042992 : reassoc_insert_powi_p = insert_powi_p;
7485 : 2042992 : reassoc_bias_loop_carried_phi_ranks_p = bias_loop_carried_phi_ranks_p;
7486 : :
7487 : 2042992 : init_reassoc ();
7488 : :
7489 : 2042992 : bool cfg_cleanup_needed;
7490 : 2042992 : cfg_cleanup_needed = do_reassoc ();
7491 : 2042992 : repropagate_negates ();
7492 : 2042992 : branch_fixup ();
7493 : :
7494 : 2042992 : fini_reassoc ();
7495 : 2042992 : return cfg_cleanup_needed ? TODO_cleanup_cfg : 0;
7496 : : }
7497 : :
7498 : : namespace {
7499 : :
7500 : : const pass_data pass_data_reassoc =
7501 : : {
7502 : : GIMPLE_PASS, /* type */
7503 : : "reassoc", /* name */
7504 : : OPTGROUP_NONE, /* optinfo_flags */
7505 : : TV_TREE_REASSOC, /* tv_id */
7506 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
7507 : : 0, /* properties_provided */
7508 : : 0, /* properties_destroyed */
7509 : : 0, /* todo_flags_start */
7510 : : TODO_update_ssa_only_virtuals, /* todo_flags_finish */
7511 : : };
7512 : :
7513 : : class pass_reassoc : public gimple_opt_pass
7514 : : {
7515 : : public:
7516 : 570162 : pass_reassoc (gcc::context *ctxt)
7517 : 1140324 : : gimple_opt_pass (pass_data_reassoc, ctxt), insert_powi_p (false)
7518 : : {}
7519 : :
7520 : : /* opt_pass methods: */
7521 : 285081 : opt_pass * clone () final override { return new pass_reassoc (m_ctxt); }
7522 : 570162 : void set_pass_param (unsigned int n, bool param) final override
7523 : : {
7524 : 570162 : gcc_assert (n == 0);
7525 : 570162 : insert_powi_p = param;
7526 : 570162 : bias_loop_carried_phi_ranks_p = !param;
7527 : 570162 : }
7528 : 2043164 : bool gate (function *) final override { return flag_tree_reassoc != 0; }
7529 : 2042992 : unsigned int execute (function *) final override
7530 : : {
7531 : 2042992 : return execute_reassoc (insert_powi_p, bias_loop_carried_phi_ranks_p);
7532 : : }
7533 : :
7534 : : private:
7535 : : /* Enable insertion of __builtin_powi calls during execute_reassoc. See
7536 : : point 3a in the pass header comment. */
7537 : : bool insert_powi_p;
7538 : : bool bias_loop_carried_phi_ranks_p;
7539 : : }; // class pass_reassoc
7540 : :
7541 : : } // anon namespace
7542 : :
7543 : : gimple_opt_pass *
7544 : 285081 : make_pass_reassoc (gcc::context *ctxt)
7545 : : {
7546 : 285081 : return new pass_reassoc (ctxt);
7547 : : }
|