在调用中获取函数的参数,llvm

Get the arguments of a function in bitcasts in calls, llvm

我对 llvm 还很陌生,无法深入研究以下 IR 行:

%call2 = call float bitcast (float (float, i32*)* @function to float (float, i32 addrspace(1)*)*)(float %11, i32 addrspace(1)* %arrayidx)

我需要从中提取的是函数参数类型的行(即,(float %11, i32 addrspace(1)* %arrayidx))

我尝试了以下方法,也尝试了一些 ConstExpr,但无法提取 addrspace(1)

for (Function::iterator block = F.begin(), blockEnd = F.end(); block != blockEnd; ++block) {
  for (BasicBlock::iterator inst = block->begin(), instEnd = block->end(); inst != instEnd; ++inst) {
    if (CallInst *call = dyn_cast<CallInst>(inst)) {
      Function *calledFunction = call->getCalledFunction();
      if (calledFunction == NULL) { // Called function is wrapped in a bitcast
      Value* v = call->getCalledValue();
      calledFunction = dyn_cast<Function>(v->stripPointerCasts());
      FunctionType *ft = calledFunction->getFunctionType(); // This gives me the type "from" (the args without addrspace(1)
      for( Function::arg_iterator arg = calledFunction->arg_begin(), marg_end = calledFunction->arg_end(); arg != marg_end ; arg++){
        Type *argTy = arg->getType();
        if (PointerType *ptrTy = dyn_cast<PointerType>(argTy)) {
          if( ptrTy->getAddressSpace() !=0)
            ...
          }
        }
      }
    }
  }
}

上面的代码给出了类型 (float, i32*) 而不是 (float, i32 addrspace(1)*)

有什么帮助吗?

llvm ir

%call2 = call float bitcast (float (float, i32*)* @function to float (float, i32 addrspace(1)*)*)(float %11, i32 addrspace(1)* %arrayidx)

正在将函数类型 float (float, i32*) 转换为 float (float, i32 addrspace(1)*) 并使用参数 (%11, %arrayidx).

调用它

如果你想要参数的类型,你可以使用 callInst::getArgOperand 检查它以获取调用指令本身的参数。

for (Function::iterator block = F.begin(), blockEnd = F.end(); block != blockEnd; ++block) {
    for (BasicBlock::iterator inst = block->begin(), instEnd = block->end(); inst != instEnd; ++inst) {
        if (CallInst *call = dyn_cast<CallInst>(inst)) {
                Value *val11 = call->getArgOperand(0);
                Value *valarrayIdx = call->getArgOperand(1);

                Type *val11ty = val11->getType(); // this should be of float
                Type *valarrayIdx = valarrayIdx->getType(); // this should be of i32 address(1)*
            }
        }
    }

CallInst::getCalledFunction 给你功能。

有关更多信息,您可以浏览 http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html