llvm-project

Форк
0
/
PatternMatch.cpp 
2294 строки · 87.6 Кб
1
//===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8

9
#include "llvm/IR/PatternMatch.h"
10
#include "llvm/ADT/APSInt.h"
11
#include "llvm/ADT/STLExtras.h"
12
#include "llvm/Analysis/ValueTracking.h"
13
#include "llvm/IR/BasicBlock.h"
14
#include "llvm/IR/Constants.h"
15
#include "llvm/IR/DataLayout.h"
16
#include "llvm/IR/DerivedTypes.h"
17
#include "llvm/IR/Function.h"
18
#include "llvm/IR/IRBuilder.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/LLVMContext.h"
21
#include "llvm/IR/MDBuilder.h"
22
#include "llvm/IR/Module.h"
23
#include "llvm/IR/NoFolder.h"
24
#include "llvm/IR/Operator.h"
25
#include "llvm/IR/Type.h"
26
#include "gtest/gtest.h"
27

28
using namespace llvm;
29
using namespace llvm::PatternMatch;
30

31
namespace {
32

33
struct PatternMatchTest : ::testing::Test {
34
  LLVMContext Ctx;
35
  std::unique_ptr<Module> M;
36
  Function *F;
37
  BasicBlock *BB;
38
  IRBuilder<NoFolder> IRB;
39

40
  PatternMatchTest()
41
      : M(new Module("PatternMatchTestModule", Ctx)),
42
        F(Function::Create(
43
            FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
44
            Function::ExternalLinkage, "f", M.get())),
45
        BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
46
};
47

48
TEST_F(PatternMatchTest, OneUse) {
49
  // Build up a little tree of values:
50
  //
51
  //   One  = (1 + 2) + 42
52
  //   Two  = One + 42
53
  //   Leaf = (Two + 8) + (Two + 13)
54
  Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)),
55
                             IRB.getInt32(42));
56
  Value *Two = IRB.CreateAdd(One, IRB.getInt32(42));
57
  Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)),
58
                              IRB.CreateAdd(Two, IRB.getInt32(13)));
59
  Value *V;
60

61
  EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
62
  EXPECT_EQ(One, V);
63

64
  EXPECT_FALSE(m_OneUse(m_Value()).match(Two));
65
  EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
66
}
67

68
TEST_F(PatternMatchTest, SpecificIntEQ) {
69
  Type *IntTy = IRB.getInt32Ty();
70
  unsigned BitWidth = IntTy->getScalarSizeInBits();
71

72
  Value *Zero = ConstantInt::get(IntTy, 0);
73
  Value *One = ConstantInt::get(IntTy, 1);
74
  Value *NegOne = ConstantInt::get(IntTy, -1);
75

76
  EXPECT_TRUE(
77
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
78
          .match(Zero));
79
  EXPECT_FALSE(
80
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
81
          .match(One));
82
  EXPECT_FALSE(
83
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0))
84
          .match(NegOne));
85

86
  EXPECT_FALSE(
87
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
88
          .match(Zero));
89
  EXPECT_TRUE(
90
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
91
          .match(One));
92
  EXPECT_FALSE(
93
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1))
94
          .match(NegOne));
95

96
  EXPECT_FALSE(
97
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
98
          .match(Zero));
99
  EXPECT_FALSE(
100
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
101
          .match(One));
102
  EXPECT_TRUE(
103
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1))
104
          .match(NegOne));
105
}
106

107
TEST_F(PatternMatchTest, SpecificIntNE) {
108
  Type *IntTy = IRB.getInt32Ty();
109
  unsigned BitWidth = IntTy->getScalarSizeInBits();
110

111
  Value *Zero = ConstantInt::get(IntTy, 0);
112
  Value *One = ConstantInt::get(IntTy, 1);
113
  Value *NegOne = ConstantInt::get(IntTy, -1);
114

115
  EXPECT_FALSE(
116
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
117
          .match(Zero));
118
  EXPECT_TRUE(
119
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
120
          .match(One));
121
  EXPECT_TRUE(
122
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0))
123
          .match(NegOne));
124

125
  EXPECT_TRUE(
126
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
127
          .match(Zero));
128
  EXPECT_FALSE(
129
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
130
          .match(One));
131
  EXPECT_TRUE(
132
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1))
133
          .match(NegOne));
134

135
  EXPECT_TRUE(
136
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
137
          .match(Zero));
138
  EXPECT_TRUE(
139
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
140
          .match(One));
141
  EXPECT_FALSE(
142
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1))
143
          .match(NegOne));
144
}
145

146
TEST_F(PatternMatchTest, SpecificIntUGT) {
147
  Type *IntTy = IRB.getInt32Ty();
148
  unsigned BitWidth = IntTy->getScalarSizeInBits();
149

150
  Value *Zero = ConstantInt::get(IntTy, 0);
151
  Value *One = ConstantInt::get(IntTy, 1);
152
  Value *NegOne = ConstantInt::get(IntTy, -1);
153

154
  EXPECT_FALSE(
155
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
156
          .match(Zero));
157
  EXPECT_TRUE(
158
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
159
          .match(One));
160
  EXPECT_TRUE(
161
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0))
162
          .match(NegOne));
163

164
  EXPECT_FALSE(
165
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
166
          .match(Zero));
167
  EXPECT_FALSE(
168
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
169
          .match(One));
170
  EXPECT_TRUE(
171
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1))
172
          .match(NegOne));
173

174
  EXPECT_FALSE(
175
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
176
          .match(Zero));
177
  EXPECT_FALSE(
178
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
179
          .match(One));
180
  EXPECT_FALSE(
181
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1))
182
          .match(NegOne));
183
}
184

185
TEST_F(PatternMatchTest, SignbitZeroChecks) {
186
  Type *IntTy = IRB.getInt32Ty();
187

188
  Value *Zero = ConstantInt::get(IntTy, 0);
189
  Value *One = ConstantInt::get(IntTy, 1);
190
  Value *NegOne = ConstantInt::get(IntTy, -1);
191

192
  EXPECT_TRUE(m_Negative().match(NegOne));
193
  EXPECT_FALSE(m_NonNegative().match(NegOne));
194
  EXPECT_FALSE(m_StrictlyPositive().match(NegOne));
195
  EXPECT_TRUE(m_NonPositive().match(NegOne));
196

197
  EXPECT_FALSE(m_Negative().match(Zero));
198
  EXPECT_TRUE(m_NonNegative().match(Zero));
199
  EXPECT_FALSE(m_StrictlyPositive().match(Zero));
200
  EXPECT_TRUE(m_NonPositive().match(Zero));
201

202
  EXPECT_FALSE(m_Negative().match(One));
203
  EXPECT_TRUE(m_NonNegative().match(One));
204
  EXPECT_TRUE(m_StrictlyPositive().match(One));
205
  EXPECT_FALSE(m_NonPositive().match(One));
206
}
207

208
TEST_F(PatternMatchTest, SpecificIntUGE) {
209
  Type *IntTy = IRB.getInt32Ty();
210
  unsigned BitWidth = IntTy->getScalarSizeInBits();
211

212
  Value *Zero = ConstantInt::get(IntTy, 0);
213
  Value *One = ConstantInt::get(IntTy, 1);
214
  Value *NegOne = ConstantInt::get(IntTy, -1);
215

216
  EXPECT_TRUE(
217
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
218
          .match(Zero));
219
  EXPECT_TRUE(
220
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
221
          .match(One));
222
  EXPECT_TRUE(
223
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0))
224
          .match(NegOne));
225

226
  EXPECT_FALSE(
227
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
228
          .match(Zero));
229
  EXPECT_TRUE(
230
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
231
          .match(One));
232
  EXPECT_TRUE(
233
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1))
234
          .match(NegOne));
235

236
  EXPECT_FALSE(
237
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
238
          .match(Zero));
239
  EXPECT_FALSE(
240
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
241
          .match(One));
242
  EXPECT_TRUE(
243
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1))
244
          .match(NegOne));
245
}
246

247
TEST_F(PatternMatchTest, SpecificIntULT) {
248
  Type *IntTy = IRB.getInt32Ty();
249
  unsigned BitWidth = IntTy->getScalarSizeInBits();
250

251
  Value *Zero = ConstantInt::get(IntTy, 0);
252
  Value *One = ConstantInt::get(IntTy, 1);
253
  Value *NegOne = ConstantInt::get(IntTy, -1);
254

255
  EXPECT_FALSE(
256
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
257
          .match(Zero));
258
  EXPECT_FALSE(
259
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
260
          .match(One));
261
  EXPECT_FALSE(
262
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0))
263
          .match(NegOne));
264

265
  EXPECT_TRUE(
266
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
267
          .match(Zero));
268
  EXPECT_FALSE(
269
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
270
          .match(One));
271
  EXPECT_FALSE(
272
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1))
273
          .match(NegOne));
274

275
  EXPECT_TRUE(
276
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
277
          .match(Zero));
278
  EXPECT_TRUE(
279
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
280
          .match(One));
281
  EXPECT_FALSE(
282
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1))
283
          .match(NegOne));
284
}
285

286
TEST_F(PatternMatchTest, SpecificIntULE) {
287
  Type *IntTy = IRB.getInt32Ty();
288
  unsigned BitWidth = IntTy->getScalarSizeInBits();
289

290
  Value *Zero = ConstantInt::get(IntTy, 0);
291
  Value *One = ConstantInt::get(IntTy, 1);
292
  Value *NegOne = ConstantInt::get(IntTy, -1);
293

294
  EXPECT_TRUE(
295
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
296
          .match(Zero));
297
  EXPECT_FALSE(
298
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
299
          .match(One));
300
  EXPECT_FALSE(
301
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0))
302
          .match(NegOne));
303

304
  EXPECT_TRUE(
305
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
306
          .match(Zero));
307
  EXPECT_TRUE(
308
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
309
          .match(One));
310
  EXPECT_FALSE(
311
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1))
312
          .match(NegOne));
313

314
  EXPECT_TRUE(
315
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
316
          .match(Zero));
317
  EXPECT_TRUE(
318
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
319
          .match(One));
320
  EXPECT_TRUE(
321
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1))
322
          .match(NegOne));
323
}
324

325
TEST_F(PatternMatchTest, SpecificIntSGT) {
326
  Type *IntTy = IRB.getInt32Ty();
327
  unsigned BitWidth = IntTy->getScalarSizeInBits();
328

329
  Value *Zero = ConstantInt::get(IntTy, 0);
330
  Value *One = ConstantInt::get(IntTy, 1);
331
  Value *NegOne = ConstantInt::get(IntTy, -1);
332

333
  EXPECT_FALSE(
334
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
335
          .match(Zero));
336
  EXPECT_TRUE(
337
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
338
          .match(One));
339
  EXPECT_FALSE(
340
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0))
341
          .match(NegOne));
342

343
  EXPECT_FALSE(
344
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
345
          .match(Zero));
346
  EXPECT_FALSE(
347
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
348
          .match(One));
349
  EXPECT_FALSE(
350
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1))
351
          .match(NegOne));
352

353
  EXPECT_TRUE(
354
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
355
          .match(Zero));
356
  EXPECT_TRUE(
357
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
358
          .match(One));
359
  EXPECT_FALSE(
360
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1))
361
          .match(NegOne));
362
}
363

364
TEST_F(PatternMatchTest, SpecificIntSGE) {
365
  Type *IntTy = IRB.getInt32Ty();
366
  unsigned BitWidth = IntTy->getScalarSizeInBits();
367

368
  Value *Zero = ConstantInt::get(IntTy, 0);
369
  Value *One = ConstantInt::get(IntTy, 1);
370
  Value *NegOne = ConstantInt::get(IntTy, -1);
371

372
  EXPECT_TRUE(
373
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
374
          .match(Zero));
375
  EXPECT_TRUE(
376
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
377
          .match(One));
378
  EXPECT_FALSE(
379
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0))
380
          .match(NegOne));
381

382
  EXPECT_FALSE(
383
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
384
          .match(Zero));
