Skip to content

Commit f7f1df4

Browse files
committed
[loop-arc] Refactor code that processes how an instruction effects a reference count completely onto RefCountState.
Previously, RefCountState provided APIs that the ARC dataflow used to determine if an instruction used, decremented, or guaranteed used a value that we are tracking. Upon further reflection, how an instruction affects a RefCountState is really internal to the RefCountState. rdar://22238729
1 parent f99577d commit f7f1df4

File tree

4 files changed

+116
-146
lines changed

4 files changed

+116
-146
lines changed

lib/SILAnalysis/ARC/ARCRegionState.cpp

+2-56
Original file line numberDiff line numberDiff line change
@@ -212,34 +212,7 @@ bool ARCRegionState::processBlockBottomUp(
212212
if (Op && OtherState->first == Op)
213213
continue;
214214

215-
// If this state is not tracking anything, skip it.
216-
if (!OtherState->second.isTrackingRefCount())
217-
continue;
218-
219-
// Check if the instruction we are visiting could potentially use our
220-
// instruction in a way that requires us to guarantee the lifetime of the
221-
// pointer up to this point. This has the effect of performing a use and a
222-
// decrement.
223-
if (OtherState->second.handlePotentialGuaranteedUser(&I, InsertPt, AA)) {
224-
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
225-
<< OtherState->second.getRCRoot());
226-
continue;
227-
}
228-
229-
// Check if the instruction we are visiting could potentially decrement
230-
// the reference counted value we are tracking... in a manner that could
231-
// cause us to change states. If we do change states continue...
232-
if (OtherState->second.handlePotentialDecrement(&I, AA)) {
233-
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
234-
<< OtherState->second.getRCRoot());
235-
continue;
236-
}
237-
238-
// Otherwise check if the reference counted value we are tracking
239-
// could be used by the given instruction.
240-
if (OtherState->second.handlePotentialUser(&I, InsertPt, AA))
241-
DEBUG(llvm::dbgs() << " Found Potential Use:\n "
242-
<< OtherState->second.getRCRoot());
215+
OtherState->second.updateForSameLoopInst(&I, InsertPt, AA);
243216
}
244217
}
245218

@@ -330,34 +303,7 @@ bool ARCRegionState::processBlockTopDown(
330303
if (Op && OtherState->first == Op)
331304
continue;
332305

333-
// If the other state is not tracking anything, bail.
334-
if (!OtherState->second.isTrackingRefCount())
335-
continue;
336-
337-
// Check if the instruction we are visiting could potentially use our
338-
// instruction in a way that requires us to guarantee the lifetime of the
339-
// pointer up to this point. This has the effect of performing a use and a
340-
// decrement.
341-
if (OtherState->second.handlePotentialGuaranteedUser(&I, &I, AA)) {
342-
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
343-
<< OtherState->second.getRCRoot());
344-
continue;
345-
}
346-
347-
// Check if the instruction we are visiting could potentially decrement
348-
// the reference counted value we are tracking in a manner that could
349-
// cause us to change states. If we do change states continue...
350-
if (OtherState->second.handlePotentialDecrement(&I, &I, AA)) {
351-
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
352-
<< OtherState->second.getRCRoot());
353-
continue;
354-
}
355-
356-
// Otherwise check if the reference counted value we are tracking
357-
// could be used by the given instruction.
358-
if (OtherState->second.handlePotentialUser(&I, AA))
359-
DEBUG(llvm::dbgs() << " Found Potential Use:\n "
360-
<< OtherState->second.getRCRoot());
306+
OtherState->second.updateForSameLoopInst(&I, &I, AA);
361307
}
362308
}
363309

lib/SILAnalysis/ARC/GlobalARCSequenceDataflow.cpp

+2-56
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,7 @@ static bool processBBTopDown(
110110
if (Op && OtherState->first == Op)
111111
continue;
112112

113-
// If the other state is not tracking anything, bail.
114-
if (!OtherState->second.isTrackingRefCount())
115-
continue;
116-
117-
// Check if the instruction we are visiting could potentially use our
118-
// instruction in a way that requires us to guarantee the lifetime of the
119-
// pointer up to this point. This has the effect of performing a use and a
120-
// decrement.
121-
if (OtherState->second.handlePotentialGuaranteedUser(&I, &I, AA)) {
122-
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
123-
<< OtherState->second.getRCRoot());
124-
continue;
125-
}
126-
127-
// Check if the instruction we are visiting could potentially decrement
128-
// the reference counted value we are tracking in a manner that could
129-
// cause us to change states. If we do change states continue...
130-
if (OtherState->second.handlePotentialDecrement(&I, &I, AA)) {
131-
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
132-
<< OtherState->second.getRCRoot());
133-
continue;
134-
}
135-
136-
// Otherwise check if the reference counted value we are tracking
137-
// could be used by the given instruction.
138-
if (OtherState->second.handlePotentialUser(&I, AA))
139-
DEBUG(llvm::dbgs() << " Found Potential Use:\n "
140-
<< OtherState->second.getRCRoot());
113+
OtherState->second.updateForSameLoopInst(&I, &I, AA);
141114
}
142115
}
143116

