Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 1 | //===-- Metadata.cpp - Implement Metadata classes -------------------------===// |
| 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "SymbolTableListTraitsImpl.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instruction.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 4b6845c | 2014-03-04 12:46:06 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LeakDetector.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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 | |
| 41 | /// \brief Canonicalize metadata arguments to intrinsics. |
| 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; |
| 86 | auto I = Store.find(MD); |
| 87 | return I == Store.end() ? nullptr : I->second; |
| 88 | } |
| 89 | |
| 90 | void MetadataAsValue::handleChangedMetadata(Metadata *MD) { |
| 91 | LLVMContext &Context = getContext(); |
| 92 | MD = canonicalizeMetadataForValue(Context, MD); |
| 93 | auto &Store = Context.pImpl->MetadataAsValues; |
| 94 | |
| 95 | // Stop tracking the old metadata. |
| 96 | Store.erase(this->MD); |
| 97 | untrack(); |
| 98 | this->MD = nullptr; |
| 99 | |
| 100 | // Start tracking MD, or RAUW if necessary. |
| 101 | auto *&Entry = Store[MD]; |
| 102 | if (Entry) { |
| 103 | replaceAllUsesWith(Entry); |
| 104 | delete this; |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | this->MD = MD; |
| 109 | track(); |
| 110 | Entry = this; |
| 111 | } |
| 112 | |
| 113 | void MetadataAsValue::track() { |
| 114 | if (MD) |
| 115 | MetadataTracking::track(&MD, *MD, *this); |
| 116 | } |
| 117 | |
| 118 | void MetadataAsValue::untrack() { |
| 119 | if (MD) |
| 120 | MetadataTracking::untrack(MD); |
| 121 | } |
| 122 | |
| 123 | void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { |
| 124 | bool WasInserted = UseMap.insert(std::make_pair(Ref, Owner)).second; |
| 125 | (void)WasInserted; |
| 126 | assert(WasInserted && "Expected to add a reference"); |
| 127 | } |
| 128 | |
| 129 | void ReplaceableMetadataImpl::dropRef(void *Ref) { |
| 130 | bool WasErased = UseMap.erase(Ref); |
| 131 | (void)WasErased; |
| 132 | assert(WasErased && "Expected to drop a reference"); |
| 133 | } |
| 134 | |
| 135 | void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, |
| 136 | const Metadata &MD) { |
| 137 | auto I = UseMap.find(Ref); |
| 138 | assert(I != UseMap.end() && "Expected to move a reference"); |
| 139 | OwnerTy Owner = I->second; |
| 140 | UseMap.erase(I); |
| 141 | addRef(New, Owner); |
| 142 | |
| 143 | // Check that the references are direct if there's no owner. |
| 144 | (void)MD; |
| 145 | assert((Owner || *static_cast<Metadata **>(Ref) == &MD) && |
| 146 | "Reference without owner must be direct"); |
| 147 | assert((Owner || *static_cast<Metadata **>(New) == &MD) && |
| 148 | "Reference without owner must be direct"); |
| 149 | } |
| 150 | |
| 151 | void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { |
| 152 | assert(!(MD && isa<MDNodeFwdDecl>(MD)) && "Expected non-temp node"); |
| 153 | |
| 154 | if (UseMap.empty()) |
| 155 | return; |
| 156 | |
| 157 | // Copy out uses since UseMap will get touched below. |
| 158 | SmallVector<std::pair<void *, OwnerTy>, 8> Uses(UseMap.begin(), UseMap.end()); |
| 159 | for (const auto &Pair : Uses) { |
| 160 | if (!Pair.second) { |
| 161 | // Update unowned tracking references directly. |
| 162 | Metadata *&Ref = *static_cast<Metadata **>(Pair.first); |
| 163 | Ref = MD; |
| 164 | MetadataTracking::track(Ref); |
| 165 | UseMap.erase(Pair.first); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // Check for MetadataAsValue. |
| 170 | if (Pair.second.is<MetadataAsValue *>()) { |
| 171 | Pair.second.get<MetadataAsValue *>()->handleChangedMetadata(MD); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | // There's a Metadata owner -- dispatch. |
| 176 | Metadata *Owner = Pair.second.get<Metadata *>(); |
| 177 | switch (Owner->getMetadataID()) { |
| 178 | #define HANDLE_METADATA_LEAF(CLASS) \ |
| 179 | case Metadata::CLASS##Kind: \ |
| 180 | cast<CLASS>(Owner)->handleChangedOperand(Pair.first, MD); \ |
| 181 | continue; |
| 182 | #include "llvm/IR/Metadata.def" |
| 183 | default: |
| 184 | llvm_unreachable("Invalid metadata subclass"); |
| 185 | } |
| 186 | } |
| 187 | assert(UseMap.empty() && "Expected all uses to be replaced"); |
| 188 | } |
| 189 | |
| 190 | void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { |
| 191 | if (UseMap.empty()) |
| 192 | return; |
| 193 | |
| 194 | if (!ResolveUsers) { |
| 195 | UseMap.clear(); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Copy out uses since UseMap could get touched below. |
| 200 | SmallVector<std::pair<void *, OwnerTy>, 8> Uses(UseMap.begin(), UseMap.end()); |
| 201 | UseMap.clear(); |
| 202 | for (const auto &Pair : Uses) { |
| 203 | if (!Pair.second) |
| 204 | continue; |
| 205 | if (Pair.second.is<MetadataAsValue *>()) |
| 206 | continue; |
| 207 | |
| 208 | // Resolve GenericMDNodes that point at this. |
| 209 | auto *Owner = dyn_cast<GenericMDNode>(Pair.second.get<Metadata *>()); |
| 210 | if (!Owner) |
| 211 | continue; |
| 212 | if (Owner->isResolved()) |
| 213 | continue; |
| 214 | Owner->decrementUnresolvedOperands(); |
| 215 | if (!Owner->hasUnresolvedOperands()) |
| 216 | Owner->resolve(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static Function *getLocalFunction(Value *V) { |
| 221 | assert(V && "Expected value"); |
| 222 | if (auto *A = dyn_cast<Argument>(V)) |
| 223 | return A->getParent(); |
| 224 | if (BasicBlock *BB = cast<Instruction>(V)->getParent()) |
| 225 | return BB->getParent(); |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | ValueAsMetadata *ValueAsMetadata::get(Value *V) { |
| 230 | assert(V && "Unexpected null Value"); |
| 231 | |
| 232 | auto &Context = V->getContext(); |
| 233 | auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; |
| 234 | if (!Entry) { |
| 235 | assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && |
| 236 | "Expected constant or function-local value"); |
| 237 | assert(!V->NameAndIsUsedByMD.getInt() && |
| 238 | "Expected this to be the only metadata use"); |
| 239 | V->NameAndIsUsedByMD.setInt(true); |
| 240 | if (auto *C = dyn_cast<Constant>(V)) |
| 241 | Entry = new ConstantAsMetadata(Context, C); |
| 242 | else |
| 243 | Entry = new LocalAsMetadata(Context, V); |
| 244 | } |
| 245 | |
| 246 | return Entry; |
| 247 | } |
| 248 | |
| 249 | ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { |
| 250 | assert(V && "Unexpected null Value"); |
| 251 | return V->getContext().pImpl->ValuesAsMetadata.lookup(V); |
| 252 | } |
| 253 | |
| 254 | void ValueAsMetadata::handleDeletion(Value *V) { |
| 255 | assert(V && "Expected valid value"); |
| 256 | |
| 257 | auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; |
| 258 | auto I = Store.find(V); |
| 259 | if (I == Store.end()) |
| 260 | return; |
| 261 | |
| 262 | // Remove old entry from the map. |
| 263 | ValueAsMetadata *MD = I->second; |
| 264 | assert(MD && "Expected valid metadata"); |
| 265 | assert(MD->getValue() == V && "Expected valid mapping"); |
| 266 | Store.erase(I); |
| 267 | |
| 268 | // Delete the metadata. |
| 269 | MD->replaceAllUsesWith(nullptr); |
| 270 | delete MD; |
| 271 | } |
| 272 | |
| 273 | void ValueAsMetadata::handleRAUW(Value *From, Value *To) { |
| 274 | assert(From && "Expected valid value"); |
| 275 | assert(To && "Expected valid value"); |
| 276 | assert(From != To && "Expected changed value"); |
| 277 | assert(From->getType() == To->getType() && "Unexpected type change"); |
| 278 | |
| 279 | LLVMContext &Context = From->getType()->getContext(); |
| 280 | auto &Store = Context.pImpl->ValuesAsMetadata; |
| 281 | auto I = Store.find(From); |
| 282 | if (I == Store.end()) { |
| 283 | assert(!From->NameAndIsUsedByMD.getInt() && |
| 284 | "Expected From not to be used by metadata"); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | // Remove old entry from the map. |
| 289 | assert(From->NameAndIsUsedByMD.getInt() && |
| 290 | "Expected From to be used by metadata"); |
| 291 | From->NameAndIsUsedByMD.setInt(false); |
| 292 | ValueAsMetadata *MD = I->second; |
| 293 | assert(MD && "Expected valid metadata"); |
| 294 | assert(MD->getValue() == From && "Expected valid mapping"); |
| 295 | Store.erase(I); |
| 296 | |
| 297 | if (isa<LocalAsMetadata>(MD)) { |
| 298 | if (auto *C = dyn_cast<Constant>(To)) { |
| 299 | // Local became a constant. |
| 300 | MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); |
| 301 | delete MD; |
| 302 | return; |
| 303 | } |
| 304 | if (getLocalFunction(From) && getLocalFunction(To) && |
| 305 | getLocalFunction(From) != getLocalFunction(To)) { |
| 306 | // Function changed. |
| 307 | MD->replaceAllUsesWith(nullptr); |
| 308 | delete MD; |
| 309 | return; |
| 310 | } |
| 311 | } else if (!isa<Constant>(To)) { |
| 312 | // Changed to function-local value. |
| 313 | MD->replaceAllUsesWith(nullptr); |
| 314 | delete MD; |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | auto *&Entry = Store[To]; |
| 319 | if (Entry) { |
| 320 | // The target already exists. |
| 321 | MD->replaceAllUsesWith(Entry); |
| 322 | delete MD; |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Update MD in place (and update the map entry). |
| 327 | assert(!To->NameAndIsUsedByMD.getInt() && |
| 328 | "Expected this to be the only metadata use"); |
| 329 | To->NameAndIsUsedByMD.setInt(true); |
| 330 | MD->V = To; |
| 331 | Entry = MD; |
| 332 | } |
Duncan P. N. Exon Smith | a69934f | 2014-11-14 18:42:09 +0000 | [diff] [blame] | 333 | |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 334 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 335 | // MDString implementation. |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 336 | // |
Chris Lattner | 5a409bd | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 337 | |
Devang Patel | dcb99d3 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 338 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 339 | auto &Store = Context.pImpl->MDStringCache; |
| 340 | auto I = Store.find(Str); |
| 341 | if (I != Store.end()) |
| 342 | return &I->second; |
| 343 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 344 | auto *Entry = StringMapEntry<MDString>::Create(Str, Store.getAllocator()); |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 345 | bool WasInserted = Store.insert(Entry); |
| 346 | (void)WasInserted; |
| 347 | assert(WasInserted && "Expected entry to be inserted"); |
Duncan P. N. Exon Smith | c1a664f | 2014-12-05 01:41:34 +0000 | [diff] [blame] | 348 | Entry->second.Entry = Entry; |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 349 | return &Entry->second; |
| 350 | } |
| 351 | |
| 352 | StringRef MDString::getString() const { |
Duncan P. N. Exon Smith | c1a664f | 2014-12-05 01:41:34 +0000 | [diff] [blame] | 353 | assert(Entry && "Expected to find string map entry"); |
| 354 | return Entry->first(); |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 358 | // MDNode implementation. |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 359 | // |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 360 | |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 361 | void *MDNode::operator new(size_t Size, unsigned NumOps) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 362 | void *Ptr = ::operator new(Size + NumOps * sizeof(MDOperand)); |
| 363 | MDOperand *First = new (Ptr) MDOperand[NumOps]; |
| 364 | return First + NumOps; |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void MDNode::operator delete(void *Mem) { |
| 368 | MDNode *N = static_cast<MDNode *>(Mem); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 369 | MDOperand *Last = static_cast<MDOperand *>(Mem); |
| 370 | ::operator delete(Last - N->NumOperands); |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 373 | MDNode::MDNode(LLVMContext &Context, unsigned ID, ArrayRef<Metadata *> MDs) |
| 374 | : Metadata(ID), Context(Context), NumOperands(MDs.size()), |
| 375 | MDNodeSubclassData(0) { |
| 376 | for (unsigned I = 0, E = MDs.size(); I != E; ++I) |
| 377 | setOperand(I, MDs[I]); |
| 378 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 379 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 380 | bool MDNode::isResolved() const { |
| 381 | if (isa<MDNodeFwdDecl>(this)) |
| 382 | return false; |
| 383 | return cast<GenericMDNode>(this)->isResolved(); |
| 384 | } |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 385 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 386 | static bool isOperandUnresolved(Metadata *Op) { |
| 387 | if (auto *N = dyn_cast_or_null<MDNode>(Op)) |
| 388 | return !N->isResolved(); |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | GenericMDNode::GenericMDNode(LLVMContext &C, ArrayRef<Metadata *> Vals) |
| 393 | : MDNode(C, GenericMDNodeKind, Vals) { |
| 394 | // Check whether any operands are unresolved, requiring re-uniquing. |
| 395 | for (const auto &Op : operands()) |
| 396 | if (isOperandUnresolved(Op)) |
| 397 | incrementUnresolvedOperands(); |
| 398 | |
| 399 | if (hasUnresolvedOperands()) |
| 400 | ReplaceableUses.reset(new ReplaceableMetadataImpl); |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 403 | GenericMDNode::~GenericMDNode() { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 404 | LLVMContextImpl *pImpl = getContext().pImpl; |
| 405 | if (isStoredDistinctInContext()) |
Jeffrey Yasskin | 2cc2476 | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 406 | pImpl->NonUniquedMDNodes.erase(this); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 407 | else |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 408 | pImpl->MDNodeSet.erase(this); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 409 | } |
| 410 | |
| 411 | void GenericMDNode::resolve() { |
| 412 | assert(!isResolved() && "Expected this to be unresolved"); |
| 413 | |
| 414 | // Move the map, so that this immediately looks resolved. |
| 415 | auto Uses = std::move(ReplaceableUses); |
| 416 | SubclassData32 = 0; |
| 417 | assert(isResolved() && "Expected this to be resolved"); |
| 418 | |
| 419 | // Drop RAUW support. |
| 420 | Uses->resolveAllUses(); |
| 421 | } |
| 422 | |
| 423 | void GenericMDNode::resolveCycles() { |
| 424 | if (isResolved()) |
| 425 | return; |
| 426 | |
| 427 | // Resolve this node immediately. |
| 428 | resolve(); |
| 429 | |
| 430 | // Resolve all operands. |
| 431 | for (const auto &Op : operands()) { |
| 432 | if (!Op) |
| 433 | continue; |
| 434 | assert(!isa<MDNodeFwdDecl>(Op) && |
| 435 | "Expected all forward declarations to be resolved"); |
| 436 | if (auto *N = dyn_cast<GenericMDNode>(Op)) |
| 437 | if (!N->isResolved()) |
| 438 | N->resolveCycles(); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 439 | } |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 442 | void MDNode::dropAllReferences() { |
| 443 | for (unsigned I = 0, E = NumOperands; I != E; ++I) |
| 444 | setOperand(I, nullptr); |
| 445 | if (auto *G = dyn_cast<GenericMDNode>(this)) |
| 446 | if (!G->isResolved()) { |
| 447 | G->ReplaceableUses->resolveAllUses(/* ResolveUsers */ false); |
| 448 | G->ReplaceableUses.reset(); |
| 449 | } |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 452 | namespace llvm { |
| 453 | /// \brief Make MDOperand transparent for hashing. |
| 454 | /// |
| 455 | /// This overload of an implementation detail of the hashing library makes |
| 456 | /// MDOperand hash to the same value as a \a Metadata pointer. |
| 457 | /// |
| 458 | /// Note that overloading \a hash_value() as follows: |
| 459 | /// |
| 460 | /// \code |
| 461 | /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } |
| 462 | /// \endcode |
| 463 | /// |
| 464 | /// does not cause MDOperand to be transparent. In particular, a bare pointer |
| 465 | /// doesn't get hashed before it's combined, whereas \a MDOperand would. |
| 466 | static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } |
| 467 | } |
| 468 | |
| 469 | void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { |
| 470 | unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); |
| 471 | assert(Op < getNumOperands() && "Expected valid operand"); |
| 472 | |
| 473 | if (isStoredDistinctInContext()) { |
| 474 | assert(isResolved() && "Expected distinct node to be resolved"); |
| 475 | |
| 476 | // This node is not uniqued. Just set the operand and be done with it. |
| 477 | setOperand(Op, New); |
| 478 | return; |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 479 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 480 | |
| 481 | auto &Store = getContext().pImpl->MDNodeSet; |
| 482 | Store.erase(this); |
| 483 | |
| 484 | Metadata *Old = getOperand(Op); |
| 485 | setOperand(Op, New); |
| 486 | |
| 487 | // Drop uniquing for self-reference cycles or if an operand drops to null. |
| 488 | // |
| 489 | // FIXME: Stop dropping uniquing when an operand drops to null. The original |
| 490 | // motivation was to prevent madness during teardown of LLVMContextImpl, but |
| 491 | // dropAllReferences() fixes that problem in a better way. (It's just here |
| 492 | // now for better staging of semantic changes.) |
| 493 | if (New == this || !New) { |
| 494 | storeDistinctInContext(); |
| 495 | setHash(0); |
| 496 | if (!isResolved()) |
| 497 | resolve(); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | // Re-calculate the hash. |
| 502 | setHash(hash_combine_range(op_begin(), op_end())); |
| 503 | #ifndef NDEBUG |
| 504 | { |
| 505 | SmallVector<Metadata *, 8> MDs(op_begin(), op_end()); |
| 506 | unsigned RawHash = hash_combine_range(MDs.begin(), MDs.end()); |
| 507 | assert(getHash() == RawHash && |
| 508 | "Expected hash of MDOperand to equal hash of Metadata*"); |
| 509 | } |
| 510 | #endif |
| 511 | |
| 512 | // Re-unique the node. |
| 513 | GenericMDNodeInfo::KeyTy Key(this); |
| 514 | auto I = Store.find_as(Key); |
| 515 | if (I == Store.end()) { |
| 516 | Store.insert(this); |
| 517 | |
| 518 | if (!isResolved()) { |
| 519 | // Check if the last unresolved operand has just been resolved; if so, |
| 520 | // resolve this as well. |
| 521 | if (isOperandUnresolved(Old)) |
| 522 | decrementUnresolvedOperands(); |
| 523 | if (isOperandUnresolved(New)) |
| 524 | incrementUnresolvedOperands(); |
| 525 | if (!hasUnresolvedOperands()) |
| 526 | resolve(); |
| 527 | } |
| 528 | |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | // Collision. |
| 533 | if (!isResolved()) { |
| 534 | // Still unresolved, so RAUW. |
| 535 | ReplaceableUses->replaceAllUsesWith(*I); |
| 536 | delete this; |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Store in non-uniqued form if this node has already been resolved. |
| 541 | setHash(0); |
| 542 | storeDistinctInContext(); |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 545 | MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Metadata *> MDs, |
| 546 | bool Insert) { |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 547 | auto &Store = Context.pImpl->MDNodeSet; |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 548 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 549 | GenericMDNodeInfo::KeyTy Key(MDs); |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 550 | auto I = Store.find_as(Key); |
| 551 | if (I != Store.end()) |
| 552 | return *I; |
| 553 | if (!Insert) |
| 554 | return nullptr; |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 555 | |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 556 | // Coallocate space for the node and Operands together, then placement new. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 557 | GenericMDNode *N = new (MDs.size()) GenericMDNode(Context, MDs); |
| 558 | N->setHash(Key.Hash); |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 559 | Store.insert(N); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 560 | return N; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 563 | MDNodeFwdDecl *MDNode::getTemporary(LLVMContext &Context, |
| 564 | ArrayRef<Metadata *> MDs) { |
| 565 | MDNodeFwdDecl *N = new (MDs.size()) MDNodeFwdDecl(Context, MDs); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 566 | LeakDetector::addGarbageObject(N); |
| 567 | return N; |
| 568 | } |
| 569 | |
| 570 | void MDNode::deleteTemporary(MDNode *N) { |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 571 | assert(isa<MDNodeFwdDecl>(N) && "Expected forward declaration"); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 572 | LeakDetector::removeGarbageObject(N); |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 573 | delete cast<MDNodeFwdDecl>(N); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 576 | void MDNode::storeDistinctInContext() { |
| 577 | assert(!IsDistinctInContext && "Expected newly distinct metadata"); |
| 578 | IsDistinctInContext = true; |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 579 | auto *G = cast<GenericMDNode>(this); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 580 | G->setHash(0); |
| 581 | getContext().pImpl->NonUniquedMDNodes.insert(G); |
Devang Patel | 82ab3f8 | 2010-02-18 20:53:16 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 584 | // Replace value from this node's operand list. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 585 | void MDNode::replaceOperandWith(unsigned I, Metadata *New) { |
| 586 | if (getOperand(I) == New) |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 587 | return; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 588 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 589 | if (auto *N = dyn_cast<GenericMDNode>(this)) { |
| 590 | N->handleChangedOperand(mutable_begin() + I, New); |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 591 | return; |
| 592 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 593 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 594 | assert(isa<MDNodeFwdDecl>(this) && "Expected an MDNode"); |
| 595 | setOperand(I, New); |
| 596 | } |
Chris Lattner | c6d17e2 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 597 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 598 | void MDNode::setOperand(unsigned I, Metadata *New) { |
| 599 | assert(I < NumOperands); |
| 600 | if (isStoredDistinctInContext() || isa<MDNodeFwdDecl>(this)) |
| 601 | // No need for a callback, this isn't uniqued. |
| 602 | mutable_begin()[I].reset(New, nullptr); |
| 603 | else |
| 604 | mutable_begin()[I].reset(New, this); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 607 | /// \brief Get a node, or a self-reference that looks like it. |
| 608 | /// |
| 609 | /// Special handling for finding self-references, for use by \a |
| 610 | /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from |
| 611 | /// when self-referencing nodes were still uniqued. If the first operand has |
| 612 | /// 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^] | 613 | static MDNode *getOrSelfReference(LLVMContext &Context, |
| 614 | ArrayRef<Metadata *> Ops) { |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 615 | if (!Ops.empty()) |
| 616 | if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0])) |
| 617 | if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { |
| 618 | for (unsigned I = 1, E = Ops.size(); I != E; ++I) |
| 619 | if (Ops[I] != N->getOperand(I)) |
| 620 | return MDNode::get(Context, Ops); |
| 621 | return N; |
| 622 | } |
| 623 | |
| 624 | return MDNode::get(Context, Ops); |
| 625 | } |
| 626 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 627 | MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { |
| 628 | if (!A) |
| 629 | return B; |
| 630 | if (!B) |
| 631 | return A; |
| 632 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 633 | SmallVector<Metadata *, 4> MDs(A->getNumOperands() + B->getNumOperands()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 634 | |
| 635 | unsigned j = 0; |
| 636 | for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 637 | MDs[j++] = A->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 639 | MDs[j++] = B->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 640 | |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 641 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 642 | // behaviour? Or was that an unintended side-effect of node uniquing? |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 643 | return getOrSelfReference(A->getContext(), MDs); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | MDNode *MDNode::intersect(MDNode *A, MDNode *B) { |
| 647 | if (!A || !B) |
| 648 | return nullptr; |
| 649 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 650 | SmallVector<Metadata *, 4> MDs; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 651 | for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 652 | Metadata *MD = A->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 653 | for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 654 | if (MD == B->getOperand(j)) { |
| 655 | MDs.push_back(MD); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | } |
| 659 | |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 660 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 661 | // behaviour? Or was that an unintended side-effect of node uniquing? |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 662 | return getOrSelfReference(A->getContext(), MDs); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 665 | MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { |
| 666 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 667 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 668 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 669 | APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF(); |
| 670 | APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 671 | if (AVal.compare(BVal) == APFloat::cmpLessThan) |
| 672 | return A; |
| 673 | return B; |
| 674 | } |
| 675 | |
| 676 | static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { |
| 677 | return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); |
| 678 | } |
| 679 | |
| 680 | static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { |
| 681 | return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); |
| 682 | } |
| 683 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 684 | static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 685 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 686 | ConstantRange NewRange(Low->getValue(), High->getValue()); |
| 687 | unsigned Size = EndPoints.size(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 688 | APInt LB = EndPoints[Size - 2]->getValue(); |
| 689 | APInt LE = EndPoints[Size - 1]->getValue(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 690 | ConstantRange LastRange(LB, LE); |
| 691 | if (canBeMerged(NewRange, LastRange)) { |
| 692 | ConstantRange Union = LastRange.unionWith(NewRange); |
| 693 | Type *Ty = High->getType(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 694 | EndPoints[Size - 2] = |
| 695 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower())); |
| 696 | EndPoints[Size - 1] = |
| 697 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper())); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 698 | return true; |
| 699 | } |
| 700 | return false; |
| 701 | } |
| 702 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 703 | static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 704 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 705 | if (!EndPoints.empty()) |
| 706 | if (tryMergeRange(EndPoints, Low, High)) |
| 707 | return; |
| 708 | |
| 709 | EndPoints.push_back(Low); |
| 710 | EndPoints.push_back(High); |
| 711 | } |
| 712 | |
| 713 | MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { |
| 714 | // Given two ranges, we want to compute the union of the ranges. This |
| 715 | // is slightly complitade by having to combine the intervals and merge |
| 716 | // the ones that overlap. |
| 717 | |
| 718 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 719 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 720 | |
| 721 | if (A == B) |
| 722 | return A; |
| 723 | |
| 724 | // First, walk both lists in older of the lower boundary of each interval. |
| 725 | // 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^] | 726 | SmallVector<ConstantInt *, 4> EndPoints; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 727 | int AI = 0; |
| 728 | int BI = 0; |
| 729 | int AN = A->getNumOperands() / 2; |
| 730 | int BN = B->getNumOperands() / 2; |
| 731 | while (AI < AN && BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 732 | ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI)); |
| 733 | ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI)); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 734 | |
| 735 | if (ALow->getValue().slt(BLow->getValue())) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 736 | addRange(EndPoints, ALow, |
| 737 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 738 | ++AI; |
| 739 | } else { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 740 | addRange(EndPoints, BLow, |
| 741 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 742 | ++BI; |
| 743 | } |
| 744 | } |
| 745 | while (AI < AN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 746 | addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)), |
| 747 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 748 | ++AI; |
| 749 | } |
| 750 | while (BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 751 | addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)), |
| 752 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 753 | ++BI; |
| 754 | } |
| 755 | |
| 756 | // If we have more than 2 ranges (4 endpoints) we have to try to merge |
| 757 | // the last and first ones. |
| 758 | unsigned Size = EndPoints.size(); |
| 759 | if (Size > 4) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 760 | ConstantInt *FB = EndPoints[0]; |
| 761 | ConstantInt *FE = EndPoints[1]; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 762 | if (tryMergeRange(EndPoints, FB, FE)) { |
| 763 | for (unsigned i = 0; i < Size - 2; ++i) { |
| 764 | EndPoints[i] = EndPoints[i + 2]; |
| 765 | } |
| 766 | EndPoints.resize(Size - 2); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // If in the end we have a single range, it is possible that it is now the |
| 771 | // full range. Just drop the metadata in that case. |
| 772 | if (EndPoints.size() == 2) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 773 | ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 774 | if (Range.isFullSet()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 775 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 778 | SmallVector<Metadata *, 4> MDs; |
| 779 | MDs.reserve(EndPoints.size()); |
| 780 | for (auto *I : EndPoints) |
| 781 | MDs.push_back(ConstantAsMetadata::get(I)); |
| 782 | return MDNode::get(A->getContext(), MDs); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 785 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 786 | // NamedMDNode implementation. |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 787 | // |
Devang Patel | 943ddf6 | 2010-01-12 18:34:06 +0000 | [diff] [blame] | 788 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 789 | static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) { |
| 790 | return *(SmallVector<TrackingMDRef, 4> *)Operands; |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 793 | NamedMDNode::NamedMDNode(const Twine &N) |
Duncan P. N. Exon Smith | c5754a6 | 2014-11-05 18:16:03 +0000 | [diff] [blame] | 794 | : Name(N.str()), Parent(nullptr), |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 795 | Operands(new SmallVector<TrackingMDRef, 4>()) {} |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 797 | NamedMDNode::~NamedMDNode() { |
| 798 | dropAllReferences(); |
| 799 | delete &getNMDOps(Operands); |
| 800 | } |
| 801 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 802 | unsigned NamedMDNode::getNumOperands() const { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 803 | return (unsigned)getNMDOps(Operands).size(); |
| 804 | } |
| 805 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 806 | MDNode *NamedMDNode::getOperand(unsigned i) const { |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 807 | assert(i < getNumOperands() && "Invalid Operand number!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 808 | auto *N = getNMDOps(Operands)[i].get(); |
| 809 | if (N && i > 10000) |
| 810 | N->dump(); |
| 811 | return cast_or_null<MDNode>(N); |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 814 | void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 815 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 816 | void NamedMDNode::eraseFromParent() { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 817 | getParent()->eraseNamedMetadata(this); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 820 | void NamedMDNode::dropAllReferences() { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 821 | getNMDOps(Operands).clear(); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 824 | StringRef NamedMDNode::getName() const { |
| 825 | return StringRef(Name); |
| 826 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 827 | |
| 828 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 829 | // Instruction Metadata method implementations. |
| 830 | // |
| 831 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 832 | void Instruction::setMetadata(StringRef Kind, MDNode *Node) { |
| 833 | if (!Node && !hasMetadata()) |
| 834 | return; |
| 835 | setMetadata(getContext().getMDKindID(Kind), Node); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 838 | MDNode *Instruction::getMetadataImpl(StringRef Kind) const { |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 839 | return getMetadataImpl(getContext().getMDKindID(Kind)); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 842 | void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { |
| 843 | SmallSet<unsigned, 5> KnownSet; |
| 844 | KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); |
| 845 | |
| 846 | // Drop debug if needed |
| 847 | if (KnownSet.erase(LLVMContext::MD_dbg)) |
| 848 | DbgLoc = DebugLoc(); |
| 849 | |
| 850 | if (!hasMetadataHashEntry()) |
| 851 | return; // Nothing to remove! |
| 852 | |
| 853 | DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore = |
| 854 | getContext().pImpl->MetadataStore; |
| 855 | |
| 856 | if (KnownSet.empty()) { |
| 857 | // Just drop our entry at the store. |
| 858 | MetadataStore.erase(this); |
| 859 | setHasMetadataHashEntry(false); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | LLVMContextImpl::MDMapTy &Info = MetadataStore[this]; |
| 864 | unsigned I; |
| 865 | unsigned E; |
| 866 | // Walk the array and drop any metadata we don't know. |
| 867 | for (I = 0, E = Info.size(); I != E;) { |
| 868 | if (KnownSet.count(Info[I].first)) { |
| 869 | ++I; |
| 870 | continue; |
| 871 | } |
| 872 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 873 | Info[I] = std::move(Info.back()); |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 874 | Info.pop_back(); |
| 875 | --E; |
| 876 | } |
| 877 | assert(E == Info.size()); |
| 878 | |
| 879 | if (E == 0) { |
| 880 | // Drop our entry at the store. |
| 881 | MetadataStore.erase(this); |
| 882 | setHasMetadataHashEntry(false); |
| 883 | } |
| 884 | } |
| 885 | |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 886 | /// setMetadata - Set the metadata of of the specified kind to the specified |
| 887 | /// node. This updates/replaces metadata if already present, or removes it if |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 888 | /// Node is null. |
| 889 | void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
| 890 | if (!Node && !hasMetadata()) |
| 891 | return; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 892 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 893 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 894 | if (KindID == LLVMContext::MD_dbg) { |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 895 | DbgLoc = DebugLoc::getFromDILocation(Node); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 896 | return; |
| 897 | } |
| 898 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 899 | // Handle the case when we're adding/updating metadata on an instruction. |
| 900 | if (Node) { |
| 901 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 902 | assert(!Info.empty() == hasMetadataHashEntry() && |
| 903 | "HasMetadata bit is wonked"); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 904 | if (Info.empty()) { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 905 | setHasMetadataHashEntry(true); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 906 | } else { |
| 907 | // Handle replacement of an existing value. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 908 | for (auto &P : Info) |
| 909 | if (P.first == KindID) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 910 | P.second.reset(Node); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 914 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 915 | // No replacement, just add it to the list. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 916 | Info.emplace_back(std::piecewise_construct, std::make_tuple(KindID), |
| 917 | std::make_tuple(Node)); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 918 | return; |
| 919 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 920 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 921 | // Otherwise, we're removing metadata from an instruction. |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 922 | assert((hasMetadataHashEntry() == |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 923 | (getContext().pImpl->MetadataStore.count(this) > 0)) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 924 | "HasMetadata bit out of date!"); |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 925 | if (!hasMetadataHashEntry()) |
| 926 | return; // Nothing to remove! |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 927 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 928 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 929 | // Common case is removing the only entry. |
| 930 | if (Info.size() == 1 && Info[0].first == KindID) { |
| 931 | getContext().pImpl->MetadataStore.erase(this); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 932 | setHasMetadataHashEntry(false); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 935 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 936 | // Handle removal of an existing value. |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 937 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 938 | if (Info[i].first == KindID) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 939 | Info[i] = std::move(Info.back()); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 940 | Info.pop_back(); |
| 941 | assert(!Info.empty() && "Removing last entry should be handled above"); |
| 942 | return; |
| 943 | } |
| 944 | // Otherwise, removing an entry that doesn't exist on the instruction. |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 947 | void Instruction::setAAMetadata(const AAMDNodes &N) { |
| 948 | setMetadata(LLVMContext::MD_tbaa, N.TBAA); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 949 | setMetadata(LLVMContext::MD_alias_scope, N.Scope); |
| 950 | setMetadata(LLVMContext::MD_noalias, N.NoAlias); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 953 | MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 954 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 955 | if (KindID == LLVMContext::MD_dbg) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 956 | return DbgLoc.getAsMDNode(); |
| 957 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 958 | if (!hasMetadataHashEntry()) return nullptr; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 959 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 960 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 961 | assert(!Info.empty() && "bit out of sync with hash table"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 962 | |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 963 | for (const auto &I : Info) |
| 964 | if (I.first == KindID) |
| 965 | return I.second; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 966 | return nullptr; |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Duncan P. N. Exon Smith | 4abd1a0 | 2014-11-01 00:26:42 +0000 | [diff] [blame] | 969 | void Instruction::getAllMetadataImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 970 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 971 | Result.clear(); |
| 972 | |
| 973 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 974 | if (!DbgLoc.isUnknown()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 975 | Result.push_back( |
| 976 | std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 977 | if (!hasMetadataHashEntry()) return; |
| 978 | } |
| 979 | |
| 980 | assert(hasMetadataHashEntry() && |
| 981 | getContext().pImpl->MetadataStore.count(this) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 982 | "Shouldn't have called this"); |
| 983 | const LLVMContextImpl::MDMapTy &Info = |
| 984 | getContext().pImpl->MetadataStore.find(this)->second; |
| 985 | assert(!Info.empty() && "Shouldn't have called this"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 986 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 987 | Result.reserve(Result.size() + Info.size()); |
| 988 | for (auto &I : Info) |
| 989 | Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 990 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 991 | // Sort the resulting array so it is stable. |
| 992 | if (Result.size() > 1) |
| 993 | array_pod_sort(Result.begin(), Result.end()); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Duncan P. N. Exon Smith | 3d5a02f | 2014-11-03 18:13:57 +0000 | [diff] [blame] | 996 | void Instruction::getAllMetadataOtherThanDebugLocImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 997 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 998 | Result.clear(); |
| 999 | assert(hasMetadataHashEntry() && |
| 1000 | getContext().pImpl->MetadataStore.count(this) && |
| 1001 | "Shouldn't have called this"); |
| 1002 | const LLVMContextImpl::MDMapTy &Info = |
Bill Wendling | dd91e73 | 2012-04-03 10:50:09 +0000 | [diff] [blame] | 1003 | getContext().pImpl->MetadataStore.find(this)->second; |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1004 | assert(!Info.empty() && "Shouldn't have called this"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 1005 | Result.reserve(Result.size() + Info.size()); |
| 1006 | for (auto &I : Info) |
| 1007 | Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); |
Bill Wendling | dd91e73 | 2012-04-03 10:50:09 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1009 | // Sort the resulting array so it is stable. |
| 1010 | if (Result.size() > 1) |
| 1011 | array_pod_sort(Result.begin(), Result.end()); |
| 1012 | } |
| 1013 | |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 1014 | /// clearMetadataHashEntries - Clear all hashtable-based metadata from |
| 1015 | /// this instruction. |
| 1016 | void Instruction::clearMetadataHashEntries() { |
| 1017 | assert(hasMetadataHashEntry() && "Caller should check"); |
| 1018 | getContext().pImpl->MetadataStore.erase(this); |
| 1019 | setHasMetadataHashEntry(false); |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |