[PATCH] D112313: [DSE] Optimize defining access of defs while walking upwards.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 27 03:24:00 PDT 2021
fhahn updated this revision to Diff 382589.
fhahn added a comment.
Do not optimize accesses for instructions also reading from memory. With the current guarantee that getLocForWriteEx only supports instructiosn that modify a single location this should guarantee that KillingDef only accesses a single location.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D112313/new/
https://reviews.llvm.org/D112313
Files:
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1358,6 +1358,14 @@
Instruction *KillingI = KillingDef->getMemoryInst();
LLVM_DEBUG(dbgs() << " trying to get dominating access\n");
+ // Only optimize defining access of KillingDef when directly starting at its
+ // defining access. The defining access also must only access KillingLoc. At
+ // the moment we only support instructions with a single write location, so
+ // it should be sufficient to disable optimizations for instructions that
+ // also read from memory.
+ bool CanOptimize = KillingDef->getDefiningAccess() == StartAccess &&
+ !KillingI->mayReadFromMemory();
+
// Find the next clobbering Mod access for DefLoc, starting at StartAccess.
Optional<MemoryLocation> CurrentLoc;
for (;; Current = cast<MemoryDef>(Current)->getDefiningAccess()) {
@@ -1399,8 +1407,10 @@
Instruction *CurrentI = CurrentDef->getMemoryInst();
if (canSkipDef(CurrentDef, !isInvisibleToCallerBeforeRet(KillingUndObj),
- TLI))
+ TLI)) {
+ CanOptimize &= !isa<FenceInst>(CurrentDef->getMemoryInst());
continue;
+ }
// Before we try to remove anything, check for any extra throwing
// instructions that block us from DSEing
@@ -1436,13 +1446,17 @@
// If Current cannot be analyzed or is not removable, check the next
// candidate.
- if (!hasAnalyzableMemoryWrite(CurrentI, TLI) || !isRemovable(CurrentI))
+ if (!hasAnalyzableMemoryWrite(CurrentI, TLI) || !isRemovable(CurrentI)) {
+ CanOptimize = false;
continue;
+ }
// If Current does not have an analyzable write location, skip it
CurrentLoc = getLocForWriteEx(CurrentI);
- if (!CurrentLoc)
+ if (!CurrentLoc) {
+ CanOptimize = false;
continue;
+ }
// AliasAnalysis does not account for loops. Limit elimination to
// candidates for which we can guarantee they always store to the same
@@ -1450,6 +1464,7 @@
if (!isGuaranteedLoopIndependent(CurrentI, KillingI, *CurrentLoc)) {
LLVM_DEBUG(dbgs() << " ... not guaranteed loop independent\n");
WalkerStepLimit -= 1;
+ CanOptimize = false;
continue;
}
@@ -1457,13 +1472,29 @@
// If the killing def is a memory terminator (e.g. lifetime.end), check
// the next candidate if the current Current does not write the same
// underlying object as the terminator.
- if (!isMemTerminator(*CurrentLoc, CurrentI, KillingI))
+ if (!isMemTerminator(*CurrentLoc, CurrentI, KillingI)) {
+ CanOptimize = false;
continue;
+ }
} else {
int64_t KillingOffset = 0;
int64_t DeadOffset = 0;
auto OR = isOverwrite(KillingI, CurrentI, KillingLoc, *CurrentLoc,
KillingOffset, DeadOffset);
+ if (CanOptimize) {
+ // CurrentDef is the earliest write clobber of KillingDef. Use it as
+ // optimized access. Do not optimize if CurrentDef is already the
+ // defining access of KillingDef.
+ if (CurrentDef != KillingDef->getDefiningAccess() &&
+ (OR == OW_Complete || OR == OW_MaybePartial))
+ KillingDef->setOptimized(CurrentDef);
+
+ // Once a may-aliasing def is encountered do not set an optimized
+ // access.
+ if (OR != OW_None)
+ CanOptimize = false;
+ }
+
// If Current does not write to the same object as KillingDef, check
// the next candidate.
if (OR == OW_Unknown || OR == OW_None)
@@ -2160,7 +2191,6 @@
for (auto &I : instructions(F))
NumRemainingStores += isa<StoreInst>(&I);
#endif
-
if (!Changed)
return PreservedAnalyses::all();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112313.382589.patch
Type: text/x-patch
Size: 4066 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211027/44886f4f/attachment.bin>
More information about the llvm-commits
mailing list