1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===// 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 #include "llvm/IR/MDBuilder.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/Metadata.h" 18 using namespace llvm; 19 20 MDString *MDBuilder::createString(StringRef Str) { 21 return MDString::get(Context, Str); 22 } 23 24 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) { 25 return ConstantAsMetadata::get(C); 26 } 27 28 MDNode *MDBuilder::createFPMath(float Accuracy) { 29 if (Accuracy == 0.0) 30 return nullptr; 31 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); 32 auto *Op = 33 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy)); 34 return MDNode::get(Context, Op); 35 } 36 37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, 38 uint32_t FalseWeight) { 39 return createBranchWeights({TrueWeight, FalseWeight}); 40 } 41 42 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) { 43 assert(Weights.size() >= 1 && "Need at least one branch weights!"); 44 45 SmallVector<Metadata *, 4> Vals(Weights.size() + 1); 46 Vals[0] = createString("branch_weights"); 47 48 Type *Int32Ty = Type::getInt32Ty(Context); 49 for (unsigned i = 0, e = Weights.size(); i != e; ++i) 50 Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i])); 51 52 return MDNode::get(Context, Vals); 53 } 54 55 MDNode *MDBuilder::createUnpredictable() { 56 return MDNode::get(Context, None); 57 } 58 59 MDNode *MDBuilder::createFunctionEntryCount( 60 uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) { 61 Type *Int64Ty = Type::getInt64Ty(Context); 62 SmallVector<Metadata *, 8> Ops; 63 Ops.push_back(createString("function_entry_count")); 64 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count))); 65 if (Imports) 66 for (auto ID : *Imports) 67 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID))); 68 return MDNode::get(Context, Ops); 69 } 70 71 MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) { 72 return MDNode::get(Context, 73 {createString("function_section_prefix"), 74 createString(Prefix)}); 75 } 76 77 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { 78 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!"); 79 80 Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); 81 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)); 82 } 83 84 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) { 85 // If the range is everything then it is useless. 86 if (Hi == Lo) 87 return nullptr; 88 89 // Return the range [Lo, Hi). 90 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)}); 91 } 92 93 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { 94 // To ensure uniqueness the root node is self-referential. 95 auto Dummy = MDNode::getTemporary(Context, None); 96 97 SmallVector<Metadata *, 3> Args(1, Dummy.get()); 98 if (Extra) 99 Args.push_back(Extra); 100 if (!Name.empty()) 101 Args.push_back(createString(Name)); 102 MDNode *Root = MDNode::get(Context, Args); 103 104 // At this point we have 105 // !0 = metadata !{} <- dummy 106 // !1 = metadata !{metadata !0} <- root 107 // Replace the dummy operand with the root node itself and delete the dummy. 108 Root->replaceOperandWith(0, Root); 109 110 // We now have 111 // !1 = metadata !{metadata !1} <- self-referential root 112 return Root; 113 } 114 115 MDNode *MDBuilder::createTBAARoot(StringRef Name) { 116 return MDNode::get(Context, createString(Name)); 117 } 118 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 *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, 122 bool isConstant) { 123 if (isConstant) { 124 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); 125 return MDNode::get(Context, 126 {createString(Name), Parent, createConstant(Flags)}); 127 } 128 return MDNode::get(Context, {createString(Name), Parent}); 129 } 130 131 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { 132 return MDNode::get(Context, createString(Name)); 133 } 134 135 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { 136 return MDNode::get(Context, {createString(Name), Domain}); 137 } 138 139 /// \brief Return metadata for a tbaa.struct node with the given 140 /// struct field descriptions. 141 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { 142 SmallVector<Metadata *, 4> Vals(Fields.size() * 3); 143 Type *Int64 = Type::getInt64Ty(Context); 144 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 145 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset)); 146 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size)); 147 Vals[i * 3 + 2] = Fields[i].TBAA; 148 } 149 return MDNode::get(Context, Vals); 150 } 151 152 /// \brief Return metadata for a TBAA struct node in the type DAG 153 /// with the given name, a list of pairs (offset, field type in the type DAG). 154 MDNode *MDBuilder::createTBAAStructTypeNode( 155 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { 156 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1); 157 Type *Int64 = Type::getInt64Ty(Context); 158 Ops[0] = createString(Name); 159 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 160 Ops[i * 2 + 1] = Fields[i].first; 161 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); 162 } 163 return MDNode::get(Context, Ops); 164 } 165 166 /// \brief Return metadata for a TBAA scalar type node with the 167 /// given name, an offset and a parent in the TBAA type DAG. 168 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, 169 uint64_t Offset) { 170 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); 171 return MDNode::get(Context, 172 {createString(Name), Parent, createConstant(Off)}); 173 } 174 175 /// \brief Return metadata for a TBAA tag node with the given 176 /// base type, access type and offset relative to the base type. 177 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, 178 uint64_t Offset, bool IsConstant) { 179 IntegerType *Int64 = Type::getInt64Ty(Context); 180 ConstantInt *Off = ConstantInt::get(Int64, Offset); 181 if (IsConstant) { 182 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off), 183 createConstant(ConstantInt::get(Int64, 1))}); 184 } 185 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)}); 186 } 187