385
  EXPECT_TRUE(
386
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
387
          .match(One));
388
  EXPECT_FALSE(
389
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1))
390
          .match(NegOne));
391

392
  EXPECT_TRUE(
393
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
394
          .match(Zero));
395
  EXPECT_TRUE(
396
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
397
          .match(One));
398
  EXPECT_TRUE(
399
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1))
400
          .match(NegOne));
401
}
402

403
TEST_F(PatternMatchTest, SpecificIntSLT) {
404
  Type *IntTy = IRB.getInt32Ty();
405
  unsigned BitWidth = IntTy->getScalarSizeInBits();
406

407
  Value *Zero = ConstantInt::get(IntTy, 0);
408
  Value *One = ConstantInt::get(IntTy, 1);
409
  Value *NegOne = ConstantInt::get(IntTy, -1);
410

411
  EXPECT_FALSE(
412
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
413
          .match(Zero));
414
  EXPECT_FALSE(
415
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
416
          .match(One));
417
  EXPECT_TRUE(
418
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0))
419
          .match(NegOne));
420

421
  EXPECT_TRUE(
422
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
423
          .match(Zero));
424
  EXPECT_FALSE(
425
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
426
          .match(One));
427
  EXPECT_TRUE(
428
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1))
429
          .match(NegOne));
430

431
  EXPECT_FALSE(
432
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
433
          .match(Zero));
434
  EXPECT_FALSE(
435
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
436
          .match(One));
437
  EXPECT_FALSE(
438
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1))
439
          .match(NegOne));
440
}
441

442
TEST_F(PatternMatchTest, SpecificIntSLE) {
443
  Type *IntTy = IRB.getInt32Ty();
444
  unsigned BitWidth = IntTy->getScalarSizeInBits();
445

446
  Value *Zero = ConstantInt::get(IntTy, 0);
447
  Value *One = ConstantInt::get(IntTy, 1);
448
  Value *NegOne = ConstantInt::get(IntTy, -1);
449

450
  EXPECT_TRUE(
451
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
452
          .match(Zero));
453
  EXPECT_FALSE(
454
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
455
          .match(One));
456
  EXPECT_TRUE(
457
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0))
458
          .match(NegOne));
459

460
  EXPECT_TRUE(
461
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
462
          .match(Zero));
463
  EXPECT_TRUE(
464
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
465
          .match(One));
466
  EXPECT_TRUE(
467
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1))
468
          .match(NegOne));
469

470
  EXPECT_FALSE(
471
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
472
          .match(Zero));
473
  EXPECT_FALSE(
474
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
475
          .match(One));
476
  EXPECT_TRUE(
477
      m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1))
478
          .match(NegOne));
479
}
480

481
TEST_F(PatternMatchTest, Unless) {
482
  Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
483

484
  EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X));
485
  EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X));
486

487
  EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X));
488
  EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X));
489

490
  EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X));
491
  EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X));
492

493
  EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X));
494
  EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
495
}
496

497
TEST_F(PatternMatchTest, BitWise) {
498
  Value *Or = IRB.CreateOr(IRB.getInt32(1), IRB.getInt32(0));
499
  Value *Xor = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
500
  Value *And = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
501
  Constant *T = IRB.getInt1(true);
502
  Constant *F = IRB.getInt1(false);
503
  Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
504
  Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
505
  Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
506
  Value *LAnd = IRB.CreateSelect(X, Y, F);
507
  Value *LOr = IRB.CreateSelect(X, T, Y);
508
  Value *Add = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
509

510
  EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Or));
511
  EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Xor));
512
  EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(And));
513
  EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
514
  EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LOr));
515
  EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(Add));
516

517
  EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Or));
518
  EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Xor));
519
  EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(And));
520

521
  EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Or));
522
  EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Xor));
523
  EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(And));
524
  EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
525
  EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LOr));
526
  EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(Add));
527

528
  EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Or));
529
  EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Xor));
530
  EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(And));
531

532
  EXPECT_FALSE(m_c_BitwiseLogic(m_One(), m_One()).match(Or));
533
  EXPECT_FALSE(m_c_BitwiseLogic(m_Zero(), m_Zero()).match(Xor));
534
}
535

536
TEST_F(PatternMatchTest, ZExtSExtSelf) {
537
  LLVMContext &Ctx = IRB.getContext();
538

539
  Value *One32 = IRB.getInt32(1);
540
  Value *One64Z = IRB.CreateZExt(One32, IntegerType::getInt64Ty(Ctx));
541
  Value *One64S = IRB.CreateSExt(One32, IntegerType::getInt64Ty(Ctx));
542

543
  EXPECT_TRUE(m_One().match(One32));
544
  EXPECT_FALSE(m_One().match(One64Z));
545
  EXPECT_FALSE(m_One().match(One64S));
546

547
  EXPECT_FALSE(m_ZExt(m_One()).match(One32));
548
  EXPECT_TRUE(m_ZExt(m_One()).match(One64Z));
549
  EXPECT_FALSE(m_ZExt(m_One()).match(One64S));
550

551
  EXPECT_FALSE(m_SExt(m_One()).match(One32));
552
  EXPECT_FALSE(m_SExt(m_One()).match(One64Z));
553
  EXPECT_TRUE(m_SExt(m_One()).match(One64S));
554

555
  EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One32));
556
  EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One64Z));
557
  EXPECT_FALSE(m_ZExtOrSelf(m_One()).match(One64S));
558

559
  EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One32));
560
  EXPECT_FALSE(m_SExtOrSelf(m_One()).match(One64Z));
561
  EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One64S));
562

563
  EXPECT_FALSE(m_ZExtOrSExt(m_One()).match(One32));
564
  EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64Z));
565
  EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64S));
566

567
  EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One32));
568
  EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64Z));
569
  EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64S));
570
}
571

572
TEST_F(PatternMatchTest, BitCast) {
573
  Value *OneDouble = ConstantFP::get(IRB.getDoubleTy(), APFloat(1.0));
574
  Value *ScalableDouble = ConstantFP::get(
575
      VectorType::get(IRB.getDoubleTy(), 2, /*Scalable=*/true), APFloat(1.0));
576
  // scalar -> scalar
577
  Value *DoubleToI64 = IRB.CreateBitCast(OneDouble, IRB.getInt64Ty());
578
  // scalar -> vector
579
  Value *DoubleToV2I32 = IRB.CreateBitCast(
580
      OneDouble, VectorType::get(IRB.getInt32Ty(), 2, /*Scalable=*/false));
581
  // vector -> scalar
582
  Value *V2I32ToDouble = IRB.CreateBitCast(DoubleToV2I32, IRB.getDoubleTy());
583
  // vector -> vector (same count)
584
  Value *V2I32ToV2Float = IRB.CreateBitCast(
585
      DoubleToV2I32, VectorType::get(IRB.getFloatTy(), 2, /*Scalable=*/false));
586
  // vector -> vector (different count)
587
  Value *V2I32TOV4I16 = IRB.CreateBitCast(
588
      DoubleToV2I32, VectorType::get(IRB.getInt16Ty(), 4, /*Scalable=*/false));
589
  // scalable vector -> scalable vector (same count)
590
  Value *NXV2DoubleToNXV2I64 = IRB.CreateBitCast(
591
      ScalableDouble, VectorType::get(IRB.getInt64Ty(), 2, /*Scalable=*/true));
592
  // scalable vector -> scalable vector (different count)
593
  Value *NXV2I64ToNXV4I32 = IRB.CreateBitCast(
594
      NXV2DoubleToNXV2I64,
595
      VectorType::get(IRB.getInt32Ty(), 4, /*Scalable=*/true));
596

597
  EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToI64));
598
  EXPECT_TRUE(m_BitCast(m_Value()).match(DoubleToV2I32));
599
  EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToDouble));
600
  EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32ToV2Float));
601
  EXPECT_TRUE(m_BitCast(m_Value()).match(V2I32TOV4I16));
602
  EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2DoubleToNXV2I64));
603
  EXPECT_TRUE(m_BitCast(m_Value()).match(NXV2I64ToNXV4I32));
604

605
  EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(DoubleToI64));
606
  EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(DoubleToV2I32));
607
  EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32ToDouble));
608
  EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(V2I32ToV2Float));
609
  EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(V2I32TOV4I16));
610
  EXPECT_TRUE(m_ElementWiseBitCast(m_Value()).match(NXV2DoubleToNXV2I64));
611
  EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(NXV2I64ToNXV4I32));
612
}
613

614
TEST_F(PatternMatchTest, CheckedInt) {
615
  Type *I8Ty = IRB.getInt8Ty();
616
  const Constant * CRes = nullptr;
617
  auto CheckUgt1 = [](const APInt &C) { return C.ugt(1); };
618
  auto CheckTrue = [](const APInt &) { return true; };
619
  auto CheckFalse = [](const APInt &) { return false; };
620
  auto CheckNonZero = [](const APInt &C) { return !C.isZero(); };
621
  auto CheckPow2 = [](const APInt &C) { return C.isPowerOf2(); };
622

623
  auto DoScalarCheck = [&](int8_t Val) {
624
    APInt APVal(8, Val);
625
    Constant *C = ConstantInt::get(I8Ty, Val);
626

627
    CRes = nullptr;
628
    EXPECT_TRUE(m_CheckedInt(CheckTrue).match(C));
629
    EXPECT_TRUE(m_CheckedInt(CRes, CheckTrue).match(C));
630
    EXPECT_EQ(CRes, C);
631

632
    CRes = nullptr;
633
    EXPECT_FALSE(m_CheckedInt(CheckFalse).match(C));
634
    EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(C));
635
    EXPECT_EQ(CRes, nullptr);
636

637
    CRes = nullptr;
638
    EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CheckUgt1).match(C));
639
    EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CRes, CheckUgt1).match(C));
640
    if (CheckUgt1(APVal))
641
      EXPECT_EQ(CRes, C);
642

643
    CRes = nullptr;
644
    EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CheckNonZero).match(C));
645
    EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CRes, CheckNonZero).match(C));
646
    if (CheckNonZero(APVal))
647
      EXPECT_EQ(CRes, C);
648

649
    CRes = nullptr;
650
    EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CheckPow2).match(C));
651
    EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CRes, CheckPow2).match(C));
652
    if (CheckPow2(APVal))
653
      EXPECT_EQ(CRes, C);
654

655
  };
656

657
  DoScalarCheck(0);
658
  DoScalarCheck(1);
659
  DoScalarCheck(2);
660
  DoScalarCheck(3);
661

662
  EXPECT_FALSE(m_CheckedInt(CheckTrue).match(UndefValue::get(I8Ty)));
663
  EXPECT_FALSE(m_CheckedInt(CRes, CheckTrue).match(UndefValue::get(I8Ty)));
664
  EXPECT_EQ(CRes, nullptr);
665

666
  EXPECT_FALSE(m_CheckedInt(CheckFalse).match(UndefValue::get(I8Ty)));
667
  EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(UndefValue::get(I8Ty)));
668
  EXPECT_EQ(CRes, nullptr);
669

670
  EXPECT_FALSE(m_CheckedInt(CheckTrue).match(PoisonValue::get(I8Ty)));
671
  EXPECT_FALSE(m_CheckedInt(CRes, CheckTrue).match(PoisonValue::get(I8Ty)));
672
  EXPECT_EQ(CRes, nullptr);
673

674
  EXPECT_FALSE(m_CheckedInt(CheckFalse).match(PoisonValue::get(I8Ty)));
675
  EXPECT_FALSE(m_CheckedInt(CRes, CheckFalse).match(PoisonValue::get(I8Ty)));
676
  EXPECT_EQ(CRes, nullptr);
677

