blob: 53c7fc8d5c40729f99e0f291771ff3083e805059 [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
31//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000032// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000033//
Chris Lattner5a409bd2009-12-28 08:30:43 +000034
David Blaikiea379b1812011-12-20 02:50:00 +000035void MDString::anchor() { }
36
Bill Wendlingc4c568b2012-04-10 20:12:16 +000037MDString::MDString(LLVMContext &C)
38 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000039
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) {
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");
Bill Wendling0156f442012-04-26 00:38:42 +0000113 return reinterpret_cast<MDNodeOperand*>(N + 1) + 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 Smithf8edc4a2014-11-13 13:17:47 +0000121MDNode::MDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
122 : Metadata(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foad5514afe2011-04-21 19:59:31 +0000123 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000124
Victor Hernandez0471abd2009-12-18 20:09:14 +0000125 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000126 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000127
128 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +0000129 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000130 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Bill Wendling5c0068f2012-04-08 10:20:49 +0000131 Op != E; ++Op, ++i) {
132 new (Op) MDNodeOperand(Vals[i]);
133
134 // Mark the first MDNodeOperand as being the first in the list of operands.
135 if (i == 0)
136 Op->setAsFirstOperand(1);
137 }
Devang Patela4f43fb2009-07-28 21:49:47 +0000138}
139
Chris Lattner8cb6c342009-12-31 01:05:46 +0000140/// ~MDNode - Destroy MDNode.
141MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000142 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000143 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000144 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
145 if (isNotUniqued()) {
146 pImpl->NonUniquedMDNodes.erase(this);
147 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000148 pImpl->MDNodeSet.RemoveNode(this);
149 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000150
Chris Lattner8cb6c342009-12-31 01:05:46 +0000151 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000152 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000153 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000154 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000155}
156
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000157static const Function *getFunctionForValue(Value *V) {
Craig Topperc6207612014-04-09 06:08:46 +0000158 if (!V) return nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000159 if (Instruction *I = dyn_cast<Instruction>(V)) {
160 BasicBlock *BB = I->getParent();
Craig Topperc6207612014-04-09 06:08:46 +0000161 return BB ? BB->getParent() : nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000162 }
Chris Lattner5522f052010-01-21 21:01:47 +0000163 if (Argument *A = dyn_cast<Argument>(V))
164 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000165 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
166 return BB->getParent();
167 if (MDNode *MD = dyn_cast<MDNode>(V))
168 return MD->getFunction();
Craig Topperc6207612014-04-09 06:08:46 +0000169 return nullptr;
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000170}
171
Victor Hernandez8296da72010-01-14 20:12:34 +0000172#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000173static const Function *assertLocalFunction(const MDNode *N) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000174 if (!N->isFunctionLocal()) return nullptr;
Victor Hernandez8296da72010-01-14 20:12:34 +0000175
Devang Patelb36df172010-07-06 21:05:17 +0000176 // FIXME: This does not handle cyclic function local metadata.
Craig Topper2617dcc2014-04-15 06:32:26 +0000177 const Function *F = nullptr, *NewF = nullptr;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000178 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000179 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000180 if (MDNode *MD = dyn_cast<MDNode>(V))
181 NewF = assertLocalFunction(MD);
182 else
183 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000184 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000185 if (!F)
Chris Lattner5522f052010-01-21 21:01:47 +0000186 F = NewF;
Craig Topper2617dcc2014-04-15 06:32:26 +0000187 else
188 assert((NewF == nullptr || F == NewF) &&
189 "inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000190 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000191 return F;
192}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000193#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000194
195// getFunction - If this metadata is function-local and recursively has a
196// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000197// Otherwise, return null. getFunction() should not be used for performance-
198// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000199const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000200#ifndef NDEBUG
201 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000202#else
Craig Topperc6207612014-04-09 06:08:46 +0000203 if (!isFunctionLocal()) return nullptr;
Duncan Sands8815f382010-05-04 14:25:42 +0000204 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
205 if (const Function *F = getFunctionForValue(getOperand(i)))
206 return F;
Craig Topperc6207612014-04-09 06:08:46 +0000207 return nullptr;
David Blaikie46a9f012012-01-20 21:51:11 +0000208#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000209}
210
Chris Lattner8cb6c342009-12-31 01:05:46 +0000211// destroy - Delete this node. Only when there are no uses.
212void MDNode::destroy() {
213 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
Eric Christopher97f6ea92012-08-14 01:09:10 +0000214 // Placement delete, then free the memory.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000215 this->~MDNode();
216 free(this);
217}
218
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000219/// \brief Check if the Value would require a function-local MDNode.
Chris Lattner450e29c2010-04-28 20:16:12 +0000220static bool isFunctionLocalValue(Value *V) {
221 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
222 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
223}
224
Jay Foad5514afe2011-04-21 19:59:31 +0000225MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
226 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000227 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000228
229 // Add all the operand pointers. Note that we don't have to add the
230 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000231 // Note that if the operands are later nulled out, the node will be
232 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000233 FoldingSetNodeID ID;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000234 for (Value *V : Vals)
235 ID.AddPointer(V);
Dan Gohman01579b22010-08-24 23:21:12 +0000236
237 void *InsertPoint;
Duncan Sands26a80f32012-03-31 08:20:11 +0000238 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
239
240 if (N || !Insert)
Dan Gohman01579b22010-08-24 23:21:12 +0000241 return N;
Duncan Sands26a80f32012-03-31 08:20:11 +0000242
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000243 bool isFunctionLocal = false;
244 switch (FL) {
245 case FL_Unknown:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000246 for (Value *V : Vals) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000247 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000248 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000249 isFunctionLocal = true;
250 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000251 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000252 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000253 break;
254 case FL_No:
255 isFunctionLocal = false;
256 break;
257 case FL_Yes:
258 isFunctionLocal = true;
259 break;
Devang Patelf7188322009-09-03 01:39:20 +0000260 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000261
262 // Coallocate space for the node and Operands together, then placement new.
Bill Wendling0156f442012-04-26 00:38:42 +0000263 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foad5514afe2011-04-21 19:59:31 +0000264 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000265
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000266 // Cache the operand hash.
267 N->Hash = ID.ComputeHash();
268
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000269 // InsertPoint will have been set by the FindNodeOrInsertPos call.
270 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
271
Devang Patelf7188322009-09-03 01:39:20 +0000272 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000273}
274
Devang Patel213264c2011-03-04 01:20:33 +0000275MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000276 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000277}
278
Jay Foad5514afe2011-04-21 19:59:31 +0000279MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
280 ArrayRef<Value*> Vals,
281 bool isFunctionLocal) {
282 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000283}
284
Jay Foad5514afe2011-04-21 19:59:31 +0000285MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
286 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000287}
288
Jay Foad5514afe2011-04-21 19:59:31 +0000289MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
290 MDNode *N =
Bill Wendling0156f442012-04-26 00:38:42 +0000291 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foad5514afe2011-04-21 19:59:31 +0000292 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000293 N->setValueSubclassData(N->getSubclassDataFromValue() |
294 NotUniquedBit);
295 LeakDetector::addGarbageObject(N);
296 return N;
297}
298
299void MDNode::deleteTemporary(MDNode *N) {
300 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000301 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000302 "Deleting a non-temporary uniqued node!");
303 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
304 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000305 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
306 "Temporary MDNode does not have NotUniquedBit set!");
307 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000308 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000309 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000310 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000311}
312
Duncan P. N. Exon Smithfcece4d2014-10-15 20:28:31 +0000313/// \brief Return specified operand.
Chris Lattner9b493022009-12-31 01:22:29 +0000314Value *MDNode::getOperand(unsigned i) const {
David Blaikie58462392013-03-08 21:08:23 +0000315 assert(i < getNumOperands() && "Invalid operand number");
Chris Lattner8cb6c342009-12-31 01:05:46 +0000316 return *getOperandPtr(const_cast<MDNode*>(this), i);
317}
318
Chris Lattnerf543eff2009-12-28 09:12:35 +0000319void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000320 // Add all the operand pointers. Note that we don't have to add the
321 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000322 // Note that if the operands are later nulled out, the node will be
323 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000324 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
325 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000326}
Devang Patel5c310be2009-08-11 18:01:24 +0000327
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000328void MDNode::setIsNotUniqued() {
329 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
330 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
331 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000332}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000333
Chris Lattner9b493022009-12-31 01:22:29 +0000334// Replace value from this node's operand list.
335void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000336 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000337
Chris Lattner450e29c2010-04-28 20:16:12 +0000338 // If is possible that someone did GV->RAUW(inst), replacing a global variable
339 // with an instruction or some other function-local object. If this is a
340 // non-function-local MDNode, it can't point to a function-local object.
341 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000342 // Likewise if the MDNode is function-local but for a different function.
343 if (To && isFunctionLocalValue(To)) {
344 if (!isFunctionLocal())
Craig Topperc6207612014-04-09 06:08:46 +0000345 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000346 else {
347 const Function *F = getFunction();
348 const Function *FV = getFunctionForValue(To);
349 // Metadata can be function-local without having an associated function.
350 // So only consider functions to have changed if non-null.
351 if (F && FV && F != FV)
Craig Topperc6207612014-04-09 06:08:46 +0000352 To = nullptr;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000353 }
354 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000355
Chris Lattner95c445d2009-12-28 09:32:10 +0000356 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000357 return;
Devang Patelf7188322009-09-03 01:39:20 +0000358
Chris Lattner30ae06b2009-12-30 21:42:11 +0000359 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000360 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000361
362 // If this node is already not being uniqued (because one of the operands
363 // already went to null), then there is nothing else to do here.
364 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000365
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000366 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
367
368 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
369 // this node to remove it, so we don't care what state the operands are in.
370 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000371
Chris Lattner30ae06b2009-12-30 21:42:11 +0000372 // If we are dropping an argument to null, we choose to not unique the MDNode
373 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000374 // brings little reuse. Also, this means we don't need to include
375 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Craig Topperc6207612014-04-09 06:08:46 +0000376 if (!To) {
Chris Lattner30ae06b2009-12-30 21:42:11 +0000377 setIsNotUniqued();
378 return;
379 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000380
Chris Lattner30ae06b2009-12-30 21:42:11 +0000381 // Now that the node is out of the folding set, get ready to reinsert it.
382 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000383 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000384 FoldingSetNodeID ID;
385 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000386 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000387 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000388 replaceAllUsesWith(N);
389 destroy();
390 return;
Devang Patelf7188322009-09-03 01:39:20 +0000391 }
392
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000393 // Cache the operand hash.
394 Hash = ID.ComputeHash();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000395 // InsertPoint will have been set by the FindNodeOrInsertPos call.
396 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000397
398 // If this MDValue was previously function-local but no longer is, clear
399 // its function-local flag.
400 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
401 bool isStillFunctionLocal = false;
402 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
403 Value *V = getOperand(i);
404 if (!V) continue;
405 if (isFunctionLocalValue(V)) {
406 isStillFunctionLocal = true;
407 break;
408 }
409 }
410 if (!isStillFunctionLocal)
411 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
412 }
Devang Patelf7188322009-09-03 01:39:20 +0000413}
414
Hal Finkel94146652014-07-24 14:25:39 +0000415MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
416 if (!A)
417 return B;
418 if (!B)
419 return A;
420
421 SmallVector<Value *, 4> Vals(A->getNumOperands() +
422 B->getNumOperands());
423
424 unsigned j = 0;
425 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
426 Vals[j++] = A->getOperand(i);
427 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
428 Vals[j++] = B->getOperand(i);
429
430 return MDNode::get(A->getContext(), Vals);
431}
432
433MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
434 if (!A || !B)
435 return nullptr;
436
437 SmallVector<Value *, 4> Vals;
438 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
439 Value *V = A->getOperand(i);
440 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
441 if (V == B->getOperand(j)) {
442 Vals.push_back(V);
443 break;
444 }
445 }
446
447 return MDNode::get(A->getContext(), Vals);
448}
449
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000450MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
451 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000452 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000453
454 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
455 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
456 if (AVal.compare(BVal) == APFloat::cmpLessThan)
457 return A;
458 return B;
459}
460
461static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
462 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
463}
464
465static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
466 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
467}
468
Craig Topperb94011f2013-07-14 04:42:23 +0000469static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000470 ConstantInt *High) {
471 ConstantRange NewRange(Low->getValue(), High->getValue());
472 unsigned Size = EndPoints.size();
473 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
474 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
475 ConstantRange LastRange(LB, LE);
476 if (canBeMerged(NewRange, LastRange)) {
477 ConstantRange Union = LastRange.unionWith(NewRange);
478 Type *Ty = High->getType();
479 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
480 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
481 return true;
482 }
483 return false;
484}
485
Craig Topperb94011f2013-07-14 04:42:23 +0000486static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000487 ConstantInt *High) {
488 if (!EndPoints.empty())
489 if (tryMergeRange(EndPoints, Low, High))
490 return;
491
492 EndPoints.push_back(Low);
493 EndPoints.push_back(High);
494}
495
496MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
497 // Given two ranges, we want to compute the union of the ranges. This
498 // is slightly complitade by having to combine the intervals and merge
499 // the ones that overlap.
500
501 if (!A || !B)
Craig Topperc6207612014-04-09 06:08:46 +0000502 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000503
504 if (A == B)
505 return A;
506
507 // First, walk both lists in older of the lower boundary of each interval.
508 // At each step, try to merge the new interval to the last one we adedd.
509 SmallVector<Value*, 4> EndPoints;
510 int AI = 0;
511 int BI = 0;
512 int AN = A->getNumOperands() / 2;
513 int BN = B->getNumOperands() / 2;
514 while (AI < AN && BI < BN) {
515 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
516 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
517
518 if (ALow->getValue().slt(BLow->getValue())) {
519 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
520 ++AI;
521 } else {
522 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
523 ++BI;
524 }
525 }
526 while (AI < AN) {
527 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
528 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
529 ++AI;
530 }
531 while (BI < BN) {
532 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
533 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
534 ++BI;
535 }
536
537 // If we have more than 2 ranges (4 endpoints) we have to try to merge
538 // the last and first ones.
539 unsigned Size = EndPoints.size();
540 if (Size > 4) {
541 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
542 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
543 if (tryMergeRange(EndPoints, FB, FE)) {
544 for (unsigned i = 0; i < Size - 2; ++i) {
545 EndPoints[i] = EndPoints[i + 2];
546 }
547 EndPoints.resize(Size - 2);
548 }
549 }
550
551 // If in the end we have a single range, it is possible that it is now the
552 // full range. Just drop the metadata in that case.
553 if (EndPoints.size() == 2) {
554 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
555 cast<ConstantInt>(EndPoints[1])->getValue());
556 if (Range.isFullSet())
Craig Topperc6207612014-04-09 06:08:46 +0000557 return nullptr;
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000558 }
559
560 return MDNode::get(A->getContext(), EndPoints);
561}
562
Devang Patel05a26fb2009-07-29 00:33:07 +0000563//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000564// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000565//
Devang Patel943ddf62010-01-12 18:34:06 +0000566
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000567static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
568 return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000569}
570
Dan Gohman2637cc12010-07-21 23:38:33 +0000571NamedMDNode::NamedMDNode(const Twine &N)
Duncan P. N. Exon Smithc5754a62014-11-05 18:16:03 +0000572 : Name(N.str()), Parent(nullptr),
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000573 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
Devang Patel5c310be2009-08-11 18:01:24 +0000574
Chris Lattner1bc810b2009-12-28 08:07:14 +0000575NamedMDNode::~NamedMDNode() {
576 dropAllReferences();
577 delete &getNMDOps(Operands);
578}
579
Chris Lattner9b493022009-12-31 01:22:29 +0000580unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000581 return (unsigned)getNMDOps(Operands).size();
582}
583
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000584MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000585 assert(i < getNumOperands() && "Invalid Operand number!");
Duncan P. N. Exon Smithb28deb12014-11-05 01:55:06 +0000586 return &*getNMDOps(Operands)[i];
Chris Lattner1bc810b2009-12-28 08:07:14 +0000587}
588
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000589void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000590 assert(!M->isFunctionLocal() &&
591 "NamedMDNode operands must not be function-local!");
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000592 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000593}
594
Devang Patel79238d72009-08-03 06:19:01 +0000595void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000596 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000597}
598
Devang Patel79238d72009-08-03 06:19:01 +0000599void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000600 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000601}
602
Devang Patelfcfee0f2010-01-07 19:39:36 +0000603StringRef NamedMDNode::getName() const {
604 return StringRef(Name);
605}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000606
607//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000608// Instruction Metadata method implementations.
609//
610
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000611void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
612 if (!Node && !hasMetadata())
613 return;
614 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000615}
616
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000617MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000618 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000619}
620
Rafael Espindolaab73c492014-01-28 16:56:46 +0000621void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
622 SmallSet<unsigned, 5> KnownSet;
623 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
624
625 // Drop debug if needed
626 if (KnownSet.erase(LLVMContext::MD_dbg))
627 DbgLoc = DebugLoc();
628
629 if (!hasMetadataHashEntry())
630 return; // Nothing to remove!
631
632 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
633 getContext().pImpl->MetadataStore;
634
635 if (KnownSet.empty()) {
636 // Just drop our entry at the store.
637 MetadataStore.erase(this);
638 setHasMetadataHashEntry(false);
639 return;
640 }
641
642 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
643 unsigned I;
644 unsigned E;
645 // Walk the array and drop any metadata we don't know.
646 for (I = 0, E = Info.size(); I != E;) {
647 if (KnownSet.count(Info[I].first)) {
648 ++I;
649 continue;
650 }
651
652 Info[I] = Info.back();
653 Info.pop_back();
654 --E;
655 }
656 assert(E == Info.size());
657
658 if (E == 0) {
659 // Drop our entry at the store.
660 MetadataStore.erase(this);
661 setHasMetadataHashEntry(false);
662 }
663}
664
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000665/// setMetadata - Set the metadata of of the specified kind to the specified
666/// node. This updates/replaces metadata if already present, or removes it if
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000667/// Node is null.
668void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
669 if (!Node && !hasMetadata())
670 return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000671
Chris Lattnerc263b422010-03-30 23:03:27 +0000672 // Handle 'dbg' as a special case since it is not stored in the hash table.
673 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000674 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000675 return;
676 }
677
Chris Lattnera0566972009-12-29 09:01:33 +0000678 // Handle the case when we're adding/updating metadata on an instruction.
679 if (Node) {
680 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000681 assert(!Info.empty() == hasMetadataHashEntry() &&
682 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000683 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000684 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000685 } else {
686 // Handle replacement of an existing value.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000687 for (auto &P : Info)
688 if (P.first == KindID) {
689 P.second = Node;
Chris Lattnera0566972009-12-29 09:01:33 +0000690 return;
691 }
692 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000693
Chris Lattnera0566972009-12-29 09:01:33 +0000694 // No replacement, just add it to the list.
695 Info.push_back(std::make_pair(KindID, Node));
696 return;
697 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000698
Chris Lattnera0566972009-12-29 09:01:33 +0000699 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000700 assert((hasMetadataHashEntry() ==
Yaron Keren6d3194f2014-06-20 10:26:56 +0000701 (getContext().pImpl->MetadataStore.count(this) > 0)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000702 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000703 if (!hasMetadataHashEntry())
704 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000705 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000706
Chris Lattnera0566972009-12-29 09:01:33 +0000707 // Common case is removing the only entry.
708 if (Info.size() == 1 && Info[0].first == KindID) {
709 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000710 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000711 return;
712 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000713
Chris Lattnerc263b422010-03-30 23:03:27 +0000714 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000715 for (unsigned i = 0, e = Info.size(); i != e; ++i)
716 if (Info[i].first == KindID) {
717 Info[i] = Info.back();
718 Info.pop_back();
719 assert(!Info.empty() && "Removing last entry should be handled above");
720 return;
721 }
722 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000723}
724
Hal Finkelcc39b672014-07-24 12:16:19 +0000725void Instruction::setAAMetadata(const AAMDNodes &N) {
726 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
Hal Finkel94146652014-07-24 14:25:39 +0000727 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
728 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
Hal Finkelcc39b672014-07-24 12:16:19 +0000729}
730
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000731MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000732 // Handle 'dbg' as a special case since it is not stored in the hash table.
733 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000734 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000735
Craig Topperc6207612014-04-09 06:08:46 +0000736 if (!hasMetadataHashEntry()) return nullptr;
Chris Lattnerc263b422010-03-30 23:03:27 +0000737
Chris Lattnera0566972009-12-29 09:01:33 +0000738 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000739 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000740
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000741 for (const auto &I : Info)
742 if (I.first == KindID)
743 return I.second;
Craig Topperc6207612014-04-09 06:08:46 +0000744 return nullptr;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000745}
746
Duncan P. N. Exon Smith4abd1a02014-11-01 00:26:42 +0000747void Instruction::getAllMetadataImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000748 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000749 Result.clear();
750
751 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000752 if (!DbgLoc.isUnknown()) {
753 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
754 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000755 if (!hasMetadataHashEntry()) return;
756 }
757
758 assert(hasMetadataHashEntry() &&
759 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000760 "Shouldn't have called this");
761 const LLVMContextImpl::MDMapTy &Info =
762 getContext().pImpl->MetadataStore.find(this)->second;
763 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000764
Chris Lattnera0566972009-12-29 09:01:33 +0000765 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000766
Chris Lattnera0566972009-12-29 09:01:33 +0000767 // Sort the resulting array so it is stable.
768 if (Result.size() > 1)
769 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000770}
771
Duncan P. N. Exon Smith3d5a02f2014-11-03 18:13:57 +0000772void Instruction::getAllMetadataOtherThanDebugLocImpl(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000773 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000774 Result.clear();
775 assert(hasMetadataHashEntry() &&
776 getContext().pImpl->MetadataStore.count(this) &&
777 "Shouldn't have called this");
778 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000779 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000780 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000781 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000782
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000783 // Sort the resulting array so it is stable.
784 if (Result.size() > 1)
785 array_pod_sort(Result.begin(), Result.end());
786}
787
Dan Gohman48a995f2010-07-20 22:25:04 +0000788/// clearMetadataHashEntries - Clear all hashtable-based metadata from
789/// this instruction.
790void Instruction::clearMetadataHashEntries() {
791 assert(hasMetadataHashEntry() && "Caller should check");
792 getContext().pImpl->MetadataStore.erase(this);
793 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000794}
795