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 "testing"
     8  
     9  // We generate memmove for copy(x[1:], x[:]), however we may change it to OpMove,
    10  // because size is known. Check that OpMove is alias-safe, or we did call memmove.
    11  func TestMove(t *testing.T) {
    12  	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}
    13  	copy(x[1:], x[:])
    14  	for i := 1; i < len(x); i++ {
    15  		if int(x[i]) != i {
    16  			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)
    17  		}
    18  	}
    19  }
    20  
    21  func TestMoveSmall(t *testing.T) {
    22  	x := [...]byte{1, 2, 3, 4, 5, 6, 7}
    23  	copy(x[1:], x[:])
    24  	for i := 1; i < len(x); i++ {
    25  		if int(x[i]) != i {
    26  			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)
    27  		}
    28  	}
    29  }
    30  
    31  func TestSubFlags(t *testing.T) {
    32  	if !subFlags32(0, 1).lt() {
    33  		t.Errorf("subFlags32(0,1).lt() returned false")
    34  	}
    35  	if !subFlags32(0, 1).ult() {
    36  		t.Errorf("subFlags32(0,1).ult() returned false")
    37  	}
    38  }
    39  
    40  func TestIsPPC64WordRotateMask(t *testing.T) {
    41  	tests := []struct {
    42  		input    int64
    43  		expected bool
    44  	}{
    45  		{0x00000001, true},
    46  		{0x80000001, true},
    47  		{0x80010001, false},
    48  		{0xFFFFFFFA, false},
    49  		{0xF0F0F0F0, false},
    50  		{0xFFFFFFFD, true},
    51  		{0x80000000, true},
    52  		{0x00000000, false},
    53  		{0xFFFFFFFF, true},
    54  		{0x0000FFFF, true},
    55  		{0xFF0000FF, true},
    56  		{0x00FFFF00, true},
    57  	}
    58  
    59  	for _, v := range tests {
    60  		if v.expected != isPPC64WordRotateMask(v.input) {
    61  			t.Errorf("isPPC64WordRotateMask(0x%x) failed", v.input)
    62  		}
    63  	}
    64  }
    65  
    66  func TestEncodeDecodePPC64WordRotateMask(t *testing.T) {
    67  	tests := []struct {
    68  		rotate int64
    69  		mask   uint64
    70  		nbits,
    71  		mb,
    72  		me,
    73  		encoded int64
    74  	}{
    75  		{1, 0x00000001, 32, 31, 31, 0x20011f20},
    76  		{2, 0x80000001, 32, 31, 0, 0x20021f01},
    77  		{3, 0xFFFFFFFD, 32, 31, 29, 0x20031f1e},
    78  		{4, 0x80000000, 32, 0, 0, 0x20040001},
    79  		{5, 0xFFFFFFFF, 32, 0, 31, 0x20050020},
    80  		{6, 0x0000FFFF, 32, 16, 31, 0x20061020},
    81  		{7, 0xFF0000FF, 32, 24, 7, 0x20071808},
    82  		{8, 0x00FFFF00, 32, 8, 23, 0x20080818},
    83  
    84  		{9, 0x0000000000FFFF00, 64, 40, 55, 0x40092838},
    85  		{10, 0xFFFF000000000000, 64, 0, 15, 0x400A0010},
    86  		{10, 0xFFFF000000000001, 64, 63, 15, 0x400A3f10},
    87  	}
    88  
    89  	for i, v := range tests {
    90  		result := encodePPC64RotateMask(v.rotate, int64(v.mask), v.nbits)
    91  		if result != v.encoded {
    92  			t.Errorf("encodePPC64RotateMask(%d,0x%x,%d) = 0x%x, expected 0x%x", v.rotate, v.mask, v.nbits, result, v.encoded)
    93  		}
    94  		rotate, mb, me, mask := DecodePPC64RotateMask(result)
    95  		if rotate != v.rotate || mb != v.mb || me != v.me || mask != v.mask {
    96  			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)
    97  		}
    98  	}
    99  }
   100  
   101  func TestMergePPC64ClrlsldiSrw(t *testing.T) {
   102  	tests := []struct {
   103  		clrlsldi int32
   104  		srw      int64
   105  		valid    bool
   106  		rotate   int64
   107  		mask     uint64
   108  	}{
   109  		// ((x>>4)&0xFF)<<4
   110  		{newPPC64ShiftAuxInt(4, 56, 63, 64), 4, true, 0, 0xFF0},
   111  		// ((x>>4)&0xFFFF)<<4
   112  		{newPPC64ShiftAuxInt(4, 48, 63, 64), 4, true, 0, 0xFFFF0},
   113  		// ((x>>4)&0xFFFF)<<17
   114  		{newPPC64ShiftAuxInt(17, 48, 63, 64), 4, false, 0, 0},
   115  		// ((x>>4)&0xFFFF)<<16
   116  		{newPPC64ShiftAuxInt(16, 48, 63, 64), 4, true, 12, 0xFFFF0000},
   117  		// ((x>>32)&0xFFFF)<<17
   118  		{newPPC64ShiftAuxInt(17, 48, 63, 64), 32, false, 0, 0},
   119  	}
   120  	for i, v := range tests {
   121  		result := mergePPC64ClrlsldiSrw(int64(v.clrlsldi), v.srw)
   122  		if v.valid && result == 0 {
   123  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) did not merge", i)
   124  		} else if !v.valid && result != 0 {
   125  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) should return 0", i)
   126  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   127  			t.Errorf("mergePPC64ClrlsldiSrw(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   128  		}
   129  	}
   130  }
   131  
   132  func TestMergePPC64ClrlsldiRlwinm(t *testing.T) {
   133  	tests := []struct {
   134  		clrlsldi int32
   135  		rlwinm   int64
   136  		valid    bool
   137  		rotate   int64
   138  		mask     uint64
   139  	}{
   140  		// ((x<<4)&0xFF00)<<4
   141  		{newPPC64ShiftAuxInt(4, 56, 63, 64), encodePPC64RotateMask(4, 0xFF00, 32), false, 0, 0},
   142  		// ((x>>4)&0xFF)<<4
   143  		{newPPC64ShiftAuxInt(4, 56, 63, 64), encodePPC64RotateMask(28, 0x0FFFFFFF, 32), true, 0, 0xFF0},
   144  		// ((x>>4)&0xFFFF)<<4
   145  		{newPPC64ShiftAuxInt(4, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), true, 0, 0xFFFF0},
   146  		// ((x>>4)&0xFFFF)<<17
   147  		{newPPC64ShiftAuxInt(17, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), false, 0, 0},
   148  		// ((x>>4)&0xFFFF)<<16
   149  		{newPPC64ShiftAuxInt(16, 48, 63, 64), encodePPC64RotateMask(28, 0xFFFF, 32), true, 12, 0xFFFF0000},
   150  		// ((x>>4)&0xF000FFFF)<<16
   151  		{newPPC64ShiftAuxInt(16, 48, 63, 64), encodePPC64RotateMask(28, 0xF000FFFF, 32), true, 12, 0xFFFF0000},
   152  	}
   153  	for i, v := range tests {
   154  		result := mergePPC64ClrlsldiRlwinm(v.clrlsldi, v.rlwinm)
   155  		if v.valid && result == 0 {
   156  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) did not merge", i)
   157  		} else if !v.valid && result != 0 {
   158  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) should return 0", i)
   159  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   160  			t.Errorf("mergePPC64ClrlsldiRlwinm(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   161  		}
   162  	}
   163  }
   164  
   165  func TestMergePPC64SldiSrw(t *testing.T) {
   166  	tests := []struct {
   167  		sld    int64
   168  		srw    int64
   169  		valid  bool
   170  		rotate int64
   171  		mask   uint64
   172  	}{
   173  		{4, 4, true, 0, 0xFFFFFFF0},
   174  		{4, 8, true, 28, 0x0FFFFFF0},
   175  		{0, 0, true, 0, 0xFFFFFFFF},
   176  		{8, 4, false, 0, 0},
   177  		{0, 32, false, 0, 0},
   178  		{0, 31, true, 1, 0x1},
   179  		{31, 31, true, 0, 0x80000000},
   180  		{32, 32, false, 0, 0},
   181  	}
   182  	for i, v := range tests {
   183  		result := mergePPC64SldiSrw(v.sld, v.srw)
   184  		if v.valid && result == 0 {
   185  			t.Errorf("mergePPC64SldiSrw(Test %d) did not merge", i)
   186  		} else if !v.valid && result != 0 {
   187  			t.Errorf("mergePPC64SldiSrw(Test %d) should return 0", i)
   188  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   189  			t.Errorf("mergePPC64SldiSrw(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   190  		}
   191  	}
   192  }
   193  
   194  func TestMergePPC64AndSrwi(t *testing.T) {
   195  	tests := []struct {
   196  		and    int64
   197  		srw    int64
   198  		valid  bool
   199  		rotate int64
   200  		mask   uint64
   201  	}{
   202  		{0x000000FF, 8, true, 24, 0xFF},
   203  		{0xF00000FF, 8, true, 24, 0xFF},
   204  		{0x0F0000FF, 4, false, 0, 0},
   205  		{0x00000000, 4, false, 0, 0},
   206  		{0xF0000000, 4, false, 0, 0},
   207  		{0xF0000000, 32, false, 0, 0},
   208  		{0xFFFFFFFF, 0, true, 0, 0xFFFFFFFF},
   209  	}
   210  	for i, v := range tests {
   211  		result := mergePPC64AndSrwi(v.and, v.srw)
   212  		if v.valid && result == 0 {
   213  			t.Errorf("mergePPC64AndSrwi(Test %d) did not merge", i)
   214  		} else if !v.valid && result != 0 {
   215  			t.Errorf("mergePPC64AndSrwi(Test %d) should return 0", i)
   216  		} else if r, _, _, m := DecodePPC64RotateMask(result); v.rotate != r || v.mask != m {
   217  			t.Errorf("mergePPC64AndSrwi(Test %d) got (%d,0x%x) expected (%d,0x%x)", i, r, m, v.rotate, v.mask)
   218  		}
   219  	}
   220  }
   221  

View as plain text