1; RUN: llc < %s -mtriple=armv6-apple-ios5.0 -mattr=+vfp2 -arm-atomic-cfg-tidy=0 | FileCheck %s -check-prefix=CHECKV6
2; RUN: llc < %s -mtriple=thumbv7-apple-ios5.0 -arm-atomic-cfg-tidy=0 | FileCheck %s -check-prefix=CHECKT2D
3; RUN: llc < %s -mtriple=armv6-linux-gnueabi -relocation-model=pic -mattr=+vfp2 -arm-atomic-cfg-tidy=0 \
4; RUN:    | FileCheck %s -check-prefix=CHECKELF
5
6; Enable tailcall optimization for iOS 5.0
7; rdar://9120031
8
9@t = weak global i32 ()* null           ; <i32 ()**> [#uses=1]
10
11declare void @g(i32, i32, i32, i32)
12
13define void @t1() {
14; CHECKELF-LABEL: t1:
15; CHECKELF: bl g(PLT)
16        call void @g( i32 1, i32 2, i32 3, i32 4 )
17        ret void
18}
19
20define void @t2() {
21; CHECKV6-LABEL: t2:
22; CHECKV6: bx r0
23; CHECKT2D-LABEL: t2:
24; CHECKT2D: ldr
25; CHECKT2D-NEXT: ldr
26; CHECKT2D-NEXT: bx r0
27        %tmp = load i32 ()** @t         ; <i32 ()*> [#uses=1]
28        %tmp.upgrd.2 = tail call i32 %tmp( )            ; <i32> [#uses=0]
29        ret void
30}
31
32define void @t3() {
33; CHECKV6-LABEL: t3:
34; CHECKV6: b _t2
35; CHECKELF-LABEL: t3:
36; CHECKELF: b t2(PLT)
37; CHECKT2D-LABEL: t3:
38; CHECKT2D: b.w _t2
39
40        tail call void @t2( )            ; <i32> [#uses=0]
41        ret void
42}
43
44; Sibcall optimization of expanded libcalls. rdar://8707777
45define double @t4(double %a) nounwind readonly ssp {
46entry:
47; CHECKV6-LABEL: t4:
48; CHECKV6: b _sin
49; CHECKELF-LABEL: t4:
50; CHECKELF: b sin(PLT)
51  %0 = tail call double @sin(double %a) nounwind readonly ; <double> [#uses=1]
52  ret double %0
53}
54
55define float @t5(float %a) nounwind readonly ssp {
56entry:
57; CHECKV6-LABEL: t5:
58; CHECKV6: b _sinf
59; CHECKELF-LABEL: t5:
60; CHECKELF: b sinf(PLT)
61  %0 = tail call float @sinf(float %a) nounwind readonly ; <float> [#uses=1]
62  ret float %0
63}
64
65declare float @sinf(float) nounwind readonly
66
67declare double @sin(double) nounwind readonly
68
69define i32 @t6(i32 %a, i32 %b) nounwind readnone {
70entry:
71; CHECKV6-LABEL: t6:
72; CHECKV6: b ___divsi3
73; CHECKELF-LABEL: t6:
74; CHECKELF: b __aeabi_idiv(PLT)
75  %0 = sdiv i32 %a, %b
76  ret i32 %0
77}
78
79; Make sure the tail call instruction isn't deleted
80; rdar://8309338
81declare void @foo() nounwind
82
83define void @t7() nounwind {
84entry:
85; CHECKT2D-LABEL: t7:
86; CHECKT2D: blxeq _foo
87; CHECKT2D-NEXT: pop.w
88; CHECKT2D-NEXT: b.w _foo
89  br i1 undef, label %bb, label %bb1.lr.ph
90
91bb1.lr.ph:
92  tail call void @foo() nounwind
93  unreachable
94
95bb:
96  tail call void @foo() nounwind
97  ret void
98}
99
100; Make sure codegenprep is duplicating ret instructions to enable tail calls.
101; rdar://11140249
102define i32 @t8(i32 %x) nounwind ssp {
103entry:
104; CHECKT2D-LABEL: t8:
105; CHECKT2D-NOT: push
106  %and = and i32 %x, 1
107  %tobool = icmp eq i32 %and, 0
108  br i1 %tobool, label %if.end, label %if.then
109
110if.then:                                          ; preds = %entry
111; CHECKT2D: bne.w _a
112  %call = tail call i32 @a(i32 %x) nounwind
113  br label %return
114
115if.end:                                           ; preds = %entry
116  %and1 = and i32 %x, 2
117  %tobool2 = icmp eq i32 %and1, 0
118  br i1 %tobool2, label %if.end5, label %if.then3
119
120if.then3:                                         ; preds = %if.end
121; CHECKT2D: bne.w _b
122  %call4 = tail call i32 @b(i32 %x) nounwind
123  br label %return
124
125if.end5:                                          ; preds = %if.end
126; CHECKT2D: b.w _c
127  %call6 = tail call i32 @c(i32 %x) nounwind
128  br label %return
129
130return:                                           ; preds = %if.end5, %if.then3, %if.then
131  %retval.0 = phi i32 [ %call, %if.then ], [ %call4, %if.then3 ], [ %call6, %if.end5 ]
132  ret i32 %retval.0
133}
134
135declare i32 @a(i32)
136
137declare i32 @b(i32)
138
139declare i32 @c(i32)
140
141; PR12419
142; rdar://11195178
143; Use the correct input chain for the tailcall node or else the call to
144; _ZN9MutexLockD1Ev would be lost.
145%class.MutexLock = type { i8 }
146
147@x = external global i32, align 4
148
149define i32 @t9() nounwind {
150; CHECKT2D-LABEL: t9:
151; CHECKT2D: blx __ZN9MutexLockC1Ev
152; CHECKT2D: blx __ZN9MutexLockD1Ev
153; CHECKT2D: b.w ___divsi3
154  %lock = alloca %class.MutexLock, align 1
155  %1 = call %class.MutexLock* @_ZN9MutexLockC1Ev(%class.MutexLock* %lock)
156  %2 = load i32* @x, align 4
157  %3 = sdiv i32 1000, %2
158  %4 = call %class.MutexLock* @_ZN9MutexLockD1Ev(%class.MutexLock* %lock)
159  ret i32 %3
160}
161
162declare %class.MutexLock* @_ZN9MutexLockC1Ev(%class.MutexLock*) unnamed_addr nounwind align 2
163
164declare %class.MutexLock* @_ZN9MutexLockD1Ev(%class.MutexLock*) unnamed_addr nounwind align 2
165
166; rdar://13827621
167; Correctly preserve the input chain for the tailcall node in the bitcast case,
168; otherwise the call to floorf is lost.
169define float @libcall_tc_test2(float* nocapture %a, float %b) {
170; CHECKT2D-LABEL: libcall_tc_test2:
171; CHECKT2D: blx _floorf
172; CHECKT2D: b.w _truncf
173  %1 = load float* %a, align 4
174  %call = tail call float @floorf(float %1)
175  store float %call, float* %a, align 4
176  %call1 = tail call float @truncf(float %b)
177  ret float %call1
178}
179
180declare float @floorf(float) readnone
181declare float @truncf(float) readnone
182