995467edf36dc5f4deebad9ebc888284940c1ff9
[oota-llvm.git] / include / llvm / IR / ModuleSlotTracker.h
1 //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_IR_MODULESLOTTRACKER_H
11 #define LLVM_IR_MODULESLOTTRACKER_H
12
13 #include <memory>
14
15 namespace llvm {
16
17 class Module;
18 class Function;
19 class SlotTracker;
20
21 /// Manage lifetime of a slot tracker for printing IR.
22 ///
23 /// Wrapper around the \a SlotTracker used internally by \a AsmWriter.  This
24 /// class allows callers to share the cost of incorporating the metadata in a
25 /// module or a function.
26 ///
27 /// If the IR changes from underneath \a ModuleSlotTracker, strings like
28 /// "<badref>" will be printed, or, worse, the wrong slots entirely.
29 class ModuleSlotTracker {
30   /// Storage for a slot tracker.
31   std::unique_ptr<SlotTracker> MachineStorage;
32
33   const Module *M = nullptr;
34   const Function *F = nullptr;
35   SlotTracker *Machine = nullptr;
36
37 public:
38   /// Wrap a preinitialized SlotTracker.
39   ModuleSlotTracker(SlotTracker &Machine, const Module *M,
40                     const Function *F = nullptr);
41
42   /// Construct a slot tracker from a module.
43   ///
44   /// If \a M is \c nullptr, uses a null slot tracker.
45   explicit ModuleSlotTracker(const Module *M);
46
47   /// Destructor to clean up storage.
48   ~ModuleSlotTracker();
49
50   SlotTracker *getMachine() const { return Machine; }
51   const Module *getModule() const { return M; }
52   const Function *getCurrentFunction() const { return F; }
53
54   /// Incorporate the given function.
55   ///
56   /// Purge the currently incorporated function and incorporate \c F.  If \c F
57   /// is currently incorporated, this is a no-op.
58   void incorporateFunction(const Function &F);
59 };
60
61 } // end namespace llvm
62
63 #endif