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 | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 21 | #include "SymbolTableListTraitsImpl.h" |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 25 | // MetadataBase implementation. |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 26 | // |
| 27 | |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 29 | // MDString implementation. |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 30 | // |
Devang Patel | dcb99d3 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 31 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 32 | LLVMContextImpl *pImpl = Context.pImpl; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 33 | StringMapEntry<MDString *> &Entry = |
| 34 | pImpl->MDStringCache.GetOrCreateValue(Str); |
| 35 | MDString *&S = Entry.getValue(); |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 36 | if (!S) S = new MDString(Context, Entry.getKey()); |
| 37 | return S; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Devang Patel | 862ef78 | 2009-11-12 00:50:58 +0000 | [diff] [blame] | 40 | MDString *MDString::get(LLVMContext &Context, const char *Str) { |
| 41 | LLVMContextImpl *pImpl = Context.pImpl; |
| 42 | StringMapEntry<MDString *> &Entry = |
| 43 | pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef()); |
| 44 | MDString *&S = Entry.getValue(); |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 45 | if (!S) new MDString(Context, Entry.getKey()); |
| 46 | return S; |
Devang Patel | 862ef78 | 2009-11-12 00:50:58 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 50 | // MDNode implementation. |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 51 | // |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 52 | MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 53 | : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) { |
Devang Patel | 49914e6e | 2009-10-21 21:25:09 +0000 | [diff] [blame] | 54 | NodeSize = NumVals; |
| 55 | Node = new ElementVH[NodeSize]; |
| 56 | ElementVH *Ptr = Node; |
Devang Patel | 27e0be2 | 2009-10-21 23:57:35 +0000 | [diff] [blame] | 57 | for (unsigned i = 0; i != NumVals; ++i) |
Devang Patel | 49914e6e | 2009-10-21 21:25:09 +0000 | [diff] [blame] | 58 | *Ptr++ = ElementVH(Vals[i], this); |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 61 | void MDNode::Profile(FoldingSetNodeID &ID) const { |
Devang Patel | 49914e6e | 2009-10-21 21:25:09 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) |
| 63 | ID.AddPointer(getElement(i)); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 66 | MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) { |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 67 | LLVMContextImpl *pImpl = Context.pImpl; |
| 68 | FoldingSetNodeID ID; |
| 69 | for (unsigned i = 0; i != NumVals; ++i) |
| 70 | ID.AddPointer(Vals[i]); |
| 71 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 72 | void *InsertPoint; |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 73 | MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 74 | if (!N) { |
| 75 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 76 | N = new MDNode(Context, Vals, NumVals); |
| 77 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 78 | } |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 79 | return N; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Devang Patel | 27e0be2 | 2009-10-21 23:57:35 +0000 | [diff] [blame] | 82 | /// ~MDNode - Destroy MDNode. |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 83 | MDNode::~MDNode() { |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 84 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 85 | pImpl->MDNodeSet.RemoveNode(this); |
Devang Patel | 27e0be2 | 2009-10-21 23:57:35 +0000 | [diff] [blame] | 86 | delete [] Node; |
| 87 | Node = NULL; |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 88 | } |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 89 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 90 | // Replace value from this node's element list. |
| 91 | void MDNode::replaceElement(Value *From, Value *To) { |
| 92 | if (From == To || !getType()) |
| 93 | return; |
| 94 | LLVMContext &Context = getType()->getContext(); |
| 95 | LLVMContextImpl *pImpl = Context.pImpl; |
| 96 | |
| 97 | // Find value. This is a linear search, do something if it consumes |
| 98 | // lot of time. It is possible that to have multiple instances of |
| 99 | // From in this MDNode's element list. |
| 100 | SmallVector<unsigned, 4> Indexes; |
| 101 | unsigned Index = 0; |
Devang Patel | 49914e6e | 2009-10-21 21:25:09 +0000 | [diff] [blame] | 102 | for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) { |
| 103 | Value *V = getElement(i); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 104 | if (V && V == From) |
| 105 | Indexes.push_back(Index); |
| 106 | } |
| 107 | |
| 108 | if (Indexes.empty()) |
| 109 | return; |
| 110 | |
| 111 | // Remove "this" from the context map. |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 112 | pImpl->MDNodeSet.RemoveNode(this); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 113 | |
| 114 | // Replace From element(s) in place. |
| 115 | for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end(); |
| 116 | I != E; ++I) { |
| 117 | unsigned Index = *I; |
| 118 | Node[Index] = ElementVH(To, this); |
| 119 | } |
| 120 | |
| 121 | // Insert updated "this" into the context's folding node set. |
| 122 | // If a node with same element list already exist then before inserting |
| 123 | // updated "this" into the folding node set, replace all uses of existing |
| 124 | // node with updated "this" node. |
| 125 | FoldingSetNodeID ID; |
| 126 | Profile(ID); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 127 | void *InsertPoint; |
| 128 | MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 129 | |
| 130 | if (N) { |
| 131 | N->replaceAllUsesWith(this); |
| 132 | delete N; |
| 133 | N = 0; |
| 134 | } |
| 135 | |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 136 | N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 137 | if (!N) { |
| 138 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 139 | N = this; |
| 140 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 144 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 145 | // NamedMDNode implementation. |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 146 | // |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 147 | NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 148 | MetadataBase *const *MDs, |
Devang Patel | 4a942d0 | 2009-07-29 21:58:56 +0000 | [diff] [blame] | 149 | unsigned NumMDs, Module *ParentModule) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 150 | : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) { |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 151 | setName(N); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 152 | |
Devang Patel | 27e0be2 | 2009-10-21 23:57:35 +0000 | [diff] [blame] | 153 | for (unsigned i = 0; i != NumMDs; ++i) |
Devang Patel | 084679e | 2009-10-22 18:25:28 +0000 | [diff] [blame] | 154 | Node.push_back(TrackingVH<MetadataBase>(MDs[i])); |
Devang Patel | 27e0be2 | 2009-10-21 23:57:35 +0000 | [diff] [blame] | 155 | |
Devang Patel | 4a942d0 | 2009-07-29 21:58:56 +0000 | [diff] [blame] | 156 | if (ParentModule) |
| 157 | ParentModule->getNamedMDList().push_back(this); |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 158 | } |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 159 | |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 160 | NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) { |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 161 | assert(NMD && "Invalid source NamedMDNode!"); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 162 | SmallVector<MetadataBase *, 4> Elems; |
| 163 | for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) |
| 164 | Elems.push_back(NMD->getElement(i)); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 165 | return new NamedMDNode(NMD->getContext(), NMD->getName().data(), |
| 166 | Elems.data(), Elems.size(), M); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 169 | /// eraseFromParent - Drop all references and remove the node from parent |
| 170 | /// module. |
| 171 | void NamedMDNode::eraseFromParent() { |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 172 | getParent()->getNamedMDList().erase(this); |
| 173 | } |
| 174 | |
| 175 | /// dropAllReferences - Remove all uses and clear node vector. |
| 176 | void NamedMDNode::dropAllReferences() { |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 177 | Node.clear(); |
| 178 | } |
| 179 | |
| 180 | NamedMDNode::~NamedMDNode() { |
| 181 | dropAllReferences(); |
| 182 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 183 | |
| 184 | //===----------------------------------------------------------------------===// |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 185 | // MetadataContextImpl implementation. |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 186 | // |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 187 | namespace llvm { |
| 188 | class MetadataContextImpl { |
| 189 | public: |
| 190 | typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; |
| 191 | typedef SmallVector<MDPairTy, 2> MDMapTy; |
| 192 | typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy; |
| 193 | friend class BitcodeReader; |
| 194 | private: |
| 195 | |
| 196 | /// MetadataStore - Collection of metadata used in this context. |
| 197 | MDStoreTy MetadataStore; |
| 198 | |
| 199 | /// MDHandlerNames - Map to hold metadata handler names. |
| 200 | StringMap<unsigned> MDHandlerNames; |
| 201 | |
| 202 | public: |
| 203 | /// registerMDKind - Register a new metadata kind and return its ID. |
| 204 | /// A metadata kind can be registered only once. |
| 205 | unsigned registerMDKind(StringRef Name); |
| 206 | |
| 207 | /// getMDKind - Return metadata kind. If the requested metadata kind |
| 208 | /// is not registered then return 0. |
| 209 | unsigned getMDKind(StringRef Name) const; |
| 210 | |
| 211 | /// getMD - Get the metadata of given kind attached to an Instruction. |
| 212 | /// If the metadata is not found then return 0. |
| 213 | MDNode *getMD(unsigned Kind, const Instruction *Inst); |
| 214 | |
| 215 | /// getMDs - Get the metadata attached to an Instruction. |
| 216 | void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const; |
| 217 | |
| 218 | /// addMD - Attach the metadata of given kind to an Instruction. |
| 219 | void addMD(unsigned Kind, MDNode *Node, Instruction *Inst); |
| 220 | |
| 221 | /// removeMD - Remove metadata of given kind attached with an instuction. |
| 222 | void removeMD(unsigned Kind, Instruction *Inst); |
| 223 | |
| 224 | /// removeAllMetadata - Remove all metadata attached with an instruction. |
| 225 | void removeAllMetadata(Instruction *Inst); |
| 226 | |
| 227 | /// copyMD - If metadata is attached with Instruction In1 then attach |
| 228 | /// the same metadata to In2. |
| 229 | void copyMD(Instruction *In1, Instruction *In2); |
| 230 | |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 231 | /// getHandlerNames - Populate client-supplied smallvector using custom |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 232 | /// metadata name and ID. |
| 233 | void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const; |
| 234 | |
| 235 | /// ValueIsDeleted - This handler is used to update metadata store |
| 236 | /// when a value is deleted. |
| 237 | void ValueIsDeleted(const Value *) {} |
| 238 | void ValueIsDeleted(Instruction *Inst) { |
| 239 | removeAllMetadata(Inst); |
| 240 | } |
| 241 | void ValueIsRAUWd(Value *V1, Value *V2); |
| 242 | |
| 243 | /// ValueIsCloned - This handler is used to update metadata store |
| 244 | /// when In1 is cloned to create In2. |
| 245 | void ValueIsCloned(const Instruction *In1, Instruction *In2); |
| 246 | }; |
| 247 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 248 | |
Devang Patel | 0c35dbd | 2009-10-20 22:50:27 +0000 | [diff] [blame] | 249 | /// registerMDKind - Register a new metadata kind and return its ID. |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 250 | /// A metadata kind can be registered only once. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 251 | unsigned MetadataContextImpl::registerMDKind(StringRef Name) { |
Devang Patel | b1a4477 | 2009-09-28 21:14:55 +0000 | [diff] [blame] | 252 | unsigned Count = MDHandlerNames.size(); |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 253 | assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!"); |
| 254 | return MDHandlerNames[Name] = Count + 1; |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /// getMDKind - Return metadata kind. If the requested metadata kind |
| 258 | /// is not registered then return 0. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 259 | unsigned MetadataContextImpl::getMDKind(StringRef Name) const { |
Devang Patel | 2505c1e | 2009-10-21 21:57:13 +0000 | [diff] [blame] | 260 | StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name); |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 261 | if (I == MDHandlerNames.end()) |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | |
| 264 | return I->getValue(); |
| 265 | } |
| 266 | |
Devang Patel | 0c35dbd | 2009-10-20 22:50:27 +0000 | [diff] [blame] | 267 | /// addMD - Attach the metadata of given kind to an Instruction. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 268 | void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node, |
| 269 | Instruction *Inst) { |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 270 | assert(Node && "Invalid null MDNode"); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 271 | Inst->HasMetadata = true; |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 272 | MDMapTy &Info = MetadataStore[Inst]; |
| 273 | if (Info.empty()) { |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 274 | Info.push_back(std::make_pair(MDKind, Node)); |
| 275 | MetadataStore.insert(std::make_pair(Inst, Info)); |
| 276 | return; |
| 277 | } |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame] | 278 | |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame] | 279 | // If there is an entry for this MDKind then replace it. |
| 280 | for (unsigned i = 0, e = Info.size(); i != e; ++i) { |
| 281 | MDPairTy &P = Info[i]; |
| 282 | if (P.first == MDKind) { |
| 283 | Info[i] = std::make_pair(MDKind, Node); |
| 284 | return; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Otherwise add a new entry. |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 289 | Info.push_back(std::make_pair(MDKind, Node)); |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Devang Patel | b403436 | 2009-09-29 20:42:25 +0000 | [diff] [blame] | 292 | /// removeMD - Remove metadata of given kind attached with an instuction. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 293 | void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) { |
Devang Patel | b403436 | 2009-09-29 20:42:25 +0000 | [diff] [blame] | 294 | MDStoreTy::iterator I = MetadataStore.find(Inst); |
| 295 | if (I == MetadataStore.end()) |
| 296 | return; |
| 297 | |
| 298 | MDMapTy &Info = I->second; |
| 299 | for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) { |
| 300 | MDPairTy &P = *MI; |
| 301 | if (P.first == Kind) { |
| 302 | Info.erase(MI); |
| 303 | return; |
| 304 | } |
| 305 | } |
Devang Patel | b403436 | 2009-09-29 20:42:25 +0000 | [diff] [blame] | 306 | } |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 307 | |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 308 | /// removeAllMetadata - Remove all metadata attached with an instruction. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 309 | void MetadataContextImpl::removeAllMetadata(Instruction *Inst) { |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 310 | MetadataStore.erase(Inst); |
| 311 | Inst->HasMetadata = false; |
Devang Patel | b403436 | 2009-09-29 20:42:25 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Devang Patel | ebaa76e | 2009-10-14 17:02:49 +0000 | [diff] [blame] | 314 | /// copyMD - If metadata is attached with Instruction In1 then attach |
| 315 | /// the same metadata to In2. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 316 | void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) { |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 317 | assert(In1 && In2 && "Invalid instruction!"); |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 318 | MDMapTy &In1Info = MetadataStore[In1]; |
| 319 | if (In1Info.empty()) |
Devang Patel | ebaa76e | 2009-10-14 17:02:49 +0000 | [diff] [blame] | 320 | return; |
| 321 | |
Devang Patel | ebaa76e | 2009-10-14 17:02:49 +0000 | [diff] [blame] | 322 | for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I) |
Devang Patel | 084679e | 2009-10-22 18:25:28 +0000 | [diff] [blame] | 323 | addMD(I->first, I->second, In2); |
Devang Patel | ebaa76e | 2009-10-14 17:02:49 +0000 | [diff] [blame] | 324 | } |
Devang Patel | b403436 | 2009-09-29 20:42:25 +0000 | [diff] [blame] | 325 | |
Devang Patel | 0c35dbd | 2009-10-20 22:50:27 +0000 | [diff] [blame] | 326 | /// getMD - Get the metadata of given kind attached to an Instruction. |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 327 | /// If the metadata is not found then return 0. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 328 | MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) { |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 329 | MDMapTy &Info = MetadataStore[Inst]; |
| 330 | if (Info.empty()) |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame] | 331 | return NULL; |
Devang Patel | 3eb5d33 | 2009-10-21 17:33:41 +0000 | [diff] [blame] | 332 | |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 333 | for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) |
| 334 | if (I->first == MDKind) |
Devang Patel | 084679e | 2009-10-22 18:25:28 +0000 | [diff] [blame] | 335 | return I->second; |
Devang Patel | 5bf7a49 | 2009-09-29 20:30:57 +0000 | [diff] [blame] | 336 | return NULL; |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Devang Patel | 0c35dbd | 2009-10-20 22:50:27 +0000 | [diff] [blame] | 339 | /// getMDs - Get the metadata attached to an Instruction. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 340 | void MetadataContextImpl:: |
Devang Patel | 6da5dbf | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 341 | getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const { |
Jeffrey Yasskin | b40d3f7 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 342 | MDStoreTy::const_iterator I = MetadataStore.find(Inst); |
Devang Patel | dec23fd | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 343 | if (I == MetadataStore.end()) |
Devang Patel | 6da5dbf | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 344 | return; |
Devang Patel | d6dd2a0 | 2009-10-26 17:09:00 +0000 | [diff] [blame] | 345 | MDs.resize(I->second.size()); |
Jeffrey Yasskin | b40d3f7 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 346 | for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end(); |
Devang Patel | 6da5dbf | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 347 | MI != ME; ++MI) |
Devang Patel | d6dd2a0 | 2009-10-26 17:09:00 +0000 | [diff] [blame] | 348 | // MD kinds are numbered from 1. |
| 349 | MDs[MI->first - 1] = std::make_pair(MI->first, MI->second); |
Devang Patel | dec23fd | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Devang Patel | 09c319e | 2009-10-22 17:40:37 +0000 | [diff] [blame] | 352 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 353 | /// metadata name and ID. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 354 | void MetadataContextImpl:: |
Devang Patel | 0fffb49 | 2009-10-22 01:01:24 +0000 | [diff] [blame] | 355 | getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const { |
Devang Patel | d6dd2a0 | 2009-10-26 17:09:00 +0000 | [diff] [blame] | 356 | Names.resize(MDHandlerNames.size()); |
Devang Patel | 0fffb49 | 2009-10-22 01:01:24 +0000 | [diff] [blame] | 357 | for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(), |
| 358 | E = MDHandlerNames.end(); I != E; ++I) |
Devang Patel | d6dd2a0 | 2009-10-26 17:09:00 +0000 | [diff] [blame] | 359 | // MD Handlers are numbered from 1. |
| 360 | Names[I->second - 1] = std::make_pair(I->second, I->first()); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 363 | /// ValueIsCloned - This handler is used to update metadata store |
| 364 | /// when In1 is cloned to create In2. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 365 | void MetadataContextImpl::ValueIsCloned(const Instruction *In1, |
| 366 | Instruction *In2) { |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 367 | // Find Metadata handles for In1. |
| 368 | MDStoreTy::iterator I = MetadataStore.find(In1); |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 369 | assert(I != MetadataStore.end() && "Invalid custom metadata info!"); |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 370 | |
| 371 | // FIXME : Give all metadata handlers a chance to adjust. |
| 372 | |
| 373 | MDMapTy &In1Info = I->second; |
| 374 | MDMapTy In2Info; |
| 375 | for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I) |
Devang Patel | 084679e | 2009-10-22 18:25:28 +0000 | [diff] [blame] | 376 | addMD(I->first, I->second, In2); |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 377 | } |
Devang Patel | e6f26a7 | 2009-10-13 17:00:54 +0000 | [diff] [blame] | 378 | |
| 379 | /// ValueIsRAUWd - This handler is used when V1's all uses are replaced by |
| 380 | /// V2. |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 381 | void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) { |
Devang Patel | e6f26a7 | 2009-10-13 17:00:54 +0000 | [diff] [blame] | 382 | Instruction *I1 = dyn_cast<Instruction>(V1); |
| 383 | Instruction *I2 = dyn_cast<Instruction>(V2); |
| 384 | if (!I1 || !I2) |
| 385 | return; |
| 386 | |
| 387 | // FIXME : Give custom handlers a chance to override this. |
| 388 | ValueIsCloned(I1, I2); |
| 389 | } |
Devang Patel | ebaa76e | 2009-10-14 17:02:49 +0000 | [diff] [blame] | 390 | |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 391 | //===----------------------------------------------------------------------===// |
| 392 | // MetadataContext implementation. |
| 393 | // |
| 394 | MetadataContext::MetadataContext() |
| 395 | : pImpl(new MetadataContextImpl()) { } |
| 396 | MetadataContext::~MetadataContext() { delete pImpl; } |
| 397 | |
| 398 | /// isValidName - Return true if Name is a valid custom metadata handler name. |
| 399 | bool MetadataContext::isValidName(StringRef MDName) { |
| 400 | if (MDName.empty()) |
| 401 | return false; |
| 402 | |
| 403 | if (!isalpha(MDName[0])) |
| 404 | return false; |
| 405 | |
| 406 | for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; |
| 407 | ++I) { |
| 408 | if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.') |
| 409 | return false; |
| 410 | } |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | /// registerMDKind - Register a new metadata kind and return its ID. |
| 415 | /// A metadata kind can be registered only once. |
| 416 | unsigned MetadataContext::registerMDKind(StringRef Name) { |
| 417 | assert(isValidName(Name) && "Invalid custome metadata name!"); |
| 418 | return pImpl->registerMDKind(Name); |
| 419 | } |
| 420 | |
| 421 | /// getMDKind - Return metadata kind. If the requested metadata kind |
| 422 | /// is not registered then return 0. |
| 423 | unsigned MetadataContext::getMDKind(StringRef Name) const { |
| 424 | return pImpl->getMDKind(Name); |
| 425 | } |
| 426 | |
| 427 | /// getMD - Get the metadata of given kind attached to an Instruction. |
| 428 | /// If the metadata is not found then return 0. |
| 429 | MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) { |
| 430 | return pImpl->getMD(Kind, Inst); |
| 431 | } |
| 432 | |
| 433 | /// getMDs - Get the metadata attached to an Instruction. |
| 434 | void MetadataContext:: |
| 435 | getMDs(const Instruction *Inst, |
| 436 | SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const { |
| 437 | return pImpl->getMDs(Inst, MDs); |
| 438 | } |
| 439 | |
| 440 | /// addMD - Attach the metadata of given kind to an Instruction. |
| 441 | void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) { |
| 442 | pImpl->addMD(Kind, Node, Inst); |
| 443 | } |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 444 | |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 445 | /// removeMD - Remove metadata of given kind attached with an instuction. |
| 446 | void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) { |
| 447 | pImpl->removeMD(Kind, Inst); |
| 448 | } |
Nick Lewycky | 898e8f7 | 2009-11-26 22:54:26 +0000 | [diff] [blame^] | 449 | |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 450 | /// removeAllMetadata - Remove all metadata attached with an instruction. |
| 451 | void MetadataContext::removeAllMetadata(Instruction *Inst) { |
| 452 | pImpl->removeAllMetadata(Inst); |
| 453 | } |
| 454 | |
| 455 | /// copyMD - If metadata is attached with Instruction In1 then attach |
| 456 | /// the same metadata to In2. |
| 457 | void MetadataContext::copyMD(Instruction *In1, Instruction *In2) { |
| 458 | pImpl->copyMD(In1, In2); |
| 459 | } |
| 460 | |
| 461 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 462 | /// metadata name and ID. |
| 463 | void MetadataContext:: |
| 464 | getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const { |
| 465 | pImpl->getHandlerNames(N); |
| 466 | } |
| 467 | |
| 468 | /// ValueIsDeleted - This handler is used to update metadata store |
| 469 | /// when a value is deleted. |
| 470 | void MetadataContext::ValueIsDeleted(Instruction *Inst) { |
| 471 | pImpl->ValueIsDeleted(Inst); |
| 472 | } |
| 473 | void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) { |
| 474 | pImpl->ValueIsRAUWd(V1, V2); |
| 475 | } |
| 476 | |
| 477 | /// ValueIsCloned - This handler is used to update metadata store |
| 478 | /// when In1 is cloned to create In2. |
| 479 | void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) { |
| 480 | pImpl->ValueIsCloned(In1, In2); |
| 481 | } |