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-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) {
|
2010-07-14 03:33:27 +08:00
|
|
|
// Use <= instead of < to permit a one-past-the-end address.
|
|
|
|
assert(Op <= N->getNumOperands() && "Invalid operand number");
|
2009-12-31 09:22:29 +08:00
|
|
|
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()?");
|
2010-03-13 09:26:15 +08:00
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
if (isNotUniqued()) {
|
|
|
|
pImpl->NonUniquedMDNodes.erase(this);
|
|
|
|
} else {
|
2009-12-31 05:42:11 +08:00
|
|
|
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) {
|
|
|
|
if (!V) return NULL;
|
2010-05-04 20:43:36 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
BasicBlock *BB = I->getParent();
|
|
|
|
return BB ? BB->getParent() : 0;
|
|
|
|
}
|
2010-01-22 05:01:47 +08:00
|
|
|
if (Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->getParent();
|
2010-05-04 20:43:36 +08:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
|
|
return BB->getParent();
|
|
|
|
if (MDNode *MD = dyn_cast<MDNode>(V))
|
|
|
|
return MD->getFunction();
|
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-07-07 05:05:17 +08:00
|
|
|
// FIXME: This does not handle cyclic function local metadata.
|
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-05-04 22:25:42 +08:00
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
|
|
|
|
if (const Function *F = getFunctionForValue(getOperand(i)))
|
|
|
|
return F;
|
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-04-29 04:16:12 +08:00
|
|
|
/// isFunctionLocalValue - Return true if this is a value that would require a
|
|
|
|
/// function-local MDNode.
|
|
|
|
static bool isFunctionLocalValue(Value *V) {
|
|
|
|
return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
|
|
|
|
(isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
|
|
|
|
}
|
|
|
|
|
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;
|
2010-01-26 10:36:35 +08:00
|
|
|
bool isFunctionLocal = false;
|
|
|
|
switch (FL) {
|
|
|
|
case FL_Unknown:
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i) {
|
|
|
|
Value *V = Vals[i];
|
|
|
|
if (!V) continue;
|
2010-04-29 04:16:12 +08:00
|
|
|
if (isFunctionLocalValue(V)) {
|
2010-01-26 10:36:35 +08:00
|
|
|
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-03-25 14:04:47 +08:00
|
|
|
FoldingSetNodeID ID;
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
|
|
|
ID.AddPointer(Vals[i]);
|
|
|
|
ID.AddBoolean(isFunctionLocal);
|
|
|
|
|
|
|
|
void *InsertPoint;
|
|
|
|
MDNode *N = NULL;
|
|
|
|
|
|
|
|
if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
|
|
|
|
return N;
|
|
|
|
|
|
|
|
if (!Insert)
|
|
|
|
return NULL;
|
|
|
|
|
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));
|
2010-03-25 13:36:13 +08:00
|
|
|
ID.AddBoolean(isFunctionLocal());
|
2009-12-31 09:05:46 +08:00
|
|
|
}
|
2009-12-28 15:41:54 +08:00
|
|
|
|
2010-03-13 09:26:15 +08:00
|
|
|
void MDNode::setIsNotUniqued() {
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
|
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
pImpl->NonUniquedMDNodes.insert(this);
|
2010-02-19 04:53:16 +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
|
|
|
|
2010-04-29 04:16:12 +08:00
|
|
|
// If is possible that someone did GV->RAUW(inst), replacing a global variable
|
|
|
|
// with an instruction or some other function-local object. If this is a
|
|
|
|
// non-function-local MDNode, it can't point to a function-local object.
|
|
|
|
// Handle this case by implicitly dropping the MDNode reference to null.
|
2010-05-04 20:43:36 +08:00
|
|
|
// Likewise if the MDNode is function-local but for a different function.
|
|
|
|
if (To && isFunctionLocalValue(To)) {
|
|
|
|
if (!isFunctionLocal())
|
|
|
|
To = 0;
|
|
|
|
else {
|
|
|
|
const Function *F = getFunction();
|
|
|
|
const Function *FV = getFunctionForValue(To);
|
|
|
|
// Metadata can be function-local without having an associated function.
|
|
|
|
// So only consider functions to have changed if non-null.
|
|
|
|
if (F && FV && F != FV)
|
|
|
|
To = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 04:16:12 +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
|
|
|
|
|
|
|
// SymbolTableListTraits specialization for MDSymbolTable.
|
2010-07-22 01:53:53 +08:00
|
|
|
void ilist_traits<NamedMDNode>::addNodeToList(NamedMDNode *N) {
|
2010-01-13 02:34:06 +08:00
|
|
|
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-07-22 02:01:42 +08:00
|
|
|
static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
|
|
|
|
return *(SmallVector<TrackingVH<MDNode>, 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-07-22 02:01:42 +08:00
|
|
|
Operands = new SmallVector<TrackingVH<MDNode>, 4>();
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2010-07-22 02:01:42 +08:00
|
|
|
SmallVector<TrackingVH<MDNode>, 4> &Node = getNMDOps(Operands);
|
2009-10-22 07:57:35 +08:00
|
|
|
for (unsigned i = 0; i != NumMDs; ++i)
|
2010-07-22 02:01:42 +08:00
|
|
|
Node.push_back(TrackingVH<MDNode>(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-07-22 02:54:18 +08:00
|
|
|
return dyn_cast<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) {
|
2010-07-22 02:01:42 +08:00
|
|
|
getNMDOps(Operands).push_back(TrackingVH<MDNode>(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-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
|
|
|
|
2010-03-31 07:03:27 +08:00
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
|
|
|
if (KindID == LLVMContext::MD_dbg) {
|
2010-04-03 04:21:22 +08:00
|
|
|
DbgLoc = DebugLoc::getFromDILocation(Node);
|
2010-03-31 07:03:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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];
|
2010-03-31 07:03:27 +08:00
|
|
|
assert(!Info.empty() == hasMetadataHashEntry() &&
|
|
|
|
"HasMetadata bit is wonked");
|
2009-12-29 07:41:32 +08:00
|
|
|
if (Info.empty()) {
|
2010-03-31 07:03:27 +08:00
|
|
|
setHasMetadataHashEntry(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.
|
2010-03-31 07:03:27 +08:00
|
|
|
assert(hasMetadataHashEntry() &&
|
|
|
|
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);
|
2010-03-31 07:03:27 +08:00
|
|
|
setHasMetadataHashEntry(false);
|
2009-12-29 07:41:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-01-11 02:48:49 +08:00
|
|
|
|
2010-03-31 07:03:27 +08:00
|
|
|
// Handle removal of an existing value.
|
2009-12-29 07:41:32 +08:00
|
|
|
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 {
|
2010-03-31 07:03:27 +08:00
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
|
|
|
if (KindID == LLVMContext::MD_dbg)
|
2010-04-01 13:23:13 +08:00
|
|
|
return DbgLoc.getAsMDNode(getContext());
|
2010-03-31 07:03:27 +08:00
|
|
|
|
|
|
|
if (!hasMetadataHashEntry()) return 0;
|
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
2010-03-31 07:03:27 +08:00
|
|
|
assert(!Info.empty() && "bit out of sync with hash table");
|
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,
|
2010-03-31 07:03:27 +08:00
|
|
|
MDNode*> > &Result) const {
|
|
|
|
Result.clear();
|
|
|
|
|
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
2010-04-01 13:23:13 +08:00
|
|
|
if (!DbgLoc.isUnknown()) {
|
|
|
|
Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
|
|
|
|
DbgLoc.getAsMDNode(getContext())));
|
2010-03-31 07:03:27 +08:00
|
|
|
if (!hasMetadataHashEntry()) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(hasMetadataHashEntry() &&
|
|
|
|
getContext().pImpl->MetadataStore.count(this) &&
|
2009-12-29 17:01:33 +08:00
|
|
|
"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.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
|
|
|
}
|
|
|
|
|
2010-04-01 13:23:13 +08:00
|
|
|
void Instruction::
|
|
|
|
getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
|
|
|
|
MDNode*> > &Result) const {
|
|
|
|
Result.clear();
|
|
|
|
assert(hasMetadataHashEntry() &&
|
|
|
|
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");
|
|
|
|
|
|
|
|
Result.append(Info.begin(), Info.end());
|
|
|
|
|
|
|
|
// Sort the resulting array so it is stable.
|
|
|
|
if (Result.size() > 1)
|
|
|
|
array_pod_sort(Result.begin(), Result.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-21 06:25:04 +08:00
|
|
|
/// clearMetadataHashEntries - Clear all hashtable-based metadata from
|
|
|
|
/// this instruction.
|
|
|
|
void Instruction::clearMetadataHashEntries() {
|
|
|
|
assert(hasMetadataHashEntry() && "Caller should check");
|
|
|
|
getContext().pImpl->MetadataStore.erase(this);
|
|
|
|
setHasMetadataHashEntry(false);
|
2009-12-29 15:44:16 +08:00
|
|
|
}
|
|
|
|
|