|
21 | 21 |
|
22 | 22 | using namespace llvm; |
23 | 23 |
|
24 | | -/// Create and return a new VPRegionBlock for loop starting at \p HeaderVPBB and |
25 | | -/// return it. |
26 | | -static VPRegionBlock *introduceRegion(VPlan &Plan, VPBasicBlock *PreheaderVPBB, |
27 | | - VPBasicBlock *HeaderVPBB, |
28 | | - VPBasicBlock *LatchVPBB) { |
29 | | - VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPBB); |
30 | | - VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPBB); |
| 24 | +/// Checks if \p HeaderVPB is a loop header block in the plain CFG; that is, it |
| 25 | +/// has exactly 2 predecessors (preheader and latch), where the block |
| 26 | +/// dominates the latch and the preheader dominates the block. If it is a |
| 27 | +/// header block, returns a pair with the corresponding preheader and latch |
| 28 | +/// blocks. Otherwise return std::nullopt. |
| 29 | +static std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>> |
| 30 | +getPreheaderAndLatch(VPBlockBase *HeaderVPB, const VPDominatorTree &VPDT) { |
| 31 | + ArrayRef<VPBlockBase *> Preds = HeaderVPB->getPredecessors(); |
| 32 | + if (Preds.size() != 2) |
| 33 | + return std::nullopt; |
| 34 | + |
| 35 | + auto *PreheaderVPBB = cast<VPBasicBlock>(Preds[0]); |
| 36 | + auto *LatchVPBB = cast<VPBasicBlock>(Preds[1]); |
| 37 | + if (VPDT.dominates(PreheaderVPBB, HeaderVPB) && |
| 38 | + VPDT.dominates(HeaderVPB, LatchVPBB)) |
| 39 | + return {std::make_pair(PreheaderVPBB, LatchVPBB)}; |
| 40 | + |
| 41 | + std::swap(PreheaderVPBB, LatchVPBB); |
| 42 | + if (VPDT.dominates(PreheaderVPBB, HeaderVPB) && |
| 43 | + VPDT.dominates(HeaderVPB, LatchVPBB)) |
| 44 | + return {std::make_pair(PreheaderVPBB, LatchVPBB)}; |
| 45 | + |
| 46 | + return std::nullopt; |
| 47 | +} |
| 48 | + |
| 49 | +/// Create a new VPRegionBlock if there is a loop starting at \p HeaderVPB. |
| 50 | +static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB, |
| 51 | + VPDominatorTree &VPDT) { |
| 52 | + auto Res = getPreheaderAndLatch(HeaderVPB, VPDT); |
| 53 | + if (!Res) |
| 54 | + return; |
| 55 | + |
| 56 | + const auto &[PreheaderVPBB, LatchVPBB] = *Res; |
| 57 | + |
| 58 | + // Swap the operands of header phis if needed. After creating the region, the |
| 59 | + // incoming value from the preheader must be the first operand and the one |
| 60 | + // from the latch must be the second operand. |
| 61 | + if (HeaderVPB->getPredecessors()[0] != PreheaderVPBB) { |
| 62 | + for (VPRecipeBase &R : cast<VPBasicBlock>(HeaderVPB)->phis()) { |
| 63 | + VPValue *Inc0 = R.getOperand(0); |
| 64 | + R.setOperand(0, R.getOperand(1)); |
| 65 | + R.setOperand(1, Inc0); |
| 66 | + } |
| 67 | + } |
| 68 | + VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPB); |
| 69 | + VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPB); |
31 | 70 | VPBlockBase *Succ = LatchVPBB->getSingleSuccessor(); |
32 | 71 | assert(LatchVPBB->getNumSuccessors() <= 1 && |
33 | 72 | "Latch has more than one successor"); |
34 | 73 | if (Succ) |
35 | 74 | VPBlockUtils::disconnectBlocks(LatchVPBB, Succ); |
36 | 75 |
|
37 | | - auto *R = Plan.createVPRegionBlock(HeaderVPBB, LatchVPBB, "", |
| 76 | + auto *R = Plan.createVPRegionBlock(HeaderVPB, LatchVPBB, "", |
38 | 77 | false /*isReplicator*/); |
39 | | - R->setParent(HeaderVPBB->getParent()); |
40 | | - // All VPBB's reachable shallowly from HeaderVPBB belong to top level loop, |
| 78 | + R->setParent(HeaderVPB->getParent()); |
| 79 | + // All VPBB's reachable shallowly from HeaderVPB belong to top level loop, |
41 | 80 | // because VPlan is expected to end at top level latch disconnected above. |
42 | | - for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPBB)) |
| 81 | + for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPB)) |
43 | 82 | VPBB->setParent(R); |
44 | 83 |
|
45 | 84 | VPBlockUtils::insertBlockAfter(R, PreheaderVPBB); |
46 | 85 | if (Succ) |
47 | 86 | VPBlockUtils::connectBlocks(R, Succ); |
48 | | - return R; |
49 | 87 | } |
50 | 88 |
|
51 | | -void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy, |
52 | | - PredicatedScalarEvolution &PSE, |
53 | | - bool RequiresScalarEpilogueCheck, |
54 | | - bool TailFolded, Loop *TheLoop) { |
| 89 | +void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy, |
| 90 | + PredicatedScalarEvolution &PSE, |
| 91 | + bool RequiresScalarEpilogueCheck, |
| 92 | + bool TailFolded, Loop *TheLoop) { |
55 | 93 | VPDominatorTree VPDT; |
56 | 94 | VPDT.recalculate(Plan); |
57 | | - for (VPBasicBlock *HeaderVPBB : VPBlockUtils::blocksOnly<VPBasicBlock>( |
58 | | - vp_depth_first_shallow(Plan.getEntry()))) { |
59 | | - auto Res = HeaderVPBB->isHeader(VPDT); |
60 | | - if (!Res) |
61 | | - continue; |
62 | | - const auto &[PreheaderVPBB, LatchVPBB] = *Res; |
63 | | - introduceRegion(Plan, PreheaderVPBB, HeaderVPBB, LatchVPBB); |
64 | | - } |
| 95 | + for (VPBlockBase *HeaderVPB : vp_depth_first_shallow(Plan.getEntry())) |
| 96 | + createLoopRegion(Plan, HeaderVPB, VPDT); |
65 | 97 |
|
66 | 98 | VPRegionBlock *TopRegion = Plan.getVectorLoopRegion(); |
67 | 99 | auto *OrigExiting = TopRegion->getExiting(); |
|
0 commit comments