Debian Codegraph @

llvm-project/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

raw
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 "../lib/Transforms/Vectorize/VPlanCFG.h"
12 #include "llvm/ADT/DepthFirstIterator.h"
13 #include "llvm/ADT/PostOrderIterator.h"
14 #include "llvm/Analysis/VectorUtils.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/IR/Instructions.h"
17 #include "gtest/gtest.h"
18 #include <string>
19
20 namespace llvm {
21 namespace {
22
23 #define CHECK_ITERATOR(Range1, ...) \
24 do { \
25 std::vector<VPInstruction *> Tmp = {__VA_ARGS__}; \
26 EXPECT_EQ((size_t)std::distance(Range1.begin(), Range1.end()), \
27 Tmp.size()); \
28 for (auto Pair : zip(Range1, make_range(Tmp.begin(), Tmp.end()))) \
29 EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair)); \
30 } while (0)
31
32 TEST(VPInstructionTest, insertBefore) {
33 VPInstruction *I1 = new VPInstruction(0, {});
34 VPInstruction *I2 = new VPInstruction(1, {});
35 VPInstruction *I3 = new VPInstruction(2, {});
36
37 VPBasicBlock VPBB1;
38 VPBB1.appendRecipe(I1);
39
40 I2->insertBefore(I1);
41 CHECK_ITERATOR(VPBB1, I2, I1);
42
43 I3->insertBefore(I2);
44 CHECK_ITERATOR(VPBB1, I3, I2, I1);
45 }
46
47 TEST(VPInstructionTest, eraseFromParent) {
48 VPInstruction *I1 = new VPInstruction(0, {});
49 VPInstruction *I2 = new VPInstruction(1, {});
50 VPInstruction *I3 = new VPInstruction(2, {});
51
52 VPBasicBlock VPBB1;
53 VPBB1.appendRecipe(I1);
54 VPBB1.appendRecipe(I2);
55 VPBB1.appendRecipe(I3);
56
57 I2->eraseFromParent();
58 CHECK_ITERATOR(VPBB1, I1, I3);
59
60 I1->eraseFromParent();
61 CHECK_ITERATOR(VPBB1, I3);
62
63 I3->eraseFromParent();
64 EXPECT_TRUE(VPBB1.empty());
65 }
66
67 TEST(VPInstructionTest, moveAfter) {
68 VPInstruction *I1 = new VPInstruction(0, {});
69 VPInstruction *I2 = new VPInstruction(1, {});
70 VPInstruction *I3 = new VPInstruction(2, {});
71
72 VPBasicBlock VPBB1;
73 VPBB1.appendRecipe(I1);
74 VPBB1.appendRecipe(I2);
75 VPBB1.appendRecipe(I3);
76
77 I1->moveAfter(I2);
78
79 CHECK_ITERATOR(VPBB1, I2, I1, I3);
80
81 VPInstruction *I4 = new VPInstruction(4, {});
82 VPInstruction *I5 = new VPInstruction(5, {});
83 VPBasicBlock VPBB2;
84 VPBB2.appendRecipe(I4);
85 VPBB2.appendRecipe(I5);
86
87 I3->moveAfter(I4);
88
89 CHECK_ITERATOR(VPBB1, I2, I1);
90 CHECK_ITERATOR(VPBB2, I4, I3, I5);
91 EXPECT_EQ(I3->getParent(), I4->getParent());
92 }
93
94 TEST(VPInstructionTest, moveBefore) {
95 VPInstruction *I1 = new VPInstruction(0, {});
96 VPInstruction *I2 = new VPInstruction(1, {});
97 VPInstruction *I3 = new VPInstruction(2, {});
98
99 VPBasicBlock VPBB1;
100 VPBB1.appendRecipe(I1);
101 VPBB1.appendRecipe(I2);
102 VPBB1.appendRecipe(I3);
103
104 I1->moveBefore(VPBB1, I3->getIterator());
105
106 CHECK_ITERATOR(VPBB1, I2, I1, I3);
107
108 VPInstruction *I4 = new VPInstruction(4, {});
109 VPInstruction *I5 = new VPInstruction(5, {});
110 VPBasicBlock VPBB2;
111 VPBB2.appendRecipe(I4);
112 VPBB2.appendRecipe(I5);
113
114 I3->moveBefore(VPBB2, I4->getIterator());
115
116 CHECK_ITERATOR(VPBB1, I2, I1);
117 CHECK_ITERATOR(VPBB2, I3, I4, I5);
118 EXPECT_EQ(I3->getParent(), I4->getParent());
119
120 VPBasicBlock VPBB3;
121
122 I4->moveBefore(VPBB3, VPBB3.end());
123
124 CHECK_ITERATOR(VPBB1, I2, I1);
125 CHECK_ITERATOR(VPBB2, I3, I5);
126 CHECK_ITERATOR(VPBB3, I4);
127 EXPECT_EQ(&VPBB3, I4->getParent());
128 }
129
130 TEST(VPInstructionTest, setOperand) {
131 VPValue *VPV1 = new VPValue();
132 VPValue *VPV2 = new VPValue();
133 VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
134 EXPECT_EQ(1u, VPV1->getNumUsers());
135 EXPECT_EQ(I1, *VPV1->user_begin());
136 EXPECT_EQ(1u, VPV2->getNumUsers());
137 EXPECT_EQ(I1, *VPV2->user_begin());
138
139 // Replace operand 0 (VPV1) with VPV3.
140 VPValue *VPV3 = new VPValue();
141 I1->setOperand(0, VPV3);
142 EXPECT_EQ(0u, VPV1->getNumUsers());
143 EXPECT_EQ(1u, VPV2->getNumUsers());
144 EXPECT_EQ(I1, *VPV2->user_begin());
145 EXPECT_EQ(1u, VPV3->getNumUsers());
146 EXPECT_EQ(I1, *VPV3->user_begin());
147
148 // Replace operand 1 (VPV2) with VPV3.
149 I1->setOperand(1, VPV3);
150 EXPECT_EQ(0u, VPV1->getNumUsers());
151 EXPECT_EQ(0u, VPV2->getNumUsers());
152 EXPECT_EQ(2u, VPV3->getNumUsers());
153 EXPECT_EQ(I1, *VPV3->user_begin());
154 EXPECT_EQ(I1, *std::next(VPV3->user_begin()));
155
156 // Replace operand 0 (VPV3) with VPV4.
157 VPValue *VPV4 = new VPValue();
158 I1->setOperand(0, VPV4);
159 EXPECT_EQ(1u, VPV3->getNumUsers());
160 EXPECT_EQ(I1, *VPV3->user_begin());
161 EXPECT_EQ(I1, *VPV4->user_begin());
162
163 // Replace operand 1 (VPV3) with VPV4.
164 I1->setOperand(1, VPV4);
165 EXPECT_EQ(0u, VPV3->getNumUsers());
166 EXPECT_EQ(I1, *VPV4->user_begin());
167 EXPECT_EQ(I1, *std::next(VPV4->user_begin()));
168
169 delete I1;
170 delete VPV1;
171 delete VPV2;
172 delete VPV3;
173 delete VPV4;
174 }
175
176 TEST(VPInstructionTest, replaceAllUsesWith) {
177 VPValue *VPV1 = new VPValue();
178 VPValue *VPV2 = new VPValue();
179 VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
180
181 // Replace all uses of VPV1 with VPV3.
182 VPValue *VPV3 = new VPValue();
183 VPV1->replaceAllUsesWith(VPV3);
184 EXPECT_EQ(VPV3, I1->getOperand(0));
185 EXPECT_EQ(VPV2, I1->getOperand(1));
186 EXPECT_EQ(0u, VPV1->getNumUsers());
187 EXPECT_EQ(1u, VPV2->getNumUsers());
188 EXPECT_EQ(I1, *VPV2->user_begin());
189 EXPECT_EQ(1u, VPV3->getNumUsers());
190 EXPECT_EQ(I1, *VPV3->user_begin());
191
192 // Replace all uses of VPV2 with VPV3.
193 VPV2->replaceAllUsesWith(VPV3);
194 EXPECT_EQ(VPV3, I1->getOperand(0));
195 EXPECT_EQ(VPV3, I1->getOperand(1));
196 EXPECT_EQ(0u, VPV1->getNumUsers());
197 EXPECT_EQ(0u, VPV2->getNumUsers());
198 EXPECT_EQ(2u, VPV3->getNumUsers());
199 EXPECT_EQ(I1, *VPV3->user_begin());
200
201 // Replace all uses of VPV3 with VPV1.
202 VPV3->replaceAllUsesWith(VPV1);
203 EXPECT_EQ(VPV1, I1->getOperand(0));
204 EXPECT_EQ(VPV1, I1->getOperand(1));
205 EXPECT_EQ(2u, VPV1->getNumUsers());
206 EXPECT_EQ(I1, *VPV1->user_begin());
207 EXPECT_EQ(0u, VPV2->getNumUsers());
208 EXPECT_EQ(0u, VPV3->getNumUsers());
209
210 VPInstruction *I2 = new VPInstruction(0, {VPV1, VPV2});
211 EXPECT_EQ(3u, VPV1->getNumUsers());
212 VPV1->replaceAllUsesWith(VPV3);
213 EXPECT_EQ(3u, VPV3->getNumUsers());
214
215 delete I1;
216 delete I2;
217 delete VPV1;
218 delete VPV2;
219 delete VPV3;
220 }
221
222 TEST(VPInstructionTest, releaseOperandsAtDeletion) {
223 VPValue *VPV1 = new VPValue();
224 VPValue *VPV2 = new VPValue();
225 VPInstruction *I1 = new VPInstruction(0, {VPV1, VPV2});
226
227 EXPECT_EQ(1u, VPV1->getNumUsers());
228 EXPECT_EQ(I1, *VPV1->user_begin());
229 EXPECT_EQ(1u, VPV2->getNumUsers());
230 EXPECT_EQ(I1, *VPV2->user_begin());
231
232 delete I1;
233
234 EXPECT_EQ(0u, VPV1->getNumUsers());
235 EXPECT_EQ(0u, VPV2->getNumUsers());
236
237 delete VPV1;
238 delete VPV2;
239 }
240 TEST(VPBasicBlockTest, getPlan) {
241 {
242 VPBasicBlock *VPBB1 = new VPBasicBlock();
243 VPBasicBlock *VPBB2 = new VPBasicBlock();
244 VPBasicBlock *VPBB3 = new VPBasicBlock();
245 VPBasicBlock *VPBB4 = new VPBasicBlock();
246
247 // VPBB1
248 // / \
249 // VPBB2 VPBB3
250 // \ /
251 // VPBB4
252 VPBlockUtils::connectBlocks(VPBB1, VPBB2);
253 VPBlockUtils::connectBlocks(VPBB1, VPBB3);
254 VPBlockUtils::connectBlocks(VPBB2, VPBB4);
255 VPBlockUtils::connectBlocks(VPBB3, VPBB4);
256
257 VPlan Plan;
258 Plan.setEntry(VPBB1);
259
260 EXPECT_EQ(&Plan, VPBB1->getPlan());
261 EXPECT_EQ(&Plan, VPBB2->getPlan());
262 EXPECT_EQ(&Plan, VPBB3->getPlan());
263 EXPECT_EQ(&Plan, VPBB4->getPlan());
264 }
265
266 {
267 // Region block is entry into VPlan.
268 VPBasicBlock *R1BB1 = new VPBasicBlock();
269 VPBasicBlock *R1BB2 = new VPBasicBlock();
270 VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
271 VPBlockUtils::connectBlocks(R1BB1, R1BB2);
272
273 VPlan Plan;
274 Plan.setEntry(R1);
275 EXPECT_EQ(&Plan, R1->getPlan());
276 EXPECT_EQ(&Plan, R1BB1->getPlan());
277 EXPECT_EQ(&Plan, R1BB2->getPlan());
278 }
279
280 {
281 // VPBasicBlock is the entry into the VPlan, followed by a region.
282 VPBasicBlock *R1BB1 = new VPBasicBlock();
283 VPBasicBlock *R1BB2 = new VPBasicBlock();
284 VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
285 VPBlockUtils::connectBlocks(R1BB1, R1BB2);
286
287 VPBasicBlock *VPBB1 = new VPBasicBlock();
288 VPBlockUtils::connectBlocks(VPBB1, R1);
289
290 VPlan Plan;
291 Plan.setEntry(VPBB1);
292 EXPECT_EQ(&Plan, VPBB1->getPlan());
293 EXPECT_EQ(&Plan, R1->getPlan());
294 EXPECT_EQ(&Plan, R1BB1->getPlan());
295 EXPECT_EQ(&Plan, R1BB2->getPlan());
296 }
297
298 {
299 VPBasicBlock *R1BB1 = new VPBasicBlock();
300 VPBasicBlock *R1BB2 = new VPBasicBlock();
301 VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB2, "R1");
302 VPBlockUtils::connectBlocks(R1BB1, R1BB2);
303
304 VPBasicBlock *R2BB1 = new VPBasicBlock();
305 VPBasicBlock *R2BB2 = new VPBasicBlock();
306 VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R2BB2, "R2");
307 VPBlockUtils::connectBlocks(R2BB1, R2BB2);
308
309 VPBasicBlock *VPBB1 = new VPBasicBlock();
310 VPBlockUtils::connectBlocks(VPBB1, R1);
311 VPBlockUtils::connectBlocks(VPBB1, R2);
312
313 VPBasicBlock *VPBB2 = new VPBasicBlock();
314 VPBlockUtils::connectBlocks(R1, VPBB2);
315 VPBlockUtils::connectBlocks(R2, VPBB2);
316
317 VPlan Plan;
318 Plan.setEntry(VPBB1);
319 EXPECT_EQ(&Plan, VPBB1->getPlan());
320 EXPECT_EQ(&Plan, R1->getPlan());
321 EXPECT_EQ(&Plan, R1BB1->getPlan());
322 EXPECT_EQ(&Plan, R1BB2->getPlan());
323 EXPECT_EQ(&Plan, R2->getPlan());
324 EXPECT_EQ(&Plan, R2BB1->getPlan());
325 EXPECT_EQ(&Plan, R2BB2->getPlan());
326 EXPECT_EQ(&Plan, VPBB2->getPlan());
327 }
328 }
329
330 TEST(VPBasicBlockTest, TraversingIteratorTest) {
331 {
332 // VPBasicBlocks only
333 // VPBB1
334 // / \
335 // VPBB2 VPBB3
336 // \ /
337 // VPBB4
338 //
339 VPBasicBlock *VPBB1 = new VPBasicBlock();
340 VPBasicBlock *VPBB2 = new VPBasicBlock();
341 VPBasicBlock *VPBB3 = new VPBasicBlock();
342 VPBasicBlock *VPBB4 = new VPBasicBlock();
343
344 VPBlockUtils::connectBlocks(VPBB1, VPBB2);
345 VPBlockUtils::connectBlocks(VPBB1, VPBB3);
346 VPBlockUtils::connectBlocks(VPBB2, VPBB4);
347 VPBlockUtils::connectBlocks(VPBB3, VPBB4);
348
349 VPBlockDeepTraversalWrapper<const VPBlockBase *> Start(VPBB1);
350 SmallVector<const VPBlockBase *> FromIterator(depth_first(Start));
351 EXPECT_EQ(4u, FromIterator.size());
352 EXPECT_EQ(VPBB1, FromIterator[0]);
353 EXPECT_EQ(VPBB2, FromIterator[1]);
354
355 // Use Plan to properly clean up created blocks.
356 VPlan Plan;
357 Plan.setEntry(VPBB1);
358 }
359
360 {
361 // 2 consecutive regions.
362 // R1 {
363 // \
364 // R1BB1
365 // / \ |--|
366 // R1BB2 R1BB3 -|
367 // \ /
368 // R1BB4
369 // }
370 // |
371 // R2 {
372 // \
373 // R2BB1
374 // |
375 // R2BB2
376 //
377 VPBasicBlock *R1BB1 = new VPBasicBlock();
378 VPBasicBlock *R1BB2 = new VPBasicBlock();
379 VPBasicBlock *R1BB3 = new VPBasicBlock();
380 VPBasicBlock *R1BB4 = new VPBasicBlock();
381 VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB4, "R1");
382 R1BB2->setParent(R1);
383 R1BB3->setParent(R1);
384 VPBlockUtils::connectBlocks(R1BB1, R1BB2);
385 VPBlockUtils::connectBlocks(R1BB1, R1BB3);
386 VPBlockUtils::connectBlocks(R1BB2, R1BB4);
387 VPBlockUtils::connectBlocks(R1BB3, R1BB4);
388 // Cycle.
389 VPBlockUtils::connectBlocks(R1BB3, R1BB3);
390
391 VPBasicBlock *R2BB1 = new VPBasicBlock();
392 VPBasicBlock *R2BB2 = new VPBasicBlock();
393 VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R2BB2, "R2");
394 VPBlockUtils::connectBlocks(R2BB1, R2BB2);
395 VPBlockUtils::connectBlocks(R1, R2);
396
397 // Successors of R1.
398 SmallVector<const VPBlockBase *> FromIterator(
399 VPAllSuccessorsIterator<VPBlockBase *>(R1),
400 VPAllSuccessorsIterator<VPBlockBase *>::end(R1));
401 EXPECT_EQ(1u, FromIterator.size());
402 EXPECT_EQ(R1BB1, FromIterator[0]);
403
404 // Depth-first.
405 VPBlockDeepTraversalWrapper<VPBlockBase *> Start(R1);
406 FromIterator.clear();
407 copy(df_begin(Start), df_end(Start), std::back_inserter(FromIterator));
408 EXPECT_EQ(8u, FromIterator.size());
409 EXPECT_EQ(R1, FromIterator[0]);
410 EXPECT_EQ(R1BB1, FromIterator[1]);
411 EXPECT_EQ(R1BB2, FromIterator[2]);
412 EXPECT_EQ(R1BB4, FromIterator[3]);
413 EXPECT_EQ(R2, FromIterator[4]);
414 EXPECT_EQ(R2BB1, FromIterator[5]);
415 EXPECT_EQ(R2BB2, FromIterator[6]);
416 EXPECT_EQ(R1BB3, FromIterator[7]);
417
418 // const VPBasicBlocks only.
419 FromIterator.clear();
420 copy(VPBlockUtils::blocksOnly<const VPBasicBlock>(depth_first(Start)),
421 std::back_inserter(FromIterator));
422 EXPECT_EQ(6u, FromIterator.size());
423 EXPECT_EQ(R1BB1, FromIterator[0]);
424 EXPECT_EQ(R1BB2, FromIterator[1]);
425 EXPECT_EQ(R1BB4, FromIterator[2]);
426 EXPECT_EQ(R2BB1, FromIterator[3]);
427 EXPECT_EQ(R2BB2, FromIterator[4]);
428 EXPECT_EQ(R1BB3, FromIterator[5]);
429
430 // VPRegionBlocks only.
431 SmallVector<VPRegionBlock *> FromIteratorVPRegion(
432 VPBlockUtils::blocksOnly<VPRegionBlock>(depth_first(Start)));
433 EXPECT_EQ(2u, FromIteratorVPRegion.size());
434 EXPECT_EQ(R1, FromIteratorVPRegion[0]);
435 EXPECT_EQ(R2, FromIteratorVPRegion[1]);
436
437 // Post-order.
438 FromIterator.clear();
439 copy(post_order(Start), std::back_inserter(FromIterator));
440 EXPECT_EQ(8u, FromIterator.size());
441 EXPECT_EQ(R2BB2, FromIterator[0]);
442 EXPECT_EQ(R2BB1, FromIterator[1]);
443 EXPECT_EQ(R2, FromIterator[2]);
444 EXPECT_EQ(R1BB4, FromIterator[3]);
445 EXPECT_EQ(R1BB2, FromIterator[4]);
446 EXPECT_EQ(R1BB3, FromIterator[5]);
447 EXPECT_EQ(R1BB1, FromIterator[6]);
448 EXPECT_EQ(R1, FromIterator[7]);
449
450 // Use Plan to properly clean up created blocks.
451 VPlan Plan;
452 Plan.setEntry(R1);
453 }
454
455 {
456 // 2 nested regions.
457 // VPBB1
458 // |
459 // R1 {
460 // R1BB1
461 // / \
462 // R2 { |
463 // \ |
464 // R2BB1 |
465 // | \ R1BB2
466 // R2BB2-| |
467 // \ |
468 // R2BB3 |
469 // } /
470 // \ /
471 // R1BB3
472 // }
473 // |
474 // VPBB2
475 //
476 VPBasicBlock *R1BB1 = new VPBasicBlock("R1BB1");
477 VPBasicBlock *R1BB2 = new VPBasicBlock("R1BB2");
478 VPBasicBlock *R1BB3 = new VPBasicBlock("R1BB3");
479 VPRegionBlock *R1 = new VPRegionBlock(R1BB1, R1BB3, "R1");
480
481 VPBasicBlock *R2BB1 = new VPBasicBlock("R2BB1");
482 VPBasicBlock *R2BB2 = new VPBasicBlock("R2BB2");
483 VPBasicBlock *R2BB3 = new VPBasicBlock("R2BB3");
484 VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R2BB3, "R2");
485 R2BB2->setParent(R2);
486 VPBlockUtils::connectBlocks(R2BB1, R2BB2);
487 VPBlockUtils::connectBlocks(R2BB2, R2BB1);
488 VPBlockUtils::connectBlocks(R2BB2, R2BB3);
489
490 R2->setParent(R1);
491 VPBlockUtils::connectBlocks(R1BB1, R2);
492 R1BB2->setParent(R1);
493 VPBlockUtils::connectBlocks(R1BB1, R1BB2);
494 VPBlockUtils::connectBlocks(R1BB2, R1BB3);
495 VPBlockUtils::connectBlocks(R2, R1BB3);
496
497 VPBasicBlock *VPBB1 = new VPBasicBlock("VPBB1");
498 VPBlockUtils::connectBlocks(VPBB1, R1);
499 VPBasicBlock *VPBB2 = new VPBasicBlock("VPBB2");
500 VPBlockUtils::connectBlocks(R1, VPBB2);
501
502 // Depth-first.
503 VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
504 SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
505 EXPECT_EQ(10u, FromIterator.size());
506 EXPECT_EQ(VPBB1, FromIterator[0]);
507 EXPECT_EQ(R1, FromIterator[1]);
508 EXPECT_EQ(R1BB1, FromIterator[2]);
509 EXPECT_EQ(R2, FromIterator[3]);
510 EXPECT_EQ(R2BB1, FromIterator[4]);
511 EXPECT_EQ(R2BB2, FromIterator[5]);
512 EXPECT_EQ(R2BB3, FromIterator[6]);
513 EXPECT_EQ(R1BB3, FromIterator[7]);
514 EXPECT_EQ(VPBB2, FromIterator[8]);
515 EXPECT_EQ(R1BB2, FromIterator[9]);
516
517 // Post-order.
518 FromIterator.clear();
519 FromIterator.append(po_begin(Start), po_end(Start));
520 EXPECT_EQ(10u, FromIterator.size());
521 EXPECT_EQ(VPBB2, FromIterator[0]);
522 EXPECT_EQ(R1BB3, FromIterator[1]);
523 EXPECT_EQ(R2BB3, FromIterator[2]);
524 EXPECT_EQ(R2BB2, FromIterator[3]);
525 EXPECT_EQ(R2BB1, FromIterator[4]);
526 EXPECT_EQ(R2, FromIterator[5]);
527 EXPECT_EQ(R1BB2, FromIterator[6]);
528 EXPECT_EQ(R1BB1, FromIterator[7]);
529 EXPECT_EQ(R1, FromIterator[8]);
530 EXPECT_EQ(VPBB1, FromIterator[9]);
531
532 // Use Plan to properly clean up created blocks.
533 VPlan Plan;
534 Plan.setEntry(VPBB1);
535 }
536
537 {
538 // VPBB1
539 // |
540 // R1 {
541 // \
542 // R2 {
543 // R2BB1
544 // |
545 // R2BB2
546 // }
547 //
548 VPBasicBlock *R2BB1 = new VPBasicBlock("R2BB1");
549 VPBasicBlock *R2BB2 = new VPBasicBlock("R2BB2");
550 VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R2BB2, "R2");
551 VPBlockUtils::connectBlocks(R2BB1, R2BB2);
552
553 VPRegionBlock *R1 = new VPRegionBlock(R2, R2, "R1");
554 R2->setParent(R1);
555
556 VPBasicBlock *VPBB1 = new VPBasicBlock("VPBB1");
557 VPBlockUtils::connectBlocks(VPBB1, R1);
558
559 // Depth-first.
560 VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
561 SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
562 EXPECT_EQ(5u, FromIterator.size());
563 EXPECT_EQ(VPBB1, FromIterator[0]);
564 EXPECT_EQ(R1, FromIterator[1]);
565 EXPECT_EQ(R2, FromIterator[2]);
566 EXPECT_EQ(R2BB1, FromIterator[3]);
567 EXPECT_EQ(R2BB2, FromIterator[4]);
568
569 // Post-order.
570 FromIterator.clear();
571 FromIterator.append(po_begin(Start), po_end(Start));
572 EXPECT_EQ(5u, FromIterator.size());
573 EXPECT_EQ(R2BB2, FromIterator[0]);
574 EXPECT_EQ(R2BB1, FromIterator[1]);
575 EXPECT_EQ(R2, FromIterator[2]);
576 EXPECT_EQ(R1, FromIterator[3]);
577 EXPECT_EQ(VPBB1, FromIterator[4]);
578
579 // Use Plan to properly clean up created blocks.
580 VPlan Plan;
581 Plan.setEntry(VPBB1);
582 }
583
584 {
585 // Nested regions with both R3 and R2 being exit nodes without successors.
586 // The successors of R1 should be used.
587 //
588 // VPBB1
589 // |
590 // R1 {
591 // \
592 // R2 {
593 // \
594 // R2BB1
595 // |
596 // R3 {
597 // R3BB1
598 // }
599 // }
600 // |
601 // VPBB2
602 //
603 VPBasicBlock *R3BB1 = new VPBasicBlock("R3BB1");
604 VPRegionBlock *R3 = new VPRegionBlock(R3BB1, R3BB1, "R3");
605
606 VPBasicBlock *R2BB1 = new VPBasicBlock("R2BB1");
607 VPRegionBlock *R2 = new VPRegionBlock(R2BB1, R3, "R2");
608 R3->setParent(R2);
609 VPBlockUtils::connectBlocks(R2BB1, R3);
610
611 VPRegionBlock *R1 = new VPRegionBlock(R2, R2, "R1");
612 R2->setParent(R1);
613
614 VPBasicBlock *VPBB1 = new VPBasicBlock("VPBB1");
615 VPBasicBlock *VPBB2 = new VPBasicBlock("VPBB2");
616 VPBlockUtils::connectBlocks(VPBB1, R1);
617 VPBlockUtils::connectBlocks(R1, VPBB2);
618
619 // Depth-first.
620 VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
621 SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
622 EXPECT_EQ(7u, FromIterator.size());
623 EXPECT_EQ(VPBB1, FromIterator[0]);
624 EXPECT_EQ(R1, FromIterator[1]);
625 EXPECT_EQ(R2, FromIterator[2]);
626 EXPECT_EQ(R2BB1, FromIterator[3]);
627 EXPECT_EQ(R3, FromIterator[4]);
628 EXPECT_EQ(R3BB1, FromIterator[5]);
629 EXPECT_EQ(VPBB2, FromIterator[6]);
630
631 SmallVector<VPBlockBase *> FromIteratorVPBB;
632 copy(VPBlockUtils::blocksOnly<VPBasicBlock>(depth_first(Start)),
633 std::back_inserter(FromIteratorVPBB));
634 EXPECT_EQ(VPBB1, FromIteratorVPBB[0]);
635 EXPECT_EQ(R2BB1, FromIteratorVPBB[1]);
636 EXPECT_EQ(R3BB1, FromIteratorVPBB[2]);
637 EXPECT_EQ(VPBB2, FromIteratorVPBB[3]);
638
639 // Post-order.
640 FromIterator.clear();
641 copy(post_order(Start), std::back_inserter(FromIterator));
642 EXPECT_EQ(7u, FromIterator.size());
643 EXPECT_EQ(VPBB2, FromIterator[0]);
644 EXPECT_EQ(R3BB1, FromIterator[1]);
645 EXPECT_EQ(R3, FromIterator[2]);
646 EXPECT_EQ(R2BB1, FromIterator[3]);
647 EXPECT_EQ(R2, FromIterator[4]);
648 EXPECT_EQ(R1, FromIterator[5]);
649 EXPECT_EQ(VPBB1, FromIterator[6]);
650
651 // Post-order, const VPRegionBlocks only.
652 VPBlockDeepTraversalWrapper<const VPBlockBase *> StartConst(VPBB1);
653 SmallVector<const VPRegionBlock *> FromIteratorVPRegion(
654 VPBlockUtils::blocksOnly<const VPRegionBlock>(post_order(StartConst)));
655 EXPECT_EQ(3u, FromIteratorVPRegion.size());
656 EXPECT_EQ(R3, FromIteratorVPRegion[0]);
657 EXPECT_EQ(R2, FromIteratorVPRegion[1]);
658 EXPECT_EQ(R1, FromIteratorVPRegion[2]);
659
660 // Post-order, VPBasicBlocks only.
661 FromIterator.clear();
662 copy(VPBlockUtils::blocksOnly<VPBasicBlock>(post_order(Start)),
663 std::back_inserter(FromIterator));
664 EXPECT_EQ(FromIterator.size(), 4u);
665 EXPECT_EQ(VPBB2, FromIterator[0]);
666 EXPECT_EQ(R3BB1, FromIterator[1]);
667 EXPECT_EQ(R2BB1, FromIterator[2]);
668 EXPECT_EQ(VPBB1, FromIterator[3]);
669
670 // Use Plan to properly clean up created blocks.
671 VPlan Plan;
672 Plan.setEntry(VPBB1);
673 }
674 }
675
676 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
677 TEST(VPBasicBlockTest, print) {
678 VPInstruction *I1 = new VPInstruction(Instruction::Add, {});
679 VPInstruction *I2 = new VPInstruction(Instruction::Sub, {I1});
680 VPInstruction *I3 = new VPInstruction(Instruction::Br, {I1, I2});
681
682 VPBasicBlock *VPBB1 = new VPBasicBlock();
683 VPBB1->appendRecipe(I1);
684 VPBB1->appendRecipe(I2);
685 VPBB1->appendRecipe(I3);
686 VPBB1->setName("bb1");
687
688 VPInstruction *I4 = new VPInstruction(Instruction::Mul, {I2, I1});
689 VPInstruction *I5 = new VPInstruction(Instruction::Ret, {I4});
690 VPBasicBlock *VPBB2 = new VPBasicBlock();
691 VPBB2->appendRecipe(I4);
692 VPBB2->appendRecipe(I5);
693 VPBB2->setName("bb2");
694
695 VPBlockUtils::connectBlocks(VPBB1, VPBB2);
696
697 // Check printing an instruction without associated VPlan.
698 {
699 std::string I3Dump;
700 raw_string_ostream OS(I3Dump);
701 VPSlotTracker SlotTracker;
702 I3->print(OS, "", SlotTracker);
703 OS.flush();
704 EXPECT_EQ("EMIT br <badref> <badref>", I3Dump);
705 }
706
707 VPlan Plan;
708 Plan.setEntry(VPBB1);
709 std::string FullDump;
710 raw_string_ostream OS(FullDump);
711 Plan.printDOT(OS);
712
713 const char *ExpectedStr = R"(digraph VPlan {
714 graph [labelloc=t, fontsize=30; label="Vectorization Plan\n for UF\>=1"]
715 node [shape=rect, fontname=Courier, fontsize=30]
716 edge [fontname=Courier, fontsize=30]
717 compound=true
718 N0 [label =
719 "bb1:\l" +
720 " EMIT vp\<%1\> = add\l" +
721 " EMIT vp\<%2\> = sub vp\<%1\>\l" +
722 " EMIT br vp\<%1\> vp\<%2\>\l" +
723 "Successor(s): bb2\l"
724 ]
725 N0 -> N1 [ label=""]
726 N1 [label =
727 "bb2:\l" +
728 " EMIT vp\<%4\> = mul vp\<%2\> vp\<%1\>\l" +
729 " EMIT ret vp\<%4\>\l" +
730 "No successors\l"
731 ]
732 }
733 )";
734 EXPECT_EQ(ExpectedStr, FullDump);
735
736 const char *ExpectedBlock1Str = R"(bb1:
737 EMIT vp<%1> = add
738 EMIT vp<%2> = sub vp<%1>
739 EMIT br vp<%1> vp<%2>
740 Successor(s): bb2
741 )";
742 std::string Block1Dump;
743 raw_string_ostream OS1(Block1Dump);
744 VPBB1->print(OS1);
745 EXPECT_EQ(ExpectedBlock1Str, Block1Dump);
746
747 // Ensure that numbering is good when dumping the second block in isolation.
748 const char *ExpectedBlock2Str = R"(bb2:
749 EMIT vp<%4> = mul vp<%2> vp<%1>
750 EMIT ret vp<%4>
751 No successors
752 )";
753 std::string Block2Dump;
754 raw_string_ostream OS2(Block2Dump);
755 VPBB2->print(OS2);
756 EXPECT_EQ(ExpectedBlock2Str, Block2Dump);
757
758 {
759 std::string I3Dump;
760 raw_string_ostream OS(I3Dump);
761 VPSlotTracker SlotTracker(&Plan);
762 I3->print(OS, "", SlotTracker);
763 OS.flush();
764 EXPECT_EQ("EMIT br vp<%1> vp<%2>", I3Dump);
765 }
766
767 {
768 std::string I4Dump;
769 raw_string_ostream OS(I4Dump);
770 OS << *I4;
771 OS.flush();
772 EXPECT_EQ("EMIT vp<%4> = mul vp<%2> vp<%1>", I4Dump);
773 }
774 }
775
776 TEST(VPBasicBlockTest, printPlanWithVFsAndUFs) {
777 VPInstruction *I1 = new VPInstruction(Instruction::Add, {});
778
779 VPBasicBlock *VPBB1 = new VPBasicBlock();
780 VPBB1->appendRecipe(I1);
781 VPBB1->setName("bb1");
782
783 VPlan Plan;
784 Plan.setName("TestPlan");
785 Plan.addVF(ElementCount::getFixed(4));
786 Plan.setEntry(VPBB1);
787
788 {
789 std::string FullDump;
790 raw_string_ostream OS(FullDump);
791 Plan.print(OS);
792
793 const char *ExpectedStr = R"(VPlan 'TestPlan for VF={4},UF>=1' {
794 bb1:
795 EMIT vp<%1> = add
796 No successors
797 }
798 )";
799 EXPECT_EQ(ExpectedStr, FullDump);
800 }
801
802 {
803 Plan.addVF(ElementCount::getScalable(8));
804 std::string FullDump;
805 raw_string_ostream OS(FullDump);
806 Plan.print(OS);
807
808 const char *ExpectedStr = R"(VPlan 'TestPlan for VF={4,vscale x 8},UF>=1' {
809 bb1:
810 EMIT vp<%1> = add
811 No successors
812 }
813 )";
814 EXPECT_EQ(ExpectedStr, FullDump);
815 }
816
817 {
818 Plan.setUF(4);
819 std::string FullDump;
820 raw_string_ostream OS(FullDump);
821 Plan.print(OS);
822
823 const char *ExpectedStr = R"(VPlan 'TestPlan for VF={4,vscale x 8},UF={4}' {
824 bb1:
825 EMIT vp<%1> = add
826 No successors
827 }
828 )";
829 EXPECT_EQ(ExpectedStr, FullDump);
830 }
831 }
832 #endif
833
834 TEST(VPRecipeTest, CastVPInstructionToVPUser) {
835 VPValue Op1;
836 VPValue Op2;
837 VPInstruction Recipe(Instruction::Add, {&Op1, &Op2});
838 EXPECT_TRUE(isa<VPUser>(&Recipe));
839 VPRecipeBase *BaseR = &Recipe;
840 EXPECT_TRUE(isa<VPUser>(BaseR));
841 EXPECT_EQ(&Recipe, BaseR);
842 }
843
844 TEST(VPRecipeTest, CastVPWidenRecipeToVPUser) {
845 LLVMContext C;
846
847 IntegerType *Int32 = IntegerType::get(C, 32);
848 auto *AI =
849 BinaryOperator::CreateAdd(UndefValue::get(Int32), UndefValue::get(Int32));
850 VPValue Op1;
851 VPValue Op2;
852 SmallVector<VPValue *, 2> Args;
853 Args.push_back(&Op1);
854 Args.push_back(&Op1);
855 VPWidenRecipe WidenR(*AI, make_range(Args.begin(), Args.end()));
856 EXPECT_TRUE(isa<VPUser>(&WidenR));
857 VPRecipeBase *WidenRBase = &WidenR;
858 EXPECT_TRUE(isa<VPUser>(WidenRBase));
859 EXPECT_EQ(&WidenR, WidenRBase);
860 delete AI;
861 }
862
863 TEST(VPRecipeTest, CastVPWidenCallRecipeToVPUserAndVPDef) {
864 LLVMContext C;
865
866 IntegerType *Int32 = IntegerType::get(C, 32);
867 FunctionType *FTy = FunctionType::get(Int32, false);
868 auto *Call = CallInst::Create(FTy, UndefValue::get(FTy));
869 VPValue Op1;
870 VPValue Op2;
871 SmallVector<VPValue *, 2> Args;
872 Args.push_back(&Op1);
873 Args.push_back(&Op2);
874 VPWidenCallRecipe Recipe(*Call, make_range(Args.begin(), Args.end()), false);
875 EXPECT_TRUE(isa<VPUser>(&Recipe));
876 VPRecipeBase *BaseR = &Recipe;
877 EXPECT_TRUE(isa<VPUser>(BaseR));
878 EXPECT_EQ(&Recipe, BaseR);
879
880 VPValue *VPV = &Recipe;
881 EXPECT_TRUE(VPV->getDefiningRecipe());
882 EXPECT_EQ(&Recipe, VPV->getDefiningRecipe());
883
884 delete Call;
885 }
886
887 TEST(VPRecipeTest, CastVPWidenSelectRecipeToVPUserAndVPDef) {
888 LLVMContext C;
889
890 IntegerType *Int1 = IntegerType::get(C, 1);
891 IntegerType *Int32 = IntegerType::get(C, 32);
892 auto *SelectI = SelectInst::Create(
893 UndefValue::get(Int1), UndefValue::get(Int32), UndefValue::get(Int32));
894 VPValue Op1;
895 VPValue Op2;
896 VPValue Op3;
897 SmallVector<VPValue *, 4> Args;
898 Args.push_back(&Op1);
899 Args.push_back(&Op2);
900 Args.push_back(&Op3);
901 VPWidenSelectRecipe WidenSelectR(*SelectI,
902 make_range(Args.begin(), Args.end()), false);
903 EXPECT_TRUE(isa<VPUser>(&WidenSelectR));
904 VPRecipeBase *BaseR = &WidenSelectR;
905 EXPECT_TRUE(isa<VPUser>(BaseR));
906 EXPECT_EQ(&WidenSelectR, BaseR);
907
908 VPValue *VPV = &WidenSelectR;
909 EXPECT_TRUE(isa<VPRecipeBase>(VPV->getDefiningRecipe()));
910 EXPECT_EQ(&WidenSelectR, VPV->getDefiningRecipe());
911
912 delete SelectI;
913 }
914
915 TEST(VPRecipeTest, CastVPWidenGEPRecipeToVPUserAndVPDef) {
916 LLVMContext C;
917
918 IntegerType *Int32 = IntegerType::get(C, 32);
919 PointerType *Int32Ptr = PointerType::get(Int32, 0);
920 auto *GEP = GetElementPtrInst::Create(Int32, UndefValue::get(Int32Ptr),
921 UndefValue::get(Int32));
922 VPValue Op1;
923 VPValue Op2;
924 SmallVector<VPValue *, 4> Args;
925 Args.push_back(&Op1);
926 Args.push_back(&Op2);
927 VPWidenGEPRecipe Recipe(GEP, make_range(Args.begin(), Args.end()));
928 EXPECT_TRUE(isa<VPUser>(&Recipe));
929 VPRecipeBase *BaseR = &Recipe;
930 EXPECT_TRUE(isa<VPUser>(BaseR));
931 EXPECT_EQ(&Recipe, BaseR);
932
933 VPValue *VPV = &Recipe;
934 EXPECT_TRUE(isa<VPRecipeBase>(VPV->getDefiningRecipe()));
935 EXPECT_EQ(&Recipe, VPV->getDefiningRecipe());
936
937 delete GEP;
938 }
939
940 TEST(VPRecipeTest, CastVPBlendRecipeToVPUser) {
941 LLVMContext C;
942
943 IntegerType *Int32 = IntegerType::get(C, 32);
944 auto *Phi = PHINode::Create(Int32, 1);
945 VPValue Op1;
946 VPValue Op2;
947 SmallVector<VPValue *, 4> Args;
948 Args.push_back(&Op1);
949 Args.push_back(&Op2);
950 VPBlendRecipe Recipe(Phi, Args);
951 EXPECT_TRUE(isa<VPUser>(&Recipe));
952 VPRecipeBase *BaseR = &Recipe;
953 EXPECT_TRUE(isa<VPUser>(BaseR));
954 delete Phi;
955 }
956
957 TEST(VPRecipeTest, CastVPInterleaveRecipeToVPUser) {
958 LLVMContext C;
959
960 VPValue Addr;
961 VPValue Mask;
962 InterleaveGroup<Instruction> IG(4, false, Align(4));
963 VPInterleaveRecipe Recipe(&IG, &Addr, {}, &Mask);
964 EXPECT_TRUE(isa<VPUser>(&Recipe));
965 VPRecipeBase *BaseR = &Recipe;
966 EXPECT_TRUE(isa<VPUser>(BaseR));
967 EXPECT_EQ(&Recipe, BaseR);
968 }
969
970 TEST(VPRecipeTest, CastVPReplicateRecipeToVPUser) {
971 LLVMContext C;
972
973 VPValue Op1;
974 VPValue Op2;
975 SmallVector<VPValue *, 4> Args;
976 Args.push_back(&Op1);
977 Args.push_back(&Op2);
978
979 VPReplicateRecipe Recipe(nullptr, make_range(Args.begin(), Args.end()), true,
980 false);
981 EXPECT_TRUE(isa<VPUser>(&Recipe));
982 VPRecipeBase *BaseR = &Recipe;
983 EXPECT_TRUE(isa<VPUser>(BaseR));
984 }
985
986 TEST(VPRecipeTest, CastVPBranchOnMaskRecipeToVPUser) {
987 LLVMContext C;
988
989 VPValue Mask;
990 VPBranchOnMaskRecipe Recipe(&Mask);
991 EXPECT_TRUE(isa<VPUser>(&Recipe));
992 VPRecipeBase *BaseR = &Recipe;
993 EXPECT_TRUE(isa<VPUser>(BaseR));
994 EXPECT_EQ(&Recipe, BaseR);
995 }
996
997 TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUserAndVPDef) {
998 LLVMContext C;
999
1000 IntegerType *Int32 = IntegerType::get(C, 32);
1001 PointerType *Int32Ptr = PointerType::get(Int32, 0);
1002 auto *Load =
1003 new LoadInst(Int32, UndefValue::get(Int32Ptr), "", false, Align(1));
1004 VPValue Addr;
1005 VPValue Mask;
1006 VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask, true, false);
1007 EXPECT_TRUE(isa<VPUser>(&Recipe));
1008 VPRecipeBase *BaseR = &Recipe;
1009 EXPECT_TRUE(isa<VPUser>(BaseR));
1010 EXPECT_EQ(&Recipe, BaseR);
1011
1012 VPValue *VPV = Recipe.getVPSingleValue();
1013 EXPECT_TRUE(isa<VPRecipeBase>(VPV->getDefiningRecipe()));
1014 EXPECT_EQ(&Recipe, VPV->getDefiningRecipe());
1015
1016 delete Load;
1017 }
1018
1019 TEST(VPRecipeTest, MayHaveSideEffectsAndMayReadWriteMemory) {
1020 LLVMContext C;
1021 IntegerType *Int1 = IntegerType::get(C, 1);
1022 IntegerType *Int32 = IntegerType::get(C, 32);
1023 PointerType *Int32Ptr = PointerType::get(Int32, 0);
1024
1025 {
1026 auto *AI = BinaryOperator::CreateAdd(UndefValue::get(Int32),
1027 UndefValue::get(Int32));
1028 VPValue Op1;
1029 VPValue Op2;
1030 SmallVector<VPValue *, 2> Args;
1031 Args.push_back(&Op1);
1032 Args.push_back(&Op1);
1033 VPWidenRecipe Recipe(*AI, make_range(Args.begin(), Args.end()));
1034 EXPECT_FALSE(Recipe.mayHaveSideEffects());
1035 EXPECT_FALSE(Recipe.mayReadFromMemory());
1036 EXPECT_FALSE(Recipe.mayWriteToMemory());
1037 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1038 delete AI;
1039 }
1040
1041 {
1042 auto *SelectI = SelectInst::Create(
1043 UndefValue::get(Int1), UndefValue::get(Int32), UndefValue::get(Int32));
1044 VPValue Op1;
1045 VPValue Op2;
1046 VPValue Op3;
1047 SmallVector<VPValue *, 4> Args;
1048 Args.push_back(&Op1);
1049 Args.push_back(&Op2);
1050 Args.push_back(&Op3);
1051 VPWidenSelectRecipe Recipe(*SelectI, make_range(Args.begin(), Args.end()),
1052 false);
1053 EXPECT_FALSE(Recipe.mayHaveSideEffects());
1054 EXPECT_FALSE(Recipe.mayReadFromMemory());
1055 EXPECT_FALSE(Recipe.mayWriteToMemory());
1056 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1057 delete SelectI;
1058 }
1059
1060 {
1061 auto *GEP = GetElementPtrInst::Create(Int32, UndefValue::get(Int32Ptr),
1062 UndefValue::get(Int32));
1063 VPValue Op1;
1064 VPValue Op2;
1065 SmallVector<VPValue *, 4> Args;
1066 Args.push_back(&Op1);
1067 Args.push_back(&Op2);
1068 VPWidenGEPRecipe Recipe(GEP, make_range(Args.begin(), Args.end()));
1069 EXPECT_FALSE(Recipe.mayHaveSideEffects());
1070 EXPECT_FALSE(Recipe.mayReadFromMemory());
1071 EXPECT_FALSE(Recipe.mayWriteToMemory());
1072 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1073 delete GEP;
1074 }
1075
1076 {
1077 VPValue Mask;
1078 VPBranchOnMaskRecipe Recipe(&Mask);
1079 EXPECT_TRUE(Recipe.mayHaveSideEffects());
1080 EXPECT_FALSE(Recipe.mayReadFromMemory());
1081 EXPECT_FALSE(Recipe.mayWriteToMemory());
1082 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1083 }
1084
1085 {
1086 VPValue ChainOp;
1087 VPValue VecOp;
1088 VPValue CondOp;
1089 VPReductionRecipe Recipe(nullptr, nullptr, &ChainOp, &CondOp, &VecOp,
1090 nullptr);
1091 EXPECT_FALSE(Recipe.mayHaveSideEffects());
1092 EXPECT_FALSE(Recipe.mayReadFromMemory());
1093 EXPECT_FALSE(Recipe.mayWriteToMemory());
1094 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1095 }
1096
1097 {
1098 auto *Load =
1099 new LoadInst(Int32, UndefValue::get(Int32Ptr), "", false, Align(1));
1100 VPValue Addr;
1101 VPValue Mask;
1102 VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask, true, false);
1103 EXPECT_TRUE(Recipe.mayHaveSideEffects());
1104 EXPECT_TRUE(Recipe.mayReadFromMemory());
1105 EXPECT_FALSE(Recipe.mayWriteToMemory());
1106 EXPECT_TRUE(Recipe.mayReadOrWriteMemory());
1107 delete Load;
1108 }
1109
1110 {
1111 auto *Store = new StoreInst(UndefValue::get(Int32),
1112 UndefValue::get(Int32Ptr), false, Align(1));
1113 VPValue Addr;
1114 VPValue Mask;
1115 VPValue StoredV;
1116 VPWidenMemoryInstructionRecipe Recipe(*Store, &Addr, &StoredV, &Mask, false,
1117 false);
1118 EXPECT_TRUE(Recipe.mayHaveSideEffects());
1119 EXPECT_FALSE(Recipe.mayReadFromMemory());
1120 EXPECT_TRUE(Recipe.mayWriteToMemory());
1121 EXPECT_TRUE(Recipe.mayReadOrWriteMemory());
1122 delete Store;
1123 }
1124
1125 {
1126 FunctionType *FTy = FunctionType::get(Int32, false);
1127 auto *Call = CallInst::Create(FTy, UndefValue::get(FTy));
1128 VPValue Op1;
1129 VPValue Op2;
1130 SmallVector<VPValue *, 2> Args;
1131 Args.push_back(&Op1);
1132 Args.push_back(&Op2);
1133 VPWidenCallRecipe Recipe(*Call, make_range(Args.begin(), Args.end()),
1134 false);
1135 EXPECT_TRUE(Recipe.mayHaveSideEffects());
1136 EXPECT_TRUE(Recipe.mayReadFromMemory());
1137 EXPECT_TRUE(Recipe.mayWriteToMemory());
1138 EXPECT_TRUE(Recipe.mayReadOrWriteMemory());
1139 delete Call;
1140 }
1141
1142 {
1143 VPValue Op1;
1144 VPValue Op2;
1145 InductionDescriptor IndDesc;
1146 VPScalarIVStepsRecipe Recipe(IndDesc, &Op1, &Op2);
1147 EXPECT_FALSE(Recipe.mayHaveSideEffects());
1148 EXPECT_FALSE(Recipe.mayReadFromMemory());
1149 EXPECT_FALSE(Recipe.mayWriteToMemory());
1150 EXPECT_FALSE(Recipe.mayReadOrWriteMemory());
1151 }
1152
1153 // The initial implementation is conservative with respect to VPInstructions.
1154 {
1155 VPValue Op1;
1156 VPValue Op2;
1157 VPInstruction VPInst(Instruction::Add, {&Op1, &Op2});
1158 VPRecipeBase &Recipe = VPInst;
1159 EXPECT_TRUE(Recipe.mayHaveSideEffects());
1160 EXPECT_TRUE(Recipe.mayReadFromMemory());
1161 EXPECT_TRUE(Recipe.mayWriteToMemory());
1162 EXPECT_TRUE(Recipe.mayReadOrWriteMemory());
1163 }
1164 }
1165
1166 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1167 TEST(VPRecipeTest, dump) {
1168 VPlan Plan;
1169 VPBasicBlock *VPBB1 = new VPBasicBlock();
1170 Plan.setEntry(VPBB1);
1171
1172 LLVMContext C;
1173
1174 IntegerType *Int32 = IntegerType::get(C, 32);
1175 auto *AI =
1176 BinaryOperator::CreateAdd(UndefValue::get(Int32), UndefValue::get(Int32));
1177 AI->setName("a");
1178 SmallVector<VPValue *, 2> Args;
1179 VPValue *ExtVPV1 = Plan.getOrAddExternalDef(ConstantInt::get(Int32, 1));
1180 VPValue *ExtVPV2 = Plan.getOrAddExternalDef(ConstantInt::get(Int32, 2));
1181 Args.push_back(ExtVPV1);
1182 Args.push_back(ExtVPV2);
1183 VPWidenRecipe *WidenR =
1184 new VPWidenRecipe(*AI, make_range(Args.begin(), Args.end()));
1185 VPBB1->appendRecipe(WidenR);
1186
1187 {
1188 // Use EXPECT_EXIT to capture stderr and compare against expected output.
1189 //
1190 // Test VPValue::dump().
1191 VPValue *VPV = WidenR;
1192 EXPECT_EXIT(
1193 {
1194 VPV->dump();
1195 exit(0);
1196 },
1197 testing::ExitedWithCode(0), "WIDEN ir<%a> = add ir<1>, ir<2>");
1198
1199 // Test VPRecipeBase::dump().
1200 VPRecipeBase *R = WidenR;
1201 EXPECT_EXIT(
1202 {
1203 R->dump();
1204 exit(0);
1205 },
1206 testing::ExitedWithCode(0), "WIDEN ir<%a> = add ir<1>, ir<2>");
1207
1208 // Test VPDef::dump().
1209 VPDef *D = WidenR;
1210 EXPECT_EXIT(
1211 {
1212 D->dump();
1213 exit(0);
1214 },
1215 testing::ExitedWithCode(0), "WIDEN ir<%a> = add ir<1>, ir<2>");
1216 }
1217
1218 delete AI;
1219 }
1220 #endif
1221
1222 TEST(VPRecipeTest, CastVPReductionRecipeToVPUser) {
1223 LLVMContext C;
1224
1225 VPValue ChainOp;
1226 VPValue VecOp;
1227 VPValue CondOp;
1228 VPReductionRecipe Recipe(nullptr, nullptr, &ChainOp, &CondOp, &VecOp,
1229 nullptr);
1230 EXPECT_TRUE(isa<VPUser>(&Recipe));
1231 VPRecipeBase *BaseR = &Recipe;
1232 EXPECT_TRUE(isa<VPUser>(BaseR));
1233 }
1234
1235 struct VPDoubleValueDef : public VPRecipeBase {
1236 VPDoubleValueDef(ArrayRef<VPValue *> Operands) : VPRecipeBase(99, Operands) {
1237 new VPValue(nullptr, this);
1238 new VPValue(nullptr, this);
1239 }
1240
1241 void execute(struct VPTransformState &State) override{};
1242 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1243 void print(raw_ostream &O, const Twine &Indent,
1244 VPSlotTracker &SlotTracker) const override {}
1245 #endif
1246 };
1247
1248 TEST(VPDoubleValueDefTest, traverseUseLists) {
1249 // Check that the def-use chains of a multi-def can be traversed in both
1250 // directions.
1251
1252 // Create a new VPDef which defines 2 values and has 2 operands.
1253 VPInstruction Op0(20, {});
1254 VPInstruction Op1(30, {});
1255 VPDoubleValueDef DoubleValueDef({&Op0, &Op1});
1256
1257 // Create a new users of the defined values.
1258 VPInstruction I1(
1259 1, {DoubleValueDef.getVPValue(0), DoubleValueDef.getVPValue(1)});
1260 VPInstruction I2(2, {DoubleValueDef.getVPValue(0)});
1261 VPInstruction I3(3, {DoubleValueDef.getVPValue(1)});
1262
1263 // Check operands of the VPDef (traversing upwards).
1264 SmallVector<VPValue *, 4> DoubleOperands(DoubleValueDef.op_begin(),
1265 DoubleValueDef.op_end());
1266 EXPECT_EQ(2u, DoubleOperands.size());
1267 EXPECT_EQ(&Op0, DoubleOperands[0]);
1268 EXPECT_EQ(&Op1, DoubleOperands[1]);
1269
1270 // Check users of the defined values (traversing downwards).
1271 SmallVector<VPUser *, 4> DoubleValueDefV0Users(
1272 DoubleValueDef.getVPValue(0)->user_begin(),
1273 DoubleValueDef.getVPValue(0)->user_end());
1274 EXPECT_EQ(2u, DoubleValueDefV0Users.size());
1275 EXPECT_EQ(&I1, DoubleValueDefV0Users[0]);
1276 EXPECT_EQ(&I2, DoubleValueDefV0Users[1]);
1277
1278 SmallVector<VPUser *, 4> DoubleValueDefV1Users(
1279 DoubleValueDef.getVPValue(1)->user_begin(),
1280 DoubleValueDef.getVPValue(1)->user_end());
1281 EXPECT_EQ(2u, DoubleValueDefV1Users.size());
1282 EXPECT_EQ(&I1, DoubleValueDefV1Users[0]);
1283 EXPECT_EQ(&I3, DoubleValueDefV1Users[1]);
1284
1285 // Now check that we can get the right VPDef for each defined value.
1286 EXPECT_EQ(&DoubleValueDef, I1.getOperand(0)->getDefiningRecipe());
1287 EXPECT_EQ(&DoubleValueDef, I1.getOperand(1)->getDefiningRecipe());
1288 EXPECT_EQ(&DoubleValueDef, I2.getOperand(0)->getDefiningRecipe());
1289 EXPECT_EQ(&DoubleValueDef, I3.getOperand(0)->getDefiningRecipe());
1290 }
1291
1292 } // namespace
1293 } // namespace llvm