aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2017-10-15 22:49:52 +0100
committerEmmanuel Odeke <emm.odeke@gmail.com>2017-10-16 19:59:24 +0000
commitbb45bc27b5d786943bdc058bdae54b1bde78613b (patch)
tree85c50e6576df03d0e16b11612a6162c668fdb227 /src
parente0111bb0f495696e78d4d17c0c0e39b294bb32dd (diff)
downloadgo-bb45bc27b5d786943bdc058bdae54b1bde78613b.tar.xz
cmd/compile: make more use of value switches
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>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/gc/esc.go18
-rw-r--r--src/cmd/compile/internal/gc/noder.go9
-rw-r--r--src/cmd/compile/internal/gc/sinit.go6
-rw-r--r--src/cmd/compile/internal/gc/ssa.go16
-rw-r--r--src/cmd/compile/internal/gc/walk.go77
5 files changed, 58 insertions, 68 deletions
diff --git a/src/cmd/compile/internal/gc/esc.go b/src/cmd/compile/internal/gc/esc.go
index e709751708..7c4fb8b557 100644
--- a/src/cmd/compile/internal/gc/esc.go
+++ b/src/cmd/compile/internal/gc/esc.go
@@ -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 {
diff --git a/src/cmd/compile/internal/gc/noder.go b/src/cmd/compile/internal/gc/noder.go
index f3f18cb918..b33dffb94f 100644
--- a/src/cmd/compile/internal/gc/noder.go
+++ b/src/cmd/compile/internal/gc/noder.go
@@ -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 {
diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go
index 6b681f8bf3..e884ab1692 100644
--- a/src/cmd/compile/internal/gc/sinit.go
+++ b/src/cmd/compile/internal/gc/sinit.go
@@ -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)
}
}
diff --git a/src/cmd/compile/internal/gc/ssa.go b/src/cmd/compile/internal/gc/ssa.go
index 39e06a62b2..3ad84abd10 100644
--- a/src/cmd/compile/internal/gc/ssa.go
+++ b/src/cmd/compile/internal/gc/ssa.go
@@ -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()
diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go
index 58c8808eca..e973de968f 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -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
}