From patchwork Mon Dec 7 04:50:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 553262 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 3F9AD140271 for ; Mon, 7 Dec 2015 15:51:55 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=nj7xkfvX; dkim-atps=neutral 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=mWPsZ4mVAmA5 cCEnsx6mjzQlonguXtMos7onanF6aQg14830cWLXcqOtTOPkGUzd5msGrUMsTp8G Bo2Ypl4KIz+SmHVoYHfBtvg3SnB8113NDEuLw8m9b2YKcZAp6U54axkp90pHgdyb Eb0Unc9MwO9IPoSiW32pbwPpgkV+ClU= 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=ISr0aBUJwGvzWRqCEV H3XxUg7fw=; b=nj7xkfvXP6N9mMgksZ32u+H7se5RwQee1BayBzNE52cAK27ZBX YvUr0J83cg5GE0aXw0G3E8Lq6QmeBSCOtjma+ROrV6C0VyGoTgIEYLWWB8C/ksVd i5g9uqr6HAHvNUeGoE2ywKHrf+7s6H/559SIKvJXx1J6mZqx4SsqZdzcE= Received: (qmail 130763 invoked by alias); 7 Dec 2015 04:51:44 -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 130478 invoked by uid 89); 7 Dec 2015 04:50:40 -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-qg0-f50.google.com Received: from mail-qg0-f50.google.com (HELO mail-qg0-f50.google.com) (209.85.192.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 07 Dec 2015 04:50:39 +0000 Received: by qgec40 with SMTP id c40so135338348qge.2 for ; Sun, 06 Dec 2015 20:50:36 -0800 (PST) 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=0NjrFnOOv3kvY4FJBEAriqTJaSncPo1u1LSvo02v5Ic=; b=TpGBOnXux8W0TzQIQtR+jmnQC7fZksqEq5JG4ougXTrc39dJiImvYdrT20cnIimVHJ Q+iBQi5UxkiFVhzpFyYrqjlK0uvAKx5H2d2Pax4wwzhVAnq3dRN8UxurBmXMC0UD1xtu w91vJGVTdR0rqAkeNftyslQl6RZ3cGP6PdAjZwBm2dEcun0OiqQ8Iqmm0vcyZk9tbMH/ 2MdHSxVAw2g7fzLA1BXQzC9oX24+Jsp6NYZ0aaxkEjn8Cf99Jvl1wQu8oVxLC99ygDIL K5TilSVhATboep+Uua4I3LJM9JpP9MNRLVLDvhtvEwBzZc1XKJJvRWrg7SByY/3oK2IW 4CAw== X-Gm-Message-State: ALoCoQme5Rn1YIM1vN3OhybYjv06c7jgCHIw0X9t1pLB8QkaQox/XPPmL8KZqmdCs8haMCPsMSjp X-Received: by 10.140.104.70 with SMTP id z64mr33755913qge.102.1449463836840; Sun, 06 Dec 2015 20:50:36 -0800 (PST) Received: from localhost.localdomain (ool-4353a8be.dyn.optonline.net. [67.83.168.190]) by smtp.gmail.com with ESMTPSA id d3sm9031554qkj.16.2015.12.06.20.50.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 06 Dec 2015 20:50:36 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: joseph@codesourcery.com, Patrick Palka Subject: [PATCH] [C FE] Fold trivial exprs that refer to const vars Date: Sun, 6 Dec 2015 23:50:15 -0500 Message-Id: <1449463815-4150-1-git-send-email-patrick@parcs.ath.cx> 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" } } */