LLVM IR: get the return value of the callsite
Asked Answered
O

2

6

Here is a quick question on analyzing LLVM IR. So basically I am trying to fetch the return value of the LLVM IR function call statement, something like this:

%47  =   call i256 @test(i256 %46)

I want to get access %47.

And this is the code I have been using to access the parameter.

      else if (funcName.contains("test")) {
        Value *op = CI->getOperand(0);
        if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(op))
          // get the first function parameter
          op = GEP->getPointerOperand();
      }

The very suprising finding is that I just cannot find something like "get return value" or so in the document: http://llvm.org/doxygen/classllvm_1_1CallInst.html

Could anyone shed some lights here? Thanks a lot.

Overcautious answered 25/3, 2019 at 16:12 Comment(4)
What exactly do you want to get here? The actual return value that the function will return at run time as a constant? Obviously you won't be able to know this until run time (unless the call is constant-folded, in which case the CallInst no longer exists). Or an instruction that will evaluate to the return value? That's just the CallInst itself.Lymph
@Lymph Thank you for the reply. So I want to get a Value instance of the variable %47.Overcautious
That's the CallInst.Lymph
I have an use case similar to the one you asked, get the value of %47 and pass it as an argument to another function which accepts i32 type. The problem is if we just use CallInst, the parameter will look something like this call%47 i32 which wont be compatible with the new function which accepts i32 type. LLVM is SSA but scenarios like this is an interesting issueMyra
E
9

CI (The call instruction) is its return value. It has a type that inherits Value.

If you want to do 1 + %47, just as an example, you make an add like this: Value * Add = BinaryOperation::Create(Instruction::Add, CI, ConstantInt::get(i256, 1), ...); The add instruction, in turn, is its result, and Add->getType() == i256 since it is the sum of two values that both have type i256.

Eggshell answered 25/3, 2019 at 17:42 Comment(4)
Is there other way to get the return value of the function if i dont want to insert the instruction. All i want is the return value of the functionMyra
Since llvm is SSA, i thought i can simply pass the value to another instrumentation function but the type is not compatible. The new function param is of i32 type. If i pass as is it creates something like this call %i32 which is not compatible with i32 param type of the new functionMyra
The instruction is the value. If you don't insert the instruction, the value doesn't enter the program's universe. If you want to know the return value of a function, there has to be a call that function, and in SSA. the call instruction is the return value, see? Or is your real goal to reason at compile time about what the function might return, if called, but without calling it?Eggshell
You can pass values, you need to cast. How to cast is up to you. Some languages sign-extend ints, so true becomes a 32-bit -1, others zero-extend so true becomes 1, for example. CastInst::Create() lets you do anything, so long as you decide what sort of cast is appropriate.Eggshell
J
0

As a different answer in case you need the string of the "return value" name, you can use inst.getName() or inst.getNameOrAsOperand() on the Instruction object, which is derived from the Value class.

Jehial answered 25/1, 2022 at 10:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.