Skip to content

Commit 0ebe825

Browse files
committed
[NFC] MOWTE: Deduplicated code.
Replaced copy-pasted code with a twice-invoked closure. In preparation to add a third invocation.
1 parent 3e84c5c commit 0ebe825

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyWrappedTypeEliminator.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -298,47 +298,51 @@ bool SILMoveOnlyWrappedTypeEliminator::process() {
298298
llvm::SmallSetVector<SILArgument *, 8> touchedArgs;
299299
llvm::SmallSetVector<SILInstruction *, 8> touchedInsts;
300300

301+
auto recordValue = [&touchedInsts, fn = fn,
302+
trivialOnly = trivialOnly](SILValue value) -> bool {
303+
if (!value->getType().isMoveOnlyWrapped() &&
304+
!value->getType().isBoxedMoveOnlyWrappedType(fn))
305+
return false;
306+
307+
// If we are looking at trivial only, skip non-trivial function args.
308+
if (trivialOnly && !isMoveOnlyWrappedTrivial(value))
309+
return false;
310+
311+
value->unsafelyEliminateMoveOnlyWrapper(fn);
312+
for (auto *use : value->getNonTypeDependentUses())
313+
touchedInsts.insert(use->getUser());
314+
return true;
315+
};
316+
301317
for (auto &bb : *fn) {
302318
for (auto *arg : bb.getArguments()) {
303-
if (!arg->getType().isMoveOnlyWrapped() &&
304-
!arg->getType().isBoxedMoveOnlyWrappedType(fn))
305-
continue;
306-
307-
// If we are looking at trivial only, skip non-trivial function args.
308-
if (trivialOnly &&
309-
!arg->getType().removingMoveOnlyWrapper().isTrivial(*fn))
319+
bool relevant = recordValue(arg);
320+
if (!relevant)
310321
continue;
311322

312-
arg->unsafelyEliminateMoveOnlyWrapper(fn);
313-
314323
// If our new type is trivial, convert the arguments ownership to
315324
// None. Otherwise, preserve the ownership kind of the argument.
316325
if (arg->getType().isTrivial(*fn))
317326
arg->setOwnershipKind(OwnershipKind::None);
318327
touchedArgs.insert(arg);
319-
for (auto *use : arg->getNonTypeDependentUses())
320-
touchedInsts.insert(use->getUser());
328+
329+
madeChange = true;
321330
}
322331

323332
for (auto &ii : bb) {
324-
for (SILValue v : ii.getResults()) {
325-
if (!v->getType().isMoveOnlyWrapped() &&
326-
!v->getType().isBoxedMoveOnlyWrappedType(fn))
327-
continue;
328-
329-
if (trivialOnly &&
330-
!isMoveOnlyWrappedTrivial(v))
333+
bool touched = false;
334+
for (SILValue value : ii.getResults()) {
335+
bool relevant = recordValue(value);
336+
if (!relevant)
331337
continue;
332338

333-
v->unsafelyEliminateMoveOnlyWrapper(fn);
334-
touchedInsts.insert(&ii);
335-
336-
// Add all users as well. This ensures we visit things like
337-
// destroy_value and end_borrow.
338-
for (auto *use : v->getNonTypeDependentUses())
339-
touchedInsts.insert(use->getUser());
340-
madeChange = true;
339+
touched = true;
341340
}
341+
if (!touched)
342+
continue;
343+
touchedInsts.insert(&ii);
344+
345+
madeChange = true;
342346
}
343347
}
344348

0 commit comments

Comments
 (0)