14f81cdd8SEugene Zelenko //===- CriticalAntiDepBreaker.cpp - Anti-dep breaker ----------------------===//
28370485dSDavid Goodwin //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68370485dSDavid Goodwin //
78370485dSDavid Goodwin //===----------------------------------------------------------------------===//
88370485dSDavid Goodwin //
98370485dSDavid Goodwin // This file implements the CriticalAntiDepBreaker class, which
108370485dSDavid Goodwin // implements register anti-dependence breaking along a blocks
118370485dSDavid Goodwin // critical path during post-RA scheduler.
128370485dSDavid Goodwin //
138370485dSDavid Goodwin //===----------------------------------------------------------------------===//
148370485dSDavid Goodwin
158370485dSDavid Goodwin #include "CriticalAntiDepBreaker.h"
164f81cdd8SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
174f81cdd8SEugene Zelenko #include "llvm/ADT/DenseMap.h"
184f81cdd8SEugene Zelenko #include "llvm/ADT/SmallVector.h"
198370485dSDavid Goodwin #include "llvm/CodeGen/MachineBasicBlock.h"
208370485dSDavid Goodwin #include "llvm/CodeGen/MachineFrameInfo.h"
214f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineFunction.h"
224f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineInstr.h"
234f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineOperand.h"
244f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineRegisterInfo.h"
254f81cdd8SEugene Zelenko #include "llvm/CodeGen/RegisterClassInfo.h"
264f81cdd8SEugene Zelenko #include "llvm/CodeGen/ScheduleDAG.h"
273f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
28b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetRegisterInfo.h"
29b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
304f81cdd8SEugene Zelenko #include "llvm/MC/MCInstrDesc.h"
314f81cdd8SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
328370485dSDavid Goodwin #include "llvm/Support/Debug.h"
338370485dSDavid Goodwin #include "llvm/Support/raw_ostream.h"
344f81cdd8SEugene Zelenko #include <cassert>
354f81cdd8SEugene Zelenko #include <utility>
368370485dSDavid Goodwin
378370485dSDavid Goodwin using namespace llvm;
388370485dSDavid Goodwin
391b9dde08SChandler Carruth #define DEBUG_TYPE "post-RA-sched"
401b9dde08SChandler Carruth
CriticalAntiDepBreaker(MachineFunction & MFi,const RegisterClassInfo & RCI)41d913448bSEric Christopher CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,
42d913448bSEric Christopher const RegisterClassInfo &RCI)
43*b932bdf5SKazu Hirata : MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),
44fc6de428SEric Christopher TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
45fc6de428SEric Christopher Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),
46fc6de428SEric Christopher DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(), false) {}
478370485dSDavid Goodwin
484f81cdd8SEugene Zelenko CriticalAntiDepBreaker::~CriticalAntiDepBreaker() = default;
498370485dSDavid Goodwin
StartBlock(MachineBasicBlock * BB)508370485dSDavid Goodwin void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
5151a9c0a1SBill Wendling const unsigned BBSize = BB->size();
5251a9c0a1SBill Wendling for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
538370485dSDavid Goodwin // Clear out the register class data.
54c0196b1bSCraig Topper Classes[i] = nullptr;
558370485dSDavid Goodwin
568370485dSDavid Goodwin // Initialize the indices to indicate that no registers are live.
57a45fe676SDavid Goodwin KillIndices[i] = ~0u;
58a45fe676SDavid Goodwin DefIndices[i] = BBSize;
59a45fe676SDavid Goodwin }
608370485dSDavid Goodwin
618370485dSDavid Goodwin // Clear "do not change" set.
625d1bca80SBenjamin Kramer KeepRegs.reset();
638370485dSDavid Goodwin
64c2d4befbSMatthias Braun bool IsReturnBlock = BB->isReturnBlock();
658370485dSDavid Goodwin
66c338679cSJakob Stoklund Olesen // Examine the live-in regs of all successors.
67905cf88dSKazu Hirata for (const MachineBasicBlock *Succ : BB->successors())
68905cf88dSKazu Hirata for (const auto &LI : Succ->liveins()) {
69d9da1627SMatthias Braun for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
7092a00839SJakob Stoklund Olesen unsigned Reg = *AI;
718370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
728e5af375SBenjamin Kramer KillIndices[Reg] = BBSize;
738370485dSDavid Goodwin DefIndices[Reg] = ~0u;
748370485dSDavid Goodwin }
758370485dSDavid Goodwin }
768370485dSDavid Goodwin
778370485dSDavid Goodwin // Mark live-out callee-saved registers. In a return block this is
788370485dSDavid Goodwin // all callee-saved registers. In non-return this is any
798370485dSDavid Goodwin // callee-saved register that is not saved in the prolog.
80941a705bSMatthias Braun const MachineFrameInfo &MFI = MF.getFrameInfo();
81941a705bSMatthias Braun BitVector Pristine = MFI.getPristineRegs(MF);
82fe34c5e4SOren Ben Simhon for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
83fe34c5e4SOren Ben Simhon ++I) {
84b9c56d12SEric Christopher unsigned Reg = *I;
850bd0aa8fSTim Shen if (!IsReturnBlock && !Pristine.test(Reg))
86b9c56d12SEric Christopher continue;
8792a00839SJakob Stoklund Olesen for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
8892a00839SJakob Stoklund Olesen unsigned Reg = *AI;
898370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
908e5af375SBenjamin Kramer KillIndices[Reg] = BBSize;
918370485dSDavid Goodwin DefIndices[Reg] = ~0u;
928370485dSDavid Goodwin }
938370485dSDavid Goodwin }
948370485dSDavid Goodwin }
958370485dSDavid Goodwin
FinishBlock()968370485dSDavid Goodwin void CriticalAntiDepBreaker::FinishBlock() {
978370485dSDavid Goodwin RegRefs.clear();
985d1bca80SBenjamin Kramer KeepRegs.reset();
998370485dSDavid Goodwin }
1008370485dSDavid Goodwin
Observe(MachineInstr & MI,unsigned Count,unsigned InsertPosIndex)1015e6e8c7aSDuncan P. N. Exon Smith void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
1028370485dSDavid Goodwin unsigned InsertPosIndex) {
103f3cfeef2SSanjay Patel // Kill instructions can define registers but are really nops, and there might
104f3cfeef2SSanjay Patel // be a real definition earlier that needs to be paired with uses dominated by
105f3cfeef2SSanjay Patel // this kill.
106f3cfeef2SSanjay Patel
107f3cfeef2SSanjay Patel // FIXME: It may be possible to remove the isKill() restriction once PR18663
108f3cfeef2SSanjay Patel // has been properly fixed. There can be value in processing kills as seen in
109f3cfeef2SSanjay Patel // the AggressiveAntiDepBreaker class.
110801bf7ebSShiva Chen if (MI.isDebugInstr() || MI.isKill())
1112061c841SDale Johannesen return;
1128370485dSDavid Goodwin assert(Count < InsertPosIndex && "Instruction index out of expected range!");
1138370485dSDavid Goodwin
114c57c220dSBob Wilson for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
115c57c220dSBob Wilson if (KillIndices[Reg] != ~0u) {
116c57c220dSBob Wilson // If Reg is currently live, then mark that it can't be renamed as
117c57c220dSBob Wilson // we don't know the extent of its live-range anymore (now that it
118c57c220dSBob Wilson // has been scheduled).
119c57c220dSBob Wilson Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
120c57c220dSBob Wilson KillIndices[Reg] = Count;
121c57c220dSBob Wilson } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
1228370485dSDavid Goodwin // Any register which was defined within the previous scheduling region
1238370485dSDavid Goodwin // may have been rescheduled and its lifetime may overlap with registers
1248370485dSDavid Goodwin // in ways not reflected in our current liveness state. For each such
1258370485dSDavid Goodwin // register, adjust the liveness state to be conservatively correct.
1268370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
12751a9c0a1SBill Wendling
1288370485dSDavid Goodwin // Move the def index to the end of the previous region, to reflect
1298370485dSDavid Goodwin // that the def could theoretically have been scheduled at the end.
1308370485dSDavid Goodwin DefIndices[Reg] = InsertPosIndex;
1318370485dSDavid Goodwin }
132c57c220dSBob Wilson }
1338370485dSDavid Goodwin
1348370485dSDavid Goodwin PrescanInstruction(MI);
1358370485dSDavid Goodwin ScanInstruction(MI, Count);
1368370485dSDavid Goodwin }
1378370485dSDavid Goodwin
1388370485dSDavid Goodwin /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
1398370485dSDavid Goodwin /// critical path.
CriticalPathStep(const SUnit * SU)14035bc4d46SDan Gohman static const SDep *CriticalPathStep(const SUnit *SU) {
141c0196b1bSCraig Topper const SDep *Next = nullptr;
1428370485dSDavid Goodwin unsigned NextDepth = 0;
1438370485dSDavid Goodwin // Find the predecessor edge with the greatest depth.
144905cf88dSKazu Hirata for (const SDep &P : SU->Preds) {
145905cf88dSKazu Hirata const SUnit *PredSU = P.getSUnit();
146905cf88dSKazu Hirata unsigned PredLatency = P.getLatency();
1478370485dSDavid Goodwin unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
1488370485dSDavid Goodwin // In the case of a latency tie, prefer an anti-dependency edge over
1498370485dSDavid Goodwin // other types of edges.
1508370485dSDavid Goodwin if (NextDepth < PredTotalLatency ||
151905cf88dSKazu Hirata (NextDepth == PredTotalLatency && P.getKind() == SDep::Anti)) {
1528370485dSDavid Goodwin NextDepth = PredTotalLatency;
153905cf88dSKazu Hirata Next = &P;
1548370485dSDavid Goodwin }
1558370485dSDavid Goodwin }
1568370485dSDavid Goodwin return Next;
1578370485dSDavid Goodwin }
1588370485dSDavid Goodwin
PrescanInstruction(MachineInstr & MI)1595e6e8c7aSDuncan P. N. Exon Smith void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
160f128bdcbSEvan Cheng // It's not safe to change register allocation for source operands of
16199475194SSanjay Patel // instructions that have special allocation requirements. Also assume all
16299475194SSanjay Patel // registers used in a call must not be changed (ABI).
163f128bdcbSEvan Cheng // FIXME: The issue with predicated instruction is more complex. We are being
164f3ecfd0eSBob Wilson // conservative here because the kill markers cannot be trusted after
165f128bdcbSEvan Cheng // if-conversion:
1667d9bef8fSFrancis Visoiu Mistrih // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
167f128bdcbSEvan Cheng // ...
1687d9bef8fSFrancis Visoiu Mistrih // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
1697d9bef8fSFrancis Visoiu Mistrih // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
1707d9bef8fSFrancis Visoiu Mistrih // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
171f128bdcbSEvan Cheng //
172f128bdcbSEvan Cheng // The first R6 kill is not really a kill since it's killed by a predicated
173f128bdcbSEvan Cheng // instruction which may not be executed. The second R6 def may or may not
174f128bdcbSEvan Cheng // re-define R6 so it's not safe to change it since the last R6 use cannot be
175f128bdcbSEvan Cheng // changed.
1766307eb55SDuncan P. N. Exon Smith bool Special =
1775e6e8c7aSDuncan P. N. Exon Smith MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);
178f128bdcbSEvan Cheng
1798370485dSDavid Goodwin // Scan the register operands for this instruction and update
1808370485dSDavid Goodwin // Classes and RegRefs.
1815e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1825e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
1838370485dSDavid Goodwin if (!MO.isReg()) continue;
1840c476111SDaniel Sanders Register Reg = MO.getReg();
1858370485dSDavid Goodwin if (Reg == 0) continue;
186c0196b1bSCraig Topper const TargetRegisterClass *NewRC = nullptr;
1878370485dSDavid Goodwin
1885e6e8c7aSDuncan P. N. Exon Smith if (i < MI.getDesc().getNumOperands())
1895e6e8c7aSDuncan P. N. Exon Smith NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
1908370485dSDavid Goodwin
1918370485dSDavid Goodwin // For now, only allow the register to be changed if its register
1928370485dSDavid Goodwin // class is consistent across all uses.
1938370485dSDavid Goodwin if (!Classes[Reg] && NewRC)
1948370485dSDavid Goodwin Classes[Reg] = NewRC;
1958370485dSDavid Goodwin else if (!NewRC || Classes[Reg] != NewRC)
1968370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
1978370485dSDavid Goodwin
1988370485dSDavid Goodwin // Now check for aliases.
19954038d79SJakob Stoklund Olesen for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
2008370485dSDavid Goodwin // If an alias of the reg is used during the live range, give up.
2018370485dSDavid Goodwin // Note that this allows us to skip checking if AntiDepReg
2028370485dSDavid Goodwin // overlaps with any of the aliases, among other things.
20354038d79SJakob Stoklund Olesen unsigned AliasReg = *AI;
2048370485dSDavid Goodwin if (Classes[AliasReg]) {
2058370485dSDavid Goodwin Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
2068370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
2078370485dSDavid Goodwin }
2088370485dSDavid Goodwin }
2098370485dSDavid Goodwin
2108370485dSDavid Goodwin // If we're still willing to consider this register, note the reference.
2118370485dSDavid Goodwin if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
2128370485dSDavid Goodwin RegRefs.insert(std::make_pair(Reg, &MO));
2138370485dSDavid Goodwin
2147b102fccSDanila Malyutin if (MO.isUse() && Special) {
2157b102fccSDanila Malyutin if (!KeepRegs.test(Reg)) {
2167b102fccSDanila Malyutin for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
2177b102fccSDanila Malyutin SubRegs.isValid(); ++SubRegs)
2187b102fccSDanila Malyutin KeepRegs.set(*SubRegs);
2197b102fccSDanila Malyutin }
2207b102fccSDanila Malyutin }
2217b102fccSDanila Malyutin }
2227b102fccSDanila Malyutin
2237b102fccSDanila Malyutin for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
2247b102fccSDanila Malyutin const MachineOperand &MO = MI.getOperand(I);
2257b102fccSDanila Malyutin if (!MO.isReg()) continue;
2267b102fccSDanila Malyutin Register Reg = MO.getReg();
2277b102fccSDanila Malyutin if (!Reg.isValid())
2287b102fccSDanila Malyutin continue;
229dc574ab5SSanjay Patel // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
230dc574ab5SSanjay Patel // it or any of its sub or super regs. We need to use KeepRegs to mark the
231dc574ab5SSanjay Patel // reg because not all uses of the same reg within an instruction are
232dc574ab5SSanjay Patel // necessarily tagged as tied.
233dc574ab5SSanjay Patel // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
234dc574ab5SSanjay Patel // def register but not the second (see PR20020 for details).
235dc574ab5SSanjay Patel // FIXME: can this check be relaxed to account for undef uses
236dc574ab5SSanjay Patel // of a register? In the above 'xor' example, the uses of %eax are undef, so
237dc574ab5SSanjay Patel // earlier instructions could still replace %eax even though the 'xor'
238dc574ab5SSanjay Patel // itself can't be changed.
2397b102fccSDanila Malyutin if (MI.isRegTiedToUseOperand(I) &&
240dc574ab5SSanjay Patel Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
241dc574ab5SSanjay Patel for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
242dc574ab5SSanjay Patel SubRegs.isValid(); ++SubRegs) {
243dc574ab5SSanjay Patel KeepRegs.set(*SubRegs);
244dc574ab5SSanjay Patel }
245dc574ab5SSanjay Patel for (MCSuperRegIterator SuperRegs(Reg, TRI);
246dc574ab5SSanjay Patel SuperRegs.isValid(); ++SuperRegs) {
247dc574ab5SSanjay Patel KeepRegs.set(*SuperRegs);
248dc574ab5SSanjay Patel }
249dc574ab5SSanjay Patel }
2508370485dSDavid Goodwin }
2518370485dSDavid Goodwin }
2528370485dSDavid Goodwin
ScanInstruction(MachineInstr & MI,unsigned Count)2535e6e8c7aSDuncan P. N. Exon Smith void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
2548370485dSDavid Goodwin // Update liveness.
255bde91766SBenjamin Kramer // Proceeding upwards, registers that are defed but not used in this
2568370485dSDavid Goodwin // instruction are now dead.
2575e6e8c7aSDuncan P. N. Exon Smith assert(!MI.isKill() && "Attempting to scan a kill instruction");
258f128bdcbSEvan Cheng
2595e6e8c7aSDuncan P. N. Exon Smith if (!TII->isPredicated(MI)) {
260f128bdcbSEvan Cheng // Predicated defs are modeled as read + write, i.e. similar to two
261f128bdcbSEvan Cheng // address updates.
2625e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2635e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
26438ce889cSJakob Stoklund Olesen
2659283681eSCraig Topper if (MO.isRegMask()) {
2669283681eSCraig Topper auto ClobbersPhysRegAndSubRegs = [&](unsigned PhysReg) {
2679283681eSCraig Topper for (MCSubRegIterator SRI(PhysReg, TRI, true); SRI.isValid(); ++SRI)
2689283681eSCraig Topper if (!MO.clobbersPhysReg(*SRI))
2699283681eSCraig Topper return false;
2709283681eSCraig Topper
2719283681eSCraig Topper return true;
2729283681eSCraig Topper };
2739283681eSCraig Topper
2749283681eSCraig Topper for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
2759283681eSCraig Topper if (ClobbersPhysRegAndSubRegs(i)) {
27638ce889cSJakob Stoklund Olesen DefIndices[i] = Count;
27738ce889cSJakob Stoklund Olesen KillIndices[i] = ~0u;
2785d1bca80SBenjamin Kramer KeepRegs.reset(i);
279c0196b1bSCraig Topper Classes[i] = nullptr;
28038ce889cSJakob Stoklund Olesen RegRefs.erase(i);
28138ce889cSJakob Stoklund Olesen }
2829283681eSCraig Topper }
2839283681eSCraig Topper }
28438ce889cSJakob Stoklund Olesen
2858370485dSDavid Goodwin if (!MO.isReg()) continue;
2860c476111SDaniel Sanders Register Reg = MO.getReg();
2878370485dSDavid Goodwin if (Reg == 0) continue;
2888370485dSDavid Goodwin if (!MO.isDef()) continue;
289dc574ab5SSanjay Patel
2908370485dSDavid Goodwin // Ignore two-addr defs.
2915e6e8c7aSDuncan P. N. Exon Smith if (MI.isRegTiedToUseOperand(i))
2925e6e8c7aSDuncan P. N. Exon Smith continue;
2938370485dSDavid Goodwin
29405aeeb5cSMitch Bodart // If we've already marked this reg as unchangeable, don't remove
29505aeeb5cSMitch Bodart // it or any of its subregs from KeepRegs.
29605aeeb5cSMitch Bodart bool Keep = KeepRegs.test(Reg);
29705aeeb5cSMitch Bodart
298d26358e1SSanjay Patel // For the reg itself and all subregs: update the def to current;
299d26358e1SSanjay Patel // reset the kill state, any restrictions, and references.
300d26358e1SSanjay Patel for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) {
301d26358e1SSanjay Patel unsigned SubregReg = *SRI;
3028370485dSDavid Goodwin DefIndices[SubregReg] = Count;
3038370485dSDavid Goodwin KillIndices[SubregReg] = ~0u;
304c0196b1bSCraig Topper Classes[SubregReg] = nullptr;
3058370485dSDavid Goodwin RegRefs.erase(SubregReg);
30605aeeb5cSMitch Bodart if (!Keep)
30705aeeb5cSMitch Bodart KeepRegs.reset(SubregReg);
3088370485dSDavid Goodwin }
3098370485dSDavid Goodwin // Conservatively mark super-registers as unusable.
31054038d79SJakob Stoklund Olesen for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
31154038d79SJakob Stoklund Olesen Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
3128370485dSDavid Goodwin }
313f128bdcbSEvan Cheng }
3145e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3155e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
3168370485dSDavid Goodwin if (!MO.isReg()) continue;
3170c476111SDaniel Sanders Register Reg = MO.getReg();
3188370485dSDavid Goodwin if (Reg == 0) continue;
3198370485dSDavid Goodwin if (!MO.isUse()) continue;
3208370485dSDavid Goodwin
321c0196b1bSCraig Topper const TargetRegisterClass *NewRC = nullptr;
3225e6e8c7aSDuncan P. N. Exon Smith if (i < MI.getDesc().getNumOperands())
3235e6e8c7aSDuncan P. N. Exon Smith NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
3248370485dSDavid Goodwin
3258370485dSDavid Goodwin // For now, only allow the register to be changed if its register
3268370485dSDavid Goodwin // class is consistent across all uses.
3278370485dSDavid Goodwin if (!Classes[Reg] && NewRC)
3288370485dSDavid Goodwin Classes[Reg] = NewRC;
3298370485dSDavid Goodwin else if (!NewRC || Classes[Reg] != NewRC)
3308370485dSDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
3318370485dSDavid Goodwin
3328370485dSDavid Goodwin RegRefs.insert(std::make_pair(Reg, &MO));
3338370485dSDavid Goodwin
3348370485dSDavid Goodwin // It wasn't previously live but now it is, this is a kill.
335d26358e1SSanjay Patel // Repeat for all aliases.
336d26358e1SSanjay Patel for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
33754038d79SJakob Stoklund Olesen unsigned AliasReg = *AI;
3388370485dSDavid Goodwin if (KillIndices[AliasReg] == ~0u) {
3398370485dSDavid Goodwin KillIndices[AliasReg] = Count;
3408370485dSDavid Goodwin DefIndices[AliasReg] = ~0u;
3418370485dSDavid Goodwin }
3428370485dSDavid Goodwin }
3438370485dSDavid Goodwin }
3448370485dSDavid Goodwin }
3458370485dSDavid Goodwin
3464b491878SAndrew Trick // Check all machine operands that reference the antidependent register and must
3474b491878SAndrew Trick // be replaced by NewReg. Return true if any of their parent instructions may
3484b491878SAndrew Trick // clobber the new register.
3494b491878SAndrew Trick //
3504b491878SAndrew Trick // Note: AntiDepReg may be referenced by a two-address instruction such that
3514b491878SAndrew Trick // it's use operand is tied to a def operand. We guard against the case in which
3524b491878SAndrew Trick // the two-address instruction also defines NewReg, as may happen with
3534b491878SAndrew Trick // pre/postincrement loads. In this case, both the use and def operands are in
3544b491878SAndrew Trick // RegRefs because the def is inserted by PrescanInstruction and not erased
35599475194SSanjay Patel // during ScanInstruction. So checking for an instruction with definitions of
3564b491878SAndrew Trick // both NewReg and AntiDepReg covers it.
35782ae9a95SAndrew Trick bool
isNewRegClobberedByRefs(RegRefIter RegRefBegin,RegRefIter RegRefEnd,unsigned NewReg)3584b491878SAndrew Trick CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
35982ae9a95SAndrew Trick RegRefIter RegRefEnd,
3604f81cdd8SEugene Zelenko unsigned NewReg) {
36182ae9a95SAndrew Trick for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
3624b491878SAndrew Trick MachineOperand *RefOper = I->second;
3634b491878SAndrew Trick
3644b491878SAndrew Trick // Don't allow the instruction defining AntiDepReg to earlyclobber its
3654b491878SAndrew Trick // operands, in case they may be assigned to NewReg. In this case antidep
3664b491878SAndrew Trick // breaking must fail, but it's too rare to bother optimizing.
3674b491878SAndrew Trick if (RefOper->isDef() && RefOper->isEarlyClobber())
36882ae9a95SAndrew Trick return true;
3694b491878SAndrew Trick
37099475194SSanjay Patel // Handle cases in which this instruction defines NewReg.
3714b491878SAndrew Trick MachineInstr *MI = RefOper->getParent();
372bfd5dd15SKazu Hirata for (const MachineOperand &CheckOper : MI->operands()) {
37338ce889cSJakob Stoklund Olesen if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
37438ce889cSJakob Stoklund Olesen return true;
37538ce889cSJakob Stoklund Olesen
3764b491878SAndrew Trick if (!CheckOper.isReg() || !CheckOper.isDef() ||
3774b491878SAndrew Trick CheckOper.getReg() != NewReg)
3784b491878SAndrew Trick continue;
3794b491878SAndrew Trick
3804b491878SAndrew Trick // Don't allow the instruction to define NewReg and AntiDepReg.
3814b491878SAndrew Trick // When AntiDepReg is renamed it will be an illegal op.
3824b491878SAndrew Trick if (RefOper->isDef())
3834b491878SAndrew Trick return true;
3844b491878SAndrew Trick
3854b491878SAndrew Trick // Don't allow an instruction using AntiDepReg to be earlyclobbered by
38699475194SSanjay Patel // NewReg.
3874b491878SAndrew Trick if (CheckOper.isEarlyClobber())
3884b491878SAndrew Trick return true;
3894b491878SAndrew Trick
39099475194SSanjay Patel // Don't allow inline asm to define NewReg at all. Who knows what it's
3914b491878SAndrew Trick // doing with it.
3924b491878SAndrew Trick if (MI->isInlineAsm())
3934b491878SAndrew Trick return true;
3944b491878SAndrew Trick }
39582ae9a95SAndrew Trick }
39682ae9a95SAndrew Trick return false;
39782ae9a95SAndrew Trick }
39882ae9a95SAndrew Trick
3992e4ae4e1SBill Schmidt unsigned CriticalAntiDepBreaker::
findSuitableFreeRegister(RegRefIter RegRefBegin,RegRefIter RegRefEnd,unsigned AntiDepReg,unsigned LastNewReg,const TargetRegisterClass * RC,SmallVectorImpl<unsigned> & Forbid)4002e4ae4e1SBill Schmidt findSuitableFreeRegister(RegRefIter RegRefBegin,
40182ae9a95SAndrew Trick RegRefIter RegRefEnd,
402a7cef4faSJim Grosbach unsigned AntiDepReg,
4038370485dSDavid Goodwin unsigned LastNewReg,
4042e4ae4e1SBill Schmidt const TargetRegisterClass *RC,
4054f81cdd8SEugene Zelenko SmallVectorImpl<unsigned> &Forbid) {
406bdb55e0cSJakob Stoklund Olesen ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
4073aed2822SKazu Hirata for (unsigned NewReg : Order) {
4088370485dSDavid Goodwin // Don't replace a register with itself.
4098370485dSDavid Goodwin if (NewReg == AntiDepReg) continue;
4108370485dSDavid Goodwin // Don't replace a register with one that was recently used to repair
4118370485dSDavid Goodwin // an anti-dependence with this AntiDepReg, because that would
4128370485dSDavid Goodwin // re-introduce that anti-dependence.
4138370485dSDavid Goodwin if (NewReg == LastNewReg) continue;
41482ae9a95SAndrew Trick // If any instructions that define AntiDepReg also define the NewReg, it's
41582ae9a95SAndrew Trick // not suitable. For example, Instruction with multiple definitions can
41682ae9a95SAndrew Trick // result in this condition.
4174b491878SAndrew Trick if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
4188370485dSDavid Goodwin // If NewReg is dead and NewReg's most recent def is not before
4198370485dSDavid Goodwin // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
420eb431da0SJim Grosbach assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
421eb431da0SJim Grosbach && "Kill and Def maps aren't consistent for AntiDepReg!");
422eb431da0SJim Grosbach assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
423eb431da0SJim Grosbach && "Kill and Def maps aren't consistent for NewReg!");
4248370485dSDavid Goodwin if (KillIndices[NewReg] != ~0u ||
4258370485dSDavid Goodwin Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
4268370485dSDavid Goodwin KillIndices[AntiDepReg] > DefIndices[NewReg])
4278370485dSDavid Goodwin continue;
4282e4ae4e1SBill Schmidt // If NewReg overlaps any of the forbidden registers, we can't use it.
4292e4ae4e1SBill Schmidt bool Forbidden = false;
430905cf88dSKazu Hirata for (unsigned R : Forbid)
431905cf88dSKazu Hirata if (TRI->regsOverlap(NewReg, R)) {
4322e4ae4e1SBill Schmidt Forbidden = true;
4332e4ae4e1SBill Schmidt break;
4342e4ae4e1SBill Schmidt }
4352e4ae4e1SBill Schmidt if (Forbidden) continue;
4368370485dSDavid Goodwin return NewReg;
4378370485dSDavid Goodwin }
4388370485dSDavid Goodwin
4398370485dSDavid Goodwin // No registers are free and available!
4408370485dSDavid Goodwin return 0;
4418370485dSDavid Goodwin }
4428370485dSDavid Goodwin
4438370485dSDavid Goodwin unsigned CriticalAntiDepBreaker::
BreakAntiDependencies(const std::vector<SUnit> & SUnits,MachineBasicBlock::iterator Begin,MachineBasicBlock::iterator End,unsigned InsertPosIndex,DbgValueVector & DbgValues)44435bc4d46SDan Gohman BreakAntiDependencies(const std::vector<SUnit> &SUnits,
44535bc4d46SDan Gohman MachineBasicBlock::iterator Begin,
44635bc4d46SDan Gohman MachineBasicBlock::iterator End,
447f02a376fSDevang Patel unsigned InsertPosIndex,
448f02a376fSDevang Patel DbgValueVector &DbgValues) {
4498370485dSDavid Goodwin // The code below assumes that there is at least one instruction,
4508370485dSDavid Goodwin // so just duck out immediately if the block is empty.
4518370485dSDavid Goodwin if (SUnits.empty()) return 0;
4528370485dSDavid Goodwin
45312ac8f03SJim Grosbach // Keep a map of the MachineInstr*'s back to the SUnit representing them.
45412ac8f03SJim Grosbach // This is used for updating debug information.
45546cc9a4aSAndrew Trick //
45646cc9a4aSAndrew Trick // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
45712ac8f03SJim Grosbach DenseMap<MachineInstr *, const SUnit *> MISUnitMap;
45812ac8f03SJim Grosbach
4598370485dSDavid Goodwin // Find the node at the bottom of the critical path.
460c0196b1bSCraig Topper const SUnit *Max = nullptr;
461fd7d4064SKazu Hirata for (const SUnit &SU : SUnits) {
462fd7d4064SKazu Hirata MISUnitMap[SU.getInstr()] = &SU;
463fd7d4064SKazu Hirata if (!Max || SU.getDepth() + SU.Latency > Max->getDepth() + Max->Latency)
464fd7d4064SKazu Hirata Max = &SU;
4658370485dSDavid Goodwin }
4660b184b85SSimon Pilgrim assert(Max && "Failed to find bottom of the critical path");
4678370485dSDavid Goodwin
4688370485dSDavid Goodwin #ifndef NDEBUG
4698370485dSDavid Goodwin {
470d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Critical path has total latency "
4718370485dSDavid Goodwin << (Max->getDepth() + Max->Latency) << "\n");
472d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Available regs:");
4738370485dSDavid Goodwin for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
4748370485dSDavid Goodwin if (KillIndices[Reg] == ~0u)
475d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
4768370485dSDavid Goodwin }
477d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
4788370485dSDavid Goodwin }
4798370485dSDavid Goodwin #endif
4808370485dSDavid Goodwin
4818370485dSDavid Goodwin // Track progress along the critical path through the SUnit graph as we walk
4828370485dSDavid Goodwin // the instructions.
48335bc4d46SDan Gohman const SUnit *CriticalPathSU = Max;
4848370485dSDavid Goodwin MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
4858370485dSDavid Goodwin
4868370485dSDavid Goodwin // Consider this pattern:
4878370485dSDavid Goodwin // A = ...
4888370485dSDavid Goodwin // ... = A
4898370485dSDavid Goodwin // A = ...
4908370485dSDavid Goodwin // ... = A
4918370485dSDavid Goodwin // A = ...
4928370485dSDavid Goodwin // ... = A
4938370485dSDavid Goodwin // A = ...
4948370485dSDavid Goodwin // ... = A
4958370485dSDavid Goodwin // There are three anti-dependencies here, and without special care,
4968370485dSDavid Goodwin // we'd break all of them using the same register:
4978370485dSDavid Goodwin // A = ...
4988370485dSDavid Goodwin // ... = A
4998370485dSDavid Goodwin // B = ...
5008370485dSDavid Goodwin // ... = B
5018370485dSDavid Goodwin // B = ...
5028370485dSDavid Goodwin // ... = B
5038370485dSDavid Goodwin // B = ...
5048370485dSDavid Goodwin // ... = B
5058370485dSDavid Goodwin // because at each anti-dependence, B is the first register that
5068370485dSDavid Goodwin // isn't A which is free. This re-introduces anti-dependencies
5078370485dSDavid Goodwin // at all but one of the original anti-dependencies that we were
5088370485dSDavid Goodwin // trying to break. To avoid this, keep track of the most recent
5098370485dSDavid Goodwin // register that each register was replaced with, avoid
5108370485dSDavid Goodwin // using it to repair an anti-dependence on the same register.
5118370485dSDavid Goodwin // This lets us produce this:
5128370485dSDavid Goodwin // A = ...
5138370485dSDavid Goodwin // ... = A
5148370485dSDavid Goodwin // B = ...
5158370485dSDavid Goodwin // ... = B
5168370485dSDavid Goodwin // C = ...
5178370485dSDavid Goodwin // ... = C
5188370485dSDavid Goodwin // B = ...
5198370485dSDavid Goodwin // ... = B
5208370485dSDavid Goodwin // This still has an anti-dependence on B, but at least it isn't on the
5218370485dSDavid Goodwin // original critical path.
5228370485dSDavid Goodwin //
5238370485dSDavid Goodwin // TODO: If we tracked more than one register here, we could potentially
5248370485dSDavid Goodwin // fix that remaining critical edge too. This is a little more involved,
5258370485dSDavid Goodwin // because unlike the most recent register, less recent registers should
5268370485dSDavid Goodwin // still be considered, though only if no other registers are available.
52751a9c0a1SBill Wendling std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
5288370485dSDavid Goodwin
5298370485dSDavid Goodwin // Attempt to break anti-dependence edges on the critical path. Walk the
5308370485dSDavid Goodwin // instructions from the bottom up, tracking information about liveness
5318370485dSDavid Goodwin // as we go to help determine which registers are available.
5328370485dSDavid Goodwin unsigned Broken = 0;
5338370485dSDavid Goodwin unsigned Count = InsertPosIndex - 1;
53499475194SSanjay Patel for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
5355e6e8c7aSDuncan P. N. Exon Smith MachineInstr &MI = *--I;
536f3cfeef2SSanjay Patel // Kill instructions can define registers but are really nops, and there
537f3cfeef2SSanjay Patel // might be a real definition earlier that needs to be paired with uses
538f3cfeef2SSanjay Patel // dominated by this kill.
539f3cfeef2SSanjay Patel
540f3cfeef2SSanjay Patel // FIXME: It may be possible to remove the isKill() restriction once PR18663
541f3cfeef2SSanjay Patel // has been properly fixed. There can be value in processing kills as seen
542f3cfeef2SSanjay Patel // in the AggressiveAntiDepBreaker class.
543801bf7ebSShiva Chen if (MI.isDebugInstr() || MI.isKill())
5442061c841SDale Johannesen continue;
5458370485dSDavid Goodwin
5468370485dSDavid Goodwin // Check if this instruction has a dependence on the critical path that
5478370485dSDavid Goodwin // is an anti-dependence that we may be able to break. If it is, set
5488370485dSDavid Goodwin // AntiDepReg to the non-zero register associated with the anti-dependence.
5498370485dSDavid Goodwin //
5508370485dSDavid Goodwin // We limit our attention to the critical path as a heuristic to avoid
5518370485dSDavid Goodwin // breaking anti-dependence edges that aren't going to significantly
5528370485dSDavid Goodwin // impact the overall schedule. There are a limited number of registers
5538370485dSDavid Goodwin // and we want to save them for the important edges.
5548370485dSDavid Goodwin //
5558370485dSDavid Goodwin // TODO: Instructions with multiple defs could have multiple
5568370485dSDavid Goodwin // anti-dependencies. The current code here only knows how to break one
5578370485dSDavid Goodwin // edge per instruction. Note that we'd have to be able to break all of
5588370485dSDavid Goodwin // the anti-dependencies in an instruction in order to be effective.
5598370485dSDavid Goodwin unsigned AntiDepReg = 0;
5605e6e8c7aSDuncan P. N. Exon Smith if (&MI == CriticalPathMI) {
56135bc4d46SDan Gohman if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
56235bc4d46SDan Gohman const SUnit *NextSU = Edge->getSUnit();
5638370485dSDavid Goodwin
5648370485dSDavid Goodwin // Only consider anti-dependence edges.
5658370485dSDavid Goodwin if (Edge->getKind() == SDep::Anti) {
5668370485dSDavid Goodwin AntiDepReg = Edge->getReg();
5678370485dSDavid Goodwin assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
568f67bf3e0SJakob Stoklund Olesen if (!MRI.isAllocatable(AntiDepReg))
5698370485dSDavid Goodwin // Don't break anti-dependencies on non-allocatable registers.
5708370485dSDavid Goodwin AntiDepReg = 0;
5715d1bca80SBenjamin Kramer else if (KeepRegs.test(AntiDepReg))
57299475194SSanjay Patel // Don't break anti-dependencies if a use down below requires
5738370485dSDavid Goodwin // this exact register.
5748370485dSDavid Goodwin AntiDepReg = 0;
5758370485dSDavid Goodwin else {
5768370485dSDavid Goodwin // If the SUnit has other dependencies on the SUnit that it
5778370485dSDavid Goodwin // anti-depends on, don't bother breaking the anti-dependency
5788370485dSDavid Goodwin // since those edges would prevent such units from being
5798370485dSDavid Goodwin // scheduled past each other regardless.
5808370485dSDavid Goodwin //
5818370485dSDavid Goodwin // Also, if there are dependencies on other SUnits with the
5828370485dSDavid Goodwin // same register as the anti-dependency, don't attempt to
5838370485dSDavid Goodwin // break it.
584905cf88dSKazu Hirata for (const SDep &P : CriticalPathSU->Preds)
585905cf88dSKazu Hirata if (P.getSUnit() == NextSU
586905cf88dSKazu Hirata ? (P.getKind() != SDep::Anti || P.getReg() != AntiDepReg)
587905cf88dSKazu Hirata : (P.getKind() == SDep::Data &&
588905cf88dSKazu Hirata P.getReg() == AntiDepReg)) {
5898370485dSDavid Goodwin AntiDepReg = 0;
5908370485dSDavid Goodwin break;
5918370485dSDavid Goodwin }
5928370485dSDavid Goodwin }
5938370485dSDavid Goodwin }
5948370485dSDavid Goodwin CriticalPathSU = NextSU;
5958370485dSDavid Goodwin CriticalPathMI = CriticalPathSU->getInstr();
5968370485dSDavid Goodwin } else {
5978370485dSDavid Goodwin // We've reached the end of the critical path.
598c0196b1bSCraig Topper CriticalPathSU = nullptr;
599c0196b1bSCraig Topper CriticalPathMI = nullptr;
6008370485dSDavid Goodwin }
6018370485dSDavid Goodwin }
6028370485dSDavid Goodwin
6038370485dSDavid Goodwin PrescanInstruction(MI);
6048370485dSDavid Goodwin
6052e4ae4e1SBill Schmidt SmallVector<unsigned, 2> ForbidRegs;
6062e4ae4e1SBill Schmidt
607f128bdcbSEvan Cheng // If MI's defs have a special allocation requirement, don't allow
608f128bdcbSEvan Cheng // any def registers to be changed. Also assume all registers
609f128bdcbSEvan Cheng // defined in a call must not be changed (ABI).
6105e6e8c7aSDuncan P. N. Exon Smith if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
6118370485dSDavid Goodwin // If this instruction's defs have special allocation requirement, don't
6128370485dSDavid Goodwin // break this anti-dependency.
6138370485dSDavid Goodwin AntiDepReg = 0;
6148370485dSDavid Goodwin else if (AntiDepReg) {
6158370485dSDavid Goodwin // If this instruction has a use of AntiDepReg, breaking it
6162e4ae4e1SBill Schmidt // is invalid. If the instruction defines other registers,
6172e4ae4e1SBill Schmidt // save a list of them so that we don't pick a new register
6182e4ae4e1SBill Schmidt // that overlaps any of them.
619bfd5dd15SKazu Hirata for (const MachineOperand &MO : MI.operands()) {
6208370485dSDavid Goodwin if (!MO.isReg()) continue;
6210c476111SDaniel Sanders Register Reg = MO.getReg();
6228370485dSDavid Goodwin if (Reg == 0) continue;
623f128bdcbSEvan Cheng if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
6248370485dSDavid Goodwin AntiDepReg = 0;
6258370485dSDavid Goodwin break;
6268370485dSDavid Goodwin }
6272e4ae4e1SBill Schmidt if (MO.isDef() && Reg != AntiDepReg)
6282e4ae4e1SBill Schmidt ForbidRegs.push_back(Reg);
6298370485dSDavid Goodwin }
6308370485dSDavid Goodwin }
6318370485dSDavid Goodwin
6328370485dSDavid Goodwin // Determine AntiDepReg's register class, if it is live and is
6338370485dSDavid Goodwin // consistently used within a single class.
634c0196b1bSCraig Topper const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
635c0196b1bSCraig Topper : nullptr;
636c0196b1bSCraig Topper assert((AntiDepReg == 0 || RC != nullptr) &&
6378370485dSDavid Goodwin "Register should be live if it's causing an anti-dependence!");
6388370485dSDavid Goodwin if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
6398370485dSDavid Goodwin AntiDepReg = 0;
6408370485dSDavid Goodwin
641cb402911SAlp Toker // Look for a suitable register to use to break the anti-dependence.
6428370485dSDavid Goodwin //
6438370485dSDavid Goodwin // TODO: Instead of picking the first free register, consider which might
6448370485dSDavid Goodwin // be the best.
6458370485dSDavid Goodwin if (AntiDepReg != 0) {
64682ae9a95SAndrew Trick std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
64782ae9a95SAndrew Trick std::multimap<unsigned, MachineOperand *>::iterator>
64882ae9a95SAndrew Trick Range = RegRefs.equal_range(AntiDepReg);
64982ae9a95SAndrew Trick if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
65082ae9a95SAndrew Trick AntiDepReg,
6518370485dSDavid Goodwin LastNewReg[AntiDepReg],
6522e4ae4e1SBill Schmidt RC, ForbidRegs)) {
653d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Breaking anti-dependence edge on "
654c71cced0SFrancis Visoiu Mistrih << printReg(AntiDepReg, TRI) << " with "
655c71cced0SFrancis Visoiu Mistrih << RegRefs.count(AntiDepReg) << " references"
656c71cced0SFrancis Visoiu Mistrih << " using " << printReg(NewReg, TRI) << "!\n");
6578370485dSDavid Goodwin
6588370485dSDavid Goodwin // Update the references to the old register to refer to the new
6598370485dSDavid Goodwin // register.
6608370485dSDavid Goodwin for (std::multimap<unsigned, MachineOperand *>::iterator
66112ac8f03SJim Grosbach Q = Range.first, QE = Range.second; Q != QE; ++Q) {
6628370485dSDavid Goodwin Q->second->setReg(NewReg);
66312ac8f03SJim Grosbach // If the SU for the instruction being updated has debug information
66412ac8f03SJim Grosbach // related to the anti-dependency register, make sure to update that
66512ac8f03SJim Grosbach // as well.
66612ac8f03SJim Grosbach const SUnit *SU = MISUnitMap[Q->second->getParent()];
66784854830SJim Grosbach if (!SU) continue;
66810ebfe06SAndrew Ng UpdateDbgValues(DbgValues, Q->second->getParent(),
66910ebfe06SAndrew Ng AntiDepReg, NewReg);
67012ac8f03SJim Grosbach }
6718370485dSDavid Goodwin
6728370485dSDavid Goodwin // We just went back in time and modified history; the
673c57c220dSBob Wilson // liveness information for the anti-dependence reg is now
6748370485dSDavid Goodwin // inconsistent. Set the state as if it were dead.
6758370485dSDavid Goodwin Classes[NewReg] = Classes[AntiDepReg];
6768370485dSDavid Goodwin DefIndices[NewReg] = DefIndices[AntiDepReg];
6778370485dSDavid Goodwin KillIndices[NewReg] = KillIndices[AntiDepReg];
6788370485dSDavid Goodwin assert(((KillIndices[NewReg] == ~0u) !=
6798370485dSDavid Goodwin (DefIndices[NewReg] == ~0u)) &&
6808370485dSDavid Goodwin "Kill and Def maps aren't consistent for NewReg!");
6818370485dSDavid Goodwin
682c0196b1bSCraig Topper Classes[AntiDepReg] = nullptr;
6838370485dSDavid Goodwin DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
6848370485dSDavid Goodwin KillIndices[AntiDepReg] = ~0u;
6858370485dSDavid Goodwin assert(((KillIndices[AntiDepReg] == ~0u) !=
6868370485dSDavid Goodwin (DefIndices[AntiDepReg] == ~0u)) &&
6878370485dSDavid Goodwin "Kill and Def maps aren't consistent for AntiDepReg!");
6888370485dSDavid Goodwin
6898370485dSDavid Goodwin RegRefs.erase(AntiDepReg);
6908370485dSDavid Goodwin LastNewReg[AntiDepReg] = NewReg;
6918370485dSDavid Goodwin ++Broken;
6928370485dSDavid Goodwin }
6938370485dSDavid Goodwin }
6948370485dSDavid Goodwin
6958370485dSDavid Goodwin ScanInstruction(MI, Count);
6968370485dSDavid Goodwin }
6978370485dSDavid Goodwin
6988370485dSDavid Goodwin return Broken;
6998370485dSDavid Goodwin }
700c228c717SThomas Raoux
701c228c717SThomas Raoux AntiDepBreaker *
createCriticalAntiDepBreaker(MachineFunction & MFi,const RegisterClassInfo & RCI)702c228c717SThomas Raoux llvm::createCriticalAntiDepBreaker(MachineFunction &MFi,
703c228c717SThomas Raoux const RegisterClassInfo &RCI) {
704c228c717SThomas Raoux return new CriticalAntiDepBreaker(MFi, RCI);
705c228c717SThomas Raoux }
706