Source file src/cmd/compile/internal/test/testdata/loadstore_test.go

     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  //
    77  //go:noinline
    78  func testDeadStorePanic_ssa(a int) (r int) {
    79  	defer func() {
    80  		recover()
    81  		r = a
    82  	}()
    83  	a = 2      // store
    84  	b := a - a // optimized to zero
    85  	c := 4
    86  	a = c / b // store, but panics
    87  	a = 3     // store
    88  	r = a
    89  	return
    90  }
    91  
    92  func testDeadStorePanic(t *testing.T) {
    93  	if want, got := 2, testDeadStorePanic_ssa(1); want != got {
    94  		t.Errorf("testDeadStorePanic failed.  want = %d, got = %d", want, got)
    95  	}
    96  }
    97  
    98  //go:noinline
    99  func loadHitStore8(x int8, p *int8) int32 {
   100  	x *= x           // try to trash high bits (arch-dependent)
   101  	*p = x           // store
   102  	return int32(*p) // load and cast
   103  }
   104  
   105  //go:noinline
   106  func loadHitStoreU8(x uint8, p *uint8) uint32 {
   107  	x *= x            // try to trash high bits (arch-dependent)
   108  	*p = x            // store
   109  	return uint32(*p) // load and cast
   110  }
   111  
   112  //go:noinline
   113  func loadHitStore16(x int16, p *int16) int32 {
   114  	x *= x           // try to trash high bits (arch-dependent)
   115  	*p = x           // store
   116  	return int32(*p) // load and cast
   117  }
   118  
   119  //go:noinline
   120  func loadHitStoreU16(x uint16, p *uint16) uint32 {
   121  	x *= x            // try to trash high bits (arch-dependent)
   122  	*p = x            // store
   123  	return uint32(*p) // load and cast
   124  }
   125  
   126  //go:noinline
   127  func loadHitStore32(x int32, p *int32) int64 {
   128  	x *= x           // try to trash high bits (arch-dependent)
   129  	*p = x           // store
   130  	return int64(*p) // load and cast
   131  }
   132  
   133  //go:noinline
   134  func loadHitStoreU32(x uint32, p *uint32) uint64 {
   135  	x *= x            // try to trash high bits (arch-dependent)
   136  	*p = x            // store
   137  	return uint64(*p) // load and cast
   138  }
   139  
   140  func testLoadHitStore(t *testing.T) {
   141  	// Test that sign/zero extensions are kept when a load-hit-store
   142  	// is replaced by a register-register move.
   143  	{
   144  		var in int8 = (1 << 6) + 1
   145  		var p int8
   146  		got := loadHitStore8(in, &p)
   147  		want := int32(in * in)
   148  		if got != want {
   149  			t.Errorf("testLoadHitStore (int8) failed. want = %d, got = %d", want, got)
   150  		}
   151  	}
   152  	{
   153  		var in uint8 = (1 << 6) + 1
   154  		var p uint8
   155  		got := loadHitStoreU8(in, &p)
   156  		want := uint32(in * in)
   157  		if got != want {
   158  			t.Errorf("testLoadHitStore (uint8) failed. want = %d, got = %d", want, got)
   159  		}
   160  	}
   161  	{
   162  		var in int16 = (1 << 10) + 1
   163  		var p int16
   164  		got := loadHitStore16(in, &p)
   165  		want := int32(in * in)
   166  		if got != want {
   167  			t.Errorf("testLoadHitStore (int16) failed. want = %d, got = %d", want, got)
   168  		}
   169  	}
   170  	{
   171  		var in uint16 = (1 << 10) + 1
   172  		var p uint16
   173  		got := loadHitStoreU16(in, &p)
   174  		want := uint32(in * in)
   175  		if got != want {
   176  			t.Errorf("testLoadHitStore (uint16) failed. want = %d, got = %d", want, got)
   177  		}
   178  	}
   179  	{
   180  		var in int32 = (1 << 30) + 1
   181  		var p int32
   182  		got := loadHitStore32(in, &p)
   183  		want := int64(in * in)
   184  		if got != want {
   185  			t.Errorf("testLoadHitStore (int32) failed. want = %d, got = %d", want, got)
   186  		}
   187  	}
   188  	{
   189  		var in uint32 = (1 << 30) + 1
   190  		var p uint32
   191  		got := loadHitStoreU32(in, &p)
   192  		want := uint64(in * in)
   193  		if got != want {
   194  			t.Errorf("testLoadHitStore (uint32) failed. want = %d, got = %d", want, got)
   195  		}
   196  	}
   197  }
   198  
   199  func TestLoadStore(t *testing.T) {
   200  	testLoadStoreOrder(t)
   201  	testStoreSize(t)
   202  	testExtStore(t)
   203  	testDeadStorePanic(t)
   204  	testLoadHitStore(t)
   205  }
   206  

View as plain text