@@ -295,6 +295,31 @@ static void deleteDeadInstruction(Instruction *I) {
295
295
I->eraseFromParent ();
296
296
}
297
297
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
+
298
323
// ===----------------------------------------------------------------------===//
299
324
//
300
325
// Implementation of LoopIdiomRecognize
@@ -908,7 +933,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
908
933
BasicBlock *Preheader = CurLoop->getLoopPreheader ();
909
934
IRBuilder<> Builder (Preheader->getTerminator ());
910
935
SCEVExpander Expander (*SE, *DL, " loop-idiom" );
911
- SCEVExpanderCleaner ExpCleaner (Expander, *DT );
936
+ ExpandedValuesCleaner EVC (Expander, TLI );
912
937
913
938
Type *DestInt8PtrTy = Builder.getInt8PtrTy (DestAS);
914
939
Type *IntIdxTy = DL->getIndexType (DestPtr->getType ());
@@ -931,6 +956,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
931
956
// base pointer and checking the region.
932
957
Value *BasePtr =
933
958
Expander.expandCodeFor (Start, DestInt8PtrTy, Preheader->getTerminator ());
959
+ EVC.add (BasePtr);
934
960
935
961
// From here on out, conservatively report to the pass manager that we've
936
962
// changed the IR, even if we later clean up these added instructions. There
@@ -1015,7 +1041,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
1015
1041
if (MSSAU && VerifyMemorySSA)
1016
1042
MSSAU->getMemorySSA ()->verifyMemorySSA ();
1017
1043
++NumMemSet;
1018
- ExpCleaner. markResultUsed ();
1044
+ EVC. commit ();
1019
1045
return true ;
1020
1046
}
1021
1047
@@ -1049,7 +1075,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1049
1075
IRBuilder<> Builder (Preheader->getTerminator ());
1050
1076
SCEVExpander Expander (*SE, *DL, " loop-idiom" );
1051
1077
1052
- SCEVExpanderCleaner ExpCleaner (Expander, *DT );
1078
+ ExpandedValuesCleaner EVC (Expander, TLI );
1053
1079
1054
1080
bool Changed = false ;
1055
1081
const SCEV *StrStart = StoreEv->getStart ();
@@ -1068,6 +1094,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1068
1094
// checking everything.
1069
1095
Value *StoreBasePtr = Expander.expandCodeFor (
1070
1096
StrStart, Builder.getInt8PtrTy (StrAS), Preheader->getTerminator ());
1097
+ EVC.add (StoreBasePtr);
1071
1098
1072
1099
// From here on out, conservatively report to the pass manager that we've
1073
1100
// changed the IR, even if we later clean up these added instructions. There
@@ -1095,6 +1122,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1095
1122
// mutated by the loop.
1096
1123
Value *LoadBasePtr = Expander.expandCodeFor (
1097
1124
LdStart, Builder.getInt8PtrTy (LdAS), Preheader->getTerminator ());
1125
+ EVC.add (LoadBasePtr);
1098
1126
1099
1127
if (mayLoopAccessLocation (LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
1100
1128
StoreSize, *AA, Stores))
@@ -1110,6 +1138,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1110
1138
1111
1139
Value *NumBytes =
1112
1140
Expander.expandCodeFor (NumBytesS, IntIdxTy, Preheader->getTerminator ());
1141
+ EVC.add (NumBytes);
1113
1142
1114
1143
CallInst *NewCall = nullptr ;
1115
1144
// Check whether to generate an unordered atomic memcpy:
@@ -1169,7 +1198,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1169
1198
if (MSSAU && VerifyMemorySSA)
1170
1199
MSSAU->getMemorySSA ()->verifyMemorySSA ();
1171
1200
++NumMemCpy;
1172
- ExpCleaner. markResultUsed ();
1201
+ EVC. commit ();
1173
1202
return true ;
1174
1203
}
1175
1204
0 commit comments