@@ -289,34 +262,7 @@ bool ARCSequenceDataflowEvaluator::processBBBottomUp(
289262
if (Op && OtherState->first == Op)
290263
continue;
291264

292-
// If this state is not tracking anything, skip it.
293-
if (!OtherState->second.isTrackingRefCount())
294-
continue;
295-
296-
// Check if the instruction we are visiting could potentially use our
297-
// instruction in a way that requires us to guarantee the lifetime of the
298-
// pointer up to this point. This has the effect of performing a use and a
299-
// decrement.
300-
if (OtherState->second.handlePotentialGuaranteedUser(&I, InsertPt, AA)) {
301-
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
302-
<< OtherState->second.getRCRoot());
303-
continue;
304-
}
305-
306-
// Check if the instruction we are visiting could potentially decrement
307-
// the reference counted value we are tracking... in a manner that could
308-
// cause us to change states. If we do change states continue...
309-
if (OtherState->second.handlePotentialDecrement(&I, AA)) {
310-
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
311-
<< OtherState->second.getRCRoot());
312-
continue;
313-
}
314-
315-
// Otherwise check if the reference counted value we are tracking
316-
// could be used by the given instruction.
317-
if (OtherState->second.handlePotentialUser(&I, InsertPt, AA))
318-
DEBUG(llvm::dbgs() << " Found Potential Use:\n "
319-
<< OtherState->second.getRCRoot());
265+
OtherState->second.updateForSameLoopInst(&I, InsertPt, AA);
320266
}
321267
}
322268

lib/SILAnalysis/ARC/RefCountState.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,39 @@ bool BottomUpRefCountState::handlePotentialUser(SILInstruction *PotentialUser,
428428
return handleUser(PotentialUser, InsertPt, getRCRoot(), AA);
429429
}
430430

431+
void BottomUpRefCountState::updateForSameLoopInst(SILInstruction *I,
432+
SILInstruction *InsertPt,
433+
AliasAnalysis *AA) {
434+
// If this state is not tracking anything, there is nothing to update.
435+
if (!isTrackingRefCount())
436+
return;
437+
438+
// Check if the instruction we are visiting could potentially use our
439+
// instruction in a way that requires us to guarantee the lifetime of the
440+
// pointer up to this point. This has the effect of performing a use and a
441+
// decrement.
442+
if (handlePotentialGuaranteedUser(I, InsertPt, AA)) {
443+
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
444+
<< getRCRoot());
445+
return;
446+
}
447+
448+
// Check if the instruction we are visiting could potentially decrement
449+
// the reference counted value we are tracking... in a manner that could
450+
// cause us to change states. If we do change states continue...
451+
if (handlePotentialDecrement(I, AA)) {
452+
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
453+
<< getRCRoot());
454+
return;
455+
}
456+
457+
// Otherwise check if the reference counted value we are tracking
458+
// could be used by the given instruction.
459+
if (!handlePotentialUser(I, InsertPt, AA))
460+
return;
461+
DEBUG(llvm::dbgs() << " Found Potential Use:\n " << getRCRoot());
462+
}
463+
431464
//===----------------------------------------------------------------------===//
432465
// Top Down Ref Count State
433466
//===----------------------------------------------------------------------===//
@@ -765,6 +798,39 @@ bool TopDownRefCountState::handlePotentialUser(SILInstruction *PotentialUser,
765798
return handleUser(PotentialUser, getRCRoot(), AA);
766799
}
767800

