Avoiding gcc function prologue overhead?

后端 未结 5 1726
逝去的感伤
逝去的感伤 2021-02-13 21:57

I\'ve lately encountered a lot of functions where gcc generates really bad code on x86. They all fit a pattern of:

if (some_condition) {
    /* do something real         


        
5条回答
  •  说谎
    说谎 (楼主)
    2021-02-13 22:38

    Update

    To explicitely suppress inlining for a single function in gcc, use:

    void foo() __attribute__ ((noinline))
    {
      ...
    }
    

    See also How can I tell gcc not to inline a function?


    Functions like this will regularly be inlined automatically unless compiled -O0 (disable optimization).

    In C++ you can hint the compiler using the inline keyword

    If the compiler won't take your hint you are probably using too many registers/branches inside the function. The situation is almost certainly resolved by extracting the 'complicated' block into it's own function.


    Update i noticed you added the fact that they are extern symbols. (Please update the question with that crucial info). Well, in a sense, with external functions, all bets are off. I cannot really believe that gcc will by definition inline all of a complex function into a tiny caller simply because it is only called from there. Perhaps you can give some sample code that demonstrates the behaviour and we can find the proper optimization flags to remedy that?

    Also, is this C or C++? In C++ I know it is common place to include the trivial decision functions inline (mostly as members defined in the class declaration). This won't give a linkage conflict like with simple (extern) C functions.

    Also you can have template functions defined that will inline perfectly in all compilation modules without resulting in link conflicts.

    I hope you are using C++ because it will give you a ton of options here.

提交回复
热议问题