]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use more range fors in gc
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 14 Mar 2018 10:50:49 +0000 (10:50 +0000)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 22 Mar 2018 18:38:19 +0000 (18:38 +0000)
Slightly simplifies the code. Made sure to exclude the cases that would
change behavior, such as when the iterated value is a string, when the
index is modified within the body, or when the slice is modified.

Also checked that all the elements are of pointer type, to avoid the
corner case where non-pointer types could be copied by mistake.

Change-Id: Iea64feb2a9a6a4c94ada9ff3ace40ee173505849
Reviewed-on: https://go-review.googlesource.com/100557
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/dwinl.go
src/cmd/compile/internal/gc/phi.go
src/cmd/compile/internal/gc/plive.go
src/cmd/compile/internal/gc/subr.go

index 33c8c8b058aa7fc75783e21f9317c7d5d2056ef1..f5142810617dc5a950dc241916d35a3ed4dd62b1 100644 (file)
@@ -100,8 +100,8 @@ func assembleInlines(fnsym *obj.LSym, dwVars []*dwarf.Var) dwarf.InlCalls {
                var m map[varPos]int
                if ii == 0 {
                        if !fnsym.WasInlined() {
-                               for j := 0; j < len(sl); j++ {
-                                       sl[j].ChildIndex = int32(j)
+                               for j, v := range sl {
+                                       v.ChildIndex = int32(j)
                                }
                                continue
                        }
@@ -121,19 +121,19 @@ func assembleInlines(fnsym *obj.LSym, dwVars []*dwarf.Var) dwarf.InlCalls {
                // parented by the inlined routine and not the top-level
                // caller.
                synthCount := len(m)
-               for j := 0; j < len(sl); j++ {
-                       canonName := unversion(sl[j].Name)
+               for _, v := range sl {
+                       canonName := unversion(v.Name)
                        vp := varPos{
                                DeclName: canonName,
-                               DeclFile: sl[j].DeclFile,
-                               DeclLine: sl[j].DeclLine,
-                               DeclCol:  sl[j].DeclCol,
+                               DeclFile: v.DeclFile,
+                               DeclLine: v.DeclLine,
+                               DeclCol:  v.DeclCol,
                        }
-                       synthesized := strings.HasPrefix(sl[j].Name, "~r") || canonName == "_"
+                       synthesized := strings.HasPrefix(v.Name, "~r") || canonName == "_"
                        if idx, found := m[vp]; found {
-                               sl[j].ChildIndex = int32(idx)
-                               sl[j].IsInAbstract = !synthesized
-                               sl[j].Name = canonName
+                               v.ChildIndex = int32(idx)
+                               v.IsInAbstract = !synthesized
+                               v.Name = canonName
                        } else {
                                // Variable can't be found in the pre-inline dcl list.
                                // In the top-level case (ii=0) this can happen
@@ -141,7 +141,7 @@ func assembleInlines(fnsym *obj.LSym, dwVars []*dwarf.Var) dwarf.InlCalls {
                                // and we're looking at a piece. We can also see
                                // return temps (~r%d) that were created during
                                // lowering, or unnamed params ("_").
-                               sl[j].ChildIndex = int32(synthCount)
+                               v.ChildIndex = int32(synthCount)
                                synthCount += 1
                        }
                }
@@ -215,8 +215,7 @@ func unversion(name string) string {
 func makePreinlineDclMap(fnsym *obj.LSym) map[varPos]int {
        dcl := preInliningDcls(fnsym)
        m := make(map[varPos]int)
-       for i := 0; i < len(dcl); i++ {
-               n := dcl[i]
+       for i, n := range dcl {
                pos := Ctxt.InnermostPos(n.Pos)
                vp := varPos{
                        DeclName: unversion(n.Sym.Name),
@@ -338,7 +337,7 @@ func (s byClassThenName) Less(i, j int) bool { return cmpDwarfVar(s[i], s[j]) }
 func (s byClassThenName) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
 
 func dumpInlCall(inlcalls dwarf.InlCalls, idx, ilevel int) {
-       for i := 0; i < ilevel; i += 1 {
+       for i := 0; i < ilevel; i++ {
                Ctxt.Logf("  ")
        }
        ic := inlcalls.Calls[idx]
@@ -363,9 +362,8 @@ func dumpInlCall(inlcalls dwarf.InlCalls, idx, ilevel int) {
 }
 
 func dumpInlCalls(inlcalls dwarf.InlCalls) {
-       n := len(inlcalls.Calls)
-       for k := 0; k < n; k += 1 {
-               if inlcalls.Calls[k].Root {
+       for k, c := range inlcalls.Calls {
+               if c.Root {
                        dumpInlCall(inlcalls, k, 0)
                }
        }
index b549f0ea6f24399c422772ea3d460228855b8497..bd66568eed0251d8360f4f4159eb81323a27f714 100644 (file)
@@ -241,7 +241,7 @@ func (s *phiState) insertVarPhis(n int, var_ *Node, defs []*ssa.Block, typ *type
                                v := c.NewValue0I(currentRoot.Pos, ssa.OpPhi, typ, int64(n)) // TODO: line number right?
                                // Note: we store the variable number in the phi's AuxInt field. Used temporarily by phi building.
                                s.s.addNamedValue(var_, v)
-                               for i := 0; i < len(c.Preds); i++ {
+                               for range c.Preds {
                                        v.AddArg(s.placeholder) // Actual args will be filled in by resolveFwdRefs.
                                }
                                if debugPhi {
index 60c726ff589e57dc8ef34abfbd87a713b5a2ea34..14e755e1b03b25a09ec090101b21c98a207be559 100644 (file)
@@ -487,8 +487,8 @@ func (lv *Liveness) prologue() {
 
                // Walk the block instructions forward to update avarinit bits.
                // avarinit describes the effect at the end of the block, not the beginning.
-               for j := 0; j < len(b.Values); j++ {
-                       pos, e := lv.valueEffects(b.Values[j])
+               for _, val := range b.Values {
+                       pos, e := lv.valueEffects(val)
                        if e&varkill != 0 {
                                be.avarinit.Unset(pos)
                        }
index b9bf1d34fba65fd53264e38204f4aaebabd62110..a6231963cdfba54b759ab70e7f382235a63cbae7 100644 (file)
@@ -73,9 +73,9 @@ func flusherrors() {
                return
        }
        sort.Stable(byPos(errors))
-       for i := 0; i < len(errors); i++ {
-               if i == 0 || errors[i].msg != errors[i-1].msg {
-                       fmt.Printf("%s", errors[i].msg)
+       for i, err := range errors {
+               if i == 0 || err.msg != errors[i-1].msg {
+                       fmt.Printf("%s", err.msg)
                }
        }
        errors = errors[:0]