@@ -364,8 +364,6 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
364
364
const BorrowScopeOperand &operand);
365
365
366
366
struct BorrowScopeIntroducingValueKind {
367
- using UnderlyingKindTy = std::underlying_type<ValueKind>::type;
368
-
369
367
// / Enum we use for exhaustive pattern matching over borrow scope introducers.
370
368
enum Kind {
371
369
LoadBorrow,
@@ -374,8 +372,10 @@ struct BorrowScopeIntroducingValueKind {
374
372
Phi,
375
373
};
376
374
377
- static Optional<BorrowScopeIntroducingValueKind> get (ValueKind kind) {
378
- switch (kind) {
375
+ static Optional<BorrowScopeIntroducingValueKind> get (SILValue value) {
376
+ if (value.getOwnershipKind () != ValueOwnershipKind::Guaranteed)
377
+ return None;
378
+ switch (value->getKind ()) {
379
379
default :
380
380
return None;
381
381
case ValueKind::LoadBorrowInst:
@@ -384,9 +384,16 @@ struct BorrowScopeIntroducingValueKind {
384
384
return BorrowScopeIntroducingValueKind (BeginBorrow);
385
385
case ValueKind::SILFunctionArgument:
386
386
return BorrowScopeIntroducingValueKind (SILFunctionArgument);
387
- case ValueKind::SILPhiArgument:
387
+ case ValueKind::SILPhiArgument: {
388
+ if (llvm::any_of (value->getParentBlock ()->getPredecessorBlocks (),
389
+ [](SILBasicBlock *block) {
390
+ return !isa<BranchInst>(block->getTerminator ());
391
+ })) {
392
+ return None;
393
+ }
388
394
return BorrowScopeIntroducingValueKind (Phi);
389
395
}
396
+ }
390
397
}
391
398
392
399
Kind value;
@@ -441,53 +448,14 @@ struct BorrowScopeIntroducingValue {
441
448
BorrowScopeIntroducingValueKind kind;
442
449
SILValue value;
443
450
444
- BorrowScopeIntroducingValue (LoadBorrowInst *lbi)
445
- : kind(BorrowScopeIntroducingValueKind::LoadBorrow), value(lbi) {}
446
- BorrowScopeIntroducingValue (BeginBorrowInst *bbi)
447
- : kind(BorrowScopeIntroducingValueKind::BeginBorrow), value(bbi) {}
448
- BorrowScopeIntroducingValue (SILFunctionArgument *arg)
449
- : kind(BorrowScopeIntroducingValueKind::SILFunctionArgument), value(arg) {
450
- assert (arg->getOwnershipKind () == ValueOwnershipKind::Guaranteed);
451
- }
452
- BorrowScopeIntroducingValue (SILPhiArgument *arg)
453
- : kind(BorrowScopeIntroducingValueKind::Phi), value(arg) {
454
- assert (llvm::all_of (arg->getParent ()->getPredecessorBlocks (),
455
- [](SILBasicBlock *block) {
456
- return isa<BranchInst>(block->getTerminator ());
457
- }) &&
458
- " Phi argument incoming values must come from branch insts!" );
459
- assert (arg->isPhiArgument () && " Can only accept a true phi argument!" );
460
- assert (arg->getOwnershipKind () == ValueOwnershipKind::Guaranteed);
461
- }
462
-
463
- BorrowScopeIntroducingValue (SILValue v)
464
- : kind(*BorrowScopeIntroducingValueKind::get (v->getKind ())), value(v) {
465
- // Validate that if we have a phi argument that all our predecessors have
466
- // branches as terminators.
467
- assert (!isa<SILPhiArgument>(v) ||
468
- (llvm::all_of (v->getParentBlock ()->getPredecessorBlocks (),
469
- [](SILBasicBlock *block) {
470
- return isa<BranchInst>(block->getTerminator ());
471
- }) &&
472
- " Phi argument incoming values must come from branch insts!" ));
473
-
474
- assert (v.getOwnershipKind () == ValueOwnershipKind::Guaranteed);
475
- }
476
-
477
451
// / If value is a borrow introducer return it after doing some checks.
452
+ // /
453
+ // / This is the only way to construct a BorrowScopeIntroducingValue. We make
454
+ // / the primary constructor private for this reason.
478
455
static Optional<BorrowScopeIntroducingValue> get (SILValue value) {
479
- auto kind = BorrowScopeIntroducingValueKind::get (value->getKind ());
480
- if (!kind || value.getOwnershipKind () != ValueOwnershipKind::Guaranteed)
481
- return None;
482
- // If kind is phi and we were not passed something with all branch
483
- // predecessors, return None.
484
- if ((*kind) == BorrowScopeIntroducingValueKind::Phi &&
485
- llvm::any_of (value->getParentBlock ()->getPredecessorBlocks (),
486
- [](SILBasicBlock *block) {
487
- return !isa<BranchInst>(block->getTerminator ());
488
- }))
456
+ auto kind = BorrowScopeIntroducingValueKind::get (value);
457
+ if (!kind)
489
458
return None;
490
- // Otherwise, create our value directly.
491
459
return BorrowScopeIntroducingValue (*kind, value);
492
460
}
493
461
0 commit comments