title: What exactly PHI instruction does and how to use it in LLVM
body: <p>LLVM has <a href="http://llvm.org/docs/LangRef.html#i_phi" rel="noreferrer">phi</a> instruction with quite weird explanation:</p>
  <blockquote>
  <p><em>The 'phi' instruction is used to implement the φ node in the SSA graph representing the function.</em></p>
  </blockquote>
  <p>Typically it is used to implement branching. If I understood correctly, it is needed to make dependency analysis possible and in some cases it could help to avoid unnecessary loading. However it's still hard to understand what it does exactly.</p>
  <p>Kaleidoscope <a href="https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.html#llvm-ir-for-if-then-else" rel="noreferrer">example</a> explains it fairly nicely for <code>if</code> case. However it's not that clear how to implement logical operations like <code>&amp;&amp;</code> and <code>||</code>. If I type the following to <a href="http://llvm.org/demo/index.cgi" rel="noreferrer">online llvm</a> compiler:</p>
  <pre><code>void main1(bool r, bool y) {
      bool l = y || r;
  }
  </code></pre>
  <p>Last several lines completely confuse me:</p>
  <pre><code>; &lt;label&gt;:10                                      ; preds = %7, %0
  %11 = phi i1 [ true, %0 ], [ %9, %7 ]
  %12 = zext i1 %11 to i8
  </code></pre>
  <p>Looks like phi node produces a result which can be used. And I was under impression that phi node just defines from which paths values coming.</p>
  <p>Could someone explain what is a Phi node, and how to implement <code>||</code> with it?</p>