Dipanjan Das via llvm-dev
2017-Apr-19 16:05 UTC
[llvm-dev] How to get the arguments of a function call in LLVM?
I want to write an LLVM pass that'll extract the arguments of function
calls. If the argument is a constant one, my objective is to recover what
that constant is.
The IR looks like
%2 = call noalias i8* @malloc(i64 512) #3
The LLVM pass looks like
bool runOnFunction(Function &F) override {
for (auto& B : F) {
for (auto& I : B) {
if(CallInst* call_inst = dyn_cast<CallInst>(&I)) {
Function* fn = call_inst->getCalledFunction();
StringRef fn_name = fn->getName();
errs() << fn_name << " : " <<
call_inst->getArgOperand(0) << "\n";
for(auto arg = fn->arg_begin(); arg != fn->arg_end();
++arg) {
errs() << *arg << "\n";
}
}
}
}
return false;
}
If I run the pass through `opt`, it produces the following
malloc : 0x3df3f40
i64 %0
What does `0x3df3f40` represent? Instead of `164` and `512`, why does it
produce `i64` and `%0`?
--
Thanks & Regards,
Dipanjan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170419/6aa99251/attachment.html>
Tim Northover via llvm-dev
2017-Apr-19 16:28 UTC
[llvm-dev] How to get the arguments of a function call in LLVM?
On 19 April 2017 at 09:05, Dipanjan Das via llvm-dev <llvm-dev at lists.llvm.org> wrote:> If I run the pass through `opt`, it produces the following > > malloc : 0x3df3f40 > i64 %0 > > What does `0x3df3f40` represent?getArgOperand returns a pointer to a Value object. You're printing that pointer directly rather than calling its print method to get a sensible representation.> Instead of `164` and `512`, why does it produce `i64` and `%0`?That's because you're iterating through the argument as it exists at the function's definition/declaration rather than the callsite. You should be using the CallInst's accessors rather than the Function's if you want the arguments used for this particular call. Cheers. Tim.