blob: 3a2b1bddf45dba3ad55ecb0d8b11f06e7ff10b6f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- 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// 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
23namespace llvm {
24
25class APInt;
26template <typename T> class ArrayRef;
27class LLVMContext;
28class Constant;
29class ConstantAsMetadata;
30class MDNode;
31class MDString;
32class Metadata;
33
34class MDBuilder {
35 LLVMContext &Context;
36
37public:
38 MDBuilder(LLVMContext &context) : Context(context) {}
39
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040 /// Return the given string as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041 MDString *createString(StringRef Str);
42
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043 /// Return the given constant as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 ConstantAsMetadata *createConstant(Constant *C);
45
46 //===------------------------------------------------------------------===//
47 // FPMath metadata.
48 //===------------------------------------------------------------------===//
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// Return metadata with the given settings. The special value 0.0
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 /// for the Accuracy parameter indicates the default (maximal precision)
52 /// setting.
53 MDNode *createFPMath(float Accuracy);
54
55 //===------------------------------------------------------------------===//
56 // Prof metadata.
57 //===------------------------------------------------------------------===//
58
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 /// Return metadata containing two branch weights.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
61
Andrew Scullcdfcccc2018-10-05 20:58:37 +010062 /// Return metadata containing a number of branch weights.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
64
65 /// Return metadata specifying that a branch or switch is unpredictable.
66 MDNode *createUnpredictable();
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.
76 MDNode *createFunctionSectionPrefix(StringRef Prefix);
77
78 //===------------------------------------------------------------------===//
79 // Range metadata.
80 //===------------------------------------------------------------------===//
81
Andrew Scullcdfcccc2018-10-05 20:58:37 +010082 /// Return metadata describing the range [Lo, Hi).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 MDNode *createRange(const APInt &Lo, const APInt &Hi);
84
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 /// Return metadata describing the range [Lo, Hi).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 MDNode *createRange(Constant *Lo, Constant *Hi);
87
88 //===------------------------------------------------------------------===//
89 // Callees metadata.
90 //===------------------------------------------------------------------===//
91
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 /// Return metadata indicating the possible callees of indirect
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 /// calls.
94 MDNode *createCallees(ArrayRef<Function *> Callees);
95
96 //===------------------------------------------------------------------===//
Andrew Walbran16937d02019-10-22 13:54:20 +010097 // 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 //===------------------------------------------------------------------===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 // AA metadata.
109 //===------------------------------------------------------------------===//
110
111protected:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Return metadata appropriate for a AA root node (scope or TBAA).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 /// Each returned node is distinct from all other metadata and will never
114 /// be identified (uniqued) with anything else.
115 MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
116 MDNode *Extra = nullptr);
117
118public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119 /// Return metadata appropriate for a TBAA root node. Each returned
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 /// node is distinct from all other metadata and will never be identified
121 /// (uniqued) with anything else.
122 MDNode *createAnonymousTBAARoot() {
123 return createAnonymousAARoot();
124 }
125
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100126 /// Return metadata appropriate for an alias scope domain node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127 /// Each returned node is distinct from all other metadata and will never
128 /// be identified (uniqued) with anything else.
129 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
130 return createAnonymousAARoot(Name);
131 }
132
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100133 /// Return metadata appropriate for an alias scope root node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100134 /// Each returned node is distinct from all other metadata and will never
135 /// be identified (uniqued) with anything else.
136 MDNode *createAnonymousAliasScope(MDNode *Domain,
137 StringRef Name = StringRef()) {
138 return createAnonymousAARoot(Name, Domain);
139 }
140
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100141 /// Return metadata appropriate for a TBAA root node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100142 /// name. This may be identified (uniqued) with other roots with the same
143 /// name.
144 MDNode *createTBAARoot(StringRef Name);
145
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146 /// Return metadata appropriate for an alias scope domain node with
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 /// the given name. This may be identified (uniqued) with other roots with
148 /// the same name.
149 MDNode *createAliasScopeDomain(StringRef Name);
150
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 /// Return metadata appropriate for an alias scope node with
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100156 /// Return metadata for a non-root TBAA node with the given name,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
158 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
159 bool isConstant = false);
160
161 struct TBAAStructField {
162 uint64_t Offset;
163 uint64_t Size;
164 MDNode *Type;
165 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
166 Offset(Offset), Size(Size), Type(Type) {}
167 };
168
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169 /// Return metadata for a tbaa.struct node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 /// struct field descriptions.
171 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Return metadata for a TBAA struct node in the type DAG
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 /// with the given name, a list of pairs (offset, field type in the type DAG).
175 MDNode *
176 createTBAAStructTypeNode(StringRef Name,
177 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
178
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179 /// Return metadata for a TBAA scalar type node with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180 /// given name, an offset and a parent in the TBAA type DAG.
181 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
182 uint64_t Offset = 0);
183
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100184 /// Return metadata for a TBAA tag node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100185 /// base type, access type and offset relative to the base type.
186 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
187 uint64_t Offset, bool IsConstant = false);
188
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100189 /// Return metadata for a TBAA type node in the TBAA type DAG with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100190 /// given parent type, size in bytes, type identifier and a list of fields.
191 MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
192 ArrayRef<TBAAStructField> Fields =
193 ArrayRef<TBAAStructField>());
194
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100195 /// Return metadata for a TBAA access tag with the given base type,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100196 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100203 /// Return mutable version of the given mutable or immutable TBAA
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100204 /// access tag.
205 MDNode *createMutableTBAAAccessTag(MDNode *Tag);
206
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100207 /// Return metadata containing an irreducible loop header weight.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
209};
210
211} // end namespace llvm
212
213#endif