Built-in function (when to enter)?

Built-in functions are just a request to compilers who insert the full text of a built-in function in every place in the code where this function is used.

But how does the compiler decide whether to embed it or not? What algorithm / mechanism does it use to solve?

Thanks,

Naveen

+6
c ++
source share
7 answers

Some common aspects:

  • Compiler option (debug builds are usually not built-in, and most compilers have options to override the built-in declaration to try to embed all or none)
  • suitable calling convention (e.g. varargs functions are usually not nested)
  • suitable for embedding: it depends on the size of the function, the frequency of the function call, the gain through the built-in and optimization settings (speed and size of the code). Often tiny functions have the greatest benefits, but a huge function can be built in if it is called only once.
  • built-in call depths and recursion settings

The third is probably the core of your question, but it really is a "specific heuristic for the compiler" - you need to check the compiler documents, but usually they do not give much guarantees. MSDN has some (limited) information for MSVC.

Besides trivialities (e.g. simple getters and very primitive functions), embedding as such is not very useful anymore. The call instruction cost has decreased, and branch prediction has improved significantly.

A great nesting opportunity is to remove code paths that the compiler knows will not be taken - as an extreme example:

inline int Foo(bool refresh = false) { if (refresh) { // ...extensive code to update m_foo } return m_foo; } 

A good compiler will be built in Foo(false) , but not Foo(true) .

When generating the communication time code, Foo can be in .cpp (without an inline declaration), and Foo(false) will still be embedded, so again the built-in has only minor effects.


To summarize: There are several scenarios in which you should try to perform manual inlining control by placing (or excluding) the built-in statements.

+4
source share

Everything I know about built-in functions (and many other C ++ stuff) is here .

Also, if you focus on the heuristics of each compiler to decide whether to use an implementation-dependent function, and you should look at each compiler documentation. Keep in mind that heuristics can also vary depending on the level of optimization.

+2
source share

The following is a FAQ for the Sun Studio 11 compiler:

The compiler generates the built-in function as a regular called function (off-line) when any of the following statements is executed:

  • You are compiling with + d.
  • Compile with -g.
  • Function address required (as with virtual function).
  • The function contains control structures that the compiler cannot create inline.
  • The function is too complicated.

According to the answer to this post on 'clamage45', "control structures that the compiler cannot generate inline":

  • the function contains illegal constructs such as loop, switch or goto

Another list can be found here . Since most of the other answers indicated that the heuristic will be a 100% compiler, from what I read, I think to make sure the function is really enabled, you need to avoid:

  • local static variables
  • Loop constructs Switch statements
  • try / catch
  • Goto
  • recursion
  • and, of course, too complicated (whatever that means)
+2
source share

I am sure that most compilers decide depending on the length of the function (when compiling) in bytes and how often it is used against the type of optimization (speed and size).

+1
source share

I know only a couple of criteria:

  • If inline matches recursion, inline will be ignored.
  • switch / while / for in most cases causes the compiler to ignore the built-in
+1
source share

It depends on the compiler. Here (first part) what the GCC manual says:

  -finline-limit = n
            By default, GCC limits the size of functions that can be inlined.
            This flag allows the control of this limit for functions that are
            explicitly marked as inline (i.e., marked with the inline keyword
            or defined within the class definition in c ++).  n is the size of
            functions that can be inlined in number of pseudo instructions (not
            counting parameter handling).  The default value of n is 600.
            Increasing this value can result in more inlined code at the cost
            of compilation time and memory consumption.  Decreasing usually
            makes the compilation faster and less code will be inlined (which
            presumably means slower programs).  This option is particularly
            useful for programs that use inlining heavily such as those based
            on recursive templates with C ++.

            Inlining is actually controlled by a number of parameters, which
            may be specified individually by using --param name = value.  The
            -finline-limit = n option sets some of these parameters as follows:

             @item max-inline-insns-single
              is set to I / 2.
             @item max-inline-insns-auto
              is set to I / 2.
             @item min-inline-insns
              is set to 130 or I / 4, whichever is smaller.
             @item max-inline-insns-rtl
              is set to I.

            See below for a documentation of the individual parameters
            controlling inlining.

            Note: pseudo instruction represents, in this particular context, an
            abstract measurement of function size.  In no way, it represents
            a count of assembly instructions and as such its exact meaning
            might change from one release to an another.
+1
source share

does it insert if you write "inline" before the function starts?

-2
source share

All Articles