1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -indvars < %s | FileCheck %s
3
4; This tests the case where a terminator can be modeled by SCEV,
5; because it has a returned attribute.
6
7target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
8
9declare i32 @foo(i32)
10
11define void @test(i8* %p) personality i8* undef {
12; CHECK-LABEL: @test(
13; CHECK-NEXT:  entry:
14; CHECK-NEXT:    br label [[LOOP:%.*]]
15; CHECK:       loop:
16; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
17; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[LOOP_LATCH]] ]
18; CHECK-NEXT:    [[RES:%.*]] = invoke i32 @foo(i32 returned [[IV]])
19; CHECK-NEXT:    to label [[LOOP_LATCH]] unwind label [[EXIT:%.*]]
20; CHECK:       loop.latch:
21; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add i64 [[INDVARS_IV]], 1
22; CHECK-NEXT:    [[IV_NEXT]] = add nuw i32 [[IV]], 1
23; CHECK-NEXT:    [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
24; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @foo(i32 [[TMP0]])
25; CHECK-NEXT:    br label [[LOOP]]
26; CHECK:       exit:
27; CHECK-NEXT:    [[LP:%.*]] = landingpad { i8*, i32 }
28; CHECK-NEXT:    cleanup
29; CHECK-NEXT:    ret void
30;
31entry:
32  br label %loop
33
34loop:
35  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
36  %res = invoke i32 @foo(i32 returned %iv)
37  to label %loop.latch unwind label %exit
38
39loop.latch:
40  %ext = zext i32 %iv to i64
41  %tmp5 = getelementptr inbounds i8, i8* %p, i64 %ext
42  %iv.next = add nuw i32 %iv, 1
43  call i32 @foo(i32 %res)
44  br label %loop
45
46exit:
47  %lp = landingpad { i8*, i32 }
48  cleanup
49  ret void
50}
51
52define void @test_critedge(i1 %c, i8* %p) personality i8* undef {
53; CHECK-LABEL: @test_critedge(
54; CHECK-NEXT:  entry:
55; CHECK-NEXT:    br label [[LOOP:%.*]]
56; CHECK:       loop:
57; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
58; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[LOOP_LATCH]] ]
59; CHECK-NEXT:    br i1 [[C:%.*]], label [[LOOP_INVOKE:%.*]], label [[LOOP_OTHER:%.*]]
60; CHECK:       loop.invoke:
61; CHECK-NEXT:    [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
62; CHECK-NEXT:    [[RES:%.*]] = invoke i32 @foo(i32 returned [[IV]])
63; CHECK-NEXT:    to label [[LOOP_LATCH]] unwind label [[EXIT:%.*]]
64; CHECK:       loop.other:
65; CHECK-NEXT:    br label [[LOOP_LATCH]]
66; CHECK:       loop.latch:
67; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ [[TMP0]], [[LOOP_INVOKE]] ], [ 0, [[LOOP_OTHER]] ]
68; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add i64 [[INDVARS_IV]], 1
69; CHECK-NEXT:    [[IV_NEXT]] = add nuw i32 [[IV]], 1
70; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @foo(i32 [[PHI]])
71; CHECK-NEXT:    br label [[LOOP]]
72; CHECK:       exit:
73; CHECK-NEXT:    [[LP:%.*]] = landingpad { i8*, i32 }
74; CHECK-NEXT:    cleanup
75; CHECK-NEXT:    ret void
76;
77entry:
78  br label %loop
79
80loop:
81  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
82  br i1 %c, label %loop.invoke, label %loop.other
83
84loop.invoke:
85  %res = invoke i32 @foo(i32 returned %iv)
86  to label %loop.latch unwind label %exit
87
88loop.other:
89  br label %loop.latch
90
91loop.latch:
92  %phi = phi i32 [ %res, %loop.invoke ], [ 0, %loop.other ]
93  %ext = zext i32 %iv to i64
94  %tmp5 = getelementptr inbounds i8, i8* %p, i64 %ext
95  %iv.next = add nuw i32 %iv, 1
96  call i32 @foo(i32 %phi)
97  br label %loop
98
99exit:
100  %lp = landingpad { i8*, i32 }
101  cleanup
102  ret void
103}
104