github.com/letsencrypt/go@v0.0.0-20160714163537-4054769a31f6/test/opt_branchlikely.go (about)

     1  // +build amd64
     2  // errorcheck -0 -d=ssa/likelyadjust/debug=1
     3  
     4  // Copyright 2016 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // Test that branches have some prediction properties.
     9  package foo
    10  
    11  func f(x, y, z int) int {
    12  	a := 0
    13  	for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    14  		for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    15  			a += j
    16  		}
    17  		for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    18  			a -= x + y + z
    19  		}
    20  	}
    21  	return a
    22  }
    23  
    24  func g(x, y, z int) int {
    25  	a := 0
    26  	if y == 0 { // ERROR "Branch prediction rule default < call"
    27  		y = g(y, z, x)
    28  	} else {
    29  		y++
    30  	}
    31  	if y == x { // ERROR "Branch prediction rule default < call"
    32  		y = g(y, z, x)
    33  	} else {
    34  	}
    35  	if y == 2 { // ERROR "Branch prediction rule default < call"
    36  		z++
    37  	} else {
    38  		y = g(z, x, y)
    39  	}
    40  	if y+z == 3 { // ERROR "Branch prediction rule call < exit"
    41  		println("ha ha")
    42  	} else {
    43  		panic("help help help")
    44  	}
    45  	if x != 0 { // ERROR "Branch prediction rule default < ret"
    46  		for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    47  			if x == 4 { // ERROR "Branch prediction rule stay in loop"
    48  				return a
    49  			}
    50  			for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    51  				for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    52  					a -= j * i
    53  				}
    54  				a += j
    55  			}
    56  		}
    57  	}
    58  	return a
    59  }
    60  
    61  func h(x, y, z int) int {
    62  	a := 0
    63  	for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    64  		for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    65  			a += j
    66  			if i == j { // ERROR "Branch prediction rule stay in loop"
    67  				break
    68  			}
    69  			a *= j
    70  		}
    71  		for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    72  			a -= k
    73  			if i == k {
    74  				continue
    75  			}
    76  			a *= k
    77  		}
    78  	}
    79  	if a > 0 { // ERROR "Branch prediction rule default < call"
    80  		a = g(x, y, z)
    81  	} else {
    82  		a = -a
    83  	}
    84  	return a
    85  }