Skip to content

Commit aea82a7

Browse files
authored
[VPlan] Remove some getCanonicalIV() uses. NFC (llvm#152969)
A lot of time getCanonicalIV() is used to get the canonical IV type, e.g. to instantiate a VPTypeAnalysis or to get the LLVMContext. However VPTypeAnalysis has a constructor that takes the VPlan directly and there's a method on VPlan to get the LLVMContext directly, so use those instead where possible. This lets us remove a constructor on VPTypeAnalysis. Also remove an unused LLVMContext argument in UnrollState whilst we're here.
1 parent 68c609b commit aea82a7

File tree

8 files changed

+35
-54
lines changed

8 files changed

+35
-54
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,8 +4022,7 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
40224022
if (VF.isScalar())
40234023
continue;
40244024

4025-
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
4026-
CM, CM.CostKind);
4025+
VPCostContext CostCtx(CM.TTI, *CM.TLI, *Plan, CM, CM.CostKind);
40274026
precomputeCosts(*Plan, VF, CostCtx);
40284027
auto Iter = vp_depth_first_deep(Plan->getVectorLoopRegion()->getEntry());
40294028
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) {
@@ -4131,7 +4130,7 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
41314130
static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
41324131
const TargetTransformInfo &TTI) {
41334132
assert(VF.isVector() && "Checking a scalar VF?");
4134-
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
4133+
VPTypeAnalysis TypeInfo(Plan);
41354134
DenseSet<VPRecipeBase *> EphemeralRecipes;
41364135
collectEphemeralRecipesForVPlan(Plan, EphemeralRecipes);
41374136
// Set of already visited types.
@@ -4279,8 +4278,7 @@ VectorizationFactor LoopVectorizationPlanner::selectVectorizationFactor() {
42794278

42804279
// Add on other costs that are modelled in VPlan, but not in the legacy
42814280
// cost model.
4282-
VPCostContext CostCtx(CM.TTI, *CM.TLI, CM.Legal->getWidestInductionType(),
4283-
CM, CM.CostKind);
4281+
VPCostContext CostCtx(CM.TTI, *CM.TLI, *P, CM, CM.CostKind);
42844282
VPRegionBlock *VectorRegion = P->getVectorLoopRegion();
42854283
assert(VectorRegion && "Expected to have a vector region!");
42864284
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
@@ -6932,8 +6930,7 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
69326930

69336931
InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
69346932
ElementCount VF) const {
6935-
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(), CM,
6936-
CM.CostKind);
6933+
VPCostContext CostCtx(CM.TTI, *CM.TLI, Plan, CM, CM.CostKind);
69376934
InstructionCost Cost = precomputeCosts(Plan, VF, CostCtx);
69386935

69396936
// Now compute and add the VPlan-based cost.
@@ -7134,8 +7131,7 @@ VectorizationFactor LoopVectorizationPlanner::computeBestVF() {
71347131
// simplifications not accounted for in the legacy cost model. If that's the
71357132
// case, don't trigger the assertion, as the extra simplifications may cause a
71367133
// different VF to be picked by the VPlan-based cost model.
7137-
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(), CM,
7138-
CM.CostKind);
7134+
VPCostContext CostCtx(CM.TTI, *CM.TLI, BestPlan, CM, CM.CostKind);
71397135
precomputeCosts(BestPlan, BestFactor.Width, CostCtx);
71407136
// Verify that the VPlan-based and legacy cost models agree, except for VPlans
71417137
// with early exits and plans with additional VPlan simplifications. The
@@ -7273,8 +7269,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
72737269
++LoopsEarlyExitVectorized;
72747270
// TODO: Move to VPlan transform stage once the transition to the VPlan-based
72757271
// cost model is complete for better cost estimates.
7276-
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
7277-
OrigLoop->getHeader()->getContext());
7272+
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF);
72787273
VPlanTransforms::runPass(VPlanTransforms::replicateByVF, BestVPlan, BestVF);
72797274
VPlanTransforms::runPass(VPlanTransforms::materializeBroadcasts, BestVPlan);
72807275
bool HasBranchWeights =
@@ -8462,7 +8457,7 @@ static VPInstruction *addResumePhiRecipeForInduction(
84628457
/// \p IVEndValues.
84638458
static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
84648459
DenseMap<VPValue *, VPValue *> &IVEndValues) {
8465-
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
8460+
VPTypeAnalysis TypeInfo(Plan);
84668461
auto *ScalarPH = Plan.getScalarPreheader();
84678462
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPH->getPredecessors()[0]);
84688463
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
@@ -8845,8 +8840,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
88458840
// TODO: Enable following transform when the EVL-version of extended-reduction
88468841
// and mulacc-reduction are implemented.
88478842
if (!CM.foldTailWithEVL()) {
8848-
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(), CM,
8849-
CM.CostKind);
8843+
VPCostContext CostCtx(CM.TTI, *CM.TLI, *Plan, CM, CM.CostKind);
88508844
VPlanTransforms::runPass(VPlanTransforms::convertToAbstractRecipes, *Plan,
88518845
CostCtx, Range);
88528846
}
@@ -10126,8 +10120,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1012610120
// Check if it is profitable to vectorize with runtime checks.
1012710121
bool ForceVectorization =
1012810122
Hints.getForce() == LoopVectorizeHints::FK_Enabled;
10129-
VPCostContext CostCtx(CM.TTI, *CM.TLI, CM.Legal->getWidestInductionType(),
10130-
CM, CM.CostKind);
10123+
VPCostContext CostCtx(CM.TTI, *CM.TLI, LVP.getPlanFor(VF.Width), CM,
10124+
CM.CostKind);
1013110125
if (!ForceVectorization &&
1013210126
!isOutsideLoopWorkProfitable(Checks, VF, L, PSE, CostCtx,
1013310127
LVP.getPlanFor(VF.Width), SEL,

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ VPTransformState::VPTransformState(const TargetTransformInfo *TTI,
246246
IRBuilderBase &Builder, VPlan *Plan,
247247
Loop *CurrentParentLoop, Type *CanonicalIVTy)
248248
: TTI(TTI), VF(VF), CFG(DT), LI(LI), AC(AC), Builder(Builder), Plan(Plan),
249-
CurrentParentLoop(CurrentParentLoop), TypeAnalysis(CanonicalIVTy),
250-
VPDT(*Plan) {}
249+
CurrentParentLoop(CurrentParentLoop), TypeAnalysis(*Plan), VPDT(*Plan) {}
251250

252251
Value *VPTransformState::get(const VPValue *Def, const VPLane &Lane) {
253252
if (Def->isLiveIn())

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ using namespace llvm;
2121

2222
#define DEBUG_TYPE "vplan"
2323

24-
VPTypeAnalysis::VPTypeAnalysis(const VPlan &Plan)
25-
: Ctx(Plan.getScalarHeader()->getIRBasicBlock()->getContext()) {
24+
VPTypeAnalysis::VPTypeAnalysis(const VPlan &Plan) : Ctx(Plan.getContext()) {
2625
if (auto LoopRegion = Plan.getVectorLoopRegion()) {
2726
if (const auto *CanIV = dyn_cast<VPCanonicalIVPHIRecipe>(
2827
&LoopRegion->getEntryBasicBlock()->front())) {
@@ -501,7 +500,7 @@ SmallVector<VPRegisterUsage, 8> llvm::calculateRegisterUsageForPlan(
501500

502501
LLVM_DEBUG(dbgs() << "LV(REG): Calculating max register usage:\n");
503502

504-
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
503+
VPTypeAnalysis TypeInfo(Plan);
505504

506505
const auto &TTICapture = TTI;
507506
auto GetRegUsage = [&TTICapture](Type *Ty, ElementCount VF) -> unsigned {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ class VPTypeAnalysis {
5858
Type *inferScalarTypeForRecipe(const VPReplicateRecipe *R);
5959

6060
public:
61-
VPTypeAnalysis(Type *CanonicalIVTy)
62-
: CanonicalIVTy(CanonicalIVTy), Ctx(CanonicalIVTy->getContext()) {}
63-
6461
VPTypeAnalysis(const VPlan &Plan);
6562

6663
/// Infer the type of \p V. Returns the scalar type of \p V.

llvm/lib/Transforms/Vectorize/VPlanHelpers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ struct VPCostContext {
351351
TargetTransformInfo::TargetCostKind CostKind;
352352

353353
VPCostContext(const TargetTransformInfo &TTI, const TargetLibraryInfo &TLI,
354-
Type *CanIVTy, LoopVectorizationCostModel &CM,
354+
const VPlan &Plan, LoopVectorizationCostModel &CM,
355355
TargetTransformInfo::TargetCostKind CostKind)
356-
: TTI(TTI), TLI(TLI), Types(CanIVTy), LLVMCtx(CanIVTy->getContext()),
357-
CM(CM), CostKind(CostKind) {}
356+
: TTI(TTI), TLI(TLI), Types(Plan), LLVMCtx(Plan.getContext()), CM(CM),
357+
CostKind(CostKind) {}
358358

359359
/// Return the cost for \p UI with \p VF using the legacy cost model as
360360
/// fallback until computing the cost of all recipes migrates to VPlan.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
571571
Kind, FPBinOp, StartV, CanonicalIV, Step, "offset.idx");
572572

573573
// Truncate base induction if needed.
574-
Type *CanonicalIVType = CanonicalIV->getScalarType();
575-
VPTypeAnalysis TypeInfo(CanonicalIVType);
574+
VPTypeAnalysis TypeInfo(Plan);
576575
Type *ResultTy = TypeInfo.inferScalarType(BaseIV);
577576
if (TruncI) {
578577
Type *TruncTy = TruncI->getType();
@@ -868,7 +867,7 @@ optimizeLatchExitInductionUser(VPlan &Plan, VPTypeAnalysis &TypeInfo,
868867
void VPlanTransforms::optimizeInductionExitUsers(
869868
VPlan &Plan, DenseMap<VPValue *, VPValue *> &EndValues) {
870869
VPBlockBase *MiddleVPBB = Plan.getMiddleBlock();
871-
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
870+
VPTypeAnalysis TypeInfo(Plan);
872871
for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks()) {
873872
for (VPRecipeBase &R : ExitVPBB->phis()) {
874873
auto *ExitIRI = cast<VPIRPhi>(&R);
@@ -1041,7 +1040,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10411040
#ifndef NDEBUG
10421041
// Verify that the cached type info is for both A and its users is still
10431042
// accurate by comparing it to freshly computed types.
1044-
VPTypeAnalysis TypeInfo2(Plan->getCanonicalIV()->getScalarType());
1043+
VPTypeAnalysis TypeInfo2(*Plan);
10451044
assert(TypeInfo.inferScalarType(A) == TypeInfo2.inferScalarType(A));
10461045
for (VPUser *U : A->users()) {
10471046
auto *R = cast<VPRecipeBase>(U);
@@ -1221,7 +1220,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
12211220
void VPlanTransforms::simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy) {
12221221
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
12231222
Plan.getEntry());
1224-
VPTypeAnalysis TypeInfo(&CanonicalIVTy);
1223+
VPTypeAnalysis TypeInfo(Plan);
12251224
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
12261225
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
12271226
simplifyRecipe(R, TypeInfo);
@@ -1799,8 +1798,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
17991798
// other uses have different types for their operands, making them invalidly
18001799
// typed.
18011800
DenseMap<VPValue *, VPWidenCastRecipe *> ProcessedTruncs;
1802-
Type *CanonicalIVType = Plan.getCanonicalIV()->getScalarType();
1803-
VPTypeAnalysis TypeInfo(CanonicalIVType);
1801+
VPTypeAnalysis TypeInfo(Plan);
18041802
VPBasicBlock *PH = Plan.getVectorPreheader();
18051803
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
18061804
vp_depth_first_deep(Plan.getVectorLoopRegion()))) {
@@ -1828,8 +1826,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
18281826
assert(OldResTy->isIntegerTy() && "only integer types supported");
18291827
(void)OldResSizeInBits;
18301828

1831-
LLVMContext &Ctx = CanonicalIVType->getContext();
1832-
auto *NewResTy = IntegerType::get(Ctx, NewResSizeInBits);
1829+
auto *NewResTy = IntegerType::get(Plan.getContext(), NewResSizeInBits);
18331830

18341831
// Any wrapping introduced by shrinking this operation shouldn't be
18351832
// considered undefined behavior. So, we can't unconditionally copy
@@ -2172,9 +2169,7 @@ static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask,
21722169

21732170
/// Replace recipes with their EVL variants.
21742171
static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
2175-
Type *CanonicalIVType = Plan.getCanonicalIV()->getScalarType();
2176-
VPTypeAnalysis TypeInfo(CanonicalIVType);
2177-
LLVMContext &Ctx = CanonicalIVType->getContext();
2172+
VPTypeAnalysis TypeInfo(Plan);
21782173
VPValue *AllOneMask = Plan.getTrue();
21792174
VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
21802175
VPBasicBlock *Header = LoopRegion->getEntryBasicBlock();
@@ -2213,9 +2208,9 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
22132208
VPValue *MaxEVL = &Plan.getVF();
22142209
// Emit VPScalarCastRecipe in preheader if VF is not a 32 bits integer.
22152210
VPBuilder Builder(LoopRegion->getPreheaderVPBB());
2216-
MaxEVL = Builder.createScalarZExtOrTrunc(MaxEVL, Type::getInt32Ty(Ctx),
2217-
TypeInfo.inferScalarType(MaxEVL),
2218-
DebugLoc());
2211+
MaxEVL = Builder.createScalarZExtOrTrunc(
2212+
MaxEVL, Type::getInt32Ty(Plan.getContext()),
2213+
TypeInfo.inferScalarType(MaxEVL), DebugLoc());
22192214

22202215
Builder.setInsertPoint(Header, Header->getFirstNonPhi());
22212216
VPValue *PrevEVL =
@@ -2230,7 +2225,7 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
22302225
m_VPValue(V1), m_VPValue(V2))))
22312226
continue;
22322227
VPValue *Imm = Plan.getOrAddLiveIn(
2233-
ConstantInt::getSigned(Type::getInt32Ty(Ctx), -1));
2228+
ConstantInt::getSigned(Type::getInt32Ty(Plan.getContext()), -1));
22342229
VPWidenIntrinsicRecipe *VPSplice = new VPWidenIntrinsicRecipe(
22352230
Intrinsic::experimental_vp_splice,
22362231
{V1, V2, Imm, AllOneMask, PrevEVL, &EVL},
@@ -2370,7 +2365,7 @@ void VPlanTransforms::addExplicitVectorLength(
23702365
Builder.setInsertPoint(CanonicalIVIncrement);
23712366
VPValue *OpVPEVL = VPEVL;
23722367

2373-
auto *I32Ty = Type::getInt32Ty(CanIVTy->getContext());
2368+
auto *I32Ty = Type::getInt32Ty(Plan.getContext());
23742369
OpVPEVL = Builder.createScalarZExtOrTrunc(
23752370
OpVPEVL, CanIVTy, I32Ty, CanonicalIVIncrement->getDebugLoc());
23762371

@@ -2712,7 +2707,7 @@ expandVPWidenIntOrFpInduction(VPWidenIntOrFpInductionRecipe *WidenIVR,
27122707

27132708
// Construct the initial value of the vector IV in the vector loop preheader.
27142709
Type *IVIntTy =
2715-
IntegerType::get(StepTy->getContext(), StepTy->getScalarSizeInBits());
2710+
IntegerType::get(Plan->getContext(), StepTy->getScalarSizeInBits());
27162711
VPValue *Init = Builder.createNaryOp(VPInstruction::StepVector, {}, IVIntTy);
27172712
if (StepTy->isFloatingPointTy())
27182713
Init = Builder.createWidenCast(Instruction::UIToFP, Init, StepTy);
@@ -2841,7 +2836,7 @@ void VPlanTransforms::dissolveLoopRegions(VPlan &Plan) {
28412836

28422837
void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan,
28432838
Type &CanonicalIVTy) {
2844-
VPTypeAnalysis TypeInfo(&CanonicalIVTy);
2839+
VPTypeAnalysis TypeInfo(Plan);
28452840
SmallVector<VPRecipeBase *> ToRemove;
28462841
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
28472842
vp_depth_first_deep(Plan.getEntry()))) {
@@ -3408,9 +3403,7 @@ void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF,
34083403
if (VF.isScalable() || !VectorLoop)
34093404
return;
34103405

3411-
VPCanonicalIVPHIRecipe *CanonicalIV = Plan.getCanonicalIV();
3412-
Type *CanonicalIVType = CanonicalIV->getScalarType();
3413-
VPTypeAnalysis TypeInfo(CanonicalIVType);
3406+
VPTypeAnalysis TypeInfo(Plan);
34143407

34153408
unsigned FixedVF = VF.getFixedValue();
34163409
SmallVector<VPInterleaveRecipe *> StoreGroups;

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct VPlanTransforms {
122122
static void clearReductionWrapFlags(VPlan &Plan);
123123

124124
/// Explicitly unroll \p Plan by \p UF.
125-
static void unrollByUF(VPlan &Plan, unsigned UF, LLVMContext &Ctx);
125+
static void unrollByUF(VPlan &Plan, unsigned UF);
126126

127127
/// Replace each VPReplicateRecipe outside on any replicate region in \p Plan
128128
/// with \p VF single-scalar recipes.

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ class UnrollState {
7474
}
7575

7676
public:
77-
UnrollState(VPlan &Plan, unsigned UF, LLVMContext &Ctx)
78-
: Plan(Plan), UF(UF), TypeInfo(Plan.getCanonicalIV()->getScalarType()) {}
77+
UnrollState(VPlan &Plan, unsigned UF) : Plan(Plan), UF(UF), TypeInfo(Plan) {}
7978

8079
void unrollBlock(VPBlockBase *VPB);
8180

@@ -409,7 +408,7 @@ void UnrollState::unrollBlock(VPBlockBase *VPB) {
409408
}
410409
}
411410

412-
void VPlanTransforms::unrollByUF(VPlan &Plan, unsigned UF, LLVMContext &Ctx) {
411+
void VPlanTransforms::unrollByUF(VPlan &Plan, unsigned UF) {
413412
assert(UF > 0 && "Unroll factor must be positive");
414413
Plan.setUF(UF);
415414
auto Cleanup = make_scope_exit([&Plan]() {
@@ -431,7 +430,7 @@ void VPlanTransforms::unrollByUF(VPlan &Plan, unsigned UF, LLVMContext &Ctx) {
431430
return;
432431
}
433432

434-
UnrollState Unroller(Plan, UF, Ctx);
433+
UnrollState Unroller(Plan, UF);
435434

436435
// Iterate over all blocks in the plan starting from Entry, and unroll
437436
// recipes inside them. This includes the vector preheader and middle blocks,

0 commit comments

Comments
 (0)