How to get function call arguments in LLVM?

I want to write an LLVM pass that will extract function call arguments. If the argument is constant, my goal is to restore what this constant is.

IR looks like

%2 = call noalias i8* @malloc(i64 512) #3

An 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 a pass through opt, it produces the following

malloc : 0x3df3f40
i64 %0

What does it mean 0x3df3f40? Instead i64and 512why does he create i64and %0?

+4
source share
1 answer

This is a pointer to Value . Try cast<>pasting it in ConstantIntand then call getValue () :

for(auto arg = fn->arg_begin(); arg != fn->arg_end(); ++arg) {
  if(auto* ci = dyn_cast<ConstantInt>(arg))
    errs() << ci->getValue() << "\n";
  errs() << *arg << "\n";
}
+4
source

Source: https://habr.com/ru/post/1675192/


All Articles