Use case for function unswitching.

Dear All,

It would be great if anyone help me out in finding a use case similar to the following example.

attribute((noinline))
int f (int p) {
if (p == 10) { // if-1
return 0;
}
// Some statements here.
return 2;
}

int g(int p) {
return f(p);
}

Here, I would like to see if the compiler does a transformation, where the if statement (if-1) in the body of the function ‘f’ is removed from ‘f’ and pushed to the caller ‘g’ as follows. Please note that I have forced not to inline ‘f’.

int f (int p) {
// Some statements here.
return 2;
}

int g(int p) {
if (p == 10) { // if-1
return 0;
}
return f(p);
}

This is a simple example that I used just to demonstrate the optimization (we may call it as function switching). If you know about any other use cases, where similar transformation happens, please let me know. When I generated the llvm IR (with -O3) such a transformation was not observed.

Regards,

Hi Raghesh,

Partial inline has support for function unswitching, you can explore it.

Note: It does not handle the functions marked with no-inline attribute.

Regards,

Ashutosh