1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -loop-predication -loop-predication-enable-iv-truncation=true < %s 2>&1 | FileCheck %s
3declare void @llvm.experimental.guard(i1, ...)
4
5declare i32 @length(i8*)
6
7declare i16 @short_length(i8*)
8; Consider range check of type i16 and i32, while IV is of type i64
9; We can loop predicate this because the IV range is within i16 and within i32.
10define i64 @iv_wider_type_rc_two_narrow_types(i32 %offA, i16 %offB, i8* %arrA, i8* %arrB) {
11; CHECK-LABEL: @iv_wider_type_rc_two_narrow_types(
12; CHECK-NEXT:  entry:
13; CHECK-NEXT:    [[LENGTHA:%.*]] = call i32 @length(i8* [[ARRA:%.*]])
14; CHECK-NEXT:    [[LENGTHB:%.*]] = call i16 @short_length(i8* [[ARRB:%.*]])
15; CHECK-NEXT:    [[TMP0:%.*]] = sub i16 [[LENGTHB]], [[OFFB:%.*]]
16; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i16 16, [[TMP0]]
17; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i16 [[OFFB]], [[LENGTHB]]
18; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP2]], [[TMP1]]
19; CHECK-NEXT:    [[TMP4:%.*]] = sub i32 [[LENGTHA]], [[OFFA:%.*]]
20; CHECK-NEXT:    [[TMP5:%.*]] = icmp ule i32 16, [[TMP4]]
21; CHECK-NEXT:    [[TMP6:%.*]] = icmp ult i32 [[OFFA]], [[LENGTHA]]
22; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP6]], [[TMP5]]
23; CHECK-NEXT:    [[TMP8:%.*]] = and i1 [[TMP3]], [[TMP7]]
24; CHECK-NEXT:    br label [[LOOP:%.*]]
25; CHECK:       loop:
26; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
27; CHECK-NEXT:    [[IV_TRUNC_32:%.*]] = trunc i64 [[IV]] to i32
28; CHECK-NEXT:    [[IV_TRUNC_16:%.*]] = trunc i64 [[IV]] to i16
29; CHECK-NEXT:    [[INDEXA:%.*]] = add i32 [[IV_TRUNC_32]], [[OFFA]]
30; CHECK-NEXT:    [[INDEXB:%.*]] = add i16 [[IV_TRUNC_16]], [[OFFB]]
31; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[TMP8]], i32 9) [ "deopt"() ]
32; CHECK-NEXT:    [[INDEXA_EXT:%.*]] = zext i32 [[INDEXA]] to i64
33; CHECK-NEXT:    [[ADDRA:%.*]] = getelementptr inbounds i8, i8* [[ARRA]], i64 [[INDEXA_EXT]]
34; CHECK-NEXT:    [[ELTA:%.*]] = load i8, i8* [[ADDRA]]
35; CHECK-NEXT:    [[INDEXB_EXT:%.*]] = zext i16 [[INDEXB]] to i64
36; CHECK-NEXT:    [[ADDRB:%.*]] = getelementptr inbounds i8, i8* [[ARRB]], i64 [[INDEXB_EXT]]
37; CHECK-NEXT:    store i8 [[ELTA]], i8* [[ADDRB]]
38; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
39; CHECK-NEXT:    [[LATCH_CHECK:%.*]] = icmp ult i64 [[IV_NEXT]], 16
40; CHECK-NEXT:    br i1 [[LATCH_CHECK]], label [[LOOP]], label [[EXIT:%.*]]
41; CHECK:       exit:
42; CHECK-NEXT:    [[IV_LCSSA:%.*]] = phi i64 [ [[IV]], [[LOOP]] ]
43; CHECK-NEXT:    ret i64 [[IV_LCSSA]]
44;
45entry:
46  %lengthA = call i32 @length(i8* %arrA)
47  %lengthB = call i16 @short_length(i8* %arrB)
48  br label %loop
49
50loop:
51  %iv = phi i64 [0, %entry ], [ %iv.next, %loop ]
52  %iv.trunc.32 = trunc i64 %iv to i32
53  %iv.trunc.16 = trunc i64 %iv to i16
54  %indexA = add i32 %iv.trunc.32, %offA
55  %indexB = add i16 %iv.trunc.16, %offB
56  %rcA = icmp ult i32 %indexA, %lengthA
57  %rcB = icmp ult i16 %indexB, %lengthB
58  %wide.chk = and i1 %rcA, %rcB
59  call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk, i32 9) [ "deopt"() ]
60  %indexA.ext = zext i32 %indexA to i64
61  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
62  %eltA = load i8, i8* %addrA
63  %indexB.ext = zext i16 %indexB to i64
64  %addrB = getelementptr inbounds i8, i8* %arrB, i64 %indexB.ext
65  store i8 %eltA, i8* %addrB
66  %iv.next = add nuw nsw i64 %iv, 1
67  %latch.check = icmp ult i64 %iv.next, 16
68  br i1 %latch.check, label %loop, label %exit
69
70exit:
71  ret i64 %iv
72}
73
74
75; Consider an IV of type long and an array access into int array.
76; IV is of type i64 while the range check operands are of type i32 and i64.
77define i64 @iv_rc_different_types(i32 %offA, i32 %offB, i8* %arrA, i8* %arrB, i64 %max)
78; CHECK-LABEL: @iv_rc_different_types(
79; CHECK-NEXT:  entry:
80; CHECK-NEXT:    [[LENGTHA:%.*]] = call i32 @length(i8* [[ARRA:%.*]])
81; CHECK-NEXT:    [[LENGTHB:%.*]] = call i32 @length(i8* [[ARRB:%.*]])
82; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[LENGTHB]], -1
83; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 [[TMP0]], [[OFFB:%.*]]
84; CHECK-NEXT:    [[TMP2:%.*]] = icmp ule i32 15, [[TMP1]]
85; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i32 [[OFFB]], [[LENGTHB]]
86; CHECK-NEXT:    [[TMP4:%.*]] = and i1 [[TMP3]], [[TMP2]]
87; CHECK-NEXT:    [[TMP5:%.*]] = add i64 [[MAX:%.*]], -1
88; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 15, [[TMP5]]
89; CHECK-NEXT:    [[TMP7:%.*]] = icmp ult i64 0, [[MAX]]
90; CHECK-NEXT:    [[TMP8:%.*]] = and i1 [[TMP7]], [[TMP6]]
91; CHECK-NEXT:    [[TMP9:%.*]] = add i32 [[LENGTHA]], -1
92; CHECK-NEXT:    [[TMP10:%.*]] = sub i32 [[TMP9]], [[OFFA:%.*]]
93; CHECK-NEXT:    [[TMP11:%.*]] = icmp ule i32 15, [[TMP10]]
94; CHECK-NEXT:    [[TMP12:%.*]] = icmp ult i32 [[OFFA]], [[LENGTHA]]
95; CHECK-NEXT:    [[TMP13:%.*]] = and i1 [[TMP12]], [[TMP11]]
96; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP4]], [[TMP8]]
97; CHECK-NEXT:    [[TMP15:%.*]] = and i1 [[TMP14]], [[TMP13]]
98; CHECK-NEXT:    br label [[LOOP:%.*]]
99; CHECK:       loop:
100; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
101; CHECK-NEXT:    [[IV_TRUNC:%.*]] = trunc i64 [[IV]] to i32
102; CHECK-NEXT:    [[INDEXA:%.*]] = add i32 [[IV_TRUNC]], [[OFFA]]
103; CHECK-NEXT:    [[INDEXB:%.*]] = add i32 [[IV_TRUNC]], [[OFFB]]
104; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[TMP15]], i32 9) [ "deopt"() ]
105; CHECK-NEXT:    [[INDEXA_EXT:%.*]] = zext i32 [[INDEXA]] to i64
106; CHECK-NEXT:    [[ADDRA:%.*]] = getelementptr inbounds i8, i8* [[ARRA]], i64 [[INDEXA_EXT]]
107; CHECK-NEXT:    [[ELTA:%.*]] = load i8, i8* [[ADDRA]]
108; CHECK-NEXT:    [[INDEXB_EXT:%.*]] = zext i32 [[INDEXB]] to i64
109; CHECK-NEXT:    [[ADDRB:%.*]] = getelementptr inbounds i8, i8* [[ARRB]], i64 [[INDEXB_EXT]]
110; CHECK-NEXT:    [[ELTB:%.*]] = load i8, i8* [[ADDRB]]
111; CHECK-NEXT:    [[RESULT:%.*]] = xor i8 [[ELTA]], [[ELTB]]
112; CHECK-NEXT:    store i8 [[RESULT]], i8* [[ADDRA]]
113; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
114; CHECK-NEXT:    [[LATCH_CHECK:%.*]] = icmp ult i64 [[IV]], 15
115; CHECK-NEXT:    br i1 [[LATCH_CHECK]], label [[LOOP]], label [[EXIT:%.*]]
116; CHECK:       exit:
117; CHECK-NEXT:    [[IV_LCSSA:%.*]] = phi i64 [ [[IV]], [[LOOP]] ]
118; CHECK-NEXT:    ret i64 [[IV_LCSSA]]
119;
120{
121entry:
122  %lengthA = call i32 @length(i8* %arrA)
123  %lengthB = call i32 @length(i8* %arrB)
124  br label %loop
125
126loop:
127  %iv = phi i64 [0, %entry ], [ %iv.next, %loop ]
128  %iv.trunc = trunc i64 %iv to i32
129  %indexA = add i32 %iv.trunc, %offA
130  %indexB = add i32 %iv.trunc, %offB
131  %rcA = icmp ult i32 %indexA, %lengthA
132  %rcIV = icmp ult i64 %iv, %max
133  %wide.chk = and i1 %rcA, %rcIV
134  %rcB = icmp ult i32 %indexB, %lengthB
135  %wide.chk.final = and i1 %wide.chk, %rcB
136  call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk.final, i32 9) [ "deopt"() ]
137  %indexA.ext = zext i32 %indexA to i64
138  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
139  %eltA = load i8, i8* %addrA
140  %indexB.ext = zext i32 %indexB to i64
141  %addrB = getelementptr inbounds i8, i8* %arrB, i64 %indexB.ext
142  %eltB = load i8, i8* %addrB
143  %result = xor i8 %eltA, %eltB
144  store i8 %result, i8* %addrA
145  %iv.next = add nuw nsw i64 %iv, 1
146  %latch.check = icmp ult i64 %iv, 15
147  br i1 %latch.check, label %loop, label %exit
148
149exit:
150  ret i64 %iv
151}
152
153; cannot narrow the IV to the range type, because we lose information.
154; for (i64 i= 5; i>= 2; i++)
155; this loop wraps around after reaching 2^64.
156define i64 @iv_rc_different_type(i32 %offA, i8* %arrA) {
157; CHECK-LABEL: @iv_rc_different_type(
158; CHECK-NEXT:  entry:
159; CHECK-NEXT:    [[LENGTHA:%.*]] = call i32 @length(i8* [[ARRA:%.*]])
160; CHECK-NEXT:    br label [[LOOP:%.*]]
161; CHECK:       loop:
162; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 5, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
163; CHECK-NEXT:    [[IV_TRUNC_32:%.*]] = trunc i64 [[IV]] to i32
164; CHECK-NEXT:    [[INDEXA:%.*]] = add i32 [[IV_TRUNC_32]], [[OFFA:%.*]]
165; CHECK-NEXT:    [[RCA:%.*]] = icmp ult i32 [[INDEXA]], [[LENGTHA]]
166; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[RCA]], i32 9) [ "deopt"() ]
167; CHECK-NEXT:    [[INDEXA_EXT:%.*]] = zext i32 [[INDEXA]] to i64
168; CHECK-NEXT:    [[ADDRA:%.*]] = getelementptr inbounds i8, i8* [[ARRA]], i64 [[INDEXA_EXT]]
169; CHECK-NEXT:    [[ELTA:%.*]] = load i8, i8* [[ADDRA]]
170; CHECK-NEXT:    [[RES:%.*]] = add i8 [[ELTA]], 2
171; CHECK-NEXT:    store i8 [[ELTA]], i8* [[ADDRA]]
172; CHECK-NEXT:    [[IV_NEXT]] = add i64 [[IV]], 1
173; CHECK-NEXT:    [[LATCH_CHECK:%.*]] = icmp sge i64 [[IV_NEXT]], 2
174; CHECK-NEXT:    br i1 [[LATCH_CHECK]], label [[LOOP]], label [[EXIT:%.*]]
175; CHECK:       exit:
176; CHECK-NEXT:    [[IV_LCSSA:%.*]] = phi i64 [ [[IV]], [[LOOP]] ]
177; CHECK-NEXT:    ret i64 [[IV_LCSSA]]
178;
179entry:
180  %lengthA = call i32 @length(i8* %arrA)
181  br label %loop
182
183loop:
184  %iv = phi i64 [ 5, %entry ], [ %iv.next, %loop ]
185  %iv.trunc.32 = trunc i64 %iv to i32
186  %indexA = add i32 %iv.trunc.32, %offA
187  %rcA = icmp ult i32 %indexA, %lengthA
188  call void (i1, ...) @llvm.experimental.guard(i1 %rcA, i32 9) [ "deopt"() ]
189  %indexA.ext = zext i32 %indexA to i64
190  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
191  %eltA = load i8, i8* %addrA
192  %res = add i8 %eltA, 2
193  store i8 %eltA, i8* %addrA
194  %iv.next = add i64 %iv, 1
195  %latch.check = icmp sge i64 %iv.next, 2
196  br i1 %latch.check, label %loop, label %exit
197
198exit:
199  ret i64 %iv
200}
201