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 : 425221 : innermost_loop_behavior_hash::hash (const value_type &e)
171 : : {
172 : 425221 : hashval_t hash;
173 : :
174 : 425221 : hash = iterative_hash_expr (e->base_address, 0);
175 : 425221 : hash = iterative_hash_expr (e->offset, hash);
176 : 425221 : hash = iterative_hash_expr (e->init, hash);
177 : 425221 : return iterative_hash_expr (e->step, hash);
178 : : }
179 : :
180 : : inline bool
181 : 303432 : innermost_loop_behavior_hash::equal (const value_type &e1,
182 : : const compare_type &e2)
183 : : {
184 : 303432 : if ((e1->base_address && !e2->base_address)
185 : 303432 : || (!e1->base_address && e2->base_address)
186 : 303432 : || (!e1->offset && e2->offset)
187 : 278755 : || (e1->offset && !e2->offset)
188 : 244064 : || (!e1->init && e2->init)
189 : 244064 : || (e1->init && !e2->init)
190 : 244064 : || (!e1->step && e2->step)
191 : 244064 : || (e1->step && !e2->step))
192 : : return false;
193 : :
194 : 244064 : if (e1->base_address && e2->base_address
195 : 488128 : && !operand_equal_p (e1->base_address, e2->base_address, 0))
196 : : return false;
197 : 42651 : if (e1->offset && e2->offset
198 : 122386 : && !operand_equal_p (e1->offset, e2->offset, 0))
199 : : return false;
200 : 42403 : if (e1->init && e2->init
201 : 121890 : && !operand_equal_p (e1->init, e2->init, 0))
202 : : return false;
203 : 16819 : if (e1->step && e2->step
204 : 70722 : && !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 : 2015182 : bb_has_predicate (basic_block bb)
244 : : {
245 : 2015182 : return bb->aux != NULL;
246 : : }
247 : :
248 : : /* Returns the gimplified predicate for basic block BB. */
249 : :
250 : : static inline tree
251 : 893351 : bb_predicate (basic_block bb)
252 : : {
253 : 893351 : 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 : 488351 : set_bb_predicate (basic_block bb, tree cond)
260 : : {
261 : 488351 : auto aux = (struct bb_predicate *) bb->aux;
262 : 488351 : gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
263 : : && is_gimple_val (TREE_OPERAND (cond, 0)))
264 : : || is_gimple_val (cond));
265 : 488351 : aux->predicate = cond;
266 : 488351 : aux->no_predicate_stmts++;
267 : :
268 : 488351 : if (dump_file && (dump_flags & TDF_DETAILS))
269 : 237 : fprintf (dump_file, "Recording block %d value %d\n", bb->index,
270 : : aux->no_predicate_stmts);
271 : 488351 : }
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 : 602419 : bb_predicate_gimplified_stmts (basic_block bb)
278 : : {
279 : 602419 : 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 : 285812 : set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts,
288 : : bool preserve_counts)
289 : : {
290 : 285812 : ((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts = stmts;
291 : 285812 : if (stmts == NULL && !preserve_counts)
292 : 234155 : ((struct bb_predicate *) bb->aux)->no_predicate_stmts = 0;
293 : 83766 : }
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 : 85905 : 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 : 85905 : for (gimple_stmt_iterator gsi = gsi_start (stmts);
308 : 208831 : !gsi_end_p (gsi); gsi_next (&gsi))
309 : : {
310 : 122926 : gimple *stmt = gsi_stmt (gsi);
311 : 122926 : delink_stmt_imm_use (stmt);
312 : 122926 : gimple_set_modified (stmt, true);
313 : 122926 : ((struct bb_predicate *) bb->aux)->no_predicate_stmts++;
314 : : }
315 : 85905 : gimple_seq_add_seq_without_update
316 : 85905 : (&(((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts), stmts);
317 : 85905 : }
318 : :
319 : : /* Return the number of statements the predicate of the basic block consists
320 : : of. */
321 : :
322 : : static inline unsigned
323 : 15747 : get_bb_num_predicate_stmts (basic_block bb)
324 : : {
325 : 15747 : 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 : 202046 : init_bb_predicate (basic_block bb)
332 : : {
333 : 202046 : bb->aux = XNEW (struct bb_predicate);
334 : 202046 : set_bb_predicate_gimplified_stmts (bb, NULL, false);
335 : 202046 : set_bb_predicate (bb, boolean_true_node);
336 : 202046 : }
337 : :
338 : : /* Release the SSA_NAMEs associated with the predicate of basic block BB. */
339 : :
340 : : static inline void
341 : 394765 : release_bb_predicate (basic_block bb)
342 : : {
343 : 394765 : gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
344 : 394765 : if (stmts)
345 : : {
346 : : /* Ensure that these stmts haven't yet been added to a bb. */
347 : 32109 : if (flag_checking)
348 : : for (gimple_stmt_iterator i = gsi_start (stmts);
349 : 88063 : !gsi_end_p (i); gsi_next (&i))
350 : 55954 : gcc_assert (! gimple_bb (gsi_stmt (i)));
351 : :
352 : : /* Discard them. */
353 : 32109 : gimple_seq_discard (stmts);
354 : 32109 : set_bb_predicate_gimplified_stmts (bb, NULL, false);
355 : : }
356 : 394765 : }
357 : :
358 : : /* Free the predicate of basic block BB. */
359 : :
360 : : static inline void
361 : 1822463 : free_bb_predicate (basic_block bb)
362 : : {
363 : 1822463 : if (!bb_has_predicate (bb))
364 : : return;
365 : :
366 : 202046 : release_bb_predicate (bb);
367 : 202046 : free (bb->aux);
368 : 202046 : bb->aux = NULL;
369 : : }
370 : :
371 : : /* Reinitialize predicate of BB with the true predicate. */
372 : :
373 : : static inline void
374 : 192719 : reset_bb_predicate (basic_block bb)
375 : : {
376 : 192719 : if (!bb_has_predicate (bb))
377 : 0 : init_bb_predicate (bb);
378 : : else
379 : : {
380 : 192719 : release_bb_predicate (bb);
381 : 192719 : set_bb_predicate (bb, boolean_true_node);
382 : : }
383 : 192719 : }
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 : 5824 : ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
392 : : {
393 : 5824 : tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
394 : 5824 : gimple *stmt = gimple_build_assign (new_name, expr);
395 : 11648 : gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (*gsi)));
396 : 5824 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
397 : 5824 : return new_name;
398 : : }
399 : :
400 : : /* Return true when COND is a false predicate. */
401 : :
402 : : static inline bool
403 : 62088 : is_false_predicate (tree cond)
404 : : {
405 : 62088 : return (cond != NULL_TREE
406 : 62088 : && (cond == boolean_false_node
407 : 62088 : || integer_zerop (cond)));
408 : : }
409 : :
410 : : /* Return true when COND is a true predicate. */
411 : :
412 : : static inline bool
413 : 1034217 : is_true_predicate (tree cond)
414 : : {
415 : 1034217 : return (cond == NULL_TREE
416 : 1034217 : || cond == boolean_true_node
417 : 1484495 : || 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 : 497980 : is_predicated (basic_block bb)
425 : : {
426 : 9856 : 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 : 445370 : parse_predicate (tree cond, tree *op0, tree *op1)
434 : : {
435 : 445370 : gimple *s;
436 : :
437 : 445370 : if (TREE_CODE (cond) == SSA_NAME
438 : 445370 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
439 : : {
440 : 62516 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
441 : : {
442 : 30189 : *op0 = gimple_assign_rhs1 (s);
443 : 30189 : *op1 = gimple_assign_rhs2 (s);
444 : 30189 : return gimple_assign_rhs_code (s);
445 : : }
446 : :
447 : 32327 : 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 : 382854 : if (COMPARISON_CLASS_P (cond))
461 : : {
462 : 575 : *op0 = TREE_OPERAND (cond, 0);
463 : 575 : *op1 = TREE_OPERAND (cond, 1);
464 : 575 : 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 : 222685 : fold_or_predicates (location_t loc, tree c1, tree c2)
474 : : {
475 : 222685 : tree op1a, op1b, op2a, op2b;
476 : 222685 : enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
477 : 222685 : enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
478 : :
479 : 222685 : if (code1 != ERROR_MARK && code2 != ERROR_MARK)
480 : : {
481 : 2167 : tree t = maybe_fold_or_comparisons (boolean_type_node, code1, op1a, op1b,
482 : : code2, op2a, op2b);
483 : 2167 : if (t)
484 : : return t;
485 : : }
486 : :
487 : 220586 : 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 : 47709 : 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 : 47709 : 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 : 47709 : 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 : 47709 : gimple_match_op cexpr (gimple_match_cond::UNCOND, COND_EXPR,
514 : 47709 : type, cond, rhs, lhs);
515 : 47709 : if (cexpr.resimplify (NULL, follow_all_ssa_edges))
516 : : {
517 : 10778 : if (gimple_simplified_result_is_gimple_val (&cexpr))
518 : 582 : return cexpr.ops[0];
519 : 10196 : else if (cexpr.code == ABS_EXPR)
520 : 2 : return build1 (ABS_EXPR, type, cexpr.ops[0]);
521 : 10194 : else if (cexpr.code == MIN_EXPR
522 : 10194 : || cexpr.code == MAX_EXPR)
523 : 7183 : return build2 ((tree_code)cexpr.code, type, cexpr.ops[0], cexpr.ops[1]);
524 : : }
525 : :
526 : 39942 : 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 : 159991 : add_to_predicate_list (class loop *loop, basic_block bb, tree nc)
536 : : {
537 : 159991 : tree bc, *tp;
538 : 159991 : basic_block dom_bb;
539 : :
540 : 159991 : if (is_true_predicate (nc))
541 : 72433 : return;
542 : :
543 : : /* If dominance tells us this basic block is always executed,
544 : : don't record any predicates for it. */
545 : 159989 : if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
546 : : return;
547 : :
548 : 93586 : 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 : 93586 : if (dom_bb != loop->header
554 : 93586 : && get_immediate_dominator (CDI_POST_DOMINATORS, dom_bb) == bb)
555 : : {
556 : 6028 : gcc_assert (flow_bb_inside_loop_p (loop, dom_bb));
557 : 6028 : bc = bb_predicate (dom_bb);
558 : 6028 : if (!is_true_predicate (bc))
559 : 6028 : set_bb_predicate (bb, bc);
560 : : else
561 : 0 : gcc_assert (is_true_predicate (bb_predicate (bb)));
562 : 6028 : 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 : 6028 : return;
566 : : }
567 : :
568 : 87558 : if (!is_predicated (bb))
569 : 83766 : bc = nc;
570 : : else
571 : : {
572 : 3792 : bc = bb_predicate (bb);
573 : 3792 : bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
574 : 3792 : 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 : 87558 : if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
583 : 31534 : tp = &TREE_OPERAND (bc, 0);
584 : : else
585 : : tp = &bc;
586 : 87558 : if (!is_gimple_val (*tp))
587 : : {
588 : 85905 : gimple_seq stmts;
589 : 85905 : *tp = force_gimple_operand (*tp, &stmts, true, NULL_TREE);
590 : 85905 : add_bb_predicate_gimplified_stmts (bb, stmts);
591 : : }
592 : 87558 : 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 : 104700 : add_to_dst_predicate_list (class loop *loop, edge e,
601 : : tree prev_cond, tree cond)
602 : : {
603 : 104700 : if (!flow_bb_inside_loop_p (loop, e->dest))
604 : : return;
605 : :
606 : 104700 : if (!is_true_predicate (prev_cond))
607 : 22310 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
608 : : prev_cond, cond);
609 : :
610 : 104700 : if (!dominated_by_p (CDI_DOMINATORS, loop->latch, e->dest))
611 : 84427 : 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 : 3534745 : bb_with_exit_edge_p (const class loop *loop, basic_block bb)
618 : : {
619 : 3534745 : edge e;
620 : 3534745 : edge_iterator ei;
621 : :
622 : 7065108 : FOR_EACH_EDGE (e, ei, bb->succs)
623 : 5004365 : 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 : 5359 : phi_convertible_by_degenerating_args (gphi *phi)
650 : : {
651 : 5359 : edge e;
652 : 5359 : tree arg, t1 = NULL, t2 = NULL;
653 : 5359 : unsigned int i, i1 = 0, i2 = 0, n1 = 0, n2 = 0;
654 : 5359 : unsigned int num_args = gimple_phi_num_args (phi);
655 : :
656 : 5359 : gcc_assert (num_args > 2);
657 : :
658 : 19412 : for (i = 0; i < num_args; i++)
659 : : {
660 : 16188 : arg = gimple_phi_arg_def (phi, i);
661 : 16188 : if (t1 == NULL || operand_equal_p (t1, arg, 0))
662 : : {
663 : 8437 : n1++;
664 : 8437 : i1 = i;
665 : 8437 : t1 = arg;
666 : : }
667 : 7751 : else if (t2 == NULL || operand_equal_p (t2, arg, 0))
668 : : {
669 : 5616 : n2++;
670 : 5616 : i2 = i;
671 : 5616 : t2 = arg;
672 : : }
673 : : else
674 : : return false;
675 : : }
676 : :
677 : 3224 : if (n1 != 1 && n2 != 1)
678 : : return false;
679 : :
680 : : /* Check if the edge corresponding to the unique arg is critical. */
681 : 3185 : e = gimple_phi_arg_edge (phi, (n1 == 1) ? i1 : i2);
682 : 3185 : 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 : 123105 : if_convertible_phi_p (class loop *loop, basic_block bb, gphi *phi)
695 : : {
696 : 123105 : if (dump_file && (dump_flags & TDF_DETAILS))
697 : : {
698 : 72 : fprintf (dump_file, "-------------------------\n");
699 : 72 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
700 : : }
701 : :
702 : 123105 : if (bb != loop->header
703 : 47609 : && gimple_phi_num_args (phi) > 2
704 : 128464 : && !phi_convertible_by_degenerating_args (phi))
705 : 2174 : any_complicated_phi = true;
706 : :
707 : 123105 : 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 : 131483 : hash_memrefs_baserefs_and_store_DRs_read_written_info (data_reference_p a)
740 : : {
741 : :
742 : 131483 : data_reference_p *master_dr, *base_master_dr;
743 : 131483 : tree base_ref = DR_BASE_OBJECT (a);
744 : 131483 : innermost_loop_behavior *innermost = &DR_INNERMOST (a);
745 : 131483 : tree ca = bb_predicate (gimple_bb (DR_STMT (a)));
746 : 131483 : bool exist1, exist2;
747 : :
748 : 131483 : master_dr = &innermost_DR_map->get_or_insert (innermost, &exist1);
749 : 131483 : if (!exist1)
750 : 97156 : *master_dr = a;
751 : :
752 : 131483 : if (DR_IS_WRITE (a))
753 : : {
754 : 43686 : IFC_DR (*master_dr)->w_predicate
755 : 87372 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
756 : 43686 : IFC_DR (*master_dr)->w_predicate);
757 : 43686 : if (is_true_predicate (IFC_DR (*master_dr)->w_predicate))
758 : 27677 : DR_W_UNCONDITIONALLY (*master_dr) = true;
759 : : }
760 : 131483 : IFC_DR (*master_dr)->rw_predicate
761 : 262966 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
762 : 131483 : IFC_DR (*master_dr)->rw_predicate);
763 : 131483 : if (is_true_predicate (IFC_DR (*master_dr)->rw_predicate))
764 : 96868 : DR_RW_UNCONDITIONALLY (*master_dr) = true;
765 : :
766 : 131483 : if (DR_IS_WRITE (a))
767 : : {
768 : 43686 : base_master_dr = &baseref_DR_map->get_or_insert (base_ref, &exist2);
769 : 43686 : if (!exist2)
770 : 33337 : *base_master_dr = a;
771 : 43686 : IFC_DR (*base_master_dr)->base_w_predicate
772 : 87372 : = fold_or_predicates (UNKNOWN_LOCATION, ca,
773 : 43686 : IFC_DR (*base_master_dr)->base_w_predicate);
774 : 43686 : if (is_true_predicate (IFC_DR (*base_master_dr)->base_w_predicate))
775 : 27911 : DR_BASE_W_UNCONDITIONALLY (*base_master_dr) = true;
776 : : }
777 : 131483 : }
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 : 229502 : idx_within_array_bound (tree ref, tree *idx, void *dta)
784 : : {
785 : 229502 : wi::overflow_type overflow;
786 : 229502 : widest_int niter, valid_niter, delta, wi_step;
787 : 229502 : tree ev, init, step;
788 : 229502 : tree low, high;
789 : 229502 : class loop *loop = (class loop*) dta;
790 : :
791 : : /* Only support within-bound access for array references. */
792 : 229502 : 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 : 141569 : if (array_ref_flexible_size_p (ref))
798 : : return false;
799 : :
800 : 88199 : ev = analyze_scalar_evolution (loop, *idx);
801 : 88199 : ev = instantiate_parameters (loop, ev);
802 : 88199 : init = initial_condition (ev);
803 : 88199 : step = evolution_part_in_loop_num (ev, loop->num);
804 : :
805 : 88199 : if (!init || TREE_CODE (init) != INTEGER_CST
806 : 78604 : || (step && TREE_CODE (step) != INTEGER_CST))
807 : : return false;
808 : :
809 : 78598 : low = array_ref_low_bound (ref);
810 : 78598 : high = array_ref_up_bound (ref);
811 : :
812 : : /* The case of nonconstant bounds could be handled, but it would be
813 : : complicated. */
814 : 78598 : if (TREE_CODE (low) != INTEGER_CST
815 : 78598 : || !high || TREE_CODE (high) != INTEGER_CST)
816 : : return false;
817 : :
818 : : /* Check if the intial idx is within bound. */
819 : 78530 : if (wi::to_widest (init) < wi::to_widest (low)
820 : 157052 : || wi::to_widest (init) > wi::to_widest (high))
821 : 15 : return false;
822 : :
823 : : /* The idx is always within bound. */
824 : 78515 : if (!step || integer_zerop (step))
825 : 1971 : return true;
826 : :
827 : 76544 : if (!max_loop_iterations (loop, &niter))
828 : : return false;
829 : :
830 : 76544 : 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 : 76251 : delta = wi::to_widest (high) - wi::to_widest (init);
838 : 76251 : wi_step = wi::to_widest (step);
839 : : }
840 : :
841 : 76544 : valid_niter = wi::div_floor (delta, wi_step, SIGNED, &overflow);
842 : : /* The iteration space of idx is within array bound. */
843 : 153088 : if (!overflow && niter <= valid_niter)
844 : : return true;
845 : :
846 : : return false;
847 : 229502 : }
848 : :
849 : : /* Return TRUE if ref is a within bound array reference. */
850 : :
851 : : bool
852 : 223416 : ref_within_array_bound (gimple *stmt, tree ref)
853 : : {
854 : 223416 : class loop *loop = loop_containing_stmt (stmt);
855 : :
856 : 223416 : gcc_assert (loop != NULL);
857 : 223416 : 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 : 2505 : base_object_writable (tree ref)
868 : : {
869 : 2505 : tree base_tree = get_base_address (ref);
870 : :
871 : 2505 : return (base_tree
872 : 2505 : && DECL_P (base_tree)
873 : 1641 : && decl_binds_to_current_def_p (base_tree)
874 : 4142 : && !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 : 19400 : 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 : 19400 : if (gimple_uid (stmt) == 0)
908 : : return false;
909 : :
910 : 19399 : data_reference_p *master_dr, *base_master_dr;
911 : 19399 : data_reference_p a = drs[gimple_uid (stmt) - 1];
912 : :
913 : 19399 : tree base = DR_BASE_OBJECT (a);
914 : 19399 : innermost_loop_behavior *innermost = &DR_INNERMOST (a);
915 : :
916 : 19399 : gcc_assert (DR_STMT (a) == stmt);
917 : 19399 : gcc_assert (DR_BASE_ADDRESS (a) || DR_OFFSET (a)
918 : : || DR_INIT (a) || DR_STEP (a));
919 : :
920 : 19399 : master_dr = innermost_DR_map->get (innermost);
921 : 19399 : gcc_assert (master_dr != NULL);
922 : :
923 : 19399 : base_master_dr = baseref_DR_map->get (base);
924 : :
925 : : /* If a is unconditionally written to it doesn't trap. */
926 : 19399 : 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 : 17698 : if (DR_RW_UNCONDITIONALLY (*master_dr)
935 : 17698 : || ref_within_array_bound (stmt, DR_REF (a)))
936 : : {
937 : : /* an unconditional read won't trap. */
938 : 6510 : 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 : 2550 : if ((base_master_dr
944 : 2550 : && DR_BASE_W_UNCONDITIONALLY (*base_master_dr))
945 : : /* or the base is known to be not readonly. */
946 : 5055 : || base_object_writable (DR_REF (a)))
947 : 1682 : 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 : 11497 : ifcvt_can_use_mask_load_store (gimple *stmt)
958 : : {
959 : : /* Check whether this is a load or store. */
960 : 11497 : tree lhs = gimple_assign_lhs (stmt);
961 : 11497 : bool is_load;
962 : 11497 : tree ref;
963 : 11497 : 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 : 8846 : else if (gimple_assign_load_p (stmt))
971 : : {
972 : 8845 : is_load = true;
973 : 8845 : ref = gimple_assign_rhs1 (stmt);
974 : : }
975 : : else
976 : : return false;
977 : :
978 : 11496 : 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 : 11436 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
984 : 11436 : if (!int_mode_for_mode (mode).exists () || VECTOR_MODE_P (mode))
985 : 31 : return false;
986 : :
987 : 11405 : 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 : 13217 : ifcvt_can_predicate (gimple *stmt)
998 : : {
999 : 13217 : basic_block bb = gimple_bb (stmt);
1000 : :
1001 : 320 : if (!(flag_tree_loop_vectorize || bb->loop_father->force_vectorize)
1002 : 13217 : || bb->loop_father->dont_vectorize
1003 : 26434 : || gimple_has_volatile_ops (stmt))
1004 : : return false;
1005 : :
1006 : 13217 : if (gimple_assign_single_p (stmt))
1007 : 11497 : return ifcvt_can_use_mask_load_store (stmt);
1008 : :
1009 : 1720 : tree_code code = gimple_assign_rhs_code (stmt);
1010 : 1720 : tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1011 : 1720 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
1012 : 1720 : if (!types_compatible_p (lhs_type, rhs_type))
1013 : : return false;
1014 : 1089 : internal_fn cond_fn = get_conditional_internal_fn (code);
1015 : 1089 : return (cond_fn != IFN_LAST
1016 : 1089 : && 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 : 64488 : if_convertible_gimple_assign_stmt_p (gimple *stmt,
1028 : : vec<data_reference_p> refs)
1029 : : {
1030 : 64488 : tree lhs = gimple_assign_lhs (stmt);
1031 : :
1032 : 64488 : if (dump_file && (dump_flags & TDF_DETAILS))
1033 : : {
1034 : 38 : fprintf (dump_file, "-------------------------\n");
1035 : 38 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1036 : : }
1037 : :
1038 : 64488 : if (!is_gimple_reg_type (TREE_TYPE (lhs)))
1039 : : return false;
1040 : :
1041 : : /* Some of these constrains might be too conservative. */
1042 : 64161 : if (stmt_ends_bb_p (stmt)
1043 : 64161 : || gimple_has_volatile_ops (stmt)
1044 : 64080 : || (TREE_CODE (lhs) == SSA_NAME
1045 : 59733 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1046 : 128241 : || gimple_has_side_effects (stmt))
1047 : : {
1048 : 81 : if (dump_file && (dump_flags & TDF_DETAILS))
1049 : 0 : fprintf (dump_file, "stmt not suitable for ifcvt\n");
1050 : 81 : 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 : 64080 : gimple_set_plf (stmt, GF_PLF_2, false);
1057 : :
1058 : 64080 : if ((! gimple_vuse (stmt)
1059 : 19400 : || gimple_could_trap_p_1 (stmt, false, false)
1060 : 19400 : || ! ifcvt_memrefs_wont_trap (stmt, refs))
1061 : 76581 : && gimple_could_trap_p (stmt))
1062 : : {
1063 : 13217 : if (ifcvt_can_predicate (stmt))
1064 : : {
1065 : 2585 : gimple_set_plf (stmt, GF_PLF_2, true);
1066 : 2585 : need_to_predicate = true;
1067 : 2585 : return true;
1068 : : }
1069 : 10632 : if (dump_file && (dump_flags & TDF_DETAILS))
1070 : 0 : fprintf (dump_file, "tree could trap...\n");
1071 : 10632 : return false;
1072 : : }
1073 : 50863 : else if (gimple_needing_rewrite_undefined (stmt))
1074 : : /* We have to rewrite stmts with undefined overflow. */
1075 : 18934 : need_to_rewrite_undefined = true;
1076 : :
1077 : : /* When if-converting stores force versioning, likewise if we
1078 : : ended up generating store data races. */
1079 : 101726 : if (gimple_vdef (stmt))
1080 : 1696 : 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 : 127079 : if_convertible_stmt_p (gimple *stmt, vec<data_reference_p> refs)
1119 : : {
1120 : 127079 : 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 : 64488 : case GIMPLE_ASSIGN:
1131 : 64488 : return if_convertible_gimple_assign_stmt_p (stmt, refs);
1132 : :
1133 : 1424 : case GIMPLE_CALL:
1134 : 1424 : {
1135 : 1424 : tree fndecl = gimple_call_fndecl (stmt);
1136 : 1424 : if (fndecl)
1137 : : {
1138 : : /* We can vectorize some builtins and functions with SIMD
1139 : : "inbranch" clones. */
1140 : 1176 : struct cgraph_node *node = cgraph_node::get (fndecl);
1141 : 1176 : if (node && node->simd_clones != NULL)
1142 : : /* Ensure that at least one clone can be "inbranch". */
1143 : 1943 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
1144 : 952 : n = n->simdclone->next_clone)
1145 : 1942 : if (n->simdclone->inbranch)
1146 : : {
1147 : 990 : gimple_set_plf (stmt, GF_PLF_2, true);
1148 : 990 : need_to_predicate = true;
1149 : 990 : 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 : 434 : int flags = gimple_call_flags (stmt);
1157 : 434 : if ((flags & ECF_CONST)
1158 : 434 : && !(flags & ECF_LOOPING_CONST_OR_PURE)
1159 : 868 : && 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 : 85983 : all_preds_critical_p (basic_block bb)
1182 : : {
1183 : 85983 : edge e;
1184 : 85983 : edge_iterator ei;
1185 : :
1186 : 167686 : FOR_EACH_EDGE (e, ei, bb->preds)
1187 : 146995 : 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 : 208742 : if_convertible_bb_p (class loop *loop, basic_block bb, basic_block exit_bb)
1205 : : {
1206 : 208742 : edge e;
1207 : 208742 : edge_iterator ei;
1208 : :
1209 : 208742 : if (dump_file && (dump_flags & TDF_DETAILS))
1210 : 92 : fprintf (dump_file, "----------[%d]-------------\n", bb->index);
1211 : :
1212 : 208742 : if (EDGE_COUNT (bb->succs) > 2)
1213 : : return false;
1214 : :
1215 : 417362 : if (gcall *call = safe_dyn_cast <gcall *> (*gsi_last_bb (bb)))
1216 : 185 : if (gimple_call_ctrl_altering_p (call))
1217 : : return false;
1218 : :
1219 : 208681 : if (exit_bb)
1220 : : {
1221 : 38423 : if (bb != loop->latch)
1222 : : {
1223 : 504 : if (dump_file && (dump_flags & TDF_DETAILS))
1224 : 0 : fprintf (dump_file, "basic block after exit bb but before latch\n");
1225 : 504 : return false;
1226 : : }
1227 : 37919 : else if (!empty_block_p (bb))
1228 : : {
1229 : 845 : if (dump_file && (dump_flags & TDF_DETAILS))
1230 : 0 : fprintf (dump_file, "non empty basic block after exit bb\n");
1231 : 845 : return false;
1232 : : }
1233 : 37074 : else if (bb == loop->latch
1234 : 37074 : && bb != exit_bb
1235 : 74148 : && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
1236 : : {
1237 : 8 : if (dump_file && (dump_flags & TDF_DETAILS))
1238 : 0 : fprintf (dump_file, "latch is not dominated by exit_block\n");
1239 : 8 : return false;
1240 : : }
1241 : : }
1242 : :
1243 : : /* Be less adventurous and handle only normal edges. */
1244 : 507298 : FOR_EACH_EDGE (e, ei, bb->succs)
1245 : 299982 : if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
1246 : : {
1247 : 8 : if (dump_file && (dump_flags & TDF_DETAILS))
1248 : 0 : fprintf (dump_file, "Difficult to handle edges\n");
1249 : 8 : 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 : 2407571 : pred_blocks_visited_p (basic_block bb, bitmap *visited)
1260 : : {
1261 : 2407571 : edge e;
1262 : 2407571 : edge_iterator ei;
1263 : 4156606 : FOR_EACH_EDGE (e, ei, bb->preds)
1264 : 2729736 : 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 : 407438 : get_loop_body_in_if_conv_order (const class loop *loop)
1278 : : {
1279 : 407438 : basic_block *blocks, *blocks_in_bfs_order;
1280 : 407438 : basic_block bb;
1281 : 407438 : bitmap visited;
1282 : 407438 : unsigned int index = 0;
1283 : 407438 : unsigned int visited_count = 0;
1284 : :
1285 : 407438 : gcc_assert (loop->num_nodes);
1286 : 407438 : gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
1287 : :
1288 : 407438 : blocks = XCNEWVEC (basic_block, loop->num_nodes);
1289 : 407438 : visited = BITMAP_ALLOC (NULL);
1290 : :
1291 : 407438 : blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
1292 : :
1293 : 407438 : index = 0;
1294 : 4208708 : while (index < loop->num_nodes)
1295 : : {
1296 : 3393875 : bb = blocks_in_bfs_order [index];
1297 : :
1298 : 3393875 : if (bb->flags & BB_IRREDUCIBLE_LOOP)
1299 : : {
1300 : 43 : free (blocks_in_bfs_order);
1301 : 43 : BITMAP_FREE (visited);
1302 : 43 : free (blocks);
1303 : 43 : return NULL;
1304 : : }
1305 : :
1306 : 3393832 : if (!bitmap_bit_p (visited, bb->index))
1307 : : {
1308 : 2407571 : if (pred_blocks_visited_p (bb, &visited)
1309 : 2407571 : || bb == loop->header)
1310 : : {
1311 : : /* This block is now visited. */
1312 : 1834308 : bitmap_set_bit (visited, bb->index);
1313 : 1834308 : blocks[visited_count++] = bb;
1314 : : }
1315 : : }
1316 : :
1317 : 3393832 : index++;
1318 : :
1319 : 3393832 : if (index == loop->num_nodes
1320 : 474958 : && visited_count != loop->num_nodes)
1321 : : /* Not done yet. */
1322 : 3393832 : index = 0;
1323 : : }
1324 : 407395 : free (blocks_in_bfs_order);
1325 : 407395 : 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 : 2231148 : for (index = 0; index < loop->num_nodes; index++)
1332 : : {
1333 : 1826226 : basic_block bb = blocks[index];
1334 : 1826226 : gimple_stmt_iterator gsi;
1335 : :
1336 : 1826226 : bool may_have_nonlocal_labels
1337 : 1826226 : = bb_with_exit_edge_p (loop, bb) || bb == loop->latch;
1338 : 16531977 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1339 : 12881998 : switch (gimple_code (gsi_stmt (gsi)))
1340 : : {
1341 : 40629 : case GIMPLE_LABEL:
1342 : 40629 : if (!may_have_nonlocal_labels)
1343 : : {
1344 : 7083 : tree label
1345 : 7083 : = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi)));
1346 : 14166 : if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
1347 : : {
1348 : 47 : free (blocks);
1349 : 47 : return NULL;
1350 : : }
1351 : : }
1352 : : /* Fallthru. */
1353 : 12879525 : case GIMPLE_ASSIGN:
1354 : 12879525 : case GIMPLE_CALL:
1355 : 12879525 : case GIMPLE_DEBUG:
1356 : 12879525 : case GIMPLE_COND:
1357 : 12879525 : case GIMPLE_SWITCH:
1358 : 12879525 : gimple_set_uid (gsi_stmt (gsi), 0);
1359 : 12879525 : break;
1360 : 2426 : default:
1361 : 2426 : free (blocks);
1362 : 2426 : 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 : 37066 : predicate_bbs (loop_p loop)
1388 : : {
1389 : 37066 : unsigned int i;
1390 : :
1391 : 239112 : for (i = 0; i < loop->num_nodes; i++)
1392 : 202046 : init_bb_predicate (ifc_bbs[i]);
1393 : :
1394 : 239112 : for (i = 0; i < loop->num_nodes; i++)
1395 : : {
1396 : 202046 : basic_block bb = ifc_bbs[i];
1397 : 202046 : 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 : 276178 : if (bb == loop->latch
1402 : 202046 : || bb_with_exit_edge_p (loop, bb))
1403 : : {
1404 : 74132 : reset_bb_predicate (bb);
1405 : 74132 : continue;
1406 : : }
1407 : :
1408 : 127914 : cond = bb_predicate (bb);
1409 : 255828 : if (gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb)))
1410 : : {
1411 : 52278 : tree c2;
1412 : 52278 : edge true_edge, false_edge;
1413 : 52278 : location_t loc = gimple_location (stmt);
1414 : 52278 : 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 : 52278 : gimple_match_op cexpr (gimple_match_cond::UNCOND,
1425 : 52278 : gimple_cond_code (stmt),
1426 : : boolean_type_node,
1427 : : gimple_cond_lhs (stmt),
1428 : 52278 : gimple_cond_rhs (stmt));
1429 : 52278 : if (cexpr.resimplify (NULL, follow_all_ssa_edges)
1430 : 3761 : && cexpr.code.is_tree_code ()
1431 : 56039 : && TREE_CODE_CLASS ((tree_code)cexpr.code) == tcc_comparison)
1432 : 570 : c = build2_loc (loc, (tree_code)cexpr.code, boolean_type_node,
1433 : : cexpr.ops[0], cexpr.ops[1]);
1434 : : else
1435 : 51708 : 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 : 52278 : 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 : 52278 : 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 : 52278 : c2 = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
1450 : : unshare_expr (c));
1451 : 52278 : add_to_dst_predicate_list (loop, false_edge,
1452 : : unshare_expr (cond), c2);
1453 : :
1454 : 52278 : cond = NULL_TREE;
1455 : : }
1456 : :
1457 : : /* Assumes the limited COND like switches checked for earlier. */
1458 : 75636 : 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 : 277610 : if (single_succ_p (bb))
1515 : : {
1516 : 75564 : 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 : 75564 : if (cond == NULL_TREE)
1522 : 0 : cond = boolean_true_node;
1523 : :
1524 : 75564 : add_to_predicate_list (loop, bb_n, cond);
1525 : : }
1526 : : }
1527 : :
1528 : : /* The loop header is always executed. */
1529 : 37066 : reset_bb_predicate (loop->header);
1530 : 37066 : gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1531 : : && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1532 : 37066 : }
1533 : :
1534 : : /* Build region by adding loop pre-header and post-header blocks. */
1535 : :
1536 : : static vec<basic_block>
1537 : 37066 : build_region (class loop *loop)
1538 : : {
1539 : 37066 : vec<basic_block> region = vNULL;
1540 : 37066 : basic_block exit_bb = NULL;
1541 : :
1542 : 37066 : gcc_assert (ifc_bbs);
1543 : : /* The first element is loop pre-header. */
1544 : 37066 : region.safe_push (loop_preheader_edge (loop)->src);
1545 : :
1546 : 239112 : for (unsigned int i = 0; i < loop->num_nodes; i++)
1547 : : {
1548 : 202046 : basic_block bb = ifc_bbs[i];
1549 : 202046 : region.safe_push (bb);
1550 : : /* Find loop postheader. */
1551 : 202046 : edge e;
1552 : 202046 : edge_iterator ei;
1553 : 448249 : FOR_EACH_EDGE (e, ei, bb->succs)
1554 : 283269 : if (loop_exit_edge_p (loop, e))
1555 : : {
1556 : 37066 : exit_bb = e->dest;
1557 : 37066 : break;
1558 : : }
1559 : : }
1560 : : /* The last element is loop post-header. */
1561 : 37066 : gcc_assert (exit_bb);
1562 : 37066 : region.safe_push (exit_bb);
1563 : 37066 : 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 : 38492 : if_convertible_loop_p_1 (class loop *loop, vec<data_reference_p> *refs)
1572 : : {
1573 : 38492 : unsigned int i;
1574 : 38492 : basic_block exit_bb = NULL;
1575 : 38492 : vec<basic_block> region;
1576 : :
1577 : 38492 : calculate_dominance_info (CDI_DOMINATORS);
1578 : :
1579 : 245808 : for (i = 0; i < loop->num_nodes; i++)
1580 : : {
1581 : 208742 : basic_block bb = ifc_bbs[i];
1582 : :
1583 : 208742 : if (!if_convertible_bb_p (loop, bb, exit_bb))
1584 : : return false;
1585 : :
1586 : 207316 : if (bb_with_exit_edge_p (loop, bb))
1587 : 38423 : exit_bb = bb;
1588 : : }
1589 : :
1590 : 37066 : data_reference_p dr;
1591 : :
1592 : 37066 : innermost_DR_map
1593 : 37066 : = new hash_map<innermost_loop_behavior_hash, data_reference_p>;
1594 : 37066 : baseref_DR_map = new hash_map<tree_operand_hash, data_reference_p>;
1595 : :
1596 : : /* Compute post-dominator tree locally. */
1597 : 37066 : region = build_region (loop);
1598 : 37066 : calculate_dominance_info_for_region (CDI_POST_DOMINATORS, region);
1599 : :
1600 : 37066 : predicate_bbs (loop);
1601 : :
1602 : : /* Free post-dominator tree since it is not used after predication. */
1603 : 37066 : free_dominance_info_for_region (cfun, CDI_POST_DOMINATORS, region);
1604 : 37066 : region.release ();
1605 : :
1606 : 168549 : for (i = 0; refs->iterate (i, &dr); i++)
1607 : : {
1608 : 131483 : tree ref = DR_REF (dr);
1609 : :
1610 : 131483 : dr->aux = XNEW (struct ifc_dr);
1611 : 131483 : DR_BASE_W_UNCONDITIONALLY (dr) = false;
1612 : 131483 : DR_RW_UNCONDITIONALLY (dr) = false;
1613 : 131483 : DR_W_UNCONDITIONALLY (dr) = false;
1614 : 131483 : IFC_DR (dr)->rw_predicate = boolean_false_node;
1615 : 131483 : IFC_DR (dr)->w_predicate = boolean_false_node;
1616 : 131483 : IFC_DR (dr)->base_w_predicate = boolean_false_node;
1617 : 131483 : if (gimple_uid (DR_STMT (dr)) == 0)
1618 : 130779 : 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 : 131483 : if (TREE_CODE (ref) == COMPONENT_REF
1624 : : || TREE_CODE (ref) == IMAGPART_EXPR
1625 : : || TREE_CODE (ref) == REALPART_EXPR
1626 : 82303 : || !(DR_BASE_ADDRESS (dr) || DR_OFFSET (dr)
1627 : 21182 : || DR_INIT (dr) || DR_STEP (dr)))
1628 : : {
1629 : 127620 : while (TREE_CODE (ref) == COMPONENT_REF
1630 : 71521 : || TREE_CODE (ref) == IMAGPART_EXPR
1631 : 198563 : || TREE_CODE (ref) == REALPART_EXPR)
1632 : 57258 : ref = TREE_OPERAND (ref, 0);
1633 : :
1634 : 70362 : memset (&DR_INNERMOST (dr), 0, sizeof (DR_INNERMOST (dr)));
1635 : 70362 : DR_BASE_ADDRESS (dr) = ref;
1636 : : }
1637 : 131483 : hash_memrefs_baserefs_and_store_DRs_read_written_info (dr);
1638 : : }
1639 : :
1640 : 184279 : for (i = 0; i < loop->num_nodes; i++)
1641 : : {
1642 : 158255 : basic_block bb = ifc_bbs[i];
1643 : 158255 : gimple_stmt_iterator itr;
1644 : :
1645 : : /* Check the if-convertibility of statements in predicated BBs. */
1646 : 158255 : if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1647 : 247291 : for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1648 : 127079 : if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1649 : 12468 : 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 : 159546 : for (i = 0; i < loop->num_nodes; i++)
1655 : : {
1656 : 133522 : basic_block bb = ifc_bbs[i];
1657 : 133522 : gphi_iterator itr;
1658 : :
1659 : 256627 : for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1660 : 123105 : if (!if_convertible_phi_p (loop, bb, itr.phi ()))
1661 : : return false;
1662 : : }
1663 : :
1664 : 26024 : if (dump_file)
1665 : 31 : 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 : 38674 : if_convertible_loop_p (class loop *loop, vec<data_reference_p> *refs)
1680 : : {
1681 : 38674 : edge e;
1682 : 38674 : edge_iterator ei;
1683 : 38674 : bool res = false;
1684 : :
1685 : : /* Handle only innermost loop. */
1686 : 38674 : 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 : 38674 : 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 : 115857 : FOR_EACH_EDGE (e, ei, loop->header->succs)
1704 : 77365 : if (loop_exit_edge_p (loop, e))
1705 : : return false;
1706 : :
1707 : 38492 : res = if_convertible_loop_p_1 (loop, refs);
1708 : :
1709 : 75558 : delete innermost_DR_map;
1710 : 38492 : innermost_DR_map = NULL;
1711 : :
1712 : 75558 : delete baseref_DR_map;
1713 : 38492 : baseref_DR_map = NULL;
1714 : :
1715 : 38492 : 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 : 12114 : strip_nop_cond_scalar_reduction (bool has_nop, tree op)
1726 : : {
1727 : 12114 : if (!has_nop)
1728 : : return op;
1729 : :
1730 : 420 : if (TREE_CODE (op) != SSA_NAME)
1731 : : return NULL_TREE;
1732 : :
1733 : 374 : gassign *stmt = safe_dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
1734 : 374 : if (!stmt
1735 : 374 : || !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 : 165 : 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 : 43225 : 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 : 43225 : tree lhs, r_op1, r_op2, r_nop1, r_nop2;
1763 : 43225 : gimple *stmt;
1764 : 43225 : gimple *header_phi = NULL;
1765 : 43225 : enum tree_code reduction_op;
1766 : 43225 : basic_block bb = gimple_bb (phi);
1767 : 43225 : class loop *loop = bb->loop_father;
1768 : 43225 : edge latch_e = loop_latch_edge (loop);
1769 : 43225 : imm_use_iterator imm_iter;
1770 : 43225 : use_operand_p use_p;
1771 : 43225 : edge e;
1772 : 43225 : edge_iterator ei;
1773 : 43225 : bool result = *has_nop = false;
1774 : 43225 : if (TREE_CODE (arg_0) != SSA_NAME || TREE_CODE (arg_1) != SSA_NAME)
1775 : : return false;
1776 : :
1777 : 27686 : if (!extended && gimple_code (SSA_NAME_DEF_STMT (arg_0)) == GIMPLE_PHI)
1778 : : {
1779 : 7411 : lhs = arg_1;
1780 : 7411 : header_phi = SSA_NAME_DEF_STMT (arg_0);
1781 : 7411 : stmt = SSA_NAME_DEF_STMT (arg_1);
1782 : : }
1783 : 20275 : else if (gimple_code (SSA_NAME_DEF_STMT (arg_1)) == GIMPLE_PHI)
1784 : : {
1785 : 9445 : lhs = arg_0;
1786 : 9445 : header_phi = SSA_NAME_DEF_STMT (arg_1);
1787 : 9445 : stmt = SSA_NAME_DEF_STMT (arg_0);
1788 : : }
1789 : : else
1790 : : return false;
1791 : 16856 : if (gimple_bb (header_phi) != loop->header)
1792 : : return false;
1793 : :
1794 : 15970 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, latch_e) != PHI_RESULT (phi))
1795 : : return false;
1796 : :
1797 : 10064 : if (gimple_code (stmt) != GIMPLE_ASSIGN
1798 : 10064 : || gimple_has_volatile_ops (stmt))
1799 : : return false;
1800 : :
1801 : 9944 : if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
1802 : : return false;
1803 : :
1804 : 9856 : if (!is_predicated (gimple_bb (stmt)))
1805 : : return false;
1806 : :
1807 : : /* Check that stmt-block is predecessor of phi-block. */
1808 : 7718 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1809 : 7655 : if (e->dest == bb)
1810 : : {
1811 : : result = true;
1812 : : break;
1813 : : }
1814 : 7592 : if (!result)
1815 : : return false;
1816 : :
1817 : 7529 : if (!has_single_use (lhs))
1818 : : return false;
1819 : :
1820 : 7485 : 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 : 7485 : if (CONVERT_EXPR_CODE_P (reduction_op))
1843 : : {
1844 : 413 : lhs = gimple_assign_rhs1 (stmt);
1845 : 413 : if (TREE_CODE (lhs) != SSA_NAME
1846 : 413 : || !has_single_use (lhs))
1847 : : return false;
1848 : :
1849 : 225 : *nop_reduc = stmt;
1850 : 225 : stmt = SSA_NAME_DEF_STMT (lhs);
1851 : 225 : if (gimple_bb (stmt) != gimple_bb (*nop_reduc)
1852 : 225 : || !is_gimple_assign (stmt))
1853 : : return false;
1854 : :
1855 : 222 : *has_nop = true;
1856 : 222 : reduction_op = gimple_assign_rhs_code (stmt);
1857 : : }
1858 : :
1859 : 7294 : if (reduction_op != PLUS_EXPR
1860 : : && reduction_op != MINUS_EXPR
1861 : 7294 : && reduction_op != MULT_EXPR
1862 : 7294 : && reduction_op != BIT_IOR_EXPR
1863 : : && reduction_op != BIT_XOR_EXPR
1864 : 1391 : && reduction_op != BIT_AND_EXPR)
1865 : : return false;
1866 : 6057 : r_op1 = gimple_assign_rhs1 (stmt);
1867 : 6057 : r_op2 = gimple_assign_rhs2 (stmt);
1868 : :
1869 : 6057 : r_nop1 = strip_nop_cond_scalar_reduction (*has_nop, r_op1);
1870 : 6057 : r_nop2 = strip_nop_cond_scalar_reduction (*has_nop, r_op2);
1871 : :
1872 : : /* Make R_OP1 to hold reduction variable. */
1873 : 6057 : if (r_nop2 == PHI_RESULT (header_phi)
1874 : 6057 : && commutative_tree_code (reduction_op))
1875 : : {
1876 : : std::swap (r_op1, r_op2);
1877 : : std::swap (r_nop1, r_nop2);
1878 : : }
1879 : 4992 : else if (r_nop1 != PHI_RESULT (header_phi))
1880 : : return false;
1881 : :
1882 : 5743 : if (*has_nop)
1883 : : {
1884 : : /* Check that R_NOP1 is used in nop_stmt or in PHI only. */
1885 : 415 : 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 : : return false;
1894 : : }
1895 : : }
1896 : :
1897 : : /* Check that R_OP1 is used in reduction stmt or in PHI only. */
1898 : 17227 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_op1)
1899 : : {
1900 : 11998 : gimple *use_stmt = USE_STMT (use_p);
1901 : 11998 : if (is_gimple_debug (use_stmt))
1902 : 29 : continue;
1903 : 11969 : if (use_stmt == stmt)
1904 : 5278 : continue;
1905 : 6691 : if (gimple_code (use_stmt) != GIMPLE_PHI)
1906 : : return false;
1907 : : }
1908 : :
1909 : 5229 : *op0 = r_op1; *op1 = r_op2;
1910 : 5229 : *reduc = stmt;
1911 : 5229 : 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 : 5229 : 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 : 5229 : gimple_stmt_iterator stmt_it;
1941 : 5229 : gimple *new_assign;
1942 : 5229 : tree rhs;
1943 : 5229 : tree rhs1 = gimple_assign_rhs1 (reduc);
1944 : 5229 : tree lhs = gimple_assign_lhs (reduc);
1945 : 5229 : tree tmp = make_temp_ssa_name (TREE_TYPE (rhs1), NULL, "_ifc_");
1946 : 5229 : tree c;
1947 : 5229 : enum tree_code reduction_op = gimple_assign_rhs_code (reduc);
1948 : 5229 : tree op_nochange = neutral_op_for_reduction (TREE_TYPE (rhs1), reduction_op,
1949 : : NULL, false);
1950 : 5229 : gimple_seq stmts = NULL;
1951 : :
1952 : 5229 : 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 : 5229 : internal_fn ifn;
1961 : 5229 : ifn = get_conditional_internal_fn (reduction_op);
1962 : 5229 : if (loop_versioned && ifn != IFN_LAST
1963 : 5227 : && vectorized_internal_fn_supported_p (ifn, TREE_TYPE (lhs))
1964 : 6380 : && !swap)
1965 : : {
1966 : 1131 : gcall *cond_call = gimple_build_call_internal (ifn, 4,
1967 : : unshare_expr (cond),
1968 : : op0, op1, op0);
1969 : 1131 : gsi_insert_before (gsi, cond_call, GSI_SAME_STMT);
1970 : 1131 : 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 : 7718 : 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 : 4098 : new_assign = gimple_build_assign (tmp, c);
1983 : 4098 : gsi_insert_before (gsi, new_assign, GSI_SAME_STMT);
1984 : : /* Build rhs for unconditional increment/decrement/logic_operation. */
1985 : 4098 : rhs = gimple_build (&stmts, reduction_op,
1986 : 4098 : TREE_TYPE (rhs1), op0, tmp);
1987 : : }
1988 : :
1989 : 5229 : 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 : 5229 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1998 : :
1999 : : /* Delete original reduction stmt. */
2000 : 5229 : stmt_it = gsi_for_stmt (reduc);
2001 : 5229 : gsi_remove (&stmt_it, true);
2002 : 5229 : release_defs (reduc);
2003 : 5229 : return rhs;
2004 : : }
2005 : :
2006 : : /* Generate a simplified conditional. */
2007 : :
2008 : : static tree
2009 : 48964 : 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 : 48964 : if (TREE_CODE (cond) == SSA_NAME)
2014 : : {
2015 : 48964 : gimple *stmt = SSA_NAME_DEF_STMT (cond);
2016 : 48964 : gassign *assign = NULL;
2017 : 48964 : if ((assign = as_a <gassign *> (stmt))
2018 : 48964 : && gimple_assign_rhs_code (assign) == BIT_AND_EXPR)
2019 : : {
2020 : 3355 : tree arg1 = gimple_assign_rhs1 (assign);
2021 : 3355 : tree arg2 = gimple_assign_rhs2 (assign);
2022 : 3355 : if (cond_set.contains ({ arg1, 1 }))
2023 : 121 : arg1 = boolean_true_node;
2024 : : else
2025 : 3234 : arg1 = gen_simplified_condition (arg1, cond_set);
2026 : :
2027 : 3355 : if (cond_set.contains ({ arg2, 1 }))
2028 : 1784 : arg2 = boolean_true_node;
2029 : : else
2030 : 1571 : arg2 = gen_simplified_condition (arg2, cond_set);
2031 : :
2032 : 3355 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, arg1, arg2);
2033 : : }
2034 : : }
2035 : 48964 : 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 : 4063 : 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 : 4063 : int len;
2067 : 4063 : int i;
2068 : 4063 : tree cond = NULL_TREE;
2069 : 4063 : tree c;
2070 : 4063 : edge e;
2071 : :
2072 : 4063 : *invert = false;
2073 : 4063 : len = arg.indexes->length ();
2074 : 4063 : gcc_assert (len > 0);
2075 : 8164 : for (i = 0; i < len; i++)
2076 : : {
2077 : 4101 : e = gimple_phi_arg_edge (phi, (*arg.indexes)[i]);
2078 : 4101 : c = bb_predicate (e->src);
2079 : 4101 : 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 : 4101 : if (len == 1 && TREE_CODE (c) == TRUTH_NOT_EXPR)
2087 : : {
2088 : 58 : c = TREE_OPERAND (c, 0);
2089 : 58 : *invert = true;
2090 : : }
2091 : :
2092 : 4101 : c = gen_simplified_condition (c, cond_set);
2093 : 4101 : c = force_gimple_operand_gsi (gsi, unshare_expr (c),
2094 : : true, NULL_TREE, true, GSI_SAME_STMT);
2095 : 4101 : if (cond != NULL_TREE)
2096 : : {
2097 : : /* Must build OR expression. */
2098 : 38 : cond = fold_or_predicates (EXPR_LOCATION (c), c, cond);
2099 : 38 : 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 : 4101 : scalar_cond_masked_key pred_cond ({ cond, 1 });
2109 : 4101 : if (!*invert)
2110 : 4043 : pred_cond.inverted_p = !pred_cond.inverted_p;
2111 : 4101 : cond_set.add (pred_cond);
2112 : : }
2113 : 4063 : gcc_assert (cond != NULL_TREE);
2114 : 4063 : 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 : 1095 : 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 : 1095 : unsigned opnum = -1;
2131 : 1095 : unsigned first;
2132 : 1095 : first = first_commutative_argument (arg1_op.code, arg1_op.type);
2133 : 3080 : for (unsigned i = 0; i < arg0_op.num_ops; i++)
2134 : : {
2135 : 2143 : if (!operand_equal_for_phi_arg_p (arg0_op.ops[i],
2136 : 2143 : arg1_op.ops[i]))
2137 : : {
2138 : : /* Can handle only one non equal operand. */
2139 : 1253 : 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 : 158 : if (i == opnum + 1 && opnum == first)
2145 : 109 : 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 : 937 : if (opnum == -1u)
2154 : : {
2155 : 0 : if (arg0_op.num_ops != 1)
2156 : : return -1;
2157 : : opnum = 0;
2158 : : }
2159 : 937 : *new_arg0 = arg0_op.ops[opnum];
2160 : 937 : *new_arg1 = arg1_op.ops[opnum];
2161 : 937 : return opnum;
2162 : :
2163 : : /* Handle commutative operations. */
2164 : 109 : commutative:
2165 : 109 : gcc_assert (first != -1u);
2166 : :
2167 : : /* Check the rest of the arguments to make sure they are the same. */
2168 : 109 : 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 : 109 : if (operand_equal_for_phi_arg_p (arg0_op.ops[first + 1],
2177 : 109 : 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 : 109 : if (operand_equal_for_phi_arg_p (arg0_op.ops[first],
2187 : 109 : arg1_op.ops[first + 1]))
2188 : : {
2189 : 77 : *new_arg0 = arg0_op.ops[first + 1];
2190 : 77 : *new_arg1 = arg1_op.ops[first];
2191 : 77 : 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 : 40058 : factor_out_operators (tree *res, gimple_stmt_iterator *gsi,
2204 : : tree *arg0, tree *arg1, gphi *phi)
2205 : : {
2206 : 40058 : gimple_match_op arg0_op, arg1_op;
2207 : 40058 : bool repeated = false;
2208 : :
2209 : 41070 : again:
2210 : 41070 : if (TREE_CODE (*arg0) != SSA_NAME || TREE_CODE (*arg1) != SSA_NAME)
2211 : : return;
2212 : :
2213 : 26511 : 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 : 26511 : if (repeated
2219 : 26511 : ? (!has_zero_uses (*arg0) || !has_zero_uses (*arg1))
2220 : 25546 : : (!has_single_use (*arg0) || !has_single_use (*arg1)))
2221 : : return;
2222 : :
2223 : 5551 : gimple *arg0_def_stmt = SSA_NAME_DEF_STMT (*arg0);
2224 : 5551 : 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 : 2131 : gcc_assert (!arg0_op.operands_occurs_in_abnormal_phi ());
2229 : :
2230 : 2131 : gimple *arg1_def_stmt = SSA_NAME_DEF_STMT (*arg1);
2231 : 2131 : 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 : 1801 : 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 : 1801 : if (arg1_op.code != arg0_op.code
2240 : 1801 : || arg1_op.num_ops != arg0_op.num_ops)
2241 : : return;
2242 : :
2243 : 1095 : tree new_arg0, new_arg1;
2244 : 1095 : int opnum = find_different_opnum (arg0_op, arg1_op, &new_arg0, &new_arg1);
2245 : 1095 : if (opnum == -1)
2246 : : return;
2247 : :
2248 : 1014 : if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
2249 : : return;
2250 : 1012 : tree new_res = make_ssa_name (TREE_TYPE (new_arg0), NULL);
2251 : :
2252 : : /* Create the operation stmt if possible and insert it. */
2253 : :
2254 : 1012 : gimple_match_op new_op = arg0_op;
2255 : 1012 : new_op.ops[opnum] = new_res;
2256 : 1012 : gimple_seq seq = NULL;
2257 : 1012 : tree result = *res;
2258 : 1012 : result = maybe_push_res_to_seq (&new_op, &seq, result);
2259 : :
2260 : : /* If we can't create the new statement, release the temp name
2261 : : and return back. */
2262 : 1012 : if (!result)
2263 : : {
2264 : 0 : release_ssa_name (new_res);
2265 : 0 : return;
2266 : : }
2267 : 1012 : gsi_insert_seq_before (gsi, seq, GSI_CONTINUE_LINKING);
2268 : :
2269 : 1012 : if (dump_file && (dump_flags & TDF_DETAILS))
2270 : : {
2271 : 7 : fprintf (dump_file, "PHI ");
2272 : 7 : print_generic_expr (dump_file, gimple_phi_result (phi));
2273 : 7 : fprintf (dump_file,
2274 : : " changed to factor operation out from COND_EXPR.\n");
2275 : 7 : fprintf (dump_file, "New stmt with OPERATION that defines ");
2276 : 7 : print_generic_expr (dump_file, result);
2277 : 7 : fprintf (dump_file, ".\n");
2278 : : }
2279 : :
2280 : : /* Remove the old operation(s) that has single use. */
2281 : 1012 : gimple_stmt_iterator gsi_for_def;
2282 : :
2283 : 1012 : gsi_for_def = gsi_for_stmt (arg0_def_stmt);
2284 : 1012 : gsi_remove (&gsi_for_def, true);
2285 : 1012 : release_defs (arg0_def_stmt);
2286 : 1012 : gsi_for_def = gsi_for_stmt (arg1_def_stmt);
2287 : 1012 : gsi_remove (&gsi_for_def, true);
2288 : 1012 : release_defs (arg1_def_stmt);
2289 : :
2290 : : /* Update the arguments and try again. */
2291 : 1012 : *arg0 = new_arg0;
2292 : 1012 : *arg1 = new_arg1;
2293 : 1012 : *res = new_res;
2294 : :
2295 : : /* Update the phi node too. */
2296 : 1012 : gimple_phi_set_result (phi, new_res);
2297 : 1012 : gimple_phi_arg (phi, 0)->def = new_arg0;
2298 : 1012 : gimple_phi_arg (phi, 1)->def = new_arg1;
2299 : 1012 : update_stmt (phi);
2300 : :
2301 : 1012 : repeated = true;
2302 : 1012 : goto again;
2303 : : }
2304 : :
2305 : : /* Create the smallest nested conditional possible. On pre-order we record
2306 : : which conditionals are live, and on post-order rewrite the chain by removing
2307 : : already active conditions.
2308 : :
2309 : : As an example we simplify:
2310 : :
2311 : : _7 = a_10 < 0;
2312 : : _21 = a_10 >= 0;
2313 : : _22 = a_10 < e_11(D);
2314 : : _23 = _21 & _22;
2315 : : _ifc__42 = _23 ? t_13 : 0;
2316 : : t_6 = _7 ? 1 : _ifc__42
2317 : :
2318 : : into
2319 : :
2320 : : _7 = a_10 < 0;
2321 : : _22 = a_10 < e_11(D);
2322 : : _ifc__42 = _22 ? t_13 : 0;
2323 : : t_6 = _7 ? 1 : _ifc__42;
2324 : :
2325 : : which produces better code. */
2326 : :
2327 : : static tree
2328 : 6090 : gen_phi_nest_statement (gphi *phi, gimple_stmt_iterator *gsi,
2329 : : scalar_cond_masked_set_type &cond_set, tree type,
2330 : : gimple **res_stmt, tree lhs0,
2331 : : vec<struct ifcvt_arg_entry> &args, unsigned idx)
2332 : : {
2333 : 12180 : if (idx == args.length ())
2334 : 2027 : return args[idx - 1].arg;
2335 : :
2336 : 4063 : bool invert;
2337 : 4063 : tree cond = gen_phi_arg_condition (phi, args[idx - 1], gsi, cond_set,
2338 : : &invert);
2339 : 4063 : tree arg1 = gen_phi_nest_statement (phi, gsi, cond_set, type, res_stmt, lhs0,
2340 : : args, idx + 1);
2341 : :
2342 : 4063 : unsigned prev = idx;
2343 : 4063 : unsigned curr = prev - 1;
2344 : 4063 : tree arg0 = args[curr].arg;
2345 : 4063 : tree rhs, lhs;
2346 : 4063 : if (idx > 1)
2347 : 2036 : lhs = make_temp_ssa_name (type, NULL, "_ifc_");
2348 : : else
2349 : : lhs = lhs0;
2350 : :
2351 : 4063 : if (invert)
2352 : 58 : rhs = fold_build_cond_expr (type, unshare_expr (cond),
2353 : : arg1, arg0);
2354 : : else
2355 : 4005 : rhs = fold_build_cond_expr (type, unshare_expr (cond),
2356 : : arg0, arg1);
2357 : 4063 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
2358 : 4063 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2359 : 4063 : update_stmt (new_stmt);
2360 : 4063 : *res_stmt = new_stmt;
2361 : 4063 : return lhs;
2362 : : }
2363 : :
2364 : : /* When flattening a PHI node we have a choice of which conditions to test to
2365 : : for all the paths from the start of the dominator block of the BB with the
2366 : : PHI node. If the PHI node has X arguments we have to only test X - 1
2367 : : conditions as the last one is implicit. It does matter which conditions we
2368 : : test first. We should test the shortest condition first (distance here is
2369 : : measures in the number of logical operators in the condition) and the
2370 : : longest one last. This allows us to skip testing the most expensive
2371 : : condition. To accomplish this we need to sort the conditions. P1 and P2
2372 : : are sorted first based on the number of logical operations (num_compares)
2373 : : and then by how often they occur in the PHI node. */
2374 : :
2375 : : static int
2376 : 35188 : cmp_arg_entry (const void *p1, const void *p2, void * /* data. */)
2377 : : {
2378 : 35188 : const ifcvt_arg_entry sval1 = *(const ifcvt_arg_entry *)p1;
2379 : 35188 : const ifcvt_arg_entry sval2 = *(const ifcvt_arg_entry *)p2;
2380 : :
2381 : 35188 : if (sval1.num_compares < sval2.num_compares)
2382 : : return -1;
2383 : 12301 : else if (sval1.num_compares > sval2.num_compares)
2384 : : return 1;
2385 : :
2386 : 541 : if (sval1.occurs < sval2.occurs)
2387 : : return -1;
2388 : 541 : else if (sval1.occurs > sval2.occurs)
2389 : 0 : return 1;
2390 : :
2391 : : return 0;
2392 : : }
2393 : :
2394 : : /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
2395 : : This routine can handle PHI nodes with more than two arguments.
2396 : :
2397 : : For example,
2398 : : S1: A = PHI <x1(1), x2(5)>
2399 : : is converted into,
2400 : : S2: A = cond ? x1 : x2;
2401 : :
2402 : : The generated code is inserted at GSI that points to the top of
2403 : : basic block's statement list.
2404 : : If PHI node has more than two arguments a chain of conditional
2405 : : expression is produced.
2406 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
2407 : : vectorization. */
2408 : :
2409 : :
2410 : : static void
2411 : 45303 : predicate_scalar_phi (gphi *phi, gimple_stmt_iterator *gsi, bool loop_versioned)
2412 : : {
2413 : 45303 : gimple *new_stmt = NULL, *reduc, *nop_reduc;
2414 : 45303 : tree rhs, res, arg0, arg1, op0, op1, scev;
2415 : 45303 : tree cond;
2416 : 45303 : unsigned int index0;
2417 : 45303 : edge e;
2418 : 45303 : basic_block bb;
2419 : 45303 : unsigned int i;
2420 : 45303 : bool has_nop;
2421 : :
2422 : 45303 : res = gimple_phi_result (phi);
2423 : 90606 : if (virtual_operand_p (res))
2424 : 40109 : return;
2425 : :
2426 : 45303 : if ((rhs = degenerate_phi_result (phi))
2427 : 45303 : || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
2428 : : res))
2429 : 45262 : && !chrec_contains_undetermined (scev)
2430 : 45262 : && scev != res
2431 : 10 : && (rhs = gimple_phi_arg_def (phi, 0))))
2432 : : {
2433 : 51 : if (dump_file && (dump_flags & TDF_DETAILS))
2434 : : {
2435 : 0 : fprintf (dump_file, "Degenerate phi!\n");
2436 : 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
2437 : : }
2438 : 51 : new_stmt = gimple_build_assign (res, rhs);
2439 : 51 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2440 : 51 : update_stmt (new_stmt);
2441 : 51 : return;
2442 : : }
2443 : :
2444 : 45252 : bb = gimple_bb (phi);
2445 : : /* Keep track of conditionals already seen. */
2446 : 45252 : scalar_cond_masked_set_type cond_set;
2447 : 45252 : if (EDGE_COUNT (bb->preds) == 2)
2448 : : {
2449 : : /* Predicate ordinary PHI node with 2 arguments. */
2450 : 40058 : edge first_edge, second_edge;
2451 : 40058 : basic_block true_bb;
2452 : 40058 : first_edge = EDGE_PRED (bb, 0);
2453 : 40058 : second_edge = EDGE_PRED (bb, 1);
2454 : 40058 : cond = bb_predicate (first_edge->src);
2455 : 40058 : cond_set.add ({ cond, 1 });
2456 : 40058 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2457 : 21425 : std::swap (first_edge, second_edge);
2458 : 40058 : if (EDGE_COUNT (first_edge->src->succs) > 1)
2459 : : {
2460 : 17748 : cond = bb_predicate (second_edge->src);
2461 : 17748 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2462 : 10249 : cond = TREE_OPERAND (cond, 0);
2463 : : else
2464 : : first_edge = second_edge;
2465 : : }
2466 : : else
2467 : 22310 : cond = bb_predicate (first_edge->src);
2468 : :
2469 : : /* Gimplify the condition to a valid cond-expr conditonal operand. */
2470 : 40058 : cond = gen_simplified_condition (cond, cond_set);
2471 : 40058 : cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
2472 : : NULL_TREE, true, GSI_SAME_STMT);
2473 : 40058 : true_bb = first_edge->src;
2474 : 40058 : if (EDGE_PRED (bb, 1)->src == true_bb)
2475 : : {
2476 : 28924 : arg0 = gimple_phi_arg_def (phi, 1);
2477 : 28924 : arg1 = gimple_phi_arg_def (phi, 0);
2478 : : }
2479 : : else
2480 : : {
2481 : 11134 : arg0 = gimple_phi_arg_def (phi, 0);
2482 : 11134 : arg1 = gimple_phi_arg_def (phi, 1);
2483 : : }
2484 : :
2485 : : /* Factor out operand if possible. This can only be done easily
2486 : : for PHI with 2 elements. */
2487 : 40058 : factor_out_operators (&res, gsi, &arg0, &arg1, phi);
2488 : :
2489 : 40058 : if (is_cond_scalar_reduction (phi, &reduc, arg0, arg1,
2490 : : &op0, &op1, false, &has_nop,
2491 : : &nop_reduc))
2492 : : {
2493 : : /* Convert reduction stmt into vectorizable form. */
2494 : 8558 : rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
2495 : 4279 : true_bb != gimple_bb (reduc),
2496 : : has_nop, nop_reduc,
2497 : : loop_versioned);
2498 : 4279 : redundant_ssa_names.safe_push (std::make_pair (res, rhs));
2499 : : }
2500 : : else
2501 : : /* Build new RHS using selected condition and arguments. */
2502 : 35779 : rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
2503 : : arg0, arg1);
2504 : 40058 : new_stmt = gimple_build_assign (res, rhs);
2505 : 40058 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2506 : 40058 : gimple_stmt_iterator new_gsi = gsi_for_stmt (new_stmt);
2507 : 40058 : if (fold_stmt (&new_gsi, follow_all_ssa_edges))
2508 : : {
2509 : 1587 : new_stmt = gsi_stmt (new_gsi);
2510 : 1587 : update_stmt (new_stmt);
2511 : : }
2512 : :
2513 : 40058 : if (dump_file && (dump_flags & TDF_DETAILS))
2514 : : {
2515 : 17 : fprintf (dump_file, "new phi replacement stmt\n");
2516 : 17 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
2517 : : }
2518 : 40058 : return;
2519 : : }
2520 : :
2521 : : /* Create hashmap for PHI node which contain vector of argument indexes
2522 : : having the same value. */
2523 : 5194 : bool swap = false;
2524 : 5194 : hash_map<tree_operand_hash, auto_vec<int> > phi_arg_map;
2525 : 5194 : unsigned int num_args = gimple_phi_num_args (phi);
2526 : : /* Vector of different PHI argument values. */
2527 : 5194 : auto_vec<ifcvt_arg_entry_t> args;
2528 : :
2529 : : /* Compute phi_arg_map, determine the list of unique PHI args and the indices
2530 : : where they are in the PHI node. The indices will be used to determine
2531 : : the conditions to apply and their complexity. */
2532 : 20941 : for (i = 0; i < num_args; i++)
2533 : : {
2534 : 15747 : tree arg;
2535 : :
2536 : 15747 : arg = gimple_phi_arg_def (phi, i);
2537 : 15747 : if (!phi_arg_map.get (arg))
2538 : 12424 : args.safe_push ({ arg, 0, 0, NULL });
2539 : 15747 : phi_arg_map.get_or_insert (arg).safe_push (i);
2540 : : }
2541 : :
2542 : : /* Determine element with max number of occurrences and complexity. Looking
2543 : : at only number of occurrences as a measure for complexity isn't enough as
2544 : : all usages can be unique but the comparisons to reach the PHI node differ
2545 : : per branch. */
2546 : 17618 : for (unsigned i = 0; i < args.length (); i++)
2547 : : {
2548 : 12424 : unsigned int len = 0;
2549 : 12424 : vec<int> *indices = phi_arg_map.get (args[i].arg);
2550 : 53019 : for (int index : *indices)
2551 : : {
2552 : 15747 : edge e = gimple_phi_arg_edge (phi, index);
2553 : 15747 : len += get_bb_num_predicate_stmts (e->src);
2554 : : }
2555 : :
2556 : 12424 : unsigned occur = indices->length ();
2557 : 12424 : if (dump_file && (dump_flags & TDF_DETAILS))
2558 : 7 : fprintf (dump_file, "Ranking %d as len=%d, idx=%d\n", i, len, occur);
2559 : 12424 : args[i].num_compares = len;
2560 : 12424 : args[i].occurs = occur;
2561 : 12424 : args[i].indexes = indices;
2562 : : }
2563 : :
2564 : : /* Sort elements based on rankings ARGS. */
2565 : 5194 : args.stablesort (cmp_arg_entry, NULL);
2566 : :
2567 : : /* Handle one special case when number of arguments with different values
2568 : : is equal 2 and one argument has the only occurrence. Such PHI can be
2569 : : handled as if would have only 2 arguments. */
2570 : 5194 : if (args.length () == 2
2571 : 8399 : && args[0].indexes->length () == 1)
2572 : : {
2573 : 3167 : index0 = (*args[0].indexes)[0];
2574 : 3167 : arg0 = args[0].arg;
2575 : 3167 : arg1 = args[1].arg;
2576 : 3167 : e = gimple_phi_arg_edge (phi, index0);
2577 : 3167 : cond = bb_predicate (e->src);
2578 : 3167 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2579 : : {
2580 : 21 : swap = true;
2581 : 21 : cond = TREE_OPERAND (cond, 0);
2582 : : }
2583 : : /* Gimplify the condition to a valid cond-expr conditonal operand. */
2584 : 3167 : cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
2585 : : NULL_TREE, true, GSI_SAME_STMT);
2586 : 3167 : if (!(is_cond_scalar_reduction (phi, &reduc, arg0 , arg1,
2587 : : &op0, &op1, true, &has_nop, &nop_reduc)))
2588 : 4415 : rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
2589 : : swap ? arg1 : arg0,
2590 : : swap ? arg0 : arg1);
2591 : : else
2592 : : {
2593 : : /* Convert reduction stmt into vectorizable form. */
2594 : 950 : rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
2595 : : swap, has_nop, nop_reduc,
2596 : : loop_versioned);
2597 : 950 : redundant_ssa_names.safe_push (std::make_pair (res, rhs));
2598 : : }
2599 : 3167 : new_stmt = gimple_build_assign (res, rhs);
2600 : 3167 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
2601 : 3167 : update_stmt (new_stmt);
2602 : : }
2603 : : else
2604 : : {
2605 : : /* Common case. */
2606 : 2027 : tree type = TREE_TYPE (gimple_phi_result (phi));
2607 : 2027 : gen_phi_nest_statement (phi, gsi, cond_set, type, &new_stmt, res,
2608 : : args, 1);
2609 : : }
2610 : :
2611 : 5194 : if (dump_file && (dump_flags & TDF_DETAILS))
2612 : : {
2613 : 3 : fprintf (dump_file, "new extended phi replacement stmt\n");
2614 : 3 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
2615 : : }
2616 : 45252 : }
2617 : :
2618 : : /* Replaces in LOOP all the scalar phi nodes other than those in the
2619 : : LOOP->header block with conditional modify expressions.
2620 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
2621 : : vectorization. */
2622 : :
2623 : : static void
2624 : 26024 : predicate_all_scalar_phis (class loop *loop, bool loop_versioned)
2625 : : {
2626 : 26024 : basic_block bb;
2627 : 26024 : unsigned int orig_loop_num_nodes = loop->num_nodes;
2628 : 26024 : unsigned int i;
2629 : :
2630 : 133522 : for (i = 1; i < orig_loop_num_nodes; i++)
2631 : : {
2632 : 107498 : gphi *phi;
2633 : 107498 : gimple_stmt_iterator gsi;
2634 : 107498 : gphi_iterator phi_gsi;
2635 : 107498 : bb = ifc_bbs[i];
2636 : :
2637 : 107498 : if (bb == loop->header)
2638 : 77077 : continue;
2639 : :
2640 : 107498 : phi_gsi = gsi_start_phis (bb);
2641 : 107498 : if (gsi_end_p (phi_gsi))
2642 : 77077 : continue;
2643 : :
2644 : 30421 : gsi = gsi_after_labels (bb);
2645 : 78030 : while (!gsi_end_p (phi_gsi))
2646 : : {
2647 : 47609 : phi = phi_gsi.phi ();
2648 : 95218 : if (virtual_operand_p (gimple_phi_result (phi)))
2649 : 2306 : gsi_next (&phi_gsi);
2650 : : else
2651 : : {
2652 : 45303 : predicate_scalar_phi (phi, &gsi, loop_versioned);
2653 : 45303 : remove_phi_node (&phi_gsi, false);
2654 : : }
2655 : : }
2656 : : }
2657 : 26024 : }
2658 : :
2659 : : /* Insert in each basic block of LOOP the statements produced by the
2660 : : gimplification of the predicates. */
2661 : :
2662 : : static void
2663 : 26024 : insert_gimplified_predicates (loop_p loop)
2664 : : {
2665 : 26024 : unsigned int i;
2666 : :
2667 : 159546 : for (i = 0; i < loop->num_nodes; i++)
2668 : : {
2669 : 133522 : basic_block bb = ifc_bbs[i];
2670 : 133522 : gimple_seq stmts;
2671 : 133522 : if (!is_predicated (bb))
2672 : 81521 : gcc_assert (bb_predicate_gimplified_stmts (bb) == NULL);
2673 : 133522 : if (!is_predicated (bb))
2674 : : {
2675 : : /* Do not insert statements for a basic block that is not
2676 : : predicated. Also make sure that the predicate of the
2677 : : basic block is set to true. */
2678 : 81521 : reset_bb_predicate (bb);
2679 : 81521 : continue;
2680 : : }
2681 : :
2682 : 52001 : stmts = bb_predicate_gimplified_stmts (bb);
2683 : 52001 : if (stmts)
2684 : : {
2685 : 51657 : if (need_to_predicate)
2686 : : {
2687 : : /* Insert the predicate of the BB just after the label,
2688 : : as the if-conversion of memory writes will use this
2689 : : predicate. */
2690 : 5448 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
2691 : 5448 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
2692 : : }
2693 : : else
2694 : : {
2695 : : /* Insert the predicate of the BB at the end of the BB
2696 : : as this would reduce the register pressure: the only
2697 : : use of this predicate will be in successor BBs. */
2698 : 46209 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2699 : :
2700 : 46209 : if (gsi_end_p (gsi)
2701 : 46209 : || stmt_ends_bb_p (gsi_stmt (gsi)))
2702 : 27971 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
2703 : : else
2704 : 18238 : gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
2705 : : }
2706 : :
2707 : : /* Once the sequence is code generated, set it to NULL. */
2708 : 51657 : set_bb_predicate_gimplified_stmts (bb, NULL, true);
2709 : : }
2710 : : }
2711 : 26024 : }
2712 : :
2713 : : /* Helper function for predicate_statements. Returns index of existent
2714 : : mask if it was created for given SIZE and -1 otherwise. */
2715 : :
2716 : : static int
2717 : 913 : mask_exists (int size, const vec<int> &vec)
2718 : : {
2719 : 913 : unsigned int ix;
2720 : 913 : int v;
2721 : 1002 : FOR_EACH_VEC_ELT (vec, ix, v)
2722 : 941 : if (v == size)
2723 : 852 : return (int) ix;
2724 : : return -1;
2725 : : }
2726 : :
2727 : : /* Helper function for predicate_statements. STMT is a memory read or
2728 : : write and it needs to be predicated by MASK. Return a statement
2729 : : that does so. */
2730 : :
2731 : : static gimple *
2732 : 1845 : predicate_load_or_store (gimple_stmt_iterator *gsi, gassign *stmt, tree mask)
2733 : : {
2734 : 1845 : gcall *new_stmt;
2735 : :
2736 : 1845 : tree lhs = gimple_assign_lhs (stmt);
2737 : 1845 : tree rhs = gimple_assign_rhs1 (stmt);
2738 : 1845 : tree ref = TREE_CODE (lhs) == SSA_NAME ? rhs : lhs;
2739 : 1845 : mark_addressable (ref);
2740 : 1845 : tree addr = force_gimple_operand_gsi (gsi, build_fold_addr_expr (ref),
2741 : : true, NULL_TREE, true, GSI_SAME_STMT);
2742 : 1845 : tree ptr = build_int_cst (reference_alias_ptr_type (ref),
2743 : 1845 : get_object_alignment (ref));
2744 : : /* Copy points-to info if possible. */
2745 : 1845 : if (TREE_CODE (addr) == SSA_NAME && !SSA_NAME_PTR_INFO (addr))
2746 : 394 : copy_ref_info (build2 (MEM_REF, TREE_TYPE (ref), addr, ptr),
2747 : : ref);
2748 : 1845 : if (TREE_CODE (lhs) == SSA_NAME)
2749 : : {
2750 : : /* Get a zero else value. This might not be what a target actually uses
2751 : : but we cannot be sure about which vector mode the vectorizer will
2752 : : choose. Therefore, leave the decision whether we need to force the
2753 : : inactive elements to zero to the vectorizer. */
2754 : 1087 : tree els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
2755 : 1087 : TREE_TYPE (lhs));
2756 : :
2757 : 1087 : new_stmt
2758 : 1087 : = gimple_build_call_internal (IFN_MASK_LOAD, 4, addr,
2759 : : ptr, mask, els);
2760 : :
2761 : 1087 : gimple_call_set_lhs (new_stmt, lhs);
2762 : 2174 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2763 : : }
2764 : : else
2765 : : {
2766 : 758 : new_stmt
2767 : 758 : = gimple_build_call_internal (IFN_MASK_STORE, 4, addr, ptr,
2768 : : mask, rhs);
2769 : 758 : gimple_move_vops (new_stmt, stmt);
2770 : : }
2771 : 1845 : gimple_call_set_nothrow (new_stmt, true);
2772 : 1845 : return new_stmt;
2773 : : }
2774 : :
2775 : : /* STMT uses OP_LHS. Check whether it is equivalent to:
2776 : :
2777 : : ... = OP_MASK ? OP_LHS : X;
2778 : :
2779 : : Return X if so, otherwise return null. OP_MASK is an SSA_NAME that is
2780 : : known to have value OP_COND. */
2781 : :
2782 : : static tree
2783 : 712 : check_redundant_cond_expr (gimple *stmt, tree op_mask, tree op_cond,
2784 : : tree op_lhs)
2785 : : {
2786 : 1397 : gassign *assign = dyn_cast <gassign *> (stmt);
2787 : 918 : if (!assign || gimple_assign_rhs_code (assign) != COND_EXPR)
2788 : : return NULL_TREE;
2789 : :
2790 : 97 : tree use_cond = gimple_assign_rhs1 (assign);
2791 : 97 : tree if_true = gimple_assign_rhs2 (assign);
2792 : 97 : tree if_false = gimple_assign_rhs3 (assign);
2793 : :
2794 : 74 : if ((use_cond == op_mask || operand_equal_p (use_cond, op_cond, 0))
2795 : 97 : && if_true == op_lhs)
2796 : : return if_false;
2797 : :
2798 : 74 : if (inverse_conditions_p (use_cond, op_cond) && if_false == op_lhs)
2799 : : return if_true;
2800 : :
2801 : : return NULL_TREE;
2802 : : }
2803 : :
2804 : : /* Return true if VALUE is available for use at STMT. SSA_NAMES is
2805 : : the set of SSA names defined earlier in STMT's block. */
2806 : :
2807 : : static bool
2808 : 23 : value_available_p (gimple *stmt, hash_set<tree_ssa_name_hash> *ssa_names,
2809 : : tree value)
2810 : : {
2811 : 23 : if (is_gimple_min_invariant (value))
2812 : : return true;
2813 : :
2814 : 23 : if (TREE_CODE (value) == SSA_NAME)
2815 : : {
2816 : 23 : if (SSA_NAME_IS_DEFAULT_DEF (value))
2817 : : return true;
2818 : :
2819 : 23 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
2820 : 23 : basic_block use_bb = gimple_bb (stmt);
2821 : 23 : return (def_bb == use_bb
2822 : 23 : ? ssa_names->contains (value)
2823 : 23 : : dominated_by_p (CDI_DOMINATORS, use_bb, def_bb));
2824 : : }
2825 : :
2826 : : return false;
2827 : : }
2828 : :
2829 : : /* Helper function for predicate_statements. STMT is a potentially-trapping
2830 : : arithmetic operation that needs to be predicated by MASK, an SSA_NAME that
2831 : : has value COND. Return a statement that does so. SSA_NAMES is the set of
2832 : : SSA names defined earlier in STMT's block. */
2833 : :
2834 : : static gimple *
2835 : 507 : predicate_rhs_code (gassign *stmt, tree mask, tree cond,
2836 : : hash_set<tree_ssa_name_hash> *ssa_names)
2837 : : {
2838 : 507 : tree lhs = gimple_assign_lhs (stmt);
2839 : 507 : tree_code code = gimple_assign_rhs_code (stmt);
2840 : 507 : unsigned int nops = gimple_num_ops (stmt);
2841 : 507 : internal_fn cond_fn = get_conditional_internal_fn (code);
2842 : :
2843 : : /* Construct the arguments to the conditional internal function. */
2844 : 507 : auto_vec<tree, 8> args;
2845 : 507 : args.safe_grow (nops + 1, true);
2846 : 507 : args[0] = mask;
2847 : 1521 : for (unsigned int i = 1; i < nops; ++i)
2848 : 1014 : args[i] = gimple_op (stmt, i);
2849 : 507 : args[nops] = NULL_TREE;
2850 : :
2851 : : /* Look for uses of the result to see whether they are COND_EXPRs that can
2852 : : be folded into the conditional call. */
2853 : 507 : imm_use_iterator imm_iter;
2854 : 507 : gimple *use_stmt;
2855 : 1219 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
2856 : : {
2857 : 712 : tree new_else = check_redundant_cond_expr (use_stmt, mask, cond, lhs);
2858 : 712 : if (new_else && value_available_p (stmt, ssa_names, new_else))
2859 : : {
2860 : 2 : if (!args[nops])
2861 : 2 : args[nops] = new_else;
2862 : 2 : if (operand_equal_p (new_else, args[nops], 0))
2863 : : {
2864 : : /* We have:
2865 : :
2866 : : LHS = IFN_COND (MASK, ..., ELSE);
2867 : : X = MASK ? LHS : ELSE;
2868 : :
2869 : : which makes X equivalent to LHS. */
2870 : 2 : tree use_lhs = gimple_assign_lhs (use_stmt);
2871 : 2 : redundant_ssa_names.safe_push (std::make_pair (use_lhs, lhs));
2872 : : }
2873 : : }
2874 : 507 : }
2875 : 507 : if (!args[nops])
2876 : 1010 : args[nops] = targetm.preferred_else_value (cond_fn, TREE_TYPE (lhs),
2877 : 505 : nops - 1, &args[1]);
2878 : :
2879 : : /* Create and insert the call. */
2880 : 507 : gcall *new_stmt = gimple_build_call_internal_vec (cond_fn, args);
2881 : 507 : gimple_call_set_lhs (new_stmt, lhs);
2882 : 507 : gimple_call_set_nothrow (new_stmt, true);
2883 : :
2884 : 507 : return new_stmt;
2885 : 507 : }
2886 : :
2887 : : /* Predicate each write to memory in LOOP.
2888 : :
2889 : : This function transforms control flow constructs containing memory
2890 : : writes of the form:
2891 : :
2892 : : | for (i = 0; i < N; i++)
2893 : : | if (cond)
2894 : : | A[i] = expr;
2895 : :
2896 : : into the following form that does not contain control flow:
2897 : :
2898 : : | for (i = 0; i < N; i++)
2899 : : | A[i] = cond ? expr : A[i];
2900 : :
2901 : : The original CFG looks like this:
2902 : :
2903 : : | bb_0
2904 : : | i = 0
2905 : : | end_bb_0
2906 : : |
2907 : : | bb_1
2908 : : | if (i < N) goto bb_5 else goto bb_2
2909 : : | end_bb_1
2910 : : |
2911 : : | bb_2
2912 : : | cond = some_computation;
2913 : : | if (cond) goto bb_3 else goto bb_4
2914 : : | end_bb_2
2915 : : |
2916 : : | bb_3
2917 : : | A[i] = expr;
2918 : : | goto bb_4
2919 : : | end_bb_3
2920 : : |
2921 : : | bb_4
2922 : : | goto bb_1
2923 : : | end_bb_4
2924 : :
2925 : : insert_gimplified_predicates inserts the computation of the COND
2926 : : expression at the beginning of the destination basic block:
2927 : :
2928 : : | bb_0
2929 : : | i = 0
2930 : : | end_bb_0
2931 : : |
2932 : : | bb_1
2933 : : | if (i < N) goto bb_5 else goto bb_2
2934 : : | end_bb_1
2935 : : |
2936 : : | bb_2
2937 : : | cond = some_computation;
2938 : : | if (cond) goto bb_3 else goto bb_4
2939 : : | end_bb_2
2940 : : |
2941 : : | bb_3
2942 : : | cond = some_computation;
2943 : : | A[i] = expr;
2944 : : | goto bb_4
2945 : : | end_bb_3
2946 : : |
2947 : : | bb_4
2948 : : | goto bb_1
2949 : : | end_bb_4
2950 : :
2951 : : predicate_statements is then predicating the memory write as follows:
2952 : :
2953 : : | bb_0
2954 : : | i = 0
2955 : : | end_bb_0
2956 : : |
2957 : : | bb_1
2958 : : | if (i < N) goto bb_5 else goto bb_2
2959 : : | end_bb_1
2960 : : |
2961 : : | bb_2
2962 : : | if (cond) goto bb_3 else goto bb_4
2963 : : | end_bb_2
2964 : : |
2965 : : | bb_3
2966 : : | cond = some_computation;
2967 : : | A[i] = cond ? expr : A[i];
2968 : : | goto bb_4
2969 : : | end_bb_3
2970 : : |
2971 : : | bb_4
2972 : : | goto bb_1
2973 : : | end_bb_4
2974 : :
2975 : : and finally combine_blocks removes the basic block boundaries making
2976 : : the loop vectorizable:
2977 : :
2978 : : | bb_0
2979 : : | i = 0
2980 : : | if (i < N) goto bb_5 else goto bb_1
2981 : : | end_bb_0
2982 : : |
2983 : : | bb_1
2984 : : | cond = some_computation;
2985 : : | A[i] = cond ? expr : A[i];
2986 : : | if (i < N) goto bb_5 else goto bb_4
2987 : : | end_bb_1
2988 : : |
2989 : : | bb_4
2990 : : | goto bb_1
2991 : : | end_bb_4
2992 : : */
2993 : :
2994 : : static void
2995 : 8847 : predicate_statements (loop_p loop)
2996 : : {
2997 : 8847 : unsigned int i, orig_loop_num_nodes = loop->num_nodes;
2998 : 8847 : auto_vec<int, 1> vect_sizes;
2999 : 8847 : auto_vec<tree, 1> vect_masks;
3000 : 8847 : hash_set<tree_ssa_name_hash> ssa_names;
3001 : :
3002 : 47617 : for (i = 1; i < orig_loop_num_nodes; i++)
3003 : : {
3004 : 38770 : gimple_stmt_iterator gsi;
3005 : 38770 : basic_block bb = ifc_bbs[i];
3006 : 38770 : tree cond = bb_predicate (bb);
3007 : 38770 : bool swap;
3008 : 38770 : int index;
3009 : :
3010 : 38770 : if (is_true_predicate (cond))
3011 : 18498 : continue;
3012 : :
3013 : 20272 : swap = false;
3014 : 20272 : if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
3015 : : {
3016 : 7304 : swap = true;
3017 : 7304 : cond = TREE_OPERAND (cond, 0);
3018 : : }
3019 : :
3020 : 20272 : vect_sizes.truncate (0);
3021 : 20272 : vect_masks.truncate (0);
3022 : :
3023 : 134120 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
3024 : : {
3025 : 93576 : gassign *stmt = dyn_cast <gassign *> (gsi_stmt (gsi));
3026 : 62088 : if (!stmt)
3027 : : ;
3028 : 62088 : else if (is_false_predicate (cond)
3029 : 62088 : && gimple_vdef (stmt))
3030 : : {
3031 : 0 : unlink_stmt_vdef (stmt);
3032 : 0 : gsi_remove (&gsi, true);
3033 : 0 : release_defs (stmt);
3034 : 0 : continue;
3035 : : }
3036 : 62088 : else if (gimple_plf (stmt, GF_PLF_2)
3037 : 62088 : && is_gimple_assign (stmt))
3038 : : {
3039 : 2352 : tree lhs = gimple_assign_lhs (stmt);
3040 : 2352 : tree mask;
3041 : 2352 : gimple *new_stmt;
3042 : 2352 : gimple_seq stmts = NULL;
3043 : 2352 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
3044 : : /* We checked before setting GF_PLF_2 that an equivalent
3045 : : integer mode exists. */
3046 : 2352 : int bitsize = GET_MODE_BITSIZE (mode).to_constant ();
3047 : 2352 : if (!vect_sizes.is_empty ()
3048 : 913 : && (index = mask_exists (bitsize, vect_sizes)) != -1)
3049 : : /* Use created mask. */
3050 : 852 : mask = vect_masks[index];
3051 : : else
3052 : : {
3053 : 1500 : if (COMPARISON_CLASS_P (cond))
3054 : 0 : mask = gimple_build (&stmts, TREE_CODE (cond),
3055 : : boolean_type_node,
3056 : 0 : TREE_OPERAND (cond, 0),
3057 : 0 : TREE_OPERAND (cond, 1));
3058 : : else
3059 : 1500 : mask = cond;
3060 : :
3061 : 1500 : if (swap)
3062 : : {
3063 : 438 : tree true_val
3064 : 438 : = constant_boolean_node (true, TREE_TYPE (mask));
3065 : 438 : mask = gimple_build (&stmts, BIT_XOR_EXPR,
3066 : 438 : TREE_TYPE (mask), mask, true_val);
3067 : : }
3068 : 1500 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3069 : :
3070 : : /* Save mask and its size for further use. */
3071 : 1500 : vect_sizes.safe_push (bitsize);
3072 : 1500 : vect_masks.safe_push (mask);
3073 : : }
3074 : 2352 : if (gimple_assign_single_p (stmt))
3075 : 1845 : new_stmt = predicate_load_or_store (&gsi, stmt, mask);
3076 : : else
3077 : 507 : new_stmt = predicate_rhs_code (stmt, mask, cond, &ssa_names);
3078 : :
3079 : 2352 : gsi_replace (&gsi, new_stmt, true);
3080 : : }
3081 : 59736 : else if (gimple_needing_rewrite_undefined (stmt))
3082 : 9273 : rewrite_to_defined_unconditional (&gsi);
3083 : 100926 : else if (gimple_vdef (stmt))
3084 : : {
3085 : 1552 : tree lhs = gimple_assign_lhs (stmt);
3086 : 1552 : tree rhs = gimple_assign_rhs1 (stmt);
3087 : 1552 : tree type = TREE_TYPE (lhs);
3088 : :
3089 : 1552 : lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
3090 : 1552 : rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
3091 : 1552 : if (swap)
3092 : 551 : std::swap (lhs, rhs);
3093 : 1552 : cond = force_gimple_operand_gsi (&gsi, unshare_expr (cond), true,
3094 : : NULL_TREE, true, GSI_SAME_STMT);
3095 : 1552 : rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
3096 : 1552 : gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
3097 : 1552 : update_stmt (stmt);
3098 : : }
3099 : :
3100 : 93576 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_2)
3101 : 93576 : && is_gimple_call (gsi_stmt (gsi)))
3102 : : {
3103 : : /* Convert functions that have a SIMD clone to IFN_MASK_CALL.
3104 : : This will cause the vectorizer to match the "in branch"
3105 : : clone variants, and serves to build the mask vector
3106 : : in a natural way. */
3107 : 990 : tree mask = cond;
3108 : 990 : gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi));
3109 : 990 : tree orig_fn = gimple_call_fn (call);
3110 : 990 : int orig_nargs = gimple_call_num_args (call);
3111 : 990 : auto_vec<tree> args;
3112 : 990 : args.safe_push (orig_fn);
3113 : 1993 : for (int i = 0; i < orig_nargs; i++)
3114 : 1003 : args.safe_push (gimple_call_arg (call, i));
3115 : : /* If `swap', we invert the mask used for the if branch for use
3116 : : when masking the function call. */
3117 : 990 : if (swap)
3118 : : {
3119 : 944 : gimple_seq stmts = NULL;
3120 : 944 : tree true_val
3121 : 944 : = constant_boolean_node (true, TREE_TYPE (mask));
3122 : 944 : mask = gimple_build (&stmts, BIT_XOR_EXPR,
3123 : 944 : TREE_TYPE (mask), mask, true_val);
3124 : 944 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3125 : : }
3126 : 990 : args.safe_push (mask);
3127 : :
3128 : : /* Replace the call with a IFN_MASK_CALL that has the extra
3129 : : condition parameter. */
3130 : 990 : gcall *new_call = gimple_build_call_internal_vec (IFN_MASK_CALL,
3131 : : args);
3132 : 990 : gimple_call_set_lhs (new_call, gimple_call_lhs (call));
3133 : 990 : gsi_replace (&gsi, new_call, true);
3134 : 990 : }
3135 : :
3136 : 93576 : tree lhs = gimple_get_lhs (gsi_stmt (gsi));
3137 : 93576 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
3138 : 59852 : ssa_names.add (lhs);
3139 : 93576 : gsi_next (&gsi);
3140 : : }
3141 : 40516 : ssa_names.empty ();
3142 : : }
3143 : 8847 : }
3144 : :
3145 : : /* Remove all GIMPLE_CONDs and GIMPLE_LABELs and GIMPLE_SWITCH of all
3146 : : the basic blocks other than the exit and latch of the LOOP. Also
3147 : : resets the GIMPLE_DEBUG information. */
3148 : :
3149 : : static void
3150 : 26024 : remove_conditions_and_labels (loop_p loop)
3151 : : {
3152 : 26024 : gimple_stmt_iterator gsi;
3153 : 26024 : unsigned int i;
3154 : :
3155 : 159546 : for (i = 0; i < loop->num_nodes; i++)
3156 : : {
3157 : 133522 : basic_block bb = ifc_bbs[i];
3158 : :
3159 : 133522 : if (bb_with_exit_edge_p (loop, bb)
3160 : 133522 : || bb == loop->latch)
3161 : 52048 : continue;
3162 : :
3163 : 570457 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
3164 : 407509 : switch (gimple_code (gsi_stmt (gsi)))
3165 : : {
3166 : 33324 : case GIMPLE_COND:
3167 : 33324 : case GIMPLE_LABEL:
3168 : 33324 : case GIMPLE_SWITCH:
3169 : 33324 : gsi_remove (&gsi, true);
3170 : 33324 : break;
3171 : :
3172 : 209930 : case GIMPLE_DEBUG:
3173 : : /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
3174 : 209930 : if (gimple_debug_bind_p (gsi_stmt (gsi)))
3175 : : {
3176 : 159915 : gimple_debug_bind_reset_value (gsi_stmt (gsi));
3177 : 159915 : update_stmt (gsi_stmt (gsi));
3178 : : }
3179 : 209930 : gsi_next (&gsi);
3180 : 209930 : break;
3181 : :
3182 : 164255 : default:
3183 : 164255 : gsi_next (&gsi);
3184 : : }
3185 : : }
3186 : 26024 : }
3187 : :
3188 : : /* Combine all the basic blocks from LOOP into one or two super basic
3189 : : blocks. Replace PHI nodes with conditional modify expressions.
3190 : : LOOP_VERSIONED should be true if we know that the loop was versioned for
3191 : : vectorization. */
3192 : :
3193 : : static void
3194 : 26024 : combine_blocks (class loop *loop, bool loop_versioned)
3195 : : {
3196 : 26024 : basic_block bb, exit_bb, merge_target_bb;
3197 : 26024 : unsigned int orig_loop_num_nodes = loop->num_nodes;
3198 : 26024 : unsigned int i;
3199 : 26024 : edge e;
3200 : 26024 : edge_iterator ei;
3201 : :
3202 : : /* Reset flow-sensitive info before predicating stmts or PHIs we
3203 : : might fold. */
3204 : 159546 : for (i = 0; i < orig_loop_num_nodes; i++)
3205 : : {
3206 : 133522 : bb = ifc_bbs[i];
3207 : 133522 : if (is_predicated (bb))
3208 : : {
3209 : 52001 : for (auto gsi = gsi_start_phis (bb);
3210 : 53420 : !gsi_end_p (gsi); gsi_next (&gsi))
3211 : 1419 : reset_flow_sensitive_info (gimple_phi_result (*gsi));
3212 : 187356 : for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3213 : : {
3214 : 83354 : gimple *stmt = gsi_stmt (gsi);
3215 : 83354 : ssa_op_iter i;
3216 : 83354 : tree op;
3217 : 122824 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
3218 : 39470 : reset_flow_sensitive_info (op);
3219 : : }
3220 : : }
3221 : : }
3222 : :
3223 : 26024 : remove_conditions_and_labels (loop);
3224 : 26024 : insert_gimplified_predicates (loop);
3225 : 26024 : predicate_all_scalar_phis (loop, loop_versioned);
3226 : :
3227 : 26024 : if (need_to_predicate || need_to_rewrite_undefined)
3228 : 8847 : predicate_statements (loop);
3229 : :
3230 : : /* Merge basic blocks. */
3231 : 26024 : exit_bb = single_exit (loop)->src;
3232 : 26024 : gcc_assert (exit_bb != loop->latch);
3233 : 159546 : for (i = 0; i < orig_loop_num_nodes; i++)
3234 : : {
3235 : 133522 : bb = ifc_bbs[i];
3236 : 133522 : free_bb_predicate (bb);
3237 : : }
3238 : :
3239 : 26024 : merge_target_bb = loop->header;
3240 : :
3241 : : /* Get at the virtual def valid for uses starting at the first block
3242 : : we merge into the header. Without a virtual PHI the loop has the
3243 : : same virtual use on all stmts. */
3244 : 26024 : gphi *vphi = get_virtual_phi (loop->header);
3245 : 26024 : tree last_vdef = NULL_TREE;
3246 : 26024 : if (vphi)
3247 : : {
3248 : 14529 : last_vdef = gimple_phi_result (vphi);
3249 : 29058 : for (gimple_stmt_iterator gsi = gsi_start_bb (loop->header);
3250 : 164871 : ! gsi_end_p (gsi); gsi_next (&gsi))
3251 : 232401 : if (gimple_vdef (gsi_stmt (gsi)))
3252 : 5006 : last_vdef = gimple_vdef (gsi_stmt (gsi));
3253 : : }
3254 : 133522 : for (i = 1; i < orig_loop_num_nodes; i++)
3255 : : {
3256 : 107498 : gimple_stmt_iterator gsi;
3257 : 107498 : gimple_stmt_iterator last;
3258 : :
3259 : 107498 : bb = ifc_bbs[i];
3260 : :
3261 : 107498 : if (bb == exit_bb || bb == loop->latch)
3262 : 52048 : continue;
3263 : :
3264 : : /* We release virtual PHIs late because we have to propagate them
3265 : : out using the current VUSE. The def might be the one used
3266 : : after the loop. */
3267 : 55450 : vphi = get_virtual_phi (bb);
3268 : 55450 : if (vphi)
3269 : : {
3270 : : /* When there's just loads inside the loop a stray virtual
3271 : : PHI merging the uses can appear, update last_vdef from
3272 : : it. */
3273 : 557 : if (!last_vdef)
3274 : 0 : last_vdef = gimple_phi_arg_def (vphi, 0);
3275 : 557 : imm_use_iterator iter;
3276 : 557 : use_operand_p use_p;
3277 : 557 : gimple *use_stmt;
3278 : 1839 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
3279 : : {
3280 : 3848 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3281 : 1283 : SET_USE (use_p, last_vdef);
3282 : 557 : }
3283 : 557 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
3284 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
3285 : 557 : gsi = gsi_for_stmt (vphi);
3286 : 557 : remove_phi_node (&gsi, true);
3287 : : }
3288 : :
3289 : : /* Make stmts member of loop->header and clear range info from all stmts
3290 : : in BB which is now no longer executed conditional on a predicate we
3291 : : could have derived it from. */
3292 : 304283 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3293 : : {
3294 : 193383 : gimple *stmt = gsi_stmt (gsi);
3295 : 193383 : gimple_set_bb (stmt, merge_target_bb);
3296 : : /* Update virtual operands. */
3297 : 193383 : if (last_vdef)
3298 : : {
3299 : 140527 : use_operand_p use_p = ssa_vuse_operand (stmt);
3300 : 13702 : if (use_p
3301 : 13702 : && USE_FROM_PTR (use_p) != last_vdef)
3302 : 720 : SET_USE (use_p, last_vdef);
3303 : 447094 : if (gimple_vdef (stmt))
3304 : 193383 : last_vdef = gimple_vdef (stmt);
3305 : : }
3306 : : else
3307 : : /* If this is the first load we arrive at update last_vdef
3308 : : so we handle stray PHIs correctly. */
3309 : 227610 : last_vdef = gimple_vuse (stmt);
3310 : : }
3311 : :
3312 : : /* Update stmt list. */
3313 : 55450 : last = gsi_last_bb (merge_target_bb);
3314 : 110900 : gsi_insert_seq_after_without_update (&last, bb_seq (bb), GSI_NEW_STMT);
3315 : 55450 : set_bb_seq (bb, NULL);
3316 : : }
3317 : :
3318 : : /* Fixup virtual operands in the exit block. */
3319 : 26024 : if (exit_bb
3320 : 26024 : && exit_bb != loop->header)
3321 : : {
3322 : : /* We release virtual PHIs late because we have to propagate them
3323 : : out using the current VUSE. The def might be the one used
3324 : : after the loop. */
3325 : 26024 : vphi = get_virtual_phi (exit_bb);
3326 : 26024 : if (vphi)
3327 : : {
3328 : : /* When there's just loads inside the loop a stray virtual
3329 : : PHI merging the uses can appear, update last_vdef from
3330 : : it. */
3331 : 1749 : if (!last_vdef)
3332 : 0 : last_vdef = gimple_phi_arg_def (vphi, 0);
3333 : 1749 : imm_use_iterator iter;
3334 : 1749 : use_operand_p use_p;
3335 : 1749 : gimple *use_stmt;
3336 : 5264 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
3337 : : {
3338 : 10545 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3339 : 3515 : SET_USE (use_p, last_vdef);
3340 : 1749 : }
3341 : 1749 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
3342 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
3343 : 1749 : gimple_stmt_iterator gsi = gsi_for_stmt (vphi);
3344 : 1749 : remove_phi_node (&gsi, true);
3345 : : }
3346 : : }
3347 : :
3348 : : /* Now remove all the edges in the loop, except for those from the exit
3349 : : block and delete the blocks we elided. */
3350 : 133522 : for (i = 1; i < orig_loop_num_nodes; i++)
3351 : : {
3352 : 107498 : bb = ifc_bbs[i];
3353 : :
3354 : 247923 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
3355 : : {
3356 : 140425 : if (e->src == exit_bb)
3357 : 26024 : ei_next (&ei);
3358 : : else
3359 : 114401 : remove_edge (e);
3360 : : }
3361 : : }
3362 : 133522 : for (i = 1; i < orig_loop_num_nodes; i++)
3363 : : {
3364 : 107498 : bb = ifc_bbs[i];
3365 : :
3366 : 107498 : if (bb == exit_bb || bb == loop->latch)
3367 : 52048 : continue;
3368 : :
3369 : 55450 : delete_basic_block (bb);
3370 : : }
3371 : :
3372 : : /* Re-connect the exit block. */
3373 : 26024 : if (exit_bb != NULL)
3374 : : {
3375 : 26024 : if (exit_bb != loop->header)
3376 : : {
3377 : : /* Connect this node to loop header. */
3378 : 26024 : make_single_succ_edge (loop->header, exit_bb, EDGE_FALLTHRU);
3379 : 26024 : set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
3380 : : }
3381 : :
3382 : : /* Redirect non-exit edges to loop->latch. */
3383 : 78072 : FOR_EACH_EDGE (e, ei, exit_bb->succs)
3384 : : {
3385 : 52048 : if (!loop_exit_edge_p (loop, e))
3386 : 26024 : redirect_edge_and_branch (e, loop->latch);
3387 : : }
3388 : 26024 : set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
3389 : : }
3390 : : else
3391 : : {
3392 : : /* If the loop does not have an exit, reconnect header and latch. */
3393 : 0 : make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
3394 : 0 : set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
3395 : : }
3396 : :
3397 : : /* If possible, merge loop header to the block with the exit edge.
3398 : : This reduces the number of basic blocks to two, to please the
3399 : : vectorizer that handles only loops with two nodes. */
3400 : 26024 : if (exit_bb
3401 : 26024 : && exit_bb != loop->header)
3402 : : {
3403 : 26024 : if (can_merge_blocks_p (loop->header, exit_bb))
3404 : 26022 : merge_blocks (loop->header, exit_bb);
3405 : : }
3406 : :
3407 : 26024 : free (ifc_bbs);
3408 : 26024 : ifc_bbs = NULL;
3409 : 26024 : }
3410 : :
3411 : : /* Version LOOP before if-converting it; the original loop
3412 : : will be if-converted, the new copy of the loop will not,
3413 : : and the LOOP_VECTORIZED internal call will be guarding which
3414 : : loop to execute. The vectorizer pass will fold this
3415 : : internal call into either true or false.
3416 : :
3417 : : Note that this function intentionally invalidates profile. Both edges
3418 : : out of LOOP_VECTORIZED must have 100% probability so the profile remains
3419 : : consistent after the condition is folded in the vectorizer. */
3420 : :
3421 : : static class loop *
3422 : 26213 : version_loop_for_if_conversion (class loop *loop, vec<gimple *> *preds)
3423 : : {
3424 : 26213 : basic_block cond_bb;
3425 : 26213 : tree cond = make_ssa_name (boolean_type_node);
3426 : 26213 : class loop *new_loop;
3427 : 26213 : gimple *g;
3428 : 26213 : gimple_stmt_iterator gsi;
3429 : 26213 : unsigned int save_length = 0;
3430 : :
3431 : 26213 : g = gimple_build_call_internal (IFN_LOOP_VECTORIZED, 2,
3432 : 26213 : build_int_cst (integer_type_node, loop->num),
3433 : : integer_zero_node);
3434 : 26213 : gimple_call_set_lhs (g, cond);
3435 : :
3436 : 26213 : void **saved_preds = NULL;
3437 : 26213 : if (any_complicated_phi || need_to_predicate)
3438 : : {
3439 : : /* Save BB->aux around loop_version as that uses the same field. */
3440 : 3337 : save_length = loop->inner ? loop->inner->num_nodes : loop->num_nodes;
3441 : 3337 : saved_preds = XALLOCAVEC (void *, save_length);
3442 : 25010 : for (unsigned i = 0; i < save_length; i++)
3443 : 21673 : saved_preds[i] = ifc_bbs[i]->aux;
3444 : : }
3445 : :
3446 : 26213 : initialize_original_copy_tables ();
3447 : : /* At this point we invalidate porfile confistency until IFN_LOOP_VECTORIZED
3448 : : is re-merged in the vectorizer. */
3449 : 26213 : new_loop = loop_version (loop, cond, &cond_bb,
3450 : : profile_probability::always (),
3451 : : profile_probability::always (),
3452 : : profile_probability::always (),
3453 : : profile_probability::always (), true);
3454 : 26213 : free_original_copy_tables ();
3455 : :
3456 : 26213 : if (any_complicated_phi || need_to_predicate)
3457 : 25010 : for (unsigned i = 0; i < save_length; i++)
3458 : 21673 : ifc_bbs[i]->aux = saved_preds[i];
3459 : :
3460 : 26213 : if (new_loop == NULL)
3461 : : return NULL;
3462 : :
3463 : 26213 : new_loop->dont_vectorize = true;
3464 : 26213 : new_loop->force_vectorize = false;
3465 : 26213 : gsi = gsi_last_bb (cond_bb);
3466 : 26213 : gimple_call_set_arg (g, 1, build_int_cst (integer_type_node, new_loop->num));
3467 : 26213 : if (preds)
3468 : 26213 : preds->safe_push (g);
3469 : 26213 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3470 : 26213 : update_ssa (TODO_update_ssa_no_phi);
3471 : 26213 : return new_loop;
3472 : : }
3473 : :
3474 : : /* Return true when LOOP satisfies the follow conditions that will
3475 : : allow it to be recognized by the vectorizer for outer-loop
3476 : : vectorization:
3477 : : - The loop is not the root node of the loop tree.
3478 : : - The loop has exactly one inner loop.
3479 : : - The loop has a single exit.
3480 : : - The loop header has a single successor, which is the inner
3481 : : loop header.
3482 : : - Each of the inner and outer loop latches have a single
3483 : : predecessor.
3484 : : - The loop exit block has a single predecessor, which is the
3485 : : inner loop's exit block. */
3486 : :
3487 : : static bool
3488 : 26213 : versionable_outer_loop_p (class loop *loop)
3489 : : {
3490 : 26213 : if (!loop_outer (loop)
3491 : 6573 : || loop->dont_vectorize
3492 : 5618 : || !loop->inner
3493 : 5618 : || loop->inner->next
3494 : 2575 : || !single_exit (loop)
3495 : 2085 : || !single_succ_p (loop->header)
3496 : 915 : || single_succ (loop->header) != loop->inner->header
3497 : 915 : || !single_pred_p (loop->latch)
3498 : 32786 : || !single_pred_p (loop->inner->latch))
3499 : 25298 : return false;
3500 : :
3501 : 915 : basic_block outer_exit = single_pred (loop->latch);
3502 : 915 : basic_block inner_exit = single_pred (loop->inner->latch);
3503 : :
3504 : 27121 : if (!single_pred_p (outer_exit) || single_pred (outer_exit) != inner_exit)
3505 : : return false;
3506 : :
3507 : 907 : if (dump_file)
3508 : 0 : fprintf (dump_file, "Found vectorizable outer loop for versioning\n");
3509 : :
3510 : : return true;
3511 : : }
3512 : :
3513 : : /* Performs splitting of critical edges. Skip splitting and return false
3514 : : if LOOP will not be converted because:
3515 : :
3516 : : - LOOP is not well formed.
3517 : : - LOOP has PHI with more than MAX_PHI_ARG_NUM arguments.
3518 : :
3519 : : Last restriction is valid only if AGGRESSIVE_IF_CONV is false. */
3520 : :
3521 : : static bool
3522 : 322653 : ifcvt_split_critical_edges (class loop *loop, bool aggressive_if_conv)
3523 : : {
3524 : 322653 : basic_block *body;
3525 : 322653 : basic_block bb;
3526 : 322653 : unsigned int num = loop->num_nodes;
3527 : 322653 : unsigned int i;
3528 : 322653 : edge e;
3529 : 322653 : edge_iterator ei;
3530 : 322653 : auto_vec<edge> critical_edges;
3531 : :
3532 : : /* Loop is not well formed. */
3533 : 322653 : if (loop->inner)
3534 : : return false;
3535 : :
3536 : 228703 : body = get_loop_body (loop);
3537 : 1887639 : for (i = 0; i < num; i++)
3538 : : {
3539 : 1434177 : bb = body[i];
3540 : 1434177 : if (!aggressive_if_conv
3541 : 1425972 : && phi_nodes (bb)
3542 : 1871031 : && EDGE_COUNT (bb->preds) > MAX_PHI_ARG_NUM)
3543 : : {
3544 : 3944 : if (dump_file && (dump_flags & TDF_DETAILS))
3545 : 0 : fprintf (dump_file,
3546 : : "BB %d has complicated PHI with more than %u args.\n",
3547 : : bb->index, MAX_PHI_ARG_NUM);
3548 : :
3549 : 3944 : free (body);
3550 : 3944 : return false;
3551 : : }
3552 : 1430233 : if (bb == loop->latch || bb_with_exit_edge_p (loop, bb))
3553 : 827938 : continue;
3554 : :
3555 : : /* Skip basic blocks not ending with conditional branch. */
3556 : 1429243 : if (!safe_is_a <gcond *> (*gsi_last_bb (bb))
3557 : 602295 : && !safe_is_a <gswitch *> (*gsi_last_bb (bb)))
3558 : 339749 : continue;
3559 : :
3560 : 789704 : FOR_EACH_EDGE (e, ei, bb->succs)
3561 : 645381 : if (EDGE_CRITICAL_P (e) && e->dest->loop_father == loop)
3562 : 118223 : critical_edges.safe_push (e);
3563 : : }
3564 : 224759 : free (body);
3565 : :
3566 : 438872 : while (critical_edges.length () > 0)
3567 : : {
3568 : 116219 : e = critical_edges.pop ();
3569 : : /* Don't split if bb can be predicated along non-critical edge. */
3570 : 116219 : if (EDGE_COUNT (e->dest->preds) > 2 || all_preds_critical_p (e->dest))
3571 : 50927 : split_edge (e);
3572 : : }
3573 : :
3574 : : return true;
3575 : 322653 : }
3576 : :
3577 : : /* Delete redundant statements produced by predication which prevents
3578 : : loop vectorization. */
3579 : :
3580 : : static void
3581 : 26261 : ifcvt_local_dce (class loop *loop)
3582 : : {
3583 : 26261 : gimple *stmt;
3584 : 26261 : gimple *stmt1;
3585 : 26261 : gimple *phi;
3586 : 26261 : gimple_stmt_iterator gsi;
3587 : 26261 : auto_vec<gimple *> worklist;
3588 : 26261 : enum gimple_code code;
3589 : 26261 : use_operand_p use_p;
3590 : 26261 : imm_use_iterator imm_iter;
3591 : :
3592 : : /* The loop has a single BB only. */
3593 : 26261 : basic_block bb = loop->header;
3594 : 26261 : tree latch_vdef = NULL_TREE;
3595 : :
3596 : 26261 : worklist.create (64);
3597 : : /* Consider all phi as live statements. */
3598 : 102424 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3599 : : {
3600 : 76163 : phi = gsi_stmt (gsi);
3601 : 76163 : gimple_set_plf (phi, GF_PLF_2, true);
3602 : 76163 : worklist.safe_push (phi);
3603 : 167026 : if (virtual_operand_p (gimple_phi_result (phi)))
3604 : 14700 : latch_vdef = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
3605 : : }
3606 : : /* Consider load/store statements, CALL and COND as live. */
3607 : 715354 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3608 : : {
3609 : 662832 : stmt = gsi_stmt (gsi);
3610 : 662832 : if (is_gimple_debug (stmt))
3611 : : {
3612 : 300636 : gimple_set_plf (stmt, GF_PLF_2, true);
3613 : 300636 : continue;
3614 : : }
3615 : 362196 : if (gimple_store_p (stmt) || gimple_assign_load_p (stmt))
3616 : : {
3617 : 72833 : gimple_set_plf (stmt, GF_PLF_2, true);
3618 : 72833 : worklist.safe_push (stmt);
3619 : 72833 : continue;
3620 : : }
3621 : 289363 : code = gimple_code (stmt);
3622 : 289363 : if (code == GIMPLE_COND || code == GIMPLE_CALL || code == GIMPLE_SWITCH)
3623 : : {
3624 : 31373 : gimple_set_plf (stmt, GF_PLF_2, true);
3625 : 31373 : worklist.safe_push (stmt);
3626 : 31373 : continue;
3627 : : }
3628 : 257990 : gimple_set_plf (stmt, GF_PLF_2, false);
3629 : :
3630 : 257990 : if (code == GIMPLE_ASSIGN)
3631 : : {
3632 : 257981 : tree lhs = gimple_assign_lhs (stmt);
3633 : 603398 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
3634 : : {
3635 : 362760 : stmt1 = USE_STMT (use_p);
3636 : 362760 : if (!is_gimple_debug (stmt1) && gimple_bb (stmt1) != bb)
3637 : : {
3638 : 17343 : gimple_set_plf (stmt, GF_PLF_2, true);
3639 : 17343 : worklist.safe_push (stmt);
3640 : 17343 : break;
3641 : : }
3642 : : }
3643 : : }
3644 : : }
3645 : : /* Propagate liveness through arguments of live stmt. */
3646 : 450915 : while (worklist.length () > 0)
3647 : : {
3648 : 424654 : ssa_op_iter iter;
3649 : 424654 : use_operand_p use_p;
3650 : 424654 : tree use;
3651 : :
3652 : 424654 : stmt = worklist.pop ();
3653 : 1500760 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3654 : : {
3655 : 651452 : use = USE_FROM_PTR (use_p);
3656 : 651452 : if (TREE_CODE (use) != SSA_NAME)
3657 : 39840 : continue;
3658 : 611612 : stmt1 = SSA_NAME_DEF_STMT (use);
3659 : 611612 : if (gimple_bb (stmt1) != bb || gimple_plf (stmt1, GF_PLF_2))
3660 : 384670 : continue;
3661 : 226942 : gimple_set_plf (stmt1, GF_PLF_2, true);
3662 : 226942 : worklist.safe_push (stmt1);
3663 : : }
3664 : : }
3665 : : /* Delete dead statements. */
3666 : 26261 : gsi = gsi_last_bb (bb);
3667 : 689093 : while (!gsi_end_p (gsi))
3668 : : {
3669 : 662832 : gimple_stmt_iterator gsiprev = gsi;
3670 : 662832 : gsi_prev (&gsiprev);
3671 : 662832 : stmt = gsi_stmt (gsi);
3672 : 662832 : if (!gimple_has_volatile_ops (stmt)
3673 : 662646 : && gimple_store_p (stmt)
3674 : 375784 : && gimple_vdef (stmt))
3675 : : {
3676 : 19927 : tree lhs = gimple_get_lhs (stmt);
3677 : 19927 : ao_ref write;
3678 : 19927 : ao_ref_init (&write, lhs);
3679 : :
3680 : 19927 : if (dse_classify_store (&write, stmt, false, NULL, NULL, latch_vdef)
3681 : : == DSE_STORE_DEAD)
3682 : 342 : delete_dead_or_redundant_assignment (&gsi, "dead");
3683 : 19927 : gsi = gsiprev;
3684 : 19927 : continue;
3685 : 19927 : }
3686 : :
3687 : 642905 : if (gimple_plf (stmt, GF_PLF_2))
3688 : : {
3689 : 629200 : gsi = gsiprev;
3690 : 629200 : continue;
3691 : : }
3692 : 13705 : if (dump_file && (dump_flags & TDF_DETAILS))
3693 : : {
3694 : 16 : fprintf (dump_file, "Delete dead stmt in bb#%d\n", bb->index);
3695 : 16 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3696 : : }
3697 : 13705 : gsi_remove (&gsi, true);
3698 : 13705 : release_defs (stmt);
3699 : 13705 : gsi = gsiprev;
3700 : : }
3701 : 26261 : }
3702 : :
3703 : : /* Return true if VALUE is already available on edge PE. */
3704 : :
3705 : : static bool
3706 : 220834 : ifcvt_available_on_edge_p (edge pe, tree value)
3707 : : {
3708 : 220834 : if (is_gimple_min_invariant (value))
3709 : : return true;
3710 : :
3711 : 215188 : if (TREE_CODE (value) == SSA_NAME)
3712 : : {
3713 : 214151 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
3714 : 214151 : if (!def_bb || dominated_by_p (CDI_DOMINATORS, pe->dest, def_bb))
3715 : 23403 : return true;
3716 : : }
3717 : :
3718 : : return false;
3719 : : }
3720 : :
3721 : : /* Return true if STMT can be hoisted from if-converted loop LOOP to
3722 : : edge PE. */
3723 : :
3724 : : static bool
3725 : 648785 : ifcvt_can_hoist (class loop *loop, edge pe, gimple *stmt)
3726 : : {
3727 : 648785 : if (auto *call = dyn_cast<gcall *> (stmt))
3728 : : {
3729 : 5118 : if (gimple_call_internal_p (call)
3730 : 5118 : && internal_fn_mask_index (gimple_call_internal_fn (call)) >= 0)
3731 : : return false;
3732 : : }
3733 : 961313 : else if (auto *assign = dyn_cast<gassign *> (stmt))
3734 : : {
3735 : 390311 : if (gimple_assign_rhs_code (assign) == COND_EXPR)
3736 : : return false;
3737 : : }
3738 : : else
3739 : : return false;
3740 : :
3741 : 280643 : if (gimple_has_side_effects (stmt)
3742 : 279383 : || gimple_could_trap_p (stmt)
3743 : 203694 : || stmt_could_throw_p (cfun, stmt)
3744 : 203692 : || gimple_vdef (stmt)
3745 : 483649 : || gimple_vuse (stmt))
3746 : 78663 : return false;
3747 : :
3748 : 201980 : int num_args = gimple_num_args (stmt);
3749 : 201980 : if (pe != loop_preheader_edge (loop))
3750 : : {
3751 : 224825 : for (int i = 0; i < num_args; ++i)
3752 : 220834 : if (!ifcvt_available_on_edge_p (pe, gimple_arg (stmt, i)))
3753 : : return false;
3754 : : }
3755 : : else
3756 : : {
3757 : 7859 : for (int i = 0; i < num_args; ++i)
3758 : 7604 : if (!expr_invariant_in_loop_p (loop, gimple_arg (stmt, i)))
3759 : : return false;
3760 : : }
3761 : :
3762 : : return true;
3763 : : }
3764 : :
3765 : : /* Hoist invariant statements from LOOP to edge PE. */
3766 : :
3767 : : static void
3768 : 26261 : ifcvt_hoist_invariants (class loop *loop, edge pe)
3769 : : {
3770 : : /* Only hoist from the now unconditionally executed part of the loop. */
3771 : 26261 : basic_block bb = loop->header;
3772 : 26261 : gimple_stmt_iterator hoist_gsi = {};
3773 : 701307 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
3774 : : {
3775 : 648785 : gimple *stmt = gsi_stmt (gsi);
3776 : 648785 : if (ifcvt_can_hoist (loop, pe, stmt))
3777 : : {
3778 : : /* Once we've hoisted one statement, insert other statements
3779 : : after it. */
3780 : 4246 : gsi_remove (&gsi, false);
3781 : 4246 : if (hoist_gsi.ptr)
3782 : 1869 : gsi_insert_after (&hoist_gsi, stmt, GSI_NEW_STMT);
3783 : : else
3784 : : {
3785 : 2377 : gsi_insert_on_edge_immediate (pe, stmt);
3786 : 2377 : hoist_gsi = gsi_for_stmt (stmt);
3787 : : }
3788 : 4246 : continue;
3789 : : }
3790 : 644539 : gsi_next (&gsi);
3791 : : }
3792 : 26261 : }
3793 : :
3794 : : /* Returns the DECL_FIELD_BIT_OFFSET of the bitfield accesse in stmt iff its
3795 : : type mode is not BLKmode. If BITPOS is not NULL it will hold the poly_int64
3796 : : value of the DECL_FIELD_BIT_OFFSET of the bitfield access and STRUCT_EXPR,
3797 : : if not NULL, will hold the tree representing the base struct of this
3798 : : bitfield. */
3799 : :
3800 : : static tree
3801 : 1186 : get_bitfield_rep (gassign *stmt, bool write, tree *bitpos,
3802 : : tree *struct_expr)
3803 : : {
3804 : 1186 : tree comp_ref = write ? gimple_assign_lhs (stmt)
3805 : 396 : : gimple_assign_rhs1 (stmt);
3806 : :
3807 : 1186 : tree field_decl = TREE_OPERAND (comp_ref, 1);
3808 : 1186 : tree ref_offset = component_ref_field_offset (comp_ref);
3809 : 1186 : tree rep_decl = DECL_BIT_FIELD_REPRESENTATIVE (field_decl);
3810 : :
3811 : : /* Bail out if the representative is not a suitable type for a scalar
3812 : : register variable. */
3813 : 1186 : if (!is_gimple_reg_type (TREE_TYPE (rep_decl)))
3814 : : return NULL_TREE;
3815 : :
3816 : : /* Bail out if the DECL_SIZE of the field_decl isn't the same as the BF's
3817 : : precision. */
3818 : 1170 : unsigned HOST_WIDE_INT bf_prec
3819 : 1170 : = TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)));
3820 : 1170 : if (compare_tree_int (DECL_SIZE (field_decl), bf_prec) != 0)
3821 : : return NULL_TREE;
3822 : :
3823 : 1170 : if (TREE_CODE (DECL_FIELD_OFFSET (rep_decl)) != INTEGER_CST
3824 : 1170 : || TREE_CODE (ref_offset) != INTEGER_CST)
3825 : : {
3826 : 2 : if (dump_file && (dump_flags & TDF_DETAILS))
3827 : 2 : fprintf (dump_file, "\t Bitfield NOT OK to lower,"
3828 : : " offset is non-constant.\n");
3829 : 2 : return NULL_TREE;
3830 : : }
3831 : :
3832 : 1168 : if (struct_expr)
3833 : 584 : *struct_expr = TREE_OPERAND (comp_ref, 0);
3834 : :
3835 : 1168 : if (bitpos)
3836 : : {
3837 : : /* To calculate the bitposition of the BITFIELD_REF we have to determine
3838 : : where our bitfield starts in relation to the container REP_DECL. The
3839 : : DECL_FIELD_OFFSET of the original bitfield's member FIELD_DECL tells
3840 : : us how many bytes from the start of the structure there are until the
3841 : : start of the group of bitfield members the FIELD_DECL belongs to,
3842 : : whereas DECL_FIELD_BIT_OFFSET will tell us how many bits from that
3843 : : position our actual bitfield member starts. For the container
3844 : : REP_DECL adding DECL_FIELD_OFFSET and DECL_FIELD_BIT_OFFSET will tell
3845 : : us the distance between the start of the structure and the start of
3846 : : the container, though the first is in bytes and the later other in
3847 : : bits. With this in mind we calculate the bit position of our new
3848 : : BITFIELD_REF by subtracting the number of bits between the start of
3849 : : the structure and the container from the number of bits from the start
3850 : : of the structure and the actual bitfield member. */
3851 : 584 : tree bf_pos = fold_build2 (MULT_EXPR, bitsizetype,
3852 : : ref_offset,
3853 : : build_int_cst (bitsizetype, BITS_PER_UNIT));
3854 : 584 : bf_pos = fold_build2 (PLUS_EXPR, bitsizetype, bf_pos,
3855 : : DECL_FIELD_BIT_OFFSET (field_decl));
3856 : 584 : tree rep_pos = fold_build2 (MULT_EXPR, bitsizetype,
3857 : : DECL_FIELD_OFFSET (rep_decl),
3858 : : build_int_cst (bitsizetype, BITS_PER_UNIT));
3859 : 584 : rep_pos = fold_build2 (PLUS_EXPR, bitsizetype, rep_pos,
3860 : : DECL_FIELD_BIT_OFFSET (rep_decl));
3861 : :
3862 : 584 : *bitpos = fold_build2 (MINUS_EXPR, bitsizetype, bf_pos, rep_pos);
3863 : : }
3864 : :
3865 : : return rep_decl;
3866 : :
3867 : : }
3868 : :
3869 : : /* Lowers the bitfield described by DATA.
3870 : : For a write like:
3871 : :
3872 : : struct.bf = _1;
3873 : :
3874 : : lower to:
3875 : :
3876 : : __ifc_1 = struct.<representative>;
3877 : : __ifc_2 = BIT_INSERT_EXPR (__ifc_1, _1, bitpos);
3878 : : struct.<representative> = __ifc_2;
3879 : :
3880 : : For a read:
3881 : :
3882 : : _1 = struct.bf;
3883 : :
3884 : : lower to:
3885 : :
3886 : : __ifc_1 = struct.<representative>;
3887 : : _1 = BIT_FIELD_REF (__ifc_1, bitsize, bitpos);
3888 : :
3889 : : where representative is a legal load that contains the bitfield value,
3890 : : bitsize is the size of the bitfield and bitpos the offset to the start of
3891 : : the bitfield within the representative. */
3892 : :
3893 : : static void
3894 : 584 : lower_bitfield (gassign *stmt, bool write)
3895 : : {
3896 : 584 : tree struct_expr;
3897 : 584 : tree bitpos;
3898 : 584 : tree rep_decl = get_bitfield_rep (stmt, write, &bitpos, &struct_expr);
3899 : 584 : tree rep_type = TREE_TYPE (rep_decl);
3900 : 584 : tree bf_type = TREE_TYPE (gimple_assign_lhs (stmt));
3901 : :
3902 : 584 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3903 : 584 : if (dump_file && (dump_flags & TDF_DETAILS))
3904 : : {
3905 : 9 : fprintf (dump_file, "Lowering:\n");
3906 : 9 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3907 : 9 : fprintf (dump_file, "to:\n");
3908 : : }
3909 : :
3910 : : /* REP_COMP_REF is a COMPONENT_REF for the representative. NEW_VAL is it's
3911 : : defining SSA_NAME. */
3912 : 584 : tree rep_comp_ref = build3 (COMPONENT_REF, rep_type, struct_expr, rep_decl,
3913 : : NULL_TREE);
3914 : 584 : tree new_val = ifc_temp_var (rep_type, rep_comp_ref, &gsi);
3915 : :
3916 : 584 : if (dump_file && (dump_flags & TDF_DETAILS))
3917 : 9 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
3918 : :
3919 : 584 : if (write)
3920 : : {
3921 : 390 : new_val = ifc_temp_var (rep_type,
3922 : : build3 (BIT_INSERT_EXPR, rep_type, new_val,
3923 : : unshare_expr (gimple_assign_rhs1 (stmt)),
3924 : : bitpos), &gsi);
3925 : :
3926 : 390 : if (dump_file && (dump_flags & TDF_DETAILS))
3927 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
3928 : :
3929 : 390 : gimple *new_stmt = gimple_build_assign (unshare_expr (rep_comp_ref),
3930 : : new_val);
3931 : 390 : gimple_move_vops (new_stmt, stmt);
3932 : 390 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
3933 : :
3934 : 390 : if (dump_file && (dump_flags & TDF_DETAILS))
3935 : 0 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
3936 : : }
3937 : : else
3938 : : {
3939 : 194 : tree bfr = build3 (BIT_FIELD_REF, bf_type, new_val,
3940 : 194 : build_int_cst (bitsizetype, TYPE_PRECISION (bf_type)),
3941 : : bitpos);
3942 : 194 : new_val = ifc_temp_var (bf_type, bfr, &gsi);
3943 : :
3944 : 194 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (stmt),
3945 : : new_val);
3946 : 194 : gimple_move_vops (new_stmt, stmt);
3947 : 194 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
3948 : :
3949 : 194 : if (dump_file && (dump_flags & TDF_DETAILS))
3950 : 9 : print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
3951 : : }
3952 : :
3953 : 584 : gsi_remove (&gsi, true);
3954 : 584 : }
3955 : :
3956 : : /* Return TRUE if there are bitfields to lower in this LOOP. Fill TO_LOWER
3957 : : with data structures representing these bitfields. */
3958 : :
3959 : : static bool
3960 : 236351 : bitfields_to_lower_p (class loop *loop,
3961 : : vec <gassign *> &reads_to_lower,
3962 : : vec <gassign *> &writes_to_lower)
3963 : : {
3964 : 236351 : gimple_stmt_iterator gsi;
3965 : :
3966 : 236351 : if (dump_file && (dump_flags & TDF_DETAILS))
3967 : : {
3968 : 29 : fprintf (dump_file, "Analyzing loop %d for bitfields:\n", loop->num);
3969 : : }
3970 : :
3971 : 1005311 : for (unsigned i = 0; i < loop->num_nodes; ++i)
3972 : : {
3973 : 768982 : basic_block bb = ifc_bbs[i];
3974 : 6376974 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3975 : : {
3976 : 4839032 : gassign *stmt = dyn_cast<gassign*> (gsi_stmt (gsi));
3977 : 4839032 : if (!stmt)
3978 : 4713139 : continue;
3979 : :
3980 : 2018678 : tree op = gimple_assign_lhs (stmt);
3981 : 2018678 : bool write = TREE_CODE (op) == COMPONENT_REF;
3982 : :
3983 : 2018678 : if (!write)
3984 : 1988620 : op = gimple_assign_rhs1 (stmt);
3985 : :
3986 : 2018678 : if (TREE_CODE (op) != COMPONENT_REF)
3987 : 1892785 : continue;
3988 : :
3989 : 125893 : if (DECL_BIT_FIELD_TYPE (TREE_OPERAND (op, 1)))
3990 : : {
3991 : 606 : if (dump_file && (dump_flags & TDF_DETAILS))
3992 : 11 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3993 : :
3994 : 606 : if (TREE_THIS_VOLATILE (op))
3995 : : {
3996 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
3997 : 0 : fprintf (dump_file, "\t Bitfield NO OK to lower,"
3998 : : " the access is volatile.\n");
3999 : 22 : return false;
4000 : : }
4001 : :
4002 : 602 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op)))
4003 : : {
4004 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4005 : 0 : fprintf (dump_file, "\t Bitfield NO OK to lower,"
4006 : : " field type is not Integral.\n");
4007 : 0 : return false;
4008 : : }
4009 : :
4010 : 602 : if (!get_bitfield_rep (stmt, write, NULL, NULL))
4011 : : {
4012 : 18 : if (dump_file && (dump_flags & TDF_DETAILS))
4013 : 2 : fprintf (dump_file, "\t Bitfield NOT OK to lower,"
4014 : : " representative is BLKmode.\n");
4015 : 18 : return false;
4016 : : }
4017 : :
4018 : 584 : if (dump_file && (dump_flags & TDF_DETAILS))
4019 : 9 : fprintf (dump_file, "\tBitfield OK to lower.\n");
4020 : 584 : if (write)
4021 : 390 : writes_to_lower.safe_push (stmt);
4022 : : else
4023 : 194 : reads_to_lower.safe_push (stmt);
4024 : : }
4025 : : }
4026 : : }
4027 : 472516 : return !reads_to_lower.is_empty () || !writes_to_lower.is_empty ();
4028 : : }
4029 : :
4030 : :
4031 : : /* If-convert LOOP when it is legal. For the moment this pass has no
4032 : : profitability analysis. Returns non-zero todo flags when something
4033 : : changed. */
4034 : :
4035 : : unsigned int
4036 : 504425 : tree_if_conversion (class loop *loop, vec<gimple *> *preds)
4037 : : {
4038 : 504425 : unsigned int todo = 0;
4039 : 504425 : bool aggressive_if_conv;
4040 : 504425 : class loop *rloop;
4041 : 504425 : auto_vec <gassign *, 4> reads_to_lower;
4042 : 504425 : auto_vec <gassign *, 4> writes_to_lower;
4043 : 504425 : bitmap exit_bbs;
4044 : 504425 : edge pe;
4045 : 504425 : auto_vec<data_reference_p, 10> refs;
4046 : 505332 : bool loop_versioned;
4047 : :
4048 : 505332 : again:
4049 : 505332 : rloop = NULL;
4050 : 505332 : ifc_bbs = NULL;
4051 : 505332 : need_to_lower_bitfields = false;
4052 : 505332 : need_to_ifcvt = false;
4053 : 505332 : need_to_predicate = false;
4054 : 505332 : need_to_rewrite_undefined = false;
4055 : 505332 : any_complicated_phi = false;
4056 : 505332 : loop_versioned = false;
4057 : :
4058 : : /* Apply more aggressive if-conversion when loop or its outer loop were
4059 : : marked with simd pragma. When that's the case, we try to if-convert
4060 : : loop containing PHIs with more than MAX_PHI_ARG_NUM arguments. */
4061 : 505332 : aggressive_if_conv = loop->force_vectorize;
4062 : 505332 : if (!aggressive_if_conv)
4063 : : {
4064 : 497005 : class loop *outer_loop = loop_outer (loop);
4065 : 497005 : if (outer_loop && outer_loop->force_vectorize)
4066 : 8643 : aggressive_if_conv = true;
4067 : : }
4068 : :
4069 : : /* If there are more than two BBs in the loop then there is at least one if
4070 : : to convert. */
4071 : 505332 : if (loop->num_nodes > 2
4072 : 505332 : && !ifcvt_split_critical_edges (loop, aggressive_if_conv))
4073 : 97894 : goto cleanup;
4074 : :
4075 : 407438 : ifc_bbs = get_loop_body_in_if_conv_order (loop);
4076 : 407438 : if (!ifc_bbs)
4077 : : {
4078 : 2516 : if (dump_file && (dump_flags & TDF_DETAILS))
4079 : 3 : fprintf (dump_file, "Irreducible loop\n");
4080 : 2516 : goto cleanup;
4081 : : }
4082 : :
4083 : 404922 : if (find_data_references_in_loop (loop, &refs) == chrec_dont_know)
4084 : 155868 : goto cleanup;
4085 : :
4086 : 249054 : if (loop->num_nodes > 2)
4087 : : {
4088 : : /* More than one loop exit is too much to handle. */
4089 : 137519 : if (!single_exit (loop))
4090 : : {
4091 : 98845 : if (dump_file && (dump_flags & TDF_DETAILS))
4092 : 10 : fprintf (dump_file, "Can not ifcvt due to multiple exits\n");
4093 : : }
4094 : : else
4095 : : {
4096 : 38674 : need_to_ifcvt = true;
4097 : :
4098 : 38674 : if (!if_convertible_loop_p (loop, &refs)
4099 : 38674 : || !dbg_cnt (if_conversion_tree))
4100 : 12650 : goto cleanup;
4101 : :
4102 : 26024 : if ((need_to_predicate || any_complicated_phi)
4103 : 3337 : && ((!flag_tree_loop_vectorize && !loop->force_vectorize)
4104 : 3337 : || loop->dont_vectorize))
4105 : 0 : goto cleanup;
4106 : : }
4107 : : }
4108 : :
4109 : 236404 : if ((flag_tree_loop_vectorize || loop->force_vectorize)
4110 : 236351 : && !loop->dont_vectorize)
4111 : 236351 : need_to_lower_bitfields = bitfields_to_lower_p (loop, reads_to_lower,
4112 : : writes_to_lower);
4113 : :
4114 : 236404 : if (!need_to_ifcvt && !need_to_lower_bitfields)
4115 : 210143 : goto cleanup;
4116 : :
4117 : : /* The edge to insert invariant stmts on. */
4118 : 26261 : pe = loop_preheader_edge (loop);
4119 : :
4120 : : /* Since we have no cost model, always version loops unless the user
4121 : : specified -ftree-loop-if-convert or unless versioning is required.
4122 : : Either version this loop, or if the pattern is right for outer-loop
4123 : : vectorization, version the outer loop. In the latter case we will
4124 : : still if-convert the original inner loop. */
4125 : 26261 : if (need_to_lower_bitfields
4126 : 26022 : || need_to_predicate
4127 : 24033 : || any_complicated_phi
4128 : 22687 : || flag_tree_loop_if_convert != 1)
4129 : : {
4130 : 26213 : class loop *vloop
4131 : 26213 : = (versionable_outer_loop_p (loop_outer (loop))
4132 : 26213 : ? loop_outer (loop) : loop);
4133 : 26213 : class loop *nloop = version_loop_for_if_conversion (vloop, preds);
4134 : 26213 : if (nloop == NULL)
4135 : 0 : goto cleanup;
4136 : 26213 : if (vloop != loop)
4137 : : {
4138 : : /* If versionable_outer_loop_p decided to version the
4139 : : outer loop, version also the inner loop of the non-vectorized
4140 : : loop copy. So we transform:
4141 : : loop1
4142 : : loop2
4143 : : into:
4144 : : if (LOOP_VECTORIZED (1, 3))
4145 : : {
4146 : : loop1
4147 : : loop2
4148 : : }
4149 : : else
4150 : : loop3 (copy of loop1)
4151 : : if (LOOP_VECTORIZED (4, 5))
4152 : : loop4 (copy of loop2)
4153 : : else
4154 : : loop5 (copy of loop4) */
4155 : 907 : gcc_assert (nloop->inner && nloop->inner->next == NULL);
4156 : : rloop = nloop->inner;
4157 : : }
4158 : : else
4159 : : /* If we versioned loop then make sure to insert invariant
4160 : : stmts before the .LOOP_VECTORIZED check since the vectorizer
4161 : : will re-use that for things like runtime alias versioning
4162 : : whose condition can end up using those invariants. */
4163 : 25306 : pe = single_pred_edge (gimple_bb (preds->last ()));
4164 : :
4165 : : loop_versioned = true;
4166 : : }
4167 : :
4168 : 26261 : if (need_to_lower_bitfields)
4169 : : {
4170 : 239 : if (dump_file && (dump_flags & TDF_DETAILS))
4171 : : {
4172 : 9 : fprintf (dump_file, "-------------------------\n");
4173 : 9 : fprintf (dump_file, "Start lowering bitfields\n");
4174 : : }
4175 : 433 : while (!reads_to_lower.is_empty ())
4176 : 194 : lower_bitfield (reads_to_lower.pop (), false);
4177 : 629 : while (!writes_to_lower.is_empty ())
4178 : 390 : lower_bitfield (writes_to_lower.pop (), true);
4179 : :
4180 : 239 : if (dump_file && (dump_flags & TDF_DETAILS))
4181 : : {
4182 : 9 : fprintf (dump_file, "Done lowering bitfields\n");
4183 : 9 : fprintf (dump_file, "-------------------------\n");
4184 : : }
4185 : : }
4186 : 26261 : if (need_to_ifcvt)
4187 : : {
4188 : : /* Before we rewrite edges we'll record their original position in the
4189 : : edge map such that we can map the edges between the ifcvt and the
4190 : : non-ifcvt loop during peeling. */
4191 : 26024 : uintptr_t idx = 0;
4192 : 104096 : for (edge exit : get_loop_exit_edges (loop))
4193 : 26024 : exit->aux = (void*)idx++;
4194 : :
4195 : : /* Now all statements are if-convertible. Combine all the basic
4196 : : blocks into one huge basic block doing the if-conversion
4197 : : on-the-fly. */
4198 : 26024 : combine_blocks (loop, loop_versioned);
4199 : : }
4200 : :
4201 : : std::pair <tree, tree> *name_pair;
4202 : : unsigned ssa_names_idx;
4203 : 31492 : FOR_EACH_VEC_ELT (redundant_ssa_names, ssa_names_idx, name_pair)
4204 : 5231 : replace_uses_by (name_pair->first, name_pair->second);
4205 : 26261 : redundant_ssa_names.release ();
4206 : :
4207 : : /* Perform local CSE, this esp. helps the vectorizer analysis if loads
4208 : : and stores are involved. CSE only the loop body, not the entry
4209 : : PHIs, those are to be kept in sync with the non-if-converted copy.
4210 : : ??? We'll still keep dead stores though. */
4211 : 26261 : exit_bbs = BITMAP_ALLOC (NULL);
4212 : 105197 : for (edge exit : get_loop_exit_edges (loop))
4213 : 26432 : bitmap_set_bit (exit_bbs, exit->dest->index);
4214 : 26261 : todo |= do_rpo_vn (cfun, loop_preheader_edge (loop), exit_bbs,
4215 : : false, true, true);
4216 : :
4217 : : /* Delete dead predicate computations. */
4218 : 26261 : ifcvt_local_dce (loop);
4219 : 26261 : BITMAP_FREE (exit_bbs);
4220 : :
4221 : 26261 : ifcvt_hoist_invariants (loop, pe);
4222 : :
4223 : 26261 : todo |= TODO_cleanup_cfg;
4224 : :
4225 : 505332 : cleanup:
4226 : 505332 : data_reference_p dr;
4227 : 505332 : unsigned int i;
4228 : 1665324 : for (i = 0; refs.iterate (i, &dr); i++)
4229 : : {
4230 : 1159992 : free (dr->aux);
4231 : 1159992 : free_data_ref (dr);
4232 : : }
4233 : 505332 : refs.truncate (0);
4234 : :
4235 : 505332 : if (ifc_bbs)
4236 : : {
4237 : : unsigned int i;
4238 : :
4239 : 2067839 : for (i = 0; i < loop->num_nodes; i++)
4240 : 1688941 : free_bb_predicate (ifc_bbs[i]);
4241 : :
4242 : 378898 : free (ifc_bbs);
4243 : 378898 : ifc_bbs = NULL;
4244 : : }
4245 : 505332 : if (rloop != NULL)
4246 : : {
4247 : 907 : loop = rloop;
4248 : 907 : reads_to_lower.truncate (0);
4249 : 907 : writes_to_lower.truncate (0);
4250 : 907 : goto again;
4251 : : }
4252 : :
4253 : 504425 : return todo;
4254 : 504425 : }
4255 : :
4256 : : /* Tree if-conversion pass management. */
4257 : :
4258 : : namespace {
4259 : :
4260 : : const pass_data pass_data_if_conversion =
4261 : : {
4262 : : GIMPLE_PASS, /* type */
4263 : : "ifcvt", /* name */
4264 : : OPTGROUP_NONE, /* optinfo_flags */
4265 : : TV_TREE_LOOP_IFCVT, /* tv_id */
4266 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4267 : : 0, /* properties_provided */
4268 : : 0, /* properties_destroyed */
4269 : : 0, /* todo_flags_start */
4270 : : 0, /* todo_flags_finish */
4271 : : };
4272 : :
4273 : : class pass_if_conversion : public gimple_opt_pass
4274 : : {
4275 : : public:
4276 : 285689 : pass_if_conversion (gcc::context *ctxt)
4277 : 571378 : : gimple_opt_pass (pass_data_if_conversion, ctxt)
4278 : : {}
4279 : :
4280 : : /* opt_pass methods: */
4281 : : bool gate (function *) final override;
4282 : : unsigned int execute (function *) final override;
4283 : :
4284 : : }; // class pass_if_conversion
4285 : :
4286 : : bool
4287 : 241718 : pass_if_conversion::gate (function *fun)
4288 : : {
4289 : 33834 : return (((flag_tree_loop_vectorize || fun->has_force_vectorize_loops)
4290 : 209691 : && flag_tree_loop_if_convert != 0)
4291 : 273754 : || flag_tree_loop_if_convert == 1);
4292 : : }
4293 : :
4294 : : unsigned int
4295 : 209703 : pass_if_conversion::execute (function *fun)
4296 : : {
4297 : 209703 : unsigned todo = 0;
4298 : :
4299 : 419406 : if (number_of_loops (fun) <= 1)
4300 : : return 0;
4301 : :
4302 : 209703 : auto_vec<gimple *> preds;
4303 : 1140468 : for (auto loop : loops_list (cfun, 0))
4304 : 511359 : if (flag_tree_loop_if_convert == 1
4305 : 511116 : || ((flag_tree_loop_vectorize || loop->force_vectorize)
4306 : 508295 : && !loop->dont_vectorize))
4307 : 504425 : todo |= tree_if_conversion (loop, &preds);
4308 : :
4309 : 209703 : if (todo)
4310 : : {
4311 : 17384 : free_numbers_of_iterations_estimates (fun);
4312 : 17384 : scev_reset ();
4313 : : }
4314 : :
4315 : 209703 : if (flag_checking)
4316 : : {
4317 : 209699 : basic_block bb;
4318 : 7548829 : FOR_EACH_BB_FN (bb, fun)
4319 : 7339130 : gcc_assert (!bb->aux);
4320 : : }
4321 : :
4322 : : /* Perform IL update now, it might elide some loops. */
4323 : 209703 : if (todo & TODO_cleanup_cfg)
4324 : : {
4325 : 17384 : cleanup_tree_cfg ();
4326 : 17384 : if (need_ssa_update_p (fun))
4327 : 0 : todo |= TODO_update_ssa;
4328 : : }
4329 : 209703 : if (todo & TODO_update_ssa_any)
4330 : 0 : update_ssa (todo & TODO_update_ssa_any);
4331 : :
4332 : : /* If if-conversion elided the loop fall back to the original one. Likewise
4333 : : if the loops are not nested in the same outer loop. */
4334 : 235916 : for (unsigned i = 0; i < preds.length (); ++i)
4335 : : {
4336 : 26213 : gimple *g = preds[i];
4337 : 26213 : if (!gimple_bb (g))
4338 : 0 : continue;
4339 : 26213 : auto ifcvt_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 0)));
4340 : 26213 : auto orig_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 1)));
4341 : 26213 : if (!ifcvt_loop || !orig_loop)
4342 : : {
4343 : 2 : if (dump_file)
4344 : 0 : fprintf (dump_file, "If-converted loop vanished\n");
4345 : 2 : fold_loop_internal_call (g, boolean_false_node);
4346 : : }
4347 : 26211 : else if (loop_outer (ifcvt_loop) != loop_outer (orig_loop))
4348 : : {
4349 : 0 : if (dump_file)
4350 : 0 : fprintf (dump_file, "If-converted loop in different outer loop\n");
4351 : 0 : fold_loop_internal_call (g, boolean_false_node);
4352 : : }
4353 : : }
4354 : :
4355 : 209703 : return 0;
4356 : 209703 : }
4357 : :
4358 : : } // anon namespace
4359 : :
4360 : : gimple_opt_pass *
4361 : 285689 : make_pass_if_conversion (gcc::context *ctxt)
4362 : : {
4363 : 285689 : return new pass_if_conversion (ctxt);
4364 : : }
|