Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 1 | //===-- Metadata.cpp - Implement Metadata classes -------------------------===// |
| 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 | |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 14 | #include "LLVMContextImpl.h" |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 15 | #include "llvm/Metadata.h" |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 16 | #include "llvm/LLVMContext.h" |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 18 | #include "llvm/Instruction.h" |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 19 | #include "SymbolTableListTraitsImpl.h" |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 23 | //MetadataBase implementation |
| 24 | // |
| 25 | |
| 26 | /// resizeOperands - Metadata keeps track of other metadata uses using |
| 27 | /// OperandList. Resize this list to hold anticipated number of metadata |
| 28 | /// operands. |
| 29 | void MetadataBase::resizeOperands(unsigned NumOps) { |
| 30 | unsigned e = getNumOperands(); |
| 31 | if (NumOps == 0) { |
| 32 | NumOps = e*2; |
| 33 | if (NumOps < 2) NumOps = 2; |
| 34 | } else if (NumOps > NumOperands) { |
| 35 | // No resize needed. |
| 36 | if (ReservedSpace >= NumOps) return; |
| 37 | } else if (NumOps == NumOperands) { |
| 38 | if (ReservedSpace == NumOps) return; |
| 39 | } else { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | ReservedSpace = NumOps; |
| 44 | Use *OldOps = OperandList; |
| 45 | Use *NewOps = allocHungoffUses(NumOps); |
| 46 | std::copy(OldOps, OldOps + e, NewOps); |
| 47 | OperandList = NewOps; |
| 48 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
| 49 | } |
| 50 | //===----------------------------------------------------------------------===// |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 51 | //MDString implementation |
| 52 | // |
| 53 | MDString *MDString::get(LLVMContext &Context, const StringRef &Str) { |
| 54 | LLVMContextImpl *pImpl = Context.pImpl; |
| 55 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 56 | StringMapEntry<MDString *> &Entry = |
| 57 | pImpl->MDStringCache.GetOrCreateValue(Str); |
| 58 | MDString *&S = Entry.getValue(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 59 | if (!S) S = new MDString(Context, Entry.getKeyData(), |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 60 | Entry.getKeyLength()); |
| 61 | |
| 62 | return S; |
| 63 | } |
| 64 | |
| 65 | //===----------------------------------------------------------------------===// |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 66 | //MDNode implementation |
| 67 | // |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 68 | MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals) |
| 69 | : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) { |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 70 | NumOperands = 0; |
| 71 | resizeOperands(NumVals); |
| 72 | for (unsigned i = 0; i != NumVals; ++i) { |
| 73 | // Only record metadata uses. |
| 74 | if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i])) |
| 75 | OperandList[NumOperands++] = MB; |
Devang Patel | a33c5a9 | 2009-09-04 21:32:05 +0000 | [diff] [blame] | 76 | else if(Vals[i] && |
| 77 | Vals[i]->getType()->getTypeID() == Type::MetadataTyID) |
| 78 | OperandList[NumOperands++] = Vals[i]; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 79 | Node.push_back(ElementVH(Vals[i], this)); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 80 | } |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 83 | void MDNode::Profile(FoldingSetNodeID &ID) const { |
| 84 | for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I) |
| 85 | ID.AddPointer(*I); |
| 86 | } |
| 87 | |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 88 | MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) { |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 89 | LLVMContextImpl *pImpl = Context.pImpl; |
| 90 | FoldingSetNodeID ID; |
| 91 | for (unsigned i = 0; i != NumVals; ++i) |
| 92 | ID.AddPointer(Vals[i]); |
| 93 | |
| 94 | pImpl->ConstantsLock.reader_acquire(); |
| 95 | void *InsertPoint; |
| 96 | MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 97 | pImpl->ConstantsLock.reader_release(); |
Devang Patel | 9a767cb | 2009-09-09 17:30:04 +0000 | [diff] [blame] | 98 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 99 | if (!N) { |
| 100 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 101 | N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 102 | if (!N) { |
| 103 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 104 | N = new MDNode(Context, Vals, NumVals); |
| 105 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return N; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 112 | /// dropAllReferences - Remove all uses and clear node vector. |
| 113 | void MDNode::dropAllReferences() { |
| 114 | User::dropAllReferences(); |
| 115 | Node.clear(); |
| 116 | } |
| 117 | |
| 118 | MDNode::~MDNode() { |
Devang Patel | cbde073 | 2009-09-09 17:44:26 +0000 | [diff] [blame] | 119 | { |
| 120 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 121 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 122 | pImpl->MDNodeSet.RemoveNode(this); |
| 123 | } |
Devang Patel | 7b8f61f | 2009-08-27 15:32:38 +0000 | [diff] [blame] | 124 | dropAllReferences(); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 125 | } |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 126 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 127 | // Replace value from this node's element list. |
| 128 | void MDNode::replaceElement(Value *From, Value *To) { |
| 129 | if (From == To || !getType()) |
| 130 | return; |
| 131 | LLVMContext &Context = getType()->getContext(); |
| 132 | LLVMContextImpl *pImpl = Context.pImpl; |
| 133 | |
| 134 | // Find value. This is a linear search, do something if it consumes |
| 135 | // lot of time. It is possible that to have multiple instances of |
| 136 | // From in this MDNode's element list. |
| 137 | SmallVector<unsigned, 4> Indexes; |
| 138 | unsigned Index = 0; |
| 139 | for (SmallVector<ElementVH, 4>::iterator I = Node.begin(), |
| 140 | E = Node.end(); I != E; ++I, ++Index) { |
| 141 | Value *V = *I; |
| 142 | if (V && V == From) |
| 143 | Indexes.push_back(Index); |
| 144 | } |
| 145 | |
| 146 | if (Indexes.empty()) |
| 147 | return; |
| 148 | |
| 149 | // Remove "this" from the context map. |
| 150 | { |
| 151 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 152 | pImpl->MDNodeSet.RemoveNode(this); |
| 153 | } |
| 154 | |
Devang Patel | a33c5a9 | 2009-09-04 21:32:05 +0000 | [diff] [blame] | 155 | // MDNode only lists metadata elements in operand list, because MDNode |
| 156 | // used by MDNode is considered a valid use. However on the side, MDNode |
| 157 | // using a non-metadata value is not considered a "use" of non-metadata |
| 158 | // value. |
| 159 | SmallVector<unsigned, 4> OpIndexes; |
| 160 | unsigned OpIndex = 0; |
| 161 | for (User::op_iterator OI = op_begin(), OE = op_end(); |
| 162 | OI != OE; ++OI, OpIndex++) { |
| 163 | if (*OI == From) |
| 164 | OpIndexes.push_back(OpIndex); |
| 165 | } |
| 166 | if (MetadataBase *MDTo = dyn_cast_or_null<MetadataBase>(To)) { |
| 167 | for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(), |
| 168 | OE = OpIndexes.end(); OI != OE; ++OI) |
| 169 | setOperand(*OI, MDTo); |
| 170 | } else { |
| 171 | for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(), |
| 172 | OE = OpIndexes.end(); OI != OE; ++OI) |
| 173 | setOperand(*OI, 0); |
| 174 | } |
| 175 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 176 | // Replace From element(s) in place. |
| 177 | for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end(); |
| 178 | I != E; ++I) { |
| 179 | unsigned Index = *I; |
| 180 | Node[Index] = ElementVH(To, this); |
| 181 | } |
| 182 | |
| 183 | // Insert updated "this" into the context's folding node set. |
| 184 | // If a node with same element list already exist then before inserting |
| 185 | // updated "this" into the folding node set, replace all uses of existing |
| 186 | // node with updated "this" node. |
| 187 | FoldingSetNodeID ID; |
| 188 | Profile(ID); |
| 189 | pImpl->ConstantsLock.reader_acquire(); |
| 190 | void *InsertPoint; |
| 191 | MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 192 | pImpl->ConstantsLock.reader_release(); |
| 193 | |
| 194 | if (N) { |
| 195 | N->replaceAllUsesWith(this); |
| 196 | delete N; |
| 197 | N = 0; |
| 198 | } |
| 199 | |
| 200 | { |
| 201 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 202 | N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 203 | if (!N) { |
| 204 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 205 | N = this; |
| 206 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | //NamedMDNode implementation |
| 213 | // |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 214 | NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, |
| 215 | MetadataBase*const* MDs, |
Devang Patel | 4a942d0 | 2009-07-29 21:58:56 +0000 | [diff] [blame] | 216 | unsigned NumMDs, Module *ParentModule) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 217 | : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) { |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 218 | setName(N); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 219 | NumOperands = 0; |
| 220 | resizeOperands(NumMDs); |
| 221 | |
| 222 | for (unsigned i = 0; i != NumMDs; ++i) { |
| 223 | if (MDs[i]) |
| 224 | OperandList[NumOperands++] = MDs[i]; |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 225 | Node.push_back(WeakMetadataVH(MDs[i])); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 226 | } |
Devang Patel | 4a942d0 | 2009-07-29 21:58:56 +0000 | [diff] [blame] | 227 | if (ParentModule) |
| 228 | ParentModule->getNamedMDList().push_back(this); |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 229 | } |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 230 | |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 231 | NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) { |
| 232 | assert (NMD && "Invalid source NamedMDNode!"); |
| 233 | SmallVector<MetadataBase *, 4> Elems; |
| 234 | for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) |
| 235 | Elems.push_back(NMD->getElement(i)); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 236 | return new NamedMDNode(NMD->getContext(), NMD->getName().data(), |
| 237 | Elems.data(), Elems.size(), M); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 240 | /// eraseFromParent - Drop all references and remove the node from parent |
| 241 | /// module. |
| 242 | void NamedMDNode::eraseFromParent() { |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 243 | getParent()->getNamedMDList().erase(this); |
| 244 | } |
| 245 | |
| 246 | /// dropAllReferences - Remove all uses and clear node vector. |
| 247 | void NamedMDNode::dropAllReferences() { |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 248 | User::dropAllReferences(); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 249 | Node.clear(); |
| 250 | } |
| 251 | |
| 252 | NamedMDNode::~NamedMDNode() { |
| 253 | dropAllReferences(); |
| 254 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 255 | |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | //Metadata implementation |
| 258 | // |
| 259 | |
| 260 | /// RegisterMDKind - Register a new metadata kind and return its ID. |
| 261 | /// A metadata kind can be registered only once. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 262 | unsigned MetadataContext::RegisterMDKind(const char *Name) { |
Devang Patel | ba4a6fd | 2009-09-29 00:01:14 +0000 | [diff] [blame] | 263 | assert (validName(Name) && "Invalid custome metadata name!"); |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 264 | unsigned Count = MDHandlerNames.size(); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 265 | StringMap<unsigned>::iterator I = MDHandlerNames.find(Name); |
| 266 | assert(I == MDHandlerNames.end() && "Already registered MDKind!"); |
| 267 | MDHandlerNames[Name] = Count + 1; |
| 268 | return Count + 1; |
| 269 | } |
| 270 | |
Devang Patel | ba4a6fd | 2009-09-29 00:01:14 +0000 | [diff] [blame] | 271 | /// validName - Return true if Name is a valid custom metadata handler name. |
| 272 | bool MetadataContext::validName(const char *Name) { |
| 273 | if (!Name) |
| 274 | return false; |
| 275 | |
| 276 | if (!isalpha(*Name)) |
| 277 | return false; |
| 278 | |
| 279 | unsigned Length = strlen(Name); |
| 280 | unsigned Count = 1; |
| 281 | ++Name; |
| 282 | while (Name && |
| 283 | (isalnum(*Name) || *Name == '_' || *Name == '-' || *Name == '.')) { |
| 284 | ++Name; |
| 285 | ++Count; |
| 286 | } |
| 287 | if (Length != Count) |
| 288 | return false; |
| 289 | return true; |
| 290 | } |
| 291 | |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 292 | /// getMDKind - Return metadata kind. If the requested metadata kind |
| 293 | /// is not registered then return 0. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 294 | unsigned MetadataContext::getMDKind(const char *Name) { |
Devang Patel | ba4a6fd | 2009-09-29 00:01:14 +0000 | [diff] [blame] | 295 | assert (validName(Name) && "Invalid custome metadata name!"); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 296 | StringMap<unsigned>::iterator I = MDHandlerNames.find(Name); |
| 297 | if (I == MDHandlerNames.end()) |
| 298 | return 0; |
| 299 | |
| 300 | return I->getValue(); |
| 301 | } |
| 302 | |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 303 | /// addMD - Attach the metadata of given kind with an Instruction. |
| 304 | void MetadataContext::addMD(unsigned MDKind, MDNode *Node, Instruction *Inst) { |
| 305 | assert (Node && "Unable to add custome metadata"); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 306 | Inst->HasMetadata = true; |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 307 | MDStoreTy::iterator I = MetadataStore.find(Inst); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 308 | if (I == MetadataStore.end()) { |
| 309 | MDMapTy Info; |
| 310 | Info.push_back(std::make_pair(MDKind, Node)); |
| 311 | MetadataStore.insert(std::make_pair(Inst, Info)); |
| 312 | return; |
| 313 | } |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 314 | |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 315 | MDMapTy &Info = I->second; |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 316 | // If there is an entry for this MDKind then replace it. |
| 317 | for (unsigned i = 0, e = Info.size(); i != e; ++i) { |
| 318 | MDPairTy &P = Info[i]; |
| 319 | if (P.first == MDKind) { |
| 320 | Info[i] = std::make_pair(MDKind, Node); |
| 321 | return; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Otherwise add a new entry. |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 326 | Info.push_back(std::make_pair(MDKind, Node)); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | /// getMD - Get the metadata of given kind attached with an Instruction. |
| 331 | /// If the metadata is not found then return 0. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 332 | MDNode *MetadataContext::getMD(unsigned MDKind, const Instruction *Inst) { |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 333 | MDStoreTy::iterator I = MetadataStore.find(Inst); |
| 334 | if (I == MetadataStore.end()) |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 335 | return NULL; |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 336 | |
| 337 | MDMapTy &Info = I->second; |
| 338 | for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) |
| 339 | if (I->first == MDKind) |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 340 | return dyn_cast_or_null<MDNode>(I->second); |
| 341 | return NULL; |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Devang Patel | dec23fd | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 344 | /// getMDs - Get the metadata attached with an Instruction. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 345 | const MetadataContext::MDMapTy *MetadataContext::getMDs(const Instruction *Inst) { |
Devang Patel | dec23fd | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 346 | MDStoreTy::iterator I = MetadataStore.find(Inst); |
| 347 | if (I == MetadataStore.end()) |
| 348 | return NULL; |
| 349 | |
| 350 | return &(I->second); |
| 351 | } |
| 352 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 353 | /// getHandlerNames - Get handler names. This is used by bitcode |
| 354 | /// writer. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 355 | const StringMap<unsigned> *MetadataContext::getHandlerNames() { |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 356 | return &MDHandlerNames; |
| 357 | } |
| 358 | |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 359 | /// ValueIsDeleted - This handler is used to update metadata store |
| 360 | /// when a value is deleted. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 361 | void MetadataContext::ValueIsDeleted(const Instruction *Inst) { |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 362 | // Find Metadata handles for this instruction. |
| 363 | MDStoreTy::iterator I = MetadataStore.find(Inst); |
Devang Patel | 5619779 | 2009-09-29 20:01:19 +0000 | [diff] [blame] | 364 | assert (I != MetadataStore.end() && "Invalid custom metadata info!"); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 365 | MDMapTy &Info = I->second; |
| 366 | |
| 367 | // FIXME : Give all metadata handlers a chance to adjust. |
| 368 | |
| 369 | // Remove the entries for this instruction. |
| 370 | Info.clear(); |
Devang Patel | 5619779 | 2009-09-29 20:01:19 +0000 | [diff] [blame] | 371 | MetadataStore.erase(I); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 372 | } |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 373 | |
| 374 | /// ValueIsCloned - This handler is used to update metadata store |
| 375 | /// when In1 is cloned to create In2. |
Devang Patel | 2d85eef | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 376 | void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) { |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 377 | // Find Metadata handles for In1. |
| 378 | MDStoreTy::iterator I = MetadataStore.find(In1); |
Devang Patel | 5619779 | 2009-09-29 20:01:19 +0000 | [diff] [blame] | 379 | assert (I != MetadataStore.end() && "Invalid custom metadata info!"); |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 380 | |
| 381 | // FIXME : Give all metadata handlers a chance to adjust. |
| 382 | |
| 383 | MDMapTy &In1Info = I->second; |
| 384 | MDMapTy In2Info; |
| 385 | for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I) |
| 386 | if (MDNode *MD = dyn_cast_or_null<MDNode>(I->second)) |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame^] | 387 | addMD(I->first, MD, In2); |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 388 | } |