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