Skip to content

Commit aa4c5db

Browse files
committed
Simplify sinking after #72540
1 parent 1212730 commit aa4c5db

File tree

1 file changed

+13
-60
lines changed

1 file changed

+13
-60
lines changed

llvm/lib/Target/RISCV/RISCVFoldMasks.cpp

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "RISCV.h"
3030
#include "RISCVISelDAGToDAG.h"
3131
#include "RISCVSubtarget.h"
32-
#include "llvm/ADT/SmallSet.h"
3332

3433
using namespace llvm;
3534

@@ -120,64 +119,6 @@ static unsigned getVMSetForLMul(RISCVII::VLMUL LMUL) {
120119
llvm_unreachable("Unknown VLMUL enum");
121120
}
122121

123-
// Try to sink From to before To, also sinking any instructions between From and
124-
// To where there is a write-after-read dependency on a physical register.
125-
static bool sinkInstructionAndDeps(MachineInstr &From, MachineInstr &To) {
126-
assert(From.getParent() == To.getParent());
127-
SmallVector<MachineInstr *> Worklist, ToSink;
128-
Worklist.push_back(&From);
129-
130-
// Rather than compute whether or not we saw a store for every instruction,
131-
// just compute it once even if it's more conservative.
132-
bool SawStore = false;
133-
for (MachineBasicBlock::instr_iterator II = From.getIterator();
134-
II != To.getIterator(); II++) {
135-
if (II->mayStore()) {
136-
SawStore = true;
137-
break;
138-
}
139-
}
140-
141-
while (!Worklist.empty()) {
142-
MachineInstr *MI = Worklist.pop_back_val();
143-
144-
if (!MI->isSafeToMove(nullptr, SawStore))
145-
return false;
146-
147-
SmallSet<Register, 8> Defs, Uses;
148-
for (MachineOperand &Def : MI->all_defs())
149-
Defs.insert(Def.getReg());
150-
for (MachineOperand &Use : MI->all_uses())
151-
Uses.insert(Use.getReg());
152-
153-
// If anything from [MI, To] uses a definition of MI, we can't sink it.
154-
for (MachineBasicBlock::instr_iterator II = MI->getIterator();
155-
II != To.getIterator(); II++) {
156-
for (MachineOperand &Use : II->all_uses()) {
157-
if (Defs.contains(Use.getReg()))
158-
return false;
159-
}
160-
}
161-
162-
// If MI uses any physical registers, we need to sink any instructions after
163-
// it where there might be a write-after-read dependency.
164-
for (MachineBasicBlock::instr_iterator II = MI->getIterator();
165-
II != To.getIterator(); II++) {
166-
bool NeedsSink = any_of(II->all_defs(), [&Uses](MachineOperand &Def) {
167-
return Def.getReg().isPhysical() && Uses.contains(Def.getReg());
168-
});
169-
if (NeedsSink)
170-
Worklist.push_back(&*II);
171-
}
172-
173-
ToSink.push_back(MI);
174-
}
175-
176-
for (MachineInstr *MI : ToSink)
177-
MI->moveBefore(&To);
178-
return true;
179-
}
180-
181122
// Returns true if LHS is the same register as RHS, or if LHS is undefined.
182123
bool RISCVFoldMasks::isOpSameAs(const MachineOperand &LHS,
183124
const MachineOperand &RHS) {
@@ -330,8 +271,18 @@ bool RISCVFoldMasks::foldVMergeIntoOps(MachineInstr &MI,
330271
#endif
331272

332273
// Sink True down to MI so that it can access MI's operands.
333-
if (!sinkInstructionAndDeps(TrueMI, MI))
274+
assert(!TrueMI.hasImplicitDef());
275+
bool SawStore = false;
276+
for (MachineBasicBlock::instr_iterator II = TrueMI.getIterator();
277+
II != MI.getIterator(); II++) {
278+
if (II->mayStore()) {
279+
SawStore = true;
280+
break;
281+
}
282+
}
283+
if (!TrueMI.isSafeToMove(nullptr, SawStore))
334284
return false;
285+
TrueMI.moveBefore(&MI);
335286

336287
// Set the merge to the false operand of the merge.
337288
TrueMI.getOperand(1).setReg(False->getReg());
@@ -392,6 +343,8 @@ bool RISCVFoldMasks::foldVMergeIntoOps(MachineInstr &MI,
392343

393344
MRI->replaceRegWith(MI.getOperand(0).getReg(), TrueMI.getOperand(0).getReg());
394345
MI.eraseFromParent();
346+
if (IsMasked)
347+
MaskDef->eraseFromParent();
395348

396349
return true;
397350
}

0 commit comments

Comments
 (0)