Skip to content

Commit 2c083ca

Browse files
authored
Merge pull request swiftlang#35550 from eeckstein/tinyptrvector-for-args
SIL: Use TinyPtrVector for the argument list in SILBasicBlock
2 parents 45ce10a + 47c596d commit 2c083ca

File tree

8 files changed

+61
-88
lines changed

8 files changed

+61
-88
lines changed

include/swift/SIL/LinearLifetimeChecker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "swift/Basic/LLVM.h"
1818
#include "swift/SIL/SILArgument.h"
1919
#include "swift/SIL/SILInstruction.h"
20+
#include "swift/SIL/SILBasicBlock.h"
21+
#include "swift/SIL/SILFunction.h"
2022
#include "swift/SIL/SILValue.h"
2123
#include "llvm/ADT/SmallPtrSet.h"
2224

include/swift/SIL/OwnershipUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/Basic/LLVM.h"
1818
#include "swift/SIL/SILArgument.h"
1919
#include "swift/SIL/SILInstruction.h"
20+
#include "swift/SIL/SILBasicBlock.h"
2021
#include "swift/SIL/SILValue.h"
2122
#include "llvm/ADT/SmallPtrSet.h"
2223
#include "llvm/ADT/SmallVector.h"

include/swift/SIL/PatternMatch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define SWIFT_SIL_PATTERNMATCH_H
2323

