mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-18 13:34:04 +00:00
[PeepholeOptimizer] Refactor optimizeUncoalescable logic
Reapply r242294. - Create a new CopyRewriter for Uncoalescable copy-like instructions - Change the ValueTracker to return a ValueTrackerResult This makes optimizeUncoalescable looks more like optimizeCoalescable and use the CopyRewritter infrastructure. This is also the preparation for looking up into PHI nodes in the ValueTracker. rdar://problem/20404526 Differential Revision: http://reviews.llvm.org/D11195 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242940 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
b18e7bdac8
commit
e7aafe5ecc
@ -107,6 +107,8 @@ STATISTIC(NumUncoalescableCopies, "Number of uncoalescable copies optimized");
|
|||||||
STATISTIC(NumRewrittenCopies, "Number of copies rewritten");
|
STATISTIC(NumRewrittenCopies, "Number of copies rewritten");
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
class ValueTrackerResult;
|
||||||
|
|
||||||
class PeepholeOptimizer : public MachineFunctionPass {
|
class PeepholeOptimizer : public MachineFunctionPass {
|
||||||
const TargetInstrInfo *TII;
|
const TargetInstrInfo *TII;
|
||||||
const TargetRegisterInfo *TRI;
|
const TargetRegisterInfo *TRI;
|
||||||
@ -171,6 +173,55 @@ namespace {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// \brief Helper class to hold a reply for ValueTracker queries. Contains the
|
||||||
|
/// returned sources for a given search and the instructions where the sources
|
||||||
|
/// were tracked from.
|
||||||
|
class ValueTrackerResult {
|
||||||
|
private:
|
||||||
|
/// Track all sources found by one ValueTracker query.
|
||||||
|
SmallVector<TargetInstrInfo::RegSubRegPair, 2> RegSrcs;
|
||||||
|
|
||||||
|
/// Instruction using the sources in 'RegSrcs'.
|
||||||
|
const MachineInstr *Inst;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ValueTrackerResult() : Inst(nullptr) {}
|
||||||
|
ValueTrackerResult(unsigned Reg, unsigned SubReg) : Inst(nullptr) {
|
||||||
|
addSource(Reg, SubReg);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isValid() const { return getNumSources() > 0; }
|
||||||
|
|
||||||
|
void setInst(const MachineInstr *I) { Inst = I; }
|
||||||
|
const MachineInstr *getInst() const { return Inst; }
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
RegSrcs.clear();
|
||||||
|
Inst = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addSource(unsigned SrcReg, unsigned SrcSubReg) {
|
||||||
|
RegSrcs.push_back(TargetInstrInfo::RegSubRegPair(SrcReg, SrcSubReg));
|
||||||
|
}
|
||||||
|
|
||||||
|
void setSource(int Idx, unsigned SrcReg, unsigned SrcSubReg) {
|
||||||
|
assert(Idx < getNumSources() && "Reg pair source out of index");
|
||||||
|
RegSrcs[Idx] = TargetInstrInfo::RegSubRegPair(SrcReg, SrcSubReg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getNumSources() const { return RegSrcs.size(); }
|
||||||
|
|
||||||
|
unsigned getSrcReg(int Idx) const {
|
||||||
|
assert(Idx < getNumSources() && "Reg source out of index");
|
||||||
|
return RegSrcs[Idx].Reg;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned getSrcSubReg(int Idx) const {
|
||||||
|
assert(Idx < getNumSources() && "SubReg source out of index");
|
||||||
|
return RegSrcs[Idx].SubReg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// \brief Helper class to track the possible sources of a value defined by
|
/// \brief Helper class to track the possible sources of a value defined by
|
||||||
/// a (chain of) copy related instructions.
|
/// a (chain of) copy related instructions.
|
||||||
/// Given a definition (instruction and definition index), this class
|
/// Given a definition (instruction and definition index), this class
|
||||||
@ -213,23 +264,23 @@ namespace {
|
|||||||
|
|
||||||
/// \brief Dispatcher to the right underlying implementation of
|
/// \brief Dispatcher to the right underlying implementation of
|
||||||
/// getNextSource.
|
/// getNextSource.
|
||||||
bool getNextSourceImpl(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceImpl();
|
||||||
/// \brief Specialized version of getNextSource for Copy instructions.
|
/// \brief Specialized version of getNextSource for Copy instructions.
|
||||||
bool getNextSourceFromCopy(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromCopy();
|
||||||
/// \brief Specialized version of getNextSource for Bitcast instructions.
|
/// \brief Specialized version of getNextSource for Bitcast instructions.
|
||||||
bool getNextSourceFromBitcast(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromBitcast();
|
||||||
/// \brief Specialized version of getNextSource for RegSequence
|
/// \brief Specialized version of getNextSource for RegSequence
|
||||||
/// instructions.
|
/// instructions.
|
||||||
bool getNextSourceFromRegSequence(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromRegSequence();
|
||||||
/// \brief Specialized version of getNextSource for InsertSubreg
|
/// \brief Specialized version of getNextSource for InsertSubreg
|
||||||
/// instructions.
|
/// instructions.
|
||||||
bool getNextSourceFromInsertSubreg(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromInsertSubreg();
|
||||||
/// \brief Specialized version of getNextSource for ExtractSubreg
|
/// \brief Specialized version of getNextSource for ExtractSubreg
|
||||||
/// instructions.
|
/// instructions.
|
||||||
bool getNextSourceFromExtractSubreg(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromExtractSubreg();
|
||||||
/// \brief Specialized version of getNextSource for SubregToReg
|
/// \brief Specialized version of getNextSource for SubregToReg
|
||||||
/// instructions.
|
/// instructions.
|
||||||
bool getNextSourceFromSubregToReg(unsigned &SrcReg, unsigned &SrcSubReg);
|
ValueTrackerResult getNextSourceFromSubregToReg();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// \brief Create a ValueTracker instance for the value defined by \p Reg.
|
/// \brief Create a ValueTracker instance for the value defined by \p Reg.
|
||||||
@ -276,16 +327,10 @@ namespace {
|
|||||||
|
|
||||||
/// \brief Following the use-def chain, get the next available source
|
/// \brief Following the use-def chain, get the next available source
|
||||||
/// for the tracked value.
|
/// for the tracked value.
|
||||||
/// When the returned value is not nullptr, \p SrcReg gives the register
|
/// \return A ValueTrackerResult containing the a set of registers
|
||||||
/// that contain the tracked value.
|
/// and sub registers with tracked values. A ValueTrackerResult with
|
||||||
/// \note The sub register index returned in \p SrcSubReg must be used
|
/// an empty set of registers means no source was found.
|
||||||
/// on \p SrcReg to access the actual value.
|
ValueTrackerResult getNextSource();
|
||||||
/// \return Unless the returned value is nullptr (i.e., no source found),
|
|
||||||
/// \p SrcReg gives the register of the next source used in the returned
|
|
||||||
/// instruction and \p SrcSubReg the sub-register index to be used on that
|
|
||||||
/// source to get the tracked value. When nullptr is returned, no
|
|
||||||
/// alternative source has been found.
|
|
||||||
const MachineInstr *getNextSource(unsigned &SrcReg, unsigned &SrcSubReg);
|
|
||||||
|
|
||||||
/// \brief Get the last register where the initial value can be found.
|
/// \brief Get the last register where the initial value can be found.
|
||||||
/// Initially this is the register of the definition.
|
/// Initially this is the register of the definition.
|
||||||
@ -560,11 +605,11 @@ bool PeepholeOptimizer::findNextSource(unsigned &Reg, unsigned &SubReg) {
|
|||||||
// or find a more suitable source.
|
// or find a more suitable source.
|
||||||
ValueTracker ValTracker(Reg, DefSubReg, *MRI, !DisableAdvCopyOpt, TII);
|
ValueTracker ValTracker(Reg, DefSubReg, *MRI, !DisableAdvCopyOpt, TII);
|
||||||
do {
|
do {
|
||||||
unsigned CopySrcReg, CopySrcSubReg;
|
ValueTrackerResult Res = ValTracker.getNextSource();
|
||||||
if (!ValTracker.getNextSource(CopySrcReg, CopySrcSubReg))
|
if (!Res.isValid())
|
||||||
break;
|
break;
|
||||||
Src = CopySrcReg;
|
Src = Res.getSrcReg(0);
|
||||||
SrcSubReg = CopySrcSubReg;
|
SrcSubReg = Res.getSrcSubReg(0);
|
||||||
|
|
||||||
// Do not extend the live-ranges of physical registers as they add
|
// Do not extend the live-ranges of physical registers as they add
|
||||||
// constraints to the register allocator.
|
// constraints to the register allocator.
|
||||||
@ -653,7 +698,7 @@ public:
|
|||||||
|
|
||||||
/// \brief Rewrite the current source with \p NewReg and \p NewSubReg
|
/// \brief Rewrite the current source with \p NewReg and \p NewSubReg
|
||||||
/// if possible.
|
/// if possible.
|
||||||
/// \return True if the rewritting was possible, false otherwise.
|
/// \return True if the rewriting was possible, false otherwise.
|
||||||
virtual bool RewriteCurrentSource(unsigned NewReg, unsigned NewSubReg) {
|
virtual bool RewriteCurrentSource(unsigned NewReg, unsigned NewSubReg) {
|
||||||
if (!CopyLike.isCopy() || CurrentSrcIdx != 1)
|
if (!CopyLike.isCopy() || CurrentSrcIdx != 1)
|
||||||
return false;
|
return false;
|
||||||
@ -662,6 +707,91 @@ public:
|
|||||||
MOSrc.setSubReg(NewSubReg);
|
MOSrc.setSubReg(NewSubReg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \brief Rewrite the current source with \p NewSrcReg and \p NewSecSubReg
|
||||||
|
/// by creating a new COPY instruction. \p DefReg and \p DefSubReg contain the
|
||||||
|
/// definition to be rewritten from the original copylike instruction.
|
||||||
|
/// \return The new COPY if the rewriting was possible, nullptr otherwise.
|
||||||
|
/// This is needed to handle Uncoalescable copies, since they are copy
|
||||||
|
/// like instructions that aren't recognized by the register allocator.
|
||||||
|
virtual MachineInstr *RewriteCurrentSource(unsigned DefReg,
|
||||||
|
unsigned DefSubReg,
|
||||||
|
unsigned NewSrcReg,
|
||||||
|
unsigned NewSrcSubReg) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief Helper class to rewrite uncoalescable copy like instructions
|
||||||
|
/// into new COPY (coalescable friendly) instructions.
|
||||||
|
class UncoalescableRewriter : public CopyRewriter {
|
||||||
|
protected:
|
||||||
|
const TargetInstrInfo &TII;
|
||||||
|
MachineRegisterInfo &MRI;
|
||||||
|
/// The number of defs in the bitcast
|
||||||
|
unsigned NumDefs;
|
||||||
|
|
||||||
|
public:
|
||||||
|
UncoalescableRewriter(MachineInstr &MI, const TargetInstrInfo &TII,
|
||||||
|
MachineRegisterInfo &MRI)
|
||||||
|
: CopyRewriter(MI), TII(TII), MRI(MRI) {
|
||||||
|
NumDefs = MI.getDesc().getNumDefs();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Get the next rewritable def source (TrackReg, TrackSubReg)
|
||||||
|
/// All such sources need to be considered rewritable in order to
|
||||||
|
/// rewrite a uncoalescable copy-like instruction. This method return
|
||||||
|
/// each definition that must be checked if rewritable.
|
||||||
|
///
|
||||||
|
bool getNextRewritableSource(unsigned &SrcReg, unsigned &SrcSubReg,
|
||||||
|
unsigned &TrackReg,
|
||||||
|
unsigned &TrackSubReg) override {
|
||||||
|
// Find the next non-dead definition and continue from there.
|
||||||
|
if (CurrentSrcIdx == NumDefs)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
while (CopyLike.getOperand(CurrentSrcIdx).isDead()) {
|
||||||
|
++CurrentSrcIdx;
|
||||||
|
if (CurrentSrcIdx == NumDefs)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// What we track are the alternative sources of the definition.
|
||||||
|
const MachineOperand &MODef = CopyLike.getOperand(CurrentSrcIdx);
|
||||||
|
TrackReg = MODef.getReg();
|
||||||
|
TrackSubReg = MODef.getSubReg();
|
||||||
|
|
||||||
|
CurrentSrcIdx++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Rewrite the current source with \p NewSrcReg and \p NewSrcSubReg
|
||||||
|
/// by creating a new COPY instruction. \p DefReg and \p DefSubReg contain the
|
||||||
|
/// definition to be rewritten from the original copylike instruction.
|
||||||
|
/// \return The new COPY if the rewriting was possible, nullptr otherwise.
|
||||||
|
MachineInstr *RewriteCurrentSource(unsigned DefReg, unsigned DefSubReg,
|
||||||
|
unsigned NewSrcReg,
|
||||||
|
unsigned NewSrcSubReg) override {
|
||||||
|
assert(!TargetRegisterInfo::isPhysicalRegister(DefReg) &&
|
||||||
|
"We do not rewrite physical registers");
|
||||||
|
|
||||||
|
const TargetRegisterClass *DefRC = MRI.getRegClass(DefReg);
|
||||||
|
unsigned NewVR = MRI.createVirtualRegister(DefRC);
|
||||||
|
|
||||||
|
MachineInstr *NewCopy =
|
||||||
|
BuildMI(*CopyLike.getParent(), &CopyLike, CopyLike.getDebugLoc(),
|
||||||
|
TII.get(TargetOpcode::COPY), NewVR)
|
||||||
|
.addReg(NewSrcReg, 0, NewSrcSubReg);
|
||||||
|
|
||||||
|
NewCopy->getOperand(0).setSubReg(DefSubReg);
|
||||||
|
if (DefSubReg)
|
||||||
|
NewCopy->getOperand(0).setIsUndef();
|
||||||
|
|
||||||
|
MRI.replaceRegWith(DefReg, NewVR);
|
||||||
|
MRI.clearKillFlags(NewVR);
|
||||||
|
|
||||||
|
return NewCopy;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief Specialized rewriter for INSERT_SUBREG instruction.
|
/// \brief Specialized rewriter for INSERT_SUBREG instruction.
|
||||||
@ -850,7 +980,13 @@ public:
|
|||||||
/// \return A pointer to a dynamically allocated CopyRewriter or nullptr
|
/// \return A pointer to a dynamically allocated CopyRewriter or nullptr
|
||||||
/// if no rewriter works for \p MI.
|
/// if no rewriter works for \p MI.
|
||||||
static CopyRewriter *getCopyRewriter(MachineInstr &MI,
|
static CopyRewriter *getCopyRewriter(MachineInstr &MI,
|
||||||
const TargetInstrInfo &TII) {
|
const TargetInstrInfo &TII,
|
||||||
|
MachineRegisterInfo &MRI) {
|
||||||
|
// Handle uncoalescable copy-like instructions.
|
||||||
|
if (MI.isBitcast() || (MI.isRegSequenceLike() || MI.isInsertSubregLike() ||
|
||||||
|
MI.isExtractSubregLike()))
|
||||||
|
return new UncoalescableRewriter(MI, TII, MRI);
|
||||||
|
|
||||||
switch (MI.getOpcode()) {
|
switch (MI.getOpcode()) {
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -889,7 +1025,7 @@ bool PeepholeOptimizer::optimizeCoalescableCopy(MachineInstr *MI) {
|
|||||||
|
|
||||||
bool Changed = false;
|
bool Changed = false;
|
||||||
// Get the right rewriter for the current copy.
|
// Get the right rewriter for the current copy.
|
||||||
std::unique_ptr<CopyRewriter> CpyRewriter(getCopyRewriter(*MI, *TII));
|
std::unique_ptr<CopyRewriter> CpyRewriter(getCopyRewriter(*MI, *TII, *MRI));
|
||||||
// If none exists, bails out.
|
// If none exists, bails out.
|
||||||
if (!CpyRewriter)
|
if (!CpyRewriter)
|
||||||
return false;
|
return false;
|
||||||
@ -899,9 +1035,8 @@ bool PeepholeOptimizer::optimizeCoalescableCopy(MachineInstr *MI) {
|
|||||||
TrackSubReg)) {
|
TrackSubReg)) {
|
||||||
unsigned NewSrc = TrackReg;
|
unsigned NewSrc = TrackReg;
|
||||||
unsigned NewSubReg = TrackSubReg;
|
unsigned NewSubReg = TrackSubReg;
|
||||||
// Try to find a more suitable source.
|
// Try to find a more suitable source. If we failed to do so, or get the
|
||||||
// If we failed to do so, or get the actual source,
|
// actual source, move to the next source.
|
||||||
// move to the next source.
|
|
||||||
if (!findNextSource(NewSrc, NewSubReg) || SrcReg == NewSrc)
|
if (!findNextSource(NewSrc, NewSubReg) || SrcReg == NewSrc)
|
||||||
continue;
|
continue;
|
||||||
// Rewrite source.
|
// Rewrite source.
|
||||||
@ -939,45 +1074,47 @@ bool PeepholeOptimizer::optimizeUncoalescableCopy(
|
|||||||
SmallVector<
|
SmallVector<
|
||||||
std::pair<TargetInstrInfo::RegSubRegPair, TargetInstrInfo::RegSubRegPair>,
|
std::pair<TargetInstrInfo::RegSubRegPair, TargetInstrInfo::RegSubRegPair>,
|
||||||
4> RewritePairs;
|
4> RewritePairs;
|
||||||
for (const MachineOperand &MODef : MI->defs()) {
|
// Get the right rewriter for the current copy.
|
||||||
if (MODef.isDead())
|
std::unique_ptr<CopyRewriter> CpyRewriter(getCopyRewriter(*MI, *TII, *MRI));
|
||||||
// We can ignore those.
|
// If none exists, bails out.
|
||||||
continue;
|
if (!CpyRewriter)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Rewrite each rewritable source by generating new COPYs. This works
|
||||||
|
// differently from optimizeCoalescableCopy since it first makes sure that all
|
||||||
|
// definitions can be rewritten.
|
||||||
|
unsigned SrcReg, SrcSubReg, TrackReg, TrackSubReg;
|
||||||
|
while (CpyRewriter->getNextRewritableSource(SrcReg, SrcSubReg, TrackReg,
|
||||||
|
TrackSubReg)) {
|
||||||
// If a physical register is here, this is probably for a good reason.
|
// If a physical register is here, this is probably for a good reason.
|
||||||
// Do not rewrite that.
|
// Do not rewrite that.
|
||||||
if (TargetRegisterInfo::isPhysicalRegister(MODef.getReg()))
|
if (TargetRegisterInfo::isPhysicalRegister(TrackReg))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// If we do not know how to rewrite this definition, there is no point
|
// If we do not know how to rewrite this definition, there is no point
|
||||||
// in trying to kill this instruction.
|
// in trying to kill this instruction.
|
||||||
TargetInstrInfo::RegSubRegPair Def(MODef.getReg(), MODef.getSubReg());
|
TargetInstrInfo::RegSubRegPair Def(TrackReg, TrackSubReg);
|
||||||
TargetInstrInfo::RegSubRegPair Src = Def;
|
TargetInstrInfo::RegSubRegPair Src = Def;
|
||||||
if (!findNextSource(Src.Reg, Src.SubReg))
|
if (!findNextSource(Src.Reg, Src.SubReg))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
RewritePairs.push_back(std::make_pair(Def, Src));
|
RewritePairs.push_back(std::make_pair(Def, Src));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The change is possible for all defs, do it.
|
// The change is possible for all defs, do it.
|
||||||
for (const auto &PairDefSrc : RewritePairs) {
|
for (const auto &PairDefSrc : RewritePairs) {
|
||||||
const auto &Def = PairDefSrc.first;
|
const auto &Def = PairDefSrc.first;
|
||||||
const auto &Src = PairDefSrc.second;
|
const auto &Src = PairDefSrc.second;
|
||||||
|
|
||||||
// Rewrite the "copy" in a way the register coalescer understands.
|
// Rewrite the "copy" in a way the register coalescer understands.
|
||||||
assert(!TargetRegisterInfo::isPhysicalRegister(Def.Reg) &&
|
MachineInstr *NewCopy = CpyRewriter->RewriteCurrentSource(
|
||||||
"We do not rewrite physical registers");
|
Def.Reg, Def.SubReg, Src.Reg, Src.SubReg);
|
||||||
const TargetRegisterClass *DefRC = MRI->getRegClass(Def.Reg);
|
assert(NewCopy && "Should be able to always generate a new copy");
|
||||||
unsigned NewVR = MRI->createVirtualRegister(DefRC);
|
|
||||||
MachineInstr *NewCopy = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
|
// We extended the lifetime of Src and clear the kill flags to
|
||||||
TII->get(TargetOpcode::COPY),
|
// account for that.
|
||||||
NewVR).addReg(Src.Reg, 0, Src.SubReg);
|
|
||||||
NewCopy->getOperand(0).setSubReg(Def.SubReg);
|
|
||||||
if (Def.SubReg)
|
|
||||||
NewCopy->getOperand(0).setIsUndef();
|
|
||||||
LocalMIs.insert(NewCopy);
|
|
||||||
MRI->replaceRegWith(Def.Reg, NewVR);
|
|
||||||
MRI->clearKillFlags(NewVR);
|
|
||||||
// We extended the lifetime of Src.
|
|
||||||
// Clear the kill flags to account for that.
|
|
||||||
MRI->clearKillFlags(Src.Reg);
|
MRI->clearKillFlags(Src.Reg);
|
||||||
|
LocalMIs.insert(NewCopy);
|
||||||
}
|
}
|
||||||
// MI is now dead.
|
// MI is now dead.
|
||||||
MI->eraseFromParent();
|
MI->eraseFromParent();
|
||||||
@ -1190,8 +1327,7 @@ bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
|
|||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromCopy(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromCopy() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert(Def->isCopy() && "Invalid definition");
|
assert(Def->isCopy() && "Invalid definition");
|
||||||
// Copy instruction are supposed to be: Def = Src.
|
// Copy instruction are supposed to be: Def = Src.
|
||||||
// If someone breaks this assumption, bad things will happen everywhere.
|
// If someone breaks this assumption, bad things will happen everywhere.
|
||||||
@ -1200,29 +1336,26 @@ bool ValueTracker::getNextSourceFromCopy(unsigned &SrcReg,
|
|||||||
if (Def->getOperand(DefIdx).getSubReg() != DefSubReg)
|
if (Def->getOperand(DefIdx).getSubReg() != DefSubReg)
|
||||||
// If we look for a different subreg, it means we want a subreg of src.
|
// If we look for a different subreg, it means we want a subreg of src.
|
||||||
// Bails as we do not support composing subreg yet.
|
// Bails as we do not support composing subreg yet.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
// Otherwise, we want the whole source.
|
// Otherwise, we want the whole source.
|
||||||
const MachineOperand &Src = Def->getOperand(1);
|
const MachineOperand &Src = Def->getOperand(1);
|
||||||
SrcReg = Src.getReg();
|
return ValueTrackerResult(Src.getReg(), Src.getSubReg());
|
||||||
SrcSubReg = Src.getSubReg();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromBitcast(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromBitcast() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert(Def->isBitcast() && "Invalid definition");
|
assert(Def->isBitcast() && "Invalid definition");
|
||||||
|
|
||||||
// Bail if there are effects that a plain copy will not expose.
|
// Bail if there are effects that a plain copy will not expose.
|
||||||
if (Def->hasUnmodeledSideEffects())
|
if (Def->hasUnmodeledSideEffects())
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
// Bitcasts with more than one def are not supported.
|
// Bitcasts with more than one def are not supported.
|
||||||
if (Def->getDesc().getNumDefs() != 1)
|
if (Def->getDesc().getNumDefs() != 1)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
if (Def->getOperand(DefIdx).getSubReg() != DefSubReg)
|
if (Def->getOperand(DefIdx).getSubReg() != DefSubReg)
|
||||||
// If we look for a different subreg, it means we want a subreg of the src.
|
// If we look for a different subreg, it means we want a subreg of the src.
|
||||||
// Bails as we do not support composing subreg yet.
|
// Bails as we do not support composing subreg yet.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
unsigned SrcIdx = Def->getNumOperands();
|
unsigned SrcIdx = Def->getNumOperands();
|
||||||
for (unsigned OpIdx = DefIdx + 1, EndOpIdx = SrcIdx; OpIdx != EndOpIdx;
|
for (unsigned OpIdx = DefIdx + 1, EndOpIdx = SrcIdx; OpIdx != EndOpIdx;
|
||||||
@ -1233,17 +1366,14 @@ bool ValueTracker::getNextSourceFromBitcast(unsigned &SrcReg,
|
|||||||
assert(!MO.isDef() && "We should have skipped all the definitions by now");
|
assert(!MO.isDef() && "We should have skipped all the definitions by now");
|
||||||
if (SrcIdx != EndOpIdx)
|
if (SrcIdx != EndOpIdx)
|
||||||
// Multiple sources?
|
// Multiple sources?
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
SrcIdx = OpIdx;
|
SrcIdx = OpIdx;
|
||||||
}
|
}
|
||||||
const MachineOperand &Src = Def->getOperand(SrcIdx);
|
const MachineOperand &Src = Def->getOperand(SrcIdx);
|
||||||
SrcReg = Src.getReg();
|
return ValueTrackerResult(Src.getReg(), Src.getSubReg());
|
||||||
SrcSubReg = Src.getSubReg();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromRegSequence(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromRegSequence() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert((Def->isRegSequence() || Def->isRegSequenceLike()) &&
|
assert((Def->isRegSequence() || Def->isRegSequenceLike()) &&
|
||||||
"Invalid definition");
|
"Invalid definition");
|
||||||
|
|
||||||
@ -1262,16 +1392,16 @@ bool ValueTracker::getNextSourceFromRegSequence(unsigned &SrcReg,
|
|||||||
// have this case.
|
// have this case.
|
||||||
// If we can ascertain (or force) that this never happens, we could
|
// If we can ascertain (or force) that this never happens, we could
|
||||||
// turn that into an assertion.
|
// turn that into an assertion.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
if (!TII)
|
if (!TII)
|
||||||
// We could handle the REG_SEQUENCE here, but we do not want to
|
// We could handle the REG_SEQUENCE here, but we do not want to
|
||||||
// duplicate the code from the generic TII.
|
// duplicate the code from the generic TII.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
SmallVector<TargetInstrInfo::RegSubRegPairAndIdx, 8> RegSeqInputRegs;
|
SmallVector<TargetInstrInfo::RegSubRegPairAndIdx, 8> RegSeqInputRegs;
|
||||||
if (!TII->getRegSequenceInputs(*Def, DefIdx, RegSeqInputRegs))
|
if (!TII->getRegSequenceInputs(*Def, DefIdx, RegSeqInputRegs))
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
// We are looking at:
|
// We are looking at:
|
||||||
// Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
|
// Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
|
||||||
@ -1280,22 +1410,19 @@ bool ValueTracker::getNextSourceFromRegSequence(unsigned &SrcReg,
|
|||||||
if (RegSeqInput.SubIdx == DefSubReg) {
|
if (RegSeqInput.SubIdx == DefSubReg) {
|
||||||
if (RegSeqInput.SubReg)
|
if (RegSeqInput.SubReg)
|
||||||
// Bails if we have to compose sub registers.
|
// Bails if we have to compose sub registers.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
SrcReg = RegSeqInput.Reg;
|
return ValueTrackerResult(RegSeqInput.Reg, RegSeqInput.SubReg);
|
||||||
SrcSubReg = RegSeqInput.SubReg;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the subreg we are tracking is super-defined by another subreg,
|
// If the subreg we are tracking is super-defined by another subreg,
|
||||||
// we could follow this value. However, this would require to compose
|
// we could follow this value. However, this would require to compose
|
||||||
// the subreg and we do not do that for now.
|
// the subreg and we do not do that for now.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromInsertSubreg(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromInsertSubreg() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert((Def->isInsertSubreg() || Def->isInsertSubregLike()) &&
|
assert((Def->isInsertSubreg() || Def->isInsertSubregLike()) &&
|
||||||
"Invalid definition");
|
"Invalid definition");
|
||||||
|
|
||||||
@ -1303,17 +1430,17 @@ bool ValueTracker::getNextSourceFromInsertSubreg(unsigned &SrcReg,
|
|||||||
// If we are composing subreg, bails out.
|
// If we are composing subreg, bails out.
|
||||||
// Same remark as getNextSourceFromRegSequence.
|
// Same remark as getNextSourceFromRegSequence.
|
||||||
// I.e., this may be turned into an assert.
|
// I.e., this may be turned into an assert.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
if (!TII)
|
if (!TII)
|
||||||
// We could handle the REG_SEQUENCE here, but we do not want to
|
// We could handle the REG_SEQUENCE here, but we do not want to
|
||||||
// duplicate the code from the generic TII.
|
// duplicate the code from the generic TII.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
TargetInstrInfo::RegSubRegPair BaseReg;
|
TargetInstrInfo::RegSubRegPair BaseReg;
|
||||||
TargetInstrInfo::RegSubRegPairAndIdx InsertedReg;
|
TargetInstrInfo::RegSubRegPairAndIdx InsertedReg;
|
||||||
if (!TII->getInsertSubregInputs(*Def, DefIdx, BaseReg, InsertedReg))
|
if (!TII->getInsertSubregInputs(*Def, DefIdx, BaseReg, InsertedReg))
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
// We are looking at:
|
// We are looking at:
|
||||||
// Def = INSERT_SUBREG v0, v1, sub1
|
// Def = INSERT_SUBREG v0, v1, sub1
|
||||||
@ -1323,9 +1450,7 @@ bool ValueTracker::getNextSourceFromInsertSubreg(unsigned &SrcReg,
|
|||||||
|
|
||||||
// #1 Check if the inserted register matches the required sub index.
|
// #1 Check if the inserted register matches the required sub index.
|
||||||
if (InsertedReg.SubIdx == DefSubReg) {
|
if (InsertedReg.SubIdx == DefSubReg) {
|
||||||
SrcReg = InsertedReg.Reg;
|
return ValueTrackerResult(InsertedReg.Reg, InsertedReg.SubReg);
|
||||||
SrcSubReg = InsertedReg.SubReg;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
// #2 Otherwise, if the sub register we are looking for is not partial
|
// #2 Otherwise, if the sub register we are looking for is not partial
|
||||||
// defined by the inserted element, we can look through the main
|
// defined by the inserted element, we can look through the main
|
||||||
@ -1336,7 +1461,7 @@ bool ValueTracker::getNextSourceFromInsertSubreg(unsigned &SrcReg,
|
|||||||
// subregisters, bails out.
|
// subregisters, bails out.
|
||||||
if (MRI.getRegClass(MODef.getReg()) != MRI.getRegClass(BaseReg.Reg) ||
|
if (MRI.getRegClass(MODef.getReg()) != MRI.getRegClass(BaseReg.Reg) ||
|
||||||
BaseReg.SubReg)
|
BaseReg.SubReg)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
// Get the TRI and check if the inserted sub-register overlaps with the
|
// Get the TRI and check if the inserted sub-register overlaps with the
|
||||||
// sub-register we are tracking.
|
// sub-register we are tracking.
|
||||||
@ -1344,16 +1469,13 @@ bool ValueTracker::getNextSourceFromInsertSubreg(unsigned &SrcReg,
|
|||||||
if (!TRI ||
|
if (!TRI ||
|
||||||
(TRI->getSubRegIndexLaneMask(DefSubReg) &
|
(TRI->getSubRegIndexLaneMask(DefSubReg) &
|
||||||
TRI->getSubRegIndexLaneMask(InsertedReg.SubIdx)) != 0)
|
TRI->getSubRegIndexLaneMask(InsertedReg.SubIdx)) != 0)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
// At this point, the value is available in v0 via the same subreg
|
// At this point, the value is available in v0 via the same subreg
|
||||||
// we used for Def.
|
// we used for Def.
|
||||||
SrcReg = BaseReg.Reg;
|
return ValueTrackerResult(BaseReg.Reg, DefSubReg);
|
||||||
SrcSubReg = DefSubReg;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromExtractSubreg(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromExtractSubreg() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert((Def->isExtractSubreg() ||
|
assert((Def->isExtractSubreg() ||
|
||||||
Def->isExtractSubregLike()) && "Invalid definition");
|
Def->isExtractSubregLike()) && "Invalid definition");
|
||||||
// We are looking at:
|
// We are looking at:
|
||||||
@ -1362,29 +1484,26 @@ bool ValueTracker::getNextSourceFromExtractSubreg(unsigned &SrcReg,
|
|||||||
// Bails if we have to compose sub registers.
|
// Bails if we have to compose sub registers.
|
||||||
// Indeed, if DefSubReg != 0, we would have to compose it with sub0.
|
// Indeed, if DefSubReg != 0, we would have to compose it with sub0.
|
||||||
if (DefSubReg)
|
if (DefSubReg)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
if (!TII)
|
if (!TII)
|
||||||
// We could handle the EXTRACT_SUBREG here, but we do not want to
|
// We could handle the EXTRACT_SUBREG here, but we do not want to
|
||||||
// duplicate the code from the generic TII.
|
// duplicate the code from the generic TII.
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
TargetInstrInfo::RegSubRegPairAndIdx ExtractSubregInputReg;
|
TargetInstrInfo::RegSubRegPairAndIdx ExtractSubregInputReg;
|
||||||
if (!TII->getExtractSubregInputs(*Def, DefIdx, ExtractSubregInputReg))
|
if (!TII->getExtractSubregInputs(*Def, DefIdx, ExtractSubregInputReg))
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
// Bails if we have to compose sub registers.
|
// Bails if we have to compose sub registers.
|
||||||
// Likewise, if v0.subreg != 0, we would have to compose v0.subreg with sub0.
|
// Likewise, if v0.subreg != 0, we would have to compose v0.subreg with sub0.
|
||||||
if (ExtractSubregInputReg.SubReg)
|
if (ExtractSubregInputReg.SubReg)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
// Otherwise, the value is available in the v0.sub0.
|
// Otherwise, the value is available in the v0.sub0.
|
||||||
SrcReg = ExtractSubregInputReg.Reg;
|
return ValueTrackerResult(ExtractSubregInputReg.Reg, ExtractSubregInputReg.SubIdx);
|
||||||
SrcSubReg = ExtractSubregInputReg.SubIdx;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceFromSubregToReg(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSourceFromSubregToReg() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
assert(Def->isSubregToReg() && "Invalid definition");
|
assert(Def->isSubregToReg() && "Invalid definition");
|
||||||
// We are looking at:
|
// We are looking at:
|
||||||
// Def = SUBREG_TO_REG Imm, v0, sub0
|
// Def = SUBREG_TO_REG Imm, v0, sub0
|
||||||
@ -1394,71 +1513,71 @@ bool ValueTracker::getNextSourceFromSubregToReg(unsigned &SrcReg,
|
|||||||
// we track are included in sub0 and if yes, we would have to
|
// we track are included in sub0 and if yes, we would have to
|
||||||
// determine the right subreg in v0.
|
// determine the right subreg in v0.
|
||||||
if (DefSubReg != Def->getOperand(3).getImm())
|
if (DefSubReg != Def->getOperand(3).getImm())
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
// Bails if we have to compose sub registers.
|
// Bails if we have to compose sub registers.
|
||||||
// Likewise, if v0.subreg != 0, we would have to compose it with sub0.
|
// Likewise, if v0.subreg != 0, we would have to compose it with sub0.
|
||||||
if (Def->getOperand(2).getSubReg())
|
if (Def->getOperand(2).getSubReg())
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
SrcReg = Def->getOperand(2).getReg();
|
return ValueTrackerResult(Def->getOperand(2).getReg(),
|
||||||
SrcSubReg = Def->getOperand(3).getImm();
|
Def->getOperand(3).getImm());
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValueTracker::getNextSourceImpl(unsigned &SrcReg, unsigned &SrcSubReg) {
|
ValueTrackerResult ValueTracker::getNextSourceImpl() {
|
||||||
assert(Def && "This method needs a valid definition");
|
assert(Def && "This method needs a valid definition");
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
(DefIdx < Def->getDesc().getNumDefs() || Def->getDesc().isVariadic()) &&
|
(DefIdx < Def->getDesc().getNumDefs() || Def->getDesc().isVariadic()) &&
|
||||||
Def->getOperand(DefIdx).isDef() && "Invalid DefIdx");
|
Def->getOperand(DefIdx).isDef() && "Invalid DefIdx");
|
||||||
if (Def->isCopy())
|
if (Def->isCopy())
|
||||||
return getNextSourceFromCopy(SrcReg, SrcSubReg);
|
return getNextSourceFromCopy();
|
||||||
if (Def->isBitcast())
|
if (Def->isBitcast())
|
||||||
return getNextSourceFromBitcast(SrcReg, SrcSubReg);
|
return getNextSourceFromBitcast();
|
||||||
// All the remaining cases involve "complex" instructions.
|
// All the remaining cases involve "complex" instructions.
|
||||||
// Bails if we did not ask for the advanced tracking.
|
// Bails if we did not ask for the advanced tracking.
|
||||||
if (!UseAdvancedTracking)
|
if (!UseAdvancedTracking)
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
if (Def->isRegSequence() || Def->isRegSequenceLike())
|
if (Def->isRegSequence() || Def->isRegSequenceLike())
|
||||||
return getNextSourceFromRegSequence(SrcReg, SrcSubReg);
|
return getNextSourceFromRegSequence();
|
||||||
if (Def->isInsertSubreg() || Def->isInsertSubregLike())
|
if (Def->isInsertSubreg() || Def->isInsertSubregLike())
|
||||||
return getNextSourceFromInsertSubreg(SrcReg, SrcSubReg);
|
return getNextSourceFromInsertSubreg();
|
||||||
if (Def->isExtractSubreg() || Def->isExtractSubregLike())
|
if (Def->isExtractSubreg() || Def->isExtractSubregLike())
|
||||||
return getNextSourceFromExtractSubreg(SrcReg, SrcSubReg);
|
return getNextSourceFromExtractSubreg();
|
||||||
if (Def->isSubregToReg())
|
if (Def->isSubregToReg())
|
||||||
return getNextSourceFromSubregToReg(SrcReg, SrcSubReg);
|
return getNextSourceFromSubregToReg();
|
||||||
return false;
|
return ValueTrackerResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
const MachineInstr *ValueTracker::getNextSource(unsigned &SrcReg,
|
ValueTrackerResult ValueTracker::getNextSource() {
|
||||||
unsigned &SrcSubReg) {
|
|
||||||
// If we reach a point where we cannot move up in the use-def chain,
|
// If we reach a point where we cannot move up in the use-def chain,
|
||||||
// there is nothing we can get.
|
// there is nothing we can get.
|
||||||
if (!Def)
|
if (!Def)
|
||||||
return nullptr;
|
return ValueTrackerResult();
|
||||||
|
|
||||||
const MachineInstr *PrevDef = nullptr;
|
ValueTrackerResult Res = getNextSourceImpl();
|
||||||
// Try to find the next source.
|
if (Res.isValid()) {
|
||||||
if (getNextSourceImpl(SrcReg, SrcSubReg)) {
|
|
||||||
// Update definition, definition index, and subregister for the
|
// Update definition, definition index, and subregister for the
|
||||||
// next call of getNextSource.
|
// next call of getNextSource.
|
||||||
// Update the current register.
|
// Update the current register.
|
||||||
Reg = SrcReg;
|
bool OneRegSrc = Res.getNumSources() == 1;
|
||||||
// Update the return value before moving up in the use-def chain.
|
if (OneRegSrc)
|
||||||
PrevDef = Def;
|
Reg = Res.getSrcReg(0);
|
||||||
|
// Update the result before moving up in the use-def chain
|
||||||
|
// with the instruction containing the last found sources.
|
||||||
|
Res.setInst(Def);
|
||||||
|
|
||||||
// If we can still move up in the use-def chain, move to the next
|
// If we can still move up in the use-def chain, move to the next
|
||||||
// defintion.
|
// defintion.
|
||||||
if (!TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
if (!TargetRegisterInfo::isPhysicalRegister(Reg) && OneRegSrc) {
|
||||||
Def = MRI.getVRegDef(Reg);
|
Def = MRI.getVRegDef(Reg);
|
||||||
DefIdx = MRI.def_begin(Reg).getOperandNo();
|
DefIdx = MRI.def_begin(Reg).getOperandNo();
|
||||||
DefSubReg = SrcSubReg;
|
DefSubReg = Res.getSrcSubReg(0);
|
||||||
return PrevDef;
|
return Res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we end up here, this means we will not be able to find another source
|
// If we end up here, this means we will not be able to find another source
|
||||||
// for the next iteration.
|
// for the next iteration. Make sure any new call to getNextSource bails out
|
||||||
// Make sure any new call to getNextSource bails out early by cutting the
|
// early by cutting the use-def chain.
|
||||||
// use-def chain.
|
|
||||||
Def = nullptr;
|
Def = nullptr;
|
||||||
return PrevDef;
|
return Res;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user