From patchwork Wed Sep 2 14:52:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyrylo Tkachov X-Patchwork-Id: 142720 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 166A84BA79BA for ; Wed, 2 Sep 2026 14:54:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 166A84BA79BA 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=RvmdZ3o+ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azlp170100005.outbound.protection.outlook.com [IPv6:2a01:111:f403:c005::5]) by sourceware.org (Postfix) with ESMTPS id 8C6634B9DB66 for ; Wed, 2 Sep 2026 14:53:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C6634B9DB66 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 8C6634B9DB66 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2a01:111:f403:c005::5 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1788360828; cv=pass; b=K1eFSlpkT9pDCCclEo+iaZxpnc4fV7evSRIgYfzC/9Sbe5/5LDNhMha7YNa07fnOuAKoOrcsHm/4BbsFq60i7Ws5bf3wHPB/KQMv96tYrotN9b3KD8jvw6kqsQBjh7R1Ib8ctVXZcpFanyhEGPuGIuCJ5502rKkDs6/8iPgcOu8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1788360828; c=relaxed/simple; bh=NnAQhXLD61PW7vwlh2HV6PYxSZEph/cDGTaGuIx2/dg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ifIQPUDsecUH22EJjXLKKQHjy0P8q2YX8SK+OJhrMlwUrWpODiaSAuso1opOK8SYD6RJ9xWJWaf127baAhqL83JXgys0e3Y56ODrm03aOsaW0tFnIBjUJ/el/Rt7PJLW3ec2Bh+FOMJa6OINB/gC3FYBPs+X42zUfTbf/fVYMsQ= 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=RvmdZ3o+ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C6634B9DB66 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GA3h+pz5z96y7AP7x1pchiMIpqv9ylmqgEVyrxR+aoNrFas3UCYNVDfsmtG5QJ4OqcWAJ2ecK+mWTvQ4wHKKu8Ibq1LLp6JsDPi835JtjApAUt5U1bEF2sKFht/aI2n0RM7mW92qN2UYU1PKCZb3ZujonZymokP1e+GAyc45H1z85qkht7XIr/tqhG6CqqLDcCw8lFITPSzZ75wM4WF9mL92285Zk09jL5g/qlZsZW02ZgbGmwT2wExAmA1Zmlx3b0JKtPbleMs8vaqH/DoVEiE0XWxL3RK7pjVixWfJxSdPbtAP+Sn+enFv66IlfvGzraQFTUngzX+kcK0yvu0I8A== 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=olVPoo8AikYy4Q17j67lvkXpHMRRH3k3RbAUlX5D2JM=; b=tHgWYpMTQYB0RcrPhzeAbnneH3PNXaOhgpCPj4x5J96U0TXfMv/GZ4a5aWSvVwxvZVA3hxp22SavRvO0hlPcpzuPDtAxesekpgPOdU1VuaFWdXQ/owFJJmvyJ95sOXoryIj2jut31FFHs/GcGkUPU9KTRjRglpAYQ0qWA2TmfXGB9bVDDIi7l4zBichJkKjRTf/6PAQeIPZu/4ZAxw4zbHFVFSVO0udGKcTaeyrJ54MToERliuhmTBbVVm0hIgr49E9FgccLPltidmcE/k+6WAmzq/1MT0KfdkBxHQoP5aiBklsgvPv+LEDwWw2LR3jG3OfzfbBYj2Lj9JqtEA4F8A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=olVPoo8AikYy4Q17j67lvkXpHMRRH3k3RbAUlX5D2JM=; b=RvmdZ3o+R1Yohr6IJK4UQBgUyhjrlPxI2Vpy0Q2k+E/7kssgdR32QDVIBfmnD6Kxg67+C00q17VQx/JM14sqRuj4EVB2hugEPq+S3L/rpYSLRa5m4Wx8+M3ZcymwdaTYYRPw4ZA061dzMbeuW92crJSf4T1PCEwYhFm2riCj59tGm4qNCmSF4+Tyc5voAIDcJp6Me/Ew5QE0POZzT43NDMhop2GTIbcAi5zCdr+gzbWS0tVG8YLAezhDQ3+KV7hOs933C6CmypM5Kgh0aWnDFClRTR70mT4/Uv/j8awRPTkH0sWu8R75AxYrtU9wEZ846cDGTLkvZykMhVi9H1mJ3A== Received: from SA9PR03CA0016.namprd03.prod.outlook.com (2603:10b6:806:20::21) by DM6PR12MB4172.namprd12.prod.outlook.com (2603:10b6:5:212::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.360.13; Wed, 2 Sep 2026 14:53:40 +0000 Received: from SA2PEPF00001509.namprd04.prod.outlook.com (2603:10b6:806:20:cafe::12) by SA9PR03CA0016.outlook.office365.com (2603:10b6:806:20::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.382.11 via Frontend Transport; Wed, 2 Sep 2026 14:53:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by SA2PEPF00001509.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.382.8 via Frontend Transport; Wed, 2 Sep 2026 14:53:39 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.46; Wed, 2 Sep 2026 07:53:11 -0700 Received: from ktkachov-mlt.nvidia.com (10.126.230.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.46; Wed, 2 Sep 2026 07:53:10 -0700 From: To: CC: Kyrylo Tkachov Subject: [PATCH 04/13] match-sat-alu.pd: Recognize borrow-test saturating subtraction Date: Wed, 2 Sep 2026 16:52:45 +0200 Message-ID: <20260902145254.77832-6-ktkachov@nvidia.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260902145254.77832-1-ktkachov@nvidia.com> References: <20260902145254.77832-1-ktkachov@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [10.126.230.37] X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001509:EE_|DM6PR12MB4172:EE_ X-MS-Office365-Filtering-Correlation-Id: 0647bbbf-9a37-47b4-b359-08df0901f994 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|1800799024|376014|23010399003|36860700016|13003099007|6133799003|10067099003|56012099006|11063799006|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: LKwYSKvvz9JIWNtroy+e12qGHtbC19mSLDesUvLHI1pzsI7qyHmk732Lei8dW4FOQsY6EN3gSfjctP4unOn6rdWuDZEmOqgrcJ+dvb5qMO7wzZYRacQRUtp+ubJyK6ryOz67PWDgbiEdrnBI8+siEv0R9kvefHtweaSrtYyxv+x7iDhXG1oi+ent4+6ozIymo/FPBIcwAYkY1+R0epi9IDTM4OL6TorqxkjaZQV6Ti3IjFLFKBInRjcvZW8ECu3ReWcmYQ4CCoM3LYrYxaV48zbJ94dDQYS0+HT11g79wZMgvKezoQ5OQk3N0LK5oJsWQMYzJiJOOZQin0O+lYuoKP8TXP+0Hkp2VbVgpbBv3B/l5cex9FUk99Xu328/G0dMFJeSJAz5ItMrPL3aM5TPTprX+/rYswoysF0AMBHyehpS5gK/quccvRFFCsmpIFeMH8o9tLkr/Q8eTfS1obONaQtApbfxFCnoIliFRU9DhoyfvMqx9WZRvwLi+WYSuREhuNV1BTIF2rIVhrzdxjJa5a+GXfe7vnF2Hip4wq83YKUfV4zfodk1yf6RH5Bv9m3R2TQdehuOBaG/zaUnZeQNRHPMQc0bo0I+/TchsUwhsRWm2I7dglZlbt5ofGYVCFI/JnkWzOORq1TogZpFiwYFcCytY7b7C2jf/XhjXMFfDeOmsZvBBber/zklGp+GhbpSpJOooF4zXDv3OFXEpx9A4w== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(1800799024)(376014)(23010399003)(36860700016)(13003099007)(6133799003)(10067099003)(56012099006)(11063799006)(18002099003)(22082099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9H5kmH8NFB4w+GRGMMsNKAThLGA3ulALjqkUigrWCaSEmPv9RFVHgMx4yZoiJqy9cwdNIMmiByYpkrfwFT31obzwdNna/WqjvDFQlFZ8EKssQ0tP4j69V/ONPO9qliaT1PS1WfniDproZtIoABRBl99oaBmu9uAXdU5M5jvlVQLSyFosMPfqAEg8sYpGcBrmkedcrJGGwZ/x2JTp8FCUl5bh73jHDyED9X7CKitI5/olMMfUdvHXEgPWaZOobL+AIw1xeVrZL5jLYS9EI8Ez/UjGjB7q6cpJOkubkJvAo5+ciknmAAmz4uE/zJoIwf5JmZCILW3ytiv6FzvGk6wQYoVuT23RQj6snk01GWFSJtApOZ68oadyDea0uf4SV5hL/m0rF6JdpHEnW5tQJwMcK0rolshw9p2qhu8m1ive4/CB3cVdGxqDy1RdKjoXuLdw X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Sep 2026 14:53:39.5437 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0647bbbf-9a37-47b4-b359-08df0901f994 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.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001509.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4172 X-Spam-Status: No, score=-8.1 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, KAM_SHORT, 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 For unsigned subtraction, the wrapped difference is greater than the minuend exactly when the subtraction borrows. These forms are therefore saturating subtraction: R = X - Y R > X ? 0 : R R <= X ? R : 0 Recognize both comparison operand orders and both result arm orders. AArch64 -O3, vector body: before: sub v30.4s, v31.4s, v30.4s cmhs v31.4s, v31.4s, v30.4s and v31.16b, v31.16b, v30.16b after: uqsub v30.4s, v31.4s, v30.4s Three vector operations become one. Remove four existing expected-failure markers for the supported forms. Bootstrapped and tested on aarch64-none-linux-gnu. Ok for trunk? gcc/ChangeLog: * match-sat-alu.pd (unsigned_integer_sat_sub): Add the four forms that compare the difference with the minuend. gcc/testsuite/ChangeLog: * gcc.target/aarch64/sat_u_sub_borrow-1.c: New test. * gcc.target/aarch64/saturating_arithmetic_1.c: Remove the xfail on usub. * gcc.target/aarch64/sve/saturating_arithmetic_1.c: Remove the xfail on usubq. * gcc.target/aarch64/sve/saturating_arithmetic_3.c: Likewise. * gcc.target/aarch64/sve/saturating_arithmetic_4.c: Likewise. Signed-off-by: Kyrylo Tkachov --- gcc/match-sat-alu.pd | 9 ++++++++ .../gcc.target/aarch64/sat_u_sub_borrow-1.c | 22 +++++++++++++++++++ .../aarch64/saturating_arithmetic_1.c | 2 +- .../aarch64/sve/saturating_arithmetic_1.c | 2 +- .../aarch64/sve/saturating_arithmetic_3.c | 2 +- .../aarch64/sve/saturating_arithmetic_4.c | 2 +- 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/sat_u_sub_borrow-1.c diff --git a/gcc/match-sat-alu.pd b/gcc/match-sat-alu.pd index 8c7bc0782a0..dfb2eb9800f 100644 --- a/gcc/match-sat-alu.pd +++ b/gcc/match-sat-alu.pd @@ -131,6 +131,15 @@ along with GCC; see the file COPYING3. If not see /* SAT_U_SUB = (X - Y) * (X >= Y) */ (mult:c (minus @0 @1) (convert (ge @0 @1))) (if (types_match (type, @0, @1)))) + /* The wrapped difference exceeds the minuend exactly when the subtraction + borrows, so a source that names the difference and then tests it against + the minuend is a saturating subtract. */ + (match (unsigned_integer_sat_sub @0 @1) + /* SAT_U_SUB = (X - Y) > X ? 0 : X - Y, and the commuted comparison. */ + (cond^ (gt:c (minus@2 @0 @1) @0) integer_zerop @2)) + (match (unsigned_integer_sat_sub @0 @1) + /* SAT_U_SUB = (X - Y) <= X ? X - Y : 0, and the commuted comparison. */ + (cond^ (le:c (minus@2 @0 @1) @0) @2 integer_zerop)) (match (unsigned_integer_sat_sub @0 @1) /* SAT_U_SUB = X - MIN (X, Y). The MIN has to be single use: while it stays live the saturating subtract is computed beside it instead of diff --git a/gcc/testsuite/gcc.target/aarch64/sat_u_sub_borrow-1.c b/gcc/testsuite/gcc.target/aarch64/sat_u_sub_borrow-1.c new file mode 100644 index 00000000000..8b86cb92922 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sat_u_sub_borrow-1.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; + +/* The wrapped difference is larger than the minuend exactly when the + subtraction borrowed, so each of these is a saturating subtract. */ + +#define DEF(N, T) \ + T f1_##N (T a, T b) { T r = a - b; return r > a ? 0 : r; } \ + T f2_##N (T a, T b) { T r = a - b; if (r > a) r = 0; return r; } \ + T f3_##N (T a, T b) { T r = a - b; return r <= a ? r : 0; } + +DEF (8, u8) +DEF (16, u16) +DEF (32, u32) +DEF (64, u64) + +/* { dg-final { scan-tree-dump-times "\\.SAT_SUB " 12 "optimized" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/saturating_arithmetic_1.c b/gcc/testsuite/gcc.target/aarch64/saturating_arithmetic_1.c index 8fc1569845b..c73a024edec 100644 --- a/gcc/testsuite/gcc.target/aarch64/saturating_arithmetic_1.c +++ b/gcc/testsuite/gcc.target/aarch64/saturating_arithmetic_1.c @@ -19,7 +19,7 @@ ** ret */ /* -** usub: { xfail *-*-* } +** usub: ** dup v([0-9]+).8b, w[01] ** dup v([0-9]+).8b, w[01] ** uqsub b([0-9]+), b\1, b\2 diff --git a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_1.c b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_1.c index 6936e9a2704..4cfab740c40 100644 --- a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_1.c +++ b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_1.c @@ -39,7 +39,7 @@ ** ... */ /* -** usubq: { xfail *-*-* } +** usubq: ** ... ** ld1b\tz([0-9]+)\.b, .* ** ld1b\tz([0-9]+)\.b, .* diff --git a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_3.c b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_3.c index 14e2de59b1e..def7c82e256 100644 --- a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_3.c +++ b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_3.c @@ -40,7 +40,7 @@ ** ... */ /* -** usubq: { xfail *-*-* } +** usubq: ** ... ** ld1w\tz([0-9]+)\.s, .* ** ld1w\tz([0-9]+)\.s, .* diff --git a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_4.c b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_4.c index 05a5786b4ab..5b9f8740ed4 100644 --- a/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_4.c +++ b/gcc/testsuite/gcc.target/aarch64/sve/saturating_arithmetic_4.c @@ -40,7 +40,7 @@ ** ... */ /* -** usubq: { xfail *-*-* } +** usubq: ** ... ** ld1d\tz([0-9]+)\.d, .* ** ld1d\tz([0-9]+)\.d, .*