github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/compile/internal/gc/testdata/dupLoad_test.go (about) 1 // Copyright 2016 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 // This test makes sure that we don't split a single 6 // load up into two separate loads. 7 8 package main 9 10 import "testing" 11 12 //go:noinline 13 func read1(b []byte) (uint16, uint16) { 14 // There is only a single read of b[0]. The two 15 // returned values must have the same low byte. 16 v := b[0] 17 return uint16(v), uint16(v) | uint16(b[1])<<8 18 } 19 20 func main1(t *testing.T) { 21 const N = 100000 22 done := make(chan struct{}) 23 b := make([]byte, 2) 24 go func() { 25 for i := 0; i < N; i++ { 26 b[0] = byte(i) 27 b[1] = byte(i) 28 } 29 done <- struct{}{} 30 }() 31 go func() { 32 for i := 0; i < N; i++ { 33 x, y := read1(b) 34 if byte(x) != byte(y) { 35 t.Fatalf("x=%x y=%x\n", x, y) 36 } 37 } 38 done <- struct{}{} 39 }() 40 <-done 41 <-done 42 } 43 44 //go:noinline 45 func read2(b []byte) (uint16, uint16) { 46 // There is only a single read of b[1]. The two 47 // returned values must have the same high byte. 48 v := uint16(b[1]) << 8 49 return v, uint16(b[0]) | v 50 } 51 52 func main2(t *testing.T) { 53 const N = 100000 54 done := make(chan struct{}) 55 b := make([]byte, 2) 56 go func() { 57 for i := 0; i < N; i++ { 58 b[0] = byte(i) 59 b[1] = byte(i) 60 } 61 done <- struct{}{} 62 }() 63 go func() { 64 for i := 0; i < N; i++ { 65 x, y := read2(b) 66 if x&0xff00 != y&0xff00 { 67 t.Fatalf("x=%x y=%x\n", x, y) 68 } 69 } 70 done <- struct{}{} 71 }() 72 <-done 73 <-done 74 } 75 76 func TestDupLoad(t *testing.T) { 77 main1(t) 78 main2(t) 79 }