Branch data Line data Source code
1 : : /* If-conversion for vectorizer.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 : : Contributed by Devang Patel <dpatel@apple.com>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : 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 : : /* This pass implements a tree level if-conversion of loops. Its
22 : : initial goal is to help the vectorizer to vectorize loops with
23 : : conditions.
24 : :
25 : : A short description of if-conversion:
26 : :
27 : : o Decide if a loop is if-convertible or not.
28 : : o Walk all loop basic blocks in breadth first order (BFS order).
29 : : o Remove conditional statements (at the end of basic block)
30 : : and propagate condition into destination basic blocks'
31 : : predicate list.
32 : : o Replace modify expression with conditional modify expression
33 : : using current basic block's condition.
34 : : o Merge all basic blocks
35 : : o Replace phi nodes with conditional modify expr
36 : : o Merge all basic blocks into header
37 : :
38 : : Sample transformation:
39 : :
40 : : INPUT
41 : : -----
42 : :
43 : : # i_23 = PHI <0(0), i_18(10)>;
44 : : <L0>:;
45 : : j_15 = A[i_23];
46 : : if (j_15 > 41) goto <L1>; else goto <L17>;
47 : :
48 : : <L17>:;
49 : : goto <bb 3> (<L3>);
50 : :
51 : : <L1>:;
52 : :
53 : : # iftmp.2_4 = PHI <0(8), 42(2)>;
54 : : <L3>:;
55 : : A[i_23] = iftmp.2_4;
56 : : i_18 = i_23 + 1;
57 : : if (i_18 <= 15) goto <L19>; else goto <L18>;
58 : :
59 : : <L19>:;
60 : : goto <bb 1> (<L0>);
61 : :
62 : : <L18>:;
63 : :
64 : : OUTPUT
65 : : ------
66 : :
67 : : # i_23 = PHI <0(0), i_18(10)>;
68 : : <L0>:;
69 : : j_15 = A[i_23];
70 : :
71 : : <L3>:;
72 : : iftmp.2_4 = j_15 > 41 ? 42 : 0;
73 : : A[i_23] = iftmp.2_4;
74 : : i_18 = i_23 + 1;
75 : : if (i_18 <= 15) goto <L19>; else goto <L18>;
76 : :
77 : : <L19>:;
78 : : goto <bb 1> (<L0>);
79 : :
80 : : <L18>:;
81 : : */
82 : :
83 : : #include "config.h"
84 : : #include "system.h"
85 : : #include "coretypes.h"
86 : : #include "backend.h"
87 : : #include "rtl.h"
88 : : #include "tree.h"
89 : : #include "gimple.h"
90 : : #include "cfghooks.h"
91 : : #include "tree-pass.h"
92 : : #include "ssa.h"
93 : : #include "expmed.h"
94 : : #include "expr.h"
95 : : #include "optabs-tree.h"
96 : : #include "gimple-pretty-print.h"
97 : : #include "alias.h"
98 : : #include "fold-const.h"
99 : : #include "stor-layout.h"
100 : : #include "gimple-iterator.h"
101 : : #include "gimple-fold.h"
102 : : #include "gimplify.h"
103 : : #include "gimplify-me.h"
104 : : #include "tree-cfg.h"
105 : : #include "tree-into-ssa.h"
106 : : #include "tree-ssa.h"
107 : : #include "cfgloop.h"
108 : : #include "tree-data-ref.h"
109 : : #include "tree-scalar-evolution.h"
110 : : #include "tree-ssa-loop.h"
111 : : #include "tree-ssa-loop-niter.h"
112 : : #include "tree-ssa-loop-ivopts.h"
113 : : #include "tree-ssa-address.h"
114 : : #include "dbgcnt.h"
115 : : #include "tree-hash-traits.h"
116 : : #include "varasm.h"
117 : : #include "builtins.h"
118 : : #include "cfganal.h"
119 : : #include "internal-fn.h"
120 : : #include "fold-const.h"
121 : : #include "tree-ssa-sccvn.h"
122 : : #include "tree-cfgcleanup.h"
123 : : #include "tree-ssa-dse.h"
124 : : #include "tree-vectorizer.h"
125 : : #include "tree-eh.h"
126 : : #include "cgraph.h"
127 : :
128 : : /* For lang_hooks.types.type_for_mode. */
129 : : #include "langhooks.h"
130 : :
131 : : /* Only handle PHIs with no more arguments unless we are asked to by
132 : : simd pragma. */
133 : : #define MAX_PHI_ARG_NUM \
134 : : ((unsigned) param_max_tree_if_conversion_phi_args)
135 : :
136 : : /* True if we've converted a statement that was only executed when some
137 : : condition C was true, and if for correctness we need to predicate the
138 : : statement to ensure that it is a no-op when C is false. See
139 : : predicate_statements for the kinds of predication we support. */
140 : : static bool need_to_predicate;
141 : :
142 : : /* True if we have to rewrite stmts that may invoke undefined behavior
143 : : when a condition C was false so it doesn't if it is always executed.
144 : : See predicate_statements for the kinds of predication we support. */
145 : : static bool need_to_rewrite_undefined;
146 : :
147 : : /* Indicate if there are any complicated PHIs that need to be handled in
148 : : if-conversion. Complicated PHI has more than two arguments and can't
149 : : be degenerated to two arguments PHI. See more information in comment
150 : : before phi_convertible_by_degenerating_args. */
151 : : static bool any_complicated_phi;
152 : :
153 : : /* True if we have bitfield accesses we can lower. */
154 : : static bool need_to_lower_bitfields;
155 : :
156 : : /* True if there is any ifcvting to be done. */
157 : : static bool need_to_ifcvt;
158 : :
159 : : /* Hash for struct innermost_loop_behavior. It depends on the user to
160 : : free the memory. */
161 : :
162 : : struct innermost_loop_behavior_hash : nofree_ptr_hash <innermost_loop_behavior>
163 : : {
164 : : static inline hashval_t hash (const value_type &);
165 : : static inline bool equal (const value_type &,
166 : : const compare_type &);
167 : : };
168 : :
169 : : inline hashval_t
170 : 417492 : innermost_loop_behavior_hash::hash (const value_type &e)
171 : : {
172 : 417492 : hashval_t hash;
173 : :
174 : 417492 : hash = iterative_hash_expr (e->base_address, 0);
175 : 417492 : hash = iterative_hash_expr (e->offset, hash);
176 : 417492 : hash = iterative_hash_expr (e->init, hash);
177 : 417492 : return iterative_hash_expr (e->step, hash);
178 : : }
179 : :
180 : : inline bool
181 : 293783 : innermost_loop_behavior_hash::equal (const value_type &e1,
182 : : const compare_type &e2)
183 : : {
184 : 293783 : if ((e1->base_address && !e2->base_address)
185 : 293783 : || (!e1->base_address && e2->base_address)
186 : 293783 : || (!e1->offset && e2->offset)
187 : 269479 : || (e1->offset && !e2->offset)
188 : 237402 : || (!e1->init && e2->init)
189 : 237402 : || (e1->init && !e2->init)
190 : 237402 : || (!e1->step && e2->step)
191 : 237402 : || (e1->step && !e2->step))
192 : : return false;
193 : :
194 : 237402 : if (e1->base_address && e2->base_address
195 : 474804 : && !operand_equal_p (e1->base_address, e2->base_address, 0))
196 : : return false;
197 : 41920 : if (e1->offset && e2->offset
198 : 120538 : && !operand_equal_p (e1->offset, e2->offset, 0))
199 : : return false;
200 : 41681 : if (e1->init && e2->init
201 : 120060 : && !operand_equal_p (e1->init, e2->init, 0))
202 : : return false;
203 : 17149 : if (e1->step && e2->step
204 : 70996 : && !operand_equal_p (e1->step, e2->step, 0))
205 : : return false;
206 : :
207 : : return true;
208 : : }
209 : :
210 : : /* List of basic blocks in if-conversion-suitable order. */
211 : : static basic_block *ifc_bbs;
212 : :
213 : : /* Hash table to store <DR's innermost loop behavior, DR> pairs. */
214 : : static hash_map<innermost_loop_behavior_hash,
215 : : data_reference_p> *innermost_DR_map;
216 : :
217 : : /* Hash table to store <base reference, DR> pairs. */
218 : : static hash_map<tree_operand_hash, data_reference_p> *baseref_DR_map;
219 : :
220 : : /* List of redundant SSA names: the first should be replaced by the second. */
221 : : static vec< std::pair<tree, tree> > redundant_ssa_names;
222 : :
223 : : /* Structure used to predicate basic blocks. This is attached to the
224 : : ->aux field of the BBs in the loop to be if-converted. */
225 : : struct bb_predicate {
226 : :
227 : : /* The condition under which this basic block is executed. */
228 : : tree predicate;
229 : :
230 : : /* PREDICATE is gimplified, and the sequence of statements is
231 : : recorded here, in order to avoid the duplication of computations
232 : : that occur in previous conditions. See PR44483. */
233 : : gimple_seq predicate_gimplified_stmts;
234 : :
235 : : /* Records the number of statements recorded into
236 : : PREDICATE_GIMPLIFIED_STMTS. */
237 : : unsigned no_predicate_stmts;
238 : : };
239 : :
240 : : /* Returns true when the basic block BB has a predicate. */
241 : :
242 : : static inline bool
243 : 1950001 : bb_has_predicate (basic_block bb)
244 : : {
245 : 1950001 : return bb->aux != NULL;
246 : : }
247 : :
248 : : /* Returns the gimplified predicate for basic block BB. */
249 : :
250 : : static inline tree
251 : 903194 : bb_predicate (basic_block bb)
252 : : {
253 : 903194 : return ((struct bb_predicate *) bb->aux)->predicate;
254 : : }
255 : :
256 : : /* Sets the gimplified predicate COND for basic block BB. */
257 : :
258 : : static inline void
259 : 492408 : set_bb_predicate (basic_block bb, tree cond)
260 : : {
261 : 492408 : auto aux = (struct bb_predicate *) bb->aux;
262 : 492408 : gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
263 : : && is_gimple_val (TREE_OPERAND (cond, 0)))
264 : : || is_gimple_val (cond));
265 : 492408 : aux->predicate = cond;
266 : 492408 : aux->no_predicate_stmts++;
267 : :
268 : 492408 : if (dump_file && (dump_flags & TDF_DETAILS))
269 : 263 : fprintf (dump_file, "Recording block %d value %d\n", bb->index,
270 : : aux->no_predicate_stmts);
271 : 492408 : }
272 : :
273 : : /* Returns the sequence of statements of the gimplification of the
274 : : predicate for basic block BB. */
275 : :
276 : : static inline gimple_seq
277 : 610195 : bb_predicate_gimplified_stmts (basic_block bb)
278 : : {
279 : 610195 : return ((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts;
280 : : }
281 : :
282 : : /* Sets the sequence of statements STMTS of the gimplification of the
283 : : predicate for basic block BB. If PRESERVE_COUNTS then don't clear the predicate
284 : : counts. */
285 : :
286 : : static inline void
287 : 289634 : set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts,
288 : : bool preserve_counts)
289 : : {
290 : 289634 : ((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts = stmts;
291 : 289634 : if (stmts == NULL && !preserve_counts)
292 : 236964 : ((struct bb_predicate *) bb->aux)->no_predicate_stmts = 0;
293 : 85596 : }
294 : :
295 : : /* Adds the sequence of statements STMTS to the sequence of statements
296 : : of the predicate for basic block BB. */
297 : :
298 : : static inline void
299 : 87951 : add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
300 : : {
301 : : /* We might have updated some stmts in STMTS via force_gimple_operand
302 : : calling fold_stmt and that producing multiple stmts. Delink immediate
303 : : uses so update_ssa after loop versioning doesn't get confused for
304 : : the not yet inserted predicates.
305 : : ??? This should go away once we reliably avoid updating stmts
306 : : not in any BB. */
307 : 87951 : for (gimple_stmt_iterator gsi = gsi_start (stmts);
308 : 214939 : !gsi_end_p (gsi); gsi_next (&gsi))
309 : : {
310 : 126988 : gimple *stmt = gsi_stmt (gsi);
311 : 126988 : delink_stmt_imm_use (stmt);
312 : 126988 : gimple_set_modified (stmt, true);
313 : 126988 : ((struct bb_predicate *) bb->aux)->no_predicate_stmts++;
314 : : }
315 : 87951 : gimple_seq_add_seq_without_update
316 : 87951 : (&(((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts), stmts);
317 : 87951 : }
318 : :
319 : : /* Return the number of statements the predicate of the basic block consists
320 : : of. */
321 : :
322 : : static inline unsigned
323 : 16261 : get_bb_num_predicate_stmts (basic_block bb)
324 : : {
325 : 16261 : return ((struct bb_predicate *) bb->aux)->no_predicate_stmts;
326 : : }
327 : :
328 : : /* Initializes to TRUE the predicate of basic block BB. */
329 : :
330 : : static inline void
331 : 204038 : init_bb_predicate (basic_block bb)
332 : : {
333 : 204038 : bb->aux = XNEW (struct bb_predicate);
334 : 204038 : set_bb_predicate_gimplified_stmts (bb, NULL, false);
335 : 204038 : set_bb_predicate (bb, boolean_true_node);
336 : 204038 : }
337 : :
338 : : /* Release the SSA_NAMEs associated with the predicate of basic block BB. */
339 : :
340 : : static inline void
341 : 399372 : release_bb_predicate (basic_block bb)
342 : : {
343 : 399372 : gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
344 : 399372 : if (stmts)
345 : : {
346 : : /* Ensure that these stmts haven't yet been added to a bb. */
347 : 32926 : if (flag_checking)
348 : : for (gimple_stmt_iterator i = gsi_start (stmts);
349 : 91768 : !gsi_end_p (i); gsi_next (&i))
350 : 58842 : gcc_assert (! gimple_bb (gsi_stmt (i)));
351 : :
352 : : /* Discard them. */
353 : 32926 : gimple_seq_discard (stmts);
354 : 32926 : set_bb_predicate_gimplified_stmts (bb, NULL, false);
355 : : }
356 : 399372 : }
357 : :
358 : : /* Free the predicate of basic block BB. */
359 : :
360 : : static inline void
361 : 1754667 : free_bb_predicate (basic_block bb)
362 : : {
363 : 1754667 : if (!bb_has_predicate (bb))
364 : : return;
365 : :
366 : 204038 : release_bb_predicate (bb);
367 : 204038 : free (bb->aux);
368 : 204038 : bb->aux = NULL;
369 : : }
370 : :
371 : : /* Reinitialize predicate of BB with the true predicate. */
372 : :
373 : : static inline void
374 : 195334 : reset_bb_predicate (basic_block bb)
375 : : {
376 : 195334 : if (!bb_has_predicate (bb))
377 : 0 : init_bb_predicate (bb);
378 : : else
379 : : {
380 : 195334 : release_bb_predicate (bb);
381 : 195334 : set_bb_predicate (bb, boolean_true_node);
382 : : }
383 : 195334 : }
384 : :
385 : : /* Returns a new SSA_NAME of type TYPE that is assigned the value of
386 : : the expression EXPR. Inserts the statement created for this
387 : : computation before GSI and leaves the iterator GSI at the same
388 : : statement. */
389 : :
390 : : static tree
391 : 5765 : ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
392 : : {
393 : 5765 : tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
394 : 5765 : gimple *stmt = gimple_build_assign (new_name, expr);
395 : 11530 : gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (*gsi)));
396 : 5765 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
397 : 5765 : return new_name;
398 : : }
399 : :
400 : : /* Return true when COND is a false predicate. */
401 : :
402 : : static inline bool
403 : 63122 : is_false_predicate (tree cond)
404 : : {
405 : 63122 : return (cond != NULL_TREE
406 : 63122 : && (cond == boolean_false_node
407 : 63122 : || integer_zerop (cond)));
408 : : }
409 : :
410 : : /* Return true when COND is a true predicate. */
411 : :
412 : : static inline bool
413 : 1045051 : is_true_predicate (tree cond)
414 : : {
415 : 1045051 : return (cond == NULL_TREE
416 : 1045051 : || cond == boolean_true_node
417 : 1497363 : || integer_onep (cond));
418 : : }
419 : :
420 : : /* Returns true when BB has a predicate that is not trivial: true or
421 : : NULL_TREE. */
422 : :
423 : : static inline bool
424 : 506962 : is_predicated (basic_block bb)
425 : : {
426 : 10037 : return !is_true_predicate (bb_predicate (bb));
427 : : }
428 : :
429 : : /* Parses the predicate COND and returns its comparison code and
430 : : operands OP0 and OP1. */
431 : :
432 : : static enum tree_code
433 : 447468 : parse_predicate (tree cond, tree *op0, tree *op1)
434 : : {
435 : 447468 : gimple *s;
436 : :
437 : 447468 : if (TREE_CODE (cond) == SSA_NAME
438 : 447468 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
439 : : {
440 : 61851 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
441 : : {
442 : 29924 : *op0 = gimple_assign_rhs1 (s);
443 : 29924 : *op1 = gimple_assign_rhs2 (s);
444 : 29924 : return gimple_assign_rhs_code (s);
445 : : }
446 : :
447 : 31927 : else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
448 : : {
449 : 0 : tree op = gimple_assign_rhs1 (s);
450 : 0 : tree type = TREE_TYPE (op);
451 : 0 : enum tree_code code = parse_predicate (op, op0, op1);
452 : :
453 : 0 : return code == ERROR_MARK ? ERROR_MARK
454 : 0 : : invert_tree_comparison (code, HONOR_NANS (type));
455 : : }
456 : :
457 : : return ERROR_MARK;
458 : : }
459 : :
460 : 385617 : if (COMPARISON_CLASS_P (cond))
461 : : {
462 : 453 : *op0 = TREE_OPERAND (cond, 0);
463 : 453 : *op1 = TREE_OPERAND (cond, 1);
464 : 453 : return TREE_CODE (cond);
465 : : }
466 : :
467 : : return ERROR_MARK;
468 : : }
469 : :
470 : : /* Returns the fold of predicate C1 OR C2 at location LOC. */
471 : :
472 : : static tree
473 : 223734 : fold_or_predicates (location_t loc, tree c1, tree c2)
474 : : {
475 : 223734 : tree op1a, op1b, op2a, op2b;
476 : 223734 : enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
477 : 223734 : enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
478 : :
479 : 223734 : if (code1 != ERROR_MARK && code2 != ERROR_MARK)
480 : : {
481 : 2010 : tree t = maybe_fold_or_comparisons (boolean_type_node, code1, op1a, op1b,
482 : : code2, op2a, op2b);
483 : 2010 : if (t)
484 : : return t;
485 : : }
486 : :
487 : 221792 : return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
488 : : }
489 : :
490 : : /* Returns either a COND_EXPR or the folded expression if the folded
491 : : expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
492 : : a constant or a SSA_NAME. */
493 : :
494 : : static tree
495 : 47972 : fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
496 : : {
497 : : /* Short cut the case where both rhs and lhs are the same. */
498 : 47972 : if (operand_equal_p (rhs, lhs))
499 : : return rhs;
500 : :
501 : : /* If COND is comparison r != 0 and r has boolean type, convert COND
502 : : to SSA_NAME to accept by vect bool pattern. */
503 : 47972 : if (TREE_CODE (cond) == NE_EXPR)
504 : : {
505 : 0 : tree op0 = TREE_OPERAND (cond, 0);
506 : 0 : tree op1 = TREE_OPERAND (cond, 1);
507 : 0 : if (TREE_CODE (op0) == SSA_NAME
508 : 0 : && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
509 : 0 : && (integer_zerop (op1)))
510 : : cond = op0;
511 : : }
512 : :
513 : 47972 : gimple_match_op cexpr (gimple_match_cond::UNCOND, COND_EXPR,
514 : 47972 : type, cond, rhs, lhs);
515 : 47972 : if (cexpr.resimplify (NULL, follow_all_ssa_edges))
516 : : {
517 : 10708 : if (gimple_simplified_result_is_gimple_val (&cexpr))
518 : 563 : return cexpr.ops[0];
519 : 10145 : else if (cexpr.code == ABS_EXPR)
520 : 2 : return build1 (ABS_EXPR, type, cexpr.ops[0]);
521 : 10143 : else if (cexpr.code == MIN_EXPR
522 : 10143 : || cexpr.code == MAX_EXPR)
523 : 7175 : return build2 ((tree_code)cexpr.code, type, cexpr.ops[0], cexpr.ops[1]);
524 : : }
525 : :
526 : 40232 : return build3 (COND_EXPR, type, cond, rhs, lhs);
527 : : }
528 : :
529 : : /* Add condition NC to the predicate list of basic block BB. LOOP is
530 : : the loop to be if-converted. Use predicate of cd-equivalent block
531 : : for join bb if it exists: we call basic blocks bb1 and bb2
532 : : cd-equivalent if they are executed under the same condition. */
533 : :
534 : : static inline void
535 : 161295 : add_to_predicate_list (class loop *loop, basic_block bb, tree nc)
536 : : {
537 : 161295 : tree bc, *tp;
538 : 161295 : basic_block dom_bb;
539 : :
540 : 161295 : if (is_true_predicate (nc))
541 : 71749 : return;
542 : :
543 : : /* If dominance tells us this basic block is always executed,
544 : : don't record any predicates for it. */
545 : 161293 : if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
546 : : return;
547 : :
548 : 93036 : dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
549 : : /* We use notion of cd equivalence to get simpler predicate for
550 : : join block, e.g. if join block has 2 predecessors with predicates
551 : : p1 & p2 and p1 & !p2, we'd like to get p1 for it instead of
552 : : p1 & p2 | p1 & !p2. */
553 : 93036 : if (dom_bb != loop->header
554 : 93036 : && get_immediate_dominator (CDI_POST_DOMINATORS, dom_bb) == bb)
555 : : {
556 : 3490 : gcc_assert (flow_bb_inside_loop_p (loop, dom_bb));
557 : 3490 : bc = bb_predicate (dom_bb);
558 : 3490 : if (!is_true_predicate (bc))
559 : 3490 : set_bb_predicate (bb, bc);
560 : : else
561 : 0 : gcc_assert (is_true_predicate (bb_predicate (bb)));
562 : 3490 : if (dump_file && (dump_flags & TDF_DETAILS))
563 : 4 : fprintf (dump_file, "Use predicate of bb#%d for bb#%d\n",
564 : : dom_bb->index, bb->index);
565 : 3490 : return;
566 : : }
567 : :
568 : 89546 : if (!is_predicated (bb))
569 : 85596 : bc = nc;
570 : : else
571 : : {
572 : 3950 : bc = bb_predicate (bb);
573 : 3950 : bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
574 : 3950 : if (is_true_predicate (bc))
575 : : {
576 : 0 : reset_bb_predicate (bb);
577 : 0 : return;
578 : : }
579 : : }
580 : :
581 : : /* Allow a TRUTH_NOT_EXPR around the main predicate. */
582 : 89546 : if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
583 : 32193 : tp = &TREE_OPERAND (bc, 0);
584 : : else
585 : : tp = &bc;
586 : 89546 : if (!is_gimple_val (*tp))
587 : : {
588 : 87951 : gimple_seq stmts;
589 : 87951 : *tp = force_gimple_operand (*tp, &stmts, true, NULL_TREE);
590 : 87951 : add_bb_predicate_gimplified_stmts (bb, stmts);
591 : : }
592 : 89546 : set_bb_predicate (bb, bc);
593 : : }
594 : :
595 : : /* Add the condition COND to the previous condition PREV_COND, and add
596 : : this to the predicate list of the destination of edge E. LOOP is
597 : : the loop to be if-converted. */
598 : :
599 : : static void
600 : 105534 : add_to_dst_predicate_list (class loop *loop, edge e,
601 : : tree prev_cond, tree cond)
602 : : {
603 : 105534 : if (!flow_bb_inside_loop_p (loop, e->dest))
604 : : return;
605 : :
606 : 105534 : if (!is_true_predicate (prev_cond))
607 : 22092 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
608 : : prev_cond, cond);
609 : :
610 : 105534 : if (!dominated_by_p (CDI_DOMINATORS, loop->latch, e->dest))
611 : 85054 : add_to_predicate_list (loop, e->dest, cond);
612 : : }
613 : :
614 : : /* Return true if one of the successor edges of BB exits LOOP. */
615 : :
616 : : static bool
617 : 3404909 : bb_with_exit_edge_p (const class loop *loop, basic_block bb)
618 : : {
619 : 3404909 : edge e;
620 : 3404909 : edge_iterator ei;
621 : :
622 : 6792550 : FOR_EACH_EDGE (e, ei, bb->succs)
623 : 4827349 : if (loop_exit_edge_p (loop, e))
624 : : return true;
625 : :
626 : : return false;
627 : : }
628 : :
629 : : /* Given PHI which has more than two arguments, this function checks if
630 : : it's if-convertible by degenerating its arguments. Specifically, if
631 : : below two conditions are satisfied:
632 : :
633 : : 1) Number of PHI arguments with different values equals to 2 and one
634 : : argument has the only occurrence.
635 : : 2) The edge corresponding to the unique argument isn't critical edge.
636 : :
637 : : Such PHI can be handled as PHIs have only two arguments. For example,
638 : : below PHI:
639 : :
640 : : res = PHI <A_1(e1), A_1(e2), A_2(e3)>;
641 : :
642 : : can be transformed into:
643 : :
644 : : res = (predicate of e3) ? A_2 : A_1;
645 : :
646 : : Return TRUE if it is the case, FALSE otherwise. */
647 : :
648 : : static bool
649 : 5518 : phi_convertible_by_degenerating_args (gphi *phi)
650 : : {
651 : 5518 : edge e;
652 : 5518 : tree arg, t1 = NULL, t2 = NULL;
653 : 5518 : unsigned int i, i1 = 0, i2 = 0, n1 = 0, n2 = 0;
654 : 5518 : unsigned int num_args = gimple_phi_num_args (phi);
655 : :
656 : 5518 : gcc_assert (num_args > 2);
657 : :
658 : 20044 : for (i = 0; i < num_args; i++)
659 : : {
660 : 16729 : arg = gimple_phi_arg_def (phi, i);
661 : 16729 : if (t1 == NULL || operand_equal_p (t1, arg, 0))
662 : : {
663 : 7524 : n1++;
664 : 7524 : i1 = i;
665 : 7524 : t1 = arg;
666 : : }
667 : 9205 : else if (t2 == NULL || operand_equal_p (t2, arg, 0))
668 : : {
669 : 7002 : n2++;
670 : 7002 : i2 = i;
671 : 7002 : t2 = arg;
672 : : }
673 : : else
674 : : return false;
675 : : }
676 : :
677 : 3315 : if (n1 != 1 && n2 != 1)
678 : : return false;
679 : :
680 : : /* Check if the edge corresponding to the unique arg is critical. */
681 : 3252 : e = gimple_phi_arg_edge (phi, (n1 == 1) ? i1 : i2);
682 : 3252 : if (EDGE_COUNT (e->src->succs) > 1)
683 : : return false;
684 : :
685 : : return true;
686 : : }
687 : :
688 : : /* Return true when PHI is if-convertible. PHI is part of loop LOOP
689 : : and it belongs to basic block BB. Note at this point, it is sure
690 : : that PHI is if-convertible. This function updates global variable
691 : : ANY_COMPLICATED_PHI if PHI is complicated. */
692 : :
693 : : static bool
694 : 124634 : if_convertible_phi_p (class loop *loop, basic_block bb, gphi *phi)
695 : : {
696 : 124634 : if (dump_file && (dump_flags & TDF_DETAILS))
697 : : {
698 : 79 : fprintf (dump_file, "-------------------------\n");
699 : 79 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
700 : : }
701 : :
702 : 124634 : if (bb != loop->header
703 : 47936 : && gimple_phi_num_args (phi) > 2
704 : 130152 : && !phi_convertible_by_degenerating_args (phi))
705 : 2266 : any_complicated_phi = true;
706 : :
707 : 124634 : return true;
708 : : }
709 : :
710 : : /* Records the status of a data reference. This struct is attached to
711 : : each DR->aux field. */
712 : :
713 : : struct ifc_dr {
714 : : bool rw_unconditionally;
715 : : bool w_unconditionally;
716 : : bool written_at_least_once;
717 : :
718 : : tree rw_predicate;
719 : : tree w_predicate;
720 : : tree base_w_predicate;
721 : : };
722 : :
723 : : #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
724 : : #define DR_BASE_W_UNCONDITIONALLY(DR) (IFC_DR (DR)->written_at_least_once)
725 : : #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
726 : : #define DR_W_UNCONDITIONALLY(DR) (IFC_DR (DR)->w_unconditionally)
727 : :
728 : : /* Iterates over DR's and stores refs, DR and base refs, DR pairs in
729 : : HASH tables. While storing them in HASH table, it checks if the
730 : : reference is unconditionally read or written and stores that as a flag
731 : : information. For base reference it checks if it is written atlest once
732 : : unconditionally and stores it as flag information along with DR.
733 : : In other words for every data reference A in STMT there exist other
734 : : accesses to a data reference with the same base with predicates that
735 : : add up (OR-up) to the true predicate: this ensures that the data
736 : : reference A is touched (read or written) on every iteration of the
737 : : if-converted loop. */
738 : : static void
739 : 131879 : hash_memrefs_baserefs_and_store_DRs_read_written_info (data_reference_p a)
740 : : {
741 : :
742 : 131879 : data_reference_p *master_dr, *base_master_dr;
743 : 131879 : tree base_ref = DR_BASE_OBJECT (a);
744 : 131879 : innermost_loop_behavior *innermost = &DR_INNERMOST (a);
745 : 131879 : tree ca = bb_predicate (gimple_bb (DR_STMT (a)));
746 : 131879 : bool exist1, exist2;
747 : :
748 : 131879 : master_dr = &innermost_DR_map->get_or_insert (innermost, &exist1);
749 : 131879 : if (!exist1)
750 : 97747 : *master_dr = a;
751 : :
752 : 131879 : if (DR_IS_WRITE (a))
753 : : {
754 : 43919 : IFC_DR (*master_dr)->w_predicate
755 : 87838 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
756 : 43919 : IFC_DR (*master_dr)->w_predicate);
757 : 43919 : if (is_true_predicate (IFC_DR (*master_dr)->w_predicate))
758 : 28190 : DR_W_UNCONDITIONALLY (*master_dr) = true;
759 : : }
760 : 131879 : IFC_DR (*master_dr)->rw_predicate
761 : 263758 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
762 : 131879 : IFC_DR (*master_dr)->rw_predicate);
763 : 131879 : if (is_true_predicate (IFC_DR (*master_dr)->rw_predicate))
764 : 97468 : DR_RW_UNCONDITIONALLY (*master_dr) = true;
765 : :
766 : 131879 : if (DR_IS_WRITE (a))
767 : : {
768 : 43919 : base_master_dr = &baseref_DR_map->get_or_insert (base_ref, &exist2);
769 : 43919 : if (!exist2)
770 : 33909 : *base_master_dr = a;
771 : 43919 : IFC_DR (*base_master_dr)->base_w_predicate
772 : 87838 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
773 : 43919 : IFC_DR (*base_master_dr)->base_w_predicate);
774 : 43919 : if (is_true_predicate (IFC_DR (*base_master_dr)->base_w_predicate))
775 : 28424 : DR_BASE_W_UNCONDITIONALLY (*base_master_dr) = true;
776 : : }
777 : 131879 : }
778 : :
779 : : /* Return TRUE if can prove the index IDX of an array reference REF is
780 : : within array bound. Return false otherwise. */
781 : :
782 : : static bool
783 : 231099 : idx_within_array_bound (tree ref, tree *idx, void *dta)
784 : : {
785 : 231099 : wi::overflow_type overflow;
786 : 231099 : widest_int niter, valid_niter, delta, wi_step;
787 : 231099 : tree ev, init, step;
788 : 231099 : tree low, high;
789 : 231099 : class loop *loop = (class loop*) dta;
790 : :
791 : : /* Only support within-bound access for array references. */
792 : 231099 : if (TREE_CODE (ref) != ARRAY_REF)
793 : : return false;
794 : :
795 : : /* For arrays that might have flexible sizes, it is not guaranteed that they
796 : : do not extend over their declared size. */
797 : 142011 : if (array_ref_flexible_size_p (ref))
798 : : return false;
799 : :
800 : 88602 : ev = analyze_scalar_evolution (loop, *idx);
801 : 88602 : ev = instantiate_parameters (loop, ev);
802 : 88602 : init = initial_condition (ev);
803 : 88602 : step = evolution_part_in_loop_num (ev, loop->num);
804 : :
805 : 88602 : if (!init || TREE_CODE (init) != INTEGER_CST
806 : 78662 : || (step && TREE_CODE (step) != INTEGER_CST))
807 : : return false;
808 : :
809 : 78656 : low = array_ref_low_bound (ref);
810 : 78656 : high = array_ref_up_bound (ref);
811 : :
812 : : /* The case of nonconstant bounds could be handled, but it would be
813 : : complicated. */
814 : 78656 : if (TREE_CODE (low) != INTEGER_CST
815 : 78656 : || !high || TREE_CODE (high) != INTEGER_CST)
816 : : return false;
817 : :
818 : : /* Check if the intial idx is within bound. */
819 : 78588 : if (wi::to_widest (init) < wi::to_widest (low)
820 : 157168 : || wi::to_widest (init) > wi::to_widest (high))
821 : 15 : return false;
822 : :
823 : : /* The idx is always within bound. */
824 : 78573 : if (!step || integer_zerop (step))
825 : 1971 : return true;
826 : :
827 : 76602 : if (!max_loop_iterations (loop, &niter))
828 : : return false;
829 : :
830 : 76602 : if (wi::to_widest (step) < 0)
831 : : {
832 : 293 : delta = wi::to_widest (init) - wi::to_widest (low);
833 : 293 : wi_step = -wi::to_widest (step);
834 : : }
835 : : else
836 : : {
837 : 76309 : delta = wi::to_widest (high) - wi::to_widest (init);
838 : 76309 : wi_step = wi::to_widest (step);
839 : : }
840 : :
841 : 76602 : valid_niter = wi::div_floor (delta, wi_step, SIGNED, &overflow);
842 : : /* The iteration space of idx is within array bound. */
843 : 153204 : if (!overflow && niter <= valid_niter)
844 : : return true;
845 : :
846 : : return false;
847 : 231099 : }
848 : :
849 : : /* Return TRUE if ref is a within bound array reference. */
850 : :
851 : : bool
852 : 225013 : ref_within_array_bound (gimple *stmt, tree ref)
853 : : {
854 : 225013 : class loop *loop = loop_containing_stmt (stmt);
855 : :
856 : 225013 : gcc_assert (loop != NULL);
857 : 225013 : return for_each_index (&ref, idx_within_array_bound, loop);
858 : : }
859 : :
860 : :
861 : : /* Given a memory reference expression T, return TRUE if base object
862 : : it refers to is writable. The base object of a memory reference
863 : : is the main object being referenced, which is returned by function
864 : : get_base_address. */
865 : :
866 : : static bool
867 : 2530 : base_object_writable (tree ref)
868 : : {
869 : 2530 : tree base_tree = get_base_address (ref);
870 : :
871 : 2530 : return (base_tree
872 : 2530 : && DECL_P (base_tree)
873 : 1658 : && decl_binds_to_current_def_p (base_tree)
874 : 4184 : && !TREE_READONLY (base_tree));
875 : : }
876 : :
877 : : /* Return true when the memory references of STMT won't trap in the
878 : : if-converted code. There are two things that we have to check for:
879 : :
880 : : - writes to memory occur to writable memory: if-conversion of
881 : : memory writes transforms the conditional memory writes into
882 : : unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
883 : : into "A[i] = cond ? foo : A[i]", and as the write to memory may not
884 : : be executed at all in the original code, it may be a readonly
885 : : memory. To check that A is not const-qualified, we check that
886 : : there exists at least an unconditional write to A in the current
887 : : function.
888 : :
889 : : - reads or writes to memory are valid memory accesses for every
890 : : iteration. To check that the memory accesses are correctly formed
891 : : and that we are allowed to read and write in these locations, we
892 : : check that the memory accesses to be if-converted occur at every
893 : : iteration unconditionally.
894 : :
895 : : Returns true for the memory reference in STMT, same memory reference
896 : : is read or written unconditionally atleast once and the base memory
897 : : reference is written unconditionally once. This is to check reference
898 : : will not write fault. Also retuns true if the memory reference is
899 : : unconditionally read once then we are conditionally writing to memory
900 : : which is defined as read and write and is bound to the definition
901 : : we are seeing. */
902 : : static bool
903 : 19533 : ifcvt_memrefs_wont_trap (gimple *stmt, vec<data_reference_p> drs)
904 : : {
905 : : /* If DR didn't see a reference here we can't use it to tell
906 : : whether the ref traps or not. */
907 : 19533 : if (gimple_uid (stmt) == 0)
908 : : return false;
909 : :
910 : 19532 : data_reference_p *master_dr, *base_master_dr;
911 : 19532 : data_reference_p a = drs[gimple_uid (stmt) - 1];
912 : :
913 : 19532 : tree base = DR_BASE_OBJECT (a);
914 : 19532 : innermost_loop_behavior *innermost = &DR_INNERMOST (a);
915 : :
916 : 19532 : gcc_assert (DR_STMT (a) == stmt);
917 : 19532 : gcc_assert (DR_BASE_ADDRESS (a) || DR_OFFSET (a)
918 : : || DR_INIT (a) || DR_STEP (a));
919 : :
920 : 19532 : master_dr = innermost_DR_map->get (innermost);
921 : 19532 : gcc_assert (master_dr != NULL);
922 : :
923 : 19532 : base_master_dr = baseref_DR_map->get (base);
924 : :
925 : : /* If a is unconditionally written to it doesn't trap. */
926 : 19532 : if (DR_W_UNCONDITIONALLY (*master_dr))
927 : : return true;
928 : :
929 : : /* If a is unconditionally accessed then ...
930 : :
931 : : Even a is conditional access, we can treat it as an unconditional
932 : : one if it's an array reference and all its index are within array
933 : : bound. */
934 : 17681 : if (DR_RW_UNCONDITIONALLY (*master_dr)
935 : 17681 : || ref_within_array_bound (stmt, DR_REF (a)))
936 : : {
937 : : /* an unconditional read won't trap. */
938 : 6552 : if (DR_IS_READ (a))
939 : : return true;
940 : :
941 : : /* an unconditionaly write won't trap if the base is written
942 : : to unconditionally. */
943 : 2567 : if ((base_master_dr
944 : 2567 : && DR_BASE_W_UNCONDITIONALLY (*base_master_dr))
945 : : /* or the base is known to be not readonly. */
946 : 5097 : || base_object_writable (DR_REF (a)))
947 : 1691 : return !ref_can_have_store_data_races (base);
948 : : }
949 : :
950 : : return false;
951 : : }
952 : :
953 : : /* Return true if STMT could be converted into a masked load or store
954 : : (conditional load or store based on a mask computed from bb predicate). */
955 : :
956 : : static bool
957 : 11434 : ifcvt_can_use_mask_load_store (gimple *stmt)
958 : : {
959 : : /* Check whether this is a load or store. */
960 : 11434 : tree lhs = gimple_assign_lhs (stmt);
961 : 11434 : bool is_load;
962 : 11434 : tree ref;
963 : 11434 : if (gimple_store_p (stmt))
964 : : {
965 : 2651 : if (!is_gimple_val (gimple_assign_rhs1 (stmt)))
966 : : return false;
967 : : is_load = false;
968 : : ref = lhs;
969 : : }
970 : 8783 : else if (gimple_assign_load_p (stmt))
971 : : {
972 : 8782 : is_load = true;
973 : 8782 : ref = gimple_assign_rhs1 (stmt);
974 : : }
975 : : else
976 : : return false;
977 : :
978 : 11433 : if (may_be_nonaddressable_p (ref))
979 : : return false;
980 : :
981 : : /* Mask should be integer mode of the same size as the load/store
982 : : mode. */
983 : 11374 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
984 : 11374 : if (!int_mode_for_mode (mode).exists () || VECTOR_MODE_P (mode))
985 : 31 : return false;
986 : :
987 : 11343 : if (can_vec_mask_load_store_p (mode, VOIDmode, is_load))
988 : : return true;
989 : :
990 : : return false;
991 : : }
992 : :
993 : : /* Return true if STMT could be converted from an operation that is
994 : : unconditional to one that is conditional on a bb predicate mask. */
995 : :
996 : : static bool
997 : 13288 : ifcvt_can_predicate (gimple *stmt)
998 : : {
999 : 13288 : basic_block bb = gimple_bb (stmt);
1000 : :
1001 : 320 : if (!(flag_tree_loop_vectorize || bb->loop_father->force_vectorize)
1002 : 13288 : || bb->loop_father->dont_vectorize
1003 : 26576 : || gimple_has_volatile_ops (stmt))
1004 : : return false;
1005 : :
1006 : 13288 : if (gimple_assign_single_p (stmt))
1007 : 11434 : return ifcvt_can_use_mask_load_store (stmt);
1008 : :
1009 : 1854 : tree_code code = gimple_assign_rhs_code (stmt);
1010 : 1854 : tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1011 : 1854 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
1012 : 1854 : if (!types_compatible_p (lhs_type, rhs_type))
1013 : : return false;
1014 : 1100 : internal_fn cond_fn = get_conditional_internal_fn (code);
1015 : 1100 : return (cond_fn != IFN_LAST
1016 : 1100 : && vectorized_internal_fn_supported_p (cond_fn, lhs_type));
1017 : : }
1018 : :
1019 : : /* Return true when STMT is if-convertible.
1020 : :
1021 : : GIMPLE_ASSIGN statement is not if-convertible if,
1022 : : - it is not movable,
1023 : : - it could trap,
1024 : : - LHS is not var decl. */
1025 : :
1026 : : static bool
1027 : 65435 : if_convertible_gimple_assign_stmt_p (gimple *stmt,
1028 : : vec<data_reference_p> refs)
1029 : : {
1030 : 65435 : tree lhs = gimple_assign_lhs (stmt);
1031 : :
1032 : 65435 : if (dump_file && (dump_flags & TDF_DETAILS))
1033 : : {
1034 : 42 : fprintf (dump_file, "-------------------------\n");
1035 : 42 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1036 : : }
1037 : :
1038 : 65435 : if (!is_gimple_reg_type (TREE_TYPE (lhs)))
1039 : : return false;
1040 : :
1041 : : /* Some of these constrains might be too conservative. */
1042 : 65104 : if (stmt_ends_bb_p (stmt)
1043 : 65104 : || gimple_has_volatile_ops (stmt)
1044 : 65029 : || (TREE_CODE (lhs) == SSA_NAME
1045 : 60663 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1046 : 130133 : || gimple_has_side_effects (stmt))
1047 : : {
1048 : 75 : if (dump_file && (dump_flags & TDF_DETAILS))
1049 : 0 : fprintf (dump_file, "stmt not suitable for ifcvt\n");
1050 : 75 : return false;
1051 : : }
1052 : :
1053 : : /* tree-into-ssa.cc uses GF_PLF_1, so avoid it, because
1054 : : in between if_convertible_loop_p and combine_blocks
1055 : : we can perform loop versioning. */
1056 : 65029 : gimple_set_plf (stmt, GF_PLF_2, false);
1057 : :
1058 : 65029 : if ((! gimple_vuse (stmt)
1059 : 19533 : || gimple_could_trap_p_1 (stmt, false, false)
1060 : 19533 : || ! ifcvt_memrefs_wont_trap (stmt, refs))
1061 : 77472 : && gimple_could_trap_p (stmt))
1062 : : {
1063 : 13288 : if (ifcvt_can_predicate (stmt))
1064 : : {
1065 : 2643 : gimple_set_plf (stmt, GF_PLF_2, true);
1066 : 2643 : need_to_predicate = true;
1067 : 2643 : return true;
1068 : : }
1069 : 10645 : if (dump_file && (dump_flags & TDF_DETAILS))
1070 : 0 : fprintf (dump_file, "tree could trap...\n");
1071 : 10645 : return false;
1072 : : }
1073 : 51741 : else if (gimple_needing_rewrite_undefined (stmt))
1074 : : /* We have to rewrite stmts with undefined overflow. */
1075 : 19237 : need_to_rewrite_undefined = true;
1076 : :
1077 : : /* When if-converting stores force versioning, likewise if we
1078 : : ended up generating store data races. */
1079 : 103482 : if (gimple_vdef (stmt))
1080 : 1715 : need_to_predicate = true;
1081 : :
1082 : : return true;
1083 : : }
1084 : :
1085 : : /* Return true when SW switch statement is equivalent to cond, that
1086 : : all non default labels point to the same label.
1087 : :
1088 : : Fallthrough is not checked for and could even happen
1089 : : with cond (using goto), so is handled.
1090 : :
1091 : : This is intended for switches created by the if-switch-conversion
1092 : : pass, but can handle some programmer supplied cases too. */
1093 : :
1094 : : static bool
1095 : 14 : if_convertible_switch_p (gswitch *sw)
1096 : : {
1097 : 14 : if (gimple_switch_num_labels (sw) <= 1)
1098 : : return false;
1099 : 14 : tree label = CASE_LABEL (gimple_switch_label (sw, 1));
1100 : 43 : for (unsigned i = 1; i < gimple_switch_num_labels (sw); i++)
1101 : : {
1102 : 29 : if (CASE_LABEL (gimple_switch_label (sw, i)) != label)
1103 : : return false;
1104 : : }
1105 : : return true;
1106 : : }
1107 : :
1108 : : /* Return true when STMT is if-convertible.
1109 : :
1110 : : A statement is if-convertible if:
1111 : : - it is an if-convertible GIMPLE_ASSIGN,
1112 : : - it is a GIMPLE_LABEL or a GIMPLE_COND,
1113 : : - it is a switch equivalent to COND
1114 : : - it is builtins call,
1115 : : - it is a call to a function with a SIMD clone. */
1116 : :
1117 : : static bool
1118 : 127634 : if_convertible_stmt_p (gimple *stmt, vec<data_reference_p> refs)
1119 : : {
1120 : 127634 : switch (gimple_code (stmt))
1121 : : {
1122 : : case GIMPLE_LABEL:
1123 : : case GIMPLE_DEBUG:
1124 : : case GIMPLE_COND:
1125 : : return true;
1126 : :
1127 : 14 : case GIMPLE_SWITCH:
1128 : 14 : return if_convertible_switch_p (as_a <gswitch *> (stmt));
1129 : :
1130 : 65435 : case GIMPLE_ASSIGN:
1131 : 65435 : return if_convertible_gimple_assign_stmt_p (stmt, refs);
1132 : :
1133 : 1456 : case GIMPLE_CALL:
1134 : 1456 : {
1135 : 1456 : tree fndecl = gimple_call_fndecl (stmt);
1136 : 1456 : if (fndecl)
1137 : : {
1138 : : /* We can vectorize some builtins and functions with SIMD
1139 : : "inbranch" clones. */
1140 : 1214 : struct cgraph_node *node = cgraph_node::get (fndecl);
1141 : 1214 : if (node && node->simd_clones != NULL)
1142 : : /* Ensure that at least one clone can be "inbranch". */
1143 : 1955 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
1144 : 956 : n = n->simdclone->next_clone)
1145 : 1954 : if (n->simdclone->inbranch)
1146 : : {
1147 : 998 : gimple_set_plf (stmt, GF_PLF_2, true);
1148 : 998 : need_to_predicate = true;
1149 : 998 : return true;
1150 : : }
1151 : : }
1152 : :
1153 : : /* There are some IFN_s that are used to replace builtins but have the
1154 : : same semantics. Even if MASK_CALL cannot handle them vectorable_call
1155 : : will insert the proper selection, so do not block conversion. */
1156 : 458 : int flags = gimple_call_flags (stmt);
1157 : 458 : if ((flags & ECF_CONST)
1158 : 458 : && !(flags & ECF_LOOPING_CONST_OR_PURE)
1159 : 916 : && gimple_call_combined_fn (stmt) != CFN_LAST)
1160 : : return true;
1161 : :
1162 : : return false;
1163 : : }
1164 : :
1165 : 0 : default:
1166 : : /* Don't know what to do with 'em so don't do anything. */
1167 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1168 : : {
1169 : 0 : fprintf (dump_file, "don't know what to do\n");
1170 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1171 : : }
1172 : : return false;
1173 : : }
1174 : : }
1175 : :
1176 : : /* Assumes that BB has more than 1 predecessors.
1177 : : Returns false if at least one successor is not on critical edge
1178 : : and true otherwise. */
1179 : :
1180 : : static inline bool
1181 : 73808 : all_preds_critical_p (basic_block bb)
1182 : : {
1183 : 73808 : edge e;
1184 : 73808 : edge_iterator ei;
1185 : :
1186 : 146815 : FOR_EACH_EDGE (e, ei, bb->preds)
1187 : 129882 : if (EDGE_COUNT (e->src->succs) == 1)
1188 : : return false;
1189 : : return true;
1190 : : }
1191 : :
1192 : : /* Return true when BB is if-convertible. This routine does not check
1193 : : basic block's statements and phis.
1194 : :
1195 : : A basic block is not if-convertible if:
1196 : : - it is non-empty and it is after the exit block (in BFS order),
1197 : : - it is after the exit block but before the latch,
1198 : : - its edges are not normal.
1199 : :
1200 : : EXIT_BB is the basic block containing the exit of the LOOP. BB is
1201 : : inside LOOP. */
1202 : :
1203 : : static bool
1204 : 210660 : if_convertible_bb_p (class loop *loop, basic_block bb, basic_block exit_bb)
1205 : : {
1206 : 210660 : edge e;
1207 : 210660 : edge_iterator ei;
1208 : :
1209 : 210660 : if (dump_file && (dump_flags & TDF_DETAILS))
1210 : 102 : fprintf (dump_file, "----------[%d]-------------\n", bb->index);
1211 : :
1212 : 210660 : if (EDGE_COUNT (bb->succs) > 2)
1213 : : return false;
1214 : :
1215 : 421198 : if (gcall *call = safe_dyn_cast <gcall *> (*gsi_last_bb (bb)))
1216 : 187 : if (gimple_call_ctrl_altering_p (call))
1217 : : return false;
1218 : :
1219 : 210599 : if (exit_bb)
1220 : : {
1221 : 38862 : if (bb != loop->latch)
1222 : : {
1223 : 501 : if (dump_file && (dump_flags & TDF_DETAILS))
1224 : 0 : fprintf (dump_file, "basic block after exit bb but before latch\n");
1225 : 501 : return false;
1226 : : }
1227 : 38361 : else if (!empty_block_p (bb))
1228 : : {
1229 : 842 : if (dump_file && (dump_flags & TDF_DETAILS))
1230 : 0 : fprintf (dump_file, "non empty basic block after exit bb\n");
1231 : 842 : return false;
1232 : : }
1233 : 37519 : else if (bb == loop->latch
1234 : 37519 : && bb != exit_bb
1235 : 75038 : && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
1236 : : {
1237 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
1238 : 0 : fprintf (dump_file, "latch is not dominated by exit_block\n");
1239 : 4 : return false;
1240 : : }
1241 : : }
1242 : :
1243 : : /* Be less adventurous and handle only normal edges. */
1244 : 512012 : FOR_EACH_EDGE (e, ei, bb->succs)
1245 : 302770 : if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
1246 : : {
1247 : 10 : if (dump_file && (dump_flags & TDF_DETAILS))
1248 : 0 : fprintf (dump_file, "Difficult to handle edges\n");
1249 : 10 : return false;
1250 : : }
1251 : :
1252 : : return true;
1253 : : }
1254 : :
1255 : : /* Return true when all predecessor blocks of BB are visited. The
1256 : : VISITED bitmap keeps track of the visited blocks. */
1257 : :
1258 : : static bool
1259 : 2266199 : pred_blocks_visited_p (basic_block bb, bitmap *visited)
1260 : : {
1261 : 2266199 : edge e;
1262 : 2266199 : edge_iterator ei;
1263 : 3916942 : FOR_EACH_EDGE (e, ei, bb->preds)
1264 : 2558490 : if (!bitmap_bit_p (*visited, e->src->index))
1265 : : return false;
1266 : :
1267 : : return true;
1268 : : }
1269 : :
1270 : : /* Get body of a LOOP in suitable order for if-conversion. It is
1271 : : caller's responsibility to deallocate basic block list.
1272 : : If-conversion suitable order is, breadth first sort (BFS) order
1273 : : with an additional constraint: select a block only if all its
1274 : : predecessors are already selected. */
1275 : :
1276 : : static basic_block *
1277 : 408047 : get_loop_body_in_if_conv_order (const class loop *loop)
1278 : : {
1279 : 408047 : basic_block *blocks, *blocks_in_bfs_order;
1280 : 408047 : basic_block bb;
1281 : 408047 : bitmap visited;
1282 : 408047 : unsigned int index = 0;
1283 : 408047 : unsigned int visited_count = 0;
1284 : :
1285 : 408047 : gcc_assert (loop->num_nodes);
1286 : 408047 : gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
1287 : :
1288 : 408047 : blocks = XCNEWVEC (basic_block, loop->num_nodes);
1289 : 408047 : visited = BITMAP_ALLOC (NULL);
1290 : :
1291 : 408047 : blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
1292 : :
1293 : 408047 : index = 0;
1294 : 3912924 : while (index < loop->num_nodes)
1295 : : {
1296 : 3096882 : bb = blocks_in_bfs_order [index];
1297 : :
1298 : 3096882 : if (bb->flags & BB_IRREDUCIBLE_LOOP)
1299 : : {
1300 : 52 : free (blocks_in_bfs_order);
1301 : 52 : BITMAP_FREE (visited);
1302 : 52 : free (blocks);
1303 : 52 : return NULL;
1304 : : }
1305 : :
1306 : 3096830 : if (!bitmap_bit_p (visited, bb->index))
1307 : : {
1308 : 2266199 : if (pred_blocks_visited_p (bb, &visited)
1309 : 2266199 : || bb == loop->header)
1310 : : {
1311 : : /* This block is now visited. */
1312 : 1766499 : bitmap_set_bit (visited, bb->index);
1313 : 1766499 : blocks[visited_count++] = bb;
1314 : : }
1315 : : }
1316 : :
1317 : 3096830 : index++;
1318 : :
1319 : 3096830 : if (index == loop->num_nodes
1320 : 470020 : && visited_count != loop->num_nodes)
1321 : : /* Not done yet. */
1322 : 3096830 : index = 0;
1323 : : }
1324 : 407995 : free (blocks_in_bfs_order);
1325 : 407995 : BITMAP_FREE (visited);
1326 : :
1327 : : /* Go through loop and reject if-conversion or lowering of bitfields if we
1328 : : encounter statements we do not believe the vectorizer will be able to
1329 : : handle. If adding a new type of statement here, make sure
1330 : : 'ifcvt_local_dce' is also able to handle it propertly. */
1331 : 2163939 : for (index = 0; index < loop->num_nodes; index++)
1332 : : {
1333 : 1758410 : basic_block bb = blocks[index];
1334 : 1758410 : gimple_stmt_iterator gsi;
1335 : :
1336 : 1758410 : bool may_have_nonlocal_labels
1337 : 1758410 : = bb_with_exit_edge_p (loop, bb) || bb == loop->latch;
1338 : 15682092 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1339 : 12167738 : switch (gimple_code (gsi_stmt (gsi)))
1340 : : {
1341 : 40343 : case GIMPLE_LABEL:
1342 : 40343 : if (!may_have_nonlocal_labels)
1343 : : {
1344 : 6624 : tree label
1345 : 6624 : = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi)));
1346 : 13248 : if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
1347 : : {
1348 : 41 : free (blocks);
1349 : 41 : return NULL;
1350 : : }
1351 : : }
1352 : : /* Fallthru. */
1353 : 12165272 : case GIMPLE_ASSIGN:
1354 : 12165272 : case GIMPLE_CALL:
1355 : 12165272 : case GIMPLE_DEBUG:
1356 : 12165272 : case GIMPLE_COND:
1357 : 12165272 : case GIMPLE_SWITCH:
1358 : 12165272 : gimple_set_uid (gsi_stmt (gsi), 0);
1359 : 12165272 : break;
1360 : 2425 : default:
1361 : 2425 : free (blocks);
1362 : 2425 : return NULL;
1363 : : }
1364 : : }
1365 : : return blocks;
1366 : : }
1367 : :
1368 : : /* Returns true when the analysis of the predicates for all the basic
1369 : : blocks in LOOP succeeded.
1370 : :
1371 : : predicate_bbs first allocates the predicates of the basic blocks.
1372 : : These fields are then initialized with the tree expressions
1373 : : representing the predicates under which a basic block is executed
1374 : : in the LOOP. As the loop->header is executed at each iteration, it
1375 : : has the "true" predicate. Other statements executed under a
1376 : : condition are predicated with that condition, for example
1377 : :
1378 : : | if (x)
1379 : : | S1;
1380 : : | else
1381 : : | S2;
1382 : :
1383 : : S1 will be predicated with "x", and
1384 : : S2 will be predicated with "!x". */
1385 : :
1386 : : static void
1387 : 37515 : predicate_bbs (loop_p loop)
1388 : : {
1389 : 37515 : unsigned int i;
1390 : :
1391 : 241553 : for (i = 0; i < loop->num_nodes; i++)
1392 : 204038 : init_bb_predicate (ifc_bbs[i]);
1393 : :
1394 : 241553 : for (i = 0; i < loop->num_nodes; i++)
1395 : : {
1396 : 204038 : basic_block bb = ifc_bbs[i];
1397 : 204038 : tree cond;
1398 : :
1399 : : /* The loop latch and loop exit block are always executed and
1400 : : have no extra conditions to be processed: skip them. */
1401 : 279068 : if (bb == loop->latch
1402 : 204038 : || bb_with_exit_edge_p (loop, bb))
1403 : : {
1404 : 75030 : reset_bb_predicate (bb);
1405 : 75030 : continue;
1406 : : }
1407 : :
1408 : 129008 : cond = bb_predicate (bb);
1409 : 258016 : if (gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb)))
1410 : : {
1411 : 52695 : tree c2;
1412 : 52695 : edge true_edge, false_edge;
1413 : 52695 : location_t loc = gimple_location (stmt);
1414 : 52695 : tree c;
1415 : : /* gcc.dg/fold-bopcond-1.c shows that despite all forwprop passes
1416 : : conditions can remain unfolded because of multiple uses so
1417 : : try to re-fold here, especially to get precision changing
1418 : : conversions sorted out. Do not simply fold the stmt since
1419 : : this is analysis only. When conditions were embedded in
1420 : : COND_EXPRs those were folded separately before folding the
1421 : : COND_EXPR but as they are now outside we have to make sure
1422 : : to fold them. Do it here - another opportunity would be to
1423 : : fold predicates as they are inserted. */
1424 : 52695 : gimple_match_op cexpr (gimple_match_cond::UNCOND,
1425 : 52695 : gimple_cond_code (stmt),
1426 : : boolean_type_node,
1427 : : gimple_cond_lhs (stmt),
1428 : 52695 : gimple_cond_rhs (stmt));
1429 : 52695 : if (cexpr.resimplify (NULL, follow_all_ssa_edges)
1430 : 3804 : && cexpr.code.is_tree_code ()
1431 : 56499 : && TREE_CODE_CLASS ((tree_code)cexpr.code) == tcc_comparison)
1432 : 562 : c = build2_loc (loc, (tree_code)cexpr.code, boolean_type_node,
1433 : : cexpr.ops[0], cexpr.ops[1]);
1434 : : else
1435 : 52133 : c = build2_loc (loc, gimple_cond_code (stmt),
1436 : : boolean_type_node,
1437 : : gimple_cond_lhs (stmt),
1438 : : gimple_cond_rhs (stmt));
1439 : :
1440 : : /* Add new condition into destination's predicate list. */
1441 : 52695 : extract_true_false_edges_from_block (gimple_bb (stmt),
1442 : : &true_edge, &false_edge);
1443 : :
1444 : : /* If C is true, then TRUE_EDGE is taken. */
1445 : 52695 : add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
1446 : : unshare_expr (c));
1447 : :
1448 : : /* If C is false, then FALSE_EDGE is taken. */
1449 : 52695 : c2 = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
1450 : : unshare_expr (c));
1451 : 52695 : add_to_dst_predicate_list (loop, false_edge,
1452 : : unshare_expr (cond), c2);
1453 : :
1454 : 52695 : cond = NULL_TREE;
1455 : : }
1456 : :
1457 : : /* Assumes the limited COND like switches checked for earlier. */
1458 : 76313 : else if (gswitch *sw = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
1459 : : {
1460 : 72 : location_t loc = gimple_location (*gsi_last_bb (bb));
1461 : :
1462 : 72 : tree default_label = CASE_LABEL (gimple_switch_default_label (sw));
1463 : 72 : tree cond_label = CASE_LABEL (gimple_switch_label (sw, 1));
1464 : :
1465 : 72 : edge false_edge = find_edge (bb, label_to_block (cfun, default_label));
1466 : 72 : edge true_edge = find_edge (bb, label_to_block (cfun, cond_label));
1467 : :
1468 : : /* Create chain of switch tests for each case. */
1469 : 72 : tree switch_cond = NULL_TREE;
1470 : 72 : tree index = gimple_switch_index (sw);
1471 : 278 : for (unsigned i = 1; i < gimple_switch_num_labels (sw); i++)
1472 : : {
1473 : 206 : tree label = gimple_switch_label (sw, i);
1474 : 206 : tree case_cond;
1475 : 206 : if (CASE_HIGH (label))
1476 : : {
1477 : 7 : tree low = build2_loc (loc, GE_EXPR,
1478 : : boolean_type_node,
1479 : 7 : index, fold_convert_loc (loc, TREE_TYPE (index),
1480 : 7 : CASE_LOW (label)));
1481 : 14 : tree high = build2_loc (loc, LE_EXPR,
1482 : : boolean_type_node,
1483 : 7 : index, fold_convert_loc (loc, TREE_TYPE (index),
1484 : 7 : CASE_HIGH (label)));
1485 : 7 : case_cond = build2_loc (loc, TRUTH_AND_EXPR,
1486 : : boolean_type_node,
1487 : : low, high);
1488 : : }
1489 : : else
1490 : 199 : case_cond = build2_loc (loc, EQ_EXPR,
1491 : : boolean_type_node,
1492 : : index,
1493 : 199 : fold_convert_loc (loc, TREE_TYPE (index),
1494 : 199 : CASE_LOW (label)));
1495 : 206 : if (i > 1)
1496 : 134 : switch_cond = build2_loc (loc, TRUTH_OR_EXPR,
1497 : : boolean_type_node,
1498 : : case_cond, switch_cond);
1499 : : else
1500 : : switch_cond = case_cond;
1501 : : }
1502 : :
1503 : 72 : add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
1504 : : unshare_expr (switch_cond));
1505 : 72 : switch_cond = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
1506 : : unshare_expr (switch_cond));
1507 : 72 : add_to_dst_predicate_list (loop, false_edge,
1508 : : unshare_expr (cond), switch_cond);
1509 : 72 : cond = NULL_TREE;
1510 : : }
1511 : :
1512 : : /* If current bb has only one successor, then consider it as an
1513 : : unconditional goto. */
1514 : 280279 : if (single_succ_p (bb))
1515 : : {
1516 : 76241 : basic_block bb_n = single_succ (bb);
1517 : :
1518 : : /* The successor bb inherits the predicate of its
1519 : : predecessor. If there is no predicate in the predecessor
1520 : : bb, then consider the successor bb as always executed. */
1521 : 76241 : if (cond == NULL_TREE)
1522 : 0 : cond = boolean_true_node;
1523 : :
1524 : 76241 : add_to_predicate_list (loop, bb_n, cond);
1525 : : }
1526 : : }
1527 : :
1528 : : /* The loop header is always executed. */
1529 : 37515 : reset_bb_predicate (loop->header);
1530 : 37515 : gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1531 : : && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1532 : 37515 : }
1533 : :
1534 : : /* Build region by adding loop pre-header and post-header blocks. */
1535 : :
1536 : : static vec<basic_block>
1537 : 37515 : build_region (class loop *loop)
1538 : : {
1539 : 37515 : vec<basic_block> region = vNULL;
1540 : 37515 : basic_block exit_bb = NULL;
1541 : :
1542 : 37515 : gcc_assert (ifc_bbs);
1543 : : /* The first element is loop pre-header. */
1544 : 37515 : region.safe_push (loop_preheader_edge (loop)->src);
1545 : :
1546 : 241553 : for (unsigned int i = 0; i < loop->num_nodes; i++)
1547 : : {
1548 : 204038 : basic_block bb = ifc_bbs[i];
1549 : 204038 : region.safe_push (bb);
1550 : : /* Find loop postheader. */
1551 : 204038 : edge e;
1552 : 204038 : edge_iterator ei;
1553 : 452701 : FOR_EACH_EDGE (e, ei, bb->succs)
1554 : 286178 : if (loop_exit_edge_p (loop, e))
1555 : : {
1556 : 37515 : exit_bb = e->dest;
1557 : 37515 : break;
1558 : : }
1559 : : }
1560 : : /* The last element is loop post-header. */
1561 : 37515 : gcc_assert (exit_bb);
1562 : 37515 : region.safe_push (exit_bb);
1563 : 37515 : return region;
1564 : : }
1565 : :
1566 : : /* Return true when LOOP is if-convertible. This is a helper function
1567 : : for if_convertible_loop_p. REFS and DDRS are initialized and freed
1568 : : in if_convertible_loop_p. */
1569 : :
1570 : : static bool
1571 : 38933 : if_convertible_loop_p_1 (class loop *loop, vec<data_reference_p> *refs)
1572 : : {
1573 : 38933 : unsigned int i;
1574 : 38933 : basic_block exit_bb = NULL;
1575 : 38933 : vec<basic_block> region;
1576 : :
1577 : 38933 : calculate_dominance_info (CDI_DOMINATORS);
1578 : :
1579 : 248175 : for (i = 0; i < loop->num_nodes; i++)
1580 : : {
1581 : 210660 : basic_block bb = ifc_bbs[i];
1582 : :
1583 : 210660 : if (!if_convertible_bb_p (loop, bb, exit_bb))
1584 : : return false;
1585 : :
1586 : 209242 : if (bb_with_exit_edge_p (loop, bb))
1587 : 38862 : exit_bb = bb;
1588 : : }
1589 : :
1590 : 37515 : data_reference_p dr;
1591 : :
1592 : 37515 : innermost_DR_map
1593 : 37515 : = new hash_map<innermost_loop_behavior_hash, data_reference_p>;
1594 : 37515 : baseref_DR_map = new hash_map<tree_operand_hash, data_reference_p>;
1595 : :
1596 : : /* Compute post-dominator tree locally. */
1597 : 37515 : region = build_region (loop);
1598 : 37515 : calculate_dominance_info_for_region (CDI_POST_DOMINATORS, region);
1599 : :
1600 : 37515 : predicate_bbs (loop);
1601 : :
1602 : : /* Free post-dominator tree since it is not used after predication. */
1603 : 37515 : free_dominance_info_for_region (cfun, CDI_POST_DOMINATORS, region);
1604 : 37515 : region.release ();
1605 : :
1606 : 169394 : for (i = 0; refs->iterate (i, &dr); i++)
1607 : : {
1608 : 131879 : tree ref = DR_REF (dr);
1609 : :
1610 : 131879 : dr->aux = XNEW (struct ifc_dr);
1611 : 131879 : DR_BASE_W_UNCONDITIONALLY (dr) = false;
1612 : 131879 : DR_RW_UNCONDITIONALLY (dr) = false;
1613 : 131879 : DR_W_UNCONDITIONALLY (dr) = false;
1614 : 131879 : IFC_DR (dr)->rw_predicate = boolean_false_node;
1615 : 131879 : IFC_DR (dr)->w_predicate = boolean_false_node;
1616 : 131879 : IFC_DR (dr)->base_w_predicate = boolean_false_node;
1617 : 131879 : if (gimple_uid (DR_STMT (dr)) == 0)
1618 : 131176 : gimple_set_uid (DR_STMT (dr), i + 1);
1619 : :
1620 : : /* If DR doesn't have innermost loop behavior or it's a compound
1621 : : memory reference, we synthesize its innermost loop behavior
1622 : : for hashing. */
1623 : 131879 : if (TREE_CODE (ref) == COMPONENT_REF
1624 : : || TREE_CODE (ref) == IMAGPART_EXPR
1625 : : || TREE_CODE (ref) == REALPART_EXPR
1626 : 83335 : || !(DR_BASE_ADDRESS (dr) || DR_OFFSET (dr)
1627 : 21027 : || DR_INIT (dr) || DR_STEP (dr)))
1628 : : {
1629 : 125848 : while (TREE_CODE (ref) == COMPONENT_REF
1630 : 70730 : || TREE_CODE (ref) == IMAGPART_EXPR
1631 : 196000 : || TREE_CODE (ref) == REALPART_EXPR)
1632 : 56277 : ref = TREE_OPERAND (ref, 0);
1633 : :
1634 : 69571 : memset (&DR_INNERMOST (dr), 0, sizeof (DR_INNERMOST (dr)));
1635 : 69571 : DR_BASE_ADDRESS (dr) = ref;
1636 : : }
1637 : 131879 : hash_memrefs_baserefs_and_store_DRs_read_written_info (dr);
1638 : : }
1639 : :
1640 : 187173 : for (i = 0; i < loop->num_nodes; i++)
1641 : : {
1642 : 160729 : basic_block bb = ifc_bbs[i];
1643 : 160729 : gimple_stmt_iterator itr;
1644 : :
1645 : : /* Check the if-convertibility of statements in predicated BBs. */
1646 : 160729 : if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1647 : 250171 : for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1648 : 127634 : if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1649 : 12489 : return false;
1650 : : }
1651 : :
1652 : : /* Checking PHIs needs to be done after stmts, as the fact whether there
1653 : : are any masked loads or stores affects the tests. */
1654 : 162237 : for (i = 0; i < loop->num_nodes; i++)
1655 : : {
1656 : 135793 : basic_block bb = ifc_bbs[i];
1657 : 135793 : gphi_iterator itr;
1658 : :
1659 : 260427 : for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1660 : 124634 : if (!if_convertible_phi_p (loop, bb, itr.phi ()))
1661 : : return false;
1662 : : }
1663 : :
1664 : 26444 : if (dump_file)
1665 : 33 : fprintf (dump_file, "Applying if-conversion\n");
1666 : :
1667 : : return true;
1668 : : }
1669 : :
1670 : : /* Return true when LOOP is if-convertible.
1671 : : LOOP is if-convertible if:
1672 : : - it is innermost,
1673 : : - it has two or more basic blocks,
1674 : : - it has only one exit,
1675 : : - loop header is not the exit edge,
1676 : : - if its basic blocks and phi nodes are if convertible. */
1677 : :
1678 : : static bool
1679 : 39119 : if_convertible_loop_p (class loop *loop, vec<data_reference_p> *refs)
1680 : : {
1681 : 39119 : edge e;
1682 : 39119 : edge_iterator ei;
1683 : 39119 : bool res = false;
1684 : :
1685 : : /* Handle only innermost loop. */
1686 : 39119 : if (!loop || loop->inner)
1687 : : {
1688 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1689 : 0 : fprintf (dump_file, "not innermost loop\n");
1690 : 0 : return false;
1691 : : }
1692 : :
1693 : : /* If only one block, no need for if-conversion. */
1694 : 39119 : if (loop->num_nodes <= 2)
1695 : : {
1696 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1697 : 0 : fprintf (dump_file, "less than 2 basic blocks\n");
1698 : 0 : return false;
1699 : : }
1700 : :
1701 : : /* If one of the loop header's edge is an exit edge then do not
1702 : : apply if-conversion. */
1703 : 117188 : FOR_EACH_EDGE (e, ei, loop->header->succs)
1704 : 78255 : if (loop_exit_edge_p (loop, e))
1705 : : return false;
1706 : :
1707 : 38933 : res = if_convertible_loop_p_1 (loop, refs);
1708 : :
1709 : 76448 : delete innermost_DR_map;
1710 : 38933 : innermost_DR_map = NULL;
1711 : :
1712 : 76448 : delete baseref_DR_map;
1713 : 38933 : baseref_DR_map = NULL;
1714 : :
1715 : 38933 : return res;
1716 : : }
1717 : :
1718 : : /* Return reduc_1 if has_nop.
1719 : :
1720 : : if (...)
1721 : : tmp1 = (unsigned type) reduc_1;
1722 : : tmp2 = tmp1 + rhs2;
1723 : : reduc_3 = (signed type) tmp2. */
1724 : : static tree
1725 : 12492 : strip_nop_cond_scalar_reduction (bool has_nop, tree op)
1726 : : {
1727 : 12492 : if (!has_nop)
1728 : : return op;
1729 : :
1730 : 404 : if (TREE_CODE (op) != SSA_NAME)
1731 : : return NULL_TREE;
1732 : :
1733 : 358 : gassign *stmt = safe_dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
1734 : 358 : if (!stmt
1735 : 358 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
1736 : 220 : || !tree_nop_conversion_p (TREE_TYPE (op), TREE_TYPE
1737 : : (gimple_assign_rhs1 (stmt))))
1738 : 149 : return NULL_TREE;
1739 : :
1740 : 209 : return gimple_assign_rhs1 (stmt);
1741 : : }
1742 : :
1743 : : /* Returns true if def-stmt for phi argument ARG is simple increment/decrement
1744 : : which is in predicated basic block.
1745 : : In fact, the following PHI pattern is searching:
1746 : : loop-header:
1747 : : reduc_1 = PHI <..., reduc_2>
1748 : : ...
1749 : : if (...)
1750 : : reduc_3 = ...
1751 : : reduc_2 = PHI <reduc_1, reduc_3>
1752 : :
1753 : : ARG_0 and ARG_1 are correspondent PHI arguments.
1754 : : REDUC, OP0 and OP1 contain reduction stmt and its operands.
1755 : : EXTENDED is true if PHI has > 2 arguments. */
1756 : :
1757 : : static bool
1758 : 43507 : is_cond_scalar_reduction (gimple *phi, gimple **reduc, tree arg_0, tree arg_1,
1759 : : tree *op0, tree *op1, bool extended, bool* has_nop,
1760 : : gimple **nop_reduc)
1761 : : {
1762 : 43507 : tree lhs, r_op1, r_op2, r_nop1, r_nop2;
1763 : 43507 : gimple *stmt;
1764 : 43507 : gimple *header_phi = NULL;
1765 : 43507 : enum tree_code reduction_op;
1766 : 43507 : basic_block bb = gimple_bb (phi);
1767 : 43507 : class loop *loop = bb->loop_father;
1768 : 43507 : edge latch_e = loop_latch_edge (loop);
1769 : 43507 : imm_use_iterator imm_iter;
1770 : 43507 : use_operand_p use_p;
1771 : 43507 : edge e;
1772 : 43507 : edge_iterator ei;
1773 : 43507 : bool result = *has_nop = false;
1774 : 43507 : if (TREE_CODE (arg_0) != SSA_NAME || TREE_CODE (arg_1) != SSA_NAME)
1775 : : return false;
1776 : :
1777 : 27875 : if (!extended && gimple_code (SSA_NAME_DEF_STMT (arg_0)) == GIMPLE_PHI)
1778 : : {
1779 : 7393 : lhs = arg_1;
1780 : 7393 : header_phi = SSA_NAME_DEF_STMT (arg_0);
1781 : 7393 : stmt = SSA_NAME_DEF_STMT (arg_1);
1782 : : }
1783 : 20482 : else if (gimple_code (SSA_NAME_DEF_STMT (arg_1)) == GIMPLE_PHI)
1784 : : {
1785 : 9639 : lhs = arg_0;
1786 : 9639 : header_phi = SSA_NAME_DEF_STMT (arg_1);
1787 : 9639 : stmt = SSA_NAME_DEF_STMT (arg_0);
1788 : : }
1789 : : else
1790 : : return false;
1791 : 17032 : if (gimple_bb (header_phi) != loop->header)
1792 : : return false;
1793 : :
1794 : 16146 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, latch_e) != PHI_RESULT (phi))
1795 : : return false;
1796 : :
1797 : 10254 : if (gimple_code (stmt) != GIMPLE_ASSIGN
1798 : 10254 : || gimple_has_volatile_ops (stmt))
1799 : : return false;
1800 : :
1801 : 10124 : if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
1802 : : return false;
1803 : :
1804 : 10037 : if (!is_predicated (gimple_bb (stmt)))
1805 : : return false;
1806 : :
1807 : : /* Check that stmt-block is predecessor of phi-block. */
1808 : 7898 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1809 : 7838 : if (e->dest == bb)
1810 : : {
1811 : : result = true;
1812 : : break;
1813 : : }
1814 : 7778 : if (!result)
1815 : : return false;
1816 : :
1817 : 7718 : if (!has_single_use (lhs))
1818 : : return false;
1819 : :
1820 : 7675 : reduction_op = gimple_assign_rhs_code (stmt);
1821 : :
1822 : : /* Catch something like below
1823 : :
1824 : : loop-header:
1825 : : reduc_1 = PHI <..., reduc_2>
1826 : : ...
1827 : : if (...)
1828 : : tmp1 = (unsigned type) reduc_1;
1829 : : tmp2 = tmp1 + rhs2;
1830 : : reduc_3 = (signed type) tmp2;
1831 : :
1832 : : reduc_2 = PHI <reduc_1, reduc_3>
1833 : :
1834 : : and convert to
1835 : :
1836 : : reduc_2 = PHI <0, reduc_1>
1837 : : tmp1 = (unsigned type)reduc_1;
1838 : : ifcvt = cond_expr ? rhs2 : 0
1839 : : tmp2 = tmp1 +/- ifcvt;
1840 : : reduc_1 = (signed type)tmp2; */
1841 : :
1842 : 7675 : if (CONVERT_EXPR_CODE_P (reduction_op))
1843 : : {
1844 : 403 : lhs = gimple_assign_rhs1 (stmt);
1845 : 403 : if (TREE_CODE (lhs) != SSA_NAME
1846 : 403 : || !has_single_use (lhs))
1847 : : return false;
1848 : :
1849 : 217 : *nop_reduc = stmt;
1850 : 217 : stmt = SSA_NAME_DEF_STMT (lhs);
1851 : 217 : if (gimple_bb (stmt) != gimple_bb (*nop_reduc)
1852 : 217 : || !is_gimple_assign (stmt))
1853 : : return false;
1854 : :
1855 : 214 : *has_nop = true;
1856 : 214 : reduction_op = gimple_assign_rhs_code (stmt);
1857 : : }
1858 : :
1859 : 7486 : if (reduction_op != PLUS_EXPR
1860 : : && reduction_op != MINUS_EXPR
1861 : 7486 : && reduction_op != MULT_EXPR
1862 : 7486 : && reduction_op != BIT_IOR_EXPR
1863 : : && reduction_op != BIT_XOR_EXPR
1864 : 1386 : && reduction_op != BIT_AND_EXPR)
1865 : : return false;
1866 : 6246 : r_op1 = gimple_assign_rhs1 (stmt);
1867 : 6246 : r_op2 = gimple_assign_rhs2 (stmt);
1868 : :
1869 : 6246 : r_nop1 = strip_nop_cond_scalar_reduction (*has_nop, r_op1);
1870 : 6246 : r_nop2 = strip_nop_cond_scalar_reduction (*has_nop, r_op2);
1871 : :
1872 : : /* Make R_OP1 to hold reduction variable. */
1873 : 6246 : if (r_nop2 == PHI_RESULT (header_phi)
1874 : 6246 : && commutative_tree_code (reduction_op))
1875 : : {
1876 : : std::swap (r_op1, r_op2);
1877 : : std::swap (r_nop1, r_nop2);
1878 : : }
1879 : 5156 : else if (r_nop1 != PHI_RESULT (header_phi))
1880 : : return false;
1881 : :
1882 : 5935 : if (*has_nop)
1883 : : {
1884 : : /* Check that R_NOP1 is used in nop_stmt or in PHI only. */
1885 : 578 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_nop1)
1886 : : {
1887 : 294 : gimple *use_stmt = USE_STMT (use_p);
1888 : 294 : if (is_gimple_debug (use_stmt))
1889 : 0 : continue;
1890 : 294 : if (use_stmt == SSA_NAME_DEF_STMT (r_op1))
1891 : 121 : continue;
1892 : 173 : if (use_stmt != phi)
1893 : 42 : return false;
1894 : 163 : }
1895 : : }
1896 : :
1897 : : /* Check that R_OP1 is used in reduction stmt or in PHI only. */
1898 : 23673 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_op1)
1899 : : {
1900 : 12353 : gimple *use_stmt = USE_STMT (use_p);
1901 : 12353 : if (is_gimple_debug (use_stmt))
1902 : 29 : continue;
1903 : 12324 : if (use_stmt == stmt)
1904 : 5464 : continue;
1905 : 6860 : if (gimple_code (use_stmt) != GIMPLE_PHI)
1906 : 466 : return false;
1907 : 466 : }
1908 : :
1909 : 5427 : *op0 = r_op1; *op1 = r_op2;
1910 : 5427 : *reduc = stmt;
1911 : 5427 : return true;
1912 : : }
1913 : :
1914 : : /* Converts conditional scalar reduction into unconditional form, e.g.
1915 : : bb_4
1916 : : if (_5 != 0) goto bb_5 else goto bb_6
1917 : : end_bb_4
1918 : : bb_5
1919 : : res_6 = res_13 + 1;
1920 : : end_bb_5
1921 : : bb_6
1922 : : # res_2 = PHI <res_13(4), res_6(5)>
1923 : : end_bb_6
1924 : :
1925 : : will be converted into sequence
1926 : : _ifc__1 = _5 != 0 ? 1 : 0;
1927 : : res_2 = res_13 + _ifc__1;
1928 : : Argument SWAP tells that arguments of conditional expression should be
1929 : : swapped.
1930 : : If LOOP_VERSIONED is true if we assume that we versioned the loop for
1931 : : vectorization. In that case we can create a COND_OP.
1932 : : Returns rhs of resulting PHI assignment. */
1933 : :
1934 : : static tree
1935 : 5427 : convert_scalar_cond_reduction (gimple *reduc, gimple_stmt_iterator *gsi,
1936 : : tree cond, tree op0, tree op1, bool swap,
1937 : : bool has_nop, gimple* nop_reduc,
1938 : : bool loop_versioned)
1939 : : {
1940 : 5427 : gimple_stmt_iterator stmt_it;
1941 : 5427 : gimple *new_assign;
1942 : 5427 : tree rhs;
1943 : 5427 : tree rhs1 = gimple_assign_rhs1 (reduc);
1944 : 5427 : tree lhs = gimple_assign_lhs (reduc);
1945 : 5427 : tree tmp = make_temp_ssa_name (TREE_TYPE (rhs1), NULL, "_ifc_");
1946 : 5427 : tree c;
1947 : 5427 : enum tree_code reduction_op = gimple_assign_rhs_code (reduc);
1948 : 5427 : tree op_nochange = neutral_op_for_reduction (TREE_TYPE (rhs1), reduction_op,
1949 : : NULL, false);
1950 : 5427 : gimple_seq stmts = NULL;
1951 : :
1952 : 5427 : if (dump_file && (dump_flags & TDF_DETAILS))
1953 : : {
1954 : 2 : fprintf (dump_file, "Found cond scalar reduction.\n");
1955 : 2 : print_gimple_stmt (dump_file, reduc, 0, TDF_SLIM);
1956 : : }
1957 : :
1958 : : /* If possible create a COND_OP instead of a COND_EXPR and an OP_EXPR.
1959 : : The COND_OP will have a neutral_op else value. */
1960 : 5427 : internal_fn ifn;
1961 : 5427 : ifn = get_conditional_internal_fn (reduction_op);
1962 : 5427 : if (loop_versioned && ifn != IFN_LAST
1963 : 5425 : && vectorized_internal_fn_supported_p (ifn, TREE_TYPE (lhs))
1964 : 6785 : && !swap)
1965 : : {
1966 : 1338 : gcall *cond_call = gimple_build_call_internal (ifn, 4,
1967 : : unshare_expr (cond),
1968 : : op0, op1, op0);
1969 : 1338 : gsi_insert_before (gsi, cond_call, GSI_SAME_STMT);
1970 : 1338 : gimple_call_set_lhs (cond_call, tmp);
1971 : : rhs = tmp;
1972 : : }
1973 : : else
1974 : : {
1975 : : /* Build cond expression using COND and constant operand
1976 : : of reduction rhs. */
1977 : 7694 : c = fold_build_cond_expr (TREE_TYPE (rhs1),
1978 : : unshare_expr (cond),
1979 : : swap ? op_nochange : op1,
1980 : : swap ? op1 : op_nochange);
1981 : : /* Create assignment stmt and insert it at GSI. */
1982 : 4089 : new_assign = gimple_build_assign (tmp, c);
1983 : 4089 : gsi_insert_before (gsi, new_assign, GSI_SAME_STMT);
1984 : : /* Build rhs for unconditional increment/decrement/logic_operation. */
1985 : 4089 : rhs = gimple_build (&stmts, reduction_op,
1986 : 4089 : TREE_TYPE (rhs1), op0, tmp);
1987 : : }
1988 : :
1989 : 5427 : if (has_nop)
1990 : : {
1991 : 121 : rhs = gimple_convert (&stmts,
1992 : 121 : TREE_TYPE (gimple_assign_lhs (nop_reduc)), rhs);
1993 : 121 : stmt_it = gsi_for_stmt (nop_reduc);
1994 : 121 : gsi_remove (&stmt_it, true);
1995 : 121 : release_defs (nop_reduc);
1996 : : }
1997 : 5427 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1998 : :
1999 : : /* Delete original reduction stmt. */
2000 : 5427 : stmt_it = gsi_for_stmt (reduc);
2001 : 5427 : gsi_remove (&stmt_it, true);
2002 : 5427 : release_defs (reduc);
2003 : 5427 : return rhs;
2004 : : }
2005 : :
2006 : : /* Generate a simplified conditional. */
2007 : :
2008 : : static tree
2009 : 49627 : gen_simplified_condition (tree cond, scalar_cond_masked_set_type &cond_set)
2010 : : {
2011 : : /* Check if the value is already live in a previous branch. This resolves
2012 : : nested conditionals from diamond PHI reductions. */
2013 : 49627 : if (TREE_CODE (cond) == SSA_NAME)
2014 : : {
2015 : 49619 : gimple *stmt = SSA_NAME_DEF_STMT (cond);
2016 : 49619 : gassign *assign = NULL;
2017 : 49619 : if ((assign = as_a <gassign *> (stmt))
2018 : 49619 : && gimple_assign_rhs_code (assign) == BIT_AND_EXPR)
2019 : : {
2020 : 3483 : tree arg1 = gimple_assign_rhs1 (assign);
2021 : 3483 : tree arg2 = gimple_assign_rhs2 (assign);
2022 : 3483 : if (cond_set.contains ({ arg1, 1 }))
2023 : 153 : arg1 = boolean_true_node;
2024 : : else
2025 : 3330 : arg1 = gen_simplified_condition (arg1, cond_set);
2026 : :
2027 : 3483 : if (cond_set.contains ({ arg2, 1 }))
2028 : 1788 : arg2 = boolean_true_node;
2029 : : else
2030 : 1695 : arg2 = gen_simplified_condition (arg2, cond_set);
2031 : :
2032 : 3483 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, arg1, arg2);
2033 : : }
2034 : : }
2035 : 49627 : return cond;
2036 : : }
2037 : :
2038 : : /* Structure used to track meta-data on PHI arguments used to generate
2039 : : most efficient comparison sequence to slatten a PHI node. */
2040 : :
2041 : : typedef struct ifcvt_arg_entry
2042 : : {
2043 : : /* The PHI node argument value. */
2044 : : tree arg;
2045 : :
2046 : : /* The number of compares required to reach this PHI node from start of the
2047 : : BB being if-converted. */
2048 : : unsigned num_compares;
2049 : :
2050 : : /* The number of times this PHI node argument appears in the current PHI
2051 : : node. */
2052 : : unsigned occurs;
2053 : :
2054 : : /* The indices at which this PHI arg occurs inside the PHI node. */
2055 : : vec <int> *indexes;
2056 : : } ifcvt_arg_entry_t;
2057 : :
2058 : : /* Produce condition for all occurrences of ARG in PHI node. Set *INVERT
2059 : : as to whether the condition is inverted. */
2060 : :
2061 : : static tree
2062 : 4240 : gen_phi_arg_condition (gphi *phi, ifcvt_arg_entry_t &arg,
2063 : : gimple_stmt_iterator *gsi,
2064 : : scalar_cond_masked_set_type &cond_set, bool *invert)
2065 : : {
2066 : 4240 : int len;
2067 : 4240 : int i;
2068 : 4240 : tree cond = NULL_TREE;
2069 : 4240 : tree c;
2070 : 4240 : edge e;
2071 : :
2072 : 4240 : *invert = false;
2073 : 4240 : len = arg.indexes->length ();
2074 : 4240 : gcc_assert (len > 0);
2075 : 8547 : for (i = 0; i < len; i++)
2076 : : {
2077 : 4307 : e = gimple_phi_arg_edge (phi, (*arg.indexes)[i]);
2078 : 4307 : c = bb_predicate (e->src);
2079 : 4307 : if (is_true_predicate (c))
2080 : : {
2081 : 0 : cond = c;
2082 : 0 : break;
2083 : : }
2084 : : /* If we have just a single inverted predicate, signal that and
2085 : : instead invert the COND_EXPR arms. */
2086 : 4307 : if (len == 1 && TREE_CODE (c) == TRUTH_NOT_EXPR)
2087 : : {
2088 : 84 : c = TREE_OPERAND (c, 0);
2089 : 84 : *invert = true;
2090 : : }
2091 : :
2092 : 4307 : c = gen_simplified_condition (c, cond_set);
2093 : 4307 : c = force_gimple_operand_gsi (gsi, unshare_expr (c),
2094 : : true, NULL_TREE, true, GSI_SAME_STMT);
2095 : 4307 : if (cond != NULL_TREE)
2096 : : {
2097 : : /* Must build OR expression. */
2098 : 67 : cond = fold_or_predicates (EXPR_LOCATION (c), c, cond);
2099 : 67 : cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
2100 : : NULL_TREE, true, GSI_SAME_STMT);
2101 : : }
2102 : : else
2103 : : cond = c;
2104 : :
2105 : : /* Register the new possibly simplified conditional. When more than 2
2106 : : entries in a phi node we chain entries in the false branch, so the
2107 : : inverted condition is active. */
2108 : 4307 : scalar_cond_masked_key pred_cond ({ cond, 1 });
2109 : 4307 : if (!*invert)
2110 : 4223 : pred_cond.inverted_p = !pred_cond.inverted_p;
2111 : 4307 : cond_set.add (pred_cond);
2112 : : }
2113 : 4240 : gcc_assert (cond != NULL_TREE);
2114 : 4240 : return cond;
2115 : : }
2116 : :
2117 : : /* Find the operand which is different between ARG0_OP and ARG1_OP.
2118 : : Returns the operand num where the difference is.
2119 : : Set NEWARG0 and NEWARG1 from the different argument.
2120 : : Returns -1 if none is found.
2121 : : If ARG0_OP/ARG1_OP is commutative also try swapping the
2122 : : two commutative operands and return the operand number where
2123 : : the difference happens in ARG0_OP. */
2124 : :
2125 : : static int
2126 : 1114 : find_different_opnum (const gimple_match_op &arg0_op,
2127 : : const gimple_match_op &arg1_op,
2128 : : tree *new_arg0, tree *new_arg1)
2129 : : {
2130 : 1114 : unsigned opnum = -1;
2131 : 1114 : unsigned first;
2132 : 1114 : first = first_commutative_argument (arg1_op.code, arg1_op.type);
2133 : 3199 : for (unsigned i = 0; i < arg0_op.num_ops; i++)
2134 : : {
2135 : 2199 : if (!operand_equal_for_phi_arg_p (arg0_op.ops[i],
2136 : 2199 : arg1_op.ops[i]))
2137 : : {
2138 : : /* Can handle only one non equal operand. */
2139 : 1228 : if (opnum != -1u)
2140 : : {
2141 : : /* Though if opnum is right before i and opnum is equal
2142 : : to the first communtative argument, handle communtative
2143 : : specially. */
2144 : 114 : if (i == opnum + 1 && opnum == first)
2145 : 65 : goto commutative;
2146 : : return -1;
2147 : : }
2148 : : opnum = i;
2149 : : }
2150 : : }
2151 : : /* If all operands are equal only do this is there was single
2152 : : operand. */
2153 : 1000 : if (opnum == -1u)
2154 : : {
2155 : 0 : if (arg0_op.num_ops != 1)
2156 : : return -1;
2157 : : opnum = 0;
2158 : : }
2159 : 1000 : *new_arg0 = arg0_op.ops[opnum];
2160 : 1000 : *new_arg1 = arg1_op.ops[opnum];
2161 : 1000 : return opnum;
2162 : :
2163 : : /* Handle commutative operations. */
2164 : 65 : commutative:
2165 : 65 : gcc_assert (first != -1u);
2166 : :
2167 : : /* Check the rest of the arguments to make sure they are the same. */
2168 : 65 : for (unsigned i = first + 2; i < arg0_op.num_ops; i++)
2169 : 0 : if (!operand_equal_for_phi_arg_p (arg0_op.ops[i],
2170 : 0 : arg1_op.ops[i]))
2171 : : return -1;
2172 : :
2173 : : /* If the arg0[first+1] and arg1[first] are the same
2174 : : then the one which is different is arg0[first] and arg1[first+1]
2175 : : return first since this is based on arg0. */
2176 : 65 : if (operand_equal_for_phi_arg_p (arg0_op.ops[first + 1],
2177 : 65 : arg1_op.ops[first]))
2178 : : {
2179 : 0 : *new_arg0 = arg0_op.ops[first];
2180 : 0 : *new_arg1 = arg1_op.ops[first + 1];
2181 : 0 : return first;
2182 : : }
2183 : : /* If the arg0[first] and arg1[first+1] are the same
2184 : : then the one which is different is arg0[first+1] and arg1[first]
2185 : : return first+1 since this is based on arg0. */
2186 : 65 : if (operand_equal_for_phi_arg_p (arg0_op.ops[first],
2187 : 65 : arg1_op.ops[first + 1]))
2188 : : {
2189 : 35 : *new_arg0 = arg0_op.ops[first + 1];
2190 : 35 : *new_arg1 = arg1_op.ops[first];
2191 : 35 : return first + 1;
2192 : : }
2193 : : return -1;
2194 : : }
2195 : :
2196 : : /* Factors out an operation from *ARG0 and *ARG1 and
2197 : : create the new statement at GSI. *RES is the
2198 : : result of that new statement. Update *ARG0 and *ARG1
2199 : : and *RES to the new values if the factoring happened.
2200 : : Loops until all of the factoring is completed. */
2201 : :
2202 : : static void
2203 : 40295 : factor_out_operators (tree *res, gimple_stmt_iterator *gsi,
2204 : : tree *arg0, tree *arg1, gphi *phi)
2205 : : {
2206 : 40295 : gimple_match_op arg0_op, arg1_op;
2207 : 40295 : bool repeated = false;
2208 : :
2209 : 41320 : again:
2210 : 41320 : if (TREE_CODE (*arg0) != SSA_NAME || TREE_CODE (*arg1) != SSA_NAME)
2211 : : return;
2212 : :
2213 : 26711 : if (operand_equal_p (*arg0, *arg1))
2214 : : return;
2215 : :
2216 : : /* If either args have > 1 use, then this transformation actually
2217 : : increases the number of expressions evaluated at runtime. */
2218 : 26711 : if (repeated
2219 : 26711 : ? (!has_zero_uses (*arg0) || !has_zero_uses (*arg1))
2220 : 25728 : : (!has_single_use (*arg0) || !has_single_use (*arg1)))
2221 : : return;
2222 : :
2223 : 5584 : gimple *arg0_def_stmt = SSA_NAME_DEF_STMT (*arg0);
2224 : 5584 : if (!gimple_extract_op (arg0_def_stmt, &arg0_op))
2225 : : return;
2226 : :
2227 : : /* At this point there should be no ssa names occuring in abnormals. */
2228 : 2142 : gcc_assert (!arg0_op.operands_occurs_in_abnormal_phi ());
2229 : :
2230 : 2142 : gimple *arg1_def_stmt = SSA_NAME_DEF_STMT (*arg1);
2231 : 2142 : if (!gimple_extract_op (arg1_def_stmt, &arg1_op))
2232 : : return;
2233 : :
2234 : : /* At this point there should be no ssa names occuring in abnormals. */
2235 : 1818 : gcc_assert (!arg1_op.operands_occurs_in_abnormal_phi ());
2236 : :
2237 : : /* No factoring can happen if the codes are different
2238 : : or the number operands. */
2239 : 1818 : if (arg1_op.code != arg0_op.code
2240 : 1818 : || arg1_op.num_ops != arg0_op.num_ops)
2241 : : return;
2242 : :
2243 : 1114 : tree new_arg0, new_arg1;
2244 : 1114 : int opnum = find_different_opnum (arg0_op, arg1_op, &new_arg0, &new_arg1);
2245 : 1114 : if (opnum == -1)
2246 : : return;
2247 : :
2248 : : /* BIT_FIELD_REF and BIT_INSERT_EXPR can't be factored out for non-0 operands
2249 : : as the other operands require constants. */
2250 : 1035 : if ((arg1_op.code == BIT_FIELD_REF
2251 : 1026 : || arg1_op.code == BIT_INSERT_EXPR)
2252 : 1044 : && opnum != 0)
2253 : : return;
2254 : :
2255 : :
2256 : 1027 : if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
2257 : : return;
2258 : 1025 : tree new_res = make_ssa_name (TREE_TYPE (new_arg0), NULL);
2259 : :
2260 : : /* Create the operation stmt if possible and insert it. */
2261 : :
2262 : 1025 : gimple_match_op new_op = arg0_op;
2263 : 1025 : new_op.ops[opnum] = new_res;
2264 : 1025 : gimple_seq seq = NULL;
2265 : 1025 : tree result = *res;
2266 : 1025 : result = maybe_push_res_to_seq (&new_op, &seq, result);
2267 : :
2268 : : /* If we can't create the new statement, release the temp name
2269 : : and return back. */
2270 : 1025 : if (!result)
2271 : : {
2272 : 0 : release_ssa_name (new_res);
2273 : 0 : return;
2274 : : }
2275 : 1025 : gsi_insert_seq_before (gsi, seq, GSI_CONTINUE_LINKING);
2276 : :
2277 : 1025 : if (dump_file && (dump_flags & TDF_DETAILS))
2278 : : {
2279 : 9 : fprintf (dump_file, "PHI ");
2280 : 9 : print_generic_expr (dump_file, gimple_phi_result (phi));
2281 : 9 : fprintf (dump_file,
2282 : : " changed to factor operation out from COND_EXPR.\n");
2283 : 9 : fprintf (dump_file, "New stmt with OPERATION that defines ");
2284 : 9 : print_generic_expr (dump_file, result);
2285 : 9 : fprintf (dump_file, ".\n");
2286 : : }
2287 : :
2288 : : /* Remove the old operation(s) that has single use. */
2289 : 1025 : gimple_stmt_iterator gsi_for_def;
2290 : :
2291 : 1025 : gsi_for_def = gsi_for_stmt (arg0_def_stmt);
2292 : 1025 : gsi_remove (&gsi_for_def, true);
2293 : 1025 : release_defs (arg0_def_stmt);
2294 : 1025 : gsi_for_def = gsi_for_stmt (arg1_def_stmt);
2295 : 1025 : gsi_remove (&gsi_for_def, true);
2296 : 1025 : release_defs (arg1_def_stmt);
2297 : :
2298 : : /* Update the arguments and try again. */
2299 : 1025 : *arg0 = new_arg0;
2300 : 1025 : *arg1 = new_arg1;
2301 : 1025 : *res = new_res;
2302 : :
2303 : : /* Update the phi node too. */
2304 : 1025 : gimple_phi_set_result (phi, new_res);
2305 : 1025 : gimple_phi_arg (phi, 0)->def = new_arg0;
2306 : 1025 : gimple_phi_arg (phi, 1)->def = new_arg1;
2307 : 1025 : update_stmt (phi);
2308 : :
2309 : 1025 : repeated = true;
2310 : 1025 : goto again;
2311 : : }
2312 : :
2313 : : /* Create the smallest nested conditional possible. On pre-order we record
2314 : : which conditionals are live, and on post-order rewrite the chain by removing
2315 : : already active conditions.
2316 : :
2317 : : As an example we simplify:
2318 : :
2319 : : _7 = a_10 < 0;
2320 : : _21 = a_10 >= 0;
2321 : : _22 = a_10 < e_11(D);
2322 : : _23 = _21 & _22;
2323 : : _ifc__42 = _23 ? t_13 : 0;
2324 : : t_6 = _7 ? 1 : _ifc__42
2325 : :
2326 : : into
2327 : :
2328 : : _7 = a_10 < 0;
2329 : : _22 = a_10 < e_11(D);
2330 : : _ifc__42 = _22 ? t_13 : 0;
2331 : : t_6 = _7 ? 1 : _ifc__42;
2332 : :
2333 : : which produces better code. */
2334 : :
2335 : : static tree
2336 : 6362 : gen_phi_nest_statement (gphi *phi, gimple_stmt_iterator *gsi,
2337 : : scalar_cond_masked_set_type &cond_set, tree type,
2338 : : gimple **res_stmt, tree lhs0,
2339 : : vec<struct ifcvt_arg_entry> &args, unsigned idx)
2340 : : {
2341 : 12724 : if (idx == args.length ())
2342 : 2122 : return args[idx - 1].arg;
2343 : :
2344 : 4240 : bool invert;
2345 : 4240 : tree cond = gen_phi_arg_condition (phi, args[idx - 1], gsi, cond_set,
2346 : : &invert);
2347 : 4240 : tree arg1 = gen_phi_nest_statement (phi, gsi, cond_set, type, res_stmt, lhs0,
2348 : : args, idx + 1);
2349 : :
2350 : 4240 : unsigned prev = idx;
2351 : 4240 : unsigned curr = prev - 1;
2352 : 4240 : tree arg0 = args[curr].arg;
2353 : 4240 : tree rhs, lhs;
2354 : 4240 : if (idx > 1)
2355 : 2118 : lhs = make_temp_ssa_name (type, NULL, "_ifc_");
2356 : : else
2357 : : lhs = lhs0;
2358 : :
2359 : 4240 : if (invert)
2360 : 84 : rhs = fold_build_cond_expr (type, unshare_expr (cond),
2361 : : arg1, arg0);
2362 : : else
2363 : 4156 : rhs = fold_build_cond_expr (type, unshare_expr (cond),
2364 : : arg0, arg1);
2365 : 4240 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
2366 : 4240 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2367 : 4240 : update_stmt (new_stmt);
2368 : 4240 : *res_stmt = new_stmt;
2369 : 4240 : return lhs;
2370 : : }
2371 : :
2372 : : /* When flattening a PHI node we have a choice of which conditions to test to
2373 : : for all the paths from the start of the dominator block of the BB with the
2374 : : PHI node. If the PHI node has X arguments we have to only test X - 1
2375 : : conditions as the last one is implicit. It does matter which conditions we
2376 : : test first. We should test the shortest condition first (distance here is
2377 : : measures in the number of logical operators in the condition) and the
2378 : : longest one last. This allows us to skip testing the most expensive
2379 : : condition. To accomplish this we need to sort the conditions. P1 and P2
2380 : : are sorted first based on the number of logical operations (num_compares)
2381 : : and then by how often they occur in the PHI node. */
2382 : :
2383 : : static int
2384 : 36318 : cmp_arg_entry (const void *p1, const void *p2, void * /* data. */)
2385 : : {
2386 : 36318 : const ifcvt_arg_entry sval1 = *(const ifcvt_arg_entry *)p1;
2387 : 36318 : const ifcvt_arg_entry sval2 = *(const ifcvt_arg_entry *)p2;
2388 : :
2389 : 36318 : if (sval1.num_compares < sval2.num_compares)
2390 : : return -1;
2391 : 12741 : else if (sval1.num_compares > sval2.num_compares)
2392 : : return 1;
2393 : :
2394 : 621 : if (sval1.occurs < sval2.occurs)
2395 : : return -1;
2396 : 621 : else if (sval1.occurs > sval2.occurs)
2397 : 0 : return 1;
2398 : :
2399 : : return 0;
2400 : : }
2401 : :
2402 : : /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
2403 : : This routine can handle PHI nodes with more than two arguments.
2404 : :
2405 : : For example,
2406 : : S1: A = PHI <x1(1), x2(5)>
2407 : : is converted into,
2408 : : S2: A = cond ? x1 : x2;
2409 : :
2410 : : The generated code is inserted at GSI that points to the top of
2411 : : basic block's statement list.
2412 : : If PHI node has more than two arguments a chain of conditional
2413 : : expression is produced.
2414 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
2415 : : vectorization. */
2416 : :
2417 : :
2418 : : static void
2419 : 45686 : predicate_scalar_phi (gphi *phi, gimple_stmt_iterator *gsi, bool loop_versioned)
2420 : : {
2421 : 45686 : gimple *new_stmt = NULL, *reduc, *nop_reduc;
2422 : 45686 : tree rhs, res, arg0, arg1, op0, op1, scev;
2423 : 45686 : tree cond;
2424 : 45686 : unsigned int index0;
2425 : 45686 : edge e;
2426 : 45686 : basic_block bb;
2427 : 45686 : unsigned int i;
2428 : 45686 : bool has_nop;
2429 : :
2430 : 45686 : res = gimple_phi_result (phi);
2431 : 91372 : if (virtual_operand_p (res))
2432 : 40352 : return;
2433 : :
2434 : 45686 : if ((rhs = degenerate_phi_result (phi))
2435 : 45686 : || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
2436 : : res))
2437 : 45641 : && !chrec_contains_undetermined (scev)
2438 : 45641 : && scev != res
2439 : 12 : && (rhs = gimple_phi_arg_def (phi, 0))))
2440 : : {
2441 : 57 : if (dump_file && (dump_flags & TDF_DETAILS))
2442 : : {
2443 : 0 : fprintf (dump_file, "Degenerate phi!\n");
2444 : 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
2445 : : }
2446 : 57 : new_stmt = gimple_build_assign (res, rhs);
2447 : 57 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2448 : 57 : update_stmt (new_stmt);
2449 : 57 : return;
2450 : : }
2451 : :
2452 : 45629 : bb = gimple_bb (phi);
2453 : : /* Keep track of conditionals already seen. */
2454 : 45629 : scalar_cond_masked_set_type cond_set;
2455 : 45629 : if (EDGE_COUNT (bb->preds) == 2)
2456 : : {
2457 : : /* Predicate ordinary PHI node with 2 arguments. */
2458 : 40295 : edge first_edge, second_edge;
2459 : 40295 : basic_block true_bb;
2460 : 40295 : first_edge = EDGE_PRED (bb, 0);
2461 : 40295 : second_edge = EDGE_PRED (bb, 1);
2462 : 40295 : cond = bb_predicate (first_edge->src);
2463 : 40295 : cond_set.add ({ cond, 1 });
2464 : 40295 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2465 : 18809 : std::swap (first_edge, second_edge);
2466 : 40295 : if (EDGE_COUNT (first_edge->src->succs) > 1)
2467 : : {
2468 : 19514 : cond = bb_predicate (second_edge->src);
2469 : 19514 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2470 : 10250 : cond = TREE_OPERAND (cond, 0);
2471 : : else
2472 : : first_edge = second_edge;
2473 : : }
2474 : : else
2475 : 20781 : cond = bb_predicate (first_edge->src);
2476 : :
2477 : : /* Gimplify the condition to a valid cond-expr conditonal operand. */
2478 : 40295 : cond = gen_simplified_condition (cond, cond_set);
2479 : 40295 : cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
2480 : : NULL_TREE, true, GSI_SAME_STMT);
2481 : 40295 : true_bb = first_edge->src;
2482 : 40295 : if (EDGE_PRED (bb, 1)->src == true_bb)
2483 : : {
2484 : 28073 : arg0 = gimple_phi_arg_def (phi, 1);
2485 : 28073 : arg1 = gimple_phi_arg_def (phi, 0);
2486 : : }
2487 : : else
2488 : : {
2489 : 12222 : arg0 = gimple_phi_arg_def (phi, 0);
2490 : 12222 : arg1 = gimple_phi_arg_def (phi, 1);
2491 : : }
2492 : :
2493 : : /* Factor out operand if possible. This can only be done easily
2494 : : for PHI with 2 elements. */
2495 : 40295 : factor_out_operators (&res, gsi, &arg0, &arg1, phi);
2496 : :
2497 : 40295 : if (is_cond_scalar_reduction (phi, &reduc, arg0, arg1,
2498 : : &op0, &op1, false, &has_nop,
2499 : : &nop_reduc))
2500 : : {
2501 : : /* Convert reduction stmt into vectorizable form. */
2502 : 8976 : rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
2503 : 4488 : true_bb != gimple_bb (reduc),
2504 : : has_nop, nop_reduc,
2505 : : loop_versioned);
2506 : 4488 : redundant_ssa_names.safe_push (std::make_pair (res, rhs));
2507 : : }
2508 : : else
2509 : : /* Build new RHS using selected condition and arguments. */
2510 : 35807 : rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
2511 : : arg0, arg1);
2512 : 40295 : new_stmt = gimple_build_assign (res, rhs);
2513 : 40295 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2514 : 40295 : gimple_stmt_iterator new_gsi = gsi_for_stmt (new_stmt);
2515 : 40295 : if (fold_stmt (&new_gsi, follow_all_ssa_edges))
2516 : : {
2517 : 1515 : new_stmt = gsi_stmt (new_gsi);
2518 : 1515 : update_stmt (new_stmt);
2519 : : }
2520 : :
2521 : 40295 : if (dump_file && (dump_flags & TDF_DETAILS))
2522 : : {
2523 : 19 : fprintf (dump_file, "new phi replacement stmt\n");
2524 : 19 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
2525 : : }
2526 : 40295 : return;
2527 : : }
2528 : :
2529 : : /* Create hashmap for PHI node which contain vector of argument indexes
2530 : : having the same value. */
2531 : 5334 : bool swap = false;
2532 : 5334 : hash_map<tree_operand_hash, auto_vec<int> > phi_arg_map;
2533 : 5334 : unsigned int num_args = gimple_phi_num_args (phi);
2534 : : /* Vector of different PHI argument values. */
2535 : 5334 : auto_vec<ifcvt_arg_entry_t> args;
2536 : :
2537 : : /* Compute phi_arg_map, determine the list of unique PHI args and the indices
2538 : : where they are in the PHI node. The indices will be used to determine
2539 : : the conditions to apply and their complexity. */
2540 : 21595 : for (i = 0; i < num_args; i++)
2541 : : {
2542 : 16261 : tree arg;
2543 : :
2544 : 16261 : arg = gimple_phi_arg_def (phi, i);
2545 : 16261 : if (!phi_arg_map.get (arg))
2546 : 12786 : args.safe_push ({ arg, 0, 0, NULL });
2547 : 16261 : phi_arg_map.get_or_insert (arg).safe_push (i);
2548 : : }
2549 : :
2550 : : /* Determine element with max number of occurrences and complexity. Looking
2551 : : at only number of occurrences as a measure for complexity isn't enough as
2552 : : all usages can be unique but the comparisons to reach the PHI node differ
2553 : : per branch. */
2554 : 18120 : for (unsigned i = 0; i < args.length (); i++)
2555 : : {
2556 : 12786 : unsigned int len = 0;
2557 : 12786 : vec<int> *indices = phi_arg_map.get (args[i].arg);
2558 : 54619 : for (int index : *indices)
2559 : : {
2560 : 16261 : edge e = gimple_phi_arg_edge (phi, index);
2561 : 16261 : len += get_bb_num_predicate_stmts (e->src);
2562 : : }
2563 : :
2564 : 12786 : unsigned occur = indices->length ();
2565 : 12786 : if (dump_file && (dump_flags & TDF_DETAILS))
2566 : 7 : fprintf (dump_file, "Ranking %d as len=%d, idx=%d\n", i, len, occur);
2567 : 12786 : args[i].num_compares = len;
2568 : 12786 : args[i].occurs = occur;
2569 : 12786 : args[i].indexes = indices;
2570 : : }
2571 : :
2572 : : /* Sort elements based on rankings ARGS. */
2573 : 5334 : args.stablesort (cmp_arg_entry, NULL);
2574 : :
2575 : : /* Handle one special case when number of arguments with different values
2576 : : is equal 2 and one argument has the only occurrence. Such PHI can be
2577 : : handled as if would have only 2 arguments. */
2578 : 5334 : if (args.length () == 2
2579 : 8613 : && args[0].indexes->length () == 1)
2580 : : {
2581 : 3212 : index0 = (*args[0].indexes)[0];
2582 : 3212 : arg0 = args[0].arg;
2583 : 3212 : arg1 = args[1].arg;
2584 : 3212 : e = gimple_phi_arg_edge (phi, index0);
2585 : 3212 : cond = bb_predicate (e->src);
2586 : 3212 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2587 : : {
2588 : 39 : swap = true;
2589 : 39 : cond = TREE_OPERAND (cond, 0);
2590 : : }
2591 : : /* Gimplify the condition to a valid cond-expr conditonal operand. */
2592 : 3212 : cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
2593 : : NULL_TREE, true, GSI_SAME_STMT);
2594 : 3212 : if (!(is_cond_scalar_reduction (phi, &reduc, arg0 , arg1,
2595 : : &op0, &op1, true, &has_nop, &nop_reduc)))
2596 : 4509 : rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
2597 : : swap ? arg1 : arg0,
2598 : : swap ? arg0 : arg1);
2599 : : else
2600 : : {
2601 : : /* Convert reduction stmt into vectorizable form. */
2602 : 939 : rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
2603 : : swap, has_nop, nop_reduc,
2604 : : loop_versioned);
2605 : 939 : redundant_ssa_names.safe_push (std::make_pair (res, rhs));
2606 : : }
2607 : 3212 : new_stmt = gimple_build_assign (res, rhs);
2608 : 3212 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2609 : 3212 : update_stmt (new_stmt);
2610 : : }
2611 : : else
2612 : : {
2613 : : /* Common case. */
2614 : 2122 : tree type = TREE_TYPE (gimple_phi_result (phi));
2615 : 2122 : gen_phi_nest_statement (phi, gsi, cond_set, type, &new_stmt, res,
2616 : : args, 1);
2617 : : }
2618 : :
2619 : 5334 : if (dump_file && (dump_flags & TDF_DETAILS))
2620 : : {
2621 : 3 : fprintf (dump_file, "new extended phi replacement stmt\n");
2622 : 3 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
2623 : : }
2624 : 45629 : }
2625 : :
2626 : : /* Replaces in LOOP all the scalar phi nodes other than those in the
2627 : : LOOP->header block with conditional modify expressions.
2628 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
2629 : : vectorization. */
2630 : :
2631 : : static void
2632 : 26444 : predicate_all_scalar_phis (class loop *loop, bool loop_versioned)
2633 : : {
2634 : 26444 : basic_block bb;
2635 : 26444 : unsigned int orig_loop_num_nodes = loop->num_nodes;
2636 : 26444 : unsigned int i;
2637 : :
2638 : 135793 : for (i = 1; i < orig_loop_num_nodes; i++)
2639 : : {
2640 : 109349 : gphi *phi;
2641 : 109349 : gimple_stmt_iterator gsi;
2642 : 109349 : gphi_iterator phi_gsi;
2643 : 109349 : bb = ifc_bbs[i];
2644 : :
2645 : 109349 : if (bb == loop->header)
2646 : 78589 : continue;
2647 : :
2648 : 109349 : phi_gsi = gsi_start_phis (bb);
2649 : 109349 : if (gsi_end_p (phi_gsi))
2650 : 78589 : continue;
2651 : :
2652 : 30760 : gsi = gsi_after_labels (bb);
2653 : 78696 : while (!gsi_end_p (phi_gsi))
2654 : : {
2655 : 47936 : phi = phi_gsi.phi ();
2656 : 95872 : if (virtual_operand_p (gimple_phi_result (phi)))
2657 : 2250 : gsi_next (&phi_gsi);
2658 : : else
2659 : : {
2660 : 45686 : predicate_scalar_phi (phi, &gsi, loop_versioned);
2661 : 45686 : remove_phi_node (&phi_gsi, false);
2662 : : }
2663 : : }
2664 : : }
2665 : 26444 : }
2666 : :
2667 : : /* Insert in each basic block of LOOP the statements produced by the
2668 : : gimplification of the predicates. */
2669 : :
2670 : : static void
2671 : 26444 : insert_gimplified_predicates (loop_p loop)
2672 : : {
2673 : 26444 : unsigned int i;
2674 : :
2675 : 162237 : for (i = 0; i < loop->num_nodes; i++)
2676 : : {
2677 : 135793 : basic_block bb = ifc_bbs[i];
2678 : 135793 : gimple_seq stmts;
2679 : 135793 : if (!is_predicated (bb))
2680 : 82789 : gcc_assert (bb_predicate_gimplified_stmts (bb) == NULL);
2681 : 135793 : if (!is_predicated (bb))
2682 : : {
2683 : : /* Do not insert statements for a basic block that is not
2684 : : predicated. Also make sure that the predicate of the
2685 : : basic block is set to true. */
2686 : 82789 : reset_bb_predicate (bb);
2687 : 82789 : continue;
2688 : : }
2689 : :
2690 : 53004 : stmts = bb_predicate_gimplified_stmts (bb);
2691 : 53004 : if (stmts)
2692 : : {
2693 : 52670 : if (need_to_predicate)
2694 : : {
2695 : : /* Insert the predicate of the BB just after the label,
2696 : : as the if-conversion of memory writes will use this
2697 : : predicate. */
2698 : 5552 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
2699 : 5552 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
2700 : : }
2701 : : else
2702 : : {
2703 : : /* Insert the predicate of the BB at the end of the BB
2704 : : as this would reduce the register pressure: the only
2705 : : use of this predicate will be in successor BBs. */
2706 : 47118 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2707 : :
2708 : 47118 : if (gsi_end_p (gsi)
2709 : 47118 : || stmt_ends_bb_p (gsi_stmt (gsi)))
2710 : 28387 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
2711 : : else
2712 : 18731 : gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
2713 : : }
2714 : :
2715 : : /* Once the sequence is code generated, set it to NULL. */
2716 : 52670 : set_bb_predicate_gimplified_stmts (bb, NULL, true);
2717 : : }
2718 : : }
2719 : 26444 : }
2720 : :
2721 : : /* Helper function for predicate_statements. Returns index of existent
2722 : : mask if it was created for given SIZE and -1 otherwise. */
2723 : :
2724 : : static int
2725 : 911 : mask_exists (int size, const vec<int> &vec)
2726 : : {
2727 : 911 : unsigned int ix;
2728 : 911 : int v;
2729 : 1000 : FOR_EACH_VEC_ELT (vec, ix, v)
2730 : 939 : if (v == size)
2731 : 850 : return (int) ix;
2732 : : return -1;
2733 : : }
2734 : :
2735 : : /* Helper function for predicate_statements. STMT is a memory read or
2736 : : write and it needs to be predicated by MASK. Return a statement
2737 : : that does so. */
2738 : :
2739 : : static gimple *
2740 : 1884 : predicate_load_or_store (gimple_stmt_iterator *gsi, gassign *stmt, tree mask)
2741 : : {
2742 : 1884 : gcall *new_stmt;
2743 : :
2744 : 1884 : tree lhs = gimple_assign_lhs (stmt);
2745 : 1884 : tree rhs = gimple_assign_rhs1 (stmt);
2746 : 1884 : tree ref = TREE_CODE (lhs) == SSA_NAME ? rhs : lhs;
2747 : 1884 : mark_addressable (ref);
2748 : 1884 : tree addr = force_gimple_operand_gsi (gsi, build_fold_addr_expr (ref),
2749 : : true, NULL_TREE, true, GSI_SAME_STMT);
2750 : 1884 : tree ptr = build_int_cst (reference_alias_ptr_type (ref),
2751 : 1884 : get_object_alignment (ref));
2752 : : /* Copy points-to info if possible. */
2753 : 1884 : if (TREE_CODE (addr) == SSA_NAME && !SSA_NAME_PTR_INFO (addr))
2754 : 392 : copy_ref_info (build2 (MEM_REF, TREE_TYPE (ref), addr, ptr),
2755 : : ref);
2756 : 1884 : if (TREE_CODE (lhs) == SSA_NAME)
2757 : : {
2758 : : /* Get a zero else value. This might not be what a target actually uses
2759 : : but we cannot be sure about which vector mode the vectorizer will
2760 : : choose. Therefore, leave the decision whether we need to force the
2761 : : inactive elements to zero to the vectorizer. */
2762 : 1126 : tree els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
2763 : 1126 : TREE_TYPE (lhs));
2764 : :
2765 : 1126 : new_stmt
2766 : 1126 : = gimple_build_call_internal (IFN_MASK_LOAD, 4, addr,
2767 : : ptr, mask, els);
2768 : :
2769 : 1126 : gimple_call_set_lhs (new_stmt, lhs);
2770 : 2252 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2771 : : }
2772 : : else
2773 : : {
2774 : 758 : new_stmt
2775 : 758 : = gimple_build_call_internal (IFN_MASK_STORE, 4, addr, ptr,
2776 : : mask, rhs);
2777 : 758 : gimple_move_vops (new_stmt, stmt);
2778 : : }
2779 : 1884 : gimple_call_set_nothrow (new_stmt, true);
2780 : 1884 : return new_stmt;
2781 : : }
2782 : :
2783 : : /* STMT uses OP_LHS. Check whether it is equivalent to:
2784 : :
2785 : : ... = OP_MASK ? OP_LHS : X;
2786 : :
2787 : : Return X if so, otherwise return null. OP_MASK is an SSA_NAME that is
2788 : : known to have value OP_COND. */
2789 : :
2790 : : static tree
2791 : 714 : check_redundant_cond_expr (gimple *stmt, tree op_mask, tree op_cond,
2792 : : tree op_lhs)
2793 : : {
2794 : 1401 : gassign *assign = dyn_cast <gassign *> (stmt);
2795 : 922 : if (!assign || gimple_assign_rhs_code (assign) != COND_EXPR)
2796 : : return NULL_TREE;
2797 : :
2798 : 97 : tree use_cond = gimple_assign_rhs1 (assign);
2799 : 97 : tree if_true = gimple_assign_rhs2 (assign);
2800 : 97 : tree if_false = gimple_assign_rhs3 (assign);
2801 : :
2802 : 74 : if ((use_cond == op_mask || operand_equal_p (use_cond, op_cond, 0))
2803 : 97 : && if_true == op_lhs)
2804 : : return if_false;
2805 : :
2806 : 74 : if (inverse_conditions_p (use_cond, op_cond) && if_false == op_lhs)
2807 : : return if_true;
2808 : :
2809 : : return NULL_TREE;
2810 : : }
2811 : :
2812 : : /* Return true if VALUE is available for use at STMT. SSA_NAMES is
2813 : : the set of SSA names defined earlier in STMT's block. */
2814 : :
2815 : : static bool
2816 : 23 : value_available_p (gimple *stmt, hash_set<tree_ssa_name_hash> *ssa_names,
2817 : : tree value)
2818 : : {
2819 : 23 : if (is_gimple_min_invariant (value))
2820 : : return true;
2821 : :
2822 : 23 : if (TREE_CODE (value) == SSA_NAME)
2823 : : {
2824 : 23 : if (SSA_NAME_IS_DEFAULT_DEF (value))
2825 : : return true;
2826 : :
2827 : 23 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
2828 : 23 : basic_block use_bb = gimple_bb (stmt);
2829 : 23 : return (def_bb == use_bb
2830 : 23 : ? ssa_names->contains (value)
2831 : 23 : : dominated_by_p (CDI_DOMINATORS, use_bb, def_bb));
2832 : : }
2833 : :
2834 : : return false;
2835 : : }
2836 : :
2837 : : /* Helper function for predicate_statements. STMT is a potentially-trapping
2838 : : arithmetic operation that needs to be predicated by MASK, an SSA_NAME that
2839 : : has value COND. Return a statement that does so. SSA_NAMES is the set of
2840 : : SSA names defined earlier in STMT's block. */
2841 : :
2842 : : static gimple *
2843 : 509 : predicate_rhs_code (gassign *stmt, tree mask, tree cond,
2844 : : hash_set<tree_ssa_name_hash> *ssa_names)
2845 : : {
2846 : 509 : tree lhs = gimple_assign_lhs (stmt);
2847 : 509 : tree_code code = gimple_assign_rhs_code (stmt);
2848 : 509 : unsigned int nops = gimple_num_ops (stmt);
2849 : 509 : internal_fn cond_fn = get_conditional_internal_fn (code);
2850 : :
2851 : : /* Construct the arguments to the conditional internal function. */
2852 : 509 : auto_vec<tree, 8> args;
2853 : 509 : args.safe_grow (nops + 1, true);
2854 : 509 : args[0] = mask;
2855 : 1527 : for (unsigned int i = 1; i < nops; ++i)
2856 : 1018 : args[i] = gimple_op (stmt, i);
2857 : 509 : args[nops] = NULL_TREE;
2858 : :
2859 : : /* Look for uses of the result to see whether they are COND_EXPRs that can
2860 : : be folded into the conditional call. */
2861 : 509 : imm_use_iterator imm_iter;
2862 : 509 : gimple *use_stmt;
2863 : 1732 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
2864 : : {
2865 : 714 : tree new_else = check_redundant_cond_expr (use_stmt, mask, cond, lhs);
2866 : 714 : if (new_else && value_available_p (stmt, ssa_names, new_else))
2867 : : {
2868 : 2 : if (!args[nops])
2869 : 2 : args[nops] = new_else;
2870 : 2 : if (operand_equal_p (new_else, args[nops], 0))
2871 : : {
2872 : : /* We have:
2873 : :
2874 : : LHS = IFN_COND (MASK, ..., ELSE);
2875 : : X = MASK ? LHS : ELSE;
2876 : :
2877 : : which makes X equivalent to LHS. */
2878 : 2 : tree use_lhs = gimple_assign_lhs (use_stmt);
2879 : 2 : redundant_ssa_names.safe_push (std::make_pair (use_lhs, lhs));
2880 : : }
2881 : : }
2882 : 509 : }
2883 : 509 : if (!args[nops])
2884 : 1014 : args[nops] = targetm.preferred_else_value (cond_fn, TREE_TYPE (lhs),
2885 : 507 : nops - 1, &args[1]);
2886 : :
2887 : : /* Create and insert the call. */
2888 : 509 : gcall *new_stmt = gimple_build_call_internal_vec (cond_fn, args);
2889 : 509 : gimple_call_set_lhs (new_stmt, lhs);
2890 : 509 : gimple_call_set_nothrow (new_stmt, true);
2891 : :
2892 : 509 : return new_stmt;
2893 : 509 : }
2894 : :
2895 : : /* Predicate each write to memory in LOOP.
2896 : :
2897 : : This function transforms control flow constructs containing memory
2898 : : writes of the form:
2899 : :
2900 : : | for (i = 0; i < N; i++)
2901 : : | if (cond)
2902 : : | A[i] = expr;
2903 : :
2904 : : into the following form that does not contain control flow:
2905 : :
2906 : : | for (i = 0; i < N; i++)
2907 : : | A[i] = cond ? expr : A[i];
2908 : :
2909 : : The original CFG looks like this:
2910 : :
2911 : : | bb_0
2912 : : | i = 0
2913 : : | end_bb_0
2914 : : |
2915 : : | bb_1
2916 : : | if (i < N) goto bb_5 else goto bb_2
2917 : : | end_bb_1
2918 : : |
2919 : : | bb_2
2920 : : | cond = some_computation;
2921 : : | if (cond) goto bb_3 else goto bb_4
2922 : : | end_bb_2
2923 : : |
2924 : : | bb_3
2925 : : | A[i] = expr;
2926 : : | goto bb_4
2927 : : | end_bb_3
2928 : : |
2929 : : | bb_4
2930 : : | goto bb_1
2931 : : | end_bb_4
2932 : :
2933 : : insert_gimplified_predicates inserts the computation of the COND
2934 : : expression at the beginning of the destination basic block:
2935 : :
2936 : : | bb_0
2937 : : | i = 0
2938 : : | end_bb_0
2939 : : |
2940 : : | bb_1
2941 : : | if (i < N) goto bb_5 else goto bb_2
2942 : : | end_bb_1
2943 : : |
2944 : : | bb_2
2945 : : | cond = some_computation;
2946 : : | if (cond) goto bb_3 else goto bb_4
2947 : : | end_bb_2
2948 : : |
2949 : : | bb_3
2950 : : | cond = some_computation;
2951 : : | A[i] = expr;
2952 : : | goto bb_4
2953 : : | end_bb_3
2954 : : |
2955 : : | bb_4
2956 : : | goto bb_1
2957 : : | end_bb_4
2958 : :
2959 : : predicate_statements is then predicating the memory write as follows:
2960 : :
2961 : : | bb_0
2962 : : | i = 0
2963 : : | end_bb_0
2964 : : |
2965 : : | bb_1
2966 : : | if (i < N) goto bb_5 else goto bb_2
2967 : : | end_bb_1
2968 : : |
2969 : : | bb_2
2970 : : | if (cond) goto bb_3 else goto bb_4
2971 : : | end_bb_2
2972 : : |
2973 : : | bb_3
2974 : : | cond = some_computation;
2975 : : | A[i] = cond ? expr : A[i];
2976 : : | goto bb_4
2977 : : | end_bb_3
2978 : : |
2979 : : | bb_4
2980 : : | goto bb_1
2981 : : | end_bb_4
2982 : :
2983 : : and finally combine_blocks removes the basic block boundaries making
2984 : : the loop vectorizable:
2985 : :
2986 : : | bb_0
2987 : : | i = 0
2988 : : | if (i < N) goto bb_5 else goto bb_1
2989 : : | end_bb_0
2990 : : |
2991 : : | bb_1
2992 : : | cond = some_computation;
2993 : : | A[i] = cond ? expr : A[i];
2994 : : | if (i < N) goto bb_5 else goto bb_4
2995 : : | end_bb_1
2996 : : |
2997 : : | bb_4
2998 : : | goto bb_1
2999 : : | end_bb_4
3000 : : */
3001 : :
3002 : : static void
3003 : 9086 : predicate_statements (loop_p loop)
3004 : : {
3005 : 9086 : unsigned int i, orig_loop_num_nodes = loop->num_nodes;
3006 : 9086 : auto_vec<int, 1> vect_sizes;
3007 : 9086 : auto_vec<tree, 1> vect_masks;
3008 : 9086 : hash_set<tree_ssa_name_hash> ssa_names;
3009 : :
3010 : 48882 : for (i = 1; i < orig_loop_num_nodes; i++)
3011 : : {
3012 : 39796 : gimple_stmt_iterator gsi;
3013 : 39796 : basic_block bb = ifc_bbs[i];
3014 : 39796 : tree cond = bb_predicate (bb);
3015 : 39796 : bool swap;
3016 : 39796 : int index;
3017 : :
3018 : 39796 : if (is_true_predicate (cond))
3019 : 18991 : continue;
3020 : :
3021 : 20805 : swap = false;
3022 : 20805 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
3023 : : {
3024 : 7571 : swap = true;
3025 : 7571 : cond = TREE_OPERAND (cond, 0);
3026 : : }
3027 : :
3028 : 20805 : vect_sizes.truncate (0);
3029 : 20805 : vect_masks.truncate (0);
3030 : :
3031 : 135109 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
3032 : : {
3033 : 93499 : gassign *stmt = dyn_cast <gassign *> (gsi_stmt (gsi));
3034 : 63122 : if (!stmt)
3035 : : ;
3036 : 63122 : else if (is_false_predicate (cond)
3037 : 63122 : && gimple_vdef (stmt))
3038 : : {
3039 : 0 : unlink_stmt_vdef (stmt);
3040 : 0 : gsi_remove (&gsi, true);
3041 : 0 : release_defs (stmt);
3042 : 0 : continue;
3043 : : }
3044 : 63122 : else if (gimple_plf (stmt, GF_PLF_2)
3045 : 63122 : && is_gimple_assign (stmt))
3046 : : {
3047 : 2393 : tree lhs = gimple_assign_lhs (stmt);
3048 : 2393 : tree mask;
3049 : 2393 : gimple *new_stmt;
3050 : 2393 : gimple_seq stmts = NULL;
3051 : 2393 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
3052 : : /* We checked before setting GF_PLF_2 that an equivalent
3053 : : integer mode exists. */
3054 : 2393 : int bitsize = GET_MODE_BITSIZE (mode).to_constant ();
3055 : 2393 : if (!vect_sizes.is_empty ()
3056 : 911 : && (index = mask_exists (bitsize, vect_sizes)) != -1)
3057 : : /* Use created mask. */
3058 : 850 : mask = vect_masks[index];
3059 : : else
3060 : : {
3061 : 1543 : if (COMPARISON_CLASS_P (cond))
3062 : 0 : mask = gimple_build (&stmts, TREE_CODE (cond),
3063 : : boolean_type_node,
3064 : 0 : TREE_OPERAND (cond, 0),
3065 : 0 : TREE_OPERAND (cond, 1));
3066 : : else
3067 : 1543 : mask = cond;
3068 : :
3069 : 1543 : if (swap)
3070 : : {
3071 : 455 : tree true_val
3072 : 455 : = constant_boolean_node (true, TREE_TYPE (mask));
3073 : 455 : mask = gimple_build (&stmts, BIT_XOR_EXPR,
3074 : 455 : TREE_TYPE (mask), mask, true_val);
3075 : : }
3076 : 1543 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3077 : :
3078 : : /* Save mask and its size for further use. */
3079 : 1543 : vect_sizes.safe_push (bitsize);
3080 : 1543 : vect_masks.safe_push (mask);
3081 : : }
3082 : 2393 : if (gimple_assign_single_p (stmt))
3083 : 1884 : new_stmt = predicate_load_or_store (&gsi, stmt, mask);
3084 : : else
3085 : 509 : new_stmt = predicate_rhs_code (stmt, mask, cond, &ssa_names);
3086 : :
3087 : 2393 : gsi_replace (&gsi, new_stmt, true);
3088 : : }
3089 : 60729 : else if (gimple_needing_rewrite_undefined (stmt))
3090 : 9397 : rewrite_to_defined_unconditional (&gsi);
3091 : 102664 : else if (gimple_vdef (stmt))
3092 : : {
3093 : 1563 : tree lhs = gimple_assign_lhs (stmt);
3094 : 1563 : tree rhs = gimple_assign_rhs1 (stmt);
3095 : 1563 : tree type = TREE_TYPE (lhs);
3096 : :
3097 : 1563 : lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
3098 : 1563 : rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
3099 : 1563 : if (swap)
3100 : 559 : std::swap (lhs, rhs);
3101 : 1563 : cond = force_gimple_operand_gsi (&gsi, unshare_expr (cond), true,
3102 : : NULL_TREE, true, GSI_SAME_STMT);
3103 : 1563 : rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
3104 : 1563 : gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
3105 : 1563 : update_stmt (stmt);
3106 : : }
3107 : :
3108 : 93499 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_2)
3109 : 93499 : && is_gimple_call (gsi_stmt (gsi)))
3110 : : {
3111 : : /* Convert functions that have a SIMD clone to IFN_MASK_CALL.
3112 : : This will cause the vectorizer to match the "in branch"
3113 : : clone variants, and serves to build the mask vector
3114 : : in a natural way. */
3115 : 998 : tree mask = cond;
3116 : 998 : gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi));
3117 : 998 : tree orig_fn = gimple_call_fn (call);
3118 : 998 : int orig_nargs = gimple_call_num_args (call);
3119 : 998 : auto_vec<tree> args;
3120 : 998 : args.safe_push (orig_fn);
3121 : 2011 : for (int i = 0; i < orig_nargs; i++)
3122 : 1013 : args.safe_push (gimple_call_arg (call, i));
3123 : : /* If `swap', we invert the mask used for the if branch for use
3124 : : when masking the function call. */
3125 : 998 : if (swap)
3126 : : {
3127 : 948 : gimple_seq stmts = NULL;
3128 : 948 : tree true_val
3129 : 948 : = constant_boolean_node (true, TREE_TYPE (mask));
3130 : 948 : mask = gimple_build (&stmts, BIT_XOR_EXPR,
3131 : 948 : TREE_TYPE (mask), mask, true_val);
3132 : 948 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3133 : : }
3134 : 998 : args.safe_push (mask);
3135 : :
3136 : : /* Replace the call with a IFN_MASK_CALL that has the extra
3137 : : condition parameter. */
3138 : 998 : gcall *new_call = gimple_build_call_internal_vec (IFN_MASK_CALL,
3139 : : args);
3140 : 998 : gimple_call_set_lhs (new_call, gimple_call_lhs (call));
3141 : 998 : gsi_replace (&gsi, new_call, true);
3142 : 998 : }
3143 : :
3144 : 93499 : tree lhs = gimple_get_lhs (gsi_stmt (gsi));
3145 : 93499 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
3146 : 60885 : ssa_names.add (lhs);
3147 : 93499 : gsi_next (&gsi);
3148 : : }
3149 : 41583 : ssa_names.empty ();
3150 : : }
3151 : 9086 : }
3152 : :
3153 : : /* Remove all GIMPLE_CONDs and GIMPLE_LABELs and GIMPLE_SWITCH of all
3154 : : the basic blocks other than the exit and latch of the LOOP. Also
3155 : : resets the GIMPLE_DEBUG information. */
3156 : :
3157 : : static void
3158 : 26444 : remove_conditions_and_labels (loop_p loop)
3159 : : {
3160 : 26444 : gimple_stmt_iterator gsi;
3161 : 26444 : unsigned int i;
3162 : :
3163 : 162237 : for (i = 0; i < loop->num_nodes; i++)
3164 : : {
3165 : 135793 : basic_block bb = ifc_bbs[i];
3166 : :
3167 : 135793 : if (bb_with_exit_edge_p (loop, bb)
3168 : 135793 : || bb == loop->latch)
3169 : 52888 : continue;
3170 : :
3171 : 574841 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
3172 : 409031 : switch (gimple_code (gsi_stmt (gsi)))
3173 : : {
3174 : 33797 : case GIMPLE_COND:
3175 : 33797 : case GIMPLE_LABEL:
3176 : 33797 : case GIMPLE_SWITCH:
3177 : 33797 : gsi_remove (&gsi, true);
3178 : 33797 : break;
3179 : :
3180 : 207725 : case GIMPLE_DEBUG:
3181 : : /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
3182 : 207725 : if (gimple_debug_bind_p (gsi_stmt (gsi)))
3183 : : {
3184 : 158022 : gimple_debug_bind_reset_value (gsi_stmt (gsi));
3185 : 158022 : update_stmt (gsi_stmt (gsi));
3186 : : }
3187 : 207725 : gsi_next (&gsi);
3188 : 207725 : break;
3189 : :
3190 : 167509 : default:
3191 : 167509 : gsi_next (&gsi);
3192 : : }
3193 : : }
3194 : 26444 : }
3195 : :
3196 : : /* Combine all the basic blocks from LOOP into one or two super basic
3197 : : blocks. Replace PHI nodes with conditional modify expressions.
3198 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
3199 : : vectorization. */
3200 : :
3201 : : static void
3202 : 26444 : combine_blocks (class loop *loop, bool loop_versioned)
3203 : : {
3204 : 26444 : basic_block bb, exit_bb, merge_target_bb;
3205 : 26444 : unsigned int orig_loop_num_nodes = loop->num_nodes;
3206 : 26444 : unsigned int i;
3207 : 26444 : edge e;
3208 : 26444 : edge_iterator ei;
3209 : :
3210 : : /* Reset flow-sensitive info before predicating stmts or PHIs we
3211 : : might fold. */
3212 : 162237 : for (i = 0; i < orig_loop_num_nodes; i++)
3213 : : {
3214 : 135793 : bb = ifc_bbs[i];
3215 : 135793 : if (is_predicated (bb))
3216 : : {
3217 : 53004 : for (auto gsi = gsi_start_phis (bb);
3218 : 54322 : !gsi_end_p (gsi); gsi_next (&gsi))
3219 : 1318 : reset_flow_sensitive_info (gimple_phi_result (*gsi));
3220 : 189626 : for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3221 : : {
3222 : 83618 : gimple *stmt = gsi_stmt (gsi);
3223 : 83618 : ssa_op_iter i;
3224 : 83618 : tree op;
3225 : 124037 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
3226 : 40419 : reset_flow_sensitive_info (op);
3227 : : }
3228 : : }
3229 : : }
3230 : :
3231 : 26444 : remove_conditions_and_labels (loop);
3232 : 26444 : insert_gimplified_predicates (loop);
3233 : 26444 : predicate_all_scalar_phis (loop, loop_versioned);
3234 : :
3235 : 26444 : if (need_to_predicate || need_to_rewrite_undefined)
3236 : 9086 : predicate_statements (loop);
3237 : :
3238 : : /* Merge basic blocks. */
3239 : 26444 : exit_bb = single_exit (loop)->src;
3240 : 26444 : gcc_assert (exit_bb != loop->latch);
3241 : 162237 : for (i = 0; i < orig_loop_num_nodes; i++)
3242 : : {
3243 : 135793 : bb = ifc_bbs[i];
3244 : 135793 : free_bb_predicate (bb);
3245 : : }
3246 : :
3247 : 26444 : merge_target_bb = loop->header;
3248 : :
3249 : : /* Get at the virtual def valid for uses starting at the first block
3250 : : we merge into the header. Without a virtual PHI the loop has the
3251 : : same virtual use on all stmts. */
3252 : 26444 : gphi *vphi = get_virtual_phi (loop->header);
3253 : 26444 : tree last_vdef = NULL_TREE;
3254 : 26444 : if (vphi)
3255 : : {
3256 : 14747 : last_vdef = gimple_phi_result (vphi);
3257 : 29494 : for (gimple_stmt_iterator gsi = gsi_start_bb (loop->header);
3258 : 163987 : ! gsi_end_p (gsi); gsi_next (&gsi))
3259 : 232601 : if (gimple_vdef (gsi_stmt (gsi)))
3260 : 5035 : last_vdef = gimple_vdef (gsi_stmt (gsi));
3261 : : }
3262 : 135793 : for (i = 1; i < orig_loop_num_nodes; i++)
3263 : : {
3264 : 109349 : gimple_stmt_iterator gsi;
3265 : 109349 : gimple_stmt_iterator last;
3266 : :
3267 : 109349 : bb = ifc_bbs[i];
3268 : :
3269 : 109349 : if (bb == exit_bb || bb == loop->latch)
3270 : 52888 : continue;
3271 : :
3272 : : /* We release virtual PHIs late because we have to propagate them
3273 : : out using the current VUSE. The def might be the one used
3274 : : after the loop. */
3275 : 56461 : vphi = get_virtual_phi (bb);
3276 : 56461 : if (vphi)
3277 : : {
3278 : : /* When there's just loads inside the loop a stray virtual
3279 : : PHI merging the uses can appear, update last_vdef from
3280 : : it. */
3281 : 499 : if (!last_vdef)
3282 : 0 : last_vdef = gimple_phi_arg_def (vphi, 0);
3283 : 499 : imm_use_iterator iter;
3284 : 499 : use_operand_p use_p;
3285 : 499 : gimple *use_stmt;
3286 : 2222 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
3287 : : {
3288 : 3674 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3289 : 1225 : SET_USE (use_p, last_vdef);
3290 : 499 : }
3291 : 499 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
3292 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
3293 : 499 : gsi = gsi_for_stmt (vphi);
3294 : 499 : remove_phi_node (&gsi, true);
3295 : : }
3296 : :
3297 : : /* Make stmts member of loop->header and clear range info from all stmts
3298 : : in BB which is now no longer executed conditional on a predicate we
3299 : : could have derived it from. */
3300 : 307755 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3301 : : {
3302 : 194833 : gimple *stmt = gsi_stmt (gsi);
3303 : 194833 : gimple_set_bb (stmt, merge_target_bb);
3304 : : /* Update virtual operands. */
3305 : 194833 : if (last_vdef)
3306 : : {
3307 : 141278 : use_operand_p use_p = ssa_vuse_operand (stmt);
3308 : 13911 : if (use_p
3309 : 13911 : && USE_FROM_PTR (use_p) != last_vdef)
3310 : 719 : SET_USE (use_p, last_vdef);
3311 : 450707 : if (gimple_vdef (stmt))
3312 : 194833 : last_vdef = gimple_vdef (stmt);
3313 : : }
3314 : : else
3315 : : /* If this is the first load we arrive at update last_vdef
3316 : : so we handle stray PHIs correctly. */
3317 : 229884 : last_vdef = gimple_vuse (stmt);
3318 : : }
3319 : :
3320 : : /* Update stmt list. */
3321 : 56461 : last = gsi_last_bb (merge_target_bb);
3322 : 112922 : gsi_insert_seq_after_without_update (&last, bb_seq (bb), GSI_NEW_STMT);
3323 : 56461 : set_bb_seq (bb, NULL);
3324 : : }
3325 : :
3326 : : /* Fixup virtual operands in the exit block. */
3327 : 26444 : if (exit_bb
3328 : 26444 : && exit_bb != loop->header)
3329 : : {
3330 : : /* We release virtual PHIs late because we have to propagate them
3331 : : out using the current VUSE. The def might be the one used
3332 : : after the loop. */
3333 : 26444 : vphi = get_virtual_phi (exit_bb);
3334 : 26444 : if (vphi)
3335 : : {
3336 : : /* When there's just loads inside the loop a stray virtual
3337 : : PHI merging the uses can appear, update last_vdef from
3338 : : it. */
3339 : 1751 : if (!last_vdef)
3340 : 0 : last_vdef = gimple_phi_arg_def (vphi, 0);
3341 : 1751 : imm_use_iterator iter;
3342 : 1751 : use_operand_p use_p;
3343 : 1751 : gimple *use_stmt;
3344 : 7025 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
3345 : : {
3346 : 10569 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3347 : 3523 : SET_USE (use_p, last_vdef);
3348 : 1751 : }
3349 : 1751 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
3350 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
3351 : 1751 : gimple_stmt_iterator gsi = gsi_for_stmt (vphi);
3352 : 1751 : remove_phi_node (&gsi, true);
3353 : : }
3354 : : }
3355 : :
3356 : : /* Now remove all the edges in the loop, except for those from the exit
3357 : : block and delete the blocks we elided. */
3358 : 135793 : for (i = 1; i < orig_loop_num_nodes; i++)
3359 : : {
3360 : 109349 : bb = ifc_bbs[i];
3361 : :
3362 : 252108 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
3363 : : {
3364 : 142759 : if (e->src == exit_bb)
3365 : 26444 : ei_next (&ei);
3366 : : else
3367 : 116315 : remove_edge (e);
3368 : : }
3369 : : }
3370 : 135793 : for (i = 1; i < orig_loop_num_nodes; i++)
3371 : : {
3372 : 109349 : bb = ifc_bbs[i];
3373 : :
3374 : 109349 : if (bb == exit_bb || bb == loop->latch)
3375 : 52888 : continue;
3376 : :
3377 : 56461 : delete_basic_block (bb);
3378 : : }
3379 : :
3380 : : /* Re-connect the exit block. */
3381 : 26444 : if (exit_bb != NULL)
3382 : : {
3383 : 26444 : if (exit_bb != loop->header)
3384 : : {
3385 : : /* Connect this node to loop header. */
3386 : 26444 : make_single_succ_edge (loop->header, exit_bb, EDGE_FALLTHRU);
3387 : 26444 : set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
3388 : : }
3389 : :
3390 : : /* Redirect non-exit edges to loop->latch. */
3391 : 79332 : FOR_EACH_EDGE (e, ei, exit_bb->succs)
3392 : : {
3393 : 52888 : if (!loop_exit_edge_p (loop, e))
3394 : 26444 : redirect_edge_and_branch (e, loop->latch);
3395 : : }
3396 : 26444 : set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
3397 : : }
3398 : : else
3399 : : {
3400 : : /* If the loop does not have an exit, reconnect header and latch. */
3401 : 0 : make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
3402 : 0 : set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
3403 : : }
3404 : :
3405 : : /* If possible, merge loop header to the block with the exit edge.
3406 : : This reduces the number of basic blocks to two, to please the
3407 : : vectorizer that handles only loops with two nodes. */
3408 : 26444 : if (exit_bb
3409 : 26444 : && exit_bb != loop->header)
3410 : : {
3411 : 26444 : if (can_merge_blocks_p (loop->header, exit_bb))
3412 : 26442 : merge_blocks (loop->header, exit_bb);
3413 : : }
3414 : :
3415 : 26444 : free (ifc_bbs);
3416 : 26444 : ifc_bbs = NULL;
3417 : 26444 : }
3418 : :
3419 : : /* Version LOOP before if-converting it; the original loop
3420 : : will be if-converted, the new copy of the loop will not,
3421 : : and the LOOP_VECTORIZED internal call will be guarding which
3422 : : loop to execute. The vectorizer pass will fold this
3423 : : internal call into either true or false.
3424 : :
3425 : : Note that this function intentionally invalidates profile. Both edges
3426 : : out of LOOP_VECTORIZED must have 100% probability so the profile remains
3427 : : consistent after the condition is folded in the vectorizer. */
3428 : :
3429 : : static class loop *
3430 : 26631 : version_loop_for_if_conversion (class loop *loop, vec<gimple *> *preds)
3431 : : {
3432 : 26631 : basic_block cond_bb;
3433 : 26631 : tree cond = make_ssa_name (boolean_type_node);
3434 : 26631 : class loop *new_loop;
3435 : 26631 : gimple *g;
3436 : 26631 : gimple_stmt_iterator gsi;
3437 : 26631 : unsigned int save_length = 0;
3438 : :
3439 : 26631 : g = gimple_build_call_internal (IFN_LOOP_VECTORIZED, 2,
3440 : 26631 : build_int_cst (integer_type_node, loop->num),
3441 : : integer_zero_node);
3442 : 26631 : gimple_call_set_lhs (g, cond);
3443 : :
3444 : 26631 : void **saved_preds = NULL;
3445 : 26631 : if (any_complicated_phi || need_to_predicate)
3446 : : {
3447 : : /* Save BB->aux around loop_version as that uses the same field. */
3448 : 3418 : save_length = loop->inner ? loop->inner->num_nodes : loop->num_nodes;
3449 : 3418 : saved_preds = XALLOCAVEC (void *, save_length);
3450 : 25686 : for (unsigned i = 0; i < save_length; i++)
3451 : 22268 : saved_preds[i] = ifc_bbs[i]->aux;
3452 : : }
3453 : :
3454 : 26631 : initialize_original_copy_tables ();
3455 : : /* At this point we invalidate profile consistency until IFN_LOOP_VECTORIZED
3456 : : is re-merged in the vectorizer. */
3457 : 26631 : new_loop = loop_version (loop, cond, &cond_bb,
3458 : : profile_probability::always (),
3459 : : profile_probability::always (),
3460 : : profile_probability::always (),
3461 : : profile_probability::always (), true);
3462 : 26631 : free_original_copy_tables ();
3463 : :
3464 : 26631 : if (any_complicated_phi || need_to_predicate)
3465 : 25686 : for (unsigned i = 0; i < save_length; i++)
3466 : 22268 : ifc_bbs[i]->aux = saved_preds[i];
3467 : :
3468 : 26631 : if (new_loop == NULL)
3469 : : return NULL;
3470 : :
3471 : 26631 : new_loop->dont_vectorize = true;
3472 : 26631 : new_loop->force_vectorize = false;
3473 : 26631 : gsi = gsi_last_bb (cond_bb);
3474 : 26631 : gimple_call_set_arg (g, 1, build_int_cst (integer_type_node, new_loop->num));
3475 : 26631 : if (preds)
3476 : 26631 : preds->safe_push (g);
3477 : 26631 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3478 : 26631 : update_ssa (TODO_update_ssa_no_phi);
3479 : 26631 : return new_loop;
3480 : : }
3481 : :
3482 : : /* Return true when LOOP satisfies the follow conditions that will
3483 : : allow it to be recognized by the vectorizer for outer-loop
3484 : : vectorization:
3485 : : - The loop is not the root node of the loop tree.
3486 : : - The loop has exactly one inner loop.
3487 : : - The loop has a single exit.
3488 : : - The loop header has a single successor, which is the inner
3489 : : loop header.
3490 : : - Each of the inner and outer loop latches have a single
3491 : : predecessor.
3492 : : - The loop exit block has a single predecessor, which is the
3493 : : inner loop's exit block. */
3494 : :
3495 : : static bool
3496 : 26631 : versionable_outer_loop_p (class loop *loop)
3497 : : {
3498 : 26631 : if (!loop_outer (loop)
3499 : 6551 : || loop->dont_vectorize
3500 : 5597 : || !loop->inner
3501 : 5597 : || loop->inner->next
3502 : 2674 : || !single_exit (loop)
3503 : 2197 : || !single_succ_p (loop->header)
3504 : 914 : || single_succ (loop->header) != loop->inner->header
3505 : 914 : || !single_pred_p (loop->latch)
3506 : 33182 : || !single_pred_p (loop->inner->latch))
3507 : 25717 : return false;
3508 : :
3509 : 914 : basic_block outer_exit = single_pred (loop->latch);
3510 : 914 : basic_block inner_exit = single_pred (loop->inner->latch);
3511 : :
3512 : 27538 : if (!single_pred_p (outer_exit) || single_pred (outer_exit) != inner_exit)
3513 : : return false;
3514 : :
3515 : 906 : if (dump_file)
3516 : 0 : fprintf (dump_file, "Found vectorizable outer loop for versioning\n");
3517 : :
3518 : : return true;
3519 : : }
3520 : :
3521 : : /* Performs splitting of critical edges. Skip splitting and return false
3522 : : if LOOP will not be converted because:
3523 : :
3524 : : - LOOP is not well formed.
3525 : : - LOOP has PHI with more than MAX_PHI_ARG_NUM arguments.
3526 : :
3527 : : Last restriction is valid only if AGGRESSIVE_IF_CONV is false. */
3528 : :
3529 : : static bool
3530 : 324830 : ifcvt_split_critical_edges (class loop *loop, bool aggressive_if_conv)
3531 : : {
3532 : 324830 : basic_block *body;
3533 : 324830 : basic_block bb;
3534 : 324830 : unsigned int num = loop->num_nodes;
3535 : 324830 : unsigned int i;
3536 : 324830 : edge e;
3537 : 324830 : edge_iterator ei;
3538 : 324830 : auto_vec<edge> critical_edges;
3539 : :
3540 : : /* Loop is not well formed. */
3541 : 324830 : if (loop->inner)
3542 : : return false;
3543 : :
3544 : 229086 : body = get_loop_body (loop);
3545 : 1820874 : for (i = 0; i < num; i++)
3546 : : {
3547 : 1368276 : bb = body[i];
3548 : 1368276 : if (!aggressive_if_conv
3549 : 1360037 : && phi_nodes (bb)
3550 : 1784786 : && EDGE_COUNT (bb->preds) > MAX_PHI_ARG_NUM)
3551 : : {
3552 : 5574 : if (dump_file && (dump_flags & TDF_DETAILS))
3553 : 0 : fprintf (dump_file,
3554 : : "BB %d has complicated PHI with more than %u args.\n",
3555 : : bb->index, MAX_PHI_ARG_NUM);
3556 : :
3557 : 5574 : free (body);
3558 : 5574 : return false;
3559 : : }
3560 : 1362702 : if (bb == loop->latch || bb_with_exit_edge_p (loop, bb))
3561 : 809891 : continue;
3562 : :
3563 : : /* Skip basic blocks not ending with conditional branch. */
3564 : 1314113 : if (!safe_is_a <gcond *> (*gsi_last_bb (bb))
3565 : 552811 : && !safe_is_a <gswitch *> (*gsi_last_bb (bb)))
3566 : 307798 : continue;
3567 : :
3568 : 736709 : FOR_EACH_EDGE (e, ei, bb->succs)
3569 : 602816 : if (EDGE_CRITICAL_P (e) && e->dest->loop_father == loop)
3570 : 111120 : critical_edges.safe_push (e);
3571 : : }
3572 : 223512 : free (body);
3573 : :
3574 : 432996 : while (critical_edges.length () > 0)
3575 : : {
3576 : 108166 : e = critical_edges.pop ();
3577 : : /* Don't split if bb can be predicated along non-critical edge. */
3578 : 108166 : if (EDGE_COUNT (e->dest->preds) > 2 || all_preds_critical_p (e->dest))
3579 : 51291 : split_edge (e);
3580 : : }
3581 : :
3582 : : return true;
3583 : 324830 : }
3584 : :
3585 : : /* Delete redundant statements produced by predication which prevents
3586 : : loop vectorization. */
3587 : :
3588 : : static void
3589 : 26680 : ifcvt_local_dce (class loop *loop)
3590 : : {
3591 : 26680 : gimple *stmt;
3592 : 26680 : gimple *stmt1;
3593 : 26680 : gimple *phi;
3594 : 26680 : gimple_stmt_iterator gsi;
3595 : 26680 : auto_vec<gimple *> worklist;
3596 : 26680 : enum gimple_code code;
3597 : 26680 : use_operand_p use_p;
3598 : 26680 : imm_use_iterator imm_iter;
3599 : :
3600 : : /* The loop has a single BB only. */
3601 : 26680 : basic_block bb = loop->header;
3602 : 26680 : tree latch_vdef = NULL_TREE;
3603 : :
3604 : 26680 : worklist.create (64);
3605 : : /* Consider all phi as live statements. */
3606 : 104041 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3607 : : {
3608 : 77361 : phi = gsi_stmt (gsi);
3609 : 77361 : gimple_set_plf (phi, GF_PLF_2, true);
3610 : 77361 : worklist.safe_push (phi);
3611 : 169638 : if (virtual_operand_p (gimple_phi_result (phi)))
3612 : 14916 : latch_vdef = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
3613 : : }
3614 : : /* Consider load/store statements, CALL and COND as live. */
3615 : 717617 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3616 : : {
3617 : 664257 : stmt = gsi_stmt (gsi);
3618 : 664257 : if (is_gimple_debug (stmt))
3619 : : {
3620 : 296164 : gimple_set_plf (stmt, GF_PLF_2, true);
3621 : 296164 : continue;
3622 : : }
3623 : 368093 : if (gimple_store_p (stmt) || gimple_assign_load_p (stmt))
3624 : : {
3625 : 73819 : gimple_set_plf (stmt, GF_PLF_2, true);
3626 : 73819 : worklist.safe_push (stmt);
3627 : 73819 : continue;
3628 : : }
3629 : 294274 : code = gimple_code (stmt);
3630 : 294274 : if (code == GIMPLE_COND || code == GIMPLE_CALL || code == GIMPLE_SWITCH)
3631 : : {
3632 : 32055 : gimple_set_plf (stmt, GF_PLF_2, true);
3633 : 32055 : worklist.safe_push (stmt);
3634 : 32055 : continue;
3635 : : }
3636 : 262219 : gimple_set_plf (stmt, GF_PLF_2, false);
3637 : :
3638 : 262219 : if (code == GIMPLE_ASSIGN)
3639 : : {
3640 : 262210 : tree lhs = gimple_assign_lhs (stmt);
3641 : 874697 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
3642 : : {
3643 : 367586 : stmt1 = USE_STMT (use_p);
3644 : 367586 : if (!is_gimple_debug (stmt1) && gimple_bb (stmt1) != bb)
3645 : : {
3646 : 17309 : gimple_set_plf (stmt, GF_PLF_2, true);
3647 : 17309 : worklist.safe_push (stmt);
3648 : 17309 : break;
3649 : : }
3650 : 262210 : }
3651 : : }
3652 : : }
3653 : : /* Propagate liveness through arguments of live stmt. */
3654 : 458403 : while (worklist.length () > 0)
3655 : : {
3656 : 431723 : ssa_op_iter iter;
3657 : 431723 : use_operand_p use_p;
3658 : 431723 : tree use;
3659 : :
3660 : 431723 : stmt = worklist.pop ();
3661 : 1525372 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3662 : : {
3663 : 661926 : use = USE_FROM_PTR (use_p);
3664 : 661926 : if (TREE_CODE (use) != SSA_NAME)
3665 : 40837 : continue;
3666 : 621089 : stmt1 = SSA_NAME_DEF_STMT (use);
3667 : 621089 : if (gimple_bb (stmt1) != bb || gimple_plf (stmt1, GF_PLF_2))
3668 : 389910 : continue;
3669 : 231179 : gimple_set_plf (stmt1, GF_PLF_2, true);
3670 : 231179 : worklist.safe_push (stmt1);
3671 : : }
3672 : : }
3673 : : /* Delete dead statements. */
3674 : 26680 : gsi = gsi_last_bb (bb);
3675 : 690937 : while (!gsi_end_p (gsi))
3676 : : {
3677 : 664257 : gimple_stmt_iterator gsiprev = gsi;
3678 : 664257 : gsi_prev (&gsiprev);
3679 : 664257 : stmt = gsi_stmt (gsi);
3680 : 664257 : if (!gimple_has_volatile_ops (stmt)
3681 : 664090 : && gimple_store_p (stmt)
3682 : 381802 : && gimple_vdef (stmt))
3683 : : {
3684 : 20197 : tree lhs = gimple_get_lhs (stmt);
3685 : 20197 : ao_ref write;
3686 : 20197 : ao_ref_init (&write, lhs);
3687 : :
3688 : 20197 : if (dse_classify_store (&write, stmt, false, NULL, NULL, latch_vdef)
3689 : : == DSE_STORE_DEAD)
3690 : 330 : delete_dead_or_redundant_assignment (&gsi, "dead");
3691 : 20197 : gsi = gsiprev;
3692 : 20197 : continue;
3693 : 20197 : }
3694 : :
3695 : 644060 : if (gimple_plf (stmt, GF_PLF_2))
3696 : : {
3697 : 630329 : gsi = gsiprev;
3698 : 630329 : continue;
3699 : : }
3700 : 13731 : if (dump_file && (dump_flags & TDF_DETAILS))
3701 : : {
3702 : 16 : fprintf (dump_file, "Delete dead stmt in bb#%d\n", bb->index);
3703 : 16 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3704 : : }
3705 : 13731 : gsi_remove (&gsi, true);
3706 : 13731 : release_defs (stmt);
3707 : 13731 : gsi = gsiprev;
3708 : : }
3709 : 26680 : }
3710 : :
3711 : : /* Return true if VALUE is already available on edge PE. */
3712 : :
3713 : : static bool
3714 : 225567 : ifcvt_available_on_edge_p (edge pe, tree value)
3715 : : {
3716 : 225567 : if (is_gimple_min_invariant (value))
3717 : : return true;
3718 : :
3719 : 219822 : if (TREE_CODE (value) == SSA_NAME)
3720 : : {
3721 : 218715 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
3722 : 218715 : if (!def_bb || dominated_by_p (CDI_DOMINATORS, pe->dest, def_bb))
3723 : 24135 : return true;
3724 : : }
3725 : :
3726 : : return false;
3727 : : }
3728 : :
3729 : : /* Return true if STMT can be hoisted from if-converted loop LOOP to
3730 : : edge PE. */
3731 : :
3732 : : static bool
3733 : 650196 : ifcvt_can_hoist (class loop *loop, edge pe, gimple *stmt)
3734 : : {
3735 : 650196 : if (auto *call = dyn_cast<gcall *> (stmt))
3736 : : {
3737 : 5381 : if (gimple_call_internal_p (call)
3738 : 5381 : && internal_fn_mask_index (gimple_call_internal_fn (call)) >= 0)
3739 : : return false;
3740 : : }
3741 : 967981 : else if (auto *assign = dyn_cast<gassign *> (stmt))
3742 : : {
3743 : 396581 : if (gimple_assign_rhs_code (assign) == COND_EXPR)
3744 : : return false;
3745 : : }
3746 : : else
3747 : : return false;
3748 : :
3749 : 285487 : if (gimple_has_side_effects (stmt)
3750 : 284238 : || gimple_could_trap_p (stmt)
3751 : 207505 : || stmt_could_throw_p (cfun, stmt)
3752 : 207503 : || gimple_vdef (stmt)
3753 : 492319 : || gimple_vuse (stmt))
3754 : 79675 : return false;
3755 : :
3756 : 205812 : int num_args = gimple_num_args (stmt);
3757 : 205812 : if (pe != loop_preheader_edge (loop))
3758 : : {
3759 : 229501 : for (int i = 0; i < num_args; ++i)
3760 : 225567 : if (!ifcvt_available_on_edge_p (pe, gimple_arg (stmt, i)))
3761 : : return false;
3762 : : }
3763 : : else
3764 : : {
3765 : 7866 : for (int i = 0; i < num_args; ++i)
3766 : 7610 : if (!expr_invariant_in_loop_p (loop, gimple_arg (stmt, i)))
3767 : : return false;
3768 : : }
3769 : :
3770 : : return true;
3771 : : }
3772 : :
3773 : : /* Hoist invariant statements from LOOP to edge PE. */
3774 : :
3775 : : static void
3776 : 26680 : ifcvt_hoist_invariants (class loop *loop, edge pe)
3777 : : {
3778 : : /* Only hoist from the now unconditionally executed part of the loop. */
3779 : 26680 : basic_block bb = loop->header;
3780 : 26680 : gimple_stmt_iterator hoist_gsi = {};
3781 : 703556 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
3782 : : {
3783 : 650196 : gimple *stmt = gsi_stmt (gsi);
3784 : 650196 : if (ifcvt_can_hoist (loop, pe, stmt))
3785 : : {
3786 : : /* Once we've hoisted one statement, insert other statements
3787 : : after it. */
3788 : 4190 : gsi_remove (&gsi, false);
3789 : 4190 : if (hoist_gsi.ptr)
3790 : 1807 : gsi_insert_after (&hoist_gsi, stmt, GSI_NEW_STMT);
3791 : : else
3792 : : {
3793 : 2383 : gsi_insert_on_edge_immediate (pe, stmt);
3794 : 2383 : hoist_gsi = gsi_for_stmt (stmt);
3795 : : }
3796 : 4190 : continue;
3797 : : }
3798 : 646006 : gsi_next (&gsi);
3799 : : }
3800 : 26680 : }
3801 : :
3802 : : /* Returns the DECL_FIELD_BIT_OFFSET of the bitfield accesse in stmt iff its
3803 : : type mode is not BLKmode. If BITPOS is not NULL it will hold the poly_int64
3804 : : value of the DECL_FIELD_BIT_OFFSET of the bitfield access and STRUCT_EXPR,
3805 : : if not NULL, will hold the tree representing the base struct of this
3806 : : bitfield. */
3807 : :
3808 : : static tree
3809 : 1093 : get_bitfield_rep (gassign *stmt, bool write, tree *bitpos,
3810 : : tree *struct_expr)
3811 : : {
3812 : 1093 : tree comp_ref = write ? gimple_assign_lhs (stmt)
3813 : 400 : : gimple_assign_rhs1 (stmt);
3814 : :
3815 : 1093 : tree field_decl = TREE_OPERAND (comp_ref, 1);
3816 : 1093 : tree ref_offset = component_ref_field_offset (comp_ref);
3817 : 1093 : tree rep_decl = DECL_BIT_FIELD_REPRESENTATIVE (field_decl);
3818 : :
3819 : : /* Bail out if the representative is not a suitable type for a scalar
3820 : : register variable. */
3821 : 1093 : if (!is_gimple_reg_type (TREE_TYPE (rep_decl)))
3822 : : return NULL_TREE;
3823 : :
3824 : : /* Bail out if the DECL_SIZE of the field_decl isn't the same as the BF's
3825 : : precision. */
3826 : 1078 : unsigned HOST_WIDE_INT bf_prec
3827 : 1078 : = TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)));
3828 : 1078 : if (compare_tree_int (DECL_SIZE (field_decl), bf_prec) != 0)
3829 : : return NULL_TREE;
3830 : :
3831 : 1078 : if (TREE_CODE (DECL_FIELD_OFFSET (rep_decl)) != INTEGER_CST
3832 : 1078 : || TREE_CODE (ref_offset) != INTEGER_CST)
3833 : : {
3834 : 2 : if (dump_file && (dump_flags & TDF_DETAILS))
3835 : 2 : fprintf (dump_file, "\t Bitfield NOT OK to lower,"
3836 : : " offset is non-constant.\n");
3837 : 2 : return NULL_TREE;
3838 : : }
3839 : :
3840 : 1076 : if (struct_expr)
3841 : 538 : *struct_expr = TREE_OPERAND (comp_ref, 0);
3842 : :
3843 : 1076 : if (bitpos)
3844 : : {
3845 : : /* To calculate the bitposition of the BITFIELD_REF we have to determine
3846 : : where our bitfield starts in relation to the container REP_DECL. The
3847 : : DECL_FIELD_OFFSET of the original bitfield's member FIELD_DECL tells
3848 : : us how many bytes from the start of the structure there are until the
3849 : : start of the group of bitfield members the FIELD_DECL belongs to,
3850 : : whereas DECL_FIELD_BIT_OFFSET will tell us how many bits from that
3851 : : position our actual bitfield member starts. For the container
3852 : : REP_DECL adding DECL_FIELD_OFFSET and DECL_FIELD_BIT_OFFSET will tell
3853 : : us the distance between the start of the structure and the start of
3854 : : the container, though the first is in bytes and the later other in
3855 : : bits. With this in mind we calculate the bit position of our new
3856 : : BITFIELD_REF by subtracting the number of bits between the start of
3857 : : the structure and the container from the number of bits from the start
3858 : : of the structure and the actual bitfield member. */
3859 : 538 : tree bf_pos = fold_build2 (MULT_EXPR, bitsizetype,
3860 : : ref_offset,
3861 : : build_int_cst (bitsizetype, BITS_PER_UNIT));
3862 : 538 : bf_pos = fold_build2 (PLUS_EXPR, bitsizetype, bf_pos,
3863 : : DECL_FIELD_BIT_OFFSET (field_decl));
3864 : 538 : tree rep_pos = fold_build2 (MULT_EXPR, bitsizetype,
3865 : : DECL_FIELD_OFFSET (rep_decl),
3866 : : build_int_cst (bitsizetype, BITS_PER_UNIT));
3867 : 538 : rep_pos = fold_build2 (PLUS_EXPR, bitsizetype, rep_pos,
3868 : : DECL_FIELD_BIT_OFFSET (rep_decl));
3869 : :
3870 : 538 : *bitpos = fold_build2 (MINUS_EXPR, bitsizetype, bf_pos, rep_pos);
3871 : : }
3872 : :
3873 : : return rep_decl;
3874 : :
3875 : : }
3876 : :
3877 : : /* Lowers the bitfield described by DATA.
3878 : : For a write like:
3879 : :
3880 : : struct.bf = _1;
3881 : :
3882 : : lower to:
3883 : :
3884 : : __ifc_1 = struct.<representative>;
3885 : : __ifc_2 = BIT_INSERT_EXPR (__ifc_1, _1, bitpos);
3886 : : struct.<representative> = __ifc_2;
3887 : :
3888 : : For a read:
3889 : :
3890 : : _1 = struct.bf;
3891 : :
3892 : : lower to:
3893 : :
3894 : : __ifc_1 = struct.<representative>;
3895 : : _1 = BIT_FIELD_REF (__ifc_1, bitsize, bitpos);
3896 : :
3897 : : where representative is a legal load that contains the bitfield value,
3898 : : bitsize is the size of the bitfield and bitpos the offset to the start of
3899 : : the bitfield within the representative. */
3900 : :
3901 : : static void
3902 : 538 : lower_bitfield (gassign *stmt, bool write)
3903 : : {
3904 : 538 : tree struct_expr;
3905 : 538 : tree bitpos;
3906 : 538 : tree rep_decl = get_bitfield_rep (stmt, write, &bitpos, &struct_expr);
3907 : 538 : tree rep_type = TREE_TYPE (rep_decl);
3908 : 538 : tree bf_type = TREE_TYPE (gimple_assign_lhs (stmt));
3909 : :
3910 : 538 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3911 : 538 : if (dump_file && (dump_flags & TDF_DETAILS))
3912 : : {
3913 : 9 : fprintf (dump_file, "Lowering:\n");
3914 : 9 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3915 : 9 : fprintf (dump_file, "to:\n");
3916 : : }
3917 : :
3918 : : /* REP_COMP_REF is a COMPONENT_REF for the representative. NEW_VAL is it's
3919 : : defining SSA_NAME. */
3920 : 538 : tree rep_comp_ref = build3 (COMPONENT_REF, rep_type, struct_expr, rep_decl,
3921 : : NULL_TREE);
3922 : 538 : tree new_val = ifc_temp_var (rep_type, rep_comp_ref, &gsi);
3923 : :
3924 : 538 : if (dump_file && (dump_flags & TDF_DETAILS))
3925 : 9 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
3926 : :
3927 : 538 : if (write)
3928 : : {
3929 : 342 : new_val = ifc_temp_var (rep_type,
3930 : : build3 (BIT_INSERT_EXPR, rep_type, new_val,
3931 : : unshare_expr (gimple_assign_rhs1 (stmt)),
3932 : : bitpos), &gsi);
3933 : :
3934 : 342 : if (dump_file && (dump_flags & TDF_DETAILS))
3935 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
3936 : :
3937 : 342 : gimple *new_stmt = gimple_build_assign (unshare_expr (rep_comp_ref),
3938 : : new_val);
3939 : 342 : gimple_move_vops (new_stmt, stmt);
3940 : 342 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
3941 : :
3942 : 342 : if (dump_file && (dump_flags & TDF_DETAILS))
3943 : 0 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
3944 : : }
3945 : : else
3946 : : {
3947 : 196 : tree bfr = build3 (BIT_FIELD_REF, bf_type, new_val,
3948 : 196 : build_int_cst (bitsizetype, TYPE_PRECISION (bf_type)),
3949 : : bitpos);
3950 : 196 : new_val = ifc_temp_var (bf_type, bfr, &gsi);
3951 : :
3952 : 196 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (stmt),
3953 : : new_val);
3954 : 196 : gimple_move_vops (new_stmt, stmt);
3955 : 196 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
3956 : :
3957 : 196 : if (dump_file && (dump_flags & TDF_DETAILS))
3958 : 9 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
3959 : : }
3960 : :
3961 : 538 : gsi_remove (&gsi, true);
3962 : 538 : }
3963 : :
3964 : : /* Return TRUE if there are bitfields to lower in this LOOP. Fill TO_LOWER
3965 : : with data structures representing these bitfields. */
3966 : :
3967 : : static bool
3968 : 238779 : bitfields_to_lower_p (class loop *loop,
3969 : : vec <gassign *> &reads_to_lower,
3970 : : vec <gassign *> &writes_to_lower)
3971 : : {
3972 : 238779 : gimple_stmt_iterator gsi;
3973 : :
3974 : 238779 : if (dump_file && (dump_flags & TDF_DETAILS))
3975 : : {
3976 : 31 : fprintf (dump_file, "Analyzing loop %d for bitfields:\n", loop->num);
3977 : : }
3978 : :
3979 : 1013334 : for (unsigned i = 0; i < loop->num_nodes; ++i)
3980 : : {
3981 : 774576 : basic_block bb = ifc_bbs[i];
3982 : 6421304 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3983 : : {
3984 : 4872173 : gassign *stmt = dyn_cast<gassign*> (gsi_stmt (gsi));
3985 : 4872173 : if (!stmt)
3986 : 4747105 : continue;
3987 : :
3988 : 2034511 : tree op = gimple_assign_lhs (stmt);
3989 : 2034511 : bool write = TREE_CODE (op) == COMPONENT_REF;
3990 : :
3991 : 2034511 : if (!write)
3992 : 2004905 : op = gimple_assign_rhs1 (stmt);
3993 : :
3994 : 2034511 : if (TREE_CODE (op) != COMPONENT_REF)
3995 : 1909443 : continue;
3996 : :
3997 : 125068 : if (DECL_BIT_FIELD_TYPE (TREE_OPERAND (op, 1)))
3998 : : {
3999 : 559 : if (dump_file && (dump_flags & TDF_DETAILS))
4000 : 11 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
4001 : :
4002 : 559 : if (TREE_THIS_VOLATILE (op))
4003 : : {
4004 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
4005 : 0 : fprintf (dump_file, "\t Bitfield NO OK to lower,"
4006 : : " the access is volatile.\n");
4007 : 21 : return false;
4008 : : }
4009 : :
4010 : 555 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op)))
4011 : : {
4012 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4013 : 0 : fprintf (dump_file, "\t Bitfield NO OK to lower,"
4014 : : " field type is not Integral.\n");
4015 : 0 : return false;
4016 : : }
4017 : :
4018 : 555 : if (!get_bitfield_rep (stmt, write, NULL, NULL))
4019 : : {
4020 : 17 : if (dump_file && (dump_flags & TDF_DETAILS))
4021 : 2 : fprintf (dump_file, "\t Bitfield NOT OK to lower,"
4022 : : " representative is BLKmode.\n");
4023 : 17 : return false;
4024 : : }
4025 : :
4026 : 538 : if (dump_file && (dump_flags & TDF_DETAILS))
4027 : 9 : fprintf (dump_file, "\tBitfield OK to lower.\n");
4028 : 538 : if (write)
4029 : 342 : writes_to_lower.safe_push (stmt);
4030 : : else
4031 : 196 : reads_to_lower.safe_push (stmt);
4032 : : }
4033 : : }
4034 : : }
4035 : 477373 : return !reads_to_lower.is_empty () || !writes_to_lower.is_empty ();
4036 : : }
4037 : :
4038 : :
4039 : : /* If-convert LOOP when it is legal. For the moment this pass has no
4040 : : profitability analysis. Returns non-zero todo flags when something
4041 : : changed. */
4042 : :
4043 : : unsigned int
4044 : 508459 : tree_if_conversion (class loop *loop, vec<gimple *> *preds)
4045 : : {
4046 : 508459 : unsigned int todo = 0;
4047 : 508459 : bool aggressive_if_conv;
4048 : 508459 : class loop *rloop;
4049 : 508459 : auto_vec <gassign *, 4> reads_to_lower;
4050 : 508459 : auto_vec <gassign *, 4> writes_to_lower;
4051 : 508459 : bitmap exit_bbs;
4052 : 508459 : edge pe;
4053 : 508459 : auto_vec<data_reference_p, 10> refs;
4054 : 509365 : bool loop_versioned;
4055 : :
4056 : 509365 : again:
4057 : 509365 : rloop = NULL;
4058 : 509365 : ifc_bbs = NULL;
4059 : 509365 : need_to_lower_bitfields = false;
4060 : 509365 : need_to_ifcvt = false;
4061 : 509365 : need_to_predicate = false;
4062 : 509365 : need_to_rewrite_undefined = false;
4063 : 509365 : any_complicated_phi = false;
4064 : 509365 : loop_versioned = false;
4065 : :
4066 : : /* Apply more aggressive if-conversion when loop or its outer loop were
4067 : : marked with simd pragma. When that's the case, we try to if-convert
4068 : : loop containing PHIs with more than MAX_PHI_ARG_NUM arguments. */
4069 : 509365 : aggressive_if_conv = loop->force_vectorize;
4070 : 509365 : if (!aggressive_if_conv)
4071 : : {
4072 : 501015 : class loop *outer_loop = loop_outer (loop);
4073 : 501015 : if (outer_loop && outer_loop->force_vectorize)
4074 : 8666 : aggressive_if_conv = true;
4075 : : }
4076 : :
4077 : : /* If there are more than two BBs in the loop then there is at least one if
4078 : : to convert. */
4079 : 509365 : if (loop->num_nodes > 2
4080 : 509365 : && !ifcvt_split_critical_edges (loop, aggressive_if_conv))
4081 : 101318 : goto cleanup;
4082 : :
4083 : 408047 : ifc_bbs = get_loop_body_in_if_conv_order (loop);
4084 : 408047 : if (!ifc_bbs)
4085 : : {
4086 : 2518 : if (dump_file && (dump_flags & TDF_DETAILS))
4087 : 3 : fprintf (dump_file, "Irreducible loop\n");
4088 : 2518 : goto cleanup;
4089 : : }
4090 : :
4091 : 405529 : if (find_data_references_in_loop (loop, &refs) == chrec_dont_know)
4092 : 154022 : goto cleanup;
4093 : :
4094 : 251507 : if (loop->num_nodes > 2)
4095 : : {
4096 : : /* More than one loop exit is too much to handle. */
4097 : 138438 : if (!single_exit (loop))
4098 : : {
4099 : 99319 : if (dump_file && (dump_flags & TDF_DETAILS))
4100 : 10 : fprintf (dump_file, "Can not ifcvt due to multiple exits\n");
4101 : : }
4102 : : else
4103 : : {
4104 : 39119 : need_to_ifcvt = true;
4105 : :
4106 : 39119 : if (!if_convertible_loop_p (loop, &refs)
4107 : 39119 : || !dbg_cnt (if_conversion_tree))
4108 : 12675 : goto cleanup;
4109 : :
4110 : 26444 : if ((need_to_predicate || any_complicated_phi)
4111 : 3418 : && ((!flag_tree_loop_vectorize && !loop->force_vectorize)
4112 : 3418 : || loop->dont_vectorize))
4113 : 0 : goto cleanup;
4114 : : }
4115 : : }
4116 : :
4117 : 238832 : if ((flag_tree_loop_vectorize || loop->force_vectorize)
4118 : 238779 : && !loop->dont_vectorize)
4119 : 238779 : need_to_lower_bitfields = bitfields_to_lower_p (loop, reads_to_lower,
4120 : : writes_to_lower);
4121 : :
4122 : 238832 : if (!need_to_ifcvt && !need_to_lower_bitfields)
4123 : 212152 : goto cleanup;
4124 : :
4125 : : /* The edge to insert invariant stmts on. */
4126 : 26680 : pe = loop_preheader_edge (loop);
4127 : :
4128 : : /* Since we have no cost model, always version loops unless the user
4129 : : specified -ftree-loop-if-convert or unless versioning is required.
4130 : : Either version this loop, or if the pattern is right for outer-loop
4131 : : vectorization, version the outer loop. In the latter case we will
4132 : : still if-convert the original inner loop. */
4133 : 26680 : if (need_to_lower_bitfields
4134 : 26442 : || need_to_predicate
4135 : 24412 : || any_complicated_phi
4136 : 23026 : || flag_tree_loop_if_convert != 1)
4137 : : {
4138 : 26631 : class loop *vloop
4139 : 26631 : = (versionable_outer_loop_p (loop_outer (loop))
4140 : 26631 : ? loop_outer (loop) : loop);
4141 : 26631 : class loop *nloop = version_loop_for_if_conversion (vloop, preds);
4142 : 26631 : if (nloop == NULL)
4143 : 0 : goto cleanup;
4144 : 26631 : if (vloop != loop)
4145 : : {
4146 : : /* If versionable_outer_loop_p decided to version the
4147 : : outer loop, version also the inner loop of the non-vectorized
4148 : : loop copy. So we transform:
4149 : : loop1
4150 : : loop2
4151 : : into:
4152 : : if (LOOP_VECTORIZED (1, 3))
4153 : : {
4154 : : loop1
4155 : : loop2
4156 : : }
4157 : : else
4158 : : loop3 (copy of loop1)
4159 : : if (LOOP_VECTORIZED (4, 5))
4160 : : loop4 (copy of loop2)
4161 : : else
4162 : : loop5 (copy of loop4) */
4163 : 906 : gcc_assert (nloop->inner && nloop->inner->next == NULL);
4164 : : rloop = nloop->inner;
4165 : : }
4166 : : else
4167 : : /* If we versioned loop then make sure to insert invariant
4168 : : stmts before the .LOOP_VECTORIZED check since the vectorizer
4169 : : will re-use that for things like runtime alias versioning
4170 : : whose condition can end up using those invariants. */
4171 : 25725 : pe = single_pred_edge (gimple_bb (preds->last ()));
4172 : :
4173 : : loop_versioned = true;
4174 : : }
4175 : :
4176 : 26680 : if (need_to_lower_bitfields)
4177 : : {
4178 : 238 : if (dump_file && (dump_flags & TDF_DETAILS))
4179 : : {
4180 : 9 : fprintf (dump_file, "-------------------------\n");
4181 : 9 : fprintf (dump_file, "Start lowering bitfields\n");
4182 : : }
4183 : 434 : while (!reads_to_lower.is_empty ())
4184 : 196 : lower_bitfield (reads_to_lower.pop (), false);
4185 : 580 : while (!writes_to_lower.is_empty ())
4186 : 342 : lower_bitfield (writes_to_lower.pop (), true);
4187 : :
4188 : 238 : if (dump_file && (dump_flags & TDF_DETAILS))
4189 : : {
4190 : 9 : fprintf (dump_file, "Done lowering bitfields\n");
4191 : 9 : fprintf (dump_file, "-------------------------\n");
4192 : : }
4193 : : }
4194 : 26680 : if (need_to_ifcvt)
4195 : : {
4196 : : /* Before we rewrite edges we'll record their original position in the
4197 : : edge map such that we can map the edges between the ifcvt and the
4198 : : non-ifcvt loop during peeling. */
4199 : 26444 : uintptr_t idx = 0;
4200 : 105776 : for (edge exit : get_loop_exit_edges (loop))
4201 : 26444 : exit->aux = (void*)idx++;
4202 : :
4203 : : /* Now all statements are if-convertible. Combine all the basic
4204 : : blocks into one huge basic block doing the if-conversion
4205 : : on-the-fly. */
4206 : 26444 : combine_blocks (loop, loop_versioned);
4207 : : }
4208 : :
4209 : : std::pair <tree, tree> *name_pair;
4210 : : unsigned ssa_names_idx;
4211 : 32109 : FOR_EACH_VEC_ELT (redundant_ssa_names, ssa_names_idx, name_pair)
4212 : 5429 : replace_uses_by (name_pair->first, name_pair->second);
4213 : 26680 : redundant_ssa_names.release ();
4214 : :
4215 : : /* Perform local CSE, this esp. helps the vectorizer analysis if loads
4216 : : and stores are involved. CSE only the loop body, not the entry
4217 : : PHIs, those are to be kept in sync with the non-if-converted copy.
4218 : : ??? We'll still keep dead stores though. */
4219 : 26680 : exit_bbs = BITMAP_ALLOC (NULL);
4220 : 106877 : for (edge exit : get_loop_exit_edges (loop))
4221 : 26855 : bitmap_set_bit (exit_bbs, exit->dest->index);
4222 : 26680 : todo |= do_rpo_vn (cfun, loop_preheader_edge (loop), exit_bbs,
4223 : : false, true, true);
4224 : :
4225 : : /* Delete dead predicate computations. */
4226 : 26680 : ifcvt_local_dce (loop);
4227 : 26680 : BITMAP_FREE (exit_bbs);
4228 : :
4229 : 26680 : ifcvt_hoist_invariants (loop, pe);
4230 : :
4231 : 26680 : todo |= TODO_cleanup_cfg;
4232 : :
4233 : 509365 : cleanup:
4234 : 509365 : data_reference_p dr;
4235 : 509365 : unsigned int i;
4236 : 1654574 : for (i = 0; refs.iterate (i, &dr); i++)
4237 : : {
4238 : 1145209 : free (dr->aux);
4239 : 1145209 : free_data_ref (dr);
4240 : : }
4241 : 509365 : refs.truncate (0);
4242 : :
4243 : 509365 : if (ifc_bbs)
4244 : : {
4245 : : unsigned int i;
4246 : :
4247 : 1997959 : for (i = 0; i < loop->num_nodes; i++)
4248 : 1618874 : free_bb_predicate (ifc_bbs[i]);
4249 : :
4250 : 379085 : free (ifc_bbs);
4251 : 379085 : ifc_bbs = NULL;
4252 : : }
4253 : 509365 : if (rloop != NULL)
4254 : : {
4255 : 906 : loop = rloop;
4256 : 906 : reads_to_lower.truncate (0);
4257 : 906 : writes_to_lower.truncate (0);
4258 : 906 : goto again;
4259 : : }
4260 : :
4261 : 508459 : return todo;
4262 : 508459 : }
4263 : :
4264 : : /* Tree if-conversion pass management. */
4265 : :
4266 : : namespace {
4267 : :
4268 : : const pass_data pass_data_if_conversion =
4269 : : {
4270 : : GIMPLE_PASS, /* type */
4271 : : "ifcvt", /* name */
4272 : : OPTGROUP_NONE, /* optinfo_flags */
4273 : : TV_TREE_LOOP_IFCVT, /* tv_id */
4274 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4275 : : 0, /* properties_provided */
4276 : : 0, /* properties_destroyed */
4277 : : 0, /* todo_flags_start */
4278 : : 0, /* todo_flags_finish */
4279 : : };
4280 : :
4281 : : class pass_if_conversion : public gimple_opt_pass
4282 : : {
4283 : : public:
4284 : 289302 : pass_if_conversion (gcc::context *ctxt)
4285 : 578604 : : gimple_opt_pass (pass_data_if_conversion, ctxt)
4286 : : {}
4287 : :
4288 : : /* opt_pass methods: */
4289 : : bool gate (function *) final override;
4290 : : unsigned int execute (function *) final override;
4291 : :
4292 : : }; // class pass_if_conversion
4293 : :
4294 : : bool
4295 : 242657 : pass_if_conversion::gate (function *fun)
4296 : : {
4297 : 33985 : return (((flag_tree_loop_vectorize || fun->has_force_vectorize_loops)
4298 : 210484 : && flag_tree_loop_if_convert != 0)
4299 : 274839 : || flag_tree_loop_if_convert == 1);
4300 : : }
4301 : :
4302 : : unsigned int
4303 : 210496 : pass_if_conversion::execute (function *fun)
4304 : : {
4305 : 210496 : unsigned todo = 0;
4306 : :
4307 : 420992 : if (number_of_loops (fun) <= 1)
4308 : : return 0;
4309 : :
4310 : 210496 : auto_vec<gimple *> preds;
4311 : 1147253 : for (auto loop : loops_list (cfun, 0))
4312 : 515765 : if (flag_tree_loop_if_convert == 1
4313 : 515518 : || ((flag_tree_loop_vectorize || loop->force_vectorize)
4314 : 512680 : && !loop->dont_vectorize))
4315 : 508459 : todo |= tree_if_conversion (loop, &preds);
4316 : :
4317 : 210496 : if (todo)
4318 : : {
4319 : 17815 : free_numbers_of_iterations_estimates (fun);
4320 : 17815 : scev_reset ();
4321 : : }
4322 : :
4323 : 210496 : if (flag_checking)
4324 : : {
4325 : 210492 : basic_block bb;
4326 : 7236465 : FOR_EACH_BB_FN (bb, fun)
4327 : 7025973 : gcc_assert (!bb->aux);
4328 : : }
4329 : :
4330 : : /* Perform IL update now, it might elide some loops. */
4331 : 210496 : if (todo & TODO_cleanup_cfg)
4332 : : {
4333 : 17815 : cleanup_tree_cfg ();
4334 : 17815 : if (need_ssa_update_p (fun))
4335 : 0 : todo |= TODO_update_ssa;
4336 : : }
4337 : 210496 : if (todo & TODO_update_ssa_any)
4338 : 0 : update_ssa (todo & TODO_update_ssa_any);
4339 : :
4340 : : /* If if-conversion elided the loop fall back to the original one. Likewise
4341 : : if the loops are not nested in the same outer loop. */
4342 : 237127 : for (unsigned i = 0; i < preds.length (); ++i)
4343 : : {
4344 : 26631 : gimple *g = preds[i];
4345 : 26631 : if (!gimple_bb (g))
4346 : 0 : continue;
4347 : 26631 : auto ifcvt_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 0)));
4348 : 26631 : auto orig_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 1)));
4349 : 26631 : if (!ifcvt_loop || !orig_loop)
4350 : : {
4351 : 2 : if (dump_file)
4352 : 0 : fprintf (dump_file, "If-converted loop vanished\n");
4353 : 2 : fold_loop_internal_call (g, boolean_false_node);
4354 : : }
4355 : 26629 : else if (loop_outer (ifcvt_loop) != loop_outer (orig_loop))
4356 : : {
4357 : 0 : if (dump_file)
4358 : 0 : fprintf (dump_file, "If-converted loop in different outer loop\n");
4359 : 0 : fold_loop_internal_call (g, boolean_false_node);
4360 : : }
4361 : : }
4362 : :
4363 : 210496 : return 0;
4364 : 210496 : }
4365 : :
4366 : : } // anon namespace
4367 : :
4368 : : gimple_opt_pass *
4369 : 289302 : make_pass_if_conversion (gcc::context *ctxt)
4370 : : {
4371 : 289302 : return new pass_if_conversion (ctxt);
4372 : : }
|