Changes between Version 3 and Version 4 of Commentary/Compiler/Backends/NCG/RegisterAllocator


Ignore:
Timestamp:
Sep 17, 2007 4:59:29 PM (8 years ago)
Author:
guest
Comment:

shift code map to new page

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Compiler/Backends/NCG/RegisterAllocator

    v3 v4  
    1 = The NCG Register Allocator =
     1= The Register Allocator =
    22
    33== Overview ==
    44
    5 TODO: what a register allocator is responsible for.
     5The register allocator is responsible for assigning real/hardware regs (hregs) to each of the virtual regs (vregs) present in the code emitted by the native code generator. It also inserts spill/reload instructions to save vregs to the stack in situations where not enough hregs are available.
    66
    77GHC currently provides two register allocation algorithms, simple linear scan and graph coloring. Although some of the code is shared between the two, as we only want to maintain a single algorithm, support for linear scan will be removed in a subsequent version.
     
    99In the meantime, there are three options for register allocation:
    1010
    11  * Linear allocator
    12    The linear allocator is currently turned on by default. This is what you get when you compile with -fasm. The linear allocator does a single pass through the code, allocating registers on a first-come-first-served basis. It is quick, and does a reasonable job for code with little register pressure. It has no look-ahead. If say, a particular register will be clobbered by a function call, it does not know to avoid allocating to that register in the code before the call - and subsequently inserts more spill/reload instructions than the other algorithms.
     11 * '''Linear scan'''[[BR]]
     12   The linear allocator is currently turned on by default. This is what you get when you compile with {{{-fasm}}}. The linear allocator does a single pass through the code, allocating registers on a first-come-first-served basis. It is quick, and does a reasonable job for code with little register pressure. It has no look-ahead. If say, a particular register will be clobbered by a function call, it does not know to avoid allocating to that register in the code before the call and subsequently inserts more spill/reload instructions than the other algorithms.
    1313
    14  * Graph coloring allocator
    15    Enabled with {{{-fregs-graph}}}. The graph coloring algorithm operates on the native code for a whole function at a time. From each function it extracts a register conflict graph which represents which virtual regs are in use (live) at the same time, and thus cannot share a real reg. It tries to assign real regs (represented as colors) to the nodes so that no two adjacent nodes share the same color. This algorithm tends to do better than the linear allocator because the conflict graph helps it avoid the look-ahead problem. The coloring allocator also tries to allocate the source and destination of register-to-register move instruction to the same real reg. This is done by coalescing (merging) move-related nodes. If this succeeds then the move instruction can be erased.
     14 * '''Graph coloring''' (enabled with {{{-fregs-graph}}})[[BR]]
     15   The graph coloring algorithm operates on the code for a whole function at a time. From each function it extracts a register conflict graph. The conflict graph has a node for every vreg in the code and an edge between two vregs if they are in use at the same time and thus cannot share the same hreg. It tries to assign hregs (represented as colors) to the nodes so that no two adjacent nodes share the same color, if it can't then it inserts spill code, rebuilds the graph and tries again. This algorithm tends to do better than the linear allocator because the conflict graph helps it avoid the look-ahead problem. The coloring allocator also tries to allocate the source and destination of register-to-register move instruction to the same hreg. This is done by coalescing (merging) move-related nodes. If this succeeds then the move instruction can be erased.
    1616
    17  * Graph coloring with iterative coalescing
    18    Enabled with {{{-fregs-iterative}}}. Iterative coalescing is an improvement over regular graph coloring whereby coalescing passes are interleaved with coloring passes. Iterative coalescing does a better job than regular graph coloring, but is slower.
     17 * '''Graph coloring with iterative coalescing''' (enabled with {{{-fregs-iterative}}})[[BR]]
     18   Iterative coalescing is an improvement over regular graph coloring whereby coalescing passes are interleaved with coloring passes. Iterative coalescing does a better job than regular graph coloring, but is slower because it must alternate between the coloring and coalescing of nodes.
    1919
    2020
    21 == Code ==
    22 
    23 The register allocator code is split into two main sections, the register allocator proper and a generic graph coloring library. The graph coloring library is also used by the Stg->Cmm converter.
    24 
    25 === The register allocator ===
    26 
    27  * [[GhcFile(compiler/nativeGen/RegLiveness.hs)]] [[BR]]
    28    Defines {{{LiveInstr}}} and {{{LiveCmmTop}}} which carry native machine instructions annotated with register liveness information. It also provides functions to annotate native code ({{{NatCmmTop}}}) with this liveness information, and to slurp out sets of register conflicts for feeding into the coloring allocator.
    29 
    30  * [[GhcFile(compiler/nativeGen/RegAllocColor.hs)]] [[BR]]
    31    Defines {{{regAlloc}}}, the main driver function for the graph coloring allocator. The driver accepts {{{LiveCmmTop}}}s which use virtual regs, and produces{{{NatCmmTops}}} which use real machine regs. This module also provides functions to help build and deep seq the register conflict graph.
    32 
    33  * [[GhcFile(compiler/nativeGen/RegAllocLinear.hs)]] [[BR]]
    34    Defines the linear scan allocator. Its interface is identical to the coloring allocator.
    35 
    36  * [[GhcFile(compiler/nativeGen/RegAllocInfo.hs)]] [[BR]]
    37    Defines the register information function, {{{regUsage}}}, which takes a set of real and virtual registers and returns the actual registers used by a particular {{{Instr}}}; register allocation is in AT&T syntax order (source, destination), in an internal function, {{{usage}}}; defines the {{{RegUsage}}} data type[[BR]][[BR]]
    38 
    39  * [[GhcFile(compiler/nativeGen/RegSpillCost.hs)]] [[BR]]
    40    Defines {{{chooseSpill}}} which is responsible for selecting a virtual reg to spill to the stack when not enough real regs are available.
    41 
    42  * [[GhcFile(compiler/nativeGen/RegSpill.hs)]] [[BR]]
    43    Defines {{{regSpill}}} which takes {{{LiveCmmTop}}}s and inserts spill/reload instructions virtual regs that wouldn't fit in real regs. {{{regSpill}}} simply inserts spill/reloads for every use/def of a particular virtual reg.
    44  
    45  * [[GhcFile(compiler/nativeGen/RegSpillClean.hs)]] [[BR]]
    46    The spill cleaner is run after real regs have been allocated and erases spill/reload instructions inserted by {{{regSpill}}} that weren't strictly nessesary.
    47 
    48  * [[GhcFile(compiler/nativeGen/RegAllocStats.hs)]] [[BR]]
    49    Defines data types and pretty printers used for collecting statistics and debugging info from the coloring allocator.
    50 
    51 === Graph coloring ===
    52 
    53  * [[GhcFile(compiler/util/GraphBase.hs)]] [[BR]]
    54    Defines the basic {{{Graph}}}, {{{Node}}} and {{{Triv}}} types used by the coloring algorithm.
    55 
    56  * [[GhcFile(compiler/util/GraphColor.hs)]] [[BR]]
    57    Defines the function {{{colorGraph}}} which is responsible for assigning colors (real regs) to nodes (virtual regs) in the register conflict graph. For more detail see [[wiki:Commentary/Compiler/GraphColoring Graph Coloring]].
    58 
    59  * [[GhcFile(compiler/util/GraphOps.hs)]] [[BR]]
    60    Defines functions to perform basic operations on the graphs such as adding, deleting, and coalescing nodes.
    61 
    62  * [[GhcFile(compiler/util/GraphPps.hs)]] [[BR]]
    63    Defines functions for pretty print graphs in human readable-ish and graphviz format.
    64 
    65 === Miscellanea ===
    66 
    67  * [[GhcFile(compiler/nativeGen/RegCoalesce.hs)]] [[BR]]
    68    Defines a function {{{regCoalesce}}} that does aggressive coalescing directly on {{{LiveCmmTops}}}, without using the graph. This isn't used at the moment but has been left in incase we want to rejig the allocator when the new CPS converter comes online.
    69 
    70  * [[GhcFile(compiler/nativeGen/RegArchBase.hs)]] [[BR]]
    71    Defines utils for calculating whether a register in the conflict graph is trivially colorable, in a generic way which handles aliasing between register classes. This module is not used directly by GHC. TODO: link to description of algorithm.
    72 
    73  * [[GhcFile(compiler/nativeGen/RegArchX86.hs)]] [[BR]]
    74    Contains a description of the aliasing constraints between the register sets on x86. This module is not used directly by GHC.
    75 
     21== [wiki:Commentary/Compiler/Backends/NCG/RegisterAllocator/Code Code map] ==