[PATCH] D76483: [DivRemPairs] Freeze operands if they can be undef values

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 20 02:40:42 PDT 2020


aqjune created this revision.
Herald added subscribers: llvm-commits, hiraditya, nemanjai.
Herald added a project: LLVM.
aqjune edited the summary of this revision.
aqjune added reviewers: spatel, lebedev.ri, george.burgess.iv.
aqjune added subscribers: nlopes, regehr.
Herald added a subscriber: wuzish.

DivRemPairs is unsound with respect to undef values.

  // bb1:
  //   %rem = srem %x, %y
  // bb2:
  //   %div = sdiv %x, %y
  // -->
  // bb1:
  //   %div = sdiv %x, %y
  //   %mul = mul %div, %y
  //   %rem = sub %x, %mul

If X can be undef, X should be freezed first.
For example, let's assume that Y = 1 & X = undef:

    %div = sdiv undef, 1 // %div = undef
    %rem = srem undef, 1 // %rem = 0
  =>
    %div = sdiv undef, 1 // %div = undef
    %mul = mul %div, 1   // %mul = undef
    %rem = sub %x, %mul  // %rem = undef - undef = undef

http://volta.cs.utah.edu:8080/z/m7Xrx5

Same for Y. If X = 1 and Y = (undef | 1), %rem in src is either 1 or 0,
but %rem in tgt can be one of many integer values.

This resolves https://bugs.llvm.org/show_bug.cgi?id=42619 .

This miscompilation disappears if undef value is removed, but it may take a while.
DivRemPair happens pretty late during the optimization pipeline, so this optimization seemed as a good candidate to fix without major regression using freeze than other broken optimizations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76483

Files:
  llvm/lib/Transforms/Scalar/DivRemPairs.cpp
  llvm/test/Transforms/DivRemPairs/PowerPC/div-expanded-rem-pair.ll
  llvm/test/Transforms/DivRemPairs/PowerPC/div-rem-pairs.ll
  llvm/test/Transforms/DivRemPairs/X86/div-rem-pairs.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76483.251573.patch
Type: text/x-patch
Size: 12251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200320/802aa5fc/attachment.bin>


More information about the llvm-commits mailing list