論文 A Linear Time Algorithm for Placing phi-Nodes:閱讀筆記

介紹

這個論文提出了一種簡單高效率的插入ϕ\phi-node的方法,也指出了傳統插入ϕ\phi-node算法的一些弊端。
注:這個論文還有一些前置論文,我懶得看了

想要解決的問題

論文想要解決的是在計算dominance frontier時候潛在的O(N2)O(N^2)的複雜度。論文指出計算ϕ\phi-node插入位置可以在線性時間內完成,核心就在於處理dominator tree的順序,同時這種方式還可以on-the-fly的方式計算dominance frontier。

該論文使用了一種DJgraphDJ-graph的結構來作爲整個算法的基礎。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

DJ
注:上圖源於Static Single Assignment Book

傳統placing ϕ\phi-node算法回顧

構造SSA介紹的插入ϕ\phi-node的算法比較粗糙,還沒有考慮到live信息,效率也比較低。
標準SSA插入
注:上圖來自於Data Flow Analysis Theory and Pratice

這個算法有兩個特點,一是預先計算好所有的dominance frontier信息,二是迭代的方式插入ϕ\phi-node的效率比較低。

背景知識

有兩點背景知識以前沒有接觸過,一個是dominance frontier的拓展,從一個節點的DF(x)DF(x) 拓展到一個節點集合 DF(S)DF(S)

DF(S)=xSDF(x)DF(S) = \bigcup_{x \in S} DF(x)

另一個是iterated dominance frontier IDF(S)IDF(S)或者(DF+(SDF^+(S)(這也是我爲什麼看llvm的代碼IDFCalculatorBase看不懂的原因 😃),IDS(S)IDS(S)是通過迭代計算DF(S)DF(S)得到的,其實也就是DFDF的傳遞閉包。

IDF1(S)=DF(S)IDFi+1=DF(SIDFi(S))IDF_1(S) = DF(S) \\ IDF_{i+1} = DF(S \cup IDF_i(S))

其實在傳統的ϕ\phi-node插入算法中,迭代就是爲了計算這個IDF(S)IDF(S)

另外,對於Jedge(a,b)J-edge(a, b),所有aa DT上的ancestors(包括aa)也不會strictly dominate bb,也就是bb也在這些ancestor的DF集合中。例如Fig3.3中,(FF, GG)是一個JedgeJ-edge,所有{(FF, GG), (EE, GG), (BB, GG)}也是DFedgeDF-edge

那麼JedgeJ-edgeDFDF的關係是DFDF可以有簡單的JedgeJ-edge推出來。

核心實現

首先DJgraphDJ-graph有幾個需要在着重強調的特性,

線性時間構造DJ-graph

flowgraph
DJ
注:上圖來源於論文

首先DJgraphDJ-graph以dominator tree作爲骨架,第一點就是在其上添加join edges,例如我們要爲Figure 2中的節點2附着join edge,首先在flowgraph中找到destination爲節點2的邊,例如121 \rightarrow 2262 \rightarrow 6,但是11支配22,所以我們在dominator tree加上626 \rightarrow 2。只要我們考察完flowgraph所有的邊,再結合dominator tree就可以在構造出DJgraphDJ-graph

DJgraphDJ-graph有以下三個屬性:

  • 前面我們已經探討了JJ edge 和dominance frontier的關係,例如對於Jedge(a,b)J-edge (a, b)bb在所有aa及其ancestor的DFDF集合中。
  • 對於yDF(x)y \in DF(x)(同樣yIDF(x)y \in IDF(x)),yy在dominator tree中的level永遠小於等於xx。這是整篇論文的關鍵,換句話說,如果我們要找xx的dominance fontier,只找level值小於等於xx的節點就夠了。
  • yDF(x)y \in DF(x),當且僅當存在zSubTree(x)z \in SubTree(x),並且存在一條JedgeJ-edge zyz \rightarrow y同時yy的level值小於等於xx的level值。

computing dominance frontier

論文推出了一條引理,

Lemma 1 : A node zDF(x)z \in DF(x) iff there exists a ySubTree(x)y \in SubTree(x) with yzy \rightarrow z as a JedgeJ-edge and z.levelx.levelz.level \le x.level

通過上面的引理論文給出了一個計算dominance frontier的算法,
算法

例如我們要計算Figure 2中節點33的dominance frontier,首先SubTree(3)=3,9,10,11,12,13,14SubTree(3) = {3, 9, 10, 11, 12, 13, 14}JedgeJ-edge1012,1112,133,1315,1412{10 \rightarrow 12, 11 \rightarrow 12, 13 \rightarrow 3, 13 \rightarrow 15, 14 \rightarrow 12}。而其中節點331515滿足上面的引理,所以DF(3)=3,15DF(3) = {3, 15}

該篇論文算法的另一個核心就是順序,例如我們要計算DF(9,12)DF({9, 12}),因爲12SubTree(9)12 \in SubTree(9),所以我們在計算dominance frontier時,節點1212SubTreeSubTree被處理了兩遍,所以在計算dominance frontier時按照dominator tree的level從下到上處理。

如下圖所示,在處理DF(w)DF(w)之前,DF(x)DF(x)已經計算出來了。
示意圖
注:上圖來自與Static Single Assignment Book

插入ϕ\phi-node

在我們得到DJgraphDJ-graph之後,就可以計算ϕ\phi-node插入的位置。這裏的算法使用《Static Single Assignment Book》的描述。

算法

例如對vv進行定義的節點有11334477。首先算法使用一個OrderedBucketOrderedBucket來組織這些節點,然後按照depth從大到小處理以這些節點爲起始點的JJ-edge,如果這個edge滿足引理Lemma 1,則把JJ-edge的終止節點加入DF(1,3,4,7)DF({1, 3, 4, 7})中。

這篇論文的算法針對《構造SSA》的改進有以下幾點:

  • 把計算dominance frontier的粒度從單個節點擴展到一個節點集合。例如對於變量xxdefdef通常也是一個節點集合。
  • 不需要預先計算dominance frontier,可以on-the-fly地計算dominance frontier
  • 通過JJ-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.
//
//===--------------------------------------------------------------------===//
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章