Skip to content

Commit 1ee15a3

Browse files
committed
Add an enum to represent when to build gen/kill set and when to perform the actual DSE
1 parent f31040b commit 1ee15a3

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

lib/SILPasses/Scalar/DeadStoreElimination.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,17 @@ static llvm::cl::opt<bool> EnableLocalStoreDSE("enable-local-store-dse",
9292
STATISTIC(NumDeadStores, "Number of dead stores removed");
9393
STATISTIC(NumPartialDeadStores, "Number of partial dead stores removed");
9494

95+
/// Are we building the gen/kill sets or actually performing the DSE.
96+
enum class DSEComputeKind {BuildGenKillSet, PerformDSE};
97+
9598
//===----------------------------------------------------------------------===//
9699
// Utility Functions
97100
//===----------------------------------------------------------------------===//
98101

102+
static inline bool isBuildingGenKillSet(DSEComputeKind Kind) {
103+
return Kind == DSEComputeKind::BuildGenKillSet;
104+
}
105+
99106
/// Returns true if this is an instruction that may have side effects in a
100107
/// general sense but are inert from a load store perspective.
101108
static bool isDeadStoreInertInstruction(SILInstruction *Inst) {
@@ -323,26 +330,26 @@ class DSEContext {
323330
/// There is a read to a location, expand the location into individual fields
324331
/// before processing them.
325332
void processRead(SILInstruction *Inst, BlockState *State, SILValue Mem,
326-
bool BuildGenKillSet);
333+
DSEComputeKind Kind);
327334

328335
/// There is a write to a location, expand the location into individual fields
329336
/// before processing them.
330337
void processWrite(SILInstruction *Inst, BlockState *State, SILValue Val,
331-
SILValue Mem, bool BuildGenKillSet);
338+
SILValue Mem, DSEComputeKind Kind);
332339

333340
/// Process Instructions. Extract MemLocations from SIL LoadInst.
334-
void processLoadInst(SILInstruction *Inst, bool BuildGenKillSet);
341+
void processLoadInst(SILInstruction *Inst, DSEComputeKind Kind);
335342

336343
/// Process Instructions. Extract MemLocations from SIL StoreInst.
337-
void processStoreInst(SILInstruction *Inst, bool BuildGenKillSet);
344+
void processStoreInst(SILInstruction *Inst, DSEComputeKind Kind);
338345

339346
/// Process Instructions. Extract MemLocations from SIL DebugValueAddrInst.
340347
/// DebugValueAddrInst operand maybe promoted to DebugValue, when this is
341348
/// done, DebugValueAddrInst is effectively a read on the MemLocation.
342349
void processDebugValueAddrInst(SILInstruction *I);
343350

344351
/// Process Instructions. Extract MemLocations from SIL Unknown Memory Inst.
345-
void processUnknownReadMemInst(SILInstruction *Inst, bool BuildGenKillSet);
352+
void processUnknownReadMemInst(SILInstruction *Inst, DSEComputeKind Kind);
346353

347354
/// Check whether the instruction invalidate any MemLocations due to change in
348355
/// its MemLocation Base.
@@ -367,7 +374,7 @@ class DSEContext {
367374
/// the loop basic block will have store to x.a and therefore x.a = 13 can now
368375
/// be considered dead.
369376
///
370-
void invalidateMemLocationBase(SILInstruction *Inst, bool BuildGenKillSet);
377+
void invalidateMemLocationBase(SILInstruction *Inst, DSEComputeKind Kind);
371378

372379
/// Get the bit representing the location in the MemLocationVault.
373380
///
@@ -400,7 +407,7 @@ class DSEContext {
400407
void mergeSuccessorStates(SILBasicBlock *BB);
401408

402409
/// Update the BlockState based on the given instruction.
403-
void processInstruction(SILInstruction *I, bool BuildGenKillSet);
410+
void processInstruction(SILInstruction *I, DSEComputeKind Kind);
404411

405412
/// Entry point for global dead store elimination.
406413
void run();
@@ -423,7 +430,7 @@ unsigned DSEContext::getMemLocationBit(const MemLocation &Loc) {
423430

424431
void DSEContext::processBasicBlockForGenKillSet(SILBasicBlock *BB) {
425432
for (auto I = BB->rbegin(), E = BB->rend(); I != E; ++I) {
426-
processInstruction(&(*I), true);
433+
processInstruction(&(*I), DSEComputeKind::BuildGenKillSet);
427434
}
428435
}
429436

@@ -449,7 +456,7 @@ bool DSEContext::processBasicBlock(SILBasicBlock *BB) {
449456

450457
// Process instructions in post-order fashion.
451458
for (auto I = BB->rbegin(), E = BB->rend(); I != E; ++I) {
452-
processInstruction(&(*I), false);
459+
processInstruction(&(*I), DSEComputeKind::PerformDSE);
453460
}
454461

455462
// If WriteSetIn changes, then keep iterating until reached a fixed
@@ -487,11 +494,11 @@ void DSEContext::mergeSuccessorStates(SILBasicBlock *BB) {
487494
}
488495

489496
void DSEContext::invalidateMemLocationBase(SILInstruction *I,
490-
bool BuildGenKillSet) {
497+
DSEComputeKind Kind) {
491498
// If this instruction defines the base of a location, then we need to
492499
// invalidate any locations with the same base.
493500
BlockState *S = getBlockState(I);
494-
if (BuildGenKillSet) {
501+
if (isBuildingGenKillSet(Kind)) {
495502
for (unsigned i = 0; i < S->MemLocationNum; ++i) {
496503
if (MemLocationVault[i].getBase().getDef() != I)
497504
continue;
@@ -574,7 +581,7 @@ void DSEContext::updateGenKillSetForWrite(BlockState *S, unsigned bit) {
574581
}
575582

576583
void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
577-
bool BuildGenKillSet) {
584+
DSEComputeKind Kind) {
578585
// Construct a MemLocation to represent the memory read by this instruction.
579586
// NOTE: The base will point to the actual object this inst is accessing,
580587
// not this particular field.
@@ -593,7 +600,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
593600
// If we cant figure out the Base or Projection Path for the read instruction,
594601
// process it as an unknown memory instruction for now.
595602
if (!L.isValid()) {
596-
processUnknownReadMemInst(I, BuildGenKillSet);
603+
processUnknownReadMemInst(I, Kind);
597604
return;
598605
}
599606

@@ -610,7 +617,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
610617
// separate reads.
611618
MemLocationList Locs;
612619
MemLocation::expand(L, &I->getModule(), Locs, TypeExpansionVault);
613-
if (BuildGenKillSet) {
620+
if (isBuildingGenKillSet(Kind)) {
614621
for (auto &E : Locs) {
615622
// Only building the gen and kill sets for now.
616623
updateGenKillSetForRead(S, getMemLocationBit(E));
@@ -625,7 +632,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
625632
}
626633

627634
void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
628-
SILValue Mem, bool BuildGenKillSet) {
635+
SILValue Mem, DSEComputeKind Kind) {
629636
// Construct a MemLocation to represent the memory read by this instruction.
630637
// NOTE: The base will point to the actual object this inst is accessing,
631638
// not this particular field.
@@ -662,7 +669,7 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
662669
MemLocationList Locs;
663670
MemLocation::expand(L, Mod, Locs, TypeExpansionVault);
664671
llvm::BitVector V(Locs.size());
665-
if (BuildGenKillSet) {
672+
if (isBuildingGenKillSet(Kind)) {
666673
for (auto &E : Locs) {
667674
// Only building the gen and kill sets here.
668675
updateGenKillSetForWrite(S, getMemLocationBit(E));
@@ -680,7 +687,7 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
680687
}
681688

682689
// Data flow has not stablized, do not perform the DSE just yet.
683-
if (BuildGenKillSet)
690+
if (isBuildingGenKillSet(Kind))
684691
return;
685692

686693
// Fully dead store - stores to all the components are dead, therefore this
@@ -739,15 +746,15 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
739746
}
740747
}
741748

742-
void DSEContext::processLoadInst(SILInstruction *I, bool BuildGenKillSet) {
749+
void DSEContext::processLoadInst(SILInstruction *I, DSEComputeKind Kind) {
743750
SILValue Mem = cast<LoadInst>(I)->getOperand();
744-
processRead(I, getBlockState(I), Mem, BuildGenKillSet);
751+
processRead(I, getBlockState(I), Mem, Kind);
745752
}
746753

747-
void DSEContext::processStoreInst(SILInstruction *I, bool BuildGenKillSet) {
754+
void DSEContext::processStoreInst(SILInstruction *I, DSEComputeKind Kind) {
748755
SILValue Val = cast<StoreInst>(I)->getSrc();
749756
SILValue Mem = cast<StoreInst>(I)->getDest();
750-
processWrite(I, getBlockState(I), Val, Mem, BuildGenKillSet);
757+
processWrite(I, getBlockState(I), Val, Mem, Kind);
751758
}
752759

753760
void DSEContext::processDebugValueAddrInst(SILInstruction *I) {
@@ -763,10 +770,10 @@ void DSEContext::processDebugValueAddrInst(SILInstruction *I) {
763770
}
764771

765772
void DSEContext::processUnknownReadMemInst(SILInstruction *I,
766-
bool BuildGenKillSet) {
773+
DSEComputeKind Kind) {
767774
BlockState *S = getBlockState(I);
768775
// Update the gen kill set.
769-
if (BuildGenKillSet) {
776+
if (isBuildingGenKillSet(Kind)) {
770777
for (unsigned i = 0; i < S->MemLocationNum; ++i) {
771778
if (!AA->mayReadFromMemory(I, MemLocationVault[i].getBase()))
772779
continue;
@@ -788,7 +795,7 @@ void DSEContext::processUnknownReadMemInst(SILInstruction *I,
788795
}
789796
}
790797

791-
void DSEContext::processInstruction(SILInstruction *I, bool BuildGenKillSet) {
798+
void DSEContext::processInstruction(SILInstruction *I, DSEComputeKind Kind) {
792799
// If this instruction has side effects, but is inert from a store
793800
// perspective, skip it.
794801
if (isDeadStoreInertInstruction(I))
@@ -799,17 +806,17 @@ void DSEContext::processInstruction(SILInstruction *I, bool BuildGenKillSet) {
799806
// TODO: process more instructions.
800807
//
801808
if (isa<LoadInst>(I)) {
802-
processLoadInst(I, BuildGenKillSet);
809+
processLoadInst(I, Kind);
803810
} else if (isa<StoreInst>(I)) {
804-
processStoreInst(I, BuildGenKillSet);
811+
processStoreInst(I, Kind);
805812
} else if (isa<DebugValueAddrInst>(I)) {
806813
processDebugValueAddrInst(I);
807814
} else if (I->mayReadFromMemory()) {
808-
processUnknownReadMemInst(I, BuildGenKillSet);
815+
processUnknownReadMemInst(I, Kind);
809816
}
810817

811818
// Check whether this instruction will invalidate any other MemLocations.
812-
invalidateMemLocationBase(I, BuildGenKillSet);
819+
invalidateMemLocationBase(I, Kind);
813820
}
814821

815822
SILValue DSEContext::createExtract(SILValue Base,

0 commit comments

Comments
 (0)