Skip to content

Commit 7a6ee11

Browse files
committed
[NFC] Add a utility function for testing for a specific BuiltinInst
1 parent b01436d commit 7a6ee11

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

include/swift/SIL/OwnershipUseVisitor.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ bool OwnershipUseVisitor<Impl>::visitInnerBorrowScopeEnd(Operand *borrowEnd) {
313313
return handleUsePoint(borrowEnd, UseLifetimeConstraint::NonLifetimeEnding);
314314
}
315315
case OperandOwnership::InstantaneousUse: {
316-
auto builtinUser = dyn_cast<BuiltinInst>(borrowEnd->getUser());
317-
if (builtinUser && builtinUser->getBuiltinKind() ==
318-
BuiltinValueKind::EndAsyncLetLifetime) {
316+
if (isBuiltinInst(borrowEnd->getUser(), BuiltinValueKind::EndAsyncLetLifetime)) {
319317
return handleUsePoint(borrowEnd,
320318
UseLifetimeConstraint::NonLifetimeEnding);
321319
}

include/swift/SIL/SILInstruction.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,6 +4309,33 @@ class BuiltinInst final
43094309
}
43104310
};
43114311

4312+
inline BuiltinInst *isBuiltinInst(SILValue value,
4313+
BuiltinValueKind kind) {
4314+
if (auto bi = dyn_cast<BuiltinInst>(value)) {
4315+
if (bi->getBuiltinKind() == kind)
4316+
return bi;
4317+
}
4318+
return nullptr;
4319+
}
4320+
4321+
inline BuiltinInst *isBuiltinInst(SILInstruction *inst,
4322+
BuiltinValueKind kind) {
4323+
if (auto bi = dyn_cast<BuiltinInst>(inst)) {
4324+
if (bi->getBuiltinKind() == kind)
4325+
return bi;
4326+
}
4327+
return nullptr;
4328+
}
4329+
4330+
inline const BuiltinInst *isBuiltinInst(const SILInstruction *inst,
4331+
BuiltinValueKind kind) {
4332+
if (auto bi = dyn_cast<BuiltinInst>(inst)) {
4333+
if (bi->getBuiltinKind() == kind)
4334+
return bi;
4335+
}
4336+
return nullptr;
4337+
}
4338+
43124339
/// Increments a given profiler counter for a given PGO function name. This is
43134340
/// lowered to the \c llvm.instrprof.increment LLVM intrinsic.
43144341
class IncrementProfilerCounterInst final

lib/SIL/IR/SILInstruction.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,13 +1525,9 @@ bool SILInstruction::isTriviallyDuplicatable() const {
15251525
}
15261526

15271527
bool SILInstruction::mayTrap() const {
1528-
if (auto *BI = dyn_cast<BuiltinInst>(this)) {
1529-
if (auto Kind = BI->getBuiltinKind()) {
1530-
if (Kind.value() == BuiltinValueKind::WillThrow) {
1531-
// We don't want willThrow instructions to be removed.
1532-
return true;
1533-
}
1534-
}
1528+
if (isBuiltinInst(this, BuiltinValueKind::WillThrow)) {
1529+
// We don't want willThrow instructions to be removed.
1530+
return true;
15351531
}
15361532
switch(getKind()) {
15371533
case SILInstructionKind::CondFailInst:

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,7 @@ bool BorrowingOperand::visitScopeEndingUses(
734734
bool dead = true;
735735
for (auto *use : user->getUses()) {
736736
dead = false;
737-
auto builtinUser = dyn_cast<BuiltinInst>(use->getUser());
738-
if (!builtinUser
739-
|| builtinUser->getBuiltinKind() != BuiltinValueKind::EndAsyncLetLifetime)
737+
if (!isBuiltinInst(use->getUser(), BuiltinValueKind::EndAsyncLetLifetime))
740738
continue;
741739

742740
if (!visitScopeEnd(use)) {

0 commit comments

Comments
 (0)