[LLVMdev] How to correlate LLVA with native ISA

John Criswell
2008-12-08T20:28:35Z

Keun Soo Yim wrote:
>
>  Hi,
>
>  How to correlate the LLVM IR-leve instructions and memory values
>  with the machine instructions and memory locations?
Can you tell us what goal you are trying to accomplish that requires you
to do this?  There might be better ways of doing what you want.

The answer to your question probably depends on whether you're trying to
write a pure LLVM analysis/transform, a JIT, or can interpose at the
static code generator.
>
>  For example, if CMP instruction in machine ISA is selected for
> the ICMP instruction in LLVA,
>  with the Instruction datastructure for ICMP, is it possible to get
> the memory address of CMP instruction? Assume that the code segment
> base address is given.
 From working strictly with the LLVM IR, I don't believe this is
possible.  There is no instruction that can give you the address of an
LLVM instruction.  There are multiple reasons for this: first, it would
allow one to write code that branches into the middle of basic blocks,
making LLVM's analysis passes much more tedious to write.  Second,
instructions may be expanded to multiple machine instructions or
peephole optimized away into 0 instructions during code generation.

If you're willing to work with the LLVM static code generator or JIT
infrastructure, then things might be different.  The code generator may
have knowledge of the correlation between LLVM IR instructions and
native code instructions; you may be able to enhance it to get the
information you need.
>
>  Similarly, by implementing an LLVM IR-level pass, is it feasible to
> get the runtime memory address
>  of a LLVM IR-level variable in global area? Assume the data segment
> base address is given.
You should be able to find the address of anything that is link-time
accessible: these include externally visible global variables and
externally visible functions.  The name of a global variable is its
location in physical memory.  Memory allocated by alloca and malloc are
also guaranteed to be "real" memory locations; the value of the alloca
or malloc is the location within real memory.

It's not possible, however, to get the address of an SSA virtual
register.  The code generator is free to put these into spill locations
on the stack or into physical machine registers; in fact, the code
generator can put an SSA value into different registers at different
points in the function.

You can do things like writing a transform that will take selected SSA
registers and change them into alloca'ed or malloc'ed memory (or even
global variables).  It will hurt performance, but it will allow you to
get a pointer to the real memory location in which they're stored.

>
>  In the LLVM library, there are already some classes starting with
> Machine but I was not able to find
>  any existing methods that would give the above information.
These are used for code generation.  Again, you may be able to do more
fancy things at (static or dynamic) code generation time, but in pure
LLVM IR transforms, your options are somewhat limited.

-- John T.

>
>  Thanks in advance.
>
>  Best,
>  Keun Soo
>
>