llvm-project
llvm-project copied to clipboard
Missed optimization: failure to use div-by-zero UB to delete redundant select
Given this function:
define i64 @incorrect_safe_div(i64 %n, i64 %d) {
%1 = icmp eq i64 %d, 0
%2 = udiv i64 %n, %d
%3 = select i1 %1, i64 -1, i64 %2
ret i64 %3
}
LLVM does not seem to notice that this select is unconditional, because %2 should imply that %d != 0 in the entire block, and therefore %1 can be replaced with false. Alive2 confirms this optimization is valid: https://alive2.llvm.org/ce/z/7nLDRe.
This is probably just a matter of adding udiv support to isKnownNonNullFromDominatingCondition().
@luxufan Are you still working on this? I have a patch almost ready to fix this.
Candidate PR: https://github.com/llvm/llvm-project/pull/67282