Skip to content

Commit 3888464

Browse files
committed
Temporarily revert "[SCEVExpander] Add helper to clean up instrs inserted while expanding."
This reverts commit 7829c33. The assertion is triggering on some internal code. A reduced test case is in progress.
1 parent 6dbf0cf commit 3888464

File tree

3 files changed

+34
-81
lines changed

3 files changed

+34
-81
lines changed

llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,6 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
180180
ChainedPhis.clear();
181181
}
182182

183-
/// Return a vector containing all instructions inserted during expansion.
184-
SmallVector<Instruction *, 32> getAllInsertedInstructions() const {
185-
SmallVector<Instruction *, 32> Result;
186-
for (auto &VH : InsertedValues) {
187-
Value *V = VH;
188-
if (auto *Inst = dyn_cast<Instruction>(V))
189-
Result.push_back(Inst);
190-
}
191-
for (auto &VH : InsertedPostIncValues) {
192-
Value *V = VH;
193-
if (auto *Inst = dyn_cast<Instruction>(V))
194-
Result.push_back(Inst);
195-
}
196-
197-
return Result;
198-
}
199-
200183
/// Return true for expressions that can't be evaluated at runtime
201184
/// within given \b Budget.
202185
///
@@ -469,27 +452,6 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
469452
/// If no PHIs have been created, return the unchanged operand \p OpIdx.
470453
Value *fixupLCSSAFormFor(Instruction *User, unsigned OpIdx);
471454
};
472-
473-
/// Helper to remove instructions inserted during SCEV expansion, unless they
474-
/// are marked as used.
475-
class SCEVExpanderCleaner {
476-
SCEVExpander &Expander;
477-
478-
DominatorTree &DT;
479-
480-
/// Indicates whether the result of the expansion is used. If false, the
481-
/// instructions added during expansion are removed.
482-
bool ResultUsed;
483-
484-
public:
485-
SCEVExpanderCleaner(SCEVExpander &Expander, DominatorTree &DT)
486-
: Expander(Expander), DT(DT), ResultUsed(false) {}
487-
488-
~SCEVExpanderCleaner();
489-
490-
/// Indicate that the result of the expansion is used.
491-
void markResultUsed() { ResultUsed = true; }
492-
};
493455
} // namespace llvm
494456

495457
#endif

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 33 additions & 4 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,7 +933,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
908933
BasicBlock *Preheader = CurLoop->getLoopPreheader();
909934
IRBuilder<> Builder(Preheader->getTerminator());
910935
SCEVExpander Expander(*SE, *DL, "loop-idiom");
911-
SCEVExpanderCleaner ExpCleaner(Expander, *DT);
936+
ExpandedValuesCleaner EVC(Expander, TLI);
912937

913938
Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
914939
Type *IntIdxTy = DL->getIndexType(DestPtr->getType());
@@ -931,6 +956,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
931956
// base pointer and checking the region.
932957
Value *BasePtr =
933958
Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
959+
EVC.add(BasePtr);
934960

935961
// From here on out, conservatively report to the pass manager that we've
936962
// changed the IR, even if we later clean up these added instructions. There
@@ -1015,7 +1041,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
10151041
if (MSSAU && VerifyMemorySSA)
10161042
MSSAU->getMemorySSA()->verifyMemorySSA();
10171043
++NumMemSet;
1018-
ExpCleaner.markResultUsed();
1044+
EVC.commit();
10191045
return true;
10201046
}
10211047

@@ -1049,7 +1075,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
10491075
IRBuilder<> Builder(Preheader->getTerminator());
10501076
SCEVExpander Expander(*SE, *DL, "loop-idiom");
10511077

1052-
SCEVExpanderCleaner ExpCleaner(Expander, *DT);
1078+
ExpandedValuesCleaner EVC(Expander, TLI);
10531079

10541080
bool Changed = false;
10551081
const SCEV *StrStart = StoreEv->getStart();
@@ -1068,6 +1094,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
10681094
// checking everything.
10691095
Value *StoreBasePtr = Expander.expandCodeFor(
10701096
StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator());
1097+
EVC.add(StoreBasePtr);
10711098

10721099
// From here on out, conservatively report to the pass manager that we've
10731100
// changed the IR, even if we later clean up these added instructions. There
@@ -1095,6 +1122,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
10951122
// mutated by the loop.
10961123
Value *LoadBasePtr = Expander.expandCodeFor(
10971124
LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator());
1125+
EVC.add(LoadBasePtr);
10981126

10991127
if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
11001128
StoreSize, *AA, Stores))
@@ -1110,6 +1138,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
11101138

11111139
Value *NumBytes =
11121140
Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator());
1141+
EVC.add(NumBytes);
11131142

11141143
CallInst *NewCall = nullptr;
11151144
// Check whether to generate an unordered atomic memcpy:
@@ -1169,7 +1198,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
11691198
if (MSSAU && VerifyMemorySSA)
11701199
MSSAU->getMemorySSA()->verifyMemorySSA();
11711200
++NumMemCpy;
1172-
ExpCleaner.markResultUsed();
1201+
EVC.commit();
11731202
return true;
11741203
}
11751204

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,12 +1882,10 @@ Value *SCEVExpander::expand(const SCEV *S) {
18821882
// there) so that it is guaranteed to dominate any user inside the loop.
18831883
if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
18841884
InsertPt = &*L->getHeader()->getFirstInsertionPt();
1885-
18861885
while (InsertPt->getIterator() != Builder.GetInsertPoint() &&
18871886
(isInsertedInstruction(InsertPt) ||
1888-
isa<DbgInfoIntrinsic>(InsertPt))) {
1887+
isa<DbgInfoIntrinsic>(InsertPt)))
18891888
InsertPt = &*std::next(InsertPt->getIterator());
1890-
}
18911889
break;
18921890
}
18931891
}
@@ -2632,40 +2630,4 @@ bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint,
26322630
}
26332631
return false;
26342632
}
2635-
2636-
SCEVExpanderCleaner::~SCEVExpanderCleaner() {
2637-
// Result is used, nothing to remove.
2638-
if (ResultUsed)
2639-
return;
2640-
2641-
auto InsertedInstructions = Expander.getAllInsertedInstructions();
2642-
#ifndef NDEBUG
2643-
SmallPtrSet<Instruction *, 8> InsertedSet(InsertedInstructions.begin(),
2644-
InsertedInstructions.end());
2645-
(void)InsertedSet;
2646-
#endif
2647-
// Remove sets with value handles.
2648-
Expander.clear();
2649-
2650-
// Sort so that earlier instructions do not dominate later instructions.
2651-
stable_sort(InsertedInstructions, [this](Instruction *A, Instruction *B) {
2652-
return DT.dominates(B, A);
2653-
});
2654-
// Remove all inserted instructions.
2655-
for (Instruction *I : InsertedInstructions) {
2656-
2657-
#ifndef NDEBUG
2658-
assert(all_of(I->users(),
2659-
[&InsertedSet](Value *U) {
2660-
return InsertedSet.contains(cast<Instruction>(U));
2661-
}) &&
2662-
"removed instruction should only be used by instructions inserted "
2663-
"during expansion");
2664-
#endif
2665-
assert(!I->getType()->isVoidTy() &&
2666-
"inserted instruction should have non-void types");
2667-
I->replaceAllUsesWith(UndefValue::get(I->getType()));
2668-
I->eraseFromParent();
2669-
}
2670-
}
26712633
}

0 commit comments

Comments
 (0)