Skip to content

Commit 22b18b2

Browse files
authored
Merge pull request #4183 from trentxintong/RRCM
Small refactoring in RRCM.
2 parents c80b70f + 34eadd4 commit 22b18b2

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

include/swift/SILOptimizer/Analysis/ARCAnalysis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class SILFunction;
3939
} // end namespace swift
4040

4141
namespace swift {
42+
/// Return true if this is a retain instruction.
43+
bool isRetainInstruction(SILInstruction *II);
44+
45+
/// Return true if this is a release instruction.
46+
bool isReleaseInstruction(SILInstruction *II);
4247

4348
using RetainList = llvm::SmallVector<SILInstruction *, 1>;
4449
using ReleaseList = llvm::SmallVector<SILInstruction *, 1>;

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ using namespace swift;
3131

3232
using BasicBlockRetainValue = std::pair<SILBasicBlock *, SILValue>;
3333

34+
//===----------------------------------------------------------------------===//
35+
// Utility Analysis
36+
//===----------------------------------------------------------------------===//
37+
38+
bool swift::isRetainInstruction(SILInstruction *I) {
39+
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I);
40+
}
41+
42+
43+
bool swift::isReleaseInstruction(SILInstruction *I) {
44+
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
45+
}
46+
3447
//===----------------------------------------------------------------------===//
3548
// Decrement Analysis
3649
//===----------------------------------------------------------------------===//

lib/SILOptimizer/Transforms/RetainReleaseCodeMotion.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
/// Region B to Region A, that means there is no blocking operation from where
4141
/// the release was in Region B and where the release is hoisted to in Region A.
4242
///
43-
/// The question is whether we can introduce such operation while we hoist other
44-
/// releases. The answer is NO. because if such releases exist, they would be
45-
/// blocked by the old release (we remove old release and recreate new ones at
46-
/// the end of the pass) and will not be able to be hoisted beyond the old
47-
/// release.
43+
/// The question is whether we can introduce such operation while we hoist
44+
/// other releases. The answer is NO. because if such releases exist, they
45+
/// would be blocked by the old release (we remove old release and recreate new
46+
/// ones at the end of the pass) and will not be able to be hoisted beyond the
47+
/// old release.
4848
///
4949
/// This proof also hinges on the fact that if release A interferes with
5050
/// releases B then release B must interfere with release A. i.e. the 2
@@ -64,19 +64,18 @@
6464
///
6565
/// TODO: There are a lot of code duplications between retain and release code
6666
/// motion in the data flow part. Consider whether we can share them.
67-
/// Essentially, we can implement the release code motion by inverting the retain
68-
/// code motion, but this can also make the code less readable.
67+
/// Essentially, we can implement the release code motion by inverting the
68+
/// retain code motion, but this can also make the code less readable.
6969
///
7070
//===----------------------------------------------------------------------===//
7171

7272
#define DEBUG_TYPE "sil-rr-code-motion"
7373
#include "swift/SIL/SILBuilder.h"
7474
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
75+
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
7576
#include "swift/SILOptimizer/Analysis/EscapeAnalysis.h"
76-
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
7777
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
7878
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
79-
#include "swift/SILOptimizer/Analysis/ValueTracking.h"
8079
#include "swift/SILOptimizer/PassManager/Passes.h"
8180
#include "swift/SILOptimizer/PassManager/Transforms.h"
8281
#include "swift/SILOptimizer/Utils/CFG.h"
@@ -94,19 +93,8 @@ STATISTIC(NumRetainsSunk, "Number of retains sunk");
9493
STATISTIC(NumReleasesHoisted, "Number of releases hoisted");
9594

9695
llvm::cl::opt<bool> DisableRRCodeMotion("disable-rr-cm", llvm::cl::init(false));
97-
llvm::cl::opt<bool> DisableIfWithCriticalEdge("disable-with-critical-edge", llvm::cl::init(false));
98-
99-
//===----------------------------------------------------------------------===//
100-
// Utility
101-
//===----------------------------------------------------------------------===//
102-
103-
static bool isRetainInstruction(SILInstruction *I) {
104-
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I);
105-
}
106-
107-
static bool isReleaseInstruction(SILInstruction *I) {
108-
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
109-
}
96+
llvm::cl::opt<bool>
97+
DisableIfWithCriticalEdge("disable-with-critical-edge", llvm::cl::init(false));
11098

