Source file test/fixedbugs/issue82825.go

     1  // run
     2  
     3  // Copyright 2026 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  package main
     8  
     9  import "fmt"
    10  
    11  // The unbounded shift lowering emits CMPQconst [width] count feeding the
    12  // count mask (SBBQcarrymask). A comparison of the same count against the
    13  // same width shares those flags; SBBQ writes them, so the SETcc must not
    14  // read them after the mask. Regression test for the missing clobberFlags.
    15  
    16  //go:noinline
    17  func lshEq(x, y uint64) uint64 {
    18  	r := x << y
    19  	eq := uint64(0)
    20  	if y == 64 {
    21  		eq = 1
    22  	}
    23  	return r + eq
    24  }
    25  
    26  //go:noinline
    27  func lshNe(x, y uint64) uint64 {
    28  	r := x << y
    29  	ne := uint64(0)
    30  	if y != 64 {
    31  		ne = 1
    32  	}
    33  	return r + ne
    34  }
    35  
    36  //go:noinline
    37  func rshU(x, y uint64) uint64 {
    38  	r := x >> y
    39  	eq := uint64(0)
    40  	if y == 64 {
    41  		eq = 1
    42  	}
    43  	return r + eq
    44  }
    45  
    46  //go:noinline
    47  func rshS(x int64, y uint64) int64 {
    48  	r := x >> y
    49  	eq := int64(0)
    50  	if y == 64 {
    51  		eq = 1
    52  	}
    53  	return r + eq
    54  }
    55  
    56  //go:noinline
    57  func lsh32(x uint32, y uint64) uint32 {
    58  	r := x << y
    59  	eq := uint32(0)
    60  	if y == 32 {
    61  		eq = 1
    62  	}
    63  	return r + eq
    64  }
    65  
    66  // 386: the decomposed 64-bit comparison shares CMPLconst [1] hi with the
    67  // shift's count saturation (SBBLcarrymask), so SETEQ reads clobbered flags.
    68  //
    69  //go:noinline
    70  func cmpSat(x int16) int16 {
    71  	r := int16(-2047) << uint64(uint64(x))
    72  	if uint64(x) <= 8589934591 { // 0x1FFFFFFFF; false for negative x
    73  		r = int16(x) &^ 3
    74  	}
    75  	return r
    76  }
    77  
    78  // The count comparison may also use <= or >: each of ==, !=, <= and > against the
    79  // width lowers to the same CMP const [width] count as the shift's bounds mask.
    80  
    81  //go:noinline
    82  func rshLe16(x, y int16) int16 {
    83  	r := ^int16(uint16(y)) >> uint64(int16(x))
    84  	b := int16(0)
    85  	if uint64(int16(x)) <= 16 {
    86  		b = 1
    87  	}
    88  	return r + b
    89  }
    90  
    91  //go:noinline
    92  func rshGt64(x int64) int64 {
    93  	r := int64(36028797018963969) >> uint64(int64(x))
    94  	b := int64(0)
    95  	if uint64(int64(x)) > 64 {
    96  		b = 1
    97  	}
    98  	return r + b
    99  }
   100  
   101  func main() {
   102  	if got := lshEq(1, 65); got != 0 {
   103  		fmt.Printf("lshEq(1, 65) = %d, want 0\n", got)
   104  		panic("FAIL")
   105  	}
   106  	if got := lshEq(1, 63); got != 9223372036854775808 {
   107  		fmt.Printf("lshEq(1, 63) = %d, want 9223372036854775808\n", got)
   108  		panic("FAIL")
   109  	}
   110  	if got := lshEq(1, 64); got != 1 {
   111  		fmt.Printf("lshEq(1, 64) = %d, want 1\n", got)
   112  		panic("FAIL")
   113  	}
   114  	if got := lshNe(1, 65); got != 1 {
   115  		fmt.Printf("lshNe(1, 65) = %d, want 1\n", got)
   116  		panic("FAIL")
   117  	}
   118  	if got := rshU(1, 65); got != 0 {
   119  		fmt.Printf("rshU(1, 65) = %d, want 0\n", got)
   120  		panic("FAIL")
   121  	}
   122  	if got := rshS(-1, 65); got != -1 {
   123  		fmt.Printf("rshS(-1, 65) = %d, want -1\n", got)
   124  		panic("FAIL")
   125  	}
   126  	if got := lsh32(1, 33); got != 0 {
   127  		fmt.Printf("lsh32(1, 33) = %d, want 0\n", got)
   128  		panic("FAIL")
   129  	}
   130  	if got := cmpSat(-1); got != 0 {
   131  		fmt.Printf("cmpSat(-1) = %d, want 0\n", got)
   132  		panic("FAIL")
   133  	}
   134  	if got := cmpSat(5); got != 4 {
   135  		fmt.Printf("cmpSat(5) = %d, want 4\n", got)
   136  		panic("FAIL")
   137  	}
   138  	if got := rshLe16(-32768, -32768); got != 0 {
   139  		fmt.Printf("rshLe16(-32768, -32768) = %d, want 0\n", got)
   140  		panic("FAIL")
   141  	}
   142  	if got := rshGt64(-1 << 63); got != 1 {
   143  		fmt.Printf("rshGt64(-1<<63) = %d, want 1\n", got)
   144  		panic("FAIL")
   145  	}
   146  }
   147  

View as plain text