| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package ssa |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmd/compile/internal/types" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestSCCPBasic(t *testing.T) { |
| 14 |
c := testConfig(t) |
| 15 |
fun := c.Fun("b1", |
| 16 |
Bloc("b1", |
| 17 |
Valu("mem", OpInitMem, types.TypeMem, 0, nil), |
| 18 |
Valu("v1", OpConst64, c.config.Types.Int64, 20, nil), |
| 19 |
Valu("v2", OpConst64, c.config.Types.Int64, 21, nil), |
| 20 |
Valu("v3", OpConst64F, c.config.Types.Float64, 21.0, nil), |
| 21 |
Valu("v4", OpConstBool, c.config.Types.Bool, 1, nil), |
| 22 |
Valu("t1", OpAdd64, c.config.Types.Int64, 0, nil, "v1", "v2"), |
| 23 |
Valu("t2", OpDiv64, c.config.Types.Int64, 0, nil, "t1", "v1"), |
| 24 |
Valu("t3", OpAdd64, c.config.Types.Int64, 0, nil, "t1", "t2"), |
| 25 |
Valu("t4", OpSub64, c.config.Types.Int64, 0, nil, "t3", "v2"), |
| 26 |
Valu("t5", OpMul64, c.config.Types.Int64, 0, nil, "t4", "v2"), |
| 27 |
Valu("t6", OpMod64, c.config.Types.Int64, 0, nil, "t5", "v2"), |
| 28 |
Valu("t7", OpAnd64, c.config.Types.Int64, 0, nil, "t6", "v2"), |
| 29 |
Valu("t8", OpOr64, c.config.Types.Int64, 0, nil, "t7", "v2"), |
| 30 |
Valu("t9", OpXor64, c.config.Types.Int64, 0, nil, "t8", "v2"), |
| 31 |
Valu("t10", OpNeg64, c.config.Types.Int64, 0, nil, "t9"), |
| 32 |
Valu("t11", OpCom64, c.config.Types.Int64, 0, nil, "t10"), |
| 33 |
Valu("t12", OpNeg64, c.config.Types.Int64, 0, nil, "t11"), |
| 34 |
Valu("t13", OpFloor, c.config.Types.Float64, 0, nil, "v3"), |
| 35 |
Valu("t14", OpSqrt, c.config.Types.Float64, 0, nil, "t13"), |
| 36 |
Valu("t15", OpCeil, c.config.Types.Float64, 0, nil, "t14"), |
| 37 |
Valu("t16", OpTrunc, c.config.Types.Float64, 0, nil, "t15"), |
| 38 |
Valu("t17", OpRoundToEven, c.config.Types.Float64, 0, nil, "t16"), |
| 39 |
Valu("t18", OpTrunc64to32, c.config.Types.Int64, 0, nil, "t12"), |
| 40 |
Valu("t19", OpCvt64Fto64, c.config.Types.Float64, 0, nil, "t17"), |
| 41 |
Valu("t20", OpCtz64, c.config.Types.Int64, 0, nil, "v2"), |
| 42 |
Valu("t21", OpSlicemask, c.config.Types.Int64, 0, nil, "t20"), |
| 43 |
Valu("t22", OpIsNonNil, c.config.Types.Int64, 0, nil, "v2"), |
| 44 |
Valu("t23", OpNot, c.config.Types.Bool, 0, nil, "v4"), |
| 45 |
Valu("t24", OpEq64, c.config.Types.Bool, 0, nil, "v1", "v2"), |
| 46 |
Valu("t25", OpLess64, c.config.Types.Bool, 0, nil, "v1", "v2"), |
| 47 |
Valu("t26", OpLeq64, c.config.Types.Bool, 0, nil, "v1", "v2"), |
| 48 |
Valu("t27", OpEqB, c.config.Types.Bool, 0, nil, "v4", "v4"), |
| 49 |
Valu("t28", OpLsh64x64, c.config.Types.Int64, 0, nil, "v2", "v1"), |
| 50 |
Valu("t29", OpIsInBounds, c.config.Types.Int64, 0, nil, "v2", "v1"), |
| 51 |
Valu("t30", OpIsSliceInBounds, c.config.Types.Int64, 0, nil, "v2", "v1"), |
| 52 |
Goto("b2")), |
| 53 |
Bloc("b2", |
| 54 |
Exit("mem"))) |
| 55 |
sccp(fun.f) |
| 56 |
CheckFunc(fun.f) |
| 57 |
for name, value := range fun.values { |
| 58 |
if strings.HasPrefix(name, "t") { |
| 59 |
if !isConst(value) { |
| 60 |
t.Errorf("Must be constant: %v", value.LongString()) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestSCCPIf(t *testing.T) { |
| 67 |
c := testConfig(t) |
| 68 |
fun := c.Fun("b1", |
| 69 |
Bloc("b1", |
| 70 |
Valu("mem", OpInitMem, types.TypeMem, 0, nil), |
| 71 |
Valu("v1", OpConst64, c.config.Types.Int64, 0, nil), |
| 72 |
Valu("v2", OpConst64, c.config.Types.Int64, 1, nil), |
| 73 |
Valu("cmp", OpLess64, c.config.Types.Bool, 0, nil, "v1", "v2"), |
| 74 |
If("cmp", "b2", "b3")), |
| 75 |
Bloc("b2", |
| 76 |
Valu("v3", OpConst64, c.config.Types.Int64, 3, nil), |
| 77 |
Goto("b4")), |
| 78 |
Bloc("b3", |
| 79 |
Valu("v4", OpConst64, c.config.Types.Int64, 4, nil), |
| 80 |
Goto("b4")), |
| 81 |
Bloc("b4", |
| 82 |
Valu("merge", OpPhi, c.config.Types.Int64, 0, nil, "v3", "v4"), |
| 83 |
Exit("mem"))) |
| 84 |
sccp(fun.f) |
| 85 |
CheckFunc(fun.f) |
| 86 |
for _, b := range fun.blocks { |
| 87 |
for _, v := range b.Values { |
| 88 |
if v == fun.values["merge"] { |
| 89 |
if !isConst(v) { |
| 90 |
t.Errorf("Must be constant: %v", v.LongString()) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |