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