11199
//===----------------------------------------------------------------------===//
112100
// Block State
@@ -199,13 +187,14 @@ class CodeMotionContext {
199187
/// retain or release.
200188
SILValue getRCRoot(SILInstruction *I) {
201189
assert(isRetainInstruction(I) || isReleaseInstruction(I) &&
202-
"Extracting RC root from Invalid instruction");
190+
"Extracting RC root from invalid instruction");
203191
return getRCRoot(I->getOperand(0));
204192
}
205193

206194
public:
207195
/// Constructor.
208-
CodeMotionContext(llvm::SpecificBumpPtrAllocator<BlockState> &BPA, SILFunction *F,
196+
CodeMotionContext(llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
197+
SILFunction *F,
209198
PostOrderFunctionInfo *PO, AliasAnalysis *AA,
210199
RCIdentityFunctionInfo *RCFI)
211200
: MultiIteration(true), BPA(BPA), F(F), PO(PO), AA(AA), RCFI(RCFI) {}
@@ -396,8 +385,9 @@ void RetainCodeMotionContext::initializeCodeMotionDataFlow() {
396385

397386
// Initialize all the data flow bit vector for all basic blocks.
398387
for (auto &BB : *F) {
399-
BlockStates[&BB] = new (BPA.Allocate()) RetainBlockState(&BB == &*F->begin(),
400-
RCRootVault.size(), MultiIteration);
388+
BlockStates[&BB] = new (BPA.Allocate())
389+
RetainBlockState(&BB == &*F->begin(),
390+
RCRootVault.size(), MultiIteration);
401391
}
402392
}
403393

@@ -981,11 +971,11 @@ void ReleaseCodeMotionContext::computeCodeMotionInsertPoints() {
981971
namespace {
982972

983973
/// Code motion kind.
984-
enum CMKind : unsigned { Retain = 0, Release = 1};
974+
enum CodeMotionKind : unsigned { Retain = 0, Release = 1};
985975

986976
class RRCodeMotion : public SILFunctionTransform {
987977
/// Whether to hoist releases or sink retains.
988-
CMKind Kind;
978+
CodeMotionKind Kind;
989979

990980
/// Freeze epilogue release or not.
991981
bool FreezeEpilogueReleases;
@@ -994,10 +984,11 @@ class RRCodeMotion : public SILFunctionTransform {
994984
StringRef getName() override { return "SIL Retain Release Code Motion"; }
995985

996986
/// Constructor.
997-
RRCodeMotion(CMKind H, bool F) : Kind(H), FreezeEpilogueReleases(F) {}
987+
RRCodeMotion(CodeMotionKind H, bool F) : Kind(H), FreezeEpilogueReleases(F) {}
998988

999989
/// The entry point to the transformation.
1000990
void run() override {
991+
// Code motion disabled.
1001992
if (DisableRRCodeMotion)
1002993
return;
1003994

@@ -1056,15 +1047,15 @@ class RRCodeMotion : public SILFunctionTransform {
10561047

10571048
/// Sink Retains.
10581049
SILTransform *swift::createRetainSinking() {
1059-
return new RRCodeMotion(CMKind::Retain, false);
1050+
return new RRCodeMotion(CodeMotionKind::Retain, false);
10601051
}
10611052

10621053
/// Hoist releases, but not epilogue release.
10631054
SILTransform *swift::createReleaseHoisting() {
1064-
return new RRCodeMotion(CMKind::Release, true);
1055+
return new RRCodeMotion(CodeMotionKind::Release, true);
10651056
}
10661057

10671058
/// Hoist all releases.
10681059
SILTransform *swift::createLateReleaseHoisting() {
1069-
return new RRCodeMotion(CMKind::Release, false);
1060+
return new RRCodeMotion(CodeMotionKind::Release, false);
10701061
}

lib/SILOptimizer/Transforms/SILCodeMotion.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ namespace {
4646
// Utility
4747
//===----------------------------------------------------------------------===//
4848

49-
static bool isReleaseInstruction(SILInstruction *I) {
50-
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
51-
}
52-
5349
static void createRefCountOpForPayload(SILBuilder &Builder, SILInstruction *I,
5450
EnumElementDecl *EnumDecl,
5551
SILValue DefOfEnum = SILValue()) {

0 commit comments

Comments
 (0)