Is there a way to tell gcc that a function that has side effects should only be called once if two subsequent calls have the same arguments. I want the following behaviour:
foo(6);//run this function
foo(6);//optimize this away
foo(6);//optimize this away
foo(5);//run this function
foo(6);//run this function again
I can make foo
check a global variable before it does any work, but that is less than optimal.
void inline foo(int i){
static int last_i=i+1;
if(last_i != i){
last_i==i;
//do_work...
}
}
Since foo
is an inline function the compiler should be able to look across invokations of foo()
and see that it doesn't have to execute it. The problem is the compiler can't optimize like that for global variables, is there a way to let the compiler know it is safe to do so?