[LLVMdev] How to extract the starting address of each basic block with llvm?

Petsas Athanasios
Fri, 28 Mar 2014 20:04:37 -0000

Thank you very much Jevin for the suggestion!

But how I can use blockaddress function? Do I have to write a pass in order
to use it?
As I told you I am new in llvm. Could you provide me a guide or an example?

Thank you very much,
Thanasis


On Fri, Mar 28, 2014 at 8:27 PM, Jevin Sweval <jevinsweval at gmail.com>
wrote:

> On Fri, Mar 28, 2014 at 1:15 PM, Petsas Athanasios <petsas at
ics.forth.gr>
> wrote:
> > Dear all,
> >
> > I use the clang frontend to produce the control flow graph of a C
program
> > through
> > this command:
> > clang -Xclang -analyze -Xclang -analyzer-checker=debug.DumpCFG test.c
> >
> > The produced information contains all the basic blocks identified by
> labels,
> > e.g. B1, B2, etc. along with their predecessors and successors.
> >
> > Is there a way to get the starting address of each of these basic
block?
> > I would like to map the basic block labels produced by CFG  with the
> actual
> > corresponding basic block starting addresses.
> >
> > Could I use llvm to do this? From what I understand this issued is
> > unrelevant with
> > IR. Maybe I could get something from the machine code (MC). Perhaps
> > I have to write a pass for llvm backend. Maybe I could get this
> information
> > from
> > BranchFolder and IfConverter machine function passes:
> >
>
http://llvm.org/docs/WritingAnLLVMBackend.html#branch-folding-and-if-conversion
> > but I am not sure.
> >
> > I am new to llvm so I need some help,
> >
> > Thank you,
> >
>
> Check out blockaddress(@function, %block) as documented in:
> http://llvm.org/docs/LangRef.html#addresses-of-basic-blocks
>
> The documentation says it only has defined behavior when used with
> indirectbr, but I have been able to store the block address in a
> global variable that survives lowering and is the correct address.
>
>       GlobalVariable *GV = new GlobalVariable(M, bbPtr,
> /*isConstant=*/true,
>                                    GlobalValue::ExternalLinkage,
>                                    BlockAddress::get(bb), name);
>
> Beware, the IR BB may be optimized away when lowering to assembly. You
> can detect this case when blockaddress() returns 1.
>
> Cheers,
> Jevin
>



--
Thanasis Petsas
Distributed Computing Systems (DCS)
Institute of Computer Science (ICS/FORTH)
Heraklion, Crete
Greece (GR)

http://www.thanasispetsas.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20140328/b056536e/attachment.html>