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