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__anon0673fe7a0111::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 Constant *ScalarNaN = ConstantFP::getNaN(ScalarTy, true);
1097
1098 Constant *VectorZeroUndef =
1099 ConstantVector::get({ScalarUndef, ScalarZero, ScalarUndef, ScalarZero});
1100
1101 Constant *VectorInfUndef = ConstantVector::get(
1102 {ScalarPosInf, ScalarNegInf, ScalarUndef, ScalarPosInf});
1103
1104 Constant *VectorNaNUndef =
1105 ConstantVector::get({ScalarUndef, ScalarNaN, ScalarNaN, ScalarNaN});
1106
1107 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1108 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1109 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1110 EXPECT_FALSE(match(VectorZero, m_Undef()));
1111 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1112 EXPECT_FALSE(match(VectorInfUndef, m_Undef()));
1113 EXPECT_FALSE(match(VectorNaNUndef, m_Undef()));
1114
1115 EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1116 EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1117 EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1118 EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1119 EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
1120 EXPECT_FALSE(match(VectorInfUndef, m_AnyZeroFP()));
1121 EXPECT_FALSE(match(VectorNaNUndef, m_AnyZeroFP()));
1122
1123 EXPECT_FALSE(match(ScalarUndef, m_NaN()));
1124 EXPECT_FALSE(match(VectorUndef, m_NaN()));
1125 EXPECT_FALSE(match(VectorZeroUndef, m_NaN()));
1126 EXPECT_FALSE(match(ScalarPosInf, m_NaN()));
1127 EXPECT_FALSE(match(ScalarNegInf, m_NaN()));
1128 EXPECT_TRUE(match(ScalarNaN, m_NaN()));
1129 EXPECT_FALSE(match(VectorInfUndef, m_NaN()));
1130 EXPECT_TRUE(match(VectorNaNUndef, m_NaN()));
1131
1132 EXPECT_FALSE(match(ScalarUndef, m_NonNaN()));
1133 EXPECT_FALSE(match(VectorUndef, m_NonNaN()));
1134 EXPECT_TRUE(match(VectorZeroUndef, m_NonNaN()));
1135 EXPECT_TRUE(match(ScalarPosInf, m_NonNaN()));
1136 EXPECT_TRUE(match(ScalarNegInf, m_NonNaN()));
1137 EXPECT_FALSE(match(ScalarNaN, m_NonNaN()));
1138 EXPECT_TRUE(match(VectorInfUndef, m_NonNaN()));
1139 EXPECT_FALSE(match(VectorNaNUndef, m_NonNaN()));
1140
1141 EXPECT_FALSE(match(ScalarUndef, m_Inf()));
1142 EXPECT_FALSE(match(VectorUndef, m_Inf()));
1143 EXPECT_FALSE(match(VectorZeroUndef, m_Inf()));
1144 EXPECT_TRUE(match(ScalarPosInf, m_Inf()));
1145 EXPECT_TRUE(match(ScalarNegInf, m_Inf()));
1146 EXPECT_FALSE(match(ScalarNaN, m_Inf()));
1147 EXPECT_TRUE(match(VectorInfUndef, m_Inf()));
1148 EXPECT_FALSE(match(VectorNaNUndef, m_Inf()));
1149
1150 EXPECT_FALSE(match(ScalarUndef, m_NonInf()));
1151 EXPECT_FALSE(match(VectorUndef, m_NonInf()));
1152 EXPECT_TRUE(match(VectorZeroUndef, m_NonInf()));
1153 EXPECT_FALSE(match(ScalarPosInf, m_NonInf()));
1154 EXPECT_FALSE(match(ScalarNegInf, m_NonInf()));
1155 EXPECT_TRUE(match(ScalarNaN, m_NonInf()));
1156 EXPECT_FALSE(match(VectorInfUndef, m_NonInf()));
1157 EXPECT_TRUE(match(VectorNaNUndef, m_NonInf()));
1158
1159 EXPECT_FALSE(match(ScalarUndef, m_Finite()));
1160 EXPECT_FALSE(match(VectorUndef, m_Finite()));
1161 EXPECT_TRUE(match(VectorZeroUndef, m_Finite()));
1162 EXPECT_FALSE(match(ScalarPosInf, m_Finite()));
1163 EXPECT_FALSE(match(ScalarNegInf, m_Finite()));
1164 EXPECT_FALSE(match(ScalarNaN, m_Finite()));
1165 EXPECT_FALSE(match(VectorInfUndef, m_Finite()));
1166 EXPECT_FALSE(match(VectorNaNUndef, m_Finite()));
1167
1168 const APFloat *C;
1169 // Regardless of whether undefs are allowed,
1170 // a fully undef constant does not match.
1171 EXPECT_FALSE(match(ScalarUndef, m_APFloat(C)));
1172 EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidUndef(C)));
1173 EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowUndef(C)));
1174 EXPECT_FALSE(match(VectorUndef, m_APFloat(C)));
1175 EXPECT_FALSE(match(VectorUndef, m_APFloatForbidUndef(C)));
1176 EXPECT_FALSE(match(VectorUndef, m_APFloatAllowUndef(C)));
1177
1178 // We can always match simple constants and simple splats.
1179 C = nullptr;
1180 EXPECT_TRUE(match(ScalarZero, m_APFloat(C)));
1181 EXPECT_TRUE(C->isZero());
1182 C = nullptr;
1183 EXPECT_TRUE(match(ScalarZero, m_APFloatForbidUndef(C)));
1184 EXPECT_TRUE(C->isZero());
1185 C = nullptr;
1186 EXPECT_TRUE(match(ScalarZero, m_APFloatAllowUndef(C)));
1187 EXPECT_TRUE(C->isZero());
1188 C = nullptr;
1189 EXPECT_TRUE(match(VectorZero, m_APFloat(C)));
1190 EXPECT_TRUE(C->isZero());
1191 C = nullptr;
1192 EXPECT_TRUE(match(VectorZero, m_APFloatForbidUndef(C)));
1193 EXPECT_TRUE(C->isZero());
1194 C = nullptr;
1195 EXPECT_TRUE(match(VectorZero, m_APFloatAllowUndef(C)));
1196 EXPECT_TRUE(C->isZero());
1197
1198 // Whether splats with undef can be matched depends on the matcher.
1199 EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C)));
1200 EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidUndef(C)));
1201 C = nullptr;
1202 EXPECT_TRUE(match(VectorZeroUndef, m_APFloatAllowUndef(C)));
1203 EXPECT_TRUE(C->isZero());
1204 C = nullptr;
1205 EXPECT_TRUE(match(VectorZeroUndef, m_Finite(C)));
1206 EXPECT_TRUE(C->isZero());
1207 }
1208
TEST_F(PatternMatchTest,FloatingPointFNeg)1209 TEST_F(PatternMatchTest, FloatingPointFNeg) {
1210 Type *FltTy = IRB.getFloatTy();
1211 Value *One = ConstantFP::get(FltTy, 1.0);
1212 Value *Z = ConstantFP::get(FltTy, 0.0);
1213 Value *NZ = ConstantFP::get(FltTy, -0.0);
1214 Value *V = IRB.CreateFNeg(One);
1215 Value *V1 = IRB.CreateFSub(NZ, One);
1216 Value *V2 = IRB.CreateFSub(Z, One);
1217 Value *V3 = IRB.CreateFAdd(NZ, One);
1218 Value *Match;
1219
1220 // Test FNeg(1.0)
1221 EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1222 EXPECT_EQ(One, Match);
1223
1224 // Test FSub(-0.0, 1.0)
1225 EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1226 EXPECT_EQ(One, Match);
1227
1228 // Test FSub(0.0, 1.0)
1229 EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1230 cast<Instruction>(V2)->setHasNoSignedZeros(true);
1231 EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1232 EXPECT_EQ(One, Match);
1233
1234 // Test FAdd(-0.0, 1.0)
1235 EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1236 }
1237
TEST_F(PatternMatchTest,CondBranchTest)1238 TEST_F(PatternMatchTest, CondBranchTest) {
1239 BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1240 BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1241 Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1242
1243 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1244
1245 BasicBlock *A, *B;
1246 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1247 EXPECT_EQ(TrueBB, A);
1248 EXPECT_EQ(FalseBB, B);
1249
1250 EXPECT_FALSE(
1251 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1252 EXPECT_FALSE(
1253 match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1254 EXPECT_FALSE(
1255 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1256 EXPECT_TRUE(
1257 match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1258
1259 // Check we can use m_Deferred with branches.
1260 EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1261 Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1262 A = nullptr;
1263 EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1264 }
1265
TEST_F(PatternMatchTest,WithOverflowInst)1266 TEST_F(PatternMatchTest, WithOverflowInst) {
1267 Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1268 IRB.getInt32(0), IRB.getInt32(0));
1269 Value *Add0 = IRB.CreateExtractValue(Add, 0);
1270 Value *Add1 = IRB.CreateExtractValue(Add, 1);
1271
1272 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1273 EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1274 EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1275 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1276 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1277 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1278
1279 WithOverflowInst *WOI;
1280 EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1281 EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1282 EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1283
1284 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1285 EXPECT_EQ(Add, WOI);
1286 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1287 EXPECT_EQ(Add, WOI);
1288 }
1289
TEST_F(PatternMatchTest,MinMaxIntrinsics)1290 TEST_F(PatternMatchTest, MinMaxIntrinsics) {
1291 Type *Ty = IRB.getInt32Ty();
1292 Value *L = ConstantInt::get(Ty, 1);
1293 Value *R = ConstantInt::get(Ty, 2);
1294 Value *MatchL, *MatchR;
1295
1296 // Check for intrinsic ID match and capture of operands.
1297 EXPECT_TRUE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1298 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1299 EXPECT_EQ(L, MatchL);
1300 EXPECT_EQ(R, MatchR);
1301
1302 EXPECT_TRUE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1303 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1304 EXPECT_EQ(L, MatchL);
1305 EXPECT_EQ(R, MatchR);
1306
1307 EXPECT_TRUE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1308 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1309 EXPECT_EQ(L, MatchL);
1310 EXPECT_EQ(R, MatchR);
1311
1312 EXPECT_TRUE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1313 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1314 EXPECT_EQ(L, MatchL);
1315 EXPECT_EQ(R, MatchR);
1316
1317 // Check for intrinsic ID mismatch.
1318 EXPECT_FALSE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1319 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1320 EXPECT_FALSE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1321 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1322 EXPECT_FALSE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1323 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1324 EXPECT_FALSE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1325 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1326 }
1327
TEST_F(PatternMatchTest,IntrinsicMatcher)1328 TEST_F(PatternMatchTest, IntrinsicMatcher) {
1329 Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1330 Value *Hash = IRB.getInt64(0);
1331 Value *Num = IRB.getInt32(1);
1332 Value *Index = IRB.getInt32(2);
1333 Value *Step = IRB.getInt64(3);
1334
1335 Value *Ops[] = {Name, Hash, Num, Index, Step};
1336 Module *M = BB->getParent()->getParent();
1337 Function *TheFn =
1338 Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1339
1340 Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1341
1342 // Match without capturing.
1343 EXPECT_TRUE(match(
1344 Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1345 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1346 EXPECT_FALSE(match(
1347 Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1348 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1349
1350 // Match with capturing.
1351 Value *Arg1 = nullptr;
1352 Value *Arg2 = nullptr;
1353 Value *Arg3 = nullptr;
1354 Value *Arg4 = nullptr;
1355 Value *Arg5 = nullptr;
1356 EXPECT_TRUE(
1357 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1358 m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1359 m_Value(Arg4), m_Value(Arg5))));
1360 EXPECT_EQ(Arg1, Name);
1361 EXPECT_EQ(Arg2, Hash);
1362 EXPECT_EQ(Arg3, Num);
1363 EXPECT_EQ(Arg4, Index);
1364 EXPECT_EQ(Arg5, Step);
1365
1366 // Match specific second argument.
1367 EXPECT_TRUE(
1368 match(Intrinsic5,
1369 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1370 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1371 EXPECT_FALSE(
1372 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1373 m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1374 m_Value())));
1375
1376 // Match specific third argument.
1377 EXPECT_TRUE(
1378 match(Intrinsic5,
1379 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1380 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1381 EXPECT_FALSE(
1382 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1383 m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1384 m_Value())));
1385
1386 // Match specific fourth argument.
1387 EXPECT_TRUE(
1388 match(Intrinsic5,
1389 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1390 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1391 EXPECT_FALSE(
1392 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1393 m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1394 m_Value())));
1395
1396 // Match specific fifth argument.
1397 EXPECT_TRUE(
1398 match(Intrinsic5,
1399 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1400 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1401 EXPECT_FALSE(
1402 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1403 m_Value(), m_Value(), m_Value(), m_Value(),
1404 m_SpecificInt(10))));
1405 }
1406
1407 namespace {
1408
1409 struct is_unsigned_zero_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::is_unsigned_zero_pred1410 bool isValue(const APInt &C) { return C.isNullValue(); }
1411 };
1412
1413 struct is_float_zero_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::is_float_zero_pred1414 bool isValue(const APFloat &C) { return C.isZero(); }
1415 };
1416
1417 template <typename T> struct always_true_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::always_true_pred1418 bool isValue(const T &) { return true; }
1419 };
1420
1421 template <typename T> struct always_false_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::always_false_pred1422 bool isValue(const T &) { return false; }
1423 };
1424
1425 struct is_unsigned_max_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::is_unsigned_max_pred1426 bool isValue(const APInt &C) { return C.isMaxValue(); }
1427 };
1428
1429 struct is_float_nan_pred {
isValue__anon0673fe7a0111::__anon0673fe7a0311::is_float_nan_pred1430 bool isValue(const APFloat &C) { return C.isNaN(); }
1431 };
1432
1433 } // namespace
1434
TEST_F(PatternMatchTest,ConstantPredicateType)1435 TEST_F(PatternMatchTest, ConstantPredicateType) {
1436
1437 // Scalar integer
1438 APInt U32Max = APInt::getAllOnesValue(32);
1439 APInt U32Zero = APInt::getNullValue(32);
1440 APInt U32DeadBeef(32, 0xDEADBEEF);
1441
1442 Type *U32Ty = Type::getInt32Ty(Ctx);
1443
1444 Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max);
1445 Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero);
1446 Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef);
1447
1448 EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1449 EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1450 EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1451 EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1452
1453 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1454 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1455 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1456 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1457
1458 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1459 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1460 EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1461 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1462
1463 // Scalar float
1464 APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle());
1465 APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle());
1466 APFloat F32Pi(3.14f);
1467
1468 Type *F32Ty = Type::getFloatTy(Ctx);
1469
1470 Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN);
1471 Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero);
1472 Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi);
1473
1474 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1475 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1476 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1477 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1478
1479 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1480 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1481 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1482 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1483
1484 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1485 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1486 EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1487 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1488
1489 auto FixedEC = ElementCount::getFixed(4);
1490 auto ScalableEC = ElementCount::getScalable(4);
1491
1492 // Vector splat
1493
1494 for (auto EC : {FixedEC, ScalableEC}) {
1495 // integer
1496
1497 Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max);
1498 Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero);
1499 Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef);
1500
1501 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1502 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1503 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1504 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1505
1506 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1507 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1508 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1509 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1510
1511 EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1512 EXPECT_FALSE(
1513 match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1514 EXPECT_TRUE(
1515 match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1516 EXPECT_FALSE(
1517 match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1518
1519 // float
1520
1521 Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN);
1522 Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero);
1523 Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi);
1524
1525 EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1526 EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1527 EXPECT_TRUE(
1528 match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1529 EXPECT_FALSE(
1530 match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1531
1532 EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1533 EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1534 EXPECT_TRUE(
1535 match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1536 EXPECT_FALSE(
1537 match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1538
1539 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1540 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1541 EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1542 EXPECT_FALSE(
1543 match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1544 }
1545
1546 // Int arbitrary vector
1547
1548 Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef});
1549 Constant *CU32Undef = UndefValue::get(U32Ty);
1550 Constant *CU32MaxWithUndef =
1551 ConstantVector::get({CU32Undef, CU32Max, CU32Undef});
1552
1553 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>()));
1554 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>()));
1555 EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>()));
1556 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>()));
1557
1558 EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>()));
1559 EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>()));
1560 EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>()));
1561 EXPECT_FALSE(
1562 match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>()));
1563
1564 // Float arbitrary vector
1565
1566 Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi});
1567 Constant *CF32Undef = UndefValue::get(F32Ty);
1568 Constant *CF32NaNWithUndef =
1569 ConstantVector::get({CF32Undef, CF32NaN, CF32Undef});
1570
1571 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>()));
1572 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>()));
1573 EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>()));
1574 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>()));
1575
1576 EXPECT_TRUE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>()));
1577 EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>()));
1578 EXPECT_TRUE(
1579 match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>()));
1580 EXPECT_FALSE(
1581 match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
1582 }
1583
1584 template <typename T> struct MutableConstTest : PatternMatchTest { };
1585
1586 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1587 std::tuple<const Value*, const Instruction *>>
1588 MutableConstTestTypes;
1589 TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
1590
TYPED_TEST(MutableConstTest,ICmp)1591 TYPED_TEST(MutableConstTest, ICmp) {
1592 auto &IRB = PatternMatchTest::IRB;
1593
1594 typedef std::tuple_element_t<0, TypeParam> ValueType;
1595 typedef std::tuple_element_t<1, TypeParam> InstructionType;
1596
1597 Value *L = IRB.getInt32(1);
1598 Value *R = IRB.getInt32(2);
1599 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1600
1601 ValueType MatchL;
1602 ValueType MatchR;
1603 ICmpInst::Predicate MatchPred;
1604
1605 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1606 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1607 EXPECT_EQ(L, MatchL);
1608 EXPECT_EQ(R, MatchR);
1609 }
1610
1611 } // anonymous namespace.
1612