Skip to content

Commit

Permalink
cmd/compile: make more use of value switches
Browse files Browse the repository at this point in the history
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í <[email protected]>
Run-TryBot: Emmanuel Odeke <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Emmanuel Odeke <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
mvdan authored and odeke-em committed Oct 16, 2017
1 parent e0111bb commit bb45bc2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 68 deletions.
18 changes: 8 additions & 10 deletions src/cmd/compile/internal/gc/esc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions src/cmd/compile/internal/gc/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/compile/internal/gc/sinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/compile/internal/gc/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
77 changes: 35 additions & 42 deletions src/cmd/compile/internal/gc/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit bb45bc2

Please sign in to comment.