247 lines · plain
1; RUN: opt < %s -passes=tailcallelim -verify-dom-info -S | FileCheck %s2 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 void13}14 15; Make sure that we do not do TRE if pointer to local stack16; escapes through function call.17define i32 @test1() {18; CHECK: i32 @test1()19; CHECK-NEXT: alloca20 %A = alloca i32 ; <ptr> [#uses=2]21 store i32 5, ptr %A22 call void @use(ptr %A)23; CHECK: call i32 @test124 %X = call i32 @test1() ; <i32> [#uses=1]25 ret i32 %X26}27 28; This function contains intervening instructions which should be moved out of the way29define i32 @test2(i32 %X) {30; CHECK: i32 @test231; CHECK-NOT: call32; CHECK: ret i3233entry:34 %tmp.1 = icmp eq i32 %X, 0 ; <i1> [#uses=1]35 br i1 %tmp.1, label %then.0, label %endif.036then.0: ; preds = %entry37 %tmp.4 = add i32 %X, 1 ; <i32> [#uses=1]38 ret i32 %tmp.439endif.0: ; preds = %entry40 %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.844}45 46; Though this case seems to be fairly unlikely to occur in the wild, someone47; plunked it into the demo script, so maybe they care about it.48define i32 @test3(i32 %c) {49; CHECK: i32 @test350; 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: call55; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 056; CHECK-NOT: ret57; CHECK: return:58; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 059; CHECK: ret i32 %current.ret.tr160entry:61 %tmp.1 = icmp eq i32 %c, 0 ; <i1> [#uses=1]62 br i1 %tmp.1, label %return, label %else63else: ; preds = %entry64 %tmp.5 = add i32 %c, -1 ; <i32> [#uses=1]65 %tmp.3 = call i32 @test3(i32 %tmp.5) ; <i32> [#uses=0]66 ret i32 067return: ; preds = %entry68 ret i32 069}70 71; Make sure that a nocapture pointer does not stop adding a tail call marker to72; an unrelated call and additionally that we do not mark the nocapture call with73; a tail call.74;75; rdar://1432428176define void @test4() {77; CHECK: void @test478; CHECK-NOT: tail call void @use_nocapture79; CHECK: tail call void @noarg()80; CHECK: ret void81 %a = alloca i3282 call void @use_nocapture(ptr %a)83 call void @noarg()84 ret void85}86 87; Make sure that we do not perform TRE even with a nocapture use. This is due to88; bad codegen caused by PR962.89;90; rdar://14324281.91define ptr @test5(ptr nocapture %A, i1 %cond) {92; CHECK: ptr @test593; CHECK-NOT: tailrecurse:94; CHECK: ret ptr null95 %B = alloca i3296 br i1 %cond, label %cond_true, label %cond_false97cond_true:98 call ptr @test5(ptr %B, i1 false)99 ret ptr null100cond_false:101 call void @use2_nocapture(ptr %A, ptr %B)102 call void @noarg()103 ret ptr null104}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 call112; CHECK: ret void113 %c = alloca [100 x i8], align 16114 call void @use2_nocapture(ptr %b, ptr %c)115 ret void116}117 118; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.119;120; rdar://14324281121define void @test7(ptr %a, ptr %b) nounwind uwtable {122entry:123; CHECK-LABEL: @test7(124; CHECK-NOT: tail call125; CHECK: ret void126 %c = alloca [100 x i8], align 16127 call void @use2_nocapture(ptr %c, ptr %a)128 call void @use2_nocapture(ptr %b, ptr %c)129 ret void130}131 132; If we have a mix of escaping captured/non-captured allocas, ensure that we do133; 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 @test8138; CHECK-NOT: tailrecurse:139; CHECK-NOT: tail call140; CHECK: ret ptr null141 %B = alloca i32142 %B2 = alloca i32143 br i1 %cond, label %cond_true, label %cond_false144cond_true:145 call void @use(ptr %B2)146 call ptr @test8(ptr %B, i1 false)147 ret ptr null148cond_false:149 call void @use2_nocapture(ptr %A, ptr %B)150 call void @noarg()151 ret ptr null152}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 void160}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: @test10167entry:168 %x = alloca %struct.X, align 8169 br i1 %b, label %if.then, label %if.end170 171if.then: ; preds = %entry172 call void @ctor(ptr %agg.result)173; CHECK: tail call void @ctor174 br label %return175 176if.end:177 call void @ctor(ptr %x)178; CHECK: call void @ctor179 br label %return180 181return:182 ret void183}184 185declare void @test11_helper1(ptr nocapture, ptr)186declare void @test11_helper2(ptr)187define void @test11() {188; CHECK-LABEL: @test11189; CHECK-NOT: tail190 %a = alloca ptr191 %b = alloca i8192 call void @test11_helper1(ptr %a, ptr %b) ; a = &b193 %c = load ptr, ptr %a194 call void @test11_helper2(ptr %c)195; CHECK: call void @test11_helper2196 ret void197}198 199; PR25928200define void @test12() {201entry:202; CHECK-LABEL: @test12203; CHECK: {{^ *}} call void undef(ptr undef) [ "foo"(ptr %e) ]204 %e = alloca i8205 call void undef(ptr undef) [ "foo"(ptr %e) ]206 unreachable207}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 escape212; point, and both calls below can be marked tail.213define void @test13() {214; CHECK-LABEL: @test13215; 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.foo219 call void @bar(ptr byval(%struct.foo) %f)220 call void @bar(ptr byval(%struct.foo) null)221 ret void222}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: @test14227; CHECK: tail call void @bar228entry:229 call void @bar(ptr byval(%struct.foo) %f)230 ret void231}232 233; If a byval parameter is copied into an alloca and passed byval the call can234; be marked tail.235define void @test15(ptr byval(%struct.foo) %f) {236; CHECK-LABEL: @test15237; CHECK: tail call void @bar238entry:239 %agg.tmp = alloca %struct.foo240 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 void243}244 245declare void @bar(ptr byval(%struct.foo))246declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1)247