Skip to content

[pmo] Simplify code by inverting an if check. #21640

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
63 changes: 39 additions & 24 deletions lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,35 +1273,50 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
}
}

// If the memory object has non-trivial type, then removing the deallocation
// will drop any releases. Check that there is nothing preventing removal.
// If our memory is trivially typed, we can just remove it without needing to
// consider if the stored value needs to be destroyed. So at this point,
// delete the memory!
if (MemoryType.isTrivial(Module)) {
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated trivial allocation: "
<< *TheMemory);

// If it is safe to remove, do it. Recursively remove all instructions
// hanging off the allocation instruction, then return success. Let the
// caller remove the allocation itself to avoid iterator invalidation.
eraseUsesOfInstruction(TheMemory);

return true;
}

// Otherwise removing the deallocation will drop any releases. Check that
// there is nothing preventing removal.
llvm::SmallVector<unsigned, 8> DestroyAddrIndices;
llvm::SmallVector<AvailableValue, 32> AvailableValueList;
llvm::SmallVector<unsigned, 8> AvailableValueStartOffsets;

if (!MemoryType.isTrivial(Module)) {
for (auto P : llvm::enumerate(Releases)) {
auto *R = P.value();
if (R == nullptr)
continue;

// We stash all of the destroy_addr that we see.
if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
AvailableValueStartOffsets.push_back(AvailableValueList.size());
// Make sure we can actually promote this destroy addr. If we can not,
// then we must bail. In order to not gather available values twice, we
// gather the available values here that we will use to promote the
// values.
if (!canPromoteDestroyAddr(DAI, AvailableValueList))
return false;
DestroyAddrIndices.push_back(P.index());
continue;
}
for (auto P : llvm::enumerate(Releases)) {
auto *R = P.value();
if (R == nullptr)
continue;

LLVM_DEBUG(llvm::dbgs() << "*** Failed to remove autogenerated alloc: "
"kept alive by release: " << *R);
return false;
// We stash all of the destroy_addr that we see.
if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
AvailableValueStartOffsets.push_back(AvailableValueList.size());
// Make sure we can actually promote this destroy addr. If we can not,
// then we must bail. In order to not gather available values twice, we
// gather the available values here that we will use to promote the
// values.
if (!canPromoteDestroyAddr(DAI, AvailableValueList))
return false;
DestroyAddrIndices.push_back(P.index());
continue;
}

LLVM_DEBUG(llvm::dbgs()
<< "*** Failed to remove autogenerated non-trivial alloc: "
"kept alive by release: "
<< *R);
return false;
}

// If we reached this point, we can promote all of our destroy_addr.
Expand All @@ -1324,7 +1339,7 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
Releases[DestroyAddrIndex] = nullptr;
}

LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated alloc_stack: "
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated non-trivial alloc: "
<< *TheMemory);

// If it is safe to remove, do it. Recursively remove all instructions
Expand Down