Duncan P. N. Exon Smith | 71db642 | 2015-02-02 18:20:15 +0000 | [diff] [blame] | 1 | //===- Metadata.cpp - Implement Metadata classes --------------------------===// |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 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 implements the Metadata classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Metadata.h" |
Chris Lattner | 1300f45 | 2009-12-28 08:24:16 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 16 | #include "MetadataImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "SymbolTableListTraitsImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
David Majnemer | fa0f1e6 | 2016-08-16 18:48:34 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SetVector.h" |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ConstantRange.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instruction.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
| 26 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 27 | #include "llvm/IR/ValueHandle.h" |
Duncan P. N. Exon Smith | 46d91ad | 2014-11-14 18:42:06 +0000 | [diff] [blame] | 28 | |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 31 | MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD) |
| 32 | : Value(Ty, MetadataAsValueVal), MD(MD) { |
| 33 | track(); |
| 34 | } |
| 35 | |
| 36 | MetadataAsValue::~MetadataAsValue() { |
| 37 | getType()->getContext().pImpl->MetadataAsValues.erase(MD); |
| 38 | untrack(); |
| 39 | } |
| 40 | |
Sanjay Patel | 9da9c76 | 2016-03-12 20:44:58 +0000 | [diff] [blame] | 41 | /// Canonicalize metadata arguments to intrinsics. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 42 | /// |
| 43 | /// To support bitcode upgrades (and assembly semantic sugar) for \a |
| 44 | /// MetadataAsValue, we need to canonicalize certain metadata. |
| 45 | /// |
| 46 | /// - nullptr is replaced by an empty MDNode. |
| 47 | /// - An MDNode with a single null operand is replaced by an empty MDNode. |
| 48 | /// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped. |
| 49 | /// |
| 50 | /// This maintains readability of bitcode from when metadata was a type of |
| 51 | /// value, and these bridges were unnecessary. |
| 52 | static Metadata *canonicalizeMetadataForValue(LLVMContext &Context, |
| 53 | Metadata *MD) { |
| 54 | if (!MD) |
| 55 | // !{} |
| 56 | return MDNode::get(Context, None); |
| 57 | |
| 58 | // Return early if this isn't a single-operand MDNode. |
| 59 | auto *N = dyn_cast<MDNode>(MD); |
| 60 | if (!N || N->getNumOperands() != 1) |
| 61 | return MD; |
| 62 | |
| 63 | if (!N->getOperand(0)) |
| 64 | // !{} |
| 65 | return MDNode::get(Context, None); |
| 66 | |
| 67 | if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0))) |
| 68 | // Look through the MDNode. |
| 69 | return C; |
| 70 | |
| 71 | return MD; |
| 72 | } |
| 73 | |
| 74 | MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) { |
| 75 | MD = canonicalizeMetadataForValue(Context, MD); |
| 76 | auto *&Entry = Context.pImpl->MetadataAsValues[MD]; |
| 77 | if (!Entry) |
| 78 | Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD); |
| 79 | return Entry; |
| 80 | } |
| 81 | |
| 82 | MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context, |
| 83 | Metadata *MD) { |
| 84 | MD = canonicalizeMetadataForValue(Context, MD); |
| 85 | auto &Store = Context.pImpl->MetadataAsValues; |
Benjamin Kramer | 4c1f097 | 2015-02-08 21:56:09 +0000 | [diff] [blame] | 86 | return Store.lookup(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void MetadataAsValue::handleChangedMetadata(Metadata *MD) { |
| 90 | LLVMContext &Context = getContext(); |
| 91 | MD = canonicalizeMetadataForValue(Context, MD); |
| 92 | auto &Store = Context.pImpl->MetadataAsValues; |
| 93 | |
| 94 | // Stop tracking the old metadata. |
| 95 | Store.erase(this->MD); |
| 96 | untrack(); |
| 97 | this->MD = nullptr; |
| 98 | |
| 99 | // Start tracking MD, or RAUW if necessary. |
| 100 | auto *&Entry = Store[MD]; |
| 101 | if (Entry) { |
| 102 | replaceAllUsesWith(Entry); |
| 103 | delete this; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | this->MD = MD; |
| 108 | track(); |
| 109 | Entry = this; |
| 110 | } |
| 111 | |
| 112 | void MetadataAsValue::track() { |
| 113 | if (MD) |
| 114 | MetadataTracking::track(&MD, *MD, *this); |
| 115 | } |
| 116 | |
| 117 | void MetadataAsValue::untrack() { |
| 118 | if (MD) |
| 119 | MetadataTracking::untrack(MD); |
| 120 | } |
| 121 | |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 122 | bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) { |
| 123 | assert(Ref && "Expected live reference"); |
| 124 | assert((Owner || *static_cast<Metadata **>(Ref) == &MD) && |
| 125 | "Reference without owner must be direct"); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 126 | if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) { |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 127 | R->addRef(Ref, Owner); |
| 128 | return true; |
| 129 | } |
Duncan P. N. Exon Smith | 4b1bc64 | 2016-04-23 04:15:56 +0000 | [diff] [blame] | 130 | if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) { |
| 131 | assert(!PH->Use && "Placeholders can only be used once"); |
| 132 | assert(!Owner && "Unexpected callback to owner"); |
| 133 | PH->Use = static_cast<Metadata **>(Ref); |
| 134 | return true; |
| 135 | } |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | void MetadataTracking::untrack(void *Ref, Metadata &MD) { |
| 140 | assert(Ref && "Expected live reference"); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 141 | if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 142 | R->dropRef(Ref); |
Duncan P. N. Exon Smith | 4b1bc64 | 2016-04-23 04:15:56 +0000 | [diff] [blame] | 143 | else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) |
| 144 | PH->Use = nullptr; |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) { |
| 148 | assert(Ref && "Expected live reference"); |
| 149 | assert(New && "Expected live reference"); |
| 150 | assert(Ref != New && "Expected change"); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 151 | if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) { |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 152 | R->moveRef(Ref, New, MD); |
| 153 | return true; |
| 154 | } |
Duncan P. N. Exon Smith | 4b1bc64 | 2016-04-23 04:15:56 +0000 | [diff] [blame] | 155 | assert(!isa<DistinctMDOperandPlaceholder>(MD) && |
| 156 | "Unexpected move of an MDOperand"); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 157 | assert(!isReplaceable(MD) && |
| 158 | "Expected un-replaceable metadata, since we didn't move a reference"); |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | bool MetadataTracking::isReplaceable(const Metadata &MD) { |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 163 | return ReplaceableMetadataImpl::isReplaceable(MD); |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 166 | void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 167 | bool WasInserted = |
| 168 | UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex))) |
| 169 | .second; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 170 | (void)WasInserted; |
| 171 | assert(WasInserted && "Expected to add a reference"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 172 | |
| 173 | ++NextIndex; |
| 174 | assert(NextIndex != 0 && "Unexpected overflow"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void ReplaceableMetadataImpl::dropRef(void *Ref) { |
| 178 | bool WasErased = UseMap.erase(Ref); |
| 179 | (void)WasErased; |
| 180 | assert(WasErased && "Expected to drop a reference"); |
| 181 | } |
| 182 | |
| 183 | void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, |
| 184 | const Metadata &MD) { |
| 185 | auto I = UseMap.find(Ref); |
| 186 | assert(I != UseMap.end() && "Expected to move a reference"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 187 | auto OwnerAndIndex = I->second; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 188 | UseMap.erase(I); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 189 | bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second; |
| 190 | (void)WasInserted; |
| 191 | assert(WasInserted && "Expected to add a reference"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 192 | |
| 193 | // Check that the references are direct if there's no owner. |
| 194 | (void)MD; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 195 | assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 196 | "Reference without owner must be direct"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 197 | assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 198 | "Reference without owner must be direct"); |
| 199 | } |
| 200 | |
| 201 | void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 202 | if (UseMap.empty()) |
| 203 | return; |
| 204 | |
| 205 | // Copy out uses since UseMap will get touched below. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 206 | typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; |
| 207 | SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); |
| 208 | std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { |
| 209 | return L.second.second < R.second.second; |
| 210 | }); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 211 | for (const auto &Pair : Uses) { |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 212 | // Check that this Ref hasn't disappeared after RAUW (when updating a |
| 213 | // previous Ref). |
| 214 | if (!UseMap.count(Pair.first)) |
| 215 | continue; |
| 216 | |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 217 | OwnerTy Owner = Pair.second.first; |
| 218 | if (!Owner) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 219 | // Update unowned tracking references directly. |
| 220 | Metadata *&Ref = *static_cast<Metadata **>(Pair.first); |
| 221 | Ref = MD; |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 222 | if (MD) |
| 223 | MetadataTracking::track(Ref); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 224 | UseMap.erase(Pair.first); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | // Check for MetadataAsValue. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 229 | if (Owner.is<MetadataAsValue *>()) { |
| 230 | Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 231 | continue; |
| 232 | } |
| 233 | |
| 234 | // There's a Metadata owner -- dispatch. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 235 | Metadata *OwnerMD = Owner.get<Metadata *>(); |
| 236 | switch (OwnerMD->getMetadataID()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 237 | #define HANDLE_METADATA_LEAF(CLASS) \ |
| 238 | case Metadata::CLASS##Kind: \ |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 239 | cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \ |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 240 | continue; |
| 241 | #include "llvm/IR/Metadata.def" |
| 242 | default: |
| 243 | llvm_unreachable("Invalid metadata subclass"); |
| 244 | } |
| 245 | } |
| 246 | assert(UseMap.empty() && "Expected all uses to be replaced"); |
| 247 | } |
| 248 | |
| 249 | void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { |
| 250 | if (UseMap.empty()) |
| 251 | return; |
| 252 | |
| 253 | if (!ResolveUsers) { |
| 254 | UseMap.clear(); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | // Copy out uses since UseMap could get touched below. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 259 | typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; |
| 260 | SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); |
| 261 | std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { |
| 262 | return L.second.second < R.second.second; |
| 263 | }); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 264 | UseMap.clear(); |
| 265 | for (const auto &Pair : Uses) { |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 266 | auto Owner = Pair.second.first; |
| 267 | if (!Owner) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 268 | continue; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 269 | if (Owner.is<MetadataAsValue *>()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 270 | continue; |
| 271 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 272 | // Resolve MDNodes that point at this. |
| 273 | auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>()); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 274 | if (!OwnerMD) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 275 | continue; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 276 | if (OwnerMD->isResolved()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 277 | continue; |
Duncan P. N. Exon Smith | 34c3d10 | 2015-01-12 19:43:15 +0000 | [diff] [blame] | 278 | OwnerMD->decrementUnresolvedOperandCount(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 282 | ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) { |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 283 | if (auto *N = dyn_cast<MDNode>(&MD)) |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 284 | return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses(); |
| 285 | return dyn_cast<ValueAsMetadata>(&MD); |
| 286 | } |
| 287 | |
| 288 | ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) { |
| 289 | if (auto *N = dyn_cast<MDNode>(&MD)) |
| 290 | return N->isResolved() ? nullptr : N->Context.getReplaceableUses(); |
| 291 | return dyn_cast<ValueAsMetadata>(&MD); |
| 292 | } |
| 293 | |
| 294 | bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) { |
| 295 | if (auto *N = dyn_cast<MDNode>(&MD)) |
| 296 | return !N->isResolved(); |
Chandler Carruth | a7dc087 | 2015-12-29 02:14:50 +0000 | [diff] [blame] | 297 | return dyn_cast<ValueAsMetadata>(&MD); |
| 298 | } |
| 299 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 300 | static Function *getLocalFunction(Value *V) { |
| 301 | assert(V && "Expected value"); |
| 302 | if (auto *A = dyn_cast<Argument>(V)) |
| 303 | return A->getParent(); |
| 304 | if (BasicBlock *BB = cast<Instruction>(V)->getParent()) |
| 305 | return BB->getParent(); |
| 306 | return nullptr; |
| 307 | } |
| 308 | |
| 309 | ValueAsMetadata *ValueAsMetadata::get(Value *V) { |
| 310 | assert(V && "Unexpected null Value"); |
| 311 | |
| 312 | auto &Context = V->getContext(); |
| 313 | auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; |
| 314 | if (!Entry) { |
| 315 | assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && |
| 316 | "Expected constant or function-local value"); |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 317 | assert(!V->IsUsedByMD && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 318 | "Expected this to be the only metadata use"); |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 319 | V->IsUsedByMD = true; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 320 | if (auto *C = dyn_cast<Constant>(V)) |
Duncan P. N. Exon Smith | 1c00c9f | 2015-01-05 20:41:25 +0000 | [diff] [blame] | 321 | Entry = new ConstantAsMetadata(C); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 322 | else |
Duncan P. N. Exon Smith | 1c00c9f | 2015-01-05 20:41:25 +0000 | [diff] [blame] | 323 | Entry = new LocalAsMetadata(V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return Entry; |
| 327 | } |
| 328 | |
| 329 | ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { |
| 330 | assert(V && "Unexpected null Value"); |
| 331 | return V->getContext().pImpl->ValuesAsMetadata.lookup(V); |
| 332 | } |
| 333 | |
| 334 | void ValueAsMetadata::handleDeletion(Value *V) { |
| 335 | assert(V && "Expected valid value"); |
| 336 | |
| 337 | auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; |
| 338 | auto I = Store.find(V); |
| 339 | if (I == Store.end()) |
| 340 | return; |
| 341 | |
| 342 | // Remove old entry from the map. |
| 343 | ValueAsMetadata *MD = I->second; |
| 344 | assert(MD && "Expected valid metadata"); |
| 345 | assert(MD->getValue() == V && "Expected valid mapping"); |
| 346 | Store.erase(I); |
| 347 | |
| 348 | // Delete the metadata. |
| 349 | MD->replaceAllUsesWith(nullptr); |
| 350 | delete MD; |
| 351 | } |
| 352 | |
| 353 | void ValueAsMetadata::handleRAUW(Value *From, Value *To) { |
| 354 | assert(From && "Expected valid value"); |
| 355 | assert(To && "Expected valid value"); |
| 356 | assert(From != To && "Expected changed value"); |
| 357 | assert(From->getType() == To->getType() && "Unexpected type change"); |
| 358 | |
| 359 | LLVMContext &Context = From->getType()->getContext(); |
| 360 | auto &Store = Context.pImpl->ValuesAsMetadata; |
| 361 | auto I = Store.find(From); |
| 362 | if (I == Store.end()) { |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 363 | assert(!From->IsUsedByMD && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 364 | "Expected From not to be used by metadata"); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | // Remove old entry from the map. |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 369 | assert(From->IsUsedByMD && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 370 | "Expected From to be used by metadata"); |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 371 | From->IsUsedByMD = false; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 372 | ValueAsMetadata *MD = I->second; |
| 373 | assert(MD && "Expected valid metadata"); |
| 374 | assert(MD->getValue() == From && "Expected valid mapping"); |
| 375 | Store.erase(I); |
| 376 | |
| 377 | if (isa<LocalAsMetadata>(MD)) { |
| 378 | if (auto *C = dyn_cast<Constant>(To)) { |
| 379 | // Local became a constant. |
| 380 | MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); |
| 381 | delete MD; |
| 382 | return; |
| 383 | } |
| 384 | if (getLocalFunction(From) && getLocalFunction(To) && |
| 385 | getLocalFunction(From) != getLocalFunction(To)) { |
| 386 | // Function changed. |
| 387 | MD->replaceAllUsesWith(nullptr); |
| 388 | delete MD; |
| 389 | return; |
| 390 | } |
| 391 | } else if (!isa<Constant>(To)) { |
| 392 | // Changed to function-local value. |
| 393 | MD->replaceAllUsesWith(nullptr); |
| 394 | delete MD; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | auto *&Entry = Store[To]; |
| 399 | if (Entry) { |
| 400 | // The target already exists. |
| 401 | MD->replaceAllUsesWith(Entry); |
| 402 | delete MD; |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Update MD in place (and update the map entry). |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 407 | assert(!To->IsUsedByMD && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 408 | "Expected this to be the only metadata use"); |
Owen Anderson | 7349ab9 | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 409 | To->IsUsedByMD = true; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 410 | MD->V = To; |
| 411 | Entry = MD; |
| 412 | } |
Duncan P. N. Exon Smith | a69934f | 2014-11-14 18:42:09 +0000 | [diff] [blame] | 413 | |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 415 | // MDString implementation. |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 416 | // |
Chris Lattner | 5a409bd | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 417 | |
Devang Patel | dcb99d3 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 418 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 419 | auto &Store = Context.pImpl->MDStringCache; |
Benjamin Kramer | eab3d36 | 2016-07-21 13:37:48 +0000 | [diff] [blame] | 420 | auto I = Store.try_emplace(Str); |
Mehdi Amini | cb708b2 | 2016-03-25 05:58:04 +0000 | [diff] [blame] | 421 | auto &MapEntry = I.first->getValue(); |
| 422 | if (!I.second) |
| 423 | return &MapEntry; |
| 424 | MapEntry.Entry = &*I.first; |
| 425 | return &MapEntry; |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | StringRef MDString::getString() const { |
Duncan P. N. Exon Smith | c1a664f | 2014-12-05 01:41:34 +0000 | [diff] [blame] | 429 | assert(Entry && "Expected to find string map entry"); |
| 430 | return Entry->first(); |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 434 | // MDNode implementation. |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 435 | // |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 436 | |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 437 | // Assert that the MDNode types will not be unaligned by the objects |
| 438 | // prepended to them. |
| 439 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
James Y Knight | f27e441 | 2015-06-17 13:53:12 +0000 | [diff] [blame] | 440 | static_assert( \ |
| 441 | llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment, \ |
| 442 | "Alignment is insufficient after objects prepended to " #CLASS); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 443 | #include "llvm/IR/Metadata.def" |
| 444 | |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 445 | void *MDNode::operator new(size_t Size, unsigned NumOps) { |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 446 | size_t OpSize = NumOps * sizeof(MDOperand); |
| 447 | // uint64_t is the most aligned type we need support (ensured by static_assert |
| 448 | // above) |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 449 | OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>()); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 450 | void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize; |
Duncan P. N. Exon Smith | 22600ff | 2014-12-09 23:56:39 +0000 | [diff] [blame] | 451 | MDOperand *O = static_cast<MDOperand *>(Ptr); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 452 | for (MDOperand *E = O - NumOps; O != E; --O) |
| 453 | (void)new (O - 1) MDOperand; |
| 454 | return Ptr; |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Naomi Musgrave | 21c1bc4 | 2015-08-31 21:06:08 +0000 | [diff] [blame] | 457 | void MDNode::operator delete(void *Mem) { |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 458 | MDNode *N = static_cast<MDNode *>(Mem); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 459 | size_t OpSize = N->NumOperands * sizeof(MDOperand); |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 460 | OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>()); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 461 | |
Duncan P. N. Exon Smith | 22600ff | 2014-12-09 23:56:39 +0000 | [diff] [blame] | 462 | MDOperand *O = static_cast<MDOperand *>(Mem); |
| 463 | for (MDOperand *E = O - N->NumOperands; O != E; --O) |
| 464 | (O - 1)->~MDOperand(); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 465 | ::operator delete(reinterpret_cast<char *>(Mem) - OpSize); |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Duncan P. N. Exon Smith | f134045 | 2015-01-19 18:36:18 +0000 | [diff] [blame] | 468 | MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 469 | ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2) |
| 470 | : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), |
| 471 | NumUnresolved(0), Context(Context) { |
| 472 | unsigned Op = 0; |
| 473 | for (Metadata *MD : Ops1) |
| 474 | setOperand(Op++, MD); |
| 475 | for (Metadata *MD : Ops2) |
| 476 | setOperand(Op++, MD); |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 477 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 478 | if (!isUniqued()) |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 479 | return; |
| 480 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 481 | // Count the unresolved operands. If there are any, RAUW support will be |
| 482 | // added lazily on first reference. |
| 483 | countUnresolvedOperands(); |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Duncan P. N. Exon Smith | 03e0583 | 2015-01-20 02:56:57 +0000 | [diff] [blame] | 486 | TempMDNode MDNode::clone() const { |
| 487 | switch (getMetadataID()) { |
| 488 | default: |
| 489 | llvm_unreachable("Invalid MDNode subclass"); |
| 490 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
| 491 | case CLASS##Kind: \ |
| 492 | return cast<CLASS>(this)->cloneImpl(); |
| 493 | #include "llvm/IR/Metadata.def" |
| 494 | } |
| 495 | } |
| 496 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 497 | static bool isOperandUnresolved(Metadata *Op) { |
| 498 | if (auto *N = dyn_cast_or_null<MDNode>(Op)) |
| 499 | return !N->isResolved(); |
| 500 | return false; |
| 501 | } |
| 502 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 503 | void MDNode::countUnresolvedOperands() { |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 504 | assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted"); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 505 | assert(isUniqued() && "Expected this to be uniqued"); |
Sanjoy Das | 39c226f | 2016-06-10 21:18:39 +0000 | [diff] [blame] | 506 | NumUnresolved = count_if(operands(), isOperandUnresolved); |
Duncan P. N. Exon Smith | c5a0e2e | 2015-01-19 22:18:29 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 509 | void MDNode::makeUniqued() { |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 510 | assert(isTemporary() && "Expected this to be temporary"); |
| 511 | assert(!isResolved() && "Expected this to be unresolved"); |
| 512 | |
Duncan P. N. Exon Smith | cb33d6f | 2015-03-31 20:50:50 +0000 | [diff] [blame] | 513 | // Enable uniquing callbacks. |
| 514 | for (auto &Op : mutable_operands()) |
| 515 | Op.reset(Op.get(), this); |
| 516 | |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 517 | // Make this 'uniqued'. |
| 518 | Storage = Uniqued; |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 519 | countUnresolvedOperands(); |
| 520 | if (!NumUnresolved) { |
| 521 | dropReplaceableUses(); |
| 522 | assert(isResolved() && "Expected this to be resolved"); |
| 523 | } |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 524 | |
| 525 | assert(isUniqued() && "Expected this to be uniqued"); |
| 526 | } |
| 527 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 528 | void MDNode::makeDistinct() { |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 529 | assert(isTemporary() && "Expected this to be temporary"); |
| 530 | assert(!isResolved() && "Expected this to be unresolved"); |
| 531 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 532 | // Drop RAUW support and store as a distinct node. |
| 533 | dropReplaceableUses(); |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 534 | storeDistinctInContext(); |
| 535 | |
| 536 | assert(isDistinct() && "Expected this to be distinct"); |
| 537 | assert(isResolved() && "Expected this to be resolved"); |
| 538 | } |
| 539 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 540 | void MDNode::resolve() { |
Duncan P. N. Exon Smith | b8f7960 | 2015-01-19 19:26:24 +0000 | [diff] [blame] | 541 | assert(isUniqued() && "Expected this to be uniqued"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 542 | assert(!isResolved() && "Expected this to be unresolved"); |
| 543 | |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 544 | NumUnresolved = 0; |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 545 | dropReplaceableUses(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 546 | |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 547 | assert(isResolved() && "Expected this to be resolved"); |
| 548 | } |
| 549 | |
| 550 | void MDNode::dropReplaceableUses() { |
| 551 | assert(!NumUnresolved && "Unexpected unresolved operand"); |
| 552 | |
| 553 | // Drop any RAUW support. |
| 554 | if (Context.hasReplaceableUses()) |
| 555 | Context.takeReplaceableUses()->resolveAllUses(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 558 | void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 559 | assert(isUniqued() && "Expected this to be uniqued"); |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 560 | assert(NumUnresolved != 0 && "Expected unresolved operands"); |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 561 | |
Duncan P. N. Exon Smith | 0c87d77 | 2015-01-12 19:45:44 +0000 | [diff] [blame] | 562 | // Check if an operand was resolved. |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 563 | if (!isOperandUnresolved(Old)) { |
| 564 | if (isOperandUnresolved(New)) |
| 565 | // An operand was un-resolved! |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 566 | ++NumUnresolved; |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 567 | } else if (!isOperandUnresolved(New)) |
Duncan P. N. Exon Smith | 0c87d77 | 2015-01-12 19:45:44 +0000 | [diff] [blame] | 568 | decrementUnresolvedOperandCount(); |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 571 | void MDNode::decrementUnresolvedOperandCount() { |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 572 | assert(!isResolved() && "Expected this to be unresolved"); |
| 573 | if (isTemporary()) |
| 574 | return; |
| 575 | |
| 576 | assert(isUniqued() && "Expected this to be uniqued"); |
| 577 | if (--NumUnresolved) |
| 578 | return; |
| 579 | |
| 580 | // Last unresolved operand has just been resolved. |
| 581 | dropReplaceableUses(); |
| 582 | assert(isResolved() && "Expected this to become resolved"); |
Duncan P. N. Exon Smith | 34c3d10 | 2015-01-12 19:43:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Teresa Johnson | b703c77 | 2016-03-29 18:24:19 +0000 | [diff] [blame] | 585 | void MDNode::resolveCycles() { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 586 | if (isResolved()) |
| 587 | return; |
| 588 | |
| 589 | // Resolve this node immediately. |
| 590 | resolve(); |
| 591 | |
| 592 | // Resolve all operands. |
| 593 | for (const auto &Op : operands()) { |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 594 | auto *N = dyn_cast_or_null<MDNode>(Op); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 595 | if (!N) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 596 | continue; |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 597 | |
| 598 | assert(!N->isTemporary() && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 599 | "Expected all forward declarations to be resolved"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 600 | if (!N->isResolved()) |
| 601 | N->resolveCycles(); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 602 | } |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Duncan P. N. Exon Smith | 4ee4a98 | 2015-02-10 19:13:46 +0000 | [diff] [blame] | 605 | static bool hasSelfReference(MDNode *N) { |
| 606 | for (Metadata *MD : N->operands()) |
| 607 | if (MD == N) |
| 608 | return true; |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | MDNode *MDNode::replaceWithPermanentImpl() { |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 613 | switch (getMetadataID()) { |
| 614 | default: |
| 615 | // If this type isn't uniquable, replace with a distinct node. |
| 616 | return replaceWithDistinctImpl(); |
| 617 | |
| 618 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
| 619 | case CLASS##Kind: \ |
| 620 | break; |
| 621 | #include "llvm/IR/Metadata.def" |
| 622 | } |
| 623 | |
| 624 | // Even if this type is uniquable, self-references have to be distinct. |
Duncan P. N. Exon Smith | 4ee4a98 | 2015-02-10 19:13:46 +0000 | [diff] [blame] | 625 | if (hasSelfReference(this)) |
| 626 | return replaceWithDistinctImpl(); |
| 627 | return replaceWithUniquedImpl(); |
| 628 | } |
| 629 | |
Duncan P. N. Exon Smith | 8647529 | 2015-01-19 23:17:09 +0000 | [diff] [blame] | 630 | MDNode *MDNode::replaceWithUniquedImpl() { |
| 631 | // Try to uniquify in place. |
| 632 | MDNode *UniquedNode = uniquify(); |
Duncan P. N. Exon Smith | 4ee4a98 | 2015-02-10 19:13:46 +0000 | [diff] [blame] | 633 | |
Duncan P. N. Exon Smith | 8647529 | 2015-01-19 23:17:09 +0000 | [diff] [blame] | 634 | if (UniquedNode == this) { |
| 635 | makeUniqued(); |
| 636 | return this; |
| 637 | } |
| 638 | |
| 639 | // Collision, so RAUW instead. |
| 640 | replaceAllUsesWith(UniquedNode); |
| 641 | deleteAsSubclass(); |
| 642 | return UniquedNode; |
| 643 | } |
| 644 | |
| 645 | MDNode *MDNode::replaceWithDistinctImpl() { |
| 646 | makeDistinct(); |
| 647 | return this; |
| 648 | } |
| 649 | |
Duncan P. N. Exon Smith | 118632d | 2015-01-12 20:09:34 +0000 | [diff] [blame] | 650 | void MDTuple::recalculateHash() { |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 651 | setHash(MDTupleInfo::KeyTy::calculateHash(this)); |
Duncan P. N. Exon Smith | 967629e | 2015-01-12 19:16:34 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 654 | void MDNode::dropAllReferences() { |
| 655 | for (unsigned I = 0, E = NumOperands; I != E; ++I) |
| 656 | setOperand(I, nullptr); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 657 | if (Context.hasReplaceableUses()) { |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 658 | Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false); |
| 659 | (void)Context.takeReplaceableUses(); |
| 660 | } |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 663 | void MDNode::handleChangedOperand(void *Ref, Metadata *New) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 664 | unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); |
| 665 | assert(Op < getNumOperands() && "Expected valid operand"); |
| 666 | |
Duncan P. N. Exon Smith | 3d58056 | 2015-01-19 19:28:28 +0000 | [diff] [blame] | 667 | if (!isUniqued()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 668 | // This node is not uniqued. Just set the operand and be done with it. |
| 669 | setOperand(Op, New); |
| 670 | return; |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 671 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 672 | |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 673 | // This node is uniqued. |
| 674 | eraseFromStore(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 675 | |
| 676 | Metadata *Old = getOperand(Op); |
| 677 | setOperand(Op, New); |
| 678 | |
Duncan P. N. Exon Smith | 9cbc69d | 2016-08-03 18:19:43 +0000 | [diff] [blame] | 679 | // Drop uniquing for self-reference cycles and deleted constants. |
| 680 | if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 681 | if (!isResolved()) |
| 682 | resolve(); |
Duncan P. N. Exon Smith | f08b8b4 | 2015-01-19 19:25:33 +0000 | [diff] [blame] | 683 | storeDistinctInContext(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 687 | // Re-unique the node. |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 688 | auto *Uniqued = uniquify(); |
| 689 | if (Uniqued == this) { |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 690 | if (!isResolved()) |
| 691 | resolveAfterOperandChange(Old, New); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | |
| 695 | // Collision. |
| 696 | if (!isResolved()) { |
| 697 | // Still unresolved, so RAUW. |
Duncan P. N. Exon Smith | d9e6eb7 | 2015-01-12 19:36:35 +0000 | [diff] [blame] | 698 | // |
| 699 | // First, clear out all operands to prevent any recursion (similar to |
| 700 | // dropAllReferences(), but we still need the use-list). |
| 701 | for (unsigned O = 0, E = getNumOperands(); O != E; ++O) |
| 702 | setOperand(O, nullptr); |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 703 | if (Context.hasReplaceableUses()) |
| 704 | Context.getReplaceableUses()->replaceAllUsesWith(Uniqued); |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 705 | deleteAsSubclass(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 706 | return; |
| 707 | } |
| 708 | |
Duncan P. N. Exon Smith | d9e6eb7 | 2015-01-12 19:36:35 +0000 | [diff] [blame] | 709 | // Store in non-uniqued form if RAUW isn't possible. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 710 | storeDistinctInContext(); |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 713 | void MDNode::deleteAsSubclass() { |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 714 | switch (getMetadataID()) { |
| 715 | default: |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 716 | llvm_unreachable("Invalid subclass of MDNode"); |
| 717 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 718 | case CLASS##Kind: \ |
| 719 | delete cast<CLASS>(this); \ |
| 720 | break; |
| 721 | #include "llvm/IR/Metadata.def" |
| 722 | } |
| 723 | } |
| 724 | |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 725 | template <class T, class InfoT> |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 726 | static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) { |
| 727 | if (T *U = getUniqued(Store, N)) |
| 728 | return U; |
| 729 | |
| 730 | Store.insert(N); |
| 731 | return N; |
| 732 | } |
| 733 | |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 734 | template <class NodeTy> struct MDNode::HasCachedHash { |
| 735 | typedef char Yes[1]; |
| 736 | typedef char No[2]; |
| 737 | template <class U, U Val> struct SFINAE {}; |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 738 | |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 739 | template <class U> |
| 740 | static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *); |
| 741 | template <class U> static No &check(...); |
| 742 | |
| 743 | static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes); |
| 744 | }; |
| 745 | |
| 746 | MDNode *MDNode::uniquify() { |
Duncan P. N. Exon Smith | 4ee4a98 | 2015-02-10 19:13:46 +0000 | [diff] [blame] | 747 | assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node"); |
| 748 | |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 749 | // Try to insert into uniquing store. |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 750 | switch (getMetadataID()) { |
| 751 | default: |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 752 | llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); |
| 753 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 754 | case CLASS##Kind: { \ |
| 755 | CLASS *SubclassThis = cast<CLASS>(this); \ |
| 756 | std::integral_constant<bool, HasCachedHash<CLASS>::value> \ |
| 757 | ShouldRecalculateHash; \ |
| 758 | dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ |
| 759 | return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \ |
| 760 | } |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 761 | #include "llvm/IR/Metadata.def" |
| 762 | } |
| 763 | } |
| 764 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 765 | void MDNode::eraseFromStore() { |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 766 | switch (getMetadataID()) { |
| 767 | default: |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 768 | llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); |
| 769 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 770 | case CLASS##Kind: \ |
Duncan P. N. Exon Smith | 6cf10d2 | 2015-01-19 22:47:08 +0000 | [diff] [blame] | 771 | getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 772 | break; |
| 773 | #include "llvm/IR/Metadata.def" |
| 774 | } |
| 775 | } |
| 776 | |
Duncan P. N. Exon Smith | ac3128d | 2015-01-12 20:13:56 +0000 | [diff] [blame] | 777 | MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 778 | StorageType Storage, bool ShouldCreate) { |
| 779 | unsigned Hash = 0; |
| 780 | if (Storage == Uniqued) { |
| 781 | MDTupleInfo::KeyTy Key(MDs); |
Duncan P. N. Exon Smith | b57f9e9 | 2015-01-19 20:16:50 +0000 | [diff] [blame] | 782 | if (auto *N = getUniqued(Context.pImpl->MDTuples, Key)) |
| 783 | return N; |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 784 | if (!ShouldCreate) |
| 785 | return nullptr; |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 786 | Hash = Key.getHash(); |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 787 | } else { |
| 788 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); |
| 789 | } |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 790 | |
Duncan P. N. Exon Smith | 5b8c440 | 2015-01-19 20:18:13 +0000 | [diff] [blame] | 791 | return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs), |
| 792 | Storage, Context.pImpl->MDTuples); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 795 | void MDNode::deleteTemporary(MDNode *N) { |
| 796 | assert(N->isTemporary() && "Expected temporary node"); |
Duncan P. N. Exon Smith | 8d53697 | 2015-01-22 21:36:45 +0000 | [diff] [blame] | 797 | N->replaceAllUsesWith(nullptr); |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 798 | N->deleteAsSubclass(); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 801 | void MDNode::storeDistinctInContext() { |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 802 | assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses"); |
| 803 | assert(!NumUnresolved && "Unexpected unresolved nodes"); |
Duncan P. N. Exon Smith | f134045 | 2015-01-19 18:36:18 +0000 | [diff] [blame] | 804 | Storage = Distinct; |
Duncan P. N. Exon Smith | fef609f | 2016-04-03 21:23:52 +0000 | [diff] [blame] | 805 | assert(isResolved() && "Expected this to be resolved"); |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 806 | |
| 807 | // Reset the hash. |
| 808 | switch (getMetadataID()) { |
| 809 | default: |
| 810 | llvm_unreachable("Invalid subclass of MDNode"); |
| 811 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
| 812 | case CLASS##Kind: { \ |
| 813 | std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \ |
| 814 | dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \ |
| 815 | break; \ |
| 816 | } |
| 817 | #include "llvm/IR/Metadata.def" |
| 818 | } |
| 819 | |
Duncan P. N. Exon Smith | 3eef9d1 | 2016-04-19 23:59:13 +0000 | [diff] [blame] | 820 | getContext().pImpl->DistinctMDNodes.push_back(this); |
Devang Patel | 82ab3f8 | 2010-02-18 20:53:16 +0000 | [diff] [blame] | 821 | } |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 822 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 823 | void MDNode::replaceOperandWith(unsigned I, Metadata *New) { |
| 824 | if (getOperand(I) == New) |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 825 | return; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 826 | |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 827 | if (!isUniqued()) { |
Duncan P. N. Exon Smith | daa335a | 2015-01-12 18:01:45 +0000 | [diff] [blame] | 828 | setOperand(I, New); |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 831 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 832 | handleChangedOperand(mutable_begin() + I, New); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 833 | } |
Chris Lattner | c6d17e2 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 834 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 835 | void MDNode::setOperand(unsigned I, Metadata *New) { |
| 836 | assert(I < NumOperands); |
Duncan P. N. Exon Smith | efdf285 | 2015-01-19 19:29:25 +0000 | [diff] [blame] | 837 | mutable_begin()[I].reset(New, isUniqued() ? this : nullptr); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Sanjay Patel | 9da9c76 | 2016-03-12 20:44:58 +0000 | [diff] [blame] | 840 | /// Get a node or a self-reference that looks like it. |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 841 | /// |
| 842 | /// Special handling for finding self-references, for use by \a |
| 843 | /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from |
| 844 | /// when self-referencing nodes were still uniqued. If the first operand has |
| 845 | /// the same operands as \c Ops, return the first operand instead. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 846 | static MDNode *getOrSelfReference(LLVMContext &Context, |
| 847 | ArrayRef<Metadata *> Ops) { |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 848 | if (!Ops.empty()) |
| 849 | if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0])) |
| 850 | if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { |
| 851 | for (unsigned I = 1, E = Ops.size(); I != E; ++I) |
| 852 | if (Ops[I] != N->getOperand(I)) |
| 853 | return MDNode::get(Context, Ops); |
| 854 | return N; |
| 855 | } |
| 856 | |
| 857 | return MDNode::get(Context, Ops); |
| 858 | } |
| 859 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 860 | MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { |
| 861 | if (!A) |
| 862 | return B; |
| 863 | if (!B) |
| 864 | return A; |
| 865 | |
David Majnemer | fa0f1e6 | 2016-08-16 18:48:34 +0000 | [diff] [blame] | 866 | SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end()); |
| 867 | MDs.insert(B->op_begin(), B->op_end()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 868 | |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 869 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 870 | // behaviour? Or was that an unintended side-effect of node uniquing? |
David Majnemer | fa0f1e6 | 2016-08-16 18:48:34 +0000 | [diff] [blame] | 871 | return getOrSelfReference(A->getContext(), MDs.getArrayRef()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | MDNode *MDNode::intersect(MDNode *A, MDNode *B) { |
| 875 | if (!A || !B) |
| 876 | return nullptr; |
| 877 | |
David Majnemer | 00940fb | 2016-08-16 18:48:37 +0000 | [diff] [blame^] | 878 | SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end()); |
| 879 | SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end()); |
| 880 | MDs.remove_if([&](Metadata *MD) { return !is_contained(BSet, MD); }); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 881 | |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 882 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 883 | // behaviour? Or was that an unintended side-effect of node uniquing? |
David Majnemer | 00940fb | 2016-08-16 18:48:37 +0000 | [diff] [blame^] | 884 | return getOrSelfReference(A->getContext(), MDs.getArrayRef()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Bjorn Steinbrink | 5ec7522 | 2015-02-08 17:07:14 +0000 | [diff] [blame] | 887 | MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) { |
| 888 | if (!A || !B) |
| 889 | return nullptr; |
| 890 | |
David Majnemer | fa0f1e6 | 2016-08-16 18:48:34 +0000 | [diff] [blame] | 891 | return concatenate(A, B); |
Bjorn Steinbrink | 5ec7522 | 2015-02-08 17:07:14 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 894 | MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { |
| 895 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 896 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 897 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 898 | APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF(); |
| 899 | APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 900 | if (AVal.compare(BVal) == APFloat::cmpLessThan) |
| 901 | return A; |
| 902 | return B; |
| 903 | } |
| 904 | |
| 905 | static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { |
| 906 | return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); |
| 907 | } |
| 908 | |
| 909 | static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { |
| 910 | return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); |
| 911 | } |
| 912 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 913 | static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 914 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 915 | ConstantRange NewRange(Low->getValue(), High->getValue()); |
| 916 | unsigned Size = EndPoints.size(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 917 | APInt LB = EndPoints[Size - 2]->getValue(); |
| 918 | APInt LE = EndPoints[Size - 1]->getValue(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 919 | ConstantRange LastRange(LB, LE); |
| 920 | if (canBeMerged(NewRange, LastRange)) { |
| 921 | ConstantRange Union = LastRange.unionWith(NewRange); |
| 922 | Type *Ty = High->getType(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 923 | EndPoints[Size - 2] = |
| 924 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower())); |
| 925 | EndPoints[Size - 1] = |
| 926 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper())); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 927 | return true; |
| 928 | } |
| 929 | return false; |
| 930 | } |
| 931 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 932 | static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 933 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 934 | if (!EndPoints.empty()) |
| 935 | if (tryMergeRange(EndPoints, Low, High)) |
| 936 | return; |
| 937 | |
| 938 | EndPoints.push_back(Low); |
| 939 | EndPoints.push_back(High); |
| 940 | } |
| 941 | |
| 942 | MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { |
| 943 | // Given two ranges, we want to compute the union of the ranges. This |
| 944 | // is slightly complitade by having to combine the intervals and merge |
| 945 | // the ones that overlap. |
| 946 | |
| 947 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 948 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 949 | |
| 950 | if (A == B) |
| 951 | return A; |
| 952 | |
| 953 | // First, walk both lists in older of the lower boundary of each interval. |
| 954 | // At each step, try to merge the new interval to the last one we adedd. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 955 | SmallVector<ConstantInt *, 4> EndPoints; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 956 | int AI = 0; |
| 957 | int BI = 0; |
| 958 | int AN = A->getNumOperands() / 2; |
| 959 | int BN = B->getNumOperands() / 2; |
| 960 | while (AI < AN && BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 961 | ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI)); |
| 962 | ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI)); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 963 | |
| 964 | if (ALow->getValue().slt(BLow->getValue())) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 965 | addRange(EndPoints, ALow, |
| 966 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 967 | ++AI; |
| 968 | } else { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 969 | addRange(EndPoints, BLow, |
| 970 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 971 | ++BI; |
| 972 | } |
| 973 | } |
| 974 | while (AI < AN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 975 | addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)), |
| 976 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 977 | ++AI; |
| 978 | } |
| 979 | while (BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 980 | addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)), |
| 981 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 982 | ++BI; |
| 983 | } |
| 984 | |
| 985 | // If we have more than 2 ranges (4 endpoints) we have to try to merge |
| 986 | // the last and first ones. |
| 987 | unsigned Size = EndPoints.size(); |
| 988 | if (Size > 4) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 989 | ConstantInt *FB = EndPoints[0]; |
| 990 | ConstantInt *FE = EndPoints[1]; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 991 | if (tryMergeRange(EndPoints, FB, FE)) { |
| 992 | for (unsigned i = 0; i < Size - 2; ++i) { |
| 993 | EndPoints[i] = EndPoints[i + 2]; |
| 994 | } |
| 995 | EndPoints.resize(Size - 2); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | // If in the end we have a single range, it is possible that it is now the |
| 1000 | // full range. Just drop the metadata in that case. |
| 1001 | if (EndPoints.size() == 2) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1002 | ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 1003 | if (Range.isFullSet()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1004 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1007 | SmallVector<Metadata *, 4> MDs; |
| 1008 | MDs.reserve(EndPoints.size()); |
| 1009 | for (auto *I : EndPoints) |
| 1010 | MDs.push_back(ConstantAsMetadata::get(I)); |
| 1011 | return MDNode::get(A->getContext(), MDs); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 1014 | MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) { |
| 1015 | if (!A || !B) |
| 1016 | return nullptr; |
| 1017 | |
| 1018 | ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0)); |
| 1019 | ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0)); |
| 1020 | if (AVal->getZExtValue() < BVal->getZExtValue()) |
| 1021 | return A; |
| 1022 | return B; |
| 1023 | } |
| 1024 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 1025 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 1026 | // NamedMDNode implementation. |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 1027 | // |
Devang Patel | 943ddf6 | 2010-01-12 18:34:06 +0000 | [diff] [blame] | 1028 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1029 | static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) { |
| 1030 | return *(SmallVector<TrackingMDRef, 4> *)Operands; |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1033 | NamedMDNode::NamedMDNode(const Twine &N) |
Duncan P. N. Exon Smith | c5754a6 | 2014-11-05 18:16:03 +0000 | [diff] [blame] | 1034 | : Name(N.str()), Parent(nullptr), |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1035 | Operands(new SmallVector<TrackingMDRef, 4>()) {} |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1037 | NamedMDNode::~NamedMDNode() { |
| 1038 | dropAllReferences(); |
| 1039 | delete &getNMDOps(Operands); |
| 1040 | } |
| 1041 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 1042 | unsigned NamedMDNode::getNumOperands() const { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1043 | return (unsigned)getNMDOps(Operands).size(); |
| 1044 | } |
| 1045 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1046 | MDNode *NamedMDNode::getOperand(unsigned i) const { |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 1047 | assert(i < getNumOperands() && "Invalid Operand number!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1048 | auto *N = getNMDOps(Operands)[i].get(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1049 | return cast_or_null<MDNode>(N); |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1052 | void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1053 | |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1054 | void NamedMDNode::setOperand(unsigned I, MDNode *New) { |
| 1055 | assert(I < getNumOperands() && "Invalid operand number"); |
| 1056 | getNMDOps(Operands)[I].reset(New); |
| 1057 | } |
| 1058 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 1059 | void NamedMDNode::eraseFromParent() { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1060 | getParent()->eraseNamedMetadata(this); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 1063 | void NamedMDNode::dropAllReferences() { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 1064 | getNMDOps(Operands).clear(); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 1067 | StringRef NamedMDNode::getName() const { |
| 1068 | return StringRef(Name); |
| 1069 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 1070 | |
| 1071 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1072 | // Instruction Metadata method implementations. |
| 1073 | // |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1074 | void MDAttachmentMap::set(unsigned ID, MDNode &MD) { |
| 1075 | for (auto &I : Attachments) |
| 1076 | if (I.first == ID) { |
| 1077 | I.second.reset(&MD); |
| 1078 | return; |
| 1079 | } |
| 1080 | Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID), |
| 1081 | std::make_tuple(&MD)); |
| 1082 | } |
| 1083 | |
| 1084 | void MDAttachmentMap::erase(unsigned ID) { |
| 1085 | if (empty()) |
| 1086 | return; |
| 1087 | |
| 1088 | // Common case is one/last value. |
| 1089 | if (Attachments.back().first == ID) { |
| 1090 | Attachments.pop_back(); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E; |
| 1095 | ++I) |
| 1096 | if (I->first == ID) { |
| 1097 | *I = std::move(Attachments.back()); |
| 1098 | Attachments.pop_back(); |
| 1099 | return; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | MDNode *MDAttachmentMap::lookup(unsigned ID) const { |
| 1104 | for (const auto &I : Attachments) |
| 1105 | if (I.first == ID) |
| 1106 | return I.second; |
| 1107 | return nullptr; |
| 1108 | } |
| 1109 | |
| 1110 | void MDAttachmentMap::getAll( |
| 1111 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
| 1112 | Result.append(Attachments.begin(), Attachments.end()); |
| 1113 | |
| 1114 | // Sort the resulting array so it is stable. |
| 1115 | if (Result.size() > 1) |
| 1116 | array_pod_sort(Result.begin(), Result.end()); |
| 1117 | } |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1118 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1119 | void MDGlobalAttachmentMap::insert(unsigned ID, MDNode &MD) { |
| 1120 | Attachments.push_back({ID, TrackingMDNodeRef(&MD)}); |
| 1121 | } |
| 1122 | |
| 1123 | void MDGlobalAttachmentMap::get(unsigned ID, |
| 1124 | SmallVectorImpl<MDNode *> &Result) { |
| 1125 | for (auto A : Attachments) |
| 1126 | if (A.MDKind == ID) |
| 1127 | Result.push_back(A.Node); |
| 1128 | } |
| 1129 | |
| 1130 | void MDGlobalAttachmentMap::erase(unsigned ID) { |
| 1131 | auto Follower = Attachments.begin(); |
| 1132 | for (auto Leader = Attachments.begin(), E = Attachments.end(); Leader != E; |
| 1133 | ++Leader) { |
| 1134 | if (Leader->MDKind != ID) { |
| 1135 | if (Follower != Leader) |
| 1136 | *Follower = std::move(*Leader); |
| 1137 | ++Follower; |
| 1138 | } |
| 1139 | } |
| 1140 | Attachments.resize(Follower - Attachments.begin()); |
| 1141 | } |
| 1142 | |
| 1143 | void MDGlobalAttachmentMap::getAll( |
| 1144 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
| 1145 | for (auto &A : Attachments) |
| 1146 | Result.emplace_back(A.MDKind, A.Node); |
| 1147 | |
| 1148 | // Sort the resulting array so it is stable with respect to metadata IDs. We |
| 1149 | // need to preserve the original insertion order though. |
| 1150 | std::stable_sort( |
| 1151 | Result.begin(), Result.end(), |
| 1152 | [](const std::pair<unsigned, MDNode *> &A, |
| 1153 | const std::pair<unsigned, MDNode *> &B) { return A.first < B.first; }); |
| 1154 | } |
| 1155 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1156 | void Instruction::setMetadata(StringRef Kind, MDNode *Node) { |
| 1157 | if (!Node && !hasMetadata()) |
| 1158 | return; |
| 1159 | setMetadata(getContext().getMDKindID(Kind), Node); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1162 | MDNode *Instruction::getMetadataImpl(StringRef Kind) const { |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1163 | return getMetadataImpl(getContext().getMDKindID(Kind)); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Adrian Prantl | cbdfdb7 | 2015-08-20 22:00:30 +0000 | [diff] [blame] | 1166 | void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) { |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1167 | SmallSet<unsigned, 5> KnownSet; |
| 1168 | KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); |
| 1169 | |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1170 | if (!hasMetadataHashEntry()) |
| 1171 | return; // Nothing to remove! |
| 1172 | |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1173 | auto &InstructionMetadata = getContext().pImpl->InstructionMetadata; |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1174 | |
| 1175 | if (KnownSet.empty()) { |
| 1176 | // Just drop our entry at the store. |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1177 | InstructionMetadata.erase(this); |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1178 | setHasMetadataHashEntry(false); |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1182 | auto &Info = InstructionMetadata[this]; |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1183 | Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) { |
| 1184 | return !KnownSet.count(I.first); |
| 1185 | }); |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1186 | |
Duncan P. N. Exon Smith | 75ef0c0 | 2015-04-24 20:23:44 +0000 | [diff] [blame] | 1187 | if (Info.empty()) { |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1188 | // Drop our entry at the store. |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1189 | InstructionMetadata.erase(this); |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 1190 | setHasMetadataHashEntry(false); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1194 | void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
| 1195 | if (!Node && !hasMetadata()) |
| 1196 | return; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1198 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 1199 | if (KindID == LLVMContext::MD_dbg) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 1200 | DbgLoc = DebugLoc(Node); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1201 | return; |
| 1202 | } |
| 1203 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1204 | // Handle the case when we're adding/updating metadata on an instruction. |
| 1205 | if (Node) { |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1206 | auto &Info = getContext().pImpl->InstructionMetadata[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1207 | assert(!Info.empty() == hasMetadataHashEntry() && |
| 1208 | "HasMetadata bit is wonked"); |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1209 | if (Info.empty()) |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1210 | setHasMetadataHashEntry(true); |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1211 | Info.set(KindID, *Node); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1212 | return; |
| 1213 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1215 | // Otherwise, we're removing metadata from an instruction. |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 1216 | assert((hasMetadataHashEntry() == |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1217 | (getContext().pImpl->InstructionMetadata.count(this) > 0)) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1218 | "HasMetadata bit out of date!"); |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 1219 | if (!hasMetadataHashEntry()) |
| 1220 | return; // Nothing to remove! |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1221 | auto &Info = getContext().pImpl->InstructionMetadata[this]; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1222 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1223 | // Handle removal of an existing value. |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1224 | Info.erase(KindID); |
| 1225 | |
| 1226 | if (!Info.empty()) |
| 1227 | return; |
| 1228 | |
| 1229 | getContext().pImpl->InstructionMetadata.erase(this); |
| 1230 | setHasMetadataHashEntry(false); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1233 | void Instruction::setAAMetadata(const AAMDNodes &N) { |
| 1234 | setMetadata(LLVMContext::MD_tbaa, N.TBAA); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 1235 | setMetadata(LLVMContext::MD_alias_scope, N.Scope); |
| 1236 | setMetadata(LLVMContext::MD_noalias, N.NoAlias); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1239 | MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1240 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 1241 | if (KindID == LLVMContext::MD_dbg) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1242 | return DbgLoc.getAsMDNode(); |
| 1243 | |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1244 | if (!hasMetadataHashEntry()) |
| 1245 | return nullptr; |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1246 | auto &Info = getContext().pImpl->InstructionMetadata[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1247 | assert(!Info.empty() && "bit out of sync with hash table"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1248 | |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1249 | return Info.lookup(KindID); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Duncan P. N. Exon Smith | 4abd1a0 | 2014-11-01 00:26:42 +0000 | [diff] [blame] | 1252 | void Instruction::getAllMetadataImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1253 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1254 | Result.clear(); |
| 1255 | |
| 1256 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 1257 | if (DbgLoc) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1258 | Result.push_back( |
| 1259 | std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1260 | if (!hasMetadataHashEntry()) return; |
| 1261 | } |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1263 | assert(hasMetadataHashEntry() && |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1264 | getContext().pImpl->InstructionMetadata.count(this) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1265 | "Shouldn't have called this"); |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1266 | const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1267 | assert(!Info.empty() && "Shouldn't have called this"); |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1268 | Info.getAll(Result); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Duncan P. N. Exon Smith | 3d5a02f | 2014-11-03 18:13:57 +0000 | [diff] [blame] | 1271 | void Instruction::getAllMetadataOtherThanDebugLocImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1272 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1273 | Result.clear(); |
| 1274 | assert(hasMetadataHashEntry() && |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1275 | getContext().pImpl->InstructionMetadata.count(this) && |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1276 | "Shouldn't have called this"); |
Duncan P. N. Exon Smith | 14a384b | 2015-04-24 20:19:13 +0000 | [diff] [blame] | 1277 | const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1278 | assert(!Info.empty() && "Shouldn't have called this"); |
Duncan P. N. Exon Smith | cbc28dc | 2015-04-24 20:36:25 +0000 | [diff] [blame] | 1279 | Info.getAll(Result); |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Sanjay Patel | d66607b | 2016-04-26 17:11:17 +0000 | [diff] [blame] | 1282 | bool Instruction::extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) { |
| 1283 | assert((getOpcode() == Instruction::Br || |
| 1284 | getOpcode() == Instruction::Select) && |
| 1285 | "Looking for branch weights on something besides branch or select"); |
| 1286 | |
| 1287 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 1288 | if (!ProfileData || ProfileData->getNumOperands() != 3) |
| 1289 | return false; |
| 1290 | |
| 1291 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); |
| 1292 | if (!ProfDataName || !ProfDataName->getString().equals("branch_weights")) |
| 1293 | return false; |
| 1294 | |
| 1295 | auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)); |
| 1296 | auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2)); |
| 1297 | if (!CITrue || !CIFalse) |
| 1298 | return false; |
| 1299 | |
| 1300 | TrueVal = CITrue->getValue().getZExtValue(); |
| 1301 | FalseVal = CIFalse->getValue().getZExtValue(); |
| 1302 | |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
Dehao Chen | 9232f98 | 2016-07-11 16:48:54 +0000 | [diff] [blame] | 1306 | bool Instruction::extractProfTotalWeight(uint64_t &TotalVal) { |
| 1307 | assert((getOpcode() == Instruction::Br || |
| 1308 | getOpcode() == Instruction::Select || |
Dehao Chen | 71021cd | 2016-07-11 17:36:02 +0000 | [diff] [blame] | 1309 | getOpcode() == Instruction::Call || |
| 1310 | getOpcode() == Instruction::Invoke) && |
Dehao Chen | 9232f98 | 2016-07-11 16:48:54 +0000 | [diff] [blame] | 1311 | "Looking for branch weights on something besides branch"); |
| 1312 | |
| 1313 | TotalVal = 0; |
| 1314 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 1315 | if (!ProfileData) |
| 1316 | return false; |
| 1317 | |
| 1318 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); |
| 1319 | if (!ProfDataName || !ProfDataName->getString().equals("branch_weights")) |
| 1320 | return false; |
| 1321 | |
| 1322 | TotalVal = 0; |
David Majnemer | dd8f6bd | 2016-07-11 17:09:06 +0000 | [diff] [blame] | 1323 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) { |
Dehao Chen | 9232f98 | 2016-07-11 16:48:54 +0000 | [diff] [blame] | 1324 | auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i)); |
| 1325 | if (!V) |
| 1326 | return false; |
| 1327 | TotalVal += V->getValue().getZExtValue(); |
| 1328 | } |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 1332 | void Instruction::clearMetadataHashEntries() { |
| 1333 | assert(hasMetadataHashEntry() && "Caller should check"); |
Duncan P. N. Exon Smith | 391fc56 | 2015-04-24 20:16:42 +0000 | [diff] [blame] | 1334 | getContext().pImpl->InstructionMetadata.erase(this); |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 1335 | setHasMetadataHashEntry(false); |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 1336 | } |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1337 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1338 | void GlobalObject::getMetadata(unsigned KindID, |
| 1339 | SmallVectorImpl<MDNode *> &MDs) const { |
| 1340 | if (hasMetadata()) |
| 1341 | getContext().pImpl->GlobalObjectMetadata[this].get(KindID, MDs); |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1344 | void GlobalObject::getMetadata(StringRef Kind, |
| 1345 | SmallVectorImpl<MDNode *> &MDs) const { |
| 1346 | if (hasMetadata()) |
| 1347 | getMetadata(getContext().getMDKindID(Kind), MDs); |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1350 | void GlobalObject::addMetadata(unsigned KindID, MDNode &MD) { |
| 1351 | if (!hasMetadata()) |
| 1352 | setHasMetadataHashEntry(true); |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1353 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1354 | getContext().pImpl->GlobalObjectMetadata[this].insert(KindID, MD); |
| 1355 | } |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1356 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1357 | void GlobalObject::addMetadata(StringRef Kind, MDNode &MD) { |
| 1358 | addMetadata(getContext().getMDKindID(Kind), MD); |
| 1359 | } |
| 1360 | |
| 1361 | void GlobalObject::eraseMetadata(unsigned KindID) { |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1362 | // Nothing to unset. |
| 1363 | if (!hasMetadata()) |
| 1364 | return; |
| 1365 | |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 1366 | auto &Store = getContext().pImpl->GlobalObjectMetadata[this]; |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1367 | Store.erase(KindID); |
| 1368 | if (Store.empty()) |
| 1369 | clearMetadata(); |
| 1370 | } |
| 1371 | |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 1372 | void GlobalObject::getAllMetadata( |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1373 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { |
| 1374 | MDs.clear(); |
| 1375 | |
| 1376 | if (!hasMetadata()) |
| 1377 | return; |
| 1378 | |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 1379 | getContext().pImpl->GlobalObjectMetadata[this].getAll(MDs); |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 1382 | void GlobalObject::clearMetadata() { |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1383 | if (!hasMetadata()) |
| 1384 | return; |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 1385 | getContext().pImpl->GlobalObjectMetadata.erase(this); |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 1386 | setHasMetadataHashEntry(false); |
| 1387 | } |
Duncan P. N. Exon Smith | b56b5af | 2015-08-28 21:55:35 +0000 | [diff] [blame] | 1388 | |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 1389 | void GlobalObject::setMetadata(unsigned KindID, MDNode *N) { |
| 1390 | eraseMetadata(KindID); |
| 1391 | if (N) |
| 1392 | addMetadata(KindID, *N); |
| 1393 | } |
| 1394 | |
| 1395 | void GlobalObject::setMetadata(StringRef Kind, MDNode *N) { |
| 1396 | setMetadata(getContext().getMDKindID(Kind), N); |
| 1397 | } |
| 1398 | |
| 1399 | MDNode *GlobalObject::getMetadata(unsigned KindID) const { |
| 1400 | SmallVector<MDNode *, 1> MDs; |
| 1401 | getMetadata(KindID, MDs); |
| 1402 | assert(MDs.size() <= 1 && "Expected at most one metadata attachment"); |
| 1403 | if (MDs.empty()) |
| 1404 | return nullptr; |
| 1405 | return MDs[0]; |
| 1406 | } |
| 1407 | |
| 1408 | MDNode *GlobalObject::getMetadata(StringRef Kind) const { |
| 1409 | return getMetadata(getContext().getMDKindID(Kind)); |
| 1410 | } |
| 1411 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1412 | void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) { |
Peter Collingbourne | 4f7c16d | 2016-06-24 17:42:21 +0000 | [diff] [blame] | 1413 | SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
| 1414 | Other->getAllMetadata(MDs); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1415 | for (auto &MD : MDs) { |
| 1416 | // We need to adjust the type metadata offset. |
| 1417 | if (Offset != 0 && MD.first == LLVMContext::MD_type) { |
| 1418 | auto *OffsetConst = cast<ConstantInt>( |
| 1419 | cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue()); |
| 1420 | Metadata *TypeId = MD.second->getOperand(1); |
| 1421 | auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get( |
| 1422 | OffsetConst->getType(), OffsetConst->getValue() + Offset)); |
| 1423 | addMetadata(LLVMContext::MD_type, |
| 1424 | *MDNode::get(getContext(), {NewOffsetMD, TypeId})); |
| 1425 | continue; |
| 1426 | } |
Peter Collingbourne | 4f7c16d | 2016-06-24 17:42:21 +0000 | [diff] [blame] | 1427 | addMetadata(MD.first, *MD.second); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | void GlobalObject::addTypeMetadata(unsigned Offset, Metadata *TypeID) { |
| 1432 | addMetadata( |
| 1433 | LLVMContext::MD_type, |
| 1434 | *MDTuple::get(getContext(), |
| 1435 | {llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 1436 | Type::getInt64Ty(getContext()), Offset)), |
| 1437 | TypeID})); |
Peter Collingbourne | 4f7c16d | 2016-06-24 17:42:21 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Duncan P. N. Exon Smith | b56b5af | 2015-08-28 21:55:35 +0000 | [diff] [blame] | 1440 | void Function::setSubprogram(DISubprogram *SP) { |
| 1441 | setMetadata(LLVMContext::MD_dbg, SP); |
| 1442 | } |
| 1443 | |
| 1444 | DISubprogram *Function::getSubprogram() const { |
| 1445 | return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg)); |
| 1446 | } |