blockopt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssa
  5. // Simple block optimizations to simplify the control flow graph.
  6. // TODO(adonovan): opt: instead of creating several "unreachable" blocks
  7. // per function in the Builder, reuse a single one (e.g. at Blocks[1])
  8. // to reduce garbage.
  9. import (
  10. "fmt"
  11. "os"
  12. )
  13. // If true, perform sanity checking and show progress at each
  14. // successive iteration of optimizeBlocks. Very verbose.
  15. const debugBlockOpt = false
  16. // markReachable sets Index=-1 for all blocks reachable from b.
  17. func markReachable(b *BasicBlock) {
  18. b.Index = -1
  19. for _, succ := range b.Succs {
  20. if succ.Index == 0 {
  21. markReachable(succ)
  22. }
  23. }
  24. }
  25. func DeleteUnreachableBlocks(f *Function) {
  26. deleteUnreachableBlocks(f)
  27. }
  28. // deleteUnreachableBlocks marks all reachable blocks of f and
  29. // eliminates (nils) all others, including possibly cyclic subgraphs.
  30. //
  31. func deleteUnreachableBlocks(f *Function) {
  32. const white, black = 0, -1
  33. // We borrow b.Index temporarily as the mark bit.
  34. for _, b := range f.Blocks {
  35. b.Index = white
  36. }
  37. markReachable(f.Blocks[0])
  38. if f.Recover != nil {
  39. markReachable(f.Recover)
  40. }
  41. for i, b := range f.Blocks {
  42. if b.Index == white {
  43. for _, c := range b.Succs {
  44. if c.Index == black {
  45. c.removePred(b) // delete white->black edge
  46. }
  47. }
  48. if debugBlockOpt {
  49. fmt.Fprintln(os.Stderr, "unreachable", b)
  50. }
  51. f.Blocks[i] = nil // delete b
  52. }
  53. }
  54. f.removeNilBlocks()
  55. }
  56. // jumpThreading attempts to apply simple jump-threading to block b,
  57. // in which a->b->c become a->c if b is just a Jump.
  58. // The result is true if the optimization was applied.
  59. //
  60. func jumpThreading(f *Function, b *BasicBlock) bool {
  61. if b.Index == 0 {
  62. return false // don't apply to entry block
  63. }
  64. if b.Instrs == nil {
  65. return false
  66. }
  67. if _, ok := b.Instrs[0].(*Jump); !ok {
  68. return false // not just a jump
  69. }
  70. c := b.Succs[0]
  71. if c == b {
  72. return false // don't apply to degenerate jump-to-self.
  73. }
  74. if c.hasPhi() {
  75. return false // not sound without more effort
  76. }
  77. for j, a := range b.Preds {
  78. a.replaceSucc(b, c)
  79. // If a now has two edges to c, replace its degenerate If by Jump.
  80. if len(a.Succs) == 2 && a.Succs[0] == c && a.Succs[1] == c {
  81. jump := new(Jump)
  82. jump.setBlock(a)
  83. a.Instrs[len(a.Instrs)-1] = jump
  84. a.Succs = a.Succs[:1]
  85. c.removePred(b)
  86. } else {
  87. if j == 0 {
  88. c.replacePred(b, a)
  89. } else {
  90. c.Preds = append(c.Preds, a)
  91. }
  92. }
  93. if debugBlockOpt {
  94. fmt.Fprintln(os.Stderr, "jumpThreading", a, b, c)
  95. }
  96. }
  97. f.Blocks[b.Index] = nil // delete b
  98. return true
  99. }
  100. // fuseBlocks attempts to apply the block fusion optimization to block
  101. // a, in which a->b becomes ab if len(a.Succs)==len(b.Preds)==1.
  102. // The result is true if the optimization was applied.
  103. //
  104. func fuseBlocks(f *Function, a *BasicBlock) bool {
  105. if len(a.Succs) != 1 {
  106. return false
  107. }
  108. b := a.Succs[0]
  109. if len(b.Preds) != 1 {
  110. return false
  111. }
  112. // Degenerate &&/|| ops may result in a straight-line CFG
  113. // containing φ-nodes. (Ideally we'd replace such them with
  114. // their sole operand but that requires Referrers, built later.)
  115. if b.hasPhi() {
  116. return false // not sound without further effort
  117. }
  118. // Eliminate jump at end of A, then copy all of B across.
  119. a.Instrs = append(a.Instrs[:len(a.Instrs)-1], b.Instrs...)
  120. for _, instr := range b.Instrs {
  121. instr.setBlock(a)
  122. }
  123. // A inherits B's successors
  124. a.Succs = append(a.succs2[:0], b.Succs...)
  125. // Fix up Preds links of all successors of B.
  126. for _, c := range b.Succs {
  127. c.replacePred(b, a)
  128. }
  129. if debugBlockOpt {
  130. fmt.Fprintln(os.Stderr, "fuseBlocks", a, b)
  131. }
  132. f.Blocks[b.Index] = nil // delete b
  133. return true
  134. }
  135. func OptimizeBlocks(f *Function) {
  136. optimizeBlocks(f)
  137. }
  138. // optimizeBlocks() performs some simple block optimizations on a
  139. // completed function: dead block elimination, block fusion, jump
  140. // threading.
  141. //
  142. func optimizeBlocks(f *Function) {
  143. deleteUnreachableBlocks(f)
  144. // Loop until no further progress.
  145. changed := true
  146. for changed {
  147. changed = false
  148. if debugBlockOpt {
  149. f.WriteTo(os.Stderr)
  150. mustSanityCheck(f, nil)
  151. }
  152. for _, b := range f.Blocks {
  153. // f.Blocks will temporarily contain nils to indicate
  154. // deleted blocks; we remove them at the end.
  155. if b == nil {
  156. continue
  157. }
  158. // Fuse blocks. b->c becomes bc.
  159. if fuseBlocks(f, b) {
  160. changed = true
  161. }
  162. // a->b->c becomes a->c if b contains only a Jump.
  163. if jumpThreading(f, b) {
  164. changed = true
  165. continue // (b was disconnected)
  166. }
  167. }
  168. }
  169. f.removeNilBlocks()
  170. }