@@ -79,6 +79,10 @@ static cl::opt<LoopVectorizeHints::ScalableForceKind>
79
79
" Scalable vectorization is available and favored when the "
80
80
" cost is inconclusive." )));
81
81
82
+ static cl::opt<bool >
83
+ EnableCSA (" enable-csa-vectorization" , cl::init(false ), cl::Hidden,
84
+ cl::desc(" Control whether CSA loop vectorization is enabled" ));
85
+
82
86
// / Maximum vectorization interleave count.
83
87
static const unsigned MaxInterleaveFactor = 16 ;
84
88
@@ -749,6 +753,15 @@ bool LoopVectorizationLegality::setupOuterLoopInductions() {
749
753
return llvm::all_of (Header->phis (), IsSupportedPhi);
750
754
}
751
755
756
+ void LoopVectorizationLegality::addCSAPhi (
757
+ PHINode *Phi, const CSADescriptor &CSADesc,
758
+ SmallPtrSetImpl<Value *> &AllowedExit) {
759
+ assert (CSADesc.isValid () && " Expected Valid CSADescriptor" );
760
+ LLVM_DEBUG (dbgs () << " LV: found legal CSA opportunity" << *Phi << " \n " );
761
+ AllowedExit.insert (Phi);
762
+ CSAs.insert ({Phi, CSADesc});
763
+ }
764
+
752
765
// / Checks if a function is scalarizable according to the TLI, in
753
766
// / the sense that it should be vectorized and then expanded in
754
767
// / multiple scalar calls. This is represented in the
@@ -866,14 +879,23 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
866
879
continue ;
867
880
}
868
881
869
- // As a last resort, coerce the PHI to a AddRec expression
870
- // and re-try classifying it a an induction PHI.
882
+ // Try to coerce the PHI to a AddRec expression and re-try classifying
883
+ // it a an induction PHI.
871
884
if (InductionDescriptor::isInductionPHI (Phi, TheLoop, PSE, ID, true ) &&
872
885
!IsDisallowedStridedPointerInduction (ID)) {
873
886
addInductionPhi (Phi, ID, AllowedExit);
874
887
continue ;
875
888
}
876
889
890
+ // Check if the PHI can be classified as a CSA PHI.
891
+ if (EnableCSA || (TTI->enableCSAVectorization () &&
892
+ EnableCSA.getNumOccurrences () == 0 )) {
893
+ if (auto CSADesc = CSADescriptor::isCSAPhi (Phi, TheLoop)) {
894
+ addCSAPhi (Phi, CSADesc, AllowedExit);
895
+ continue ;
896
+ }
897
+ }
898
+
877
899
reportVectorizationFailure (" Found an unidentified PHI" ,
878
900
" value that could not be identified as "
879
901
" reduction is used outside the loop" ,
@@ -1555,11 +1577,15 @@ bool LoopVectorizationLegality::canFoldTailByMasking() const {
1555
1577
for (const auto &Reduction : getReductionVars ())
1556
1578
ReductionLiveOuts.insert (Reduction.second .getLoopExitInstr ());
1557
1579
1580
+ SmallPtrSet<const Value *, 8 > CSALiveOuts;
1581
+ for (const auto &CSA: getCSAs ())
1582
+ CSALiveOuts.insert (CSA.second .getAssignment ());
1583
+
1558
1584
// TODO: handle non-reduction outside users when tail is folded by masking.
1559
1585
for (auto *AE : AllowedExit) {
1560
1586
// Check that all users of allowed exit values are inside the loop or
1561
- // are the live-out of a reduction.
1562
- if (ReductionLiveOuts.count (AE))
1587
+ // are the live-out of a reduction or a CSA
1588
+ if (ReductionLiveOuts.count (AE) || CSALiveOuts. count (AE) )
1563
1589
continue ;
1564
1590
for (User *U : AE->users ()) {
1565
1591
Instruction *UI = cast<Instruction>(U);
0 commit comments