9

I am using C++98. To what extent can function calls be reordered? I am not using any global state, only state of objects local to the function.

My particular case is:

{
  RaiiType T;
  Object1.FunctionCall();
  Object2.FunctionCall();
}

Where Object1 and Object2 are declared in the next scope up. Is the constructor for T permitted to be reordered after either function call, assuming that it can be trivially proven (at least to a human) that there are no dependencies between the construction and the function calls?


In my particular case, the RAII object is used to time the execution of the function calls.

Graznarak
  • 3,626
  • 4
  • 28
  • 47
  • It depends on "no dependencies". If you mean that the difference can not be observed, then yes, the compiler (and even the CPU itself) is free to reorder the operations. – Daniel Frey Jun 02 '15 at 21:32
  • 1
    No reordering that change the observed behaviour. But the compiler is allowed to do anything under the "as if rule", if you cant tell the difference. – sp2danny Jun 02 '15 at 21:38
  • one of you should post this as answer("as if rule" preferably) – Creris Jun 02 '15 at 21:41
  • 2
    I'll just leave this here: possible duplicate of http://stackoverflow.com/questions/26190364/is-it-legal-for-a-c-optimizer-to-reorder-calls-to-clock – Marco A. Jun 02 '15 at 21:47

1 Answers1

6

So long as a standards-compliant program could not tell the difference in its observable behavior, the compiler (as well as other components in the system) may freely reorder instructions and operations however it likes.

Community
  • 1
  • 1
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • in other words, under "as if rule"(could've been included, just to make it a bit more complete, but its your decision) – Creris Jun 02 '15 at 21:42
  • 1
    Is a call to `clock()` (or mechanisms from ``) considered "observable behavior" according to the spec? See the update to the question. – Nemo Jun 02 '15 at 21:52
  • 1
    @Nemo It cannot be. If it were, no optimizations would be legal because all optimizations can change the time code takes to run. You can't "synthesize" guarantees, and clearly this can't be a guarantee you have. – David Schwartz Jun 02 '15 at 22:56
  • 1
    @DavidSchwartz: I did not mean the result of the call; I meant the event of the call itself. That is, can the compiler reorder the calls to `clock` across the code you are trying to time, or (say) swap the calls to `clock` with each other, according to the standard? There are related Q&As on SO already, but the answers are largely contradictory and do not cite the spec, as far as I can tell. But this is ultimately what this questioner is really trying to ask... – Nemo Jun 02 '15 at 23:46
  • @Nemo Yes, it can. For precisely the reason I stated -- if the results of calls to functions like `clock` were guaranteed, no optimizations would be possible. So they can't be guaranteed, and you can't "synthesize" a guarantee. (That is, create a guarantee from pieces that include things not guaranteed.) – David Schwartz Jun 03 '15 at 00:22
  • To put it another way, the specifications for functions like `clock` say what values they return and the C++ specification says when, in the program flow, they'll get called. Since you don't have a guarantee from the latter, the guarantee from the former doesn't help you. – David Schwartz Jun 03 '15 at 00:31
  • @DavidSchwartz: So there is no standard-conforming way to measure the time a C++ fragment takes to execute, despite the presence of ``? (This would not necessarily inhibit optimization in any significant way. Now that the standard has "time" functions, they could be specified to impose (very limited) sequence constraints on state changes to the abstract machine. This would not inhibit optimizations in general, but only reordering across functions that ask what time it is. This does not strike me as insane, but perhaps I have not thought it through sufficiently.) – Nemo Jun 03 '15 at 01:09
  • @Nemo Correct. Performance, and performance measurement, is inherently platform-specific. There are generic, platform-independent methods that usually work reasonably well, but don't pretend you have guarantees. – David Schwartz Jun 03 '15 at 01:13