• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt -S -loop-predication -loop-predication-enable-iv-truncation=true < %s 2>&1 | FileCheck %s
2declare void @llvm.experimental.guard(i1, ...)
3
4declare i32 @length(i8*)
5
6declare i16 @short_length(i8*)
7; Consider range check of type i16 and i32, while IV is of type i64
8; We can loop predicate this because the IV range is within i16 and within i32.
9define i64 @iv_wider_type_rc_two_narrow_types(i32 %offA, i16 %offB, i8* %arrA, i8* %arrB) {
10; CHECK-LABEL: iv_wider_type_rc_two_narrow_types
11entry:
12; CHECK-LABEL: entry:
13; CHECK: [[idxB:[^ ]+]] = sub i16 %lengthB, %offB
14; CHECK-NEXT: [[limit_checkB:[^ ]+]] = icmp ule i16 16, [[idxB]]
15; CHECK-NEXT: [[first_iteration_checkB:[^ ]+]] = icmp ult i16 %offB, %lengthB
16; CHECK-NEXT: [[WideChkB:[^ ]+]] = and i1 [[first_iteration_checkB]], [[limit_checkB]]
17; CHECK-NEXT: [[idxA:[^ ]+]] = sub i32 %lengthA, %offA
18; CHECK-NEXT: [[limit_checkA:[^ ]+]] = icmp ule i32 16, [[idxA]]
19; CHECK-NEXT: [[first_iteration_checkA:[^ ]+]] = icmp ult i32 %offA, %lengthA
20; CHECK-NEXT: [[WideChkA:[^ ]+]] = and i1 [[first_iteration_checkA]], [[limit_checkA]]
21  %lengthA = call i32 @length(i8* %arrA)
22  %lengthB = call i16 @short_length(i8* %arrB)
23   br label %loop
24
25loop:
26; CHECK-LABEL: loop:
27; CHECK: [[invariant_check:[^ ]+]] = and i1 [[WideChkB]], [[WideChkA]]
28; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[invariant_check]], i32 9)
29  %iv = phi i64 [0, %entry ], [ %iv.next, %loop ]
30  %iv.trunc.32 = trunc i64 %iv to i32
31  %iv.trunc.16 = trunc i64 %iv to i16
32  %indexA = add i32 %iv.trunc.32, %offA
33  %indexB = add i16 %iv.trunc.16, %offB
34  %rcA = icmp ult i32 %indexA, %lengthA
35  %rcB = icmp ult i16 %indexB, %lengthB
36  %wide.chk = and i1 %rcA, %rcB
37  call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk, i32 9) [ "deopt"() ]
38  %indexA.ext = zext i32 %indexA to i64
39  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
40  %eltA = load i8, i8* %addrA
41  %indexB.ext = zext i16 %indexB to i64
42  %addrB = getelementptr inbounds i8, i8* %arrB, i64 %indexB.ext
43  store i8 %eltA, i8* %addrB
44  %iv.next = add nuw nsw i64 %iv, 1
45  %latch.check = icmp ult i64 %iv.next, 16
46  br i1 %latch.check, label %loop, label %exit
47
48exit:
49 ret i64 %iv
50}
51
52
53; Consider an IV of type long and an array access into int array.
54; IV is of type i64 while the range check operands are of type i32 and i64.
55define i64 @iv_rc_different_types(i32 %offA, i32 %offB, i8* %arrA, i8* %arrB, i64 %max)
56{
57; CHECK-LABEL: iv_rc_different_types
58entry:
59; CHECK-LABEL: entry:
60; CHECK: [[lenB:[^ ]+]] = add i32 %lengthB, -1
61; CHECK-NEXT: [[idxB:[^ ]+]] = sub i32 [[lenB]], %offB
62; CHECK-NEXT: [[limit_checkB:[^ ]+]] = icmp ule i32 15, [[idxB]]
63; CHECK-NEXT: [[first_iteration_checkB:[^ ]+]] = icmp ult i32 %offB, %lengthB
64; CHECK-NEXT: [[WideChkB:[^ ]+]] = and i1 [[first_iteration_checkB]], [[limit_checkB]]
65; CHECK-NEXT: [[maxMinusOne:[^ ]+]] = add i64 %max, -1
66; CHECK-NEXT: [[limit_checkMax:[^ ]+]] = icmp ule i64 15, [[maxMinusOne]]
67; CHECK-NEXT: [[first_iteration_checkMax:[^ ]+]] = icmp ult  i64 0, %max
68; CHECK-NEXT: [[WideChkMax:[^ ]+]] = and i1 [[first_iteration_checkMax]], [[limit_checkMax]]
69; CHECK-NEXT: [[lenA:[^ ]+]] = add i32 %lengthA, -1
70; CHECK-NEXT: [[idxA:[^ ]+]] = sub i32 [[lenA]], %offA
71; CHECK-NEXT: [[limit_checkA:[^ ]+]] = icmp ule i32 15, [[idxA]]
72; CHECK-NEXT: [[first_iteration_checkA:[^ ]+]] = icmp ult i32 %offA, %lengthA
73; CHECK-NEXT: [[WideChkA:[^ ]+]] = and i1 [[first_iteration_checkA]], [[limit_checkA]]
74  %lengthA = call i32 @length(i8* %arrA)
75  %lengthB = call i32 @length(i8* %arrB)
76  br label %loop
77
78loop:
79; CHECK-LABEL: loop:
80; CHECK: [[BandMax:[^ ]+]] = and i1 [[WideChkB]], [[WideChkMax]]
81; CHECK: [[ABandMax:[^ ]+]] = and i1 [[BandMax]], [[WideChkA]]
82; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 [[ABandMax]], i32 9)
83  %iv = phi i64 [0, %entry ], [ %iv.next, %loop ]
84  %iv.trunc = trunc i64 %iv to i32
85  %indexA = add i32 %iv.trunc, %offA
86  %indexB = add i32 %iv.trunc, %offB
87  %rcA = icmp ult i32 %indexA, %lengthA
88  %rcIV = icmp ult i64 %iv, %max
89  %wide.chk = and i1 %rcA, %rcIV
90  %rcB = icmp ult i32 %indexB, %lengthB
91  %wide.chk.final = and i1 %wide.chk, %rcB
92  call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk.final, i32 9) [ "deopt"() ]
93  %indexA.ext = zext i32 %indexA to i64
94  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
95  %eltA = load i8, i8* %addrA
96  %indexB.ext = zext i32 %indexB to i64
97  %addrB = getelementptr inbounds i8, i8* %arrB, i64 %indexB.ext
98  %eltB = load i8, i8* %addrB
99  %result = xor i8 %eltA, %eltB
100  store i8 %result, i8* %addrA
101  %iv.next = add nuw nsw i64 %iv, 1
102  %latch.check = icmp ult i64 %iv, 15
103  br i1 %latch.check, label %loop, label %exit
104
105exit:
106  ret i64 %iv
107}
108
109; cannot narrow the IV to the range type, because we lose information.
110; for (i64 i= 5; i>= 2; i++)
111; this loop wraps around after reaching 2^64.
112define i64 @iv_rc_different_type(i32 %offA, i8* %arrA) {
113; CHECK-LABEL: iv_rc_different_type
114entry:
115  %lengthA = call i32 @length(i8* %arrA)
116  br label %loop
117
118loop:
119; CHECK-LABEL: loop:
120; CHECK: %rcA = icmp ult i32 %indexA, %lengthA
121; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %rcA, i32 9)
122  %iv = phi i64 [ 5, %entry ], [ %iv.next, %loop ]
123  %iv.trunc.32 = trunc i64 %iv to i32
124  %indexA = add i32 %iv.trunc.32, %offA
125  %rcA = icmp ult i32 %indexA, %lengthA
126  call void (i1, ...) @llvm.experimental.guard(i1 %rcA, i32 9) [ "deopt"() ]
127  %indexA.ext = zext i32 %indexA to i64
128  %addrA = getelementptr inbounds i8, i8* %arrA, i64 %indexA.ext
129  %eltA = load i8, i8* %addrA
130  %res = add i8 %eltA, 2
131  store i8 %eltA, i8* %addrA
132  %iv.next = add i64 %iv, 1
133  %latch.check = icmp sge i64 %iv.next, 2
134  br i1 %latch.check, label %loop, label %exit
135
136exit:
137 ret i64 %iv
138}
139