@@ -26,6 +26,7 @@ struct RISCVMoveMerge : public MachineFunctionPass {
26
26
27
27
RISCVMoveMerge () : MachineFunctionPass(ID) {}
28
28
29
+ const RISCVSubtarget *ST;
29
30
const RISCVInstrInfo *TII;
30
31
const TargetRegisterInfo *TRI;
31
32
@@ -37,15 +38,15 @@ struct RISCVMoveMerge : public MachineFunctionPass {
37
38
// Merge the two instructions indicated into a single pair instruction.
38
39
MachineBasicBlock::iterator
39
40
mergePairedInsns (MachineBasicBlock::iterator I,
40
- MachineBasicBlock::iterator Paired, unsigned Opcode );
41
+ MachineBasicBlock::iterator Paired, bool MoveFromSToA );
41
42
42
43
// Look for C.MV instruction that can be combined with
43
44
// the given instruction into CM.MVA01S or CM.MVSA01. Return the matching
44
45
// instruction if one exists.
45
46
MachineBasicBlock::iterator
46
- findMatchingInst (MachineBasicBlock::iterator &MBBI, unsigned InstOpcode ,
47
+ findMatchingInst (MachineBasicBlock::iterator &MBBI, bool MoveFromSToA ,
47
48
const DestSourcePair &RegPair);
48
- bool mergeMoveSARegPair (const RISCVSubtarget &STI, MachineBasicBlock &MBB);
49
+ bool mergeMoveSARegPair (MachineBasicBlock &MBB);
49
50
bool runOnMachineFunction (MachineFunction &Fn) override ;
50
51
51
52
StringRef getPassName () const override { return RISCV_MOVE_MERGE_NAME; }
@@ -58,41 +59,21 @@ char RISCVMoveMerge::ID = 0;
58
59
INITIALIZE_PASS (RISCVMoveMerge, " riscv-move-merge" , RISCV_MOVE_MERGE_NAME,
59
60
false , false )
60
61
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 ())
73
64
return RISCV::CM_MVA01S;
74
65
75
- if (STI .hasVendorXqccmp ())
66
+ if (ST .hasVendorXqccmp ())
76
67
return RISCV::QC_CM_MVA01S;
77
68
78
69
llvm_unreachable (" Unhandled subtarget with paired A to S move." );
79
70
}
80
71
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 ())
93
74
return RISCV::CM_MVSA01;
94
75
95
- if (STI .hasVendorXqccmp ())
76
+ if (ST .hasVendorXqccmp ())
96
77
return RISCV::QC_CM_MVSA01;
97
78
98
79
llvm_unreachable (" Unhandled subtarget with paired S to A move" );
@@ -123,21 +104,20 @@ bool RISCVMoveMerge::isCandidateToMergeMVSA01(const DestSourcePair &RegPair) {
123
104
MachineBasicBlock::iterator
124
105
RISCVMoveMerge::mergePairedInsns (MachineBasicBlock::iterator I,
125
106
MachineBasicBlock::iterator Paired,
126
- unsigned Opcode ) {
107
+ bool MoveFromSToA ) {
127
108
const MachineOperand *Sreg1, *Sreg2;
128
109
MachineBasicBlock::iterator E = I->getParent ()->end ();
129
110
MachineBasicBlock::iterator NextI = next_nodbg (I, E);
130
111
DestSourcePair FirstPair = TII->isCopyInstrImpl (*I).value ();
131
112
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 ();
135
115
136
116
if (NextI == Paired)
137
117
NextI = next_nodbg (NextI, E);
138
118
DebugLoc DL = I->getDebugLoc ();
139
119
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
141
121
// copied operand needs to be scoped outside the if since we make a pointer
142
122
// to it.
143
123
MachineOperand PairedSource = *PairedRegs.Source ;
@@ -151,17 +131,20 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
151
131
// mv a0, s2
152
132
// mv a1, s1 => cm.mva01s s2,s1
153
133
bool StartWithX10 = ARegInFirstPair == RISCV::X10;
154
- if (isMoveFromAToS (Opcode)) {
134
+ unsigned Opcode;
135
+ if (MoveFromSToA) {
155
136
// We are moving one of the copies earlier so its kill flag may become
156
137
// invalid. Clear the copied kill flag if there are any reads of the
157
138
// register between the new location and the old location.
158
139
for (auto It = std::next (I); It != Paired && PairedSource.isKill (); ++It)
159
140
if (It->readsRegister (PairedSource.getReg (), TRI))
160
141
PairedSource.setIsKill (false );
161
142
143
+ Opcode = getMoveFromSToAOpcode (*ST);
162
144
Sreg1 = StartWithX10 ? FirstPair.Source : &PairedSource;
163
145
Sreg2 = StartWithX10 ? &PairedSource : FirstPair.Source ;
164
146
} else {
147
+ Opcode = getMoveFromAToSOpcode (*ST);
165
148
Sreg1 = StartWithX10 ? FirstPair.Destination : PairedRegs.Destination ;
166
149
Sreg2 = StartWithX10 ? PairedRegs.Destination : FirstPair.Destination ;
167
150
}
@@ -175,7 +158,7 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
175
158
176
159
MachineBasicBlock::iterator
177
160
RISCVMoveMerge::findMatchingInst (MachineBasicBlock::iterator &MBBI,
178
- unsigned InstOpcode ,
161
+ bool MoveFromSToA ,
179
162
const DestSourcePair &RegPair) {
180
163
MachineBasicBlock::iterator E = MBBI->getParent ()->end ();
181
164
@@ -193,7 +176,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
193
176
Register SourceReg = SecondPair->Source ->getReg ();
194
177
Register DestReg = SecondPair->Destination ->getReg ();
195
178
196
- if (isMoveFromAToS (InstOpcode) && isCandidateToMergeMVA01S (*SecondPair)) {
179
+ if (MoveFromSToA && isCandidateToMergeMVA01S (*SecondPair)) {
197
180
// If register pair is valid and destination registers are different.
198
181
if ((RegPair.Destination ->getReg () == DestReg))
199
182
return E;
@@ -207,8 +190,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
207
190
return E;
208
191
209
192
return I;
210
- } else if (isMoveFromSToA (InstOpcode) &&
211
- isCandidateToMergeMVSA01 (*SecondPair)) {
193
+ } else if (!MoveFromSToA && isCandidateToMergeMVSA01 (*SecondPair)) {
212
194
if ((RegPair.Source ->getReg () == SourceReg) ||
213
195
(RegPair.Destination ->getReg () == DestReg))
214
196
return E;
@@ -229,8 +211,7 @@ RISCVMoveMerge::findMatchingInst(MachineBasicBlock::iterator &MBBI,
229
211
230
212
// Finds instructions, which could be represented as C.MV instructions and
231
213
// merged into CM.MVA01S or CM.MVSA01.
232
- bool RISCVMoveMerge::mergeMoveSARegPair (const RISCVSubtarget &STI,
233
- MachineBasicBlock &MBB) {
214
+ bool RISCVMoveMerge::mergeMoveSARegPair (MachineBasicBlock &MBB) {
234
215
bool Modified = false ;
235
216
236
217
for (MachineBasicBlock::iterator MBBI = MBB.begin (), E = MBB.end ();
@@ -239,22 +220,17 @@ bool RISCVMoveMerge::mergeMoveSARegPair(const RISCVSubtarget &STI,
239
220
// can, return Dest/Src register pair.
240
221
auto RegPair = TII->isCopyInstrImpl (*MBBI);
241
222
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)) {
249
225
++MBBI;
250
226
continue ;
251
227
}
252
228
253
229
MachineBasicBlock::iterator Paired =
254
- findMatchingInst (MBBI, Opcode , RegPair.value ());
230
+ findMatchingInst (MBBI, MoveFromSToA , RegPair.value ());
255
231
// If matching instruction can be found merge them.
256
232
if (Paired != E) {
257
- MBBI = mergePairedInsns (MBBI, Paired, Opcode );
233
+ MBBI = mergePairedInsns (MBBI, Paired, MoveFromSToA );
258
234
Modified = true ;
259
235
continue ;
260
236
}
@@ -268,20 +244,20 @@ bool RISCVMoveMerge::runOnMachineFunction(MachineFunction &Fn) {
268
244
if (skipFunction (Fn.getFunction ()))
269
245
return false ;
270
246
271
- const RISCVSubtarget *Subtarget = &Fn.getSubtarget <RISCVSubtarget>();
272
- if (!(Subtarget ->hasStdExtZcmp () || Subtarget ->hasVendorXqccmp () ))
247
+ ST = &Fn.getSubtarget <RISCVSubtarget>();
248
+ if (!ST ->hasStdExtZcmp () && !ST ->hasVendorXqccmp ())
273
249
return false ;
274
250
275
- TII = Subtarget ->getInstrInfo ();
276
- TRI = Subtarget ->getRegisterInfo ();
251
+ TII = ST ->getInstrInfo ();
252
+ TRI = ST ->getRegisterInfo ();
277
253
// Resize the modified and used register unit trackers. We do this once
278
254
// per function and then clear the register units each time we optimize a
279
255
// move.
280
256
ModifiedRegUnits.init (*TRI);
281
257
UsedRegUnits.init (*TRI);
282
258
bool Modified = false ;
283
259
for (auto &MBB : Fn)
284
- Modified |= mergeMoveSARegPair (*Subtarget, MBB);
260
+ Modified |= mergeMoveSARegPair (MBB);
285
261
return Modified;
286
262
}
287
263
0 commit comments