admin管理员组文章数量:1315327
Adding inline
to a function suggests the compiler that calls to the function be as fast as possible. (ISO/IEC 9899:1999 §6.7.4.5)
Dependent on the optimization level, a compiler might also decide to "inline" a function (substitute the call with its instructions).
ISO/IEC 9899:1999 defines in Annex C "Sequence points" / §5.1.2.3 that
The call to a function, after the arguments have been evaluated (6.5.2.2).
(and others) as a sequence point.
My Questions:
- What defines the C99 abstract machine in terms of function calls that have been optimized by inlining? Do these sequence points need to be retained even if the optimization removes the call?
- What is the behavior of typical embedded C compilers in C99 mode when aggressive optimization is used?
Adding inline
to a function suggests the compiler that calls to the function be as fast as possible. (ISO/IEC 9899:1999 §6.7.4.5)
Dependent on the optimization level, a compiler might also decide to "inline" a function (substitute the call with its instructions).
ISO/IEC 9899:1999 defines in Annex C "Sequence points" / §5.1.2.3 that
The call to a function, after the arguments have been evaluated (6.5.2.2).
(and others) as a sequence point.
My Questions:
- What defines the C99 abstract machine in terms of function calls that have been optimized by inlining? Do these sequence points need to be retained even if the optimization removes the call?
- What is the behavior of typical embedded C compilers in C99 mode when aggressive optimization is used?
- 1 The "as-if" rule determines what compilers can and can't do when optimizing (en.cppreference/w/c/language/as_if). I'm sure there's a dup on this site somewhere. – Mat Commented Jan 30 at 9:12
- 1 It does not mention C99 but you might want to take a look at What exactly is the "as-if" rule? – Gerhardh Commented Jan 30 at 9:17
- 8 "in terms of function calls that have been optimized by inlining" I think this is the wrong question. Why would the abstract machine consider optimizations? It is the compiler's job to stick to the rules for the abstract machine when doing optimizations. – Gerhardh Commented Jan 30 at 9:19
- 2 @Gerhardh What optimizations that are allowed is specified by the admittedly fuzzy chapter regarding the abstract machine, so I don't see why that's the wrong question. Sequencing is also defined in the same chapter. – Lundin Commented Jan 30 at 14:00
2 Answers
Reset to default 10... (and others) as a sequence point.
There are no other guaranteed sequence points except that one - after argument evaluation but before the function is called. Same goes for an inlined function. Except in case of inlining, argument evaluation may or may not get carried out depending on if that evaluation contains needed side effects or not (explained further below).
The function may or may not have full expressions as defined in Annex C, in which case there are sequence points inside the function body.
- What defines the C99 abstract machine in terms of function calls that have been optimized by inlining? Do these sequence points need to be retained even if the optimization removes the call?
In general - not specific to C99 nor inlined functions - the C standard speaks of needed side effects, for example C99 5.1.2.3 (emphasis mine):
In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
Sequence points aren't "holy", only needed side effects are. A needed side effect cannot get optimized out, but it can be optimized to run faster, it can be re-ordered across sequence points etc etc.
The only side effect that can neither get optimized nor reordered is a volatile
access. In case an access to a volatile
object happens, the compiler must carry out that evaluation and do so before the next sequence point present, meaning in that specific scenario, the sequence point is respected. In C23 this is phrased as "volatile accesses to objects are evaluated strictly according to the rules of the abstract machine."
For every other situation, the compiler is free to carry out the above quoted optimization. It is also free to do instruction re-ordering across sequence points, as long as that doesn't affect the program behavior.
For example leave out the whole function call, skip writes to local variables that aren't used ("needed") by the caller, re-order the function call etc etc. Those optimizations are possible regardless of inlining.
The only thing that inlining makes different is that a normal function defined elsewhere with external linkage can't make assumptions of the state of parameters on the caller side. Therefore function call arguments usually need to be both evaluated and copied into a temporary location before the function is called. But when the function is inlined, the compiler may draw further conclusions about the variables used as arguments and optimize accordingly.
- What is the behavior of typical embedded C compilers in C99 mode when aggressive optimization is used.
Same as for any other compiler. When the function is inlined, the function call is skipped, as is the parameter passing part according to the ABI. The variables that would have been passed can still be stored wherever the compiler already stored them, for example in fast registers rather than on the stack if the function call ABI required that. Meaning that there may be extra benefits in performance beyond just leaving out the function call overhead.
Though historically, various more or less exotic embedded system compilers did a lousy job of optimizing code in general. Many also suffer from poor C conformance. Many of these compilers are still on the market.
5.1.2.3 Program execution
- The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant.
本文标签: cC99 Is an inlined function still a sequence pointStack Overflow
版权声明:本文标题:c - C99: Is an inlined function still a sequence point? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977720a2408221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论