You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.4 KiB

  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. // deleteUnreachableBlocks marks all reachable blocks of f and
  26. // eliminates (nils) all others, including possibly cyclic subgraphs.
  27. //
  28. func deleteUnreachableBlocks(f *Function) {
  29. const white, black = 0, -1
  30. // We borrow b.Index temporarily as the mark bit.
  31. for _, b := range f.Blocks {
  32. b.Index = white
  33. }
  34. markReachable(f.Blocks[0])
  35. if f.Recover != nil {
  36. markReachable(f.Recover)
  37. }
  38. for i, b := range f.Blocks {
  39. if b.Index == white {
  40. for _, c := range b.Succs {
  41. if c.Index == black {
  42. c.removePred(b) // delete white->black edge
  43. }
  44. }
  45. if debugBlockOpt {
  46. fmt.Fprintln(os.Stderr, "unreachable", b)
  47. }
  48. f.Blocks[i] = nil // delete b
  49. }
  50. }
  51. f.removeNilBlocks()
  52. }
  53. // jumpThreading attempts to apply simple jump-threading to block b,
  54. // in which a->b->c become a->c if b is just a Jump.
  55. // The result is true if the optimization was applied.
  56. //
  57. func jumpThreading(f *Function, b *BasicBlock) bool {
  58. if b.Index == 0 {
  59. return false // don't apply to entry block
  60. }
  61. if b.Instrs == nil {
  62. return false
  63. }
  64. if _, ok := b.Instrs[0].(*Jump); !ok {
  65. return false // not just a jump
  66. }
  67. c := b.Succs[0]
  68. if c == b {
  69. return false // don't apply to degenerate jump-to-self.
  70. }
  71. if c.hasPhi() {
  72. return false // not sound without more effort
  73. }
  74. for j, a := range b.Preds {
  75. a.replaceSucc(b, c)
  76. // If a now has two edges to c, replace its degenerate If by Jump.
  77. if len(a.Succs) == 2 && a.Succs[0] == c && a.Succs[1] == c {
  78. jump := new(Jump)
  79. jump.setBlock(a)
  80. a.Instrs[len(a.Instrs)-1] = jump
  81. a.Succs = a.Succs[:1]
  82. c.removePred(b)
  83. } else {
  84. if j == 0 {
  85. c.replacePred(b, a)
  86. } else {
  87. c.Preds = append(c.Preds, a)
  88. }
  89. }
  90. if debugBlockOpt {
  91. fmt.Fprintln(os.Stderr, "jumpThreading", a, b, c)
  92. }
  93. }
  94. f.Blocks[b.Index] = nil // delete b
  95. return true
  96. }
  97. // fuseBlocks attempts to apply the block fusion optimization to block
  98. // a, in which a->b becomes ab if len(a.Succs)==len(b.Preds)==1.
  99. // The result is true if the optimization was applied.
  100. //
  101. func fuseBlocks(f *Function, a *BasicBlock) bool {
  102. if len(a.Succs) != 1 {
  103. return false
  104. }
  105. b := a.Succs[0]
  106. if len(b.Preds) != 1 {
  107. return false
  108. }
  109. // Degenerate &&/|| ops may result in a straight-line CFG
  110. // containing φ-nodes. (Ideally we'd replace such them with
  111. // their sole operand but that requires Referrers, built later.)
  112. if b.hasPhi() {
  113. return false // not sound without further effort
  114. }
  115. // Eliminate jump at end of A, then copy all of B across.
  116. a.Instrs = append(a.Instrs[:len(a.Instrs)-1], b.Instrs...)
  117. for _, instr := range b.Instrs {
  118. instr.setBlock(a)
  119. }
  120. // A inherits B's successors
  121. a.Succs = append(a.succs2[:0], b.Succs...)
  122. // Fix up Preds links of all successors of B.
  123. for _, c := range b.Succs {
  124. c.replacePred(b, a)
  125. }
  126. if debugBlockOpt {
  127. fmt.Fprintln(os.Stderr, "fuseBlocks", a, b)
  128. }
  129. f.Blocks[b.Index] = nil // delete b
  130. return true
  131. }
  132. // optimizeBlocks() performs some simple block optimizations on a
  133. // completed function: dead block elimination, block fusion, jump
  134. // threading.
  135. //
  136. func optimizeBlocks(f *Function) {
  137. deleteUnreachableBlocks(f)
  138. // Loop until no further progress.
  139. changed := true
  140. for changed {
  141. changed = false
  142. if debugBlockOpt {
  143. f.WriteTo(os.Stderr)
  144. mustSanityCheck(f, nil)
  145. }
  146. for _, b := range f.Blocks {
  147. // f.Blocks will temporarily contain nils to indicate
  148. // deleted blocks; we remove them at the end.
  149. if b == nil {
  150. continue
  151. }
  152. // Fuse blocks. b->c becomes bc.
  153. if fuseBlocks(f, b) {
  154. changed = true
  155. }
  156. // a->b->c becomes a->c if b contains only a Jump.
  157. if jumpThreading(f, b) {
  158. changed = true
  159. continue // (b was disconnected)
  160. }
  161. }
  162. }
  163. f.removeNilBlocks()
  164. }