LLVM  9.0.0svn
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/GlobalValue.h"
20 #include "llvm/Support/DataTypes.h"
21 #include <utility>
22 
23 namespace llvm {
24 
25 class APInt;
26 template <typename T> class ArrayRef;
27 class LLVMContext;
28 class Constant;
29 class ConstantAsMetadata;
30 class MDNode;
31 class MDString;
32 class Metadata;
33 
34 class MDBuilder {
35  LLVMContext &Context;
36 
37 public:
38  MDBuilder(LLVMContext &context) : Context(context) {}
39 
40  /// Return the given string as metadata.
42 
43  /// Return the given constant as metadata.
45 
46  //===------------------------------------------------------------------===//
47  // FPMath metadata.
48  //===------------------------------------------------------------------===//
49 
50  /// Return metadata with the given settings. The special value 0.0
51  /// for the Accuracy parameter indicates the default (maximal precision)
52  /// setting.
53  MDNode *createFPMath(float Accuracy);
54 
55  //===------------------------------------------------------------------===//
56  // Prof metadata.
57  //===------------------------------------------------------------------===//
58 
59  /// Return metadata containing two branch weights.
60  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
61 
62  /// Return metadata containing a number of branch weights.
64 
65  /// Return metadata specifying that a branch or switch is unpredictable.
67 
68  /// Return metadata containing the entry \p Count for a function, a boolean
69  /// \Synthetic indicating whether the counts were synthetized, and the
70  /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
71  /// enable the same inlines as the profiled optimized binary
72  MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
73  const DenseSet<GlobalValue::GUID> *Imports);
74 
75  /// Return metadata containing the section prefix for a function.
77 
78  //===------------------------------------------------------------------===//
79  // Range metadata.
80  //===------------------------------------------------------------------===//
81 
82  /// Return metadata describing the range [Lo, Hi).
83  MDNode *createRange(const APInt &Lo, const APInt &Hi);
84 
85  /// Return metadata describing the range [Lo, Hi).
87 
88  //===------------------------------------------------------------------===//
89  // Callees metadata.
90  //===------------------------------------------------------------------===//
91 
92  /// Return metadata indicating the possible callees of indirect
93  /// calls.
95 
96  //===------------------------------------------------------------------===//
97  // Callback metadata.
98  //===------------------------------------------------------------------===//
99 
100  /// Return metadata describing a callback (see llvm::AbstractCallSite).
101  MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments,
102  bool VarArgsArePassed);
103 
104  /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
105  MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB);
106 
107  //===------------------------------------------------------------------===//
108  // AA metadata.
109  //===------------------------------------------------------------------===//
110 
111 protected:
112  /// Return metadata appropriate for a AA root node (scope or TBAA).
113  /// Each returned node is distinct from all other metadata and will never
114  /// be identified (uniqued) with anything else.
116  MDNode *Extra = nullptr);
117 
118 public:
119  /// Return metadata appropriate for a TBAA root node. Each returned
120  /// node is distinct from all other metadata and will never be identified
121  /// (uniqued) with anything else.
123  return createAnonymousAARoot();
124  }
125 
126  /// Return metadata appropriate for an alias scope domain node.
127  /// Each returned node is distinct from all other metadata and will never
128  /// be identified (uniqued) with anything else.
130  return createAnonymousAARoot(Name);
131  }
132 
133  /// Return metadata appropriate for an alias scope root node.
134  /// Each returned node is distinct from all other metadata and will never
135  /// be identified (uniqued) with anything else.
137  StringRef Name = StringRef()) {
138  return createAnonymousAARoot(Name, Domain);
139  }
140 
141  /// Return metadata appropriate for a TBAA root node with the given
142  /// name. This may be identified (uniqued) with other roots with the same
143  /// name.
145 
146  /// Return metadata appropriate for an alias scope domain node with
147  /// the given name. This may be identified (uniqued) with other roots with
148  /// the same name.
150 
151  /// Return metadata appropriate for an alias scope node with
152  /// the given name. This may be identified (uniqued) with other scopes with
153  /// the same name and domain.
154  MDNode *createAliasScope(StringRef Name, MDNode *Domain);
155 
156  /// Return metadata for a non-root TBAA node with the given name,
157  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
158  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
159  bool isConstant = false);
160 
162  uint64_t Offset;
163  uint64_t Size;
165  TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
166  Offset(Offset), Size(Size), Type(Type) {}
167  };
168 
169  /// Return metadata for a tbaa.struct node with the given
170  /// struct field descriptions.
172 
173  /// Return metadata for a TBAA struct node in the type DAG
174  /// with the given name, a list of pairs (offset, field type in the type DAG).
175  MDNode *
177  ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
178 
179  /// Return metadata for a TBAA scalar type node with the
180  /// given name, an offset and a parent in the TBAA type DAG.
182  uint64_t Offset = 0);
183 
184  /// Return metadata for a TBAA tag node with the given
185  /// base type, access type and offset relative to the base type.
187  uint64_t Offset, bool IsConstant = false);
188 
189  /// Return metadata for a TBAA type node in the TBAA type DAG with the
190  /// given parent type, size in bytes, type identifier and a list of fields.
191  MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
194 
195  /// Return metadata for a TBAA access tag with the given base type,
196  /// final access type, offset of the access relative to the base type, size of
197  /// the access and flag indicating whether the accessed object can be
198  /// considered immutable for the purposes of the TBAA analysis.
199  MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
200  uint64_t Offset, uint64_t Size,
201  bool IsImmutable = false);
202 
203  /// Return mutable version of the given mutable or immutable TBAA
204  /// access tag.
206 
207  /// Return metadata containing an irreducible loop header weight.
208  MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
209 };
210 
211 } // end namespace llvm
212 
213 #endif
uint64_t CallInst * C
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:86
static bool isConstant(const MachineInstr &MI)
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:155
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
MDNode * createAnonymousTBAARoot()
Return metadata appropriate for a TBAA root node.
Definition: MDBuilder.h:122
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition: MDBuilder.h:136
MDBuilder(LLVMContext &context)
Definition: MDBuilder.h:38
Metadata node.
Definition: Metadata.h:863
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:307
MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean indicating whether the counts w...
Definition: MDBuilder.cpp:59
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, offset of the access relative to the base type, size of the access and flag indicating whether the accessed object can be considered immutable for the purposes of the TBAA analysis.
Definition: MDBuilder.cpp:266
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, size in bytes, type identifier and a list of fields.
Definition: MDBuilder.cpp:250
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
This is an important base class in LLVM.
Definition: Constant.h:41
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:109
AMDGPU Lower Kernel Arguments
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:193
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:203
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
MDNode * createAnonymousAliasScopeDomain(StringRef Name=StringRef())
Return metadata appropriate for an alias scope domain node.
Definition: MDBuilder.h:129
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:230
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:126
Class for arbitrary precision integers.
Definition: APInt.h:69
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type)
Definition: MDBuilder.h:165
MDNode * createCallees(ArrayRef< Function *> Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:102
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:216
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:197
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:239
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, and value for &#39;pointsToConstantMemory&#39;.
Definition: MDBuilder.cpp:183
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:80
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:177
A single uniqued string.
Definition: Metadata.h:603
Root of the metadata hierarchy.
Definition: Metadata.h:57
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:280