2015-03-03 13:38:14 -08:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
import (
|
2016-12-06 17:08:06 -08:00
|
|
|
"cmd/internal/src"
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
"fmt"
|
|
|
|
|
"math"
|
2016-05-11 15:25:17 -04:00
|
|
|
"strings"
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
)
|
2015-06-25 23:13:57 -05:00
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// A Func represents a Go func declaration (or function literal) and
|
2016-03-01 23:21:55 +00:00
|
|
|
// its body. This package compiles each Func independently.
|
2015-03-03 13:38:14 -08:00
|
|
|
type Func struct {
|
2017-02-03 14:28:32 -08:00
|
|
|
Config *Config // architecture information
|
|
|
|
|
pass *pass // current pass information (name, options, etc.)
|
|
|
|
|
Name string // e.g. bytes·Compare
|
|
|
|
|
Type Type // type signature of the function.
|
|
|
|
|
Blocks []*Block // unordered set of all basic blocks (note: not indexable by ID)
|
|
|
|
|
Entry *Block // the entry basic block
|
|
|
|
|
bid idAlloc // block ID allocator
|
|
|
|
|
vid idAlloc // value ID allocator
|
2015-03-03 13:38:14 -08:00
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
scheduled bool // Values in Blocks are in final order
|
2016-11-10 16:03:47 -05:00
|
|
|
NoSplit bool // true if function is marked as nosplit. Used by schedule check pass.
|
2015-08-03 12:33:03 -07:00
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// when register allocation is done, maps value ids to locations
|
|
|
|
|
RegAlloc []Location
|
2015-10-22 14:22:38 -07:00
|
|
|
|
2015-11-02 08:10:26 -08:00
|
|
|
// map from LocalSlot to set of Values that we want to store in that slot.
|
|
|
|
|
NamedValues map[LocalSlot][]*Value
|
2016-03-01 23:21:55 +00:00
|
|
|
// Names is a copy of NamedValues.Keys. We keep a separate list
|
2015-10-22 14:22:38 -07:00
|
|
|
// of keys to make iteration order deterministic.
|
2015-11-02 08:10:26 -08:00
|
|
|
Names []LocalSlot
|
2016-01-28 13:46:30 -08:00
|
|
|
|
|
|
|
|
freeValues *Value // free Values linked by argstorage[0]. All other fields except ID are 0/nil.
|
2016-04-28 16:52:47 -07:00
|
|
|
freeBlocks *Block // free Blocks linked by succstorage[0].b. All other fields except ID are 0/nil.
|
2016-02-28 15:51:11 -08:00
|
|
|
|
2016-09-16 13:50:18 -07:00
|
|
|
cachedPostorder []*Block // cached postorder traversal
|
|
|
|
|
cachedIdom []*Block // cached immediate dominators
|
|
|
|
|
cachedSdom SparseTree // cached dominator tree
|
|
|
|
|
cachedLoopnest *loopnest // cached loop nest information
|
2016-04-11 21:51:29 +02:00
|
|
|
|
2017-02-09 10:45:35 -08:00
|
|
|
auxmap auxmap // map from aux values to opaque ids used by CSE
|
|
|
|
|
|
2016-02-28 15:51:11 -08:00
|
|
|
constants map[int64][]*Value // constants cache, keyed by constant value; users must check value's Op and Type
|
2015-03-03 13:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NumBlocks returns an integer larger than the id of any Block in the Func.
|
|
|
|
|
func (f *Func) NumBlocks() int {
|
|
|
|
|
return f.bid.num()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NumValues returns an integer larger than the id of any Value in the Func.
|
|
|
|
|
func (f *Func) NumValues() int {
|
|
|
|
|
return f.vid.num()
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 22:19:46 -06:00
|
|
|
// newSparseSet returns a sparse set that can store at least up to n integers.
|
|
|
|
|
func (f *Func) newSparseSet(n int) *sparseSet {
|
2016-02-02 06:35:34 -05:00
|
|
|
for i, scr := range f.Config.scrSparse {
|
2016-01-28 22:19:46 -06:00
|
|
|
if scr != nil && scr.cap() >= n {
|
2016-02-02 06:35:34 -05:00
|
|
|
f.Config.scrSparse[i] = nil
|
2016-01-28 22:19:46 -06:00
|
|
|
scr.clear()
|
|
|
|
|
return scr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newSparseSet(n)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 06:35:34 -05:00
|
|
|
// retSparseSet returns a sparse set to the config's cache of sparse sets to be reused by f.newSparseSet.
|
2016-01-28 22:19:46 -06:00
|
|
|
func (f *Func) retSparseSet(ss *sparseSet) {
|
2016-02-02 06:35:34 -05:00
|
|
|
for i, scr := range f.Config.scrSparse {
|
2016-01-28 22:19:46 -06:00
|
|
|
if scr == nil {
|
2016-02-02 06:35:34 -05:00
|
|
|
f.Config.scrSparse[i] = ss
|
2016-01-28 22:19:46 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-02 06:35:34 -05:00
|
|
|
f.Config.scrSparse = append(f.Config.scrSparse, ss)
|
2016-01-28 22:19:46 -06:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 13:46:30 -08:00
|
|
|
// newValue allocates a new Value with the given fields and places it at the end of b.Values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) newValue(op Op, t Type, b *Block, pos src.XPos) *Value {
|
2016-01-28 13:46:30 -08:00
|
|
|
var v *Value
|
|
|
|
|
if f.freeValues != nil {
|
|
|
|
|
v = f.freeValues
|
|
|
|
|
f.freeValues = v.argstorage[0]
|
|
|
|
|
v.argstorage[0] = nil
|
|
|
|
|
} else {
|
|
|
|
|
ID := f.vid.get()
|
|
|
|
|
if int(ID) < len(f.Config.values) {
|
|
|
|
|
v = &f.Config.values[ID]
|
|
|
|
|
} else {
|
|
|
|
|
v = &Value{ID: ID}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
v.Op = op
|
|
|
|
|
v.Type = t
|
|
|
|
|
v.Block = b
|
2016-12-07 18:14:35 -08:00
|
|
|
v.Pos = pos
|
2016-01-28 13:46:30 -08:00
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
2015-06-25 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-07 14:45:46 -05:00
|
|
|
// newValueNoBlock allocates a new Value with the given fields.
|
|
|
|
|
// The returned value is not placed in any block. Once the caller
|
|
|
|
|
// decides on a block b, it must set b.Block and append
|
|
|
|
|
// the returned value to b.Values.
|
|
|
|
|
func (f *Func) newValueNoBlock(op Op, t Type, pos src.XPos) *Value {
|
|
|
|
|
var v *Value
|
|
|
|
|
if f.freeValues != nil {
|
|
|
|
|
v = f.freeValues
|
|
|
|
|
f.freeValues = v.argstorage[0]
|
|
|
|
|
v.argstorage[0] = nil
|
|
|
|
|
} else {
|
|
|
|
|
ID := f.vid.get()
|
|
|
|
|
if int(ID) < len(f.Config.values) {
|
|
|
|
|
v = &f.Config.values[ID]
|
|
|
|
|
} else {
|
|
|
|
|
v = &Value{ID: ID}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
v.Op = op
|
|
|
|
|
v.Type = t
|
|
|
|
|
v.Block = nil // caller must fix this.
|
|
|
|
|
v.Pos = pos
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
// logPassStat writes a string key and int value as a warning in a
|
|
|
|
|
// tab-separated format easily handled by spreadsheets or awk.
|
|
|
|
|
// file names, lines, and function names are included to provide enough (?)
|
|
|
|
|
// context to allow item-by-item comparisons across runs.
|
|
|
|
|
// For example:
|
|
|
|
|
// awk 'BEGIN {FS="\t"} $3~/TIME/{sum+=$4} END{print "t(ns)=",sum}' t.log
|
cmd/compile: use sparse algorithm for phis in large program
This adds a sparse method for locating nearest ancestors
in a dominator tree, and checks blocks with more than one
predecessor for differences and inserts phi functions where
there are.
Uses reversed post order to cut number of passes, running
it from first def to last use ("last use" for paramout and
mem is end-of-program; last use for a phi input from a
backedge is the source of the back edge)
Includes a cutover from old algorithm to new to avoid paying
large constant factor for small programs. This keeps normal
builds running at about the same time, while not running
over-long on large machine-generated inputs.
Add "phase" flags for ssa/build -- ssa/build/stats prints
number of blocks, values (before and after linking references
and inserting phis, so expansion can be measured), and their
product; the product governs the cutover, where a good value
seems to be somewhere between 1 and 5 million.
Among the files compiled by make.bash, this is the shape of
the tail of the distribution for #blocks, #vars, and their
product:
#blocks #vars product
max 6171 28180 173,898,780
99.9% 1641 6548 10,401,878
99% 463 1909 873,721
95% 152 639 95,235
90% 84 359 30,021
The old algorithm is indeed usually fastest, for 99%ile
values of usually.
The fix to LookupVarOutgoing
( https://go-review.googlesource.com/#/c/22790/ )
deals with some of the same problems addressed by this CL,
but on at least one bug ( #15537 ) this change is still
a significant help.
With this CL:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 4m35.200s
user 13m16.644s
sys 0m36.712s
and pprof reports 3.4GB allocated in one of the larger profiles
With tip:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 10m36.569s
user 25m52.286s
sys 4m3.696s
and pprof reports 8.3GB allocated in the same larger profile
With this CL, most of the compilation time on the benchmarked
input is spent in register/stack allocation (cumulative 53%)
and in the sparse lookup algorithm itself (cumulative 20%).
Fixes #15537.
Change-Id: Ia0299dda6a291534d8b08e5f9883216ded677a00
Reviewed-on: https://go-review.googlesource.com/22342
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-21 13:24:58 -04:00
|
|
|
func (f *Func) LogStat(key string, args ...interface{}) {
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
value := ""
|
|
|
|
|
for _, a := range args {
|
|
|
|
|
value += fmt.Sprintf("\t%v", a)
|
|
|
|
|
}
|
cmd/compile: use sparse algorithm for phis in large program
This adds a sparse method for locating nearest ancestors
in a dominator tree, and checks blocks with more than one
predecessor for differences and inserts phi functions where
there are.
Uses reversed post order to cut number of passes, running
it from first def to last use ("last use" for paramout and
mem is end-of-program; last use for a phi input from a
backedge is the source of the back edge)
Includes a cutover from old algorithm to new to avoid paying
large constant factor for small programs. This keeps normal
builds running at about the same time, while not running
over-long on large machine-generated inputs.
Add "phase" flags for ssa/build -- ssa/build/stats prints
number of blocks, values (before and after linking references
and inserting phis, so expansion can be measured), and their
product; the product governs the cutover, where a good value
seems to be somewhere between 1 and 5 million.
Among the files compiled by make.bash, this is the shape of
the tail of the distribution for #blocks, #vars, and their
product:
#blocks #vars product
max 6171 28180 173,898,780
99.9% 1641 6548 10,401,878
99% 463 1909 873,721
95% 152 639 95,235
90% 84 359 30,021
The old algorithm is indeed usually fastest, for 99%ile
values of usually.
The fix to LookupVarOutgoing
( https://go-review.googlesource.com/#/c/22790/ )
deals with some of the same problems addressed by this CL,
but on at least one bug ( #15537 ) this change is still
a significant help.
With this CL:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 4m35.200s
user 13m16.644s
sys 0m36.712s
and pprof reports 3.4GB allocated in one of the larger profiles
With tip:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 10m36.569s
user 25m52.286s
sys 4m3.696s
and pprof reports 8.3GB allocated in the same larger profile
With this CL, most of the compilation time on the benchmarked
input is spent in register/stack allocation (cumulative 53%)
and in the sparse lookup algorithm itself (cumulative 20%).
Fixes #15537.
Change-Id: Ia0299dda6a291534d8b08e5f9883216ded677a00
Reviewed-on: https://go-review.googlesource.com/22342
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-21 13:24:58 -04:00
|
|
|
n := "missing_pass"
|
|
|
|
|
if f.pass != nil {
|
2016-05-11 15:25:17 -04:00
|
|
|
n = strings.Replace(f.pass.name, " ", "_", -1)
|
cmd/compile: use sparse algorithm for phis in large program
This adds a sparse method for locating nearest ancestors
in a dominator tree, and checks blocks with more than one
predecessor for differences and inserts phi functions where
there are.
Uses reversed post order to cut number of passes, running
it from first def to last use ("last use" for paramout and
mem is end-of-program; last use for a phi input from a
backedge is the source of the back edge)
Includes a cutover from old algorithm to new to avoid paying
large constant factor for small programs. This keeps normal
builds running at about the same time, while not running
over-long on large machine-generated inputs.
Add "phase" flags for ssa/build -- ssa/build/stats prints
number of blocks, values (before and after linking references
and inserting phis, so expansion can be measured), and their
product; the product governs the cutover, where a good value
seems to be somewhere between 1 and 5 million.
Among the files compiled by make.bash, this is the shape of
the tail of the distribution for #blocks, #vars, and their
product:
#blocks #vars product
max 6171 28180 173,898,780
99.9% 1641 6548 10,401,878
99% 463 1909 873,721
95% 152 639 95,235
90% 84 359 30,021
The old algorithm is indeed usually fastest, for 99%ile
values of usually.
The fix to LookupVarOutgoing
( https://go-review.googlesource.com/#/c/22790/ )
deals with some of the same problems addressed by this CL,
but on at least one bug ( #15537 ) this change is still
a significant help.
With this CL:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 4m35.200s
user 13m16.644s
sys 0m36.712s
and pprof reports 3.4GB allocated in one of the larger profiles
With tip:
/tmp/gopath$ rm -rf pkg bin
/tmp/gopath$ time go get -v -gcflags -memprofile=y.mprof \
github.com/gogo/protobuf/test/theproto3/combos/...
...
real 10m36.569s
user 25m52.286s
sys 4m3.696s
and pprof reports 8.3GB allocated in the same larger profile
With this CL, most of the compilation time on the benchmarked
input is spent in register/stack allocation (cumulative 53%)
and in the sparse lookup algorithm itself (cumulative 20%).
Fixes #15537.
Change-Id: Ia0299dda6a291534d8b08e5f9883216ded677a00
Reviewed-on: https://go-review.googlesource.com/22342
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-21 13:24:58 -04:00
|
|
|
}
|
2016-12-07 18:14:35 -08:00
|
|
|
f.Config.Warnl(f.Entry.Pos, "\t%s\t%s%s\t%s", n, key, value, f.Name)
|
[dev.ssa] cmd/compile: enhance command line option processing for SSA
The -d compiler flag can also specify ssa phase and flag,
for example -d=ssa/generic_cse/time,ssa/generic_cse/stats
Spaces in the phase names can be specified with an
underscore. Flags currently parsed (not necessarily
recognized by the phases yet) are:
on, off, mem, time, debug, stats, and test
On, off and time are handled in the harness,
debug, stats, and test are interpreted by the phase itself.
The pass is now attached to the Func being compiled, and a
new method logStats(key, ...value) on *Func to encourage a
semi-standardized format for that output. Output fields
are separated by tabs to ease digestion by awk and
spreadsheets. For example,
if f.pass.stats > 0 {
f.logStat("CSE REWRITES", rewrites)
}
Change-Id: I16db2b5af64c50ca9a47efeb51d961147a903abc
Reviewed-on: https://go-review.googlesource.com/19885
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Todd Neal <todd@tneal.org>
2016-02-25 13:10:51 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 23:21:55 +00:00
|
|
|
// freeValue frees a value. It must no longer be referenced.
|
2016-01-28 13:46:30 -08:00
|
|
|
func (f *Func) freeValue(v *Value) {
|
2016-01-28 15:54:45 -08:00
|
|
|
if v.Block == nil {
|
2016-01-28 13:46:30 -08:00
|
|
|
f.Fatalf("trying to free an already freed value")
|
2015-06-25 23:13:57 -05:00
|
|
|
}
|
2016-03-15 20:45:50 -07:00
|
|
|
if v.Uses != 0 {
|
|
|
|
|
f.Fatalf("value %s still has %d uses", v, v.Uses)
|
|
|
|
|
}
|
2016-01-28 13:46:30 -08:00
|
|
|
// Clear everything but ID (which we reuse).
|
|
|
|
|
id := v.ID
|
2016-03-08 22:13:20 -06:00
|
|
|
|
2017-03-08 12:50:00 -08:00
|
|
|
// Values with zero arguments and OpOffPtr values might be cached, so remove them there.
|
2016-03-08 22:13:20 -06:00
|
|
|
nArgs := opcodeTable[v.Op].argLen
|
2017-03-08 12:50:00 -08:00
|
|
|
if nArgs == 0 || v.Op == OpOffPtr {
|
2016-03-08 22:13:20 -06:00
|
|
|
vv := f.constants[v.AuxInt]
|
|
|
|
|
for i, cv := range vv {
|
|
|
|
|
if v == cv {
|
|
|
|
|
vv[i] = vv[len(vv)-1]
|
2017-03-08 14:48:43 -08:00
|
|
|
vv[len(vv)-1] = nil
|
2016-03-08 22:13:20 -06:00
|
|
|
f.constants[v.AuxInt] = vv[0 : len(vv)-1]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-28 13:46:30 -08:00
|
|
|
*v = Value{}
|
|
|
|
|
v.ID = id
|
|
|
|
|
v.argstorage[0] = f.freeValues
|
|
|
|
|
f.freeValues = v
|
2015-06-25 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 13:46:30 -08:00
|
|
|
// newBlock allocates a new Block of the given kind and places it at the end of f.Blocks.
|
2015-03-03 13:38:14 -08:00
|
|
|
func (f *Func) NewBlock(kind BlockKind) *Block {
|
2016-01-28 13:46:30 -08:00
|
|
|
var b *Block
|
|
|
|
|
if f.freeBlocks != nil {
|
|
|
|
|
b = f.freeBlocks
|
2016-04-28 16:52:47 -07:00
|
|
|
f.freeBlocks = b.succstorage[0].b
|
|
|
|
|
b.succstorage[0].b = nil
|
2016-01-28 13:46:30 -08:00
|
|
|
} else {
|
|
|
|
|
ID := f.bid.get()
|
|
|
|
|
if int(ID) < len(f.Config.blocks) {
|
|
|
|
|
b = &f.Config.blocks[ID]
|
|
|
|
|
} else {
|
|
|
|
|
b = &Block{ID: ID}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-25 23:13:57 -05:00
|
|
|
b.Kind = kind
|
|
|
|
|
b.Func = f
|
2016-01-28 15:54:45 -08:00
|
|
|
b.Preds = b.predstorage[:0]
|
|
|
|
|
b.Succs = b.succstorage[:0]
|
|
|
|
|
b.Values = b.valstorage[:0]
|
2015-03-03 13:38:14 -08:00
|
|
|
f.Blocks = append(f.Blocks, b)
|
2016-09-16 13:50:18 -07:00
|
|
|
f.invalidateCFG()
|
2015-03-03 13:38:14 -08:00
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 13:46:30 -08:00
|
|
|
func (f *Func) freeBlock(b *Block) {
|
2016-01-28 15:54:45 -08:00
|
|
|
if b.Func == nil {
|
|
|
|
|
f.Fatalf("trying to free an already freed block")
|
|
|
|
|
}
|
2016-01-28 13:46:30 -08:00
|
|
|
// Clear everything but ID (which we reuse).
|
|
|
|
|
id := b.ID
|
|
|
|
|
*b = Block{}
|
|
|
|
|
b.ID = id
|
2016-04-28 16:52:47 -07:00
|
|
|
b.succstorage[0].b = f.freeBlocks
|
2016-01-28 13:46:30 -08:00
|
|
|
f.freeBlocks = b
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue0 returns a new value in the block with no arguments and zero aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue0(pos src.XPos, op Op, t Type) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
2015-03-03 13:38:14 -08:00
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue returns a new value in the block with no arguments and an auxint value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue0I(pos src.XPos, op Op, t Type, auxint int64) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue returns a new value in the block with no arguments and an aux value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue0A(pos src.XPos, op Op, t Type, aux interface{}) *Value {
|
2015-06-11 21:29:25 -07:00
|
|
|
if _, ok := aux.(int64); ok {
|
2016-03-01 23:21:55 +00:00
|
|
|
// Disallow int64 aux values. They should be in the auxint field instead.
|
2015-06-11 21:29:25 -07:00
|
|
|
// Maybe we want to allow this at some point, but for now we disallow it
|
|
|
|
|
// to prevent errors like using NewValue1A instead of NewValue1I.
|
2015-06-24 14:03:39 -07:00
|
|
|
b.Fatalf("aux field has int64 type op=%s type=%s aux=%v", op, t, aux)
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
|
|
|
|
v.Aux = aux
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue returns a new value in the block with no arguments and both an auxint and aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue0IA(pos src.XPos, op Op, t Type, auxint int64, aux interface{}) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
|
|
|
|
v.Aux = aux
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1 returns a new value in the block with one argument and zero aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue1(pos src.XPos, op Op, t Type, arg *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
2015-04-15 15:51:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg
|
2016-03-15 20:45:50 -07:00
|
|
|
arg.Uses++
|
2015-04-15 15:51:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue1I returns a new value in the block with one argument and an auxint value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue1I(pos src.XPos, op Op, t Type, auxint int64, arg *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg
|
2016-03-15 20:45:50 -07:00
|
|
|
arg.Uses++
|
2015-06-11 21:29:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1A returns a new value in the block with one argument and an aux value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue1A(pos src.XPos, op Op, t Type, aux interface{}, arg *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
|
|
|
|
v.Aux = aux
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg
|
2016-03-15 20:45:50 -07:00
|
|
|
arg.Uses++
|
2015-06-11 21:29:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1IA returns a new value in the block with one argument and both an auxint and aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue1IA(pos src.XPos, op Op, t Type, auxint int64, aux interface{}, arg *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
|
|
|
|
v.Aux = aux
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg
|
2016-03-15 20:45:50 -07:00
|
|
|
arg.Uses++
|
2015-06-11 21:29:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue2 returns a new value in the block with two arguments and zero aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue2(pos src.XPos, op Op, t Type, arg0, arg1 *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
2015-04-15 15:51:25 -07:00
|
|
|
v.Args = v.argstorage[:2]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg0
|
|
|
|
|
v.argstorage[1] = arg1
|
2016-03-15 20:45:50 -07:00
|
|
|
arg0.Uses++
|
|
|
|
|
arg1.Uses++
|
2015-04-15 15:51:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 15:45:20 +01:00
|
|
|
// NewValue2I returns a new value in the block with two arguments and an auxint value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue2I(pos src.XPos, op Op, t Type, auxint int64, arg0, arg1 *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
2015-06-27 15:45:20 +01:00
|
|
|
v.Args = v.argstorage[:2]
|
2016-01-28 13:46:30 -08:00
|
|
|
v.argstorage[0] = arg0
|
|
|
|
|
v.argstorage[1] = arg1
|
2016-03-15 20:45:50 -07:00
|
|
|
arg0.Uses++
|
|
|
|
|
arg1.Uses++
|
2015-06-27 15:45:20 +01:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue3 returns a new value in the block with three arguments and zero aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue3(pos src.XPos, op Op, t Type, arg0, arg1, arg2 *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = 0
|
2016-04-11 13:17:52 -07:00
|
|
|
v.Args = v.argstorage[:3]
|
|
|
|
|
v.argstorage[0] = arg0
|
|
|
|
|
v.argstorage[1] = arg1
|
|
|
|
|
v.argstorage[2] = arg2
|
2016-03-15 20:45:50 -07:00
|
|
|
arg0.Uses++
|
|
|
|
|
arg1.Uses++
|
|
|
|
|
arg2.Uses++
|
2015-04-15 15:51:25 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-14 21:47:20 -07:00
|
|
|
// NewValue3I returns a new value in the block with three arguments and an auxint value.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue3I(pos src.XPos, op Op, t Type, auxint int64, arg0, arg1, arg2 *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-01-28 13:46:30 -08:00
|
|
|
v.AuxInt = auxint
|
2016-04-11 13:17:52 -07:00
|
|
|
v.Args = v.argstorage[:3]
|
|
|
|
|
v.argstorage[0] = arg0
|
|
|
|
|
v.argstorage[1] = arg1
|
|
|
|
|
v.argstorage[2] = arg2
|
2016-03-15 20:45:50 -07:00
|
|
|
arg0.Uses++
|
|
|
|
|
arg1.Uses++
|
|
|
|
|
arg2.Uses++
|
2015-08-14 21:47:20 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 16:02:57 -07:00
|
|
|
// NewValue4 returns a new value in the block with four arguments and zero aux values.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (b *Block) NewValue4(pos src.XPos, op Op, t Type, arg0, arg1, arg2, arg3 *Value) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := b.Func.newValue(op, t, b, pos)
|
2016-08-25 16:02:57 -07:00
|
|
|
v.AuxInt = 0
|
|
|
|
|
v.Args = []*Value{arg0, arg1, arg2, arg3}
|
|
|
|
|
arg0.Uses++
|
|
|
|
|
arg1.Uses++
|
|
|
|
|
arg2.Uses++
|
|
|
|
|
arg3.Uses++
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:51:11 -08:00
|
|
|
// constVal returns a constant value for c.
|
2017-03-08 12:45:12 -08:00
|
|
|
func (f *Func) constVal(pos src.XPos, op Op, t Type, c int64, setAuxInt bool) *Value {
|
2016-02-28 15:51:11 -08:00
|
|
|
if f.constants == nil {
|
|
|
|
|
f.constants = make(map[int64][]*Value)
|
|
|
|
|
}
|
|
|
|
|
vv := f.constants[c]
|
|
|
|
|
for _, v := range vv {
|
2016-04-03 14:44:29 -07:00
|
|
|
if v.Op == op && v.Type.Compare(t) == CMPeq {
|
2017-03-08 12:45:12 -08:00
|
|
|
if setAuxInt && v.AuxInt != c {
|
2016-03-08 22:13:20 -06:00
|
|
|
panic(fmt.Sprintf("cached const %s should have AuxInt of %d", v.LongString(), c))
|
|
|
|
|
}
|
2016-02-28 15:51:11 -08:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-06 18:06:09 -08:00
|
|
|
var v *Value
|
2017-03-08 12:45:12 -08:00
|
|
|
if setAuxInt {
|
2016-12-08 13:49:51 -08:00
|
|
|
v = f.Entry.NewValue0I(pos, op, t, c)
|
2016-03-06 18:06:09 -08:00
|
|
|
} else {
|
2016-12-08 13:49:51 -08:00
|
|
|
v = f.Entry.NewValue0(pos, op, t)
|
2016-03-06 18:06:09 -08:00
|
|
|
}
|
2016-02-28 15:51:11 -08:00
|
|
|
f.constants[c] = append(vv, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-06 18:06:09 -08:00
|
|
|
// These magic auxint values let us easily cache non-numeric constants
|
|
|
|
|
// using the same constants map while making collisions unlikely.
|
|
|
|
|
// These values are unlikely to occur in regular code and
|
|
|
|
|
// are easy to grep for in case of bugs.
|
|
|
|
|
const (
|
|
|
|
|
constSliceMagic = 1122334455
|
|
|
|
|
constInterfaceMagic = 2233445566
|
|
|
|
|
constNilMagic = 3344556677
|
|
|
|
|
constEmptyStringMagic = 4455667788
|
|
|
|
|
)
|
|
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// ConstInt returns an int constant representing its argument.
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstBool(pos src.XPos, t Type, c bool) *Value {
|
2015-09-08 16:52:25 -07:00
|
|
|
i := int64(0)
|
|
|
|
|
if c {
|
|
|
|
|
i = 1
|
|
|
|
|
}
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConstBool, t, i, true)
|
2015-09-08 16:52:25 -07:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstInt8(pos src.XPos, t Type, c int8) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst8, t, int64(c), true)
|
2015-07-28 14:19:20 -07:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstInt16(pos src.XPos, t Type, c int16) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst16, t, int64(c), true)
|
2015-07-28 14:19:20 -07:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstInt32(pos src.XPos, t Type, c int32) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst32, t, int64(c), true)
|
2015-07-28 14:19:20 -07:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstInt64(pos src.XPos, t Type, c int64) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst64, t, c, true)
|
2015-07-28 14:19:20 -07:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstFloat32(pos src.XPos, t Type, c float64) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst32F, t, int64(math.Float64bits(float64(float32(c)))), true)
|
2015-08-12 16:38:11 -04:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstFloat64(pos src.XPos, t Type, c float64) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConst64F, t, int64(math.Float64bits(c)), true)
|
2016-03-06 18:06:09 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstSlice(pos src.XPos, t Type) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConstSlice, t, constSliceMagic, false)
|
2016-03-06 18:06:09 -08:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstInterface(pos src.XPos, t Type) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConstInterface, t, constInterfaceMagic, false)
|
2016-03-06 18:06:09 -08:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstNil(pos src.XPos, t Type) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
return f.constVal(pos, OpConstNil, t, constNilMagic, false)
|
2016-03-06 18:06:09 -08:00
|
|
|
}
|
2016-12-15 17:17:01 -08:00
|
|
|
func (f *Func) ConstEmptyString(pos src.XPos, t Type) *Value {
|
2016-12-08 13:49:51 -08:00
|
|
|
v := f.constVal(pos, OpConstString, t, constEmptyStringMagic, false)
|
2016-03-06 18:06:09 -08:00
|
|
|
v.Aux = ""
|
|
|
|
|
return v
|
2015-08-12 16:38:11 -04:00
|
|
|
}
|
2017-03-08 12:50:00 -08:00
|
|
|
func (f *Func) ConstOffPtrSP(pos src.XPos, t Type, c int64, sp *Value) *Value {
|
|
|
|
|
v := f.constVal(pos, OpOffPtr, t, c, true)
|
|
|
|
|
if len(v.Args) == 0 {
|
|
|
|
|
v.AddArg(sp)
|
|
|
|
|
}
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
|
}
|
2015-06-12 11:01:13 -07:00
|
|
|
|
2016-01-13 11:14:57 -08:00
|
|
|
func (f *Func) Logf(msg string, args ...interface{}) { f.Config.Logf(msg, args...) }
|
2016-01-29 14:44:15 -05:00
|
|
|
func (f *Func) Log() bool { return f.Config.Log() }
|
2016-12-07 18:14:35 -08:00
|
|
|
func (f *Func) Fatalf(msg string, args ...interface{}) { f.Config.Fatalf(f.Entry.Pos, msg, args...) }
|
2016-01-28 13:46:30 -08:00
|
|
|
|
|
|
|
|
func (f *Func) Free() {
|
2016-09-16 13:50:18 -07:00
|
|
|
// Clear cached CFG info.
|
|
|
|
|
f.invalidateCFG()
|
|
|
|
|
|
2016-01-28 13:46:30 -08:00
|
|
|
// Clear values.
|
|
|
|
|
n := f.vid.num()
|
|
|
|
|
if n > len(f.Config.values) {
|
|
|
|
|
n = len(f.Config.values)
|
|
|
|
|
}
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
|
f.Config.values[i] = Value{}
|
|
|
|
|
f.Config.values[i].ID = ID(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear blocks.
|
|
|
|
|
n = f.bid.num()
|
|
|
|
|
if n > len(f.Config.blocks) {
|
|
|
|
|
n = len(f.Config.blocks)
|
|
|
|
|
}
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
|
f.Config.blocks[i] = Block{}
|
|
|
|
|
f.Config.blocks[i].ID = ID(i)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 12:01:05 -08:00
|
|
|
// Clear locs.
|
|
|
|
|
n = len(f.RegAlloc)
|
|
|
|
|
if n > len(f.Config.locs) {
|
|
|
|
|
n = len(f.Config.locs)
|
|
|
|
|
}
|
|
|
|
|
head := f.Config.locs[:n]
|
|
|
|
|
for i := range head {
|
|
|
|
|
head[i] = nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 13:46:30 -08:00
|
|
|
// Unregister from config.
|
|
|
|
|
if f.Config.curFunc != f {
|
|
|
|
|
f.Fatalf("free of function which isn't the last one allocated")
|
|
|
|
|
}
|
|
|
|
|
f.Config.curFunc = nil
|
|
|
|
|
*f = Func{} // just in case
|
|
|
|
|
}
|
2016-09-16 13:50:18 -07:00
|
|
|
|
|
|
|
|
// postorder returns the reachable blocks in f in a postorder traversal.
|
|
|
|
|
func (f *Func) postorder() []*Block {
|
|
|
|
|
if f.cachedPostorder == nil {
|
|
|
|
|
f.cachedPostorder = postorder(f)
|
|
|
|
|
}
|
|
|
|
|
return f.cachedPostorder
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 01:09:52 +09:00
|
|
|
// Idom returns a map from block ID to the immediate dominator of that block.
|
2016-09-16 13:50:18 -07:00
|
|
|
// f.Entry.ID maps to nil. Unreachable blocks map to nil as well.
|
2016-10-09 01:09:52 +09:00
|
|
|
func (f *Func) Idom() []*Block {
|
2016-09-16 13:50:18 -07:00
|
|
|
if f.cachedIdom == nil {
|
|
|
|
|
f.cachedIdom = dominators(f)
|
|
|
|
|
}
|
|
|
|
|
return f.cachedIdom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sdom returns a sparse tree representing the dominator relationships
|
|
|
|
|
// among the blocks of f.
|
|
|
|
|
func (f *Func) sdom() SparseTree {
|
|
|
|
|
if f.cachedSdom == nil {
|
2016-10-09 01:09:52 +09:00
|
|
|
f.cachedSdom = newSparseTree(f, f.Idom())
|
2016-09-16 13:50:18 -07:00
|
|
|
}
|
|
|
|
|
return f.cachedSdom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// loopnest returns the loop nest information for f.
|
|
|
|
|
func (f *Func) loopnest() *loopnest {
|
|
|
|
|
if f.cachedLoopnest == nil {
|
|
|
|
|
f.cachedLoopnest = loopnestfor(f)
|
|
|
|
|
}
|
|
|
|
|
return f.cachedLoopnest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// invalidateCFG tells f that its CFG has changed.
|
|
|
|
|
func (f *Func) invalidateCFG() {
|
|
|
|
|
f.cachedPostorder = nil
|
|
|
|
|
f.cachedIdom = nil
|
|
|
|
|
f.cachedSdom = nil
|
|
|
|
|
f.cachedLoopnest = nil
|
|
|
|
|
}
|