Skip to content

Commit 8d77e45

Browse files
author
eric fang
committed
cmd/compile: fix bug of conditional instructions on arm64
CL 302231 added some optimization rules with instructions CSETM, CSINC, CSINV, and CSNEG, but did not deal with the situation where flag is constant, resulting in some cases that could be more optimized cannot be optimized, and the FlagConstant value is passed to codegen pass. This CL adds these missing rules. Fixes #45359 Change-Id: I700608cfb9a6a768a18d1fd5d374d7e92aa6f838 Reviewed-on: https://go-review.googlesource.com/c/go/+/307650 Reviewed-by: eric fang <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Zhang <[email protected]> Reviewed-by: Alberto Donizetti <[email protected]> Run-TryBot: eric fang <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: eric fang <[email protected]> Trust: Alberto Donizetti <[email protected]>
1 parent 972e883 commit 8d77e45

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/cmd/compile/internal/ssa/gen/ARM64.rules

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,14 @@
15701570
(CSEL [cc] _ y flag) && ccARM64Eval(cc, flag) < 0 => y
15711571
(CSEL0 [cc] x flag) && ccARM64Eval(cc, flag) > 0 => x
15721572
(CSEL0 [cc] _ flag) && ccARM64Eval(cc, flag) < 0 => (MOVDconst [0])
1573+
(CSNEG [cc] x _ flag) && ccARM64Eval(cc, flag) > 0 => x
1574+
(CSNEG [cc] _ y flag) && ccARM64Eval(cc, flag) < 0 => (NEG y)
1575+
(CSINV [cc] x _ flag) && ccARM64Eval(cc, flag) > 0 => x
1576+
(CSINV [cc] _ y flag) && ccARM64Eval(cc, flag) < 0 => (Not y)
1577+
(CSINC [cc] x _ flag) && ccARM64Eval(cc, flag) > 0 => x
1578+
(CSINC [cc] _ y flag) && ccARM64Eval(cc, flag) < 0 => (ADDconst [1] y)
1579+
(CSETM [cc] flag) && ccARM64Eval(cc, flag) > 0 => (MOVDconst [-1])
1580+
(CSETM [cc] flag) && ccARM64Eval(cc, flag) < 0 => (MOVDconst [0])
15731581

15741582
// absorb flags back into boolean CSEL
15751583
(CSEL [cc] x y (CMPWconst [0] boolval)) && cc == OpARM64NotEqual && flagArg(boolval) != nil =>

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

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

test/fixedbugs/issue45359.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile
2+
3+
// Copyright 2021 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 main
8+
9+
func f() {
10+
var i, j int
11+
var b bool
12+
i = -(i &^ i)
13+
for 1>>uint(i) == 0 {
14+
_ = func() {
15+
i, b = 0, true
16+
}
17+
_ = b
18+
i %= j
19+
}
20+
}

0 commit comments

Comments
 (0)