@@ -6488,8 +6488,7 @@ class HorizontalReduction {
6488
6488
// in this case.
6489
6489
// Do not perform analysis of remaining operands of ParentStackElem.first
6490
6490
// instruction, this whole instruction is an extra argument.
6491
- RecurKind ParentRdxKind = getRdxKind (ParentStackElem.first );
6492
- ParentStackElem.second = getNumberOfOperands (ParentRdxKind);
6491
+ ParentStackElem.second = getNumberOfOperands (ParentStackElem.first );
6493
6492
} else {
6494
6493
// We ran into something like:
6495
6494
// ParentStackElem.first += ... + ExtraArg + ...
@@ -6590,7 +6589,6 @@ class HorizontalReduction {
6590
6589
if (match (I, m_Intrinsic<Intrinsic::minnum>(m_Value (), m_Value ())))
6591
6590
return RecurKind::FMin;
6592
6591
6593
-
6594
6592
if (auto *Select = dyn_cast<SelectInst>(I)) {
6595
6593
// These would also match llvm.{u,s}{min,max} intrinsic call
6596
6594
// if were not guarded by the SelectInst check above.
@@ -6660,64 +6658,54 @@ class HorizontalReduction {
6660
6658
return RecurKind::None;
6661
6659
}
6662
6660
6663
- // / Return true if this operation is a cmp+select idiom.
6664
- static bool isCmpSel (RecurKind Kind) {
6665
- return RecurrenceDescriptor::isIntMinMaxRecurrenceKind (Kind);
6666
- }
6667
-
6668
6661
// / Get the index of the first operand.
6669
- static unsigned getFirstOperandIndex (RecurKind Kind) {
6670
- // We allow calling this before 'Kind' is set, so handle that specially.
6671
- if (Kind == RecurKind::None)
6672
- return 0 ;
6673
- return isCmpSel (Kind) ? 1 : 0 ;
6662
+ static unsigned getFirstOperandIndex (Instruction *I) {
6663
+ return isa<SelectInst>(I) ? 1 : 0 ;
6674
6664
}
6675
6665
6676
6666
// / Total number of operands in the reduction operation.
6677
- static unsigned getNumberOfOperands (RecurKind Kind ) {
6678
- return isCmpSel (Kind ) ? 3 : 2 ;
6667
+ static unsigned getNumberOfOperands (Instruction *I ) {
6668
+ return isa<SelectInst>(I ) ? 3 : 2 ;
6679
6669
}
6680
6670
6681
6671
// / Checks if the instruction is in basic block \p BB.
6682
6672
// / For a min/max reduction check that both compare and select are in \p BB.
6683
- static bool hasSameParent (RecurKind Kind, Instruction *I, BasicBlock *BB,
6684
- bool IsRedOp) {
6685
- if (IsRedOp && isCmpSel (Kind) ) {
6686
- auto *Cmp = cast<Instruction>(cast<SelectInst>(I) ->getCondition ());
6687
- return I ->getParent () == BB && Cmp->getParent () == BB;
6673
+ static bool hasSameParent (Instruction *I, BasicBlock *BB, bool IsRedOp) {
6674
+ auto *Sel = dyn_cast<SelectInst>(I);
6675
+ if (IsRedOp && Sel ) {
6676
+ auto *Cmp = cast<Instruction>(Sel ->getCondition ());
6677
+ return Sel ->getParent () == BB && Cmp->getParent () == BB;
6688
6678
}
6689
6679
return I->getParent () == BB;
6690
6680
}
6691
6681
6692
6682
// / Expected number of uses for reduction operations/reduced values.
6693
- static bool hasRequiredNumberOfUses (RecurKind Kind, Instruction *I,
6694
- bool IsReductionOp) {
6695
- assert (Kind != RecurKind::None && " Reduction type not set" );
6683
+ static bool hasRequiredNumberOfUses (bool MatchCmpSel, Instruction *I) {
6696
6684
// SelectInst must be used twice while the condition op must have single
6697
6685
// use only.
6698
- if (isCmpSel (Kind))
6699
- return I->hasNUses (2 ) &&
6700
- (!IsReductionOp ||
6701
- cast<SelectInst>(I)->getCondition ()->hasOneUse ());
6686
+ if (MatchCmpSel) {
6687
+ if (auto *Sel = dyn_cast<SelectInst>(I))
6688
+ return Sel->hasNUses (2 ) && Sel->getCondition ()->hasOneUse ();
6689
+ return I->hasNUses (2 );
6690
+ }
6702
6691
6703
6692
// Arithmetic reduction operation must be used once only.
6704
6693
return I->hasOneUse ();
6705
6694
}
6706
6695
6707
6696
// / Initializes the list of reduction operations.
6708
- void initReductionOps (RecurKind Kind ) {
6709
- if (isCmpSel (Kind ))
6697
+ void initReductionOps (Instruction *I ) {
6698
+ if (isa<SelectInst>(I ))
6710
6699
ReductionOps.assign (2 , ReductionOpsType ());
6711
6700
else
6712
6701
ReductionOps.assign (1 , ReductionOpsType ());
6713
6702
}
6714
6703
6715
6704
// / Add all reduction operations for the reduction instruction \p I.
6716
- void addReductionOps (RecurKind Kind, Instruction *I) {
6717
- assert (Kind != RecurKind::None && " Expected reduction operation." );
6718
- if (isCmpSel (Kind)) {
6719
- ReductionOps[0 ].emplace_back (cast<SelectInst>(I)->getCondition ());
6720
- ReductionOps[1 ].emplace_back (I);
6705
+ void addReductionOps (Instruction *I) {
6706
+ if (auto *Sel = dyn_cast<SelectInst>(I)) {
6707
+ ReductionOps[0 ].emplace_back (Sel->getCondition ());
6708
+ ReductionOps[1 ].emplace_back (Sel);
6721
6709
} else {
6722
6710
ReductionOps[0 ].emplace_back (I);
6723
6711
}
@@ -6726,12 +6714,12 @@ class HorizontalReduction {
6726
6714
static Value *getLHS (RecurKind Kind, Instruction *I) {
6727
6715
if (Kind == RecurKind::None)
6728
6716
return nullptr ;
6729
- return I->getOperand (getFirstOperandIndex (Kind ));
6717
+ return I->getOperand (getFirstOperandIndex (I ));
6730
6718
}
6731
6719
static Value *getRHS (RecurKind Kind, Instruction *I) {
6732
6720
if (Kind == RecurKind::None)
6733
6721
return nullptr ;
6734
- return I->getOperand (getFirstOperandIndex (Kind ) + 1 );
6722
+ return I->getOperand (getFirstOperandIndex (I ) + 1 );
6735
6723
}
6736
6724
6737
6725
public:
@@ -6783,16 +6771,16 @@ class HorizontalReduction {
6783
6771
// Post order traverse the reduction tree starting at B. We only handle true
6784
6772
// trees containing only binary operators.
6785
6773
SmallVector<std::pair<Instruction *, unsigned >, 32 > Stack;
6786
- Stack.push_back (std::make_pair (B, getFirstOperandIndex (RdxKind )));
6787
- initReductionOps (RdxKind );
6774
+ Stack.push_back (std::make_pair (B, getFirstOperandIndex (B )));
6775
+ initReductionOps (B );
6788
6776
while (!Stack.empty ()) {
6789
6777
Instruction *TreeN = Stack.back ().first ;
6790
6778
unsigned EdgeToVisit = Stack.back ().second ++;
6791
6779
const RecurKind TreeRdxKind = getRdxKind (TreeN);
6792
6780
bool IsReducedValue = TreeRdxKind != RdxKind;
6793
6781
6794
6782
// Postorder visit.
6795
- if (IsReducedValue || EdgeToVisit == getNumberOfOperands (TreeRdxKind )) {
6783
+ if (IsReducedValue || EdgeToVisit == getNumberOfOperands (TreeN )) {
6796
6784
if (IsReducedValue)
6797
6785
ReducedVals.push_back (TreeN);
6798
6786
else {
@@ -6810,7 +6798,7 @@ class HorizontalReduction {
6810
6798
markExtraArg (Stack[Stack.size () - 2 ], TreeN);
6811
6799
ExtraArgs.erase (TreeN);
6812
6800
} else
6813
- addReductionOps (RdxKind, TreeN);
6801
+ addReductionOps (TreeN);
6814
6802
}
6815
6803
// Retract.
6816
6804
Stack.pop_back ();
@@ -6836,8 +6824,8 @@ class HorizontalReduction {
6836
6824
// ultimate reduction.
6837
6825
const bool IsRdxInst = EdgeRdxKind == RdxKind;
6838
6826
if (EdgeInst != Phi && EdgeInst != B &&
6839
- hasSameParent (RdxKind, EdgeInst, B->getParent (), IsRdxInst) &&
6840
- hasRequiredNumberOfUses (RdxKind , EdgeInst, IsRdxInst ) &&
6827
+ hasSameParent (EdgeInst, B->getParent (), IsRdxInst) &&
6828
+ hasRequiredNumberOfUses (isa<SelectInst>(B) , EdgeInst) &&
6841
6829
(!LeafOpcode || LeafOpcode == EdgeInst->getOpcode () || IsRdxInst)) {
6842
6830
if (IsRdxInst) {
6843
6831
// We need to be able to reassociate the reduction operations.
@@ -6850,7 +6838,7 @@ class HorizontalReduction {
6850
6838
LeafOpcode = EdgeInst->getOpcode ();
6851
6839
}
6852
6840
Stack.push_back (
6853
- std::make_pair (EdgeInst, getFirstOperandIndex (EdgeRdxKind )));
6841
+ std::make_pair (EdgeInst, getFirstOperandIndex (EdgeInst )));
6854
6842
continue ;
6855
6843
}
6856
6844
// I is an extra argument for TreeN (its parent operation).
@@ -6997,7 +6985,7 @@ class HorizontalReduction {
6997
6985
// Emit a reduction. If the root is a select (min/max idiom), the insert
6998
6986
// point is the compare condition of that select.
6999
6987
Instruction *RdxRootInst = cast<Instruction>(ReductionRoot);
7000
- if (isCmpSel (RdxKind ))
6988
+ if (isa<SelectInst>(RdxRootInst ))
7001
6989
Builder.SetInsertPoint (getCmpForMinMaxReduction (RdxRootInst));
7002
6990
else
7003
6991
Builder.SetInsertPoint (RdxRootInst);
@@ -7039,7 +7027,7 @@ class HorizontalReduction {
7039
7027
// select, we also have to RAUW for the compare instruction feeding the
7040
7028
// reduction root. That's because the original compare may have extra uses
7041
7029
// besides the final select of the reduction.
7042
- if (isCmpSel (RdxKind )) {
7030
+ if (isa<SelectInst>(ReductionRoot )) {
7043
7031
if (auto *VecSelect = dyn_cast<SelectInst>(VectorizedTree)) {
7044
7032
Instruction *ScalarCmp =
7045
7033
getCmpForMinMaxReduction (cast<Instruction>(ReductionRoot));
0 commit comments