Skip to content

Commit 4615a50

Browse files
author
Max Kazantsev
committed
[IPT] Drop cache less eagerly in GVN and LoopSafetyInfo
Current strategy of dropping `InstructionPrecedenceTracking` cache is to invalidate the entire basic block whenever we change its contents. In fact, `InstructionPrecedenceTracking` has 2 internal strictures: `OrderedInstructions` that is needed to be invalidated whenever the contents changes, and the map with first special instructions in block. This second map does not need an update if we add/remove a non-special instuction because it cannot affect the contents of this map. This patch changes API of `InstructionPrecedenceTracking` so that it now accounts for reasons under which we invalidate blocks. This should lead to much less recalculations of the map and should save us some compile time because in practice we don't typically add/remove special instructions. Differential Revision: https://reviews.llvm.org/D54462 Reviewed By: efriedma llvm-svn: 350694
1 parent f2a75ee commit 4615a50

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ class InstructionPrecedenceTracking {
7575
virtual ~InstructionPrecedenceTracking() = default;
7676

7777
public:
78-
/// Clears cached information about this particular block.
79-
void invalidateBlock(const BasicBlock *BB);
78+
/// Notifies this tracking that we are going to insert a new instruction \p
79+
/// Inst to the basic block \p BB. It makes all necessary updates to internal
80+
/// caches to keep them consistent.
81+
void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB);
82+
83+
/// Notifies this tracking that we are going to remove the instruction \p Inst
84+
/// It makes all necessary updates to internal caches to keep them consistent.
85+
void removeInstruction(const Instruction *Inst);
8086

8187
/// Invalidates all information from this tracking.
8288
void clear();

llvm/include/llvm/Analysis/MustExecute.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class ICFLoopSafetyInfo: public LoopSafetyInfo {
151151
const;
152152

153153
/// Inform the safety info that we are planning to insert a new instruction
154-
/// into the basic block \p BB. It will make all cache updates to keep it
155-
/// correct after this insertion.
156-
void insertInstructionTo(const BasicBlock *BB);
154+
/// \p Inst into the basic block \p BB. It will make all cache updates to keep
155+
/// it correct after this insertion.
156+
void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB);
157157

158158
/// Inform safety info that we are planning to remove the instruction \p Inst
159159
/// from its block. It will make all cache updates to keep it correct after

llvm/lib/Analysis/InstructionPrecedenceTracking.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ void InstructionPrecedenceTracking::validateAll() const {
9999
}
100100
#endif
101101

102-
void InstructionPrecedenceTracking::invalidateBlock(const BasicBlock *BB) {
102+
void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
103+
const BasicBlock *BB) {
104+
if (isSpecialInstruction(Inst))
105+
FirstSpecialInsts.erase(BB);
103106
OI.invalidateBlock(BB);
104-
FirstSpecialInsts.erase(BB);
107+
}
108+
109+
void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
110+
if (isSpecialInstruction(Inst))
111+
FirstSpecialInsts.erase(Inst->getParent());
112+
OI.invalidateBlock(Inst->getParent());
105113
}
106114

107115
void InstructionPrecedenceTracking::clear() {

llvm/lib/Analysis/MustExecute.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,15 @@ void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
8383
computeBlockColors(CurLoop);
8484
}
8585

86-
void ICFLoopSafetyInfo::insertInstructionTo(const BasicBlock *BB) {
87-
ICF.invalidateBlock(BB);
88-
MW.invalidateBlock(BB);
86+
void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst,
87+
const BasicBlock *BB) {
88+
ICF.insertInstructionTo(Inst, BB);
89+
MW.insertInstructionTo(Inst, BB);
8990
}
9091

9192
void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
92-
// TODO: So far we just conservatively drop cache, but maybe we can not do it
93-
// when Inst is not an ICF instruction. Follow-up on that.
94-
ICF.invalidateBlock(Inst->getParent());
95-
MW.invalidateBlock(Inst->getParent());
93+
ICF.removeInstruction(Inst);
94+
MW.removeInstruction(Inst);
9695
}
9796

9897
void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,10 +2079,9 @@ bool GVN::processBlock(BasicBlock *BB) {
20792079
salvageDebugInfo(*I);
20802080
if (MD) MD->removeInstruction(I);
20812081
LLVM_DEBUG(verifyRemoved(I));
2082+
ICF->removeInstruction(I);
20822083
I->eraseFromParent();
20832084
}
2084-
2085-
ICF->invalidateBlock(BB);
20862085
InstrsToErase.clear();
20872086

20882087
if (AtStart)
@@ -2301,7 +2300,7 @@ bool GVN::performScalarPRE(Instruction *CurInst) {
23012300
LLVM_DEBUG(verifyRemoved(CurInst));
23022301
// FIXME: Intended to be markInstructionForDeletion(CurInst), but it causes
23032302
// some assertion failures.
2304-
ICF->invalidateBlock(CurrentBlock);
2303+
ICF->removeInstruction(CurInst);
23052304
CurInst->eraseFromParent();
23062305
++NumGVNInstr;
23072306

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,13 @@ bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
742742
auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0);
743743
auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor);
744744
ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags());
745-
SafetyInfo->insertInstructionTo(I.getParent());
745+
SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent());
746746
ReciprocalDivisor->insertBefore(&I);
747747

748748
auto Product =
749749
BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor);
750750
Product->setFastMathFlags(I.getFastMathFlags());
751-
SafetyInfo->insertInstructionTo(I.getParent());
751+
SafetyInfo->insertInstructionTo(Product, I.getParent());
752752
Product->insertAfter(&I);
753753
I.replaceAllUsesWith(Product);
754754
eraseInstruction(I, *SafetyInfo, CurAST);
@@ -1189,7 +1189,7 @@ static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo,
11891189
static void moveInstructionBefore(Instruction &I, Instruction &Dest,
11901190
ICFLoopSafetyInfo &SafetyInfo) {
11911191
SafetyInfo.removeInstruction(&I);
1192-
SafetyInfo.insertInstructionTo(Dest.getParent());
1192+
SafetyInfo.insertInstructionTo(&I, Dest.getParent());
11931193
I.moveBefore(&Dest);
11941194
}
11951195

0 commit comments

Comments
 (0)