801+
void TopDownRefCountState::updateForSameLoopInst(SILInstruction *I,
802+
SILInstruction *InsertPt,
803+
AliasAnalysis *AA) {
804+
// If the other state is not tracking anything, bail.
805+
if (!isTrackingRefCount())
806+
return;
807+
808+
// Check if the instruction we are visiting could potentially use our
809+
// instruction in a way that requires us to guarantee the lifetime of the
810+
// pointer up to this point. This has the effect of performing a use and a
811+
// decrement.
812+
if (handlePotentialGuaranteedUser(I, InsertPt, AA)) {
813+
DEBUG(llvm::dbgs() << " Found Potential Guaranteed Use:\n "
814+
<< getRCRoot());
815+
return;
816+
}
817+
818+
// Check if the instruction we are visiting could potentially decrement
819+
// the reference counted value we are tracking in a manner that could
820+
// cause us to change states. If we do change states continue...
821+
if (handlePotentialDecrement(I, InsertPt, AA)) {
822+
DEBUG(llvm::dbgs() << " Found Potential Decrement:\n "
823+
<< getRCRoot());
824+
return;
825+
}
826+
827+
// Otherwise check if the reference counted value we are tracking
828+
// could be used by the given instruction.
829+
if (!handlePotentialUser(I, AA))
830+
return;
831+
DEBUG(llvm::dbgs() << " Found Potential Use:\n " << getRCRoot());
832+
}
833+
768834
//===----------------------------------------------------------------------===//
769835
// Printing Utilities
770836
//===----------------------------------------------------------------------===//

lib/SILAnalysis/ARC/RefCountState.h

