[LLVMdev] Pinning registers in LLVM

David Terei davidterei at gmail.com
Sun Jun 28 23:00:18 PDT 2009


Hi all,

I'm working on using LLVM as a back-end for an existing compiler (GHC  
Haskell compiler) and one of the problems I'm having is pinning a  
global variable to a actual machine register. I've seen mixed  
terminology for this feature/idea, so what I mean by this is that I  
want to be able to put a global variable into a specified hardware  
register. This declaration should thus reserve that machine register  
for exclusive use by this global variable. This is used in GHC since  
it defines an abstract machine as part of its execution model, with  
this abstract machine consisting of several virtual registers. Due to  
the frequency the virtual registers are accessed it is best for  
performance that they be permanently assigned to a physical machine  
register.

GCC supports an extension to enable this feature, which is described  
here:

http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Global-Reg-Vars.html

A very simple example C program using this feature:

--------------------------
#include <stdio.h>

register int R1 __asm__ ("esi");

int main(void)
{
	R1 = 3;
	printf("register: %d\n", R1);
	R1 *= 2;
	printf("register: %d\n", R1);
	return 0;
}
--------------------------

llvm-gcc doesn't compile this program correctly, although according to  
the llvm-gcc release notes this extension was first supported by llvm- 
gcc in 1.9.

gcc output:

     register: 3
     register: 6

llvm-gcc output:

     register: 3
     register: 16330

So just hoping this can be done with LLVM and if so, then how.

Cheers,
David Terei



More information about the llvm-dev mailing list