From 9a243303b86a0c5aaf25875a7d07dabd629b0662 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Sun, 4 Sep 2016 16:59:46 -0700 Subject: [PATCH] cmd/compile: omit some temp panicdivide calls 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 TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/cmd/compile/internal/gc/ssa.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/cmd/compile/internal/gc/ssa.go b/src/cmd/compile/internal/gc/ssa.go index 548fd83847..e3c1afb2a2 100644 --- a/src/cmd/compile/internal/gc/ssa.go +++ b/src/cmd/compile/internal/gc/ssa.go @@ -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.