mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels the SIL/XXUtils convention. New: - InstOptUtils.h - CFGOptUtils.h - BasicBlockOptUtils.h - ValueLifetime.h Removed: - Local.h - Two conflicting CFG.h files This reorganization is helpful before I introduce more utilities for block cloning similar to SinkAddressProjections. Move the control flow utilies out of Local.h, which was an unreadable, unprincipled mess. Rename it to InstOptUtils.h, and confine it to small APIs for working with individual instructions. These are the optimizer's additions to /SIL/InstUtils.h. Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now there is only SIL/CFG.h, resolving the naming conflict within the swift project (this has always been a problem for source tools). Limit this header to low-level APIs for working with branches and CFG edges. Add BasicBlockOptUtils.h for block level transforms (it makes me sad that I can't use BBOptUtils.h, but SIL already has BasicBlockUtils.h). These are larger APIs for cloning or removing whole blocks.
This commit is contained in:
@@ -24,12 +24,13 @@
|
||||
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
|
||||
#include "swift/SILOptimizer/PassManager/Passes.h"
|
||||
#include "swift/SILOptimizer/PassManager/Transforms.h"
|
||||
#include "swift/SILOptimizer/Utils/CFG.h"
|
||||
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
|
||||
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
|
||||
#include "swift/SILOptimizer/Utils/CastOptimizer.h"
|
||||
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
|
||||
#include "swift/SILOptimizer/Utils/Local.h"
|
||||
#include "swift/SILOptimizer/Utils/ConstantFolding.h"
|
||||
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
|
||||
#include "swift/SILOptimizer/Utils/SILInliner.h"
|
||||
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
|
||||
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
|
||||
#include "llvm/ADT/SmallPtrSet.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
@@ -201,125 +202,6 @@ namespace {
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
/// Return true if there are any users of V outside the specified block.
|
||||
static bool isUsedOutsideOfBlock(SILValue V, SILBasicBlock *BB) {
|
||||
for (auto UI : V->getUses())
|
||||
if (UI->getUser()->getParent() != BB)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Populate 'projections' with the chain of address projections leading
|
||||
// to and including 'inst'.
|
||||
//
|
||||
// Populate 'inBlockDefs' with all the non-address value definitions in
|
||||
// the block that will be used outside this block after projection sinking.
|
||||
//
|
||||
// Return true on success, even if projections is empty.
|
||||
bool SinkAddressProjections::analyzeAddressProjections(SILInstruction *inst) {
|
||||
projections.clear();
|
||||
inBlockDefs.clear();
|
||||
|
||||
SILBasicBlock *bb = inst->getParent();
|
||||
auto pushOperandVal = [&](SILValue def) {
|
||||
if (def->getParentBlock() != bb)
|
||||
return true;
|
||||
|
||||
if (!def->getType().isAddress()) {
|
||||
inBlockDefs.insert(def);
|
||||
return true;
|
||||
}
|
||||
if (auto *addressProj = dyn_cast<SingleValueInstruction>(def)) {
|
||||
if (addressProj->isTriviallyDuplicatable()) {
|
||||
projections.push_back(addressProj);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Can't handle a multi-value or unclonable address producer.
|
||||
return false;
|
||||
};
|
||||
// Check the given instruction for any address-type results.
|
||||
for (auto result : inst->getResults()) {
|
||||
if (!isUsedOutsideOfBlock(result, bb))
|
||||
continue;
|
||||
if (!pushOperandVal(result))
|
||||
return false;
|
||||
}
|
||||
// Recurse upward through address projections.
|
||||
for (unsigned idx = 0; idx < projections.size(); ++idx) {
|
||||
// Only one address result/operand can be handled per instruction.
|
||||
if (projections.size() != idx + 1)
|
||||
return false;
|
||||
|
||||
for (SILValue operandVal : projections[idx]->getOperandValues())
|
||||
pushOperandVal(operandVal);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Clone the projections gathered by 'analyzeAddressProjections' at
|
||||
// their use site outside this block.
|
||||
bool SinkAddressProjections::cloneProjections() {
|
||||
if (projections.empty())
|
||||
return false;
|
||||
|
||||
SILBasicBlock *bb = projections.front()->getParent();
|
||||
SmallVector<Operand *, 4> usesToReplace;
|
||||
// Clone projections in last-to-first order.
|
||||
for (unsigned idx = 0; idx < projections.size(); ++idx) {
|
||||
auto *oldProj = projections[idx];
|
||||
assert(oldProj->getParent() == bb);
|
||||
usesToReplace.clear();
|
||||
for (Operand *use : oldProj->getUses()) {
|
||||
if (use->getUser()->getParent() != bb)
|
||||
usesToReplace.push_back(use);
|
||||
}
|
||||
for (Operand *use : usesToReplace) {
|
||||
auto *newProj = oldProj->clone(use->getUser());
|
||||
use->set(cast<SingleValueInstruction>(newProj));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Helper function to perform SSA updates in case of jump threading.
|
||||
void swift::updateSSAAfterCloning(BasicBlockCloner &Cloner,
|
||||
SILBasicBlock *SrcBB, SILBasicBlock *DestBB) {
|
||||
SILSSAUpdater SSAUp;
|
||||
for (auto AvailValPair : Cloner.AvailVals) {
|
||||
ValueBase *Inst = AvailValPair.first;
|
||||
if (Inst->use_empty())
|
||||
continue;
|
||||
|
||||
SILValue NewRes(AvailValPair.second);
|
||||
|
||||
SmallVector<UseWrapper, 16> UseList;
|
||||
// Collect the uses of the value.
|
||||
for (auto Use : Inst->getUses())
|
||||
UseList.push_back(UseWrapper(Use));
|
||||
|
||||
SSAUp.Initialize(Inst->getType());
|
||||
SSAUp.AddAvailableValue(DestBB, Inst);
|
||||
SSAUp.AddAvailableValue(SrcBB, NewRes);
|
||||
|
||||
if (UseList.empty())
|
||||
continue;
|
||||
|
||||
// Update all the uses.
|
||||
for (auto U : UseList) {
|
||||
Operand *Use = U;
|
||||
SILInstruction *User = Use->getUser();
|
||||
assert(User && "Missing user");
|
||||
|
||||
// Ignore uses in the same basic block.
|
||||
if (User->getParent() == DestBB)
|
||||
continue;
|
||||
|
||||
SSAUp.RewriteUse(*Use);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SILValue getTerminatorCondition(TermInst *Term) {
|
||||
if (auto *CondBr = dyn_cast<CondBranchInst>(Term))
|
||||
return stripExpectIntrinsic(CondBr->getCondition());
|
||||
@@ -1044,7 +926,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
|
||||
|
||||
// Are the arguments to this block used outside of the block.
|
||||
for (auto Arg : DestBB->getArguments())
|
||||
if ((NeedToUpdateSSA |= isUsedOutsideOfBlock(Arg, DestBB))) {
|
||||
if ((NeedToUpdateSSA |= isUsedOutsideOfBlock(Arg))) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user