Skip to content

Commit 722d0bb

Browse files
committed
[di] Add an accessor for NumElements and use that instead of accessing the field directly.
1 parent 2cf6d02 commit 722d0bb

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ class ElementUseCollector {
561561

562562
void trackDestroy(SILInstruction *Destroy) { UseInfo.trackDestroy(Destroy); }
563563

564-
unsigned getNumMemoryElements() const { return TheMemory.NumElements; }
564+
/// Return the raw number of elements including the 'super.init' value.
565+
unsigned getNumMemoryElements() const { return TheMemory.getNumElements(); }
565566

566567
SILInstruction *getMemoryInst() const { return TheMemory.MemoryInst; }
567568

@@ -588,7 +589,8 @@ void ElementUseCollector::addElementUses(unsigned BaseEltNo, SILType UseTy,
588589
// If we're in a subelement of a struct or enum, just mark the struct, not
589590
// things that come after it in a parent tuple.
590591
unsigned NumElements = 1;
591-
if (TheMemory.NumElements != 1 && !InStructSubElement && !InEnumSubElement)
592+
if (TheMemory.getNumElements() != 1 && !InStructSubElement &&
593+
!InEnumSubElement)
592594
NumElements =
593595
getElementCountRec(TypeExpansionContext(*User->getFunction()), Module,
594596
UseTy, IsSelfOfNonDelegatingInitializer);
@@ -843,14 +845,14 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
843845
// by the callee, and this is enforced by sema, so we can consider it
844846
// a nonmutating use.
845847
bool isLet = true;
846-
847-
for (unsigned i = 0; i < TheMemory.NumElements; ++i) {
848+
849+
for (unsigned i = 0; i < TheMemory.getNumElements(); ++i) {
848850
if (!TheMemory.isElementLetProperty(i)) {
849851
isLet = false;
850852
break;
851853
}
852854
}
853-
855+
854856
if (isLet) {
855857
addElementUses(BaseEltNo, PointeeType, User, DIUseKind::IndirectIn);
856858
continue;
@@ -1186,7 +1188,7 @@ void ElementUseCollector::collectClassSelfUses() {
11861188
// We can safely handle anything else as an escape. They should all happen
11871189
// after super.init is invoked. As such, all elements must be initialized
11881190
// and super.init must be called.
1189-
trackUse(DIMemoryUse(User, DIUseKind::Load, 0, TheMemory.NumElements));
1191+
trackUse(DIMemoryUse(User, DIUseKind::Load, 0, TheMemory.getNumElements()));
11901192
}
11911193

11921194
assert(StoresOfArgumentToSelf == 1 &&
@@ -1461,7 +1463,7 @@ void ElementUseCollector::collectClassSelfUses(
14611463
Kind = DIUseKind::Escape;
14621464
}
14631465

1464-
trackUse(DIMemoryUse(User, Kind, 0, TheMemory.NumElements));
1466+
trackUse(DIMemoryUse(User, Kind, 0, TheMemory.getNumElements()));
14651467
}
14661468
}
14671469

@@ -1593,7 +1595,7 @@ void ClassInitElementUseCollector::collectClassInitSelfUses() {
15931595
// When we're analyzing a delegating constructor, we aren't field sensitive at
15941596
// all. Just treat all members of self as uses of the single
15951597
// non-field-sensitive value.
1596-
assert(TheMemory.NumElements == 1 && "delegating inits only have 1 bit");
1598+
assert(TheMemory.getNumElements() == 1 && "delegating inits only have 1 bit");
15971599
auto *MUI = TheMemory.MemoryInst;
15981600

15991601
// The number of stores of the initial 'self' argument into the self box
@@ -1827,7 +1829,8 @@ void swift::ownership::collectDIElementUsesFrom(
18271829
// When we're analyzing a delegating constructor, we aren't field sensitive
18281830
// at all. Just treat all members of self as uses of the single
18291831
// non-field-sensitive value.
1830-
assert(MemoryInfo.NumElements == 1 && "delegating inits only have 1 bit");
1832+
assert(MemoryInfo.getNumElements() == 1 &&
1833+
"delegating inits only have 1 bit");
18311834
collectDelegatingInitUses(MemoryInfo, UseInfo, MemoryInfo.MemoryInst);
18321835
return;
18331836
}

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class DIMemoryObjectInfo {
6060
/// This is the base type of the memory allocation.
6161
SILType MemorySILType;
6262

63-
public:
6463
/// This is the count of elements being analyzed. For memory objects that are
6564
/// tuples, this is the flattened element count. For 'self' members in init
6665
/// methods, this is the local field count (+1 for super/self classes were
@@ -103,6 +102,13 @@ class DIMemoryObjectInfo {
103102
return NumElements - (unsigned)isDerivedClassSelf();
104103
}
105104

105+
/// Return the number of elements, including the extra "super.init" tracker in
106+
/// initializers of derived classes.
107+
///
108+
/// \see getNumMemoryElements() for the number of elements, excluding the
109+
/// extra "super.init" tracker in the initializers of derived classes.
110+
unsigned getNumElements() const { return NumElements; }
111+
106112
/// Return true if this is 'self' in any kind of initializer.
107113
bool isAnyInitSelf() const { return !MemoryInst->isVar(); }
108114

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,9 @@ namespace {
435435
void emitSelfConsumedDiagnostic(SILInstruction *Inst);
436436

437437
LiveOutBlockState &getBlockInfo(SILBasicBlock *BB) {
438-
return PerBlockInfo.insert({BB,
439-
LiveOutBlockState(TheMemory.NumElements)}).first->second;
438+
return PerBlockInfo
439+
.insert({BB, LiveOutBlockState(TheMemory.getNumElements())})
440+
.first->second;
440441
}
441442

442443
AvailabilitySet getLivenessAtInst(SILInstruction *Inst, unsigned FirstElt,
@@ -640,7 +641,7 @@ void LifetimeChecker::noteUninitializedMembers(const DIMemoryUse &Use) {
640641
if (Liveness.get(i) == DIKind::Yes) continue;
641642

642643
// Ignore a failed super.init requirement.
643-
if (i == TheMemory.NumElements-1 && TheMemory.isDerivedClassSelf())
644+
if (i == TheMemory.getNumElements() - 1 && TheMemory.isDerivedClassSelf())
644645
continue;
645646

646647
std::string Name;
@@ -672,7 +673,7 @@ std::string LifetimeChecker::getUninitElementName(const DIMemoryUse &Use) {
672673
// Verify that it isn't the super.init marker that failed. The client should
673674
// handle this, not pass it down to diagnoseInitError.
674675
assert((!TheMemory.isDerivedClassSelf() ||
675-
firstUndefElement != TheMemory.NumElements-1) &&
676+
firstUndefElement != TheMemory.getNumElements() - 1) &&
676677
"super.init failure not handled in the right place");
677678

678679
// If the definition is a declaration, try to reconstruct a name and
@@ -938,8 +939,8 @@ void LifetimeChecker::handleStoreUse(unsigned UseID) {
938939

939940
if (TheMemory.isNonRootClassSelf()) {
940941
if (getSelfInitializedAtInst(Use.Inst) != DIKind::Yes) {
941-
auto SelfLiveness = getLivenessAtInst(Use.Inst,
942-
0, TheMemory.NumElements);
942+
auto SelfLiveness =
943+
getLivenessAtInst(Use.Inst, 0, TheMemory.getNumElements());
943944
if (SelfLiveness.isAllYes()) {
944945
emitSelfConsumedDiagnostic(Use.Inst);
945946
return;
@@ -1816,11 +1817,12 @@ void LifetimeChecker::handleSelfInitUse(unsigned UseID) {
18161817

18171818
// Determine the liveness states of the memory object, including the
18181819
// self/super.init state.
1819-
AvailabilitySet Liveness = getLivenessAtInst(Inst, 0, TheMemory.NumElements);
1820+
AvailabilitySet Liveness =
1821+
getLivenessAtInst(Inst, 0, TheMemory.getNumElements());
18201822

18211823
// self/super.init() calls require that self/super.init has not already
18221824
// been called. If it has, reject the program.
1823-
switch (Liveness.get(TheMemory.NumElements-1)) {
1825+
switch (Liveness.get(TheMemory.getNumElements() - 1)) {
18241826
case DIKind::No: // This is good! Keep going.
18251827
break;
18261828
case DIKind::Yes:
@@ -1838,15 +1840,16 @@ void LifetimeChecker::handleSelfInitUse(unsigned UseID) {
18381840
}
18391841

18401842
if (TheMemory.isDelegatingInit()) {
1841-
assert(TheMemory.NumElements == 1 && "delegating inits have a single elt");
1843+
assert(TheMemory.getNumElements() == 1 &&
1844+
"delegating inits have a single elt");
18421845

18431846
// Lower Assign instructions if needed.
18441847
if (isa<AssignInst>(Use.Inst) || isa<AssignByWrapperInst>(Use.Inst))
18451848
NeedsUpdateForInitState.push_back(UseID);
18461849
} else {
18471850
// super.init also requires that all ivars are initialized before the
18481851
// superclass initializer runs.
1849-
for (unsigned i = 0, e = TheMemory.NumElements-1; i != e; ++i) {
1852+
for (unsigned i = 0, e = TheMemory.getNumElements() - 1; i != e; ++i) {
18501853
if (Liveness.get(i) == DIKind::Yes) continue;
18511854

18521855
// If the super.init call is implicit generated, produce a specific
@@ -2045,7 +2048,7 @@ void LifetimeChecker::processNonTrivialRelease(unsigned ReleaseID) {
20452048
assert(isa<StrongReleaseInst>(Release) || isa<DestroyValueInst>(Release) ||
20462049
isa<DestroyAddrInst>(Release));
20472050

2048-
auto Availability = getLivenessAtInst(Release, 0, TheMemory.NumElements);
2051+
auto Availability = getLivenessAtInst(Release, 0, TheMemory.getNumElements());
20492052
DIKind SelfInitialized = DIKind::Yes;
20502053

20512054
if (TheMemory.isNonRootClassSelf()) {
@@ -2188,7 +2191,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() {
21882191
SILLocation Loc = TheMemory.getLoc();
21892192
Loc.markAutoGenerated();
21902193

2191-
unsigned NumMemoryElements = TheMemory.NumElements;
2194+
unsigned NumMemoryElements = TheMemory.getNumElements();
21922195

21932196
// We might need an extra bit to check if self was consumed.
21942197
if (HasConditionalSelfInitialized)
@@ -2338,10 +2341,10 @@ handleConditionalDestroys(SILValue ControlVariableAddr) {
23382341
SILBuilderWithScope B(TheMemory.MemoryInst);
23392342
Identifier ShiftRightFn, TruncateFn;
23402343

2341-
unsigned NumMemoryElements = TheMemory.NumElements;
2342-
2343-
unsigned SelfInitializedElt = TheMemory.NumElements;
2344-
unsigned SuperInitElt = TheMemory.NumElements - 1;
2344+
unsigned NumMemoryElements = TheMemory.getNumElements();
2345+
2346+
unsigned SelfInitializedElt = TheMemory.getNumElements();
2347+
unsigned SuperInitElt = TheMemory.getNumElements() - 1;
23452348

23462349
// We might need an extra bit to check if self was consumed.
23472350
if (HasConditionalSelfInitialized)
@@ -2698,7 +2701,7 @@ AvailabilitySet LifetimeChecker::getLivenessAtInst(SILInstruction *Inst,
26982701
LLVM_DEBUG(llvm::dbgs() << "Get liveness " << FirstElt << ", #" << NumElts
26992702
<< " at " << *Inst);
27002703

2701-
AvailabilitySet Result(TheMemory.NumElements);
2704+
AvailabilitySet Result(TheMemory.getNumElements());
27022705

27032706
// Empty tuple queries return a completely "unknown" vector, since they don't
27042707
// care about any of the elements.
@@ -2709,13 +2712,13 @@ AvailabilitySet LifetimeChecker::getLivenessAtInst(SILInstruction *Inst,
27092712

27102713
// The vastly most common case is memory allocations that are not tuples,
27112714
// so special case this with a more efficient algorithm.
2712-
if (TheMemory.NumElements == 1) {
2715+
if (TheMemory.getNumElements() == 1) {
27132716
return getLivenessAtNonTupleInst(Inst, InstBB, Result);
27142717
}
27152718

27162719
// Check locally to see if any elements are satisfied within the block, and
27172720
// keep track of which ones are still needed in the NeededElements set.
2718-
SmallBitVector NeededElements(TheMemory.NumElements);
2721+
SmallBitVector NeededElements(TheMemory.getNumElements());
27192722
NeededElements.set(FirstElt, FirstElt+NumElts);
27202723

27212724
// If there is a store in the current block, scan the block to see if the
@@ -2842,9 +2845,9 @@ bool LifetimeChecker::isInitializedAtUse(const DIMemoryUse &Use,
28422845

28432846
// If the client wants to know about super.init, check to see if we failed
28442847
// it or some other element.
2845-
if (Use.FirstElement+Use.NumElements == TheMemory.NumElements &&
2848+
if (Use.FirstElement + Use.NumElements == TheMemory.getNumElements() &&
28462849
TheMemory.isAnyDerivedClassSelf() &&
2847-
Liveness.get(Liveness.size()-1) != DIKind::Yes) {
2850+
Liveness.get(Liveness.size() - 1) != DIKind::Yes) {
28482851
if (SuperInitDone) *SuperInitDone = false;
28492852
}
28502853

@@ -2864,8 +2867,8 @@ bool LifetimeChecker::isInitializedAtUse(const DIMemoryUse &Use,
28642867
// we caught it, self is no longer available.
28652868
if (TheMemory.isNonRootClassSelf()) {
28662869
if (getSelfInitializedAtInst(Use.Inst) != DIKind::Yes) {
2867-
auto SelfLiveness = getLivenessAtInst(Use.Inst,
2868-
0, TheMemory.NumElements);
2870+
auto SelfLiveness =
2871+
getLivenessAtInst(Use.Inst, 0, TheMemory.getNumElements());
28692872
if (SelfLiveness.isAllYes()) {
28702873
if (FailedSelfUse) *FailedSelfUse = true;
28712874
return false;

0 commit comments

Comments
 (0)