diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index f5a76458e06320..c71ad2782c050d 100644 --- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -383,6 +383,17 @@ static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) { AC->registerAssumption(cast(CI)); } +static void convertMetadataToAssumes(LoadInst *LI, Value *Val, + const DataLayout &DL, AssumptionCache *AC, + const DominatorTree *DT) { + // If the load was marked as nonnull we don't want to lose + // that information when we erase this Load. So we preserve + // it with an assume. + if (AC && LI->getMetadata(LLVMContext::MD_nonnull) && + !isKnownNonZero(Val, DL, 0, AC, LI, DT)) + addAssumeNonNull(AC, LI); +} + static void removeIntrinsicUsers(AllocaInst *AI) { // Knowing that this alloca is promotable, we know that it's safe to kill all // instructions except for load and store. @@ -475,13 +486,7 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, if (ReplVal == LI) ReplVal = PoisonValue::get(LI->getType()); - // If the load was marked as nonnull we don't want to lose - // that information when we erase this Load. So we preserve - // it with an assume. - if (AC && LI->getMetadata(LLVMContext::MD_nonnull) && - !isKnownNonZero(ReplVal, DL, 0, AC, LI, &DT)) - addAssumeNonNull(AC, LI); - + convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT); LI->replaceAllUsesWith(ReplVal); LI->eraseFromParent(); LBI.deleteValue(LI); @@ -584,11 +589,7 @@ static bool promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info, ReplVal = std::prev(I)->second->getOperand(0); } - // Note, if the load was marked as nonnull we don't want to lose that - // information when we erase it. So we preserve it with an assume. - if (AC && LI->getMetadata(LLVMContext::MD_nonnull) && - !isKnownNonZero(ReplVal, DL, 0, AC, LI, &DT)) - addAssumeNonNull(AC, LI); + convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT); // If the replacement value is the load, this must occur in unreachable // code. @@ -1047,13 +1048,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, continue; Value *V = IncomingVals[AI->second]; - - // If the load was marked as nonnull we don't want to lose - // that information when we erase this Load. So we preserve - // it with an assume. - if (AC && LI->getMetadata(LLVMContext::MD_nonnull) && - !isKnownNonZero(V, SQ.DL, 0, AC, LI, &DT)) - addAssumeNonNull(AC, LI); + convertMetadataToAssumes(LI, V, SQ.DL, AC, &DT); // Anything using the load now uses the current value. LI->replaceAllUsesWith(V);