Skip to content

Commit 24baed7

Browse files
committed
optimize JumpTable; find more constants
before: befoe_early_deadcode: 294839 constants 67689 dce, after_prove: 92576 constants 1303 dce before_lower: 422 constants 160 dce after: befoe_early_deadcode: 301885 constants 67692 dce after_prove: 92603 constants 1314 dce before_lower: 422 constants 171 dce
1 parent e601521 commit 24baed7

File tree

1 file changed

+35
-9
lines changed
  • src/cmd/compile/internal/ssa

1 file changed

+35
-9
lines changed

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ type worklist struct {
6565
defBlock map[*Value][]*Block // use blocks of def
6666
}
6767

68+
// possibleConst checks if Value can be fold to const. For those Values that can never become
69+
// constants(e.g. StaticCall), we don't make futile efforts.
6870
func possibleConst(val *Value) bool {
71+
if isConst(val) {
72+
return true
73+
}
6974
switch val.Op {
70-
case OpConst64, OpConst32, OpConst16, OpConst8,
71-
OpConstBool, OpConst32F, OpConst64F:
72-
fallthrough
7375
case OpCopy:
7476
fallthrough
7577
case OpPhi:
@@ -79,7 +81,7 @@ func possibleConst(val *Value) bool {
7981
OpNeg8, OpNeg16, OpNeg32, OpNeg64, OpNeg32F, OpNeg64F,
8082
OpCom8, OpCom16, OpCom32, OpCom64,
8183
// math
82-
OpFloor, OpCeil, OpTrunc, OpRoundToEven,
84+
OpFloor, OpCeil, OpTrunc, OpRoundToEven, OpSqrt,
8385
// conversion
8486
OpTrunc16to8, OpTrunc32to8, OpTrunc32to16, OpTrunc64to8,
8587
OpTrunc64to16, OpTrunc64to32, OpCvt32to32F, OpCvt32to64F,
@@ -89,6 +91,12 @@ func possibleConst(val *Value) bool {
8991
OpZeroExt8to16, OpZeroExt8to32, OpZeroExt8to64, OpZeroExt16to32,
9092
OpZeroExt16to64, OpZeroExt32to64, OpSignExt8to16, OpSignExt8to32,
9193
OpSignExt8to64, OpSignExt16to32, OpSignExt16to64, OpSignExt32to64,
94+
// bit
95+
OpCtz8, OpCtz16, OpCtz32, OpCtz64,
96+
// mask
97+
OpSlicemask,
98+
// safety check
99+
OpIsNonNil,
92100
// not
93101
OpNot:
94102
fallthrough
@@ -122,7 +130,7 @@ func possibleConst(val *Value) bool {
122130
OpLsh64x64, OpRsh64x64, OpRsh64Ux64, OpLsh32x64,
123131
OpRsh32x64, OpRsh32Ux64, OpLsh16x64, OpRsh16x64,
124132
OpRsh16Ux64, OpLsh8x64, OpRsh8x64, OpRsh8Ux64,
125-
// inbound safety check
133+
// safety check
126134
OpIsInBounds, OpIsSliceInBounds,
127135
// bit
128136
OpAnd8, OpAnd16, OpAnd32, OpAnd64,
@@ -308,7 +316,7 @@ func (t *worklist) visitValue(val *Value) {
308316
OpNeg8, OpNeg16, OpNeg32, OpNeg64, OpNeg32F, OpNeg64F,
309317
OpCom8, OpCom16, OpCom32, OpCom64,
310318
// math
311-
OpFloor, OpCeil, OpTrunc, OpRoundToEven,
319+
OpFloor, OpCeil, OpTrunc, OpRoundToEven, OpSqrt,
312320
// conversion
313321
OpTrunc16to8, OpTrunc32to8, OpTrunc32to16, OpTrunc64to8,
314322
OpTrunc64to16, OpTrunc64to32, OpCvt32to32F, OpCvt32to64F,
@@ -318,6 +326,12 @@ func (t *worklist) visitValue(val *Value) {
318326
OpZeroExt8to16, OpZeroExt8to32, OpZeroExt8to64, OpZeroExt16to32,
319327
OpZeroExt16to64, OpZeroExt32to64, OpSignExt8to16, OpSignExt8to32,
320328
OpSignExt8to64, OpSignExt16to32, OpSignExt16to64, OpSignExt32to64,
329+
// bit
330+
OpCtz8, OpCtz16, OpCtz32, OpCtz64,
331+
// mask
332+
OpSlicemask,
333+
// safety check
334+
OpIsNonNil,
321335
// not
322336
OpNot:
323337
var lt1 = t.getLatticeCell(val.Args[0])
@@ -359,7 +373,7 @@ func (t *worklist) visitValue(val *Value) {
359373
OpLsh64x64, OpRsh64x64, OpRsh64Ux64, OpLsh32x64,
360374
OpRsh32x64, OpRsh32Ux64, OpLsh16x64, OpRsh16x64,
361375
OpRsh16Ux64, OpLsh8x64, OpRsh8x64, OpRsh8Ux64,
362-
// inbound safety check
376+
// safety check
363377
OpIsInBounds, OpIsSliceInBounds,
364378
// bit
365379
OpAnd8, OpAnd16, OpAnd32, OpAnd64,
@@ -449,10 +463,22 @@ func (t *worklist) replaceConst() (int, int) {
449463
block.ResetControls()
450464
rewireCnt++
451465
if t.f.pass.debug > 0 {
452-
fmt.Printf("Rewire %v successors\n", block)
466+
fmt.Printf("Rewire BlockIf %v successors\n", block)
453467
}
454468
case BlockJumpTable:
455-
// TODO: optimize jump table
469+
var idx = int(lt.val.AuxInt)
470+
var targetBlock = block.Succs[idx].b
471+
for len(block.Succs) > 0 {
472+
block.removeEdge(0)
473+
}
474+
block.AddEdgeTo(targetBlock)
475+
block.Kind = BlockPlain
476+
block.Likely = BranchUnknown
477+
block.ResetControls()
478+
rewireCnt++
479+
if t.f.pass.debug > 0 {
480+
fmt.Printf("Rewire JumpTable %v successors\n", block)
481+
}
456482
}
457483
}
458484
}

0 commit comments

Comments
 (0)