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