xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/CriticalAntiDepBreaker.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===- CriticalAntiDepBreaker.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 CriticalAntiDepBreaker class, which
100b57cec5SDimitry Andric // implements register anti-dependence breaking along a blocks
110b57cec5SDimitry Andric // critical path during post-RA scheduler.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "CriticalAntiDepBreaker.h"
160b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
170b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
180b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
310b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
330b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
340b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
350b57cec5SDimitry Andric #include <cassert>
360b57cec5SDimitry Andric #include <map>
370b57cec5SDimitry Andric #include <utility>
380b57cec5SDimitry Andric #include <vector>
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric using namespace llvm;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #define DEBUG_TYPE "post-RA-sched"
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,
450b57cec5SDimitry Andric                                                const RegisterClassInfo &RCI)
460b57cec5SDimitry Andric     : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
470b57cec5SDimitry Andric       TII(MF.getSubtarget().getInstrInfo()),
480b57cec5SDimitry Andric       TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
490b57cec5SDimitry Andric       Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),
500b57cec5SDimitry Andric       DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(), false) {}
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric CriticalAntiDepBreaker::~CriticalAntiDepBreaker() = default;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
550b57cec5SDimitry Andric   const unsigned BBSize = BB->size();
560b57cec5SDimitry Andric   for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
570b57cec5SDimitry Andric     // Clear out the register class data.
580b57cec5SDimitry Andric     Classes[i] = nullptr;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric     // Initialize the indices to indicate that no registers are live.
610b57cec5SDimitry Andric     KillIndices[i] = ~0u;
620b57cec5SDimitry Andric     DefIndices[i] = BBSize;
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // Clear "do not change" set.
660b57cec5SDimitry Andric   KeepRegs.reset();
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   bool IsReturnBlock = BB->isReturnBlock();
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // Examine the live-in regs of all successors.
710b57cec5SDimitry Andric   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
720b57cec5SDimitry Andric          SE = BB->succ_end(); SI != SE; ++SI)
730b57cec5SDimitry Andric     for (const auto &LI : (*SI)->liveins()) {
740b57cec5SDimitry Andric       for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
750b57cec5SDimitry Andric         unsigned Reg = *AI;
760b57cec5SDimitry Andric         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
770b57cec5SDimitry Andric         KillIndices[Reg] = BBSize;
780b57cec5SDimitry Andric         DefIndices[Reg] = ~0u;
790b57cec5SDimitry Andric       }
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // Mark live-out callee-saved registers. In a return block this is
830b57cec5SDimitry Andric   // all callee-saved registers. In non-return this is any
840b57cec5SDimitry Andric   // callee-saved register that is not saved in the prolog.
850b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
860b57cec5SDimitry Andric   BitVector Pristine = MFI.getPristineRegs(MF);
870b57cec5SDimitry Andric   for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
880b57cec5SDimitry Andric        ++I) {
890b57cec5SDimitry Andric     unsigned Reg = *I;
900b57cec5SDimitry Andric     if (!IsReturnBlock && !Pristine.test(Reg))
910b57cec5SDimitry Andric       continue;
920b57cec5SDimitry Andric     for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
930b57cec5SDimitry Andric       unsigned Reg = *AI;
940b57cec5SDimitry Andric       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
950b57cec5SDimitry Andric       KillIndices[Reg] = BBSize;
960b57cec5SDimitry Andric       DefIndices[Reg] = ~0u;
970b57cec5SDimitry Andric     }
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric void CriticalAntiDepBreaker::FinishBlock() {
1020b57cec5SDimitry Andric   RegRefs.clear();
1030b57cec5SDimitry Andric   KeepRegs.reset();
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
1070b57cec5SDimitry Andric                                      unsigned InsertPosIndex) {
1080b57cec5SDimitry Andric   // Kill instructions can define registers but are really nops, and there might
1090b57cec5SDimitry Andric   // be a real definition earlier that needs to be paired with uses dominated by
1100b57cec5SDimitry Andric   // this kill.
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // FIXME: It may be possible to remove the isKill() restriction once PR18663
1130b57cec5SDimitry Andric   // has been properly fixed. There can be value in processing kills as seen in
1140b57cec5SDimitry Andric   // the AggressiveAntiDepBreaker class.
1150b57cec5SDimitry Andric   if (MI.isDebugInstr() || MI.isKill())
1160b57cec5SDimitry Andric     return;
1170b57cec5SDimitry Andric   assert(Count < InsertPosIndex && "Instruction index out of expected range!");
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
1200b57cec5SDimitry Andric     if (KillIndices[Reg] != ~0u) {
1210b57cec5SDimitry Andric       // If Reg is currently live, then mark that it can't be renamed as
1220b57cec5SDimitry Andric       // we don't know the extent of its live-range anymore (now that it
1230b57cec5SDimitry Andric       // has been scheduled).
1240b57cec5SDimitry Andric       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
1250b57cec5SDimitry Andric       KillIndices[Reg] = Count;
1260b57cec5SDimitry Andric     } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
1270b57cec5SDimitry Andric       // Any register which was defined within the previous scheduling region
1280b57cec5SDimitry Andric       // may have been rescheduled and its lifetime may overlap with registers
1290b57cec5SDimitry Andric       // in ways not reflected in our current liveness state. For each such
1300b57cec5SDimitry Andric       // register, adjust the liveness state to be conservatively correct.
1310b57cec5SDimitry Andric       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       // Move the def index to the end of the previous region, to reflect
1340b57cec5SDimitry Andric       // that the def could theoretically have been scheduled at the end.
1350b57cec5SDimitry Andric       DefIndices[Reg] = InsertPosIndex;
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   PrescanInstruction(MI);
1400b57cec5SDimitry Andric   ScanInstruction(MI, Count);
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
1440b57cec5SDimitry Andric /// critical path.
1450b57cec5SDimitry Andric static const SDep *CriticalPathStep(const SUnit *SU) {
1460b57cec5SDimitry Andric   const SDep *Next = nullptr;
1470b57cec5SDimitry Andric   unsigned NextDepth = 0;
1480b57cec5SDimitry Andric   // Find the predecessor edge with the greatest depth.
1490b57cec5SDimitry Andric   for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
1500b57cec5SDimitry Andric        P != PE; ++P) {
1510b57cec5SDimitry Andric     const SUnit *PredSU = P->getSUnit();
1520b57cec5SDimitry Andric     unsigned PredLatency = P->getLatency();
1530b57cec5SDimitry Andric     unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
1540b57cec5SDimitry Andric     // In the case of a latency tie, prefer an anti-dependency edge over
1550b57cec5SDimitry Andric     // other types of edges.
1560b57cec5SDimitry Andric     if (NextDepth < PredTotalLatency ||
1570b57cec5SDimitry Andric         (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
1580b57cec5SDimitry Andric       NextDepth = PredTotalLatency;
1590b57cec5SDimitry Andric       Next = &*P;
1600b57cec5SDimitry Andric     }
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric   return Next;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
1660b57cec5SDimitry Andric   // It's not safe to change register allocation for source operands of
1670b57cec5SDimitry Andric   // instructions that have special allocation requirements. Also assume all
1680b57cec5SDimitry Andric   // registers used in a call must not be changed (ABI).
1690b57cec5SDimitry Andric   // FIXME: The issue with predicated instruction is more complex. We are being
1700b57cec5SDimitry Andric   // conservative here because the kill markers cannot be trusted after
1710b57cec5SDimitry Andric   // if-conversion:
1720b57cec5SDimitry Andric   // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
1730b57cec5SDimitry Andric   // ...
1740b57cec5SDimitry Andric   // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
1750b57cec5SDimitry Andric   // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
1760b57cec5SDimitry Andric   // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
1770b57cec5SDimitry Andric   //
1780b57cec5SDimitry Andric   // The first R6 kill is not really a kill since it's killed by a predicated
1790b57cec5SDimitry Andric   // instruction which may not be executed. The second R6 def may or may not
1800b57cec5SDimitry Andric   // re-define R6 so it's not safe to change it since the last R6 use cannot be
1810b57cec5SDimitry Andric   // changed.
1820b57cec5SDimitry Andric   bool Special =
1830b57cec5SDimitry Andric       MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   // Scan the register operands for this instruction and update
1860b57cec5SDimitry Andric   // Classes and RegRefs.
1870b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1880b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
1890b57cec5SDimitry Andric     if (!MO.isReg()) continue;
190*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
1910b57cec5SDimitry Andric     if (Reg == 0) continue;
1920b57cec5SDimitry Andric     const TargetRegisterClass *NewRC = nullptr;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric     if (i < MI.getDesc().getNumOperands())
1950b57cec5SDimitry Andric       NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric     // For now, only allow the register to be changed if its register
1980b57cec5SDimitry Andric     // class is consistent across all uses.
1990b57cec5SDimitry Andric     if (!Classes[Reg] && NewRC)
2000b57cec5SDimitry Andric       Classes[Reg] = NewRC;
2010b57cec5SDimitry Andric     else if (!NewRC || Classes[Reg] != NewRC)
2020b57cec5SDimitry Andric       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     // Now check for aliases.
2050b57cec5SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
2060b57cec5SDimitry Andric       // If an alias of the reg is used during the live range, give up.
2070b57cec5SDimitry Andric       // Note that this allows us to skip checking if AntiDepReg
2080b57cec5SDimitry Andric       // overlaps with any of the aliases, among other things.
2090b57cec5SDimitry Andric       unsigned AliasReg = *AI;
2100b57cec5SDimitry Andric       if (Classes[AliasReg]) {
2110b57cec5SDimitry Andric         Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
2120b57cec5SDimitry Andric         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
2130b57cec5SDimitry Andric       }
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric     // If we're still willing to consider this register, note the reference.
2170b57cec5SDimitry Andric     if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
2180b57cec5SDimitry Andric       RegRefs.insert(std::make_pair(Reg, &MO));
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric     // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
2210b57cec5SDimitry Andric     // it or any of its sub or super regs. We need to use KeepRegs to mark the
2220b57cec5SDimitry Andric     // reg because not all uses of the same reg within an instruction are
2230b57cec5SDimitry Andric     // necessarily tagged as tied.
2240b57cec5SDimitry Andric     // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
2250b57cec5SDimitry Andric     // def register but not the second (see PR20020 for details).
2260b57cec5SDimitry Andric     // FIXME: can this check be relaxed to account for undef uses
2270b57cec5SDimitry Andric     // of a register? In the above 'xor' example, the uses of %eax are undef, so
2280b57cec5SDimitry Andric     // earlier instructions could still replace %eax even though the 'xor'
2290b57cec5SDimitry Andric     // itself can't be changed.
2300b57cec5SDimitry Andric     if (MI.isRegTiedToUseOperand(i) &&
2310b57cec5SDimitry Andric         Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
2320b57cec5SDimitry Andric       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
2330b57cec5SDimitry Andric            SubRegs.isValid(); ++SubRegs) {
2340b57cec5SDimitry Andric         KeepRegs.set(*SubRegs);
2350b57cec5SDimitry Andric       }
2360b57cec5SDimitry Andric       for (MCSuperRegIterator SuperRegs(Reg, TRI);
2370b57cec5SDimitry Andric            SuperRegs.isValid(); ++SuperRegs) {
2380b57cec5SDimitry Andric         KeepRegs.set(*SuperRegs);
2390b57cec5SDimitry Andric       }
2400b57cec5SDimitry Andric     }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     if (MO.isUse() && Special) {
2430b57cec5SDimitry Andric       if (!KeepRegs.test(Reg)) {
2440b57cec5SDimitry Andric         for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
2450b57cec5SDimitry Andric              SubRegs.isValid(); ++SubRegs)
2460b57cec5SDimitry Andric           KeepRegs.set(*SubRegs);
2470b57cec5SDimitry Andric       }
2480b57cec5SDimitry Andric     }
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
2530b57cec5SDimitry Andric   // Update liveness.
2540b57cec5SDimitry Andric   // Proceeding upwards, registers that are defed but not used in this
2550b57cec5SDimitry Andric   // instruction are now dead.
2560b57cec5SDimitry Andric   assert(!MI.isKill() && "Attempting to scan a kill instruction");
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   if (!TII->isPredicated(MI)) {
2590b57cec5SDimitry Andric     // Predicated defs are modeled as read + write, i.e. similar to two
2600b57cec5SDimitry Andric     // address updates.
2610b57cec5SDimitry Andric     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2620b57cec5SDimitry Andric       MachineOperand &MO = MI.getOperand(i);
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric       if (MO.isRegMask())
2650b57cec5SDimitry Andric         for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
2660b57cec5SDimitry Andric           if (MO.clobbersPhysReg(i)) {
2670b57cec5SDimitry Andric             DefIndices[i] = Count;
2680b57cec5SDimitry Andric             KillIndices[i] = ~0u;
2690b57cec5SDimitry Andric             KeepRegs.reset(i);
2700b57cec5SDimitry Andric             Classes[i] = nullptr;
2710b57cec5SDimitry Andric             RegRefs.erase(i);
2720b57cec5SDimitry Andric           }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric       if (!MO.isReg()) continue;
275*8bcb0991SDimitry Andric       Register Reg = MO.getReg();
2760b57cec5SDimitry Andric       if (Reg == 0) continue;
2770b57cec5SDimitry Andric       if (!MO.isDef()) continue;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric       // Ignore two-addr defs.
2800b57cec5SDimitry Andric       if (MI.isRegTiedToUseOperand(i))
2810b57cec5SDimitry Andric         continue;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric       // If we've already marked this reg as unchangeable, don't remove
2840b57cec5SDimitry Andric       // it or any of its subregs from KeepRegs.
2850b57cec5SDimitry Andric       bool Keep = KeepRegs.test(Reg);
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric       // For the reg itself and all subregs: update the def to current;
2880b57cec5SDimitry Andric       // reset the kill state, any restrictions, and references.
2890b57cec5SDimitry Andric       for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) {
2900b57cec5SDimitry Andric         unsigned SubregReg = *SRI;
2910b57cec5SDimitry Andric         DefIndices[SubregReg] = Count;
2920b57cec5SDimitry Andric         KillIndices[SubregReg] = ~0u;
2930b57cec5SDimitry Andric         Classes[SubregReg] = nullptr;
2940b57cec5SDimitry Andric         RegRefs.erase(SubregReg);
2950b57cec5SDimitry Andric         if (!Keep)
2960b57cec5SDimitry Andric           KeepRegs.reset(SubregReg);
2970b57cec5SDimitry Andric       }
2980b57cec5SDimitry Andric       // Conservatively mark super-registers as unusable.
2990b57cec5SDimitry Andric       for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
3000b57cec5SDimitry Andric         Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
3010b57cec5SDimitry Andric     }
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3040b57cec5SDimitry Andric     MachineOperand &MO = MI.getOperand(i);
3050b57cec5SDimitry Andric     if (!MO.isReg()) continue;
306*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
3070b57cec5SDimitry Andric     if (Reg == 0) continue;
3080b57cec5SDimitry Andric     if (!MO.isUse()) continue;
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric     const TargetRegisterClass *NewRC = nullptr;
3110b57cec5SDimitry Andric     if (i < MI.getDesc().getNumOperands())
3120b57cec5SDimitry Andric       NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric     // For now, only allow the register to be changed if its register
3150b57cec5SDimitry Andric     // class is consistent across all uses.
3160b57cec5SDimitry Andric     if (!Classes[Reg] && NewRC)
3170b57cec5SDimitry Andric       Classes[Reg] = NewRC;
3180b57cec5SDimitry Andric     else if (!NewRC || Classes[Reg] != NewRC)
3190b57cec5SDimitry Andric       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     RegRefs.insert(std::make_pair(Reg, &MO));
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric     // It wasn't previously live but now it is, this is a kill.
3240b57cec5SDimitry Andric     // Repeat for all aliases.
3250b57cec5SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
3260b57cec5SDimitry Andric       unsigned AliasReg = *AI;
3270b57cec5SDimitry Andric       if (KillIndices[AliasReg] == ~0u) {
3280b57cec5SDimitry Andric         KillIndices[AliasReg] = Count;
3290b57cec5SDimitry Andric         DefIndices[AliasReg] = ~0u;
3300b57cec5SDimitry Andric       }
3310b57cec5SDimitry Andric     }
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric // Check all machine operands that reference the antidependent register and must
3360b57cec5SDimitry Andric // be replaced by NewReg. Return true if any of their parent instructions may
3370b57cec5SDimitry Andric // clobber the new register.
3380b57cec5SDimitry Andric //
3390b57cec5SDimitry Andric // Note: AntiDepReg may be referenced by a two-address instruction such that
3400b57cec5SDimitry Andric // it's use operand is tied to a def operand. We guard against the case in which
3410b57cec5SDimitry Andric // the two-address instruction also defines NewReg, as may happen with
3420b57cec5SDimitry Andric // pre/postincrement loads. In this case, both the use and def operands are in
3430b57cec5SDimitry Andric // RegRefs because the def is inserted by PrescanInstruction and not erased
3440b57cec5SDimitry Andric // during ScanInstruction. So checking for an instruction with definitions of
3450b57cec5SDimitry Andric // both NewReg and AntiDepReg covers it.
3460b57cec5SDimitry Andric bool
3470b57cec5SDimitry Andric CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
3480b57cec5SDimitry Andric                                                 RegRefIter RegRefEnd,
3490b57cec5SDimitry Andric                                                 unsigned NewReg) {
3500b57cec5SDimitry Andric   for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
3510b57cec5SDimitry Andric     MachineOperand *RefOper = I->second;
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric     // Don't allow the instruction defining AntiDepReg to earlyclobber its
3540b57cec5SDimitry Andric     // operands, in case they may be assigned to NewReg. In this case antidep
3550b57cec5SDimitry Andric     // breaking must fail, but it's too rare to bother optimizing.
3560b57cec5SDimitry Andric     if (RefOper->isDef() && RefOper->isEarlyClobber())
3570b57cec5SDimitry Andric       return true;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     // Handle cases in which this instruction defines NewReg.
3600b57cec5SDimitry Andric     MachineInstr *MI = RefOper->getParent();
3610b57cec5SDimitry Andric     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
3620b57cec5SDimitry Andric       const MachineOperand &CheckOper = MI->getOperand(i);
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric       if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
3650b57cec5SDimitry Andric         return true;
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric       if (!CheckOper.isReg() || !CheckOper.isDef() ||
3680b57cec5SDimitry Andric           CheckOper.getReg() != NewReg)
3690b57cec5SDimitry Andric         continue;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric       // Don't allow the instruction to define NewReg and AntiDepReg.
3720b57cec5SDimitry Andric       // When AntiDepReg is renamed it will be an illegal op.
3730b57cec5SDimitry Andric       if (RefOper->isDef())
3740b57cec5SDimitry Andric         return true;
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric       // Don't allow an instruction using AntiDepReg to be earlyclobbered by
3770b57cec5SDimitry Andric       // NewReg.
3780b57cec5SDimitry Andric       if (CheckOper.isEarlyClobber())
3790b57cec5SDimitry Andric         return true;
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric       // Don't allow inline asm to define NewReg at all. Who knows what it's
3820b57cec5SDimitry Andric       // doing with it.
3830b57cec5SDimitry Andric       if (MI->isInlineAsm())
3840b57cec5SDimitry Andric         return true;
3850b57cec5SDimitry Andric     }
3860b57cec5SDimitry Andric   }
3870b57cec5SDimitry Andric   return false;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric unsigned CriticalAntiDepBreaker::
3910b57cec5SDimitry Andric findSuitableFreeRegister(RegRefIter RegRefBegin,
3920b57cec5SDimitry Andric                          RegRefIter RegRefEnd,
3930b57cec5SDimitry Andric                          unsigned AntiDepReg,
3940b57cec5SDimitry Andric                          unsigned LastNewReg,
3950b57cec5SDimitry Andric                          const TargetRegisterClass *RC,
3960b57cec5SDimitry Andric                          SmallVectorImpl<unsigned> &Forbid) {
3970b57cec5SDimitry Andric   ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
3980b57cec5SDimitry Andric   for (unsigned i = 0; i != Order.size(); ++i) {
3990b57cec5SDimitry Andric     unsigned NewReg = Order[i];
4000b57cec5SDimitry Andric     // Don't replace a register with itself.
4010b57cec5SDimitry Andric     if (NewReg == AntiDepReg) continue;
4020b57cec5SDimitry Andric     // Don't replace a register with one that was recently used to repair
4030b57cec5SDimitry Andric     // an anti-dependence with this AntiDepReg, because that would
4040b57cec5SDimitry Andric     // re-introduce that anti-dependence.
4050b57cec5SDimitry Andric     if (NewReg == LastNewReg) continue;
4060b57cec5SDimitry Andric     // If any instructions that define AntiDepReg also define the NewReg, it's
4070b57cec5SDimitry Andric     // not suitable.  For example, Instruction with multiple definitions can
4080b57cec5SDimitry Andric     // result in this condition.
4090b57cec5SDimitry Andric     if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
4100b57cec5SDimitry Andric     // If NewReg is dead and NewReg's most recent def is not before
4110b57cec5SDimitry Andric     // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
4120b57cec5SDimitry Andric     assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
4130b57cec5SDimitry Andric            && "Kill and Def maps aren't consistent for AntiDepReg!");
4140b57cec5SDimitry Andric     assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
4150b57cec5SDimitry Andric            && "Kill and Def maps aren't consistent for NewReg!");
4160b57cec5SDimitry Andric     if (KillIndices[NewReg] != ~0u ||
4170b57cec5SDimitry Andric         Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
4180b57cec5SDimitry Andric         KillIndices[AntiDepReg] > DefIndices[NewReg])
4190b57cec5SDimitry Andric       continue;
4200b57cec5SDimitry Andric     // If NewReg overlaps any of the forbidden registers, we can't use it.
4210b57cec5SDimitry Andric     bool Forbidden = false;
4220b57cec5SDimitry Andric     for (SmallVectorImpl<unsigned>::iterator it = Forbid.begin(),
4230b57cec5SDimitry Andric            ite = Forbid.end(); it != ite; ++it)
4240b57cec5SDimitry Andric       if (TRI->regsOverlap(NewReg, *it)) {
4250b57cec5SDimitry Andric         Forbidden = true;
4260b57cec5SDimitry Andric         break;
4270b57cec5SDimitry Andric       }
4280b57cec5SDimitry Andric     if (Forbidden) continue;
4290b57cec5SDimitry Andric     return NewReg;
4300b57cec5SDimitry Andric   }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   // No registers are free and available!
4330b57cec5SDimitry Andric   return 0;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric unsigned CriticalAntiDepBreaker::
4370b57cec5SDimitry Andric BreakAntiDependencies(const std::vector<SUnit> &SUnits,
4380b57cec5SDimitry Andric                       MachineBasicBlock::iterator Begin,
4390b57cec5SDimitry Andric                       MachineBasicBlock::iterator End,
4400b57cec5SDimitry Andric                       unsigned InsertPosIndex,
4410b57cec5SDimitry Andric                       DbgValueVector &DbgValues) {
4420b57cec5SDimitry Andric   // The code below assumes that there is at least one instruction,
4430b57cec5SDimitry Andric   // so just duck out immediately if the block is empty.
4440b57cec5SDimitry Andric   if (SUnits.empty()) return 0;
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric   // Keep a map of the MachineInstr*'s back to the SUnit representing them.
4470b57cec5SDimitry Andric   // This is used for updating debug information.
4480b57cec5SDimitry Andric   //
4490b57cec5SDimitry Andric   // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
4500b57cec5SDimitry Andric   DenseMap<MachineInstr *, const SUnit *> MISUnitMap;
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   // Find the node at the bottom of the critical path.
4530b57cec5SDimitry Andric   const SUnit *Max = nullptr;
4540b57cec5SDimitry Andric   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
4550b57cec5SDimitry Andric     const SUnit *SU = &SUnits[i];
4560b57cec5SDimitry Andric     MISUnitMap[SU->getInstr()] = SU;
4570b57cec5SDimitry Andric     if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
4580b57cec5SDimitry Andric       Max = SU;
4590b57cec5SDimitry Andric   }
460*8bcb0991SDimitry Andric   assert(Max && "Failed to find bottom of the critical path");
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric #ifndef NDEBUG
4630b57cec5SDimitry Andric   {
4640b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Critical path has total latency "
4650b57cec5SDimitry Andric                       << (Max->getDepth() + Max->Latency) << "\n");
4660b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Available regs:");
4670b57cec5SDimitry Andric     for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
4680b57cec5SDimitry Andric       if (KillIndices[Reg] == ~0u)
4690b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
4700b57cec5SDimitry Andric     }
4710b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << '\n');
4720b57cec5SDimitry Andric   }
4730b57cec5SDimitry Andric #endif
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   // Track progress along the critical path through the SUnit graph as we walk
4760b57cec5SDimitry Andric   // the instructions.
4770b57cec5SDimitry Andric   const SUnit *CriticalPathSU = Max;
4780b57cec5SDimitry Andric   MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   // Consider this pattern:
4810b57cec5SDimitry Andric   //   A = ...
4820b57cec5SDimitry Andric   //   ... = A
4830b57cec5SDimitry Andric   //   A = ...
4840b57cec5SDimitry Andric   //   ... = A
4850b57cec5SDimitry Andric   //   A = ...
4860b57cec5SDimitry Andric   //   ... = A
4870b57cec5SDimitry Andric   //   A = ...
4880b57cec5SDimitry Andric   //   ... = A
4890b57cec5SDimitry Andric   // There are three anti-dependencies here, and without special care,
4900b57cec5SDimitry Andric   // we'd break all of them using the same register:
4910b57cec5SDimitry Andric   //   A = ...
4920b57cec5SDimitry Andric   //   ... = A
4930b57cec5SDimitry Andric   //   B = ...
4940b57cec5SDimitry Andric   //   ... = B
4950b57cec5SDimitry Andric   //   B = ...
4960b57cec5SDimitry Andric   //   ... = B
4970b57cec5SDimitry Andric   //   B = ...
4980b57cec5SDimitry Andric   //   ... = B
4990b57cec5SDimitry Andric   // because at each anti-dependence, B is the first register that
5000b57cec5SDimitry Andric   // isn't A which is free.  This re-introduces anti-dependencies
5010b57cec5SDimitry Andric   // at all but one of the original anti-dependencies that we were
5020b57cec5SDimitry Andric   // trying to break.  To avoid this, keep track of the most recent
5030b57cec5SDimitry Andric   // register that each register was replaced with, avoid
5040b57cec5SDimitry Andric   // using it to repair an anti-dependence on the same register.
5050b57cec5SDimitry Andric   // This lets us produce this:
5060b57cec5SDimitry Andric   //   A = ...
5070b57cec5SDimitry Andric   //   ... = A
5080b57cec5SDimitry Andric   //   B = ...
5090b57cec5SDimitry Andric   //   ... = B
5100b57cec5SDimitry Andric   //   C = ...
5110b57cec5SDimitry Andric   //   ... = C
5120b57cec5SDimitry Andric   //   B = ...
5130b57cec5SDimitry Andric   //   ... = B
5140b57cec5SDimitry Andric   // This still has an anti-dependence on B, but at least it isn't on the
5150b57cec5SDimitry Andric   // original critical path.
5160b57cec5SDimitry Andric   //
5170b57cec5SDimitry Andric   // TODO: If we tracked more than one register here, we could potentially
5180b57cec5SDimitry Andric   // fix that remaining critical edge too. This is a little more involved,
5190b57cec5SDimitry Andric   // because unlike the most recent register, less recent registers should
5200b57cec5SDimitry Andric   // still be considered, though only if no other registers are available.
5210b57cec5SDimitry Andric   std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric   // Attempt to break anti-dependence edges on the critical path. Walk the
5240b57cec5SDimitry Andric   // instructions from the bottom up, tracking information about liveness
5250b57cec5SDimitry Andric   // as we go to help determine which registers are available.
5260b57cec5SDimitry Andric   unsigned Broken = 0;
5270b57cec5SDimitry Andric   unsigned Count = InsertPosIndex - 1;
5280b57cec5SDimitry Andric   for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
5290b57cec5SDimitry Andric     MachineInstr &MI = *--I;
5300b57cec5SDimitry Andric     // Kill instructions can define registers but are really nops, and there
5310b57cec5SDimitry Andric     // might be a real definition earlier that needs to be paired with uses
5320b57cec5SDimitry Andric     // dominated by this kill.
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric     // FIXME: It may be possible to remove the isKill() restriction once PR18663
5350b57cec5SDimitry Andric     // has been properly fixed. There can be value in processing kills as seen
5360b57cec5SDimitry Andric     // in the AggressiveAntiDepBreaker class.
5370b57cec5SDimitry Andric     if (MI.isDebugInstr() || MI.isKill())
5380b57cec5SDimitry Andric       continue;
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric     // Check if this instruction has a dependence on the critical path that
5410b57cec5SDimitry Andric     // is an anti-dependence that we may be able to break. If it is, set
5420b57cec5SDimitry Andric     // AntiDepReg to the non-zero register associated with the anti-dependence.
5430b57cec5SDimitry Andric     //
5440b57cec5SDimitry Andric     // We limit our attention to the critical path as a heuristic to avoid
5450b57cec5SDimitry Andric     // breaking anti-dependence edges that aren't going to significantly
5460b57cec5SDimitry Andric     // impact the overall schedule. There are a limited number of registers
5470b57cec5SDimitry Andric     // and we want to save them for the important edges.
5480b57cec5SDimitry Andric     //
5490b57cec5SDimitry Andric     // TODO: Instructions with multiple defs could have multiple
5500b57cec5SDimitry Andric     // anti-dependencies. The current code here only knows how to break one
5510b57cec5SDimitry Andric     // edge per instruction. Note that we'd have to be able to break all of
5520b57cec5SDimitry Andric     // the anti-dependencies in an instruction in order to be effective.
5530b57cec5SDimitry Andric     unsigned AntiDepReg = 0;
5540b57cec5SDimitry Andric     if (&MI == CriticalPathMI) {
5550b57cec5SDimitry Andric       if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
5560b57cec5SDimitry Andric         const SUnit *NextSU = Edge->getSUnit();
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric         // Only consider anti-dependence edges.
5590b57cec5SDimitry Andric         if (Edge->getKind() == SDep::Anti) {
5600b57cec5SDimitry Andric           AntiDepReg = Edge->getReg();
5610b57cec5SDimitry Andric           assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
5620b57cec5SDimitry Andric           if (!MRI.isAllocatable(AntiDepReg))
5630b57cec5SDimitry Andric             // Don't break anti-dependencies on non-allocatable registers.
5640b57cec5SDimitry Andric             AntiDepReg = 0;
5650b57cec5SDimitry Andric           else if (KeepRegs.test(AntiDepReg))
5660b57cec5SDimitry Andric             // Don't break anti-dependencies if a use down below requires
5670b57cec5SDimitry Andric             // this exact register.
5680b57cec5SDimitry Andric             AntiDepReg = 0;
5690b57cec5SDimitry Andric           else {
5700b57cec5SDimitry Andric             // If the SUnit has other dependencies on the SUnit that it
5710b57cec5SDimitry Andric             // anti-depends on, don't bother breaking the anti-dependency
5720b57cec5SDimitry Andric             // since those edges would prevent such units from being
5730b57cec5SDimitry Andric             // scheduled past each other regardless.
5740b57cec5SDimitry Andric             //
5750b57cec5SDimitry Andric             // Also, if there are dependencies on other SUnits with the
5760b57cec5SDimitry Andric             // same register as the anti-dependency, don't attempt to
5770b57cec5SDimitry Andric             // break it.
5780b57cec5SDimitry Andric             for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
5790b57cec5SDimitry Andric                  PE = CriticalPathSU->Preds.end(); P != PE; ++P)
5800b57cec5SDimitry Andric               if (P->getSUnit() == NextSU ?
5810b57cec5SDimitry Andric                     (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
5820b57cec5SDimitry Andric                     (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
5830b57cec5SDimitry Andric                 AntiDepReg = 0;
5840b57cec5SDimitry Andric                 break;
5850b57cec5SDimitry Andric               }
5860b57cec5SDimitry Andric           }
5870b57cec5SDimitry Andric         }
5880b57cec5SDimitry Andric         CriticalPathSU = NextSU;
5890b57cec5SDimitry Andric         CriticalPathMI = CriticalPathSU->getInstr();
5900b57cec5SDimitry Andric       } else {
5910b57cec5SDimitry Andric         // We've reached the end of the critical path.
5920b57cec5SDimitry Andric         CriticalPathSU = nullptr;
5930b57cec5SDimitry Andric         CriticalPathMI = nullptr;
5940b57cec5SDimitry Andric       }
5950b57cec5SDimitry Andric     }
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric     PrescanInstruction(MI);
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric     SmallVector<unsigned, 2> ForbidRegs;
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric     // If MI's defs have a special allocation requirement, don't allow
6020b57cec5SDimitry Andric     // any def registers to be changed. Also assume all registers
6030b57cec5SDimitry Andric     // defined in a call must not be changed (ABI).
6040b57cec5SDimitry Andric     if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
6050b57cec5SDimitry Andric       // If this instruction's defs have special allocation requirement, don't
6060b57cec5SDimitry Andric       // break this anti-dependency.
6070b57cec5SDimitry Andric       AntiDepReg = 0;
6080b57cec5SDimitry Andric     else if (AntiDepReg) {
6090b57cec5SDimitry Andric       // If this instruction has a use of AntiDepReg, breaking it
6100b57cec5SDimitry Andric       // is invalid.  If the instruction defines other registers,
6110b57cec5SDimitry Andric       // save a list of them so that we don't pick a new register
6120b57cec5SDimitry Andric       // that overlaps any of them.
6130b57cec5SDimitry Andric       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
6140b57cec5SDimitry Andric         MachineOperand &MO = MI.getOperand(i);
6150b57cec5SDimitry Andric         if (!MO.isReg()) continue;
616*8bcb0991SDimitry Andric         Register Reg = MO.getReg();
6170b57cec5SDimitry Andric         if (Reg == 0) continue;
6180b57cec5SDimitry Andric         if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
6190b57cec5SDimitry Andric           AntiDepReg = 0;
6200b57cec5SDimitry Andric           break;
6210b57cec5SDimitry Andric         }
6220b57cec5SDimitry Andric         if (MO.isDef() && Reg != AntiDepReg)
6230b57cec5SDimitry Andric           ForbidRegs.push_back(Reg);
6240b57cec5SDimitry Andric       }
6250b57cec5SDimitry Andric     }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric     // Determine AntiDepReg's register class, if it is live and is
6280b57cec5SDimitry Andric     // consistently used within a single class.
6290b57cec5SDimitry Andric     const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
6300b57cec5SDimitry Andric                                                     : nullptr;
6310b57cec5SDimitry Andric     assert((AntiDepReg == 0 || RC != nullptr) &&
6320b57cec5SDimitry Andric            "Register should be live if it's causing an anti-dependence!");
6330b57cec5SDimitry Andric     if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
6340b57cec5SDimitry Andric       AntiDepReg = 0;
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric     // Look for a suitable register to use to break the anti-dependence.
6370b57cec5SDimitry Andric     //
6380b57cec5SDimitry Andric     // TODO: Instead of picking the first free register, consider which might
6390b57cec5SDimitry Andric     // be the best.
6400b57cec5SDimitry Andric     if (AntiDepReg != 0) {
6410b57cec5SDimitry Andric       std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
6420b57cec5SDimitry Andric                 std::multimap<unsigned, MachineOperand *>::iterator>
6430b57cec5SDimitry Andric         Range = RegRefs.equal_range(AntiDepReg);
6440b57cec5SDimitry Andric       if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
6450b57cec5SDimitry Andric                                                      AntiDepReg,
6460b57cec5SDimitry Andric                                                      LastNewReg[AntiDepReg],
6470b57cec5SDimitry Andric                                                      RC, ForbidRegs)) {
6480b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Breaking anti-dependence edge on "
6490b57cec5SDimitry Andric                           << printReg(AntiDepReg, TRI) << " with "
6500b57cec5SDimitry Andric                           << RegRefs.count(AntiDepReg) << " references"
6510b57cec5SDimitry Andric                           << " using " << printReg(NewReg, TRI) << "!\n");
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric         // Update the references to the old register to refer to the new
6540b57cec5SDimitry Andric         // register.
6550b57cec5SDimitry Andric         for (std::multimap<unsigned, MachineOperand *>::iterator
6560b57cec5SDimitry Andric              Q = Range.first, QE = Range.second; Q != QE; ++Q) {
6570b57cec5SDimitry Andric           Q->second->setReg(NewReg);
6580b57cec5SDimitry Andric           // If the SU for the instruction being updated has debug information
6590b57cec5SDimitry Andric           // related to the anti-dependency register, make sure to update that
6600b57cec5SDimitry Andric           // as well.
6610b57cec5SDimitry Andric           const SUnit *SU = MISUnitMap[Q->second->getParent()];
6620b57cec5SDimitry Andric           if (!SU) continue;
6630b57cec5SDimitry Andric           UpdateDbgValues(DbgValues, Q->second->getParent(),
6640b57cec5SDimitry Andric                           AntiDepReg, NewReg);
6650b57cec5SDimitry Andric         }
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric         // We just went back in time and modified history; the
6680b57cec5SDimitry Andric         // liveness information for the anti-dependence reg is now
6690b57cec5SDimitry Andric         // inconsistent. Set the state as if it were dead.
6700b57cec5SDimitry Andric         Classes[NewReg] = Classes[AntiDepReg];
6710b57cec5SDimitry Andric         DefIndices[NewReg] = DefIndices[AntiDepReg];
6720b57cec5SDimitry Andric         KillIndices[NewReg] = KillIndices[AntiDepReg];
6730b57cec5SDimitry Andric         assert(((KillIndices[NewReg] == ~0u) !=
6740b57cec5SDimitry Andric                 (DefIndices[NewReg] == ~0u)) &&
6750b57cec5SDimitry Andric              "Kill and Def maps aren't consistent for NewReg!");
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric         Classes[AntiDepReg] = nullptr;
6780b57cec5SDimitry Andric         DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
6790b57cec5SDimitry Andric         KillIndices[AntiDepReg] = ~0u;
6800b57cec5SDimitry Andric         assert(((KillIndices[AntiDepReg] == ~0u) !=
6810b57cec5SDimitry Andric                 (DefIndices[AntiDepReg] == ~0u)) &&
6820b57cec5SDimitry Andric              "Kill and Def maps aren't consistent for AntiDepReg!");
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric         RegRefs.erase(AntiDepReg);
6850b57cec5SDimitry Andric         LastNewReg[AntiDepReg] = NewReg;
6860b57cec5SDimitry Andric         ++Broken;
6870b57cec5SDimitry Andric       }
6880b57cec5SDimitry Andric     }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric     ScanInstruction(MI, Count);
6910b57cec5SDimitry Andric   }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   return Broken;
6940b57cec5SDimitry Andric }
695