[LLVMdev] Fwd: Need help to add operand for global metadata

Henry Chung zhguanwen at gmail.com
Thu Oct 2 03:06:11 PDT 2014

Hi llvm-ers,

I try to assign unique IDs for each basic block in a module using globle
metadata (NamedMDNode). However, I got an error: '*Invalide operand for
global metadata!*' and I have no idea how to deal with it. My llvm version
is 3.6.0 and I use visual studio 2013 to build the project. The way to add
MDNode is learnt from LLVM Giri project (their project is based on llvm

Thanks for your time. :-)

Here is the code snippet:

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),


ZHONG, Guanwen (Henry)
*Ph.D. Candidate*
*System & Networking Research Lab*
*National University of Singapore*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141002/268d2bb4/attachment.html>

More information about the llvm-dev mailing list