From patchwork Mon Aug 18 02:00:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 380668 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C8293140129 for ; Mon, 18 Aug 2014 12:01:46 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=VKn8pbloF1uf 1ZCgfUsnM2jDCB8xkydBduCvwDAhXCfFHx4HWTaY0ljcX+OLQAFVi8J4C9ONosZW J4xAH9KH9l1EKj94tfE8ofIp0Yds1lpDhbf7uq07LxRnzzGd/0nd7ylMq4pBMnNp 0bzQuCD77gkVc2Anf1zD+PIj4k1Kr0M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=LLz2ZhaIKkYuWCYzoj Vm2J7ZK2o=; b=womUyRDgGxx/aJ4OGqUt4WGxtXpUpyBGPP1rWttB69MAHvaOcC O6ZsclLPK6w3VojqhwNf+VwaX47hss0ropZvropK0EGKFFhEi9bm5BpqeHRwczDB 9ujRDEacLvkkkf3ZTXFoCFqoZQgvNUBjz52QRbB0Q41OVbWLJPLJbogv4= Received: (qmail 28456 invoked by alias); 18 Aug 2014 02:01:36 -0000 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 Received: (qmail 26747 invoked by uid 89); 18 Aug 2014 02:01:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-qa0-f45.google.com Received: from mail-qa0-f45.google.com (HELO mail-qa0-f45.google.com) (209.85.216.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 18 Aug 2014 02:01:23 +0000 Received: by mail-qa0-f45.google.com with SMTP id cm18so3866924qab.32 for ; Sun, 17 Aug 2014 19:01:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=rj3oZfJ6K3rGkGyajZPacPjR4eg95J9GTWkoZG7OoBg=; b=BSfKKjgEEYOyNY9WbSQQ6BwJDNGwX/SNzPdbRqNwcRFCkriC7YweefLUM5CNYTbN21 3JZPkwUXALQh1Y8H59AwCDEV+hiEQOmO2IzTu94a06xs/RjgMxnCpOfe6Ke2SJ5NiXZA M8pEBBEC0x+zEkzM1jid9q1swO81JIgol6FLgU93xtz4aZvigADTkogm5nMJEx+KNsvV 6Nl+Z2bwhiCSJOqCMcC5Vmxwb3o4E+HOpFQYH96MJeW1p5Di9pXHXbe9vJFISuE0n6Xb G/4CPt9jLYVXG13KSZqC+b3ZMS/F2PIDNTtTJURLGuKy7idgIikbe1U+itSOXQe+V+n2 Sl0A== X-Gm-Message-State: ALoCoQm+SHAFwHKZH01cMSKygWUAUCf7DC4NigyUwoyBXT29bnd1aWHzEHeQBXu4kAlPkLFzz6yO X-Received: by 10.140.85.135 with SMTP id n7mr48230952qgd.22.1408327279499; Sun, 17 Aug 2014 19:01:19 -0700 (PDT) Received: from localhost.localdomain (ool-4353af5c.dyn.optonline.net. [67.83.175.92]) by mx.google.com with ESMTPSA id b19sm27286494qaw.44.2014.08.17.19.01.16 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 17 Aug 2014 19:01:18 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: joseph@codesourcery.com, jason@redhat.com, Patrick Palka Subject: [PATCH] Fix promotion of const local arrays to static storage Date: Sun, 17 Aug 2014 22:00:59 -0400 Message-Id: <1408327259-23386-1-git-send-email-patrick@parcs.ath.cx> Hi, The fix for PR38615 indirectly broke the promotion of const local arrays to static storage in many cases. The commit in question, r143570, made it so that only arrays that don't potentially escape from the scope in which they're defined (i.e. arrays for which TREE_ADDRESSABLE is 0) are candidates for the promotion to static storage. The problem is that both the C and C++ frontends contain ancient code (dating back to 1994) that sets the TREE_ADDRESSABLE bit on arrays indexed by non-constant or out-of-range indices. As a result, const arrays that are indexed by non-constant or out-of-range values are no longer candidates for promotion to static storage following the fix to PR38615, because their TREE_ADDRESSABLE bit will always be set. Consequently, array promotion is essentially broken for a large class of C and C++ code. E.g. this simple example is no longer a candidate for promotion: int foo (int i) { const int x[] = { 1, 2, 3 }; return x[i]; /* non-constant index */ } This patch removes the ancient code that is responsible for dubiously setting the TREE_ADDRESSABLE bit on arrays indexed by non-constant or out-of-range values. I don't think that this code is necessary or useful anymore. Bootstrapped and regtested on x86_64-unknown-linux-gnu, OK for trunk? 2014-08-18 Patrick Palka * c-typeck.c (build_array_ref): Don't mark arrays indexed by non-constant or out-of-range values as addressable. 2014-08-18 Patrick Palka * typeck.c (build_array_ref): Don't mark arrays indexed by non-constant or out-of-range values as addressable. 2014-08-18 Patrick Palka * g++.dg/opt/static4.C: Check for static promotion. * g++.dg/opt/static7.C: New test. * gcc.dg/static1.c: New test. --- gcc/c/c-typeck.c | 23 ----------------------- gcc/cp/typeck.c | 25 ------------------------- gcc/testsuite/g++.dg/opt/static4.C | 4 ++++ gcc/testsuite/g++.dg/opt/static7.C | 12 ++++++++++++ gcc/testsuite/gcc.dg/static1.c | 12 ++++++++++++ 5 files changed, 28 insertions(+), 48 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/static7.C create mode 100644 gcc/testsuite/gcc.dg/static1.c diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index e6745be..022ff8d 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -2487,29 +2487,6 @@ build_array_ref (location_t loc, tree array, tree index) { tree rval, type; - /* An array that is indexed by a non-constant - cannot be stored in a register; we must be able to do - address arithmetic on its address. - Likewise an array of elements of variable size. */ - if (TREE_CODE (index) != INTEGER_CST - || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) - && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) - { - if (!c_mark_addressable (array)) - return error_mark_node; - } - /* An array that is indexed by a constant value which is not within - the array bounds cannot be stored in a register either; because we - would get a crash in store_bit_field/extract_bit_field when trying - to access a non-existent part of the register. */ - if (TREE_CODE (index) == INTEGER_CST - && TYPE_DOMAIN (TREE_TYPE (array)) - && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) - { - if (!c_mark_addressable (array)) - return error_mark_node; - } - if (pedantic || warn_c90_c99_compat) { tree foo = array; diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index fa3283d..6dd056d 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3112,31 +3112,6 @@ cp_build_array_ref (location_t loc, tree array, tree idx, pointer arithmetic.) */ idx = cp_perform_integral_promotions (idx, complain); - /* An array that is indexed by a non-constant - cannot be stored in a register; we must be able to do - address arithmetic on its address. - Likewise an array of elements of variable size. */ - if (TREE_CODE (idx) != INTEGER_CST - || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) - && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) - != INTEGER_CST))) - { - if (!cxx_mark_addressable (array)) - return error_mark_node; - } - - /* An array that is indexed by a constant value which is not within - the array bounds cannot be stored in a register either; because we - would get a crash in store_bit_field/extract_bit_field when trying - to access a non-existent part of the register. */ - if (TREE_CODE (idx) == INTEGER_CST - && TYPE_DOMAIN (TREE_TYPE (array)) - && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) - { - if (!cxx_mark_addressable (array)) - return error_mark_node; - } - if (!lvalue_p (array)) { if (complain & tf_error) diff --git a/gcc/testsuite/g++.dg/opt/static4.C b/gcc/testsuite/g++.dg/opt/static4.C index 87e11b0..bae22c9 100644 --- a/gcc/testsuite/g++.dg/opt/static4.C +++ b/gcc/testsuite/g++.dg/opt/static4.C @@ -3,6 +3,7 @@ // if they are promoted to static storage. // { dg-do compile } +// { dg-options "-fdump-tree-gimple" } int g(int i) { if (i<1) { @@ -13,3 +14,6 @@ int g(int i) { return x[i]; } } + +// { dg-final { scan-tree-dump-times "static const int" 2 "gimple" } } +// { dg-final { cleanup-tree-dump "gimple" } } diff --git a/gcc/testsuite/g++.dg/opt/static7.C b/gcc/testsuite/g++.dg/opt/static7.C new file mode 100644 index 0000000..2a9f678 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/static7.C @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-gimple" } */ + +int +foo (int x) +{ + const int y[] = { 1, 2, 3 }; + return y[x]; +} + +/* { dg-final { scan-tree-dump "static const int" "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */ diff --git a/gcc/testsuite/gcc.dg/static1.c b/gcc/testsuite/gcc.dg/static1.c new file mode 100644 index 0000000..2a9f678 --- /dev/null +++ b/gcc/testsuite/gcc.dg/static1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-gimple" } */ + +int +foo (int x) +{ + const int y[] = { 1, 2, 3 }; + return y[x]; +} + +/* { dg-final { scan-tree-dump "static const int" "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */