Skip to content

Commit

Permalink
Merge pull request #35550 from eeckstein/tinyptrvector-for-args
Browse files Browse the repository at this point in the history
SIL: Use TinyPtrVector for the argument list in SILBasicBlock
  • Loading branch information
eeckstein authored Jan 22, 2021
2 parents 45ce10a + 47c596d commit 2c083ca
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 88 deletions.
2 changes: 2 additions & 0 deletions include/swift/SIL/LinearLifetimeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "swift/Basic/LLVM.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILValue.h"
#include "llvm/ADT/SmallPtrSet.h"

Expand Down
1 change: 1 addition & 0 deletions include/swift/SIL/OwnershipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/Basic/LLVM.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILValue.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
Expand Down
1 change: 1 addition & 0 deletions include/swift/SIL/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define SWIFT_SIL_PATTERNMATCH_H

#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILUndef.h"
namespace swift {

Expand Down
46 changes: 6 additions & 40 deletions include/swift/SIL/SILArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

#include "swift/Basic/Compiler.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILFunctionConventions.h"

namespace swift {

class SILBasicBlock;
class SILModule;
class SILUndef;
class TermInst;

// Map an argument index onto a SILArgumentConvention.
inline SILArgumentConvention
Expand Down Expand Up @@ -72,11 +73,6 @@ class SILArgument : public ValueBase {
SILType type, ValueOwnershipKind ownershipKind,
const ValueDecl *inputDecl = nullptr);

SILArgument(ValueKind subClassKind, SILBasicBlock *inputParentBlock,
SILBasicBlock::arg_iterator positionInArgumentArray, SILType type,
ValueOwnershipKind ownershipKind,
const ValueDecl *inputDecl = nullptr);

// A special constructor, only intended for use in
// SILBasicBlock::replacePHIArg and replaceFunctionArg.
explicit SILArgument(ValueKind subClassKind, SILType type,
Expand Down Expand Up @@ -115,14 +111,7 @@ class SILArgument : public ValueBase {
node->getKind() <= SILNodeKind::Last_SILArgument;
}

unsigned getIndex() const {
for (auto p : llvm::enumerate(getParent()->getArguments())) {
if (p.value() == this) {
return p.index();
}
}
llvm_unreachable("SILArgument not argument of its parent BB");
}
unsigned getIndex() const;

/// Return true if this block argument is actually a phi argument as
/// opposed to a cast or projection.
Expand Down Expand Up @@ -208,14 +197,6 @@ class SILPhiArgument : public SILArgument {
const ValueDecl *decl = nullptr)
: SILArgument(ValueKind::SILPhiArgument, parentBlock, type, ownershipKind,
decl) {}

SILPhiArgument(SILBasicBlock *parentBlock,
SILBasicBlock::arg_iterator argArrayInsertPt, SILType type,
ValueOwnershipKind ownershipKind,
const ValueDecl *decl = nullptr)
: SILArgument(ValueKind::SILPhiArgument, parentBlock, argArrayInsertPt,
type, ownershipKind, decl) {}

// A special constructor, only intended for use in
// SILBasicBlock::replacePHIArg.
explicit SILPhiArgument(SILType type, ValueOwnershipKind ownershipKind,
Expand Down Expand Up @@ -311,13 +292,6 @@ class SILFunctionArgument : public SILArgument {
const ValueDecl *decl = nullptr)
: SILArgument(ValueKind::SILFunctionArgument, parentBlock, type,
ownershipKind, decl) {}
SILFunctionArgument(SILBasicBlock *parentBlock,
SILBasicBlock::arg_iterator argArrayInsertPt,
SILType type, ValueOwnershipKind ownershipKind,
const ValueDecl *decl = nullptr)
: SILArgument(ValueKind::SILFunctionArgument, parentBlock,
argArrayInsertPt, type, ownershipKind, decl) {}

// A special constructor, only intended for use in
// SILBasicBlock::replaceFunctionArg.
explicit SILFunctionArgument(SILType type, ValueOwnershipKind ownershipKind,
Expand All @@ -326,22 +300,14 @@ class SILFunctionArgument : public SILArgument {
}

public:
bool isIndirectResult() const {
auto numIndirectResults =
getFunction()->getConventions().getNumIndirectSILResults();
return getIndex() < numIndirectResults;
}
bool isIndirectResult() const;

SILArgumentConvention getArgumentConvention() const {
return getFunction()->getConventions().getSILArgumentConvention(getIndex());
}
SILArgumentConvention getArgumentConvention() const;

/// Given that this is an entry block argument, and given that it does
/// not correspond to an indirect result, return the corresponding
/// SILParameterInfo.
SILParameterInfo getKnownParameterInfo() const {
return getFunction()->getConventions().getParamInfoForSILArg(getIndex());
}
SILParameterInfo getKnownParameterInfo() const;

/// Returns true if this SILArgument is the self argument of its
/// function. This means that this will return false always for SILArguments
Expand Down
36 changes: 11 additions & 25 deletions include/swift/SIL/SILBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "swift/Basic/Range.h"
#include "swift/SIL/SILArgumentArrayRef.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILArgument.h"
#include "llvm/ADT/TinyPtrVector.h"

namespace swift {

Expand Down Expand Up @@ -48,7 +50,9 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
SILSuccessor *PredList;

/// This is the list of basic block arguments for this block.
std::vector<SILArgument *> ArgumentList;
/// A TinyPtrVector is the right choice, because ~98% of blocks have 0 or 1
/// arguments.
TinyPtrVector<SILArgument *> ArgumentList;

/// The ordered set of instructions in the SILBasicBlock.
InstListType InstList;
Expand Down Expand Up @@ -177,8 +181,8 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
// SILBasicBlock Argument List Inspection and Manipulation
//===--------------------------------------------------------------------===//

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

bool args_empty() const { return ArgumentList.empty(); }
size_t args_size() const { return ArgumentList.size(); }
Expand Down Expand Up @@ -241,13 +245,9 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
const ValueDecl *D = nullptr,
bool disableEntryBlockVerification = false);

SILFunctionArgument *insertFunctionArgument(unsigned Index, SILType Ty,
SILFunctionArgument *insertFunctionArgument(unsigned AtArgPos, SILType Ty,
ValueOwnershipKind OwnershipKind,
const ValueDecl *D = nullptr) {
arg_iterator Pos = ArgumentList.begin();
std::advance(Pos, Index);
return insertFunctionArgument(Pos, Ty, OwnershipKind, D);
}
const ValueDecl *D = nullptr);

/// Replace the \p{i}th Function arg with a new Function arg with SILType \p
/// Ty and ValueDecl \p D.
Expand Down Expand Up @@ -277,19 +277,11 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
const ValueDecl *D = nullptr);

/// Insert a new SILPhiArgument with type \p Ty and \p Decl at position \p
/// Pos.
SILPhiArgument *insertPhiArgument(arg_iterator Pos, SILType Ty,
/// AtArgPos.
SILPhiArgument *insertPhiArgument(unsigned AtArgPos, SILType Ty,
ValueOwnershipKind Kind,
const ValueDecl *D = nullptr);

SILPhiArgument *insertPhiArgument(unsigned Index, SILType Ty,
ValueOwnershipKind Kind,
const ValueDecl *D = nullptr) {
arg_iterator Pos = ArgumentList.begin();
std::advance(Pos, Index);
return insertPhiArgument(Pos, Ty, Kind, D);
}

/// Remove all block arguments.
void dropAllArguments() { ArgumentList.clear(); }

Expand Down Expand Up @@ -456,12 +448,6 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
void insertArgument(arg_iterator Iter, SILArgument *Arg) {
ArgumentList.insert(Iter, Arg);
}

/// Insert a new SILFunctionArgument with type \p Ty and \p Decl at position
/// \p Pos.
SILFunctionArgument *insertFunctionArgument(arg_iterator Pos, SILType Ty,
ValueOwnershipKind OwnershipKind,
const ValueDecl *D = nullptr);
};

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
Expand Down
44 changes: 27 additions & 17 deletions lib/SIL/IR/SILArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@ SILArgument::SILArgument(ValueKind subClassKind,
inputParentBlock->insertArgument(inputParentBlock->args_end(), this);
}

SILArgument::SILArgument(ValueKind subClassKind,
SILBasicBlock *inputParentBlock,
SILBasicBlock::arg_iterator argArrayInsertPt,
SILType type, ValueOwnershipKind ownershipKind,
const ValueDecl *inputDecl)
: ValueBase(subClassKind, type, IsRepresentative::Yes),
parentBlock(inputParentBlock), decl(inputDecl) {
Bits.SILArgument.VOKind = static_cast<unsigned>(ownershipKind);
// Function arguments need to have a decl.
assert(!inputParentBlock->getParent()->isBare() &&
inputParentBlock->getParent()->size() == 1
? decl != nullptr
: true);
inputParentBlock->insertArgument(argArrayInsertPt, this);
}


SILFunction *SILArgument::getFunction() {
return getParent()->getParent();
}
Expand All @@ -62,6 +45,33 @@ SILModule &SILArgument::getModule() const {
return getFunction()->getModule();
}

unsigned SILArgument::getIndex() const {
for (auto p : llvm::enumerate(getParent()->getArguments())) {
if (p.value() == this) {
return p.index();
}
}
llvm_unreachable("SILArgument not argument of its parent BB");
}

bool SILFunctionArgument::isIndirectResult() const {
auto numIndirectResults =
getFunction()->getConventions().getNumIndirectSILResults();
return getIndex() < numIndirectResults;
}

SILArgumentConvention SILFunctionArgument::getArgumentConvention() const {
return getFunction()->getConventions().getSILArgumentConvention(getIndex());
}

/// Given that this is an entry block argument, and given that it does
/// not correspond to an indirect result, return the corresponding
/// SILParameterInfo.
SILParameterInfo SILFunctionArgument::getKnownParameterInfo() const {
return getFunction()->getConventions().getParamInfoForSILArg(getIndex());
}


//===----------------------------------------------------------------------===//
// SILBlockArgument
//===----------------------------------------------------------------------===//
Expand Down
18 changes: 12 additions & 6 deletions lib/SIL/IR/SILBasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ SILBasicBlock::createFunctionArgument(SILType Ty, const ValueDecl *D,
return new (getModule()) SILFunctionArgument(this, Ty, OwnershipKind, D);
}

SILFunctionArgument *SILBasicBlock::insertFunctionArgument(arg_iterator Iter,
SILFunctionArgument *SILBasicBlock::insertFunctionArgument(unsigned AtArgPos,
SILType Ty,
ValueOwnershipKind OwnershipKind,
const ValueDecl *D) {
assert(isEntry() && "Function Arguments can only be in the entry block");
return new (getModule()) SILFunctionArgument(this, Iter, Ty, OwnershipKind, D);
auto *arg = new (getModule()) SILFunctionArgument(Ty, OwnershipKind, D);
arg->parentBlock = this;
insertArgument(ArgumentList.begin() + AtArgPos, arg);
return arg;
}

SILFunctionArgument *SILBasicBlock::replaceFunctionArgument(
Expand All @@ -190,7 +193,7 @@ SILFunctionArgument *SILBasicBlock::replaceFunctionArgument(

// TODO: When we switch to malloc/free allocation we'll be leaking memory
// here.
ArgumentList[i] = NewArg;
*(ArgumentList.begin() + i) = NewArg;

return NewArg;
}
Expand All @@ -216,7 +219,7 @@ SILPhiArgument *SILBasicBlock::replacePhiArgument(unsigned i, SILType Ty,

// TODO: When we switch to malloc/free allocation we'll be leaking memory
// here.
ArgumentList[i] = NewArg;
*(ArgumentList.begin() + i) = NewArg;

return NewArg;
}
Expand Down Expand Up @@ -255,13 +258,16 @@ SILPhiArgument *SILBasicBlock::createPhiArgument(SILType Ty,
return new (getModule()) SILPhiArgument(this, Ty, Kind, D);
}

SILPhiArgument *SILBasicBlock::insertPhiArgument(arg_iterator Iter, SILType Ty,
SILPhiArgument *SILBasicBlock::insertPhiArgument(unsigned AtArgPos, SILType Ty,
ValueOwnershipKind Kind,
const ValueDecl *D) {
assert(!isEntry() && "PHI Arguments can not be in the entry block");
if (Ty.isTrivial(*getParent()))
Kind = OwnershipKind::None;
return new (getModule()) SILPhiArgument(this, Iter, Ty, Kind, D);
auto *arg = new (getModule()) SILPhiArgument(Ty, Kind, D);
arg->parentBlock = this;
insertArgument(ArgumentList.begin() + AtArgPos, arg);
return arg;
}

void SILBasicBlock::eraseArgument(int Index) {
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/ARC/RCStateTransition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "RCStateTransition.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILFunction.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Debug.h"

Expand Down

0 comments on commit 2c083ca

Please sign in to comment.