1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=loop-unroll -mtriple=arm64-apple-iphoneos -S %s | FileCheck %s
4 ; Check we unroll even with optsize, if the result is smaller, either because
5 ; we have single iteration loops or bodies with constant folding opportunities
6 ; after fully unrolling.
10 define void @fully_unrolled_single_iteration(ptr %src) #0 {
11 ; CHECK-LABEL: @fully_unrolled_single_iteration(
13 ; CHECK-NEXT: [[ARR:%.*]] = alloca [4 x i32], align 4
14 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
16 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[SRC:%.*]]
17 ; CHECK-NEXT: store i32 [[V]], ptr [[ARR]], align 4
18 ; CHECK-NEXT: call void @use(ptr nonnull [[ARR]])
19 ; CHECK-NEXT: ret void
22 %arr = alloca [4 x i32], align 4
25 for.body: ; preds = %for.body, %entry
26 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
27 %src.idx = getelementptr inbounds i32, ptr %src, i64 %indvars.iv
28 %v = load i32, ptr %src.idx
29 %arrayidx = getelementptr inbounds [4 x i32], ptr %arr, i64 0, i64 %indvars.iv
30 store i32 %v, ptr %arrayidx, align 4
31 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
32 %exitcond = icmp eq i64 %indvars.iv.next, 1
33 br i1 %exitcond, label %for.cond.cleanup, label %for.body
35 for.cond.cleanup: ; preds = %for.cond
36 call void @use(ptr nonnull %arr) #4
41 define void @fully_unrolled_smaller() #0 {
42 ; CHECK-LABEL: @fully_unrolled_smaller(
44 ; CHECK-NEXT: [[ARR:%.*]] = alloca [4 x i32], align 4
45 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
47 ; CHECK-NEXT: store i32 16, ptr [[ARR]], align 4
48 ; CHECK-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 1
49 ; CHECK-NEXT: store i32 4104, ptr [[ARRAYIDX_1]], align 4
50 ; CHECK-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 2
51 ; CHECK-NEXT: store i32 1048592, ptr [[ARRAYIDX_2]], align 4
52 ; CHECK-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 3
53 ; CHECK-NEXT: store i32 268435480, ptr [[ARRAYIDX_3]], align 4
54 ; CHECK-NEXT: call void @use(ptr nonnull [[ARR]])
55 ; CHECK-NEXT: ret void
58 %arr = alloca [4 x i32], align 4
61 for.body: ; preds = %for.body, %entry
62 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
63 %indvars.iv.tr = trunc i64 %indvars.iv to i32
64 %shl.0 = shl i32 %indvars.iv.tr, 3
65 %shl.1 = shl i32 16, %shl.0
66 %or = or i32 %shl.1, %shl.0
67 %arrayidx = getelementptr inbounds [4 x i32], ptr %arr, i64 0, i64 %indvars.iv
68 store i32 %or, ptr %arrayidx, align 4
69 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
70 %exitcond = icmp eq i64 %indvars.iv, 3
71 br i1 %exitcond, label %for.cond.cleanup, label %for.body
73 for.cond.cleanup: ; preds = %for.cond
74 call void @use(ptr nonnull %arr) #4
78 define void @fully_unrolled_smaller_Oz() #1 {
79 ; CHECK-LABEL: @fully_unrolled_smaller_Oz(
81 ; CHECK-NEXT: [[ARR:%.*]] = alloca [4 x i32], align 4
82 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
84 ; CHECK-NEXT: store i32 16, ptr [[ARR]], align 4
85 ; CHECK-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 1
86 ; CHECK-NEXT: store i32 4104, ptr [[ARRAYIDX_1]], align 4
87 ; CHECK-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 2
88 ; CHECK-NEXT: store i32 1048592, ptr [[ARRAYIDX_2]], align 4
89 ; CHECK-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 3
90 ; CHECK-NEXT: store i32 268435480, ptr [[ARRAYIDX_3]], align 4
91 ; CHECK-NEXT: call void @use(ptr nonnull [[ARR]])
92 ; CHECK-NEXT: ret void
95 %arr = alloca [4 x i32], align 4
98 for.body: ; preds = %for.body, %entry
99 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
100 %indvars.iv.tr = trunc i64 %indvars.iv to i32
101 %shl.0 = shl i32 %indvars.iv.tr, 3
102 %shl.1 = shl i32 16, %shl.0
103 %or = or i32 %shl.1, %shl.0
104 %arrayidx = getelementptr inbounds [4 x i32], ptr %arr, i64 0, i64 %indvars.iv
105 store i32 %or, ptr %arrayidx, align 4
106 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
107 %exitcond = icmp eq i64 %indvars.iv, 3
108 br i1 %exitcond, label %for.cond.cleanup, label %for.body
110 for.cond.cleanup: ; preds = %for.cond
111 call void @use(ptr nonnull %arr) #4
116 define void @fully_unrolled_bigger() #0 {
117 ; CHECK-LABEL: @fully_unrolled_bigger(
119 ; CHECK-NEXT: [[ARR:%.*]] = alloca [4 x i32], align 4
120 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
122 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
123 ; CHECK-NEXT: [[INDVARS_IV_TR:%.*]] = trunc i64 [[INDVARS_IV]] to i32
124 ; CHECK-NEXT: [[SHL_0:%.*]] = shl i32 [[INDVARS_IV_TR]], 3
125 ; CHECK-NEXT: [[SHL_1:%.*]] = shl i32 16, [[SHL_0]]
126 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL_1]], [[SHL_0]]
127 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [4 x i32], ptr [[ARR]], i64 0, i64 [[INDVARS_IV]]
128 ; CHECK-NEXT: store i32 [[OR]], ptr [[ARRAYIDX]], align 4
129 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
130 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[INDVARS_IV]], 7
131 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
132 ; CHECK: for.cond.cleanup:
133 ; CHECK-NEXT: call void @use(ptr nonnull [[ARR]])
134 ; CHECK-NEXT: ret void
137 %arr = alloca [4 x i32], align 4
140 for.body: ; preds = %for.body, %entry
141 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
142 %indvars.iv.tr = trunc i64 %indvars.iv to i32
143 %shl.0 = shl i32 %indvars.iv.tr, 3
144 %shl.1 = shl i32 16, %shl.0
145 %or = or i32 %shl.1, %shl.0
146 %arrayidx = getelementptr inbounds [4 x i32], ptr %arr, i64 0, i64 %indvars.iv
147 store i32 %or, ptr %arrayidx, align 4
148 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
149 %exitcond = icmp eq i64 %indvars.iv, 7
150 br i1 %exitcond, label %for.cond.cleanup, label %for.body
152 for.cond.cleanup: ; preds = %for.cond
153 call void @use(ptr nonnull %arr) #4
157 declare void @use(ptr)
159 attributes #0 = { optsize }
160 attributes #1 = { minsize optsize }