| 1 |
// Copyright 2025 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 TestLoopRotateNested(t *testing.T) { |
| 13 |
c := testConfig(t) |
| 14 |
fun := c.Fun("entry", |
| 15 |
Bloc("entry", |
| 16 |
Valu("mem", OpInitMem, types.TypeMem, 0, nil), |
| 17 |
Valu("constTrue", OpConstBool, types.Types[types.TBOOL], 1, nil), |
| 18 |
Goto("outerHeader")), |
| 19 |
Bloc("outerHeader", |
| 20 |
If("constTrue", "outerBody", "outerExit")), |
| 21 |
Bloc("outerBody", |
| 22 |
Goto("innerHeader")), |
| 23 |
Bloc("innerHeader", |
| 24 |
If("constTrue", "innerBody", "innerExit")), |
| 25 |
Bloc("innerBody", |
| 26 |
Goto("innerTop")), |
| 27 |
Bloc("innerTop", |
| 28 |
Goto("innerHeader")), |
| 29 |
Bloc("innerExit", |
| 30 |
Goto("outerTop")), |
| 31 |
Bloc("outerTop", |
| 32 |
Goto("outerHeader")), |
| 33 |
Bloc("outerExit", |
| 34 |
Exit("mem"))) |
| 35 |
|
| 36 |
blockName := make([]string, len(fun.f.Blocks)+1) |
| 37 |
for name, block := range fun.blocks { |
| 38 |
blockName[block.ID] = name |
| 39 |
} |
| 40 |
|
| 41 |
CheckFunc(fun.f) |
| 42 |
loopRotate(fun.f) |
| 43 |
CheckFunc(fun.f) |
| 44 |
|
| 45 |
// Verify the resulting block order |
| 46 |
expected := []string{ |
| 47 |
"entry", |
| 48 |
"outerTop", |
| 49 |
"outerHeader", |
| 50 |
"outerBody", |
| 51 |
"innerTop", |
| 52 |
"innerHeader", |
| 53 |
"innerBody", |
| 54 |
"innerExit", |
| 55 |
"outerExit", |
| 56 |
} |
| 57 |
if len(expected) != len(fun.f.Blocks) { |
| 58 |
t.Fatalf("expected %d blocks, found %d", len(expected), len(fun.f.Blocks)) |
| 59 |
} |
| 60 |
for i, b := range fun.f.Blocks { |
| 61 |
if expected[i] != blockName[b.ID] { |
| 62 |
t.Errorf("position %d: expected %s, found %s", i, expected[i], blockName[b.ID]) |
| 63 |
} |
| 64 |
} |
| 65 |
} |