<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Mar 27, 2014 at 9:30 AM, Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">> That's my idea, yes. I'm not sure how Clang would transform the named<br>

> registers into the intrinsic, but something along the lines of:<br>
><br>
> i8* @SP = "SP";<br>
><br>
> define void @step() nounwind {<br>
> entry:<br>
>   %0 = call i32 @llvm.read_register(i8* @SP)<br>
>   %1 = add i32 %0, i32 4<br>
>   call void @llvm.write_register(i8* @SP, %1)<br>
> }<br>
><br>
> declare void @llvm.write_register(i8*, i32) nounwind readnone<br>
> declare i32 @llvm.read_register(i8*) nounwind readnone<br>
<br>
</div>I would not produce any llvm global for it. So some insanity like<br>
<br>
register long a asm("rsp");<br>
long f(long x) {<br>
  long ret = a;<br>
  a = x;<br>
  return ret;<br>
}<br>
<br>
would compile to<br>
<br>
define i64 @f(i64 %x) {<br>
  %ret = call i64 @llvm.read_register("rsp");<br>
  call void @llvm.write_register("rsp", i64 %x)<br>
  ret %ret<br>
}<br>
declare void @llvm.write_register(i8*, i64)<br>
declare i64 @llvm.read_register(i8*)</blockquote><div><br></div><div>I don't think that works. Per the GCC documentation, a global register variable reserves the register entirely for use with that name in a translation unit. We don't seem to want exactly that model, but the approach you're suggesting doesn't seem to capture the semantics. For instance:</div>
<div><br></div><div>register long a asm("r12");</div><div>void f(long n) {</div><div>  n *= 3;</div><div>  a += n;</div><div>}</div><div><br></div><div>... could do the wrong thing if the multiplication happens to use r12.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
>> This is not exactly the semantics gcc uses since the register would<br>
>> still be allocatable, but should cover 99% of the uses, including<br>
>> reading the stack pointer in the kernel.<br>
><br>
> <a href="http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html" target="_blank">http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html</a><br>
><br>
> It seems that the semantics is to avoid PCS registers, or they will be<br>
> clobbered...<br>
<br>
</div>Yes, it is really odd. It says "Global register variables reserve<br>
registers throughout the program.", which is obviously not the case<br>
since not all compile units might see it.<br>
<div class=""><br>
><br>
>> For example, is it legal to move the read of rsp out of  a<br>
>> loop?<br>
><br>
> No. It should be a volatile read/write.<br>
<br>
</div>Agreed. With the intrinsic the semantics are easy to represent.<br>
<br>
Cheers,<br>
Rafael<br>
<div class=""><div class="h5">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br></div></div>