Clang Project

clang_source_code/test/Profile/cxx-throws.cpp
1// Test instrumentation of C++ exception handling constructs.
2
3// FIXME: Don't seek bb labels, like "if.else"
4// REQUIRES: asserts
5
6// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument=clang -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOGEN %s
7// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument=clang -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOGEN-EXC %s
8
9// RUN: llvm-profdata merge %S/Inputs/cxx-throws.proftext -o %t.profdata
10// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOUSE %s
11// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOUSE-EXC %s
12
13// PGOGEN: @[[THC:__profc__Z6throwsv]] = {{(private|internal)}} global [9 x i64] zeroinitializer
14// PGOGEN-EXC: @[[THC:__profc__Z6throwsv]] = {{(private|internal)}} global [9 x i64] zeroinitializer
15// PGOGEN: @[[UNC:__profc__Z11unreachablei]] = {{(private|internal)}} global [3 x i64] zeroinitializer
16
17// PGOGEN-LABEL: @_Z6throwsv()
18// PGOUSE-LABEL: @_Z6throwsv()
19// PGOGEN: store {{.*}} @[[THC]], i64 0, i64 0
20void throws() {
21  // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 1
22  // PGOUSE: br {{.*}} !prof ![[TH1:[0-9]+]]
23  for (int i = 0; i < 100; ++i) {
24    try {
25      // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 3
26      // PGOUSE: br {{.*}} !prof ![[TH2:[0-9]+]]
27      if (i % 3) {
28        // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 4
29        // PGOUSE: br {{.*}} !prof ![[TH3:[0-9]+]]
30        if (i < 50)
31          throw 1;
32      } else {
33        // The catch block may be emitted after the throw above, we can skip it
34        // by looking for an else block, but this will break if anyone puts an
35        // else in the catch
36        // PGOUSE: if.else{{.*}}:
37        // PGOGEN: if.else{{.*}}:
38
39        // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 5
40        // PGOUSE: br {{.*}} !prof ![[TH4:[0-9]+]]
41        if (i >= 50)
42          throw 0;
43      }
44    } catch (int e) {
45      // PGOUSE-EXC: catch{{.*}}:
46      // PGOGEN-EXC: catch{{.*}}:
47
48      // PGOGEN-EXC: store {{.*}} @[[THC]], i64 0, i64 6
49      // PGOGEN-EXC: store {{.*}} @[[THC]], i64 0, i64 7
50      // PGOUSE-EXC: br {{.*}} !prof ![[TH5:[0-9]+]]
51      if (e) {}
52    }
53    // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 2
54
55    // PGOGEN: store {{.*}} @[[THC]], i64 0, i64 8
56    // PGOUSE: br {{.*}} !prof ![[TH6:[0-9]+]]
57    if (i < 100) {}
58  }
59
60  // PGOUSE-NOT: br {{.*}} !prof ![0-9]+
61  // PGOUSE: ret void
62}
63
64// PGOGEN-LABEL: @_Z11unreachablei(i32
65// PGOUSE-LABEL: @_Z11unreachablei(i32
66// PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 0
67void unreachable(int i) {
68  // PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 1
69  // PGOUSE: br {{.*}} !prof ![[UN1:[0-9]+]]
70  if (i)
71    throw i;
72
73  // PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 2
74  // Since we never reach here, the weights should all be zero (and skipped)
75  // PGOUSE-NOT: br {{.*}} !prof !{{.*}}
76  if (i) {}
77}
78
79// PGOUSE-DAG: ![[TH1]] = !{!"branch_weights", i32 101, i32 2}
80// PGOUSE-DAG: ![[TH2]] = !{!"branch_weights", i32 67, i32 35}
81// PGOUSE-DAG: ![[TH3]] = !{!"branch_weights", i32 34, i32 34}
82// PGOUSE-DAG: ![[TH4]] = !{!"branch_weights", i32 18, i32 18}
83// PGOUSE-EXC: ![[TH5]] = !{!"branch_weights", i32 34, i32 18}
84// PGOUSE-DAG: ![[TH6]] = !{!"branch_weights", i32 101, i32 1}
85// PGOUSE-DAG: ![[UN1]] = !{!"branch_weights", i32 2, i32 1}
86
87int main(int argc, const char *argv[]) {
88  throws();
89  try {
90    unreachable(1);
91  } catch (int) {}
92  return 0;
93}
94