The as-if rule: what your compiler is allowed to change

Foundations

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:

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;
}
  1. Find what is observable. Here, only the return value. Nothing is printed and nothing is volatile.
  2. 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.
  3. Check that nobody can tell the difference. For every n, the same number comes out.
  4. 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.

Four lines of C and what gcc and clang emit for them at -O2; only output, volatile accesses and synchronised thread communication are compared What you wrote What runs at -O2 for (i = 0; i < n; i++) s += i; replaced clang: n>0 ? n(n−1)/2 : 0 printf("hello\n"); swapped puts("hello"); · same output memset(pw, 0, 64); // last use removed nothing: the password stays in memory while (!done) { } // plain int hoisted read once: gcc spins forever, clang deletes the loop The only thing compared: what the outside world can see printed and written output · volatile reads and writes · atomics and locks between threads
Left: the source. Right: what gcc 14 / clang 19 emit at -O2 (gcc keeps a faster loop in row 1). Solid rows are pure wins. Dashed rows are legal too, and they are where people get hurt.
Four lines of C and what gcc and clang emit for them at -O2; only output, volatile accesses and synchronised thread communication are compared You wrote → what runs at -O2 for (i = 0; i < n; i++) s += i; replaced clang: n>0 ? n(n−1)/2 : 0 printf("hello\n"); swapped puts("hello"); · same output memset(pw, 0, 64); // last use removed nothing: the password stays in memory while (!done) { } // plain int hoisted read once: gcc spins, clang deletes it Only this is compared: printed and written output · volatile access · atomics and locks
Top: the source. Below each: what gcc 14 / clang 19 emit at -O2 (gcc keeps a faster loop in row 1). Solid: pure wins. Dashed: legal, and where people get hurt.

Which languages use it

All compiled and JIT-compiled languages. (A JIT compiles code while the program runs.)

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

In short: the compiler keeps output, volatile accesses and synchronised thread communication — nothing else. If something must happen, make it observable.

Questions? Let's Chat :)

os cs fundamentals