1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -S -loop-unroll -unroll-peel-max-count=4 -verify-dom-info | FileCheck %s
3; RUN: opt < %s -S -loop-unroll -unroll-peel-max-count=4 -unroll-allow-loop-nests-peeling -verify-dom-info | FileCheck %s --check-prefix PEELED
4
5declare void @f1()
6declare void @f2()
7
8; In this case we cannot peel the inner loop, because the condition involves
9; the outer induction variable.
10; Peel the loop nest if allowed by the flag -unroll-allow-loop-nests-peeling.
11define void @test1(i32 %k) {
12; CHECK-LABEL: @test1(
13; CHECK-NEXT:  for.body.lr.ph:
14; CHECK-NEXT:    br label [[OUTER_HEADER:%.*]]
15; CHECK:       outer.header:
16; CHECK-NEXT:    [[J:%.*]] = phi i32 [ 0, [[FOR_BODY_LR_PH:%.*]] ], [ [[J_INC:%.*]], [[OUTER_INC:%.*]] ]
17; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
18; CHECK:       for.body:
19; CHECK-NEXT:    [[I_05:%.*]] = phi i32 [ 0, [[OUTER_HEADER]] ], [ [[INC:%.*]], [[FOR_INC:%.*]] ]
20; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult i32 [[J]], 2
21; CHECK-NEXT:    br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
22; CHECK:       if.then:
23; CHECK-NEXT:    call void @f1()
24; CHECK-NEXT:    br label [[FOR_INC]]
25; CHECK:       if.else:
26; CHECK-NEXT:    call void @f2()
27; CHECK-NEXT:    br label [[FOR_INC]]
28; CHECK:       for.inc:
29; CHECK-NEXT:    [[INC]] = add nsw i32 [[I_05]], 1
30; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[INC]], [[K:%.*]]
31; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[OUTER_INC]]
32; CHECK:       outer.inc:
33; CHECK-NEXT:    [[J_INC]] = add nsw i32 [[J]], 1
34; CHECK-NEXT:    [[OUTER_CMP:%.*]] = icmp slt i32 [[J_INC]], [[K]]
35; CHECK-NEXT:    br i1 [[OUTER_CMP]], label [[OUTER_HEADER]], label [[FOR_END:%.*]], !llvm.loop !{{.*}}
36; CHECK:       for.end:
37; CHECK-NEXT:    ret void
38;
39; PEELED-LABEL: @test1(
40; PEELED-NEXT:  for.body.lr.ph:
41; PEELED-NEXT:    br label [[OUTER_HEADER_PEEL_BEGIN:%.*]]
42; PEELED:       outer.header.peel.begin:
43; PEELED-NEXT:    br label [[OUTER_HEADER_PEEL:%.*]]
44; PEELED:       outer.header.peel:
45; PEELED-NEXT:    br label [[FOR_BODY_PEEL:%.*]]
46; PEELED:       for.body.peel:
47; PEELED-NEXT:    [[I_05_PEEL:%.*]] = phi i32 [ 0, [[OUTER_HEADER_PEEL]] ], [ [[INC_PEEL:%.*]], [[FOR_INC_PEEL:%.*]] ]
48; PEELED-NEXT:    [[CMP1_PEEL:%.*]] = icmp ult i32 0, 2
49; PEELED-NEXT:    br i1 [[CMP1_PEEL]], label [[IF_THEN_PEEL:%.*]], label [[IF_ELSE_PEEL:%.*]]
50; PEELED:       if.else.peel:
51; PEELED-NEXT:    call void @f2()
52; PEELED-NEXT:    br label [[FOR_INC_PEEL]]
53; PEELED:       if.then.peel:
54; PEELED-NEXT:    call void @f1()
55; PEELED-NEXT:    br label [[FOR_INC_PEEL]]
56; PEELED:       for.inc.peel:
57; PEELED-NEXT:    [[INC_PEEL]] = add nsw i32 [[I_05_PEEL]], 1
58; PEELED-NEXT:    [[CMP_PEEL:%.*]] = icmp slt i32 [[INC_PEEL]], [[K:%.*]]
59; PEELED-NEXT:    br i1 [[CMP_PEEL]], label [[FOR_BODY_PEEL]], label [[OUTER_INC_PEEL:%.*]]
60; PEELED:       outer.inc.peel:
61; PEELED-NEXT:    [[J_INC_PEEL:%.*]] = add nsw i32 0, 1
62; PEELED-NEXT:    [[OUTER_CMP_PEEL:%.*]] = icmp slt i32 [[J_INC_PEEL]], [[K]]
63; PEELED-NEXT:    br i1 [[OUTER_CMP_PEEL]], label [[OUTER_HEADER_PEEL_NEXT:%.*]], label [[FOR_END:%[^,]*]]
64; Verify that MD_loop metadata is dropped.
65; PEELED-NOT:   , !llvm.loop !{{[0-9]*}}
66; PEELED:       outer.header.peel.next:
67; PEELED-NEXT:    br label [[OUTER_HEADER_PEEL2:%.*]]
68; PEELED:       outer.header.peel2:
69; PEELED-NEXT:    br label [[FOR_BODY_PEEL3:%.*]]
70; PEELED:       for.body.peel3:
71; PEELED-NEXT:    [[I_05_PEEL4:%.*]] = phi i32 [ 0, [[OUTER_HEADER_PEEL2]] ], [ [[INC_PEEL9:%.*]], [[FOR_INC_PEEL8:%.*]] ]
72; PEELED-NEXT:    [[CMP1_PEEL5:%.*]] = icmp ult i32 [[J_INC_PEEL]], 2
73; PEELED-NEXT:    br i1 [[CMP1_PEEL5]], label [[IF_THEN_PEEL7:%.*]], label [[IF_ELSE_PEEL6:%.*]]
74; PEELED:       if.else.peel6:
75; PEELED-NEXT:    call void @f2()
76; PEELED-NEXT:    br label [[FOR_INC_PEEL8]]
77; PEELED:       if.then.peel7:
78; PEELED-NEXT:    call void @f1()
79; PEELED-NEXT:    br label [[FOR_INC_PEEL8]]
80; PEELED:       for.inc.peel8:
81; PEELED-NEXT:    [[INC_PEEL9]] = add nsw i32 [[I_05_PEEL4]], 1
82; PEELED-NEXT:    [[CMP_PEEL10:%.*]] = icmp slt i32 [[INC_PEEL9]], [[K]]
83; PEELED-NEXT:    br i1 [[CMP_PEEL10]], label [[FOR_BODY_PEEL3]], label [[OUTER_INC_PEEL11:%.*]]
84; PEELED:       outer.inc.peel11:
85; PEELED-NEXT:    [[J_INC_PEEL12:%.*]] = add nsw i32 [[J_INC_PEEL]], 1
86; PEELED-NEXT:    [[OUTER_CMP_PEEL13:%.*]] = icmp slt i32 [[J_INC_PEEL12]], [[K]]
87; PEELED-NEXT:    br i1 [[OUTER_CMP_PEEL13]], label [[OUTER_HEADER_PEEL_NEXT1:%.*]], label [[FOR_END]]
88; Verify that MD_loop metadata is dropped.
89; PEELED-NOT:   , !llvm.loop !{{[0-9]*}}
90; PEELED:       outer.header.peel.next1:
91; PEELED-NEXT:    br label [[OUTER_HEADER_PEEL_NEXT14:%.*]]
92; PEELED:       outer.header.peel.next14:
93; PEELED-NEXT:    br label [[FOR_BODY_LR_PH_PEEL_NEWPH:%.*]]
94; PEELED:       for.body.lr.ph.peel.newph:
95; PEELED-NEXT:    br label [[OUTER_HEADER:%.*]]
96; PEELED:       outer.header:
97; PEELED-NEXT:    [[J:%.*]] = phi i32 [ [[J_INC_PEEL12]], [[FOR_BODY_LR_PH_PEEL_NEWPH]] ], [ [[J_INC:%.*]], [[OUTER_INC:%.*]] ]
98; PEELED-NEXT:    br label [[FOR_BODY:%.*]]
99; PEELED:       for.body:
100; PEELED-NEXT:    [[I_05:%.*]] = phi i32 [ 0, [[OUTER_HEADER]] ], [ [[INC:%.*]], [[FOR_INC:%.*]] ]
101; PEELED-NEXT:    br i1 false, label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
102; PEELED:       if.then:
103; PEELED-NEXT:    call void @f1()
104; PEELED-NEXT:    br label [[FOR_INC]]
105; PEELED:       if.else:
106; PEELED-NEXT:    call void @f2()
107; PEELED-NEXT:    br label [[FOR_INC]]
108; PEELED:       for.inc:
109; PEELED-NEXT:    [[INC]] = add nsw i32 [[I_05]], 1
110; PEELED-NEXT:    [[CMP:%.*]] = icmp slt i32 [[INC]], [[K]]
111; PEELED-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[OUTER_INC]]
112; PEELED:       outer.inc:
113; PEELED-NEXT:    [[J_INC]] = add nuw nsw i32 [[J]], 1
114; PEELED-NEXT:    [[OUTER_CMP:%.*]] = icmp slt i32 [[J_INC]], [[K]]
115; PEELED-NEXT:    br i1 [[OUTER_CMP]], label [[OUTER_HEADER]], label [[FOR_END_LOOPEXIT:%.*]], !llvm.loop !{{.*}}
116; PEELED:       for.end.loopexit:
117; PEELED-NEXT:    br label [[FOR_END]]
118; PEELED:       for.end:
119; PEELED-NEXT:    ret void
120;
121for.body.lr.ph:
122  br label %outer.header
123
124outer.header:
125  %j = phi i32 [ 0, %for.body.lr.ph ], [ %j.inc, %outer.inc ]
126  br label %for.body
127
128for.body:
129  %i.05 = phi i32 [ 0, %outer.header ], [ %inc, %for.inc ]
130  %cmp1 = icmp ult i32 %j, 2
131  br i1 %cmp1, label %if.then, label %if.else
132
133if.then:
134  call void @f1()
135  br label %for.inc
136
137if.else:
138  call void @f2()
139  br label %for.inc
140
141for.inc:
142  %inc = add nsw i32 %i.05, 1
143  %cmp = icmp slt i32 %inc, %k
144  br i1 %cmp, label %for.body, label %outer.inc
145
146outer.inc:
147  %j.inc = add nsw i32 %j, 1
148  %outer.cmp = icmp slt i32 %j.inc, %k
149  br i1 %outer.cmp, label %outer.header, label %for.end, !llvm.loop !0
150
151for.end:
152  ret void
153}
154
155!0 = distinct !{!0}
156