Skip to content

Commit 9aa3111

Browse files
jgu222igcbot
authored andcommitted
Better way to delete dead insts
Add a new field m_toBeDeleted, which has all dead root instructions. A dead root instruction is one whose use is empty, this include store instruction. No functional change
1 parent 2432e58 commit 9aa3111

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

IGC/Compiler/CISACodeGen/MemOpt.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,7 @@ namespace {
24032403
uint32_t getAlignment() const;
24042404
AddressModel getAddressModel(CodeGenContext* Ctx) const;
24052405
Value* getValueOperand() const;
2406+
bool isStore() const { return isa<StoreInst>(Inst); }
24062407
};
24072408

24082409
typedef SmallVector<LdStInfo, 8> InstAndOffsetPairs;
@@ -2634,23 +2635,32 @@ namespace {
26342635
// If true, IGC needs to emulate I64.
26352636
bool m_hasI64Emu = false;
26362637

2637-
// All insts that have been combined and will be deleted at the end.
2638-
SmallVector<Instruction*, 16> m_combinedInsts;
26392638
//
26402639
// Temporary reused for each BB.
26412640
//
26422641
// Inst order within a BB.
26432642
DenseMap<const Instruction*, int> m_instOrder;
2643+
// Per-BB: all insts that have been combined and will be deleted.
2644+
DenseMap<const Instruction*, int> m_combinedInsts;
2645+
// All root instructions (ie their uses are empty, including stores)
2646+
// that are to be deleted at the end of each BB.
2647+
SmallVector<Instruction*, 16> m_toBeDeleted;
2648+
void appendToBeDeleted(Instruction* I) {
2649+
if (I != nullptr)
2650+
m_toBeDeleted.push_back(I);
2651+
}
2652+
// Control the way that a load/store is handled.
2653+
// [more for future improvement]
2654+
DenseMap<const Instruction*, int> m_visited;
26442655

26452656
// a bundle : a group of loads or a group of store.
26462657
// Each bundle will be combined into a single load or single store.
26472658
std::list<BundleInfo> m_bundles;
26482659

2649-
DenseMap<const Instruction*, int> m_visited;
2650-
26512660
void init(BasicBlock* BB) {
26522661
m_visited.clear();
26532662
m_instOrder.clear();
2663+
m_combinedInsts.clear();
26542664
}
26552665
void setInstOrder(BasicBlock* BB);
26562666
void setVisited(Instruction* I) { m_visited[I] = 1; }
@@ -3499,13 +3509,13 @@ void LdStCombine::createBundles(BasicBlock* BB, InstAndOffsetPairs& LoadStores)
34993509
return;
35003510
}
35013511

3502-
auto isBundled = [](const LdStInfo* LSI, SmallVector<Instruction*, 16>& L) {
3503-
return (std::find(L.begin(), L.end(), LSI->Inst) != L.end());
3512+
auto isBundled = [](const LdStInfo* LSI, DenseMap<const Instruction*, int>& L) {
3513+
return (L.count(LSI->Inst) > 0);
35043514
};
35053515
auto setBundled = [&isBundled](LdStInfo* LSI,
3506-
SmallVector<Instruction*, 16>& L) {
3516+
DenseMap<const Instruction*, int>& L) {
35073517
if (!isBundled(LSI, L)) {
3508-
L.push_back(LSI->Inst);
3518+
L[LSI->Inst] = 1;
35093519
}
35103520
};
35113521

@@ -3666,6 +3676,9 @@ void LdStCombine::createBundles(BasicBlock* BB, InstAndOffsetPairs& LoadStores)
36663676
LdStInfo& tlsi = LoadStores[k];
36673677
newBundle.LoadStores.push_back(tlsi);
36683678
setBundled(&tlsi, m_combinedInsts);
3679+
if (tlsi.isStore()) {
3680+
appendToBeDeleted(tlsi.Inst);
3681+
}
36693682
setVisited(tlsi.Inst);
36703683
}
36713684
i = e + 1;
@@ -4531,14 +4544,7 @@ void LdStCombine::scatterCopy(
45314544
Type* aTy = nV->getType();
45324545
Value* newV = getValueFromStruct(aTy);
45334546
nV->replaceAllUsesWith(newV);
4534-
4535-
// V (all Vals) will be deleted at the end. Here, only delete
4536-
// its uses so that V will have empty use later.
4537-
if (Instruction* tI = dyn_cast<Instruction>(nV)) {
4538-
if (tI != V) {
4539-
tI->eraseFromParent();
4540-
}
4541-
}
4547+
appendToBeDeleted(dyn_cast<Instruction>(nV));
45424548
}
45434549
}
45444550
} else {
@@ -4595,14 +4601,7 @@ void LdStCombine::scatterCopy(
45954601
}
45964602
Value* newV = createValueFromElements(vecElts, aTy);
45974603
nV->replaceAllUsesWith(newV);
4598-
4599-
// V (all Vals) will be deleted at the end.
4600-
// Here, just delete its uses so that V will have empty use.
4601-
if (Instruction* tI = dyn_cast<Instruction>(nV)) {
4602-
if (tI != V) {
4603-
tI->eraseFromParent();
4604-
}
4605-
}
4604+
appendToBeDeleted(dyn_cast<Instruction>(nV));
46064605
}
46074606
}
46084607
}
@@ -4868,8 +4867,8 @@ void LdStCombine::createCombinedLoads(Function& F)
48684867

48694868
void LdStCombine::eraseDeadInsts()
48704869
{
4871-
RecursivelyDeleteDeadInstructions(m_combinedInsts);
4872-
m_combinedInsts.clear();
4870+
RecursivelyDeleteDeadInstructions(m_toBeDeleted);
4871+
m_toBeDeleted.clear();
48734872
}
48744873

48754874
void BundleInfo::print(raw_ostream& O, int BundleID) const

0 commit comments

Comments
 (0)