diff options
author | Philip Reames <listmail@philipreames.com> | 2014-10-20 22:40:55 +0000 |
---|---|---|
committer | Philip Reames <listmail@philipreames.com> | 2014-10-20 22:40:55 +0000 |
commit | 90f3f15da5c96356f9952109446f945090180b27 (patch) | |
tree | 6c42c7a7049548318b79ca6c0b1fc37a224029f8 | |
parent | 0d1978b8130c14b97d2e5ca66e5bfb975d6e46b4 (diff) | |
download | llvm-90f3f15da5c96356f9952109446f945090180b27.tar.gz |
Introduce a 'nonnull' metadata on Load instructions.
The newly introduced 'nonnull' metadata is analogous to existing 'nonnull' attributes, but applies to load instructions rather than call arguments or returns. Long term, it would be nice to combine these into a single construct. The value of the load is allowed to vary between successive loads, but null is not a valid value to be loaded by any load marked nonnull.
Reviewed by: Hal Finkel
Differential Revision: http://reviews.llvm.org/D5220
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220240 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | docs/LangRef.rst | 10 | ||||
-rw-r--r-- | lib/Analysis/ValueTracking.cpp | 4 | ||||
-rw-r--r-- | test/Transforms/InstSimplify/compare.ll | 23 |
3 files changed, 36 insertions, 1 deletions
diff --git a/docs/LangRef.rst b/docs/LangRef.rst index 96883a8090f4..9e44705530a0 100644 --- a/docs/LangRef.rst +++ b/docs/LangRef.rst @@ -5157,7 +5157,7 @@ Syntax: :: - <result> = load [volatile] <ty>* <pointer>[, align <alignment>][, !nontemporal !<index>][, !invariant.load !<index>] + <result> = load [volatile] <ty>* <pointer>[, align <alignment>][, !nontemporal !<index>][, !invariant.load !<index>][, !nonnull !<index>] <result> = load atomic [volatile] <ty>* <pointer> [singlethread] <ordering>, align <alignment> !<index> = !{ i32 1 } @@ -5213,6 +5213,14 @@ address points to memory which does not change value during program execution. The optimizer may then move this load around, for example, by hoisting it out of loops using loop invariant code motion. +The optional ``!nonnull`` metadata must reference a single +metadata name ``<index>`` corresponding to a metadata node with no +entries. The existence of the ``!nonnull`` metadata on the +instruction tells the optimizer that the value loaded is known to +never be null. This is analogous to the ''nonnull'' attribute +on parameters and return values. This metadata can only be applied +to loads of a pointer type. + Semantics: """""""""" diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 22f0486e8a2c..462d5b7cf0bb 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -2622,6 +2622,10 @@ bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) { if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) return !GV->hasExternalWeakLinkage(); + // A Load tagged w/nonnull metadata is never null. + if (const LoadInst *LI = dyn_cast<LoadInst>(V)) + return LI->getMetadata("nonnull"); + if (ImmutableCallSite CS = V) if (CS.isReturnNonNull()) return true; diff --git a/test/Transforms/InstSimplify/compare.ll b/test/Transforms/InstSimplify/compare.ll index b30648301ae6..38fd747172f0 100644 --- a/test/Transforms/InstSimplify/compare.ll +++ b/test/Transforms/InstSimplify/compare.ll @@ -949,6 +949,29 @@ define i1 @returns_nonnull_as_deref() { ; CHECK: ret } +define i1 @nonnull_load(i32** %addr) { + %ptr = load i32** %addr, !nonnull !{} + %cmp = icmp eq i32* %ptr, null + ret i1 %cmp +; CHECK-LABEL: @nonnull_load +; CHECK: ret i1 false +} + +define i1 @nonnull_load_as_outer(i32* addrspace(1)* %addr) { + %ptr = load i32* addrspace(1)* %addr, !nonnull !{} + %cmp = icmp eq i32* %ptr, null + ret i1 %cmp +; CHECK-LABEL: @nonnull_load_as_outer +; CHECK: ret i1 false +} +define i1 @nonnull_load_as_inner(i32 addrspace(1)** %addr) { + %ptr = load i32 addrspace(1)** %addr, !nonnull !{} + %cmp = icmp eq i32 addrspace(1)* %ptr, null + ret i1 %cmp +; CHECK-LABEL: @nonnull_load_as_inner +; CHECK: ret i1 false +} + ; If a bit is known to be zero for A and known to be one for B, ; then A and B cannot be equal. define i1 @icmp_eq_const(i32 %a) nounwind { |