From patchwork Fri Aug 4 21:48:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Ross X-Patchwork-Id: 131372 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp118064vqr; Fri, 4 Aug 2023 14:49:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmoHBlys8rKGHzXvk5zGCK+8aBrhYBgdqkSXO8K4tL9vr+dxSFPpHnuMTTAQ+X1io2/01A X-Received: by 2002:a17:906:319b:b0:966:17b2:5b0b with SMTP id 27-20020a170906319b00b0096617b25b0bmr2372011ejy.49.1691185755558; Fri, 04 Aug 2023 14:49:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691185755; cv=none; d=google.com; s=arc-20160816; b=lcT7rOrohBJsrY5xuvU84R9aHVtUOZqP/7gGhN/HiE9n//i3KtSaMYooMci6U7xPjg XHv6IlKAkn3kpd2p1Rn0BCRcreTKm/1T+lP8fcyQwVXyKyMPOH1ZY55mzGVLvAgy24zd d2MGc9ADzEJzWzWf3UoTnS+x3CvmlOcfqOi/lZ+m6OoZ5W4mr6UlYZPdkV2R2SvLybTX YAvQBqjDC3+pzCPTrGXog9OfvZao6vPBikrsY/+00eE9U1J/JuPMnUOxBKUKiJO42yT+ eKris2DKHREiy7AoWsrxMYLGXQTJy02ipcvRqDS05ihdgleTOKIJqZ8s4Str9FiHmTbX 5PoA== 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:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0AE1UVjdtf0/dFmwzEfh45tdu0XWHe9L3S1Lo1MWxxc=; fh=+7oqyIxxdDZHhy0RvojIG59KP1WcqOWasYDHPRLsigU=; b=FmFex6TxBvML9UiEf8Vknq9M4+Yu65hl9BLqtau37FjBpQP00uxs3ZbkuxWjmkGLBY PnpGrMrCTHjKBQMuT71GbqulhZcogpMHrwko4nDE52OuCWxc4HbbLenLdBkl17CO4U9f mqxVbcsIUGqhzNdLOPfuD4xVmP9NNR9/is13lzPvTFJ/rtrc522n6LsrComMFZjj+lzG X+xziC98Xz0eMF7QHCnrftsilYJpidEHGgnMidsbQQRON/nGHT2ZHhJVLw8QbpO85L+q gqldX/zRXWHeEbt+KSYgHd05z/XPe/6f8H9TpCPiJhjK4uxaraBu+jcB4ndg65plXqKF BhVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CvEhKWjG; 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 f2-20020a17090660c200b0099b59dc6ab8si2077509ejk.108.2023.08.04.14.49.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 14:49:15 -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=CvEhKWjG; 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 3EE223858C50 for ; Fri, 4 Aug 2023 21:49:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EE223858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691185754; bh=0AE1UVjdtf0/dFmwzEfh45tdu0XWHe9L3S1Lo1MWxxc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=CvEhKWjGmnNQMTl+0TBBpbkkclWsiZKsY895LHPsLsUWR7kQypSN0lepje6RL9mmR 5V1jgPKUQB71VuJbGNL6H2KgP5V6hL6w0wcS/pGkDlqK2WSaIAVqiYly644hTE7lCI res+h13OfLhtaZjd3lVcS/++Fh1t1ckPTlOcW1M4= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id B66103858D32 for ; Fri, 4 Aug 2023 21:48:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B66103858D32 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-217-WCqsFwpkMeiH5fvwl0PkfQ-1; Fri, 04 Aug 2023 17:48:30 -0400 X-MC-Unique: WCqsFwpkMeiH5fvwl0PkfQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B6EA910113C1 for ; Fri, 4 Aug 2023 21:48:29 +0000 (UTC) Received: from drross.com (unknown [10.2.16.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 988CF40C2063; Fri, 4 Aug 2023 21:48:29 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: Drew Ross Subject: [PATCH] match.pd: Implement missed optimization ((x ^ y) & z) | x -> (z & y) | x [PR109938] Date: Fri, 4 Aug 2023 17:48:17 -0400 Message-Id: <20230804214817.1256642-1-drross@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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: Drew Ross via Gcc-patches From: Drew Ross Reply-To: Drew Ross Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773336794629573768 X-GMAIL-MSGID: 1773336794629573768 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. PR tree-opt/109938 gcc/ChangeLog: * match.pd ((x ^ y) & z) | x -> (z & y) | x: New simplification. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/pr109938.c: New test. * gcc.dg/tree-ssa/pr109938.c: New test. --- gcc/match.pd | 10 +- .../gcc.c-torture/execute/pr109938.c | 33 +++++ gcc/testsuite/gcc.dg/tree-ssa/pr109938.c | 125 ++++++++++++++++++ 3 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr109938.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109938.c diff --git a/gcc/match.pd b/gcc/match.pd index ee6cef6b09d..884dc622b25 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1946,10 +1946,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bitop:c (rbitop:c (bit_not @0) @1) @0) (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 */ +(for op (bit_ior bit_xor) + (simplify + (bit_ior:c (nop_convert1? (bit_and:c (nop_convert2? (op:c @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 diff --git a/gcc/testsuite/gcc.c-torture/execute/pr109938.c b/gcc/testsuite/gcc.c-torture/execute/pr109938.c new file mode 100644 index 00000000000..a65d13b305d --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr109938.c @@ -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; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109938.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109938.c new file mode 100644 index 00000000000..0cae55886c6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109938.c @@ -0,0 +1,125 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse1 -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 " \\\^ " "dse1" } } */ +/* { dg-final { scan-tree-dump-times " \\\| " 15 "dse1" } } */ +/* { dg-final { scan-tree-dump-times " & " 15 "dse1" } } */