Source file test/prove_constant_folding.go
1 // errorcheck -0 -d=ssa/prove/debug=2 2 3 //go:build amd64 || arm64 4 5 // Copyright 2022 The Go Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 9 package main 10 11 func f0i(x int) int { 12 if x == 20 { 13 return x // ERROR "Proved.+is constant 20$" 14 } 15 16 if (x + 20) == 20 { 17 return x + 5 // ERROR "Proved.+is constant 0$" "Proved.+is constant 5$" "x\+d >=? w" 18 } 19 20 return x + 1 21 } 22 23 func f0u(x uint) int { 24 if x == 20 { 25 return int(x) // ERROR "Proved.+is constant 20$" 26 } 27 28 if (x + 20) == 20 { 29 return int(x + 5) // ERROR "Proved.+is constant 0$" "Proved.+is constant 5$" "x\+d >=? w" 30 } 31 32 if x < 1000 { 33 return int(x) >> 31 // ERROR "(Proved.+is constant 0|Proved Rsh[0-9]+x[0-9]+ is unsigned)$" 34 } 35 if x := int32(x); x < -1000 { 36 return int(x >> 31) // ERROR "Proved.+is constant -1$" 37 } 38 39 return int(x) + 1 40 } 41 42 // Check that prove is zeroing these right shifts of positive ints by bit-width - 1. 43 // e.g (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) && ft.isNonNegative(n) -> 0 44 func sh64(n int64) int64 { 45 if n < 0 { 46 return n 47 } 48 return n >> 63 // ERROR "(Proved .+ is constant 0|Proved Rsh[0-9]+x[0-9]+ is unsigned)$" 49 } 50 51 func sh32(n int32) int32 { 52 if n < 0 { 53 return n 54 } 55 return n >> 31 // ERROR "(Proved .+ is constant 0|Proved Rsh[0-9]+x[0-9]+ is unsigned)$" 56 } 57 58 func sh32x64(n int32) int32 { 59 if n < 0 { 60 return n 61 } 62 return n >> uint64(31) // ERROR "(Proved .+ is constant 0|Proved Rsh[0-9]+x[0-9]+ is unsigned)$" 63 } 64 65 func sh32x64n(n int32) int32 { 66 if n >= 0 { 67 return 0 68 } 69 return n >> 31 // ERROR "Proved .+ is constant -1$" 70 } 71 72 func sh16(n int16) int16 { 73 if n < 0 { 74 return n 75 } 76 return n >> 15 // ERROR "(Proved .+ is constant 0|Proved Rsh[0-9]+x[0-9]+ is unsigned)$" 77 } 78 79 func sh64noopt(n int64) int64 { 80 return n >> 63 // not optimized; n could be negative 81 } 82