blob: 3c6c846b2feee8ae329ece18435b6cb9a2b8ba06 [file] [log] [blame]
Devang Patela4f43fb2009-07-28 21:49:47 +00001//===-- 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 Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Metadata.h"
Chris Lattner1300f452009-12-28 08:24:16 +000015#include "LLVMContextImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "SymbolTableListTraitsImpl.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/STLExtras.h"
Rafael Espindolaab73c492014-01-28 16:56:46 +000019#include "llvm/ADT/SmallSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringMap.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000022#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Instruction.h"
24#include "llvm/IR/LLVMContext.h"
Chandler Carruth4b6845c2014-03-04 12:46:06 +000025#include "llvm/IR/LeakDetector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000027#include "llvm/IR/ValueHandle.h"
Duncan P. N. Exon Smith46d91ad2014-11-14 18:42:06 +000028
Devang Patela4f43fb2009-07-28 21:49:47 +000029using namespace llvm;
30
Duncan P. N. Exon Smitha69934f2014-11-14 18:42:09 +000031Metadata::Metadata(LLVMContext &Context, unsigned ID)
32 : Value(Type::getMetadataTy(Context), ID) {}
33
Devang Patela4f43fb2009-07-28 21:49:47 +000034//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000035// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000036//
Chris Lattner5a409bd2009-12-28 08:30:43 +000037
David Blaikiea379b1812011-12-20 02:50:00 +000038void MDString::anchor() { }
39
Devang Pateldcb99d32009-10-22 00:10:15 +000040MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Duncan P. N. Exon Smithf17e7402014-11-14 01:17:09 +000041 auto &Store = Context.pImpl->MDStringCache;
42 auto I = Store.find(Str);
43 if (I != Store.end())
44 return &I->second;
45
46 auto *Entry =
47 StringMapEntry<MDString>::Create(Str, Store.getAllocator(), Context);
48 bool WasInserted = Store.insert(Entry);
49 (void)WasInserted;
50 assert(WasInserted && "Expected entry to be inserted");
51 return &Entry->second;
52}
53
54StringRef MDString::getString() const {
55 return StringMapEntry<MDString>::GetStringMapEntryFromValue(*this).first();
Owen Anderson0087fe62009-07-31 21:35:40 +000056}
57
58//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000059// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000060//
61
Chris Lattner9b493022009-12-31 01:22:29 +000062// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000063namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000064class MDNodeOperand : public CallbackVH {
Bill Wendling5c0068f2012-04-08 10:20:49 +000065 MDNode *getParent() {
66 MDNodeOperand *Cur = this;
67
68 while (Cur->getValPtrInt() != 1)
69 --Cur;
70
71 assert(Cur->getValPtrInt() == 1 &&
72 "Couldn't find the beginning of the operand list!");
73 return reinterpret_cast<MDNode*>(Cur) - 1;
74 }
75
Chris Lattner74a6ad62009-12-28 07:41:54 +000076public:
Bill Wendling5c0068f2012-04-08 10:20:49 +000077 MDNodeOperand(Value *V) : CallbackVH(V) {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000078 virtual ~MDNodeOperand();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000079
Bill Wendling0156f442012-04-26 00:38:42 +000080 void set(Value *V) {
81 unsigned IsFirst = this->getValPtrInt();
82 this->setValPtr(V);
83 this->setAsFirstOperand(IsFirst);
84 }
Bill Wendling5c0068f2012-04-08 10:20:49 +000085
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +000086 /// \brief Accessor method to mark the operand as the first in the list.
Bill Wendling5c0068f2012-04-08 10:20:49 +000087 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000088
Craig Topperf398d7c2014-03-05 06:35:38 +000089 void deleted() override;
90 void allUsesReplacedWith(Value *NV) override;
Chris Lattner74a6ad62009-12-28 07:41:54 +000091};
92} // end namespace llvm.
93
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000094// Provide out-of-line definition to prevent weak vtable.
95MDNodeOperand::~MDNodeOperand() {}
Chris Lattner74a6ad62009-12-28 07:41:54 +000096
Chris Lattner9b493022009-12-31 01:22:29 +000097void MDNodeOperand::deleted() {
Craig Topperc6207612014-04-09 06:08:46 +000098 getParent()->replaceOperand(this, nullptr);
Chris Lattner74a6ad62009-12-28 07:41:54 +000099}
100
Chris Lattner9b493022009-12-31 01:22:29 +0000101void MDNodeOperand::allUsesReplacedWith(Value *NV) {
Bill Wendling5c0068f2012-04-08 10:20:49 +0000102 getParent()->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +0000103}
104
Chris Lattner74a6ad62009-12-28 07:41:54 +0000105//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000106// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +0000107//
Chris Lattner74a6ad62009-12-28 07:41:54 +0000108
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000109/// \brief Get the MDNodeOperand's coallocated on the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +0000110static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000111 static_assert(sizeof(GenericMDNode) == sizeof(MDNode),
112 "Expected subclasses to have no size overhead");
113 static_assert(sizeof(MDNodeFwdDecl) == sizeof(MDNode),
114 "Expected subclasses to have no size overhead");
115
Dan Gohman1e0213a2010-07-13 19:33:27 +0000116 // Use <= instead of < to permit a one-past-the-end address.
117 assert(Op <= N->getNumOperands() && "Invalid operand number");
Bill Wendling0156f442012-04-26 00:38:42 +0000118 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +0000119}
120
Eric Christopher24e51b72012-02-15 09:09:29 +0000121void MDNode::replaceOperandWith(unsigned i, Value *Val) {
122 MDNodeOperand *Op = getOperandPtr(this, i);
123 replaceOperand(Op, Val);
124}
125
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000126MDNode::MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
127 bool isFunctionLocal)
128 : Metadata(C, ID), Hash(0) {
Jay Foad5514afe2011-04-21 19:59:31 +0000129 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000130
Victor Hernandez0471abd2009-12-18 20:09:14 +0000131 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000132 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000133
134 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +0000135 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000136 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Bill Wendling5c0068f2012-04-08 10:20:49 +0000137 Op != E; ++Op, ++i) {
138 new (Op) MDNodeOperand(Vals[i]);
139
140 // Mark the first MDNodeOperand as being the first in the list of operands.
141 if (i == 0)
142 Op->setAsFirstOperand(1);
143 }
Devang Patela4f43fb2009-07-28 21:49:47 +0000144}
145
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000146GenericMDNode::~GenericMDNode() {
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000147 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
148 if (isNotUniqued()) {
149 pImpl->NonUniquedMDNodes.erase(this);
150 } else {
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000151 pImpl->MDNodeSet.erase(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000152 }
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000153}
154
155MDNode::~MDNode() {
156 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
157 "Not being destroyed through destroy()?");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000158
Chris Lattner8cb6c342009-12-31 01:05:46 +0000159 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000160 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000161 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000162 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000163}
164
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000165static const Function *getFunctionForValue(Value *V) {
Craig Topperc6207612014-04-09 06:08:46 +0000166 if (!V) return nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000167 if (Instruction *I = dyn_cast<Instruction>(V)) {
168 BasicBlock *BB = I->getParent();
Craig Topperc6207612014-04-09 06:08:46 +0000169 return BB ? BB->getParent() : nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000170 }
Chris Lattner5522f052010-01-21 21:01:47 +0000171 if (Argument *A = dyn_cast<Argument>(V))
172 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000173 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
174 return BB->getParent();
175 if (MDNode *MD = dyn_cast<MDNode>(V))
176 return MD->getFunction();
Craig Topperc6207612014-04-09 06:08:46 +0000177 return nullptr;
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000178}
179
Victor Hernandez8296da72010-01-14 20:12:34 +0000180#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000181static const Function *assertLocalFunction(const MDNode *N) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000182 if (!N->isFunctionLocal()) return nullptr;
Victor Hernandez8296da72010-01-14 20:12:34 +0000183
Devang Patelb36df172010-07-06 21:05:17 +0000184 // FIXME: This does not handle cyclic function local metadata.
Craig Topper2617dcc2014-04-15 06:32:26 +0000185 const Function *F = nullptr, *NewF = nullptr;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000186 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000187 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000188 if (MDNode *MD = dyn_cast<MDNode>(V))
189 NewF = assertLocalFunction(MD);
190 else
191 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000192 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000193 if (!F)
Chris Lattner5522f052010-01-21 21:01:47 +0000194 F = NewF;
Craig Topper2617dcc2014-04-15 06:32:26 +0000195 else
196 assert((NewF == nullptr || F == NewF) &&
197 "inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000198 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000199 return F;
200}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000201#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000202
203// getFunction - If this metadata is function-local and recursively has a
204// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000205// Otherwise, return null. getFunction() should not be used for performance-
206// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000207const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000208#ifndef NDEBUG
209 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000210#else
Craig Topperc6207612014-04-09 06:08:46 +0000211 if (!isFunctionLocal()) return nullptr;
Duncan Sands8815f382010-05-04 14:25:42 +0000212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
213 if (const Function *F = getFunctionForValue(getOperand(i)))
214 return F;
Craig Topperc6207612014-04-09 06:08:46 +0000215 return nullptr;
David Blaikie46a9f012012-01-20 21:51:11 +0000216#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000217}
218
Chris Lattner8cb6c342009-12-31 01:05:46 +0000219// destroy - Delete this node. Only when there are no uses.
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000220void GenericMDNode::destroy() {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000221 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
Eric Christopher97f6ea92012-08-14 01:09:10 +0000222 // Placement delete, then free the memory.
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000223 this->~GenericMDNode();
224 free(this);
225}
226
227void MDNodeFwdDecl::destroy() {
228 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
229 // Placement delete, then free the memory.
230 this->~MDNodeFwdDecl();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000231 free(this);
232}
233
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000234/// \brief Check if the Value would require a function-local MDNode.
Chris Lattner450e29c2010-04-28 20:16:12 +0000235static bool isFunctionLocalValue(Value *V) {
236 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
237 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
238}
239
Jay Foad5514afe2011-04-21 19:59:31 +0000240MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
241 FunctionLocalness FL, bool Insert) {
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000242 auto &Store = Context.pImpl->MDNodeSet;
Dan Gohman01579b22010-08-24 23:21:12 +0000243
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000244 GenericMDNodeInfo::KeyTy Key(Vals);
245 auto I = Store.find_as(Key);
246 if (I != Store.end())
247 return *I;
248 if (!Insert)
249 return nullptr;
Duncan Sands26a80f32012-03-31 08:20:11 +0000250
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000251 bool isFunctionLocal = false;
252 switch (FL) {
253 case FL_Unknown:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000254 for (Value *V : Vals) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000255 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000256 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000257 isFunctionLocal = true;
258 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000259 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000260 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000261 break;
262 case FL_No:
263 isFunctionLocal = false;
264 break;
265 case FL_Yes:
266 isFunctionLocal = true;
267 break;
Devang Patelf7188322009-09-03 01:39:20 +0000268 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000269
270 // Coallocate space for the node and Operands together, then placement new.
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000271 void *Ptr =
272 malloc(sizeof(GenericMDNode) + Vals.size() * sizeof(MDNodeOperand));
273 GenericMDNode *N = new (Ptr) GenericMDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000274
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000275 N->Hash = Key.Hash;
276 Store.insert(N);
Devang Patelf7188322009-09-03 01:39:20 +0000277 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000278}
279
Devang Patel213264c2011-03-04 01:20:33 +0000280MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000281 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000282}
283
Jay Foad5514afe2011-04-21 19:59:31 +0000284MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
285 ArrayRef<Value*> Vals,
286 bool isFunctionLocal) {
287 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000288}
289
Jay Foad5514afe2011-04-21 19:59:31 +0000290MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
291 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000292}
293
Jay Foad5514afe2011-04-21 19:59:31 +0000294MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000295 MDNode *N = (MDNode *)malloc(sizeof(MDNodeFwdDecl) +
296 Vals.size() * sizeof(MDNodeOperand));
297 N = new (N) MDNodeFwdDecl(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000298 N->setValueSubclassData(N->getSubclassDataFromValue() |
299 NotUniquedBit);
300 LeakDetector::addGarbageObject(N);
301 return N;
302}
303
304void MDNode::deleteTemporary(MDNode *N) {
305 assert(N->use_empty() && "Temporary MDNode has uses!");
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000306 assert(isa<MDNodeFwdDecl>(N) && "Expected forward declaration");
Dan Gohman16a5d982010-08-20 22:02:26 +0000307 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
308 "Temporary MDNode does not have NotUniquedBit set!");
309 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000310 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000311 LeakDetector::removeGarbageObject(N);
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000312 cast<MDNodeFwdDecl>(N)->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000313}
314
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000315/// \brief Return specified operand.
Chris Lattner9b493022009-12-31 01:22:29 +0000316Value *MDNode::getOperand(unsigned i) const {
David Blaikie58462392013-03-08 21:08:23 +0000317 assert(i < getNumOperands() && "Invalid operand number");
Chris Lattner8cb6c342009-12-31 01:05:46 +0000318 return *getOperandPtr(const_cast<MDNode*>(this), i);
319}
320
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000321void MDNode::setIsNotUniqued() {
322 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
323 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000324 auto *G = cast<GenericMDNode>(this);
325 G->Hash = 0;
326 pImpl->NonUniquedMDNodes.insert(G);
Devang Patel82ab3f82010-02-18 20:53:16 +0000327}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000328
Chris Lattner9b493022009-12-31 01:22:29 +0000329// Replace value from this node's operand list.
330void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000331 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000332
Chris Lattner450e29c2010-04-28 20:16:12 +0000333 // If is possible that someone did GV->RAUW(inst), replacing a global variable
334 // with an instruction or some other function-local object. If this is a
335 // non-function-local MDNode, it can't point to a function-local object.
336 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000337 // Likewise if the MDNode is function-local but for a different function.
338 if (To && isFunctionLocalValue(To)) {
339 if (!isFunctionLocal())
Craig Topperc6207612014-04-09 06:08:46 +0000340 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000341 else {
342 const Function *F = getFunction();
343 const Function *FV = getFunctionForValue(To);
344 // Metadata can be function-local without having an associated function.
345 // So only consider functions to have changed if non-null.
346 if (F && FV && F != FV)
Craig Topperc6207612014-04-09 06:08:46 +0000347 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000348 }
349 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000350
Chris Lattner95c445d2009-12-28 09:32:10 +0000351 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000352 return;
Devang Patelf7188322009-09-03 01:39:20 +0000353
Chris Lattner30ae06b2009-12-30 21:42:11 +0000354 // If this node is already not being uniqued (because one of the operands
355 // already went to null), then there is nothing else to do here.
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000356 if (isNotUniqued()) {
357 Op->set(To);
358 return;
359 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000360
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000361 auto &Store = getContext().pImpl->MDNodeSet;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000362 auto *N = cast<GenericMDNode>(this);
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000363
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000364 // Remove "this" from the context map.
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000365 Store.erase(N);
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000366
367 // Update the operand.
368 Op->set(To);
Chris Lattner95c445d2009-12-28 09:32:10 +0000369
Chris Lattner30ae06b2009-12-30 21:42:11 +0000370 // If we are dropping an argument to null, we choose to not unique the MDNode
371 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000372 // brings little reuse. Also, this means we don't need to include
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000373 // isFunctionLocal bits in the hash for MDNodes.
Craig Topperc6207612014-04-09 06:08:46 +0000374 if (!To) {
Chris Lattner30ae06b2009-12-30 21:42:11 +0000375 setIsNotUniqued();
376 return;
377 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000378
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000379 // Now that the node is out of the table, get ready to reinsert it. First,
380 // check to see if another node with the same operands already exists in the
381 // set. If so, then this node is redundant.
382 SmallVector<Value *, 8> Vals;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000383 GenericMDNodeInfo::KeyTy Key(N, Vals);
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000384 auto I = Store.find_as(Key);
385 if (I != Store.end()) {
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000386 N->replaceAllUsesWith(*I);
387 N->destroy();
Dan Gohman25a7bc92010-09-28 22:07:19 +0000388 return;
Devang Patelf7188322009-09-03 01:39:20 +0000389 }
390
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000391 N->Hash = Key.Hash;
392 Store.insert(N);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000393
394 // If this MDValue was previously function-local but no longer is, clear
395 // its function-local flag.
396 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
397 bool isStillFunctionLocal = false;
398 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
399 Value *V = getOperand(i);
400 if (!V) continue;
401 if (isFunctionLocalValue(V)) {
402 isStillFunctionLocal = true;
403 break;
404 }
405 }
406 if (!isStillFunctionLocal)
407 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
408 }
Devang Patelf7188322009-09-03 01:39:20 +0000409}
410
Hal Finkel94146652014-07-24 14:25:39 +0000411MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
412 if (!A)
413 return B;
414 if (!B)
415 return A;
416
417 SmallVector<Value *, 4> Vals(A->getNumOperands() +
418 B->getNumOperands());
419
420 unsigned j = 0;
421 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
422 Vals[j++] = A->getOperand(i);
423 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
424 Vals[j++] = B->getOperand(i);
425
426 return MDNode::get(A->getContext(), Vals);
427}
428
429MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
430 if (!A || !B)
431 return nullptr;
432
433 SmallVector<Value *, 4> Vals;
434 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
435 Value *V = A->getOperand(i);
436 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
437 if (V == B->getOperand(j)) {
438 Vals.push_back(V);
439 break;
440 }
441 }
442
443 return MDNode::get(A->getContext(), Vals);
444}
445
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000446MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
447 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000448 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000449
450 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
451 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
452 if (AVal.compare(BVal) == APFloat::cmpLessThan)
453 return A;
454 return B;
455}
456
457static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
458 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
459}
460
461static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
462 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
463}
464
Craig Topperb94011f2013-07-14 04:42:23 +0000465static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000466 ConstantInt *High) {
467 ConstantRange NewRange(Low->getValue(), High->getValue());
468 unsigned Size = EndPoints.size();
469 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
470 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
471 ConstantRange LastRange(LB, LE);
472 if (canBeMerged(NewRange, LastRange)) {
473 ConstantRange Union = LastRange.unionWith(NewRange);
474 Type *Ty = High->getType();
475 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
476 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
477 return true;
478 }
479 return false;
480}
481
Craig Topperb94011f2013-07-14 04:42:23 +0000482static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000483 ConstantInt *High) {
484 if (!EndPoints.empty())
485 if (tryMergeRange(EndPoints, Low, High))
486 return;
487
488 EndPoints.push_back(Low);
489 EndPoints.push_back(High);
490}
491
492MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
493 // Given two ranges, we want to compute the union of the ranges. This
494 // is slightly complitade by having to combine the intervals and merge
495 // the ones that overlap.
496
497 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000498 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000499
500 if (A == B)
501 return A;
502
503 // First, walk both lists in older of the lower boundary of each interval.
504 // At each step, try to merge the new interval to the last one we adedd.
505 SmallVector<Value*, 4> EndPoints;
506 int AI = 0;
507 int BI = 0;
508 int AN = A->getNumOperands() / 2;
509 int BN = B->getNumOperands() / 2;
510 while (AI < AN && BI < BN) {
511 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
512 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
513
514 if (ALow->getValue().slt(BLow->getValue())) {
515 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
516 ++AI;
517 } else {
518 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
519 ++BI;
520 }
521 }
522 while (AI < AN) {
523 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
524 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
525 ++AI;
526 }
527 while (BI < BN) {
528 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
529 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
530 ++BI;
531 }
532
533 // If we have more than 2 ranges (4 endpoints) we have to try to merge
534 // the last and first ones.
535 unsigned Size = EndPoints.size();
536 if (Size > 4) {
537 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
538 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
539 if (tryMergeRange(EndPoints, FB, FE)) {
540 for (unsigned i = 0; i < Size - 2; ++i) {
541 EndPoints[i] = EndPoints[i + 2];
542 }
543 EndPoints.resize(Size - 2);
544 }
545 }
546
547 // If in the end we have a single range, it is possible that it is now the
548 // full range. Just drop the metadata in that case.
549 if (EndPoints.size() == 2) {
550 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
551 cast<ConstantInt>(EndPoints[1])->getValue());
552 if (Range.isFullSet())
Craig Topperc6207612014-04-09 06:08:46 +0000553 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000554 }
555
556 return MDNode::get(A->getContext(), EndPoints);
557}
558
Devang Patel05a26fb2009-07-29 00:33:07 +0000559//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000560// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000561//
Devang Patel943ddf62010-01-12 18:34:06 +0000562
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000563static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
564 return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000565}
566
Dan Gohman2637cc12010-07-21 23:38:33 +0000567NamedMDNode::NamedMDNode(const Twine &N)
Duncan P. N. Exon Smithc5754a62014-11-05 18:16:03 +0000568 : Name(N.str()), Parent(nullptr),
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000569 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
Devang Patel5c310be2009-08-11 18:01:24 +0000570
Chris Lattner1bc810b2009-12-28 08:07:14 +0000571NamedMDNode::~NamedMDNode() {
572 dropAllReferences();
573 delete &getNMDOps(Operands);
574}
575
Chris Lattner9b493022009-12-31 01:22:29 +0000576unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000577 return (unsigned)getNMDOps(Operands).size();
578}
579
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000580MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000581 assert(i < getNumOperands() && "Invalid Operand number!");
Duncan P. N. Exon Smithb28deb12014-11-05 01:55:06 +0000582 return &*getNMDOps(Operands)[i];
Chris Lattner1bc810b2009-12-28 08:07:14 +0000583}
584
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000585void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000586 assert(!M->isFunctionLocal() &&
587 "NamedMDNode operands must not be function-local!");
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000588 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000589}
590
Devang Patel79238d72009-08-03 06:19:01 +0000591void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000592 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000593}
594
Devang Patel79238d72009-08-03 06:19:01 +0000595void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000596 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000597}
598
Devang Patelfcfee0f2010-01-07 19:39:36 +0000599StringRef NamedMDNode::getName() const {
600 return StringRef(Name);
601}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000602
603//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000604// Instruction Metadata method implementations.
605//
606
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000607void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
608 if (!Node && !hasMetadata())
609 return;
610 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000611}
612
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000613MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000614 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000615}
616
Rafael Espindolaab73c492014-01-28 16:56:46 +0000617void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
618 SmallSet<unsigned, 5> KnownSet;
619 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
620
621 // Drop debug if needed
622 if (KnownSet.erase(LLVMContext::MD_dbg))
623 DbgLoc = DebugLoc();
624
625 if (!hasMetadataHashEntry())
626 return; // Nothing to remove!
627
628 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
629 getContext().pImpl->MetadataStore;
630
631 if (KnownSet.empty()) {
632 // Just drop our entry at the store.
633 MetadataStore.erase(this);
634 setHasMetadataHashEntry(false);
635 return;
636 }
637
638 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
639 unsigned I;
640 unsigned E;
641 // Walk the array and drop any metadata we don't know.
642 for (I = 0, E = Info.size(); I != E;) {
643 if (KnownSet.count(Info[I].first)) {
644 ++I;
645 continue;
646 }
647
648 Info[I] = Info.back();
649 Info.pop_back();
650 --E;
651 }
652 assert(E == Info.size());
653
654 if (E == 0) {
655 // Drop our entry at the store.
656 MetadataStore.erase(this);
657 setHasMetadataHashEntry(false);
658 }
659}
660
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000661/// setMetadata - Set the metadata of of the specified kind to the specified
662/// node. This updates/replaces metadata if already present, or removes it if
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000663/// Node is null.
664void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
665 if (!Node && !hasMetadata())
666 return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000667
Chris Lattnerc263b422010-03-30 23:03:27 +0000668 // Handle 'dbg' as a special case since it is not stored in the hash table.
669 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000670 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000671 return;
672 }
673
Chris Lattnera0566972009-12-29 09:01:33 +0000674 // Handle the case when we're adding/updating metadata on an instruction.
675 if (Node) {
676 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000677 assert(!Info.empty() == hasMetadataHashEntry() &&
678 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000679 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000680 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000681 } else {
682 // Handle replacement of an existing value.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000683 for (auto &P : Info)
684 if (P.first == KindID) {
685 P.second = Node;
Chris Lattnera0566972009-12-29 09:01:33 +0000686 return;
687 }
688 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000689
Chris Lattnera0566972009-12-29 09:01:33 +0000690 // No replacement, just add it to the list.
691 Info.push_back(std::make_pair(KindID, Node));
692 return;
693 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000694
Chris Lattnera0566972009-12-29 09:01:33 +0000695 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000696 assert((hasMetadataHashEntry() ==
Yaron Keren6d3194f2014-06-20 10:26:56 +0000697 (getContext().pImpl->MetadataStore.count(this) > 0)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000698 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000699 if (!hasMetadataHashEntry())
700 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000701 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000702
Chris Lattnera0566972009-12-29 09:01:33 +0000703 // Common case is removing the only entry.
704 if (Info.size() == 1 && Info[0].first == KindID) {
705 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000706 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000707 return;
708 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000709
Chris Lattnerc263b422010-03-30 23:03:27 +0000710 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000711 for (unsigned i = 0, e = Info.size(); i != e; ++i)
712 if (Info[i].first == KindID) {
713 Info[i] = Info.back();
714 Info.pop_back();
715 assert(!Info.empty() && "Removing last entry should be handled above");
716 return;
717 }
718 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000719}
720
Hal Finkelcc39b672014-07-24 12:16:19 +0000721void Instruction::setAAMetadata(const AAMDNodes &N) {
722 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
Hal Finkel94146652014-07-24 14:25:39 +0000723 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
724 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
Hal Finkelcc39b672014-07-24 12:16:19 +0000725}
726
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000727MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000728 // Handle 'dbg' as a special case since it is not stored in the hash table.
729 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000730 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000731
Craig Topperc6207612014-04-09 06:08:46 +0000732 if (!hasMetadataHashEntry()) return nullptr;
Chris Lattnerc263b422010-03-30 23:03:27 +0000733
Chris Lattnera0566972009-12-29 09:01:33 +0000734 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000735 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000736
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000737 for (const auto &I : Info)
738 if (I.first == KindID)
739 return I.second;
Craig Topperc6207612014-04-09 06:08:46 +0000740 return nullptr;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000741}
742
Duncan P. N. Exon Smith4abd1a02014-11-01 00:26:42 +0000743void Instruction::getAllMetadataImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000744 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000745 Result.clear();
746
747 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000748 if (!DbgLoc.isUnknown()) {
749 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
750 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000751 if (!hasMetadataHashEntry()) return;
752 }
753
754 assert(hasMetadataHashEntry() &&
755 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000756 "Shouldn't have called this");
757 const LLVMContextImpl::MDMapTy &Info =
758 getContext().pImpl->MetadataStore.find(this)->second;
759 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000760
Chris Lattnera0566972009-12-29 09:01:33 +0000761 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000762
Chris Lattnera0566972009-12-29 09:01:33 +0000763 // Sort the resulting array so it is stable.
764 if (Result.size() > 1)
765 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000766}
767
Duncan P. N. Exon Smith3d5a02f2014-11-03 18:13:57 +0000768void Instruction::getAllMetadataOtherThanDebugLocImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000769 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000770 Result.clear();
771 assert(hasMetadataHashEntry() &&
772 getContext().pImpl->MetadataStore.count(this) &&
773 "Shouldn't have called this");
774 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000775 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000776 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000777 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000778
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000779 // Sort the resulting array so it is stable.
780 if (Result.size() > 1)
781 array_pod_sort(Result.begin(), Result.end());
782}
783
Dan Gohman48a995f2010-07-20 22:25:04 +0000784/// clearMetadataHashEntries - Clear all hashtable-based metadata from
785/// this instruction.
786void Instruction::clearMetadataHashEntries() {
787 assert(hasMetadataHashEntry() && "Caller should check");
788 getContext().pImpl->MetadataStore.erase(this);
789 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000790}
791