mirror of https://github.com/tinygo-org/tinygo.git
wasmstm32webassemblymicrocontrollerarmavrspiwasiadafruitarduinocircuitplayground-expressgpioi2cllvmmicrobitnrf51nrf52nrf52840samd21tinygo
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
428 lines
12 KiB
428 lines
12 KiB
6 years ago
|
package ir
|
||
7 years ago
|
|
||
|
import (
|
||
7 years ago
|
"go/types"
|
||
6 years ago
|
"sort"
|
||
|
"strings"
|
||
|
|
||
7 years ago
|
"golang.org/x/tools/go/ssa"
|
||
|
)
|
||
|
|
||
6 years ago
|
// This file implements several optimization passes (analysis + transform) to
|
||
|
// optimize code in SSA form before it is compiled to LLVM IR. It is based on
|
||
6 years ago
|
// the IR defined in ir.go.
|
||
|
|
||
6 years ago
|
// Make a readable version of a method signature (including the function name,
|
||
7 years ago
|
// excluding the receiver name). This string is used internally to match
|
||
|
// interfaces and to call the correct method on an interface. Examples:
|
||
|
//
|
||
|
// String() string
|
||
|
// Read([]byte) (int, error)
|
||
6 years ago
|
func MethodSignature(method *types.Func) string {
|
||
|
return method.Name() + Signature(method.Type().(*types.Signature))
|
||
|
}
|
||
|
|
||
|
// Make a readable version of a function (pointer) signature. This string is
|
||
|
// used internally to match signatures (like in AnalyseFunctionPointers).
|
||
|
// Examples:
|
||
|
//
|
||
|
// () string
|
||
|
// (string, int) (int, error)
|
||
|
func Signature(sig *types.Signature) string {
|
||
|
s := ""
|
||
7 years ago
|
if sig.Params().Len() == 0 {
|
||
6 years ago
|
s += "()"
|
||
7 years ago
|
} else {
|
||
6 years ago
|
s += "("
|
||
7 years ago
|
for i := 0; i < sig.Params().Len(); i++ {
|
||
|
if i > 0 {
|
||
6 years ago
|
s += ", "
|
||
7 years ago
|
}
|
||
6 years ago
|
s += sig.Params().At(i).Type().String()
|
||
7 years ago
|
}
|
||
6 years ago
|
s += ")"
|
||
7 years ago
|
}
|
||
|
if sig.Results().Len() == 0 {
|
||
|
// keep as-is
|
||
|
} else if sig.Results().Len() == 1 {
|
||
6 years ago
|
s += " " + sig.Results().At(0).Type().String()
|
||
7 years ago
|
} else {
|
||
6 years ago
|
s += " ("
|
||
7 years ago
|
for i := 0; i < sig.Results().Len(); i++ {
|
||
|
if i > 0 {
|
||
6 years ago
|
s += ", "
|
||
7 years ago
|
}
|
||
6 years ago
|
s += sig.Results().At(i).Type().String()
|
||
7 years ago
|
}
|
||
6 years ago
|
s += ")"
|
||
7 years ago
|
}
|
||
6 years ago
|
return s
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// Convert an interface type to a string of all method strings, separated by
|
||
|
// "; ". For example: "Read([]byte) (int, error); Close() error"
|
||
|
func InterfaceKey(itf *types.Interface) string {
|
||
|
methodStrings := []string{}
|
||
|
for i := 0; i < itf.NumMethods(); i++ {
|
||
|
method := itf.Method(i)
|
||
|
methodStrings = append(methodStrings, MethodSignature(method))
|
||
|
}
|
||
|
sort.Strings(methodStrings)
|
||
|
return strings.Join(methodStrings, ";")
|
||
|
}
|
||
|
|
||
7 years ago
|
// Fill in parents of all functions.
|
||
|
//
|
||
|
// All packages need to be added before this pass can run, or it will produce
|
||
|
// incorrect results.
|
||
6 years ago
|
func (p *Program) AnalyseCallgraph() {
|
||
|
for _, f := range p.Functions {
|
||
|
// Clear, if AnalyseCallgraph has been called before.
|
||
|
f.children = nil
|
||
|
f.parents = nil
|
||
|
|
||
6 years ago
|
for _, block := range f.Blocks {
|
||
6 years ago
|
for _, instr := range block.Instrs {
|
||
|
switch instr := instr.(type) {
|
||
|
case *ssa.Call:
|
||
|
if instr.Common().IsInvoke() {
|
||
|
continue
|
||
|
}
|
||
|
switch call := instr.Call.Value.(type) {
|
||
|
case *ssa.Builtin:
|
||
|
// ignore
|
||
|
case *ssa.Function:
|
||
|
if isCGoInternal(call.Name()) {
|
||
|
continue
|
||
|
}
|
||
|
child := p.GetFunction(call)
|
||
|
if child.CName() != "" {
|
||
|
continue // assume non-blocking
|
||
|
}
|
||
6 years ago
|
if child.RelString(nil) == "time.Sleep" {
|
||
6 years ago
|
f.blocking = true
|
||
|
}
|
||
|
f.children = append(f.children, child)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for _, f := range p.Functions {
|
||
|
for _, child := range f.children {
|
||
|
child.parents = append(child.parents, f)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Find all types that are put in an interface.
|
||
|
func (p *Program) AnalyseInterfaceConversions() {
|
||
|
// Clear, if AnalyseTypes has been called before.
|
||
6 years ago
|
p.typesWithoutMethods = map[string]int{"nil": 0}
|
||
6 years ago
|
p.typesWithMethods = map[string]*TypeWithMethods{}
|
||
6 years ago
|
|
||
|
for _, f := range p.Functions {
|
||
6 years ago
|
for _, block := range f.Blocks {
|
||
6 years ago
|
for _, instr := range block.Instrs {
|
||
|
switch instr := instr.(type) {
|
||
|
case *ssa.MakeInterface:
|
||
6 years ago
|
methods := getAllMethods(f.Prog, instr.X.Type())
|
||
6 years ago
|
name := instr.X.Type().String()
|
||
|
if _, ok := p.typesWithMethods[name]; !ok && len(methods) > 0 {
|
||
6 years ago
|
t := &TypeWithMethods{
|
||
6 years ago
|
t: instr.X.Type(),
|
||
|
Num: len(p.typesWithMethods),
|
||
|
Methods: make(map[string]*types.Selection),
|
||
|
}
|
||
|
for _, sel := range methods {
|
||
6 years ago
|
name := MethodSignature(sel.Obj().(*types.Func))
|
||
6 years ago
|
t.Methods[name] = sel
|
||
|
}
|
||
6 years ago
|
p.typesWithMethods[name] = t
|
||
6 years ago
|
} else if _, ok := p.typesWithoutMethods[name]; !ok && len(methods) == 0 {
|
||
|
p.typesWithoutMethods[name] = len(p.typesWithoutMethods)
|
||
|
}
|
||
|
}
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// Analyse which function pointer signatures need a context parameter.
|
||
|
// This makes calling function pointers more efficient.
|
||
|
func (p *Program) AnalyseFunctionPointers() {
|
||
|
// Clear, if AnalyseFunctionPointers has been called before.
|
||
|
p.fpWithContext = map[string]struct{}{}
|
||
|
|
||
|
for _, f := range p.Functions {
|
||
6 years ago
|
for _, block := range f.Blocks {
|
||
6 years ago
|
for _, instr := range block.Instrs {
|
||
|
switch instr := instr.(type) {
|
||
|
case ssa.CallInstruction:
|
||
|
for _, arg := range instr.Common().Args {
|
||
|
switch arg := arg.(type) {
|
||
|
case *ssa.Function:
|
||
|
f := p.GetFunction(arg)
|
||
|
f.addressTaken = true
|
||
|
}
|
||
|
}
|
||
|
case *ssa.DebugRef:
|
||
|
default:
|
||
|
// For anything that isn't a call...
|
||
|
for _, operand := range instr.Operands(nil) {
|
||
|
if operand == nil || *operand == nil || isCGoInternal((*operand).Name()) {
|
||
|
continue
|
||
|
}
|
||
|
switch operand := (*operand).(type) {
|
||
|
case *ssa.Function:
|
||
|
f := p.GetFunction(operand)
|
||
|
f.addressTaken = true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
switch instr := instr.(type) {
|
||
|
case *ssa.MakeClosure:
|
||
|
fn := instr.Fn.(*ssa.Function)
|
||
|
sig := Signature(fn.Signature)
|
||
|
p.fpWithContext[sig] = struct{}{}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Analyse which functions are recursively blocking.
|
||
|
//
|
||
|
// Depends on AnalyseCallgraph.
|
||
6 years ago
|
func (p *Program) AnalyseBlockingRecursive() {
|
||
|
worklist := make([]*Function, 0)
|
||
7 years ago
|
|
||
|
// Fill worklist with directly blocking functions.
|
||
6 years ago
|
for _, f := range p.Functions {
|
||
|
if f.blocking {
|
||
|
worklist = append(worklist, f)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Keep reducing this worklist by marking a function as recursively blocking
|
||
|
// from the worklist and pushing all its parents that are non-blocking.
|
||
|
// This is somewhat similar to a worklist in a mark-sweep garbage collector.
|
||
|
// The work items are then grey objects.
|
||
|
for len(worklist) != 0 {
|
||
|
// Pick the topmost.
|
||
6 years ago
|
f := worklist[len(worklist)-1]
|
||
7 years ago
|
worklist = worklist[:len(worklist)-1]
|
||
6 years ago
|
for _, parent := range f.parents {
|
||
|
if !parent.blocking {
|
||
|
parent.blocking = true
|
||
|
worklist = append(worklist, parent)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Check whether we need a scheduler. A scheduler is only necessary when there
|
||
|
// are go calls that start blocking functions (if they're not blocking, the go
|
||
|
// function can be turned into a regular function call).
|
||
7 years ago
|
//
|
||
|
// Depends on AnalyseBlockingRecursive.
|
||
6 years ago
|
func (p *Program) AnalyseGoCalls() {
|
||
|
p.goCalls = nil
|
||
|
for _, f := range p.Functions {
|
||
6 years ago
|
for _, block := range f.Blocks {
|
||
6 years ago
|
for _, instr := range block.Instrs {
|
||
|
switch instr := instr.(type) {
|
||
|
case *ssa.Go:
|
||
|
p.goCalls = append(p.goCalls, instr)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for _, instr := range p.goCalls {
|
||
|
switch instr := instr.Call.Value.(type) {
|
||
|
case *ssa.Builtin:
|
||
|
case *ssa.Function:
|
||
|
if p.functionMap[instr].blocking {
|
||
|
p.needsScheduler = true
|
||
|
}
|
||
|
default:
|
||
|
panic("unknown go call function type")
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// Simple pass that removes dead code. This pass makes later analysis passes
|
||
|
// more useful.
|
||
|
func (p *Program) SimpleDCE() {
|
||
6 years ago
|
// Unmark all functions.
|
||
6 years ago
|
for _, f := range p.Functions {
|
||
|
f.flag = false
|
||
|
}
|
||
|
|
||
|
// Initial set of live functions. Include main.main, *.init and runtime.*
|
||
|
// functions.
|
||
|
main := p.mainPkg.Members["main"].(*ssa.Function)
|
||
6 years ago
|
runtimePkg := p.Program.ImportedPackage("runtime")
|
||
6 years ago
|
p.GetFunction(main).flag = true
|
||
|
worklist := []*ssa.Function{main}
|
||
|
for _, f := range p.Functions {
|
||
6 years ago
|
if f.Synthetic == "package initializer" || f.Pkg == runtimePkg {
|
||
|
if f.flag || isCGoInternal(f.Name()) {
|
||
6 years ago
|
continue
|
||
|
}
|
||
|
f.flag = true
|
||
6 years ago
|
worklist = append(worklist, f.Function)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Mark all called functions recursively.
|
||
|
for len(worklist) != 0 {
|
||
|
f := worklist[len(worklist)-1]
|
||
|
worklist = worklist[:len(worklist)-1]
|
||
|
for _, block := range f.Blocks {
|
||
|
for _, instr := range block.Instrs {
|
||
|
if instr, ok := instr.(*ssa.MakeInterface); ok {
|
||
6 years ago
|
for _, sel := range getAllMethods(p.Program, instr.X.Type()) {
|
||
|
fn := p.Program.MethodValue(sel)
|
||
6 years ago
|
callee := p.GetFunction(fn)
|
||
|
if callee == nil {
|
||
|
// TODO: why is this necessary?
|
||
|
p.addFunction(fn)
|
||
|
callee = p.GetFunction(fn)
|
||
|
}
|
||
6 years ago
|
if !callee.flag {
|
||
|
callee.flag = true
|
||
6 years ago
|
worklist = append(worklist, callee.Function)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
for _, operand := range instr.Operands(nil) {
|
||
|
if operand == nil || *operand == nil || isCGoInternal((*operand).Name()) {
|
||
|
continue
|
||
|
}
|
||
|
switch operand := (*operand).(type) {
|
||
|
case *ssa.Function:
|
||
|
f := p.GetFunction(operand)
|
||
6 years ago
|
if f == nil {
|
||
|
// FIXME HACK: this function should have been
|
||
|
// discovered already. It is not for bound methods.
|
||
|
p.addFunction(operand)
|
||
|
f = p.GetFunction(operand)
|
||
|
}
|
||
6 years ago
|
if !f.flag {
|
||
|
f.flag = true
|
||
|
worklist = append(worklist, operand)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Remove unmarked functions.
|
||
6 years ago
|
livefunctions := []*Function{}
|
||
6 years ago
|
for _, f := range p.Functions {
|
||
|
if f.flag {
|
||
|
livefunctions = append(livefunctions, f)
|
||
|
} else {
|
||
6 years ago
|
delete(p.functionMap, f.Function)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
p.Functions = livefunctions
|
||
|
}
|
||
|
|
||
7 years ago
|
// Whether this function needs a scheduler.
|
||
|
//
|
||
|
// Depends on AnalyseGoCalls.
|
||
6 years ago
|
func (p *Program) NeedsScheduler() bool {
|
||
|
return p.needsScheduler
|
||
7 years ago
|
}
|
||
|
|
||
|
// Whether this function blocks. Builtins are also accepted for convenience.
|
||
|
// They will always be non-blocking.
|
||
|
//
|
||
|
// Depends on AnalyseBlockingRecursive.
|
||
6 years ago
|
func (p *Program) IsBlocking(f *Function) bool {
|
||
|
if !p.needsScheduler {
|
||
7 years ago
|
return false
|
||
|
}
|
||
6 years ago
|
return f.blocking
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
// Return the type number and whether this type is actually used. Used in
|
||
|
// interface conversions (type is always used) and type asserts (type may not be
|
||
|
// used, meaning assert is always false in this program).
|
||
|
//
|
||
|
// May only be used after all packages have been added to the analyser.
|
||
6 years ago
|
func (p *Program) TypeNum(typ types.Type) (int, bool) {
|
||
|
if n, ok := p.typesWithoutMethods[typ.String()]; ok {
|
||
7 years ago
|
return n, true
|
||
6 years ago
|
} else if meta, ok := p.typesWithMethods[typ.String()]; ok {
|
||
|
return len(p.typesWithoutMethods) + meta.Num, true
|
||
7 years ago
|
} else {
|
||
|
return -1, false // type is never put in an interface
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// InterfaceNum returns the numeric interface ID of this type, for use in type
|
||
|
// asserts.
|
||
|
func (p *Program) InterfaceNum(itfType *types.Interface) int {
|
||
|
key := InterfaceKey(itfType)
|
||
|
if itf, ok := p.interfaces[key]; !ok {
|
||
|
num := len(p.interfaces)
|
||
|
p.interfaces[key] = &Interface{Num: num, Type: itfType}
|
||
|
return num
|
||
|
} else {
|
||
|
return itf.Num
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// MethodNum returns the numeric ID of this method, to be used in method lookups
|
||
|
// on interfaces for example.
|
||
6 years ago
|
func (p *Program) MethodNum(method *types.Func) int {
|
||
6 years ago
|
name := MethodSignature(method)
|
||
6 years ago
|
if _, ok := p.methodSignatureNames[name]; !ok {
|
||
|
p.methodSignatureNames[name] = len(p.methodSignatureNames)
|
||
7 years ago
|
}
|
||
6 years ago
|
return p.methodSignatureNames[MethodSignature(method)]
|
||
7 years ago
|
}
|
||
|
|
||
|
// The start index of the first dynamic type that has methods.
|
||
|
// Types without methods always have a lower ID and types with methods have this
|
||
|
// or a higher ID.
|
||
|
//
|
||
|
// May only be used after all packages have been added to the analyser.
|
||
6 years ago
|
func (p *Program) FirstDynamicType() int {
|
||
|
return len(p.typesWithoutMethods)
|
||
7 years ago
|
}
|
||
|
|
||
|
// Return all types with methods, sorted by type ID.
|
||
6 years ago
|
func (p *Program) AllDynamicTypes() []*TypeWithMethods {
|
||
|
l := make([]*TypeWithMethods, len(p.typesWithMethods))
|
||
6 years ago
|
for _, m := range p.typesWithMethods {
|
||
7 years ago
|
l[m.Num] = m
|
||
|
}
|
||
|
return l
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// Return all interface types, sorted by interface ID.
|
||
|
func (p *Program) AllInterfaces() []*Interface {
|
||
|
l := make([]*Interface, len(p.interfaces))
|
||
|
for _, itf := range p.interfaces {
|
||
|
l[itf.Num] = itf
|
||
|
}
|
||
|
return l
|
||
|
}
|
||
|
|
||
6 years ago
|
func (p *Program) FunctionNeedsContext(f *Function) bool {
|
||
|
if !f.addressTaken {
|
||
|
return false
|
||
|
}
|
||
6 years ago
|
return p.SignatureNeedsContext(f.Signature)
|
||
6 years ago
|
}
|
||
|
|
||
|
func (p *Program) SignatureNeedsContext(sig *types.Signature) bool {
|
||
|
_, needsContext := p.fpWithContext[Signature(sig)]
|
||
|
return needsContext
|
||
|
}
|