diff options
author | Matt Arsenault <Matthew.Arsenault@amd.com> | 2025-02-05 23:29:02 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-05 23:29:02 +0700 |
commit | 58a88001f3f2e41f9d17d8eb953d58921d92dedf (patch) | |
tree | eec1ce7cc67b1c3b211c4c14944de37399767c0a /llvm/lib/CodeGen/PeepholeOptimizer.cpp | |
parent | 92e3cd70698c2e06787500694c6a962d9228676d (diff) | |
download | llvm-58a88001f3f2e41f9d17d8eb953d58921d92dedf.zip llvm-58a88001f3f2e41f9d17d8eb953d58921d92dedf.tar.gz llvm-58a88001f3f2e41f9d17d8eb953d58921d92dedf.tar.bz2 |
PeepholeOpt: Fix looking for def of current copy to coalesce (#125533)
This fixes the handling of subregister extract copies. This
will allow AMDGPU to remove its implementation of
shouldRewriteCopySrc, which exists as a 10 year old workaround
to this bug. peephole-opt-fold-reg-sequence-subreg.mir will
show the expected improvement once the custom implementation
is removed.
The copy coalescing processing here is overly abstracted
from what's actually happening. Previously when visiting
coalescable copy-like instructions, we would parse the
sources one at a time and then pass the def of the root
instruction into findNextSource. This means that the
first thing the new ValueTracker constructed would do
is getVRegDef to find the instruction we are currently
processing. This adds an unnecessary step, placing
a useless entry in the RewriteMap, and required skipping
the no-op case where getNewSource would return the original
source operand. This was a problem since in the case
of a subregister extract, shouldRewriteCopySource would always
say that it is useful to rewrite and the use-def chain walk
would abort, returning the original operand. Move the process
to start looking at the source operand to begin with.
This does not fix the confused handling in the uncoalescable
copy case which is proving to be more difficult. Some currently
handled cases have multiple defs from a single source, and other
handled cases have 0 input operands. It would be simpler if
this was implemented with isCopyLikeInstr, rather than guessing
at the operand structure as it does now.
There are some improvements and some regressions. The
regressions appear to be downstream issues for the most part. One
of the uglier regressions is in PPC, where a sequence of insert_subrgs
is used to build registers. I opened #125502 to use reg_sequence instead,
which may help.
The worst regression is an absurd SPARC testcase using a <251 x fp128>,
which uses a very long chain of insert_subregs.
We need improved subregister handling locally in PeepholeOptimizer,
and other pasess like MachineCSE to fix some of the other regressions.
We should handle subregister composes and folding more indexes
into insert_subreg and reg_sequence.
Diffstat (limited to 'llvm/lib/CodeGen/PeepholeOptimizer.cpp')
-rw-r--r-- | llvm/lib/CodeGen/PeepholeOptimizer.cpp | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp index e0053fb..745c0d4 100644 --- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp +++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp @@ -465,7 +465,8 @@ private: bool optimizeUncoalescableCopy(MachineInstr &MI, SmallPtrSetImpl<MachineInstr *> &LocalMIs); bool optimizeRecurrence(MachineInstr &PHI); - bool findNextSource(RegSubRegPair RegSubReg, RewriteMapTy &RewriteMap); + bool findNextSource(const TargetRegisterClass *DefRC, unsigned DefSubReg, + RegSubRegPair RegSubReg, RewriteMapTy &RewriteMap); bool isMoveImmediate(MachineInstr &MI, SmallSet<Register, 4> &ImmDefRegs, DenseMap<Register, MachineInstr *> &ImmDefMIs); bool foldImmediate(MachineInstr &MI, SmallSet<Register, 4> &ImmDefRegs, @@ -991,8 +992,10 @@ bool PeepholeOptimizer::optimizeCondBranch(MachineInstr &MI) { return TII->optimizeCondBranch(MI); } -/// Try to find the next source that share the same register file -/// for the value defined by \p Reg and \p SubReg. +/// Try to find a better source value that shares the same register file to +/// replace \p RegSubReg in an instruction like +/// `DefRC.DefSubReg = COPY RegSubReg` +/// /// When true is returned, the \p RewriteMap can be used by the client to /// retrieve all Def -> Use along the way up to the next source. Any found /// Use that is not itself a key for another entry, is the next source to @@ -1002,17 +1005,15 @@ bool PeepholeOptimizer::optimizeCondBranch(MachineInstr &MI) { /// share the same register file as \p Reg and \p SubReg. The client should /// then be capable to rewrite all intermediate PHIs to get the next source. /// \return False if no alternative sources are available. True otherwise. -bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg, +bool PeepholeOptimizer::findNextSource(const TargetRegisterClass *DefRC, + unsigned DefSubReg, + RegSubRegPair RegSubReg, RewriteMapTy &RewriteMap) { // Do not try to find a new source for a physical register. // So far we do not have any motivating example for doing that. // Thus, instead of maintaining untested code, we will revisit that if // that changes at some point. Register Reg = RegSubReg.Reg; - if (Reg.isPhysical()) - return false; - const TargetRegisterClass *DefRC = MRI->getRegClass(Reg); - SmallVector<RegSubRegPair, 4> SrcToLook; RegSubRegPair CurSrcPair = RegSubReg; SrcToLook.push_back(CurSrcPair); @@ -1076,7 +1077,7 @@ bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg, // Keep following the chain if the value isn't any better yet. const TargetRegisterClass *SrcRC = MRI->getRegClass(CurSrcPair.Reg); - if (!TRI->shouldRewriteCopySrc(DefRC, RegSubReg.SubReg, SrcRC, + if (!TRI->shouldRewriteCopySrc(DefRC, DefSubReg, SrcRC, CurSrcPair.SubReg)) continue; @@ -1184,21 +1185,33 @@ bool PeepholeOptimizer::optimizeCoalescableCopyImpl(Rewriter &&CpyRewriter) { bool Changed = false; // Get the right rewriter for the current copy. // Rewrite each rewritable source. - RegSubRegPair Src; + RegSubRegPair Dst; RegSubRegPair TrackPair; - while (CpyRewriter.getNextRewritableSource(Src, TrackPair)) { + while (CpyRewriter.getNextRewritableSource(TrackPair, Dst)) { + if (Dst.Reg.isPhysical()) { + // Do not try to find a new source for a physical register. + // So far we do not have any motivating example for doing that. + // Thus, instead of maintaining untested code, we will revisit that if + // that changes at some point. + continue; + } + + const TargetRegisterClass *DefRC = MRI->getRegClass(Dst.Reg); + // Keep track of PHI nodes and its incoming edges when looking for sources. RewriteMapTy RewriteMap; // Try to find a more suitable source. If we failed to do so, or get the // actual source, move to the next source. - if (!findNextSource(TrackPair, RewriteMap)) + if (!findNextSource(DefRC, Dst.SubReg, TrackPair, RewriteMap)) continue; // Get the new source to rewrite. TODO: Only enable handling of multiple // sources (PHIs) once we have a motivating example and testcases for it. RegSubRegPair NewSrc = getNewSource(MRI, TII, TrackPair, RewriteMap, /*HandleMultipleSources=*/false); - if (Src.Reg == NewSrc.Reg || NewSrc.Reg == 0) + assert(TrackPair.Reg != NewSrc.Reg && + "should not rewrite source to original value"); + if (!NewSrc.Reg) continue; // Rewrite source. @@ -1325,9 +1338,14 @@ bool PeepholeOptimizer::optimizeUncoalescableCopy( if (Def.Reg.isPhysical()) return false; + // FIXME: Uncoalescable copies are treated differently by + // UncoalescableRewriter, and this probably should not share + // API. getNextRewritableSource really finds rewritable defs. + const TargetRegisterClass *DefRC = MRI->getRegClass(Def.Reg); + // If we do not know how to rewrite this definition, there is no point // in trying to kill this instruction. - if (!findNextSource(Def, RewriteMap)) + if (!findNextSource(DefRC, Def.SubReg, Def, RewriteMap)) return false; RewritePairs.push_back(Def); |