@@ -101,29 +101,6 @@ void SILCombiner::addReachableCodeToWorklist(SILBasicBlock *BB) {
101
101
addInitialGroup (InstrsForSILCombineWorklist);
102
102
}
103
103
104
- static void eraseSingleInstFromFunction (
105
- SILInstruction &I,
106
- SmallSILInstructionWorklist<256 > &Worklist,
107
- bool AddOperandsToWorklist) {
108
- LLVM_DEBUG (llvm::dbgs () << " SC: ERASE " << I << ' \n ' );
109
-
110
- assert (!I.hasUsesOfAnyResult () && " Cannot erase instruction that is used!" );
111
-
112
- // Make sure that we reprocess all operands now that we reduced their
113
- // use counts.
114
- if (I.getNumOperands () < 8 && AddOperandsToWorklist) {
115
- for (auto &OpI : I.getAllOperands ()) {
116
- if (auto *Op = OpI.get ()->getDefiningInstruction ()) {
117
- LLVM_DEBUG (llvm::dbgs () << " SC: add op " << *Op
118
- << " from erased inst to worklist\n " );
119
- Worklist.add (Op);
120
- }
121
- }
122
- }
123
- Worklist.erase (&I);
124
- I.eraseFromParent ();
125
- }
126
-
127
104
// ===----------------------------------------------------------------------===//
128
105
// Implementation
129
106
// ===----------------------------------------------------------------------===//
@@ -147,8 +124,8 @@ class SILCombineCanonicalize final : CanonicalizeInstruction {
147
124
// Just delete the given 'inst' and record its operands. The callback isn't
148
125
// allowed to mutate any other instructions.
149
126
void killInstruction (SILInstruction *inst) override {
150
- eraseSingleInstFromFunction (*inst, Worklist ,
151
- /* AddOperandsToWorklist*/ true );
127
+ Worklist. eraseSingleInstFromFunction (*inst,
128
+ /* AddOperandsToWorklist*/ true );
152
129
changed = true ;
153
130
}
154
131
@@ -216,34 +193,12 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
216
193
if (SILInstruction *Result = visit (I)) {
217
194
++NumCombined;
218
195
// Should we replace the old instruction with a new one?
219
- if (Result != I) {
220
- assert (&*std::prev (SILBasicBlock::iterator (I)) == Result &&
221
- " Expected new instruction inserted before existing instruction!" );
222
-
223
- LLVM_DEBUG (llvm::dbgs () << " SC: Old = " << *I << ' \n '
224
- << " New = " << *Result << ' \n ' );
225
-
226
- // Everything uses the new instruction now.
227
- replaceInstUsesPairwiseWith (I, Result);
228
-
229
- // Push the new instruction and any users onto the worklist.
230
- Worklist.add (Result);
231
- Worklist.addUsersOfAllResultsToWorklist (Result);
232
-
233
- eraseInstFromFunction (*I);
234
- } else {
235
- LLVM_DEBUG (llvm::dbgs () << " SC: Mod = " << OrigI << ' \n '
236
- << " New = " << *I << ' \n ' );
237
-
238
- // If the instruction was modified, it's possible that it is now dead.
239
- // if so, remove it.
240
- if (isInstructionTriviallyDead (I)) {
241
- eraseInstFromFunction (*I);
242
- } else {
243
- Worklist.add (I);
244
- Worklist.addUsersOfAllResultsToWorklist (I);
245
- }
246
- }
196
+ Worklist.replaceInstructionWithInstruction (I, Result
197
+ #ifndef NDEBUG
198
+ ,
199
+ OrigI
200
+ #endif
201
+ );
247
202
MadeChange = true ;
248
203
}
249
204
@@ -278,88 +233,6 @@ bool SILCombiner::runOnFunction(SILFunction &F) {
278
233
return Changed;
279
234
}
280
235
281
- // Insert the instruction New before instruction Old in Old's parent BB. Add
282
- // New to the worklist.
283
- SILInstruction *SILCombiner::insertNewInstBefore (SILInstruction *New,
284
- SILInstruction &Old) {
285
- assert (New && New->getParent () == nullptr &&
286
- " New instruction already inserted into a basic block!" );
287
- SILBasicBlock *BB = Old.getParent ();
288
- BB->insert (&Old, New); // Insert inst
289
- Worklist.add (New);
290
- return New;
291
- }
292
-
293
- // This method is to be used when an instruction is found to be dead,
294
- // replaceable with another preexisting expression. Here we add all uses of I
295
- // to the worklist, replace all uses of I with the new value, then return I,
296
- // so that the combiner will know that I was modified.
297
- void SILCombiner::replaceInstUsesWith (SingleValueInstruction &I, ValueBase *V) {
298
- Worklist.addUsersToWorklist (&I); // Add all modified instrs to worklist.
299
-
300
- LLVM_DEBUG (llvm::dbgs () << " SC: Replacing " << I << " \n "
301
- << " with " << *V << ' \n ' );
302
-
303
- I.replaceAllUsesWith (V);
304
- }
305
-
306
- void SILCombiner::replaceValueUsesWith (SILValue oldValue, SILValue newValue) {
307
- Worklist.addUsersToWorklist (oldValue); // Add all modified instrs to worklist.
308
-
309
- LLVM_DEBUG (llvm::dbgs () << " SC: Replacing " << oldValue << " \n "
310
- << " with " << newValue << ' \n ' );
311
-
312
- oldValue->replaceAllUsesWith (newValue);
313
- }
314
-
315
- // / Replace all of the results of the old instruction with the
316
- // / corresponding results of the new instruction.
317
- void SILCombiner::replaceInstUsesPairwiseWith (SILInstruction *oldI,
318
- SILInstruction *newI) {
319
- LLVM_DEBUG (llvm::dbgs () << " SC: Replacing " << *oldI << " \n "
320
- << " with " << *newI << ' \n ' );
321
-
322
- auto oldResults = oldI->getResults ();
323
- auto newResults = newI->getResults ();
324
- assert (oldResults.size () == newResults.size ());
325
- for (auto i : indices (oldResults)) {
326
- // Add all modified instrs to worklist.
327
- Worklist.addUsersToWorklist (oldResults[i]);
328
-
329
- oldResults[i]->replaceAllUsesWith (newResults[i]);
330
- }
331
- }
332
-
333
- // Some instructions can never be "trivially dead" due to side effects or
334
- // producing a void value. In those cases, since we cannot rely on
335
- // SILCombines trivially dead instruction DCE in order to delete the
336
- // instruction, visit methods should use this method to delete the given
337
- // instruction and upon completion of their peephole return the value returned
338
- // by this method.
339
- SILInstruction *
340
- SILCombiner::eraseInstFromFunction (SILInstruction &I,
341
- SILBasicBlock::iterator &InstIter,
342
- bool AddOperandsToWorklist) {
343
- // Delete any debug users first.
344
- for (auto result : I.getResults ()) {
345
- while (!result->use_empty ()) {
346
- auto *user = result->use_begin ()->getUser ();
347
- assert (user->isDebugInstruction ());
348
- if (InstIter == user->getIterator ())
349
- ++InstIter;
350
- Worklist.erase (user);
351
- user->eraseFromParent ();
352
- }
353
- }
354
- if (InstIter == I.getIterator ())
355
- ++InstIter;
356
-
357
- eraseSingleInstFromFunction (I, Worklist, AddOperandsToWorklist);
358
- MadeChange = true ;
359
- // Dummy return, so the caller doesn't need to explicitly return nullptr.
360
- return nullptr ;
361
- }
362
-
363
236
// ===----------------------------------------------------------------------===//
364
237
// Entry Points
365
238
// ===----------------------------------------------------------------------===//
0 commit comments