Skip to content

Commit 4e8cfde

Browse files
committed
LICM: rename MayWrites -> SideEffectInsts
Because the set includes all side-effect instructions, also may-reads. NFC
1 parent cca638a commit 4e8cfde

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

lib/SILOptimizer/LoopTransforms/LICM.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,25 @@ using InstSet = llvm::SmallPtrSet<SILInstruction *, 8>;
4747

4848
using InstVector = llvm::SmallVector<SILInstruction *, 8>;
4949

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.
5652
template <SILInstructionKind K, typename T>
57-
static bool mayWriteTo(AliasAnalysis *AA, WriteSet &MayWrites,
53+
static bool mayWriteTo(AliasAnalysis *AA, InstSet &SideEffectInsts,
5854
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 "
6258
<< *Inst << "\n");
6359
return true;
6460
}
6561
return false;
6662
}
6763

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.
7066
/// Note: This function should only be called on a read-only apply!
7167
static bool mayWriteTo(AliasAnalysis *AA, SideEffectAnalysis *SEA,
72-
WriteSet &MayWrites, ApplyInst *AI) {
68+
InstSet &SideEffectInsts, ApplyInst *AI) {
7369
FunctionSideEffects E;
7470
SEA->getCalleeEffects(E, AI);
7571
assert(E.getMemBehavior(RetainObserveKind::IgnoreRetains) <=
@@ -87,9 +83,9 @@ static bool mayWriteTo(AliasAnalysis *AA, SideEffectAnalysis *SEA,
8783
SILValue Arg = AI->getArgument(Idx);
8884

8985
// 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 "
9389
<< *AI << "\n");
9490
return true;
9591
}
@@ -192,17 +188,17 @@ static bool hoistInstructions(SILLoop *Loop, DominanceInfo *DT,
192188
return Changed;
193189
}
194190

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
196192
/// Loop. This includes all writes of the sub loops and the loop itself.
197193
struct LoopNestSummary {
198194
SILLoop *Loop;
199-
WriteSet MayWrites;
195+
InstSet SideEffectInsts;
200196

201197
LoopNestSummary(SILLoop *Curr) : Loop(Curr) {}
202198

203199

204200
void copySummary(LoopNestSummary &Other) {
205-
MayWrites.insert(Other.MayWrites.begin(), Other.MayWrites.end());
201+
SideEffectInsts.insert(Other.SideEffectInsts.begin(), Other.SideEffectInsts.end());
206202
}
207203

208204
LoopNestSummary(const LoopNestSummary &) = delete;
@@ -284,8 +280,8 @@ static bool sinkInstruction(DominanceInfo *DT,
284280
}
285281
if (Changed && !ExitBB) {
286282
// 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);
289285
Inst->getParent()->erase(Inst);
290286
}
291287
return Changed;
@@ -476,9 +472,10 @@ static bool isSafeReadOnlyApply(SideEffectAnalysis *SEA, ApplyInst *AI) {
476472
return (MB <= SILInstruction::MemoryBehavior::MayRead);
477473
}
478474

479-
static void checkSideEffects(swift::SILInstruction &Inst, WriteSet &MayWrites) {
475+
static void checkSideEffects(swift::SILInstruction &Inst,
476+
InstSet &SideEffectInsts) {
480477
if (Inst.mayHaveSideEffects()) {
481-
MayWrites.insert(&Inst);
478+
SideEffectInsts.insert(&Inst);
482479
}
483480
}
484481

@@ -551,7 +548,7 @@ static bool isCoveredByScope(BeginAccessInst *BI, DominanceInfo *DT,
551548
static bool analyzeBeginAccess(BeginAccessInst *BI,
552549
SmallVector<BeginAccessInst *, 8> &BeginAccesses,
553550
SmallVector<FullApplySite, 8> &fullApplies,
554-
WriteSet &MayWrites,
551+
InstSet &SideEffectInsts,
555552
AccessedStorageAnalysis *ASA,
556553
DominanceInfo *DT) {
557554
const AccessedStorage &storage =
@@ -595,12 +592,12 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
595592
// TODO Introduce "Pure Swift" deinitializers
596593
// We can then make use of alias information for instr's operands
597594
// 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()) {
601598
continue;
602599
}
603-
if (!isCoveredByScope(BI, DT, mayWrite))
600+
if (!isCoveredByScope(BI, DT, I))
604601
return false;
605602
}
606603

@@ -611,12 +608,12 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
611608
// Computes set of instructions we may be able to move out of the loop
612609
// Important Note:
613610
// 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 -
615612
// even if the loop is otherwise skipped!
616613
// This is because outer loops will depend on the inner loop's writes.
617614
void LoopTreeOptimization::analyzeCurrentLoop(
618615
std::unique_ptr<LoopNestSummary> &CurrSummary) {
619-
WriteSet &MayWrites = CurrSummary->MayWrites;
616+
InstSet &sideEffects = CurrSummary->SideEffectInsts;
620617
SILLoop *Loop = CurrSummary->Loop;
621618
LLVM_DEBUG(llvm::dbgs() << " Analyzing accesses.\n");
622619

@@ -651,7 +648,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
651648
auto *BI = dyn_cast<BeginAccessInst>(&Inst);
652649
assert(BI && "Expected a Begin Access");
653650
BeginAccesses.push_back(BI);
654-
checkSideEffects(Inst, MayWrites);
651+
checkSideEffects(Inst, sideEffects);
655652
break;
656653
}
657654
case SILInstructionKind::RefElementAddrInst: {
@@ -665,7 +662,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
665662
// cond_fail that would have protected (executed before) a memory access
666663
// must - after hoisting - also be executed before said access.
667664
HoistUp.insert(&Inst);
668-
checkSideEffects(Inst, MayWrites);
665+
checkSideEffects(Inst, sideEffects);
669666
break;
670667
}
671668
case SILInstructionKind::ApplyInst: {
@@ -681,7 +678,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
681678
if (auto fullApply = FullApplySite::isa(&Inst)) {
682679
fullApplies.push_back(fullApply);
683680
}
684-
checkSideEffects(Inst, MayWrites);
681+
checkSideEffects(Inst, sideEffects);
685682
if (canHoistUpDefault(&Inst, Loop, DomTree, RunsOnHighLevelSIL)) {
686683
HoistUp.insert(&Inst);
687684
}
@@ -697,23 +694,23 @@ void LoopTreeOptimization::analyzeCurrentLoop(
697694
return;
698695
}
699696
for (auto *AI : ReadOnlyApplies) {
700-
if (!mayWriteTo(AA, SEA, MayWrites, AI)) {
697+
if (!mayWriteTo(AA, SEA, sideEffects, AI)) {
701698
HoistUp.insert(AI);
702699
}
703700
}
704701
for (auto *LI : Loads) {
705-
if (!mayWriteTo(AA, MayWrites, LI)) {
702+
if (!mayWriteTo(AA, sideEffects, LI)) {
706703
HoistUp.insert(LI);
707704
}
708705
}
709-
bool mayWritesMayRelease =
710-
std::any_of(MayWrites.begin(), MayWrites.end(),
706+
bool sideEffectsMayRelease =
707+
std::any_of(sideEffects.begin(), sideEffects.end(),
711708
[&](SILInstruction *W) { return W->mayRelease(); });
712709
for (auto *FL : FixLifetimes) {
713710
if (!DomTree->dominates(FL->getOperand()->getParentBlock(), Preheader)) {
714711
continue;
715712
}
716-
if (!mayWriteTo(AA, MayWrites, FL) || !mayWritesMayRelease) {
713+
if (!mayWriteTo(AA, sideEffects, FL) || !sideEffectsMayRelease) {
717714
SinkDown.push_back(FL);
718715
}
719716
}
@@ -723,7 +720,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
723720
LLVM_DEBUG(llvm::dbgs() << "Some end accesses can't be handled\n");
724721
continue;
725722
}
726-
if (analyzeBeginAccess(BI, BeginAccesses, fullApplies, MayWrites, ASA,
723+
if (analyzeBeginAccess(BI, BeginAccesses, fullApplies, sideEffects, ASA,
727724
DomTree)) {
728725
SpecialHoist.push_back(BI);
729726
}

0 commit comments

Comments
 (0)