<div dir="ltr">Hi Rasha,<div><br></div><div style>First, you should not erase that cloned function XD the idea is that your pass should create that new function, add it to a module (that could be the same one where the original function is, or another one that you could be creating if that is the case), and then just let it be: the pass manager should identify this new function (after your pass returned) and all the other analysis and transformations should be called for this new function you created (even your own pass could be called again for this new function, depending on the type of you pass). If you call eraseFromParent, then all the instruction, BB's, arguments, and everything you cloned will be deleted from memory, and it'll just seem like nothing was cloned.</div>

<div style><br></div><div style>Ok, now in order to identify all the values between the two copies of the functions you need to use that ValueToValueMap that you created and used as an argument for cloneFunction. This VMap can be seen as a hash of values, and everything in the cloned function is mapped to something in the original function inside this map. So, if you need to identify an equivalent BB between them, for example, you could use VMap[BBfromOrigFunc] to access the pointer to the equivalent BB in you clone.</div>

<div style><br></div><div style>You can address any value using this map, but only using a original function's value as the key for VMap. So, you must be careful and not applying any transformations on your original function, unless you don't need the VMap anymore.</div>

<div style><br></div><div style>Also, you will probably need to use llvm::cast or llvm::dyn_cast to get the right type for your references, since every reference in a ValueToValueMap is a Value ;)</div><div style><br></div>

<div style>PS: I'm adding the llvm-dev list back into the recipients, since someone else could also find this informations useful.</div><div style><br></div><div style>Cheers,</div><div style><br></div></div><div class="gmail_extra">

<br clear="all"><div><br>--<br>Cristianno Martins<br>PhD Student of Computer Science<br>University of Campinas<br><a href="mailto:cmartins@ic.unicamp.br" target="_blank">cmartins@ic.unicamp.br</a><br><a href="mailto:cristiannomartins@hotmail.com" target="_blank"></a></div>


<br><br><div class="gmail_quote">On Mon, Jun 10, 2013 at 7:00 AM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div><div>Thank you for your reply<br>Actually, I did these three steps. Moreover, the step of eraseFromParent()<br>      ClonedFunction->eraseFromParent();<br></div>in the last step in the pass.<br></div>


<div>However, I tried to copy each basic block with its successors and the result is good.<br></div><div>I want to ask you another question. How could I compare the basic block in the OriginalFunction with the basic block in the ClonedFunction?<br>


</div><div>If I have basic block bb from ClonedFunction and I need to find it in the OriginalFunction, how to do that?<br></div><div>Thank you again for your help<br></div><div><div><div><br></div></div></div></div><div class="HOEnZb">

<div class="h5"><div class="gmail_extra">
<br><br><div class="gmail_quote">On 10 June 2013 02:28, Cristianno Martins <span dir="ltr"><<a href="mailto:cristiannomartins@gmail.com" target="_blank">cristiannomartins@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr">Well, that's odd. Some time ago I applied the cloneFunction function in one of my passes, but it was just for a test case. So, I'm not actually using it right now, but I guess this specific piece of code is not that different from then. Ok, so I will try to take you all the way through using this function as I did, and you can check if there is any difference between my approach and yours, ok?<div>




<br></div><div>First, I have in originalFunction the pointer of the function that I want to clone. clonedFunction will be my pointer to the new clone of the originalFunction, and VMap is a ValueToValueMap, that is used by the cloneFunction to remap the references on each instruction and basic-block of the function (you see, when you clone an instruction, the values used by that clone are the same ones on the original, because the clone() function from llvm::Instruction does not clone its uses, just the instruction itself).</div>




<div><br></div><div>Anyways, the code needed to clone that function should be something like</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font color="#93c47d">// clone the function, but does not add it to any module</font></div>




<div><p>clonedFunction = CloneFunction(originalFunction, VMap, <span>false</span>);</p></div><div><p><font color="#93c47d">// since this copy will not be called from outside this module, overwrites whatever linkage mode the original had (also cloned) to internal</font></p>




</div><div><p>clonedFunction->setLinkage(GlobalValue::InternalLinkage);</p></div><div><p><font color="#93c47d">// finally, inserts the clone into the same module where originalFunction is.</font></p>

</div><div><p>originalFunction->getParent()->getFunctionList().push_back(clonedFunction);</p></div></blockquote><div>







<p><br></p><p>After this three steps, I had in clonedFunction an exact copy of all instructions and, as a consequence, the same CFG from the original one.</p></div><div>Can you see any difference from the way you called that function?</div>




<div><br></div></div><div class="gmail_extra"><div><br clear="all"><div><br>--<br>Cristianno Martins<br>PhD Student of Computer Science<br>University of Campinas<br><a href="mailto:cmartins@ic.unicamp.br" target="_blank">cmartins@ic.unicamp.br</a><br>




<a href="mailto:cristiannomartins@hotmail.com" target="_blank"></a></div>
<br><br></div><div><div><div class="gmail_quote">On Sun, Jun 9, 2013 at 6:06 AM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div><div><div>Firstly, Thank you for your reply.<br></div>Secondly, I tried clone function before and it gave me some problems. I tried to use he clone function in the PartialInling.cpp and it gives me the same problems<br>





</div>The main problem in cloning that when i tried to print the cfg for the original function in a dot file it's printed with the basic block details, but the cloned one is printed the same shape of CFG with empty blocks. I'm not sure , but do you think that it may give me the real blocks or it's something dummy. I'm trying this function from a long time. If you could help me to reach to the real CFG I'll be so grateful for you.<br>





