Message ID | 20220901111851.2595874-2-aldyh@redhat.com |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp200341wro; Thu, 1 Sep 2022 04:19:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR7FZEJJUVpZzZJIXb+GhNpm4HK+ScaMDVFNCVk8saayiF8d3uOSEUtvrtU4kUJ38Hp//xkp X-Received: by 2002:a05:6402:2499:b0:440:942a:40c2 with SMTP id q25-20020a056402249900b00440942a40c2mr29484173eda.37.1662031186817; Thu, 01 Sep 2022 04:19:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662031186; cv=none; d=google.com; s=arc-20160816; b=SJotrRnnRvZ8PP/XRfpQXjUbr1cS+IprqQoVNeoiJtii7+EgY1vX3hws3aTKHc4x/M uyXKhu+QvgFb8a9eqiSf40joEWxudjXWQxWVVX+kkjtI/lPqpTZ3dxS/z8AizElhTyxh g7Uf4l1cCe7Vno+wCjc2P5I19jDPwsjeao7Vdh0juI/sM7c+aL+B51Zfh41d+cM5Ha1R TmjrY74hBB1jcCA4N+J7KOYpfEy4lJU+eX3WJKqlKHMgF5FN39QJEbGJ3427VI3bonjF oOC9aK3MlXobXdJ2yGl3oTu5DHsy40p63IHwdp8VbHlAmUem01SkrMROEu60hYqGwoms U9rw== 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:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=Kjvu89fcFlJ2/Mj8vkj1nTpFKrSuvmCzbuTnwj6Whsk=; b=u9SPYwVYvh5Jr1POKZZcW9ElkQ18ujBDbgvtfe16UVBK57Utwem9qq+ClJfLWtG1Nn VoSfGJs/6QDDeVAhKnUXj0vMM+BnveOleJ2Vs3pC65aEQ/twxzL5QIgrQV+PwN6x6Zc5 X7H1dtcJblWeUJUvs+Sj4uXYuIq8sydaKxKyVrE/QdlngEh9Km1s4cvwsleeWXy05YjB qr+8ZIL1iCqARe+HXLprH5ZIlG3FhRlXhjL48CE/v3W6MljpxkZ9OnaJrso8iYJqRg2N cQMah2eFtY49k0ywNg+/qaTUsRCouB8/Ep6VYIXOvIypAmMgisDHipFLAG12BDWB/j8Y reyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K7JZYLg3; 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 dr9-20020a170907720900b007414e669eefsi11462963ejc.439.2022.09.01.04.19.46 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 04:19:46 -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=K7JZYLg3; 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 829FF382DB11 for <ouuuleilei@gmail.com>; Thu, 1 Sep 2022 11:19:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 829FF382DB11 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662031185; bh=Kjvu89fcFlJ2/Mj8vkj1nTpFKrSuvmCzbuTnwj6Whsk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=K7JZYLg33ikGLqEpTC14GDG5Z4j7QtxYo6g3RMG4QKU+hvfDcdQzwjdQKt34jNPow Hl6fckVkS7PSkUsRoOcm3aWYICpoaQMtV5C8JncPUJUzaaa4rUb9TWWUgpkFHBH9o8 Jt0LEhPgmTh/j6MqAB2nwJmlVOxbD4T+92hpX+Y0= 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 A47A9385829F for <gcc-patches@gcc.gnu.org>; Thu, 1 Sep 2022 11:19:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A47A9385829F 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-452-xiE4CjLkNB2ZXECrfYM-ag-1; Thu, 01 Sep 2022 07:18:59 -0400 X-MC-Unique: xiE4CjLkNB2ZXECrfYM-ag-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 D1650101A54E; Thu, 1 Sep 2022 11:18:58 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.149]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6ACC3492C3B; Thu, 1 Sep 2022 11:18:58 +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 281BIup42596639 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 1 Sep 2022 13:18:56 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 281BIuFP2596638; Thu, 1 Sep 2022 13:18:56 +0200 To: GCC patches <gcc-patches@gcc.gnu.org> Subject: [COMMITTED] Implement ranger folder for __builtin_signbit. Date: Thu, 1 Sep 2022 13:18:51 +0200 Message-Id: <20220901111851.2595874-2-aldyh@redhat.com> In-Reply-To: <20220901111851.2595874-1-aldyh@redhat.com> References: <20220901111851.2595874-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.4 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, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Aldy Hernandez via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Aldy Hernandez <aldyh@redhat.com> Cc: Jakub Jelinek <jakub@redhat.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742766013502848027?= X-GMAIL-MSGID: =?utf-8?q?1742766013502848027?= |
Series |
[COMMITTED] Implement ranger folder for __builtin_signbit.
|
|
Commit Message
Aldy Hernandez
Sept. 1, 2022, 11:18 a.m. UTC
Now that we keep track of the signbit, we can use it to fold __builtin_signbit. I am assuming I don't have try too hard to get the actual signbit number and 1 will do. Especially, since we're inconsistent in trunk whether we fold the builtin or whether we calculate it at runtime. abulafia:~$ cat a.c float nzero = -0.0; main(){ printf("0x%x\n", __builtin_signbit(-0.0)); printf("0x%x\n", __builtin_signbit(nzero)); } abulafia:~$ gcc a.c -w && ./a.out 0x1 0x80000000 It is amazing that we've been failing to fold something as simple as this: if (x > 5.0) num = __builtin_signbit (x); It does the right thing now :-P. gcc/ChangeLog: * gimple-range-fold.cc (fold_using_range::range_of_builtin_int_call): Add case for CFN_BUILT_IN_SIGNBIT. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp-float-signbit-1.c: New test. --- gcc/gimple-range-fold.cc | 20 +++++++++++++++++++ .../gcc.dg/tree-ssa/vrp-float-signbit-1.c | 12 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-1.c
Comments
On Thu, 1 Sep 2022, Aldy Hernandez via Gcc-patches wrote: > Now that we keep track of the signbit, we can use it to fold __builtin_signbit. > > I am assuming I don't have try too hard to get the actual signbit > number and 1 will do. Especially, since we're inconsistent in trunk whether > we fold the builtin or whether we calculate it at runtime. The main thing to watch out for is cases where, in the abstract machine, there is a single call executed to __builtin_signbit, but after code transformations, some uses of the result of that call are optimized to use a 0 or 1 value and other uses end up using a runtime value - inconsistency between different calls is fine, inconsistency where only a single call is executed in the abstract machine isn't. (Cf. bugs 102930, 85957, 93681, 93806, 93682, for example; the test in bug 93806 comment 27 is the sort of thing to try.)
On Thu, Sep 1, 2022 at 6:41 PM Joseph Myers <joseph@codesourcery.com> wrote: > > On Thu, 1 Sep 2022, Aldy Hernandez via Gcc-patches wrote: > > > Now that we keep track of the signbit, we can use it to fold __builtin_signbit. > > > > I am assuming I don't have try too hard to get the actual signbit > > number and 1 will do. Especially, since we're inconsistent in trunk whether > > we fold the builtin or whether we calculate it at runtime. > > The main thing to watch out for is cases where, in the abstract machine, > there is a single call executed to __builtin_signbit, but after code > transformations, some uses of the result of that call are optimized to use > a 0 or 1 value and other uses end up using a runtime value - inconsistency > between different calls is fine, inconsistency where only a single call is > executed in the abstract machine isn't. (Cf. bugs 102930, 85957, 93681, > 93806, 93682, for example; the test in bug 93806 comment 27 is the sort of > thing to try.) Can't we just be consistent with the runtime? I'm happy to return whatever value is appropriate for the architecture. It doesn't have to be 1. Though ISTM that if we know the sign on one side of a conditional, we should know the sign on the other side of the conditional, so we should fold all uses of __builtin_signbit in that case. So maybe we're ok. On the other hand, we could narrow the range to nonzero, which we can model perfectly for integers (and __builtin_signbit returns one). If we take this approach it means we can't fold: if (x < -5.0) num = __builtin_signbit (x); but we could fold: if (x > 5.0) num = __builtin_signbit (x); since that's always 0. And it also means we could fold conditional checks against zero/nonzero: void func(float x) { if (x < -5.0 && !__builtin_signbit (x)) link_error (); } Whatever works for y'all. Aldy p.s. My head exploded after reading half of PR93806. I should just go back to integers.
diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc index b0b22106320..d8497fc9be7 100644 --- a/gcc/gimple-range-fold.cc +++ b/gcc/gimple-range-fold.cc @@ -1023,6 +1023,26 @@ fold_using_range::range_of_builtin_int_call (irange &r, gcall *call, break; } + case CFN_BUILT_IN_SIGNBIT: + { + arg = gimple_call_arg (call, 0); + frange tmp; + if (src.get_operand (tmp, arg)) + { + if (tmp.get_signbit ().varying_p ()) + return false; + if (tmp.get_signbit ().yes_p ()) + { + tree one = build_one_cst (type); + r.set (one, one); + } + else + r.set_zero (type); + return true; + } + break; + } + case CFN_BUILT_IN_TOUPPER: { arg = gimple_call_arg (call, 0); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-1.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-1.c new file mode 100644 index 00000000000..3fa783ec460 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-signbit-1.c @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp" } + +int num; + +void func(float x) +{ + if (x > 5.0) + num = __builtin_signbit (x); +} + +// { dg-final { scan-tree-dump-times "num = 0;" 1 "evrp" } }