From patchwork Thu Sep 19 12:17:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Konstantinos Eleftheriou X-Patchwork-Id: 97696 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D40DA385842D for ; Thu, 19 Sep 2024 12:17:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 892753858CD9 for ; Thu, 19 Sep 2024 12:17:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 892753858CD9 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 892753858CD9 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726748249; cv=none; b=MTvSElXNJMOlV0yt7BGMWXfQ7RJNTfPrGsC3p5EKjKKKdybSQLluHQnxdZ0Kvv8wLyMek2dzV46bo9JMFScAO27ims/Uv/0MHPOxSPiv5dEvyJ/mrhVNyigUHVm4Aimdnh/Na//15tz/LPD9PSJ5nWkAwKrhP08P6zE82mUIGs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726748249; c=relaxed/simple; bh=QtOxni+rpRreLdmwX6Znv7NjFnAzZ6eTPFVUnBrYakA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gG3GUtGO+kTq0c1jEWKV8f8a1Ow2IhHR0YTq5jhATcHGnPRqccEMBiveuefZcDsFC9nMfdhwnfi2o3cKVgSrc6dTtbDaAD4SHiXa3o46Zuf1Nf58GQKV5YzaL93nr1qevTEWJVOXB0ssBuNYe3Gfs6Tb0zfxNBiRNDHnXcQoHYc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a90188ae58eso94077366b.1 for ; Thu, 19 Sep 2024 05:17:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1726748244; x=1727353044; 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=C0rGeJvd8OGU2XutOqYoim7EDoN8W4VjqJ3CarTYctA=; b=bLF4GWlXhTPRUc9nPLkJXut9Su72dTo6Tfq1CgBtLqw1GY06YDynNCADnfmcFRDS79 pNmDasY7AGtbu9LoNTgkzb6iHLINOtWDpstqMEpRGF6iC/7kJ34Y+S22M1On/aLM1/7T ZXY3VX7NIXUQKLCl51cLx53YirnrmcHjRr1qx/vGrFPuATeQq7K5lTBSAPDQNVLa44Yv h9ePN4ow/NPLK0qBMbHjet4hAymDrSc7l9JxqnxHdyTI38okajgd/5LuBAqk+wzOq3Fe 6Raeu1H2zzXy5UL28pbRBdW2T4IcnMKvsN+4NDLlXvJLPRe0Gu/H+qn3sAdqe3ChMT1Z wQcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726748244; x=1727353044; 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=C0rGeJvd8OGU2XutOqYoim7EDoN8W4VjqJ3CarTYctA=; b=nMlmUXnYj0Atriyf7sGFvTaqD4xyRPaOkV8MQ/k09Z652Q20aEJB2qd+ZdgkQqXvEH Me+yQ+rHskNQJLJ299UdSqoxDUeFCfzRXPR0Njx1XKa3cv9HKRoZ06VbtA+XE3jCmzZm pT56yyT/kW0q027my3oLuhPjr3V8Otj/A2Ozu5FIIRxi4pceXO0erCpYy59VoixQPiH/ 3JEr+Zfc6YdJsMG553/tKEKP4NhWGQM16Sv8UFSmOfcJBY7B/ObSdleyjRA9CuCSWa9G 6dAAPoPuvDiVd6aXRyQI7Rzc7ohS2CIttY0rIDvpqnCzSnLZxHhIioJJKJPehHRbqT2J 5VGg== X-Gm-Message-State: AOJu0YxJp1a7Ivd85LWqndUNv18gCxaNQaUMBVA217tBZvSSnXEzEewy mJC7Lnv0yhG/+k2fiCzZ4qF3NAFCs6kgB9XzNC3WU+gbf9KC5rH9ej59+CaO1ztgi0RYRVRD5IL YO4g= X-Google-Smtp-Source: AGHT+IGPXJmsfl35djNzEaUk/Oab5cr3Jl4RNQJeycjVoXS4HFd0QscskINm0fRu2NmAalUmFz9bPw== X-Received: by 2002:a17:906:f5a9:b0:a8a:780f:4faf with SMTP id a640c23a62f3a-a90296182c5mr2437110366b.47.1726748243514; Thu, 19 Sep 2024 05:17:23 -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-a906109669fsm713269266b.3.2024.09.19.05.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2024 05:17:23 -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 v4] match: Fix A || B not optimized to true when !B implies A [PR114326] Date: Thu, 19 Sep 2024 14:17:21 +0200 Message-ID: <20240919121721.334624-1-konstantinos.eleftheriou@vrull.eu> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.0 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 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~patchwork=sourceware.org@gcc.gnu.org From: kelefth In expressions like (a != b || ((a ^ b) & c) == d) and (a != b || (a ^ b) == c), (a ^ b) is folded to false. In the equivalent expressions (((a ^ b) & c) == d || a != b) and ((a ^ b) == c || a != b) this is not happening. This patch adds the following simplifications in match.pd: ((a ^ b) & c) cmp d || a != b --> 0 cmp d || a != b (a ^ b) cmp c || a != b --> 0 cmp c || 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.c: New test. * gcc.dg/tree-ssa/fold-xor-or.c: New test. Tested-by: Christoph Müllner Signed-off-by: Philipp Tomsich Signed-off-by: Konstantinos Eleftheriou --- gcc/match.pd | 32 ++++++++++- .../gcc.dg/tree-ssa/fold-xor-and-or.c | 55 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or.c | 55 +++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or.c diff --git a/gcc/match.pd b/gcc/match.pd index 4aa610e2270..3c4f9b5f774 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3761,6 +3761,36 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (types_match (type, TREE_TYPE (@0))) (bit_xor @0 { build_one_cst (type); } )))))) +/* ((a ^ b) & c) cmp d || a != b --> (0 cmp d || a != b). */ +(for cmp (simple_comparison) + (simplify + (bit_ior + (cmp:c + (bit_and:c + (bit_xor:c @0 @1) + tree_expr_nonzero_p@2) + @3) + (ne:c@4 @0 @1)) + (bit_ior + (cmp + { build_zero_cst (TREE_TYPE (@0)); } + @3) + @4))) + +/* (a ^ b) cmp c || a != b --> (0 cmp c || a != b). */ +(for cmp (simple_comparison) + (simplify + (bit_ior + (cmp:c + (bit_xor:c @0 @1) + @2) + (ne:c@3 @0 @1)) + (bit_ior + (cmp + { build_zero_cst (TREE_TYPE (@0)); } + @2) + @3))) + /* We can't reassociate at all for saturating types. */ (if (!TYPE_SATURATING (type)) @@ -10763,4 +10793,4 @@ and, } } (if (full_perm_p) - (vec_perm (op@3 @0 @1) @3 @2)))))) + (vec_perm (op@3 @0 @1) @3 @2)))))) \ No newline at end of file diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or.c new file mode 100644 index 00000000000..e5dc98e7541 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-and-or.c @@ -0,0 +1,55 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +typedef unsigned long int uint64_t; + +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; +} + +int cmp3(int d1, int d2) { + if (10 > (0xabcd & (d2 ^ d1)) || d2 != d1) + return 0; + return 1; +} + +int cmp4(int d1, int d2) { + if (d2 != d1 || 10 > (0xabcd & (d2 ^ d1))) + return 0; + return 1; +} + +int cmp1_64(uint64_t d1, uint64_t d2) { + if (((d1 ^ d2) & 0xabcd) == 0 || d1 != d2) + return 0; + return 1; +} + +int cmp2_64(uint64_t d1, uint64_t d2) { + if (d1 != d2 || ((d1 ^ d2) & 0xabcd) == 0) + return 0; + return 1; +} + +int cmp3_64(uint64_t d1, uint64_t d2) { + if (10 > (0xabcd & (d2 ^ d1)) || d2 != d1) + return 0; + return 1; +} + +int cmp4_64(uint64_t d1, uint64_t d2) { + if (d2 != d1 || 10 > (0xabcd & (d2 ^ d1))) + 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.c b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or.c new file mode 100644 index 00000000000..c55cfbcc84c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fold-xor-or.c @@ -0,0 +1,55 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +typedef unsigned long int uint64_t; + +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; +} + +int cmp3(int d1, int d2) { + if (0xabcd > (d2 ^ d1) || d2 != d1) + return 0; + return 1; +} + +int cmp4(int d1, int d2) { + if (d2 != d1 || 0xabcd > (d2 ^ d1)) + return 0; + return 1; +} + +int cmp1_64(uint64_t d1, uint64_t d2) { + if ((d1 ^ d2) == 0xabcd || d1 != d2) + return 0; + return 1; +} + +int cmp2_64(uint64_t d1, uint64_t d2) { + if (d1 != d2 || (d1 ^ d2) == 0xabcd) + return 0; + return 1; +} + +int cmp3_64(uint64_t d1, uint64_t d2) { + if (0xabcd > (d2 ^ d1) || d2 != d1) + return 0; + return 1; +} + +int cmp4_64(uint64_t d1, uint64_t d2) { + if (d2 != d1 || 0xabcd > (d2 ^ d1)) + 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" } } */