1 //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_IR_MODULESLOTTRACKER_H
10 #define LLVM_IR_MODULESLOTTRACKER_H
21 /// Manage lifetime of a slot tracker for printing IR.
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.
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 bool ShouldCreateStorage
= false;
33 bool ShouldInitializeAllMetadata
= false;
35 const Module
*M
= nullptr;
36 const Function
*F
= nullptr;
37 SlotTracker
*Machine
= nullptr;
40 /// Wrap a preinitialized SlotTracker.
41 ModuleSlotTracker(SlotTracker
&Machine
, const Module
*M
,
42 const Function
*F
= nullptr);
44 /// Construct a slot tracker from a module.
46 /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes
47 /// a slot tracker, and initializes all metadata slots. \c
48 /// ShouldInitializeAllMetadata defaults to true because this is expected to
49 /// be shared between multiple callers, and otherwise MDNode references will
51 explicit ModuleSlotTracker(const Module
*M
,
52 bool ShouldInitializeAllMetadata
= true);
54 /// Destructor to clean up storage.
57 /// Lazily creates a slot tracker.
58 SlotTracker
*getMachine();
60 const Module
*getModule() const { return M
; }
61 const Function
*getCurrentFunction() const { return F
; }
63 /// Incorporate the given function.
65 /// Purge the currently incorporated function and incorporate \c F. If \c F
66 /// is currently incorporated, this is a no-op.
67 void incorporateFunction(const Function
&F
);
69 /// Return the slot number of the specified local value.
71 /// A function that defines this value should be incorporated prior to calling
73 /// Return -1 if the value is not in the function's SlotTracker.
74 int getLocalSlot(const Value
*V
);
77 } // end namespace llvm