diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -169,7 +169,6 @@ ///< enable code coverage analysis. CODEGENOPT(DumpCoverageMapping , 1, 0) ///< Dump the generated coverage mapping ///< regions. -CODEGENOPT(MisExpect , 1, 0) ///< Validate __builtin_expect with PGO counters /// If -fpcc-struct-return or -freg-struct-return is specified. ENUM_CODEGENOPT(StructReturnConvention, StructReturnConventionKind, 2, SRCK_Default) diff --git a/clang/test/Profile/Inputs/misexpect-switch-default.proftext b/clang/test/Profile/Inputs/misexpect-switch-default.proftext new file mode 100644 --- /dev/null +++ b/clang/test/Profile/Inputs/misexpect-switch-default.proftext @@ -0,0 +1,17 @@ +main +# Func Hash: +8712453512413296413 +# Num Counters: +9 +# Counter Values: +1 +20000 +20000 +4066 +11889 +0 +0 +4045 +0 + + diff --git a/clang/test/Profile/misexpect-branch.c b/clang/test/Profile/misexpect-branch.c --- a/clang/test/Profile/misexpect-branch.c +++ b/clang/test/Profile/misexpect-branch.c @@ -1,7 +1,7 @@ // Test that misexpect detects mis-annotated branches // RUN: llvm-profdata merge %S/Inputs/misexpect-branch.proftext -o %t.profdata -// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) @@ -16,7 +16,7 @@ int bar() { int rando = buzz(); int x = 0; - if (likely(rando % (outer_loop * inner_loop) == 0)) { // expected-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% of profiled executions.}} expected-remark-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% of profiled executions.}} + if (likely(rando % (outer_loop * inner_loop) == 0)) { // expected-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% of profiled executions.}} x = baz(rando); } else { x = foo(50); diff --git a/clang/test/Profile/misexpect-switch-default.c b/clang/test/Profile/misexpect-switch-default.c --- a/clang/test/Profile/misexpect-switch-default.c +++ b/clang/test/Profile/misexpect-switch-default.c @@ -1,7 +1,7 @@ // Test that misexpect detects mis-annotated switch statements for default case -// RUN: llvm-profdata merge %S/Inputs/misexpect-switch.proftext -o %t.profdata -// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect +// RUN: llvm-profdata merge %S/Inputs/misexpect-switch-default.proftext -o %t.profdata +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only int sum(int *buff, int size); int random_sample(int *buff, int size); @@ -17,26 +17,24 @@ int main() { init_arry(); int val = 0; - - int j, k; - for (j = 0; j < outer_loop; ++j) { - for (k = 0; k < inner_loop; ++k) { - unsigned condition = rand() % 5; - switch (__builtin_expect(condition, 6)) { // expected-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% of profiled executions.}} - case 0: - val += sum(arry, arry_size); - break; - case 1: - case 2: - case 3: - case 4: - val += random_sample(arry, arry_size); - break; - default: - __builtin_unreachable(); - } // end switch - } // end inner_loop - } // end outer_loop + int j; + for (j = 0; j < outer_loop * inner_loop; ++j) { + unsigned condition = rand() % 5; + switch (__builtin_expect(condition, 6)) { // expected-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% of profiled executions.}} + case 0: + val += sum(arry, arry_size); + break; + case 1: + case 2: + case 3: + break; + case 4: + val += random_sample(arry, arry_size); + break; + default: + __builtin_unreachable(); + } // end switch + } // end outer_loop return 0; } diff --git a/clang/test/Profile/misexpect-switch-only-default-case.c b/clang/test/Profile/misexpect-switch-only-default-case.c --- a/clang/test/Profile/misexpect-switch-only-default-case.c +++ b/clang/test/Profile/misexpect-switch-only-default-case.c @@ -1,7 +1,7 @@ // Test that misexpect emits no warning when there is only one switch case // RUN: llvm-profdata merge %S/Inputs/misexpect-switch-default-only.proftext -o %t.profdata -// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -fmisexpect +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only // expected-no-diagnostics int sum(int *buff, int size); diff --git a/clang/test/Profile/misexpect-switch.c b/clang/test/Profile/misexpect-switch.c --- a/clang/test/Profile/misexpect-switch.c +++ b/clang/test/Profile/misexpect-switch.c @@ -1,7 +1,7 @@ // Test that misexpect detects mis-annotated switch statements // RUN: llvm-profdata merge %S/Inputs/misexpect-switch.proftext -o %t.profdata -// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only int sum(int *buff, int size); int random_sample(int *buff, int size); diff --git a/llvm/lib/Transforms/Utils/MisExpect.cpp b/llvm/lib/Transforms/Utils/MisExpect.cpp --- a/llvm/lib/Transforms/Utils/MisExpect.cpp +++ b/llvm/lib/Transforms/Utils/MisExpect.cpp @@ -114,7 +114,7 @@ mdconst::dyn_extract(MD->getOperand(i)); RealWeights[i - 1] = Value->getZExtValue(); } - misexpect::verifyMisExpect(&I, RealWeights, I.getParent()->getParent()->getContext()); + misexpect::verifyMisExpect(&I, RealWeights, I.getContext()); } } }