blob: c6107f5d8042cde1b8cadf1b8a712f077f8f3536 [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"
Devang Patela4f43fb2009-07-28 21:49:47 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000031// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000032//
Chris Lattner5a409bd2009-12-28 08:30:43 +000033
David Blaikiea379b1812011-12-20 02:50:00 +000034void MDString::anchor() { }
35
Bill Wendlingc4c568b2012-04-10 20:12:16 +000036MDString::MDString(LLVMContext &C)
37 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000038
Devang Pateldcb99d32009-10-22 00:10:15 +000039MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000040 LLVMContextImpl *pImpl = Context.pImpl;
Bill Wendlingc4c568b2012-04-10 20:12:16 +000041 StringMapEntry<Value*> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000042 pImpl->MDStringCache.GetOrCreateValue(Str);
Bill Wendlingc4c568b2012-04-10 20:12:16 +000043 Value *&S = Entry.getValue();
44 if (!S) S = new MDString(Context);
45 S->setValueName(&Entry);
46 return cast<MDString>(S);
Owen Anderson0087fe62009-07-31 21:35:40 +000047}
48
49//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000050// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000051//
52
Chris Lattner9b493022009-12-31 01:22:29 +000053// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000054namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000055class MDNodeOperand : public CallbackVH {
Bill Wendling5c0068f2012-04-08 10:20:49 +000056 MDNode *getParent() {
57 MDNodeOperand *Cur = this;
58
59 while (Cur->getValPtrInt() != 1)
60 --Cur;
61
62 assert(Cur->getValPtrInt() == 1 &&
63 "Couldn't find the beginning of the operand list!");
64 return reinterpret_cast<MDNode*>(Cur) - 1;
65 }
66
Chris Lattner74a6ad62009-12-28 07:41:54 +000067public:
Bill Wendling5c0068f2012-04-08 10:20:49 +000068 MDNodeOperand(Value *V) : CallbackVH(V) {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000069 virtual ~MDNodeOperand();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000070
Bill Wendling0156f442012-04-26 00:38:42 +000071 void set(Value *V) {
72 unsigned IsFirst = this->getValPtrInt();
73 this->setValPtr(V);
74 this->setAsFirstOperand(IsFirst);
75 }
Bill Wendling5c0068f2012-04-08 10:20:49 +000076
77 /// setAsFirstOperand - Accessor method to mark the operand as the first in
78 /// the list.
79 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000080
Craig Topperf398d7c2014-03-05 06:35:38 +000081 void deleted() override;
82 void allUsesReplacedWith(Value *NV) override;
Chris Lattner74a6ad62009-12-28 07:41:54 +000083};
84} // end namespace llvm.
85
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000086// Provide out-of-line definition to prevent weak vtable.
87MDNodeOperand::~MDNodeOperand() {}
Chris Lattner74a6ad62009-12-28 07:41:54 +000088
Chris Lattner9b493022009-12-31 01:22:29 +000089void MDNodeOperand::deleted() {
Bill Wendling5c0068f2012-04-08 10:20:49 +000090 getParent()->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000091}
92
Chris Lattner9b493022009-12-31 01:22:29 +000093void MDNodeOperand::allUsesReplacedWith(Value *NV) {
Bill Wendling5c0068f2012-04-08 10:20:49 +000094 getParent()->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000095}
96
Chris Lattner74a6ad62009-12-28 07:41:54 +000097//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000098// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000099//
Chris Lattner74a6ad62009-12-28 07:41:54 +0000100
Chris Lattner9b493022009-12-31 01:22:29 +0000101/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +0000102/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +0000103static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +0000104 // Use <= instead of < to permit a one-past-the-end address.
105 assert(Op <= N->getNumOperands() && "Invalid operand number");
Bill Wendling0156f442012-04-26 00:38:42 +0000106 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +0000107}
108
Eric Christopher24e51b72012-02-15 09:09:29 +0000109void MDNode::replaceOperandWith(unsigned i, Value *Val) {
110 MDNodeOperand *Op = getOperandPtr(this, i);
111 replaceOperand(Op, Val);
112}
113
Jay Foad5514afe2011-04-21 19:59:31 +0000114MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +0000115: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foad5514afe2011-04-21 19:59:31 +0000116 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000117
Victor Hernandez0471abd2009-12-18 20:09:14 +0000118 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000119 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000120
121 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +0000122 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000123 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Bill Wendling5c0068f2012-04-08 10:20:49 +0000124 Op != E; ++Op, ++i) {
125 new (Op) MDNodeOperand(Vals[i]);
126
127 // Mark the first MDNodeOperand as being the first in the list of operands.
128 if (i == 0)
129 Op->setAsFirstOperand(1);
130 }
Devang Patela4f43fb2009-07-28 21:49:47 +0000131}
132
Chris Lattner8cb6c342009-12-31 01:05:46 +0000133/// ~MDNode - Destroy MDNode.
134MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000135 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000136 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000137 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
138 if (isNotUniqued()) {
139 pImpl->NonUniquedMDNodes.erase(this);
140 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000141 pImpl->MDNodeSet.RemoveNode(this);
142 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000143
Chris Lattner8cb6c342009-12-31 01:05:46 +0000144 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000145 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000146 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000147 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000148}
149
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000150static const Function *getFunctionForValue(Value *V) {
151 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000152 if (Instruction *I = dyn_cast<Instruction>(V)) {
153 BasicBlock *BB = I->getParent();
154 return BB ? BB->getParent() : 0;
155 }
Chris Lattner5522f052010-01-21 21:01:47 +0000156 if (Argument *A = dyn_cast<Argument>(V))
157 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000158 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
159 return BB->getParent();
160 if (MDNode *MD = dyn_cast<MDNode>(V))
161 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000162 return NULL;
163}
164
Victor Hernandez8296da72010-01-14 20:12:34 +0000165#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000166static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000167 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000168
Devang Patelb36df172010-07-06 21:05:17 +0000169 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000170 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000171 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000172 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000173 if (MDNode *MD = dyn_cast<MDNode>(V))
174 NewF = assertLocalFunction(MD);
175 else
176 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000177 }
Chris Lattner5522f052010-01-21 21:01:47 +0000178 if (F == 0)
179 F = NewF;
180 else
181 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000182 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000183 return F;
184}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000185#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000186
187// getFunction - If this metadata is function-local and recursively has a
188// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000189// Otherwise, return null. getFunction() should not be used for performance-
190// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000191const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000192#ifndef NDEBUG
193 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000194#else
Victor Hernandez8c85e252010-01-14 01:45:14 +0000195 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000196 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
197 if (const Function *F = getFunctionForValue(getOperand(i)))
198 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000199 return NULL;
David Blaikie46a9f012012-01-20 21:51:11 +0000200#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000201}
202
Chris Lattner8cb6c342009-12-31 01:05:46 +0000203// destroy - Delete this node. Only when there are no uses.
204void MDNode::destroy() {
205 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
Eric Christopher97f6ea92012-08-14 01:09:10 +0000206 // Placement delete, then free the memory.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000207 this->~MDNode();
208 free(this);
209}
210
Chris Lattner450e29c2010-04-28 20:16:12 +0000211/// isFunctionLocalValue - Return true if this is a value that would require a
212/// function-local MDNode.
213static bool isFunctionLocalValue(Value *V) {
214 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
215 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
216}
217
Jay Foad5514afe2011-04-21 19:59:31 +0000218MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
219 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000220 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000221
222 // Add all the operand pointers. Note that we don't have to add the
223 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000224 // Note that if the operands are later nulled out, the node will be
225 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000226 FoldingSetNodeID ID;
Jay Foad5514afe2011-04-21 19:59:31 +0000227 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman01579b22010-08-24 23:21:12 +0000228 ID.AddPointer(Vals[i]);
229
230 void *InsertPoint;
Duncan Sands26a80f32012-03-31 08:20:11 +0000231 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
232
233 if (N || !Insert)
Dan Gohman01579b22010-08-24 23:21:12 +0000234 return N;
Duncan Sands26a80f32012-03-31 08:20:11 +0000235
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000236 bool isFunctionLocal = false;
237 switch (FL) {
238 case FL_Unknown:
Jay Foad5514afe2011-04-21 19:59:31 +0000239 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000240 Value *V = Vals[i];
241 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000242 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000243 isFunctionLocal = true;
244 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000245 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000246 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000247 break;
248 case FL_No:
249 isFunctionLocal = false;
250 break;
251 case FL_Yes:
252 isFunctionLocal = true;
253 break;
Devang Patelf7188322009-09-03 01:39:20 +0000254 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000255
256 // Coallocate space for the node and Operands together, then placement new.
Bill Wendling0156f442012-04-26 00:38:42 +0000257 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foad5514afe2011-04-21 19:59:31 +0000258 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000259
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000260 // Cache the operand hash.
261 N->Hash = ID.ComputeHash();
262
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000263 // InsertPoint will have been set by the FindNodeOrInsertPos call.
264 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
265
Devang Patelf7188322009-09-03 01:39:20 +0000266 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000267}
268
Devang Patel213264c2011-03-04 01:20:33 +0000269MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000270 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000271}
272
Jay Foad5514afe2011-04-21 19:59:31 +0000273MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
274 ArrayRef<Value*> Vals,
275 bool isFunctionLocal) {
276 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000277}
278
Jay Foad5514afe2011-04-21 19:59:31 +0000279MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
280 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000281}
282
Jay Foad5514afe2011-04-21 19:59:31 +0000283MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
284 MDNode *N =
Bill Wendling0156f442012-04-26 00:38:42 +0000285 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foad5514afe2011-04-21 19:59:31 +0000286 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000287 N->setValueSubclassData(N->getSubclassDataFromValue() |
288 NotUniquedBit);
289 LeakDetector::addGarbageObject(N);
290 return N;
291}
292
293void MDNode::deleteTemporary(MDNode *N) {
294 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000295 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000296 "Deleting a non-temporary uniqued node!");
297 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
298 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000299 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
300 "Temporary MDNode does not have NotUniquedBit set!");
301 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000302 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000303 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000304 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000305}
306
Chris Lattner9b493022009-12-31 01:22:29 +0000307/// getOperand - Return specified operand.
308Value *MDNode::getOperand(unsigned i) const {
David Blaikie58462392013-03-08 21:08:23 +0000309 assert(i < getNumOperands() && "Invalid operand number");
Chris Lattner8cb6c342009-12-31 01:05:46 +0000310 return *getOperandPtr(const_cast<MDNode*>(this), i);
311}
312
Chris Lattnerf543eff2009-12-28 09:12:35 +0000313void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000314 // Add all the operand pointers. Note that we don't have to add the
315 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000316 // Note that if the operands are later nulled out, the node will be
317 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000318 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
319 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000320}
Devang Patel5c310be2009-08-11 18:01:24 +0000321
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000322void MDNode::setIsNotUniqued() {
323 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
324 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
325 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000326}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000327
Chris Lattner9b493022009-12-31 01:22:29 +0000328// Replace value from this node's operand list.
329void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000330 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000331
Chris Lattner450e29c2010-04-28 20:16:12 +0000332 // If is possible that someone did GV->RAUW(inst), replacing a global variable
333 // with an instruction or some other function-local object. If this is a
334 // non-function-local MDNode, it can't point to a function-local object.
335 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000336 // Likewise if the MDNode is function-local but for a different function.
337 if (To && isFunctionLocalValue(To)) {
338 if (!isFunctionLocal())
339 To = 0;
340 else {
341 const Function *F = getFunction();
342 const Function *FV = getFunctionForValue(To);
343 // Metadata can be function-local without having an associated function.
344 // So only consider functions to have changed if non-null.
345 if (F && FV && F != FV)
346 To = 0;
347 }
348 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000349
Chris Lattner95c445d2009-12-28 09:32:10 +0000350 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000351 return;
Devang Patelf7188322009-09-03 01:39:20 +0000352
Chris Lattner30ae06b2009-12-30 21:42:11 +0000353 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000354 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000355
356 // If this node is already not being uniqued (because one of the operands
357 // already went to null), then there is nothing else to do here.
358 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000359
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000360 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
361
362 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
363 // this node to remove it, so we don't care what state the operands are in.
364 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000365
Chris Lattner30ae06b2009-12-30 21:42:11 +0000366 // If we are dropping an argument to null, we choose to not unique the MDNode
367 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000368 // brings little reuse. Also, this means we don't need to include
369 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000370 if (To == 0) {
371 setIsNotUniqued();
372 return;
373 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000374
Chris Lattner30ae06b2009-12-30 21:42:11 +0000375 // Now that the node is out of the folding set, get ready to reinsert it.
376 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000377 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000378 FoldingSetNodeID ID;
379 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000380 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000381 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000382 replaceAllUsesWith(N);
383 destroy();
384 return;
Devang Patelf7188322009-09-03 01:39:20 +0000385 }
386
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000387 // Cache the operand hash.
388 Hash = ID.ComputeHash();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000389 // InsertPoint will have been set by the FindNodeOrInsertPos call.
390 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000391
392 // If this MDValue was previously function-local but no longer is, clear
393 // its function-local flag.
394 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
395 bool isStillFunctionLocal = false;
396 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
397 Value *V = getOperand(i);
398 if (!V) continue;
399 if (isFunctionLocalValue(V)) {
400 isStillFunctionLocal = true;
401 break;
402 }
403 }
404 if (!isStillFunctionLocal)
405 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
406 }
Devang Patelf7188322009-09-03 01:39:20 +0000407}
408
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000409MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
410 if (!A || !B)
411 return NULL;
412
413 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
414 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
415 if (AVal.compare(BVal) == APFloat::cmpLessThan)
416 return A;
417 return B;
418}
419
420static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
421 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
422}
423
424static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
425 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
426}
427
Craig Topperb94011f2013-07-14 04:42:23 +0000428static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000429 ConstantInt *High) {
430 ConstantRange NewRange(Low->getValue(), High->getValue());
431 unsigned Size = EndPoints.size();
432 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
433 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
434 ConstantRange LastRange(LB, LE);
435 if (canBeMerged(NewRange, LastRange)) {
436 ConstantRange Union = LastRange.unionWith(NewRange);
437 Type *Ty = High->getType();
438 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
439 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
440 return true;
441 }
442 return false;
443}
444
Craig Topperb94011f2013-07-14 04:42:23 +0000445static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
Hal Finkel16ddd4b2012-06-16 20:33:37 +0000446 ConstantInt *High) {
447 if (!EndPoints.empty())
448 if (tryMergeRange(EndPoints, Low, High))
449 return;
450
451 EndPoints.push_back(Low);
452 EndPoints.push_back(High);
453}
454
455MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
456 // Given two ranges, we want to compute the union of the ranges. This
457 // is slightly complitade by having to combine the intervals and merge
458 // the ones that overlap.
459
460 if (!A || !B)
461 return NULL;
462
463 if (A == B)
464 return A;
465
466 // First, walk both lists in older of the lower boundary of each interval.
467 // At each step, try to merge the new interval to the last one we adedd.
468 SmallVector<Value*, 4> EndPoints;
469 int AI = 0;
470 int BI = 0;
471 int AN = A->getNumOperands() / 2;
472 int BN = B->getNumOperands() / 2;
473 while (AI < AN && BI < BN) {
474 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
475 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
476
477 if (ALow->getValue().slt(BLow->getValue())) {
478 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
479 ++AI;
480 } else {
481 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
482 ++BI;
483 }
484 }
485 while (AI < AN) {
486 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
487 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
488 ++AI;
489 }
490 while (BI < BN) {
491 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
492 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
493 ++BI;
494 }
495
496 // If we have more than 2 ranges (4 endpoints) we have to try to merge
497 // the last and first ones.
498 unsigned Size = EndPoints.size();
499 if (Size > 4) {
500 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
501 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
502 if (tryMergeRange(EndPoints, FB, FE)) {
503 for (unsigned i = 0; i < Size - 2; ++i) {
504 EndPoints[i] = EndPoints[i + 2];
505 }
506 EndPoints.resize(Size - 2);
507 }
508 }
509
510 // If in the end we have a single range, it is possible that it is now the
511 // full range. Just drop the metadata in that case.
512 if (EndPoints.size() == 2) {
513 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
514 cast<ConstantInt>(EndPoints[1])->getValue());
515 if (Range.isFullSet())
516 return NULL;
517 }
518
519 return MDNode::get(A->getContext(), EndPoints);
520}
521
Devang Patel05a26fb2009-07-29 00:33:07 +0000522//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000523// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000524//
Devang Patel943ddf62010-01-12 18:34:06 +0000525
Dan Gohman846b9e12010-07-21 18:01:42 +0000526static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
527 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000528}
529
Dan Gohman2637cc12010-07-21 23:38:33 +0000530NamedMDNode::NamedMDNode(const Twine &N)
531 : Name(N.str()), Parent(0),
532 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000533}
534
Chris Lattner1bc810b2009-12-28 08:07:14 +0000535NamedMDNode::~NamedMDNode() {
536 dropAllReferences();
537 delete &getNMDOps(Operands);
538}
539
Chris Lattner9b493022009-12-31 01:22:29 +0000540/// getNumOperands - Return number of NamedMDNode operands.
541unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000542 return (unsigned)getNMDOps(Operands).size();
543}
544
Chris Lattner9b493022009-12-31 01:22:29 +0000545/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000546MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000547 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000548 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000549}
550
Chris Lattner9b493022009-12-31 01:22:29 +0000551/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000552void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000553 assert(!M->isFunctionLocal() &&
554 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000555 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000556}
557
Devang Patel79238d72009-08-03 06:19:01 +0000558/// eraseFromParent - Drop all references and remove the node from parent
559/// module.
560void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000561 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000562}
563
564/// dropAllReferences - Remove all uses and clear node vector.
565void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000566 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000567}
568
Devang Patelfcfee0f2010-01-07 19:39:36 +0000569/// getName - Return a constant reference to this named metadata's name.
570StringRef NamedMDNode::getName() const {
571 return StringRef(Name);
572}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000573
574//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000575// Instruction Metadata method implementations.
576//
577
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000578void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000579 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000580 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000581}
582
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000583MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000584 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000585}
586
Rafael Espindolaab73c492014-01-28 16:56:46 +0000587void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
588 SmallSet<unsigned, 5> KnownSet;
589 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
590
591 // Drop debug if needed
592 if (KnownSet.erase(LLVMContext::MD_dbg))
593 DbgLoc = DebugLoc();
594
595 if (!hasMetadataHashEntry())
596 return; // Nothing to remove!
597
598 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
599 getContext().pImpl->MetadataStore;
600
601 if (KnownSet.empty()) {
602 // Just drop our entry at the store.
603 MetadataStore.erase(this);
604 setHasMetadataHashEntry(false);
605 return;
606 }
607
608 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
609 unsigned I;
610 unsigned E;
611 // Walk the array and drop any metadata we don't know.
612 for (I = 0, E = Info.size(); I != E;) {
613 if (KnownSet.count(Info[I].first)) {
614 ++I;
615 continue;
616 }
617
618 Info[I] = Info.back();
619 Info.pop_back();
620 --E;
621 }
622 assert(E == Info.size());
623
624 if (E == 0) {
625 // Drop our entry at the store.
626 MetadataStore.erase(this);
627 setHasMetadataHashEntry(false);
628 }
629}
630
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000631/// setMetadata - Set the metadata of of the specified kind to the specified
632/// node. This updates/replaces metadata if already present, or removes it if
633/// Node is null.
634void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
635 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000636
Chris Lattnerc263b422010-03-30 23:03:27 +0000637 // Handle 'dbg' as a special case since it is not stored in the hash table.
638 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000639 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000640 return;
641 }
642
Chris Lattnera0566972009-12-29 09:01:33 +0000643 // Handle the case when we're adding/updating metadata on an instruction.
644 if (Node) {
645 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000646 assert(!Info.empty() == hasMetadataHashEntry() &&
647 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000648 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000649 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000650 } else {
651 // Handle replacement of an existing value.
652 for (unsigned i = 0, e = Info.size(); i != e; ++i)
653 if (Info[i].first == KindID) {
654 Info[i].second = Node;
655 return;
656 }
657 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000658
Chris Lattnera0566972009-12-29 09:01:33 +0000659 // No replacement, just add it to the list.
660 Info.push_back(std::make_pair(KindID, Node));
661 return;
662 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000663
Chris Lattnera0566972009-12-29 09:01:33 +0000664 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000665 assert((hasMetadataHashEntry() ==
666 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000667 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000668 if (!hasMetadataHashEntry())
669 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000670 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000671
Chris Lattnera0566972009-12-29 09:01:33 +0000672 // Common case is removing the only entry.
673 if (Info.size() == 1 && Info[0].first == KindID) {
674 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000675 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000676 return;
677 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000678
Chris Lattnerc263b422010-03-30 23:03:27 +0000679 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000680 for (unsigned i = 0, e = Info.size(); i != e; ++i)
681 if (Info[i].first == KindID) {
682 Info[i] = Info.back();
683 Info.pop_back();
684 assert(!Info.empty() && "Removing last entry should be handled above");
685 return;
686 }
687 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000688}
689
690MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000691 // Handle 'dbg' as a special case since it is not stored in the hash table.
692 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000693 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000694
695 if (!hasMetadataHashEntry()) return 0;
696
Chris Lattnera0566972009-12-29 09:01:33 +0000697 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000698 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000699
Chris Lattnera0566972009-12-29 09:01:33 +0000700 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
701 I != E; ++I)
702 if (I->first == KindID)
703 return I->second;
704 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000705}
706
707void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000708 MDNode*> > &Result) const {
709 Result.clear();
710
711 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000712 if (!DbgLoc.isUnknown()) {
713 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
714 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000715 if (!hasMetadataHashEntry()) return;
716 }
717
718 assert(hasMetadataHashEntry() &&
719 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000720 "Shouldn't have called this");
721 const LLVMContextImpl::MDMapTy &Info =
722 getContext().pImpl->MetadataStore.find(this)->second;
723 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000724
Chris Lattnera0566972009-12-29 09:01:33 +0000725 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000726
Chris Lattnera0566972009-12-29 09:01:33 +0000727 // Sort the resulting array so it is stable.
728 if (Result.size() > 1)
729 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000730}
731
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000732void Instruction::
733getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
734 MDNode*> > &Result) const {
735 Result.clear();
736 assert(hasMetadataHashEntry() &&
737 getContext().pImpl->MetadataStore.count(this) &&
738 "Shouldn't have called this");
739 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000740 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000741 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000742 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000743
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000744 // Sort the resulting array so it is stable.
745 if (Result.size() > 1)
746 array_pod_sort(Result.begin(), Result.end());
747}
748
Dan Gohman48a995f2010-07-20 22:25:04 +0000749/// clearMetadataHashEntries - Clear all hashtable-based metadata from
750/// this instruction.
751void Instruction::clearMetadataHashEntries() {
752 assert(hasMetadataHashEntry() && "Caller should check");
753 getContext().pImpl->MetadataStore.erase(this);
754 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000755}
756