1 //===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IR/PatternMatch.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/NoFolder.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/Type.h"
26 #include "gtest/gtest.h"
27
28 using namespace llvm;
29 using namespace llvm::PatternMatch;
30
31 namespace {
32
33 struct PatternMatchTest : ::testing::Test {
34 LLVMContext Ctx;
35 std::unique_ptr<Module> M;
36 Function *F;
37 BasicBlock *BB;
38 IRBuilder<NoFolder> IRB;
39
PatternMatchTest__anonc17fe69b0111::PatternMatchTest40 PatternMatchTest()
41 : M(new Module("PatternMatchTestModule", Ctx)),
42 F(Function::Create(
43 FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
44 Function::ExternalLinkage, "f", M.get())),
45 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
46 };
47
TEST_F(PatternMatchTest,OneUse)48 TEST_F(PatternMatchTest, OneUse) {
49 // Build up a little tree of values:
50 //
51 // One = (1 + 2) + 42
52 // Two = One + 42
53 // Leaf = (Two + 8) + (Two + 13)
54 Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)),
55 IRB.getInt32(42));
56 Value *Two = IRB.CreateAdd(One, IRB.getInt32(42));
57 Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)),
58 IRB.CreateAdd(Two, IRB.getInt32(13)));
59 Value *V;
60
61 EXPECT_TRUE(m_OneUse(m_Value(V)).match(One));
62 EXPECT_EQ(One, V);
63
64 EXPECT_FALSE(m_OneUse(m_Value()).match(Two));
65 EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
66 }
67
TEST_F(PatternMatchTest,CommutativeDeferredValue)68 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
69 Value *X = IRB.getInt32(1);
70 Value *Y = IRB.getInt32(2);
71
72 {
73 Value *tX = X;
74 EXPECT_TRUE(match(X, m_Deferred(tX)));
75 EXPECT_FALSE(match(Y, m_Deferred(tX)));
76 }
77 {
78 const Value *tX = X;
79 EXPECT_TRUE(match(X, m_Deferred(tX)));
80 EXPECT_FALSE(match(Y, m_Deferred(tX)));
81 }
82 {
83 Value *const tX = X;
84 EXPECT_TRUE(match(X, m_Deferred(tX)));
85 EXPECT_FALSE(match(Y, m_Deferred(tX)));
86 }
87 {
88 const Value *const tX = X;
89 EXPECT_TRUE(match(X, m_Deferred(tX)));
90 EXPECT_FALSE(match(Y, m_Deferred(tX)));
91 }
92
93 {
94 Value *tX = nullptr;
95 EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX))));
96 EXPECT_EQ(tX, X);
97 }
98 {
99 Value *tX = nullptr;
100 EXPECT_FALSE(
101 match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX))));
102 }
103
104 auto checkMatch = [X, Y](Value *Pattern) {
105 Value *tX = nullptr, *tY = nullptr;
106 EXPECT_TRUE(match(
107 Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY)))));
108 EXPECT_EQ(tX, X);
109 EXPECT_EQ(tY, Y);
110 };
111
112 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y)));
113 checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X)));
114 checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X));
115 checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X));
116 }
117
TEST_F(PatternMatchTest,FloatingPointOrderedMin)118 TEST_F(PatternMatchTest, FloatingPointOrderedMin) {
119 Type *FltTy = IRB.getFloatTy();
120 Value *L = ConstantFP::get(FltTy, 1.0);
121 Value *R = ConstantFP::get(FltTy, 2.0);
122 Value *MatchL, *MatchR;
123
124 // Test OLT.
125 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
126 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
127 EXPECT_EQ(L, MatchL);
128 EXPECT_EQ(R, MatchR);
129
130 // Test OLE.
131 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
132 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
133 EXPECT_EQ(L, MatchL);
134 EXPECT_EQ(R, MatchR);
135
136 // Test no match on OGE.
137 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
138 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
139
140 // Test no match on OGT.
141 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
142 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
143
144 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
145 // %cmp = fcmp oge L, R
146 // %min = select %cmp R, L
147 // Given L == NaN
148 // the above is expanded to %cmp == false ==> %min = L
149 // which is true for UnordFMin, not OrdFMin, so test that:
150
151 // [OU]GE with inverted select.
152 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
153 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
154 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
155 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
156 EXPECT_EQ(L, MatchL);
157 EXPECT_EQ(R, MatchR);
158
159 // [OU]GT with inverted select.
160 EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
161 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
162 EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR))
163 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
164 EXPECT_EQ(L, MatchL);
165 EXPECT_EQ(R, MatchR);
166 }
167
TEST_F(PatternMatchTest,FloatingPointOrderedMax)168 TEST_F(PatternMatchTest, FloatingPointOrderedMax) {
169 Type *FltTy = IRB.getFloatTy();
170 Value *L = ConstantFP::get(FltTy, 1.0);
171 Value *R = ConstantFP::get(FltTy, 2.0);
172 Value *MatchL, *MatchR;
173
174 // Test OGT.
175 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
176 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R)));
177 EXPECT_EQ(L, MatchL);
178 EXPECT_EQ(R, MatchR);
179
180 // Test OGE.
181 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
182 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R)));
183 EXPECT_EQ(L, MatchL);
184 EXPECT_EQ(R, MatchR);
185
186 // Test no match on OLE.
187 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
188 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R)));
189
190 // Test no match on OLT.
191 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
192 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R)));
193
194
195 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
196 // %cmp = fcmp ole L, R
197 // %max = select %cmp, R, L
198 // Given L == NaN,
199 // the above is expanded to %cmp == false ==> %max == L
200 // which is true for UnordFMax, not OrdFMax, so test that:
201
202 // [OU]LE with inverted select.
203 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
204 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
205 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
206 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
207 EXPECT_EQ(L, MatchL);
208 EXPECT_EQ(R, MatchR);
209
210 // [OUT]LT with inverted select.
211 EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
212 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
213 EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR))
214 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
215 EXPECT_EQ(L, MatchL);
216 EXPECT_EQ(R, MatchR);
217 }
218
TEST_F(PatternMatchTest,FloatingPointUnorderedMin)219 TEST_F(PatternMatchTest, FloatingPointUnorderedMin) {
220 Type *FltTy = IRB.getFloatTy();
221 Value *L = ConstantFP::get(FltTy, 1.0);
222 Value *R = ConstantFP::get(FltTy, 2.0);
223 Value *MatchL, *MatchR;
224
225 // Test ULT.
226 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
227 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
228 EXPECT_EQ(L, MatchL);
229 EXPECT_EQ(R, MatchR);
230
231 // Test ULE.
232 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
233 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
234 EXPECT_EQ(L, MatchL);
235 EXPECT_EQ(R, MatchR);
236
237 // Test no match on UGE.
238 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
239 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
240
241 // Test no match on UGT.
242 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
243 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
244
245 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
246 // %cmp = fcmp uge L, R
247 // %min = select %cmp R, L
248 // Given L == NaN
249 // the above is expanded to %cmp == true ==> %min = R
250 // which is true for OrdFMin, not UnordFMin, so test that:
251
252 // [UO]GE with inverted select.
253 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
254 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L)));
255 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
256 .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L)));
257 EXPECT_EQ(L, MatchL);
258 EXPECT_EQ(R, MatchR);
259
260 // [UO]GT with inverted select.
261 EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
262 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L)));
263 EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR))
264 .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L)));
265 EXPECT_EQ(L, MatchL);
266 EXPECT_EQ(R, MatchR);
267 }
268
TEST_F(PatternMatchTest,FloatingPointUnorderedMax)269 TEST_F(PatternMatchTest, FloatingPointUnorderedMax) {
270 Type *FltTy = IRB.getFloatTy();
271 Value *L = ConstantFP::get(FltTy, 1.0);
272 Value *R = ConstantFP::get(FltTy, 2.0);
273 Value *MatchL, *MatchR;
274
275 // Test UGT.
276 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
277 .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R)));
278 EXPECT_EQ(L, MatchL);
279 EXPECT_EQ(R, MatchR);
280
281 // Test UGE.
282 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
283 .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R)));
284 EXPECT_EQ(L, MatchL);
285 EXPECT_EQ(R, MatchR);
286
287 // Test no match on ULE.
288 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
289 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R)));
290
291 // Test no match on ULT.
292 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
293 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R)));
294
295 // Test inverted selects. Note, that this "inverts" the ordering, e.g.:
296 // %cmp = fcmp ule L, R
297 // %max = select %cmp R, L
298 // Given L == NaN
299 // the above is expanded to %cmp == true ==> %max = R
300 // which is true for OrdFMax, not UnordFMax, so test that:
301
302 // [UO]LE with inverted select.
303 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
304 .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L)));
305 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
306 .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L)));
307 EXPECT_EQ(L, MatchL);
308 EXPECT_EQ(R, MatchR);
309
310 // [UO]LT with inverted select.
311 EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
312 .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L)));
313 EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR))
314 .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L)));
315 EXPECT_EQ(L, MatchL);
316 EXPECT_EQ(R, MatchR);
317 }
318
TEST_F(PatternMatchTest,OverflowingBinOps)319 TEST_F(PatternMatchTest, OverflowingBinOps) {
320 Value *L = IRB.getInt32(1);
321 Value *R = IRB.getInt32(2);
322 Value *MatchL, *MatchR;
323
324 EXPECT_TRUE(
325 m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R)));
326 EXPECT_EQ(L, MatchL);
327 EXPECT_EQ(R, MatchR);
328 MatchL = MatchR = nullptr;
329 EXPECT_TRUE(
330 m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R)));
331 EXPECT_EQ(L, MatchL);
332 EXPECT_EQ(R, MatchR);
333 MatchL = MatchR = nullptr;
334 EXPECT_TRUE(
335 m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R)));
336 EXPECT_EQ(L, MatchL);
337 EXPECT_EQ(R, MatchR);
338 MatchL = MatchR = nullptr;
339 EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match(
340 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
341 EXPECT_EQ(L, MatchL);
342 EXPECT_EQ(R, MatchR);
343
344 EXPECT_TRUE(
345 m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R)));
346 EXPECT_EQ(L, MatchL);
347 EXPECT_EQ(R, MatchR);
348 MatchL = MatchR = nullptr;
349 EXPECT_TRUE(
350 m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R)));
351 EXPECT_EQ(L, MatchL);
352 EXPECT_EQ(R, MatchR);
353 MatchL = MatchR = nullptr;
354 EXPECT_TRUE(
355 m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R)));
356 EXPECT_EQ(L, MatchL);
357 EXPECT_EQ(R, MatchR);
358 MatchL = MatchR = nullptr;
359 EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match(
360 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
361 EXPECT_EQ(L, MatchL);
362 EXPECT_EQ(R, MatchR);
363
364 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
365 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
366 EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
367 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
368 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
369 EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
370 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
371 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R)));
372 EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
373 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
374 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(
375 IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false)));
376 EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
377
378 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R)));
379 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R)));
380 EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R)));
381 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R)));
382 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R)));
383 EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
384 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R)));
385 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R)));
386 EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
387 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R)));
388 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(
389 IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true)));
390 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
391 }
392
TEST_F(PatternMatchTest,LoadStoreOps)393 TEST_F(PatternMatchTest, LoadStoreOps) {
394 // Create this load/store sequence:
395 //
396 // %p = alloca i32*
397 // %0 = load i32*, i32** %p
398 // store i32 42, i32* %0
399
400 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
401 Value *LoadInst = IRB.CreateLoad(Alloca);
402 Value *FourtyTwo = IRB.getInt32(42);
403 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
404 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
405
406 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
407 EXPECT_EQ(Alloca, MatchLoad);
408
409 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
410
411 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
412
413 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
414 .match(StoreInst));
415 EXPECT_EQ(FourtyTwo, MatchStoreVal);
416 EXPECT_EQ(Alloca, MatchStorePointer);
417
418 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
419 .match(Alloca));
420
421 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
422 .match(StoreInst));
423 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
424 .match(StoreInst));
425 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
426 .match(StoreInst));
427 }
428
TEST_F(PatternMatchTest,VectorOps)429 TEST_F(PatternMatchTest, VectorOps) {
430 // Build up small tree of vector operations
431 //
432 // Val = 0 + 1
433 // Val2 = Val + 3
434 // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef>
435 // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4>
436 // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4>
437 // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2>
438 //
439 // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer
440 // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2>
441 // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer
442 // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer
443 //
444 // SP1 = VectorSplat(2, i8 2)
445 // SP2 = VectorSplat(2, i8 %Val)
446 Type *VecTy = VectorType::get(IRB.getInt8Ty(), 2);
447 Type *i32 = IRB.getInt32Ty();
448 Type *i32VecTy = VectorType::get(i32, 2);
449
450 Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1));
451 Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3));
452
453 SmallVector<Constant *, 2> VecElemIdxs;
454 VecElemIdxs.push_back(ConstantInt::get(i32, 0));
455 VecElemIdxs.push_back(ConstantInt::get(i32, 2));
456 auto *IdxVec = ConstantVector::get(VecElemIdxs);
457
458 Value *UndefVec = UndefValue::get(VecTy);
459 Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
460 Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
461 Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
462 Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
463
464 Value *EX1 = IRB.CreateExtractElement(VI4, Val);
465 Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0);
466 Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1);
467
468 Value *Zero = ConstantAggregateZero::get(i32VecTy);
469 Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero);
470 Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec);
471 Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero);
472 Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero);
473
474 Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2));
475 Value *SP2 = IRB.CreateVectorSplat(2, Val);
476
477 Value *A = nullptr, *B = nullptr, *C = nullptr;
478
479 // Test matching insertelement
480 EXPECT_TRUE(match(VI1, m_InsertElement(m_Value(), m_Value(), m_Value())));
481 EXPECT_TRUE(
482 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_ConstantInt())));
483 EXPECT_TRUE(
484 match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_Zero())));
485 EXPECT_TRUE(
486 match(VI1, m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero())));
487 EXPECT_TRUE(match(VI2, m_InsertElement(m_Value(), m_Value(), m_Value())));
488 EXPECT_FALSE(
489 match(VI2, m_InsertElement(m_Value(), m_Value(), m_ConstantInt())));
490 EXPECT_FALSE(
491 match(VI2, m_InsertElement(m_Value(), m_ConstantInt(), m_Value())));
492 EXPECT_FALSE(match(VI2, m_InsertElement(m_Constant(), m_Value(), m_Value())));
493 EXPECT_TRUE(match(VI3, m_InsertElement(m_Value(A), m_Value(B), m_Value(C))));
494 EXPECT_TRUE(A == VI1);
495 EXPECT_TRUE(B == Val2);
496 EXPECT_TRUE(isa<ConstantInt>(C));
497 A = B = C = nullptr; // reset
498
499 // Test matching extractelement
500 EXPECT_TRUE(match(EX1, m_ExtractElement(m_Value(A), m_Value(B))));
501 EXPECT_TRUE(A == VI4);
502 EXPECT_TRUE(B == Val);
503 A = B = C = nullptr; // reset
504 EXPECT_FALSE(match(EX1, m_ExtractElement(m_Value(), m_ConstantInt())));
505 EXPECT_TRUE(match(EX2, m_ExtractElement(m_Value(), m_ConstantInt())));
506 EXPECT_TRUE(match(EX3, m_ExtractElement(m_Constant(), m_ConstantInt())));
507
508 // Test matching shufflevector
509 EXPECT_TRUE(match(SI1, m_ShuffleVector(m_Value(), m_Undef(), m_Zero())));
510 EXPECT_TRUE(match(SI2, m_ShuffleVector(m_Value(A), m_Value(B), m_Value(C))));
511 EXPECT_TRUE(A == VI3);
512 EXPECT_TRUE(B == VI4);
513 EXPECT_TRUE(C == IdxVec);
514 A = B = C = nullptr; // reset
515
516 // Test matching the vector splat pattern
517 EXPECT_TRUE(match(
518 SI1,
519 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()),
520 m_Undef(), m_Zero())));
521 EXPECT_FALSE(match(
522 SI3, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
523 m_Undef(), m_Zero())));
524 EXPECT_FALSE(match(
525 SI4, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()),
526 m_Undef(), m_Zero())));
527 EXPECT_TRUE(match(
528 SP1,
529 m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(2), m_Zero()),
530 m_Undef(), m_Zero())));
531 EXPECT_TRUE(match(
532 SP2, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(A), m_Zero()),
533 m_Undef(), m_Zero())));
534 EXPECT_TRUE(A == Val);
535 }
536
537 template <typename T> struct MutableConstTest : PatternMatchTest { };
538
539 typedef ::testing::Types<std::tuple<Value*, Instruction*>,
540 std::tuple<const Value*, const Instruction *>>
541 MutableConstTestTypes;
542 TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes);
543
TYPED_TEST(MutableConstTest,ICmp)544 TYPED_TEST(MutableConstTest, ICmp) {
545 auto &IRB = PatternMatchTest::IRB;
546
547 typedef typename std::tuple_element<0, TypeParam>::type ValueType;
548 typedef typename std::tuple_element<1, TypeParam>::type InstructionType;
549
550 Value *L = IRB.getInt32(1);
551 Value *R = IRB.getInt32(2);
552 ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT;
553
554 ValueType MatchL;
555 ValueType MatchR;
556 ICmpInst::Predicate MatchPred;
557
558 EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR))
559 .match((InstructionType)IRB.CreateICmp(Pred, L, R)));
560 EXPECT_EQ(L, MatchL);
561 EXPECT_EQ(R, MatchR);
562 }
563
564 } // anonymous namespace.
565