From patchwork Fri Oct 21 03:37:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 78594 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp1097365qge; Thu, 20 Oct 2016 20:39:54 -0700 (PDT) X-Received: by 10.99.143.68 with SMTP id r4mr6072342pgn.170.1477021194356; Thu, 20 Oct 2016 20:39:54 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id g16si613143pfj.150.2016.10.20.20.39.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Oct 2016 20:39:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-439185-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-439185-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-439185-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=v2k88UxfMB5s+0gEc GtQvFmRsWNx2LHOLQGzMtO1xguvdjvLdkq815E06i3i01Q6/MZmvKz8pxSWoS6zs IOFrpSdpx+CuwDVmULUct8kjwSMmhgrmJ1TZgoWDDBpA0VqhOib1i9DZsvuFuqJ7 1s6eBmRlLU52kkmKIjq+Zb2sZI= 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 :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=uaNQ0MINyExfeONdXq/ebnw 6HSM=; b=JRMmFbsw9dsb0GIzKuEslrmh7Z6O9gZIPq7WF+MQ0hcztZsOBKK3ugv C8HDqBmlHUXYqVTetfuId36DusER1rTIjh/Td4bDGFm4SK2+dT2v0//AFqvoDf77 UjxQ8cxgsGHxjWDVeewHZZJs+Mv2IMVa+h5Z97GMMGBaNJlnODtY= Received: (qmail 110128 invoked by alias); 21 Oct 2016 03:39:38 -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 68861 invoked by uid 89); 21 Oct 2016 03:38:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=po, UD:fold-const.h, foldconsth, HX-Received:10.99.100.132 X-HELO: mail-pf0-f181.google.com Received: from mail-pf0-f181.google.com (HELO mail-pf0-f181.google.com) (209.85.192.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 21 Oct 2016 03:38:02 +0000 Received: by mail-pf0-f181.google.com with SMTP id r16so48477204pfg.1 for ; Thu, 20 Oct 2016 20:38:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to; bh=HxZGYEcK1jroR58fc1EOIIrZBtvjHtDCjXO4Iz/trGs=; b=DuFbHdPzqwPFIOZUOfBy6PkVLz/t+36VU6Bw1/hGrtaSOZE03xLil7zFudWEAloQjn 7Zebdr5sZcNJpTjRw3L8EQ88aUcZx6S7POwAvk8lSTPx9M7k4Lp/TOd800NQPT7GhF5X c9mEkPv+ABWLljXPlChZ+RkfKFU2hBfmjk02P+yqa42yvV+NIf6h3pIQQnHX05JMfn4G kxBNIyXo2aR78J+NkekKaYDlGfxOhTOHaHsNadH81O2pkNLJpyDhhOVhj9conPm7F+jJ sAiq3/q3XRYzd/EHBjmdW4Y14NK9PlNfClCzpLcvbsnQWn73z+n0xxp7hmTNj3Cb59fz mIGA== X-Gm-Message-State: AA6/9Rlueyk58OR8q72rPN2yx+v4nEDoXuICSeOm1T/lFeRVPCES1XSlpVX/9z+cQclW7O1h X-Received: by 10.99.100.132 with SMTP id y126mr6000130pgb.67.1477021080281; Thu, 20 Oct 2016 20:38:00 -0700 (PDT) Received: from [10.1.1.7] (58-6-183-210.dyn.iinet.net.au. [58.6.183.210]) by smtp.gmail.com with ESMTPSA id k89sm543565pfg.6.2016.10.20.20.37.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Oct 2016 20:37:59 -0700 (PDT) Subject: Re: [RFC][IPA-VRP] ADDR_EXPR and nonnull To: Jan Hubicka References: <0987d0d3-9b7b-eeb8-cf2a-17d1d0df7f49@linaro.org> <20161019142620.GA69032@kam.mff.cuni.cz> <26222603-662f-57b9-8087-724cc5e88191@linaro.org> <76ab5854-4c4e-ae4a-8d33-e2b1bdd02522@linaro.org> <20161020121524.GC19926@kam.mff.cuni.cz> Cc: Richard Biener , "gcc-patches@gcc.gnu.org" From: kugan Message-ID: Date: Fri, 21 Oct 2016 14:37:54 +1100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <20161020121524.GC19926@kam.mff.cuni.cz> X-IsSubscribed: yes Hi, On 20/10/16 23:15, Jan Hubicka wrote: >> Hi Richard, >> >> >> On 20/10/16 18:41, Richard Biener wrote: >>> On Thu, 20 Oct 2016, kugan wrote: >>> >>>> >>>> >>>> On 20/10/16 01:26, Jan Hubicka wrote: >>>>>>> Would excluding weak symbols (I believe I can check DECL_WEAK for this) >>>>>>> good >>>>>>> enough. Or looking for acceptable subset would work? >>>>>> >>>>>> I think we should add a symtab helper to tell if address_nonzero_p (if >>>>>> that doesn't aleady exist). >>>>> >>>>> We have node->nonzero_address() >>>> >>>> Thanks for the pointer. Here is an attempt. Does this look OK? >>>> >>>> + if (TREE_CODE (arg) == ADDR_EXPR) >>>> + { >>>> + /* See if the AADR_EXPR is nonnull. */ >>>> + varpool_node *node = NULL; >>>> + tree base = TREE_OPERAND (arg, 0); >>>> + base = get_base_address (base); >>>> + >>>> + if (decl_address_ip_invariant_p (base) >>>> + || !is_global_var (base)) >>>> + { >>>> + /* If the symbol address is local or >>>> + constant. */ >>> >>> "constant" doesn't matter. You want >>> >>> if (TREE_CODE (base) == CONST_DECL >>> || TREE_CODE (base) == PARM_DECL >>> || TREE_CODE (base) == RESULT_DECL) >>> addr_nonzero = true; >>> else if (VAR_P (base)) > Better to check decl_in_symtab_p (decl) >>> addr_nonzero = ! is_global_var (base) || (varpool_node::get >>> (base)->nonzero_address ()); > and symtab_node::get. > > I wonder if we can't unify the logic with tree_expr_nonzero_warnv_p > and corresponding vrp code? Are you saying that we should export tree_expr_nonzero_warnv_p and use here with the logic from above added to tree_expr_nonzero_warnv_p as shown in the attached patch? Thanks, Kugan > > Otherwise the patch looks fine to me. > Honza > diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 89ed89d..6fa42c9 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8954,7 +8954,7 @@ mask_with_tz (tree type, const wide_int &x, const wide_int &y) is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change *STRICT_OVERFLOW_P. */ -static bool +bool tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) { tree type = TREE_TYPE (t); @@ -13402,9 +13402,15 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) return true; /* Constants are never weak. */ - if (CONSTANT_CLASS_P (base)) + if (CONSTANT_CLASS_P (base) + || TREE_CODE (base) == PARM_DECL + || TREE_CODE (base) == RESULT_DECL) return true; + if (VAR_P (base) + && (!is_global_var (base) + || varpool_node::get (base)->nonzero_address ())) + return true; return false; } diff --git a/gcc/fold-const.h b/gcc/fold-const.h index bc22c88..a19a27a 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -198,6 +198,7 @@ extern tree fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off); /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */ extern tree fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off); +extern bool tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p); #define fold_build_pointer_plus_hwi(p,o) \ fold_build_pointer_plus_hwi_loc (UNKNOWN_LOCATION, p, o) diff --git a/gcc/ipa-prop.c b/gcc/ipa-prop.c index 353b638..55a2ea1 100644 --- a/gcc/ipa-prop.c +++ b/gcc/ipa-prop.c @@ -1670,9 +1670,20 @@ ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi, if (POINTER_TYPE_P (TREE_TYPE (arg))) { + bool addr_nonzero = false; + bool strict_overflow = false; + if (TREE_CODE (arg) == SSA_NAME && param_type && get_ptr_nonnull (arg)) + addr_nonzero = true; + else if (tree_expr_nonzero_warnv_p (arg, &strict_overflow)) + { + if (!strict_overflow) + addr_nonzero = true; + } + + if (addr_nonzero) { jfunc->vr_known = true; jfunc->m_vr.type = VR_ANTI_RANGE; diff --git a/gcc/testsuite/gcc.dg/ipa/vrp5.c b/gcc/testsuite/gcc.dg/ipa/vrp5.c index e69de29..571798d 100644 --- a/gcc/testsuite/gcc.dg/ipa/vrp5.c +++ b/gcc/testsuite/gcc.dg/ipa/vrp5.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-ipa-cp-details -fdump-tree-vrp1" } */ + +static __attribute__((noinline, noclone)) +int foo (int *p) +{ + if (!p) + return 0; + *p = 1; +} + +struct st +{ + int a; + int b; +}; + +int arr1[10]; +int a; +int bar (struct st *s) +{ + int arr2[10]; + int b; + if (!s) + return 0; + foo (&s->a); + foo (&a); + foo (&b); + foo (&arr1[1]); + foo (&arr2[1]); +} + +/* { dg-final { scan-ipa-dump "Setting nonnull for 0" "cp" } } */ +/* { dg-final { scan-tree-dump-times "if" 1 "vrp1" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/vrp6.c b/gcc/testsuite/gcc.dg/ipa/vrp6.c index e69de29..971db44 100644 --- a/gcc/testsuite/gcc.dg/ipa/vrp6.c +++ b/gcc/testsuite/gcc.dg/ipa/vrp6.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-ipa-cp-details -fdump-tree-vrp1" } */ + +static __attribute__((noinline, noclone)) +int foo (int *p) +{ + if (!p) + return 0; + *p = 1; +} + +struct st +{ + int a; + int b; +}; + +struct st s2; +int a; +int bar (struct st *s) +{ + struct st s3; + int b; + if (!s) + return 0; + foo (&s->a); + foo (&s2.a); + foo (&s3.a); + foo (&a); + foo (&b); +} + +/* { dg-final { scan-ipa-dump "Setting nonnull for 0" "cp" } } */ +/* { dg-final { scan-tree-dump-times "if" 1 "vrp1" } } */