--- /dev/null
+//===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_IR_MODULESLOTTRACKER_H
+#define LLVM_IR_MODULESLOTTRACKER_H
+
+#include <memory>
+
+namespace llvm {
+
+class Module;
+class Function;
+class SlotTracker;
+
+/// Manage lifetime of a slot tracker for printing IR.
+///
+/// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This
+/// class allows callers to share the cost of incorporating the metadata in a
+/// module or a function.
+///
+/// If the IR changes from underneath \a ModuleSlotTracker, strings like
+/// "<badref>" will be printed, or, worse, the wrong slots entirely.
+class ModuleSlotTracker {
+ /// Storage for a slot tracker.
+ std::unique_ptr<SlotTracker> MachineStorage;
+
+ const Module *M = nullptr;
+ const Function *F = nullptr;
+ SlotTracker *Machine = nullptr;
+
+public:
+ /// Wrap a preinitialized SlotTracker.
+ ModuleSlotTracker(SlotTracker &Machine, const Module *M,
+ const Function *F = nullptr);
+
+ /// Construct a slot tracker from a module.
+ ///
+ /// If \a M is \c nullptr, uses a null slot tracker.
+ explicit ModuleSlotTracker(const Module *M);
+
+ /// Destructor to clean up storage.
+ ~ModuleSlotTracker();
+
+ SlotTracker *getMachine() const { return Machine; }
+ const Module *getModule() const { return M; }
+ const Function *getCurrentFunction() const { return F; }
+
+ /// Incorporate the given function.
+ ///
+ /// 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);
+};
+
+} // end namespace llvm
+
+#endif
#include "llvm/CodeGen/SlotIndexes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Support/Debug.h"
<< " is null\n";
return;
}
+ const Function *F = MF->getFunction();
+ const Module *M = F ? F->getParent() : nullptr;
+ ModuleSlotTracker MST(M);
+ print(OS, MST, Indexes);
+}
+
+void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST,
+ SlotIndexes *Indexes) const {
+ const MachineFunction *MF = getParent();
+ if (!MF) {
+ OS << "Can't print out MachineBasicBlock because parent MachineFunction"
+ << " is null\n";
+ return;
+ }
if (Indexes)
OS << Indexes->getMBBStartIdx(this) << '\t';
const char *Comma = "";
if (const BasicBlock *LBB = getBasicBlock()) {
OS << Comma << "derived from LLVM BB ";
- LBB->printAsOperand(OS, /*PrintType=*/false);
+ LBB->printAsOperand(OS, /*PrintType=*/false, MST);
Comma = ", ";
}
if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/TypeFinder.h"
OS << '>';
}
-namespace {
+namespace llvm {
//===----------------------------------------------------------------------===//
// SlotTracker Class: Enumerate slot numbers for unnamed values
//===----------------------------------------------------------------------===//
SlotTracker(const SlotTracker &) = delete;
void operator=(const SlotTracker &) = delete;
};
-} // namespace
+} // namespace llvm
+
+ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
+ const Function *F)
+ : M(M), F(F), Machine(&Machine) {}
+
+ModuleSlotTracker::ModuleSlotTracker(const Module *M)
+ : MachineStorage(
+ M ? new SlotTracker(M, /* ShouldInitializeAllMetadata */ true)
+ : nullptr),
+ M(M), Machine(MachineStorage.get()) {}
+
+ModuleSlotTracker::~ModuleSlotTracker() {}
+
+void ModuleSlotTracker::incorporateFunction(const Function &F) {
+ if (!Machine)
+ return;
+
+ // Nothing to do if this is the right function already.
+ if (this->F == &F)
+ return;
+ if (this->F)
+ Machine->purgeFunction();
+ Machine->incorporateFunction(&F);
+ this->F = &F;
+}
static SlotTracker *createSlotTracker(const Module *M) {
return new SlotTracker(M);
class AssemblyWriter {
formatted_raw_ostream &Out;
const Module *TheModule;
- std::unique_ptr<SlotTracker> ModuleSlotTracker;
+ std::unique_ptr<SlotTracker> SlotTrackerStorage;
SlotTracker &Machine;
TypePrinting TypePrinter;
AssemblyAnnotationWriter *AnnotationWriter;
AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
AssemblyAnnotationWriter *AAW,
bool ShouldPreserveUseListOrder)
- : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
- Machine(*ModuleSlotTracker), AnnotationWriter(AAW),
+ : Out(o), TheModule(M), SlotTrackerStorage(createSlotTracker(M)),
+ Machine(*SlotTrackerStorage), AnnotationWriter(AAW),
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
init();
}
}
}
-void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
- // Fast path: Don't construct and populate a TypePrinting object if we
- // won't be needing any types printed.
- bool IsMetadata = isa<MetadataAsValue>(this);
- if (!PrintType && ((!isa<Constant>(this) && !IsMetadata) || hasName() ||
- isa<GlobalValue>(this))) {
- WriteAsOperandInternal(O, this, nullptr, nullptr, M);
- return;
+/// Print without a type, skipping the TypePrinting object.
+///
+/// \return \c true iff printing was succesful.
+static bool printWithoutType(const Value &V, raw_ostream &O,
+ SlotTracker *Machine, const Module *M) {
+ if (V.hasName() || isa<GlobalValue>(V) ||
+ (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
+ WriteAsOperandInternal(O, &V, nullptr, Machine, M);
+ return true;
}
+ return false;
+}
- if (!M)
- M = getModuleFromVal(this);
-
+static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
+ ModuleSlotTracker &MST) {
TypePrinting TypePrinter;
- if (M)
+ if (const Module *M = MST.getModule())
TypePrinter.incorporateTypes(*M);
if (PrintType) {
- TypePrinter.print(getType(), O);
+ TypePrinter.print(V.getType(), O);
O << ' ';
}
- SlotTracker Machine(M, /* ShouldInitializeAllMetadata */ IsMetadata);
- WriteAsOperandInternal(O, this, &TypePrinter, &Machine, M);
+ WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
+ MST.getModule());
+}
+
+void Value::printAsOperand(raw_ostream &O, bool PrintType,
+ const Module *M) const {
+ if (!M)
+ M = getModuleFromVal(this);
+
+ if (!PrintType)
+ if (printWithoutType(*this, O, nullptr, M))
+ return;
+
+ SlotTracker Machine(
+ M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
+ ModuleSlotTracker MST(Machine, M);
+ printAsOperandImpl(*this, O, PrintType, MST);
+}
+
+void Value::printAsOperand(raw_ostream &O, bool PrintType,
+ ModuleSlotTracker &MST) const {
+ if (!PrintType)
+ if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
+ return;
+
+ printAsOperandImpl(*this, O, PrintType, MST);
}
static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,