Merging r261039:
[oota-llvm.git] / test / CodeGen / X86 / catchpad-regmask.ll
1 ; RUN: llc < %s | FileCheck %s
2
3 ; Based on this code:
4 ;
5 ; extern "C" int array[4];
6 ; extern "C" void global_array(int idx1, int idx2, int idx3) {
7 ;   try {
8 ;     array[idx1] = 111;
9 ;     throw;
10 ;   } catch (...) {
11 ;     array[idx2] = 222;
12 ;   }
13 ;   array[idx3] = 333;
14 ; }
15 ; extern "C" __declspec(dllimport) int imported;
16 ; extern "C" void access_imported() {
17 ;   try {
18 ;     imported = 111;
19 ;     throw;
20 ;   } catch (...) {
21 ;     imported = 222;
22 ;   }
23 ;   imported = 333;
24 ; }
25
26 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
27 target triple = "x86_64-pc-windows-msvc18.0.0"
28
29 %eh.ThrowInfo = type { i32, i32, i32, i32 }
30
31 @array = external global [4 x i32], align 16
32 @imported = external dllimport global i32, align 4
33
34 ; Function Attrs: uwtable
35 define void @global_array(i32 %idx1, i32 %idx2, i32 %idx3) #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
36 entry:
37   %idxprom = sext i32 %idx1 to i64
38   %arrayidx = getelementptr inbounds [4 x i32], [4 x i32]* @array, i64 0, i64 %idxprom
39   store i32 111, i32* %arrayidx, align 4, !tbaa !2
40   invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #1
41           to label %unreachable unwind label %catch.dispatch
42
43 catch.dispatch:                                   ; preds = %entry
44   %cs1 = catchswitch within none [label %catch] unwind to caller
45
46 catch:                                            ; preds = %catch.dispatch
47   %0 = catchpad within %cs1 [i8* null, i32 64, i8* null]
48   %idxprom1 = sext i32 %idx2 to i64
49   %arrayidx2 = getelementptr inbounds [4 x i32], [4 x i32]* @array, i64 0, i64 %idxprom1
50   store i32 222, i32* %arrayidx2, align 4, !tbaa !2
51   catchret from %0 to label %try.cont
52
53 try.cont:                                         ; preds = %catch
54   %idxprom3 = sext i32 %idx3 to i64
55   %arrayidx4 = getelementptr inbounds [4 x i32], [4 x i32]* @array, i64 0, i64 %idxprom3
56   store i32 333, i32* %arrayidx4, align 4, !tbaa !2
57   ret void
58
59 unreachable:                                      ; preds = %entry
60   unreachable
61 }
62
63 ; CHECK-LABEL: global_array: # @global_array
64 ; CHECK: pushq %rbp
65 ;       First array access
66 ; CHECK: movslq  %ecx, %[[idx:[^ ]*]]
67 ; CHECK: leaq    array(%rip), %[[base:[^ ]*]]
68 ; CHECK: movl    $111, (%[[base]],%[[idx]],4)
69 ;       Might throw an exception and return to below...
70 ; CHECK: callq   _CxxThrowException
71 ;       Third array access must remat the address of array
72 ; CHECK: movslq  {{.*}}, %[[idx:[^ ]*]]
73 ; CHECK: leaq    array(%rip), %[[base:[^ ]*]]
74 ; CHECK: movl    $333, (%[[base]],%[[idx]],4)
75 ; CHECK: popq %rbp
76 ; CHECK: retq
77
78 ; CHECK: "?catch$2@?0?global_array@4HA":
79 ; CHECK: pushq   %rbp
80 ; CHECK: movslq  {{.*}}, %[[idx:[^ ]*]]
81 ; CHECK: leaq    array(%rip), %[[base:[^ ]*]]
82 ; CHECK: movl    $222, (%[[base]],%[[idx]],4)
83 ; CHECK: popq    %rbp
84 ; CHECK: retq                            # CATCHRET
85
86 declare void @_CxxThrowException(i8*, %eh.ThrowInfo*)
87
88 declare i32 @__CxxFrameHandler3(...)
89
90 ; Function Attrs: uwtable
91 define void @access_imported() #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
92 entry:
93   store i32 111, i32* @imported, align 4, !tbaa !2
94   invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #1
95           to label %unreachable unwind label %catch.dispatch
96
97 catch.dispatch:                                   ; preds = %entry
98   %cs1 = catchswitch within none [label %catch] unwind to caller
99
100 catch:                                            ; preds = %catch.dispatch
101   %0 = catchpad within %cs1 [i8* null, i32 64, i8* null]
102   store i32 222, i32* @imported, align 4, !tbaa !2
103   catchret from %0 to label %try.cont
104
105 try.cont:                                         ; preds = %catch
106   store i32 333, i32* @imported, align 4, !tbaa !2
107   ret void
108
109 unreachable:                                      ; preds = %entry
110   unreachable
111 }
112
113 ; CHECK-LABEL: access_imported: # @access_imported
114 ; CHECK: pushq %rbp
115 ; CHECK: movq    __imp_imported(%rip), %[[base:[^ ]*]]
116 ; CHECK: movl    $111, (%[[base]])
117 ;       Might throw an exception and return to below...
118 ; CHECK: callq   _CxxThrowException
119 ;       Third access must reload the address of imported
120 ; CHECK: movq    __imp_imported(%rip), %[[base:[^ ]*]]
121 ; CHECK: movl    $333, (%[[base]])
122 ; CHECK: popq %rbp
123 ; CHECK: retq
124
125 ; CHECK: "?catch$2@?0?access_imported@4HA":
126 ; CHECK: pushq   %rbp
127 ; CHECK: movq    __imp_imported(%rip), %[[base:[^ ]*]]
128 ; CHECK: movl    $222, (%[[base]])
129 ; CHECK: popq    %rbp
130 ; CHECK: retq                            # CATCHRET
131
132
133 attributes #0 = { uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
134 attributes #1 = { noreturn }
135
136 !llvm.module.flags = !{!0}
137 !llvm.ident = !{!1}
138
139 !0 = !{i32 1, !"PIC Level", i32 2}
140 !1 = !{!"clang version 3.8.0 "}
141 !2 = !{!3, !3, i64 0}
142 !3 = !{!"int", !4, i64 0}
143 !4 = !{!"omnipotent char", !5, i64 0}
144 !5 = !{!"Simple C/C++ TBAA"}