Skip to content

Small standardization gardening #8128

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 3 commits into from
Mar 15, 2017
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
160 changes: 81 additions & 79 deletions lib/SILGen/Cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Cleanup.h"
#include "SILGenFunction.h"

using namespace swift;
using namespace Lowering;

Expand All @@ -28,34 +29,34 @@ namespace {
/// A CleanupBuffer is a location to which to temporarily copy a
/// cleanup.
class CleanupBuffer {
SmallVector<char, sizeof(Cleanup) + 10 * sizeof(void*)> Data;
SmallVector<char, sizeof(Cleanup) + 10 * sizeof(void *)> data;

public:
CleanupBuffer(const Cleanup &cleanup) {
size_t size = cleanup.allocated_size();
Data.set_size(size);
memcpy(Data.data(), reinterpret_cast<const void*>(&cleanup), size);
data.set_size(size);
memcpy(data.data(), reinterpret_cast<const void *>(&cleanup), size);
}
Cleanup &getCopy() { return *reinterpret_cast<Cleanup*>(Data.data()); }

Cleanup &getCopy() { return *reinterpret_cast<Cleanup *>(data.data()); }
};
} // end anonymous namespace

void CleanupManager::popTopDeadCleanups(CleanupsDepth end) {
Stack.checkIterator(end);
stack.checkIterator(end);

while (Stack.stable_begin() != end && Stack.begin()->isDead()) {
assert(!Stack.empty());
Stack.pop();
Stack.checkIterator(end);
while (stack.stable_begin() != end && stack.begin()->isDead()) {
assert(!stack.empty());
stack.pop();
stack.checkIterator(end);
}
}

void CleanupManager::emitCleanups(CleanupsDepth depth, CleanupLocation l,
void CleanupManager::emitCleanups(CleanupsDepth depth, CleanupLocation loc,
bool popCleanups) {
auto begin = Stack.stable_begin();
auto begin = stack.stable_begin();
while (begin != depth) {
auto iter = Stack.find(begin);
auto iter = stack.find(begin);

Cleanup &stackCleanup = *iter;

Expand All @@ -65,78 +66,78 @@ void CleanupManager::emitCleanups(CleanupsDepth depth, CleanupLocation l,
Cleanup &cleanup = buffer.getCopy();

// Advance stable iterator.
begin = Stack.stabilize(++iter);
begin = stack.stabilize(++iter);

// Pop now.
if (popCleanups)
Stack.pop();
stack.pop();

if (cleanup.isActive() && Gen.B.hasValidInsertionPoint())
cleanup.emit(Gen, l);
if (cleanup.isActive() && SGF.B.hasValidInsertionPoint())
cleanup.emit(SGF, loc);

Stack.checkIterator(begin);
stack.checkIterator(begin);
}
}

/// Leave a scope, with all its cleanups.
void CleanupManager::endScope(CleanupsDepth depth, CleanupLocation l) {
Stack.checkIterator(depth);
void CleanupManager::endScope(CleanupsDepth depth, CleanupLocation loc) {
stack.checkIterator(depth);

// FIXME: Thread a branch through the cleanups if there are any active
// cleanups and we have a valid insertion point.
if (!::hasAnyActiveCleanups(Stack.begin(), Stack.find(depth))) {

if (!::hasAnyActiveCleanups(stack.begin(), stack.find(depth))) {
return;
}

// Iteratively mark cleanups dead and pop them.
// Maybe we'd get better results if we marked them all dead in one shot?
emitCleanups(depth, l);
emitCleanups(depth, loc);
}

bool CleanupManager::hasAnyActiveCleanups(CleanupsDepth from,
CleanupsDepth to) {
return ::hasAnyActiveCleanups(Stack.find(from), Stack.find(to));
return ::hasAnyActiveCleanups(stack.find(from), stack.find(to));
}

bool CleanupManager::hasAnyActiveCleanups(CleanupsDepth from) {
return ::hasAnyActiveCleanups(Stack.begin(), Stack.find(from));
return ::hasAnyActiveCleanups(stack.begin(), stack.find(from));
}

/// emitBranchAndCleanups - Emit a branch to the given jump destination,
/// threading out through any cleanups we might need to run. This does not
/// pop the cleanup stack.
void CleanupManager::emitBranchAndCleanups(JumpDest Dest,
SILLocation BranchLoc,
ArrayRef<SILValue> Args) {
SILGenBuilder &B = Gen.getBuilder();
assert(B.hasValidInsertionPoint() && "Emitting branch in invalid spot");
emitCleanups(Dest.getDepth(), Dest.getCleanupLocation(), /*popCleanups=*/false);
B.createBranch(BranchLoc, Dest.getBlock(), Args);
void CleanupManager::emitBranchAndCleanups(JumpDest dest, SILLocation branchLoc,
ArrayRef<SILValue> args) {
SILGenBuilder &builder = SGF.getBuilder();
assert(builder.hasValidInsertionPoint() && "Emitting branch in invalid spot");
emitCleanups(dest.getDepth(), dest.getCleanupLocation(),
/*popCleanups=*/false);
builder.createBranch(branchLoc, dest.getBlock(), args);
}

void CleanupManager::emitCleanupsForReturn(CleanupLocation Loc) {
SILGenBuilder &B = Gen.getBuilder();
assert(B.hasValidInsertionPoint() && "Emitting return in invalid spot");
(void) B;
emitCleanups(Stack.stable_end(), Loc, /*popCleanups=*/false);
void CleanupManager::emitCleanupsForReturn(CleanupLocation loc) {
SILGenBuilder &builder = SGF.getBuilder();
assert(builder.hasValidInsertionPoint() && "Emitting return in invalid spot");
(void)builder;
emitCleanups(stack.stable_end(), loc, /*popCleanups=*/false);
}

/// Emit a new block that jumps to the specified location and runs necessary
/// cleanups based on its level. If there are no cleanups to run, this just
/// returns the dest block.
SILBasicBlock *CleanupManager::emitBlockForCleanups(JumpDest Dest,
SILLocation BranchLoc,
ArrayRef<SILValue> Args) {
SILBasicBlock *CleanupManager::emitBlockForCleanups(JumpDest dest,
SILLocation branchLoc,
ArrayRef<SILValue> args) {
// If there are no cleanups to run, just return the Dest block directly.
if (!hasAnyActiveCleanups(Dest.getDepth()))
return Dest.getBlock();
if (!hasAnyActiveCleanups(dest.getDepth()))
return dest.getBlock();

// Otherwise, create and emit a new block.
auto *NewBB = Gen.createBasicBlock();
SavedInsertionPoint IPRAII(Gen, NewBB);
emitBranchAndCleanups(Dest, BranchLoc, Args);
return NewBB;
auto *newBlock = SGF.createBasicBlock();
SavedInsertionPoint IPRAII(SGF, newBlock);
emitBranchAndCleanups(dest, branchLoc, args);
return newBlock;
}


Expand All @@ -149,17 +150,17 @@ Cleanup &CleanupManager::initCleanup(Cleanup &cleanup,
}

void CleanupManager::setCleanupState(CleanupsDepth depth, CleanupState state) {
auto iter = Stack.find(depth);
assert(iter != Stack.end() && "can't change end of cleanups stack");
auto iter = stack.find(depth);
assert(iter != stack.end() && "can't change end of cleanups stack");
setCleanupState(*iter, state);
if (state == CleanupState::Dead && iter == Stack.begin())
popTopDeadCleanups(InnermostScope);

if (state == CleanupState::Dead && iter == stack.begin())
popTopDeadCleanups(innermostScope);
}

void CleanupManager::forwardCleanup(CleanupsDepth handle) {
auto iter = Stack.find(handle);
assert(iter != Stack.end() && "can't change end of cleanups stack");
auto iter = stack.find(handle);
assert(iter != stack.end() && "can't change end of cleanups stack");
Cleanup &cleanup = *iter;
assert(cleanup.isActive() && "forwarding inactive or dead cleanup?");

Expand All @@ -168,18 +169,18 @@ void CleanupManager::forwardCleanup(CleanupsDepth handle) {
: CleanupState::Dormant);
setCleanupState(cleanup, newState);

if (newState == CleanupState::Dead && iter == Stack.begin())
popTopDeadCleanups(InnermostScope);
if (newState == CleanupState::Dead && iter == stack.begin())
popTopDeadCleanups(innermostScope);
}

void CleanupManager::setCleanupState(Cleanup &cleanup, CleanupState state) {
assert(Gen.B.hasValidInsertionPoint() &&
assert(SGF.B.hasValidInsertionPoint() &&
"changing cleanup state at invalid IP");

// Do the transition now to avoid doing it in N places below.
CleanupState oldState = cleanup.getState();
(void)oldState;
cleanup.setState(Gen, state);
cleanup.setState(SGF, state);

assert(state != oldState && "trivial cleanup state change");
assert(oldState != CleanupState::Dead && "changing state of dead cleanup");
Expand All @@ -194,45 +195,46 @@ void CleanupStateRestorationScope::pushCleanupState(CleanupHandle handle,
// Don't put the cleanup in a state we can't restore it from.
assert(newState != CleanupState::Dead && "cannot restore cleanup from death");

auto iter = Cleanups.Stack.find(handle);
assert(iter != Cleanups.Stack.end() && "can't change end of cleanups stack");
auto iter = cleanups.stack.find(handle);
assert(iter != cleanups.stack.end() && "can't change end of cleanups stack");
Cleanup &cleanup = *iter;
assert(cleanup.getState() != CleanupState::Dead &&
"changing state of dead cleanup");

CleanupState oldState = cleanup.getState();
cleanup.setState(Cleanups.Gen, newState);
cleanup.setState(cleanups.SGF, newState);

SavedStates.push_back({handle, oldState});
savedStates.push_back({handle, oldState});
}

void
CleanupStateRestorationScope::pushCurrentCleanupState(CleanupHandle handle) {
auto iter = Cleanups.Stack.find(handle);
assert(iter != Cleanups.Stack.end() && "can't change end of cleanups stack");
auto iter = cleanups.stack.find(handle);
assert(iter != cleanups.stack.end() && "can't change end of cleanups stack");
Cleanup &cleanup = *iter;
assert(cleanup.getState() != CleanupState::Dead &&
"changing state of dead cleanup");

CleanupState oldState = cleanup.getState();
SavedStates.push_back({handle, oldState});
savedStates.push_back({handle, oldState});
}

void CleanupStateRestorationScope::popImpl() {
// Restore cleanup states in the opposite order in which we saved them.
for (auto i = SavedStates.rbegin(), e = SavedStates.rend(); i != e; ++i) {
for (auto i = savedStates.rbegin(), e = savedStates.rend(); i != e; ++i) {
CleanupHandle handle = i->first;
CleanupState stateToRestore = i->second;

auto iter = Cleanups.Stack.find(handle);
assert(iter != Cleanups.Stack.end() && "can't change end of cleanups stack");
auto iter = cleanups.stack.find(handle);
assert(iter != cleanups.stack.end() &&
"can't change end of cleanups stack");
Cleanup &cleanup = *iter;
assert(cleanup.getState() != CleanupState::Dead &&
"changing state of dead cleanup");
cleanup.setState(Cleanups.Gen, stateToRestore);
cleanup.setState(cleanups.SGF, stateToRestore);
}

SavedStates.clear();
savedStates.clear();
}

void CleanupStateRestorationScope::pop() && { popImpl(); }
Expand All @@ -255,22 +257,22 @@ llvm::raw_ostream &Lowering::operator<<(llvm::raw_ostream &os,

void CleanupManager::dump() const {
#ifndef NDEBUG
auto begin = Stack.stable_begin();
auto end = Stack.stable_end();
auto begin = stack.stable_begin();
auto end = stack.stable_end();
while (begin != end) {
auto iter = Stack.find(begin);
auto iter = stack.find(begin);
const Cleanup &stackCleanup = *iter;
llvm::errs() << "CLEANUP DEPTH: " << begin.getDepth() << "\n";
stackCleanup.dump(Gen);
begin = Stack.stabilize(++iter);
Stack.checkIterator(begin);
stackCleanup.dump(SGF);
begin = stack.stabilize(++iter);
stack.checkIterator(begin);
}
#endif
}

void CleanupManager::dump(CleanupHandle handle) const {
auto iter = Stack.find(handle);
auto iter = stack.find(handle);
const Cleanup &stackCleanup = *iter;
llvm::errs() << "CLEANUP DEPTH: " << handle.getDepth() << "\n";
stackCleanup.dump(Gen);
stackCleanup.dump(SGF);
}
Loading