[LLVMdev] How could I get memory address for each basic block?

Brian R. Gaeke gaeke at uiuc.edu
Thu Aug 19 20:57:12 PDT 2004


Note: The way we currently map LLVM basic blocks to native code addresses
only works with the SparcV9 target.  It's turned on by passing the
"-march=sparcv9 -enable-maps" options to llc.

In the SparcV9 target, we rely on having a one-to-one correspondence
between MachineBasicBlocks and LLVM BasicBlocks. The MappingInfo.cpp pass
(in lib/Target/SparcV9) stores a table of tuples of the form

  <block number, starting machine instr, number of machine instrs>

in a global variable called FunctionBB, such that there is one tuple for
each basic block in each function.

Since each SparcV9 machine instruction is 4 bytes, the "starting machine
instr" and "number of machine instrs" can be trivially translated into
addresses, given that you know the address of the function, and assuming
that the "block number" uniquely identifies and orders both LLVM BasicBlocks
and SparcV9 MachineBasicBlocks within a function.

Now, Qiuyu can probably accomplish his stated task more simply: just
make the target-specific AsmPrinter pass output a global symbol for each
basic block.  In SparcV9AsmPrinter.cpp, for example, you'd add it to
SparcV9AsmPrinter::emitBasicBlock().  Then you could get the addresses
using nm(1) or dlsym(3).

But the question remains: why get the address of each basic block?
Is there some problem we're not solving at a higher level?

-Brian

> I'd be interested to hear how that's being done - if Brian would reply 
> to the list about it, I'd appreciate it.
> 
> On Aug 19, 2004, at 3:39 PM, Vikram S. Adve wrote:
> >The dynamic optimization project (an internal research project in our 
> >group) uses some way to map LLVM basic blocks to native code 
> >addresses.  If this is what you want, perhaps you can ask Brian Gaeke 
> >(gaeke at uiuc.edu) to give you some information about how that is done.
> >
> >On Aug 19, 2004, at 5:11 PM, Misha Brukman wrote:
> >>On Thu, Aug 19, 2004 at 01:13:42PM -0700, Zhang Qiuyu wrote:
> >>>On the pervious mail, I am trying to add label for each basic basic
> >>>because I think I could get address by using nm if nm can show the
> >>>address of each label, but it seems not . so do you guys have some
> >>>idea how to get address for each basic block? Thanks.
> >>
> >>LLVM does not have a notion of C-style 'labels' that you can address 
> >>and
> >>use as parameters.  LLVM labels are simply NAMES of basic blocks, and
> >>they are printed out as `labels'.
> >>
> >>You can 'set' a name for a BasicBlock, but that's not what you are
> >>looking for, please see my other email for more details.

-- 
gaeke at uiuc.edu




More information about the llvm-dev mailing list