Source file src/cmd/compile/internal/ssacompile/looprotate_test.go

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

View as plain text