llvm.org GIT mirror llvm / 9adb212
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243336 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Lorenz 5 years ago
3 changed file(s) with 73 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1616 class Module;
1717 class Function;
1818 class SlotTracker;
19 class Value;
1920
2021 /// Manage lifetime of a slot tracker for printing IR.
2122 ///
6061 /// Purge the currently incorporated function and incorporate \c F. If \c F
6162 /// is currently incorporated, this is a no-op.
6263 void incorporateFunction(const Function &F);
64
65 /// Return the slot number of the specified local value.
66 ///
67 /// A function that defines this value should be incorporated prior to calling
68 /// this method.
69 /// Return -1 if the value is not in the function's SlotTracker.
70 int getLocalSlot(const Value *V);
6371 };
6472
6573 } // end namespace llvm
699699 Machine->purgeFunction();
700700 Machine->incorporateFunction(&F);
701701 this->F = &F;
702 }
703
704 int ModuleSlotTracker::getLocalSlot(const Value *V) {
705 assert(F && "No function incorporated");
706 return Machine->getLocalSlot(V);
702707 }
703708
704709 static SlotTracker *createSlotTracker(const Module *M) {
174174 #undef CHECK_PRINT_AS_OPERAND
175175 }
176176
177 TEST(ValueTest, getLocalSlots) {
178 // Verify that the getLocalSlot method returns the correct slot numbers.
179 LLVMContext C;
180 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
181 "entry:\n"
182 " %0 = add i32 %y, 1\n"
183 " %1 = add i32 %y, 1\n"
184 " br label %2\n"
185 "\n"
186 " ret void\n"
187 "}\n";
188 SMDiagnostic Err;
189 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
190
191 Function *F = M->getFunction("f");
192 ASSERT_TRUE(F);
193 ASSERT_FALSE(F->empty());
194 BasicBlock &EntryBB = F->getEntryBlock();
195 ASSERT_EQ(3u, EntryBB.size());
196 BasicBlock *BB2 = ++F->begin();
197 ASSERT_TRUE(BB2);
198
199 Instruction *I0 = EntryBB.begin();
200 ASSERT_TRUE(I0);
201 Instruction *I1 = ++EntryBB.begin();
202 ASSERT_TRUE(I1);
203
204 ModuleSlotTracker MST(M.get());
205 MST.incorporateFunction(*F);
206 EXPECT_EQ(MST.getLocalSlot(I0), 0);
207 EXPECT_EQ(MST.getLocalSlot(I1), 1);
208 EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1);
209 EXPECT_EQ(MST.getLocalSlot(BB2), 2);
210 }
211
212 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
213 TEST(ValueTest, getLocalSlotDeath) {
214 LLVMContext C;
215 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
216 "entry:\n"
217 " %0 = add i32 %y, 1\n"
218 " %1 = add i32 %y, 1\n"
219 " br label %2\n"
220 "\n"
221 " ret void\n"
222 "}\n";
223 SMDiagnostic Err;
224 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
225
226 Function *F = M->getFunction("f");
227 ASSERT_TRUE(F);
228 ASSERT_FALSE(F->empty());
229 BasicBlock *BB2 = ++F->begin();
230 ASSERT_TRUE(BB2);
231
232 ModuleSlotTracker MST(M.get());
233 EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
234 }
235 #endif
236
177237 } // end anonymous namespace