From patchwork Thu Oct 6 10:51:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1779 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp246116wrs; Thu, 6 Oct 2022 03:52:52 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4voMIm4AUCdpBxlBpfkpeJ/hgDA7hcSmPlBLN5IQdl4J7ZEwgNBTIwESJbgroxGiDP9VRu X-Received: by 2002:a05:6402:360d:b0:459:5f40:5b0a with SMTP id el13-20020a056402360d00b004595f405b0amr3868176edb.168.1665053572187; Thu, 06 Oct 2022 03:52:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665053572; cv=none; d=google.com; s=arc-20160816; b=n9GuB5B3MBGSAJ453/IbHiOIM7ZHuGfaZH01Ig40FLh8Izv4Sh2na2bWdKXsFVhz2Q 2YcXoncXXZZRXPyT4cI350ABAcunmuwGjMR/SovqpFdw0UhCnnFeb8I1mmrbheMMuzXC FvLrHrha9qAr12Kt2GyQzHKBWtDZdk7h1ZASrjgakLhNe5kCaACQkvlFhPuQyiH+1TNF R6R/SFk6SMXxCiIa5hHXrlAsnXdCXVPyX6I6kFJ+wW5kkEoPKfRH/JiM6cVwuxN0IY36 3xMkKaxtFGaUp4i61+uGsLCOnLjz6EnudQPhwwLmH7jyp58biM8ZzA100EDKU3pX1JAs AsyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=glCq29kg+tN2Ojy7KGZJq1sjEljgu5L3FYuvUBV59lU=; b=mP98DB5MAz8OAlkj2DYFpnqVoGcvIlAcMPVTuVpO1+3yp5N2M+ecvsrwEj66RtK6tM Wrcn4NkRi3ZcEoznLGvOaeUHOuBkXoAtV7GWFxGVbryVAwdm8x4ko4C0HC1fRq4Ph+lA 9nEV6eJ0CTNIU6TFa7p0VqYBbFaWR7c6UJoDSmYC6Fi33un8hvNm6mlekUr5pIoqTraG kQIJAL8wyHHLKPfQo6gax69YC/AykfGuF02feJrdrKgcYfCthJ/m4jWygTwYMk8zP9lB P7W0ZVC+NVYfJyTC6N9f4NtttdcUjSKv4u41TXMvTAijRkcaeY/Z58JOOsT9SNEGgjbf WFMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="bF/P7DIK"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id xa8-20020a170906fd8800b0073d8e4e8c77si14979631ejb.1005.2022.10.06.03.52.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 03:52:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="bF/P7DIK"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33F383885527 for ; Thu, 6 Oct 2022 10:52:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33F383885527 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665053571; bh=glCq29kg+tN2Ojy7KGZJq1sjEljgu5L3FYuvUBV59lU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=bF/P7DIKWRZ0ecl0Bpbfg/qyzkaZk1OuQYHJBVkGLkxz/wecq+YyUCW7xkhAsrHNh fPX5E2mrcmPA1piwqZa2lFjaKspMEl2mWAAaP1mKjzQOO8v7Svgq1FY7t+kjRvp+tA Ec0isYd8Mw9n1JFVJKnXbc1jymeEOYmyU+NT9LYU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 42DA9384B833 for ; Thu, 6 Oct 2022 10:51:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 42DA9384B833 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-556-a45Cq8rXO4Oas_tXofigVw-1; Thu, 06 Oct 2022 06:51:23 -0400 X-MC-Unique: a45Cq8rXO4Oas_tXofigVw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B3769858F13; Thu, 6 Oct 2022 10:51:22 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.195.165]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C5231121320; Thu, 6 Oct 2022 10:51:22 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 296ApJY31719091 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 6 Oct 2022 12:51:19 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 296ApJe91719090; Thu, 6 Oct 2022 12:51:19 +0200 To: GCC patches Subject: [RFC] Add op1_range for __builtin_signbit. Date: Thu, 6 Oct 2022 12:51:10 +0200 Message-Id: <20221006105110.1719060-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Cc: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745935214714718045?= X-GMAIL-MSGID: =?utf-8?q?1745935214714718045?= 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" } }