]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: prettify loop iterations
authorMarvin Stenger <marvin.stenger94@gmail.com>
Wed, 23 Mar 2016 15:35:50 +0000 (16:35 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 23 Mar 2016 22:49:49 +0000 (22:49 +0000)
This commit replaces some of

for i := len(x) - 1; i >= 0; i-- {...}

style loops, which do not rely on reverse iteration order.

Change-Id: I5542834286562da058200c06e7a173b13760e54d
Reviewed-on: https://go-review.googlesource.com/21044
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/gc/esc.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/gc/typecheck.go
src/cmd/compile/internal/ssa/dom.go
src/cmd/compile/internal/ssa/stackalloc.go

index 31ba300d7f9386f8e4f84728328bc33d41887bd5..037ddf48221a4cac98d0e9c00930dcb21d89fa28 100644 (file)
@@ -455,15 +455,15 @@ func escAnalyze(all []*Node, recursive bool) {
        e.nodeEscState(&e.theSink).Escloopdepth = -1
        e.recursive = recursive
 
-       for i := len(all) - 1; i >= 0; i-- {
-               if n := all[i]; n.Op == ODCLFUNC {
+       for _, n := range all {
+               if n.Op == ODCLFUNC {
                        n.Esc = EscFuncPlanned
                }
        }
 
        // flow-analyze functions
-       for i := len(all) - 1; i >= 0; i-- {
-               if n := all[i]; n.Op == ODCLFUNC {
+       for _, n := range all {
+               if n.Op == ODCLFUNC {
                        escfunc(e, n)
                }
        }
@@ -477,8 +477,8 @@ func escAnalyze(all []*Node, recursive bool) {
        }
 
        // for all top level functions, tag the typenodes corresponding to the param nodes
-       for i := len(all) - 1; i >= 0; i-- {
-               if n := all[i]; n.Op == ODCLFUNC {
+       for _, n := range all {
+               if n.Op == ODCLFUNC {
                        esctag(e, n)
                }
        }
index 92589a8e0e032e3c66ddd388f509fb0535bd7e46..09b433d7f2b6f630df49ecb4390a355954cea864 100644 (file)
@@ -436,9 +436,7 @@ func Main() {
        if Debug['l'] != 0 {
                // Find functions that can be inlined and clone them before walk expands them.
                visitBottomUp(xtop, func(list []*Node, recursive bool) {
-                       // TODO: use a range statement here if the order does not matter
-                       for i := len(list) - 1; i >= 0; i-- {
-                               n := list[i]
+                       for _, n := range list {
                                if n.Op == ODCLFUNC {
                                        caninl(n)
                                        inlcalls(n)
index 2bc216c75d301c38b7d773d73dc6d7c83c33239a..1f9b1c8b4ad8c388e40c6f40875feb53d0777cc8 100644 (file)
@@ -2801,9 +2801,8 @@ func keydup(n *Node, hash map[uint32][]*Node) {
        case CTSTR:
                h = 0
                s := n.Val().U.(string)
-               for i := len(n.Val().U.(string)); i > 0; i-- {
-                       h = h*PRIME1 + uint32(s[0])
-                       s = s[1:]
+               for i := 0; i < len(s); i++ {
+                       h = h*PRIME1 + uint32(s[i])
                }
        }
 
index 7de8c354a188ddecca95fbbeefb5d5f29c92ee3e..d4dccda05881474ebb5eb51107fafb9f5c552c8c 100644 (file)
@@ -163,11 +163,10 @@ func postDominators(f *Func) []*Block {
 
        // find the exit blocks
        var exits []*Block
-       for i := len(f.Blocks) - 1; i >= 0; i-- {
-               switch f.Blocks[i].Kind {
+       for _, b := range f.Blocks {
+               switch b.Kind {
                case BlockExit, BlockRet, BlockRetJmp, BlockCall, BlockCheck:
-                       exits = append(exits, f.Blocks[i])
-                       break
+                       exits = append(exits, b)
                }
        }
 
index 253c83f1631118098590fed062eb404f7fb131cc..ad8b7be9bae3af15e735a5e0c9f5d617467ce7a6 100644 (file)
@@ -155,7 +155,7 @@ func (s *stackAllocState) stackalloc() {
                slots = make([]int, n)
                s.slots = slots
        }
-       for i := f.NumValues() - 1; i >= 0; i-- {
+       for i := range slots {
                slots[i] = -1
        }