1// Tests for instrumentation of C++ methods, constructors, and destructors.
2
3// RUN: %clang %s -o - -emit-llvm -S -fprofile-instr-generate -fno-exceptions -target %itanium_abi_triple > %tgen
4// RUN: FileCheck --input-file=%tgen -check-prefix=CTRGEN %s
5// RUN: FileCheck --input-file=%tgen -check-prefix=DTRGEN %s
6// RUN: FileCheck --input-file=%tgen -check-prefix=MTHGEN %s
7// RUN: FileCheck --input-file=%tgen -check-prefix=WRPGEN %s
8
9// RUN: llvm-profdata merge %S/Inputs/cxx-class.proftext -o %t.profdata
10// RUN: %clang %s -o - -emit-llvm -S -fprofile-instr-use=%t.profdata -fno-exceptions -target %itanium_abi_triple > %tuse
11// RUN: FileCheck --input-file=%tuse -check-prefix=CTRUSE %s
12// RUN: FileCheck --input-file=%tuse -check-prefix=DTRUSE %s
13// RUN: FileCheck --input-file=%tuse -check-prefix=MTHUSE %s
14// RUN: FileCheck --input-file=%tuse -check-prefix=WRPUSE %s
15
16class Simple {
17  int Member;
18public:
19  // CTRGEN-LABEL: define {{.*}} @_ZN6SimpleC2Ei(
20  // CTRUSE-LABEL: define {{.*}} @_ZN6SimpleC2Ei(
21  // CTRGEN: store {{.*}} @[[SCC:__llvm_profile_counters__ZN6SimpleC2Ei]], i64 0, i64 0
22  explicit Simple(int Member) : Member(Member) {
23    // CTRGEN: store {{.*}} @[[SCC]], i64 0, i64 1
24    // CTRUSE: br {{.*}} !prof ![[SC1:[0-9]+]]
25    if (Member) {}
26    // CTRGEN-NOT: store {{.*}} @[[SCC]],
27    // CTRUSE-NOT: br {{.*}} !prof ![0-9]+
28    // CTRUSE: ret
29  }
30  // CTRUSE: ![[SC1]] = metadata !{metadata !"branch_weights", i32 100, i32 2}
31
32  // DTRGEN-LABEL: define {{.*}} @_ZN6SimpleD2Ev(
33  // DTRUSE-LABEL: define {{.*}} @_ZN6SimpleD2Ev(
34  // DTRGEN: store {{.*}} @[[SDC:__llvm_profile_counters__ZN6SimpleD2Ev]], i64 0, i64 0
35  ~Simple() {
36    // DTRGEN: store {{.*}} @[[SDC]], i64 0, i64 1
37    // DTRUSE: br {{.*}} !prof ![[SD1:[0-9]+]]
38    if (Member) {}
39    // DTRGEN-NOT: store {{.*}} @[[SDC]],
40    // DTRUSE-NOT: br {{.*}} !prof ![0-9]+
41    // DTRUSE: ret
42  }
43  // DTRUSE: ![[SD1]] = metadata !{metadata !"branch_weights", i32 100, i32 2}
44
45  // MTHGEN-LABEL: define {{.*}} @_ZN6Simple6methodEv(
46  // MTHUSE-LABEL: define {{.*}} @_ZN6Simple6methodEv(
47  // MTHGEN: store {{.*}} @[[SMC:__llvm_profile_counters__ZN6Simple6methodEv]], i64 0, i64 0
48  void method() {
49    // MTHGEN: store {{.*}} @[[SMC]], i64 0, i64 1
50    // MTHUSE: br {{.*}} !prof ![[SM1:[0-9]+]]
51    if (Member) {}
52    // MTHGEN-NOT: store {{.*}} @[[SMC]],
53    // MTHUSE-NOT: br {{.*}} !prof ![0-9]+
54    // MTHUSE: ret
55  }
56  // MTHUSE: ![[SM1]] = metadata !{metadata !"branch_weights", i32 100, i32 2}
57};
58
59// WRPGEN-LABEL: define {{.*}} @_Z14simple_wrapperv(
60// WRPUSE-LABEL: define {{.*}} @_Z14simple_wrapperv(
61// WRPGEN: store {{.*}} @[[SWC:__llvm_profile_counters__Z14simple_wrapperv]], i64 0, i64 0
62void simple_wrapper() {
63  // WRPGEN: store {{.*}} @[[SWC]], i64 0, i64 1
64  // WRPUSE: br {{.*}} !prof ![[SW1:[0-9]+]]
65  for (int I = 0; I < 100; ++I) {
66    Simple S(I);
67    S.method();
68  }
69  // WRPGEN-NOT: store {{.*}} @[[SWC]],
70  // WRPUSE-NOT: br {{.*}} !prof ![0-9]+
71  // WRPUSE: ret
72}
73// WRPUSE: ![[SW1]] = metadata !{metadata !"branch_weights", i32 101, i32 2}
74
75int main(int argc, const char *argv[]) {
76  simple_wrapper();
77  return 0;
78}
79