]> Cypherpunks.ru repositories - gostls13.git/blob - test/inline.go
cmd/compile: reduce inline cost of OCONVOP
[gostls13.git] / test / inline.go
1 // errorcheck -0 -m
2
3 // Copyright 2015 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Test, using compiler diagnostic flags, that inlining is working.
8 // Compiles but does not run.
9
10 package foo
11
12 import (
13         "math"
14         "runtime"
15         "unsafe"
16 )
17
18 func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
19         return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
20 }
21
22 func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
23         return unsafe.Pointer(uintptr(p) + x)
24 }
25
26 func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
27         return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
28 }
29
30 //go:noinline
31 func g(x int) int {
32         return x + 1
33 }
34
35 func h(x int) int { // ERROR "can inline h"
36         return x + 2
37 }
38
39 func i(x int) int { // ERROR "can inline i"
40         const y = 2
41         return x + y
42 }
43
44 func j(x int) int { // ERROR "can inline j"
45         switch {
46         case x > 0:
47                 return x + 2
48         default:
49                 return x + 1
50         }
51 }
52
53 func _() int { // ERROR "can inline _"
54         tmp1 := h
55         tmp2 := tmp1
56         return tmp2(0) // ERROR "inlining call to h"
57 }
58
59 var somethingWrong error
60
61 // local closures can be inlined
62 func l(x, y int) (int, int, error) { // ERROR "can inline l"
63         e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result"
64                 return 0, 0, err
65         }
66         if x == y {
67                 e(somethingWrong) // ERROR "inlining call to l.func1"
68         } else {
69                 f := e
70                 f(nil) // ERROR "inlining call to l.func1"
71         }
72         return y, x, nil
73 }
74
75 // any re-assignment prevents closure inlining
76 func m() int {
77         foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape"
78         x := foo()
79         foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape"
80         return x + foo()
81 }
82
83 // address taking prevents closure inlining
84 func n() int {
85         foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape"
86         bar := &foo
87         x := (*bar)() + foo()
88         return x
89 }
90
91 // make sure assignment inside closure is detected
92 func o() int {
93         foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape"
94         func(x int) {                  // ERROR "can inline o.func2"
95                 if x > 10 {
96                         foo = func() int { return 2 } // ERROR "can inline o.func2"
97                 }
98         }(11) // ERROR "func literal does not escape" "inlining call to o.func2"
99         return foo()
100 }
101
102 func p() int { // ERROR "can inline p"
103         return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1"
104 }
105
106 func q(x int) int { // ERROR "can inline q"
107         foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape"
108         return foo()                       // ERROR "inlining call to q.func1"
109 }
110
111 func r(z int) int {
112         foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape"
113                 return x + z
114         }
115         bar := func(x int) int { // ERROR "func literal does not escape" "can inline r.func2"
116                 return x + func(y int) int { // ERROR "can inline r.func2.1" "can inline r.func3"
117                         return 2*y + x*z
118                 }(x) // ERROR "inlining call to r.func2.1"
119         }
120         return foo(42) + bar(42) // ERROR "inlining call to r.func1" "inlining call to r.func2" "inlining call to r.func3"
121 }
122
123 func s0(x int) int { // ERROR "can inline s0"
124         foo := func() { // ERROR "can inline s0.func1" "func literal does not escape"
125                 x = x + 1
126         }
127         foo() // ERROR "inlining call to s0.func1"
128         return x
129 }
130
131 func s1(x int) int { // ERROR "can inline s1"
132         foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape"
133                 return x
134         }
135         x = x + 1
136         return foo() // ERROR "inlining call to s1.func1"
137 }
138
139 // can't currently inline functions with a break statement
140 func switchBreak(x, y int) int {
141         var n int
142         switch x {
143         case 0:
144                 n = 1
145         Done:
146                 switch y {
147                 case 0:
148                         n += 10
149                         break Done
150                 }
151                 n = 2
152         }
153         return n
154 }
155
156 func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape"
157         switch x.(type) {
158         case int:
159                 return x.(int)
160         default:
161                 return 0
162         }
163 }
164
165 type T struct{}
166
167 func (T) meth(int, int) {} // ERROR "can inline T.meth"
168
169 func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k"
170
171 func _() { // ERROR "can inline _"
172         T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth"
173 }
174
175 func small1() { // ERROR "can inline small1"
176         runtime.GC()
177 }
178 func small2() int { // ERROR "can inline small2"
179         return runtime.GOMAXPROCS(0)
180 }
181 func small3(t T) { // ERROR "can inline small3"
182         t.meth2(3, 5)
183 }
184 func small4(t T) { // not inlineable - has 2 calls.
185         t.meth2(runtime.GOMAXPROCS(0), 5)
186 }
187 func (T) meth2(int, int) { // not inlineable - has 2 calls.
188         runtime.GC()
189         runtime.GC()
190 }
191
192 // Issue #29737 - make sure we can do inlining for a chain of recursive functions
193 func ee() { // ERROR "can inline ee"
194         ff(100) // ERROR "inlining call to ff" "inlining call to gg" "inlining call to hh"
195 }
196
197 func ff(x int) { // ERROR "can inline ff"
198         if x < 0 {
199                 return
200         }
201         gg(x - 1)
202 }
203 func gg(x int) { // ERROR "can inline gg"
204         hh(x - 1)
205 }
206 func hh(x int) { // ERROR "can inline hh"
207         ff(x - 1) // ERROR "inlining call to ff"  // ERROR "inlining call to gg"
208 }
209
210 // Issue #14768 - make sure we can inline for loops.
211 func for1(fn func() bool) { // ERROR "can inline for1" "fn does not escape"
212         for {
213                 if fn() {
214                         break
215                 } else {
216                         continue
217                 }
218         }
219 }
220
221 // BAD: for2 should be inlineable too.
222 func for2(fn func() bool) { // ERROR "fn does not escape"
223 Loop:
224         for {
225                 if fn() {
226                         break Loop
227                 } else {
228                         continue Loop
229                 }
230         }
231 }
232
233 // Issue #18493 - make sure we can do inlining of functions with a method value
234 type T1 struct{}
235
236 func (a T1) meth(val int) int { // ERROR "can inline T1.meth" "inlining call to T1.meth"
237         return val + 5
238 }
239
240 func getMeth(t1 T1) func(int) int { // ERROR "can inline getMeth"
241         return t1.meth // ERROR "t1.meth escapes to heap"
242 }
243
244 func ii() { // ERROR "can inline ii"
245         var t1 T1
246         f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape"
247         _ = f(3)
248 }
249
250 // Issue #42194 - make sure that functions evaluated in
251 // go and defer statements can be inlined.
252 func gd1(int) {
253         defer gd1(gd2()) // ERROR "inlining call to gd2"
254         defer gd3()()    // ERROR "inlining call to gd3"
255         go gd1(gd2())    // ERROR "inlining call to gd2"
256         go gd3()()       // ERROR "inlining call to gd3"
257 }
258
259 func gd2() int { // ERROR "can inline gd2"
260         return 1
261 }
262
263 func gd3() func() { // ERROR "can inline gd3"
264         return ii
265 }
266
267 // Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost.
268 func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape"
269         _ = d[:6]
270         d[0] = math.Float32bits(x[0]) // ERROR "inlining call to math.Float32bits"
271         d[1] = math.Float32bits(x[1]) // ERROR "inlining call to math.Float32bits"
272         d[2] = math.Float32bits(x[2]) // ERROR "inlining call to math.Float32bits"
273         d[3] = math.Float32bits(x[3]) // ERROR "inlining call to math.Float32bits"
274         d[4] = math.Float32bits(x[4]) // ERROR "inlining call to math.Float32bits"
275         d[5] = math.Float32bits(x[5]) // ERROR "inlining call to math.Float32bits"
276 }
277
278 // Ensure OCONVNOP is zero cost.
279 func Conv(v uint64) uint64 { // ERROR "can inline Conv"
280         return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)"
281 }
282 func conv2(v uint64) uint64 { // ERROR "can inline conv2"
283         return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1"
284 }
285 func conv1(v uint64) uint64 { // ERROR "can inline conv1"
286         return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))
287 }