go/src/cmd/compile/internal/ssa
Cherry Zhang c96e3bcc97 cmd/compile: fix type of OffPtr in some optimization rules
In some optimization rules the type of generated OffPtr was
incorrectly set to the type of the pointee, instead of the
pointer. When the OffPtr value is spilled, this may generate
a spill of the wrong type, e.g. a floating point spill of an
integer (pointer) value. On Wasm, this leads to invalid
bytecode.

Fixes #27961.

Change-Id: I5d464847eb900ed90794105c0013a1a7330756cc
Reviewed-on: https://go-review.googlesource.com/c/139257
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
2018-10-03 15:01:47 +00:00
..
gen cmd/compile: fix type of OffPtr in some optimization rules 2018-10-03 15:01:47 +00:00
testdata cmd/compile: update TestNexting golden file 2018-09-26 16:45:42 +00:00
biasedsparsemap.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
block.go cmd/compile: adjust Pos setting for "empty" blocks 2017-11-08 22:39:49 +00:00
branchelim.go cmd/compile: fix trivial typos in comments 2018-05-29 02:39:52 +00:00
branchelim_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
cache.go cmd/compile: reuse liveness structures 2018-05-22 20:44:06 +00:00
check.go cmd/compile: fix store-to-load forwarding of 32-bit sNaNs 2018-09-05 15:27:15 +00:00
checkbce.go cmd/compile: move Frontend field from ssa.Config to ssa.Func 2017-03-17 23:18:57 +00:00
compile.go cmd/compile: clean the output of GOSSAFUNC 2018-08-23 05:11:17 +00:00
config.go cmd/compile: omit racefuncentry/exit when they are not needed 2018-08-20 22:07:22 +00:00
copyelim.go all: remove some double spaces from comments 2017-08-26 15:09:09 +00:00
copyelim_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
critical.go cmd/compile: move Frontend field from ssa.Config to ssa.Func 2017-03-17 23:18:57 +00:00
cse.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
cse_test.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
deadcode.go cmd/compile: update WBLoads during deadcode 2018-05-29 17:45:36 +00:00
deadcode_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
deadstore.go cmd/compile: count nil check as use in dead auto elim 2018-08-30 02:19:37 +00:00
deadstore_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
debug.go cmd/compile: remove empty branches 2018-08-20 13:42:37 +00:00
debug_test.go cmd/compile: avoid compressed dwarf when testing for gdb on OSX 2018-07-19 13:58:32 +00:00
decompose.go cmd/compile/internal/types: remove ElemType wrapper 2018-04-24 22:24:47 +00:00
dom.go cmd/compile: stack-allocate 2 worklists in order, dom passes 2018-04-04 11:12:32 +00:00
dom_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
export_test.go cmd/compile: do not allow regalloc to LoadReg G register 2018-05-30 16:39:21 +00:00
flagalloc.go cmd/compile: optimize 386's comparison 2018-08-20 14:23:22 +00:00
func.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
func_test.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
fuse.go cmd/compile: common up code in fuse for joining blocks 2018-05-22 21:46:36 +00:00
fuse_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
html.go cmd/compile/internal/ssa: fix a == a to a == b 2018-09-19 08:29:07 +00:00
id.go cmd/compile: in a Tarjan algorithm, DFS should really be DFS 2016-04-22 19:21:16 +00:00
layout.go cmd/compile: decouple emitted block order from regalloc block order 2018-02-22 03:02:34 +00:00
lca.go cmd/compile: remove redundant function idom 2016-10-11 16:43:12 +00:00
lca_test.go cmd/compile: clean up SSA test API 2017-03-19 05:37:39 +00:00
likelyadjust.go cmd/compile/internal/ssa: update regalloc in loops 2018-03-20 21:02:39 +00:00
location.go cmd/compile: dense numbering for GP registers 2018-05-22 15:55:01 +00:00
loop_test.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
loopbce.go cmd/compile: ensure that loop condition is detected correctly 2018-07-09 18:23:39 +00:00
loopreschedchecks.go cmd/compile: finish GOEXPERIMENT=preemptibleloops repair 2018-03-26 17:44:14 +00:00
looprotate.go cmd/compile: make loop finder more aware of irreducible loops 2017-10-05 18:49:10 +00:00
lower.go cmd/compile: don't lower OpConvert 2018-04-20 18:46:39 +00:00
magic.go cmd/compile: move constant divide strength reduction to SSA rules 2017-02-17 06:16:44 +00:00
magic_test.go cmd/compile: move constant divide strength reduction to SSA rules 2017-02-17 06:16:44 +00:00
nilcheck.go cmd/compile: remove unnecessary nil-check 2018-09-04 17:44:14 +00:00
nilcheck_test.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
numberlines.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
op.go cmd/compile: document regalloc fields 2018-09-24 19:55:51 +00:00
opGen.go cmd/compile: instrinsify math/bits.Mul on ppc64x 2018-10-02 18:56:06 +00:00
opt.go cmd/compile: repair name propagation into aggregate parts 2017-11-05 17:30:11 +00:00
passbm_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
phielim.go cmd/compile: move Frontend field from ssa.Config to ssa.Func 2017-03-17 23:18:57 +00:00
phiopt.go Revert "cmd/compile: add Type.MustSize and Type.MustAlignment" 2017-04-28 01:24:13 +00:00
poset.go cmd/compile: reduce allocations in prove by reusing posets 2018-05-14 14:44:55 +00:00
poset_test.go cmd/compile: reduce allocations in prove by reusing posets 2018-05-14 14:44:55 +00:00
print.go cmd/compile: rename SSA Register.Name to Register.String 2017-08-17 21:53:08 +00:00
prove.go cmd/compile/internal/ssa: fix a typo 2018-09-24 22:07:18 +00:00
README.md cmd/compile: reorganise and improve ssa/README.md 2018-07-03 16:27:51 +00:00
redblack32.go cmd/compile: remove some redundant types in decls 2017-09-17 09:51:38 +00:00
redblack32_test.go cmd/compile: remove empty branches 2018-08-20 13:42:37 +00:00
regalloc.go cmd/compile: document regalloc fields 2018-09-24 19:55:51 +00:00
regalloc_test.go cmd/compile: do not allow regalloc to LoadReg G register 2018-05-30 16:39:21 +00:00
rewrite.go cmd/compile/internal/gc: remove redundant for label 2018-09-19 13:08:54 +00:00
rewrite386.go cmd/compile: avoid more float32 <-> float64 conversions in compiler 2018-09-17 14:37:45 +00:00
rewrite_test.go cmd/compile/internal/ssa: inline memmove with known size 2017-11-02 20:30:25 +00:00
rewriteAMD64.go cmd/compile: fix precedence order bug 2018-09-24 17:43:55 +00:00
rewriteARM.go cmd/compile: optimize ARM's comparision 2018-09-05 14:54:08 +00:00
rewriteARM64.go cmd/compile: fix type of OffPtr in some optimization rules 2018-10-03 15:01:47 +00:00
rewritedec.go cmd/compile/internal/types: remove ElemType wrapper 2018-04-24 22:24:47 +00:00
rewritedec64.go cmd/compile: use prove pass to detect Ctz of non-zero values 2018-04-26 18:22:28 +00:00
rewritegeneric.go cmd/compile: fix type of OffPtr in some optimization rules 2018-10-03 15:01:47 +00:00
rewriteMIPS.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
rewriteMIPS64.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
rewritePPC64.go cmd/compile: instrinsify math/bits.Mul on ppc64x 2018-10-02 18:56:06 +00:00
rewriteS390X.go cmd/compile: make math/bits.RotateLeft{32,64} intrinsics on s390x 2018-09-05 08:29:02 +00:00
rewriteWasm.go cmd/compile: avoid more float32 <-> float64 conversions in compiler 2018-09-17 14:37:45 +00:00
schedule.go Revert "cmd/compile: make OpAddr depend on VarDef in storeOrder" 2018-06-29 19:48:48 +00:00
schedule_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
shift_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
shortcircuit.go cmd: remove a few more unused parameters 2018-04-09 17:10:25 +00:00
shortcircuit_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
sizeof_test.go cmd/compile: add wasm stack optimization 2018-05-23 21:37:59 +00:00
softfloat.go cmd/compile: avoid more float32 <-> float64 conversions in compiler 2018-09-17 14:37:45 +00:00
sparsemap.go all: update comment URLs from HTTP to HTTPS, where possible 2018-06-01 21:52:00 +00:00
sparseset.go all: update comment URLs from HTTP to HTTPS, where possible 2018-06-01 21:52:00 +00:00
sparsetree.go cmd/compile: fix phi-function updates for preemptible loops 2017-07-14 15:54:13 +00:00
sparsetreemap.go cmd/compile: remove redundant function idom 2016-10-11 16:43:12 +00:00
stackalloc.go cmd/compile: add wasm stack optimization 2018-05-23 21:37:59 +00:00
stackframe.go cmd/compile: move Frontend field from ssa.Config to ssa.Func 2017-03-17 23:18:57 +00:00
stmtlines_test.go cmd/link: move DIE of global variables to their compile unit 2018-09-27 11:58:35 +00:00
tighten.go cmd/compile: add wasm architecture 2018-05-04 17:56:12 +00:00
TODO cmd/compile: update SSA TODO file 2018-04-24 23:35:13 +00:00
trim.go all: remove some unused result params 2017-08-28 06:52:55 +00:00
value.go cmd/compile: reorganise and improve ssa/README.md 2018-07-03 16:27:51 +00:00
writebarrier.go cmd/compile: add LocalAddr that takes SP,mem operands 2018-07-12 18:45:31 +00:00
writebarrier_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
zcse.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
zeroextension_test.go cmd/compile/internal/ssa: refactor zeroUpper32Bits 2018-02-27 20:38:32 +00:00

