xref: /aosp_15_r20/external/llvm/lib/CodeGen/CriticalAntiDepBreaker.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===----- CriticalAntiDepBreaker.cpp - Anti-dep breaker -------- ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the CriticalAntiDepBreaker class, which
11*9880d681SAndroid Build Coastguard Worker // implements register anti-dependence breaking along a blocks
12*9880d681SAndroid Build Coastguard Worker // critical path during post-RA scheduler.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "CriticalAntiDepBreaker.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "post-RA-sched"
29*9880d681SAndroid Build Coastguard Worker 
CriticalAntiDepBreaker(MachineFunction & MFi,const RegisterClassInfo & RCI)30*9880d681SAndroid Build Coastguard Worker CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,
31*9880d681SAndroid Build Coastguard Worker                                                const RegisterClassInfo &RCI)
32*9880d681SAndroid Build Coastguard Worker     : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
33*9880d681SAndroid Build Coastguard Worker       TII(MF.getSubtarget().getInstrInfo()),
34*9880d681SAndroid Build Coastguard Worker       TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
35*9880d681SAndroid Build Coastguard Worker       Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),
36*9880d681SAndroid Build Coastguard Worker       DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(), false) {}
37*9880d681SAndroid Build Coastguard Worker 
~CriticalAntiDepBreaker()38*9880d681SAndroid Build Coastguard Worker CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
39*9880d681SAndroid Build Coastguard Worker }
40*9880d681SAndroid Build Coastguard Worker 
StartBlock(MachineBasicBlock * BB)41*9880d681SAndroid Build Coastguard Worker void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
42*9880d681SAndroid Build Coastguard Worker   const unsigned BBSize = BB->size();
43*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
44*9880d681SAndroid Build Coastguard Worker     // Clear out the register class data.
45*9880d681SAndroid Build Coastguard Worker     Classes[i] = nullptr;
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker     // Initialize the indices to indicate that no registers are live.
48*9880d681SAndroid Build Coastguard Worker     KillIndices[i] = ~0u;
49*9880d681SAndroid Build Coastguard Worker     DefIndices[i] = BBSize;
50*9880d681SAndroid Build Coastguard Worker   }
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   // Clear "do not change" set.
53*9880d681SAndroid Build Coastguard Worker   KeepRegs.reset();
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker   bool IsReturnBlock = BB->isReturnBlock();
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker   // Examine the live-in regs of all successors.
58*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
59*9880d681SAndroid Build Coastguard Worker          SE = BB->succ_end(); SI != SE; ++SI)
60*9880d681SAndroid Build Coastguard Worker     for (const auto &LI : (*SI)->liveins()) {
61*9880d681SAndroid Build Coastguard Worker       for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
62*9880d681SAndroid Build Coastguard Worker         unsigned Reg = *AI;
63*9880d681SAndroid Build Coastguard Worker         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
64*9880d681SAndroid Build Coastguard Worker         KillIndices[Reg] = BBSize;
65*9880d681SAndroid Build Coastguard Worker         DefIndices[Reg] = ~0u;
66*9880d681SAndroid Build Coastguard Worker       }
67*9880d681SAndroid Build Coastguard Worker     }
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker   // Mark live-out callee-saved registers. In a return block this is
70*9880d681SAndroid Build Coastguard Worker   // all callee-saved registers. In non-return this is any
71*9880d681SAndroid Build Coastguard Worker   // callee-saved register that is not saved in the prolog.
72*9880d681SAndroid Build Coastguard Worker   const MachineFrameInfo *MFI = MF.getFrameInfo();
73*9880d681SAndroid Build Coastguard Worker   BitVector Pristine = MFI->getPristineRegs(MF);
74*9880d681SAndroid Build Coastguard Worker   for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
75*9880d681SAndroid Build Coastguard Worker     if (!IsReturnBlock && !Pristine.test(*I)) continue;
76*9880d681SAndroid Build Coastguard Worker     for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
77*9880d681SAndroid Build Coastguard Worker       unsigned Reg = *AI;
78*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
79*9880d681SAndroid Build Coastguard Worker       KillIndices[Reg] = BBSize;
80*9880d681SAndroid Build Coastguard Worker       DefIndices[Reg] = ~0u;
81*9880d681SAndroid Build Coastguard Worker     }
82*9880d681SAndroid Build Coastguard Worker   }
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker 
FinishBlock()85*9880d681SAndroid Build Coastguard Worker void CriticalAntiDepBreaker::FinishBlock() {
86*9880d681SAndroid Build Coastguard Worker   RegRefs.clear();
87*9880d681SAndroid Build Coastguard Worker   KeepRegs.reset();
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker 
Observe(MachineInstr & MI,unsigned Count,unsigned InsertPosIndex)90*9880d681SAndroid Build Coastguard Worker void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
91*9880d681SAndroid Build Coastguard Worker                                      unsigned InsertPosIndex) {
92*9880d681SAndroid Build Coastguard Worker   // Kill instructions can define registers but are really nops, and there might
93*9880d681SAndroid Build Coastguard Worker   // be a real definition earlier that needs to be paired with uses dominated by
94*9880d681SAndroid Build Coastguard Worker   // this kill.
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   // FIXME: It may be possible to remove the isKill() restriction once PR18663
97*9880d681SAndroid Build Coastguard Worker   // has been properly fixed. There can be value in processing kills as seen in
98*9880d681SAndroid Build Coastguard Worker   // the AggressiveAntiDepBreaker class.
99*9880d681SAndroid Build Coastguard Worker   if (MI.isDebugValue() || MI.isKill())
100*9880d681SAndroid Build Coastguard Worker     return;
101*9880d681SAndroid Build Coastguard Worker   assert(Count < InsertPosIndex && "Instruction index out of expected range!");
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker   for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
104*9880d681SAndroid Build Coastguard Worker     if (KillIndices[Reg] != ~0u) {
105*9880d681SAndroid Build Coastguard Worker       // If Reg is currently live, then mark that it can't be renamed as
106*9880d681SAndroid Build Coastguard Worker       // we don't know the extent of its live-range anymore (now that it
107*9880d681SAndroid Build Coastguard Worker       // has been scheduled).
108*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
109*9880d681SAndroid Build Coastguard Worker       KillIndices[Reg] = Count;
110*9880d681SAndroid Build Coastguard Worker     } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
111*9880d681SAndroid Build Coastguard Worker       // Any register which was defined within the previous scheduling region
112*9880d681SAndroid Build Coastguard Worker       // may have been rescheduled and its lifetime may overlap with registers
113*9880d681SAndroid Build Coastguard Worker       // in ways not reflected in our current liveness state. For each such
114*9880d681SAndroid Build Coastguard Worker       // register, adjust the liveness state to be conservatively correct.
115*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker       // Move the def index to the end of the previous region, to reflect
118*9880d681SAndroid Build Coastguard Worker       // that the def could theoretically have been scheduled at the end.
119*9880d681SAndroid Build Coastguard Worker       DefIndices[Reg] = InsertPosIndex;
120*9880d681SAndroid Build Coastguard Worker     }
121*9880d681SAndroid Build Coastguard Worker   }
122*9880d681SAndroid Build Coastguard Worker 
123*9880d681SAndroid Build Coastguard Worker   PrescanInstruction(MI);
124*9880d681SAndroid Build Coastguard Worker   ScanInstruction(MI, Count);
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
128*9880d681SAndroid Build Coastguard Worker /// critical path.
CriticalPathStep(const SUnit * SU)129*9880d681SAndroid Build Coastguard Worker static const SDep *CriticalPathStep(const SUnit *SU) {
130*9880d681SAndroid Build Coastguard Worker   const SDep *Next = nullptr;
131*9880d681SAndroid Build Coastguard Worker   unsigned NextDepth = 0;
132*9880d681SAndroid Build Coastguard Worker   // Find the predecessor edge with the greatest depth.
133*9880d681SAndroid Build Coastguard Worker   for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
134*9880d681SAndroid Build Coastguard Worker        P != PE; ++P) {
135*9880d681SAndroid Build Coastguard Worker     const SUnit *PredSU = P->getSUnit();
136*9880d681SAndroid Build Coastguard Worker     unsigned PredLatency = P->getLatency();
137*9880d681SAndroid Build Coastguard Worker     unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
138*9880d681SAndroid Build Coastguard Worker     // In the case of a latency tie, prefer an anti-dependency edge over
139*9880d681SAndroid Build Coastguard Worker     // other types of edges.
140*9880d681SAndroid Build Coastguard Worker     if (NextDepth < PredTotalLatency ||
141*9880d681SAndroid Build Coastguard Worker         (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
142*9880d681SAndroid Build Coastguard Worker       NextDepth = PredTotalLatency;
143*9880d681SAndroid Build Coastguard Worker       Next = &*P;
144*9880d681SAndroid Build Coastguard Worker     }
145*9880d681SAndroid Build Coastguard Worker   }
146*9880d681SAndroid Build Coastguard Worker   return Next;
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker 
PrescanInstruction(MachineInstr & MI)149*9880d681SAndroid Build Coastguard Worker void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
150*9880d681SAndroid Build Coastguard Worker   // It's not safe to change register allocation for source operands of
151*9880d681SAndroid Build Coastguard Worker   // instructions that have special allocation requirements. Also assume all
152*9880d681SAndroid Build Coastguard Worker   // registers used in a call must not be changed (ABI).
153*9880d681SAndroid Build Coastguard Worker   // FIXME: The issue with predicated instruction is more complex. We are being
154*9880d681SAndroid Build Coastguard Worker   // conservative here because the kill markers cannot be trusted after
155*9880d681SAndroid Build Coastguard Worker   // if-conversion:
156*9880d681SAndroid Build Coastguard Worker   // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
157*9880d681SAndroid Build Coastguard Worker   // ...
158*9880d681SAndroid Build Coastguard Worker   // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
159*9880d681SAndroid Build Coastguard Worker   // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
160*9880d681SAndroid Build Coastguard Worker   // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
161*9880d681SAndroid Build Coastguard Worker   //
162*9880d681SAndroid Build Coastguard Worker   // The first R6 kill is not really a kill since it's killed by a predicated
163*9880d681SAndroid Build Coastguard Worker   // instruction which may not be executed. The second R6 def may or may not
164*9880d681SAndroid Build Coastguard Worker   // re-define R6 so it's not safe to change it since the last R6 use cannot be
165*9880d681SAndroid Build Coastguard Worker   // changed.
166*9880d681SAndroid Build Coastguard Worker   bool Special =
167*9880d681SAndroid Build Coastguard Worker       MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);
168*9880d681SAndroid Build Coastguard Worker 
169*9880d681SAndroid Build Coastguard Worker   // Scan the register operands for this instruction and update
170*9880d681SAndroid Build Coastguard Worker   // Classes and RegRefs.
171*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
172*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI.getOperand(i);
173*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg()) continue;
174*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
175*9880d681SAndroid Build Coastguard Worker     if (Reg == 0) continue;
176*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *NewRC = nullptr;
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker     if (i < MI.getDesc().getNumOperands())
179*9880d681SAndroid Build Coastguard Worker       NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
180*9880d681SAndroid Build Coastguard Worker 
181*9880d681SAndroid Build Coastguard Worker     // For now, only allow the register to be changed if its register
182*9880d681SAndroid Build Coastguard Worker     // class is consistent across all uses.
183*9880d681SAndroid Build Coastguard Worker     if (!Classes[Reg] && NewRC)
184*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = NewRC;
185*9880d681SAndroid Build Coastguard Worker     else if (!NewRC || Classes[Reg] != NewRC)
186*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker     // Now check for aliases.
189*9880d681SAndroid Build Coastguard Worker     for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
190*9880d681SAndroid Build Coastguard Worker       // If an alias of the reg is used during the live range, give up.
191*9880d681SAndroid Build Coastguard Worker       // Note that this allows us to skip checking if AntiDepReg
192*9880d681SAndroid Build Coastguard Worker       // overlaps with any of the aliases, among other things.
193*9880d681SAndroid Build Coastguard Worker       unsigned AliasReg = *AI;
194*9880d681SAndroid Build Coastguard Worker       if (Classes[AliasReg]) {
195*9880d681SAndroid Build Coastguard Worker         Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
196*9880d681SAndroid Build Coastguard Worker         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
197*9880d681SAndroid Build Coastguard Worker       }
198*9880d681SAndroid Build Coastguard Worker     }
199*9880d681SAndroid Build Coastguard Worker 
200*9880d681SAndroid Build Coastguard Worker     // If we're still willing to consider this register, note the reference.
201*9880d681SAndroid Build Coastguard Worker     if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
202*9880d681SAndroid Build Coastguard Worker       RegRefs.insert(std::make_pair(Reg, &MO));
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker     // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
205*9880d681SAndroid Build Coastguard Worker     // it or any of its sub or super regs. We need to use KeepRegs to mark the
206*9880d681SAndroid Build Coastguard Worker     // reg because not all uses of the same reg within an instruction are
207*9880d681SAndroid Build Coastguard Worker     // necessarily tagged as tied.
208*9880d681SAndroid Build Coastguard Worker     // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
209*9880d681SAndroid Build Coastguard Worker     // def register but not the second (see PR20020 for details).
210*9880d681SAndroid Build Coastguard Worker     // FIXME: can this check be relaxed to account for undef uses
211*9880d681SAndroid Build Coastguard Worker     // of a register? In the above 'xor' example, the uses of %eax are undef, so
212*9880d681SAndroid Build Coastguard Worker     // earlier instructions could still replace %eax even though the 'xor'
213*9880d681SAndroid Build Coastguard Worker     // itself can't be changed.
214*9880d681SAndroid Build Coastguard Worker     if (MI.isRegTiedToUseOperand(i) &&
215*9880d681SAndroid Build Coastguard Worker         Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
216*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
217*9880d681SAndroid Build Coastguard Worker            SubRegs.isValid(); ++SubRegs) {
218*9880d681SAndroid Build Coastguard Worker         KeepRegs.set(*SubRegs);
219*9880d681SAndroid Build Coastguard Worker       }
220*9880d681SAndroid Build Coastguard Worker       for (MCSuperRegIterator SuperRegs(Reg, TRI);
221*9880d681SAndroid Build Coastguard Worker            SuperRegs.isValid(); ++SuperRegs) {
222*9880d681SAndroid Build Coastguard Worker         KeepRegs.set(*SuperRegs);
223*9880d681SAndroid Build Coastguard Worker       }
224*9880d681SAndroid Build Coastguard Worker     }
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker     if (MO.isUse() && Special) {
227*9880d681SAndroid Build Coastguard Worker       if (!KeepRegs.test(Reg)) {
228*9880d681SAndroid Build Coastguard Worker         for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
229*9880d681SAndroid Build Coastguard Worker              SubRegs.isValid(); ++SubRegs)
230*9880d681SAndroid Build Coastguard Worker           KeepRegs.set(*SubRegs);
231*9880d681SAndroid Build Coastguard Worker       }
232*9880d681SAndroid Build Coastguard Worker     }
233*9880d681SAndroid Build Coastguard Worker   }
234*9880d681SAndroid Build Coastguard Worker }
235*9880d681SAndroid Build Coastguard Worker 
ScanInstruction(MachineInstr & MI,unsigned Count)236*9880d681SAndroid Build Coastguard Worker void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
237*9880d681SAndroid Build Coastguard Worker   // Update liveness.
238*9880d681SAndroid Build Coastguard Worker   // Proceeding upwards, registers that are defed but not used in this
239*9880d681SAndroid Build Coastguard Worker   // instruction are now dead.
240*9880d681SAndroid Build Coastguard Worker   assert(!MI.isKill() && "Attempting to scan a kill instruction");
241*9880d681SAndroid Build Coastguard Worker 
242*9880d681SAndroid Build Coastguard Worker   if (!TII->isPredicated(MI)) {
243*9880d681SAndroid Build Coastguard Worker     // Predicated defs are modeled as read + write, i.e. similar to two
244*9880d681SAndroid Build Coastguard Worker     // address updates.
245*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
246*9880d681SAndroid Build Coastguard Worker       MachineOperand &MO = MI.getOperand(i);
247*9880d681SAndroid Build Coastguard Worker 
248*9880d681SAndroid Build Coastguard Worker       if (MO.isRegMask())
249*9880d681SAndroid Build Coastguard Worker         for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
250*9880d681SAndroid Build Coastguard Worker           if (MO.clobbersPhysReg(i)) {
251*9880d681SAndroid Build Coastguard Worker             DefIndices[i] = Count;
252*9880d681SAndroid Build Coastguard Worker             KillIndices[i] = ~0u;
253*9880d681SAndroid Build Coastguard Worker             KeepRegs.reset(i);
254*9880d681SAndroid Build Coastguard Worker             Classes[i] = nullptr;
255*9880d681SAndroid Build Coastguard Worker             RegRefs.erase(i);
256*9880d681SAndroid Build Coastguard Worker           }
257*9880d681SAndroid Build Coastguard Worker 
258*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg()) continue;
259*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
260*9880d681SAndroid Build Coastguard Worker       if (Reg == 0) continue;
261*9880d681SAndroid Build Coastguard Worker       if (!MO.isDef()) continue;
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker       // Ignore two-addr defs.
264*9880d681SAndroid Build Coastguard Worker       if (MI.isRegTiedToUseOperand(i))
265*9880d681SAndroid Build Coastguard Worker         continue;
266*9880d681SAndroid Build Coastguard Worker 
267*9880d681SAndroid Build Coastguard Worker       // If we've already marked this reg as unchangeable, don't remove
268*9880d681SAndroid Build Coastguard Worker       // it or any of its subregs from KeepRegs.
269*9880d681SAndroid Build Coastguard Worker       bool Keep = KeepRegs.test(Reg);
270*9880d681SAndroid Build Coastguard Worker 
271*9880d681SAndroid Build Coastguard Worker       // For the reg itself and all subregs: update the def to current;
272*9880d681SAndroid Build Coastguard Worker       // reset the kill state, any restrictions, and references.
273*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) {
274*9880d681SAndroid Build Coastguard Worker         unsigned SubregReg = *SRI;
275*9880d681SAndroid Build Coastguard Worker         DefIndices[SubregReg] = Count;
276*9880d681SAndroid Build Coastguard Worker         KillIndices[SubregReg] = ~0u;
277*9880d681SAndroid Build Coastguard Worker         Classes[SubregReg] = nullptr;
278*9880d681SAndroid Build Coastguard Worker         RegRefs.erase(SubregReg);
279*9880d681SAndroid Build Coastguard Worker         if (!Keep)
280*9880d681SAndroid Build Coastguard Worker           KeepRegs.reset(SubregReg);
281*9880d681SAndroid Build Coastguard Worker       }
282*9880d681SAndroid Build Coastguard Worker       // Conservatively mark super-registers as unusable.
283*9880d681SAndroid Build Coastguard Worker       for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
284*9880d681SAndroid Build Coastguard Worker         Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
285*9880d681SAndroid Build Coastguard Worker     }
286*9880d681SAndroid Build Coastguard Worker   }
287*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
288*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI.getOperand(i);
289*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg()) continue;
290*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
291*9880d681SAndroid Build Coastguard Worker     if (Reg == 0) continue;
292*9880d681SAndroid Build Coastguard Worker     if (!MO.isUse()) continue;
293*9880d681SAndroid Build Coastguard Worker 
294*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *NewRC = nullptr;
295*9880d681SAndroid Build Coastguard Worker     if (i < MI.getDesc().getNumOperands())
296*9880d681SAndroid Build Coastguard Worker       NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
297*9880d681SAndroid Build Coastguard Worker 
298*9880d681SAndroid Build Coastguard Worker     // For now, only allow the register to be changed if its register
299*9880d681SAndroid Build Coastguard Worker     // class is consistent across all uses.
300*9880d681SAndroid Build Coastguard Worker     if (!Classes[Reg] && NewRC)
301*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = NewRC;
302*9880d681SAndroid Build Coastguard Worker     else if (!NewRC || Classes[Reg] != NewRC)
303*9880d681SAndroid Build Coastguard Worker       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
304*9880d681SAndroid Build Coastguard Worker 
305*9880d681SAndroid Build Coastguard Worker     RegRefs.insert(std::make_pair(Reg, &MO));
306*9880d681SAndroid Build Coastguard Worker 
307*9880d681SAndroid Build Coastguard Worker     // It wasn't previously live but now it is, this is a kill.
308*9880d681SAndroid Build Coastguard Worker     // Repeat for all aliases.
309*9880d681SAndroid Build Coastguard Worker     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
310*9880d681SAndroid Build Coastguard Worker       unsigned AliasReg = *AI;
311*9880d681SAndroid Build Coastguard Worker       if (KillIndices[AliasReg] == ~0u) {
312*9880d681SAndroid Build Coastguard Worker         KillIndices[AliasReg] = Count;
313*9880d681SAndroid Build Coastguard Worker         DefIndices[AliasReg] = ~0u;
314*9880d681SAndroid Build Coastguard Worker       }
315*9880d681SAndroid Build Coastguard Worker     }
316*9880d681SAndroid Build Coastguard Worker   }
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker // Check all machine operands that reference the antidependent register and must
320*9880d681SAndroid Build Coastguard Worker // be replaced by NewReg. Return true if any of their parent instructions may
321*9880d681SAndroid Build Coastguard Worker // clobber the new register.
322*9880d681SAndroid Build Coastguard Worker //
323*9880d681SAndroid Build Coastguard Worker // Note: AntiDepReg may be referenced by a two-address instruction such that
324*9880d681SAndroid Build Coastguard Worker // it's use operand is tied to a def operand. We guard against the case in which
325*9880d681SAndroid Build Coastguard Worker // the two-address instruction also defines NewReg, as may happen with
326*9880d681SAndroid Build Coastguard Worker // pre/postincrement loads. In this case, both the use and def operands are in
327*9880d681SAndroid Build Coastguard Worker // RegRefs because the def is inserted by PrescanInstruction and not erased
328*9880d681SAndroid Build Coastguard Worker // during ScanInstruction. So checking for an instruction with definitions of
329*9880d681SAndroid Build Coastguard Worker // both NewReg and AntiDepReg covers it.
330*9880d681SAndroid Build Coastguard Worker bool
isNewRegClobberedByRefs(RegRefIter RegRefBegin,RegRefIter RegRefEnd,unsigned NewReg)331*9880d681SAndroid Build Coastguard Worker CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
332*9880d681SAndroid Build Coastguard Worker                                                 RegRefIter RegRefEnd,
333*9880d681SAndroid Build Coastguard Worker                                                 unsigned NewReg)
334*9880d681SAndroid Build Coastguard Worker {
335*9880d681SAndroid Build Coastguard Worker   for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
336*9880d681SAndroid Build Coastguard Worker     MachineOperand *RefOper = I->second;
337*9880d681SAndroid Build Coastguard Worker 
338*9880d681SAndroid Build Coastguard Worker     // Don't allow the instruction defining AntiDepReg to earlyclobber its
339*9880d681SAndroid Build Coastguard Worker     // operands, in case they may be assigned to NewReg. In this case antidep
340*9880d681SAndroid Build Coastguard Worker     // breaking must fail, but it's too rare to bother optimizing.
341*9880d681SAndroid Build Coastguard Worker     if (RefOper->isDef() && RefOper->isEarlyClobber())
342*9880d681SAndroid Build Coastguard Worker       return true;
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker     // Handle cases in which this instruction defines NewReg.
345*9880d681SAndroid Build Coastguard Worker     MachineInstr *MI = RefOper->getParent();
346*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
347*9880d681SAndroid Build Coastguard Worker       const MachineOperand &CheckOper = MI->getOperand(i);
348*9880d681SAndroid Build Coastguard Worker 
349*9880d681SAndroid Build Coastguard Worker       if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
350*9880d681SAndroid Build Coastguard Worker         return true;
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker       if (!CheckOper.isReg() || !CheckOper.isDef() ||
353*9880d681SAndroid Build Coastguard Worker           CheckOper.getReg() != NewReg)
354*9880d681SAndroid Build Coastguard Worker         continue;
355*9880d681SAndroid Build Coastguard Worker 
356*9880d681SAndroid Build Coastguard Worker       // Don't allow the instruction to define NewReg and AntiDepReg.
357*9880d681SAndroid Build Coastguard Worker       // When AntiDepReg is renamed it will be an illegal op.
358*9880d681SAndroid Build Coastguard Worker       if (RefOper->isDef())
359*9880d681SAndroid Build Coastguard Worker         return true;
360*9880d681SAndroid Build Coastguard Worker 
361*9880d681SAndroid Build Coastguard Worker       // Don't allow an instruction using AntiDepReg to be earlyclobbered by
362*9880d681SAndroid Build Coastguard Worker       // NewReg.
363*9880d681SAndroid Build Coastguard Worker       if (CheckOper.isEarlyClobber())
364*9880d681SAndroid Build Coastguard Worker         return true;
365*9880d681SAndroid Build Coastguard Worker 
366*9880d681SAndroid Build Coastguard Worker       // Don't allow inline asm to define NewReg at all. Who knows what it's
367*9880d681SAndroid Build Coastguard Worker       // doing with it.
368*9880d681SAndroid Build Coastguard Worker       if (MI->isInlineAsm())
369*9880d681SAndroid Build Coastguard Worker         return true;
370*9880d681SAndroid Build Coastguard Worker     }
371*9880d681SAndroid Build Coastguard Worker   }
372*9880d681SAndroid Build Coastguard Worker   return false;
373*9880d681SAndroid Build Coastguard Worker }
374*9880d681SAndroid Build Coastguard Worker 
375*9880d681SAndroid Build Coastguard Worker unsigned CriticalAntiDepBreaker::
findSuitableFreeRegister(RegRefIter RegRefBegin,RegRefIter RegRefEnd,unsigned AntiDepReg,unsigned LastNewReg,const TargetRegisterClass * RC,SmallVectorImpl<unsigned> & Forbid)376*9880d681SAndroid Build Coastguard Worker findSuitableFreeRegister(RegRefIter RegRefBegin,
377*9880d681SAndroid Build Coastguard Worker                          RegRefIter RegRefEnd,
378*9880d681SAndroid Build Coastguard Worker                          unsigned AntiDepReg,
379*9880d681SAndroid Build Coastguard Worker                          unsigned LastNewReg,
380*9880d681SAndroid Build Coastguard Worker                          const TargetRegisterClass *RC,
381*9880d681SAndroid Build Coastguard Worker                          SmallVectorImpl<unsigned> &Forbid)
382*9880d681SAndroid Build Coastguard Worker {
383*9880d681SAndroid Build Coastguard Worker   ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
384*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i != Order.size(); ++i) {
385*9880d681SAndroid Build Coastguard Worker     unsigned NewReg = Order[i];
386*9880d681SAndroid Build Coastguard Worker     // Don't replace a register with itself.
387*9880d681SAndroid Build Coastguard Worker     if (NewReg == AntiDepReg) continue;
388*9880d681SAndroid Build Coastguard Worker     // Don't replace a register with one that was recently used to repair
389*9880d681SAndroid Build Coastguard Worker     // an anti-dependence with this AntiDepReg, because that would
390*9880d681SAndroid Build Coastguard Worker     // re-introduce that anti-dependence.
391*9880d681SAndroid Build Coastguard Worker     if (NewReg == LastNewReg) continue;
392*9880d681SAndroid Build Coastguard Worker     // If any instructions that define AntiDepReg also define the NewReg, it's
393*9880d681SAndroid Build Coastguard Worker     // not suitable.  For example, Instruction with multiple definitions can
394*9880d681SAndroid Build Coastguard Worker     // result in this condition.
395*9880d681SAndroid Build Coastguard Worker     if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
396*9880d681SAndroid Build Coastguard Worker     // If NewReg is dead and NewReg's most recent def is not before
397*9880d681SAndroid Build Coastguard Worker     // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
398*9880d681SAndroid Build Coastguard Worker     assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
399*9880d681SAndroid Build Coastguard Worker            && "Kill and Def maps aren't consistent for AntiDepReg!");
400*9880d681SAndroid Build Coastguard Worker     assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
401*9880d681SAndroid Build Coastguard Worker            && "Kill and Def maps aren't consistent for NewReg!");
402*9880d681SAndroid Build Coastguard Worker     if (KillIndices[NewReg] != ~0u ||
403*9880d681SAndroid Build Coastguard Worker         Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
404*9880d681SAndroid Build Coastguard Worker         KillIndices[AntiDepReg] > DefIndices[NewReg])
405*9880d681SAndroid Build Coastguard Worker       continue;
406*9880d681SAndroid Build Coastguard Worker     // If NewReg overlaps any of the forbidden registers, we can't use it.
407*9880d681SAndroid Build Coastguard Worker     bool Forbidden = false;
408*9880d681SAndroid Build Coastguard Worker     for (SmallVectorImpl<unsigned>::iterator it = Forbid.begin(),
409*9880d681SAndroid Build Coastguard Worker            ite = Forbid.end(); it != ite; ++it)
410*9880d681SAndroid Build Coastguard Worker       if (TRI->regsOverlap(NewReg, *it)) {
411*9880d681SAndroid Build Coastguard Worker         Forbidden = true;
412*9880d681SAndroid Build Coastguard Worker         break;
413*9880d681SAndroid Build Coastguard Worker       }
414*9880d681SAndroid Build Coastguard Worker     if (Forbidden) continue;
415*9880d681SAndroid Build Coastguard Worker     return NewReg;
416*9880d681SAndroid Build Coastguard Worker   }
417*9880d681SAndroid Build Coastguard Worker 
418*9880d681SAndroid Build Coastguard Worker   // No registers are free and available!
419*9880d681SAndroid Build Coastguard Worker   return 0;
420*9880d681SAndroid Build Coastguard Worker }
421*9880d681SAndroid Build Coastguard Worker 
422*9880d681SAndroid Build Coastguard Worker unsigned CriticalAntiDepBreaker::
BreakAntiDependencies(const std::vector<SUnit> & SUnits,MachineBasicBlock::iterator Begin,MachineBasicBlock::iterator End,unsigned InsertPosIndex,DbgValueVector & DbgValues)423*9880d681SAndroid Build Coastguard Worker BreakAntiDependencies(const std::vector<SUnit>& SUnits,
424*9880d681SAndroid Build Coastguard Worker                       MachineBasicBlock::iterator Begin,
425*9880d681SAndroid Build Coastguard Worker                       MachineBasicBlock::iterator End,
426*9880d681SAndroid Build Coastguard Worker                       unsigned InsertPosIndex,
427*9880d681SAndroid Build Coastguard Worker                       DbgValueVector &DbgValues) {
428*9880d681SAndroid Build Coastguard Worker   // The code below assumes that there is at least one instruction,
429*9880d681SAndroid Build Coastguard Worker   // so just duck out immediately if the block is empty.
430*9880d681SAndroid Build Coastguard Worker   if (SUnits.empty()) return 0;
431*9880d681SAndroid Build Coastguard Worker 
432*9880d681SAndroid Build Coastguard Worker   // Keep a map of the MachineInstr*'s back to the SUnit representing them.
433*9880d681SAndroid Build Coastguard Worker   // This is used for updating debug information.
434*9880d681SAndroid Build Coastguard Worker   //
435*9880d681SAndroid Build Coastguard Worker   // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
436*9880d681SAndroid Build Coastguard Worker   DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
437*9880d681SAndroid Build Coastguard Worker 
438*9880d681SAndroid Build Coastguard Worker   // Find the node at the bottom of the critical path.
439*9880d681SAndroid Build Coastguard Worker   const SUnit *Max = nullptr;
440*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
441*9880d681SAndroid Build Coastguard Worker     const SUnit *SU = &SUnits[i];
442*9880d681SAndroid Build Coastguard Worker     MISUnitMap[SU->getInstr()] = SU;
443*9880d681SAndroid Build Coastguard Worker     if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
444*9880d681SAndroid Build Coastguard Worker       Max = SU;
445*9880d681SAndroid Build Coastguard Worker   }
446*9880d681SAndroid Build Coastguard Worker 
447*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
448*9880d681SAndroid Build Coastguard Worker   {
449*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Critical path has total latency "
450*9880d681SAndroid Build Coastguard Worker           << (Max->getDepth() + Max->Latency) << "\n");
451*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Available regs:");
452*9880d681SAndroid Build Coastguard Worker     for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
453*9880d681SAndroid Build Coastguard Worker       if (KillIndices[Reg] == ~0u)
454*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << " " << TRI->getName(Reg));
455*9880d681SAndroid Build Coastguard Worker     }
456*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << '\n');
457*9880d681SAndroid Build Coastguard Worker   }
458*9880d681SAndroid Build Coastguard Worker #endif
459*9880d681SAndroid Build Coastguard Worker 
460*9880d681SAndroid Build Coastguard Worker   // Track progress along the critical path through the SUnit graph as we walk
461*9880d681SAndroid Build Coastguard Worker   // the instructions.
462*9880d681SAndroid Build Coastguard Worker   const SUnit *CriticalPathSU = Max;
463*9880d681SAndroid Build Coastguard Worker   MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
464*9880d681SAndroid Build Coastguard Worker 
465*9880d681SAndroid Build Coastguard Worker   // Consider this pattern:
466*9880d681SAndroid Build Coastguard Worker   //   A = ...
467*9880d681SAndroid Build Coastguard Worker   //   ... = A
468*9880d681SAndroid Build Coastguard Worker   //   A = ...
469*9880d681SAndroid Build Coastguard Worker   //   ... = A
470*9880d681SAndroid Build Coastguard Worker   //   A = ...
471*9880d681SAndroid Build Coastguard Worker   //   ... = A
472*9880d681SAndroid Build Coastguard Worker   //   A = ...
473*9880d681SAndroid Build Coastguard Worker   //   ... = A
474*9880d681SAndroid Build Coastguard Worker   // There are three anti-dependencies here, and without special care,
475*9880d681SAndroid Build Coastguard Worker   // we'd break all of them using the same register:
476*9880d681SAndroid Build Coastguard Worker   //   A = ...
477*9880d681SAndroid Build Coastguard Worker   //   ... = A
478*9880d681SAndroid Build Coastguard Worker   //   B = ...
479*9880d681SAndroid Build Coastguard Worker   //   ... = B
480*9880d681SAndroid Build Coastguard Worker   //   B = ...
481*9880d681SAndroid Build Coastguard Worker   //   ... = B
482*9880d681SAndroid Build Coastguard Worker   //   B = ...
483*9880d681SAndroid Build Coastguard Worker   //   ... = B
484*9880d681SAndroid Build Coastguard Worker   // because at each anti-dependence, B is the first register that
485*9880d681SAndroid Build Coastguard Worker   // isn't A which is free.  This re-introduces anti-dependencies
486*9880d681SAndroid Build Coastguard Worker   // at all but one of the original anti-dependencies that we were
487*9880d681SAndroid Build Coastguard Worker   // trying to break.  To avoid this, keep track of the most recent
488*9880d681SAndroid Build Coastguard Worker   // register that each register was replaced with, avoid
489*9880d681SAndroid Build Coastguard Worker   // using it to repair an anti-dependence on the same register.
490*9880d681SAndroid Build Coastguard Worker   // This lets us produce this:
491*9880d681SAndroid Build Coastguard Worker   //   A = ...
492*9880d681SAndroid Build Coastguard Worker   //   ... = A
493*9880d681SAndroid Build Coastguard Worker   //   B = ...
494*9880d681SAndroid Build Coastguard Worker   //   ... = B
495*9880d681SAndroid Build Coastguard Worker   //   C = ...
496*9880d681SAndroid Build Coastguard Worker   //   ... = C
497*9880d681SAndroid Build Coastguard Worker   //   B = ...
498*9880d681SAndroid Build Coastguard Worker   //   ... = B
499*9880d681SAndroid Build Coastguard Worker   // This still has an anti-dependence on B, but at least it isn't on the
500*9880d681SAndroid Build Coastguard Worker   // original critical path.
501*9880d681SAndroid Build Coastguard Worker   //
502*9880d681SAndroid Build Coastguard Worker   // TODO: If we tracked more than one register here, we could potentially
503*9880d681SAndroid Build Coastguard Worker   // fix that remaining critical edge too. This is a little more involved,
504*9880d681SAndroid Build Coastguard Worker   // because unlike the most recent register, less recent registers should
505*9880d681SAndroid Build Coastguard Worker   // still be considered, though only if no other registers are available.
506*9880d681SAndroid Build Coastguard Worker   std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
507*9880d681SAndroid Build Coastguard Worker 
508*9880d681SAndroid Build Coastguard Worker   // Attempt to break anti-dependence edges on the critical path. Walk the
509*9880d681SAndroid Build Coastguard Worker   // instructions from the bottom up, tracking information about liveness
510*9880d681SAndroid Build Coastguard Worker   // as we go to help determine which registers are available.
511*9880d681SAndroid Build Coastguard Worker   unsigned Broken = 0;
512*9880d681SAndroid Build Coastguard Worker   unsigned Count = InsertPosIndex - 1;
513*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
514*9880d681SAndroid Build Coastguard Worker     MachineInstr &MI = *--I;
515*9880d681SAndroid Build Coastguard Worker     // Kill instructions can define registers but are really nops, and there
516*9880d681SAndroid Build Coastguard Worker     // might be a real definition earlier that needs to be paired with uses
517*9880d681SAndroid Build Coastguard Worker     // dominated by this kill.
518*9880d681SAndroid Build Coastguard Worker 
519*9880d681SAndroid Build Coastguard Worker     // FIXME: It may be possible to remove the isKill() restriction once PR18663
520*9880d681SAndroid Build Coastguard Worker     // has been properly fixed. There can be value in processing kills as seen
521*9880d681SAndroid Build Coastguard Worker     // in the AggressiveAntiDepBreaker class.
522*9880d681SAndroid Build Coastguard Worker     if (MI.isDebugValue() || MI.isKill())
523*9880d681SAndroid Build Coastguard Worker       continue;
524*9880d681SAndroid Build Coastguard Worker 
525*9880d681SAndroid Build Coastguard Worker     // Check if this instruction has a dependence on the critical path that
526*9880d681SAndroid Build Coastguard Worker     // is an anti-dependence that we may be able to break. If it is, set
527*9880d681SAndroid Build Coastguard Worker     // AntiDepReg to the non-zero register associated with the anti-dependence.
528*9880d681SAndroid Build Coastguard Worker     //
529*9880d681SAndroid Build Coastguard Worker     // We limit our attention to the critical path as a heuristic to avoid
530*9880d681SAndroid Build Coastguard Worker     // breaking anti-dependence edges that aren't going to significantly
531*9880d681SAndroid Build Coastguard Worker     // impact the overall schedule. There are a limited number of registers
532*9880d681SAndroid Build Coastguard Worker     // and we want to save them for the important edges.
533*9880d681SAndroid Build Coastguard Worker     //
534*9880d681SAndroid Build Coastguard Worker     // TODO: Instructions with multiple defs could have multiple
535*9880d681SAndroid Build Coastguard Worker     // anti-dependencies. The current code here only knows how to break one
536*9880d681SAndroid Build Coastguard Worker     // edge per instruction. Note that we'd have to be able to break all of
537*9880d681SAndroid Build Coastguard Worker     // the anti-dependencies in an instruction in order to be effective.
538*9880d681SAndroid Build Coastguard Worker     unsigned AntiDepReg = 0;
539*9880d681SAndroid Build Coastguard Worker     if (&MI == CriticalPathMI) {
540*9880d681SAndroid Build Coastguard Worker       if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
541*9880d681SAndroid Build Coastguard Worker         const SUnit *NextSU = Edge->getSUnit();
542*9880d681SAndroid Build Coastguard Worker 
543*9880d681SAndroid Build Coastguard Worker         // Only consider anti-dependence edges.
544*9880d681SAndroid Build Coastguard Worker         if (Edge->getKind() == SDep::Anti) {
545*9880d681SAndroid Build Coastguard Worker           AntiDepReg = Edge->getReg();
546*9880d681SAndroid Build Coastguard Worker           assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
547*9880d681SAndroid Build Coastguard Worker           if (!MRI.isAllocatable(AntiDepReg))
548*9880d681SAndroid Build Coastguard Worker             // Don't break anti-dependencies on non-allocatable registers.
549*9880d681SAndroid Build Coastguard Worker             AntiDepReg = 0;
550*9880d681SAndroid Build Coastguard Worker           else if (KeepRegs.test(AntiDepReg))
551*9880d681SAndroid Build Coastguard Worker             // Don't break anti-dependencies if a use down below requires
552*9880d681SAndroid Build Coastguard Worker             // this exact register.
553*9880d681SAndroid Build Coastguard Worker             AntiDepReg = 0;
554*9880d681SAndroid Build Coastguard Worker           else {
555*9880d681SAndroid Build Coastguard Worker             // If the SUnit has other dependencies on the SUnit that it
556*9880d681SAndroid Build Coastguard Worker             // anti-depends on, don't bother breaking the anti-dependency
557*9880d681SAndroid Build Coastguard Worker             // since those edges would prevent such units from being
558*9880d681SAndroid Build Coastguard Worker             // scheduled past each other regardless.
559*9880d681SAndroid Build Coastguard Worker             //
560*9880d681SAndroid Build Coastguard Worker             // Also, if there are dependencies on other SUnits with the
561*9880d681SAndroid Build Coastguard Worker             // same register as the anti-dependency, don't attempt to
562*9880d681SAndroid Build Coastguard Worker             // break it.
563*9880d681SAndroid Build Coastguard Worker             for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
564*9880d681SAndroid Build Coastguard Worker                  PE = CriticalPathSU->Preds.end(); P != PE; ++P)
565*9880d681SAndroid Build Coastguard Worker               if (P->getSUnit() == NextSU ?
566*9880d681SAndroid Build Coastguard Worker                     (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
567*9880d681SAndroid Build Coastguard Worker                     (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
568*9880d681SAndroid Build Coastguard Worker                 AntiDepReg = 0;
569*9880d681SAndroid Build Coastguard Worker                 break;
570*9880d681SAndroid Build Coastguard Worker               }
571*9880d681SAndroid Build Coastguard Worker           }
572*9880d681SAndroid Build Coastguard Worker         }
573*9880d681SAndroid Build Coastguard Worker         CriticalPathSU = NextSU;
574*9880d681SAndroid Build Coastguard Worker         CriticalPathMI = CriticalPathSU->getInstr();
575*9880d681SAndroid Build Coastguard Worker       } else {
576*9880d681SAndroid Build Coastguard Worker         // We've reached the end of the critical path.
577*9880d681SAndroid Build Coastguard Worker         CriticalPathSU = nullptr;
578*9880d681SAndroid Build Coastguard Worker         CriticalPathMI = nullptr;
579*9880d681SAndroid Build Coastguard Worker       }
580*9880d681SAndroid Build Coastguard Worker     }
581*9880d681SAndroid Build Coastguard Worker 
582*9880d681SAndroid Build Coastguard Worker     PrescanInstruction(MI);
583*9880d681SAndroid Build Coastguard Worker 
584*9880d681SAndroid Build Coastguard Worker     SmallVector<unsigned, 2> ForbidRegs;
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker     // If MI's defs have a special allocation requirement, don't allow
587*9880d681SAndroid Build Coastguard Worker     // any def registers to be changed. Also assume all registers
588*9880d681SAndroid Build Coastguard Worker     // defined in a call must not be changed (ABI).
589*9880d681SAndroid Build Coastguard Worker     if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
590*9880d681SAndroid Build Coastguard Worker       // If this instruction's defs have special allocation requirement, don't
591*9880d681SAndroid Build Coastguard Worker       // break this anti-dependency.
592*9880d681SAndroid Build Coastguard Worker       AntiDepReg = 0;
593*9880d681SAndroid Build Coastguard Worker     else if (AntiDepReg) {
594*9880d681SAndroid Build Coastguard Worker       // If this instruction has a use of AntiDepReg, breaking it
595*9880d681SAndroid Build Coastguard Worker       // is invalid.  If the instruction defines other registers,
596*9880d681SAndroid Build Coastguard Worker       // save a list of them so that we don't pick a new register
597*9880d681SAndroid Build Coastguard Worker       // that overlaps any of them.
598*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
599*9880d681SAndroid Build Coastguard Worker         MachineOperand &MO = MI.getOperand(i);
600*9880d681SAndroid Build Coastguard Worker         if (!MO.isReg()) continue;
601*9880d681SAndroid Build Coastguard Worker         unsigned Reg = MO.getReg();
602*9880d681SAndroid Build Coastguard Worker         if (Reg == 0) continue;
603*9880d681SAndroid Build Coastguard Worker         if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
604*9880d681SAndroid Build Coastguard Worker           AntiDepReg = 0;
605*9880d681SAndroid Build Coastguard Worker           break;
606*9880d681SAndroid Build Coastguard Worker         }
607*9880d681SAndroid Build Coastguard Worker         if (MO.isDef() && Reg != AntiDepReg)
608*9880d681SAndroid Build Coastguard Worker           ForbidRegs.push_back(Reg);
609*9880d681SAndroid Build Coastguard Worker       }
610*9880d681SAndroid Build Coastguard Worker     }
611*9880d681SAndroid Build Coastguard Worker 
612*9880d681SAndroid Build Coastguard Worker     // Determine AntiDepReg's register class, if it is live and is
613*9880d681SAndroid Build Coastguard Worker     // consistently used within a single class.
614*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
615*9880d681SAndroid Build Coastguard Worker                                                     : nullptr;
616*9880d681SAndroid Build Coastguard Worker     assert((AntiDepReg == 0 || RC != nullptr) &&
617*9880d681SAndroid Build Coastguard Worker            "Register should be live if it's causing an anti-dependence!");
618*9880d681SAndroid Build Coastguard Worker     if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
619*9880d681SAndroid Build Coastguard Worker       AntiDepReg = 0;
620*9880d681SAndroid Build Coastguard Worker 
621*9880d681SAndroid Build Coastguard Worker     // Look for a suitable register to use to break the anti-dependence.
622*9880d681SAndroid Build Coastguard Worker     //
623*9880d681SAndroid Build Coastguard Worker     // TODO: Instead of picking the first free register, consider which might
624*9880d681SAndroid Build Coastguard Worker     // be the best.
625*9880d681SAndroid Build Coastguard Worker     if (AntiDepReg != 0) {
626*9880d681SAndroid Build Coastguard Worker       std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
627*9880d681SAndroid Build Coastguard Worker                 std::multimap<unsigned, MachineOperand *>::iterator>
628*9880d681SAndroid Build Coastguard Worker         Range = RegRefs.equal_range(AntiDepReg);
629*9880d681SAndroid Build Coastguard Worker       if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
630*9880d681SAndroid Build Coastguard Worker                                                      AntiDepReg,
631*9880d681SAndroid Build Coastguard Worker                                                      LastNewReg[AntiDepReg],
632*9880d681SAndroid Build Coastguard Worker                                                      RC, ForbidRegs)) {
633*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "Breaking anti-dependence edge on "
634*9880d681SAndroid Build Coastguard Worker               << TRI->getName(AntiDepReg)
635*9880d681SAndroid Build Coastguard Worker               << " with " << RegRefs.count(AntiDepReg) << " references"
636*9880d681SAndroid Build Coastguard Worker               << " using " << TRI->getName(NewReg) << "!\n");
637*9880d681SAndroid Build Coastguard Worker 
638*9880d681SAndroid Build Coastguard Worker         // Update the references to the old register to refer to the new
639*9880d681SAndroid Build Coastguard Worker         // register.
640*9880d681SAndroid Build Coastguard Worker         for (std::multimap<unsigned, MachineOperand *>::iterator
641*9880d681SAndroid Build Coastguard Worker              Q = Range.first, QE = Range.second; Q != QE; ++Q) {
642*9880d681SAndroid Build Coastguard Worker           Q->second->setReg(NewReg);
643*9880d681SAndroid Build Coastguard Worker           // If the SU for the instruction being updated has debug information
644*9880d681SAndroid Build Coastguard Worker           // related to the anti-dependency register, make sure to update that
645*9880d681SAndroid Build Coastguard Worker           // as well.
646*9880d681SAndroid Build Coastguard Worker           const SUnit *SU = MISUnitMap[Q->second->getParent()];
647*9880d681SAndroid Build Coastguard Worker           if (!SU) continue;
648*9880d681SAndroid Build Coastguard Worker           for (DbgValueVector::iterator DVI = DbgValues.begin(),
649*9880d681SAndroid Build Coastguard Worker                  DVE = DbgValues.end(); DVI != DVE; ++DVI)
650*9880d681SAndroid Build Coastguard Worker             if (DVI->second == Q->second->getParent())
651*9880d681SAndroid Build Coastguard Worker               UpdateDbgValue(*DVI->first, AntiDepReg, NewReg);
652*9880d681SAndroid Build Coastguard Worker         }
653*9880d681SAndroid Build Coastguard Worker 
654*9880d681SAndroid Build Coastguard Worker         // We just went back in time and modified history; the
655*9880d681SAndroid Build Coastguard Worker         // liveness information for the anti-dependence reg is now
656*9880d681SAndroid Build Coastguard Worker         // inconsistent. Set the state as if it were dead.
657*9880d681SAndroid Build Coastguard Worker         Classes[NewReg] = Classes[AntiDepReg];
658*9880d681SAndroid Build Coastguard Worker         DefIndices[NewReg] = DefIndices[AntiDepReg];
659*9880d681SAndroid Build Coastguard Worker         KillIndices[NewReg] = KillIndices[AntiDepReg];
660*9880d681SAndroid Build Coastguard Worker         assert(((KillIndices[NewReg] == ~0u) !=
661*9880d681SAndroid Build Coastguard Worker                 (DefIndices[NewReg] == ~0u)) &&
662*9880d681SAndroid Build Coastguard Worker              "Kill and Def maps aren't consistent for NewReg!");
663*9880d681SAndroid Build Coastguard Worker 
664*9880d681SAndroid Build Coastguard Worker         Classes[AntiDepReg] = nullptr;
665*9880d681SAndroid Build Coastguard Worker         DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
666*9880d681SAndroid Build Coastguard Worker         KillIndices[AntiDepReg] = ~0u;
667*9880d681SAndroid Build Coastguard Worker         assert(((KillIndices[AntiDepReg] == ~0u) !=
668*9880d681SAndroid Build Coastguard Worker                 (DefIndices[AntiDepReg] == ~0u)) &&
669*9880d681SAndroid Build Coastguard Worker              "Kill and Def maps aren't consistent for AntiDepReg!");
670*9880d681SAndroid Build Coastguard Worker 
671*9880d681SAndroid Build Coastguard Worker         RegRefs.erase(AntiDepReg);
672*9880d681SAndroid Build Coastguard Worker         LastNewReg[AntiDepReg] = NewReg;
673*9880d681SAndroid Build Coastguard Worker         ++Broken;
674*9880d681SAndroid Build Coastguard Worker       }
675*9880d681SAndroid Build Coastguard Worker     }
676*9880d681SAndroid Build Coastguard Worker 
677*9880d681SAndroid Build Coastguard Worker     ScanInstruction(MI, Count);
678*9880d681SAndroid Build Coastguard Worker   }
679*9880d681SAndroid Build Coastguard Worker 
680*9880d681SAndroid Build Coastguard Worker   return Broken;
681*9880d681SAndroid Build Coastguard Worker }
682