678
  auto DoVecCheckImpl = [&](ArrayRef<std::optional<int8_t>> Vals,
679
                            function_ref<bool(const APInt &)> CheckFn,
680
                            bool UndefAsPoison) {
681
    SmallVector<Constant *> VecElems;
682
    std::optional<bool> Okay;
683
    bool AllSame = true;
684
    bool HasUndef = false;
685
    std::optional<APInt> First;
686
    for (const std::optional<int8_t> &Val : Vals) {
687
      if (!Val.has_value()) {
688
        VecElems.push_back(UndefAsPoison ? PoisonValue::get(I8Ty)
689
                                         : UndefValue::get(I8Ty));
690
        HasUndef = true;
691
      } else {
692
        if (!Okay.has_value())
693
          Okay = true;
694
        APInt APVal(8, *Val);
695
        if (!First.has_value())
696
          First = APVal;
697
        else
698
          AllSame &= First->eq(APVal);
699
        Okay = *Okay && CheckFn(APVal);
700
        VecElems.push_back(ConstantInt::get(I8Ty, *Val));
701
      }
702
    }
703

704
    Constant *C = ConstantVector::get(VecElems);
705
    EXPECT_EQ(!(HasUndef && !UndefAsPoison) && Okay.value_or(false),
706
              m_CheckedInt(CheckFn).match(C));
707

708
    CRes = nullptr;
709
    bool Expec = !(HasUndef && !UndefAsPoison) && Okay.value_or(false);
710
    EXPECT_EQ(Expec, m_CheckedInt(CRes, CheckFn).match(C));
711
    if (Expec) {
712
      EXPECT_NE(CRes, nullptr);
713
      if (AllSame)
714
        EXPECT_EQ(CRes, C);
715
    }
716
  };
717
  auto DoVecCheck = [&](ArrayRef<std::optional<int8_t>> Vals) {
718
    DoVecCheckImpl(Vals, CheckTrue, /*UndefAsPoison=*/false);
719
    DoVecCheckImpl(Vals, CheckFalse, /*UndefAsPoison=*/false);
720
    DoVecCheckImpl(Vals, CheckTrue, /*UndefAsPoison=*/true);
721
    DoVecCheckImpl(Vals, CheckFalse, /*UndefAsPoison=*/true);
722
    DoVecCheckImpl(Vals, CheckUgt1, /*UndefAsPoison=*/false);
723
    DoVecCheckImpl(Vals, CheckNonZero, /*UndefAsPoison=*/false);
724
    DoVecCheckImpl(Vals, CheckPow2, /*UndefAsPoison=*/false);
725
  };
726

727
  DoVecCheck({0, 1});
728
  DoVecCheck({1, 1});
729
  DoVecCheck({1, 2});
730
  DoVecCheck({1, std::nullopt});
731
  DoVecCheck({1, std::nullopt, 1});
732
  DoVecCheck({1, std::nullopt, 2});
733
  DoVecCheck({std::nullopt, std::nullopt, std::nullopt});
734
}
735

736
TEST_F(PatternMatchTest, Power2) {
737
  Value *C128 = IRB.getInt32(128);
738
  Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
739

740
  EXPECT_TRUE(m_Power2().match(C128));
741
  EXPECT_FALSE(m_Power2().match(CNeg128));
742

743
  EXPECT_TRUE(m_Power2OrZero().match(C128));
744
  EXPECT_FALSE(m_Power2OrZero().match(CNeg128));
745

746
  EXPECT_FALSE(m_NegatedPower2().match(C128));
747
  EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
748

749
  EXPECT_FALSE(m_NegatedPower2OrZero().match(C128));
750
  EXPECT_TRUE(m_NegatedPower2OrZero().match(CNeg128));
751

752
  Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
753
  Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
754

755
  EXPECT_TRUE(m_Power2().match(CIntMin));
756
  EXPECT_TRUE(m_Power2().match(CNegIntMin));
757

758
  EXPECT_TRUE(m_Power2OrZero().match(CIntMin));
759
  EXPECT_TRUE(m_Power2OrZero().match(CNegIntMin));
760

761
  EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
762
  EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
763

764
  EXPECT_TRUE(m_NegatedPower2OrZero().match(CIntMin));
765
  EXPECT_TRUE(m_NegatedPower2OrZero().match(CNegIntMin));
766

767
  Value *CZero = IRB.getInt64(0);
768

769
  EXPECT_FALSE(m_Power2().match(CZero));
770

771
  EXPECT_TRUE(m_Power2OrZero().match(CZero));
772

773
  EXPECT_FALSE(m_NegatedPower2().match(CZero));
774

775
  EXPECT_TRUE(m_NegatedPower2OrZero().match(CZero));
776
}
777

778
TEST_F(PatternMatchTest, Not) {
779
  Value *C1 = IRB.getInt32(1);
780
  Value *C2 = IRB.getInt32(2);
781
  Value *C3 = IRB.getInt32(3);
782
  Instruction *Not = BinaryOperator::CreateXor(C1, C2);
783

784
  // When `m_Not` does not match the `not` itself,
785
  // it should not try to apply the inner matcher.
786
  Value *Val = C3;
787
  EXPECT_FALSE(m_Not(m_Value(Val)).match(Not));
788
  EXPECT_EQ(Val, C3);
789
  Not->deleteValue();
790
}
791

792
TEST_F(PatternMatchTest, CommutativeDeferredValue) {
793
  Value *X = IRB.getInt32(1);
794
  Value *Y = IRB.getInt32(2);
795

796
  {
797
    Value *tX = X;
798
    EXPECT_TRUE(match(X, m_Deferred(tX)));
799
    EXPECT_FALSE(match(Y, m_Deferred(tX)));
800
  }
801
  {
802
    const Value *tX = X;
803
    EXPECT_TRUE(match(X, m_Deferred(tX)));
804
    EXPECT_FALSE(match(Y, m_Deferred(tX)));
805
  }
806
  {
807
    Value *const tX = X;
808
    EXPECT_TRUE(match(X, m_Deferred(tX)));
809
    EXPECT_FALSE(match(Y, m_Deferred(tX)));
810
  }
811
  {
812
    const Value *const tX = X;
813
    EXPECT_TRUE(match(X, m_Deferred(tX)));
814
    EXPECT_FALSE(match(Y, m_Deferred(tX)));
815
  }
816

817
  {
818
    Value *tX = nullptr;
819
    EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
820
    EXPECT_EQ(tX, X);
821
  }
822
  {
823
    Value *tX = nullptr;
824
    EXPECT_FALSE(
825
        match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
826
  }
827

828
  auto checkMatch = [X, Y](Value *Pattern) {
829
    Value *tX = nullptr, *tY = nullptr;
830
    EXPECT_TRUE(match(
831
        Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
832
    EXPECT_EQ(tX, X);
833
    EXPECT_EQ(tY, Y);
834
  };
835

836
  checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
837
  checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
838
  checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
839
  checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
840
}
841

842
TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
843
  Type *FltTy = IRB.getFloatTy();
844
  Value *L = ConstantFP::get(FltTy, 1.0);
845
  Value *R = ConstantFP::get(FltTy, 2.0);
846
  Value *MatchL, *MatchR;
847

848
  // Test OLT.
849
  EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
850
                  .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
851
  EXPECT_EQ(L, MatchL);
852
  EXPECT_EQ(R, MatchR);
853

854
  // Test OLE.
855
  EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
856
                  .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
857
  EXPECT_EQ(L, MatchL);
858
  EXPECT_EQ(R, MatchR);
859

860
  // Test no match on OGE.
861
  EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
862
                   .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
863

864
  // Test no match on OGT.
865
  EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
866
                   .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
867

868
  // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
869
  // %cmp = fcmp oge L, R
870
  // %min = select %cmp R, L
871
  // Given L == NaN
872
  // the above is expanded to %cmp == false ==> %min = L
873
  // which is true for UnordFMin, not OrdFMin, so test that:
874

875
  // [OU]GE with inverted select.
876
  EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
877
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
878
  EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
879
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
880
  EXPECT_EQ(L, MatchL);
881
  EXPECT_EQ(R, MatchR);
882

883
  // [OU]GT with inverted select.
884
  EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
885
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
886
  EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
887
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
888
  EXPECT_EQ(L, MatchL);
889
  EXPECT_EQ(R, MatchR);
890
}
891

892
TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
893
  Type *FltTy = IRB.getFloatTy();
894
  Value *L = ConstantFP::get(FltTy, 1.0);
895
  Value *R = ConstantFP::get(FltTy, 2.0);
896
  Value *MatchL, *MatchR;
897

898
  // Test OGT.
899
  EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
900
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
901
  EXPECT_EQ(L, MatchL);
902
  EXPECT_EQ(R, MatchR);
903

904
  // Test OGE.
905
  EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
906
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
907
  EXPECT_EQ(L, MatchL);
908
  EXPECT_EQ(R, MatchR);
909

910
  // Test no match on OLE.
911
  EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
912
                   .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
913

914
  // Test no match on OLT.
915
  EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
916
                   .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
917

918

919
  // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
920
  // %cmp = fcmp ole L, R
921
  // %max = select %cmp, R, L
922
  // Given L == NaN,
923
  // the above is expanded to %cmp == false ==> %max == L
924
  // which is true for UnordFMax, not OrdFMax, so test that:
925

926
  // [OU]LE with inverted select.
927
  EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
928
                   .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
929
  EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
930
                  .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
931
  EXPECT_EQ(L, MatchL);
932
  EXPECT_EQ(R, MatchR);
933

934
  // [OUT]LT with inverted select.
935
  EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
936
                   .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
937
  EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
938
                  .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
939
  EXPECT_EQ(L, MatchL);
940
  EXPECT_EQ(R, MatchR);
941
}
942

943
TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
944
  Type *FltTy = IRB.getFloatTy();
945
  Value *L = ConstantFP::get(FltTy, 1.0);
946
  Value *R = ConstantFP::get(FltTy, 2.0);
947
  Value *MatchL, *MatchR;
948

949
  // Test ULT.
950
  EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
951
                  .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
952
  EXPECT_EQ(L, MatchL);
953
  EXPECT_EQ(R, MatchR);
954

955
  // Test ULE.
956
  EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
957
                  .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
958
  EXPECT_EQ(L, MatchL);
959
  EXPECT_EQ(R, MatchR);
960

961
  // Test no match on UGE.
962
  EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
963
                   .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
964

965
  // Test no match on UGT.
966
  EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
967
                   .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
968

969
  // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
970
  // %cmp = fcmp uge L, R
971
  // %min = select %cmp R, L
972
  // Given L == NaN
973
  // the above is expanded to %cmp == true ==> %min = R
974
  // which is true for OrdFMin, not UnordFMin, so test that:
975

976
  // [UO]GE with inverted select.
977
  EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
978
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
979
  EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
980
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
981
  EXPECT_EQ(L, MatchL);
982
  EXPECT_EQ(R, MatchR);
983

984
  // [UO]GT with inverted select.
985
  EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
986
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
987
  EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
988
                  .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
989
  EXPECT_EQ(L, MatchL);
990
  EXPECT_EQ(R, MatchR);
991
}
992

993
TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
994
  Type *FltTy = IRB.getFloatTy();
995
  Value *L = ConstantFP::get(FltTy, 1.0);
996
  Value *R = ConstantFP::get(FltTy, 2.0);
997
  Value *MatchL, *MatchR;
998

999
  // Test UGT.
1000
  EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1001
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
1002
  EXPECT_EQ(L, MatchL);
1003
  EXPECT_EQ(R, MatchR);
1004

1005
  // Test UGE.
1006
  EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1007
                  .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
1008
  EXPECT_EQ(L, MatchL);
1009
  EXPECT_EQ(R, MatchR);
1010

1011
  // Test no match on ULE.
1012
  EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1013
                   .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
1014

1015
  // Test no match on ULT.
1016
  EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1017
                   .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
1018

1019
  // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
1020
  // %cmp = fcmp ule L, R
1021
  // %max = select %cmp R, L
1022
  // Given L == NaN
1023
  // the above is expanded to %cmp == true ==> %max = R
1024
  // which is true for OrdFMax, not UnordFMax, so test that:
1025

1026
  // [UO]LE with inverted select.
1027
  EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1028
                  .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
1029
  EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1030
                  .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
