Closed
Description
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
}
Activity
device-libs: Optimize odd/even integer checks in pow*
elhewaty commentedon Sep 4, 2023
Can I fix this? But I will need some help, as I am not familiar with LLVM system yet
elhewaty commentedon Sep 4, 2023
%i4 = icmp eq i32 %i3, 1
what if the
1
were0
?dc03 commentedon Sep 4, 2023
That would be equivalent to
not(or(x, y))
: https://alive2.llvm.org/ce/z/8z7Q4Gelhewaty commentedon Sep 4, 2023
so the
xor
case holds foreq
,1
, andzext
onlydc03 commentedon Sep 4, 2023
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.
elhewaty commentedon Sep 4, 2023
So we need to create a new function
foldICmpAddExtI1
and handle all our cases? or do we have something like this already?dc03 commentedon Sep 4, 2023
I think
foldICmpAddConstant
may be what you're looking for: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L2898elhewaty commentedon Sep 6, 2023
@arsenm Here is a candidate patch : https://reviews.llvm.org/D159464
elhewaty commentedon Sep 14, 2023
Can you please review the patch again.
elhewaty commentedon Sep 20, 2023
@arsenm
ping
elhewaty commentedon Sep 30, 2023
@arsenm @nikic @goldsteinn Here's a candidate pull: #67895
3 remaining items