寄存器分配与编译器实现的深入探讨
1. 表达式树的寄存器分配
表达式树的寄存器分配相较于任意流图的寄存器分配要简单得多,无需进行全局数据流分析或构建干扰图。下面介绍几种相关的算法。
1.1 简单寄存器分配算法(SimpleAlloc)
function SimpleAlloc(t)
for each nontrivial tile u that is a child of t
SimpleAlloc(u)
for each nontrivial tile u that is a child of t
n ← n - 1
n ← n + 1
assign rn to hold the value at the root of t
该算法按后序遍历树,为每个图块的根分配一个寄存器。以 n
初始化为零,对根(图块 9)应用此算法,会产生如下分配结果: {tile2 → r1, tile4 → r2, tile5 → r2, tile6 → r1, tile8 → r2, tile9 → r1}
。此算法可与 Maximal Munch 结合,因为二者都是自底向上遍历。
不过,该算法并不总是能实现最优分配。例如对于如下树结构:
.+
MEM(NAME a)
*
MEM(NAME b)
MEM(NAME c)