Skip to content

Commit 7825d17

Browse files
committed
[NFC] Introduce NullaryInstructionWithTypeDependentOperandsBase
We have a similar helper class for the unary-but-for case, might as well have one for the nullary-but-for case.
1 parent 0e3a9ad commit 7825d17

File tree

2 files changed

+70
-64
lines changed

2 files changed

+70
-64
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,49 @@ class InstructionBaseWithTrailingOperands
18231823
}
18241824
};
18251825

1826+
/// A template base class for instructions that take no operands except
1827+
/// for type-dependent operands. The operands are tail allocated after the
1828+
/// instruction. Further trailing data can be allocated as well if
1829+
/// TRAILING_TYPES are provided.
1830+
template<SILInstructionKind Kind,
1831+
typename Derived,
1832+
typename Base,
1833+
typename... OtherTrailingTypes>
1834+
class NullaryInstructionWithTypeDependentOperandsBase
1835+
: public InstructionBaseWithTrailingOperands<Kind, Derived, Base,
1836+
OtherTrailingTypes...> {
1837+
protected:
1838+
friend InstructionBaseWithTrailingOperands<Kind, Derived, Operand,
1839+
OtherTrailingTypes...>;
1840+
1841+
using TrailingObjects =
1842+
InstructionBaseWithTrailingOperands<Kind, Derived, Operand,
1843+
OtherTrailingTypes...>;
1844+
1845+
template <typename... Args>
1846+
NullaryInstructionWithTypeDependentOperandsBase(SILDebugLocation debugLoc,
1847+
ArrayRef<SILValue> typeDependentOperands,
1848+
Args &&...args)
1849+
: InstructionBaseWithTrailingOperands<Kind, Derived, Base,
1850+
OtherTrailingTypes...>(
1851+
typeDependentOperands,
1852+
debugLoc,
1853+
std::forward<Args>(args)...) {}
1854+
1855+
public:
1856+
unsigned getNumTypeDependentOperands() const {
1857+
return this->getAllOperands().size();
1858+
}
1859+
1860+
ArrayRef<Operand> getTypeDependentOperands() const {
1861+
return this->getAllOperands();
1862+
}
1863+
1864+
MutableArrayRef<Operand> getTypeDependentOperands() {
1865+
return this->getAllOperands();
1866+
}
1867+
};
1868+
18261869
/// A template base class for instructions that take a single regular SILValue
18271870
/// operand, a set of type dependent operands and has no result
18281871
/// or a single value result. The operands are tail allocated after the
@@ -2351,7 +2394,7 @@ class AllocRefDynamicInst final
23512394
/// is an address pointing to the contained element. The contained
23522395
/// element is uninitialized.
23532396
class AllocBoxInst final
2354-
: public InstructionBaseWithTrailingOperands<
2397+
: public NullaryInstructionWithTypeDependentOperandsBase<
23552398
SILInstructionKind::AllocBoxInst,
23562399
AllocBoxInst, AllocationInst, char>
23572400
{
@@ -2392,14 +2435,6 @@ class AllocBoxInst final
23922435
Optional<SILDebugVariable> getVarInfo() const {
23932436
return VarInfo.get(getDecl(), getTrailingObjects<char>());
23942437
};
2395-
2396-
ArrayRef<Operand> getTypeDependentOperands() const {
2397-
return getAllOperands();
2398-
}
2399-
2400-
MutableArrayRef<Operand> getTypeDependentOperands() {
2401-
return getAllOperands();
2402-
}
24032438
};
24042439

24052440
/// This represents the allocation of a heap box for an existential container.
@@ -2408,7 +2443,7 @@ class AllocBoxInst final
24082443
/// is an address pointing to the contained element. The contained
24092444
/// value is uninitialized.
24102445
class AllocExistentialBoxInst final
2411-
: public InstructionBaseWithTrailingOperands<
2446+
: public NullaryInstructionWithTypeDependentOperandsBase<
24122447
SILInstructionKind::AllocExistentialBoxInst,
24132448
AllocExistentialBoxInst, AllocationInst> {
24142449
friend SILBuilder;
@@ -2420,7 +2455,8 @@ class AllocExistentialBoxInst final
24202455
ArrayRef<ProtocolConformanceRef> Conformances,
24212456
ArrayRef<SILValue> TypeDependentOperands,
24222457
SILFunction *Parent)
2423-
: InstructionBaseWithTrailingOperands(TypeDependentOperands, DebugLoc,
2458+
: NullaryInstructionWithTypeDependentOperandsBase(DebugLoc,
2459+
TypeDependentOperands,
24242460
ExistentialType.getObjectType()),
24252461
ConcreteType(ConcreteType), Conformances(Conformances) {}
24262462

@@ -2437,14 +2473,6 @@ class AllocExistentialBoxInst final
24372473
ArrayRef<ProtocolConformanceRef> getConformances() const {
24382474
return Conformances;
24392475
}
2440-
2441-
ArrayRef<Operand> getTypeDependentOperands() const {
2442-
return getAllOperands();
2443-
}
2444-
2445-
MutableArrayRef<Operand> getTypeDependentOperands() {
2446-
return getAllOperands();
2447-
}
24482476
};
24492477

