From patchwork Tue Aug 4 09:45:37 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyrylo Tkachov X-Patchwork-Id: 140573 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B03E4BA2E07 for ; Tue, 4 Aug 2026 09:46:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B03E4BA2E07 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=qpris7Qd X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazlp170120001.outbound.protection.outlook.com [IPv6:2a01:111:f403:c10d::1]) by sourceware.org (Postfix) with ESMTPS id 771944BA2E07 for ; Tue, 4 Aug 2026 09:46:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 771944BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nvidia.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 771944BA2E07 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2a01:111:f403:c10d::1 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785836767; cv=pass; b=jvb3fRCeIfzNH1TL167tl8BeVX69cAACwlbt8nDcbAF7gqPd3LHILweqRl7UF2jR+GsMGXLmarGiqb16M0ceV3H51VPiL0SpcjoKnB7EZUfw0WnfzvbWRt7NmYc1Xs4fQUXn5ZX8PQuqzsZEQ+8wrAghrSz1maS1eIp9bpcBDfo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785836767; c=relaxed/simple; bh=eEpMYnp6zIgQSKpQ0+NLDE05LVSxsGr/RHVJ4NG7900=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B7cwkZ1TJwW4p2O/IfZrNG7wO1EjP9jGIo3lGwgm9K7uMVrGnCdOvbu7ECD0MB4VlrdDG3vEEoiISnbdzJ9a2aJHFOUkKzWNlcxez99HWVSL3HydeMR9vnd4p+u6Jd6S6p3SrTh7IZbxGMjcKcvAQ/zXuOU5y1BiKYLiP8ll7MM= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=qpris7Qd DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 771944BA2E07 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NuTZ5n4sjYfrSTQIpM6OvS5ifoqq6gX4uq5CqZ1rkNPZ06XUgCSxAbSL6lXUC9L+O0spzv1WJ1uEMUP8VxnZ2WpDIsowEmIUvtD93u8HR3qNeXAzc/1SsqELSRFKa7y2cQ/nU+QEnDWo2XFNjA5cOKVh4NH2a7MxzQZN3OxSJwFurG42mMgB2Y51Yq/fffrHS2kMsjDCHPoplDqH0iIfdolcSV37Lb56adcWEOa8BfLuOap6zZtXIKr2VWxBwLniZsWiuYA/Eif6u6cg32JGmw1KDBDRLpu5x6V+kUL3Hx7ltgtcfm3UKr7eWAlPYrhutuGMF5idkxzdtLI1/BpgSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2iNuUjPg46wy/TAcyITJfIFxscDiqSggCyOisD761XM=; b=v8+B5QwCx8gOXiM0cmNQNx7bsF1lnfQPG0DAVuWafqyuBLXNx3Nq/8WDZhqHvvNE7V9wRh576tB1QtR1qiCH8FaxUIUHMiAD0oTJDqW/OTOSH8U/+vv2zb4zvgt90u2mdtEx8fSzbS5Mr4gJuq8UJlbySbaPogHHTeyyeqouzeOqWDYVO01rWE47JkIBbjy/4O7pU2bLVpWCg/1L19ZczXlUnqBpPj05jsd/qbg2BjstJN/GWC9WIZYvO6DmMLXuSkNkWM0yc9nFTHhddJ1BeP4jdRgd4j0UnS/Kz8BYPejsf3KAmDhlw5XOl91Lwwd8mmYKxtjYVbs4Li3EZApQWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2iNuUjPg46wy/TAcyITJfIFxscDiqSggCyOisD761XM=; b=qpris7QdKVayl8y5uTNTJro6d5BvgRVta4fa0INbhKHc7HiA6j1qGpncxqgsWXqlFaO4EXz34abyB/6Zt85huVy4VoxEZDYA33a96fvISt82+rG97VVVkZK1Za28QSeP3VeSy6x+UPwRWctnKi9WbMaVv/n0miMBFDcSW6a9Ua88OgkEmBYOFTO8DHBY8plN/aDZ6QNip826fZ4YqsDGqykLzqtdoI4EvkfTsmCuL2nxqRwdvwzMWDRa91N/vKpcBGWM2PcNCGyB4jxAfGdsJA/TZBWDcSQ9WUIWWEzblMuZ1AsTLTqLiexnhNeSO7RDIT1YflrEnXood7rhjeSNNA== Received: from CH5P223CA0007.NAMP223.PROD.OUTLOOK.COM (2603:10b6:610:1f3::18) by DS7PR12MB6047.namprd12.prod.outlook.com (2603:10b6:8:84::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.270.17; Tue, 4 Aug 2026 09:46:02 +0000 Received: from CH1PEPF0000AD75.namprd04.prod.outlook.com (2603:10b6:610:1f3:cafe::63) by CH5P223CA0007.outlook.office365.com (2603:10b6:610:1f3::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.292.16 via Frontend Transport; Tue, 4 Aug 2026 09:46:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CH1PEPF0000AD75.mail.protection.outlook.com (10.167.244.54) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.292.8 via Frontend Transport; Tue, 4 Aug 2026 09:46:01 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Tue, 4 Aug 2026 02:45:47 -0700 Received: from ktkachov-mlt.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Tue, 4 Aug 2026 02:45:46 -0700 From: To: CC: Kyrylo Tkachov Subject: [PATCH] match.pd: drop an operand discarded by a shift Date: Tue, 4 Aug 2026 11:45:37 +0200 Message-ID: <20260804094537.54778-1-ktkachov@nvidia.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 X-Originating-IP: [10.126.231.37] X-ClientProxiedBy: rnnvmail203.nvidia.com (10.129.68.9) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000AD75:EE_|DS7PR12MB6047:EE_ X-MS-Office365-Filtering-Correlation-Id: 6fc861cc-7f45-4f68-dfd9-08def20d3202 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|1800799024|82310400026|23010399003|36860700016|6133799003|56012099006|5023799004|11063799006|10067099003|18002099003; X-Microsoft-Antispam-Message-Info: 2El+7nOwLn2QMAs92vnUu5U908LJoJd8wgP+7lafjbsLAcUgGYAmKvUXhQNIj+0b6t0V6XhPT5L/k1qnFckIB2yrr8daK5G9FzxQ7VD6V7GgR6oA6mbXcHTXH1Ad1X32DKVeyTLvlbSK13naVSLgbTys0m/J6QY5OoJahCtizZg8s05tfnI72/ITzczwxg0i2QWqrwGmymAS2KmhCHsAbK2pchqIOtm78UW6gR+LkCGhJ256get9IoCcmoZf/RJ/gCBvlWqpN8Msnefy7iI2/jJwqDmWuwdO6D8Y3UI2dOrnKZGxjqQa/OCttRME+iD73ILdKEV0X3DhrOFITbbz8ypU+Wpj9T72nqiKUyh80l4eGkUz1GLFILiG00G56KQUuWKJtPuOBjwq61Bw5YLUYYR2OMDo82zX6P+HhgrJUIfQQCJvOYFhzIf9ne/Jt0pa42URzM3qImJN0C6cgUmbVwsVF4LpZVGeEJC/4f8e/V8uBrP+0IRsf975KH1RbKMf5OXnxxZNKNjW0rVweqgxVmSC7gK9QME/XsTya6qEO9KHwfNyyiUzorTRjg0E3Er4QbPso3Yp+IDO16nYUU/AGpAYO7lwU5CRZ2AXEmRaMbY50mH3b2cqMrmfBPuyMTCKQ5PTNNRum4P5Zno5r7nrK81jgh40wO0yR3n8k5jvd+kEJLU5WO1VWwiea0RONtZZkjzu2dn9WGCSBqSgi5MfeA== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(1800799024)(82310400026)(23010399003)(36860700016)(6133799003)(56012099006)(5023799004)(11063799006)(10067099003)(18002099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qtSJ9otGD1z77QzfbkA0ke2gaMS4EeHs/4rEWY5l7lQrlq1WrXdKvFSgGAmuLP8YoO97AeurOXuIk0ojIzKyN46t/XnBPUiKa6wEUhC0b+wtUzPnBLY/q/UZahl7Fs1W22qfWhBMWjp+HM9nhplGmIxXnjVCZl+EEpRBffRj11I3s7PFyB+j3d9xlxretAdIq6lTi0Iyn2EMmoTuziCjsXoeMWcnMkWQ0+MuHJxmDE7MvNheC3/DiHymuZlyx2DhpcmhfEq3p1A2Wf+3cR/3TPZbeP0jg1DNob6o3zwqDeFy/Z2SkMPYMyBPqmSf9YfPKmSNLydZ0LSUUqZpBZoXwjKzKn6CY073EXlRdUPXhtxXbrWLCleec048znmoCuPIvbwGty3dsPs0Qbu8r5hciUbdmvL0upLc7QF4JfIzTL9fRWPNe3Omiut6uJ4/C90u X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Aug 2026 09:46:01.9079 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6fc861cc-7f45-4f68-dfd9-08def20d3202 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CH1PEPF0000AD75.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6047 X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GIT_PATCH_0, LOCAL_AUTHENTICATION_FAIL_SPF, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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: Kyrylo Tkachov Neither an inclusive nor an exclusive or can carry, so an operand whose set bits all lie below the shift count contributes nothing to the result: int f (int a, int b) { return (a ^ (b & 1)) >> 1; } aarch64 -O2 before: and w1, w1, 1 eor w0, w1, w0 asr w0, w0, 1 after: asr w0, w0, 1 The set bits are read from tree_nonzero_bits, so the rule also fires when the operand is a boolean, a narrow value or anything else whose range the middle end already knows. Found by mining the optimized dumps of real code, where the shape comes from flag bits packed into the low bits of a word. There is deliberately no single use restriction. Most real instances keep the exclusive or alive for another use and still save the shift's operand being computed on this path. Bootstrapped and tested on aarch64-none-linux-gnu. Ok for trunk? Thanks, Kyrill gcc/ChangeLog: * match.pd ((X | Y) >> C, (X ^ Y) >> C): New simplification. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/shift-drops-bitop-1.c: New test. Signed-off-by: Kyrylo Tkachov --- gcc/match.pd | 11 ++++++++++ .../gcc.dg/tree-ssa/shift-drops-bitop-1.c | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/shift-drops-bitop-1.c diff --git a/gcc/match.pd b/gcc/match.pd index b27d9a0bad0..21bcfd069d4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5032,6 +5032,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) - TYPE_PRECISION (TREE_TYPE (@2))))) (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1)))) +/* (X op Y) >> C -> X >> C when every set bit of Y lies below bit C. + Neither an inclusive nor an exclusive or can carry into the bits the + shift keeps, so Y contributes nothing to the result. */ +(for op (bit_ior bit_xor) + (simplify + (rshift (op:c @0 @1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (type) + && wi::ltu_p (wi::to_wide (@2), element_precision (type)) + && (tree_nonzero_bits (@1) + & wi::mask (tree_to_uhwi (@2), true, element_precision (type))) == 0) + (rshift @0 @2)))) #if GIMPLE /* (X >> C1) << (C1 + C2) -> X << C2 if the low C1 bits of X are zero. */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/shift-drops-bitop-1.c b/gcc/testsuite/gcc.dg/tree-ssa/shift-drops-bitop-1.c new file mode 100644 index 00000000000..fe9cca34b4d --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/shift-drops-bitop-1.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-original -fdump-tree-optimized" } */ + +/* Neither an inclusive nor an exclusive or can carry, so an operand whose + set bits all lie below the shift count contributes nothing. */ + +int f1 (int a, int b) { return (a ^ (b & 1)) >> 1; } +unsigned f2 (unsigned a, unsigned b) { return (a | (b & 7)) >> 3; } +long f3 (long a, int c) { return (a ^ (long) (c != 0)) >> 1; } + +/* GENERIC folding must preserve evaluation of the discarded operand. */ +int side; +int f4 (int a, int b) { return (a ^ ((side++, b) & 1)) >> 1; } + +/* Bit 1 of the mask survives the shift, so the exclusive or stays. */ +int keep (int a, int b) { return (a ^ (b & 3)) >> 1; } + +/* { dg-final { scan-tree-dump-times " \\^ " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-not " \\| " "optimized" } } */ +/* { dg-final { scan-tree-dump-times " \\^ " 2 "original" } } */ +/* { dg-final { scan-tree-dump-not " \\| " "original" } } */ +/* { dg-final { scan-tree-dump-times "side\\+\\+" 1 "original" } } */