Skip to content

Commit 4798080

Browse files
Add c[lt]z idiom recognition
This recognises the patterns of the form: while (n & 1) { n >>= 1 } Unfortunately there are currently two issues relating to this patch. Firstly, simplify_using_initial_conditions does not recognise that (n != 0) and ((n & 1) == 0) implies that ((n >> 1) != 0). This preconditions arise following the loop copy-header pass, and the assumptions returned by number_of_iterations_exit_assumptions then prevent final value replacement from using the niter result. I'm not sure what is the best way to fix this - one approach could be to modify simplify_using_initial_conditions to handle this sort of case, but it seems that it basically wants the information that ranger could give anway, so would something like that be a better option? The second issue arises in the vectoriser, which is able to determine that the niter->assumptions are always true. When building with -march=armv8.4-a+sve -S -O3, we get this codegen: foo (unsigned int b) { int c = 0; if (b == 0) return PREC; while (!(b & (1 << (PREC - 1)))) { b <<= 1; c++; } return c; } foo: .LFB0: .cfi_startproc cmp w0, 0 cbz w0, .L6 blt .L7 lsl w1, w0, 1 clz w2, w1 cmp w2, 14 bls .L8 mov x0, 0 cntw x3 add w1, w2, 1 index z1.s, #0, #1 whilelo p0.s, wzr, w1 .L4: add x0, x0, x3 mov p1.b, p0.b mov z0.d, z1.d whilelo p0.s, w0, w1 incw z1.s b.any .L4 add z0.s, z0.s, #1 lastb w0, p1, z0.s ret .p2align 2,,3 .L8: mov w0, 0 b .L3 .p2align 2,,3 .L13: lsl w1, w1, 1 .L3: add w0, w0, 1 tbz w1, #31, .L13 ret .p2align 2,,3 .L6: mov w0, 32 ret .p2align 2,,3 .L7: mov w0, 0 ret .cfi_endproc In essence, the vectoriser uses the niter information to determine exactly how many iterations of the loop it needs to run. It then uses SVE whilelo instructions to run this number of iterations. The original loop counter is also vectorised, despite only being used in the final iteration, and then the final value of this counter is used as the return value (which is the same as the number of iterations it computed in the first place). This vectorisation is obviously bad, and I think it exposes a latent bug in the vectoriser, rather than being an issue caused by this specific patch. gcc/ChangeLog: * tree-ssa-loop-niter.cc (number_of_iterations_cltz): New. (number_of_iterations_bitcount): Add call to the above. (number_of_iterations_exit_assumptions): Add EQ_EXPR case for c[lt]z idiom recognition. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cltz-max.c: New test. * gcc.dg/tree-ssa/clz-char.c: New test. * gcc.dg/tree-ssa/clz-int.c: New test. * gcc.dg/tree-ssa/clz-long-long.c: New test. * gcc.dg/tree-ssa/clz-long.c: New test. * gcc.dg/tree-ssa/ctz-char.c: New test. * gcc.dg/tree-ssa/ctz-int.c: New test. * gcc.dg/tree-ssa/ctz-long-long.c: New test. * gcc.dg/tree-ssa/ctz-long.c: New test.
1 parent 0419b9b commit 4798080

File tree

10 files changed

+517
-0
lines changed

10 files changed

