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