<!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>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 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 instruction<BR>
>  return lval;<BR>
> }<BR>
><BR>
> What I have suggested to my students is to omit the (non-existent) copy instruction<BR>
> and use the "rval" above as a replacement for all future occurrences of "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 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>
</FONT>
</P>

</BODY>
</HTML>