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 MDNode *MDBuilder::createFPMath(float Accuracy) { 25 if (Accuracy == 0.0) 26 return nullptr; 27 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); 28 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy); 29 return MDNode::get(Context, Op); 30 } 31 32 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, 33 uint32_t FalseWeight) { 34 uint32_t Weights[] = {TrueWeight, FalseWeight}; 35 return createBranchWeights(Weights); 36 } 37 38 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) { 39 assert(Weights.size() >= 2 && "Need at least two branch weights!"); 40 41 SmallVector<Value *, 4> Vals(Weights.size() + 1); 42 Vals[0] = createString("branch_weights"); 43 44 Type *Int32Ty = Type::getInt32Ty(Context); 45 for (unsigned i = 0, e = Weights.size(); i != e; ++i) 46 Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]); 47 48 return MDNode::get(Context, Vals); 49 } 50 51 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { 52 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!"); 53 // If the range is everything then it is useless. 54 if (Hi == Lo) 55 return nullptr; 56 57 // Return the range [Lo, Hi). 58 Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); 59 Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)}; 60 return MDNode::get(Context, Range); 61 } 62 63 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { 64 // To ensure uniqueness the root node is self-referential. 65 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>()); 66 67 SmallVector<Value *, 3> Args(1, Dummy); 68 if (Extra) 69 Args.push_back(Extra); 70 if (!Name.empty()) 71 Args.push_back(createString(Name)); 72 MDNode *Root = MDNode::get(Context, Args); 73 74 // At this point we have 75 // !0 = metadata !{} <- dummy 76 // !1 = metadata !{metadata !0} <- root 77 // Replace the dummy operand with the root node itself and delete the dummy. 78 Root->replaceOperandWith(0, Root); 79 MDNode::deleteTemporary(Dummy); 80 // We now have 81 // !1 = metadata !{metadata !1} <- self-referential root 82 return Root; 83 } 84 85 MDNode *MDBuilder::createTBAARoot(StringRef Name) { 86 return MDNode::get(Context, createString(Name)); 87 } 88 89 /// \brief Return metadata for a non-root TBAA node with the given name, 90 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. 91 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, 92 bool isConstant) { 93 if (isConstant) { 94 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); 95 Value *Ops[3] = {createString(Name), Parent, Flags}; 96 return MDNode::get(Context, Ops); 97 } else { 98 Value *Ops[2] = {createString(Name), Parent}; 99 return MDNode::get(Context, Ops); 100 } 101 } 102 103 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { 104 return MDNode::get(Context, createString(Name)); 105 } 106 107 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { 108 Value *Ops[2] = { createString(Name), Domain }; 109 return MDNode::get(Context, Ops); 110 } 111 112 /// \brief Return metadata for a tbaa.struct node with the given 113 /// struct field descriptions. 114 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { 115 SmallVector<Value *, 4> Vals(Fields.size() * 3); 116 Type *Int64 = Type::getInt64Ty(Context); 117 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 118 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset); 119 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size); 120 Vals[i * 3 + 2] = Fields[i].TBAA; 121 } 122 return MDNode::get(Context, Vals); 123 } 124 125 /// \brief Return metadata for a TBAA struct node in the type DAG 126 /// with the given name, a list of pairs (offset, field type in the type DAG). 127 MDNode *MDBuilder::createTBAAStructTypeNode( 128 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { 129 SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1); 130 Type *Int64 = Type::getInt64Ty(Context); 131 Ops[0] = createString(Name); 132 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 133 Ops[i * 2 + 1] = Fields[i].first; 134 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second); 135 } 136 return MDNode::get(Context, Ops); 137 } 138 139 /// \brief Return metadata for a TBAA scalar type node with the 140 /// given name, an offset and a parent in the TBAA type DAG. 141 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, 142 uint64_t Offset) { 143 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); 144 Value *Ops[3] = {createString(Name), Parent, Off}; 145 return MDNode::get(Context, Ops); 146 } 147 148 /// \brief Return metadata for a TBAA tag node with the given 149 /// base type, access type and offset relative to the base type. 150 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, 151 uint64_t Offset) { 152 Type *Int64 = Type::getInt64Ty(Context); 153 Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)}; 154 return MDNode::get(Context, Ops); 155 } 156