From patchwork Tue Jun 20 16:45:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 110615 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3798877vqr; Tue, 20 Jun 2023 09:46:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6nVmXLbHsdip0Vuh841nRy0qh6fFwpmrRTv6mlkoQVVV7Pm9zbMUioCdVP25YoBFVlGYmt X-Received: by 2002:a17:907:2da4:b0:988:76ed:e563 with SMTP id gt36-20020a1709072da400b0098876ede563mr7569816ejc.50.1687279578656; Tue, 20 Jun 2023 09:46:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687279578; cv=none; d=google.com; s=arc-20160816; b=vMOkEhc6MlbNmk5S+O9h+HRX9Bljrq5ePw2QTxSLu61RAMmsB4nZICmEVUp48gXZe2 TuzhbMoq8AD54aqEDpiTQnBbLdz6GDyDdaGJdBuuX3j0mxSj3i8V3t37XqwRkE1y3YUQ 1LR2QH7X2pRBga5hSx8NeFzIrpoi6u33MLic8QkHRLRkize6FKNNEzgn1/5qAY/b4wlf zRafZexi7BD4aABUR3TB22qwU2XaqioR8PXiNHt4JkDyGcdqKWSRv157Y+ob1p5XV22R ybxsUETN0NQXhAcvRkac0VOIG6a84sxOjPB4ZoNqryMb05+4ZhjVP36HMPKBmGp6zmoO 5ZvQ== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ljK/WklQUtjter5ygslFi+9AFevz7YmXRHdqEsHQIj0=; b=DwayS1DWesL0Dhp4kZ4GczQMC7xBGnckbcMaNYYisLfL3v6xZVc+9kO0X+BOB0jEWw eKEKRHgboAYONRzXg12p6NzKbQ79gRbcXLsvGpSGgFTXQYn7N0z63TPsO1Cs3aemWpsZ t/R2qCnwW/7lZ6aZXYIbpkHj7GPNEzET+LCwDRV9bQ7YNcDhWAsvU+JG+ZAFry8JGNB1 8M2UJ1i15NDPoIKzr+ImlBc4HbfNxDFHJRoF36kBWdwzfjxuENJahwpYqT8CUYezwph1 2O7RXvlU24ulkGsQnW/uCI43zDoM3AZePuLU6ekbzgjUNVYpel68PvQ6U7UfzuBILgsQ IPKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HqCXSJAK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id lc15-20020a170906f90f00b009829d1e46aasi1346646ejb.708.2023.06.20.09.46.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 09:46:18 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HqCXSJAK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 548C83858D1E for ; Tue, 20 Jun 2023 16:46:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 548C83858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687279577; bh=ljK/WklQUtjter5ygslFi+9AFevz7YmXRHdqEsHQIj0=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HqCXSJAKZ+BYVAqCO4j6JsdmiQ6JdwJCflswI46fg9Z8NAxRnHj1zB0KoKeIqVmpR TCtWZEB9GaXi1zcMGa0l0/ehHZ8kUFKGYWqtOTR98ggDHXHZwUKnzLw87w+5lNMUOl OaNaCQk0spnwXXmHx2k1gdP1MTp58X3yliZQz8/s= 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 51C503857C51 for ; Tue, 20 Jun 2023 16:45:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51C503857C51 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-64-9fG8OODTMHiBK-KHCk3u7g-1; Tue, 20 Jun 2023 12:45:30 -0400 X-MC-Unique: 9fG8OODTMHiBK-KHCk3u7g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 178EB185A7AE; Tue, 20 Jun 2023 16:45:30 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CF954200A398; Tue, 20 Jun 2023 16:45:29 +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 35KGjRne4083047 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 20 Jun 2023 18:45:27 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 35KGjQqX4083046; Tue, 20 Jun 2023 18:45:26 +0200 Date: Tue, 20 Jun 2023 18:45:25 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Small uaddc/usubc pattern matching improvement [PR79173] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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_H5, 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.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?1769240871670225558?= X-GMAIL-MSGID: =?utf-8?q?1769240871670225558?= Hi! In the following testcase we fail to pattern recognize the least significant .UADDC call. The reason is that arg3 in that case is _3 = .ADD_OVERFLOW (...); _2 = __imag__ _3; _1 = _2 != 0; arg3 = (unsigned long) _1; and while before the changes arg3 has a single use in some .ADD_OVERFLOW later on, we add a .UADDC call next to it (and gsi_remove/gsi_replace only what is strictly necessary and leave quite a few dead stmts around which next DCE cleans up) and so it all of sudden isn't used just once, but twice (.ADD_OVERFLOW and .UADDC) and so uaddc_cast fails. While we could tweak uaddc_cast and not require has_single_use in these uses, there is also no vrp that would figure out that because __imag__ _3 is in [0, 1] range, it can just use arg3 = __imag__ _3; and drop the comparison and cast. We already search if either arg2 or arg3 is ultimately set from __imag__ of .{{ADD,SUB}_OVERFLOW,U{ADD,SUB}C} call, so the following patch just remembers the lhs of __imag__ from that case and uses it later. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-06-20 Jakub Jelinek PR middle-end/79173 * tree-ssa-math-opts.cc (match_uaddc_usubc): Remember lhs of IMAGPART_EXPR of arg2/arg3 and use that as arg3 if it has the right type. * g++.target/i386/pr79173-1.C: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2023-06-20 08:57:38.000000000 +0200 +++ gcc/tree-ssa-math-opts.cc 2023-06-20 10:33:52.969805538 +0200 @@ -4728,6 +4728,7 @@ match_uaddc_usubc (gimple_stmt_iterator if (!types_compatible_p (type, TREE_TYPE (arg1))) return false; int kind[2] = { 0, 0 }; + tree arg_im[2] = { NULL_TREE, NULL_TREE }; /* At least one of arg2 and arg3 should have type compatible with arg1/rhs[0], and the other one should have value in [0, 1] range. If both are in [0, 1] range and type compatible with @@ -4758,6 +4759,7 @@ match_uaddc_usubc (gimple_stmt_iterator g = uaddc_ne0 (g); if (!uaddc_is_cplxpart (g, IMAGPART_EXPR)) continue; + arg_im[i] = gimple_assign_lhs (g); g = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (g), 0)); if (!is_gimple_call (g) || !gimple_call_internal_p (g)) continue; @@ -4781,6 +4783,7 @@ match_uaddc_usubc (gimple_stmt_iterator { std::swap (arg2, arg3); std::swap (kind[0], kind[1]); + std::swap (arg_im[0], arg_im[1]); } if ((kind[0] & 1) == 0 || (kind[1] & 6) == 0) return false; @@ -4810,6 +4813,8 @@ match_uaddc_usubc (gimple_stmt_iterator /* Build .UADDC/.USUBC call which will be placed before the stmt. */ gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf2); gimple *g; + if ((kind[1] & 4) != 0 && types_compatible_p (type, TREE_TYPE (arg_im[1]))) + arg3 = arg_im[1]; if ((kind[1] & 1) == 0) { if (TREE_CODE (arg3) == INTEGER_CST) --- gcc/testsuite/g++.target/i386/pr79173-1.C.jj 2023-06-20 09:44:37.515578731 +0200 +++ gcc/testsuite/g++.target/i386/pr79173-1.C 2023-06-20 10:35:33.650418101 +0200 @@ -0,0 +1,33 @@ +// PR middle-end/79173 +// { dg-do compile { target c++11 } } +// { dg-options "-O2 -fno-stack-protector -masm=att" } +// { dg-final { scan-assembler-times "addq\t%r\[^\n\r]*, \\\(%rdi\\\)" 1 { target lp64 } } } +// { dg-final { scan-assembler-times "adcq\t%r\[^\n\r]*, 8\\\(%rdi\\\)" 1 { target lp64 } } } +// { dg-final { scan-assembler-times "adcq\t%r\[^\n\r]*, 16\\\(%rdi\\\)" 1 { target lp64 } } } +// { dg-final { scan-assembler-times "adcq\t%r\[^\n\r]*, 24\\\(%rdi\\\)" 1 { target lp64 } } } +// { dg-final { scan-assembler-times "addl\t%e\[^\n\r]*, \\\(%e\[^\n\r]*\\\)" 1 { target ia32 } } } +// { dg-final { scan-assembler-times "adcl\t%e\[^\n\r]*, 4\\\(%e\[^\n\r]*\\\)" 1 { target ia32 } } } +// { dg-final { scan-assembler-times "adcl\t%e\[^\n\r]*, 8\\\(%e\[^\n\r]*\\\)" 1 { target ia32 } } } +// { dg-final { scan-assembler-times "adcl\t%e\[^\n\r]*, 12\\\(%e\[^\n\r]*\\\)" 1 { target ia32 } } } + +template +inline constexpr T +uaddc (T x, T y, T carry_in, T &carry_out) noexcept +{ + [[gnu::assume (carry_in <= 1)]]; + x += y; + carry_out = x < y; + x += carry_in; + carry_out += x < carry_in; + return x; +} + +void +foo (unsigned long *p, unsigned long *q) +{ + unsigned long c; + p[0] = uaddc (p[0], q[0], 0UL, c); + p[1] = uaddc (p[1], q[1], c, c); + p[2] = uaddc (p[2], q[2], c, c); + p[3] = uaddc (p[3], q[3], c, c); +}