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