+46-34
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,11 @@ class BottomUpRefCountState : public RefCountState {
202202
/// true.
203203
bool initWithMutatorInst(SILInstruction *I);
204204

205-
/// Check if PotentialDecrement can decrement the reference count associated
206-
/// with the value we are tracking. If so advance the state's sequence
207-
/// appropriately and return true. Otherwise return false.
208-
bool handlePotentialDecrement(SILInstruction *Decrement, AliasAnalysis *AA);
209-
210-
/// Check if PotentialUser could be a use of the reference counted value that
211-
/// requires user to be alive. If so advance the state's sequence
212-
/// appropriately and return true. Otherwise return false.
213-
bool handlePotentialUser(SILInstruction *PotentialUser,
214-
SILInstruction *InsertPt, AliasAnalysis *AA);
215-
216-
/// Check if PotentialGuaranteedUser can use the reference count associated
217-
/// with the value we are tracking. If so advance the state's sequence
218-
/// appropriately and return true. Otherwise return false.
219-
bool handlePotentialGuaranteedUser(SILInstruction *User,
220-
SILInstruction *InsertPt,
221-
AliasAnalysis *AA);
205+
/// Update this reference count's state given the instruction \p I. \p
206+
/// InsertPt is the point furthest up the CFG where we can move the currently
207+
/// tracked reference count.
208+
void updateForSameLoopInst(SILInstruction *I, SILInstruction *InsertPt,
209+
AliasAnalysis *AA);
222210

223211
/// Attempt to merge \p Other into this ref count state. Return true if we
224212
/// succeed and false otherwise.
@@ -249,6 +237,11 @@ class BottomUpRefCountState : public RefCountState {
249237
/// advance return true. Otherwise return false.
250238
bool handleDecrement(SILInstruction *PotentialDecrement);
251239

240+
/// Check if PotentialDecrement can decrement the reference count associated
241+
/// with the value we are tracking. If so advance the state's sequence
242+
/// appropriately and return true. Otherwise return false.
243+
bool handlePotentialDecrement(SILInstruction *Decrement, AliasAnalysis *AA);
244+
252245
/// Returns true if given the current lattice state, do we care if the value
253246
/// we are tracking is used.
254247
bool valueCanBeUsedGivenLatticeState() const;
@@ -260,6 +253,12 @@ class BottomUpRefCountState : public RefCountState {
260253
bool handleUser(SILInstruction *PotentialUser, SILInstruction *InsertPt,
261254
SILValue RCIdentity, AliasAnalysis *AA);
262255

256+
/// Check if PotentialUser could be a use of the reference counted value that
257+
/// requires user to be alive. If so advance the state's sequence
258+
/// appropriately and return true. Otherwise return false.
259+
bool handlePotentialUser(SILInstruction *PotentialUser,
260+
SILInstruction *InsertPt, AliasAnalysis *AA);
261+
263262
/// Returns true if given the current lattice state, do we care if the value
264263
/// we are tracking is used.
265264
bool valueCanBeGuaranteedUsedGivenLatticeState() const;
@@ -272,6 +271,13 @@ class BottomUpRefCountState : public RefCountState {
272271
SILInstruction *InsertPt, SILValue RCIdentity,
273272
AliasAnalysis *AA);
274273

274+
/// Check if PotentialGuaranteedUser can use the reference count associated
275+
/// with the value we are tracking. If so advance the state's sequence
276+
/// appropriately and return true. Otherwise return false.
277+
bool handlePotentialGuaranteedUser(SILInstruction *User,
278+
SILInstruction *InsertPt,
279+
AliasAnalysis *AA);
280+
275281
/// We have a matching ref count inst. Return true if we advance the sequence
276282
/// and false otherwise.
277283
bool handleRefCountInstMatch(SILInstruction *RefCountInst);
@@ -322,23 +328,11 @@ class TopDownRefCountState : public RefCountState {
322328
/// Uninitialize the current state.
323329
void clear();
324330

325-
/// Check if PotentialDecrement can decrement the reference count associated
326-
/// with the value we are tracking. If so advance the state's sequence
327-
/// appropriately and return true. Otherwise return false.
328-
bool handlePotentialDecrement(SILInstruction *PotentialDecrement,
329-
SILInstruction *InsertPt, AliasAnalysis *AA);
330-
331-
/// Check if PotentialUser could be a use of the reference counted value that
332-
/// requires user to be alive. If so advance the state's sequence
333-
/// appropriately and return true. Otherwise return false.
334-
bool handlePotentialUser(SILInstruction *PotentialUser, AliasAnalysis *AA);
335-
336-
/// Check if PotentialGuaranteedUser can use the reference count associated
337-
/// with the value we are tracking. If so advance the state's sequence
338-
/// appropriately and return true. Otherwise return false.
339-
bool handlePotentialGuaranteedUser(SILInstruction *PotentialGuaranteedUser,
340-
SILInstruction *InsertPt,
341-
AliasAnalysis *AA);
331+
/// Update this reference count's state given the instruction \p I. \p
332+
/// InsertPt is the point furthest up the CFG where we can move the currently
333+
/// tracked reference count.
334+
void updateForSameLoopInst(SILInstruction *I, SILInstruction *InsertPt,
335+
AliasAnalysis *AA);
342336

343337
/// Returns true if the passed in ref count inst matches the ref count inst
344338
/// we are tracking. This handles generically retains/release.
@@ -361,6 +355,12 @@ class TopDownRefCountState : public RefCountState {
361355
bool handleDecrement(SILInstruction *PotentialDecrement,
362356
SILInstruction *InsertPt);
363357

358+
/// Check if PotentialDecrement can decrement the reference count associated
359+
/// with the value we are tracking. If so advance the state's sequence
360+
/// appropriately and return true. Otherwise return false.
361+
bool handlePotentialDecrement(SILInstruction *PotentialDecrement,
362+
SILInstruction *InsertPt, AliasAnalysis *AA);
363+
364364
/// Returns true if given the current lattice state, do we care if the value
365365
/// we are tracking is used.
366366
bool valueCanBeUsedGivenLatticeState() const;
@@ -370,6 +370,11 @@ class TopDownRefCountState : public RefCountState {
370370
bool handleUser(SILInstruction *PotentialUser,
371371
SILValue RCIdentity, AliasAnalysis *AA);
372372

373+
/// Check if PotentialUser could be a use of the reference counted value that
374+
/// requires user to be alive. If so advance the state's sequence
375+
/// appropriately and return true. Otherwise return false.
376+
bool handlePotentialUser(SILInstruction *PotentialUser, AliasAnalysis *AA);
377+
373378
/// Returns true if given the current lattice state, do we care if the value
374379
/// we are tracking is used.
375380
bool valueCanBeGuaranteedUsedGivenLatticeState() const;
@@ -380,6 +385,13 @@ class TopDownRefCountState : public RefCountState {
380385
SILInstruction *InsertPt, SILValue RCIdentity,
381386
AliasAnalysis *AA);
382387

388+
/// Check if PotentialGuaranteedUser can use the reference count associated
389+
/// with the value we are tracking. If so advance the state's sequence
390+
/// appropriately and return true. Otherwise return false.
391+
bool handlePotentialGuaranteedUser(SILInstruction *PotentialGuaranteedUser,
392+
SILInstruction *InsertPt,
393+
AliasAnalysis *AA);
394+
383395
/// We have a matching ref count inst. Return true if we advance the sequence
384396
/// and false otherwise.
385397
bool handleRefCountInstMatch(SILInstruction *RefCountInst);

0 commit comments

Comments
 (0)