blob: 3f8e6918019514e353fc111178391e2fe42571ff [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"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000010#include "llvm/ADT/STLExtras.h"
11#include "llvm/Analysis/ValueTracking.h"
12#include "llvm/IR/BasicBlock.h"
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/DerivedTypes.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000016#include "llvm/IR/Function.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000017#include "llvm/IR/IRBuilder.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000018#include "llvm/IR/Instructions.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000019#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/MDBuilder.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000021#include "llvm/IR/Module.h"
Chandler Carruth64396b02014-03-04 12:05:47 +000022#include "llvm/IR/NoFolder.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000023#include "llvm/IR/Operator.h"
Chandler Carruth4603e96a2014-01-05 02:07:20 +000024#include "llvm/IR/Type.h"
Arnold Schwaighofere972d032013-05-05 01:54:46 +000025#include "gtest/gtest.h"
26
Chandler Carruth4603e96a2014-01-05 02:07:20 +000027using namespace llvm;
Arnold Schwaighofere972d032013-05-05 01:54:46 +000028using namespace llvm::PatternMatch;
29
Arnold Schwaighofere972d032013-05-05 01:54:46 +000030namespace {
31
Chandler Carruth4603e96a2014-01-05 02:07:20 +000032struct PatternMatchTest : ::testing::Test {
33 LLVMContext Ctx;
Ahmed Charles56440fd2014-03-06 05:51:42 +000034 std::unique_ptr<Module> M;
Chandler Carruth4603e96a2014-01-05 02:07:20 +000035 Function *F;
36 BasicBlock *BB;
Mehdi Aminiba9fba82016-03-13 21:05:13 +000037 IRBuilder<NoFolder> IRB;
Dmitry Venikovcb238de2019-07-15 14:47:45 +000038 TargetLibraryInfoImpl TLII;
39 TargetLibraryInfo TLI;
Arnold Schwaighofere972d032013-05-05 01:54:46 +000040
Chandler Carruth4603e96a2014-01-05 02:07:20 +000041 PatternMatchTest()
42 : M(new Module("PatternMatchTestModule", Ctx)),
43 F(Function::Create(
44 FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
45 Function::ExternalLinkage, "f", M.get())),
Dmitry Venikovcb238de2019-07-15 14:47:45 +000046 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB), TLI(TLII) {}
Chandler Carruth4603e96a2014-01-05 02:07:20 +000047};
Arnold Schwaighofere972d032013-05-05 01:54:46 +000048
Chandler Carruthcde91b42014-01-05 09:14:53 +000049TEST_F(PatternMatchTest, OneUse) {
50 // Build up a little tree of values:
51 //
52 // One = (1 + 2) + 42
53 // Two = One + 42
54 // Leaf = (Two + 8) + (Two + 13)
55 Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)),
56 IRB.getInt32(42));
57 Value *Two = IRB.CreateAdd(One, IRB.getInt32(42));
58 Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)),
59 IRB.CreateAdd(Two, IRB.getInt32(13)));
60 Value *V;
61
62 EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
63 EXPECT_EQ(One, V);
64
65 EXPECT_FALSE(m_OneUse(m_Value()).match(Two));
66 EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
67}
68
Roman Lebedevc5f92bd2019-07-10 16:07:35 +000069TEST_F(PatternMatchTest, SpecificIntEQ) {
70 Type *IntTy = IRB.getInt32Ty();
71 unsigned BitWidth = IntTy->getScalarSizeInBits();
72
73 Value *Zero = ConstantInt::get(IntTy, 0);
74 Value *One = ConstantInt::get(IntTy, 1);
75 Value *NegOne = ConstantInt::get(IntTy, -1);
76
77 EXPECT_TRUE(
78 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
79 .match(Zero));
80 EXPECT_FALSE(
81 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
82 .match(One));
83 EXPECT_FALSE(
84 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
85 .match(NegOne));
86
87 EXPECT_FALSE(
88 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
89 .match(Zero));
90 EXPECT_TRUE(
91 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
92 .match(One));
93 EXPECT_FALSE(
94 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
95 .match(NegOne));
96
97 EXPECT_FALSE(
98 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
99 .match(Zero));
100 EXPECT_FALSE(
101 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
102 .match(One));
103 EXPECT_TRUE(
104 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
105 .match(NegOne));
106}
107
108TEST_F(PatternMatchTest, SpecificIntNE) {
109 Type *IntTy = IRB.getInt32Ty();
110 unsigned BitWidth = IntTy->getScalarSizeInBits();
111
112 Value *Zero = ConstantInt::get(IntTy, 0);
113 Value *One = ConstantInt::get(IntTy, 1);
114 Value *NegOne = ConstantInt::get(IntTy, -1);
115
116 EXPECT_FALSE(
117 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
118 .match(Zero));
119 EXPECT_TRUE(
120 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
121 .match(One));
122 EXPECT_TRUE(
123 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
124 .match(NegOne));
125
126 EXPECT_TRUE(
127 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
128 .match(Zero));
129 EXPECT_FALSE(
130 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
131 .match(One));
132 EXPECT_TRUE(
133 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
134 .match(NegOne));
135
136 EXPECT_TRUE(
137 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
138 .match(Zero));
139 EXPECT_TRUE(
140 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
141 .match(One));
142 EXPECT_FALSE(
143 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
144 .match(NegOne));
145}
146
147TEST_F(PatternMatchTest, SpecificIntUGT) {
148 Type *IntTy = IRB.getInt32Ty();
149 unsigned BitWidth = IntTy->getScalarSizeInBits();
150
151 Value *Zero = ConstantInt::get(IntTy, 0);
152 Value *One = ConstantInt::get(IntTy, 1);
153 Value *NegOne = ConstantInt::get(IntTy, -1);
154
155 EXPECT_FALSE(
156 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
157 .match(Zero));
158 EXPECT_TRUE(
159 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
160 .match(One));
161 EXPECT_TRUE(
162 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
163 .match(NegOne));
164
165 EXPECT_FALSE(
166 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
167 .match(Zero));
168 EXPECT_FALSE(
169 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
170 .match(One));
171 EXPECT_TRUE(
172 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
173 .match(NegOne));
174
175 EXPECT_FALSE(
176 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
177 .match(Zero));
178 EXPECT_FALSE(
179 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
180 .match(One));
181 EXPECT_FALSE(
182 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
183 .match(NegOne));
184}
185
186TEST_F(PatternMatchTest, SpecificIntUGE) {
187 Type *IntTy = IRB.getInt32Ty();
188 unsigned BitWidth = IntTy->getScalarSizeInBits();
189
190 Value *Zero = ConstantInt::get(IntTy, 0);
191 Value *One = ConstantInt::get(IntTy, 1);
192 Value *NegOne = ConstantInt::get(IntTy, -1);
193
194 EXPECT_TRUE(
195 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
196 .match(Zero));
197 EXPECT_TRUE(
198 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
199 .match(One));
200 EXPECT_TRUE(
201 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
202 .match(NegOne));
203
204 EXPECT_FALSE(
205 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
206 .match(Zero));
207 EXPECT_TRUE(
208 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
209 .match(One));
210 EXPECT_TRUE(
211 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
212 .match(NegOne));
213
214 EXPECT_FALSE(
215 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
216 .match(Zero));
217 EXPECT_FALSE(
218 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
219 .match(One));
220 EXPECT_TRUE(
221 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
222 .match(NegOne));
223}
224
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000225TEST_F(PatternMatchTest, SpecificIntULT) {
226 Type *IntTy = IRB.getInt32Ty();
227 unsigned BitWidth = IntTy->getScalarSizeInBits();
228
229 Value *Zero = ConstantInt::get(IntTy, 0);
230 Value *One = ConstantInt::get(IntTy, 1);
231 Value *NegOne = ConstantInt::get(IntTy, -1);
232
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000233 EXPECT_FALSE(
234 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
235 .match(Zero));
236 EXPECT_FALSE(
237 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
238 .match(One));
239 EXPECT_FALSE(
240 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
241 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000242
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000243 EXPECT_TRUE(
244 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
245 .match(Zero));
246 EXPECT_FALSE(
247 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
248 .match(One));
249 EXPECT_FALSE(
250 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
251 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000252
Roman Lebedevc5f92bd2019-07-10 16:07:35 +0000253 EXPECT_TRUE(
254 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
255 .match(Zero));
256 EXPECT_TRUE(
257 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
258 .match(One));
259 EXPECT_FALSE(
260 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
261 .match(NegOne));
262}
263
264TEST_F(PatternMatchTest, SpecificIntULE) {
265 Type *IntTy = IRB.getInt32Ty();
266 unsigned BitWidth = IntTy->getScalarSizeInBits();
267
268 Value *Zero = ConstantInt::get(IntTy, 0);
269 Value *One = ConstantInt::get(IntTy, 1);
270 Value *NegOne = ConstantInt::get(IntTy, -1);
271
272 EXPECT_TRUE(
273 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
274 .match(Zero));
275 EXPECT_FALSE(
276 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
277 .match(One));
278 EXPECT_FALSE(
279 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
280 .match(NegOne));
281
282 EXPECT_TRUE(
283 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
284 .match(Zero));
285 EXPECT_TRUE(
286 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
287 .match(One));
288 EXPECT_FALSE(
289 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
290 .match(NegOne));
291
292 EXPECT_TRUE(
293 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
294 .match(Zero));
295 EXPECT_TRUE(
296 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
297 .match(One));
298 EXPECT_TRUE(
299 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
300 .match(NegOne));
301}
302
303TEST_F(PatternMatchTest, SpecificIntSGT) {
304 Type *IntTy = IRB.getInt32Ty();
305 unsigned BitWidth = IntTy->getScalarSizeInBits();
306
307 Value *Zero = ConstantInt::get(IntTy, 0);
308 Value *One = ConstantInt::get(IntTy, 1);
309 Value *NegOne = ConstantInt::get(IntTy, -1);
310
311 EXPECT_FALSE(
312 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
313 .match(Zero));
314 EXPECT_TRUE(
315 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
316 .match(One));
317 EXPECT_FALSE(
318 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
319 .match(NegOne));
320
321 EXPECT_FALSE(
322 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
323 .match(Zero));
324 EXPECT_FALSE(
325 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
326 .match(One));
327 EXPECT_FALSE(
328 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
329 .match(NegOne));
330
331 EXPECT_TRUE(
332 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
333 .match(Zero));
334 EXPECT_TRUE(
335 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
336 .match(One));
337 EXPECT_FALSE(
338 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
339 .match(NegOne));
340}
341
342TEST_F(PatternMatchTest, SpecificIntSGE) {
343 Type *IntTy = IRB.getInt32Ty();
344 unsigned BitWidth = IntTy->getScalarSizeInBits();
345
346 Value *Zero = ConstantInt::get(IntTy, 0);
347 Value *One = ConstantInt::get(IntTy, 1);
348 Value *NegOne = ConstantInt::get(IntTy, -1);
349
350 EXPECT_TRUE(
351 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
352 .match(Zero));
353 EXPECT_TRUE(
354 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
355 .match(One));
356 EXPECT_FALSE(
357 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
358 .match(NegOne));
359
360 EXPECT_FALSE(
361 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
362 .match(Zero));
363 EXPECT_TRUE(
364 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
365 .match(One));
366 EXPECT_FALSE(
367 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
368 .match(NegOne));
369
370 EXPECT_TRUE(
371 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
372 .match(Zero));
373 EXPECT_TRUE(
374 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
375 .match(One));
376 EXPECT_TRUE(
377 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
378 .match(NegOne));
379}
380
381TEST_F(PatternMatchTest, SpecificIntSLT) {
382 Type *IntTy = IRB.getInt32Ty();
383 unsigned BitWidth = IntTy->getScalarSizeInBits();
384
385 Value *Zero = ConstantInt::get(IntTy, 0);
386 Value *One = ConstantInt::get(IntTy, 1);
387 Value *NegOne = ConstantInt::get(IntTy, -1);
388
389 EXPECT_FALSE(
390 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
391 .match(Zero));
392 EXPECT_FALSE(
393 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
394 .match(One));
395 EXPECT_TRUE(
396 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
397 .match(NegOne));
398
399 EXPECT_TRUE(
400 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
401 .match(Zero));
402 EXPECT_FALSE(
403 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
404 .match(One));
405 EXPECT_TRUE(
406 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
407 .match(NegOne));
408
409 EXPECT_FALSE(
410 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
411 .match(Zero));
412 EXPECT_FALSE(
413 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
414 .match(One));
415 EXPECT_FALSE(
416 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
417 .match(NegOne));
418}
419
420TEST_F(PatternMatchTest, SpecificIntSLE) {
421 Type *IntTy = IRB.getInt32Ty();
422 unsigned BitWidth = IntTy->getScalarSizeInBits();
423
424 Value *Zero = ConstantInt::get(IntTy, 0);
425 Value *One = ConstantInt::get(IntTy, 1);
426 Value *NegOne = ConstantInt::get(IntTy, -1);
427
428 EXPECT_TRUE(
429 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
430 .match(Zero));
431 EXPECT_FALSE(
432 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
433 .match(One));
434 EXPECT_TRUE(
435 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
436 .match(NegOne));
437
438 EXPECT_TRUE(
439 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
440 .match(Zero));
441 EXPECT_TRUE(
442 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
443 .match(One));
444 EXPECT_TRUE(
445 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
446 .match(NegOne));
447
448 EXPECT_FALSE(
449 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
450 .match(Zero));
451 EXPECT_FALSE(
452 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
453 .match(One));
454 EXPECT_TRUE(
455 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
456 .match(NegOne));
Roman Lebedevfe107fc2019-06-29 11:51:37 +0000457}
458
Roman Lebedev6959b8e2018-04-27 21:23:20 +0000459TEST_F(PatternMatchTest, CommutativeDeferredValue) {
460 Value *X = IRB.getInt32(1);
461 Value *Y = IRB.getInt32(2);
462
463 {
464 Value *tX = X;
465 EXPECT_TRUE(match(X, m_Deferred(tX)));
466 EXPECT_FALSE(match(Y, m_Deferred(tX)));
467 }
468 {
469 const Value *tX = X;
470 EXPECT_TRUE(match(X, m_Deferred(tX)));
471 EXPECT_FALSE(match(Y, m_Deferred(tX)));
472 }
473 {
474 Value *const tX = X;
475 EXPECT_TRUE(match(X, m_Deferred(tX)));
476 EXPECT_FALSE(match(Y, m_Deferred(tX)));
477 }
478 {
479 const Value *const tX = X;
480 EXPECT_TRUE(match(X, m_Deferred(tX)));
481 EXPECT_FALSE(match(Y, m_Deferred(tX)));
482 }
483
484 {
485 Value *tX = nullptr;
486 EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
487 EXPECT_EQ(tX, X);
488 }
489 {
490 Value *tX = nullptr;
491 EXPECT_FALSE(
492 match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
493 }
494
495 auto checkMatch = [X, Y](Value *Pattern) {
496 Value *tX = nullptr, *tY = nullptr;
497 EXPECT_TRUE(match(
498 Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
499 EXPECT_EQ(tX, X);
500 EXPECT_EQ(tY, Y);
501 };
502
503 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
504 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
505 checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
506 checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
507}
508
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000509TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000510 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000511 Value *L = ConstantFP::get(FltTy, 1.0);
512 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000513 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000514
515 // Test OLT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000516 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
517 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000518 EXPECT_EQ(L, MatchL);
519 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000520
521 // Test OLE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000522 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
523 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000524 EXPECT_EQ(L, MatchL);
525 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000526
527 // Test no match on OGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000528 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
529 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000530
531 // Test no match on OGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000532 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
533 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000534
Craig Topperc6635522017-06-13 17:18:45 +0000535 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
536 // %cmp = fcmp oge L, R
537 // %min = select %cmp R, L
538 // Given L == NaN
539 // the above is expanded to %cmp == false ==> %min = L
540 // which is true for UnordFMin, not OrdFMin, so test that:
541
542 // [OU]GE with inverted select.
543 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000544 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000545 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
546 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000547 EXPECT_EQ(L, MatchL);
548 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000549
Craig Topperc6635522017-06-13 17:18:45 +0000550 // [OU]GT with inverted select.
551 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000552 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000553 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
554 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000555 EXPECT_EQ(L, MatchL);
556 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000557}
558
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000559TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000560 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000561 Value *L = ConstantFP::get(FltTy, 1.0);
562 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000563 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000564
565 // Test OGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000566 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
567 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000568 EXPECT_EQ(L, MatchL);
569 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000570
571 // Test OGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000572 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
573 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000574 EXPECT_EQ(L, MatchL);
575 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000576
577 // Test no match on OLE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000578 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
579 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000580
581 // Test no match on OLT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000582 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
583 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000584
Craig Topperc6635522017-06-13 17:18:45 +0000585
586 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
587 // %cmp = fcmp ole L, R
588 // %max = select %cmp, R, L
589 // Given L == NaN,
590 // the above is expanded to %cmp == false ==> %max == L
591 // which is true for UnordFMax, not OrdFMax, so test that:
592
593 // [OU]LE with inverted select.
594 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
595 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
Chandler Carruth91f4e602014-01-05 02:23:11 +0000596 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
Craig Topperc6635522017-06-13 17:18:45 +0000597 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000598 EXPECT_EQ(L, MatchL);
599 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000600
Craig Topperc6635522017-06-13 17:18:45 +0000601 // [OUT]LT with inverted select.
602 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
603 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
Chandler Carruth91f4e602014-01-05 02:23:11 +0000604 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
Craig Topperc6635522017-06-13 17:18:45 +0000605 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000606 EXPECT_EQ(L, MatchL);
607 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000608}
609
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000610TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000611 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000612 Value *L = ConstantFP::get(FltTy, 1.0);
613 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000614 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000615
616 // Test ULT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000617 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
618 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000619 EXPECT_EQ(L, MatchL);
620 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000621
622 // Test ULE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000623 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
624 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000625 EXPECT_EQ(L, MatchL);
626 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000627
628 // Test no match on UGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000629 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
630 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000631
632 // Test no match on UGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000633 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
634 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000635
Craig Topperc6635522017-06-13 17:18:45 +0000636 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
637 // %cmp = fcmp uge L, R
638 // %min = select %cmp R, L
639 // Given L == NaN
640 // the above is expanded to %cmp == true ==> %min = R
641 // which is true for OrdFMin, not UnordFMin, so test that:
642
643 // [UO]GE with inverted select.
644 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000645 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000646 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
647 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000648 EXPECT_EQ(L, MatchL);
649 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000650
Craig Topperc6635522017-06-13 17:18:45 +0000651 // [UO]GT with inverted select.
652 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000653 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000654 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
655 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000656 EXPECT_EQ(L, MatchL);
657 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000658}
659
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000660TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
Chandler Carruth91f4e602014-01-05 02:23:11 +0000661 Type *FltTy = IRB.getFloatTy();
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000662 Value *L = ConstantFP::get(FltTy, 1.0);
663 Value *R = ConstantFP::get(FltTy, 2.0);
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000664 Value *MatchL, *MatchR;
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000665
666 // Test UGT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000667 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
668 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000669 EXPECT_EQ(L, MatchL);
670 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000671
672 // Test UGE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000673 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
674 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000675 EXPECT_EQ(L, MatchL);
676 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000677
678 // Test no match on ULE.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000679 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
680 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000681
682 // Test no match on ULT.
Chandler Carruth91f4e602014-01-05 02:23:11 +0000683 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
684 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000685
Craig Topperc6635522017-06-13 17:18:45 +0000686 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
687 // %cmp = fcmp ule L, R
688 // %max = select %cmp R, L
689 // Given L == NaN
690 // the above is expanded to %cmp == true ==> %max = R
691 // which is true for OrdFMax, not UnordFMax, so test that:
692
693 // [UO]LE with inverted select.
694 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000695 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000696 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
697 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000698 EXPECT_EQ(L, MatchL);
699 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000700
Craig Topperc6635522017-06-13 17:18:45 +0000701 // [UO]LT with inverted select.
702 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
Chandler Carruth91f4e602014-01-05 02:23:11 +0000703 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
Craig Topperc6635522017-06-13 17:18:45 +0000704 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
705 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
Chandler Carruth4603e96a2014-01-05 02:07:20 +0000706 EXPECT_EQ(L, MatchL);
707 EXPECT_EQ(R, MatchR);
Arnold Schwaighofere972d032013-05-05 01:54:46 +0000708}
709
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000710TEST_F(PatternMatchTest, OverflowingBinOps) {
711 Value *L = IRB.getInt32(1);
712 Value *R = IRB.getInt32(2);
713 Value *MatchL, *MatchR;
714
715 EXPECT_TRUE(
716 m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
717 EXPECT_EQ(L, MatchL);
718 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000719 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000720 EXPECT_TRUE(
721 m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
722 EXPECT_EQ(L, MatchL);
723 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000724 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000725 EXPECT_TRUE(
726 m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
727 EXPECT_EQ(L, MatchL);
728 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000729 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000730 EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
731 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
732 EXPECT_EQ(L, MatchL);
733 EXPECT_EQ(R, MatchR);
734
735 EXPECT_TRUE(
736 m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
737 EXPECT_EQ(L, MatchL);
738 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000739 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000740 EXPECT_TRUE(
741 m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
742 EXPECT_EQ(L, MatchL);
743 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000744 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000745 EXPECT_TRUE(
746 m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
747 EXPECT_EQ(L, MatchL);
748 EXPECT_EQ(R, MatchR);
Craig Topper66f09ad2014-06-08 22:29:17 +0000749 MatchL = MatchR = nullptr;
Chandler Carruthc77d50a2014-01-05 03:28:29 +0000750 EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
751 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
752 EXPECT_EQ(L, MatchL);
753 EXPECT_EQ(R, MatchR);
754
755 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
756 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
757 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
758 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
759 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
760 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
761 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
762 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
763 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
764 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
765 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
766 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
767 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
768
769 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
770 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
771 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
772 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
773 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
774 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
775 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
776 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
777 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
778 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
779 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
780 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
781 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
782}
783
Sjoerd Meijerc6079012018-06-20 07:27:45 +0000784TEST_F(PatternMatchTest, LoadStoreOps) {
785 // Create this load/store sequence:
786 //
787 // %p = alloca i32*
788 // %0 = load i32*, i32** %p
789 // store i32 42, i32* %0
790
791 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
James Y Knight14359ef2019-02-01 20:44:24 +0000792 Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
Sjoerd Meijerc6079012018-06-20 07:27:45 +0000793 Value *FourtyTwo = IRB.getInt32(42);
794 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
795 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
796
797 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
798 EXPECT_EQ(Alloca, MatchLoad);
799
800 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
801
802 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
803
804 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
805 .match(StoreInst));
806 EXPECT_EQ(FourtyTwo, MatchStoreVal);
807 EXPECT_EQ(Alloca, MatchStorePointer);
808
809 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
810 .match(Alloca));
811
812 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
813 .match(StoreInst));
814 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
815 .match(StoreInst));
816 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
817 .match(StoreInst));
818}
819
Daniel Neilson45796f62018-03-28 15:39:00 +0000820TEST_F(PatternMatchTest, VectorOps) {
821 // Build up small tree of vector operations
822 //
823 // Val = 0 + 1
824 // Val2 = Val + 3
825 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
826 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
827 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
828 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
829 //
830 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
831 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
832 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
833 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
834 //
835 // SP1 = VectorSplat(2, i8 2)
836 // SP2 = VectorSplat(2, i8 %Val)
837 Type *VecTy = VectorType::get(IRB.getInt8Ty(), 2);
838 Type *i32 = IRB.getInt32Ty();
839 Type *i32VecTy = VectorType::get(i32, 2);
840
841 Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
842 Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
843
844 SmallVector<Constant *, 2> VecElemIdxs;
845 VecElemIdxs.push_back(ConstantInt::get(i32, 0));
846 VecElemIdxs.push_back(ConstantInt::get(i32, 2));
847 auto *IdxVec = ConstantVector::get(VecElemIdxs);
848
849 Value *UndefVec = UndefValue::get(VecTy);
850 Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
851 Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
852 Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
853 Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
854
855 Value *EX1 = IRB.CreateExtractElement(VI4, Val);
856 Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
857 Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
858
859 Value *Zero = ConstantAggregateZero::get(i32VecTy);
860 Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero);
861 Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
862 Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero);
863 Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero);
864
865 Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
866 Value *SP2 = IRB.CreateVectorSplat(2, Val);
867
868 Value *A = nullptr, *B = nullptr, *C = nullptr;
869
870 // Test matching insertelement
871 EXPECT_TRUE(match(VI1, m_InsertElement(m_Value(), m_Value(), m_Value())));
872 EXPECT_TRUE(
873 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_ConstantInt())));
874 EXPECT_TRUE(
875 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_Zero())));
876 EXPECT_TRUE(
877 match(VI1, m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero())));
878 EXPECT_TRUE(match(VI2, m_InsertElement(m_Value(), m_Value(), m_Value())));
879 EXPECT_FALSE(
880 match(VI2, m_InsertElement(m_Value(), m_Value(), m_ConstantInt())));
881 EXPECT_FALSE(
882 match(VI2, m_InsertElement(m_Value(), m_ConstantInt(), m_Value())));
883 EXPECT_FALSE(match(VI2, m_InsertElement(m_Constant(), m_Value(), m_Value())));
884 EXPECT_TRUE(match(VI3, m_InsertElement(m_Value(A), m_Value(B), m_Value(C))));
885 EXPECT_TRUE(A == VI1);
886 EXPECT_TRUE(B == Val2);
887 EXPECT_TRUE(isa<ConstantInt>(C));
888 A = B = C = nullptr; // reset
889
890 // Test matching extractelement
891 EXPECT_TRUE(match(EX1, m_ExtractElement(m_Value(A), m_Value(B))));
892 EXPECT_TRUE(A == VI4);
893 EXPECT_TRUE(B == Val);
894 A = B = C = nullptr; // reset
895 EXPECT_FALSE(match(EX1, m_ExtractElement(m_Value(), m_ConstantInt())));
896 EXPECT_TRUE(match(EX2, m_ExtractElement(m_Value(), m_ConstantInt())));
897 EXPECT_TRUE(match(EX3, m_ExtractElement(m_Constant(), m_ConstantInt())));
898
899 // Test matching shufflevector
900 EXPECT_TRUE(match(SI1, m_ShuffleVector(m_Value(), m_Undef(), m_Zero())));
901 EXPECT_TRUE(match(SI2, m_ShuffleVector(m_Value(A), m_Value(B), m_Value(C))));
902 EXPECT_TRUE(A == VI3);
903 EXPECT_TRUE(B == VI4);
904 EXPECT_TRUE(C == IdxVec);
905 A = B = C = nullptr; // reset
906
907 // Test matching the vector splat pattern
908 EXPECT_TRUE(match(
909 SI1,
910 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()),
911 m_Undef(), m_Zero())));
912 EXPECT_FALSE(match(
913 SI3, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
914 m_Undef(), m_Zero())));
915 EXPECT_FALSE(match(
916 SI4, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
917 m_Undef(), m_Zero())));
918 EXPECT_TRUE(match(
919 SP1,
920 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(2), m_Zero()),
921 m_Undef(), m_Zero())));
922 EXPECT_TRUE(match(
923 SP2, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(A), m_Zero()),
924 m_Undef(), m_Zero())));
925 EXPECT_TRUE(A == Val);
926}
927
Sanjay Patelf5ead292018-11-20 16:08:19 +0000928TEST_F(PatternMatchTest, VectorUndefInt) {
929 Type *ScalarTy = IRB.getInt8Ty();
930 Type *VectorTy = VectorType::get(ScalarTy, 4);
931 Constant *ScalarUndef = UndefValue::get(ScalarTy);
932 Constant *VectorUndef = UndefValue::get(VectorTy);
933 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
934 Constant *VectorZero = Constant::getNullValue(VectorTy);
935
936 SmallVector<Constant *, 4> Elems;
937 Elems.push_back(ScalarUndef);
938 Elems.push_back(ScalarZero);
939 Elems.push_back(ScalarUndef);
940 Elems.push_back(ScalarZero);
941 Constant *VectorZeroUndef = ConstantVector::get(Elems);
942
943 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
944 EXPECT_TRUE(match(VectorUndef, m_Undef()));
945 EXPECT_FALSE(match(ScalarZero, m_Undef()));
946 EXPECT_FALSE(match(VectorZero, m_Undef()));
947 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
948
949 EXPECT_FALSE(match(ScalarUndef, m_Zero()));
950 EXPECT_FALSE(match(VectorUndef, m_Zero()));
951 EXPECT_TRUE(match(ScalarZero, m_Zero()));
952 EXPECT_TRUE(match(VectorZero, m_Zero()));
953 EXPECT_TRUE(match(VectorZeroUndef, m_Zero()));
954}
955
956TEST_F(PatternMatchTest, VectorUndefFloat) {
957 Type *ScalarTy = IRB.getFloatTy();
958 Type *VectorTy = VectorType::get(ScalarTy, 4);
959 Constant *ScalarUndef = UndefValue::get(ScalarTy);
960 Constant *VectorUndef = UndefValue::get(VectorTy);
961 Constant *ScalarZero = Constant::getNullValue(ScalarTy);
962 Constant *VectorZero = Constant::getNullValue(VectorTy);
963
964 SmallVector<Constant *, 4> Elems;
965 Elems.push_back(ScalarUndef);
966 Elems.push_back(ScalarZero);
967 Elems.push_back(ScalarUndef);
968 Elems.push_back(ScalarZero);
969 Constant *VectorZeroUndef = ConstantVector::get(Elems);
970
971 EXPECT_TRUE(match(ScalarUndef, m_Undef()));
972 EXPECT_TRUE(match(VectorUndef, m_Undef()));
973 EXPECT_FALSE(match(ScalarZero, m_Undef()));
974 EXPECT_FALSE(match(VectorZero, m_Undef()));
975 EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
976
977 EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
978 EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
979 EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
980 EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
981 EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
982}
983
Cameron McInallybe7138b2019-05-03 21:19:12 +0000984TEST_F(PatternMatchTest, FloatingPointFNeg) {
985 Type *FltTy = IRB.getFloatTy();
986 Value *One = ConstantFP::get(FltTy, 1.0);
987 Value *Z = ConstantFP::get(FltTy, 0.0);
988 Value *NZ = ConstantFP::get(FltTy, -0.0);
989 Value *V = IRB.CreateFNeg(One);
990 Value *V1 = IRB.CreateFSub(NZ, One);
991 Value *V2 = IRB.CreateFSub(Z, One);
992 Value *V3 = IRB.CreateFAdd(NZ, One);
993 Value *Match;
994
995 // Test FNeg(1.0)
996 EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
997 EXPECT_EQ(One, Match);
998
999 // Test FSub(-0.0, 1.0)
1000 EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1001 EXPECT_EQ(One, Match);
1002
1003 // Test FSub(0.0, 1.0)
1004 EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1005 cast<Instruction>(V2)->setHasNoSignedZeros(true);
1006 EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1007 EXPECT_EQ(One, Match);
1008
1009 // Test FAdd(-0.0, 1.0)
1010 EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1011}
1012
Dmitry Venikovcb238de2019-07-15 14:47:45 +00001013TEST_F(PatternMatchTest, LibFunc) {
1014 Type *FltTy = IRB.getFloatTy();
1015 Value *One = ConstantFP::get(FltTy, 1.0);
1016 Value *Two = ConstantFP::get(FltTy, 2.0);
1017 Value *MatchOne, *MatchTwo;
1018
1019 StringRef TanName = TLI.getName(LibFunc_tan);
1020 FunctionCallee TanCallee = M->getOrInsertFunction(TanName, FltTy, FltTy);
1021 CallInst *Tan = IRB.CreateCall(TanCallee, One, TanName);
1022
1023 StringRef PowName = TLI.getName(LibFunc_pow);
1024 FunctionCallee PowCallee = M->getOrInsertFunction(PowName, FltTy, FltTy, FltTy);
1025 CallInst *Pow = IRB.CreateCall(PowCallee, {One, Two}, PowName);
1026
1027 EXPECT_TRUE(match(Tan, m_LibFunc<LibFunc_tan>(TLI)));
1028 EXPECT_FALSE(match(Tan, m_LibFunc<LibFunc_pow>(TLI)));
1029 EXPECT_FALSE(match(Pow, m_LibFunc<LibFunc_tan>(TLI)));
1030
1031 EXPECT_TRUE(match(Tan, m_LibFunc<LibFunc_tan>(TLI, m_Value(MatchOne))));
1032 EXPECT_EQ(One, MatchOne);
1033 EXPECT_FALSE(match(Tan, m_LibFunc<LibFunc_sin>(TLI, m_Value())));
1034
1035 EXPECT_TRUE(match(Pow, m_LibFunc<LibFunc_pow>(TLI, m_Value(MatchOne),
1036 m_Value(MatchTwo))));
1037 EXPECT_EQ(One, MatchOne);
1038 EXPECT_EQ(Two, MatchTwo);
1039 EXPECT_FALSE(match(Pow, m_LibFunc<LibFunc_fminf>(TLI, m_Value(), m_Value())));
1040
1041 TLII.disableAllFunctions();
1042 EXPECT_FALSE(match(Tan, m_LibFunc<LibFunc_tan>(TLI)));
1043 EXPECT_FALSE(match(Tan, m_LibFunc<LibFunc_tan>(TLI, m_Value())));
1044 EXPECT_FALSE(match(Pow, m_LibFunc<LibFunc_pow>(TLI, m_Value(), m_Value())));
1045}
1046
Pete Cooperab47fa62016-08-12 22:16:05 +00001047template <typename T> struct MutableConstTest : PatternMatchTest { };
1048
1049typedef ::testing::Types<std::tuple<Value*, Instruction*>,
1050 std::tuple<const Value*, const Instruction *>>
1051 MutableConstTestTypes;
1052TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
1053
1054TYPED_TEST(MutableConstTest, ICmp) {
1055 auto &IRB = PatternMatchTest::IRB;
1056
1057 typedef typename std::tuple_element<0, TypeParam>::type ValueType;
1058 typedef typename std::tuple_element<1, TypeParam>::type InstructionType;
1059
1060 Value *L = IRB.getInt32(1);
1061 Value *R = IRB.getInt32(2);
1062 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
1063
1064 ValueType MatchL;
1065 ValueType MatchR;
1066 ICmpInst::Predicate MatchPred;
1067
1068 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
1069 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
1070 EXPECT_EQ(L, MatchL);
1071 EXPECT_EQ(R, MatchR);
1072}
1073
Arnold Schwaighofere972d032013-05-05 01:54:46 +00001074} // anonymous namespace.