2009-07-29 05:49:47 +08:00
|
|
|
//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Metadata classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Metadata.h"
|
2009-12-28 16:24:16 +08:00
|
|
|
#include "LLVMContextImpl.h"
|
2009-08-01 05:35:40 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2009-07-29 08:33:07 +08:00
|
|
|
#include "llvm/Module.h"
|
2009-09-17 02:09:00 +08:00
|
|
|
#include "llvm/Instruction.h"
|
2009-10-23 03:36:54 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2010-01-13 02:57:56 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-07-30 01:16:17 +08:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
2009-12-28 16:24:16 +08:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-29 05:49:47 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-01 05:35:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 15:10:59 +08:00
|
|
|
// MDString implementation.
|
2009-08-01 05:35:40 +08:00
|
|
|
//
|
2009-12-28 16:30:43 +08:00
|
|
|
|
|
|
|
MDString::MDString(LLVMContext &C, StringRef S)
|
2010-01-23 06:52:10 +08:00
|
|
|
: Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
|
2009-12-28 16:30:43 +08:00
|
|
|
|
2009-10-22 08:10:15 +08:00
|
|
|
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
|
2009-08-01 05:35:40 +08:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
2010-01-11 02:48:49 +08:00
|
|
|
StringMapEntry<MDString *> &Entry =
|
2009-08-01 05:35:40 +08:00
|
|
|
pImpl->MDStringCache.GetOrCreateValue(Str);
|
|
|
|
MDString *&S = Entry.getValue();
|
2009-11-27 06:54:26 +08:00
|
|
|
if (!S) S = new MDString(Context, Entry.getKey());
|
|
|
|
return S;
|
2009-08-01 05:35:40 +08:00
|
|
|
}
|
|
|
|
|
2009-11-12 08:50:58 +08:00
|
|
|
MDString *MDString::get(LLVMContext &Context, const char *Str) {
|
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
2010-01-11 02:48:49 +08:00
|
|
|
StringMapEntry<MDString *> &Entry =
|
2009-11-12 08:50:58 +08:00
|
|
|
pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
|
|
|
|
MDString *&S = Entry.getValue();
|
2009-11-28 03:57:53 +08:00
|
|
|
if (!S) S = new MDString(Context, Entry.getKey());
|
2009-11-27 06:54:26 +08:00
|
|
|
return S;
|
2009-11-12 08:50:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-28 15:41:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-31 09:22:29 +08:00
|
|
|
// MDNodeOperand implementation.
|
2009-12-28 15:41:54 +08:00
|
|
|
//
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
// Use CallbackVH to hold MDNode operands.
|
2009-12-28 15:41:54 +08:00
|
|
|
namespace llvm {
|
2009-12-31 09:22:29 +08:00
|
|
|
class MDNodeOperand : public CallbackVH {
|
2009-12-28 15:41:54 +08:00
|
|
|
MDNode *Parent;
|
|
|
|
public:
|
2009-12-31 09:22:29 +08:00
|
|
|
MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
|
|
|
|
~MDNodeOperand() {}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-31 09:05:46 +08:00
|
|
|
void set(Value *V) {
|
2009-12-28 17:10:16 +08:00
|
|
|
setValPtr(V);
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-28 15:41:54 +08:00
|
|
|
virtual void deleted();
|
|
|
|
virtual void allUsesReplacedWith(Value *NV);
|
|
|
|
};
|
|
|
|
} // end namespace llvm.
|
|
|
|
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
void MDNodeOperand::deleted() {
|
|
|
|
Parent->replaceOperand(this, 0);
|
2009-12-28 15:41:54 +08:00
|
|
|
}
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
void MDNodeOperand::allUsesReplacedWith(Value *NV) {
|
|
|
|
Parent->replaceOperand(this, NV);
|
2009-12-28 15:41:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-29 05:49:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 15:10:59 +08:00
|
|
|
// MDNode implementation.
|
2009-07-29 05:49:47 +08:00
|
|
|
//
|
2009-12-28 15:41:54 +08:00
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
|
2009-12-31 09:05:46 +08:00
|
|
|
/// the end of the MDNode.
|
2009-12-31 09:22:29 +08:00
|
|
|
static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
|
|
|
|
assert(Op < N->getNumOperands() && "Invalid operand number");
|
|
|
|
return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
|
2009-12-31 09:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
|
|
|
|
bool isFunctionLocal)
|
2010-01-23 06:52:10 +08:00
|
|
|
: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
|
2009-12-31 09:05:46 +08:00
|
|
|
NumOperands = NumVals;
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-31 09:05:46 +08:00
|
|
|
if (isFunctionLocal)
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
|
|
|
|
|
|
|
|
// Initialize the operand list, which is co-allocated on the end of the node.
|
2009-12-31 09:22:29 +08:00
|
|
|
for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
|
2009-12-31 09:05:46 +08:00
|
|
|
Op != E; ++Op, ++Vals)
|
2009-12-31 09:22:29 +08:00
|
|
|
new (Op) MDNodeOperand(*Vals, this);
|
2009-12-31 09:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-28 17:12:35 +08:00
|
|
|
/// ~MDNode - Destroy MDNode.
|
|
|
|
MDNode::~MDNode() {
|
2010-01-11 02:48:49 +08:00
|
|
|
assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
|
2009-12-31 09:05:46 +08:00
|
|
|
"Not being destroyed through destroy()?");
|
2009-12-31 05:42:11 +08:00
|
|
|
if (!isNotUniqued()) {
|
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-31 09:05:46 +08:00
|
|
|
// Destroy the operands.
|
2009-12-31 09:22:29 +08:00
|
|
|
for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
|
2009-12-31 09:05:46 +08:00
|
|
|
Op != E; ++Op)
|
2009-12-31 09:22:29 +08:00
|
|
|
Op->~MDNodeOperand();
|
2009-12-28 17:12:35 +08:00
|
|
|
}
|
|
|
|
|
2010-01-20 12:45:57 +08:00
|
|
|
static const Function *getFunctionForValue(Value *V) {
|
2010-01-20 14:22:33 +08:00
|
|
|
assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
|
2010-01-20 12:45:57 +08:00
|
|
|
if (!V) return NULL;
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
return I->getParent()->getParent();
|
2010-01-22 05:01:47 +08:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
|
|
return BB->getParent();
|
|
|
|
if (Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->getParent();
|
2010-01-20 12:45:57 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-15 04:12:34 +08:00
|
|
|
#ifndef NDEBUG
|
2010-01-20 12:45:57 +08:00
|
|
|
static const Function *assertLocalFunction(const MDNode *N) {
|
2010-01-22 05:01:47 +08:00
|
|
|
if (!N->isFunctionLocal()) return 0;
|
2010-01-15 04:12:34 +08:00
|
|
|
|
2010-01-22 05:01:47 +08:00
|
|
|
const Function *F = 0, *NewF = 0;
|
2010-01-14 09:45:14 +08:00
|
|
|
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
|
2010-01-20 12:45:57 +08:00
|
|
|
if (Value *V = N->getOperand(i)) {
|
2010-01-22 05:01:47 +08:00
|
|
|
if (MDNode *MD = dyn_cast<MDNode>(V))
|
|
|
|
NewF = assertLocalFunction(MD);
|
|
|
|
else
|
|
|
|
NewF = getFunctionForValue(V);
|
2010-01-19 04:36:54 +08:00
|
|
|
}
|
2010-01-22 05:01:47 +08:00
|
|
|
if (F == 0)
|
|
|
|
F = NewF;
|
|
|
|
else
|
|
|
|
assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
|
2010-01-14 09:45:14 +08:00
|
|
|
}
|
|
|
|
return F;
|
|
|
|
}
|
2010-01-19 04:36:54 +08:00
|
|
|
#endif
|
2010-01-14 09:45:14 +08:00
|
|
|
|
|
|
|
// getFunction - If this metadata is function-local and recursively has a
|
|
|
|
// function-local operand, return the first such operand's parent function.
|
2010-01-19 06:55:08 +08:00
|
|
|
// Otherwise, return null. getFunction() should not be used for performance-
|
|
|
|
// critical code because it recursively visits all the MDNode's operands.
|
2010-01-20 12:45:57 +08:00
|
|
|
const Function *MDNode::getFunction() const {
|
2010-01-19 04:36:54 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
return assertLocalFunction(this);
|
|
|
|
#endif
|
2010-01-14 09:45:14 +08:00
|
|
|
if (!isFunctionLocal()) return NULL;
|
2010-01-19 04:36:54 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
|
2010-01-20 12:45:57 +08:00
|
|
|
if (Value *V = getOperand(i)) {
|
2010-01-20 14:01:02 +08:00
|
|
|
if (MDNode *MD = dyn_cast<MDNode>(V)) {
|
2010-01-22 05:01:47 +08:00
|
|
|
if (const Function *F = MD->getFunction())
|
|
|
|
return F;
|
2010-01-20 14:22:33 +08:00
|
|
|
} else {
|
|
|
|
return getFunctionForValue(V);
|
2010-01-20 14:01:02 +08:00
|
|
|
}
|
2010-01-20 12:45:57 +08:00
|
|
|
}
|
2010-01-19 04:36:54 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
2010-01-14 09:45:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-31 09:05:46 +08:00
|
|
|
// destroy - Delete this node. Only when there are no uses.
|
|
|
|
void MDNode::destroy() {
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
|
|
|
|
// Placement delete, the free the memory.
|
|
|
|
this->~MDNode();
|
|
|
|
free(this);
|
2009-07-29 05:49:47 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 15:14:18 +08:00
|
|
|
MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
|
2010-01-26 10:36:35 +08:00
|
|
|
unsigned NumVals, FunctionLocalness FL,
|
|
|
|
bool Insert) {
|
2009-09-03 09:39:20 +08:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
|
|
|
ID.AddPointer(Vals[i]);
|
|
|
|
|
|
|
|
void *InsertPoint;
|
2010-01-26 10:36:35 +08:00
|
|
|
MDNode *N = NULL;
|
|
|
|
|
|
|
|
if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
|
|
|
|
return N;
|
|
|
|
|
|
|
|
if (!Insert)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bool isFunctionLocal = false;
|
|
|
|
switch (FL) {
|
|
|
|
case FL_Unknown:
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i) {
|
|
|
|
Value *V = Vals[i];
|
|
|
|
if (!V) continue;
|
|
|
|
if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
|
|
|
|
(isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
|
|
|
|
isFunctionLocal = true;
|
|
|
|
break;
|
2010-01-10 15:14:18 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-26 10:36:35 +08:00
|
|
|
break;
|
|
|
|
case FL_No:
|
|
|
|
isFunctionLocal = false;
|
|
|
|
break;
|
|
|
|
case FL_Yes:
|
|
|
|
isFunctionLocal = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-10 15:14:18 +08:00
|
|
|
|
2010-01-26 10:36:35 +08:00
|
|
|
// Coallocate space for the node and Operands together, then placement new.
|
|
|
|
void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
|
|
|
|
N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
|
|
|
|
|
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
|
|
|
pImpl->MDNodeSet.InsertNode(N, InsertPoint);
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-09-03 09:39:20 +08:00
|
|
|
return N;
|
2009-08-01 05:35:40 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 15:14:18 +08:00
|
|
|
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, FL_Unknown);
|
|
|
|
}
|
|
|
|
|
2010-01-26 10:36:35 +08:00
|
|
|
MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
|
2010-01-10 15:14:18 +08:00
|
|
|
unsigned NumVals, bool isFunctionLocal) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
|
|
|
|
}
|
|
|
|
|
2010-01-26 10:36:35 +08:00
|
|
|
MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
|
|
|
|
unsigned NumVals) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
|
|
|
|
}
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
/// getOperand - Return specified operand.
|
|
|
|
Value *MDNode::getOperand(unsigned i) const {
|
2009-12-31 09:05:46 +08:00
|
|
|
return *getOperandPtr(const_cast<MDNode*>(this), i);
|
2009-12-28 15:41:54 +08:00
|
|
|
}
|
|
|
|
|
2009-12-31 09:05:46 +08:00
|
|
|
void MDNode::Profile(FoldingSetNodeID &ID) const {
|
2009-12-31 09:22:29 +08:00
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
|
|
|
|
ID.AddPointer(getOperand(i));
|
2009-12-31 09:05:46 +08:00
|
|
|
}
|
2009-12-28 15:41:54 +08:00
|
|
|
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
// Replace value from this node's operand list.
|
|
|
|
void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
|
2009-12-28 17:32:10 +08:00
|
|
|
Value *From = *Op;
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-28 17:32:10 +08:00
|
|
|
if (From == To)
|
2009-09-03 09:39:20 +08:00
|
|
|
return;
|
|
|
|
|
2009-12-31 05:42:11 +08:00
|
|
|
// Update the operand.
|
2009-12-31 09:05:46 +08:00
|
|
|
Op->set(To);
|
2009-12-31 05:42:11 +08:00
|
|
|
|
|
|
|
// If this node is already not being uniqued (because one of the operands
|
|
|
|
// already went to null), then there is nothing else to do here.
|
|
|
|
if (isNotUniqued()) return;
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-28 17:24:53 +08:00
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
|
|
|
|
// Remove "this" from the context map. FoldingSet doesn't have to reprofile
|
|
|
|
// this node to remove it, so we don't care what state the operands are in.
|
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
2009-12-28 17:32:10 +08:00
|
|
|
|
2009-12-31 05:42:11 +08:00
|
|
|
// If we are dropping an argument to null, we choose to not unique the MDNode
|
|
|
|
// anymore. This commonly occurs during destruction, and uniquing these
|
|
|
|
// brings little reuse.
|
|
|
|
if (To == 0) {
|
|
|
|
setIsNotUniqued();
|
|
|
|
return;
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-31 05:42:11 +08:00
|
|
|
// Now that the node is out of the folding set, get ready to reinsert it.
|
|
|
|
// First, check to see if another node with the same operands already exists
|
|
|
|
// in the set. If it doesn't exist, this returns the position to insert it.
|
2009-09-03 09:39:20 +08:00
|
|
|
FoldingSetNodeID ID;
|
|
|
|
Profile(ID);
|
|
|
|
void *InsertPoint;
|
|
|
|
MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->replaceAllUsesWith(this);
|
2009-12-31 09:05:46 +08:00
|
|
|
N->destroy();
|
2009-12-28 17:24:53 +08:00
|
|
|
N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
assert(N == 0 && "shouldn't be in the map now!"); (void)N;
|
2009-09-03 09:39:20 +08:00
|
|
|
}
|
|
|
|
|
2009-12-28 17:24:53 +08:00
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
|
|
|
pImpl->MDNodeSet.InsertNode(this, InsertPoint);
|
2009-09-03 09:39:20 +08:00
|
|
|
}
|
|
|
|
|
2009-07-29 08:33:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 15:10:59 +08:00
|
|
|
// NamedMDNode implementation.
|
2009-07-29 08:33:07 +08:00
|
|
|
//
|
2010-01-13 02:34:06 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
// SymbolTableListTraits specialization for MDSymbolTable.
|
|
|
|
void ilist_traits<NamedMDNode>
|
|
|
|
::addNodeToList(NamedMDNode *N) {
|
|
|
|
assert(N->getParent() == 0 && "Value already in a container!!");
|
|
|
|
Module *Owner = getListOwner();
|
|
|
|
N->setParent(Owner);
|
|
|
|
MDSymbolTable &ST = Owner->getMDSymbolTable();
|
|
|
|
ST.insert(N->getName(), N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
|
|
|
|
N->setParent(0);
|
|
|
|
Module *Owner = getListOwner();
|
|
|
|
MDSymbolTable &ST = Owner->getMDSymbolTable();
|
|
|
|
ST.remove(N->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 04:41:31 +08:00
|
|
|
static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
|
|
|
|
return *(SmallVector<WeakVH, 4>*)Operands;
|
2009-12-28 16:07:14 +08:00
|
|
|
}
|
|
|
|
|
2010-01-13 02:57:56 +08:00
|
|
|
NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
|
2010-01-11 02:48:49 +08:00
|
|
|
MDNode *const *MDs,
|
2009-07-30 05:58:56 +08:00
|
|
|
unsigned NumMDs, Module *ParentModule)
|
2010-01-09 08:30:14 +08:00
|
|
|
: Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
|
2009-07-30 01:16:17 +08:00
|
|
|
setName(N);
|
2010-01-06 04:41:31 +08:00
|
|
|
Operands = new SmallVector<WeakVH, 4>();
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2010-01-06 04:41:31 +08:00
|
|
|
SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
|
2009-10-22 07:57:35 +08:00
|
|
|
for (unsigned i = 0; i != NumMDs; ++i)
|
2010-01-06 04:41:31 +08:00
|
|
|
Node.push_back(WeakVH(MDs[i]));
|
2009-10-22 07:57:35 +08:00
|
|
|
|
2010-01-13 02:34:06 +08:00
|
|
|
if (ParentModule)
|
2009-07-30 05:58:56 +08:00
|
|
|
ParentModule->getNamedMDList().push_back(this);
|
2009-07-29 08:33:07 +08:00
|
|
|
}
|
2009-08-03 14:19:01 +08:00
|
|
|
|
2009-08-12 02:01:24 +08:00
|
|
|
NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
|
2009-10-19 15:10:59 +08:00
|
|
|
assert(NMD && "Invalid source NamedMDNode!");
|
2010-01-06 04:41:31 +08:00
|
|
|
SmallVector<MDNode *, 4> Elems;
|
2009-12-31 09:22:29 +08:00
|
|
|
Elems.reserve(NMD->getNumOperands());
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
|
|
|
|
Elems.push_back(NMD->getOperand(i));
|
2009-08-14 05:58:54 +08:00
|
|
|
return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
|
|
|
|
Elems.data(), Elems.size(), M);
|
2009-08-12 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
2009-12-28 16:07:14 +08:00
|
|
|
NamedMDNode::~NamedMDNode() {
|
|
|
|
dropAllReferences();
|
|
|
|
delete &getNMDOps(Operands);
|
|
|
|
}
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
/// getNumOperands - Return number of NamedMDNode operands.
|
|
|
|
unsigned NamedMDNode::getNumOperands() const {
|
2009-12-28 16:07:14 +08:00
|
|
|
return (unsigned)getNMDOps(Operands).size();
|
|
|
|
}
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
/// getOperand - Return specified operand.
|
2010-01-06 04:41:31 +08:00
|
|
|
MDNode *NamedMDNode::getOperand(unsigned i) const {
|
2009-12-31 09:22:29 +08:00
|
|
|
assert(i < getNumOperands() && "Invalid Operand number!");
|
2010-01-06 04:41:31 +08:00
|
|
|
return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
|
2009-12-28 16:07:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-31 09:22:29 +08:00
|
|
|
/// addOperand - Add metadata Operand.
|
2010-01-06 04:41:31 +08:00
|
|
|
void NamedMDNode::addOperand(MDNode *M) {
|
|
|
|
getNMDOps(Operands).push_back(WeakVH(M));
|
2009-12-28 16:07:14 +08:00
|
|
|
}
|
|
|
|
|
2009-08-03 14:19:01 +08:00
|
|
|
/// eraseFromParent - Drop all references and remove the node from parent
|
|
|
|
/// module.
|
|
|
|
void NamedMDNode::eraseFromParent() {
|
|
|
|
getParent()->getNamedMDList().erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dropAllReferences - Remove all uses and clear node vector.
|
|
|
|
void NamedMDNode::dropAllReferences() {
|
2009-12-28 16:07:14 +08:00
|
|
|
getNMDOps(Operands).clear();
|
2009-08-03 14:19:01 +08:00
|
|
|
}
|
|
|
|
|
2010-01-08 03:39:36 +08:00
|
|
|
/// setName - Set the name of this named metadata.
|
2010-01-13 02:57:56 +08:00
|
|
|
void NamedMDNode::setName(const Twine &NewName) {
|
|
|
|
assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
|
|
|
|
|
|
|
|
SmallString<256> NameData;
|
2010-01-13 20:45:23 +08:00
|
|
|
StringRef NameRef = NewName.toStringRef(NameData);
|
2010-01-13 02:57:56 +08:00
|
|
|
|
|
|
|
// Name isn't changing?
|
|
|
|
if (getName() == NameRef)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Name = NameRef.str();
|
2010-01-13 02:34:06 +08:00
|
|
|
if (Parent)
|
2010-01-13 02:57:56 +08:00
|
|
|
Parent->getMDSymbolTable().insert(NameRef, this);
|
2010-01-08 03:39:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getName - Return a constant reference to this named metadata's name.
|
|
|
|
StringRef NamedMDNode::getName() const {
|
|
|
|
return StringRef(Name);
|
|
|
|
}
|
2009-09-17 02:09:00 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-31 09:05:46 +08:00
|
|
|
// LLVMContext MDKind naming implementation.
|
2009-09-17 02:09:00 +08:00
|
|
|
//
|
2009-10-23 03:36:54 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/// isValidName - Return true if Name is a valid custom metadata handler name.
|
|
|
|
static bool isValidName(StringRef MDName) {
|
|
|
|
if (MDName.empty())
|
|
|
|
return false;
|
2009-12-29 07:41:32 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
if (!isalpha(MDName[0]))
|
|
|
|
return false;
|
2009-12-29 07:41:32 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-10-23 03:36:54 +08:00
|
|
|
}
|
2009-12-29 17:01:33 +08:00
|
|
|
#endif
|
2009-09-17 02:09:00 +08:00
|
|
|
|
2009-12-29 04:45:51 +08:00
|
|
|
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
|
2009-12-29 17:01:33 +08:00
|
|
|
unsigned LLVMContext::getMDKindID(StringRef Name) const {
|
|
|
|
assert(isValidName(Name) && "Invalid MDNode name");
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
unsigned &Entry = pImpl->CustomMDKindNames[Name];
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 04:45:51 +08:00
|
|
|
// If this is new, assign it its ID.
|
2009-12-29 17:01:33 +08:00
|
|
|
if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
|
2009-12-29 04:45:51 +08:00
|
|
|
return Entry;
|
2009-09-17 02:09:00 +08:00
|
|
|
}
|
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
2009-12-29 17:01:33 +08:00
|
|
|
void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
|
|
|
|
Names.resize(pImpl->CustomMDKindNames.size()+1);
|
2009-12-29 07:41:32 +08:00
|
|
|
Names[0] = "";
|
2009-12-29 17:01:33 +08:00
|
|
|
for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
|
2010-01-11 02:48:49 +08:00
|
|
|
E = pImpl->CustomMDKindNames.end(); I != E; ++I)
|
2009-12-29 07:41:32 +08:00
|
|
|
// MD Handlers are numbered from 1.
|
|
|
|
Names[I->second] = I->first();
|
|
|
|
}
|
2009-09-30 04:30:57 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Instruction Metadata method implementations.
|
|
|
|
//
|
2009-09-30 04:30:57 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
void Instruction::setMetadata(const char *Kind, MDNode *Node) {
|
|
|
|
if (Node == 0 && !hasMetadata()) return;
|
|
|
|
setMetadata(getContext().getMDKindID(Kind), Node);
|
2009-09-17 02:09:00 +08:00
|
|
|
}
|
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
MDNode *Instruction::getMetadataImpl(const char *Kind) const {
|
|
|
|
return getMetadataImpl(getContext().getMDKindID(Kind));
|
2009-12-29 07:41:32 +08:00
|
|
|
}
|
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
/// setMetadata - Set the metadata of of the specified kind to the specified
|
|
|
|
/// node. This updates/replaces metadata if already present, or removes it if
|
|
|
|
/// Node is null.
|
|
|
|
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
|
|
|
|
if (Node == 0 && !hasMetadata()) return;
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
// Handle the case when we're adding/updating metadata on an instruction.
|
|
|
|
if (Node) {
|
2009-12-29 17:01:33 +08:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
|
|
|
assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
|
2009-12-29 07:41:32 +08:00
|
|
|
if (Info.empty()) {
|
2009-12-29 17:01:33 +08:00
|
|
|
setHasMetadata(true);
|
2009-12-29 07:41:32 +08:00
|
|
|
} else {
|
|
|
|
// Handle replacement of an existing value.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
2009-12-29 17:01:33 +08:00
|
|
|
if (Info[i].first == KindID) {
|
2009-12-29 07:41:32 +08:00
|
|
|
Info[i].second = Node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
// No replacement, just add it to the list.
|
2009-12-29 17:01:33 +08:00
|
|
|
Info.push_back(std::make_pair(KindID, Node));
|
2009-09-30 04:42:25 +08:00
|
|
|
return;
|
2009-12-29 07:41:32 +08:00
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
// Otherwise, we're removing metadata from an instruction.
|
2009-12-29 17:01:33 +08:00
|
|
|
assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
|
2009-12-29 07:41:32 +08:00
|
|
|
"HasMetadata bit out of date!");
|
2009-12-29 17:01:33 +08:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
// Common case is removing the only entry.
|
2009-12-29 17:01:33 +08:00
|
|
|
if (Info.size() == 1 && Info[0].first == KindID) {
|
|
|
|
getContext().pImpl->MetadataStore.erase(this);
|
|
|
|
setHasMetadata(false);
|
2009-12-29 07:41:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
// Handle replacement of an existing value.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
2009-12-29 17:01:33 +08:00
|
|
|
if (Info[i].first == KindID) {
|
2009-12-29 07:41:32 +08:00
|
|
|
Info[i] = Info.back();
|
|
|
|
Info.pop_back();
|
|
|
|
assert(!Info.empty() && "Removing last entry should be handled above");
|
2009-09-30 04:42:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-29 07:41:32 +08:00
|
|
|
// Otherwise, removing an entry that doesn't exist on the instruction.
|
2009-09-30 04:42:25 +08:00
|
|
|
}
|
2009-11-27 06:54:26 +08:00
|
|
|
|
2009-12-29 07:41:32 +08:00
|
|
|
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
|
2009-12-29 17:01:33 +08:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
|
|
|
assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (I->first == KindID)
|
|
|
|
return I->second;
|
|
|
|
return 0;
|
2009-12-29 07:41:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
|
|
|
|
MDNode*> > &Result)const {
|
2009-12-29 17:01:33 +08:00
|
|
|
assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
|
|
|
|
"Shouldn't have called this");
|
|
|
|
const LLVMContextImpl::MDMapTy &Info =
|
|
|
|
getContext().pImpl->MetadataStore.find(this)->second;
|
|
|
|
assert(!Info.empty() && "Shouldn't have called this");
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
Result.clear();
|
|
|
|
Result.append(Info.begin(), Info.end());
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
// Sort the resulting array so it is stable.
|
|
|
|
if (Result.size() > 1)
|
|
|
|
array_pod_sort(Result.begin(), Result.end());
|
2009-12-29 07:41:32 +08:00
|
|
|
}
|
|
|
|
|
2009-12-29 15:44:16 +08:00
|
|
|
/// removeAllMetadata - Remove all metadata from this instruction.
|
|
|
|
void Instruction::removeAllMetadata() {
|
|
|
|
assert(hasMetadata() && "Caller should check");
|
2009-12-29 17:01:33 +08:00
|
|
|
getContext().pImpl->MetadataStore.erase(this);
|
|
|
|
setHasMetadata(false);
|
2009-12-29 15:44:16 +08:00
|
|
|
}
|
|
|
|
|