xref: /llvm-project/llvm/test/Transforms/LoopUnroll/AArch64/unroll-optsize.ll (revision ef992b60798b6cd2c50b25351bfc392e319896b7)
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
3
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.
7
8declare i32 @get()
9
10define void @fully_unrolled_single_iteration(ptr %src) #0 {
11; CHECK-LABEL: @fully_unrolled_single_iteration(
12; CHECK-NEXT:  entry:
13; CHECK-NEXT:    [[ARR:%.*]] = alloca [4 x i32], align 4
14; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
15; CHECK:       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
20;
21entry:
22  %arr = alloca [4 x i32], align 4
23  br label %for.body
24
25for.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
34
35for.cond.cleanup:                                 ; preds = %for.cond
36  call void @use(ptr nonnull %arr) #4
37  ret void
38}
39
40
41define void @fully_unrolled_smaller() #0 {
42; CHECK-LABEL: @fully_unrolled_smaller(
43; CHECK-NEXT:  entry:
44; CHECK-NEXT:    [[ARR:%.*]] = alloca [4 x i32], align 4
45; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
46; CHECK:       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
56;
57entry:
58  %arr = alloca [4 x i32], align 4
59  br label %for.body
60
61for.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
72
73for.cond.cleanup:                                 ; preds = %for.cond
74  call void @use(ptr nonnull %arr) #4
75  ret void
76}
77
78define void @fully_unrolled_smaller_Oz() #1 {
79; CHECK-LABEL: @fully_unrolled_smaller_Oz(
80; CHECK-NEXT:  entry:
81; CHECK-NEXT:    [[ARR:%.*]] = alloca [4 x i32], align 4
82; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
83; CHECK:       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
93;
94entry:
95  %arr = alloca [4 x i32], align 4
96  br label %for.body
97
98for.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
109
110for.cond.cleanup:                                 ; preds = %for.cond
111  call void @use(ptr nonnull %arr) #4
112  ret void
113}
114
115
116define void @fully_unrolled_bigger() #0 {
117; CHECK-LABEL: @fully_unrolled_bigger(
118; CHECK-NEXT:  entry:
119; CHECK-NEXT:    [[ARR:%.*]] = alloca [4 x i32], align 4
120; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
121; CHECK:       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
135;
136entry:
137  %arr = alloca [4 x i32], align 4
138  br label %for.body
139
140for.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
151
152for.cond.cleanup:                                 ; preds = %for.cond
153  call void @use(ptr nonnull %arr) #4
154  ret void
155}
156
157declare void @use(ptr)
158
159attributes #0 = { optsize }
160attributes #1 = { minsize optsize }
161