changing llvm::Function signature after code generation, before last CreateRet

烂漫一生 提交于 2019-12-08 06:48:00

问题


I'm trying to implement the following functionality;

  • a function with no explicit return will by default return the last evaluation in the last executed block

So, currently the process i'm doing is

1) create a Function

llvm::Function* result = llvm::Function::Create(Compiler::Detail::getAnonymousFunctionSignature(llvmContext),
                            llvm::GlobalValue::ExternalLinkage,
                            name,
                            module());
                    result->setCallingConv( llvm::CallingConv::C );

2) add blocks and evaluations to the blocks

builder.createFoo.....

However, only in the second phase i have the llvm::Value* (and compile-time type) that i want to use by default as return value. The problem is that i need to use this type to determine the signature of the created function

Question:

how do i solve the problem?

  • is possible to change the signature after the function is created? is it legal?
  • do i need to create a new function with the updated signature and copy/assign the entry block of the first function to it and thats it? or do i need to reevaluate all the expressions?
  • is possible to not create the function before code generation? if it is so, at what point should i create the function?

a code example of how to achieve this would be awesome. thanks!


回答1:


You cannot change function signature, because this will mean that it will have different Type (and thus you will need to update all the users, etc.; this procedure in most cases cannot be done automagically).

There are multiple possible solutions, for example, you can create the function with the updated signature, then use the functions from lib/Transforms/Utils/CloneFunction.cpp to copy the function body and then hack on the return type.




回答2:


A better solution exists than CloneFunctionInto(), according to https://stackoverflow.com/a/18751365/2024042:

NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());

Where NF is the new function you're cloning into and F is the old function that you have just cloned.



来源:https://stackoverflow.com/questions/5369790/changing-llvmfunction-signature-after-code-generation-before-last-createret

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!