llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Missed optimization: failure to use div-by-zero UB to delete redundant select

Open mcy opened this issue 3 years ago • 3 comments

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.

mcy avatar Jul 30 '23 18:07 mcy

This is probably just a matter of adding udiv support to isKnownNonNullFromDominatingCondition().

nikic avatar Sep 22 '23 12:09 nikic

@luxufan Are you still working on this? I have a patch almost ready to fix this.

dc03 avatar Sep 24 '23 17:09 dc03

Candidate PR: https://github.com/llvm/llvm-project/pull/67282

dc03 avatar Sep 25 '23 04:09 dc03