<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'>Thank you John and Frits. I had been experimenting with the creation of a new function<div>and the moving of the BBs from the old one into the new one, but couldn't make it work.</div><div>The code Frits proposed works quite well, but if the original main defined argsc but not argsv (which</div><div>shouldn't happen but oh well, both clang and gcc let it through with a warning) then the</div><div>verification pass fails due to uses of the old argsc parameter being present in the new body.</div><div><br></div><div>oldArgsc->replaceAllUsesWith( newArgsc ) solves this problem completely.</div><div><br></div><div>Thank you again,</div><div>Gabriel</div><div><br><hr id="zwchr"><b>De: </b>"Frits van Bommel" <fvbommel@gmail.com><br><b>Para: </b>"John Criswell" <criswell@illinois.edu><br><b>CC: </b>"Gabriel Rodríguez" <grodriguez@udc.es>, llvmdev@cs.uiuc.edu<br><b>Enviados: </b>Lunes, 28 de Febrero 2011 17:23:17<br><b>Asunto: </b>Re: [LLVMdev] Extending FunctionType<br><br>On Mon, Feb 28, 2011 at 5:10 PM, John Criswell <criswell@illinois.edu> wrote:<br>> On 2/28/11 10:04 AM, Frits van Bommel wrote:<br>>> I don't think a full clone is necessary, since he wants to replace the<br>>> function. He only needs to create the new function and splice in the<br>>> body of the old one.<br>><br>> That is exactly what MakeFunctionClone() does.  It creates a new function<br>> and uses CloneFunctionInto() to create a copy of the instructions in the old<br>> function in the new function.  The old function can be removed afterward if<br>> desired.<br><br>I'm pretty sure MakeFunctionClone() actually *copies* basic blocks +<br>instructions instead of moving them over.<br>Splicing the basic block list into the new function should be much<br>more efficient, especially for large functions; it's probably<br>constant-time operation and shouldn't allocate any memory.<br><br>> I'm not sure if the code below would work.  I don't see a mechanism that<br>> updates instructions that use the old function's arguments to use the new<br>> function's arguments.<br><br>He's adding explicit argc and argv arguments to main(), implying the<br>old function didn't have any arguments. But for a general function<br>it'd be easy enough to call replaceAllUsesWith() on each old argument,<br>since there's no need to worry about preserving the integrity of the<br>old function.<br><br>>> Gabriel: look at Function::getBasicBlockList() and<br>>> iplist<>::splice(iterator, iplist). Something like<br>>>   Function *NewF = Function::Create(NewFnType, OldF->getLinkage());<br>>>   NewF->getBasicBlockList().splice(NewF->begin(),<br>>> OldF->getBasicBlockList());<br>>>   NewF->takeName(OldF);<br>>>   OldF->eraseFromParent();<br>>> is probably what you're looking for.<br>>> (Note: completely untested)<br><br>And I did put a disclaimer about correctness :).<br><br><br>Reading MakeFunctionClone() though, I do see some other things that I missed:<br>* A Module* argument to Function::Create (necessitating an empty ""<br>name argument) so it gets inserted in the module.<br>* Copying function attributes.<br>* Copying the calling convention.<br></div></div></body></html>