Skip to content

Commit 8893da7

Browse files
wdvxdr1123dr2chase
authored andcommitted
cmd/compile: fix wrong optimization for eliding Not in Phi
The previous rule may move the phi value into a wrong block. This CL make it only rewrite the phi value not the If block, so that the phi value will stay in old block. Fixes #56777 Change-Id: I9479a5c7f28529786968413d35b82a16181bb1f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/451496 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Wayne Zuo <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 205f636 commit 8893da7

File tree

3 files changed

+87
-30
lines changed

3 files changed

+87
-30
lines changed

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,11 @@
961961
(NilCheck (GetG mem) mem) => mem
962962

963963
(If (Not cond) yes no) => (If cond no yes)
964-
(If (Phi <t> nx:(Not x) ny:(Not y)) yes no) && nx.Uses == 1 && ny.Uses == 1 => (If (Phi <t> x y) no yes)
965964
(If (ConstBool [c]) yes no) && c => (First yes no)
966965
(If (ConstBool [c]) yes no) && !c => (First no yes)
967966

967+
(Phi <t> nx:(Not x) ny:(Not y)) && nx.Uses == 1 && ny.Uses == 1 => (Not (Phi <t> x y))
968+
968969
// Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
969970
(Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
970971
(Convert (Convert ptr mem) mem) => ptr

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

Lines changed: 29 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue56777.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// compile
2+
3+
// Copyright 2022 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+
package p
8+
9+
func fn(setText []rune, negate bool) int {
10+
ranges := []singleRange{}
11+
12+
if len(setText) > 0 {
13+
fillFirst := false
14+
l := len(setText)
15+
if negate {
16+
if setText[0] == 0 {
17+
setText = setText[1:]
18+
} else {
19+
l++
20+
fillFirst = true
21+
}
22+
}
23+
24+
if l%2 == 0 {
25+
ranges = make([]singleRange, l/2)
26+
} else {
27+
ranges = make([]singleRange, l/2+1)
28+
}
29+
30+
first := true
31+
if fillFirst {
32+
ranges[0] = singleRange{first: 0}
33+
first = false
34+
}
35+
36+
i := 0
37+
for _, r := range setText {
38+
if first {
39+
// lower bound in a new range
40+
ranges[i] = singleRange{first: r}
41+
first = false
42+
} else {
43+
ranges[i].last = r - 1
44+
i++
45+
first = true
46+
}
47+
}
48+
}
49+
50+
return len(ranges)
51+
}
52+
53+
type singleRange struct {
54+
first rune
55+
last rune
56+
}

0 commit comments

Comments
 (0)