Skip to content

Commit 719a932

Browse files
authored
Merge pull request swiftlang#28493 from gottesmm/pr-29cc76c7ab2ff93438398e13a6657e50a043a1ce
2 parents ff4ecd2 + 37dcacf commit 719a932

File tree

2 files changed

+217
-178
lines changed

2 files changed

+217
-178
lines changed

include/swift/SIL/SILArgument.h

Lines changed: 121 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,52 @@ struct SILArgumentKind {
6262
};
6363

6464
class SILArgument : public ValueBase {
65-
void operator=(const SILArgument &) = delete;
66-
void operator delete(void *Ptr, size_t) SWIFT_DELETE_OPERATOR_DELETED
65+
friend class SILBasicBlock;
66+
67+
SILBasicBlock *parentBlock;
68+
const ValueDecl *decl;
69+
70+
protected:
71+
SILArgument(ValueKind subClassKind, SILBasicBlock *inputParentBlock,
72+
SILType type, ValueOwnershipKind ownershipKind,
73+
const ValueDecl *inputDecl = nullptr);
74+
75+
SILArgument(ValueKind subClassKind, SILBasicBlock *inputParentBlock,
76+
SILBasicBlock::arg_iterator positionInArgumentArray, SILType type,
77+
ValueOwnershipKind ownershipKind,
78+
const ValueDecl *inputDecl = nullptr);
6779

68-
SILBasicBlock *ParentBB;
69-
const ValueDecl *Decl;
80+
// A special constructor, only intended for use in
81+
// SILBasicBlock::replacePHIArg and replaceFunctionArg.
82+
explicit SILArgument(ValueKind subClassKind, SILType type,
83+
ValueOwnershipKind ownershipKind,
84+
const ValueDecl *inputDecl = nullptr)
85+
: ValueBase(subClassKind, type, IsRepresentative::Yes),
86+
parentBlock(nullptr), decl(inputDecl) {
87+
Bits.SILArgument.VOKind = static_cast<unsigned>(ownershipKind);
88+
}
7089

7190
public:
91+
void operator=(const SILArgument &) = delete;
92+
void operator delete(void *, size_t) SWIFT_DELETE_OPERATOR_DELETED;
93+
7294
ValueOwnershipKind getOwnershipKind() const {
7395
return static_cast<ValueOwnershipKind>(Bits.SILArgument.VOKind);
7496
}
75-
void setOwnershipKind(ValueOwnershipKind NewKind) {
76-
Bits.SILArgument.VOKind = static_cast<unsigned>(NewKind);
97+
98+
void setOwnershipKind(ValueOwnershipKind newKind) {
99+
Bits.SILArgument.VOKind = static_cast<unsigned>(newKind);
77100
}
78101

79-
SILBasicBlock *getParent() { return ParentBB; }
80-
const SILBasicBlock *getParent() const { return ParentBB; }
102+
SILBasicBlock *getParent() { return parentBlock; }
103+
const SILBasicBlock *getParent() const { return parentBlock; }
81104

82105
SILFunction *getFunction();
83106
const SILFunction *getFunction() const;
84107

85108
SILModule &getModule() const;
86109

87-
const ValueDecl *getDecl() const { return Decl; }
110+
const ValueDecl *getDecl() const { return decl; }
88111

89112
static bool classof(const SILInstruction *) = delete;
90113
static bool classof(const SILUndef *) = delete;
@@ -94,31 +117,32 @@ class SILArgument : public ValueBase {
94117
}
95118

96119
unsigned getIndex() const {
97-
ArrayRef<SILArgument *> Args = getParent()->getArguments();
98-
for (unsigned i = 0, e = Args.size(); i != e; ++i)
99-
if (Args[i] == this)
100-
return i;
120+
for (auto p : llvm::enumerate(getParent()->getArguments())) {
121+
if (p.value() == this) {
122+
return p.index();
123+
}
124+
}
101125
llvm_unreachable("SILArgument not argument of its parent BB");
102126
}
103127

104128
/// Return true if this block argument is actually a phi argument as
105129
/// opposed to a cast or projection.
106-
bool isPhiArgument();
130+
bool isPhiArgument() const;
107131

108132
/// If this argument is a phi, return the incoming phi value for the given
109133
/// predecessor BB. If this argument is not a phi, return an invalid SILValue.
110-
SILValue getIncomingPhiValue(SILBasicBlock *predBB);
134+
SILValue getIncomingPhiValue(SILBasicBlock *predBlock) const;
111135

112136
/// If this argument is a phi, populate `OutArray` with the incoming phi
113137
/// values for each predecessor BB. If this argument is not a phi, return
114138
/// false.
115-
bool getIncomingPhiValues(llvm::SmallVectorImpl<SILValue> &ReturnedPhiValues);
139+
bool getIncomingPhiValues(SmallVectorImpl<SILValue> &returnedPhiValues) const;
116140

117141
/// If this argument is a phi, populate `OutArray` with each predecessor block
118142
/// and its incoming phi value. If this argument is not a phi, return false.
119-
bool getIncomingPhiValues(
120-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
121-
&ReturnedPredAndPhiValuePairs);
143+
bool
144+
getIncomingPhiValues(SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
145+
&returnedPredAndPhiValuePairs) const;
122146

123147
/// Returns true if we were able to find a single terminator operand value for
124148
/// each predecessor of this arguments basic block. The found values are
@@ -127,7 +151,8 @@ class SILArgument : public ValueBase {
127151
/// Note: this peeks through any projections or cast implied by the
128152
/// terminator. e.g. the incoming value for a switch_enum payload argument is
129153
/// the enum itself (the operand of the switch_enum).
130-
bool getSingleTerminatorOperands(llvm::SmallVectorImpl<SILValue> &OutArray);
154+
bool getSingleTerminatorOperands(
155+
SmallVectorImpl<SILValue> &returnedSingleTermOperands) const;
131156

132157
/// Returns true if we were able to find single terminator operand values for
133158
/// each predecessor of this arguments basic block. The found values are
@@ -137,7 +162,8 @@ class SILArgument : public ValueBase {
137162
/// terminator. e.g. the incoming value for a switch_enum payload argument is
138163
/// the enum itself (the operand of the switch_enum).
139164
bool getSingleTerminatorOperands(
140-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>> &OutArray);
165+
SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
166+
&returnedSingleTermOperands) const;
141167

142168
/// If this SILArgument's parent block has a single predecessor whose
143169
/// terminator has a single operand, return the incoming operand of the
@@ -153,56 +179,61 @@ class SILArgument : public ValueBase {
153179
}
154180

155181
protected:
156-
SILArgument(ValueKind SubClassKind, SILBasicBlock *ParentBB, SILType Ty,
157-
ValueOwnershipKind OwnershipKind,
158-
const ValueDecl *D = nullptr);
159-
SILArgument(ValueKind SubClassKind, SILBasicBlock *ParentBB,
160-
SILBasicBlock::arg_iterator Pos, SILType Ty,
161-
ValueOwnershipKind OwnershipKind,
162-
const ValueDecl *D = nullptr);
163-
164-
// A special constructor, only intended for use in
165-
// SILBasicBlock::replacePHIArg and replaceFunctionArg.
166-
explicit SILArgument(ValueKind SubClassKind, SILType Ty,
167-
ValueOwnershipKind OwnershipKind,
168-
const ValueDecl *D = nullptr)
169-
: ValueBase(SubClassKind, Ty, IsRepresentative::Yes), ParentBB(nullptr),
170-
Decl(D) {
171-
Bits.SILArgument.VOKind = static_cast<unsigned>(OwnershipKind);
182+
void setParent(SILBasicBlock *newParentBlock) {
183+
parentBlock = newParentBlock;
172184
}
173-
void setParent(SILBasicBlock *P) { ParentBB = P; }
174-
175-
friend SILBasicBlock;
176185
};
177186

178187
class SILPhiArgument : public SILArgument {
188+
friend class SILBasicBlock;
189+
190+
SILPhiArgument(SILBasicBlock *parentBlock, SILType type,
191+
ValueOwnershipKind ownershipKind,
192+
const ValueDecl *decl = nullptr)
193+
: SILArgument(ValueKind::SILPhiArgument, parentBlock, type, ownershipKind,
194+
decl) {}
195+
196+
SILPhiArgument(SILBasicBlock *parentBlock,
197+
SILBasicBlock::arg_iterator argArrayInsertPt, SILType type,
198+
ValueOwnershipKind ownershipKind,
199+
const ValueDecl *decl = nullptr)
200+
: SILArgument(ValueKind::SILPhiArgument, parentBlock, argArrayInsertPt,
201+
type, ownershipKind, decl) {}
202+
203+
// A special constructor, only intended for use in
204+
// SILBasicBlock::replacePHIArg.
205+
explicit SILPhiArgument(SILType type, ValueOwnershipKind ownershipKind,
206+
const ValueDecl *decl = nullptr)
207+
: SILArgument(ValueKind::SILPhiArgument, type, ownershipKind, decl) {}
208+
179209
public:
180210
/// Return true if this is block argument is actually a phi argument as
181211
/// opposed to a cast or projection.
182-
bool isPhiArgument();
212+
bool isPhiArgument() const;
183213

184214
/// If this argument is a phi, return the incoming phi value for the given
185215
/// predecessor BB. If this argument is not a phi, return an invalid SILValue.
186216
///
187217
/// FIXME: Once SILPhiArgument actually implies that it is a phi argument,
188218
/// this will be guaranteed to return a valid SILValue.
189-
SILValue getIncomingPhiValue(SILBasicBlock *BB);
219+
SILValue getIncomingPhiValue(SILBasicBlock *predBlock) const;
190220

191221
/// If this argument is a phi, populate `OutArray` with the incoming phi
192222
/// values for each predecessor BB. If this argument is not a phi, return
193223
/// false.
194224
///
195225
/// FIXME: Once SILPhiArgument actually implies that it is a phi argument,
196226
/// this will always succeed.
197-
bool getIncomingPhiValues(llvm::SmallVectorImpl<SILValue> &OutArray);
227+
bool getIncomingPhiValues(SmallVectorImpl<SILValue> &returnedPhiValues) const;
198228

199229
/// If this argument is a phi, populate `OutArray` with each predecessor block
200230
/// and its incoming phi value. If this argument is not a phi, return false.
201231
///
202232
/// FIXME: Once SILPhiArgument actually implies that it is a phi argument,
203233
/// this will always succeed.
204-
bool getIncomingPhiValues(
205-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>> &OutArray);
234+
bool
235+
getIncomingPhiValues(SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
236+
&returnedPredAndPhiValuePairs) const;
206237

207238
/// Returns true if we were able to find a single terminator operand value for
208239
/// each predecessor of this arguments basic block. The found values are
@@ -211,7 +242,8 @@ class SILPhiArgument : public SILArgument {
211242
/// Note: this peeks through any projections or cast implied by the
212243
/// terminator. e.g. the incoming value for a switch_enum payload argument is
213244
/// the enum itself (the operand of the switch_enum).
214-
bool getSingleTerminatorOperands(llvm::SmallVectorImpl<SILValue> &OutArray);
245+
bool getSingleTerminatorOperands(
246+
SmallVectorImpl<SILValue> &returnedSingleTermOperands) const;
215247

216248
/// Returns true if we were able to find single terminator operand values for
217249
/// each predecessor of this arguments basic block. The found values are
@@ -221,7 +253,8 @@ class SILPhiArgument : public SILArgument {
221253
/// terminator. e.g. the incoming value for a switch_enum payload argument is
222254
/// the enum itself (the operand of the switch_enum).
223255
bool getSingleTerminatorOperands(
224-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>> &OutArray);
256+
SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
257+
&returnedSingleTermOperands) const;
225258

226259
/// If this SILArgument's parent block has a single predecessor whose
227260
/// terminator has a single operand, return the incoming operand of the
@@ -236,30 +269,35 @@ class SILPhiArgument : public SILArgument {
236269
static bool classof(const SILNode *node) {
237270
return node->getKind() == SILNodeKind::SILPhiArgument;
238271
}
272+
};
239273

240-
private:
241-
friend SILBasicBlock;
242-
SILPhiArgument(SILBasicBlock *ParentBB, SILType Ty, ValueOwnershipKind OwnershipKind,
243-
const ValueDecl *D = nullptr)
244-
: SILArgument(ValueKind::SILPhiArgument, ParentBB, Ty, OwnershipKind, D) {}
245-
SILPhiArgument(SILBasicBlock *ParentBB, SILBasicBlock::arg_iterator Pos,
246-
SILType Ty, ValueOwnershipKind OwnershipKind,
247-
const ValueDecl *D = nullptr)
248-
: SILArgument(ValueKind::SILPhiArgument, ParentBB, Pos, Ty, OwnershipKind, D) {}
274+
class SILFunctionArgument : public SILArgument {
275+
friend class SILBasicBlock;
276+
277+
SILFunctionArgument(SILBasicBlock *parentBlock, SILType type,
278+
ValueOwnershipKind ownershipKind,
279+
const ValueDecl *decl = nullptr)
280+
: SILArgument(ValueKind::SILFunctionArgument, parentBlock, type,
281+
ownershipKind, decl) {}
282+
SILFunctionArgument(SILBasicBlock *parentBlock,
283+
SILBasicBlock::arg_iterator argArrayInsertPt,
284+
SILType type, ValueOwnershipKind ownershipKind,
285+
const ValueDecl *decl = nullptr)
286+
: SILArgument(ValueKind::SILFunctionArgument, parentBlock,
287+
argArrayInsertPt, type, ownershipKind, decl) {}
249288

250289
// A special constructor, only intended for use in
251-
// SILBasicBlock::replacePHIArg.
252-
explicit SILPhiArgument(SILType Ty, ValueOwnershipKind OwnershipKind,
253-
const ValueDecl *D = nullptr)
254-
: SILArgument(ValueKind::SILPhiArgument, Ty, OwnershipKind, D) {}
255-
};
290+
// SILBasicBlock::replaceFunctionArg.
291+
explicit SILFunctionArgument(SILType type, ValueOwnershipKind ownershipKind,
292+
const ValueDecl *decl = nullptr)
293+
: SILArgument(ValueKind::SILFunctionArgument, type, ownershipKind, decl) {
294+
}
256295

257-
class SILFunctionArgument : public SILArgument {
258296
public:
259297
bool isIndirectResult() const {
260298
auto numIndirectResults =
261299
getFunction()->getConventions().getNumIndirectSILResults();
262-
return (getIndex() < numIndirectResults);
300+
return getIndex() < numIndirectResults;
263301
}
264302

265303
SILArgumentConvention getArgumentConvention() const {
@@ -280,76 +318,66 @@ class SILFunctionArgument : public SILArgument {
280318
bool isSelf() const;
281319

282320
/// Returns true if this SILArgument is passed via the given convention.
283-
bool hasConvention(SILArgumentConvention P) const {
284-
return getArgumentConvention() == P;
321+
bool hasConvention(SILArgumentConvention convention) const {
322+
return getArgumentConvention() == convention;
285323
}
286324

287325
static bool classof(const SILInstruction *) = delete;
288326
static bool classof(const SILUndef *) = delete;
289327
static bool classof(const SILNode *node) {
290328
return node->getKind() == SILNodeKind::SILFunctionArgument;
291329
}
292-
293-
private:
294-
friend SILBasicBlock;
295-
296-
SILFunctionArgument(SILBasicBlock *ParentBB, SILType Ty, ValueOwnershipKind OwnershipKind,
297-
const ValueDecl *D = nullptr)
298-
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Ty, OwnershipKind, D) {}
299-
SILFunctionArgument(SILBasicBlock *ParentBB, SILBasicBlock::arg_iterator Pos,
300-
SILType Ty, ValueOwnershipKind OwnershipKind, const ValueDecl *D = nullptr)
301-
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Pos, Ty, OwnershipKind, D) {}
302-
303-
// A special constructor, only intended for use in
304-
// SILBasicBlock::replaceFunctionArg.
305-
explicit SILFunctionArgument(SILType Ty, ValueOwnershipKind OwnershipKind,
306-
const ValueDecl *D = nullptr)
307-
: SILArgument(ValueKind::SILFunctionArgument, Ty, OwnershipKind, D) {}
308330
};
309331

310332
//===----------------------------------------------------------------------===//
311333
// Out of line Definitions for SILArgument to avoid Forward Decl issues
312334
//===----------------------------------------------------------------------===//
313335

314-
inline bool SILArgument::isPhiArgument() {
336+
inline bool SILArgument::isPhiArgument() const {
315337
if (auto *phiArg = dyn_cast<SILPhiArgument>(this))
316338
return phiArg->isPhiArgument();
317339

318340
return false;
319341
}
320342

321-
inline SILValue SILArgument::getIncomingPhiValue(SILBasicBlock *BB) {
343+
inline SILValue
344+
SILArgument::getIncomingPhiValue(SILBasicBlock *predBlock) const {
322345
if (isa<SILFunctionArgument>(this))
323346
return SILValue();
324-
return cast<SILPhiArgument>(this)->getIncomingPhiValue(BB);
347+
return cast<SILPhiArgument>(this)->getIncomingPhiValue(predBlock);
325348
}
326349

327-
inline bool
328-
SILArgument::getIncomingPhiValues(llvm::SmallVectorImpl<SILValue> &OutArray) {
350+
inline bool SILArgument::getIncomingPhiValues(
351+
SmallVectorImpl<SILValue> &returnedPhiValues) const {
329352
if (isa<SILFunctionArgument>(this))
330353
return false;
331-
return cast<SILPhiArgument>(this)->getIncomingPhiValues(OutArray);
354+
return cast<SILPhiArgument>(this)->getIncomingPhiValues(returnedPhiValues);
332355
}
333356

334357
inline bool SILArgument::getIncomingPhiValues(
335-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>> &OutArray) {
358+
SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
359+
&returnedPredAndPhiValuePairs) const {
336360
if (isa<SILFunctionArgument>(this))
337361
return false;
338-
return cast<SILPhiArgument>(this)->getIncomingPhiValues(OutArray);
362+
return cast<SILPhiArgument>(this)->getIncomingPhiValues(
363+
returnedPredAndPhiValuePairs);
339364
}
340365

341366
inline bool SILArgument::getSingleTerminatorOperands(
342-
llvm::SmallVectorImpl<SILValue> &OutArray) {
367+
SmallVectorImpl<SILValue> &returnedSingleTermOperands) const {
343368
if (isa<SILFunctionArgument>(this))
344369
return false;
345-
return cast<SILPhiArgument>(this)->getSingleTerminatorOperands(OutArray);
370+
return cast<SILPhiArgument>(this)->getSingleTerminatorOperands(
371+
returnedSingleTermOperands);
346372
}
347373

348374
inline bool SILArgument::getSingleTerminatorOperands(
349-
llvm::SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>> &OutArray) {
375+
SmallVectorImpl<std::pair<SILBasicBlock *, SILValue>>
376+
&returnedSingleTermOperands) const {
350377
if (isa<SILFunctionArgument>(this))
351378
return false;
352-
return cast<SILPhiArgument>(this)->getSingleTerminatorOperands(OutArray);
379+
return cast<SILPhiArgument>(this)->getSingleTerminatorOperands(
380+
returnedSingleTermOperands);
353381
}
354382

355383
} // end swift namespace

0 commit comments

Comments
 (0)