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