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