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