[LLVMdev] Question about the IR code of conditional flow
John Criswell
criswell at illinois.edu
Mon Apr 16 07:24:13 PDT 2012
On 4/15/12 11:51 PM, 胡渐飞 wrote:
> I used llvm.org/demo <http://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:
The code is most likely being transformed by an LLVM pass which modifies
each function to have a single return instruction.
Several compiler algorithms assume that a function has a single exit;
the transform changes a function to meet this assumption.
As Eli has already stated, you can put as many returns in your functions
as you like (as long as they're at the end of basic blocks).
-- John T.
> 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 uwtablereadnone {
> %1 =icmp slt i32 %X, 100
> br i1 %1,label %2,label %4
>
> ;<label>:2 ; preds = %0
> %3 =mul nswi32 %X, 3
> br label %6
>
> ;<label>:4 ; preds = %0
> %5 =add nswi32 %X, 1
> br label %6
>
> ;<label>:6 ; preds = %4, %2
> %.0 =phi i32 [ %3, %2 ], [ %5, %4 ]
> %7 =add nswi32 %.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 nswi32 %X, 3
> ret %3
> ;<label>:4 ; preds = %0
> %5 =add nswi32 %X, 1
> ret %5
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120416/76e823d3/attachment.html>
More information about the llvm-dev
mailing list