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