github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/ssa/writebarrier_test.go (about)

     1  // Copyright 2016 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  	"github.com/go-asm/go/cmd/compile/types"
    11  )
    12  
    13  func TestWriteBarrierStoreOrder(t *testing.T) {
    14  	// Make sure writebarrier phase works even StoreWB ops are not in dependency order
    15  	c := testConfig(t)
    16  	ptrType := c.config.Types.BytePtr
    17  	fun := c.Fun("entry",
    18  		Bloc("entry",
    19  			Valu("start", OpInitMem, types.TypeMem, 0, nil),
    20  			Valu("sb", OpSB, c.config.Types.Uintptr, 0, nil),
    21  			Valu("sp", OpSP, c.config.Types.Uintptr, 0, nil),
    22  			Valu("v", OpConstNil, ptrType, 0, nil),
    23  			Valu("addr1", OpAddr, ptrType, 0, nil, "sb"),
    24  			Valu("wb2", OpStore, types.TypeMem, 0, ptrType, "addr1", "v", "wb1"),
    25  			Valu("wb1", OpStore, types.TypeMem, 0, ptrType, "addr1", "v", "start"), // wb1 and wb2 are out of order
    26  			Goto("exit")),
    27  		Bloc("exit",
    28  			Exit("wb2")))
    29  
    30  	CheckFunc(fun.f)
    31  	writebarrier(fun.f)
    32  	CheckFunc(fun.f)
    33  }
    34  
    35  func TestWriteBarrierPhi(t *testing.T) {
    36  	// Make sure writebarrier phase works for single-block loop, where
    37  	// a Phi op takes the store in the same block as argument.
    38  	// See issue #19067.
    39  	c := testConfig(t)
    40  	ptrType := c.config.Types.BytePtr
    41  	fun := c.Fun("entry",
    42  		Bloc("entry",
    43  			Valu("start", OpInitMem, types.TypeMem, 0, nil),
    44  			Valu("sb", OpSB, c.config.Types.Uintptr, 0, nil),
    45  			Valu("sp", OpSP, c.config.Types.Uintptr, 0, nil),
    46  			Goto("loop")),
    47  		Bloc("loop",
    48  			Valu("phi", OpPhi, types.TypeMem, 0, nil, "start", "wb"),
    49  			Valu("v", OpConstNil, ptrType, 0, nil),
    50  			Valu("addr", OpAddr, ptrType, 0, nil, "sb"),
    51  			Valu("wb", OpStore, types.TypeMem, 0, ptrType, "addr", "v", "phi"), // has write barrier
    52  			Goto("loop")))
    53  
    54  	CheckFunc(fun.f)
    55  	writebarrier(fun.f)
    56  	CheckFunc(fun.f)
    57  }