system.h (dump_file): Do not define.
[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 &
823 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
824 {
825 if (dump_file && (dump_flags & TDF_DETAILS))
826 fprintf (dump_file, "Difficult to handle edges\n");
827 return false;
828 }
829
830 if (EDGE_COUNT (bb->preds) == 2
831 && bb != loop->header
832 && !bb_postdominates_preds (bb))
833 return false;
834
835 return true;
836 }
837
838 /* Return true when all predecessor blocks of BB are visited. The
839 VISITED bitmap keeps track of the visited blocks. */
840
841 static bool
842 pred_blocks_visited_p (basic_block bb, bitmap *visited)
843 {
844 edge e;
845 edge_iterator ei;
846 FOR_EACH_EDGE (e, ei, bb->preds)
847 if (!bitmap_bit_p (*visited, e->src->index))
848 return false;
849
850 return true;
851 }
852
853 /* Get body of a LOOP in suitable order for if-conversion. It is
854 caller's responsibility to deallocate basic block list.
855 If-conversion suitable order is, breadth first sort (BFS) order
856 with an additional constraint: select a block only if all its
857 predecessors are already selected. */
858
859 static basic_block *
860 get_loop_body_in_if_conv_order (const struct loop *loop)
861 {
862 basic_block *blocks, *blocks_in_bfs_order;
863 basic_block bb;
864 bitmap visited;
865 unsigned int index = 0;
866 unsigned int visited_count = 0;
867
868 gcc_assert (loop->num_nodes);
869 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
870
871 blocks = XCNEWVEC (basic_block, loop->num_nodes);
872 visited = BITMAP_ALLOC (NULL);
873
874 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
875
876 index = 0;
877 while (index < loop->num_nodes)
878 {
879 bb = blocks_in_bfs_order [index];
880
881 if (bb->flags & BB_IRREDUCIBLE_LOOP)
882 {
883 free (blocks_in_bfs_order);
884 BITMAP_FREE (visited);
885 free (blocks);
886 return NULL;
887 }
888
889 if (!bitmap_bit_p (visited, bb->index))
890 {
891 if (pred_blocks_visited_p (bb, &visited)
892 || bb == loop->header)
893 {
894 /* This block is now visited. */
895 bitmap_set_bit (visited, bb->index);
896 blocks[visited_count++] = bb;
897 }
898 }
899
900 index++;
901
902 if (index == loop->num_nodes
903 && visited_count != loop->num_nodes)
904 /* Not done yet. */
905 index = 0;
906 }
907 free (blocks_in_bfs_order);
908 BITMAP_FREE (visited);
909 return blocks;
910 }
911
912 /* Returns true when the analysis of the predicates for all the basic
913 blocks in LOOP succeeded.
914
915 predicate_bbs first allocates the predicates of the basic blocks.
916 These fields are then initialized with the tree expressions
917 representing the predicates under which a basic block is executed
918 in the LOOP. As the loop->header is executed at each iteration, it
919 has the "true" predicate. Other statements executed under a
920 condition are predicated with that condition, for example
921
922 | if (x)
923 | S1;
924 | else
925 | S2;
926
927 S1 will be predicated with "x", and
928 S2 will be predicated with "!x". */
929
930 static bool
931 predicate_bbs (loop_p loop)
932 {
933 unsigned int i;
934
935 for (i = 0; i < loop->num_nodes; i++)
936 init_bb_predicate (ifc_bbs[i]);
937
938 for (i = 0; i < loop->num_nodes; i++)
939 {
940 basic_block bb = ifc_bbs[i];
941 tree cond;
942 gimple_stmt_iterator itr;
943
944 /* The loop latch is always executed and has no extra conditions
945 to be processed: skip it. */
946 if (bb == loop->latch)
947 {
948 reset_bb_predicate (loop->latch);
949 continue;
950 }
951
952 cond = bb_predicate (bb);
953
954 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
955 {
956 gimple stmt = gsi_stmt (itr);
957
958 switch (gimple_code (stmt))
959 {
960 case GIMPLE_LABEL:
961 case GIMPLE_ASSIGN:
962 case GIMPLE_CALL:
963 case GIMPLE_DEBUG:
964 break;
965
966 case GIMPLE_COND:
967 {
968 tree c2;
969 edge true_edge, false_edge;
970 location_t loc = gimple_location (stmt);
971 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
972 boolean_type_node,
973 gimple_cond_lhs (stmt),
974 gimple_cond_rhs (stmt));
975
976 /* Add new condition into destination's predicate list. */
977 extract_true_false_edges_from_block (gimple_bb (stmt),
978 &true_edge, &false_edge);
979
980 /* If C is true, then TRUE_EDGE is taken. */
981 add_to_dst_predicate_list (loop, true_edge,
982 unshare_expr (cond),
983 unshare_expr (c));
984
985 /* If C is false, then FALSE_EDGE is taken. */
986 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
987 boolean_type_node, unshare_expr (c));
988 add_to_dst_predicate_list (loop, false_edge,
989 unshare_expr (cond), c2);
990
991 cond = NULL_TREE;
992 break;
993 }
994
995 default:
996 /* Not handled yet in if-conversion. */
997 return false;
998 }
999 }
1000
1001 /* If current bb has only one successor, then consider it as an
1002 unconditional goto. */
1003 if (single_succ_p (bb))
1004 {
1005 basic_block bb_n = single_succ (bb);
1006
1007 /* The successor bb inherits the predicate of its
1008 predecessor. If there is no predicate in the predecessor
1009 bb, then consider the successor bb as always executed. */
1010 if (cond == NULL_TREE)
1011 cond = boolean_true_node;
1012
1013 add_to_predicate_list (bb_n, cond);
1014 }
1015 }
1016
1017 /* The loop header is always executed. */
1018 reset_bb_predicate (loop->header);
1019 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1020 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1021
1022 return true;
1023 }
1024
1025 /* Return true when LOOP is if-convertible. This is a helper function
1026 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1027 in if_convertible_loop_p. */
1028
1029 static bool
1030 if_convertible_loop_p_1 (struct loop *loop,
1031 VEC (loop_p, heap) **loop_nest,
1032 VEC (data_reference_p, heap) **refs,
1033 VEC (ddr_p, heap) **ddrs)
1034 {
1035 bool res;
1036 unsigned int i;
1037 basic_block exit_bb = NULL;
1038
1039 /* Don't if-convert the loop when the data dependences cannot be
1040 computed: the loop won't be vectorized in that case. */
1041 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1042 if (!res)
1043 return false;
1044
1045 calculate_dominance_info (CDI_DOMINATORS);
1046 calculate_dominance_info (CDI_POST_DOMINATORS);
1047
1048 /* Allow statements that can be handled during if-conversion. */
1049 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1050 if (!ifc_bbs)
1051 {
1052 if (dump_file && (dump_flags & TDF_DETAILS))
1053 fprintf (dump_file, "Irreducible loop\n");
1054 return false;
1055 }
1056
1057 for (i = 0; i < loop->num_nodes; i++)
1058 {
1059 basic_block bb = ifc_bbs[i];
1060
1061 if (!if_convertible_bb_p (loop, bb, exit_bb))
1062 return false;
1063
1064 if (bb_with_exit_edge_p (loop, bb))
1065 exit_bb = bb;
1066 }
1067
1068 res = predicate_bbs (loop);
1069 if (!res)
1070 return false;
1071
1072 if (flag_tree_loop_if_convert_stores)
1073 {
1074 data_reference_p dr;
1075
1076 for (i = 0; VEC_iterate (data_reference_p, *refs, i, dr); i++)
1077 {
1078 dr->aux = XNEW (struct ifc_dr);
1079 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1080 DR_RW_UNCONDITIONALLY (dr) = -1;
1081 }
1082 }
1083
1084 for (i = 0; i < loop->num_nodes; i++)
1085 {
1086 basic_block bb = ifc_bbs[i];
1087 gimple_stmt_iterator itr;
1088
1089 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1090 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1091 return false;
1092
1093 /* Check the if-convertibility of statements in predicated BBs. */
1094 if (is_predicated (bb))
1095 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1096 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1097 return false;
1098 }
1099
1100 if (dump_file)
1101 fprintf (dump_file, "Applying if-conversion\n");
1102
1103 return true;
1104 }
1105
1106 /* Return true when LOOP is if-convertible.
1107 LOOP is if-convertible if:
1108 - it is innermost,
1109 - it has two or more basic blocks,
1110 - it has only one exit,
1111 - loop header is not the exit edge,
1112 - if its basic blocks and phi nodes are if convertible. */
1113
1114 static bool
1115 if_convertible_loop_p (struct loop *loop)
1116 {
1117 edge e;
1118 edge_iterator ei;
1119 bool res = false;
1120 VEC (data_reference_p, heap) *refs;
1121 VEC (ddr_p, heap) *ddrs;
1122 VEC (loop_p, heap) *loop_nest;
1123
1124 /* Handle only innermost loop. */
1125 if (!loop || loop->inner)
1126 {
1127 if (dump_file && (dump_flags & TDF_DETAILS))
1128 fprintf (dump_file, "not innermost loop\n");
1129 return false;
1130 }
1131
1132 /* If only one block, no need for if-conversion. */
1133 if (loop->num_nodes <= 2)
1134 {
1135 if (dump_file && (dump_flags & TDF_DETAILS))
1136 fprintf (dump_file, "less than 2 basic blocks\n");
1137 return false;
1138 }
1139
1140 /* More than one loop exit is too much to handle. */
1141 if (!single_exit (loop))
1142 {
1143 if (dump_file && (dump_flags & TDF_DETAILS))
1144 fprintf (dump_file, "multiple exits\n");
1145 return false;
1146 }
1147
1148 /* If one of the loop header's edge is an exit edge then do not
1149 apply if-conversion. */
1150 FOR_EACH_EDGE (e, ei, loop->header->succs)
1151 if (loop_exit_edge_p (loop, e))
1152 return false;
1153
1154 refs = VEC_alloc (data_reference_p, heap, 5);
1155 ddrs = VEC_alloc (ddr_p, heap, 25);
1156 loop_nest = VEC_alloc (loop_p, heap, 3);
1157 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1158
1159 if (flag_tree_loop_if_convert_stores)
1160 {
1161 data_reference_p dr;
1162 unsigned int i;
1163
1164 for (i = 0; VEC_iterate (data_reference_p, refs, i, dr); i++)
1165 free (dr->aux);
1166 }
1167
1168 VEC_free (loop_p, heap, loop_nest);
1169 free_data_refs (refs);
1170 free_dependence_relations (ddrs);
1171 return res;
1172 }
1173
1174 /* Basic block BB has two predecessors. Using predecessor's bb
1175 predicate, set an appropriate condition COND for the PHI node
1176 replacement. Return the true block whose phi arguments are
1177 selected when cond is true. LOOP is the loop containing the
1178 if-converted region, GSI is the place to insert the code for the
1179 if-conversion. */
1180
1181 static basic_block
1182 find_phi_replacement_condition (struct loop *loop,
1183 basic_block bb, tree *cond,
1184 gimple_stmt_iterator *gsi)
1185 {
1186 edge first_edge, second_edge;
1187 tree tmp_cond;
1188
1189 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1190 first_edge = EDGE_PRED (bb, 0);
1191 second_edge = EDGE_PRED (bb, 1);
1192
1193 /* Use condition based on following criteria:
1194 1)
1195 S1: x = !c ? a : b;
1196
1197 S2: x = c ? b : a;
1198
1199 S2 is preferred over S1. Make 'b' first_bb and use its condition.
1200
1201 2) Do not make loop header first_bb.
1202
1203 3)
1204 S1: x = !(c == d)? a : b;
1205
1206 S21: t1 = c == d;
1207 S22: x = t1 ? b : a;
1208
1209 S3: x = (c == d) ? b : a;
1210
1211 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
1212 its condition.
1213
1214 4) If pred B is dominated by pred A then use pred B's condition.
1215 See PR23115. */
1216
1217 /* Select condition that is not TRUTH_NOT_EXPR. */
1218 tmp_cond = bb_predicate (first_edge->src);
1219 gcc_assert (tmp_cond);
1220
1221 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1222 {
1223 edge tmp_edge;
1224
1225 tmp_edge = first_edge;
1226 first_edge = second_edge;
1227 second_edge = tmp_edge;
1228 }
1229
1230 /* Check if FIRST_BB is loop header or not and make sure that
1231 FIRST_BB does not dominate SECOND_BB. */
1232 if (first_edge->src == loop->header
1233 || dominated_by_p (CDI_DOMINATORS,
1234 second_edge->src, first_edge->src))
1235 {
1236 *cond = bb_predicate (second_edge->src);
1237
1238 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1239 *cond = TREE_OPERAND (*cond, 0);
1240 else
1241 /* Select non loop header bb. */
1242 first_edge = second_edge;
1243 }
1244 else
1245 *cond = bb_predicate (first_edge->src);
1246
1247 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1248 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1249 is_gimple_condexpr, NULL_TREE,
1250 true, GSI_SAME_STMT);
1251
1252 return first_edge->src;
1253 }
1254
1255 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1256 This routine does not handle PHI nodes with more than two
1257 arguments.
1258
1259 For example,
1260 S1: A = PHI <x1(1), x2(5)>
1261 is converted into,
1262 S2: A = cond ? x1 : x2;
1263
1264 The generated code is inserted at GSI that points to the top of
1265 basic block's statement list. When COND is true, phi arg from
1266 TRUE_BB is selected. */
1267
1268 static void
1269 predicate_scalar_phi (gimple phi, tree cond,
1270 basic_block true_bb,
1271 gimple_stmt_iterator *gsi)
1272 {
1273 gimple new_stmt;
1274 basic_block bb;
1275 tree rhs, res, arg, scev;
1276
1277 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1278 && gimple_phi_num_args (phi) == 2);
1279
1280 res = gimple_phi_result (phi);
1281 /* Do not handle virtual phi nodes. */
1282 if (!is_gimple_reg (SSA_NAME_VAR (res)))
1283 return;
1284
1285 bb = gimple_bb (phi);
1286
1287 if ((arg = degenerate_phi_result (phi))
1288 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1289 res))
1290 && !chrec_contains_undetermined (scev)
1291 && scev != res
1292 && (arg = gimple_phi_arg_def (phi, 0))))
1293 rhs = arg;
1294 else
1295 {
1296 tree arg_0, arg_1;
1297 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1298 if (EDGE_PRED (bb, 1)->src == true_bb)
1299 {
1300 arg_0 = gimple_phi_arg_def (phi, 1);
1301 arg_1 = gimple_phi_arg_def (phi, 0);
1302 }
1303 else
1304 {
1305 arg_0 = gimple_phi_arg_def (phi, 0);
1306 arg_1 = gimple_phi_arg_def (phi, 1);
1307 }
1308
1309 gcc_checking_assert (bb == bb->loop_father->header
1310 || bb_postdominates_preds (bb));
1311
1312 /* Build new RHS using selected condition and arguments. */
1313 rhs = build3 (COND_EXPR, TREE_TYPE (res),
1314 unshare_expr (cond), arg_0, arg_1);
1315 }
1316
1317 new_stmt = gimple_build_assign (res, rhs);
1318 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
1319 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1320 update_stmt (new_stmt);
1321
1322 if (dump_file && (dump_flags & TDF_DETAILS))
1323 {
1324 fprintf (dump_file, "new phi replacement stmt\n");
1325 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1326 }
1327 }
1328
1329 /* Replaces in LOOP all the scalar phi nodes other than those in the
1330 LOOP->header block with conditional modify expressions. */
1331
1332 static void
1333 predicate_all_scalar_phis (struct loop *loop)
1334 {
1335 basic_block bb;
1336 unsigned int orig_loop_num_nodes = loop->num_nodes;
1337 unsigned int i;
1338
1339 for (i = 1; i < orig_loop_num_nodes; i++)
1340 {
1341 gimple phi;
1342 tree cond = NULL_TREE;
1343 gimple_stmt_iterator gsi, phi_gsi;
1344 basic_block true_bb = NULL;
1345 bb = ifc_bbs[i];
1346
1347 if (bb == loop->header)
1348 continue;
1349
1350 phi_gsi = gsi_start_phis (bb);
1351 if (gsi_end_p (phi_gsi))
1352 continue;
1353
1354 /* BB has two predecessors. Using predecessor's aux field, set
1355 appropriate condition for the PHI node replacement. */
1356 gsi = gsi_after_labels (bb);
1357 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
1358
1359 while (!gsi_end_p (phi_gsi))
1360 {
1361 phi = gsi_stmt (phi_gsi);
1362 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1363 release_phi_node (phi);
1364 gsi_next (&phi_gsi);
1365 }
1366
1367 set_phi_nodes (bb, NULL);
1368 }
1369 }
1370
1371 /* Insert in each basic block of LOOP the statements produced by the
1372 gimplification of the predicates. */
1373
1374 static void
1375 insert_gimplified_predicates (loop_p loop)
1376 {
1377 unsigned int i;
1378
1379 for (i = 0; i < loop->num_nodes; i++)
1380 {
1381 basic_block bb = ifc_bbs[i];
1382 gimple_seq stmts;
1383
1384 if (!is_predicated (bb))
1385 {
1386 /* Do not insert statements for a basic block that is not
1387 predicated. Also make sure that the predicate of the
1388 basic block is set to true. */
1389 reset_bb_predicate (bb);
1390 continue;
1391 }
1392
1393 stmts = bb_predicate_gimplified_stmts (bb);
1394 if (stmts)
1395 {
1396 if (flag_tree_loop_if_convert_stores)
1397 {
1398 /* Insert the predicate of the BB just after the label,
1399 as the if-conversion of memory writes will use this
1400 predicate. */
1401 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1402 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1403 }
1404 else
1405 {
1406 /* Insert the predicate of the BB at the end of the BB
1407 as this would reduce the register pressure: the only
1408 use of this predicate will be in successor BBs. */
1409 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1410
1411 if (gsi_end_p (gsi)
1412 || stmt_ends_bb_p (gsi_stmt (gsi)))
1413 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1414 else
1415 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1416 }
1417
1418 /* Once the sequence is code generated, set it to NULL. */
1419 set_bb_predicate_gimplified_stmts (bb, NULL);
1420 }
1421 }
1422 }
1423
1424 /* Predicate each write to memory in LOOP.
1425
1426 This function transforms control flow constructs containing memory
1427 writes of the form:
1428
1429 | for (i = 0; i < N; i++)
1430 | if (cond)
1431 | A[i] = expr;
1432
1433 into the following form that does not contain control flow:
1434
1435 | for (i = 0; i < N; i++)
1436 | A[i] = cond ? expr : A[i];
1437
1438 The original CFG looks like this:
1439
1440 | bb_0
1441 | i = 0
1442 | end_bb_0
1443 |
1444 | bb_1
1445 | if (i < N) goto bb_5 else goto bb_2
1446 | end_bb_1
1447 |
1448 | bb_2
1449 | cond = some_computation;
1450 | if (cond) goto bb_3 else goto bb_4
1451 | end_bb_2
1452 |
1453 | bb_3
1454 | A[i] = expr;
1455 | goto bb_4
1456 | end_bb_3
1457 |
1458 | bb_4
1459 | goto bb_1
1460 | end_bb_4
1461
1462 insert_gimplified_predicates inserts the computation of the COND
1463 expression at the beginning of the destination basic block:
1464
1465 | bb_0
1466 | i = 0
1467 | end_bb_0
1468 |
1469 | bb_1
1470 | if (i < N) goto bb_5 else goto bb_2
1471 | end_bb_1
1472 |
1473 | bb_2
1474 | cond = some_computation;
1475 | if (cond) goto bb_3 else goto bb_4
1476 | end_bb_2
1477 |
1478 | bb_3
1479 | cond = some_computation;
1480 | A[i] = expr;
1481 | goto bb_4
1482 | end_bb_3
1483 |
1484 | bb_4
1485 | goto bb_1
1486 | end_bb_4
1487
1488 predicate_mem_writes is then predicating the memory write as follows:
1489
1490 | bb_0
1491 | i = 0
1492 | end_bb_0
1493 |
1494 | bb_1
1495 | if (i < N) goto bb_5 else goto bb_2
1496 | end_bb_1
1497 |
1498 | bb_2
1499 | if (cond) goto bb_3 else goto bb_4
1500 | end_bb_2
1501 |
1502 | bb_3
1503 | cond = some_computation;
1504 | A[i] = cond ? expr : A[i];
1505 | goto bb_4
1506 | end_bb_3
1507 |
1508 | bb_4
1509 | goto bb_1
1510 | end_bb_4
1511
1512 and finally combine_blocks removes the basic block boundaries making
1513 the loop vectorizable:
1514
1515 | bb_0
1516 | i = 0
1517 | if (i < N) goto bb_5 else goto bb_1
1518 | end_bb_0
1519 |
1520 | bb_1
1521 | cond = some_computation;
1522 | A[i] = cond ? expr : A[i];
1523 | if (i < N) goto bb_5 else goto bb_4
1524 | end_bb_1
1525 |
1526 | bb_4
1527 | goto bb_1
1528 | end_bb_4
1529 */
1530
1531 static void
1532 predicate_mem_writes (loop_p loop)
1533 {
1534 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1535
1536 for (i = 1; i < orig_loop_num_nodes; i++)
1537 {
1538 gimple_stmt_iterator gsi;
1539 basic_block bb = ifc_bbs[i];
1540 tree cond = bb_predicate (bb);
1541 bool swap;
1542 gimple stmt;
1543
1544 if (is_true_predicate (cond))
1545 continue;
1546
1547 swap = false;
1548 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1549 {
1550 swap = true;
1551 cond = TREE_OPERAND (cond, 0);
1552 }
1553
1554 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1555 if ((stmt = gsi_stmt (gsi))
1556 && gimple_assign_single_p (stmt)
1557 && gimple_vdef (stmt))
1558 {
1559 tree lhs = gimple_assign_lhs (stmt);
1560 tree rhs = gimple_assign_rhs1 (stmt);
1561 tree type = TREE_TYPE (lhs);
1562
1563 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1564 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1565 if (swap)
1566 {
1567 tree tem = lhs;
1568 lhs = rhs;
1569 rhs = tem;
1570 }
1571 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1572 is_gimple_condexpr, NULL_TREE,
1573 true, GSI_SAME_STMT);
1574 rhs = build3 (COND_EXPR, type, unshare_expr (cond), rhs, lhs);
1575 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1576 update_stmt (stmt);
1577 }
1578 }
1579 }
1580
1581 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1582 other than the exit and latch of the LOOP. Also resets the
1583 GIMPLE_DEBUG information. */
1584
1585 static void
1586 remove_conditions_and_labels (loop_p loop)
1587 {
1588 gimple_stmt_iterator gsi;
1589 unsigned int i;
1590
1591 for (i = 0; i < loop->num_nodes; i++)
1592 {
1593 basic_block bb = ifc_bbs[i];
1594
1595 if (bb_with_exit_edge_p (loop, bb)
1596 || bb == loop->latch)
1597 continue;
1598
1599 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1600 switch (gimple_code (gsi_stmt (gsi)))
1601 {
1602 case GIMPLE_COND:
1603 case GIMPLE_LABEL:
1604 gsi_remove (&gsi, true);
1605 break;
1606
1607 case GIMPLE_DEBUG:
1608 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1609 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1610 {
1611 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1612 update_stmt (gsi_stmt (gsi));
1613 }
1614 gsi_next (&gsi);
1615 break;
1616
1617 default:
1618 gsi_next (&gsi);
1619 }
1620 }
1621 }
1622
1623 /* Combine all the basic blocks from LOOP into one or two super basic
1624 blocks. Replace PHI nodes with conditional modify expressions. */
1625
1626 static void
1627 combine_blocks (struct loop *loop)
1628 {
1629 basic_block bb, exit_bb, merge_target_bb;
1630 unsigned int orig_loop_num_nodes = loop->num_nodes;
1631 unsigned int i;
1632 edge e;
1633 edge_iterator ei;
1634
1635 remove_conditions_and_labels (loop);
1636 insert_gimplified_predicates (loop);
1637 predicate_all_scalar_phis (loop);
1638
1639 if (flag_tree_loop_if_convert_stores)
1640 predicate_mem_writes (loop);
1641
1642 /* Merge basic blocks: first remove all the edges in the loop,
1643 except for those from the exit block. */
1644 exit_bb = NULL;
1645 for (i = 0; i < orig_loop_num_nodes; i++)
1646 {
1647 bb = ifc_bbs[i];
1648 free_bb_predicate (bb);
1649 if (bb_with_exit_edge_p (loop, bb))
1650 {
1651 gcc_assert (exit_bb == NULL);
1652 exit_bb = bb;
1653 }
1654 }
1655 gcc_assert (exit_bb != loop->latch);
1656
1657 for (i = 1; i < orig_loop_num_nodes; i++)
1658 {
1659 bb = ifc_bbs[i];
1660
1661 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1662 {
1663 if (e->src == exit_bb)
1664 ei_next (&ei);
1665 else
1666 remove_edge (e);
1667 }
1668 }
1669
1670 if (exit_bb != NULL)
1671 {
1672 if (exit_bb != loop->header)
1673 {
1674 /* Connect this node to loop header. */
1675 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1676 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1677 }
1678
1679 /* Redirect non-exit edges to loop->latch. */
1680 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1681 {
1682 if (!loop_exit_edge_p (loop, e))
1683 redirect_edge_and_branch (e, loop->latch);
1684 }
1685 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1686 }
1687 else
1688 {
1689 /* If the loop does not have an exit, reconnect header and latch. */
1690 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1691 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1692 }
1693
1694 merge_target_bb = loop->header;
1695 for (i = 1; i < orig_loop_num_nodes; i++)
1696 {
1697 gimple_stmt_iterator gsi;
1698 gimple_stmt_iterator last;
1699
1700 bb = ifc_bbs[i];
1701
1702 if (bb == exit_bb || bb == loop->latch)
1703 continue;
1704
1705 /* Make stmts member of loop->header. */
1706 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1707 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1708
1709 /* Update stmt list. */
1710 last = gsi_last_bb (merge_target_bb);
1711 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1712 set_bb_seq (bb, NULL);
1713
1714 delete_basic_block (bb);
1715 }
1716
1717 /* If possible, merge loop header to the block with the exit edge.
1718 This reduces the number of basic blocks to two, to please the
1719 vectorizer that handles only loops with two nodes. */
1720 if (exit_bb
1721 && exit_bb != loop->header
1722 && can_merge_blocks_p (loop->header, exit_bb))
1723 merge_blocks (loop->header, exit_bb);
1724
1725 free (ifc_bbs);
1726 ifc_bbs = NULL;
1727
1728 /* Post-dominators are corrupt now. */
1729 free_dominance_info (CDI_POST_DOMINATORS);
1730 }
1731
1732 /* If-convert LOOP when it is legal. For the moment this pass has no
1733 profitability analysis. Returns true when something changed. */
1734
1735 static bool
1736 tree_if_conversion (struct loop *loop)
1737 {
1738 bool changed = false;
1739 ifc_bbs = NULL;
1740
1741 if (!if_convertible_loop_p (loop)
1742 || !dbg_cnt (if_conversion_tree))
1743 goto cleanup;
1744
1745 /* Now all statements are if-convertible. Combine all the basic
1746 blocks into one huge basic block doing the if-conversion
1747 on-the-fly. */
1748 combine_blocks (loop);
1749
1750 if (flag_tree_loop_if_convert_stores)
1751 mark_sym_for_renaming (gimple_vop (cfun));
1752
1753 changed = true;
1754
1755 cleanup:
1756 if (ifc_bbs)
1757 {
1758 unsigned int i;
1759
1760 for (i = 0; i < loop->num_nodes; i++)
1761 free_bb_predicate (ifc_bbs[i]);
1762
1763 free (ifc_bbs);
1764 ifc_bbs = NULL;
1765 }
1766
1767 return changed;
1768 }
1769
1770 /* Tree if-conversion pass management. */
1771
1772 static unsigned int
1773 main_tree_if_conversion (void)
1774 {
1775 loop_iterator li;
1776 struct loop *loop;
1777 bool changed = false;
1778 unsigned todo = 0;
1779
1780 if (number_of_loops () <= 1)
1781 return 0;
1782
1783 FOR_EACH_LOOP (li, loop, 0)
1784 changed |= tree_if_conversion (loop);
1785
1786 if (changed)
1787 todo |= TODO_cleanup_cfg;
1788
1789 if (changed && flag_tree_loop_if_convert_stores)
1790 todo |= TODO_update_ssa_only_virtuals;
1791
1792 free_dominance_info (CDI_POST_DOMINATORS);
1793
1794 #ifdef ENABLE_CHECKING
1795 {
1796 basic_block bb;
1797 FOR_EACH_BB (bb)
1798 gcc_assert (!bb->aux);
1799 }
1800 #endif
1801
1802 return todo;
1803 }
1804
1805 /* Returns true when the if-conversion pass is enabled. */
1806
1807 static bool
1808 gate_tree_if_conversion (void)
1809 {
1810 return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
1811 || flag_tree_loop_if_convert == 1
1812 || flag_tree_loop_if_convert_stores == 1);
1813 }
1814
1815 struct gimple_opt_pass pass_if_conversion =
1816 {
1817 {
1818 GIMPLE_PASS,
1819 "ifcvt", /* name */
1820 gate_tree_if_conversion, /* gate */
1821 main_tree_if_conversion, /* execute */
1822 NULL, /* sub */
1823 NULL, /* next */
1824 0, /* static_pass_number */
1825 TV_NONE, /* tv_id */
1826 PROP_cfg | PROP_ssa, /* properties_required */
1827 0, /* properties_provided */
1828 0, /* properties_destroyed */
1829 0, /* todo_flags_start */
1830 TODO_verify_stmts | TODO_verify_flow
1831 /* todo_flags_finish */
1832 }
1833 };