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