github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/cmd/compile/internal/gc/testdata/loadstore_ssa.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 func main() { 106 107 testLoadStoreOrder() 108 testStoreSize() 109 testExtStore() 110 testDeadStorePanic() 111 112 if failed { 113 panic("failed") 114 } 115 }