@@ -210,38 +210,27 @@ func HeapAllocReason(n ir.Node) string {
210
210
if n .Op () == ir .OMAKESLICE {
211
211
n := n .(* ir.MakeExpr )
212
212
213
+ r := & n .Cap
214
+ if n .Cap == nil {
215
+ r = & n .Len
216
+ }
217
+
213
218
// Try to determine static values of make() calls, to avoid allocating them on the heap.
214
219
// We are doing this in escape analysis, so that it happens after inlining and devirtualization.
215
- if n .Cap != nil {
216
- if s := ir .StaticValue (n .Cap ); s .Op () == ir .OLITERAL {
217
- cap , ok := s .(* ir.BasicLit )
218
- if ! ok || cap .Val ().Kind () != constant .Int {
219
- base .Fatalf ("unexpected BasicLit Kind" )
220
- }
221
- if constant .Compare (cap .Val (), token .GEQ , constant .MakeInt64 (0 )) {
222
- n .Cap = s
223
- }
220
+ if s := ir .StaticValue (* r ); s .Op () == ir .OLITERAL {
221
+ lit , ok := s .(* ir.BasicLit )
222
+ if ! ok || lit .Val ().Kind () != constant .Int {
223
+ base .Fatalf ("unexpected BasicLit Kind" )
224
224
}
225
- } else if n .Len != nil {
226
- if s := ir .StaticValue (n .Len ); s .Op () == ir .OLITERAL {
227
- len , ok := s .(* ir.BasicLit )
228
- if ! ok || len .Val ().Kind () != constant .Int {
229
- base .Fatalf ("unexpected BasicLit Kind" )
230
- }
231
- if constant .Compare (len .Val (), token .GEQ , constant .MakeInt64 (0 )) {
232
- n .Len = s
233
- }
225
+ if constant .Compare (lit .Val (), token .GEQ , constant .MakeInt64 (0 )) {
226
+ * r = lit
234
227
}
235
228
}
236
229
237
- r := n .Cap
238
- if r == nil {
239
- r = n .Len
240
- }
241
- if ! ir .IsSmallIntConst (r ) {
230
+ if ! ir .IsSmallIntConst (* r ) {
242
231
return "non-constant size"
243
232
}
244
- if t := n .Type (); t .Elem ().Size () != 0 && ir .Int64Val (r ) > ir .MaxImplicitStackVarSize / t .Elem ().Size () {
233
+ if t := n .Type (); t .Elem ().Size () != 0 && ir .Int64Val (* r ) > ir .MaxImplicitStackVarSize / t .Elem ().Size () {
245
234
return "too large for stack"
246
235
}
247
236
}
0 commit comments