-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Reland "[LICM] Sink unused l-invariant loads in preheader #157559" #170204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VigneshwarJ
wants to merge
6
commits into
llvm:main
Choose a base branch
from
VigneshwarJ:sink_reland
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+539
−314
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ad1bda
Reland "[LICM] Sink unused l-invariant loads in preheader. #157559"
VigneshwarJ 73941b5
Merge branch 'main' into sink_reland
VigneshwarJ b40341a
fix values
VigneshwarJ 823738f
review changes
VigneshwarJ 91b8fd5
Merge branch 'main' into sink_reland
VigneshwarJ 2972393
tests for caching defining access
VigneshwarJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,11 @@ static void moveInstructionBefore(Instruction &I, BasicBlock::iterator Dest, | |
| ICFLoopSafetyInfo &SafetyInfo, | ||
| MemorySSAUpdater &MSSAU, ScalarEvolution *SE); | ||
|
|
||
| static bool sinkUnusedInvariantsFromPreheaderToExit( | ||
| Loop *L, AAResults *AA, ICFLoopSafetyInfo *SafetyInfo, | ||
| MemorySSAUpdater &MSSAU, ScalarEvolution *SE, DominatorTree *DT, | ||
| SinkAndHoistLICMFlags &SinkFlags, OptimizationRemarkEmitter *ORE); | ||
|
|
||
| static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L, | ||
| function_ref<void(Instruction *)> Fn); | ||
| using PointersAndHasReadsOutsideSet = | ||
|
|
@@ -471,6 +476,12 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, | |
| TLI, TTI, L, MSSAU, &SafetyInfo, Flags, ORE) | ||
| : sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, TTI, L, | ||
| MSSAU, &SafetyInfo, Flags, ORE); | ||
|
|
||
| // sink pre-header defs that are unused in-loop into the unique exit to reduce | ||
| // pressure. | ||
| Changed |= sinkUnusedInvariantsFromPreheaderToExit(L, AA, &SafetyInfo, MSSAU, | ||
| SE, DT, Flags, ORE); | ||
|
|
||
| Flags.setIsSink(false); | ||
| if (Preheader) | ||
| Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, AC, TLI, L, | ||
|
|
@@ -1469,6 +1480,118 @@ static void moveInstructionBefore(Instruction &I, BasicBlock::iterator Dest, | |
| SE->forgetBlockAndLoopDispositions(&I); | ||
| } | ||
|
|
||
| // If there's a single exit block, sink any loop-invariant values that were | ||
| // defined in the preheader but not used inside the loop into the exit block | ||
| // to reduce register pressure in the loop. | ||
| static bool sinkUnusedInvariantsFromPreheaderToExit( | ||
| Loop *L, AAResults *AA, ICFLoopSafetyInfo *SafetyInfo, | ||
| MemorySSAUpdater &MSSAU, ScalarEvolution *SE, DominatorTree *DT, | ||
| SinkAndHoistLICMFlags &SinkFlags, OptimizationRemarkEmitter *ORE) { | ||
| BasicBlock *ExitBlock = L->getExitBlock(); | ||
| if (!ExitBlock) | ||
| return false; | ||
|
|
||
| BasicBlock *Preheader = L->getLoopPreheader(); | ||
| if (!Preheader) | ||
| return false; | ||
|
|
||
| bool MadeAnyChanges = false; | ||
| MemoryAccess *ExitDef = nullptr; | ||
|
|
||
| for (Instruction &I : llvm::make_early_inc_range(llvm::reverse(*Preheader))) { | ||
|
|
||
| // Skip terminator. | ||
| if (Preheader->getTerminator() == &I) | ||
| continue; | ||
|
|
||
| // New instructions were inserted at the end of the preheader. | ||
| if (isa<PHINode>(I)) | ||
| break; | ||
|
|
||
| // Don't move instructions which might have side effects, since the side | ||
| // effects need to complete before instructions inside the loop. Note that | ||
| // it's okay if the instruction might have undefined behavior: LoopSimplify | ||
| // guarantees that the preheader dominates the exit block. | ||
| if (I.mayHaveSideEffects()) | ||
| continue; | ||
|
|
||
| if (!canSinkOrHoistInst(I, AA, DT, L, MSSAU, true, SinkFlags, nullptr)) | ||
| continue; | ||
|
|
||
| // Determine if there is a use in or before the loop (direct or | ||
| // otherwise). | ||
| bool UsedInLoopOrPreheader = false; | ||
| for (Use &U : I.uses()) { | ||
| auto *UserI = cast<Instruction>(U.getUser()); | ||
| BasicBlock *UseBB = UserI->getParent(); | ||
| if (auto *PN = dyn_cast<PHINode>(UserI)) { | ||
| UseBB = PN->getIncomingBlock(U); | ||
| } | ||
| if (UseBB == Preheader || L->contains(UseBB)) { | ||
| UsedInLoopOrPreheader = true; | ||
| break; | ||
| } | ||
| } | ||
| if (UsedInLoopOrPreheader) | ||
| continue; | ||
|
|
||
| // Move the instruction. | ||
| SafetyInfo->removeInstruction(&I); | ||
| SafetyInfo->insertInstructionTo(&I, ExitBlock); | ||
| I.moveBefore(*ExitBlock, ExitBlock->getFirstInsertionPt()); | ||
| if (SE) | ||
| SE->forgetValue(&I); | ||
|
|
||
| // Update MemorySSA. | ||
| if (auto *OldMA = MSSAU.getMemorySSA()->getMemoryAccess(&I)) { | ||
| // apviding the expensive getPreviousDefRecursive call by manually | ||
| // setting the defining access. | ||
| if (!ExitDef) { | ||
| if (auto *MPhi = MSSAU.getMemorySSA()->getMemoryAccess(ExitBlock)) { | ||
| ExitDef = MPhi; | ||
| } else { | ||
| BasicBlock *Current = *predecessors(ExitBlock).begin(); | ||
| while (true) { | ||
| if (auto *Accesses = | ||
| MSSAU.getMemorySSA()->getBlockAccesses(Current)) { | ||
| if (!Accesses->empty()) { | ||
| MemoryAccess *Back = | ||
| const_cast<MemoryAccess *>(&Accesses->back()); | ||
| if (isa<MemoryDef>(Back) || isa<MemoryPhi>(Back)) | ||
| ExitDef = Back; | ||
| else | ||
|
||
| ExitDef = MSSAU.getMemorySSA() | ||
| ->getWalker() | ||
| ->getClobberingMemoryAccess(Back); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (Current == L->getHeader()) { | ||
| Current = Preheader; | ||
| continue; | ||
| } | ||
|
|
||
| if (pred_empty(Current)) { | ||
| ExitDef = MSSAU.getMemorySSA()->getLiveOnEntryDef(); | ||
| break; | ||
| } | ||
| Current = *pred_begin(Current); | ||
| } | ||
| } | ||
| } | ||
| MemoryAccess *NewMA = MSSAU.createMemoryAccessInBB(&I, ExitDef, ExitBlock, | ||
| MemorySSA::Beginning); | ||
| OldMA->replaceAllUsesWith(NewMA); | ||
| MSSAU.removeMemoryAccess(OldMA); | ||
| } | ||
|
|
||
| MadeAnyChanges = true; | ||
| } | ||
|
|
||
| return MadeAnyChanges; | ||
| } | ||
|
|
||
| static Instruction *sinkThroughTriviallyReplaceablePHI( | ||
| PHINode *TPN, Instruction *I, LoopInfo *LI, | ||
| SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There probably should be a helper to get a range that trims off the terminator. Shouldn't compare against isTerminator, can check I.isTerminator too