Introduction to the Go compiler's SSA backend

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

It is recommended that you first read cmd/compile/README.md if you are not familiar with the Go compiler already. That document gives an overview of the compiler, and explains what is SSA's part and purpose in it.

Key concepts

The names described below may be loosely related to their Go counterparts, but note that they are not equivalent. For example, a Go block statement has a variable scope, yet SSA has no notion of variables nor variable scopes.

It may also be surprising that values and blocks are named after their unique sequential IDs. They rarely correspond to named entities in the original code, such as variables or function parameters. The sequential IDs also allow the compiler to avoid maps, and it is always possible to track back the values to Go code using debug and position information.

Values

Values are the basic building blocks of SSA. Per SSA's very definition, a value is defined exactly once, but it may be used any number of times. A value mainly consists of a unique identifier, an operator, a type, and some arguments.

An operator or Op describes the operation that computes the value. The semantics of each operator can be found in gen/*Ops.go. For example, OpAdd8 takes two value arguments holding 8-bit integers and results in their addition. Here is a possible SSA representation of the addition of two uint8 values:

// var c uint8 = a + b
v4 = Add8 <uint8> v2 v3

A value's type will usually be a Go type. For example, the value in the example above has a uint8 type, and a constant boolean value will have a bool type. However, certain types don't come from Go and are special; below we will cover memory, the most common of them.

See value.go for more information.

Memory types

memory represents the global memory state. 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 ensures that memory operations are kept in the right order. For example:

// *a = 3
// *b = *a
v10 = Store <mem> {int} v6 v8 v1
v14 = Store <mem> {int} v7 v8 v10

Here, Store stores its second argument (of type int) into the first argument (of type *int). The last argument is the memory state; since the second store depends on the memory value defined by the first store, the two stores cannot be reordered.

See cmd/compile/internal/types/type.go for more information.

Blocks

A block represents a basic block in the control flow graph of a function. It is, essentially, a list of values that define the operation of this block. Besides the list of values, blocks mainly consist of a unique identifier, a kind, and a list of successor blocks.

The simplest kind is a plain block; it simply hands the control flow to another block, thus its successors list contains one block.

Another common block kind is the exit block. These have a final value, called control value, which must return a memory state. This is necessary for functions to return some values, for example - the caller needs some memory state to depend on, to ensure that it receives those return values correctly.

The last important block kind we will mention is the if block. Its control value must be a boolean value, and it has exactly two successor blocks. The control flow is handed to the first successor if the bool is true, and to the second otherwise.

Here is a sample if-else control flow represented with basic blocks:

// func(b bool) int {
// 	if b {
// 		return 2
// 	}
// 	return 3
// }
b1:
  v1 = InitMem <mem>
  v2 = SP <uintptr>
  v5 = Addr <*int> {~r1} v2
  v6 = Arg <bool> {b}
  v8 = Const64 <int> [2]
  v12 = Const64 <int> [3]
  If v6 -> b2 b3
b2: <- b1
  v10 = VarDef <mem> {~r1} v1
  v11 = Store <mem> {int} v5 v8 v10
  Ret v11
b3: <- b1
  v14 = VarDef <mem> {~r1} v1
  v15 = Store <mem> {int} v5 v12 v14
  Ret v15

See block.go for more information.

Functions

A function represents a function declaration along with its body. It mainly consists of a name, a type (its signature), a list of blocks that form its body, and the entry block within said list.

When a function is called, the control flow is handed to its entry block. If the function terminates, the control flow will eventually reach an exit block, thus ending the function call.

Note that a function may have zero or multiple exit blocks, just like a Go function can have any number of return points, but it must have exactly one entry point block.

Also note that some SSA functions are autogenerated, such as the hash functions for each type used as a map key.

For example, this is what an empty function can look like in SSA, with a single exit block that returns an uninteresting memory state:

foo func()
  b1:
    v1 = InitMem <mem>
    Ret v1

See func.go for more information.

Compiler passes

Having a program in SSA form is not very useful on its own. Its advantage lies in how easy it is to write optimizations that modify the program to make it better. The way the Go compiler accomplishes this is via a list of passes.

Each pass transforms a SSA function in some way. For example, a dead code elimination pass will remove blocks and values that it can prove will never be executed, and a nil check elimination pass will remove nil checks which it can prove to be redundant.

Compiler passes work on one function at a time, and by default run sequentially and exactly once.

The lower pass is special; it converts the SSA representation from being machine-independent to being machine-dependent. That is, some abstract operators are replaced with their non-generic counterparts, potentially reducing or increasing the final number of values.

See the passes list defined in compile.go for more information.

Playing with SSA

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.

Hacking on SSA

While most compiler passes are implemented directly in Go code, some others are code generated. This is currently done via rewrite rules, which have their own syntax and are maintained in gen/*.rules. Simpler optimizations can be written easily and quickly this way, but rewrite rules are not suitable for more complex optimizations.

To read more on rewrite rules, have a look at the top comments in gen/generic.rules and gen/rulegen.go.

Similarly, the code to manage operators is also code generated from gen/*Ops.go, as it is easier to maintain a few tables than a lot of code. After changing the rules or operators, see gen/README for instructions on how to generate the Go code again.