Register Allocation Zach Ma
Overview • Memory Model • Register Classes • Local Register Allocation • Global Register Allocation
Memory model • The choice of memory model fundamentally determines the allocator’s task. • In a register-to-register model, the allocator tries to minimize the inserts of the load and store. • In contrast, in a compiler with a memory-to-memory model, the allocator tries to remove as many loads and stores as possible, since this can significantly improve the overall performance.
Register Classes • Most processors have distinct classes of registers for different kinds of values. E.g. The early IBM 360 machines had 16 general-purpose registers and four floating-point registers. • On most processors, general-purpose registers and floating-point registers are not used to hold the same kinds of values. If different register classes overlap, the compiler must allocate them together. • A similar problem arises on architectures that allow values of different length to be stored in general-purpose registers.
Local Register Allocation • Top-Down Simple principle: the most heavily used values should reside in registers. • Bottom-up Keep list of available registers for each register class, and allocate them when needed.
Top-down Approach • Implementing Algorithm • Compute a priority for each virtual register • Sort the virtual registers into priority order • Assign registers in priority order • Rewrite the code • Primary weakness It dedicates a physical register to one virtual register for the entire basic block.
Bottom-up Approach • Begin with all the registers unoccupied. • Most of the complications in the algorithm occur in the routines Ensure, Allocate, and Free. • Ensure just make sure a virtual register is or will occupy a physical register. • Allocate returns a physical register from the free list of a register class. • Free simply needs to push the freed register onto the stack and reset its fields to their initial values.
Bottom-up Continue • Advantage In some sense, it maximizes the benefit obtained for the cost of the spill. • Clean and Dirty Value A value that need not be stored is called clean, while a value that needs a store is called dirty. E.g. constant, value created by a load from memory
Two Examples of Spill • Assume that x1 is clean and x2 is dirty, and the remainder of the block is x3 x1 x2, on a two-register machine • Same with the first example, but the remainder of the block is x3 x1 x3 x1 x2
Live Ranges • Aclosed set of related definitions and uses that serves as the base name space
Global Register Allocation • Graph-coloring allocators build a graph, called the interference graph, to model the conflicts between live ranges. • If the compiler cannot directly construct a k-coloring for the graph, it modifies the underlying code by spilling some values to memory and tries again.
Advanced Topics • Variations on Graph-Coloring Allocation • Global Register Allocation over SSA Form