For the following C code
void foo() {
int forty_two = 42;
if (forty_two == 42) {
}
}
clang -S -emit-llvm foo.c
emits this IR code:
define dso_local void @foo() #0 {
%1 = alloca i32, align 4
store i32 42, i32* %1, align 4
%2 = load i32, i32* %1, align 4
%3 = icmp eq i32 %2, 42
br i1 %3, label %4, label %5
4: ; preds = %0
br label %5
5: ; preds = %4, %0
ret void
}
And for the IR, LLVM (llc foo.ll
) generates the x64 code below. It's abridged for readability.
foo: # @foo
# %bb.0:
pushq %rbp
movq %rsp, %rbp
movl $42, -4(%rbp)
cmpl $42, -4(%rbp)
jne .LBB0_2
# %bb.1:
jmp .LBB0_2
.LBB0_2:
popq %rbp
retq
In contrast to the native code emitted by LLVM, translating the IR code in a straightforward way would contain a number of redundant instructions. Something along these lines:
foo:
# %bb.0:
pushq %rbp
movq %rsp, %rbp
movl $42, -4(%rbp)
cmpl $42, -4(%rbp)
# create the i1 boolean in a register.
# (This instruction is redundant and LLVM doesn't emit is)
sete %al
# See whether the comparison's result was `true` or `false`.
# (This instruction is redundant and LLVM doesn't emit it)
cmpb $1, %al
jne .LBB0_2
# %bb.1:
jmp .LBB0_2
.LBB0_2:
popq %rbp
retq
My question is: Where is the portion of LLVM code that makes sure these redundant instructions are not emitted? And how does it work?
I read an excellent post Life of an instruction in LLVM by @Eli Bendersky and looked at the code in SelectionDAGBuilder::visitICmp
and SelectionDAGBuilder::visitBr
. But didn't manage to figure out the answer on my own.