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