Skip to content

Commit 4989337

Browse files
committed
[dev.ssa] cmd/compile: allow control values to be CSEd
With the separate flagalloc pass, it should be fine to allow CSE of control values. The worst that can happen is that the comparison gets un-CSEd by flagalloc. Fix bug in flagalloc where flag restores were getting clobbered by rematerialization during register allocation. Change-Id: If476cf98b69973e8f1a8eb29441136dd12fab8ad Reviewed-on: https://go-review.googlesource.com/17760 Reviewed-by: David Chase <[email protected]> Run-TryBot: Keith Randall <[email protected]>
1 parent c140df0 commit 4989337

6 files changed

Lines changed: 27 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ func cse(f *Func) {
153153
i++
154154
}
155155
}
156-
// TODO(khr): if value is a control value, do we need to keep it block-local?
157156
}
158157
}
159158

@@ -166,6 +165,16 @@ func cse(f *Func) {
166165
}
167166
}
168167
}
168+
if v := b.Control; v != nil {
169+
if x := rewrite[v.ID]; x != nil {
170+
if v.Op == OpNilCheck {
171+
// nilcheck pass will remove the nil checks and log
172+
// them appropriately, so don't mess with them here.
173+
continue
174+
}
175+
b.Control = x
176+
}
177+
}
169178
}
170179
}
171180

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ func flagalloc(f *Func) {
2121
// Walk blocks backwards. Poor-man's postorder traversal.
2222
for i := len(f.Blocks) - 1; i >= 0; i-- {
2323
b := f.Blocks[i]
24+
if len(b.Preds) > 1 {
25+
// Don't use any flags register at the start
26+
// of a merge block. This causes problems
27+
// in regalloc because some of the rematerialization
28+
// instructions used on incoming merge edges clobber
29+
// the flags register.
30+
// TODO: only for architectures where this matters?
31+
continue
32+
}
2433
// Walk values backwards to figure out what flag
2534
// value we want in the flag register at the start
2635
// of the block.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
(If (SETGF cmp) yes no) -> (UGT cmp yes no)
371371
(If (SETGEF cmp) yes no) -> (UGE cmp yes no)
372372
(If (SETEQF cmp) yes no) -> (EQF cmp yes no)
373-
(If (SETNEF cmp) yes no) -> (EQF cmp yes no)
373+
(If (SETNEF cmp) yes no) -> (NEF cmp yes no)
374374

375375
(If cond yes no) -> (NE (TESTB cond cond) yes no)
376376

src/cmd/compile/internal/ssa/gen/AMD64Ops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func init() {
433433
name: "DUFFCOPY",
434434
reg: regInfo{
435435
inputs: []regMask{buildReg("DI"), buildReg("SI")},
436-
clobbers: buildReg("DI SI X0"), // uses X0 as a temporary
436+
clobbers: buildReg("DI SI X0 FLAGS"), // uses X0 as a temporary
437437
},
438438
},
439439

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3177,7 +3177,7 @@ var opcodeTable = [...]opInfo{
31773177
{0, 128}, // .DI
31783178
{1, 64}, // .SI
31793179
},
3180-
clobbers: 65728, // .SI .DI .X0
3180+
clobbers: 8590000320, // .SI .DI .X0 .FLAGS
31813181
},
31823182
},
31833183
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14213,23 +14213,23 @@ func rewriteBlockAMD64(b *Block) bool {
1421314213
;
1421414214
// match: (If (SETNEF cmp) yes no)
1421514215
// cond:
14216-
// result: (EQF cmp yes no)
14216+
// result: (NEF cmp yes no)
1421714217
{
1421814218
v := b.Control
1421914219
if v.Op != OpAMD64SETNEF {
14220-
goto endfe25939ca97349543bc2d2ce4f97ba41
14220+
goto endaa989df10b5bbc5fdf8f7f0b81767e86
1422114221
}
1422214222
cmp := v.Args[0]
1422314223
yes := b.Succs[0]
1422414224
no := b.Succs[1]
14225-
b.Kind = BlockAMD64EQF
14225+
b.Kind = BlockAMD64NEF
1422614226
b.Control = cmp
1422714227
b.Succs[0] = yes
1422814228
b.Succs[1] = no
1422914229
return true
1423014230
}
14231-
goto endfe25939ca97349543bc2d2ce4f97ba41
14232-
endfe25939ca97349543bc2d2ce4f97ba41:
14231+
goto endaa989df10b5bbc5fdf8f7f0b81767e86
14232+
endaa989df10b5bbc5fdf8f7f0b81767e86:
1423314233
;
1423414234
// match: (If cond yes no)
1423514235
// cond:

0 commit comments

Comments
 (0)