LLVM  13.0.0git
MDBuilder.h
Go to the documentation of this file.
1 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- 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 // This file defines the MDBuilder class, which is used as a convenient way to
10 // create LLVM metadata with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_MDBUILDER_H
15 #define LLVM_IR_MDBUILDER_H
16 
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <utility>
23 
24 namespace llvm {
25 
26 class APInt;
27 template <typename T> class ArrayRef;
28 class LLVMContext;
29 class Constant;
30 class ConstantAsMetadata;
31 class MDNode;
32 class MDString;
33 class Metadata;
34 
35 class MDBuilder {
36  LLVMContext &Context;
37 
38 public:
39  MDBuilder(LLVMContext &context) : Context(context) {}
40 
41  /// Return the given string as metadata.
43 
44  /// Return the given constant as metadata.
46 
47  //===------------------------------------------------------------------===//
48  // FPMath metadata.
49  //===------------------------------------------------------------------===//
50 
51  /// Return metadata with the given settings. The special value 0.0
52  /// for the Accuracy parameter indicates the default (maximal precision)
53  /// setting.
54  MDNode *createFPMath(float Accuracy);
55 
56  //===------------------------------------------------------------------===//
57  // Prof metadata.
58  //===------------------------------------------------------------------===//
59 
60  /// Return metadata containing two branch weights.
61  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
62 
63  /// Return metadata containing a number of branch weights.
65 
66  /// Return metadata specifying that a branch or switch is unpredictable.
68 
69  /// Return metadata containing the entry \p Count for a function, a boolean
70  /// \Synthetic indicating whether the counts were synthetized, and the
71  /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
72  /// enable the same inlines as the profiled optimized binary
73  MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
74  const DenseSet<GlobalValue::GUID> *Imports);
75 
76  /// Return metadata containing the section prefix for a function.
78 
79  /// Return metadata containing the pseudo probe descriptor for a function.
80  MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, Function *F);
81 
82  //===------------------------------------------------------------------===//
83  // Range metadata.
84  //===------------------------------------------------------------------===//
85 
86  /// Return metadata describing the range [Lo, Hi).
87  MDNode *createRange(const APInt &Lo, const APInt &Hi);
88 
89  /// Return metadata describing the range [Lo, Hi).
91 
92  //===------------------------------------------------------------------===//
93  // Callees metadata.
94  //===------------------------------------------------------------------===//
95 
96  /// Return metadata indicating the possible callees of indirect
97  /// calls.
99 
100  //===------------------------------------------------------------------===//
101  // Callback metadata.
102  //===------------------------------------------------------------------===//
103 
104  /// Return metadata describing a callback (see llvm::AbstractCallSite).
105  MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments,
106  bool VarArgsArePassed);
107 
108  /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
109  MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB);
110 
111  //===------------------------------------------------------------------===//
112  // AA metadata.
113  //===------------------------------------------------------------------===//
114 
115 protected:
116  /// Return metadata appropriate for a AA root node (scope or TBAA).
117  /// Each returned node is distinct from all other metadata and will never
118  /// be identified (uniqued) with anything else.
120  MDNode *Extra = nullptr);
121 
122 public:
123  /// Return metadata appropriate for a TBAA root node. Each returned
124  /// node is distinct from all other metadata and will never be identified
125  /// (uniqued) with anything else.
127  return createAnonymousAARoot();
128  }
129 
130  /// Return metadata appropriate for an alias scope domain node.
131  /// Each returned node is distinct from all other metadata and will never
132  /// be identified (uniqued) with anything else.
134  return createAnonymousAARoot(Name);
135  }
136 
137  /// Return metadata appropriate for an alias scope root node.
138  /// Each returned node is distinct from all other metadata and will never
139  /// be identified (uniqued) with anything else.
141  StringRef Name = StringRef()) {
143  }
144 
145  /// Return metadata appropriate for a TBAA root node with the given
146  /// name. This may be identified (uniqued) with other roots with the same
147  /// name.
149 
150  /// Return metadata appropriate for an alias scope domain node with
151  /// the given name. This may be identified (uniqued) with other roots with
152  /// the same name.
154 
155  /// Return metadata appropriate for an alias scope node with
156  /// the given name. This may be identified (uniqued) with other scopes with
157  /// the same name and domain.
159 
160  /// Return metadata for a non-root TBAA node with the given name,
161  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
163  bool isConstant = false);
164 
166  uint64_t Offset;
167  uint64_t Size;
169  TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
170  Offset(Offset), Size(Size), Type(Type) {}
171  };
172 
173  /// Return metadata for a tbaa.struct node with the given
174  /// struct field descriptions.
176 
177  /// Return metadata for a TBAA struct node in the type DAG
178  /// with the given name, a list of pairs (offset, field type in the type DAG).
179  MDNode *
181  ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
182 
183  /// Return metadata for a TBAA scalar type node with the
184  /// given name, an offset and a parent in the TBAA type DAG.
186  uint64_t Offset = 0);
187 
188  /// Return metadata for a TBAA tag node with the given
189  /// base type, access type and offset relative to the base type.
191  uint64_t Offset, bool IsConstant = false);
192 
193  /// Return metadata for a TBAA type node in the TBAA type DAG with the
194  /// given parent type, size in bytes, type identifier and a list of fields.
195  MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
198 
199  /// Return metadata for a TBAA access tag with the given base type,
200  /// final access type, offset of the access relative to the base type, size of
201  /// the access and flag indicating whether the accessed object can be
202  /// considered immutable for the purposes of the TBAA analysis.
204  uint64_t Offset, uint64_t Size,
205  bool IsImmutable = false);
206 
207  /// Return mutable version of the given mutable or immutable TBAA
208  /// access tag.
210 
211  /// Return metadata containing an irreducible loop header weight.
212  MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
213 };
214 
215 } // end namespace llvm
216 
217 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::MDBuilder::TBAAStructField::Offset
uint64_t Offset
Definition: MDBuilder.h:166
llvm
Definition: AllocatorList.h:23
llvm::MDBuilder::createConstant
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
llvm::MDBuilder::createMutableTBAAAccessTag
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:274
llvm::MDBuilder::TBAAStructField::Size
uint64_t Size
Definition: MDBuilder.h:167
llvm::MDBuilder::createTBAAStructTypeNode
MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t >> Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset,...
Definition: MDBuilder.cpp:210
llvm::MDBuilder::createFPMath
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
llvm::MDBuilder::createAliasScopeDomain
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:187
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::Function
Definition: Function.h:61
StringRef.h
llvm::MDBuilder::createPseudoProbeDesc
MDNode * createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, Function *F)
Return metadata containing the pseudo probe descriptor for a function.
Definition: MDBuilder.cpp:309
llvm::MipsISD::Lo
@ Lo
Definition: MipsISelLowering.h:79
llvm::MDBuilder::createAliasScope
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:191
llvm::MDBuilder::createUnpredictable
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
BaseType
llvm::MDBuilder::createAnonymousAliasScope
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition: MDBuilder.h:140
llvm::MDBuilder::MDBuilder
MDBuilder(LLVMContext &context)
Definition: MDBuilder.h:39
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MipsISD::Hi
@ Hi
Definition: MipsISelLowering.h:75
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
GlobalValue.h
Constants.h
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:69
llvm::MDBuilder::TBAAStructField::Type
MDNode * Type
Definition: MDBuilder.h:168
llvm::MDBuilder::createAnonymousTBAARoot
MDNode * createAnonymousTBAARoot()
Return metadata appropriate for a TBAA root node.
Definition: MDBuilder.h:126
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::MDBuilder::createBranchWeights
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
llvm::MDBuilder::createTBAANode
MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree,...
Definition: MDBuilder.cpp:177
Domain
Domain
Definition: CorrelatedValuePropagation.cpp:660
DenseSet.h
llvm::MDBuilder::TBAAStructField::TBAAStructField
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type)
Definition: MDBuilder.h:169
llvm::MDBuilder::createTBAAStructTagNode
MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, bool IsConstant=false)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
Definition: MDBuilder.cpp:233
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::MDBuilder::createAnonymousAliasScopeDomain
MDNode * createAnonymousAliasScopeDomain(StringRef Name=StringRef())
Return metadata appropriate for an alias scope domain node.
Definition: MDBuilder.h:133
llvm::MDBuilder::mergeCallbackEncodings
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:124
llvm::MDBuilder::createString
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
llvm::DenseSet< GlobalValue::GUID >
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
isConstant
static bool isConstant(const MachineInstr &MI)
Definition: AMDGPUInstructionSelector.cpp:2269
llvm::MDBuilder::createTBAAAccessTag
MDNode * createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, uint64_t Size, bool IsImmutable=false)
Return metadata for a TBAA access tag with the given base type, final access type,...
Definition: MDBuilder.cpp:260
llvm::MDBuilder::createCallbackEncoding
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:107
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::ArrayRef< uint32_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::MDBuilder::createFunctionSectionPrefix
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:78
llvm::MDBuilder::createAnonymousAARoot
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:153
llvm::ConstantAsMetadata
Definition: Metadata.h:412
llvm::MDBuilder::createTBAAStructNode
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:197
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MDBuilder::TBAAStructField
Definition: MDBuilder.h:165
Arguments
AMDGPU Lower Kernel Arguments
Definition: AMDGPULowerKernelArguments.cpp:243
llvm::MDBuilder::createTBAAScalarTypeNode
MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
Definition: MDBuilder.cpp:224
llvm::MDBuilder
Definition: MDBuilder.h:35
llvm::MDBuilder::createTBAARoot
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:171
llvm::MDBuilder::createCallees
MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:100
llvm::MDBuilder::createRange
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:84
LineType::Metadata
@ Metadata
DataTypes.h
llvm::MDBuilder::createTBAATypeNode
MDNode * createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id, ArrayRef< TBAAStructField > Fields=ArrayRef< TBAAStructField >())
Return metadata for a TBAA type node in the TBAA type DAG with the given parent type,...
Definition: MDBuilder.cpp:244
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:221
llvm::MDString
A single uniqued string.
Definition: Metadata.h:611
llvm::MDBuilder::createFunctionEntryCount
MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean \Synthetic indicating whether th...
Definition: MDBuilder.cpp:59
llvm::MDBuilder::createIrrLoopHeaderWeight
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:301