How get IR value from llvm.dbg.declare

谁说我不能喝 提交于 2019-12-11 13:55:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!