Skip to content

Commit 0eacaab

Browse files
committed
SIL: add SILArgument::isErased()
Similar to SILInstruction, it's now possible to check if an argument was erased from a SILBasicBlock.
1 parent bf254cc commit 0eacaab

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

include/swift/SIL/SILArgument.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ class SILArgument : public ValueBase {
101101

102102
SILBasicBlock *getParent() const { return parentBlock; }
103103

104+
/// Returns true if this argument is erased from a basic block.
105+
///
106+
/// Note that SILArguments which are erased from a SILBasicBlock are not
107+
/// destroyed and freed, but are kept in memory. So it's safe to keep a
108+
/// pointer to an erased argument and then at a later time check if its
109+
/// erased.
110+
bool isErased() const { return !parentBlock; }
111+
104112
SILFunction *getFunction();
105113
const SILFunction *getFunction() const;
106114

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public SwiftObjectHeader {
308308
const ValueDecl *D = nullptr);
309309

310310
/// Remove all block arguments.
311-
void dropAllArguments() { ArgumentList.clear(); }
311+
void dropAllArguments();
312312

313313
//===--------------------------------------------------------------------===//
314314
// Successors

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ SILFunctionArgument *SILBasicBlock::replaceFunctionArgument(
180180

181181
SILFunctionArgument *NewArg = new (M) SILFunctionArgument(Ty, Kind, D);
182182
NewArg->setParent(this);
183+
ArgumentList[i]->parentBlock = nullptr;
183184

184185
// TODO: When we switch to malloc/free allocation we'll be leaking memory
185186
// here.
@@ -203,6 +204,7 @@ SILPhiArgument *SILBasicBlock::replacePhiArgument(unsigned i, SILType Ty,
203204

204205
SILPhiArgument *NewArg = new (M) SILPhiArgument(Ty, Kind, D);
205206
NewArg->setParent(this);
207+
ArgumentList[i]->parentBlock = nullptr;
206208

207209
// TODO: When we switch to malloc/free allocation we'll be leaking memory
208210
// here.
@@ -257,9 +259,17 @@ SILPhiArgument *SILBasicBlock::insertPhiArgument(unsigned AtArgPos, SILType Ty,
257259
return arg;
258260
}
259261

262+
void SILBasicBlock::dropAllArguments() {
263+
for (SILArgument *arg : ArgumentList) {
264+
arg->parentBlock = nullptr;
265+
}
266+
ArgumentList.clear();
267+
}
268+
260269
void SILBasicBlock::eraseArgument(int Index) {
261270
assert(getArgument(Index)->use_empty() &&
262271
"Erasing block argument that has uses!");
272+
ArgumentList[Index]->parentBlock = nullptr;
263273
ArgumentList.erase(ArgumentList.begin() + Index);
264274
}
265275

0 commit comments

Comments
 (0)