如何提取从LLVM的getelementptr指令数组索引值

问题描述:

array[5] = 20; 

等效LLVM IR如何提取从LLVM的getelementptr指令数组索引值

%arrayidx = getelementptr inbounds i32, i32* %2, i64 5 
store i32 20, i32* %arrayidx, align 4 

如何从LLVM IR提取5?

+0

你的意思是使用C++ API? –

+0

是的,我的意思是C++ –

如果您有GetElementPtrInst* GEP,则可以使用GEP->getOperand(i)(操作数0是指针,其余操作数是索引)访问索引。为了得到值5,就可以检查该指数是一个ConstantInt如果是这样得到它的价值,像这样:

if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(1)) { 
    uint64_t Idx = CI->getZExtValue(); 
}