1 //===---- 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 24 namespace llvm { 25 26 class APInt; 27 template <typename T> class ArrayRef; 28 class LLVMContext; 29 class Constant; 30 class ConstantAsMetadata; 31 class MDNode; 32 class MDString; 33 34 class MDBuilder { 35 LLVMContext &Context; 36 37 public: 38 MDBuilder(LLVMContext &context) : Context(context) {} 39 40 /// \brief Return the given string as metadata. 41 MDString *createString(StringRef Str); 42 43 /// \brief Return the given constant as metadata. 44 ConstantAsMetadata *createConstant(Constant *C); 45 46 //===------------------------------------------------------------------===// 47 // FPMath metadata. 48 //===------------------------------------------------------------------===// 49 50 /// \brief Return metadata with the given settings. The special value 0.0 51 /// for the Accuracy parameter indicates the default (maximal precision) 52 /// setting. 53 MDNode *createFPMath(float Accuracy); 54 55 //===------------------------------------------------------------------===// 56 // Prof metadata. 57 //===------------------------------------------------------------------===// 58 59 /// \brief Return metadata containing two branch weights. 60 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight); 61 62 /// \brief Return metadata containing a number of branch weights. 63 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, 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); 73 74 /// Return metadata containing the section prefix for a function. 75 MDNode *createFunctionSectionPrefix(StringRef Prefix); 76 77 //===------------------------------------------------------------------===// 78 // Range metadata. 79 //===------------------------------------------------------------------===// 80 81 /// \brief Return metadata describing the range [Lo, Hi). 82 MDNode *createRange(const APInt &Lo, const APInt &Hi); 83 84 /// \brief Return metadata describing the range [Lo, Hi). 85 MDNode *createRange(Constant *Lo, Constant *Hi); 86 87 //===------------------------------------------------------------------===// 88 // Callees metadata. 89 //===------------------------------------------------------------------===// 90 91 /// \brief Return metadata indicating the possible callees of indirect 92 /// calls. 93 MDNode *createCallees(ArrayRef<Function *> Callees); 94 95 //===------------------------------------------------------------------===// 96 // AA metadata. 97 //===------------------------------------------------------------------===// 98 99 protected: 100 /// \brief Return metadata appropriate for a AA root node (scope or TBAA). 101 /// Each returned node is distinct from all other metadata and will never 102 /// be identified (uniqued) with anything else. 103 MDNode *createAnonymousAARoot(StringRef Name = StringRef(), 104 MDNode *Extra = nullptr); 105 106 public: 107 /// \brief Return metadata appropriate for a TBAA root node. Each returned 108 /// node is distinct from all other metadata and will never be identified 109 /// (uniqued) with anything else. 110 MDNode *createAnonymousTBAARoot() { 111 return createAnonymousAARoot(); 112 } 113 114 /// \brief Return metadata appropriate for an alias scope domain node. 115 /// Each returned node is distinct from all other metadata and will never 116 /// be identified (uniqued) with anything else. 117 MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) { 118 return createAnonymousAARoot(Name); 119 } 120 121 /// \brief Return metadata appropriate for an alias scope root node. 122 /// Each returned node is distinct from all other metadata and will never 123 /// be identified (uniqued) with anything else. 124 MDNode *createAnonymousAliasScope(MDNode *Domain, 125 StringRef Name = StringRef()) { 126 return createAnonymousAARoot(Name, Domain); 127 } 128 129 /// \brief Return metadata appropriate for a TBAA root node with the given 130 /// name. This may be identified (uniqued) with other roots with the same 131 /// name. 132 MDNode *createTBAARoot(StringRef Name); 133 134 /// \brief Return metadata appropriate for an alias scope domain node with 135 /// the given name. This may be identified (uniqued) with other roots with 136 /// the same name. 137 MDNode *createAliasScopeDomain(StringRef Name); 138 139 /// \brief Return metadata appropriate for an alias scope node with 140 /// the given name. This may be identified (uniqued) with other scopes with 141 /// the same name and domain. 142 MDNode *createAliasScope(StringRef Name, MDNode *Domain); 143 144 /// \brief Return metadata for a non-root TBAA node with the given name, 145 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. 146 MDNode *createTBAANode(StringRef Name, MDNode *Parent, 147 bool isConstant = false); 148 149 struct TBAAStructField { 150 uint64_t Offset; 151 uint64_t Size; 152 MDNode *TBAA; 153 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) : 154 Offset(Offset), Size(Size), TBAA(TBAA) {} 155 }; 156 157 /// \brief Return metadata for a tbaa.struct node with the given 158 /// struct field descriptions. 159 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields); 160 161 /// \brief Return metadata for a TBAA struct node in the type DAG 162 /// with the given name, a list of pairs (offset, field type in the type DAG). 163 MDNode * 164 createTBAAStructTypeNode(StringRef Name, 165 ArrayRef<std::pair<MDNode *, uint64_t>> Fields); 166 167 /// \brief Return metadata for a TBAA scalar type node with the 168 /// given name, an offset and a parent in the TBAA type DAG. 169 MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, 170 uint64_t Offset = 0); 171 172 /// \brief Return metadata for a TBAA tag node with the given 173 /// base type, access type and offset relative to the base type. 174 MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, 175 uint64_t Offset, bool IsConstant = false); 176 }; 177 178 } // end namespace llvm 179 180 #endif 181