@@ -26,6 +26,7 @@ struct RISCVMoveMerge : public MachineFunctionPass {
2626
2727 RISCVMoveMerge () : MachineFunctionPass(ID) {}
2828
29+ const RISCVSubtarget *ST;
2930 const RISCVInstrInfo *TII;
3031 const TargetRegisterInfo *TRI;
3132
@@ -37,15 +38,15 @@ struct RISCVMoveMerge : public MachineFunctionPass {
3738 // Merge the two instructions indicated into a single pair instruction.
3839 MachineBasicBlock::iterator
3940 mergePairedInsns (MachineBasicBlock::iterator I,
40- MachineBasicBlock::iterator Paired, unsigned Opcode );
41+ MachineBasicBlock::iterator Paired, bool MoveFromSToA );
4142
4243 // Look for C.MV instruction that can be combined with
4344 // the given instruction into CM.MVA01S or CM.MVSA01. Return the matching
4445 // instruction if one exists.
4546 MachineBasicBlock::iterator
46- findMatchingInst (MachineBasicBlock::iterator &MBBI, unsigned InstOpcode ,
47+ findMatchingInst (MachineBasicBlock::iterator &MBBI, bool MoveFromSToA ,
4748 const DestSourcePair &RegPair);
48- bool mergeMoveSARegPair (const RISCVSubtarget &STI, MachineBasicBlock &MBB);
49+ bool mergeMoveSARegPair (MachineBasicBlock &MBB);
4950 bool runOnMachineFunction (MachineFunction &Fn) override ;
5051
5152 StringRef getPassName () const override { return RISCV_MOVE_MERGE_NAME; }
@@ -58,41 +59,21 @@ char RISCVMoveMerge::ID = 0;
5859INITIALIZE_PASS (RISCVMoveMerge, " riscv-move-merge" , RISCV_MOVE_MERGE_NAME,
5960 false , false )
6061
61- static bool isMoveFromAToS(unsigned Opcode) {
62- switch (Opcode) {
63- case RISCV::CM_MVA01S:
64- case RISCV::QC_CM_MVA01S:
65- return true ;
66- default :
67- return false ;
68- }
69- }
70-
71- static unsigned getMoveFromAToSOpcode (const RISCVSubtarget &STI) {
72- if (STI.hasStdExtZcmp ())
62+ static unsigned getMoveFromSToAOpcode(const RISCVSubtarget &ST) {
63+ if (ST.hasStdExtZcmp ())
7364 return RISCV::CM_MVA01S;
7465
75- if (STI .hasVendorXqccmp ())
66+ if (ST .hasVendorXqccmp ())
7667 return RISCV::QC_CM_MVA01S;
7768
7869 llvm_unreachable (" Unhandled subtarget with paired A to S move." );
7970}
8071
81- static bool isMoveFromSToA (unsigned Opcode) {
82- switch (Opcode) {
83- case RISCV::CM_MVSA01:
84- case RISCV::QC_CM_MVSA01:
85- return true ;
86- default :
87- return false ;
88- }
89- }
90-
91- static unsigned getMoveFromSToAOpcode (const RISCVSubtarget &STI) {
92- if (STI.hasStdExtZcmp ())
72+ static unsigned getMoveFromAToSOpcode (const RISCVSubtarget &ST) {
73+ if (ST.hasStdExtZcmp ())
9374 return RISCV::CM_MVSA01;
9475
95- if (STI .hasVendorXqccmp ())
76+ if (ST .hasVendorXqccmp ())
9677 return RISCV::QC_CM_MVSA01;
9778
9879 llvm_unreachable (" Unhandled subtarget with paired S to A move" );
@@ -123,21 +104,20 @@ bool RISCVMoveMerge::isCandidateToMergeMVSA01(const DestSourcePair &RegPair) {
123104MachineBasicBlock::iterator
124105RISCVMoveMerge::mergePairedInsns (MachineBasicBlock::iterator I,
125106 MachineBasicBlock::iterator Paired,
126- unsigned Opcode ) {
107+ bool MoveFromSToA ) {
127108 const MachineOperand *Sreg1, *Sreg2;
128109 MachineBasicBlock::iterator E = I->getParent ()->end ();
129110 MachineBasicBlock::iterator NextI = next_nodbg (I, E);
130111 DestSourcePair FirstPair = TII->isCopyInstrImpl (*I).value ();
131112 DestSourcePair PairedRegs = TII->isCopyInstrImpl (*Paired).value ();
132- Register ARegInFirstPair = isMoveFromAToS (Opcode)
133- ? FirstPair.Destination ->getReg ()
134- : FirstPair.Source ->getReg ();
113+ Register ARegInFirstPair = MoveFromSToA ? FirstPair.Destination ->getReg ()
114+ : FirstPair.Source ->getReg ();
135115
136116 if (NextI == Paired)
137117 NextI = next_nodbg (NextI, E);
138118 DebugLoc DL = I->getDebugLoc ();
139119
140- // Make a copy so we can update the kill flag in the MoveFromAToS case. The
120+ // Make a copy so we can update the kill flag in the MoveFromSToA case. The
141121 // copied operand needs to be scoped outside the if since we make a pointer
142122 // to it.
143123 MachineOperand PairedSource = *PairedRegs.Source ;
@@ -151,17 +131,20 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
151131 // mv a0, s2
152132 // mv a1, s1 => cm.mva01s s2,s1
153133 bool StartWithX10 = ARegInFirstPair == RISCV::X10;
154- if (isMoveFromAToS (Opcode)) {
134+ unsigned Opcode;
135+ if (MoveFromSToA) {
155136 // We are moving one of the copies earlier so its kill flag may become
156137 // invalid. Clear the copied kill flag if there are any reads of the
157138 // register between the new location and the old location.
158139 for (auto It = std::next (I); It != Paired && PairedSource.isKill (); ++It)
159140 if (It->readsRegister (PairedSource.getReg (), TRI))
160141 PairedSource.setIsKill (false );
161142
143+ Opcode = getMoveFromSToAOpcode (*ST);
162144 Sreg1 = StartWithX10 ? FirstPair.Source : &PairedSource;
163145 Sreg2 = StartWithX10 ? &PairedSource : FirstPair.Source ;
164146 } else {
147+ Opcode = getMoveFromAToSOpcode (*ST);
165148 Sreg1 = StartWithX10 ? FirstPair.Destination : PairedRegs.Destination ;
166149 Sreg2 = StartWithX10 ? PairedRegs.Destination : FirstPair.Destination ;
167150 }
@@ -175,7 +158,7 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
175158
176159MachineBasicBlock::iterator
177160RISCVMoveMerge::findMatchingInst (MachineBasicBlock::iterator &MBBI,
178- unsigned InstOpcode ,
161+ bool MoveFromSToA ,
179162 const DestSourcePair &RegPair) {
180163 MachineBasicBlock::iterator E = MBBI->getParent ()->end ();
181164
@@ -193,7 +176,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
193176 Register SourceReg = SecondPair->Source ->getReg ();
194177 Register DestReg = SecondPair->Destination ->getReg ();
195178
196- if (isMoveFromAToS (InstOpcode) && isCandidateToMergeMVA01S (*SecondPair)) {
179+ if (MoveFromSToA && isCandidateToMergeMVA01S (*SecondPair)) {
197180 // If register pair is valid and destination registers are different.
198181 if ((RegPair.Destination ->getReg () == DestReg))
199182 return E;
@@ -207,8 +190,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
207190 return E;
208191
209192 return I;
210- } else if (isMoveFromSToA (InstOpcode) &&
211- isCandidateToMergeMVSA01 (*SecondPair)) {
193+ } else if (!MoveFromSToA && isCandidateToMergeMVSA01 (*SecondPair)) {
212194 if ((RegPair.Source ->getReg () == SourceReg) ||
213195 (RegPair.Destination ->getReg () == DestReg))
214196 return E;
@@ -229,8 +211,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
229211
230212// Finds instructions, which could be represented as C.MV instructions and
231213// merged into CM.MVA01S or CM.MVSA01.
232- bool RISCVMoveMerge::mergeMoveSARegPair (const RISCVSubtarget &STI,
233- MachineBasicBlock &MBB) {
214+ bool RISCVMoveMerge::mergeMoveSARegPair (MachineBasicBlock &MBB) {
234215 bool Modified = false ;
235216
236217 for (MachineBasicBlock::iterator MBBI = MBB.begin (), E = MBB.end ();
@@ -239,22 +220,17 @@ bool RISCVMoveMerge::mergeMoveSARegPair(const RISCVSubtarget &STI,
239220 // can, return Dest/Src register pair.
240221 auto RegPair = TII->isCopyInstrImpl (*MBBI);
241222 if (RegPair.has_value ()) {
242- unsigned Opcode = 0 ;
243-
244- if (isCandidateToMergeMVA01S (*RegPair))
245- Opcode = getMoveFromAToSOpcode (STI);
246- else if (isCandidateToMergeMVSA01 (*RegPair))
247- Opcode = getMoveFromSToAOpcode (STI);
248- else {
223+ bool MoveFromSToA = isCandidateToMergeMVA01S (*RegPair);
224+ if (!MoveFromSToA && !isCandidateToMergeMVSA01 (*RegPair)) {
249225 ++MBBI;
250226 continue ;
251227 }
252228
253229 MachineBasicBlock::iterator Paired =
254- findMatchingInst (MBBI, Opcode , RegPair.value ());
230+ findMatchingInst (MBBI, MoveFromSToA , RegPair.value ());
255231 // If matching instruction can be found merge them.
256232 if (Paired != E) {
257- MBBI = mergePairedInsns (MBBI, Paired, Opcode );
233+ MBBI = mergePairedInsns (MBBI, Paired, MoveFromSToA );
258234 Modified = true ;
259235 continue ;
260236 }
@@ -268,20 +244,20 @@ bool RISCVMoveMerge::runOnMachineFunction(MachineFunction &Fn) {
268244 if (skipFunction (Fn.getFunction ()))
269245 return false ;
270246
271- const RISCVSubtarget *Subtarget = &Fn.getSubtarget <RISCVSubtarget>();
272- if (!(Subtarget ->hasStdExtZcmp () || Subtarget ->hasVendorXqccmp () ))
247+ ST = &Fn.getSubtarget <RISCVSubtarget>();
248+ if (!ST ->hasStdExtZcmp () && !ST ->hasVendorXqccmp ())
273249 return false ;
274250
275- TII = Subtarget ->getInstrInfo ();
276- TRI = Subtarget ->getRegisterInfo ();
251+ TII = ST ->getInstrInfo ();
252+ TRI = ST ->getRegisterInfo ();
277253 // Resize the modified and used register unit trackers. We do this once
278254 // per function and then clear the register units each time we optimize a
279255 // move.
280256 ModifiedRegUnits.init (*TRI);
281257 UsedRegUnits.init (*TRI);
282258 bool Modified = false ;
283259 for (auto &MBB : Fn)
284- Modified |= mergeMoveSARegPair (*Subtarget, MBB);
260+ Modified |= mergeMoveSARegPair (MBB);
285261 return Modified;
286262}
287263
0 commit comments