1; RUN: opt < %s -indvars -S | FileCheck %s
2target triple = "aarch64--linux-gnu"
3
4; Provide legal integer types.
5target datalayout = "n8:16:32:64"
6
7
8; Check the loop exit i32 compare instruction and operand are widened to i64
9; instead of truncating IV before its use in the i32 compare instruction.
10
11@idx = common global i32 0, align 4
12@e = common global i32 0, align 4
13@ptr = common global i32* null, align 8
14
15; CHECK-LABEL: @test1
16; CHECK: for.body.lr.ph:
17; CHECK: sext i32
18; CHECK: for.cond:
19; CHECK: icmp slt i64
20; CHECK: for.body:
21; CHECK: phi i64
22
23define i32 @test1() {
24entry:
25  store i32 -1, i32* @idx, align 4
26  %0 = load i32, i32* @e, align 4
27  %cmp4 = icmp slt i32 %0, 0
28  br i1 %cmp4, label %for.end.loopexit, label %for.body.lr.ph
29
30for.body.lr.ph:
31  %1 = load i32*, i32** @ptr, align 8
32  %2 = load i32, i32* @e, align 4
33  br label %for.body
34
35for.cond:
36  %inc = add nsw i32 %i.05, 1
37  %cmp = icmp slt i32 %i.05, %2
38  br i1 %cmp, label %for.body, label %for.cond.for.end.loopexit_crit_edge
39
40for.body:
41  %i.05 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.cond ]
42  %idxprom = sext i32 %i.05 to i64
43  %arrayidx = getelementptr inbounds i32, i32* %1, i64 %idxprom
44  %3 = load i32, i32* %arrayidx, align 4
45  %tobool = icmp eq i32 %3, 0
46  br i1 %tobool, label %if.then, label %for.cond
47
48if.then:
49  %i.05.lcssa = phi i32 [ %i.05, %for.body ]
50  store i32 %i.05.lcssa, i32* @idx, align 4
51  br label %for.end
52
53for.cond.for.end.loopexit_crit_edge:
54  br label %for.end.loopexit
55
56for.end.loopexit:
57  br label %for.end
58
59for.end:
60  %4 = load i32, i32* @idx, align 4
61  ret i32 %4
62}
63
64; CHECK-LABEL: @test2
65; CHECK: for.body4.us
66; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
67; CHECK: %cmp2.us = icmp slt i64
68; CHECK-NOT: %2 = trunc i64 %indvars.iv.next to i32
69; CHECK-NOT: %cmp2.us = icmp slt i32
70
71define void @test2([8 x i8]* %a, i8* %b, i8 %limit) {
72entry:
73  %conv = zext i8 %limit to i32
74  br i1 undef, label %for.cond1.preheader, label %for.cond1.preheader.us
75
76for.cond1.preheader.us:
77  %storemerge5.us = phi i32 [ 0, %entry ], [ %inc14.us, %for.inc13.us ]
78  br i1 true, label %for.body4.lr.ph.us, label %for.inc13.us
79
80for.inc13.us:
81  %inc14.us = add nsw i32 %storemerge5.us, 1
82  %cmp.us = icmp slt i32 %inc14.us, 4
83  br i1 %cmp.us, label %for.cond1.preheader.us, label %for.end
84
85for.body4.us:
86  %storemerge14.us = phi i32 [ 0, %for.body4.lr.ph.us ], [ %inc.us, %for.body4.us ]
87  %idxprom.us = sext i32 %storemerge14.us to i64
88  %arrayidx6.us = getelementptr inbounds [8 x i8], [8 x i8]* %a, i64 %idxprom5.us, i64 %idxprom.us
89  %0 = load i8, i8* %arrayidx6.us, align 1
90  %idxprom7.us = zext i8 %0 to i64
91  %arrayidx8.us = getelementptr inbounds i8, i8* %b, i64 %idxprom7.us
92  %1 = load i8, i8* %arrayidx8.us, align 1
93  store i8 %1, i8* %arrayidx6.us, align 1
94  %inc.us = add nsw i32 %storemerge14.us, 1
95  %cmp2.us = icmp slt i32 %inc.us, %conv
96  br i1 %cmp2.us, label %for.body4.us, label %for.inc13.us
97
98for.body4.lr.ph.us:
99  %idxprom5.us = sext i32 %storemerge5.us to i64
100  br label %for.body4.us
101
102for.cond1.preheader:
103  %storemerge5 = phi i32 [ 0, %entry ], [ %inc14, %for.inc13 ]
104  br i1 false, label %for.inc13, label %for.inc13
105
106for.inc13:
107  %inc14 = add nsw i32 %storemerge5, 1
108  %cmp = icmp slt i32 %inc14, 4
109  br i1 %cmp, label %for.cond1.preheader, label %for.end
110
111for.end:
112  ret void
113}
114
115; CHECK-LABEL: @test3
116; CHECK: sext i32 %b
117; CHECK: for.cond:
118; CHECK: phi i64
119; CHECK: icmp slt i64
120
121define i32 @test3(i32* %a, i32 %b) {
122entry:
123  br label %for.cond
124
125for.cond:
126  %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
127  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
128  %cmp = icmp slt i32 %i.0, %b
129  br i1 %cmp, label %for.body, label %for.end
130
131for.body:
132  %idxprom = sext i32 %i.0 to i64
133  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
134  %0 = load i32, i32* %arrayidx, align 4
135  %add = add nsw i32 %sum.0, %0
136  %inc = add nsw i32 %i.0, 1
137  br label %for.cond
138
139for.end:
140  ret i32 %sum.0
141}
142
143declare i32 @fn1(i8 signext)
144
145; PR21030
146; CHECK-LABEL: @test4
147; CHECK: for.body:
148; CHECK: phi i32
149; CHECK: icmp sgt i8
150
151define i32 @test4(i32 %a) {
152entry:
153  br label %for.body
154
155for.body:
156  %c.07 = phi i8 [ -3, %entry ], [ %dec, %for.body ]
157  %conv6 = zext i8 %c.07 to i32
158  %or = or i32 %a, %conv6
159  %conv3 = trunc i32 %or to i8
160  %call = call i32 @fn1(i8 signext %conv3)
161  %dec = add i8 %c.07, -1
162  %cmp = icmp sgt i8 %dec, -14
163  br i1 %cmp, label %for.body, label %for.end
164
165for.end:
166  ret i32 0
167}
168
169; CHECK-LABEL: @test5
170; CHECK: zext i32 %b
171; CHECK: for.cond:
172; CHECK: phi i64
173; CHECK: icmp ule i64
174
175define i32 @test5(i32* %a, i32 %b) {
176entry:
177  br label %for.cond
178
179for.cond:
180  %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
181  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
182  %cmp = icmp ule i32 %i.0, %b
183  br i1 %cmp, label %for.body, label %for.end
184
185for.body:
186  %idxprom = zext i32 %i.0 to i64
187  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
188  %0 = load i32, i32* %arrayidx, align 4
189  %add = add nsw i32 %sum.0, %0
190  %inc = add nsw i32 %i.0, 1
191  br label %for.cond
192
193for.end:
194  ret i32 %sum.0
195}
196