[llvm] [NFC] [DSE] Refactor DSE (PR #100956)
Haopeng Liu via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 19 10:17:27 PDT 2024
================
@@ -2132,182 +2177,182 @@ struct DSEState {
}
return MadeChange;
}
-};
-static bool eliminateDeadStores(Function &F, AliasAnalysis &AA, MemorySSA &MSSA,
- DominatorTree &DT, PostDominatorTree &PDT,
- const TargetLibraryInfo &TLI,
- const LoopInfo &LI) {
- bool MadeChange = false;
+ // Try to eliminate dead defs killed by `KillingLocWrapper` and return the
+ // change state: whether make any change, and whether make a partial delete
+ // by a non memory-terminator instruction.
+ std::pair<bool, bool>
+ eliminateDeadDefs(MemoryLocationWrapper &KillingLocWrapper);
- DSEState State(F, AA, MSSA, DT, PDT, TLI, LI);
- // For each store:
- for (unsigned I = 0; I < State.MemDefs.size(); I++) {
- MemoryDef *KillingDef = State.MemDefs[I];
- if (State.SkipStores.count(KillingDef))
+ // Try to eliminate dead defs killed by `KillingDefWrapper` and return the
+ // change state: whether make any change.
+ bool eliminateDeadDefs(MemoryDefWrapper &KillingDefWrapper);
+};
+
+std::pair<bool, bool>
+DSEState::eliminateDeadDefs(MemoryLocationWrapper &KillingLocWrapper) {
+ bool Changed = false;
+ bool Shortened = false;
+ unsigned ScanLimit = MemorySSAScanLimit;
+ unsigned WalkerStepLimit = MemorySSAUpwardsStepLimit;
+ unsigned PartialLimit = MemorySSAPartialStoreLimit;
+ // Worklist of MemoryAccesses that may be killed by KillingDef.
+ SmallSetVector<MemoryAccess *, 8> ToCheck;
+ ToCheck.insert(KillingLocWrapper.GetDefiningAccess());
+
+ // Check if MemoryAccesses in the worklist are killed by KillingDef.
+ for (unsigned I = 0; I < ToCheck.size(); I++) {
+ MemoryAccess *Current = ToCheck[I];
+ if (SkipStores.count(Current))
continue;
- Instruction *KillingI = KillingDef->getMemoryInst();
+ std::optional<MemoryAccess *> MaybeDeadAccess = getDomMemoryDef(
+ KillingLocWrapper, Current, ScanLimit, WalkerStepLimit, PartialLimit);
- std::optional<MemoryLocation> MaybeKillingLoc;
- if (State.isMemTerminatorInst(KillingI)) {
- if (auto KillingLoc = State.getLocForTerminator(KillingI))
- MaybeKillingLoc = KillingLoc->first;
- } else {
- MaybeKillingLoc = State.getLocForWrite(KillingI);
+ if (!MaybeDeadAccess) {
+ LLVM_DEBUG(dbgs() << " finished walk\n");
+ continue;
}
-
- if (!MaybeKillingLoc) {
- LLVM_DEBUG(dbgs() << "Failed to find analyzable write location for "
- << *KillingI << "\n");
+ MemoryAccess *DeadAccess = *MaybeDeadAccess;
+ LLVM_DEBUG(dbgs() << " Checking if we can kill " << *DeadAccess);
+ if (isa<MemoryPhi>(DeadAccess)) {
+ LLVM_DEBUG(dbgs() << "\n ... adding incoming values to worklist\n");
+ for (Value *V : cast<MemoryPhi>(DeadAccess)->incoming_values()) {
+ MemoryAccess *IncomingAccess = cast<MemoryAccess>(V);
+ BasicBlock *IncomingBlock = IncomingAccess->getBlock();
+ BasicBlock *PhiBlock = DeadAccess->getBlock();
+
+ // We only consider incoming MemoryAccesses that come before the
+ // MemoryPhi. Otherwise we could discover candidates that do not
+ // strictly dominate our starting def.
+ if (PostOrderNumbers[IncomingBlock] > PostOrderNumbers[PhiBlock])
+ ToCheck.insert(IncomingAccess);
+ }
continue;
}
- MemoryLocation KillingLoc = *MaybeKillingLoc;
- assert(KillingLoc.Ptr && "KillingLoc should not be null");
- const Value *KillingUndObj = getUnderlyingObject(KillingLoc.Ptr);
- LLVM_DEBUG(dbgs() << "Trying to eliminate MemoryDefs killed by "
- << *KillingDef << " (" << *KillingI << ")\n");
-
- unsigned ScanLimit = MemorySSAScanLimit;
- unsigned WalkerStepLimit = MemorySSAUpwardsStepLimit;
- unsigned PartialLimit = MemorySSAPartialStoreLimit;
- // Worklist of MemoryAccesses that may be killed by KillingDef.
- SmallSetVector<MemoryAccess *, 8> ToCheck;
- // Track MemoryAccesses that have been deleted in the loop below, so we can
- // skip them. Don't use SkipStores for this, which may contain reused
- // MemoryAccess addresses.
- SmallPtrSet<MemoryAccess *, 8> Deleted;
- [[maybe_unused]] unsigned OrigNumSkipStores = State.SkipStores.size();
- ToCheck.insert(KillingDef->getDefiningAccess());
-
- bool Shortend = false;
- bool IsMemTerm = State.isMemTerminatorInst(KillingI);
- // Check if MemoryAccesses in the worklist are killed by KillingDef.
- for (unsigned I = 0; I < ToCheck.size(); I++) {
- MemoryAccess *Current = ToCheck[I];
- if (Deleted.contains(Current))
- continue;
-
- std::optional<MemoryAccess *> MaybeDeadAccess = State.getDomMemoryDef(
- KillingDef, Current, KillingLoc, KillingUndObj, ScanLimit,
- WalkerStepLimit, IsMemTerm, PartialLimit);
-
- if (!MaybeDeadAccess) {
- LLVM_DEBUG(dbgs() << " finished walk\n");
+ MemoryDefWrapper DeadDefWrapper(
+ cast<MemoryDef>(DeadAccess),
+ getLocForInst(cast<MemoryDef>(DeadAccess)->getMemoryInst()));
+ MemoryLocationWrapper &DeadLocWrapper = *DeadDefWrapper.DefinedLocation;
+ LLVM_DEBUG(dbgs() << " (" << *DeadLocWrapper.DefInst << ")\n");
+ ToCheck.insert(DeadLocWrapper.GetDefiningAccess());
+ NumGetDomMemoryDefPassed++;
+
+ if (!DebugCounter::shouldExecute(MemorySSACounter))
+ continue;
+ if (isMemTerminatorInst(KillingLocWrapper.DefInst)) {
+ if (!(KillingLocWrapper.UnderlyingObject ==
+ DeadLocWrapper.UnderlyingObject))
continue;
+ LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
+ << *DeadLocWrapper.DefInst << "\n KILLER: "
+ << *KillingLocWrapper.DefInst << '\n');
+ deleteDeadInstruction(DeadLocWrapper.DefInst);
+ ++NumFastStores;
+ Changed = true;
+ } else {
+ // Check if DeadI overwrites KillingI.
+ int64_t KillingOffset = 0;
+ int64_t DeadOffset = 0;
+ OverwriteResult OR =
+ isOverwrite(KillingLocWrapper.DefInst, DeadLocWrapper.DefInst,
+ KillingLocWrapper.MemLoc, DeadLocWrapper.MemLoc,
+ KillingOffset, DeadOffset);
+ if (OR == OW_MaybePartial) {
+ auto Iter =
+ IOLs.insert(std::make_pair<BasicBlock *, InstOverlapIntervalsTy>(
+ DeadLocWrapper.DefInst->getParent(), InstOverlapIntervalsTy()));
+ auto &IOL = Iter.first->second;
+ OR = isPartialOverwrite(KillingLocWrapper.MemLoc, DeadLocWrapper.MemLoc,
+ KillingOffset, DeadOffset,
+ DeadLocWrapper.DefInst, IOL);
}
-
- MemoryAccess *DeadAccess = *MaybeDeadAccess;
- LLVM_DEBUG(dbgs() << " Checking if we can kill " << *DeadAccess);
- if (isa<MemoryPhi>(DeadAccess)) {
- LLVM_DEBUG(dbgs() << "\n ... adding incoming values to worklist\n");
- for (Value *V : cast<MemoryPhi>(DeadAccess)->incoming_values()) {
- MemoryAccess *IncomingAccess = cast<MemoryAccess>(V);
- BasicBlock *IncomingBlock = IncomingAccess->getBlock();
- BasicBlock *PhiBlock = DeadAccess->getBlock();
-
- // We only consider incoming MemoryAccesses that come before the
- // MemoryPhi. Otherwise we could discover candidates that do not
- // strictly dominate our starting def.
- if (State.PostOrderNumbers[IncomingBlock] >
- State.PostOrderNumbers[PhiBlock])
- ToCheck.insert(IncomingAccess);
+ if (EnablePartialStoreMerging && OR == OW_PartialEarlierWithFullLater) {
+ auto *DeadSI = dyn_cast<StoreInst>(DeadLocWrapper.DefInst);
+ auto *KillingSI = dyn_cast<StoreInst>(KillingLocWrapper.DefInst);
+ // We are re-using tryToMergePartialOverlappingStores, which requires
+ // DeadSI to dominate KillingSI.
+ // TODO: implement tryToMergeParialOverlappingStores using MemorySSA.
+ if (DeadSI && KillingSI && DT.dominates(DeadSI, KillingSI)) {
+ if (Constant *Merged = tryToMergePartialOverlappingStores(
+ KillingSI, DeadSI, KillingOffset, DeadOffset, DL, BatchAA,
+ &DT)) {
+
+ // Update stored value of earlier store to merged constant.
+ DeadSI->setOperand(0, Merged);
+ ++NumModifiedStores;
+ Changed = true;
+ Shortened = true;
+
+ // Remove killing store and remove any outstanding overlap
+ // intervals for the updated store.
+ deleteDeadInstruction(KillingSI);
+ auto I = IOLs.find(DeadSI->getParent());
+ if (I != IOLs.end())
+ I->second.erase(DeadSI);
+ break;
+ }
}
- continue;
}
- auto *DeadDefAccess = cast<MemoryDef>(DeadAccess);
- Instruction *DeadI = DeadDefAccess->getMemoryInst();
- LLVM_DEBUG(dbgs() << " (" << *DeadI << ")\n");
- ToCheck.insert(DeadDefAccess->getDefiningAccess());
- NumGetDomMemoryDefPassed++;
-
- if (!DebugCounter::shouldExecute(MemorySSACounter))
- continue;
-
- MemoryLocation DeadLoc = *State.getLocForWrite(DeadI);
-
- if (IsMemTerm) {
- const Value *DeadUndObj = getUnderlyingObject(DeadLoc.Ptr);
- if (KillingUndObj != DeadUndObj)
- continue;
- LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " << *DeadI
- << "\n KILLER: " << *KillingI << '\n');
- State.deleteDeadInstruction(DeadI, &Deleted);
+ if (OR == OW_Complete) {
+ LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
+ << *DeadLocWrapper.DefInst
+ << "\n KILLER: " << *DefInst << '\n');
+ deleteDeadInstruction(DeadLocWrapper.DefInst);
++NumFastStores;
- MadeChange = true;
- } else {
- // Check if DeadI overwrites KillingI.
- int64_t KillingOffset = 0;
- int64_t DeadOffset = 0;
- OverwriteResult OR = State.isOverwrite(
- KillingI, DeadI, KillingLoc, DeadLoc, KillingOffset, DeadOffset);
- if (OR == OW_MaybePartial) {
- auto Iter = State.IOLs.insert(
- std::make_pair<BasicBlock *, InstOverlapIntervalsTy>(
- DeadI->getParent(), InstOverlapIntervalsTy()));
- auto &IOL = Iter.first->second;
- OR = isPartialOverwrite(KillingLoc, DeadLoc, KillingOffset,
- DeadOffset, DeadI, IOL);
- }
-
- if (EnablePartialStoreMerging && OR == OW_PartialEarlierWithFullLater) {
- auto *DeadSI = dyn_cast<StoreInst>(DeadI);
- auto *KillingSI = dyn_cast<StoreInst>(KillingI);
- // We are re-using tryToMergePartialOverlappingStores, which requires
- // DeadSI to dominate KillingSI.
- // TODO: implement tryToMergeParialOverlappingStores using MemorySSA.
- if (DeadSI && KillingSI && DT.dominates(DeadSI, KillingSI)) {
- if (Constant *Merged = tryToMergePartialOverlappingStores(
- KillingSI, DeadSI, KillingOffset, DeadOffset, State.DL,
- State.BatchAA, &DT)) {
-
- // Update stored value of earlier store to merged constant.
- DeadSI->setOperand(0, Merged);
- ++NumModifiedStores;
- MadeChange = true;
-
- Shortend = true;
- // Remove killing store and remove any outstanding overlap
- // intervals for the updated store.
- State.deleteDeadInstruction(KillingSI, &Deleted);
- auto I = State.IOLs.find(DeadSI->getParent());
- if (I != State.IOLs.end())
- I->second.erase(DeadSI);
- break;
- }
- }
- }
-
- if (OR == OW_Complete) {
- LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " << *DeadI
- << "\n KILLER: " << *KillingI << '\n');
- State.deleteDeadInstruction(DeadI, &Deleted);
- ++NumFastStores;
- MadeChange = true;
- }
+ Changed = true;
}
}
+ }
+ return {Changed, Shortened};
+}
- assert(State.SkipStores.size() - OrigNumSkipStores == Deleted.size() &&
- "SkipStores and Deleted out of sync?");
+bool DSEState::eliminateDeadDefs(MemoryDefWrapper &KillingDefWrapper) {
+ if (!KillingDefWrapper.DefinedLocation.has_value()) {
+ LLVM_DEBUG(dbgs() << "Failed to find analyzable write location for "
+ << *DefInst << "\n");
+ return false;
+ }
+ LLVM_DEBUG(dbgs() << "Trying to eliminate MemoryDefs killed by " << *MemDef
----------------
haopliu wrote:
Ooh, code in `LLVM_DEBUG` was ignored in my cmake (release mode) then unit test cannot find such mistakes! Fixed these mistakes.
https://github.com/llvm/llvm-project/pull/100956
More information about the llvm-commits
mailing list