From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11688 invoked by alias); 7 Dec 2015 04:56:22 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 11672 invoked by uid 89); 7 Dec 2015 04:56:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-oi0-f54.google.com Received: from mail-oi0-f54.google.com (HELO mail-oi0-f54.google.com) (209.85.218.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 07 Dec 2015 04:56:19 +0000 Received: by oiww189 with SMTP id w189so93388206oiw.3 for ; Sun, 06 Dec 2015 20:56:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=pILb/YBDPLSMFdWaCUhmwk2/gBhyQLu+xkqaP9p/GPI=; b=K5UbMAbTs434QMuX42pxSZP8BBsnalNkc+9f/fDFfu/GX9XzHDTyXAO9QCcM48yqDI s/mKLnGuX3ZwmkU4bSxKLrkZW/ALNiqUDTH3sS2Q9wKL388RrVTshZ9BRDILGOikfqii HgojI00vDvMK1Ew5XjeQmoIB0BOo3GFdJWtxuigm8XNs6aOGERvYmFn8W0lZYqfUiSZ/ 5bzaCS2yr9F9BmVn/0mKAAZ1sDMhk/75FEuu3z5tMgM4BIkuqcl655DmIb+fkRv23/3q K9jkcWszuEWjl8P9CN+wDM311M+H/XvWDZ3R99guX3hDffBu4lPwyWGjGROQSV38ibPr csgw== X-Gm-Message-State: ALoCoQlWU8fjdD+jQUsaRAUTBLN16EuY8+K3zwTZugXDuM4sV7h7xHrtwCia2CpDrOsgsyMvI8a5 X-Received: by 10.202.188.66 with SMTP id m63mr20251764oif.127.1449464177557; Sun, 06 Dec 2015 20:56:17 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.28.234 with HTTP; Sun, 6 Dec 2015 20:55:58 -0800 (PST) In-Reply-To: <1449463815-4150-1-git-send-email-patrick@parcs.ath.cx> References: <1449463815-4150-1-git-send-email-patrick@parcs.ath.cx> From: Patrick Palka Date: Mon, 07 Dec 2015 04:56:00 -0000 Message-ID: Subject: Re: [PATCH] [C FE] Fold trivial exprs that refer to const vars To: GCC Patches Cc: "Joseph S. Myers" , Patrick Palka Content-Type: text/plain; charset=UTF-8 X-SW-Source: 2015-12/txt/msg00691.txt.bz2 On Sun, Dec 6, 2015 at 11:50 PM, Patrick Palka wrote: > There is a minor inconsistency in the folding behavior within the C > frontend. The C frontend does not currently fold the expression "x", > where x is a const int, yet the FE does fold the expression "x + 0". > > This happens because decl_constant_value is called in c_fully_fold only > while recursing over the operands of the expression being folded, i.e. > there is no top-level call to decl_constant_value to handle the case > where the expression being folded happens to be a singular expression > such as "x", as opposed to "x + 5" (where x is a const variable). > > To fix this inconsistency, this patch calls decl_constant_value in > c_fully fold after folding the given expression. > > Bootstrap + regtest in progress on x86_64-pc-linux-gnu, OK to commit if > testing succeeds? > > gcc/c/ChangeLog: > > * c-fold.c (c_fully_fold): Call > decl_constant_value_for_optimization after folding > the given expression. > * c-typeck.c (digest_init): Remove redundant call to > decl_constant_value_for_optimization. > > gcc/testsuite/ChangeLog: > > * gcc.dg/fold-const-1.c: New test. > --- > gcc/c/c-fold.c | 1 + > gcc/c/c-typeck.c | 1 - > gcc/testsuite/gcc.dg/fold-const-1.c | 23 +++++++++++++++++++++++ > 3 files changed, 24 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.dg/fold-const-1.c > > diff --git a/gcc/c/c-fold.c b/gcc/c/c-fold.c > index c554e17..ab0b37f 100644 > --- a/gcc/c/c-fold.c > +++ b/gcc/c/c-fold.c > @@ -88,6 +88,7 @@ c_fully_fold (tree expr, bool in_init, bool *maybe_const) > } > ret = c_fully_fold_internal (expr, in_init, maybe_const, > &maybe_const_itself, false); > + ret = decl_constant_value_for_optimization (ret); > if (eptype) > ret = fold_convert_loc (loc, eptype, ret); > *maybe_const &= maybe_const_itself; > diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c > index b691072..4886fc2 100644 > --- a/gcc/c/c-typeck.c > +++ b/gcc/c/c-typeck.c > @@ -6791,7 +6791,6 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, > inside_init = TREE_OPERAND (inside_init, 0); > } > inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); > - inside_init = decl_constant_value_for_optimization (inside_init); > > /* Initialization of an array of chars from a string constant > optionally enclosed in braces. */ > diff --git a/gcc/testsuite/gcc.dg/fold-const-1.c b/gcc/testsuite/gcc.dg/fold-const-1.c > new file mode 100644 > index 0000000..9f043b8 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/fold-const-1.c > @@ -0,0 +1,23 @@ > +/* { dg-options "-O -fdump-tree-gimple -fdump-tree-ccp1" } */ > + > +extern void dummy (const void *); > + > +int > +f1 (void) > +{ > + const int x = 7; > + dummy (&x); > + return x; > +} > + > +void foo (int); > + > +void > +f2 (void) > +{ > + const int x = 7; > + foo (x); > +} > + > +/* { dg-final { scan-tree-dump "foo \\(7\\);" "gimple" } } */ > +/* { dg-final { scan-tree-dump-times "return 7;" 2 "ccp1" } } */ Oops, this last dg-final line should say { scan-tree-dump "return 7;" "ccp1" } of course.