Devang Patel | 0a9f7b9 | 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 | |
| 14 | #include "llvm/Metadata.h" |
Chris Lattner | 37da0ad | 2009-12-28 08:24:16 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 16 | #include "llvm/LLVMContext.h" |
Devang Patel | f457d13 | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Devang Patel | 937b1e9 | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 18 | #include "llvm/Instruction.h" |
Devang Patel | 9d89df1 | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
Devang Patel | d77fdba | 2010-01-12 18:57:56 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Devang Patel | 28bc9d8 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 23 | #include "SymbolTableListTraitsImpl.h" |
Dan Gohman | 489b29b | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 37da0ad | 2009-12-28 08:24:16 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ValueHandle.h" |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | b2a33b46 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 29 | // MDString implementation. |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 30 | // |
Chris Lattner | 5e9cd43 | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 31 | |
| 32 | MDString::MDString(LLVMContext &C, StringRef S) |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 33 | : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {} |
Chris Lattner | 5e9cd43 | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 34 | |
Devang Patel | 49b63a1 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 35 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 36 | LLVMContextImpl *pImpl = Context.pImpl; |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 37 | StringMapEntry<MDString *> &Entry = |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 38 | pImpl->MDStringCache.GetOrCreateValue(Str); |
| 39 | MDString *&S = Entry.getValue(); |
Nick Lewycky | 2353122 | 2009-11-26 22:54:26 +0000 | [diff] [blame] | 40 | if (!S) S = new MDString(Context, Entry.getKey()); |
| 41 | return S; |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 45 | // MDNodeOperand implementation. |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 46 | // |
| 47 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 48 | // Use CallbackVH to hold MDNode operands. |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 49 | namespace llvm { |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 50 | class MDNodeOperand : public CallbackVH { |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 51 | MDNode *Parent; |
| 52 | public: |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 53 | MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {} |
| 54 | ~MDNodeOperand() {} |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 55 | |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 56 | void set(Value *V) { |
Chris Lattner | 1d880e5 | 2009-12-28 09:10:16 +0000 | [diff] [blame] | 57 | setValPtr(V); |
Chris Lattner | 1d880e5 | 2009-12-28 09:10:16 +0000 | [diff] [blame] | 58 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 59 | |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 60 | virtual void deleted(); |
| 61 | virtual void allUsesReplacedWith(Value *NV); |
| 62 | }; |
| 63 | } // end namespace llvm. |
| 64 | |
| 65 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 66 | void MDNodeOperand::deleted() { |
| 67 | Parent->replaceOperand(this, 0); |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 70 | void MDNodeOperand::allUsesReplacedWith(Value *NV) { |
| 71 | Parent->replaceOperand(this, NV); |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
Chris Lattner | b2a33b46 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 77 | // MDNode implementation. |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 78 | // |
Chris Lattner | c5e08a9 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 80 | /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 81 | /// the end of the MDNode. |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 82 | static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { |
Dan Gohman | ac80975 | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 83 | // Use <= instead of < to permit a one-past-the-end address. |
| 84 | assert(Op <= N->getNumOperands() && "Invalid operand number"); |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 85 | return reinterpret_cast<MDNodeOperand*>(N+1)+Op; |
Chris Lattner | f241018 | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 88 | MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 89 | : Value(Type::getMetadataTy(C), Value::MDNodeVal) { |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 90 | NumOperands = Vals.size(); |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 91 | |
Victor Hernandez | 5d30162 | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 92 | if (isFunctionLocal) |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 93 | setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit); |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 94 | |
| 95 | // Initialize the operand list, which is co-allocated on the end of the node. |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 96 | unsigned i = 0; |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 97 | for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 98 | Op != E; ++Op, ++i) |
| 99 | new (Op) MDNodeOperand(Vals[i], this); |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 102 | |
| 103 | /// ~MDNode - Destroy MDNode. |
| 104 | MDNode::~MDNode() { |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 105 | assert((getSubclassDataFromValue() & DestroyFlag) != 0 && |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 106 | "Not being destroyed through destroy()?"); |
Jeffrey Yasskin | 6f555ca | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 107 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 108 | if (isNotUniqued()) { |
| 109 | pImpl->NonUniquedMDNodes.erase(this); |
| 110 | } else { |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 111 | pImpl->MDNodeSet.RemoveNode(this); |
| 112 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 113 | |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 114 | // Destroy the operands. |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 115 | for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 116 | Op != E; ++Op) |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 117 | Op->~MDNodeOperand(); |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 120 | static const Function *getFunctionForValue(Value *V) { |
| 121 | if (!V) return NULL; |
Duncan Sands | 203f7cb | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 122 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 123 | BasicBlock *BB = I->getParent(); |
| 124 | return BB ? BB->getParent() : 0; |
| 125 | } |
Chris Lattner | d3a6d90 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 126 | if (Argument *A = dyn_cast<Argument>(V)) |
| 127 | return A->getParent(); |
Duncan Sands | 203f7cb | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 128 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 129 | return BB->getParent(); |
| 130 | if (MDNode *MD = dyn_cast<MDNode>(V)) |
| 131 | return MD->getFunction(); |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | |
Victor Hernandez | e05f66e | 2010-01-14 20:12:34 +0000 | [diff] [blame] | 135 | #ifndef NDEBUG |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 136 | static const Function *assertLocalFunction(const MDNode *N) { |
Chris Lattner | d3a6d90 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 137 | if (!N->isFunctionLocal()) return 0; |
Victor Hernandez | e05f66e | 2010-01-14 20:12:34 +0000 | [diff] [blame] | 138 | |
Devang Patel | 6db2389 | 2010-07-06 21:05:17 +0000 | [diff] [blame] | 139 | // FIXME: This does not handle cyclic function local metadata. |
Chris Lattner | d3a6d90 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 140 | const Function *F = 0, *NewF = 0; |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 141 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 142 | if (Value *V = N->getOperand(i)) { |
Chris Lattner | d3a6d90 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 143 | if (MDNode *MD = dyn_cast<MDNode>(V)) |
| 144 | NewF = assertLocalFunction(MD); |
| 145 | else |
| 146 | NewF = getFunctionForValue(V); |
Victor Hernandez | 54630e1 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | d3a6d90 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 148 | if (F == 0) |
| 149 | F = NewF; |
| 150 | else |
| 151 | assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata"); |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 152 | } |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 153 | return F; |
| 154 | } |
Victor Hernandez | 54630e1 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 155 | #endif |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 156 | |
| 157 | // getFunction - If this metadata is function-local and recursively has a |
| 158 | // function-local operand, return the first such operand's parent function. |
Victor Hernandez | 6cead78 | 2010-01-18 22:55:08 +0000 | [diff] [blame] | 159 | // Otherwise, return null. getFunction() should not be used for performance- |
| 160 | // critical code because it recursively visits all the MDNode's operands. |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 161 | const Function *MDNode::getFunction() const { |
Victor Hernandez | 54630e1 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 162 | #ifndef NDEBUG |
| 163 | return assertLocalFunction(this); |
| 164 | #endif |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 165 | if (!isFunctionLocal()) return NULL; |
Duncan Sands | d29f528 | 2010-05-04 14:25:42 +0000 | [diff] [blame] | 166 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 167 | if (const Function *F = getFunctionForValue(getOperand(i))) |
| 168 | return F; |
Victor Hernandez | 54630e1 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 169 | return NULL; |
Victor Hernandez | c7650b4 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 172 | // destroy - Delete this node. Only when there are no uses. |
| 173 | void MDNode::destroy() { |
| 174 | setValueSubclassData(getSubclassDataFromValue() | DestroyFlag); |
| 175 | // Placement delete, the free the memory. |
| 176 | this->~MDNode(); |
| 177 | free(this); |
| 178 | } |
| 179 | |
Chris Lattner | f3f325b | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 180 | /// isFunctionLocalValue - Return true if this is a value that would require a |
| 181 | /// function-local MDNode. |
| 182 | static bool isFunctionLocalValue(Value *V) { |
| 183 | return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) || |
| 184 | (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()); |
| 185 | } |
| 186 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 187 | MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, |
| 188 | FunctionLocalness FL, bool Insert) { |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 189 | LLVMContextImpl *pImpl = Context.pImpl; |
Dan Gohman | 7548fb3 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 190 | |
| 191 | // Add all the operand pointers. Note that we don't have to add the |
| 192 | // isFunctionLocal bit because that's implied by the operands. |
Dan Gohman | 56b092e | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 193 | // Note that if the operands are later nulled out, the node will be |
| 194 | // removed from the uniquing map. |
Dan Gohman | 7548fb3 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 195 | FoldingSetNodeID ID; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 196 | for (unsigned i = 0; i != Vals.size(); ++i) |
Dan Gohman | 7548fb3 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 197 | ID.AddPointer(Vals[i]); |
| 198 | |
| 199 | void *InsertPoint; |
| 200 | MDNode *N = NULL; |
| 201 | |
| 202 | if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint))) |
| 203 | return N; |
| 204 | |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 205 | bool isFunctionLocal = false; |
| 206 | switch (FL) { |
| 207 | case FL_Unknown: |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 208 | for (unsigned i = 0; i != Vals.size(); ++i) { |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 209 | Value *V = Vals[i]; |
| 210 | if (!V) continue; |
Chris Lattner | f3f325b | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 211 | if (isFunctionLocalValue(V)) { |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 212 | isFunctionLocal = true; |
| 213 | break; |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 214 | } |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 215 | } |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 216 | break; |
| 217 | case FL_No: |
| 218 | isFunctionLocal = false; |
| 219 | break; |
| 220 | case FL_Yes: |
| 221 | isFunctionLocal = true; |
| 222 | break; |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 223 | } |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 224 | |
| 225 | // Coallocate space for the node and Operands together, then placement new. |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 226 | void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand)); |
| 227 | N = new (Ptr) MDNode(Context, Vals, isFunctionLocal); |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 228 | |
| 229 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 230 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
| 231 | |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 232 | return N; |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Devang Patel | 566bd12 | 2011-03-04 01:20:33 +0000 | [diff] [blame] | 235 | MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) { |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 236 | return getMDNode(Context, Vals, FL_Unknown); |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 239 | MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, |
| 240 | ArrayRef<Value*> Vals, |
| 241 | bool isFunctionLocal) { |
| 242 | return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No); |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 245 | MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) { |
| 246 | return getMDNode(Context, Vals, FL_Unknown, false); |
Victor Hernandez | e685f23 | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 249 | MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) { |
| 250 | MDNode *N = |
| 251 | (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand)); |
| 252 | N = new (N) MDNode(Context, Vals, FL_No); |
Dan Gohman | 489b29b | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 253 | N->setValueSubclassData(N->getSubclassDataFromValue() | |
| 254 | NotUniquedBit); |
| 255 | LeakDetector::addGarbageObject(N); |
| 256 | return N; |
| 257 | } |
| 258 | |
| 259 | void MDNode::deleteTemporary(MDNode *N) { |
| 260 | assert(N->use_empty() && "Temporary MDNode has uses!"); |
Dan Gohman | 990bdd5 | 2010-08-21 02:52:29 +0000 | [diff] [blame] | 261 | assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) && |
Dan Gohman | 97cfa7d | 2010-08-23 22:32:05 +0000 | [diff] [blame] | 262 | "Deleting a non-temporary uniqued node!"); |
| 263 | assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) && |
| 264 | "Deleting a non-temporary non-uniqued node!"); |
Dan Gohman | 489b29b | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 265 | assert((N->getSubclassDataFromValue() & NotUniquedBit) && |
| 266 | "Temporary MDNode does not have NotUniquedBit set!"); |
| 267 | assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 && |
Dan Gohman | 990bdd5 | 2010-08-21 02:52:29 +0000 | [diff] [blame] | 268 | "Temporary MDNode has DestroyFlag set!"); |
Dan Gohman | 489b29b | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 269 | LeakDetector::removeGarbageObject(N); |
Benjamin Kramer | 9dd3e92 | 2010-08-21 15:07:23 +0000 | [diff] [blame] | 270 | N->destroy(); |
Dan Gohman | 489b29b | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 273 | /// getOperand - Return specified operand. |
| 274 | Value *MDNode::getOperand(unsigned i) const { |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 275 | return *getOperandPtr(const_cast<MDNode*>(this), i); |
| 276 | } |
| 277 | |
Chris Lattner | f241018 | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 278 | void MDNode::Profile(FoldingSetNodeID &ID) const { |
Dan Gohman | 7548fb3 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 279 | // Add all the operand pointers. Note that we don't have to add the |
| 280 | // isFunctionLocal bit because that's implied by the operands. |
Dan Gohman | 56b092e | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 281 | // Note that if the operands are later nulled out, the node will be |
| 282 | // removed from the uniquing map. |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 283 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 284 | ID.AddPointer(getOperand(i)); |
Devang Patel | 4771e16 | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 285 | } |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 286 | |
Jeffrey Yasskin | 6f555ca | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 287 | void MDNode::setIsNotUniqued() { |
| 288 | setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit); |
| 289 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 290 | pImpl->NonUniquedMDNodes.insert(this); |
Devang Patel | 06fdacc | 2010-02-18 20:53:16 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | f241018 | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 293 | // Replace value from this node's operand list. |
| 294 | void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { |
Chris Lattner | df58389 | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 295 | Value *From = *Op; |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 296 | |
Chris Lattner | f3f325b | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 297 | // If is possible that someone did GV->RAUW(inst), replacing a global variable |
| 298 | // with an instruction or some other function-local object. If this is a |
| 299 | // non-function-local MDNode, it can't point to a function-local object. |
| 300 | // Handle this case by implicitly dropping the MDNode reference to null. |
Duncan Sands | 203f7cb | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 301 | // Likewise if the MDNode is function-local but for a different function. |
| 302 | if (To && isFunctionLocalValue(To)) { |
| 303 | if (!isFunctionLocal()) |
| 304 | To = 0; |
| 305 | else { |
| 306 | const Function *F = getFunction(); |
| 307 | const Function *FV = getFunctionForValue(To); |
| 308 | // Metadata can be function-local without having an associated function. |
| 309 | // So only consider functions to have changed if non-null. |
| 310 | if (F && FV && F != FV) |
| 311 | To = 0; |
| 312 | } |
| 313 | } |
Chris Lattner | f3f325b | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | df58389 | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 315 | if (From == To) |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 316 | return; |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 54a1f9f | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 318 | // Update the operand. |
Chris Lattner | b76359e | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 319 | Op->set(To); |
Chris Lattner | 54a1f9f | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 320 | |
| 321 | // If this node is already not being uniqued (because one of the operands |
| 322 | // already went to null), then there is nothing else to do here. |
| 323 | if (isNotUniqued()) return; |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 4964257 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 325 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 326 | |
| 327 | // Remove "this" from the context map. FoldingSet doesn't have to reprofile |
| 328 | // this node to remove it, so we don't care what state the operands are in. |
| 329 | pImpl->MDNodeSet.RemoveNode(this); |
Chris Lattner | df58389 | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 54a1f9f | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 331 | // If we are dropping an argument to null, we choose to not unique the MDNode |
| 332 | // anymore. This commonly occurs during destruction, and uniquing these |
Dan Gohman | 56b092e | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 333 | // brings little reuse. Also, this means we don't need to include |
| 334 | // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes. |
Chris Lattner | 54a1f9f | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 335 | if (To == 0) { |
| 336 | setIsNotUniqued(); |
| 337 | return; |
| 338 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 54a1f9f | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 340 | // Now that the node is out of the folding set, get ready to reinsert it. |
| 341 | // First, check to see if another node with the same operands already exists |
Dan Gohman | f53458f | 2010-09-28 22:07:19 +0000 | [diff] [blame] | 342 | // in the set. If so, then this node is redundant. |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 343 | FoldingSetNodeID ID; |
| 344 | Profile(ID); |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 345 | void *InsertPoint; |
Dan Gohman | f375ebe | 2010-09-28 21:02:55 +0000 | [diff] [blame] | 346 | if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) { |
Dan Gohman | f53458f | 2010-09-28 22:07:19 +0000 | [diff] [blame] | 347 | replaceAllUsesWith(N); |
| 348 | destroy(); |
| 349 | return; |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Chris Lattner | 4964257 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 352 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 353 | pImpl->MDNodeSet.InsertNode(this, InsertPoint); |
Dan Gohman | 5781f3e | 2010-09-14 01:37:57 +0000 | [diff] [blame] | 354 | |
| 355 | // If this MDValue was previously function-local but no longer is, clear |
| 356 | // its function-local flag. |
| 357 | if (isFunctionLocal() && !isFunctionLocalValue(To)) { |
| 358 | bool isStillFunctionLocal = false; |
| 359 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 360 | Value *V = getOperand(i); |
| 361 | if (!V) continue; |
| 362 | if (isFunctionLocalValue(V)) { |
| 363 | isStillFunctionLocal = true; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | if (!isStillFunctionLocal) |
| 368 | setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit); |
| 369 | } |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Devang Patel | f457d13 | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
Chris Lattner | b2a33b46 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 373 | // NamedMDNode implementation. |
Devang Patel | f457d13 | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 374 | // |
Devang Patel | 26028f2 | 2010-01-12 18:34:06 +0000 | [diff] [blame] | 375 | |
Dan Gohman | d9c86dc | 2010-07-21 18:01:42 +0000 | [diff] [blame] | 376 | static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) { |
| 377 | return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands; |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 380 | NamedMDNode::NamedMDNode(const Twine &N) |
| 381 | : Name(N.str()), Parent(0), |
| 382 | Operands(new SmallVector<TrackingVH<MDNode>, 4>()) { |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 385 | NamedMDNode::~NamedMDNode() { |
| 386 | dropAllReferences(); |
| 387 | delete &getNMDOps(Operands); |
| 388 | } |
| 389 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 390 | /// getNumOperands - Return number of NamedMDNode operands. |
| 391 | unsigned NamedMDNode::getNumOperands() const { |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 392 | return (unsigned)getNMDOps(Operands).size(); |
| 393 | } |
| 394 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 395 | /// getOperand - Return specified operand. |
Devang Patel | 3e30c2a | 2010-01-05 20:41:31 +0000 | [diff] [blame] | 396 | MDNode *NamedMDNode::getOperand(unsigned i) const { |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 397 | assert(i < getNumOperands() && "Invalid Operand number!"); |
Dan Gohman | 872814a | 2010-07-21 18:54:18 +0000 | [diff] [blame] | 398 | return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]); |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 401 | /// addOperand - Add metadata Operand. |
Devang Patel | 3e30c2a | 2010-01-05 20:41:31 +0000 | [diff] [blame] | 402 | void NamedMDNode::addOperand(MDNode *M) { |
Dan Gohman | 5781f3e | 2010-09-14 01:37:57 +0000 | [diff] [blame] | 403 | assert(!M->isFunctionLocal() && |
| 404 | "NamedMDNode operands must not be function-local!"); |
Dan Gohman | d9c86dc | 2010-07-21 18:01:42 +0000 | [diff] [blame] | 405 | getNMDOps(Operands).push_back(TrackingVH<MDNode>(M)); |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Devang Patel | a82f883 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 408 | /// eraseFromParent - Drop all references and remove the node from parent |
| 409 | /// module. |
| 410 | void NamedMDNode::eraseFromParent() { |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 411 | getParent()->eraseNamedMetadata(this); |
Devang Patel | a82f883 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /// dropAllReferences - Remove all uses and clear node vector. |
| 415 | void NamedMDNode::dropAllReferences() { |
Chris Lattner | 5710969 | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 416 | getNMDOps(Operands).clear(); |
Devang Patel | a82f883 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 419 | /// getName - Return a constant reference to this named metadata's name. |
| 420 | StringRef NamedMDNode::getName() const { |
| 421 | return StringRef(Name); |
| 422 | } |
Devang Patel | 937b1e9 | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 423 | |
| 424 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 425 | // Instruction Metadata method implementations. |
| 426 | // |
| 427 | |
| 428 | void Instruction::setMetadata(const char *Kind, MDNode *Node) { |
| 429 | if (Node == 0 && !hasMetadata()) return; |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 430 | setMetadata(getContext().getMDKindID(Kind), Node); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | MDNode *Instruction::getMetadataImpl(const char *Kind) const { |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 434 | return getMetadataImpl(getContext().getMDKindID(Kind)); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /// setMetadata - Set the metadata of of the specified kind to the specified |
| 438 | /// node. This updates/replaces metadata if already present, or removes it if |
| 439 | /// Node is null. |
| 440 | void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
| 441 | if (Node == 0 && !hasMetadata()) return; |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 442 | |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 443 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 444 | if (KindID == LLVMContext::MD_dbg) { |
Chris Lattner | 84e679b | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 445 | DbgLoc = DebugLoc::getFromDILocation(Node); |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 449 | // Handle the case when we're adding/updating metadata on an instruction. |
| 450 | if (Node) { |
| 451 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 452 | assert(!Info.empty() == hasMetadataHashEntry() && |
| 453 | "HasMetadata bit is wonked"); |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 454 | if (Info.empty()) { |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 455 | setHasMetadataHashEntry(true); |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 456 | } else { |
| 457 | // Handle replacement of an existing value. |
| 458 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 459 | if (Info[i].first == KindID) { |
| 460 | Info[i].second = Node; |
| 461 | return; |
| 462 | } |
| 463 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 465 | // No replacement, just add it to the list. |
| 466 | Info.push_back(std::make_pair(KindID, Node)); |
| 467 | return; |
| 468 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 470 | // Otherwise, we're removing metadata from an instruction. |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 471 | assert(hasMetadataHashEntry() && |
| 472 | getContext().pImpl->MetadataStore.count(this) && |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 473 | "HasMetadata bit out of date!"); |
| 474 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 476 | // Common case is removing the only entry. |
| 477 | if (Info.size() == 1 && Info[0].first == KindID) { |
| 478 | getContext().pImpl->MetadataStore.erase(this); |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 479 | setHasMetadataHashEntry(false); |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 482 | |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 483 | // Handle removal of an existing value. |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 484 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 485 | if (Info[i].first == KindID) { |
| 486 | Info[i] = Info.back(); |
| 487 | Info.pop_back(); |
| 488 | assert(!Info.empty() && "Removing last entry should be handled above"); |
| 489 | return; |
| 490 | } |
| 491 | // Otherwise, removing an entry that doesn't exist on the instruction. |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 495 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 496 | if (KindID == LLVMContext::MD_dbg) |
Chris Lattner | 61336ae | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 497 | return DbgLoc.getAsMDNode(getContext()); |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 498 | |
| 499 | if (!hasMetadataHashEntry()) return 0; |
| 500 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 501 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 502 | assert(!Info.empty() && "bit out of sync with hash table"); |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 504 | for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); |
| 505 | I != E; ++I) |
| 506 | if (I->first == KindID) |
| 507 | return I->second; |
| 508 | return 0; |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 512 | MDNode*> > &Result) const { |
| 513 | Result.clear(); |
| 514 | |
| 515 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
Chris Lattner | 61336ae | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 516 | if (!DbgLoc.isUnknown()) { |
| 517 | Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, |
| 518 | DbgLoc.getAsMDNode(getContext()))); |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 519 | if (!hasMetadataHashEntry()) return; |
| 520 | } |
| 521 | |
| 522 | assert(hasMetadataHashEntry() && |
| 523 | getContext().pImpl->MetadataStore.count(this) && |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 524 | "Shouldn't have called this"); |
| 525 | const LLVMContextImpl::MDMapTy &Info = |
| 526 | getContext().pImpl->MetadataStore.find(this)->second; |
| 527 | assert(!Info.empty() && "Shouldn't have called this"); |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 529 | Result.append(Info.begin(), Info.end()); |
Mikhail Glushenkov | 0d75d87 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 531 | // Sort the resulting array so it is stable. |
| 532 | if (Result.size() > 1) |
| 533 | array_pod_sort(Result.begin(), Result.end()); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Chris Lattner | 61336ae | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 536 | void Instruction:: |
| 537 | getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, |
| 538 | MDNode*> > &Result) const { |
| 539 | Result.clear(); |
| 540 | assert(hasMetadataHashEntry() && |
| 541 | getContext().pImpl->MetadataStore.count(this) && |
| 542 | "Shouldn't have called this"); |
| 543 | const LLVMContextImpl::MDMapTy &Info = |
| 544 | getContext().pImpl->MetadataStore.find(this)->second; |
| 545 | assert(!Info.empty() && "Shouldn't have called this"); |
| 546 | |
| 547 | Result.append(Info.begin(), Info.end()); |
| 548 | |
| 549 | // Sort the resulting array so it is stable. |
| 550 | if (Result.size() > 1) |
| 551 | array_pod_sort(Result.begin(), Result.end()); |
| 552 | } |
| 553 | |
| 554 | |
Dan Gohman | 4f1be4a | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 555 | /// clearMetadataHashEntries - Clear all hashtable-based metadata from |
| 556 | /// this instruction. |
| 557 | void Instruction::clearMetadataHashEntries() { |
| 558 | assert(hasMetadataHashEntry() && "Caller should check"); |
| 559 | getContext().pImpl->MetadataStore.erase(this); |
| 560 | setHasMetadataHashEntry(false); |
Chris Lattner | 508b19a | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 561 | } |
| 562 | |