1 //===- llvm/unittests/Transforms/Vectorize/VPlanTest.cpp - VPlan tests ----===//
2 //
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "../lib/Transforms/Vectorize/VPlan.h"
11 #include "llvm/Analysis/VectorUtils.h"
12 #include "llvm/IR/Instruction.h"
13 #include "llvm/IR/Instructions.h"
14 #include "gtest/gtest.h"
15 #include <string>
16 
17 namespace llvm {
18 namespace {
19 
20 #define CHECK_ITERATOR(Range1, ...)                                            \
21   do {                                                                         \
22     std::vector<VPInstruction *> Tmp = {__VA_ARGS__};                          \
23     EXPECT_EQ((size_t)std::distance(Range1.begin(), Range1.end()),             \
24               Tmp.size());                                                     \
25     for (auto Pair : zip(Range1, make_range(Tmp.begin(), Tmp.end())))          \
26       EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));                        \
27   } while (0)
28 
29 TEST(VPInstructionTest, insertBefore) {
30   VPInstruction *I1 = new VPInstruction(0, {});
31   VPInstruction *I2 = new VPInstruction(1, {});
32   VPInstruction *I3 = new VPInstruction(2, {});
33 
34   VPBasicBlock VPBB1;
35   VPBB1.appendRecipe(I1);
36 
37   I2->insertBefore(I1);
38   CHECK_ITERATOR(VPBB1, I2, I1);
39 
40   I3->insertBefore(I2);
41   CHECK_ITERATOR(VPBB1, I3, I2, I1);
42 }
43 
44 TEST(VPInstructionTest, eraseFromParent) {
45   VPInstruction *I1 = new VPInstruction(0, {});
46   VPInstruction *I2 = new VPInstruction(1, {});
47   VPInstruction *I3 = new VPInstruction(2, {});
48 
49   VPBasicBlock VPBB1;
50   VPBB1.appendRecipe(I1);
51   VPBB1.appendRecipe(I2);
52   VPBB1.appendRecipe(I3);
53 
54   I2->eraseFromParent();
55   CHECK_ITERATOR(VPBB1, I1, I3);
56 
57   I1->eraseFromParent();
58   CHECK_ITERATOR(VPBB1, I3);
59 
60   I3->eraseFromParent();
61   EXPECT_TRUE(VPBB1.empty());
62 }
63 
64 TEST(VPInstructionTest, moveAfter) {
65   VPInstruction *I1 = new VPInstruction(0, {});
66   VPInstruction *I2 = new VPInstruction(1, {});
67   VPInstruction *I3 = new VPInstruction(2, {});
68 
69   VPBasicBlock VPBB1;
70   VPBB1.appendRecipe(I1);
71   VPBB1.appendRecipe(I2);
72   VPBB1.appendRecipe(I3);
73 
74   I1->moveAfter(I2);
75 
76   CHECK_ITERATOR(VPBB1, I2, I1, I3);
77 
78   VPInstruction *I4 = new VPInstruction(4, {});
79   VPInstruction *I5 = new VPInstruction(5, {});
80   VPBasicBlock VPBB2;
81   VPBB2.appendRecipe(I4);
82   VPBB2.appendRecipe(I5);
83 
84   I3->moveAfter(I4);
85 
86   CHECK_ITERATOR(VPBB1, I2, I1);
87   CHECK_ITERATOR(VPBB2, I4, I3, I5);
88   EXPECT_EQ(I3->getParent(), I4->getParent());
89 }
90 
91 TEST(VPInstructionTest, setOperand) {
92   VPValue *VPV1 = new VPValue();
93   VPValue *VPV2 = new VPValue();
94   VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
95   EXPECT_EQ(1u, VPV1->getNumUsers());
96   EXPECT_EQ(I1, *VPV1->user_begin());
97   EXPECT_EQ(1u, VPV2->getNumUsers());
98   EXPECT_EQ(I1, *VPV2->user_begin());
99 
100   // Replace operand 0 (VPV1) with VPV3.
101   VPValue *VPV3 = new VPValue();
102   I1->setOperand(0, VPV3);
103   EXPECT_EQ(0u, VPV1->getNumUsers());
104   EXPECT_EQ(1u, VPV2->getNumUsers());
105   EXPECT_EQ(I1, *VPV2->user_begin());
106   EXPECT_EQ(1u, VPV3->getNumUsers());
107   EXPECT_EQ(I1, *VPV3->user_begin());
108 
109   // Replace operand 1 (VPV2) with VPV3.
110   I1->setOperand(1, VPV3);
111   EXPECT_EQ(0u, VPV1->getNumUsers());
112   EXPECT_EQ(0u, VPV2->getNumUsers());
113   EXPECT_EQ(2u, VPV3->getNumUsers());
114   EXPECT_EQ(I1, *VPV3->user_begin());
115   EXPECT_EQ(I1, *std::next(VPV3->user_begin()));
116 
117   // Replace operand 0 (VPV3) with VPV4.
118   VPValue *VPV4 = new VPValue();
119   I1->setOperand(0, VPV4);
120   EXPECT_EQ(1u, VPV3->getNumUsers());
121   EXPECT_EQ(I1, *VPV3->user_begin());
122   EXPECT_EQ(I1, *VPV4->user_begin());
123 
124   // Replace operand 1 (VPV3) with VPV4.
125   I1->setOperand(1, VPV4);
126   EXPECT_EQ(0u, VPV3->getNumUsers());
127   EXPECT_EQ(I1, *VPV4->user_begin());
128   EXPECT_EQ(I1, *std::next(VPV4->user_begin()));
129 
130   delete I1;
131   delete VPV1;
132   delete VPV2;
133   delete VPV3;
134   delete VPV4;
135 }
136 
137 TEST(VPInstructionTest, replaceAllUsesWith) {
138   VPValue *VPV1 = new VPValue();
139   VPValue *VPV2 = new VPValue();
140   VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
141 
142   // Replace all uses of VPV1 with VPV3.
143   VPValue *VPV3 = new VPValue();
144   VPV1->replaceAllUsesWith(VPV3);
145   EXPECT_EQ(VPV3, I1->getOperand(0));
146   EXPECT_EQ(VPV2, I1->getOperand(1));
147   EXPECT_EQ(0u, VPV1->getNumUsers());
148   EXPECT_EQ(1u, VPV2->getNumUsers());
149   EXPECT_EQ(I1, *VPV2->user_begin());
150   EXPECT_EQ(1u, VPV3->getNumUsers());
151   EXPECT_EQ(I1, *VPV3->user_begin());
152 
153   // Replace all uses of VPV2 with VPV3.
154   VPV2->replaceAllUsesWith(VPV3);
155   EXPECT_EQ(VPV3, I1->getOperand(0));
156   EXPECT_EQ(VPV3, I1->getOperand(1));
157   EXPECT_EQ(0u, VPV1->getNumUsers());
158   EXPECT_EQ(0u, VPV2->getNumUsers());
159   EXPECT_EQ(2u, VPV3->getNumUsers());
160   EXPECT_EQ(I1, *VPV3->user_begin());
161 
162   // Replace all uses of VPV3 with VPV1.
163   VPV3->replaceAllUsesWith(VPV1);
164   EXPECT_EQ(VPV1, I1->getOperand(0));
165   EXPECT_EQ(VPV1, I1->getOperand(1));
166   EXPECT_EQ(2u, VPV1->getNumUsers());
167   EXPECT_EQ(I1, *VPV1->user_begin());
168   EXPECT_EQ(0u, VPV2->getNumUsers());
169   EXPECT_EQ(0u, VPV3->getNumUsers());
170 
171   VPInstruction *I2 = new VPInstruction(0, {VPV1, VPV2});
172   EXPECT_EQ(3u, VPV1->getNumUsers());
173   VPV1->replaceAllUsesWith(VPV3);
174   EXPECT_EQ(3u, VPV3->getNumUsers());
175 
176   delete I1;
177   delete I2;
178   delete VPV1;
179   delete VPV2;
180   delete VPV3;
181 }
182 
183 TEST(VPInstructionTest, releaseOperandsAtDeletion) {
184   VPValue *VPV1 = new VPValue();
185   VPValue *VPV2 = new VPValue();
186   VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
187 
188   EXPECT_EQ(1u, VPV1->getNumUsers());
189   EXPECT_EQ(I1, *VPV1->user_begin());
190   EXPECT_EQ(1u, VPV2->getNumUsers());
191   EXPECT_EQ(I1, *VPV2->user_begin());
192 
193   delete I1;
194 
195   EXPECT_EQ(0u, VPV1->getNumUsers());
196   EXPECT_EQ(0u, VPV2->getNumUsers());
197 
198   delete VPV1;
199   delete VPV2;
200 }
201 TEST(VPBasicBlockTest, getPlan) {
202   {
203     VPBasicBlock *VPBB1 = new VPBasicBlock();
204     VPBasicBlock *VPBB2 = new VPBasicBlock();
205     VPBasicBlock *VPBB3 = new VPBasicBlock();
206     VPBasicBlock *VPBB4 = new VPBasicBlock();
207 
208     //     VPBB1
209     //     /   \
210     // VPBB2  VPBB3
211     //    \    /
212     //    VPBB4
213     VPBlockUtils::connectBlocks(VPBB1, VPBB2);
214     VPBlockUtils::connectBlocks(VPBB1, VPBB3);
215     VPBlockUtils::connectBlocks(VPBB2, VPBB4);
216     VPBlockUtils::connectBlocks(VPBB3, VPBB4);
217 
218     VPlan Plan;
219     Plan.setEntry(VPBB1);
220 
221     EXPECT_EQ(&Plan, VPBB1->getPlan());
222     EXPECT_EQ(&Plan, VPBB2->getPlan());
223     EXPECT_EQ(&Plan, VPBB3->getPlan());
224     EXPECT_EQ(&Plan, VPBB4->getPlan());
225   }
226 
227   {
228     // Region block is entry into VPlan.
229     VPBasicBlock *R1BB1 = new VPBasicBlock();
230     VPBasicBlock *R1BB2 = new VPBasicBlock();
231     VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
232     VPBlockUtils::connectBlocks(R1BB1, R1BB2);
233 
234     VPlan Plan;
235     Plan.setEntry(R1);
236     EXPECT_EQ(&Plan, R1->getPlan());
237     EXPECT_EQ(&Plan, R1BB1->getPlan());
238     EXPECT_EQ(&Plan, R1BB2->getPlan());
239   }
240 
241   {
242     // VPBasicBlock is the entry into the VPlan, followed by a region.
243     VPBasicBlock *R1BB1 = new VPBasicBlock();
244     VPBasicBlock *R1BB2 = new VPBasicBlock();
245     VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
246     VPBlockUtils::connectBlocks(R1BB1, R1BB2);
247 
248     VPBasicBlock *VPBB1 = new VPBasicBlock();
249     VPBlockUtils::connectBlocks(VPBB1, R1);
250 
251     VPlan Plan;
252     Plan.setEntry(VPBB1);
253     EXPECT_EQ(&Plan, VPBB1->getPlan());
254     EXPECT_EQ(&Plan, R1->getPlan());
255     EXPECT_EQ(&Plan, R1BB1->getPlan());
256     EXPECT_EQ(&Plan, R1BB2->getPlan());
257   }
258 
259   {
260     VPBasicBlock *R1BB1 = new VPBasicBlock();
261     VPBasicBlock *R1BB2 = new VPBasicBlock();
262     VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
263     VPBlockUtils::connectBlocks(R1BB1, R1BB2);
264 
265     VPBasicBlock *R2BB1 = new VPBasicBlock();
266     VPBasicBlock *R2BB2 = new VPBasicBlock();
267     VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R2BB2, "R2");
268     VPBlockUtils::connectBlocks(R2BB1, R2BB2);
269 
270     VPBasicBlock *VPBB1 = new VPBasicBlock();
271     VPBlockUtils::connectBlocks(VPBB1, R1);
272     VPBlockUtils::connectBlocks(VPBB1, R2);
273 
274     VPBasicBlock *VPBB2 = new VPBasicBlock();
275     VPBlockUtils::connectBlocks(R1, VPBB2);
276     VPBlockUtils::connectBlocks(R2, VPBB2);
277 
278     VPlan Plan;
279     Plan.setEntry(VPBB1);
280     EXPECT_EQ(&Plan, VPBB1->getPlan());
281     EXPECT_EQ(&Plan, R1->getPlan());
282     EXPECT_EQ(&Plan, R1BB1->getPlan());
283     EXPECT_EQ(&Plan, R1BB2->getPlan());
284     EXPECT_EQ(&Plan, R2->getPlan());
285     EXPECT_EQ(&Plan, R2BB1->getPlan());
286     EXPECT_EQ(&Plan, R2BB2->getPlan());
287     EXPECT_EQ(&Plan, VPBB2->getPlan());
288   }
289 }
290 
291 TEST(VPBasicBlockTest, print) {
292   VPInstruction *I1 = new VPInstruction(Instruction::Add, {});
293   VPInstruction *I2 = new VPInstruction(Instruction::Sub, {I1});
294   VPInstruction *I3 = new VPInstruction(Instruction::Br, {I1, I2});
295 
296   VPBasicBlock *VPBB1 = new VPBasicBlock();
297   VPBB1->appendRecipe(I1);
298   VPBB1->appendRecipe(I2);
299   VPBB1->appendRecipe(I3);
300 
301   VPInstruction *I4 = new VPInstruction(Instruction::Mul, {I2, I1});
302   VPInstruction *I5 = new VPInstruction(Instruction::Ret, {I4});
303   VPBasicBlock *VPBB2 = new VPBasicBlock();
304   VPBB2->appendRecipe(I4);
305   VPBB2->appendRecipe(I5);
306 
307   VPBlockUtils::connectBlocks(VPBB1, VPBB2);
308 
309   // Check printing an instruction without associated VPlan.
310   {
311     std::string I3Dump;
312     raw_string_ostream OS(I3Dump);
313     I3->print(OS);
314     OS.flush();
315     EXPECT_EQ("br <badref> <badref>", I3Dump);
316   }
317 
318   VPlan Plan;
319   Plan.setEntry(VPBB1);
320   std::string FullDump;
321   raw_string_ostream(FullDump) << Plan;
322 
323   const char *ExpectedStr = R"(digraph VPlan {
324 graph [labelloc=t, fontsize=30; label="Vectorization Plan"]
325 node [shape=rect, fontname=Courier, fontsize=30]
326 edge [fontname=Courier, fontsize=30]
327 compound=true
328   N0 [label =
329     ":\n" +
330       "EMIT vp<%0> = add\l" +
331       "EMIT vp<%1> = sub vp<%0>\l" +
332       "EMIT br vp<%0> vp<%1>\l"
333   ]
334   N0 -> N1 [ label=""]
335   N1 [label =
336     ":\n" +
337       "EMIT vp<%2> = mul vp<%1> vp<%0>\l" +
338       "EMIT ret vp<%2>\l"
339   ]
340 }
341 )";
342   EXPECT_EQ(ExpectedStr, FullDump);
343 
344   {
345     std::string I3Dump;
346     raw_string_ostream OS(I3Dump);
347     I3->print(OS);
348     OS.flush();
349     EXPECT_EQ("br vp<%0> vp<%1>", I3Dump);
350   }
351 
352   {
353     std::string I4Dump;
354     raw_string_ostream OS(I4Dump);
355     OS << *I4;
356     OS.flush();
357     EXPECT_EQ("vp<%2> = mul vp<%1> vp<%0>", I4Dump);
358   }
359 }
360 
361 TEST(VPRecipeTest, CastVPInstructionToVPUser) {
362   VPValue Op1;
363   VPValue Op2;
364   VPInstruction Recipe(Instruction::Add, {&Op1, &Op2});
365   EXPECT_TRUE(isa<VPUser>(&Recipe));
366   VPRecipeBase *BaseR = &Recipe;
367   EXPECT_TRUE(isa<VPUser>(BaseR));
368   EXPECT_EQ(&Recipe, BaseR->toVPUser());
369 }
370 
371 TEST(VPRecipeTest, CastVPWidenRecipeToVPUser) {
372   LLVMContext C;
373 
374   IntegerType *Int32 = IntegerType::get(C, 32);
375   auto *AI =
376       BinaryOperator::CreateAdd(UndefValue::get(Int32), UndefValue::get(Int32));
377   VPValue Op1;
378   VPValue Op2;
379   SmallVector<VPValue *, 2> Args;
380   Args.push_back(&Op1);
381   Args.push_back(&Op1);
382   VPWidenRecipe WidenR(*AI, make_range(Args.begin(), Args.end()));
383   EXPECT_TRUE(isa<VPUser>(&WidenR));
384   VPRecipeBase *WidenRBase = &WidenR;
385   EXPECT_TRUE(isa<VPUser>(WidenRBase));
386   EXPECT_EQ(&WidenR, WidenRBase->toVPUser());
387   delete AI;
388 }
389 
390 TEST(VPRecipeTest, CastVPWidenCallRecipeToVPUser) {
391   LLVMContext C;
392 
393   IntegerType *Int32 = IntegerType::get(C, 32);
394   FunctionType *FTy = FunctionType::get(Int32, false);
395   auto *Call = CallInst::Create(FTy, UndefValue::get(FTy));
396   VPValue Op1;
397   VPValue Op2;
398   SmallVector<VPValue *, 2> Args;
399   Args.push_back(&Op1);
400   Args.push_back(&Op2);
401   VPWidenCallRecipe Recipe(*Call, make_range(Args.begin(), Args.end()));
402   EXPECT_TRUE(isa<VPUser>(&Recipe));
403   VPRecipeBase *BaseR = &Recipe;
404   EXPECT_TRUE(isa<VPUser>(BaseR));
405   EXPECT_EQ(&Recipe, BaseR->toVPUser());
406   delete Call;
407 }
408 
409 TEST(VPRecipeTest, CastVPWidenSelectRecipeToVPUser) {
410   LLVMContext C;
411 
412   IntegerType *Int1 = IntegerType::get(C, 1);
413   IntegerType *Int32 = IntegerType::get(C, 32);
414   auto *SelectI = SelectInst::Create(
415       UndefValue::get(Int1), UndefValue::get(Int32), UndefValue::get(Int32));
416   VPValue Op1;
417   VPValue Op2;
418   VPValue Op3;
419   SmallVector<VPValue *, 4> Args;
420   Args.push_back(&Op1);
421   Args.push_back(&Op2);
422   Args.push_back(&Op3);
423   VPWidenSelectRecipe WidenSelectR(*SelectI,
424                                    make_range(Args.begin(), Args.end()), false);
425   EXPECT_TRUE(isa<VPUser>(&WidenSelectR));
426   VPRecipeBase *BaseR = &WidenSelectR;
427   EXPECT_TRUE(isa<VPUser>(BaseR));
428   EXPECT_EQ(&WidenSelectR, BaseR->toVPUser());
429   delete SelectI;
430 }
431 
432 TEST(VPRecipeTest, CastVPWidenGEPRecipeToVPUser) {
433   LLVMContext C;
434 
435   IntegerType *Int32 = IntegerType::get(C, 32);
436   PointerType *Int32Ptr = PointerType::get(Int32, 0);
437   auto *GEP = GetElementPtrInst::Create(Int32, UndefValue::get(Int32Ptr),
438                                         UndefValue::get(Int32));
439   VPValue Op1;
440   VPValue Op2;
441   SmallVector<VPValue *, 4> Args;
442   Args.push_back(&Op1);
443   Args.push_back(&Op2);
444   VPWidenGEPRecipe Recipe(GEP, make_range(Args.begin(), Args.end()));
445   EXPECT_TRUE(isa<VPUser>(&Recipe));
446   VPRecipeBase *BaseR = &Recipe;
447   EXPECT_TRUE(isa<VPUser>(BaseR));
448   EXPECT_EQ(&Recipe, BaseR->toVPUser());
449   delete GEP;
450 }
451 
452 TEST(VPRecipeTest, CastVPBlendRecipeToVPUser) {
453   LLVMContext C;
454 
455   IntegerType *Int32 = IntegerType::get(C, 32);
456   auto *Phi = PHINode::Create(Int32, 1);
457   VPValue Op1;
458   VPValue Op2;
459   SmallVector<VPValue *, 4> Args;
460   Args.push_back(&Op1);
461   Args.push_back(&Op2);
462   VPBlendRecipe Recipe(Phi, Args);
463   EXPECT_TRUE(isa<VPUser>(&Recipe));
464   VPRecipeBase *BaseR = &Recipe;
465   EXPECT_TRUE(isa<VPUser>(BaseR));
466   delete Phi;
467 }
468 
469 TEST(VPRecipeTest, CastVPInterleaveRecipeToVPUser) {
470   LLVMContext C;
471 
472   VPValue Addr;
473   VPValue Mask;
474   InterleaveGroup<Instruction> IG(4, false, Align(4));
475   VPInterleaveRecipe Recipe(&IG, &Addr, {}, &Mask);
476   EXPECT_TRUE(isa<VPUser>(&Recipe));
477   VPRecipeBase *BaseR = &Recipe;
478   EXPECT_TRUE(isa<VPUser>(BaseR));
479   EXPECT_EQ(&Recipe, BaseR->toVPUser());
480 }
481 
482 TEST(VPRecipeTest, CastVPReplicateRecipeToVPUser) {
483   LLVMContext C;
484 
485   VPValue Op1;
486   VPValue Op2;
487   SmallVector<VPValue *, 4> Args;
488   Args.push_back(&Op1);
489   Args.push_back(&Op2);
490 
491   VPReplicateRecipe Recipe(nullptr, make_range(Args.begin(), Args.end()), true,
492                            false);
493   EXPECT_TRUE(isa<VPUser>(&Recipe));
494   VPRecipeBase *BaseR = &Recipe;
495   EXPECT_TRUE(isa<VPUser>(BaseR));
496 }
497 
498 TEST(VPRecipeTest, CastVPBranchOnMaskRecipeToVPUser) {
499   LLVMContext C;
500 
501   VPValue Mask;
502   VPBranchOnMaskRecipe Recipe(&Mask);
503   EXPECT_TRUE(isa<VPUser>(&Recipe));
504   VPRecipeBase *BaseR = &Recipe;
505   EXPECT_TRUE(isa<VPUser>(BaseR));
506   EXPECT_EQ(&Recipe, BaseR->toVPUser());
507 }
508 
509 TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUser) {
510   LLVMContext C;
511 
512   IntegerType *Int32 = IntegerType::get(C, 32);
513   PointerType *Int32Ptr = PointerType::get(Int32, 0);
514   auto *Load =
515       new LoadInst(Int32, UndefValue::get(Int32Ptr), "", false, Align(1));
516   VPValue Addr;
517   VPValue Mask;
518   VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask);
519   EXPECT_TRUE(isa<VPUser>(&Recipe));
520   VPRecipeBase *BaseR = &Recipe;
521   EXPECT_TRUE(isa<VPUser>(BaseR));
522   EXPECT_EQ(&Recipe, BaseR->toVPUser());
523   delete Load;
524 }
525 
526 TEST(VPRecipeTest, CastVPReductionRecipeToVPUser) {
527   LLVMContext C;
528 
529   VPValue ChainOp;
530   VPValue VecOp;
531   VPValue CondOp;
532   VPReductionRecipe Recipe(nullptr, nullptr, &ChainOp, &CondOp, &VecOp, false,
533                            nullptr);
534   EXPECT_TRUE(isa<VPUser>(&Recipe));
535   VPRecipeBase *BaseR = &Recipe;
536   EXPECT_TRUE(isa<VPUser>(BaseR));
537 }
538 
539 struct VPDoubleValueDef : public VPUser, public VPDef {
540   VPDoubleValueDef(ArrayRef<VPValue *> Operands) : VPUser(Operands), VPDef() {
541     new VPValue(nullptr, this);
542     new VPValue(nullptr, this);
543   }
544 };
545 
546 TEST(VPDoubleValueDefTest, traverseUseLists) {
547   // Check that the def-use chains of a multi-def can be traversed in both
548   // directions.
549 
550   // Create a new VPDef which defines 2 values and has 2 operands.
551   VPInstruction Op0(20, {});
552   VPInstruction Op1(30, {});
553   VPDoubleValueDef DoubleValueDef({&Op0, &Op1});
554 
555   // Create a new users of the defined values.
556   VPInstruction I1(
557       1, {DoubleValueDef.getVPValue(0), DoubleValueDef.getVPValue(1)});
558   VPInstruction I2(2, {DoubleValueDef.getVPValue(0)});
559   VPInstruction I3(3, {DoubleValueDef.getVPValue(1)});
560 
561   // Check operands of the VPDef (traversing upwards).
562   SmallVector<VPValue *, 4> DoubleOperands(DoubleValueDef.op_begin(),
563                                            DoubleValueDef.op_end());
564   EXPECT_EQ(2u, DoubleOperands.size());
565   EXPECT_EQ(&Op0, DoubleOperands[0]);
566   EXPECT_EQ(&Op1, DoubleOperands[1]);
567 
568   // Check users of the defined values (traversing downwards).
569   SmallVector<VPUser *, 4> DoubleValueDefV0Users(
570       DoubleValueDef.getVPValue(0)->user_begin(),
571       DoubleValueDef.getVPValue(0)->user_end());
572   EXPECT_EQ(2u, DoubleValueDefV0Users.size());
573   EXPECT_EQ(&I1, DoubleValueDefV0Users[0]);
574   EXPECT_EQ(&I2, DoubleValueDefV0Users[1]);
575 
576   SmallVector<VPUser *, 4> DoubleValueDefV1Users(
577       DoubleValueDef.getVPValue(1)->user_begin(),
578       DoubleValueDef.getVPValue(1)->user_end());
579   EXPECT_EQ(2u, DoubleValueDefV1Users.size());
580   EXPECT_EQ(&I1, DoubleValueDefV1Users[0]);
581   EXPECT_EQ(&I3, DoubleValueDefV1Users[1]);
582 
583   // Now check that we can get the right VPDef for each defined value.
584   EXPECT_EQ(&DoubleValueDef, I1.getOperand(0)->getDef());
585   EXPECT_EQ(&DoubleValueDef, I1.getOperand(1)->getDef());
586   EXPECT_EQ(&DoubleValueDef, I2.getOperand(0)->getDef());
587   EXPECT_EQ(&DoubleValueDef, I3.getOperand(0)->getDef());
588 }
589 
590 } // namespace
591 } // namespace llvm
592