1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
31 #include "basic-block.h"
33 #include "diagnostic.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "tree-dump.h"
37 #include "langhooks.h"
39 static void tree_ssa_phiopt (void);
40 static bool conditional_replacement (basic_block
, tree
, tree
, tree
);
41 static bool value_replacement (basic_block
, tree
, tree
, tree
);
42 static bool abs_replacement (basic_block
, tree
, tree
, tree
);
43 static void replace_phi_with_stmt (block_stmt_iterator
, basic_block
,
44 basic_block
, tree
, tree
);
45 static bool candidate_bb_for_phi_optimization (basic_block
,
49 /* This pass eliminates PHI nodes which can be trivially implemented as
50 an assignment from a conditional expression. i.e. if we have something
54 if (cond) goto bb2; else goto bb1;
57 x = PHI (0 (bb1), 1 (bb0)
59 We can rewrite that as:
66 bb1 will become unreachable and bb0 and bb2 will almost always
67 be merged into a single block. This occurs often due to gimplification
70 Also done is the following optimization:
73 if (a != b) goto bb2; else goto bb1;
76 x = PHI (a (bb1), b (bb0))
78 We can rewrite that as:
85 This can sometimes occur as a result of other optimizations. A
86 similar transformation is done by the ifcvt RTL optimizer.
88 This pass also eliminates PHI nodes which are really absolute
89 values. i.e. if we have something like:
92 if (a >= 0) goto bb2; else goto bb1;
96 x = PHI (x (bb1), a (bb0));
98 We can rewrite that as:
105 bb1 will become unreachable and bb0 and bb2 will almost always be merged
106 into a single block. Similar transformations are done by the ifcvt
110 tree_ssa_phiopt (void)
113 bool removed_phis
= false;
115 /* Search every basic block for PHI nodes we may be able to optimize. */
118 tree arg0
, arg1
, phi
;
120 /* We're searching for blocks with one PHI node which has two
122 phi
= phi_nodes (bb
);
123 if (phi
&& PHI_CHAIN (phi
) == NULL
124 && PHI_NUM_ARGS (phi
) == 2)
126 arg0
= PHI_ARG_DEF (phi
, 0);
127 arg1
= PHI_ARG_DEF (phi
, 1);
129 /* Do the replacement of conditional if it can be done. */
130 if (conditional_replacement (bb
, phi
, arg0
, arg1
)
131 || value_replacement (bb
, phi
, arg0
, arg1
)
132 || abs_replacement (bb
, phi
, arg0
, arg1
))
134 /* We have done the replacement so we need to rebuild the
135 cfg when this pass is complete. */
142 /* Return TRUE if block BB has no executable statements, otherwise return
145 empty_block_p (basic_block bb
)
147 block_stmt_iterator bsi
;
149 /* BB must have no executable statements. */
150 bsi
= bsi_start (bb
);
151 while (!bsi_end_p (bsi
)
152 && (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
153 || IS_EMPTY_STMT (bsi_stmt (bsi
))))
156 if (!bsi_end_p (bsi
))
162 /* BB is a basic block which has only one PHI node with precisely two
165 Examine both of BB's predecessors to see if one ends with a
166 COND_EXPR and the other is a successor of the COND_EXPR. If so, then
167 we may be able to optimize PHI nodes at the start of BB.
169 If so, mark store the block with the COND_EXPR into COND_BLOCK_P
170 and the other block into OTHER_BLOCK_P and return true, otherwise
174 candidate_bb_for_phi_optimization (basic_block bb
,
175 basic_block
*cond_block_p
,
176 basic_block
*other_block_p
)
179 basic_block cond_block
, other_block
;
181 /* One of the alternatives must come from a block ending with
183 last0
= last_stmt (EDGE_PRED (bb
, 0)->src
);
184 last1
= last_stmt (EDGE_PRED (bb
, 1)->src
);
185 if (last0
&& TREE_CODE (last0
) == COND_EXPR
)
187 cond_block
= EDGE_PRED (bb
, 0)->src
;
188 other_block
= EDGE_PRED (bb
, 1)->src
;
190 else if (last1
&& TREE_CODE (last1
) == COND_EXPR
)
192 other_block
= EDGE_PRED (bb
, 0)->src
;
193 cond_block
= EDGE_PRED (bb
, 1)->src
;
198 /* COND_BLOCK must have precisely two successors. We indirectly
199 verify that those successors are BB and OTHER_BLOCK. */
200 if (EDGE_COUNT (cond_block
->succs
) != 2
201 || (EDGE_SUCC (cond_block
, 0)->flags
& EDGE_ABNORMAL
) != 0
202 || (EDGE_SUCC (cond_block
, 1)->flags
& EDGE_ABNORMAL
) != 0)
205 /* OTHER_BLOCK must have a single predecessor which is COND_BLOCK,
206 OTHER_BLOCK must have a single successor which is BB and
207 OTHER_BLOCK must have no PHI nodes. */
208 if (EDGE_COUNT (other_block
->preds
) != 1
209 || EDGE_PRED (other_block
, 0)->src
!= cond_block
210 || EDGE_COUNT (other_block
->succs
) != 1
211 || EDGE_SUCC (other_block
, 0)->dest
!= bb
212 || phi_nodes (other_block
))
215 *cond_block_p
= cond_block
;
216 *other_block_p
= other_block
;
217 /* Everything looks OK. */
221 /* Replace PHI in block BB with statement NEW. NEW is inserted after
222 BSI. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
223 is known to have two edges, one of which must reach BB). */
226 replace_phi_with_stmt (block_stmt_iterator bsi
, basic_block bb
,
227 basic_block cond_block
, tree phi
, tree
new)
229 basic_block block_to_remove
;
231 /* Insert our new statement at the head of our block. */
232 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
234 /* Register our new statement as the defining statement for
236 SSA_NAME_DEF_STMT (PHI_RESULT (phi
)) = new;
238 /* Remove the now useless PHI node.
240 We do not want to use remove_phi_node since that releases the
241 SSA_NAME as well and the SSA_NAME is still being used. */
242 release_phi_node (phi
);
243 bb_ann (bb
)->phi_nodes
= NULL
;
245 /* Remove the empty basic block. */
246 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
248 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
249 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
251 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
255 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
256 EDGE_SUCC (cond_block
, 1)->flags
257 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
259 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
261 delete_basic_block (block_to_remove
);
263 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
264 bsi
= bsi_last (cond_block
);
267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
269 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
274 /* The function conditional_replacement does the main work of doing the
275 conditional replacement. Return true if the replacement is done.
276 Otherwise return false.
277 BB is the basic block where the replacement is going to be done on. ARG0
278 is argument 0 from PHI. Likewise for ARG1. */
281 conditional_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
284 tree old_result
= NULL
;
285 basic_block other_block
= NULL
;
286 basic_block cond_block
= NULL
;
288 block_stmt_iterator bsi
;
289 edge true_edge
, false_edge
;
292 /* The PHI arguments have the constants 0 and 1, then convert
293 it to the conditional. */
294 if ((integer_zerop (arg0
) && integer_onep (arg1
))
295 || (integer_zerop (arg1
) && integer_onep (arg0
)))
300 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
)
301 || !empty_block_p (other_block
))
304 /* If the condition is not a naked SSA_NAME and its type does not
305 match the type of the result, then we have to create a new
306 variable to optimize this case as it would likely create
307 non-gimple code when the condition was converted to the
309 cond
= COND_EXPR_COND (last_stmt (cond_block
));
310 result
= PHI_RESULT (phi
);
311 if (TREE_CODE (cond
) != SSA_NAME
312 && !lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
314 new_var
= make_rename_temp (TREE_TYPE (cond
), NULL
);
319 /* If the condition was a naked SSA_NAME and the type is not the
320 same as the type of the result, then convert the type of the
322 if (!lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
323 cond
= fold_convert (TREE_TYPE (result
), cond
);
325 /* We need to know which is the true edge and which is the false
326 edge so that we know when to invert the condition below. */
327 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
329 /* Insert our new statement at the head of our block. */
330 bsi
= bsi_after_labels (bb
);
335 if (!COMPARISON_CLASS_P (old_result
))
338 new1
= build (TREE_CODE (old_result
), TREE_TYPE (old_result
),
339 TREE_OPERAND (old_result
, 0),
340 TREE_OPERAND (old_result
, 1));
342 new1
= build (MODIFY_EXPR
, TREE_TYPE (old_result
), new_var
, new1
);
343 bsi_insert_after (&bsi
, new1
, BSI_NEW_STMT
);
346 /* At this point we know we have a COND_EXPR with two successors.
347 One successor is BB, the other successor is an empty block which
348 falls through into BB.
350 There is a single PHI node at the join point (BB) and its arguments
351 are constants (0, 1).
353 So, given the condition COND, and the two PHI arguments, we can
354 rewrite this PHI into non-branching code:
356 dest = (COND) or dest = COND'
358 We use the condition as-is if the argument associated with the
359 true edge has the value one or the argument associated with the
360 false edge as the value zero. Note that those conditions are not
361 the same since only one of the outgoing edges from the COND_EXPR
362 will directly reach BB and thus be associated with an argument. */
363 if ((PHI_ARG_EDGE (phi
, 0) == true_edge
&& integer_onep (arg0
))
364 || (PHI_ARG_EDGE (phi
, 0) == false_edge
&& integer_zerop (arg0
))
365 || (PHI_ARG_EDGE (phi
, 1) == true_edge
&& integer_onep (arg1
))
366 || (PHI_ARG_EDGE (phi
, 1) == false_edge
&& integer_zerop (arg1
)))
368 new = build (MODIFY_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
369 PHI_RESULT (phi
), cond
);
373 tree cond1
= invert_truthvalue (cond
);
376 /* If what we get back is a conditional expression, there is no
377 way that it can be gimple. */
378 if (TREE_CODE (cond
) == COND_EXPR
)
381 /* If what we get back is not gimple try to create it as gimple by
382 using a temporary variable. */
383 if (is_gimple_cast (cond
)
384 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
386 tree temp
= TREE_OPERAND (cond
, 0);
387 tree new_var_1
= make_rename_temp (TREE_TYPE (temp
), NULL
);
388 new = build (MODIFY_EXPR
, TREE_TYPE (new_var_1
), new_var_1
, temp
);
389 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
390 cond
= fold_convert (TREE_TYPE (result
), new_var_1
);
393 if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
394 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
397 new = build (MODIFY_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
398 PHI_RESULT (phi
), cond
);
401 replace_phi_with_stmt (bsi
, bb
, cond_block
, phi
, new);
403 /* Note that we optimized this PHI. */
407 /* The function value_replacement does the main work of doing the value
408 replacement. Return true if the replacement is done. Otherwise return
410 BB is the basic block where the replacement is going to be done on. ARG0
411 is argument 0 from the PHI. Likewise for ARG1. */
414 value_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
417 basic_block other_block
= NULL
;
418 basic_block cond_block
= NULL
;
420 edge true_edge
, false_edge
;
422 /* If the type says honor signed zeros we cannot do this
424 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
427 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
)
428 || !empty_block_p (other_block
))
431 cond
= COND_EXPR_COND (last_stmt (cond_block
));
432 result
= PHI_RESULT (phi
);
434 /* This transformation is only valid for equality comparisons. */
435 if (TREE_CODE (cond
) != NE_EXPR
&& TREE_CODE (cond
) != EQ_EXPR
)
438 /* We need to know which is the true edge and which is the false
439 edge so that we know if have abs or negative abs. */
440 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
442 /* At this point we know we have a COND_EXPR with two successors.
443 One successor is BB, the other successor is an empty block which
444 falls through into BB.
446 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
448 There is a single PHI node at the join point (BB) with two arguments.
450 We now need to verify that the two arguments in the PHI node match
451 the two arguments to the equality comparison. */
453 if ((operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 0))
454 && operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 1)))
455 || (operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 0))
456 && operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 1))))
461 /* For NE_EXPR, we want to build an assignment result = arg where
462 arg is the PHI argument associated with the true edge. For
463 EQ_EXPR we want the PHI argument associated with the false edge. */
464 e
= (TREE_CODE (cond
) == NE_EXPR
? true_edge
: false_edge
);
466 /* Unfortunately, E may not reach BB (it may instead have gone to
467 OTHER_BLOCK). If that is the case, then we want the single outgoing
468 edge from OTHER_BLOCK which reaches BB and represents the desired
469 path from COND_BLOCK. */
470 if (e
->dest
== other_block
)
471 e
= EDGE_SUCC (e
->dest
, 0);
473 /* Now we know the incoming edge to BB that has the argument for the
474 RHS of our new assignment statement. */
475 if (PHI_ARG_EDGE (phi
, 0) == e
)
480 /* Build the new assignment. */
481 new = build (MODIFY_EXPR
, TREE_TYPE (result
), result
, arg
);
483 replace_phi_with_stmt (bsi_after_labels (bb
), bb
, cond_block
, phi
, new);
485 /* Note that we optimized this PHI. */
491 /* The function absolute_replacement does the main work of doing the absolute
492 replacement. Return true if the replacement is done. Otherwise return
494 bb is the basic block where the replacement is going to be done on. arg0
495 is argument 0 from the phi. Likewise for arg1. */
497 abs_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
500 basic_block other_block
= NULL
;
501 basic_block cond_block
= NULL
;
503 block_stmt_iterator bsi
;
504 edge true_edge
, false_edge
;
507 tree rhs
= NULL
, lhs
= NULL
;
509 enum tree_code cond_code
;
511 /* If the type says honor signed zeros we cannot do this
513 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
516 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
))
519 /* OTHER_BLOCK must have only one executable statement which must have the
520 form arg0 = -arg1 or arg1 = -arg0. */
521 bsi
= bsi_start (other_block
);
522 while (!bsi_end_p (bsi
))
524 tree stmt
= bsi_stmt (bsi
);
526 /* Empty statements and labels are uninteresting. */
527 if (TREE_CODE (stmt
) == LABEL_EXPR
528 || IS_EMPTY_STMT (stmt
))
534 /* If we found the assignment, but it was not the only executable
535 statement in OTHER_BLOCK, then we can not optimize. */
539 /* If we got here, then we have found the first executable statement
540 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
541 arg1 = -arg0, then we can not optimize. */
542 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
544 lhs
= TREE_OPERAND (stmt
, 0);
545 rhs
= TREE_OPERAND (stmt
, 1);
547 if (TREE_CODE (rhs
) == NEGATE_EXPR
)
549 rhs
= TREE_OPERAND (rhs
, 0);
551 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
552 if ((lhs
== arg0
&& rhs
== arg1
)
553 || (lhs
== arg1
&& rhs
== arg0
))
568 /* If we did not find the proper negation assignment, then we can not
573 cond
= COND_EXPR_COND (last_stmt (cond_block
));
574 result
= PHI_RESULT (phi
);
576 /* Only relationals comparing arg[01] against zero are interesting. */
577 cond_code
= TREE_CODE (cond
);
578 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
579 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
582 /* Make sure the conditional is arg[01] OP y. */
583 if (TREE_OPERAND (cond
, 0) != rhs
)
586 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))
587 ? real_zerop (TREE_OPERAND (cond
, 1))
588 : integer_zerop (TREE_OPERAND (cond
, 1)))
593 /* We need to know which is the true edge and which is the false
594 edge so that we know if have abs or negative abs. */
595 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
597 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
598 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
599 the false edge goes to OTHER_BLOCK. */
600 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
605 if (e
->dest
== other_block
)
611 lhs
= make_rename_temp (TREE_TYPE (result
), NULL
);
615 /* Build the modify expression with abs expression. */
616 new = build (MODIFY_EXPR
, TREE_TYPE (lhs
),
617 lhs
, build1 (ABS_EXPR
, TREE_TYPE (lhs
), rhs
));
619 replace_phi_with_stmt (bsi_after_labels (bb
), bb
, cond_block
, phi
, new);
624 /* Get the right BSI. We want to insert after the recently
625 added ABS_EXPR statement (which we know is the first statement
627 bsi
= bsi_start (bb
);
629 new = build (MODIFY_EXPR
, TREE_TYPE (result
),
630 result
, build1 (NEGATE_EXPR
, TREE_TYPE (lhs
), lhs
));
632 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
634 /* Register the new statement as defining the temporary -- this is
635 normally done by replace_phi_with_stmt, but the link will be wrong
636 if we had to negate the resulting value. */
637 SSA_NAME_DEF_STMT (result
) = new;
640 /* Note that we optimized this PHI. */
645 /* Always do these optimizations if we have SSA
653 struct tree_opt_pass pass_phiopt
=
656 gate_phiopt
, /* gate */
657 tree_ssa_phiopt
, /* execute */
660 0, /* static_pass_number */
661 TV_TREE_PHIOPT
, /* tv_id */
662 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
663 0, /* properties_provided */
664 0, /* properties_destroyed */
665 0, /* todo_flags_start */
666 TODO_cleanup_cfg
| TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */
667 | TODO_verify_ssa
| TODO_rename_vars