Skip to content

[constant-folding] Eliminate complexity introducing pre-mature optimization. #27997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1939,51 +1939,8 @@ ConstantFolder::processWorkList() {
// Ok, we have succeeded. Add user to the FoldedUsers list and perform
// the necessary cleanups, RAUWs, etc.
FoldedUsers.insert(User);
++NumInstFolded;

InvalidateInstructions = true;

// If the constant produced a tuple, be smarter than RAUW: explicitly
// nuke any tuple_extract instructions using the apply. This is a
// common case for functions returning multiple values.
if (auto *TI = dyn_cast<TupleInst>(C)) {
for (SILValue Result : User->getResults()) {
for (auto UI = Result->use_begin(), UE = Result->use_end();
UI != UE;) {
Operand *O = *UI++;

// If the user is a tuple_extract, just substitute the right
// value in.
if (auto *TEI = dyn_cast<TupleExtractInst>(O->getUser())) {
SILValue NewVal = TI->getOperand(TEI->getFieldNo());
TEI->replaceAllUsesWith(NewVal);
TEI->dropAllReferences();
FoldedUsers.insert(TEI);
if (auto *Inst = NewVal->getDefiningInstruction())
WorkList.insert(Inst);
continue;
}

if (auto *DTI = dyn_cast<DestructureTupleInst>(O->getUser())) {
SILValue NewVal = TI->getOperand(O->getOperandNumber());
auto OwnershipKind = NewVal.getOwnershipKind();
if (OwnershipKind.isCompatibleWith(
ValueOwnershipKind::Guaranteed)) {
SILValue DTIResult = DTI->getResult(O->getOperandNumber());
DTIResult->replaceAllUsesWith(NewVal);
FoldedUsers.insert(DTI);
if (auto *Inst = NewVal->getDefiningInstruction())
WorkList.insert(Inst);
continue;
}
}
}
}

if (llvm::all_of(User->getResults(),
[](SILValue v) { return v->use_empty(); }))
FoldedUsers.insert(TI);
}
++NumInstFolded;

// We were able to fold, so all users should use the new folded
// value. If we don't have any such users, continue.
Expand Down