tree: 4b37f5798c834c053faa69050a0a9fa4f3d3721c [path history] [tgz]
  1. gen/
  2. testdata/
  3. biasedsparsemap.go
  4. block.go
  5. branchelim.go
  6. branchelim_test.go
  7. cache.go
  8. check.go
  9. checkbce.go
  10. compile.go
  11. config.go
  12. copyelim.go
  13. copyelim_test.go
  14. critical.go
  15. cse.go
  16. cse_test.go
  17. deadcode.go
  18. deadcode_test.go
  19. deadstore.go
  20. deadstore_test.go
  21. debug.go
  22. debug_test.go
  23. decompose.go
  24. dom.go
  25. dom_test.go
  26. export_test.go
  27. flagalloc.go
  28. func.go
  29. func_test.go
  30. fuse.go
  31. fuse_test.go
  32. html.go
  33. id.go
  34. layout.go
  35. lca.go
  36. lca_test.go
  37. likelyadjust.go
  38. location.go
  39. loop_test.go
  40. loopbce.go
  41. loopreschedchecks.go
  42. looprotate.go
  43. lower.go
  44. magic.go
  45. magic_test.go
  46. nilcheck.go
  47. nilcheck_test.go
  48. numberlines.go
  49. op.go
  50. opGen.go
  51. opt.go
  52. passbm_test.go
  53. phielim.go
  54. phiopt.go
  55. poset.go
  56. poset_test.go
  57. print.go
  58. prove.go
  59. README.md
  60. redblack32.go
  61. redblack32_test.go
  62. regalloc.go
  63. regalloc_test.go
  64. rewrite.go
  65. rewrite386.go
  66. rewrite_test.go
  67. rewriteAMD64.go
  68. rewriteARM.go
  69. rewriteARM64.go
  70. rewritedec.go
  71. rewritedec64.go
  72. rewritegeneric.go
  73. rewriteMIPS.go
  74. rewriteMIPS64.go
  75. rewritePPC64.go
  76. rewriteS390X.go
  77. rewriteWasm.go
  78. schedule.go
  79. schedule_test.go
  80. shift_test.go
  81. shortcircuit.go
  82. shortcircuit_test.go
  83. sizeof_test.go
  84. softfloat.go
  85. sparsemap.go
  86. sparseset.go
  87. sparsetree.go
  88. sparsetreemap.go
  89. stackalloc.go
  90. stackframe.go
  91. stmtlines_test.go
  92. tighten.go
  93. TODO
  94. trim.go
  95. value.go
  96. writebarrier.go
  97. writebarrier_test.go
  98. zcse.go
  99. zeroextension_test.go
src/cmd/compile/internal/ssa/README.md

This package contains the compiler‘s Static Single Assignment form component. If you’re not familiar with SSA, Wikipedia is a good starting point:

https://en.wikipedia.org/wiki/Static_single_assignment_form

SSA is useful to perform transformations and optimizations, which can be found in this package in the form of compiler passes and rewrite rules. The former can be found in the “passes” array in compile.go, while the latter are generated from gen/*.rules.

Like most other SSA forms, funcs consist of blocks and values. Values perform an operation, which is encoded in the form of an operator and a number of arguments. The semantics of each Op can be found in gen/*Ops.go.

gen/* is used to generate code in the ssa package. This includes opGen.go from gen/Ops.go, and the rewrite.go files from gen/*.rules. To regenerate these files, see gen/README.

Blocks can have multiple forms. For example, BlockPlain will always hand the control flow to another block, and BlockIf will flow to one of two blocks depending on a value. See block.go for more details.

Values also have types. For example, a constant boolean value will have a Bool type, and a variable definition value will have a memory type.

The memory type is special - it represents the global memory state. For example, an Op that takes a memory argument depends on that memory state, and an Op which has the memory type impacts the state of memory. This is important so that memory operations are kept in the right order.

For example, take this program:

func f(a, b *int) {
	*a = 3
	*b = *a
}

The two generated stores may show up as follows:

v10 (4) = Store <mem> {int} v6 v8 v1
v14 (5) = Store <mem> {int} v7 v8 v10

Since the second store has a memory argument v10, it cannot be reordered before the first store, which sets that global memory state. And the logic translates to the code; reordering the two assignments would result in a different program.

A good way to see and get used to the compiler‘s SSA in action is via GOSSAFUNC. For example, to see func Foo’s initial SSA form and final generated assembly, one can run:

GOSSAFUNC=Foo go build

The generated ssa.html file will also contain the SSA func at each of the compile passes, making it easy to see what each pass does to a particular program. You can also click on values and blocks to highlight them, to help follow the control flow and values.