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

507 views
Skip to first unread message

Petsas Athanasios

unread,
Mar 28, 2014, 1:15:09 PM3/28/14
to llv...@cs.uiuc.edu
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,

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

http://www.thanasispetsas.com/

Jevin Sweval

unread,
Mar 28, 2014, 2:27:09 PM3/28/14
to Petsas Athanasios, LLVMdev List
On Fri, Mar 28, 2014 at 1:15 PM, Petsas Athanasios <pet...@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
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Petsas Athanasios

unread,
Mar 28, 2014, 4:04:37 PM3/28/14
to Jevin Sweval, LLVMdev List
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

Jevin Sweval

unread,
Mar 28, 2014, 6:57:33 PM3/28/14
to Petsas Athanasios, LLVMdev List
On Fri, Mar 28, 2014 at 4:04 PM, Petsas Athanasios <pet...@ics.forth.gr> wrote:
> 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

Well, a basic block doesn't have a concrete address until it is linked
by the linker. I think that you will have to write a pass that stores
the blockaddress of every BB to global variables like I mentioned in
my last mail. The perhaps you could use objdump + a script to dump the
addresses?
Reply all
Reply to author
Forward
0 new messages