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 42DA9384B833 for ; Thu, 6 Oct 2022 10:51:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 42DA9384B833 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=1665053485; 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: content-transfer-encoding:content-transfer-encoding; bh=glCq29kg+tN2Ojy7KGZJq1sjEljgu5L3FYuvUBV59lU=; b=SrztxEGKYpe2vI9Qyf4CeiV8c1a83V4x3/3i4HLUUJw9WHcAJZehUSBJEAm+RS1KlD8v7r WICplO0UdIvNmw0DOCN4neh43DMvLgInEJ2eNDZznUNCptJtXC33VSNp3IemAvrgsWvNIp QZ9sTzsLhkGw2cgFhBnk4jplIOyPU4o= 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 From: Aldy Hernandez To: GCC patches Cc: Jakub Jelinek , Richard Biener , Andrew MacLeod , Aldy Hernandez 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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