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