填充LLVM CloneFunction VMAP [英] Filling the LLVM CloneFunction VMAP
问题描述
我想编写一些代码,给定LLVM函数F,该代码在同一模块中创建一个精确的副本(因此可以在以后保留原始副本的情况下操纵该副本).我想用CloneFunctionInto方法做到这一点.
I want to write some code that, given an LLVM function F, creates an exact copy in the same module (so the copy can be manipulated later while preserving the original). I want to do this with the CloneFunctionInto method.
我当前的尝试涉及尝试将每个(新arg,旧arg)对插入VMap.以前,我曾尝试插入未初始化的VMap,然后将其反过来放置.令人印象深刻的是,所有这三个都导致了完全相同的错误消息:
My current attempt involves trying to insert each (new arg,old arg) pair into the VMap. Previously I've tried inserting an uninitialised VMap and putting the pair the other way round. Impressively, all 3 have resulted in the exact same error message:
断言`VMap.count(& I)&& 未指定从源参数的映射!"失败.
Assertion `VMap.count(&I) && "No mapping from source argument specified!"' failed.
//F and S are defined higher up in the code
FunctionType *FType = F->getFunctionType();
Function *new_F = cast<Function>(M->getOrInsertFunction(S,FType));
std::vector<Type*> ArgTypes;
ValueToValueMapTy VMap;
Function::arg_iterator old_args = F->arg_begin();
for (Function::arg_iterator new_args = new_F->arg_begin(), new_args_end = new_F->arg_end();new_args != new_args_end; new_args++) {
std::pair<Value*,Value*> pair(&*new_args,&*old_args);
VMap.insert(pair);
if (VMap.count(&*new_args)>0) {
errs().write_escaped("Mapping added") << '\n';
}
old_args++;
}
SmallVector<ReturnInst*, 8> Returns;
CloneFunctionInto(new_F, F, VMap, false, Returns, "_new", 0, 0);
在使用中,已添加映射"消息被打印正确的次数(即每个参数一次),所以我真的不确定错误在哪里.
In use, the 'mapping added' message is printed the correct number of times (i.e. once for each argument), so I'm really unsure where the error is.
推荐答案
当您只想克隆一个函数时,可以使用CloneFunction
代替CloneFunctionInto
.
You can use CloneFunction
instead of CloneFunctionInto
when you just want to clone a function.
还CloneFunction
向您展示了如何处理ValueToValueMap
进行克隆:
Also CloneFunction
shows you how to handle a ValueToValueMap
for cloning:
来自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 }
这篇关于填充LLVM CloneFunction VMAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!