24502478
/// GenericSpecializationInformation - provides information about a generic
@@ -6660,28 +6688,19 @@ class SelectValueInst final
66606688
/// MetatypeInst - Represents the production of an instance of a given metatype
66616689
/// named statically.
66626690
class MetatypeInst final
6663-
: public InstructionBaseWithTrailingOperands<
6691+
: public NullaryInstructionWithTypeDependentOperandsBase<
66646692
SILInstructionKind::MetatypeInst,
66656693
MetatypeInst, SingleValueInstruction> {
66666694
friend SILBuilder;
66676695

66686696
/// Constructs a MetatypeInst
66696697
MetatypeInst(SILDebugLocation DebugLoc, SILType Metatype,
66706698
ArrayRef<SILValue> TypeDependentOperands)
6671-
: InstructionBaseWithTrailingOperands(TypeDependentOperands, DebugLoc,
6672-
Metatype) {}
6699+
: NullaryInstructionWithTypeDependentOperandsBase(DebugLoc,
6700+
TypeDependentOperands, Metatype) {}
66736701

66746702
static MetatypeInst *create(SILDebugLocation DebugLoc, SILType Metatype,
66756703
SILFunction *F);
6676-
6677-
public:
6678-
ArrayRef<Operand> getTypeDependentOperands() const {
6679-
return getAllOperands();
6680-
}
6681-
6682-
MutableArrayRef<Operand> getTypeDependentOperands() {
6683-
return getAllOperands();
6684-
}
66856704
};
66866705

66876706
/// Represents loading a dynamic metatype from a value.
@@ -7029,7 +7048,7 @@ class ObjCSuperMethodInst
70297048
/// and a protocol method constant, extracts the implementation of that method
70307049
/// for the type.
70317050
class WitnessMethodInst final
7032-
: public InstructionBaseWithTrailingOperands<
7051+
: public NullaryInstructionWithTypeDependentOperandsBase<
70337052
SILInstructionKind::WitnessMethodInst,
70347053
WitnessMethodInst, MethodInst> {
70357054
friend SILBuilder;
@@ -7040,8 +7059,8 @@ class WitnessMethodInst final
70407059
WitnessMethodInst(SILDebugLocation DebugLoc, CanType LookupType,
70417060
ProtocolConformanceRef Conformance, SILDeclRef Member,
70427061
SILType Ty, ArrayRef<SILValue> TypeDependentOperands)
7043-
: InstructionBaseWithTrailingOperands(TypeDependentOperands,
7044-
DebugLoc, Ty, Member),
7062+
: NullaryInstructionWithTypeDependentOperandsBase(DebugLoc,
7063+
TypeDependentOperands, Ty, Member),
70457064
LookupType(LookupType), Conformance(Conformance) {}
70467065

70477066
/// Create a witness method call of a protocol requirement, passing in a lookup
@@ -7068,14 +7087,6 @@ class WitnessMethodInst final
70687087
}
70697088

70707089
ProtocolConformanceRef getConformance() const { return Conformance; }
7071-
7072-
ArrayRef<Operand> getTypeDependentOperands() const {
7073-
return getAllOperands();
7074-
}
7075-
7076-
MutableArrayRef<Operand> getTypeDependentOperands() {
7077-
return getAllOperands();
7078-
}
70797090
};
70807091

