Register Allocation
Register allocation is an NP-complete problem
Graph coloring is also NP-complete
Coloring By Simplification
A linear-time approximation algorithm that gives good results
Build
Construct the interference graphs
- Each node represents a temporary value
- An edge (t1, t2) indicates a pair of temporaries that cannot be assigned to the same register
Simplify
Color the graph using a simple heuristic
Suppose the graph G contains a node m with fewer than K neighbors (K: the number of machine registers)
Let G’ be the graph G – {m} obtained by removing m, if G’ can be colored, then so can G
即,一个结点的邻居数量少于 K,那么我染色完它的邻居,一定能留一个寄存器给它
This lead naturally to a stack-based (or recursive) algorithm for coloring
- Repeatedly remove (and push on a stack) nodes of degree less than K
- Each such simplification will decrease the degrees of other nodes, leading to more opportunities for simplification
A node with degree < K is always K-colorable
Recursively remove k-colorable nodes from the graph and push them to a stack, until the graph contains nodes only of significant degree (that is, nodes of degree ≥ K)
Spill
Spilling: We MAY choose some node in the graph and decide to represent it in memory, not registers
Optimistic Coloring:An optimistic approximation to the effect of spilling: the spilled node does not interfere with any of the other nodes remaining in the graph
It can therefore be removed and pushed on the stack, and the simplify process continued.
spill 后继续重复 Simplify,循环两个步骤,直到生成空图
Select
Assign colors to nodes in the graph
Starting with the empty graph, we rebuild the original graph by repeatedly adding a node from the top of the stack
When we add a node to the graph, there must be acolor for it.
这里的 b 就满足了乐观染色,虽然它被设置为了 Potential Spill,此时已有两个邻居染色,但这里要染它时是由空闲颜色的,所以可以染色
如果一个结点已经没有颜色能分配了,那就是 actual spill
每次发现 actual spill 就得对程序进行重写:
- fetch them from memory just before each use
- and store them back after each def.
Thus, a spilled temporary will turn into several new temporaries with tiny live ranges.
These will interfere with other temporaries in the graph
So the algorithm is repeated on this rewritten program. This process iterates until simplify succeeds with no spills.
In practice, one or two iterations almost always suffice
Chapter11.pdf 35 页
Coalescing
If there is no edge in the interference graph between the source and destination of a MOVE instruction, the MOVE can be eliminated.
因为两者能使用同一个寄存器
The source and destination nodes are coalesced into a new node whose edges are the union of those of the nodes being replaced.
但是新节点的邻居变多了,可能超出寄存器数量
有两种具体的实现方式
-
Briggs
- Nodes a and b can be coalesced if the resulting node ab will have fewer than K neighbors of significant degree (i.e., having >= K edges)
- 非 significant degree 的邻居可以先被 simplify 掉,剩下少于 K 个邻居,于是新节点也可以被 simplify 掉
-
George
- Nodes a and b can be coalesced if for every neighbor t of a, either t already interferes with b or t is of insignificant degree (< K).
- 符合第一个条件的边不会被重复增加至新节点,符合第二个条件的边能被优化,于是新节点的边被压制在合并前的数量
这里需要额外区分 move 相关的边
将虚线变为实线,放弃尝试对该虚线进行合并
Precolored Nodes
Some registers are used for special purpose
For each of such registers, use the particular temporary that is permanently bound to that register. (E.g., FP)
Such temporaries are precolored
- Only one precolored node of each color
- Precolored nodes all interfere with each other.
It is common to give an ordinary temporary the same color as a precolored register, as long as they don’t interfere
- We cannot simplify a precoloared node
- We should not spill precolored nodes to memory
现在我们的 simplify, coalesce, and spill 目标就不是将图变为空图,而是变为只剩下预着色结点的图
后面还没看完