[cfe-dev] Specifying registers for local variables

Daniel Sanders Daniel.Sanders at imgtec.com
Fri Dec 19 13:17:23 PST 2014


> -----Original Message-----
> From: cfe-dev-bounces at cs.uiuc.edu [mailto:cfe-dev-bounces at 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
> 
> On 18 December 2014 at 10:12, Simon Atanasyan <simon at atanasyan.com>
> wrote:
> > 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.

> > 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
> > ...
> >
> > --
> > Simon
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list