Hi, I'm somehow a newby with llvm, I've been working with it for 4 months. I'm trying to convert a function to 'main' in order to execute this concrete function only when the program is run. To do so, I've been able to remove all other functions from the module, and change the function name to "main". I've then allocated new registers for all the formal parameters in the function definition, and update all the subsequent references to the parameters to the allocated values. (I don't care if the correct value is not passed to the function). To continue, I need to remove the arguments of the function. Here is where I start having problems. I've tried to do so in three different ways: * Create a new function "main" without arguments and copy all basic-block's from the original function to the new one. I've had problems with this approach because of cloning not updating operators of the basic-block instructions. Thanks to http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-June/051063.html I've solved this partially, but I'm still facing problems when updating branch instructions. * Since all parameters are now useless, I've tried to pass the -deadargelim optimization pass with opt -deadargelim < file-1.bc > file-2.bc. However, the optimization pass does not realize by itself that the function parameters are not being used, and does not eliminate them. * I've tried delete( function->arg_begin() ); with a (somehow predicted) segmentation fault. * I've read http://llvm.org/docs/doxygen/html/DeadArgumentElimination_8cpp_source.html to try to understand how deadargelim eliminates the unused arguments of a function, but I'm not able to fully understand the code. Which one is the best approach to remove the unused arguments of the function?. I'm stuck here. Thanks a lot !! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131003/69850a6a/attachment.html>
Hi Pablo,> * Create a new function "main" without arguments and copy all basic-block's > from the original function to the new one. I've had problems with this > approach because of cloning not updating operators of the basic-block > instructions. Thanks toThis is the approach I'd take. It sounds like you may have discovered it already, but there's a function "CloneFunctionInto" which should be able to do everything for you. What I'd do is create a new skeleton function "main". Populate it with your new register definitions that will take over from the arguments. So main would then look like: define i32 @main() { %arg1 = alloca float ... %argN = alloca i8 } Then call CloneFunctionInto, telling it about these %argX registers and how they map to the original function's arguments. With luck everything should be copied and updated properly for you. Cheers. Tim.
This may be a really naive suggestion (I'm sure I know even less about LLVM than you), but could you just create a wrapper function called main that calls the function you want to be main? Surely there's a pass that would remove pointless calls like that, which would give you the result you're asking for. On Oct 3, 2013 11:38 AM, "Pablo González de Aledo" <pablo.aledo at gmail.com> wrote:> Hi, I'm somehow a newby with llvm, I've been working with it for 4 months. > > I'm trying to convert a function to 'main' in order to execute this > concrete function only when the program is run. To do so, I've been able to > remove all other functions from the module, and change the function name to > "main". > > I've then allocated new registers for all the formal parameters in the > function definition, and update all the subsequent references to the > parameters to the allocated values. (I don't care if the correct value is > not passed to the function). > > To continue, I need to remove the arguments of the function. Here is where > I start having problems. I've tried to do so in three different ways: > > * Create a new function "main" without arguments and copy all > basic-block's from the original function to the new one. I've had problems > with this approach because of cloning not updating operators of the > basic-block instructions. Thanks to > http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-June/051063.html I've > solved this partially, but I'm still facing problems when updating branch > instructions. > * Since all parameters are now useless, I've tried to pass > the -deadargelim optimization pass with opt -deadargelim < file-1.bc > > file-2.bc. However, the optimization pass does not realize by itself that > the function parameters are not being used, and does not eliminate them. > * I've tried delete( function->arg_begin() ); with a (somehow predicted) > segmentation fault. > * I've read > http://llvm.org/docs/doxygen/html/DeadArgumentElimination_8cpp_source.html to > try to understand how deadargelim eliminates the unused arguments of a > function, but I'm not able to fully understand the code. > > Which one is the best approach to remove the unused arguments of the > function?. I'm stuck here. > > Thanks a lot !! > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131004/08d9e52b/attachment.html>
Seemingly Similar Threads
- [LLVMdev] opt -globaldce -deadargelim yields different result then when run separately
- Replacing a function from one module into another one
- Replacing a function from one module into another one
- Bug in pass 'ipsccp' on function attribute 'argmemonly'?
- Bug in pass 'ipsccp' on function attribute 'argmemonly'?