Skip to content

Commit 36263a7

Browse files
committed
[LoopUtils] remove redundant opcode parameter; NFC
While here, rename the inaccurate getRecurrenceBinOp() because that was also used to get CmpInst opcodes. The recurrence/reduction kind should always refer to the expected opcode for a reduction. SLP appears to be the only direct caller of createSimpleTargetReduction(), and that calling code ideally should not be carrying around both an opcode and a reduction kind. This should allow us to generalize reduction matching to use intrinsics instead of only binops.
1 parent efc82c4 commit 36263a7

File tree

7 files changed

+36
-50
lines changed

7 files changed

+36
-50
lines changed

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ class RecurrenceDescriptor {
139139
/// Returns identity corresponding to the RecurrenceKind.
140140
static Constant *getRecurrenceIdentity(RecurKind K, Type *Tp);
141141

142-
/// Returns the opcode of binary operation corresponding to the
143-
/// RecurrenceKind.
144-
static unsigned getRecurrenceBinOp(RecurKind Kind);
142+
/// Returns the opcode corresponding to the RecurrenceKind.
143+
static unsigned getOpcode(RecurKind Kind);
145144

146145
/// Returns true if Phi is a reduction of type Kind and adds it to the
147146
/// RecurrenceDescriptor. If either \p DB is non-null or \p AC and \p DT are
@@ -178,9 +177,7 @@ class RecurrenceDescriptor {
178177

179178
RecurKind getRecurrenceKind() const { return Kind; }
180179

181-
unsigned getRecurrenceBinOp() const {
182-
return getRecurrenceBinOp(getRecurrenceKind());
183-
}
180+
unsigned getOpcode() const { return getOpcode(getRecurrenceKind()); }
184181

185182
FastMathFlags getFastMathFlags() const { return FMF; }
186183

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ Value *getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op,
366366
/// required to implement the reduction.
367367
/// Fast-math-flags are propagated using the IRBuilder's setting.
368368
Value *createSimpleTargetReduction(IRBuilderBase &B,
369-
const TargetTransformInfo *TTI,
370-
unsigned Opcode, Value *Src,
369+
const TargetTransformInfo *TTI, Value *Src,
371370
RecurKind RdxKind,
372371
ArrayRef<Value *> RedOps = None);
373372

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,7 @@ Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurKind K, Type *Tp) {
800800
}
801801
}
802802

