[PATCH] D99642: For non-null pointer checks, do not descend through out-of-bounds GEPs

Momchil Velikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 8 05:31:31 PDT 2021


chill updated this revision to Diff 336071.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99642/new/

https://reviews.llvm.org/D99642

Files:
  llvm/lib/Analysis/LazyValueInfo.cpp
  llvm/test/Transforms/JumpThreading/nonnull-gep-out-of-bounds.ll


Index: llvm/test/Transforms/JumpThreading/nonnull-gep-out-of-bounds.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/JumpThreading/nonnull-gep-out-of-bounds.ll
@@ -0,0 +1,18 @@
+; RUN: opt -jump-threading -S %s -o - | FileCheck %s
+
+define i32 @f(i64* %a, i64 %i) {
+entry:
+  store i64 0, i64* %a, align 8
+  %p = getelementptr i64, i64* %a, i64 %i
+  %c = icmp eq i64* %p, null
+  ; `%a` is non-null at the end of the block, because we store through it.
+  ; However, `%p` is derived from `%a` via a GEP that is not `inbounds`, therefore we cannot judge `%p` is non-null as well
+  ; and must retain the `icmp` instruction.
+  ; CHECK: %c = icmp eq i64* %p, null
+  br i1 %c, label %if.else, label %if.then
+if.then:
+  ret i32 0
+
+if.else:
+  ret i32 1
+}
Index: llvm/lib/Analysis/LazyValueInfo.cpp
===================================================================
--- llvm/lib/Analysis/LazyValueInfo.cpp
+++ llvm/lib/Analysis/LazyValueInfo.cpp
@@ -658,7 +658,7 @@
                            Val->getType()->getPointerAddressSpace()))
     return false;
 
-  Val = getUnderlyingObject(Val);
+  Val = Val->stripInBoundsOffsets();
   return TheCache.isNonNullAtEndOfBlock(Val, BB, [](BasicBlock *BB) {
     NonNullPointerSet NonNullPointers;
     for (Instruction &I : *BB)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99642.336071.patch
Type: text/x-patch
Size: 1341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210408/f374c821/attachment.bin>


More information about the llvm-commits mailing list