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

Jevin Sweval jevinsweval at gmail.com
Fri Mar 28 11:27:09 PDT 2014


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



More information about the llvm-dev mailing list