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