[InstCombine] Simplify @llvm.usub.with.overflow+non-zero check (PR43251)
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 19 Sep 2019 17:25:19 +0000 (17:25 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 19 Sep 2019 17:25:19 +0000 (17:25 +0000)
commit1bee20881ff3823819a179667fe1d3eae573bba2
treece903f23f0411b2ea3b3fd222ddc35e7aa448ecc
parent0df3165d2b8735012392e6730319f335459f0776
[InstCombine] Simplify @llvm.usub.with.overflow+non-zero check (PR43251)

Summary:
This is again motivated by D67122 sanitizer check enhancement.
That patch seemingly worsens `-fsanitize=pointer-overflow`
overhead from 25% to 50%, which strongly implies missing folds.

In this particular case, given
```
char* test(char& base, unsigned long offset) {
  return &base - offset;
}
```
it will end up producing something like
https://godbolt.org/z/luGEju
which after optimizations reduces down to roughly
```
declare void @use64(i64)
define i1 @test(i8* dereferenceable(1) %base, i64 %offset) {
  %base_int = ptrtoint i8* %base to i64
  %adjusted = sub i64 %base_int, %offset
  call void @use64(i64 %adjusted)
  %not_null = icmp ne i64 %adjusted, 0
  %no_underflow = icmp ule i64 %adjusted, %base_int
  %no_underflow_and_not_null = and i1 %not_null, %no_underflow
  ret i1 %no_underflow_and_not_null
}
```
Without D67122 there was no `%not_null`,
and in this particular case we can "get rid of it", by merging two checks:
Here we are checking: `Base u>= Offset && (Base u- Offset) != 0`, but that is simply `Base u> Offset`

Alive proofs:
https://rise4fun.com/Alive/QOs

The `@llvm.usub.with.overflow` pattern itself is not handled here
because this is the main pattern, that we currently consider canonical.

https://bugs.llvm.org/show_bug.cgi?id=43251

Reviewers: spatel, nikic, xbolva00, majnemer

Reviewed By: xbolva00, majnemer

Subscribers: vsk, majnemer, xbolva00, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D67356

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372341 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll