如何使用 IRBuilder - LLVM 6.0 加载在单独函数中分配的地址

How to load an address that was allocated in a separate function using IRBuilder - LLVM 6.0

正在进行一个研究项目,该项目需要将加载指令添加到分析的 LLVM IR 代码中,以加载一个函数 func_A 地址,这些地址是使用 [=13= 在单独的函数 func_B 中分配的].示例如下。

define void @func_B() {
  %1 = alloca [1 x i32], align 4
}

define void @func_A() {
  // load the address allocated above here using IRBuilder in an analysis pass 
  // to the IR code, something like the following:
  // IRBuilder<> builder(); 
  // builder.CreateLoad(val);
}

我已经能够找到要加载的 llvm:value*,即上面示例中的 val,但问题是 val 是声明的本地标识符在 func_B 中并且在 func_A.[=27= 中加载时可能与 func_A 的标识符冲突(比如 func_A 也声明了一个名为 %1 的本地标识符) ]

如何加载 func_B%1 而不会在 func_A 中发生冲突?请注意,我不能将 %1 作为函数参数传递给 func_A,因为我不想更改任何 IR 代码,而是添加一些加载指令。

如有任何帮助,我们将不胜感激!

显然,你不能那样做。在下面的示例中,就好像您想从 bar() 访问 int a

int foo()
{
  int a = 5;
}

int bar()
{
  ...
}

由于 %1 是在堆栈上分配的,它的内存在函数 func_B 完成时被释放,因此它甚至可能在 func_A 执行期间不存在。

你唯一能做的就是将%1的值存储到func_B中的全局变量中,并在func_A中加载它:

@var = [1 x i32] zeroinitializer

define void @func_B() {
  %1 = alloca [1 x i32], align 4
  store %1, @var
}

define void @func_A() {
  // load the address allocated above here using IRBuilder in an analysis pass 
  // to the IR code, something like the following:
  // IRBuilder<> builder(); 
  // builder.CreateLoad(val);
  %1 = load @var
}