From patchwork Wed Jul 27 10:53:06 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 107023 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 5433FB6F76 for ; Wed, 27 Jul 2011 20:53:35 +1000 (EST) Received: (qmail 18334 invoked by alias); 27 Jul 2011 10:53:33 -0000 Received: (qmail 18323 invoked by uid 22791); 27 Jul 2011 10:53:32 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_FAIL X-Spam-Check-By: sourceware.org Received: from smtp-vbr12.xs4all.nl (HELO smtp-vbr12.xs4all.nl) (194.109.24.32) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 27 Jul 2011 10:53:18 +0000 Received: from [192.168.0.101] ([89.42.210.59]) (authenticated bits=0) by smtp-vbr12.xs4all.nl (8.13.8/8.13.8) with ESMTP id p6RArBld071399 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 27 Jul 2011 12:53:16 +0200 (CEST) (envelope-from vries@codesourcery.com) Message-ID: <4E2FEE12.9040808@codesourcery.com> Date: Wed, 27 Jul 2011 13:53:06 +0300 From: Tom de Vries User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11 MIME-Version: 1.0 To: rguenther@suse.de CC: "gcc-patches@gcc.gnu.org" Subject: [PATCH PR43513, 1/3] Replace vla with array - Implementation. References: <4E2FED87.8020300@codesourcery.com> In-Reply-To: <4E2FED87.8020300@codesourcery.com> Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 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; }