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