Question

Given a llvm.dbg.declare, how can I get its llvm value?

e.g.

call void @llvm.dbg.declare(metadata !{i32** %r}, metadata !23), !dbg !24

I want get the Value i32** %r, not the metadata !{i32** %r}.

Please give me the code!

Thanks!

Était-ce utile?

La solution 2

metadata !{i32** %r} is the 1st operand of the call instruction, and i32** %r is the 1st operand of the metadata. So something like this should work:

CallInst I = ... // get the @llvm.dbg.declare call
Value* referredValue = cast<MDNode>(I->getOperand(0))->getOperand(0);

Autres conseils

In later versions of LLVM, it is not allowed to cast Metadata from Value (I was on LLVM 7.0.1). Special classes MetadataAsValue and ValueAsMetadata are required for the cast.

CallInst *CI;      /* Call to llvm.dbg.declare */
AllocaInst *AI;    /* AllocaInst is the result */

Metadata *Meta = cast<MetadataAsValue>(CI->getOperand(0))->getMetadata();
if (isa<ValueAsMetadata>(Meta)) {
  Value *V = cast <ValueAsMetadata>(Meta)->getValue();
  AI = cast<AllocaInst>(V);
}

As you can see, the AllocaInst is wrapped inside ValueAsMetadata and then MetadataAsValue.

If you also want the get the DIVariable from this call.

DIVariable *V = cast<DIVariable>(cast<MetadataAsValue>(CI->getOperand(1))->getMetadata());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top