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