[PATCH] D144671: [InstCombine] prevent miscompiles from select-of-div/rem transform

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 12:55:13 PST 2023


spatel created this revision.
spatel added reviewers: regehr, nlopes, aqjune, nikic.
Herald added subscribers: StephenFan, hiraditya, mcrosier.
Herald added a project: All.
spatel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This avoids the danger shown in issue #60906 <https://github.com/llvm/llvm-project/issues/60906>. There were no regression tests for these patterns, so these potential failures have been around for a long time.

We could carve out some exceptions to this bailout. For example, the transform is actually ok with udiv/urem and a common divisor because poison in the dividend would not cause immediate UB - the divisor has to be zero, and that would be UB in the original code too. That doesn't work with **signed** div/rem because we can choose the poison dividend to be MinSignedValue, and that overflows when divided by -1 causing UB that doesn't exist in the original code.

Here are a couple of examples that can be corrected with 'noundef':
https://alive2.llvm.org/ce/z/sXHTTC


https://reviews.llvm.org/D144671

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/select-divrem.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144671.499956.patch
Type: text/x-patch
Size: 5897 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230223/c01b4b72/attachment-0001.bin>


More information about the llvm-commits mailing list