Skip to content

Commit e6a4677

Browse files
committed
[VPlan] Replace VPWidenCastRecipe by VPInstructionWithType (NFC) (WIP).
WIP as it depends on llvm#129706.
1 parent 29a14c1 commit e6a4677

24 files changed

+240
-296
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,13 @@ class VPBuilder {
246246
new VPDerivedIVRecipe(Kind, FPBinOp, Start, Current, Step, Name));
247247
}
248248

249-
VPInstruction *createScalarCast(Instruction::CastOps Opcode, VPValue *Op,
250-
Type *ResultTy, DebugLoc DL) {
251-
return tryInsertInstruction(
252-
new VPInstructionWithType(Opcode, Op, ResultTy, DL));
253-
}
254-
255-
VPWidenCastRecipe *createWidenCast(Instruction::CastOps Opcode, VPValue *Op,
256-
Type *ResultTy) {
257-
return tryInsertInstruction(new VPWidenCastRecipe(Opcode, Op, ResultTy));
249+
VPInstructionWithType *createCast(Instruction::CastOps Opcode, VPValue *Op,
250+
Type *ResultTy, DebugLoc DL = {},
251+
const Twine &Name = "",
252+
Instruction *CI = nullptr) {
253+
auto *VPI = new VPInstructionWithType(Opcode, {Op}, ResultTy, DL, Name);
254+
VPI->setUnderlyingValue(CI);
255+
return tryInsertInstruction(VPI);
258256
}
259257

260258
VPScalarIVStepsRecipe *

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4434,8 +4434,7 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
44344434
[](const auto *R) { return Instruction::Load; })
44354435
.Case<VPWidenCallRecipe, VPWidenIntrinsicRecipe>(
44364436
[](const auto *R) { return Instruction::Call; })
4437-
.Case<VPInstruction, VPWidenRecipe, VPReplicateRecipe,
4438-
VPWidenCastRecipe>(
4437+
.Case<VPInstruction, VPWidenRecipe, VPReplicateRecipe>(
44394438
[](const auto *R) { return R->getOpcode(); })
44404439
.Case<VPInterleaveRecipe>([](const VPInterleaveRecipe *R) {
44414440
return R->getStoredValues().empty() ? Instruction::Load
@@ -4496,14 +4495,11 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
44964495
if (EphemeralRecipes.contains(&R))
44974496
continue;
44984497
// Continue early if the recipe is considered to not produce a vector
4499-
// result. Note that this includes VPInstruction where some opcodes may
4500-
// produce a vector, to preserve existing behavior as VPInstructions model
4501-
// aspects not directly mapped to existing IR instructions.
4498+
// result.
45024499
switch (R.getVPDefID()) {
45034500
case VPDef::VPDerivedIVSC:
45044501
case VPDef::VPScalarIVStepsSC:
45054502
case VPDef::VPReplicateSC:
4506-
case VPDef::VPInstructionSC:
45074503
case VPDef::VPCanonicalIVPHISC:
45084504
case VPDef::VPVectorPointerSC:
45094505
case VPDef::VPReverseVectorPointerSC:
@@ -4516,7 +4512,6 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
45164512
case VPDef::VPActiveLaneMaskPHISC:
45174513
case VPDef::VPWidenCallSC:
45184514
case VPDef::VPWidenCanonicalIVSC:
4519-
case VPDef::VPWidenCastSC:
45204515
case VPDef::VPWidenGEPSC:
45214516
case VPDef::VPWidenIntrinsicSC:
45224517
case VPDef::VPWidenSC:
@@ -4533,6 +4528,15 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
45334528
case VPDef::VPWidenStoreEVLSC:
45344529
case VPDef::VPWidenStoreSC:
45354530
break;
4531+
case VPDef::VPInstructionSC: {
4532+
// Note that for VPInstruction some opcodes may produce a vector. To
4533+
// preserve existing behavior only consider them vector-generating if
4534+
// they are casts with an underlying value.
4535+
if (Instruction::isCast(cast<VPInstruction>(&R)->getOpcode()) &&
4536+
R.getVPSingleValue()->getUnderlyingValue())
4537+
break;
4538+
continue;
4539+
}
45364540
default:
45374541
llvm_unreachable("unhandled recipe");
45384542
}
@@ -8937,8 +8941,15 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
89378941
}
89388942

89398943
if (auto *CI = dyn_cast<CastInst>(Instr)) {
8940-
return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
8941-
*CI);
8944+
auto *VPI =
8945+
isa<PossiblyNonNegInst>(CI)
8946+
? new VPInstructionWithType(CI->getOpcode(), {Operands[0]},
8947+
CI->getType(), {CI->hasNonNeg()}, {})
8948+
: new VPInstructionWithType(CI->getOpcode(), {Operands[0]},
8949+
CI->getType(), {});
8950+
8951+
VPI->setUnderlyingValue(CI);
8952+
return VPI;
89428953
}
89438954

89448955
return tryToWiden(Instr, Operands);
@@ -9060,9 +9071,9 @@ static VPInstruction *addResumePhiRecipeForInduction(
90609071
// the widest induction) and thus may be wider than the induction here.
90619072
Type *ScalarTypeOfWideIV = TypeInfo.inferScalarType(WideIV);
90629073
if (ScalarTypeOfWideIV != TypeInfo.inferScalarType(EndValue)) {
9063-
EndValue = VectorPHBuilder.createScalarCast(Instruction::Trunc, EndValue,
9064-
ScalarTypeOfWideIV,
9065-
WideIV->getDebugLoc());
9074+
EndValue =
9075+
VectorPHBuilder.createCast(Instruction::Trunc, EndValue,
9076+
ScalarTypeOfWideIV, WideIV->getDebugLoc());
90669077
}
90679078

90689079
auto *ResumePhiRecipe =
@@ -9860,12 +9871,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
98609871
RdxDesc.getRecurrenceKind())) {
98619872
assert(!PhiR->isInLoop() && "Unexpected truncated inloop reduction!");
98629873
Type *RdxTy = RdxDesc.getRecurrenceType();
9863-
auto *Trunc =
9864-
new VPWidenCastRecipe(Instruction::Trunc, NewExitingVPV, RdxTy);
9874+
auto *Trunc = new VPInstructionWithType(Instruction::Trunc, NewExitingVPV,
9875+
RdxTy, {});
98659876
auto *Extnd =
98669877
RdxDesc.isSigned()
9867-
? new VPWidenCastRecipe(Instruction::SExt, Trunc, PhiTy)
9868-
: new VPWidenCastRecipe(Instruction::ZExt, Trunc, PhiTy);
9878+
? new VPInstructionWithType(Instruction::SExt, Trunc, PhiTy, {})
9879+
: new VPInstructionWithType(Instruction::ZExt, Trunc, PhiTy, {});
98699880

98709881
Trunc->insertAfter(NewExitingVPV->getDefiningRecipe());
98719882
Extnd->insertAfter(Trunc);

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
519519
case VPRecipeBase::VPReverseVectorPointerSC:
520520
case VPRecipeBase::VPWidenCallSC:
521521
case VPRecipeBase::VPWidenCanonicalIVSC:
522-
case VPRecipeBase::VPWidenCastSC:
523522
case VPRecipeBase::VPWidenGEPSC:
524523
case VPRecipeBase::VPWidenIntrinsicSC:
525524
case VPRecipeBase::VPWidenSC:
@@ -598,13 +597,15 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
598597
DisjointFlagsTy(bool IsDisjoint) : IsDisjoint(IsDisjoint) {}
599598
};
600599