70817092
/// Access allowed to the opened value by the open_existential_addr instruction.
@@ -7420,7 +7431,7 @@ class DeinitExistentialValueInst
74207431
/// the pack type parameters, and the requirements on the pack type
74217432
/// parameters.
74227433
class OpenPackElementInst final
7423-
: public InstructionBaseWithTrailingOperands<
7434+
: public UnaryInstructionWithTypeDependentOperandsBase<
74247435
SILInstructionKind::OpenPackElementInst,
74257436
OpenPackElementInst,
74267437
SingleValueInstruction> {
@@ -7460,7 +7471,8 @@ class OpenPackElementInst final
74607471
GenericEnvironment *Env;
74617472

74627473
OpenPackElementInst(SILDebugLocation debugLoc,
7463-
ArrayRef<SILValue> allOperands,
7474+
SILValue packIndexOperand,
7475+
ArrayRef<SILValue> typeDependentOperands,
74647476
SILType type,
74657477
GenericEnvironment *env);
74667478

@@ -7481,14 +7493,6 @@ class OpenPackElementInst final
74817493
SILValue getIndexOperand() const {
74827494
return getAllOperands()[0].get();
74837495
}
7484-
7485-
ArrayRef<Operand> getTypeDependentOperands() const {
7486-
return getAllOperands().slice(1);
7487-
}
7488-
7489-
MutableArrayRef<Operand> getTypeDependentOperands() {
7490-
return getAllOperands().slice(1);
7491-
}
74927496
};
74937497

74947498
/// Projects the capture storage address from a @block_storage address.

lib/SIL/IR/SILInstructions.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ AllocBoxInst::AllocBoxInst(SILDebugLocation Loc, CanSILBoxType BoxType,
360360
SILFunction &F, Optional<SILDebugVariable> Var,
361361
bool hasDynamicLifetime,
362362
bool reflection)
363-
: InstructionBaseWithTrailingOperands(
364-
TypeDependentOperands, Loc, SILType::getPrimitiveObjectType(BoxType)),
363+
: NullaryInstructionWithTypeDependentOperandsBase(
364+
Loc, TypeDependentOperands, SILType::getPrimitiveObjectType(BoxType)),
365365
VarInfo(Var, getTrailingObjects<char>()),
366366
HasDynamicLifetime(hasDynamicLifetime),
367367
Reflection(reflection) {}
@@ -2253,9 +2253,11 @@ OpenExistentialValueInst::OpenExistentialValueInst(
22532253
}
22542254

22552255
OpenPackElementInst::OpenPackElementInst(
2256-
SILDebugLocation debugLoc, ArrayRef<SILValue> allOperands,
2256+
SILDebugLocation debugLoc, SILValue packIndexOperand,
2257+
ArrayRef<SILValue> typeDependentOperands,
22572258
SILType type, GenericEnvironment *env)
2258-
: InstructionBaseWithTrailingOperands(allOperands, debugLoc, type),
2259+
: UnaryInstructionWithTypeDependentOperandsBase(debugLoc, packIndexOperand,
2260+
typeDependentOperands, type),
22592261
Env(env) {
22602262
}
22612263

@@ -2264,24 +2266,24 @@ OpenPackElementInst *OpenPackElementInst::create(
22642266
GenericEnvironment *env) {
22652267
assert(indexOperand->getType().is<BuiltinPackIndexType>());
22662268

2267-
SmallVector<SILValue, 8> allOperands;
2268-
allOperands.push_back(indexOperand);
2269+
SmallVector<SILValue, 8> typeDependentOperands;
22692270

22702271
// open_pack_element references the pack substitutions and
22712272
// the types used in the shape class.
2272-
TypeDependentOperandCollector typeDependentOperands;
2273+
TypeDependentOperandCollector collector;
22732274
env->forEachPackElementBinding([&](ElementArchetypeType *elementType,
22742275
PackType *packSubstitution) {
2275-
typeDependentOperands.collect(packSubstitution->getCanonicalType());
2276+
collector.collect(packSubstitution->getCanonicalType());
22762277
});
2277-
typeDependentOperands.collect(env->getOpenedElementShapeClass());
2278-
typeDependentOperands.addTo(allOperands, F);
2278+
collector.collect(env->getOpenedElementShapeClass());
2279+
collector.addTo(typeDependentOperands, F);
22792280

22802281
SILType type = SILType::getSILTokenType(F.getASTContext());
22812282

2282-
auto size = totalSizeToAlloc<swift::Operand>(allOperands.size());
2283+
auto size = totalSizeToAlloc<swift::Operand>(1 + typeDependentOperands.size());
22832284
auto buffer = F.getModule().allocateInst(size, alignof(OpenPackElementInst));
2284-
return ::new (buffer) OpenPackElementInst(debugLoc, allOperands, type, env);
2285+
return ::new (buffer) OpenPackElementInst(debugLoc, indexOperand,
2286+
typeDependentOperands, type, env);
22852287
}
22862288

22872289
BeginCOWMutationInst::BeginCOWMutationInst(SILDebugLocation loc,

0 commit comments

Comments
 (0)