@@ -563,6 +563,9 @@ namespace {
563
563
struct UseState {
564
564
MarkMustCheckInst *address;
565
565
566
+ using InstToBitMap =
567
+ llvm::SmallMapVector<SILInstruction *, SmallBitVector, 4 >;
568
+
566
569
llvm::Optional<unsigned > cachedNumSubelements;
567
570
568
571
// / The blocks that consume fields of the value.
@@ -576,7 +579,7 @@ struct UseState {
576
579
577
580
// / A map from a liveness requiring use to the part of the type that it
578
581
// / requires liveness for.
579
- llvm::SmallMapVector<SILInstruction *, SmallBitVector, 4 > livenessUses;
582
+ InstToBitMap livenessUses;
580
583
581
584
// / A map from a load [copy] or load [take] that we determined must be
582
585
// / converted to a load_borrow to the part of the type tree that it needs to
@@ -622,11 +625,11 @@ struct UseState {
622
625
623
626
// / A map from an instruction that initializes memory to the description of
624
627
// / the part of the type tree that it initializes.
625
- llvm::SmallMapVector<SILInstruction *, TypeTreeLeafTypeRange, 4 > initInsts;
628
+ InstToBitMap initInsts;
626
629
627
630
// / memInstMustReinitialize insts. Contains both insts like copy_addr/store
628
631
// / [assign] that are reinits that we will convert to inits and true reinits.
629
- llvm::SmallMapVector<SILInstruction *, SmallBitVector, 4 > reinitInsts;
632
+ InstToBitMap reinitInsts;
630
633
631
634
// / The set of drop_deinits of this mark_must_check
632
635
SmallSetVector<SILInstruction *, 2 > dropDeinitInsts;
@@ -653,32 +656,39 @@ struct UseState {
653
656
return *cachedNumSubelements;
654
657
}
655
658
656
- SmallBitVector &getOrCreateLivenessUse (SILInstruction *inst) {
657
- auto iter = livenessUses. find (inst);
658
- if ( iter == livenessUses. end ()) {
659
- iter = livenessUses. insert ({inst, SmallBitVector ( getNumSubelements ())})
660
- .first ;
659
+ SmallBitVector &getOrCreateAffectedBits (SILInstruction *inst,
660
+ InstToBitMap &map) {
661
+ auto iter = map. find (inst);
662
+ if ( iter == map. end ()) {
663
+ iter = map. insert ({inst, SmallBitVector ( getNumSubelements ())}) .first ;
661
664
}
662
665
return iter->second ;
663
666
}
664
667
668
+ void setAffectedBits (SILInstruction *inst, SmallBitVector const &bits,
669
+ InstToBitMap &map) {
670
+ getOrCreateAffectedBits (inst, map) |= bits;
671
+ }
672
+
673
+ void setAffectedBits (SILInstruction *inst, TypeTreeLeafTypeRange range,
674
+ InstToBitMap &map) {
675
+ range.setBits (getOrCreateAffectedBits (inst, map));
676
+ }
677
+
665
678
void recordLivenessUse (SILInstruction *inst, SmallBitVector const &bits) {
666
- getOrCreateLivenessUse (inst) |= bits;
679
+ setAffectedBits (inst, bits, livenessUses) ;
667
680
}
668
681
669
682
void recordLivenessUse (SILInstruction *inst, TypeTreeLeafTypeRange range) {
670
- auto &bits = getOrCreateLivenessUse (inst);
671
- range.setBits (bits);
683
+ setAffectedBits (inst, range, livenessUses);
672
684
}
673
685
674
686
void recordReinitUse (SILInstruction *inst, TypeTreeLeafTypeRange range) {
675
- auto iter = reinitInsts.find (inst);
676
- if (iter == reinitInsts.end ()) {
677
- iter =
678
- reinitInsts.insert ({inst, SmallBitVector (getNumSubelements ())}).first ;
679
- }
680
- auto &bits = iter->second ;
681
- range.setBits (bits);
687
+ setAffectedBits (inst, range, reinitInsts);
688
+ }
689
+
690
+ void recordInitUse (SILInstruction *inst, TypeTreeLeafTypeRange range) {
691
+ setAffectedBits (inst, range, initInsts);
682
692
}
683
693
684
694
// / Returns true if this is a terminator instruction that although it doesn't
@@ -875,7 +885,7 @@ struct UseState {
875
885
{
876
886
auto iter = initInsts.find (inst);
877
887
if (iter != initInsts.end ()) {
878
- if (span.setIntersection (iter->second ))
888
+ if (span.intersects (iter->second ))
879
889
return true ;
880
890
}
881
891
}
@@ -1020,7 +1030,7 @@ void UseState::initializeLiveness(
1020
1030
llvm::dbgs ()
1021
1031
<< " Found in/in_guaranteed/inout/inout_aliasable argument as "
1022
1032
" an init... adding mark_must_check as init!\n " );
1023
- initInsts. insert ({ address, liveness.getTopLevelSpan ()} );
1033
+ recordInitUse ( address, liveness.getTopLevelSpan ());
1024
1034
liveness.initializeDef (address, liveness.getTopLevelSpan ());
1025
1035
break ;
1026
1036
case swift::SILArgumentConvention::Indirect_Out:
@@ -1051,14 +1061,14 @@ void UseState::initializeLiveness(
1051
1061
// later invariants that we assert upon remain true.
1052
1062
LLVM_DEBUG (llvm::dbgs () << " Found move only arg closure box use... "
1053
1063
" adding mark_must_check as init!\n " );
1054
- initInsts. insert ({ address, liveness.getTopLevelSpan ()} );
1064
+ recordInitUse ( address, liveness.getTopLevelSpan ());
1055
1065
liveness.initializeDef (address, liveness.getTopLevelSpan ());
1056
1066
}
1057
1067
} else if (auto *box = dyn_cast<AllocBoxInst>(
1058
1068
lookThroughOwnershipInsts (projectBox->getOperand ()))) {
1059
1069
LLVM_DEBUG (llvm::dbgs () << " Found move only var allocbox use... "
1060
1070
" adding mark_must_check as init!\n " );
1061
- initInsts. insert ({ address, liveness.getTopLevelSpan ()} );
1071
+ recordInitUse ( address, liveness.getTopLevelSpan ());
1062
1072
liveness.initializeDef (address, liveness.getTopLevelSpan ());
1063
1073
}
1064
1074
}
@@ -1069,7 +1079,7 @@ void UseState::initializeLiveness(
1069
1079
stripAccessMarkers (address->getOperand ()))) {
1070
1080
LLVM_DEBUG (llvm::dbgs () << " Found ref_element_addr use... "
1071
1081
" adding mark_must_check as init!\n " );
1072
- initInsts. insert ({ address, liveness.getTopLevelSpan ()} );
1082
+ recordInitUse ( address, liveness.getTopLevelSpan ());
1073
1083
liveness.initializeDef (address, liveness.getTopLevelSpan ());
1074
1084
}
1075
1085
@@ -1079,7 +1089,7 @@ void UseState::initializeLiveness(
1079
1089
dyn_cast<GlobalAddrInst>(stripAccessMarkers (address->getOperand ()))) {
1080
1090
LLVM_DEBUG (llvm::dbgs () << " Found global_addr use... "
1081
1091
" adding mark_must_check as init!\n " );
1082
- initInsts. insert ({ address, liveness.getTopLevelSpan ()} );
1092
+ recordInitUse ( address, liveness.getTopLevelSpan ());
1083
1093
liveness.initializeDef (address, liveness.getTopLevelSpan ());
1084
1094
}
1085
1095
@@ -1752,8 +1762,7 @@ bool GatherUsesVisitor::visitUse(Operand *op) {
1752
1762
if (!leafRange)
1753
1763
return false ;
1754
1764
1755
- assert (!useState.initInsts .count (user));
1756
- useState.initInsts .insert ({user, *leafRange});
1765
+ useState.recordInitUse (user, *leafRange);
1757
1766
return true ;
1758
1767
}
1759
1768
0 commit comments