Skip to content

Commit 0dce4da

Browse files
committed
[SIL] Tweaked worklist instruction methods' style.
Adjusted some variable names.
1 parent 91fff19 commit 0dce4da

File tree

1 file changed

+84
-74
lines changed

1 file changed

+84
-74
lines changed

include/swift/SIL/SILInstructionWorklist.h

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -134,80 +134,88 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
134134
worklist.clear();
135135
}
136136

137-
/// Find usages of \p I and replace them with usages of \p Result.
137+
/// Find usages of \p instruction and replace them with usages of \p result.
138138
///
139-
/// Intended to be called during visitation after \p I has been removed from
140-
/// the worklist.
139+
/// Intended to be called during visitation after \p instruction has been
140+
/// removed from the worklist.
141141
///
142-
/// \p I the instruction whose usages will be replaced
143-
/// \p Result the instruction whose usages will replace \p I
144-
bool replaceInstructionWithInstruction(
145-
SILInstruction *I,
146-
SILInstruction *Result
142+
/// \p instruction the instruction whose usages will be replaced
143+
/// \p result the instruction whose usages will replace \p instruction
144+
///
145+
/// \return whether the instruction was deleted or modified.
146+
bool replaceInstructionWithInstruction(SILInstruction *instruction,
147+
SILInstruction *result
147148
#ifndef NDNEBUG
148-
, std::string OrigI
149+
,
150+
std::string instructionDescription
149151
#endif
150152
) {
151-
if (Result != I) {
152-
assert(&*std::prev(SILBasicBlock::iterator(I)) == Result &&
153-
"Expected new instruction inserted before existing instruction!");
153+
if (result != instruction) {
154+
assert(&*std::prev(instruction->getIterator()) == result &&
155+
"Expected new instruction inserted before existing instruction!");
154156

155157
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
156-
stream << loggingName << ": Old = " << *I << '\n'
157-
<< " " << " New = " << *Result << '\n';
158+
stream << loggingName << ": Old = " << *instruction << '\n'
159+
<< " "
160+
<< " New = " << *result << '\n';
158161
});
159162

160163
// Everything uses the new instruction now.
161-
replaceInstUsesPairwiseWith(I, Result);
164+
replaceInstUsesPairwiseWith(instruction, result);
162165

163166
// Push the new instruction and any users onto the worklist.
164-
add(Result);
165-
addUsersOfAllResultsToWorklist(Result);
167+
add(result);
168+
addUsersOfAllResultsToWorklist(result);
166169

167-
eraseInstFromFunction(*I);
170+
eraseInstFromFunction(*instruction);
168171

169172
return true;
170173
} else {
171174
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
172-
stream << loggingName << ": Mod = " << OrigI << '\n'
173-
<< " " << " New = " << *I << '\n';
175+
stream << loggingName << ": Mod = " << instructionDescription << '\n'
176+
<< " "
177+
<< " New = " << *instruction << '\n';
174178
});
175179

176180
// If the instruction was modified, it's possible that it is now dead.
177181
// if so, remove it.
178-
if (isInstructionTriviallyDead(I)) {
179-
eraseInstFromFunction(*I);
182+
if (isInstructionTriviallyDead(instruction)) {
183+
eraseInstFromFunction(*instruction);
180184
} else {
181-
add(I);
182-
addUsersOfAllResultsToWorklist(I);
185+
add(instruction);
186+
addUsersOfAllResultsToWorklist(instruction);
183187
}
184188
return false;
185189
}
186190
}
187191

188-
// Insert the instruction New before instruction Old in Old's parent BB. Add
189-
// New to the worklist.
190-
SILInstruction *insertNewInstBefore(SILInstruction *New, SILInstruction &Old) {
191-
assert(New && New->getParent() == nullptr &&
192-
"New instruction already inserted into a basic block!");
193-
SILBasicBlock *BB = Old.getParent();
194-
BB->insert(&Old, New); // Insert inst
195-
add(New);
196-
return New;
192+
// Insert the instruction newInstruction before instruction old in old's
193+
// parent block. Add newInstruction to the worklist.
194+
SILInstruction *insertNewInstBefore(SILInstruction *newInstruction,
195+
SILInstruction &old) {
196+
assert(newInstruction && newInstruction->getParent() == nullptr &&
197+
"newInstruction instruction already inserted into a basic block!");
198+
SILBasicBlock *block = old.getParent();
199+
block->insert(&old, newInstruction); // Insert inst
200+
add(newInstruction);
201+
return newInstruction;
197202
}
198203

199204
// This method is to be used when an instruction is found to be dead,
200-
// replaceable with another preexisting expression. Here we add all uses of I
201-
// to the worklist, and replace all uses of I with the new value.
202-
void replaceInstUsesWith(SingleValueInstruction &I, ValueBase *V) {
203-
addUsersToWorklist(&I); // Add all modified instrs to worklist.
205+
// replaceable with another preexisting expression. Here we add all uses of
206+
// instruction to the worklist, and replace all uses of instruction with the
207+
// new value.
208+
void replaceInstUsesWith(SingleValueInstruction &instruction,
209+
ValueBase *value) {
210+
addUsersToWorklist(&instruction); // Add all modified instrs to worklist.
204211

205212
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
206-
stream << loggingName << ": Replacing " << I << '\n'
207-
<< " " << " with " << *V << '\n';
213+
stream << loggingName << ": Replacing " << instruction << '\n'
214+
<< " "
215+
<< " with " << *value << '\n';
208216
});
209217

210-
I.replaceAllUsesWith(V);
218+
instruction.replaceAllUsesWith(value);
211219
}
212220

