Minor optimization of variable bit testing

gcc/
	* match.pd: New pattern to simplify (1 << n) & M ==/!= 0 for M
	being a power of 2.

gcc/testsuite
	* gcc.dg/tree-ssa/bittest.c: New test
This commit is contained in:
Jeff Law 2021-11-08 23:23:34 -05:00
parent 1bd89833d7
commit 2abd924f91
2 changed files with 38 additions and 0 deletions

View File

@ -3441,6 +3441,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
(bit_and @4 { newmaskt; })))))))))))))
/* ((1 << n) & M) != 0 -> n == log2 (M) */
(for cmp (ne eq)
icmp (eq ne)
(simplify
(cmp
(bit_and
(nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
(icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
wi::exact_log2 (wi::to_wide (@1))); }))))
/* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
(X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
(for shift (lshift rshift)

View File

@ -0,0 +1,27 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
void bar (void);
void
foo(unsigned int abc123)
{
unsigned int xyzpdq = (1 << abc123);
if ((xyzpdq & 0x800) != 0)
bar();
}
void
baz(unsigned int abc123)
{
unsigned int xyzpdq = (1 << abc123);
if ((xyzpdq & 0x800) == 0)
bar();
}
/* What we want to verify is that the bit test against xyzpdq is
replaced with a test against abc123 which avoids the shifting
and bit ops. */
/* { dg-final { scan-tree-dump-not "xyzpdq" "optimized"} } */
/* { dg-final { scan-tree-dump-times "if .abc123" 2 "optimized"} } */