<div>Wayne,</div><div><br></div><div>The short answer is "copy instruction" (of which LLVM has none, outside of ADDs with 0, ORs with 0, etc.) never show up in SSA forms.  You would have to specifically force your compiler to generate such instructions.  I've never read the dragon book, but I expect such instruction sequences are there only for instructional purposes, not as anything a real compiler would ever generate.  The only place copy instructions should be inserted is in lowering PHI instructions - which is often heavily coupled with register operations.  </div>
<div><br></div><div>It looks as if you are giving exactly the right advice, as far as replacing the entry in the symbol table.  As far as I know, this is not just the most obvious way to do it, but the <i>only</i> way when generating SSA form.  The only time an assignment expression should involve generating a copy (move) is if you are directly emitting a linear IR (or machine code).  Java and .NET both do this.</div>
<div><br></div><div>Hope this helps.</div><div><br></div><div>-Joshua</div><br><div class="gmail_quote">On Fri, Apr 22, 2011 at 11:40 AM, Wayne Cochran <span dir="ltr"><<a href="mailto:wcochran@vancouver.wsu.edu">wcochran@vancouver.wsu.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">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>
--w<br>
<br>
Wayne O. Cochran<br>
Clinical Assistant Professor, Computer Science<br>
Washington State University Vancouver<br>
<a href="mailto:wcochran@vancouver.wsu.edu">wcochran@vancouver.wsu.edu</a><br>
<a href="http://ezekiel.vancouver.wsu.edu/~wayne" target="_blank">http://ezekiel.vancouver.wsu.edu/~wayne</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<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>
</blockquote></div><br>