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