From patchwork Fri Jul 21 18:18:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Drew Ross X-Patchwork-Id: 124051 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp386677vqg; Fri, 21 Jul 2023 11:19:34 -0700 (PDT) X-Google-Smtp-Source: APBJJlHzYOSCOLBYe2jUqYUXjhzSh8ScpBzv/a/e4KrrTgEp7eY37SOceCK/ITdYnEZdhl49R8bZ X-Received: by 2002:a17:906:31d6:b0:992:d337:6e44 with SMTP id f22-20020a17090631d600b00992d3376e44mr2284949ejf.58.1689963574469; Fri, 21 Jul 2023 11:19:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689963574; cv=none; d=google.com; s=arc-20160816; b=FwtP4XplxEd2v7Cj2vQc5RMyADzFaCxCNRszO51ytJGPPn+3V3oiSsZQrj4sFW8qzb 10B8mTbN0HjzmDs8matC4cSr9FAw4vgH9EfC63wbBHUkoDekMqJgkwq9YgOT26ocrlT0 UKifHu21my2qx9dAm8ny+dNIyFTuJthbQ1cfTLZIsQexL5vh9ZyIos66Msn12jcUStYc iwfuw1o8Tch0Nul+03l+eIt3syxVRwMfS2k6JgwNY+opCqImtU80ySHN3M4p7Rpaik35 M2dfbbeD+X9Ee+FtvQ3aWks1FNW9vgQepdnjROICkqnNuJJRes7s2qTMA4vHBQiXYYHW CvXA== 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=OnzB/chaXHvNd4hVeIPKueC9uIC7BgoAxX4EA8u1I+0=; fh=r1bGfsQXk6Ui0+1fnlMvWMaGuyYPYOk+Hot6R4QLdpU=; b=j3WSyxf8KyXx/J1YMteqjsgqUG0rkUqujDhZd4dPAePhaeXAL9z+mu+tcfYg434xx8 ZE+XgMxDVkgWNb01WTpwNxj8qmk1UwnHwxg/spA0o2J+i19zkaC4fAs/jmrtNldRHgZ0 VW+8CDEU8LcDOVFXQNhE18/+iu9KAFaNTXtSpTkbt0YunZZlaWUWa9jz3/Bzg/zuqwmB +XEH2aGC3bo3OykbMkPek78rZOtWg9N2p/Ji2fpcRUZ6kKtmw5+sYf+qR1jZEA7Y4zaU uULv9J/qpyjemFgMyB7qVNZqF6s7L7RcX8ILsrHh4ip8exjPznXhHxYmU9eIAaqjToK0 P03w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=GPWpLZWR; 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 e1-20020a1709062c0100b0099027b40d82si2510609ejh.243.2023.07.21.11.19.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jul 2023 11:19:34 -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=GPWpLZWR; 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 4F61D3853D2C for ; Fri, 21 Jul 2023 18:19:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F61D3853D2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689963573; bh=OnzB/chaXHvNd4hVeIPKueC9uIC7BgoAxX4EA8u1I+0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=GPWpLZWRtpPkrzmKVRpvB35olf82VHjVVZ58mqtOP8C9MV9QYt7Z/hdG45ipbrq3t 4v7c+Y2oApN3QCfq8IMinOk7mxafxp5Sh9+JENeVoT5cidmFFJay8nPM2+Oj0841GH zOL8YD80WyXGW/r9dVmg4yn/li3ZMhXT+ecU92NI= 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 5FBDD3858409 for ; Fri, 21 Jul 2023 18:18:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5FBDD3858409 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-632-2KoOBXbNOn2BEyUO8Zo_lw-1; Fri, 21 Jul 2023 14:18:45 -0400 X-MC-Unique: 2KoOBXbNOn2BEyUO8Zo_lw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E04341011573 for ; Fri, 21 Jul 2023 18:18:44 +0000 (UTC) Received: from drross.com (unknown [10.2.16.224]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B2064A9004; Fri, 21 Jul 2023 18:18:43 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: Drew Ross , Jakub Jelinek Subject: [PATCH] match.pd, v2: Implement missed optimization (~X | Y) ^ X -> ~(X & Y) [PR109986] Date: Fri, 21 Jul 2023 14:18:39 -0400 Message-Id: <20230721181839.119969-1-drross@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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, 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: 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: 1772055245382880476 X-GMAIL-MSGID: 1772055245382880476 Adds a simplification for (~X | Y) ^ X to be folded into ~(X & Y). Also adds the macro bitwise_equal_p for generic and gimple which returns true iff EXPR1 and EXPR2 have the same value. This helps to reduce the number of nop_converts necessary to match the pattern. Tested successfully on x86_64 and x86 targets. PR middle-end/109986 gcc/ChangeLog: * generic-match-head.cc (bitwise_equal_p): New macro. * gimple-match-head.cc (bitwise_equal_p): New macro. (gimple_nop_convert): Declare. (gimple_bitwise_equal_p): Helper for bitwise_equal_p. * match.pd ((~X | Y) ^ X -> ~(X & Y)): New simplification. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/pr109986.c: New test. * gcc.dg/tree-ssa/pr109986.c: New test. Co-authored-by: Jakub Jelinek --- gcc/generic-match-head.cc | 17 ++ gcc/gimple-match-head.cc | 36 ++++ gcc/match.pd | 6 + .../gcc.c-torture/execute/pr109986.c | 41 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr109986.c | 177 ++++++++++++++++++ 5 files changed, 277 insertions(+) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr109986.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109986.c diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc index f011204c5be..b4b5bc88f4b 100644 --- a/gcc/generic-match-head.cc +++ b/gcc/generic-match-head.cc @@ -102,3 +102,20 @@ optimize_successive_divisions_p (tree, tree) { return false; } + +/* Return true if EXPR1 and EXPR2 have the same value, but not necessarily + same type. The types can differ through nop conversions. */ + +static inline bool +bitwise_equal_p (tree expr1, tree expr2) +{ + STRIP_NOPS (expr1); + STRIP_NOPS (expr2); + if (expr1 == expr2) + return true; + if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) + return false; + if (TREE_CODE (expr1) == INTEGER_CST && TREE_CODE (expr2) == INTEGER_CST) + return wi::to_wide (expr1) == wi::to_wide (expr2); + return operand_equal_p (expr1, expr2, 0); +} diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc index b08cd891a13..f960d6cf0b9 100644 --- a/gcc/gimple-match-head.cc +++ b/gcc/gimple-match-head.cc @@ -224,3 +224,39 @@ optimize_successive_divisions_p (tree divisor, tree inner_div) } return true; } + +/* Return true if EXPR1 and EXPR2 have the same value, but not necessarily + same type. The types can differ through nop conversions. */ +#define bitwise_equal_p(expr1, expr2) gimple_bitwise_equal_p (expr1, expr2, valueize) + +bool gimple_nop_convert (tree, tree *, tree (*)(tree)); + +/* Helper function for bitwise_equal_p macro. */ + +static inline bool +gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)) +{ + if (expr1 == expr2) + return true; + if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) + return false; + if (TREE_CODE (expr1) == INTEGER_CST && TREE_CODE (expr2) == INTEGER_CST) + return wi::to_wide (expr1) == wi::to_wide (expr2); + if (operand_equal_p (expr1, expr2, 0)) + return true; + tree expr3, expr4; + if (!gimple_nop_convert (expr1, &expr3, valueize)) + expr3 = expr1; + if (!gimple_nop_convert (expr2, &expr4, valueize)) + expr4 = expr2; + if (expr1 != expr3) + { + if (operand_equal_p (expr3, expr2, 0)) + return true; + if (expr2 != expr4 && operand_equal_p (expr3, expr4, 0)) + return true; + } + if (expr2 != expr4 && operand_equal_p (expr1, expr4, 0)) + return true; + return false; +} diff --git a/gcc/match.pd b/gcc/match.pd index a17d6838c14..367e4fc5517 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1627,6 +1627,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) (convert (bit_and @1 (bit_not @0))))) +/* (~X | Y) ^ X -> ~(X & Y). */ +(simplify + (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2) + (if (bitwise_equal_p (@0, @2)) + (convert (bit_not (bit_and @0 (convert @1)))))) + /* Convert ~X ^ ~Y to X ^ Y. */ (simplify (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) diff --git a/gcc/testsuite/gcc.c-torture/execute/pr109986.c b/gcc/testsuite/gcc.c-torture/execute/pr109986.c new file mode 100644 index 00000000000..00ee9888539 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr109986.c @@ -0,0 +1,41 @@ +/* PR middle-end/109986 */ + +#include "../../gcc.dg/tree-ssa/pr109986.c" + +int +main () +{ + if (t1 (29789, 29477) != -28678) __builtin_abort (); + if (t2 (20196, -18743) != 4294965567) __builtin_abort (); + if (t3 (127, 99) != -100) __builtin_abort (); + if (t4 (100, 53) != 219) __builtin_abort (); + if (t5 (20100, 1283) != -1025) __builtin_abort (); + if (t6 (20100, 10283) != 63487) __builtin_abort (); + if (t7 (2136614690L, 1136698390L) != -1128276995L) __builtin_abort (); + if (t8 (1136698390L, 2136614690L) != -1128276995UL) __builtin_abort (); + if (t9 (9176690219839792930LL, 3176690219839721234LL) != -3175044472123688707LL) + __builtin_abort (); + if (t10 (9176690219839792930LL, 3176690219839721234LL) != 15271699601585862909ULL) + __builtin_abort (); + if (t11 (29789, 29477) != -28678) __builtin_abort (); + if (t12 (20196, -18743) != 4294965567) __builtin_abort (); + if (t13 (127, 99) != -100) __builtin_abort (); + if (t14 (100, 53) != 219) __builtin_abort (); + if (t15 (20100, 1283) != -1025) __builtin_abort (); + if (t16 (20100, 10283) != 63487) __builtin_abort (); + if (t17 (2136614690, 1136698390) != -1128276995) __builtin_abort (); + if (t18 (1136698390L, 2136614690L) != -1128276995UL) __builtin_abort (); + if (t19 (9176690219839792930LL, 3176690219839721234LL) != -3175044472123688707LL) + __builtin_abort (); + if (t20 (9176690219839792930LL, 3176690219839721234LL) != 15271699601585862909ULL) + __builtin_abort (); + v4si a1 = {1, 2, 3, 4}; + v4si a2 = {6, 7, 8, 9}; + v4si r1 = {-1, -3, -1, -1}; + v4si b1 = t21 (a1, a2); + v4si b2 = t22 (a1, a2); + 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/pr109986.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109986.c new file mode 100644 index 00000000000..45f099b5656 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109986.c @@ -0,0 +1,177 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */ + +typedef int v4si __attribute__((vector_size(16))); + +/* Generic */ +__attribute__((noipa)) int +t1 (int a, int b) +{ + return (~a | b) ^ a; +} + +__attribute__((noipa)) unsigned int +t2 (int a, int b) +{ + return a ^ (~a | (unsigned int) b); +} + +__attribute__((noipa)) char +t3 (char a, char b) +{ + return (b | ~a) ^ a; +} + +__attribute__((noipa)) unsigned char +t4 (char a, char b) +{ + return ((unsigned char) a) ^ (b | ~a); +} + +__attribute__((noipa)) short +t5 (short a, short b) +{ + return a ^ (b | ~a); +} + +__attribute__((noipa)) unsigned short +t6 (short a, short b) +{ + return ((unsigned short) a) ^ (b | ~a); +} + +__attribute__((noipa)) long +t7 (long a, long b) +{ + return a ^ (b | ~a); +} + +__attribute__((noipa)) unsigned long +t8 (long a, long b) +{ + return ((unsigned long) a) ^ (b | ~a); +} + +__attribute__((noipa)) long long +t9 (long long a, long long b) +{ + return a ^ (b | ~a); +} + +__attribute__((noipa)) unsigned long long +t10 (long long a, long long b) +{ + return ((unsigned long long) a) ^ (b | ~a); +} + +__attribute__((noipa)) v4si +t21 (v4si a, v4si b) +{ + return a ^ (b | ~a); +} + +/* Gimple */ +__attribute__((noipa)) int +t11 (int a, int b) +{ + int t1 = ~a; + int t2 = t1 | b; + int t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) unsigned int +t12 (int a, unsigned int b) +{ + int t1 = ~a; + unsigned int t2 = t1 | b; + unsigned int t3 = a ^ t2; + return t3; +} + +__attribute__((noipa)) char +t13 (char a, char b) +{ + char t1 = ~a; + char t2 = b | t1; + char t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) unsigned char +t14 (unsigned char a, char b) +{ + unsigned char t1 = ~a; + char t2 = b | t1; + unsigned char t3 = a ^ t2; + return t3; +} + +__attribute__((noipa)) short +t15 (short a, short b) +{ + short t1 = ~a; + short t2 = t1 | b; + short t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) unsigned short +t16 (unsigned short a, short b) +{ + short t1 = ~a; + short t2 = t1 | b; + unsigned short t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) long +t17 (long a, long b) +{ + long t1 = ~a; + long t2 = t1 | b; + long t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) unsigned long +t18 (long a, unsigned long b) +{ + long t1 = ~a; + unsigned long t2 = t1 | b; + unsigned long t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) long long +t19 (long long a, long long b) +{ + long long t1 = ~a; + long long t2 = t1 | b; + long long t3 = t2 ^ a; + return t3; +} + +__attribute__((noipa)) unsigned long long +t20 (long long a, long long b) +{ + long long t1 = ~a; + long long t2 = t1 | b; + unsigned long long t3 = a ^ t2; + return t3; +} + +__attribute__((noipa)) v4si +t22 (v4si a, v4si b) +{ + v4si t1 = ~a; + v4si t2 = t1 | b; + v4si t3 = a ^ t2; + return t3; +} + +/* { dg-final { scan-tree-dump-not " \\\| " "dse1" } } */ +/* { dg-final { scan-tree-dump-not " \\\^ " "dse1" } } */ +/* { dg-final { scan-tree-dump-times " ~" 22 "dse1" } } */ +/* { dg-final { scan-tree-dump-times " & " 22 "dse1" } } */ +