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