blob: d52d4fe980fcdf815b42e34afd1001b40f43118c [file] [log] [blame]
Arnold Schwaighofere972d032013-05-05 01:54:46 +00001//===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Arnold Schwaighofere972d032013-05-05 01:54:46 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/IR/PatternMatch.h"
Roman Lebedev9f88fef2019-07-25 13:34:24 +000010#include "llvm/ADT/APSInt.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000011#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"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000017#include "llvm/IR/Function.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000018#include "llvm/IR/IRBuilder.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000019#include "llvm/IR/Instructions.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000020#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/MDBuilder.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000022#include "llvm/IR/Module.h"
Chandler Carruth64396b02014-03-04 12:05:47 +000023#include "llvm/IR/NoFolder.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000024#include "llvm/IR/Operator.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000025#include "llvm/IR/Type.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000026#include "gtest/gtest.h"
27
Chandler Carruth4603e96a2014-01-05 02:07:20 +000028using namespace llvm;
Arnold Schwaighofere972d032013-05-05 01:54:46 +000029using namespace llvm::PatternMatch;
30
Arnold Schwaighofere972d032013-05-05 01:54:46 +000031namespace {
32
Chandler Carruth4603e96a2014-01-05 02:07:20 +000033struct PatternMatchTest : ::testing::Test {
34 LLVMContext Ctx;
Ahmed Charles56440fd2014-03-06 05:51:42 +000035 std::unique_ptr<Module> M;
Chandler Carruth4603e96a2014-01-05 02:07:20 +000036 Function *F;
37 BasicBlock *BB;
Mehdi Aminiba9fba82016-03-13 21:05:13 +000038 IRBuilder<NoFolder> IRB;
Arnold Schwaighofere972d032013-05-05 01:54:46 +000039
Chandler Carruth4603e96a2014-01-05 02:07:20 +000040 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())),
Ilya Biryukov7284d442019-07-15 16:43:36 +000045 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
Chandler Carruth4603e96a2014-01-05 02:07:20 +000046};
Arnold Schwaighofere972d032013-05-05 01:54:46 +000047
Chandler Carruthcde91b42014-01-05 09:14:53 +000048TEST_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
Roman Lebedevc5f92bd2019-07-10 16:07:35 +000068TEST_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
107TEST_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
146TEST_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
185TEST_F(PatternMatchTest, SpecificIntUGE) {
186 Type *IntTy = IRB.getInt32Ty();
187 unsigned BitWidth = IntTy->getScalarSizeInBits();
188
189 Value *Zero = ConstantInt::get(IntTy, 0);
190 Value *One = ConstantInt::get(IntTy, 1);
191 Value *NegOne = ConstantInt::get(IntTy, -1);
192
193 EXPECT_TRUE(
194 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
195 .match(Zero));
196 EXPECT_TRUE(
197 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
198 .match(One));
199 EXPECT_TRUE(
200 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
201 .match(NegOne));
202
203 EXPECT_FALSE(
204 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
205 .match(Zero));
206 EXPECT_TRUE(
207 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
208 .match(One));
209 EXPECT_TRUE(
210 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
211 .match(NegOne));
212
213 EXPECT_FALSE(
214 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
215 .match(Zero));
216 EXPECT_FALSE(
217 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
218 .match(One));
219 EXPECT_TRUE(
220 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
221 .match(NegOne));
222}
223
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000224TEST_F(PatternMatchTest, SpecificIntULT) {
225 Type *IntTy = IRB.getInt32Ty();
226 unsigned BitWidth = IntTy->getScalarSizeInBits();
227
228 Value *Zero = ConstantInt::get(IntTy, 0);
229 Value *One = ConstantInt::get(IntTy, 1);
230 Value *NegOne = ConstantInt::get(IntTy, -1);
231
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000232 EXPECT_FALSE(
233 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
234 .match(Zero));
235 EXPECT_FALSE(
236 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
237 .match(One));
238 EXPECT_FALSE(
239 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
240 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000241
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000242 EXPECT_TRUE(
243 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
244 .match(Zero));
245 EXPECT_FALSE(
246 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
247 .match(One));
248 EXPECT_FALSE(
249 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
250 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000251
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000252 EXPECT_TRUE(
253 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
254 .match(Zero));
255 EXPECT_TRUE(
256 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
257 .match(One));
258 EXPECT_FALSE(
259 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
260 .match(NegOne));
261}
262
263TEST_F(PatternMatchTest, SpecificIntULE) {
264 Type *IntTy = IRB.getInt32Ty();
265 unsigned BitWidth = IntTy->getScalarSizeInBits();
266
267 Value *Zero = ConstantInt::get(IntTy, 0);
268 Value *One = ConstantInt::get(IntTy, 1);
269 Value *NegOne = ConstantInt::get(IntTy, -1);
270
271 EXPECT_TRUE(
272 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
273 .match(Zero));
274 EXPECT_FALSE(
275 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
276 .match(One));
277 EXPECT_FALSE(
278 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
279 .match(NegOne));
280
281 EXPECT_TRUE(
282 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
283 .match(Zero));
284 EXPECT_TRUE(
285 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
286 .match(One));
287 EXPECT_FALSE(
288 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
289 .match(NegOne));
290
291 EXPECT_TRUE(
292 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
293 .match(Zero));
294 EXPECT_TRUE(
295 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
296 .match(One));
297 EXPECT_TRUE(
298 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
299 .match(NegOne));
300}
301
302TEST_F(PatternMatchTest, SpecificIntSGT) {
303 Type *IntTy = IRB.getInt32Ty();
304 unsigned BitWidth = IntTy->getScalarSizeInBits();
305
306 Value *Zero = ConstantInt::get(IntTy, 0);
307 Value *One = ConstantInt::get(IntTy, 1);
308 Value *NegOne = ConstantInt::get(IntTy, -1);
309
310 EXPECT_FALSE(
311 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
312 .match(Zero));
313 EXPECT_TRUE(
314 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
315 .match(One));
316 EXPECT_FALSE(
317 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
318 .match(NegOne));
319
320 EXPECT_FALSE(
321 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
322 .match(Zero));
323 EXPECT_FALSE(
324 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
325 .match(One));
326 EXPECT_FALSE(
327 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
328 .match(NegOne));
329
330 EXPECT_TRUE(
331 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
332 .match(Zero));
333 EXPECT_TRUE(
334 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
335 .match(One));
336 EXPECT_FALSE(
337 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
338 .match(NegOne));
339}
340
341TEST_F(PatternMatchTest, SpecificIntSGE) {
342 Type *IntTy = IRB.getInt32Ty();
343 unsigned BitWidth = IntTy->getScalarSizeInBits();
344
345 Value *Zero = ConstantInt::get(IntTy, 0);
346 Value *One = ConstantInt::get(IntTy, 1);
347 Value *NegOne = ConstantInt::get(IntTy, -1);
348
349 EXPECT_TRUE(
350 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
351 .match(Zero));
352 EXPECT_TRUE(
353 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
354 .match(One));
355 EXPECT_FALSE(
356 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
357 .match(NegOne));
358
359 EXPECT_FALSE(
360 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
361 .match(Zero));
362 EXPECT_TRUE(
363 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
364 .match(One));
365 EXPECT_FALSE(
366 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
367 .match(NegOne));
368
369 EXPECT_TRUE(
370 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
371 .match(Zero));
372 EXPECT_TRUE(
373 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
374 .match(One));
375 EXPECT_TRUE(
376 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
377 .match(NegOne));
378}
379
380TEST_F(PatternMatchTest, SpecificIntSLT) {
381 Type *IntTy = IRB.getInt32Ty();
382 unsigned BitWidth = IntTy->getScalarSizeInBits();
383
384 Value *Zero = ConstantInt::get(IntTy, 0);
385 Value *One = ConstantInt::get(IntTy, 1);
386 Value *NegOne = ConstantInt::get(IntTy, -1);
387
388 EXPECT_FALSE(
389 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
390 .match(Zero));
391 EXPECT_FALSE(
392 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
393 .match(One));
394 EXPECT_TRUE(
395 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
396 .match(NegOne));
397
398 EXPECT_TRUE(
399 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
400 .match(Zero));
401 EXPECT_FALSE(
402 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
403 .match(One));
404 EXPECT_TRUE(
405 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
406 .match(NegOne));
407
408 EXPECT_FALSE(
409 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
410 .match(Zero));
411 EXPECT_FALSE(
412 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
413 .match(One));
414 EXPECT_FALSE(
415 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
416 .match(NegOne));
417}
418
419TEST_F(PatternMatchTest, SpecificIntSLE) {
420 Type *IntTy = IRB.getInt32Ty();
421 unsigned BitWidth = IntTy->getScalarSizeInBits();
422
423 Value *Zero = ConstantInt::get(IntTy, 0);
424 Value *One = ConstantInt::get(IntTy, 1);
425 Value *NegOne = ConstantInt::get(IntTy, -1);
426
427 EXPECT_TRUE(
428 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
429 .match(Zero));
430 EXPECT_FALSE(
431 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
432 .match(One));
433 EXPECT_TRUE(
434 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
435 .match(NegOne));
436
437 EXPECT_TRUE(
438 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
439 .match(Zero));
440 EXPECT_TRUE(
441 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
442 .match(One));
443 EXPECT_TRUE(
444 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
445 .match(NegOne));
446
447 EXPECT_FALSE(
448 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
449 .match(Zero));
450 EXPECT_FALSE(
451 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
452 .match(One));
453 EXPECT_TRUE(
454 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
455 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000456}
457
Roman Lebedev6df3fc52019-07-25 13:34:14 +0000458TEST_F(PatternMatchTest, Unless) {
459 Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
460
461 EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X));
462 EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X));
463
464 EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X));
465 EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X));
466
467 EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X));
468 EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X));
469
470 EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X));
471 EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
472}
473
Roman Lebedev9f88fef2019-07-25 13:34:24 +0000474TEST_F(PatternMatchTest, Power2) {
475 Value *C128 = IRB.getInt32(128);
476 Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
477
478 EXPECT_TRUE(m_Power2().match(C128));
479 EXPECT_FALSE(m_Power2().match(CNeg128));
480
481 EXPECT_FALSE(m_NegatedPower2().match(C128));
482 EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
483
484 Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
485 Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
486
487 EXPECT_TRUE(m_Power2().match(CIntMin));
488 EXPECT_TRUE(m_Power2().match(CNegIntMin));
489
490 EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
491 EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
492}
493
Roman Lebedev6959b8e2018-04-27 21:23:20 +0000494TEST_F(PatternMatchTest, CommutativeDeferredValue) {
495 Value *X = IRB.getInt32(1);
496 Value *Y = IRB.getInt32(2);
497
498 {
499 Value *tX = X;
500 EXPECT_TRUE(match(X, m_Deferred(tX)));
501 EXPECT_FALSE(match(Y, m_Deferred(tX)));
502 }
503 {
504 const Value *tX = X;
505 EXPECT_TRUE(match(X, m_Deferred(tX)));
506 EXPECT_FALSE(match(Y, m_Deferred(tX)));
507 }
508 {
509 Value *const tX = X;
510 EXPECT_TRUE(match(X, m_Deferred(tX)));
511 EXPECT_FALSE(match(Y, m_Deferred(tX)));
512 }
513 {
514 const Value *const tX = X;
515 EXPECT_TRUE(match(X, m_Deferred(tX)));
516 EXPECT_FALSE(match(Y, m_Deferred(tX)));
517 }
518
519 {
520 Value *tX = nullptr;
521 EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
522 EXPECT_EQ(tX, X);
523 }
524 {
525 Value *tX = nullptr;
526 EXPECT_FALSE(
527 match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
528 }
529
530 auto checkMatch = [X, Y](Value *Pattern) {
531 Value *tX = nullptr, *tY = nullptr;
532 EXPECT_TRUE(match(
533 Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
534 EXPECT_EQ(tX, X);
535 EXPECT_EQ(tY, Y);
536 };
537
538 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
539 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
540 checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
541 checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
542}
543
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000544TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000545 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000546 Value *L = ConstantFP::get(FltTy, 1.0);
547 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000548 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000549
550 // Test OLT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000551 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
552 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000553 EXPECT_EQ(L, MatchL);
554 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000555
556 // Test OLE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000557 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
558 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000559 EXPECT_EQ(L, MatchL);
560 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000561
562 // Test no match on OGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000563 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
564 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000565
566 // Test no match on OGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000567 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
568 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000569
Craig Topperc6635522017-06-13 17:18:45 +0000570 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
571 // %cmp = fcmp oge L, R
572 // %min = select %cmp R, L
573 // Given L == NaN
574 // the above is expanded to %cmp == false ==> %min = L
575 // which is true for UnordFMin, not OrdFMin, so test that:
576
577 // [OU]GE with inverted select.
578 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000579 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000580 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
581 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000582 EXPECT_EQ(L, MatchL);
583 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000584
Craig Topperc6635522017-06-13 17:18:45 +0000585 // [OU]GT with inverted select.
586 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000587 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000588 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
589 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000590 EXPECT_EQ(L, MatchL);
591 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000592}
593
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000594TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000595 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000596 Value *L = ConstantFP::get(FltTy, 1.0);
597 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000598 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000599
600 // Test OGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000601 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
602 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000603 EXPECT_EQ(L, MatchL);
604 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000605
606 // Test OGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000607 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
608 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000609 EXPECT_EQ(L, MatchL);
610 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000611
612 // Test no match on OLE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000613 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
614 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000615
616 // Test no match on OLT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000617 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
618 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000619
Craig Topperc6635522017-06-13 17:18:45 +0000620
621 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
622 // %cmp = fcmp ole L, R
623 // %max = select %cmp, R, L
624 // Given L == NaN,
625 // the above is expanded to %cmp == false ==> %max == L
626 // which is true for UnordFMax, not OrdFMax, so test that:
627
628 // [OU]LE with inverted select.
629 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
630 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
Chandler Carruth91f4e602014-01-05 02:23:11 +0000631 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
Craig Topperc6635522017-06-13 17:18:45 +0000632 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000633 EXPECT_EQ(L, MatchL);
634 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000635
Craig Topperc6635522017-06-13 17:18:45 +0000636 // [OUT]LT with inverted select.
637 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
638 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
Chandler Carruth91f4e602014-01-05 02:23:11 +0000639 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
Craig Topperc6635522017-06-13 17:18:45 +0000640 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000641 EXPECT_EQ(L, MatchL);
642 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000643}
644
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000645TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000646 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000647 Value *L = ConstantFP::get(FltTy, 1.0);
648 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000649 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000650
651 // Test ULT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000652 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
653 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000654 EXPECT_EQ(L, MatchL);
655 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000656
657 // Test ULE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000658 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
659 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000660 EXPECT_EQ(L, MatchL);
661 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000662
663 // Test no match on UGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000664 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
665 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000666
667 // Test no match on UGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000668 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
669 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000670
Craig Topperc6635522017-06-13 17:18:45 +0000671 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
672 // %cmp = fcmp uge L, R
673 // %min = select %cmp R, L
674 // Given L == NaN
675 // the above is expanded to %cmp == true ==> %min = R
676 // which is true for OrdFMin, not UnordFMin, so test that:
677
678 // [UO]GE with inverted select.
679 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000680 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000681 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
682 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000683 EXPECT_EQ(L, MatchL);
684 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000685
Craig Topperc6635522017-06-13 17:18:45 +0000686 // [UO]GT with inverted select.
687 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000688 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000689 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
690 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000691 EXPECT_EQ(L, MatchL);
692 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000693}
694
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000695TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000696 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000697 Value *L = ConstantFP::get(FltTy, 1.0);
698 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000699 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000700
701 // Test UGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000702 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
703 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000704 EXPECT_EQ(L, MatchL);
705 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000706
707 // Test UGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000708 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
709 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000710 EXPECT_EQ(L, MatchL);
711 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000712
713 // Test no match on ULE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000714 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
715 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000716
717 // Test no match on ULT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000718 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
719 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000720
Craig Topperc6635522017-06-13 17:18:45 +0000721 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
722 // %cmp = fcmp ule L, R
723 // %max = select %cmp R, L
724 // Given L == NaN
725 // the above is expanded to %cmp == true ==> %max = R
726 // which is true for OrdFMax, not UnordFMax, so test that:
727
728 // [UO]LE with inverted select.
729 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000730 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000731 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
732 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000733 EXPECT_EQ(L, MatchL);
734 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000735
Craig Topperc6635522017-06-13 17:18:45 +0000736 // [UO]LT with inverted select.
737 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000738 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000739 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
740 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000741 EXPECT_EQ(L, MatchL);
742 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000743}
744
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000745TEST_F(PatternMatchTest, OverflowingBinOps) {
746 Value *L = IRB.getInt32(1);
747 Value *R = IRB.getInt32(2);
748 Value *MatchL, *MatchR;
749
750 EXPECT_TRUE(
751 m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
752 EXPECT_EQ(L, MatchL);
753 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000754 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000755 EXPECT_TRUE(
756 m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
757 EXPECT_EQ(L, MatchL);
758 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000759 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000760 EXPECT_TRUE(
761 m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
762 EXPECT_EQ(L, MatchL);
763 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000764 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000765 EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
766 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
767 EXPECT_EQ(L, MatchL);
768 EXPECT_EQ(R, MatchR);
769
770 EXPECT_TRUE(
771 m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
772 EXPECT_EQ(L, MatchL);
773 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000774 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000775 EXPECT_TRUE(
776 m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
777 EXPECT_EQ(L, MatchL);
778 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000779 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000780 EXPECT_TRUE(
781 m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
782 EXPECT_EQ(L, MatchL);
783 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000784 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000785 EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
786 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
787 EXPECT_EQ(L, MatchL);
788 EXPECT_EQ(R, MatchR);
789
790 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
791 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
792 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
793 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
794 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
795 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
796 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
797 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
798 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
799 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
800 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
801 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
802 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
803
804 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
805 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
806 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
807 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
808 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
809 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
810 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
811 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
812 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
813 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
814 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
815 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
816 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
817}
818
Sjoerd Meijerc6079012018-06-20 07:27:45 +0000819TEST_F(PatternMatchTest, LoadStoreOps) {
820 // Create this load/store sequence:
821 //
822 // %p = alloca i32*
823 // %0 = load i32*, i32** %p
824 // store i32 42, i32* %0
825
826 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
James Y Knight14359ef2019-02-01 20:44:24 +0000827 Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
Sjoerd Meijerc6079012018-06-20 07:27:45 +0000828 Value *FourtyTwo = IRB.getInt32(42);
829 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
830 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
831
832 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
833 EXPECT_EQ(Alloca, MatchLoad);
834
835 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
836
837 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
838
839 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
840 .match(StoreInst));
841 EXPECT_EQ(FourtyTwo, MatchStoreVal);
842 EXPECT_EQ(Alloca, MatchStorePointer);
843
844 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
845 .match(Alloca));
846
847 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
848 .match(StoreInst));
849 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
850 .match(StoreInst));
851 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
852 .match(StoreInst));
853}
854
Daniel Neilson45796f62018-03-28 15:39:00 +0000855TEST_F(PatternMatchTest, VectorOps) {
856 // Build up small tree of vector operations
857 //
858 // Val = 0 + 1
859 // Val2 = Val + 3
860 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
861 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
862 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
863 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
864 //
865 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
866 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
867 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
868 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
869 //
870 // SP1 = VectorSplat(2, i8 2)
871 // SP2 = VectorSplat(2, i8 %Val)
872 Type *VecTy = VectorType::get(IRB.getInt8Ty(), 2);
873 Type *i32 = IRB.getInt32Ty();
874 Type *i32VecTy = VectorType::get(i32, 2);
875
876 Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
877 Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
878
879 SmallVector<Constant *, 2> VecElemIdxs;
880 VecElemIdxs.push_back(ConstantInt::get(i32, 0));
881 VecElemIdxs.push_back(ConstantInt::get(i32, 2));
882 auto *IdxVec = ConstantVector::get(VecElemIdxs);
883
884 Value *UndefVec = UndefValue::get(VecTy);
885 Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
886 Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
887 Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
888 Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
889
890 Value *EX1 = IRB.CreateExtractElement(VI4, Val);
891 Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
892 Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
893
894 Value *Zero = ConstantAggregateZero::get(i32VecTy);
895 Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero);
896 Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
897 Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero);
898 Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero);
899
900 Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
901 Value *SP2 = IRB.CreateVectorSplat(2, Val);
902
903 Value *A = nullptr, *B = nullptr, *C = nullptr;
904
905 // Test matching insertelement
906 EXPECT_TRUE(match(VI1, m_InsertElement(m_Value(), m_Value(), m_Value())));
907 EXPECT_TRUE(
908 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_ConstantInt())));
909 EXPECT_TRUE(
910 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_Zero())));
911 EXPECT_TRUE(
912 match(VI1, m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero())));
913 EXPECT_TRUE(match(VI2, m_InsertElement(m_Value(), m_Value(), m_Value())));
914 EXPECT_FALSE(
915 match(VI2, m_InsertElement(m_Value(), m_Value(), m_ConstantInt())));
916 EXPECT_FALSE(
917 match(VI2, m_InsertElement(m_Value(), m_ConstantInt(), m_Value())));
918 EXPECT_FALSE(match(VI2, m_InsertElement(m_Constant(), m_Value(), m_Value())));
919 EXPECT_TRUE(match(VI3, m_InsertElement(m_Value(A), m_Value(B), m_Value(C))));
920 EXPECT_TRUE(A == VI1);
921 EXPECT_TRUE(B == Val2);
922 EXPECT_TRUE(isa<ConstantInt>(C));
923 A = B = C = nullptr; // reset
924
925 // Test matching extractelement
926 EXPECT_TRUE(match(EX1, m_ExtractElement(m_Value(A), m_Value(B))));
927 EXPECT_TRUE(A == VI4);
928 EXPECT_TRUE(B == Val);
929 A = B = C = nullptr; // reset
930 EXPECT_FALSE(match(EX1, m_ExtractElement(m_Value(), m_ConstantInt())));
931 EXPECT_TRUE(match(EX2, m_ExtractElement(m_Value(), m_ConstantInt())));
932 EXPECT_TRUE(match(EX3, m_ExtractElement(m_Constant(), m_ConstantInt())));
933
934 // Test matching shufflevector
935 EXPECT_TRUE(match(SI1, m_ShuffleVector(m_Value(), m_Undef(), m_Zero())));
936 EXPECT_TRUE(match(SI2, m_ShuffleVector(m_Value(A), m_Value(B), m_Value(C))));
937 EXPECT_TRUE(A == VI3);
938 EXPECT_TRUE(B == VI4);
939 EXPECT_TRUE(C == IdxVec);
940 A = B = C = nullptr; // reset
941
942 // Test matching the vector splat pattern
943 EXPECT_TRUE(match(
944 SI1,
945 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()),
946 m_Undef(), m_Zero())));
947 EXPECT_FALSE(match(
948 SI3, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
949 m_Undef(), m_Zero())));
950 EXPECT_FALSE(match(
951 SI4, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
952 m_Undef(), m_Zero())));
953 EXPECT_TRUE(match(
954 SP1,
955 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(2), m_Zero()),
956 m_Undef(), m_Zero())));
957 EXPECT_TRUE(match(
958 SP2, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(A), m_Zero()),
959 m_Undef(), m_Zero())));
960 EXPECT_TRUE(A == Val);
961}
962
Sanjay Patelf5ead292018-11-20 16:08:19 +0000963TEST_F(PatternMatchTest, VectorUndefInt) {
964 Type *ScalarTy = IRB.getInt8Ty();
965 Type *VectorTy = VectorType::get(ScalarTy, 4);
966 Constant *ScalarUndef = UndefValue::get(ScalarTy);
967 Constant *VectorUndef = UndefValue::get(VectorTy);
968 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
969 Constant *VectorZero = Constant::getNullValue(VectorTy);
970
971 SmallVector<Constant *, 4> Elems;
972 Elems.push_back(ScalarUndef);
973 Elems.push_back(ScalarZero);
974 Elems.push_back(ScalarUndef);
975 Elems.push_back(ScalarZero);
976 Constant *VectorZeroUndef = ConstantVector::get(Elems);
977
978 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
979 EXPECT_TRUE(match(VectorUndef, m_Undef()));
980 EXPECT_FALSE(match(ScalarZero, m_Undef()));
981 EXPECT_FALSE(match(VectorZero, m_Undef()));
982 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
983
984 EXPECT_FALSE(match(ScalarUndef, m_Zero()));
985 EXPECT_FALSE(match(VectorUndef, m_Zero()));
986 EXPECT_TRUE(match(ScalarZero, m_Zero()));
987 EXPECT_TRUE(match(VectorZero, m_Zero()));
988 EXPECT_TRUE(match(VectorZeroUndef, m_Zero()));
989}
990
991TEST_F(PatternMatchTest, VectorUndefFloat) {
992 Type *ScalarTy = IRB.getFloatTy();
993 Type *VectorTy = VectorType::get(ScalarTy, 4);
994 Constant *ScalarUndef = UndefValue::get(ScalarTy);
995 Constant *VectorUndef = UndefValue::get(VectorTy);
996 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
997 Constant *VectorZero = Constant::getNullValue(VectorTy);
998
999 SmallVector<Constant *, 4> Elems;
1000 Elems.push_back(ScalarUndef);
1001 Elems.push_back(ScalarZero);
1002 Elems.push_back(ScalarUndef);
1003 Elems.push_back(ScalarZero);
1004 Constant *VectorZeroUndef = ConstantVector::get(Elems);
1005
1006 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1007 EXPECT_TRUE(match(VectorUndef, m_Undef()));
1008 EXPECT_FALSE(match(ScalarZero, m_Undef()));
1009 EXPECT_FALSE(match(VectorZero, m_Undef()));
1010 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1011
1012 EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1013 EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1014 EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1015 EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1016 EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
1017}
1018
Cameron McInallybe7138b2019-05-03 21:19:12 +00001019TEST_F(PatternMatchTest, FloatingPointFNeg) {
1020 Type *FltTy = IRB.getFloatTy();
1021 Value *One = ConstantFP::get(FltTy, 1.0);
1022 Value *Z = ConstantFP::get(FltTy, 0.0);
1023 Value *NZ = ConstantFP::get(FltTy, -0.0);
1024 Value *V = IRB.CreateFNeg(One);
1025 Value *V1 = IRB.CreateFSub(NZ, One);
1026 Value *V2 = IRB.CreateFSub(Z, One);
1027 Value *V3 = IRB.CreateFAdd(NZ, One);
1028 Value *Match;
1029
1030 // Test FNeg(1.0)
1031 EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1032 EXPECT_EQ(One, Match);
1033
1034 // Test FSub(-0.0, 1.0)
1035 EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1036 EXPECT_EQ(One, Match);
1037
1038 // Test FSub(0.0, 1.0)
1039 EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1040 cast<Instruction>(V2)->setHasNoSignedZeros(true);
1041 EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1042 EXPECT_EQ(One, Match);
1043
1044 // Test FAdd(-0.0, 1.0)
1045 EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1046}
1047
Florian Hahn5c3bc3c2019-09-25 15:05:08 +00001048TEST_F(PatternMatchTest, CondBranchTest) {
1049 BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1050 BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1051 Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1052
1053 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1054
1055 BasicBlock *A, *B;
1056 EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1057 EXPECT_EQ(TrueBB, A);
1058 EXPECT_EQ(FalseBB, B);
1059
1060 EXPECT_FALSE(
1061 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1062 EXPECT_FALSE(
1063 match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1064 EXPECT_FALSE(
1065 match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1066 EXPECT_TRUE(
1067 match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1068
1069 // Check we can use m_Deferred with branches.
1070 EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1071 Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1072 A = nullptr;
1073 EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1074}
1075
Pete Cooperab47fa62016-08-12 22:16:05 +00001076template <typename T> struct MutableConstTest : PatternMatchTest { };
1077
1078typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1079 std::tuple<const Value*, const Instruction *>>
1080 MutableConstTestTypes;
1081TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
1082
1083TYPED_TEST(MutableConstTest, ICmp) {
1084 auto &IRB = PatternMatchTest::IRB;
1085
1086 typedef typename std::tuple_element<0, TypeParam>::type ValueType;
1087 typedef typename std::tuple_element<1, TypeParam>::type InstructionType;
1088
1089 Value *L = IRB.getInt32(1);
1090 Value *R = IRB.getInt32(2);
1091 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1092
1093 ValueType MatchL;
1094 ValueType MatchR;
1095 ICmpInst::Predicate MatchPred;
1096
1097 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1098 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1099 EXPECT_EQ(L, MatchL);
1100 EXPECT_EQ(R, MatchR);
1101}
1102
Arnold Schwaighofere972d032013-05-05 01:54:46 +00001103} // anonymous namespace.