]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make more use of value switches
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 15 Oct 2017 21:49:52 +0000 (22:49 +0100)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Mon, 16 Oct 2017 19:59:24 +0000 (19:59 +0000)
Use them to replace if/else chains with at least three comparisons,
where the code becomes clearly simpler.

Passes toolstash -cmp on std cmd.

Change-Id: Ic98aa3905944ddcab5aef5f9d9ba376853263d94
Reviewed-on: https://go-review.googlesource.com/70934
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/esc.go
src/cmd/compile/internal/gc/noder.go
src/cmd/compile/internal/gc/sinit.go
src/cmd/compile/internal/gc/ssa.go
src/cmd/compile/internal/gc/walk.go

index e70975170877e7fd945f1816c87a5258495d4307..7c4fb8b557d0d6592da7f34f0d88d275fd1ef6ec 100644 (file)
@@ -129,7 +129,8 @@ func (v *bottomUpVisitor) visitcode(n *Node, min uint32) uint32 {
        min = v.visitcodelist(n.Nbody, min)
        min = v.visitcodelist(n.Rlist, min)
 
-       if n.Op == OCALLFUNC || n.Op == OCALLMETH {
+       switch n.Op {
+       case OCALLFUNC, OCALLMETH:
                fn := n.Left
                if n.Op == OCALLMETH {
                        fn = asNode(n.Left.Sym.Def)
@@ -140,9 +141,8 @@ func (v *bottomUpVisitor) visitcode(n *Node, min uint32) uint32 {
                                min = m
                        }
                }
-       }
 
-       if n.Op == OCLOSURE {
+       case OCLOSURE:
                m := v.visit(n.Func.Closure)
                if m < min {
                        min = m
@@ -1279,16 +1279,14 @@ func parsetag(note string) uint16 {
 // to the second output (and if there are more than two outputs, there is no flow to those.)
 func describeEscape(em uint16) string {
        var s string
-       if em&EscMask == EscUnknown {
+       switch em & EscMask {
+       case EscUnknown:
                s = "EscUnknown"
-       }
-       if em&EscMask == EscNone {
+       case EscNone:
                s = "EscNone"
-       }
-       if em&EscMask == EscHeap {
+       case EscHeap:
                s = "EscHeap"
-       }
-       if em&EscMask == EscReturn {
+       case EscReturn:
                s = "EscReturn"
        }
        if em&EscContentEscapes != 0 {
index f3f18cb9186f2356bddf4604fbbcc8975ab1806b..b33dffb94f3d6056012a7070da09055360e6feda 100644 (file)
@@ -224,15 +224,14 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
        pack.Sym = my
        pack.Name.Pkg = ipkg
 
-       if my.Name == "." {
+       switch my.Name {
+       case ".":
                importdot(ipkg, pack)
                return
-       }
-       if my.Name == "init" {
+       case "init":
                yyerrorl(pack.Pos, "cannot import package as init - init must be a func")
                return
-       }
-       if my.Name == "_" {
+       case "_":
                return
        }
        if my.Def != nil {
index 6b681f8bf38b62f1251b1b3aa6b8e39d859e2172..e884ab16921c6654575c32a3517a9b850254634b 100644 (file)
@@ -213,10 +213,10 @@ func init2(n *Node, out *[]*Node) {
        init2list(n.Rlist, out)
        init2list(n.Nbody, out)
 
-       if n.Op == OCLOSURE {
+       switch n.Op {
+       case OCLOSURE:
                init2list(n.Func.Closure.Nbody, out)
-       }
-       if n.Op == ODOTMETH || n.Op == OCALLPART {
+       case ODOTMETH, OCALLPART:
                init2(asNode(n.Type.FuncType().Nname), out)
        }
 }
index 39e06a62b242537041c50c5a0a34d37ffb724fed..3ad84abd10c087cb54df7f880a37a38c801e46e7 100644 (file)
@@ -2323,7 +2323,8 @@ func (s *state) append(n *Node, inplace bool) *ssa.Value {
 // This function is intended to handle && and || better than just calling
 // s.expr(cond) and branching on the result.
 func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) {
-       if cond.Op == OANDAND {
+       switch cond.Op {
+       case OANDAND:
                mid := s.f.NewBlock(ssa.BlockPlain)
                s.stmtList(cond.Ninit)
                s.condBranch(cond.Left, mid, no, max8(likely, 0))
@@ -2336,8 +2337,7 @@ func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) {
                // the likeliness of the first branch.
                // TODO: have the frontend give us branch prediction hints for
                // OANDAND and OOROR nodes (if it ever has such info).
-       }
-       if cond.Op == OOROR {
+       case OOROR:
                mid := s.f.NewBlock(ssa.BlockPlain)
                s.stmtList(cond.Ninit)
                s.condBranch(cond.Left, yes, mid, min8(likely, 0))
@@ -2347,8 +2347,7 @@ func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) {
                // Note: if likely==-1, then both recursive calls pass -1.
                // If likely==1, then we don't have enough info to decide
                // the likelihood of the first branch.
-       }
-       if cond.Op == ONOT {
+       case ONOT:
                s.stmtList(cond.Ninit)
                s.condBranch(cond.Left, no, yes, -likely)
                return
@@ -3990,14 +3989,15 @@ func (s *state) referenceTypeBuiltin(n *Node, x *ssa.Value) *ssa.Value {
 
        b.AddEdgeTo(bElse)
        s.startBlock(bElse)
-       if n.Op == OLEN {
+       switch n.Op {
+       case OLEN:
                // length is stored in the first word for map/chan
                s.vars[n] = s.newValue2(ssa.OpLoad, lenType, x, s.mem())
-       } else if n.Op == OCAP {
+       case OCAP:
                // capacity is stored in the second word for chan
                sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Width, x)
                s.vars[n] = s.newValue2(ssa.OpLoad, lenType, sw, s.mem())
-       } else {
+       default:
                s.Fatalf("op must be OLEN or OCAP")
        }
        s.endBlock()
index 58c8808ecaf56e270d917ff029ab671c28ec4275..e973de968fc2447bf16fd5e538f2e8afc61c0c42 100644 (file)
@@ -990,61 +990,55 @@ opswitch:
                n = walkexpr(n, init)
 
        case OCONV, OCONVNOP:
-               if thearch.LinkArch.Family == sys.ARM || thearch.LinkArch.Family == sys.MIPS {
+               switch thearch.LinkArch.Family {
+               case sys.ARM, sys.MIPS:
                        if n.Left.Type.IsFloat() {
-                               if n.Type.Etype == TINT64 {
+                               switch n.Type.Etype {
+                               case TINT64:
                                        n = mkcall("float64toint64", n.Type, init, conv(n.Left, types.Types[TFLOAT64]))
-                                       break
-                               }
-
-                               if n.Type.Etype == TUINT64 {
+                                       break opswitch
+                               case TUINT64:
                                        n = mkcall("float64touint64", n.Type, init, conv(n.Left, types.Types[TFLOAT64]))
-                                       break
+                                       break opswitch
                                }
                        }
 
                        if n.Type.IsFloat() {
-                               if n.Left.Type.Etype == TINT64 {
+                               switch n.Left.Type.Etype {
+                               case TINT64:
                                        n = conv(mkcall("int64tofloat64", types.Types[TFLOAT64], init, conv(n.Left, types.Types[TINT64])), n.Type)
-                                       break
-                               }
-
-                               if n.Left.Type.Etype == TUINT64 {
+                                       break opswitch
+                               case TUINT64:
                                        n = conv(mkcall("uint64tofloat64", types.Types[TFLOAT64], init, conv(n.Left, types.Types[TUINT64])), n.Type)
-                                       break
+                                       break opswitch
                                }
                        }
-               }
 
-               if thearch.LinkArch.Family == sys.I386 {
+               case sys.I386:
                        if n.Left.Type.IsFloat() {
-                               if n.Type.Etype == TINT64 {
+                               switch n.Type.Etype {
+                               case TINT64:
                                        n = mkcall("float64toint64", n.Type, init, conv(n.Left, types.Types[TFLOAT64]))
-                                       break
-                               }
-
-                               if n.Type.Etype == TUINT64 {
+                                       break opswitch
+                               case TUINT64:
                                        n = mkcall("float64touint64", n.Type, init, conv(n.Left, types.Types[TFLOAT64]))
-                                       break
-                               }
-                               if n.Type.Etype == TUINT32 || n.Type.Etype == TUINT || n.Type.Etype == TUINTPTR {
+                                       break opswitch
+                               case TUINT32, TUINT, TUINTPTR:
                                        n = mkcall("float64touint32", n.Type, init, conv(n.Left, types.Types[TFLOAT64]))
-                                       break
+                                       break opswitch
                                }
                        }
                        if n.Type.IsFloat() {
-                               if n.Left.Type.Etype == TINT64 {
+                               switch n.Left.Type.Etype {
+                               case TINT64:
                                        n = conv(mkcall("int64tofloat64", types.Types[TFLOAT64], init, conv(n.Left, types.Types[TINT64])), n.Type)
-                                       break
-                               }
-
-                               if n.Left.Type.Etype == TUINT64 {
+                                       break opswitch
+                               case TUINT64:
                                        n = conv(mkcall("uint64tofloat64", types.Types[TFLOAT64], init, conv(n.Left, types.Types[TUINT64])), n.Type)
-                                       break
-                               }
-                               if n.Left.Type.Etype == TUINT32 || n.Left.Type.Etype == TUINT || n.Left.Type.Etype == TUINTPTR {
+                                       break opswitch
+                               case TUINT32, TUINT, TUINTPTR:
                                        n = conv(mkcall("uint32tofloat64", types.Types[TFLOAT64], init, conv(n.Left, types.Types[TUINT32])), n.Type)
-                                       break
+                                       break opswitch
                                }
                        }
                }
@@ -2419,22 +2413,21 @@ func reorder3save(n *Node, all []*Node, i int, early *[]*Node) *Node {
 // outer value means containing struct or array.
 func outervalue(n *Node) *Node {
        for {
-               if n.Op == OXDOT {
+               switch n.Op {
+               case OXDOT:
                        Fatalf("OXDOT in walk")
-               }
-               if n.Op == ODOT || n.Op == OPAREN || n.Op == OCONVNOP {
-                       n = n.Left
-                       continue
-               }
-
-               if n.Op == OINDEX && n.Left.Type != nil && n.Left.Type.IsArray() {
+               case ODOT, OPAREN, OCONVNOP:
                        n = n.Left
                        continue
+               case OINDEX:
+                       if n.Left.Type != nil && n.Left.Type.IsArray() {
+                               n = n.Left
+                               continue
+                       }
                }
 
                break
        }
-
        return n
 }