///
/// If \c M is provided, metadata nodes will be numbered canonically;
/// otherwise, pointer addresses are substituted.
+ /// @{
void print(raw_ostream &OS, const Module *M = nullptr) const;
+ void print(raw_ostream &OS, ModuleSlotTracker &MST,
+ const Module *M = nullptr) const;
+ /// @}
/// \brief Print as operand.
///
void dump() const;
/// \brief Implement operator<< on Value.
+ /// @{
void print(raw_ostream &O) const;
+ void print(raw_ostream &O, ModuleSlotTracker &MST) const;
+ /// @}
/// \brief Print the name of this Value out to the specified raw_ostream.
///
}
void Value::print(raw_ostream &ROS) const {
+ bool ShouldInitializeAllMetadata = false;
+ if (auto *I = dyn_cast<Instruction>(this))
+ ShouldInitializeAllMetadata = isReferencingMDNode(*I);
+ else if (isa<Function>(this) || isa<MetadataAsValue>(this))
+ ShouldInitializeAllMetadata = true;
+
+ ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
+ print(ROS, MST);
+}
+
+void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST) const {
formatted_raw_ostream OS(ROS);
+ SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
+ SlotTracker &SlotTable =
+ MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
+ auto incorporateFunction = [&](const Function *F) {
+ if (F)
+ MST.incorporateFunction(*F);
+ };
+
if (const Instruction *I = dyn_cast<Instruction>(this)) {
- const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr;
- SlotTracker SlotTable(
- F,
- /* ShouldInitializeAllMetadata */ isReferencingMDNode(*I));
+ incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr);
W.printInstruction(*I);
} else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
- SlotTracker SlotTable(BB->getParent());
+ incorporateFunction(BB->getParent());
AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr);
W.printBasicBlock(BB);
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
- SlotTracker SlotTable(GV->getParent(),
- /* ShouldInitializeAllMetadata */ isa<Function>(GV));
AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr);
if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
W.printGlobal(V);
else
W.printAlias(cast<GlobalAlias>(GV));
} else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
- V->getMetadata()->print(ROS, getModuleFromVal(V));
+ V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
} else if (const Constant *C = dyn_cast<Constant>(this)) {
TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS);
OS << ' ';
- WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
+ WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
} else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
- this->printAsOperand(OS);
+ this->printAsOperand(OS, /* PrintType */ true, MST);
} else {
llvm_unreachable("Unknown value to print out!");
}
printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
}
+void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
+ const Module *M) const {
+ printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
+}
+
// Value::dump - allow easy printing of Values from the debugger.
LLVM_DUMP_METHOD
void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/raw_ostream.h"
EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
+
+ ModuleSlotTracker MST(&M);
+ EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
+ EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
}
TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
+
+ ModuleSlotTracker MST(&M);
+ EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
+ EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
+ EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
+ EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
+ EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
+ EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
}
#undef EXPECT_PRINTER_EQ
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
#endif
#endif
+TEST(ValueTest, printSlots) {
+ // Check that Value::print() and Value::printAsOperand() work with and
+ // without a slot tracker.
+ 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"
+ " 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 &BB = F->getEntryBlock();
+ ASSERT_EQ(3u, BB.size());
+
+ Instruction *I0 = BB.begin();
+ ASSERT_TRUE(I0);
+ Instruction *I1 = ++BB.begin();
+ ASSERT_TRUE(I1);
+
+ ModuleSlotTracker MST(M.get());
+
+#define CHECK_PRINT(INST, STR) \
+ do { \
+ { \
+ std::string S; \
+ raw_string_ostream OS(S); \
+ INST->print(OS); \
+ EXPECT_EQ(STR, OS.str()); \
+ } \
+ { \
+ std::string S; \
+ raw_string_ostream OS(S); \
+ INST->print(OS, MST); \
+ EXPECT_EQ(STR, OS.str()); \
+ } \
+ } while (false)
+ CHECK_PRINT(I0, " %0 = add i32 %y, 1");
+ CHECK_PRINT(I1, " %1 = add i32 %y, 1");
+#undef CHECK_PRINT
+
+#define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
+ do { \
+ { \
+ std::string S; \
+ raw_string_ostream OS(S); \
+ INST->printAsOperand(OS, TYPE); \
+ EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
+ } \
+ { \
+ std::string S; \
+ raw_string_ostream OS(S); \
+ INST->printAsOperand(OS, TYPE, MST); \
+ EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
+ } \
+ } while (false)
+ CHECK_PRINT_AS_OPERAND(I0, false, "%0");
+ CHECK_PRINT_AS_OPERAND(I1, false, "%1");
+ CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0");
+ CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1");
+#undef CHECK_PRINT_AS_OPERAND
+}
+
} // end anonymous namespace