[cfe-dev] [LLVMdev] Named register variables GNU-style

Rafael EspĂ­ndola rafael.espindola at gmail.com
Thu Mar 27 09:30:46 PDT 2014


> That's my idea, yes. I'm not sure how Clang would transform the named
> registers into the intrinsic, but something along the lines of:
>
> i8* @SP = "SP";
>
> define void @step() nounwind {
> entry:
>   %0 = call i32 @llvm.read_register(i8* @SP)
>   %1 = add i32 %0, i32 4
>   call void @llvm.write_register(i8* @SP, %1)
> }
>
> declare void @llvm.write_register(i8*, i32) nounwind readnone
> declare i32 @llvm.read_register(i8*) nounwind readnone

I would not produce any llvm global for it. So some insanity like

register long a asm("rsp");
long f(long x) {
  long ret = a;
  a = x;
  return ret;
}

would compile to

define i64 @f(i64 %x) {
  %ret = call i64 @llvm.read_register("rsp");
  call void @llvm.write_register("rsp", i64 %x)
  ret %ret
}
declare void @llvm.write_register(i8*, i64)
declare i64 @llvm.read_register(i8*)

>> This is not exactly the semantics gcc uses since the register would
>> still be allocatable, but should cover 99% of the uses, including
>> reading the stack pointer in the kernel.
>
> http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html
>
> It seems that the semantics is to avoid PCS registers, or they will be
> clobbered...

Yes, it is really odd. It says "Global register variables reserve
registers throughout the program.", which is obviously not the case
since not all compile units might see it.

>
>> For example, is it legal to move the read of rsp out of  a
>> loop?
>
> No. It should be a volatile read/write.

Agreed. With the intrinsic the semantics are easy to represent.

Cheers,
Rafael



More information about the cfe-dev mailing list