1031
  EXPECT_EQ(L, MatchL);
1032
  EXPECT_EQ(R, MatchR);
1033

1034
  // [UO]LT with inverted select.
1035
  EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1036
                  .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
1037
  EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
1038
                  .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
1039
  EXPECT_EQ(L, MatchL);
1040
  EXPECT_EQ(R, MatchR);
1041
}
1042

1043
TEST_F(PatternMatchTest, OverflowingBinOps) {
1044
  Value *L = IRB.getInt32(1);
1045
  Value *R = IRB.getInt32(2);
1046
  Value *MatchL, *MatchR;
1047

1048
  EXPECT_TRUE(
1049
      m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
1050
  EXPECT_EQ(L, MatchL);
1051
  EXPECT_EQ(R, MatchR);
1052
  MatchL = MatchR = nullptr;
1053
  EXPECT_TRUE(
1054
      m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
1055
  EXPECT_EQ(L, MatchL);
1056
  EXPECT_EQ(R, MatchR);
1057
  MatchL = MatchR = nullptr;
1058
  EXPECT_TRUE(
1059
      m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
1060
  EXPECT_EQ(L, MatchL);
1061
  EXPECT_EQ(R, MatchR);
1062
  MatchL = MatchR = nullptr;
1063
  EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
1064
      IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
1065
  EXPECT_EQ(L, MatchL);
1066
  EXPECT_EQ(R, MatchR);
1067

1068
  EXPECT_TRUE(
1069
      m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
1070
  EXPECT_EQ(L, MatchL);
1071
  EXPECT_EQ(R, MatchR);
1072
  MatchL = MatchR = nullptr;
1073

1074
  EXPECT_TRUE(
1075
      m_c_NUWAdd(m_Specific(L), m_Specific(R)).match(IRB.CreateNUWAdd(L, R)));
1076
  EXPECT_TRUE(
1077
      m_c_NUWAdd(m_Specific(R), m_Specific(L)).match(IRB.CreateNUWAdd(L, R)));
1078
  EXPECT_FALSE(
1079
      m_c_NUWAdd(m_Specific(R), m_ZeroInt()).match(IRB.CreateNUWAdd(L, R)));
1080
  EXPECT_FALSE(
1081
      m_NUWAdd(m_Specific(R), m_Specific(L)).match(IRB.CreateNUWAdd(L, R)));
1082

1083
  EXPECT_TRUE(
1084
      m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
1085
  EXPECT_EQ(L, MatchL);
1086
  EXPECT_EQ(R, MatchR);
1087
  MatchL = MatchR = nullptr;
1088
  EXPECT_TRUE(
1089
      m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
1090
  EXPECT_EQ(L, MatchL);
1091
  EXPECT_EQ(R, MatchR);
1092
  MatchL = MatchR = nullptr;
1093
  EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
1094
      IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
1095
  EXPECT_EQ(L, MatchL);
1096
  EXPECT_EQ(R, MatchR);
1097

1098
  EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
1099
  EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1100
  EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
1101
  EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
1102
  EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
1103
  EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1104
  EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
1105
  EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
1106
  EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1107
  EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
1108
  EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
1109
      IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
1110
  EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1111

1112
  EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
1113
  EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
1114
  EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
1115
  EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
1116
  EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
1117
  EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1118
  EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
1119
  EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
1120
  EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1121
  EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
1122
  EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
1123
      IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
1124
  EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
1125
}
1126

1127
TEST_F(PatternMatchTest, LoadStoreOps) {
1128
  // Create this load/store sequence:
1129
  //
1130
  //  %p = alloca i32*
1131
  //  %0 = load i32*, i32** %p
1132
  //  store i32 42, i32* %0
1133

1134
  Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
1135
  Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca);
1136
  Value *FourtyTwo = IRB.getInt32(42);
1137
  Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
1138
  Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
1139

1140
  EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
1141
  EXPECT_EQ(Alloca, MatchLoad);
1142

1143
  EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
1144

1145
  EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
1146

1147
  EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
1148
                .match(StoreInst));
1149
  EXPECT_EQ(FourtyTwo, MatchStoreVal);
1150
  EXPECT_EQ(Alloca, MatchStorePointer);
1151

1152
  EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
1153
                .match(Alloca));
1154

1155
  EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
1156
                .match(StoreInst));
1157
  EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
1158
                .match(StoreInst));
1159
  EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
1160
                .match(StoreInst));
1161
}
1162

1163
TEST_F(PatternMatchTest, VectorOps) {
1164
  // Build up small tree of vector operations
1165
  //
1166
  //   Val = 0 + 1
1167
  //   Val2 = Val + 3
1168
  //   VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
1169
  //   VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
1170
  //   VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
1171
  //   VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
1172
  //
1173
  //   SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
1174
  //   SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
1175
  //   SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
1176
  //   SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
1177
  //
1178
  //   SP1 = VectorSplat(2, i8 2)
1179
  //   SP2 = VectorSplat(2, i8 %Val)
1180
  Type *VecTy = FixedVectorType::get(IRB.getInt8Ty(), 2);
1181
  Type *i32 = IRB.getInt32Ty();
1182
  Type *i32VecTy = FixedVectorType::get(i32, 2);
1183

1184
  Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
1185
  Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
1186

1187
  SmallVector<Constant *, 2> VecElemIdxs;
1188
  VecElemIdxs.push_back(ConstantInt::get(i32, 0));
1189
  VecElemIdxs.push_back(ConstantInt::get(i32, 2));
1190
  auto *IdxVec = ConstantVector::get(VecElemIdxs);
1191

1192
  Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0);
1193
  Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
1194
  Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
1195
  Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
1196

1197
  Value *EX1 = IRB.CreateExtractElement(VI4, Val);
1198
  Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
1199
  Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
1200

1201
  Constant *Zero = ConstantAggregateZero::get(i32VecTy);
1202
  SmallVector<int, 16> ZeroMask;
1203
  ShuffleVectorInst::getShuffleMask(Zero, ZeroMask);
1204

1205
  Value *SI1 = IRB.CreateShuffleVector(VI1, ZeroMask);
1206
  Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
1207
  Value *SI3 = IRB.CreateShuffleVector(VI3, ZeroMask);
1208
  Value *SI4 = IRB.CreateShuffleVector(VI4, ZeroMask);
1209

1210
  Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
1211
  Value *SP2 = IRB.CreateVectorSplat(2, Val);
1212

1213
  Value *A = nullptr, *B = nullptr, *C = nullptr;
1214

1215
  // Test matching insertelement
1216
  EXPECT_TRUE(match(VI1, m_InsertElt(m_Value(), m_Value(), m_Value())));
1217
  EXPECT_TRUE(
1218
      match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt())));
1219
  EXPECT_TRUE(
1220
      match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero())));
1221
  EXPECT_TRUE(
1222
      match(VI1, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero())));
1223
  EXPECT_TRUE(match(VI2, m_InsertElt(m_Value(), m_Value(), m_Value())));
1224
  EXPECT_FALSE(
1225
      match(VI2, m_InsertElt(m_Value(), m_Value(), m_ConstantInt())));
1226
  EXPECT_FALSE(
1227
      match(VI2, m_InsertElt(m_Value(), m_ConstantInt(), m_Value())));
1228
  EXPECT_FALSE(match(VI2, m_InsertElt(m_Constant(), m_Value(), m_Value())));
1229
  EXPECT_TRUE(match(VI3, m_InsertElt(m_Value(A), m_Value(B), m_Value(C))));
1230
  EXPECT_TRUE(A == VI1);
1231
  EXPECT_TRUE(B == Val2);
1232
  EXPECT_TRUE(isa<ConstantInt>(C));
1233
  A = B = C = nullptr; // reset
1234

1235
  // Test matching extractelement
1236
  EXPECT_TRUE(match(EX1, m_ExtractElt(m_Value(A), m_Value(B))));
1237
  EXPECT_TRUE(A == VI4);
1238
  EXPECT_TRUE(B == Val);
1239
  A = B = C = nullptr; // reset
1240
  EXPECT_FALSE(match(EX1, m_ExtractElt(m_Value(), m_ConstantInt())));
1241
  EXPECT_TRUE(match(EX2, m_ExtractElt(m_Value(), m_ConstantInt())));
1242
  EXPECT_TRUE(match(EX3, m_ExtractElt(m_Constant(), m_ConstantInt())));
1243

1244
  // Test matching shufflevector
1245
  ArrayRef<int> Mask;
1246
  EXPECT_TRUE(match(SI1, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask())));
1247
  EXPECT_TRUE(match(SI2, m_Shuffle(m_Value(A), m_Value(B), m_Mask(Mask))));
1248
  EXPECT_TRUE(A == VI3);
1249
  EXPECT_TRUE(B == VI4);
1250
  A = B = C = nullptr; // reset
1251

1252
  // Test matching the vector splat pattern
1253
  EXPECT_TRUE(match(
1254
      SI1,
1255
      m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()),
1256
                m_Undef(), m_ZeroMask())));
1257
  EXPECT_FALSE(match(
1258
      SI3, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1259
                     m_Undef(), m_ZeroMask())));
1260
  EXPECT_FALSE(match(
1261
      SI4, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()),
1262
                     m_Undef(), m_ZeroMask())));
1263
  EXPECT_TRUE(match(
1264
      SP1,
1265
      m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()),
1266
                m_Undef(), m_ZeroMask())));
1267
  EXPECT_TRUE(match(
1268
      SP2, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A), m_Zero()),
1269
                     m_Undef(), m_ZeroMask())));
1270
  EXPECT_TRUE(A == Val);
1271
}
1272

1273
TEST_F(PatternMatchTest, UndefPoisonMix) {
1274
  Type *ScalarTy = IRB.getInt8Ty();
1275
  ArrayType *ArrTy = ArrayType::get(ScalarTy, 2);
1276
  StructType *StTy = StructType::get(ScalarTy, ScalarTy);
1277
  StructType *StTy2 = StructType::get(ScalarTy, StTy);
1278
  StructType *StTy3 = StructType::get(StTy, ScalarTy);
1279
  Constant *Zero = ConstantInt::getNullValue(ScalarTy);
1280
  UndefValue *U = UndefValue::get(ScalarTy);
1281
  UndefValue *P = PoisonValue::get(ScalarTy);
1282

1283
  EXPECT_TRUE(match(ConstantVector::get({U, P}), m_Undef()));
1284
  EXPECT_TRUE(match(ConstantVector::get({P, U}), m_Undef()));
1285

1286
  EXPECT_TRUE(match(ConstantArray::get(ArrTy, {U, P}), m_Undef()));
1287
  EXPECT_TRUE(match(ConstantArray::get(ArrTy, {P, U}), m_Undef()));
1288

1289
  auto *UP = ConstantStruct::get(StTy, {U, P});
1290
  EXPECT_TRUE(match(ConstantStruct::get(StTy2, {U, UP}), m_Undef()));
1291
  EXPECT_TRUE(match(ConstantStruct::get(StTy2, {P, UP}), m_Undef()));
1292
  EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, U}), m_Undef()));
1293
  EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, P}), m_Undef()));
1294

1295
  EXPECT_FALSE(match(ConstantStruct::get(StTy, {U, Zero}), m_Undef()));
1296
  EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, U}), m_Undef()));
1297
  EXPECT_FALSE(match(ConstantStruct::get(StTy, {P, Zero}), m_Undef()));
1298
  EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, P}), m_Undef()));
1299

1300
  EXPECT_FALSE(match(ConstantStruct::get(StTy2, {Zero, UP}), m_Undef()));
1301
  EXPECT_FALSE(match(ConstantStruct::get(StTy3, {UP, Zero}), m_Undef()));
1302
}
1303

