xref: /llvm-project/llvm/unittests/IR/PatternMatch.cpp (revision d77eb9ea598f6e56a583eac40f95ca59b3130523)
1 //===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/PatternMatch.h"
10 #include "llvm/ADT/APSInt.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/NoFolder.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/Type.h"
26 #include "gtest/gtest.h"
27 
28 using namespace llvm;
29 using namespace llvm::PatternMatch;
30 
31 namespace {
32 
33 struct PatternMatchTest : ::testing::Test {
34   LLVMContext Ctx;
35   std::unique_ptr<Module> M;
36   Function *F;
37   BasicBlock *BB;
38   IRBuilder<NoFolder> IRB;
39 
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) {}
46 };
47 
48 TEST_F(PatternMatchTest, OneUse) {
49   // Build up a little tree of values:
50   //
51   //   One  = (1 + 2) + 42
52   //   Two  = One + 42
53   //   Leaf = (Two + 8) + (Two + 13)
54   Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)),
55                              IRB.getInt32(42));
56   Value *Two = IRB.CreateAdd(One, IRB.getInt32(42));
57   Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)),
58                               IRB.CreateAdd(Two, IRB.getInt32(13)));
59   Value *V;
60 
61   EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
62   EXPECT_EQ(One, V);
63 
64   EXPECT_FALSE(m_OneUse(m_Value()).match(Two));
65   EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
66 }
67 
68 TEST_F(PatternMatchTest, SpecificIntEQ) {
69   Type *IntTy = IRB.getInt32Ty();
70   unsigned BitWidth = IntTy->getScalarSizeInBits();
71 
72   Value *Zero = ConstantInt::get(IntTy, 0);
73   Value *One = ConstantInt::get(IntTy, 1);
74   Value *NegOne = ConstantInt::get(IntTy, -1);
75 
76   EXPECT_TRUE(
77       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
78           .match(Zero));
79   EXPECT_FALSE(
80       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
81           .match(One));
82   EXPECT_FALSE(
83       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
84           .match(NegOne));
85 
86   EXPECT_FALSE(
87       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
88           .match(Zero));
89   EXPECT_TRUE(
90       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
91           .match(One));
92   EXPECT_FALSE(
93       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
94           .match(NegOne));
95 
96   EXPECT_FALSE(
97       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
98           .match(Zero));
99   EXPECT_FALSE(
100       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
101           .match(One));
102   EXPECT_TRUE(
103       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
104           .match(NegOne));
105 }
106 
107 TEST_F(PatternMatchTest, SpecificIntNE) {
108   Type *IntTy = IRB.getInt32Ty();
109   unsigned BitWidth = IntTy->getScalarSizeInBits();
110 
111   Value *Zero = ConstantInt::get(IntTy, 0);
112   Value *One = ConstantInt::get(IntTy, 1);
113   Value *NegOne = ConstantInt::get(IntTy, -1);
114 
115   EXPECT_FALSE(
116       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
117           .match(Zero));
118   EXPECT_TRUE(
119       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
120           .match(One));
121   EXPECT_TRUE(
122       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
123           .match(NegOne));
124 
125   EXPECT_TRUE(
126       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
127           .match(Zero));
128   EXPECT_FALSE(
129       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
130           .match(One));
131   EXPECT_TRUE(
132       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
133           .match(NegOne));
134 
135   EXPECT_TRUE(
136       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
137           .match(Zero));
138   EXPECT_TRUE(
139       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
140           .match(One));
141   EXPECT_FALSE(
142       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
143           .match(NegOne));
144 }
145 
146 TEST_F(PatternMatchTest, SpecificIntUGT) {
147   Type *IntTy = IRB.getInt32Ty();
148   unsigned BitWidth = IntTy->getScalarSizeInBits();
149 
150   Value *Zero = ConstantInt::get(IntTy, 0);
151   Value *One = ConstantInt::get(IntTy, 1);
152   Value *NegOne = ConstantInt::get(IntTy, -1);
153 
154   EXPECT_FALSE(
155       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
156           .match(Zero));
157   EXPECT_TRUE(
158       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
159           .match(One));
160   EXPECT_TRUE(
161       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
162           .match(NegOne));
163 
164   EXPECT_FALSE(
165       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
166           .match(Zero));
167   EXPECT_FALSE(
168       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
169           .match(One));
170   EXPECT_TRUE(
171       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
172           .match(NegOne));
173 
174   EXPECT_FALSE(
175       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
176           .match(Zero));
177   EXPECT_FALSE(
178       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
179           .match(One));
180   EXPECT_FALSE(
181       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
182           .match(NegOne));
183 }
184 
185 TEST_F(PatternMatchTest, SignbitZeroChecks) {
186   Type *IntTy = IRB.getInt32Ty();
187 
188   Value *Zero = ConstantInt::get(IntTy, 0);
189   Value *One = ConstantInt::get(IntTy, 1);
190   Value *NegOne = ConstantInt::get(IntTy, -1);
191 
192   EXPECT_TRUE(m_Negative().match(NegOne));
193   EXPECT_FALSE(m_NonNegative().match(NegOne));
194   EXPECT_FALSE(m_StrictlyPositive().match(NegOne));
195   EXPECT_TRUE(m_NonPositive().match(NegOne));
196 
197   EXPECT_FALSE(m_Negative().match(Zero));
198   EXPECT_TRUE(m_NonNegative().match(Zero));
199   EXPECT_FALSE(m_StrictlyPositive().match(Zero));
200   EXPECT_TRUE(m_NonPositive().match(Zero));
201 
202   EXPECT_FALSE(m_Negative().match(One));
203   EXPECT_TRUE(m_NonNegative().match(One));
204   EXPECT_TRUE(m_StrictlyPositive().match(One));
205   EXPECT_FALSE(m_NonPositive().match(One));
206 }
207 
208 TEST_F(PatternMatchTest, SpecificIntUGE) {
209   Type *IntTy = IRB.getInt32Ty();
210   unsigned BitWidth = IntTy->getScalarSizeInBits();
211 
212   Value *Zero = ConstantInt::get(IntTy, 0);
213   Value *One = ConstantInt::get(IntTy, 1);
214   Value *NegOne = ConstantInt::get(IntTy, -1);
215 
216   EXPECT_TRUE(
217       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
218           .match(Zero));
219   EXPECT_TRUE(
220       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
221           .match(One));
222   EXPECT_TRUE(
223       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
224           .match(NegOne));
225 
226   EXPECT_FALSE(
227       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
228           .match(Zero));
229   EXPECT_TRUE(
230       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
231           .match(One));
232   EXPECT_TRUE(
233       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
234           .match(NegOne));
235 
236   EXPECT_FALSE(
237       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
238           .match(Zero));
239   EXPECT_FALSE(
240       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
241           .match(One));
242   EXPECT_TRUE(
243       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
244           .match(NegOne));
245 }
246 
247 TEST_F(PatternMatchTest, SpecificIntULT) {
248   Type *IntTy = IRB.getInt32Ty();
249   unsigned BitWidth = IntTy->getScalarSizeInBits();
250 
251   Value *Zero = ConstantInt::get(IntTy, 0);
252   Value *One = ConstantInt::get(IntTy, 1);
253   Value *NegOne = ConstantInt::get(IntTy, -1);
254 
255   EXPECT_FALSE(
256       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
257           .match(Zero));
258   EXPECT_FALSE(
259       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
260           .match(One));
261   EXPECT_FALSE(
262       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
263           .match(NegOne));
264 
265   EXPECT_TRUE(
266       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
267           .match(Zero));
268   EXPECT_FALSE(
269       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
270           .match(One));
271   EXPECT_FALSE(
272       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
273           .match(NegOne));
274 
275   EXPECT_TRUE(
276       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
277           .match(Zero));
278   EXPECT_TRUE(
279       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
280           .match(One));
281   EXPECT_FALSE(
282       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
283           .match(NegOne));
284 }
285 
286 TEST_F(PatternMatchTest, SpecificIntULE) {
287   Type *IntTy = IRB.getInt32Ty();
288   unsigned BitWidth = IntTy->getScalarSizeInBits();
289 
290   Value *Zero = ConstantInt::get(IntTy, 0);
291   Value *One = ConstantInt::get(IntTy, 1);
292   Value *NegOne = ConstantInt::get(IntTy, -1);
293 
294   EXPECT_TRUE(
295       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
296           .match(Zero));
297   EXPECT_FALSE(
298       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
299           .match(One));
300   EXPECT_FALSE(
301       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
302           .match(NegOne));
303 
304   EXPECT_TRUE(
305       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
306           .match(Zero));
307   EXPECT_TRUE(
308       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
309           .match(One));
310   EXPECT_FALSE(
311       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
312           .match(NegOne));
313 
314   EXPECT_TRUE(
315       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
316           .match(Zero));
317   EXPECT_TRUE(
318       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
319           .match(One));
320   EXPECT_TRUE(
321       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
322           .match(NegOne));
323 }
324 
325 TEST_F(PatternMatchTest, SpecificIntSGT) {
326   Type *IntTy = IRB.getInt32Ty();
327   unsigned BitWidth = IntTy->getScalarSizeInBits();
328 
329   Value *Zero = ConstantInt::get(IntTy, 0);
330   Value *One = ConstantInt::get(IntTy, 1);
331   Value *NegOne = ConstantInt::get(IntTy, -1);
332 
333   EXPECT_FALSE(
334       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
335           .match(Zero));
336   EXPECT_TRUE(
337       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
338           .match(One));
339   EXPECT_FALSE(
340       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
341           .match(NegOne));
342 
343   EXPECT_FALSE(
344       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
345           .match(Zero));
346   EXPECT_FALSE(
347       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
348           .match(One));
349   EXPECT_FALSE(
350       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
351           .match(NegOne));
352 
353   EXPECT_TRUE(
354       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
355           .match(Zero));
356   EXPECT_TRUE(
357       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
358           .match(One));
359   EXPECT_FALSE(
360       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
361           .match(NegOne));
362 }
363 
364 TEST_F(PatternMatchTest, SpecificIntSGE) {
365   Type *IntTy = IRB.getInt32Ty();
366   unsigned BitWidth = IntTy->getScalarSizeInBits();
367 
368   Value *Zero = ConstantInt::get(IntTy, 0);
369   Value *One = ConstantInt::get(IntTy, 1);
370   Value *NegOne = ConstantInt::get(IntTy, -1);
371 
372   EXPECT_TRUE(
373       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
374           .match(Zero));
375   EXPECT_TRUE(
376       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
377           .match(One));
378   EXPECT_FALSE(
379       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
380           .match(NegOne));
381 
382   EXPECT_FALSE(
383       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
384           .match(Zero));
385   EXPECT_TRUE(
386       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
387           .match(One));
388   EXPECT_FALSE(
389       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
390           .match(NegOne));
391 
392   EXPECT_TRUE(
393       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
394           .match(Zero));
395   EXPECT_TRUE(
396       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
397           .match(One));
398   EXPECT_TRUE(
399       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
400           .match(NegOne));
401 }
402 
403 TEST_F(PatternMatchTest, SpecificIntSLT) {
404   Type *IntTy = IRB.getInt32Ty();
405   unsigned BitWidth = IntTy->getScalarSizeInBits();
406 
407   Value *Zero = ConstantInt::get(IntTy, 0);
408   Value *One = ConstantInt::get(IntTy, 1);
409   Value *NegOne = ConstantInt::get(IntTy, -1);
410 
411   EXPECT_FALSE(
412       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
413           .match(Zero));
414   EXPECT_FALSE(
415       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
416           .match(One));
417   EXPECT_TRUE(
418       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
419           .match(NegOne));
420 
421   EXPECT_TRUE(
422       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
423           .match(Zero));
424   EXPECT_FALSE(
425       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
426           .match(One));
427   EXPECT_TRUE(
428       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
429           .match(NegOne));
430 
431   EXPECT_FALSE(
432       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
433           .match(Zero));
434   EXPECT_FALSE(
435       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
436           .match(One));
437   EXPECT_FALSE(
438       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
439           .match(NegOne));
440 }
441 
442 TEST_F(PatternMatchTest, SpecificIntSLE) {
443   Type *IntTy = IRB.getInt32Ty();
444   unsigned BitWidth = IntTy->getScalarSizeInBits();
445 
446   Value *Zero = ConstantInt::get(IntTy, 0);
447   Value *One = ConstantInt::get(IntTy, 1);
448   Value *NegOne = ConstantInt::get(IntTy, -1);
449 
450   EXPECT_TRUE(
451       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
452           .match(Zero));
453   EXPECT_FALSE(
454       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
455           .match(One));
456   EXPECT_TRUE(
457       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
458           .match(NegOne));
459 
460   EXPECT_TRUE(
461       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
462           .match(Zero));
463   EXPECT_TRUE(
464       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
465           .match(One));
466   EXPECT_TRUE(
467       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
468           .match(NegOne));
469 
470   EXPECT_FALSE(
471       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
472           .match(Zero));
473   EXPECT_FALSE(
474       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
475           .match(One));
476   EXPECT_TRUE(
477       m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
478           .match(NegOne));
479 }
480 
481 TEST_F(PatternMatchTest, Unless) {
482   Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
483 
484   EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X));
485   EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X));
486 
487   EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X));
488   EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X));
489 
490   EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X));
491   EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X));
492 
493   EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X));
494   EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
495 }
496 
497 TEST_F(PatternMatchTest, ZExtSExtSelf) {
498   LLVMContext &Ctx = IRB.getContext();
499 
500   Value *One32 = IRB.getInt32(1);
501   Value *One64Z = IRB.CreateZExt(One32, IntegerType::getInt64Ty(Ctx));
502   Value *One64S = IRB.CreateSExt(One32, IntegerType::getInt64Ty(Ctx));
503 
504   EXPECT_TRUE(m_One().match(One32));
505   EXPECT_FALSE(m_One().match(One64Z));
506   EXPECT_FALSE(m_One().match(One64S));
507 
508   EXPECT_FALSE(m_ZExt(m_One()).match(One32));
509   EXPECT_TRUE(m_ZExt(m_One()).match(One64Z));
510   EXPECT_FALSE(m_ZExt(m_One()).match(One64S));
511 
512   EXPECT_FALSE(m_SExt(m_One()).match(One32));
513   EXPECT_FALSE(m_SExt(m_One()).match(One64Z));
514   EXPECT_TRUE(m_SExt(m_One()).match(One64S));
515 
516   EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One32));
517   EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One64Z));
518   EXPECT_FALSE(m_ZExtOrSelf(m_One()).match(One64S));
519 
520   EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One32));
521   EXPECT_FALSE(m_SExtOrSelf(m_One()).match(One64Z));
522   EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One64S));
523 
524   EXPECT_FALSE(m_ZExtOrSExt(m_One()).match(One32));
525   EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64Z));
526   EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64S));
527 
528   EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One32));
529   EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64Z));
530   EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64S));
531 }
532 
533 TEST_F(PatternMatchTest, BitCast) {
534   Value *OneDouble = ConstantFP::get(IRB.getDoubleTy(), APFloat(1.0));
535   Value *ScalableDouble = ConstantFP::get(
536       VectorType::get(IRB.getDoubleTy(), 2, /*Scalable=*/true), APFloat(1.0));
537   // scalar -> scalar
538   Value *DoubleToI64 = IRB.CreateBitCast(OneDouble, IRB.getInt64Ty());
539   // scalar -> vector
540   Value *DoubleToV2I32 = IRB.CreateBitCast(
541       OneDouble, VectorType::get(IRB.getInt32Ty(), 2, /*Scalable=*/false));
542   // vector -> scalar
543   Value *V2I32ToDouble = IRB.CreateBitCast(DoubleToV2I32, IRB.getDoubleTy());
544   // vector -> vector (same count)
545   Value *V2I32ToV2Float = IRB.CreateBitCast(
546       DoubleToV2I32, VectorType::get(IRB.getFloatTy(), 2, /*Scalable=*/false));
547   // vector -> vector (different count)
548   Value *V2I32TOV4I16 = IRB.CreateBitCast(
549       DoubleToV2I32, VectorType::get(IRB.getInt16Ty(), 4, /*Scalable=*/false));
550   // scalable vector -> scalable vector (same count)
551   Value *NXV2DoubleToNXV2I64 = IRB.CreateBitCast(
552       ScalableDouble, VectorType::get(IRB.getInt64Ty(), 2, /*Scalable=*/true));
553   // scalable vector -> scalable vector (different count)
554   Value *NXV2I64ToNXV4I32 = IRB.CreateBitCast(
555       NXV2DoubleToNXV2I64,
556       VectorType::get(IRB.getInt32Ty(), 4, /*Scalable=*/true));
557 
558   EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToI64));
559   EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToV2I32));
560   EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToDouble));
561   EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToV2Float));
562   EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32TOV4I16));
563   EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2DoubleToNXV2I64));
564   EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2I64ToNXV4I32));
565 
566   EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(DoubleToI64));
567   EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(DoubleToV2I32));
568   EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32ToDouble));
569   EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(V2I32ToV2Float));
570   EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32TOV4I16));
571   EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(NXV2DoubleToNXV2I64));
572   EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(NXV2I64ToNXV4I32));
573 }
574 
575 TEST_F(PatternMatchTest, Power2) {
576   Value *C128 = IRB.getInt32(128);
577   Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
578 
579   EXPECT_TRUE(m_Power2().match(C128));
580   EXPECT_FALSE(m_Power2().match(CNeg128));
581 
582   EXPECT_TRUE(m_Power2OrZero().match(C128));
583   EXPECT_FALSE(m_Power2OrZero().match(CNeg128));
584 
585   EXPECT_FALSE(m_NegatedPower2().match(C128));
586   EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
587 
588   EXPECT_FALSE(m_NegatedPower2OrZero().match(C128));
589   EXPECT_TRUE(m_NegatedPower2OrZero().match(CNeg128));
590 
591   Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
592   Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
593 
594   EXPECT_TRUE(m_Power2().match(CIntMin));
595   EXPECT_TRUE(m_Power2().match(CNegIntMin));
596 
597   EXPECT_TRUE(m_Power2OrZero().match(CIntMin));
598   EXPECT_TRUE(m_Power2OrZero().match(CNegIntMin));
599 
600   EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
601   EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
602 
603   EXPECT_TRUE(m_NegatedPower2OrZero().match(CIntMin));
604   EXPECT_TRUE(m_NegatedPower2OrZero().match(CNegIntMin));
605 
606   Value *CZero = IRB.getInt64(0);
607 
608   EXPECT_FALSE(m_Power2().match(CZero));
609 
610   EXPECT_TRUE(m_Power2OrZero().match(CZero));
611 
612   EXPECT_FALSE(m_NegatedPower2().match(CZero));
613 
614   EXPECT_TRUE(m_NegatedPower2OrZero().match(CZero));
615 }
616 
617 TEST_F(PatternMatchTest, Not) {
618   Value *C1 = IRB.getInt32(1);
619   Value *C2 = IRB.getInt32(2);
620   Value *C3 = IRB.getInt32(3);
621   Instruction *Not = BinaryOperator::CreateXor(C1, C2);
622 
623   // When `m_Not` does not match the `not` itself,
624   // it should not try to apply the inner matcher.
625   Value *Val = C3;
626   EXPECT_FALSE(m_Not(m_Value(Val)).match(Not));
627   EXPECT_EQ(Val, C3);
628   Not->deleteValue();
629 }
630 
631 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
632   Value *X = IRB.getInt32(1);
633   Value *Y = IRB.getInt32(2);
634 
635   {
636     Value *tX = X;
637     EXPECT_TRUE(match(X, m_Deferred(tX)));
638     EXPECT_FALSE(match(Y, m_Deferred(tX)));
639   }
640   {
641     const Value *tX = X;
642     EXPECT_TRUE(match(X, m_Deferred(tX)));
643     EXPECT_FALSE(match(Y, m_Deferred(tX)));
644   }
645   {
646     Value *const tX = X;
647     EXPECT_TRUE(match(X, m_Deferred(tX)));
648     EXPECT_FALSE(match(Y, m_Deferred(tX)));
649   }
650   {
651     const Value *const tX = X;
652     EXPECT_TRUE(match(X, m_Deferred(tX)));
653     EXPECT_FALSE(match(Y, m_Deferred(tX)));
654   }
655 
656   {
657     Value *tX = nullptr;
658     EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
659     EXPECT_EQ(tX, X);
660   }
661   {
662     Value *tX = nullptr;
663     EXPECT_FALSE(
664         match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
665   }
666 
667   auto checkMatch = [X, Y](Value *Pattern) {
668     Value *tX = nullptr, *tY = nullptr;
669     EXPECT_TRUE(match(
670         Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
671     EXPECT_EQ(tX, X);
672     EXPECT_EQ(tY, Y);
673   };
674 
675   checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
676   checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
677   checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
678   checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
679 }
680 
681 TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
682   Type *FltTy = IRB.getFloatTy();
683   Value *L = ConstantFP::get(FltTy, 1.0);
684   Value *R = ConstantFP::get(FltTy, 2.0);
685   Value *MatchL, *MatchR;
686 
687   // Test OLT.
688   EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
689                   .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
690   EXPECT_EQ(L, MatchL);
691   EXPECT_EQ(R, MatchR);
692 
693   // Test OLE.
694   EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
695                   .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
696   EXPECT_EQ(L, MatchL);
697   EXPECT_EQ(R, MatchR);
698 
699   // Test no match on OGE.
700   EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
701                    .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
702 
703   // Test no match on OGT.
704   EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
705                    .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
706 
707   // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
708   // %cmp = fcmp oge L, R
709   // %min = select %cmp R, L
710   // Given L == NaN
711   // the above is expanded to %cmp == false ==> %min = L
712   // which is true for UnordFMin, not OrdFMin, so test that:
713 
714   // [OU]GE with inverted select.
715   EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
716                   .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
717   EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
718                   .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
719   EXPECT_EQ(L, MatchL);
720   EXPECT_EQ(R, MatchR);
721 
722   // [OU]GT with inverted select.
723   EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
724                   .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
725   EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
726                   .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
727   EXPECT_EQ(L, MatchL);
728   EXPECT_EQ(R, MatchR);
729 }
730 
731 TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
732   Type *FltTy = IRB.getFloatTy();
733   Value *L = ConstantFP::get(FltTy, 1.0);
734   Value *R = ConstantFP::get(FltTy, 2.0);
735   Value *MatchL, *MatchR;
736 
737   // Test OGT.
738   EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
739                   .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
740   EXPECT_EQ(L, MatchL);
741   EXPECT_EQ(R, MatchR);
742 
743   // Test OGE.
744   EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
745                   .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
746   EXPECT_EQ(L, MatchL);
747   EXPECT_EQ(R, MatchR);
748 
749   // Test no match on OLE.
750   EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
751                    .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
752 
753   // Test no match on OLT.
754   EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
755                    .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
756 
757 
758   // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
759   // %cmp = fcmp ole L, R
760   // %max = select %cmp, R, L
761   // Given L == NaN,
762   // the above is expanded to %cmp == false ==> %max == L
763   // which is true for UnordFMax, not OrdFMax, so test that:
764 
765   // [OU]LE with inverted select.
766   EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
767                    .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
768   EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
769                   .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
770   EXPECT_EQ(L, MatchL);
771   EXPECT_EQ(R, MatchR);
772 
773   // [OUT]LT with inverted select.
774   EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
775                    .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
776   EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
777                   .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
778   EXPECT_EQ(L, MatchL);
779   EXPECT_EQ(R, MatchR);
780 }
781 
782 TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
783   Type *FltTy = IRB.getFloatTy();
784   Value *L = ConstantFP::get(FltTy, 1.0);
785   Value *R = ConstantFP::get(FltTy, 2.0);
786   Value *MatchL, *MatchR;
787 
788   // Test ULT.
789   EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
790                   .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
791   EXPECT_EQ(L, MatchL);
792   EXPECT_EQ(R, MatchR);
793 
794   // Test ULE.
795   EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
796                   .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
797   EXPECT_EQ(L, MatchL);
798   EXPECT_EQ(R, MatchR);
799 
800   // Test no match on UGE.
801   EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
802                    .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
803 
804   // Test no match on UGT.
805   EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
806                    .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
807 
808   // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
809   // %cmp = fcmp uge L, R
810   // %min = select %cmp R, L
811   // Given L == NaN
812   // the above is expanded to %cmp == true ==> %min = R
813   // which is true for OrdFMin, not UnordFMin, so test that:
814 
815   // [UO]GE with inverted select.
816   EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
817                   .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
818   EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
819                   .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
820   EXPECT_EQ(L, MatchL);
821   EXPECT_EQ(R, MatchR);
822 
823   // [UO]GT with inverted select.
824   EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
825                   .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
826   EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
827                   .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
828   EXPECT_EQ(L, MatchL);
829   EXPECT_EQ(R, MatchR);
830 }
831 
832 TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
833   Type *FltTy = IRB.getFloatTy();
834   Value *L = ConstantFP::get(FltTy, 1.0);
835   Value *R = ConstantFP::get(FltTy, 2.0);
836   Value *MatchL, *MatchR;
837 
838   // Test UGT.
839   EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
840                   .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
841   EXPECT_EQ(L, MatchL);
842   EXPECT_EQ(R, MatchR);
843 
844   // Test UGE.
845   EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
846                   .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
847   EXPECT_EQ(L, MatchL);
848   EXPECT_EQ(R, MatchR);
849 
850   // Test no match on ULE.
851   EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
852                    .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
853 
854   // Test no match on ULT.
855   EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
856                    .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
857 
858   // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
859   // %cmp = fcmp ule L, R
860   // %max = select %cmp R, L
861   // Given L == NaN
862   // the above is expanded to %cmp == true ==> %max = R
863   // which is true for OrdFMax, not UnordFMax, so test that:
864 
865   // [UO]LE with inverted select.
866   EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
867                   .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
868   EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
869                   .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
870   EXPECT_EQ(L, MatchL);
871   EXPECT_EQ(R, MatchR);
872 
873   // [UO]LT with inverted select.
874   EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
875                   .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
876   EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
877                   .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
878   EXPECT_EQ(L, MatchL);
879   EXPECT_EQ(R, MatchR);
880 }
881 
882 TEST_F(PatternMatchTest, OverflowingBinOps) {
883   Value *L = IRB.getInt32(1);
884   Value *R = IRB.getInt32(2);
885   Value *MatchL, *MatchR;
886 
887   EXPECT_TRUE(
888       m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
889   EXPECT_EQ(L, MatchL);
890   EXPECT_EQ(R, MatchR);
891   MatchL = MatchR = nullptr;
892   EXPECT_TRUE(
893       m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
894   EXPECT_EQ(L, MatchL);
895   EXPECT_EQ(R, MatchR);
896   MatchL = MatchR = nullptr;
897   EXPECT_TRUE(
898       m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
899   EXPECT_EQ(L, MatchL);
900   EXPECT_EQ(R, MatchR);
901   MatchL = MatchR = nullptr;
902   EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
903       IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
904   EXPECT_EQ(L, MatchL);
905   EXPECT_EQ(R, MatchR);
906 
907   EXPECT_TRUE(
908       m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
909   EXPECT_EQ(L, MatchL);
910   EXPECT_EQ(R, MatchR);
911   MatchL = MatchR = nullptr;
912   EXPECT_TRUE(
913       m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
914   EXPECT_EQ(L, MatchL);
915   EXPECT_EQ(R, MatchR);
916   MatchL = MatchR = nullptr;
917   EXPECT_TRUE(
918       m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
919   EXPECT_EQ(L, MatchL);
920   EXPECT_EQ(R, MatchR);
921   MatchL = MatchR = nullptr;
922   EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
923       IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
924   EXPECT_EQ(L, MatchL);
925   EXPECT_EQ(R, MatchR);
926 
927   EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
928   EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
929   EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
930   EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
931   EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
932   EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
933   EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
934   EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
935   EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
936   EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
937   EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
938       IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
939   EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
940 
941   EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
942   EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
943   EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
944   EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
945   EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
946   EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
947   EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
948   EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
949   EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
950   EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
951   EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
952       IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
953   EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
954 }
955 
956 TEST_F(PatternMatchTest, LoadStoreOps) {
957   // Create this load/store sequence:
958   //
959   //  %p = alloca i32*
960   //  %0 = load i32*, i32** %p
961   //  store i32 42, i32* %0
962 
963   Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
964   Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
965   Value *FourtyTwo = IRB.getInt32(42);
966   Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
967   Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
968 
969   EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
970   EXPECT_EQ(Alloca, MatchLoad);
971 
972   EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
973 
974   EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
975 
976   EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
977                 .match(StoreInst));
978   EXPECT_EQ(FourtyTwo, MatchStoreVal);
979   EXPECT_EQ(Alloca, MatchStorePointer);
980 
981   EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
982                 .match(Alloca));
983 
984   EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
985                 .match(StoreInst));
986   EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
987                 .match(StoreInst));
988   EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
989                 .match(StoreInst));
990 }
991 
992 TEST_F(PatternMatchTest, VectorOps) {
993   // Build up small tree of vector operations
994   //
995   //   Val = 0 + 1
996   //   Val2 = Val + 3
997   //   VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
998   //   VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
999   //   VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
1000   //   VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
1001   //
1002   //   SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
1003   //   SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
1004   //   SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
1005   //   SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
1006   //
1007   //   SP1 = VectorSplat(2, i8 2)
1008   //   SP2 = VectorSplat(2, i8 %Val)
1009   Type *VecTy = FixedVectorType::get(IRB.getInt8Ty(), 2);
1010   Type *i32 = IRB.getInt32Ty();
1011   Type *i32VecTy = FixedVectorType::get(i32, 2);
1012 
1013   Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
1014   Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
1015 
1016   SmallVector<Constant *, 2> VecElemIdxs;
1017   VecElemIdxs.push_back(ConstantInt::get(i32, 0));
1018   VecElemIdxs.push_back(ConstantInt::get(i32, 2));
1019   auto *IdxVec = ConstantVector::get(VecElemIdxs);
1020 
1021   Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0);
1022   Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
1023   Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
1024   Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
1025 
1026   Value *EX1 = IRB.CreateExtractElement(VI4, Val);
1027   Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
1028   Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
1029 
1030   Constant *Zero = ConstantAggregateZero::get(i32VecTy);
1031   SmallVector<int, 16> ZeroMask;
1032   ShuffleVectorInst::getShuffleMask(Zero, ZeroMask);
1033 
1034   Value *SI1 = IRB.CreateShuffleVector(VI1, ZeroMask);
1035   Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
1036   Value *SI3 = IRB.CreateShuffleVector(VI3, ZeroMask);
1037   Value *SI4 = IRB.CreateShuffleVector(VI4, ZeroMask);
1038 
1039   Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
1040   Value *SP2 = IRB.CreateVectorSplat(2, Val);
1041 
1042   Value *A = nullptr, *B = nullptr, *C = nullptr;
1043 
1044   // Test matching insertelement
1045   EXPECT_TRUE(match(VI1, m_InsertElt(m_Value(), m_Value(), m_Value())));
1046   EXPECT_TRUE(
1047       match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt())));
1048   EXPECT_TRUE(
1049       match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero())));
1050   EXPECT_TRUE(
1051       match(VI1, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero())));
1052   EXPECT_TRUE(match(VI2, m_InsertElt(m_Value(), m_Value(), m_Value())));
1053   EXPECT_FALSE(
1054       match(VI2, m_InsertElt(m_Value(), m_Value(), m_ConstantInt())));
1055   EXPECT_FALSE(
1056       match(VI2, m_InsertElt(m_Value(), m_ConstantInt(), m_Value())));
1057   EXPECT_FALSE(match(VI2, m_InsertElt(m_Constant(), m_Value(), m_Value())));
1058   EXPECT_TRUE(match(VI3, m_InsertElt(m_Value(A), m_Value(B), m_Value(C))));
1059   EXPECT_TRUE(A == VI1);
1060   EXPECT_TRUE(B == Val2);
1061   EXPECT_TRUE(isa<ConstantInt>(C));
1062   A = B = C = nullptr; // reset
1063 
1064   // Test matching extractelement
1065   EXPECT_TRUE(match(EX1, m_ExtractElt(m_Value(A), m_Value(B))));
1066   EXPECT_TRUE(A == VI4);
1067   EXPECT_TRUE(B == Val);
1068   A = B = C = nullptr; // reset
1069   EXPECT_FALSE(match(EX1, m_ExtractElt(m_Value(), m_ConstantInt())));
1070   EXPECT_TRUE(match(EX2, m_ExtractElt(m_Value(), m_ConstantInt())));
1071   EXPECT_TRUE(match(EX3, m_ExtractElt(m_Constant(), m_ConstantInt())));
1072 
1073   // Test matching shufflevector
1074   ArrayRef<int> Mask;
1075   EXPECT_TRUE(match(SI1, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask())));
1076   EXPECT_TRUE(match(SI2, m_Shuffle(m_Value(A), m_Value(B), m_Mask(Mask))));
1077   EXPECT_TRUE(A == VI3);
1078   EXPECT_TRUE(B == VI4);
1079   A = B = C = nullptr; // reset
1080 
1081   // Test matching the vector splat pattern
1082   EXPECT_TRUE(match(
1083       SI1,
1084       m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()),
1085                 m_Undef(), m_ZeroMask())));
1086   EXPECT_FALSE(match(
1087       SI3, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1088                      m_Undef(), m_ZeroMask())));
1089   EXPECT_FALSE(match(
1090       SI4, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1091                      m_Undef(), m_ZeroMask())));
1092   EXPECT_TRUE(match(
1093       SP1,
1094       m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()),
1095                 m_Undef(), m_ZeroMask())));
1096   EXPECT_TRUE(match(
1097       SP2, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A), m_Zero()),
1098                      m_Undef(), m_ZeroMask())));
1099   EXPECT_TRUE(A == Val);
1100 }
1101 
1102 TEST_F(PatternMatchTest, UndefPoisonMix) {
1103   Type *ScalarTy = IRB.getInt8Ty();
1104   ArrayType *ArrTy = ArrayType::get(ScalarTy, 2);
1105   StructType *StTy = StructType::get(ScalarTy, ScalarTy);
1106   StructType *StTy2 = StructType::get(ScalarTy, StTy);
1107   StructType *StTy3 = StructType::get(StTy, ScalarTy);
1108   Constant *Zero = ConstantInt::getNullValue(ScalarTy);
1109   UndefValue *U = UndefValue::get(ScalarTy);
1110   UndefValue *P = PoisonValue::get(ScalarTy);
1111 
1112   EXPECT_TRUE(match(ConstantVector::get({U, P}), m_Undef()));
1113   EXPECT_TRUE(match(ConstantVector::get({P, U}), m_Undef()));
1114 
1115   EXPECT_TRUE(match(ConstantArray::get(ArrTy, {U, P}), m_Undef()));
1116   EXPECT_TRUE(match(ConstantArray::get(ArrTy, {P, U}), m_Undef()));
1117 
1118   auto *UP = ConstantStruct::get(StTy, {U, P});
1119   EXPECT_TRUE(match(ConstantStruct::get(StTy2, {U, UP}), m_Undef()));
1120   EXPECT_TRUE(match(ConstantStruct::get(StTy2, {P, UP}), m_Undef()));
1121   EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, U}), m_Undef()));
1122   EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, P}), m_Undef()));
1123 
1124   EXPECT_FALSE(match(ConstantStruct::get(StTy, {U, Zero}), m_Undef()));
1125   EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, U}), m_Undef()));
1126   EXPECT_FALSE(match(ConstantStruct::get(StTy, {P, Zero}), m_Undef()));
1127   EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, P}), m_Undef()));
1128 
1129   EXPECT_FALSE(match(ConstantStruct::get(StTy2, {Zero, UP}), m_Undef()));
1130   EXPECT_FALSE(match(ConstantStruct::get(StTy3, {UP, Zero}), m_Undef()));
1131 }
1132 
1133 TEST_F(PatternMatchTest, VectorUndefInt) {
1134   Type *ScalarTy = IRB.getInt8Ty();
1135   Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1136   Constant *ScalarUndef = UndefValue::get(ScalarTy);
1137   Constant *VectorUndef = UndefValue::get(VectorTy);
1138   Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1139   Constant *VectorZero = Constant::getNullValue(VectorTy);
1140 
1141   SmallVector<Constant *, 4> Elems;
1142   Elems.push_back(ScalarUndef);
1143   Elems.push_back(ScalarZero);
1144   Elems.push_back(ScalarUndef);
1145   Elems.push_back(ScalarZero);
1146   Constant *VectorZeroUndef = ConstantVector::get(Elems);
1147 
1148   EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1149   EXPECT_TRUE(match(VectorUndef, m_Undef()));
1150   EXPECT_FALSE(match(ScalarZero, m_Undef()));
1151   EXPECT_FALSE(match(VectorZero, m_Undef()));
1152   EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1153 
1154   EXPECT_FALSE(match(ScalarUndef, m_Zero()));
1155   EXPECT_FALSE(match(VectorUndef, m_Zero()));
1156   EXPECT_TRUE(match(ScalarZero, m_Zero()));
1157   EXPECT_TRUE(match(VectorZero, m_Zero()));
1158   EXPECT_TRUE(match(VectorZeroUndef, m_Zero()));
1159 
1160   const APInt *C;
1161   // Regardless of whether undefs are allowed,
1162   // a fully undef constant does not match.
1163   EXPECT_FALSE(match(ScalarUndef, m_APInt(C)));
1164   EXPECT_FALSE(match(ScalarUndef, m_APIntForbidUndef(C)));
1165   EXPECT_FALSE(match(ScalarUndef, m_APIntAllowUndef(C)));
1166   EXPECT_FALSE(match(VectorUndef, m_APInt(C)));
1167   EXPECT_FALSE(match(VectorUndef, m_APIntForbidUndef(C)));
1168   EXPECT_FALSE(match(VectorUndef, m_APIntAllowUndef(C)));
1169 
1170   // We can always match simple constants and simple splats.
1171   C = nullptr;
1172   EXPECT_TRUE(match(ScalarZero, m_APInt(C)));
1173   EXPECT_TRUE(C->isZero());
1174   C = nullptr;
1175   EXPECT_TRUE(match(ScalarZero, m_APIntForbidUndef(C)));
1176   EXPECT_TRUE(C->isZero());
1177   C = nullptr;
1178   EXPECT_TRUE(match(ScalarZero, m_APIntAllowUndef(C)));
1179   EXPECT_TRUE(C->isZero());
1180   C = nullptr;
1181   EXPECT_TRUE(match(VectorZero, m_APInt(C)));
1182   EXPECT_TRUE(C->isZero());
1183   C = nullptr;
1184   EXPECT_TRUE(match(VectorZero, m_APIntForbidUndef(C)));
1185   EXPECT_TRUE(C->isZero());
1186   C = nullptr;
1187   EXPECT_TRUE(match(VectorZero, m_APIntAllowUndef(C)));
1188   EXPECT_TRUE(C->isZero());
1189 
1190   // Whether splats with undef can be matched depends on the matcher.
1191   EXPECT_FALSE(match(VectorZeroUndef, m_APInt(C)));
1192   EXPECT_FALSE(match(VectorZeroUndef, m_APIntForbidUndef(C)));
1193   C = nullptr;
1194   EXPECT_TRUE(match(VectorZeroUndef, m_APIntAllowUndef(C)));
1195   EXPECT_TRUE(C->isZero());
1196 }
1197 
1198 TEST_F(PatternMatchTest, VectorUndefFloat) {
1199   Type *ScalarTy = IRB.getFloatTy();
1200   Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1201   Constant *ScalarUndef = UndefValue::get(ScalarTy);
1202   Constant *VectorUndef = UndefValue::get(VectorTy);
1203   Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1204   Constant *VectorZero = Constant::getNullValue(VectorTy);
1205   Constant *ScalarPosInf = ConstantFP::getInfinity(ScalarTy, false);
1206   Constant *ScalarNegInf = ConstantFP::getInfinity(ScalarTy, true);
1207   Constant *ScalarNaN = ConstantFP::getNaN(ScalarTy, true);
1208 
1209   Constant *VectorZeroUndef =
1210       ConstantVector::get({ScalarUndef, ScalarZero, ScalarUndef, ScalarZero});
1211 
1212   Constant *VectorInfUndef = ConstantVector::get(
1213       {ScalarPosInf, ScalarNegInf, ScalarUndef, ScalarPosInf});
1214 
1215   Constant *VectorNaNUndef =
1216       ConstantVector::get({ScalarUndef, ScalarNaN, ScalarNaN, ScalarNaN});
1217 
1218   EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1219   EXPECT_TRUE(match(VectorUndef, m_Undef()));
1220   EXPECT_FALSE(match(ScalarZero, m_Undef()));
1221   EXPECT_FALSE(match(VectorZero, m_Undef()));
1222   EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1223   EXPECT_FALSE(match(VectorInfUndef, m_Undef()));
1224   EXPECT_FALSE(match(VectorNaNUndef, m_Undef()));
1225 
1226   EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1227   EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1228   EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1229   EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1230   EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
1231   EXPECT_FALSE(match(VectorInfUndef, m_AnyZeroFP()));
1232   EXPECT_FALSE(match(VectorNaNUndef, m_AnyZeroFP()));
1233 
1234   EXPECT_FALSE(match(ScalarUndef, m_NaN()));
1235   EXPECT_FALSE(match(VectorUndef, m_NaN()));
1236   EXPECT_FALSE(match(VectorZeroUndef, m_NaN()));
1237   EXPECT_FALSE(match(ScalarPosInf, m_NaN()));
1238   EXPECT_FALSE(match(ScalarNegInf, m_NaN()));
1239   EXPECT_TRUE(match(ScalarNaN, m_NaN()));
1240   EXPECT_FALSE(match(VectorInfUndef, m_NaN()));
1241   EXPECT_TRUE(match(VectorNaNUndef, m_NaN()));
1242 
1243   EXPECT_FALSE(match(ScalarUndef, m_NonNaN()));
1244   EXPECT_FALSE(match(VectorUndef, m_NonNaN()));
1245   EXPECT_TRUE(match(VectorZeroUndef, m_NonNaN()));
1246   EXPECT_TRUE(match(ScalarPosInf, m_NonNaN()));
1247   EXPECT_TRUE(match(ScalarNegInf, m_NonNaN()));
1248   EXPECT_FALSE(match(ScalarNaN, m_NonNaN()));
1249   EXPECT_TRUE(match(VectorInfUndef, m_NonNaN()));
1250   EXPECT_FALSE(match(VectorNaNUndef, m_NonNaN()));
1251 
1252   EXPECT_FALSE(match(ScalarUndef, m_Inf()));
1253   EXPECT_FALSE(match(VectorUndef, m_Inf()));
1254   EXPECT_FALSE(match(VectorZeroUndef, m_Inf()));
1255   EXPECT_TRUE(match(ScalarPosInf, m_Inf()));
1256   EXPECT_TRUE(match(ScalarNegInf, m_Inf()));
1257   EXPECT_FALSE(match(ScalarNaN, m_Inf()));
1258   EXPECT_TRUE(match(VectorInfUndef, m_Inf()));
1259   EXPECT_FALSE(match(VectorNaNUndef, m_Inf()));
1260 
1261   EXPECT_FALSE(match(ScalarUndef, m_NonInf()));
1262   EXPECT_FALSE(match(VectorUndef, m_NonInf()));
1263   EXPECT_TRUE(match(VectorZeroUndef, m_NonInf()));
1264   EXPECT_FALSE(match(ScalarPosInf, m_NonInf()));
1265   EXPECT_FALSE(match(ScalarNegInf, m_NonInf()));
1266   EXPECT_TRUE(match(ScalarNaN, m_NonInf()));
1267   EXPECT_FALSE(match(VectorInfUndef, m_NonInf()));
1268   EXPECT_TRUE(match(VectorNaNUndef, m_NonInf()));
1269 
1270   EXPECT_FALSE(match(ScalarUndef, m_Finite()));
1271   EXPECT_FALSE(match(VectorUndef, m_Finite()));
1272   EXPECT_TRUE(match(VectorZeroUndef, m_Finite()));
1273   EXPECT_FALSE(match(ScalarPosInf, m_Finite()));
1274   EXPECT_FALSE(match(ScalarNegInf, m_Finite()));
1275   EXPECT_FALSE(match(ScalarNaN, m_Finite()));
1276   EXPECT_FALSE(match(VectorInfUndef, m_Finite()));
1277   EXPECT_FALSE(match(VectorNaNUndef, m_Finite()));
1278 
1279   const APFloat *C;
1280   // Regardless of whether undefs are allowed,
1281   // a fully undef constant does not match.
1282   EXPECT_FALSE(match(ScalarUndef, m_APFloat(C)));
1283   EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidUndef(C)));
1284   EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowUndef(C)));
1285   EXPECT_FALSE(match(VectorUndef, m_APFloat(C)));
1286   EXPECT_FALSE(match(VectorUndef, m_APFloatForbidUndef(C)));
1287   EXPECT_FALSE(match(VectorUndef, m_APFloatAllowUndef(C)));
1288 
1289   // We can always match simple constants and simple splats.
1290   C = nullptr;
1291   EXPECT_TRUE(match(ScalarZero, m_APFloat(C)));
1292   EXPECT_TRUE(C->isZero());
1293   C = nullptr;
1294   EXPECT_TRUE(match(ScalarZero, m_APFloatForbidUndef(C)));
1295   EXPECT_TRUE(C->isZero());
1296   C = nullptr;
1297   EXPECT_TRUE(match(ScalarZero, m_APFloatAllowUndef(C)));
1298   EXPECT_TRUE(C->isZero());
1299   C = nullptr;
1300   EXPECT_TRUE(match(VectorZero, m_APFloat(C)));
1301   EXPECT_TRUE(C->isZero());
1302   C = nullptr;
1303   EXPECT_TRUE(match(VectorZero, m_APFloatForbidUndef(C)));
1304   EXPECT_TRUE(C->isZero());
1305   C = nullptr;
1306   EXPECT_TRUE(match(VectorZero, m_APFloatAllowUndef(C)));
1307   EXPECT_TRUE(C->isZero());
1308 
1309   // Whether splats with undef can be matched depends on the matcher.
1310   EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C)));
1311   EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidUndef(C)));
1312   C = nullptr;
1313   EXPECT_TRUE(match(VectorZeroUndef, m_APFloatAllowUndef(C)));
1314   EXPECT_TRUE(C->isZero());
1315   C = nullptr;
1316   EXPECT_TRUE(match(VectorZeroUndef, m_Finite(C)));
1317   EXPECT_TRUE(C->isZero());
1318 }
1319 
1320 TEST_F(PatternMatchTest, FloatingPointFNeg) {
1321   Type *FltTy = IRB.getFloatTy();
1322   Value *One = ConstantFP::get(FltTy, 1.0);
1323   Value *Z = ConstantFP::get(FltTy, 0.0);
1324   Value *NZ = ConstantFP::get(FltTy, -0.0);
1325   Value *V = IRB.CreateFNeg(One);
1326   Value *V1 = IRB.CreateFSub(NZ, One);
1327   Value *V2 = IRB.CreateFSub(Z, One);
1328   Value *V3 = IRB.CreateFAdd(NZ, One);
1329   Value *Match;
1330 
1331   // Test FNeg(1.0)
1332   EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1333   EXPECT_EQ(One, Match);
1334 
1335   // Test FSub(-0.0, 1.0)
1336   EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1337   EXPECT_EQ(One, Match);
1338 
1339   // Test FSub(0.0, 1.0)
1340   EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1341   cast<Instruction>(V2)->setHasNoSignedZeros(true);
1342   EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1343   EXPECT_EQ(One, Match);
1344 
1345   // Test FAdd(-0.0, 1.0)
1346   EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1347 }
1348 
1349 TEST_F(PatternMatchTest, CondBranchTest) {
1350   BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1351   BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1352   Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1353 
1354   EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1355 
1356   BasicBlock *A, *B;
1357   EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1358   EXPECT_EQ(TrueBB, A);
1359   EXPECT_EQ(FalseBB, B);
1360 
1361   EXPECT_FALSE(
1362       match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1363   EXPECT_FALSE(
1364       match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1365   EXPECT_FALSE(
1366       match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1367   EXPECT_TRUE(
1368       match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1369 
1370   // Check we can use m_Deferred with branches.
1371   EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1372   Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1373   A = nullptr;
1374   EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1375 }
1376 
1377 TEST_F(PatternMatchTest, WithOverflowInst) {
1378   Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1379                                          IRB.getInt32(0), IRB.getInt32(0));
1380   Value *Add0 = IRB.CreateExtractValue(Add, 0);
1381   Value *Add1 = IRB.CreateExtractValue(Add, 1);
1382 
1383   EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1384   EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1385   EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1386   EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1387   EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1388   EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1389 
1390   WithOverflowInst *WOI;
1391   EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1392   EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1393   EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1394 
1395   EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1396   EXPECT_EQ(Add, WOI);
1397   EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1398   EXPECT_EQ(Add, WOI);
1399 }
1400 
1401 TEST_F(PatternMatchTest, MinMaxIntrinsics) {
1402   Type *Ty = IRB.getInt32Ty();
1403   Value *L = ConstantInt::get(Ty, 1);
1404   Value *R = ConstantInt::get(Ty, 2);
1405   Value *MatchL, *MatchR;
1406 
1407   // Check for intrinsic ID match and capture of operands.
1408   EXPECT_TRUE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1409                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1410   EXPECT_EQ(L, MatchL);
1411   EXPECT_EQ(R, MatchR);
1412 
1413   EXPECT_TRUE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1414                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1415   EXPECT_EQ(L, MatchL);
1416   EXPECT_EQ(R, MatchR);
1417 
1418   EXPECT_TRUE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1419                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1420   EXPECT_EQ(L, MatchL);
1421   EXPECT_EQ(R, MatchR);
1422 
1423   EXPECT_TRUE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1424                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1425   EXPECT_EQ(L, MatchL);
1426   EXPECT_EQ(R, MatchR);
1427 
1428   // Check for intrinsic ID mismatch.
1429   EXPECT_FALSE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1430                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1431   EXPECT_FALSE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1432                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1433   EXPECT_FALSE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1434                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1435   EXPECT_FALSE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1436                   .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1437 }
1438 
1439 TEST_F(PatternMatchTest, IntrinsicMatcher) {
1440   Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1441   Value *Hash = IRB.getInt64(0);
1442   Value *Num = IRB.getInt32(1);
1443   Value *Index = IRB.getInt32(2);
1444   Value *Step = IRB.getInt64(3);
1445 
1446   Value *Ops[] = {Name, Hash, Num, Index, Step};
1447   Module *M = BB->getParent()->getParent();
1448   Function *TheFn =
1449       Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1450 
1451   Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1452 
1453   // Match without capturing.
1454   EXPECT_TRUE(match(
1455       Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1456                       m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1457   EXPECT_FALSE(match(
1458       Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1459                       m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1460 
1461   // Match with capturing.
1462   Value *Arg1 = nullptr;
1463   Value *Arg2 = nullptr;
1464   Value *Arg3 = nullptr;
1465   Value *Arg4 = nullptr;
1466   Value *Arg5 = nullptr;
1467   EXPECT_TRUE(
1468       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1469                             m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1470                             m_Value(Arg4), m_Value(Arg5))));
1471   EXPECT_EQ(Arg1, Name);
1472   EXPECT_EQ(Arg2, Hash);
1473   EXPECT_EQ(Arg3, Num);
1474   EXPECT_EQ(Arg4, Index);
1475   EXPECT_EQ(Arg5, Step);
1476 
1477   // Match specific second argument.
1478   EXPECT_TRUE(
1479       match(Intrinsic5,
1480             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1481                 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1482   EXPECT_FALSE(
1483       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1484                             m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1485                             m_Value())));
1486 
1487   // Match specific third argument.
1488   EXPECT_TRUE(
1489       match(Intrinsic5,
1490             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1491                 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1492   EXPECT_FALSE(
1493       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1494                             m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1495                             m_Value())));
1496 
1497   // Match specific fourth argument.
1498   EXPECT_TRUE(
1499       match(Intrinsic5,
1500             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1501                 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1502   EXPECT_FALSE(
1503       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1504                             m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1505                             m_Value())));
1506 
1507   // Match specific fifth argument.
1508   EXPECT_TRUE(
1509       match(Intrinsic5,
1510             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1511                 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1512   EXPECT_FALSE(
1513       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1514                             m_Value(), m_Value(), m_Value(), m_Value(),
1515                             m_SpecificInt(10))));
1516 }
1517 
1518 namespace {
1519 
1520 struct is_unsigned_zero_pred {
1521   bool isValue(const APInt &C) { return C.isZero(); }
1522 };
1523 
1524 struct is_float_zero_pred {
1525   bool isValue(const APFloat &C) { return C.isZero(); }
1526 };
1527 
1528 template <typename T> struct always_true_pred {
1529   bool isValue(const T &) { return true; }
1530 };
1531 
1532 template <typename T> struct always_false_pred {
1533   bool isValue(const T &) { return false; }
1534 };
1535 
1536 struct is_unsigned_max_pred {
1537   bool isValue(const APInt &C) { return C.isMaxValue(); }
1538 };
1539 
1540 struct is_float_nan_pred {
1541   bool isValue(const APFloat &C) { return C.isNaN(); }
1542 };
1543 
1544 } // namespace
1545 
1546 TEST_F(PatternMatchTest, ConstantPredicateType) {
1547 
1548   // Scalar integer
1549   APInt U32Max = APInt::getAllOnes(32);
1550   APInt U32Zero = APInt::getZero(32);
1551   APInt U32DeadBeef(32, 0xDEADBEEF);
1552 
1553   Type *U32Ty = Type::getInt32Ty(Ctx);
1554 
1555   Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max);
1556   Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero);
1557   Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef);
1558 
1559   EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1560   EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1561   EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1562   EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1563 
1564   EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1565   EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1566   EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1567   EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1568 
1569   EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1570   EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1571   EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1572   EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1573 
1574   // Scalar float
1575   APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle());
1576   APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle());
1577   APFloat F32Pi(3.14f);
1578 
1579   Type *F32Ty = Type::getFloatTy(Ctx);
1580 
1581   Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN);
1582   Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero);
1583   Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi);
1584 
1585   EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1586   EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1587   EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1588   EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1589 
1590   EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1591   EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1592   EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1593   EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1594 
1595   EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1596   EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1597   EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1598   EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1599 
1600   auto FixedEC = ElementCount::getFixed(4);
1601   auto ScalableEC = ElementCount::getScalable(4);
1602 
1603   // Vector splat
1604 
1605   for (auto EC : {FixedEC, ScalableEC}) {
1606     // integer
1607 
1608     Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max);
1609     Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero);
1610     Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef);
1611 
1612     EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1613     EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1614     EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1615     EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1616 
1617     EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1618     EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1619     EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1620     EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1621 
1622     EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1623     EXPECT_FALSE(
1624         match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1625     EXPECT_TRUE(
1626         match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1627     EXPECT_FALSE(
1628         match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1629 
1630     // float
1631 
1632     Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN);
1633     Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero);
1634     Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi);
1635 
1636     EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1637     EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1638     EXPECT_TRUE(
1639         match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1640     EXPECT_FALSE(
1641         match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1642 
1643     EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1644     EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1645     EXPECT_TRUE(
1646         match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1647     EXPECT_FALSE(
1648         match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1649 
1650     EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1651     EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1652     EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1653     EXPECT_FALSE(
1654         match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1655   }
1656 
1657   // Int arbitrary vector
1658 
1659   Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef});
1660   Constant *CU32Undef = UndefValue::get(U32Ty);
1661   Constant *CU32MaxWithUndef =
1662       ConstantVector::get({CU32Undef, CU32Max, CU32Undef});
1663 
1664   EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>()));
1665   EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>()));
1666   EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>()));
1667   EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>()));
1668 
1669   EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>()));
1670   EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>()));
1671   EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>()));
1672   EXPECT_FALSE(
1673       match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>()));
1674 
1675   // Float arbitrary vector
1676 
1677   Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi});
1678   Constant *CF32Undef = UndefValue::get(F32Ty);
1679   Constant *CF32NaNWithUndef =
1680       ConstantVector::get({CF32Undef, CF32NaN, CF32Undef});
1681 
1682   EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>()));
1683   EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>()));
1684   EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>()));
1685   EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>()));
1686 
1687   EXPECT_TRUE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>()));
1688   EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>()));
1689   EXPECT_TRUE(
1690       match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>()));
1691   EXPECT_FALSE(
1692       match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
1693 }
1694 
1695 TEST_F(PatternMatchTest, InsertValue) {
1696   Type *StructTy = StructType::create(IRB.getContext(),
1697                                       {IRB.getInt32Ty(), IRB.getInt64Ty()});
1698   Value *Ins0 =
1699       IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0);
1700   Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1);
1701 
1702   EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value())));
1703   EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value())));
1704   EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value())));
1705   EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value())));
1706 
1707   EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20))));
1708   EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0))));
1709 
1710   EXPECT_TRUE(
1711       match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()),
1712                                    m_SpecificInt(90))));
1713   EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
1714 }
1715 
1716 TEST_F(PatternMatchTest, LogicalSelects) {
1717   Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
1718   Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
1719   Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
1720   Constant *T = IRB.getInt1(true);
1721   Constant *F = IRB.getInt1(false);
1722   Value *And = IRB.CreateSelect(X, Y, F);
1723   Value *Or = IRB.CreateSelect(X, T, Y);
1724 
1725   // Logical and:
1726   // Check basic no-capture logic - opcode and constant must match.
1727   EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Value())));
1728   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Value())));
1729   EXPECT_FALSE(match(And, m_LogicalOr(m_Value(), m_Value())));
1730   EXPECT_FALSE(match(And, m_c_LogicalOr(m_Value(), m_Value())));
1731 
1732   // Check with captures.
1733   EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Value())));
1734   EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Specific(Y))));
1735   EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(Y))));
1736 
1737   EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Value())));
1738   EXPECT_FALSE(match(And, m_LogicalAnd(m_Value(), m_Specific(X))));
1739   EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(X))));
1740 
1741   EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(X))));
1742   EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(Y))));
1743 
1744   // Check captures for commutative match.
1745   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Value())));
1746   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(Y))));
1747   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(Y))));
1748 
1749   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Value())));
1750   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(X))));
1751   EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(X))));
1752 
1753   EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(X))));
1754   EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(Y))));
1755 
1756   // Logical or:
1757   // Check basic no-capture logic - opcode and constant must match.
1758   EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Value())));
1759   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Value())));
1760   EXPECT_FALSE(match(Or, m_LogicalAnd(m_Value(), m_Value())));
1761   EXPECT_FALSE(match(Or, m_c_LogicalAnd(m_Value(), m_Value())));
1762 
1763   // Check with captures.
1764   EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Value())));
1765   EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Specific(Y))));
1766   EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(Y))));
1767 
1768   EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Value())));
1769   EXPECT_FALSE(match(Or, m_LogicalOr(m_Value(), m_Specific(X))));
1770   EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(X))));
1771 
1772   EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(X))));
1773   EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(Y))));
1774 
1775   // Check captures for commutative match.
1776   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Value())));
1777   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(Y))));
1778   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(Y))));
1779 
1780   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Value())));
1781   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(X))));
1782   EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(X))));
1783 
1784   EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(X))));
1785   EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(Y))));
1786 }
1787 
1788 TEST_F(PatternMatchTest, VectorLogicalSelects) {
1789   Type *i1 = IRB.getInt1Ty();
1790   Type *v3i1 = FixedVectorType::get(i1, 3);
1791 
1792   Value *Alloca = IRB.CreateAlloca(i1);
1793   Value *AllocaVec = IRB.CreateAlloca(v3i1);
1794   Value *Scalar = IRB.CreateLoad(i1, Alloca);
1795   Value *Vector = IRB.CreateLoad(v3i1, AllocaVec);
1796   Constant *F = Constant::getNullValue(v3i1);
1797   Constant *T = Constant::getAllOnesValue(v3i1);
1798 
1799   // select <3 x i1> Vector, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
1800   Value *VecAnd = IRB.CreateSelect(Vector, Vector, F);
1801 
1802   // select i1 Scalar, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
1803   Value *MixedTypeAnd = IRB.CreateSelect(Scalar, Vector, F);
1804 
1805   // select <3 x i1> Vector, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
1806   Value *VecOr = IRB.CreateSelect(Vector, T, Vector);
1807 
1808   // select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
1809   Value *MixedTypeOr = IRB.CreateSelect(Scalar, T, Vector);
1810 
1811   // We allow matching a real vector logical select,
1812   // but not a scalar select of vector bools.
1813   EXPECT_TRUE(match(VecAnd, m_LogicalAnd(m_Value(), m_Value())));
1814   EXPECT_FALSE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value())));
1815   EXPECT_TRUE(match(VecOr, m_LogicalOr(m_Value(), m_Value())));
1816   EXPECT_FALSE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value())));
1817 }
1818 
1819 TEST_F(PatternMatchTest, VScale) {
1820   DataLayout DL = M->getDataLayout();
1821 
1822   Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1);
1823   Value *NullPtrVec =
1824       Constant::getNullValue(PointerType::getUnqual(VecTy->getContext()));
1825   Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1));
1826   Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType()));
1827   EXPECT_TRUE(match(PtrToInt, m_VScale()));
1828 
1829   Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2);
1830   Value *NullPtrVec2 =
1831       Constant::getNullValue(PointerType::getUnqual(VecTy2->getContext()));
1832   Value *GEP2 = IRB.CreateGEP(VecTy, NullPtrVec2, IRB.getInt64(1));
1833   Value *PtrToInt2 =
1834       IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType()));
1835   EXPECT_TRUE(match(PtrToInt2, m_VScale()));
1836 }
1837 
1838 TEST_F(PatternMatchTest, NotForbidUndef) {
1839   Type *ScalarTy = IRB.getInt8Ty();
1840   Type *VectorTy = FixedVectorType::get(ScalarTy, 3);
1841   Constant *ScalarUndef = UndefValue::get(ScalarTy);
1842   Constant *ScalarOnes = Constant::getAllOnesValue(ScalarTy);
1843   Constant *VectorZero = Constant::getNullValue(VectorTy);
1844   Constant *VectorOnes = Constant::getAllOnesValue(VectorTy);
1845 
1846   SmallVector<Constant *, 3> MixedElems;
1847   MixedElems.push_back(ScalarOnes);
1848   MixedElems.push_back(ScalarOnes);
1849   MixedElems.push_back(ScalarUndef);
1850   Constant *VectorMixed = ConstantVector::get(MixedElems);
1851 
1852   Value *Not = IRB.CreateXor(VectorZero, VectorOnes);
1853   Value *X;
1854   EXPECT_TRUE(match(Not, m_Not(m_Value())));
1855   EXPECT_TRUE(match(Not, m_NotForbidUndef(m_Value(X))));
1856   EXPECT_TRUE(match(X, m_Zero()));
1857 
1858   Value *NotCommute = IRB.CreateXor(VectorOnes, VectorZero);
1859   Value *Y;
1860   EXPECT_TRUE(match(NotCommute, m_Not(m_Value())));
1861   EXPECT_TRUE(match(NotCommute, m_NotForbidUndef(m_Value(Y))));
1862   EXPECT_TRUE(match(Y, m_Zero()));
1863 
1864   Value *NotWithUndefs = IRB.CreateXor(VectorZero, VectorMixed);
1865   EXPECT_TRUE(match(NotWithUndefs, m_Not(m_Value())));
1866   EXPECT_FALSE(match(NotWithUndefs, m_NotForbidUndef(m_Value())));
1867 
1868   Value *NotWithUndefsCommute = IRB.CreateXor(VectorMixed, VectorZero);
1869   EXPECT_TRUE(match(NotWithUndefsCommute, m_Not(m_Value())));
1870   EXPECT_FALSE(match(NotWithUndefsCommute, m_NotForbidUndef(m_Value(X))));
1871 }
1872 
1873 template <typename T> struct MutableConstTest : PatternMatchTest { };
1874 
1875 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1876                          std::tuple<const Value*, const Instruction *>>
1877     MutableConstTestTypes;
1878 TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, );
1879 
1880 TYPED_TEST(MutableConstTest, ICmp) {
1881   auto &IRB = PatternMatchTest::IRB;
1882 
1883   typedef std::tuple_element_t<0, TypeParam> ValueType;
1884   typedef std::tuple_element_t<1, TypeParam> InstructionType;
1885 
1886   Value *L = IRB.getInt32(1);
1887   Value *R = IRB.getInt32(2);
1888   ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1889 
1890   ValueType MatchL;
1891   ValueType MatchR;
1892   ICmpInst::Predicate MatchPred;
1893 
1894   EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1895               .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1896   EXPECT_EQ(L, MatchL);
1897   EXPECT_EQ(R, MatchR);
1898 }
1899 
1900 TEST_F(PatternMatchTest, ConstExpr) {
1901   Constant *G =
1902       M->getOrInsertGlobal("dummy", PointerType::getUnqual(IRB.getInt32Ty()));
1903   Constant *S = ConstantExpr::getPtrToInt(G, IRB.getInt32Ty());
1904   Type *VecTy = FixedVectorType::get(IRB.getInt32Ty(), 2);
1905   PoisonValue *P = PoisonValue::get(VecTy);
1906   Constant *V = ConstantExpr::getInsertElement(P, S, IRB.getInt32(0));
1907 
1908   // The match succeeds on a constant that is a constant expression itself
1909   // or a constant that contains a constant expression.
1910   EXPECT_TRUE(match(S, m_ConstantExpr()));
1911   EXPECT_TRUE(match(V, m_ConstantExpr()));
1912 }
1913 
1914 TEST_F(PatternMatchTest, PtrAdd) {
1915   Type *PtrTy = PointerType::getUnqual(Ctx);
1916   Type *IdxTy = Type::getInt64Ty(Ctx);
1917   Constant *Null = Constant::getNullValue(PtrTy);
1918   Constant *Offset = ConstantInt::get(IdxTy, 42);
1919   Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
1920   Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
1921   Value *PtrAddConst =
1922       ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
1923 
1924   Value *A, *B;
1925   EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
1926   EXPECT_EQ(A, Null);
1927   EXPECT_EQ(B, Offset);
1928 
1929   EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
1930   EXPECT_EQ(A, Null);
1931   EXPECT_EQ(B, Offset);
1932 
1933   EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
1934 }
1935 
1936 } // anonymous namespace.
1937