llvm-project/llvm/lib/Transforms/InstCombine
Roman Lebedev 7a67ed5795 [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

llvm-svn: 372341
2019-09-19 17:25:19 +00:00
..
CMakeLists.txt [InstCombine] Optimize `atomicrmw <op>, 0` into `load atomic` when possible 2019-02-07 21:27:23 +00:00
InstCombineAddSub.cpp [InstCombine] sub(xor(x, y), or(x, y)) -> neg(and(x, y)) 2019-09-04 18:03:21 +00:00
InstCombineAndOrXor.cpp [InstCombine] Simplify @llvm.usub.with.overflow+non-zero check (PR43251) 2019-09-19 17:25:19 +00:00
InstCombineAtomicRMW.cpp [InstCombine] Extend saturating idempotent atomicrmw transform to FP 2019-03-01 19:50:36 +00:00
InstCombineCalls.cpp [InstCombine] Annotate strdup with deref_or_null 2019-09-17 10:12:48 +00:00
InstCombineCasts.cpp [InstCombine] rename variable for readability; NFC 2019-09-11 22:31:34 +00:00
InstCombineCompares.cpp [InstCombine] remove unneeded one-use checks for icmp fold 2019-09-16 16:15:25 +00:00
InstCombineInternal.h [InstCombine] fold sign-bit compares of srem 2019-09-11 12:04:26 +00:00
InstCombineLoadStoreAlloca.cpp [Transforms] move copying of load metadata to helper function; NFC 2019-07-24 22:11:11 +00:00
InstCombineMulDivRem.cpp [InstCombine] x /c fabs(x) -> copysign(1.0, x) 2019-08-12 13:43:35 +00:00
InstCombinePHI.cpp [InstCombine] SliceUpIllegalIntegerPHI - bail on out of range shifts 2019-06-24 13:13:36 +00:00
InstCombineSelect.cpp InstCombiner::visitSelectInst - rename Pred to MinMaxPred to stop shadow variable warning. NFCI. 2019-08-28 14:05:38 +00:00
InstCombineShifts.cpp [InstCombine] dropRedundantMaskingOfLeftShiftInput(): some cleanup before upcoming patch 2019-09-18 18:38:40 +00:00
InstCombineSimplifyDemanded.cpp [InstCombine][AMDGPU] Simplify tbuffer loads 2019-08-30 14:20:04 +00:00
InstCombineTables.td InstCombine/AMDGPU: Add dimension-aware image intrinsics to SimplifyDemanded 2018-06-21 13:37:31 +00:00
InstCombineVectorOps.cpp [InstCombine] fold extract+insert into identity shuffle 2019-09-08 19:03:01 +00:00
InstructionCombining.cpp [Debuginfo][Instcombiner] Do not clone dbg.declare. 2019-09-11 06:07:16 +00:00
LLVMBuild.txt Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00