[llvm] a4fb886 - [MSSA] Extending IsGuaranteedLoopInvariant to support an instruction defined in the entry block
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 23 14:51:35 PDT 2021
Author: Matteo Favaro
Date: 2021-03-23T21:50:56Z
New Revision: a4fb88669cd98db6fef7dcac88e3ec425d40c00d
URL: https://github.com/llvm/llvm-project/commit/a4fb88669cd98db6fef7dcac88e3ec425d40c00d
DIFF: https://github.com/llvm/llvm-project/commit/a4fb88669cd98db6fef7dcac88e3ec425d40c00d.diff
LOG: [MSSA] Extending IsGuaranteedLoopInvariant to support an instruction defined in the entry block
As mentioned in [[ https://reviews.llvm.org/D96979 | D96979 ]], I'm extending the **IsGuaranteedLoopInvariant** check also to the `MemorySSA.cpp` file.
@fhahn For now I didn't unify the function into `MemorySSA.h` because, as you mentioned, it's not directly MSSA related. I'm open to suggestions to find a better place so we can improve the unification process.
Reviewed By: fhahn
Differential Revision: https://reviews.llvm.org/D97155
Added:
Modified:
llvm/lib/Analysis/MemorySSA.cpp
llvm/unittests/Analysis/MemorySSATest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/MemorySSA.cpp b/llvm/lib/Analysis/MemorySSA.cpp
index 83e634103409..bdceaa790ff1 100644
--- a/llvm/lib/Analysis/MemorySSA.cpp
+++ b/llvm/lib/Analysis/MemorySSA.cpp
@@ -2550,6 +2550,11 @@ bool upward_defs_iterator::IsGuaranteedLoopInvariant(Value *Ptr) const {
};
Ptr = Ptr->stripPointerCasts();
+ if (auto *I = dyn_cast<Instruction>(Ptr)) {
+ if (I->getParent() == &I->getFunction()->getEntryBlock()) {
+ return true;
+ }
+ }
if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
GEP->hasAllConstantIndices();
diff --git a/llvm/unittests/Analysis/MemorySSATest.cpp b/llvm/unittests/Analysis/MemorySSATest.cpp
index f34677997010..f8854f8052f4 100644
--- 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 @@ TEST_F(MemorySSATest, TestLoadClobber) {
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
More information about the llvm-commits
mailing list