[PATCH] D65143: [InstCombine] Fold '(-1 u/ %x) u< %y' to '@llvm.umul.with.overflow' + overflow bit extraction

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 23 07:00:46 PDT 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: nikic, spatel, efriedma, xbolva00, RKSimon.
lebedev.ri added a project: LLVM.
Herald added a subscriber: hiraditya.
lebedev.ri added a child revision: D65144: [InstCombine] Fold '((%x * %y) u/ %x) != %y' to '@llvm.umul.with.overflow' + overflow bit extraction.

`(-1 u/ %x) u< %y` is one of (3?) common ways to check that
some unsigned multiplication (will not) overflow.
Currently, we don't catch it. We could:

  ----------------------------------------
  Name: no overflow
    %o0 = udiv i4 -1, %x
    %r = icmp ult i4 %o0, %y
  =>
    %o0 = udiv i4 -1, %x
    %n0 = umul_overflow i4 %x, %y
    %r = extractvalue {i4, i1} %n0, 1
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Name: no overflow, swapped
    %o0 = udiv i4 -1, %x
    %r = icmp ugt i4 %y, %o0
  =>
    %o0 = udiv i4 -1, %x
    %n0 = umul_overflow i4 %x, %y
    %r = extractvalue {i4, i1} %n0, 1
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Name: overflow
    %o0 = udiv i4 -1, %x
    %r = icmp uge i4 %o0, %y
  =>
    %o0 = udiv i4 -1, %x
    %n0 = umul_overflow i4 %x, %y
    %n1 = extractvalue {i4, i1} %n0, 1
    %r = xor %n1, -1
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Name: overflow
    %o0 = udiv i4 -1, %x
    %r = icmp ule i4 %y, %o0
  =>
    %o0 = udiv i4 -1, %x
    %n0 = umul_overflow i4 %x, %y
    %n1 = extractvalue {i4, i1} %n0, 1
    %r = xor %n1, -1
  
  Done: 1
  Optimization is correct!

As it can be observed from tests, while simply forming the `@llvm.umul.with.overflow`
is easy, if we were looking for the inverted answer, then more work needs to be done
to cleanup the now-pointless control-flow that was guarding against division-by-zero.
This is being addressed in follow-up patches.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65143

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/unsigned-mul-lack-of-overflow-check-via-udiv-of-allones.ll
  llvm/test/Transforms/InstCombine/unsigned-mul-overflow-check-via-udiv-of-allones.ll
  llvm/test/Transforms/PhaseOrdering/unsigned-multiply-overflow-check.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65143.211286.patch
Type: text/x-patch
Size: 13183 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190723/012ab22e/attachment.bin>


More information about the llvm-commits mailing list