LLVM 24.0.0git
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 "llvm/ADT/ArrayRef.h"
15#include <functional>
16#include <memory>
17#include <utility>
18
19namespace llvm {
20
21class Module;
22class Function;
23class SlotTracker;
24class Value;
25class DILocation;
26class MDNode;
27
28/// Abstract interface of slot tracker storage.
30public:
32
33 virtual void createMetadataSlot(const MDNode *) = 0;
34 virtual int getMetadataSlot(const MDNode *) = 0;
35};
36
37/// Manage lifetime of a slot tracker for printing IR.
38///
39/// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This
40/// class allows callers to share the cost of incorporating the metadata in a
41/// module or a function.
42///
43/// If the IR changes from underneath \a ModuleSlotTracker, strings like
44/// "<badref>" will be printed, or, worse, the wrong slots entirely.
46public:
49
50private:
51 /// Storage for a slot tracker.
52 std::unique_ptr<SlotTracker> MachineStorage;
53 bool ShouldCreateStorage = false;
54
55 const Module *M = nullptr;
56 const Function *F = nullptr;
57 SlotTracker *Machine = nullptr;
58
59 std::function<void(AbstractSlotTrackerStorage *, const Module *)>
60 ProcessModuleHookFn;
61 std::function<void(AbstractSlotTrackerStorage *, const Function *)>
62 ProcessFunctionHookFn;
63
64protected:
65 /// Renumber module metadata and then additional metadata for canonical
66 /// assembly output.
68 ArrayRef<const MDNode *> AdditionalMetadata,
69 MachineMDNodeListType *AdditionalMetadataNodes = nullptr) const;
70
71 /// Collect metadata reachable from \p AdditionalMetadata but not from the
72 /// module.
74 ArrayRef<const MDNode *> AdditionalMetadata,
75 MachineMDNodeListType &AdditionalMetadataNodes) const;
76
77public:
78 /// Wrap a preinitialized SlotTracker.
79 ModuleSlotTracker(SlotTracker &Machine, const Module *M,
80 const Function *F = nullptr);
81
82 /// Construct a slot tracker from a module.
83 ///
84 /// If \a M is \c nullptr, uses a null slot tracker.
85 explicit ModuleSlotTracker(const Module *M);
86
87 /// Destructor to clean up storage.
89
90 /// Lazily creates a slot tracker.
92
93 const Module *getModule() const { return M; }
94 const Function *getCurrentFunction() const { return F; }
95
96 /// Incorporate the given function.
97 ///
98 /// Purge the currently incorporated function and incorporate \c F. If \c F
99 /// is currently incorporated, this is a no-op.
100 void incorporateFunction(const Function &F);
101
102 /// Return the slot number of the specified local value.
103 ///
104 /// A function that defines this value should be incorporated prior to calling
105 /// this method.
106 /// Return -1 if the value is not in the function's SlotTracker.
107 int getLocalSlot(const Value *V);
108
109 void setProcessHook(
110 std::function<void(AbstractSlotTrackerStorage *, const Module *)>);
111 void setProcessHook(
112 std::function<void(AbstractSlotTrackerStorage *, const Function *)>);
113
114 void collectMDNodes(MachineMDNodeListType &L) const;
115
116 /// Return whether a debug location should be printed inline instead of by ID.
117 virtual bool shouldPrintDebugLocationInline(const DILocation *) const {
118 return false;
119 }
120};
121
122} // end namespace llvm
123
124#endif
#define LLVM_ABI
Definition Compiler.h:215
static SmallVector< const MDNode * > collectAdditionalMetadata(Module &M, const DXILDebugInfoMap &DI)
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the SmallVector class.
Abstract interface of slot tracker storage.
virtual int getMetadataSlot(const MDNode *)=0
virtual void createMetadataSlot(const MDNode *)=0
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Metadata node.
Definition Metadata.h:1069
const Module * getModule() const
ModuleSlotTracker(SlotTracker &Machine, const Module *M, const Function *F=nullptr)
Wrap a preinitialized SlotTracker.
void renumberMetadataForAssembly(ArrayRef< const MDNode * > AdditionalMetadata, MachineMDNodeListType *AdditionalMetadataNodes=nullptr) const
Renumber module metadata and then additional metadata for canonical assembly output.
virtual ~ModuleSlotTracker()
Destructor to clean up storage.
virtual bool shouldPrintDebugLocationInline(const DILocation *) const
Return whether a debug location should be printed inline instead of by ID.
SlotTracker * getMachine()
Lazily creates a slot tracker.
const Function * getCurrentFunction() const
SmallVector< std::pair< unsigned, const MDNode * >, 0 > MachineMDNodeListType
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class provides computation of slot numbers for LLVM Assembly writing.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.