]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: omit some temp panicdivide calls
authorJosh Bleecher Snyder <josharian@gmail.com>
Sun, 4 Sep 2016 23:59:46 +0000 (16:59 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 7 Sep 2016 00:30:10 +0000 (00:30 +0000)
When the divisor is known to be a constant
non-zero, don't insert panicdivide calls
that will just be eliminated later.

The main benefit here is readability of the SSA
form for compiler developers.

Change-Id: Icb7d07fc996941fbaff84524ac3e4b53d8e75fda
Reviewed-on: https://go-review.googlesource.com/28530
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/gc/ssa.go

index 548fd838475efde313d34068de770006319c8cf3..e3c1afb2a2569b5d8c17fe1f2c6075a2aa4cbe73 100644 (file)
@@ -1857,19 +1857,12 @@ func (s *state) expr(n *Node) *ssa.Value {
                }
                if n.Type.IsFloat() {
                        return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
-               } else {
-                       // do a size-appropriate check for zero
-                       cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
-                       s.check(cmp, panicdivide)
-                       return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
                }
+               return s.intDivide(n, a, b)
        case OMOD:
                a := s.expr(n.Left)
                b := s.expr(n.Right)
-               // do a size-appropriate check for zero
-               cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
-               s.check(cmp, panicdivide)
-               return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
+               return s.intDivide(n, a, b)
        case OADD, OSUB:
                a := s.expr(n.Left)
                b := s.expr(n.Right)
@@ -3230,6 +3223,22 @@ func (s *state) check(cmp *ssa.Value, fn *Node) {
        s.startBlock(bNext)
 }
 
+func (s *state) intDivide(n *Node, a, b *ssa.Value) *ssa.Value {
+       needcheck := true
+       switch b.Op {
+       case ssa.OpConst8, ssa.OpConst16, ssa.OpConst32, ssa.OpConst64:
+               if b.AuxInt != 0 {
+                       needcheck = false
+               }
+       }
+       if needcheck {
+               // do a size-appropriate check for zero
+               cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
+               s.check(cmp, panicdivide)
+       }
+       return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
+}
+
 // rtcall issues a call to the given runtime function fn with the listed args.
 // Returns a slice of results of the given result types.
 // The call is added to the end of the current block.