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