[llvm] r243336 - IR: Expose the method 'getLocalSlot' in the module slot tracker.

Alex Lorenz arphaman at gmail.com
Mon Jul 27 15:31:04 PDT 2015


Author: arphaman
Date: Mon Jul 27 17:31:04 2015
New Revision: 243336

URL: http://llvm.org/viewvc/llvm-project?rev=243336&view=rev
Log:
IR: Expose the method 'getLocalSlot' in the module slot tracker.

This commit publicly exposes the method 'getLocalSlot' in the
'ModuleSlotTracker' class.

This change is useful for MIR serialization, to serialize the unnamed basic
block and unnamed alloca references.

Reviewers: Duncan P. N. Exon Smith

Modified:
    llvm/trunk/include/llvm/IR/ModuleSlotTracker.h
    llvm/trunk/lib/IR/AsmWriter.cpp
    llvm/trunk/unittests/IR/ValueTest.cpp

Modified: llvm/trunk/include/llvm/IR/ModuleSlotTracker.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/ModuleSlotTracker.h?rev=243336&r1=243335&r2=243336&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/ModuleSlotTracker.h (original)
+++ llvm/trunk/include/llvm/IR/ModuleSlotTracker.h Mon Jul 27 17:31:04 2015
@@ -17,6 +17,7 @@ namespace llvm {
 class Module;
 class Function;
 class SlotTracker;
+class Value;
 
 /// Manage lifetime of a slot tracker for printing IR.
 ///
@@ -61,6 +62,13 @@ public:
   /// Purge the currently incorporated function and incorporate \c F.  If \c F
   /// is currently incorporated, this is a no-op.
   void incorporateFunction(const Function &F);
+
+  /// Return the slot number of the specified local value.
+  ///
+  /// A function that defines this value should be incorporated prior to calling
+  /// this method.
+  /// Return -1 if the value is not in the function's SlotTracker.
+  int getLocalSlot(const Value *V);
 };
 
 } // end namespace llvm

Modified: llvm/trunk/lib/IR/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AsmWriter.cpp?rev=243336&r1=243335&r2=243336&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp (original)
+++ llvm/trunk/lib/IR/AsmWriter.cpp Mon Jul 27 17:31:04 2015
@@ -702,6 +702,11 @@ void ModuleSlotTracker::incorporateFunct
   this->F = &F;
 }
 
+int ModuleSlotTracker::getLocalSlot(const Value *V) {
+  assert(F && "No function incorporated");
+  return Machine->getLocalSlot(V);
+}
+
 static SlotTracker *createSlotTracker(const Module *M) {
   return new SlotTracker(M);
 }

Modified: llvm/trunk/unittests/IR/ValueTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ValueTest.cpp?rev=243336&r1=243335&r2=243336&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ValueTest.cpp (original)
+++ llvm/trunk/unittests/IR/ValueTest.cpp Mon Jul 27 17:31:04 2015
@@ -175,4 +175,64 @@ TEST(ValueTest, printSlots) {
 #undef CHECK_PRINT_AS_OPERAND
 }
 
+TEST(ValueTest, getLocalSlots) {
+  // Verify that the getLocalSlot method returns the correct slot numbers.
+  LLVMContext C;
+  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
+                             "entry:\n"
+                             "  %0 = add i32 %y, 1\n"
+                             "  %1 = add i32 %y, 1\n"
+                             "  br label %2\n"
+                             "\n"
+                             "  ret void\n"
+                             "}\n";
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
+
+  Function *F = M->getFunction("f");
+  ASSERT_TRUE(F);
+  ASSERT_FALSE(F->empty());
+  BasicBlock &EntryBB = F->getEntryBlock();
+  ASSERT_EQ(3u, EntryBB.size());
+  BasicBlock *BB2 = ++F->begin();
+  ASSERT_TRUE(BB2);
+
+  Instruction *I0 = EntryBB.begin();
+  ASSERT_TRUE(I0);
+  Instruction *I1 = ++EntryBB.begin();
+  ASSERT_TRUE(I1);
+
+  ModuleSlotTracker MST(M.get());
+  MST.incorporateFunction(*F);
+  EXPECT_EQ(MST.getLocalSlot(I0), 0);
+  EXPECT_EQ(MST.getLocalSlot(I1), 1);
+  EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1);
+  EXPECT_EQ(MST.getLocalSlot(BB2), 2);
+}
+
+#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
+TEST(ValueTest, getLocalSlotDeath) {
+  LLVMContext C;
+  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
+                             "entry:\n"
+                             "  %0 = add i32 %y, 1\n"
+                             "  %1 = add i32 %y, 1\n"
+                             "  br label %2\n"
+                             "\n"
+                             "  ret void\n"
+                             "}\n";
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
+
+  Function *F = M->getFunction("f");
+  ASSERT_TRUE(F);
+  ASSERT_FALSE(F->empty());
+  BasicBlock *BB2 = ++F->begin();
+  ASSERT_TRUE(BB2);
+
+  ModuleSlotTracker MST(M.get());
+  EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
+}
+#endif
+
 } // end anonymous namespace





More information about the llvm-commits mailing list