rs6000.c (rs6000_expand_vector_set): Adjust for little endian.
[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 "gimple.h"
92 #include "gimple-ssa.h"
93 #include "tree-cfg.h"
94 #include "tree-phinodes.h"
95 #include "ssa-iterators.h"
96 #include "tree-ssanames.h"
97 #include "tree-into-ssa.h"
98 #include "tree-ssa.h"
99 #include "cfgloop.h"
100 #include "tree-chrec.h"
101 #include "tree-data-ref.h"
102 #include "tree-scalar-evolution.h"
103 #include "tree-pass.h"
104 #include "dbgcnt.h"
105
106 /* List of basic blocks in if-conversion-suitable order. */
107 static basic_block *ifc_bbs;
108
109 /* Structure used to predicate basic blocks. This is attached to the
110 ->aux field of the BBs in the loop to be if-converted. */
111 typedef struct bb_predicate_s {
112
113 /* The condition under which this basic block is executed. */
114 tree predicate;
115
116 /* PREDICATE is gimplified, and the sequence of statements is
117 recorded here, in order to avoid the duplication of computations
118 that occur in previous conditions. See PR44483. */
119 gimple_seq predicate_gimplified_stmts;
120 } *bb_predicate_p;
121
122 /* Returns true when the basic block BB has a predicate. */
123
124 static inline bool
125 bb_has_predicate (basic_block bb)
126 {
127 return bb->aux != NULL;
128 }
129
130 /* Returns the gimplified predicate for basic block BB. */
131
132 static inline tree
133 bb_predicate (basic_block bb)
134 {
135 return ((bb_predicate_p) bb->aux)->predicate;
136 }
137
138 /* Sets the gimplified predicate COND for basic block BB. */
139
140 static inline void
141 set_bb_predicate (basic_block bb, tree cond)
142 {
143 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
144 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
145 || is_gimple_condexpr (cond));
146 ((bb_predicate_p) bb->aux)->predicate = cond;
147 }
148
149 /* Returns the sequence of statements of the gimplification of the
150 predicate for basic block BB. */
151
152 static inline gimple_seq
153 bb_predicate_gimplified_stmts (basic_block bb)
154 {
155 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
156 }
157
158 /* Sets the sequence of statements STMTS of the gimplification of the
159 predicate for basic block BB. */
160
161 static inline void
162 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
163 {
164 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
165 }
166
167 /* Adds the sequence of statements STMTS to the sequence of statements
168 of the predicate for basic block BB. */
169
170 static inline void
171 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
172 {
173 gimple_seq_add_seq
174 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
175 }
176
177 /* Initializes to TRUE the predicate of basic block BB. */
178
179 static inline void
180 init_bb_predicate (basic_block bb)
181 {
182 bb->aux = XNEW (struct bb_predicate_s);
183 set_bb_predicate_gimplified_stmts (bb, NULL);
184 set_bb_predicate (bb, boolean_true_node);
185 }
186
187 /* Free the predicate of basic block BB. */
188
189 static inline void
190 free_bb_predicate (basic_block bb)
191 {
192 gimple_seq stmts;
193
194 if (!bb_has_predicate (bb))
195 return;
196
197 /* Release the SSA_NAMEs created for the gimplification of the
198 predicate. */
199 stmts = bb_predicate_gimplified_stmts (bb);
200 if (stmts)
201 {
202 gimple_stmt_iterator i;
203
204 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
205 free_stmt_operands (gsi_stmt (i));
206 }
207
208 free (bb->aux);
209 bb->aux = NULL;
210 }
211
212 /* Free the predicate of BB and reinitialize it with the true
213 predicate. */
214
215 static inline void
216 reset_bb_predicate (basic_block bb)
217 {
218 free_bb_predicate (bb);
219 init_bb_predicate (bb);
220 }
221
222 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
223 the expression EXPR. Inserts the statement created for this
224 computation before GSI and leaves the iterator GSI at the same
225 statement. */
226
227 static tree
228 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
229 {
230 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
231 gimple stmt = gimple_build_assign (new_name, expr);
232 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
233 return new_name;
234 }
235
236 /* Return true when COND is a true predicate. */
237
238 static inline bool
239 is_true_predicate (tree cond)
240 {
241 return (cond == NULL_TREE
242 || cond == boolean_true_node
243 || integer_onep (cond));
244 }
245
246 /* Returns true when BB has a predicate that is not trivial: true or
247 NULL_TREE. */
248
249 static inline bool
250 is_predicated (basic_block bb)
251 {
252 return !is_true_predicate (bb_predicate (bb));
253 }
254
255 /* Parses the predicate COND and returns its comparison code and
256 operands OP0 and OP1. */
257
258 static enum tree_code
259 parse_predicate (tree cond, tree *op0, tree *op1)
260 {
261 gimple s;
262
263 if (TREE_CODE (cond) == SSA_NAME
264 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
265 {
266 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
267 {
268 *op0 = gimple_assign_rhs1 (s);
269 *op1 = gimple_assign_rhs2 (s);
270 return gimple_assign_rhs_code (s);
271 }
272
273 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
274 {
275 tree op = gimple_assign_rhs1 (s);
276 tree type = TREE_TYPE (op);
277 enum tree_code code = parse_predicate (op, op0, op1);
278
279 return code == ERROR_MARK ? ERROR_MARK
280 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
281 }
282
283 return ERROR_MARK;
284 }
285
286 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
287 {
288 *op0 = TREE_OPERAND (cond, 0);
289 *op1 = TREE_OPERAND (cond, 1);
290 return TREE_CODE (cond);
291 }
292
293 return ERROR_MARK;
294 }
295
296 /* Returns the fold of predicate C1 OR C2 at location LOC. */
297
298 static tree
299 fold_or_predicates (location_t loc, tree c1, tree c2)
300 {
301 tree op1a, op1b, op2a, op2b;
302 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
303 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
304
305 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
306 {
307 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
308 code2, op2a, op2b);
309 if (t)
310 return t;
311 }
312
313 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
314 }
315
316 /* Returns true if N is either a constant or a SSA_NAME. */
317
318 static bool
319 constant_or_ssa_name (tree n)
320 {
321 switch (TREE_CODE (n))
322 {
323 case SSA_NAME:
324 case INTEGER_CST:
325 case REAL_CST:
326 case COMPLEX_CST:
327 case VECTOR_CST:
328 return true;
329 default:
330 return false;
331 }
332 }
333
334 /* Returns either a COND_EXPR or the folded expression if the folded
335 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
336 a constant or a SSA_NAME. */
337
338 static tree
339 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
340 {
341 tree rhs1, lhs1, cond_expr;
342 cond_expr = fold_ternary (COND_EXPR, type, cond,
343 rhs, lhs);
344
345 if (cond_expr == NULL_TREE)
346 return build3 (COND_EXPR, type, cond, rhs, lhs);
347
348 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
349
350 if (constant_or_ssa_name (cond_expr))
351 return cond_expr;
352
353 if (TREE_CODE (cond_expr) == ABS_EXPR)
354 {
355 rhs1 = TREE_OPERAND (cond_expr, 1);
356 STRIP_USELESS_TYPE_CONVERSION (rhs1);
357 if (constant_or_ssa_name (rhs1))
358 return build1 (ABS_EXPR, type, rhs1);
359 }
360
361 if (TREE_CODE (cond_expr) == MIN_EXPR
362 || TREE_CODE (cond_expr) == MAX_EXPR)
363 {
364 lhs1 = TREE_OPERAND (cond_expr, 0);
365 STRIP_USELESS_TYPE_CONVERSION (lhs1);
366 rhs1 = TREE_OPERAND (cond_expr, 1);
367 STRIP_USELESS_TYPE_CONVERSION (rhs1);
368 if (constant_or_ssa_name (rhs1)
369 && constant_or_ssa_name (lhs1))
370 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
371 }
372 return build3 (COND_EXPR, type, cond, rhs, lhs);
373 }
374
375 /* Add condition NC to the predicate list of basic block BB. */
376
377 static inline void
378 add_to_predicate_list (basic_block bb, tree nc)
379 {
380 tree bc, *tp;
381
382 if (is_true_predicate (nc))
383 return;
384
385 if (!is_predicated (bb))
386 bc = nc;
387 else
388 {
389 bc = bb_predicate (bb);
390 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
391 if (is_true_predicate (bc))
392 {
393 reset_bb_predicate (bb);
394 return;
395 }
396 }
397
398 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
399 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
400 tp = &TREE_OPERAND (bc, 0);
401 else
402 tp = &bc;
403 if (!is_gimple_condexpr (*tp))
404 {
405 gimple_seq stmts;
406 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
407 add_bb_predicate_gimplified_stmts (bb, stmts);
408 }
409 set_bb_predicate (bb, bc);
410 }
411
412 /* Add the condition COND to the previous condition PREV_COND, and add
413 this to the predicate list of the destination of edge E. LOOP is
414 the loop to be if-converted. */
415
416 static void
417 add_to_dst_predicate_list (struct loop *loop, edge e,
418 tree prev_cond, tree cond)
419 {
420 if (!flow_bb_inside_loop_p (loop, e->dest))
421 return;
422
423 if (!is_true_predicate (prev_cond))
424 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
425 prev_cond, cond);
426
427 add_to_predicate_list (e->dest, cond);
428 }
429
430 /* Return true if one of the successor edges of BB exits LOOP. */
431
432 static bool
433 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
434 {
435 edge e;
436 edge_iterator ei;
437
438 FOR_EACH_EDGE (e, ei, bb->succs)
439 if (loop_exit_edge_p (loop, e))
440 return true;
441
442 return false;
443 }
444
445 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
446 and it belongs to basic block BB.
447
448 PHI is not if-convertible if:
449 - it has more than 2 arguments.
450
451 When the flag_tree_loop_if_convert_stores is not set, PHI is not
452 if-convertible if:
453 - a virtual PHI is immediately used in another PHI node,
454 - there is a virtual PHI in a BB other than the loop->header. */
455
456 static bool
457 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
458 {
459 if (dump_file && (dump_flags & TDF_DETAILS))
460 {
461 fprintf (dump_file, "-------------------------\n");
462 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
463 }
464
465 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
466 {
467 if (dump_file && (dump_flags & TDF_DETAILS))
468 fprintf (dump_file, "More than two phi node args.\n");
469 return false;
470 }
471
472 if (flag_tree_loop_if_convert_stores)
473 return true;
474
475 /* When the flag_tree_loop_if_convert_stores is not set, check
476 that there are no memory writes in the branches of the loop to be
477 if-converted. */
478 if (virtual_operand_p (gimple_phi_result (phi)))
479 {
480 imm_use_iterator imm_iter;
481 use_operand_p use_p;
482
483 if (bb != loop->header)
484 {
485 if (dump_file && (dump_flags & TDF_DETAILS))
486 fprintf (dump_file, "Virtual phi not on loop->header.\n");
487 return false;
488 }
489
490 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
491 {
492 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
493 {
494 if (dump_file && (dump_flags & TDF_DETAILS))
495 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
496 return false;
497 }
498 }
499 }
500
501 return true;
502 }
503
504 /* Records the status of a data reference. This struct is attached to
505 each DR->aux field. */
506
507 struct ifc_dr {
508 /* -1 when not initialized, 0 when false, 1 when true. */
509 int written_at_least_once;
510
511 /* -1 when not initialized, 0 when false, 1 when true. */
512 int rw_unconditionally;
513 };
514
515 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
516 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
517 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
518
519 /* Returns true when the memory references of STMT are read or written
520 unconditionally. In other words, this function returns true when
521 for every data reference A in STMT there exist other accesses to
522 a data reference with the same base with predicates that add up (OR-up) to
523 the true predicate: this ensures that the data reference A is touched
524 (read or written) on every iteration of the if-converted loop. */
525
526 static bool
527 memrefs_read_or_written_unconditionally (gimple stmt,
528 vec<data_reference_p> drs)
529 {
530 int i, j;
531 data_reference_p a, b;
532 tree ca = bb_predicate (gimple_bb (stmt));
533
534 for (i = 0; drs.iterate (i, &a); i++)
535 if (DR_STMT (a) == stmt)
536 {
537 bool found = false;
538 int x = DR_RW_UNCONDITIONALLY (a);
539
540 if (x == 0)
541 return false;
542
543 if (x == 1)
544 continue;
545
546 for (j = 0; drs.iterate (j, &b); j++)
547 {
548 tree ref_base_a = DR_REF (a);
549 tree ref_base_b = DR_REF (b);
550
551 if (DR_STMT (b) == stmt)
552 continue;
553
554 while (TREE_CODE (ref_base_a) == COMPONENT_REF
555 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
556 || TREE_CODE (ref_base_a) == REALPART_EXPR)
557 ref_base_a = TREE_OPERAND (ref_base_a, 0);
558
559 while (TREE_CODE (ref_base_b) == COMPONENT_REF
560 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
561 || TREE_CODE (ref_base_b) == REALPART_EXPR)
562 ref_base_b = TREE_OPERAND (ref_base_b, 0);
563
564 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
565 {
566 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
567
568 if (DR_RW_UNCONDITIONALLY (b) == 1
569 || is_true_predicate (cb)
570 || is_true_predicate (ca
571 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
572 {
573 DR_RW_UNCONDITIONALLY (a) = 1;
574 DR_RW_UNCONDITIONALLY (b) = 1;
575 found = true;
576 break;
577 }
578 }
579 }
580
581 if (!found)
582 {
583 DR_RW_UNCONDITIONALLY (a) = 0;
584 return false;
585 }
586 }
587
588 return true;
589 }
590
591 /* Returns true when the memory references of STMT are unconditionally
592 written. In other words, this function returns true when for every
593 data reference A written in STMT, there exist other writes to the
594 same data reference with predicates that add up (OR-up) to the true
595 predicate: this ensures that the data reference A is written on
596 every iteration of the if-converted loop. */
597
598 static bool
599 write_memrefs_written_at_least_once (gimple stmt,
600 vec<data_reference_p> drs)
601 {
602 int i, j;
603 data_reference_p a, b;
604 tree ca = bb_predicate (gimple_bb (stmt));
605
606 for (i = 0; drs.iterate (i, &a); i++)
607 if (DR_STMT (a) == stmt
608 && DR_IS_WRITE (a))
609 {
610 bool found = false;
611 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
612
613 if (x == 0)
614 return false;
615
616 if (x == 1)
617 continue;
618
619 for (j = 0; drs.iterate (j, &b); j++)
620 if (DR_STMT (b) != stmt
621 && DR_IS_WRITE (b)
622 && same_data_refs_base_objects (a, b))
623 {
624 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
625
626 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
627 || is_true_predicate (cb)
628 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
629 ca, cb)))
630 {
631 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
632 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
633 found = true;
634 break;
635 }
636 }
637
638 if (!found)
639 {
640 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
641 return false;
642 }
643 }
644
645 return true;
646 }
647
648 /* Return true when the memory references of STMT won't trap in the
649 if-converted code. There are two things that we have to check for:
650
651 - writes to memory occur to writable memory: if-conversion of
652 memory writes transforms the conditional memory writes into
653 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
654 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
655 be executed at all in the original code, it may be a readonly
656 memory. To check that A is not const-qualified, we check that
657 there exists at least an unconditional write to A in the current
658 function.
659
660 - reads or writes to memory are valid memory accesses for every
661 iteration. To check that the memory accesses are correctly formed
662 and that we are allowed to read and write in these locations, we
663 check that the memory accesses to be if-converted occur at every
664 iteration unconditionally. */
665
666 static bool
667 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
668 {
669 return write_memrefs_written_at_least_once (stmt, refs)
670 && memrefs_read_or_written_unconditionally (stmt, refs);
671 }
672
673 /* Wrapper around gimple_could_trap_p refined for the needs of the
674 if-conversion. Try to prove that the memory accesses of STMT could
675 not trap in the innermost loop containing STMT. */
676
677 static bool
678 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
679 {
680 if (gimple_vuse (stmt)
681 && !gimple_could_trap_p_1 (stmt, false, false)
682 && ifcvt_memrefs_wont_trap (stmt, refs))
683 return false;
684
685 return gimple_could_trap_p (stmt);
686 }
687
688 /* Return true when STMT is if-convertible.
689
690 GIMPLE_ASSIGN statement is not if-convertible if,
691 - it is not movable,
692 - it could trap,
693 - LHS is not var decl. */
694
695 static bool
696 if_convertible_gimple_assign_stmt_p (gimple stmt,
697 vec<data_reference_p> refs)
698 {
699 tree lhs = gimple_assign_lhs (stmt);
700 basic_block bb;
701
702 if (dump_file && (dump_flags & TDF_DETAILS))
703 {
704 fprintf (dump_file, "-------------------------\n");
705 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
706 }
707
708 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
709 return false;
710
711 /* Some of these constrains might be too conservative. */
712 if (stmt_ends_bb_p (stmt)
713 || gimple_has_volatile_ops (stmt)
714 || (TREE_CODE (lhs) == SSA_NAME
715 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
716 || gimple_has_side_effects (stmt))
717 {
718 if (dump_file && (dump_flags & TDF_DETAILS))
719 fprintf (dump_file, "stmt not suitable for ifcvt\n");
720 return false;
721 }
722
723 if (flag_tree_loop_if_convert_stores)
724 {
725 if (ifcvt_could_trap_p (stmt, refs))
726 {
727 if (dump_file && (dump_flags & TDF_DETAILS))
728 fprintf (dump_file, "tree could trap...\n");
729 return false;
730 }
731 return true;
732 }
733
734 if (gimple_assign_rhs_could_trap_p (stmt))
735 {
736 if (dump_file && (dump_flags & TDF_DETAILS))
737 fprintf (dump_file, "tree could trap...\n");
738 return false;
739 }
740
741 bb = gimple_bb (stmt);
742
743 if (TREE_CODE (lhs) != SSA_NAME
744 && bb != bb->loop_father->header
745 && !bb_with_exit_edge_p (bb->loop_father, bb))
746 {
747 if (dump_file && (dump_flags & TDF_DETAILS))
748 {
749 fprintf (dump_file, "LHS is not var\n");
750 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
751 }
752 return false;
753 }
754
755 return true;
756 }
757
758 /* Return true when STMT is if-convertible.
759
760 A statement is if-convertible if:
761 - it is an if-convertible GIMPLE_ASSIGN,
762 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
763
764 static bool
765 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs)
766 {
767 switch (gimple_code (stmt))
768 {
769 case GIMPLE_LABEL:
770 case GIMPLE_DEBUG:
771 case GIMPLE_COND:
772 return true;
773
774 case GIMPLE_ASSIGN:
775 return if_convertible_gimple_assign_stmt_p (stmt, refs);
776
777 case GIMPLE_CALL:
778 {
779 tree fndecl = gimple_call_fndecl (stmt);
780 if (fndecl)
781 {
782 int flags = gimple_call_flags (stmt);
783 if ((flags & ECF_CONST)
784 && !(flags & ECF_LOOPING_CONST_OR_PURE)
785 /* We can only vectorize some builtins at the moment,
786 so restrict if-conversion to those. */
787 && DECL_BUILT_IN (fndecl))
788 return true;
789 }
790 return false;
791 }
792
793 default:
794 /* Don't know what to do with 'em so don't do anything. */
795 if (dump_file && (dump_flags & TDF_DETAILS))
796 {
797 fprintf (dump_file, "don't know what to do\n");
798 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
799 }
800 return false;
801 break;
802 }
803
804 return true;
805 }
806
807 /* Return true when BB is if-convertible. This routine does not check
808 basic block's statements and phis.
809
810 A basic block is not if-convertible if:
811 - it is non-empty and it is after the exit block (in BFS order),
812 - it is after the exit block but before the latch,
813 - its edges are not normal.
814
815 EXIT_BB is the basic block containing the exit of the LOOP. BB is
816 inside LOOP. */
817
818 static bool
819 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
820 {
821 edge e;
822 edge_iterator ei;
823
824 if (dump_file && (dump_flags & TDF_DETAILS))
825 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
826
827 if (EDGE_COUNT (bb->preds) > 2
828 || EDGE_COUNT (bb->succs) > 2)
829 return false;
830
831 if (exit_bb)
832 {
833 if (bb != loop->latch)
834 {
835 if (dump_file && (dump_flags & TDF_DETAILS))
836 fprintf (dump_file, "basic block after exit bb but before latch\n");
837 return false;
838 }
839 else if (!empty_block_p (bb))
840 {
841 if (dump_file && (dump_flags & TDF_DETAILS))
842 fprintf (dump_file, "non empty basic block after exit bb\n");
843 return false;
844 }
845 else if (bb == loop->latch
846 && bb != exit_bb
847 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
848 {
849 if (dump_file && (dump_flags & TDF_DETAILS))
850 fprintf (dump_file, "latch is not dominated by exit_block\n");
851 return false;
852 }
853 }
854
855 /* Be less adventurous and handle only normal edges. */
856 FOR_EACH_EDGE (e, ei, bb->succs)
857 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
858 {
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 fprintf (dump_file, "Difficult to handle edges\n");
861 return false;
862 }
863
864 /* At least one incoming edge has to be non-critical as otherwise edge
865 predicates are not equal to basic-block predicates of the edge
866 source. */
867 if (EDGE_COUNT (bb->preds) > 1
868 && bb != loop->header)
869 {
870 bool found = false;
871 FOR_EACH_EDGE (e, ei, bb->preds)
872 if (EDGE_COUNT (e->src->succs) == 1)
873 found = true;
874 if (!found)
875 {
876 if (dump_file && (dump_flags & TDF_DETAILS))
877 fprintf (dump_file, "only critical predecessors\n");
878 return false;
879 }
880 }
881
882 return true;
883 }
884
885 /* Return true when all predecessor blocks of BB are visited. The
886 VISITED bitmap keeps track of the visited blocks. */
887
888 static bool
889 pred_blocks_visited_p (basic_block bb, bitmap *visited)
890 {
891 edge e;
892 edge_iterator ei;
893 FOR_EACH_EDGE (e, ei, bb->preds)
894 if (!bitmap_bit_p (*visited, e->src->index))
895 return false;
896
897 return true;
898 }
899
900 /* Get body of a LOOP in suitable order for if-conversion. It is
901 caller's responsibility to deallocate basic block list.
902 If-conversion suitable order is, breadth first sort (BFS) order
903 with an additional constraint: select a block only if all its
904 predecessors are already selected. */
905
906 static basic_block *
907 get_loop_body_in_if_conv_order (const struct loop *loop)
908 {
909 basic_block *blocks, *blocks_in_bfs_order;
910 basic_block bb;
911 bitmap visited;
912 unsigned int index = 0;
913 unsigned int visited_count = 0;
914
915 gcc_assert (loop->num_nodes);
916 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
917
918 blocks = XCNEWVEC (basic_block, loop->num_nodes);
919 visited = BITMAP_ALLOC (NULL);
920
921 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
922
923 index = 0;
924 while (index < loop->num_nodes)
925 {
926 bb = blocks_in_bfs_order [index];
927
928 if (bb->flags & BB_IRREDUCIBLE_LOOP)
929 {
930 free (blocks_in_bfs_order);
931 BITMAP_FREE (visited);
932 free (blocks);
933 return NULL;
934 }
935
936 if (!bitmap_bit_p (visited, bb->index))
937 {
938 if (pred_blocks_visited_p (bb, &visited)
939 || bb == loop->header)
940 {
941 /* This block is now visited. */
942 bitmap_set_bit (visited, bb->index);
943 blocks[visited_count++] = bb;
944 }
945 }
946
947 index++;
948
949 if (index == loop->num_nodes
950 && visited_count != loop->num_nodes)
951 /* Not done yet. */
952 index = 0;
953 }
954 free (blocks_in_bfs_order);
955 BITMAP_FREE (visited);
956 return blocks;
957 }
958
959 /* Returns true when the analysis of the predicates for all the basic
960 blocks in LOOP succeeded.
961
962 predicate_bbs first allocates the predicates of the basic blocks.
963 These fields are then initialized with the tree expressions
964 representing the predicates under which a basic block is executed
965 in the LOOP. As the loop->header is executed at each iteration, it
966 has the "true" predicate. Other statements executed under a
967 condition are predicated with that condition, for example
968
969 | if (x)
970 | S1;
971 | else
972 | S2;
973
974 S1 will be predicated with "x", and
975 S2 will be predicated with "!x". */
976
977 static bool
978 predicate_bbs (loop_p loop)
979 {
980 unsigned int i;
981
982 for (i = 0; i < loop->num_nodes; i++)
983 init_bb_predicate (ifc_bbs[i]);
984
985 for (i = 0; i < loop->num_nodes; i++)
986 {
987 basic_block bb = ifc_bbs[i];
988 tree cond;
989 gimple_stmt_iterator itr;
990
991 /* The loop latch is always executed and has no extra conditions
992 to be processed: skip it. */
993 if (bb == loop->latch)
994 {
995 reset_bb_predicate (loop->latch);
996 continue;
997 }
998
999 cond = bb_predicate (bb);
1000
1001 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1002 {
1003 gimple stmt = gsi_stmt (itr);
1004
1005 switch (gimple_code (stmt))
1006 {
1007 case GIMPLE_LABEL:
1008 case GIMPLE_ASSIGN:
1009 case GIMPLE_CALL:
1010 case GIMPLE_DEBUG:
1011 break;
1012
1013 case GIMPLE_COND:
1014 {
1015 tree c2;
1016 edge true_edge, false_edge;
1017 location_t loc = gimple_location (stmt);
1018 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1019 boolean_type_node,
1020 gimple_cond_lhs (stmt),
1021 gimple_cond_rhs (stmt));
1022
1023 /* Add new condition into destination's predicate list. */
1024 extract_true_false_edges_from_block (gimple_bb (stmt),
1025 &true_edge, &false_edge);
1026
1027 /* If C is true, then TRUE_EDGE is taken. */
1028 add_to_dst_predicate_list (loop, true_edge,
1029 unshare_expr (cond),
1030 unshare_expr (c));
1031
1032 /* If C is false, then FALSE_EDGE is taken. */
1033 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
1034 boolean_type_node, unshare_expr (c));
1035 add_to_dst_predicate_list (loop, false_edge,
1036 unshare_expr (cond), c2);
1037
1038 cond = NULL_TREE;
1039 break;
1040 }
1041
1042 default:
1043 /* Not handled yet in if-conversion. */
1044 return false;
1045 }
1046 }
1047
1048 /* If current bb has only one successor, then consider it as an
1049 unconditional goto. */
1050 if (single_succ_p (bb))
1051 {
1052 basic_block bb_n = single_succ (bb);
1053
1054 /* The successor bb inherits the predicate of its
1055 predecessor. If there is no predicate in the predecessor
1056 bb, then consider the successor bb as always executed. */
1057 if (cond == NULL_TREE)
1058 cond = boolean_true_node;
1059
1060 add_to_predicate_list (bb_n, cond);
1061 }
1062 }
1063
1064 /* The loop header is always executed. */
1065 reset_bb_predicate (loop->header);
1066 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1067 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1068
1069 return true;
1070 }
1071
1072 /* Return true when LOOP is if-convertible. This is a helper function
1073 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1074 in if_convertible_loop_p. */
1075
1076 static bool
1077 if_convertible_loop_p_1 (struct loop *loop,
1078 vec<loop_p> *loop_nest,
1079 vec<data_reference_p> *refs,
1080 vec<ddr_p> *ddrs)
1081 {
1082 bool res;
1083 unsigned int i;
1084 basic_block exit_bb = NULL;
1085
1086 /* Don't if-convert the loop when the data dependences cannot be
1087 computed: the loop won't be vectorized in that case. */
1088 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1089 if (!res)
1090 return false;
1091
1092 calculate_dominance_info (CDI_DOMINATORS);
1093
1094 /* Allow statements that can be handled during if-conversion. */
1095 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1096 if (!ifc_bbs)
1097 {
1098 if (dump_file && (dump_flags & TDF_DETAILS))
1099 fprintf (dump_file, "Irreducible loop\n");
1100 return false;
1101 }
1102
1103 for (i = 0; i < loop->num_nodes; i++)
1104 {
1105 basic_block bb = ifc_bbs[i];
1106
1107 if (!if_convertible_bb_p (loop, bb, exit_bb))
1108 return false;
1109
1110 if (bb_with_exit_edge_p (loop, bb))
1111 exit_bb = bb;
1112 }
1113
1114 res = predicate_bbs (loop);
1115 if (!res)
1116 return false;
1117
1118 if (flag_tree_loop_if_convert_stores)
1119 {
1120 data_reference_p dr;
1121
1122 for (i = 0; refs->iterate (i, &dr); i++)
1123 {
1124 dr->aux = XNEW (struct ifc_dr);
1125 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1126 DR_RW_UNCONDITIONALLY (dr) = -1;
1127 }
1128 }
1129
1130 for (i = 0; i < loop->num_nodes; i++)
1131 {
1132 basic_block bb = ifc_bbs[i];
1133 gimple_stmt_iterator itr;
1134
1135 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1136 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1137 return false;
1138
1139 /* Check the if-convertibility of statements in predicated BBs. */
1140 if (is_predicated (bb))
1141 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1142 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1143 return false;
1144 }
1145
1146 if (dump_file)
1147 fprintf (dump_file, "Applying if-conversion\n");
1148
1149 return true;
1150 }
1151
1152 /* Return true when LOOP is if-convertible.
1153 LOOP is if-convertible if:
1154 - it is innermost,
1155 - it has two or more basic blocks,
1156 - it has only one exit,
1157 - loop header is not the exit edge,
1158 - if its basic blocks and phi nodes are if convertible. */
1159
1160 static bool
1161 if_convertible_loop_p (struct loop *loop)
1162 {
1163 edge e;
1164 edge_iterator ei;
1165 bool res = false;
1166 vec<data_reference_p> refs;
1167 vec<ddr_p> ddrs;
1168
1169 /* Handle only innermost loop. */
1170 if (!loop || loop->inner)
1171 {
1172 if (dump_file && (dump_flags & TDF_DETAILS))
1173 fprintf (dump_file, "not innermost loop\n");
1174 return false;
1175 }
1176
1177 /* If only one block, no need for if-conversion. */
1178 if (loop->num_nodes <= 2)
1179 {
1180 if (dump_file && (dump_flags & TDF_DETAILS))
1181 fprintf (dump_file, "less than 2 basic blocks\n");
1182 return false;
1183 }
1184
1185 /* More than one loop exit is too much to handle. */
1186 if (!single_exit (loop))
1187 {
1188 if (dump_file && (dump_flags & TDF_DETAILS))
1189 fprintf (dump_file, "multiple exits\n");
1190 return false;
1191 }
1192
1193 /* If one of the loop header's edge is an exit edge then do not
1194 apply if-conversion. */
1195 FOR_EACH_EDGE (e, ei, loop->header->succs)
1196 if (loop_exit_edge_p (loop, e))
1197 return false;
1198
1199 refs.create (5);
1200 ddrs.create (25);
1201 stack_vec<loop_p, 3> loop_nest;
1202 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1203
1204 if (flag_tree_loop_if_convert_stores)
1205 {
1206 data_reference_p dr;
1207 unsigned int i;
1208
1209 for (i = 0; refs.iterate (i, &dr); i++)
1210 free (dr->aux);
1211 }
1212
1213 loop_nest.release ();
1214 free_data_refs (refs);
1215 free_dependence_relations (ddrs);
1216 return res;
1217 }
1218
1219 /* Basic block BB has two predecessors. Using predecessor's bb
1220 predicate, set an appropriate condition COND for the PHI node
1221 replacement. Return the true block whose phi arguments are
1222 selected when cond is true. LOOP is the loop containing the
1223 if-converted region, GSI is the place to insert the code for the
1224 if-conversion. */
1225
1226 static basic_block
1227 find_phi_replacement_condition (basic_block bb, tree *cond,
1228 gimple_stmt_iterator *gsi)
1229 {
1230 edge first_edge, second_edge;
1231 tree tmp_cond;
1232
1233 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1234 first_edge = EDGE_PRED (bb, 0);
1235 second_edge = EDGE_PRED (bb, 1);
1236
1237 /* Prefer an edge with a not negated predicate.
1238 ??? That's a very weak cost model. */
1239 tmp_cond = bb_predicate (first_edge->src);
1240 gcc_assert (tmp_cond);
1241 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1242 {
1243 edge tmp_edge;
1244
1245 tmp_edge = first_edge;
1246 first_edge = second_edge;
1247 second_edge = tmp_edge;
1248 }
1249
1250 /* Check if the edge we take the condition from is not critical.
1251 We know that at least one non-critical edge exists. */
1252 if (EDGE_COUNT (first_edge->src->succs) > 1)
1253 {
1254 *cond = bb_predicate (second_edge->src);
1255
1256 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1257 *cond = TREE_OPERAND (*cond, 0);
1258 else
1259 /* Select non loop header bb. */
1260 first_edge = second_edge;
1261 }
1262 else
1263 *cond = bb_predicate (first_edge->src);
1264
1265 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1266 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1267 is_gimple_condexpr, NULL_TREE,
1268 true, GSI_SAME_STMT);
1269
1270 return first_edge->src;
1271 }
1272
1273 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1274 This routine does not handle PHI nodes with more than two
1275 arguments.
1276
1277 For example,
1278 S1: A = PHI <x1(1), x2(5)>
1279 is converted into,
1280 S2: A = cond ? x1 : x2;
1281
1282 The generated code is inserted at GSI that points to the top of
1283 basic block's statement list. When COND is true, phi arg from
1284 TRUE_BB is selected. */
1285
1286 static void
1287 predicate_scalar_phi (gimple phi, tree cond,
1288 basic_block true_bb,
1289 gimple_stmt_iterator *gsi)
1290 {
1291 gimple new_stmt;
1292 basic_block bb;
1293 tree rhs, res, arg, scev;
1294
1295 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1296 && gimple_phi_num_args (phi) == 2);
1297
1298 res = gimple_phi_result (phi);
1299 /* Do not handle virtual phi nodes. */
1300 if (virtual_operand_p (res))
1301 return;
1302
1303 bb = gimple_bb (phi);
1304
1305 if ((arg = degenerate_phi_result (phi))
1306 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1307 res))
1308 && !chrec_contains_undetermined (scev)
1309 && scev != res
1310 && (arg = gimple_phi_arg_def (phi, 0))))
1311 rhs = arg;
1312 else
1313 {
1314 tree arg_0, arg_1;
1315 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1316 if (EDGE_PRED (bb, 1)->src == true_bb)
1317 {
1318 arg_0 = gimple_phi_arg_def (phi, 1);
1319 arg_1 = gimple_phi_arg_def (phi, 0);
1320 }
1321 else
1322 {
1323 arg_0 = gimple_phi_arg_def (phi, 0);
1324 arg_1 = gimple_phi_arg_def (phi, 1);
1325 }
1326
1327 /* Build new RHS using selected condition and arguments. */
1328 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1329 arg_0, arg_1);
1330 }
1331
1332 new_stmt = gimple_build_assign (res, rhs);
1333 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
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 }