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