Specifying registers for local variables

Hi,

Does LLVM/Clang support specifying registers for local variables?

The following code is accepted by the Clang. It parses the `asm`
attribute in the `Parser::ParseAsmAttributesAfterDeclarator()` routine
and checks the declaration in the `Sema::ActOnVariableDeclarator()`.
But it looks like the asm label expression is not used anywhere
further. Is this language extension is unsupported by the Clang?

% cat check.c
int foo() {
  register int v asm ("edx") = 3;
  v = v + 1;
  return v;
}

The assembler output demonstrates the problem:

% gcc -S check.c && cat check.s
...
        movl $3, %edx ; use register
        movl %edx, %eax
        addl $1, %eax
        movl %eax, %edx
        movl %edx, %eax
...

% clang -S check.c && cat check.s
...
        movl $3, -4(%rbp) ; use stack
        movl -4(%rbp), %eax
        addl $1, %eax
        movl %eax, -4(%rbp)
        movl -4(%rbp), %eax
...

Hi,

Does LLVM/Clang support specifying registers for local variables?

In some way, yes. We implement what is documented by gcc:

    Local register variables in specific registers do not reserve the
     registers, except at the point where they are used as input or
     output operands in an 'asm' statement and the 'asm' statement
     itself is not deleted.

That is, it is just a way of writing a asm constraint.

From: cfe-dev-bounces@cs.uiuc.edu [mailto:cfe-dev-bounces@cs.uiuc.edu] On
Behalf Of Rafael Espíndola
Sent: 19 December 2014 15:29
To: Simon Atanasyan
Cc: Clang Developers List
Subject: Re: [cfe-dev] Specifying registers for local variables

> Hi,
>
> Does LLVM/Clang support specifying registers for local variables?

In some way, yes. We implement what is documented by gcc:

    Local register variables in specific registers do not reserve the
     registers, except at the point where they are used as input or
     output operands in an 'asm' statement and the 'asm' statement
     itself is not deleted.

That is, it is just a way of writing a asm constraint.

I'm told that GCC makes a few undocumented promises too (I'm also told that clang/llvm is unlikely to support all of them). The one that affects this example is the promise that a read of the uninitialized variable is lowered to a read of the specified register. I've just spoken to Behan Webster and he tells me that Renato added support for this kind of declaration but that it only works for the stack pointer.

Just to provide the context: The mips portion of the linux kernel uses code similar to Simon's example to read the thread context pointer ($28). Clang currently handles this as unreachable code and removes large sections of the kernel boot sequence.

I think it will work with any register as long as you use a global variable.

Cheers,
Rafael