Reputation: 185
I want to assign unique IDs for basic blocks in a module using global metadata. But I got an error: "Invalid operand for global metadata!". Any problems in the follow code snippet?
Here is part of the code I add operand of a named metadata node:
bool runOnModule(Module &M) {
...
NamedMDNode *NMD = M.getOrInsertNamedMetadata(mdKindName);
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
for (Function::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) {
errs() << "Basic Block number: " << counter << "\n";
NMD->addOperand(assignID(BI, ++counter));
errs() << "Basic Block name: " << BI->getName() << "\n";
}
}
...
}
// Assign unique IDs to Basic Blocks as Metadata Nodes
MDNode* assignID (BasicBlock* BB, unsigned id) {
// Fetch the context in which the enclosing module was defined
LLVMContext &Context = BB->getParent()->getParent()->getContext();
// Create a metadata node that contains ID as a constant:
Value* ID[2];
ID[0] = BB;
ID[1] = ConstantInt::get(Type::getInt32Ty(Context), id);
return MDNode::getWhenValsUnresolved(Context, ArrayRef<Value*>(ID, 2), false);
}
The version of llvm is 3.6.0. I am using visual studio 2013 for my project.
Thanks, henry
Solution: ID[0] should store MDString::get(Context, BB->getName())
Upvotes: 2
Views: 350
Reputation: 26878
By passing false
as the last argument for getWhenValsUnresolved
, you have created a global metadata node. Global metadata may only contain:
MDNode
s.MDString
s.If you want to use something which is function-local - such as instruction, argument or (in your case) a basic-block, you need to use local metadata. Create one by using true
as the last argument there.
Upvotes: 2