From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 32B233858423 for ; Wed, 5 Jul 2023 13:41:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 32B233858423 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688564512; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6Qg82a8NwjDHQMmxR4uXp5B25P3J1RwdzQLWRDabLDo=; b=KltEutXPdgY0d2+cjHDwnv7Y+1qcoahzpKtMU1j680IRMT0ujt/d7N0fJmwa1VR9aIB8cP FFBctvv1GOqTC5MGl7pQvI+yxnWD6t5zfmIdGQgNNTbwQP42OU21NSu8RyBI0N1nQ2g8k1 AINbyK/Xf+zE5KsTSns088Vf/n7/zPc= 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-212-bMcqZhD1NQ6MAmTrTIdIiA-1; Wed, 05 Jul 2023 09:41:51 -0400 X-MC-Unique: bMcqZhD1NQ6MAmTrTIdIiA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8877485A58A for ; Wed, 5 Jul 2023 13:41:51 +0000 (UTC) Received: from drross.com (unknown [10.22.10.143]) by smtp.corp.redhat.com (Postfix) with ESMTP id 41A094087C6A; Wed, 5 Jul 2023 13:41:51 +0000 (UTC) From: Drew Ross To: gcc-patches@gcc.gnu.org Cc: Drew Ross Subject: [PATCH] match.pd: Implement missed optimization (~X | Y) ^ X -> ~(X & Y) [PR109986] Date: Wed, 5 Jul 2023 09:41:47 -0400 Message-Id: <20230705134147.13325-1-drross@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.2 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 List-Id: Adds a simplification for (~X | Y) ^ X to be folded into ~(X & Y). Tested successfully on x86_64 and x86 targets. PR middle-end/109986 gcc/ChangeLog: * 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. --- gcc/match.pd | 11 ++ .../gcc.c-torture/execute/pr109986.c | 41 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr109986.c | 177 ++++++++++++++++++ 3 files changed, 229 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/match.pd b/gcc/match.pd index a17d6838c14..d9d7d932881 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1627,6 +1627,17 @@ 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 (nop_convert3? @0))) + @1)) (nop_convert4? @0)) + (if (types_match (type, @1)) + (bit_not (bit_and @1 (convert @0))) + (if (types_match (type, @0)) + (bit_not (bit_and (convert @1) @0)) + (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" } } */ + -- 2.39.3