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