1 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MDBuilder class, which is used as a convenient way to
11 // create LLVM metadata with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_MDBUILDER_H
16 #define LLVM_IR_MDBUILDER_H
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Support/DataTypes.h"
27 template <typename T> class ArrayRef;
30 class ConstantAsMetadata;
39 MDBuilder(LLVMContext &context) : Context(context) {}
41 /// Return the given string as metadata.
42 MDString *createString(StringRef Str);
44 /// Return the given constant as metadata.
45 ConstantAsMetadata *createConstant(Constant *C);
47 //===------------------------------------------------------------------===//
49 //===------------------------------------------------------------------===//
51 /// Return metadata with the given settings. The special value 0.0
52 /// for the Accuracy parameter indicates the default (maximal precision)
54 MDNode *createFPMath(float Accuracy);
56 //===------------------------------------------------------------------===//
58 //===------------------------------------------------------------------===//
60 /// Return metadata containing two branch weights.
61 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
63 /// Return metadata containing a number of branch weights.
64 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
66 /// Return metadata specifying that a branch or switch is unpredictable.
67 MDNode *createUnpredictable();
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);
76 /// Return metadata containing the section prefix for a function.
77 MDNode *createFunctionSectionPrefix(StringRef Prefix);
79 //===------------------------------------------------------------------===//
81 //===------------------------------------------------------------------===//
83 /// Return metadata describing the range [Lo, Hi).
84 MDNode *createRange(const APInt &Lo, const APInt &Hi);
86 /// Return metadata describing the range [Lo, Hi).
87 MDNode *createRange(Constant *Lo, Constant *Hi);
89 //===------------------------------------------------------------------===//
91 //===------------------------------------------------------------------===//
93 /// Return metadata indicating the possible callees of indirect
95 MDNode *createCallees(ArrayRef<Function *> Callees);
97 //===------------------------------------------------------------------===//
99 //===------------------------------------------------------------------===//
102 /// Return metadata appropriate for a AA root node (scope or TBAA).
103 /// Each returned node is distinct from all other metadata and will never
104 /// be identified (uniqued) with anything else.
105 MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
106 MDNode *Extra = nullptr);
109 /// Return metadata appropriate for a TBAA root node. Each returned
110 /// node is distinct from all other metadata and will never be identified
111 /// (uniqued) with anything else.
112 MDNode *createAnonymousTBAARoot() {
113 return createAnonymousAARoot();
116 /// Return metadata appropriate for an alias scope domain node.
117 /// Each returned node is distinct from all other metadata and will never
118 /// be identified (uniqued) with anything else.
119 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
120 return createAnonymousAARoot(Name);
123 /// Return metadata appropriate for an alias scope root node.
124 /// Each returned node is distinct from all other metadata and will never
125 /// be identified (uniqued) with anything else.
126 MDNode *createAnonymousAliasScope(MDNode *Domain,
127 StringRef Name = StringRef()) {
128 return createAnonymousAARoot(Name, Domain);
131 /// Return metadata appropriate for a TBAA root node with the given
132 /// name. This may be identified (uniqued) with other roots with the same
134 MDNode *createTBAARoot(StringRef Name);
136 /// Return metadata appropriate for an alias scope domain node with
137 /// the given name. This may be identified (uniqued) with other roots with
139 MDNode *createAliasScopeDomain(StringRef Name);
141 /// Return metadata appropriate for an alias scope node with
142 /// the given name. This may be identified (uniqued) with other scopes with
143 /// the same name and domain.
144 MDNode *createAliasScope(StringRef Name, MDNode *Domain);
146 /// Return metadata for a non-root TBAA node with the given name,
147 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
148 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
149 bool isConstant = false);
151 struct TBAAStructField {
155 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
156 Offset(Offset), Size(Size), Type(Type) {}
159 /// Return metadata for a tbaa.struct node with the given
160 /// struct field descriptions.
161 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
163 /// Return metadata for a TBAA struct node in the type DAG
164 /// with the given name, a list of pairs (offset, field type in the type DAG).
166 createTBAAStructTypeNode(StringRef Name,
167 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
169 /// Return metadata for a TBAA scalar type node with the
170 /// given name, an offset and a parent in the TBAA type DAG.
171 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
172 uint64_t Offset = 0);
174 /// Return metadata for a TBAA tag node with the given
175 /// base type, access type and offset relative to the base type.
176 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
177 uint64_t Offset, bool IsConstant = false);
179 /// Return metadata for a TBAA type node in the TBAA type DAG with the
180 /// given parent type, size in bytes, type identifier and a list of fields.
181 MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
182 ArrayRef<TBAAStructField> Fields =
183 ArrayRef<TBAAStructField>());
185 /// Return metadata for a TBAA access tag with the given base type,
186 /// final access type, offset of the access relative to the base type, size of
187 /// the access and flag indicating whether the accessed object can be
188 /// considered immutable for the purposes of the TBAA analysis.
189 MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
190 uint64_t Offset, uint64_t Size,
191 bool IsImmutable = false);
193 /// Return mutable version of the given mutable or immutable TBAA
195 MDNode *createMutableTBAAAccessTag(MDNode *Tag);
197 /// Return metadata containing an irreducible loop header weight.
198 MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
201 } // end namespace llvm