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