Skip to content

Share a few createIncrement/createDecrement functions #2725

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
merged 1 commit into from
May 26, 2016
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions include/swift/SILOptimizer/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ inline ValueBaseUserRange makeUserRange(

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

/// \brief Create a retain of \p Ptr before the \p InsertPt.
SILInstruction *createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt);

/// \brief Create a release of \p Ptr before the \p InsertPt.
SILInstruction *createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt);

/// \brief For each of the given instructions, if they are dead delete them
/// along with their dead operands.
///
Expand Down
18 changes: 1 addition & 17 deletions lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ static std::string getUniqueName(std::string Name, SILModule &M) {
return getUniqueName(Name + "_unique_suffix", M);
}

/// Creates a decrement on \p Ptr at insertion point \p InsertPt that creates a
/// strong_release if \p Ptr has reference semantics itself or a release_value
/// if \p Ptr is a non-trivial value without reference-semantics.
static SILInstruction *createDecrement(SILValue Ptr, SILInstruction *InsertPt) {
// Setup the builder we will use to insert at our insertion point.
SILBuilder B(InsertPt);
auto Loc = RegularLocation(SourceLoc());

// If Ptr has reference semantics itself, create a strong_release.
if (Ptr->getType().isReferenceCounted(B.getModule()))
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);

// Otherwise create a release value.
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
}

//===----------------------------------------------------------------------===//
// Function Signature Transformation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -675,7 +659,7 @@ void FunctionSignatureTransform::OwnedToGuaranteedTransformFunctionResults() {
}
// Create a release to balance it out.
assert(isa<ApplyInst>(X) && "Unknown epilogue retain");
createDecrement(X, dyn_cast<ApplyInst>(X)->getParent()->getTerminator());
createDecrementBefore(X, dyn_cast<ApplyInst>(X)->getParent()->getTerminator());
}
}
}
Expand Down
35 changes: 0 additions & 35 deletions lib/SILOptimizer/Transforms/RetainReleaseCodeMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,6 @@ static bool isReleaseInstruction(SILInstruction *I) {
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
}

/// Creates an increment on \p Ptr before insertion point \p InsertPt that
/// creates a strong_retain if \p Ptr has reference semantics itself or a
/// retain_value if \p Ptr is a non-trivial value without reference-semantics.
static SILInstruction *
createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
// Set up the builder we use to insert at our insertion point.
SILBuilder B(InsertPt);
auto Loc = RegularLocation(SourceLoc());

// If Ptr is refcounted itself, create the strong_retain and
// return.
if (Ptr->getType().isReferenceCounted(B.getModule()))
return B.createStrongRetain(Loc, Ptr, Atomicity::Atomic);

// Otherwise, create the retain_value.
return B.createRetainValue(Loc, Ptr, Atomicity::Atomic);
}

/// Creates a decrement on \p Ptr before insertion point \p InsertPt that
/// creates a strong_release if \p Ptr has reference semantics itself or
/// a release_value if \p Ptr is a non-trivial value without reference-semantics.
static SILInstruction *
createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
// Setup the builder we will use to insert at our insertion point.
SILBuilder B(InsertPt);
auto Loc = RegularLocation(SourceLoc());

// If Ptr has reference semantics itself, create a strong_release.
if (Ptr->getType().isReferenceCounted(B.getModule()))
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);

// Otherwise create a release value.
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
}

//===----------------------------------------------------------------------===//
// Block State
//===----------------------------------------------------------------------===//
Expand Down
35 changes: 35 additions & 0 deletions lib/SILOptimizer/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@

using namespace swift;

/// Creates an increment on \p Ptr before insertion point \p InsertPt that
/// creates a strong_retain if \p Ptr has reference semantics itself or a
/// retain_value if \p Ptr is a non-trivial value without reference-semantics.
SILInstruction *
swift::createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
// Set up the builder we use to insert at our insertion point.
SILBuilder B(InsertPt);
auto Loc = RegularLocation(SourceLoc());

// If Ptr is refcounted itself, create the strong_retain and
// return.
if (Ptr->getType().isReferenceCounted(B.getModule()))
return B.createStrongRetain(Loc, Ptr, Atomicity::Atomic);

// Otherwise, create the retain_value.
return B.createRetainValue(Loc, Ptr, Atomicity::Atomic);
}

/// Creates a decrement on \p Ptr before insertion point \p InsertPt that
/// creates a strong_release if \p Ptr has reference semantics itself or
/// a release_value if \p Ptr is a non-trivial value without reference-semantics.
SILInstruction *
swift::createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
// Setup the builder we will use to insert at our insertion point.
SILBuilder B(InsertPt);
auto Loc = RegularLocation(SourceLoc());

// If Ptr has reference semantics itself, create a strong_release.
if (Ptr->getType().isReferenceCounted(B.getModule()))
return B.createStrongRelease(Loc, Ptr, Atomicity::Atomic);

// Otherwise create a release value.
return B.createReleaseValue(Loc, Ptr, Atomicity::Atomic);
}

/// \brief Perform a fast local check to see if the instruction is dead.
///
/// This routine only examines the state of the instruction at hand.
Expand Down