1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -indvars -S | FileCheck %s
3
4@array = dso_local global [16777219 x i32] zeroinitializer, align 4
5
6define void @small_const_bound() {
7; CHECK-LABEL: @small_const_bound(
8; CHECK-NEXT:  entry:
9; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
10; CHECK:       for.body:
11; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 100, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
12; CHECK-NEXT:    [[IDXPROM:%.*]] = sext i32 [[IV_INT]] to i64
13; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM]]
14; CHECK-NEXT:    store i32 [[IV_INT]], i32* [[ARRAYIDX]], align 4
15; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
16; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[DEC_INT]], 0
17; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
18; CHECK:       cleanup:
19; CHECK-NEXT:    ret void
20;
21entry:
22  br label %for.body
23
24for.body:                                         ; preds = %for.body, %entry
25  %iv.int = phi i32 [ 100, %entry ], [ %dec.int, %for.body ]
26  %indvar.conv = sitofp i32 %iv.int to float
27  %conv = fptosi float %indvar.conv to i32
28  %idxprom = sext i32 %conv to i64
29  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom
30  store i32 %conv, i32* %arrayidx, align 4
31  %dec.int = add nsw i32 %iv.int, -1
32  %cmp = icmp ugt i32 %dec.int, 0
33  br i1 %cmp, label %for.body, label %cleanup
34
35cleanup:                                          ; preds = %for.body
36  ret void
37}
38
39; Negative test: The transform is *not* valid because there are too many significant bits
40define void @overflow_masked_const_bound() {
41; CHECK-LABEL: @overflow_masked_const_bound(
42; CHECK-NEXT:  entry:
43; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
44; CHECK:       for.body:
45; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 16777218, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
46; CHECK-NEXT:    [[INDVAR_CONV:%.*]] = sitofp i32 [[IV_INT]] to float
47; CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[INDVAR_CONV]] to i32
48; CHECK-NEXT:    [[IDXPROM:%.*]] = sext i32 [[CONV]] to i64
49; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM]]
50; CHECK-NEXT:    store i32 [[CONV]], i32* [[ARRAYIDX]], align 4
51; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
52; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[DEC_INT]], 0
53; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
54; CHECK:       cleanup:
55; CHECK-NEXT:    ret void
56;
57entry:
58  br label %for.body
59
60for.body:                                         ; preds = %for.body, %entry
61  %iv.int = phi i32 [ 16777218, %entry ], [ %dec.int, %for.body ] ; intermediate 16777218 (= 1 << 24 + 2)
62  %indvar.conv = sitofp i32 %iv.int to float
63  %conv = fptosi float %indvar.conv to i32
64  %idxprom = sext i32 %conv to i64
65  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom
66  store i32 %conv, i32* %arrayidx, align 4
67  %dec.int = add nsw i32 %iv.int, -1
68  %cmp = icmp ugt i32 %dec.int, 0
69  br i1 %cmp, label %for.body, label %cleanup
70
71cleanup:                                          ; preds = %for.body
72  ret void
73}
74
75; Negative test: Type mismatch between the integer IV and the fptosi result
76define void @mismatch_type_const() {
77;
78; CHECK-LABEL: @mismatch_type_const(
79; CHECK-NEXT:  entry:
80; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
81; CHECK:       for.body:
82; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 100, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
83; CHECK-NEXT:    [[INDVAR_CONV:%.*]] = sitofp i32 [[IV_INT]] to float
84; CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[INDVAR_CONV]] to i16
85; CHECK-NEXT:    [[IDXPROM32:%.*]] = sext i16 [[CONV]] to i32
86; CHECK-NEXT:    [[IDXPROM64:%.*]] = sext i16 [[CONV]] to i64
87; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM64]]
88; CHECK-NEXT:    store i32 [[IDXPROM32]], i32* [[ARRAYIDX]], align 4
89; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
90; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[DEC_INT]], 0
91; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
92; CHECK:       cleanup:
93; CHECK-NEXT:    ret void
94;
95entry:
96  br label %for.body
97
98for.body:                                         ; preds = %for.body, %entry
99  %iv.int = phi i32 [ 100, %entry ], [ %dec.int, %for.body ]
100  %indvar.conv = sitofp i32 %iv.int to float
101  %conv = fptosi float %indvar.conv to i16
102  %idxprom32 = sext i16 %conv to i32
103  %idxprom64 = sext i16 %conv to i64
104  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom64
105  store i32 %idxprom32, i32* %arrayidx, align 4
106  %dec.int = add nsw i32 %iv.int, -1
107  %cmp = icmp ugt i32 %dec.int, 0
108  br i1 %cmp, label %for.body, label %cleanup
109
110cleanup:                                          ; preds = %for.body
111  ret void
112}
113
114define void @unsigned_const_bound() {
115; CHECK-LABEL: @unsigned_const_bound(
116; CHECK-NEXT:  entry:
117; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
118; CHECK:       for.body:
119; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 100, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
120; CHECK-NEXT:    [[IDXPROM:%.*]] = zext i32 [[IV_INT]] to i64
121; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM]]
122; CHECK-NEXT:    store i32 [[IV_INT]], i32* [[ARRAYIDX]], align 4
123; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
124; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[DEC_INT]], 0
125; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
126; CHECK:       cleanup:
127; CHECK-NEXT:    ret void
128;
129entry:
130  br label %for.body
131
132for.body:                                         ; preds = %for.body, %entry
133  %iv.int = phi i32 [ 100, %entry ], [ %dec.int, %for.body ]
134  %indvar.conv = sitofp i32 %iv.int to float
135  %conv = fptoui float %indvar.conv to i32
136  %idxprom = zext i32 %conv to i64
137  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom
138  store i32 %conv, i32* %arrayidx, align 4
139  %dec.int = add nsw i32 %iv.int, -1
140  %cmp = icmp sgt i32 %dec.int, 0
141  br i1 %cmp, label %for.body, label %cleanup
142
143cleanup:                                          ; preds = %for.body
144  ret void
145}
146
147; TODO: Range including negative value.
148define void @unsigned_const_bound_with_negative () {
149; CHECK-LABEL: @unsigned_const_bound_with_negative(
150; CHECK-NEXT:  entry:
151; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
152; CHECK:       for.body:
153; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 100, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
154; CHECK-NEXT:    [[INDVAR_CONV:%.*]] = sitofp i32 [[IV_INT]] to float
155; CHECK-NEXT:    [[CONV:%.*]] = fptoui float [[INDVAR_CONV]] to i32
156; CHECK-NEXT:    [[IDXPROM:%.*]] = zext i32 [[CONV]] to i64
157; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM]]
158; CHECK-NEXT:    store i32 [[CONV]], i32* [[ARRAYIDX]], align 4
159; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
160; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[DEC_INT]], -100
161; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
162; CHECK:       cleanup:
163; CHECK-NEXT:    ret void
164;
165entry:
166  br label %for.body
167
168for.body:                                         ; preds = %for.body, %entry
169  %iv.int = phi i32 [ 100, %entry ], [ %dec.int, %for.body ]
170  %indvar.conv = sitofp i32 %iv.int to float
171  %conv = fptoui float %indvar.conv to i32
172  %idxprom = zext i32 %conv to i64
173  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom
174  store i32 %conv, i32* %arrayidx, align 4
175  %dec.int = add nsw i32 %iv.int, -1
176  %cmp = icmp sgt i32 %dec.int, -100
177  br i1 %cmp, label %for.body, label %cleanup
178
179cleanup:                                          ; preds = %for.body
180  ret void
181}
182