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