Skip to content

Vacation gardening #5920

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 6 commits into from
Nov 25, 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
22 changes: 9 additions & 13 deletions include/swift/SIL/SILArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,8 @@ class SILArgument : public ValueBase {

SILBasicBlock *ParentBB;
const ValueDecl *Decl;
public:
SILArgument(SILBasicBlock *ParentBB, SILType Ty, const ValueDecl *D=nullptr);
SILArgument(SILBasicBlock *ParentBB, SILBasicBlock::bbarg_iterator Pos,
SILType Ty, const ValueDecl *D=nullptr);

SILArgument(SILFunction::iterator ParentBB, SILType Ty,
const ValueDecl *D = nullptr)
: SILArgument(&*ParentBB, Ty, D) {}
SILArgument(SILFunction::iterator ParentBB, SILBasicBlock::bbarg_iterator Pos,
SILType Ty, const ValueDecl *D = nullptr)
: SILArgument(&*ParentBB, Pos, Ty, D) {}

public:
SILBasicBlock *getParent() { return ParentBB; }
const SILBasicBlock *getParent() const { return ParentBB; }

Expand All @@ -149,7 +139,7 @@ class SILArgument : public ValueBase {
}

unsigned getIndex() const {
ArrayRef<SILArgument *> Args = getParent()->getBBArgs();
ArrayRef<SILArgument *> Args = getParent()->getArguments();
for (unsigned i = 0, e = Args.size(); i != e; ++i)
if (Args[i] == this)
return i;
Expand Down Expand Up @@ -230,10 +220,16 @@ class SILArgument : public ValueBase {
}

private:
friend class SILBasicBlock;

SILArgument(SILBasicBlock *ParentBB, SILType Ty,
const ValueDecl *D = nullptr);
SILArgument(SILBasicBlock *ParentBB, SILBasicBlock::arg_iterator Pos,
SILType Ty, const ValueDecl *D = nullptr);

// A special constructor, only intended for use in SILBasicBlock::replaceBBArg.
explicit SILArgument(SILType Ty, const ValueDecl *D =nullptr) :
ValueBase(ValueKind::SILArgument, Ty), ParentBB(nullptr), Decl(D) {}
friend class SILBasicBlock;
void setParent(SILBasicBlock *P) { ParentBB = P; }
};

Expand Down
74 changes: 38 additions & 36 deletions include/swift/SIL/SILBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
/// automatically managed by the SILSuccessor class.
SILSuccessor *PredList;

/// BBArgList - This is the list of basic block arguments for this block.
std::vector<SILArgument*> BBArgList;
/// This is the list of basic block arguments for this block.
std::vector<SILArgument *> ArgumentList;

/// The ordered set of instructions in the SILBasicBlock.
InstListType InstList;

friend struct llvm::ilist_sentinel_traits<SILBasicBlock>;
friend struct llvm::ilist_traits<SILBasicBlock>;
SILBasicBlock() : Parent(0) {}
SILBasicBlock() : Parent(nullptr) {}
void operator=(const SILBasicBlock &) = delete;
void operator delete(void *Ptr, size_t) = delete;

public:
SILBasicBlock(SILFunction *F, SILBasicBlock *afterBB = nullptr);

public:
~SILBasicBlock();

/// Gets the ID (= index in the function's block list) of the block.
Expand Down Expand Up @@ -131,7 +132,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
/// Note that all the instructions BEFORE the specified iterator
/// stay as part of the original basic block. The old basic block is left
/// without a terminator.
SILBasicBlock *splitBasicBlock(iterator I);
SILBasicBlock *split(iterator I);

/// \brief Move the basic block to after the specified basic block in the IR.
/// The basic blocks must reside in the same function.
Expand All @@ -141,53 +142,47 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
// SILBasicBlock Argument List Inspection and Manipulation
//===--------------------------------------------------------------------===//

using bbarg_iterator = std::vector<SILArgument *>::iterator;
using const_bbarg_iterator = std::vector<SILArgument *>::const_iterator;
using arg_iterator = std::vector<SILArgument *>::iterator;
using const_arg_iterator = std::vector<SILArgument *>::const_iterator;

bool bbarg_empty() const { return BBArgList.empty(); }
size_t bbarg_size() const { return BBArgList.size(); }
bbarg_iterator bbarg_begin() { return BBArgList.begin(); }
bbarg_iterator bbarg_end() { return BBArgList.end(); }
const_bbarg_iterator bbarg_begin() const { return BBArgList.begin(); }
const_bbarg_iterator bbarg_end() const { return BBArgList.end(); }
bool args_empty() const { return ArgumentList.empty(); }
size_t args_size() const { return ArgumentList.size(); }
arg_iterator args_begin() { return ArgumentList.begin(); }
arg_iterator args_end() { return ArgumentList.end(); }
const_arg_iterator args_begin() const { return ArgumentList.begin(); }
const_arg_iterator args_end() const { return ArgumentList.end(); }

ArrayRef<SILArgument*> getBBArgs() const { return BBArgList; }
ArrayRef<SILArgument *> getArguments() const { return ArgumentList; }

unsigned getNumBBArg() const { return BBArgList.size(); }
const SILArgument *getBBArg(unsigned i) const { return BBArgList[i]; }
SILArgument *getBBArg(unsigned i) { return BBArgList[i]; }
unsigned getNumArguments() const { return ArgumentList.size(); }
const SILArgument *getArgument(unsigned i) const { return ArgumentList[i]; }
SILArgument *getArgument(unsigned i) { return ArgumentList[i]; }

/// Replace the \p{i}th BB arg with a new BBArg with SILType \p Ty and ValueDecl
/// \p D.
SILArgument *replaceBBArg(unsigned i, SILType Ty, const ValueDecl *D=nullptr);
SILArgument *replaceArgument(unsigned i, SILType Ty,
const ValueDecl *D = nullptr);

/// Erase a specific argument from the arg list.
void eraseBBArg(int Index);
void eraseArgument(int Index);

/// Allocate a new argument of type \p Ty and append it to the argument
/// list. Optionally you can pass in a value decl parameter.
SILArgument *createBBArg(SILType Ty, const ValueDecl *D=nullptr);
SILArgument *createArgument(SILType Ty, const ValueDecl *D = nullptr);

/// Insert a new SILArgument with type \p Ty and \p Decl at position \p Pos.
SILArgument *insertBBArg(bbarg_iterator Pos, SILType Ty,
const ValueDecl *D=nullptr);
SILArgument *insertArgument(arg_iterator Pos, SILType Ty,
const ValueDecl *D = nullptr);

SILArgument *insertBBArg(unsigned Index, SILType Ty,
const ValueDecl *D=nullptr) {
bbarg_iterator Pos = BBArgList.begin();
SILArgument *insertArgument(unsigned Index, SILType Ty,
const ValueDecl *D = nullptr) {
arg_iterator Pos = ArgumentList.begin();
std::advance(Pos, Index);
return insertBBArg(Pos, Ty, D);
return insertArgument(Pos, Ty, D);
}

/// \brief Remove all block arguments.
void dropAllBBArgs() { BBArgList.clear(); }

/// \brief Drops all uses that belong to this basic block.
void dropAllReferences() {
dropAllBBArgs();
for (SILInstruction &I : *this)
I.dropAllReferences();
}
void dropAllArguments() { ArgumentList.clear(); }

//===--------------------------------------------------------------------===//
// Predecessors and Successors
Expand Down Expand Up @@ -294,12 +289,19 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
return &SILBasicBlock::InstList;
}

/// \brief Drops all uses that belong to this basic block.
void dropAllReferences() {
dropAllArguments();
for (SILInstruction &I : *this)
I.dropAllReferences();
}

private:
friend class SILArgument;

/// BBArgument's ctor adds it to the argument list of this block.
void insertArgument(bbarg_iterator Iter, SILArgument *Arg) {
BBArgList.insert(Iter, Arg);
void insertArgument(arg_iterator Iter, SILArgument *Arg) {
ArgumentList.insert(Iter, Arg);
}
};

Expand Down
6 changes: 3 additions & 3 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,12 @@ SILCloner<ImplClass>::visitSILBasicBlock(SILBasicBlock* BB) {
// Only visit a successor that has not already been visited.
if (BBI == BBMap.end()) {
// Map the successor to a new BB.
auto MappedBB = new (F.getModule()) SILBasicBlock(&F);
auto *MappedBB = F.createBasicBlock();
BBMap.insert(std::make_pair(Succ.getBB(), MappedBB));
// Create new arguments for each of the original block's arguments.
for (auto &Arg : Succ.getBB()->getBBArgs()) {
for (auto &Arg : Succ.getBB()->getArguments()) {
SILValue MappedArg =
new (F.getModule()) SILArgument(MappedBB, getOpType(Arg->getType()));
MappedBB->createArgument(getOpType(Arg->getType()));

ValueMap.insert(std::make_pair(Arg, MappedArg));
}
Expand Down
15 changes: 8 additions & 7 deletions include/swift/SIL/SILFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class SILFunction
const SILBasicBlock &front() const { return *begin(); }

SILBasicBlock *createBasicBlock();
SILBasicBlock *createBasicBlock(SILBasicBlock *After);

/// Splice the body of \p F into this function at end.
void spliceBody(SILFunction *F) {
Expand Down Expand Up @@ -671,29 +672,29 @@ class SILFunction

SILArgument *getArgument(unsigned i) {
assert(!empty() && "Cannot get argument of a function without a body");
return begin()->getBBArg(i);
return begin()->getArgument(i);
}

const SILArgument *getArgument(unsigned i) const {
assert(!empty() && "Cannot get argument of a function without a body");
return begin()->getBBArg(i);
return begin()->getArgument(i);
}

ArrayRef<SILArgument *> getArguments() const {
assert(!empty() && "Cannot get arguments of a function without a body");
return begin()->getBBArgs();
return begin()->getArguments();
}

ArrayRef<SILArgument *> getIndirectResults() const {
assert(!empty() && "Cannot get arguments of a function without a body");
return begin()->getBBArgs().slice(0,
getLoweredFunctionType()->getNumIndirectResults());
return begin()->getArguments().slice(
0, getLoweredFunctionType()->getNumIndirectResults());
}

ArrayRef<SILArgument *> getArgumentsWithoutIndirectResults() const {
assert(!empty() && "Cannot get arguments of a function without a body");
return begin()->getBBArgs().slice(
getLoweredFunctionType()->getNumIndirectResults());
return begin()->getArguments().slice(
getLoweredFunctionType()->getNumIndirectResults());
}

const SILArgument *getSelfArgument() const {
Expand Down
3 changes: 1 addition & 2 deletions include/swift/SIL/SILVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class SILVisitor {
}

void visitBasicBlockArguments(SILBasicBlock *BB) {
for (auto argI = BB->bbarg_begin(), argEnd = BB->bbarg_end();
argI != argEnd;
for (auto argI = BB->args_begin(), argEnd = BB->args_end(); argI != argEnd;
++argI)
asImpl().visit(*argI);
}
Expand Down
21 changes: 11 additions & 10 deletions include/swift/SILOptimizer/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,17 @@ class EdgeThreadingCloner : public BaseThreadingCloner {
auto *Fn = BI->getFunction();
auto *SrcBB = BI->getParent();
auto *DestBB = BI->getDestBB();
auto *EdgeBB = new (Fn->getModule()) SILBasicBlock(Fn, SrcBB);
auto *EdgeBB = Fn->createBasicBlock(SrcBB);

// Create block arguments.
unsigned ArgIdx = 0;
for (auto Arg : BI->getArgs()) {
assert(Arg->getType() == DestBB->getBBArg(ArgIdx)->getType() &&
assert(Arg->getType() == DestBB->getArgument(ArgIdx)->getType() &&
"Types must match");
auto *BlockArg = EdgeBB->createBBArg(Arg->getType());
ValueMap[DestBB->getBBArg(ArgIdx)] = SILValue(BlockArg);
AvailVals.push_back(std::make_pair(DestBB->getBBArg(ArgIdx), BlockArg));
auto *BlockArg = EdgeBB->createArgument(Arg->getType());
ValueMap[DestBB->getArgument(ArgIdx)] = SILValue(BlockArg);
AvailVals.push_back(
std::make_pair(DestBB->getArgument(ArgIdx), BlockArg));
++ArgIdx;
}

Expand Down Expand Up @@ -406,18 +407,18 @@ class BasicBlockCloner : public BaseThreadingCloner {
// Create a new BB that is to be used as a target
// for cloning.
To = From->getParent()->createBasicBlock();
for (auto *Arg : FromBB->getBBArgs()) {
To->createBBArg(Arg->getType(), Arg->getDecl());
for (auto *Arg : FromBB->getArguments()) {
To->createArgument(Arg->getType(), Arg->getDecl());
}
}
DestBB = To;

// Populate the value map so that uses of the BBArgs in the SrcBB are
// replaced with the BBArgs of the DestBB.
for (unsigned i = 0, e = FromBB->bbarg_size(); i != e; ++i) {
ValueMap[FromBB->getBBArg(i)] = DestBB->getBBArg(i);
for (unsigned i = 0, e = FromBB->args_size(); i != e; ++i) {
ValueMap[FromBB->getArgument(i)] = DestBB->getArgument(i);
AvailVals.push_back(
std::make_pair(FromBB->getBBArg(i), DestBB->getBBArg(i)));
std::make_pair(FromBB->getArgument(i), DestBB->getArgument(i)));
}
}

Expand Down
23 changes: 12 additions & 11 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ emitPHINodesForBBArgs(IRGenSILFunction &IGF,
}
}

for (SILArgument *arg : make_range(silBB->bbarg_begin(), silBB->bbarg_end())) {
for (SILArgument *arg : make_range(silBB->args_begin(), silBB->args_end())) {
size_t first = phis.size();

const TypeInfo &ti = IGF.getTypeInfo(arg->getType());
Expand Down Expand Up @@ -1086,7 +1086,7 @@ static ArrayRef<SILArgument*> emitEntryPointIndirectReturn(
IGF.IndirectReturn = retTI.getAddressForPointer(params.claimNext());
}

auto bbargs = entry->getBBArgs();
auto bbargs = entry->getArguments();

// Map the indirect returns if present.
unsigned numIndirectResults = funcTy->getNumIndirectResults();
Expand Down Expand Up @@ -1289,11 +1289,12 @@ static void emitEntryPointArgumentsCOrObjC(IRGenSILFunction &IGF,
// Bind polymorphic arguments. This can only be done after binding
// all the value parameters, and must be done even for non-polymorphic
// functions because of imported Objective-C generics.
emitPolymorphicParameters(IGF, *IGF.CurSILFn, params, nullptr,
[&](unsigned paramIndex) -> llvm::Value* {
SILValue parameter = entry->getBBArgs()[paramIndex];
return IGF.getLoweredSingletonExplosion(parameter);
});
emitPolymorphicParameters(
IGF, *IGF.CurSILFn, params, nullptr,
[&](unsigned paramIndex) -> llvm::Value * {
SILValue parameter = entry->getArguments()[paramIndex];
return IGF.getLoweredSingletonExplosion(parameter);
});
}

/// Get metadata for the dynamic Self type if we have it.
Expand Down Expand Up @@ -2462,7 +2463,7 @@ static llvm::BasicBlock *emitBBMapForSwitchEnum(
//
// FIXME: This is cheesy when the destination BB has only the switch
// as a predecessor.
if (!casePair.second->bbarg_empty())
if (!casePair.second->args_empty())
dests.push_back({casePair.first,
llvm::BasicBlock::Create(IGF.IGM.getLLVMContext())});
else
Expand Down Expand Up @@ -2490,8 +2491,8 @@ void IRGenSILFunction::visitSwitchEnumInst(SwitchEnumInst *inst) {
// Bind arguments for cases that want them.
for (unsigned i = 0, e = inst->getNumCases(); i < e; ++i) {
auto casePair = inst->getCase(i);
if (!casePair.second->bbarg_empty()) {

if (!casePair.second->args_empty()) {
auto waypointBB = dests[i].second;
auto &destLBB = getLoweredBB(casePair.second);

Expand Down Expand Up @@ -2858,7 +2859,7 @@ void IRGenSILFunction::visitDynamicMethodBranchInst(DynamicMethodBranchInst *i){
&& "lowering dynamic_method_br with multiple preds for destination "
"not implemented");
// Kill the existing lowered value for the bb arg and its phi nodes.
SILValue methodArg = i->getHasMethodBB()->bbarg_begin()[0];
SILValue methodArg = i->getHasMethodBB()->args_begin()[0];
Explosion formerLLArg = getLoweredExplosion(methodArg);
for (llvm::Value *val : formerLLArg.claimAll()) {
auto phi = cast<llvm::PHINode>(val);
Expand Down
Loading