1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; This testcase tests for various features the basicaa test should be able to
3 ; determine, as noted in the comments.
5 ; RUN: opt < %s -basic-aa -gvn -instcombine -dce -S | FileCheck %s --check-prefixes=CHECK,NO_ASSUME
6 ; RUN: opt < %s -basic-aa -gvn -instcombine -dce --enable-knowledge-retention -S | FileCheck %s --check-prefixes=CHECK,USE_ASSUME
7 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
9 @Global = external global { i32 }
11 declare void @external(i32*)
12 declare void @llvm.assume(i1)
14 ; Array test: Test that operations on one local array do not invalidate
15 ; operations on another array. Important for scientific codes.
17 define i32 @different_array_test(i64 %A, i64 %B) {
18 ; NO_ASSUME-LABEL: @different_array_test(
19 ; NO_ASSUME-NEXT: [[ARRAY11:%.*]] = alloca [100 x i32], align 4
20 ; NO_ASSUME-NEXT: [[ARRAY22:%.*]] = alloca [200 x i32], align 4
21 ; NO_ASSUME-NEXT: [[ARRAY22_SUB:%.*]] = getelementptr inbounds [200 x i32], [200 x i32]* [[ARRAY22]], i64 0, i64 0
22 ; NO_ASSUME-NEXT: [[ARRAY11_SUB:%.*]] = getelementptr inbounds [100 x i32], [100 x i32]* [[ARRAY11]], i64 0, i64 0
23 ; NO_ASSUME-NEXT: call void @llvm.assume(i1 true) [ "align"(i32* [[ARRAY11_SUB]], i32 4) ]
24 ; NO_ASSUME-NEXT: call void @external(i32* nonnull [[ARRAY11_SUB]])
25 ; NO_ASSUME-NEXT: call void @external(i32* nonnull [[ARRAY22_SUB]])
26 ; NO_ASSUME-NEXT: [[POINTER2:%.*]] = getelementptr [200 x i32], [200 x i32]* [[ARRAY22]], i64 0, i64 [[B:%.*]]
27 ; NO_ASSUME-NEXT: store i32 7, i32* [[POINTER2]], align 4
28 ; NO_ASSUME-NEXT: ret i32 0
30 ; USE_ASSUME-LABEL: @different_array_test(
31 ; USE_ASSUME-NEXT: [[ARRAY11:%.*]] = alloca [100 x i32], align 4
32 ; USE_ASSUME-NEXT: [[ARRAY22:%.*]] = alloca [200 x i32], align 4
33 ; USE_ASSUME-NEXT: [[ARRAY22_SUB:%.*]] = getelementptr inbounds [200 x i32], [200 x i32]* [[ARRAY22]], i64 0, i64 0
34 ; USE_ASSUME-NEXT: [[ARRAY11_SUB:%.*]] = getelementptr inbounds [100 x i32], [100 x i32]* [[ARRAY11]], i64 0, i64 0
35 ; USE_ASSUME-NEXT: call void @external(i32* nonnull [[ARRAY11_SUB]])
36 ; USE_ASSUME-NEXT: call void @external(i32* nonnull [[ARRAY22_SUB]])
37 ; USE_ASSUME-NEXT: [[POINTER2:%.*]] = getelementptr [200 x i32], [200 x i32]* [[ARRAY22]], i64 0, i64 [[B:%.*]]
38 ; USE_ASSUME-NEXT: store i32 7, i32* [[POINTER2]], align 4
39 ; USE_ASSUME-NEXT: ret i32 0
41 %Array1 = alloca i32, i32 100
42 %Array2 = alloca i32, i32 200
43 call void @llvm.assume(i1 true) ["align"(i32* %Array1, i32 4)]
45 call void @external(i32* %Array1)
46 call void @external(i32* %Array2)
48 %pointer = getelementptr i32, i32* %Array1, i64 %A
49 %val = load i32, i32* %pointer
51 %pointer2 = getelementptr i32, i32* %Array2, i64 %B
52 store i32 7, i32* %pointer2
54 %REMOVE = load i32, i32* %pointer ; redundant with above load
55 %retval = sub i32 %REMOVE, %val
59 ; Constant index test: Constant indexes into the same array should not
60 ; interfere with each other. Again, important for scientific codes.
62 define i32 @constant_array_index_test() {
63 ; CHECK-LABEL: @constant_array_index_test(
64 ; CHECK-NEXT: [[ARRAY1:%.*]] = alloca [100 x i32], align 4
65 ; CHECK-NEXT: [[ARRAY1_SUB:%.*]] = getelementptr inbounds [100 x i32], [100 x i32]* [[ARRAY1]], i64 0, i64 0
66 ; CHECK-NEXT: call void @external(i32* nonnull [[ARRAY1_SUB]])
67 ; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds [100 x i32], [100 x i32]* [[ARRAY1]], i64 0, i64 6
68 ; CHECK-NEXT: store i32 1, i32* [[P2]], align 4
69 ; CHECK-NEXT: ret i32 0
71 %Array = alloca i32, i32 100
72 call void @external(i32* %Array)
74 %P1 = getelementptr i32, i32* %Array, i64 7
75 %P2 = getelementptr i32, i32* %Array, i64 6
77 %A = load i32, i32* %P1
78 store i32 1, i32* %P2 ; Should not invalidate load
79 %BREMOVE = load i32, i32* %P1
80 %Val = sub i32 %A, %BREMOVE
84 ; Test that if two pointers are spaced out by a constant getelementptr, that
86 define i32 @gep_distance_test(i32* %A) {
87 ; NO_ASSUME-LABEL: @gep_distance_test(
88 ; NO_ASSUME-NEXT: [[B:%.*]] = getelementptr i32, i32* [[A:%.*]], i64 2
89 ; NO_ASSUME-NEXT: store i32 7, i32* [[B]], align 4
90 ; NO_ASSUME-NEXT: ret i32 0
92 ; USE_ASSUME-LABEL: @gep_distance_test(
93 ; USE_ASSUME-NEXT: [[B:%.*]] = getelementptr i32, i32* [[A:%.*]], i64 2
94 ; USE_ASSUME-NEXT: store i32 7, i32* [[B]], align 4
95 ; USE_ASSUME-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(i32* [[A]], i64 4), "nonnull"(i32* [[A]]), "align"(i32* [[A]], i64 4) ]
96 ; USE_ASSUME-NEXT: ret i32 0
98 %REMOVEu = load i32, i32* %A
99 %B = getelementptr i32, i32* %A, i64 2 ; Cannot alias A
101 %REMOVEv = load i32, i32* %A
102 %r = sub i32 %REMOVEu, %REMOVEv
106 ; Test that if two pointers are spaced out by a constant offset, that they
107 ; cannot alias, even if there is a variable offset between them...
108 define i32 @gep_distance_test2({i32,i32}* %A, i64 %distance) {
109 ; NO_ASSUME-LABEL: @gep_distance_test2(
110 ; NO_ASSUME-NEXT: [[B:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[A:%.*]], i64 [[DISTANCE:%.*]], i32 1
111 ; NO_ASSUME-NEXT: store i32 7, i32* [[B]], align 4
112 ; NO_ASSUME-NEXT: ret i32 0
114 ; USE_ASSUME-LABEL: @gep_distance_test2(
115 ; USE_ASSUME-NEXT: [[A1:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[A:%.*]], i64 0, i32 0
116 ; USE_ASSUME-NEXT: [[B:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[A]], i64 [[DISTANCE:%.*]], i32 1
117 ; USE_ASSUME-NEXT: store i32 7, i32* [[B]], align 4
118 ; USE_ASSUME-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(i32* [[A1]], i64 4), "nonnull"({ i32, i32 }* [[A]]), "align"(i32* [[A1]], i64 4) ]
119 ; USE_ASSUME-NEXT: ret i32 0
121 %A1 = getelementptr {i32,i32}, {i32,i32}* %A, i64 0, i32 0
122 %REMOVEu = load i32, i32* %A1
123 %B = getelementptr {i32,i32}, {i32,i32}* %A, i64 %distance, i32 1
124 store i32 7, i32* %B ; B cannot alias A, it's at least 4 bytes away
125 %REMOVEv = load i32, i32* %A1
126 %r = sub i32 %REMOVEu, %REMOVEv
130 ; Test that we can do funny pointer things and that distance calc will still
132 define i32 @gep_distance_test3(i32 * %A) {
133 ; NO_ASSUME-LABEL: @gep_distance_test3(
134 ; NO_ASSUME-NEXT: [[C1:%.*]] = getelementptr i32, i32* [[A:%.*]], i64 1
135 ; NO_ASSUME-NEXT: [[C:%.*]] = bitcast i32* [[C1]] to i8*
136 ; NO_ASSUME-NEXT: store i8 42, i8* [[C]], align 1
137 ; NO_ASSUME-NEXT: ret i32 0
139 ; USE_ASSUME-LABEL: @gep_distance_test3(
140 ; USE_ASSUME-NEXT: [[C1:%.*]] = getelementptr i32, i32* [[A:%.*]], i64 1
141 ; USE_ASSUME-NEXT: [[C:%.*]] = bitcast i32* [[C1]] to i8*
142 ; USE_ASSUME-NEXT: store i8 42, i8* [[C]], align 4
143 ; USE_ASSUME-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(i32* [[A]], i64 4), "nonnull"(i32* [[A]]), "align"(i32* [[A]], i64 4) ]
144 ; USE_ASSUME-NEXT: ret i32 0
146 %X = load i32, i32* %A
147 %B = bitcast i32* %A to i8*
148 %C = getelementptr i8, i8* %B, i64 4
150 %Y = load i32, i32* %A
155 ; Test that we can disambiguate globals reached through constantexpr geps
156 define i32 @constexpr_test() {
157 ; CHECK-LABEL: @constexpr_test(
158 ; CHECK-NEXT: [[X:%.*]] = alloca i32, align 4
159 ; CHECK-NEXT: call void @external(i32* nonnull [[X]])
160 ; CHECK-NEXT: store i32 5, i32* getelementptr inbounds ({ i32 }, { i32 }* @Global, i64 0, i32 0), align 4
161 ; CHECK-NEXT: ret i32 0
164 call void @external(i32* %X)
166 %Y = load i32, i32* %X
167 store i32 5, i32* getelementptr ({ i32 }, { i32 }* @Global, i64 0, i32 0)
168 %REMOVE = load i32, i32* %X
169 %retval = sub i32 %Y, %REMOVE
176 ; These two index expressions are different, this cannot be CSE'd.
177 define i16 @zext_sext_confusion(i16* %row2col, i5 %j) nounwind{
178 ; CHECK-LABEL: @zext_sext_confusion(
180 ; CHECK-NEXT: [[SUM5_CAST:%.*]] = zext i5 [[J:%.*]] to i64
181 ; CHECK-NEXT: [[P1:%.*]] = getelementptr i16, i16* [[ROW2COL:%.*]], i64 [[SUM5_CAST]]
182 ; CHECK-NEXT: [[ROW2COL_LOAD_1_2:%.*]] = load i16, i16* [[P1]], align 1
183 ; CHECK-NEXT: [[SUM13_CAST31:%.*]] = sext i5 [[J]] to i6
184 ; CHECK-NEXT: [[SUM13_CAST:%.*]] = zext i6 [[SUM13_CAST31]] to i64
185 ; CHECK-NEXT: [[P2:%.*]] = getelementptr i16, i16* [[ROW2COL]], i64 [[SUM13_CAST]]
186 ; CHECK-NEXT: [[ROW2COL_LOAD_1_6:%.*]] = load i16, i16* [[P2]], align 1
187 ; CHECK-NEXT: [[DOTRET:%.*]] = sub i16 [[ROW2COL_LOAD_1_6]], [[ROW2COL_LOAD_1_2]]
188 ; CHECK-NEXT: ret i16 [[DOTRET]]
191 %sum5.cast = zext i5 %j to i64 ; <i64> [#uses=1]
192 %P1 = getelementptr i16, i16* %row2col, i64 %sum5.cast
193 %row2col.load.1.2 = load i16, i16* %P1, align 1 ; <i16> [#uses=1]
195 %sum13.cast31 = sext i5 %j to i6 ; <i6> [#uses=1]
196 %sum13.cast = zext i6 %sum13.cast31 to i64 ; <i64> [#uses=1]
197 %P2 = getelementptr i16, i16* %row2col, i64 %sum13.cast
198 %row2col.load.1.6 = load i16, i16* %P2, align 1 ; <i16> [#uses=1]
200 %.ret = sub i16 %row2col.load.1.6, %row2col.load.1.2 ; <i16> [#uses=1]