// Copyright 2009 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 walk import ( "go/constant" "internal/buildcfg" "unicode/utf8" "cmd/compile/internal/base" "cmd/compile/internal/ir" "cmd/compile/internal/reflectdata" "cmd/compile/internal/ssagen" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/internal/src" "cmd/internal/sys" ) func cheapComputableIndex(width int64) bool { switch ssagen.Arch.LinkArch.Family { // MIPS does not have R+R addressing // Arm64 may lack ability to generate this code in our assembler, // but the architecture supports it. case sys.PPC64, sys.S390X: return width == 1 case sys.AMD64, sys.I386, sys.ARM64, sys.ARM: switch width { case 1, 2, 4, 8: return true } } return false } // walkRange transforms various forms of ORANGE into // simpler forms. The result must be assigned back to n. // Node n may also be modified in place, and may also be // the returned node. func walkRange(nrange *ir.RangeStmt) ir.Node { base.Assert(!nrange.DistinctVars) // Should all be rewritten before escape analysis if isMapClear(nrange) { return mapRangeClear(nrange) } nfor := ir.NewForStmt(nrange.Pos(), nil, nil, nil, nil, nrange.DistinctVars) nfor.SetInit(nrange.Init()) nfor.Label = nrange.Label // variable name conventions: // ohv1, hv1, hv2: hidden (old) val 1, 2 // ha, hit: hidden aggregate, iterator // hn, hp: hidden len, pointer // hb: hidden bool // a, v1, v2: not hidden aggregate, val 1, 2 a := nrange.X t := a.Type() lno := ir.SetPos(a) v1, v2 := nrange.Key, nrange.Value if ir.IsBlank(v2) { v2 = nil } if ir.IsBlank(v1) && v2 == nil { v1 = nil } if v1 == nil && v2 != nil { base.Fatalf("walkRange: v2 != nil while v1 == nil") } var body []ir.Node var init []ir.Node switch k := t.Kind(); { default: base.Fatalf("walkRange") case types.IsInt[k]: if nn := arrayRangeClear(nrange, v1, v2, a); nn != nil { base.Pos = lno return nn } hv1 := typecheck.TempAt(base.Pos, ir.CurFunc, t) hn := typecheck.TempAt(base.Pos, ir.CurFunc, t) init = append(init, ir.NewAssignStmt(base.Pos, hv1, nil)) init = append(init, ir.NewAssignStmt(base.Pos, hn, a)) nfor.Cond = ir.NewBinaryExpr(base.Pos, ir.OLT, hv1, hn) nfor.Post = ir.NewAssignStmt(base.Pos, hv1, ir.NewBinaryExpr(base.Pos, ir.OADD, hv1, ir.NewInt(base.Pos, 1))) if v1 != nil { body = []ir.Node{rangeAssign(nrange, hv1)} } case k == types.TARRAY, k == types.TSLICE, k == types.TPTR: // TPTR is pointer-to-array if nn := arrayRangeClear(nrange, v1, v2, a); nn != nil { base.Pos = lno return nn } // Element type of the iteration var elem *types.Type switch t.Kind() { case types.TSLICE, types.TARRAY: elem = t.Elem() case types.TPTR: elem = t.Elem().Elem() } // order.stmt arranged for a copy of the array/slice variable if needed. ha := a hv1 := typecheck.TempAt(base.Pos, ir.CurFunc, types.Types[types.TINT]) hn := typecheck.TempAt(base.Pos, ir.CurFunc, types.Types[types.TINT]) init = append(init, ir.NewAssignStmt(base.Pos, hv1, nil)) init = append(init, ir.NewAssignStmt(base.Pos, hn, ir.NewUnaryExpr(base.Pos, ir.OLEN, ha))) nfor.Cond = ir.NewBinaryExpr(base.Pos, ir.OLT, hv1, hn) nfor.Post = ir.NewAssignStmt(base.Pos, hv1, ir.NewBinaryExpr(base.Pos, ir.OADD, hv1, ir.NewInt(base.Pos, 1))) // for range ha { body } if v1 == nil { break } // for v1 := range ha { body } if v2 == nil { body = []ir.Node{rangeAssign(nrange, hv1)} break } // for v1, v2 := range ha { body } if cheapComputableIndex(elem.Size()) { // v1, v2 = hv1, ha[hv1] tmp := ir.NewIndexExpr(base.Pos, ha, hv1) tmp.SetBounded(true) body = []ir.Node{rangeAssign2(nrange, hv1, tmp)} break } // Slice to iterate over var hs ir.Node if t.IsSlice() { hs = ha } else { var arr ir.Node if t.IsPtr() { arr = ha } else { arr = typecheck.NodAddr(ha) arr.SetType(t.PtrTo()) arr.SetTypecheck(1) } hs = ir.NewSliceExpr(base.Pos, ir.OSLICEARR, arr, nil, nil, nil) // old typechecker doesn't know OSLICEARR, so we set types explicitly hs.SetType(types.NewSlice(elem)) hs.SetTypecheck(1) } // We use a "pointer" to keep track of where we are in the backing array // of the slice hs. This pointer starts at hs.ptr and gets incremented // by the element size each time through the loop. // // It's tricky, though, as on the last iteration this pointer gets // incremented to point past the end of the backing array. We can't // let the garbage collector see that final out-of-bounds pointer. // // To avoid this, we keep the "pointer" alternately in 2 variables, one // pointer typed and one uintptr typed. Most of the time it lives in the // regular pointer variable, but when it might be out of bounds (after it // has been incremented, but before the loop condition has been checked) // it lives briefly in the uintptr variable. // // hp contains the pointer version (of type *T, where T is the element type). // It is guaranteed to always be in range, keeps the backing store alive, // and is updated on stack copies. If a GC occurs when this function is // suspended at any safepoint, this variable ensures correct operation. // // hu contains the equivalent uintptr version. It may point past the // end, but doesn't keep the backing store alive and doesn't get updated // on a stack copy. If a GC occurs while this function is on the top of // the stack, then the last frame is scanned conservatively and hu will // act as a reference to the backing array to ensure it is not collected. // // The "pointer" we're moving across the backing array lives in one // or the other of hp and hu as the loop proceeds. // // hp is live during most of the body of the loop. But it isn't live // at the very top of the loop, when we haven't checked i= 0 { // Int/Array case. if !x.Type().IsArray() { return nil } if x.Type().NumElem() != n { return nil } } else { // Slice case. if !ir.SameSafeExpr(x, a) { return nil } } if !ir.SameSafeExpr(lhs.Index, v1) { return nil } if !ir.IsZero(stmt.Y) { return nil } return arrayClear(stmt.Pos(), x, loop) } // arrayClear constructs a call to runtime.memclr for fast zeroing of slices and arrays. func arrayClear(wbPos src.XPos, a ir.Node, nrange *ir.RangeStmt) ir.Node { elemsize := typecheck.RangeExprType(a.Type()).Elem().Size() if elemsize <= 0 { return nil } // Convert to // if len(a) != 0 { // hp = &a[0] // hn = len(a)*sizeof(elem(a)) // memclr{NoHeap,Has}Pointers(hp, hn) // i = len(a) - 1 // } n := ir.NewIfStmt(base.Pos, nil, nil, nil) n.Cond = ir.NewBinaryExpr(base.Pos, ir.ONE, ir.NewUnaryExpr(base.Pos, ir.OLEN, a), ir.NewInt(base.Pos, 0)) // hp = &a[0] hp := typecheck.TempAt(base.Pos, ir.CurFunc, types.Types[types.TUNSAFEPTR]) ix := ir.NewIndexExpr(base.Pos, a, ir.NewInt(base.Pos, 0)) ix.SetBounded(true) addr := typecheck.ConvNop(typecheck.NodAddr(ix), types.Types[types.TUNSAFEPTR]) n.Body.Append(ir.NewAssignStmt(base.Pos, hp, addr)) // hn = len(a) * sizeof(elem(a)) hn := typecheck.TempAt(base.Pos, ir.CurFunc, types.Types[types.TUINTPTR]) mul := typecheck.Conv(ir.NewBinaryExpr(base.Pos, ir.OMUL, ir.NewUnaryExpr(base.Pos, ir.OLEN, a), ir.NewInt(base.Pos, elemsize)), types.Types[types.TUINTPTR]) n.Body.Append(ir.NewAssignStmt(base.Pos, hn, mul)) var fn ir.Node if a.Type().Elem().HasPointers() { // memclrHasPointers(hp, hn) ir.CurFunc.SetWBPos(wbPos) fn = mkcallstmt("memclrHasPointers", hp, hn) } else { // memclrNoHeapPointers(hp, hn) fn = mkcallstmt("memclrNoHeapPointers", hp, hn) } n.Body.Append(fn) // For array range clear, also set "i = len(a) - 1" if nrange != nil { idx := ir.NewAssignStmt(base.Pos, nrange.Key, typecheck.Conv(ir.NewBinaryExpr(base.Pos, ir.OSUB, ir.NewUnaryExpr(base.Pos, ir.OLEN, a), ir.NewInt(base.Pos, 1)), nrange.Key.Type())) n.Body.Append(idx) } n.Cond = typecheck.Expr(n.Cond) n.Cond = typecheck.DefaultLit(n.Cond, nil) typecheck.Stmts(n.Body) return walkStmt(n) }