Skip to content
Snippets Groups Projects
Commit 6fc14f19 authored by Roger Sayle's avatar Roger Sayle
Browse files

middle-end: Optimize ((X >> C1) & C2) != C3 for more cases.

Following my middle-end patch for PR tree-optimization/94026, I'd promised
Jeff Law that I'd clean up the dead-code in fold-const.cc now that these
optimizations are handled in match.pd.  Alas, I discovered things aren't
quite that simple, as the transformations I'd added avoided cases where
C2 overlapped with the new bits introduced by the shift, but the original
code handled any value of C2 provided that it had a single-bit set (under
the condition that C3 was always zero).

This patch upgrades the transformations supported by match.pd to cover
any values of C2 and C3, provided that C1 is a valid bit shift constant,
for all three shift types (logical right, arithmetic right and left).
This then makes the code in fold-const.cc fully redundant, and adds
support for some new (corner) cases not previously handled.  If the
constant C1 is valid for the type's precision, the shift is now always
eliminated (with C2 and C3 possibly updated to test the sign bit).

Interestingly, the fold-const.cc code that I'm now deleting was originally
added by me back in 2006 to resolve PR middle-end/21137.  I've confirmed
that those testcase(s) remain resolved with this patch (and I'll close
21137 in Bugzilla).  This patch also implements most (but not all) of the
examples mentioned in PR tree-optimization/98954, for which I have some
follow-up patches.

2022-08-09  Roger Sayle  <roger@nextmovesoftware.com>
	    Richard Biener  <rguenther@suse.de>

gcc/ChangeLog
	PR middle-end/21137
	PR tree-optimization/98954
	* fold-const.cc (fold_binary_loc): Remove optimizations to
	optimize ((X >> C1) & C2) ==/!= 0.
	* match.pd (cmp (bit_and (lshift @0 @1) @2) @3): Remove wi::ctz
	check, and handle all values of INTEGER_CSTs @2 and @3.
	(cmp (bit_and (rshift @0 @1) @2) @3): Likewise, remove wi::clz
	checks, and handle all values of INTEGER_CSTs @2 and @3.

gcc/testsuite/ChangeLog
	PR middle-end/21137
	PR tree-optimization/98954
	* gcc.dg/fold-eqandshift-4.c: New test case.
parent 9385cd9c
No related branches found
No related tags found
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment