@@ -1527,6 +1527,10 @@ class LoopVectorizationCostModel {
15271527 getReductionPatternCost (Instruction *I, ElementCount VF, Type *VectorTy,
15281528 TTI::TargetCostKind CostKind) const ;
15291529
1530+ // / Returns true if \p Op should be considered invariant and if it is
1531+ // / trivially hoistable.
1532+ bool shouldConsiderInvariant (Value *Op);
1533+
15301534private:
15311535 unsigned NumPredStores = 0 ;
15321536
@@ -6382,6 +6386,17 @@ void LoopVectorizationCostModel::setVectorizedCallDecision(ElementCount VF) {
63826386 }
63836387}
63846388
6389+ bool LoopVectorizationCostModel::shouldConsiderInvariant (Value *Op) {
6390+ if (!Legal->isInvariant (Op))
6391+ return false ;
6392+ // Consider Op invariant, if it or its operands aren't predicated
6393+ // instruction in the loop. In that case, it is not trivially hoistable.
6394+ return !isa<Instruction>(Op) || !TheLoop->contains (cast<Instruction>(Op)) ||
6395+ (!isPredicatedInst (cast<Instruction>(Op)) &&
6396+ all_of (cast<Instruction>(Op)->operands (),
6397+ [this ](Value *Op) { return shouldConsiderInvariant (Op); }));
6398+ }
6399+
63856400InstructionCost
63866401LoopVectorizationCostModel::getInstructionCost (Instruction *I,
63876402 ElementCount VF) {
@@ -6621,19 +6636,8 @@ LoopVectorizationCostModel::getInstructionCost(Instruction *I,
66216636 Op2 = cast<SCEVConstant>(PSE.getSCEV (Op2))->getValue ();
66226637 }
66236638 auto Op2Info = TTI.getOperandInfo (Op2);
6624- std::function<bool (Value *)> IsInvariant =
6625- [this , &IsInvariant](Value *Op) -> bool {
6626- if (!Legal->isInvariant (Op))
6627- return false ;
6628- // Consider Op2invariant, if it or its operands aren't predicated
6629- // instruction in the loop. In that case, it is not trivially hoistable.
6630- return !isa<Instruction>(Op) ||
6631- !TheLoop->contains (cast<Instruction>(Op)) ||
6632- (!isPredicatedInst (cast<Instruction>(Op)) &&
6633- all_of (cast<Instruction>(Op)->operands (),
6634- [&IsInvariant](Value *Op) { return IsInvariant (Op); }));
6635- };
6636- if (Op2Info.Kind == TargetTransformInfo::OK_AnyValue && IsInvariant (Op2))
6639+ if (Op2Info.Kind == TargetTransformInfo::OK_AnyValue &&
6640+ shouldConsiderInvariant (Op2))
66376641 Op2Info.Kind = TargetTransformInfo::OK_UniformValue;
66386642
66396643 SmallVector<const Value *, 4 > Operands (I->operand_values ());
0 commit comments