blob: 174616c7ab1dcd521ada89374e62bde3971633eb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_MDBUILDER_H
16#define LLVM_IR_MDBUILDER_H
17
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/Support/DataTypes.h"
22#include <utility>
23
24namespace llvm {
25
26class APInt;
27template <typename T> class ArrayRef;
28class LLVMContext;
29class Constant;
30class ConstantAsMetadata;
31class MDNode;
32class MDString;
33class Metadata;
34
35class MDBuilder {
36 LLVMContext &Context;
37
38public:
39 MDBuilder(LLVMContext &context) : Context(context) {}
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041 /// Return the given string as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 MDString *createString(StringRef Str);
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 /// Return the given constant as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045 ConstantAsMetadata *createConstant(Constant *C);
46
47 //===------------------------------------------------------------------===//
48 // FPMath metadata.
49 //===------------------------------------------------------------------===//
50
Andrew Scullcdfcccc2018-10-05 20:58:37 +010051 /// Return metadata with the given settings. The special value 0.0
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052 /// for the Accuracy parameter indicates the default (maximal precision)
53 /// setting.
54 MDNode *createFPMath(float Accuracy);
55
56 //===------------------------------------------------------------------===//
57 // Prof metadata.
58 //===------------------------------------------------------------------===//
59
Andrew Scullcdfcccc2018-10-05 20:58:37 +010060 /// Return metadata containing two branch weights.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010061 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
62
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063 /// Return metadata containing a number of branch weights.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
65
66 /// Return metadata specifying that a branch or switch is unpredictable.
67 MDNode *createUnpredictable();
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.
77 MDNode *createFunctionSectionPrefix(StringRef Prefix);
78
79 //===------------------------------------------------------------------===//
80 // Range metadata.
81 //===------------------------------------------------------------------===//
82
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083 /// Return metadata describing the range [Lo, Hi).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084 MDNode *createRange(const APInt &Lo, const APInt &Hi);
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 /// Return metadata describing the range [Lo, Hi).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 MDNode *createRange(Constant *Lo, Constant *Hi);
88
89 //===------------------------------------------------------------------===//
90 // Callees metadata.
91 //===------------------------------------------------------------------===//
92
Andrew Scullcdfcccc2018-10-05 20:58:37 +010093 /// Return metadata indicating the possible callees of indirect
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 /// calls.
95 MDNode *createCallees(ArrayRef<Function *> Callees);
96
97 //===------------------------------------------------------------------===//
98 // AA metadata.
99 //===------------------------------------------------------------------===//
100
101protected:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100102 /// Return metadata appropriate for a AA root node (scope or TBAA).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 /// 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);
107
108public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100109 /// Return metadata appropriate for a TBAA root node. Each returned
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110 /// node is distinct from all other metadata and will never be identified
111 /// (uniqued) with anything else.
112 MDNode *createAnonymousTBAARoot() {
113 return createAnonymousAARoot();
114 }
115
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100116 /// Return metadata appropriate for an alias scope domain node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 /// 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);
121 }
122
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100123 /// Return metadata appropriate for an alias scope root node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100124 /// 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);
129 }
130
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100131 /// Return metadata appropriate for a TBAA root node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132 /// name. This may be identified (uniqued) with other roots with the same
133 /// name.
134 MDNode *createTBAARoot(StringRef Name);
135
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100136 /// Return metadata appropriate for an alias scope domain node with
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100137 /// the given name. This may be identified (uniqued) with other roots with
138 /// the same name.
139 MDNode *createAliasScopeDomain(StringRef Name);
140
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100141 /// Return metadata appropriate for an alias scope node with
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100142 /// 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);
145
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146 /// Return metadata for a non-root TBAA node with the given name,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
148 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
149 bool isConstant = false);
150
151 struct TBAAStructField {
152 uint64_t Offset;
153 uint64_t Size;
154 MDNode *Type;
155 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
156 Offset(Offset), Size(Size), Type(Type) {}
157 };
158
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100159 /// Return metadata for a tbaa.struct node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100160 /// struct field descriptions.
161 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
162
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100163 /// Return metadata for a TBAA struct node in the type DAG
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100164 /// with the given name, a list of pairs (offset, field type in the type DAG).
165 MDNode *
166 createTBAAStructTypeNode(StringRef Name,
167 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
168
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169 /// Return metadata for a TBAA scalar type node with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 /// given name, an offset and a parent in the TBAA type DAG.
171 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
172 uint64_t Offset = 0);
173
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100174 /// Return metadata for a TBAA tag node with the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100175 /// 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);
178
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179 /// Return metadata for a TBAA type node in the TBAA type DAG with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180 /// 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>());
184
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100185 /// Return metadata for a TBAA access tag with the given base type,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 /// 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);
192
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100193 /// Return mutable version of the given mutable or immutable TBAA
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100194 /// access tag.
195 MDNode *createMutableTBAAAccessTag(MDNode *Tag);
196
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100197 /// Return metadata containing an irreducible loop header weight.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100198 MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
199};
200
201} // end namespace llvm
202
203#endif