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