CallInst :: Create()在LLVM中返回什么?

时间:2016-02-12 07:07:13

标签: c++ llvm instrumentation

考虑

static CallInst *Create(Value *Func,
                      ArrayRef<Value *> Args,
                      const Twine &NameStr = "",
                      Instruction *InsertBefore = 0)

这个函数,我想知道这个函数的返回值意味着什么。

例如,在以下代码中,

int foo(int a);
...
Function *foo_ptr = ~~;//say, foo is refered through getOrInsertFunction()
CallInst *ptr = CallInst::Create(foo_ptr, .../* properly set */);

CallInst * ptr是返回值。抽象地说,ptr意味着

  1. int foo(int)返回的整数值;
  2. 或CALL指令
  3. 我认为2号是答案,但是在看一些代码时开始感到困惑。

1 个答案:

答案 0 :(得分:1)

1和2都是“真实的”。它返回调用指令,当我们执行代码时,它的“值”将是函数的返回值。

为了说明,请参加这个小Pascal程序:

program p;

function f: integer;
begin
   f := 42;
end; { f }

begin
   writeln(f);
end.

转换为此LLVM-IR:

; ModuleID = 'TheModule'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%text = type { i32, i8*, i32, i32 }

@input = global %text zeroinitializer, align 8
@output = global %text zeroinitializer, align 8
@UnitIniList = constant [1 x i8*] zeroinitializer

define i32 @P.f() #0 {
entry:
  %f = alloca i32, align 4
  store i32 42, i32* %f
  %0 = load i32, i32* %f
  ret i32 %0
}

define void @__PascalMain() #0 {
entry:
  %calltmp = call i32 @P.f()
  call void @__write_int(%text* @output, i32 %calltmp, i32 0)
  call void @__write_nl(%text* @output)
  ret void
}

declare void @__write_int(%text*, i32, i32)

declare void @__write_nl(%text*)

attributes #0 = { "no-frame-pointer-elim"="true" }

call i32 @P.f()由以下人员生成:

inst = builder.CreateCall(calleF, argsV, "calltmp");

inst的内容为%calltmp = call i32 @P.f() - 这是CallInst'值'。

并且inst返回到对writeln的参数的表达式的求值。