From patchwork Thu Oct 24 14:07:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 2001746 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=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=LJX2SQw0; 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 4XZ7866Zmdz1xwy for ; Fri, 25 Oct 2024 01:09:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CB9CD3858C98 for ; Thu, 24 Oct 2024 14:09:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by sourceware.org (Postfix) with ESMTPS id 2EC8F3858CDB for ; Thu, 24 Oct 2024 14:08:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2EC8F3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2EC8F3858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729778940; cv=none; b=TugXOMXNWK2Sc8kqu4B4GabNg8TSWc+dLhLIYewskqiTXJW8LtLBj6O1zKoVaUI8GrFwUN/IDARSN+ZyQsQmK75WAs86D7I7lW5yTi/xK/q8GNEBDaf5+9QDR3Kt4FjiWyB+tSe2w5ljgqALYSIsZYO4hai+7QELWqnhqpDCaXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729778940; c=relaxed/simple; bh=rX8h/LFo81OEtkhCskSdyFAtaJwgC+S88Fx6HqvKZ2w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=I2uAU6Ctf9CwnhAP2/kCT3aZZxr2ABluljSs8QXMgjKsL9TRKsl3gagMvGcEnDd/SnVMjwMLZgLzg/5ohsI1bw3R7WwPoBTWv5h9kSekZJ5qnwsywByGCR1DPFtyHskpP5pcM0u+k3Nn7cZcTAr7NWBNT0O2A1M69JarR29n+cg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729778937; x=1761314937; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=rX8h/LFo81OEtkhCskSdyFAtaJwgC+S88Fx6HqvKZ2w=; b=LJX2SQw0Z/51Dvfk65NPGwKqJi11fP2noED/8l1jysSeiJjgyjqKDuCQ 0MDCOichCxr8v7KGlZLnVdYLBMJLpcYD9a67Uh9FAt2PNnADoehwTE15S e2ofNcj0GU4u3HBujMrst+0REmm2xn2O9SAztASRdcAP0dlkd+PPGNElf LrTD4n4tr1ul4LT+XSpvb3e59AKCqs7orrmaYdHKGCQeIW5O+qEu07MKZ lmdpFsjw7ve2PaFBW9iWPuzkXydbJ/i424tp02ABdKJ4/CyEztE2R7++0 /sWhr1mMp380E1FwTOG5KEPdYLPNt8558CVMddPqCzxV7yr3Dk6o+0vR3 g==; X-CSE-ConnectionGUID: VOVrvYa9R5KWsQEZEiMGoA== X-CSE-MsgGUID: Izb370QBSz2fG8b45JLuYg== X-IronPort-AV: E=McAfee;i="6700,10204,11235"; a="39997268" X-IronPort-AV: E=Sophos;i="6.11,229,1725346800"; d="scan'208";a="39997268" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2024 07:08:56 -0700 X-CSE-ConnectionGUID: LXnjMHYQR4m5nf6h8opS3w== X-CSE-MsgGUID: un9tlOWvTW+dfbQ/5PT0sQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,229,1725346800"; d="scan'208";a="85205356" Received: from panli.sh.intel.com ([10.239.154.73]) by fmviesa004.fm.intel.com with ESMTP; 24 Oct 2024 07:08:54 -0700 From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, Tamar.Christina@arm.com, juzhe.zhong@rivai.ai, kito.cheng@gmail.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH] Match: Simplify branch form 3 of unsigned SAT_ADD into branchless Date: Thu, 24 Oct 2024 22:07:09 +0800 Message-ID: <20241024140709.4153038-1-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, 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 From: Pan Li There are sorts of forms for the unsigned SAT_ADD. Some of them are complicated while others are cheap. This patch would like to simplify the complicated form into the cheap ones. For example as below: From the form 3 (branch): SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1. To (branchless): SAT_U_ADD = (X + Y) | - ((X + Y) < X). #define T uint8_t T sat_add_u_1 (T x, T y) { return (T)(x + y) >= x ? (x + y) : -1; } Before this patch: 1 │ uint8_t sat_add_u_1 (uint8_t x, uint8_t y) 2 │ { 3 │ uint8_t D.2809; 4 │ 5 │ _1 = x + y; 6 │ if (x <= _1) goto ; else goto ; 7 │ : 8 │ D.2809 = x + y; 9 │ goto ; 10 │ : 11 │ D.2809 = 255; 12 │ : 13 │ return D.2809; 14 │ } After this patch: 1 │ uint8_t sat_add_u_1 (uint8_t x, uint8_t y) 2 │ { 3 │ uint8_t D.2809; 4 │ 5 │ _1 = x + y; 6 │ _2 = x + y; 7 │ _3 = x > _2; 8 │ _4 = (unsigned char) _3; 9 │ _5 = -_4; 10 │ D.2809 = _1 | _5; 11 │ return D.2809; 12 │ } The simplify doesn't need to check if target support the SAT_ADD, it is somehow the optimization in gimple level. The below test suites are passed for this patch. * The rv64gcv fully regression test. * The x86 bootstrap test. * The x86 fully regression test. gcc/ChangeLog: * match.pd: Remove unsigned branch form 3 for SAT_ADD, and add simplify to branchless instead. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c: New test. * gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c: New test. * gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c: New test. * gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c: New test. Signed-off-by: Pan Li --- gcc/match.pd | 11 +++++++---- .../gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c | 15 +++++++++++++++ .../gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c | 15 +++++++++++++++ .../gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c | 15 +++++++++++++++ .../gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c | 15 +++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c diff --git a/gcc/match.pd b/gcc/match.pd index 0455dfa6993..0ecd44bd51a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3141,10 +3141,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (match (unsigned_integer_sat_add @0 @1) (bit_ior:c (usadd_left_part_2 @0 @1) (usadd_right_part_2 @0 @1))) -/* Unsigned saturation add, case 3 (branch with ge): - SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1. */ -(match (unsigned_integer_sat_add @0 @1) - (cond^ (ge (usadd_left_part_1@2 @0 @1) @0) @2 integer_minus_onep)) +/* Simplify SAT_U_ADD to the cheap form + From: SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1. + To: SAT_U_ADD = (X + Y) | - ((X + Y) < X). */ +(simplify (cond (ge (plus:c@2 @0 @1) @0) @2 integer_minus_onep) + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && types_match (type, @0, @1)) + (bit_ior @2 (negate (convert (lt @2 @0)))))) /* Unsigned saturation add, case 4 (branch with lt): SAT_U_ADD = (X + Y) < x ? -1 : (X + Y). */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c new file mode 100644 index 00000000000..7617fbaca45 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include + +#define T uint16_t + +T sat_add_u_1 (T x, T y) +{ + return (T)(x + y) >= x ? (x + y) : -1; +} + +/* { dg-final { scan-tree-dump-not " if " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " else " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " goto " "gimple" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c new file mode 100644 index 00000000000..efea85bfbb7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include + +#define T uint32_t + +T sat_add_u_1 (T x, T y) +{ + return (T)(x + y) >= x ? (x + y) : -1; +} + +/* { dg-final { scan-tree-dump-not " if " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " else " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " goto " "gimple" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c new file mode 100644 index 00000000000..b7b55d72ede --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include + +#define T uint64_t + +T sat_add_u_1 (T x, T y) +{ + return (T)(x + y) >= x ? (x + y) : -1; +} + +/* { dg-final { scan-tree-dump-not " if " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " else " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " goto " "gimple" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c new file mode 100644 index 00000000000..77e9af3dd29 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include + +#define T uint8_t + +T sat_add_u_1 (T x, T y) +{ + return (T)(x + y) >= x ? (x + y) : -1; +} + +/* { dg-final { scan-tree-dump-not " if " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " else " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " goto " "gimple" } } */