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__anon1a0850ca0111::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 = VectorType::get(IRB.getInt8Ty(), 2);
932   Type *i32 = IRB.getInt32Ty();
933   Type *i32VecTy = VectorType::get(i32, 2);
934 
935   Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
936   Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
937 
938   SmallVector<Constant *, 2> VecElemIdxs;
939   VecElemIdxs.push_back(ConstantInt::get(i32, 0));
940   VecElemIdxs.push_back(ConstantInt::get(i32, 2));
941   auto *IdxVec = ConstantVector::get(VecElemIdxs);
942 
943   Value *UndefVec = UndefValue::get(VecTy);
944   Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
945   Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
946   Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
947   Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
948 
949   Value *EX1 = IRB.CreateExtractElement(VI4, Val);
950   Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
951   Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
952 
953   Value *Zero = ConstantAggregateZero::get(i32VecTy);
954   Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero);
955   Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
956   Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero);
957   Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero);
958 
959   Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
960   Value *SP2 = IRB.CreateVectorSplat(2, Val);
961 
962   Value *A = nullptr, *B = nullptr, *C = nullptr;
963 
964   // Test matching insertelement
965   EXPECT_TRUE(match(VI1, m_InsertElement(m_Value(), m_Value(), m_Value())));
966   EXPECT_TRUE(
967       match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_ConstantInt())));
968   EXPECT_TRUE(
969       match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_Zero())));
970   EXPECT_TRUE(
971       match(VI1, m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero())));
972   EXPECT_TRUE(match(VI2, m_InsertElement(m_Value(), m_Value(), m_Value())));
973   EXPECT_FALSE(
974       match(VI2, m_InsertElement(m_Value(), m_Value(), m_ConstantInt())));
975   EXPECT_FALSE(
976       match(VI2, m_InsertElement(m_Value(), m_ConstantInt(), m_Value())));
977   EXPECT_FALSE(match(VI2, m_InsertElement(m_Constant(), m_Value(), m_Value())));
978   EXPECT_TRUE(match(VI3, m_InsertElement(m_Value(A), m_Value(B), m_Value(C))));
979   EXPECT_TRUE(A == VI1);
980   EXPECT_TRUE(B == Val2);
981   EXPECT_TRUE(isa<ConstantInt>(C));
982   A = B = C = nullptr; // reset
983 
984   // Test matching extractelement
985   EXPECT_TRUE(match(EX1, m_ExtractElement(m_Value(A), m_Value(B))));
986   EXPECT_TRUE(A == VI4);
987   EXPECT_TRUE(B == Val);
988   A = B = C = nullptr; // reset
989   EXPECT_FALSE(match(EX1, m_ExtractElement(m_Value(), m_ConstantInt())));
990   EXPECT_TRUE(match(EX2, m_ExtractElement(m_Value(), m_ConstantInt())));
991   EXPECT_TRUE(match(EX3, m_ExtractElement(m_Constant(), m_ConstantInt())));
992 
993   // Test matching shufflevector
994   EXPECT_TRUE(match(SI1, m_ShuffleVector(m_Value(), m_Undef(), m_Zero())));
995   EXPECT_TRUE(match(SI2, m_ShuffleVector(m_Value(A), m_Value(B), m_Value(C))));
996   EXPECT_TRUE(A == VI3);
997   EXPECT_TRUE(B == VI4);
998   EXPECT_TRUE(C == IdxVec);
999   A = B = C = nullptr; // reset
1000 
1001   // Test matching the vector splat pattern
1002   EXPECT_TRUE(match(
1003       SI1,
1004       m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()),
1005                       m_Undef(), m_Zero())));
1006   EXPECT_FALSE(match(
1007       SI3, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
1008                            m_Undef(), m_Zero())));
1009   EXPECT_FALSE(match(
1010       SI4, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
1011                            m_Undef(), m_Zero())));
1012   EXPECT_TRUE(match(
1013       SP1,
1014       m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(2), m_Zero()),
1015                       m_Undef(), m_Zero())));
1016   EXPECT_TRUE(match(
1017       SP2, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(A), m_Zero()),
1018                            m_Undef(), m_Zero())));
1019   EXPECT_TRUE(A == Val);
1020 }
1021 
TEST_F(PatternMatchTest,VectorUndefInt)1022 TEST_F(PatternMatchTest, VectorUndefInt) {
1023   Type *ScalarTy = IRB.getInt8Ty();
1024   Type *VectorTy = VectorType::get(ScalarTy, 4);
1025   Constant *ScalarUndef = UndefValue::get(ScalarTy);
1026   Constant *VectorUndef = UndefValue::get(VectorTy);
1027   Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1028   Constant *VectorZero = Constant::getNullValue(VectorTy);
1029 
1030   SmallVector<Constant *, 4> Elems;
1031   Elems.push_back(ScalarUndef);
1032   Elems.push_back(ScalarZero);
1033   Elems.push_back(ScalarUndef);
1034   Elems.push_back(ScalarZero);
1035   Constant *VectorZeroUndef = ConstantVector::get(Elems);
1036 
1037   EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1038   EXPECT_TRUE(match(VectorUndef, m_Undef()));
1039   EXPECT_FALSE(match(ScalarZero, m_Undef()));
1040   EXPECT_FALSE(match(VectorZero, m_Undef()));
1041   EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1042 
1043   EXPECT_FALSE(match(ScalarUndef, m_Zero()));
1044   EXPECT_FALSE(match(VectorUndef, m_Zero()));
1045   EXPECT_TRUE(match(ScalarZero, m_Zero()));
1046   EXPECT_TRUE(match(VectorZero, m_Zero()));
1047   EXPECT_TRUE(match(VectorZeroUndef, m_Zero()));
1048 }
1049 
TEST_F(PatternMatchTest,VectorUndefFloat)1050 TEST_F(PatternMatchTest, VectorUndefFloat) {
1051   Type *ScalarTy = IRB.getFloatTy();
1052   Type *VectorTy = VectorType::get(ScalarTy, 4);
1053   Constant *ScalarUndef = UndefValue::get(ScalarTy);
1054   Constant *VectorUndef = UndefValue::get(VectorTy);
1055   Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1056   Constant *VectorZero = Constant::getNullValue(VectorTy);
1057 
1058   SmallVector<Constant *, 4> Elems;
1059   Elems.push_back(ScalarUndef);
1060   Elems.push_back(ScalarZero);
1061   Elems.push_back(ScalarUndef);
1062   Elems.push_back(ScalarZero);
1063   Constant *VectorZeroUndef = ConstantVector::get(Elems);
1064 
1065   EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1066   EXPECT_TRUE(match(VectorUndef, m_Undef()));
1067   EXPECT_FALSE(match(ScalarZero, m_Undef()));
1068   EXPECT_FALSE(match(VectorZero, m_Undef()));
1069   EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1070 
1071   EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1072   EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1073   EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1074   EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1075   EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
1076 }
1077 
TEST_F(PatternMatchTest,FloatingPointFNeg)1078 TEST_F(PatternMatchTest, FloatingPointFNeg) {
1079   Type *FltTy = IRB.getFloatTy();
1080   Value *One = ConstantFP::get(FltTy, 1.0);
1081   Value *Z = ConstantFP::get(FltTy, 0.0);
1082   Value *NZ = ConstantFP::get(FltTy, -0.0);
1083   Value *V = IRB.CreateFNeg(One);
1084   Value *V1 = IRB.CreateFSub(NZ, One);
1085   Value *V2 = IRB.CreateFSub(Z, One);
1086   Value *V3 = IRB.CreateFAdd(NZ, One);
1087   Value *Match;
1088 
1089   // Test FNeg(1.0)
1090   EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1091   EXPECT_EQ(One, Match);
1092 
1093   // Test FSub(-0.0, 1.0)
1094   EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1095   EXPECT_EQ(One, Match);
1096 
1097   // Test FSub(0.0, 1.0)
1098   EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1099   cast<Instruction>(V2)->setHasNoSignedZeros(true);
1100   EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1101   EXPECT_EQ(One, Match);
1102 
1103   // Test FAdd(-0.0, 1.0)
1104   EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1105 }
1106 
TEST_F(PatternMatchTest,CondBranchTest)1107 TEST_F(PatternMatchTest, CondBranchTest) {
1108   BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1109   BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1110   Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1111 
1112   EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1113 
1114   BasicBlock *A, *B;
1115   EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1116   EXPECT_EQ(TrueBB, A);
1117   EXPECT_EQ(FalseBB, B);
1118 
1119   EXPECT_FALSE(
1120       match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1121   EXPECT_FALSE(
1122       match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1123   EXPECT_FALSE(
1124       match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1125   EXPECT_TRUE(
1126       match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1127 
1128   // Check we can use m_Deferred with branches.
1129   EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1130   Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1131   A = nullptr;
1132   EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1133 }
1134 
TEST_F(PatternMatchTest,WithOverflowInst)1135 TEST_F(PatternMatchTest, WithOverflowInst) {
1136   Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1137                                          IRB.getInt32(0), IRB.getInt32(0));
1138   Value *Add0 = IRB.CreateExtractValue(Add, 0);
1139   Value *Add1 = IRB.CreateExtractValue(Add, 1);
1140 
1141   EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1142   EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1143   EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1144   EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1145   EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1146   EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1147 
1148   WithOverflowInst *WOI;
1149   EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1150   EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1151   EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1152 
1153   EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1154   EXPECT_EQ(Add, WOI);
1155   EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1156   EXPECT_EQ(Add, WOI);
1157 }
1158 
TEST_F(PatternMatchTest,IntrinsicMatcher)1159 TEST_F(PatternMatchTest, IntrinsicMatcher) {
1160   Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1161   Value *Hash = IRB.getInt64(0);
1162   Value *Num = IRB.getInt32(1);
1163   Value *Index = IRB.getInt32(2);
1164   Value *Step = IRB.getInt64(3);
1165 
1166   Value *Ops[] = {Name, Hash, Num, Index, Step};
1167   Module *M = BB->getParent()->getParent();
1168   Function *TheFn =
1169       Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1170 
1171   Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1172 
1173   // Match without capturing.
1174   EXPECT_TRUE(match(
1175       Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1176                       m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1177   EXPECT_FALSE(match(
1178       Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1179                       m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1180 
1181   // Match with capturing.
1182   Value *Arg1 = nullptr;
1183   Value *Arg2 = nullptr;
1184   Value *Arg3 = nullptr;
1185   Value *Arg4 = nullptr;
1186   Value *Arg5 = nullptr;
1187   EXPECT_TRUE(
1188       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1189                             m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1190                             m_Value(Arg4), m_Value(Arg5))));
1191   EXPECT_EQ(Arg1, Name);
1192   EXPECT_EQ(Arg2, Hash);
1193   EXPECT_EQ(Arg3, Num);
1194   EXPECT_EQ(Arg4, Index);
1195   EXPECT_EQ(Arg5, Step);
1196 
1197   // Match specific second argument.
1198   EXPECT_TRUE(
1199       match(Intrinsic5,
1200             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1201                 m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1202   EXPECT_FALSE(
1203       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1204                             m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1205                             m_Value())));
1206 
1207   // Match specific third argument.
1208   EXPECT_TRUE(
1209       match(Intrinsic5,
1210             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1211                 m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1212   EXPECT_FALSE(
1213       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1214                             m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1215                             m_Value())));
1216 
1217   // Match specific fourth argument.
1218   EXPECT_TRUE(
1219       match(Intrinsic5,
1220             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1221                 m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1222   EXPECT_FALSE(
1223       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1224                             m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1225                             m_Value())));
1226 
1227   // Match specific fifth argument.
1228   EXPECT_TRUE(
1229       match(Intrinsic5,
1230             m_Intrinsic<Intrinsic::instrprof_increment_step>(
1231                 m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1232   EXPECT_FALSE(
1233       match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1234                             m_Value(), m_Value(), m_Value(), m_Value(),
1235                             m_SpecificInt(10))));
1236 }
1237 
1238 template <typename T> struct MutableConstTest : PatternMatchTest { };
1239 
1240 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1241                          std::tuple<const Value*, const Instruction *>>
1242     MutableConstTestTypes;
1243 TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
1244 
TYPED_TEST(MutableConstTest,ICmp)1245 TYPED_TEST(MutableConstTest, ICmp) {
1246   auto &IRB = PatternMatchTest::IRB;
1247 
1248   typedef typename std::tuple_element<0, TypeParam>::type ValueType;
1249   typedef typename std::tuple_element<1, TypeParam>::type InstructionType;
1250 
1251   Value *L = IRB.getInt32(1);
1252   Value *R = IRB.getInt32(2);
1253   ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1254 
1255   ValueType MatchL;
1256   ValueType MatchR;
1257   ICmpInst::Predicate MatchPred;
1258 
1259   EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1260               .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1261   EXPECT_EQ(L, MatchL);
1262   EXPECT_EQ(R, MatchR);
1263 }
1264 
1265 } // anonymous namespace.
1266