xref: /llvm-project/clang/test/Profile/misexpect-branch.c (revision b49ce9c304b00dae49148b6a2f5f27965000206c)
1 // Test that misexpect detects mis-annotated branches
2 
3 // test diagnostics are issued when profiling data mis-matches annotations
4 // RUN: llvm-profdata merge %S/Inputs/misexpect-branch.proftext -o %t.profdata
5 // RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=imprecise -Wmisexpect
6 // RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=exact -Wmisexpect -debug-info-kind=line-tables-only
7 
8 // there should be no diagnostics when the tolerance is sufficiently high, or when -Wmisexpect is not requested
9 // RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=foo -fdiagnostics-misexpect-tolerance=10 -Wmisexpect -debug-info-kind=line-tables-only
10 // RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=foo
11 
12 // Ensure we emit an error when we don't use pgo with tolerance threshold
13 // RUN: %clang_cc1 %s -O2 -o - -emit-llvm  -fdiagnostics-misexpect-tolerance=10 -Wmisexpect -debug-info-kind=line-tables-only 2>&1 | FileCheck -check-prefix=NO_PGO %s
14 
15 // Test -fdiagnostics-misexpect-tolerance=  requires pgo profile
16 // NO_PGO: '-fdiagnostics-misexpect-tolerance=' requires profile-guided optimization information
17 
18 // foo-no-diagnostics
19 #define likely(x) __builtin_expect(!!(x), 1)
20 #define unlikely(x) __builtin_expect(!!(x), 0)
21 
22 int foo(int);
23 int baz(int);
24 int buzz();
25 
26 const int inner_loop = 100;
27 const int outer_loop = 2000;
28 
bar()29 int bar() { // imprecise-warning-re {{potential performance regression from use of __builtin_expect(): annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions}}
30   int rando = buzz();
31   int x = 0;
32   if (likely(rando % (outer_loop * inner_loop) == 0)) { // exact-warning-re {{potential performance regression from use of __builtin_expect(): annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions}}
33     x = baz(rando);
34   } else {
35     x = foo(50);
36   }
37   return x;
38 }
39 
fizz()40 int fizz() { // imprecise-warning-re {{potential performance regression from use of __builtin_expect(): annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions}}
41   int rando = buzz();
42   int x = 0;
43   if (unlikely(rando % (outer_loop * inner_loop) == 0)) { // exact-warning-re {{potential performance regression from use of __builtin_expect(): annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions}}
44     x = baz(rando);
45   } else {
46     x = foo(50);
47   }
48   return x;
49 }
50