Skip to content

Commit d7354b6

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 a6f7758 commit d7354b6

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyWrappedTypeEliminator.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -299,45 +299,55 @@ bool SILMoveOnlyWrappedTypeEliminator::process() {
299299
llvm::SmallSetVector<SILArgument *, 8> touchedArgs;
300300
llvm::SmallSetVector<SILInstruction *, 8> touchedInsts;
301301

302+
// For each value whose type is move-only wrapped:
303+
// - rewrite the value's type
304+
// - record its users for later visitation
305+
auto visitValue = [&touchedInsts, fn = fn,
306+
trivialOnly = trivialOnly](SILValue value) -> bool {
307+
if (!value->getType().hasAnyMoveOnlyWrapping(fn))
308+
return false;
309+
310+
// If we are looking at trivial only, skip non-trivial function args.
311+
if (trivialOnly && !isMoveOnlyWrappedTrivial(value))
312+
return false;
313+
314+
for (auto *use : value->getNonTypeDependentUses())
315+
touchedInsts.insert(use->getUser());
316+
317+
value->unsafelyEliminateMoveOnlyWrapper(fn);
318+
319+
return true;
320+
};
321+
302322
for (auto &bb : *fn) {
303323
for (auto *arg : bb.getArguments()) {
304-
if (!arg->getType().hasAnyMoveOnlyWrapping(fn))
324+
bool relevant = visitValue(arg);
325+
if (!relevant)
305326
continue;
306327

307-
// If we are looking at trivial only, skip non-trivial function args.
308-
if (trivialOnly &&
309-
!arg->getType().removingMoveOnlyWrapper().isTrivial(*fn))
310-
continue;
311-
312-
arg->unsafelyEliminateMoveOnlyWrapper(fn);
313-
314328
// If our new type is trivial, convert the arguments ownership to
315329
// None. Otherwise, preserve the ownership kind of the argument.
316330
if (arg->getType().isTrivial(*fn))
317331
arg->setOwnershipKind(OwnershipKind::None);
318332
touchedArgs.insert(arg);
319-
for (auto *use : arg->getNonTypeDependentUses())
320-
touchedInsts.insert(use->getUser());
333+
334+
madeChange = true;
321335
}
322336

323337
for (auto &ii : bb) {
324-
for (SILValue v : ii.getResults()) {
325-
if (!v->getType().hasAnyMoveOnlyWrapping(fn))
326-
continue;
327-
328-
if (trivialOnly &&
329-
!isMoveOnlyWrappedTrivial(v))
338+
bool touched = false;
339+
for (SILValue value : ii.getResults()) {
340+
bool relevant = visitValue(value);
341+
if (!relevant)
330342
continue;
331343

332-
v->unsafelyEliminateMoveOnlyWrapper(fn);
333-
touchedInsts.insert(&ii);
334-
335-
// Add all users as well. This ensures we visit things like
336-
// destroy_value and end_borrow.
337-
for (auto *use : v->getNonTypeDependentUses())
338-
touchedInsts.insert(use->getUser());
339-
madeChange = true;
344+
touched = true;
340345
}
346+
if (!touched)
347+
continue;
348+
touchedInsts.insert(&ii);
349+
350+
madeChange = true;
341351
}
342352
}
343353

0 commit comments

Comments
 (0)