+/* Attempt to fold an assignment statement pointed-to by SI. Returns a
+ replacement rhs for the statement or NULL_TREE if no simplification
+ could be made. It is assumed that the operands have been previously
+ folded. */
+
+static tree
+fold_gimple_assign (gimple_stmt_iterator *si)
+{
+ gimple stmt = gsi_stmt (*si);
+ enum tree_code subcode = gimple_assign_rhs_code (stmt);
+
+ tree result = NULL;
+
+ switch (get_gimple_rhs_class (subcode))
+ {
+ case GIMPLE_SINGLE_RHS:
+ {
+ tree rhs = gimple_assign_rhs1 (stmt);
+
+ /* Try to fold a conditional expression. */
+ if (TREE_CODE (rhs) == COND_EXPR)
+ {
+ tree temp = fold (COND_EXPR_COND (rhs));
+ if (temp != COND_EXPR_COND (rhs))
+ result = fold_build3 (COND_EXPR, TREE_TYPE (rhs), temp,
+ COND_EXPR_THEN (rhs), COND_EXPR_ELSE (rhs));
+ }
+
+ /* If we couldn't fold the RHS, hand over to the generic
+ fold routines. */
+ if (result == NULL_TREE)
+ result = fold (rhs);
+
+ /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
+ that may have been added by fold, and "useless" type
+ conversions that might now be apparent due to propagation. */
+ STRIP_USELESS_TYPE_CONVERSION (result);
+
+ if (result != rhs && valid_gimple_rhs_p (result))
+ return result;
+ else
+ /* It is possible that fold_stmt_r simplified the RHS.
+ Make sure that the subcode of this statement still
+ reflects the principal operator of the rhs operand. */
+ return rhs;
+ }
+ break;
+
+ case GIMPLE_UNARY_RHS:
+ {
+ tree rhs = gimple_assign_rhs1 (stmt);
+
+ result = fold_unary (subcode, gimple_expr_type (stmt), rhs);
+ if (result)
+ {
+ /* If the operation was a conversion do _not_ mark a
+ resulting constant with TREE_OVERFLOW if the original
+ constant was not. These conversions have implementation
+ defined behavior and retaining the TREE_OVERFLOW flag
+ here would confuse later passes such as VRP. */
+ if (CONVERT_EXPR_CODE_P (subcode)
+ && TREE_CODE (result) == INTEGER_CST
+ && TREE_CODE (rhs) == INTEGER_CST)
+ TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs);
+
+ STRIP_USELESS_TYPE_CONVERSION (result);
+ if (valid_gimple_rhs_p (result))
+ return result;
+ }
+ else if (CONVERT_EXPR_CODE_P (subcode)
+ && POINTER_TYPE_P (gimple_expr_type (stmt))
+ && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt))))
+ {
+ tree type = gimple_expr_type (stmt);
+ tree t = maybe_fold_offset_to_address (gimple_assign_rhs1 (stmt),
+ integer_zero_node, type);
+ if (t)
+ return t;
+ }
+ }
+ break;
+
+ case GIMPLE_BINARY_RHS:
+ /* Try to fold pointer addition. */
+ if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
+ {
+ tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
+ if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
+ {
+ type = build_pointer_type (TREE_TYPE (TREE_TYPE (type)));
+ if (!useless_type_conversion_p
+ (TREE_TYPE (gimple_assign_lhs (stmt)), type))
+ type = TREE_TYPE (gimple_assign_rhs1 (stmt));
+ }
+ result = maybe_fold_stmt_addition (type,
+ gimple_assign_rhs1 (stmt),
+ gimple_assign_rhs2 (stmt));
+ }
+
+ if (!result)
+ result = fold_binary (subcode,
+ TREE_TYPE (gimple_assign_lhs (stmt)),
+ gimple_assign_rhs1 (stmt),
+ gimple_assign_rhs2 (stmt));
+
+ if (result)
+ {
+ STRIP_USELESS_TYPE_CONVERSION (result);
+ if (valid_gimple_rhs_p (result))
+ return result;
+
+ /* Fold might have produced non-GIMPLE, so if we trust it blindly
+ we lose canonicalization opportunities. Do not go again
+ through fold here though, or the same non-GIMPLE will be
+ produced. */
+ if (commutative_tree_code (subcode)
+ && tree_swap_operands_p (gimple_assign_rhs1 (stmt),
+ gimple_assign_rhs2 (stmt), false))
+ return build2 (subcode, TREE_TYPE (gimple_assign_lhs (stmt)),
+ gimple_assign_rhs2 (stmt),
+ gimple_assign_rhs1 (stmt));
+ }
+ break;
+
+ case GIMPLE_INVALID_RHS:
+ gcc_unreachable ();
+ }
+
+ return NULL_TREE;
+}
+
+/* Attempt to fold a conditional statement. Return true if any changes were
+ made. We only attempt to fold the condition expression, and do not perform
+ any transformation that would require alteration of the cfg. It is
+ assumed that the operands have been previously folded. */
+
+static bool
+fold_gimple_cond (gimple stmt)
+{
+ tree result = fold_binary (gimple_cond_code (stmt),
+ boolean_type_node,
+ gimple_cond_lhs (stmt),
+ gimple_cond_rhs (stmt));
+
+ if (result)
+ {
+ STRIP_USELESS_TYPE_CONVERSION (result);
+ if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result))
+ {
+ gimple_cond_set_condition_from_tree (stmt, result);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+/* Attempt to fold a call statement referenced by the statement iterator GSI.
+ The statement may be replaced by another statement, e.g., if the call
+ simplifies to a constant value. Return true if any changes were made.
+ It is assumed that the operands have been previously folded. */
+
+static bool
+fold_gimple_call (gimple_stmt_iterator *gsi)
+{
+ gimple stmt = gsi_stmt (*gsi);
+
+ tree callee = gimple_call_fndecl (stmt);
+
+ /* Check for builtins that CCP can handle using information not
+ available in the generic fold routines. */
+ if (callee && DECL_BUILT_IN (callee))
+ {
+ tree result = ccp_fold_builtin (stmt);
+
+ if (result)
+ return update_call_from_tree (gsi, result);
+ }
+ else
+ {
+ /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve
+ here are when we've propagated the address of a decl into the
+ object slot. */
+ /* ??? Should perhaps do this in fold proper. However, doing it
+ there requires that we create a new CALL_EXPR, and that requires
+ copying EH region info to the new node. Easier to just do it
+ here where we can just smash the call operand. */
+ /* ??? Is there a good reason not to do this in fold_stmt_inplace? */
+ callee = gimple_call_fn (stmt);
+ if (TREE_CODE (callee) == OBJ_TYPE_REF
+ && lang_hooks.fold_obj_type_ref
+ && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
+ && DECL_P (TREE_OPERAND
+ (OBJ_TYPE_REF_OBJECT (callee), 0)))
+ {
+ tree t;
+
+ /* ??? Caution: Broken ADDR_EXPR semantics means that
+ looking at the type of the operand of the addr_expr
+ can yield an array type. See silly exception in
+ check_pointer_types_r. */
+ t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
+ t = lang_hooks.fold_obj_type_ref (callee, t);
+ if (t)
+ {
+ gimple_call_set_fn (stmt, t);
+ return true;
+ }
+ }
+ }
+
+ return false;
+}