From patchwork Thu Oct 31 06:27:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 2004548 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=TObwxNk7; 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 4XfDfW0GJHz1xwF for ; Thu, 31 Oct 2024 17:31:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 351983857B98 for ; Thu, 31 Oct 2024 06:31:29 +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 [198.175.65.16]) by sourceware.org (Postfix) with ESMTPS id 356A23858D3C for ; Thu, 31 Oct 2024 06:29:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 356A23858D3C 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 356A23858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730356189; cv=none; b=Ct8809mX2rlqOYkB03kk1wSbDj5KrauBRTb3a+zO/sO2+vmUBUY37QnxgZHoeOup4OWiW7Ny/iGCnOModPZ70Gdau0ZOWfqXGw45vzJ32E6sWArkJcnttdtFv2ouYx5dJ4OWx/mv3SeZUAPgejeWAqCJA6AimdUH+xuONttvoQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730356189; c=relaxed/simple; bh=yxk5ZUe1CCgtraS2UOhOKRUUECj/eC6ekLJhUEaR7jA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bTe85EIOaYkAnoFs7t5qBQ6MmiYdOu2ju4NJP/iDeq0VAMPdFdHsbaeSx7qhl5r0PWKu/5nCE1iCSect7Wxf6cMNE0neWVpz52A8OveT2yAWBwhahkku89SxCTqVjrtbZdeOPQfFcn/qcRTw3im3TT+DDH2oc5c2oH+kmk0Wc5E= 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=1730356183; x=1761892183; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yxk5ZUe1CCgtraS2UOhOKRUUECj/eC6ekLJhUEaR7jA=; b=TObwxNk7i2DvAatM8hJ4cNMV33wthId1A8VdOUfcPIEBpfpmEzUUTLOA OxuJpwzyPAYsLXNvQrUJLUgCrL5OfvCCYgmvkWd7FeQuwG899GrTLje7u twzPYUn7882lDeilRXr6NUBLEBF+Q01xzxPIvOHm+WodjufVEGZGeUSzp vsf/x/ZBn7RdHDrBZO2MhSqLQhD5pEFMzRgrHAfbws/qYsppWTmi1N7KG t63wWN2n6pdsjY0aWU6YaLqKDjKF0HX0IgSU3j3KSdxRr3y2fHlGGkG5c Vou8VGSnIOm2OKwZ1WO01trNc5OfZvgJqp9uC23Roc04f+zdWfAH6VEmv g==; X-CSE-ConnectionGUID: Qv9XUv2EQwWSp0Ys1qRTZw== X-CSE-MsgGUID: I77crlsQS56wdE+b3erhUg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30238427" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30238427" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 23:29:43 -0700 X-CSE-ConnectionGUID: aHr5KEOMRA6eHPSJ+vaoFQ== X-CSE-MsgGUID: AyqZ/enUT6uJqPaqjxovJA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,247,1725346800"; d="scan'208";a="82455021" Received: from panli.sh.intel.com ([10.239.154.73]) by orviesa010.jf.intel.com with ESMTP; 30 Oct 2024 23:29:40 -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 v2 03/10] Match: Simplify branch form 8 of unsigned SAT_ADD into branchless Date: Thu, 31 Oct 2024 14:27:35 +0800 Message-ID: <20241031062742.2709845-3-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241031062742.2709845-1-pan2.li@intel.com> References: <20241031062742.2709845-1-pan2.li@intel.com> MIME-Version: 1.0 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 8 (branch): SAT_U_ADD = x > (T)(x + y) ? -1 : (x + y). To (branchless): SAT_U_ADD = (X + Y) | - ((X + Y) < X). #define T uint8_t T sat_add_u_1 (T x, T y) { return x > (T)(x + y) ? -1 : (x + y); } 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 8 for SAT_ADD, and add simplify to branchless instead. gcc/testsuite/ChangeLog: * gcc.dg/sat_arith_simplify.h: Add test helper macros. * gcc.dg/sat_u_add-simplify-4-u16.c: New test. * gcc.dg/sat_u_add-simplify-4-u32.c: New test. * gcc.dg/sat_u_add-simplify-4-u64.c: New test. * gcc.dg/sat_u_add-simplify-4-u8.c: New test. Signed-off-by: Pan Li --- gcc/match.pd | 9 ++++----- gcc/testsuite/gcc.dg/sat_arith_simplify.h | 6 ++++++ gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u16.c | 11 +++++++++++ gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u32.c | 11 +++++++++++ gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u64.c | 11 +++++++++++ gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u8.c | 11 +++++++++++ 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u16.c create mode 100644 gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u32.c create mode 100644 gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u64.c create mode 100644 gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u8.c diff --git a/gcc/match.pd b/gcc/match.pd index 5bfba46b0ce..2390e96672e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3157,6 +3157,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (cond (le @0 (plus:c@2 @0 @1)) @2 integer_minus_onep) (if (types_match (type, @0, @1)) (bit_ior (plus@2 @0 @1) (negate (convert (lt @2 @0)))))) + /* From SAT_ADD = x > (X + Y) ? -1 : (X + Y). */ + (simplify (cond (gt @0 (plus:c@2 @0 @1)) integer_minus_onep @2) + (if (types_match (type, @0, @1)) + (bit_ior (plus@2 @0 @1) (negate (convert (lt @2 @0)))))) /* From SAT_U_ADD = (X + Y) < x ? -1 : (X + Y). */ (simplify (cond (lt (plus:c@2 @0 @1) @0) integer_minus_onep @2) (if (types_match (type, @0, @1)) @@ -3174,11 +3178,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop) integer_minus_onep (usadd_left_part_2 @0 @1))) -/* Unsigned saturation add, case 8 (branch with gt): - SAT_ADD = x > (X + Y) ? -1 : (X + Y). */ -(match (unsigned_integer_sat_add @0 @1) - (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2)) - /* Unsigned saturation add, case 9 (one op is imm): SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1. */ (match (unsigned_integer_sat_add @0 @1) diff --git a/gcc/testsuite/gcc.dg/sat_arith_simplify.h b/gcc/testsuite/gcc.dg/sat_arith_simplify.h index a630936108f..89577ffabb1 100644 --- a/gcc/testsuite/gcc.dg/sat_arith_simplify.h +++ b/gcc/testsuite/gcc.dg/sat_arith_simplify.h @@ -13,4 +13,10 @@ T sat_u_add_##T##_3 (T x, T y) \ return x <= (T)(x + y) ? (x + y) : -1; \ } +#define DEF_SAT_U_ADD_4(T) \ +T sat_u_add_##T##_4 (T x, T y) \ +{ \ + return x > (T)(x + y) ? -1 : (x + y); \ +} + #endif diff --git a/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u16.c b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u16.c new file mode 100644 index 00000000000..841ff5a22bf --- /dev/null +++ b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u16.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include +#include "sat_arith_simplify.h" + +DEF_SAT_U_ADD_4 (uint16_t) + +/* { 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/sat_u_add-simplify-4-u32.c b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u32.c new file mode 100644 index 00000000000..6b305671fd9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u32.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include +#include "sat_arith_simplify.h" + +DEF_SAT_U_ADD_4 (uint32_t) + +/* { 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/sat_u_add-simplify-4-u64.c b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u64.c new file mode 100644 index 00000000000..0aac0fddab1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u64.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include +#include "sat_arith_simplify.h" + +DEF_SAT_U_ADD_4 (uint64_t) + +/* { 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/sat_u_add-simplify-4-u8.c b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u8.c new file mode 100644 index 00000000000..03bfdf6b34f --- /dev/null +++ b/gcc/testsuite/gcc.dg/sat_u_add-simplify-4-u8.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-gimple-details" } */ + +#include +#include "sat_arith_simplify.h" + +DEF_SAT_U_ADD_4 (uint8_t) + +/* { dg-final { scan-tree-dump-not " if " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " else " "gimple" } } */ +/* { dg-final { scan-tree-dump-not " goto " "gimple" } } */