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

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 23 14:51:41 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGa4fb88669cd9: [MSSA] Extending IsGuaranteedLoopInvariant to support an instruction defined in… (authored by fvrmatteo, committed by fhahn).

Repository:
  rG LLVM Github Monorepo

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
@@ -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
Index: llvm/lib/Analysis/MemorySSA.cpp
===================================================================
--- llvm/lib/Analysis/MemorySSA.cpp
+++ llvm/lib/Analysis/MemorySSA.cpp
@@ -2550,6 +2550,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.332796.patch
Type: text/x-patch
Size: 3487 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210323/05e8e4dd/attachment.bin>


More information about the llvm-commits mailing list