1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=loop-unroll,simplifycfg,instcombine,simplifycfg -switch-range-to-icmp -S -mtriple arm-none-eabi -mcpu=cortex-m7 %s | FileCheck %s
4 ; This test is meant to check that this loop is unrolled into three iterations.
5 define void @test(ptr %x, i32 %n) {
8 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[N:%.*]], -1
9 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[SUB]], 4
10 ; CHECK-NEXT: [[CMP7:%.*]] = icmp sgt i32 [[REM]], 0
11 ; CHECK-NEXT: br i1 [[CMP7]], label [[WHILE_BODY:%.*]], label [[WHILE_END:%.*]]
13 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[X:%.*]], align 4
14 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[TMP0]], 10
15 ; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
17 ; CHECK-NEXT: store i32 0, ptr [[X]], align 4
18 ; CHECK-NEXT: br label [[IF_END]]
20 ; CHECK-NEXT: [[INCDEC_PTR:%.*]] = getelementptr inbounds i8, ptr [[X]], i32 4
21 ; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i32 [[REM]], 1
22 ; CHECK-NEXT: br i1 [[CMP_NOT]], label [[WHILE_END]], label [[WHILE_BODY_1:%.*]]
23 ; CHECK: while.body.1:
24 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[INCDEC_PTR]], align 4
25 ; CHECK-NEXT: [[CMP1_1:%.*]] = icmp slt i32 [[TMP1]], 10
26 ; CHECK-NEXT: br i1 [[CMP1_1]], label [[IF_THEN_1:%.*]], label [[IF_END_1:%.*]]
28 ; CHECK-NEXT: store i32 0, ptr [[INCDEC_PTR]], align 4
29 ; CHECK-NEXT: br label [[IF_END_1]]
31 ; CHECK-NEXT: [[INCDEC_PTR_1:%.*]] = getelementptr inbounds i8, ptr [[X]], i32 8
32 ; CHECK-NEXT: [[CMP_1:%.*]] = icmp sgt i32 [[REM]], 2
33 ; CHECK-NEXT: br i1 [[CMP_1]], label [[WHILE_BODY_2:%.*]], label [[WHILE_END]]
34 ; CHECK: while.body.2:
35 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[INCDEC_PTR_1]], align 4
36 ; CHECK-NEXT: [[CMP1_2:%.*]] = icmp slt i32 [[TMP2]], 10
37 ; CHECK-NEXT: br i1 [[CMP1_2]], label [[IF_THEN_2:%.*]], label [[WHILE_END]]
39 ; CHECK-NEXT: store i32 0, ptr [[INCDEC_PTR_1]], align 4
40 ; CHECK-NEXT: br label [[WHILE_END]]
42 ; CHECK-NEXT: ret void
45 %sub = add nsw i32 %n, -1
46 %rem = srem i32 %sub, 4
47 %cmp7 = icmp sgt i32 %rem, 0
48 br i1 %cmp7, label %while.body, label %while.end
50 while.body: ; preds = %entry, %if.end
51 %x.addr.09 = phi ptr [ %incdec.ptr, %if.end ], [ %x, %entry ]
52 %n.addr.08 = phi i32 [ %dec, %if.end ], [ %rem, %entry ]
53 %0 = load i32, ptr %x.addr.09, align 4
54 %cmp1 = icmp slt i32 %0, 10
55 br i1 %cmp1, label %if.then, label %if.end
57 if.then: ; preds = %while.body
58 store i32 0, ptr %x.addr.09, align 4
61 if.end: ; preds = %if.then, %while.body
62 %incdec.ptr = getelementptr inbounds i32, ptr %x.addr.09, i32 1
63 %dec = add nsw i32 %n.addr.08, -1
64 %cmp = icmp sgt i32 %dec, 0
65 br i1 %cmp, label %while.body, label %while.end
67 while.end: ; preds = %if.end, %entry
71 ; Larger test that is still fully unrolled, thanks in part to the constant data and the max upper bound.
72 @data = internal constant [50 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50], align 4
73 define i32 @test2(i32 %l86) {
74 ; CHECK-LABEL: @test2(
76 ; CHECK-NEXT: [[L86_OFF:%.*]] = add i32 [[L86:%.*]], -1
77 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[L86_OFF]], 24
78 ; CHECK-NEXT: [[OR_COND23_NOT:%.*]] = icmp ne i32 [[L86]], 25
79 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = zext i1 [[OR_COND23_NOT]] to i32
80 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = select i1 [[SWITCH]], i32 0, i32 [[SPEC_SELECT]]
81 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
84 br label %for.body.i.i
86 for.body.i.i: ; preds = %for.body.i.i.preheader, %for.inc.i.3.i
87 %i.0137.i.i = phi i32 [ %add.i.3.i, %for.inc.i.3.i ], [ 0, %entry ]
88 %add.i.i = or i32 %i.0137.i.i, 1
89 %arrayidx.i.i = getelementptr inbounds i32, ptr @data, i32 %add.i.i
90 %l93 = load i32, ptr %arrayidx.i.i, align 4
91 %cmp1.i.i = icmp sgt i32 %l93, %l86
92 br i1 %cmp1.i.i, label %land.lhs.true.i.i, label %for.inc.i.i
94 land.lhs.true.i.i: ; preds = %for.body.i.i
95 %arrayidx2.i.i = getelementptr inbounds i32, ptr @data, i32 %i.0137.i.i
96 %l94 = load i32, ptr %arrayidx2.i.i, align 4
97 %cmp3.not.i.i = icmp sgt i32 %l94, %l86
98 br i1 %cmp3.not.i.i, label %for.inc.i.i, label %for.end.i.if.end8.i_crit_edge.i.loopexit
100 for.inc.i.i: ; preds = %land.lhs.true.i.i, %for.body.i.i
101 %exitcond.not.i.i = icmp eq i32 %add.i.i, 25
102 br i1 %exitcond.not.i.i, label %if.then6.i.i.loopexitsplit, label %for.body.i.1.i
104 for.body.i.1.i: ; preds = %for.inc.i.i
105 %add.i.1.i = or i32 %i.0137.i.i, 2
106 %arrayidx.i.1.i = getelementptr inbounds i32, ptr @data, i32 %add.i.1.i
107 %l345 = load i32, ptr %arrayidx.i.1.i, align 4
108 %cmp1.i.1.i = icmp sgt i32 %l345, %l86
109 br i1 %cmp1.i.1.i, label %land.lhs.true.i.1.i, label %for.inc.i.1.i
111 land.lhs.true.i.1.i: ; preds = %for.body.i.1.i
112 br i1 %cmp1.i.i, label %for.inc.i.1.i, label %for.end.i.i
114 for.inc.i.1.i: ; preds = %land.lhs.true.i.1.i, %for.body.i.1.i
115 %add.i.2.i = or i32 %i.0137.i.i, 3
116 %arrayidx.i.2.i = getelementptr inbounds i32, ptr @data, i32 %add.i.2.i
117 %l346 = load i32, ptr %arrayidx.i.2.i, align 4
118 %cmp1.i.2.i = icmp sgt i32 %l346, %l86
119 br i1 %cmp1.i.2.i, label %land.lhs.true.i.2.i, label %for.inc.i.2.i
121 land.lhs.true.i.2.i: ; preds = %for.inc.i.1.i
122 br i1 %cmp1.i.1.i, label %for.inc.i.2.i, label %for.end.i.if.end8.i_crit_edge.i.loopexit
124 for.inc.i.2.i: ; preds = %land.lhs.true.i.2.i, %for.inc.i.1.i
125 br label %for.body.i.3.i
127 for.body.i.3.i: ; preds = %for.inc.i.2.i
128 %add.i.3.i = add nuw nsw i32 %i.0137.i.i, 4
129 %arrayidx.i.3.i = getelementptr inbounds i32, ptr @data, i32 %add.i.3.i
130 %l347 = load i32, ptr %arrayidx.i.3.i, align 4
131 %cmp1.i.3.i = icmp sgt i32 %l347, %l86
132 br i1 %cmp1.i.3.i, label %land.lhs.true.i.3.i, label %for.inc.i.3.i
134 land.lhs.true.i.3.i: ; preds = %for.body.i.3.i
135 br i1 %cmp1.i.2.i, label %for.inc.i.3.i, label %for.end.i.i
137 for.inc.i.3.i: ; preds = %land.lhs.true.i.3.i, %for.body.i.3.i
138 br label %for.body.i.i
140 for.end.i.if.end8.i_crit_edge.i.loopexit: ; preds = %land.lhs.true.i.i, %land.lhs.true.i.2.i
141 %i.0.lcssa.i.i3.ph = phi i32 [ %i.0137.i.i, %land.lhs.true.i.i ], [ %add.i.1.i, %land.lhs.true.i.2.i ]
142 br label %for.end.i.if.end8.i_crit_edge.i
144 if.then6.i.i.loopexitsplit: ; preds = %for.inc.i.i
145 br label %if.then6.i.i.loopexit
147 for.end.i.i: ; preds = %land.lhs.true.i.3.i, %land.lhs.true.i.1.i
148 %i.0.lcssa.i.i = phi i32 [ %add.i.i, %land.lhs.true.i.1.i ], [ %add.i.2.i, %land.lhs.true.i.3.i ]
149 %cmp5.i.i = icmp eq i32 %i.0.lcssa.i.i, 25
150 br i1 %cmp5.i.i, label %if.then6.i.i, label %for.end.i.if.end8.i_crit_edge.i
152 for.end.i.if.end8.i_crit_edge.i:
155 if.then6.i.i.loopexit: