1 //===-- Metadata.cpp - Implement Metadata classes -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Metadata.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "SymbolTableListTraitsImpl.h"
24 #include "llvm/Support/LeakDetector.h"
25 #include "llvm/Support/ValueHandle.h"
28 //===----------------------------------------------------------------------===//
29 // MDString implementation.
32 void MDString::anchor() { }
34 MDString::MDString(LLVMContext &C)
35 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
37 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
38 LLVMContextImpl *pImpl = Context.pImpl;
39 StringMapEntry<Value*> &Entry =
40 pImpl->MDStringCache.GetOrCreateValue(Str);
41 Value *&S = Entry.getValue();
42 if (!S) S = new MDString(Context);
43 S->setValueName(&Entry);
44 return cast<MDString>(S);
47 //===----------------------------------------------------------------------===//
48 // MDNodeOperand implementation.
51 // Use CallbackVH to hold MDNode operands.
53 class MDNodeOperand : public CallbackVH {
55 MDNodeOperand *Cur = this;
57 while (Cur->getValPtrInt() != 1)
60 assert(Cur->getValPtrInt() == 1 &&
61 "Couldn't find the beginning of the operand list!");
62 return reinterpret_cast<MDNode*>(Cur) - 1;
66 MDNodeOperand(Value *V) : CallbackVH(V) {}
69 void set(Value *V) { this->setValPtr(V); }
71 /// setAsFirstOperand - Accessor method to mark the operand as the first in
73 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
75 virtual void deleted();
76 virtual void allUsesReplacedWith(Value *NV);
78 } // end namespace llvm.
81 void MDNodeOperand::deleted() {
82 getParent()->replaceOperand(this, 0);
85 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
86 getParent()->replaceOperand(this, NV);
89 //===----------------------------------------------------------------------===//
90 // MDNode implementation.
93 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
94 /// the end of the MDNode.
95 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
96 // Use <= instead of < to permit a one-past-the-end address.
97 assert(Op <= N->getNumOperands() && "Invalid operand number");
98 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
101 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
102 MDNodeOperand *Op = getOperandPtr(this, i);
103 replaceOperand(Op, Val);
106 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
107 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
108 NumOperands = Vals.size();
111 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
113 // Initialize the operand list, which is co-allocated on the end of the node.
115 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
116 Op != E; ++Op, ++i) {
117 new (Op) MDNodeOperand(Vals[i]);
119 // Mark the first MDNodeOperand as being the first in the list of operands.
121 Op->setAsFirstOperand(1);
126 /// ~MDNode - Destroy MDNode.
128 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
129 "Not being destroyed through destroy()?");
130 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
131 if (isNotUniqued()) {
132 pImpl->NonUniquedMDNodes.erase(this);
134 pImpl->MDNodeSet.RemoveNode(this);
137 // Destroy the operands.
138 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
140 Op->~MDNodeOperand();
143 static const Function *getFunctionForValue(Value *V) {
145 if (Instruction *I = dyn_cast<Instruction>(V)) {
146 BasicBlock *BB = I->getParent();
147 return BB ? BB->getParent() : 0;
149 if (Argument *A = dyn_cast<Argument>(V))
150 return A->getParent();
151 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
152 return BB->getParent();
153 if (MDNode *MD = dyn_cast<MDNode>(V))
154 return MD->getFunction();
159 static const Function *assertLocalFunction(const MDNode *N) {
160 if (!N->isFunctionLocal()) return 0;
162 // FIXME: This does not handle cyclic function local metadata.
163 const Function *F = 0, *NewF = 0;
164 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
165 if (Value *V = N->getOperand(i)) {
166 if (MDNode *MD = dyn_cast<MDNode>(V))
167 NewF = assertLocalFunction(MD);
169 NewF = getFunctionForValue(V);
174 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
180 // getFunction - If this metadata is function-local and recursively has a
181 // function-local operand, return the first such operand's parent function.
182 // Otherwise, return null. getFunction() should not be used for performance-
183 // critical code because it recursively visits all the MDNode's operands.
184 const Function *MDNode::getFunction() const {
186 return assertLocalFunction(this);
188 if (!isFunctionLocal()) return NULL;
189 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
190 if (const Function *F = getFunctionForValue(getOperand(i)))
196 // destroy - Delete this node. Only when there are no uses.
197 void MDNode::destroy() {
198 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
199 // Placement delete, the free the memory.
204 /// isFunctionLocalValue - Return true if this is a value that would require a
205 /// function-local MDNode.
206 static bool isFunctionLocalValue(Value *V) {
207 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
208 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
211 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
212 FunctionLocalness FL, bool Insert) {
213 LLVMContextImpl *pImpl = Context.pImpl;
215 // Add all the operand pointers. Note that we don't have to add the
216 // isFunctionLocal bit because that's implied by the operands.
217 // Note that if the operands are later nulled out, the node will be
218 // removed from the uniquing map.
220 for (unsigned i = 0; i != Vals.size(); ++i)
221 ID.AddPointer(Vals[i]);
224 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
229 bool isFunctionLocal = false;
232 for (unsigned i = 0; i != Vals.size(); ++i) {
235 if (isFunctionLocalValue(V)) {
236 isFunctionLocal = true;
242 isFunctionLocal = false;
245 isFunctionLocal = true;
249 // Coallocate space for the node and Operands together, then placement new.
250 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
251 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
253 // Cache the operand hash.
254 N->Hash = ID.ComputeHash();
256 // InsertPoint will have been set by the FindNodeOrInsertPos call.
257 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
262 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
263 return getMDNode(Context, Vals, FL_Unknown);
266 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
267 ArrayRef<Value*> Vals,
268 bool isFunctionLocal) {
269 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
272 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
273 return getMDNode(Context, Vals, FL_Unknown, false);
276 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
278 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
279 N = new (N) MDNode(Context, Vals, FL_No);
280 N->setValueSubclassData(N->getSubclassDataFromValue() |
282 LeakDetector::addGarbageObject(N);
286 void MDNode::deleteTemporary(MDNode *N) {
287 assert(N->use_empty() && "Temporary MDNode has uses!");
288 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
289 "Deleting a non-temporary uniqued node!");
290 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
291 "Deleting a non-temporary non-uniqued node!");
292 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
293 "Temporary MDNode does not have NotUniquedBit set!");
294 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
295 "Temporary MDNode has DestroyFlag set!");
296 LeakDetector::removeGarbageObject(N);
300 /// getOperand - Return specified operand.
301 Value *MDNode::getOperand(unsigned i) const {
302 return *getOperandPtr(const_cast<MDNode*>(this), i);
305 void MDNode::Profile(FoldingSetNodeID &ID) const {
306 // Add all the operand pointers. Note that we don't have to add the
307 // isFunctionLocal bit because that's implied by the operands.
308 // Note that if the operands are later nulled out, the node will be
309 // removed from the uniquing map.
310 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
311 ID.AddPointer(getOperand(i));
314 void MDNode::setIsNotUniqued() {
315 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
316 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
317 pImpl->NonUniquedMDNodes.insert(this);
320 // Replace value from this node's operand list.
321 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
324 // If is possible that someone did GV->RAUW(inst), replacing a global variable
325 // with an instruction or some other function-local object. If this is a
326 // non-function-local MDNode, it can't point to a function-local object.
327 // Handle this case by implicitly dropping the MDNode reference to null.
328 // Likewise if the MDNode is function-local but for a different function.
329 if (To && isFunctionLocalValue(To)) {
330 if (!isFunctionLocal())
333 const Function *F = getFunction();
334 const Function *FV = getFunctionForValue(To);
335 // Metadata can be function-local without having an associated function.
336 // So only consider functions to have changed if non-null.
337 if (F && FV && F != FV)
345 // Update the operand.
348 // If this node is already not being uniqued (because one of the operands
349 // already went to null), then there is nothing else to do here.
350 if (isNotUniqued()) return;
352 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
354 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
355 // this node to remove it, so we don't care what state the operands are in.
356 pImpl->MDNodeSet.RemoveNode(this);
358 // If we are dropping an argument to null, we choose to not unique the MDNode
359 // anymore. This commonly occurs during destruction, and uniquing these
360 // brings little reuse. Also, this means we don't need to include
361 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
367 // Now that the node is out of the folding set, get ready to reinsert it.
368 // First, check to see if another node with the same operands already exists
369 // in the set. If so, then this node is redundant.
373 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
374 replaceAllUsesWith(N);
379 // Cache the operand hash.
380 Hash = ID.ComputeHash();
381 // InsertPoint will have been set by the FindNodeOrInsertPos call.
382 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
384 // If this MDValue was previously function-local but no longer is, clear
385 // its function-local flag.
386 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
387 bool isStillFunctionLocal = false;
388 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
389 Value *V = getOperand(i);
391 if (isFunctionLocalValue(V)) {
392 isStillFunctionLocal = true;
396 if (!isStillFunctionLocal)
397 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
401 //===----------------------------------------------------------------------===//
402 // NamedMDNode implementation.
405 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
406 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
409 NamedMDNode::NamedMDNode(const Twine &N)
410 : Name(N.str()), Parent(0),
411 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
414 NamedMDNode::~NamedMDNode() {
416 delete &getNMDOps(Operands);
419 /// getNumOperands - Return number of NamedMDNode operands.
420 unsigned NamedMDNode::getNumOperands() const {
421 return (unsigned)getNMDOps(Operands).size();
424 /// getOperand - Return specified operand.
425 MDNode *NamedMDNode::getOperand(unsigned i) const {
426 assert(i < getNumOperands() && "Invalid Operand number!");
427 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
430 /// addOperand - Add metadata Operand.
431 void NamedMDNode::addOperand(MDNode *M) {
432 assert(!M->isFunctionLocal() &&
433 "NamedMDNode operands must not be function-local!");
434 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
437 /// eraseFromParent - Drop all references and remove the node from parent
439 void NamedMDNode::eraseFromParent() {
440 getParent()->eraseNamedMetadata(this);
443 /// dropAllReferences - Remove all uses and clear node vector.
444 void NamedMDNode::dropAllReferences() {
445 getNMDOps(Operands).clear();
448 /// getName - Return a constant reference to this named metadata's name.
449 StringRef NamedMDNode::getName() const {
450 return StringRef(Name);
453 //===----------------------------------------------------------------------===//
454 // Instruction Metadata method implementations.
457 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
458 if (Node == 0 && !hasMetadata()) return;
459 setMetadata(getContext().getMDKindID(Kind), Node);
462 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
463 return getMetadataImpl(getContext().getMDKindID(Kind));
466 /// setMetadata - Set the metadata of of the specified kind to the specified
467 /// node. This updates/replaces metadata if already present, or removes it if
469 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
470 if (Node == 0 && !hasMetadata()) return;
472 // Handle 'dbg' as a special case since it is not stored in the hash table.
473 if (KindID == LLVMContext::MD_dbg) {
474 DbgLoc = DebugLoc::getFromDILocation(Node);
478 // Handle the case when we're adding/updating metadata on an instruction.
480 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
481 assert(!Info.empty() == hasMetadataHashEntry() &&
482 "HasMetadata bit is wonked");
484 setHasMetadataHashEntry(true);
486 // Handle replacement of an existing value.
487 for (unsigned i = 0, e = Info.size(); i != e; ++i)
488 if (Info[i].first == KindID) {
489 Info[i].second = Node;
494 // No replacement, just add it to the list.
495 Info.push_back(std::make_pair(KindID, Node));
499 // Otherwise, we're removing metadata from an instruction.
500 assert((hasMetadataHashEntry() ==
501 getContext().pImpl->MetadataStore.count(this)) &&
502 "HasMetadata bit out of date!");
503 if (!hasMetadataHashEntry())
504 return; // Nothing to remove!
505 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
507 // Common case is removing the only entry.
508 if (Info.size() == 1 && Info[0].first == KindID) {
509 getContext().pImpl->MetadataStore.erase(this);
510 setHasMetadataHashEntry(false);
514 // Handle removal of an existing value.
515 for (unsigned i = 0, e = Info.size(); i != e; ++i)
516 if (Info[i].first == KindID) {
517 Info[i] = Info.back();
519 assert(!Info.empty() && "Removing last entry should be handled above");
522 // Otherwise, removing an entry that doesn't exist on the instruction.
525 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
526 // Handle 'dbg' as a special case since it is not stored in the hash table.
527 if (KindID == LLVMContext::MD_dbg)
528 return DbgLoc.getAsMDNode(getContext());
530 if (!hasMetadataHashEntry()) return 0;
532 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
533 assert(!Info.empty() && "bit out of sync with hash table");
535 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
537 if (I->first == KindID)
542 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
543 MDNode*> > &Result) const {
546 // Handle 'dbg' as a special case since it is not stored in the hash table.
547 if (!DbgLoc.isUnknown()) {
548 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
549 DbgLoc.getAsMDNode(getContext())));
550 if (!hasMetadataHashEntry()) return;
553 assert(hasMetadataHashEntry() &&
554 getContext().pImpl->MetadataStore.count(this) &&
555 "Shouldn't have called this");
556 const LLVMContextImpl::MDMapTy &Info =
557 getContext().pImpl->MetadataStore.find(this)->second;
558 assert(!Info.empty() && "Shouldn't have called this");
560 Result.append(Info.begin(), Info.end());
562 // Sort the resulting array so it is stable.
563 if (Result.size() > 1)
564 array_pod_sort(Result.begin(), Result.end());
568 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
569 MDNode*> > &Result) const {
571 assert(hasMetadataHashEntry() &&
572 getContext().pImpl->MetadataStore.count(this) &&
573 "Shouldn't have called this");
574 const LLVMContextImpl::MDMapTy &Info =
575 getContext().pImpl->MetadataStore.find(this)->second;
576 assert(!Info.empty() && "Shouldn't have called this");
577 Result.append(Info.begin(), Info.end());
579 // Sort the resulting array so it is stable.
580 if (Result.size() > 1)
581 array_pod_sort(Result.begin(), Result.end());
584 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
585 /// this instruction.
586 void Instruction::clearMetadataHashEntries() {
587 assert(hasMetadataHashEntry() && "Caller should check");
588 getContext().pImpl->MetadataStore.erase(this);
589 setHasMetadataHashEntry(false);