160 likes | 327 Views
Dead Code Elimination. SSA 架構下的最佳化. SSA Form 下應關心的 Data Structure Statements 這條 statement 所在的 basic block ,它的前一條以及後一條 Statement ,該 statement 所 use 的變數以及 define 的變數 … 等。 Variables 變數的定值點 ( 唯一,因為是 ssa 架構 ) 以及使用點 list 。 Basic Blocks
E N D
SSA架構下的最佳化 • SSA Form下應關心的Data Structure • Statements • 這條statement所在的basic block,它的前一條以及後一條Statement,該statement所use的變數以及define的變數…等。 • Variables • 變數的定值點(唯一,因為是ssa架構) 以及使用點list。 • Basic Blocks • 一個BB包含statement list,predecessor list和一個successor(條件分支結尾則不只一個) 。 • Predecessor的順序對該BB中的phi函式定義非常重要。
Dead Code Elimination • 一個變數在他的定值點被認為是live,若且唯若,該變數的use-list不為空。 • 迭代演算法(simple) While 存在某個沒有使用點的變數v,並且define v 的statement沒有其他的side effect do 刪除define v的這條statement • 刪除vx⊕y 或是vφ(x,y),要將該statement從x以及y的use-list刪去。
1 2 k2 φ(k3, 0) If k2 < 100 5 4 K3 K2 + 1 Return 1 Aggressive DCE • 一般的(conservative)DCE,會斷定 • K2 live 因為在定義k3的時候有用到 • K3 live 因為在定義k2的時候有用到 • 實際上k2,k3的值為何,對結果並沒有影響 • Aggressive 的DCE將會考量到這個問題
Node y control-dependent node x 定義 X可走到u或v V要有到exit一定要經過y U要走到exit則有一條可不用經過y Function的return就算是exit x v u y exit Control-Dependence
Control-Dependence Graph • If y control-dependent x in CFG, then there is an edge xy in it’s CDG. • If every path from v to exit pass through y, we say that y is a post-dominator of v. i.e., y is a dominator of v in reverse control-flow graph.
(a) CFG New entry node r New edge r to s New edge r to exit Create CDG r 1 2 3 4 5 6 7 exit
(b) reverse control-flow graph. Create CDG r 1 2 3 4 5 6 7 exit
(c) post-dominator tree (d) post dominance frontier Create CDG n DFG’ (n) ============ r {} 1 {r} 2 {2, r} 3 {2} 4 {r} 5 {3} 6 {3} 7 {2} 5 3 6 7 1 2 4 r exit
Create CDG • (e) CDG • If x DFG’ [n], then there is an edge xy in CDG r 2 1 4 3 7 5 6
Aggressive DCE • Algorithm • 將所有的statement預設為dead • 將以下statement標記為live • I/O,暫存器的儲存,函數return,呼叫另一個可能有side effect的函數之statement • 對其他的live statement所使用之變數做define的statement • 是一個condition statement,而且其他live的statement control-dependent此condition statement • dead之statement,將之刪除
SSA Form post-dominator tree 1 5 1 2 2 K2 φ(k3, 0) If k2 < 100 4 5 4 K3 K2 + 1 Return 1 exit Aggressive DCE
post dominance frontier CDG Aggressive DCE n DFG’ (n) ============ 1 {} 2 {2} 4 {} 5 {2} enter 2 1 4 5
BB4包含了return所以是live 並沒有live的BB control-dependent於BB2 沒有live的assign statement dependent on k2, k3 所以除了BB4以外 沒有其他live的BB或是statement了 Aggressive DCE 2 4 Return 1
Conclusions • Optimizer不該做改變program行為的變化? 即使是刪除了原本錯誤的地方。 • 只要無限迴圈內沒有輸出,就會被刪掉