From patchwork Fri Dec 8 07:56:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 175671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5304720vqy; Thu, 7 Dec 2023 23:56:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IHUZA5sduOk645K3jaRS7x/vgchyjZ6yom/b+JIhk0c8tRa7uS8X0Wl9sg8q7RQbV4/ccX0 X-Received: by 2002:ac8:5b88:0:b0:425:9d74:37b7 with SMTP id a8-20020ac85b88000000b004259d7437b7mr822372qta.127.1702022195016; Thu, 07 Dec 2023 23:56:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702022195; cv=pass; d=google.com; s=arc-20160816; b=gqPOMhmv9Ja1G3jBpsOgD/ddUY4zt3zYzlC6/BdCs7iC9fjh3BVeLwy0AVAiJrRkPW Y8cgdsW18xz0m3Ce3Ky45HaaRLTYw4BtjFNapNx7hV/45Pk0lr/2w/qsaG/nRhmAlbRm P2p1sHFCNOXJ/7IT1Q0AdzrD5lp5oOBZ/1ej04w9JSTjetW8fF2kK5DOh82sl1dOnc2t 8WIZe+/bcUlkBskHF8T4bT3E3UDdMkJ1OUP/yXLkUhZP+0X+XbJYWyxLj/j57920+BVT d0NEXrfjPZfXfeoliRY8icX8Gfl+sGAO6ZuBs6QzC90JsPWJ3TerLZrqEKJZfJMaTUUc M+xw== 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=O0nlAyntV0MjDvB/v8UTfpeFO0pQjPXoqNioKCv4TNk=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=Zo98XA6nAwRFhajEk7C7I8tAEPmkx/CS7Dy0gNodeJD47FV5yi8taWQI5P09eQErhX s/7Cg3DrTMYg9HPjdXKiryi44Wj0hEjVWWNswC43uqaTNLU9/sdHjDhD2zEafP2qsIiO /YbqAyaaGbPqMXkmrgEVIPQHzTi30JfTBHIGb1ByW8cY3A3j0WddKtMpcK95S97Hto98 w2tuuytErRcy0sujW7qyIxkNo9xOXZGxqSOluO3CBLvj2bKDlM0naaaIbdbqyf0Aovz1 WcvI/xBSUFdlRjp6r5SZK+A9npbRspdSAjdatcFepsTZR3MCLrCjtyyO6uhmqktVOzKX X+LQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HCFiRX81; 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 jx8-20020a05622a810800b00423dfac0cbcsi1504951qtb.255.2023.12.07.23.56.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 23:56:35 -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=HCFiRX81; 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 C76D73857BAE for ; Fri, 8 Dec 2023 07:56:34 +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 B9D2B385843B for ; Fri, 8 Dec 2023 07:56:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9D2B385843B 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 B9D2B385843B 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=1702022170; cv=none; b=o2zKH6WluQgprut+VvJHfzI6zuleaUHtzsEjyUxNbXWDso/SI6gFk2fzPA0sniuZTImlzVXjJxZAlyxDW/Wu3kTeb6J6lPoFAE3tNJZwpO910yWp0Ir/6FtpCESnKPYRYGkkzCAkLCncg3bH9/Psgp0Ifc7vO8OqQYboLT1Stxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702022170; c=relaxed/simple; bh=T7i2nAIhu0rsIjTuTdplv/v/+NfIsK5zKeXyA9P1JmI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=g1bdf/nSkiQlQV4P4lyVDRfFFTyhxAxc3zQiEUn6aK0LzxLhcvWqOOahXlOtuh/YYXk2+382UGDQflQyLVo5YucAHjlItBd7s4LxSkW41iHzgP6dDSQ3q2QzX1g341ZKGpB9idkHI4DbXZol7cg1WyAlBhOXQareVbAkRFvngSQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702022167; 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=O0nlAyntV0MjDvB/v8UTfpeFO0pQjPXoqNioKCv4TNk=; b=HCFiRX812rnlSe5oBWg+7mr0RavNj19voY9lHBmeI19277C0hW00Hk9LsxaGnRQ3x89Ap6 Oz3A1iNd/Y3sKQe0/+IqTzmiqK4NQqha0Qn5p4uDmXmVUjqRTC/BpB3WJ0p17kiC68/ByA nQd2GzAAYXQCgZ3gXc15GycwcSkVOU4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-610-F-VlQM28NDe0wOja2EWb1A-1; Fri, 08 Dec 2023 02:56:04 -0500 X-MC-Unique: F-VlQM28NDe0wOja2EWb1A-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 30E3D1C05AD3; Fri, 8 Dec 2023 07:56:04 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.157]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E9F79492BC6; Fri, 8 Dec 2023 07:56:03 +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 3B87u1n52911191 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 8 Dec 2023 08:56:01 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3B87u02D2911190; Fri, 8 Dec 2023 08:56:00 +0100 Date: Fri, 8 Dec 2023 08:56:00 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Avoid merging non-mergeable stmt with cast and mergeable stmt [PR112902] 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.6 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: 1784699625041577051 X-GMAIL-MSGID: 1784699625041577051 Hi! Before bitint lowering, the IL has: b.0_1 = b; _2 = -b.0_1; _3 = (unsigned _BitInt(512)) _2; a.1_4 = a; a.2_5 = (unsigned _BitInt(512)) a.1_4; _6 = _3 * a.2_5; on the first function. Now, gimple_lower_bitint has an optimization (when not -O0) that it avoids assigning underlying VAR_DECLs for certain SSA_NAMEs where it is possible to lower it in a single loop (or straight line code) rather than in multiple loops. So, e.g. the multiplication above uses handle_operand_addr, which can deal with INTEGER_CST arguments, loads but also casts, so it is fine not to assign an underlying VAR_DECL for SSA_NAMEs a.1_4 and a.2_5, as the multiplication can handle it fine. The more problematic case is the other multiplication operand. It is again a result of a (in this case narrowing) cast, so it is fine not to assign VAR_DECL for _3. Normally we can merge the load (b.0_1) with the negation (_2) and even with the following cast (_3). If _3 was used in a mergeable operation like addition, subtraction, negation, &|^ or equality comparison, all of b.0_1, _2 and _3 could be without underlying VAR_DECLs. The problem is that the current code does that even when the cast is used by a non-mergeable operation, and handle_operand_addr certainly can't handle the mergeable operations feeding the rhs1 of the cast, for multiplication we don't emit any loop in which it could appear, for other operations like shifts or non-equality comparisons we emit loops, but either in the reverse direction or with unpredictable indexes (for shifts). So, in order to lower the above correctly, we need to have an underlying VAR_DECL for either _2 or _3; if we choose _2, then the load and negation would be done in one loop and extension handled as part of the multiplication, if we choose _3, then the load, negation and cast are done in one loop and the multiplication just uses the underlying VAR_DECL computed by that. It is far easier to do this for _3, which is what the following patch implements. It actually already had code for most of it, just it did that for widening casts only (optimize unless the cast rhs1 is not SSA_NAME, or is SSA_NAME defined in some other bb, or with more than one use, etc.). This falls through into such code even for the narrowing or same precision casts, unless the cast is used in a mergeable operation. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-08 Jakub Jelinek PR tree-optimization/112902 * gimple-lower-bitint.cc (gimple_lower_bitint): For a narrowing or same precision cast don't set SSA_NAME_VERSION in m_names only if use_stmt is mergeable_op or fall through into the check that use is a store or rhs1 is not mergeable or other reasons prevent merging. * gcc.dg/bitint-52.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-12-06 09:55:18.522993378 +0100 +++ gcc/gimple-lower-bitint.cc 2023-12-07 18:05:17.183692049 +0100 @@ -5989,10 +5989,11 @@ gimple_lower_bitint (void) { if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE || (bitint_precision_kind (TREE_TYPE (rhs1)) - < bitint_prec_large) - || (TYPE_PRECISION (TREE_TYPE (rhs1)) - >= TYPE_PRECISION (TREE_TYPE (s))) - || mergeable_op (SSA_NAME_DEF_STMT (s))) + < bitint_prec_large)) + continue; + if ((TYPE_PRECISION (TREE_TYPE (rhs1)) + >= TYPE_PRECISION (TREE_TYPE (s))) + && mergeable_op (use_stmt)) continue; /* Prevent merging a widening non-mergeable cast on result of some narrower mergeable op @@ -6011,7 +6012,9 @@ gimple_lower_bitint (void) || !mergeable_op (SSA_NAME_DEF_STMT (rhs1)) || gimple_store_p (use_stmt)) continue; - if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1))) + if ((TYPE_PRECISION (TREE_TYPE (rhs1)) + < TYPE_PRECISION (TREE_TYPE (s))) + && gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1))) { /* Another exception is if the widening cast is from mergeable same precision cast from something --- gcc/testsuite/gcc.dg/bitint-52.c.jj 2023-12-08 00:35:39.970953164 +0100 +++ gcc/testsuite/gcc.dg/bitint-52.c 2023-12-08 00:35:21.983205440 +0100 @@ -0,0 +1,22 @@ +/* PR tree-optimization/112902 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +double c; +#if __BITINT_MAXWIDTH__ >= 2048 +_BitInt (512) a; +_BitInt (2048) b; + +void +foo (void) +{ + b = __builtin_mul_overflow_p (40, (_BitInt (512)) (-b * a), 0); +} + + +void +bar (void) +{ + c -= (unsigned _BitInt (512)) (a | a << b); +} +#endif