I used llvm.org/demo to generate IR code from c code.
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,
it just put a phi node instruction at the end of the function. Just like this:
int factorial(int X) {
if (X <100)
X*=3;
else
X += 1;
return X + 3;
}
the IR code genrated would be as follow:
define i32 @factorial(i32 %X) nounwind uwtable readnone {
%1 = icmp slt i32 %X, 100
br i1 %1, label %2, label %4
; <label>:2 ; preds = %0
%3 = mul nsw i32 %X, 3
br label %6
; <label>:4 ; preds = %0
%5 = add nsw i32 %X, 1
br label %6
; <label>:6 ; preds = %4, %2
%.0 = phi i32 [ %3, %2 ], [ %5, %4 ]
%7 = add nsw i32 %.0, 3
ret i32 %7
}
Is there any reason or rull to do like this, to use one phi instruction and only one ret instruction?
As I am trying to generate llvm IR code for my language, can I put each each ret at each conditional block like follows?
; <label>:2 ; preds = %0
%3 = mul nsw i32 %X, 3
ret %3
; <label>:4 ; preds = %0
%5 = add nsw i32 %X, 1
ret %5