From patchwork Wed Aug 14 15:18:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Konstantinos Eleftheriou X-Patchwork-Id: 1972458 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=vrull.eu header.i=@vrull.eu header.a=rsa-sha256 header.s=google header.b=FcXY94eb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkX2q4zrBz1yZl for ; Thu, 15 Aug 2024 01:18:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24008385B50B for ; Wed, 14 Aug 2024 15:18:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 6E2EE3858D3C for ; Wed, 14 Aug 2024 15:18:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E2EE3858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E2EE3858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723648696; cv=none; b=YoAZLOnb5RvtfJPXFDMEckk22IKfMA2TL6RnvxQs2HLEdCpTg//+wPnhe79PnervCAAlPzRpotczQHH8ER2eIWQim1KNatp0PrAKQvIDZS2HguTMdvWZ2hnbCNuqdH2udnXopgq9HKJZ2S0nR/KBLRLTxhSTTInwVDI3lMIJKic= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723648696; c=relaxed/simple; bh=5DL01JzjH2uBXC6CbPTkvgb3Gy5zaBQAdj1ViWsFwTE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OcOAO6U+xxO4mzcWprYlKL9K+OHgXnnrrRYfYRXqNE1DqtICGlkY0itPn2V9tS21WzGHxCwHV+IZxNcAJZPiYD71ZWd0PasY6LRjt2aAr1lDWukIaaQiVukGLJSjLCqbYq1rCOBDvmdC1Z9glc4H1ASMPLrKboRpdeTsLrANbb4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a7aa74d1ab8so91978766b.1 for ; Wed, 14 Aug 2024 08:18:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1723648692; x=1724253492; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=eklX+dWmajLLIOSxWxS48eblrQxP1JszbSUJOMNH/Hg=; b=FcXY94ebxL3OfGOQXnIHK6d2pQG9Xy7OFYB/feIYVtWdYsrmaidvSFJzC0lC08WzXZ fF/i+pA8KOuxy9g0aFy/JylShZyhPqqEKTi9huMTJ9wQ8EU2/fc1Yk2dCVZXbFo5+0mr o5iF6mwY5mDLZYqqxF7huGwlZtNBYeHFV3TcQwH04Ms9uQXZPs91918EYCu5MgILckbp 6lpYEbe6PD77RkWb58YkdBQuwLJg1RsOVt5+9tpw/Uyc4TWCelHuK6SgpWX7KiqIJPHn 0+Oig4E0ABtN4EFx7w8qKvtaxovl3S8lSJJ7LqgNWJZwY2u3y1rK0l10d7dHqeOYrJvU DMxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723648692; x=1724253492; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eklX+dWmajLLIOSxWxS48eblrQxP1JszbSUJOMNH/Hg=; b=LDlLKi49dVZ4nUxddH1VWNsVmAqoYqY+pLyKq96GDsNpPTBrBYcflmk1beeYB7mFmU DsCJlI4fRjrTeeVBsqQ7akvN1UrzrRXrE++PLMdUCizERG21Zqs+ZC0bFos2ajHS6pb5 Y5C0lb7hL8+l/Xm8WLWD/jUlT4Xvz+2rtoa8tzvzRCjtBLsnTWWE6TQPvc7hKMBhn/Da bCW3zaYrA05znhCaUtraE1ijxJNK5Db0KQmlwBSpAPB1UjhknrvOVlkZxMM9kCqqmUbL IDJSYeKOYYjHrdxQofqNVGqQdagHHCp1E6Ek9oxTUNuI8Vk7h51ZzoYmls84Em4kCN2S bFEw== X-Gm-Message-State: AOJu0Yx7hX95iJeb+5wrTLQ7A9EILjjVY+Y3TjUEuOErRKkKEkn0B4PA NQrdzaD3Mw8QFl++98jTKJNaj/XsUWSy1X7ssLTp1/iRkCMF+jmYiJSdajPo90IDa7uDYzV/Klw 5X8P5wA== X-Google-Smtp-Source: AGHT+IGHQhQh6GVLPAaB5q+TwTgZQ4iIveY/uZcgDf8+QhNYWPX+Bh8I6Un9YOQI4ISMkaG0bZlbsg== X-Received: by 2002:a17:907:a4a:b0:a7a:952b:95b6 with SMTP id a640c23a62f3a-a8366c2ec39mr109495866b.1.1723648691472; Wed, 14 Aug 2024 08:18:11 -0700 (PDT) Received: from altra.sec.univie.ac.at (ampere.sec.univie.ac.at. [131.130.126.104]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f4184ec9sm183251066b.219.2024.08.14.08.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 08:18:11 -0700 (PDT) From: Konstantinos Eleftheriou To: gcc-patches@gcc.gnu.org, Philipp Tomsich , =?utf-8?q?Christoph_M=C3=BCllner?= , Jeff Law Cc: kelefth Subject: [PATCH] match: Fix A || B not optimized to true when !B implies A [PR114326] Date: Wed, 14 Aug 2024 17:18:04 +0200 Message-ID: <20240814151804.217324-1-konstantinos.eleftheriou@vrull.eu> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: kelefth In expressions like (a != b || ((a ^ b) & CST0) == CST1) and (a != b || (a ^ b) == CST), (a ^ b) is folded to false. In the equivalent expressions (((a ^ b) & CST0) == CST1 || a != b) and ((a ^ b) == CST, (a ^ b) || a != b) this is not happening. This patch adds the following simplifications in match.pd: ((a ^ b) & CST0) == CST1 || a != b --> 0 == (CST1 || a != b) (a ^ b) == CST || a != b --> 0 == CST || (a != b) PR tree-optimization/114326 gcc/ChangeLog: * match.pd: Add two patterns to fold a ^ b to 0, when a == b. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/fold-xor-and-or-1.c: New test. * gcc.dg/tree-ssa/fold-xor-and-or-2.c: New test. * gcc.dg/tree-ssa/fold-xor-or-1.c: New test. * gcc.dg/tree-ssa/fold-xor-or-2.c: New test. Reviewed-by: Christoph Müllner Signed-off-by: Philipp Tomsich Signed-off-by: Konstantinos Eleftheriou --- gcc/match.pd | 30 +++++++++++++++++++ .../gcc.dg/tree-ssa/fold-xor-and-or-1.c | 17 +++++++++++ .../gcc.dg/tree-ssa/fold-xor-and-or-2.c | 19 ++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-1.c | 17 +++++++++++ gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-2.c | 19 ++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-2.c diff --git a/gcc/match.pd b/gcc/match.pd index c9c8478d286..1c55bd72f09 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -10680,3 +10680,33 @@ and, } (if (full_perm_p) (vec_perm (op@3 @0 @1) @3 @2)))))) + +/* ((a ^ b) & CST0) == CST1 || a != b --> 0 == (CST1 || a != b). */ +(for cmp (simple_comparison) + (simplify + (bit_ior + (cmp + (bit_and + (bit_xor @0 @1) + INTEGER_CST) + @3) + (ne@4 @0 @1)) + (bit_ior + (cmp + { build_zero_cst (TREE_TYPE (@0)); } + @3) + @4))) + +/* (a ^ b) == CST || a != b --> 0 == CST || (a != b). */ +(for cmp (simple_comparison) + (simplify + (bit_ior + (cmp + (bit_xor @0 @1) + @2) + (ne@3 @0 @1)) + (bit_ior + (cmp + {build_zero_cst (TREE_TYPE (@0)); } + @2) + @3))) \ No newline at end of file diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-1.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-1.c new file mode 100644 index 00000000000..2bcf98d93c3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-1.c @@ -0,0 +1,17 @@ +/* { dg-do-compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +int cmp1(int d1, int d2) { + if (((d1 ^ d2) & 0xabcd) == 0 || d1 != d2) + return 0; + return 1; +} + +int cmp2(int d1, int d2) { + if (d1 != d2 || ((d1 ^ d2) & 0xabcd) == 0) + return 0; + return 1; +} + +/* The if should be removed, so the condition should not exist */ +/* { dg-final { scan-tree-dump-not "d1_\[0-9\]+.D. \\^ d2_\[0-9\]+.D." "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-2.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-2.c new file mode 100644 index 00000000000..8771897181a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or-2.c @@ -0,0 +1,19 @@ +/* { dg-do-compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +typedef unsigned long int uint64_t; + +int cmp1(uint64_t d1, uint64_t d2) { + if (((d1 ^ d2) & 0xabcd) == 0 || d1 != d2) + return 0; + return 1; +} + +int cmp2(uint64_t d1, uint64_t d2) { + if (d1 != d2 || ((d1 ^ d2) & 0xabcd) == 0) + return 0; + return 1; +} + +/* The if should be removed, so the condition should not exist */ +/* { dg-final { scan-tree-dump-not "d1_\[0-9\]+.D. \\^ d2_\[0-9\]+.D." "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-1.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-1.c new file mode 100644 index 00000000000..eb08e086014 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-1.c @@ -0,0 +1,17 @@ +/* { dg-do-compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +int cmp1(int d1, int d2) { + if ((d1 ^ d2) == 0xabcd || d1 != d2) + return 0; + return 1; +} + +int cmp2(int d1, int d2) { + if (d1 != d2 || (d1 ^ d2) == 0xabcd) + return 0; + return 1; +} + +/* The if should be removed, so the condition should not exist */ +/* { dg-final { scan-tree-dump-not "d1_\[0-9\]+.D. \\^ d2_\[0-9\]+.D." "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-2.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-2.c new file mode 100644 index 00000000000..b621651e636 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or-2.c @@ -0,0 +1,19 @@ +/* { dg-do-compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +typedef unsigned long int uint64_t; + +int cmp1(uint64_t d1, uint64_t d2) { + if ((d1 ^ d2) == 0xabcd || d1 != d2) + return 0; + return 1; +} + +int cmp2(uint64_t d1, uint64_t d2) { + if (d1 != d2 || (d1 ^ d2) == 0xabcd) + return 0; + return 1; +} + +/* The if should be removed, so the condition should not exist */ +/* { dg-final { scan-tree-dump-not "d1_\[0-9\]+.D. \\^ d2_\[0-9\]+.D." "optimized" } } */