Skip to content

Instruction Selection

:material-circle-edit-outline: 约 570 个字 :material-clock-time-two-outline: 预计阅读时间 2 分钟

开始将规范树转化为汇编代码了

image-20250422201420506

Instruction Selection:apping IR into abstract assembly code

Register Allocation:Deciding which values will reside in registers,Replace abstract registers with real register

Instruction Selection: Why and What

The intermediate representation (Tree) language expresses only one operation in each tree node.

Areal machine instruction can often perform several of primitive operations.

image-20250422205927567

The instruction selection phase: Finding the appropriate machine instructions to implement a given intermediate representation tree

Tree Patterns

Each machine instruction can be expressed a fragment of an IR tree, called a tree pattern.

Instruction selection: tiling the tree with a minimal set of tree patterns.

Tiling: cover the tree with nonoverlapping tree patterns

The tiles are the set of tree patterns corresponding to legal machine instructions

image-20250422210842109

To illustrate instruction selection, we use the Jouette architecture invented in the textbook

The Jouette architecture

image-20250422210711594

Specification about Jouette Architecture

Register r0 always contains zero

Some instructions correspond to more than one tree pattern

Tree Patterns- Example

image-20250422210917462

It is always possible to tile the tree with tiny tiles, each covering only one node

image-20250422210939895

Optimal and Optimum Tilings

  • Best tiling: an instruction sequence of least cost
    • the shortest sequence of instructions
    • Orthe least-cost sequence has the lowest total time if the instructions take different amount of time to execute.
  • Optimum tiling: the one whose tiles sum to the lowest possible value
    • 全局最优,cost 最小的覆盖
  • Optimal tiling: the one where no two adjacent tiles can be combined into a single tile of lower cost
    • 局部最优,如果选择任意两个瓦片进行合并,代价没有变得更低,那么当前的就是最佳(Optimal)覆盖
  • Every optimum tiling is also optimal, but not vice versa

image-20250422211351723

Algorithms for Instruction Selection

Maximal Munch

always finds an optimal tiling, but not necessarily an optimum

  • Assume larger tiles = better
  • Main idea: Greedy algorithm
    • 自顶向下遍历 IR tree,用尽可能大的瓦片进行覆盖当前的结点,再递归检查剩下的
    • 能保证任意两个瓦片都不能合并

Largest tile: the one with the most nodes

  • 具体流程
    1. Starting at the root of the tree, find the largest tile that fits.
    2. Cover the root node– and perhaps several other nodes near the root– with this tile, leaving several subtrees.
    3. Repeat the same algorithm for each subtree.

The instructions are generated in reverse order

If twotiles of equal size match at the root, then the choice between them is arbitrary

image-20250428160007175

Dynamic Programming

can find the optimum based on the optimum solution of each subproblem

  • It works bottom-up
  • Assign a cost to every node in the tree

image-20250428160452799

image-20250428204228175

image-20250428204725589

image-20250428204734112

image-20250428204406567

image-20250428205448751

Tree Grammar

tree patterns 可能会非常多,非常复炸,人工实现 tiling 算法很麻烦

我们就希望用 Tree Grammar 描述 tree patterns,使用一个通用的动态规划算法来解析 Tree Grammar 生成 inst selector

CISC Machines

我们之前都是 RISC 机器,如果是 CISC 机器有更多问题要解决

image-20250428210353041

Instruction Selection for the Tiger Compiler