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