Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 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" |
Matthew Simpson | 36bbc8c | 2017-10-16 22:22:11 +0000 | [diff] [blame^] | 17 | #include "llvm/IR/Function.h" |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Metadata.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | MDString *MDBuilder::createString(StringRef Str) { |
| 22 | return MDString::get(Context, Str); |
| 23 | } |
| 24 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 25 | ConstantAsMetadata *MDBuilder::createConstant(Constant *C) { |
| 26 | return ConstantAsMetadata::get(C); |
| 27 | } |
| 28 | |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 29 | MDNode *MDBuilder::createFPMath(float Accuracy) { |
| 30 | if (Accuracy == 0.0) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 31 | return nullptr; |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 32 | assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 33 | auto *Op = |
| 34 | createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy)); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 35 | return MDNode::get(Context, Op); |
| 36 | } |
| 37 | |
| 38 | MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, |
| 39 | uint32_t FalseWeight) { |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 40 | return createBranchWeights({TrueWeight, FalseWeight}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) { |
Dehao Chen | 71021cd | 2016-07-11 17:36:02 +0000 | [diff] [blame] | 44 | assert(Weights.size() >= 1 && "Need at least one branch weights!"); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 45 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 46 | SmallVector<Metadata *, 4> Vals(Weights.size() + 1); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 47 | Vals[0] = createString("branch_weights"); |
| 48 | |
| 49 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 50 | for (unsigned i = 0, e = Weights.size(); i != e; ++i) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 51 | Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i])); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 52 | |
| 53 | return MDNode::get(Context, Vals); |
| 54 | } |
| 55 | |
Sanjay Patel | a99ab1f | 2015-09-02 19:06:43 +0000 | [diff] [blame] | 56 | MDNode *MDBuilder::createUnpredictable() { |
| 57 | return MDNode::get(Context, None); |
| 58 | } |
| 59 | |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 60 | MDNode *MDBuilder::createFunctionEntryCount( |
| 61 | uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) { |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 62 | Type *Int64Ty = Type::getInt64Ty(Context); |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 63 | SmallVector<Metadata *, 8> Ops; |
| 64 | Ops.push_back(createString("function_entry_count")); |
| 65 | Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count))); |
Ana Pazos | 90b1742 | 2017-08-29 17:13:24 +0000 | [diff] [blame] | 66 | if (Imports) { |
| 67 | SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end()); |
| 68 | std::stable_sort(OrderID.begin(), OrderID.end(), |
| 69 | [] (GlobalValue::GUID A, GlobalValue::GUID B) { |
| 70 | return A < B;}); |
| 71 | for (auto ID : OrderID) |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 72 | Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID))); |
Ana Pazos | 90b1742 | 2017-08-29 17:13:24 +0000 | [diff] [blame] | 73 | } |
Dehao Chen | a60cdd3 | 2017-02-28 18:09:44 +0000 | [diff] [blame] | 74 | return MDNode::get(Context, Ops); |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Dehao Chen | 302b69c | 2016-10-18 20:42:47 +0000 | [diff] [blame] | 77 | MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) { |
| 78 | return MDNode::get(Context, |
| 79 | {createString("function_section_prefix"), |
| 80 | createString(Prefix)}); |
| 81 | } |
| 82 | |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 83 | MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { |
| 84 | assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!"); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 85 | |
| 86 | Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); |
| 87 | return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)); |
| 88 | } |
| 89 | |
| 90 | MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) { |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 91 | // If the range is everything then it is useless. |
| 92 | if (Hi == Lo) |
| 93 | return nullptr; |
| 94 | |
| 95 | // Return the range [Lo, Hi). |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 96 | return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Matthew Simpson | 36bbc8c | 2017-10-16 22:22:11 +0000 | [diff] [blame^] | 99 | MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) { |
| 100 | SmallVector<Metadata *, 4> Ops; |
| 101 | for (Function *F : Callees) |
| 102 | Ops.push_back(createConstant(F)); |
| 103 | return MDNode::get(Context, Ops); |
| 104 | } |
| 105 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 106 | MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 107 | // To ensure uniqueness the root node is self-referential. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 108 | auto Dummy = MDNode::getTemporary(Context, None); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 109 | |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 110 | SmallVector<Metadata *, 3> Args(1, Dummy.get()); |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 111 | if (Extra) |
| 112 | Args.push_back(Extra); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 113 | if (!Name.empty()) |
| 114 | Args.push_back(createString(Name)); |
| 115 | MDNode *Root = MDNode::get(Context, Args); |
| 116 | |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 117 | // At this point we have |
| 118 | // !0 = metadata !{} <- dummy |
| 119 | // !1 = metadata !{metadata !0} <- root |
| 120 | // Replace the dummy operand with the root node itself and delete the dummy. |
| 121 | Root->replaceOperandWith(0, Root); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 122 | |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 123 | // We now have |
| 124 | // !1 = metadata !{metadata !1} <- self-referential root |
| 125 | return Root; |
| 126 | } |
| 127 | |
| 128 | MDNode *MDBuilder::createTBAARoot(StringRef Name) { |
| 129 | return MDNode::get(Context, createString(Name)); |
| 130 | } |
| 131 | |
| 132 | /// \brief Return metadata for a non-root TBAA node with the given name, |
| 133 | /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. |
| 134 | MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, |
| 135 | bool isConstant) { |
| 136 | if (isConstant) { |
| 137 | Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 138 | return MDNode::get(Context, |
| 139 | {createString(Name), Parent, createConstant(Flags)}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 140 | } |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 141 | return MDNode::get(Context, {createString(Name), Parent}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 144 | MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 145 | return MDNode::get(Context, createString(Name)); |
| 146 | } |
| 147 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 148 | MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 149 | return MDNode::get(Context, {createString(Name), Domain}); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 152 | /// \brief Return metadata for a tbaa.struct node with the given |
| 153 | /// struct field descriptions. |
| 154 | MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 155 | SmallVector<Metadata *, 4> Vals(Fields.size() * 3); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 156 | Type *Int64 = Type::getInt64Ty(Context); |
| 157 | for (unsigned i = 0, e = Fields.size(); i != e; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 158 | Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset)); |
| 159 | Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size)); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 160 | Vals[i * 3 + 2] = Fields[i].TBAA; |
| 161 | } |
| 162 | return MDNode::get(Context, Vals); |
| 163 | } |
| 164 | |
| 165 | /// \brief Return metadata for a TBAA struct node in the type DAG |
| 166 | /// with the given name, a list of pairs (offset, field type in the type DAG). |
| 167 | MDNode *MDBuilder::createTBAAStructTypeNode( |
| 168 | StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 169 | SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 170 | Type *Int64 = Type::getInt64Ty(Context); |
| 171 | Ops[0] = createString(Name); |
| 172 | for (unsigned i = 0, e = Fields.size(); i != e; ++i) { |
| 173 | Ops[i * 2 + 1] = Fields[i].first; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 174 | Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 175 | } |
| 176 | return MDNode::get(Context, Ops); |
| 177 | } |
| 178 | |
| 179 | /// \brief Return metadata for a TBAA scalar type node with the |
| 180 | /// given name, an offset and a parent in the TBAA type DAG. |
| 181 | MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, |
| 182 | uint64_t Offset) { |
| 183 | ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 184 | return MDNode::get(Context, |
| 185 | {createString(Name), Parent, createConstant(Off)}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /// \brief Return metadata for a TBAA tag node with the given |
| 189 | /// base type, access type and offset relative to the base type. |
| 190 | MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, |
Artur Pilipenko | a82f8db | 2015-06-01 14:53:55 +0000 | [diff] [blame] | 191 | uint64_t Offset, bool IsConstant) { |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 192 | IntegerType *Int64 = Type::getInt64Ty(Context); |
| 193 | ConstantInt *Off = ConstantInt::get(Int64, Offset); |
Artur Pilipenko | a82f8db | 2015-06-01 14:53:55 +0000 | [diff] [blame] | 194 | if (IsConstant) { |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 195 | return MDNode::get(Context, {BaseType, AccessType, createConstant(Off), |
| 196 | createConstant(ConstantInt::get(Int64, 1))}); |
Artur Pilipenko | a82f8db | 2015-06-01 14:53:55 +0000 | [diff] [blame] | 197 | } |
Benjamin Kramer | 0969a2a | 2015-11-22 18:03:17 +0000 | [diff] [blame] | 198 | return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)}); |
Benjamin Kramer | 6b841f1 | 2014-04-12 14:26:59 +0000 | [diff] [blame] | 199 | } |