1304
TEST_F(PatternMatchTest, VectorUndefInt) {
1305
  Type *ScalarTy = IRB.getInt8Ty();
1306
  Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1307
  Constant *ScalarUndef = UndefValue::get(ScalarTy);
1308
  Constant *VectorUndef = UndefValue::get(VectorTy);
1309
  Constant *ScalarPoison = PoisonValue::get(ScalarTy);
1310
  Constant *VectorPoison = PoisonValue::get(VectorTy);
1311
  Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1312
  Constant *VectorZero = Constant::getNullValue(VectorTy);
1313

1314
  SmallVector<Constant *, 4> Elems;
1315
  Elems.push_back(ScalarUndef);
1316
  Elems.push_back(ScalarZero);
1317
  Elems.push_back(ScalarUndef);
1318
  Elems.push_back(ScalarZero);
1319
  Constant *VectorZeroUndef = ConstantVector::get(Elems);
1320

1321
  SmallVector<Constant *, 4> Elems2;
1322
  Elems2.push_back(ScalarPoison);
1323
  Elems2.push_back(ScalarZero);
1324
  Elems2.push_back(ScalarPoison);
1325
  Elems2.push_back(ScalarZero);
1326
  Constant *VectorZeroPoison = ConstantVector::get(Elems2);
1327

1328
  EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1329
  EXPECT_TRUE(match(ScalarPoison, m_Undef()));
1330
  EXPECT_TRUE(match(VectorUndef, m_Undef()));
1331
  EXPECT_TRUE(match(VectorPoison, m_Undef()));
1332
  EXPECT_FALSE(match(ScalarZero, m_Undef()));
1333
  EXPECT_FALSE(match(VectorZero, m_Undef()));
1334
  EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1335
  EXPECT_FALSE(match(VectorZeroPoison, m_Undef()));
1336

1337
  EXPECT_FALSE(match(ScalarUndef, m_Zero()));
1338
  EXPECT_FALSE(match(ScalarPoison, m_Zero()));
1339
  EXPECT_FALSE(match(VectorUndef, m_Zero()));
1340
  EXPECT_FALSE(match(VectorPoison, m_Zero()));
1341
  EXPECT_FALSE(match(VectorZeroUndef, m_Zero()));
1342
  EXPECT_TRUE(match(ScalarZero, m_Zero()));
1343
  EXPECT_TRUE(match(VectorZero, m_Zero()));
1344
  EXPECT_TRUE(match(VectorZeroPoison, m_Zero()));
1345

1346
  const APInt *C;
1347
  // Regardless of whether poison is allowed,
1348
  // a fully undef/poison constant does not match.
1349
  EXPECT_FALSE(match(ScalarUndef, m_APInt(C)));
1350
  EXPECT_FALSE(match(ScalarUndef, m_APIntForbidPoison(C)));
1351
  EXPECT_FALSE(match(ScalarUndef, m_APIntAllowPoison(C)));
1352
  EXPECT_FALSE(match(VectorUndef, m_APInt(C)));
1353
  EXPECT_FALSE(match(VectorUndef, m_APIntForbidPoison(C)));
1354
  EXPECT_FALSE(match(VectorUndef, m_APIntAllowPoison(C)));
1355
  EXPECT_FALSE(match(ScalarPoison, m_APInt(C)));
1356
  EXPECT_FALSE(match(ScalarPoison, m_APIntForbidPoison(C)));
1357
  EXPECT_FALSE(match(ScalarPoison, m_APIntAllowPoison(C)));
1358
  EXPECT_FALSE(match(VectorPoison, m_APInt(C)));
1359
  EXPECT_FALSE(match(VectorPoison, m_APIntForbidPoison(C)));
1360
  EXPECT_FALSE(match(VectorPoison, m_APIntAllowPoison(C)));
1361

1362
  // We can always match simple constants and simple splats.
1363
  C = nullptr;
1364
  EXPECT_TRUE(match(ScalarZero, m_APInt(C)));
1365
  EXPECT_TRUE(C->isZero());
1366
  C = nullptr;
1367
  EXPECT_TRUE(match(ScalarZero, m_APIntForbidPoison(C)));
1368
  EXPECT_TRUE(C->isZero());
1369
  C = nullptr;
1370
  EXPECT_TRUE(match(ScalarZero, m_APIntAllowPoison(C)));
1371
  EXPECT_TRUE(C->isZero());
1372
  C = nullptr;
1373
  EXPECT_TRUE(match(VectorZero, m_APInt(C)));
1374
  EXPECT_TRUE(C->isZero());
1375
  C = nullptr;
1376
  EXPECT_TRUE(match(VectorZero, m_APIntForbidPoison(C)));
1377
  EXPECT_TRUE(C->isZero());
1378
  C = nullptr;
1379
  EXPECT_TRUE(match(VectorZero, m_APIntAllowPoison(C)));
1380
  EXPECT_TRUE(C->isZero());
1381

1382
  // Splats with undef are never allowed.
1383
  // Whether splats with poison can be matched depends on the matcher.
1384
  EXPECT_FALSE(match(VectorZeroUndef, m_APInt(C)));
1385
  EXPECT_FALSE(match(VectorZeroUndef, m_APIntForbidPoison(C)));
1386
  EXPECT_FALSE(match(VectorZeroUndef, m_APIntAllowPoison(C)));
1387

1388
  EXPECT_FALSE(match(VectorZeroPoison, m_APInt(C)));
1389
  EXPECT_FALSE(match(VectorZeroPoison, m_APIntForbidPoison(C)));
1390
  C = nullptr;
1391
  EXPECT_TRUE(match(VectorZeroPoison, m_APIntAllowPoison(C)));
1392
  EXPECT_TRUE(C->isZero());
1393
}
1394

1395
TEST_F(PatternMatchTest, VectorUndefFloat) {
1396
  Type *ScalarTy = IRB.getFloatTy();
1397
  Type *VectorTy = FixedVectorType::get(ScalarTy, 4);
1398
  Constant *ScalarUndef = UndefValue::get(ScalarTy);
1399
  Constant *VectorUndef = UndefValue::get(VectorTy);
1400
  Constant *ScalarPoison = PoisonValue::get(ScalarTy);
1401
  Constant *VectorPoison = PoisonValue::get(VectorTy);
1402
  Constant *ScalarZero = Constant::getNullValue(ScalarTy);
1403
  Constant *VectorZero = Constant::getNullValue(VectorTy);
1404
  Constant *ScalarPosInf = ConstantFP::getInfinity(ScalarTy, false);
1405
  Constant *ScalarNegInf = ConstantFP::getInfinity(ScalarTy, true);
1406
  Constant *ScalarNaN = ConstantFP::getNaN(ScalarTy, true);
1407

1408
  Constant *VectorZeroUndef =
1409
      ConstantVector::get({ScalarUndef, ScalarZero, ScalarUndef, ScalarZero});
1410

1411
  Constant *VectorZeroPoison =
1412
      ConstantVector::get({ScalarPoison, ScalarZero, ScalarPoison, ScalarZero});
1413

1414
  Constant *VectorInfUndef = ConstantVector::get(
1415
      {ScalarPosInf, ScalarNegInf, ScalarUndef, ScalarPosInf});
1416

1417
  Constant *VectorInfPoison = ConstantVector::get(
1418
      {ScalarPosInf, ScalarNegInf, ScalarPoison, ScalarPosInf});
1419

1420
  Constant *VectorNaNUndef =
1421
      ConstantVector::get({ScalarUndef, ScalarNaN, ScalarNaN, ScalarNaN});
1422

1423
  Constant *VectorNaNPoison =
1424
      ConstantVector::get({ScalarPoison, ScalarNaN, ScalarNaN, ScalarNaN});
1425

1426
  EXPECT_TRUE(match(ScalarUndef, m_Undef()));
1427
  EXPECT_TRUE(match(VectorUndef, m_Undef()));
1428
  EXPECT_TRUE(match(ScalarPoison, m_Undef()));
1429
  EXPECT_TRUE(match(VectorPoison, m_Undef()));
1430
  EXPECT_FALSE(match(ScalarZero, m_Undef()));
1431
  EXPECT_FALSE(match(VectorZero, m_Undef()));
1432
  EXPECT_FALSE(match(VectorZeroUndef, m_Undef()));
1433
  EXPECT_FALSE(match(VectorInfUndef, m_Undef()));
1434
  EXPECT_FALSE(match(VectorNaNUndef, m_Undef()));
1435
  EXPECT_FALSE(match(VectorZeroPoison, m_Undef()));
1436
  EXPECT_FALSE(match(VectorInfPoison, m_Undef()));
1437
  EXPECT_FALSE(match(VectorNaNPoison, m_Undef()));
1438

1439
  EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP()));
1440
  EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP()));
1441
  EXPECT_FALSE(match(ScalarPoison, m_AnyZeroFP()));
1442
  EXPECT_FALSE(match(VectorPoison, m_AnyZeroFP()));
1443
  EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP()));
1444
  EXPECT_TRUE(match(VectorZero, m_AnyZeroFP()));
1445
  EXPECT_FALSE(match(VectorZeroUndef, m_AnyZeroFP()));
1446
  EXPECT_FALSE(match(VectorInfUndef, m_AnyZeroFP()));
1447
  EXPECT_FALSE(match(VectorNaNUndef, m_AnyZeroFP()));
1448
  EXPECT_TRUE(match(VectorZeroPoison, m_AnyZeroFP()));
1449
  EXPECT_FALSE(match(VectorInfPoison, m_AnyZeroFP()));
1450
  EXPECT_FALSE(match(VectorNaNPoison, m_AnyZeroFP()));
1451

1452
  EXPECT_FALSE(match(ScalarUndef, m_NaN()));
1453
  EXPECT_FALSE(match(VectorUndef, m_NaN()));
1454
  EXPECT_FALSE(match(VectorZeroUndef, m_NaN()));
1455
  EXPECT_FALSE(match(ScalarPoison, m_NaN()));
1456
  EXPECT_FALSE(match(VectorPoison, m_NaN()));
1457
  EXPECT_FALSE(match(VectorZeroPoison, m_NaN()));
1458
  EXPECT_FALSE(match(ScalarPosInf, m_NaN()));
1459
  EXPECT_FALSE(match(ScalarNegInf, m_NaN()));
1460
  EXPECT_TRUE(match(ScalarNaN, m_NaN()));
1461
  EXPECT_FALSE(match(VectorInfUndef, m_NaN()));
1462
  EXPECT_FALSE(match(VectorNaNUndef, m_NaN()));
1463
  EXPECT_FALSE(match(VectorInfPoison, m_NaN()));
1464
  EXPECT_TRUE(match(VectorNaNPoison, m_NaN()));
1465

1466
  EXPECT_FALSE(match(ScalarUndef, m_NonNaN()));
1467
  EXPECT_FALSE(match(VectorUndef, m_NonNaN()));
1468
  EXPECT_FALSE(match(VectorZeroUndef, m_NonNaN()));
1469
  EXPECT_FALSE(match(ScalarPoison, m_NonNaN()));
1470
  EXPECT_FALSE(match(VectorPoison, m_NonNaN()));
1471
  EXPECT_TRUE(match(VectorZeroPoison, m_NonNaN()));
1472
  EXPECT_TRUE(match(ScalarPosInf, m_NonNaN()));
1473
  EXPECT_TRUE(match(ScalarNegInf, m_NonNaN()));
1474
  EXPECT_FALSE(match(ScalarNaN, m_NonNaN()));
1475
  EXPECT_FALSE(match(VectorInfUndef, m_NonNaN()));
1476
  EXPECT_FALSE(match(VectorNaNUndef, m_NonNaN()));
1477
  EXPECT_TRUE(match(VectorInfPoison, m_NonNaN()));
1478
  EXPECT_FALSE(match(VectorNaNPoison, m_NonNaN()));
1479

1480
  EXPECT_FALSE(match(ScalarUndef, m_Inf()));
1481
  EXPECT_FALSE(match(VectorUndef, m_Inf()));
1482
  EXPECT_FALSE(match(VectorZeroUndef, m_Inf()));
1483
  EXPECT_FALSE(match(ScalarPoison, m_Inf()));
1484
  EXPECT_FALSE(match(VectorPoison, m_Inf()));
1485
  EXPECT_FALSE(match(VectorZeroPoison, m_Inf()));
