Open
Description
For the IR
define i1 @src(ptr %x) {
%1 = load i8, ptr %x, align 1
%2 = icmp ult i8 %1, 2
br i1 %2, label %bb1, label %bb2
bb2:
%3 = tail call i1 @dynamic()
br label %bb3
bb1:
%4 = icmp eq i8 %1, 1
br label %bb3
bb3:
%5 = phi i1 [ %4, %bb1 ], [ %3, %bb2 ]
ret i1 %5
}
declare i1 @dynamic()
which generates the following instructions in x86 (https://godbolt.org/z/hY5EjobTv):
src: # @src
movzx eax, byte ptr [rdi]
cmp al, 2
jae dynamic@PLT # TAILCALL
cmp al, 1
sete al
ret
The conversion of eax
to bool
is redundant after jae
, we can directly ret
(eax
) as eax
is already in valid bool range [0, 1].
This issue is extracted from rust-lang/rust#121673.
I tried to implement this optimization in IR by folding icmp eq %x, 1
to trunc %x to i1
in #83829, but it doesn't work well, since InstCombine would canonicalize trunc
to (X & 1) != 0
, which results in one more and
instruction in backend.
So, I am going to implement this optimization in backend. Please let me know if there is a better way to address this issue.
If possible, please assign this issue to me, thanks.