| Version 2 (modified by guest, 6 years ago) |
|---|
The NCG Register Allocator
Overview
GHC currently provides two register allocation algorithms, simple linear scan and graph coloring. Although some of the code is currently shared between the two, as we only want to maintain a single algorithm support for linear scan will be removed in a subsequent version.
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.
The register allocator
- compiler/nativeGen/RegLiveness.hs
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.
- compiler/nativeGen/RegAllocColor.hs
Defines regAlloc, the main driver function for the graph coloring allocator. The driver accepts LiveCmmTops which use virtual regs, and producesNatCmmTops which use real machine regs. This module also provides functions to help build and deep seq the register conflict graph.
- compiler/nativeGen/RegAllocLinear.hs
Defines the linear scan allocator. Its interface is identical to the coloring allocator.
- compiler/nativeGen/RegAllocInfo.hs
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
- compiler/nativeGen/RegSpillCost.hs
Defines chooseSpill which is responsible for selecting a virtual reg to spill to the stack when not enough real regs are available.
- compiler/nativeGen/RegSpill.hs
Defines regSpill which takes LiveCmmTops 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.
- compiler/nativeGen/RegSpillClean.hs
The spill cleaner is run after registers have been allocated and erases spill/reload instructions inserted by regSpill which weren't strictly nessesary.
- compiler/nativeGen/RegAllocStats.hs
Defines data types and pretty printers used for collecting statistics and debugging info from the coloring allocator.
Graph coloring
- compiler/util/GraphBase.hs
Defines the basic Graph, Node and Triv types used by the coloring algorithm.
- compiler/util/GraphColor.hs
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 [Graph Coloring?].
- compiler/util/GraphOps.hs
Defines functions to perform basic operations on the graphs such as adding, deleting, and coalescing nodes.
- compiler/util/GraphPps.hs
Defines functions for pretty print graphs in human readable-ish and graphviz format.
Miscellanea
- compiler/nativeGen/RegCoalesce.hs
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.
- compiler/nativeGen/RegArchBase.hs
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.
- compiler/nativeGen/RegArchX86.hs
Contains a description of the aliasing constraints between the register sets on x86. This module is not used directly by GHC.
Attachments
-
checkSpills.report
(49.1 KB) - added by guest
6 years ago.
-
checkSpills.tests
(1.5 KB) - added by guest
6 years ago.
list of working nofib benchmarks to run with checkSpills.hs
-
checkSpills.hs
(10.2 KB) - added by guest
6 years ago.
script to help test allocator performance over nofib suite
-
graph-colored.png
(195.5 KB) - added by benl
6 years ago.
example colored register conflict graph
-
graph.png
(81.5 KB) - added by benl
6 years ago.
example colored register conflict graph
-
graph.dot
(4.4 KB) - added by benl
6 years ago.
graphviz source for graph.png
-
graph-colored.dot
(3.6 KB) - added by benl
6 years ago.
graphviz source for graph-colored.png
