1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32* @test1a(i32* %p, i32* %q) {
5 ; CHECK-LABEL: @test1a(
6 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32* [[P:%.*]], [[Q:%.*]]
7 ; CHECK-NEXT: [[SELECT_V:%.*]] = select i1 [[CMP]], i32* [[P]], i32* [[Q]]
8 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr i32, i32* [[SELECT_V]], i64 4
9 ; CHECK-NEXT: ret i32* [[SELECT]]
11 %gep1 = getelementptr i32, i32* %p, i64 4
12 %gep2 = getelementptr i32, i32* %q, i64 4
13 %cmp = icmp ugt i32* %p, %q
14 %select = select i1 %cmp, i32* %gep1, i32* %gep2
18 define i32* @test1b(i32* %p, i32* %q) {
19 ; CHECK-LABEL: @test1b(
20 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32* [[P:%.*]], [[Q:%.*]]
21 ; CHECK-NEXT: [[SELECT_V:%.*]] = select i1 [[CMP]], i32* [[P]], i32* [[Q]]
22 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr i32, i32* [[SELECT_V]], i64 4
23 ; CHECK-NEXT: ret i32* [[SELECT]]
25 %gep1 = getelementptr inbounds i32, i32* %p, i64 4
26 %gep2 = getelementptr i32, i32* %q, i64 4
27 %cmp = icmp ugt i32* %p, %q
28 %select = select i1 %cmp, i32* %gep1, i32* %gep2
32 define i32* @test1c(i32* %p, i32* %q) {
33 ; CHECK-LABEL: @test1c(
34 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32* [[P:%.*]], [[Q:%.*]]
35 ; CHECK-NEXT: [[SELECT_V:%.*]] = select i1 [[CMP]], i32* [[P]], i32* [[Q]]
36 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr i32, i32* [[SELECT_V]], i64 4
37 ; CHECK-NEXT: ret i32* [[SELECT]]
39 %gep1 = getelementptr i32, i32* %p, i64 4
40 %gep2 = getelementptr inbounds i32, i32* %q, i64 4
41 %cmp = icmp ugt i32* %p, %q
42 %select = select i1 %cmp, i32* %gep1, i32* %gep2
46 define i32* @test1d(i32* %p, i32* %q) {
47 ; CHECK-LABEL: @test1d(
48 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32* [[P:%.*]], [[Q:%.*]]
49 ; CHECK-NEXT: [[SELECT_V:%.*]] = select i1 [[CMP]], i32* [[P]], i32* [[Q]]
50 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[SELECT_V]], i64 4
51 ; CHECK-NEXT: ret i32* [[SELECT]]
53 %gep1 = getelementptr inbounds i32, i32* %p, i64 4
54 %gep2 = getelementptr inbounds i32, i32* %q, i64 4
55 %cmp = icmp ugt i32* %p, %q
56 %select = select i1 %cmp, i32* %gep1, i32* %gep2
60 define i32* @test2(i32* %p, i64 %x, i64 %y) {
61 ; CHECK-LABEL: @test2(
62 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
63 ; CHECK-NEXT: [[SELECT_V:%.*]] = select i1 [[CMP]], i64 [[X]], i64 [[Y]]
64 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[SELECT_V]]
65 ; CHECK-NEXT: ret i32* [[SELECT]]
67 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
68 %gep2 = getelementptr inbounds i32, i32* %p, i64 %y
69 %cmp = icmp ugt i64 %x, %y
70 %select = select i1 %cmp, i32* %gep1, i32* %gep2
75 define i32* @test2a(i32* %p, i64 %x, i64 %y) {
76 ; CHECK-LABEL: @test2a(
77 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
78 ; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
79 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
80 ; CHECK-NEXT: ret i32* [[SELECT]]
82 %gep = getelementptr inbounds i32, i32* %p, i64 %x
83 %cmp = icmp ugt i64 %x, %y
84 %select = select i1 %cmp, i32* %gep, i32* %p
89 define i32* @test2b(i32* %p, i64 %x, i64 %y) {
90 ; CHECK-LABEL: @test2b(
91 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
92 ; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 0, i64 [[X]]
93 ; CHECK-NEXT: [[SELECT:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
94 ; CHECK-NEXT: ret i32* [[SELECT]]
96 %gep = getelementptr inbounds i32, i32* %p, i64 %x
97 %cmp = icmp ugt i64 %x, %y
98 %select = select i1 %cmp, i32* %p, i32* %gep
103 define i32* @test2c(i32* %p, i64 %x, i64 %y) {
104 ; CHECK-LABEL: @test2c(
105 ; CHECK-NEXT: [[ICMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
106 ; CHECK-NEXT: [[SEL_IDX:%.*]] = select i1 [[ICMP]], i64 0, i64 6
107 ; CHECK-NEXT: [[SEL_IDX1:%.*]] = add i64 [[SEL_IDX]], [[X]]
108 ; CHECK-NEXT: [[SEL:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SEL_IDX1]]
109 ; CHECK-NEXT: ret i32* [[SEL]]
111 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
112 %gep2 = getelementptr inbounds i32, i32* %gep1, i64 6
113 %icmp = icmp ugt i64 %x, %y
114 %sel = select i1 %icmp, i32* %gep1, i32* %gep2
119 define i32* @test2d(i32* %p, i64 %x, i64 %y) {
120 ; CHECK-LABEL: @test2d(
121 ; CHECK-NEXT: [[ICMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
122 ; CHECK-NEXT: [[SEL_IDX:%.*]] = select i1 [[ICMP]], i64 6, i64 0
123 ; CHECK-NEXT: [[SEL_IDX1:%.*]] = add i64 [[SEL_IDX]], [[X]]
124 ; CHECK-NEXT: [[SEL:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SEL_IDX1]]
125 ; CHECK-NEXT: ret i32* [[SEL]]
127 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
128 %gep2 = getelementptr inbounds i32, i32* %gep1, i64 6
129 %icmp = icmp ugt i64 %x, %y
130 %sel = select i1 %icmp, i32* %gep2, i32* %gep1
134 ; Three (or more) operand GEPs are currently expected to not be optimised,
135 ; though they could be in principle.
137 define i32* @test3a([4 x i32]* %p, i64 %x, i64 %y) {
138 ; CHECK-LABEL: @test3a(
139 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[P:%.*]], i64 2, i64 [[X:%.*]]
140 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[P]], i64 2, i64 [[Y:%.*]]
141 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
142 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP1]], i32* [[GEP2]]
143 ; CHECK-NEXT: ret i32* [[SELECT]]
145 %gep1 = getelementptr inbounds [4 x i32], [4 x i32]* %p, i64 2, i64 %x
146 %gep2 = getelementptr inbounds [4 x i32], [4 x i32]* %p, i64 2, i64 %y
147 %cmp = icmp ugt i64 %x, %y
148 %select = select i1 %cmp, i32* %gep1, i32* %gep2
152 define i32* @test3b([4 x i32]* %p, [4 x i32]* %q, i64 %x, i64 %y) {
153 ; CHECK-LABEL: @test3b(
154 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[P:%.*]], i64 2, i64 [[X:%.*]]
155 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[Q:%.*]], i64 2, i64 [[X]]
156 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
157 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP1]], i32* [[GEP2]]
158 ; CHECK-NEXT: ret i32* [[SELECT]]
160 %gep1 = getelementptr inbounds [4 x i32], [4 x i32]* %p, i64 2, i64 %x
161 %gep2 = getelementptr inbounds [4 x i32], [4 x i32]* %q, i64 2, i64 %x
162 %cmp = icmp ugt i64 %x, %y
163 %select = select i1 %cmp, i32* %gep1, i32* %gep2
167 define i32* @test3c([4 x i32]* %p, i32* %q, i64 %x, i64 %y) {
168 ; CHECK-LABEL: @test3c(
169 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[P:%.*]], i64 [[X:%.*]], i64 2
170 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i32, i32* [[Q:%.*]], i64 [[X]]
171 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
172 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP1]], i32* [[GEP2]]
173 ; CHECK-NEXT: ret i32* [[SELECT]]
175 %gep1 = getelementptr inbounds [4 x i32], [4 x i32]* %p, i64 %x, i64 2
176 %gep2 = getelementptr inbounds i32, i32* %q, i64 %x
177 %cmp = icmp ugt i64 %x, %y
178 %select = select i1 %cmp, i32* %gep1, i32* %gep2
182 define i32* @test3d(i32* %p, [4 x i32]* %q, i64 %x, i64 %y) {
183 ; CHECK-LABEL: @test3d(
184 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[X:%.*]]
185 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [4 x i32], [4 x i32]* [[Q:%.*]], i64 [[X]], i64 2
186 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
187 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP1]], i32* [[GEP2]]
188 ; CHECK-NEXT: ret i32* [[SELECT]]
190 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
191 %gep2 = getelementptr inbounds [4 x i32], [4 x i32]* %q, i64 %x, i64 2
192 %cmp = icmp ugt i64 %x, %y
193 %select = select i1 %cmp, i32* %gep1, i32* %gep2
197 ; Shouldn't be optimised as it would mean introducing an extra select
199 define i32* @test4(i32* %p, i32* %q, i64 %x, i64 %y) {
200 ; CHECK-LABEL: @test4(
201 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[X:%.*]]
202 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i32, i32* [[Q:%.*]], i64 [[Y:%.*]]
203 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
204 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP1]], i32* [[GEP2]]
205 ; CHECK-NEXT: ret i32* [[SELECT]]
207 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
208 %gep2 = getelementptr inbounds i32, i32* %q, i64 %y
209 %cmp = icmp ugt i64 %x, %y
210 %select = select i1 %cmp, i32* %gep1, i32* %gep2
214 ; We cannot create a select with a vector condition but scalar operands.
216 define <2 x i64*> @test5(i64* %p1, i64* %p2, <2 x i64> %idx, <2 x i1> %cc) {
217 ; CHECK-LABEL: @test5(
218 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i64, i64* [[P1:%.*]], <2 x i64> [[IDX:%.*]]
219 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i64, i64* [[P2:%.*]], <2 x i64> [[IDX]]
220 ; CHECK-NEXT: [[SELECT:%.*]] = select <2 x i1> [[CC:%.*]], <2 x i64*> [[GEP1]], <2 x i64*> [[GEP2]]
221 ; CHECK-NEXT: ret <2 x i64*> [[SELECT]]
223 %gep1 = getelementptr i64, i64* %p1, <2 x i64> %idx
224 %gep2 = getelementptr i64, i64* %p2, <2 x i64> %idx
225 %select = select <2 x i1> %cc, <2 x i64*> %gep1, <2 x i64*> %gep2
226 ret <2 x i64*> %select
229 ; PR51069 - multiple uses
230 define i32* @test6(i32* %p, i64 %x, i64 %y) {
231 ; CHECK-LABEL: @test6(
232 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[X:%.*]]
233 ; CHECK-NEXT: [[ICMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
234 ; CHECK-NEXT: [[SEL_IDX:%.*]] = select i1 [[ICMP]], i64 [[Y]], i64 0
235 ; CHECK-NEXT: [[SEL:%.*]] = getelementptr i32, i32* [[GEP1]], i64 [[SEL_IDX]]
236 ; CHECK-NEXT: call void @use_i32p(i32* [[GEP1]])
237 ; CHECK-NEXT: ret i32* [[SEL]]
239 %gep1 = getelementptr inbounds i32, i32* %p, i64 %x
240 %gep2 = getelementptr inbounds i32, i32* %gep1, i64 %y
241 %icmp = icmp ugt i64 %x, %y
242 %sel = select i1 %icmp, i32* %gep2, i32* %gep1
243 call void @use_i32p(i32* %gep1)
246 declare void @use_i32p(i32*)