Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / IR / PatternMatch.cpp
blob367ba6ab52a596569811fce020d3040a2a91d8f9
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 //===----------------------------------------------------------------------===//
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"
28 using namespace llvm;
29 using namespace llvm::PatternMatch;
31 namespace {
33 struct PatternMatchTest : ::testing::Test {
34 LLVMContext Ctx;
35 std::unique_ptr<Module> M;
36 Function *F;
37 BasicBlock *BB;
38 IRBuilder<NoFolder> IRB;
40 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) {}
48 TEST_F(PatternMatchTest, OneUse) {
49 // Build up a little tree of values:
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;
61 EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
62 EXPECT_EQ(One, V);
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 = Constant::getAllOnesValue(IntTy);
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));
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));
96 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
97 APInt::getAllOnes(BitWidth))
98 .match(Zero));
99 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
100 APInt::getAllOnes(BitWidth))
101 .match(One));
102 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
103 APInt::getAllOnes(BitWidth))
104 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
135 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
136 APInt::getAllOnes(BitWidth))
137 .match(Zero));
138 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
139 APInt::getAllOnes(BitWidth))
140 .match(One));
141 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
142 APInt::getAllOnes(BitWidth))
143 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
174 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT,
175 APInt::getAllOnes(BitWidth))
176 .match(Zero));
177 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT,
178 APInt::getAllOnes(BitWidth))
179 .match(One));
180 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT,
181 APInt::getAllOnes(BitWidth))
182 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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 = Constant::getAllOnesValue(IntTy);
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));
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));
236 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
237 APInt::getAllOnes(BitWidth))
238 .match(Zero));
239 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
240 APInt::getAllOnes(BitWidth))
241 .match(One));
242 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
243 APInt::getAllOnes(BitWidth))
244 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
275 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
276 APInt::getAllOnes(BitWidth))
277 .match(Zero));
278 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
279 APInt::getAllOnes(BitWidth))
280 .match(One));
281 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
282 APInt::getAllOnes(BitWidth))
283 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
314 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE,
315 APInt::getAllOnes(BitWidth))
316 .match(Zero));
317 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE,
318 APInt::getAllOnes(BitWidth))
319 .match(One));
320 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE,
321 APInt::getAllOnes(BitWidth))
322 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
353 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT,
354 APInt::getAllOnes(BitWidth))
355 .match(Zero));
356 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT,
357 APInt::getAllOnes(BitWidth))
358 .match(One));
359 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT,
360 APInt::getAllOnes(BitWidth))
361 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
392 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE,
393 APInt::getAllOnes(BitWidth))
394 .match(Zero));
395 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE,
396 APInt::getAllOnes(BitWidth))
397 .match(One));
398 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE,
399 APInt::getAllOnes(BitWidth))
400 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
431 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT,
432 APInt::getAllOnes(BitWidth))
433 .match(Zero));
434 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT,
435 APInt::getAllOnes(BitWidth))
436 .match(One));
437 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT,
438 APInt::getAllOnes(BitWidth))
439 .match(NegOne));
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 = Constant::getAllOnesValue(IntTy);
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));
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));
470 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE,
471 APInt::getAllOnes(BitWidth))
472 .match(Zero));
473 EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE,
474 APInt::getAllOnes(BitWidth))
475 .match(One));
476 EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE,
477 APInt::getAllOnes(BitWidth))
478 .match(NegOne));
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, BitWise) {
498 Value *Or = IRB.CreateOr(IRB.getInt32(1), IRB.getInt32(0));
499 Value *Xor = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
500 Value *And = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
501 Constant *T = IRB.getInt1(true);
502 Constant *F = IRB.getInt1(false);
503 Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
504 Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
505 Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
506 Value *LAnd = IRB.CreateSelect(X, Y, F);
507 Value *LOr = IRB.CreateSelect(X, T, Y);
508 Value *Add = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
510 EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Or));
511 EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Xor));
512 EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(And));
513 EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
514 EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LOr));
515 EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(Add));
517 EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Or));
518 EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Xor));
519 EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(And));
521 EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Or));
522 EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Xor));
523 EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(And));
524 EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
525 EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LOr));
526 EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(Add));
528 EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Or));
529 EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Xor));
530 EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(And));
532 EXPECT_FALSE(m_c_BitwiseLogic(m_One(), m_One()).match(Or));
533 EXPECT_FALSE(m_c_BitwiseLogic(m_Zero(), m_Zero()).match(Xor));
536 TEST_F(PatternMatchTest, ZExtSExtSelf) {
537 LLVMContext &Ctx = IRB.getContext();
539 Value *One32 = IRB.getInt32(1);
540 Value *One64Z = IRB.CreateZExt(One32, IntegerType::getInt64Ty(Ctx));
541 Value *One64S = IRB.CreateSExt(One32, IntegerType::getInt64Ty(Ctx));
543 EXPECT_TRUE(m_One().match(One32));
544 EXPECT_FALSE(m_One().match(One64Z));
545 EXPECT_FALSE(m_One().match(One64S));
547 EXPECT_FALSE(m_ZExt(m_One()).match(One32));
548 EXPECT_TRUE(m_ZExt(m_One()).match(One64Z));
549 EXPECT_FALSE(m_ZExt(m_One()).match(One64S));
551 EXPECT_FALSE(m_SExt(m_One()).match(One32));
552 EXPECT_FALSE(m_SExt(m_One()).match(One64Z));
553 EXPECT_TRUE(m_SExt(m_One()).match(One64S));
555 EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One32));
556 EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One64Z));
557 EXPECT_FALSE(m_ZExtOrSelf(m_One()).match(One64S));
559 EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One32));
560 EXPECT_FALSE(m_SExtOrSelf(m_One()).match(One64Z));
561 EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One64S));
563 EXPECT_FALSE(m_ZExtOrSExt(m_One()).match(One32));
564 EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64Z));
565 EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64S));
567 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One32));
568 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64Z));
569 EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64S));
572 TEST_F(PatternMatchTest, BitCast) {
573 Value *OneDouble = ConstantFP::get(IRB.getDoubleTy(), APFloat(1.0));
574 Value *ScalableDouble = ConstantFP::get(
575 VectorType::get(IRB.getDoubleTy(), 2, /*Scalable=*/true), APFloat(1.0));
576 // scalar -> scalar
577 Value *DoubleToI64 = IRB.CreateBitCast(OneDouble, IRB.getInt64Ty());
578 // scalar -> vector
579 Value *DoubleToV2I32 = IRB.CreateBitCast(
580 OneDouble, VectorType::get(IRB.getInt32Ty(), 2, /*Scalable=*/false));
581 // vector -> scalar
582 Value *V2I32ToDouble = IRB.CreateBitCast(DoubleToV2I32, IRB.getDoubleTy());
583 // vector -> vector (same count)
584 Value *V2I32ToV2Float = IRB.CreateBitCast(
585 DoubleToV2I32, VectorType::get(IRB.getFloatTy(), 2, /*Scalable=*/false));
586 // vector -> vector (different count)
587 Value *V2I32TOV4I16 = IRB.CreateBitCast(
588 DoubleToV2I32, VectorType::get(IRB.getInt16Ty(), 4, /*Scalable=*/false));
589 // scalable vector -> scalable vector (same count)
590 Value *NXV2DoubleToNXV2I64 = IRB.CreateBitCast(
591 ScalableDouble, VectorType::get(IRB.getInt64Ty(), 2, /*Scalable=*/true));
592 // scalable vector -> scalable vector (different count)
593 Value *NXV2I64ToNXV4I32 = IRB.CreateBitCast(
594 NXV2DoubleToNXV2I64,
595 VectorType::get(IRB.getInt32Ty(), 4, /*Scalable=*/true));
597 EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToI64));
598 EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToV2I32));
599 EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToDouble));
600 EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToV2Float));
601 EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32TOV4I16));
602 EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2DoubleToNXV2I64));
603 EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2I64ToNXV4I32));
605 EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(DoubleToI64));
606 EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(DoubleToV2I32));
607 EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32ToDouble));
608 EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(V2I32ToV2Float));
609 EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32TOV4I16));
610 EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(NXV2DoubleToNXV2I64));
611 EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(NXV2I64ToNXV4I32));
614 TEST_F(PatternMatchTest, CheckedInt) {
615 Type *I8Ty = IRB.getInt8Ty();
616 const Constant * CRes = nullptr;
617 auto CheckUgt1 = [](const APInt &C) { return C.ugt(1); };
618 auto CheckTrue = [](const APInt &) { return true; };
619 auto CheckFalse = [](const APInt &) { return false; };
620 auto CheckNonZero = [](const APInt &C) { return !C.isZero(); };
621 auto CheckPow2 = [](const APInt &C) { return C.isPowerOf2(); };
623 auto DoScalarCheck = [&](int8_t Val) {
624 APInt APVal(8, Val);
625 Constant *C = ConstantInt::get(I8Ty, Val);
627 CRes = nullptr;
628 EXPECT_TRUE(m_CheckedInt(CheckTrue).match(C));
629 EXPECT_TRUE(m_CheckedInt(CRes, CheckTrue).match(C));
630 EXPECT_EQ(CRes, C);
632 CRes = nullptr;
633 EXPECT_FALSE(m_CheckedInt(CheckFalse).match(C));
634 EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(C));
635 EXPECT_EQ(CRes, nullptr);
637 CRes = nullptr;
638 EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CheckUgt1).match(C));
639 EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CRes, CheckUgt1).match(C));
640 if (CheckUgt1(APVal))
641 EXPECT_EQ(CRes, C);
643 CRes = nullptr;
644 EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CheckNonZero).match(C));
645 EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CRes, CheckNonZero).match(C));
646 if (CheckNonZero(APVal))
647 EXPECT_EQ(CRes, C);
649 CRes = nullptr;
650 EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CheckPow2).match(C));
651 EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CRes, CheckPow2).match(C));
652 if (CheckPow2(APVal))
653 EXPECT_EQ(CRes, C);
657 DoScalarCheck(0);
658 DoScalarCheck(1);
659 DoScalarCheck(2);
660 DoScalarCheck(3);
662 EXPECT_FALSE(m_CheckedInt(CheckTrue).match(UndefValue::get(I8Ty)));
663 EXPECT_FALSE(m_CheckedInt(CRes, CheckTrue).match(UndefValue::get(I8Ty)));
664 EXPECT_EQ(CRes, nullptr);
666 EXPECT_FALSE(m_CheckedInt(CheckFalse).match(UndefValue::get(I8Ty)));
667 EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(UndefValue::get(I8Ty)));
668 EXPECT_EQ(CRes, nullptr);
670 EXPECT_FALSE(m_CheckedInt(CheckTrue).match(PoisonValue::get(I8Ty)));
671 EXPECT_FALSE(m_CheckedInt(CRes, CheckTrue).match(PoisonValue::get(I8Ty)));
672 EXPECT_EQ(CRes, nullptr);
674 EXPECT_FALSE(m_CheckedInt(CheckFalse).match(PoisonValue::get(I8Ty)));
675 EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(PoisonValue::get(I8Ty)));
676 EXPECT_EQ(CRes, nullptr);
678 auto DoVecCheckImpl = [&](ArrayRef<std::optional<int8_t>> Vals,
679 function_ref<bool(const APInt &)> CheckFn,
680 bool UndefAsPoison) {
681 SmallVector<Constant *> VecElems;
682 std::optional<bool> Okay;
683 bool AllSame = true;
684 bool HasUndef = false;
685 std::optional<APInt> First;
686 for (const std::optional<int8_t> &Val : Vals) {
687 if (!Val.has_value()) {
688 VecElems.push_back(UndefAsPoison ? PoisonValue::get(I8Ty)
689 : UndefValue::get(I8Ty));
690 HasUndef = true;
691 } else {
692 if (!Okay.has_value())
693 Okay = true;
694 APInt APVal(8, *Val);
695 if (!First.has_value())
696 First = APVal;
697 else
698 AllSame &= First->eq(APVal);
699 Okay = *Okay && CheckFn(APVal);
700 VecElems.push_back(ConstantInt::get(I8Ty, *Val));
704 Constant *C = ConstantVector::get(VecElems);
705 EXPECT_EQ(!(HasUndef && !UndefAsPoison) && Okay.value_or(false),
706 m_CheckedInt(CheckFn).match(C));
708 CRes = nullptr;
709 bool Expec = !(HasUndef && !UndefAsPoison) && Okay.value_or(false);
710 EXPECT_EQ(Expec, m_CheckedInt(CRes, CheckFn).match(C));
711 if (Expec) {
712 EXPECT_NE(CRes, nullptr);
713 if (AllSame)
714 EXPECT_EQ(CRes, C);
717 auto DoVecCheck = [&](ArrayRef<std::optional<int8_t>> Vals) {
718 DoVecCheckImpl(Vals, CheckTrue, /*UndefAsPoison=*/false);
719 DoVecCheckImpl(Vals, CheckFalse, /*UndefAsPoison=*/false);
720 DoVecCheckImpl(Vals, CheckTrue, /*UndefAsPoison=*/true);
721 DoVecCheckImpl(Vals, CheckFalse, /*UndefAsPoison=*/true);
722 DoVecCheckImpl(Vals, CheckUgt1, /*UndefAsPoison=*/false);
723 DoVecCheckImpl(Vals, CheckNonZero, /*UndefAsPoison=*/false);
724 DoVecCheckImpl(Vals, CheckPow2, /*UndefAsPoison=*/false);
727 DoVecCheck({0, 1});
728 DoVecCheck({1, 1});
729 DoVecCheck({1, 2});
730 DoVecCheck({1, std::nullopt});
731 DoVecCheck({1, std::nullopt, 1});
732 DoVecCheck({1, std::nullopt, 2});
733 DoVecCheck({std::nullopt, std::nullopt, std::nullopt});
736 TEST_F(PatternMatchTest, Power2) {
737 Value *C128 = IRB.getInt32(128);
738 Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
740 EXPECT_TRUE(m_Power2().match(C128));
741 EXPECT_FALSE(m_Power2().match(CNeg128));
743 EXPECT_TRUE(m_Power2OrZero().match(C128));
744 EXPECT_FALSE(m_Power2OrZero().match(CNeg128));
746 EXPECT_FALSE(m_NegatedPower2().match(C128));
747 EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
749 EXPECT_FALSE(m_NegatedPower2OrZero().match(C128));
750 EXPECT_TRUE(m_NegatedPower2OrZero().match(CNeg128));
752 Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
753 Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
755 EXPECT_TRUE(m_Power2().match(CIntMin));
756 EXPECT_TRUE(m_Power2().match(CNegIntMin));
758 EXPECT_TRUE(m_Power2OrZero().match(CIntMin));
759 EXPECT_TRUE(m_Power2OrZero().match(CNegIntMin));
761 EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
762 EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
764 EXPECT_TRUE(m_NegatedPower2OrZero().match(CIntMin));
765 EXPECT_TRUE(m_NegatedPower2OrZero().match(CNegIntMin));
767 Value *CZero = IRB.getInt64(0);
769 EXPECT_FALSE(m_Power2().match(CZero));
771 EXPECT_TRUE(m_Power2OrZero().match(CZero));
773 EXPECT_FALSE(m_NegatedPower2().match(CZero));
775 EXPECT_TRUE(m_NegatedPower2OrZero().match(CZero));
778 TEST_F(PatternMatchTest, Not) {
779 Value *C1 = IRB.getInt32(1);
780 Value *C2 = IRB.getInt32(2);
781 Value *C3 = IRB.getInt32(3);
782 Instruction *Not = BinaryOperator::CreateXor(C1, C2);
784 // When `m_Not` does not match the `not` itself,
785 // it should not try to apply the inner matcher.
786 Value *Val = C3;
787 EXPECT_FALSE(m_Not(m_Value(Val)).match(Not));
788 EXPECT_EQ(Val, C3);
789 Not->deleteValue();
792 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
793 Value *X = IRB.getInt32(1);
794 Value *Y = IRB.getInt32(2);
797 Value *tX = X;
798 EXPECT_TRUE(match(X, m_Deferred(tX)));
799 EXPECT_FALSE(match(Y, m_Deferred(tX)));
802 const Value *tX = X;
803 EXPECT_TRUE(match(X, m_Deferred(tX)));
804 EXPECT_FALSE(match(Y, m_Deferred(tX)));
807 Value *const tX = X;
808 EXPECT_TRUE(match(X, m_Deferred(tX)));
809 EXPECT_FALSE(match(Y, m_Deferred(tX)));
812 const Value *const tX = X;
813 EXPECT_TRUE(match(X, m_Deferred(tX)));
814 EXPECT_FALSE(match(Y, m_Deferred(tX)));
818 Value *tX = nullptr;
819 EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
820 EXPECT_EQ(tX, X);
823 Value *tX = nullptr;
824 EXPECT_FALSE(
825 match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
828 auto checkMatch = [X, Y](Value *Pattern) {
829 Value *tX = nullptr, *tY = nullptr;
830 EXPECT_TRUE(match(
831 Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
832 EXPECT_EQ(tX, X);
833 EXPECT_EQ(tY, Y);
836 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
837 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
838 checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
839 checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
842 TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
843 Type *FltTy = IRB.getFloatTy();
844 Value *L = ConstantFP::get(FltTy, 1.0);
845 Value *R = ConstantFP::get(FltTy, 2.0);
846 Value *MatchL, *MatchR;
848 // Test OLT.
849 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
850 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
851 EXPECT_EQ(L, MatchL);
852 EXPECT_EQ(R, MatchR);
854 // Test OLE.
855 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
856 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
857 EXPECT_EQ(L, MatchL);
858 EXPECT_EQ(R, MatchR);
860 // Test no match on OGE.
861 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
862 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
864 // Test no match on OGT.
865 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
866 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
868 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
869 // %cmp = fcmp oge L, R
870 // %min = select %cmp R, L
871 // Given L == NaN
872 // the above is expanded to %cmp == false ==> %min = L
873 // which is true for UnordFMin, not OrdFMin, so test that:
875 // [OU]GE with inverted select.
876 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
877 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
878 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
879 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
880 EXPECT_EQ(L, MatchL);
881 EXPECT_EQ(R, MatchR);
883 // [OU]GT with inverted select.
884 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
885 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
886 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
887 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
888 EXPECT_EQ(L, MatchL);
889 EXPECT_EQ(R, MatchR);
892 TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
893 Type *FltTy = IRB.getFloatTy();
894 Value *L = ConstantFP::get(FltTy, 1.0);
895 Value *R = ConstantFP::get(FltTy, 2.0);
896 Value *MatchL, *MatchR;
898 // Test OGT.
899 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
900 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
901 EXPECT_EQ(L, MatchL);
902 EXPECT_EQ(R, MatchR);
904 // Test OGE.
905 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
906 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
907 EXPECT_EQ(L, MatchL);
908 EXPECT_EQ(R, MatchR);
910 // Test no match on OLE.
911 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
912 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
914 // Test no match on OLT.
915 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
916 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
919 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
920 // %cmp = fcmp ole L, R
921 // %max = select %cmp, R, L
922 // Given L == NaN,
923 // the above is expanded to %cmp == false ==> %max == L
924 // which is true for UnordFMax, not OrdFMax, so test that:
926 // [OU]LE with inverted select.
927 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
928 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
929 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
930 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
931 EXPECT_EQ(L, MatchL);
932 EXPECT_EQ(R, MatchR);
934 // [OUT]LT with inverted select.
935 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
936 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
937 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
938 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
939 EXPECT_EQ(L, MatchL);
940 EXPECT_EQ(R, MatchR);
943 TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
944 Type *FltTy = IRB.getFloatTy();
945 Value *L = ConstantFP::get(FltTy, 1.0);
946 Value *R = ConstantFP::get(FltTy, 2.0);
947 Value *MatchL, *MatchR;
949 // Test ULT.
950 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
951 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
952 EXPECT_EQ(L, MatchL);
953 EXPECT_EQ(R, MatchR);
955 // Test ULE.
956 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
957 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
958 EXPECT_EQ(L, MatchL);
959 EXPECT_EQ(R, MatchR);
961 // Test no match on UGE.
962 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
963 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
965 // Test no match on UGT.
966 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
967 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
969 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
970 // %cmp = fcmp uge L, R
971 // %min = select %cmp R, L
972 // Given L == NaN
973 // the above is expanded to %cmp == true ==> %min = R
974 // which is true for OrdFMin, not UnordFMin, so test that:
976 // [UO]GE with inverted select.
977 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
978 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
979 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
980 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
981 EXPECT_EQ(L, MatchL);
982 EXPECT_EQ(R, MatchR);
984 // [UO]GT with inverted select.
985 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
986 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
987 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
988 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
989 EXPECT_EQ(L, MatchL);
990 EXPECT_EQ(R, MatchR);
993 TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
994 Type *FltTy = IRB.getFloatTy();
995 Value *L = ConstantFP::get(FltTy, 1.0);
996 Value *R = ConstantFP::get(FltTy, 2.0);
997 Value *MatchL, *MatchR;
999 // Test UGT.
1000 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1001 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
1002 EXPECT_EQ(L, MatchL);
1003 EXPECT_EQ(R, MatchR);
1005 // Test UGE.
1006 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1007 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
1008 EXPECT_EQ(L, MatchL);
1009 EXPECT_EQ(R, MatchR);
1011 // Test no match on ULE.
1012 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1013 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
1015 // Test no match on ULT.
1016 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1017 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
1019 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
1020 // %cmp = fcmp ule L, R
1021 // %max = select %cmp R, L
1022 // Given L == NaN
1023 // the above is expanded to %cmp == true ==> %max = R
1024 // which is true for OrdFMax, not UnordFMax, so test that:
1026 // [UO]LE with inverted select.
1027 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1028 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
1029 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1030 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
1031 EXPECT_EQ(L, MatchL);
1032 EXPECT_EQ(R, MatchR);
1034 // [UO]LT with inverted select.
1035 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1036 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
1037 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1038 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
1039 EXPECT_EQ(L, MatchL);
1040 EXPECT_EQ(R, MatchR);
1043 TEST_F(PatternMatchTest, FloatingPointMin) {
1044 Type *FltTy = IRB.getFloatTy();
1045 Value *L = ConstantFP::get(FltTy, 1.0);
1046 Value *R = ConstantFP::get(FltTy, 2.0);
1047 Value *MatchL, *MatchR;
1049 // Test OLT.
1050 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1051 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
1052 EXPECT_EQ(L, MatchL);
1053 EXPECT_EQ(R, MatchR);
1055 // Test OLE.
1056 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1057 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
1058 EXPECT_EQ(L, MatchL);
1059 EXPECT_EQ(R, MatchR);
1061 // Test ULT.
1062 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1063 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
1064 EXPECT_EQ(L, MatchL);
1065 EXPECT_EQ(R, MatchR);
1067 // Test ULE.
1068 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1069 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
1070 EXPECT_EQ(L, MatchL);
1071 EXPECT_EQ(R, MatchR);
1073 // Test no match on OGE.
1074 EXPECT_FALSE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1075 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
1077 // Test no match on OGT.
1078 EXPECT_FALSE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1079 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
1081 // Test no match on UGE.
1082 EXPECT_FALSE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1083 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
1085 // Test no match on UGT.
1086 EXPECT_FALSE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1087 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
1089 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
1090 // %cmp = fcmp oge L, R
1091 // %min = select %cmp R, L
1093 // [OU]GE with inverted select.
1094 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1095 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
1096 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1097 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
1098 EXPECT_EQ(L, MatchL);
1099 EXPECT_EQ(R, MatchR);
1101 // [OU]GT with inverted select.
1102 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1103 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
1104 EXPECT_TRUE(m_OrdOrUnordFMin(m_Value(MatchL), m_Value(MatchR))
1105 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
1106 EXPECT_EQ(L, MatchL);
1107 EXPECT_EQ(R, MatchR);
1110 TEST_F(PatternMatchTest, FloatingPointMax) {
1111 Type *FltTy = IRB.getFloatTy();
1112 Value *L = ConstantFP::get(FltTy, 1.0);
1113 Value *R = ConstantFP::get(FltTy, 2.0);
1114 Value *MatchL, *MatchR;
1116 // Test OGT.
1117 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1118 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
1119 EXPECT_EQ(L, MatchL);
1120 EXPECT_EQ(R, MatchR);
1122 // Test OGE.
1123 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1124 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
1125 EXPECT_EQ(L, MatchL);
1126 EXPECT_EQ(R, MatchR);
1128 // Test UGT.
1129 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1130 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
1131 EXPECT_EQ(L, MatchL);
1132 EXPECT_EQ(R, MatchR);
1134 // Test UGE.
1135 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1136 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
1137 EXPECT_EQ(L, MatchL);
1138 EXPECT_EQ(R, MatchR);
1140 // Test no match on OLE.
1141 EXPECT_FALSE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1142 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
1144 // Test no match on OLT.
1145 EXPECT_FALSE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1146 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
1148 // Test no match on ULE.
1149 EXPECT_FALSE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1150 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
1152 // Test no match on ULT.
1153 EXPECT_FALSE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1154 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
1156 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
1157 // %cmp = fcmp ole L, R
1158 // %max = select %cmp, R, L
1160 // [OU]LE with inverted select.
1161 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1162 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
1163 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1164 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
1165 EXPECT_EQ(L, MatchL);
1166 EXPECT_EQ(R, MatchR);
1168 // [OUT]LT with inverted select.
1169 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1170 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
1171 EXPECT_TRUE(m_OrdOrUnordFMax(m_Value(MatchL), m_Value(MatchR))
1172 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
1173 EXPECT_EQ(L, MatchL);
1174 EXPECT_EQ(R, MatchR);
1177 TEST_F(PatternMatchTest, OverflowingBinOps) {
1178 Value *L = IRB.getInt32(1);
1179 Value *R = IRB.getInt32(2);
1180 Value *MatchL, *MatchR;
1182 EXPECT_TRUE(
1183 m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
1184 EXPECT_EQ(L, MatchL);
1185 EXPECT_EQ(R, MatchR);
1186 MatchL = MatchR = nullptr;
1187 EXPECT_TRUE(
1188 m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
1189 EXPECT_EQ(L, MatchL);
1190 EXPECT_EQ(R, MatchR);
1191 MatchL = MatchR = nullptr;
1192 EXPECT_TRUE(
1193 m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
1194 EXPECT_EQ(L, MatchL);
1195 EXPECT_EQ(R, MatchR);
1196 MatchL = MatchR = nullptr;
1197 EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
1198 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
1199 EXPECT_EQ(L, MatchL);
1200 EXPECT_EQ(R, MatchR);
1202 EXPECT_TRUE(
1203 m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
1204 EXPECT_EQ(L, MatchL);
1205 EXPECT_EQ(R, MatchR);
1206 MatchL = MatchR = nullptr;
1208 EXPECT_TRUE(
1209 m_c_NUWAdd(m_Specific(L), m_Specific(R)).match(IRB.CreateNUWAdd(L, R)));
1210 EXPECT_TRUE(
1211 m_c_NUWAdd(m_Specific(R), m_Specific(L)).match(IRB.CreateNUWAdd(L, R)));
1212 EXPECT_FALSE(
1213 m_c_NUWAdd(m_Specific(R), m_ZeroInt()).match(IRB.CreateNUWAdd(L, R)));
1214 EXPECT_FALSE(
1215 m_NUWAdd(m_Specific(R), m_Specific(L)).match(IRB.CreateNUWAdd(L, R)));
1217 EXPECT_TRUE(
1218 m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
1219 EXPECT_EQ(L, MatchL);
1220 EXPECT_EQ(R, MatchR);
1221 MatchL = MatchR = nullptr;
1222 EXPECT_TRUE(
1223 m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
1224 EXPECT_EQ(L, MatchL);
1225 EXPECT_EQ(R, MatchR);
1226 MatchL = MatchR = nullptr;
1227 EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
1228 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
1229 EXPECT_EQ(L, MatchL);
1230 EXPECT_EQ(R, MatchR);
1232 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
1233 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1234 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
1235 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
1236 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
1237 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1238 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
1239 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
1240 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1241 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
1242 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
1243 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
1244 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1246 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
1247 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1248 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
1249 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
1250 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
1251 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1252 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
1253 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
1254 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1255 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
1256 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
1257 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
1258 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1261 TEST_F(PatternMatchTest, LoadStoreOps) {
1262 // Create this load/store sequence:
1264 // %p = alloca i32*
1265 // %0 = load i32*, i32** %p
1266 // store i32 42, i32* %0
1268 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
1269 Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
1270 Value *FourtyTwo = IRB.getInt32(42);
1271 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
1272 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
1274 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
1275 EXPECT_EQ(Alloca, MatchLoad);
1277 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
1279 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
1281 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
1282 .match(StoreInst));
1283 EXPECT_EQ(FourtyTwo, MatchStoreVal);
1284 EXPECT_EQ(Alloca, MatchStorePointer);
1286 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
1287 .match(Alloca));
1289 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
1290 .match(StoreInst));
1291 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
1292 .match(StoreInst));
1293 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
1294 .match(StoreInst));
1297 TEST_F(PatternMatchTest, VectorOps) {
1298 // Build up small tree of vector operations
1300 // Val = 0 + 1
1301 // Val2 = Val + 3
1302 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
1303 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
1304 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
1305 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
1307 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
1308 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
1309 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
1310 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
1312 // SP1 = VectorSplat(2, i8 2)
1313 // SP2 = VectorSplat(2, i8 %Val)
1314 Type *VecTy = FixedVectorType::get(IRB.getInt8Ty(), 2);
1315 Type *i32 = IRB.getInt32Ty();
1316 Type *i32VecTy = FixedVectorType::get(i32, 2);
1318 Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
1319 Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
1321 SmallVector<Constant *, 2> VecElemIdxs;
1322 VecElemIdxs.push_back(ConstantInt::get(i32, 0));
1323 VecElemIdxs.push_back(ConstantInt::get(i32, 2));
1324 auto *IdxVec = ConstantVector::get(VecElemIdxs);
1326 Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0);
1327 Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
1328 Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
1329 Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
1331 Value *EX1 = IRB.CreateExtractElement(VI4, Val);
1332 Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
1333 Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
1335 Constant *Zero = ConstantAggregateZero::get(i32VecTy);
1336 SmallVector<int, 16> ZeroMask;
1337 ShuffleVectorInst::getShuffleMask(Zero, ZeroMask);
1339 Value *SI1 = IRB.CreateShuffleVector(VI1, ZeroMask);
1340 Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
1341 Value *SI3 = IRB.CreateShuffleVector(VI3, ZeroMask);
1342 Value *SI4 = IRB.CreateShuffleVector(VI4, ZeroMask);
1344 Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
1345 Value *SP2 = IRB.CreateVectorSplat(2, Val);
1347 Value *A = nullptr, *B = nullptr, *C = nullptr;
1349 // Test matching insertelement
1350 EXPECT_TRUE(match(VI1, m_InsertElt(m_Value(), m_Value(), m_Value())));
1351 EXPECT_TRUE(
1352 match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt())));
1353 EXPECT_TRUE(
1354 match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero())));
1355 EXPECT_TRUE(
1356 match(VI1, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero())));
1357 EXPECT_TRUE(match(VI2, m_InsertElt(m_Value(), m_Value(), m_Value())));
1358 EXPECT_FALSE(
1359 match(VI2, m_InsertElt(m_Value(), m_Value(), m_ConstantInt())));
1360 EXPECT_FALSE(
1361 match(VI2, m_InsertElt(m_Value(), m_ConstantInt(), m_Value())));
1362 EXPECT_FALSE(match(VI2, m_InsertElt(m_Constant(), m_Value(), m_Value())));
1363 EXPECT_TRUE(match(VI3, m_InsertElt(m_Value(A), m_Value(B), m_Value(C))));
1364 EXPECT_TRUE(A == VI1);
1365 EXPECT_TRUE(B == Val2);
1366 EXPECT_TRUE(isa<ConstantInt>(C));
1367 A = B = C = nullptr; // reset
1369 // Test matching extractelement
1370 EXPECT_TRUE(match(EX1, m_ExtractElt(m_Value(A), m_Value(B))));
1371 EXPECT_TRUE(A == VI4);
1372 EXPECT_TRUE(B == Val);
1373 A = B = C = nullptr; // reset
1374 EXPECT_FALSE(match(EX1, m_ExtractElt(m_Value(), m_ConstantInt())));
1375 EXPECT_TRUE(match(EX2, m_ExtractElt(m_Value(), m_ConstantInt())));
1376 EXPECT_TRUE(match(EX3, m_ExtractElt(m_Constant(), m_ConstantInt())));
1378 // Test matching shufflevector
1379 ArrayRef<int> Mask;
1380 EXPECT_TRUE(match(SI1, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask())));
1381 EXPECT_TRUE(match(SI2, m_Shuffle(m_Value(A), m_Value(B), m_Mask(Mask))));
1382 EXPECT_TRUE(A == VI3);
1383 EXPECT_TRUE(B == VI4);
1384 A = B = C = nullptr; // reset
1386 // Test matching the vector splat pattern
1387 EXPECT_TRUE(match(
1388 SI1,
1389 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()),
1390 m_Undef(), m_ZeroMask())));
1391 EXPECT_FALSE(match(
1392 SI3, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1393 m_Undef(), m_ZeroMask())));
1394 EXPECT_FALSE(match(
1395 SI4, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1396 m_Undef(), m_ZeroMask())));
1397 EXPECT_TRUE(match(
1398 SP1,
1399 m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()),
1400 m_Undef(), m_ZeroMask())));
1401 EXPECT_TRUE(match(
1402 SP2, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A), m_Zero()),
1403 m_Undef(), m_ZeroMask())));
1404 EXPECT_TRUE(A == Val);
1407 TEST_F(PatternMatchTest, UndefPoisonMix) {
1408 Type *ScalarTy = IRB.getInt8Ty();
1409 ArrayType *ArrTy = ArrayType::get(ScalarTy, 2);
1410 StructType *StTy = StructType::get(ScalarTy, ScalarTy);
1411 StructType *StTy2 = StructType::get(ScalarTy, StTy);
1412 StructType *StTy3 = StructType::get(StTy, ScalarTy);
1413 Constant *Zero = ConstantInt::getNullValue(ScalarTy);
1414 UndefValue *U = UndefValue::get(ScalarTy);
1415 UndefValue *P = PoisonValue::get(ScalarTy);
1417 EXPECT_TRUE(match(ConstantVector::get({U, P}), m_Undef()));
1418 EXPECT_TRUE(match(ConstantVector::get({P, U}), m_Undef()));
1420 EXPECT_TRUE(match(ConstantArray::get(ArrTy, {U, P}), m_Undef()));
1421 EXPECT_TRUE(match(ConstantArray::get(ArrTy, {P, U}), m_Undef()));
1423 auto *UP = ConstantStruct::get(StTy, {U, P});
1424 EXPECT_TRUE(match(ConstantStruct::get(StTy2, {U, UP}), m_Undef()));
1425 EXPECT_TRUE(match(ConstantStruct::get(StTy2, {P, UP}), m_Undef()));
1426 EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, U}), m_Undef()));
1427 EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, P}), m_Undef()));
1429 EXPECT_FALSE(match(ConstantStruct::get(StTy, {U, Zero}), m_Undef()));
1430 EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, U}), m_Undef()));
1431 EXPECT_FALSE(match(ConstantStruct::get(StTy, {P, Zero}), m_Undef()));
1432 EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, P}), m_Undef()));
1434 EXPECT_FALSE(match(ConstantStruct::get(StTy2, {Zero, UP}), m_Undef()));
1435 EXPECT_FALSE(match(ConstantStruct::get(StTy3, {UP, Zero}), m_Undef()));
1438 TEST_F(PatternMatchTest, VectorUndefInt) {
1439 Type *ScalarTy = IRB.getInt8Ty();
1440 Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1441 Constant *ScalarUndef = UndefValue::get(ScalarTy);
1442 Constant *VectorUndef = UndefValue::get(VectorTy);
1443 Constant *ScalarPoison = PoisonValue::get(ScalarTy);
1444 Constant *VectorPoison = PoisonValue::get(VectorTy);
1445 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1446 Constant *VectorZero = Constant::getNullValue(VectorTy);
1448 SmallVector<Constant *, 4> Elems;
1449 Elems.push_back(ScalarUndef);
1450 Elems.push_back(ScalarZero);
1451 Elems.push_back(ScalarUndef);
1452 Elems.push_back(ScalarZero);
1453 Constant *VectorZeroUndef = ConstantVector::get(Elems);
1455 SmallVector<Constant *, 4> Elems2;
1456 Elems2.push_back(ScalarPoison);
1457 Elems2.push_back(ScalarZero);
1458 Elems2.push_back(ScalarPoison);
1459 Elems2.push_back(ScalarZero);
1460 Constant *VectorZeroPoison = ConstantVector::get(Elems2);
1462 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1463 EXPECT_TRUE(match(ScalarPoison, m_Undef()));
1464 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1465 EXPECT_TRUE(match(VectorPoison, m_Undef()));
1466 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1467 EXPECT_FALSE(match(VectorZero, m_Undef()));
1468 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1469 EXPECT_FALSE(match(VectorZeroPoison, m_Undef()));
1471 EXPECT_FALSE(match(ScalarUndef, m_Zero()));
1472 EXPECT_FALSE(match(ScalarPoison, m_Zero()));
1473 EXPECT_FALSE(match(VectorUndef, m_Zero()));
1474 EXPECT_FALSE(match(VectorPoison, m_Zero()));
1475 EXPECT_FALSE(match(VectorZeroUndef, m_Zero()));
1476 EXPECT_TRUE(match(ScalarZero, m_Zero()));
1477 EXPECT_TRUE(match(VectorZero, m_Zero()));
1478 EXPECT_TRUE(match(VectorZeroPoison, m_Zero()));
1480 const APInt *C;
1481 // Regardless of whether poison is allowed,
1482 // a fully undef/poison constant does not match.
1483 EXPECT_FALSE(match(ScalarUndef, m_APInt(C)));
1484 EXPECT_FALSE(match(ScalarUndef, m_APIntForbidPoison(C)));
1485 EXPECT_FALSE(match(ScalarUndef, m_APIntAllowPoison(C)));
1486 EXPECT_FALSE(match(VectorUndef, m_APInt(C)));
1487 EXPECT_FALSE(match(VectorUndef, m_APIntForbidPoison(C)));
1488 EXPECT_FALSE(match(VectorUndef, m_APIntAllowPoison(C)));
1489 EXPECT_FALSE(match(ScalarPoison, m_APInt(C)));
1490 EXPECT_FALSE(match(ScalarPoison, m_APIntForbidPoison(C)));
1491 EXPECT_FALSE(match(ScalarPoison, m_APIntAllowPoison(C)));
1492 EXPECT_FALSE(match(VectorPoison, m_APInt(C)));
1493 EXPECT_FALSE(match(VectorPoison, m_APIntForbidPoison(C)));
1494 EXPECT_FALSE(match(VectorPoison, m_APIntAllowPoison(C)));
1496 // We can always match simple constants and simple splats.
1497 C = nullptr;
1498 EXPECT_TRUE(match(ScalarZero, m_APInt(C)));
1499 EXPECT_TRUE(C->isZero());
1500 C = nullptr;
1501 EXPECT_TRUE(match(ScalarZero, m_APIntForbidPoison(C)));
1502 EXPECT_TRUE(C->isZero());
1503 C = nullptr;
1504 EXPECT_TRUE(match(ScalarZero, m_APIntAllowPoison(C)));
1505 EXPECT_TRUE(C->isZero());
1506 C = nullptr;
1507 EXPECT_TRUE(match(VectorZero, m_APInt(C)));
1508 EXPECT_TRUE(C->isZero());
1509 C = nullptr;
1510 EXPECT_TRUE(match(VectorZero, m_APIntForbidPoison(C)));
1511 EXPECT_TRUE(C->isZero());
1512 C = nullptr;
1513 EXPECT_TRUE(match(VectorZero, m_APIntAllowPoison(C)));
1514 EXPECT_TRUE(C->isZero());
1516 // Splats with undef are never allowed.
1517 // Whether splats with poison can be matched depends on the matcher.
1518 EXPECT_FALSE(match(VectorZeroUndef, m_APInt(C)));
1519 EXPECT_FALSE(match(VectorZeroUndef, m_APIntForbidPoison(C)));
1520 EXPECT_FALSE(match(VectorZeroUndef, m_APIntAllowPoison(C)));
1522 EXPECT_FALSE(match(VectorZeroPoison, m_APInt(C)));
1523 EXPECT_FALSE(match(VectorZeroPoison, m_APIntForbidPoison(C)));
1524 C = nullptr;
1525 EXPECT_TRUE(match(VectorZeroPoison, m_APIntAllowPoison(C)));
1526 EXPECT_TRUE(C->isZero());
1529 TEST_F(PatternMatchTest, VectorUndefFloat) {
1530 Type *ScalarTy = IRB.getFloatTy();
1531 Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1532 Constant *ScalarUndef = UndefValue::get(ScalarTy);
1533 Constant *VectorUndef = UndefValue::get(VectorTy);
1534 Constant *ScalarPoison = PoisonValue::get(ScalarTy);
1535 Constant *VectorPoison = PoisonValue::get(VectorTy);
1536 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1537 Constant *VectorZero = Constant::getNullValue(VectorTy);
1538 Constant *ScalarPosInf = ConstantFP::getInfinity(ScalarTy, false);
1539 Constant *ScalarNegInf = ConstantFP::getInfinity(ScalarTy, true);
1540 Constant *ScalarNaN = ConstantFP::getNaN(ScalarTy, true);
1542 Constant *VectorZeroUndef =
1543 ConstantVector::get({ScalarUndef, ScalarZero, ScalarUndef, ScalarZero});
1545 Constant *VectorZeroPoison =
1546 ConstantVector::get({ScalarPoison, ScalarZero, ScalarPoison, ScalarZero});
1548 Constant *VectorInfUndef = ConstantVector::get(
1549 {ScalarPosInf, ScalarNegInf, ScalarUndef, ScalarPosInf});
1551 Constant *VectorInfPoison = ConstantVector::get(
1552 {ScalarPosInf, ScalarNegInf, ScalarPoison, ScalarPosInf});
1554 Constant *VectorNaNUndef =
1555 ConstantVector::get({ScalarUndef, ScalarNaN, ScalarNaN, ScalarNaN});
1557 Constant *VectorNaNPoison =
1558 ConstantVector::get({ScalarPoison, ScalarNaN, ScalarNaN, ScalarNaN});
1560 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1561 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1562 EXPECT_TRUE(match(ScalarPoison, m_Undef()));
1563 EXPECT_TRUE(match(VectorPoison, m_Undef()));
1564 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1565 EXPECT_FALSE(match(VectorZero, m_Undef()));
1566 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1567 EXPECT_FALSE(match(VectorInfUndef, m_Undef()));
1568 EXPECT_FALSE(match(VectorNaNUndef, m_Undef()));
1569 EXPECT_FALSE(match(VectorZeroPoison, m_Undef()));
1570 EXPECT_FALSE(match(VectorInfPoison, m_Undef()));
1571 EXPECT_FALSE(match(VectorNaNPoison, m_Undef()));
1573 EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1574 EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1575 EXPECT_FALSE(match(ScalarPoison, m_AnyZeroFP()));
1576 EXPECT_FALSE(match(VectorPoison, m_AnyZeroFP()));
1577 EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1578 EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1579 EXPECT_FALSE(match(VectorZeroUndef, m_AnyZeroFP()));
1580 EXPECT_FALSE(match(VectorInfUndef, m_AnyZeroFP()));
1581 EXPECT_FALSE(match(VectorNaNUndef, m_AnyZeroFP()));
1582 EXPECT_TRUE(match(VectorZeroPoison, m_AnyZeroFP()));
1583 EXPECT_FALSE(match(VectorInfPoison, m_AnyZeroFP()));
1584 EXPECT_FALSE(match(VectorNaNPoison, m_AnyZeroFP()));
1586 EXPECT_FALSE(match(ScalarUndef, m_NaN()));
1587 EXPECT_FALSE(match(VectorUndef, m_NaN()));
1588 EXPECT_FALSE(match(VectorZeroUndef, m_NaN()));
1589 EXPECT_FALSE(match(ScalarPoison, m_NaN()));
1590 EXPECT_FALSE(match(VectorPoison, m_NaN()));
1591 EXPECT_FALSE(match(VectorZeroPoison, m_NaN()));
1592 EXPECT_FALSE(match(ScalarPosInf, m_NaN()));
1593 EXPECT_FALSE(match(ScalarNegInf, m_NaN()));
1594 EXPECT_TRUE(match(ScalarNaN, m_NaN()));
1595 EXPECT_FALSE(match(VectorInfUndef, m_NaN()));
1596 EXPECT_FALSE(match(VectorNaNUndef, m_NaN()));
1597 EXPECT_FALSE(match(VectorInfPoison, m_NaN()));
1598 EXPECT_TRUE(match(VectorNaNPoison, m_NaN()));
1600 EXPECT_FALSE(match(ScalarUndef, m_NonNaN()));
1601 EXPECT_FALSE(match(VectorUndef, m_NonNaN()));
1602 EXPECT_FALSE(match(VectorZeroUndef, m_NonNaN()));
1603 EXPECT_FALSE(match(ScalarPoison, m_NonNaN()));
1604 EXPECT_FALSE(match(VectorPoison, m_NonNaN()));
1605 EXPECT_TRUE(match(VectorZeroPoison, m_NonNaN()));
1606 EXPECT_TRUE(match(ScalarPosInf, m_NonNaN()));
1607 EXPECT_TRUE(match(ScalarNegInf, m_NonNaN()));
1608 EXPECT_FALSE(match(ScalarNaN, m_NonNaN()));
1609 EXPECT_FALSE(match(VectorInfUndef, m_NonNaN()));
1610 EXPECT_FALSE(match(VectorNaNUndef, m_NonNaN()));
1611 EXPECT_TRUE(match(VectorInfPoison, m_NonNaN()));
1612 EXPECT_FALSE(match(VectorNaNPoison, m_NonNaN()));
1614 EXPECT_FALSE(match(ScalarUndef, m_Inf()));
1615 EXPECT_FALSE(match(VectorUndef, m_Inf()));
1616 EXPECT_FALSE(match(VectorZeroUndef, m_Inf()));
1617 EXPECT_FALSE(match(ScalarPoison, m_Inf()));
1618 EXPECT_FALSE(match(VectorPoison, m_Inf()));
1619 EXPECT_FALSE(match(VectorZeroPoison, m_Inf()));
1620 EXPECT_TRUE(match(ScalarPosInf, m_Inf()));
1621 EXPECT_TRUE(match(ScalarNegInf, m_Inf()));
1622 EXPECT_FALSE(match(ScalarNaN, m_Inf()));
1623 EXPECT_FALSE(match(VectorInfUndef, m_Inf()));
1624 EXPECT_FALSE(match(VectorNaNUndef, m_Inf()));
1625 EXPECT_TRUE(match(VectorInfPoison, m_Inf()));
1626 EXPECT_FALSE(match(VectorNaNPoison, m_Inf()));
1628 EXPECT_FALSE(match(ScalarUndef, m_NonInf()));
1629 EXPECT_FALSE(match(VectorUndef, m_NonInf()));
1630 EXPECT_FALSE(match(VectorZeroUndef, m_NonInf()));
1631 EXPECT_FALSE(match(ScalarPoison, m_NonInf()));
1632 EXPECT_FALSE(match(VectorPoison, m_NonInf()));
1633 EXPECT_TRUE(match(VectorZeroPoison, m_NonInf()));
1634 EXPECT_FALSE(match(ScalarPosInf, m_NonInf()));
1635 EXPECT_FALSE(match(ScalarNegInf, m_NonInf()));
1636 EXPECT_TRUE(match(ScalarNaN, m_NonInf()));
1637 EXPECT_FALSE(match(VectorInfUndef, m_NonInf()));
1638 EXPECT_FALSE(match(VectorNaNUndef, m_NonInf()));
1639 EXPECT_FALSE(match(VectorInfPoison, m_NonInf()));
1640 EXPECT_TRUE(match(VectorNaNPoison, m_NonInf()));
1642 EXPECT_FALSE(match(ScalarUndef, m_Finite()));
1643 EXPECT_FALSE(match(VectorUndef, m_Finite()));
1644 EXPECT_FALSE(match(VectorZeroUndef, m_Finite()));
1645 EXPECT_FALSE(match(ScalarPoison, m_Finite()));
1646 EXPECT_FALSE(match(VectorPoison, m_Finite()));
1647 EXPECT_TRUE(match(VectorZeroPoison, m_Finite()));
1648 EXPECT_FALSE(match(ScalarPosInf, m_Finite()));
1649 EXPECT_FALSE(match(ScalarNegInf, m_Finite()));
1650 EXPECT_FALSE(match(ScalarNaN, m_Finite()));
1651 EXPECT_FALSE(match(VectorInfUndef, m_Finite()));
1652 EXPECT_FALSE(match(VectorNaNUndef, m_Finite()));
1653 EXPECT_FALSE(match(VectorInfPoison, m_Finite()));
1654 EXPECT_FALSE(match(VectorNaNPoison, m_Finite()));
1656 auto CheckTrue = [](const APFloat &) { return true; };
1657 EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckTrue)));
1658 EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CheckTrue)));
1659 EXPECT_TRUE(match(ScalarPosInf, m_CheckedFp(CheckTrue)));
1660 EXPECT_TRUE(match(ScalarNegInf, m_CheckedFp(CheckTrue)));
1661 EXPECT_TRUE(match(ScalarNaN, m_CheckedFp(CheckTrue)));
1662 EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckTrue)));
1663 EXPECT_TRUE(match(VectorInfPoison, m_CheckedFp(CheckTrue)));
1664 EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckTrue)));
1665 EXPECT_TRUE(match(VectorNaNPoison, m_CheckedFp(CheckTrue)));
1667 auto CheckFalse = [](const APFloat &) { return false; };
1668 EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckFalse)));
1669 EXPECT_FALSE(match(VectorZeroPoison, m_CheckedFp(CheckFalse)));
1670 EXPECT_FALSE(match(ScalarPosInf, m_CheckedFp(CheckFalse)));
1671 EXPECT_FALSE(match(ScalarNegInf, m_CheckedFp(CheckFalse)));
1672 EXPECT_FALSE(match(ScalarNaN, m_CheckedFp(CheckFalse)));
1673 EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckFalse)));
1674 EXPECT_FALSE(match(VectorInfPoison, m_CheckedFp(CheckFalse)));
1675 EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckFalse)));
1676 EXPECT_FALSE(match(VectorNaNPoison, m_CheckedFp(CheckFalse)));
1678 auto CheckNonNaN = [](const APFloat &C) { return !C.isNaN(); };
1679 EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckNonNaN)));
1680 EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CheckNonNaN)));
1681 EXPECT_TRUE(match(ScalarPosInf, m_CheckedFp(CheckNonNaN)));
1682 EXPECT_TRUE(match(ScalarNegInf, m_CheckedFp(CheckNonNaN)));
1683 EXPECT_FALSE(match(ScalarNaN, m_CheckedFp(CheckNonNaN)));
1684 EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckNonNaN)));
1685 EXPECT_TRUE(match(VectorInfPoison, m_CheckedFp(CheckNonNaN)));
1686 EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckNonNaN)));
1687 EXPECT_FALSE(match(VectorNaNPoison, m_CheckedFp(CheckNonNaN)));
1689 const APFloat *C;
1690 const Constant *CC;
1691 // Regardless of whether poison is allowed,
1692 // a fully undef/poison constant does not match.
1693 EXPECT_FALSE(match(ScalarUndef, m_APFloat(C)));
1694 EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidPoison(C)));
1695 EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowPoison(C)));
1696 EXPECT_FALSE(match(ScalarUndef, m_CheckedFp(CC, CheckTrue)));
1697 EXPECT_FALSE(match(VectorUndef, m_APFloat(C)));
1698 EXPECT_FALSE(match(VectorUndef, m_APFloatForbidPoison(C)));
1699 EXPECT_FALSE(match(VectorUndef, m_APFloatAllowPoison(C)));
1700 EXPECT_FALSE(match(VectorUndef, m_CheckedFp(CC, CheckTrue)));
1701 EXPECT_FALSE(match(ScalarPoison, m_APFloat(C)));
1702 EXPECT_FALSE(match(ScalarPoison, m_APFloatForbidPoison(C)));
1703 EXPECT_FALSE(match(ScalarPoison, m_APFloatAllowPoison(C)));
1704 EXPECT_FALSE(match(ScalarPoison, m_CheckedFp(CC, CheckTrue)));
1705 EXPECT_FALSE(match(VectorPoison, m_APFloat(C)));
1706 EXPECT_FALSE(match(VectorPoison, m_APFloatForbidPoison(C)));
1707 EXPECT_FALSE(match(VectorPoison, m_APFloatAllowPoison(C)));
1708 EXPECT_FALSE(match(VectorPoison, m_CheckedFp(CC, CheckTrue)));
1710 // We can always match simple constants and simple splats.
1711 C = nullptr;
1712 EXPECT_TRUE(match(ScalarZero, m_APFloat(C)));
1713 EXPECT_TRUE(C->isZero());
1714 C = nullptr;
1715 EXPECT_TRUE(match(ScalarZero, m_APFloatForbidPoison(C)));
1716 EXPECT_TRUE(C->isZero());
1717 C = nullptr;
1718 EXPECT_TRUE(match(ScalarZero, m_APFloatAllowPoison(C)));
1719 EXPECT_TRUE(C->isZero());
1720 C = nullptr;
1721 EXPECT_TRUE(match(VectorZero, m_APFloat(C)));
1722 EXPECT_TRUE(C->isZero());
1723 C = nullptr;
1724 EXPECT_TRUE(match(VectorZero, m_APFloatForbidPoison(C)));
1725 EXPECT_TRUE(C->isZero());
1726 C = nullptr;
1727 EXPECT_TRUE(match(VectorZero, m_APFloatAllowPoison(C)));
1728 EXPECT_TRUE(C->isZero());
1730 CC = nullptr;
1731 EXPECT_TRUE(match(VectorZero, m_CheckedFp(CC, CheckTrue)));
1732 EXPECT_TRUE(CC->isNullValue());
1733 CC = nullptr;
1734 EXPECT_TRUE(match(VectorZero, m_CheckedFp(CC, CheckNonNaN)));
1735 EXPECT_TRUE(CC->isNullValue());
1737 // Splats with undef are never allowed.
1738 // Whether splats with poison can be matched depends on the matcher.
1739 EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C)));
1740 EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidPoison(C)));
1741 EXPECT_FALSE(match(VectorZeroUndef, m_APFloatAllowPoison(C)));
1742 EXPECT_FALSE(match(VectorZeroUndef, m_Finite(C)));
1744 EXPECT_FALSE(match(VectorZeroPoison, m_APFloat(C)));
1745 EXPECT_FALSE(match(VectorZeroPoison, m_APFloatForbidPoison(C)));
1746 C = nullptr;
1747 EXPECT_TRUE(match(VectorZeroPoison, m_APFloatAllowPoison(C)));
1748 EXPECT_TRUE(C->isZero());
1749 C = nullptr;
1750 EXPECT_TRUE(match(VectorZeroPoison, m_Finite(C)));
1751 EXPECT_TRUE(C->isZero());
1752 EXPECT_FALSE(match(VectorZeroPoison, m_APFloat(C)));
1753 EXPECT_FALSE(match(VectorZeroPoison, m_APFloatForbidPoison(C)));
1754 C = nullptr;
1755 EXPECT_TRUE(match(VectorZeroPoison, m_APFloatAllowPoison(C)));
1756 EXPECT_TRUE(C->isZero());
1757 C = nullptr;
1758 EXPECT_TRUE(match(VectorZeroPoison, m_Finite(C)));
1759 EXPECT_TRUE(C->isZero());
1760 CC = nullptr;
1761 C = nullptr;
1762 EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CC, CheckTrue)));
1763 EXPECT_NE(CC, nullptr);
1764 EXPECT_TRUE(match(CC, m_APFloatAllowPoison(C)));
1765 EXPECT_TRUE(C->isZero());
1766 CC = nullptr;
1767 C = nullptr;
1768 EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CC, CheckNonNaN)));
1769 EXPECT_NE(CC, nullptr);
1770 EXPECT_TRUE(match(CC, m_APFloatAllowPoison(C)));
1771 EXPECT_TRUE(C->isZero());
1774 TEST_F(PatternMatchTest, FloatingPointFNeg) {
1775 Type *FltTy = IRB.getFloatTy();
1776 Value *One = ConstantFP::get(FltTy, 1.0);
1777 Value *Z = ConstantFP::get(FltTy, 0.0);
1778 Value *NZ = ConstantFP::get(FltTy, -0.0);
1779 Value *V = IRB.CreateFNeg(One);
1780 Value *V1 = IRB.CreateFSub(NZ, One);
1781 Value *V2 = IRB.CreateFSub(Z, One);
1782 Value *V3 = IRB.CreateFAdd(NZ, One);
1783 Value *Match;
1785 // Test FNeg(1.0)
1786 EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1787 EXPECT_EQ(One, Match);
1789 // Test FSub(-0.0, 1.0)
1790 EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1791 EXPECT_EQ(One, Match);
1793 // Test FSub(0.0, 1.0)
1794 EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1795 cast<Instruction>(V2)->setHasNoSignedZeros(true);
1796 EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1797 EXPECT_EQ(One, Match);
1799 // Test FAdd(-0.0, 1.0)
1800 EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1803 TEST_F(PatternMatchTest, CondBranchTest) {
1804 BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1805 BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1806 Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1808 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1810 BasicBlock *A, *B;
1811 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1812 EXPECT_EQ(TrueBB, A);
1813 EXPECT_EQ(FalseBB, B);
1815 EXPECT_FALSE(
1816 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1817 EXPECT_FALSE(
1818 match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1819 EXPECT_FALSE(
1820 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1821 EXPECT_TRUE(
1822 match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1824 // Check we can use m_Deferred with branches.
1825 EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1826 Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1827 A = nullptr;
1828 EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1831 TEST_F(PatternMatchTest, WithOverflowInst) {
1832 Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1833 IRB.getInt32(0), IRB.getInt32(0));
1834 Value *Add0 = IRB.CreateExtractValue(Add, 0);
1835 Value *Add1 = IRB.CreateExtractValue(Add, 1);
1837 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1838 EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1839 EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1840 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1841 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1842 EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1844 WithOverflowInst *WOI;
1845 EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1846 EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1847 EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1849 EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1850 EXPECT_EQ(Add, WOI);
1851 EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1852 EXPECT_EQ(Add, WOI);
1855 TEST_F(PatternMatchTest, MinMaxIntrinsics) {
1856 Type *Ty = IRB.getInt32Ty();
1857 Value *L = ConstantInt::get(Ty, 1);
1858 Value *R = ConstantInt::get(Ty, 2);
1859 Value *MatchL, *MatchR;
1861 // Check for intrinsic ID match and capture of operands.
1862 EXPECT_TRUE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1863 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1864 EXPECT_EQ(L, MatchL);
1865 EXPECT_EQ(R, MatchR);
1867 EXPECT_TRUE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1868 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1869 EXPECT_EQ(L, MatchL);
1870 EXPECT_EQ(R, MatchR);
1872 EXPECT_TRUE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1873 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1874 EXPECT_EQ(L, MatchL);
1875 EXPECT_EQ(R, MatchR);
1877 EXPECT_TRUE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1878 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1879 EXPECT_EQ(L, MatchL);
1880 EXPECT_EQ(R, MatchR);
1882 // Check for intrinsic ID mismatch.
1883 EXPECT_FALSE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1884 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1885 EXPECT_FALSE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1886 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1887 EXPECT_FALSE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1888 .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1889 EXPECT_FALSE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1890 .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1893 TEST_F(PatternMatchTest, IntrinsicMatcher) {
1894 Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1895 Value *Hash = IRB.getInt64(0);
1896 Value *Num = IRB.getInt32(1);
1897 Value *Index = IRB.getInt32(2);
1898 Value *Step = IRB.getInt64(3);
1900 Value *Ops[] = {Name, Hash, Num, Index, Step};
1901 Module *M = BB->getParent()->getParent();
1902 Function *TheFn =
1903 Intrinsic::getOrInsertDeclaration(M, Intrinsic::instrprof_increment_step);
1905 Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1907 // Match without capturing.
1908 EXPECT_TRUE(match(
1909 Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1910 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1911 EXPECT_FALSE(match(
1912 Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1913 m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1915 // Match with capturing.
1916 Value *Arg1 = nullptr;
1917 Value *Arg2 = nullptr;
1918 Value *Arg3 = nullptr;
1919 Value *Arg4 = nullptr;
1920 Value *Arg5 = nullptr;
1921 EXPECT_TRUE(
1922 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1923 m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1924 m_Value(Arg4), m_Value(Arg5))));
1925 EXPECT_EQ(Arg1, Name);
1926 EXPECT_EQ(Arg2, Hash);
1927 EXPECT_EQ(Arg3, Num);
1928 EXPECT_EQ(Arg4, Index);
1929 EXPECT_EQ(Arg5, Step);
1931 // Match specific second argument.
1932 EXPECT_TRUE(
1933 match(Intrinsic5,
1934 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1935 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1936 EXPECT_FALSE(
1937 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1938 m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1939 m_Value())));
1941 // Match specific third argument.
1942 EXPECT_TRUE(
1943 match(Intrinsic5,
1944 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1945 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1946 EXPECT_FALSE(
1947 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1948 m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1949 m_Value())));
1951 // Match specific fourth argument.
1952 EXPECT_TRUE(
1953 match(Intrinsic5,
1954 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1955 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1956 EXPECT_FALSE(
1957 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1958 m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1959 m_Value())));
1961 // Match specific fifth argument.
1962 EXPECT_TRUE(
1963 match(Intrinsic5,
1964 m_Intrinsic<Intrinsic::instrprof_increment_step>(
1965 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1966 EXPECT_FALSE(
1967 match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1968 m_Value(), m_Value(), m_Value(), m_Value(),
1969 m_SpecificInt(10))));
1972 namespace {
1974 struct is_unsigned_zero_pred {
1975 bool isValue(const APInt &C) { return C.isZero(); }
1978 struct is_float_zero_pred {
1979 bool isValue(const APFloat &C) { return C.isZero(); }
1982 template <typename T> struct always_true_pred {
1983 bool isValue(const T &) { return true; }
1986 template <typename T> struct always_false_pred {
1987 bool isValue(const T &) { return false; }
1990 struct is_unsigned_max_pred {
1991 bool isValue(const APInt &C) { return C.isMaxValue(); }
1994 struct is_float_nan_pred {
1995 bool isValue(const APFloat &C) { return C.isNaN(); }
1998 } // namespace
2000 TEST_F(PatternMatchTest, ConstantPredicateType) {
2002 // Scalar integer
2003 APInt U32Max = APInt::getAllOnes(32);
2004 APInt U32Zero = APInt::getZero(32);
2005 APInt U32DeadBeef(32, 0xDEADBEEF);
2007 Type *U32Ty = Type::getInt32Ty(Ctx);
2009 Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max);
2010 Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero);
2011 Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef);
2013 EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>()));
2014 EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
2015 EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>()));
2016 EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>()));
2018 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
2019 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
2020 EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
2021 EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
2023 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
2024 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
2025 EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
2026 EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
2028 // Scalar float
2029 APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle());
2030 APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle());
2031 APFloat F32Pi(3.14f);
2033 Type *F32Ty = Type::getFloatTy(Ctx);
2035 Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN);
2036 Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero);
2037 Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi);
2039 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
2040 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
2041 EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
2042 EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
2044 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
2045 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
2046 EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
2047 EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
2049 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
2050 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
2051 EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
2052 EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
2054 auto FixedEC = ElementCount::getFixed(4);
2055 auto ScalableEC = ElementCount::getScalable(4);
2057 // Vector splat
2059 for (auto EC : {FixedEC, ScalableEC}) {
2060 // integer
2062 Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max);
2063 Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero);
2064 Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef);
2066 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>()));
2067 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
2068 EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>()));
2069 EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>()));
2071 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
2072 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
2073 EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
2074 EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
2076 EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
2077 EXPECT_FALSE(
2078 match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
2079 EXPECT_TRUE(
2080 match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
2081 EXPECT_FALSE(
2082 match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
2084 // float
2086 Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN);
2087 Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero);
2088 Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi);
2090 EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
2091 EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
2092 EXPECT_TRUE(
2093 match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
2094 EXPECT_FALSE(
2095 match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
2097 EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
2098 EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
2099 EXPECT_TRUE(
2100 match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
2101 EXPECT_FALSE(
2102 match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
2104 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
2105 EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
2106 EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
2107 EXPECT_FALSE(
2108 match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
2111 // Int arbitrary vector
2113 Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef});
2114 Constant *CU32Undef = UndefValue::get(U32Ty);
2115 Constant *CU32Poison = PoisonValue::get(U32Ty);
2116 Constant *CU32MaxWithUndef =
2117 ConstantVector::get({CU32Undef, CU32Max, CU32Undef});
2118 Constant *CU32MaxWithPoison =
2119 ConstantVector::get({CU32Poison, CU32Max, CU32Poison});
2121 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>()));
2122 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>()));
2123 EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>()));
2124 EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>()));
2126 EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>()));
2127 EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>()));
2128 EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>()));
2129 EXPECT_FALSE(
2130 match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>()));
2132 EXPECT_TRUE(match(CU32MaxWithPoison, cst_pred_ty<is_unsigned_max_pred>()));
2133 EXPECT_FALSE(match(CU32MaxWithPoison, cst_pred_ty<is_unsigned_zero_pred>()));
2134 EXPECT_TRUE(match(CU32MaxWithPoison, cst_pred_ty<always_true_pred<APInt>>()));
2135 EXPECT_FALSE(
2136 match(CU32MaxWithPoison, cst_pred_ty<always_false_pred<APInt>>()));
2138 // Float arbitrary vector
2140 Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi});
2141 Constant *CF32Undef = UndefValue::get(F32Ty);
2142 Constant *CF32Poison = PoisonValue::get(F32Ty);
2143 Constant *CF32NaNWithUndef =
2144 ConstantVector::get({CF32Undef, CF32NaN, CF32Undef});
2145 Constant *CF32NaNWithPoison =
2146 ConstantVector::get({CF32Poison, CF32NaN, CF32Poison});
2148 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>()));
2149 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>()));
2150 EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>()));
2151 EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>()));
2153 EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>()));
2154 EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>()));
2155 EXPECT_FALSE(
2156 match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>()));
2157 EXPECT_FALSE(
2158 match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
2160 EXPECT_TRUE(match(CF32NaNWithPoison, cstfp_pred_ty<is_float_nan_pred>()));
2161 EXPECT_FALSE(match(CF32NaNWithPoison, cstfp_pred_ty<is_float_zero_pred>()));
2162 EXPECT_TRUE(
2163 match(CF32NaNWithPoison, cstfp_pred_ty<always_true_pred<APFloat>>()));
2164 EXPECT_FALSE(
2165 match(CF32NaNWithPoison, cstfp_pred_ty<always_false_pred<APFloat>>()));
2168 TEST_F(PatternMatchTest, InsertValue) {
2169 Type *StructTy = StructType::create(IRB.getContext(),
2170 {IRB.getInt32Ty(), IRB.getInt64Ty()});
2171 Value *Ins0 =
2172 IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0);
2173 Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1);
2175 EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value())));
2176 EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value())));
2177 EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value())));
2178 EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value())));
2180 EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20))));
2181 EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0))));
2183 EXPECT_TRUE(
2184 match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()),
2185 m_SpecificInt(90))));
2186 EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
2189 TEST_F(PatternMatchTest, LogicalSelects) {
2190 Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
2191 Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
2192 Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
2193 Constant *T = IRB.getInt1(true);
2194 Constant *F = IRB.getInt1(false);
2195 Value *And = IRB.CreateSelect(X, Y, F);
2196 Value *Or = IRB.CreateSelect(X, T, Y);
2198 // Logical and:
2199 // Check basic no-capture logic - opcode and constant must match.
2200 EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Value())));
2201 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Value())));
2202 EXPECT_FALSE(match(And, m_LogicalOr(m_Value(), m_Value())));
2203 EXPECT_FALSE(match(And, m_c_LogicalOr(m_Value(), m_Value())));
2205 // Check with captures.
2206 EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Value())));
2207 EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Specific(Y))));
2208 EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(Y))));
2210 EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Value())));
2211 EXPECT_FALSE(match(And, m_LogicalAnd(m_Value(), m_Specific(X))));
2212 EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(X))));
2214 EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(X))));
2215 EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(Y))));
2217 // Check captures for commutative match.
2218 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Value())));
2219 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(Y))));
2220 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(Y))));
2222 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Value())));
2223 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(X))));
2224 EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(X))));
2226 EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(X))));
2227 EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(Y))));
2229 // Logical or:
2230 // Check basic no-capture logic - opcode and constant must match.
2231 EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Value())));
2232 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Value())));
2233 EXPECT_FALSE(match(Or, m_LogicalAnd(m_Value(), m_Value())));
2234 EXPECT_FALSE(match(Or, m_c_LogicalAnd(m_Value(), m_Value())));
2236 // Check with captures.
2237 EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Value())));
2238 EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Specific(Y))));
2239 EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(Y))));
2241 EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Value())));
2242 EXPECT_FALSE(match(Or, m_LogicalOr(m_Value(), m_Specific(X))));
2243 EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(X))));
2245 EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(X))));
2246 EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(Y))));
2248 // Check captures for commutative match.
2249 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Value())));
2250 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(Y))));
2251 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(Y))));
2253 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Value())));
2254 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(X))));
2255 EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(X))));
2257 EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(X))));
2258 EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(Y))));
2261 TEST_F(PatternMatchTest, VectorLogicalSelects) {
2262 Type *i1 = IRB.getInt1Ty();
2263 Type *v3i1 = FixedVectorType::get(i1, 3);
2265 Value *Alloca = IRB.CreateAlloca(i1);
2266 Value *AllocaVec = IRB.CreateAlloca(v3i1);
2267 Value *Scalar = IRB.CreateLoad(i1, Alloca);
2268 Value *Vector = IRB.CreateLoad(v3i1, AllocaVec);
2269 Constant *F = Constant::getNullValue(v3i1);
2270 Constant *T = Constant::getAllOnesValue(v3i1);
2272 // select <3 x i1> Vector, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
2273 Value *VecAnd = IRB.CreateSelect(Vector, Vector, F);
2275 // select i1 Scalar, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
2276 Value *MixedTypeAnd = IRB.CreateSelect(Scalar, Vector, F);
2278 // select <3 x i1> Vector, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
2279 Value *VecOr = IRB.CreateSelect(Vector, T, Vector);
2281 // select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
2282 Value *MixedTypeOr = IRB.CreateSelect(Scalar, T, Vector);
2284 // We allow matching a real vector logical select,
2285 // but not a scalar select of vector bools.
2286 EXPECT_TRUE(match(VecAnd, m_LogicalAnd(m_Value(), m_Value())));
2287 EXPECT_FALSE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value())));
2288 EXPECT_TRUE(match(VecOr, m_LogicalOr(m_Value(), m_Value())));
2289 EXPECT_FALSE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value())));
2292 TEST_F(PatternMatchTest, VScale) {
2293 DataLayout DL = M->getDataLayout();
2295 Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1);
2296 Value *NullPtrVec =
2297 Constant::getNullValue(PointerType::getUnqual(VecTy->getContext()));
2298 Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1));
2299 Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType()));
2300 EXPECT_TRUE(match(PtrToInt, m_VScale()));
2302 Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2);
2303 Value *NullPtrVec2 =
2304 Constant::getNullValue(PointerType::getUnqual(VecTy2->getContext()));
2305 Value *GEP2 = IRB.CreateGEP(VecTy, NullPtrVec2, IRB.getInt64(1));
2306 Value *PtrToInt2 =
2307 IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType()));
2308 EXPECT_TRUE(match(PtrToInt2, m_VScale()));
2311 TEST_F(PatternMatchTest, NotForbidPoison) {
2312 Type *ScalarTy = IRB.getInt8Ty();
2313 Type *VectorTy = FixedVectorType::get(ScalarTy, 3);
2314 Constant *ScalarUndef = UndefValue::get(ScalarTy);
2315 Constant *ScalarPoison = PoisonValue::get(ScalarTy);
2316 Constant *ScalarOnes = Constant::getAllOnesValue(ScalarTy);
2317 Constant *VectorZero = Constant::getNullValue(VectorTy);
2318 Constant *VectorOnes = Constant::getAllOnesValue(VectorTy);
2320 SmallVector<Constant *, 3> MixedElemsUndef;
2321 MixedElemsUndef.push_back(ScalarOnes);
2322 MixedElemsUndef.push_back(ScalarOnes);
2323 MixedElemsUndef.push_back(ScalarUndef);
2324 Constant *VectorMixedUndef = ConstantVector::get(MixedElemsUndef);
2326 SmallVector<Constant *, 3> MixedElemsPoison;
2327 MixedElemsPoison.push_back(ScalarOnes);
2328 MixedElemsPoison.push_back(ScalarOnes);
2329 MixedElemsPoison.push_back(ScalarPoison);
2330 Constant *VectorMixedPoison = ConstantVector::get(MixedElemsPoison);
2332 Value *Not = IRB.CreateXor(VectorZero, VectorOnes);
2333 Value *X;
2334 EXPECT_TRUE(match(Not, m_Not(m_Value(X))));
2335 EXPECT_TRUE(match(X, m_Zero()));
2336 X = nullptr;
2337 EXPECT_TRUE(match(Not, m_NotForbidPoison(m_Value(X))));
2338 EXPECT_TRUE(match(X, m_Zero()));
2340 Value *NotCommute = IRB.CreateXor(VectorOnes, VectorZero);
2341 Value *Y;
2342 EXPECT_TRUE(match(NotCommute, m_Not(m_Value(Y))));
2343 EXPECT_TRUE(match(Y, m_Zero()));
2344 Y = nullptr;
2345 EXPECT_TRUE(match(NotCommute, m_NotForbidPoison(m_Value(Y))));
2346 EXPECT_TRUE(match(Y, m_Zero()));
2348 Value *NotWithUndefs = IRB.CreateXor(VectorZero, VectorMixedUndef);
2349 EXPECT_FALSE(match(NotWithUndefs, m_Not(m_Value())));
2350 EXPECT_FALSE(match(NotWithUndefs, m_NotForbidPoison(m_Value())));
2352 Value *NotWithPoisons = IRB.CreateXor(VectorZero, VectorMixedPoison);
2353 EXPECT_TRUE(match(NotWithPoisons, m_Not(m_Value())));
2354 EXPECT_FALSE(match(NotWithPoisons, m_NotForbidPoison(m_Value())));
2356 Value *NotWithUndefsCommute = IRB.CreateXor(VectorMixedUndef, VectorZero);
2357 EXPECT_FALSE(match(NotWithUndefsCommute, m_Not(m_Value())));
2358 EXPECT_FALSE(match(NotWithUndefsCommute, m_NotForbidPoison(m_Value())));
2360 Value *NotWithPoisonsCommute = IRB.CreateXor(VectorMixedPoison, VectorZero);
2361 EXPECT_TRUE(match(NotWithPoisonsCommute, m_Not(m_Value())));
2362 EXPECT_FALSE(match(NotWithPoisonsCommute, m_NotForbidPoison(m_Value())));
2365 template <typename T> struct MutableConstTest : PatternMatchTest { };
2367 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
2368 std::tuple<const Value*, const Instruction *>>
2369 MutableConstTestTypes;
2370 TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, );
2372 TYPED_TEST(MutableConstTest, ICmp) {
2373 auto &IRB = PatternMatchTest::IRB;
2375 typedef std::tuple_element_t<0, TypeParam> ValueType;
2376 typedef std::tuple_element_t<1, TypeParam> InstructionType;
2378 Value *L = IRB.getInt32(1);
2379 Value *R = IRB.getInt32(2);
2380 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
2382 ValueType MatchL;
2383 ValueType MatchR;
2384 ICmpInst::Predicate MatchPred;
2386 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
2387 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2388 EXPECT_EQ(L, MatchL);
2389 EXPECT_EQ(R, MatchR);
2391 EXPECT_TRUE(m_Cmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
2392 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2393 EXPECT_EQ(L, MatchL);
2394 EXPECT_EQ(R, MatchR);
2396 EXPECT_TRUE(m_ICmp(m_Specific(L), m_Specific(R))
2397 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2399 EXPECT_TRUE(m_Cmp(m_Specific(L), m_Specific(R))
2400 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2402 EXPECT_FALSE(m_ICmp(m_Specific(R), m_Specific(L))
2403 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2404 EXPECT_FALSE(m_Cmp(m_Specific(R), m_Specific(L))
2405 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2407 EXPECT_TRUE(m_c_ICmp(m_Specific(R), m_Specific(L))
2408 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2410 EXPECT_FALSE(m_c_ICmp(m_Specific(R), m_Specific(R))
2411 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2413 EXPECT_TRUE(m_SpecificICmp(Pred, m_Specific(L), m_Specific(R))
2414 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2415 EXPECT_TRUE(m_SpecificCmp(Pred, m_Specific(L), m_Specific(R))
2416 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2418 EXPECT_FALSE(m_SpecificICmp(Pred, m_Specific(R), m_Specific(L))
2419 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2420 EXPECT_FALSE(m_SpecificCmp(Pred, m_Specific(R), m_Specific(L))
2421 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2423 MatchL = nullptr;
2424 MatchR = nullptr;
2425 EXPECT_TRUE(m_SpecificICmp(Pred, m_Value(MatchL), m_Value(MatchR))
2426 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2427 EXPECT_EQ(L, MatchL);
2428 EXPECT_EQ(R, MatchR);
2429 MatchL = nullptr;
2430 MatchR = nullptr;
2431 EXPECT_TRUE(m_SpecificCmp(Pred, m_Value(MatchL), m_Value(MatchR))
2432 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2433 EXPECT_EQ(L, MatchL);
2434 EXPECT_EQ(R, MatchR);
2436 EXPECT_FALSE(m_SpecificICmp(Pred, m_Specific(R), m_Specific(L))
2437 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2438 EXPECT_FALSE(m_SpecificCmp(Pred, m_Specific(R), m_Specific(L))
2439 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2441 EXPECT_FALSE(m_SpecificICmp(ICmpInst::getInversePredicate(Pred),
2442 m_Specific(L), m_Specific(R))
2443 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2444 EXPECT_FALSE(m_SpecificCmp(ICmpInst::getInversePredicate(Pred), m_Specific(L),
2445 m_Specific(R))
2446 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2448 EXPECT_FALSE(m_SpecificICmp(ICmpInst::getInversePredicate(Pred),
2449 m_Value(MatchL), m_Value(MatchR))
2450 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2451 EXPECT_FALSE(m_SpecificCmp(ICmpInst::getInversePredicate(Pred),
2452 m_Value(MatchL), m_Value(MatchR))
2453 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2455 EXPECT_TRUE(m_c_SpecificICmp(Pred, m_Specific(L), m_Specific(R))
2456 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2457 EXPECT_TRUE(m_c_SpecificICmp(ICmpInst::getSwappedPredicate(Pred),
2458 m_Specific(R), m_Specific(L))
2459 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2460 EXPECT_FALSE(m_c_SpecificICmp(Pred, m_Specific(R), m_Specific(L))
2461 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2464 TYPED_TEST(MutableConstTest, FCmp) {
2465 auto &IRB = PatternMatchTest::IRB;
2467 typedef std::tuple_element_t<0, TypeParam> ValueType;
2468 typedef std::tuple_element_t<1, TypeParam> InstructionType;
2470 Value *L = Constant::getNullValue(IRB.getFloatTy());
2471 Value *R = ConstantFP::getInfinity(IRB.getFloatTy(), true);
2472 FCmpInst::Predicate Pred = FCmpInst::FCMP_OGT;
2474 ValueType MatchL;
2475 ValueType MatchR;
2476 FCmpInst::Predicate MatchPred;
2478 EXPECT_TRUE(m_FCmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
2479 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2480 EXPECT_EQ(L, MatchL);
2481 EXPECT_EQ(R, MatchR);
2483 EXPECT_TRUE(m_Cmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
2484 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2485 EXPECT_EQ(L, MatchL);
2486 EXPECT_EQ(R, MatchR);
2488 EXPECT_TRUE(m_FCmp(m_Specific(L), m_Specific(R))
2489 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2491 EXPECT_TRUE(m_Cmp(m_Specific(L), m_Specific(R))
2492 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2494 EXPECT_FALSE(m_FCmp(m_Specific(R), m_Specific(L))
2495 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2496 EXPECT_FALSE(m_Cmp(m_Specific(R), m_Specific(L))
2497 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2499 EXPECT_TRUE(m_SpecificFCmp(Pred, m_Specific(L), m_Specific(R))
2500 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2501 EXPECT_TRUE(m_SpecificCmp(Pred, m_Specific(L), m_Specific(R))
2502 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2504 EXPECT_FALSE(m_SpecificFCmp(Pred, m_Specific(R), m_Specific(L))
2505 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2506 EXPECT_FALSE(m_SpecificCmp(Pred, m_Specific(R), m_Specific(L))
2507 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2509 MatchL = nullptr;
2510 MatchR = nullptr;
2511 EXPECT_TRUE(m_SpecificFCmp(Pred, m_Value(MatchL), m_Value(MatchR))
2512 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2513 EXPECT_EQ(L, MatchL);
2514 EXPECT_EQ(R, MatchR);
2515 MatchL = nullptr;
2516 MatchR = nullptr;
2517 EXPECT_TRUE(m_SpecificCmp(Pred, m_Value(MatchL), m_Value(MatchR))
2518 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2519 EXPECT_EQ(L, MatchL);
2520 EXPECT_EQ(R, MatchR);
2522 EXPECT_FALSE(m_SpecificFCmp(Pred, m_Specific(R), m_Specific(L))
2523 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2524 EXPECT_FALSE(m_SpecificCmp(Pred, m_Specific(R), m_Specific(L))
2525 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2527 EXPECT_FALSE(m_SpecificFCmp(FCmpInst::getInversePredicate(Pred),
2528 m_Specific(L), m_Specific(R))
2529 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2530 EXPECT_FALSE(m_SpecificCmp(FCmpInst::getInversePredicate(Pred), m_Specific(L),
2531 m_Specific(R))
2532 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2534 EXPECT_FALSE(m_SpecificFCmp(FCmpInst::getInversePredicate(Pred),
2535 m_Value(MatchL), m_Value(MatchR))
2536 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2537 EXPECT_FALSE(m_SpecificCmp(FCmpInst::getInversePredicate(Pred),
2538 m_Value(MatchL), m_Value(MatchR))
2539 .match((InstructionType)IRB.CreateFCmp(Pred, L, R)));
2542 TEST_F(PatternMatchTest, ConstExpr) {
2543 Constant *G =
2544 M->getOrInsertGlobal("dummy", PointerType::getUnqual(IRB.getInt32Ty()));
2545 Constant *S = ConstantExpr::getPtrToInt(G, IRB.getInt32Ty());
2546 Type *VecTy = FixedVectorType::get(IRB.getInt32Ty(), 2);
2547 PoisonValue *P = PoisonValue::get(VecTy);
2548 Constant *V = ConstantExpr::getInsertElement(P, S, IRB.getInt32(0));
2550 // The match succeeds on a constant that is a constant expression itself
2551 // or a constant that contains a constant expression.
2552 EXPECT_TRUE(match(S, m_ConstantExpr()));
2553 EXPECT_TRUE(match(V, m_ConstantExpr()));
2556 TEST_F(PatternMatchTest, PtrAdd) {
2557 Type *PtrTy = PointerType::getUnqual(Ctx);
2558 Type *IdxTy = Type::getInt64Ty(Ctx);
2559 Constant *Null = Constant::getNullValue(PtrTy);
2560 Constant *Offset = ConstantInt::get(IdxTy, 42);
2561 Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
2562 Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2563 Value *PtrAddConst =
2564 ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2566 Value *A, *B;
2567 EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2568 EXPECT_EQ(A, Null);
2569 EXPECT_EQ(B, Offset);
2571 EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2572 EXPECT_EQ(A, Null);
2573 EXPECT_EQ(B, Offset);
2575 EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2578 } // anonymous namespace.