[LLVMdev] Transforming SwitchInst to BranchInst

Pete Cooper peter_cooper at apple.com
Fri Jul 24 11:05:23 PDT 2015


Hi Zhoulai

If you just don’t want any switch statements, then take a look at LowerSwitch.cpp.  I think it will do what you want.

You can also invoke it with ‘opt -lowerswitch ...’.

Thanks,
Pete
> On Jul 24, 2015, at 10:46 AM, Zhoulai <zell08v at gmail.com> wrote:
> 
> Hi, 
> 
> Are there some built-in LLVM transformation pass, or written library code that transforms LLVM::SwitchInst into if-condition statements (LLVM:: BranchInst)?
> 
> The purpose of the transformation is that we have a legacy program analyzer that includes an LLVM pass manipulating if-condition statements. Statements of LLVM::SwithchInst should have been handled in the same manner but was not done. Thus to transform these SwitchInst to if-condition looks a viable alternative for us.  
> 
> To illustrate, I give a simple C snippet with 'switch' and the expected transformation.
> ---------------------------------------- 
> Original program: 
> 
>  char grade;
>  ...
>   switch(grade)
>     {
>     case 'A' :
>       printf("Excellent!\n" );
>       break;
>     case 'B' :
>       printf("Well done\n" );
>       break;
>   
>     default :
>       printf("Invalid grade\n" );
>     }
> 
> may be transformed to something like
> 
> if (grad=='A') 
>           printf("Excellent!\n" );
> else if (grad=='B')
>       printf("Well done\n" );
> else 
>        printf("Invalid grade\n" );
> -------------------------------------------------
>  
> Are you aware of such a transformation pass ? Thanks.
> Zhoulai
> _______________________________________________
> 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/20150724/c7053060/attachment.html>


More information about the llvm-dev mailing list