How to get the const argument data from Function?


I am doing a project involving checking a called specific function’s argument. Suppose that the function is int f(const char* str).

When I am analyzing such a snippet:


, then compiled by Clang, I will have the “hello” as a Constant Array in the IR code. My goal is to call APIs of LLVM to get the “hello” from IR code.

Now suppose the I got the llvm::Function* fn from some Module, so I can get the access the ArgumentList by iterator const_arg_iterator it.

However, the iterator now I got is of type llvm::ilist_iterator<const llvm::Argument>::const_iterator. When I am trying to cast it into ``llvm::ilist_iterator::const_iterator`, it can’t success. But if I get the raw pointer out of it, the program will simply crash.

While Argument inherits Value, ConstantArray inherits Value as well. But how should I cast one into another? It is worth nothing that, hen I used getType()->getTypeID(), it is ConstantArrayVal, and that seems to be the right direction.

Thanks for any help!

  • Zhen

llvm::Argument represents the formal parameter passed to its parent
function. In other words, if you have

int f(int x) {

then, within the body of `f`, `x` will be represented using an
llvm::Argument. This is independent of any specific calls to `f`.

Now every call / invoke to `f` will be represented using an
llvm::CallInst / llvm::InvokeInst. These structures will contain the
call target, and the set of actual parameter values passed to that
call target in that call which you can access using getArgumentNo. So


will be represented using a llvm::CallInst which has 42 as the 0th
argument. At runtime, of course, the actual parameter values will be
bound to formal parameter values in the usual sense during the control
transfer corresponding to the call.

-- Sanjoy