From patchwork Thu Jan 26 16:30:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 48789 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp363903wrn; Thu, 26 Jan 2023 08:31:08 -0800 (PST) X-Google-Smtp-Source: AMrXdXu4pI5StN9QMOBqlKYiOkW/iPtE1Te4vndOCzTi2OuzmO9IR0Q8NZI3XdhHAcZncO4yVuCO X-Received: by 2002:aa7:c845:0:b0:497:b6bc:b811 with SMTP id g5-20020aa7c845000000b00497b6bcb811mr36364815edt.33.1674750668012; Thu, 26 Jan 2023 08:31:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674750668; cv=none; d=google.com; s=arc-20160816; b=e1XgTMOyTkaW2KZ6eWMhmtQD1aZMMc6gJKNy5jnJCEwu6dP07ExRe/vpVbglG6j0Qo AVPJfK9yosR8LinutuHHiuC1Vpa5ujX5x6QavmPoRtvGDWYjcUckD3cTGj/+5ampYiPm Dh1qw3TKysV4KTWnw+HlKGP2pCZxnYAFLLdXkXKBx6myWboIY9pa4zMSfQCQ1qUo9etx FK+79Pdzk/a8p8J8lNEoBrd/1AAyhcjXCwwbEp4SjSL/k9G0s+/ew9CI8e+Yqsxu1RLb OI9TOE43esU6MmK5xh1mMtWbqSymI74l4iBoycNCnD4HDzg9b+aTXyt4zRGmMhTq+iIV Wx8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=DaxN+zVetV5/oecQdl2BCo7tevA21GO/B9MKY4WotS4=; b=JlZKMQZf0zNs9fGiMOFLct8vQwd8lcShbwEmz6ongd6gzCZdGdMS7E6tDI3tKHPXWb 9tiRvE7teQ4Pn7FS0ZPTUo1NRvjO7M7cfw3l7g9B/uNRnzx5R3rmuTRBhJQ1ogn6rQ88 zf7YWqXlVe9HO7R0wtm5b4kllEBJUuvLfaMLFhJcFYZxwDl6g0rp4UrRvpgh+kU50srR Rwaj5iGNJW0sQONox9G9wapWM85B3X/dWgcofJaQCo8GjZyAypHfUFypiZpkzk+4RXQP PRRDgBeCXHST30w70hx9h+t4zfZ5O+JTZI3DR6joY+wLJJfkyhb836uPkvY93o9Eh39x 7oiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YtdyDWdF; 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 fi20-20020a056402551400b004a19043fe9fsi22527edb.361.2023.01.26.08.31.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 08:31:07 -0800 (PST) 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=YtdyDWdF; 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 BAC15385840A for ; Thu, 26 Jan 2023 16:31:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAC15385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674750666; bh=DaxN+zVetV5/oecQdl2BCo7tevA21GO/B9MKY4WotS4=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=YtdyDWdFwHj948x5VLouDOjMl2r5AGw6Gv6OyuKMeM76rLa+lmR0EnOlN9QOyHq1K xZAH2s3EwpPI7ryfQfudohoy3zZtLVoNO3OFW+4/LGm9gCMidMRzpJ2yg58P7EriO/ 1jZ/G8p/VYLfyhw48gjX2b7K0317o0Z8so0uON+M= 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 256893858C2F for ; Thu, 26 Jan 2023 16:30:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 256893858C2F 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-211-hJCap1wmP3iv9R46JBvFCg-1; Thu, 26 Jan 2023 11:30:20 -0500 X-MC-Unique: hJCap1wmP3iv9R46JBvFCg-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 33747811E6E for ; Thu, 26 Jan 2023 16:30:20 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DF981492B02; Thu, 26 Jan 2023 16:30:19 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 30QGUHIp2746689 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 26 Jan 2023 17:30:17 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 30QGUG622746688; Thu, 26 Jan 2023 17:30:16 +0100 Date: Thu, 26 Jan 2023 17:30:16 +0100 To: Aldy Hernandez Cc: gcc-patches@gcc.gnu.org Subject: [committed] frange: Fix up foperator_{,not_}equal::fold_range for signed zeros [PR108540] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: 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?1756103356709538844?= X-GMAIL-MSGID: =?utf-8?q?1756103356709538844?= Hi! The following testcases are miscompiled, because threader sees some SSA_NAME would have -0.0 value and when computing range of SSA_NAME == 0.0 foperator_equal::fold_range sees one operand has [-0.0, -0.0] singleton range, the other [0.0, 0.0], they aren't equal (frange operator== uses real_identical etc. rather than real comparisons) and so it thinks they compare unequal. With signed zeros -0.0 == 0.0 is true though, so we need to special case the both ranges singleton code. Similarly, if we see op1 range being say [-42.0, -0.0] and op2 range [0.0, 42.0], we'd check that the intersection of the two ranges is empty (that is correct) and fold the result of == between such operands to [0, 0] which is wrong, because -0.0 == 0.0, it needs to be [0, 1]. Similarly for foperator_not_equal::fold_range. Bootstrapped/regtested on x86_64-linux and i686-linux, approved in the PR by Aldy, committed to trunk. 2023-01-26 Jakub Jelinek PR tree-optimization/108540 * range-op-float.cc (foperator_equal::fold_range): If both op1 and op2 are singletons, use range_true even if op1 != op2 when one range is [-0.0, -0.0] and another [0.0, 0.0]. Similarly, even if intersection of the ranges is empty and one has zero low bound and another zero high bound, use range_true_and_false rather than range_false. (foperator_not_equal::fold_range): If both op1 and op2 are singletons, use range_false even if op1 != op2 when one range is [-0.0, -0.0] and another [0.0, 0.0]. Similarly, even if intersection of the ranges is empty and one has zero low bound and another zero high bound, use range_true_and_false rather than range_true. * gcc.c-torture/execute/ieee/pr108540-1.c: New test. * gcc.c-torture/execute/ieee/pr108540-2.c: New test. Jakub --- gcc/range-op-float.cc.jj 2023-01-16 09:39:36.191929750 +0100 +++ gcc/range-op-float.cc 2023-01-26 13:44:10.542899269 +0100 @@ -607,6 +607,10 @@ foperator_equal::fold_range (irange &r, { if (op1 == op2) r = range_true (type); + // If one operand is -0.0 and other 0.0, they are still equal. + else if (real_iszero (&op1.lower_bound ()) + && real_iszero (&op2.lower_bound ())) + r = range_true (type); else r = range_false (type); } @@ -617,7 +621,18 @@ foperator_equal::fold_range (irange &r, frange tmp = op1; tmp.intersect (op2); if (tmp.undefined_p ()) - r = range_false (type); + { + // If one range is [whatever, -0.0] and another + // [0.0, whatever2], we don't know anything either, + // because -0.0 == 0.0. + if ((real_iszero (&op1.upper_bound ()) + && real_iszero (&op2.lower_bound ())) + || (real_iszero (&op1.lower_bound ()) + && real_iszero (&op2.upper_bound ()))) + r = range_true_and_false (type); + else + r = range_false (type); + } else r = range_true_and_false (type); } @@ -708,10 +723,14 @@ foperator_not_equal::fold_range (irange // consist of a single value, and then compare them. else if (op1.singleton_p () && op2.singleton_p ()) { - if (op1 != op2) - r = range_true (type); - else + if (op1 == op2) r = range_false (type); + // If one operand is -0.0 and other 0.0, they are still equal. + else if (real_iszero (&op1.lower_bound ()) + && real_iszero (&op2.lower_bound ())) + r = range_false (type); + else + r = range_true (type); } else if (!maybe_isnan (op1, op2)) { @@ -720,7 +739,18 @@ foperator_not_equal::fold_range (irange frange tmp = op1; tmp.intersect (op2); if (tmp.undefined_p ()) - r = range_true (type); + { + // If one range is [whatever, -0.0] and another + // [0.0, whatever2], we don't know anything either, + // because -0.0 == 0.0. + if ((real_iszero (&op1.upper_bound ()) + && real_iszero (&op2.lower_bound ())) + || (real_iszero (&op1.lower_bound ()) + && real_iszero (&op2.upper_bound ()))) + r = range_true_and_false (type); + else + r = range_true (type); + } else r = range_true_and_false (type); } --- gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c.jj 2023-01-26 13:54:55.131463151 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c 2023-01-26 13:54:31.333811525 +0100 @@ -0,0 +1,84 @@ +/* PR tree-optimization/108540 */ + +__attribute__((noipa)) void +bar (const char *cp, unsigned long size, char sign, int dsgn) +{ + if (__builtin_strcmp (cp, "ZERO") != 0 || size != 4 || sign != '-' || dsgn != 1) + __builtin_abort (); +} + +__attribute__((noipa)) void +foo (int x, int ch, double d) +{ + const char *cp = ""; + unsigned long size = 0; + char sign = '\0'; + switch (x) + { + case 42: + if (__builtin_isinf (d)) + { + if (d < 0) + sign = '-'; + cp = "Inf"; + size = 3; + break; + } + if (__builtin_isnan (d)) + { + cp = "NaN"; + size = 3; + break; + } + if (d < 0) + { + d = -d; + sign = '-'; + } + else if (d == 0.0 && __builtin_signbit (d)) + sign = '-'; + else + sign = '\0'; + if (ch == 'a' || ch == 'A') + { + union U { long long l; double d; } u; + int dsgn; + u.d = d; + if (u.l < 0) + { + dsgn = 1; + u.l &= 0x7fffffffffffffffLL; + } + else + dsgn = 0; + if (__builtin_isinf (d)) + { + cp = "INF"; + size = 3; + } + else if (__builtin_isnan (d)) + { + cp = "NAN"; + size = 3; + } + else if (d == 0) + { + cp = "ZERO"; + size = 4; + } + else + { + cp = "WRONG"; + size = 5; + } + bar (cp, size, sign, dsgn); + } + } +} + +int +main () +{ + foo (42, 'a', -0.0); + return 0; +} --- gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-2.c.jj 2023-01-26 13:55:01.186374500 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-2.c 2023-01-26 13:54:36.662733487 +0100 @@ -0,0 +1,23 @@ +/* PR tree-optimization/108540 */ + +__attribute__((noipa)) int +foo (int x, double d) +{ + if (x == 42) + d = -0.0; + if (d == 0.0) + return 42; + return 12; +} + +int +main () +{ + if (foo (42, 5.0) != 42 + || foo (42, 0.0) != 42 + || foo (42, -0.0) != 42 + || foo (10, 5.0) != 12 + || foo (10, 0.0) != 42 + || foo (10, -0.0) != 42) + __builtin_abort (); +}