Skip to content

Commit 90e38c4

Browse files
author
Jennifer Schmitz
committed
SVE intrinsics: Add constant folding for svindex.
This patch folds svindex with constant arguments into a vector series. We implemented this in svindex_impl::fold using the function build_vec_series. For example, svuint64_t f1 () { return svindex_u642 (10, 3); } compiled with -O2 -march=armv8.2-a+sve, is folded to {10, 13, 16, ...} in the gimple pass lower. This optimization benefits cases where svindex is used in combination with other gimple-level optimizations. For example, svuint64_t f2 () { return svmul_x (svptrue_b64 (), svindex_u64 (10, 3), 5); } has previously been compiled to f2: index z0.d, riscvarchive#10, #3 mul z0.d, z0.d, #5 ret Now, it is compiled to f2: mov x0, 50 index z0.d, x0, riscvarchive#15 ret We added test cases checking - the application of the transform during gimple for constant arguments, - the interaction with another gimple-level optimization. The patch was bootstrapped and regtested on aarch64-linux-gnu, no regression. OK for mainline? Signed-off-by: Jennifer Schmitz <[email protected]> gcc/ * config/aarch64/aarch64-sve-builtins-base.cc (svindex_impl::fold): Add constant folding. gcc/testsuite/ * gcc.target/aarch64/sve/index_const_fold.c: New test.
1 parent 078f7c4 commit 90e38c4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

gcc/config/aarch64/aarch64-sve-builtins-base.cc

+14
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,20 @@ class svdup_neonq_impl : public function_base
13011301

13021302
class svindex_impl : public function_base
13031303
{
1304+
public:
1305+
gimple *
1306+
fold (gimple_folder &f) const override
1307+
{
1308+
/* Apply constant folding if base and step are integer constants. */
1309+
tree vec_type = TREE_TYPE (f.lhs);
1310+
tree base = gimple_call_arg (f.call, 0);
1311+
tree step = gimple_call_arg (f.call, 1);
1312+
if (TREE_CODE (base) != INTEGER_CST || TREE_CODE (step) != INTEGER_CST)
1313+
return NULL;
1314+
return gimple_build_assign (f.lhs,
1315+
build_vec_series (vec_type, base, step));
1316+
}
1317+
13041318
public:
13051319
rtx
13061320
expand (function_expander &e) const override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2 -fdump-tree-optimized" } */
3+
4+
#include <arm_sve.h>
5+
#include <stdint.h>
6+
7+
#define INDEX_CONST(TYPE, TY) \
8+
sv##TYPE f_##TY##_index_const () \
9+
{ \
10+
return svindex_##TY (10, 3); \
11+
}
12+
13+
#define MULT_INDEX(TYPE, TY) \
14+
sv##TYPE f_##TY##_mult_index () \
15+
{ \
16+
return svmul_x (svptrue_b8 (), \
17+
svindex_##TY (10, 3), \
18+
5); \
19+
}
20+
21+
#define ALL_TESTS(TYPE, TY) \
22+
INDEX_CONST (TYPE, TY) \
23+
MULT_INDEX (TYPE, TY)
24+
25+
ALL_TESTS (uint8_t, u8)
26+
ALL_TESTS (uint16_t, u16)
27+
ALL_TESTS (uint32_t, u32)
28+
ALL_TESTS (uint64_t, u64)
29+
ALL_TESTS (int8_t, s8)
30+
ALL_TESTS (int16_t, s16)
31+
ALL_TESTS (int32_t, s32)
32+
ALL_TESTS (int64_t, s64)
33+
34+
/* { dg-final { scan-tree-dump-times "return \\{ 10, 13, 16, ... \\}" 8 "optimized" } } */
35+
/* { dg-final { scan-tree-dump-times "return \\{ 50, 65, 80, ... \\}" 8 "optimized" } } */

0 commit comments

Comments
 (0)