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;
38 MDBuilder(LLVMContext &context) : Context(context) {}
40 /// \brief Return the given string as metadata.
41 MDString *createString(StringRef Str);
43 /// \brief Return the given constant as metadata.
44 ConstantAsMetadata *createConstant(Constant *C);
46 //===------------------------------------------------------------------===//
48 //===------------------------------------------------------------------===//
50 /// \brief Return metadata with the given settings. The special value 0.0
51 /// for the Accuracy parameter indicates the default (maximal precision)
53 MDNode *createFPMath(float Accuracy);
55 //===------------------------------------------------------------------===//
57 //===------------------------------------------------------------------===//
59 /// \brief Return metadata containing two branch weights.
60 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
62 /// \brief Return metadata containing a number of branch weights.
63 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
65 /// Return metadata specifying that a branch or switch is unpredictable.
66 MDNode *createUnpredictable();
68 /// Return metadata containing the entry \p Count for a function, and the
69 /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
70 /// enable the same inlines as the profiled optimized binary
71 MDNode *createFunctionEntryCount(uint64_t Count,
72 const DenseSet<GlobalValue::GUID> *Imports);
74 /// Return metadata containing the section prefix for a function.
75 MDNode *createFunctionSectionPrefix(StringRef Prefix);
77 //===------------------------------------------------------------------===//
79 //===------------------------------------------------------------------===//
81 /// \brief Return metadata describing the range [Lo, Hi).
82 MDNode *createRange(const APInt &Lo, const APInt &Hi);
84 /// \brief Return metadata describing the range [Lo, Hi).
85 MDNode *createRange(Constant *Lo, Constant *Hi);
87 //===------------------------------------------------------------------===//
89 //===------------------------------------------------------------------===//
92 /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
93 /// Each returned node is distinct from all other metadata and will never
94 /// be identified (uniqued) with anything else.
95 MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
96 MDNode *Extra = nullptr);
99 /// \brief Return metadata appropriate for a TBAA root node. Each returned
100 /// node is distinct from all other metadata and will never be identified
101 /// (uniqued) with anything else.
102 MDNode *createAnonymousTBAARoot() {
103 return createAnonymousAARoot();
106 /// \brief Return metadata appropriate for an alias scope domain node.
107 /// Each returned node is distinct from all other metadata and will never
108 /// be identified (uniqued) with anything else.
109 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
110 return createAnonymousAARoot(Name);
113 /// \brief Return metadata appropriate for an alias scope root node.
114 /// Each returned node is distinct from all other metadata and will never
115 /// be identified (uniqued) with anything else.
116 MDNode *createAnonymousAliasScope(MDNode *Domain,
117 StringRef Name = StringRef()) {
118 return createAnonymousAARoot(Name, Domain);
121 /// \brief Return metadata appropriate for a TBAA root node with the given
122 /// name. This may be identified (uniqued) with other roots with the same
124 MDNode *createTBAARoot(StringRef Name);
126 /// \brief Return metadata appropriate for an alias scope domain node with
127 /// the given name. This may be identified (uniqued) with other roots with
129 MDNode *createAliasScopeDomain(StringRef Name);
131 /// \brief Return metadata appropriate for an alias scope node with
132 /// the given name. This may be identified (uniqued) with other scopes with
133 /// the same name and domain.
134 MDNode *createAliasScope(StringRef Name, MDNode *Domain);
136 /// \brief Return metadata for a non-root TBAA node with the given name,
137 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
138 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
139 bool isConstant = false);
141 struct TBAAStructField {
145 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
146 Offset(Offset), Size(Size), TBAA(TBAA) {}
149 /// \brief Return metadata for a tbaa.struct node with the given
150 /// struct field descriptions.
151 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
153 /// \brief Return metadata for a TBAA struct node in the type DAG
154 /// with the given name, a list of pairs (offset, field type in the type DAG).
156 createTBAAStructTypeNode(StringRef Name,
157 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
159 /// \brief Return metadata for a TBAA scalar type node with the
160 /// given name, an offset and a parent in the TBAA type DAG.
161 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
162 uint64_t Offset = 0);
164 /// \brief Return metadata for a TBAA tag node with the given
165 /// base type, access type and offset relative to the base type.
166 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
167 uint64_t Offset, bool IsConstant = false);
170 } // end namespace llvm