go/src/cmd/compile/internal/ssa
Lynn Boger 403f91c244 cmd/compile/internal: merge rules in PPC64.rules
This uses rulegen syntax which allows similar rules
to be combined, saving lines in the rules file.
The Lsh16x32 rule had an incorrect value and that was
fixed.

Change-Id: I637410e39d8554825076aca5ac24083ce05ab186
Reviewed-on: https://go-review.googlesource.com/c/go/+/429035
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Run-TryBot: Lynn Boger <laboger@linux.vnet.ibm.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
2022-09-08 14:58:11 +00:00
..
gen cmd/compile/internal: merge rules in PPC64.rules 2022-09-08 14:58:11 +00:00
testdata cmd/compile: avoid copying Pos from ONAME when creating converts for maps 2022-08-11 21:21:49 +00:00
addressingmodes.go cmd/compile: add SARXQload and SARXLload 2022-04-13 17:48:12 +00:00
bench_test.go cmd/compile/internal/ssa: strengthen phiopt pass 2021-03-29 05:50:11 +00:00
biasedsparsemap.go cmd/compile: index line number tables by source file to improve sparsity 2019-05-14 18:48:16 +00:00
block.go cmd/compile/internal/ssa: increase the bit width of BlockKind type 2022-05-11 23:20:01 +00:00
branchelim.go all: gofmt main repo 2022-04-11 16:34:30 +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: re-use regalloc's []valState 2019-05-10 00:14:40 +00:00
check.go cmd/compile: issue VarDef only for pointer-ful types 2022-08-22 18:57:00 +00:00
checkbce.go cmd/compile: enable optimizer logging for bounds checking 2019-11-10 17:12:35 +00:00
compile.go cmd/compile/internal/ssa: use strings.Builder 2022-09-07 01:29:43 +00:00
config.go cmd/compile, cmd/link: enable Duff's device on darwin/arm64 2022-08-08 17:54:10 +00:00
copyelim.go cmd/compile: manage Slot array better 2021-05-08 17:03:18 +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: factor out code to remove phi argument 2021-10-25 03:00:02 +00:00
cse.go all: fix spelling 2022-05-17 21:46:33 +00:00
cse_test.go cmd/compile: issue VarDef only for pointer-ful types 2022-08-22 18:57:00 +00:00
deadcode.go cmd/compile: factor out code to remove phi argument 2021-10-25 03:00:02 +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: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +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: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +00:00
debug_lines_test.go cmd/compile/internal/ssa: use strings.Builder 2022-09-07 01:29:43 +00:00
debug_test.go cmd/compile/internal/ssa: use strings.Builder 2022-09-07 01:29:43 +00:00
decompose.go cmd/compile: fix error message formatting in decomposeBuiltInPhi 2022-08-23 21:22:15 +00:00
dom.go cmd/compile: simplify postorder 2019-08-29 19:54:46 +00:00
dom_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
expand_calls.go cmd/compile: fix wrong position when rewriting to OpArg/OpArgXXX 2022-08-30 00:49:19 +00:00
export_test.go cmd/compile: issue VarDef only for pointer-ful types 2022-08-22 18:57:00 +00:00
flagalloc.go cmd/compile: remove go115flagallocdeadcode 2020-10-22 15:30:02 +00:00
flags_amd64_test.s all: add //go:build lines to assembly files 2021-05-13 09:12:17 +00:00
flags_arm64_test.s all: add //go:build lines to assembly files 2021-05-13 09:12:17 +00:00
flags_test.go all: go fmt std cmd (but revert vendor) 2021-02-20 03:54:50 +00:00
func.go cmd/compile: gofmt -w -s 2022-08-17 23:18:21 +00:00
func_test.go all: fix TODO comment hanging indents 2022-04-01 18:18:05 +00:00
fuse.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
fuse_branchredirect.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
fuse_comparisons.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
fuse_test.go cmd/compile/internal/ssa: handle more cases in fuse pass 2021-03-16 02:25:37 +00:00
html.go cmd/compile/internal/ssa: use strings.Builder 2022-09-07 01:29:43 +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: manage Slot array better 2021-05-08 17:03:18 +00:00
lca.go cmd/compile/internal/ssa: prealloc slice 2021-03-13 18:09:48 +00:00
lca_test.go cmd/compile: remove a few unused bits of code 2019-09-01 14:25:12 +00:00
likelyadjust.go cmd/compile/internal/ssa: unnecessary loop break 2021-03-25 21:28:23 +00:00
location.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
loopbce.go cmd/compile: tighten bounds for induction variables in strided loops 2022-08-31 22:04:55 +00:00
loopreschedchecks.go cmd/compile: fix loopreschedchecks for regabi 2022-05-02 19:53:56 +00:00
looprotate.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
lower.go cmd/compile: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +00:00
magic.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
magic_test.go cmd/compile: add signed divisibility rules 2019-04-30 22:02:07 +00:00
nilcheck.go cmd/compile: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +00:00
nilcheck_test.go [dev.regabi] cmd/compile: add ssa.Aux tag interface for Value.Aux 2020-12-08 01:46:31 +00:00
numberlines.go cmd/compile: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +00:00
op.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
opGen.go cmd/compile: intrinsify RotateLeft{32,64} on loong64 2022-08-30 03:21:44 +00:00
opt.go cmd/compile: invalidate zero-use values during rewrite 2020-08-27 22:56:29 +00:00
passbm_test.go all: add empty line between copyright header and package clause 2020-08-17 09:45:44 +00:00
phielim.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
phiopt.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
poset.go all: fix spelling 2022-05-17 21:46:33 +00:00
poset_test.go cmd/compile: in poset, implement path collapsing 2019-10-26 07:52:35 +00:00
print.go cmd/compile/internal/ssa: use strings.Builder 2022-09-07 01:29:43 +00:00
prove.go cmd/compile: teach prove about bitwise OR operation 2022-08-31 09:35:45 +00:00
README.md doc: lint Markdown for trailing spaces and code blocks 2022-05-27 15:07:38 +00:00
regalloc.go cmd/compile: better propagation of desired registers 2022-03-31 20:19:48 +00:00
regalloc_test.go cmd/compile: introduce special ssa Aux type for calls 2020-09-16 20:57:24 +00:00
rewrite.go cmd/compile: optimize RotateLeft8/16 on arm64 2022-09-02 17:46:31 +00:00
rewrite386.go cmd/compile: combine multiple rotate instructions 2022-08-31 22:10:52 +00:00
rewrite386splitload.go cmd/compile: make no-op rewrite funcs smaller 2021-03-22 16:01:44 +00:00
rewrite_test.go cmd/compile: fix ANDI/SRWI merge on ppc64 2021-04-15 21:57:02 +00:00
rewriteAMD64.go cmd/compile: move SSA rotate instruction detection to arch-independent rules 2022-08-23 21:24:14 +00:00
rewriteAMD64splitload.go cmd/compile: make no-op rewrite funcs smaller 2021-03-22 16:01:44 +00:00
rewriteARM.go cmd/compile: move SSA rotate instruction detection to arch-independent rules 2022-08-23 21:24:14 +00:00
rewriteARM64.go cmd/compile: Add shiftIsBounded check for logic shifts of arm64 2022-09-07 20:10:13 +00:00
rewriteCond_test.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
rewritedec.go cmd/compile: allow conversion from slice to array ptr 2021-04-21 00:53:48 +00:00
rewritedec64.go cmd/compile: move SSA rotate instruction detection to arch-independent rules 2022-08-23 21:24:14 +00:00
rewritegeneric.go cmd/compile: rewrite >>c<<c to &^(1<<c-1) 2022-09-02 18:51:37 +00:00
rewriteLOONG64.go cmd/compile: intrinsify RotateLeft{32,64} on loong64 2022-08-30 03:21:44 +00:00
rewriteMIPS.go cmd/compile: restore tail call for method wrappers 2021-09-17 22:59:44 +00:00
rewriteMIPS64.go cmd/compile: restore tail call for method wrappers 2021-09-17 22:59:44 +00:00
rewritePPC64.go cmd/compile/internal: merge rules in PPC64.rules 2022-09-08 14:58:11 +00:00
rewriteRISCV64.go cmd/compile: remove redundant SEQZ/SNEZ on riscv64 2022-09-07 05:39:23 +00:00
rewriteS390X.go cmd/compile: move SSA rotate instruction detection to arch-independent rules 2022-08-23 21:24:14 +00:00
rewriteWasm.go cmd/compile: restore tail call for method wrappers 2021-09-17 22:59:44 +00:00
schedule.go cmd/compile: fix score for Select{0,1} with type flags 2022-08-26 19:15:02 +00:00
schedule_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
shift_test.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
shortcircuit.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
shortcircuit_test.go cmd/compile: change ssa.Type into *types.Type 2017-05-09 23:01:51 +00:00
sizeof_test.go [dev.regabi] cmd/compile: change LocalSlot.N to *ir.Name 2020-12-08 01:46:40 +00:00
softfloat.go [dev.typeparams] cmd/compile: make softfloat mode work with register ABI 2021-08-03 16:14:24 +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 all: gofmt main repo 2022-04-11 16:34:30 +00:00
stackalloc.go cmd/compile: manage Slot array better 2021-05-08 17:03:18 +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/compile: update TestStmtLines threshold for riscv64 2022-05-06 04:22:04 +00:00
tighten.go cmd/compile: handle aggregate OpArg in registers 2021-03-03 15:00:48 +00:00
TODO cmd/compile: update SSA TODO file 2018-04-24 23:35:13 +00:00
trim.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
tuple.go cmd/compile: change StaticCall to return a "Results" 2021-02-26 02:52:33 +00:00
value.go cmd/compile: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +00:00
writebarrier.go cmd/compile: rip out support for OpVarKill from ssa backend 2022-08-18 17:36:52 +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
xposmap.go cmd/compile: index line number tables by source file to improve sparsity 2019-05-14 18:48:16 +00:00
zcse.go [dev.regabi] cmd/compile: add ssa.Aux tag interface for Value.Aux 2020-12-08 01:46:31 +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. It has a single control value that 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.

The value specified in GOSSAFUNC can also be a package-qualified function name, e.g.

GOSSAFUNC=blah.Foo go build

This will match any function named "Foo" within a package whose final suffix is "blah" (e.g. something/blah.Foo, anotherthing/extra/blah.Foo).

If non-HTML dumps are needed, append a "+" to the GOSSAFUNC value and dumps will be written to stdout:

GOSSAFUNC=Bar+ go build

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.