1486
  EXPECT_TRUE(match(ScalarPosInf, m_Inf()));
1487
  EXPECT_TRUE(match(ScalarNegInf, m_Inf()));
1488
  EXPECT_FALSE(match(ScalarNaN, m_Inf()));
1489
  EXPECT_FALSE(match(VectorInfUndef, m_Inf()));
1490
  EXPECT_FALSE(match(VectorNaNUndef, m_Inf()));
1491
  EXPECT_TRUE(match(VectorInfPoison, m_Inf()));
1492
  EXPECT_FALSE(match(VectorNaNPoison, m_Inf()));
1493

1494
  EXPECT_FALSE(match(ScalarUndef, m_NonInf()));
1495
  EXPECT_FALSE(match(VectorUndef, m_NonInf()));
1496
  EXPECT_FALSE(match(VectorZeroUndef, m_NonInf()));
1497
  EXPECT_FALSE(match(ScalarPoison, m_NonInf()));
1498
  EXPECT_FALSE(match(VectorPoison, m_NonInf()));
1499
  EXPECT_TRUE(match(VectorZeroPoison, m_NonInf()));
1500
  EXPECT_FALSE(match(ScalarPosInf, m_NonInf()));
1501
  EXPECT_FALSE(match(ScalarNegInf, m_NonInf()));
1502
  EXPECT_TRUE(match(ScalarNaN, m_NonInf()));
1503
  EXPECT_FALSE(match(VectorInfUndef, m_NonInf()));
1504
  EXPECT_FALSE(match(VectorNaNUndef, m_NonInf()));
1505
  EXPECT_FALSE(match(VectorInfPoison, m_NonInf()));
1506
  EXPECT_TRUE(match(VectorNaNPoison, m_NonInf()));
1507

1508
  EXPECT_FALSE(match(ScalarUndef, m_Finite()));
1509
  EXPECT_FALSE(match(VectorUndef, m_Finite()));
1510
  EXPECT_FALSE(match(VectorZeroUndef, m_Finite()));
1511
  EXPECT_FALSE(match(ScalarPoison, m_Finite()));
1512
  EXPECT_FALSE(match(VectorPoison, m_Finite()));
1513
  EXPECT_TRUE(match(VectorZeroPoison, m_Finite()));
1514
  EXPECT_FALSE(match(ScalarPosInf, m_Finite()));
1515
  EXPECT_FALSE(match(ScalarNegInf, m_Finite()));
1516
  EXPECT_FALSE(match(ScalarNaN, m_Finite()));
1517
  EXPECT_FALSE(match(VectorInfUndef, m_Finite()));
1518
  EXPECT_FALSE(match(VectorNaNUndef, m_Finite()));
1519
  EXPECT_FALSE(match(VectorInfPoison, m_Finite()));
1520
  EXPECT_FALSE(match(VectorNaNPoison, m_Finite()));
1521

1522
  auto CheckTrue = [](const APFloat &) { return true; };
1523
  EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckTrue)));
1524
  EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CheckTrue)));
1525
  EXPECT_TRUE(match(ScalarPosInf, m_CheckedFp(CheckTrue)));
1526
  EXPECT_TRUE(match(ScalarNegInf, m_CheckedFp(CheckTrue)));
1527
  EXPECT_TRUE(match(ScalarNaN, m_CheckedFp(CheckTrue)));
1528
  EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckTrue)));
1529
  EXPECT_TRUE(match(VectorInfPoison, m_CheckedFp(CheckTrue)));
1530
  EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckTrue)));
1531
  EXPECT_TRUE(match(VectorNaNPoison, m_CheckedFp(CheckTrue)));
1532

1533
  auto CheckFalse = [](const APFloat &) { return false; };
1534
  EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckFalse)));
1535
  EXPECT_FALSE(match(VectorZeroPoison, m_CheckedFp(CheckFalse)));
1536
  EXPECT_FALSE(match(ScalarPosInf, m_CheckedFp(CheckFalse)));
1537
  EXPECT_FALSE(match(ScalarNegInf, m_CheckedFp(CheckFalse)));
1538
  EXPECT_FALSE(match(ScalarNaN, m_CheckedFp(CheckFalse)));
1539
  EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckFalse)));
1540
  EXPECT_FALSE(match(VectorInfPoison, m_CheckedFp(CheckFalse)));
1541
  EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckFalse)));
1542
  EXPECT_FALSE(match(VectorNaNPoison, m_CheckedFp(CheckFalse)));
1543

1544
  auto CheckNonNaN = [](const APFloat &C) { return !C.isNaN(); };
1545
  EXPECT_FALSE(match(VectorZeroUndef, m_CheckedFp(CheckNonNaN)));
1546
  EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CheckNonNaN)));
1547
  EXPECT_TRUE(match(ScalarPosInf, m_CheckedFp(CheckNonNaN)));
1548
  EXPECT_TRUE(match(ScalarNegInf, m_CheckedFp(CheckNonNaN)));
1549
  EXPECT_FALSE(match(ScalarNaN, m_CheckedFp(CheckNonNaN)));
1550
  EXPECT_FALSE(match(VectorInfUndef, m_CheckedFp(CheckNonNaN)));
1551
  EXPECT_TRUE(match(VectorInfPoison, m_CheckedFp(CheckNonNaN)));
1552
  EXPECT_FALSE(match(VectorNaNUndef, m_CheckedFp(CheckNonNaN)));
1553
  EXPECT_FALSE(match(VectorNaNPoison, m_CheckedFp(CheckNonNaN)));
1554

1555
  const APFloat *C;
1556
  const Constant *CC;
1557
  // Regardless of whether poison is allowed,
1558
  // a fully undef/poison constant does not match.
1559
  EXPECT_FALSE(match(ScalarUndef, m_APFloat(C)));
1560
  EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidPoison(C)));
1561
  EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowPoison(C)));
1562
  EXPECT_FALSE(match(ScalarUndef, m_CheckedFp(CC, CheckTrue)));
1563
  EXPECT_FALSE(match(VectorUndef, m_APFloat(C)));
1564
  EXPECT_FALSE(match(VectorUndef, m_APFloatForbidPoison(C)));
1565
  EXPECT_FALSE(match(VectorUndef, m_APFloatAllowPoison(C)));
1566
  EXPECT_FALSE(match(VectorUndef, m_CheckedFp(CC, CheckTrue)));
1567
  EXPECT_FALSE(match(ScalarPoison, m_APFloat(C)));
1568
  EXPECT_FALSE(match(ScalarPoison, m_APFloatForbidPoison(C)));
1569
  EXPECT_FALSE(match(ScalarPoison, m_APFloatAllowPoison(C)));
1570
  EXPECT_FALSE(match(ScalarPoison, m_CheckedFp(CC, CheckTrue)));
1571
  EXPECT_FALSE(match(VectorPoison, m_APFloat(C)));
1572
  EXPECT_FALSE(match(VectorPoison, m_APFloatForbidPoison(C)));
1573
  EXPECT_FALSE(match(VectorPoison, m_APFloatAllowPoison(C)));
1574
  EXPECT_FALSE(match(VectorPoison, m_CheckedFp(CC, CheckTrue)));
1575

1576
  // We can always match simple constants and simple splats.
1577
  C = nullptr;
1578
  EXPECT_TRUE(match(ScalarZero, m_APFloat(C)));
1579
  EXPECT_TRUE(C->isZero());
1580
  C = nullptr;
1581
  EXPECT_TRUE(match(ScalarZero, m_APFloatForbidPoison(C)));
1582
  EXPECT_TRUE(C->isZero());
1583
  C = nullptr;
1584
  EXPECT_TRUE(match(ScalarZero, m_APFloatAllowPoison(C)));
1585
  EXPECT_TRUE(C->isZero());
1586
  C = nullptr;
1587
  EXPECT_TRUE(match(VectorZero, m_APFloat(C)));
1588
  EXPECT_TRUE(C->isZero());
1589
  C = nullptr;
1590
  EXPECT_TRUE(match(VectorZero, m_APFloatForbidPoison(C)));
1591
  EXPECT_TRUE(C->isZero());
1592
  C = nullptr;
1593
  EXPECT_TRUE(match(VectorZero, m_APFloatAllowPoison(C)));
1594
  EXPECT_TRUE(C->isZero());
1595

1596
  CC = nullptr;
1597
  EXPECT_TRUE(match(VectorZero, m_CheckedFp(CC, CheckTrue)));
1598
  EXPECT_TRUE(CC->isNullValue());
1599
  CC = nullptr;
1600
  EXPECT_TRUE(match(VectorZero, m_CheckedFp(CC, CheckNonNaN)));
1601
  EXPECT_TRUE(CC->isNullValue());
1602

1603
  // Splats with undef are never allowed.
1604
  // Whether splats with poison can be matched depends on the matcher.
1605
  EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C)));
1606
  EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidPoison(C)));
1607
  EXPECT_FALSE(match(VectorZeroUndef, m_APFloatAllowPoison(C)));
1608
  EXPECT_FALSE(match(VectorZeroUndef, m_Finite(C)));
1609

1610
  EXPECT_FALSE(match(VectorZeroPoison, m_APFloat(C)));
1611
  EXPECT_FALSE(match(VectorZeroPoison, m_APFloatForbidPoison(C)));
1612
  C = nullptr;
1613
  EXPECT_TRUE(match(VectorZeroPoison, m_APFloatAllowPoison(C)));
1614
  EXPECT_TRUE(C->isZero());
1615
  C = nullptr;
1616
  EXPECT_TRUE(match(VectorZeroPoison, m_Finite(C)));
1617
  EXPECT_TRUE(C->isZero());
1618
  EXPECT_FALSE(match(VectorZeroPoison, m_APFloat(C)));
1619
  EXPECT_FALSE(match(VectorZeroPoison, m_APFloatForbidPoison(C)));
1620
  C = nullptr;
1621
  EXPECT_TRUE(match(VectorZeroPoison, m_APFloatAllowPoison(C)));
1622
  EXPECT_TRUE(C->isZero());
1623
  C = nullptr;
1624
  EXPECT_TRUE(match(VectorZeroPoison, m_Finite(C)));
1625
  EXPECT_TRUE(C->isZero());
1626
  CC = nullptr;
1627
  C = nullptr;
1628
  EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CC, CheckTrue)));
1629
  EXPECT_NE(CC, nullptr);
1630
  EXPECT_TRUE(match(CC, m_APFloatAllowPoison(C)));
1631
  EXPECT_TRUE(C->isZero());
1632
  CC = nullptr;
1633
  C = nullptr;
1634
  EXPECT_TRUE(match(VectorZeroPoison, m_CheckedFp(CC, CheckNonNaN)));
1635
  EXPECT_NE(CC, nullptr);
1636
  EXPECT_TRUE(match(CC, m_APFloatAllowPoison(C)));
1637
  EXPECT_TRUE(C->isZero());
1638
}
1639

1640
TEST_F(PatternMatchTest, FloatingPointFNeg) {
1641
  Type *FltTy = IRB.getFloatTy();
1642
  Value *One = ConstantFP::get(FltTy, 1.0);
1643
  Value *Z = ConstantFP::get(FltTy, 0.0);
1644
  Value *NZ = ConstantFP::get(FltTy, -0.0);
1645
  Value *V = IRB.CreateFNeg(One);
1646
  Value *V1 = IRB.CreateFSub(NZ, One);
1647
  Value *V2 = IRB.CreateFSub(Z, One);
1648
  Value *V3 = IRB.CreateFAdd(NZ, One);
1649
  Value *Match;
1650

1651
  // Test FNeg(1.0)
1652
  EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
1653
  EXPECT_EQ(One, Match);
1654

1655
  // Test FSub(-0.0, 1.0)
1656
  EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
1657
  EXPECT_EQ(One, Match);
1658

1659
  // Test FSub(0.0, 1.0)
1660
  EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
1661
  cast<Instruction>(V2)->setHasNoSignedZeros(true);
1662
  EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
1663
  EXPECT_EQ(One, Match);
1664

1665
  // Test FAdd(-0.0, 1.0)
1666
  EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
1667
}
1668

