If X or Y is known to be a non-zero power of 2 we can prove the known
bit.
I.e assume X is a variable and Y is a power of 2 constants.
- assume(X & Y == 0) -> Known(X).Zeros[Log2(Y)] = 1
- assume(X & Y == Y) -> Known(X).Ones[Log2(Y)] = 1
Differential D140851
[InstCombine]: Add cases for assume (X & Y != {0|Y}) goldstein.w.n on Jan 2 2023, 11:22 AM. Authored by
Details If X or Y is known to be a non-zero power of 2 we can prove the known I.e assume X is a variable and Y is a power of 2 constants.
Diff Detail
Event Timeline
Comment Actions The implementation looks basically fine. I think it would be easiest to land this if it's detached from the patch stack, with some dedicated tests -- which would probably be pretty much just these? declare void @llvm.assume(i1) define i32 @pow2(i32 %x) { %and = and i32 %x, 4 %cmp = icmp ne i32 %and, 0 call void @llvm.assume(i1 %cmp) %and2 = and i32 %x, 4 ret i32 %and2 } define i32 @not_pow2(i32 %x) { %and = and i32 %x, 3 %cmp = icmp ne i32 %and, 0 call void @llvm.assume(i1 %cmp) %and2 = and i32 %x, 3 ret i32 %and2 }
Comment Actions Done, test patch is: https://reviews.llvm.org/D141412
Added those tests along with some other variations. Also added some tests for when the power of 2 is non-constant but My plan is:
That sound reasonable? Also, if you have a moment about an unrelated thing. I'm working on a patch
Do you have any advise about which of these (if any) make the most sense. And if none Comment Actions LGTM Extremely unlikely. Both because of compile-time impact, and because this would add implementation complexity to all other transforms, that now need to keep MSSA up to date.
This is possible -- probably easiest if it covers your motivating cases.
Handling this in EarlyCSE/GVN does seem most principled. I'm not sure it's necessary to actually perform the conversion from str to mem+strlen in advance -- shouldn't we be able to look up whether we have an available strlen call, and perform the transform from str to mem if we do? Of course, it would be a bit of an awkward special case... Comment Actions Thank you for the advise :) The canonicalization wouldn't be necessary, it would be just be a simple way to avoid having to check the strlen case for each function (although Think I'll give EarlyCSE/GVN a shot (I was leaning towards EarlyCSE), if it doesn't work out i'll just do in the AAResults method. |
nit: Drop newline