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 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Metadata.h" |
Chris Lattner | 1300f45 | 2009-12-28 08:24:16 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "SymbolTableListTraitsImpl.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Instruction.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/Module.h" |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConstantRange.h" |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 25 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 1300f45 | 2009-12-28 08:24:16 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 30 | // MDString implementation. |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | 5a409bd | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 32 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 33 | void MDString::anchor() { } |
| 34 | |
Bill Wendling | c4c568b | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 35 | MDString::MDString(LLVMContext &C) |
| 36 | : Value(Type::getMetadataTy(C), Value::MDStringVal) {} |
Chris Lattner | 5a409bd | 2009-12-28 08:30:43 +0000 | [diff] [blame] | 37 | |
Devang Patel | dcb99d3 | 2009-10-22 00:10:15 +0000 | [diff] [blame] | 38 | MDString *MDString::get(LLVMContext &Context, StringRef Str) { |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 39 | LLVMContextImpl *pImpl = Context.pImpl; |
Bill Wendling | c4c568b | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 40 | StringMapEntry<Value*> &Entry = |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 41 | pImpl->MDStringCache.GetOrCreateValue(Str); |
Bill Wendling | c4c568b | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 42 | Value *&S = Entry.getValue(); |
| 43 | if (!S) S = new MDString(Context); |
| 44 | S->setValueName(&Entry); |
| 45 | return cast<MDString>(S); |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 49 | // MDNodeOperand implementation. |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 50 | // |
| 51 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 52 | // Use CallbackVH to hold MDNode operands. |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 53 | namespace llvm { |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 54 | class MDNodeOperand : public CallbackVH { |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 55 | MDNode *getParent() { |
| 56 | MDNodeOperand *Cur = this; |
| 57 | |
| 58 | while (Cur->getValPtrInt() != 1) |
| 59 | --Cur; |
| 60 | |
| 61 | assert(Cur->getValPtrInt() == 1 && |
| 62 | "Couldn't find the beginning of the operand list!"); |
| 63 | return reinterpret_cast<MDNode*>(Cur) - 1; |
| 64 | } |
| 65 | |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 66 | public: |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 67 | MDNodeOperand(Value *V) : CallbackVH(V) {} |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 68 | virtual ~MDNodeOperand(); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 69 | |
Bill Wendling | 0156f44 | 2012-04-26 00:38:42 +0000 | [diff] [blame] | 70 | void set(Value *V) { |
| 71 | unsigned IsFirst = this->getValPtrInt(); |
| 72 | this->setValPtr(V); |
| 73 | this->setAsFirstOperand(IsFirst); |
| 74 | } |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 75 | |
| 76 | /// setAsFirstOperand - Accessor method to mark the operand as the first in |
| 77 | /// the list. |
| 78 | void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 80 | virtual void deleted(); |
| 81 | virtual void allUsesReplacedWith(Value *NV); |
| 82 | }; |
| 83 | } // end namespace llvm. |
| 84 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 85 | // Provide out-of-line definition to prevent weak vtable. |
| 86 | MDNodeOperand::~MDNodeOperand() {} |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 88 | void MDNodeOperand::deleted() { |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 89 | getParent()->replaceOperand(this, 0); |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 92 | void MDNodeOperand::allUsesReplacedWith(Value *NV) { |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 93 | getParent()->replaceOperand(this, NV); |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 97 | // MDNode implementation. |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 98 | // |
Chris Lattner | 74a6ad6 | 2009-12-28 07:41:54 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 100 | /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 101 | /// the end of the MDNode. |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 102 | static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 103 | // Use <= instead of < to permit a one-past-the-end address. |
| 104 | assert(Op <= N->getNumOperands() && "Invalid operand number"); |
Bill Wendling | 0156f44 | 2012-04-26 00:38:42 +0000 | [diff] [blame] | 105 | return reinterpret_cast<MDNodeOperand*>(N + 1) + Op; |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Eric Christopher | 24e51b7 | 2012-02-15 09:09:29 +0000 | [diff] [blame] | 108 | void MDNode::replaceOperandWith(unsigned i, Value *Val) { |
| 109 | MDNodeOperand *Op = getOperandPtr(this, i); |
| 110 | replaceOperand(Op, Val); |
| 111 | } |
| 112 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 113 | MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) |
Devang Patel | ac277eb | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 114 | : Value(Type::getMetadataTy(C), Value::MDNodeVal) { |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 115 | NumOperands = Vals.size(); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 116 | |
Victor Hernandez | 0471abd | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 117 | if (isFunctionLocal) |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 118 | setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 119 | |
| 120 | // Initialize the operand list, which is co-allocated on the end of the node. |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 121 | unsigned i = 0; |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 122 | for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; |
Bill Wendling | 5c0068f | 2012-04-08 10:20:49 +0000 | [diff] [blame] | 123 | Op != E; ++Op, ++i) { |
| 124 | new (Op) MDNodeOperand(Vals[i]); |
| 125 | |
| 126 | // Mark the first MDNodeOperand as being the first in the list of operands. |
| 127 | if (i == 0) |
| 128 | Op->setAsFirstOperand(1); |
| 129 | } |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 132 | /// ~MDNode - Destroy MDNode. |
| 133 | MDNode::~MDNode() { |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 134 | assert((getSubclassDataFromValue() & DestroyFlag) != 0 && |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 135 | "Not being destroyed through destroy()?"); |
Jeffrey Yasskin | 2cc2476 | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 136 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 137 | if (isNotUniqued()) { |
| 138 | pImpl->NonUniquedMDNodes.erase(this); |
| 139 | } else { |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 140 | pImpl->MDNodeSet.RemoveNode(this); |
| 141 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 143 | // Destroy the operands. |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 144 | for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 145 | Op != E; ++Op) |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 146 | Op->~MDNodeOperand(); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 149 | static const Function *getFunctionForValue(Value *V) { |
| 150 | if (!V) return NULL; |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 151 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 152 | BasicBlock *BB = I->getParent(); |
| 153 | return BB ? BB->getParent() : 0; |
| 154 | } |
Chris Lattner | 5522f05 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 155 | if (Argument *A = dyn_cast<Argument>(V)) |
| 156 | return A->getParent(); |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 157 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 158 | return BB->getParent(); |
| 159 | if (MDNode *MD = dyn_cast<MDNode>(V)) |
| 160 | return MD->getFunction(); |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
Victor Hernandez | 8296da7 | 2010-01-14 20:12:34 +0000 | [diff] [blame] | 164 | #ifndef NDEBUG |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 165 | static const Function *assertLocalFunction(const MDNode *N) { |
Chris Lattner | 5522f05 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 166 | if (!N->isFunctionLocal()) return 0; |
Victor Hernandez | 8296da7 | 2010-01-14 20:12:34 +0000 | [diff] [blame] | 167 | |
Devang Patel | b36df17 | 2010-07-06 21:05:17 +0000 | [diff] [blame] | 168 | // FIXME: This does not handle cyclic function local metadata. |
Chris Lattner | 5522f05 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 169 | const Function *F = 0, *NewF = 0; |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 170 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 171 | if (Value *V = N->getOperand(i)) { |
Chris Lattner | 5522f05 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 172 | if (MDNode *MD = dyn_cast<MDNode>(V)) |
| 173 | NewF = assertLocalFunction(MD); |
| 174 | else |
| 175 | NewF = getFunctionForValue(V); |
Victor Hernandez | fdf27a6 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 5522f05 | 2010-01-21 21:01:47 +0000 | [diff] [blame] | 177 | if (F == 0) |
| 178 | F = NewF; |
| 179 | else |
| 180 | assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata"); |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 181 | } |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 182 | return F; |
| 183 | } |
Victor Hernandez | fdf27a6 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 184 | #endif |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 185 | |
| 186 | // getFunction - If this metadata is function-local and recursively has a |
| 187 | // function-local operand, return the first such operand's parent function. |
Victor Hernandez | 06828f0 | 2010-01-18 22:55:08 +0000 | [diff] [blame] | 188 | // Otherwise, return null. getFunction() should not be used for performance- |
| 189 | // critical code because it recursively visits all the MDNode's operands. |
Victor Hernandez | e5f2af7 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 190 | const Function *MDNode::getFunction() const { |
Victor Hernandez | fdf27a6 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 191 | #ifndef NDEBUG |
| 192 | return assertLocalFunction(this); |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 193 | #else |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 194 | if (!isFunctionLocal()) return NULL; |
Duncan Sands | 8815f38 | 2010-05-04 14:25:42 +0000 | [diff] [blame] | 195 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 196 | if (const Function *F = getFunctionForValue(getOperand(i))) |
| 197 | return F; |
Victor Hernandez | fdf27a6 | 2010-01-18 20:36:54 +0000 | [diff] [blame] | 198 | return NULL; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 199 | #endif |
Victor Hernandez | 8c85e25 | 2010-01-14 01:45:14 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 202 | // destroy - Delete this node. Only when there are no uses. |
| 203 | void MDNode::destroy() { |
| 204 | setValueSubclassData(getSubclassDataFromValue() | DestroyFlag); |
Eric Christopher | 97f6ea9 | 2012-08-14 01:09:10 +0000 | [diff] [blame] | 205 | // Placement delete, then free the memory. |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 206 | this->~MDNode(); |
| 207 | free(this); |
| 208 | } |
| 209 | |
Chris Lattner | 450e29c | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 210 | /// isFunctionLocalValue - Return true if this is a value that would require a |
| 211 | /// function-local MDNode. |
| 212 | static bool isFunctionLocalValue(Value *V) { |
| 213 | return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) || |
| 214 | (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()); |
| 215 | } |
| 216 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 217 | MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, |
| 218 | FunctionLocalness FL, bool Insert) { |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 219 | LLVMContextImpl *pImpl = Context.pImpl; |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 220 | |
| 221 | // Add all the operand pointers. Note that we don't have to add the |
| 222 | // isFunctionLocal bit because that's implied by the operands. |
Dan Gohman | 62ddc15 | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 223 | // Note that if the operands are later nulled out, the node will be |
| 224 | // removed from the uniquing map. |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 225 | FoldingSetNodeID ID; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 226 | for (unsigned i = 0; i != Vals.size(); ++i) |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 227 | ID.AddPointer(Vals[i]); |
| 228 | |
| 229 | void *InsertPoint; |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 230 | MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 231 | |
| 232 | if (N || !Insert) |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 233 | return N; |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 234 | |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 235 | bool isFunctionLocal = false; |
| 236 | switch (FL) { |
| 237 | case FL_Unknown: |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 238 | for (unsigned i = 0; i != Vals.size(); ++i) { |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 239 | Value *V = Vals[i]; |
| 240 | if (!V) continue; |
Chris Lattner | 450e29c | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 241 | if (isFunctionLocalValue(V)) { |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 242 | isFunctionLocal = true; |
| 243 | break; |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 244 | } |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 245 | } |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 246 | break; |
| 247 | case FL_No: |
| 248 | isFunctionLocal = false; |
| 249 | break; |
| 250 | case FL_Yes: |
| 251 | isFunctionLocal = true; |
| 252 | break; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 253 | } |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 254 | |
| 255 | // Coallocate space for the node and Operands together, then placement new. |
Bill Wendling | 0156f44 | 2012-04-26 00:38:42 +0000 | [diff] [blame] | 256 | void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 257 | N = new (Ptr) MDNode(Context, Vals, isFunctionLocal); |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 258 | |
Benjamin Kramer | 2335a5c | 2012-04-11 14:06:54 +0000 | [diff] [blame] | 259 | // Cache the operand hash. |
| 260 | N->Hash = ID.ComputeHash(); |
| 261 | |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 262 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 263 | pImpl->MDNodeSet.InsertNode(N, InsertPoint); |
| 264 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 265 | return N; |
Owen Anderson | 0087fe6 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Devang Patel | 213264c | 2011-03-04 01:20:33 +0000 | [diff] [blame] | 268 | MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) { |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 269 | return getMDNode(Context, Vals, FL_Unknown); |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 272 | MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, |
| 273 | ArrayRef<Value*> Vals, |
| 274 | bool isFunctionLocal) { |
| 275 | return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No); |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 278 | MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) { |
| 279 | return getMDNode(Context, Vals, FL_Unknown, false); |
Victor Hernandez | 7e8ce9a | 2010-01-26 02:36:35 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 282 | MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) { |
| 283 | MDNode *N = |
Bill Wendling | 0156f44 | 2012-04-26 00:38:42 +0000 | [diff] [blame] | 284 | (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 285 | N = new (N) MDNode(Context, Vals, FL_No); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 286 | N->setValueSubclassData(N->getSubclassDataFromValue() | |
| 287 | NotUniquedBit); |
| 288 | LeakDetector::addGarbageObject(N); |
| 289 | return N; |
| 290 | } |
| 291 | |
| 292 | void MDNode::deleteTemporary(MDNode *N) { |
| 293 | assert(N->use_empty() && "Temporary MDNode has uses!"); |
Dan Gohman | 573869f | 2010-08-21 02:52:29 +0000 | [diff] [blame] | 294 | assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) && |
Dan Gohman | 5d29673 | 2010-08-23 22:32:05 +0000 | [diff] [blame] | 295 | "Deleting a non-temporary uniqued node!"); |
| 296 | assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) && |
| 297 | "Deleting a non-temporary non-uniqued node!"); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 298 | assert((N->getSubclassDataFromValue() & NotUniquedBit) && |
| 299 | "Temporary MDNode does not have NotUniquedBit set!"); |
| 300 | assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 && |
Dan Gohman | 573869f | 2010-08-21 02:52:29 +0000 | [diff] [blame] | 301 | "Temporary MDNode has DestroyFlag set!"); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 302 | LeakDetector::removeGarbageObject(N); |
Benjamin Kramer | 1f3b0c0 | 2010-08-21 15:07:23 +0000 | [diff] [blame] | 303 | N->destroy(); |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 306 | /// getOperand - Return specified operand. |
| 307 | Value *MDNode::getOperand(unsigned i) const { |
David Blaikie | 5846239 | 2013-03-08 21:08:23 +0000 | [diff] [blame] | 308 | assert(i < getNumOperands() && "Invalid operand number"); |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 309 | return *getOperandPtr(const_cast<MDNode*>(this), i); |
| 310 | } |
| 311 | |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 312 | void MDNode::Profile(FoldingSetNodeID &ID) const { |
Dan Gohman | 01579b2 | 2010-08-24 23:21:12 +0000 | [diff] [blame] | 313 | // Add all the operand pointers. Note that we don't have to add the |
| 314 | // isFunctionLocal bit because that's implied by the operands. |
Dan Gohman | 62ddc15 | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 315 | // Note that if the operands are later nulled out, the node will be |
| 316 | // removed from the uniquing map. |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 317 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 318 | ID.AddPointer(getOperand(i)); |
Devang Patel | d7fd6ab | 2009-08-03 22:51:10 +0000 | [diff] [blame] | 319 | } |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 320 | |
Jeffrey Yasskin | 2cc2476 | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 321 | void MDNode::setIsNotUniqued() { |
| 322 | setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit); |
| 323 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 324 | pImpl->NonUniquedMDNodes.insert(this); |
Devang Patel | 82ab3f8 | 2010-02-18 20:53:16 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | f543eff | 2009-12-28 09:12:35 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 327 | // Replace value from this node's operand list. |
| 328 | void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { |
Chris Lattner | 95c445d | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 329 | Value *From = *Op; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 450e29c | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 331 | // If is possible that someone did GV->RAUW(inst), replacing a global variable |
| 332 | // with an instruction or some other function-local object. If this is a |
| 333 | // non-function-local MDNode, it can't point to a function-local object. |
| 334 | // Handle this case by implicitly dropping the MDNode reference to null. |
Duncan Sands | c2928c6 | 2010-05-04 12:43:36 +0000 | [diff] [blame] | 335 | // Likewise if the MDNode is function-local but for a different function. |
| 336 | if (To && isFunctionLocalValue(To)) { |
| 337 | if (!isFunctionLocal()) |
| 338 | To = 0; |
| 339 | else { |
| 340 | const Function *F = getFunction(); |
| 341 | const Function *FV = getFunctionForValue(To); |
| 342 | // Metadata can be function-local without having an associated function. |
| 343 | // So only consider functions to have changed if non-null. |
| 344 | if (F && FV && F != FV) |
| 345 | To = 0; |
| 346 | } |
| 347 | } |
Chris Lattner | 450e29c | 2010-04-28 20:16:12 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 95c445d | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 349 | if (From == To) |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 350 | return; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 30ae06b | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 352 | // Update the operand. |
Chris Lattner | 8cb6c34 | 2009-12-31 01:05:46 +0000 | [diff] [blame] | 353 | Op->set(To); |
Chris Lattner | 30ae06b | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 354 | |
| 355 | // If this node is already not being uniqued (because one of the operands |
| 356 | // already went to null), then there is nothing else to do here. |
| 357 | if (isNotUniqued()) return; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 358 | |
Chris Lattner | c6d17e2 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 359 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
| 360 | |
| 361 | // Remove "this" from the context map. FoldingSet doesn't have to reprofile |
| 362 | // this node to remove it, so we don't care what state the operands are in. |
| 363 | pImpl->MDNodeSet.RemoveNode(this); |
Chris Lattner | 95c445d | 2009-12-28 09:32:10 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 30ae06b | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 365 | // If we are dropping an argument to null, we choose to not unique the MDNode |
| 366 | // anymore. This commonly occurs during destruction, and uniquing these |
Dan Gohman | 62ddc15 | 2010-08-30 21:18:41 +0000 | [diff] [blame] | 367 | // brings little reuse. Also, this means we don't need to include |
| 368 | // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes. |
Chris Lattner | 30ae06b | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 369 | if (To == 0) { |
| 370 | setIsNotUniqued(); |
| 371 | return; |
| 372 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 30ae06b | 2009-12-30 21:42:11 +0000 | [diff] [blame] | 374 | // Now that the node is out of the folding set, get ready to reinsert it. |
| 375 | // First, check to see if another node with the same operands already exists |
Dan Gohman | 25a7bc9 | 2010-09-28 22:07:19 +0000 | [diff] [blame] | 376 | // in the set. If so, then this node is redundant. |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 377 | FoldingSetNodeID ID; |
| 378 | Profile(ID); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 379 | void *InsertPoint; |
Dan Gohman | 1a450c6 | 2010-09-28 21:02:55 +0000 | [diff] [blame] | 380 | if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) { |
Dan Gohman | 25a7bc9 | 2010-09-28 22:07:19 +0000 | [diff] [blame] | 381 | replaceAllUsesWith(N); |
| 382 | destroy(); |
| 383 | return; |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Benjamin Kramer | 2335a5c | 2012-04-11 14:06:54 +0000 | [diff] [blame] | 386 | // Cache the operand hash. |
| 387 | Hash = ID.ComputeHash(); |
Chris Lattner | c6d17e2 | 2009-12-28 09:24:53 +0000 | [diff] [blame] | 388 | // InsertPoint will have been set by the FindNodeOrInsertPos call. |
| 389 | pImpl->MDNodeSet.InsertNode(this, InsertPoint); |
Dan Gohman | e1328dc | 2010-09-14 01:37:57 +0000 | [diff] [blame] | 390 | |
| 391 | // If this MDValue was previously function-local but no longer is, clear |
| 392 | // its function-local flag. |
| 393 | if (isFunctionLocal() && !isFunctionLocalValue(To)) { |
| 394 | bool isStillFunctionLocal = false; |
| 395 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 396 | Value *V = getOperand(i); |
| 397 | if (!V) continue; |
| 398 | if (isFunctionLocalValue(V)) { |
| 399 | isStillFunctionLocal = true; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | if (!isStillFunctionLocal) |
| 404 | setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit); |
| 405 | } |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 408 | MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { |
| 409 | if (!A || !B) |
| 410 | return NULL; |
| 411 | |
| 412 | APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF(); |
| 413 | APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF(); |
| 414 | if (AVal.compare(BVal) == APFloat::cmpLessThan) |
| 415 | return A; |
| 416 | return B; |
| 417 | } |
| 418 | |
| 419 | static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { |
| 420 | return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); |
| 421 | } |
| 422 | |
| 423 | static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { |
| 424 | return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); |
| 425 | } |
| 426 | |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 427 | static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 428 | ConstantInt *High) { |
| 429 | ConstantRange NewRange(Low->getValue(), High->getValue()); |
| 430 | unsigned Size = EndPoints.size(); |
| 431 | APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue(); |
| 432 | APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue(); |
| 433 | ConstantRange LastRange(LB, LE); |
| 434 | if (canBeMerged(NewRange, LastRange)) { |
| 435 | ConstantRange Union = LastRange.unionWith(NewRange); |
| 436 | Type *Ty = High->getType(); |
| 437 | EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower()); |
| 438 | EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper()); |
| 439 | return true; |
| 440 | } |
| 441 | return false; |
| 442 | } |
| 443 | |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 444 | static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, |
Hal Finkel | 16ddd4b | 2012-06-16 20:33:37 +0000 | [diff] [blame] | 445 | ConstantInt *High) { |
| 446 | if (!EndPoints.empty()) |
| 447 | if (tryMergeRange(EndPoints, Low, High)) |
| 448 | return; |
| 449 | |
| 450 | EndPoints.push_back(Low); |
| 451 | EndPoints.push_back(High); |
| 452 | } |
| 453 | |
| 454 | MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { |
| 455 | // Given two ranges, we want to compute the union of the ranges. This |
| 456 | // is slightly complitade by having to combine the intervals and merge |
| 457 | // the ones that overlap. |
| 458 | |
| 459 | if (!A || !B) |
| 460 | return NULL; |
| 461 | |
| 462 | if (A == B) |
| 463 | return A; |
| 464 | |
| 465 | // First, walk both lists in older of the lower boundary of each interval. |
| 466 | // At each step, try to merge the new interval to the last one we adedd. |
| 467 | SmallVector<Value*, 4> EndPoints; |
| 468 | int AI = 0; |
| 469 | int BI = 0; |
| 470 | int AN = A->getNumOperands() / 2; |
| 471 | int BN = B->getNumOperands() / 2; |
| 472 | while (AI < AN && BI < BN) { |
| 473 | ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI)); |
| 474 | ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI)); |
| 475 | |
| 476 | if (ALow->getValue().slt(BLow->getValue())) { |
| 477 | addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1))); |
| 478 | ++AI; |
| 479 | } else { |
| 480 | addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1))); |
| 481 | ++BI; |
| 482 | } |
| 483 | } |
| 484 | while (AI < AN) { |
| 485 | addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)), |
| 486 | cast<ConstantInt>(A->getOperand(2 * AI + 1))); |
| 487 | ++AI; |
| 488 | } |
| 489 | while (BI < BN) { |
| 490 | addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)), |
| 491 | cast<ConstantInt>(B->getOperand(2 * BI + 1))); |
| 492 | ++BI; |
| 493 | } |
| 494 | |
| 495 | // If we have more than 2 ranges (4 endpoints) we have to try to merge |
| 496 | // the last and first ones. |
| 497 | unsigned Size = EndPoints.size(); |
| 498 | if (Size > 4) { |
| 499 | ConstantInt *FB = cast<ConstantInt>(EndPoints[0]); |
| 500 | ConstantInt *FE = cast<ConstantInt>(EndPoints[1]); |
| 501 | if (tryMergeRange(EndPoints, FB, FE)) { |
| 502 | for (unsigned i = 0; i < Size - 2; ++i) { |
| 503 | EndPoints[i] = EndPoints[i + 2]; |
| 504 | } |
| 505 | EndPoints.resize(Size - 2); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // If in the end we have a single range, it is possible that it is now the |
| 510 | // full range. Just drop the metadata in that case. |
| 511 | if (EndPoints.size() == 2) { |
| 512 | ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(), |
| 513 | cast<ConstantInt>(EndPoints[1])->getValue()); |
| 514 | if (Range.isFullSet()) |
| 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | return MDNode::get(A->getContext(), EndPoints); |
| 519 | } |
| 520 | |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 521 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0c23e8 | 2009-10-19 07:10:59 +0000 | [diff] [blame] | 522 | // NamedMDNode implementation. |
Devang Patel | 05a26fb | 2009-07-29 00:33:07 +0000 | [diff] [blame] | 523 | // |
Devang Patel | 943ddf6 | 2010-01-12 18:34:06 +0000 | [diff] [blame] | 524 | |
Dan Gohman | 846b9e1 | 2010-07-21 18:01:42 +0000 | [diff] [blame] | 525 | static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) { |
| 526 | return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands; |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 529 | NamedMDNode::NamedMDNode(const Twine &N) |
| 530 | : Name(N.str()), Parent(0), |
| 531 | Operands(new SmallVector<TrackingVH<MDNode>, 4>()) { |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 534 | NamedMDNode::~NamedMDNode() { |
| 535 | dropAllReferences(); |
| 536 | delete &getNMDOps(Operands); |
| 537 | } |
| 538 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 539 | /// getNumOperands - Return number of NamedMDNode operands. |
| 540 | unsigned NamedMDNode::getNumOperands() const { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 541 | return (unsigned)getNMDOps(Operands).size(); |
| 542 | } |
| 543 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 544 | /// getOperand - Return specified operand. |
Devang Patel | e307348 | 2010-01-05 20:41:31 +0000 | [diff] [blame] | 545 | MDNode *NamedMDNode::getOperand(unsigned i) const { |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 546 | assert(i < getNumOperands() && "Invalid Operand number!"); |
Dan Gohman | 093cb79 | 2010-07-21 18:54:18 +0000 | [diff] [blame] | 547 | return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]); |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 550 | /// addOperand - Add metadata Operand. |
Devang Patel | e307348 | 2010-01-05 20:41:31 +0000 | [diff] [blame] | 551 | void NamedMDNode::addOperand(MDNode *M) { |
Dan Gohman | e1328dc | 2010-09-14 01:37:57 +0000 | [diff] [blame] | 552 | assert(!M->isFunctionLocal() && |
| 553 | "NamedMDNode operands must not be function-local!"); |
Dan Gohman | 846b9e1 | 2010-07-21 18:01:42 +0000 | [diff] [blame] | 554 | getNMDOps(Operands).push_back(TrackingVH<MDNode>(M)); |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 557 | /// eraseFromParent - Drop all references and remove the node from parent |
| 558 | /// module. |
| 559 | void NamedMDNode::eraseFromParent() { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 560 | getParent()->eraseNamedMetadata(this); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /// dropAllReferences - Remove all uses and clear node vector. |
| 564 | void NamedMDNode::dropAllReferences() { |
Chris Lattner | 1bc810b | 2009-12-28 08:07:14 +0000 | [diff] [blame] | 565 | getNMDOps(Operands).clear(); |
Devang Patel | 79238d7 | 2009-08-03 06:19:01 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 568 | /// getName - Return a constant reference to this named metadata's name. |
| 569 | StringRef NamedMDNode::getName() const { |
| 570 | return StringRef(Name); |
| 571 | } |
Devang Patel | d5497a4b | 2009-09-16 18:09:00 +0000 | [diff] [blame] | 572 | |
| 573 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 574 | // Instruction Metadata method implementations. |
| 575 | // |
| 576 | |
Benjamin Kramer | b3bd019 | 2011-12-06 11:50:26 +0000 | [diff] [blame] | 577 | void Instruction::setMetadata(StringRef Kind, MDNode *Node) { |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 578 | if (Node == 0 && !hasMetadata()) return; |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 579 | setMetadata(getContext().getMDKindID(Kind), Node); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Benjamin Kramer | b3bd019 | 2011-12-06 11:50:26 +0000 | [diff] [blame] | 582 | MDNode *Instruction::getMetadataImpl(StringRef Kind) const { |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 583 | return getMetadataImpl(getContext().getMDKindID(Kind)); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// setMetadata - Set the metadata of of the specified kind to the specified |
| 587 | /// node. This updates/replaces metadata if already present, or removes it if |
| 588 | /// Node is null. |
| 589 | void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
| 590 | if (Node == 0 && !hasMetadata()) return; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 591 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 592 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 593 | if (KindID == LLVMContext::MD_dbg) { |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 594 | DbgLoc = DebugLoc::getFromDILocation(Node); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 598 | // Handle the case when we're adding/updating metadata on an instruction. |
| 599 | if (Node) { |
| 600 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 601 | assert(!Info.empty() == hasMetadataHashEntry() && |
| 602 | "HasMetadata bit is wonked"); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 603 | if (Info.empty()) { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 604 | setHasMetadataHashEntry(true); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 605 | } else { |
| 606 | // Handle replacement of an existing value. |
| 607 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 608 | if (Info[i].first == KindID) { |
| 609 | Info[i].second = Node; |
| 610 | return; |
| 611 | } |
| 612 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 613 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 614 | // No replacement, just add it to the list. |
| 615 | Info.push_back(std::make_pair(KindID, Node)); |
| 616 | return; |
| 617 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 618 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 619 | // Otherwise, we're removing metadata from an instruction. |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 620 | assert((hasMetadataHashEntry() == |
| 621 | getContext().pImpl->MetadataStore.count(this)) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 622 | "HasMetadata bit out of date!"); |
Nick Lewycky | 4c13138 | 2011-12-27 01:17:40 +0000 | [diff] [blame] | 623 | if (!hasMetadataHashEntry()) |
| 624 | return; // Nothing to remove! |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 625 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 626 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 627 | // Common case is removing the only entry. |
| 628 | if (Info.size() == 1 && Info[0].first == KindID) { |
| 629 | getContext().pImpl->MetadataStore.erase(this); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 630 | setHasMetadataHashEntry(false); |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 631 | return; |
| 632 | } |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 633 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 634 | // Handle removal of an existing value. |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 635 | for (unsigned i = 0, e = Info.size(); i != e; ++i) |
| 636 | if (Info[i].first == KindID) { |
| 637 | Info[i] = Info.back(); |
| 638 | Info.pop_back(); |
| 639 | assert(!Info.empty() && "Removing last entry should be handled above"); |
| 640 | return; |
| 641 | } |
| 642 | // Otherwise, removing an entry that doesn't exist on the instruction. |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 646 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
| 647 | if (KindID == LLVMContext::MD_dbg) |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 648 | return DbgLoc.getAsMDNode(getContext()); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 649 | |
| 650 | if (!hasMetadataHashEntry()) return 0; |
| 651 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 652 | LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 653 | assert(!Info.empty() && "bit out of sync with hash table"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 654 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 655 | for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); |
| 656 | I != E; ++I) |
| 657 | if (I->first == KindID) |
| 658 | return I->second; |
| 659 | return 0; |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 663 | MDNode*> > &Result) const { |
| 664 | Result.clear(); |
| 665 | |
| 666 | // Handle 'dbg' as a special case since it is not stored in the hash table. |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 667 | if (!DbgLoc.isUnknown()) { |
| 668 | Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, |
| 669 | DbgLoc.getAsMDNode(getContext()))); |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 670 | if (!hasMetadataHashEntry()) return; |
| 671 | } |
| 672 | |
| 673 | assert(hasMetadataHashEntry() && |
| 674 | getContext().pImpl->MetadataStore.count(this) && |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 675 | "Shouldn't have called this"); |
| 676 | const LLVMContextImpl::MDMapTy &Info = |
| 677 | getContext().pImpl->MetadataStore.find(this)->second; |
| 678 | assert(!Info.empty() && "Shouldn't have called this"); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 679 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 680 | Result.append(Info.begin(), Info.end()); |
Mikhail Glushenkov | ed3bd13 | 2010-01-10 18:48:49 +0000 | [diff] [blame] | 681 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 682 | // Sort the resulting array so it is stable. |
| 683 | if (Result.size() > 1) |
| 684 | array_pod_sort(Result.begin(), Result.end()); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 687 | void Instruction:: |
| 688 | getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, |
| 689 | MDNode*> > &Result) const { |
| 690 | Result.clear(); |
| 691 | assert(hasMetadataHashEntry() && |
| 692 | getContext().pImpl->MetadataStore.count(this) && |
| 693 | "Shouldn't have called this"); |
| 694 | const LLVMContextImpl::MDMapTy &Info = |
Bill Wendling | dd91e73 | 2012-04-03 10:50:09 +0000 | [diff] [blame] | 695 | getContext().pImpl->MetadataStore.find(this)->second; |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 696 | assert(!Info.empty() && "Shouldn't have called this"); |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 697 | Result.append(Info.begin(), Info.end()); |
Bill Wendling | dd91e73 | 2012-04-03 10:50:09 +0000 | [diff] [blame] | 698 | |
Chris Lattner | c0f5ce3 | 2010-04-01 05:23:13 +0000 | [diff] [blame] | 699 | // Sort the resulting array so it is stable. |
| 700 | if (Result.size() > 1) |
| 701 | array_pod_sort(Result.begin(), Result.end()); |
| 702 | } |
| 703 | |
Dan Gohman | 48a995f | 2010-07-20 22:25:04 +0000 | [diff] [blame] | 704 | /// clearMetadataHashEntries - Clear all hashtable-based metadata from |
| 705 | /// this instruction. |
| 706 | void Instruction::clearMetadataHashEntries() { |
| 707 | assert(hasMetadataHashEntry() && "Caller should check"); |
| 708 | getContext().pImpl->MetadataStore.erase(this); |
| 709 | setHasMetadataHashEntry(false); |
Chris Lattner | 6801780 | 2009-12-29 07:44:16 +0000 | [diff] [blame] | 710 | } |
| 711 | |