llvm-project
llvm-project copied to clipboard
Missed optimization for one-hot encoding
Alive2: https://alive2.llvm.org/ce/z/EeCsH6
define i32 @src(i32 noundef %x) {
%cmp3 = icmp eq i32 %x, 3
%bit3 = select i1 %cmp3, i32 8, i32 0
%cmp2 = icmp eq i32 %x, 2
%bit2 = select i1 %cmp2, i32 4, i32 0
%or23 = or disjoint i32 %bit3, %bit2
%cmp1 = icmp eq i32 %x, 1
%bit1 = select i1 %cmp1, i32 2, i32 0
%or123 = or disjoint i32 %or23, %bit1
%cmp0 = icmp eq i32 %x, 0
%bit0 = zext i1 %cmp0 to i32
%or0123 = or disjoint i32 %or123, %bit0
ret i32 %or0123
}
define i32 @tgt(i32 %x) {
%cmp = icmp ult i32 %x, 4
%shl = shl i32 1, %x
%val = select i1 %cmp, i32 %shl, i32 0
ret i32 %val
}
I found this pattern in some verilator-generated code.
I want to work. Can you assign it to me?
You can match this pattern in InstCombinerImpl::visitOr.
However, as this pattern is extracted from verilator-generated code (https://github.com/Maxpicca-Li/CDIM), I don't know how to generalize it. It is recommended to write some Verilog variants and check the verilator's output.