1515//
1616// ===----------------------------------------------------------------------===//
1717
18- #include " llvm/Transforms/Utils/PromoteMemToReg.h"
1918#include " llvm/ADT/ArrayRef.h"
2019#include " llvm/ADT/DenseMap.h"
2120#include " llvm/ADT/STLExtras.h"
2221#include " llvm/ADT/SmallPtrSet.h"
2322#include " llvm/ADT/SmallVector.h"
2423#include " llvm/ADT/Statistic.h"
2524#include " llvm/Analysis/AliasSetTracker.h"
25+ #include " llvm/Analysis/AssumptionCache.h"
2626#include " llvm/Analysis/InstructionSimplify.h"
2727#include " llvm/Analysis/IteratedDominanceFrontier.h"
2828#include " llvm/Analysis/ValueTracking.h"
3838#include " llvm/IR/Metadata.h"
3939#include " llvm/IR/Module.h"
4040#include " llvm/Transforms/Utils/Local.h"
41+ #include " llvm/Transforms/Utils/PromoteMemToReg.h"
4142#include < algorithm>
4243using namespace llvm ;
4344
@@ -301,6 +302,18 @@ struct PromoteMem2Reg {
301302
302303} // end of anonymous namespace
303304
305+ // / Given a LoadInst LI this adds assume(LI != null) after it.
306+ static void addAssumeNonNull (AssumptionCache *AC, LoadInst *LI) {
307+ Function *AssumeIntrinsic =
308+ Intrinsic::getDeclaration (LI->getModule (), Intrinsic::assume);
309+ ICmpInst *LoadNotNull = new ICmpInst (ICmpInst::ICMP_NE, LI,
310+ Constant::getNullValue (LI->getType ()));
311+ LoadNotNull->insertAfter (LI);
312+ CallInst *CI = CallInst::Create (AssumeIntrinsic, {LoadNotNull});
313+ CI->insertAfter (LoadNotNull);
314+ AC->registerAssumption (CI);
315+ }
316+
304317static void removeLifetimeIntrinsicUsers (AllocaInst *AI) {
305318 // Knowing that this alloca is promotable, we know that it's safe to kill all
306319 // instructions except for load and store.
@@ -334,9 +347,9 @@ static void removeLifetimeIntrinsicUsers(AllocaInst *AI) {
334347// / and thus must be phi-ed with undef. We fall back to the standard alloca
335348// / promotion algorithm in that case.
336349static bool rewriteSingleStoreAlloca (AllocaInst *AI, AllocaInfo &Info,
337- LargeBlockInfo &LBI,
338- DominatorTree &DT ,
339- AliasSetTracker *AST ) {
350+ LargeBlockInfo &LBI, DominatorTree &DT,
351+ AliasSetTracker *AST ,
352+ AssumptionCache *AC ) {
340353 StoreInst *OnlyStore = Info.OnlyStore ;
341354 bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand (0 ));
342355 BasicBlock *StoreBB = OnlyStore->getParent ();
@@ -387,6 +400,14 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
387400 // code.
388401 if (ReplVal == LI)
389402 ReplVal = UndefValue::get (LI->getType ());
403+
404+ // If the load was marked as nonnull we don't want to lose
405+ // that information when we erase this Load. So we preserve
406+ // it with an assume.
407+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
408+ !llvm::isKnownNonNullAt (ReplVal, LI, &DT))
409+ addAssumeNonNull (AC, LI);
410+
390411 LI->replaceAllUsesWith (ReplVal);
391412 if (AST && LI->getType ()->isPointerTy ())
392413 AST->deleteValue (LI);
@@ -435,7 +456,9 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
435456// / }
436457static bool promoteSingleBlockAlloca (AllocaInst *AI, const AllocaInfo &Info,
437458 LargeBlockInfo &LBI,
438- AliasSetTracker *AST) {
459+ AliasSetTracker *AST,
460+ DominatorTree &DT,
461+ AssumptionCache *AC) {
439462 // The trickiest case to handle is when we have large blocks. Because of this,
440463 // this code is optimized assuming that large blocks happen. This does not
441464 // significantly pessimize the small block case. This uses LargeBlockInfo to
@@ -476,10 +499,17 @@ static bool promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
476499 // There is no store before this load, bail out (load may be affected
477500 // by the following stores - see main comment).
478501 return false ;
479- }
480- else
502+ } else {
481503 // Otherwise, there was a store before this load, the load takes its value.
482- LI->replaceAllUsesWith (std::prev (I)->second ->getOperand (0 ));
504+ // Note, if the load was marked as nonnull we don't want to lose that
505+ // information when we erase it. So we preserve it with an assume.
506+ Value *ReplVal = std::prev (I)->second ->getOperand (0 );
507+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
508+ !llvm::isKnownNonNullAt (ReplVal, LI, &DT))
509+ addAssumeNonNull (AC, LI);
510+
511+ LI->replaceAllUsesWith (ReplVal);
512+ }
483513
484514 if (AST && LI->getType ()->isPointerTy ())
485515 AST->deleteValue (LI);
@@ -553,7 +583,7 @@ void PromoteMem2Reg::run() {
553583 // If there is only a single store to this value, replace any loads of
554584 // it that are directly dominated by the definition with the value stored.
555585 if (Info.DefiningBlocks .size () == 1 ) {
556- if (rewriteSingleStoreAlloca (AI, Info, LBI, DT, AST)) {
586+ if (rewriteSingleStoreAlloca (AI, Info, LBI, DT, AST, AC )) {
557587 // The alloca has been processed, move on.
558588 RemoveFromAllocasList (AllocaNum);
559589 ++NumSingleStore;
@@ -564,7 +594,7 @@ void PromoteMem2Reg::run() {
564594 // If the alloca is only read and written in one basic block, just perform a
565595 // linear sweep over the block to eliminate it.
566596 if (Info.OnlyUsedInOneBlock &&
567- promoteSingleBlockAlloca (AI, Info, LBI, AST)) {
597+ promoteSingleBlockAlloca (AI, Info, LBI, AST, DT, AC )) {
568598 // The alloca has been processed, move on.
569599 RemoveFromAllocasList (AllocaNum);
570600 continue ;
@@ -940,6 +970,13 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
940970
941971 Value *V = IncomingVals[AI->second ];
942972
973+ // If the load was marked as nonnull we don't want to lose
974+ // that information when we erase this Load. So we preserve
975+ // it with an assume.
976+ if (AC && LI->getMetadata (LLVMContext::MD_nonnull) &&
977+ !llvm::isKnownNonNullAt (V, LI, &DT))
978+ addAssumeNonNull (AC, LI);
979+
943980 // Anything using the load now uses the current value.
944981 LI->replaceAllUsesWith (V);
945982 if (AST && LI->getType ()->isPointerTy ())
0 commit comments