1 / 15

Dead Code Elimination

Dead Code Elimination. SSA 架構下的最佳化. SSA Form 下應關心的 Data Structure Statements 這條 statement 所在的 basic block ,它的前一條以及後一條 Statement ,該 statement 所 use 的變數以及 define 的變數 … 等。 Variables 變數的定值點 ( 唯一,因為是 ssa 架構 ) 以及使用點 list 。 Basic Blocks

nelson
Download Presentation

Dead Code Elimination

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dead Code Elimination

  2. 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函式定義非常重要。

  3. Dead Code Elimination • 一個變數在他的定值點被認為是live,若且唯若,該變數的use-list不為空。 • 迭代演算法(simple) While 存在某個沒有使用點的變數v,並且define v 的statement沒有其他的side effect do 刪除define v的這條statement • 刪除vx⊕y 或是vφ(x,y),要將該statement從x以及y的use-list刪去。

  4. 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將會考量到這個問題

  5. 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

  6. Control-Dependence Graph • If y control-dependent x in CFG, then there is an edge xy 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.

  7. (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

  8. (b) reverse control-flow graph. Create CDG r 1 2 3 4 5 6 7 exit

  9. (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

  10. Create CDG • (e) CDG • If x DFG’ [n], then there is an edge xy in CDG r 2 1 4 3 7 5 6

  11. 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,將之刪除

  12. 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

  13. post dominance frontier CDG Aggressive DCE n DFG’ (n) ============ 1 {} 2 {2} 4 {} 5 {2} enter 2 1 4 5

  14. 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

  15. Conclusions • Optimizer不該做改變program行為的變化? 即使是刪除了原本錯誤的地方。 • 只要無限迴圈內沒有輸出,就會被刪掉

More Related