@@ -716,7 +716,8 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
716716// / Check if \p VPV is an untruncated wide induction, either before or after the
717717// / increment. If so return the header IV (before the increment), otherwise
718718// / return null.
719- static VPWidenInductionRecipe *getOptimizableIVOf (VPValue *VPV) {
719+ static VPWidenInductionRecipe *getOptimizableIVOf (VPValue *VPV,
720+ ScalarEvolution &SE) {
720721 auto *WideIV = dyn_cast<VPWidenInductionRecipe>(VPV);
721722 if (WideIV) {
722723 // VPV itself is a wide induction, separately compute the end value for exit
@@ -753,13 +754,13 @@ static VPWidenInductionRecipe *getOptimizableIVOf(VPValue *VPV) {
753754 // IVStep will be the negated step of the subtraction. Check if Step == -1
754755 // * IVStep.
755756 VPValue *Step;
756- if (!match (VPV, m_Sub (m_VPValue (), m_VPValue (Step))) ||
757- !Step->isLiveIn () || !IVStep->isLiveIn ())
757+ if (!match (VPV, m_Sub (m_VPValue (), m_VPValue (Step))))
758758 return false ;
759- auto *StepCI = dyn_cast<ConstantInt>(Step->getLiveInIRValue ());
760- auto *IVStepCI = dyn_cast<ConstantInt>(IVStep->getLiveInIRValue ());
761- return StepCI && IVStepCI &&
762- StepCI->getValue () == (-1 * IVStepCI->getValue ());
759+ const SCEV *IVStepSCEV = vputils::getSCEVExprForVPValue (IVStep, SE);
760+ const SCEV *StepSCEV = vputils::getSCEVExprForVPValue (Step, SE);
761+ return !isa<SCEVCouldNotCompute>(IVStepSCEV) &&
762+ !isa<SCEVCouldNotCompute>(StepSCEV) &&
763+ IVStepSCEV == SE.getNegativeSCEV (StepSCEV);
763764 }
764765 default :
765766 return ID.getKind () == InductionDescriptor::IK_PtrInduction &&
@@ -776,15 +777,16 @@ static VPWidenInductionRecipe *getOptimizableIVOf(VPValue *VPV) {
776777static VPValue *optimizeEarlyExitInductionUser (VPlan &Plan,
777778 VPTypeAnalysis &TypeInfo,
778779 VPBlockBase *PredVPBB,
779- VPValue *Op) {
780+ VPValue *Op,
781+ ScalarEvolution &SE) {
780782 VPValue *Incoming, *Mask;
781783 if (!match (Op, m_VPInstruction<VPInstruction::ExtractLane>(
782784 m_VPInstruction<VPInstruction::FirstActiveLane>(
783785 m_VPValue (Mask)),
784786 m_VPValue (Incoming))))
785787 return nullptr ;
786788
787- auto *WideIV = getOptimizableIVOf (Incoming);
789+ auto *WideIV = getOptimizableIVOf (Incoming, SE );
788790 if (!WideIV)
789791 return nullptr ;
790792
@@ -827,15 +829,14 @@ static VPValue *optimizeEarlyExitInductionUser(VPlan &Plan,
827829
828830// / Attempts to optimize the induction variable exit values for users in the
829831// / exit block coming from the latch in the original scalar loop.
830- static VPValue *
831- optimizeLatchExitInductionUser (VPlan &Plan, VPTypeAnalysis &TypeInfo,
832- VPBlockBase *PredVPBB, VPValue *Op,
833- DenseMap<VPValue *, VPValue *> &EndValues) {
832+ static VPValue *optimizeLatchExitInductionUser (
833+ VPlan &Plan, VPTypeAnalysis &TypeInfo, VPBlockBase *PredVPBB, VPValue *Op,
834+ DenseMap<VPValue *, VPValue *> &EndValues, ScalarEvolution &SE) {
834835 VPValue *Incoming;
835836 if (!match (Op, m_ExtractLastElement (m_VPValue (Incoming))))
836837 return nullptr ;
837838
838- auto *WideIV = getOptimizableIVOf (Incoming);
839+ auto *WideIV = getOptimizableIVOf (Incoming, SE );
839840 if (!WideIV)
840841 return nullptr ;
841842
@@ -874,7 +875,8 @@ optimizeLatchExitInductionUser(VPlan &Plan, VPTypeAnalysis &TypeInfo,
874875}
875876
876877void VPlanTransforms::optimizeInductionExitUsers (
877- VPlan &Plan, DenseMap<VPValue *, VPValue *> &EndValues) {
878+ VPlan &Plan, DenseMap<VPValue *, VPValue *> &EndValues,
879+ ScalarEvolution &SE) {
878880 VPBlockBase *MiddleVPBB = Plan.getMiddleBlock ();
879881 VPTypeAnalysis TypeInfo (Plan);
880882 for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks ()) {
@@ -884,11 +886,12 @@ void VPlanTransforms::optimizeInductionExitUsers(
884886 for (auto [Idx, PredVPBB] : enumerate(ExitVPBB->getPredecessors ())) {
885887 VPValue *Escape = nullptr ;
886888 if (PredVPBB == MiddleVPBB)
887- Escape = optimizeLatchExitInductionUser (
888- Plan, TypeInfo, PredVPBB, ExitIRI->getOperand (Idx), EndValues);
889+ Escape = optimizeLatchExitInductionUser (Plan, TypeInfo, PredVPBB,
890+ ExitIRI->getOperand (Idx),
891+ EndValues, SE);
889892 else
890893 Escape = optimizeEarlyExitInductionUser (Plan, TypeInfo, PredVPBB,
891- ExitIRI->getOperand (Idx));
894+ ExitIRI->getOperand (Idx), SE );
892895 if (Escape)
893896 ExitIRI->setOperand (Idx, Escape);
894897 }
0 commit comments