填充LLVM克隆函数VMAP

填充LLVM克隆函数VMAP,llvm,compiler-optimization,llvm-c++-api,Llvm,Compiler Optimization,Llvm C++ Api,我想编写一些代码,在给定LLVM函数F的情况下,在同一个模块中创建一个精确的副本(以便稍后可以在保留原始副本的同时对副本进行操作)。我想用CloneFunctionInto方法来实现这一点 我当前的尝试包括尝试将每个(新arg、旧arg)对插入到VMap中。之前,我尝试过插入一个未初始化的VMap,并将其反过来放置。令人印象深刻的是,所有3个都产生了完全相同的错误消息: 断言'VMap.count(&I)&&“未指定源参数的映射!”“失败 //F和S在代码中定义得更高 FunctionType*

我想编写一些代码,在给定LLVM函数F的情况下,在同一个模块中创建一个精确的副本(以便稍后可以在保留原始副本的同时对副本进行操作)。我想用CloneFunctionInto方法来实现这一点

我当前的尝试包括尝试将每个(新arg、旧arg)对插入到VMap中。之前,我尝试过插入一个未初始化的VMap,并将其反过来放置。令人印象深刻的是,所有3个都产生了完全相同的错误消息:

断言'VMap.count(&I)&&“未指定源参数的映射!”“失败

//F和S在代码中定义得更高
FunctionType*FType=F->getFunctionType();
函数*new_F=cast(M->getOrInsertFunction(S,FType));
std::向量argtype;
ValueToValueMapTy VMap;
函数::arg_迭代器old_args=F->arg_begin();
对于(函数::arg_迭代器new_args=new_F->arg_begin(),new_args_end=new_F->arg_end();new_args!=new_args_end;new_args++){
std::pair pair(&*新参数,&*旧参数);
VMap.插入(对);
如果(VMap.count(&*新参数)>0){

errs()。write_escaped(“Mapping added”)当您只想克隆函数时,可以使用
CloneFunction
而不是
CloneFunction into

另外,
CloneFunction
向您展示了如何处理用于克隆的
ValueToValueMap

CloneFunction.cpp

00223 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
00224                               bool ModuleLevelChanges,
00225                               ClonedCodeInfo *CodeInfo) {
00226   std::vector<Type*> ArgTypes;
00227 
00228   // The user might be deleting arguments to the function by specifying them in
00229   // the VMap.  If so, we need to not add the arguments to the arg ty vector
00230   //
00231   for (const Argument &I : F->args())
00232     if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
00233       ArgTypes.push_back(I.getType());
00234 
00235   // Create a new function type...
00236   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
00237                                     ArgTypes, F->getFunctionType()->isVarArg());
00238 
00239   // Create the new function...
00240   Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
00241 
00242   // Loop over the arguments, copying the names of the mapped arguments over...
00243   Function::arg_iterator DestI = NewF->arg_begin();
00244   for (const Argument & I : F->args())
00245     if (VMap.count(&I) == 0) {     // Is this argument preserved?
00246       DestI->setName(I.getName()); // Copy the name over...
00247       VMap[&I] = &*DestI++;        // Add mapping to VMap
00248     }
00249 
00250   if (ModuleLevelChanges)
00251     CloneDebugInfoMetadata(NewF, F, VMap);
00252 
00253   SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
00254   CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
00255   return NewF;
00256 }
00223函数*llvm::克隆函数(const函数*F、ValueToValueMapTy和VMap、,
00224布尔模块电平变化,
00225克隆代码信息*代码信息){
00226标准::向量ArgTypes;
00227
00228//用户可能正在删除函数的参数,方法是在
00229//VMap。如果是这样,我们不需要将参数添加到参数向量
00230   //
00231 for(常量参数&I:F->args())
00232 if(VMap.count(&I)==0)//是否尚未将参数映射到任何对象?
00233 ArgTypes.push_back(I.getType());
00234
00235//创建新的函数类型。。。
00236 FunctionType*FTy=FunctionType::get(F->getFunctionType()->getReturnType(),
00237 ArgTypes,F->getFunctionType()->isVarArg());
00238
00239//创建新函数。。。
00240函数*NewF=Function::Create(FTy,F->getLinkage(),F->getName());
00241
00242//在参数上循环,在参数上复制映射参数的名称。。。
00243函数::arg_迭代器DestI=NewF->arg_begin();
00244 for(常量参数&I:F->args())
00245如果(VMap.count(&I)==0){//此参数是否保留?
00246 DestI->setName(I.getName());//将名称复制到。。。
00247 VMap[&I]=&*DestI++;//将映射添加到VMap
00248     }
00249
00250如果(模块级别变化)
00251克隆数据库元数据(NewF、F、VMap);
00252
00253 SmallVector返回;//忽略克隆的返回。
00254克隆函数(NewF、F、VMap、ModuleLevelChanges、Returns、“、CodeInfo);
00255返回NewF;
00256 }

