xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AggressiveAntiDepBreaker.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===- AggressiveAntiDepBreaker.cpp - Anti-dep breaker --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the AggressiveAntiDepBreaker class, which
100b57cec5SDimitry Andric // implements register anti-dependence breaking during post-RA
110b57cec5SDimitry Andric // scheduling. It attempts to break all anti-dependencies within a
120b57cec5SDimitry Andric // block.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "AggressiveAntiDepBreaker.h"
170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
200b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
340b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
350b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
360b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric #include <cassert>
390b57cec5SDimitry Andric #include <map>
400b57cec5SDimitry Andric #include <set>
410b57cec5SDimitry Andric #include <utility>
420b57cec5SDimitry Andric #include <vector>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #define DEBUG_TYPE "post-RA-sched"
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
490b57cec5SDimitry Andric static cl::opt<int>
500b57cec5SDimitry Andric DebugDiv("agg-antidep-debugdiv",
510b57cec5SDimitry Andric          cl::desc("Debug control for aggressive anti-dep breaker"),
520b57cec5SDimitry Andric          cl::init(0), cl::Hidden);
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric static cl::opt<int>
550b57cec5SDimitry Andric DebugMod("agg-antidep-debugmod",
560b57cec5SDimitry Andric          cl::desc("Debug control for aggressive anti-dep breaker"),
570b57cec5SDimitry Andric          cl::init(0), cl::Hidden);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
600b57cec5SDimitry Andric                                                MachineBasicBlock *BB)
610b57cec5SDimitry Andric     : NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
620b57cec5SDimitry Andric       GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0),
630b57cec5SDimitry Andric       DefIndices(TargetRegs, 0) {
640b57cec5SDimitry Andric   const unsigned BBSize = BB->size();
650b57cec5SDimitry Andric   for (unsigned i = 0; i < NumTargetRegs; ++i) {
660b57cec5SDimitry Andric     // Initialize all registers to be in their own group. Initially we
670b57cec5SDimitry Andric     // assign the register to the same-indexed GroupNode.
680b57cec5SDimitry Andric     GroupNodeIndices[i] = i;
690b57cec5SDimitry Andric     // Initialize the indices to indicate that no registers are live.
700b57cec5SDimitry Andric     KillIndices[i] = ~0u;
710b57cec5SDimitry Andric     DefIndices[i] = BBSize;
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
760b57cec5SDimitry Andric   unsigned Node = GroupNodeIndices[Reg];
770b57cec5SDimitry Andric   while (GroupNodes[Node] != Node)
780b57cec5SDimitry Andric     Node = GroupNodes[Node];
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   return Node;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric void AggressiveAntiDepState::GetGroupRegs(
840b57cec5SDimitry Andric   unsigned Group,
850b57cec5SDimitry Andric   std::vector<unsigned> &Regs,
860b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
870b57cec5SDimitry Andric {
880b57cec5SDimitry Andric   for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
890b57cec5SDimitry Andric     if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
900b57cec5SDimitry Andric       Regs.push_back(Reg);
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) {
950b57cec5SDimitry Andric   assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
960b57cec5SDimitry Andric   assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   // find group for each register
990b57cec5SDimitry Andric   unsigned Group1 = GetGroup(Reg1);
1000b57cec5SDimitry Andric   unsigned Group2 = GetGroup(Reg2);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // if either group is 0, then that must become the parent
1030b57cec5SDimitry Andric   unsigned Parent = (Group1 == 0) ? Group1 : Group2;
1040b57cec5SDimitry Andric   unsigned Other = (Parent == Group1) ? Group2 : Group1;
1050b57cec5SDimitry Andric   GroupNodes.at(Other) = Parent;
1060b57cec5SDimitry Andric   return Parent;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) {
1100b57cec5SDimitry Andric   // Create a new GroupNode for Reg. Reg's existing GroupNode must
1110b57cec5SDimitry Andric   // stay as is because there could be other GroupNodes referring to
1120b57cec5SDimitry Andric   // it.
1130b57cec5SDimitry Andric   unsigned idx = GroupNodes.size();
1140b57cec5SDimitry Andric   GroupNodes.push_back(idx);
1150b57cec5SDimitry Andric   GroupNodeIndices[Reg] = idx;
1160b57cec5SDimitry Andric   return idx;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric bool AggressiveAntiDepState::IsLive(unsigned Reg) {
1200b57cec5SDimitry Andric   // KillIndex must be defined and DefIndex not defined for a register
1210b57cec5SDimitry Andric   // to be live.
1220b57cec5SDimitry Andric   return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
1260b57cec5SDimitry Andric     MachineFunction &MFi, const RegisterClassInfo &RCI,
1270b57cec5SDimitry Andric     TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
1280b57cec5SDimitry Andric     : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
1290b57cec5SDimitry Andric       TII(MF.getSubtarget().getInstrInfo()),
1300b57cec5SDimitry Andric       TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) {
1310b57cec5SDimitry Andric   /* Collect a bitset of all registers that are only broken if they
1320b57cec5SDimitry Andric      are on the critical path. */
1330b57cec5SDimitry Andric   for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
1340b57cec5SDimitry Andric     BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
1350b57cec5SDimitry Andric     if (CriticalPathSet.none())
1360b57cec5SDimitry Andric       CriticalPathSet = CPSet;
1370b57cec5SDimitry Andric     else
1380b57cec5SDimitry Andric       CriticalPathSet |= CPSet;
1390b57cec5SDimitry Andric    }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric    LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
1420b57cec5SDimitry Andric    LLVM_DEBUG(for (unsigned r
1430b57cec5SDimitry Andric                    : CriticalPathSet.set_bits()) dbgs()
1440b57cec5SDimitry Andric               << " " << printReg(r, TRI));
1450b57cec5SDimitry Andric    LLVM_DEBUG(dbgs() << '\n');
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
1490b57cec5SDimitry Andric   delete State;
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
1530b57cec5SDimitry Andric   assert(!State);
1540b57cec5SDimitry Andric   State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   bool IsReturnBlock = BB->isReturnBlock();
1570b57cec5SDimitry Andric   std::vector<unsigned> &KillIndices = State->GetKillIndices();
1580b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Examine the live-in regs of all successors.
1610b57cec5SDimitry Andric   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1620b57cec5SDimitry Andric          SE = BB->succ_end(); SI != SE; ++SI)
1630b57cec5SDimitry Andric     for (const auto &LI : (*SI)->liveins()) {
1640b57cec5SDimitry Andric       for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
1650b57cec5SDimitry Andric         unsigned Reg = *AI;
1660b57cec5SDimitry Andric         State->UnionGroups(Reg, 0);
1670b57cec5SDimitry Andric         KillIndices[Reg] = BB->size();
1680b57cec5SDimitry Andric         DefIndices[Reg] = ~0u;
1690b57cec5SDimitry Andric       }
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   // Mark live-out callee-saved registers. In a return block this is
1730b57cec5SDimitry Andric   // all callee-saved registers. In non-return this is any
1740b57cec5SDimitry Andric   // callee-saved register that is not saved in the prolog.
1750b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
1760b57cec5SDimitry Andric   BitVector Pristine = MFI.getPristineRegs(MF);
1770b57cec5SDimitry Andric   for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
1780b57cec5SDimitry Andric        ++I) {
1790b57cec5SDimitry Andric     unsigned Reg = *I;
1800b57cec5SDimitry Andric     if (!IsReturnBlock && !Pristine.test(Reg))
1810b57cec5SDimitry Andric       continue;
1820b57cec5SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
1830b57cec5SDimitry Andric       unsigned AliasReg = *AI;
1840b57cec5SDimitry Andric       State->UnionGroups(AliasReg, 0);
1850b57cec5SDimitry Andric       KillIndices[AliasReg] = BB->size();
1860b57cec5SDimitry Andric       DefIndices[AliasReg] = ~0u;
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric void AggressiveAntiDepBreaker::FinishBlock() {
1920b57cec5SDimitry Andric   delete State;
1930b57cec5SDimitry Andric   State = nullptr;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
1970b57cec5SDimitry Andric                                        unsigned InsertPosIndex) {
1980b57cec5SDimitry Andric   assert(Count < InsertPosIndex && "Instruction index out of expected range!");
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   std::set<unsigned> PassthruRegs;
2010b57cec5SDimitry Andric   GetPassthruRegs(MI, PassthruRegs);
2020b57cec5SDimitry Andric   PrescanInstruction(MI, Count, PassthruRegs);
2030b57cec5SDimitry Andric   ScanInstruction(MI, Count);
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Observe: ");
2060b57cec5SDimitry Andric   LLVM_DEBUG(MI.dump());
2070b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tRegs:");
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
2100b57cec5SDimitry Andric   for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
2110b57cec5SDimitry Andric     // If Reg is current live, then mark that it can't be renamed as
2120b57cec5SDimitry Andric     // we don't know the extent of its live-range anymore (now that it
2130b57cec5SDimitry Andric     // has been scheduled). If it is not live but was defined in the
2140b57cec5SDimitry Andric     // previous schedule region, then set its def index to the most
2150b57cec5SDimitry Andric     // conservative location (i.e. the beginning of the previous
2160b57cec5SDimitry Andric     // schedule region).
2170b57cec5SDimitry Andric     if (State->IsLive(Reg)) {
2180b57cec5SDimitry Andric       LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs()
2190b57cec5SDimitry Andric                  << " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg)
2200b57cec5SDimitry Andric                  << "->g0(region live-out)");
2210b57cec5SDimitry Andric       State->UnionGroups(Reg, 0);
2220b57cec5SDimitry Andric     } else if ((DefIndices[Reg] < InsertPosIndex)
2230b57cec5SDimitry Andric                && (DefIndices[Reg] >= Count)) {
2240b57cec5SDimitry Andric       DefIndices[Reg] = Count;
2250b57cec5SDimitry Andric     }
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
2310b57cec5SDimitry Andric                                                 MachineOperand &MO) {
2320b57cec5SDimitry Andric   if (!MO.isReg() || !MO.isImplicit())
2330b57cec5SDimitry Andric     return false;
2340b57cec5SDimitry Andric 
235*8bcb0991SDimitry Andric   Register Reg = MO.getReg();
2360b57cec5SDimitry Andric   if (Reg == 0)
2370b57cec5SDimitry Andric     return false;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   MachineOperand *Op = nullptr;
2400b57cec5SDimitry Andric   if (MO.isDef())
2410b57cec5SDimitry Andric     Op = MI.findRegisterUseOperand(Reg, true);
2420b57cec5SDimitry Andric   else
2430b57cec5SDimitry Andric     Op = MI.findRegisterDefOperand(Reg);
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   return(Op && Op->isImplicit());
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric void AggressiveAntiDepBreaker::GetPassthruRegs(
2490b57cec5SDimitry Andric     MachineInstr &MI, std::set<unsigned> &PassthruRegs) {
2500b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2510b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
2520b57cec5SDimitry Andric     if (!MO.isReg()) continue;
2530b57cec5SDimitry Andric     if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
2540b57cec5SDimitry Andric         IsImplicitDefUse(MI, MO)) {
255*8bcb0991SDimitry Andric       const Register Reg = MO.getReg();
2560b57cec5SDimitry Andric       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
2570b57cec5SDimitry Andric            SubRegs.isValid(); ++SubRegs)
2580b57cec5SDimitry Andric         PassthruRegs.insert(*SubRegs);
2590b57cec5SDimitry Andric     }
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric /// AntiDepEdges - Return in Edges the anti- and output- dependencies
2640b57cec5SDimitry Andric /// in SU that we want to consider for breaking.
2650b57cec5SDimitry Andric static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) {
2660b57cec5SDimitry Andric   SmallSet<unsigned, 4> RegSet;
2670b57cec5SDimitry Andric   for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
2680b57cec5SDimitry Andric        P != PE; ++P) {
2690b57cec5SDimitry Andric     if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
2700b57cec5SDimitry Andric       if (RegSet.insert(P->getReg()).second)
2710b57cec5SDimitry Andric         Edges.push_back(&*P);
2720b57cec5SDimitry Andric     }
2730b57cec5SDimitry Andric   }
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
2770b57cec5SDimitry Andric /// critical path.
2780b57cec5SDimitry Andric static const SUnit *CriticalPathStep(const SUnit *SU) {
2790b57cec5SDimitry Andric   const SDep *Next = nullptr;
2800b57cec5SDimitry Andric   unsigned NextDepth = 0;
2810b57cec5SDimitry Andric   // Find the predecessor edge with the greatest depth.
2820b57cec5SDimitry Andric   if (SU) {
2830b57cec5SDimitry Andric     for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
2840b57cec5SDimitry Andric          P != PE; ++P) {
2850b57cec5SDimitry Andric       const SUnit *PredSU = P->getSUnit();
2860b57cec5SDimitry Andric       unsigned PredLatency = P->getLatency();
2870b57cec5SDimitry Andric       unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
2880b57cec5SDimitry Andric       // In the case of a latency tie, prefer an anti-dependency edge over
2890b57cec5SDimitry Andric       // other types of edges.
2900b57cec5SDimitry Andric       if (NextDepth < PredTotalLatency ||
2910b57cec5SDimitry Andric           (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
2920b57cec5SDimitry Andric         NextDepth = PredTotalLatency;
2930b57cec5SDimitry Andric         Next = &*P;
2940b57cec5SDimitry Andric       }
2950b57cec5SDimitry Andric     }
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   return (Next) ? Next->getSUnit() : nullptr;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
3020b57cec5SDimitry Andric                                              const char *tag,
3030b57cec5SDimitry Andric                                              const char *header,
3040b57cec5SDimitry Andric                                              const char *footer) {
3050b57cec5SDimitry Andric   std::vector<unsigned> &KillIndices = State->GetKillIndices();
3060b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
3070b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
3080b57cec5SDimitry Andric     RegRefs = State->GetRegRefs();
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // FIXME: We must leave subregisters of live super registers as live, so that
3110b57cec5SDimitry Andric   // we don't clear out the register tracking information for subregisters of
3120b57cec5SDimitry Andric   // super registers we're still tracking (and with which we're unioning
3130b57cec5SDimitry Andric   // subregister definitions).
3140b57cec5SDimitry Andric   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
3150b57cec5SDimitry Andric     if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
3160b57cec5SDimitry Andric       LLVM_DEBUG(if (!header && footer) dbgs() << footer);
3170b57cec5SDimitry Andric       return;
3180b57cec5SDimitry Andric     }
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   if (!State->IsLive(Reg)) {
3210b57cec5SDimitry Andric     KillIndices[Reg] = KillIdx;
3220b57cec5SDimitry Andric     DefIndices[Reg] = ~0u;
3230b57cec5SDimitry Andric     RegRefs.erase(Reg);
3240b57cec5SDimitry Andric     State->LeaveGroup(Reg);
3250b57cec5SDimitry Andric     LLVM_DEBUG(if (header) {
3260b57cec5SDimitry Andric       dbgs() << header << printReg(Reg, TRI);
3270b57cec5SDimitry Andric       header = nullptr;
3280b57cec5SDimitry Andric     });
3290b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
3300b57cec5SDimitry Andric     // Repeat for subregisters. Note that we only do this if the superregister
3310b57cec5SDimitry Andric     // was not live because otherwise, regardless whether we have an explicit
3320b57cec5SDimitry Andric     // use of the subregister, the subregister's contents are needed for the
3330b57cec5SDimitry Andric     // uses of the superregister.
3340b57cec5SDimitry Andric     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
3350b57cec5SDimitry Andric       unsigned SubregReg = *SubRegs;
3360b57cec5SDimitry Andric       if (!State->IsLive(SubregReg)) {
3370b57cec5SDimitry Andric         KillIndices[SubregReg] = KillIdx;
3380b57cec5SDimitry Andric         DefIndices[SubregReg] = ~0u;
3390b57cec5SDimitry Andric         RegRefs.erase(SubregReg);
3400b57cec5SDimitry Andric         State->LeaveGroup(SubregReg);
3410b57cec5SDimitry Andric         LLVM_DEBUG(if (header) {
3420b57cec5SDimitry Andric           dbgs() << header << printReg(Reg, TRI);
3430b57cec5SDimitry Andric           header = nullptr;
3440b57cec5SDimitry Andric         });
3450b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g"
3460b57cec5SDimitry Andric                           << State->GetGroup(SubregReg) << tag);
3470b57cec5SDimitry Andric       }
3480b57cec5SDimitry Andric     }
3490b57cec5SDimitry Andric   }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   LLVM_DEBUG(if (!header && footer) dbgs() << footer);
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric void AggressiveAntiDepBreaker::PrescanInstruction(
3550b57cec5SDimitry Andric     MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {
3560b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
3570b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
3580b57cec5SDimitry Andric     RegRefs = State->GetRegRefs();
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   // Handle dead defs by simulating a last-use of the register just
3610b57cec5SDimitry Andric   // after the def. A dead def can occur because the def is truly
3620b57cec5SDimitry Andric   // dead, or because only a subregister is live at the def. If we
3630b57cec5SDimitry Andric   // don't do this the dead def will be incorrectly merged into the
3640b57cec5SDimitry Andric   // previous def.
3650b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3660b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
3670b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef()) continue;
368*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
3690b57cec5SDimitry Andric     if (Reg == 0) continue;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric     HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tDef Groups:");
3750b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3760b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
3770b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef()) continue;
378*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
3790b57cec5SDimitry Andric     if (Reg == 0) continue;
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
3820b57cec5SDimitry Andric                       << State->GetGroup(Reg));
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric     // If MI's defs have a special allocation requirement, don't allow
3850b57cec5SDimitry Andric     // any def registers to be changed. Also assume all registers
3860b57cec5SDimitry Andric     // defined in a call must not be changed (ABI). Inline assembly may
3870b57cec5SDimitry Andric     // reference either system calls or the register directly. Skip it until we
3880b57cec5SDimitry Andric     // can tell user specified registers from compiler-specified.
3890b57cec5SDimitry Andric     if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
3900b57cec5SDimitry Andric         MI.isInlineAsm()) {
3910b57cec5SDimitry Andric       LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
3920b57cec5SDimitry Andric       State->UnionGroups(Reg, 0);
3930b57cec5SDimitry Andric     }
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric     // Any aliased that are live at this point are completely or
3960b57cec5SDimitry Andric     // partially defined here, so group those aliases with Reg.
3970b57cec5SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
3980b57cec5SDimitry Andric       unsigned AliasReg = *AI;
3990b57cec5SDimitry Andric       if (State->IsLive(AliasReg)) {
4000b57cec5SDimitry Andric         State->UnionGroups(Reg, AliasReg);
4010b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via "
4020b57cec5SDimitry Andric                           << printReg(AliasReg, TRI) << ")");
4030b57cec5SDimitry Andric       }
4040b57cec5SDimitry Andric     }
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric     // Note register reference...
4070b57cec5SDimitry Andric     const TargetRegisterClass *RC = nullptr;
4080b57cec5SDimitry Andric     if (i < MI.getDesc().getNumOperands())
4090b57cec5SDimitry Andric       RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
4100b57cec5SDimitry Andric     AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
4110b57cec5SDimitry Andric     RegRefs.insert(std::make_pair(Reg, RR));
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   // Scan the register defs for this instruction and update
4170b57cec5SDimitry Andric   // live-ranges.
4180b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
4190b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
4200b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef()) continue;
421*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
4220b57cec5SDimitry Andric     if (Reg == 0) continue;
4230b57cec5SDimitry Andric     // Ignore KILLs and passthru registers for liveness...
4240b57cec5SDimitry Andric     if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
4250b57cec5SDimitry Andric       continue;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric     // Update def for Reg and aliases.
4280b57cec5SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
4290b57cec5SDimitry Andric       // We need to be careful here not to define already-live super registers.
4300b57cec5SDimitry Andric       // If the super register is already live, then this definition is not
4310b57cec5SDimitry Andric       // a definition of the whole super register (just a partial insertion
4320b57cec5SDimitry Andric       // into it). Earlier subregister definitions (which we've not yet visited
4330b57cec5SDimitry Andric       // because we're iterating bottom-up) need to be linked to the same group
4340b57cec5SDimitry Andric       // as this definition.
4350b57cec5SDimitry Andric       if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
4360b57cec5SDimitry Andric         continue;
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric       DefIndices[*AI] = Count;
4390b57cec5SDimitry Andric     }
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
4440b57cec5SDimitry Andric                                                unsigned Count) {
4450b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tUse Groups:");
4460b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
4470b57cec5SDimitry Andric     RegRefs = State->GetRegRefs();
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // If MI's uses have special allocation requirement, don't allow
4500b57cec5SDimitry Andric   // any use registers to be changed. Also assume all registers
4510b57cec5SDimitry Andric   // used in a call must not be changed (ABI).
4520b57cec5SDimitry Andric   // Inline Assembly register uses also cannot be safely changed.
4530b57cec5SDimitry Andric   // FIXME: The issue with predicated instruction is more complex. We are being
4540b57cec5SDimitry Andric   // conservatively here because the kill markers cannot be trusted after
4550b57cec5SDimitry Andric   // if-conversion:
4560b57cec5SDimitry Andric   // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
4570b57cec5SDimitry Andric   // ...
4580b57cec5SDimitry Andric   // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
4590b57cec5SDimitry Andric   // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
4600b57cec5SDimitry Andric   // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
4610b57cec5SDimitry Andric   //
4620b57cec5SDimitry Andric   // The first R6 kill is not really a kill since it's killed by a predicated
4630b57cec5SDimitry Andric   // instruction which may not be executed. The second R6 def may or may not
4640b57cec5SDimitry Andric   // re-define R6 so it's not safe to change it since the last R6 use cannot be
4650b57cec5SDimitry Andric   // changed.
4660b57cec5SDimitry Andric   bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
4670b57cec5SDimitry Andric                  TII->isPredicated(MI) || MI.isInlineAsm();
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   // Scan the register uses for this instruction and update
4700b57cec5SDimitry Andric   // live-ranges, groups and RegRefs.
4710b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
4720b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
4730b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isUse()) continue;
474*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
4750b57cec5SDimitry Andric     if (Reg == 0) continue;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
4780b57cec5SDimitry Andric                       << State->GetGroup(Reg));
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     // It wasn't previously live but now it is, this is a kill. Forget
4810b57cec5SDimitry Andric     // the previous live-range information and start a new live-range
4820b57cec5SDimitry Andric     // for the register.
4830b57cec5SDimitry Andric     HandleLastUse(Reg, Count, "(last-use)");
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric     if (Special) {
4860b57cec5SDimitry Andric       LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
4870b57cec5SDimitry Andric       State->UnionGroups(Reg, 0);
4880b57cec5SDimitry Andric     }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric     // Note register reference...
4910b57cec5SDimitry Andric     const TargetRegisterClass *RC = nullptr;
4920b57cec5SDimitry Andric     if (i < MI.getDesc().getNumOperands())
4930b57cec5SDimitry Andric       RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
4940b57cec5SDimitry Andric     AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
4950b57cec5SDimitry Andric     RegRefs.insert(std::make_pair(Reg, RR));
4960b57cec5SDimitry Andric   }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric   // Form a group of all defs and uses of a KILL instruction to ensure
5010b57cec5SDimitry Andric   // that all registers are renamed as a group.
5020b57cec5SDimitry Andric   if (MI.isKill()) {
5030b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\tKill Group:");
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric     unsigned FirstReg = 0;
5060b57cec5SDimitry Andric     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
5070b57cec5SDimitry Andric       MachineOperand &MO = MI.getOperand(i);
5080b57cec5SDimitry Andric       if (!MO.isReg()) continue;
509*8bcb0991SDimitry Andric       Register Reg = MO.getReg();
5100b57cec5SDimitry Andric       if (Reg == 0) continue;
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric       if (FirstReg != 0) {
5130b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI));
5140b57cec5SDimitry Andric         State->UnionGroups(FirstReg, Reg);
5150b57cec5SDimitry Andric       } else {
5160b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
5170b57cec5SDimitry Andric         FirstReg = Reg;
5180b57cec5SDimitry Andric       }
5190b57cec5SDimitry Andric     }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
5220b57cec5SDimitry Andric   }
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
5260b57cec5SDimitry Andric   BitVector BV(TRI->getNumRegs(), false);
5270b57cec5SDimitry Andric   bool first = true;
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   // Check all references that need rewriting for Reg. For each, use
5300b57cec5SDimitry Andric   // the corresponding register class to narrow the set of registers
5310b57cec5SDimitry Andric   // that are appropriate for renaming.
5320b57cec5SDimitry Andric   for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
5330b57cec5SDimitry Andric     const TargetRegisterClass *RC = Q.second.RC;
5340b57cec5SDimitry Andric     if (!RC) continue;
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric     BitVector RCBV = TRI->getAllocatableSet(MF, RC);
5370b57cec5SDimitry Andric     if (first) {
5380b57cec5SDimitry Andric       BV |= RCBV;
5390b57cec5SDimitry Andric       first = false;
5400b57cec5SDimitry Andric     } else {
5410b57cec5SDimitry Andric       BV &= RCBV;
5420b57cec5SDimitry Andric     }
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
5450b57cec5SDimitry Andric   }
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   return BV;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
5510b57cec5SDimitry Andric                                 unsigned AntiDepGroupIndex,
5520b57cec5SDimitry Andric                                 RenameOrderType& RenameOrder,
5530b57cec5SDimitry Andric                                 std::map<unsigned, unsigned> &RenameMap) {
5540b57cec5SDimitry Andric   std::vector<unsigned> &KillIndices = State->GetKillIndices();
5550b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
5560b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
5570b57cec5SDimitry Andric     RegRefs = State->GetRegRefs();
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   // Collect all referenced registers in the same group as
5600b57cec5SDimitry Andric   // AntiDepReg. These all need to be renamed together if we are to
5610b57cec5SDimitry Andric   // break the anti-dependence.
5620b57cec5SDimitry Andric   std::vector<unsigned> Regs;
5630b57cec5SDimitry Andric   State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
5640b57cec5SDimitry Andric   assert(!Regs.empty() && "Empty register group!");
5650b57cec5SDimitry Andric   if (Regs.empty())
5660b57cec5SDimitry Andric     return false;
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   // Find the "superest" register in the group. At the same time,
5690b57cec5SDimitry Andric   // collect the BitVector of registers that can be used to rename
5700b57cec5SDimitry Andric   // each register.
5710b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
5720b57cec5SDimitry Andric                     << ":\n");
5730b57cec5SDimitry Andric   std::map<unsigned, BitVector> RenameRegisterMap;
5740b57cec5SDimitry Andric   unsigned SuperReg = 0;
5750b57cec5SDimitry Andric   for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
5760b57cec5SDimitry Andric     unsigned Reg = Regs[i];
5770b57cec5SDimitry Andric     if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
5780b57cec5SDimitry Andric       SuperReg = Reg;
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric     // If Reg has any references, then collect possible rename regs
5810b57cec5SDimitry Andric     if (RegRefs.count(Reg) > 0) {
5820b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":");
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric       BitVector &BV = RenameRegisterMap[Reg];
5850b57cec5SDimitry Andric       assert(BV.empty());
5860b57cec5SDimitry Andric       BV = GetRenameRegisters(Reg);
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric       LLVM_DEBUG({
5890b57cec5SDimitry Andric         dbgs() << " ::";
5900b57cec5SDimitry Andric         for (unsigned r : BV.set_bits())
5910b57cec5SDimitry Andric           dbgs() << " " << printReg(r, TRI);
5920b57cec5SDimitry Andric         dbgs() << "\n";
5930b57cec5SDimitry Andric       });
5940b57cec5SDimitry Andric     }
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   // All group registers should be a subreg of SuperReg.
5980b57cec5SDimitry Andric   for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
5990b57cec5SDimitry Andric     unsigned Reg = Regs[i];
6000b57cec5SDimitry Andric     if (Reg == SuperReg) continue;
6010b57cec5SDimitry Andric     bool IsSub = TRI->isSubRegister(SuperReg, Reg);
6020b57cec5SDimitry Andric     // FIXME: remove this once PR18663 has been properly fixed. For now,
6030b57cec5SDimitry Andric     // return a conservative answer:
6040b57cec5SDimitry Andric     // assert(IsSub && "Expecting group subregister");
6050b57cec5SDimitry Andric     if (!IsSub)
6060b57cec5SDimitry Andric       return false;
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric #ifndef NDEBUG
6100b57cec5SDimitry Andric   // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
6110b57cec5SDimitry Andric   if (DebugDiv > 0) {
6120b57cec5SDimitry Andric     static int renamecnt = 0;
6130b57cec5SDimitry Andric     if (renamecnt++ % DebugDiv != DebugMod)
6140b57cec5SDimitry Andric       return false;
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric     dbgs() << "*** Performing rename " << printReg(SuperReg, TRI)
6170b57cec5SDimitry Andric            << " for debug ***\n";
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric #endif
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   // Check each possible rename register for SuperReg in round-robin
6220b57cec5SDimitry Andric   // order. If that register is available, and the corresponding
6230b57cec5SDimitry Andric   // registers are available for the other group subregisters, then we
6240b57cec5SDimitry Andric   // can use those registers to rename.
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   // FIXME: Using getMinimalPhysRegClass is very conservative. We should
6270b57cec5SDimitry Andric   // check every use of the register and find the largest register class
6280b57cec5SDimitry Andric   // that can be used in all of them.
6290b57cec5SDimitry Andric   const TargetRegisterClass *SuperRC =
6300b57cec5SDimitry Andric     TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
6330b57cec5SDimitry Andric   if (Order.empty()) {
6340b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
6350b57cec5SDimitry Andric     return false;
6360b57cec5SDimitry Andric   }
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tFind Registers:");
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric   unsigned OrigR = RenameOrder[SuperRC];
6430b57cec5SDimitry Andric   unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
6440b57cec5SDimitry Andric   unsigned R = OrigR;
6450b57cec5SDimitry Andric   do {
6460b57cec5SDimitry Andric     if (R == 0) R = Order.size();
6470b57cec5SDimitry Andric     --R;
6480b57cec5SDimitry Andric     const unsigned NewSuperReg = Order[R];
6490b57cec5SDimitry Andric     // Don't consider non-allocatable registers
6500b57cec5SDimitry Andric     if (!MRI.isAllocatable(NewSuperReg)) continue;
6510b57cec5SDimitry Andric     // Don't replace a register with itself.
6520b57cec5SDimitry Andric     if (NewSuperReg == SuperReg) continue;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':');
6550b57cec5SDimitry Andric     RenameMap.clear();
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric     // For each referenced group register (which must be a SuperReg or
6580b57cec5SDimitry Andric     // a subregister of SuperReg), find the corresponding subregister
6590b57cec5SDimitry Andric     // of NewSuperReg and make sure it is free to be renamed.
6600b57cec5SDimitry Andric     for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
6610b57cec5SDimitry Andric       unsigned Reg = Regs[i];
6620b57cec5SDimitry Andric       unsigned NewReg = 0;
6630b57cec5SDimitry Andric       if (Reg == SuperReg) {
6640b57cec5SDimitry Andric         NewReg = NewSuperReg;
6650b57cec5SDimitry Andric       } else {
6660b57cec5SDimitry Andric         unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
6670b57cec5SDimitry Andric         if (NewSubRegIdx != 0)
6680b57cec5SDimitry Andric           NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
6690b57cec5SDimitry Andric       }
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI));
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric       // Check if Reg can be renamed to NewReg.
6740b57cec5SDimitry Andric       if (!RenameRegisterMap[Reg].test(NewReg)) {
6750b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "(no rename)");
6760b57cec5SDimitry Andric         goto next_super_reg;
6770b57cec5SDimitry Andric       }
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric       // If NewReg is dead and NewReg's most recent def is not before
6800b57cec5SDimitry Andric       // Regs's kill, it's safe to replace Reg with NewReg. We
6810b57cec5SDimitry Andric       // must also check all aliases of NewReg, because we can't define a
6820b57cec5SDimitry Andric       // register when any sub or super is already live.
6830b57cec5SDimitry Andric       if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
6840b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "(live)");
6850b57cec5SDimitry Andric         goto next_super_reg;
6860b57cec5SDimitry Andric       } else {
6870b57cec5SDimitry Andric         bool found = false;
6880b57cec5SDimitry Andric         for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
6890b57cec5SDimitry Andric           unsigned AliasReg = *AI;
6900b57cec5SDimitry Andric           if (State->IsLive(AliasReg) ||
6910b57cec5SDimitry Andric               (KillIndices[Reg] > DefIndices[AliasReg])) {
6920b57cec5SDimitry Andric             LLVM_DEBUG(dbgs()
6930b57cec5SDimitry Andric                        << "(alias " << printReg(AliasReg, TRI) << " live)");
6940b57cec5SDimitry Andric             found = true;
6950b57cec5SDimitry Andric             break;
6960b57cec5SDimitry Andric           }
6970b57cec5SDimitry Andric         }
6980b57cec5SDimitry Andric         if (found)
6990b57cec5SDimitry Andric           goto next_super_reg;
7000b57cec5SDimitry Andric       }
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric       // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
7030b57cec5SDimitry Andric       // defines 'NewReg' via an early-clobber operand.
7040b57cec5SDimitry Andric       for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
7050b57cec5SDimitry Andric         MachineInstr *UseMI = Q.second.Operand->getParent();
7060b57cec5SDimitry Andric         int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
7070b57cec5SDimitry Andric         if (Idx == -1)
7080b57cec5SDimitry Andric           continue;
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric         if (UseMI->getOperand(Idx).isEarlyClobber()) {
7110b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "(ec)");
7120b57cec5SDimitry Andric           goto next_super_reg;
7130b57cec5SDimitry Andric         }
7140b57cec5SDimitry Andric       }
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric       // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
7170b57cec5SDimitry Andric       // 'Reg' is an early-clobber define and that instruction also uses
7180b57cec5SDimitry Andric       // 'NewReg'.
7190b57cec5SDimitry Andric       for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
7200b57cec5SDimitry Andric         if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
7210b57cec5SDimitry Andric           continue;
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric         MachineInstr *DefMI = Q.second.Operand->getParent();
7240b57cec5SDimitry Andric         if (DefMI->readsRegister(NewReg, TRI)) {
7250b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "(ec)");
7260b57cec5SDimitry Andric           goto next_super_reg;
7270b57cec5SDimitry Andric         }
7280b57cec5SDimitry Andric       }
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric       // Record that 'Reg' can be renamed to 'NewReg'.
7310b57cec5SDimitry Andric       RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
7320b57cec5SDimitry Andric     }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric     // If we fall-out here, then every register in the group can be
7350b57cec5SDimitry Andric     // renamed, as recorded in RenameMap.
7360b57cec5SDimitry Andric     RenameOrder.erase(SuperRC);
7370b57cec5SDimitry Andric     RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
7380b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "]\n");
7390b57cec5SDimitry Andric     return true;
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   next_super_reg:
7420b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ']');
7430b57cec5SDimitry Andric   } while (R != EndR);
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   // No registers are free and available!
7480b57cec5SDimitry Andric   return false;
7490b57cec5SDimitry Andric }
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric /// BreakAntiDependencies - Identifiy anti-dependencies within the
7520b57cec5SDimitry Andric /// ScheduleDAG and break them by renaming registers.
7530b57cec5SDimitry Andric unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
7540b57cec5SDimitry Andric                               const std::vector<SUnit> &SUnits,
7550b57cec5SDimitry Andric                               MachineBasicBlock::iterator Begin,
7560b57cec5SDimitry Andric                               MachineBasicBlock::iterator End,
7570b57cec5SDimitry Andric                               unsigned InsertPosIndex,
7580b57cec5SDimitry Andric                               DbgValueVector &DbgValues) {
7590b57cec5SDimitry Andric   std::vector<unsigned> &KillIndices = State->GetKillIndices();
7600b57cec5SDimitry Andric   std::vector<unsigned> &DefIndices = State->GetDefIndices();
7610b57cec5SDimitry Andric   std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
7620b57cec5SDimitry Andric     RegRefs = State->GetRegRefs();
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   // The code below assumes that there is at least one instruction,
7650b57cec5SDimitry Andric   // so just duck out immediately if the block is empty.
7660b57cec5SDimitry Andric   if (SUnits.empty()) return 0;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   // For each regclass the next register to use for renaming.
7690b57cec5SDimitry Andric   RenameOrderType RenameOrder;
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   // ...need a map from MI to SUnit.
7720b57cec5SDimitry Andric   std::map<MachineInstr *, const SUnit *> MISUnitMap;
7730b57cec5SDimitry Andric   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
7740b57cec5SDimitry Andric     const SUnit *SU = &SUnits[i];
7750b57cec5SDimitry Andric     MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
7760b57cec5SDimitry Andric                                                                SU));
7770b57cec5SDimitry Andric   }
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric   // Track progress along the critical path through the SUnit graph as
7800b57cec5SDimitry Andric   // we walk the instructions. This is needed for regclasses that only
7810b57cec5SDimitry Andric   // break critical-path anti-dependencies.
7820b57cec5SDimitry Andric   const SUnit *CriticalPathSU = nullptr;
7830b57cec5SDimitry Andric   MachineInstr *CriticalPathMI = nullptr;
7840b57cec5SDimitry Andric   if (CriticalPathSet.any()) {
7850b57cec5SDimitry Andric     for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
7860b57cec5SDimitry Andric       const SUnit *SU = &SUnits[i];
7870b57cec5SDimitry Andric       if (!CriticalPathSU ||
7880b57cec5SDimitry Andric           ((SU->getDepth() + SU->Latency) >
7890b57cec5SDimitry Andric            (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
7900b57cec5SDimitry Andric         CriticalPathSU = SU;
7910b57cec5SDimitry Andric       }
7920b57cec5SDimitry Andric     }
793*8bcb0991SDimitry Andric     assert(CriticalPathSU && "Failed to find SUnit critical path");
7940b57cec5SDimitry Andric     CriticalPathMI = CriticalPathSU->getInstr();
7950b57cec5SDimitry Andric   }
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric #ifndef NDEBUG
7980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
7990b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Available regs:");
8000b57cec5SDimitry Andric   for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
8010b57cec5SDimitry Andric     if (!State->IsLive(Reg))
8020b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
8030b57cec5SDimitry Andric   }
8040b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << '\n');
8050b57cec5SDimitry Andric #endif
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   BitVector RegAliases(TRI->getNumRegs());
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric   // Attempt to break anti-dependence edges. Walk the instructions
8100b57cec5SDimitry Andric   // from the bottom up, tracking information about liveness as we go
8110b57cec5SDimitry Andric   // to help determine which registers are available.
8120b57cec5SDimitry Andric   unsigned Broken = 0;
8130b57cec5SDimitry Andric   unsigned Count = InsertPosIndex - 1;
8140b57cec5SDimitry Andric   for (MachineBasicBlock::iterator I = End, E = Begin;
8150b57cec5SDimitry Andric        I != E; --Count) {
8160b57cec5SDimitry Andric     MachineInstr &MI = *--I;
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric     if (MI.isDebugInstr())
8190b57cec5SDimitry Andric       continue;
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Anti: ");
8220b57cec5SDimitry Andric     LLVM_DEBUG(MI.dump());
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric     std::set<unsigned> PassthruRegs;
8250b57cec5SDimitry Andric     GetPassthruRegs(MI, PassthruRegs);
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     // Process the defs in MI...
8280b57cec5SDimitry Andric     PrescanInstruction(MI, Count, PassthruRegs);
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric     // The dependence edges that represent anti- and output-
8310b57cec5SDimitry Andric     // dependencies that are candidates for breaking.
8320b57cec5SDimitry Andric     std::vector<const SDep *> Edges;
8330b57cec5SDimitry Andric     const SUnit *PathSU = MISUnitMap[&MI];
8340b57cec5SDimitry Andric     AntiDepEdges(PathSU, Edges);
8350b57cec5SDimitry Andric 
8360b57cec5SDimitry Andric     // If MI is not on the critical path, then we don't rename
8370b57cec5SDimitry Andric     // registers in the CriticalPathSet.
8380b57cec5SDimitry Andric     BitVector *ExcludeRegs = nullptr;
8390b57cec5SDimitry Andric     if (&MI == CriticalPathMI) {
8400b57cec5SDimitry Andric       CriticalPathSU = CriticalPathStep(CriticalPathSU);
8410b57cec5SDimitry Andric       CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
8420b57cec5SDimitry Andric     } else if (CriticalPathSet.any()) {
8430b57cec5SDimitry Andric       ExcludeRegs = &CriticalPathSet;
8440b57cec5SDimitry Andric     }
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric     // Ignore KILL instructions (they form a group in ScanInstruction
8470b57cec5SDimitry Andric     // but don't cause any anti-dependence breaking themselves)
8480b57cec5SDimitry Andric     if (!MI.isKill()) {
8490b57cec5SDimitry Andric       // Attempt to break each anti-dependency...
8500b57cec5SDimitry Andric       for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
8510b57cec5SDimitry Andric         const SDep *Edge = Edges[i];
8520b57cec5SDimitry Andric         SUnit *NextSU = Edge->getSUnit();
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric         if ((Edge->getKind() != SDep::Anti) &&
8550b57cec5SDimitry Andric             (Edge->getKind() != SDep::Output)) continue;
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric         unsigned AntiDepReg = Edge->getReg();
8580b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI));
8590b57cec5SDimitry Andric         assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric         if (!MRI.isAllocatable(AntiDepReg)) {
8620b57cec5SDimitry Andric           // Don't break anti-dependencies on non-allocatable registers.
8630b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " (non-allocatable)\n");
8640b57cec5SDimitry Andric           continue;
8650b57cec5SDimitry Andric         } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
8660b57cec5SDimitry Andric           // Don't break anti-dependencies for critical path registers
8670b57cec5SDimitry Andric           // if not on the critical path
8680b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " (not critical-path)\n");
8690b57cec5SDimitry Andric           continue;
8700b57cec5SDimitry Andric         } else if (PassthruRegs.count(AntiDepReg) != 0) {
8710b57cec5SDimitry Andric           // If the anti-dep register liveness "passes-thru", then
8720b57cec5SDimitry Andric           // don't try to change it. It will be changed along with
8730b57cec5SDimitry Andric           // the use if required to break an earlier antidep.
8740b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " (passthru)\n");
8750b57cec5SDimitry Andric           continue;
8760b57cec5SDimitry Andric         } else {
8770b57cec5SDimitry Andric           // No anti-dep breaking for implicit deps
8780b57cec5SDimitry Andric           MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg);
8790b57cec5SDimitry Andric           assert(AntiDepOp && "Can't find index for defined register operand");
8800b57cec5SDimitry Andric           if (!AntiDepOp || AntiDepOp->isImplicit()) {
8810b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << " (implicit)\n");
8820b57cec5SDimitry Andric             continue;
8830b57cec5SDimitry Andric           }
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric           // If the SUnit has other dependencies on the SUnit that
8860b57cec5SDimitry Andric           // it anti-depends on, don't bother breaking the
8870b57cec5SDimitry Andric           // anti-dependency since those edges would prevent such
8880b57cec5SDimitry Andric           // units from being scheduled past each other
8890b57cec5SDimitry Andric           // regardless.
8900b57cec5SDimitry Andric           //
8910b57cec5SDimitry Andric           // Also, if there are dependencies on other SUnits with the
8920b57cec5SDimitry Andric           // same register as the anti-dependency, don't attempt to
8930b57cec5SDimitry Andric           // break it.
8940b57cec5SDimitry Andric           for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
8950b57cec5SDimitry Andric                  PE = PathSU->Preds.end(); P != PE; ++P) {
8960b57cec5SDimitry Andric             if (P->getSUnit() == NextSU ?
8970b57cec5SDimitry Andric                 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
8980b57cec5SDimitry Andric                 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
8990b57cec5SDimitry Andric               AntiDepReg = 0;
9000b57cec5SDimitry Andric               break;
9010b57cec5SDimitry Andric             }
9020b57cec5SDimitry Andric           }
9030b57cec5SDimitry Andric           for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
9040b57cec5SDimitry Andric                  PE = PathSU->Preds.end(); P != PE; ++P) {
9050b57cec5SDimitry Andric             if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
9060b57cec5SDimitry Andric                 (P->getKind() != SDep::Output)) {
9070b57cec5SDimitry Andric               LLVM_DEBUG(dbgs() << " (real dependency)\n");
9080b57cec5SDimitry Andric               AntiDepReg = 0;
9090b57cec5SDimitry Andric               break;
9100b57cec5SDimitry Andric             } else if ((P->getSUnit() != NextSU) &&
9110b57cec5SDimitry Andric                        (P->getKind() == SDep::Data) &&
9120b57cec5SDimitry Andric                        (P->getReg() == AntiDepReg)) {
9130b57cec5SDimitry Andric               LLVM_DEBUG(dbgs() << " (other dependency)\n");
9140b57cec5SDimitry Andric               AntiDepReg = 0;
9150b57cec5SDimitry Andric               break;
9160b57cec5SDimitry Andric             }
9170b57cec5SDimitry Andric           }
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric           if (AntiDepReg == 0) continue;
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric           // If the definition of the anti-dependency register does not start
9220b57cec5SDimitry Andric           // a new live range, bail out. This can happen if the anti-dep
9230b57cec5SDimitry Andric           // register is a sub-register of another register whose live range
9240b57cec5SDimitry Andric           // spans over PathSU. In such case, PathSU defines only a part of
9250b57cec5SDimitry Andric           // the larger register.
9260b57cec5SDimitry Andric           RegAliases.reset();
9270b57cec5SDimitry Andric           for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI)
9280b57cec5SDimitry Andric             RegAliases.set(*AI);
9290b57cec5SDimitry Andric           for (SDep S : PathSU->Succs) {
9300b57cec5SDimitry Andric             SDep::Kind K = S.getKind();
9310b57cec5SDimitry Andric             if (K != SDep::Data && K != SDep::Output && K != SDep::Anti)
9320b57cec5SDimitry Andric               continue;
9330b57cec5SDimitry Andric             unsigned R = S.getReg();
9340b57cec5SDimitry Andric             if (!RegAliases[R])
9350b57cec5SDimitry Andric               continue;
9360b57cec5SDimitry Andric             if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
9370b57cec5SDimitry Andric               continue;
9380b57cec5SDimitry Andric             AntiDepReg = 0;
9390b57cec5SDimitry Andric             break;
9400b57cec5SDimitry Andric           }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric           if (AntiDepReg == 0) continue;
9430b57cec5SDimitry Andric         }
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric         assert(AntiDepReg != 0);
9460b57cec5SDimitry Andric         if (AntiDepReg == 0) continue;
9470b57cec5SDimitry Andric 
9480b57cec5SDimitry Andric         // Determine AntiDepReg's register group.
9490b57cec5SDimitry Andric         const unsigned GroupIndex = State->GetGroup(AntiDepReg);
9500b57cec5SDimitry Andric         if (GroupIndex == 0) {
9510b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " (zero group)\n");
9520b57cec5SDimitry Andric           continue;
9530b57cec5SDimitry Andric         }
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << '\n');
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric         // Look for a suitable register to use to break the anti-dependence.
9580b57cec5SDimitry Andric         std::map<unsigned, unsigned> RenameMap;
9590b57cec5SDimitry Andric         if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
9600b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
9610b57cec5SDimitry Andric                             << printReg(AntiDepReg, TRI) << ":");
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric           // Handle each group register...
9640b57cec5SDimitry Andric           for (std::map<unsigned, unsigned>::iterator
9650b57cec5SDimitry Andric                  S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
9660b57cec5SDimitry Andric             unsigned CurrReg = S->first;
9670b57cec5SDimitry Andric             unsigned NewReg = S->second;
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->"
9700b57cec5SDimitry Andric                               << printReg(NewReg, TRI) << "("
9710b57cec5SDimitry Andric                               << RegRefs.count(CurrReg) << " refs)");
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric             // Update the references to the old register CurrReg to
9740b57cec5SDimitry Andric             // refer to the new register NewReg.
9750b57cec5SDimitry Andric             for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
9760b57cec5SDimitry Andric               Q.second.Operand->setReg(NewReg);
9770b57cec5SDimitry Andric               // If the SU for the instruction being updated has debug
9780b57cec5SDimitry Andric               // information related to the anti-dependency register, make
9790b57cec5SDimitry Andric               // sure to update that as well.
9800b57cec5SDimitry Andric               const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
9810b57cec5SDimitry Andric               if (!SU) continue;
9820b57cec5SDimitry Andric               UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),
9830b57cec5SDimitry Andric                               AntiDepReg, NewReg);
9840b57cec5SDimitry Andric             }
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric             // We just went back in time and modified history; the
9870b57cec5SDimitry Andric             // liveness information for CurrReg is now inconsistent. Set
9880b57cec5SDimitry Andric             // the state as if it were dead.
9890b57cec5SDimitry Andric             State->UnionGroups(NewReg, 0);
9900b57cec5SDimitry Andric             RegRefs.erase(NewReg);
9910b57cec5SDimitry Andric             DefIndices[NewReg] = DefIndices[CurrReg];
9920b57cec5SDimitry Andric             KillIndices[NewReg] = KillIndices[CurrReg];
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric             State->UnionGroups(CurrReg, 0);
9950b57cec5SDimitry Andric             RegRefs.erase(CurrReg);
9960b57cec5SDimitry Andric             DefIndices[CurrReg] = KillIndices[CurrReg];
9970b57cec5SDimitry Andric             KillIndices[CurrReg] = ~0u;
9980b57cec5SDimitry Andric             assert(((KillIndices[CurrReg] == ~0u) !=
9990b57cec5SDimitry Andric                     (DefIndices[CurrReg] == ~0u)) &&
10000b57cec5SDimitry Andric                    "Kill and Def maps aren't consistent for AntiDepReg!");
10010b57cec5SDimitry Andric           }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric           ++Broken;
10040b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << '\n');
10050b57cec5SDimitry Andric         }
10060b57cec5SDimitry Andric       }
10070b57cec5SDimitry Andric     }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric     ScanInstruction(MI, Count);
10100b57cec5SDimitry Andric   }
10110b57cec5SDimitry Andric 
10120b57cec5SDimitry Andric   return Broken;
10130b57cec5SDimitry Andric }
1014