From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 0AB333858C50; Tue, 12 Sep 2023 09:46:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0AB333858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id BA4F81F74A; Tue, 12 Sep 2023 09:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1694511990; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=p0ZTQWLOq8+zxX4scloUfK3CuL+Ah3tjWN7sIHwvxaQ=; b=EN8b6NEZYD+0758+wPGZgCSG1Qir3MsSPj1j6Sxq9VcjCJU2L827xBrG9kWHnumccUaSfX ZnGq8MnVDidMOTARR417ZXebv6BusY97ldo0TkBGhg6cEdUC4Wd6xHD8eeNzQUQeKDJuuR IK34dbZU6Cg3G3vog7f7KA5qMOrQv2s= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1694511990; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=p0ZTQWLOq8+zxX4scloUfK3CuL+Ah3tjWN7sIHwvxaQ=; b=h6SDr/pYh64h8p7BowFW/zyNntiddsMqPkUPVuWdr0IVeuyb0OQWMqdfqpEaCdnIJ5G6l5 /zJX6oAFlSTN6MCQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 540402C142; Tue, 12 Sep 2023 09:46:30 +0000 (UTC) Date: Tue, 12 Sep 2023 09:46:30 +0000 (UTC) From: Richard Biener To: Jiufu Guo cc: gcc-patches@gcc.gnu.org, jeffreyalaw@gmail.com, richard.sandiford@arm.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org, bergner@linux.ibm.com, amacleod@redhat.com, aldyh@redhat.com Subject: Re: [PATCH] Checking undefined_p before using the vr In-Reply-To: <20230907020245.2888379-1-guojiufu@linux.ibm.com> Message-ID: References: <20230907020245.2888379-1-guojiufu@linux.ibm.com> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, 7 Sep 2023, Jiufu Guo wrote: > Hi, > > As discussed in PR111303: > > For pattern "(X + C) / N": "div (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)", > Even if "X" has value-range and "X + C" does not overflow, "@3" may still > be undefined. Like below example: > > _3 = _2 + -5; > if (0 != 0) > goto ; [34.00%] > else > goto ; [66.00%] > ;; succ: 3 > ;; 4 > > ;; basic block 3, loop depth 0 > ;; pred: 2 > _5 = _3 / 5; > ;; succ: 4 > > The whole pattern "(_2 + -5 ) / 5" is in "bb 3", but "bb 3" would be > unreachable (because "if (0 != 0)" is always false). > And "get_range_query (cfun)->range_of_expr (vr3, @3)" is checked in > "bb 3", "range_of_expr" gets an "undefined vr3". Where "@3" is "_5". > > So, before using "vr3", it would be safe to check "!vr3.undefined_p ()". > > Bootstrap & regtest pass on ppc64{,le} and x86_64. > Is this ok for trunk? OK, but I wonder why ->range_of_expr () doesn't return false for undefined_p ()? While "undefined" technically means we can treat it as nonnegative_p (or not, maybe but maybe not both), we seem to not want to do that. So why expose it at all to ranger users (yes, internally we in some places want to handle undefined). Richard. > BR, > Jeff (Jiufu Guo) > > PR middle-end/111303 > > gcc/ChangeLog: > > * match.pd ((X - N * M) / N): Add undefined_p checking. > (X + N * M) / N): Likewise. > ((X + C) div_rshift N): Likewise. > > gcc/testsuite/ChangeLog: > > * gcc.dg/pr111303.c: New test. > > --- > gcc/match.pd | 3 +++ > gcc/testsuite/gcc.dg/pr111303.c | 11 +++++++++++ > 2 files changed, 14 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/pr111303.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 801edb128f9..e2583ca7960 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -975,6 +975,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* "X+(N*M)" doesn't overflow. */ > && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3) > && get_range_query (cfun)->range_of_expr (vr4, @4) > + && !vr4.undefined_p () > /* "X+N*M" is not with opposite sign as "X". */ > && (TYPE_UNSIGNED (type) > || (vr0.nonnegative_p () && vr4.nonnegative_p ()) > @@ -995,6 +996,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* "X - (N*M)" doesn't overflow. */ > && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3) > && get_range_query (cfun)->range_of_expr (vr4, @4) > + && !vr4.undefined_p () > /* "X-N*M" is not with opposite sign as "X". */ > && (TYPE_UNSIGNED (type) > || (vr0.nonnegative_p () && vr4.nonnegative_p ()) > @@ -1025,6 +1027,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* "X+C" doesn't overflow. */ > && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1) > && get_range_query (cfun)->range_of_expr (vr3, @3) > + && !vr3.undefined_p () > /* "X+C" and "X" are not of opposite sign. */ > && (TYPE_UNSIGNED (type) > || (vr0.nonnegative_p () && vr3.nonnegative_p ()) > diff --git a/gcc/testsuite/gcc.dg/pr111303.c b/gcc/testsuite/gcc.dg/pr111303.c > new file mode 100644 > index 00000000000..eaabe55c105 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr111303.c > @@ -0,0 +1,11 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2" } */ > + > +/* Make sure no ICE. */ > +unsigned char a; > +int b(int c) { > + if (c >= 5000) > + return c / 5; > +} > +void d() { b(a - 5); } > +int main() {} > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)