1669
TEST_F(PatternMatchTest, CondBranchTest) {
1670
  BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F);
1671
  BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F);
1672
  Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB);
1673

1674
  EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock())));
1675

1676
  BasicBlock *A, *B;
1677
  EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B))));
1678
  EXPECT_EQ(TrueBB, A);
1679
  EXPECT_EQ(FalseBB, B);
1680

1681
  EXPECT_FALSE(
1682
      match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock())));
1683
  EXPECT_FALSE(
1684
      match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB))));
1685
  EXPECT_FALSE(
1686
      match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB))));
1687
  EXPECT_TRUE(
1688
      match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB))));
1689

1690
  // Check we can use m_Deferred with branches.
1691
  EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1692
  Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB);
1693
  A = nullptr;
1694
  EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A))));
1695
}
1696

1697
TEST_F(PatternMatchTest, WithOverflowInst) {
1698
  Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow,
1699
                                         IRB.getInt32(0), IRB.getInt32(0));
1700
  Value *Add0 = IRB.CreateExtractValue(Add, 0);
1701
  Value *Add1 = IRB.CreateExtractValue(Add, 1);
1702

1703
  EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value())));
1704
  EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value())));
1705
  EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value())));
1706
  EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value())));
1707
  EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1708
  EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value())));
1709

1710
  WithOverflowInst *WOI;
1711
  EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI)));
1712
  EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI)));
1713
  EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI)));
1714

1715
  EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI))));
1716
  EXPECT_EQ(Add, WOI);
1717
  EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI))));
1718
  EXPECT_EQ(Add, WOI);
1719
}
1720

1721
TEST_F(PatternMatchTest, MinMaxIntrinsics) {
1722
  Type *Ty = IRB.getInt32Ty();
1723
  Value *L = ConstantInt::get(Ty, 1);
1724
  Value *R = ConstantInt::get(Ty, 2);
1725
  Value *MatchL, *MatchR;
1726

1727
  // Check for intrinsic ID match and capture of operands.
1728
  EXPECT_TRUE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1729
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1730
  EXPECT_EQ(L, MatchL);
1731
  EXPECT_EQ(R, MatchR);
1732

1733
  EXPECT_TRUE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1734
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1735
  EXPECT_EQ(L, MatchL);
1736
  EXPECT_EQ(R, MatchR);
1737

1738
  EXPECT_TRUE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1739
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1740
  EXPECT_EQ(L, MatchL);
1741
  EXPECT_EQ(R, MatchR);
1742

1743
  EXPECT_TRUE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1744
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1745
  EXPECT_EQ(L, MatchL);
1746
  EXPECT_EQ(R, MatchR);
1747

1748
  // Check for intrinsic ID mismatch.
1749
  EXPECT_FALSE(m_SMax(m_Value(MatchL), m_Value(MatchR))
1750
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R)));
1751
  EXPECT_FALSE(m_SMin(m_Value(MatchL), m_Value(MatchR))
1752
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R)));
1753
  EXPECT_FALSE(m_UMax(m_Value(MatchL), m_Value(MatchR))
1754
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R)));
1755
  EXPECT_FALSE(m_UMin(m_Value(MatchL), m_Value(MatchR))
1756
                  .match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R)));
1757
}
1758

1759
TEST_F(PatternMatchTest, IntrinsicMatcher) {
1760
  Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1761
  Value *Hash = IRB.getInt64(0);
1762
  Value *Num = IRB.getInt32(1);
1763
  Value *Index = IRB.getInt32(2);
1764
  Value *Step = IRB.getInt64(3);
1765

1766
  Value *Ops[] = {Name, Hash, Num, Index, Step};
1767
  Module *M = BB->getParent()->getParent();
1768
  Function *TheFn =
1769
      Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1770

1771
  Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1772

1773
  // Match without capturing.
1774
  EXPECT_TRUE(match(
1775
      Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1776
                      m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1777
  EXPECT_FALSE(match(
1778
      Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1779
                      m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1780

1781
  // Match with capturing.
1782
  Value *Arg1 = nullptr;
1783
  Value *Arg2 = nullptr;
1784
  Value *Arg3 = nullptr;
1785
  Value *Arg4 = nullptr;
1786
  Value *Arg5 = nullptr;
1787
  EXPECT_TRUE(
1788
      match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1789
                            m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1790
                            m_Value(Arg4), m_Value(Arg5))));
1791
  EXPECT_EQ(Arg1, Name);
1792
  EXPECT_EQ(Arg2, Hash);
1793
  EXPECT_EQ(Arg3, Num);
1794
  EXPECT_EQ(Arg4, Index);
1795
  EXPECT_EQ(Arg5, Step);
1796

1797
  // Match specific second argument.
1798
  EXPECT_TRUE(
1799
      match(Intrinsic5,
1800
            m_Intrinsic<Intrinsic::instrprof_increment_step>(
1801
                m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1802
  EXPECT_FALSE(
1803
      match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1804
                            m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1805
                            m_Value())));
1806

1807
  // Match specific third argument.
1808
  EXPECT_TRUE(
1809
      match(Intrinsic5,
1810
            m_Intrinsic<Intrinsic::instrprof_increment_step>(
1811
                m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1812
  EXPECT_FALSE(
1813
      match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1814
                            m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1815
                            m_Value())));
1816

1817
  // Match specific fourth argument.
1818
  EXPECT_TRUE(
1819
      match(Intrinsic5,
1820
            m_Intrinsic<Intrinsic::instrprof_increment_step>(
1821
                m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1822
  EXPECT_FALSE(
1823
      match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1824
                            m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1825
                            m_Value())));
1826

1827
  // Match specific fifth argument.
1828
  EXPECT_TRUE(
1829
      match(Intrinsic5,
1830
            m_Intrinsic<Intrinsic::instrprof_increment_step>(
1831
                m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1832
  EXPECT_FALSE(
1833
      match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1834
                            m_Value(), m_Value(), m_Value(), m_Value(),
1835
                            m_SpecificInt(10))));
1836
}
1837

1838
namespace {
1839

1840
struct is_unsigned_zero_pred {
1841
  bool isValue(const APInt &C) { return C.isZero(); }
1842
};
1843

1844
struct is_float_zero_pred {
1845
  bool isValue(const APFloat &C) { return C.isZero(); }
1846
};
1847

1848
template <typename T> struct always_true_pred {
1849
  bool isValue(const T &) { return true; }
1850
};
1851

1852
template <typename T> struct always_false_pred {
1853
  bool isValue(const T &) { return false; }
1854
};
1855

1856
struct is_unsigned_max_pred {
1857
  bool isValue(const APInt &C) { return C.isMaxValue(); }
1858
};
1859

1860
struct is_float_nan_pred {
1861
  bool isValue(const APFloat &C) { return C.isNaN(); }
1862
};
1863

1864
} // namespace
1865

1866
TEST_F(PatternMatchTest, ConstantPredicateType) {
1867

1868
  // Scalar integer
1869
  APInt U32Max = APInt::getAllOnes(32);
1870
  APInt U32Zero = APInt::getZero(32);
1871
  APInt U32DeadBeef(32, 0xDEADBEEF);
1872

1873
  Type *U32Ty = Type::getInt32Ty(Ctx);
1874

1875
  Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max);
1876
  Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero);
1877
  Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef);
1878

1879
  EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1880
  EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1881
  EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1882
  EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1883

1884
  EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1885
  EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1886
  EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1887
  EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1888

1889
  EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1890
  EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1891
  EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1892
  EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1893

1894
  // Scalar float
1895
  APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle());
1896
  APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle());
1897
  APFloat F32Pi(3.14f);
1898

1899
  Type *F32Ty = Type::getFloatTy(Ctx);
1900

1901
  Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN);
1902
  Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero);
1903
  Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi);
1904

1905
  EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1906
  EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1907
  EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1908
  EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1909

1910
  EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1911
  EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1912
  EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1913
  EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1914

1915
  EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1916
  EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1917
  EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1918
  EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1919

1920
  auto FixedEC = ElementCount::getFixed(4);
1921
  auto ScalableEC = ElementCount::getScalable(4);
1922

1923
  // Vector splat
1924

1925
  for (auto EC : {FixedEC, ScalableEC}) {
1926
    // integer
1927

1928
    Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max);
1929
    Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero);
1930
    Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef);
1931

1932
    EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>()));
1933
    EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>()));
1934
    EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>()));
1935
    EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>()));
1936

1937
    EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>()));
1938
    EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>()));
1939
    EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>()));
1940
    EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>()));
1941

1942
    EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>()));
1943
    EXPECT_FALSE(
1944
        match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>()));
1945
    EXPECT_TRUE(
1946
        match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>()));
1947
    EXPECT_FALSE(
1948
        match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>()));
1949

1950
    // float
1951

1952
    Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN);
1953
    Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero);
1954
    Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi);
1955

1956
    EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>()));
1957
    EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>()));
1958
    EXPECT_TRUE(
1959
        match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>()));
1960
    EXPECT_FALSE(
1961
        match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>()));
1962

1963
    EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>()));
1964
    EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>()));
1965
    EXPECT_TRUE(
1966
        match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>()));
1967
    EXPECT_FALSE(
1968
        match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>()));
1969

1970
    EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>()));
1971
    EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>()));
1972
    EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>()));
1973
    EXPECT_FALSE(
1974
        match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>()));
1975
  }
1976

1977
  // Int arbitrary vector
1978

1979
  Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef});
1980
  Constant *CU32Undef = UndefValue::get(U32Ty);
1981
  Constant *CU32Poison = PoisonValue::get(U32Ty);
1982
  Constant *CU32MaxWithUndef =
1983
      ConstantVector::get({CU32Undef, CU32Max, CU32Undef});
1984
  Constant *CU32MaxWithPoison =
1985
      ConstantVector::get({CU32Poison, CU32Max, CU32Poison});
1986

1987
  EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>()));
1988
  EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>()));
1989
  EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>()));
1990
  EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>()));
1991

1992
  EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>()));
1993
  EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>()));
1994
  EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>()));
1995
  EXPECT_FALSE(
1996
      match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>()));
1997

1998
  EXPECT_TRUE(match(CU32MaxWithPoison, cst_pred_ty<is_unsigned_max_pred>()));
1999
  EXPECT_FALSE(match(CU32MaxWithPoison, cst_pred_ty<is_unsigned_zero_pred>()));
2000
  EXPECT_TRUE(match(CU32MaxWithPoison, cst_pred_ty<always_true_pred<APInt>>()));
2001
  EXPECT_FALSE(
2002
      match(CU32MaxWithPoison, cst_pred_ty<always_false_pred<APInt>>()));
2003

2004
  // Float arbitrary vector
2005

2006
  Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi});
2007
  Constant *CF32Undef = UndefValue::get(F32Ty);
2008
  Constant *CF32Poison = PoisonValue::get(F32Ty);
2009
  Constant *CF32NaNWithUndef =
2010
      ConstantVector::get({CF32Undef, CF32NaN, CF32Undef});
2011
  Constant *CF32NaNWithPoison =
2012
      ConstantVector::get({CF32Poison, CF32NaN, CF32Poison});
2013

2014
  EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>()));
2015
  EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>()));
2016
  EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>()));
2017
  EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>()));
2018

2019
  EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>()));
2020
  EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>()));
2021
  EXPECT_FALSE(
2022
      match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>()));
2023
  EXPECT_FALSE(
2024
      match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
2025

2026
  EXPECT_TRUE(match(CF32NaNWithPoison, cstfp_pred_ty<is_float_nan_pred>()));
2027
  EXPECT_FALSE(match(CF32NaNWithPoison, cstfp_pred_ty<is_float_zero_pred>()));
2028
  EXPECT_TRUE(
2029
      match(CF32NaNWithPoison, cstfp_pred_ty<always_true_pred<APFloat>>()));
2030
  EXPECT_FALSE(
2031
      match(CF32NaNWithPoison, cstfp_pred_ty<always_false_pred<APFloat>>()));
2032
}
2033

