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());