803-
/// This function translates the recurrence kind to an LLVM binary operator.
804-
unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurKind Kind) {
803+
unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
805804
switch (Kind) {
806805
case RecurKind::Add:
807806
return Instruction::Add;
@@ -833,7 +832,7 @@ unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurKind Kind) {
833832
SmallVector<Instruction *, 4>
834833
RecurrenceDescriptor::getReductionOpChain(PHINode *Phi, Loop *L) const {
835834
SmallVector<Instruction *, 4> ReductionOperations;
836-
unsigned RedOp = getRecurrenceBinOp(Kind);
835+
unsigned RedOp = getOpcode(Kind);
837836

838837
// Search down from the Phi to the LoopExitInstr, looking for instructions
839838
// with a single user of the correct type for the reduction.

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ Value *llvm::getShuffleReduction(IRBuilderBase &Builder, Value *Src,
979979

980980
Value *llvm::createSimpleTargetReduction(IRBuilderBase &Builder,
981981
const TargetTransformInfo *TTI,
982-
unsigned Opcode, Value *Src,
983-
RecurKind RdxKind,
982+
Value *Src, RecurKind RdxKind,
984983
ArrayRef<Value *> RedOps) {
984+
unsigned Opcode = RecurrenceDescriptor::getOpcode(RdxKind);
985985
TargetTransformInfo::ReductionFlags RdxFlags;
986986
RdxFlags.IsMaxOp = RdxKind == RecurKind::SMax || RdxKind == RecurKind::UMax ||
987987
RdxKind == RecurKind::FMax;
@@ -991,42 +991,34 @@ Value *llvm::createSimpleTargetReduction(IRBuilderBase &Builder,
991991
return getShuffleReduction(Builder, Src, Opcode, RdxKind, RedOps);
992992

993993
auto *SrcVecEltTy = cast<VectorType>(Src->getType())->getElementType();
994-
switch (Opcode) {
995-
case Instruction::Add:
994+
switch (RdxKind) {
995+
case RecurKind::Add:
996996
return Builder.CreateAddReduce(Src);
997-
case Instruction::Mul:
997+
case RecurKind::Mul:
998998
return Builder.CreateMulReduce(Src);
999-
case Instruction::And:
999+
case RecurKind::And:
10001000
return Builder.CreateAndReduce(Src);
1001-
case Instruction::Or:
1001+
case RecurKind::Or:
10021002
return Builder.CreateOrReduce(Src);
1003-
case Instruction::Xor:
1003+
case RecurKind::Xor:
10041004
return Builder.CreateXorReduce(Src);
1005-
case Instruction::FAdd:
1005+
case RecurKind::FAdd:
10061006
return Builder.CreateFAddReduce(ConstantFP::getNegativeZero(SrcVecEltTy),
10071007
Src);
1008-
case Instruction::FMul:
1008+
case RecurKind::FMul:
10091009
return Builder.CreateFMulReduce(ConstantFP::get(SrcVecEltTy, 1.0), Src);
1010-
case Instruction::ICmp:
1011-
switch (RdxKind) {
1012-
case RecurKind::SMax:
1013-
return Builder.CreateIntMaxReduce(Src, true);
1014-
case RecurKind::SMin:
1015-
return Builder.CreateIntMinReduce(Src, true);
1016-
case RecurKind::UMax:
1017-
return Builder.CreateIntMaxReduce(Src, false);
1018-
case RecurKind::UMin:
1019-
return Builder.CreateIntMinReduce(Src, false);
1020-
default:
1021-
llvm_unreachable("Unexpected min/max reduction type");
1022-
}
1023-
case Instruction::FCmp:
1024-
assert((RdxKind == RecurKind::FMax || RdxKind == RecurKind::FMin) &&
1025-
"Unexpected min/max reduction type");
1026-
if (RdxKind == RecurKind::FMax)
1027-
return Builder.CreateFPMaxReduce(Src);
1028-
else
1029-
return Builder.CreateFPMinReduce(Src);
1010+
case RecurKind::SMax:
1011+
return Builder.CreateIntMaxReduce(Src, true);
1012+
case RecurKind::SMin:
1013+
return Builder.CreateIntMinReduce(Src, true);
1014+
case RecurKind::UMax:
1015+
return Builder.CreateIntMaxReduce(Src, false);
1016+
case RecurKind::UMin:
1017+
return Builder.CreateIntMinReduce(Src, false);
1018+
case RecurKind::FMax:
1019+
return Builder.CreateFPMaxReduce(Src);
1020+
case RecurKind::FMin:
1021+
return Builder.CreateFPMinReduce(Src);
10301022
default:
10311023
llvm_unreachable("Unhandled opcode");
10321024
}
@@ -1040,8 +1032,7 @@ Value *llvm::createTargetReduction(IRBuilderBase &B,
10401032
// descriptor.
10411033
IRBuilderBase::FastMathFlagGuard FMFGuard(B);
10421034
B.setFastMathFlags(Desc.getFastMathFlags());
1043-
return createSimpleTargetReduction(B, TTI, Desc.getRecurrenceBinOp(), Src,
1044-
Desc.getRecurrenceKind());
1035+
return createSimpleTargetReduction(B, TTI, Src, Desc.getRecurrenceKind());
10451036
}
10461037

10471038
void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) {

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,7 +4254,7 @@ void InnerLoopVectorizer::fixReduction(PHINode *Phi) {
42544254
RecurrenceDescriptor RdxDesc = Legal->getReductionVars()[Phi];
42554255
if (PreferPredicatedReductionSelect ||
42564256
TTI->preferPredicatedReductionSelect(
4257-
RdxDesc.getRecurrenceBinOp(), Phi->getType(),
4257+
RdxDesc.getOpcode(), Phi->getType(),
42584258
TargetTransformInfo::ReductionFlags())) {
42594259
auto *VecRdxPhi = cast<PHINode>(getOrCreateVectorValue(Phi, Part));
42604260
VecRdxPhi->setIncomingValueForBlock(
@@ -4296,7 +4296,7 @@ void InnerLoopVectorizer::fixReduction(PHINode *Phi) {
42964296

42974297
// Reduce all of the unrolled parts into a single vector.
42984298
Value *ReducedPartRdx = VectorLoopValueMap.getVectorValue(LoopExitInst, 0);
4299-
unsigned Op = RecurrenceDescriptor::getRecurrenceBinOp(RK);
4299+
unsigned Op = RecurrenceDescriptor::getOpcode(RK);
43004300

43014301
// The middle block terminator has already been assigned a DebugLoc here (the
43024302
// OrigLoop's single latch terminator). We want the whole middle block to
@@ -7325,7 +7325,7 @@ void LoopVectorizationCostModel::collectInLoopReductions() {
73257325

73267326
// If the target would prefer this reduction to happen "in-loop", then we
73277327
// want to record it as such.
7328-
unsigned Opcode = RdxDesc.getRecurrenceBinOp();
7328+
unsigned Opcode = RdxDesc.getOpcode();
73297329
if (!PreferInLoopReductions &&
73307330
!TTI.preferInLoopReduction(Opcode, Phi->getType(),
73317331
TargetTransformInfo::ReductionFlags()))

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7255,9 +7255,9 @@ class HorizontalReduction {
72557255
// FIXME: The builder should use an FMF guard. It should not be hard-coded
72567256
// to 'fast'.
72577257
assert(Builder.getFastMathFlags().isFast() && "Expected 'fast' FMF");
7258-
return createSimpleTargetReduction(
7259-
Builder, TTI, RdxTreeInst.getOpcode(), VectorizedValue,
7260-
RdxTreeInst.getKind(), ReductionOps.back());
7258+
return createSimpleTargetReduction(Builder, TTI, VectorizedValue,
7259+
RdxTreeInst.getKind(),
7260+
ReductionOps.back());
72617261
}
72627262

72637263
Value *TmpVec = VectorizedValue;

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent,
917917
printAsOperand(O, SlotTracker);
918918
O << " = ";
919919
getChainOp()->printAsOperand(O, SlotTracker);
920-
O << " + reduce." << Instruction::getOpcodeName(RdxDesc->getRecurrenceBinOp())
920+
O << " + reduce." << Instruction::getOpcodeName(RdxDesc->getOpcode())
921921
<< " (";
922922
getVecOp()->printAsOperand(O, SlotTracker);
923923
if (getCondOp()) {

0 commit comments

Comments
 (0)