diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -2454,6 +2454,12 @@ unsigned Depth, const SimplifyQuery &Q) { unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL); switch (I->getOpcode()) { + case Instruction::Alloca: + // Alloca never returns null, malloc might. + if (PointerType *PtrTy = dyn_cast(I->getType()); + PtrTy && PtrTy->getAddressSpace() == 0) + return true; + break; case Instruction::GetElementPtr: if (I->getType()->isPointerTy()) return isGEPKnownNonNull(cast(I), Depth, Q); @@ -2688,6 +2694,13 @@ // handled in isKnownNonZero. return false; case Instruction::Call: + if (const auto *Call = dyn_cast(I)) { + if (Call->isReturnNonNull()) + return true; + if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) + return isKnownNonZero(RP, Depth, Q); + } + if (auto *II = dyn_cast(I)) { switch (II->getIntrinsicID()) { case Intrinsic::sshl_sat: @@ -2838,10 +2851,6 @@ // Check for pointer simplifications. if (PointerType *PtrTy = dyn_cast(V->getType())) { - // Alloca never returns null, malloc might. - if (isa(V) && PtrTy->getAddressSpace() == 0) - return true; - // A byval, inalloca may not be null in a non-default addres space. A // nonnull argument is assumed never 0. if (const Argument *A = dyn_cast(V)) { @@ -2850,13 +2859,6 @@ A->hasNonNullAttr())) return true; } - - if (const auto *Call = dyn_cast(V)) { - if (Call->isReturnNonNull()) - return true; - if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) - return isKnownNonZero(RP, Depth, Q); - } } if (const auto *I = dyn_cast(V))