go/src/cmd/compile/internal/ssa
Giovanni Bajo 67656ba71b cmd/compile: improve undo of poset
prove uses the poset datastructure in a DFS walk, and always undoes
it back to its pristine status. Before this CL, poset's undo of
a new node creation didn't fully deallocate the node, which means
that at the end of prove there was still some allocated memory pending.

This was not a problem until now because the posets used by prove
were discarded after each function, but it would prevent recycling
them between functions (as a followup CL does).

Change-Id: I1c1c99c03fe19ad765395a43958cb256f686765a
Reviewed-on: https://go-review.googlesource.com/112976
Run-TryBot: Giovanni Bajo <rasky@develer.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
2018-05-14 14:31:48 +00:00
..
gen cmd/compile: fix zero extend after float->int conversion 2018-05-10 17:32:43 +00:00
testdata cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
biasedsparsemap.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
bits_bootstrap.go cmd/compile/internal/ssa: use math/bits for register sets 2018-02-14 18:29:22 +00:00
bits_go19.go cmd/compile/internal/ssa: use math/bits for register sets 2018-02-14 18:29:22 +00:00
block.go cmd/compile: adjust Pos setting for "empty" blocks 2017-11-08 22:39:49 +00:00
branchelim.go cmd/compile/internal/ssa: tweak branchelim cost model on amd64 2018-04-25 16:06:54 +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: cache sparse maps across ssa passes 2018-03-15 17:24:39 +00:00
check.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51: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: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
config.go cmd/compile/internal/ssa: initialize t.UInt in SetTypPtrs() 2018-05-10 14:50:30 +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/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
deadcode.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +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: add some generic composite type optimizations 2018-05-08 10:31:21 +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/internal/ssa: track stack-only vars 2018-03-14 20:52:29 +00:00
debug_test.go cmd/compile: invoke gdb more carefully in ssa/debug_test.go 2018-03-26 18:44:52 +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/internal/types: remove ElemType wrapper 2018-04-24 22:24:47 +00:00
flagalloc.go cmd/compile: rename memory-using operations 2018-05-08 19:16:50 +00:00
func.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
func_test.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
fuse.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +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: add IsStmt breakpoint info to src.lico 2018-04-04 22:11:34 +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: rename SSA Register.Name to Register.String 2017-08-17 21:53:08 +00:00
loop_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
loopbce.go cmd/compile: teach prove to handle expressions like len(s)-delta 2018-04-29 09:38:32 +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: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
nilcheck_test.go cmd/compile/internal/ssa: add Op{SP,SB} type checks to check.go 2018-04-24 15:51:15 +00:00
numberlines.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
op.go cmd/compile/internal/ssa: note zero-width Ops 2018-03-02 18:55:45 +00:00
opGen.go cmd/compile: rename memory-using operations 2018-05-08 19:16:50 +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: improve undo of poset 2018-05-14 14:31:48 +00:00
poset_test.go cmd/compile: improve undo of poset 2018-05-14 14:31:48 +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: recognize some OpRsh64Ux64 Values as non-negative 2018-05-01 19:38:09 +00:00
README.md cmd/compile/internal/ssa: add initial README 2018-03-31 20:18:09 +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: use sparse algorithm for phis in large program 2016-05-16 21:08:05 +00:00
regalloc.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
regalloc_test.go cmd/compile: test for moving spills 2017-05-24 21:36:06 +00:00
rewrite.go cmd/compile: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
rewrite386.go cmd/compile: rename memory-using operations 2018-05-08 19:16:50 +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 zero extend after float->int conversion 2018-05-10 17:32:43 +00:00
rewriteARM.go cmd/compile: intrinsify runtime.getcallerpc on all link register architectures 2018-05-02 16:59:27 +00:00
rewriteARM64.go cmd/compile: intrinsify runtime.getcallerpc on arm64 2018-04-30 13:29:14 +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: convert memmove call into Move when arguments are disjoint 2018-05-09 11:20:40 +00:00
rewriteMIPS.go cmd/compile: intrinsify runtime.getcallerpc on all link register architectures 2018-05-02 16:59:27 +00:00
rewriteMIPS64.go cmd/compile: intrinsify runtime.getcallerpc on all link register architectures 2018-05-02 16:59:27 +00:00
rewritePPC64.go cmd/compile,test: combine byte loads and stores on ppc64le 2018-05-08 13:15:39 +00:00
rewriteS390X.go cmd/compile: simplify shift lowering on s390x 2018-05-08 16:19:56 +00:00
rewriteWasm.go cmd/internal/obj/wasm: avoid invalid offsets for Load/Store 2018-05-10 12:05:17 +00:00
schedule.go cmd/compile: add wasm architecture 2018-05-04 17:56:12 +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 [dev.debug] cmd/compile: track variable decomposition in LocalSlot 2017-07-26 18:39:39 +00:00
softfloat.go cmd/compile: use soft-float routines for soft-float targets 2017-11-30 17:37:37 +00:00
sparsemap.go cmd/compile: cache sparse maps across ssa passes 2018-03-15 17:24:39 +00:00
sparseset.go cmd/compile: reduce element size of arrays in sparse{map,set} 2016-05-18 14:05:14 +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: don't let spills clobber arguments 2018-01-23 21:51:55 +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: assign and preserve statement boundaries. 2018-05-14 14:09:49 +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: assign and preserve statement boundaries. 2018-05-14 14:09:49 +00:00
writebarrier.go cmd/compile/internal/types: remove ElemType wrapper 2018-04-24 22:24:47 +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

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.