1212// ===----------------------------------------------------------------------===//
1313
1414#include " RISCVVSETVLCluster.h"
15+ #include " RISCV.h"
16+ #include " RISCVSubtarget.h"
17+ #include " llvm/ADT/DenseMap.h"
18+ #include " llvm/CodeGen/MachineInstr.h"
19+ #include " llvm/CodeGen/MachineRegisterInfo.h"
1520#include " llvm/CodeGen/ScheduleDAGInstrs.h"
21+ #include < utility>
1622
1723using namespace llvm ;
1824
@@ -24,7 +30,62 @@ class VSETVLClustering : public ScheduleDAGMutation {
2430 void apply (ScheduleDAGInstrs *DAG) override ;
2531};
2632
27- void VSETVLClustering::apply (ScheduleDAGInstrs *DAG) {}
33+ static void
34+ collectCandidate (ScheduleDAGInstrs *DAG,
35+ SmallVector<std::pair<SUnit *, VSETVLIInfo>> &Candidate) {
36+ for (SUnit &SU : DAG->SUnits ) {
37+ if (SU.isInstr ()) {
38+ MachineInstr *MI = SU.getInstr ();
39+ const RISCVSubtarget &STI = MI->getMF ()->getSubtarget <RISCVSubtarget>();
40+ uint64_t TSFlags = MI->getDesc ().TSFlags ;
41+ if (!RISCVII::hasSEWOp (TSFlags))
42+ continue ;
43+
44+ VSETVLIInfo Info = computeInfoForInstr (*MI, &STI, nullptr );
45+ Candidate.push_back (std::make_pair (&SU, Info));
46+ }
47+ }
48+ }
49+
50+ void VSETVLClustering::apply (ScheduleDAGInstrs *DAG) {
51+ SmallVector<std::pair<SUnit *, VSETVLIInfo>> Candidate;
52+ collectCandidate (DAG, Candidate);
53+
54+ SmallVector<SmallVector<std::pair<SUnit *, VSETVLIInfo>>> VSETVLGroups;
55+
56+ for (auto &Tmp : Candidate) {
57+ SUnit *SU = Tmp.first ;
58+ VSETVLIInfo Info = Tmp.second ;
59+ bool Found = false ;
60+ for (auto &Group : VSETVLGroups) {
61+ if (Group[0 ].second == Info &&
62+ all_of (Group, [SU, DAG](std::pair<SUnit *, VSETVLIInfo> Member) {
63+ return !DAG->IsReachable (SU, Member.first ) &&
64+ !DAG->IsReachable (Member.first , SU);
65+ })) {
66+ Found = true ;
67+ Group.push_back (Tmp);
68+ break ;
69+ }
70+ }
71+
72+ if (!Found) {
73+ SmallVector<std::pair<SUnit *, VSETVLIInfo>> NewGroup;
74+ NewGroup.push_back (Tmp);
75+ VSETVLGroups.push_back (NewGroup);
76+ }
77+ }
78+
79+ for (auto &Group : VSETVLGroups) {
80+ if (Group.size () < 2 )
81+ continue ;
82+
83+ SUnit *CurrSU = Group[0 ].first ;
84+ for (auto *I = Group.begin () + 1 ; I != Group.end (); I++) {
85+ DAG->addEdge (CurrSU, SDep (I->first , SDep::Cluster));
86+ }
87+ }
88+ }
2889
2990} // end namespace
3091
0 commit comments