From patchwork Tue Feb 25 12:31:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 107058 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B142B3858C60 for ; Tue, 25 Feb 2025 12:33:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B142B3858C60 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Jkvf2sBT 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 ESMTP id D83263858D26 for ; Tue, 25 Feb 2025 12:31:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D83263858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D83263858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740486706; cv=none; b=gMVtH1nn18j49JKmYGmEbDGEqeB75KKO0tsVi98xnvFb2sIT6kfYYMayhQXUGy4gPIL6O1ImP0nRelCapcKZhNHgy4pmcnm4es+qJvcxc9JGr3SJo3USNOXGsQ9hzhODod/Kk1PogRb5Lxhu7tqxtjN/jg5sm2PTRcvxPcctCxw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740486706; c=relaxed/simple; bh=awl7FClGj2Jr12YJf/jgvfSc59ikF+sUXeU/BHAp7ig=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=YEM1IvhaFCc/kOj4uruGMOg/LitGieh5eaagph1f8bX8IoWni+oMGz2SH5JSOOVEULm6YGmAD4w4crDtI1hGXbNb0tbXSTEx1WQTFKKSfOuXcZRxgiKmQ23U0yeSJa3+2jjNIAXtLmCNt0KW4DkbUpeN9rMzIPFBaQCOXFWOy48= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D83263858D26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740486705; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=17y/xVvoqmRa9sjnxQPVHCKfYjfXeQQ+BKHCo6tNAq0=; b=Jkvf2sBT1CS+Y4UsSQWgo53e23FHVw+4S0nTi0aZCNSUkGB2L1Ma4poV4xs7t6hFph11Ss 87dUVEyOMa37SsMweeGhPCHzf48AtmlMXSd7MEW18L5RyU9KeGQSkMsHzSsxmH7Bh7YuVU Nz0UK0dT5k5TcJhjMpkr2Xfjkew7fb0= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-462-zolsekIiOVinYE8dy24OZQ-1; Tue, 25 Feb 2025 07:31:42 -0500 X-MC-Unique: zolsekIiOVinYE8dy24OZQ-1 X-Mimecast-MFC-AGG-ID: zolsekIiOVinYE8dy24OZQ_1740486701 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 91F1519560BC; Tue, 25 Feb 2025 12:31:40 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.44.32.22]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CF6F71800944; Tue, 25 Feb 2025 12:31:38 +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 51PCVUSl2209171 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 25 Feb 2025 13:31:35 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 51PCVTgj2209170; Tue, 25 Feb 2025 13:31:29 +0100 Date: Tue, 25 Feb 2025 13:31:29 +0100 From: Jakub Jelinek To: Richard Sandiford , Richard Biener , Jeff Law , Eric Botcazou Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] simplify-rtx: Fix up simplify_logical_relational_operation [PR119002] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: dMjsl3ETu3p5GwlkYaNwTcyo-DjFUssov0L1nZOhxyY_1740486701 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.8 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! The following testcase is miscompiled on powerpc64le-linux starting with r15-6777. That change has the if (HONOR_NANS (GET_MODE (XEXP (op0, 0)))) all = 15; lines which work fine if the comparisons use MODE_FLOAT or MODE_INT operands (or say MODE_VECTOR* etc.). But on this testcase on ppc64le during combine we see (set (reg:SI 134) (ior:SI (ge:SI (reg:CCFP 128) (const_int 0 [0])) (lt:SI (reg:CCFP 128) (const_int 0 [0])))) HONOR_NANS is obviously false on CCFPmode, because MODE_HAS_NANS is false, it isn't FLOAT_MODE_P. But still it is a MODE_CC mode used for floating point comparisons and so we need to consider the possibility of unordered operands. I'm not sure how we could look at the setter of those MODE_CC regs from the simplifiers, after all they can happen in the middle of combiner trying to combine multiple instructions. So, instead the following patch attempts to be conservative for MODE_CC with some exceptions. One is flag_finite_math_only, regardless of MODE_HAS_NANS in that case HONOR_NANS will be always false. Another one is for targets which provide REVERSE_CONDITION condition and reverse one way the floating point MODE_CC modes and another the integral ones. If REVERSE_CONDITION for GT gives LE, then unordered is not an option. And finally it searches if there are any scalar floating point modes with MODE_HAS_NANS at all, if not, it is also safe to assume there are no NaNs. Bootstrapped/regtested on x86_64-linux, i686-linux, powerpc64le-linux, aarch64-linux and bootstrapped on s390x-linux (regtest there still pending). Ok for trunk? Or any other ideas how to handle this? 2025-02-24 Jakub Jelinek PR rtl-optimization/119002 * simplify-rtx.cc: Include tm_p.h. (simplify_context::simplify_logical_relational_operation): Set all = 15 also if op0's first operand has MODE_CC mode and it is or could be floating point comparison which honors NaNs. * gcc.c-torture/execute/ieee/pr119002.c: New test. Jakub --- gcc/simplify-rtx.cc.jj 2025-01-15 08:43:39.611918569 +0100 +++ gcc/simplify-rtx.cc 2025-02-24 21:16:09.980758481 +0100 @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. #include "selftest-rtl.h" #include "rtx-vector-builder.h" #include "rtlanal.h" +#include "tm_p.h" /* Simplification and canonicalization of RTL. */ @@ -2675,6 +2676,24 @@ simplify_context::simplify_logical_relat /* See whether the operands might be unordered. */ if (HONOR_NANS (GET_MODE (XEXP (op0, 0)))) all = 15; + else if (GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC + && !flag_finite_math_only) + { + /* HONOR_NANS will be false for MODE_CC comparisons, eventhough + they could actually be floating point. If the mode is + reversible, ask the backend if it could be unordered, otherwise + err on the side of caution and assume it could be unordered + if any supported floating mode honors NaNs. */ + machine_mode mode = GET_MODE (XEXP (op0, 0)); + if (!REVERSIBLE_CC_MODE (mode) + || REVERSE_CONDITION (GT, mode) != LE) + FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT) + if (HONOR_NANS (mode)) + { + all = 15; + break; + } + } mask0 = comparison_to_mask (code0) & all; mask1 = comparison_to_mask (code1) & all; } --- gcc/testsuite/gcc.c-torture/execute/ieee/pr119002.c.jj 2025-02-24 21:18:45.880622627 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/pr119002.c 2025-02-24 21:19:02.418396051 +0100 @@ -0,0 +1,23 @@ +/* PR rtl-optimization/119002 */ + +__attribute__((noipa)) unsigned int +foo (void *x, float y, float z) +{ + unsigned int a, b; + float c, d, e; + c = y; + d = z; + a = c < d; + d = y; + e = z; + b = d >= e; + a |= b; + return a; +} + +int +main () +{ + if (foo ((void *) 0, 0.f, __builtin_nanf (""))) + __builtin_abort (); +}