From patchwork Sat Oct 14 00:57:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1848725 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=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Lmf3HV4J; 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 4S6lN75ldjz1yq3 for ; Sat, 14 Oct 2023 11:57:41 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 827B23857C4F for ; Sat, 14 Oct 2023 00:57:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id E492F3858D28 for ; Sat, 14 Oct 2023 00:57:18 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E492F3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::233 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697245040; cv=none; b=xonumzkw11fDc39WtvwGmMa9rZ9WMP0RnRG0beUzmFTB39eEc8Ohdtjg6B+bdQmOAwVdCLGTHE+77jUPs47lb8lLMwuw8xx3OwOiigmTTFUuiwy2odHg8kJM1tnGHHplEskhaFm4N0kSvYNqBtFSSCqBUYSE1HNaoc00GG94EKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697245040; c=relaxed/simple; bh=Hyt16ComvIt+VSYyrmJD5ppE1nMeZJMtjcZX07rg9Xk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=qB86fOkflRjWKhHLsdoTqioC5UTiOh+BMlB3sylVweJeAfupDVmycyG8D8J41Oe3DBfdFJgUSMJqy/o0KQdAeH+CJ0SRY0ts3Smvx3nIor6doBTYejL7j9181DZ/Sa8d687jVGT2KsLMGMmT9QvYOZroasRuIKuHP9VU1lzr1bc= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E492F3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3b2b1af964dso759538b6e.1 for ; Fri, 13 Oct 2023 17:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697245038; x=1697849838; 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=3I78IdTUufZKURv0R0uK4Rei5QBTXmj1BWiNdJxIpKI=; b=Lmf3HV4JWIQMS8puy42LlVLNOGlFOqCOBdpzPn+Zd7NRcZqPP359Y7nZrOtEFOZzhW GVyZfv+tcRAnnUVHVZp//admlJis6PS5qu0Hc7m+NBzXfXOXmDeUKGewURfDIOX0eWev gyZo+rtwrj67hKMTfxEdncCKL3Mex3HpfBVmKdZUYc9sAEt2FXB/Hz3z57LFTUdts+jF fOeMmlyzALkh4RmT/rTY+cePrCqENTZYKYMVLjS1uCHuYoHEgcafiEAHA57gw8H7e5cV +m/ZlgRDmMq7vkVfSp/wDDpehl+gQ7wX2WnHKZsb7ISlhF3rr2lii5GRZLCKnViOx6+f AVbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697245038; x=1697849838; 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=3I78IdTUufZKURv0R0uK4Rei5QBTXmj1BWiNdJxIpKI=; b=NcWoj7aINOS+MIdpu4ETlAx4bfloi0iWnhCg0sdVDS1i78P4et0C7o2vbL5A+yrU6Y vU93zrqGEsus+sa54QyHRJ+Tu18hjI3jwkzv2pa+tWdoNtbvYuyOK9zZGEI253LKAQpg 6uA6NXxt5ivy3PSFLv5yG5rcToxwDf6r6Bhxb/dVbO+Wn8QVEyWyGeKgiJkugUO3NjT3 P/iI+SQgeWFjUrwbY3BDUHvkZpcYCQi99j4USh2CiIR5x1bjiaZOL91lkEXxu06Uyg9g 1tjc1OECjTuyaM+Vfd66HRWdbppM1WwhqM1u/qgwPwXhssDSaCWRdhrQXMkBQL05ibmk lMeQ== X-Gm-Message-State: AOJu0YzlYmz53wWrKOPmUPhnjrG5QwBsy9egw1kQdCRDDdzUX6Psmb5F bzLbE9dh/BPESzo9KUdt4JtQh7akesCIeQ== X-Google-Smtp-Source: AGHT+IHnKreId1Qe3Xz3d1yUZAgeN3nQtxb66FeSIu5xRkuSWXPJ3YR4vOZL6UF/kpz3KuvAT0PprQ== X-Received: by 2002:a05:6808:1404:b0:3af:e451:9e6 with SMTP id w4-20020a056808140400b003afe45109e6mr20900918oiv.15.1697245037594; Fri, 13 Oct 2023 17:57:17 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.35.186]) by smtp.gmail.com with ESMTPSA id d9-20020a634f09000000b005aa800c149bsm1854654pgb.39.2023.10.13.17.57.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 17:57:16 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] MATCH: [PR111432] Simplify `a & (x | CST)` to a when we know that (a & ~CST) == 0 Date: Fri, 13 Oct 2023 17:57:13 -0700 Message-Id: <20231014005713.2702564-1-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org This adds the simplification `a & (x | CST)` to a when we know that `(a & ~CST) == 0`. In a similar fashion as `a & CST` is handle. I looked into handling `a | (x & CST)` but that I don't see any decent simplifications happening. OK? Bootstrapped and tested on x86_linux-gnu with no regressions. PR tree-optimization/111432 gcc/ChangeLog: * match.pd (`a & (x | CST)`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bitops-7.c: New test. --- gcc/match.pd | 8 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c diff --git a/gcc/match.pd b/gcc/match.pd index 51e5065d086..45624f3dcb4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1550,6 +1550,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) @0)) + +/* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */ +(simplify + (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0) + @0)) + /* x | C -> C if we know that x & ~C == 0. */ (simplify (bit_ior SSA_NAME@0 INTEGER_CST@1) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c new file mode 100644 index 00000000000..7fb18db3a11 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/111432 */ + +int +foo3(int c, int bb) +{ + if ((bb & ~3)!=0) __builtin_unreachable(); + return (bb & (c|3)); +} + +int +foo_bool(int c, _Bool bb) +{ + return (bb & (c|7)); +} + +/* Both of these functions should be able to remove the `IOR` and `AND` + as the only bits that are non-zero for bb is set on the other side + of the `AND`. + */ + +/* { dg-final { scan-tree-dump-not "bit_ior_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "bit_and_expr, " "optimized" } } */