Skip to content

Commit 7108be1

Browse files
committed
SILOptimizer: Use BasicBlockData in RedundantLoadElimination and DeadStoreElimination
1 parent 55761fa commit 7108be1

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

lib/SILOptimizer/Transforms/DeadStoreElimination.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
6969
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
7070
#include "swift/SILOptimizer/Utils/LoadStoreOptUtils.h"
71+
#include "swift/SIL/BasicBlockData.h"
7172
#include "llvm/ADT/BitVector.h"
7273
#include "llvm/ADT/DenseSet.h"
7374
#include "llvm/ADT/Statistic.h"
@@ -215,10 +216,10 @@ class DSEContext;
215216
class BlockState {
216217
public:
217218
/// The basic block this BlockState represents.
218-
SILBasicBlock *BB;
219+
SILBasicBlock *BB = nullptr;
219220

220221
/// Keep the number of LSLocations in the LocationVault.
221-
unsigned LocationNum;
222+
unsigned LocationNum = 0;
222223

223224
/// A bit vector for which the ith bit represents the ith LSLocation in
224225
/// LocationVault. If the bit is set, then the location currently has an
@@ -267,15 +268,12 @@ class BlockState {
267268
llvm::DenseMap<SILValue, SILValue> LiveStores;
268269

269270
/// Constructors.
270-
BlockState(SILBasicBlock *B, unsigned LocationNum, bool Optimistic)
271-
: BB(B), LocationNum(LocationNum) {
272-
init(LocationNum, Optimistic);
273-
}
271+
BlockState() = default;
274272

275273
void dump();
276274

277275
/// Initialize the bitvectors for the current basic block.
278-
void init(unsigned LocationNum, bool Optimistic);
276+
void init(SILBasicBlock *BB, unsigned LocationNum, bool Optimistic);
279277

280278
/// Check whether the BBWriteSetIn has changed. If it does, we need to rerun
281279
/// the data flow on this block's predecessors to reach fixed point.
@@ -345,11 +343,8 @@ enum class ProcessKind {
345343
/// Epilogue release analysis.
346344
EpilogueARCFunctionInfo *EAFI;
347345

348-
/// The allocator we are using.
349-
llvm::SpecificBumpPtrAllocator<BlockState> &BPA;
350-
351346
/// Map every basic block to its location state.
352-
llvm::SmallDenseMap<SILBasicBlock *, BlockState *> BBToLocState;
347+
BasicBlockData<BlockState> BBToLocState;
353348

354349
/// Keeps all the locations for the current function. The BitVector in each
355350
/// BlockState is then laid on top of it to keep track of which LSLocation
@@ -374,7 +369,7 @@ enum class ProcessKind {
374369
LSLocationBaseMap BaseToLocIndex;
375370

376371
/// Return the BlockState for the basic block this basic block belongs to.
377-
BlockState *getBlockState(SILBasicBlock *B) { return BBToLocState[B]; }
372+
BlockState *getBlockState(SILBasicBlock *B) { return &BBToLocState[B]; }
378373

379374
/// Return the BlockState for the basic block this instruction belongs to.
380375
BlockState *getBlockState(SILInstruction *I) {
@@ -451,7 +446,7 @@ enum class ProcessKind {
451446
AliasAnalysis *AA, TypeExpansionAnalysis *TE,
452447
EpilogueARCFunctionInfo *EAFI,
453448
llvm::SpecificBumpPtrAllocator<BlockState> &BPA)
454-
: Mod(M), F(F), PM(PM), AA(AA), TE(TE), EAFI(EAFI), BPA(BPA) {}
449+
: Mod(M), F(F), PM(PM), AA(AA), TE(TE), EAFI(EAFI), BBToLocState(F) {}
455450

456451
void dump();
457452

@@ -494,7 +489,10 @@ void BlockState::dump() {
494489
<< ", gen=" << BBGenSet << ", kill=" << BBKillSet << '\n';
495490
}
496491

497-
void BlockState::init(unsigned LocationNum, bool Optimistic) {
492+
void BlockState::init(SILBasicBlock *BB, unsigned LocationNum, bool Optimistic) {
493+
this->BB = BB;
494+
this->LocationNum = LocationNum;
495+
498496
// For function that requires just 1 iteration of the data flow to converge
499497
// we set the initial state of BBWriteSetIn to 0.
500498
//
@@ -1209,10 +1207,9 @@ bool DSEContext::run() {
12091207
//
12101208
// Initialize the BBToLocState mapping.
12111209
unsigned LocationNum = this->getLocationVault().size();
1212-
for (auto &B : *F) {
1213-
auto *State = new (BPA.Allocate()) BlockState(&B, LocationNum, Optimistic);
1214-
BBToLocState[&B] = State;
1215-
State->initStoreSetAtEndOfBlock(*this);
1210+
for (auto bs : BBToLocState) {
1211+
bs.data.init(&bs.block, LocationNum, Optimistic);
1212+
bs.data.initStoreSetAtEndOfBlock(*this);
12161213
}
12171214

12181215
// We perform dead store elimination in the following phases.
@@ -1247,20 +1244,19 @@ bool DSEContext::run() {
12471244

12481245
// Finally, delete the dead stores and create the live stores.
12491246
bool Changed = false;
1250-
for (SILBasicBlock &BB : *F) {
1247+
for (auto bs : BBToLocState) {
12511248
// Create the stores that are alive due to partial dead stores.
1252-
auto *S = getBlockState(&BB);
1253-
for (auto &X : S->LiveAddr) {
1249+
for (auto &X : bs.data.LiveAddr) {
12541250
Changed = true;
1255-
auto I = S->LiveStores.find(X);
1251+
auto I = bs.data.LiveStores.find(X);
12561252
SILInstruction *Inst = I->first->getDefiningInstruction();
12571253
auto *IT = &*std::next(Inst->getIterator());
12581254
SILBuilderWithScope Builder(IT);
12591255
Builder.createStore(Inst->getLoc(), I->second, I->first,
12601256
StoreOwnershipQualifier::Unqualified);
12611257
}
12621258
// Delete the dead stores.
1263-
for (auto &I : getBlockState(&BB)->DeadStores) {
1259+
for (auto &I : getBlockState(&bs.block)->DeadStores) {
12641260
Changed = true;
12651261
LLVM_DEBUG(llvm::dbgs() << "*** Removing: " << *I << " ***\n");
12661262
// This way, we get rid of pass dependence on DCE.

lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
8888
#include "swift/SILOptimizer/Utils/LoadStoreOptUtils.h"
8989
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
90+
#include "swift/SIL/BasicBlockData.h"
9091
#include "llvm/ADT/BitVector.h"
9192
#include "llvm/ADT/MapVector.h"
9293
#include "llvm/ADT/None.h"
@@ -468,7 +469,7 @@ class RLEContext {
468469
llvm::DenseMap<LSValue, unsigned> ValToBitIndex;
469470

470471
/// A map from each BasicBlock to its BlockState.
471-
llvm::SmallDenseMap<SILBasicBlock *, BlockState, 16> BBToLocState;
472+
BasicBlockData<BlockState> BBToLocState;
472473

473474
/// Keeps a list of basic blocks that have LoadInsts. If a basic block does
474475
/// not have LoadInst, we do not actually perform the last iteration where
@@ -1211,7 +1212,7 @@ RLEContext::RLEContext(SILFunction *F, SILPassManager *PM, AliasAnalysis *AA,
12111212
TypeExpansionAnalysis *TE, PostOrderFunctionInfo *PO,
12121213
EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads,
12131214
JointPostDominanceSetComputer &computer)
1214-
: Fn(F), PM(PM), AA(AA), TE(TE), PO(PO), EAFI(EAFI),
1215+
: Fn(F), PM(PM), AA(AA), TE(TE), PO(PO), EAFI(EAFI), BBToLocState(F),
12151216
ArrayType(disableArrayLoads
12161217
? F->getModule().getASTContext().getArrayDecl()
12171218
: nullptr),
@@ -1592,10 +1593,9 @@ bool RLEContext::run() {
15921593
// For all basic blocks in the function, initialize a BB state. Since we
15931594
// know all the locations accessed in this function, we can resize the bit
15941595
// vector to the appropriate size.
1595-
for (auto &B : *Fn) {
1596-
BBToLocState[&B] = BlockState();
1597-
BBToLocState[&B].init(&B, LocationVault.size(), Optimistic &&
1598-
BBToProcess.find(&B) != BBToProcess.end());
1596+
for (auto bs : BBToLocState) {
1597+
bs.data.init(&bs.block, LocationVault.size(), Optimistic &&
1598+
BBToProcess.find(&bs.block) != BBToProcess.end());
15991599
}
16001600

16011601
LLVM_DEBUG(for (unsigned i = 0; i < LocationVault.size(); ++i) {
@@ -1613,9 +1613,8 @@ bool RLEContext::run() {
16131613
// Finally, perform the redundant load replacements.
16141614
llvm::SmallVector<SILInstruction *, 16> InstsToDelete;
16151615
bool SILChanged = false;
1616-
for (auto &B : *Fn) {
1617-
auto &State = BBToLocState[&B];
1618-
auto &Loads = State.getRL();
1616+
for (auto bs : BBToLocState) {
1617+
auto &Loads = bs.data.getRL();
16191618
// Nothing to forward.
16201619
if (Loads.empty())
16211620
continue;
@@ -1624,7 +1623,7 @@ bool RLEContext::run() {
16241623
//
16251624
// NOTE: we could end up with different SIL depending on the ordering load
16261625
// forwarding is performed.
1627-
for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) {
1626+
for (auto I = bs.block.rbegin(), E = bs.block.rend(); I != E; ++I) {
16281627
auto V = dyn_cast<SingleValueInstruction>(&*I);
16291628
if (!V)
16301629
continue;

0 commit comments

Comments
 (0)