LLVM  9.0.0svn
ModuleSlotTracker.h
Go to the documentation of this file.
1 //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_IR_MODULESLOTTRACKER_H
10 #define LLVM_IR_MODULESLOTTRACKER_H
11 
12 #include <memory>
13 
14 namespace llvm {
15 
16 class Module;
17 class Function;
18 class SlotTracker;
19 class Value;
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.
30  /// Storage for a slot tracker.
31  std::unique_ptr<SlotTracker> MachineStorage;
32  bool ShouldCreateStorage = false;
33  bool ShouldInitializeAllMetadata = false;
34 
35  const Module *M = nullptr;
36  const Function *F = nullptr;
37  SlotTracker *Machine = nullptr;
38 
39 public:
40  /// Wrap a preinitialized SlotTracker.
41  ModuleSlotTracker(SlotTracker &Machine, const Module *M,
42  const Function *F = nullptr);
43 
44  /// Construct a slot tracker from a module.
45  ///
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
50  /// not match up.
51  explicit ModuleSlotTracker(const Module *M,
52  bool ShouldInitializeAllMetadata = true);
53 
54  /// Destructor to clean up storage.
56 
57  /// Lazily creates a slot tracker.
59 
60  const Module *getModule() const { return M; }
61  const Function *getCurrentFunction() const { return F; }
62 
63  /// Incorporate the given function.
64  ///
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);
68 
69  /// Return the slot number of the specified local value.
70  ///
71  /// A function that defines this value should be incorporated prior to calling
72  /// this method.
73  /// Return -1 if the value is not in the function's SlotTracker.
74  int getLocalSlot(const Value *V);
75 };
76 
77 } // end namespace llvm
78 
79 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
const Function * getCurrentFunction() const
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
Definition: AsmWriter.cpp:857
Manage lifetime of a slot tracker for printing IR.
SlotTracker * getMachine()
Lazily creates a slot tracker.
Definition: AsmWriter.cpp:832
const Module * getModule() const
This class provides computation of slot numbers for LLVM Assembly writing.
Definition: AsmWriter.cpp:668
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:843
ModuleSlotTracker(SlotTracker &Machine, const Module *M, const Function *F=nullptr)
Wrap a preinitialized SlotTracker.
Definition: AsmWriter.cpp:821
~ModuleSlotTracker()
Destructor to clean up storage.
LLVM Value Representation.
Definition: Value.h:72