Index: llvm/lib/Analysis/MemorySSA.cpp =================================================================== --- llvm/lib/Analysis/MemorySSA.cpp +++ llvm/lib/Analysis/MemorySSA.cpp @@ -2551,6 +2551,11 @@ }; Ptr = Ptr->stripPointerCasts(); + if (auto *I = dyn_cast(Ptr)) { + if (I->getParent() == &I->getFunction()->getEntryBlock()) { + return true; + } + } if (auto *GEP = dyn_cast(Ptr)) { return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) && GEP->hasAllConstantIndices(); Index: llvm/unittests/Analysis/MemorySSATest.cpp =================================================================== --- llvm/unittests/Analysis/MemorySSATest.cpp +++ llvm/unittests/Analysis/MemorySSATest.cpp @@ -1669,3 +1669,46 @@ MemoryAccess *Load2Clobber = Walker->getClobberingMemoryAccess(Load2Access); EXPECT_EQ(Load2Clobber, Load1Access); } + +// We want to test if the location information are retained +// when the IsGuaranteedLoopInvariant function handles a +// memory access referring to a pointer defined in the entry +// block, hence automatically guaranteed to be loop invariant. +TEST_F(MemorySSATest, TestLoopInvariantEntryBlockPointer) { + F = Function::Create( + FunctionType::get(B.getVoidTy(), + {B.getInt64Ty(), B.getInt8PtrTy(), B.getInt8PtrTy()}, + false), + GlobalValue::ExternalLinkage, "F", &M); + // Craft the basic blocks + BasicBlock *Entry = BasicBlock::Create(C, "", F); + BasicBlock *Body = BasicBlock::Create(C, "", F); + BasicBlock *Exit = BasicBlock::Create(C, "", F); + // Craft the instructions + B.SetInsertPoint(Entry); + Value *GEP = B.CreateGEP(B.getInt8Ty(), F->getArg(1), F->getArg(0)); + Value *BC0 = B.CreateBitCast(GEP, B.getInt64Ty()); + Value *BC1 = B.CreateBitCast(GEP, B.getInt32Ty()); + Value *CC0 = B.CreateLoad(F->getArg(2), /* Volatile */ true); + B.CreateCondBr(CC0, Body, Exit); + B.SetInsertPoint(Body); + B.CreateStore(B.getInt32(1), BC1); + B.CreateBr(Exit); + B.SetInsertPoint(Exit); + StoreInst *SI = B.CreateStore(B.getInt64(0), BC0); + B.CreateRetVoid(); + // Setup the analysis + setupAnalyses(); + MemorySSA &MSSA = *Analyses->MSSA; + // Get the memory access and location + MemoryAccess *MA = MSSA.getMemoryAccess(SI); + MemoryLocation ML = MemoryLocation::get(SI); + // Use the 'upward_defs_iterator' which internally calls + // IsGuaranteedLoopInvariant + auto ItA = upward_defs_begin({MA, ML}, MSSA.getDomTree()); + auto ItB = upward_defs_begin({ItA->first, ItA->second}, MSSA.getDomTree()); + // Check if the location information have been retained + EXPECT_TRUE(ItB->second.Size.isPrecise()); + EXPECT_TRUE(ItB->second.Size.hasValue()); + EXPECT_TRUE(ItB->second.Size.getValue() == 8); +} \ No newline at end of file