From patchwork Sun Jul 21 10:32:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 94293 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 C96793861002 for ; Sun, 21 Jul 2024 10:33:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 8F58A386075E for ; Sun, 21 Jul 2024 10:33:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8F58A386075E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8F58A386075E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721557985; cv=none; b=tm239PBT32p8FL4YCVPbLovXpisLD3nmQ2YPUIowVwc65efQdpUDLUj9NQOfOajvCzBxRofbSGo/vzQmcPbspachFjJ8QqH1cU4FGg1cHyl0WZ2I1cYF63ixhhroP9Rq44I4aSci39lmr46xDE5HreFM6+OWlnZZBoUEQudppmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721557985; c=relaxed/simple; bh=OrbAw7tH62eJYDxTJFK+jd326/iCRYqwEdBuEUHrr4I=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=pX6qnKmObHuf0zQG3AFUu9uSROCvnPUEIf272GPxV+TSRzeEPT4tZdtQkBYbMhxUuZjbdtRNt9n7+gDjZQCZbp+AR+MpLxL0SAx9hmEh/6z0mw40t5J0/NnHrNrAXpvy9gOhshBtcn2ILQDta4L2tudQF7iaB8RvNLratKjuuOs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 76E161F898 for ; Sun, 21 Jul 2024 10:33:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721557982; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=oWI20OERKAyWn3aAhUvbb77rRIyeI7ZYtaoimhX0g0w=; b=Q3cHOs8WH4c2D7JwQ/uLYDsnjTX2bDfiWWwYKMNlUGcyEJeN0lie9IMrBhLOfgy4WPkB9T 4r5pkhSCkSxdUZNHe7G2h6QW7aEpDP75Mv1mXeK/wqejTT/pBF6BncOEtwV31FuWdB6WzT QlpLzmli0UT+vTDOduX5/FDS39CFPoQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721557982; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=oWI20OERKAyWn3aAhUvbb77rRIyeI7ZYtaoimhX0g0w=; b=91ifjnAPy/oqgfxONzOcT80iVKSnIWewHYV10Qniljy3ychyXBMm/McRsayBGUgZnSA7Vq hsW4j8ZTvebPt7Dg== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721557982; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=oWI20OERKAyWn3aAhUvbb77rRIyeI7ZYtaoimhX0g0w=; b=Q3cHOs8WH4c2D7JwQ/uLYDsnjTX2bDfiWWwYKMNlUGcyEJeN0lie9IMrBhLOfgy4WPkB9T 4r5pkhSCkSxdUZNHe7G2h6QW7aEpDP75Mv1mXeK/wqejTT/pBF6BncOEtwV31FuWdB6WzT QlpLzmli0UT+vTDOduX5/FDS39CFPoQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721557982; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=oWI20OERKAyWn3aAhUvbb77rRIyeI7ZYtaoimhX0g0w=; b=91ifjnAPy/oqgfxONzOcT80iVKSnIWewHYV10Qniljy3ychyXBMm/McRsayBGUgZnSA7Vq hsW4j8ZTvebPt7Dg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 5AE8F13ABD for ; Sun, 21 Jul 2024 10:33:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id IWKFFN7jnGY4NwAAD6G6ig (envelope-from ) for ; Sun, 21 Jul 2024 10:33:02 +0000 Date: Sun, 21 Jul 2024 12:32:53 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH][RFC] tree-optimization/114659 - VN and FP to int punning MIME-Version: 1.0 Message-Id: <20240721103302.5AE8F13ABD@imap1.dmz-prg2.suse.org> X-Spam-Score: -4.10 X-Spamd-Result: default: False [-4.10 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo] X-Spam-Level: X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org The following addresses another case where x87 FP loads mangle the bit representation and thus are not suitable for a representative in other types. VN was value-numbering a later integer load of 'x' as the same as a former float load of 'x'. The following disables this when the result is not known constant. This now regresses gcc.dg/tree-ssa/ssa-fre-7.c but for x87 float the optimization might elide a FP load/store "noop" move that isn't noop on x87 and thus the desired transform is invalid. Nevertheless it's bad to pessimize all targets for this. I was wondering if it's possible to key this on reg_raw_mode[] but that needs a hard register number (and suspiciously the array has no DFmode or SFmode on x86_64 but only XFmode). So would this need a new target hook? Should this use some other mechanism to query for the correctness of performing the load in another mode and then punning to the destination mode? Bootstrap and regtest running on x86_64-unknown-linux-gnu. PR tree-optimization/114659 * tree-ssa-sccvn.cc (visit_reference_op_load): Do not pun from a scalar floating point mode load to a different type unless we can do so by constnat folding. * gcc.target/i386/pr114659.c: New testcase. --- gcc/testsuite/gcc.target/i386/pr114659.c | 62 ++++++++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 7 +++ 2 files changed, 69 insertions(+) create mode 100644 gcc/testsuite/gcc.target/i386/pr114659.c diff --git a/gcc/testsuite/gcc.target/i386/pr114659.c b/gcc/testsuite/gcc.target/i386/pr114659.c new file mode 100644 index 00000000000..e1e24d55687 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr114659.c @@ -0,0 +1,62 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +int +my_totalorderf (float const *x, float const *y) +{ + int xs = __builtin_signbit (*x); + int ys = __builtin_signbit (*y); + if (!xs != !ys) + return xs; + + int xn = __builtin_isnan (*x); + int yn = __builtin_isnan (*y); + if (!xn != !yn) + return !xn == !xs; + if (!xn) + return *x <= *y; + + unsigned int extended_sign = -!!xs; + union { unsigned int i; float f; } xu = {0}, yu = {0}; + __builtin_memcpy (&xu.f, x, sizeof (float)); + __builtin_memcpy (&yu.f, y, sizeof (float)); + return (xu.i ^ extended_sign) <= (yu.i ^ extended_sign); +} + +static float +positive_NaNf () +{ + float volatile nan = 0.0f / 0.0f; + return (__builtin_signbit (nan) ? - nan : nan); +} + +typedef union { float value; unsigned int word[1]; } memory_float; + +static memory_float +construct_memory_SNaNf (float quiet_value) +{ + memory_float m; + m.value = quiet_value; + m.word[0] ^= (unsigned int) 1 << 22; + m.word[0] |= (unsigned int) 1; + return m; +} + +memory_float x[7] = + { + { 0 }, + { 1e-5 }, + { 1 }, + { 1e37 }, + { 1.0f / 0.0f }, + }; + +int +main () +{ + x[5] = construct_memory_SNaNf (positive_NaNf ()); + x[6] = (memory_float) { positive_NaNf () }; + if (! my_totalorderf (&x[5].value, &x[6].value)) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 0139f1b4e30..62f3de11b56 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -5825,6 +5825,13 @@ visit_reference_op_load (tree lhs, tree op, gimple *stmt) result = NULL_TREE; else if (CONSTANT_CLASS_P (result)) result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result); + /* Do not treat a float-mode load as preserving the bit + representation. See PR114659, on for x87 FP modes there + is no load instruction that does not at least turn sNaNs + into qNaNs. But allow the case of a constant FP value we an + fold above. */ + else if (SCALAR_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (result)))) + result = NULL_TREE; else { /* We will be setting the value number of lhs to the value number