diff --git a/llvm/include/llvm/Analysis/MemoryBuiltins.h b/llvm/include/llvm/Analysis/MemoryBuiltins.h --- a/llvm/include/llvm/Analysis/MemoryBuiltins.h +++ b/llvm/include/llvm/Analysis/MemoryBuiltins.h @@ -115,9 +115,10 @@ const TargetLibraryInfo *TLI, std::function Mapper); -/// If this allocation function initializes memory to a fixed value, return -/// said value in the requested type. Otherwise, return nullptr. -Constant *getInitialValueOfAllocation(const CallBase *Alloc, +/// If this is a call to an allocation function that initializes memory to a +/// fixed value, return said value in the requested type. Otherwise, return +/// nullptr. +Constant *getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty); diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp --- a/llvm/lib/Analysis/MemoryBuiltins.cpp +++ b/llvm/lib/Analysis/MemoryBuiltins.cpp @@ -420,10 +420,12 @@ return Size; } -Constant *llvm::getInitialValueOfAllocation(const CallBase *Alloc, +Constant *llvm::getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty) { - assert(isAllocationFn(Alloc, TLI)); + auto *Alloc = dyn_cast(V); + if (!Alloc) + return nullptr; // malloc and aligned_alloc are uninitialized (undef) if (isMallocLikeFn(Alloc, TLI) || isAlignedAllocLikeFn(Alloc, TLI)) diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -222,8 +222,8 @@ const TargetLibraryInfo *TLI) { if (isa(Obj)) return UndefValue::get(&Ty); - if (isAllocationFn(&Obj, TLI)) - return getInitialValueOfAllocation(&cast(Obj), TLI, &Ty); + if (Constant *Init = getInitialValueOfAllocation(&Obj, TLI, &Ty)) + return Init; auto *GV = dyn_cast(&Obj); if (!GV) return nullptr; diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1796,10 +1796,9 @@ if (!isRemovable(DefI)) return false; - if (StoredConstant && isAllocationFn(DefUO, &TLI)) { - auto *CB = cast(DefUO); - auto *InitC = getInitialValueOfAllocation(CB, &TLI, - StoredConstant->getType()); + if (StoredConstant) { + Constant *InitC = + getInitialValueOfAllocation(DefUO, &TLI, StoredConstant->getType()); // If the clobbering access is LiveOnEntry, no instructions between them // can modify the memory location. if (InitC && InitC == StoredConstant) diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -1232,12 +1232,11 @@ return true; } - if (isAllocationFn(DepInst, TLI)) - if (auto *InitVal = getInitialValueOfAllocation(cast(DepInst), - TLI, Load->getType())) { - Res = AvailableValue::get(InitVal); - return true; - } + if (Constant *InitVal = + getInitialValueOfAllocation(DepInst, TLI, Load->getType())) { + Res = AvailableValue::get(InitVal); + return true; + } if (StoreInst *S = dyn_cast(DepInst)) { // Reject loads and stores that are to the same address but are of diff --git a/llvm/lib/Transforms/Scalar/NewGVN.cpp b/llvm/lib/Transforms/Scalar/NewGVN.cpp --- a/llvm/lib/Transforms/Scalar/NewGVN.cpp +++ b/llvm/lib/Transforms/Scalar/NewGVN.cpp @@ -1507,9 +1507,8 @@ else if (auto *II = dyn_cast(DepInst)) { if (II->getIntrinsicID() == Intrinsic::lifetime_start) return createConstantExpression(UndefValue::get(LoadType)); - } else if (isAllocationFn(DepInst, TLI)) - if (auto *InitVal = getInitialValueOfAllocation(cast(DepInst), - TLI, LoadType)) + } else if (auto *InitVal = + getInitialValueOfAllocation(DepInst, TLI, LoadType)) return createConstantExpression(InitVal); return nullptr;