]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-if-conv.c
cfgloop.h (struct loop): Rename force_vect into force_vectorize.
[thirdparty/gcc.git] / gcc / tree-if-conv.c
CommitLineData
40923b20 1/* If-conversion for vectorizer.
23a5b65a 2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
40923b20
DP
3 Contributed by Devang Patel <dpatel@apple.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
40923b20
DP
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
40923b20 20
98c07c54
SP
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.
40923b20
DP
24
25 A short description of if-conversion:
26
e0ddb4bd 27 o Decide if a loop is if-convertible or not.
40923b20
DP
28 o Walk all loop basic blocks in breadth first order (BFS order).
29 o Remove conditional statements (at the end of basic block)
61b5f210 30 and propagate condition into destination basic blocks'
40923b20
DP
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];
61b5f210 70
40923b20
DP
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>;
61b5f210 76
40923b20
DP
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 "tm.h"
40923b20 87#include "tree.h"
d8a2d370 88#include "stor-layout.h"
40923b20 89#include "flags.h"
40923b20 90#include "basic-block.h"
cf835838 91#include "gimple-pretty-print.h"
2fb9a547
AM
92#include "tree-ssa-alias.h"
93#include "internal-fn.h"
94#include "gimple-fold.h"
95#include "gimple-expr.h"
96#include "is-a.h"
18f429e2 97#include "gimple.h"
45b0be94 98#include "gimplify.h"
5be5c238 99#include "gimple-iterator.h"
18f429e2 100#include "gimplify-me.h"
442b4905
AM
101#include "gimple-ssa.h"
102#include "tree-cfg.h"
103#include "tree-phinodes.h"
104#include "ssa-iterators.h"
d8a2d370 105#include "stringpool.h"
442b4905
AM
106#include "tree-ssanames.h"
107#include "tree-into-ssa.h"
7a300452 108#include "tree-ssa.h"
40923b20
DP
109#include "cfgloop.h"
110#include "tree-chrec.h"
111#include "tree-data-ref.h"
112#include "tree-scalar-evolution.h"
5ce9450f
JJ
113#include "tree-ssa-loop-ivopts.h"
114#include "tree-ssa-address.h"
40923b20 115#include "tree-pass.h"
53aa40a8 116#include "dbgcnt.h"
5ce9450f
JJ
117#include "expr.h"
118#include "optabs.h"
40923b20 119
40923b20
DP
120/* List of basic blocks in if-conversion-suitable order. */
121static basic_block *ifc_bbs;
122
7b14477e
SP
123/* Structure used to predicate basic blocks. This is attached to the
124 ->aux field of the BBs in the loop to be if-converted. */
125typedef struct bb_predicate_s {
126
127 /* The condition under which this basic block is executed. */
128 tree predicate;
129
130 /* PREDICATE is gimplified, and the sequence of statements is
131 recorded here, in order to avoid the duplication of computations
132 that occur in previous conditions. See PR44483. */
133 gimple_seq predicate_gimplified_stmts;
134} *bb_predicate_p;
135
136/* Returns true when the basic block BB has a predicate. */
137
138static inline bool
139bb_has_predicate (basic_block bb)
140{
141 return bb->aux != NULL;
142}
143
144/* Returns the gimplified predicate for basic block BB. */
145
146static inline tree
147bb_predicate (basic_block bb)
148{
149 return ((bb_predicate_p) bb->aux)->predicate;
150}
151
152/* Sets the gimplified predicate COND for basic block BB. */
153
154static inline void
155set_bb_predicate (basic_block bb, tree cond)
156{
747633c5
RG
157 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
158 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
159 || is_gimple_condexpr (cond));
7b14477e
SP
160 ((bb_predicate_p) bb->aux)->predicate = cond;
161}
162
163/* Returns the sequence of statements of the gimplification of the
164 predicate for basic block BB. */
165
166static inline gimple_seq
167bb_predicate_gimplified_stmts (basic_block bb)
168{
169 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
170}
171
172/* Sets the sequence of statements STMTS of the gimplification of the
173 predicate for basic block BB. */
174
175static inline void
176set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
177{
178 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
179}
180
181/* Adds the sequence of statements STMTS to the sequence of statements
182 of the predicate for basic block BB. */
183
184static inline void
185add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
186{
187 gimple_seq_add_seq
188 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
189}
190
191/* Initializes to TRUE the predicate of basic block BB. */
192
193static inline void
194init_bb_predicate (basic_block bb)
195{
196 bb->aux = XNEW (struct bb_predicate_s);
197 set_bb_predicate_gimplified_stmts (bb, NULL);
29caa68a 198 set_bb_predicate (bb, boolean_true_node);
7b14477e
SP
199}
200
5ce9450f
JJ
201/* Release the SSA_NAMEs associated with the predicate of basic block BB,
202 but don't actually free it. */
7b14477e
SP
203
204static inline void
5ce9450f 205release_bb_predicate (basic_block bb)
7b14477e 206{
5ce9450f 207 gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
7b14477e
SP
208 if (stmts)
209 {
210 gimple_stmt_iterator i;
211
212 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
6a58ccca 213 free_stmt_operands (cfun, gsi_stmt (i));
5ce9450f 214 set_bb_predicate_gimplified_stmts (bb, NULL);
7b14477e 215 }
5ce9450f
JJ
216}
217
218/* Free the predicate of basic block BB. */
7b14477e 219
5ce9450f
JJ
220static inline void
221free_bb_predicate (basic_block bb)
222{
223 if (!bb_has_predicate (bb))
224 return;
225
226 release_bb_predicate (bb);
7b14477e
SP
227 free (bb->aux);
228 bb->aux = NULL;
229}
230
5ce9450f 231/* Reinitialize predicate of BB with the true predicate. */
29caa68a
SP
232
233static inline void
234reset_bb_predicate (basic_block bb)
235{
5ce9450f
JJ
236 if (!bb_has_predicate (bb))
237 init_bb_predicate (bb);
238 else
239 {
240 release_bb_predicate (bb);
241 set_bb_predicate (bb, boolean_true_node);
242 }
29caa68a
SP
243}
244
bd544141
SP
245/* Returns a new SSA_NAME of type TYPE that is assigned the value of
246 the expression EXPR. Inserts the statement created for this
247 computation before GSI and leaves the iterator GSI at the same
248 statement. */
40923b20 249
bd544141
SP
250static tree
251ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
40923b20 252{
83d5977e
RG
253 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
254 gimple stmt = gimple_build_assign (new_name, expr);
bd544141 255 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
83d5977e 256 return new_name;
baaa8e96
SP
257}
258
0247298c
SP
259/* Return true when COND is a true predicate. */
260
261static inline bool
262is_true_predicate (tree cond)
263{
264 return (cond == NULL_TREE
265 || cond == boolean_true_node
266 || integer_onep (cond));
267}
268
269/* Returns true when BB has a predicate that is not trivial: true or
270 NULL_TREE. */
271
272static inline bool
273is_predicated (basic_block bb)
274{
7b14477e 275 return !is_true_predicate (bb_predicate (bb));
0247298c
SP
276}
277
d89e5e20
SP
278/* Parses the predicate COND and returns its comparison code and
279 operands OP0 and OP1. */
280
281static enum tree_code
282parse_predicate (tree cond, tree *op0, tree *op1)
283{
284 gimple s;
285
286 if (TREE_CODE (cond) == SSA_NAME
287 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
288 {
289 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
290 {
291 *op0 = gimple_assign_rhs1 (s);
292 *op1 = gimple_assign_rhs2 (s);
293 return gimple_assign_rhs_code (s);
294 }
295
296 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
297 {
298 tree op = gimple_assign_rhs1 (s);
299 tree type = TREE_TYPE (op);
300 enum tree_code code = parse_predicate (op, op0, op1);
301
302 return code == ERROR_MARK ? ERROR_MARK
303 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
304 }
305
306 return ERROR_MARK;
307 }
308
309 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
310 {
311 *op0 = TREE_OPERAND (cond, 0);
312 *op1 = TREE_OPERAND (cond, 1);
313 return TREE_CODE (cond);
314 }
315
316 return ERROR_MARK;
317}
318
59ee2304
SP
319/* Returns the fold of predicate C1 OR C2 at location LOC. */
320
321static tree
322fold_or_predicates (location_t loc, tree c1, tree c2)
323{
324 tree op1a, op1b, op2a, op2b;
325 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
326 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
327
328 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
329 {
330 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
331 code2, op2a, op2b);
332 if (t)
333 return t;
334 }
335
336 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
337}
338
f35613b2
AP
339/* Returns true if N is either a constant or a SSA_NAME. */
340
341static bool
342constant_or_ssa_name (tree n)
343{
344 switch (TREE_CODE (n))
345 {
346 case SSA_NAME:
347 case INTEGER_CST:
348 case REAL_CST:
349 case COMPLEX_CST:
350 case VECTOR_CST:
351 return true;
352 default:
353 return false;
354 }
355}
356
357/* Returns either a COND_EXPR or the folded expression if the folded
358 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
359 a constant or a SSA_NAME. */
360
361static tree
362fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
363{
364 tree rhs1, lhs1, cond_expr;
365 cond_expr = fold_ternary (COND_EXPR, type, cond,
366 rhs, lhs);
367
368 if (cond_expr == NULL_TREE)
369 return build3 (COND_EXPR, type, cond, rhs, lhs);
370
371 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
372
373 if (constant_or_ssa_name (cond_expr))
374 return cond_expr;
375
376 if (TREE_CODE (cond_expr) == ABS_EXPR)
377 {
378 rhs1 = TREE_OPERAND (cond_expr, 1);
379 STRIP_USELESS_TYPE_CONVERSION (rhs1);
380 if (constant_or_ssa_name (rhs1))
381 return build1 (ABS_EXPR, type, rhs1);
382 }
383
384 if (TREE_CODE (cond_expr) == MIN_EXPR
385 || TREE_CODE (cond_expr) == MAX_EXPR)
386 {
387 lhs1 = TREE_OPERAND (cond_expr, 0);
388 STRIP_USELESS_TYPE_CONVERSION (lhs1);
389 rhs1 = TREE_OPERAND (cond_expr, 1);
390 STRIP_USELESS_TYPE_CONVERSION (rhs1);
391 if (constant_or_ssa_name (rhs1)
392 && constant_or_ssa_name (lhs1))
393 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
394 }
395 return build3 (COND_EXPR, type, cond, rhs, lhs);
396}
397
5ce9450f
JJ
398/* Add condition NC to the predicate list of basic block BB. LOOP is
399 the loop to be if-converted. */
baaa8e96 400
0247298c 401static inline void
5ce9450f 402add_to_predicate_list (struct loop *loop, basic_block bb, tree nc)
baaa8e96 403{
747633c5 404 tree bc, *tp;
d89e5e20
SP
405
406 if (is_true_predicate (nc))
407 return;
408
409 if (!is_predicated (bb))
5ce9450f
JJ
410 {
411 /* If dominance tells us this basic block is always executed, don't
412 record any predicates for it. */
413 if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
414 return;
415
416 bc = nc;
417 }
d89e5e20
SP
418 else
419 {
d89e5e20 420 bc = bb_predicate (bb);
59ee2304 421 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
747633c5
RG
422 if (is_true_predicate (bc))
423 {
424 reset_bb_predicate (bb);
425 return;
426 }
d89e5e20
SP
427 }
428
747633c5
RG
429 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
430 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
431 tp = &TREE_OPERAND (bc, 0);
432 else
433 tp = &bc;
434 if (!is_gimple_condexpr (*tp))
d89e5e20
SP
435 {
436 gimple_seq stmts;
747633c5 437 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
d89e5e20
SP
438 add_bb_predicate_gimplified_stmts (bb, stmts);
439 }
747633c5 440 set_bb_predicate (bb, bc);
baaa8e96
SP
441}
442
e1449456
SP
443/* Add the condition COND to the previous condition PREV_COND, and add
444 this to the predicate list of the destination of edge E. LOOP is
445 the loop to be if-converted. */
baaa8e96 446
0247298c 447static void
baaa8e96 448add_to_dst_predicate_list (struct loop *loop, edge e,
e1449456 449 tree prev_cond, tree cond)
baaa8e96 450{
baaa8e96 451 if (!flow_bb_inside_loop_p (loop, e->dest))
0247298c 452 return;
baaa8e96 453
0247298c
SP
454 if (!is_true_predicate (prev_cond))
455 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
456 prev_cond, cond);
40923b20 457
5ce9450f 458 add_to_predicate_list (loop, e->dest, cond);
baaa8e96 459}
8ad02175 460
98c07c54 461/* Return true if one of the successor edges of BB exits LOOP. */
40923b20 462
baaa8e96
SP
463static bool
464bb_with_exit_edge_p (struct loop *loop, basic_block bb)
465{
466 edge e;
467 edge_iterator ei;
40923b20 468
baaa8e96
SP
469 FOR_EACH_EDGE (e, ei, bb->succs)
470 if (loop_exit_edge_p (loop, e))
98c07c54 471 return true;
40923b20 472
98c07c54 473 return false;
baaa8e96 474}
77bd31de 475
98c07c54 476/* Return true when PHI is if-convertible. PHI is part of loop LOOP
40923b20 477 and it belongs to basic block BB.
98c07c54
SP
478
479 PHI is not if-convertible if:
bd544141
SP
480 - it has more than 2 arguments.
481
482 When the flag_tree_loop_if_convert_stores is not set, PHI is not
483 if-convertible if:
484 - a virtual PHI is immediately used in another PHI node,
485 - there is a virtual PHI in a BB other than the loop->header. */
40923b20
DP
486
487static bool
5ce9450f
JJ
488if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi,
489 bool any_mask_load_store)
40923b20
DP
490{
491 if (dump_file && (dump_flags & TDF_DETAILS))
492 {
493 fprintf (dump_file, "-------------------------\n");
726a989a 494 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
40923b20 495 }
61b5f210 496
726a989a 497 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
40923b20
DP
498 {
499 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file, "More than two phi node args.\n");
501 return false;
502 }
61b5f210 503
5ce9450f 504 if (flag_tree_loop_if_convert_stores || any_mask_load_store)
bd544141
SP
505 return true;
506
507 /* When the flag_tree_loop_if_convert_stores is not set, check
508 that there are no memory writes in the branches of the loop to be
509 if-converted. */
ea057359 510 if (virtual_operand_p (gimple_phi_result (phi)))
40923b20 511 {
f430bae8
AM
512 imm_use_iterator imm_iter;
513 use_operand_p use_p;
93d15c33
JJ
514
515 if (bb != loop->header)
516 {
517 if (dump_file && (dump_flags & TDF_DETAILS))
bd544141 518 fprintf (dump_file, "Virtual phi not on loop->header.\n");
93d15c33
JJ
519 return false;
520 }
bd544141 521
726a989a 522 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
40923b20 523 {
726a989a 524 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
40923b20
DP
525 {
526 if (dump_file && (dump_flags & TDF_DETAILS))
527 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
528 return false;
529 }
530 }
531 }
532
533 return true;
534}
535
4b9c23ea
SP
536/* Records the status of a data reference. This struct is attached to
537 each DR->aux field. */
538
539struct ifc_dr {
540 /* -1 when not initialized, 0 when false, 1 when true. */
541 int written_at_least_once;
542
543 /* -1 when not initialized, 0 when false, 1 when true. */
544 int rw_unconditionally;
545};
546
547#define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
548#define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
549#define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
550
e1fd038a
SP
551/* Returns true when the memory references of STMT are read or written
552 unconditionally. In other words, this function returns true when
553 for every data reference A in STMT there exist other accesses to
4979c28b
RG
554 a data reference with the same base with predicates that add up (OR-up) to
555 the true predicate: this ensures that the data reference A is touched
e1fd038a
SP
556 (read or written) on every iteration of the if-converted loop. */
557
558static bool
559memrefs_read_or_written_unconditionally (gimple stmt,
9771b263 560 vec<data_reference_p> drs)
e1fd038a
SP
561{
562 int i, j;
563 data_reference_p a, b;
564 tree ca = bb_predicate (gimple_bb (stmt));
565
9771b263 566 for (i = 0; drs.iterate (i, &a); i++)
e1fd038a
SP
567 if (DR_STMT (a) == stmt)
568 {
569 bool found = false;
4b9c23ea
SP
570 int x = DR_RW_UNCONDITIONALLY (a);
571
572 if (x == 0)
573 return false;
574
575 if (x == 1)
576 continue;
e1fd038a 577
9771b263 578 for (j = 0; drs.iterate (j, &b); j++)
4979c28b
RG
579 {
580 tree ref_base_a = DR_REF (a);
581 tree ref_base_b = DR_REF (b);
582
583 if (DR_STMT (b) == stmt)
584 continue;
585
586 while (TREE_CODE (ref_base_a) == COMPONENT_REF
587 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
588 || TREE_CODE (ref_base_a) == REALPART_EXPR)
589 ref_base_a = TREE_OPERAND (ref_base_a, 0);
590
591 while (TREE_CODE (ref_base_b) == COMPONENT_REF
592 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
593 || TREE_CODE (ref_base_b) == REALPART_EXPR)
594 ref_base_b = TREE_OPERAND (ref_base_b, 0);
595
596 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
597 {
598 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
599
600 if (DR_RW_UNCONDITIONALLY (b) == 1
601 || is_true_predicate (cb)
602 || is_true_predicate (ca
603 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
604 {
605 DR_RW_UNCONDITIONALLY (a) = 1;
606 DR_RW_UNCONDITIONALLY (b) = 1;
607 found = true;
608 break;
609 }
610 }
e1fd038a
SP
611 }
612
613 if (!found)
4b9c23ea
SP
614 {
615 DR_RW_UNCONDITIONALLY (a) = 0;
616 return false;
617 }
e1fd038a
SP
618 }
619
620 return true;
621}
622
623/* Returns true when the memory references of STMT are unconditionally
624 written. In other words, this function returns true when for every
625 data reference A written in STMT, there exist other writes to the
626 same data reference with predicates that add up (OR-up) to the true
627 predicate: this ensures that the data reference A is written on
628 every iteration of the if-converted loop. */
629
630static bool
631write_memrefs_written_at_least_once (gimple stmt,
9771b263 632 vec<data_reference_p> drs)
e1fd038a
SP
633{
634 int i, j;
635 data_reference_p a, b;
636 tree ca = bb_predicate (gimple_bb (stmt));
637
9771b263 638 for (i = 0; drs.iterate (i, &a); i++)
e1fd038a 639 if (DR_STMT (a) == stmt
b0af49c4 640 && DR_IS_WRITE (a))
e1fd038a
SP
641 {
642 bool found = false;
4b9c23ea
SP
643 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
644
645 if (x == 0)
646 return false;
647
648 if (x == 1)
649 continue;
e1fd038a 650
9771b263 651 for (j = 0; drs.iterate (j, &b); j++)
e1fd038a 652 if (DR_STMT (b) != stmt
b0af49c4 653 && DR_IS_WRITE (b)
e1fd038a
SP
654 && same_data_refs_base_objects (a, b))
655 {
656 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
657
4b9c23ea
SP
658 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
659 || is_true_predicate (cb)
e1fd038a
SP
660 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
661 ca, cb)))
662 {
4b9c23ea
SP
663 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
664 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
e1fd038a
SP
665 found = true;
666 break;
667 }
668 }
669
670 if (!found)
4b9c23ea
SP
671 {
672 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
673 return false;
674 }
e1fd038a
SP
675 }
676
677 return true;
678}
679
680/* Return true when the memory references of STMT won't trap in the
681 if-converted code. There are two things that we have to check for:
682
683 - writes to memory occur to writable memory: if-conversion of
684 memory writes transforms the conditional memory writes into
685 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
686 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
687 be executed at all in the original code, it may be a readonly
688 memory. To check that A is not const-qualified, we check that
689 there exists at least an unconditional write to A in the current
690 function.
691
692 - reads or writes to memory are valid memory accesses for every
693 iteration. To check that the memory accesses are correctly formed
694 and that we are allowed to read and write in these locations, we
695 check that the memory accesses to be if-converted occur at every
696 iteration unconditionally. */
697
698static bool
9771b263 699ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
e1fd038a
SP
700{
701 return write_memrefs_written_at_least_once (stmt, refs)
702 && memrefs_read_or_written_unconditionally (stmt, refs);
703}
704
705/* Wrapper around gimple_could_trap_p refined for the needs of the
706 if-conversion. Try to prove that the memory accesses of STMT could
707 not trap in the innermost loop containing STMT. */
708
709static bool
9771b263 710ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
e1fd038a
SP
711{
712 if (gimple_vuse (stmt)
713 && !gimple_could_trap_p_1 (stmt, false, false)
714 && ifcvt_memrefs_wont_trap (stmt, refs))
715 return false;
716
717 return gimple_could_trap_p (stmt);
718}
719
5ce9450f
JJ
720/* Return true if STMT could be converted into a masked load or store
721 (conditional load or store based on a mask computed from bb predicate). */
722
723static bool
724ifcvt_can_use_mask_load_store (gimple stmt)
725{
726 tree lhs, ref;
727 enum machine_mode mode;
728 basic_block bb = gimple_bb (stmt);
729 bool is_load;
730
b15b5979 731 if (!(flag_tree_loop_vectorize || bb->loop_father->force_vectorize)
5ce9450f
JJ
732 || bb->loop_father->dont_vectorize
733 || !gimple_assign_single_p (stmt)
734 || gimple_has_volatile_ops (stmt))
735 return false;
736
737 /* Check whether this is a load or store. */
738 lhs = gimple_assign_lhs (stmt);
739 if (gimple_store_p (stmt))
740 {
741 if (!is_gimple_val (gimple_assign_rhs1 (stmt)))
742 return false;
743 is_load = false;
744 ref = lhs;
745 }
746 else if (gimple_assign_load_p (stmt))
747 {
748 is_load = true;
749 ref = gimple_assign_rhs1 (stmt);
750 }
751 else
752 return false;
753
754 if (may_be_nonaddressable_p (ref))
755 return false;
756
757 /* Mask should be integer mode of the same size as the load/store
758 mode. */
759 mode = TYPE_MODE (TREE_TYPE (lhs));
760 if (int_mode_for_mode (mode) == BLKmode
761 || VECTOR_MODE_P (mode))
762 return false;
763
764 if (can_vec_mask_load_store_p (mode, is_load))
765 return true;
766
767 return false;
768}
769
98c07c54
SP
770/* Return true when STMT is if-convertible.
771
726a989a 772 GIMPLE_ASSIGN statement is not if-convertible if,
b6779d81
SP
773 - it is not movable,
774 - it could trap,
bd544141 775 - LHS is not var decl. */
40923b20
DP
776
777static bool
e1fd038a 778if_convertible_gimple_assign_stmt_p (gimple stmt,
5ce9450f
JJ
779 vec<data_reference_p> refs,
780 bool *any_mask_load_store)
40923b20 781{
98c07c54 782 tree lhs = gimple_assign_lhs (stmt);
bd544141 783 basic_block bb;
3ece6cc2 784
40923b20
DP
785 if (dump_file && (dump_flags & TDF_DETAILS))
786 {
787 fprintf (dump_file, "-------------------------\n");
726a989a 788 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
40923b20 789 }
61b5f210 790
bd544141
SP
791 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
792 return false;
793
3ece6cc2 794 /* Some of these constrains might be too conservative. */
726a989a
RB
795 if (stmt_ends_bb_p (stmt)
796 || gimple_has_volatile_ops (stmt)
3ece6cc2
RE
797 || (TREE_CODE (lhs) == SSA_NAME
798 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
726a989a 799 || gimple_has_side_effects (stmt))
40923b20
DP
800 {
801 if (dump_file && (dump_flags & TDF_DETAILS))
3ece6cc2 802 fprintf (dump_file, "stmt not suitable for ifcvt\n");
40923b20
DP
803 return false;
804 }
805
5ce9450f
JJ
806 /* tree-into-ssa.c uses GF_PLF_1, so avoid it, because
807 in between if_convertible_loop_p and combine_blocks
808 we can perform loop versioning. */
809 gimple_set_plf (stmt, GF_PLF_2, false);
810
bd544141
SP
811 if (flag_tree_loop_if_convert_stores)
812 {
e1fd038a 813 if (ifcvt_could_trap_p (stmt, refs))
bd544141 814 {
5ce9450f
JJ
815 if (ifcvt_can_use_mask_load_store (stmt))
816 {
817 gimple_set_plf (stmt, GF_PLF_2, true);
818 *any_mask_load_store = true;
819 return true;
820 }
bd544141
SP
821 if (dump_file && (dump_flags & TDF_DETAILS))
822 fprintf (dump_file, "tree could trap...\n");
823 return false;
824 }
825 return true;
826 }
827
4204425f 828 if (gimple_assign_rhs_could_trap_p (stmt))
40923b20 829 {
5ce9450f
JJ
830 if (ifcvt_can_use_mask_load_store (stmt))
831 {
832 gimple_set_plf (stmt, GF_PLF_2, true);
833 *any_mask_load_store = true;
834 return true;
835 }
40923b20
DP
836 if (dump_file && (dump_flags & TDF_DETAILS))
837 fprintf (dump_file, "tree could trap...\n");
838 return false;
839 }
840
bd544141
SP
841 bb = gimple_bb (stmt);
842
726a989a 843 if (TREE_CODE (lhs) != SSA_NAME
bd544141
SP
844 && bb != bb->loop_father->header
845 && !bb_with_exit_edge_p (bb->loop_father, bb))
40923b20 846 {
5ce9450f
JJ
847 if (ifcvt_can_use_mask_load_store (stmt))
848 {
849 gimple_set_plf (stmt, GF_PLF_2, true);
850 *any_mask_load_store = true;
851 return true;
852 }
40923b20
DP
853 if (dump_file && (dump_flags & TDF_DETAILS))
854 {
855 fprintf (dump_file, "LHS is not var\n");
726a989a 856 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
40923b20
DP
857 }
858 return false;
859 }
860
40923b20
DP
861 return true;
862}
863
98c07c54
SP
864/* Return true when STMT is if-convertible.
865
866 A statement is if-convertible if:
d47657bd 867 - it is an if-convertible GIMPLE_ASSIGN,
bd544141 868 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
40923b20
DP
869
870static bool
5ce9450f
JJ
871if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs,
872 bool *any_mask_load_store)
40923b20 873{
726a989a 874 switch (gimple_code (stmt))
40923b20 875 {
726a989a 876 case GIMPLE_LABEL:
b5b8b0ac 877 case GIMPLE_DEBUG:
98c07c54
SP
878 case GIMPLE_COND:
879 return true;
61b5f210 880
b5b8b0ac 881 case GIMPLE_ASSIGN:
5ce9450f
JJ
882 return if_convertible_gimple_assign_stmt_p (stmt, refs,
883 any_mask_load_store);
61b5f210 884
d7978bff
RG
885 case GIMPLE_CALL:
886 {
887 tree fndecl = gimple_call_fndecl (stmt);
888 if (fndecl)
889 {
890 int flags = gimple_call_flags (stmt);
891 if ((flags & ECF_CONST)
892 && !(flags & ECF_LOOPING_CONST_OR_PURE)
893 /* We can only vectorize some builtins at the moment,
894 so restrict if-conversion to those. */
895 && DECL_BUILT_IN (fndecl))
896 return true;
897 }
898 return false;
899 }
900
40923b20
DP
901 default:
902 /* Don't know what to do with 'em so don't do anything. */
903 if (dump_file && (dump_flags & TDF_DETAILS))
904 {
905 fprintf (dump_file, "don't know what to do\n");
726a989a 906 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
40923b20
DP
907 }
908 return false;
909 break;
910 }
911
912 return true;
913}
914
98c07c54
SP
915/* Return true when BB is if-convertible. This routine does not check
916 basic block's statements and phis.
917
918 A basic block is not if-convertible if:
919 - it is non-empty and it is after the exit block (in BFS order),
920 - it is after the exit block but before the latch,
921 - its edges are not normal.
922
923 EXIT_BB is the basic block containing the exit of the LOOP. BB is
924 inside LOOP. */
40923b20 925
61b5f210 926static bool
3d91803a 927if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
40923b20
DP
928{
929 edge e;
628f6a4e 930 edge_iterator ei;
40923b20
DP
931
932 if (dump_file && (dump_flags & TDF_DETAILS))
933 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
61b5f210 934
bc447143
SP
935 if (EDGE_COUNT (bb->preds) > 2
936 || EDGE_COUNT (bb->succs) > 2)
937 return false;
938
3d91803a 939 if (exit_bb)
40923b20
DP
940 {
941 if (bb != loop->latch)
942 {
943 if (dump_file && (dump_flags & TDF_DETAILS))
944 fprintf (dump_file, "basic block after exit bb but before latch\n");
945 return false;
946 }
947 else if (!empty_block_p (bb))
948 {
baaa8e96
SP
949 if (dump_file && (dump_flags & TDF_DETAILS))
950 fprintf (dump_file, "non empty basic block after exit bb\n");
951 return false;
952 }
953 else if (bb == loop->latch
954 && bb != exit_bb
955 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
956 {
957 if (dump_file && (dump_flags & TDF_DETAILS))
958 fprintf (dump_file, "latch is not dominated by exit_block\n");
959 return false;
960 }
961 }
962
963 /* Be less adventurous and handle only normal edges. */
964 FOR_EACH_EDGE (e, ei, bb->succs)
a315c44c 965 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
baaa8e96
SP
966 {
967 if (dump_file && (dump_flags & TDF_DETAILS))
98c07c54 968 fprintf (dump_file, "Difficult to handle edges\n");
baaa8e96
SP
969 return false;
970 }
971
4ded8276
RB
972 /* At least one incoming edge has to be non-critical as otherwise edge
973 predicates are not equal to basic-block predicates of the edge
974 source. */
975 if (EDGE_COUNT (bb->preds) > 1
976 && bb != loop->header)
977 {
978 bool found = false;
979 FOR_EACH_EDGE (e, ei, bb->preds)
980 if (EDGE_COUNT (e->src->succs) == 1)
981 found = true;
982 if (!found)
983 {
984 if (dump_file && (dump_flags & TDF_DETAILS))
985 fprintf (dump_file, "only critical predecessors\n");
986 return false;
987 }
988 }
db963b52 989
baaa8e96
SP
990 return true;
991}
992
98c07c54
SP
993/* Return true when all predecessor blocks of BB are visited. The
994 VISITED bitmap keeps track of the visited blocks. */
baaa8e96
SP
995
996static bool
997pred_blocks_visited_p (basic_block bb, bitmap *visited)
998{
999 edge e;
1000 edge_iterator ei;
1001 FOR_EACH_EDGE (e, ei, bb->preds)
1002 if (!bitmap_bit_p (*visited, e->src->index))
1003 return false;
1004
1005 return true;
1006}
1007
1008/* Get body of a LOOP in suitable order for if-conversion. It is
1009 caller's responsibility to deallocate basic block list.
1010 If-conversion suitable order is, breadth first sort (BFS) order
1011 with an additional constraint: select a block only if all its
1012 predecessors are already selected. */
1013
1014static basic_block *
1015get_loop_body_in_if_conv_order (const struct loop *loop)
1016{
1017 basic_block *blocks, *blocks_in_bfs_order;
1018 basic_block bb;
1019 bitmap visited;
1020 unsigned int index = 0;
1021 unsigned int visited_count = 0;
1022
1023 gcc_assert (loop->num_nodes);
fefa31b5 1024 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
baaa8e96
SP
1025
1026 blocks = XCNEWVEC (basic_block, loop->num_nodes);
1027 visited = BITMAP_ALLOC (NULL);
1028
1029 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
1030
1031 index = 0;
1032 while (index < loop->num_nodes)
1033 {
1034 bb = blocks_in_bfs_order [index];
1035
1036 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1037 {
1038 free (blocks_in_bfs_order);
1039 BITMAP_FREE (visited);
1040 free (blocks);
1041 return NULL;
1042 }
1043
1044 if (!bitmap_bit_p (visited, bb->index))
1045 {
1046 if (pred_blocks_visited_p (bb, &visited)
1047 || bb == loop->header)
1048 {
1049 /* This block is now visited. */
1050 bitmap_set_bit (visited, bb->index);
1051 blocks[visited_count++] = bb;
1052 }
40923b20 1053 }
61b5f210 1054
baaa8e96 1055 index++;
40923b20 1056
baaa8e96
SP
1057 if (index == loop->num_nodes
1058 && visited_count != loop->num_nodes)
1059 /* Not done yet. */
1060 index = 0;
1061 }
1062 free (blocks_in_bfs_order);
1063 BITMAP_FREE (visited);
1064 return blocks;
40923b20
DP
1065}
1066
e1449456
SP
1067/* Returns true when the analysis of the predicates for all the basic
1068 blocks in LOOP succeeded.
1069
7b14477e 1070 predicate_bbs first allocates the predicates of the basic blocks.
32ccbfac
SP
1071 These fields are then initialized with the tree expressions
1072 representing the predicates under which a basic block is executed
1073 in the LOOP. As the loop->header is executed at each iteration, it
1074 has the "true" predicate. Other statements executed under a
1075 condition are predicated with that condition, for example
e1449456
SP
1076
1077 | if (x)
1078 | S1;
1079 | else
1080 | S2;
1081
5521cae9
SP
1082 S1 will be predicated with "x", and
1083 S2 will be predicated with "!x". */
e1449456 1084
5ce9450f 1085static void
e1449456
SP
1086predicate_bbs (loop_p loop)
1087{
1088 unsigned int i;
1089
1090 for (i = 0; i < loop->num_nodes; i++)
7b14477e 1091 init_bb_predicate (ifc_bbs[i]);
e1449456
SP
1092
1093 for (i = 0; i < loop->num_nodes; i++)
1094 {
7b14477e
SP
1095 basic_block bb = ifc_bbs[i];
1096 tree cond;
5ce9450f 1097 gimple stmt;
e1449456 1098
7b14477e
SP
1099 /* The loop latch is always executed and has no extra conditions
1100 to be processed: skip it. */
1101 if (bb == loop->latch)
1102 {
29caa68a 1103 reset_bb_predicate (loop->latch);
7b14477e
SP
1104 continue;
1105 }
1106
1107 cond = bb_predicate (bb);
5ce9450f
JJ
1108 stmt = last_stmt (bb);
1109 if (stmt && gimple_code (stmt) == GIMPLE_COND)
e1449456 1110 {
5ce9450f
JJ
1111 tree c2;
1112 edge true_edge, false_edge;
1113 location_t loc = gimple_location (stmt);
1114 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1115 boolean_type_node,
1116 gimple_cond_lhs (stmt),
1117 gimple_cond_rhs (stmt));
1118
1119 /* Add new condition into destination's predicate list. */
1120 extract_true_false_edges_from_block (gimple_bb (stmt),
1121 &true_edge, &false_edge);
1122
1123 /* If C is true, then TRUE_EDGE is taken. */
1124 add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
1125 unshare_expr (c));
1126
1127 /* If C is false, then FALSE_EDGE is taken. */
1128 c2 = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
1129 unshare_expr (c));
1130 add_to_dst_predicate_list (loop, false_edge,
1131 unshare_expr (cond), c2);
1132
1133 cond = NULL_TREE;
e1449456
SP
1134 }
1135
1136 /* If current bb has only one successor, then consider it as an
1137 unconditional goto. */
1138 if (single_succ_p (bb))
1139 {
1140 basic_block bb_n = single_succ (bb);
1141
1142 /* The successor bb inherits the predicate of its
1143 predecessor. If there is no predicate in the predecessor
1144 bb, then consider the successor bb as always executed. */
1145 if (cond == NULL_TREE)
1146 cond = boolean_true_node;
1147
5ce9450f 1148 add_to_predicate_list (loop, bb_n, cond);
e1449456
SP
1149 }
1150 }
1151
1152 /* The loop header is always executed. */
29caa68a 1153 reset_bb_predicate (loop->header);
7b14477e
SP
1154 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1155 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
e1449456
SP
1156}
1157
e1fd038a
SP
1158/* Return true when LOOP is if-convertible. This is a helper function
1159 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1160 in if_convertible_loop_p. */
40923b20
DP
1161
1162static bool
e1fd038a 1163if_convertible_loop_p_1 (struct loop *loop,
9771b263
DN
1164 vec<loop_p> *loop_nest,
1165 vec<data_reference_p> *refs,
5ce9450f 1166 vec<ddr_p> *ddrs, bool *any_mask_load_store)
40923b20 1167{
e1fd038a 1168 bool res;
40923b20 1169 unsigned int i;
3d91803a 1170 basic_block exit_bb = NULL;
40923b20 1171
6d795034
SP
1172 /* Don't if-convert the loop when the data dependences cannot be
1173 computed: the loop won't be vectorized in that case. */
01be8516 1174 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
e1fd038a
SP
1175 if (!res)
1176 return false;
6d795034 1177
40923b20 1178 calculate_dominance_info (CDI_DOMINATORS);
40923b20
DP
1179
1180 /* Allow statements that can be handled during if-conversion. */
1181 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1182 if (!ifc_bbs)
1183 {
1184 if (dump_file && (dump_flags & TDF_DETAILS))
4ab71973 1185 fprintf (dump_file, "Irreducible loop\n");
40923b20
DP
1186 return false;
1187 }
61b5f210 1188
40923b20
DP
1189 for (i = 0; i < loop->num_nodes; i++)
1190 {
e1449456 1191 basic_block bb = ifc_bbs[i];
40923b20 1192
3d91803a 1193 if (!if_convertible_bb_p (loop, bb, exit_bb))
40923b20
DP
1194 return false;
1195
e1449456
SP
1196 if (bb_with_exit_edge_p (loop, bb))
1197 exit_bb = bb;
1198 }
1199
5ce9450f
JJ
1200 for (i = 0; i < loop->num_nodes; i++)
1201 {
1202 basic_block bb = ifc_bbs[i];
1203 gimple_stmt_iterator gsi;
1204
1205 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1206 switch (gimple_code (gsi_stmt (gsi)))
1207 {
1208 case GIMPLE_LABEL:
1209 case GIMPLE_ASSIGN:
1210 case GIMPLE_CALL:
1211 case GIMPLE_DEBUG:
1212 case GIMPLE_COND:
1213 break;
1214 default:
1215 return false;
1216 }
1217 }
e1449456 1218
4b9c23ea
SP
1219 if (flag_tree_loop_if_convert_stores)
1220 {
1221 data_reference_p dr;
1222
9771b263 1223 for (i = 0; refs->iterate (i, &dr); i++)
4b9c23ea
SP
1224 {
1225 dr->aux = XNEW (struct ifc_dr);
1226 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1227 DR_RW_UNCONDITIONALLY (dr) = -1;
1228 }
5ce9450f 1229 predicate_bbs (loop);
4b9c23ea
SP
1230 }
1231
e1449456
SP
1232 for (i = 0; i < loop->num_nodes; i++)
1233 {
1234 basic_block bb = ifc_bbs[i];
1235 gimple_stmt_iterator itr;
1236
e1fd038a 1237 /* Check the if-convertibility of statements in predicated BBs. */
5ce9450f 1238 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
e1fd038a 1239 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
5ce9450f
JJ
1240 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs,
1241 any_mask_load_store))
e1fd038a 1242 return false;
40923b20
DP
1243 }
1244
5ce9450f
JJ
1245 if (flag_tree_loop_if_convert_stores)
1246 for (i = 0; i < loop->num_nodes; i++)
1247 free_bb_predicate (ifc_bbs[i]);
1248
1249 /* Checking PHIs needs to be done after stmts, as the fact whether there
1250 are any masked loads or stores affects the tests. */
1251 for (i = 0; i < loop->num_nodes; i++)
1252 {
1253 basic_block bb = ifc_bbs[i];
1254 gimple_stmt_iterator itr;
1255
1256 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1257 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr),
1258 *any_mask_load_store))
1259 return false;
1260 }
1261
40923b20 1262 if (dump_file)
4ab71973 1263 fprintf (dump_file, "Applying if-conversion\n");
40923b20 1264
40923b20
DP
1265 return true;
1266}
1267
e1fd038a
SP
1268/* Return true when LOOP is if-convertible.
1269 LOOP is if-convertible if:
1270 - it is innermost,
1271 - it has two or more basic blocks,
1272 - it has only one exit,
1273 - loop header is not the exit edge,
1274 - if its basic blocks and phi nodes are if convertible. */
1275
1276static bool
5ce9450f 1277if_convertible_loop_p (struct loop *loop, bool *any_mask_load_store)
e1fd038a
SP
1278{
1279 edge e;
1280 edge_iterator ei;
1281 bool res = false;
9771b263
DN
1282 vec<data_reference_p> refs;
1283 vec<ddr_p> ddrs;
e1fd038a
SP
1284
1285 /* Handle only innermost loop. */
1286 if (!loop || loop->inner)
1287 {
1288 if (dump_file && (dump_flags & TDF_DETAILS))
1289 fprintf (dump_file, "not innermost loop\n");
1290 return false;
1291 }
1292
1293 /* If only one block, no need for if-conversion. */
1294 if (loop->num_nodes <= 2)
1295 {
1296 if (dump_file && (dump_flags & TDF_DETAILS))
1297 fprintf (dump_file, "less than 2 basic blocks\n");
1298 return false;
1299 }
1300
1301 /* More than one loop exit is too much to handle. */
1302 if (!single_exit (loop))
1303 {
1304 if (dump_file && (dump_flags & TDF_DETAILS))
1305 fprintf (dump_file, "multiple exits\n");
1306 return false;
1307 }
1308
1309 /* If one of the loop header's edge is an exit edge then do not
1310 apply if-conversion. */
1311 FOR_EACH_EDGE (e, ei, loop->header->succs)
1312 if (loop_exit_edge_p (loop, e))
1313 return false;
1314
9771b263
DN
1315 refs.create (5);
1316 ddrs.create (25);
00f96dc9 1317 auto_vec<loop_p, 3> loop_nest;
5ce9450f
JJ
1318 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs,
1319 any_mask_load_store);
e1fd038a 1320
4b9c23ea
SP
1321 if (flag_tree_loop_if_convert_stores)
1322 {
1323 data_reference_p dr;
1324 unsigned int i;
1325
9771b263 1326 for (i = 0; refs.iterate (i, &dr); i++)
4b9c23ea
SP
1327 free (dr->aux);
1328 }
1329
e1fd038a
SP
1330 free_data_refs (refs);
1331 free_dependence_relations (ddrs);
1332 return res;
1333}
1334
7b14477e
SP
1335/* Basic block BB has two predecessors. Using predecessor's bb
1336 predicate, set an appropriate condition COND for the PHI node
1337 replacement. Return the true block whose phi arguments are
1338 selected when cond is true. LOOP is the loop containing the
1339 if-converted region, GSI is the place to insert the code for the
1340 if-conversion. */
40923b20 1341
7f7e0703 1342static basic_block
4ded8276 1343find_phi_replacement_condition (basic_block bb, tree *cond,
bd544141 1344 gimple_stmt_iterator *gsi)
40923b20 1345{
8ad02175 1346 edge first_edge, second_edge;
c6540bde 1347 tree tmp_cond;
40923b20 1348
1c280337 1349 gcc_assert (EDGE_COUNT (bb->preds) == 2);
8ad02175
UB
1350 first_edge = EDGE_PRED (bb, 0);
1351 second_edge = EDGE_PRED (bb, 1);
40923b20 1352
4ded8276
RB
1353 /* Prefer an edge with a not negated predicate.
1354 ??? That's a very weak cost model. */
7b14477e 1355 tmp_cond = bb_predicate (first_edge->src);
77bd31de 1356 gcc_assert (tmp_cond);
40923b20
DP
1357 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1358 {
8ad02175
UB
1359 edge tmp_edge;
1360
1361 tmp_edge = first_edge;
1362 first_edge = second_edge;
1363 second_edge = tmp_edge;
40923b20 1364 }
1c280337 1365
4ded8276
RB
1366 /* Check if the edge we take the condition from is not critical.
1367 We know that at least one non-critical edge exists. */
1368 if (EDGE_COUNT (first_edge->src->succs) > 1)
40923b20 1369 {
7b14477e 1370 *cond = bb_predicate (second_edge->src);
8ad02175 1371
8ad02175 1372 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
747633c5 1373 *cond = TREE_OPERAND (*cond, 0);
0bca51f0 1374 else
8ad02175
UB
1375 /* Select non loop header bb. */
1376 first_edge = second_edge;
40923b20 1377 }
1c280337 1378 else
7b14477e 1379 *cond = bb_predicate (first_edge->src);
61b5f210 1380
747633c5
RG
1381 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1382 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1383 is_gimple_condexpr, NULL_TREE,
1384 true, GSI_SAME_STMT);
40923b20 1385
8ad02175 1386 return first_edge->src;
40923b20
DP
1387}
1388
bd544141
SP
1389/* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1390 This routine does not handle PHI nodes with more than two
1391 arguments.
40923b20 1392
40923b20 1393 For example,
b8f4632c 1394 S1: A = PHI <x1(1), x2(5)>
40923b20
DP
1395 is converted into,
1396 S2: A = cond ? x1 : x2;
98c07c54
SP
1397
1398 The generated code is inserted at GSI that points to the top of
1399 basic block's statement list. When COND is true, phi arg from
1400 TRUE_BB is selected. */
40923b20
DP
1401
1402static void
bd544141
SP
1403predicate_scalar_phi (gimple phi, tree cond,
1404 basic_block true_bb,
1405 gimple_stmt_iterator *gsi)
40923b20 1406{
726a989a 1407 gimple new_stmt;
40923b20 1408 basic_block bb;
e639b206 1409 tree rhs, res, arg, scev;
40923b20 1410
98c07c54
SP
1411 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1412 && gimple_phi_num_args (phi) == 2);
b8698a0f 1413
bd544141
SP
1414 res = gimple_phi_result (phi);
1415 /* Do not handle virtual phi nodes. */
ea057359 1416 if (virtual_operand_p (res))
bd544141
SP
1417 return;
1418
726a989a 1419 bb = gimple_bb (phi);
40923b20 1420
e639b206
SP
1421 if ((arg = degenerate_phi_result (phi))
1422 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1423 res))
1424 && !chrec_contains_undetermined (scev)
1425 && scev != res
1426 && (arg = gimple_phi_arg_def (phi, 0))))
e7cb8957 1427 rhs = arg;
40923b20
DP
1428 else
1429 {
e7cb8957
SP
1430 tree arg_0, arg_1;
1431 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1432 if (EDGE_PRED (bb, 1)->src == true_bb)
1433 {
1434 arg_0 = gimple_phi_arg_def (phi, 1);
1435 arg_1 = gimple_phi_arg_def (phi, 0);
1436 }
1437 else
1438 {
1439 arg_0 = gimple_phi_arg_def (phi, 0);
1440 arg_1 = gimple_phi_arg_def (phi, 1);
1441 }
40923b20 1442
e7cb8957 1443 /* Build new RHS using selected condition and arguments. */
f35613b2
AP
1444 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1445 arg_0, arg_1);
e7cb8957 1446 }
40923b20 1447
bd544141 1448 new_stmt = gimple_build_assign (res, rhs);
726a989a 1449 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
f430bae8 1450 update_stmt (new_stmt);
40923b20
DP
1451
1452 if (dump_file && (dump_flags & TDF_DETAILS))
1453 {
1454 fprintf (dump_file, "new phi replacement stmt\n");
726a989a 1455 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
40923b20
DP
1456 }
1457}
1458
bd544141 1459/* Replaces in LOOP all the scalar phi nodes other than those in the
7b14477e 1460 LOOP->header block with conditional modify expressions. */
40923b20
DP
1461
1462static void
bd544141 1463predicate_all_scalar_phis (struct loop *loop)
40923b20
DP
1464{
1465 basic_block bb;
1466 unsigned int orig_loop_num_nodes = loop->num_nodes;
1467 unsigned int i;
1468
40923b20
DP
1469 for (i = 1; i < orig_loop_num_nodes; i++)
1470 {
726a989a
RB
1471 gimple phi;
1472 tree cond = NULL_TREE;
1473 gimple_stmt_iterator gsi, phi_gsi;
7f7e0703 1474 basic_block true_bb = NULL;
40923b20 1475 bb = ifc_bbs[i];
61b5f210 1476
0ecf0d5f 1477 if (bb == loop->header)
40923b20
DP
1478 continue;
1479
726a989a 1480 phi_gsi = gsi_start_phis (bb);
7b14477e
SP
1481 if (gsi_end_p (phi_gsi))
1482 continue;
40923b20 1483
62ef2431 1484 /* BB has two predecessors. Using predecessor's aux field, set
40923b20 1485 appropriate condition for the PHI node replacement. */
7b14477e 1486 gsi = gsi_after_labels (bb);
4ded8276 1487 true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
40923b20 1488
726a989a 1489 while (!gsi_end_p (phi_gsi))
40923b20 1490 {
726a989a 1491 phi = gsi_stmt (phi_gsi);
bd544141 1492 predicate_scalar_phi (phi, cond, true_bb, &gsi);
40923b20 1493 release_phi_node (phi);
726a989a 1494 gsi_next (&phi_gsi);
40923b20 1495 }
7b14477e 1496
726a989a 1497 set_phi_nodes (bb, NULL);
40923b20 1498 }
40923b20
DP
1499}
1500
7b14477e
SP
1501/* Insert in each basic block of LOOP the statements produced by the
1502 gimplification of the predicates. */
1503
1504static void
5ce9450f 1505insert_gimplified_predicates (loop_p loop, bool any_mask_load_store)
7b14477e
SP
1506{
1507 unsigned int i;
1508
1509 for (i = 0; i < loop->num_nodes; i++)
1510 {
1511 basic_block bb = ifc_bbs[i];
bd544141 1512 gimple_seq stmts;
7b14477e 1513
5c8b27d7
SP
1514 if (!is_predicated (bb))
1515 {
1516 /* Do not insert statements for a basic block that is not
1517 predicated. Also make sure that the predicate of the
1518 basic block is set to true. */
1519 reset_bb_predicate (bb);
1520 continue;
1521 }
1522
bd544141 1523 stmts = bb_predicate_gimplified_stmts (bb);
7b14477e
SP
1524 if (stmts)
1525 {
5ce9450f
JJ
1526 if (flag_tree_loop_if_convert_stores
1527 || any_mask_load_store)
bd544141
SP
1528 {
1529 /* Insert the predicate of the BB just after the label,
1530 as the if-conversion of memory writes will use this
1531 predicate. */
1532 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1533 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1534 }
7b14477e 1535 else
bd544141
SP
1536 {
1537 /* Insert the predicate of the BB at the end of the BB
1538 as this would reduce the register pressure: the only
1539 use of this predicate will be in successor BBs. */
1540 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1541
1542 if (gsi_end_p (gsi)
1543 || stmt_ends_bb_p (gsi_stmt (gsi)))
1544 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1545 else
1546 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1547 }
7b14477e
SP
1548
1549 /* Once the sequence is code generated, set it to NULL. */
1550 set_bb_predicate_gimplified_stmts (bb, NULL);
1551 }
1552 }
1553}
1554
bd544141
SP
1555/* Predicate each write to memory in LOOP.
1556
1557 This function transforms control flow constructs containing memory
1558 writes of the form:
1559
1560 | for (i = 0; i < N; i++)
1561 | if (cond)
1562 | A[i] = expr;
1563
1564 into the following form that does not contain control flow:
1565
1566 | for (i = 0; i < N; i++)
1567 | A[i] = cond ? expr : A[i];
1568
1569 The original CFG looks like this:
1570
1571 | bb_0
1572 | i = 0
1573 | end_bb_0
1574 |
1575 | bb_1
1576 | if (i < N) goto bb_5 else goto bb_2
1577 | end_bb_1
1578 |
1579 | bb_2
1580 | cond = some_computation;
1581 | if (cond) goto bb_3 else goto bb_4
1582 | end_bb_2
1583 |
1584 | bb_3
1585 | A[i] = expr;
1586 | goto bb_4
1587 | end_bb_3
1588 |
1589 | bb_4
1590 | goto bb_1
1591 | end_bb_4
1592
1593 insert_gimplified_predicates inserts the computation of the COND
1594 expression at the beginning of the destination basic block:
1595
1596 | bb_0
1597 | i = 0
1598 | end_bb_0
1599 |
1600 | bb_1
1601 | if (i < N) goto bb_5 else goto bb_2
1602 | end_bb_1
1603 |
1604 | bb_2
1605 | cond = some_computation;
1606 | if (cond) goto bb_3 else goto bb_4
1607 | end_bb_2
1608 |
1609 | bb_3
1610 | cond = some_computation;
1611 | A[i] = expr;
1612 | goto bb_4
1613 | end_bb_3
1614 |
1615 | bb_4
1616 | goto bb_1
1617 | end_bb_4
1618
1619 predicate_mem_writes is then predicating the memory write as follows:
1620
1621 | bb_0
1622 | i = 0
1623 | end_bb_0
1624 |
1625 | bb_1
1626 | if (i < N) goto bb_5 else goto bb_2
1627 | end_bb_1
1628 |
1629 | bb_2
1630 | if (cond) goto bb_3 else goto bb_4
1631 | end_bb_2
1632 |
1633 | bb_3
1634 | cond = some_computation;
1635 | A[i] = cond ? expr : A[i];
1636 | goto bb_4
1637 | end_bb_3
1638 |
1639 | bb_4
1640 | goto bb_1
1641 | end_bb_4
1642
1643 and finally combine_blocks removes the basic block boundaries making
1644 the loop vectorizable:
1645
1646 | bb_0
1647 | i = 0
1648 | if (i < N) goto bb_5 else goto bb_1
1649 | end_bb_0
1650 |
1651 | bb_1
1652 | cond = some_computation;
1653 | A[i] = cond ? expr : A[i];
1654 | if (i < N) goto bb_5 else goto bb_4
1655 | end_bb_1
1656 |
1657 | bb_4
1658 | goto bb_1
1659 | end_bb_4
1660*/
1661
1662static void
1663predicate_mem_writes (loop_p loop)
1664{
1665 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1666
1667 for (i = 1; i < orig_loop_num_nodes; i++)
1668 {
1669 gimple_stmt_iterator gsi;
1670 basic_block bb = ifc_bbs[i];
1671 tree cond = bb_predicate (bb);
95df37bf 1672 bool swap;
bd544141
SP
1673 gimple stmt;
1674
1675 if (is_true_predicate (cond))
1676 continue;
1677
95df37bf
RG
1678 swap = false;
1679 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1680 {
1681 swap = true;
1682 cond = TREE_OPERAND (cond, 0);
1683 }
1684
bd544141 1685 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5ce9450f
JJ
1686 if (!gimple_assign_single_p (stmt = gsi_stmt (gsi)))
1687 continue;
1688 else if (gimple_plf (stmt, GF_PLF_2))
1689 {
1690 tree lhs = gimple_assign_lhs (stmt);
1691 tree rhs = gimple_assign_rhs1 (stmt);
1692 tree ref, addr, ptr, masktype, mask_op0, mask_op1, mask;
1693 gimple new_stmt;
1694 int bitsize = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (lhs)));
1695
1696 masktype = build_nonstandard_integer_type (bitsize, 1);
1697 mask_op0 = build_int_cst (masktype, swap ? 0 : -1);
1698 mask_op1 = build_int_cst (masktype, swap ? -1 : 0);
1699 ref = TREE_CODE (lhs) == SSA_NAME ? rhs : lhs;
1700 mark_addressable (ref);
1701 addr = force_gimple_operand_gsi (&gsi, build_fold_addr_expr (ref),
1702 true, NULL_TREE, true,
1703 GSI_SAME_STMT);
1704 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1705 is_gimple_condexpr, NULL_TREE,
1706 true, GSI_SAME_STMT);
1707 mask = fold_build_cond_expr (masktype, unshare_expr (cond),
1708 mask_op0, mask_op1);
1709 mask = ifc_temp_var (masktype, mask, &gsi);
1710 ptr = build_int_cst (reference_alias_ptr_type (ref), 0);
1711 /* Copy points-to info if possible. */
1712 if (TREE_CODE (addr) == SSA_NAME && !SSA_NAME_PTR_INFO (addr))
1713 copy_ref_info (build2 (MEM_REF, TREE_TYPE (ref), addr, ptr),
1714 ref);
1715 if (TREE_CODE (lhs) == SSA_NAME)
1716 {
1717 new_stmt
1718 = gimple_build_call_internal (IFN_MASK_LOAD, 3, addr,
1719 ptr, mask);
1720 gimple_call_set_lhs (new_stmt, lhs);
1721 }
1722 else
1723 new_stmt
1724 = gimple_build_call_internal (IFN_MASK_STORE, 4, addr, ptr,
1725 mask, rhs);
8e91d222 1726 gsi_replace (&gsi, new_stmt, true);
5ce9450f
JJ
1727 }
1728 else if (gimple_vdef (stmt))
bd544141
SP
1729 {
1730 tree lhs = gimple_assign_lhs (stmt);
1731 tree rhs = gimple_assign_rhs1 (stmt);
1732 tree type = TREE_TYPE (lhs);
1733
1734 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1735 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
95df37bf
RG
1736 if (swap)
1737 {
1738 tree tem = lhs;
1739 lhs = rhs;
1740 rhs = tem;
1741 }
1742 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1743 is_gimple_condexpr, NULL_TREE,
1744 true, GSI_SAME_STMT);
f35613b2 1745 rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
bd544141
SP
1746 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1747 update_stmt (stmt);
1748 }
1749 }
1750}
1751
76b84776 1752/* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
718d3588
SP
1753 other than the exit and latch of the LOOP. Also resets the
1754 GIMPLE_DEBUG information. */
76b84776
SP
1755
1756static void
1757remove_conditions_and_labels (loop_p loop)
1758{
1759 gimple_stmt_iterator gsi;
1760 unsigned int i;
1761
1762 for (i = 0; i < loop->num_nodes; i++)
1763 {
7b14477e 1764 basic_block bb = ifc_bbs[i];
76b84776
SP
1765
1766 if (bb_with_exit_edge_p (loop, bb)
1767 || bb == loop->latch)
1768 continue;
1769
1770 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
718d3588
SP
1771 switch (gimple_code (gsi_stmt (gsi)))
1772 {
1773 case GIMPLE_COND:
1774 case GIMPLE_LABEL:
1775 gsi_remove (&gsi, true);
1776 break;
1777
1778 case GIMPLE_DEBUG:
1779 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1780 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1781 {
1782 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1783 update_stmt (gsi_stmt (gsi));
1784 }
1785 gsi_next (&gsi);
1786 break;
1787
1788 default:
1789 gsi_next (&gsi);
1790 }
76b84776
SP
1791 }
1792}
1793
62ef2431
SP
1794/* Combine all the basic blocks from LOOP into one or two super basic
1795 blocks. Replace PHI nodes with conditional modify expressions. */
40923b20
DP
1796
1797static void
5ce9450f 1798combine_blocks (struct loop *loop, bool any_mask_load_store)
40923b20
DP
1799{
1800 basic_block bb, exit_bb, merge_target_bb;
1801 unsigned int orig_loop_num_nodes = loop->num_nodes;
1802 unsigned int i;
36b24193
ZD
1803 edge e;
1804 edge_iterator ei;
2b74282d 1805
5ce9450f 1806 predicate_bbs (loop);
76b84776 1807 remove_conditions_and_labels (loop);
5ce9450f 1808 insert_gimplified_predicates (loop, any_mask_load_store);
bd544141
SP
1809 predicate_all_scalar_phis (loop);
1810
5ce9450f 1811 if (flag_tree_loop_if_convert_stores || any_mask_load_store)
bd544141 1812 predicate_mem_writes (loop);
40923b20 1813
98c07c54
SP
1814 /* Merge basic blocks: first remove all the edges in the loop,
1815 except for those from the exit block. */
40923b20 1816 exit_bb = NULL;
36b24193
ZD
1817 for (i = 0; i < orig_loop_num_nodes; i++)
1818 {
1819 bb = ifc_bbs[i];
c2b5fc8d 1820 free_bb_predicate (bb);
36b24193
ZD
1821 if (bb_with_exit_edge_p (loop, bb))
1822 {
c6542175 1823 gcc_assert (exit_bb == NULL);
36b24193 1824 exit_bb = bb;
36b24193
ZD
1825 }
1826 }
1827 gcc_assert (exit_bb != loop->latch);
40923b20 1828
40923b20
DP
1829 for (i = 1; i < orig_loop_num_nodes; i++)
1830 {
40923b20
DP
1831 bb = ifc_bbs[i];
1832
36b24193 1833 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
40923b20 1834 {
36b24193
ZD
1835 if (e->src == exit_bb)
1836 ei_next (&ei);
1837 else
1838 remove_edge (e);
1839 }
1840 }
40923b20 1841
36b24193
ZD
1842 if (exit_bb != NULL)
1843 {
1844 if (exit_bb != loop->header)
1845 {
98c07c54 1846 /* Connect this node to loop header. */
36b24193
ZD
1847 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1848 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
40923b20
DP
1849 }
1850
36b24193
ZD
1851 /* Redirect non-exit edges to loop->latch. */
1852 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1853 {
1854 if (!loop_exit_edge_p (loop, e))
1855 redirect_edge_and_branch (e, loop->latch);
1856 }
1857 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1858 }
1859 else
1860 {
98c07c54 1861 /* If the loop does not have an exit, reconnect header and latch. */
36b24193
ZD
1862 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1863 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1864 }
0ecf0d5f 1865
36b24193
ZD
1866 merge_target_bb = loop->header;
1867 for (i = 1; i < orig_loop_num_nodes; i++)
1868 {
726a989a
RB
1869 gimple_stmt_iterator gsi;
1870 gimple_stmt_iterator last;
ac0bd801 1871
36b24193 1872 bb = ifc_bbs[i];
537a2904 1873
36b24193
ZD
1874 if (bb == exit_bb || bb == loop->latch)
1875 continue;
537a2904 1876
76b84776
SP
1877 /* Make stmts member of loop->header. */
1878 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1879 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
40923b20
DP
1880
1881 /* Update stmt list. */
726a989a
RB
1882 last = gsi_last_bb (merge_target_bb);
1883 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1884 set_bb_seq (bb, NULL);
40923b20 1885
598ec7bd 1886 delete_basic_block (bb);
40923b20 1887 }
a2159c4c 1888
98c07c54
SP
1889 /* If possible, merge loop header to the block with the exit edge.
1890 This reduces the number of basic blocks to two, to please the
0f741287 1891 vectorizer that handles only loops with two nodes. */
0ecf0d5f 1892 if (exit_bb
36b24193
ZD
1893 && exit_bb != loop->header
1894 && can_merge_blocks_p (loop->header, exit_bb))
598ec7bd 1895 merge_blocks (loop->header, exit_bb);
c2b5fc8d
JJ
1896
1897 free (ifc_bbs);
1898 ifc_bbs = NULL;
40923b20
DP
1899}
1900
5ce9450f
JJ
1901/* Version LOOP before if-converting it, the original loop
1902 will be then if-converted, the new copy of the loop will not,
1903 and the LOOP_VECTORIZED internal call will be guarding which
1904 loop to execute. The vectorizer pass will fold this
1905 internal call into either true or false. */
40923b20 1906
0f741287 1907static bool
5ce9450f
JJ
1908version_loop_for_if_conversion (struct loop *loop)
1909{
1910 basic_block cond_bb;
1911 tree cond = make_ssa_name (boolean_type_node, NULL);
1912 struct loop *new_loop;
1913 gimple g;
1914 gimple_stmt_iterator gsi;
1915
1916 g = gimple_build_call_internal (IFN_LOOP_VECTORIZED, 2,
1917 build_int_cst (integer_type_node, loop->num),
1918 integer_zero_node);
1919 gimple_call_set_lhs (g, cond);
1920
1921 initialize_original_copy_tables ();
1922 new_loop = loop_version (loop, cond, &cond_bb,
1923 REG_BR_PROB_BASE, REG_BR_PROB_BASE,
1924 REG_BR_PROB_BASE, true);
1925 free_original_copy_tables ();
1926 if (new_loop == NULL)
1927 return false;
1928 new_loop->dont_vectorize = true;
b15b5979 1929 new_loop->force_vectorize = false;
5ce9450f
JJ
1930 gsi = gsi_last_bb (cond_bb);
1931 gimple_call_set_arg (g, 1, build_int_cst (integer_type_node, new_loop->num));
1932 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1933 update_ssa (TODO_update_ssa);
1934 return true;
1935}
1936
1937/* If-convert LOOP when it is legal. For the moment this pass has no
1938 profitability analysis. Returns non-zero todo flags when something
1939 changed. */
1940
1941static unsigned int
6cbcfa9d 1942tree_if_conversion (struct loop *loop)
40923b20 1943{
5ce9450f 1944 unsigned int todo = 0;
baaa8e96 1945 ifc_bbs = NULL;
5ce9450f 1946 bool any_mask_load_store = false;
40923b20 1947
5ce9450f 1948 if (!if_convertible_loop_p (loop, &any_mask_load_store)
53aa40a8 1949 || !dbg_cnt (if_conversion_tree))
e1449456 1950 goto cleanup;
b6779d81 1951
5ce9450f 1952 if (any_mask_load_store
b15b5979 1953 && ((!flag_tree_loop_vectorize && !loop->force_vectorize)
5ce9450f
JJ
1954 || loop->dont_vectorize))
1955 goto cleanup;
1956
1957 if (any_mask_load_store && !version_loop_for_if_conversion (loop))
1958 goto cleanup;
1959
e1449456
SP
1960 /* Now all statements are if-convertible. Combine all the basic
1961 blocks into one huge basic block doing the if-conversion
1962 on-the-fly. */
5ce9450f 1963 combine_blocks (loop, any_mask_load_store);
bd544141 1964
5ce9450f
JJ
1965 todo |= TODO_cleanup_cfg;
1966 if (flag_tree_loop_if_convert_stores || any_mask_load_store)
1967 {
1968 mark_virtual_operands_for_renaming (cfun);
1969 todo |= TODO_update_ssa_only_virtuals;
1970 }
40923b20 1971
e1449456 1972 cleanup:
e1449456
SP
1973 if (ifc_bbs)
1974 {
7b14477e
SP
1975 unsigned int i;
1976
1977 for (i = 0; i < loop->num_nodes; i++)
1978 free_bb_predicate (ifc_bbs[i]);
1979
e1449456
SP
1980 free (ifc_bbs);
1981 ifc_bbs = NULL;
1982 }
0f741287 1983
5ce9450f 1984 return todo;
40923b20
DP
1985}
1986
1987/* Tree if-conversion pass management. */
1988
c2924966 1989static unsigned int
40923b20
DP
1990main_tree_if_conversion (void)
1991{
40923b20 1992 struct loop *loop;
bd544141 1993 unsigned todo = 0;
40923b20 1994
0fc822d0 1995 if (number_of_loops (cfun) <= 1)
c2924966 1996 return 0;
40923b20 1997
f0bd40b1 1998 FOR_EACH_LOOP (loop, 0)
74bf76ed
JJ
1999 if (flag_tree_loop_if_convert == 1
2000 || flag_tree_loop_if_convert_stores == 1
b15b5979 2001 || ((flag_tree_loop_vectorize || loop->force_vectorize)
5ce9450f
JJ
2002 && !loop->dont_vectorize))
2003 todo |= tree_if_conversion (loop);
bd544141 2004
c6542175 2005#ifdef ENABLE_CHECKING
05232ff6
RB
2006 {
2007 basic_block bb;
11cd3bed 2008 FOR_EACH_BB_FN (bb, cfun)
05232ff6
RB
2009 gcc_assert (!bb->aux);
2010 }
c6542175
RG
2011#endif
2012
bd544141 2013 return todo;
40923b20
DP
2014}
2015
384a5197
SP
2016/* Returns true when the if-conversion pass is enabled. */
2017
40923b20
DP
2018static bool
2019gate_tree_if_conversion (void)
2020{
b15b5979 2021 return (((flag_tree_loop_vectorize || cfun->has_force_vectorize_loops)
74bf76ed 2022 && flag_tree_loop_if_convert != 0)
bd544141
SP
2023 || flag_tree_loop_if_convert == 1
2024 || flag_tree_loop_if_convert_stores == 1);
40923b20
DP
2025}
2026
27a4cd48
DM
2027namespace {
2028
2029const pass_data pass_data_if_conversion =
40923b20 2030{
27a4cd48
DM
2031 GIMPLE_PASS, /* type */
2032 "ifcvt", /* name */
2033 OPTGROUP_NONE, /* optinfo_flags */
2034 true, /* has_gate */
2035 true, /* has_execute */
2036 TV_NONE, /* tv_id */
2037 ( PROP_cfg | PROP_ssa ), /* properties_required */
2038 0, /* properties_provided */
2039 0, /* properties_destroyed */
2040 0, /* todo_flags_start */
4ded8276
RB
2041 ( TODO_verify_stmts | TODO_verify_flow
2042 | TODO_verify_ssa ), /* todo_flags_finish */
40923b20 2043};
27a4cd48
DM
2044
2045class pass_if_conversion : public gimple_opt_pass
2046{
2047public:
c3284718
RS
2048 pass_if_conversion (gcc::context *ctxt)
2049 : gimple_opt_pass (pass_data_if_conversion, ctxt)
27a4cd48
DM
2050 {}
2051
2052 /* opt_pass methods: */
2053 bool gate () { return gate_tree_if_conversion (); }
2054 unsigned int execute () { return main_tree_if_conversion (); }
2055
2056}; // class pass_if_conversion
2057
2058} // anon namespace
2059
2060gimple_opt_pass *
2061make_pass_if_conversion (gcc::context *ctxt)
2062{
2063 return new pass_if_conversion (ctxt);
2064}