mapping a virtual register to a specific address

Does LLVM allow mapping a virtual register to a specific memory location? If not, how do we extract information on where the virtual registers are mapped to?

Thanks,
Amruth

Does LLVM allow mapping a virtual register to a specific memory location? If not, how do we extract information on where the virtual registers are mapped to?

In general, you do not know where a virtual register is mapped, and the mapping is dependent on the back-end. It could be allocated or spilled onto the stack, or it may live entirely within a physical register (e.g. no address).

To elaborate on what Justin said, at the LLVM IR level, you don’t know where a virtual register will live after code generation (or even if it will live in the same “place;” the code generator is free to allocate it to a register for part of its life and a stack spill slot for another part of its life). You may be able to get this information if you write a MachineFunction pass or some other code that is run by LLVM after code generation and register allocation (because you can see the generated code). However, I don’t know enough about MachineFunction passes to be sure that it’s possible; that’s just where I’d look if I were you. – John T.

Thanks Justin. It was really helpful.

Can i use “alloca” instruction to allocate memory at a particular address in the stack? Is it possible to do that? As far as I know, alloca does not take any address as an operand.

The only control alloca gives you over the address is alignment. If you really need an address, you can do as John suggested and write a MachineFunctionPass that executes towards the end of codegen to see where in the stack it is allocated. Also keep in mind that there is not necessarily a one-to-one mapping from virtual register to physical register. An LLVM IR register can hold a complex type that will take multiple physical registers to hold.

If you want to get the address at run-time, you can also pass the alloca’d pointer to some user function:

%a = alloca i32
call void @foo(i32* %a) // Call foo with the stack pointer