From patchwork Mon Dec 5 10:29:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 29632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2168619wrr; Mon, 5 Dec 2022 02:30:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf4aVy1py6IuToXkmJmepSsVl7Y8Dj+qOa12QC5cMnhW4hL6YM8hdC57ATxrfcA6jbmmLe8b X-Received: by 2002:a17:906:1e55:b0:7ad:a0cb:f79e with SMTP id i21-20020a1709061e5500b007ada0cbf79emr51162150ejj.458.1670236208058; Mon, 05 Dec 2022 02:30:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670236208; cv=none; d=google.com; s=arc-20160816; b=xPbuGFCMcaFzU4v593TIF+JZDi1U46lfgiSHZcm3ASpzwQiV3gdMW1VB40Z/lqEyKo NsSS5E+7BMo30E+LgcQJLJL1jMw3ynkkx+dBDu9QfR+/4xFfHpswdHyhebJ5mpgPrvfl qcW73zbeHHNwiIJmNSzj6OebQE+MVqns0uy/aWp+2h6imOTBBIcqkkosKPir8cVkVpVG kt8hGvjHk0mUevpbpDLjlkqCpicUX3J67l/UX6kg6/YMKIe8zC+PXl3XKbmh8cHHy/i3 qD3HK18gICaDxCrqip8MjSz6MoDgjbgxd5eI5CUF+0yVv4ifxUun1e9eNT3iuzXceoen DXvA== 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-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=eaAbc5kYvJOxvAMC0jkuIanHJQJ1RFdMHYS90kcNV7w=; b=YjpkpxqUcIZRphFVfpELHGvLaC+EPeL+TuNsDKNXtucnSdFAkNqi4vwMdkkHaJeDHh vRuqONlMBkSb3Ml1xNbL6q4ooXAKaKgtCn0kgddUXIMQf6hkzG3mjB2VBr6akgFUkpgi PKw1EsOsz0pTwWIsv36lLkNGjHOutquLV/NTQW21lMab+SY3afFEfgSwh2ZN/l0NtMgq 1nLAuiAF5KWMdpypoUJR2XUPJikkvyQIU01FSUPyjcACgPCFhicb7FtWqUi1yrshE5AK nzNz646jQluffm0eiitMNaVZGNEwhBNQhaQVQxydOxXRymEyPPmc31A6qvkjbP2akWLs mUuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WVDUvdp8; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sh17-20020a1709076e9100b007b7eb087819si11429357ejc.200.2022.12.05.02.30.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 02:30:08 -0800 (PST) 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=WVDUvdp8; 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 CEDF438983A9 for ; Mon, 5 Dec 2022 10:30:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CEDF438983A9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670236206; bh=eaAbc5kYvJOxvAMC0jkuIanHJQJ1RFdMHYS90kcNV7w=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=WVDUvdp8f2m8KkVR9/QaT1+WKHrv1o90LLFBgghVISC3fhkiTDsbjt7xKCsI5FLbX uw3ExIShPojDd2b4osmZDFqmK5vPPPDbuFXnITVMUXGbJQmx9BYy9PU8tSbkkMh4Ii XhnutkELNue+odzLZGVngGcVId90e1eo9pVxQyz0= 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 2AEF6382FCB9 for ; Mon, 5 Dec 2022 10:29:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2AEF6382FCB9 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-219-PZD2URW_OK2N22hNrCi6wg-1; Mon, 05 Dec 2022 05:29:21 -0500 X-MC-Unique: PZD2URW_OK2N22hNrCi6wg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0C180101A54E; Mon, 5 Dec 2022 10:29:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 54F81C1908B; Mon, 5 Dec 2022 10:29:20 +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 2B5ATDNF3411356 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 5 Dec 2022 11:29:14 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2B5ATCSG3411355; Mon, 5 Dec 2022 11:29:12 +0100 Date: Mon, 5 Dec 2022 11:29:12 +0100 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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?1751369601694772865?= X-GMAIL-MSGID: =?utf-8?q?1751369601694772865?= Hi! As reported in the PR, the following pr106805.c testcase is miscompiled with the default -ftrapping-math, because we fold all the comparisons into constants and don't raise any exceptions. The match.pd pattern handles just simple comparisons, from those EQ/NE are quiet and don't raise exceptions on anything but sNaN, while GT/GE/LT/LE are signaling and do raise exceptions even on qNaN. fold_relational_const handles this IMHO correctly: /* Handle the cases where either operand is a NaN. */ if (real_isnan (c0) || real_isnan (c1)) { switch (code) { case EQ_EXPR: case ORDERED_EXPR: result = 0; break; case NE_EXPR: case UNORDERED_EXPR: case UNLT_EXPR: case UNLE_EXPR: case UNGT_EXPR: case UNGE_EXPR: case UNEQ_EXPR: result = 1; break; case LT_EXPR: case LE_EXPR: case GT_EXPR: case GE_EXPR: case LTGT_EXPR: if (flag_trapping_math) return NULL_TREE; result = 0; break; default: gcc_unreachable (); } return constant_boolean_node (result, type); } by folding the signaling comparisons only if -fno-trapping-math. The following patch does the same in match.pd. Unfortunately the pr106805.c testcase still fails, but no longer because of match.pd, but on the trunk because of the still unresolved ranger problems (same issue as for fold-overflow-1.c etc.) and on 12 branch (and presumably trunk too) somewhere during expansion the comparisons are also expanded into constants (which is ok for -fno-trapping-math, but not ok with that). Though, I think the patch is a small step in the direction, so I'd like to commit this patch without the gcc.dg/pr106805.c testcase for now. Bootstrapped/regtested on x86_64-linux and i686-linux, appart from the +FAIL: gcc.dg/pr106805.c execution test nothing else appears. Ok for trunk without the last testcase? 2022-12-05 Jakub Jelinek PR middle-end/106805 * match.pd (cmp @0 REAL_CST@1): Don't optimize x cmp NaN or NaN cmp x to false/true for cmp >/>=/ QNAN; - /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_1 = \\(float\\)" "original" } } */ } { volatile int nonfinite_2; nonfinite_2 = (float) x >= QNAN; - /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_2 = \\(float\\)" "original" } } */ } { volatile int nonfinite_3; nonfinite_3 = (float) x < QNAN; - /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_3 = \\(float\\)" "original" } } */ } { volatile int nonfinite_4; nonfinite_4 = (float) x <= QNAN; - /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_4 = \\(float\\)" "original" } } */ } { --- gcc/testsuite/c-c++-common/pr57371-5.c.jj 2022-12-02 13:34:14.397714696 +0100 +++ gcc/testsuite/c-c++-common/pr57371-5.c 2022-12-02 13:35:05.915949599 +0100 @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fno-signaling-nans -fno-trapping-math -fdump-tree-original" } */ + +/* We can not get rid of comparison in tests below because of + pending NaN exceptions. + + TODO: avoid under -fno-trapping-math. */ + +#define QNAN __builtin_nanf ("0") + +void nonfinite(unsigned short x) { + { + volatile int nonfinite_1; + nonfinite_1 = (float) x > QNAN; + /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */ + } + + { + volatile int nonfinite_2; + nonfinite_2 = (float) x >= QNAN; + /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */ + } + + { + volatile int nonfinite_3; + nonfinite_3 = (float) x < QNAN; + /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */ + } + + { + volatile int nonfinite_4; + nonfinite_4 = (float) x <= QNAN; + /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */ + } + + { + volatile int nonfinite_11; + nonfinite_11 = (float) x == QNAN; + /* { dg-final { scan-tree-dump "nonfinite_11 = 0" "original" } } */ + } + + { + volatile int nonfinite_12; + nonfinite_12 = (float) x != QNAN; + /* { dg-final { scan-tree-dump "nonfinite_12 = 1" "original" } } */ + } +} --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x.jj 2020-01-14 20:02:47.175603962 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x 2022-12-05 10:44:10.037871848 +0100 @@ -1,3 +1,4 @@ +lappend additional_flags "-fno-trapping-math" # The ARM VxWorks kernel uses an external floating-point library in # which routines like __ledf2 are just aliases for __cmpdf2. These # routines therefore don't handle NaNs correctly. --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c.jj 2022-12-05 10:44:21.406705781 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c 2022-12-05 10:44:57.237182398 +0100 @@ -0,0 +1,31 @@ + +const double dnan = 1.0/0.0 - 1.0/0.0; +double x = 1.0; + +extern void link_error (void); +extern void abort (void); + +main () +{ +#if ! defined (__vax__) && ! defined (_CRAY) + /* NaN is an IEEE unordered operand. All these test should be false. */ + if (dnan == dnan) + link_error (); + if (dnan != x) + x = 1.0; + else + link_error (); + + if (dnan == x) + link_error (); +#endif + exit (0); +} + +#ifndef __OPTIMIZE__ +void link_error (void) +{ + abort (); +} +#endif + --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x.jj 2022-12-05 10:44:30.338575309 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x 2022-12-05 10:44:32.991536565 +0100 @@ -0,0 +1,16 @@ +# The ARM VxWorks kernel uses an external floating-point library in +# which routines like __ledf2 are just aliases for __cmpdf2. These +# routines therefore don't handle NaNs correctly. +if [istarget "arm*-*-vxworks*"] { + set torture_eval_before_execute { + global compiler_conditional_xfail_data + set compiler_conditional_xfail_data { + "The ARM kernel uses a flawed floating-point library." + { "*-*-*" } + { "-O0" } + { "-mrtp" } + } + } +} + +return 0 --- gcc/testsuite/gcc.dg/pr106805.c.jj 2022-12-02 13:02:06.813354371 +0100 +++ gcc/testsuite/gcc.dg/pr106805.c 2022-12-02 13:11:42.844792390 +0100 @@ -0,0 +1,29 @@ +/* PR middle-end/106805 */ +/* { dg-do run } */ +/* { dg-options "-O2 -ftrapping-math" } */ +/* { dg-add-options ieee } */ +/* { dg-require-effective-target fenv_exceptions_double } */ + +#include +#include + +static inline int +foo (double x, double y) +{ + return x >= y && x <= y; +} + +int +main () +{ + double x = __builtin_nan (""); + volatile int r; + r = foo (__builtin_nan (""), 1.0); + if (!fetestexcept (FE_INVALID)) + abort (); + feclearexcept (FE_ALL_EXCEPT); + r = foo (x, __builtin_inf ()); + if (!fetestexcept (FE_INVALID)) + abort (); + return 0; +}