Black Lives Matter. Support the Equal Justice Initiative.

Source file test/opt_branchlikely.go

Documentation: test

     1  // +build amd64
     2  // errorcheck -0 -d=ssa/likelyadjust/debug=1,ssa/insert_resched_checks/off
     3  // rescheduling check insertion is turned off because the inserted conditional branches perturb the errorcheck
     4  
     5  // Copyright 2016 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  // Test that branches have some prediction properties.
    10  package foo
    11  
    12  func f(x, y, z int) int {
    13  	a := 0
    14  	for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    15  		for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    16  			a += j
    17  		}
    18  		for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    19  			a -= x + y + z
    20  		}
    21  	}
    22  	return a
    23  }
    24  
    25  func g(x, y, z int) int {
    26  	a := 0
    27  	if y == 0 { // ERROR "Branch prediction rule default < call"
    28  		y = g(y, z, x)
    29  	} else {
    30  		y++
    31  	}
    32  	if y == x { // ERROR "Branch prediction rule default < call"
    33  		y = g(y, z, x)
    34  	} else {
    35  	}
    36  	if y == 2 { // ERROR "Branch prediction rule default < call"
    37  		z++
    38  	} else {
    39  		y = g(z, x, y)
    40  	}
    41  	if y+z == 3 { // ERROR "Branch prediction rule call < exit"
    42  		println("ha ha")
    43  	} else {
    44  		panic("help help help")
    45  	}
    46  	if x != 0 { // ERROR "Branch prediction rule default < ret"
    47  		for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    48  			if x == 4 { // ERROR "Branch prediction rule stay in loop"
    49  				return a
    50  			}
    51  			for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    52  				for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    53  					a -= j * i
    54  				}
    55  				a += j
    56  			}
    57  		}
    58  	}
    59  	return a
    60  }
    61  
    62  func h(x, y, z int) int {
    63  	a := 0
    64  	for i := 0; i < x; i++ { // ERROR "Branch prediction rule stay in loop"
    65  		for j := 0; j < y; j++ { // ERROR "Branch prediction rule stay in loop"
    66  			a += j
    67  			if i == j { // ERROR "Branch prediction rule stay in loop"
    68  				break
    69  			}
    70  			a *= j
    71  		}
    72  		for k := 0; k < z; k++ { // ERROR "Branch prediction rule stay in loop"
    73  			a -= k
    74  			if i == k {
    75  				continue
    76  			}
    77  			a *= k
    78  		}
    79  	}
    80  	if a > 0 { // ERROR "Branch prediction rule default < call"
    81  		a = g(x, y, z)
    82  	} else {
    83  		a = -a
    84  	}
    85  	return a
    86  }
    87  

View as plain text