// Return true iff we think it might be beneficial to turn this alloc instruction // into a statically allocated global. // Precondition: we are compiling the init function. func (fr *frame) shouldStaticallyAllocate(alloc *ssa.Alloc) bool { // First, see if the allocated type is an array or struct, and if so determine // the number of elements in the type. If the type is anything else, we // statically allocate unconditionally. var numElems int64 switch ty := deref(alloc.Type()).Underlying().(type) { case *types.Array: numElems = ty.Len() case *types.Struct: numElems = int64(ty.NumFields()) default: return true } // We treat the number of referrers to the alloc instruction as a rough // proxy for the number of elements initialized. If the data structure // is densely initialized (> 1/4 elements initialized), enable the // optimization. return int64(len(*alloc.Referrers()))*4 > numElems }