Definition
A live-in register of a region of machine code (a function, a block of instructions, or any piece chosen for analysis) is a register whose value on entry to the region may be read before the region writes that register. Its value is therefore produced outside the region, by whatever ran before, and the live-in registers are the region’s register inputs. Any claim about the region that depends on such a value holds only if a fact about every way of entering the region justifies that value.
The idea
A function that adds to a counter inside a structure finds the structure through register r0. Nothing inside the function writes r0 before that read, so the address comes from the caller. Looked at alone, the instruction ldr r2, [r0, #4] (load the 4-byte word at address r0 + 4 into r2) could read from anywhere: whether it is safe depends entirely on what every caller puts in r0. A live-in register is a channel through which assumptions enter the code.
Listing the live-in registers lists the region’s inputs, including the hidden ones. A function’s source code names its arguments, but its machine code also reads the return address, the stack pointer, registers it saves on behalf of its caller, and sometimes the condition flags §4.42. Each of these needs a justified fact, or must be treated as holding any value at all. Analyzing a region as if a live-in register held a convenient value, such as the address seen in one test run, proves something about that run only.
How it works
Live-variable analysis, a standard data-flow analysis §3.15 (a method that computes a fact for every point of a program by propagating facts along the program’s possible paths), finds the live-in registers. It works backward through the region, because whether a value is still needed depends on what comes later. For each instruction it records the registers the instruction reads, its use set, and the registers it certainly writes, its def set (def for definition).
At each exit of the region, start with the set of registers that the code after the region may read. If only the reads inside the region matter, start with the empty set; the result is then the registers the region reads before writing them, which compiler texts call upward-exposed uses.
Walk backward. The live set before an instruction is the live set after it, minus the def set, plus the use set. A register that an instruction both reads and writes stays live, because the read happens first.
Where control flow splits, the live set after the branch is the union of the live sets at the start of every possible successor, because a value is needed if any path may read it. Loops are handled by repeating the walk until no set changes.
The live set before the region’s first instruction is the set of live-in registers.
The same method covers the condition flags and memory locations. The flags are live-in when the region reads them before setting them, for example when it begins with a conditional instruction or with an add-with-carry. A conditional instruction whose condition may fail does not certainly write its destination, so it does not remove that register from the live set.
A fact about a live-in register may be assumed at the entry only if it holds on every path into the entry. The usual sources of such facts are these.
The calling convention. The ABI §12.47 (application binary interface), the set of rules by which separately compiled code cooperates, fixes which register carries which argument and what the caller guarantees about the machine state. The Procedure Call Standard for the Arm Architecture (AAPCS), for example, passes the first four 32-bit arguments in r0 to r3, puts the return address in the link register §4.45 (LR, register r14), requires the stack pointer (SP, register r13) to be a multiple of 8 at a call between separately compiled units, and requires a function to return with r4 to r8, r10 and r11 holding the values they had on entry (r9 as well, unless the platform assigns r9 another role).
The caller’s contract. The ABI says where a pointer arrives, not whether it points to valid memory. That fact comes from a precondition, a condition every caller must establish before the call, such as “r0 points to a writable object of at least 8 bytes”, stated in the function’s specification and checked at each call site.
Analysis of the code before the region. When the callers are available, reaching definitions §3.16 (which writes can supply the value read here) and interval analysis §3.9 (which range of numbers a register can hold) give the register’s possible values at each call site, combined over all call sites and paths.
Hardware-defined entry states. At reset, a Cortex-M core loads SP from the first word of the vector table §6.14 and starts at the address in the second word; r0 to r12 and the condition flags then have unknown values. At exception entry the core saves eight registers on the stack in a fixed layout.
Program-wide invariants. A register reserved for one purpose throughout a program, such as a register that holds a base address, carries a fact only if every piece of code that can run before the region preserves it.
Worked example
Take this Thumb function, which adds r1 to the 4-byte counter stored 4 bytes into a structure whose address is in r0.
ldr r2, [r0, #4]r2 = word at address r0 + 4
adds r2, r2, r1r2 = r2 + r1, setting the flags
str r2, [r0, #4]store r2 at address r0 + 4
bx lrreturn to the address in LR
Ask which registers the function itself reads before writing, so start with the empty set at the exit and walk backward.
Before
bx lr: it reads LR, so the live set is .Before
str: it reads r0 and r2 and writes no register: .Before
adds: it writes r2 and the flags and reads r2 and r1: .Before
ldr: it writes r2 and reads r0: .
The live-in registers are r0, r1 and LR. Register r2 is not live-in, because the function writes it before reading it. The flags are not live-in, because adds sets them and nothing before it reads them.
The AAPCS supplies what these registers mean: r0 and r1 are the first two arguments, and LR is the return address. It does not supply what r0 points to. The load and the store each access the 4 bytes at addresses r0 + 4 to r0 + 7, so memory safety needs the precondition “the bytes from r0 to r0 + 7 lie inside one writable object that is still allocated”. Check it at each call site.
Caller A passes the address of a 16-byte structure at
0x20000100. The accessed bytes0x20000104to0x20000107lie inside0x20000100to0x2000010F. The precondition holds.Caller B passes the address of a 6-byte buffer at
0x20000200, which ends at0x20000205. The function accesses up to0x20000207, so bytes,0x20000206and0x20000207, lie outside the buffer. The precondition fails, and a proof of the function says nothing about this call.
The live-in set depends on where the region starts. A region that begins at adds, for example a fragment analyzed on its own, has live-in registers r0, r1, r2 and LR, and the fact it needs about r2, that r2 holds the word loaded from r0 + 4, comes from the ldr outside the fragment. Saved registers are live-in too: had the function begun with push {r4, lr} (save r4 and LR on the stack), r4 would be live-in, although the function never computes with it. The only obligation about r4 is to restore it unchanged.
Stated precisely
Let a region be given by its instructions and its control-flow graph §3.13. For each instruction , let be the registers may read, the registers writes on every execution, and the instructions inside that can run directly after . Let be the set of registers assumed live at the exits of . The live sets are the smallest sets that satisfy, for every instruction , A register is live-in to if for the entry instruction . Equivalently, some path from reaches an instruction that reads , or reaches an exit with , without first passing an instruction that writes .
The computed set is complete, meaning it contains every register that some execution reads before writing it, if the control-flow graph contains every possible successor of every instruction, including the targets of indirect jumps, and every use set lists every register its instruction may read, including the registers a called function reads. It may contain extra registers, which costs precision but not correctness.
Let be a fact about the live-in registers, such as “r0 points to a writable object of at least 8 bytes”. A proof that the region satisfies a property whenever holds at its entry, written as the Hoare triple §3.31 , applies to an execution only if held when that execution entered . It applies to every execution of the program when holds at every way of entering : at every call site, at every branch into from outside, and at every exception return or other transfer that can land on its first instruction.
Limits and common mistakes
An ABI is a convention, not an enforcement mechanism. Hand-written assembly, an interrupt handler §6.12, or a call through a corrupted function pointer can enter the region with any register values.
A fact proved for one caller does not transfer to another. Context sensitivity §3.12, keeping separate facts for each call site, avoids merging facts from different callers, but a proof that covers all callers still needs the precondition at each of them.
General-purpose registers are not the only inputs. The flags, the stack pointer, floating-point registers and memory can all be live-in, and code that starts with a conditional instruction or an add-with-carry reads flags it did not set.
An incomplete control-flow graph can make the analysis miss a live-in register. When the target of an indirect jump or call is unknown, the analysis must assume that the jump may read every register.
A value is not a permission. Knowing that r0 holds a particular address does not say whether the memory there is writable, or still allocated, at the moment of access; those are separate facts about the object, not about the register.
Register names, comments, the value seen in one debugger session, and the behavior of the one caller the author had in mind justify nothing about other executions. When no justified fact is available, the register must be treated as able to hold any 32-bit value, or the result must be stated as conditional on the assumed fact.
Questions answered
What facts justify a live-in register’s value or range?
A live-in register’s value or range is justified only by a fact that holds on every path by which execution can enter the region, because the value was produced outside the region before the region reads it. Such facts come from five sources: the calling convention of the ABI (application binary interface), which says which register carries which argument, that the link register holds the return address and which registers must be given back unchanged, but not whether a pointer is valid; a precondition in the function’s contract, checked at every call site, such as “r0 points to a writable object of at least 8 bytes”; analysis of the code before the region, such as reaching definitions and interval analysis, combined over all call sites and paths; hardware-defined entry states, such as the registers loaded at reset or saved at exception entry; and program-wide invariants about reserved registers, valid only if all code that can run first preserves them.
Register names, comments, the value seen in one test run, or the behavior of one particular caller justify nothing about other executions. When no justified fact is available, treat the register as holding any value, or state the result as conditional on the assumed fact and establish that fact at every entry before relying on the result.
Related entries: Reaching definitions (§3.16), Context sensitivity (§3.12), Machine-code proof boundary (§4.48), Data-flow analysis (§3.15), Native application binary interface (ABI) (§12.47), Link register (LR) (§4.45). Further reading: [81, 82].