blob: 27ba9f7a87ba68f2fb3a0a0430b2c80b49422024 [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)
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000069 ++Cur;
Bill Wendling5c0068f2012-04-08 10:20:49 +000070
71 assert(Cur->getValPtrInt() == 1 &&
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000072 "Couldn't find the end of the operand list!");
73 return reinterpret_cast<MDNode *>(Cur + 1);
Bill Wendling5c0068f2012-04-08 10:20:49 +000074 }
75
Chris Lattner74a6ad62009-12-28 07:41:54 +000076public:
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000077 MDNodeOperand() {}
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) {
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000081 unsigned IsLast = this->getValPtrInt();
Bill Wendling0156f442012-04-26 00:38:42 +000082 this->setValPtr(V);
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000083 this->setAsLastOperand(IsLast);
Bill Wendling0156f442012-04-26 00:38:42 +000084 }
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.
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +000087 void setAsLastOperand(unsigned I) { this->setValPtrInt(I); }
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) {
Dan Gohman1e0213a2010-07-13 19:33:27 +0000111 // Use <= instead of < to permit a one-past-the-end address.
112 assert(Op <= N->getNumOperands() && "Invalid operand number");
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000113 return reinterpret_cast<MDNodeOperand *>(N) - N->getNumOperands() + Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +0000114}
115
Eric Christopher24e51b72012-02-15 09:09:29 +0000116void MDNode::replaceOperandWith(unsigned i, Value *Val) {
117 MDNodeOperand *Op = getOperandPtr(this, i);
118 replaceOperand(Op, Val);
119}
120
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000121void *MDNode::operator new(size_t Size, unsigned NumOps) {
122 void *Ptr = ::operator new(Size + NumOps * sizeof(MDNodeOperand));
123 MDNodeOperand *Op = static_cast<MDNodeOperand *>(Ptr);
124 if (NumOps) {
125 MDNodeOperand *Last = Op + NumOps;
126 for (; Op != Last; ++Op)
127 new (Op) MDNodeOperand();
128 (Op - 1)->setAsLastOperand(1);
129 }
130 return Op;
131}
132
133void MDNode::operator delete(void *Mem) {
134 MDNode *N = static_cast<MDNode *>(Mem);
135 MDNodeOperand *Op = static_cast<MDNodeOperand *>(Mem);
136 for (unsigned I = 0, E = N->NumOperands; I != E; ++I)
137 (--Op)->~MDNodeOperand();
138 ::operator delete(Op);
139}
140
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000141MDNode::MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
142 bool isFunctionLocal)
Duncan P. N. Exon Smith4db24cc2014-11-18 02:20:29 +0000143 : Metadata(C, ID) {
Jay Foad5514afe2011-04-21 19:59:31 +0000144 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000145
Victor Hernandez0471abd2009-12-18 20:09:14 +0000146 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000147 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000148
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000149 // Initialize the operand list.
Jay Foad5514afe2011-04-21 19:59:31 +0000150 unsigned i = 0;
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000151 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op + NumOperands;
152 Op != E; ++Op, ++i)
153 Op->set(Vals[i]);
Devang Patela4f43fb2009-07-28 21:49:47 +0000154}
155
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000156GenericMDNode::~GenericMDNode() {
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000157 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
158 if (isNotUniqued()) {
159 pImpl->NonUniquedMDNodes.erase(this);
160 } else {
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000161 pImpl->MDNodeSet.erase(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000162 }
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000163}
164
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000165void GenericMDNode::dropAllReferences() {
166 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op + NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000167 Op != E; ++Op)
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000168 Op->set(nullptr);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000169}
170
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000171static const Function *getFunctionForValue(Value *V) {
Craig Topperc6207612014-04-09 06:08:46 +0000172 if (!V) return nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000173 if (Instruction *I = dyn_cast<Instruction>(V)) {
174 BasicBlock *BB = I->getParent();
Craig Topperc6207612014-04-09 06:08:46 +0000175 return BB ? BB->getParent() : nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000176 }
Chris Lattner5522f052010-01-21 21:01:47 +0000177 if (Argument *A = dyn_cast<Argument>(V))
178 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000179 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
180 return BB->getParent();
181 if (MDNode *MD = dyn_cast<MDNode>(V))
182 return MD->getFunction();
Craig Topperc6207612014-04-09 06:08:46 +0000183 return nullptr;
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000184}
185
Victor Hernandez8296da72010-01-14 20:12:34 +0000186#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000187static const Function *assertLocalFunction(const MDNode *N) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000188 if (!N->isFunctionLocal()) return nullptr;
Victor Hernandez8296da72010-01-14 20:12:34 +0000189
Devang Patelb36df172010-07-06 21:05:17 +0000190 // FIXME: This does not handle cyclic function local metadata.
Craig Topper2617dcc2014-04-15 06:32:26 +0000191 const Function *F = nullptr, *NewF = nullptr;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000192 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000193 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000194 if (MDNode *MD = dyn_cast<MDNode>(V))
195 NewF = assertLocalFunction(MD);
196 else
197 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000198 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000199 if (!F)
Chris Lattner5522f052010-01-21 21:01:47 +0000200 F = NewF;
Craig Topper2617dcc2014-04-15 06:32:26 +0000201 else
202 assert((NewF == nullptr || F == NewF) &&
203 "inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000204 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000205 return F;
206}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000207#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000208
209// getFunction - If this metadata is function-local and recursively has a
210// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000211// Otherwise, return null. getFunction() should not be used for performance-
212// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000213const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000214#ifndef NDEBUG
215 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000216#else
Craig Topperc6207612014-04-09 06:08:46 +0000217 if (!isFunctionLocal()) return nullptr;
Duncan Sands8815f382010-05-04 14:25:42 +0000218 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
219 if (const Function *F = getFunctionForValue(getOperand(i)))
220 return F;
Craig Topperc6207612014-04-09 06:08:46 +0000221 return nullptr;
David Blaikie46a9f012012-01-20 21:51:11 +0000222#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000223}
224
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000225/// \brief Check if the Value would require a function-local MDNode.
Chris Lattner450e29c2010-04-28 20:16:12 +0000226static bool isFunctionLocalValue(Value *V) {
227 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
228 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
229}
230
Jay Foad5514afe2011-04-21 19:59:31 +0000231MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
232 FunctionLocalness FL, bool Insert) {
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000233 auto &Store = Context.pImpl->MDNodeSet;
Dan Gohman01579b22010-08-24 23:21:12 +0000234
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000235 GenericMDNodeInfo::KeyTy Key(Vals);
236 auto I = Store.find_as(Key);
237 if (I != Store.end())
238 return *I;
239 if (!Insert)
240 return nullptr;
Duncan Sands26a80f32012-03-31 08:20:11 +0000241
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000242 bool isFunctionLocal = false;
243 switch (FL) {
244 case FL_Unknown:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000245 for (Value *V : Vals) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000246 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000247 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000248 isFunctionLocal = true;
249 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000250 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000251 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000252 break;
253 case FL_No:
254 isFunctionLocal = false;
255 break;
256 case FL_Yes:
257 isFunctionLocal = true;
258 break;
Devang Patelf7188322009-09-03 01:39:20 +0000259 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000260
261 // Coallocate space for the node and Operands together, then placement new.
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000262 GenericMDNode *N =
263 new (Vals.size()) GenericMDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000264
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000265 N->Hash = Key.Hash;
266 Store.insert(N);
Devang Patelf7188322009-09-03 01:39:20 +0000267 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000268}
269
Devang Patel213264c2011-03-04 01:20:33 +0000270MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000271 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000272}
273
Jay Foad5514afe2011-04-21 19:59:31 +0000274MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
275 ArrayRef<Value*> Vals,
276 bool isFunctionLocal) {
277 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000278}
279
Jay Foad5514afe2011-04-21 19:59:31 +0000280MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
281 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000282}
283
Jay Foad5514afe2011-04-21 19:59:31 +0000284MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000285 MDNode *N = new (Vals.size()) MDNodeFwdDecl(Context, Vals, FL_No);
286 N->setValueSubclassData(N->getSubclassDataFromValue() | NotUniquedBit);
Dan Gohman16a5d982010-08-20 22:02:26 +0000287 LeakDetector::addGarbageObject(N);
288 return N;
289}
290
291void MDNode::deleteTemporary(MDNode *N) {
292 assert(N->use_empty() && "Temporary MDNode has uses!");
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000293 assert(isa<MDNodeFwdDecl>(N) && "Expected forward declaration");
Dan Gohman16a5d982010-08-20 22:02:26 +0000294 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
295 "Temporary MDNode does not have NotUniquedBit set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000296 LeakDetector::removeGarbageObject(N);
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000297 delete cast<MDNodeFwdDecl>(N);
Dan Gohman16a5d982010-08-20 22:02:26 +0000298}
299
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000300/// \brief Return specified operand.
Chris Lattner9b493022009-12-31 01:22:29 +0000301Value *MDNode::getOperand(unsigned i) const {
David Blaikie58462392013-03-08 21:08:23 +0000302 assert(i < getNumOperands() && "Invalid operand number");
Chris Lattner8cb6c342009-12-31 01:05:46 +0000303 return *getOperandPtr(const_cast<MDNode*>(this), i);
304}
305
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000306void MDNode::setIsNotUniqued() {
307 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
308 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000309 auto *G = cast<GenericMDNode>(this);
310 G->Hash = 0;
311 pImpl->NonUniquedMDNodes.insert(G);
Devang Patel82ab3f82010-02-18 20:53:16 +0000312}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000313
Chris Lattner9b493022009-12-31 01:22:29 +0000314// Replace value from this node's operand list.
315void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000316 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000317
Chris Lattner450e29c2010-04-28 20:16:12 +0000318 // If is possible that someone did GV->RAUW(inst), replacing a global variable
319 // with an instruction or some other function-local object. If this is a
320 // non-function-local MDNode, it can't point to a function-local object.
321 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000322 // Likewise if the MDNode is function-local but for a different function.
323 if (To && isFunctionLocalValue(To)) {
324 if (!isFunctionLocal())
Craig Topperc6207612014-04-09 06:08:46 +0000325 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000326 else {
327 const Function *F = getFunction();
328 const Function *FV = getFunctionForValue(To);
329 // Metadata can be function-local without having an associated function.
330 // So only consider functions to have changed if non-null.
331 if (F && FV && F != FV)
Craig Topperc6207612014-04-09 06:08:46 +0000332 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000333 }
334 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000335
Chris Lattner95c445d2009-12-28 09:32:10 +0000336 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000337 return;
Devang Patelf7188322009-09-03 01:39:20 +0000338
Chris Lattner30ae06b2009-12-30 21:42:11 +0000339 // If this node is already not being uniqued (because one of the operands
340 // already went to null), then there is nothing else to do here.
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000341 if (isNotUniqued()) {
342 Op->set(To);
343 return;
344 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000345
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000346 auto &Store = getContext().pImpl->MDNodeSet;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000347 auto *N = cast<GenericMDNode>(this);
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000348
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000349 // Remove "this" from the context map.
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000350 Store.erase(N);
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000351
352 // Update the operand.
353 Op->set(To);
Chris Lattner95c445d2009-12-28 09:32:10 +0000354
Chris Lattner30ae06b2009-12-30 21:42:11 +0000355 // If we are dropping an argument to null, we choose to not unique the MDNode
356 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000357 // brings little reuse. Also, this means we don't need to include
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000358 // isFunctionLocal bits in the hash for MDNodes.
Craig Topperc6207612014-04-09 06:08:46 +0000359 if (!To) {
Chris Lattner30ae06b2009-12-30 21:42:11 +0000360 setIsNotUniqued();
361 return;
362 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000363
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000364 // Now that the node is out of the table, get ready to reinsert it. First,
365 // check to see if another node with the same operands already exists in the
366 // set. If so, then this node is redundant.
367 SmallVector<Value *, 8> Vals;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000368 GenericMDNodeInfo::KeyTy Key(N, Vals);
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000369 auto I = Store.find_as(Key);
370 if (I != Store.end()) {
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000371 N->replaceAllUsesWith(*I);
Duncan P. N. Exon Smithc23610b2014-11-18 01:56:14 +0000372 delete N;
Dan Gohman25a7bc92010-09-28 22:07:19 +0000373 return;
Devang Patelf7188322009-09-03 01:39:20 +0000374 }
375
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +0000376 N->Hash = Key.Hash;
377 Store.insert(N);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000378
379 // If this MDValue was previously function-local but no longer is, clear
380 // its function-local flag.
381 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
382 bool isStillFunctionLocal = false;
383 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
384 Value *V = getOperand(i);
385 if (!V) continue;
386 if (isFunctionLocalValue(V)) {
387 isStillFunctionLocal = true;
388 break;
389 }
390 }
391 if (!isStillFunctionLocal)
392 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
393 }
Devang Patelf7188322009-09-03 01:39:20 +0000394}
395
Hal Finkel94146652014-07-24 14:25:39 +0000396MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
397 if (!A)
398 return B;
399 if (!B)
400 return A;
401
402 SmallVector<Value *, 4> Vals(A->getNumOperands() +
403 B->getNumOperands());
404
405 unsigned j = 0;
406 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
407 Vals[j++] = A->getOperand(i);
408 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
409 Vals[j++] = B->getOperand(i);
410
411 return MDNode::get(A->getContext(), Vals);
412}
413
414MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
415 if (!A || !B)
416 return nullptr;
417
418 SmallVector<Value *, 4> Vals;
419 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
420 Value *V = A->getOperand(i);
421 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
422 if (V == B->getOperand(j)) {
423 Vals.push_back(V);
424 break;
425 }
426 }
427
428 return MDNode::get(A->getContext(), Vals);
429}
430
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000431MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
432 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000433 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000434
435 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
436 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
437 if (AVal.compare(BVal) == APFloat::cmpLessThan)
438 return A;
439 return B;
440}
441
442static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
443 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
444}
445
446static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
447 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
448}
449
Craig Topperb94011f2013-07-14 04:42:23 +0000450static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000451 ConstantInt *High) {
452 ConstantRange NewRange(Low->getValue(), High->getValue());
453 unsigned Size = EndPoints.size();
454 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
455 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
456 ConstantRange LastRange(LB, LE);
457 if (canBeMerged(NewRange, LastRange)) {
458 ConstantRange Union = LastRange.unionWith(NewRange);
459 Type *Ty = High->getType();
460 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
461 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
462 return true;
463 }
464 return false;
465}
466
Craig Topperb94011f2013-07-14 04:42:23 +0000467static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000468 ConstantInt *High) {
469 if (!EndPoints.empty())
470 if (tryMergeRange(EndPoints, Low, High))
471 return;
472
473 EndPoints.push_back(Low);
474 EndPoints.push_back(High);
475}
476
477MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
478 // Given two ranges, we want to compute the union of the ranges. This
479 // is slightly complitade by having to combine the intervals and merge
480 // the ones that overlap.
481
482 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000483 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000484
485 if (A == B)
486 return A;
487
488 // First, walk both lists in older of the lower boundary of each interval.
489 // At each step, try to merge the new interval to the last one we adedd.
490 SmallVector<Value*, 4> EndPoints;
491 int AI = 0;
492 int BI = 0;
493 int AN = A->getNumOperands() / 2;
494 int BN = B->getNumOperands() / 2;
495 while (AI < AN && BI < BN) {
496 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
497 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
498
499 if (ALow->getValue().slt(BLow->getValue())) {
500 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
501 ++AI;
502 } else {
503 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
504 ++BI;
505 }
506 }
507 while (AI < AN) {
508 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
509 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
510 ++AI;
511 }
512 while (BI < BN) {
513 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
514 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
515 ++BI;
516 }
517
518 // If we have more than 2 ranges (4 endpoints) we have to try to merge
519 // the last and first ones.
520 unsigned Size = EndPoints.size();
521 if (Size > 4) {
522 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
523 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
524 if (tryMergeRange(EndPoints, FB, FE)) {
525 for (unsigned i = 0; i < Size - 2; ++i) {
526 EndPoints[i] = EndPoints[i + 2];
527 }
528 EndPoints.resize(Size - 2);
529 }
530 }
531
532 // If in the end we have a single range, it is possible that it is now the
533 // full range. Just drop the metadata in that case.
534 if (EndPoints.size() == 2) {
535 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
536 cast<ConstantInt>(EndPoints[1])->getValue());
537 if (Range.isFullSet())
Craig Topperc6207612014-04-09 06:08:46 +0000538 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000539 }
540
541 return MDNode::get(A->getContext(), EndPoints);
542}
543
Devang Patel05a26fb2009-07-29 00:33:07 +0000544//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000545// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000546//
Devang Patel943ddf62010-01-12 18:34:06 +0000547
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000548static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
549 return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000550}
551
Dan Gohman2637cc12010-07-21 23:38:33 +0000552NamedMDNode::NamedMDNode(const Twine &N)
Duncan P. N. Exon Smithc5754a62014-11-05 18:16:03 +0000553 : Name(N.str()), Parent(nullptr),
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000554 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
Devang Patel5c310be2009-08-11 18:01:24 +0000555
Chris Lattner1bc810b2009-12-28 08:07:14 +0000556NamedMDNode::~NamedMDNode() {
557 dropAllReferences();
558 delete &getNMDOps(Operands);
559}
560
Chris Lattner9b493022009-12-31 01:22:29 +0000561unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000562 return (unsigned)getNMDOps(Operands).size();
563}
564
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000565MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000566 assert(i < getNumOperands() && "Invalid Operand number!");
Duncan P. N. Exon Smithb28deb12014-11-05 01:55:06 +0000567 return &*getNMDOps(Operands)[i];
Chris Lattner1bc810b2009-12-28 08:07:14 +0000568}
569
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000570void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000571 assert(!M->isFunctionLocal() &&
572 "NamedMDNode operands must not be function-local!");
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000573 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000574}
575
Devang Patel79238d72009-08-03 06:19:01 +0000576void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000577 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000578}
579
Devang Patel79238d72009-08-03 06:19:01 +0000580void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000581 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000582}
583
Devang Patelfcfee0f2010-01-07 19:39:36 +0000584StringRef NamedMDNode::getName() const {
585 return StringRef(Name);
586}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000587
588//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000589// Instruction Metadata method implementations.
590//
591
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000592void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
593 if (!Node && !hasMetadata())
594 return;
595 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000596}
597
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000598MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000599 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000600}
601
Rafael Espindolaab73c492014-01-28 16:56:46 +0000602void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
603 SmallSet<unsigned, 5> KnownSet;
604 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
605
606 // Drop debug if needed
607 if (KnownSet.erase(LLVMContext::MD_dbg))
608 DbgLoc = DebugLoc();
609
610 if (!hasMetadataHashEntry())
611 return; // Nothing to remove!
612
613 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
614 getContext().pImpl->MetadataStore;
615
616 if (KnownSet.empty()) {
617 // Just drop our entry at the store.
618 MetadataStore.erase(this);
619 setHasMetadataHashEntry(false);
620 return;
621 }
622
623 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
624 unsigned I;
625 unsigned E;
626 // Walk the array and drop any metadata we don't know.
627 for (I = 0, E = Info.size(); I != E;) {
628 if (KnownSet.count(Info[I].first)) {
629 ++I;
630 continue;
631 }
632
633 Info[I] = Info.back();
634 Info.pop_back();
635 --E;
636 }
637 assert(E == Info.size());
638
639 if (E == 0) {
640 // Drop our entry at the store.
641 MetadataStore.erase(this);
642 setHasMetadataHashEntry(false);
643 }
644}
645
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000646/// setMetadata - Set the metadata of of the specified kind to the specified
647/// node. This updates/replaces metadata if already present, or removes it if
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000648/// Node is null.
649void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
650 if (!Node && !hasMetadata())
651 return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000652
Chris Lattnerc263b422010-03-30 23:03:27 +0000653 // Handle 'dbg' as a special case since it is not stored in the hash table.
654 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000655 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000656 return;
657 }
658
Chris Lattnera0566972009-12-29 09:01:33 +0000659 // Handle the case when we're adding/updating metadata on an instruction.
660 if (Node) {
661 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000662 assert(!Info.empty() == hasMetadataHashEntry() &&
663 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000664 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000665 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000666 } else {
667 // Handle replacement of an existing value.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000668 for (auto &P : Info)
669 if (P.first == KindID) {
670 P.second = Node;
Chris Lattnera0566972009-12-29 09:01:33 +0000671 return;
672 }
673 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000674
Chris Lattnera0566972009-12-29 09:01:33 +0000675 // No replacement, just add it to the list.
676 Info.push_back(std::make_pair(KindID, Node));
677 return;
678 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000679
Chris Lattnera0566972009-12-29 09:01:33 +0000680 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000681 assert((hasMetadataHashEntry() ==
Yaron Keren6d3194f2014-06-20 10:26:56 +0000682 (getContext().pImpl->MetadataStore.count(this) > 0)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000683 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000684 if (!hasMetadataHashEntry())
685 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000686 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000687
Chris Lattnera0566972009-12-29 09:01:33 +0000688 // Common case is removing the only entry.
689 if (Info.size() == 1 && Info[0].first == KindID) {
690 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000691 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000692 return;
693 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000694
Chris Lattnerc263b422010-03-30 23:03:27 +0000695 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000696 for (unsigned i = 0, e = Info.size(); i != e; ++i)
697 if (Info[i].first == KindID) {
698 Info[i] = Info.back();
699 Info.pop_back();
700 assert(!Info.empty() && "Removing last entry should be handled above");
701 return;
702 }
703 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000704}
705
Hal Finkelcc39b672014-07-24 12:16:19 +0000706void Instruction::setAAMetadata(const AAMDNodes &N) {
707 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
Hal Finkel94146652014-07-24 14:25:39 +0000708 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
709 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
Hal Finkelcc39b672014-07-24 12:16:19 +0000710}
711
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000712MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000713 // Handle 'dbg' as a special case since it is not stored in the hash table.
714 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000715 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000716
Craig Topperc6207612014-04-09 06:08:46 +0000717 if (!hasMetadataHashEntry()) return nullptr;
Chris Lattnerc263b422010-03-30 23:03:27 +0000718
Chris Lattnera0566972009-12-29 09:01:33 +0000719 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000720 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000721
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000722 for (const auto &I : Info)
723 if (I.first == KindID)
724 return I.second;
Craig Topperc6207612014-04-09 06:08:46 +0000725 return nullptr;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000726}
727
Duncan P. N. Exon Smith4abd1a02014-11-01 00:26:42 +0000728void Instruction::getAllMetadataImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000729 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000730 Result.clear();
731
732 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000733 if (!DbgLoc.isUnknown()) {
734 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
735 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000736 if (!hasMetadataHashEntry()) return;
737 }
738
739 assert(hasMetadataHashEntry() &&
740 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000741 "Shouldn't have called this");
742 const LLVMContextImpl::MDMapTy &Info =
743 getContext().pImpl->MetadataStore.find(this)->second;
744 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000745
Chris Lattnera0566972009-12-29 09:01:33 +0000746 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000747
Chris Lattnera0566972009-12-29 09:01:33 +0000748 // Sort the resulting array so it is stable.
749 if (Result.size() > 1)
750 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000751}
752
Duncan P. N. Exon Smith3d5a02f2014-11-03 18:13:57 +0000753void Instruction::getAllMetadataOtherThanDebugLocImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000754 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000755 Result.clear();
756 assert(hasMetadataHashEntry() &&
757 getContext().pImpl->MetadataStore.count(this) &&
758 "Shouldn't have called this");
759 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000760 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000761 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000762 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000763
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000764 // Sort the resulting array so it is stable.
765 if (Result.size() > 1)
766 array_pod_sort(Result.begin(), Result.end());
767}
768
Dan Gohman48a995f2010-07-20 22:25:04 +0000769/// clearMetadataHashEntries - Clear all hashtable-based metadata from
770/// this instruction.
771void Instruction::clearMetadataHashEntries() {
772 assert(hasMetadataHashEntry() && "Caller should check");
773 getContext().pImpl->MetadataStore.erase(this);
774 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000775}
776