[LLVMdev] CFG using LLVM

Hersh.S. Iyer coolhersh at gmail.com
Mon Nov 16 06:28:49 PST 2009


I used successors to find the basic blocks that can be visited and those
that cannot be reached.
My pass just prints out those blocks which can be reached. The problem is
that I want to include
this in my compiler code rather than as a separate .cpp file which will
perform the pass when I use 'opt'.
I have seen that there is something called a PassManager class. Will this
help me any way ?
Basically, I want to run this pass on the functions and generate the
optimized .ll file.
Right now, the .ll file is generated without optimizations. Also, is there a
way that I can make
LLVM generate unreachable basic blocks ? I need to test my pass and not able
to find suitable examples.

Regards,
Hersh

On Sat, Nov 14, 2009 at 2:53 PM, John Criswell <criswell at uiuc.edu> wrote:

> Hersh.S. Iyer wrote:
>
>> Hi John,
>>
>> Thanks for the reply.
>> I read the document and was able to write simple passes too.
>> I used members of CFG.H to find the successor and predecessor of the
>> blocks.
>> So this way I get to know which blocks are reachable and which are not.
>> All this said and done, if a block is not reachable, can I delete it?
>>
> In general, yes, I believe you can delete it, unless you want to optimize
> programs that utilize undefined behavior (e.g., a program that manufactures
> function pointers).
>
> There are more aggressive algorithms that perform constant propagation
> along with dead code elimination; this can reveal basic blocks that are
> reachable through conditional branches but for which the condition can be
> proven constant if dead code is ignored.
>
>> The function pass goes over each function in the .ll code and finds basic
>> blocks in them. So
>> all I need to do is to create a new .ll file which is same as the old one
>> except that the dead
>> blocks are gone.
>> The inbuilt passes won't work as I need to write my own pass.
>>
> Why do you need to write your own pass?  Passes can be executed one after
> another, so if you pass needs to do something else besides remove dead basic
> blocks, you run the dead code elimination pass first and then your pass
> after it.
>
> -- John T.
>
>  Your help is appreciated.
>>
>> Thanks,
>> Hersh
>>
>> On Sat, Nov 14, 2009 at 10:14 AM, John Criswell <criswell at uiuc.edu<mailto:
>> criswell at uiuc.edu>> wrote:
>>
>>    Hersh.S. Iyer wrote:
>>
>>        Hi,
>>
>>        I am a new user of LLVM. I am using it as the IR for a
>>        compiler for a subset of LUA.
>>        I have the .ll file ready and it executes fine when passed to
>>        the llvm interpreter.
>>
>>    Welcome!
>>
>>
>>        Now, I wish to perform a few optimizations to the code
>>        starting with dead code elimination.
>>        For this I would need the CFG. I am very new to all of this
>>        stuff. Please help me out guys.
>>        The way I want to proceed is to start at the top of the CFG
>>        and then find out the blocks
>>        reachable from here. At the end, if any basic block remains
>>        unreachable, then I would
>>        classify it as dead code.
>>
>>    LLVM already has at least two dead-code elimination passes (-dce
>>    and -adce); I believe it also has a pass to remove dead basic
>>    blocks.  These can be run using the opt tool and can be
>>    incorporated into any tool that you're building.  They're
>>    documented at http://llvm.org/docs/Passes.html.  Is there a reason
>>    these won't work for your purpose?
>>
>>    If you do need to write your own pass, you'll notice that the CFG
>>    of a function is explicit in the IR: each basic block ends with a
>>    terminator instruction which explicitly lists the successor basic
>>    blocks to which control flow can move.  Assuming that you write a
>>    function pass (which is given a Function & reference as input),
>>    you can find the entry basic block by using the getEntryBlock()
>>    method and then follow the CFG by finding the basic block's
>>    terminator instruction (using the getTerminator() method) and then
>>    finding the successors listed in the terminator instruction (using
>>    the getSuccessor() method).
>>
>>    You'll find doxygen (http://llvm.org/doxygen/hierarchy.html)
>>    invaluable in looking up what methods each class has and how to
>>    use them.
>>
>>    Have you read the Programmer's Manual and the "How to Write an
>>    LLVM Pass" document?
>>
>>    -- John T.
>>
>>
>>        I read a few things about llvm passes and felt this could be
>>        made a pass. Please let me
>>        know what's the right and fastest way to do this.
>>
>>        Regards,
>>        Hersh
>>
>>
>>
>>
>>
>> --
>> HSIyer
>>
>
>


-- 
HSIyer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091116/9a8a552b/attachment.html>


More information about the llvm-dev mailing list