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/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
25 template <typename T> class ArrayRef;
28 class ConstantAsMetadata;
36 MDBuilder(LLVMContext &context) : Context(context) {}
38 /// \brief Return the given string as metadata.
39 MDString *createString(StringRef Str);
41 /// \brief Return the given constant as metadata.
42 ConstantAsMetadata *createConstant(Constant *C);
44 //===------------------------------------------------------------------===//
46 //===------------------------------------------------------------------===//
48 /// \brief Return metadata with the given settings. The special value 0.0
49 /// for the Accuracy parameter indicates the default (maximal precision)
51 MDNode *createFPMath(float Accuracy);
53 //===------------------------------------------------------------------===//
55 //===------------------------------------------------------------------===//
57 /// \brief Return metadata containing two branch weights.
58 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
60 /// \brief Return metadata containing a number of branch weights.
61 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
63 //===------------------------------------------------------------------===//
65 //===------------------------------------------------------------------===//
67 /// \brief Return metadata describing the range [Lo, Hi).
68 MDNode *createRange(const APInt &Lo, const APInt &Hi);
70 //===------------------------------------------------------------------===//
72 //===------------------------------------------------------------------===//
75 /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
76 /// Each returned node is distinct from all other metadata and will never
77 /// be identified (uniqued) with anything else.
78 MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
79 MDNode *Extra = nullptr);
82 /// \brief Return metadata appropriate for a TBAA root node. Each returned
83 /// node is distinct from all other metadata and will never be identified
84 /// (uniqued) with anything else.
85 MDNode *createAnonymousTBAARoot() {
86 return createAnonymousAARoot();
89 /// \brief Return metadata appropriate for an alias scope domain node.
90 /// Each returned node is distinct from all other metadata and will never
91 /// be identified (uniqued) with anything else.
92 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
93 return createAnonymousAARoot(Name);
96 /// \brief Return metadata appropriate for an alias scope root node.
97 /// Each returned node is distinct from all other metadata and will never
98 /// be identified (uniqued) with anything else.
99 MDNode *createAnonymousAliasScope(MDNode *Domain,
100 StringRef Name = StringRef()) {
101 return createAnonymousAARoot(Name, Domain);
104 /// \brief Return metadata appropriate for a TBAA root node with the given
105 /// name. This may be identified (uniqued) with other roots with the same
107 MDNode *createTBAARoot(StringRef Name);
109 /// \brief Return metadata appropriate for an alias scope domain node with
110 /// the given name. This may be identified (uniqued) with other roots with
112 MDNode *createAliasScopeDomain(StringRef Name);
114 /// \brief Return metadata appropriate for an alias scope node with
115 /// the given name. This may be identified (uniqued) with other scopes with
116 /// the same name and domain.
117 MDNode *createAliasScope(StringRef Name, MDNode *Domain);
119 /// \brief Return metadata for a non-root TBAA node with the given name,
120 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
121 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
122 bool isConstant = false);
124 struct TBAAStructField {
128 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
129 Offset(Offset), Size(Size), TBAA(TBAA) {}
132 /// \brief Return metadata for a tbaa.struct node with the given
133 /// struct field descriptions.
134 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
136 /// \brief Return metadata for a TBAA struct node in the type DAG
137 /// with the given name, a list of pairs (offset, field type in the type DAG).
139 createTBAAStructTypeNode(StringRef Name,
140 ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
142 /// \brief Return metadata for a TBAA scalar type node with the
143 /// given name, an offset and a parent in the TBAA type DAG.
144 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
145 uint64_t Offset = 0);
147 /// \brief Return metadata for a TBAA tag node with the given
148 /// base type, access type and offset relative to the base type.
149 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
153 } // end namespace llvm