The as-if rule: what your compiler is allowed to change
The as-if rule says that the compiler doesn’t have to run your code the way you wrote it. It can run anything it likes, as long as the result looks as if it ran your code exactly as written.
This rule is what lets compilers make code fast. Nearly every optimisation uses it: removing a loop, precomputing a number, reordering two lines.
The C++ standard says a compiler only has to copy the observable behaviour of the program, and a footnote calls this “the as-if rule”. The C standard (§5.1.2.3) says the same thing without naming it.
What “from the outside” means
Only these things are observable. They must happen, with the same values and in the same order:
- Input and output: what is printed, read, or written to files.
volatilereads and writes: every one of them happens, in order. Avolatilevariable is memory that something outside the program can change, like a hardware register.- Communication between threads, but only through atomics, locks and other synchronisation. An atomic variable is one that threads can safely read and write at the same moment.
Everything else may change: local variables, loops, temporary values, the order of calculations.
Step by step: how the compiler uses it
Take this C function:
int sum(int n) {
int s = 0;
for (int i = 0; i < n; i++)
s += i;
return s;
}
- Find what is observable. Here, only the return value. Nothing is printed and nothing is
volatile. - Find a cheaper way to get the same result. For n > 0 the sum 0 + 1 + … + (n−1) is n(n−1)/2. For n ≤ 0 the loop never runs, so the answer is 0.
- Check that nobody can tell the difference. For every
n, the same number comes out. - Emit the cheaper code.
clang -O2(clang 19) emits no loop at all: one check for n ≤ 0, then one multiply and a shift.gcc -O2(gcc 14) keeps a faster loop. Both are allowed: the rule fixes what comes out, not how.
The figure shows four real rewrites, checked with gcc -O2 -S (gcc 14) and clang -O2 -S (clang 19) on x86-64.
Which languages use it
All compiled and JIT-compiled languages. (A JIT compiles code while the program runs.)
- C, C++: written into the standard. C++ also lets the compiler skip a copy constructor or a
newallocation even when they have side effects. - Rust, Swift: the same contract, carried out by the LLVM optimiser they share with clang.
- Java, C#, Go: inside one thread, the code must behave as if it ran in program order. Other threads see a fixed order only through locks, atomics and (in Go) channels.
- JavaScript: the JIT compiles from a guess (for example “this is always an integer”) and goes back to slow code if the guess fails.
- Python (CPython): barely. It precomputes constants:
60 * 60 * 24is stored as86400.
Where it surprises people
All three are legal. The program promised less than its author thought.
1. Wiping a password. pw is a local array, and nobody reads it after memset(pw, 0, 64), so the write is not observable. (If pw belonged to the caller, the caller could read it, and the wipe would stay.) gcc and clang both delete it, and the password stays in memory. Fix: a wipe that is promised to stay — memset_s (C11’s optional Annex K, missing from glibc), explicit_bzero (glibc, BSD), SecureZeroMemory (Windows), the zeroize crate (Rust).
2. Benchmarks that measure nothing. If the result is thrown away, the whole loop can go, and you time an empty function. Fix: use the result — benchmark::DoNotOptimize(x) (C++), std::hint::black_box(x) (Rust), a JMH Blackhole (Java).
3. A flag shared between threads. while (!done) { } on a plain int: in one thread nothing can change done, so it is read once. gcc spins forever, clang deletes the loop. Another thread writing done without a lock or atomic is a data race, which C and C++ do not define at all. Fix: an atomic — atomic_int (C), std::atomic<bool> (C++), AtomicBool (Rust), a volatile field (Java; in C and C++ volatile does not make threads safe).
What it does not promise
- Speed or memory use: neither is observable.
- What a debugger shows: that is why debug builds use
-O0(no optimisation). - Anything, if the program has undefined behaviour (integer overflow, reading past an array, a data race).
In short: the compiler keeps output, volatile accesses and synchronised thread communication — nothing else. If something must happen, make it observable.