1; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN
2; RUN: opt < %s -pgo-instr-gen -instrprof -S | FileCheck %s --check-prefix=LOWER
3
4; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN
5; RUN: opt < %s -passes=pgo-instr-gen,instrprof -S | FileCheck %s --check-prefix=LOWER
6
7; This test is to verify that PGO runtime library calls get created with the
8; appropriate operand bundle funclet information when a memory intrinsic
9; being value profiled is called within an exception handler.
10
11; Test case based on this source:
12;  #include <memory.h>
13;
14;  extern void may_throw(int);
15;
16;  #define MSG "0123456789012345\0"
17;  unsigned len = 16;
18;  char msg[200];
19;
20;  void run(int count) {
21;    try {
22;      may_throw(count);
23;    }
24;    catch (...) {
25;      memcpy(msg, MSG, len);
26;      throw;
27;    }
28;  }
29
30%eh.ThrowInfo = type { i32, i32, i32, i32 }
31
32$"??_C@_0BC@CABPINND@Exception?5caught?$AA?$AA@" = comdat any
33
34@"?len@@3IA" = dso_local global i32 16, align 4
35@"?msg@@3PADA" = dso_local global [200 x i8] zeroinitializer, align 16
36@"??_C@_0BC@CABPINND@Exception?5caught?$AA?$AA@" = linkonce_odr dso_local unnamed_addr constant [18 x i8] c"0123456789012345\00\00", comdat, align 1
37
38define dso_local void @"?run@@YAXH@Z"(i32 %count) personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
39entry:
40  invoke void @"?may_throw@@YAXH@Z"(i32 %count)
41          to label %try.cont unwind label %catch.dispatch
42
43catch.dispatch:                                   ; preds = %entry
44  %tmp = catchswitch within none [label %catch] unwind to caller
45
46catch:                                            ; preds = %catch.dispatch
47  %tmp1 = catchpad within %tmp [i8* null, i32 64, i8* null]
48  %tmp2 = load i32, i32* @"?len@@3IA", align 4
49  %conv = zext i32 %tmp2 to i64
50  call void @llvm.memcpy.p0i8.p0i8.i64(
51    i8* getelementptr inbounds ([200 x i8], [200 x i8]* @"?msg@@3PADA", i64 0, i64 0),
52    i8* getelementptr inbounds ([18 x i8], [18 x i8]* @"??_C@_0BC@CABPINND@Exception?5caught?$AA?$AA@", i64 0, i64 0),
53    i64 %conv, i1 false)
54  call void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #3 [ "funclet"(token %tmp1) ]
55  unreachable
56
57try.cont:                                         ; preds = %entry
58  ret void
59}
60
61; GEN: catch:
62; GEN: call void @llvm.instrprof.value.profile(
63; GEN-SAME: [ "funclet"(token %tmp1) ]
64
65; LOWER: catch:
66; LOWER: call void @__llvm_profile_instrument_range(
67; LOWER-SAME: [ "funclet"(token %tmp1) ]
68
69declare dso_local void @"?may_throw@@YAXH@Z"(i32)
70declare dso_local i32 @__CxxFrameHandler3(...)
71
72declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i1)
73declare dso_local void @_CxxThrowException(i8*, %eh.ThrowInfo*)
74