1 //===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/PatternMatch.h"
10 #include "llvm/ADT/APSInt.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/NoFolder.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/Type.h"
26 #include "gtest/gtest.h"
27
28 using namespace llvm;
29 using namespace llvm::PatternMatch;
30
31 namespace {
32
33 struct PatternMatchTest : ::testing::Test {
34 LLVMContext Ctx;
35 std::unique_ptr<Module> M;
36 Function *F;
37 BasicBlock *BB;
38 IRBuilder<NoFolder> IRB;
39
PatternMatchTest__anonf80b6d690111::PatternMatchTest40 PatternMatchTest()
41 : M(new Module("PatternMatchTestModule", Ctx)),
42 F(Function::Create(
43 FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
44 Function::ExternalLinkage, "f", M.get())),
45 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
46 };
47
TEST_F(PatternMatchTest,OneUse)48 TEST_F(PatternMatchTest, OneUse) {
49 // Build up a little tree of values:
50 //
51 // One = (1 + 2) + 42
52 // Two = One + 42
53 // Leaf = (Two + 8) + (Two + 13)
54 Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)),
55 IRB.getInt32(42));
56 Value *Two = IRB.CreateAdd(One, IRB.getInt32(42));
57 Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)),
58 IRB.CreateAdd(Two, IRB.getInt32(13)));
59 Value *V;
60
61 EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
62 EXPECT_EQ(One, V);
63
64 EXPECT_FALSE(m_OneUse(m_Value()).match(Two));
65 EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
66 }
67
TEST_F(PatternMatchTest,SpecificIntEQ)68 TEST_F(PatternMatchTest, SpecificIntEQ) {
69 Type *IntTy = IRB.getInt32Ty();
70 unsigned BitWidth = IntTy->getScalarSizeInBits();
71
72 Value *Zero = ConstantInt::get(IntTy, 0);
73 Value *One = ConstantInt::get(IntTy, 1);
74 Value *NegOne = ConstantInt::get(IntTy, -1);
75
76 EXPECT_TRUE(
77 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
78 .match(Zero));
79 EXPECT_FALSE(
80 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
81 .match(One));
82 EXPECT_FALSE(
83 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
84 .match(NegOne));
85
86 EXPECT_FALSE(
87 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
88 .match(Zero));
89 EXPECT_TRUE(
90 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
91 .match(One));
92 EXPECT_FALSE(
93 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
94 .match(NegOne));
95
96 EXPECT_FALSE(
97 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
98 .match(Zero));
99 EXPECT_FALSE(
100 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
101 .match(One));
102 EXPECT_TRUE(
103 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
104 .match(NegOne));
105 }
106
TEST_F(PatternMatchTest,SpecificIntNE)107 TEST_F(PatternMatchTest, SpecificIntNE) {
108 Type *IntTy = IRB.getInt32Ty();
109 unsigned BitWidth = IntTy->getScalarSizeInBits();
110
111 Value *Zero = ConstantInt::get(IntTy, 0);
112 Value *One = ConstantInt::get(IntTy, 1);
113 Value *NegOne = ConstantInt::get(IntTy, -1);
114
115 EXPECT_FALSE(
116 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
117 .match(Zero));
118 EXPECT_TRUE(
119 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
120 .match(One));
121 EXPECT_TRUE(
122 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
123 .match(NegOne));
124
125 EXPECT_TRUE(
126 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
127 .match(Zero));
128 EXPECT_FALSE(
129 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
130 .match(One));
131 EXPECT_TRUE(
132 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
133 .match(NegOne));
134
135 EXPECT_TRUE(
136 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
137 .match(Zero));
138 EXPECT_TRUE(
139 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
140 .match(One));
141 EXPECT_FALSE(
142 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
143 .match(NegOne));
144 }
145
TEST_F(PatternMatchTest,SpecificIntUGT)146 TEST_F(PatternMatchTest, SpecificIntUGT) {
147 Type *IntTy = IRB.getInt32Ty();
148 unsigned BitWidth = IntTy->getScalarSizeInBits();
149
150 Value *Zero = ConstantInt::get(IntTy, 0);
151 Value *One = ConstantInt::get(IntTy, 1);
152 Value *NegOne = ConstantInt::get(IntTy, -1);
153
154 EXPECT_FALSE(
155 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
156 .match(Zero));
157 EXPECT_TRUE(
158 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
159 .match(One));
160 EXPECT_TRUE(
161 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
162 .match(NegOne));
163
164 EXPECT_FALSE(
165 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
166 .match(Zero));
167 EXPECT_FALSE(
168 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
169 .match(One));
170 EXPECT_TRUE(
171 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
172 .match(NegOne));
173
174 EXPECT_FALSE(
175 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
176 .match(Zero));
177 EXPECT_FALSE(
178 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
179 .match(One));
180 EXPECT_FALSE(
181 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
182 .match(NegOne));
183 }
184
TEST_F(PatternMatchTest,SignbitZeroChecks)185 TEST_F(PatternMatchTest, SignbitZeroChecks) {
186 Type *IntTy = IRB.getInt32Ty();
187
188 Value *Zero = ConstantInt::get(IntTy, 0);
189 Value *One = ConstantInt::get(IntTy, 1);
190 Value *NegOne = ConstantInt::get(IntTy, -1);
191
192 EXPECT_TRUE(m_Negative().match(NegOne));
193 EXPECT_FALSE(m_NonNegative().match(NegOne));
194 EXPECT_FALSE(m_StrictlyPositive().match(NegOne));
195 EXPECT_TRUE(m_NonPositive().match(NegOne));
196
197 EXPECT_FALSE(m_Negative().match(Zero));
198 EXPECT_TRUE(m_NonNegative().match(Zero));
199 EXPECT_FALSE(m_StrictlyPositive().match(Zero));
200 EXPECT_TRUE(m_NonPositive().match(Zero));
201
202 EXPECT_FALSE(m_Negative().match(One));
203 EXPECT_TRUE(m_NonNegative().match(One));
204 EXPECT_TRUE(m_StrictlyPositive().match(One));
205 EXPECT_FALSE(m_NonPositive().match(One));
206 }
207
TEST_F(PatternMatchTest,SpecificIntUGE)208 TEST_F(PatternMatchTest, SpecificIntUGE) {
209 Type *IntTy = IRB.getInt32Ty();
210 unsigned BitWidth = IntTy->getScalarSizeInBits();
211
212 Value *Zero = ConstantInt::get(IntTy, 0);
213 Value *One = ConstantInt::get(IntTy, 1);
214 Value *NegOne = ConstantInt::get(IntTy, -1);
215
216 EXPECT_TRUE(
217 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
218 .match(Zero));
219 EXPECT_TRUE(
220 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
221 .match(One));
222 EXPECT_TRUE(
223 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
224 .match(NegOne));
225
226 EXPECT_FALSE(
227 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
228 .match(Zero));
229 EXPECT_TRUE(
230 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
231 .match(One));
232 EXPECT_TRUE(
233 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
234 .match(NegOne));
235
236 EXPECT_FALSE(
237 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
238 .match(Zero));
239 EXPECT_FALSE(
240 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
241 .match(One));
242 EXPECT_TRUE(
243 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
244 .match(NegOne));
245 }
246
TEST_F(PatternMatchTest,SpecificIntULT)247 TEST_F(PatternMatchTest, SpecificIntULT) {
248 Type *IntTy = IRB.getInt32Ty();
249 unsigned BitWidth = IntTy->getScalarSizeInBits();
250
251 Value *Zero = ConstantInt::get(IntTy, 0);
252 Value *One = ConstantInt::get(IntTy, 1);
253 Value *NegOne = ConstantInt::get(IntTy, -1);
254
255 EXPECT_FALSE(
256 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
257 .match(Zero));
258 EXPECT_FALSE(
259 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
260 .match(One));
261 EXPECT_FALSE(
262 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
263 .match(NegOne));
264
265 EXPECT_TRUE(
266 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
267 .match(Zero));
268 EXPECT_FALSE(
269 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
270 .match(One));
271 EXPECT_FALSE(
272 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
273 .match(NegOne));
274
275 EXPECT_TRUE(
276 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
277 .match(Zero));
278 EXPECT_TRUE(
279 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
280 .match(One));
281 EXPECT_FALSE(
282 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
283 .match(NegOne));
284 }
285
TEST_F(PatternMatchTest,SpecificIntULE)286 TEST_F(PatternMatchTest, SpecificIntULE) {
287 Type *IntTy = IRB.getInt32Ty();
288 unsigned BitWidth = IntTy->getScalarSizeInBits();
289
290 Value *Zero = ConstantInt::get(IntTy, 0);
291 Value *One = ConstantInt::get(IntTy, 1);
292 Value *NegOne = ConstantInt::get(IntTy, -1);
293
294 EXPECT_TRUE(
295 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
296 .match(Zero));
297 EXPECT_FALSE(
298 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
299 .match(One));
300 EXPECT_FALSE(
301 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
302 .match(NegOne));
303
304 EXPECT_TRUE(
305 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
306 .match(Zero));
307 EXPECT_TRUE(
308 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
309 .match(One));
310 EXPECT_FALSE(
311 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
312 .match(NegOne));
313
314 EXPECT_TRUE(
315 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
316 .match(Zero));
317 EXPECT_TRUE(
318 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
319 .match(One));
320 EXPECT_TRUE(
321 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
322 .match(NegOne));
323 }
324
TEST_F(PatternMatchTest,SpecificIntSGT)325 TEST_F(PatternMatchTest, SpecificIntSGT) {
326 Type *IntTy = IRB.getInt32Ty();
327 unsigned BitWidth = IntTy->getScalarSizeInBits();
328
329 Value *Zero = ConstantInt::get(IntTy, 0);
330 Value *One = ConstantInt::get(IntTy, 1);
331 Value *NegOne = ConstantInt::get(IntTy, -1);
332
333 EXPECT_FALSE(
334 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
335 .match(Zero));
336 EXPECT_TRUE(
337 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
338 .match(One));
339 EXPECT_FALSE(
340 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
341 .match(NegOne));
342
343 EXPECT_FALSE(
344 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
345 .match(Zero));
346 EXPECT_FALSE(
347 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
348 .match(One));
349 EXPECT_FALSE(
350 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
351 .match(NegOne));
352
353 EXPECT_TRUE(
354 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
355 .match(Zero));
356 EXPECT_TRUE(
357 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
358 .match(One));
359 EXPECT_FALSE(
360 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
361 .match(NegOne));
362 }
363
TEST_F(PatternMatchTest,SpecificIntSGE)364 TEST_F(PatternMatchTest, SpecificIntSGE) {
365 Type *IntTy = IRB.getInt32Ty();
366 unsigned BitWidth = IntTy->getScalarSizeInBits();
367
368 Value *Zero = ConstantInt::get(IntTy, 0);
369 Value *One = ConstantInt::get(IntTy, 1);
370 Value *NegOne = ConstantInt::get(IntTy, -1);
371
372 EXPECT_TRUE(
373 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
374 .match(Zero));
375 EXPECT_TRUE(
376 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
377 .match(One));
378 EXPECT_FALSE(
379 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
380 .match(NegOne));
381
382 EXPECT_FALSE(
383 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
384 .match(Zero));
385 EXPECT_TRUE(
386 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
387 .match(One));
388 EXPECT_FALSE(
389 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
390 .match(NegOne));
391
392 EXPECT_TRUE(
393 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
394 .match(Zero));
395 EXPECT_TRUE(
396 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
397 .match(One));
398 EXPECT_TRUE(
399 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
400 .match(NegOne));
401 }
402
TEST_F(PatternMatchTest,SpecificIntSLT)403 TEST_F(PatternMatchTest, SpecificIntSLT) {
404 Type *IntTy = IRB.getInt32Ty();
405 unsigned BitWidth = IntTy->getScalarSizeInBits();
406
407 Value *Zero = ConstantInt::get(IntTy, 0);
408 Value *One = ConstantInt::get(IntTy, 1);
409 Value *NegOne = ConstantInt::get(IntTy, -1);
410
411 EXPECT_FALSE(
412 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
413 .match(Zero));
414 EXPECT_FALSE(
415 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
416 .match(One));
417 EXPECT_TRUE(
418 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
419 .match(NegOne));
420
421 EXPECT_TRUE(
422 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
423 .match(Zero));
424 EXPECT_FALSE(
425 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
426 .match(One));
427 EXPECT_TRUE(
428 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
429 .match(NegOne));
430
431 EXPECT_FALSE(
432 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
433 .match(Zero));
434 EXPECT_FALSE(
435 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
436 .match(One));
437 EXPECT_FALSE(
438 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
439 .match(NegOne));
440 }
441
TEST_F(PatternMatchTest,SpecificIntSLE)442 TEST_F(PatternMatchTest, SpecificIntSLE) {
443 Type *IntTy = IRB.getInt32Ty();
444 unsigned BitWidth = IntTy->getScalarSizeInBits();
445
446 Value *Zero = ConstantInt::get(IntTy, 0);
447 Value *One = ConstantInt::get(IntTy, 1);
448 Value *NegOne = ConstantInt::get(IntTy, -1);
449
450 EXPECT_TRUE(
451 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
452 .match(Zero));
453 EXPECT_FALSE(
454 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
455 .match(One));
456 EXPECT_TRUE(
457 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
458 .match(NegOne));
459
460 EXPECT_TRUE(
461 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
462 .match(Zero));
463 EXPECT_TRUE(
464 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
465 .match(One));
466 EXPECT_TRUE(
467 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
468 .match(NegOne));
469
470 EXPECT_FALSE(
471 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
472 .match(Zero));
473 EXPECT_FALSE(
474 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
475 .match(One));
476 EXPECT_TRUE(
477 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
478 .match(NegOne));
479 }
480
TEST_F(PatternMatchTest,Unless)481 TEST_F(PatternMatchTest, Unless) {
482 Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
483
484 EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X));
485 EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X));
486
487 EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X));
488 EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X));
489
490 EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X));
491 EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X));
492
493 EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X));
494 EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
495 }
496
TEST_F(PatternMatchTest,ZExtSExtSelf)497 TEST_F(PatternMatchTest, ZExtSExtSelf) {
498 LLVMContext &Ctx = IRB.getContext();
499
500 Value *One32 = IRB.getInt32(1);
501 Value *One64Z = IRB.CreateZExt(One32, IntegerType::getInt64Ty(Ctx));
502 Value *One64S = IRB.CreateSExt(One32, IntegerType::getInt64Ty(Ctx));
503
504 EXPECT_TRUE(m_One().match(One32));
505 EXPECT_FALSE(m_One().match(One64Z));
506 EXPECT_FALSE(m_One().match(One64S));
507
508 EXPECT_FALSE(m_ZExt(m_One()).match(One32));
509 EXPECT_TRUE(m_ZExt(m_One()).match(One64Z));
510 EXPECT_FALSE(m_ZExt(m_One()).match(One64S));
511
512 EXPECT_FALSE(m_SExt(m_One()).match(One32));
513 EXPECT_FALSE(m_SExt(m_One()).match(One64Z));
514 EXPECT_TRUE(m_SExt(m_One()).match(One64S));
515
516 EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One32));
517 EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One64Z));
518 EXPECT_FALSE(m_ZExtOrSelf(m_One()).match(One64S));
519
520 EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One32));
521 EXPECT_FALSE(m_SExtOrSelf(m_One()).match(One64Z));
522 EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One64S));
523
524 EXPECT_FALSE(m_ZExtOrSExt(m_One()).match(One32));
525 EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64Z));
526 EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64S));
527
528 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One32));
529 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64Z));
530 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64S));
531 }
532
TEST_F(PatternMatchTest,Power2)533 TEST_F(PatternMatchTest, Power2) {
534 Value *C128 = IRB.getInt32(128);
535 Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
536
537 EXPECT_TRUE(m_Power2().match(C128));
538 EXPECT_FALSE(m_Power2().match(CNeg128));
539
540 EXPECT_FALSE(m_NegatedPower2().match(C128));
541 EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
542
543 Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
544 Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
545
546 EXPECT_TRUE(m_Power2().match(CIntMin));
547 EXPECT_TRUE(m_Power2().match(CNegIntMin));
548
549 EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
550 EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
551 }
552
TEST_F(PatternMatchTest,CommutativeDeferredValue)553 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
554 Value *X = IRB.getInt32(1);
555 Value *Y = IRB.getInt32(2);
556
557 {
558 Value *tX = X;
559 EXPECT_TRUE(match(X, m_Deferred(tX)));
560 EXPECT_FALSE(match(Y, m_Deferred(tX)));
561 }
562 {
563 const Value *tX = X;
564 EXPECT_TRUE(match(X, m_Deferred(tX)));
565 EXPECT_FALSE(match(Y, m_Deferred(tX)));
566 }
567 {
568 Value *const tX = X;
569 EXPECT_TRUE(match(X, m_Deferred(tX)));
570 EXPECT_FALSE(match(Y, m_Deferred(tX)));
571 }
572 {
573 const Value *const tX = X;
574 EXPECT_TRUE(match(X, m_Deferred(tX)));
575 EXPECT_FALSE(match(Y, m_Deferred(tX)));
576 }
577
578 {
579 Value *tX = nullptr;
580 EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
581 EXPECT_EQ(tX, X);
582 }
583 {
584 Value *tX = nullptr;
585 EXPECT_FALSE(
586 match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
587 }
588
589 auto checkMatch = [X, Y](Value *Pattern) {
590 Value *tX = nullptr, *tY = nullptr;
591 EXPECT_TRUE(match(
592 Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
593 EXPECT_EQ(tX, X);
594 EXPECT_EQ(tY, Y);
595 };
596
597 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
598 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
599 checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
600 checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
601 }
602
TEST_F(PatternMatchTest,FloatingPointOrderedMin)603 TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
604 Type *FltTy = IRB.getFloatTy();
605 Value *L = ConstantFP::get(FltTy, 1.0);
606 Value *R = ConstantFP::get(FltTy, 2.0);
607 Value *MatchL, *MatchR;
608
609 // Test OLT.
610 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
611 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
612 EXPECT_EQ(L, MatchL);
613 EXPECT_EQ(R, MatchR);
614
615 // Test OLE.
616 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
617 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
618 EXPECT_EQ(L, MatchL);
619 EXPECT_EQ(R, MatchR);
620
621 // Test no match on OGE.
622 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
623 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
624
625 // Test no match on OGT.
626 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
627 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
628
629 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
630 // %cmp = fcmp oge L, R
631 // %min = select %cmp R, L
632 // Given L == NaN
633 // the above is expanded to %cmp == false ==> %min = L
634 // which is true for UnordFMin, not OrdFMin, so test that:
635
636 // [OU]GE with inverted select.
637 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
638 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
639 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
640 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
641 EXPECT_EQ(L, MatchL);
642 EXPECT_EQ(R, MatchR);
643
644 // [OU]GT with inverted select.
645 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
646 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
647 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
648 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
649 EXPECT_EQ(L, MatchL);
650 EXPECT_EQ(R, MatchR);
651 }
652
TEST_F(PatternMatchTest,FloatingPointOrderedMax)653 TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
654 Type *FltTy = IRB.getFloatTy();
655 Value *L = ConstantFP::get(FltTy, 1.0);
656 Value *R = ConstantFP::get(FltTy, 2.0);
657 Value *MatchL, *MatchR;
658
659 // Test OGT.
660 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
661 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
662 EXPECT_EQ(L, MatchL);
663 EXPECT_EQ(R, MatchR);
664
665 // Test OGE.
666 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
667 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
668 EXPECT_EQ(L, MatchL);
669 EXPECT_EQ(R, MatchR);
670
671 // Test no match on OLE.
672 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
673 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
674
675 // Test no match on OLT.
676 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
677 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
678
679
680 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
681 // %cmp = fcmp ole L, R
682 // %max = select %cmp, R, L
683 // Given L == NaN,
684 // the above is expanded to %cmp == false ==> %max == L
685 // which is true for UnordFMax, not OrdFMax, so test that:
686
687 // [OU]LE with inverted select.
688 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
689 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
690 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
691 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
692 EXPECT_EQ(L, MatchL);
693 EXPECT_EQ(R, MatchR);
694
695 // [OUT]LT with inverted select.
696 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
697 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
698 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
699 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
700 EXPECT_EQ(L, MatchL);
701 EXPECT_EQ(R, MatchR);
702 }
703
TEST_F(PatternMatchTest,FloatingPointUnorderedMin)704 TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
705 Type *FltTy = IRB.getFloatTy();
706 Value *L = ConstantFP::get(FltTy, 1.0);
707 Value *R = ConstantFP::get(FltTy, 2.0);
708 Value *MatchL, *MatchR;
709
710 // Test ULT.
711 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
712 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
713 EXPECT_EQ(L, MatchL);
714 EXPECT_EQ(R, MatchR);
715
716 // Test ULE.
717 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
718 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
719 EXPECT_EQ(L, MatchL);
720 EXPECT_EQ(R, MatchR);
721
722 // Test no match on UGE.
723 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
724 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
725
726 // Test no match on UGT.
727 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
728 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
729
730 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
731 // %cmp = fcmp uge L, R
732 // %min = select %cmp R, L
733 // Given L == NaN
734 // the above is expanded to %cmp == true ==> %min = R
735 // which is true for OrdFMin, not UnordFMin, so test that:
736
737 // [UO]GE with inverted select.
738 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
739 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
740 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
741 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
742 EXPECT_EQ(L, MatchL);
743 EXPECT_EQ(R, MatchR);
744
745 // [UO]GT with inverted select.
746 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
747 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
748 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
749 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
750 EXPECT_EQ(L, MatchL);
751 EXPECT_EQ(R, MatchR);
752 }
753
TEST_F(PatternMatchTest,FloatingPointUnorderedMax)754 TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
755 Type *FltTy = IRB.getFloatTy();
756 Value *L = ConstantFP::get(FltTy, 1.0);
757 Value *R = ConstantFP::get(FltTy, 2.0);
758 Value *MatchL, *MatchR;
759
760 // Test UGT.
761 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
762 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
763 EXPECT_EQ(L, MatchL);
764 EXPECT_EQ(R, MatchR);
765
766 // Test UGE.
767 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
768 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
769 EXPECT_EQ(L, MatchL);
770 EXPECT_EQ(R, MatchR);
771
772 // Test no match on ULE.
773 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
774 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
775
776 // Test no match on ULT.
777 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
778 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
779
780 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
781 // %cmp = fcmp ule L, R
782 // %max = select %cmp R, L
783 // Given L == NaN
784 // the above is expanded to %cmp == true ==> %max = R
785 // which is true for OrdFMax, not UnordFMax, so test that:
786
787 // [UO]LE with inverted select.
788 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
789 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
790 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
791 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
792 EXPECT_EQ(L, MatchL);
793 EXPECT_EQ(R, MatchR);
794
795 // [UO]LT with inverted select.
796 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
797 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
798 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
799 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
800 EXPECT_EQ(L, MatchL);
801 EXPECT_EQ(R, MatchR);
802 }
803
TEST_F(PatternMatchTest,OverflowingBinOps)804 TEST_F(PatternMatchTest, OverflowingBinOps) {
805 Value *L = IRB.getInt32(1);
806 Value *R = IRB.getInt32(2);
807 Value *MatchL, *MatchR;
808
809 EXPECT_TRUE(
810 m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
811 EXPECT_EQ(L, MatchL);
812 EXPECT_EQ(R, MatchR);
813 MatchL = MatchR = nullptr;
814 EXPECT_TRUE(
815 m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
816 EXPECT_EQ(L, MatchL);
817 EXPECT_EQ(R, MatchR);
818 MatchL = MatchR = nullptr;
819 EXPECT_TRUE(
820 m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
821 EXPECT_EQ(L, MatchL);
822 EXPECT_EQ(R, MatchR);
823 MatchL = MatchR = nullptr;
824 EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
825 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
826 EXPECT_EQ(L, MatchL);
827 EXPECT_EQ(R, MatchR);
828
829 EXPECT_TRUE(
830 m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
831 EXPECT_EQ(L, MatchL);
832 EXPECT_EQ(R, MatchR);
833 MatchL = MatchR = nullptr;
834 EXPECT_TRUE(
835 m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
836 EXPECT_EQ(L, MatchL);
837 EXPECT_EQ(R, MatchR);
838 MatchL = MatchR = nullptr;
839 EXPECT_TRUE(
840 m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
841 EXPECT_EQ(L, MatchL);
842 EXPECT_EQ(R, MatchR);
843 MatchL = MatchR = nullptr;
844 EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
845 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
846 EXPECT_EQ(L, MatchL);
847 EXPECT_EQ(R, MatchR);
848
849 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
850 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
851 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
852 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
853 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
854 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
855 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
856 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
857 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
858 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
859 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
860 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
861 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
862
863 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
864 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
865 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
866 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
867 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
868 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
869 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
870 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
871 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
872 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
873 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
874 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
875 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
876 }
877
TEST_F(PatternMatchTest,LoadStoreOps)878 TEST_F(PatternMatchTest, LoadStoreOps) {
879 // Create this load/store sequence:
880 //
881 // %p = alloca i32*
882 // %0 = load i32*, i32** %p
883 // store i32 42, i32* %0
884
885 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
886 Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
887 Value *FourtyTwo = IRB.getInt32(42);
888 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
889 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
890
891 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
892 EXPECT_EQ(Alloca, MatchLoad);
893
894 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
895
896 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
897
898 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
899 .match(StoreInst));
900 EXPECT_EQ(FourtyTwo, MatchStoreVal);
901 EXPECT_EQ(Alloca, MatchStorePointer);
902
903 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
904 .match(Alloca));
905
906 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
907 .match(StoreInst));
908 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
909 .match(StoreInst));
910 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
911 .match(StoreInst));
912 }
913
TEST_F(PatternMatchTest,VectorOps)914 TEST_F(PatternMatchTest, VectorOps) {
915 // Build up small tree of vector operations
916 //
917 // Val = 0 + 1
918 // Val2 = Val + 3
919 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
920 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
921 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
922 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
923 //
924 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
925 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
926 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
927 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
928 //
929 // SP1 = VectorSplat(2, i8 2)
930 // SP2 = VectorSplat(2, i8 %Val)
931 Type *VecTy = FixedVectorType::get(IRB.getInt8Ty(), 2);
932 Type *i32 = IRB.getInt32Ty();
933 Type *i32VecTy = FixedVectorType::get(i32, 2);
934
935 Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
936 Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
937
938 SmallVector<Constant *, 2> VecElemIdxs;
939 VecElemIdxs.push_back(ConstantInt::get(i32, 0));
940 VecElemIdxs.push_back(ConstantInt::get(i32, 2));
941 auto *IdxVec = ConstantVector::get(VecElemIdxs);
942
943 Value *UndefVec = UndefValue::get(VecTy);
944 Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
945 Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
946 Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
947 Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
948
949 Value *EX1 = IRB.CreateExtractElement(VI4, Val);
950 Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
951 Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
952
953 Value *Zero = ConstantAggregateZero::get(i32VecTy);
954 Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero);
955 Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
956 Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero);
957 Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero);
958
959 Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
960 Value *SP2 = IRB.CreateVectorSplat(2, Val);
961
962 Value *A = nullptr, *B = nullptr, *C = nullptr;
963
964 // Test matching insertelement
965 EXPECT_TRUE(match(VI1, m_InsertElt(m_Value(), m_Value(), m_Value())));
966 EXPECT_TRUE(
967 match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt())));
968 EXPECT_TRUE(
969 match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero())));
970 EXPECT_TRUE(
971 match(VI1, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero())));
972 EXPECT_TRUE(match(VI2, m_InsertElt(m_Value(), m_Value(), m_Value())));
973 EXPECT_FALSE(
974 match(VI2, m_InsertElt(m_Value(), m_Value(), m_ConstantInt())));
975 EXPECT_FALSE(
976 match(VI2, m_InsertElt(m_Value(), m_ConstantInt(), m_Value())));
977 EXPECT_FALSE(match(VI2, m_InsertElt(m_Constant(), m_Value(), m_Value())));
978 EXPECT_TRUE(match(VI3, m_InsertElt(m_Value(A), m_Value(B), m_Value(C))));
979 EXPECT_TRUE(A == VI1);
980 EXPECT_TRUE(B == Val2);
981 EXPECT_TRUE(isa<ConstantInt>(C));
982 A = B = C = nullptr; // reset
983
984 // Test matching extractelement
985 EXPECT_TRUE(match(EX1, m_ExtractElt(m_Value(A), m_Value(B))));
986 EXPECT_TRUE(A == VI4);
987 EXPECT_TRUE(B == Val);
988 A = B = C = nullptr; // reset
989 EXPECT_FALSE(match(EX1, m_ExtractElt(m_Value(), m_ConstantInt())));
990 EXPECT_TRUE(match(EX2, m_ExtractElt(m_Value(), m_ConstantInt())));
991 EXPECT_TRUE(match(EX3, m_ExtractElt(m_Constant(), m_ConstantInt())));
992
993 // Test matching shufflevector
994 ArrayRef<int> Mask;
995 EXPECT_TRUE(match(SI1, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask())));
996 EXPECT_TRUE(match(SI2, m_Shuffle(m_Value(A), m_Value(B), m_Mask(Mask))));
997 EXPECT_TRUE(A == VI3);
998 EXPECT_TRUE(B == VI4);
999 A = B = C = nullptr; // reset
1000
1001 // Test matching the vector splat pattern
1002 EXPECT_TRUE(match(
1003 SI1,
1004 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()),
1005 m_Undef(), m_ZeroMask())));
1006 EXPECT_FALSE(match(
1007 SI3, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1008 m_Undef(), m_ZeroMask())));
1009 EXPECT_FALSE(match(
1010 SI4, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1011 m_Undef(), m_ZeroMask())));
1012 EXPECT_TRUE(match(
1013 SP1,
1014 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()),
1015 m_Undef(), m_ZeroMask())));
1016 EXPECT_TRUE(match(
1017 SP2, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A), m_Zero()),
1018 m_Undef(), m_ZeroMask())));
1019 EXPECT_TRUE(A == Val);
1020 }
1021
TEST_F(PatternMatchTest,VectorUndefInt)1022 TEST_F(PatternMatchTest, VectorUndefInt) {
1023 Type *ScalarTy = IRB.getInt8Ty();
1024 Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1025 Constant *ScalarUndef = UndefValue::get(ScalarTy);
1026 Constant *VectorUndef = UndefValue::get(VectorTy);
1027 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1028 Constant *VectorZero = Constant::getNullValue(VectorTy);
1029
1030 SmallVector<Constant *, 4> Elems;
1031 Elems.push_back(ScalarUndef);
1032 Elems.push_back(ScalarZero);
1033 Elems.push_back(ScalarUndef);
1034 Elems.push_back(ScalarZero);
1035 Constant *VectorZeroUndef = ConstantVector::get(Elems);
1036
1037 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1038 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1039 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1040 EXPECT_FALSE(match(VectorZero, m_Undef()));
1041 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1042
1043 EXPECT_FALSE(match(ScalarUndef, m_Zero()));
1044 EXPECT_FALSE(match(VectorUndef, m_Zero()));
1045 EXPECT_TRUE(match(ScalarZero, m_Zero()));
1046 EXPECT_TRUE(match(VectorZero, m_Zero()));
1047 EXPECT_TRUE(match(VectorZeroUndef, m_Zero()));
1048
1049 const APInt *C;
1050 // Regardless of whether undefs are allowed,
1051 // a fully undef constant does not match.
1052 EXPECT_FALSE(match(ScalarUndef, m_APInt(C)));
1053 EXPECT_FALSE(match(ScalarUndef, m_APIntForbidUndef(C)));
1054 EXPECT_FALSE(match(ScalarUndef, m_APIntAllowUndef(C)));
1055 EXPECT_FALSE(match(VectorUndef, m_APInt(C)));
1056 EXPECT_FALSE(match(VectorUndef, m_APIntForbidUndef(C)));
1057 EXPECT_FALSE(match(VectorUndef, m_APIntAllowUndef(C)));
1058
1059 // We can always match simple constants and simple splats.
1060 C = nullptr;
1061 EXPECT_TRUE(match(ScalarZero, m_APInt(C)));
1062 EXPECT_TRUE(C->isNullValue());
1063 C = nullptr;
1064 EXPECT_TRUE(match(ScalarZero, m_APIntForbidUndef(C)));
1065 EXPECT_TRUE(C->isNullValue());
1066 C = nullptr;
1067 EXPECT_TRUE(match(ScalarZero, m_APIntAllowUndef(C)));
1068 EXPECT_TRUE(C->isNullValue());
1069 C = nullptr;
1070 EXPECT_TRUE(match(VectorZero, m_APInt(C)));
1071 EXPECT_TRUE(C->isNullValue());
1072 C = nullptr;
1073 EXPECT_TRUE(match(VectorZero, m_APIntForbidUndef(C)));
1074 EXPECT_TRUE(C->isNullValue());
1075 C = nullptr;
1076 EXPECT_TRUE(match(VectorZero, m_APIntAllowUndef(C)));
1077 EXPECT_TRUE(C->isNullValue());
1078
1079 // Whether splats with undef can be matched depends on the matcher.
1080 EXPECT_FALSE(match(VectorZeroUndef, m_APInt(C)));
1081 EXPECT_FALSE(match(VectorZeroUndef, m_APIntForbidUndef(C)));
1082 C = nullptr;
1083 EXPECT_TRUE(match(VectorZeroUndef, m_APIntAllowUndef(C)));
1084 EXPECT_TRUE(C->isNullValue());
1085 }
1086
TEST_F(PatternMatchTest,VectorUndefFloat)1087 TEST_F(PatternMatchTest, VectorUndefFloat) {
1088 Type *ScalarTy = IRB.getFloatTy();
1089 Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1090 Constant *ScalarUndef = UndefValue::get(ScalarTy);
1091 Constant *VectorUndef = UndefValue::get(VectorTy);
1092 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1093 Constant *VectorZero = Constant::getNullValue(VectorTy);
1094 Constant *ScalarPosInf = ConstantFP::getInfinity(ScalarTy, false);
1095 Constant *ScalarNegInf = ConstantFP::getInfinity(ScalarTy, true);
1096
1097 SmallVector<Constant *, 4> Elems;
1098 Elems.push_back(ScalarUndef);
1099 Elems.push_back(ScalarZero);
1100 Elems.push_back(ScalarUndef);
1101 Elems.push_back(ScalarZero);
1102 Constant *VectorZeroUndef = ConstantVector::get(Elems);
1103
1104 SmallVector<Constant *, 4> InfElems;
1105 InfElems.push_back(ScalarPosInf);
1106 InfElems.push_back(ScalarNegInf);
1107 InfElems.push_back(ScalarUndef);
1108 InfElems.push_back(ScalarPosInf);
1109 Constant *VectorInfUndef = ConstantVector::get(InfElems);
1110
1111 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1112 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1113 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1114 EXPECT_FALSE(match(VectorZero, m_Undef()));
1115 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1116
1117 EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1118 EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1119 EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1120 EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1121 EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
1122
1123 EXPECT_FALSE(match(ScalarUndef, m_Inf()));
1124 EXPECT_FALSE(match(VectorUndef, m_Inf()));
1125 EXPECT_FALSE(match(VectorZeroUndef, m_Inf()));
1126 EXPECT_TRUE(match(ScalarPosInf, m_Inf()));
1127 EXPECT_TRUE(match(ScalarNegInf, m_Inf()));
1128 EXPECT_TRUE(match(VectorInfUndef, m_Inf()));
1129
1130 const APFloat *C;
1131 // Regardless of whether undefs are allowed,
1132 // a fully undef constant does not match.
1133 EXPECT_FALSE(match(ScalarUndef, m_APFloat(C)));
1134 EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidUndef(C)));
1135 EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowUndef(C)));
1136 EXPECT_FALSE(match(VectorUndef, m_APFloat(C)));
1137 EXPECT_FALSE(match(VectorUndef, m_APFloatForbidUndef(C)));
1138 EXPECT_FALSE(match(VectorUndef, m_APFloatAllowUndef(C)));
1139
1140 // We can always match simple constants and simple splats.
1141 C = nullptr;
1142 EXPECT_TRUE(match(ScalarZero, m_APFloat(C)));
1143 EXPECT_TRUE(C->isZero());
1144 C = nullptr;
1145 EXPECT_TRUE(match(ScalarZero, m_APFloatForbidUndef(C)));
1146 EXPECT_TRUE(C->isZero());
1147 C = nullptr;
1148 EXPECT_TRUE(match(ScalarZero, m_APFloatAllowUndef(C)));
1149 EXPECT_TRUE(C->isZero());
1150 C = nullptr;
1151 EXPECT_TRUE(match(VectorZero, m_APFloat(C)));
1152 EXPECT_TRUE(C->isZero());
1153 C = nullptr;
1154 EXPECT_TRUE(match(VectorZero, m_APFloatForbidUndef(C)));
1155 EXPECT_TRUE(C->isZero());
1156 C = nullptr;
1157 EXPECT_TRUE(match(VectorZero, m_APFloatAllowUndef(C)));
1158 EXPECT_TRUE(C->isZero());
1159
1160 // Whether splats with undef can be matched depends on the matcher.
1161 EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C)));
1162 EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidUndef(C)));
1163 C = nullptr;
1164 EXPECT_TRUE(match(VectorZeroUndef, m_APFloatAllowUndef(C)));
1165 EXPECT_TRUE(C->isZero());
1166 }
1167
TEST_F(PatternMatchTest,FloatingPointFNeg)1168 TEST_F(PatternMatchTest, FloatingPointFNeg) {
1169 Type *FltTy = IRB.getFloatTy();
1170 Value *One = ConstantFP::get(FltTy, 1.0);
1171 Value *Z = ConstantFP::get(FltTy, 0.0);
1172 Value *NZ = ConstantFP::get(FltTy, -0.0);
1173 Value *V = IRB.CreateFNeg(One);
1174 Value *V1 = IRB.CreateFSub(NZ, One);
1175 Value *V2 = IRB.CreateFSub(Z, One);
1176 Value *V3 = IRB.CreateFAdd(NZ, One);
1177 Value *Match;
1178
1179 // Test FNeg(1.0)
1180 EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1181 EXPECT_EQ(One, Match);
1182
1183 // Test FSub(-0.0, 1.0)
1184 EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1185 EXPECT_EQ(One, Match);
1186
1187 // Test FSub(0.0, 1.0)
1188 EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1189 cast<Instruction>(V2)->setHasNoSignedZeros(true);
1190 EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1191 EXPECT_EQ(One, Match);
1192
1193 // Test FAdd(-0.0, 1.0)
1194 EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1195 }
1196
TEST_F(PatternMatchTest,CondBranchTest)1197 TEST_F(PatternMatchTest, CondBranchTest) {
1198 BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1199 BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1200 Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1201
1202 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1203
1204 BasicBlock *A, *B;
1205 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1206 EXPECT_EQ(TrueBB, A);
1207 EXPECT_EQ(FalseBB, B);
1208
1209 EXPECT_FALSE(
1210 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1211 EXPECT_FALSE(
1212 match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1213 EXPECT_FALSE(
1214 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1215 EXPECT_TRUE(
1216 match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1217
1218 // Check we can use m_Deferred with branches.
1219 EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1220 Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1221 A = nullptr;
1222 EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1223 }
1224
TEST_F(PatternMatchTest,WithOverflowInst)1225 TEST_F(PatternMatchTest, WithOverflowInst) {
1226 Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1227 IRB.getInt32(0), IRB.getInt32(0));
1228 Value *Add0 = IRB.CreateExtractValue(Add, 0);
1229 Value *Add1 = IRB.CreateExtractValue(Add, 1);
1230
1231 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1232 EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1233 EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1234 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1235 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1236 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1237
1238 WithOverflowInst *WOI;
1239 EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1240 EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1241 EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1242
1243 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1244 EXPECT_EQ(Add, WOI);
1245 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1246 EXPECT_EQ(Add, WOI);
1247 }
1248
TEST_F(PatternMatchTest,IntrinsicMatcher)1249 TEST_F(PatternMatchTest, IntrinsicMatcher) {
1250 Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1251 Value *Hash = IRB.getInt64(0);
1252 Value *Num = IRB.getInt32(1);
1253 Value *Index = IRB.getInt32(2);
1254 Value *Step = IRB.getInt64(3);
1255
1256 Value *Ops[] = {Name, Hash, Num, Index, Step};
1257 Module *M = BB->getParent()->getParent();
1258 Function *TheFn =
1259 Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1260
1261 Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1262
1263 // Match without capturing.
1264 EXPECT_TRUE(match(
1265 Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1266 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1267 EXPECT_FALSE(match(
1268 Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1269 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1270
1271 // Match with capturing.
1272 Value *Arg1 = nullptr;
1273 Value *Arg2 = nullptr;
1274 Value *Arg3 = nullptr;
1275 Value *Arg4 = nullptr;
1276 Value *Arg5 = nullptr;
1277 EXPECT_TRUE(
1278 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1279 m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1280 m_Value(Arg4), m_Value(Arg5))));
1281 EXPECT_EQ(Arg1, Name);
1282 EXPECT_EQ(Arg2, Hash);
1283 EXPECT_EQ(Arg3, Num);
1284 EXPECT_EQ(Arg4, Index);
1285 EXPECT_EQ(Arg5, Step);
1286
1287 // Match specific second argument.
1288 EXPECT_TRUE(
1289 match(Intrinsic5,
1290 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1291 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1292 EXPECT_FALSE(
1293 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1294 m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1295 m_Value())));
1296
1297 // Match specific third argument.
1298 EXPECT_TRUE(
1299 match(Intrinsic5,
1300 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1301 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1302 EXPECT_FALSE(
1303 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1304 m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1305 m_Value())));
1306
1307 // Match specific fourth argument.
1308 EXPECT_TRUE(
1309 match(Intrinsic5,
1310 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1311 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1312 EXPECT_FALSE(
1313 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1314 m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1315 m_Value())));
1316
1317 // Match specific fifth argument.
1318 EXPECT_TRUE(
1319 match(Intrinsic5,
1320 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1321 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1322 EXPECT_FALSE(
1323 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1324 m_Value(), m_Value(), m_Value(), m_Value(),
1325 m_SpecificInt(10))));
1326 }
1327
1328 namespace {
1329
1330 struct is_unsigned_zero_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::is_unsigned_zero_pred1331 bool isValue(const APInt &C) { return C.isNullValue(); }
1332 };
1333
1334 struct is_float_zero_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::is_float_zero_pred1335 bool isValue(const APFloat &C) { return C.isZero(); }
1336 };
1337
1338 template <typename T> struct always_true_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::always_true_pred1339 bool isValue(const T &) { return true; }
1340 };
1341
1342 template <typename T> struct always_false_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::always_false_pred1343 bool isValue(const T &) { return false; }
1344 };
1345
1346 struct is_unsigned_max_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::is_unsigned_max_pred1347 bool isValue(const APInt &C) { return C.isMaxValue(); }
1348 };
1349
1350 struct is_float_nan_pred {
isValue__anonf80b6d690111::__anonf80b6d690311::is_float_nan_pred1351 bool isValue(const APFloat &C) { return C.isNaN(); }
1352 };
1353
1354 } // namespace
1355
TEST_F(PatternMatchTest,ConstantPredicateType)1356 TEST_F(PatternMatchTest, ConstantPredicateType) {
1357
1358 // Scalar integer
1359 APInt U32Max = APInt::getAllOnesValue(32);
1360 APInt U32Zero = APInt::getNullValue(32);
1361 APInt U32DeadBeef(32, 0xDEADBEEF);
1362
1363 Type *U32Ty = Type::getInt32Ty(Ctx);
1364
1365 Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max);
1366 Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero);
1367 Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef);
1368
1369 EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1370 EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1371 EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1372 EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1373
1374 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1375 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1376 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1377 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1378
1379 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1380 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1381 EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1382 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1383
1384 // Scalar float
1385 APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle());
1386 APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle());
1387 APFloat F32Pi(3.14f);
1388
1389 Type *F32Ty = Type::getFloatTy(Ctx);
1390
1391 Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN);
1392 Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero);
1393 Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi);
1394
1395 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1396 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1397 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1398 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1399
1400 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1401 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1402 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1403 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1404
1405 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1406 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1407 EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1408 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1409
1410 ElementCount FixedEC(4, false);
1411 ElementCount ScalableEC(4, true);
1412
1413 // Vector splat
1414
1415 for (auto EC : {FixedEC, ScalableEC}) {
1416 // integer
1417
1418 Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max);
1419 Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero);
1420 Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef);
1421
1422 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1423 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1424 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1425 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1426
1427 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1428 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1429 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1430 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1431
1432 EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1433 EXPECT_FALSE(
1434 match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1435 EXPECT_TRUE(
1436 match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1437 EXPECT_FALSE(
1438 match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1439
1440 // float
1441
1442 Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN);
1443 Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero);
1444 Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi);
1445
1446 EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1447 EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1448 EXPECT_TRUE(
1449 match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1450 EXPECT_FALSE(
1451 match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1452
1453 EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1454 EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1455 EXPECT_TRUE(
1456 match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1457 EXPECT_FALSE(
1458 match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1459
1460 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1461 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1462 EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1463 EXPECT_FALSE(
1464 match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1465 }
1466
1467 // Int arbitrary vector
1468
1469 Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef});
1470 Constant *CU32Undef = UndefValue::get(U32Ty);
1471 Constant *CU32MaxWithUndef =
1472 ConstantVector::get({CU32Undef, CU32Max, CU32Undef});
1473
1474 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>()));
1475 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>()));
1476 EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>()));
1477 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>()));
1478
1479 EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>()));
1480 EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>()));
1481 EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>()));
1482 EXPECT_FALSE(
1483 match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>()));
1484
1485 // Float arbitrary vector
1486
1487 Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi});
1488 Constant *CF32Undef = UndefValue::get(F32Ty);
1489 Constant *CF32NaNWithUndef =
1490 ConstantVector::get({CF32Undef, CF32NaN, CF32Undef});
1491
1492 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>()));
1493 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>()));
1494 EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>()));
1495 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>()));
1496
1497 EXPECT_TRUE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>()));
1498 EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>()));
1499 EXPECT_TRUE(
1500 match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>()));
1501 EXPECT_FALSE(
1502 match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
1503 }
1504
1505 template <typename T> struct MutableConstTest : PatternMatchTest { };
1506
1507 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1508 std::tuple<const Value*, const Instruction *>>
1509 MutableConstTestTypes;
1510 TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
1511
TYPED_TEST(MutableConstTest,ICmp)1512 TYPED_TEST(MutableConstTest, ICmp) {
1513 auto &IRB = PatternMatchTest::IRB;
1514
1515 typedef std::tuple_element_t<0, TypeParam> ValueType;
1516 typedef std::tuple_element_t<1, TypeParam> InstructionType;
1517
1518 Value *L = IRB.getInt32(1);
1519 Value *R = IRB.getInt32(2);
1520 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1521
1522 ValueType MatchL;
1523 ValueType MatchR;
1524 ICmpInst::Predicate MatchPred;
1525
1526 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1527 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1528 EXPECT_EQ(L, MatchL);
1529 EXPECT_EQ(R, MatchR);
1530 }
1531
1532 } // anonymous namespace.
1533