* [PATCH] xtensa: Make use of CLAMPS instruction if configured
[not found] <76202f6e-a28a-9132-8838-aaff0b252847.ref@yahoo.co.jp>
@ 2023-02-26 17:27 ` Takayuki 'January June' Suwa
2023-02-27 1:26 ` Max Filippov
2023-02-27 12:10 ` Max Filippov
0 siblings, 2 replies; 3+ messages in thread
From: Takayuki 'January June' Suwa @ 2023-02-26 17:27 UTC (permalink / raw)
To: GCC Patches; +Cc: Max Filippov
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")])
+\f
+;; 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")])
+
\f
;; Count redundant leading sign bits and leading/trailing zeros,
;; and find first bit.
--
2.30.2
(Totally off-topic, but do you know anything about the SALT/SALTU instructions?
I see them in the "Core Architecture Instructions" in a recent Cadence document
but not in slightly older Tensilica one...)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] xtensa: Make use of CLAMPS instruction if configured
2023-02-26 17:27 ` [PATCH] xtensa: Make use of CLAMPS instruction if configured Takayuki 'January June' Suwa
@ 2023-02-27 1:26 ` Max Filippov
2023-02-27 12:10 ` Max Filippov
1 sibling, 0 replies; 3+ messages in thread
From: Max Filippov @ 2023-02-27 1:26 UTC (permalink / raw)
To: Takayuki 'January June' Suwa; +Cc: GCC Patches
On Sun, Feb 26, 2023 at 9:27 AM Takayuki 'January June' Suwa
<jjsuwa_sys3175@yahoo.co.jp> wrote:
> This patch introduces the use of CLAMPS instruction when the instruction
> is configured.
Testing.
> (Totally off-topic, but do you know anything about the SALT/SALTU instructions?
> I see them in the "Core Architecture Instructions" in a recent Cadence document
> but not in slightly older Tensilica one...)
Yes, they are a part of the core xtensa instruction set since the
release RG-2015.0.
I believe this translates to the test (XTENSA_MARCH_EARLIEST >= 260000).
There's a chapter "Added instructions" at the end of the xtensa ISA book with
the list of such opcodes.
--
Thanks.
-- Max
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] xtensa: Make use of CLAMPS instruction if configured
2023-02-26 17:27 ` [PATCH] xtensa: Make use of CLAMPS instruction if configured Takayuki 'January June' Suwa
2023-02-27 1:26 ` Max Filippov
@ 2023-02-27 12:10 ` Max Filippov
1 sibling, 0 replies; 3+ messages in thread
From: Max Filippov @ 2023-02-27 12:10 UTC (permalink / raw)
To: Takayuki 'January June' Suwa; +Cc: GCC Patches
On Sun, Feb 26, 2023 at 9:27 AM Takayuki 'January June' Suwa
<jjsuwa_sys3175@yahoo.co.jp> wrote:
>
> 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(+)
Regtested for target=xtensa-linux-uclibc, no new regressions.
Committed to master.
--
Thanks.
-- Max
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-02-27 12:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <76202f6e-a28a-9132-8838-aaff0b252847.ref@yahoo.co.jp>
2023-02-26 17:27 ` [PATCH] xtensa: Make use of CLAMPS instruction if configured Takayuki 'January June' Suwa
2023-02-27 1:26 ` Max Filippov
2023-02-27 12:10 ` Max Filippov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).