From patchwork Tue Nov 11 13:48:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 409454 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 D7AAC14017A for ; Wed, 12 Nov 2014 00:48:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=I/mp8Dxm/GvUnNEmpm vNx9LTqCBvjZBTOm4f32VipAm1ZfbbaUWJ8RIY7AUQOGKF6DMtndw+jVxTcx1tSD rT51OhhF4DZfibAAES6Z5Q5bz5h+Pyd2MSotEeCZcPP7j0rH0ktTEcFe/fETlIsZ SpNLh2gU9ZF1NtZ6v1bhpDMvs= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=0zRtMlwq+GkdQOacdD6U+7pv UWs=; b=NUWt5Wg398dc9lrbk2MevaK4c5jWCwaoAHNvSiBtfkv+O+FqFAZ1jeVS d6y/IqzFpoN/B4Vay660VrN5To/8ZtgIiFB2S7Gn6G8Vuah160OpqCnVKAek3zZC BHg8Gk1ctFFVSnWm2sUadV/p2Qq1a18LQxDc7NkxbPs4n+GPvCM= Received: (qmail 14841 invoked by alias); 11 Nov 2014 13:48:18 -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 14830 invoked by uid 89); 11 Nov 2014 13:48:17 -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, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-oi0-f46.google.com Received: from mail-oi0-f46.google.com (HELO mail-oi0-f46.google.com) (209.85.218.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 11 Nov 2014 13:48:14 +0000 Received: by mail-oi0-f46.google.com with SMTP id g201so6955167oib.5 for ; Tue, 11 Nov 2014 05:48:12 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.202.86.82 with SMTP id k79mr7791590oib.58.1415713692332; Tue, 11 Nov 2014 05:48:12 -0800 (PST) Received: by 10.76.101.132 with HTTP; Tue, 11 Nov 2014 05:48:12 -0800 (PST) In-Reply-To: <1415707831-652-1-git-send-email-patrick@parcs.ath.cx> References: <1415707831-652-1-git-send-email-patrick@parcs.ath.cx> Date: Tue, 11 Nov 2014 14:48:12 +0100 Message-ID: Subject: Re: [PATCH] Look through widening type conversions for possible edge assertions From: Richard Biener To: Patrick Palka Cc: GCC Patches X-IsSubscribed: yes On Tue, Nov 11, 2014 at 1:10 PM, Patrick Palka wrote: > This patch is a replacement for the 2nd VRP refactoring patch. It > simply teaches VRP to look through widening type conversions when > finding suitable edge assertions, e.g. > > bool p = x != y; > int q = (int) p; > if (q == 0) // new edge assert: p == 0 and therefore x == y I think the proper fix is to forward x != y to q == 0 instead of this one. That said - the tree-ssa-forwprop.c restriction on only forwarding single-uses into conditions is clearly bogus here. I suggest to relax it for conversions and compares. Like with Thanks, Richard. > The new testcase requires that such an edge assertion be inserted. > > Full bootstrap + regtest on x86_64-unknown-linux-gnu in progress. Does > the patch look OK for trunk if no new regressions? > > 2014-11-11 Patrick Palka > > gcc/ > * tree-vrp.c (register_edge_assert_for): Look through > widening type conversions for posible edge assertions. > > gcc/testsuite/ > * gcc.dg/vrp-1.c: New testcase. > --- > gcc/testsuite/gcc.dg/vrp-1.c | 31 +++++++++++++++++++++++++++++++ > gcc/tree-vrp.c | 22 ++++++++++++++++++++++ > 2 files changed, 53 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/vrp-1.c > > diff --git a/gcc/testsuite/gcc.dg/vrp-1.c b/gcc/testsuite/gcc.dg/vrp-1.c > new file mode 100644 > index 0000000..df5334e > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/vrp-1.c > @@ -0,0 +1,31 @@ > +/* { dg-options "-O2" } */ > + > +void runtime_error (void) __attribute__ ((noreturn)); > +void compiletime_error (void) __attribute__ ((noreturn, error (""))); > + > +static void > +compiletime_check_equals_1 (int *x, int y) > +{ > + int __p = *x != y; > + if (__builtin_constant_p (__p) && __p) > + compiletime_error (); > + if (__p) > + runtime_error (); > +} > + > +static void > +compiletime_check_equals_2 (int *x, int y) > +{ > + int __p = *x != y; > + if (__builtin_constant_p (__p) && __p) > + compiletime_error (); /* { dg-error "call to" } */ > + if (__p) > + runtime_error (); > +} > + > +void > +foo (int *x) > +{ > + compiletime_check_equals_1 (x, 5); > + compiletime_check_equals_2 (x, 10); > +} > diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c > index f0a4382..979ab44 100644 > --- a/gcc/tree-vrp.c > +++ b/gcc/tree-vrp.c > @@ -5634,6 +5634,7 @@ register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si, > the value zero or one, then we may be able to assert values > for SSA_NAMEs which flow into COND. */ > > + > /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining > statement of NAME we can assert both operands of the BIT_AND_EXPR > have nonzero value. */ > @@ -5673,6 +5674,27 @@ register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si, > register_edge_assert_for_1 (op1, EQ_EXPR, e, si); > } > } > + > + /* In the case of NAME != 0 or NAME == 0, if NAME's defining statement > + is a widening type conversion then we can assert that NAME's > + RHS is accordingly nonzero or zero. */ > + if ((comp_code == EQ_EXPR || comp_code == NE_EXPR) > + && integer_zerop (val)) > + { > + gimple def_stmt = SSA_NAME_DEF_STMT (name); > + if (is_gimple_assign (def_stmt)) > + { > + enum tree_code def_code = gimple_assign_rhs_code (def_stmt); > + if (CONVERT_EXPR_CODE_P (def_code)) > + { > + tree lhs = gimple_assign_lhs (def_stmt); > + tree rhs = gimple_assign_rhs1 (def_stmt); > + if (TYPE_PRECISION (TREE_TYPE (lhs)) > + >= TYPE_PRECISION (TREE_TYPE (rhs))) > + register_edge_assert_for_1 (rhs, comp_code, e, si); > + } > + } > + } > } > > > -- > 2.2.0.rc1.16.g6066a7e > Index: tree-ssa-forwprop.c =================================================================== --- tree-ssa-forwprop.c (revision 217349) +++ tree-ssa-forwprop.c (working copy) @@ -476,7 +476,7 @@ forward_propagate_into_comparison_1 (gim { rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt); tmp = combine_cond_expr_cond (stmt, code, type, - rhs0, op1, !single_use0_p); + rhs0, op1, false); if (tmp) return tmp; }