Optimizing GCC Returns

I would like to know if GCC can optimize code like

int foo(args) {
    if(is_true) {
        do_smth;
        n = call_func(args);
        do_smth;
        return n;
    }
    else {
        return call_func(args);
    }
}

      

so if I enter another call_func branch, the call will be executed as if there was no call to foo? I am writing a kernel module and to solve one specific problem I need to make it look like call_func is called directly. To be more specific, call_func is a system call. Foo is my version of this system call. In the is_true case, I have to do smth., Call this system call and return it back. But in! Is_true I would like to somehow change the call stack itself so that at the current level there was call_func instead of foo. Is it possible?

Is_true implementation:

struct task_struct * cur_task = current;
if(check_dir(cur_task)) {
...
}

      

check_dir is a function to check if we want to do something with the directory from which the system call was called.

+3


source to share


1 answer


This is called tail- call optimization (it is not specified in any C standard, on the contrary, some languages ​​-eg Scheme and Ocaml- indicate that tail call optimization is required). In some cases, the latest GCC compilers can optimize this .

But it really depends on the details, in particular the actual arguments passed to call_func

If you depend on it, comment out your code and check with the help gcc -fverbose-asm -O2 -S

that your compiler does it.



Note that this optimization is not required and can be compiler, compile flag, processor, and ABI .

(so that it can run on x86-64 but not 32 bit ia32 or ARM, you really need to check!)

+3


source







All Articles