[PATCH] D65144: [InstCombine] Fold '((%x * %y) u/ %x) != %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:47 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 parent revision: D65143: [InstCombine] Fold '(-1 u/ %x) u< %y' to '@llvm.umul.with.overflow' + overflow bit extraction.

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

  $ /repositories/alive2/build-Clang-unknown/alive -root-only ~/llvm-patch1.ll 
  Processing /home/lebedevri/llvm-patch1.ll..
  
  ----------------------------------------
  Name: no overflow
    %o0 = mul i4 %y, %x
    %o1 = udiv i4 %o0, %x
    %r = icmp ne i4 %o1, %y
    ret i1 %r
  =>
    %n0 = umul_overflow i4 %x, %y
    %o0 = extractvalue {i4, i1} %n0, 0
    %o1 = udiv %o0, %x
    %r = extractvalue {i4, i1} %n0, 1
    ret %r
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Name: no overflow
    %o0 = mul i4 %y, %x
    %o1 = udiv i4 %o0, %x
    %r = icmp eq i4 %o1, %y
    ret i1 %r
  =>
    %n0 = umul_overflow i4 %x, %y
    %o0 = extractvalue {i4, i1} %n0, 0
    %o1 = udiv %o0, %x
    %n1 = extractvalue {i4, i1} %n0, 1
    %r = xor %n1, -1
    ret i1 %r
  
  Done: 1
  Optimization is correct!


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65144

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65144.211287.patch
Type: text/x-patch
Size: 12193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190723/8bade128/attachment.bin>


More information about the llvm-commits mailing list