Transforming SwitchInst to BranchInst

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Transforming SwitchInst to BranchInst

Zhoulai
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
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Transforming SwitchInst to BranchInst

Pete Cooper
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 <[hidden email]> 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
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Transforming SwitchInst to BranchInst

Chen Li
In reply to this post by Zhoulai

On Jul 24, 2015, at 10:46 AM, Zhoulai <[hidden email]> wrote:

Hi,

Are there some built-in LLVM transformation pass, or written library code that transforms LLVM::SwitchInst into if-condition statements (LLVM:: BranchInst)?

There is a -lowerswitch pass that rewrites switch instructions with a sequence of branches. 

thanks,
chen




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
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev