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