aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc/testdata/loadstore.go
blob: 4d67864a6d5baf77ec30d5a392d8261ee0fe5a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// run

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Tests load/store ordering

package main

import "fmt"

// testLoadStoreOrder tests for reordering of stores/loads.
func testLoadStoreOrder() {
	z := uint32(1000)
	if testLoadStoreOrder_ssa(&z, 100) == 0 {
		println("testLoadStoreOrder failed")
		failed = true
	}
}

//go:noinline
func testLoadStoreOrder_ssa(z *uint32, prec uint) int {
	old := *z         // load
	*z = uint32(prec) // store
	if *z < old {     // load
		return 1
	}
	return 0
}

func testStoreSize() {
	a := [4]uint16{11, 22, 33, 44}
	testStoreSize_ssa(&a[0], &a[2], 77)
	want := [4]uint16{77, 22, 33, 44}
	if a != want {
		fmt.Println("testStoreSize failed.  want =", want, ", got =", a)
		failed = true
	}
}

//go:noinline
func testStoreSize_ssa(p *uint16, q *uint16, v uint32) {
	// Test to make sure that (Store ptr (Trunc32to16 val) mem)
	// does not end up as a 32-bit store. It must stay a 16 bit store
	// even when Trunc32to16 is rewritten to be a nop.
	// To ensure that we get rewrite the Trunc32to16 before
	// we rewrite the Store, we force the truncate into an
	// earlier basic block by using it on both branches.
	w := uint16(v)
	if p != nil {
		*p = w
	} else {
		*q = w
	}
}

var failed = false

//go:noinline
func testExtStore_ssa(p *byte, b bool) int {
	x := *p
	*p = 7
	if b {
		return int(x)
	}
	return 0
}

func testExtStore() {
	const start = 8
	var b byte = start
	if got := testExtStore_ssa(&b, true); got != start {
		fmt.Println("testExtStore failed.  want =", start, ", got =", got)
		failed = true
	}
}

var b int

// testDeadStorePanic_ssa ensures that we don't optimize away stores
// that could be read by after recover().  Modeled after fixedbugs/issue1304.
//go:noinline
func testDeadStorePanic_ssa(a int) (r int) {
	defer func() {
		recover()
		r = a
	}()
	a = 2      // store
	b := a - a // optimized to zero
	c := 4
	a = c / b // store, but panics
	a = 3     // store
	r = a
	return
}

func testDeadStorePanic() {
	if want, got := 2, testDeadStorePanic_ssa(1); want != got {
		fmt.Println("testDeadStorePanic failed.  want =", want, ", got =", got)
		failed = true
	}
}

func main() {

	testLoadStoreOrder()
	testStoreSize()
	testExtStore()
	testDeadStorePanic()

	if failed {
		panic("failed")
	}
}