Skip to content

Commit dc09c65

Browse files
committed
LoopIdiomRecognize: use ExpandedValuesCleaner in another place
This is a necessary cleanup after having expanded a SCEV. See: https://reviews.llvm.org/D84071#inline-774728 Differential Revision: https://reviews.llvm.org/D84174
1 parent 4d75cc4 commit dc09c65

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,31 @@ static void deleteDeadInstruction(Instruction *I) {
295295
I->eraseFromParent();
296296
}
297297

298+
namespace {
299+
class ExpandedValuesCleaner {
300+
SCEVExpander &Expander;
301+
TargetLibraryInfo *TLI;
302+
SmallVector<Value *, 4> ExpandedValues;
303+
bool Commit = false;
304+
305+
public:
306+
ExpandedValuesCleaner(SCEVExpander &Expander, TargetLibraryInfo *TLI)
307+
: Expander(Expander), TLI(TLI) {}
308+
309+
void add(Value *V) { ExpandedValues.push_back(V); }
310+
311+
void commit() { Commit = true; }
312+
313+
~ExpandedValuesCleaner() {
314+
if (!Commit) {
315+
Expander.clear();
316+
for (auto *V : ExpandedValues)
317+
RecursivelyDeleteTriviallyDeadInstructions(V, TLI);
318+
}
319+
}
320+
};
321+
} // namespace
322+
298323
//===----------------------------------------------------------------------===//
299324
//
300325
// Implementation of LoopIdiomRecognize
@@ -908,6 +933,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
908933
BasicBlock *Preheader = CurLoop->getLoopPreheader();
909934
IRBuilder<> Builder(Preheader->getTerminator());
910935
SCEVExpander Expander(*SE, *DL, "loop-idiom");
936+
ExpandedValuesCleaner EVC(Expander, TLI);
911937

912938
Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
913939
Type *IntIdxTy = DL->getIndexType(DestPtr->getType());
@@ -930,6 +956,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
930956
// base pointer and checking the region.
931957
Value *BasePtr =
932958
Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
959+
EVC.add(BasePtr);
933960

934961
// From here on out, conservatively report to the pass manager that we've
935962
// changed the IR, even if we later clean up these added instructions. There
@@ -941,12 +968,8 @@ bool LoopIdiomRecognize::processLoopStridedStore(
941968
Changed = true;
942969

943970
if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount,
944-
StoreSize, *AA, Stores)) {
945-
Expander.clear();
946-
// If we generated new code for the base pointer, clean up.
947-
RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI);
971+
StoreSize, *AA, Stores))
948972
return Changed;
949-
}
950973

951974
if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset))
952975
return Changed;
@@ -1018,34 +1041,10 @@ bool LoopIdiomRecognize::processLoopStridedStore(
10181041
if (MSSAU && VerifyMemorySSA)
10191042
MSSAU->getMemorySSA()->verifyMemorySSA();
10201043
++NumMemSet;
1044+
EVC.commit();
10211045
return true;
10221046
}
10231047

1024-
namespace {
1025-
class ExpandedValuesCleaner {
1026-
SCEVExpander &Expander;
1027-
TargetLibraryInfo *TLI;
1028-
SmallVector<Value *, 4> ExpandedValues;
1029-
bool Commit = false;
1030-
1031-
public:
1032-
ExpandedValuesCleaner(SCEVExpander &Expander, TargetLibraryInfo *TLI)
1033-
: Expander(Expander), TLI(TLI) {}
1034-
1035-
void add(Value *V) { ExpandedValues.push_back(V); }
1036-
1037-
void commit() { Commit = true; }
1038-
1039-
~ExpandedValuesCleaner() {
1040-
if (!Commit) {
1041-
Expander.clear();
1042-
for (auto *V : ExpandedValues)
1043-
RecursivelyDeleteTriviallyDeadInstructions(V, TLI);
1044-
}
1045-
}
1046-
};
1047-
} // namespace
1048-
10491048
/// If the stored value is a strided load in the same loop with the same stride
10501049
/// this may be transformable into a memcpy. This kicks in for stuff like
10511050
/// for (i) A[i] = B[i];

0 commit comments

Comments
 (0)