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