Skip to content

Commit 971caa3

Browse files
committed
[VPlan] Simplify VPDerivedIVRecipe.
1 parent 009b77f commit 971caa3

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ Value *VPInstruction::generate(VPTransformState &State) {
384384
IRBuilderBase &Builder = State.Builder;
385385

386386
if (Instruction::isBinaryOp(getOpcode())) {
387-
bool OnlyFirstLaneUsed = vputils::onlyFirstLaneUsed(this);
387+
bool OnlyFirstLaneUsed =
388+
vputils::onlyFirstLaneUsed(this) || all_of(operands(), [](VPValue *Op) {
389+
return vputils::isUniformAfterVectorization(Op);
390+
});
388391
Value *A = State.get(getOperand(0), OnlyFirstLaneUsed);
389392
Value *B = State.get(getOperand(1), OnlyFirstLaneUsed);
390393
auto *Res =
@@ -692,12 +695,8 @@ void VPInstruction::execute(VPTransformState &State) {
692695
if (!hasResult())
693696
return;
694697
assert(GeneratedValue && "generate must produce a value");
695-
assert(
696-
(GeneratedValue->getType()->isVectorTy() == !GeneratesPerFirstLaneOnly ||
697-
State.VF.isScalar()) &&
698-
"scalar value but not only first lane defined");
699698
State.set(this, GeneratedValue,
700-
/*IsScalar*/ GeneratesPerFirstLaneOnly);
699+
/*IsScalar*/ !GeneratedValue->getType()->isVectorTy());
701700
}
702701

703702
bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,32 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
969969

970970
if (match(&R, m_c_Mul(m_VPValue(A), m_SpecificInt(1))))
971971
return R.getVPSingleValue()->replaceAllUsesWith(A);
972+
973+
if (auto *DerivedIV = dyn_cast<VPDerivedIVRecipe>(&R)) {
974+
VPValue *Start = DerivedIV->getStartValue();
975+
VPValue *Step = DerivedIV->getStepValue();
976+
VPValue *Op = DerivedIV->getOperand(1);
977+
if (Step->isLiveIn() && Start->isLiveIn()) {
978+
auto *StartC = dyn_cast<ConstantInt>(Start->getLiveInIRValue());
979+
auto *StepC = dyn_cast<ConstantInt>(Step->getLiveInIRValue());
980+
if (Op->isLiveIn()) {
981+
auto *OpC = dyn_cast_if_present<ConstantInt>(Op->getUnderlyingValue());
982+
if (OpC && OpC->getType() == StepC->getType() && StartC->getType()) {
983+
APInt Folded =
984+
StartC->getValue() + OpC->getValue() * StepC->getValue();
985+
DerivedIV->replaceAllUsesWith(
986+
R.getParent()->getPlan()->getOrAddLiveIn(
987+
ConstantInt::get(TypeInfo.getContext(), Folded)));
988+
}
989+
}
990+
if (StartC && StartC->isZero() && StepC && StepC->isOne() &&
991+
TypeInfo.inferScalarType(DerivedIV) ==
992+
TypeInfo.inferScalarType(DerivedIV->getOperand(1))) {
993+
DerivedIV->replaceAllUsesWith(DerivedIV->getOperand(1));
994+
DerivedIV->eraseFromParent();
995+
}
996+
}
997+
}
972998
}
973999

9741000
/// Move loop-invariant recipes out of the vector loop region in \p Plan.

0 commit comments

Comments
 (0)