1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004, 2005, 2007 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-pass.h"
33 #include "tree-dump.h"
34 #include "langhooks.h"
36 /* This pass propagates the RHS of assignment statements into use
37 sites of the LHS of the assignment. It's basically a specialized
38 form of tree combination. It is hoped all of this can disappear
39 when we have a generalized tree combiner.
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:
145 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
146 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
149 This will (of course) be extended as other needs arise. */
152 /* Set to true if we delete EH edges during the optimization. */
153 static bool cfg_changed
;
156 /* Given an SSA_NAME VAR, return true if and only if VAR is defined by
160 ssa_name_defined_by_comparison_p (tree var
)
162 tree def
= SSA_NAME_DEF_STMT (var
);
164 if (TREE_CODE (def
) == MODIFY_EXPR
)
166 tree rhs
= TREE_OPERAND (def
, 1);
167 return COMPARISON_CLASS_P (rhs
);
173 /* Forward propagate a single-use variable into COND once. Return a
174 new condition if successful. Return NULL_TREE otherwise. */
177 forward_propagate_into_cond_1 (tree cond
, tree
*test_var_p
)
179 tree new_cond
= NULL_TREE
;
180 enum tree_code cond_code
= TREE_CODE (cond
);
181 tree test_var
= NULL_TREE
;
185 /* If the condition is not a lone variable or an equality test of an
186 SSA_NAME against an integral constant, then we do not have an
189 Note these conditions also ensure the COND_EXPR has no
190 virtual operands or other side effects. */
191 if (cond_code
!= SSA_NAME
192 && !((cond_code
== EQ_EXPR
|| cond_code
== NE_EXPR
)
193 && TREE_CODE (TREE_OPERAND (cond
, 0)) == SSA_NAME
194 && CONSTANT_CLASS_P (TREE_OPERAND (cond
, 1))
195 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))))
198 /* Extract the single variable used in the test into TEST_VAR. */
199 if (cond_code
== SSA_NAME
)
202 test_var
= TREE_OPERAND (cond
, 0);
204 /* Now get the defining statement for TEST_VAR. Skip this case if
205 it's not defined by some MODIFY_EXPR. */
206 def
= SSA_NAME_DEF_STMT (test_var
);
207 if (TREE_CODE (def
) != MODIFY_EXPR
)
210 def_rhs
= TREE_OPERAND (def
, 1);
212 /* If TEST_VAR is set by adding or subtracting a constant
213 from an SSA_NAME, then it is interesting to us as we
214 can adjust the constant in the conditional and thus
215 eliminate the arithmetic operation. */
216 if (TREE_CODE (def_rhs
) == PLUS_EXPR
217 || TREE_CODE (def_rhs
) == MINUS_EXPR
)
219 tree op0
= TREE_OPERAND (def_rhs
, 0);
220 tree op1
= TREE_OPERAND (def_rhs
, 1);
222 /* The first operand must be an SSA_NAME and the second
223 operand must be a constant. */
224 if (TREE_CODE (op0
) != SSA_NAME
225 || !CONSTANT_CLASS_P (op1
)
226 || !INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
229 /* Don't propagate if the first operand occurs in
231 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
))
234 if (has_single_use (test_var
))
236 enum tree_code new_code
;
239 /* If the variable was defined via X + C, then we must
240 subtract C from the constant in the conditional.
241 Otherwise we add C to the constant in the
242 conditional. The result must fold into a valid
243 gimple operand to be optimizable. */
244 new_code
= (TREE_CODE (def_rhs
) == PLUS_EXPR
245 ? MINUS_EXPR
: PLUS_EXPR
);
246 t
= int_const_binop (new_code
, TREE_OPERAND (cond
, 1), op1
, 0);
247 if (!is_gimple_val (t
))
250 new_cond
= build2 (cond_code
, boolean_type_node
, op0
, t
);
254 /* These cases require comparisons of a naked SSA_NAME or
255 comparison of an SSA_NAME against zero or one. */
256 else if (TREE_CODE (cond
) == SSA_NAME
257 || integer_zerop (TREE_OPERAND (cond
, 1))
258 || integer_onep (TREE_OPERAND (cond
, 1)))
260 /* If TEST_VAR is set from a relational operation
261 between two SSA_NAMEs or a combination of an SSA_NAME
262 and a constant, then it is interesting. */
263 if (COMPARISON_CLASS_P (def_rhs
))
265 tree op0
= TREE_OPERAND (def_rhs
, 0);
266 tree op1
= TREE_OPERAND (def_rhs
, 1);
268 /* Both operands of DEF_RHS must be SSA_NAMEs or
270 if ((TREE_CODE (op0
) != SSA_NAME
271 && !is_gimple_min_invariant (op0
))
272 || (TREE_CODE (op1
) != SSA_NAME
273 && !is_gimple_min_invariant (op1
)))
276 /* Don't propagate if the first operand occurs in
278 if (TREE_CODE (op0
) == SSA_NAME
279 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0
))
282 /* Don't propagate if the second operand occurs in
284 if (TREE_CODE (op1
) == SSA_NAME
285 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1
))
288 if (has_single_use (test_var
))
290 /* TEST_VAR was set from a relational operator. */
291 new_cond
= build2 (TREE_CODE (def_rhs
),
292 boolean_type_node
, op0
, op1
);
294 /* Invert the conditional if necessary. */
295 if ((cond_code
== EQ_EXPR
296 && integer_zerop (TREE_OPERAND (cond
, 1)))
297 || (cond_code
== NE_EXPR
298 && integer_onep (TREE_OPERAND (cond
, 1))))
300 new_cond
= invert_truthvalue (new_cond
);
302 /* If we did not get a simple relational
303 expression or bare SSA_NAME, then we can
304 not optimize this case. */
305 if (!COMPARISON_CLASS_P (new_cond
)
306 && TREE_CODE (new_cond
) != SSA_NAME
)
307 new_cond
= NULL_TREE
;
312 /* If TEST_VAR is set from a TRUTH_NOT_EXPR, then it
314 else if (TREE_CODE (def_rhs
) == TRUTH_NOT_EXPR
)
316 enum tree_code new_code
;
318 def_rhs
= TREE_OPERAND (def_rhs
, 0);
320 /* DEF_RHS must be an SSA_NAME or constant. */
321 if (TREE_CODE (def_rhs
) != SSA_NAME
322 && !is_gimple_min_invariant (def_rhs
))
325 /* Don't propagate if the operand occurs in
327 if (TREE_CODE (def_rhs
) == SSA_NAME
328 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_rhs
))
331 if (cond_code
== SSA_NAME
332 || (cond_code
== NE_EXPR
333 && integer_zerop (TREE_OPERAND (cond
, 1)))
334 || (cond_code
== EQ_EXPR
335 && integer_onep (TREE_OPERAND (cond
, 1))))
340 new_cond
= build2 (new_code
, boolean_type_node
, def_rhs
,
341 fold_convert (TREE_TYPE (def_rhs
),
345 /* If TEST_VAR was set from a cast of an integer type
346 to a boolean type or a cast of a boolean to an
347 integral, then it is interesting. */
348 else if (TREE_CODE (def_rhs
) == NOP_EXPR
349 || TREE_CODE (def_rhs
) == CONVERT_EXPR
)
354 outer_type
= TREE_TYPE (def_rhs
);
355 inner_type
= TREE_TYPE (TREE_OPERAND (def_rhs
, 0));
357 if ((TREE_CODE (outer_type
) == BOOLEAN_TYPE
358 && INTEGRAL_TYPE_P (inner_type
))
359 || (TREE_CODE (inner_type
) == BOOLEAN_TYPE
360 && INTEGRAL_TYPE_P (outer_type
)))
362 else if (INTEGRAL_TYPE_P (outer_type
)
363 && INTEGRAL_TYPE_P (inner_type
)
364 && TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == SSA_NAME
365 && ssa_name_defined_by_comparison_p (TREE_OPERAND (def_rhs
,
371 /* Don't propagate if the operand occurs in
373 if (TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == SSA_NAME
374 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND
378 if (has_single_use (test_var
))
380 enum tree_code new_code
;
383 if (cond_code
== SSA_NAME
384 || (cond_code
== NE_EXPR
385 && integer_zerop (TREE_OPERAND (cond
, 1)))
386 || (cond_code
== EQ_EXPR
387 && integer_onep (TREE_OPERAND (cond
, 1))))
392 new_arg
= TREE_OPERAND (def_rhs
, 0);
393 new_cond
= build2 (new_code
, boolean_type_node
, new_arg
,
394 fold_convert (TREE_TYPE (new_arg
),
400 *test_var_p
= test_var
;
404 /* COND is a condition of the form:
406 x == const or x != const
408 Look back to x's defining statement and see if x is defined as
412 If const is unchanged if we convert it to type, then we can build
413 the equivalent expression:
416 y == const or y != const
418 Which may allow further optimizations.
420 Return the equivalent comparison or NULL if no such equivalent comparison
424 find_equivalent_equality_comparison (tree cond
)
426 tree op0
= TREE_OPERAND (cond
, 0);
427 tree op1
= TREE_OPERAND (cond
, 1);
428 tree def_stmt
= SSA_NAME_DEF_STMT (op0
);
431 && TREE_CODE (def_stmt
) == MODIFY_EXPR
432 && TREE_CODE (TREE_OPERAND (def_stmt
, 1)) == SSA_NAME
)
433 def_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (def_stmt
, 1));
435 /* OP0 might have been a parameter, so first make sure it
436 was defined by a MODIFY_EXPR. */
437 if (def_stmt
&& TREE_CODE (def_stmt
) == MODIFY_EXPR
)
439 tree def_rhs
= TREE_OPERAND (def_stmt
, 1);
441 /* If either operand to the comparison is a pointer to
442 a function, then we can not apply this optimization
443 as some targets require function pointers to be
444 canonicalized and in this case this optimization would
445 eliminate a necessary canonicalization. */
446 if ((POINTER_TYPE_P (TREE_TYPE (op0
))
447 && TREE_CODE (TREE_TYPE (TREE_TYPE (op0
))) == FUNCTION_TYPE
)
448 || (POINTER_TYPE_P (TREE_TYPE (op1
))
449 && TREE_CODE (TREE_TYPE (TREE_TYPE (op1
))) == FUNCTION_TYPE
))
452 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
453 if ((TREE_CODE (def_rhs
) == NOP_EXPR
454 || TREE_CODE (def_rhs
) == CONVERT_EXPR
)
455 && TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == SSA_NAME
)
457 tree def_rhs_inner
= TREE_OPERAND (def_rhs
, 0);
458 tree def_rhs_inner_type
= TREE_TYPE (def_rhs_inner
);
461 if (TYPE_PRECISION (def_rhs_inner_type
)
462 > TYPE_PRECISION (TREE_TYPE (def_rhs
)))
465 /* If the inner type of the conversion is a pointer to
466 a function, then we can not apply this optimization
467 as some targets require function pointers to be
468 canonicalized. This optimization would result in
469 canonicalization of the pointer when it was not originally
471 if (POINTER_TYPE_P (def_rhs_inner_type
)
472 && TREE_CODE (TREE_TYPE (def_rhs_inner_type
)) == FUNCTION_TYPE
)
475 /* What we want to prove is that if we convert OP1 to
476 the type of the object inside the NOP_EXPR that the
477 result is still equivalent to SRC.
479 If that is true, the build and return new equivalent
480 condition which uses the source of the typecast and the
481 new constant (which has only changed its type). */
482 new = fold_build1 (TREE_CODE (def_rhs
), def_rhs_inner_type
, op1
);
483 STRIP_USELESS_TYPE_CONVERSION (new);
484 if (is_gimple_val (new) && tree_int_cst_equal (new, op1
))
485 return build2 (TREE_CODE (cond
), TREE_TYPE (cond
),
492 /* STMT is a COND_EXPR
494 This routine attempts to find equivalent forms of the condition
495 which we may be able to optimize better. */
498 simplify_cond (tree stmt
)
500 tree cond
= COND_EXPR_COND (stmt
);
502 if (COMPARISON_CLASS_P (cond
))
504 tree op0
= TREE_OPERAND (cond
, 0);
505 tree op1
= TREE_OPERAND (cond
, 1);
507 if (TREE_CODE (op0
) == SSA_NAME
&& is_gimple_min_invariant (op1
))
509 /* First see if we have test of an SSA_NAME against a constant
510 where the SSA_NAME is defined by an earlier typecast which
511 is irrelevant when performing tests against the given
513 if (TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
515 tree new_cond
= find_equivalent_equality_comparison (cond
);
519 COND_EXPR_COND (stmt
) = new_cond
;
527 /* Forward propagate a single-use variable into COND_EXPR as many
528 times as possible. */
531 forward_propagate_into_cond (tree cond_expr
)
533 gcc_assert (TREE_CODE (cond_expr
) == COND_EXPR
);
537 tree test_var
= NULL_TREE
;
538 tree cond
= COND_EXPR_COND (cond_expr
);
539 tree new_cond
= forward_propagate_into_cond_1 (cond
, &test_var
);
541 /* Return if unsuccessful. */
542 if (new_cond
== NULL_TREE
)
546 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
548 fprintf (dump_file
, " Replaced '");
549 print_generic_expr (dump_file
, cond
, dump_flags
);
550 fprintf (dump_file
, "' with '");
551 print_generic_expr (dump_file
, new_cond
, dump_flags
);
552 fprintf (dump_file
, "'\n");
555 COND_EXPR_COND (cond_expr
) = new_cond
;
556 update_stmt (cond_expr
);
558 if (has_zero_uses (test_var
))
560 tree def
= SSA_NAME_DEF_STMT (test_var
);
561 block_stmt_iterator bsi
= bsi_for_stmt (def
);
562 bsi_remove (&bsi
, true);
566 /* There are further simplifications that can be performed
567 on COND_EXPRs. Specifically, when comparing an SSA_NAME
568 against a constant where the SSA_NAME is the result of a
569 conversion. Perhaps this should be folded into the rest
570 of the COND_EXPR simplification code. */
571 simplify_cond (cond_expr
);
574 /* We've just substituted an ADDR_EXPR into stmt. Update all the
575 relevant data structures to match. */
578 tidy_after_forward_propagate_addr (tree stmt
)
580 /* We may have turned a trapping insn into a non-trapping insn. */
581 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
582 && tree_purge_dead_eh_edges (bb_for_stmt (stmt
)))
585 if (TREE_CODE (TREE_OPERAND (stmt
, 1)) == ADDR_EXPR
)
586 recompute_tree_invariant_for_addr_expr (TREE_OPERAND (stmt
, 1));
588 mark_new_vars_to_rename (stmt
);
591 /* STMT defines LHS which is contains the address of the 0th element
592 in an array. USE_STMT uses LHS to compute the address of an
593 arbitrary element within the array. The (variable) byte offset
594 of the element is contained in OFFSET.
596 We walk back through the use-def chains of OFFSET to verify that
597 it is indeed computing the offset of an element within the array
598 and extract the index corresponding to the given byte offset.
600 We then try to fold the entire address expression into a form
603 If we are successful, we replace the right hand side of USE_STMT
604 with the new address computation. */
607 forward_propagate_addr_into_variable_array_index (tree offset
, tree lhs
,
608 tree stmt
, tree use_stmt
)
612 /* The offset must be defined by a simple MODIFY_EXPR statement. */
613 if (TREE_CODE (offset
) != MODIFY_EXPR
)
616 /* The RHS of the statement which defines OFFSET must be a gimple
617 cast of another SSA_NAME. */
618 offset
= TREE_OPERAND (offset
, 1);
619 if (!is_gimple_cast (offset
))
622 offset
= TREE_OPERAND (offset
, 0);
623 if (TREE_CODE (offset
) != SSA_NAME
)
626 /* Get the defining statement of the offset before type
628 offset
= SSA_NAME_DEF_STMT (offset
);
630 /* The statement which defines OFFSET before type conversion
631 must be a simple MODIFY_EXPR. */
632 if (TREE_CODE (offset
) != MODIFY_EXPR
)
635 /* The RHS of the statement which defines OFFSET must be a
636 multiplication of an object by the size of the array elements.
637 This implicitly verifies that the size of the array elements
639 offset
= TREE_OPERAND (offset
, 1);
640 if (TREE_CODE (offset
) != MULT_EXPR
641 || TREE_CODE (TREE_OPERAND (offset
, 1)) != INTEGER_CST
642 || !simple_cst_equal (TREE_OPERAND (offset
, 1),
643 TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (lhs
)))))
646 /* The first operand to the MULT_EXPR is the desired index. */
647 index
= TREE_OPERAND (offset
, 0);
649 /* Replace the pointer addition with array indexing. */
650 TREE_OPERAND (use_stmt
, 1) = unshare_expr (TREE_OPERAND (stmt
, 1));
651 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (use_stmt
, 1), 0), 1) = index
;
653 /* That should have created gimple, so there is no need to
654 record information to undo the propagation. */
655 fold_stmt_inplace (use_stmt
);
656 tidy_after_forward_propagate_addr (use_stmt
);
660 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
662 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
663 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
664 node or for recovery of array indexing from pointer arithmetic.
666 CHANGED is an optional pointer to a boolean variable set to true if
667 either the LHS or RHS was changed in the USE_STMT.
669 Return true if the propagation was successful (the propagation can
670 be not totally successful, yet things may have been changed). */
673 forward_propagate_addr_expr_1 (tree stmt
, tree use_stmt
, bool *changed
)
675 tree name
= TREE_OPERAND (stmt
, 0);
676 tree lhs
, rhs
, array_ref
;
678 /* Strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
679 ADDR_EXPR will not appear on the LHS. */
680 lhs
= TREE_OPERAND (use_stmt
, 0);
681 while (TREE_CODE (lhs
) == COMPONENT_REF
|| TREE_CODE (lhs
) == ARRAY_REF
)
682 lhs
= TREE_OPERAND (lhs
, 0);
684 /* Now see if the LHS node is an INDIRECT_REF using NAME. If so,
685 propagate the ADDR_EXPR into the use of NAME and fold the result. */
686 if (TREE_CODE (lhs
) == INDIRECT_REF
&& TREE_OPERAND (lhs
, 0) == name
)
688 /* This should always succeed in creating gimple, so there is
689 no need to save enough state to undo this propagation. */
690 TREE_OPERAND (lhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
691 fold_stmt_inplace (use_stmt
);
692 tidy_after_forward_propagate_addr (use_stmt
);
697 /* Trivial case. The use statement could be a trivial copy. We
698 go ahead and handle that case here since it's trivial and
699 removes the need to run copy-prop before this pass to get
700 the best results. Also note that by handling this case here
701 we can catch some cascading effects, ie the single use is
702 in a copy, and the copy is used later by a single INDIRECT_REF
704 else if (TREE_CODE (lhs
) == SSA_NAME
&& TREE_OPERAND (use_stmt
, 1) == name
)
706 TREE_OPERAND (use_stmt
, 1) = unshare_expr (TREE_OPERAND (stmt
, 1));
707 tidy_after_forward_propagate_addr (use_stmt
);
713 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
714 nodes from the RHS. */
715 rhs
= TREE_OPERAND (use_stmt
, 1);
716 while (TREE_CODE (rhs
) == COMPONENT_REF
717 || TREE_CODE (rhs
) == ARRAY_REF
718 || TREE_CODE (rhs
) == ADDR_EXPR
)
719 rhs
= TREE_OPERAND (rhs
, 0);
721 /* Now see if the RHS node is an INDIRECT_REF using NAME. If so,
722 propagate the ADDR_EXPR into the use of NAME and fold the result. */
723 if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_OPERAND (rhs
, 0) == name
)
725 /* This should always succeed in creating gimple, so there is
726 no need to save enough state to undo this propagation. */
727 TREE_OPERAND (rhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
728 fold_stmt_inplace (use_stmt
);
729 tidy_after_forward_propagate_addr (use_stmt
);
735 /* The remaining cases are all for turning pointer arithmetic into
736 array indexing. They only apply when we have the address of
737 element zero in an array. If that is not the case then there
739 array_ref
= TREE_OPERAND (TREE_OPERAND (stmt
, 1), 0);
740 if (TREE_CODE (array_ref
) != ARRAY_REF
741 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
742 || !integer_zerop (TREE_OPERAND (array_ref
, 1)))
745 /* If the use of the ADDR_EXPR must be a PLUS_EXPR, or else there
747 if (TREE_CODE (rhs
) != PLUS_EXPR
)
750 /* Try to optimize &x[0] + C where C is a multiple of the size
751 of the elements in X into &x[C/element size]. */
752 if (TREE_OPERAND (rhs
, 0) == name
753 && TREE_CODE (TREE_OPERAND (rhs
, 1)) == INTEGER_CST
)
755 tree orig
= unshare_expr (rhs
);
756 TREE_OPERAND (rhs
, 0) = unshare_expr (TREE_OPERAND (stmt
, 1));
758 /* If folding succeeds, then we have just exposed new variables
759 in USE_STMT which will need to be renamed. If folding fails,
760 then we need to put everything back the way it was. */
761 if (fold_stmt_inplace (use_stmt
))
763 tidy_after_forward_propagate_addr (use_stmt
);
770 TREE_OPERAND (use_stmt
, 1) = orig
;
771 update_stmt (use_stmt
);
776 /* Try to optimize &x[0] + OFFSET where OFFSET is defined by
777 converting a multiplication of an index by the size of the
778 array elements, then the result is converted into the proper
779 type for the arithmetic. */
780 if (TREE_OPERAND (rhs
, 0) == name
781 && TREE_CODE (TREE_OPERAND (rhs
, 1)) == SSA_NAME
782 /* Avoid problems with IVopts creating PLUS_EXPRs with a
783 different type than their operands. */
784 && lang_hooks
.types_compatible_p (TREE_TYPE (name
), TREE_TYPE (rhs
)))
787 tree offset_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 1));
789 res
= forward_propagate_addr_into_variable_array_index (offset_stmt
, lhs
,
796 /* Same as the previous case, except the operands of the PLUS_EXPR
798 if (TREE_OPERAND (rhs
, 1) == name
799 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
800 /* Avoid problems with IVopts creating PLUS_EXPRs with a
801 different type than their operands. */
802 && lang_hooks
.types_compatible_p (TREE_TYPE (name
), TREE_TYPE (rhs
)))
805 tree offset_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 0));
806 res
= forward_propagate_addr_into_variable_array_index (offset_stmt
, lhs
,
815 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
816 SOME is a pointer to a boolean value indicating whether we
817 propagated the address expression anywhere.
819 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
820 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
821 node or for recovery of array indexing from pointer arithmetic.
822 Returns true, if all uses have been propagated into. */
825 forward_propagate_addr_expr (tree stmt
, bool *some
)
827 int stmt_loop_depth
= bb_for_stmt (stmt
)->loop_depth
;
828 tree name
= TREE_OPERAND (stmt
, 0);
829 imm_use_iterator iter
;
833 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
837 /* If the use is not in a simple assignment statement, then
838 there is nothing we can do. */
839 if (TREE_CODE (use_stmt
) != MODIFY_EXPR
)
845 /* If the use is in a deeper loop nest, then we do not want
846 to propagate the ADDR_EXPR into the loop as that is likely
847 adding expression evaluations into the loop. */
848 if (bb_for_stmt (use_stmt
)->loop_depth
> stmt_loop_depth
)
854 /* If the use_stmt has side-effects, don't propagate into it. */
855 if (stmt_ann (use_stmt
)->has_volatile_ops
)
861 result
= forward_propagate_addr_expr_1 (stmt
, use_stmt
, some
);
869 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
870 If so, we can change STMT into lhs = y which can later be copy
871 propagated. Similarly for negation.
873 This could trivially be formulated as a forward propagation
874 to immediate uses. However, we already had an implementation
875 from DOM which used backward propagation via the use-def links.
877 It turns out that backward propagation is actually faster as
878 there's less work to do for each NOT/NEG expression we find.
879 Backwards propagation needs to look at the statement in a single
880 backlink. Forward propagation needs to look at potentially more
881 than one forward link. */
884 simplify_not_neg_expr (tree stmt
)
886 tree rhs
= TREE_OPERAND (stmt
, 1);
887 tree rhs_def_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (rhs
, 0));
889 /* See if the RHS_DEF_STMT has the same form as our statement. */
890 if (TREE_CODE (rhs_def_stmt
) == MODIFY_EXPR
891 && TREE_CODE (TREE_OPERAND (rhs_def_stmt
, 1)) == TREE_CODE (rhs
))
893 tree rhs_def_operand
= TREE_OPERAND (TREE_OPERAND (rhs_def_stmt
, 1), 0);
895 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
896 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
897 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
899 TREE_OPERAND (stmt
, 1) = rhs_def_operand
;
905 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
906 the condition which we may be able to optimize better. */
909 simplify_switch_expr (tree stmt
)
911 tree cond
= SWITCH_COND (stmt
);
914 /* The optimization that we really care about is removing unnecessary
915 casts. That will let us do much better in propagating the inferred
916 constant at the switch target. */
917 if (TREE_CODE (cond
) == SSA_NAME
)
919 def
= SSA_NAME_DEF_STMT (cond
);
920 if (TREE_CODE (def
) == MODIFY_EXPR
)
922 def
= TREE_OPERAND (def
, 1);
923 if (TREE_CODE (def
) == NOP_EXPR
)
928 def
= TREE_OPERAND (def
, 0);
930 #ifdef ENABLE_CHECKING
931 /* ??? Why was Jeff testing this? We are gimple... */
932 gcc_assert (is_gimple_val (def
));
935 to
= TREE_TYPE (cond
);
936 ti
= TREE_TYPE (def
);
938 /* If we have an extension that preserves value, then we
939 can copy the source value into the switch. */
941 need_precision
= TYPE_PRECISION (ti
);
943 if (! INTEGRAL_TYPE_P (ti
))
945 else if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
947 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
949 if (TYPE_PRECISION (to
) < need_precision
)
954 SWITCH_COND (stmt
) = def
;
962 /* Main entry point for the forward propagation optimizer. */
965 tree_ssa_forward_propagate_single_use_vars (void)
968 unsigned int todoflags
= 0;
974 block_stmt_iterator bsi
;
976 /* Note we update BSI within the loop as necessary. */
977 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
979 tree stmt
= bsi_stmt (bsi
);
981 /* If this statement sets an SSA_NAME to an address,
982 try to propagate the address into the uses of the SSA_NAME. */
983 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
985 tree lhs
= TREE_OPERAND (stmt
, 0);
986 tree rhs
= TREE_OPERAND (stmt
, 1);
989 if (TREE_CODE (lhs
) != SSA_NAME
)
995 if (TREE_CODE (rhs
) == ADDR_EXPR
)
998 if (forward_propagate_addr_expr (stmt
, &some
))
999 bsi_remove (&bsi
, true);
1003 todoflags
|= TODO_update_smt_usage
;
1005 else if ((TREE_CODE (rhs
) == BIT_NOT_EXPR
1006 || TREE_CODE (rhs
) == NEGATE_EXPR
)
1007 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
1009 simplify_not_neg_expr (stmt
);
1015 else if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1017 simplify_switch_expr (stmt
);
1020 else if (TREE_CODE (stmt
) == COND_EXPR
)
1022 forward_propagate_into_cond (stmt
);
1031 cleanup_tree_cfg ();
1037 gate_forwprop (void)
1042 struct tree_opt_pass pass_forwprop
= {
1043 "forwprop", /* name */
1044 gate_forwprop
, /* gate */
1045 tree_ssa_forward_propagate_single_use_vars
, /* execute */
1048 0, /* static_pass_number */
1049 TV_TREE_FORWPROP
, /* tv_id */
1051 | PROP_alias
, /* properties_required */
1052 0, /* properties_provided */
1053 PROP_smt_usage
, /* properties_destroyed */
1054 0, /* todo_flags_start */
1055 TODO_dump_func
/* todo_flags_finish */
1057 | TODO_update_ssa
| TODO_verify_ssa
,