213221
// This method is to be used when a value is found to be dead,
@@ -219,7 +227,8 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
219227

220228
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
221229
stream << loggingName << ": Replacing " << oldValue << '\n'
222-
<< " " << " with " << newValue << '\n';
230+
<< " "
231+
<< " with " << newValue << '\n';
223232
});
224233

225234
oldValue->replaceAllUsesWith(newValue);
@@ -228,7 +237,8 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
228237
void replaceInstUsesPairwiseWith(SILInstruction *oldI, SILInstruction *newI) {
229238
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
230239
stream << loggingName << ": Replacing " << *oldI << '\n'
231-
<< " " << " with " << *newI << '\n';
240+
<< " "
241+
<< " with " << *newI << '\n';
232242
});
233243

234244
auto oldResults = oldI->getResults();
@@ -243,61 +253,61 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
243253
}
244254

245255
// Some instructions can never be "trivially dead" due to side effects or
246-
// producing a void value. In those cases visit methods should use this
247-
// method to delete the given instruction and upon completion of their
248-
// peephole return the value returned by this method.
249-
void eraseInstFromFunction(SILInstruction &I,
250-
SILBasicBlock::iterator &InstIter,
251-
bool AddOperandsToWorklist = true) {
256+
// producing a void value. In those cases, visit methods should use this
257+
// method to delete the given instruction.
258+
void eraseInstFromFunction(SILInstruction &instruction,
259+
SILBasicBlock::iterator &iterator,
260+
bool addOperandsToWorklist = true) {
252261
// Delete any debug users first.
253-
for (auto result : I.getResults()) {
262+
for (auto result : instruction.getResults()) {
254263
while (!result->use_empty()) {
255264
auto *user = result->use_begin()->getUser();
256265
assert(user->isDebugInstruction());
257-
if (InstIter == user->getIterator())
258-
++InstIter;
266+
if (iterator == user->getIterator())
267+
++iterator;
259268
erase(user);
260269
user->eraseFromParent();
261270
}
262271
}
263-
if (InstIter == I.getIterator())
264-
++InstIter;
272+
if (iterator == instruction.getIterator())
273+
++iterator;
265274

266-
eraseSingleInstFromFunction(I, AddOperandsToWorklist);
275+
eraseSingleInstFromFunction(instruction, addOperandsToWorklist);
267276
}
268277

269-
void eraseInstFromFunction(SILInstruction &I,
270-
bool AddOperandsToWorklist = true) {
278+
void eraseInstFromFunction(SILInstruction &instruction,
279+
bool addOperandsToWorklist = true) {
271280
SILBasicBlock::iterator nullIter;
272-
return eraseInstFromFunction(I, nullIter, AddOperandsToWorklist);
281+
return eraseInstFromFunction(instruction, nullIter, addOperandsToWorklist);
273282
}
274283

275-
void eraseSingleInstFromFunction(
276-
SILInstruction &I,
277-
bool AddOperandsToWorklist) {
278-
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
279-
stream << loggingName << ": ERASE " << I << '\n';
280-
});
284+
void eraseSingleInstFromFunction(SILInstruction &instruction,
285+
bool addOperandsToWorklist) {
286+
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
287+
stream << loggingName << ": ERASE " << instruction << '\n';
288+
});
281289

282-
assert(!I.hasUsesOfAnyResult() && "Cannot erase instruction that is used!");
290+
assert(!instruction.hasUsesOfAnyResult() &&
291+
"Cannot erase instruction that is used!");
283292

284293
// Make sure that we reprocess all operands now that we reduced their
285294
// use counts.
286-
if (I.getNumOperands() < 8 && AddOperandsToWorklist) {
287-
for (auto &OpI : I.getAllOperands()) {
288-
if (auto *Op = OpI.get()->getDefiningInstruction()) {
289-
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
290-
stream << loggingName << ": add op " << *Op << '\n'
291-
<< " from erased inst to worklist\n";
295+
if (instruction.getNumOperands() < 8 && addOperandsToWorklist) {
296+
for (auto &operand : instruction.getAllOperands()) {
297+
if (auto *operandInstruction =
298+
operand.get()->getDefiningInstruction()) {
299+
withDebugStream([&](llvm::raw_ostream &stream,
300+
StringRef loggingName) {
301+
stream << loggingName << ": add op " << *operandInstruction << '\n'
302+
<< " from erased inst to worklist\n";
292303
});
293-
add(Op);
304+
add(operandInstruction);
294305
}
295306
}
296307
}
297-
erase(&I);
298-
I.eraseFromParent();
308+
erase(&instruction);
309+
instruction.eraseFromParent();
299310
}
300-
301311
};
302312

303313
// TODO: This name is somewhat unpleasant. Once the type is templated over its

0 commit comments

Comments
 (0)