From patchwork Sun Feb 26 17:27:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takayuki 'January June' Suwa X-Patchwork-Id: 65653 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E009385781F for ; Sun, 26 Feb 2023 17:28:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E009385781F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677432501; bh=oNos5PAgh1McrBxaRKWKpAQM0DabMVwmfcudS71NU70=; h=Date:To:Cc:Subject:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=F55haQSxK46HUOSAqm4lnGNq34K0EJZJZev+ejbSYpOSlYOq2LcbduNu4MlSX0Srr QjcqTVsxNRCrkC0rY07wecFJMBK6AMlxrfF+XINXnnGdBLkuvuZU9xg4qEW3wMlymE NXaZM92K2tvfwHeJgozWqhiTmKBlfcj0102gizLM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sonicconh6001-vm4.mail.ssk.yahoo.co.jp (sonicconh6001-vm4.mail.ssk.yahoo.co.jp [182.22.37.13]) by sourceware.org (Postfix) with ESMTPS id A72893858D28 for ; Sun, 26 Feb 2023 17:27:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A72893858D28 X-YMail-OSG: Ylde1JMVM1kMDFcwAPYtdU22e2GLx6XWm9dyYaCtpz8WRA5kFr9gHCOccZKcco_ F_ZHcgQSvRnSsez3u5Ea38BUjrQCLeY7vPffpEuJa1jVJud_4PrAcUk8qSwq2agOnc0maZBKBFGt jntzPWIzfORO_HryMI1G0vnuRTuR1zYDOrBv_vehxy_qEBUzaKyOu_pGDdhPzkzX3Dglfu9ypQAT 52xPgvMSg1Zpn5qtgcj8moJH4oGgA6Gy2tDm2YBYszfqGOdkaB_z1PAeswJ.I458J9CJA3sS2Wtp lRhwJC1EJftC8IZI.O9.xDmAhCvJ0SDkwt5BG1oS2UjnegG2xxHZf1hZFvSEs93Nezu1i39NWviD K9tIXZsNngWLJ7tppUXtr6CJWmw9kQ_vJGI1WAgQQCtnppnrWRZ5c6bYTVqY2jmb2NTnka6iz.KK L2NaCQC4xrKMMhJKOTE9anrmghM3Ff8Hgo9smEI_eiY7z0SeRxXy7y9ccSzakQsnIP58nlm_dC1z YObLDUFqGNnIHoPXdXmWLdyRe7N4b9o8MSZV.4w8YddA12ycweVM7VV72NDUBv2E2EzyG452d1hb Nol1Q1qHXLudOVz2xJDhqfvgXX4gmSLTJlbCqLTbKDlI4adsfrkpJiwxA.5BJIXf4yXchBZ340jM 4iTOR2DZowDEmk4F1en2AyrvhVcbXODHlrxUknSpffmj0yDnxjgqHWIIyRmfQU2.DWXYttwhClNr l4FW3iH.tHphuecsY43dDz2wxSdZi9F9QLVeusnW_SMw69Ezqx8_OjLoXXFr6g.0V_W4IGVv9JiQ QgmzwictlmdaRQW6Zq0PjNMlZ9XNd6VzaVKzihA5hXR_TmYCdX4z8zoTJAcli5G957_QvfFKEKwA OSFTKlYCCdXKU.SrMO1xkt3O3Uhk7gPu5Fpn5ZoMPTghzkf5.TC.jehgkEwoI.P7IfP6rPQALoTz 1a2g- Received: from sonicgw.mail.yahoo.co.jp by sonicconh6001.mail.ssk.yahoo.co.jp with HTTP; Sun, 26 Feb 2023 17:27:46 +0000 Received: by smtphe6009.mail.ssk.ynwp.yahoo.co.jp (YJ Hermes SMTP Server) with ESMTPA ID d20ac510197ff6dfb8685b544ec23690; Mon, 27 Feb 2023 02:27:43 +0900 (JST) Message-ID: <76202f6e-a28a-9132-8838-aaff0b252847@yahoo.co.jp> Date: Mon, 27 Feb 2023 02:27:42 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 To: GCC Patches Cc: Max Filippov Subject: [PATCH] xtensa: Make use of CLAMPS instruction if configured References: <76202f6e-a28a-9132-8838-aaff0b252847.ref@yahoo.co.jp> X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_DMARC_STATUS, NML_ADSP_CUSTOM_MED, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Takayuki 'January June' Suwa via Gcc-patches From: Takayuki 'January June' Suwa Reply-To: Takayuki 'January June' Suwa Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch introduces the use of CLAMPS instruction when the instruction is configured. /* example */ int test(int a) { if (a < -512) return -512; if (a > 511) return 511; return a; } ;; prereq: TARGET_CLAMPS test: clamps a2, a2, 9 ret.n gcc/ChangeLog: * config/xtensa/xtensa-protos.h (xtensa_match_CLAMPS_imms_p): New prototype. * config/xtensa/xtensa.cc (xtensa_match_CLAMPS_imms_p): New function. * config/xtensa/xtensa.h (TARGET_CLAMPS): New macro definition. * config/xtensa/xtensa.md (*xtensa_clamps): New insn pattern. --- gcc/config/xtensa/xtensa-protos.h | 1 + gcc/config/xtensa/xtensa.cc | 13 +++++++++++ gcc/config/xtensa/xtensa.h | 1 + gcc/config/xtensa/xtensa.md | 37 +++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/gcc/config/xtensa/xtensa-protos.h b/gcc/config/xtensa/xtensa-protos.h index c81cf94323a..64cbf27c248 100644 --- a/gcc/config/xtensa/xtensa-protos.h +++ b/gcc/config/xtensa/xtensa-protos.h @@ -60,6 +60,7 @@ extern bool xtensa_tls_referenced_p (rtx); extern enum rtx_code xtensa_shlrd_which_direction (rtx, rtx); extern bool xtensa_split1_finished_p (void); extern void xtensa_split_DI_reg_imm (rtx *); +extern bool xtensa_match_CLAMPS_imms_p (rtx, rtx); #ifdef TREE_CODE extern void init_cumulative_args (CUMULATIVE_ARGS *, int); diff --git a/gcc/config/xtensa/xtensa.cc b/gcc/config/xtensa/xtensa.cc index 5044bc25c2f..7287aa7a258 100644 --- a/gcc/config/xtensa/xtensa.cc +++ b/gcc/config/xtensa/xtensa.cc @@ -2611,6 +2611,19 @@ xtensa_emit_add_imm (rtx dst, rtx src, HOST_WIDE_INT imm, rtx scratch, } +/* Return true if the constants used in the application of smin() following + smax() meet the specifications of the CLAMPS machine instruction. */ +bool +xtensa_match_CLAMPS_imms_p (rtx cst_max, rtx cst_min) +{ + int max, min; + + return IN_RANGE (max = exact_log2 (-INTVAL (cst_max)), 7, 22) + && IN_RANGE (min = exact_log2 (INTVAL (cst_min) + 1), 7, 22) + && max == min; +} + + /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */ static bool diff --git a/gcc/config/xtensa/xtensa.h b/gcc/config/xtensa/xtensa.h index d4cd5def7b5..058602e44ee 100644 --- a/gcc/config/xtensa/xtensa.h +++ b/gcc/config/xtensa/xtensa.h @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #define TARGET_NSA XCHAL_HAVE_NSA #define TARGET_MINMAX XCHAL_HAVE_MINMAX #define TARGET_SEXT XCHAL_HAVE_SEXT +#define TARGET_CLAMPS XCHAL_HAVE_CLAMPS #define TARGET_BOOLEANS XCHAL_HAVE_BOOLEANS #define TARGET_HARD_FLOAT XCHAL_HAVE_FP #define TARGET_HARD_FLOAT_DIV XCHAL_HAVE_FP_DIV diff --git a/gcc/config/xtensa/xtensa.md b/gcc/config/xtensa/xtensa.md index b60dec2447f..3521fa33b47 100644 --- a/gcc/config/xtensa/xtensa.md +++ b/gcc/config/xtensa/xtensa.md @@ -446,6 +446,43 @@ (set_attr "mode" "SI") (set_attr "length" "3")]) + +;; Signed clamp. + +(define_insn_and_split "*xtensa_clamps" + [(set (match_operand:SI 0 "register_operand" "=a") + (smax:SI (smin:SI (match_operand:SI 1 "register_operand" "r") + (match_operand:SI 2 "const_int_operand" "i")) + (match_operand:SI 3 "const_int_operand" "i")))] + "TARGET_CLAMPS + && xtensa_match_CLAMPS_imms_p (operands[3], operands[2])" + "#" + "&& 1" + [(set (match_dup 0) + (smin:SI (smax:SI (match_dup 1) + (match_dup 3)) + (match_dup 2)))] + "" + [(set_attr "type" "arith") + (set_attr "mode" "SI") + (set_attr "length" "3")]) + +(define_insn "*xtensa_clamps" + [(set (match_operand:SI 0 "register_operand" "=a") + (smin:SI (smax:SI (match_operand:SI 1 "register_operand" "r") + (match_operand:SI 2 "const_int_operand" "i")) + (match_operand:SI 3 "const_int_operand" "i")))] + "TARGET_CLAMPS + && xtensa_match_CLAMPS_imms_p (operands[2], operands[3])" +{ + static char result[64]; + sprintf (result, "clamps\t%%0, %%1, %d", floor_log2 (-INTVAL (operands[2]))); + return result; +} + [(set_attr "type" "arith") + (set_attr "mode" "SI") + (set_attr "length" "3")]) + ;; Count redundant leading sign bits and leading/trailing zeros, ;; and find first bit.