|
| 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 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func checkValueMotion(t *testing.T, fun fun, valName, expectedBlock string) { |
| 13 | + for _, b := range fun.f.Blocks { |
| 14 | + for _, v := range b.Values { |
| 15 | + if v == fun.values[valName] { |
| 16 | + if fun.blocks[expectedBlock] != b { |
| 17 | + t.Errorf("Error: %v\n", v.LongString()) |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// var d int |
| 25 | +// var p = 15 |
| 26 | +// |
| 27 | +// for i := 0; i < 10; i++ { |
| 28 | +// t := 1 * p |
| 29 | +// d = i + t |
| 30 | +// } |
| 31 | +// |
| 32 | +// t should be hoisted to dominator block of loop header |
| 33 | +func TestHoistLoopIVSimple(t *testing.T) { |
| 34 | + c := testConfig(t) |
| 35 | + fun := c.Fun("b1", |
| 36 | + Bloc("b1", |
| 37 | + Valu("mem", OpInitMem, types.TypeMem, 0, nil), |
| 38 | + Valu("zero", OpConst64, c.config.Types.Int64, 0, nil), |
| 39 | + Valu("one", OpConst64, c.config.Types.Int64, 1, nil), |
| 40 | + Valu("ten", OpConst64, c.config.Types.Int64, 10, nil), |
| 41 | + Valu("p", OpConst64, c.config.Types.Int64, 15, nil), |
| 42 | + Goto("b2")), |
| 43 | + Bloc("b2", |
| 44 | + Valu("i", OpPhi, c.config.Types.Int64, 0, nil, "one", "i2"), |
| 45 | + Valu("d", OpPhi, c.config.Types.Int64, 0, nil, "zero", "d2"), |
| 46 | + Valu("cmp", OpLess64, c.config.Types.Bool, 0, nil, "i", "ten"), |
| 47 | + If("cmp", "b3", "b4")), |
| 48 | + Bloc("b3", |
| 49 | + Valu("loopiv", OpMul64, c.config.Types.Int64, 0, nil, "one", "p"), |
| 50 | + Valu("d2", OpAdd64, c.config.Types.Int64, 0, nil, "loopiv", "d"), |
| 51 | + Valu("i2", OpAdd64, c.config.Types.Int64, 0, nil, "i", "one"), |
| 52 | + Goto("b2")), |
| 53 | + Bloc("b4", |
| 54 | + Exit("mem"))) |
| 55 | + |
| 56 | + CheckFunc(fun.f) |
| 57 | + hoistLoopInvariant(fun.f) |
| 58 | + CheckFunc(fun.f) |
| 59 | + checkValueMotion(t, fun, "loopiv", "b1") |
| 60 | +} |
| 61 | + |
| 62 | +func BenchmarkHoistIV1Opt(b *testing.B) { |
| 63 | + var d = 0 |
| 64 | + var a = 3 |
| 65 | + |
| 66 | + for i := 0; i < b.N; i++ { |
| 67 | + d = i + (a*10 - a + 3) |
| 68 | + } |
| 69 | + _ = d |
| 70 | +} |
| 71 | + |
| 72 | +func BenchmarkHoistIV1Manual(b *testing.B) { |
| 73 | + var d = 0 |
| 74 | + var a = 3 |
| 75 | + val := (a*10 - a + 3) |
| 76 | + for i := 0; i < b.N; i++ { |
| 77 | + d = i + val |
| 78 | + } |
| 79 | + _ = d |
| 80 | +} |
| 81 | + |
| 82 | +//go:noinline |
| 83 | +func hoistLoopIV2Opt(n, d int) { |
| 84 | + t := 0 |
| 85 | + for i := 0; i < n*d; i++ { |
| 86 | + t += 1 |
| 87 | + } |
| 88 | + _ = t |
| 89 | +} |
| 90 | + |
| 91 | +//go:noinline |
| 92 | +func hoistLoopIV2Manual(n, d int) { |
| 93 | + t := 0 |
| 94 | + val := n * d |
| 95 | + for i := 0; i < val; i++ { |
| 96 | + t += 1 |
| 97 | + } |
| 98 | + _ = t |
| 99 | +} |
| 100 | + |
| 101 | +func BenchmarkHoistIV2Opt(b *testing.B) { |
| 102 | + for i := 0; i < b.N; i++ { |
| 103 | + hoistLoopIV2Opt(i%10, i%5) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func BenchmarkHoistIV2Manual(b *testing.B) { |
| 108 | + for i := 0; i < b.N; i++ { |
| 109 | + hoistLoopIV2Manual(i%10, i%5) |
| 110 | + } |
| 111 | +} |
0 commit comments