blob: eb26fba906ea8cea513692bc2e87ebfa8820b1eb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- 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
15namespace llvm {
16
17class Module;
18class Function;
19class SlotTracker;
20class 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.
30class ModuleSlotTracker {
31 /// Storage for a slot tracker.
32 std::unique_ptr<SlotTracker> MachineStorage;
33 bool ShouldCreateStorage = false;
34 bool ShouldInitializeAllMetadata = false;
35
36 const Module *M = nullptr;
37 const Function *F = nullptr;
38 SlotTracker *Machine = nullptr;
39
40public:
41 /// Wrap a preinitialized SlotTracker.
42 ModuleSlotTracker(SlotTracker &Machine, const Module *M,
43 const Function *F = nullptr);
44
45 /// Construct a slot tracker from a module.
46 ///
47 /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes
48 /// a slot tracker, and initializes all metadata slots. \c
49 /// ShouldInitializeAllMetadata defaults to true because this is expected to
50 /// be shared between multiple callers, and otherwise MDNode references will
51 /// not match up.
52 explicit ModuleSlotTracker(const Module *M,
53 bool ShouldInitializeAllMetadata = true);
54
55 /// Destructor to clean up storage.
56 ~ModuleSlotTracker();
57
58 /// Lazily creates a slot tracker.
59 SlotTracker *getMachine();
60
61 const Module *getModule() const { return M; }
62 const Function *getCurrentFunction() const { return F; }
63
64 /// Incorporate the given function.
65 ///
66 /// Purge the currently incorporated function and incorporate \c F. If \c F
67 /// is currently incorporated, this is a no-op.
68 void incorporateFunction(const Function &F);
69
70 /// Return the slot number of the specified local value.
71 ///
72 /// A function that defines this value should be incorporated prior to calling
73 /// this method.
74 /// Return -1 if the value is not in the function's SlotTracker.
75 int getLocalSlot(const Value *V);
76};
77
78} // end namespace llvm
79
80#endif