Skip to content

Commit 401dc02

Browse files
joshariantomocy
authored andcommitted
cmd/compile: handle infinite loops in shortcircuit pass
The newly upgraded shortcircuit pass attempted to remove infinite loops. Stop doing that. Fixes golang#33903 Change-Id: I0fc9c1b5f2427e54ce650806602ef5e3ad65aca5 Reviewed-on: https://go-review.googlesource.com/c/go/+/192144 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 9e3a158 commit 401dc02

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/cmd/compile/internal/ssa/shortcircuit.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func shortcircuit(f *Func) {
5050
}
5151
}
5252

53-
// Step 3: Redirect control flow around known branches.
53+
// Step 2: Redirect control flow around known branches.
5454
// p:
5555
// ... goto b ...
5656
// b: <- p ...
@@ -124,7 +124,6 @@ func shortcircuitBlock(b *Block) bool {
124124
if a.Op != OpConstBool {
125125
continue
126126
}
127-
changed = true
128127
// The predecessor we come in from.
129128
e1 := b.Preds[i]
130129
p := e1.b
@@ -138,8 +137,15 @@ func shortcircuitBlock(b *Block) bool {
138137
}
139138
e2 := b.Succs[si]
140139
t := e2.b
140+
if p == b || t == b {
141+
// This is an infinite loop; we can't remove it. See issue 33903.
142+
continue
143+
}
141144
ti := e2.i
142145

146+
// Update CFG and Phis.
147+
changed = true
148+
143149
// Remove b's incoming edge from p.
144150
b.removePred(i)
145151
n := len(b.Preds)

test/fixedbugs/issue33903.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Check that the shortcircuit pass correctly handles infinite loops.
8+
9+
package p
10+
11+
func f() {
12+
var p, q bool
13+
for {
14+
p = p && q
15+
}
16+
}

0 commit comments

Comments
 (0)