Skip to content

Commit ad49166

Browse files
committed
Extract the negating subtraction to its own recipe
1 parent 132f245 commit ad49166

File tree

3 files changed

+60
-53
lines changed

3 files changed

+60
-53
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8936,6 +8936,20 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
89368936
std::swap(BinOp, Accumulator);
89378937

89388938
unsigned ReductionOpcode = Reduction->getOpcode();
8939+
if (ReductionOpcode == Instruction::Sub) {
8940+
VPBasicBlock *ParentBlock = Builder.getInsertBlock();
8941+
if (!ParentBlock)
8942+
return nullptr;
8943+
8944+
auto *const Zero = ConstantInt::get(Reduction->getType(), 0);
8945+
SmallVector<VPValue *, 2> Ops;
8946+
Ops.push_back(Plan.getOrAddLiveIn(Zero));
8947+
Ops.push_back(cast<VPWidenRecipe>(BinOp->getDefiningRecipe()));
8948+
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()));
8949+
ParentBlock->appendRecipe(BinOp->getDefiningRecipe());
8950+
ReductionOpcode = Instruction::Add;
8951+
}
8952+
89398953
if (CM.blockNeedsPredicationForAnyReason(Reduction->getParent())) {
89408954
assert((ReductionOpcode == Instruction::Add ||
89418955
ReductionOpcode == Instruction::Sub) &&

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,8 @@ void VPPartialReductionRecipe::execute(VPTransformState &State) {
339339
Value *PhiVal = State.get(getOperand(1));
340340
assert(PhiVal && BinOpVal && "Phi and Mul must be set");
341341

342-
unsigned Opcode = getOpcode();
343-
344-
if (Opcode == Instruction::Sub) {
345-
bool HasNSW = cast<Instruction>(BinOpVal)->hasNoSignedWrap();
346-
BinOpVal = Builder.CreateNeg(BinOpVal, "", HasNSW);
347-
Opcode = Instruction::Add;
348-
}
349-
350-
assert(Opcode == Instruction::Add && "Unhandled partial reduction opcode");
342+
assert(getOpcode() == Instruction::Add &&
343+
"Unhandled partial reduction opcode");
351344

352345
Type *RetTy = PhiVal->getType();
353346

0 commit comments

Comments
 (0)