1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
34 #include "tree-dump.h"
35 #include "langhooks.h"
37 /* This pass propagates the RHS of assignment statements into use
38 sites of the LHS of the assignment. It's basically a specialized
39 form of tree combination.
41 Note carefully that after propagation the resulting statement
42 must still be a proper gimple statement. Right now we simply
43 only perform propagations we know will result in valid gimple
44 code. One day we'll want to generalize this code.
46 One class of common cases we handle is forward propagating a single use
47 variable into a COND_EXPR.
51 if (x) goto ... else goto ...
53 Will be transformed into:
56 if (a COND b) goto ... else goto ...
58 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
60 Or (assuming c1 and c2 are constants):
64 if (x EQ/NEQ c2) goto ... else goto ...
66 Will be transformed into:
69 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
71 Similarly for x = a - c1.
77 if (x) goto ... else goto ...
79 Will be transformed into:
82 if (a == 0) goto ... else goto ...
84 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
85 For these cases, we propagate A into all, possibly more than one,
86 COND_EXPRs that use X.
92 if (x) goto ... else goto ...
94 Will be transformed into:
97 if (a != 0) goto ... else goto ...
99 (Assuming a is an integral type and x is a boolean or x is an
100 integral and a is a boolean.)
102 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
103 For these cases, we propagate A into all, possibly more than one,
104 COND_EXPRs that use X.
106 In addition to eliminating the variable and the statement which assigns
107 a value to the variable, we may be able to later thread the jump without
108 adding insane complexity in the dominator optimizer.
110 Also note these transformations can cascade. We handle this by having
111 a worklist of COND_EXPR statements to examine. As we make a change to
112 a statement, we put it back on the worklist to examine on the next
113 iteration of the main loop.
115 A second class of propagation opportunities arises for ADDR_EXPR
128 ptr2 = ptr + <constant>;
132 ptr2 = &x[constant/elementsize];
137 offset = index * element_size;
138 offset_p = (pointer) offset;
139 ptr2 = ptr + offset_p
141 Will get turned into:
146 This will (of course) be extended as other needs arise. */
149 /* Set to true if we delete EH edges during the optimization. */
150 static bool cfg_changed
;
153 /* Given an SSA_NAME VAR, return true if and only if VAR is defined by
157 ssa_name_defined_by_comparison_p (tree var
)
159 tree def
= SSA_NAME_DEF_STMT (var
);
161 if (TREE_CODE (def
) == MODIFY_EXPR
)
163 tree rhs
= TREE_OPERAND (def
, 1);
164 return COMPARISON_CLASS_P (rhs
);
170 /* Forward propagate a single-use variable into COND once. Return a
171 new condition if successful. Return NULL_TREE otherwise. */
174 forward_propagate_into_cond_1 (tree cond
, tree
*test_var_p
)
176 tree new_cond
= NULL_TREE
;
177 enum tree_code cond_code
= TREE_CODE (cond
);
178 tree test_var
= NULL_TREE
;
182 /* If the condition is not a lone variable or an equality test of an
183 SSA_NAME against an integral constant, then we do not have an
186 Note these conditions also ensure the COND_EXPR has no
187 virtual operands or other side effects. */
188 if (cond_code
!= SSA_NAME
189 && !((cond_code
== EQ_EXPR
|| cond_code
== NE_EXPR
)
190 && TREE_CODE (TREE_OPERAND (cond
, 0)) == SSA_NAME
191 && CONSTANT_CLASS_P (TREE_OPERAND (cond
, 1))
192 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))))
195 /* Extract the single variable used in the test into TEST_VAR. */
196 if (cond_code
== SSA_NAME
)
199 test_var
= TREE_OPERAND (cond
, 0);
201 /* Now get the defining statement for TEST_VAR. Skip this case if
202 it's not defined by some MODIFY_EXPR. */
203 def
= SSA_NAME_DEF_STMT (test_var
);
204 if (TREE_CODE (def
) != MODIFY_EXPR
)
207 def_rhs
= TREE_OPERAND (def
, 1);
209 /* If TEST_VAR is set by adding or subtracting a constant
210 from an SSA_NAME, then it is interesting to us as we
211 can adjust the constant in the conditional and thus
212 eliminate the arithmetic operation. */
213 if (TREE_CODE (def_rhs
) == PLUS_EXPR
214 || TREE_CODE (def_rhs
) == MINUS_EXPR
)
216 tree op0
= TREE_OPERAND (def_rhs
, 0);
217 tree op1
= TREE_OPERAND (def_rhs
, 1);
219 /* The first operand must be an SSA_NAME and the second
220 operand must be a constant. */
221 if (TREE_CODE (op0
) != SSA_NAME
222 || !CONSTANT_CLASS_P (op1
)
223 || !INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
226 /* Don't propagate if the first operand occurs in
228 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
))
231 if (has_single_use (test_var
))
233 enum tree_code new_code
;
236 /* If the variable was defined via X + C, then we must
237 subtract C from the constant in the conditional.
238 Otherwise we add C to the constant in the
239 conditional. The result must fold into a valid
240 gimple operand to be optimizable. */
241 new_code
= (TREE_CODE (def_rhs
) == PLUS_EXPR
242 ? MINUS_EXPR
: PLUS_EXPR
);
243 t
= int_const_binop (new_code
, TREE_OPERAND (cond
, 1), op1
, 0);
244 if (!is_gimple_val (t
))
247 new_cond
= build2 (cond_code
, boolean_type_node
, op0
, t
);
251 /* These cases require comparisons of a naked SSA_NAME or
252 comparison of an SSA_NAME against zero or one. */
253 else if (TREE_CODE (cond
) == SSA_NAME
254 || integer_zerop (TREE_OPERAND (cond
, 1))
255 || integer_onep (TREE_OPERAND (cond
, 1)))
257 /* If TEST_VAR is set from a relational operation
258 between two SSA_NAMEs or a combination of an SSA_NAME
259 and a constant, then it is interesting. */
260 if (COMPARISON_CLASS_P (def_rhs
))
262 tree op0
= TREE_OPERAND (def_rhs
, 0);
263 tree op1
= TREE_OPERAND (def_rhs
, 1);
265 /* Both operands of DEF_RHS must be SSA_NAMEs or
267 if ((TREE_CODE (op0
) != SSA_NAME
268 && !is_gimple_min_invariant (op0
))
269 || (TREE_CODE (op1
) != SSA_NAME
270 && !is_gimple_min_invariant (op1
)))
273 /* Don't propagate if the first operand occurs in
275 if (TREE_CODE (op0
) == SSA_NAME
276 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
))
279 /* Don't propagate if the second operand occurs in
281 if (TREE_CODE (op1
) == SSA_NAME
282 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1
))
285 if (has_single_use (test_var
))
287 /* TEST_VAR was set from a relational operator. */
288 new_cond
= build2 (TREE_CODE (def_rhs
),
289 boolean_type_node
, op0
, op1
);
291 /* Invert the conditional if necessary. */
292 if ((cond_code
== EQ_EXPR
293 && integer_zerop (TREE_OPERAND (cond
, 1)))
294 || (cond_code
== NE_EXPR
295 && integer_onep (TREE_OPERAND (cond
, 1))))
297 new_cond
= invert_truthvalue (new_cond
);
299 /* If we did not get a simple relational
300 expression or bare SSA_NAME, then we can
301 not optimize this case. */
302 if (!COMPARISON_CLASS_P (new_cond
)
303 && TREE_CODE (new_cond
) != SSA_NAME
)
304 new_cond
= NULL_TREE
;
309 /* If TEST_VAR is set from a TRUTH_NOT_EXPR, then it
311 else if (TREE_CODE (def_rhs
) == TRUTH_NOT_EXPR
)
313 enum tree_code new_code
;
315 def_rhs
= TREE_OPERAND (def_rhs
, 0);
317 /* DEF_RHS must be an SSA_NAME or constant. */
318 if (TREE_CODE (def_rhs
) != SSA_NAME
319 && !is_gimple_min_invariant (def_rhs
))
322 /* Don't propagate if the operand occurs in
324 if (TREE_CODE (def_rhs
) == SSA_NAME
325 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_rhs
))
328 if (cond_code
== SSA_NAME
329 || (cond_code
== NE_EXPR
330 && integer_zerop (TREE_OPERAND (cond
, 1)))
331 || (cond_code
== EQ_EXPR
332 && integer_onep (TREE_OPERAND (cond
, 1))))
337 new_cond
= build2 (new_code
, boolean_type_node
, def_rhs
,
338 fold_convert (TREE_TYPE (def_rhs
),
342 /* If TEST_VAR was set from a cast of an integer type
343 to a boolean type or a cast of a boolean to an
344 integral, then it is interesting. */
345 else if (TREE_CODE (def_rhs
) == NOP_EXPR
346 || TREE_CODE (def_rhs
) == CONVERT_EXPR
)
351 outer_type
= TREE_TYPE (def_rhs
);
352 inner_type
= TREE_TYPE (TREE_OPERAND (def_rhs
, 0));
354 if ((TREE_CODE (outer_type
) == BOOLEAN_TYPE
355 && INTEGRAL_TYPE_P (inner_type
))
356 || (TREE_CODE (inner_type
) == BOOLEAN_TYPE
357 && INTEGRAL_TYPE_P (outer_type
)))
359 else if (INTEGRAL_TYPE_P (outer_type
)
360 && INTEGRAL_TYPE_P (inner_type
)
361 && TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == SSA_NAME
362 && ssa_name_defined_by_comparison_p (TREE_OPERAND (def_rhs
,
368 /* Don't propagate if the operand occurs in
370 if (TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == SSA_NAME
371 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND
375 if (has_single_use (test_var
))
377 enum tree_code new_code
;
380 if (cond_code
== SSA_NAME
381 || (cond_code
== NE_EXPR
382 && integer_zerop (TREE_OPERAND (cond
, 1)))
383 || (cond_code
== EQ_EXPR
384 && integer_onep (TREE_OPERAND (cond
, 1))))
389 new_arg
= TREE_OPERAND (def_rhs
, 0);
390 new_cond
= build2 (new_code
, boolean_type_node
, new_arg
,
391 fold_convert (TREE_TYPE (new_arg
),
397 *test_var_p
= test_var
;
401 /* Forward propagate a single-use variable into COND_EXPR as many
402 times as possible. */
405 forward_propagate_into_cond (tree cond_expr
)
407 gcc_assert (TREE_CODE (cond_expr
) == COND_EXPR
);
411 tree test_var
= NULL_TREE
;
412 tree cond
= COND_EXPR_COND (cond_expr
);
413 tree new_cond
= forward_propagate_into_cond_1 (cond
, &test_var
);
415 /* Return if unsuccessful. */
416 if (new_cond
== NULL_TREE
)
420 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
422 fprintf (dump_file
, " Replaced '");
423 print_generic_expr (dump_file
, cond
, dump_flags
);
424 fprintf (dump_file
, "' with '");
425 print_generic_expr (dump_file
, new_cond
, dump_flags
);
426 fprintf (dump_file
, "'\n");
429 COND_EXPR_COND (cond_expr
) = new_cond
;
430 update_stmt (cond_expr
);
432 if (has_zero_uses (test_var
))
434 tree def
= SSA_NAME_DEF_STMT (test_var
);
435 block_stmt_iterator bsi
= bsi_for_stmt (def
);
436 bsi_remove (&bsi
, true);
441 /* We've just substituted an ADDR_EXPR into stmt. Update all the
442 relevant data structures to match. */
445 tidy_after_forward_propagate_addr (tree stmt
)
447 /* We may have turned a trapping insn into a non-trapping insn. */
448 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
449 && tree_purge_dead_eh_edges (bb_for_stmt (stmt
)))
452 if (TREE_CODE (TREE_OPERAND (stmt
, 1)) == ADDR_EXPR
)
453 recompute_tree_invariant_for_addr_expr (TREE_OPERAND (stmt
, 1));
455 mark_new_vars_to_rename (stmt
);
458 /* STMT defines LHS which is contains the address of the 0th element
459 in an array. USE_STMT uses LHS to compute the address of an
460 arbitrary element within the array. The (variable) byte offset
461 of the element is contained in OFFSET.
463 We walk back through the use-def chains of OFFSET to verify that
464 it is indeed computing the offset of an element within the array
465 and extract the index corresponding to the given byte offset.
467 We then try to fold the entire address expression into a form
470 If we are successful, we replace the right hand side of USE_STMT
471 with the new address computation. */
474 forward_propagate_addr_into_variable_array_index (tree offset
, tree lhs
,
475 tree stmt
, tree use_stmt
)
479 /* The offset must be defined by a simple MODIFY_EXPR statement. */
480 if (TREE_CODE (offset
) != MODIFY_EXPR
)
483 /* The RHS of the statement which defines OFFSET must be a gimple
484 cast of another SSA_NAME. */
485 offset
= TREE_OPERAND (offset
, 1);
486 if (!is_gimple_cast (offset
))
489 offset
= TREE_OPERAND (offset
, 0);
490 if (TREE_CODE (offset
) != SSA_NAME
)
493 /* Get the defining statement of the offset before type
495 offset
= SSA_NAME_DEF_STMT (offset
);
497 /* The statement which defines OFFSET before type conversion
498 must be a simple MODIFY_EXPR. */
499 if (TREE_CODE (offset
) != MODIFY_EXPR
)
502 /* The RHS of the statement which defines OFFSET must be a
503 multiplication of an object by the size of the array elements.
504 This implicitly verifies that the size of the array elements
506 offset
= TREE_OPERAND (offset
, 1);
507 if (TREE_CODE (offset
) != MULT_EXPR
508 || TREE_CODE (TREE_OPERAND (offset
, 1)) != INTEGER_CST
509 || !simple_cst_equal (TREE_OPERAND (offset
, 1),
510 TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (lhs
)))))
513 /* The first operand to the MULT_EXPR is the desired index. */
514 index
= TREE_OPERAND (offset
, 0);
516 /* Replace the pointer addition with array indexing. */
517 TREE_OPERAND (use_stmt
, 1) = unshare_expr (TREE_OPERAND (stmt
, 1));
518 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (use_stmt
, 1), 0), 1) = index
;
520 /* That should have created gimple, so there is no need to
521 record information to undo the propagation. */
522 fold_stmt_inplace (use_stmt
);
523 tidy_after_forward_propagate_addr (use_stmt
);
527 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
529 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
530 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
531 node or for recovery of array indexing from pointer arithmetic.
532 Return true, if the propagation was successful. */
535 forward_propagate_addr_expr_1 (tree stmt
, tree use_stmt
)
537 tree name
= TREE_OPERAND (stmt
, 0);
538 tree lhs
, rhs
, array_ref
;
540 /* Strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
541 ADDR_EXPR will not appear on the LHS. */
542 lhs
= TREE_OPERAND (use_stmt
, 0);
543 while (TREE_CODE (lhs
) == COMPONENT_REF
|| TREE_CODE (lhs
) == ARRAY_REF
)
544 lhs
= TREE_OPERAND (lhs
, 0);
546 /* Now see if the LHS node is an INDIRECT_REF using NAME. If so,
547 propagate the ADDR_EXPR into the use of NAME and fold the result. */
548 if (TREE_CODE (lhs
) == INDIRECT_REF
&& TREE_OPERAND (lhs
, 0) == name
)
550 /* This should always succeed in creating gimple, so there is
551 no need to save enough state to undo this propagation. */
552 TREE_OPERAND (lhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
553 fold_stmt_inplace (use_stmt
);
554 tidy_after_forward_propagate_addr (use_stmt
);
558 /* Trivial case. The use statement could be a trivial copy. We
559 go ahead and handle that case here since it's trivial and
560 removes the need to run copy-prop before this pass to get
561 the best results. Also note that by handling this case here
562 we can catch some cascading effects, ie the single use is
563 in a copy, and the copy is used later by a single INDIRECT_REF
565 if (TREE_CODE (lhs
) == SSA_NAME
&& TREE_OPERAND (use_stmt
, 1) == name
)
567 TREE_OPERAND (use_stmt
, 1) = unshare_expr (TREE_OPERAND (stmt
, 1));
568 tidy_after_forward_propagate_addr (use_stmt
);
572 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
573 nodes from the RHS. */
574 rhs
= TREE_OPERAND (use_stmt
, 1);
575 while (TREE_CODE (rhs
) == COMPONENT_REF
576 || TREE_CODE (rhs
) == ARRAY_REF
577 || TREE_CODE (rhs
) == ADDR_EXPR
)
578 rhs
= TREE_OPERAND (rhs
, 0);
580 /* Now see if the RHS node is an INDIRECT_REF using NAME. If so,
581 propagate the ADDR_EXPR into the use of NAME and fold the result. */
582 if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_OPERAND (rhs
, 0) == name
)
584 /* This should always succeed in creating gimple, so there is
585 no need to save enough state to undo this propagation. */
586 TREE_OPERAND (rhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
587 fold_stmt_inplace (use_stmt
);
588 tidy_after_forward_propagate_addr (use_stmt
);
592 /* The remaining cases are all for turning pointer arithmetic into
593 array indexing. They only apply when we have the address of
594 element zero in an array. If that is not the case then there
596 array_ref
= TREE_OPERAND (TREE_OPERAND (stmt
, 1), 0);
597 if (TREE_CODE (array_ref
) != ARRAY_REF
598 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
599 || !integer_zerop (TREE_OPERAND (array_ref
, 1)))
602 /* If the use of the ADDR_EXPR must be a PLUS_EXPR, or else there
604 if (TREE_CODE (rhs
) != PLUS_EXPR
)
607 /* Try to optimize &x[0] + C where C is a multiple of the size
608 of the elements in X into &x[C/element size]. */
609 if (TREE_OPERAND (rhs
, 0) == name
610 && TREE_CODE (TREE_OPERAND (rhs
, 1)) == INTEGER_CST
)
612 tree orig
= unshare_expr (rhs
);
613 TREE_OPERAND (rhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
615 /* If folding succeeds, then we have just exposed new variables
616 in USE_STMT which will need to be renamed. If folding fails,
617 then we need to put everything back the way it was. */
618 if (fold_stmt_inplace (use_stmt
))
620 tidy_after_forward_propagate_addr (use_stmt
);
625 TREE_OPERAND (use_stmt
, 1) = orig
;
626 update_stmt (use_stmt
);
631 /* Try to optimize &x[0] + OFFSET where OFFSET is defined by
632 converting a multiplication of an index by the size of the
633 array elements, then the result is converted into the proper
634 type for the arithmetic. */
635 if (TREE_OPERAND (rhs
, 0) == name
636 && TREE_CODE (TREE_OPERAND (rhs
, 1)) == SSA_NAME
637 /* Avoid problems with IVopts creating PLUS_EXPRs with a
638 different type than their operands. */
639 && lang_hooks
.types_compatible_p (TREE_TYPE (name
), TREE_TYPE (rhs
)))
641 tree offset_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 1));
642 return forward_propagate_addr_into_variable_array_index (offset_stmt
, lhs
,
646 /* Same as the previous case, except the operands of the PLUS_EXPR
648 if (TREE_OPERAND (rhs
, 1) == name
649 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
650 /* Avoid problems with IVopts creating PLUS_EXPRs with a
651 different type than their operands. */
652 && lang_hooks
.types_compatible_p (TREE_TYPE (name
), TREE_TYPE (rhs
)))
654 tree offset_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 0));
655 return forward_propagate_addr_into_variable_array_index (offset_stmt
, lhs
,
661 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
663 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
664 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
665 node or for recovery of array indexing from pointer arithmetic.
666 Returns true, if all uses have been propagated into. */
669 forward_propagate_addr_expr (tree stmt
)
671 int stmt_loop_depth
= bb_for_stmt (stmt
)->loop_depth
;
672 tree name
= TREE_OPERAND (stmt
, 0);
673 use_operand_p imm_use
;
674 imm_use_iterator iter
;
677 FOR_EACH_IMM_USE_SAFE (imm_use
, iter
, name
)
679 tree use_stmt
= USE_STMT (imm_use
);
681 /* If the use is not in a simple assignment statement, then
682 there is nothing we can do. */
683 if (TREE_CODE (use_stmt
) != MODIFY_EXPR
)
689 /* If the use is in a deeper loop nest, then we do not want
690 to propagate the ADDR_EXPR into the loop as that is likely
691 adding expression evaluations into the loop. */
692 if (bb_for_stmt (use_stmt
)->loop_depth
> stmt_loop_depth
)
698 all
= forward_propagate_addr_expr_1 (stmt
, use_stmt
) && all
;
704 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
705 If so, we can change STMT into lhs = y which can later be copy
706 propagated. Similarly for negation.
708 This could trivially be formulated as a forward propagation
709 to immediate uses. However, we already had an implementation
710 from DOM which used backward propagation via the use-def links.
712 It turns out that backward propagation is actually faster as
713 there's less work to do for each NOT/NEG expression we find.
714 Backwards propagation needs to look at the statement in a single
715 backlink. Forward propagation needs to look at potentially more
716 than one forward link. */
719 simplify_not_neg_expr (tree stmt
)
721 tree rhs
= TREE_OPERAND (stmt
, 1);
722 tree rhs_def_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 0));
724 /* See if the RHS_DEF_STMT has the same form as our statement. */
725 if (TREE_CODE (rhs_def_stmt
) == MODIFY_EXPR
726 && TREE_CODE (TREE_OPERAND (rhs_def_stmt
, 1)) == TREE_CODE (rhs
))
728 tree rhs_def_operand
= TREE_OPERAND (TREE_OPERAND (rhs_def_stmt
, 1), 0);
730 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
731 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
732 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
734 TREE_OPERAND (stmt
, 1) = rhs_def_operand
;
740 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
741 the condition which we may be able to optimize better. */
744 simplify_switch_expr (tree stmt
)
746 tree cond
= SWITCH_COND (stmt
);
749 /* The optimization that we really care about is removing unnecessary
750 casts. That will let us do much better in propagating the inferred
751 constant at the switch target. */
752 if (TREE_CODE (cond
) == SSA_NAME
)
754 def
= SSA_NAME_DEF_STMT (cond
);
755 if (TREE_CODE (def
) == MODIFY_EXPR
)
757 def
= TREE_OPERAND (def
, 1);
758 if (TREE_CODE (def
) == NOP_EXPR
)
763 def
= TREE_OPERAND (def
, 0);
765 #ifdef ENABLE_CHECKING
766 /* ??? Why was Jeff testing this? We are gimple... */
767 gcc_assert (is_gimple_val (def
));
770 to
= TREE_TYPE (cond
);
771 ti
= TREE_TYPE (def
);
773 /* If we have an extension that preserves value, then we
774 can copy the source value into the switch. */
776 need_precision
= TYPE_PRECISION (ti
);
778 if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
780 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
782 if (TYPE_PRECISION (to
) < need_precision
)
787 SWITCH_COND (stmt
) = def
;
795 /* Main entry point for the forward propagation optimizer. */
798 tree_ssa_forward_propagate_single_use_vars (void)
806 block_stmt_iterator bsi
;
808 /* Note we update BSI within the loop as necessary. */
809 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
811 tree stmt
= bsi_stmt (bsi
);
813 /* If this statement sets an SSA_NAME to an address,
814 try to propagate the address into the uses of the SSA_NAME. */
815 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
817 tree lhs
= TREE_OPERAND (stmt
, 0);
818 tree rhs
= TREE_OPERAND (stmt
, 1);
821 if (TREE_CODE (lhs
) != SSA_NAME
)
827 if (TREE_CODE (rhs
) == ADDR_EXPR
)
829 if (forward_propagate_addr_expr (stmt
))
830 bsi_remove (&bsi
, true);
834 else if ((TREE_CODE (rhs
) == BIT_NOT_EXPR
835 || TREE_CODE (rhs
) == NEGATE_EXPR
)
836 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
838 simplify_not_neg_expr (stmt
);
844 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
846 simplify_switch_expr (stmt
);
849 else if (TREE_CODE (stmt
) == COND_EXPR
)
851 forward_propagate_into_cond (stmt
);
870 struct tree_opt_pass pass_forwprop
= {
871 "forwprop", /* name */
872 gate_forwprop
, /* gate */
873 tree_ssa_forward_propagate_single_use_vars
, /* execute */
876 0, /* static_pass_number */
877 TV_TREE_FORWPROP
, /* tv_id */
879 | PROP_alias
, /* properties_required */
880 0, /* properties_provided */
881 0, /* properties_destroyed */
882 0, /* todo_flags_start */
883 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */
884 | TODO_update_ssa
| TODO_verify_ssa
,