Skip to content

Commit 2db0315

Browse files
committed
[VPlan] Check VPValue step in isCanonical (NFCI).
Update the isCanonical() implementations to check the VPValue step operand instead of the step in the induction descriptor. At the moment this is NFC, but it enables further optimizations if the step is replaced by a constant in D147783. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D147891
1 parent 83ce139 commit 2db0315

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/ADT/Twine.h"
3434
#include "llvm/ADT/ilist.h"
3535
#include "llvm/ADT/ilist_node.h"
36+
#include "llvm/Analysis/IVDescriptors.h"
3637
#include "llvm/Analysis/LoopInfo.h"
3738
#include "llvm/Analysis/VectorUtils.h"
3839
#include "llvm/IR/DebugLoc.h"
@@ -47,7 +48,6 @@ namespace llvm {
4748

4849
class BasicBlock;
4950
class DominatorTree;
50-
class InductionDescriptor;
5151
class InnerLoopVectorizer;
5252
class IRBuilderBase;
5353
class LoopInfo;
@@ -1848,9 +1848,11 @@ class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
18481848
return true;
18491849
}
18501850

1851-
/// Check if the induction described by \p ID is canonical, i.e. has the same
1852-
/// start, step (of 1), and type as the canonical IV.
1853-
bool isCanonical(const InductionDescriptor &ID, Type *Ty) const;
1851+
/// Check if the induction described by \p Kind, /p Start and \p Step is
1852+
/// canonical, i.e. has the same start, step (of 1), and type as the
1853+
/// canonical IV.
1854+
bool isCanonical(InductionDescriptor::InductionKind Kind, VPValue *Start,
1855+
VPValue *Step, Type *Ty) const;
18541856
};
18551857

18561858
/// A recipe for generating the active lane mask for the vector loop that is

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,13 @@ void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O, const Twine &Indent,
735735
#endif
736736

737737
bool VPWidenIntOrFpInductionRecipe::isCanonical() const {
738+
// The step may be defined by a recipe in the preheader (e.g. if it requires
739+
// SCEV expansion), but for the canonical induction the step is required to be
740+
// 1, which is represented as live-in.
741+
if (getStepValue()->getDefiningRecipe())
742+
return false;
743+
auto *StepC = dyn_cast<ConstantInt>(getStepValue()->getLiveInIRValue());
738744
auto *StartC = dyn_cast<ConstantInt>(getStartValue()->getLiveInIRValue());
739-
auto *StepC = dyn_cast<SCEVConstant>(getInductionDescriptor().getStep());
740745
return StartC && StartC->isZero() && StepC && StepC->isOne();
741746
}
742747

@@ -1091,20 +1096,22 @@ void VPCanonicalIVPHIRecipe::print(raw_ostream &O, const Twine &Indent,
10911096
}
10921097
#endif
10931098

1094-
bool VPCanonicalIVPHIRecipe::isCanonical(const InductionDescriptor &ID,
1095-
Type *Ty) const {
1096-
if (Ty != getScalarType())
1099+
bool VPCanonicalIVPHIRecipe::isCanonical(
1100+
InductionDescriptor::InductionKind Kind, VPValue *Start, VPValue *Step,
1101+
Type *Ty) const {
1102+
// The types must match and it must be an integer induction.
1103+
if (Ty != getScalarType() || Kind != InductionDescriptor::IK_IntInduction)
1104+
return false;
1105+
// Start must match the start value of this canonical induction.
1106+
if (Start != getStartValue())
10971107
return false;
1098-
// The start value of ID must match the start value of this canonical
1099-
// induction.
1100-
if (getStartValue()->getLiveInIRValue() != ID.getStartValue())
1108+
1109+
// If the step is defined by a recipe, it is not a ConstantInt.
1110+
if (Step->getDefiningRecipe())
11011111
return false;
11021112

1103-
ConstantInt *Step = ID.getConstIntStepValue();
1104-
// ID must also be incremented by one. IK_IntInduction always increment the
1105-
// induction by Step, but the binary op may not be set.
1106-
return ID.getKind() == InductionDescriptor::IK_IntInduction && Step &&
1107-
Step->isOne();
1113+
ConstantInt *StepC = dyn_cast<ConstantInt>(Step->getLiveInIRValue());
1114+
return StepC && StepC->isOne();
11081115
}
11091116

11101117
bool VPWidenPointerInductionRecipe::onlyScalarsGenerated(ElementCount VF) {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ void VPlanTransforms::optimizeInductions(VPlan &Plan, ScalarEvolution &SE) {
520520
VPValue *Step =
521521
vputils::getOrCreateVPValueForSCEVExpr(Plan, ID.getStep(), SE);
522522
VPValue *BaseIV = CanonicalIV;
523-
if (!CanonicalIV->isCanonical(ID, ResultTy)) {
523+
if (!CanonicalIV->isCanonical(ID.getKind(), WideIV->getStartValue(), Step,
524+
ResultTy)) {
524525
BaseIV = new VPDerivedIVRecipe(ID, WideIV->getStartValue(), CanonicalIV,
525526
Step, ResultTy);
526527
HeaderVPBB->insert(BaseIV->getDefiningRecipe(), IP);

0 commit comments

Comments
 (0)