2034
TEST_F(PatternMatchTest, InsertValue) {
2035
  Type *StructTy = StructType::create(IRB.getContext(),
2036
                                      {IRB.getInt32Ty(), IRB.getInt64Ty()});
2037
  Value *Ins0 =
2038
      IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0);
2039
  Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1);
2040

2041
  EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value())));
2042
  EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value())));
2043
  EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value())));
2044
  EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value())));
2045

2046
  EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20))));
2047
  EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0))));
2048

2049
  EXPECT_TRUE(
2050
      match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()),
2051
                                   m_SpecificInt(90))));
2052
  EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
2053
}
2054

2055
TEST_F(PatternMatchTest, LogicalSelects) {
2056
  Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
2057
  Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
2058
  Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
2059
  Constant *T = IRB.getInt1(true);
2060
  Constant *F = IRB.getInt1(false);
2061
  Value *And = IRB.CreateSelect(X, Y, F);
2062
  Value *Or = IRB.CreateSelect(X, T, Y);
2063

2064
  // Logical and:
2065
  // Check basic no-capture logic - opcode and constant must match.
2066
  EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Value())));
2067
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Value())));
2068
  EXPECT_FALSE(match(And, m_LogicalOr(m_Value(), m_Value())));
2069
  EXPECT_FALSE(match(And, m_c_LogicalOr(m_Value(), m_Value())));
2070

2071
  // Check with captures.
2072
  EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Value())));
2073
  EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Specific(Y))));
2074
  EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(Y))));
2075

2076
  EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Value())));
2077
  EXPECT_FALSE(match(And, m_LogicalAnd(m_Value(), m_Specific(X))));
2078
  EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(X))));
2079

2080
  EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(X))));
2081
  EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(Y))));
2082

2083
  // Check captures for commutative match.
2084
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Value())));
2085
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(Y))));
2086
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(Y))));
2087

2088
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Value())));
2089
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(X))));
2090
  EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(X))));
2091

2092
  EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(X))));
2093
  EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(Y))));
2094

2095
  // Logical or:
2096
  // Check basic no-capture logic - opcode and constant must match.
2097
  EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Value())));
2098
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Value())));
2099
  EXPECT_FALSE(match(Or, m_LogicalAnd(m_Value(), m_Value())));
2100
  EXPECT_FALSE(match(Or, m_c_LogicalAnd(m_Value(), m_Value())));
2101

2102
  // Check with captures.
2103
  EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Value())));
2104
  EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Specific(Y))));
2105
  EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(Y))));
2106

2107
  EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Value())));
2108
  EXPECT_FALSE(match(Or, m_LogicalOr(m_Value(), m_Specific(X))));
2109
  EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(X))));
2110

2111
  EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(X))));
2112
  EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(Y))));
2113

2114
  // Check captures for commutative match.
2115
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Value())));
2116
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(Y))));
2117
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(Y))));
2118

2119
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Value())));
2120
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(X))));
2121
  EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(X))));
2122

2123
  EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(X))));
2124
  EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(Y))));
2125
}
2126

2127
TEST_F(PatternMatchTest, VectorLogicalSelects) {
2128
  Type *i1 = IRB.getInt1Ty();
2129
  Type *v3i1 = FixedVectorType::get(i1, 3);
2130

2131
  Value *Alloca = IRB.CreateAlloca(i1);
2132
  Value *AllocaVec = IRB.CreateAlloca(v3i1);
2133
  Value *Scalar = IRB.CreateLoad(i1, Alloca);
2134
  Value *Vector = IRB.CreateLoad(v3i1, AllocaVec);
2135
  Constant *F = Constant::getNullValue(v3i1);
2136
  Constant *T = Constant::getAllOnesValue(v3i1);
2137

2138
  // select <3 x i1> Vector, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
2139
  Value *VecAnd = IRB.CreateSelect(Vector, Vector, F);
2140

2141
  // select i1 Scalar, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0>
2142
  Value *MixedTypeAnd = IRB.CreateSelect(Scalar, Vector, F);
2143

2144
  // select <3 x i1> Vector, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
2145
  Value *VecOr = IRB.CreateSelect(Vector, T, Vector);
2146

2147
  // select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector
2148
  Value *MixedTypeOr = IRB.CreateSelect(Scalar, T, Vector);
2149

2150
  // We allow matching a real vector logical select,
2151
  // but not a scalar select of vector bools.
2152
  EXPECT_TRUE(match(VecAnd, m_LogicalAnd(m_Value(), m_Value())));
2153
  EXPECT_FALSE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value())));
2154
  EXPECT_TRUE(match(VecOr, m_LogicalOr(m_Value(), m_Value())));
2155
  EXPECT_FALSE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value())));
2156
}
2157

2158
TEST_F(PatternMatchTest, VScale) {
2159
  DataLayout DL = M->getDataLayout();
2160

2161
  Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1);
2162
  Value *NullPtrVec =
2163
      Constant::getNullValue(PointerType::getUnqual(VecTy->getContext()));
2164
  Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1));
2165
  Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType()));
2166
  EXPECT_TRUE(match(PtrToInt, m_VScale()));
2167

2168
  Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2);
2169
  Value *NullPtrVec2 =
2170
      Constant::getNullValue(PointerType::getUnqual(VecTy2->getContext()));
2171
  Value *GEP2 = IRB.CreateGEP(VecTy, NullPtrVec2, IRB.getInt64(1));
2172
  Value *PtrToInt2 =
2173
      IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType()));
2174
  EXPECT_TRUE(match(PtrToInt2, m_VScale()));
2175
}
2176

2177
TEST_F(PatternMatchTest, NotForbidPoison) {
2178
  Type *ScalarTy = IRB.getInt8Ty();
2179
  Type *VectorTy = FixedVectorType::get(ScalarTy, 3);
2180
  Constant *ScalarUndef = UndefValue::get(ScalarTy);
2181
  Constant *ScalarPoison = PoisonValue::get(ScalarTy);
2182
  Constant *ScalarOnes = Constant::getAllOnesValue(ScalarTy);
2183
  Constant *VectorZero = Constant::getNullValue(VectorTy);
2184
  Constant *VectorOnes = Constant::getAllOnesValue(VectorTy);
2185

2186
  SmallVector<Constant *, 3> MixedElemsUndef;
2187
  MixedElemsUndef.push_back(ScalarOnes);
2188
  MixedElemsUndef.push_back(ScalarOnes);
2189
  MixedElemsUndef.push_back(ScalarUndef);
2190
  Constant *VectorMixedUndef = ConstantVector::get(MixedElemsUndef);
2191

2192
  SmallVector<Constant *, 3> MixedElemsPoison;
2193
  MixedElemsPoison.push_back(ScalarOnes);
2194
  MixedElemsPoison.push_back(ScalarOnes);
2195
  MixedElemsPoison.push_back(ScalarPoison);
2196
  Constant *VectorMixedPoison = ConstantVector::get(MixedElemsPoison);
2197

2198
  Value *Not = IRB.CreateXor(VectorZero, VectorOnes);
2199
  Value *X;
2200
  EXPECT_TRUE(match(Not, m_Not(m_Value(X))));
2201
  EXPECT_TRUE(match(X, m_Zero()));
2202
  X = nullptr;
2203
  EXPECT_TRUE(match(Not, m_NotForbidPoison(m_Value(X))));
2204
  EXPECT_TRUE(match(X, m_Zero()));
2205

2206
  Value *NotCommute = IRB.CreateXor(VectorOnes, VectorZero);
2207
  Value *Y;
2208
  EXPECT_TRUE(match(NotCommute, m_Not(m_Value(Y))));
2209
  EXPECT_TRUE(match(Y, m_Zero()));
2210
  Y = nullptr;
2211
  EXPECT_TRUE(match(NotCommute, m_NotForbidPoison(m_Value(Y))));
2212
  EXPECT_TRUE(match(Y, m_Zero()));
2213

2214
  Value *NotWithUndefs = IRB.CreateXor(VectorZero, VectorMixedUndef);
2215
  EXPECT_FALSE(match(NotWithUndefs, m_Not(m_Value())));
2216
  EXPECT_FALSE(match(NotWithUndefs, m_NotForbidPoison(m_Value())));
2217

2218
  Value *NotWithPoisons = IRB.CreateXor(VectorZero, VectorMixedPoison);
2219
  EXPECT_TRUE(match(NotWithPoisons, m_Not(m_Value())));
2220
  EXPECT_FALSE(match(NotWithPoisons, m_NotForbidPoison(m_Value())));
2221

2222
  Value *NotWithUndefsCommute = IRB.CreateXor(VectorMixedUndef, VectorZero);
2223
  EXPECT_FALSE(match(NotWithUndefsCommute, m_Not(m_Value())));
2224
  EXPECT_FALSE(match(NotWithUndefsCommute, m_NotForbidPoison(m_Value())));
2225

2226
  Value *NotWithPoisonsCommute = IRB.CreateXor(VectorMixedPoison, VectorZero);
2227
  EXPECT_TRUE(match(NotWithPoisonsCommute, m_Not(m_Value())));
2228
  EXPECT_FALSE(match(NotWithPoisonsCommute, m_NotForbidPoison(m_Value())));
2229
}
2230

2231
template <typename T> struct MutableConstTest : PatternMatchTest { };
2232

2233
typedef ::testing::Types<std::tuple<Value*, Instruction*>,
2234
                         std::tuple<const Value*, const Instruction *>>
2235
    MutableConstTestTypes;
2236
TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, );
2237

2238
TYPED_TEST(MutableConstTest, ICmp) {
2239
  auto &IRB = PatternMatchTest::IRB;
2240

2241
  typedef std::tuple_element_t<0, TypeParam> ValueType;
2242
  typedef std::tuple_element_t<1, TypeParam> InstructionType;
2243

2244
  Value *L = IRB.getInt32(1);
2245
  Value *R = IRB.getInt32(2);
2246
  ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
2247

2248
  ValueType MatchL;
2249
  ValueType MatchR;
2250
  ICmpInst::Predicate MatchPred;
2251

2252
  EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
2253
              .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
2254
  EXPECT_EQ(L, MatchL);
2255
  EXPECT_EQ(R, MatchR);
2256
}
2257

2258
TEST_F(PatternMatchTest, ConstExpr) {
2259
  Constant *G =
2260
      M->getOrInsertGlobal("dummy", PointerType::getUnqual(IRB.getInt32Ty()));
2261
  Constant *S = ConstantExpr::getPtrToInt(G, IRB.getInt32Ty());
2262
  Type *VecTy = FixedVectorType::get(IRB.getInt32Ty(), 2);
2263
  PoisonValue *P = PoisonValue::get(VecTy);
2264
  Constant *V = ConstantExpr::getInsertElement(P, S, IRB.getInt32(0));
2265

2266
  // The match succeeds on a constant that is a constant expression itself
2267
  // or a constant that contains a constant expression.
2268
  EXPECT_TRUE(match(S, m_ConstantExpr()));
2269
  EXPECT_TRUE(match(V, m_ConstantExpr()));
2270
}
2271

2272
TEST_F(PatternMatchTest, PtrAdd) {
2273
  Type *PtrTy = PointerType::getUnqual(Ctx);
2274
  Type *IdxTy = Type::getInt64Ty(Ctx);
2275
  Constant *Null = Constant::getNullValue(PtrTy);
2276
  Constant *Offset = ConstantInt::get(IdxTy, 42);
2277
  Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
2278
  Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2279
  Value *PtrAddConst =
2280
      ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2281

2282
  Value *A, *B;
2283
  EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2284
  EXPECT_EQ(A, Null);
2285
  EXPECT_EQ(B, Offset);
2286

2287
  EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2288
  EXPECT_EQ(A, Null);
2289
  EXPECT_EQ(B, Offset);
2290

2291
  EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2292
}
2293

2294
} // anonymous namespace.
2295

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.