- return NULL_TREE;
-}
-
-
-/* Return the string length, maximum string length or maximum value of
- ARG in LENGTH.
- If ARG is an SSA name variable, follow its use-def chains. If LENGTH
- is not NULL and, for TYPE == 0, its value is not equal to the length
- we determine or if we are unable to determine the length or value,
- return false. VISITED is a bitmap of visited variables.
- TYPE is 0 if string length should be returned, 1 for maximum string
- length and 2 for maximum value ARG can have. */
-
-static bool
-get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
-{
- tree var, def_stmt, val;
-
- if (TREE_CODE (arg) != SSA_NAME)
- {
- if (TREE_CODE (arg) == COND_EXPR)
- return get_maxval_strlen (COND_EXPR_THEN (arg), length, visited, type)
- && get_maxval_strlen (COND_EXPR_ELSE (arg), length, visited, type);
- /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
- else if (TREE_CODE (arg) == ADDR_EXPR
- && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
- && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
- {
- tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
- if (TREE_CODE (aop0) == INDIRECT_REF
- && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
- return get_maxval_strlen (TREE_OPERAND (aop0, 0),
- length, visited, type);
- }
-
- if (type == 2)
- {
- val = arg;
- if (TREE_CODE (val) != INTEGER_CST
- || tree_int_cst_sgn (val) < 0)
- return false;
- }
- else
- val = c_strlen (arg, 1);
- if (!val)
- return false;
-
- if (*length)
- {
- if (type > 0)
- {
- if (TREE_CODE (*length) != INTEGER_CST
- || TREE_CODE (val) != INTEGER_CST)
- return false;
-
- if (tree_int_cst_lt (*length, val))
- *length = val;
- return true;
- }
- else if (simple_cst_equal (val, *length) != 1)
- return false;
- }
-
- *length = val;
- return true;
- }
-
- /* If we were already here, break the infinite cycle. */
- if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
- return true;
- bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
-
- var = arg;
- def_stmt = SSA_NAME_DEF_STMT (var);
-
- switch (TREE_CODE (def_stmt))
- {
- case GIMPLE_MODIFY_STMT:
- {
- tree rhs;
-
- /* The RHS of the statement defining VAR must either have a
- constant length or come from another SSA_NAME with a constant
- length. */
- rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
- STRIP_NOPS (rhs);
- return get_maxval_strlen (rhs, length, visited, type);
- }
-
- case PHI_NODE:
- {
- /* All the arguments of the PHI node must have the same constant
- length. */
- int i;
-
- for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
- {
- tree arg = PHI_ARG_DEF (def_stmt, i);
-
- /* If this PHI has itself as an argument, we cannot
- determine the string length of this argument. However,
- if we can find a constant string length for the other
- PHI args then we can still be sure that this is a
- constant string length. So be optimistic and just
- continue with the next argument. */
- if (arg == PHI_RESULT (def_stmt))
- continue;
-
- if (!get_maxval_strlen (arg, length, visited, type))
- return false;
- }
-
- return true;
- }
-
- default:
- break;
- }
-
-
- return false;
-}
-
-
-/* Fold builtin call FN in statement STMT. If it cannot be folded into a
- constant, return NULL_TREE. Otherwise, return its constant value. */
-
-static tree
-ccp_fold_builtin (tree stmt, tree fn)
-{
- tree result, val[3];
- tree callee, a;
- int arg_mask, i, type;
- bitmap visited;
- bool ignore;
- call_expr_arg_iterator iter;
- int nargs;
-
- ignore = TREE_CODE (stmt) != GIMPLE_MODIFY_STMT;
-
- /* First try the generic builtin folder. If that succeeds, return the
- result directly. */
- result = fold_call_expr (fn, ignore);
- if (result)
- {
- if (ignore)
- STRIP_NOPS (result);
- return result;
- }
-
- /* Ignore MD builtins. */
- callee = get_callee_fndecl (fn);
- if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
- return NULL_TREE;
-
- /* If the builtin could not be folded, and it has no argument list,
- we're done. */
- nargs = call_expr_nargs (fn);
- if (nargs == 0)
- return NULL_TREE;
-
- /* Limit the work only for builtins we know how to simplify. */
- switch (DECL_FUNCTION_CODE (callee))
- {
- case BUILT_IN_STRLEN:
- case BUILT_IN_FPUTS:
- case BUILT_IN_FPUTS_UNLOCKED:
- arg_mask = 1;
- type = 0;
- break;
- case BUILT_IN_STRCPY:
- case BUILT_IN_STRNCPY:
- arg_mask = 2;
- type = 0;
- break;
- case BUILT_IN_MEMCPY_CHK:
- case BUILT_IN_MEMPCPY_CHK:
- case BUILT_IN_MEMMOVE_CHK:
- case BUILT_IN_MEMSET_CHK:
- case BUILT_IN_STRNCPY_CHK:
- arg_mask = 4;
- type = 2;
- break;
- case BUILT_IN_STRCPY_CHK:
- case BUILT_IN_STPCPY_CHK:
- arg_mask = 2;
- type = 1;
- break;
- case BUILT_IN_SNPRINTF_CHK:
- case BUILT_IN_VSNPRINTF_CHK:
- arg_mask = 2;
- type = 2;
- break;
- default:
- return NULL_TREE;
- }
-
- /* Try to use the dataflow information gathered by the CCP process. */
- visited = BITMAP_ALLOC (NULL);
-
- memset (val, 0, sizeof (val));
- init_call_expr_arg_iterator (fn, &iter);
- for (i = 0; arg_mask; i++, arg_mask >>= 1)
- {
- a = next_call_expr_arg (&iter);
- if (arg_mask & 1)
- {
- bitmap_clear (visited);
- if (!get_maxval_strlen (a, &val[i], visited, type))
- val[i] = NULL_TREE;
- }
- }
-
- BITMAP_FREE (visited);
-
- result = NULL_TREE;
- switch (DECL_FUNCTION_CODE (callee))
- {
- case BUILT_IN_STRLEN:
- if (val[0])
- {
- tree new_val = fold_convert (TREE_TYPE (fn), val[0]);
-
- /* If the result is not a valid gimple value, or not a cast
- of a valid gimple value, then we can not use the result. */
- if (is_gimple_val (new_val)
- || (is_gimple_cast (new_val)
- && is_gimple_val (TREE_OPERAND (new_val, 0))))
- return new_val;
- }
- break;
-
- case BUILT_IN_STRCPY:
- if (val[1] && is_gimple_val (val[1]) && nargs == 2)
- result = fold_builtin_strcpy (callee,
- CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- val[1]);
- break;
-
- case BUILT_IN_STRNCPY:
- if (val[1] && is_gimple_val (val[1]) && nargs == 3)
- result = fold_builtin_strncpy (callee,
- CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- CALL_EXPR_ARG (fn, 2),
- val[1]);
- break;
-
- case BUILT_IN_FPUTS:
- result = fold_builtin_fputs (CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- TREE_CODE (stmt) != GIMPLE_MODIFY_STMT, 0,
- val[0]);
- break;
-
- case BUILT_IN_FPUTS_UNLOCKED:
- result = fold_builtin_fputs (CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- TREE_CODE (stmt) != GIMPLE_MODIFY_STMT, 1,
- val[0]);
- break;
-
- case BUILT_IN_MEMCPY_CHK:
- case BUILT_IN_MEMPCPY_CHK:
- case BUILT_IN_MEMMOVE_CHK:
- case BUILT_IN_MEMSET_CHK:
- if (val[2] && is_gimple_val (val[2]))
- result = fold_builtin_memory_chk (callee,
- CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- CALL_EXPR_ARG (fn, 2),
- CALL_EXPR_ARG (fn, 3),
- val[2], ignore,
- DECL_FUNCTION_CODE (callee));
- break;
-
- case BUILT_IN_STRCPY_CHK:
- case BUILT_IN_STPCPY_CHK:
- if (val[1] && is_gimple_val (val[1]))
- result = fold_builtin_stxcpy_chk (callee,
- CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- CALL_EXPR_ARG (fn, 2),
- val[1], ignore,
- DECL_FUNCTION_CODE (callee));
- break;
-
- case BUILT_IN_STRNCPY_CHK:
- if (val[2] && is_gimple_val (val[2]))
- result = fold_builtin_strncpy_chk (CALL_EXPR_ARG (fn, 0),
- CALL_EXPR_ARG (fn, 1),
- CALL_EXPR_ARG (fn, 2),
- CALL_EXPR_ARG (fn, 3),
- val[2]);
- break;
-
- case BUILT_IN_SNPRINTF_CHK:
- case BUILT_IN_VSNPRINTF_CHK:
- if (val[1] && is_gimple_val (val[1]))
- result = fold_builtin_snprintf_chk (fn, val[1],
- DECL_FUNCTION_CODE (callee));
- break;
-
- default:
- gcc_unreachable ();
- }
-
- if (result && ignore)
- result = fold_ignored_result (result);
- return result;
-}
-
-
-/* Fold the statement pointed to by STMT_P. In some cases, this function may
- replace the whole statement with a new one. Returns true iff folding
- makes any changes. */
-
-bool
-fold_stmt (tree *stmt_p)
-{
- tree rhs, result, stmt;
- struct fold_stmt_r_data fold_stmt_r_data;
- bool changed = false;
- bool inside_addr_expr = false;
-
- stmt = *stmt_p;
-
- fold_stmt_r_data.stmt = stmt;
- fold_stmt_r_data.changed_p = &changed;
- fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
-
- /* If we replaced constants and the statement makes pointer dereferences,
- then we may need to fold instances of *&VAR into VAR, etc. */
- if (walk_tree (stmt_p, fold_stmt_r, &fold_stmt_r_data, NULL))
- {
- *stmt_p = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
- return true;
- }
-
- rhs = get_rhs (stmt);
- if (!rhs)
- return changed;
- result = NULL_TREE;
-
- if (TREE_CODE (rhs) == CALL_EXPR)
- {
- tree callee;
-
- /* Check for builtins that CCP can handle using information not
- available in the generic fold routines. */
- callee = get_callee_fndecl (rhs);
- if (callee && DECL_BUILT_IN (callee))
- result = ccp_fold_builtin (stmt, rhs);
- 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. Also
- CALL_EXPR_RETURN_SLOT_OPT needs to be handled correctly and
- copied, fold_call_expr does not have not information. */
- callee = CALL_EXPR_FN (rhs);
- 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)
- {
- CALL_EXPR_FN (rhs) = t;
- changed = true;
- }
- }
- }
- }
- else 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)
- changed |= set_rhs (stmt_p, result);
-
- return changed;
-}
-
-/* Perform the minimal folding on statement STMT. Only operations like
- *&x created by constant propagation are handled. The statement cannot
- be replaced with a new one. */
-
-bool
-fold_stmt_inplace (tree stmt)
-{
- tree old_stmt = stmt, rhs, new_rhs;
- struct fold_stmt_r_data fold_stmt_r_data;
- bool changed = false;
- bool inside_addr_expr = false;
-
- fold_stmt_r_data.stmt = stmt;
- fold_stmt_r_data.changed_p = &changed;
- fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
-
- walk_tree (&stmt, fold_stmt_r, &fold_stmt_r_data, NULL);
- gcc_assert (stmt == old_stmt);
-
- rhs = get_rhs (stmt);
- if (!rhs || rhs == stmt)
- return changed;
-
- new_rhs = fold (rhs);
- STRIP_USELESS_TYPE_CONVERSION (new_rhs);
- if (new_rhs == rhs)
- return changed;
-
- changed |= set_rhs (&stmt, new_rhs);
- gcc_assert (stmt == old_stmt);
-
- return changed;
-}
-\f
-/* Try to optimize out __builtin_stack_restore. Optimize it out
- if there is another __builtin_stack_restore in the same basic
- block and no calls or ASM_EXPRs are in between, or if this block's
- only outgoing edge is to EXIT_BLOCK and there are no calls or
- ASM_EXPRs after this __builtin_stack_restore. */
-
-static tree
-optimize_stack_restore (basic_block bb, tree call, block_stmt_iterator i)
-{
- tree stack_save, stmt, callee;
-
- if (TREE_CODE (call) != CALL_EXPR
- || call_expr_nargs (call) != 1
- || TREE_CODE (CALL_EXPR_ARG (call, 0)) != SSA_NAME
- || !POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (call, 0))))
- return NULL_TREE;
-
- for (bsi_next (&i); !bsi_end_p (i); bsi_next (&i))
- {
- tree call;
-
- stmt = bsi_stmt (i);
- if (TREE_CODE (stmt) == ASM_EXPR)
- return NULL_TREE;
- call = get_call_expr_in (stmt);
- if (call == NULL)
- continue;
-
- callee = get_callee_fndecl (call);
- if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
- return NULL_TREE;
-
- if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
- break;
- }
-
- if (bsi_end_p (i)
- && (! single_succ_p (bb)
- || single_succ_edge (bb)->dest != EXIT_BLOCK_PTR))
- return NULL_TREE;
-
- stack_save = SSA_NAME_DEF_STMT (CALL_EXPR_ARG (call, 0));
- if (TREE_CODE (stack_save) != GIMPLE_MODIFY_STMT
- || GIMPLE_STMT_OPERAND (stack_save, 0) != CALL_EXPR_ARG (call, 0)
- || TREE_CODE (GIMPLE_STMT_OPERAND (stack_save, 1)) != CALL_EXPR
- || tree_could_throw_p (stack_save)
- || !has_single_use (CALL_EXPR_ARG (call, 0)))
- return NULL_TREE;
-
- callee = get_callee_fndecl (GIMPLE_STMT_OPERAND (stack_save, 1));
- if (!callee
- || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
- || DECL_FUNCTION_CODE (callee) != BUILT_IN_STACK_SAVE
- || call_expr_nargs (GIMPLE_STMT_OPERAND (stack_save, 1)) != 0)
- return NULL_TREE;
-
- stmt = stack_save;
- push_stmt_changes (&stmt);
- if (!set_rhs (&stmt,
- build_int_cst (TREE_TYPE (CALL_EXPR_ARG (call, 0)), 0)))
- {
- discard_stmt_changes (&stmt);
- return NULL_TREE;
- }
- gcc_assert (stmt == stack_save);
- pop_stmt_changes (&stmt);
-