Skip to content

Commit 91fff19

Browse files
committed
[SILCombiner] Use methods from SILInstructionWorklist.
In the previous commit, various methods for adding, replacing, and removing instructions were duplicate from SILCombiner into SILInstructionWorklist. Here, SILCombiner is modified to call through to the methods which were added to SILInstructionWorklist.
1 parent bf8f266 commit 91fff19

File tree

2 files changed

+27
-140
lines changed

2 files changed

+27
-140
lines changed

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 8 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,6 @@ void SILCombiner::addReachableCodeToWorklist(SILBasicBlock *BB) {
101101
addInitialGroup(InstrsForSILCombineWorklist);
102102
}
103103

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-
127104
//===----------------------------------------------------------------------===//
128105
// Implementation
129106
//===----------------------------------------------------------------------===//
@@ -147,8 +124,8 @@ class SILCombineCanonicalize final : CanonicalizeInstruction {
147124
// Just delete the given 'inst' and record its operands. The callback isn't
148125
// allowed to mutate any other instructions.
149126
void killInstruction(SILInstruction *inst) override {
150-
eraseSingleInstFromFunction(*inst, Worklist,
151-
/*AddOperandsToWorklist*/ true);
127+
Worklist.eraseSingleInstFromFunction(*inst,
128+
/*AddOperandsToWorklist*/ true);
152129
changed = true;
153130
}
154131

@@ -216,34 +193,12 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
216193
if (SILInstruction *Result = visit(I)) {
217194
++NumCombined;
218195
// 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+
);
247202
MadeChange = true;
248203
}
249204

@@ -278,88 +233,6 @@ bool SILCombiner::runOnFunction(SILFunction &F) {
278233
return Changed;
279234
}
280235

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-
363236
//===----------------------------------------------------------------------===//
364237
// Entry Points
365238
//===----------------------------------------------------------------------===//

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,30 @@ class SILCombiner :
103103

104104
// Insert the instruction New before instruction Old in Old's parent BB. Add
105105
// New to the worklist.
106-
SILInstruction *insertNewInstBefore(SILInstruction *New, SILInstruction &Old);
106+
SILInstruction *insertNewInstBefore(SILInstruction *New,
107+
SILInstruction &Old) {
108+
return Worklist.insertNewInstBefore(New, Old);
109+
}
107110

108111
// This method is to be used when an instruction is found to be dead,
109112
// replaceable with another preexisting expression. Here we add all uses of I
110113
// to the worklist, replace all uses of I with the new value, then return I,
111114
// so that the combiner will know that I was modified.
112-
void replaceInstUsesWith(SingleValueInstruction &I, ValueBase *V);
115+
void replaceInstUsesWith(SingleValueInstruction &I, ValueBase *V) {
116+
return Worklist.replaceInstUsesWith(I, V);
117+
}
113118

114119
// This method is to be used when a value is found to be dead,
115120
// replaceable with another preexisting expression. Here we add all
116121
// uses of oldValue to the worklist, replace all uses of oldValue
117122
// with newValue.
118-
void replaceValueUsesWith(SILValue oldValue, SILValue newValue);
123+
void replaceValueUsesWith(SILValue oldValue, SILValue newValue) {
124+
Worklist.replaceValueUsesWith(oldValue, newValue);
125+
}
119126

120-
void replaceInstUsesPairwiseWith(SILInstruction *oldI, SILInstruction *newI);
127+
void replaceInstUsesPairwiseWith(SILInstruction *oldI, SILInstruction *newI) {
128+
Worklist.replaceInstUsesPairwiseWith(oldI, newI);
129+
}
121130

122131
// Some instructions can never be "trivially dead" due to side effects or
123132
// producing a void value. In those cases, since we cannot rely on
@@ -127,7 +136,12 @@ class SILCombiner :
127136
// by this method.
128137
SILInstruction *eraseInstFromFunction(SILInstruction &I,
129138
SILBasicBlock::iterator &InstIter,
130-
bool AddOperandsToWorklist = true);
139+
bool AddOperandsToWorklist = true) {
140+
Worklist.eraseInstFromFunction(I, InstIter, AddOperandsToWorklist);
141+
MadeChange = true;
142+
// Dummy return, so the caller doesn't need to explicitly return nullptr.
143+
return nullptr;
144+
}
131145

132146
SILInstruction *eraseInstFromFunction(SILInstruction &I,
133147
bool AddOperandsToWorklist = true) {

0 commit comments

Comments
 (0)