Debian Codegraph @

src/cmd/compile/internal/ssa/README.md

raw
1 <!---
2 // Copyright 2018 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5 -->
6
7 ## Introduction to the Go compiler's SSA backend
8
9 This package contains the compiler's Static Single Assignment form component. If
10 you're not familiar with SSA, its [Wikipedia
11 article](https://en.wikipedia.org/wiki/Static_single_assignment_form) is a good
12 starting point.
13
14 It is recommended that you first read [cmd/compile/README.md](../../README.md)
15 if you are not familiar with the Go compiler already. That document gives an
16 overview of the compiler, and explains what is SSA's part and purpose in it.
17
18 ### Key concepts
19
20 The names described below may be loosely related to their Go counterparts, but
21 note that they are not equivalent. For example, a Go block statement has a
22 variable scope, yet SSA has no notion of variables nor variable scopes.
23
24 It may also be surprising that values and blocks are named after their unique
25 sequential IDs. They rarely correspond to named entities in the original code,
26 such as variables or function parameters. The sequential IDs also allow the
27 compiler to avoid maps, and it is always possible to track back the values to Go
28 code using debug and position information.
29
30 #### Values
31
32 Values are the basic building blocks of SSA. Per SSA's very definition, a
33 value is defined exactly once, but it may be used any number of times. A value
34 mainly consists of a unique identifier, an operator, a type, and some arguments.
35
36 An operator or `Op` describes the operation that computes the value. The
37 semantics of each operator can be found in `_gen/*Ops.go`. For example, `OpAdd8`
38 takes two value arguments holding 8-bit integers and results in their addition.
39 Here is a possible SSA representation of the addition of two `uint8` values:
40
41 // var c uint8 = a + b
42 v4 = Add8 <uint8> v2 v3
43
44 A value's type will usually be a Go type. For example, the value in the example
45 above has a `uint8` type, and a constant boolean value will have a `bool` type.
46 However, certain types don't come from Go and are special; below we will cover
47 `memory`, the most common of them.
48
49 Some operators contain an auxiliary field. The aux fields are usually printed as
50 enclosed in `[]` or `{}`, and could be the constant op argument, argument type,
51 etc.
52 for example:
53
54 v13 (?) = Const64 <int> [1]
55
56 Here the aux field is the constant op argument, the op is creating a `Const64`
57 value of 1. One more example:
58
59 v17 (361) = Store <mem> {int} v16 v14 v8
60
61 Here the aux field is the type of the value being `Store`ed, which is int.
62
63 See [value.go](value.go) and `_gen/*Ops.go` for more information.
64
65 #### Memory types
66
67 `memory` represents the global memory state. An `Op` that takes a memory
68 argument depends on that memory state, and an `Op` which has the memory type
69 impacts the state of memory. This ensures that memory operations are kept in the
70 right order. For example:
71
72 // *a = 3
73 // *b = *a
74 v10 = Store <mem> {int} v6 v8 v1
75 v14 = Store <mem> {int} v7 v8 v10
76
77 Here, `Store` stores its second argument (of type `int`) into the first argument
78 (of type `*int`). The last argument is the memory state; since the second store
79 depends on the memory value defined by the first store, the two stores cannot be
80 reordered.
81
82 See [cmd/compile/internal/types/type.go](../types/type.go) for more information.
83
84 #### Blocks
85
86 A block represents a basic block in the control flow graph of a function. It is,
87 essentially, a list of values that define the operation of this block. Besides
88 the list of values, blocks mainly consist of a unique identifier, a kind, and a
89 list of successor blocks.
90
91 The simplest kind is a `plain` block; it simply hands the control flow to
92 another block, thus its successors list contains one block.
93
94 Another common block kind is the `exit` block. These have a final value, called
95 control value, which must return a memory state. This is necessary for functions
96 to return some values, for example - the caller needs some memory state to
97 depend on, to ensure that it receives those return values correctly.
98
99 The last important block kind we will mention is the `if` block. It has a single
100 control value that must be a boolean value, and it has exactly two successor
101 blocks. The control flow is handed to the first successor if the bool is true,
102 and to the second otherwise.
103
104 Here is a sample if-else control flow represented with basic blocks:
105
106 // func(b bool) int {
107 // if b {
108 // return 2
109 // }
110 // return 3
111 // }
112 b1:
113 v1 = InitMem <mem>
114 v2 = SP <uintptr>
115 v5 = Addr <*int> {~r1} v2
116 v6 = Arg <bool> {b}
117 v8 = Const64 <int> [2]
118 v12 = Const64 <int> [3]
119 If v6 -> b2 b3
120 b2: <- b1
121 v10 = VarDef <mem> {~r1} v1
122 v11 = Store <mem> {int} v5 v8 v10
123 Ret v11
124 b3: <- b1
125 v14 = VarDef <mem> {~r1} v1
126 v15 = Store <mem> {int} v5 v12 v14
127 Ret v15
128
129 <!---
130 TODO: can we come up with a shorter example that still shows the control flow?
131 -->
132
133 See [block.go](block.go) for more information.
134
135 #### Functions
136
137 A function represents a function declaration along with its body. It mainly
138 consists of a name, a type (its signature), a list of blocks that form its body,
139 and the entry block within said list.
140
141 When a function is called, the control flow is handed to its entry block. If the
142 function terminates, the control flow will eventually reach an exit block, thus
143 ending the function call.
144
145 Note that a function may have zero or multiple exit blocks, just like a Go
146 function can have any number of return points, but it must have exactly one
147 entry point block.
148
149 Also note that some SSA functions are autogenerated, such as the hash functions
150 for each type used as a map key.
151
152 For example, this is what an empty function can look like in SSA, with a single
153 exit block that returns an uninteresting memory state:
154
155 foo func()
156 b1:
157 v1 = InitMem <mem>
158 Ret v1
159
160 See [func.go](func.go) for more information.
161
162 ### Compiler passes
163
164 Having a program in SSA form is not very useful on its own. Its advantage lies
165 in how easy it is to write optimizations that modify the program to make it
166 better. The way the Go compiler accomplishes this is via a list of passes.
167
168 Each pass transforms a SSA function in some way. For example, a dead code
169 elimination pass will remove blocks and values that it can prove will never be
170 executed, and a nil check elimination pass will remove nil checks which it can
171 prove to be redundant.
172
173 Compiler passes work on one function at a time, and by default run sequentially
174 and exactly once.
175
176 The `lower` pass is special; it converts the SSA representation from being
177 machine-independent to being machine-dependent. That is, some abstract operators
178 are replaced with their non-generic counterparts, potentially reducing or
179 increasing the final number of values.
180
181 <!---
182 TODO: Probably explain here why the ordering of the passes matters, and why some
183 passes like deadstore have multiple variants at different stages.
184 -->
185
186 See the `passes` list defined in [compile.go](compile.go) for more information.
187
188 ### Playing with SSA
189
190 A good way to see and get used to the compiler's SSA in action is via
191 `GOSSAFUNC`. For example, to see func `Foo`'s initial SSA form and final
192 generated assembly, one can run:
193
194 GOSSAFUNC=Foo go build
195
196 The generated `ssa.html` file will also contain the SSA func at each of the
197 compile passes, making it easy to see what each pass does to a particular
198 program. You can also click on values and blocks to highlight them, to help
199 follow the control flow and values.
200
201 The value specified in GOSSAFUNC can also be a package-qualified function
202 name, e.g.
203
204 GOSSAFUNC=blah.Foo go build
205
206 This will match any function named "Foo" within a package whose final
207 suffix is "blah" (e.g. something/blah.Foo, anotherthing/extra/blah.Foo).
208
209 The users may also print the Control Flow Graph(CFG) by specifying in
210 `GOSSAFUNC` value in the following format:
211
212 GOSSAFUNC="$FunctionName:$PassName1,$PassName2,..." go build
213
214 For example, the following command will print SSA with CFGs attached to the
215 `sccp` and `generic deadcode` pass columns:
216
217 GOSSAFUNC="blah.Foo:sccp,generic deadcode" go build
218
219 If non-HTML dumps are needed, append a "+" to the GOSSAFUNC value
220 and dumps will be written to stdout:
221
222 GOSSAFUNC=Bar+ go build
223
224 <!---
225 TODO: need more ideas for this section
226 -->
227
228 ### Hacking on SSA
229
230 While most compiler passes are implemented directly in Go code, some others are
231 code generated. This is currently done via rewrite rules, which have their own
232 syntax and are maintained in `_gen/*.rules`. Simpler optimizations can be written
233 easily and quickly this way, but rewrite rules are not suitable for more complex
234 optimizations.
235
236 To read more on rewrite rules, have a look at the top comments in
237 [_gen/generic.rules](_gen/generic.rules) and [_gen/rulegen.go](_gen/rulegen.go).
238
239 Similarly, the code to manage operators is also code generated from
240 `_gen/*Ops.go`, as it is easier to maintain a few tables than a lot of code.
241 After changing the rules or operators, run `go generate cmd/compile/internal/ssa`
242 to generate the Go code again.
243
244 <!---
245 TODO: more tips and info could likely go here
246 -->