2424
#include "swift/SIL/SILArgument.h"
25+
#include "swift/SIL/SILInstruction.h"
2526
#include "swift/SIL/SILUndef.h"
2627
namespace swift {
2728

include/swift/SIL/SILArgument.h

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
#include "swift/Basic/Compiler.h"
1717
#include "swift/SIL/SILArgumentConvention.h"
18-
#include "swift/SIL/SILFunction.h"
1918
#include "swift/SIL/SILValue.h"
19+
#include "swift/SIL/SILFunctionConventions.h"
2020

2121
namespace swift {
2222

2323
class SILBasicBlock;
2424
class SILModule;
2525
class SILUndef;
26+
class TermInst;
2627

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

75-
SILArgument(ValueKind subClassKind, SILBasicBlock *inputParentBlock,
76-
SILBasicBlock::arg_iterator positionInArgumentArray, SILType type,
77-
ValueOwnershipKind ownershipKind,
78-
const ValueDecl *inputDecl = nullptr);
79-
8076
// A special constructor, only intended for use in
8177
// SILBasicBlock::replacePHIArg and replaceFunctionArg.
8278
explicit SILArgument(ValueKind subClassKind, SILType type,
@@ -115,14 +111,7 @@ class SILArgument : public ValueBase {
115111
node->getKind() <= SILNodeKind::Last_SILArgument;
116112
}
117113

118-
unsigned getIndex() const {
119-
for (auto p : llvm::enumerate(getParent()->getArguments())) {
120-
if (p.value() == this) {
121-
return p.index();
122-
}
123-
}
124-
llvm_unreachable("SILArgument not argument of its parent BB");
125-
}
114+
unsigned getIndex() const;
126115

127116
/// Return true if this block argument is actually a phi argument as
128117
/// opposed to a cast or projection.
@@ -208,14 +197,6 @@ class SILPhiArgument : public SILArgument {
208197
const ValueDecl *decl = nullptr)
209198
: SILArgument(ValueKind::SILPhiArgument, parentBlock, type, ownershipKind,
210199
decl) {}
211-
212-
SILPhiArgument(SILBasicBlock *parentBlock,
213-
SILBasicBlock::arg_iterator argArrayInsertPt, SILType type,
214-
ValueOwnershipKind ownershipKind,
215-
const ValueDecl *decl = nullptr)
216-
: SILArgument(ValueKind::SILPhiArgument, parentBlock, argArrayInsertPt,
217-
type, ownershipKind, decl) {}
218-
219200
// A special constructor, only intended for use in
220201
// SILBasicBlock::replacePHIArg.
221202
explicit SILPhiArgument(SILType type, ValueOwnershipKind ownershipKind,
@@ -311,13 +292,6 @@ class SILFunctionArgument : public SILArgument {
311292
const ValueDecl *decl = nullptr)
312293
: SILArgument(ValueKind::SILFunctionArgument, parentBlock, type,
313294
ownershipKind, decl) {}
314-
SILFunctionArgument(SILBasicBlock *parentBlock,
315-
SILBasicBlock::arg_iterator argArrayInsertPt,
316-
SILType type, ValueOwnershipKind ownershipKind,
317-
const ValueDecl *decl = nullptr)
318-
: SILArgument(ValueKind::SILFunctionArgument, parentBlock,
319-
argArrayInsertPt, type, ownershipKind, decl) {}
320-
321295
// A special constructor, only intended for use in
322296
// SILBasicBlock::replaceFunctionArg.
323297
explicit SILFunctionArgument(SILType type, ValueOwnershipKind ownershipKind,
@@ -326,22 +300,14 @@ class SILFunctionArgument : public SILArgument {
326300
}
327301

328302
public:
329-
bool isIndirectResult() const {
330-
auto numIndirectResults =
331-
getFunction()->getConventions().getNumIndirectSILResults();
332-
return getIndex() < numIndirectResults;
333-
}
303+
bool isIndirectResult() const;
334304

335-
SILArgumentConvention getArgumentConvention() const {
336-
return getFunction()->getConventions().getSILArgumentConvention(getIndex());
337-
}
305+
SILArgumentConvention getArgumentConvention() const;
338306

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

346312
/// Returns true if this SILArgument is the self argument of its
347313
/// function. This means that this will return false always for SILArguments

include/swift/SIL/SILBasicBlock.h

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "swift/Basic/Range.h"
2222
#include "swift/SIL/SILArgumentArrayRef.h"
2323
#include "swift/SIL/SILInstruction.h"
24+
#include "swift/SIL/SILArgument.h"
25+
#include "llvm/ADT/TinyPtrVector.h"
2426

2527
namespace swift {
2628

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

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

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

180-
using arg_iterator = std::vector<SILArgument *>::iterator;
181-
using const_arg_iterator = std::vector<SILArgument *>::const_iterator;
184+
using arg_iterator = TinyPtrVector<SILArgument *>::iterator;
185+
using const_arg_iterator = TinyPtrVector<SILArgument *>::const_iterator;
182186

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

244-
SILFunctionArgument *insertFunctionArgument(unsigned Index, SILType Ty,
248+
SILFunctionArgument *insertFunctionArgument(unsigned AtArgPos, SILType Ty,
245249
ValueOwnershipKind OwnershipKind,
246-
const ValueDecl *D = nullptr) {
247-
arg_iterator Pos = ArgumentList.begin();
248-
std::advance(Pos, Index);
249-
return insertFunctionArgument(Pos, Ty, OwnershipKind, D);
250-
}
250+
const ValueDecl *D = nullptr);
251251

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

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

285-
SILPhiArgument *insertPhiArgument(unsigned Index, SILType Ty,
286-
ValueOwnershipKind Kind,
287-
const ValueDecl *D = nullptr) {
288-
arg_iterator Pos = ArgumentList.begin();
289-
std::advance(Pos, Index);
290-
return insertPhiArgument(Pos, Ty, Kind, D);
291-
}
292-
293285
/// Remove all block arguments.
294286
void dropAllArguments() { ArgumentList.clear(); }
295287

@@ -456,12 +448,6 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
456448
void insertArgument(arg_iterator Iter, SILArgument *Arg) {
457449
ArgumentList.insert(Iter, Arg);
458450
}
459-
460-
/// Insert a new SILFunctionArgument with type \p Ty and \p Decl at position
461-
/// \p Pos.
462-
SILFunctionArgument *insertFunctionArgument(arg_iterator Pos, SILType Ty,
463-
ValueOwnershipKind OwnershipKind,
464-
const ValueDecl *D = nullptr);
465451
};
466452

467453
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,

lib/SIL/IR/SILArgument.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,6 @@ SILArgument::SILArgument(ValueKind subClassKind,
3333
inputParentBlock->insertArgument(inputParentBlock->args_end(), this);
3434
}
3535

36-
SILArgument::SILArgument(ValueKind subClassKind,
37-
SILBasicBlock *inputParentBlock,
38-
SILBasicBlock::arg_iterator argArrayInsertPt,
39-
SILType type, ValueOwnershipKind ownershipKind,
40-
const ValueDecl *inputDecl)
41-
: ValueBase(subClassKind, type, IsRepresentative::Yes),
42-
parentBlock(inputParentBlock), decl(inputDecl) {
43-
Bits.SILArgument.VOKind = static_cast<unsigned>(ownershipKind);
44-
// Function arguments need to have a decl.
45-
assert(!inputParentBlock->getParent()->isBare() &&
46-
inputParentBlock->getParent()->size() == 1
47-
? decl != nullptr
48-
: true);
49-
inputParentBlock->insertArgument(argArrayInsertPt, this);
50-
}
51-
52-
5336
SILFunction *SILArgument::getFunction() {
5437
return getParent()->getParent();
5538
}
@@ -62,6 +45,33 @@ SILModule &SILArgument::getModule() const {
6245
return getFunction()->getModule();
6346
}
6447

48+
unsigned SILArgument::getIndex() const {
49+
for (auto p : llvm::enumerate(getParent()->getArguments())) {
50+
if (p.value() == this) {
51+
return p.index();
52+
}
53+
}
54+
llvm_unreachable("SILArgument not argument of its parent BB");
55+
}
56+
57+
bool SILFunctionArgument::isIndirectResult() const {
58+
auto numIndirectResults =
59+
getFunction()->getConventions().getNumIndirectSILResults();
60+
return getIndex() < numIndirectResults;
61+
}
62+
63+
SILArgumentConvention SILFunctionArgument::getArgumentConvention() const {
64+
return getFunction()->getConventions().getSILArgumentConvention(getIndex());
65+
}
66+
67+
/// Given that this is an entry block argument, and given that it does
68+
/// not correspond to an indirect result, return the corresponding
69+
/// SILParameterInfo.
70+
SILParameterInfo SILFunctionArgument::getKnownParameterInfo() const {
71+
return getFunction()->getConventions().getParamInfoForSILArg(getIndex());
72+
}
73+
74+
6575
//===----------------------------------------------------------------------===//
6676
// SILBlockArgument
6777
//===----------------------------------------------------------------------===//

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,15 @@ SILBasicBlock::createFunctionArgument(SILType Ty, const ValueDecl *D,
163163
return new (getModule()) SILFunctionArgument(this, Ty, OwnershipKind, D);
164164
}
165165

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

174177
SILFunctionArgument *SILBasicBlock::replaceFunctionArgument(
@@ -190,7 +193,7 @@ SILFunctionArgument *SILBasicBlock::replaceFunctionArgument(
190193

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

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

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

221224
return NewArg;
222225
}
@@ -255,13 +258,16 @@ SILPhiArgument *SILBasicBlock::createPhiArgument(SILType Ty,
255258
return new (getModule()) SILPhiArgument(this, Ty, Kind, D);
256259
}
257260

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

267273
void SILBasicBlock::eraseArgument(int Index) {

lib/SILOptimizer/ARC/RCStateTransition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "RCStateTransition.h"
1616
#include "swift/SIL/SILInstruction.h"
17+
#include "swift/SIL/SILFunction.h"
1718
#include "llvm/ADT/StringSwitch.h"
1819
#include "llvm/Support/Debug.h"
1920

0 commit comments

Comments
 (0)