问题
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!
回答1:
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);
回答2:
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());
来源:https://stackoverflow.com/questions/19947606/how-get-ir-value-from-llvm-dbg-declare