当您只想克隆一个函数时,可以使用
CloneFunction
而不是
CloneFunctionInto

另外,
CloneFunction
向您展示了如何处理用于克隆的
ValueToValueMap

CloneFunction.cpp

00223 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
00224                               bool ModuleLevelChanges,
00225                               ClonedCodeInfo *CodeInfo) {
00226   std::vector<Type*> ArgTypes;
00227 
00228   // The user might be deleting arguments to the function by specifying them in
00229   // the VMap.  If so, we need to not add the arguments to the arg ty vector
00230   //
00231   for (const Argument &I : F->args())
00232     if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
00233       ArgTypes.push_back(I.getType());
00234 
00235   // Create a new function type...
00236   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
00237                                     ArgTypes, F->getFunctionType()->isVarArg());
00238 
00239   // Create the new function...
00240   Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
00241 
00242   // Loop over the arguments, copying the names of the mapped arguments over...
00243   Function::arg_iterator DestI = NewF->arg_begin();
00244   for (const Argument & I : F->args())
00245     if (VMap.count(&I) == 0) {     // Is this argument preserved?
00246       DestI->setName(I.getName()); // Copy the name over...
00247       VMap[&I] = &*DestI++;        // Add mapping to VMap
00248     }
00249 
00250   if (ModuleLevelChanges)
00251     CloneDebugInfoMetadata(NewF, F, VMap);
00252 
00253   SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
00254   CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
00255   return NewF;
00256 }
00223函数*llvm::克隆函数(const函数*F、ValueToValueMapTy和VMap、,
00224布尔模块电平变化,
00225克隆代码信息*代码信息){
00226标准::向量ArgTypes;
00227
00228//用户可能正在删除函数的参数,方法是在
00229//VMap。如果是这样,我们不需要将参数添加到参数向量
00230   //
00231 for(常量参数&I:F->args())
00232 if(VMap.count(&I)==0)//是否尚未将参数映射到任何对象?
00233 ArgTypes.push_back(I.getType());
00234
00235//创建新的函数类型。。。
00236 FunctionType*FTy=FunctionType::get(F->getFunctionType()->getReturnType(),
00237 ArgTypes,F->getFunctionType()->isVarArg());
00238
00239//创建新函数。。。
00240函数*NewF=Function::Create(FTy,F->getLinkage(),F->getName());
00241
00242//在参数上循环,在参数上复制映射参数的名称。。。
00243函数::arg_迭代器DestI=NewF->arg_begin();
00244 for(常量参数&I:F->args())
00245如果(VMap.count(&I)==0){//此参数是否保留?
00246 DestI->setName(I.getName());//将名称复制到。。。
00247 VMap[&I]=&*DestI++;//将映射添加到VMap
00248     }
00249
00250如果(模块级别变化)
00251克隆数据库元数据(NewF、F、VMap);
00252
00253 SmallVector返回;//忽略克隆的返回。
00254克隆函数(NewF、F、VMap、ModuleLevelChanges、Returns、“、CodeInfo);
00255返回NewF;
00256 }