[PATCH] D97155: [MSSA] Extending IsGuaranteedLoopInvariant to support an instruction defined in the entry block

Matteo Favaro via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 5 08:35:34 PST 2021


fvrmatteo updated this revision to Diff 328539.
fvrmatteo added a comment.

Added a unit test to verify that the modification to **IsGuaranteeLoopInvariant** leads to retaining the location information associated to a MemoryAccess (in this case a MemoryPhi) when handling a pointer defined in the entry block.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97155/new/

https://reviews.llvm.org/D97155

Files:
  llvm/lib/Analysis/MemorySSA.cpp
  llvm/unittests/Analysis/MemorySSATest.cpp


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
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<Instruction>(Ptr)) {
+    if (I->getParent() == &I->getFunction()->getEntryBlock()) {
+      return true;
+    }
+  }
   if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
     return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
            GEP->hasAllConstantIndices();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97155.328539.patch
Type: text/x-patch
Size: 2836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210305/c966c47f/attachment.bin>


More information about the llvm-commits mailing list