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" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/STLExtras.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/SmallString.h" |
| 22 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 23 | #include "llvm/IR/ConstantRange.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame^] | 24 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instruction.h" |
| 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 28 | #include "llvm/IR/ValueHandle.h" |
Duncan P. N. Exon Smith | 46d91ad | 2014-11-14 18:42:06 +0000 | [diff] [blame] | 29 | |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 32 | MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD) |
| 33 | : Value(Ty, MetadataAsValueVal), MD(MD) { |
| 34 | track(); |
| 35 | } |
| 36 | |
| 37 | MetadataAsValue::~MetadataAsValue() { |
| 38 | getType()->getContext().pImpl->MetadataAsValues.erase(MD); |
| 39 | untrack(); |
| 40 | } |
| 41 | |
| 42 | /// \brief Canonicalize metadata arguments to intrinsics. |
| 43 | /// |
| 44 | /// To support bitcode upgrades (and assembly semantic sugar) for \a |
| 45 | /// MetadataAsValue, we need to canonicalize certain metadata. |
| 46 | /// |
| 47 | /// - nullptr is replaced by an empty MDNode. |
| 48 | /// - An MDNode with a single null operand is replaced by an empty MDNode. |
| 49 | /// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped. |
| 50 | /// |
| 51 | /// This maintains readability of bitcode from when metadata was a type of |
| 52 | /// value, and these bridges were unnecessary. |
| 53 | static Metadata *canonicalizeMetadataForValue(LLVMContext &Context, |
| 54 | Metadata *MD) { |
| 55 | if (!MD) |
| 56 | // !{} |
| 57 | return MDNode::get(Context, None); |
| 58 | |
| 59 | // Return early if this isn't a single-operand MDNode. |
| 60 | auto *N = dyn_cast<MDNode>(MD); |
| 61 | if (!N || N->getNumOperands() != 1) |
| 62 | return MD; |
| 63 | |
| 64 | if (!N->getOperand(0)) |
| 65 | // !{} |
| 66 | return MDNode::get(Context, None); |
| 67 | |
| 68 | if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0))) |
| 69 | // Look through the MDNode. |
| 70 | return C; |
| 71 | |
| 72 | return MD; |
| 73 | } |
| 74 | |
| 75 | MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) { |
| 76 | MD = canonicalizeMetadataForValue(Context, MD); |
| 77 | auto *&Entry = Context.pImpl->MetadataAsValues[MD]; |
| 78 | if (!Entry) |
| 79 | Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD); |
| 80 | return Entry; |
| 81 | } |
| 82 | |
| 83 | MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context, |
| 84 | Metadata *MD) { |
| 85 | MD = canonicalizeMetadataForValue(Context, MD); |
| 86 | auto &Store = Context.pImpl->MetadataAsValues; |
| 87 | auto I = Store.find(MD); |
| 88 | return I == Store.end() ? nullptr : I->second; |
| 89 | } |
| 90 | |
| 91 | void MetadataAsValue::handleChangedMetadata(Metadata *MD) { |
| 92 | LLVMContext &Context = getContext(); |
| 93 | MD = canonicalizeMetadataForValue(Context, MD); |
| 94 | auto &Store = Context.pImpl->MetadataAsValues; |
| 95 | |
| 96 | // Stop tracking the old metadata. |
| 97 | Store.erase(this->MD); |
| 98 | untrack(); |
| 99 | this->MD = nullptr; |
| 100 | |
| 101 | // Start tracking MD, or RAUW if necessary. |
| 102 | auto *&Entry = Store[MD]; |
| 103 | if (Entry) { |
| 104 | replaceAllUsesWith(Entry); |
| 105 | delete this; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | this->MD = MD; |
| 110 | track(); |
| 111 | Entry = this; |
| 112 | } |
| 113 | |
| 114 | void MetadataAsValue::track() { |
| 115 | if (MD) |
| 116 | MetadataTracking::track(&MD, *MD, *this); |
| 117 | } |
| 118 | |
| 119 | void MetadataAsValue::untrack() { |
| 120 | if (MD) |
| 121 | MetadataTracking::untrack(MD); |
| 122 | } |
| 123 | |
| 124 | void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 125 | bool WasInserted = |
| 126 | UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex))) |
| 127 | .second; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 128 | (void)WasInserted; |
| 129 | assert(WasInserted && "Expected to add a reference"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 130 | |
| 131 | ++NextIndex; |
| 132 | assert(NextIndex != 0 && "Unexpected overflow"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void ReplaceableMetadataImpl::dropRef(void *Ref) { |
| 136 | bool WasErased = UseMap.erase(Ref); |
| 137 | (void)WasErased; |
| 138 | assert(WasErased && "Expected to drop a reference"); |
| 139 | } |
| 140 | |
| 141 | void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, |
| 142 | const Metadata &MD) { |
| 143 | auto I = UseMap.find(Ref); |
| 144 | assert(I != UseMap.end() && "Expected to move a reference"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 145 | auto OwnerAndIndex = I->second; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 146 | UseMap.erase(I); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 147 | bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second; |
| 148 | (void)WasInserted; |
| 149 | assert(WasInserted && "Expected to add a reference"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 150 | |
| 151 | // Check that the references are direct if there's no owner. |
| 152 | (void)MD; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 153 | assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 154 | "Reference without owner must be direct"); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 155 | assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 156 | "Reference without owner must be direct"); |
| 157 | } |
| 158 | |
| 159 | void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 160 | assert(!(MD && isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) && |
| 161 | "Expected non-temp node"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 162 | |
| 163 | if (UseMap.empty()) |
| 164 | return; |
| 165 | |
| 166 | // Copy out uses since UseMap will get touched below. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 167 | typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; |
| 168 | SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); |
| 169 | std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { |
| 170 | return L.second.second < R.second.second; |
| 171 | }); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 172 | for (const auto &Pair : Uses) { |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 173 | // Check that this Ref hasn't disappeared after RAUW (when updating a |
| 174 | // previous Ref). |
| 175 | if (!UseMap.count(Pair.first)) |
| 176 | continue; |
| 177 | |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 178 | OwnerTy Owner = Pair.second.first; |
| 179 | if (!Owner) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 180 | // Update unowned tracking references directly. |
| 181 | Metadata *&Ref = *static_cast<Metadata **>(Pair.first); |
| 182 | Ref = MD; |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 183 | if (MD) |
| 184 | MetadataTracking::track(Ref); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 185 | UseMap.erase(Pair.first); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | // Check for MetadataAsValue. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 190 | if (Owner.is<MetadataAsValue *>()) { |
| 191 | Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | |
| 195 | // There's a Metadata owner -- dispatch. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 196 | Metadata *OwnerMD = Owner.get<Metadata *>(); |
| 197 | switch (OwnerMD->getMetadataID()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 198 | #define HANDLE_METADATA_LEAF(CLASS) \ |
| 199 | case Metadata::CLASS##Kind: \ |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 200 | cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \ |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 201 | continue; |
| 202 | #include "llvm/IR/Metadata.def" |
| 203 | default: |
| 204 | llvm_unreachable("Invalid metadata subclass"); |
| 205 | } |
| 206 | } |
| 207 | assert(UseMap.empty() && "Expected all uses to be replaced"); |
| 208 | } |
| 209 | |
| 210 | void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { |
| 211 | if (UseMap.empty()) |
| 212 | return; |
| 213 | |
| 214 | if (!ResolveUsers) { |
| 215 | UseMap.clear(); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // Copy out uses since UseMap could get touched below. |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 220 | typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; |
| 221 | SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); |
| 222 | std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { |
| 223 | return L.second.second < R.second.second; |
| 224 | }); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 225 | UseMap.clear(); |
| 226 | for (const auto &Pair : Uses) { |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 227 | auto Owner = Pair.second.first; |
| 228 | if (!Owner) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 229 | continue; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 230 | if (Owner.is<MetadataAsValue *>()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 231 | continue; |
| 232 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 233 | // Resolve MDNodes that point at this. |
| 234 | auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>()); |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 235 | if (!OwnerMD) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 236 | continue; |
Duncan P. N. Exon Smith | 21909e3 | 2014-12-09 21:12:56 +0000 | [diff] [blame] | 237 | if (OwnerMD->isResolved()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 238 | continue; |
Duncan P. N. Exon Smith | 34c3d10 | 2015-01-12 19:43:15 +0000 | [diff] [blame] | 239 | OwnerMD->decrementUnresolvedOperandCount(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | static Function *getLocalFunction(Value *V) { |
| 244 | assert(V && "Expected value"); |
| 245 | if (auto *A = dyn_cast<Argument>(V)) |
| 246 | return A->getParent(); |
| 247 | if (BasicBlock *BB = cast<Instruction>(V)->getParent()) |
| 248 | return BB->getParent(); |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
| 252 | ValueAsMetadata *ValueAsMetadata::get(Value *V) { |
| 253 | assert(V && "Unexpected null Value"); |
| 254 | |
| 255 | auto &Context = V->getContext(); |
| 256 | auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; |
| 257 | if (!Entry) { |
| 258 | assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && |
| 259 | "Expected constant or function-local value"); |
| 260 | assert(!V->NameAndIsUsedByMD.getInt() && |
| 261 | "Expected this to be the only metadata use"); |
| 262 | V->NameAndIsUsedByMD.setInt(true); |
| 263 | if (auto *C = dyn_cast<Constant>(V)) |
Duncan P. N. Exon Smith | 1c00c9f | 2015-01-05 20:41:25 +0000 | [diff] [blame] | 264 | Entry = new ConstantAsMetadata(C); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 265 | else |
Duncan P. N. Exon Smith | 1c00c9f | 2015-01-05 20:41:25 +0000 | [diff] [blame] | 266 | Entry = new LocalAsMetadata(V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return Entry; |
| 270 | } |
| 271 | |
| 272 | ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { |
| 273 | assert(V && "Unexpected null Value"); |
| 274 | return V->getContext().pImpl->ValuesAsMetadata.lookup(V); |
| 275 | } |
| 276 | |
| 277 | void ValueAsMetadata::handleDeletion(Value *V) { |
| 278 | assert(V && "Expected valid value"); |
| 279 | |
| 280 | auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; |
| 281 | auto I = Store.find(V); |
| 282 | if (I == Store.end()) |
| 283 | return; |
| 284 | |
| 285 | // Remove old entry from the map. |
| 286 | ValueAsMetadata *MD = I->second; |
| 287 | assert(MD && "Expected valid metadata"); |
| 288 | assert(MD->getValue() == V && "Expected valid mapping"); |
| 289 | Store.erase(I); |
| 290 | |
| 291 | // Delete the metadata. |
| 292 | MD->replaceAllUsesWith(nullptr); |
| 293 | delete MD; |
| 294 | } |
| 295 | |
| 296 | void ValueAsMetadata::handleRAUW(Value *From, Value *To) { |
| 297 | assert(From && "Expected valid value"); |
| 298 | assert(To && "Expected valid value"); |
| 299 | assert(From != To && "Expected changed value"); |
| 300 | assert(From->getType() == To->getType() && "Unexpected type change"); |
| 301 | |
| 302 | LLVMContext &Context = From->getType()->getContext(); |
| 303 | auto &Store = Context.pImpl->ValuesAsMetadata; |
| 304 | auto I = Store.find(From); |
| 305 | if (I == Store.end()) { |
| 306 | assert(!From->NameAndIsUsedByMD.getInt() && |
| 307 | "Expected From not to be used by metadata"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | // Remove old entry from the map. |
| 312 | assert(From->NameAndIsUsedByMD.getInt() && |
| 313 | "Expected From to be used by metadata"); |
| 314 | From->NameAndIsUsedByMD.setInt(false); |
| 315 | ValueAsMetadata *MD = I->second; |
| 316 | assert(MD && "Expected valid metadata"); |
| 317 | assert(MD->getValue() == From && "Expected valid mapping"); |
| 318 | Store.erase(I); |
| 319 | |
| 320 | if (isa<LocalAsMetadata>(MD)) { |
| 321 | if (auto *C = dyn_cast<Constant>(To)) { |
| 322 | // Local became a constant. |
| 323 | MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); |
| 324 | delete MD; |
| 325 | return; |
| 326 | } |
| 327 | if (getLocalFunction(From) && getLocalFunction(To) && |
| 328 | getLocalFunction(From) != getLocalFunction(To)) { |
| 329 | // Function changed. |
| 330 | MD->replaceAllUsesWith(nullptr); |
| 331 | delete MD; |
| 332 | return; |
| 333 | } |
| 334 | } else if (!isa<Constant>(To)) { |
| 335 | // Changed to function-local value. |
| 336 | MD->replaceAllUsesWith(nullptr); |
| 337 | delete MD; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | auto *&Entry = Store[To]; |
| 342 | if (Entry) { |
| 343 | // The target already exists. |
| 344 | MD->replaceAllUsesWith(Entry); |
| 345 | delete MD; |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // Update MD in place (and update the map entry). |
| 350 | assert(!To->NameAndIsUsedByMD.getInt() && |
| 351 | "Expected this to be the only metadata use"); |
| 352 | To->NameAndIsUsedByMD.setInt(true); |
| 353 | MD->V = To; |
| 354 | Entry = MD; |
| 355 | } |
Duncan P. N. Exon Smith | a69934f | 2014-11-14 18:42:09 +0000 | [diff] [blame] | 356 | |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 358 | // MDString implementation. |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 359 | // |
Chris Lattner | 5a409bd | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 360 | |
Devang Patel | dcb99d3 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 361 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 362 | auto &Store = Context.pImpl->MDStringCache; |
| 363 | auto I = Store.find(Str); |
| 364 | if (I != Store.end()) |
| 365 | return &I->second; |
| 366 | |
Duncan P. N. Exon Smith | 5622831 | 2014-12-09 18:52:38 +0000 | [diff] [blame] | 367 | auto *Entry = |
| 368 | StringMapEntry<MDString>::Create(Str, Store.getAllocator(), MDString()); |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 369 | bool WasInserted = Store.insert(Entry); |
| 370 | (void)WasInserted; |
| 371 | assert(WasInserted && "Expected entry to be inserted"); |
Duncan P. N. Exon Smith | c1a664f | 2014-12-05 01:41:34 +0000 | [diff] [blame] | 372 | Entry->second.Entry = Entry; |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 373 | return &Entry->second; |
| 374 | } |
| 375 | |
| 376 | StringRef MDString::getString() const { |
Duncan P. N. Exon Smith | c1a664f | 2014-12-05 01:41:34 +0000 | [diff] [blame] | 377 | assert(Entry && "Expected to find string map entry"); |
| 378 | return Entry->first(); |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 382 | // MDNode implementation. |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 383 | // |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 384 | |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 385 | void *MDNode::operator new(size_t Size, unsigned NumOps) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 386 | void *Ptr = ::operator new(Size + NumOps * sizeof(MDOperand)); |
Duncan P. N. Exon Smith | 22600ff | 2014-12-09 23:56:39 +0000 | [diff] [blame] | 387 | MDOperand *O = static_cast<MDOperand *>(Ptr); |
| 388 | for (MDOperand *E = O + NumOps; O != E; ++O) |
| 389 | (void)new (O) MDOperand; |
| 390 | return O; |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void MDNode::operator delete(void *Mem) { |
| 394 | MDNode *N = static_cast<MDNode *>(Mem); |
Duncan P. N. Exon Smith | 22600ff | 2014-12-09 23:56:39 +0000 | [diff] [blame] | 395 | MDOperand *O = static_cast<MDOperand *>(Mem); |
| 396 | for (MDOperand *E = O - N->NumOperands; O != E; --O) |
| 397 | (O - 1)->~MDOperand(); |
| 398 | ::operator delete(O); |
Duncan P. N. Exon Smith | c23610b | 2014-11-18 01:56:14 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Duncan P. N. Exon Smith | f134045 | 2015-01-19 18:36:18 +0000 | [diff] [blame] | 401 | MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 402 | ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2) |
| 403 | : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), |
| 404 | NumUnresolved(0), Context(Context) { |
| 405 | unsigned Op = 0; |
| 406 | for (Metadata *MD : Ops1) |
| 407 | setOperand(Op++, MD); |
| 408 | for (Metadata *MD : Ops2) |
| 409 | setOperand(Op++, MD); |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 410 | |
Duncan P. N. Exon Smith | a1ae4f6 | 2015-01-19 23:15:21 +0000 | [diff] [blame] | 411 | if (isDistinct()) |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 412 | return; |
| 413 | |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 414 | if (isUniqued()) |
| 415 | // Check whether any operands are unresolved, requiring re-uniquing. If |
| 416 | // not, don't support RAUW. |
| 417 | if (!countUnresolvedOperands()) |
Duncan P. N. Exon Smith | a1ae4f6 | 2015-01-19 23:15:21 +0000 | [diff] [blame] | 418 | return; |
| 419 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 420 | this->Context.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context)); |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Duncan P. N. Exon Smith | 03e0583 | 2015-01-20 02:56:57 +0000 | [diff] [blame] | 423 | TempMDNode MDNode::clone() const { |
| 424 | switch (getMetadataID()) { |
| 425 | default: |
| 426 | llvm_unreachable("Invalid MDNode subclass"); |
| 427 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
| 428 | case CLASS##Kind: \ |
| 429 | return cast<CLASS>(this)->cloneImpl(); |
| 430 | #include "llvm/IR/Metadata.def" |
| 431 | } |
| 432 | } |
| 433 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 434 | static bool isOperandUnresolved(Metadata *Op) { |
| 435 | if (auto *N = dyn_cast_or_null<MDNode>(Op)) |
| 436 | return !N->isResolved(); |
| 437 | return false; |
| 438 | } |
| 439 | |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 440 | unsigned MDNode::countUnresolvedOperands() { |
| 441 | assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted"); |
Duncan P. N. Exon Smith | c5a0e2e | 2015-01-19 22:18:29 +0000 | [diff] [blame] | 442 | for (const auto &Op : operands()) |
| 443 | NumUnresolved += unsigned(isOperandUnresolved(Op)); |
| 444 | return NumUnresolved; |
| 445 | } |
| 446 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 447 | void MDNode::makeUniqued() { |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 448 | assert(isTemporary() && "Expected this to be temporary"); |
| 449 | assert(!isResolved() && "Expected this to be unresolved"); |
| 450 | |
| 451 | // Make this 'uniqued'. |
| 452 | Storage = Uniqued; |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 453 | if (!countUnresolvedOperands()) |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 454 | resolve(); |
| 455 | |
| 456 | assert(isUniqued() && "Expected this to be uniqued"); |
| 457 | } |
| 458 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 459 | void MDNode::makeDistinct() { |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 460 | assert(isTemporary() && "Expected this to be temporary"); |
| 461 | assert(!isResolved() && "Expected this to be unresolved"); |
| 462 | |
| 463 | // Pretend to be uniqued, resolve the node, and then store in distinct table. |
| 464 | Storage = Uniqued; |
| 465 | resolve(); |
| 466 | storeDistinctInContext(); |
| 467 | |
| 468 | assert(isDistinct() && "Expected this to be distinct"); |
| 469 | assert(isResolved() && "Expected this to be resolved"); |
| 470 | } |
| 471 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 472 | void MDNode::resolve() { |
Duncan P. N. Exon Smith | b8f7960 | 2015-01-19 19:26:24 +0000 | [diff] [blame] | 473 | assert(isUniqued() && "Expected this to be uniqued"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 474 | assert(!isResolved() && "Expected this to be unresolved"); |
| 475 | |
| 476 | // Move the map, so that this immediately looks resolved. |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 477 | auto Uses = Context.takeReplaceableUses(); |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 478 | NumUnresolved = 0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 479 | assert(isResolved() && "Expected this to be resolved"); |
| 480 | |
| 481 | // Drop RAUW support. |
| 482 | Uses->resolveAllUses(); |
| 483 | } |
| 484 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 485 | void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 486 | assert(NumUnresolved != 0 && "Expected unresolved operands"); |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 487 | |
Duncan P. N. Exon Smith | 0c87d77 | 2015-01-12 19:45:44 +0000 | [diff] [blame] | 488 | // Check if an operand was resolved. |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 489 | if (!isOperandUnresolved(Old)) { |
| 490 | if (isOperandUnresolved(New)) |
| 491 | // An operand was un-resolved! |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 492 | ++NumUnresolved; |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 493 | } else if (!isOperandUnresolved(New)) |
Duncan P. N. Exon Smith | 0c87d77 | 2015-01-12 19:45:44 +0000 | [diff] [blame] | 494 | decrementUnresolvedOperandCount(); |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 497 | void MDNode::decrementUnresolvedOperandCount() { |
Duncan P. N. Exon Smith | 909131b | 2015-01-19 23:18:34 +0000 | [diff] [blame] | 498 | if (!--NumUnresolved) |
Duncan P. N. Exon Smith | 0c87d77 | 2015-01-12 19:45:44 +0000 | [diff] [blame] | 499 | // Last unresolved operand has just been resolved. |
Duncan P. N. Exon Smith | 34c3d10 | 2015-01-12 19:43:15 +0000 | [diff] [blame] | 500 | resolve(); |
| 501 | } |
| 502 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 503 | void MDNode::resolveCycles() { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 504 | if (isResolved()) |
| 505 | return; |
| 506 | |
| 507 | // Resolve this node immediately. |
| 508 | resolve(); |
| 509 | |
| 510 | // Resolve all operands. |
| 511 | for (const auto &Op : operands()) { |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 512 | auto *N = dyn_cast_or_null<MDNode>(Op); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 513 | if (!N) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 514 | continue; |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 515 | |
| 516 | assert(!N->isTemporary() && |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 517 | "Expected all forward declarations to be resolved"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 518 | if (!N->isResolved()) |
| 519 | N->resolveCycles(); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 520 | } |
Duncan P. N. Exon Smith | 50846f8 | 2014-11-18 00:37:17 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Duncan P. N. Exon Smith | 8647529 | 2015-01-19 23:17:09 +0000 | [diff] [blame] | 523 | MDNode *MDNode::replaceWithUniquedImpl() { |
| 524 | // Try to uniquify in place. |
| 525 | MDNode *UniquedNode = uniquify(); |
| 526 | if (UniquedNode == this) { |
| 527 | makeUniqued(); |
| 528 | return this; |
| 529 | } |
| 530 | |
| 531 | // Collision, so RAUW instead. |
| 532 | replaceAllUsesWith(UniquedNode); |
| 533 | deleteAsSubclass(); |
| 534 | return UniquedNode; |
| 535 | } |
| 536 | |
| 537 | MDNode *MDNode::replaceWithDistinctImpl() { |
| 538 | makeDistinct(); |
| 539 | return this; |
| 540 | } |
| 541 | |
Duncan P. N. Exon Smith | 118632d | 2015-01-12 20:09:34 +0000 | [diff] [blame] | 542 | void MDTuple::recalculateHash() { |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 543 | setHash(MDTupleInfo::KeyTy::calculateHash(this)); |
Duncan P. N. Exon Smith | 967629e | 2015-01-12 19:16:34 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 546 | void MDNode::dropAllReferences() { |
| 547 | for (unsigned I = 0, E = NumOperands; I != E; ++I) |
| 548 | setOperand(I, nullptr); |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 549 | if (!isResolved()) { |
| 550 | Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false); |
| 551 | (void)Context.takeReplaceableUses(); |
| 552 | } |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 555 | void MDNode::handleChangedOperand(void *Ref, Metadata *New) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 556 | unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); |
| 557 | assert(Op < getNumOperands() && "Expected valid operand"); |
| 558 | |
Duncan P. N. Exon Smith | 3d58056 | 2015-01-19 19:28:28 +0000 | [diff] [blame] | 559 | if (!isUniqued()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 560 | // This node is not uniqued. Just set the operand and be done with it. |
| 561 | setOperand(Op, New); |
| 562 | return; |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 563 | } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 564 | |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 565 | // This node is uniqued. |
| 566 | eraseFromStore(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 567 | |
| 568 | Metadata *Old = getOperand(Op); |
| 569 | setOperand(Op, New); |
| 570 | |
Duncan P. N. Exon Smith | bcd960a | 2015-01-05 23:31:54 +0000 | [diff] [blame] | 571 | // Drop uniquing for self-reference cycles. |
| 572 | if (New == this) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 573 | if (!isResolved()) |
| 574 | resolve(); |
Duncan P. N. Exon Smith | f08b8b4 | 2015-01-19 19:25:33 +0000 | [diff] [blame] | 575 | storeDistinctInContext(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 576 | return; |
| 577 | } |
| 578 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 579 | // Re-unique the node. |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 580 | auto *Uniqued = uniquify(); |
| 581 | if (Uniqued == this) { |
Duncan P. N. Exon Smith | 3a16d80 | 2015-01-12 19:14:15 +0000 | [diff] [blame] | 582 | if (!isResolved()) |
| 583 | resolveAfterOperandChange(Old, New); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
| 587 | // Collision. |
| 588 | if (!isResolved()) { |
| 589 | // Still unresolved, so RAUW. |
Duncan P. N. Exon Smith | d9e6eb7 | 2015-01-12 19:36:35 +0000 | [diff] [blame] | 590 | // |
| 591 | // First, clear out all operands to prevent any recursion (similar to |
| 592 | // dropAllReferences(), but we still need the use-list). |
| 593 | for (unsigned O = 0, E = getNumOperands(); O != E; ++O) |
| 594 | setOperand(O, nullptr); |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 595 | Context.getReplaceableUses()->replaceAllUsesWith(Uniqued); |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 596 | deleteAsSubclass(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
Duncan P. N. Exon Smith | d9e6eb7 | 2015-01-12 19:36:35 +0000 | [diff] [blame] | 600 | // 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] | 601 | storeDistinctInContext(); |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 604 | void MDNode::deleteAsSubclass() { |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 605 | switch (getMetadataID()) { |
| 606 | default: |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 607 | llvm_unreachable("Invalid subclass of MDNode"); |
| 608 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 609 | case CLASS##Kind: \ |
| 610 | delete cast<CLASS>(this); \ |
| 611 | break; |
| 612 | #include "llvm/IR/Metadata.def" |
| 613 | } |
| 614 | } |
| 615 | |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 616 | template <class T, class InfoT> |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 617 | static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) { |
| 618 | if (T *U = getUniqued(Store, N)) |
| 619 | return U; |
| 620 | |
| 621 | Store.insert(N); |
| 622 | return N; |
| 623 | } |
| 624 | |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 625 | template <class NodeTy> struct MDNode::HasCachedHash { |
| 626 | typedef char Yes[1]; |
| 627 | typedef char No[2]; |
| 628 | template <class U, U Val> struct SFINAE {}; |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 629 | |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 630 | template <class U> |
| 631 | static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *); |
| 632 | template <class U> static No &check(...); |
| 633 | |
| 634 | static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes); |
| 635 | }; |
| 636 | |
| 637 | MDNode *MDNode::uniquify() { |
Duncan P. N. Exon Smith | f9d1bc9 | 2015-01-19 22:52:07 +0000 | [diff] [blame] | 638 | // Try to insert into uniquing store. |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 639 | switch (getMetadataID()) { |
| 640 | default: |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 641 | llvm_unreachable("Invalid subclass of MDNode"); |
| 642 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 643 | case CLASS##Kind: { \ |
| 644 | CLASS *SubclassThis = cast<CLASS>(this); \ |
| 645 | std::integral_constant<bool, HasCachedHash<CLASS>::value> \ |
| 646 | ShouldRecalculateHash; \ |
| 647 | dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ |
| 648 | return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \ |
| 649 | } |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 650 | #include "llvm/IR/Metadata.def" |
| 651 | } |
| 652 | } |
| 653 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 654 | void MDNode::eraseFromStore() { |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 655 | switch (getMetadataID()) { |
| 656 | default: |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 657 | llvm_unreachable("Invalid subclass of MDNode"); |
| 658 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 659 | case CLASS##Kind: \ |
Duncan P. N. Exon Smith | 6cf10d2 | 2015-01-19 22:47:08 +0000 | [diff] [blame] | 660 | getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \ |
Duncan P. N. Exon Smith | bf68e80 | 2015-01-12 20:56:33 +0000 | [diff] [blame] | 661 | break; |
| 662 | #include "llvm/IR/Metadata.def" |
| 663 | } |
| 664 | } |
| 665 | |
Duncan P. N. Exon Smith | ac3128d | 2015-01-12 20:13:56 +0000 | [diff] [blame] | 666 | MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 667 | StorageType Storage, bool ShouldCreate) { |
| 668 | unsigned Hash = 0; |
| 669 | if (Storage == Uniqued) { |
| 670 | MDTupleInfo::KeyTy Key(MDs); |
Duncan P. N. Exon Smith | b57f9e9 | 2015-01-19 20:16:50 +0000 | [diff] [blame] | 671 | if (auto *N = getUniqued(Context.pImpl->MDTuples, Key)) |
| 672 | return N; |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 673 | if (!ShouldCreate) |
| 674 | return nullptr; |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 675 | Hash = Key.getHash(); |
Duncan P. N. Exon Smith | 1b0064d | 2015-01-19 20:14:15 +0000 | [diff] [blame] | 676 | } else { |
| 677 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); |
| 678 | } |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 679 | |
Duncan P. N. Exon Smith | 5b8c440 | 2015-01-19 20:18:13 +0000 | [diff] [blame] | 680 | return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs), |
| 681 | Storage, Context.pImpl->MDTuples); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 684 | void MDNode::deleteTemporary(MDNode *N) { |
| 685 | assert(N->isTemporary() && "Expected temporary node"); |
Duncan P. N. Exon Smith | 8d53697 | 2015-01-22 21:36:45 +0000 | [diff] [blame] | 686 | N->replaceAllUsesWith(nullptr); |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 687 | N->deleteAsSubclass(); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 690 | void MDNode::storeDistinctInContext() { |
Duncan P. N. Exon Smith | f08b8b4 | 2015-01-19 19:25:33 +0000 | [diff] [blame] | 691 | assert(isResolved() && "Expected resolved nodes"); |
Duncan P. N. Exon Smith | f134045 | 2015-01-19 18:36:18 +0000 | [diff] [blame] | 692 | Storage = Distinct; |
Duncan P. N. Exon Smith | 0f52999 | 2015-01-20 00:57:33 +0000 | [diff] [blame] | 693 | |
| 694 | // Reset the hash. |
| 695 | switch (getMetadataID()) { |
| 696 | default: |
| 697 | llvm_unreachable("Invalid subclass of MDNode"); |
| 698 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
| 699 | case CLASS##Kind: { \ |
| 700 | std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \ |
| 701 | dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \ |
| 702 | break; \ |
| 703 | } |
| 704 | #include "llvm/IR/Metadata.def" |
| 705 | } |
| 706 | |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 707 | getContext().pImpl->DistinctMDNodes.insert(this); |
Devang Patel | 82ab3f8 | 2010-02-18 20:53:16 +0000 | [diff] [blame] | 708 | } |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 709 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 710 | void MDNode::replaceOperandWith(unsigned I, Metadata *New) { |
| 711 | if (getOperand(I) == New) |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 712 | return; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 713 | |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 714 | if (!isUniqued()) { |
Duncan P. N. Exon Smith | daa335a | 2015-01-12 18:01:45 +0000 | [diff] [blame] | 715 | setOperand(I, New); |
Duncan P. N. Exon Smith | f39c3b8 | 2014-11-17 23:28:21 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 718 | |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 719 | handleChangedOperand(mutable_begin() + I, New); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 720 | } |
Chris Lattner | c6d17e2 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 721 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 722 | void MDNode::setOperand(unsigned I, Metadata *New) { |
| 723 | assert(I < NumOperands); |
Duncan P. N. Exon Smith | efdf285 | 2015-01-19 19:29:25 +0000 | [diff] [blame] | 724 | mutable_begin()[I].reset(New, isUniqued() ? this : nullptr); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 727 | /// \brief Get a node, or a self-reference that looks like it. |
| 728 | /// |
| 729 | /// Special handling for finding self-references, for use by \a |
| 730 | /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from |
| 731 | /// when self-referencing nodes were still uniqued. If the first operand has |
| 732 | /// 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] | 733 | static MDNode *getOrSelfReference(LLVMContext &Context, |
| 734 | ArrayRef<Metadata *> Ops) { |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 735 | if (!Ops.empty()) |
| 736 | if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0])) |
| 737 | if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { |
| 738 | for (unsigned I = 1, E = Ops.size(); I != E; ++I) |
| 739 | if (Ops[I] != N->getOperand(I)) |
| 740 | return MDNode::get(Context, Ops); |
| 741 | return N; |
| 742 | } |
| 743 | |
| 744 | return MDNode::get(Context, Ops); |
| 745 | } |
| 746 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 747 | MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { |
| 748 | if (!A) |
| 749 | return B; |
| 750 | if (!B) |
| 751 | return A; |
| 752 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 753 | SmallVector<Metadata *, 4> MDs(A->getNumOperands() + B->getNumOperands()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 754 | |
| 755 | unsigned j = 0; |
| 756 | 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] | 757 | MDs[j++] = A->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 758 | 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] | 759 | MDs[j++] = B->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 760 | |
Duncan P. N. Exon Smith | 9c51b50 | 2014-12-07 20:32:11 +0000 | [diff] [blame] | 761 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 762 | // 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] | 763 | return getOrSelfReference(A->getContext(), MDs); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | MDNode *MDNode::intersect(MDNode *A, MDNode *B) { |
| 767 | if (!A || !B) |
| 768 | return nullptr; |
| 769 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 770 | SmallVector<Metadata *, 4> MDs; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 771 | 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] | 772 | Metadata *MD = A->getOperand(i); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 773 | 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] | 774 | if (MD == B->getOperand(j)) { |
| 775 | MDs.push_back(MD); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | } |
| 779 | |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 780 | // FIXME: This preserves long-standing behaviour, but is it really the right |
| 781 | // 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] | 782 | return getOrSelfReference(A->getContext(), MDs); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 785 | MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { |
| 786 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 787 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 788 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 789 | APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF(); |
| 790 | APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 791 | if (AVal.compare(BVal) == APFloat::cmpLessThan) |
| 792 | return A; |
| 793 | return B; |
| 794 | } |
| 795 | |
| 796 | static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { |
| 797 | return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); |
| 798 | } |
| 799 | |
| 800 | static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { |
| 801 | return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); |
| 802 | } |
| 803 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 804 | static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 805 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 806 | ConstantRange NewRange(Low->getValue(), High->getValue()); |
| 807 | unsigned Size = EndPoints.size(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 808 | APInt LB = EndPoints[Size - 2]->getValue(); |
| 809 | APInt LE = EndPoints[Size - 1]->getValue(); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 810 | ConstantRange LastRange(LB, LE); |
| 811 | if (canBeMerged(NewRange, LastRange)) { |
| 812 | ConstantRange Union = LastRange.unionWith(NewRange); |
| 813 | Type *Ty = High->getType(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 814 | EndPoints[Size - 2] = |
| 815 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower())); |
| 816 | EndPoints[Size - 1] = |
| 817 | cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper())); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | return false; |
| 821 | } |
| 822 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 823 | static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints, |
| 824 | ConstantInt *Low, ConstantInt *High) { |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 825 | if (!EndPoints.empty()) |
| 826 | if (tryMergeRange(EndPoints, Low, High)) |
| 827 | return; |
| 828 | |
| 829 | EndPoints.push_back(Low); |
| 830 | EndPoints.push_back(High); |
| 831 | } |
| 832 | |
| 833 | MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { |
| 834 | // Given two ranges, we want to compute the union of the ranges. This |
| 835 | // is slightly complitade by having to combine the intervals and merge |
| 836 | // the ones that overlap. |
| 837 | |
| 838 | if (!A || !B) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 839 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 840 | |
| 841 | if (A == B) |
| 842 | return A; |
| 843 | |
| 844 | // First, walk both lists in older of the lower boundary of each interval. |
| 845 | // 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] | 846 | SmallVector<ConstantInt *, 4> EndPoints; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 847 | int AI = 0; |
| 848 | int BI = 0; |
| 849 | int AN = A->getNumOperands() / 2; |
| 850 | int BN = B->getNumOperands() / 2; |
| 851 | while (AI < AN && BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 852 | ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI)); |
| 853 | ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI)); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 854 | |
| 855 | if (ALow->getValue().slt(BLow->getValue())) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 856 | addRange(EndPoints, ALow, |
| 857 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 858 | ++AI; |
| 859 | } else { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 860 | addRange(EndPoints, BLow, |
| 861 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 862 | ++BI; |
| 863 | } |
| 864 | } |
| 865 | while (AI < AN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 866 | addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)), |
| 867 | mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 868 | ++AI; |
| 869 | } |
| 870 | while (BI < BN) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 871 | addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)), |
| 872 | mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 873 | ++BI; |
| 874 | } |
| 875 | |
| 876 | // If we have more than 2 ranges (4 endpoints) we have to try to merge |
| 877 | // the last and first ones. |
| 878 | unsigned Size = EndPoints.size(); |
| 879 | if (Size > 4) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 880 | ConstantInt *FB = EndPoints[0]; |
| 881 | ConstantInt *FE = EndPoints[1]; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 882 | if (tryMergeRange(EndPoints, FB, FE)) { |
| 883 | for (unsigned i = 0; i < Size - 2; ++i) { |
| 884 | EndPoints[i] = EndPoints[i + 2]; |
| 885 | } |
| 886 | EndPoints.resize(Size - 2); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | // If in the end we have a single range, it is possible that it is now the |
| 891 | // full range. Just drop the metadata in that case. |
| 892 | if (EndPoints.size() == 2) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 893 | ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 894 | if (Range.isFullSet()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 895 | return nullptr; |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 898 | SmallVector<Metadata *, 4> MDs; |
| 899 | MDs.reserve(EndPoints.size()); |
| 900 | for (auto *I : EndPoints) |
| 901 | MDs.push_back(ConstantAsMetadata::get(I)); |
| 902 | return MDNode::get(A->getContext(), MDs); |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 905 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 906 | // NamedMDNode implementation. |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 907 | // |
Devang Patel | 943ddf6 | 2010-01-12 18:34:06 +0000 | [diff] [blame] | 908 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 909 | static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) { |
| 910 | return *(SmallVector<TrackingMDRef, 4> *)Operands; |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 913 | NamedMDNode::NamedMDNode(const Twine &N) |
Duncan P. N. Exon Smith | c5754a6 | 2014-11-05 18:16:03 +0000 | [diff] [blame] | 914 | : Name(N.str()), Parent(nullptr), |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 915 | Operands(new SmallVector<TrackingMDRef, 4>()) {} |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 917 | NamedMDNode::~NamedMDNode() { |
| 918 | dropAllReferences(); |
| 919 | delete &getNMDOps(Operands); |
| 920 | } |
| 921 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 922 | unsigned NamedMDNode::getNumOperands() const { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 923 | return (unsigned)getNMDOps(Operands).size(); |
| 924 | } |
| 925 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 926 | MDNode *NamedMDNode::getOperand(unsigned i) const { |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 927 | assert(i < getNumOperands() && "Invalid Operand number!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 928 | auto *N = getNMDOps(Operands)[i].get(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 929 | return cast_or_null<MDNode>(N); |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 932 | void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 933 | |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 934 | void NamedMDNode::setOperand(unsigned I, MDNode *New) { |
| 935 | assert(I < getNumOperands() && "Invalid operand number"); |
| 936 | getNMDOps(Operands)[I].reset(New); |
| 937 | } |
| 938 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 939 | void NamedMDNode::eraseFromParent() { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 940 | getParent()->eraseNamedMetadata(this); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 943 | void NamedMDNode::dropAllReferences() { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 944 | getNMDOps(Operands).clear(); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 947 | StringRef NamedMDNode::getName() const { |
| 948 | return StringRef(Name); |
| 949 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 950 | |
| 951 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 952 | // Instruction Metadata method implementations. |
| 953 | // |
| 954 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 955 | void Instruction::setMetadata(StringRef Kind, MDNode *Node) { |
| 956 | if (!Node && !hasMetadata()) |
| 957 | return; |
| 958 | setMetadata(getContext().getMDKindID(Kind), Node); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 961 | MDNode *Instruction::getMetadataImpl(StringRef Kind) const { |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 962 | return getMetadataImpl(getContext().getMDKindID(Kind)); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 965 | void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { |
| 966 | SmallSet<unsigned, 5> KnownSet; |
| 967 | KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); |
| 968 | |
| 969 | // Drop debug if needed |
| 970 | if (KnownSet.erase(LLVMContext::MD_dbg)) |
| 971 | DbgLoc = DebugLoc(); |
| 972 | |
| 973 | if (!hasMetadataHashEntry()) |
| 974 | return; // Nothing to remove! |
| 975 | |
| 976 | DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore = |
| 977 | getContext().pImpl->MetadataStore; |
| 978 | |
| 979 | if (KnownSet.empty()) { |
| 980 | // Just drop our entry at the store. |
| 981 | MetadataStore.erase(this); |
| 982 | setHasMetadataHashEntry(false); |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | LLVMContextImpl::MDMapTy &Info = MetadataStore[this]; |
| 987 | unsigned I; |
| 988 | unsigned E; |
| 989 | // Walk the array and drop any metadata we don't know. |
| 990 | for (I = 0, E = Info.size(); I != E;) { |
| 991 | if (KnownSet.count(Info[I].first)) { |
| 992 | ++I; |
| 993 | continue; |
| 994 | } |
| 995 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 996 | Info[I] = std::move(Info.back()); |
Rafael Espindola | ab73c49 | 2014-01-28 16:56:46 +0000 | [diff] [blame] | 997 | Info.pop_back(); |
| 998 | --E; |
| 999 | } |
| 1000 | assert(E == Info.size()); |
| 1001 | |
| 1002 | if (E == 0) { |
| 1003 | // Drop our entry at the store. |
| 1004 | MetadataStore.erase(this); |
| 1005 | setHasMetadataHashEntry(false); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1009 | /// setMetadata - Set the metadata of of the specified kind to the specified |
| 1010 | /// 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] | 1011 | /// Node is null. |
| 1012 | void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
| 1013 | if (!Node && !hasMetadata()) |
| 1014 | return; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1016 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 1017 | if (KindID == LLVMContext::MD_dbg) { |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 1018 | DbgLoc = DebugLoc::getFromDILocation(Node); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1022 | // Handle the case when we're adding/updating metadata on an instruction. |
| 1023 | if (Node) { |
| 1024 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1025 | assert(!Info.empty() == hasMetadataHashEntry() && |
| 1026 | "HasMetadata bit is wonked"); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1027 | if (Info.empty()) { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1028 | setHasMetadataHashEntry(true); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | // Handle replacement of an existing value. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1031 | for (auto &P : Info) |
| 1032 | if (P.first == KindID) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1033 | P.second.reset(Node); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1038 | // No replacement, just add it to the list. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1039 | Info.emplace_back(std::piecewise_construct, std::make_tuple(KindID), |
| 1040 | std::make_tuple(Node)); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1044 | // Otherwise, we're removing metadata from an instruction. |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 1045 | assert((hasMetadataHashEntry() == |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 1046 | (getContext().pImpl->MetadataStore.count(this) > 0)) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1047 | "HasMetadata bit out of date!"); |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 1048 | if (!hasMetadataHashEntry()) |
| 1049 | return; // Nothing to remove! |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1050 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1052 | // Common case is removing the only entry. |
| 1053 | if (Info.size() == 1 && Info[0].first == KindID) { |
| 1054 | getContext().pImpl->MetadataStore.erase(this); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1055 | setHasMetadataHashEntry(false); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1059 | // Handle removal of an existing value. |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1060 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 1061 | if (Info[i].first == KindID) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1062 | Info[i] = std::move(Info.back()); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1063 | Info.pop_back(); |
| 1064 | assert(!Info.empty() && "Removing last entry should be handled above"); |
| 1065 | return; |
| 1066 | } |
| 1067 | // Otherwise, removing an entry that doesn't exist on the instruction. |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1070 | void Instruction::setAAMetadata(const AAMDNodes &N) { |
| 1071 | setMetadata(LLVMContext::MD_tbaa, N.TBAA); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 1072 | setMetadata(LLVMContext::MD_alias_scope, N.Scope); |
| 1073 | setMetadata(LLVMContext::MD_noalias, N.NoAlias); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1076 | MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1077 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 1078 | if (KindID == LLVMContext::MD_dbg) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1079 | return DbgLoc.getAsMDNode(); |
| 1080 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1081 | if (!hasMetadataHashEntry()) return nullptr; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1083 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1084 | assert(!Info.empty() && "bit out of sync with hash table"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1085 | |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1086 | for (const auto &I : Info) |
| 1087 | if (I.first == KindID) |
| 1088 | return I.second; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1089 | return nullptr; |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Duncan P. N. Exon Smith | 4abd1a0 | 2014-11-01 00:26:42 +0000 | [diff] [blame] | 1092 | void Instruction::getAllMetadataImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1093 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1094 | Result.clear(); |
| 1095 | |
| 1096 | // 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] | 1097 | if (!DbgLoc.isUnknown()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1098 | Result.push_back( |
| 1099 | std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 1100 | if (!hasMetadataHashEntry()) return; |
| 1101 | } |
| 1102 | |
| 1103 | assert(hasMetadataHashEntry() && |
| 1104 | getContext().pImpl->MetadataStore.count(this) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1105 | "Shouldn't have called this"); |
| 1106 | const LLVMContextImpl::MDMapTy &Info = |
| 1107 | getContext().pImpl->MetadataStore.find(this)->second; |
| 1108 | assert(!Info.empty() && "Shouldn't have called this"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 1109 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1110 | Result.reserve(Result.size() + Info.size()); |
| 1111 | for (auto &I : Info) |
| 1112 | 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] | 1113 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 1114 | // Sort the resulting array so it is stable. |
| 1115 | if (Result.size() > 1) |
| 1116 | array_pod_sort(Result.begin(), Result.end()); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Duncan P. N. Exon Smith | 3d5a02f | 2014-11-03 18:13:57 +0000 | [diff] [blame] | 1119 | void Instruction::getAllMetadataOtherThanDebugLocImpl( |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1120 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1121 | Result.clear(); |
| 1122 | assert(hasMetadataHashEntry() && |
| 1123 | getContext().pImpl->MetadataStore.count(this) && |
| 1124 | "Shouldn't have called this"); |
| 1125 | const LLVMContextImpl::MDMapTy &Info = |
Bill Wendling | dd91e73 | 2012-04-03 10:50:09 +0000 | [diff] [blame] | 1126 | getContext().pImpl->MetadataStore.find(this)->second; |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1127 | assert(!Info.empty() && "Shouldn't have called this"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1128 | Result.reserve(Result.size() + Info.size()); |
| 1129 | for (auto &I : Info) |
| 1130 | 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] | 1131 | |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 1132 | // Sort the resulting array so it is stable. |
| 1133 | if (Result.size() > 1) |
| 1134 | array_pod_sort(Result.begin(), Result.end()); |
| 1135 | } |
| 1136 | |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 1137 | /// clearMetadataHashEntries - Clear all hashtable-based metadata from |
| 1138 | /// this instruction. |
| 1139 | void Instruction::clearMetadataHashEntries() { |
| 1140 | assert(hasMetadataHashEntry() && "Caller should check"); |
| 1141 | getContext().pImpl->MetadataStore.erase(this); |
| 1142 | setHasMetadataHashEntry(false); |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 1143 | } |