<font>I used <a href="http://llvm.org/demo">llvm.org/demo</a> to generate IR code from c code. <br>And I found that: when "return " statement appears several times in different conditional block, IR code does not genrate one "ret " instruction for each return statement,<br>
it just put a phi node instruction at the end of the function. Just like this:<br>int factorial(int X) {<br> if (X <100)<br> X*=3;<br>else <br> X += 1;<br>return X + 3;<br>}<br>the IR code genrated would be as follow:</font><br>
<pre><span><span class="llvm_keyword">define</span> <span class="llvm_type">i32</span> @factorial(<span class="llvm_type">i32</span> %X) <span class="llvm_keyword">nounwind</span> uwtable <span class="llvm_keyword">readnone</span> {
%1 = <span class="llvm_keyword">icmp</span> <span class="llvm_keyword">slt</span> <span class="llvm_type">i32</span> %X, 100
<span class="llvm_keyword">br</span> <span class="llvm_type">i1</span> %1, <span class="llvm_type">label</span> %2, <span class="llvm_type">label</span> %4
; <<span class="llvm_type">label</span>>:2 ; preds = %0
%3 = <span class="llvm_keyword">mul</span> nsw <span class="llvm_type">i32</span> %X, 3
<span class="llvm_keyword">br</span> <span class="llvm_type">label</span> %6
; <<span class="llvm_type">label</span>>:4 ; preds = %0
%5 = <span class="llvm_keyword">add</span> nsw <span class="llvm_type">i32</span> %X, 1
<span class="llvm_keyword">br</span> <span class="llvm_type">label</span> %6
; <<span class="llvm_type">label</span>>:6 ; preds = %4, %2
%.0 = <span class="llvm_keyword">phi</span> <span class="llvm_type">i32</span> [ %3, %2 ], [ %5, %4 ]
%7 = <span class="llvm_keyword">add</span> nsw <span class="llvm_type">i32</span> %.0, 3
<span class="llvm_keyword">ret</span> <span class="llvm_type">i32</span> %7
}<br>Is there any reason or rull to do like this, to use one phi instruction and only one ret instruction?<br>As I am trying to generate llvm IR code for my language, can I put each each ret at each conditional block like follows?<br>
<br></span><span>; <<span class="llvm_type">label</span>>:2 ; preds = %0
%3 = <span class="llvm_keyword">mul</span> nsw <span class="llvm_type">i32</span> %X, 3<br> ret %3<br> ; <<span class="llvm_type">label</span>>:4 ; preds = %0
%5 = <span class="llvm_keyword">add</span> nsw <span class="llvm_type">i32</span> %X, 1
ret %5</span>
<br></pre><br>