From patchwork Thu Aug 10 15:43:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 134054 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp508205vqi; Thu, 10 Aug 2023 08:43:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWvbf1gH2e5iAHkRbaNraHrjcH9diYs/IoDbYBsYApx8Impb/lrtssccBC7gJz5nEXnAGp X-Received: by 2002:a05:6402:2050:b0:51e:ed6:df38 with SMTP id bc16-20020a056402205000b0051e0ed6df38mr2425792edb.13.1691682232559; Thu, 10 Aug 2023 08:43:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691682232; cv=none; d=google.com; s=arc-20160816; b=L7aUE0mssGjzrvEc29upYXenX7XuZvIDE7nYQi7K2Yn1+8/r1eW2aO5FSHq8IaY8Qm Lldev8ZKQ0MiArK6/0QMkOZ8zpX07Gq169EN/hOxczuLxY6hHDJ7IlODSeP2/+gqV0cy Wsue+q62iYWPQQPKFwKTU1wCW/h2o72kmcDK5f1CFlTcZDgyaU2tnBN/5czj4bOo1k2+ NhNyNqmNv9JsWVGrDwfUwBEe8wce0BdKNd+B3tR756XFdxigXSnYDwnxa3B+n/m1lOuo yT9lJLxyZig35PCSX81F7VIMRoBIpKrgeTlx4khzuBK2NdpuNv8B4euTU6YNrCN7M++2 Lv/g== 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-transfer-encoding:content-disposition:in-reply-to :mime-version:references:message-id:subject:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=F7xTKzdZAji+DGCwBkM+dOFVUyfZWrAmznZDEXjvUgM=; fh=dk4XvQPBRj9jh0QV6yJJKF98rJqSYlIMmXHF8jvx9iw=; b=h7VTTYoXwLbObHzjOwwQR4vv9msz+XfX48EoX/ybNHql6BT4adnUd9/wzkpwK2knu/ Oa8haQ/KLBsqZ/xNBrcDwq8vY2shJBr2vwiRDgK1n2ccq4WXkoMCxE7dZv7QB3w5orT7 RCaJNy/yTOjR34kFQeOzRV+l8Jq5HixGznvJo3d/qFSf85h49PLSrKaDswQyEiAJzmSc s9QQ/UwOsxShMGeVaVf85ph7CSq2z5TBgB9jykAw/A2zqKqDPZTLd1Oq8wUTNF/u/Q1k N6QpV3uxA/yqpzcI5fCZuX/6AkQE8Joqc5FbGzQcUHwITC9D3cMlc4/qAGipiUCEMt27 PLLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=w7VG7H7z; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dy24-20020a05640231f800b0052290105ca7si1569170edb.653.2023.08.10.08.43.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 08:43:52 -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=w7VG7H7z; 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 3DBA43857701 for ; Thu, 10 Aug 2023 15:43:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DBA43857701 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691682231; bh=F7xTKzdZAji+DGCwBkM+dOFVUyfZWrAmznZDEXjvUgM=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=w7VG7H7zLchGR6v7pAtClU8agWTWcZu1ZpmpizXB2PzJiRsNKDA4RhcTkFbth9s5e ivTe7inN8NTfPZzSLrI2Pjyz5Buje+uxn4rF63kg/ozx97QfEpgZnWj+IpPQeVr7AA Vh2PZLXzlUGb1FG9NKuzpLhjhpIMDgDkw7MQ8yos= 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 [216.145.221.124]) by sourceware.org (Postfix) with ESMTPS id 7C7313858288 for ; Thu, 10 Aug 2023 15:43:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C7313858288 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-2-OfFCHK1TOCSOuHfwFQq5gQ-1; Thu, 10 Aug 2023 11:43:05 -0400 X-MC-Unique: OfFCHK1TOCSOuHfwFQq5gQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 89C7085CBE5; Thu, 10 Aug 2023 15:43:05 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.169]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4F0E15CBFF; Thu, 10 Aug 2023 15:43:05 +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 37AFh3KI2356904 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 10 Aug 2023 17:43:03 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37AFh2nw2356903; Thu, 10 Aug 2023 17:43:02 +0200 Date: Thu, 10 Aug 2023 17:43:02 +0200 To: Richard Biener , Drew Ross Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd, v2: Implement missed optimization ((x ^ y) & z) | x -> (z & y) | x [PR109938] Message-ID: References: <20230804214817.1256642-1-drross@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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, 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.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: INBOX X-GMAIL-THRID: 1773857388952190527 X-GMAIL-MSGID: 1773857388952190527 Hi! On Thu, Aug 10, 2023 at 12:28:24PM +0200, Jakub Jelinek via Gcc-patches wrote: > On Tue, Aug 08, 2023 at 03:18:51PM +0200, Richard Biener via Gcc-patches wrote: > > On Fri, Aug 4, 2023 at 11:49 PM Drew Ross via Gcc-patches > > wrote: > > > > > > Adds a simplification for ((x ^ y) & z) | x to be folded into > > > (z & y) | x. Merges this simplification with ((x | y) & z) | x -> (z & y) | x > > > to prevent duplicate pattern. Tested successfully on x86_64 and x86 targets. > > > > OK. > > Shouldn't > (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0) > be changed to > (bit_ior:c (nop_convert1?:s > (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3) > rather than > (bit_ior:c (nop_convert1? (bit_and:c (nop_convert2? (op:c @0 @1)) @2)) @3) > in the patch? > I mean the :s modifiers were there for a reason, if some of the > intermediates aren't a single use, then the simplification doesn't simplify > anything and can even make things larger. Here it is in patch form. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-08-10 Drew Ross Jakub Jelinek PR tree-optimization/109938 * match.pd (((x ^ y) & z) | x -> (z & y) | x): New simplification. * gcc.c-torture/execute/pr109938.c: New test. * gcc.dg/tree-ssa/pr109938.c: New test. Jakub --- gcc/match.pd.jj 2023-08-10 09:26:19.390805079 +0200 +++ gcc/match.pd 2023-08-10 13:33:17.959654775 +0200 @@ -1972,10 +1972,14 @@ (define_operator_list SYNC_FETCH_AND_AND (if (bitwise_inverted_equal_p (@0, @2)) (bitop @0 @1)))) -/* ((x | y) & z) | x -> (z & y) | x */ -(simplify - (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0) - (bit_ior (bit_and @2 @1) @0)) +/* ((x | y) & z) | x -> (z & y) | x + ((x ^ y) & z) | x -> (z & y) | x */ +(for op (bit_ior bit_xor) + (simplify + (bit_ior:c (nop_convert1?:s + (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3) + (if (bitwise_equal_p (@0, @3)) + (convert (bit_ior (bit_and @1 (convert @2)) (convert @0)))))) /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ (simplify --- gcc/testsuite/gcc.dg/tree-ssa/pr109938.c.jj 2023-08-10 13:22:19.513095403 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr109938.c 2023-08-10 13:35:24.428841774 +0200 @@ -0,0 +1,125 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse2 -Wno-psabi" } */ + +typedef int v4si __attribute__((vector_size(4 * sizeof(int)))); + +/* Generic */ +__attribute__((noipa)) int +t1 (int a, int b, int c) +{ + return ((a ^ c) & b) | a; +} + +__attribute__((noipa)) unsigned int +t2 (int a, unsigned int b, int c) +{ + return ((a ^ c) & b) | a; +} + +__attribute__((noipa)) unsigned long +t3 (unsigned long a, long b, unsigned long c) +{ + return ((a ^ c) & b) | a; +} + +__attribute__((noipa)) unsigned short +t4 (short a, unsigned short b, unsigned short c) +{ + return (unsigned short) ((a ^ c) & b) | a; +} + +__attribute__((noipa)) unsigned char +t5 (unsigned char a, signed char b, signed char c) +{ + return ((a ^ c) & b) | a; +} + +__attribute__((noipa)) long long +t6 (long long a, long long b, long long c) +{ + return ((a ^ c) & (unsigned long long) b) | a; +} + +/* Gimple */ +__attribute__((noipa)) int +t7 (int a, int b, int c) +{ + int t1 = a ^ c; + int t2 = t1 & b; + int t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) int +t8 (int a, unsigned int b, unsigned int c) +{ + unsigned int t1 = a ^ c; + int t2 = t1 & b; + int t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) unsigned int +t9 (unsigned int a, unsigned int b, int c) +{ + unsigned int t1 = a ^ c; + unsigned int t2 = t1 & b; + unsigned int t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) unsigned long +t10 (unsigned long a, long b, unsigned long c) +{ + unsigned long t1 = a ^ c; + unsigned long t2 = t1 & b; + unsigned long t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) unsigned short +t11 (short a, unsigned short b, short c) +{ + short t1 = a ^ c; + unsigned short t2 = t1 & b; + unsigned short t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) unsigned char +t12 (signed char a, unsigned char b, signed char c) +{ + unsigned char t1 = a ^ c; + unsigned char t2 = t1 & b; + unsigned char t3 = t2 | a; + return t3; +} + +__attribute__((noipa)) unsigned long long +t13 (unsigned long long a, long long b, unsigned long long c) +{ + long long t1 = a ^ c; + long long t2 = t1 & b; + unsigned long long t3 = t2 | a; + return t3; +} + +/* Vectors */ +__attribute__((noipa)) v4si +t14 (v4si a, v4si b, v4si c) +{ + return ((a ^ c) & b) | a; +} + +__attribute__((noipa)) v4si +t15 (v4si a, v4si b, v4si c) +{ + v4si t1 = a ^ c; + v4si t2 = t1 & b; + v4si t3 = t2 | a; + return t3; +} + +/* { dg-final { scan-tree-dump-not " \\\^ " "dse2" } } */ +/* { dg-final { scan-tree-dump-times " \\\| " 15 "dse2" } } */ +/* { dg-final { scan-tree-dump-times " & " 15 "dse2" } } */ --- gcc/testsuite/gcc.c-torture/execute/pr109938.c.jj 2023-08-10 13:22:19.513095403 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr109938.c 2023-08-10 13:22:19.513095403 +0200 @@ -0,0 +1,33 @@ +/* PR tree-opt/109938 */ + +#include "../../gcc.dg/tree-ssa/pr109938.c" + +int +main () +{ + if (t1 (29789, 29477, 23942) != 30045) __builtin_abort (); + if (t2 (-20196, 18743, -32901) != -1729) __builtin_abort (); + if (t3 (2136614690L, 1136698390L, 2123767997L) != 2145003318UL) __builtin_abort (); + if (t4 (-4878, 9977, 23313) != 61171) __builtin_abort (); + if (t5 (127, 99, 43) != 127) __builtin_abort (); + if (t6 (9176690219839792930LL, 3176690219839721234LL, 5671738468274920831LL) + != 9177833729112616754LL) __builtin_abort (); + if (t7 (29789, 29477, 23942) != 30045) __builtin_abort (); + if (t8 (23489, 99477, 87942) != 90053) __builtin_abort (); + if (t9 (10489, 66477, -73313) != 10749) __builtin_abort (); + if (t10 (2136614690L, -1136614690L, 4136614690UL) != 4284131106UL) + __builtin_abort (); + if (t11 (29789, 29477, 12345) != 29821) __builtin_abort (); + if (t12 (-120, 98, -73) != 170) __builtin_abort (); + if (t13 (9176690219839792930ULL, -3176690219839721234LL, 5671738468274920831ULL) + != 9221726284835125102ULL) __builtin_abort (); + v4si a1 = {29789, -20196, 23489, 10489}; + v4si a2 = {29477, 18743, 99477, 66477}; + v4si a3 = {23942, -32901, 87942, -73313}; + v4si r1 = {30045, 63807, 90053, 10749}; + v4si b1 = t14 (a1, a2, a3); + v4si b2 = t15 (a1, a2, a3); + if (__builtin_memcmp (&b1, &r1, sizeof (b1) != 0)) __builtin_abort(); + if (__builtin_memcmp (&b2, &r1, sizeof (b2) != 0)) __builtin_abort(); + return 0; +}