diff --git a/llvm/lib/Analysis/MemorySSA.cpp b/llvm/lib/Analysis/MemorySSA.cpp --- a/llvm/lib/Analysis/MemorySSA.cpp +++ b/llvm/lib/Analysis/MemorySSA.cpp @@ -2550,6 +2550,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(); diff --git a/llvm/unittests/Analysis/MemorySSATest.cpp b/llvm/unittests/Analysis/MemorySSATest.cpp --- a/llvm/unittests/Analysis/MemorySSATest.cpp +++ b/llvm/unittests/Analysis/MemorySSATest.cpp @@ -11,12 +11,14 @@ #include "llvm/Analysis/BasicAliasAnalysis.h" #include "llvm/Analysis/MemorySSAUpdater.h" #include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/AsmParser/Parser.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" using namespace llvm; @@ -1669,3 +1671,52 @@ 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) { + SMDiagnostic E; + auto LocalM = + parseAssemblyString("define void @test(i64 %a0, i8* %a1, i1* %a2) {\n" + "entry:\n" + "%v0 = getelementptr i8, i8* %a1, i64 %a0\n" + "%v1 = bitcast i8* %v0 to i64*\n" + "%v2 = bitcast i8* %v0 to i32*\n" + "%v3 = load i1, i1* %a2\n" + "br i1 %v3, label %body, label %exit\n" + "body:\n" + "store i32 1, i32* %v2\n" + "br label %exit\n" + "exit:\n" + "store i64 0, i64* %v1\n" + "ret void\n" + "}", + E, C); + ASSERT_TRUE(LocalM); + F = LocalM->getFunction("test"); + ASSERT_TRUE(F); + // Setup the analysis + setupAnalyses(); + MemorySSA &MSSA = *Analyses->MSSA; + // Find the exit block + for (auto &BB : *F) { + if (BB.getName() == "exit") { + // Get the store instruction + auto *SI = BB.getFirstNonPHI(); + // 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