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