@@ -47,29 +47,25 @@ using InstSet = llvm::SmallPtrSet<SILInstruction *, 8>;
47
47
48
48
using InstVector = llvm::SmallVector<SILInstruction *, 8 >;
49
49
50
- // / A subset of instruction which may have side effects.
51
- // / Doesn't contain ones that have special handling (e.g. fix_lifetime)
52
- using WriteSet = SmallPtrSet<SILInstruction *, 8 >;
53
-
54
- // / Returns true if the \p MayWrites set contains any memory writes which may
55
- // / alias with the memory addressed by \a LI.
50
+ // / Returns true if the \p SideEffectInsts set contains any memory writes which
51
+ // / may alias with the memory addressed by \a LI.
56
52
template <SILInstructionKind K, typename T>
57
- static bool mayWriteTo (AliasAnalysis *AA, WriteSet &MayWrites ,
53
+ static bool mayWriteTo (AliasAnalysis *AA, InstSet &SideEffectInsts ,
58
54
UnaryInstructionBase<K, T> *Inst) {
59
- for (auto *W : MayWrites )
60
- if (AA->mayWriteToMemory (W , Inst->getOperand ())) {
61
- LLVM_DEBUG (llvm::dbgs () << " mayWriteTo\n " << *W << " to "
55
+ for (auto *I : SideEffectInsts )
56
+ if (AA->mayWriteToMemory (I , Inst->getOperand ())) {
57
+ LLVM_DEBUG (llvm::dbgs () << " mayWriteTo\n " << *I << " to "
62
58
<< *Inst << " \n " );
63
59
return true ;
64
60
}
65
61
return false ;
66
62
}
67
63
68
- // / Returns true if the \p MayWrites set contains any memory writes which may
69
- // / alias with any memory which is read by \p AI.
64
+ // / Returns true if the \p SideEffectInsts set contains any memory writes which
65
+ // / may alias with any memory which is read by \p AI.
70
66
// / Note: This function should only be called on a read-only apply!
71
67
static bool mayWriteTo (AliasAnalysis *AA, SideEffectAnalysis *SEA,
72
- WriteSet &MayWrites , ApplyInst *AI) {
68
+ InstSet &SideEffectInsts , ApplyInst *AI) {
73
69
FunctionSideEffects E;
74
70
SEA->getCalleeEffects (E, AI);
75
71
assert (E.getMemBehavior (RetainObserveKind::IgnoreRetains) <=
@@ -87,9 +83,9 @@ static bool mayWriteTo(AliasAnalysis *AA, SideEffectAnalysis *SEA,
87
83
SILValue Arg = AI->getArgument (Idx);
88
84
89
85
// Check if the memory addressed by the argument may alias any writes.
90
- for (auto *W : MayWrites ) {
91
- if (AA->mayWriteToMemory (W , Arg)) {
92
- LLVM_DEBUG (llvm::dbgs () << " mayWriteTo\n " << *W << " to "
86
+ for (auto *I : SideEffectInsts ) {
87
+ if (AA->mayWriteToMemory (I , Arg)) {
88
+ LLVM_DEBUG (llvm::dbgs () << " mayWriteTo\n " << *I << " to "
93
89
<< *AI << " \n " );
94
90
return true ;
95
91
}
@@ -192,17 +188,17 @@ static bool hoistInstructions(SILLoop *Loop, DominanceInfo *DT,
192
188
return Changed;
193
189
}
194
190
195
- // / Summary of may writes occurring in the loop tree rooted at \p
191
+ // / Summary of side effect instructions occurring in the loop tree rooted at \p
196
192
// / Loop. This includes all writes of the sub loops and the loop itself.
197
193
struct LoopNestSummary {
198
194
SILLoop *Loop;
199
- WriteSet MayWrites ;
195
+ InstSet SideEffectInsts ;
200
196
201
197
LoopNestSummary (SILLoop *Curr) : Loop(Curr) {}
202
198
203
199
204
200
void copySummary (LoopNestSummary &Other) {
205
- MayWrites .insert (Other.MayWrites .begin (), Other.MayWrites .end ());
201
+ SideEffectInsts .insert (Other.SideEffectInsts .begin (), Other.SideEffectInsts .end ());
206
202
}
207
203
208
204
LoopNestSummary (const LoopNestSummary &) = delete ;
@@ -284,8 +280,8 @@ static bool sinkInstruction(DominanceInfo *DT,
284
280
}
285
281
if (Changed && !ExitBB) {
286
282
// Created clones of instruction
287
- // Remove it from the may write set - dangling pointer
288
- LoopSummary->MayWrites .erase (Inst);
283
+ // Remove it from the side-effect set - dangling pointer
284
+ LoopSummary->SideEffectInsts .erase (Inst);
289
285
Inst->getParent ()->erase (Inst);
290
286
}
291
287
return Changed;
@@ -476,9 +472,10 @@ static bool isSafeReadOnlyApply(SideEffectAnalysis *SEA, ApplyInst *AI) {
476
472
return (MB <= SILInstruction::MemoryBehavior::MayRead);
477
473
}
478
474
479
- static void checkSideEffects (swift::SILInstruction &Inst, WriteSet &MayWrites) {
475
+ static void checkSideEffects (swift::SILInstruction &Inst,
476
+ InstSet &SideEffectInsts) {
480
477
if (Inst.mayHaveSideEffects ()) {
481
- MayWrites .insert (&Inst);
478
+ SideEffectInsts .insert (&Inst);
482
479
}
483
480
}
484
481
@@ -551,7 +548,7 @@ static bool isCoveredByScope(BeginAccessInst *BI, DominanceInfo *DT,
551
548
static bool analyzeBeginAccess (BeginAccessInst *BI,
552
549
SmallVector<BeginAccessInst *, 8 > &BeginAccesses,
553
550
SmallVector<FullApplySite, 8 > &fullApplies,
554
- WriteSet &MayWrites ,
551
+ InstSet &SideEffectInsts ,
555
552
AccessedStorageAnalysis *ASA,
556
553
DominanceInfo *DT) {
557
554
const AccessedStorage &storage =
@@ -595,12 +592,12 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
595
592
// TODO Introduce "Pure Swift" deinitializers
596
593
// We can then make use of alias information for instr's operands
597
594
// If they don't alias - we might get away with not recording a conflict
598
- for (auto mayWrite : MayWrites ) {
599
- // we actually compute all MayWrites in analyzeCurrentLoop
600
- if (!mayWrite ->mayRelease ()) {
595
+ for (SILInstruction *I : SideEffectInsts ) {
596
+ // we actually compute all SideEffectInsts in analyzeCurrentLoop
597
+ if (!I ->mayRelease ()) {
601
598
continue ;
602
599
}
603
- if (!isCoveredByScope (BI, DT, mayWrite ))
600
+ if (!isCoveredByScope (BI, DT, I ))
604
601
return false ;
605
602
}
606
603
@@ -611,12 +608,12 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
611
608
// Computes set of instructions we may be able to move out of the loop
612
609
// Important Note:
613
610
// We can't bail out of this method! we have to run it on all loops.
614
- // We *need* to discover all MayWrites -
611
+ // We *need* to discover all SideEffectInsts -
615
612
// even if the loop is otherwise skipped!
616
613
// This is because outer loops will depend on the inner loop's writes.
617
614
void LoopTreeOptimization::analyzeCurrentLoop (
618
615
std::unique_ptr<LoopNestSummary> &CurrSummary) {
619
- WriteSet &MayWrites = CurrSummary->MayWrites ;
616
+ InstSet &sideEffects = CurrSummary->SideEffectInsts ;
620
617
SILLoop *Loop = CurrSummary->Loop ;
621
618
LLVM_DEBUG (llvm::dbgs () << " Analyzing accesses.\n " );
622
619
@@ -651,7 +648,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
651
648
auto *BI = dyn_cast<BeginAccessInst>(&Inst);
652
649
assert (BI && " Expected a Begin Access" );
653
650
BeginAccesses.push_back (BI);
654
- checkSideEffects (Inst, MayWrites );
651
+ checkSideEffects (Inst, sideEffects );
655
652
break ;
656
653
}
657
654
case SILInstructionKind::RefElementAddrInst: {
@@ -665,7 +662,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
665
662
// cond_fail that would have protected (executed before) a memory access
666
663
// must - after hoisting - also be executed before said access.
667
664
HoistUp.insert (&Inst);
668
- checkSideEffects (Inst, MayWrites );
665
+ checkSideEffects (Inst, sideEffects );
669
666
break ;
670
667
}
671
668
case SILInstructionKind::ApplyInst: {
@@ -681,7 +678,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
681
678
if (auto fullApply = FullApplySite::isa (&Inst)) {
682
679
fullApplies.push_back (fullApply);
683
680
}
684
- checkSideEffects (Inst, MayWrites );
681
+ checkSideEffects (Inst, sideEffects );
685
682
if (canHoistUpDefault (&Inst, Loop, DomTree, RunsOnHighLevelSIL)) {
686
683
HoistUp.insert (&Inst);
687
684
}
@@ -697,23 +694,23 @@ void LoopTreeOptimization::analyzeCurrentLoop(
697
694
return ;
698
695
}
699
696
for (auto *AI : ReadOnlyApplies) {
700
- if (!mayWriteTo (AA, SEA, MayWrites , AI)) {
697
+ if (!mayWriteTo (AA, SEA, sideEffects , AI)) {
701
698
HoistUp.insert (AI);
702
699
}
703
700
}
704
701
for (auto *LI : Loads) {
705
- if (!mayWriteTo (AA, MayWrites , LI)) {
702
+ if (!mayWriteTo (AA, sideEffects , LI)) {
706
703
HoistUp.insert (LI);
707
704
}
708
705
}
709
- bool mayWritesMayRelease =
710
- std::any_of (MayWrites .begin (), MayWrites .end (),
706
+ bool sideEffectsMayRelease =
707
+ std::any_of (sideEffects .begin (), sideEffects .end (),
711
708
[&](SILInstruction *W) { return W->mayRelease (); });
712
709
for (auto *FL : FixLifetimes) {
713
710
if (!DomTree->dominates (FL->getOperand ()->getParentBlock (), Preheader)) {
714
711
continue ;
715
712
}
716
- if (!mayWriteTo (AA, MayWrites , FL) || !mayWritesMayRelease ) {
713
+ if (!mayWriteTo (AA, sideEffects , FL) || !sideEffectsMayRelease ) {
717
714
SinkDown.push_back (FL);
718
715
}
719
716
}
@@ -723,7 +720,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
723
720
LLVM_DEBUG (llvm::dbgs () << " Some end accesses can't be handled\n " );
724
721
continue ;
725
722
}
726
- if (analyzeBeginAccess (BI, BeginAccesses, fullApplies, MayWrites , ASA,
723
+ if (analyzeBeginAccess (BI, BeginAccesses, fullApplies, sideEffects , ASA,
727
724
DomTree)) {
728
725
SpecialHoist.push_back (BI);
729
726
}
0 commit comments