|
29 | 29 | #include "RISCV.h"
|
30 | 30 | #include "RISCVISelDAGToDAG.h"
|
31 | 31 | #include "RISCVSubtarget.h"
|
32 |
| -#include "llvm/ADT/SmallSet.h" |
33 | 32 |
|
34 | 33 | using namespace llvm;
|
35 | 34 |
|
@@ -120,64 +119,6 @@ static unsigned getVMSetForLMul(RISCVII::VLMUL LMUL) {
|
120 | 119 | llvm_unreachable("Unknown VLMUL enum");
|
121 | 120 | }
|
122 | 121 |
|
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 |
| - |
181 | 122 | // Returns true if LHS is the same register as RHS, or if LHS is undefined.
|
182 | 123 | bool RISCVFoldMasks::isOpSameAs(const MachineOperand &LHS,
|
183 | 124 | const MachineOperand &RHS) {
|
@@ -330,8 +271,18 @@ bool RISCVFoldMasks::foldVMergeIntoOps(MachineInstr &MI,
|
330 | 271 | #endif
|
331 | 272 |
|
332 | 273 | // 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)) |
334 | 284 | return false;
|
| 285 | + TrueMI.moveBefore(&MI); |
335 | 286 |
|
336 | 287 | // Set the merge to the false operand of the merge.
|
337 | 288 | TrueMI.getOperand(1).setReg(False->getReg());
|
@@ -392,6 +343,8 @@ bool RISCVFoldMasks::foldVMergeIntoOps(MachineInstr &MI,
|
392 | 343 |
|
393 | 344 | MRI->replaceRegWith(MI.getOperand(0).getReg(), TrueMI.getOperand(0).getReg());
|
394 | 345 | MI.eraseFromParent();
|
| 346 | + if (IsMasked) |
| 347 | + MaskDef->eraseFromParent(); |
395 | 348 |
|
396 | 349 | return true;
|
397 | 350 | }
|
|
0 commit comments