Why doesn't this `and` get eliminated

define dso_local i32 @f(i32 %0) {

%2 = and i32 %0, 7

%3 = icmp eq i32 %2, 7

%4 = zext i1 %3 to i32

ret i32 %4

}

I thought instcombine would remove it. It doesn’t and nothing else does either. LLVM Version is 10.0.0.

/Riyaz

I don’t think its legal to remove it. That code is equivalent to asking (%0 mod 8) == 7. Which is true for 7 and 15 and 23, etc. You can’t just remove the mod part of that or it would just return true for %0 == 7.

alive2 agrees with Craig:

Oops! My bad! The code I intended to write was:

define dso_local i32 @f(i3 %0) {

%2 = and i3 %0, 7

%3 = icmp eq i3 %2, 7

%4 = zext i1 %3 to i32

ret i32 %4

}

The and does get removed in this case as expected. Thanks for answering the dumb question. :blush:

/Riyaz