[PATCH] D67332: [InstSimplify] simplifyUnsignedRangeCheck(): if we know that X != 0, handle more cases

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 8 11:10:23 PDT 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, nikic, vsk.
lebedev.ri added a project: LLVM.
Herald added a subscriber: hiraditya.

This is motivated by D67122 <https://reviews.llvm.org/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/LK5-iH
which after optimizations reduces down to roughly

  define i1 @t0(i8* nonnull %base, i64 %offset) {
    %base_int = ptrtoint i8* %base to i64
    %adjusted = add i64 %base_int, %offset
    %non_null_after_adjustment = icmp ne i64 %adjusted, 0
    %no_overflow_during_adjustment = icmp uge i64 %adjusted, %base_int
    %res = and i1 %non_null_after_adjustment, %no_overflow_during_adjustment
    ret i1 %res
  }

Without D67122 <https://reviews.llvm.org/D67122> there was no `%non_null_after_adjustment`,
and in this particular case we can get rid of the overhead:

Here we add some offset to a non-null pointer,
and check that the result does not overflow and is not a null pointer.
But since the base pointer is already non-null, and we check for overflow,
that overflow check will already catch the null pointer,
so the separate null check is redundant and can be dropped.

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

There are more patterns of "unsigned-add-with-overflow", they are not handled here,
but this is the main pattern, that we currently consider canonical,
so it makes sense to handle it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67332

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/redundant-null-check-in-uadd_with_overflow-of-nonnull-ptr.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67332.219273.patch
Type: text/x-patch
Size: 14079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190908/a14f51d6/attachment.bin>


More information about the llvm-commits mailing list