介紹
這個論文提出了一種簡單高效率的插入-node的方法,也指出了傳統插入-node算法的一些弊端。
注:這個論文還有一些前置論文,我懶得看了
想要解決的問題
論文想要解決的是在計算dominance frontier時候潛在的的複雜度。論文指出計算-node插入位置可以在線性時間內完成,核心就在於處理dominator tree的順序,同時這種方式還可以on-the-fly的方式計算dominance frontier。
該論文使用了一種的結構來作爲整個算法的基礎。DJ-Graph的本質上是在dominator tree上添加了J-edge(join-edge),
The tree skeleton is augmented with J-edges (join edges) that correspond to all edges of the CFG whose source does not strictly dominate its destination. - Static Single Assignment Book
注:上圖源於Static Single Assignment Book
傳統placing -node算法回顧
在構造SSA介紹的插入-node的算法比較粗糙,還沒有考慮到live信息,效率也比較低。
注:上圖來自於Data Flow Analysis Theory and Pratice
這個算法有兩個特點,一是預先計算好所有的dominance frontier信息,二是迭代的方式插入-node的效率比較低。
背景知識
有兩點背景知識以前沒有接觸過,一個是dominance frontier的拓展,從一個節點的 拓展到一個節點集合 。
另一個是iterated dominance frontier 或者()(這也是我爲什麼看llvm的代碼IDFCalculatorBase
看不懂的原因 😃),是通過迭代計算得到的,其實也就是的傳遞閉包。
其實在傳統的-node插入算法中,迭代就是爲了計算這個。
另外,對於,所有 DT上的ancestors(包括)也不會strictly dominate ,也就是也在這些ancestor的DF集合中。例如Fig3.3中,(, )是一個,所有{(, ), (, ), (, )}也是。
那麼和的關係是可以有簡單的推出來。
核心實現
首先有幾個需要在着重強調的特性,
線性時間構造DJ-graph
注:上圖來源於論文
首先以dominator tree作爲骨架,第一點就是在其上添加join edges,例如我們要爲Figure 2中的節點2附着join edge,首先在flowgraph中找到destination爲節點2的邊,例如和,但是支配,所以我們在dominator tree加上。只要我們考察完flowgraph所有的邊,再結合dominator tree就可以在構造出。
有以下三個屬性:
- 前面我們已經探討了 edge 和dominance frontier的關係,例如對於,在所有及其ancestor的集合中。
- 對於(同樣),在dominator tree中的level永遠小於等於。這是整篇論文的關鍵,換句話說,如果我們要找的dominance fontier,只找level值小於等於的節點就夠了。
- ,當且僅當存在,並且存在一條 同時的level值小於等於的level值。
computing dominance frontier
論文推出了一條引理,
Lemma 1 : A node iff there exists a with as a and
通過上面的引理論文給出了一個計算dominance frontier的算法,
例如我們要計算Figure 2中節點的dominance frontier,首先,有。而其中節點,滿足上面的引理,所以。
該篇論文算法的另一個核心就是順序,例如我們要計算,因爲,所以我們在計算dominance frontier時,節點的被處理了兩遍,所以在計算dominance frontier時按照dominator tree的level從下到上處理。
如下圖所示,在處理之前,已經計算出來了。
注:上圖來自與Static Single Assignment Book
插入-node
在我們得到之後,就可以計算-node插入的位置。這裏的算法使用《Static Single Assignment Book》的描述。
例如對進行定義的節點有,,,。首先算法使用一個來組織這些節點,然後按照depth從大到小處理以這些節點爲起始點的-edge,如果這個edge滿足引理Lemma 1,則把-edge的終止節點加入中。
這篇論文的算法針對《構造SSA》的改進有以下幾點:
- 把計算dominance frontier的粒度從單個節點擴展到一個節點集合。例如對於變量的通常也是一個節點集合。
- 不需要預先計算dominance frontier,可以on-the-fly地計算dominance frontier
- 通過-edge,以bottom up的方式地進行處理,保證每個節點每條邊只處理一遍沒提升了效率
效果及使用情況
通過論文作者的描述,該算法實現了5倍的提升。llvm最開始的時候使用的是Cytron的算法,後來就使用本論文中的算法,見GenericIteratedDominanceFrontier.h。
//===- IteratedDominanceFrontier.h - Calculate IDF ------------*- C++ -*-===//
//
// Compute iterated dominance frontiers using a linear time algorithm.
//
// The algorithm used here is based on:
//
// Sreedhar and Gao. A linear time algorithm for placing phi-nodes.
// In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of
// Programming Languages
// POPL '95. ACM, New York, NY, 62-73.
//
// It has been modified to not explicitly use the DJ graph data structure and
// to directly compute pruned SSA using per-veriable liveness information.
//
//===--------------------------------------------------------------------===//