From patchwork Sat Nov 25 10:17:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 169693 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp1847782vqx; Sat, 25 Nov 2023 02:18:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHsXrdpAxGmdtqLHrDjHe0U+yTznWnCetN89sBcjdQGyXu36ilt1jIzzSBnhdlYYGVhPPpf X-Received: by 2002:a05:620a:8e05:b0:77d:761a:573d with SMTP id re5-20020a05620a8e0500b0077d761a573dmr5307908qkn.5.1700907507855; Sat, 25 Nov 2023 02:18:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700907507; cv=pass; d=google.com; s=arc-20160816; b=MH98t+kjwwK1FAGRrQ/SnicfoKw/htaxbQc8L+h/FOzlQdp/QAFpbXYSqvuzEVxGit avaaXxqMJvKweUBxYYmoHaQhGkO7sJqyZg+pNst8JXCgCt2HwnqRyg4cKbmEp+tkh+Rg 5lurnVYUUHAoP0x773p8Ew0s2QwDRtD8HapAVTpU5Ns7YffzKQ9cbN6AXkMPriExh/Gv uX4vicSebH1UoCR9GaPRtWKYAh/hYV15mHQq8FhKk7t58+MMYNeRiTarfTsP6kDuwLhG LtZvMQ0jyXMcvmvDjtSqQfoys055p+QSgwQoUuwYkSPKCdY5Phd6GwhIqBC2aCUaNDsh Tjrw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=WTYML8/wwiQ9D8w3LvStcIrY9CAunSncCstv71g3WPs=; fh=YiXCTCu+Oc5wdBpADe+GucY8SH8Id9WCMEE3/maoC7w=; b=jp8axmTX6QjfTGdMg5Rxx2S6JeM/f0H/6BppYnTau+FCvFZQPOVWCvqxhun1FnaXG8 NQyUVf1OaRNpTgEz32HaRL35AVku2E48mtSFKQUYcGR7OWho93uBkBXVRIJCAVAUV/p6 GE9Ap+sBmgfRwceChGDbveAgGeqPkU4I/xCk99UFXC/MyHIIcx/QF1O+xgGB7kLjtOgE PbYX/vpH7K8D4fCzw0Bx5ta/BRtmZK2zc9HRjfIWjtVZazG0RMGfwb/+z0m7uRxpppzl IMHi0IoxITsxZ8XksR6fsc2anhP/bp/Qj8RfLjMCc+c7Inh8P2OAbE6a5J2Io699nRZ0 ULlA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P4yXVMSn; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w28-20020a05620a095c00b0077bcd1f26a0si4917936qkw.163.2023.11.25.02.18.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 02:18:27 -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=@redhat.com header.s=mimecast20190719 header.b=P4yXVMSn; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9DEFA3858412 for ; Sat, 25 Nov 2023 10:18:27 +0000 (GMT) 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 C36783858C62 for ; Sat, 25 Nov 2023 10:18:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C36783858C62 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 C36783858C62 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=1700907484; cv=none; b=FqAXNPQ9XXHxiNIJzGq9i9tFO+1qR6n/VQ81RzXJxXeFfpN4OcTu4Dssc+DWELJXUXZ1Tc9Wq4WBdOAdWAzhl3ICkA0iUvN4oIyryNBVsba4vmxtBL2VbQeQzb8R/ms/vorXWcrioyw5W8uXCzr8EOEcwuwNZ2LMkZFk37OuQio= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700907484; c=relaxed/simple; bh=vnt7jpSd9E3yPw+jQW01Uh4mPXpm9txK+C8iZNGn3yY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=x5MHvLGrxmB+xMTKTbtPyZAdlfQ10rQDrQod76/P0Utp/iI7Z2F9qK6tcJ0ZFPDhEpq0Bu/55acR2jWjQUD1YCM3mg+hyjoPzY/PD97UvzagCBw5SxURNov0oxbl9BvLegHTXShxsDG6FXqvrfk61m75+akMhJXAxcTI0brksco= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700907481; 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=WTYML8/wwiQ9D8w3LvStcIrY9CAunSncCstv71g3WPs=; b=P4yXVMSnFaV7JZF67nt95OtPPjXNGcjqCvXkyRMnO9cwKnWXTlPKBJQGfO/O6SysRxsYsb PeAv5VxwmdC/7xgVhoFt3zFk4pG9nAYeHm4km0LHew5eY/jXYvQGyx89wDxZoA4Zai1Gp1 L5HxLERAapI9gFSz/erpvCwiqe1Lr8s= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-368-lokkq_02P4CA1gDNjY2Sig-1; Sat, 25 Nov 2023 05:17:55 -0500 X-MC-Unique: lokkq_02P4CA1gDNjY2Sig-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED33680F82E; Sat, 25 Nov 2023 10:17:54 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A5DF8492BE7; Sat, 25 Nov 2023 10:17:54 +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 3APAHpYR1782864 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 25 Nov 2023 11:17:52 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3APAHneZ1782863; Sat, 25 Nov 2023 11:17:49 +0100 Date: Sat, 25 Nov 2023 11:17:48 +0100 From: Jakub Jelinek To: Segher Boessenkool , David Edelsohn Cc: gcc-patches@gcc.gnu.org, Tamar Christina , Andrew Pinski Subject: [PATCH] rs6000: Canonicalize copysign (x, -1) back to -abs (x) in the backend [PR112606] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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_H3, RCVD_IN_MSPIKE_WL, 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.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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783530791018004780 X-GMAIL-MSGID: 1783530791018004780 Hi! The middle-end has been changed quite recently to canonicalize -abs (x) to copysign (x, -1) rather than the other way around. While I agree with that at GIMPLE level, since it matches the GIMPLE goal of as few operations as possible for a canonical form (-abs (x) is 2 GIMPLE statements, copysign (x, -1) is just one), I must say I don't really like that being done on RTL as well (or at least not canonicalizing (COPYSIGN x, negative) back to (NEG (ABS x))), because on most targets most of floating point constants need to be loaded from memory, there are a few exceptions but -1 is often not one of them. Anyway, the following patch fixes the rs6000 regression caused by the change in GIMPLE canonicalization (i.e. the desirable one). As rs6000 clearly prefers -abs (x) form because it has a single instruction to do that while it also has copysign instruction, but that requires loading the -1 from memory, the following patch just ensures the copysign expander can actually see the floating point constant and in that case emits the -abs (x) code (or in the hypothetical case of copysign with non-negative constant abs (x) - but there copysign (x, 1) in GIMPLE is canonicalized to abs (x)), otherwise forces the operand to be the expected gpc_reg_operand and does what it did before. Bootstrapped/regtested on powerpc64le-linux, ok for trunk? 2023-11-25 Jakub Jelinek PR target/112606 * config/rs6000/rs6000.md (copysign3): Change predicate of the last argument from gpc_reg_operand to any_operand. If operands[2] is CONST_DOUBLE, emit abs or neg abs depending on its sign, otherwise if it doesn't satisfy gpc_reg_operand, force it to REG using copy_to_mode_reg. Jakub --- gcc/config/rs6000/rs6000.md.jj 2023-10-13 19:34:43.927834877 +0200 +++ gcc/config/rs6000/rs6000.md 2023-11-24 18:54:13.587876170 +0100 @@ -5358,7 +5358,7 @@ (define_expand "copysign3" (set (match_dup 4) (neg:SFDF (abs:SFDF (match_dup 1)))) (set (match_operand:SFDF 0 "gpc_reg_operand") - (if_then_else:SFDF (ge (match_operand:SFDF 2 "gpc_reg_operand") + (if_then_else:SFDF (ge (match_operand:SFDF 2 "any_operand") (match_dup 5)) (match_dup 3) (match_dup 4)))] @@ -5369,6 +5369,24 @@ (define_expand "copysign3" || TARGET_CMPB || VECTOR_UNIT_VSX_P (mode))" { + /* Middle-end canonicalizes -fabs (x) to copysign (x, -1), + but PowerPC prefers -fabs (x). */ + if (CONST_DOUBLE_AS_FLOAT_P (operands[2])) + { + if (real_isneg (CONST_DOUBLE_REAL_VALUE (operands[2]))) + { + operands[3] = gen_reg_rtx (mode); + emit_insn (gen_abs2 (operands[3], operands[1])); + emit_insn (gen_neg2 (operands[0], operands[3])); + } + else + emit_insn (gen_abs2 (operands[0], operands[1])); + DONE; + } + + if (!gpc_reg_operand (operands[2], mode)) + operands[2] = copy_to_mode_reg (mode, operands[2]); + if (TARGET_CMPB || VECTOR_UNIT_VSX_P (mode)) { emit_insn (gen_copysign3_fcpsgn (operands[0], operands[1],