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
,
48 static bool empty_block_p (basic_block
);
50 /* This pass eliminates PHI nodes which can be trivially implemented as
51 an assignment from a conditional expression. ie if we have something
55 if (cond) goto bb2; else goto bb1;
58 x = PHI (0 (bb1), 1 (bb0)
60 We can rewrite that as:
67 bb1 will become unreachable and bb0 and bb2 will almost always
68 be merged into a single block. This occurs often due to gimplification
71 Also done is the following optimization:
74 if (a != b) goto bb2; else goto bb1;
77 x = PHI (a (bb1), b (bb0))
79 We can rewrite that as:
86 This can sometimes occur as a result of other optimizations. A
87 similar transformation is done by the ifcvt RTL optimizer.
89 This pass also eliminates PHI nodes which are really absolute
90 values. i.e. if we have something like:
93 if (a >= 0) goto bb2; else goto bb1;
97 x = PHI (x (bb1), a (bb0));
99 We can rewrite that as:
106 bb1 will become unreachable and bb0 and bb2 will almost always be merged
107 into a single block. Similar transformations are done by the ifcvt
111 tree_ssa_phiopt (void)
114 bool removed_phis
= false;
116 /* Search every basic block for PHI nodes we may be able to optimize. */
119 tree arg0
, arg1
, phi
;
121 /* We're searching for blocks with one PHI node which has two
123 phi
= phi_nodes (bb
);
124 if (phi
&& PHI_CHAIN (phi
) == NULL
125 && PHI_NUM_ARGS (phi
) == 2)
127 arg0
= PHI_ARG_DEF (phi
, 0);
128 arg1
= PHI_ARG_DEF (phi
, 1);
130 /* Do the replacement of conditional if it can be done. */
131 if (conditional_replacement (bb
, phi
, arg0
, arg1
)
132 || value_replacement (bb
, phi
, arg0
, arg1
)
133 || abs_replacement (bb
, phi
, arg0
, arg1
))
135 /* We have done the replacement so we need to rebuild the
136 cfg when this pass is complete. */
142 /* If we removed any PHIs, then we have unreachable blocks and blocks
143 which need to be merged in the CFG. */
148 /* Return TRUE if block BB has no executable statements, otherwise return
151 empty_block_p (basic_block bb
)
153 block_stmt_iterator bsi
;
155 /* BB must have no executable statements. */
156 bsi
= bsi_start (bb
);
157 while (!bsi_end_p (bsi
)
158 && (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
159 || IS_EMPTY_STMT (bsi_stmt (bsi
))))
162 if (!bsi_end_p (bsi
))
168 /* BB is a basic block which has only one PHI node with precisely two
171 Examine both of BB's predecessors to see if one ends with a
172 COND_EXPR and the other is a successor of the COND_EXPR. If so, then
173 we may be able to optimize PHI nodes at the start of BB.
175 If so, mark store the block with the COND_EXPR into COND_BLOCK_P
176 and the other block into OTHER_BLOCK_P and return true, otherwise
180 candidate_bb_for_phi_optimization (basic_block bb
,
181 basic_block
*cond_block_p
,
182 basic_block
*other_block_p
)
185 basic_block cond_block
, other_block
;
187 /* One of the alternatives must come from a block ending with
189 last0
= last_stmt (bb
->pred
->src
);
190 last1
= last_stmt (bb
->pred
->pred_next
->src
);
191 if (last0
&& TREE_CODE (last0
) == COND_EXPR
)
193 cond_block
= bb
->pred
->src
;
194 other_block
= bb
->pred
->pred_next
->src
;
196 else if (last1
&& TREE_CODE (last1
) == COND_EXPR
)
198 other_block
= bb
->pred
->src
;
199 cond_block
= bb
->pred
->pred_next
->src
;
204 /* COND_BLOCK must have precisely two successors. We indirectly
205 verify that those successors are BB and OTHER_BLOCK. */
206 if (!cond_block
->succ
207 || !cond_block
->succ
->succ_next
208 || cond_block
->succ
->succ_next
->succ_next
209 || (cond_block
->succ
->flags
& EDGE_ABNORMAL
) != 0
210 || (cond_block
->succ
->succ_next
->flags
& EDGE_ABNORMAL
) != 0)
213 /* OTHER_BLOCK must have a single predecessor which is COND_BLOCK,
214 OTHER_BLOCK must have a single successor which is BB and
215 OTHER_BLOCK must have no PHI nodes. */
216 if (!other_block
->pred
217 || other_block
->pred
->src
!= cond_block
218 || other_block
->pred
->pred_next
219 || !other_block
->succ
220 || other_block
->succ
->dest
!= bb
221 || other_block
->succ
->succ_next
222 || phi_nodes (other_block
))
225 *cond_block_p
= cond_block
;
226 *other_block_p
= other_block
;
227 /* Everything looks OK. */
231 /* Replace PHI in block BB with statement NEW. NEW is inserted after
232 BSI. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
233 is known to have two edges, one of which must reach BB). */
236 replace_phi_with_stmt (block_stmt_iterator bsi
, basic_block bb
,
237 basic_block cond_block
, tree phi
, tree
new)
239 /* Insert our new statement at the head of our block. */
240 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
242 /* Register our new statement as the defining statement for
244 SSA_NAME_DEF_STMT (PHI_RESULT (phi
)) = new;
246 /* Remove the now useless PHI node.
248 We do not want to use remove_phi_node since that releases the
249 SSA_NAME as well and the SSA_NAME is still being used. */
250 release_phi_node (phi
);
251 bb_ann (bb
)->phi_nodes
= NULL
;
253 /* Disconnect the edge leading into the empty block. That will
254 make the empty block unreachable and it will be removed later. */
255 if (cond_block
->succ
->dest
== bb
)
257 cond_block
->succ
->flags
|= EDGE_FALLTHRU
;
258 cond_block
->succ
->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
259 ssa_remove_edge (cond_block
->succ
->succ_next
);
263 cond_block
->succ
->succ_next
->flags
|= EDGE_FALLTHRU
;
264 cond_block
->succ
->succ_next
->flags
265 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
266 ssa_remove_edge (cond_block
->succ
);
269 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
270 bsi
= bsi_last (cond_block
);
273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
275 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
280 /* The function conditional_replacement does the main work of doing the
281 conditional replacement. Return true if the replacement is done.
282 Otherwise return false.
283 BB is the basic block where the replacement is going to be done on. ARG0
284 is argument 0 from PHI. Likewise for ARG1. */
287 conditional_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
290 tree old_result
= NULL
;
291 basic_block other_block
= NULL
;
292 basic_block cond_block
= NULL
;
294 block_stmt_iterator bsi
;
295 edge true_edge
, false_edge
;
298 /* The PHI arguments have the constants 0 and 1, then convert
299 it to the conditional. */
300 if ((integer_zerop (arg0
) && integer_onep (arg1
))
301 || (integer_zerop (arg1
) && integer_onep (arg0
)))
306 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
)
307 || !empty_block_p (other_block
))
310 /* If the condition is not a naked SSA_NAME and its type does not
311 match the type of the result, then we have to create a new
312 variable to optimize this case as it would likely create
313 non-gimple code when the condition was converted to the
315 cond
= COND_EXPR_COND (last_stmt (cond_block
));
316 result
= PHI_RESULT (phi
);
317 if (TREE_CODE (cond
) != SSA_NAME
318 && !lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
320 new_var
= make_rename_temp (TREE_TYPE (cond
), NULL
);
325 /* If the condition was a naked SSA_NAME and the type is not the
326 same as the type of the result, then convert the type of the
328 if (!lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
329 cond
= fold_convert (TREE_TYPE (result
), cond
);
331 /* We need to know which is the true edge and which is the false
332 edge so that we know when to invert the condition below. */
333 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
335 /* Insert our new statement at the head of our block. */
336 bsi
= bsi_start (bb
);
341 if (TREE_CODE_CLASS (TREE_CODE (old_result
)) != '<')
344 new1
= build (TREE_CODE (old_result
), TREE_TYPE (result
),
345 TREE_OPERAND (old_result
, 0),
346 TREE_OPERAND (old_result
, 1));
348 new1
= build (MODIFY_EXPR
, TREE_TYPE (result
), new_var
, new1
);
349 bsi_insert_after (&bsi
, new1
, BSI_NEW_STMT
);
352 /* At this point we know we have a COND_EXPR with two successors.
353 One successor is BB, the other successor is an empty block which
354 falls through into BB.
356 There is a single PHI node at the join point (BB) and its arguments
357 are constants (0, 1).
359 So, given the condition COND, and the two PHI arguments, we can
360 rewrite this PHI into non-branching code:
362 dest = (COND) or dest = COND'
364 We use the condition as-is if the argument associated with the
365 true edge has the value one or the argument associated with the
366 false edge as the value zero. Note that those conditions are not
367 the same since only one of the outgoing edges from the COND_EXPR
368 will directly reach BB and thus be associated with an argument. */
369 if ((PHI_ARG_EDGE (phi
, 0) == true_edge
&& integer_onep (arg0
))
370 || (PHI_ARG_EDGE (phi
, 0) == false_edge
&& integer_zerop (arg0
))
371 || (PHI_ARG_EDGE (phi
, 1) == true_edge
&& integer_onep (arg1
))
372 || (PHI_ARG_EDGE (phi
, 1) == false_edge
&& integer_zerop (arg1
)))
374 new = build (MODIFY_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
375 PHI_RESULT (phi
), cond
);
379 tree cond1
= invert_truthvalue (cond
);
382 /* If what we get back is a conditional expression, there is no
383 way that it can be gimple. */
384 if (TREE_CODE (cond
) == COND_EXPR
)
387 /* If what we get back is not gimple try to create it as gimple by
388 using a temporary variable. */
389 if (is_gimple_cast (cond
)
390 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
392 tree temp
= TREE_OPERAND (cond
, 0);
393 tree new_var_1
= make_rename_temp (TREE_TYPE (temp
), NULL
);
394 new = build (MODIFY_EXPR
, TREE_TYPE (new_var_1
), new_var_1
, temp
);
395 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
396 cond
= fold_convert (TREE_TYPE (result
), new_var_1
);
399 if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
400 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
403 new = build (MODIFY_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
404 PHI_RESULT (phi
), cond
);
407 replace_phi_with_stmt (bsi
, bb
, cond_block
, phi
, new);
409 /* Note that we optimized this PHI. */
413 /* The function value_replacement does the main work of doing the value
414 replacement. Return true if the replacement is done. Otherwise return
416 BB is the basic block where the replacement is going to be done on. ARG0
417 is argument 0 from the PHI. Likewise for ARG1. */
420 value_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
423 basic_block other_block
= NULL
;
424 basic_block cond_block
= NULL
;
426 edge true_edge
, false_edge
;
428 /* If the type says honor signed zeros we cannot do this
430 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
433 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
)
434 || !empty_block_p (other_block
))
437 cond
= COND_EXPR_COND (last_stmt (cond_block
));
438 result
= PHI_RESULT (phi
);
440 /* This transformation is only valid for equality comparisons. */
441 if (TREE_CODE (cond
) != NE_EXPR
&& TREE_CODE (cond
) != EQ_EXPR
)
444 /* We need to know which is the true edge and which is the false
445 edge so that we know if have abs or negative abs. */
446 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
448 /* At this point we know we have a COND_EXPR with two successors.
449 One successor is BB, the other successor is an empty block which
450 falls through into BB.
452 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
454 There is a single PHI node at the join point (BB) with two arguments.
456 We now need to verify that the two arguments in the PHI node match
457 the two arguments to the equality comparison. */
459 if ((operand_equal_p (arg0
, TREE_OPERAND (cond
, 0), 0)
460 && operand_equal_p (arg1
, TREE_OPERAND (cond
, 1), 0))
461 || (operand_equal_p (arg1
, TREE_OPERAND (cond
, 0), 0)
462 && operand_equal_p (arg0
, TREE_OPERAND (cond
, 1), 0)))
467 /* For NE_EXPR, we want to build an assignment result = arg where
468 arg is the PHI argument associated with the true edge. For
469 EQ_EXPR we want the PHI argument associated with the false edge. */
470 e
= (TREE_CODE (cond
) == NE_EXPR
? true_edge
: false_edge
);
472 /* Unfortunately, E may not reach BB (it may instead have gone to
473 OTHER_BLOCK). If that is the case, then we want the single outgoing
474 edge from OTHER_BLOCK which reaches BB and represents the desired
475 path from COND_BLOCK. */
476 if (e
->dest
== other_block
)
479 /* Now we know the incoming edge to BB that has the argument for the
480 RHS of our new assignment statement. */
481 if (PHI_ARG_EDGE (phi
, 0) == e
)
486 /* Build the new assignment. */
487 new = build (MODIFY_EXPR
, TREE_TYPE (result
), result
, arg
);
489 replace_phi_with_stmt (bsi_start (bb
), bb
, cond_block
, phi
, new);
491 /* Note that we optimized this PHI. */
497 /* The function absolute_replacement does the main work of doing the absolute
498 replacement. Return true if the replacement is done. Otherwise return
500 bb is the basic block where the replacement is going to be done on. arg0
501 is argument 0 from the phi. Likewise for arg1. */
503 abs_replacement (basic_block bb
, tree phi
, tree arg0
, tree arg1
)
506 basic_block other_block
= NULL
;
507 basic_block cond_block
= NULL
;
509 block_stmt_iterator bsi
;
510 edge true_edge
, false_edge
;
513 tree rhs
= NULL
, lhs
= NULL
;
515 enum tree_code cond_code
;
517 /* If the type says honor signed zeros we cannot do this
519 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
522 if (!candidate_bb_for_phi_optimization (bb
, &cond_block
, &other_block
))
525 /* OTHER_BLOCK must have only one executable statement which must have the
526 form arg0 = -arg1 or arg1 = -arg0. */
527 bsi
= bsi_start (other_block
);
528 while (!bsi_end_p (bsi
))
530 tree stmt
= bsi_stmt (bsi
);
532 /* Empty statements and labels are uninteresting. */
533 if (TREE_CODE (stmt
) == LABEL_EXPR
534 || IS_EMPTY_STMT (stmt
))
540 /* If we found the assignment, but it was not the only executable
541 statement in OTHER_BLOCK, then we can not optimize. */
545 /* If we got here, then we have found the first executable statement
546 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
547 arg1 = -arg0, then we can not optimize. */
548 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
550 lhs
= TREE_OPERAND (stmt
, 0);
551 rhs
= TREE_OPERAND (stmt
, 1);
553 if (TREE_CODE (rhs
) == NEGATE_EXPR
)
555 rhs
= TREE_OPERAND (rhs
, 0);
557 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
558 if ((lhs
== arg0
&& rhs
== arg1
)
559 || (lhs
== arg1
&& rhs
== arg0
))
574 /* If we did not find the proper negation assignment, then we can not
579 cond
= COND_EXPR_COND (last_stmt (cond_block
));
580 result
= PHI_RESULT (phi
);
582 /* Only relationals comparing arg[01] against zero are interesting. */
583 cond_code
= TREE_CODE (cond
);
584 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
585 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
588 /* Make sure the conditional is arg[01] OP y. */
589 if (TREE_OPERAND (cond
, 0) != rhs
)
592 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))
593 ? real_zerop (TREE_OPERAND (cond
, 1))
594 : integer_zerop (TREE_OPERAND (cond
, 1)))
599 /* We need to know which is the true edge and which is the false
600 edge so that we know if have abs or negative abs. */
601 extract_true_false_edges_from_block (cond_block
, &true_edge
, &false_edge
);
603 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
604 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
605 the false edge goes to OTHER_BLOCK. */
606 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
611 if (e
->dest
== other_block
)
617 lhs
= make_rename_temp (TREE_TYPE (result
), NULL
);
621 /* Build the modify expression with abs expression. */
622 new = build (MODIFY_EXPR
, TREE_TYPE (lhs
),
623 lhs
, build1 (ABS_EXPR
, TREE_TYPE (lhs
), rhs
));
625 replace_phi_with_stmt (bsi_start (bb
), bb
, cond_block
, phi
, new);
630 /* Get the right BSI. We want to insert after the recently
631 added ABS_EXPR statement (which we know is the first statement
633 bsi
= bsi_start (bb
);
635 new = build (MODIFY_EXPR
, TREE_TYPE (result
),
636 result
, build1 (NEGATE_EXPR
, TREE_TYPE (lhs
), lhs
));
638 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
640 /* Register the new statement as defining the temporary -- this is
641 normally done by replace_phi_with_stmt, but the link will be wrong
642 if we had to negate the resulting value. */
643 SSA_NAME_DEF_STMT (result
) = new;
646 /* Note that we optimized this PHI. */
651 /* Always do these optimizations if we have SSA
659 struct tree_opt_pass pass_phiopt
=
662 gate_phiopt
, /* gate */
663 tree_ssa_phiopt
, /* execute */
666 0, /* static_pass_number */
667 TV_TREE_PHIOPT
, /* tv_id */
668 PROP_cfg
| PROP_ssa
, /* properties_required */
669 0, /* properties_provided */
670 0, /* properties_destroyed */
671 0, /* todo_flags_start */
672 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */
673 | TODO_verify_ssa
| TODO_rename_vars