Commit 18559e2d authored by David Chase's avatar David Chase

[dev.ssa] cmd/compile: make zero-divide panic from div/mod explicit

Added an explicit compare-zero and branch-to-panic for
integer division and mod so that other optimizations will
not be fooled by their implicit panics.

Change-Id: Ibf96f636b541c0088861907c537a6beb4b99fa4c
Reviewed-on: https://go-review.googlesource.com/16450Reviewed-by: 's avatarKeith Randall <khr@golang.org>
parent c24681ae
......@@ -858,6 +858,8 @@ var Panicindex *Node
var panicslice *Node
var panicdivide *Node
var throwreturn *Node
var growslice *Node
......
......@@ -339,6 +339,7 @@ func compile(fn *Node) {
Deferreturn = Sysfunc("deferreturn")
Panicindex = Sysfunc("panicindex")
panicslice = Sysfunc("panicslice")
panicdivide = Sysfunc("panicdivide")
throwreturn = Sysfunc("throwreturn")
growslice = Sysfunc("growslice")
typedmemmove_nostore = Sysfunc("typedmemmove_nostore")
......
......@@ -1655,9 +1655,22 @@ func (s *state) expr(n *Node) *ssa.Value {
xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal)
ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag)
}
return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
}
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)
}
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)
case OADD, OSUB:
a := s.expr(n.Left)
......@@ -1670,7 +1683,7 @@ func (s *state) expr(n *Node) *ssa.Value {
s.newValue2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)))
}
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
case OAND, OOR, OMOD, OHMUL, OXOR:
case OAND, OOR, OHMUL, OXOR:
a := s.expr(n.Left)
b := s.expr(n.Right)
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment