github.com/freddyisaac/sicortex-golang@v0.0.0-20231019035217-e03519e66f60/src/cmd/compile/internal/ssa/loop_test.go (about)

     1  // Copyright 2017 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  	"testing"
     9  )
    10  
    11  func TestLoopConditionS390X(t *testing.T) {
    12  	// Test that a simple loop condition does not generate a conditional
    13  	// move (issue #19227).
    14  	//
    15  	// MOVDLT is generated when Less64 is lowered but should be
    16  	// optimized into an LT branch.
    17  	//
    18  	// For example, compiling the following loop:
    19  	//
    20  	//   for i := 0; i < N; i++ {
    21  	//     sum += 3
    22  	//   }
    23  	//
    24  	// should generate assembly similar to:
    25  	//   loop:
    26  	//     CMP    R0, R1
    27  	//     BGE    done
    28  	//     ADD    $3, R4
    29  	//     ADD    $1, R1
    30  	//     BR     loop
    31  	//   done:
    32  	//
    33  	// rather than:
    34  	// loop:
    35  	//     MOVD   $0, R2
    36  	//     MOVD   $1, R3
    37  	//     CMP    R0, R1
    38  	//     MOVDLT R2, R3
    39  	//     CMPW   R2, $0
    40  	//     BNE    done
    41  	//     ADD    $3, R4
    42  	//     ADD    $1, R1
    43  	//     BR     loop
    44  	//   done:
    45  	//
    46  	c := testConfigS390X(t)
    47  	fun := Fun(c, "entry",
    48  		Bloc("entry",
    49  			Valu("mem", OpInitMem, TypeMem, 0, nil),
    50  			Valu("SP", OpSP, TypeUInt64, 0, nil),
    51  			Valu("Nptr", OpOffPtr, TypeInt64Ptr, 8, nil, "SP"),
    52  			Valu("ret", OpOffPtr, TypeInt64Ptr, 16, nil, "SP"),
    53  			Valu("N", OpLoad, TypeInt64, 0, nil, "Nptr", "mem"),
    54  			Valu("starti", OpConst64, TypeInt64, 0, nil),
    55  			Valu("startsum", OpConst64, TypeInt64, 0, nil),
    56  			Goto("b1")),
    57  		Bloc("b1",
    58  			Valu("phii", OpPhi, TypeInt64, 0, nil, "starti", "i"),
    59  			Valu("phisum", OpPhi, TypeInt64, 0, nil, "startsum", "sum"),
    60  			Valu("cmp1", OpLess64, TypeBool, 0, nil, "phii", "N"),
    61  			If("cmp1", "b2", "b3")),
    62  		Bloc("b2",
    63  			Valu("c1", OpConst64, TypeInt64, 1, nil),
    64  			Valu("i", OpAdd64, TypeInt64, 0, nil, "phii", "c1"),
    65  			Valu("c3", OpConst64, TypeInt64, 3, nil),
    66  			Valu("sum", OpAdd64, TypeInt64, 0, nil, "phisum", "c3"),
    67  			Goto("b1")),
    68  		Bloc("b3",
    69  			Valu("store", OpStore, TypeMem, 8, nil, "ret", "phisum", "mem"),
    70  			Exit("store")))
    71  	CheckFunc(fun.f)
    72  	Compile(fun.f)
    73  	CheckFunc(fun.f)
    74  
    75  	checkOpcodeCounts(t, fun.f, map[Op]int{
    76  		OpS390XMOVDLT:    0,
    77  		OpS390XMOVDGT:    0,
    78  		OpS390XMOVDLE:    0,
    79  		OpS390XMOVDGE:    0,
    80  		OpS390XMOVDEQ:    0,
    81  		OpS390XMOVDNE:    0,
    82  		OpS390XCMP:       1,
    83  		OpS390XCMPWconst: 0,
    84  	})
    85  
    86  	fun.f.Free()
    87  }