//
MDString::MDString(LLVMContext &C, StringRef S)
- : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
+ : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
LLVMContextImpl *pImpl = Context.pImpl;
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
bool isFunctionLocal)
-: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
+: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
NumOperands = NumVals;
if (isFunctionLocal)
MDNode::~MDNode() {
assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
"Not being destroyed through destroy()?");
- if (!isNotUniqued()) {
- LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+ LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+ if (isNotUniqued()) {
+ pImpl->NonUniquedMDNodes.erase(this);
+ } else {
pImpl->MDNodeSet.RemoveNode(this);
}
}
static const Function *getFunctionForValue(Value *V) {
+ assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
if (!V) return NULL;
if (Instruction *I = dyn_cast<Instruction>(V))
return I->getParent()->getParent();
- if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) return BB->getParent();
- if (Argument *A = dyn_cast<Argument>(V)) return A->getParent();
+ if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
+ return BB->getParent();
+ if (Argument *A = dyn_cast<Argument>(V))
+ return A->getParent();
return NULL;
}
#ifndef NDEBUG
static const Function *assertLocalFunction(const MDNode *N) {
- if (!N->isFunctionLocal()) return NULL;
+ if (!N->isFunctionLocal()) return 0;
- const Function *F = NULL, *NewF = NULL;
+ const Function *F = 0, *NewF = 0;
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
if (Value *V = N->getOperand(i)) {
- if (MDNode *MD = dyn_cast<MDNode>(V)) NewF = assertLocalFunction(MD);
- else NewF = getFunctionForValue(V);
+ if (MDNode *MD = dyn_cast<MDNode>(V))
+ NewF = assertLocalFunction(MD);
+ else
+ NewF = getFunctionForValue(V);
}
- if (F && NewF) assert(F == NewF && "inconsistent function-local metadata");
- else if (!F) F = NewF;
+ if (F == 0)
+ F = NewF;
+ else
+ assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
}
return F;
}
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
if (Value *V = getOperand(i)) {
- if (MDNode *MD = dyn_cast<MDNode>(V))
- if (const Function *F = MD->getFunction()) return F;
- else
+ if (MDNode *MD = dyn_cast<MDNode>(V)) {
+ if (const Function *F = MD->getFunction())
+ return F;
+ } else {
return getFunctionForValue(V);
+ }
}
}
return NULL;
}
MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
- unsigned NumVals, FunctionLocalness FL) {
+ unsigned NumVals, FunctionLocalness FL,
+ bool Insert) {
LLVMContextImpl *pImpl = Context.pImpl;
FoldingSetNodeID ID;
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
void *InsertPoint;
- MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
- if (!N) {
- 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;
- }
+ 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;
}
- break;
- case FL_No:
- isFunctionLocal = false;
- break;
- case FL_Yes:
- isFunctionLocal = true;
- break;
}
+ break;
+ case FL_No:
+ isFunctionLocal = false;
+ break;
+ case FL_Yes:
+ isFunctionLocal = true;
+ break;
+ }
- // 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);
+ // 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);
- // InsertPoint will have been set by the FindNodeOrInsertPos call.
- pImpl->MDNodeSet.InsertNode(N, InsertPoint);
- }
return N;
}
return getMDNode(Context, Vals, NumVals, FL_Unknown);
}
-MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
+MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
unsigned NumVals, bool isFunctionLocal) {
return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
}
+MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
+ unsigned NumVals) {
+ return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
+}
+
/// getOperand - Return specified operand.
Value *MDNode::getOperand(unsigned i) const {
return *getOperandPtr(const_cast<MDNode*>(this), i);
ID.AddPointer(getOperand(i));
}
+void MDNode::setIsNotUniqued() {
+ setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
+ LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+ pImpl->NonUniquedMDNodes.insert(this);
+}
// Replace value from this node's operand list.
void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {