[LLVMdev] Question about the IR code of conditional flow

Eli Friedman eli.friedman at gmail.com
Sun Apr 15 22:24:03 PDT 2012


On Sun, Apr 15, 2012 at 9:51 PM, 胡渐飞 <hujianfei258 at gmail.com> wrote:
> 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?

Yes, feel free to generate ret instructions wherever they seem
appropriate; there isn't any restriction on the number of ret
instructions in a function.

-Eli




More information about the llvm-dev mailing list