blob: 85f8ff938366c875335ccac2d677d3dd06075b55 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_IR_MODULESLOTTRACKER_H
10#define LLVM_IR_MODULESLOTTRACKER_H
11
12#include <memory>
13
14namespace llvm {
15
16class Module;
17class Function;
18class SlotTracker;
19class 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.
29class ModuleSlotTracker {
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
39public:
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.
55 ~ModuleSlotTracker();
56
57 /// Lazily creates a slot tracker.
58 SlotTracker *getMachine();
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