github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/testdata/loadstore_test.go (about)

     1  // Copyright 2015 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  // Tests load/store ordering
     6  
     7  package main
     8  
     9  import "testing"
    10  
    11  // testLoadStoreOrder tests for reordering of stores/loads.
    12  func testLoadStoreOrder(t *testing.T) {
    13  	z := uint32(1000)
    14  	if testLoadStoreOrder_ssa(&z, 100) == 0 {
    15  		t.Errorf("testLoadStoreOrder failed")
    16  	}
    17  }
    18  
    19  //go:noinline
    20  func testLoadStoreOrder_ssa(z *uint32, prec uint) int {
    21  	old := *z         // load
    22  	*z = uint32(prec) // store
    23  	if *z < old {     // load
    24  		return 1
    25  	}
    26  	return 0
    27  }
    28  
    29  func testStoreSize(t *testing.T) {
    30  	a := [4]uint16{11, 22, 33, 44}
    31  	testStoreSize_ssa(&a[0], &a[2], 77)
    32  	want := [4]uint16{77, 22, 33, 44}
    33  	if a != want {
    34  		t.Errorf("testStoreSize failed.  want = %d, got = %d", want, a)
    35  	}
    36  }
    37  
    38  //go:noinline
    39  func testStoreSize_ssa(p *uint16, q *uint16, v uint32) {
    40  	// Test to make sure that (Store ptr (Trunc32to16 val) mem)
    41  	// does not end up as a 32-bit store. It must stay a 16 bit store
    42  	// even when Trunc32to16 is rewritten to be a nop.
    43  	// To ensure that we get rewrite the Trunc32to16 before
    44  	// we rewrite the Store, we force the truncate into an
    45  	// earlier basic block by using it on both branches.
    46  	w := uint16(v)
    47  	if p != nil {
    48  		*p = w
    49  	} else {
    50  		*q = w
    51  	}
    52  }
    53  
    54  //go:noinline
    55  func testExtStore_ssa(p *byte, b bool) int {
    56  	x := *p
    57  	*p = 7
    58  	if b {
    59  		return int(x)
    60  	}
    61  	return 0
    62  }
    63  
    64  func testExtStore(t *testing.T) {
    65  	const start = 8
    66  	var b byte = start
    67  	if got := testExtStore_ssa(&b, true); got != start {
    68  		t.Errorf("testExtStore failed.  want = %d, got = %d", start, got)
    69  	}
    70  }
    71  
    72  var b int
    73  
    74  // testDeadStorePanic_ssa ensures that we don't optimize away stores
    75  // that could be read by after recover().  Modeled after fixedbugs/issue1304.
    76  //go:noinline
    77  func testDeadStorePanic_ssa(a int) (r int) {
    78  	defer func() {
    79  		recover()
    80  		r = a
    81  	}()
    82  	a = 2      // store
    83  	b := a - a // optimized to zero
    84  	c := 4
    85  	a = c / b // store, but panics
    86  	a = 3     // store
    87  	r = a
    88  	return
    89  }
    90  
    91  func testDeadStorePanic(t *testing.T) {
    92  	if want, got := 2, testDeadStorePanic_ssa(1); want != got {
    93  		t.Errorf("testDeadStorePanic failed.  want = %d, got = %d", want, got)
    94  	}
    95  }
    96  
    97  //go:noinline
    98  func loadHitStore8(x int8, p *int8) int32 {
    99  	x *= x           // try to trash high bits (arch-dependent)
   100  	*p = x           // store
   101  	return int32(*p) // load and cast
   102  }
   103  
   104  //go:noinline
   105  func loadHitStoreU8(x uint8, p *uint8) uint32 {
   106  	x *= x            // try to trash high bits (arch-dependent)
   107  	*p = x            // store
   108  	return uint32(*p) // load and cast
   109  }
   110  
   111  //go:noinline
   112  func loadHitStore16(x int16, p *int16) int32 {
   113  	x *= x           // try to trash high bits (arch-dependent)
   114  	*p = x           // store
   115  	return int32(*p) // load and cast
   116  }
   117  
   118  //go:noinline
   119  func loadHitStoreU16(x uint16, p *uint16) uint32 {
   120  	x *= x            // try to trash high bits (arch-dependent)
   121  	*p = x            // store
   122  	return uint32(*p) // load and cast
   123  }
   124  
   125  //go:noinline
   126  func loadHitStore32(x int32, p *int32) int64 {
   127  	x *= x           // try to trash high bits (arch-dependent)
   128  	*p = x           // store
   129  	return int64(*p) // load and cast
   130  }
   131  
   132  //go:noinline
   133  func loadHitStoreU32(x uint32, p *uint32) uint64 {
   134  	x *= x            // try to trash high bits (arch-dependent)
   135  	*p = x            // store
   136  	return uint64(*p) // load and cast
   137  }
   138  
   139  func testLoadHitStore(t *testing.T) {
   140  	// Test that sign/zero extensions are kept when a load-hit-store
   141  	// is replaced by a register-register move.
   142  	{
   143  		var in int8 = (1 << 6) + 1
   144  		var p int8
   145  		got := loadHitStore8(in, &p)
   146  		want := int32(in * in)
   147  		if got != want {
   148  			t.Errorf("testLoadHitStore (int8) failed. want = %d, got = %d", want, got)
   149  		}
   150  	}
   151  	{
   152  		var in uint8 = (1 << 6) + 1
   153  		var p uint8
   154  		got := loadHitStoreU8(in, &p)
   155  		want := uint32(in * in)
   156  		if got != want {
   157  			t.Errorf("testLoadHitStore (uint8) failed. want = %d, got = %d", want, got)
   158  		}
   159  	}
   160  	{
   161  		var in int16 = (1 << 10) + 1
   162  		var p int16
   163  		got := loadHitStore16(in, &p)
   164  		want := int32(in * in)
   165  		if got != want {
   166  			t.Errorf("testLoadHitStore (int16) failed. want = %d, got = %d", want, got)
   167  		}
   168  	}
   169  	{
   170  		var in uint16 = (1 << 10) + 1
   171  		var p uint16
   172  		got := loadHitStoreU16(in, &p)
   173  		want := uint32(in * in)
   174  		if got != want {
   175  			t.Errorf("testLoadHitStore (uint16) failed. want = %d, got = %d", want, got)
   176  		}
   177  	}
   178  	{
   179  		var in int32 = (1 << 30) + 1
   180  		var p int32
   181  		got := loadHitStore32(in, &p)
   182  		want := int64(in * in)
   183  		if got != want {
   184  			t.Errorf("testLoadHitStore (int32) failed. want = %d, got = %d", want, got)
   185  		}
   186  	}
   187  	{
   188  		var in uint32 = (1 << 30) + 1
   189  		var p uint32
   190  		got := loadHitStoreU32(in, &p)
   191  		want := uint64(in * in)
   192  		if got != want {
   193  			t.Errorf("testLoadHitStore (uint32) failed. want = %d, got = %d", want, got)
   194  		}
   195  	}
   196  }
   197  
   198  func TestLoadStore(t *testing.T) {
   199  	testLoadStoreOrder(t)
   200  	testStoreSize(t)
   201  	testExtStore(t)
   202  	testDeadStorePanic(t)
   203  	testLoadHitStore(t)
   204  }