From patchwork Mon Oct 9 21:27:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1845503 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=OD1jTabz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4S4BwG3l8Jz1ypX for ; Tue, 10 Oct 2023 08:28:12 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3900C385841E for ; Mon, 9 Oct 2023 21:28:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 078463858D28 for ; Mon, 9 Oct 2023 21:27:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 078463858D28 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-pl1-x62a.google.com with SMTP id d9443c01a7336-1c5c91bec75so32989335ad.3 for ; Mon, 09 Oct 2023 14:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696886876; x=1697491676; 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=vsWsNA5jbT/l5Og4CS07wywZ6pFWfxDKw+cTkgavXT4=; b=OD1jTabzoyAftzWqSQENjsn3p0QplnxBnj5f+mIzsMUo5e/LUioaBfTS8C64+E+EO9 apfQ2LcQ7v5VL3Gu56l6D9Av48JiZEihezaVZ22WGSxTGDOzfN1REDUqfq8/h+Of3MCc 7iEIOl2dZ4/UPEOIpsraUcY1PbqoVJj5nPXPvjhK6jhPseyREsCLCxBRKBPLBbocPQp4 bWZXzn4BgwyChpohnm5rOyu7tkOahe2lK+i3CIlTccoGqYAekK1f/WXyDuyywfuRH1xH 6X07oDTPDKhmpplycQPF3IFH40c34AK3nU2SKhIPY4ugHXf7/fLqisnDwzsFvQNzgIqm 7qMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696886876; x=1697491676; 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=vsWsNA5jbT/l5Og4CS07wywZ6pFWfxDKw+cTkgavXT4=; b=cup6+MdAXmDTFFTMs0ShFCWTaWTWdwQcQqh47HPpYEGlUpuyyKyhBK/4RTb1bL6gXc ANK4SAo2yBm6Ei93IfTZquJZ7QPlqFAjtuG726Md5IOS+iiBXxHlk88zXt4lI7SRmj6V nzX/O1zVLoZiqK3Fok34DS17DNpXY+CuVTLF5QgojFXwB2jVwufkJ00l/Qod7nvCNtVm RZBvZ/kZNjoR4lk+Vo3TVV7Lm3qUS+aebw3NX7JD8ieRED4NpUwGDX7O3fc/2diVk63q 8syD3CiAZKyTPAyFaGMlmVBo3oWj+VPCNkF4chy3q54R+syqfRdqTnMHqeUSskNih9/q 55xw== X-Gm-Message-State: AOJu0YxbFcRjMXBLeneORxIQLkhYzOKUbF5w/iMSeLF+fiairw2CJ2BL hqQ3QWYkQW+GqMKf4Ihw0eB/MahM6+i7gQ== X-Google-Smtp-Source: AGHT+IFgX9xdpVs4hYHApXyHuHMS1WwlwGI80IAIid+tw7q6UAARYNOOol4QayngSZOlPHjR0o/Xow== X-Received: by 2002:a17:903:120f:b0:1c0:c86a:5425 with SMTP id l15-20020a170903120f00b001c0c86a5425mr16061154plh.19.1696886876369; Mon, 09 Oct 2023 14:27:56 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id c12-20020a170902c1cc00b001c60a548331sm10061110plc.304.2023.10.09.14.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:27:55 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] MATCH: [PR111679] Add alternative simplification of `a | ((~a) ^ b)` Date: Mon, 9 Oct 2023 14:27:51 -0700 Message-Id: <20231009212751.474885-1-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org So currently we have a simplification for `a | ~(a ^ b)` but that does not match the case where we had originally `(~a) | (a ^ b)` so we need to add a new pattern that matches that and uses bitwise_inverted_equal_p that also catches comparisons too. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111679 gcc/ChangeLog: * match.pd (`a | ((~a) ^ b)`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bitops-5.c: New test. --- gcc/match.pd | 8 +++++++ gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c | 27 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c diff --git a/gcc/match.pd b/gcc/match.pd index 31bfd8b6b68..49740d189a7 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1350,6 +1350,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (TREE_TYPE (@0)) == 1) (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))) +/* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */ +(simplify + (bit_ior:c @0 (bit_xor:cs @1 @2)) + (with { bool wascmp; } + (if (bitwise_inverted_equal_p (@0, @1, wascmp) + && (!wascmp || element_precision (type) == 1)) + (bit_ior @0 (bit_not @2))))) + /* (a | b) | (a &^ b) --> a | b */ (for op (bit_and bit_xor) (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c new file mode 100644 index 00000000000..990610e3002 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/111679 */ + +int f1(int a, int b) +{ + return (~a) | (a ^ b); // ~(a & b) or (~a) | (~b) +} + +_Bool fb(_Bool c, _Bool d) +{ + return (!c) | (c ^ d); // ~(c & d) or (~c) | (~d) +} + +_Bool fb1(int x, int y) +{ + _Bool a = x == 10, b = y > 100; + return (!a) | (a ^ b); // ~(a & b) or (~a) | (~b) + // or (x != 10) | (y <= 100) +} + +/* { dg-final { scan-tree-dump-not "bit_xor_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_not_expr, " 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "ne_expr, _\[0-9\]+, x_\[0-9\]+" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "le_expr, _\[0-9\]+, y_\[0-9\]+" 1 "optimized" } } */