2727#include " swift/SILOptimizer/PassManager/Passes.h"
2828#include " swift/SILOptimizer/PassManager/Transforms.h"
2929#include " swift/SILOptimizer/Utils/InstOptUtils.h"
30+ #include " swift/SILOptimizer/Utils/OwnershipOptUtils.h"
3031#include " llvm/ADT/STLExtras.h"
3132#include " llvm/ADT/Statistic.h"
3233#include " llvm/Support/CommandLine.h"
@@ -917,6 +918,8 @@ static const int SinkSearchWindow = 6;
917918
918919// / Returns True if we can sink this instruction to another basic block.
919920static bool canSinkInstruction (SILInstruction *Inst) {
921+ if (hasOwnershipOperandsOrResults (Inst))
922+ return false ;
920923 return !Inst->hasUsesOfAnyResult () && !isa<TermInst>(Inst);
921924}
922925
@@ -1178,6 +1181,9 @@ static bool sinkArgument(EnumCaseDataflowContext &Context, SILBasicBlock *BB, un
11781181 if (!FSI || !hasOneNonDebugUse (FSI))
11791182 return false ;
11801183
1184+ if (hasOwnershipOperandsOrResults (FSI))
1185+ return false ;
1186+
11811187 // The list of identical instructions.
11821188 SmallVector<SingleValueInstruction *, 8 > Clones;
11831189 Clones.push_back (FSI);
@@ -1743,38 +1749,43 @@ static bool processFunction(SILFunction *F, AliasAnalysis *AA,
17431749 LLVM_DEBUG (llvm::dbgs () << " Merging predecessors!\n " );
17441750 State.mergePredecessorStates ();
17451751
1746- // If our predecessors cover any of our enum values, attempt to hoist
1747- // releases up the CFG onto enum payloads or sink retains out of switch
1748- // regions.
1749- LLVM_DEBUG (llvm::dbgs () << " Attempting to move releases into "
1750- " predecessors!\n " );
1751-
1752- // Perform a relatively local forms of retain sinking and release hoisting
1753- // regarding switch regions and SILargument. This are not handled by retain
1754- // release code motion.
1755- if (HoistReleases) {
1756- Changed |= State.hoistDecrementsIntoSwitchRegions (AA);
1757- }
1752+ if (!F->hasOwnership ()) {
1753+ // If our predecessors cover any of our enum values, attempt to hoist
1754+ // releases up the CFG onto enum payloads or sink retains out of switch
1755+ // regions.
1756+ LLVM_DEBUG (llvm::dbgs () << " Attempting to move releases into "
1757+ " predecessors!\n " );
1758+
1759+ // Perform a relatively local forms of retain sinking and release hoisting
1760+ // regarding switch regions and SILargument. This are not handled by retain
1761+ // release code motion.
1762+ if (HoistReleases) {
1763+ Changed |= State.hoistDecrementsIntoSwitchRegions (AA);
1764+ }
17581765
1759- // Sink switch related retains.
1760- Changed |= sinkIncrementsIntoSwitchRegions (State.getBB (), AA, RCIA);
1761- Changed |= State.sinkIncrementsOutOfSwitchRegions (AA, RCIA);
1766+ // Sink switch related retains.
1767+ Changed |= sinkIncrementsIntoSwitchRegions (State.getBB (), AA, RCIA);
1768+ Changed |= State.sinkIncrementsOutOfSwitchRegions (AA, RCIA);
17621769
1763- // Then attempt to sink code from predecessors. This can include retains
1764- // which is why we always attempt to move releases up the CFG before sinking
1765- // code from predecessors. We will never sink the hoisted releases from
1766- // predecessors since the hoisted releases will be on the enum payload
1767- // instead of the enum itself.
1768- Changed |= canonicalizeRefCountInstrs (State.getBB ());
1770+ // Then attempt to sink code from predecessors. This can include retains
1771+ // which is why we always attempt to move releases up the CFG before sinking
1772+ // code from predecessors. We will never sink the hoisted releases from
1773+ // predecessors since the hoisted releases will be on the enum payload
1774+ // instead of the enum itself.
1775+ Changed |= canonicalizeRefCountInstrs (State.getBB ());
1776+ }
17691777 Changed |= sinkCodeFromPredecessors (BBToStateMap, State.getBB ());
17701778 Changed |= sinkArgumentsFromPredecessors (BBToStateMap, State.getBB ());
17711779 Changed |= sinkLiteralsFromPredecessors (State.getBB ());
1772- // Try to hoist release of a SILArgument to predecessors.
1773- Changed |= hoistSILArgumentReleaseInst (State.getBB ());
17741780
1775- // Then perform the dataflow.
1776- LLVM_DEBUG (llvm::dbgs () << " Performing the dataflow!\n " );
1777- Changed |= State.process ();
1781+ if (!F->hasOwnership ()) {
1782+ // Try to hoist release of a SILArgument to predecessors.
1783+ Changed |= hoistSILArgumentReleaseInst (State.getBB ());
1784+
1785+ // Then perform the dataflow.
1786+ LLVM_DEBUG (llvm::dbgs () << " Performing the dataflow!\n " );
1787+ Changed |= State.process ();
1788+ }
17781789 }
17791790
17801791 return Changed;
@@ -1791,9 +1802,6 @@ class SILCodeMotion : public SILFunctionTransform {
17911802 // / The entry point to the transformation.
17921803 void run () override {
17931804 auto *F = getFunction ();
1794- // Skip functions with ownership for now.
1795- if (F->hasOwnership ())
1796- return ;
17971805
17981806 auto *AA = getAnalysis<AliasAnalysis>(F);
17991807 auto *PO = getAnalysis<PostOrderAnalysis>()->get (F);
0 commit comments