1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:04:57 -07:00

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 <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2016-09-04 16:59:46 -07:00
parent e10286aeda
commit 9a243303b8

View File

@ -1857,19 +1857,12 @@ func (s *state) expr(n *Node) *ssa.Value {
} }
if n.Type.IsFloat() { if n.Type.IsFloat() {
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 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: case OMOD:
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
// do a size-appropriate check for zero return s.intDivide(n, a, b)
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)
case OADD, OSUB: case OADD, OSUB:
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
@ -3230,6 +3223,22 @@ func (s *state) check(cmp *ssa.Value, fn *Node) {
s.startBlock(bNext) 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. // rtcall issues a call to the given runtime function fn with the listed args.
// Returns a slice of results of the given result types. // Returns a slice of results of the given result types.
// The call is added to the end of the current block. // The call is added to the end of the current block.