1; Test that instrumentation counter promotion for loops does not fail during
2; compilation for loops that exit to a catchswitch block. In this case, counters
3; do not get promoted out of the loop body.
4
5; RUN: opt < %s -pgo-instr-gen -instrprof -pgo-instrument-entry=false -do-counter-promotion=true -S | FileCheck %s --check-prefixes=CHECK,NOTENTRY
6; RUN: opt < %s -passes=pgo-instr-gen,instrprof -pgo-instrument-entry=false -do-counter-promotion=true -S | FileCheck %s --check-prefixes=CHECK,NOTENTRY
7; RUN: opt < %s -pgo-instr-gen -instrprof -pgo-instrument-entry=true -do-counter-promotion=true -S | FileCheck %s --check-prefixes=CHECK,ENTRY
8; RUN: opt < %s -passes=pgo-instr-gen,instrprof -pgo-instrument-entry=true -do-counter-promotion=true -S | FileCheck %s --check-prefixes=CHECK,ENTRY
9
10; Source used to create test:
11;
12; extern void may_throw(int);
13; char buffer[200];
14; void run(int count) {
15;   try {
16;    for (int i = 0; i < count; ++i) {
17;      if (buffer[i] == 0)
18;        break;
19;      may_throw(i);
20;    }
21;  }
22;  catch (...) {
23;     throw;
24;  }
25;}
26
27%eh.ThrowInfo = type { i32, i32, i32, i32 }
28
29@"?buffer@@3PADA" = dso_local local_unnamed_addr global [200 x i8] zeroinitializer, align 16
30define dso_local void @"?run@@YAXH@Z"(i32 %count) local_unnamed_addr personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
31entry:
32  br label %for.cond
33
34for.cond:                                         ; preds = %for.inc, %entry
35  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
36  %cmp = icmp slt i32 %i.0, %count
37  br i1 %cmp, label %for.body, label %cleanup
38
39for.body:                                         ; preds = %for.cond
40; CHECK: for.body:
41; NOTENTRY: %pgocount1 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 0)
42; TENTRY: %pgocount1 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
43; CHECK: %1 = add i64 %pgocount1, 1
44; NOTENTRY: store i64 %1, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 0)
45; ENTRY: store i64 %1, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
46  %idxprom = zext i32 %i.0 to i64
47  %arrayidx = getelementptr inbounds [200 x i8], [200 x i8]* @"?buffer@@3PADA", i64 0, i64 %idxprom
48  %0 = load i8, i8* %arrayidx, align 1
49  %cmp1 = icmp eq i8 %0, 0
50  br i1 %cmp1, label %cleanup, label %if.end
51
52if.end:                                           ; preds = %for.body
53  invoke void @"?may_throw@@YAXH@Z"(i32 %i.0)
54          to label %for.inc unwind label %catch.dispatch
55
56for.inc:                                          ; preds = %if.end
57; CHECK: for.inc:
58; NOTENTRY: %pgocount2 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
59; ENTRY: %pgocount2 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 2)
60; CHECK: %3 = add i64 %pgocount2, 1
61; NOTENTRY: store i64 %3, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
62; ENTRY: store i64 %3, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 2)
63  %inc = add nuw nsw i32 %i.0, 1
64  br label %for.cond
65
66cleanup:                                          ; preds = %for.body, %for.cond
67  ret void
68
69catch.dispatch:                                   ; preds = %if.end
70  %1 = catchswitch within none [label %catch] unwind to caller
71
72catch:                                            ; preds = %catch.dispatch
73  %2 = catchpad within %1 [i8* null, i32 64, i8* null]
74  call void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #2 [ "funclet"(token %2) ]
75  unreachable
76}
77declare dso_local void @"?may_throw@@YAXH@Z"(i32)
78declare dso_local void @_CxxThrowException(i8*, %eh.ThrowInfo*)
79declare dso_local i32 @__CxxFrameHandler3(...)
80