Source file src/cmd/compile/internal/ssa/rewrite_test.go

     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  package ssa
     6  
     7  import (
     8  	"cmd/compile/internal/rttype"
     9  	"math"
    10  	"math/rand"
    11  	"reflect"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  // We generate memmove for copy(x[1:], x[:]), however we may change it to OpMove,
    17  // because size is known. Check that OpMove is alias-safe, or we did call memmove.
    18  func TestMove(t *testing.T) {
    19  	x := [...]byte{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}
    20  	copy(x[1:], x[:])
    21  	for i := 1; i < len(x); i++ {
    22  		if int(x[i]) != i {
    23  			t.Errorf("Memmove got converted to OpMove in alias-unsafe way. Got %d instead of %d in position %d", int(x[i]), i, i+1)
    24  		}
    25  	}
    26  }
    27  
    28  func TestMoveSmall(t *testing.T) {
    29  	x := [...]byte{1, 2, 3, 4, 5, 6, 7}
    30  	copy(x[1:], x[:])
    31  	for i := 1; i < len(x); i++ {
    32  		if int(x[i]) != i {
    33  			t.Errorf("Memmove got converted to OpMove in alias-unsafe way. Got %d instead of %d in position %d", int(x[i]), i, i+1)
    34  		}
    35  	}
    36  }
    37  
    38  func TestSubFlags(t *testing.T) {
    39  	if !subFlags32(0, 1).lt() {
    40  		t.Errorf("subFlags32(0,1).lt() returned false")
    41  	}
    42  	if !subFlags32(0, 1).ult() {
    43  		t.Errorf("subFlags32(0,1).ult() returned false")
    44  	}
    45  }
    46  
    47  //go:noinline
    48  func unopt(f func(float64) float64, x float32) float32 {
    49  	return float32(f(float64(x)))
    50  }
    51  
    52  func differ(x, y float32) bool {
    53  	if x != x && y != y {
    54  		// if both are NaN, exact bit pattern of the NaN is uninteresting
    55  		return false
    56  	}
    57  	return math.Float32bits(x) != math.Float32bits(y)
    58  }
    59  
    60  func test32bitUnary(t *testing.T, x float32) {
    61  	if want, got := unopt(math.Round, x), float32(math.Round(float64(x))); differ(want, got) {
    62  		t.Errorf("Optimized 32-bit Round did not match, x=%f, want=%f, got=%f", x, want, got)
    63  	}
    64  	if want, got := unopt(math.RoundToEven, x), float32(math.RoundToEven(float64(x))); differ(want, got) {
    65  		t.Errorf("Optimized 32-bit RoundToEven did not match, x=%f, want=%f, got=%f", x, want, got)
    66  	}
    67  	if want, got := unopt(math.Trunc, x), float32(math.Trunc(float64(x))); differ(want, got) {
    68  		t.Errorf("Optimized 32-bit Trunc did not match, x=%f, want=%f, got=%f", x, want, got)
    69  	}
    70  	if want, got := unopt(math.Ceil, x), float32(math.Ceil(float64(x))); differ(want, got) {
    71  		t.Errorf("Optimized 32-bit Ceil did not match, x=%f, want=%f, got=%f", x, want, got)
    72  	}
    73  	if want, got := unopt(math.Floor, x), float32(math.Floor(float64(x))); differ(want, got) {
    74  		t.Errorf("Optimized 32-bit Floor did not match, x=%f, want=%f, got=%f", x, want, got)
    75  	}
    76  	if x >= 0 {
    77  		if want, got := unopt(math.Sqrt, x), float32(math.Sqrt(float64(x))); differ(want, got) {
    78  			t.Errorf("Optimized 32-bit Sqrt did not match, x=%f, want=%f, got=%f", x, want, got)
    79  		}
    80  	}
    81  	if want, got := unopt(math.Abs, x), float32(math.Abs(float64(x))); differ(want, got) {
    82  		t.Errorf("Optimized 32-bit Abs did not match, x=%f, want=%f, got=%f", x, want, got)
    83  	}
    84  
    85  }
    86  
    87  var zero float32
    88  
    89  func Test32bitUnary(t *testing.T) {
    90  	// this is mostly for testing rounding.
    91  	test32bitUnary(t, -1.5)
    92  	test32bitUnary(t, -0.5)
    93  	test32bitUnary(t, 0.5)
    94  	test32bitUnary(t, 1.5)
    95  
    96  	test32bitUnary(t, -1.4)
    97  	test32bitUnary(t, -0.4)
    98  	test32bitUnary(t, 0.4)
    99  	test32bitUnary(t, 1.4)
   100  
   101  	test32bitUnary(t, -1.6)
   102  	test32bitUnary(t, -0.6)
   103  	test32bitUnary(t, 0.6)
   104  	test32bitUnary(t, 1.6)
   105  
   106  	// negative zero
   107  	test32bitUnary(t, 1/(-1/zero))
   108  
   109  	var rnd = rand.New(rand.NewSource(0))
   110  
   111  	for i := uint32(0); i <= 1<<20; i++ {
   112  		test32bitUnary(t, math.Float32frombits(math.Float32bits(math.MaxFloat32)-i))
   113  		test32bitUnary(t, float32(i)+1.5)
   114  		test32bitUnary(t, math.Float32frombits(rnd.Uint32()))
   115  	}
   116  
   117  }
   118  
   119  func TestIsPPC64WordRotateMask(t *testing.T) {
   120  	tests := []struct {
   121  		input    int64
   122  		expected bool
   123  	}{
   124  		{0x00000001, true},
   125  		{0x80000001, true},
   126  		{0x80010001, false},
   127  		{0xFFFFFFFA, false},
   128  		{0xF0F0F0F0, false},
   129  		{0xFFFFFFFD, true},
   130  		{0x80000000, true},
   131  		{0x00000000, false},
   132  		{0xFFFFFFFF, true},
   133  		{0x0000FFFF, true},
   134  		{0xFF0000FF, true},
   135  		{0x00FFFF00, true},
   136  	}
   137  
   138  	for _, v := range tests {
   139  		if v.expected != isPPC64WordRotateMask(v.input) {
   140  			t.Errorf("isPPC64WordRotateMask(0x%x) failed", v.input)
   141  		}
   142  	}
   143  }
   144  
   145  func TestEncodeDecodePPC64WordRotateMask(t *testing.T) {
   146  	tests := []struct {
   147  		rotate int64
   148  		mask   uint64
   149  		nbits,
   150  		mb,
   151  		me,
   152  		encoded int64
   153  	}{
   154  		{1, 0x00000001, 32, 31, 31, 0x20011f20},
   155  		{2, 0x80000001, 32, 31, 0, 0x20021f01},
   156  		{3, 0xFFFFFFFD, 32, 31, 29, 0x20031f1e},
   157  		{4, 0x80000000, 32, 0, 0, 0x20040001},
   158  		{5, 0xFFFFFFFF, 32, 0, 31, 0x20050020},
   159  		{6, 0x0000FFFF, 32, 16, 31, 0x20061020},
   160  		{7, 0xFF0000FF, 32, 24, 7, 0x20071808},
   161  		{8, 0x00FFFF00, 32, 8, 23, 0x20080818},
   162  
   163  		{9, 0x0000000000FFFF00, 64, 40, 55, 0x40092838},
   164  		{10, 0xFFFF000000000000, 64, 0, 15, 0x400A0010},
   165  		{10, 0xFFFF000000000001, 64, 63, 15, 0x400A3f10},
   166  	}
   167  
   168  	for i, v := range tests {
   169  		result := encodePPC64RotateMask(v.rotate, int64(v.mask), v.nbits)
   170  		if result != v.encoded {
   171  			t.Errorf("encodePPC64RotateMask(%d,0x%x,%d) = 0x%x, expected 0x%x", v.rotate, v.mask, v.nbits, result, v.encoded)
   172  		}
   173  		rotate, mb, me, mask := DecodePPC64RotateMask(result)
   174  		if rotate != v.rotate || mb != v.mb || me != v.me || mask != v.mask {
   175  			t.Errorf("DecodePPC64Failure(Test %d) got (%d, %d, %d, %x) expected (%d, %d, %d, %x)", i, rotate, mb, me, mask, v.rotate, v.mb, v.me, v.mask)
   176  		}
   177  	}
   178  }
   179  
   180  func TestMergePPC64ClrlsldiSrw(t *testing.T) {
   181  	tests := []struct {
   182  		clrlsldi int32
   183  		srw      int64
   184  		valid    bool
   185  		rotate   int64
   186  		mask     uint64
   187  	}{
   188  		// ((x>>4)&0xFF)<<4
   189  		{newPPC64ShiftAuxInt(4, 56, 63, 64), 4, true, 0, 0xFF0},
   190  		// ((x>>4)&0xFFFF)<<4
   191  		{newPPC64ShiftAuxInt(4, 48, 63, 64), 4, true, 0, 0xFFFF0},
   192  		// ((x>>4)&0xFFFF)<<17
   193  		{newPPC64ShiftAuxInt(17, 48, 63, 64), 4, false, 0, 0},
   194  		// ((x>>4)&0xFFFF)<<16
   195  		{newPPC64ShiftAuxInt(16, 48, 63, 64), 4, true, 12, 0xFFFF0000},
   196  		// ((x>>32)&0xFFFF)<<17
   197  		{newPPC64ShiftAuxInt(17, 48, 63, 64), 32, false, 0, 0},
   198  	}
   199  	for i, v := range tests {
   200  		result := mergePPC64ClrlsldiSrw(int64(v.clrlsldi), v.srw)
   201  		if v.valid && result == 0 {
   202  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) did not merge", i)
   203  		} else if !v.valid && result != 0 {
   204  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) should return 0", i)
   205  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   206  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   207  		}
   208  	}
   209  }
   210  
   211  func TestMergePPC64ClrlsldiRlwinm(t *testing.T) {
   212  	tests := []struct {
   213  		clrlsldi int32
   214  		rlwinm   int64
   215  		valid    bool
   216  		rotate   int64
   217  		mask     uint64
   218  	}{
   219  		// ((x<<4)&0xFF00)<<4
   220  		{newPPC64ShiftAuxInt(4, 56, 63, 64), encodePPC64RotateMask(4, 0xFF00, 32), false, 0, 0},
   221  		// ((x>>4)&0xFF)<<4
   222  		{newPPC64ShiftAuxInt(4, 56, 63, 64), encodePPC64RotateMask(28, 0x0FFFFFFF, 32), true, 0, 0xFF0},
   223  		// ((x>>4)&0xFFFF)<<4
   224  		{newPPC64ShiftAuxInt(4, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), true, 0, 0xFFFF0},
   225  		// ((x>>4)&0xFFFF)<<17
   226  		{newPPC64ShiftAuxInt(17, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), false, 0, 0},
   227  		// ((x>>4)&0xFFFF)<<16
   228  		{newPPC64ShiftAuxInt(16, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), true, 12, 0xFFFF0000},
   229  		// ((x>>4)&0xF000FFFF)<<16
   230  		{newPPC64ShiftAuxInt(16, 48, 63, 64), encodePPC64RotateMask(28, 0xF000FFFF, 32), true, 12, 0xFFFF0000},
   231  	}
   232  	for i, v := range tests {
   233  		result := mergePPC64ClrlsldiRlwinm(v.clrlsldi, v.rlwinm)
   234  		if v.valid && result == 0 {
   235  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) did not merge", i)
   236  		} else if !v.valid && result != 0 {
   237  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) should return 0", i)
   238  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   239  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   240  		}
   241  	}
   242  }
   243  
   244  func TestMergePPC64SldiSrw(t *testing.T) {
   245  	tests := []struct {
   246  		sld    int64
   247  		srw    int64
   248  		valid  bool
   249  		rotate int64
   250  		mask   uint64
   251  	}{
   252  		{4, 4, true, 0, 0xFFFFFFF0},
   253  		{4, 8, true, 28, 0x0FFFFFF0},
   254  		{0, 0, true, 0, 0xFFFFFFFF},
   255  		{8, 4, false, 0, 0},
   256  		{0, 32, false, 0, 0},
   257  		{0, 31, true, 1, 0x1},
   258  		{31, 31, true, 0, 0x80000000},
   259  		{32, 32, false, 0, 0},
   260  	}
   261  	for i, v := range tests {
   262  		result := mergePPC64SldiSrw(v.sld, v.srw)
   263  		if v.valid && result == 0 {
   264  			t.Errorf("mergePPC64SldiSrw(Test %d) did not merge", i)
   265  		} else if !v.valid && result != 0 {
   266  			t.Errorf("mergePPC64SldiSrw(Test %d) should return 0", i)
   267  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   268  			t.Errorf("mergePPC64SldiSrw(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   269  		}
   270  	}
   271  }
   272  
   273  func TestMergePPC64AndSrwi(t *testing.T) {
   274  	tests := []struct {
   275  		and    int64
   276  		srw    int64
   277  		valid  bool
   278  		rotate int64
   279  		mask   uint64
   280  	}{
   281  		{0x000000FF, 8, true, 24, 0xFF},
   282  		{0xF00000FF, 8, true, 24, 0xFF},
   283  		{0x0F0000FF, 4, false, 0, 0},
   284  		{0x00000000, 4, false, 0, 0},
   285  		{0xF0000000, 4, false, 0, 0},
   286  		{0xF0000000, 32, false, 0, 0},
   287  		{0xFFFFFFFF, 0, true, 0, 0xFFFFFFFF},
   288  	}
   289  	for i, v := range tests {
   290  		result := mergePPC64AndSrwi(v.and, v.srw)
   291  		if v.valid && result == 0 {
   292  			t.Errorf("mergePPC64AndSrwi(Test %d) did not merge", i)
   293  		} else if !v.valid && result != 0 {
   294  			t.Errorf("mergePPC64AndSrwi(Test %d) should return 0", i)
   295  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   296  			t.Errorf("mergePPC64AndSrwi(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   297  		}
   298  	}
   299  }
   300  
   301  func TestDisjointTypes(t *testing.T) {
   302  	tests := []struct {
   303  		v1, v2   any // two pointers to some types
   304  		expected bool
   305  	}{
   306  		{new(int8), new(int8), false},
   307  		{new(int8), new(float32), false},
   308  		{new(int8), new(*int8), true},
   309  		{new(*int8), new(*float32), false},
   310  		{new(*int8), new(chan<- int8), false},
   311  		{new(**int8), new(*int8), false},
   312  		{new(***int8), new(**int8), false},
   313  		{new(int8), new(chan<- int8), true},
   314  		{new(int), unsafe.Pointer(nil), false},
   315  		{new(byte), new(string), false},
   316  		{new(int), new(string), false},
   317  		{new(*int8), new(struct{ a, b int }), true},
   318  		{new(*int8), new(struct {
   319  			a *int
   320  			b int
   321  		}), false},
   322  		{new(*int8), new(struct {
   323  			a int
   324  			b *int
   325  		}), false}, // with more precise analysis it should be true
   326  		{new(*byte), new(string), false},
   327  		{new(int), new(struct {
   328  			a int
   329  			b *int
   330  		}), false},
   331  		{new(float64), new(complex128), false},
   332  		{new(*byte), new([]byte), false},
   333  		{new(int), new([]byte), false},
   334  		{new(int), new([2]*byte), false}, // with more recise analysis it should be true
   335  		{new([2]int), new(*byte), true},
   336  	}
   337  	for _, tst := range tests {
   338  		t1 := rttype.FromReflect(reflect.TypeOf(tst.v1))
   339  		t2 := rttype.FromReflect(reflect.TypeOf(tst.v2))
   340  		result := disjointTypes(t1, t2)
   341  		if result != tst.expected {
   342  			t.Errorf("disjointTypes(%s, %s) got %t expected %t", t1.String(), t2.String(), result, tst.expected)
   343  		}
   344  	}
   345  }
   346  
   347  //go:noinline
   348  func foo(p1 *int64, p2 *float64) int64 {
   349  	*p1 = 10
   350  	*p2 = 0 // disjointTypes shouldn't consider this and preceding stores as non-aliasing
   351  	return *p1
   352  }
   353  
   354  func TestDisjointTypesRun(t *testing.T) {
   355  	f := float64(0)
   356  	i := (*int64)(unsafe.Pointer(&f))
   357  	r := foo(i, &f)
   358  	if r != 0 {
   359  		t.Errorf("disjointTypes gives an incorrect answer that leads to an incorrect optimization.")
   360  	}
   361  }
   362  

View as plain text