Skip to content

Commit c758848

Browse files
committed
Share a few createIncrement/createDecrement functions
1 parent d8e11f5 commit c758848

File tree

4 files changed

+42
-52
lines changed

4 files changed

+42
-52
lines changed

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ inline ValueBaseUserRange makeUserRange(
4242

4343
using DeadInstructionSet = llvm::SmallSetVector<SILInstruction *, 8>;
4444

45+
/// \brief Create a retain of \p Ptr before the \p InsertPt.
46+
SILInstruction *createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt);
47+
48+
/// \brief Create a release of \p Ptr before the \p InsertPt.
49+
SILInstruction *createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt);
50+
4551
/// \brief For each of the given instructions, if they are dead delete them
4652
/// along with their dead operands.
4753
///

lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,6 @@ static std::string getUniqueName(std::string Name, SILModule &M) {
8888
return getUniqueName(Name + "_unique_suffix", M);
8989
}
9090

91-
/// Creates a decrement on \p Ptr at insertion point \p InsertPt that creates a
92-
/// strong_release if \p Ptr has reference semantics itself or a release_value
93-
/// if \p Ptr is a non-trivial value without reference-semantics.
94-
static SILInstruction *createDecrement(SILValue Ptr, SILInstruction *InsertPt) {
95-
// Setup the builder we will use to insert at our insertion point.
96-
SILBuilder B(InsertPt);
97-
auto Loc = RegularLocation(SourceLoc());
98-
99-
// If Ptr has reference semantics itself, create a strong_release.
100-
if (Ptr->getType().isReferenceCounted(B.getModule()))
101-
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);
102-
103-
// Otherwise create a release value.
104-
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
105-
}
106-
10791
//===----------------------------------------------------------------------===//
10892
// Function Signature Transformation
10993
//===----------------------------------------------------------------------===//
@@ -675,7 +659,7 @@ void FunctionSignatureTransform::OwnedToGuaranteedTransformFunctionResults() {
675659
}
676660
// Create a release to balance it out.
677661
assert(isa<ApplyInst>(X) && "Unknown epilogue retain");
678-
createDecrement(X, dyn_cast<ApplyInst>(X)->getParent()->getTerminator());
662+
createDecrementBefore(X, dyn_cast<ApplyInst>(X)->getParent()->getTerminator());
679663
}
680664
}
681665
}

lib/SILOptimizer/Transforms/RetainReleaseCodeMotion.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,41 +107,6 @@ static bool isReleaseInstruction(SILInstruction *I) {
107107
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
108108
}
109109

110-
/// Creates an increment on \p Ptr before insertion point \p InsertPt that
111-
/// creates a strong_retain if \p Ptr has reference semantics itself or a
112-
/// retain_value if \p Ptr is a non-trivial value without reference-semantics.
113-
static SILInstruction *
114-
createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
115-
// Set up the builder we use to insert at our insertion point.
116-
SILBuilder B(InsertPt);
117-
auto Loc = RegularLocation(SourceLoc());
118-
119-
// If Ptr is refcounted itself, create the strong_retain and
120-
// return.
121-
if (Ptr->getType().isReferenceCounted(B.getModule()))
122-
return B.createStrongRetain(Loc, Ptr, Atomicity::Atomic);
123-
124-
// Otherwise, create the retain_value.
125-
return B.createRetainValue(Loc, Ptr, Atomicity::Atomic);
126-
}
127-
128-
/// Creates a decrement on \p Ptr before insertion point \p InsertPt that
129-
/// creates a strong_release if \p Ptr has reference semantics itself or
130-
/// a release_value if \p Ptr is a non-trivial value without reference-semantics.
131-
static SILInstruction *
132-
createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
133-
// Setup the builder we will use to insert at our insertion point.
134-
SILBuilder B(InsertPt);
135-
auto Loc = RegularLocation(SourceLoc());
136-
137-
// If Ptr has reference semantics itself, create a strong_release.
138-
if (Ptr->getType().isReferenceCounted(B.getModule()))
139-
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);
140-
141-
// Otherwise create a release value.
142-
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
143-
}
144-
145110
//===----------------------------------------------------------------------===//
146111
// Block State
147112
//===----------------------------------------------------------------------===//

lib/SILOptimizer/Utils/Local.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,41 @@
3131

3232
using namespace swift;
3333

34+
/// Creates an increment on \p Ptr before insertion point \p InsertPt that
35+
/// creates a strong_retain if \p Ptr has reference semantics itself or a
36+
/// retain_value if \p Ptr is a non-trivial value without reference-semantics.
37+
SILInstruction *
38+
swift::createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
39+
// Set up the builder we use to insert at our insertion point.
40+
SILBuilder B(InsertPt);
41+
auto Loc = RegularLocation(SourceLoc());
42+
43+
// If Ptr is refcounted itself, create the strong_retain and
44+
// return.
45+
if (Ptr->getType().isReferenceCounted(B.getModule()))
46+
return B.createStrongRetain(Loc, Ptr, Atomicity::Atomic);
47+
48+
// Otherwise, create the retain_value.
49+
return B.createRetainValue(Loc, Ptr, Atomicity::Atomic);
50+
}
51+
52+
/// Creates a decrement on \p Ptr before insertion point \p InsertPt that
53+
/// creates a strong_release if \p Ptr has reference semantics itself or
54+
/// a release_value if \p Ptr is a non-trivial value without reference-semantics.
55+
SILInstruction *
56+
swift::createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
57+
// Setup the builder we will use to insert at our insertion point.
58+
SILBuilder B(InsertPt);
59+
auto Loc = RegularLocation(SourceLoc());
60+
61+
// If Ptr has reference semantics itself, create a strong_release.
62+
if (Ptr->getType().isReferenceCounted(B.getModule()))
63+
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);
64+
65+
// Otherwise create a release value.
66+
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
67+
}
68+
3469
/// \brief Perform a fast local check to see if the instruction is dead.
3570
///
3671
/// This routine only examines the state of the instruction at hand.

0 commit comments

Comments
 (0)