Comparison of add of extended booleans not folded to xor
I was looking at some code that tried to compute a sort of enum scheme by adding booleans, and comparing to constant values which failed to form nice boolean code. This is really just an xor.
https://alive2.llvm.org/ce/z/RSnV-d
define i1 @src(i1 %arg, i1 %arg1) {
bb:
%i = zext i1 %arg to i32
%i2 = zext i1 %arg1 to i32
%i3 = add nuw nsw i32 %i2, %i
%i4 = icmp eq i32 %i3, 1
ret i1 %i4
}
define i1 @tgt(i1 %arg, i1 %arg1) {
bb:
%xor = xor i1 %arg, %arg1
ret i1 %xor
}
Can I fix this? But I will need some help, as I am not familiar with LLVM system yet
%i4 = icmp eq i32 %i3, 1
what if the 1 were 0?
%i4 = icmp eq i32 %i3, 1what if the1were0?
That would be equivalent to not(or(x, y)): https://alive2.llvm.org/ce/z/8z7Q4G
so the xor case holds for eq, 1, and zext only
so the
xorcase holds foreqand1only
Yes, if you sketch out the truth table for this function you will see it is equivalent to the table for an xor, and with 0 it is equivalent to the table for a nor.
So we need to create a new function foldICmpAddExtI1 and handle all our cases? or do we have something like this already?
I think foldICmpAddConstant may be what you're looking for: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L2898
@arsenm Here is a candidate patch : https://reviews.llvm.org/D159464
Can you please review the patch again.
@arsenm ping
@arsenm @nikic @goldsteinn Here's a candidate pull: https://github.com/llvm/llvm-project/pull/67895