|
| 1 | +//===-- VPlanConstruction.cpp - Transforms for initial VPlan construction -===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file |
| 10 | +/// This file implements transforms for initial VPlan construction |
| 11 | +/// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "LoopVectorizationPlanner.h" |
| 15 | +#include "VPlan.h" |
| 16 | +#include "VPlanCFG.h" |
| 17 | +#include "VPlanTransforms.h" |
| 18 | +#include "llvm/Analysis/LoopInfo.h" |
| 19 | +#include "llvm/Analysis/ScalarEvolution.h" |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | + |
| 23 | +void VPlanTransforms::introduceTopLevelVectorLoopRegion( |
| 24 | + VPlan &Plan, Type *InductionTy, PredicatedScalarEvolution &PSE, |
| 25 | + bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop) { |
| 26 | + // TODO: Generalize to introduce all loop regions. |
| 27 | + auto *HeaderVPBB = cast<VPBasicBlock>(Plan.getEntry()->getSingleSuccessor()); |
| 28 | + VPBlockUtils::disconnectBlocks(Plan.getEntry(), HeaderVPBB); |
| 29 | + |
| 30 | + VPBasicBlock *OriginalLatch = |
| 31 | + cast<VPBasicBlock>(HeaderVPBB->getSinglePredecessor()); |
| 32 | + VPBlockUtils::disconnectBlocks(OriginalLatch, HeaderVPBB); |
| 33 | + VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph"); |
| 34 | + VPBlockUtils::connectBlocks(Plan.getEntry(), VecPreheader); |
| 35 | + assert(OriginalLatch->getNumSuccessors() == 0 && "expected no predecessors"); |
| 36 | + |
| 37 | + // Create SCEV and VPValue for the trip count. |
| 38 | + // We use the symbolic max backedge-taken-count, which works also when |
| 39 | + // vectorizing loops with uncountable early exits. |
| 40 | + const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount(); |
| 41 | + assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) && |
| 42 | + "Invalid loop count"); |
| 43 | + ScalarEvolution &SE = *PSE.getSE(); |
| 44 | + const SCEV *TripCount = SE.getTripCountFromExitCount(BackedgeTakenCountSCEV, |
| 45 | + InductionTy, TheLoop); |
| 46 | + Plan.setTripCount( |
| 47 | + vputils::getOrCreateVPValueForSCEVExpr(Plan, TripCount, SE)); |
| 48 | + |
| 49 | + // Create VPRegionBlock, with existing header and new empty latch block, to be |
| 50 | + // filled |
| 51 | + VPBasicBlock *LatchVPBB = Plan.createVPBasicBlock("vector.latch"); |
| 52 | + VPBlockUtils::insertBlockAfter(LatchVPBB, OriginalLatch); |
| 53 | + auto *TopRegion = Plan.createVPRegionBlock( |
| 54 | + HeaderVPBB, LatchVPBB, "vector loop", false /*isReplicator*/); |
| 55 | + for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPBB)) |
| 56 | + VPBB->setParent(TopRegion); |
| 57 | + |
| 58 | + VPBlockUtils::insertBlockAfter(TopRegion, VecPreheader); |
| 59 | + VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block"); |
| 60 | + VPBlockUtils::insertBlockAfter(MiddleVPBB, TopRegion); |
| 61 | + |
| 62 | + VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph"); |
| 63 | + VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader()); |
| 64 | + if (!RequiresScalarEpilogueCheck) { |
| 65 | + VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // If needed, add a check in the middle block to see if we have completed |
| 70 | + // all of the iterations in the first vector loop. Three cases: |
| 71 | + // 1) If (N - N%VF) == N, then we *don't* need to run the remainder. |
| 72 | + // Thus if tail is to be folded, we know we don't need to run the |
| 73 | + // remainder and we can set the condition to true. |
| 74 | + // 2) If we require a scalar epilogue, there is no conditional branch as |
| 75 | + // we unconditionally branch to the scalar preheader. Do nothing. |
| 76 | + // 3) Otherwise, construct a runtime check. |
| 77 | + BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock(); |
| 78 | + auto *VPExitBlock = Plan.getExitBlock(IRExitBlock); |
| 79 | + // The connection order corresponds to the operands of the conditional branch. |
| 80 | + VPBlockUtils::insertBlockAfter(VPExitBlock, MiddleVPBB); |
| 81 | + VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH); |
| 82 | + |
| 83 | + auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator(); |
| 84 | + // Here we use the same DebugLoc as the scalar loop latch terminator instead |
| 85 | + // of the corresponding compare because they may have ended up with |
| 86 | + // different line numbers and we want to avoid awkward line stepping while |
| 87 | + // debugging. Eg. if the compare has got a line number inside the loop. |
| 88 | + VPBuilder Builder(MiddleVPBB); |
| 89 | + VPValue *Cmp = |
| 90 | + TailFolded |
| 91 | + ? Plan.getOrAddLiveIn(ConstantInt::getTrue( |
| 92 | + IntegerType::getInt1Ty(TripCount->getType()->getContext()))) |
| 93 | + : Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(), |
| 94 | + &Plan.getVectorTripCount(), |
| 95 | + ScalarLatchTerm->getDebugLoc(), "cmp.n"); |
| 96 | + Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp}, |
| 97 | + ScalarLatchTerm->getDebugLoc()); |
| 98 | +} |
0 commit comments