2Overview Memory Model Register Classes Local Register Allocation Global Register Allocation
3Memory modelThe 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.
4Register ClassesMost 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.
5Local Register Allocation Top-DownSimple principle: the most heavily used values should reside in registers.Bottom-upKeep list of available registers for each register class, and allocate them when needed.
6Top-down Approach Implementing Algorithm Compute a priority for each virtual registerSort the virtual registers into priority orderAssign registers in priority orderRewrite the codePrimary weaknessIt dedicates a physical register to one virtual register for the entire basic block.
7Bottom-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.
9Bottom-up Continue Advantage In some sense, it maximizes the benefit obtained for the cost of the spill.Clean and Dirty ValueA 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
10Two Examples of SpillAssume that x1 is clean and x2 is dirty, and the remainder of the block is x3 x1 x2, on a two-register machineSame with the first example, but the remainder of the block is x3 x1 x3 x1 x2
11Live RangesA closed set of related definitions and uses that serves as the base name space
12Global 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.
13Advanced Topics Variations on Graph-Coloring Allocation Global Register Allocation over SSA Form