GCC返回优化布置 [英] GCC Return optimiztion

查看:112
本文介绍了GCC返回优化布置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果GCC可以优化code像

  INT美孚(参数){
    如果(is_true){
        do_smth;
        N = call_func(参数);
        do_smth;
        返回N;
    }
    其他{
        返回call_func(参数);
    }
}

所以,如果我在else分支call_func的电话很会像没有富调用执行?我在写内核模块,并为解决一个特定的问题,我需要它看起来像这样call_func被直接调用。为了更具体call_func是一些系统调用。富是我的版本的系统调用。在is_true情况下,我应该做的不便,请拨打本系统调用,然后返回它的回报。 !但在is_true我想以某种方式改变调用栈本身 - 因此,在目前的水平还有的call_func而不是富。它甚至有可能?

is_true的执行情况:

 结构的task_struct * cur_task =电流;
如果(check_dir(cur_task)){
...
}

check_dir是函数来检查,如果我们想要做的事与目录,从系统调用被调用。


解决方案

这就是所谓尾调用优化(它的的任何C标准规定;与此相反,一些语言-eg计划及Ocaml-指定尾调用优化是需要发生)。在部分的情况下,近期 GCC编译器能够做到这一点的优化

但它确实取决于细节,尤其是传递给 call_func

的实际参数

如果你依赖于它,请评论你的code和 GCC -fverbose-ASM -O2 -S 你的编译器是这样做的。

注意,这个优化是不是必需的,可能是编译器,编译标志,处理器和 ABI 具体。

(因此它可以在x86-64的工作,但不能在32位IA32或ARM;!你真的应该检查)

I'd 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 that if i'm in else branch call_func's call would be executed like there was no foo call? I'm writing kernel module, and for solving one particular issue I need it to look like this call_func was called directly. To be more specific call_func is some system call. Foo is my version of this system call. In is_true case I should do smth, call this system call and then return its return. But in !is_true I'd like to somehow change the call stack itself - so that on current level there was call_func instead of foo. Is it even possible?

Implementation of is_true:

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

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

解决方案

This is called tail-call optimization (it is not specified in any C standard; in contrast, some languages -e.g. Scheme and Ocaml- specify that tail-call optimization is required to happen). In some cases, recent GCC compilers can do that optimization.

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

If you depend on it, please comment your code and check with gcc -fverbose-asm -O2 -S that your compiler is doing that.

Notice that this optimization is not required, and might be compiler, compilation flags, processor and ABI specific.

(so it could work on x86-64 but not on 32 bits ia32 or ARM; you really should check!)

这篇关于GCC返回优化布置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