[InstCombine] foldICmpBinOp(): consider inverted check in 'unsigned sub overflow...
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 5 Sep 2019 17:41:02 +0000 (17:41 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 5 Sep 2019 17:41:02 +0000 (17:41 +0000)
commit8360c42e25108450184fbfb93870fecd0d21e6f4
tree60e599ca122ff997b3cc69600caf9a8efed36b3b
parentecb7ea1ae7c6658771f6de4a957f0ddcc1cf4a8d
[InstCombine] foldICmpBinOp(): consider inverted check in 'unsigned sub overflow' check

A follow-up for r329011.
This may be changed to produce @llvm.sub.with.overflow in a later patch,
but for now just make things more consistent overall.

A few observations stem from this:
* There does not seem to be a similar one-instruction fold for uadd-overflow
* I'm not sure we'll want to canonicalize `B u> A` as `usub.with.overflow`,
  so since the `icmp` here no longer refers to `sub`,
  reconstructing `usub.with.overflow` will be problematic,
  and will likely require standalone pass (similar to DivRemPairs).

https://rise4fun.com/Alive/Zqs

Name: (A - B) u> A --> B u> A
  %t0 = sub i8 %A, %B
  %r = icmp ugt i8 %t0, %A
=>
  %r = icmp ugt i8 %B, %A

Name: (A - B) u<= A --> B u<= A
  %t0 = sub i8 %A, %B
  %r = icmp ule i8 %t0, %A
=>
  %r = icmp ule i8 %B, %A

Name: C u< (C - D) --> C u< D
  %t0 = sub i8 %C, %D
  %r = icmp ult i8 %C, %t0
=>
  %r = icmp ult i8 %C, %D

Name: C u>= (C - D) --> C u>= D
  %t0 = sub i8 %C, %D
  %r = icmp uge i8 %C, %t0
=>
  %r = icmp uge i8 %C, %D

llvm-svn: 371101
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/unsigned-sub-lack-of-overflow-check.ll
llvm/test/Transforms/InstCombine/unsigned-sub-overflow-check.ll