On 07/27/2011 01:50 PM, Tom de Vries wrote: > Hi Richard, > > I have a patch set for bug 43513 - The stack pointer is adjusted twice. > > 01_pr43513.3.patch > 02_pr43513.3.test.patch > 03_pr43513.3.mudflap.patch > > The patch set has been bootstrapped and reg-tested on x86_64. > > I will sent out the patches individually. >
The patch replaces a vla __builtin_alloca that has a constant argument with an array declaration. OK for trunk? Thanks, - Tom 2011-07-27 Tom de Vries <t...@codesourcery.com> PR middle-end/43513 * builtins.c (fold_builtin_alloca): New function. * tree.h (fold_builtin_alloca): Declare. * gimple-fold.c (gimple_fold_builtin): Use fold_builtin_alloca.
Index: gcc/builtins.c =================================================================== --- gcc/builtins.c (revision 175801) +++ gcc/builtins.c (working copy) @@ -6193,6 +6193,43 @@ builtin_mathfn_code (const_tree t) return DECL_FUNCTION_CODE (fndecl); } +/* Detects a vla-related alloca with a constant argument. Declares fixed-size + array and return the address, if found, otherwise returns NULL_TREE. */ + +tree +fold_builtin_alloca (tree lhs, tree arg) +{ + unsigned HOST_WIDE_INT size, n_elem, elem_size; + tree var_type, vla_type, elem_type, array_type; + + if (lhs == NULL_TREE) + return NULL_TREE; + + /* Detect constant argument. */ + if (TREE_CODE (arg) != INTEGER_CST || !host_integerp (arg, 1)) + return NULL_TREE; + size = TREE_INT_CST_LOW (arg); + + /* Detect a vla. */ + var_type = TREE_TYPE (SSA_NAME_VAR (lhs)); + if (TREE_CODE (var_type) != POINTER_TYPE) + return NULL_TREE; + vla_type = TREE_TYPE (var_type); + if (TREE_CODE (vla_type) != ARRAY_TYPE) + return NULL_TREE; + if (TREE_CODE (TYPE_MAXVAL (TYPE_DOMAIN (vla_type))) == INTEGER_CST) + return NULL_TREE; + elem_type = TREE_TYPE (vla_type); + if (TREE_CODE (TYPE_SIZE_UNIT (elem_type)) != INTEGER_CST) + return NULL_TREE; + + /* Declare a fixed-size array and return the address instead. */ + elem_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (elem_type)); + n_elem = size / elem_size; + array_type = build_array_type_nelts (elem_type, n_elem); + return build_fold_addr_expr (create_tmp_var (array_type, "vla_cst")); +} + /* Fold a call to __builtin_constant_p, if we know its argument ARG will evaluate to a constant. */ Index: gcc/tree.h =================================================================== --- gcc/tree.h (revision 175801) +++ gcc/tree.h (working copy) @@ -5321,6 +5321,7 @@ truth_value_p (enum tree_code code) /* In builtins.c */ extern tree fold_call_expr (location_t, tree, bool); +extern tree fold_builtin_alloca (tree, tree); extern tree fold_builtin_fputs (location_t, tree, tree, bool, bool, tree); extern tree fold_builtin_strcpy (location_t, tree, tree, tree, tree); extern tree fold_builtin_strncpy (location_t, tree, tree, tree, tree, tree); Index: gcc/gimple-fold.c =================================================================== --- gcc/gimple-fold.c (revision 175801) +++ gcc/gimple-fold.c (working copy) @@ -1246,6 +1246,9 @@ gimple_fold_builtin (gimple stmt) arg_idx = 1; type = 2; break; + case BUILT_IN_ALLOCA: + return fold_builtin_alloca (gimple_call_lhs (stmt), + gimple_call_arg (stmt, 0)); default: return NULL_TREE; }