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