2009-07-28 21:49:47 +00: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 08:24:16 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2009-07-31 21:35:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2009-07-29 00:33:07 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-09-16 18:09:00 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2009-10-22 19:36:54 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2010-01-12 18:57:56 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-07-29 17:16:17 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
2010-08-20 22:02:26 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2009-12-28 08:24:16 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-28 21:49:47 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-31 21:35:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// MDString implementation.
|
2009-07-31 21:35:40 +00:00
|
|
|
//
|
2009-12-28 08:30:43 +00:00
|
|
|
|
|
|
|
MDString::MDString(LLVMContext &C, StringRef S)
|
2010-01-22 22:52:10 +00:00
|
|
|
: Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
|
2009-12-28 08:30:43 +00:00
|
|
|
|
2009-10-22 00:10:15 +00:00
|
|
|
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
|
2009-07-31 21:35:40 +00:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
2010-01-10 18:48:49 +00:00
|
|
|
StringMapEntry<MDString *> &Entry =
|
2009-07-31 21:35:40 +00:00
|
|
|
pImpl->MDStringCache.GetOrCreateValue(Str);
|
|
|
|
MDString *&S = Entry.getValue();
|
2009-11-26 22:54:26 +00:00
|
|
|
if (!S) S = new MDString(Context, Entry.getKey());
|
|
|
|
return S;
|
2009-07-31 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-31 01:22:29 +00:00
|
|
|
// MDNodeOperand implementation.
|
2009-12-28 07:41:54 +00:00
|
|
|
//
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
// Use CallbackVH to hold MDNode operands.
|
2009-12-28 07:41:54 +00:00
|
|
|
namespace llvm {
|
2009-12-31 01:22:29 +00:00
|
|
|
class MDNodeOperand : public CallbackVH {
|
2009-12-28 07:41:54 +00:00
|
|
|
MDNode *Parent;
|
|
|
|
public:
|
2009-12-31 01:22:29 +00:00
|
|
|
MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
|
|
|
|
~MDNodeOperand() {}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-31 01:05:46 +00:00
|
|
|
void set(Value *V) {
|
2009-12-28 09:10:16 +00:00
|
|
|
setValPtr(V);
|
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
virtual void deleted();
|
|
|
|
virtual void allUsesReplacedWith(Value *NV);
|
|
|
|
};
|
|
|
|
} // end namespace llvm.
|
|
|
|
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
void MDNodeOperand::deleted() {
|
|
|
|
Parent->replaceOperand(this, 0);
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
void MDNodeOperand::allUsesReplacedWith(Value *NV) {
|
|
|
|
Parent->replaceOperand(this, NV);
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-28 21:49:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// MDNode implementation.
|
2009-07-28 21:49:47 +00:00
|
|
|
//
|
2009-12-28 07:41:54 +00:00
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
|
2009-12-31 01:05:46 +00:00
|
|
|
/// the end of the MDNode.
|
2009-12-31 01:22:29 +00:00
|
|
|
static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
|
2010-07-13 19:33:27 +00:00
|
|
|
// Use <= instead of < to permit a one-past-the-end address.
|
|
|
|
assert(Op <= N->getNumOperands() && "Invalid operand number");
|
2009-12-31 01:22:29 +00:00
|
|
|
return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
|
2009-12-31 01:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
|
|
|
|
bool isFunctionLocal)
|
2010-01-22 22:52:10 +00:00
|
|
|
: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
|
2009-12-31 01:05:46 +00:00
|
|
|
NumOperands = NumVals;
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-31 01:05:46 +00:00
|
|
|
if (isFunctionLocal)
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
|
|
|
|
|
|
|
|
// Initialize the operand list, which is co-allocated on the end of the node.
|
2009-12-31 01:22:29 +00:00
|
|
|
for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
|
2009-12-31 01:05:46 +00:00
|
|
|
Op != E; ++Op, ++Vals)
|
2009-12-31 01:22:29 +00:00
|
|
|
new (Op) MDNodeOperand(*Vals, this);
|
2009-12-31 01:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-28 09:12:35 +00:00
|
|
|
/// ~MDNode - Destroy MDNode.
|
|
|
|
MDNode::~MDNode() {
|
2010-01-10 18:48:49 +00:00
|
|
|
assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
|
2009-12-31 01:05:46 +00:00
|
|
|
"Not being destroyed through destroy()?");
|
2010-03-13 01:26:15 +00:00
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
if (isNotUniqued()) {
|
|
|
|
pImpl->NonUniquedMDNodes.erase(this);
|
|
|
|
} else {
|
2009-12-30 21:42:11 +00:00
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-31 01:05:46 +00:00
|
|
|
// Destroy the operands.
|
2009-12-31 01:22:29 +00:00
|
|
|
for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
|
2009-12-31 01:05:46 +00:00
|
|
|
Op != E; ++Op)
|
2009-12-31 01:22:29 +00:00
|
|
|
Op->~MDNodeOperand();
|
2009-12-28 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 04:45:57 +00:00
|
|
|
static const Function *getFunctionForValue(Value *V) {
|
|
|
|
if (!V) return NULL;
|
2010-05-04 12:43:36 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
BasicBlock *BB = I->getParent();
|
|
|
|
return BB ? BB->getParent() : 0;
|
|
|
|
}
|
2010-01-21 21:01:47 +00:00
|
|
|
if (Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->getParent();
|
2010-05-04 12:43:36 +00:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
|
|
return BB->getParent();
|
|
|
|
if (MDNode *MD = dyn_cast<MDNode>(V))
|
|
|
|
return MD->getFunction();
|
2010-01-20 04:45:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-14 20:12:34 +00:00
|
|
|
#ifndef NDEBUG
|
2010-01-20 04:45:57 +00:00
|
|
|
static const Function *assertLocalFunction(const MDNode *N) {
|
2010-01-21 21:01:47 +00:00
|
|
|
if (!N->isFunctionLocal()) return 0;
|
2010-01-14 20:12:34 +00:00
|
|
|
|
2010-07-06 21:05:17 +00:00
|
|
|
// FIXME: This does not handle cyclic function local metadata.
|
2010-01-21 21:01:47 +00:00
|
|
|
const Function *F = 0, *NewF = 0;
|
2010-01-14 01:45:14 +00:00
|
|
|
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
|
2010-01-20 04:45:57 +00:00
|
|
|
if (Value *V = N->getOperand(i)) {
|
2010-01-21 21:01:47 +00:00
|
|
|
if (MDNode *MD = dyn_cast<MDNode>(V))
|
|
|
|
NewF = assertLocalFunction(MD);
|
|
|
|
else
|
|
|
|
NewF = getFunctionForValue(V);
|
2010-01-18 20:36:54 +00:00
|
|
|
}
|
2010-01-21 21:01:47 +00:00
|
|
|
if (F == 0)
|
|
|
|
F = NewF;
|
|
|
|
else
|
|
|
|
assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
|
2010-01-14 01:45:14 +00:00
|
|
|
}
|
|
|
|
return F;
|
|
|
|
}
|
2010-01-18 20:36:54 +00:00
|
|
|
#endif
|
2010-01-14 01:45:14 +00: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-18 22:55:08 +00: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 04:45:57 +00:00
|
|
|
const Function *MDNode::getFunction() const {
|
2010-01-18 20:36:54 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
return assertLocalFunction(this);
|
|
|
|
#endif
|
2010-01-14 01:45:14 +00:00
|
|
|
if (!isFunctionLocal()) return NULL;
|
2010-05-04 14:25:42 +00:00
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
|
|
|
|
if (const Function *F = getFunctionForValue(getOperand(i)))
|
|
|
|
return F;
|
2010-01-18 20:36:54 +00:00
|
|
|
return NULL;
|
2010-01-14 01:45:14 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:05:46 +00: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-28 21:49:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-28 20:16:12 +00: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 07:14:18 +00:00
|
|
|
MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
|
2010-01-26 02:36:35 +00:00
|
|
|
unsigned NumVals, FunctionLocalness FL,
|
|
|
|
bool Insert) {
|
2009-09-03 01:39:20 +00:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
2010-08-24 23:21:12 +00:00
|
|
|
|
|
|
|
// Add all the operand pointers. Note that we don't have to add the
|
|
|
|
// isFunctionLocal bit because that's implied by the operands.
|
2010-08-30 21:18:41 +00:00
|
|
|
// Note that if the operands are later nulled out, the node will be
|
|
|
|
// removed from the uniquing map.
|
2010-08-24 23:21:12 +00:00
|
|
|
FoldingSetNodeID ID;
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
|
|
|
ID.AddPointer(Vals[i]);
|
|
|
|
|
|
|
|
void *InsertPoint;
|
|
|
|
MDNode *N = NULL;
|
|
|
|
|
|
|
|
if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
|
|
|
|
return N;
|
|
|
|
|
2010-01-26 02:36:35 +00: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-28 20:16:12 +00:00
|
|
|
if (isFunctionLocalValue(V)) {
|
2010-01-26 02:36:35 +00:00
|
|
|
isFunctionLocal = true;
|
|
|
|
break;
|
2010-01-10 07:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-26 02:36:35 +00:00
|
|
|
break;
|
|
|
|
case FL_No:
|
|
|
|
isFunctionLocal = false;
|
|
|
|
break;
|
|
|
|
case FL_Yes:
|
|
|
|
isFunctionLocal = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-10 07:14:18 +00:00
|
|
|
|
2010-01-26 02:36:35 +00: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-10 18:48:49 +00:00
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
return N;
|
2009-07-31 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 01:20:33 +00:00
|
|
|
MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
|
|
|
|
return getMDNode(Context, Vals.data(), Vals.size(), FL_Unknown);
|
|
|
|
}
|
2010-01-10 07:14:18 +00:00
|
|
|
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, FL_Unknown);
|
|
|
|
}
|
|
|
|
|
2010-01-26 02:36:35 +00:00
|
|
|
MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
|
2010-01-10 07:14:18 +00:00
|
|
|
unsigned NumVals, bool isFunctionLocal) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
|
|
|
|
}
|
|
|
|
|
2010-01-26 02:36:35 +00:00
|
|
|
MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
|
|
|
|
unsigned NumVals) {
|
|
|
|
return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
|
|
|
|
}
|
|
|
|
|
2010-08-20 22:02:26 +00:00
|
|
|
MDNode *MDNode::getTemporary(LLVMContext &Context, Value *const *Vals,
|
|
|
|
unsigned NumVals) {
|
|
|
|
MDNode *N = (MDNode *)malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
|
|
|
|
N = new (N) MDNode(Context, Vals, NumVals, FL_No);
|
|
|
|
N->setValueSubclassData(N->getSubclassDataFromValue() |
|
|
|
|
NotUniquedBit);
|
|
|
|
LeakDetector::addGarbageObject(N);
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MDNode::deleteTemporary(MDNode *N) {
|
|
|
|
assert(N->use_empty() && "Temporary MDNode has uses!");
|
2010-08-21 02:52:29 +00:00
|
|
|
assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
|
2010-08-23 22:32:05 +00:00
|
|
|
"Deleting a non-temporary uniqued node!");
|
|
|
|
assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
|
|
|
|
"Deleting a non-temporary non-uniqued node!");
|
2010-08-20 22:02:26 +00:00
|
|
|
assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
|
|
|
|
"Temporary MDNode does not have NotUniquedBit set!");
|
|
|
|
assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
|
2010-08-21 02:52:29 +00:00
|
|
|
"Temporary MDNode has DestroyFlag set!");
|
2010-08-20 22:02:26 +00:00
|
|
|
LeakDetector::removeGarbageObject(N);
|
2010-08-21 15:07:23 +00:00
|
|
|
N->destroy();
|
2010-08-20 22:02:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
/// getOperand - Return specified operand.
|
|
|
|
Value *MDNode::getOperand(unsigned i) const {
|
2009-12-31 01:05:46 +00:00
|
|
|
return *getOperandPtr(const_cast<MDNode*>(this), i);
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:05:46 +00:00
|
|
|
void MDNode::Profile(FoldingSetNodeID &ID) const {
|
2010-08-24 23:21:12 +00:00
|
|
|
// Add all the operand pointers. Note that we don't have to add the
|
|
|
|
// isFunctionLocal bit because that's implied by the operands.
|
2010-08-30 21:18:41 +00:00
|
|
|
// Note that if the operands are later nulled out, the node will be
|
|
|
|
// removed from the uniquing map.
|
2009-12-31 01:22:29 +00:00
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
|
|
|
|
ID.AddPointer(getOperand(i));
|
2009-12-31 01:05:46 +00:00
|
|
|
}
|
2009-12-28 07:41:54 +00:00
|
|
|
|
2010-03-13 01:26:15 +00:00
|
|
|
void MDNode::setIsNotUniqued() {
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
|
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
pImpl->NonUniquedMDNodes.insert(this);
|
2010-02-18 20:53:16 +00:00
|
|
|
}
|
2009-12-28 07:41:54 +00:00
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
// Replace value from this node's operand list.
|
|
|
|
void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
|
2009-12-28 09:32:10 +00:00
|
|
|
Value *From = *Op;
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2010-04-28 20:16:12 +00: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 12:43:36 +00: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-28 20:16:12 +00:00
|
|
|
|
2009-12-28 09:32:10 +00:00
|
|
|
if (From == To)
|
2009-09-03 01:39:20 +00:00
|
|
|
return;
|
|
|
|
|
2009-12-30 21:42:11 +00:00
|
|
|
// Update the operand.
|
2009-12-31 01:05:46 +00:00
|
|
|
Op->set(To);
|
2009-12-30 21:42:11 +00: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-10 18:48:49 +00:00
|
|
|
|
2009-12-28 09:24:53 +00: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 09:32:10 +00:00
|
|
|
|
2009-12-30 21:42:11 +00: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
|
2010-08-30 21:18:41 +00:00
|
|
|
// brings little reuse. Also, this means we don't need to include
|
|
|
|
// isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
|
2009-12-30 21:42:11 +00:00
|
|
|
if (To == 0) {
|
|
|
|
setIsNotUniqued();
|
|
|
|
return;
|
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-30 21:42:11 +00: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
|
2010-09-28 22:07:19 +00:00
|
|
|
// in the set. If so, then this node is redundant.
|
2009-09-03 01:39:20 +00:00
|
|
|
FoldingSetNodeID ID;
|
|
|
|
Profile(ID);
|
|
|
|
void *InsertPoint;
|
2010-09-28 21:02:55 +00:00
|
|
|
if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
|
2010-09-28 22:07:19 +00:00
|
|
|
replaceAllUsesWith(N);
|
|
|
|
destroy();
|
|
|
|
return;
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 09:24:53 +00:00
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
|
|
|
pImpl->MDNodeSet.InsertNode(this, InsertPoint);
|
2010-09-14 01:37:57 +00:00
|
|
|
|
|
|
|
// If this MDValue was previously function-local but no longer is, clear
|
|
|
|
// its function-local flag.
|
|
|
|
if (isFunctionLocal() && !isFunctionLocalValue(To)) {
|
|
|
|
bool isStillFunctionLocal = false;
|
|
|
|
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
|
|
|
|
Value *V = getOperand(i);
|
|
|
|
if (!V) continue;
|
|
|
|
if (isFunctionLocalValue(V)) {
|
|
|
|
isStillFunctionLocal = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isStillFunctionLocal)
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
|
|
|
|
}
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 00:33:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// NamedMDNode implementation.
|
2009-07-29 00:33:07 +00:00
|
|
|
//
|
2010-01-12 18:34:06 +00:00
|
|
|
|
2010-07-21 18:01:42 +00:00
|
|
|
static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
|
|
|
|
return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
|
2009-12-28 08:07:14 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 23:38:33 +00:00
|
|
|
NamedMDNode::NamedMDNode(const Twine &N)
|
|
|
|
: Name(N.str()), Parent(0),
|
|
|
|
Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
|
2009-08-11 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 08:07:14 +00:00
|
|
|
NamedMDNode::~NamedMDNode() {
|
|
|
|
dropAllReferences();
|
|
|
|
delete &getNMDOps(Operands);
|
|
|
|
}
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
/// getNumOperands - Return number of NamedMDNode operands.
|
|
|
|
unsigned NamedMDNode::getNumOperands() const {
|
2009-12-28 08:07:14 +00:00
|
|
|
return (unsigned)getNMDOps(Operands).size();
|
|
|
|
}
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
/// getOperand - Return specified operand.
|
2010-01-05 20:41:31 +00:00
|
|
|
MDNode *NamedMDNode::getOperand(unsigned i) const {
|
2009-12-31 01:22:29 +00:00
|
|
|
assert(i < getNumOperands() && "Invalid Operand number!");
|
2010-07-21 18:54:18 +00:00
|
|
|
return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
|
2009-12-28 08:07:14 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:22:29 +00:00
|
|
|
/// addOperand - Add metadata Operand.
|
2010-01-05 20:41:31 +00:00
|
|
|
void NamedMDNode::addOperand(MDNode *M) {
|
2010-09-14 01:37:57 +00:00
|
|
|
assert(!M->isFunctionLocal() &&
|
|
|
|
"NamedMDNode operands must not be function-local!");
|
2010-07-21 18:01:42 +00:00
|
|
|
getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
|
2009-12-28 08:07:14 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 06:19:01 +00:00
|
|
|
/// eraseFromParent - Drop all references and remove the node from parent
|
|
|
|
/// module.
|
|
|
|
void NamedMDNode::eraseFromParent() {
|
2010-07-21 23:38:33 +00:00
|
|
|
getParent()->eraseNamedMetadata(this);
|
2009-08-03 06:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dropAllReferences - Remove all uses and clear node vector.
|
|
|
|
void NamedMDNode::dropAllReferences() {
|
2009-12-28 08:07:14 +00:00
|
|
|
getNMDOps(Operands).clear();
|
2009-08-03 06:19:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 19:39:36 +00:00
|
|
|
/// getName - Return a constant reference to this named metadata's name.
|
|
|
|
StringRef NamedMDNode::getName() const {
|
|
|
|
return StringRef(Name);
|
|
|
|
}
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Instruction Metadata method implementations.
|
|
|
|
//
|
2009-09-29 20:30:57 +00:00
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
void Instruction::setMetadata(const char *Kind, MDNode *Node) {
|
|
|
|
if (Node == 0 && !hasMetadata()) return;
|
|
|
|
setMetadata(getContext().getMDKindID(Kind), Node);
|
2009-09-16 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
MDNode *Instruction::getMetadataImpl(const char *Kind) const {
|
|
|
|
return getMetadataImpl(getContext().getMDKindID(Kind));
|
2009-12-28 23:41:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 09:01:33 +00: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-10 18:48:49 +00:00
|
|
|
|
2010-03-30 23:03:27 +00:00
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
|
|
|
if (KindID == LLVMContext::MD_dbg) {
|
2010-04-02 20:21:22 +00:00
|
|
|
DbgLoc = DebugLoc::getFromDILocation(Node);
|
2010-03-30 23:03:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// Handle the case when we're adding/updating metadata on an instruction.
|
|
|
|
if (Node) {
|
2009-12-29 09:01:33 +00:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
2010-03-30 23:03:27 +00:00
|
|
|
assert(!Info.empty() == hasMetadataHashEntry() &&
|
|
|
|
"HasMetadata bit is wonked");
|
2009-12-28 23:41:32 +00:00
|
|
|
if (Info.empty()) {
|
2010-03-30 23:03:27 +00:00
|
|
|
setHasMetadataHashEntry(true);
|
2009-12-28 23:41:32 +00:00
|
|
|
} else {
|
|
|
|
// Handle replacement of an existing value.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
2009-12-29 09:01:33 +00:00
|
|
|
if (Info[i].first == KindID) {
|
2009-12-28 23:41:32 +00:00
|
|
|
Info[i].second = Node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// No replacement, just add it to the list.
|
2009-12-29 09:01:33 +00:00
|
|
|
Info.push_back(std::make_pair(KindID, Node));
|
2009-09-29 20:42:25 +00:00
|
|
|
return;
|
2009-12-28 23:41:32 +00:00
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// Otherwise, we're removing metadata from an instruction.
|
2010-03-30 23:03:27 +00:00
|
|
|
assert(hasMetadataHashEntry() &&
|
|
|
|
getContext().pImpl->MetadataStore.count(this) &&
|
2009-12-28 23:41:32 +00:00
|
|
|
"HasMetadata bit out of date!");
|
2009-12-29 09:01:33 +00:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// Common case is removing the only entry.
|
2009-12-29 09:01:33 +00:00
|
|
|
if (Info.size() == 1 && Info[0].first == KindID) {
|
|
|
|
getContext().pImpl->MetadataStore.erase(this);
|
2010-03-30 23:03:27 +00:00
|
|
|
setHasMetadataHashEntry(false);
|
2009-12-28 23:41:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2010-03-30 23:03:27 +00:00
|
|
|
// Handle removal of an existing value.
|
2009-12-28 23:41:32 +00:00
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
2009-12-29 09:01:33 +00:00
|
|
|
if (Info[i].first == KindID) {
|
2009-12-28 23:41:32 +00:00
|
|
|
Info[i] = Info.back();
|
|
|
|
Info.pop_back();
|
|
|
|
assert(!Info.empty() && "Removing last entry should be handled above");
|
2009-09-29 20:42:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-28 23:41:32 +00:00
|
|
|
// Otherwise, removing an entry that doesn't exist on the instruction.
|
2009-09-29 20:42:25 +00:00
|
|
|
}
|
2009-11-26 22:54:26 +00:00
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
|
2010-03-30 23:03:27 +00:00
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
|
|
|
if (KindID == LLVMContext::MD_dbg)
|
2010-04-01 05:23:13 +00:00
|
|
|
return DbgLoc.getAsMDNode(getContext());
|
2010-03-30 23:03:27 +00:00
|
|
|
|
|
|
|
if (!hasMetadataHashEntry()) return 0;
|
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
|
2010-03-30 23:03:27 +00:00
|
|
|
assert(!Info.empty() && "bit out of sync with hash table");
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-29 09:01:33 +00: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-28 23:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
|
2010-03-30 23:03:27 +00:00
|
|
|
MDNode*> > &Result) const {
|
|
|
|
Result.clear();
|
|
|
|
|
|
|
|
// Handle 'dbg' as a special case since it is not stored in the hash table.
|
2010-04-01 05:23:13 +00:00
|
|
|
if (!DbgLoc.isUnknown()) {
|
|
|
|
Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
|
|
|
|
DbgLoc.getAsMDNode(getContext())));
|
2010-03-30 23:03:27 +00:00
|
|
|
if (!hasMetadataHashEntry()) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(hasMetadataHashEntry() &&
|
|
|
|
getContext().pImpl->MetadataStore.count(this) &&
|
2009-12-29 09:01:33 +00: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-10 18:48:49 +00:00
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
Result.append(Info.begin(), Info.end());
|
2010-01-10 18:48:49 +00:00
|
|
|
|
2009-12-29 09:01:33 +00:00
|
|
|
// Sort the resulting array so it is stable.
|
|
|
|
if (Result.size() > 1)
|
|
|
|
array_pod_sort(Result.begin(), Result.end());
|
2009-12-28 23:41:32 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 05:23:13 +00: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-20 22:25:04 +00: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 07:44:16 +00:00
|
|
|
}
|
|
|
|
|