From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 312D03858D37 for ; Mon, 10 Oct 2022 09:42:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 312D03858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665394933; h=from:from:reply-to:subject:subject: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=Q8GoxhMW9YGvtOH6FgGxt7lg4b1n+a9N8qxhz5amJQA=; b=YIEnQWfetg8clDDiUPio2Qtdf4nWN0ka3HSV1Lv1SYfiU+2IAKxQXUbuXh/S6ixLvwvssf cqy8sjeryQSshUkGM26lNuHz01cf9Qi8Mf+105G6Xr+iGjOe1FKo30RjkM+LjKnQyNmp/3 HJ5Hfms9d9ZiOlFsOZqfZrRKRLUtaAs= Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-122-hr7yGx_9PZqUr6VfbxKgOg-1; Mon, 10 Oct 2022 05:42:13 -0400 X-MC-Unique: hr7yGx_9PZqUr6VfbxKgOg-1 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-356a9048111so101469157b3.6 for ; Mon, 10 Oct 2022 02:42:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Q8GoxhMW9YGvtOH6FgGxt7lg4b1n+a9N8qxhz5amJQA=; b=Nt6M8q34SB3p9m2IQX9mUm8k6P24mYrwvURNYzEY7R3Nmxaed+0Wve1NthMrFSG79a eSkWU1KZLPO0b/YLSvy5szaFZrVbgwdyn4rtTOflyXRxEwCA6Z9hJ25Y2FlXTLuqJ7EP pJdbYbFmibt3im6n9zuUR/bgkD1uz4LW3kJoIfvrMBbImd57FHOTJHRkw30IhgeNkD6J 2ix0x6gNkR6h9YRIG3j81R4xq/WEVYU0s6sh2kohDZSSQxoQW3XCX8E1BaC6R50ZjNBg 7DU5+RC7Zf0GHNHj1hhrNuMgIbRQwcWERon0ZAzYLaXGA1WonJ9OFp8aQSSh7quM6dDP Xtnw== X-Gm-Message-State: ACrzQf1GfqjqqLrkeoBu1bfZr05nlKUuQBzCEvECSuOpAeEBLZ4jyHoD 1DplL77wOd4Dfjku6yd1h3SSO/IVane9ZIXKLZpWKl067l12P5TSeACNOzH8yi5ZvHq6LH9Uey2 6ukyy1DAhc0W/Nd5zUSFsvt2okvOlZRWPTg== X-Received: by 2002:a5b:7c1:0:b0:6c1:656b:a604 with SMTP id t1-20020a5b07c1000000b006c1656ba604mr1317185ybq.617.1665394932323; Mon, 10 Oct 2022 02:42:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7sday7vD+Jb40ELsKg2+ECc20j1iI3GAThCo65PJSjYuNBs87/5T4b1RDr5a5aAsgefl693wBiSYgBJ49SZUs= X-Received: by 2002:a5b:7c1:0:b0:6c1:656b:a604 with SMTP id t1-20020a5b07c1000000b006c1656ba604mr1317176ybq.617.1665394932072; Mon, 10 Oct 2022 02:42:12 -0700 (PDT) MIME-Version: 1.0 References: <20221006105110.1719060-1-aldyh@redhat.com> In-Reply-To: <20221006105110.1719060-1-aldyh@redhat.com> From: Aldy Hernandez Date: Mon, 10 Oct 2022 11:42:01 +0200 Message-ID: Subject: Re: [RFC] Add op1_range for __builtin_signbit. To: GCC patches Cc: Jakub Jelinek , Richard Biener , Andrew MacLeod X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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: I have committed this patch, as the __builtin_signbit range-op entry is correct. I'd still like clarification on the folding). Aldy On Thu, Oct 6, 2022 at 12:51 PM Aldy Hernandez wrote: > > This is the op1_range range-op entry for __builtin_signbit. It allows > us to wind back through a call to signbit. > > For example, on the true side of if (__builtin_signbit(x_5) != 0) we > can crop down the range of x_5 to: > > [frange] float [-Inf, -0.0 (-0x0.0p+0)] -NAN > > Similarly on the false side, we can crop to: > > [frange] float [0.0 (0x0.0p+0), +Inf] +NAN > > This brings about an interesting question, can we fold the second > conditional here as always false? > > void foo(float x) > { > if (__builtin_signbit (x)) > { > if (x > 0.0) > link_error(); > } > } > > The only values for x at the second conditional are negative values, > or -NAN, so it will always evaluate to false. ISTM that we > *shouldn't* fold this conditional as there is user observable behavior > if there is a signaling NAN. For that matter, that is exactly what we > do in ranger-ops. We leave the conditional in place, but ranger > is able to determine that "x" is UNDEFINED on the path leading up to > link_error: > > =========== BB 3 ============ > Imports: x_3(D) > Exports: x_3(D) > x_3(D) [frange] float [-Inf, -0.0 (-0x0.0p+0)] -NAN > : > if (x_3(D) > 0.0) > goto ; [INV] > else > goto ; [INV] > > 3->4 (T) x_3(D) : [frange] UNDEFINED > 3->5 (F) x_3(D) : [frange] float [-Inf, -0.0 (-0x0.0p+0)] -NAN > > This would allow users of ranger to query x_3 on the 3->4 and notice > it's unreachable, without VRP removing the conditional. > > I believe this is correct, but would like confirmation from the experts. > > gcc/ChangeLog: > > * gimple-range-op.cc: Add op1_range entry for __builtin_signbit. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/vrp-float-signbit-3.c: New test. > --- > gcc/gimple-range-op.cc | 20 +++++++++++++++++++ > .../gcc.dg/tree-ssa/vrp-float-signbit-3.c | 14 +++++++++++++ > 2 files changed, 34 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-3.c > > diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc > index 42ebc7d6ce5..abc33e7af0c 100644 > --- a/gcc/gimple-range-op.cc > +++ b/gcc/gimple-range-op.cc > @@ -306,6 +306,7 @@ class cfn_signbit : public range_operator_float > { > public: > using range_operator_float::fold_range; > + using range_operator_float::op1_range; > virtual bool fold_range (irange &r, tree type, const frange &lh, > const irange &, relation_kind) const > { > @@ -320,6 +321,25 @@ public: > } > return false; > } > + virtual bool op1_range (frange &r, tree type, const irange &lhs, > + const frange &, relation_kind) const override > + { > + if (lhs.zero_p ()) > + { > + r.set (type, dconst0, frange_val_max (type)); > + r.update_nan (false); > + return true; > + } > + if (!lhs.contains_p (build_zero_cst (lhs.type ()))) > + { > + REAL_VALUE_TYPE dconstm0 = dconst0; > + dconstm0.sign = 1; > + r.set (type, frange_val_min (type), dconstm0); > + r.update_nan (true); > + return true; > + } > + return false; > + } > } op_cfn_signbit; > > // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER. > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-3.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-3.c > new file mode 100644 > index 00000000000..dd3090aeb10 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-3.c > @@ -0,0 +1,14 @@ > +// { dg-do compile } > +// { dg-options "-O2 -ffast-math -fdump-tree-evrp" } > + > +void link_error(); > + > +void foo(float x) > +{ > + if (__builtin_signbit (x)) > + { > + if (x > 0.0) > + link_error(); > + } > +} > +// { dg-final { scan-tree-dump-not "link_error" "evrp" } } > -- > 2.37.1 >