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 class Value; 21 22 /// Manage lifetime of a slot tracker for printing IR. 23 /// 24 /// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This 25 /// class allows callers to share the cost of incorporating the metadata in a 26 /// module or a function. 27 /// 28 /// If the IR changes from underneath \a ModuleSlotTracker, strings like 29 /// "<badref>" will be printed, or, worse, the wrong slots entirely. 30 class ModuleSlotTracker { 31 /// Storage for a slot tracker. 32 std::unique_ptr<SlotTracker> MachineStorage; 33 34 const Module *M = nullptr; 35 const Function *F = nullptr; 36 SlotTracker *Machine = nullptr; 37 38 public: 39 /// Wrap a preinitialized SlotTracker. 40 ModuleSlotTracker(SlotTracker &Machine, const Module *M, 41 const Function *F = nullptr); 42 43 /// Construct a slot tracker from a module. 44 /// 45 /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes 46 /// a slot tracker, and initializes all metadata slots. \c 47 /// ShouldInitializeAllMetadata defaults to true because this is expected to 48 /// be shared between multiple callers, and otherwise MDNode references will 49 /// not match up. 50 explicit ModuleSlotTracker(const Module *M, 51 bool ShouldInitializeAllMetadata = true); 52 53 /// Destructor to clean up storage. 54 ~ModuleSlotTracker(); 55 getMachine()56 SlotTracker *getMachine() const { return Machine; } getModule()57 const Module *getModule() const { return M; } getCurrentFunction()58 const Function *getCurrentFunction() const { return F; } 59 60 /// Incorporate the given function. 61 /// 62 /// Purge the currently incorporated function and incorporate \c F. If \c F 63 /// is currently incorporated, this is a no-op. 64 void incorporateFunction(const Function &F); 65 66 /// Return the slot number of the specified local value. 67 /// 68 /// A function that defines this value should be incorporated prior to calling 69 /// this method. 70 /// Return -1 if the value is not in the function's SlotTracker. 71 int getLocalSlot(const Value *V); 72 }; 73 74 } // end namespace llvm 75 76 #endif 77