600+
struct NonNegFlagsTy {
601+
char NonNeg : 1;
602+
NonNegFlagsTy(bool IsNonNeg = false) : NonNeg(IsNonNeg) {}
603+
};
604+
601605
private:
602606
struct ExactFlagsTy {
603607
char IsExact : 1;
604608
};
605-
struct NonNegFlagsTy {
606-
char NonNeg : 1;
607-
};
608609
struct FastMathFlagsTy {
609610
char AllowReassoc : 1;
610611
char NoNaNs : 1;
@@ -698,6 +699,12 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
698699
: VPSingleDefRecipe(SC, Operands, DL), OpType(OperationType::DisjointOp),
699700
DisjointFlags(DisjointFlags) {}
700701

702+
template <typename IterT>
703+
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands,
704+
NonNegFlagsTy NonNegFlags, DebugLoc DL = {})
705+
: VPSingleDefRecipe(SC, Operands, DL), OpType(OperationType::NonNegOp),
706+
NonNegFlags(NonNegFlags) {}
707+
701708
protected:
702709
template <typename IterT>
703710
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands,
@@ -710,7 +717,6 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
710717
return R->getVPDefID() == VPRecipeBase::VPInstructionSC ||
711718
R->getVPDefID() == VPRecipeBase::VPWidenSC ||
712719
R->getVPDefID() == VPRecipeBase::VPWidenGEPSC ||
713-
R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
714720
R->getVPDefID() == VPRecipeBase::VPWidenIntrinsicSC ||
715721
R->getVPDefID() == VPRecipeBase::VPReplicateSC ||
716722
R->getVPDefID() == VPRecipeBase::VPReverseVectorPointerSC ||
@@ -953,6 +959,12 @@ class VPInstruction : public VPRecipeWithIRFlags,
953959
VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
954960
FastMathFlags FMFs, DebugLoc DL = {}, const Twine &Name = "");
955961

