Skip to content

[ssa-updater] Modernize style before adding support for guaranteed parameters #33347

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
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
57 changes: 31 additions & 26 deletions include/swift/SILOptimizer/Utils/SILSSAUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
#include "swift/SIL/SILValue.h"

namespace llvm {
template<typename T> class SSAUpdaterTraits;
template<typename T> class SmallVectorImpl;
}

template <typename T>
class SSAUpdaterTraits;

template <typename T>
class SmallVectorImpl;

} // namespace llvm

namespace swift {

Expand All @@ -31,7 +36,7 @@ class SILUndef;

/// Independent utility that canonicalizes BB arguments by reusing structurally
/// equivalent arguments and replacing the original arguments with casts.
SILValue replaceBBArgWithCast(SILPhiArgument *Arg);
SILValue replaceBBArgWithCast(SILPhiArgument *arg);

/// This class updates SSA for a set of SIL instructions defined in multiple
/// blocks.
Expand All @@ -40,38 +45,38 @@ class SILSSAUpdater {

// A map of basic block to available phi value.
using AvailableValsTy = llvm::DenseMap<SILBasicBlock *, SILValue>;
std::unique_ptr<AvailableValsTy> AV;
std::unique_ptr<AvailableValsTy> blockToAvailableValueMap;

SILType ValType;
SILType type;

// The SSAUpdaterTraits specialization uses this sentinel to mark 'new' phi
// nodes (all the incoming edge arguments have this sentinel set).
std::unique_ptr<SILUndef, void(*)(SILUndef *)> PHISentinel;
std::unique_ptr<SILUndef, void (*)(SILUndef *)> phiSentinel;

// If not null updated with inserted 'phi' nodes (SILArgument).
SmallVectorImpl<SILPhiArgument *> *InsertedPHIs;
SmallVectorImpl<SILPhiArgument *> *insertedPhis;

// Not copyable.
void operator=(const SILSSAUpdater &) = delete;
SILSSAUpdater(const SILSSAUpdater &) = delete;

public:
explicit SILSSAUpdater(
SmallVectorImpl<SILPhiArgument *> *InsertedPHIs = nullptr);
SmallVectorImpl<SILPhiArgument *> *insertedPhis = nullptr);
~SILSSAUpdater();

void setInsertedPhis(SmallVectorImpl<SILPhiArgument *> *insertedPhis) {
InsertedPHIs = insertedPhis;
void setInsertedPhis(SmallVectorImpl<SILPhiArgument *> *inputInsertedPhis) {
insertedPhis = inputInsertedPhis;
}

/// Initialize for a use of a value of type.
void Initialize(SILType T);
void initialize(SILType type);

bool HasValueForBlock(SILBasicBlock *BB) const;
void AddAvailableValue(SILBasicBlock *BB, SILValue V);
bool hasValueForBlock(SILBasicBlock *block) const;
void addAvailableValue(SILBasicBlock *block, SILValue value);

/// Construct SSA for a value that is live at the *end* of a basic block.
SILValue GetValueAtEndOfBlock(SILBasicBlock *BB);
SILValue getValueAtEndOfBlock(SILBasicBlock *block);

/// Construct SSA for a value that is live in the middle of a block.
/// This handles the case where the use is before a definition of the value.
Expand All @@ -85,15 +90,15 @@ class SILSSAUpdater {
///
/// In this case we need to insert a 'PHI' node at the beginning of BB2
/// merging val_1 and val_2.
SILValue GetValueInMiddleOfBlock(SILBasicBlock *BB);
SILValue getValueInMiddleOfBlock(SILBasicBlock *block);

void RewriteUse(Operand &Op);
void rewriteUse(Operand &operand);

void *allocate(unsigned Size, unsigned Align) const;
static void deallocateSentinel(SILUndef *U);
private:
void *allocate(unsigned size, unsigned align) const;
static void deallocateSentinel(SILUndef *undef);

SILValue GetValueAtEndOfBlockInternal(SILBasicBlock *BB);
private:
SILValue getValueAtEndOfBlockInternal(SILBasicBlock *block);
};

/// Utility to wrap 'Operand's to deal with invalidation of
Expand All @@ -112,15 +117,15 @@ class SILSSAUpdater {
/// identify the use allowing us to reconstruct the use after the branch has
/// been changed.
class UseWrapper {
Operand *U;
SILBasicBlock *Parent;
Operand *wrappedUse;
SILBasicBlock *parent;
enum {
kRegularUse,
kBranchUse,
kCondBranchUseTrue,
kCondBranchUseFalse
} Type;
unsigned Idx;
} type;
unsigned index;

public:

Expand All @@ -131,7 +136,7 @@ class UseWrapper {
/// (ValueUseIterator) become invalid as they point to freed operands.
/// Instead we store the branch's parent and the idx so that we can
/// reconstruct the use.
UseWrapper(Operand *Use);
UseWrapper(Operand *use);

Operand *getOperand();

Expand Down
8 changes: 4 additions & 4 deletions lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@ class RegionCloner : public SILCloner<RegionCloner> {
return;

// Update SSA form.
SSAUp.Initialize(V->getType());
SSAUp.AddAvailableValue(OrigBB, V);
SSAUp.initialize(V->getType());
SSAUp.addAvailableValue(OrigBB, V);
SILValue NewVal = getMappedValue(V);
SSAUp.AddAvailableValue(getOpBasicBlock(OrigBB), NewVal);
SSAUp.addAvailableValue(getOpBasicBlock(OrigBB), NewVal);
for (auto U : UseList) {
Operand *Use = U;
SSAUp.RewriteUse(*Use);
SSAUp.rewriteUse(*Use);
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/SILOptimizer/LoopTransforms/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,8 @@ void LoopTreeOptimization::hoistLoadsAndStores(SILValue addr, SILLoop *loop, Ins
LLVM_DEBUG(llvm::dbgs() << "Creating preload " << *initialLoad);

SILSSAUpdater ssaUpdater;
ssaUpdater.Initialize(initialLoad->getType());
ssaUpdater.AddAvailableValue(preheader, initialLoad);
ssaUpdater.initialize(initialLoad->getType());
ssaUpdater.addAvailableValue(preheader, initialLoad);

// Set all stored values as available values in the ssaUpdater.
// If there are multiple stores in a block, only the last one counts.
Expand All @@ -1078,7 +1078,7 @@ void LoopTreeOptimization::hoistLoadsAndStores(SILValue addr, SILLoop *loop, Ins
if (isLoadFromAddr(dyn_cast<LoadInst>(SI->getSrc()), addr))
return;

ssaUpdater.AddAvailableValue(SI->getParent(), SI->getSrc());
ssaUpdater.addAvailableValue(SI->getParent(), SI->getSrc());
}
}

Expand All @@ -1099,7 +1099,7 @@ void LoopTreeOptimization::hoistLoadsAndStores(SILValue addr, SILLoop *loop, Ins
// If we didn't see a store in this block yet, get the current value from
// the ssaUpdater.
if (!currentVal)
currentVal = ssaUpdater.GetValueInMiddleOfBlock(block);
currentVal = ssaUpdater.getValueInMiddleOfBlock(block);
SILValue projectedValue = projectLoadValue(LI->getOperand(), addr,
currentVal, LI);
LLVM_DEBUG(llvm::dbgs() << "Replacing stored load " << *LI << " with "
Expand All @@ -1117,8 +1117,8 @@ void LoopTreeOptimization::hoistLoadsAndStores(SILValue addr, SILLoop *loop, Ins
"should have split critical edges");
SILBuilder B(succ->begin());
auto *SI = B.createStore(loc.getValue(),
ssaUpdater.GetValueInMiddleOfBlock(succ),
addr, StoreOwnershipQualifier::Unqualified);
ssaUpdater.getValueInMiddleOfBlock(succ), addr,
StoreOwnershipQualifier::Unqualified);
(void)SI;
LLVM_DEBUG(llvm::dbgs() << "Creating loop-exit store " << *SI);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/SILOptimizer/LoopTransforms/LoopRotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ static void updateSSAForUseOfValue(
assert(Res->getType() == MappedValue->getType() && "The types must match");

insertedPhis.clear();
updater.Initialize(Res->getType());
updater.AddAvailableValue(Header, Res);
updater.AddAvailableValue(EntryCheckBlock, MappedValue);
updater.initialize(Res->getType());
updater.addAvailableValue(Header, Res);
updater.addAvailableValue(EntryCheckBlock, MappedValue);

// Because of the way that phi nodes are represented we have to collect all
// uses before we update SSA. Modifying one phi node can invalidate another
Expand All @@ -155,7 +155,7 @@ static void updateSSAForUseOfValue(

assert(user->getParent() != EntryCheckBlock
&& "The entry check block should dominate the header");
updater.RewriteUse(*use);
updater.rewriteUse(*use);
}
// Canonicalize inserted phis to avoid extra BB Args.
for (SILPhiArgument *arg : insertedPhis) {
Expand Down
8 changes: 4 additions & 4 deletions lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ updateSSA(SILModule &M, SILLoop *Loop,
if (!Loop->contains(Use->getUser()->getParent()))
UseList.push_back(UseWrapper(Use));
// Update SSA of use with the available values.
SSAUp.Initialize(OrigValue->getType());
SSAUp.AddAvailableValue(OrigValue->getParentBlock(), OrigValue);
SSAUp.initialize(OrigValue->getType());
SSAUp.addAvailableValue(OrigValue->getParentBlock(), OrigValue);
for (auto NewValue : MapEntry.second)
SSAUp.AddAvailableValue(NewValue->getParentBlock(), NewValue);
SSAUp.addAvailableValue(NewValue->getParentBlock(), NewValue);
for (auto U : UseList) {
Operand *Use = U;
SSAUp.RewriteUse(*Use);
SSAUp.rewriteUse(*Use);
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
// lifetime respecting loops.
SmallVector<SILPhiArgument *, 8> insertedPhis;
SILSSAUpdater updater(&insertedPhis);
updater.Initialize(optionalEscapingClosureTy);
updater.initialize(optionalEscapingClosureTy);

// Create an Optional<() -> ()>.none in the entry block of the function and
// add it as an available value to the SSAUpdater.
Expand All @@ -256,7 +256,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
SILBuilderWithScope b(fn.getEntryBlock()->begin());
return b.createOptionalNone(loc, optionalEscapingClosureTy);
}();
updater.AddAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
updater.addAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);

// Create a copy of the convert_escape_to_no_escape and add it as an available
// value to the SSA updater.
Expand All @@ -270,7 +270,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
cvt->setLifetimeGuaranteed();
cvt->setOperand(innerCVI);
SILBuilderWithScope b(std::next(cvt->getIterator()));
updater.AddAvailableValue(
updater.addAvailableValue(
cvt->getParent(),
b.createOptionalSome(loc, innerCVI, optionalEscapingClosureTy));
return innerCVI;
Expand All @@ -284,13 +284,13 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
{
// Before the copy value, insert an extra destroy_value to handle
// loops. Since we used our enum value this is safe.
SILValue v = updater.GetValueInMiddleOfBlock(cvi->getParent());
SILValue v = updater.getValueInMiddleOfBlock(cvi->getParent());
SILBuilderWithScope(cvi).createDestroyValue(loc, v);
}

for (auto *block : exitingBlocks) {
auto *safeDestructionPt = getDeinitSafeClosureDestructionPoint(block);
SILValue v = updater.GetValueAtEndOfBlock(block);
SILValue v = updater.getValueAtEndOfBlock(block);
SILBuilderWithScope(safeDestructionPt).createDestroyValue(loc, v);
}

Expand Down Expand Up @@ -849,15 +849,15 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,

SmallVector<SILPhiArgument *, 8> insertedPhis;
SILSSAUpdater updater(&insertedPhis);
updater.Initialize(optionalEscapingClosureTy);
updater.initialize(optionalEscapingClosureTy);

// Create the Optional.none as the beginning available value.
SILValue entryBlockOptionalNone;
{
SILBuilderWithScope b(fn.getEntryBlock()->begin());
entryBlockOptionalNone =
b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.AddAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
updater.addAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
}
assert(entryBlockOptionalNone);

Expand All @@ -872,7 +872,7 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
// operand consumed at +1, so we don't need a copy) to it.
auto *result = b.createOptionalSome(autoGenLoc, sentinelClosure,
optionalEscapingClosureTy);
updater.AddAvailableValue(result->getParent(), result);
updater.addAvailableValue(result->getParent(), result);
return result;
}();

Expand All @@ -881,22 +881,22 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
auto *result = b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.AddAvailableValue(result->getParent(), result);
updater.addAvailableValue(result->getParent(), result);
}

// Now that we have all of our available values, insert a destroy_value before
// the initial Optional.some value using the SSA updater to ensure that we
// handle loops correctly.
{
SILValue v = updater.GetValueInMiddleOfBlock(initialValue->getParent());
SILValue v = updater.getValueInMiddleOfBlock(initialValue->getParent());
SILBuilderWithScope(initialValue).createDestroyValue(autoGenLoc, v);
}

// And insert an is_escaping_closure, cond_fail, destroy_value at each of the
// lifetime end points. This ensures we do not expand our lifetime too much.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
SILValue v = updater.GetValueInMiddleOfBlock(singleDestroy->getParent());
SILValue v = updater.getValueInMiddleOfBlock(singleDestroy->getParent());
SILValue isEscaping =
b.createIsEscapingClosure(loc, v, IsEscapingClosureInst::ObjCEscaping);
b.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
Expand All @@ -911,7 +911,7 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,

for (auto *block : exitingBlocks) {
auto *safeDestructionPt = getDeinitSafeClosureDestructionPoint(block);
SILValue v = updater.GetValueAtEndOfBlock(block);
SILValue v = updater.getValueAtEndOfBlock(block);
SILBuilderWithScope(safeDestructionPt).createDestroyValue(autoGenLoc, v);
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
// have multiple insertion points if we are storing exactly the same value
// implying that we can just copy firstVal at each insertion point.
SILSSAUpdater updater(&insertedPhiNodes);
updater.Initialize(loadTy);
updater.initialize(loadTy);

Optional<SILValue> singularValue;
for (auto *insertPt : insertPts) {
Expand All @@ -707,7 +707,7 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
}

// And then put the value into the SSA updater.
updater.AddAvailableValue(insertPt->getParent(), eltVal);
updater.addAvailableValue(insertPt->getParent(), eltVal);
}

// If we only are tracking a singular value, we do not need to construct
Expand All @@ -727,7 +727,7 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
}

// Finally, grab the value from the SSA updater.
SILValue result = updater.GetValueInMiddleOfBlock(B.getInsertionBB());
SILValue result = updater.getValueInMiddleOfBlock(B.getInsertionBB());
assert(result.getOwnershipKind().isCompatibleWith(ValueOwnershipKind::Owned));
if (isTake() || !B.hasOwnership()) {
return result;
Expand Down Expand Up @@ -863,7 +863,7 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
// never have the same value along all paths unless we have a trivial value
// meaning the SSA updater given a non-trivial value must /always/ be used.
SILSSAUpdater updater(&insertedPhiNodes);
updater.Initialize(loadTy);
updater.initialize(loadTy);

Optional<SILValue> singularValue;
for (auto *i : insertPts) {
Expand All @@ -881,7 +881,7 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
singularValue = SILValue();
}

updater.AddAvailableValue(i->getParent(), eltVal);
updater.addAvailableValue(i->getParent(), eltVal);
}

SILBasicBlock *insertBlock = B.getInsertionBB();
Expand All @@ -902,7 +902,7 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
}

// Finally, grab the value from the SSA updater.
SILValue eltVal = updater.GetValueInMiddleOfBlock(insertBlock);
SILValue eltVal = updater.getValueInMiddleOfBlock(insertBlock);
assert(!B.hasOwnership() ||
eltVal.getOwnershipKind().isCompatibleWith(ValueOwnershipKind::Owned));
assert(eltVal->getType() == loadTy && "Subelement types mismatch");
Expand Down
Loading