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