962+
VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands,
963+
NonNegFlagsTy NonNegFlags, DebugLoc DL = {},
964+
const Twine &Name = "")
965+
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, NonNegFlags, DL),
966+
Opcode(Opcode), Name(Name.str()) {}
967+
956968
VP_CLASSOF_IMPL(VPDef::VPInstructionSC)
957969

958970
VPInstruction *clone() override {
@@ -1039,6 +1051,11 @@ class VPInstructionWithType : public VPInstruction {
10391051
Type *ResultTy, DebugLoc DL, const Twine &Name = "")
10401052
: VPInstruction(Opcode, Operands, DL, Name), ResultTy(ResultTy) {}
10411053

1054+
VPInstructionWithType(unsigned Opcode, ArrayRef<VPValue *> Operands,
1055+
Type *ResultTy, NonNegFlagsTy Flags, DebugLoc DL,
1056+
const Twine &Name = "")
1057+
: VPInstruction(Opcode, Operands, Flags, DL, Name), ResultTy(ResultTy) {}
1058+
10421059
static inline bool classof(const VPRecipeBase *R) {
10431060
auto *VPI = dyn_cast<VPInstruction>(R);
10441061
return VPI && Instruction::isCast(VPI->getOpcode());
@@ -1051,18 +1068,17 @@ class VPInstructionWithType : public VPInstruction {
10511068
VPInstruction *clone() override {
10521069
auto *New =
10531070
new VPInstructionWithType(getOpcode(), {getOperand(0)}, getResultType(),
1054-
getDebugLoc(), getName());
1071+
{}, getDebugLoc(), getName());
10551072
New->setUnderlyingValue(getUnderlyingValue());
1073+
New->transferFlags(*this);
10561074
return New;
10571075
}
10581076

10591077
void execute(VPTransformState &State) override;
10601078

10611079
/// Return the cost of this VPIRInstruction.
10621080
InstructionCost computeCost(ElementCount VF,
1063-
VPCostContext &Ctx) const override {
1064-
return 0;
1065-
}
1081+
VPCostContext &Ctx) const override;
10661082

10671083
Type *getResultType() const { return ResultTy; }
10681084

@@ -1180,58 +1196,6 @@ class VPWidenRecipe : public VPRecipeWithIRFlags {
11801196
#endif
11811197
};
11821198

1183-
/// VPWidenCastRecipe is a recipe to create vector cast instructions.
1184-
class VPWidenCastRecipe : public VPRecipeWithIRFlags {
1185-
/// Cast instruction opcode.
1186-
Instruction::CastOps Opcode;
1187-
1188-
/// Result type for the cast.
1189-
Type *ResultTy;
1190-
1191-
public:
1192-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1193-
CastInst &UI)
1194-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, UI), Opcode(Opcode),
1195-
ResultTy(ResultTy) {
1196-
assert(UI.getOpcode() == Opcode &&
1197-
"opcode of underlying cast doesn't match");
1198-
}
1199-
1200-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1201-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op), Opcode(Opcode),
1202-
ResultTy(ResultTy) {}
1203-
1204-
~VPWidenCastRecipe() override = default;
1205-
1206-
VPWidenCastRecipe *clone() override {
1207-
if (auto *UV = getUnderlyingValue())
1208-
return new VPWidenCastRecipe(Opcode, getOperand(0), ResultTy,
1209-
*cast<CastInst>(UV));
1210-
1211-
return new VPWidenCastRecipe(Opcode, getOperand(0), ResultTy);
1212-
}
1213-
1214-
VP_CLASSOF_IMPL(VPDef::VPWidenCastSC)
1215-
1216-
/// Produce widened copies of the cast.
1217-
void execute(VPTransformState &State) override;
1218-
1219-
/// Return the cost of this VPWidenCastRecipe.
1220-
InstructionCost computeCost(ElementCount VF,
1221-
VPCostContext &Ctx) const override;
1222-
1223-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1224-
/// Print the recipe.
1225-
void print(raw_ostream &O, const Twine &Indent,
1226-
VPSlotTracker &SlotTracker) const override;
1227-
#endif
1228-
1229-
Instruction::CastOps getOpcode() const { return Opcode; }
1230-
1231-
/// Returns the result type of the cast.
1232-
Type *getResultType() const { return ResultTy; }
1233-
};
1234-
12351199
/// A recipe for widening vector intrinsics.
12361200
class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags {
12371201
/// ID of the vector intrinsic to widen.

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
261261
// TODO: Use info from interleave group.
262262
return V->getUnderlyingValue()->getType();
263263
})
264-
.Case<VPWidenCastRecipe>(
265-
[](const VPWidenCastRecipe *R) { return R->getResultType(); })
266264
.Case<VPExpandSCEVRecipe>([](const VPExpandSCEVRecipe *R) {
267265
return R->getSCEV()->getType();
268266
})

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ using UnaryVPInstruction_match =
204204
template <typename Op0_t, unsigned Opcode>
205205
using AllUnaryRecipe_match =
206206
UnaryRecipe_match<Op0_t, Opcode, VPWidenRecipe, VPReplicateRecipe,
207-
VPWidenCastRecipe, VPInstruction>;
207+
VPInstruction>;
208208

209209
template <typename Op0_t, typename Op1_t, unsigned Opcode, bool Commutative,
210210
typename... RecipeTys>
@@ -220,7 +220,7 @@ template <typename Op0_t, typename Op1_t, unsigned Opcode,
220220
bool Commutative = false>
221221
using AllBinaryRecipe_match =
222222
BinaryRecipe_match<Op0_t, Op1_t, Opcode, Commutative, VPWidenRecipe,
223-
VPReplicateRecipe, VPWidenCastRecipe, VPInstruction>;
223+
VPReplicateRecipe, VPInstruction>;
224224

225225
template <unsigned Opcode, typename Op0_t>
226226
inline UnaryVPInstruction_match<Op0_t, Opcode>

0 commit comments

Comments
 (0)