@@ -134,80 +134,88 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
134
134
worklist.clear ();
135
135
}
136
136
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 .
138
138
// /
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.
141
141
// /
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
147
148
#ifndef NDNEBUG
148
- , std::string OrigI
149
+ ,
150
+ std::string instructionDescription
149
151
#endif
150
152
) {
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!" );
154
156
155
157
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 ' ;
158
161
});
159
162
160
163
// Everything uses the new instruction now.
161
- replaceInstUsesPairwiseWith (I, Result );
164
+ replaceInstUsesPairwiseWith (instruction, result );
162
165
163
166
// Push the new instruction and any users onto the worklist.
164
- add (Result );
165
- addUsersOfAllResultsToWorklist (Result );
167
+ add (result );
168
+ addUsersOfAllResultsToWorklist (result );
166
169
167
- eraseInstFromFunction (*I );
170
+ eraseInstFromFunction (*instruction );
168
171
169
172
return true ;
170
173
} else {
171
174
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 ' ;
174
178
});
175
179
176
180
// If the instruction was modified, it's possible that it is now dead.
177
181
// if so, remove it.
178
- if (isInstructionTriviallyDead (I )) {
179
- eraseInstFromFunction (*I );
182
+ if (isInstructionTriviallyDead (instruction )) {
183
+ eraseInstFromFunction (*instruction );
180
184
} else {
181
- add (I );
182
- addUsersOfAllResultsToWorklist (I );
185
+ add (instruction );
186
+ addUsersOfAllResultsToWorklist (instruction );
183
187
}
184
188
return false ;
185
189
}
186
190
}
187
191
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;
197
202
}
198
203
199
204
// 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.
204
211
205
212
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 ' ;
208
216
});
209
217
210
- I .replaceAllUsesWith (V );
218
+ instruction .replaceAllUsesWith (value );
211
219
}
212
220
213
221
// This method is to be used when a value is found to be dead,
@@ -219,7 +227,8 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
219
227
220
228
withDebugStream ([&](llvm::raw_ostream &stream, StringRef loggingName) {
221
229
stream << loggingName << " : Replacing " << oldValue << ' \n '
222
- << " " << " with " << newValue << ' \n ' ;
230
+ << " "
231
+ << " with " << newValue << ' \n ' ;
223
232
});
224
233
225
234
oldValue->replaceAllUsesWith (newValue);
@@ -228,7 +237,8 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
228
237
void replaceInstUsesPairwiseWith (SILInstruction *oldI, SILInstruction *newI) {
229
238
withDebugStream ([&](llvm::raw_ostream &stream, StringRef loggingName) {
230
239
stream << loggingName << " : Replacing " << *oldI << ' \n '
231
- << " " << " with " << *newI << ' \n ' ;
240
+ << " "
241
+ << " with " << *newI << ' \n ' ;
232
242
});
233
243
234
244
auto oldResults = oldI->getResults ();
@@ -243,61 +253,61 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
243
253
}
244
254
245
255
// 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 ) {
252
261
// Delete any debug users first.
253
- for (auto result : I .getResults ()) {
262
+ for (auto result : instruction .getResults ()) {
254
263
while (!result->use_empty ()) {
255
264
auto *user = result->use_begin ()->getUser ();
256
265
assert (user->isDebugInstruction ());
257
- if (InstIter == user->getIterator ())
258
- ++InstIter ;
266
+ if (iterator == user->getIterator ())
267
+ ++iterator ;
259
268
erase (user);
260
269
user->eraseFromParent ();
261
270
}
262
271
}
263
- if (InstIter == I .getIterator ())
264
- ++InstIter ;
272
+ if (iterator == instruction .getIterator ())
273
+ ++iterator ;
265
274
266
- eraseSingleInstFromFunction (I, AddOperandsToWorklist );
275
+ eraseSingleInstFromFunction (instruction, addOperandsToWorklist );
267
276
}
268
277
269
- void eraseInstFromFunction (SILInstruction &I ,
270
- bool AddOperandsToWorklist = true ) {
278
+ void eraseInstFromFunction (SILInstruction &instruction ,
279
+ bool addOperandsToWorklist = true ) {
271
280
SILBasicBlock::iterator nullIter;
272
- return eraseInstFromFunction (I , nullIter, AddOperandsToWorklist );
281
+ return eraseInstFromFunction (instruction , nullIter, addOperandsToWorklist );
273
282
}
274
283
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
+ });
281
289
282
- assert (!I.hasUsesOfAnyResult () && " Cannot erase instruction that is used!" );
290
+ assert (!instruction.hasUsesOfAnyResult () &&
291
+ " Cannot erase instruction that is used!" );
283
292
284
293
// Make sure that we reprocess all operands now that we reduced their
285
294
// 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 " ;
292
303
});
293
- add (Op );
304
+ add (operandInstruction );
294
305
}
295
306
}
296
307
}
297
- erase (&I );
298
- I .eraseFromParent ();
308
+ erase (&instruction );
309
+ instruction .eraseFromParent ();
299
310
}
300
-
301
311
};
302
312
303
313
// TODO: This name is somewhat unpleasant. Once the type is templated over its
0 commit comments