Section 4.46

Load-store forwarding analysis

Definition

Load-store forwarding analysis is a program-analysis step that decides whether a load, an instruction that reads memory, must return the value that an earlier store, an instruction that writes memory, put there. If it must, an analyzer may use the stored value as the load’s result, and a compiler may delete the load. The answer is yes only if the load reads bytes that the store wrote, with matching width and byte order §4.21, the store comes first on every path, and nothing can change those bytes in between.

The idea

A program stores the value 5 into a variable and a few instructions later reads the variable back. If an analyzer may conclude that the read returns 5, it can prove, for example, that a later bounds check passes. In source code this looks obvious. In machine code it is a claim that needs proof, because memory is shared. Between the store and the load, a different pointer may write some of the same bytes, a called function may write them, an interrupt handler §6.12 may run and write them, or a DMA §4.36 (direct memory access) controller, hardware that copies data into memory without the processor, may overwrite them. If the address belongs to a device register rather than to ordinary memory, each read may return a fresh value from the hardware.

A shared whiteboard is a fair picture. You can rely on reading back what you wrote only if you look at exactly the patch you wrote on, and nobody who can reach that patch has written there in the meantime. The analysis is the job of listing everybody who can reach the patch. The picture stops at device registers: some patches change by themselves, and some change because you read them.

The same words also name a processor feature. Store-to-load forwarding in a processor’s microarchitecture §4.39, its internal design, hands a load the value of a store that is still waiting in the processor’s store buffer, a queue of stores not yet written to memory, after the hardware has compared the two addresses at run time. That feature makes execution faster and never changes what a program computes. It is no evidence for an analysis, which must prove its conclusion for every execution before any of them happens.

How it works

Write the store’s span as the byte addresses from aa up to but not including a+ma + m, where aa is its address and mm its width in bytes, and the load’s span as bb up to b+nb + n. The analysis proves four facts and then reads the value off the stored bytes.

  1. Same bytes. Every byte the load reads was written by the store: a≤ba \le b and b+n≤a+mb + n \le a + m. The usual proof is that both addresses are computed from the same base register, which no instruction between them changes, plus constant offsets.

  2. Same interpretation. The instruction encodings §2.7 fix the widths: on Arm, STR and LDR move 4 bytes, STRH and LDRH move 2, and STRB and LDRB move 1. The byte order (endianness) decides which stored bytes form which part of the loaded value. A load narrower than a register is then widened, by zero extension §4.19 (LDRH, LDRB) or by sign extension, which copies the top bit (LDRSH, LDRSB).

  3. Order. The store runs before the load on every path that reaches the load. The control-flow graph §3.13, the map of which instruction can follow which, shows this: every path from the function’s entry to the load passes through the store.

  4. No interference. On every path from the store to the load, nothing writes any byte of the load’s span. The candidates are:

    • other stores: alias analysis §3.18, which decides which addresses may refer to the same memory, must show that their spans do not overlap the load’s span;

    • calls, which need an interprocedural summary §15.14, a description of the memory that the called function may write;

    • interrupt handlers and other threads that share the memory, which can write between any two instructions unless interrupts §6.11 are disabled or a lock keeps them out;

    • DMA transfers and other hardware that writes memory;

    • the end of the object’s lifetime, for example a call to free or the return of the function that owned a stack variable.

If any fact cannot be proved, the analysis forgets the stored value and treats the load’s result as unknown. If an intervening store is known exactly, the analysis can instead update the affected bytes of the remembered value.

Forwarding never applies to memory-mapped I/O §4.35 (MMIO), in which device registers appear at memory addresses: a read can return a new value from the hardware or change the device’s state, for example by removing a byte from a receive queue. For the same reason a C compiler never forwards to a read of an object declared with the volatile qualifier §4.34, which tells the compiler that every read must really happen.

Worked example

Take a little-endian processor, which stores the least significant byte at the lowest address, as most Cortex-M processors do. A 4-byte store writes 0x11223344 at address 0x1000. The bytes are 44 at 0x1000, 33 at 0x1001, 22 at 0x1002 and 11 at 0x1003. The store’s span runs from 0x1000 up to 0x1004.

A 4-byte load from 0x1000 reads exactly that span, so with nothing in between it returns 0x11223344. A 2-byte load from 0x1002 reads 0x1002 and 0x1003. It lies inside the store’s span, because 0x1000 is at most 0x1002, and 0x1002 plus 2 is 0x1004, which is at most 0x1004. It returns the bytes 22 and 11, read as 0x1122.

Now insert a 1-byte store of 0xAA at 0x1001 between the 4-byte store and the 4-byte load. Its span, 0x1001 up to 0x1002, overlaps the load’s span. The bytes become 44, AA, 22, 11, and the load returns 0x1122AA44, not 0x11223344. An analysis that knows the second store exactly computes 0x1122AA44. An analysis that knows only that the second store writes through a pointer whose value it cannot bound must drop the fact altogether. A 1-byte store at 0x1004 would not matter: its span starts where the load’s span ends.

