[LLVMdev] CFG of a function

Cristianno Martins cristiannomartins at gmail.com
Mon Jun 10 07:47:39 PDT 2013


Hi Rasha,

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.

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.

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.

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 ;)

PS: I'm adding the llvm-dev list back into the recipients, since someone
else could also find this informations useful.

Cheers,



--
Cristianno Martins
PhD Student of Computer Science
University of Campinas
cmartins at ic.unicamp.br
<cristiannomartins at hotmail.com>


On Mon, Jun 10, 2013 at 7:00 AM, Rasha Omar <rasha.sala7 at gmail.com> wrote:

> Thank you for your reply
> Actually, I did these three steps. Moreover, the step of eraseFromParent()
>       ClonedFunction->eraseFromParent();
> in the last step in the pass.
> However, I tried to copy each basic block with its successors and the
> result is good.
> I want to ask you another question. How could I compare the basic block in
> the OriginalFunction with the basic block in the ClonedFunction?
> If I have basic block bb from ClonedFunction and I need to find it in the
> OriginalFunction, how to do that?
> Thank you again for your help
>
>
>
> On 10 June 2013 02:28, Cristianno Martins <cristiannomartins at gmail.com>wrote:
>
>> 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?
>>
>> 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).
>>
>> Anyways, the code needed to clone that function should be something like
>>
>> // clone the function, but does not add it to any module
>>
>> clonedFunction = CloneFunction(originalFunction, VMap, false);
>>
>> // since this copy will not be called from outside this module,
>> overwrites whatever linkage mode the original had (also cloned) to internal
>>
>> clonedFunction->setLinkage(GlobalValue::InternalLinkage);
>>
>> // finally, inserts the clone into the same module where originalFunction
>> is.
>>
>>
>> originalFunction->getParent()->getFunctionList().push_back(clonedFunction);
>>
>>
>> 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.
>> Can you see any difference from the way you called that function?
>>
>>
>>
>> --
>> Cristianno Martins
>> PhD Student of Computer Science
>> University of Campinas
>> cmartins at ic.unicamp.br
>>  <cristiannomartins at hotmail.com>
>>
>>
>> On Sun, Jun 9, 2013 at 6:06 AM, Rasha Omar <rasha.sala7 at gmail.com> wrote:
>>
>>> Firstly, Thank you for your reply.
>>> 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
>>> 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.
>>> Thank you for your real help
>>>
>>>
>>> On 8 June 2013 05:16, Cristianno Martins <cristiannomartins at gmail.com>wrote:
>>>
>>>> Hello Rasha,
>>>>
>>>> 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.
>>>>
>>>> Hope this can be helpful,
>>>>
>>>>
>>>> --
>>>> Cristianno Martins
>>>> PhD Student of Computer Science
>>>> University of Campinas
>>>> cmartins at ic.unicamp.br
>>>>  <cristiannomartins at hotmail.com>
>>>>
>>>>
>>>> On Fri, Jun 7, 2013 at 2:43 PM, Rasha Omar <rasha.sala7 at gmail.com>wrote:
>>>>
>>>>> But I don't want to map only basic blocks, I need too to map the edges
>>>>> "the whole CFG of the function"
>>>>> 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
>>>>> Thank you for help and patience
>>>>>
>>>>>
>>>>> On 6 June 2013 10:59, Alexandru Ionut Diaconescu <
>>>>> alexandruionutdiaconescu at gmail.com> wrote:
>>>>>
>>>>>> I don't use a function for do the mapping, it may be 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.
>>>>>>
>>>>>> Good luck
>>>>>>
>>>>>>
>>>>>> On Thu, Jun 6, 2013 at 10:55 AM, Rasha Omar <rasha.sala7 at gmail.com>wrote:
>>>>>>
>>>>>>> I think I understood that, but what I mean is what is the function
>>>>>>> responsible to do mapping is it MapValue() in ValueMapper.h?
>>>>>>>
>>>>>>> Thanks for your help
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 6 June 2013 09:54, Alexandru Ionut Diaconescu <
>>>>>>> alexandruionutdiaconescu at gmail.com> wrote:
>>>>>>>
>>>>>>>> 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.
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wed, Jun 5, 2013 at 11:03 PM, Rasha Omar <rasha.sala7 at gmail.com>wrote:
>>>>>>>>
>>>>>>>>> What do you mean by mapping to integers?
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On 5 June 2013 22:32, Alexandru Ionut Diaconescu <
>>>>>>>>> alexandruionutdiaconescu at gmail.com> wrote:
>>>>>>>>>
>>>>>>>>>> Why you don't map the basic blocks to integers and apply
>>>>>>>>>> algorithms on the integer graph? And construct your new CFG.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On Wed, Jun 5, 2013 at 10:27 PM, Rasha Omar <
>>>>>>>>>> rasha.sala7 at gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>>> How could I keep the function CFG in another memory space.
>>>>>>>>>>> I want to be able to change one of the old CFG , but keeping the
>>>>>>>>>>> original one in another space.
>>>>>>>>>>> Thanks
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> *Rasha Salah Omar
>>>>>>>>>>> Msc Student at E-JUST
>>>>>>>>>>> Demonestrator  at Faculty of Computers and Informatics
>>>>>>>>>>> Benha University
>>>>>>>>>>> *
>>>>>>>>>>>
>>>>>>>>>>> _______________________________________________
>>>>>>>>>>> LLVM Developers mailing list
>>>>>>>>>>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>>>>>>>>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> Best regards,
>>>>>>>>>> Alexandru Ionut Diaconescu
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> *Rasha Salah Omar
>>>>>>>>> Msc Student at E-JUST
>>>>>>>>> Demonestrator  at Faculty of Computers and Informatics
>>>>>>>>> Benha University
>>>>>>>>> *
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Best regards,
>>>>>>>> Alexandru Ionut Diaconescu
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> *Rasha Salah Omar
>>>>>>> Msc Student at E-JUST
>>>>>>> Demonestrator  at Faculty of Computers and Informatics
>>>>>>> Benha University
>>>>>>> *
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Best regards,
>>>>>> Alexandru Ionut Diaconescu
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> *Rasha Salah Omar
>>>>> Msc Student at E-JUST
>>>>> Demonestrator  at Faculty of Computers and Informatics
>>>>> Benha University
>>>>> *
>>>>>
>>>>> _______________________________________________
>>>>> LLVM Developers mailing list
>>>>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>> *Rasha Salah Omar
>>> Msc Student at E-JUST
>>> Demonestrator  at Faculty of Computers and Informatics
>>> Benha University
>>> *
>>>
>>
>>
>
>
> --
> *Rasha Salah Omar
> Msc Student at E-JUST
> Demonestrator  at Faculty of Computers and Informatics
> Benha University
> *
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130610/247040c2/attachment.html>


More information about the llvm-dev mailing list