blob: d56a1c2a7d3cbee62676216f92229bc5609c6ed4 [file] [log] [blame]
Chijun Sima8b5de482018-08-04 08:13:47 +00001; RUN: opt < %s -tailcallelim -verify-dom-info -S | FileCheck %s
2; RUN: opt < %s -passes=tailcallelim -verify-dom-info -S | FileCheck %s
Chris Lattner3c31f8c2003-12-08 23:16:25 +00003
Chris Lattnerfbcd1652010-08-31 18:05:08 +00004define i32 @test1_factorial(i32 %x) {
Chris Lattner3c31f8c2003-12-08 23:16:25 +00005entry:
Tanya Lattner5f4b3552008-03-10 07:21:50 +00006 %tmp.1 = icmp sgt i32 %x, 0 ; <i1> [#uses=1]
7 br i1 %tmp.1, label %then, label %else
8then: ; preds = %entry
9 %tmp.6 = add i32 %x, -1 ; <i32> [#uses=1]
Chris Lattnerfbcd1652010-08-31 18:05:08 +000010 %tmp.4 = call i32 @test1_factorial( i32 %tmp.6 ) ; <i32> [#uses=1]
Tanya Lattner5f4b3552008-03-10 07:21:50 +000011 %tmp.7 = mul i32 %tmp.4, %x ; <i32> [#uses=1]
12 ret i32 %tmp.7
13else: ; preds = %entry
14 ret i32 1
Chris Lattner3c31f8c2003-12-08 23:16:25 +000015}
16
Stephen Lina76289a2013-07-14 01:50:49 +000017; CHECK-LABEL: define i32 @test1_factorial(
Chris Lattnerfbcd1652010-08-31 18:05:08 +000018; CHECK: phi i32
19; CHECK-NOT: call i32
20; CHECK: else:
21
22; This is a more aggressive form of accumulator recursion insertion, which
23; requires noticing that X doesn't change as we perform the tailcall.
24
25define i32 @test2_mul(i32 %x, i32 %y) {
26entry:
27 %tmp.1 = icmp eq i32 %y, 0 ; <i1> [#uses=1]
28 br i1 %tmp.1, label %return, label %endif
29endif: ; preds = %entry
30 %tmp.8 = add i32 %y, -1 ; <i32> [#uses=1]
31 %tmp.5 = call i32 @test2_mul( i32 %x, i32 %tmp.8 ) ; <i32> [#uses=1]
32 %tmp.9 = add i32 %tmp.5, %x ; <i32> [#uses=1]
33 ret i32 %tmp.9
34return: ; preds = %entry
35 ret i32 %x
36}
37
Stephen Lina76289a2013-07-14 01:50:49 +000038; CHECK-LABEL: define i32 @test2_mul(
Chris Lattnerfbcd1652010-08-31 18:05:08 +000039; CHECK: phi i32
40; CHECK-NOT: call i32
Chris Lattnere2295f12010-08-31 18:44:03 +000041; CHECK: return:
42
43
44define i64 @test3_fib(i64 %n) nounwind readnone {
Stephen Linc1c7a132013-07-14 01:42:54 +000045; CHECK-LABEL: @test3_fib(
Chris Lattnere2295f12010-08-31 18:44:03 +000046entry:
47; CHECK: tailrecurse:
48; CHECK: %accumulator.tr = phi i64 [ %n, %entry ], [ %3, %bb1 ]
49; CHECK: %n.tr = phi i64 [ %n, %entry ], [ %2, %bb1 ]
50 switch i64 %n, label %bb1 [
51; CHECK: switch i64 %n.tr, label %bb1 [
52 i64 0, label %bb2
53 i64 1, label %bb2
54 ]
55
56bb1:
57; CHECK: bb1:
58 %0 = add i64 %n, -1
59; CHECK: %0 = add i64 %n.tr, -1
60 %1 = tail call i64 @test3_fib(i64 %0) nounwind
61; CHECK: %1 = tail call i64 @test3_fib(i64 %0)
62 %2 = add i64 %n, -2
63; CHECK: %2 = add i64 %n.tr, -2
64 %3 = tail call i64 @test3_fib(i64 %2) nounwind
65; CHECK-NOT: tail call i64 @test3_fib
66 %4 = add nsw i64 %3, %1
67; CHECK: add nsw i64 %accumulator.tr, %1
68 ret i64 %4
69; CHECK: br label %tailrecurse
70
71bb2:
72; CHECK: bb2:
73 ret i64 %n
74; CHECK: ret i64 %accumulator.tr
75}