<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>RE: [LLVMdev] copy instructions</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>That's cool. The students grok the SSA thing fine -- especially since all the var's<BR>
in the language we're translating from are immutable (functional style) which makes SSA fairly<BR>
straight forward; the phi instruction is needed in a few places where branching occurs.<BR>
<BR>
Wayne O. Cochran<BR>
Assistant Professor Computer Science<BR>
wcochran@vancouver.wsu.edu<BR>
<BR>
<BR>
<BR>
-----Original Message-----<BR>
From: Reid Kleckner [<A HREF="mailto:reid.kleckner@gmail.com">mailto:reid.kleckner@gmail.com</A>]<BR>
Sent: Sat 4/23/2011 11:28 AM<BR>
To: Cochran, Wayne Owen<BR>
Cc: Eli Friedman; llvmdev@cs.uiuc.edu<BR>
Subject: Re: [LLVMdev] copy instructions<BR>
<BR>
The mem2reg pass will rewrite allocas and loads and stores to SSA<BR>
virtual registers. Essentially it's a transformation from non-SSA to<BR>
SSA form. That said, I don't know if you want your students to<BR>
implement their own SSA transformation.<BR>
<BR>
Reid<BR>
<BR>
On Sat, Apr 23, 2011 at 1:57 PM, Cochran, Wayne Owen<BR>
<wcochran@vancouver.wsu.edu> wrote:<BR>
> It is my understanding, the alloca memory routines are used<BR>
> for forcing variables to be allocated on the stack frame -- which<BR>
> you would want for source level debugging.<BR>
> When SSA registers are used, LLVM will decide what goes into<BR>
> registers and what will spill over to the stack frame.<BR>
> I want the latter.<BR>
> --w<BR>
><BR>
> Wayne O. Cochran<BR>
> Assistant Professor Computer Science<BR>
> wcochran@vancouver.wsu.edu<BR>
><BR>
><BR>
><BR>
> -----Original Message-----<BR>
> From: Eli Friedman [<A HREF="mailto:eli.friedman@gmail.com">mailto:eli.friedman@gmail.com</A>]<BR>
> Sent: Fri 4/22/2011 5:53 PM<BR>
> To: Cochran, Wayne Owen<BR>
> Cc: llvmdev@cs.uiuc.edu<BR>
> Subject: Re: [LLVMdev] copy instructions<BR>
><BR>
> On Fri, Apr 22, 2011 at 10:40 AM, Wayne Cochran<BR>
> <wcochran@vancouver.wsu.edu> wrote:<BR>
>> This is a simple SSA code generation 101 question.<BR>
>><BR>
>> If I follow the IR code generation techniques in the Dragon book the<BR>
>> statement<BR>
>> x = y + z<BR>
>> would translate into something like this in SSA/LLVM<BR>
>> %0 = add %y, %z<BR>
>> %x = %0<BR>
>> Obviously "copy instructions" like %foo = %bar are senseless in SSA<BR>
>> since %foo and %bar are immutably fixed to the same value and there<BR>
>> is no need for two aliases for the same thing (this is my own observation,<BR>
>> please tell me if my thinking is off).<BR>
>><BR>
>> What are the general code generation techniques to avoid "copy<BR>
>> instructions"?<BR>
>><BR>
>> For example, the simple code generation methods that yield the translation<BR>
>> above might look like the following:<BR>
>><BR>
>> Value *AddExpression::codeGen() {<BR>
>> Value *l = left->codeGen();<BR>
>> Value *r = right->codeGen();<BR>
>> Value *result = new TempValue; // get unique temporary<BR>
>> emit(result->str() + " add " + l->str() + ", " r-str());<BR>
>> return result;<BR>
>> }<BR>
>><BR>
>> Value *assignExpression::codeGen() {<BR>
>> Value *rval = rvalue->codeGen();<BR>
>> Value *lval = new NameValue(ident);<BR>
>> emit(lval->str() + " = " + rval->str()); // emit (silly) copy<BR>
>> instruction<BR>
>> return lval;<BR>
>> }<BR>
>><BR>
>> What I have suggested to my students is to omit the (non-existent) copy<BR>
>> instruction<BR>
>> and use the "rval" above as a replacement for all future occurrences of<BR>
>> "ident."<BR>
>> i.e., something like the following:<BR>
>><BR>
>> Value *assignExpression::codeGen() {<BR>
>> Value *rval = rvalue->codeGen();<BR>
>> update symbol table so that all future reference to "ident" are replaced<BR>
>> with rval<BR>
>> return rval;<BR>
>> }<BR>
>><BR>
>> Using this scheme, the following<BR>
>> x = y + z<BR>
>> u = x * y + foo(x)<BR>
>> would be translated into<BR>
>> %0 = add %y, %z<BR>
>> %1 = mul %0, %y<BR>
>> %2 = call foo(%0)<BR>
>> %3 = add %1, %2<BR>
>><BR>
>><BR>
>> Is there a more obvious approach to avoiding "copy instructions"?<BR>
><BR>
> The recommended approach to generating LLVM IR is simply not to try to<BR>
> generate code in SSA form; see<BR>
> <A HREF="http://llvm.org/docs/tutorial/LangImpl7.html#memory">http://llvm.org/docs/tutorial/LangImpl7.html#memory</A> .<BR>
><BR>
> -Eli<BR>
><BR>
><BR>
> _______________________________________________<BR>
> LLVM Developers mailing list<BR>
> LLVMdev@cs.uiuc.edu <A HREF="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</A><BR>
> <A HREF="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</A><BR>
><BR>
><BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>