C++ 在函数LLVM中创建局部变量

C++ 在函数LLVM中创建局部变量,c++,llvm,C++,Llvm,在llvm::Module中有两个有趣的字段: typedef SymbolTableList<Function> FunctionListType; typedef SymbolTableList<GlobalVariable> GlobalListType; GlobalListType GlobalList; ///< The Global Variables in the module FunctionListType FunctionList;

llvm::Module
中有两个有趣的字段:

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

GlobalListType GlobalList;      ///< The Global Variables in the module
FunctionListType FunctionList;  ///< The Functions in the module
typedef SymbolTableList函数listType;
typedef SymbolTableList GlobalListType;
GlobalListType GlobalList;//<模块中的全局变量
FunctionListType FunctionList;//<模块中的函数

因此,如果我们将定义一些函数或全局变量,我们将能够从程序的任何其他位置使用它们,只需向模块请求它们。但是函数局部变量呢?如何定义它们

局部变量在运行时通过
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)) {
      ..
    }
  }
}
for(自动I=F->begin(),E=F->end();I!=E;++I){
对于(自动J=I->begin(),E=I->end();J!=E;++J){
if(自动AI=动态铸造(*J)){
..
}
}
}

恐怕没有明确而简短的答案,因此我只能让您参考以下文章,从中您可以了解有关LLVM IR的更多信息:,。因此,我分配变量并将它们推送到basicblock(由函数拥有)。我做了一些操作并返回值,我应该从这个allocas中释放内存吗?正如我正确理解的,函数是由基本块组成的,它是由指令组成的,不是吗?@koshachok“我应该从这个allocas中释放内存吗?”-不,当函数退出时,它会被释放。“正如我正确理解的那样,函数是由基本块组成的,它是由指令组成的,不是吗?”-是的,没错。