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