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