summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/inline/inlheur/names.go
blob: 022385087b7451fd3c99c2c738b3dcee6e9eb997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2023 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 inlheur

import (
	"cmd/compile/internal/ir"
	"go/constant"
)

// nameFinder provides a set of "isXXX" query methods for clients to
// ask whether a given AST node corresponds to a function, a constant
// value, and so on. These methods use an underlying ir.ReassignOracle
// to return more precise results in cases where an "interesting"
// value is assigned to a singly-defined local temp. Example:
//
//	const q = 101
//	fq := func() int { return q }
//	copyOfConstant := q
//	copyOfFunc := f
//	interestingCall(copyOfConstant, copyOfFunc)
//
// A name finder query method invoked on the arguments being passed to
// "interestingCall" will be able detect that 'copyOfConstant' always
// evaluates to a constant (even though it is in fact a PAUTO local
// variable). A given nameFinder can also operate without using
// ir.ReassignOracle (in cases where it is not practical to look
// at the entire function); in such cases queries will still work
// for explicit constant values and functions.
type nameFinder struct {
	ro *ir.ReassignOracle
}

// newNameFinder returns a new nameFinder object with a reassignment
// oracle initialized based on the function fn, or if fn is nil,
// without an underlying ReassignOracle.
func newNameFinder(fn *ir.Func) *nameFinder {
	var ro *ir.ReassignOracle
	if fn != nil {
		ro = &ir.ReassignOracle{}
		ro.Init(fn)
	}
	return &nameFinder{ro: ro}
}

// funcName returns the *ir.Name for the func or method
// corresponding to node 'n', or nil if n can't be proven
// to contain a function value.
func (nf *nameFinder) funcName(n ir.Node) *ir.Name {
	sv := n
	if nf.ro != nil {
		sv = nf.ro.StaticValue(n)
	}
	if name := ir.StaticCalleeName(sv); name != nil {
		return name
	}
	return nil
}

// isAllocatedMem returns true if node n corresponds to a memory
// allocation expression (make, new, or equivalent).
func (nf *nameFinder) isAllocatedMem(n ir.Node) bool {
	sv := n
	if nf.ro != nil {
		sv = nf.ro.StaticValue(n)
	}
	switch sv.Op() {
	case ir.OMAKESLICE, ir.ONEW, ir.OPTRLIT, ir.OSLICELIT:
		return true
	}
	return false
}

// constValue returns the underlying constant.Value for an AST node n
// if n is itself a constant value/expr, or if n is a singly assigned
// local containing constant expr/value (or nil not constant).
func (nf *nameFinder) constValue(n ir.Node) constant.Value {
	sv := n
	if nf.ro != nil {
		sv = nf.ro.StaticValue(n)
	}
	if sv.Op() == ir.OLITERAL {
		return sv.Val()
	}
	return nil
}

// isNil returns whether n is nil (or singly
// assigned local containing nil).
func (nf *nameFinder) isNil(n ir.Node) bool {
	sv := n
	if nf.ro != nil {
		sv = nf.ro.StaticValue(n)
	}
	return sv.Op() == ir.ONIL
}

func (nf *nameFinder) staticValue(n ir.Node) ir.Node {
	if nf.ro == nil {
		return n
	}
	return nf.ro.StaticValue(n)
}

func (nf *nameFinder) reassigned(n *ir.Name) bool {
	if nf.ro == nil {
		return true
	}
	return nf.ro.Reassigned(n)
}

func (nf *nameFinder) isConcreteConvIface(n ir.Node) bool {
	sv := n
	if nf.ro != nil {
		sv = nf.ro.StaticValue(n)
	}
	if sv.Op() != ir.OCONVIFACE {
		return false
	}
	return !sv.(*ir.ConvExpr).X.Type().IsInterface()
}

func isSameFuncName(v1, v2 *ir.Name) bool {
	// NB: there are a few corner cases where pointer equality
	// doesn't work here, but this should be good enough for
	// our purposes here.
	return v1 == v2
}