c-在函数LLVM中创建局部变量

在llvm :: Module中,有2个有趣的字段:

typedef SymbolTableList<Function> FunctionListType;
typedef SymbolTableList<GlobalVariable> GlobalListType;

GlobalListType GlobalList;      ///< The Global Variables in the module
FunctionListType FunctionList;  ///< The Functions in the module

因此,如果我们要定义一些函数或全局变量,我们将可以在程序的任何其他位置使用它们,而只需向我们的模块询问它们即可.但是函数局部变量呢?如何定义它们?

解决方法:

局部变量在运行时通过alloca分配.

要创建AllocaInst,您需要

llvm::BasicBlock::iterator I = ...
const llvm::Type *Ty = 
auto AI = new AllocaInst(Ty, 0, Name, I);

要在函数中查找alloca,您需要遍历指令:

for (auto I = F->begin(), E = F->end(); I != E; ++I) {
  for (auto J = I->begin(), E = I->end(); J != E; ++J) {
    if (auto AI = dyn_cast<AllocaInst>(*J)) {
      ..
    }
  }
}
上一篇:c#-LLVM绑定中的不平衡堆栈警告


下一篇:LLVM’s New Versioning Scheme