</div>Thank you for your real help<br></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On 8 June 2013 05:16, Cristianno Martins <span dir="ltr"><<a href="mailto:cristiannomartins@gmail.com" target="_blank">cristiannomartins@gmail.com</a>></span> wrote:<br>





<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello Rasha,<div><br></div><div>I think what you want you can do in LLVM by creating a copy of the whole function you are trying to modify, and applying your transformations only on this new copy. You can easily do that using CloneFunction (a function defined in llvm/Transforms/Util/Cloning.h). An example of its use can be seen on the function unswitchFunction(Function* f), from lib/Transforms/IPO/PartialInlining.cpp file.</div>







<div><br></div><div>Hope this can be helpful,</div>







</div><div class="gmail_extra"><br clear="all"><div><br>--<br>Cristianno Martins<br>PhD Student of Computer Science<br>University of Campinas<br><a href="mailto:cmartins@ic.unicamp.br" target="_blank">cmartins@ic.unicamp.br</a><br>







<a href="mailto:cristiannomartins@hotmail.com" target="_blank"></a></div><div><div>
<br><br><div class="gmail_quote">On Fri, Jun 7, 2013 at 2:43 PM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div dir="ltr"><div><div>But I don't want to map only basic blocks, I need too to map the edges "the whole CFG of the function"<br></div>Save the CFG of the function in another memory address and call it for example orgCFG and change the CFG by referencing to the orgCFG<br>








</div><div>Thank you for help and patience <br></div></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On 6 June 2013 10:59, Alexandru Ionut Diaconescu <span dir="ltr"><<a href="mailto:alexandruionutdiaconescu@gmail.com" target="_blank">alexandruionutdiaconescu@gmail.com</a>></span> wrote:<br>








<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I don't use a function for do the mapping, it may be <span style="font-family:arial,sans-serif;font-size:13px">MapValue(). If it does not work, alias an int identifier for each basic block. Be aware because basic block cannot have the same name (getName) in the same function, but they might have the same name being in different functions. Therefore, take into account the function name as well.</span><div>









<span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Good luck</span></div></div><div><div><div class="gmail_extra">
<br><br><div class="gmail_quote">
On Thu, Jun 6, 2013 at 10:55 AM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">









<div dir="ltr"><div>I think I understood that, but what I mean is what is the function responsible to do mapping is it MapValue() in ValueMapper.h?<br><br></div><div>Thanks for your help<br></div><br></div><div>
<div><div class="gmail_extra">
<br><br><div class="gmail_quote">On 6 June 2013 09:54, Alexandru Ionut Diaconescu <span dir="ltr"><<a href="mailto:alexandruionutdiaconescu@gmail.com" target="_blank">alexandruionutdiaconescu@gmail.com</a>></span> wrote:<br>










<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Map every basic block from the CFG to a set of integers. The successors from the CFG can be used to make the edges in your simplified graph. The pair (Callee,Caller) can link the CFG-s between them in a larger CFG-like.</div>










<div><div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 5, 2013 at 11:03 PM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br>











<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">What do you mean by mapping to integers?<br></div><div><div><div class="gmail_extra">
<br><br><div class="gmail_quote">On 5 June 2013 22:32, Alexandru Ionut Diaconescu <span dir="ltr"><<a href="mailto:alexandruionutdiaconescu@gmail.com" target="_blank">alexandruionutdiaconescu@gmail.com</a>></span> wrote:<br>












<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Why you don't map the basic blocks to integers and apply algorithms on the integer graph? And construct your new CFG.<br>












</div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Wed, Jun 5, 2013 at 10:27 PM, Rasha Omar <span dir="ltr"><<a href="mailto:rasha.sala7@gmail.com" target="_blank">rasha.sala7@gmail.com</a>></span> wrote:<br>













</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div><div>How could I keep the function CFG in another memory space.<br>
</div>I want to be able to change one of the old CFG , but keeping the original one in another space.<br>
</div>Thanks<span><font color="#888888"><br clear="all"><div>
<div><div><div><div><div><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">














<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div></div></div></div></div></div></font></span></div>
<br></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><span><font color="#888888"><br><br clear="all"><br>-- <br><font style="color:rgb(153,153,153)">Best regards,</font><br style="color:rgb(153,153,153)"><font style="color:rgb(153,153,153)">Alexandru Ionut Diaconescu</font><br>














</font></span></div>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">












<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><font style="color:rgb(153,153,153)">Best regards,</font><br style="color:rgb(153,153,153)">
<font style="color:rgb(153,153,153)">Alexandru Ionut Diaconescu</font><br>
</div>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">










<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><font style="color:rgb(153,153,153)">Best regards,</font><br style="color:rgb(153,153,153)">
<font style="color:rgb(153,153,153)">Alexandru Ionut Diaconescu</font><br>
</div>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">








<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div>
</div></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">





<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><b><span style="color:rgb(0,0,102)">Rasha Salah Omar</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Msc Student at E-JUST</span><br style="color:rgb(0,0,102)">


<span style="color:rgb(0,0,102)">Demonestrator  at Faculty of Computers and Informatics</span><br style="color:rgb(0,0,102)"><span style="color:rgb(0,0,102)">Benha University</span><br></b></div>
</div>
</div></div></blockquote></div><br></div>