1; RUN: opt < %s -tailcallelim -verify-dom-info -S | FileCheck %s 2 3declare void @noarg() 4declare void @use(i32*) 5declare void @use_nocapture(i32* nocapture) 6declare void @use2_nocapture(i32* nocapture, i32* nocapture) 7 8; Trivial case. Mark @noarg with tail call. 9define void @test0() { 10; CHECK: tail call void @noarg() 11 call void @noarg() 12 ret void 13} 14 15; PR615. Make sure that we do not move the alloca so that it interferes with the tail call. 16define i32 @test1() { 17; CHECK: i32 @test1() 18; CHECK-NEXT: alloca 19 %A = alloca i32 ; <i32*> [#uses=2] 20 store i32 5, i32* %A 21 call void @use(i32* %A) 22; CHECK: tail call i32 @test1 23 %X = tail call i32 @test1() ; <i32> [#uses=1] 24 ret i32 %X 25} 26 27; This function contains intervening instructions which should be moved out of the way 28define i32 @test2(i32 %X) { 29; CHECK: i32 @test2 30; CHECK-NOT: call 31; CHECK: ret i32 32entry: 33 %tmp.1 = icmp eq i32 %X, 0 ; <i1> [#uses=1] 34 br i1 %tmp.1, label %then.0, label %endif.0 35then.0: ; preds = %entry 36 %tmp.4 = add i32 %X, 1 ; <i32> [#uses=1] 37 ret i32 %tmp.4 38endif.0: ; preds = %entry 39 %tmp.10 = add i32 %X, -1 ; <i32> [#uses=1] 40 %tmp.8 = call i32 @test2(i32 %tmp.10) ; <i32> [#uses=1] 41 %DUMMY = add i32 %X, 1 ; <i32> [#uses=0] 42 ret i32 %tmp.8 43} 44 45; Though this case seems to be fairly unlikely to occur in the wild, someone 46; plunked it into the demo script, so maybe they care about it. 47define i32 @test3(i32 %c) { 48; CHECK: i32 @test3 49; CHECK: tailrecurse: 50; CHECK: %ret.tr = phi i32 [ undef, %entry ], [ %current.ret.tr, %else ] 51; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %else ] 52; CHECK: else: 53; CHECK-NOT: call 54; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 0 55; CHECK-NOT: ret 56; CHECK: return: 57; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 0 58; CHECK: ret i32 %current.ret.tr1 59entry: 60 %tmp.1 = icmp eq i32 %c, 0 ; <i1> [#uses=1] 61 br i1 %tmp.1, label %return, label %else 62else: ; preds = %entry 63 %tmp.5 = add i32 %c, -1 ; <i32> [#uses=1] 64 %tmp.3 = call i32 @test3(i32 %tmp.5) ; <i32> [#uses=0] 65 ret i32 0 66return: ; preds = %entry 67 ret i32 0 68} 69 70; Make sure that a nocapture pointer does not stop adding a tail call marker to 71; an unrelated call and additionally that we do not mark the nocapture call with 72; a tail call. 73; 74; rdar://14324281 75define void @test4() { 76; CHECK: void @test4 77; CHECK-NOT: tail call void @use_nocapture 78; CHECK: tail call void @noarg() 79; CHECK: ret void 80 %a = alloca i32 81 call void @use_nocapture(i32* %a) 82 call void @noarg() 83 ret void 84} 85 86; Make sure that we do not perform TRE even with a nocapture use. This is due to 87; bad codegen caused by PR962. 88; 89; rdar://14324281. 90define i32* @test5(i32* nocapture %A, i1 %cond) { 91; CHECK: i32* @test5 92; CHECK-NOT: tailrecurse: 93; CHECK: ret i32* null 94 %B = alloca i32 95 br i1 %cond, label %cond_true, label %cond_false 96cond_true: 97 call i32* @test5(i32* %B, i1 false) 98 ret i32* null 99cond_false: 100 call void @use2_nocapture(i32* %A, i32* %B) 101 call void @noarg() 102 ret i32* null 103} 104 105; PR14143: Make sure that we do not mark functions with nocapture allocas with tail. 106; 107; rdar://14324281. 108define void @test6(i32* %a, i32* %b) { 109; CHECK-LABEL: @test6( 110; CHECK-NOT: tail call 111; CHECK: ret void 112 %c = alloca [100 x i8], align 16 113 %tmp = bitcast [100 x i8]* %c to i32* 114 call void @use2_nocapture(i32* %b, i32* %tmp) 115 ret void 116} 117 118; PR14143: Make sure that we do not mark functions with nocapture allocas with tail. 119; 120; rdar://14324281 121define void @test7(i32* %a, i32* %b) nounwind uwtable { 122entry: 123; CHECK-LABEL: @test7( 124; CHECK-NOT: tail call 125; CHECK: ret void 126 %c = alloca [100 x i8], align 16 127 %0 = bitcast [100 x i8]* %c to i32* 128 call void @use2_nocapture(i32* %0, i32* %a) 129 call void @use2_nocapture(i32* %b, i32* %0) 130 ret void 131} 132 133; If we have a mix of escaping captured/non-captured allocas, ensure that we do 134; not do anything including marking callsites with the tail call marker. 135; 136; rdar://14324281. 137define i32* @test8(i32* nocapture %A, i1 %cond) { 138; CHECK: i32* @test8 139; CHECK-NOT: tailrecurse: 140; CHECK-NOT: tail call 141; CHECK: ret i32* null 142 %B = alloca i32 143 %B2 = alloca i32 144 br i1 %cond, label %cond_true, label %cond_false 145cond_true: 146 call void @use(i32* %B2) 147 call i32* @test8(i32* %B, i1 false) 148 ret i32* null 149cond_false: 150 call void @use2_nocapture(i32* %A, i32* %B) 151 call void @noarg() 152 ret i32* null 153} 154 155; Don't tail call if a byval arg is captured. 156define void @test9(i32* byval(i32) %a) { 157; CHECK-LABEL: define void @test9( 158; CHECK: {{^ *}}call void @use( 159 call void @use(i32* %a) 160 ret void 161} 162 163%struct.X = type { i8* } 164 165declare void @ctor(%struct.X*) 166define void @test10(%struct.X* noalias sret(%struct.X) %agg.result, i1 zeroext %b) { 167; CHECK-LABEL: @test10 168entry: 169 %x = alloca %struct.X, align 8 170 br i1 %b, label %if.then, label %if.end 171 172if.then: ; preds = %entry 173 call void @ctor(%struct.X* %agg.result) 174; CHECK: tail call void @ctor 175 br label %return 176 177if.end: 178 call void @ctor(%struct.X* %x) 179; CHECK: call void @ctor 180 br label %return 181 182return: 183 ret void 184} 185 186declare void @test11_helper1(i8** nocapture, i8*) 187declare void @test11_helper2(i8*) 188define void @test11() { 189; CHECK-LABEL: @test11 190; CHECK-NOT: tail 191 %a = alloca i8* 192 %b = alloca i8 193 call void @test11_helper1(i8** %a, i8* %b) ; a = &b 194 %c = load i8*, i8** %a 195 call void @test11_helper2(i8* %c) 196; CHECK: call void @test11_helper2 197 ret void 198} 199 200; PR25928 201define void @test12() { 202entry: 203; CHECK-LABEL: @test12 204; CHECK: {{^ *}} call void undef(i8* undef) [ "foo"(i8* %e) ] 205 %e = alloca i8 206 call void undef(i8* undef) [ "foo"(i8* %e) ] 207 unreachable 208} 209 210%struct.foo = type { [10 x i32] } 211 212; If an alloca is passed byval it is not a use of the alloca or an escape 213; point, and both calls below can be marked tail. 214define void @test13() { 215; CHECK-LABEL: @test13 216; CHECK: tail call void @bar(%struct.foo* byval(%struct.foo) %f) 217; CHECK: tail call void @bar(%struct.foo* null) 218entry: 219 %f = alloca %struct.foo 220 call void @bar(%struct.foo* byval(%struct.foo) %f) 221 call void @bar(%struct.foo* null) 222 ret void 223} 224 225; A call which passes a byval parameter using byval can be marked tail. 226define void @test14(%struct.foo* byval(%struct.foo) %f) { 227; CHECK-LABEL: @test14 228; CHECK: tail call void @bar 229entry: 230 call void @bar(%struct.foo* byval(%struct.foo) %f) 231 ret void 232} 233 234; If a byval parameter is copied into an alloca and passed byval the call can 235; be marked tail. 236define void @test15(%struct.foo* byval(%struct.foo) %f) { 237; CHECK-LABEL: @test15 238; CHECK: tail call void @bar 239entry: 240 %agg.tmp = alloca %struct.foo 241 %0 = bitcast %struct.foo* %agg.tmp to i8* 242 %1 = bitcast %struct.foo* %f to i8* 243 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 40, i1 false) 244 call void @bar(%struct.foo* byval(%struct.foo) %agg.tmp) 245 ret void 246} 247 248declare void @bar(%struct.foo* byval(%struct.foo)) 249declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1) 250