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