summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2014-05-20 05:13:21 +0000
committerNick Lewycky <nicholas@mxc.ca>2014-05-20 05:13:21 +0000
commit4bf804fe0d352ea5a69beb0962d99c333beabd32 (patch)
treeed07f6733eaf17ab0b165b3d9da063ad389539b7
parent956583e98e57bea245137a7833a4b329edd897cf (diff)
downloadllvm-4bf804fe0d352ea5a69beb0962d99c333beabd32.tar.gz
llvm-4bf804fe0d352ea5a69beb0962d99c333beabd32.tar.bz2
llvm-4bf804fe0d352ea5a69beb0962d99c333beabd32.tar.xz
Teach isKnownNonNull that a nonnull return is not null. Add a test for this case as well as the case of a nonnull attribute (already handled but not tested).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209193 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Analysis/ValueTracking.cpp5
-rw-r--r--test/Transforms/InstSimplify/compare.ll17
2 files changed, 22 insertions, 0 deletions
diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp
index 280f15b509..4f48753578 100644
--- a/lib/Analysis/ValueTracking.cpp
+++ b/lib/Analysis/ValueTracking.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/MemoryBuiltins.h"
+#include "llvm/IR/CallSite.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
@@ -2073,6 +2074,10 @@ bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
return !GV->hasExternalWeakLinkage();
+ if (ImmutableCallSite CS = V)
+ if (CS.paramHasAttr(0, Attribute::NonNull))
+ return true;
+
// operator new never returns null.
if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true))
return true;
diff --git a/test/Transforms/InstSimplify/compare.ll b/test/Transforms/InstSimplify/compare.ll
index 1a62e27dd8..105e244ed8 100644
--- a/test/Transforms/InstSimplify/compare.ll
+++ b/test/Transforms/InstSimplify/compare.ll
@@ -866,3 +866,20 @@ define i1 @exact_ashr_sgt_false(i32 %a) {
; CHECK-LABEL: @exact_ashr_sgt_false
; CHECK-NEXT: ret i1 false
}
+
+define i1 @nonnull_arg(i32* nonnull %i) {
+ %cmp = icmp eq i32* %i, null
+ ret i1 %cmp
+; CHECK-LABEL: @nonnull_arg
+; CHECK: ret i1 false
+}
+
+declare nonnull i32* @returns_nonnull_helper()
+define i1 @returns_nonnull() {
+ %call = call nonnull i32* @returns_nonnull_helper()
+ %cmp = icmp eq i32* %call, null
+ ret i1 %cmp
+; CHECK-LABEL: @returns_nonnull
+; CHECK: ret i1 false
+}
+