Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -3387,6 +3387,11 @@ // Alloca never returns null, malloc might. if (isa(V)) return true; + // A GEP inbounds of a ptr of address space 0 can never return null because: + // 1) the input argument cannot be null; 2) it cannot overflow. + if (const GetElementPtrInst *GEP = dyn_cast(V)) + return GEP->isInBounds() && GEP->getAddressSpace() == 0; + // A byval, inalloca, or nonnull argument is never null. if (const Argument *A = dyn_cast(V)) return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr(); Index: test/Transforms/FunctionAttrs/nonnull.ll =================================================================== --- test/Transforms/FunctionAttrs/nonnull.ll +++ test/Transforms/FunctionAttrs/nonnull.ll @@ -216,3 +216,14 @@ unreachable } +; CHECK: define nonnull i32* @gep1( +define i32* @gep1(i32* %p) { + %q = getelementptr inbounds i32, i32* %p, i32 1 + ret i32* %q +} + +; CHECK: define i32 addrspace(3)* @gep2( +define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) { + %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1 + ret i32 addrspace(3)* %q +}