+517
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2 -fno-tree-loop-optimize -fdump-tree-optimized" } */
3+
4+
#define PREC (__CHAR_BIT__)
5+
6+
int clz_count1 (unsigned char b) {
7+
int c = 0;
8+
9+
if (b == 0)
10+
return 0;
11+
12+
while (!(b & (1 << (PREC - 1)))) {
13+
b <<= 1;
14+
c++;
15+
}
16+
if (c <= PREC - 1)
17+
return 0;
18+
else
19+
return 34567;
20+
}
21+
22+
int clz_count2 (unsigned char b) {
23+
int c = 0;
24+
25+
if (b == 0)
26+
return 0;
27+
28+
while (!(b & (1 << PREC - 1))) {
29+
b <<= 1;
30+
c++;
31+
}
32+
if (c <= PREC - 2)
33+
return 0;
34+
else
35+
return 76543;
36+
}
37+
38+
int ctz_count1 (unsigned char b) {
39+
int c = 0;
40+
41+
if (b == 0)
42+
return 0;
43+
44+
while (!(b & 1)) {
45+
b >>= 1;
46+
c++;
47+
}
48+
if (c <= PREC - 1)
49+
return 0;
50+
else
51+
return 23456;
52+
}
53+
54+
int ctz_count2 (unsigned char b) {
55+
int c = 0;
56+
57+
if (b == 0)
58+
return 0;
59+
60+
while (!(b & 1)) {
61+
b >>= 1;
62+
c++;
63+
}
64+
if (c <= PREC - 2)
65+
return 0;
66+
else
67+
return 65432;
68+
}
69+
/* { dg-final { scan-tree-dump-times "34567" 0 "optimized" } } */
70+
/* { dg-final { scan-tree-dump-times "76543" 1 "optimized" } } */
71+
/* { dg-final { scan-tree-dump-times "23456" 0 "optimized" } } */
72+
/* { dg-final { scan-tree-dump-times "65432" 1 "optimized" } } */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target clzl } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned char b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & (1 << (PREC - 1)))) {
16+
b <<= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1 << (PREC - 1)) != 0)
28+
__builtin_abort ();
29+
if (foo(35) != PREC - 6)
30+
__builtin_abort ();
31+
return 0;
32+
}
33+
34+
/* { dg-final { scan-tree-dump-times "__builtin_clz|\\.CLZ" 1 "optimized" } } */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target clzl } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_INT__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned int b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & (1 << (PREC - 1)))) {
16+
b <<= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1 << (PREC - 1)) != 0)
28+
__builtin_abort ();
29+
if (foo(35) != PREC - 6)
30+
__builtin_abort ();
31+
return 0;
32+
}
33+
34+
/* { dg-final { scan-tree-dump-times "__builtin_clz|\\.CLZ" 1 "optimized" } } */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target clzll } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned long long b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & (1LL << (PREC - 1)))) {
16+
b <<= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1LL << (PREC - 1)) != 0)
28+
__builtin_abort ();
29+
if (foo(35) != PREC - 6)
30+
__builtin_abort ();
31+
return 0;
32+
}
33+
34+
/* { dg-final { scan-tree-dump-times "__builtin_clz|\\.CLZ" 1 "optimized" } } */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target clzl } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_LONG__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned long b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & (1L << (PREC - 1)))) {
16+
b <<= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1L << (PREC - 1)) != 0)
28+
__builtin_abort ();
29+
if (foo(35) != PREC - 6)
30+
__builtin_abort ();
31+
return 0;
32+
}
33+
34+
/* { dg-final { scan-tree-dump-times "__builtin_clz|\\.CLZ" 1 "optimized" } } */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target ctz } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned char b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & 1)) {
16+
b >>= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(128) != 7)
28+
__builtin_abort ();
29+
if (foo(96) != 5)
30+
__builtin_abort ();
31+
if (foo(35) != 0)
32+
__builtin_abort ();
33+
return 0;
34+
}
35+
36+
/* { dg-final { scan-tree-dump-times "__builtin_ctz|\\.CTZ" 1 "optimized" } } */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target ctz } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_INT__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned int b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & 1)) {
16+
b >>= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1 << (PREC - 1)) != PREC - 1)
28+
__builtin_abort ();
29+
if (foo(96) != 5)
30+
__builtin_abort ();
31+
if (foo(35) != 0)
32+
__builtin_abort ();
33+
return 0;
34+
}
35+
36+
/* { dg-final { scan-tree-dump-times "__builtin_ctz|\\.CTZ" 1 "optimized" } } */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target ctzll } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned long long b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & 1)) {
16+
b >>= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1LL << (PREC - 1)) != PREC - 1)
28+
__builtin_abort ();
29+
if (foo(96) != 5)
30+
__builtin_abort ();
31+
if (foo(35) != 0)
32+
__builtin_abort ();
33+
return 0;
34+
}
35+
36+
/* { dg-final { scan-tree-dump-times "__builtin_ctz|\\.CTZ" 1 "optimized" } } */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* { dg-do run } */
2+
/* { dg-require-effective-target ctzl } */
3+
/* { dg-options "-O2 -fno-tree-ch -fdump-tree-optimized" } */
4+
5+
#define PREC (__CHAR_BIT__ * __SIZEOF_LONG__)
6+
7+
int
8+
__attribute__ ((noinline, noclone))
9+
foo (unsigned long b) {
10+
int c = 0;
11+
12+
if (b == 0)
13+
return PREC;
14+
15+
while (!(b & 1)) {
16+
b >>= 1;
17+
c++;
18+
}
19+
20+
return c;
21+
}
22+
23+
int main()
24+
{
25+
if (foo(0) != PREC)
26+
__builtin_abort ();
27+
if (foo(1L << (PREC - 1)) != PREC - 1)
28+
__builtin_abort ();
29+
if (foo(96) != 5)
30+
__builtin_abort ();
31+
if (foo(35) != 0)
32+
__builtin_abort ();
33+
return 0;
34+
}
35+
36+
/* { dg-final { scan-tree-dump-times "__builtin_ctz|\\.CTZ" 1 "optimized" } } */

0 commit comments

Comments
 (0)