1 //===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
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
7 //===----------------------------------------------------------------------===//
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"
29 using namespace llvm::PatternMatch
;
33 struct PatternMatchTest
: ::testing::Test
{
35 std::unique_ptr
<Module
> M
;
38 IRBuilder
<NoFolder
> IRB
;
41 : M(new Module("PatternMatchTestModule", Ctx
)),
43 FunctionType::get(Type::getVoidTy(Ctx
), /* IsVarArg */ false),
44 Function::ExternalLinkage
, "f", M
.get())),
45 BB(BasicBlock::Create(Ctx
, "entry", F
)), IRB(BB
) {}
48 TEST_F(PatternMatchTest
, OneUse
) {
49 // Build up a little tree of values:
53 // Leaf = (Two + 8) + (Two + 13)
54 Value
*One
= IRB
.CreateAdd(IRB
.CreateAdd(IRB
.getInt32(1), IRB
.getInt32(2)),
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)));
61 EXPECT_TRUE(m_OneUse(m_Value(V
)).match(One
));
64 EXPECT_FALSE(m_OneUse(m_Value()).match(Two
));
65 EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf
));
68 TEST_F(PatternMatchTest
, SpecificIntEQ
) {
69 Type
*IntTy
= IRB
.getInt32Ty();
70 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
72 Value
*Zero
= ConstantInt::get(IntTy
, 0);
73 Value
*One
= ConstantInt::get(IntTy
, 1);
74 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
77 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 0))
80 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 0))
83 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 0))
87 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 1))
90 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 1))
93 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, 1))
97 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, -1))
100 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, -1))
103 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ
, APInt(BitWidth
, -1))
107 TEST_F(PatternMatchTest
, SpecificIntNE
) {
108 Type
*IntTy
= IRB
.getInt32Ty();
109 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
111 Value
*Zero
= ConstantInt::get(IntTy
, 0);
112 Value
*One
= ConstantInt::get(IntTy
, 1);
113 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
116 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 0))
119 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 0))
122 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 0))
126 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 1))
129 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 1))
132 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, 1))
136 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, -1))
139 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, -1))
142 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE
, APInt(BitWidth
, -1))
146 TEST_F(PatternMatchTest
, SpecificIntUGT
) {
147 Type
*IntTy
= IRB
.getInt32Ty();
148 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
150 Value
*Zero
= ConstantInt::get(IntTy
, 0);
151 Value
*One
= ConstantInt::get(IntTy
, 1);
152 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
155 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 0))
158 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 0))
161 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 0))
165 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 1))
168 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 1))
171 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, 1))
175 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, -1))
178 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, -1))
181 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT
, APInt(BitWidth
, -1))
185 TEST_F(PatternMatchTest
, SignbitZeroChecks
) {
186 Type
*IntTy
= IRB
.getInt32Ty();
188 Value
*Zero
= ConstantInt::get(IntTy
, 0);
189 Value
*One
= ConstantInt::get(IntTy
, 1);
190 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
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
));
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
));
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
));
208 TEST_F(PatternMatchTest
, SpecificIntUGE
) {
209 Type
*IntTy
= IRB
.getInt32Ty();
210 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
212 Value
*Zero
= ConstantInt::get(IntTy
, 0);
213 Value
*One
= ConstantInt::get(IntTy
, 1);
214 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
217 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 0))
220 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 0))
223 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 0))
227 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 1))
230 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 1))
233 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, 1))
237 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, -1))
240 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, -1))
243 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE
, APInt(BitWidth
, -1))
247 TEST_F(PatternMatchTest
, SpecificIntULT
) {
248 Type
*IntTy
= IRB
.getInt32Ty();
249 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
251 Value
*Zero
= ConstantInt::get(IntTy
, 0);
252 Value
*One
= ConstantInt::get(IntTy
, 1);
253 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
256 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 0))
259 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 0))
262 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 0))
266 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 1))
269 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 1))
272 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, 1))
276 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, -1))
279 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, -1))
282 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT
, APInt(BitWidth
, -1))
286 TEST_F(PatternMatchTest
, SpecificIntULE
) {
287 Type
*IntTy
= IRB
.getInt32Ty();
288 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
290 Value
*Zero
= ConstantInt::get(IntTy
, 0);
291 Value
*One
= ConstantInt::get(IntTy
, 1);
292 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
295 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 0))
298 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 0))
301 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 0))
305 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 1))
308 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 1))
311 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, 1))
315 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, -1))
318 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, -1))
321 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE
, APInt(BitWidth
, -1))
325 TEST_F(PatternMatchTest
, SpecificIntSGT
) {
326 Type
*IntTy
= IRB
.getInt32Ty();
327 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
329 Value
*Zero
= ConstantInt::get(IntTy
, 0);
330 Value
*One
= ConstantInt::get(IntTy
, 1);
331 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
334 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 0))
337 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 0))
340 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 0))
344 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 1))
347 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 1))
350 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, 1))
354 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, -1))
357 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, -1))
360 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT
, APInt(BitWidth
, -1))
364 TEST_F(PatternMatchTest
, SpecificIntSGE
) {
365 Type
*IntTy
= IRB
.getInt32Ty();
366 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
368 Value
*Zero
= ConstantInt::get(IntTy
, 0);
369 Value
*One
= ConstantInt::get(IntTy
, 1);
370 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
373 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 0))
376 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 0))
379 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 0))
383 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 1))
386 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 1))
389 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, 1))
393 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, -1))
396 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, -1))
399 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE
, APInt(BitWidth
, -1))
403 TEST_F(PatternMatchTest
, SpecificIntSLT
) {
404 Type
*IntTy
= IRB
.getInt32Ty();
405 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
407 Value
*Zero
= ConstantInt::get(IntTy
, 0);
408 Value
*One
= ConstantInt::get(IntTy
, 1);
409 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
412 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 0))
415 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 0))
418 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 0))
422 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 1))
425 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 1))
428 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, 1))
432 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, -1))
435 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, -1))
438 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT
, APInt(BitWidth
, -1))
442 TEST_F(PatternMatchTest
, SpecificIntSLE
) {
443 Type
*IntTy
= IRB
.getInt32Ty();
444 unsigned BitWidth
= IntTy
->getScalarSizeInBits();
446 Value
*Zero
= ConstantInt::get(IntTy
, 0);
447 Value
*One
= ConstantInt::get(IntTy
, 1);
448 Value
*NegOne
= ConstantInt::get(IntTy
, -1);
451 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 0))
454 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 0))
457 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 0))
461 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 1))
464 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 1))
467 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, 1))
471 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, -1))
474 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, -1))
477 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE
, APInt(BitWidth
, -1))
481 TEST_F(PatternMatchTest
, Unless
) {
482 Value
*X
= IRB
.CreateAdd(IRB
.getInt32(1), IRB
.getInt32(0));
484 EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X
));
485 EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X
));
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
));
490 EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X
));
491 EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X
));
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
));
497 TEST_F(PatternMatchTest
, ZExtSExtSelf
) {
498 LLVMContext
&Ctx
= IRB
.getContext();
500 Value
*One32
= IRB
.getInt32(1);
501 Value
*One64Z
= IRB
.CreateZExt(One32
, IntegerType::getInt64Ty(Ctx
));
502 Value
*One64S
= IRB
.CreateSExt(One32
, IntegerType::getInt64Ty(Ctx
));
504 EXPECT_TRUE(m_One().match(One32
));
505 EXPECT_FALSE(m_One().match(One64Z
));
506 EXPECT_FALSE(m_One().match(One64S
));
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
));
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
));
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
));
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
));
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
));
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
));
533 TEST_F(PatternMatchTest
, Power2
) {
534 Value
*C128
= IRB
.getInt32(128);
535 Value
*CNeg128
= ConstantExpr::getNeg(cast
<Constant
>(C128
));
537 EXPECT_TRUE(m_Power2().match(C128
));
538 EXPECT_FALSE(m_Power2().match(CNeg128
));
540 EXPECT_FALSE(m_NegatedPower2().match(C128
));
541 EXPECT_TRUE(m_NegatedPower2().match(CNeg128
));
543 Value
*CIntMin
= IRB
.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
544 Value
*CNegIntMin
= ConstantExpr::getNeg(cast
<Constant
>(CIntMin
));
546 EXPECT_TRUE(m_Power2().match(CIntMin
));
547 EXPECT_TRUE(m_Power2().match(CNegIntMin
));
549 EXPECT_TRUE(m_NegatedPower2().match(CIntMin
));
550 EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin
));
553 TEST_F(PatternMatchTest
, Not
) {
554 Value
*C1
= IRB
.getInt32(1);
555 Value
*C2
= IRB
.getInt32(2);
556 Value
*C3
= IRB
.getInt32(3);
557 Instruction
*Not
= BinaryOperator::CreateXor(C1
, C2
);
559 // When `m_Not` does not match the `not` itself,
560 // it should not try to apply the inner matcher.
562 EXPECT_FALSE(m_Not(m_Value(Val
)).match(Not
));
567 TEST_F(PatternMatchTest
, CommutativeDeferredValue
) {
568 Value
*X
= IRB
.getInt32(1);
569 Value
*Y
= IRB
.getInt32(2);
573 EXPECT_TRUE(match(X
, m_Deferred(tX
)));
574 EXPECT_FALSE(match(Y
, m_Deferred(tX
)));
578 EXPECT_TRUE(match(X
, m_Deferred(tX
)));
579 EXPECT_FALSE(match(Y
, m_Deferred(tX
)));
583 EXPECT_TRUE(match(X
, m_Deferred(tX
)));
584 EXPECT_FALSE(match(Y
, m_Deferred(tX
)));
587 const Value
*const tX
= X
;
588 EXPECT_TRUE(match(X
, m_Deferred(tX
)));
589 EXPECT_FALSE(match(Y
, m_Deferred(tX
)));
594 EXPECT_TRUE(match(IRB
.CreateAnd(X
, X
), m_And(m_Value(tX
), m_Deferred(tX
))));
600 match(IRB
.CreateAnd(X
, Y
), m_c_And(m_Value(tX
), m_Deferred(tX
))));
603 auto checkMatch
= [X
, Y
](Value
*Pattern
) {
604 Value
*tX
= nullptr, *tY
= nullptr;
606 Pattern
, m_c_And(m_Value(tX
), m_c_And(m_Deferred(tX
), m_Value(tY
)))));
611 checkMatch(IRB
.CreateAnd(X
, IRB
.CreateAnd(X
, Y
)));
612 checkMatch(IRB
.CreateAnd(X
, IRB
.CreateAnd(Y
, X
)));
613 checkMatch(IRB
.CreateAnd(IRB
.CreateAnd(X
, Y
), X
));
614 checkMatch(IRB
.CreateAnd(IRB
.CreateAnd(Y
, X
), X
));
617 TEST_F(PatternMatchTest
, FloatingPointOrderedMin
) {
618 Type
*FltTy
= IRB
.getFloatTy();
619 Value
*L
= ConstantFP::get(FltTy
, 1.0);
620 Value
*R
= ConstantFP::get(FltTy
, 2.0);
621 Value
*MatchL
, *MatchR
;
624 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
625 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLT(L
, R
), L
, R
)));
626 EXPECT_EQ(L
, MatchL
);
627 EXPECT_EQ(R
, MatchR
);
630 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
631 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLE(L
, R
), L
, R
)));
632 EXPECT_EQ(L
, MatchL
);
633 EXPECT_EQ(R
, MatchR
);
635 // Test no match on OGE.
636 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
637 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGE(L
, R
), L
, R
)));
639 // Test no match on OGT.
640 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
641 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGT(L
, R
), L
, R
)));
643 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
644 // %cmp = fcmp oge L, R
645 // %min = select %cmp R, L
647 // the above is expanded to %cmp == false ==> %min = L
648 // which is true for UnordFMin, not OrdFMin, so test that:
650 // [OU]GE with inverted select.
651 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
652 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGE(L
, R
), R
, L
)));
653 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
654 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGE(L
, R
), R
, L
)));
655 EXPECT_EQ(L
, MatchL
);
656 EXPECT_EQ(R
, MatchR
);
658 // [OU]GT with inverted select.
659 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
660 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGT(L
, R
), R
, L
)));
661 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL
), m_Value(MatchR
))
662 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGT(L
, R
), R
, L
)));
663 EXPECT_EQ(L
, MatchL
);
664 EXPECT_EQ(R
, MatchR
);
667 TEST_F(PatternMatchTest
, FloatingPointOrderedMax
) {
668 Type
*FltTy
= IRB
.getFloatTy();
669 Value
*L
= ConstantFP::get(FltTy
, 1.0);
670 Value
*R
= ConstantFP::get(FltTy
, 2.0);
671 Value
*MatchL
, *MatchR
;
674 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
675 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGT(L
, R
), L
, R
)));
676 EXPECT_EQ(L
, MatchL
);
677 EXPECT_EQ(R
, MatchR
);
680 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
681 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGE(L
, R
), L
, R
)));
682 EXPECT_EQ(L
, MatchL
);
683 EXPECT_EQ(R
, MatchR
);
685 // Test no match on OLE.
686 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
687 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLE(L
, R
), L
, R
)));
689 // Test no match on OLT.
690 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
691 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLT(L
, R
), L
, R
)));
694 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
695 // %cmp = fcmp ole L, R
696 // %max = select %cmp, R, L
698 // the above is expanded to %cmp == false ==> %max == L
699 // which is true for UnordFMax, not OrdFMax, so test that:
701 // [OU]LE with inverted select.
702 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
703 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLE(L
, R
), R
, L
)));
704 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
705 .match(IRB
.CreateSelect(IRB
.CreateFCmpULE(L
, R
), R
, L
)));
706 EXPECT_EQ(L
, MatchL
);
707 EXPECT_EQ(R
, MatchR
);
709 // [OUT]LT with inverted select.
710 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
711 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLT(L
, R
), R
, L
)));
712 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL
), m_Value(MatchR
))
713 .match(IRB
.CreateSelect(IRB
.CreateFCmpULT(L
, R
), R
, L
)));
714 EXPECT_EQ(L
, MatchL
);
715 EXPECT_EQ(R
, MatchR
);
718 TEST_F(PatternMatchTest
, FloatingPointUnorderedMin
) {
719 Type
*FltTy
= IRB
.getFloatTy();
720 Value
*L
= ConstantFP::get(FltTy
, 1.0);
721 Value
*R
= ConstantFP::get(FltTy
, 2.0);
722 Value
*MatchL
, *MatchR
;
725 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
726 .match(IRB
.CreateSelect(IRB
.CreateFCmpULT(L
, R
), L
, R
)));
727 EXPECT_EQ(L
, MatchL
);
728 EXPECT_EQ(R
, MatchR
);
731 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
732 .match(IRB
.CreateSelect(IRB
.CreateFCmpULE(L
, R
), L
, R
)));
733 EXPECT_EQ(L
, MatchL
);
734 EXPECT_EQ(R
, MatchR
);
736 // Test no match on UGE.
737 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
738 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGE(L
, R
), L
, R
)));
740 // Test no match on UGT.
741 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
742 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGT(L
, R
), L
, R
)));
744 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
745 // %cmp = fcmp uge L, R
746 // %min = select %cmp R, L
748 // the above is expanded to %cmp == true ==> %min = R
749 // which is true for OrdFMin, not UnordFMin, so test that:
751 // [UO]GE with inverted select.
752 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
753 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGE(L
, R
), R
, L
)));
754 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
755 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGE(L
, R
), R
, L
)));
756 EXPECT_EQ(L
, MatchL
);
757 EXPECT_EQ(R
, MatchR
);
759 // [UO]GT with inverted select.
760 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
761 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGT(L
, R
), R
, L
)));
762 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL
), m_Value(MatchR
))
763 .match(IRB
.CreateSelect(IRB
.CreateFCmpOGT(L
, R
), R
, L
)));
764 EXPECT_EQ(L
, MatchL
);
765 EXPECT_EQ(R
, MatchR
);
768 TEST_F(PatternMatchTest
, FloatingPointUnorderedMax
) {
769 Type
*FltTy
= IRB
.getFloatTy();
770 Value
*L
= ConstantFP::get(FltTy
, 1.0);
771 Value
*R
= ConstantFP::get(FltTy
, 2.0);
772 Value
*MatchL
, *MatchR
;
775 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
776 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGT(L
, R
), L
, R
)));
777 EXPECT_EQ(L
, MatchL
);
778 EXPECT_EQ(R
, MatchR
);
781 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
782 .match(IRB
.CreateSelect(IRB
.CreateFCmpUGE(L
, R
), L
, R
)));
783 EXPECT_EQ(L
, MatchL
);
784 EXPECT_EQ(R
, MatchR
);
786 // Test no match on ULE.
787 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
788 .match(IRB
.CreateSelect(IRB
.CreateFCmpULE(L
, R
), L
, R
)));
790 // Test no match on ULT.
791 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
792 .match(IRB
.CreateSelect(IRB
.CreateFCmpULT(L
, R
), L
, R
)));
794 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
795 // %cmp = fcmp ule L, R
796 // %max = select %cmp R, L
798 // the above is expanded to %cmp == true ==> %max = R
799 // which is true for OrdFMax, not UnordFMax, so test that:
801 // [UO]LE with inverted select.
802 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
803 .match(IRB
.CreateSelect(IRB
.CreateFCmpULE(L
, R
), R
, L
)));
804 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
805 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLE(L
, R
), R
, L
)));
806 EXPECT_EQ(L
, MatchL
);
807 EXPECT_EQ(R
, MatchR
);
809 // [UO]LT with inverted select.
810 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
811 .match(IRB
.CreateSelect(IRB
.CreateFCmpULT(L
, R
), R
, L
)));
812 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL
), m_Value(MatchR
))
813 .match(IRB
.CreateSelect(IRB
.CreateFCmpOLT(L
, R
), R
, L
)));
814 EXPECT_EQ(L
, MatchL
);
815 EXPECT_EQ(R
, MatchR
);
818 TEST_F(PatternMatchTest
, OverflowingBinOps
) {
819 Value
*L
= IRB
.getInt32(1);
820 Value
*R
= IRB
.getInt32(2);
821 Value
*MatchL
, *MatchR
;
824 m_NSWAdd(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNSWAdd(L
, R
)));
825 EXPECT_EQ(L
, MatchL
);
826 EXPECT_EQ(R
, MatchR
);
827 MatchL
= MatchR
= nullptr;
829 m_NSWSub(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNSWSub(L
, R
)));
830 EXPECT_EQ(L
, MatchL
);
831 EXPECT_EQ(R
, MatchR
);
832 MatchL
= MatchR
= nullptr;
834 m_NSWMul(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNSWMul(L
, R
)));
835 EXPECT_EQ(L
, MatchL
);
836 EXPECT_EQ(R
, MatchR
);
837 MatchL
= MatchR
= nullptr;
838 EXPECT_TRUE(m_NSWShl(m_Value(MatchL
), m_Value(MatchR
)).match(
839 IRB
.CreateShl(L
, R
, "", /* NUW */ false, /* NSW */ true)));
840 EXPECT_EQ(L
, MatchL
);
841 EXPECT_EQ(R
, MatchR
);
844 m_NUWAdd(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNUWAdd(L
, R
)));
845 EXPECT_EQ(L
, MatchL
);
846 EXPECT_EQ(R
, MatchR
);
847 MatchL
= MatchR
= nullptr;
849 m_NUWSub(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNUWSub(L
, R
)));
850 EXPECT_EQ(L
, MatchL
);
851 EXPECT_EQ(R
, MatchR
);
852 MatchL
= MatchR
= nullptr;
854 m_NUWMul(m_Value(MatchL
), m_Value(MatchR
)).match(IRB
.CreateNUWMul(L
, R
)));
855 EXPECT_EQ(L
, MatchL
);
856 EXPECT_EQ(R
, MatchR
);
857 MatchL
= MatchR
= nullptr;
858 EXPECT_TRUE(m_NUWShl(m_Value(MatchL
), m_Value(MatchR
)).match(
859 IRB
.CreateShl(L
, R
, "", /* NUW */ true, /* NSW */ false)));
860 EXPECT_EQ(L
, MatchL
);
861 EXPECT_EQ(R
, MatchR
);
863 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB
.CreateAdd(L
, R
)));
864 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB
.CreateNUWAdd(L
, R
)));
865 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB
.CreateNSWSub(L
, R
)));
866 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB
.CreateSub(L
, R
)));
867 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB
.CreateNUWSub(L
, R
)));
868 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB
.CreateNSWAdd(L
, R
)));
869 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB
.CreateMul(L
, R
)));
870 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB
.CreateNUWMul(L
, R
)));
871 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB
.CreateNSWAdd(L
, R
)));
872 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB
.CreateShl(L
, R
)));
873 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
874 IRB
.CreateShl(L
, R
, "", /* NUW */ true, /* NSW */ false)));
875 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB
.CreateNSWAdd(L
, R
)));
877 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB
.CreateAdd(L
, R
)));
878 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB
.CreateNSWAdd(L
, R
)));
879 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB
.CreateNUWSub(L
, R
)));
880 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB
.CreateSub(L
, R
)));
881 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB
.CreateNSWSub(L
, R
)));
882 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB
.CreateNUWAdd(L
, R
)));
883 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB
.CreateMul(L
, R
)));
884 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB
.CreateNSWMul(L
, R
)));
885 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB
.CreateNUWAdd(L
, R
)));
886 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB
.CreateShl(L
, R
)));
887 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
888 IRB
.CreateShl(L
, R
, "", /* NUW */ false, /* NSW */ true)));
889 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB
.CreateNUWAdd(L
, R
)));
892 TEST_F(PatternMatchTest
, LoadStoreOps
) {
893 // Create this load/store sequence:
896 // %0 = load i32*, i32** %p
897 // store i32 42, i32* %0
899 Value
*Alloca
= IRB
.CreateAlloca(IRB
.getInt32Ty());
900 Value
*LoadInst
= IRB
.CreateLoad(IRB
.getInt32Ty(), Alloca
);
901 Value
*FourtyTwo
= IRB
.getInt32(42);
902 Value
*StoreInst
= IRB
.CreateStore(FourtyTwo
, Alloca
);
903 Value
*MatchLoad
, *MatchStoreVal
, *MatchStorePointer
;
905 EXPECT_TRUE(m_Load(m_Value(MatchLoad
)).match(LoadInst
));
906 EXPECT_EQ(Alloca
, MatchLoad
);
908 EXPECT_TRUE(m_Load(m_Specific(Alloca
)).match(LoadInst
));
910 EXPECT_FALSE(m_Load(m_Value(MatchLoad
)).match(Alloca
));
912 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal
), m_Value(MatchStorePointer
))
914 EXPECT_EQ(FourtyTwo
, MatchStoreVal
);
915 EXPECT_EQ(Alloca
, MatchStorePointer
);
917 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal
), m_Value(MatchStorePointer
))
920 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca
))
922 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo
))
924 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca
))
928 TEST_F(PatternMatchTest
, VectorOps
) {
929 // Build up small tree of vector operations
933 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
934 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
935 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
936 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
938 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
939 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
940 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
941 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
943 // SP1 = VectorSplat(2, i8 2)
944 // SP2 = VectorSplat(2, i8 %Val)
945 Type
*VecTy
= FixedVectorType::get(IRB
.getInt8Ty(), 2);
946 Type
*i32
= IRB
.getInt32Ty();
947 Type
*i32VecTy
= FixedVectorType::get(i32
, 2);
949 Value
*Val
= IRB
.CreateAdd(IRB
.getInt8(0), IRB
.getInt8(1));
950 Value
*Val2
= IRB
.CreateAdd(Val
, IRB
.getInt8(3));
952 SmallVector
<Constant
*, 2> VecElemIdxs
;
953 VecElemIdxs
.push_back(ConstantInt::get(i32
, 0));
954 VecElemIdxs
.push_back(ConstantInt::get(i32
, 2));
955 auto *IdxVec
= ConstantVector::get(VecElemIdxs
);
957 Value
*VI1
= IRB
.CreateInsertElement(VecTy
, IRB
.getInt8(1), (uint64_t)0);
958 Value
*VI2
= IRB
.CreateInsertElement(VI1
, Val2
, Val
);
959 Value
*VI3
= IRB
.CreateInsertElement(VI1
, Val2
, (uint64_t)1);
960 Value
*VI4
= IRB
.CreateInsertElement(VI1
, IRB
.getInt8(2), Val
);
962 Value
*EX1
= IRB
.CreateExtractElement(VI4
, Val
);
963 Value
*EX2
= IRB
.CreateExtractElement(VI4
, (uint64_t)0);
964 Value
*EX3
= IRB
.CreateExtractElement(IdxVec
, (uint64_t)1);
966 Constant
*Zero
= ConstantAggregateZero::get(i32VecTy
);
967 SmallVector
<int, 16> ZeroMask
;
968 ShuffleVectorInst::getShuffleMask(Zero
, ZeroMask
);
970 Value
*SI1
= IRB
.CreateShuffleVector(VI1
, ZeroMask
);
971 Value
*SI2
= IRB
.CreateShuffleVector(VI3
, VI4
, IdxVec
);
972 Value
*SI3
= IRB
.CreateShuffleVector(VI3
, ZeroMask
);
973 Value
*SI4
= IRB
.CreateShuffleVector(VI4
, ZeroMask
);
975 Value
*SP1
= IRB
.CreateVectorSplat(2, IRB
.getInt8(2));
976 Value
*SP2
= IRB
.CreateVectorSplat(2, Val
);
978 Value
*A
= nullptr, *B
= nullptr, *C
= nullptr;
980 // Test matching insertelement
981 EXPECT_TRUE(match(VI1
, m_InsertElt(m_Value(), m_Value(), m_Value())));
983 match(VI1
, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt())));
985 match(VI1
, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero())));
987 match(VI1
, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero())));
988 EXPECT_TRUE(match(VI2
, m_InsertElt(m_Value(), m_Value(), m_Value())));
990 match(VI2
, m_InsertElt(m_Value(), m_Value(), m_ConstantInt())));
992 match(VI2
, m_InsertElt(m_Value(), m_ConstantInt(), m_Value())));
993 EXPECT_FALSE(match(VI2
, m_InsertElt(m_Constant(), m_Value(), m_Value())));
994 EXPECT_TRUE(match(VI3
, m_InsertElt(m_Value(A
), m_Value(B
), m_Value(C
))));
995 EXPECT_TRUE(A
== VI1
);
996 EXPECT_TRUE(B
== Val2
);
997 EXPECT_TRUE(isa
<ConstantInt
>(C
));
998 A
= B
= C
= nullptr; // reset
1000 // Test matching extractelement
1001 EXPECT_TRUE(match(EX1
, m_ExtractElt(m_Value(A
), m_Value(B
))));
1002 EXPECT_TRUE(A
== VI4
);
1003 EXPECT_TRUE(B
== Val
);
1004 A
= B
= C
= nullptr; // reset
1005 EXPECT_FALSE(match(EX1
, m_ExtractElt(m_Value(), m_ConstantInt())));
1006 EXPECT_TRUE(match(EX2
, m_ExtractElt(m_Value(), m_ConstantInt())));
1007 EXPECT_TRUE(match(EX3
, m_ExtractElt(m_Constant(), m_ConstantInt())));
1009 // Test matching shufflevector
1011 EXPECT_TRUE(match(SI1
, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask())));
1012 EXPECT_TRUE(match(SI2
, m_Shuffle(m_Value(A
), m_Value(B
), m_Mask(Mask
))));
1013 EXPECT_TRUE(A
== VI3
);
1014 EXPECT_TRUE(B
== VI4
);
1015 A
= B
= C
= nullptr; // reset
1017 // Test matching the vector splat pattern
1020 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()),
1021 m_Undef(), m_ZeroMask())));
1023 SI3
, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1024 m_Undef(), m_ZeroMask())));
1026 SI4
, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1027 m_Undef(), m_ZeroMask())));
1030 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()),
1031 m_Undef(), m_ZeroMask())));
1033 SP2
, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A
), m_Zero()),
1034 m_Undef(), m_ZeroMask())));
1035 EXPECT_TRUE(A
== Val
);
1038 TEST_F(PatternMatchTest
, UndefPoisonMix
) {
1039 Type
*ScalarTy
= IRB
.getInt8Ty();
1040 ArrayType
*ArrTy
= ArrayType::get(ScalarTy
, 2);
1041 StructType
*StTy
= StructType::get(ScalarTy
, ScalarTy
);
1042 StructType
*StTy2
= StructType::get(ScalarTy
, StTy
);
1043 StructType
*StTy3
= StructType::get(StTy
, ScalarTy
);
1044 Constant
*Zero
= ConstantInt::getNullValue(ScalarTy
);
1045 UndefValue
*U
= UndefValue::get(ScalarTy
);
1046 UndefValue
*P
= PoisonValue::get(ScalarTy
);
1048 EXPECT_TRUE(match(ConstantVector::get({U
, P
}), m_Undef()));
1049 EXPECT_TRUE(match(ConstantVector::get({P
, U
}), m_Undef()));
1051 EXPECT_TRUE(match(ConstantArray::get(ArrTy
, {U
, P
}), m_Undef()));
1052 EXPECT_TRUE(match(ConstantArray::get(ArrTy
, {P
, U
}), m_Undef()));
1054 auto *UP
= ConstantStruct::get(StTy
, {U
, P
});
1055 EXPECT_TRUE(match(ConstantStruct::get(StTy2
, {U
, UP
}), m_Undef()));
1056 EXPECT_TRUE(match(ConstantStruct::get(StTy2
, {P
, UP
}), m_Undef()));
1057 EXPECT_TRUE(match(ConstantStruct::get(StTy3
, {UP
, U
}), m_Undef()));
1058 EXPECT_TRUE(match(ConstantStruct::get(StTy3
, {UP
, P
}), m_Undef()));
1060 EXPECT_FALSE(match(ConstantStruct::get(StTy
, {U
, Zero
}), m_Undef()));
1061 EXPECT_FALSE(match(ConstantStruct::get(StTy
, {Zero
, U
}), m_Undef()));
1062 EXPECT_FALSE(match(ConstantStruct::get(StTy
, {P
, Zero
}), m_Undef()));
1063 EXPECT_FALSE(match(ConstantStruct::get(StTy
, {Zero
, P
}), m_Undef()));
1065 EXPECT_FALSE(match(ConstantStruct::get(StTy2
, {Zero
, UP
}), m_Undef()));
1066 EXPECT_FALSE(match(ConstantStruct::get(StTy3
, {UP
, Zero
}), m_Undef()));
1069 TEST_F(PatternMatchTest
, VectorUndefInt
) {
1070 Type
*ScalarTy
= IRB
.getInt8Ty();
1071 Type
*VectorTy
= FixedVectorType::get(ScalarTy
, 4);
1072 Constant
*ScalarUndef
= UndefValue::get(ScalarTy
);
1073 Constant
*VectorUndef
= UndefValue::get(VectorTy
);
1074 Constant
*ScalarZero
= Constant::getNullValue(ScalarTy
);
1075 Constant
*VectorZero
= Constant::getNullValue(VectorTy
);
1077 SmallVector
<Constant
*, 4> Elems
;
1078 Elems
.push_back(ScalarUndef
);
1079 Elems
.push_back(ScalarZero
);
1080 Elems
.push_back(ScalarUndef
);
1081 Elems
.push_back(ScalarZero
);
1082 Constant
*VectorZeroUndef
= ConstantVector::get(Elems
);
1084 EXPECT_TRUE(match(ScalarUndef
, m_Undef()));
1085 EXPECT_TRUE(match(VectorUndef
, m_Undef()));
1086 EXPECT_FALSE(match(ScalarZero
, m_Undef()));
1087 EXPECT_FALSE(match(VectorZero
, m_Undef()));
1088 EXPECT_FALSE(match(VectorZeroUndef
, m_Undef()));
1090 EXPECT_FALSE(match(ScalarUndef
, m_Zero()));
1091 EXPECT_FALSE(match(VectorUndef
, m_Zero()));
1092 EXPECT_TRUE(match(ScalarZero
, m_Zero()));
1093 EXPECT_TRUE(match(VectorZero
, m_Zero()));
1094 EXPECT_TRUE(match(VectorZeroUndef
, m_Zero()));
1097 // Regardless of whether undefs are allowed,
1098 // a fully undef constant does not match.
1099 EXPECT_FALSE(match(ScalarUndef
, m_APInt(C
)));
1100 EXPECT_FALSE(match(ScalarUndef
, m_APIntForbidUndef(C
)));
1101 EXPECT_FALSE(match(ScalarUndef
, m_APIntAllowUndef(C
)));
1102 EXPECT_FALSE(match(VectorUndef
, m_APInt(C
)));
1103 EXPECT_FALSE(match(VectorUndef
, m_APIntForbidUndef(C
)));
1104 EXPECT_FALSE(match(VectorUndef
, m_APIntAllowUndef(C
)));
1106 // We can always match simple constants and simple splats.
1108 EXPECT_TRUE(match(ScalarZero
, m_APInt(C
)));
1109 EXPECT_TRUE(C
->isZero());
1111 EXPECT_TRUE(match(ScalarZero
, m_APIntForbidUndef(C
)));
1112 EXPECT_TRUE(C
->isZero());
1114 EXPECT_TRUE(match(ScalarZero
, m_APIntAllowUndef(C
)));
1115 EXPECT_TRUE(C
->isZero());
1117 EXPECT_TRUE(match(VectorZero
, m_APInt(C
)));
1118 EXPECT_TRUE(C
->isZero());
1120 EXPECT_TRUE(match(VectorZero
, m_APIntForbidUndef(C
)));
1121 EXPECT_TRUE(C
->isZero());
1123 EXPECT_TRUE(match(VectorZero
, m_APIntAllowUndef(C
)));
1124 EXPECT_TRUE(C
->isZero());
1126 // Whether splats with undef can be matched depends on the matcher.
1127 EXPECT_FALSE(match(VectorZeroUndef
, m_APInt(C
)));
1128 EXPECT_FALSE(match(VectorZeroUndef
, m_APIntForbidUndef(C
)));
1130 EXPECT_TRUE(match(VectorZeroUndef
, m_APIntAllowUndef(C
)));
1131 EXPECT_TRUE(C
->isZero());
1134 TEST_F(PatternMatchTest
, VectorUndefFloat
) {
1135 Type
*ScalarTy
= IRB
.getFloatTy();
1136 Type
*VectorTy
= FixedVectorType::get(ScalarTy
, 4);
1137 Constant
*ScalarUndef
= UndefValue::get(ScalarTy
);
1138 Constant
*VectorUndef
= UndefValue::get(VectorTy
);
1139 Constant
*ScalarZero
= Constant::getNullValue(ScalarTy
);
1140 Constant
*VectorZero
= Constant::getNullValue(VectorTy
);
1141 Constant
*ScalarPosInf
= ConstantFP::getInfinity(ScalarTy
, false);
1142 Constant
*ScalarNegInf
= ConstantFP::getInfinity(ScalarTy
, true);
1143 Constant
*ScalarNaN
= ConstantFP::getNaN(ScalarTy
, true);
1145 Constant
*VectorZeroUndef
=
1146 ConstantVector::get({ScalarUndef
, ScalarZero
, ScalarUndef
, ScalarZero
});
1148 Constant
*VectorInfUndef
= ConstantVector::get(
1149 {ScalarPosInf
, ScalarNegInf
, ScalarUndef
, ScalarPosInf
});
1151 Constant
*VectorNaNUndef
=
1152 ConstantVector::get({ScalarUndef
, ScalarNaN
, ScalarNaN
, ScalarNaN
});
1154 EXPECT_TRUE(match(ScalarUndef
, m_Undef()));
1155 EXPECT_TRUE(match(VectorUndef
, m_Undef()));
1156 EXPECT_FALSE(match(ScalarZero
, m_Undef()));
1157 EXPECT_FALSE(match(VectorZero
, m_Undef()));
1158 EXPECT_FALSE(match(VectorZeroUndef
, m_Undef()));
1159 EXPECT_FALSE(match(VectorInfUndef
, m_Undef()));
1160 EXPECT_FALSE(match(VectorNaNUndef
, m_Undef()));
1162 EXPECT_FALSE(match(ScalarUndef
, m_AnyZeroFP()));
1163 EXPECT_FALSE(match(VectorUndef
, m_AnyZeroFP()));
1164 EXPECT_TRUE(match(ScalarZero
, m_AnyZeroFP()));
1165 EXPECT_TRUE(match(VectorZero
, m_AnyZeroFP()));
1166 EXPECT_TRUE(match(VectorZeroUndef
, m_AnyZeroFP()));
1167 EXPECT_FALSE(match(VectorInfUndef
, m_AnyZeroFP()));
1168 EXPECT_FALSE(match(VectorNaNUndef
, m_AnyZeroFP()));
1170 EXPECT_FALSE(match(ScalarUndef
, m_NaN()));
1171 EXPECT_FALSE(match(VectorUndef
, m_NaN()));
1172 EXPECT_FALSE(match(VectorZeroUndef
, m_NaN()));
1173 EXPECT_FALSE(match(ScalarPosInf
, m_NaN()));
1174 EXPECT_FALSE(match(ScalarNegInf
, m_NaN()));
1175 EXPECT_TRUE(match(ScalarNaN
, m_NaN()));
1176 EXPECT_FALSE(match(VectorInfUndef
, m_NaN()));
1177 EXPECT_TRUE(match(VectorNaNUndef
, m_NaN()));
1179 EXPECT_FALSE(match(ScalarUndef
, m_NonNaN()));
1180 EXPECT_FALSE(match(VectorUndef
, m_NonNaN()));
1181 EXPECT_TRUE(match(VectorZeroUndef
, m_NonNaN()));
1182 EXPECT_TRUE(match(ScalarPosInf
, m_NonNaN()));
1183 EXPECT_TRUE(match(ScalarNegInf
, m_NonNaN()));
1184 EXPECT_FALSE(match(ScalarNaN
, m_NonNaN()));
1185 EXPECT_TRUE(match(VectorInfUndef
, m_NonNaN()));
1186 EXPECT_FALSE(match(VectorNaNUndef
, m_NonNaN()));
1188 EXPECT_FALSE(match(ScalarUndef
, m_Inf()));
1189 EXPECT_FALSE(match(VectorUndef
, m_Inf()));
1190 EXPECT_FALSE(match(VectorZeroUndef
, m_Inf()));
1191 EXPECT_TRUE(match(ScalarPosInf
, m_Inf()));
1192 EXPECT_TRUE(match(ScalarNegInf
, m_Inf()));
1193 EXPECT_FALSE(match(ScalarNaN
, m_Inf()));
1194 EXPECT_TRUE(match(VectorInfUndef
, m_Inf()));
1195 EXPECT_FALSE(match(VectorNaNUndef
, m_Inf()));
1197 EXPECT_FALSE(match(ScalarUndef
, m_NonInf()));
1198 EXPECT_FALSE(match(VectorUndef
, m_NonInf()));
1199 EXPECT_TRUE(match(VectorZeroUndef
, m_NonInf()));
1200 EXPECT_FALSE(match(ScalarPosInf
, m_NonInf()));
1201 EXPECT_FALSE(match(ScalarNegInf
, m_NonInf()));
1202 EXPECT_TRUE(match(ScalarNaN
, m_NonInf()));
1203 EXPECT_FALSE(match(VectorInfUndef
, m_NonInf()));
1204 EXPECT_TRUE(match(VectorNaNUndef
, m_NonInf()));
1206 EXPECT_FALSE(match(ScalarUndef
, m_Finite()));
1207 EXPECT_FALSE(match(VectorUndef
, m_Finite()));
1208 EXPECT_TRUE(match(VectorZeroUndef
, m_Finite()));
1209 EXPECT_FALSE(match(ScalarPosInf
, m_Finite()));
1210 EXPECT_FALSE(match(ScalarNegInf
, m_Finite()));
1211 EXPECT_FALSE(match(ScalarNaN
, m_Finite()));
1212 EXPECT_FALSE(match(VectorInfUndef
, m_Finite()));
1213 EXPECT_FALSE(match(VectorNaNUndef
, m_Finite()));
1216 // Regardless of whether undefs are allowed,
1217 // a fully undef constant does not match.
1218 EXPECT_FALSE(match(ScalarUndef
, m_APFloat(C
)));
1219 EXPECT_FALSE(match(ScalarUndef
, m_APFloatForbidUndef(C
)));
1220 EXPECT_FALSE(match(ScalarUndef
, m_APFloatAllowUndef(C
)));
1221 EXPECT_FALSE(match(VectorUndef
, m_APFloat(C
)));
1222 EXPECT_FALSE(match(VectorUndef
, m_APFloatForbidUndef(C
)));
1223 EXPECT_FALSE(match(VectorUndef
, m_APFloatAllowUndef(C
)));
1225 // We can always match simple constants and simple splats.
1227 EXPECT_TRUE(match(ScalarZero
, m_APFloat(C
)));
1228 EXPECT_TRUE(C
->isZero());
1230 EXPECT_TRUE(match(ScalarZero
, m_APFloatForbidUndef(C
)));
1231 EXPECT_TRUE(C
->isZero());
1233 EXPECT_TRUE(match(ScalarZero
, m_APFloatAllowUndef(C
)));
1234 EXPECT_TRUE(C
->isZero());
1236 EXPECT_TRUE(match(VectorZero
, m_APFloat(C
)));
1237 EXPECT_TRUE(C
->isZero());
1239 EXPECT_TRUE(match(VectorZero
, m_APFloatForbidUndef(C
)));
1240 EXPECT_TRUE(C
->isZero());
1242 EXPECT_TRUE(match(VectorZero
, m_APFloatAllowUndef(C
)));
1243 EXPECT_TRUE(C
->isZero());
1245 // Whether splats with undef can be matched depends on the matcher.
1246 EXPECT_FALSE(match(VectorZeroUndef
, m_APFloat(C
)));
1247 EXPECT_FALSE(match(VectorZeroUndef
, m_APFloatForbidUndef(C
)));
1249 EXPECT_TRUE(match(VectorZeroUndef
, m_APFloatAllowUndef(C
)));
1250 EXPECT_TRUE(C
->isZero());
1252 EXPECT_TRUE(match(VectorZeroUndef
, m_Finite(C
)));
1253 EXPECT_TRUE(C
->isZero());
1256 TEST_F(PatternMatchTest
, FloatingPointFNeg
) {
1257 Type
*FltTy
= IRB
.getFloatTy();
1258 Value
*One
= ConstantFP::get(FltTy
, 1.0);
1259 Value
*Z
= ConstantFP::get(FltTy
, 0.0);
1260 Value
*NZ
= ConstantFP::get(FltTy
, -0.0);
1261 Value
*V
= IRB
.CreateFNeg(One
);
1262 Value
*V1
= IRB
.CreateFSub(NZ
, One
);
1263 Value
*V2
= IRB
.CreateFSub(Z
, One
);
1264 Value
*V3
= IRB
.CreateFAdd(NZ
, One
);
1268 EXPECT_TRUE(match(V
, m_FNeg(m_Value(Match
))));
1269 EXPECT_EQ(One
, Match
);
1271 // Test FSub(-0.0, 1.0)
1272 EXPECT_TRUE(match(V1
, m_FNeg(m_Value(Match
))));
1273 EXPECT_EQ(One
, Match
);
1275 // Test FSub(0.0, 1.0)
1276 EXPECT_FALSE(match(V2
, m_FNeg(m_Value(Match
))));
1277 cast
<Instruction
>(V2
)->setHasNoSignedZeros(true);
1278 EXPECT_TRUE(match(V2
, m_FNeg(m_Value(Match
))));
1279 EXPECT_EQ(One
, Match
);
1281 // Test FAdd(-0.0, 1.0)
1282 EXPECT_FALSE(match(V3
, m_FNeg(m_Value(Match
))));
1285 TEST_F(PatternMatchTest
, CondBranchTest
) {
1286 BasicBlock
*TrueBB
= BasicBlock::Create(Ctx
, "TrueBB", F
);
1287 BasicBlock
*FalseBB
= BasicBlock::Create(Ctx
, "FalseBB", F
);
1288 Value
*Br1
= IRB
.CreateCondBr(IRB
.getTrue(), TrueBB
, FalseBB
);
1290 EXPECT_TRUE(match(Br1
, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1293 EXPECT_TRUE(match(Br1
, m_Br(m_Value(), m_BasicBlock(A
), m_BasicBlock(B
))));
1294 EXPECT_EQ(TrueBB
, A
);
1295 EXPECT_EQ(FalseBB
, B
);
1298 match(Br1
, m_Br(m_Value(), m_SpecificBB(FalseBB
), m_BasicBlock())));
1300 match(Br1
, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB
))));
1302 match(Br1
, m_Br(m_Value(), m_SpecificBB(FalseBB
), m_BasicBlock(TrueBB
))));
1304 match(Br1
, m_Br(m_Value(), m_SpecificBB(TrueBB
), m_BasicBlock(FalseBB
))));
1306 // Check we can use m_Deferred with branches.
1307 EXPECT_FALSE(match(Br1
, m_Br(m_Value(), m_BasicBlock(A
), m_Deferred(A
))));
1308 Value
*Br2
= IRB
.CreateCondBr(IRB
.getTrue(), TrueBB
, TrueBB
);
1310 EXPECT_TRUE(match(Br2
, m_Br(m_Value(), m_BasicBlock(A
), m_Deferred(A
))));
1313 TEST_F(PatternMatchTest
, WithOverflowInst
) {
1314 Value
*Add
= IRB
.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow
,
1315 IRB
.getInt32(0), IRB
.getInt32(0));
1316 Value
*Add0
= IRB
.CreateExtractValue(Add
, 0);
1317 Value
*Add1
= IRB
.CreateExtractValue(Add
, 1);
1319 EXPECT_TRUE(match(Add0
, m_ExtractValue
<0>(m_Value())));
1320 EXPECT_FALSE(match(Add0
, m_ExtractValue
<1>(m_Value())));
1321 EXPECT_FALSE(match(Add1
, m_ExtractValue
<0>(m_Value())));
1322 EXPECT_TRUE(match(Add1
, m_ExtractValue
<1>(m_Value())));
1323 EXPECT_FALSE(match(Add
, m_ExtractValue
<1>(m_Value())));
1324 EXPECT_FALSE(match(Add
, m_ExtractValue
<1>(m_Value())));
1326 WithOverflowInst
*WOI
;
1327 EXPECT_FALSE(match(Add0
, m_WithOverflowInst(WOI
)));
1328 EXPECT_FALSE(match(Add1
, m_WithOverflowInst(WOI
)));
1329 EXPECT_TRUE(match(Add
, m_WithOverflowInst(WOI
)));
1331 EXPECT_TRUE(match(Add0
, m_ExtractValue
<0>(m_WithOverflowInst(WOI
))));
1332 EXPECT_EQ(Add
, WOI
);
1333 EXPECT_TRUE(match(Add1
, m_ExtractValue
<1>(m_WithOverflowInst(WOI
))));
1334 EXPECT_EQ(Add
, WOI
);
1337 TEST_F(PatternMatchTest
, MinMaxIntrinsics
) {
1338 Type
*Ty
= IRB
.getInt32Ty();
1339 Value
*L
= ConstantInt::get(Ty
, 1);
1340 Value
*R
= ConstantInt::get(Ty
, 2);
1341 Value
*MatchL
, *MatchR
;
1343 // Check for intrinsic ID match and capture of operands.
1344 EXPECT_TRUE(m_SMax(m_Value(MatchL
), m_Value(MatchR
))
1345 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::smax
, L
, R
)));
1346 EXPECT_EQ(L
, MatchL
);
1347 EXPECT_EQ(R
, MatchR
);
1349 EXPECT_TRUE(m_SMin(m_Value(MatchL
), m_Value(MatchR
))
1350 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::smin
, L
, R
)));
1351 EXPECT_EQ(L
, MatchL
);
1352 EXPECT_EQ(R
, MatchR
);
1354 EXPECT_TRUE(m_UMax(m_Value(MatchL
), m_Value(MatchR
))
1355 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::umax
, L
, R
)));
1356 EXPECT_EQ(L
, MatchL
);
1357 EXPECT_EQ(R
, MatchR
);
1359 EXPECT_TRUE(m_UMin(m_Value(MatchL
), m_Value(MatchR
))
1360 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::umin
, L
, R
)));
1361 EXPECT_EQ(L
, MatchL
);
1362 EXPECT_EQ(R
, MatchR
);
1364 // Check for intrinsic ID mismatch.
1365 EXPECT_FALSE(m_SMax(m_Value(MatchL
), m_Value(MatchR
))
1366 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::smin
, L
, R
)));
1367 EXPECT_FALSE(m_SMin(m_Value(MatchL
), m_Value(MatchR
))
1368 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::umax
, L
, R
)));
1369 EXPECT_FALSE(m_UMax(m_Value(MatchL
), m_Value(MatchR
))
1370 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::umin
, L
, R
)));
1371 EXPECT_FALSE(m_UMin(m_Value(MatchL
), m_Value(MatchR
))
1372 .match(IRB
.CreateBinaryIntrinsic(Intrinsic::smax
, L
, R
)));
1375 TEST_F(PatternMatchTest
, IntrinsicMatcher
) {
1376 Value
*Name
= IRB
.CreateAlloca(IRB
.getInt8Ty());
1377 Value
*Hash
= IRB
.getInt64(0);
1378 Value
*Num
= IRB
.getInt32(1);
1379 Value
*Index
= IRB
.getInt32(2);
1380 Value
*Step
= IRB
.getInt64(3);
1382 Value
*Ops
[] = {Name
, Hash
, Num
, Index
, Step
};
1383 Module
*M
= BB
->getParent()->getParent();
1385 Intrinsic::getDeclaration(M
, Intrinsic::instrprof_increment_step
);
1387 Value
*Intrinsic5
= CallInst::Create(TheFn
, Ops
, "", BB
);
1389 // Match without capturing.
1391 Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1392 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1394 Intrinsic5
, m_Intrinsic
<Intrinsic::memmove
>(
1395 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1397 // Match with capturing.
1398 Value
*Arg1
= nullptr;
1399 Value
*Arg2
= nullptr;
1400 Value
*Arg3
= nullptr;
1401 Value
*Arg4
= nullptr;
1402 Value
*Arg5
= nullptr;
1404 match(Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1405 m_Value(Arg1
), m_Value(Arg2
), m_Value(Arg3
),
1406 m_Value(Arg4
), m_Value(Arg5
))));
1407 EXPECT_EQ(Arg1
, Name
);
1408 EXPECT_EQ(Arg2
, Hash
);
1409 EXPECT_EQ(Arg3
, Num
);
1410 EXPECT_EQ(Arg4
, Index
);
1411 EXPECT_EQ(Arg5
, Step
);
1413 // Match specific second argument.
1416 m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1417 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1419 match(Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1420 m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1423 // Match specific third argument.
1426 m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1427 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1429 match(Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1430 m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1433 // Match specific fourth argument.
1436 m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1437 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1439 match(Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1440 m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1443 // Match specific fifth argument.
1446 m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1447 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1449 match(Intrinsic5
, m_Intrinsic
<Intrinsic::instrprof_increment_step
>(
1450 m_Value(), m_Value(), m_Value(), m_Value(),
1451 m_SpecificInt(10))));
1456 struct is_unsigned_zero_pred
{
1457 bool isValue(const APInt
&C
) { return C
.isZero(); }
1460 struct is_float_zero_pred
{
1461 bool isValue(const APFloat
&C
) { return C
.isZero(); }
1464 template <typename T
> struct always_true_pred
{
1465 bool isValue(const T
&) { return true; }
1468 template <typename T
> struct always_false_pred
{
1469 bool isValue(const T
&) { return false; }
1472 struct is_unsigned_max_pred
{
1473 bool isValue(const APInt
&C
) { return C
.isMaxValue(); }
1476 struct is_float_nan_pred
{
1477 bool isValue(const APFloat
&C
) { return C
.isNaN(); }
1482 TEST_F(PatternMatchTest
, ConstantPredicateType
) {
1485 APInt U32Max
= APInt::getAllOnes(32);
1486 APInt U32Zero
= APInt::getZero(32);
1487 APInt
U32DeadBeef(32, 0xDEADBEEF);
1489 Type
*U32Ty
= Type::getInt32Ty(Ctx
);
1491 Constant
*CU32Max
= Constant::getIntegerValue(U32Ty
, U32Max
);
1492 Constant
*CU32Zero
= Constant::getIntegerValue(U32Ty
, U32Zero
);
1493 Constant
*CU32DeadBeef
= Constant::getIntegerValue(U32Ty
, U32DeadBeef
);
1495 EXPECT_TRUE(match(CU32Max
, cst_pred_ty
<is_unsigned_max_pred
>()));
1496 EXPECT_FALSE(match(CU32Max
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1497 EXPECT_TRUE(match(CU32Max
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1498 EXPECT_FALSE(match(CU32Max
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1500 EXPECT_FALSE(match(CU32Zero
, cst_pred_ty
<is_unsigned_max_pred
>()));
1501 EXPECT_TRUE(match(CU32Zero
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1502 EXPECT_TRUE(match(CU32Zero
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1503 EXPECT_FALSE(match(CU32Zero
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1505 EXPECT_FALSE(match(CU32DeadBeef
, cst_pred_ty
<is_unsigned_max_pred
>()));
1506 EXPECT_FALSE(match(CU32DeadBeef
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1507 EXPECT_TRUE(match(CU32DeadBeef
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1508 EXPECT_FALSE(match(CU32DeadBeef
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1511 APFloat F32NaN
= APFloat::getNaN(APFloat::IEEEsingle());
1512 APFloat F32Zero
= APFloat::getZero(APFloat::IEEEsingle());
1513 APFloat
F32Pi(3.14f
);
1515 Type
*F32Ty
= Type::getFloatTy(Ctx
);
1517 Constant
*CF32NaN
= ConstantFP::get(F32Ty
, F32NaN
);
1518 Constant
*CF32Zero
= ConstantFP::get(F32Ty
, F32Zero
);
1519 Constant
*CF32Pi
= ConstantFP::get(F32Ty
, F32Pi
);
1521 EXPECT_TRUE(match(CF32NaN
, cstfp_pred_ty
<is_float_nan_pred
>()));
1522 EXPECT_FALSE(match(CF32NaN
, cstfp_pred_ty
<is_float_zero_pred
>()));
1523 EXPECT_TRUE(match(CF32NaN
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1524 EXPECT_FALSE(match(CF32NaN
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1526 EXPECT_FALSE(match(CF32Zero
, cstfp_pred_ty
<is_float_nan_pred
>()));
1527 EXPECT_TRUE(match(CF32Zero
, cstfp_pred_ty
<is_float_zero_pred
>()));
1528 EXPECT_TRUE(match(CF32Zero
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1529 EXPECT_FALSE(match(CF32Zero
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1531 EXPECT_FALSE(match(CF32Pi
, cstfp_pred_ty
<is_float_nan_pred
>()));
1532 EXPECT_FALSE(match(CF32Pi
, cstfp_pred_ty
<is_float_zero_pred
>()));
1533 EXPECT_TRUE(match(CF32Pi
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1534 EXPECT_FALSE(match(CF32Pi
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1536 auto FixedEC
= ElementCount::getFixed(4);
1537 auto ScalableEC
= ElementCount::getScalable(4);
1541 for (auto EC
: {FixedEC
, ScalableEC
}) {
1544 Constant
*CSplatU32Max
= ConstantVector::getSplat(EC
, CU32Max
);
1545 Constant
*CSplatU32Zero
= ConstantVector::getSplat(EC
, CU32Zero
);
1546 Constant
*CSplatU32DeadBeef
= ConstantVector::getSplat(EC
, CU32DeadBeef
);
1548 EXPECT_TRUE(match(CSplatU32Max
, cst_pred_ty
<is_unsigned_max_pred
>()));
1549 EXPECT_FALSE(match(CSplatU32Max
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1550 EXPECT_TRUE(match(CSplatU32Max
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1551 EXPECT_FALSE(match(CSplatU32Max
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1553 EXPECT_FALSE(match(CSplatU32Zero
, cst_pred_ty
<is_unsigned_max_pred
>()));
1554 EXPECT_TRUE(match(CSplatU32Zero
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1555 EXPECT_TRUE(match(CSplatU32Zero
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1556 EXPECT_FALSE(match(CSplatU32Zero
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1558 EXPECT_FALSE(match(CSplatU32DeadBeef
, cst_pred_ty
<is_unsigned_max_pred
>()));
1560 match(CSplatU32DeadBeef
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1562 match(CSplatU32DeadBeef
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1564 match(CSplatU32DeadBeef
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1568 Constant
*CSplatF32NaN
= ConstantVector::getSplat(EC
, CF32NaN
);
1569 Constant
*CSplatF32Zero
= ConstantVector::getSplat(EC
, CF32Zero
);
1570 Constant
*CSplatF32Pi
= ConstantVector::getSplat(EC
, CF32Pi
);
1572 EXPECT_TRUE(match(CSplatF32NaN
, cstfp_pred_ty
<is_float_nan_pred
>()));
1573 EXPECT_FALSE(match(CSplatF32NaN
, cstfp_pred_ty
<is_float_zero_pred
>()));
1575 match(CSplatF32NaN
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1577 match(CSplatF32NaN
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1579 EXPECT_FALSE(match(CSplatF32Zero
, cstfp_pred_ty
<is_float_nan_pred
>()));
1580 EXPECT_TRUE(match(CSplatF32Zero
, cstfp_pred_ty
<is_float_zero_pred
>()));
1582 match(CSplatF32Zero
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1584 match(CSplatF32Zero
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1586 EXPECT_FALSE(match(CSplatF32Pi
, cstfp_pred_ty
<is_float_nan_pred
>()));
1587 EXPECT_FALSE(match(CSplatF32Pi
, cstfp_pred_ty
<is_float_zero_pred
>()));
1588 EXPECT_TRUE(match(CSplatF32Pi
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1590 match(CSplatF32Pi
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1593 // Int arbitrary vector
1595 Constant
*CMixedU32
= ConstantVector::get({CU32Max
, CU32Zero
, CU32DeadBeef
});
1596 Constant
*CU32Undef
= UndefValue::get(U32Ty
);
1597 Constant
*CU32MaxWithUndef
=
1598 ConstantVector::get({CU32Undef
, CU32Max
, CU32Undef
});
1600 EXPECT_FALSE(match(CMixedU32
, cst_pred_ty
<is_unsigned_max_pred
>()));
1601 EXPECT_FALSE(match(CMixedU32
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1602 EXPECT_TRUE(match(CMixedU32
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1603 EXPECT_FALSE(match(CMixedU32
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1605 EXPECT_TRUE(match(CU32MaxWithUndef
, cst_pred_ty
<is_unsigned_max_pred
>()));
1606 EXPECT_FALSE(match(CU32MaxWithUndef
, cst_pred_ty
<is_unsigned_zero_pred
>()));
1607 EXPECT_TRUE(match(CU32MaxWithUndef
, cst_pred_ty
<always_true_pred
<APInt
>>()));
1609 match(CU32MaxWithUndef
, cst_pred_ty
<always_false_pred
<APInt
>>()));
1611 // Float arbitrary vector
1613 Constant
*CMixedF32
= ConstantVector::get({CF32NaN
, CF32Zero
, CF32Pi
});
1614 Constant
*CF32Undef
= UndefValue::get(F32Ty
);
1615 Constant
*CF32NaNWithUndef
=
1616 ConstantVector::get({CF32Undef
, CF32NaN
, CF32Undef
});
1618 EXPECT_FALSE(match(CMixedF32
, cstfp_pred_ty
<is_float_nan_pred
>()));
1619 EXPECT_FALSE(match(CMixedF32
, cstfp_pred_ty
<is_float_zero_pred
>()));
1620 EXPECT_TRUE(match(CMixedF32
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1621 EXPECT_FALSE(match(CMixedF32
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1623 EXPECT_TRUE(match(CF32NaNWithUndef
, cstfp_pred_ty
<is_float_nan_pred
>()));
1624 EXPECT_FALSE(match(CF32NaNWithUndef
, cstfp_pred_ty
<is_float_zero_pred
>()));
1626 match(CF32NaNWithUndef
, cstfp_pred_ty
<always_true_pred
<APFloat
>>()));
1628 match(CF32NaNWithUndef
, cstfp_pred_ty
<always_false_pred
<APFloat
>>()));
1631 TEST_F(PatternMatchTest
, InsertValue
) {
1632 Type
*StructTy
= StructType::create(IRB
.getContext(),
1633 {IRB
.getInt32Ty(), IRB
.getInt64Ty()});
1635 IRB
.CreateInsertValue(UndefValue::get(StructTy
), IRB
.getInt32(20), 0);
1636 Value
*Ins1
= IRB
.CreateInsertValue(Ins0
, IRB
.getInt64(90), 1);
1638 EXPECT_TRUE(match(Ins0
, m_InsertValue
<0>(m_Value(), m_Value())));
1639 EXPECT_FALSE(match(Ins0
, m_InsertValue
<1>(m_Value(), m_Value())));
1640 EXPECT_FALSE(match(Ins1
, m_InsertValue
<0>(m_Value(), m_Value())));
1641 EXPECT_TRUE(match(Ins1
, m_InsertValue
<1>(m_Value(), m_Value())));
1643 EXPECT_TRUE(match(Ins0
, m_InsertValue
<0>(m_Undef(), m_SpecificInt(20))));
1644 EXPECT_FALSE(match(Ins0
, m_InsertValue
<0>(m_Undef(), m_SpecificInt(0))));
1647 match(Ins1
, m_InsertValue
<1>(m_InsertValue
<0>(m_Value(), m_Value()),
1648 m_SpecificInt(90))));
1649 EXPECT_FALSE(match(IRB
.getInt64(99), m_InsertValue
<0>(m_Value(), m_Value())));
1652 TEST_F(PatternMatchTest
, LogicalSelects
) {
1653 Value
*Alloca
= IRB
.CreateAlloca(IRB
.getInt1Ty());
1654 Value
*X
= IRB
.CreateLoad(IRB
.getInt1Ty(), Alloca
);
1655 Value
*Y
= IRB
.CreateLoad(IRB
.getInt1Ty(), Alloca
);
1656 Constant
*T
= IRB
.getInt1(true);
1657 Constant
*F
= IRB
.getInt1(false);
1658 Value
*And
= IRB
.CreateSelect(X
, Y
, F
);
1659 Value
*Or
= IRB
.CreateSelect(X
, T
, Y
);
1662 // Check basic no-capture logic - opcode and constant must match.
1663 EXPECT_TRUE(match(And
, m_LogicalAnd(m_Value(), m_Value())));
1664 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Value(), m_Value())));
1665 EXPECT_FALSE(match(And
, m_LogicalOr(m_Value(), m_Value())));
1666 EXPECT_FALSE(match(And
, m_c_LogicalOr(m_Value(), m_Value())));
1668 // Check with captures.
1669 EXPECT_TRUE(match(And
, m_LogicalAnd(m_Specific(X
), m_Value())));
1670 EXPECT_TRUE(match(And
, m_LogicalAnd(m_Value(), m_Specific(Y
))));
1671 EXPECT_TRUE(match(And
, m_LogicalAnd(m_Specific(X
), m_Specific(Y
))));
1673 EXPECT_FALSE(match(And
, m_LogicalAnd(m_Specific(Y
), m_Value())));
1674 EXPECT_FALSE(match(And
, m_LogicalAnd(m_Value(), m_Specific(X
))));
1675 EXPECT_FALSE(match(And
, m_LogicalAnd(m_Specific(Y
), m_Specific(X
))));
1677 EXPECT_FALSE(match(And
, m_LogicalAnd(m_Specific(X
), m_Specific(X
))));
1678 EXPECT_FALSE(match(And
, m_LogicalAnd(m_Specific(Y
), m_Specific(Y
))));
1680 // Check captures for commutative match.
1681 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Specific(X
), m_Value())));
1682 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Value(), m_Specific(Y
))));
1683 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Specific(X
), m_Specific(Y
))));
1685 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Specific(Y
), m_Value())));
1686 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Value(), m_Specific(X
))));
1687 EXPECT_TRUE(match(And
, m_c_LogicalAnd(m_Specific(Y
), m_Specific(X
))));
1689 EXPECT_FALSE(match(And
, m_c_LogicalAnd(m_Specific(X
), m_Specific(X
))));
1690 EXPECT_FALSE(match(And
, m_c_LogicalAnd(m_Specific(Y
), m_Specific(Y
))));
1693 // Check basic no-capture logic - opcode and constant must match.
1694 EXPECT_TRUE(match(Or
, m_LogicalOr(m_Value(), m_Value())));
1695 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Value(), m_Value())));
1696 EXPECT_FALSE(match(Or
, m_LogicalAnd(m_Value(), m_Value())));
1697 EXPECT_FALSE(match(Or
, m_c_LogicalAnd(m_Value(), m_Value())));
1699 // Check with captures.
1700 EXPECT_TRUE(match(Or
, m_LogicalOr(m_Specific(X
), m_Value())));
1701 EXPECT_TRUE(match(Or
, m_LogicalOr(m_Value(), m_Specific(Y
))));
1702 EXPECT_TRUE(match(Or
, m_LogicalOr(m_Specific(X
), m_Specific(Y
))));
1704 EXPECT_FALSE(match(Or
, m_LogicalOr(m_Specific(Y
), m_Value())));
1705 EXPECT_FALSE(match(Or
, m_LogicalOr(m_Value(), m_Specific(X
))));
1706 EXPECT_FALSE(match(Or
, m_LogicalOr(m_Specific(Y
), m_Specific(X
))));
1708 EXPECT_FALSE(match(Or
, m_LogicalOr(m_Specific(X
), m_Specific(X
))));
1709 EXPECT_FALSE(match(Or
, m_LogicalOr(m_Specific(Y
), m_Specific(Y
))));
1711 // Check captures for commutative match.
1712 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Specific(X
), m_Value())));
1713 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Value(), m_Specific(Y
))));
1714 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Specific(X
), m_Specific(Y
))));
1716 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Specific(Y
), m_Value())));
1717 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Value(), m_Specific(X
))));
1718 EXPECT_TRUE(match(Or
, m_c_LogicalOr(m_Specific(Y
), m_Specific(X
))));
1720 EXPECT_FALSE(match(Or
, m_c_LogicalOr(m_Specific(X
), m_Specific(X
))));
1721 EXPECT_FALSE(match(Or
, m_c_LogicalOr(m_Specific(Y
), m_Specific(Y
))));
1724 TEST_F(PatternMatchTest
, VectorLogicalSelects
) {
1725 Type
*i1
= IRB
.getInt1Ty();
1726 Type
*v3i1
= FixedVectorType::get(i1
, 3);
1728 Value
*Alloca
= IRB
.CreateAlloca(i1
);
1729 Value
*AllocaVec
= IRB
.CreateAlloca(v3i1
);
1730 Value
*Scalar
= IRB
.CreateLoad(i1
, Alloca
);
1731 Value
*Vector
= IRB
.CreateLoad(v3i1
, AllocaVec
);
1732 Constant
*F
= Constant::getNullValue(v3i1
);
1733 Constant
*T
= Constant::getAllOnesValue(v3i1
);
1735 // select <3 x i1> Vector, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
1736 Value
*VecAnd
= IRB
.CreateSelect(Vector
, Vector
, F
);
1738 // select i1 Scalar, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
1739 Value
*MixedTypeAnd
= IRB
.CreateSelect(Scalar
, Vector
, F
);
1741 // select <3 x i1> Vector, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
1742 Value
*VecOr
= IRB
.CreateSelect(Vector
, T
, Vector
);
1744 // select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
1745 Value
*MixedTypeOr
= IRB
.CreateSelect(Scalar
, T
, Vector
);
1747 // We allow matching a real vector logical select,
1748 // but not a scalar select of vector bools.
1749 EXPECT_TRUE(match(VecAnd
, m_LogicalAnd(m_Value(), m_Value())));
1750 EXPECT_FALSE(match(MixedTypeAnd
, m_LogicalAnd(m_Value(), m_Value())));
1751 EXPECT_TRUE(match(VecOr
, m_LogicalOr(m_Value(), m_Value())));
1752 EXPECT_FALSE(match(MixedTypeOr
, m_LogicalOr(m_Value(), m_Value())));
1755 TEST_F(PatternMatchTest
, VScale
) {
1756 DataLayout DL
= M
->getDataLayout();
1758 Type
*VecTy
= ScalableVectorType::get(IRB
.getInt8Ty(), 1);
1760 Constant::getNullValue(PointerType::getUnqual(VecTy
->getContext()));
1761 Value
*GEP
= IRB
.CreateGEP(VecTy
, NullPtrVec
, IRB
.getInt64(1));
1762 Value
*PtrToInt
= IRB
.CreatePtrToInt(GEP
, DL
.getIntPtrType(GEP
->getType()));
1763 EXPECT_TRUE(match(PtrToInt
, m_VScale()));
1765 Type
*VecTy2
= ScalableVectorType::get(IRB
.getInt8Ty(), 2);
1766 Value
*NullPtrVec2
=
1767 Constant::getNullValue(PointerType::getUnqual(VecTy2
->getContext()));
1768 Value
*GEP2
= IRB
.CreateGEP(VecTy
, NullPtrVec2
, IRB
.getInt64(1));
1770 IRB
.CreatePtrToInt(GEP2
, DL
.getIntPtrType(GEP2
->getType()));
1771 EXPECT_TRUE(match(PtrToInt2
, m_VScale()));
1774 TEST_F(PatternMatchTest
, NotForbidUndef
) {
1775 Type
*ScalarTy
= IRB
.getInt8Ty();
1776 Type
*VectorTy
= FixedVectorType::get(ScalarTy
, 3);
1777 Constant
*ScalarUndef
= UndefValue::get(ScalarTy
);
1778 Constant
*ScalarOnes
= Constant::getAllOnesValue(ScalarTy
);
1779 Constant
*VectorZero
= Constant::getNullValue(VectorTy
);
1780 Constant
*VectorOnes
= Constant::getAllOnesValue(VectorTy
);
1782 SmallVector
<Constant
*, 3> MixedElems
;
1783 MixedElems
.push_back(ScalarOnes
);
1784 MixedElems
.push_back(ScalarOnes
);
1785 MixedElems
.push_back(ScalarUndef
);
1786 Constant
*VectorMixed
= ConstantVector::get(MixedElems
);
1788 Value
*Not
= IRB
.CreateXor(VectorZero
, VectorOnes
);
1790 EXPECT_TRUE(match(Not
, m_Not(m_Value())));
1791 EXPECT_TRUE(match(Not
, m_NotForbidUndef(m_Value(X
))));
1792 EXPECT_TRUE(match(X
, m_Zero()));
1794 Value
*NotCommute
= IRB
.CreateXor(VectorOnes
, VectorZero
);
1796 EXPECT_TRUE(match(NotCommute
, m_Not(m_Value())));
1797 EXPECT_TRUE(match(NotCommute
, m_NotForbidUndef(m_Value(Y
))));
1798 EXPECT_TRUE(match(Y
, m_Zero()));
1800 Value
*NotWithUndefs
= IRB
.CreateXor(VectorZero
, VectorMixed
);
1801 EXPECT_TRUE(match(NotWithUndefs
, m_Not(m_Value())));
1802 EXPECT_FALSE(match(NotWithUndefs
, m_NotForbidUndef(m_Value())));
1804 Value
*NotWithUndefsCommute
= IRB
.CreateXor(VectorMixed
, VectorZero
);
1805 EXPECT_TRUE(match(NotWithUndefsCommute
, m_Not(m_Value())));
1806 EXPECT_FALSE(match(NotWithUndefsCommute
, m_NotForbidUndef(m_Value(X
))));
1809 template <typename T
> struct MutableConstTest
: PatternMatchTest
{ };
1811 typedef ::testing::Types
<std::tuple
<Value
*, Instruction
*>,
1812 std::tuple
<const Value
*, const Instruction
*>>
1813 MutableConstTestTypes
;
1814 TYPED_TEST_SUITE(MutableConstTest
, MutableConstTestTypes
, );
1816 TYPED_TEST(MutableConstTest
, ICmp
) {
1817 auto &IRB
= PatternMatchTest::IRB
;
1819 typedef std::tuple_element_t
<0, TypeParam
> ValueType
;
1820 typedef std::tuple_element_t
<1, TypeParam
> InstructionType
;
1822 Value
*L
= IRB
.getInt32(1);
1823 Value
*R
= IRB
.getInt32(2);
1824 ICmpInst::Predicate Pred
= ICmpInst::ICMP_UGT
;
1828 ICmpInst::Predicate MatchPred
;
1830 EXPECT_TRUE(m_ICmp(MatchPred
, m_Value(MatchL
), m_Value(MatchR
))
1831 .match((InstructionType
)IRB
.CreateICmp(Pred
, L
, R
)));
1832 EXPECT_EQ(L
, MatchL
);
1833 EXPECT_EQ(R
, MatchR
);
1836 TEST_F(PatternMatchTest
, ConstExpr
) {
1838 M
->getOrInsertGlobal("dummy", PointerType::getUnqual(IRB
.getInt32Ty()));
1839 Constant
*S
= ConstantExpr::getPtrToInt(G
, IRB
.getInt32Ty());
1840 Type
*VecTy
= FixedVectorType::get(IRB
.getInt32Ty(), 2);
1841 PoisonValue
*P
= PoisonValue::get(VecTy
);
1842 Constant
*V
= ConstantExpr::getInsertElement(P
, S
, IRB
.getInt32(0));
1844 // The match succeeds on a constant that is a constant expression itself
1845 // or a constant that contains a constant expression.
1846 EXPECT_TRUE(match(S
, m_ConstantExpr()));
1847 EXPECT_TRUE(match(V
, m_ConstantExpr()));
1850 } // anonymous namespace.