BDCE has two phases:
- It asks SimplifyDemandedBits if all the bits of an instruction are dead, and if so, replaces all its uses with the constant zero.
- Then, it asks SimplifyDemandedBits again if the instruction is really dead (no side effects etc..) and if so, eliminates it.
Now, in 1) if all the bits of an instruction are dead, we may end up replacing a dbg use:
%call = tail call i32 (...) @g() #4, !dbg !15 tail call void @llvm.dbg.value(metadata i32 %call, i64 0, metadata !8, metadata !16), !dbg !17
->
%call = tail call i32 (...) @g() #4, !dbg !15 tail call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !8, metadata !16), !dbg !17
but not eliminating the call because it may have arbitrary side effects.
In other words, we lose some debug informations.
There are various possible solutions to the problem. E.g.:
- If the instruction has side effects and no non-dbg uses, BDCE should do nothing with it.
- We could introduce a variant of RAUW, called RAUWExceptDbg that doesn't strip dbg uses.
I tried to implement 2), but things get very ugly very quickly. Therefore, this patch implements 1), after discussing the solution with David/Hal.
"I.use_empty()" is a bit more straightforward than "!I.hasNUsesOrMore(1)".