From patchwork Mon Sep 5 06:26:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp40565wrt; Sun, 4 Sep 2022 23:27:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR6gVqHZCnyNn9pgsW11sQiGqIi8i7ZZwCvv7MNbNd7PUs3rVkXPFcDLR/E0cqWQH/ek9J7G X-Received: by 2002:a17:906:cc57:b0:73d:cdfd:28b4 with SMTP id mm23-20020a170906cc5700b0073dcdfd28b4mr33277741ejb.211.1662359269076; Sun, 04 Sep 2022 23:27:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662359269; cv=none; d=google.com; s=arc-20160816; b=vhxEO60Sp/pouiWK1v6fS7wSU8TTRqLk8EuSyh5bxmfCVdjD7YxXa8N0oo3EKFhl7O ef4MFWWQiOPeHx8Mmc92ZWiv9vUBm8xn0mIBr3pyRMiczMrQmCxvidW1LmOQvqLGLudo 0hGXdGPEUPj4RbSrPu9uc4CDAz0f3yXk4GAQZJJoQLHtqpPcxkQOXY8rFRSfaJL86WFj DF5+16KGAsJzf+A3r7+4IKSK5P9LgCE9GAQaoEzXcM58PJcKquD+zEV+5k6GQiMdbiN9 gR1YIkLg31JwFj9lSYvBP+pPv1ONX+YNapRi5L/bKSwIDylJr2d87/CSKrCxdu7mzCXD u2rA== 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=iJ07Jl0KLh/SNu/PTDQIxBfZ22ER/Zx1IpfK/h7Z9+k=; b=CFHyge/Qt1wbOsi+rFZ7sYK51M+aNiEyf1Y9Nyg33UuT0zzsbjoOjmPScsnCZBZPbe J5Wn1HZBtxqBzZctQ3dMoheiMtOQuC4DOb2wt8M62HcrCztLClvNXgdrg5AbDKijsp4k PnOPUxT66KpKp6C2nxmq44q862NbmjBa8Qqh9/F7vh9NJq+SjHeiT1PhYHXqydGcXCp7 I9AOfUTVDjuxDPoclda0HBWiL/5p7rGxxBSajH7FYGUIO97QWA+q3w/9dg1S6VCWcOKI X5I4GWJ5iyWPG+e8iY5YiVxS7xhguzX/zcXIXO5urBhexXO7Sjm3tl/Q60tr/AHq3//d Xgaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yp6btV+J; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dr18-20020a170907721200b00741c0bd705dsi7174123ejc.631.2022.09.04.23.27.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Sep 2022 23:27:49 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yp6btV+J; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 DD9653857BB7 for ; Mon, 5 Sep 2022 06:27:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD9653857BB7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662359267; bh=iJ07Jl0KLh/SNu/PTDQIxBfZ22ER/Zx1IpfK/h7Z9+k=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=yp6btV+JSDbcgTlitI2gx3ofS6/9xyTKoJrWgYik/Qm9uz7qFY4aqT5v+sWyzcCCS 3q/0D9ZGShvprWw5ABACDijYUqzBpNam3F2/VnMKrV23y79rmwvibAQJJip236hZBJ vUc3r1WnxQoMlTr5JoFrrVNIdreaILmVhLYDNaOI= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 150273858D32 for ; Mon, 5 Sep 2022 06:27:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 150273858D32 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-325-mtQ8Aj4UNYucPk1cEUvBxQ-1; Mon, 05 Sep 2022 02:27:01 -0400 X-MC-Unique: mtQ8Aj4UNYucPk1cEUvBxQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 481D11C05143; Mon, 5 Sep 2022 06:27:01 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.210]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D43CD403167; Mon, 5 Sep 2022 06:27:00 +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 2856Qw4f3240747 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 5 Sep 2022 08:26:58 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2856QvM33240746; Mon, 5 Sep 2022 08:26:57 +0200 To: GCC patches Subject: [PATCH] Fold __builtin_signbit to nonzero instead of 1. Date: Mon, 5 Sep 2022 08:26:55 +0200 Message-Id: <20220905062655.3240675-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 , Joseph Myers 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?1743110032757534313?= X-GMAIL-MSGID: =?utf-8?q?1743110032757534313?= After Joseph's comment wrt __builtin_signbit, I have been unable to convince myself that arbitrarily folding __builtin_signbit () of a negative number to 1 is correct. For example, on the true side of x < -5.0 we know the sign bit is set, but on the false side, we know nothing because X may be a NAN. I don't want to put ourselves in a position where the same call to __builtin_signbit can return two different things (1 or negative whatever), so I'm going to return nonzero which is correct across the board until someone can convince me otherwise. Setting the range to nonzero still allows us to fold conditionals checking __fold_builtin, while not actually propagating a 1. Zero propagation still works. That being said, I still think we should be folding __builtin_signbit's of negative numbers to whatever the hardware returns, instead of 1/0 like what the front ends do. I am going to push this if tests succeed. gcc/ChangeLog: * gimple-range-fold.cc (fold_using_range::range_of_builtin_int_call): Fold a set signbit in __builtin_signbit to nonzero. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp-float-signbit-2.c: New test. --- gcc/gimple-range-fold.cc | 5 +--- .../gcc.dg/tree-ssa/vrp-float-signbit-2.c | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-2.c diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc index d8497fc9be7..3543f0980b8 100644 --- a/gcc/gimple-range-fold.cc +++ b/gcc/gimple-range-fold.cc @@ -1032,10 +1032,7 @@ fold_using_range::range_of_builtin_int_call (irange &r, gcall *call, if (tmp.get_signbit ().varying_p ()) return false; if (tmp.get_signbit ().yes_p ()) - { - tree one = build_one_cst (type); - r.set (one, one); - } + r.set_nonzero (type); else r.set_zero (type); return true; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-2.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-2.c new file mode 100644 index 00000000000..954c7ebd4f8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-2.c @@ -0,0 +1,24 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp" } + +// Test that the only thing we know about the signbit about negative number is +// that it's not 0. + +void link_error (); + +int num; + +void func(float x) +{ + if (x < -5.0) + { + num = __builtin_signbit (x); + + // We may not know the exact signbit, but we know it's not 0. + if (!__builtin_signbit (x)) + link_error (); + } +} + +// { dg-final { scan-tree-dump-not "num = \[-0-9\];" "evrp" } } +// { dg-final { scan-tree-dump-not "link_error" "evrp" } }