The same reasoning explains a classic bug. A C program waits with while (!done) { }, where done is an ordinary global variable that an interrupt handler sets to 1. The loop contains no store to done and no call, so the compiler’s forwarding analysis concludes that every read returns the value of the first read. The compiler reads done once, and the loop may never end. The analysis follows the C rules, which assume that no other writer exists unless the program says so; the program broke that assumption. Declaring done volatile, or as an atomic object, tells the compiler that another writer exists.

Stated precisely

Let a store write the mm-byte value vv, read as an unsigned integer, at address aa, and let a later load read nn bytes at address bb, where aa and bb are the values the addresses have when each instruction runs. Suppose:

  1. a≤ba \le b and b+n≤a+mb + n \le a + m, computed without wrap-around at the top of the address space;

  2. the store precedes the load on every path that reaches the load;

  3. no agent (instruction, called function, interrupt handler, thread, DMA transfer or debugger) writes any byte from bb to b+n−1b + n - 1 between the store and the load;

  4. the bytes are ordinary memory, where a read returns the last value written and has no side effect;

  5. the object that holds the bytes is alive from the store to the load.

Then, on a little-endian machine, the load returns the unsigned value r=⌊v28(b−a)⌋mod28n,r = \left\lfloor \frac{v}{2^{8(b-a)}} \right\rfloor \bmod 2^{8n}, where ⌊x⌋\lfloor x \rfloor is xx rounded down. A sign-extending load then reads rr as an 8n8n-bit two’s-complement number, the usual encoding of signed integers. On a big-endian machine, which stores the most significant byte first, the exponent 8(b−a)8(b - a) becomes 8(a+m−b−n)8(a + m - b - n).

In the example, v=𝟶𝚡𝟷𝟷𝟸𝟸𝟹𝟹𝟺𝟺v = \texttt{0x11223344}, b−a=2b - a = 2 and n=2n = 2, so r=⌊v/216⌋mod216=𝟶𝚡𝟷𝟷𝟸𝟸r = \lfloor v / 2^{16} \rfloor \bmod 2^{16} = \texttt{0x1122}. Two spans, from aa up to a+ma + m and from cc up to c+kc + k, share a byte exactly when a<c+ka < c + k and c<a+mc < a + m. For the store at 0x1004, c=a+mc = a + m, so the second condition fails and the spans share no byte.

Limits and common mistakes

A forwarded value is a fact about one load. It does not show that the rest of memory is unchanged, and removing the load can change timing and the sequence of memory accesses, which matters to code that measures time or drives hardware.

Unknown must stay unknown. Treating an unanalyzed call or an unmodeled instruction as if it wrote nothing makes the analysis lose soundness §3.7, the guarantee that everything it proves is true. This is the most common error in such analyses.

Equal register values are not enough. Two different addresses can overlap in part, as the 1-byte store above shows. The same virtual address can name different bytes after a memory mapping changes. A pointer can hold the right address of an object whose lifetime has ended; in C, reading through it is undefined behavior §4.12, whatever bytes are still there.

Some instructions fault on an address that is not a multiple of their width, and a misaligned access may be carried out as several smaller accesses, between which another writer can act.

Source-language rules can allow more forwarding than the machine-code view does. Under the C rules of strict aliasing §4.23, a compiler may assume that a store through a pointer to one type does not change an object of an unrelated type, and forward the earlier value across that store.

Concurrent access needs a memory model as well. When another processor core writes the bytes, the language’s or the architecture’s rules on ordering decide which value a load may return; a forwarding fact proved for one thread says nothing about them.

Questions answered

What proves identical addresses, access widths, ordering, and absence of aliases?

Each of the four is proved by a different piece of evidence, and a load may be given the value of an earlier store only when all four are present. Identical addresses: a calculation showing that both addresses come from the same base value, unchanged in between, plus constant offsets, or that both are the same constant. Access widths: the instruction encodings, which fix how many bytes each access moves (on Arm, 4 for LDR and STR, 2 for LDRH and STRH, 1 for LDRB and STRB), together with a containment test. If the store writes mm bytes at address aa and the load reads nn bytes at address bb, the test is a≤ba \le b and b+n≤a+mb + n \le a + m; the byte order and the load’s zero or sign extension then fix the value.

Ordering: the control-flow graph, the map of which instruction can follow which, showing that every path to the load passes through the store. Absence of aliases: alias analysis showing that no other store between them can touch those bytes; a summary for every call in between, stating which memory the called function may write; and evidence that no interrupt handler, other thread or DMA (direct memory access) transfer writes those bytes in that window. The address must also be ordinary memory, not a device register, and the object must still be alive. If any piece is missing, the load’s value is unknown.

Related entries: Alias analysis (§3.18), Direct memory access (DMA) (§4.36), Helper contract (§2.21), Endianness (§4.21), Volatile qualifier (§4.34), Memory-mapped I/O (MMIO) (§4.35), Strict aliasing and effective type (§4.23), Compiler optimization (§3.39), Interprocedural summary (§15.14).