Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 1 | //===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===// |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 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 AggressiveAntiDepBreaker class, which |
| 11 | // implements register anti-dependence breaking during post-RA |
| 12 | // scheduling. It attempts to break all anti-dependencies within a |
| 13 | // block. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 17 | #include "AggressiveAntiDepBreaker.h" |
| 18 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
Andrew Trick | 05ff466 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/RegisterClassInfo.h" |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetRegisterInfo.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "post-RA-sched" |
| 31 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 32 | // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod |
| 33 | static cl::opt<int> |
| 34 | DebugDiv("agg-antidep-debugdiv", |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 35 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 36 | cl::init(0), cl::Hidden); |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 37 | static cl::opt<int> |
| 38 | DebugMod("agg-antidep-debugmod", |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 39 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 40 | cl::init(0), cl::Hidden); |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 41 | |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 42 | AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs, |
| 43 | MachineBasicBlock *BB) : |
Bill Wendling | 51a9c0a | 2010-07-15 19:58:14 +0000 | [diff] [blame] | 44 | NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0), |
| 45 | GroupNodeIndices(TargetRegs, 0), |
| 46 | KillIndices(TargetRegs, 0), |
| 47 | DefIndices(TargetRegs, 0) |
| 48 | { |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 49 | const unsigned BBSize = BB->size(); |
| 50 | for (unsigned i = 0; i < NumTargetRegs; ++i) { |
| 51 | // Initialize all registers to be in their own group. Initially we |
| 52 | // assign the register to the same-indexed GroupNode. |
| 53 | GroupNodeIndices[i] = i; |
| 54 | // Initialize the indices to indicate that no registers are live. |
| 55 | KillIndices[i] = ~0u; |
| 56 | DefIndices[i] = BBSize; |
| 57 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Bill Wendling | 5a8d15c | 2010-07-15 19:41:20 +0000 | [diff] [blame] | 60 | unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 61 | unsigned Node = GroupNodeIndices[Reg]; |
| 62 | while (GroupNodes[Node] != Node) |
| 63 | Node = GroupNodes[Node]; |
| 64 | |
| 65 | return Node; |
| 66 | } |
| 67 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 68 | void AggressiveAntiDepState::GetGroupRegs( |
| 69 | unsigned Group, |
| 70 | std::vector<unsigned> &Regs, |
| 71 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 72 | { |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 73 | for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 74 | if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0)) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 75 | Regs.push_back(Reg); |
| 76 | } |
| 77 | } |
| 78 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 79 | unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 80 | { |
| 81 | assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!"); |
| 82 | assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 83 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 84 | // find group for each register |
| 85 | unsigned Group1 = GetGroup(Reg1); |
| 86 | unsigned Group2 = GetGroup(Reg2); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 87 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 88 | // if either group is 0, then that must become the parent |
| 89 | unsigned Parent = (Group1 == 0) ? Group1 : Group2; |
| 90 | unsigned Other = (Parent == Group1) ? Group2 : Group1; |
| 91 | GroupNodes.at(Other) = Parent; |
| 92 | return Parent; |
| 93 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 94 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 95 | unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 96 | { |
| 97 | // Create a new GroupNode for Reg. Reg's existing GroupNode must |
| 98 | // stay as is because there could be other GroupNodes referring to |
| 99 | // it. |
| 100 | unsigned idx = GroupNodes.size(); |
| 101 | GroupNodes.push_back(idx); |
| 102 | GroupNodeIndices[Reg] = idx; |
| 103 | return idx; |
| 104 | } |
| 105 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 106 | bool AggressiveAntiDepState::IsLive(unsigned Reg) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 107 | { |
| 108 | // KillIndex must be defined and DefIndex not defined for a register |
| 109 | // to be live. |
| 110 | return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u)); |
| 111 | } |
| 112 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 113 | AggressiveAntiDepBreaker::AggressiveAntiDepBreaker( |
| 114 | MachineFunction &MFi, const RegisterClassInfo &RCI, |
| 115 | TargetSubtargetInfo::RegClassVector &CriticalPathRCs) |
| 116 | : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 117 | TII(MF.getSubtarget().getInstrInfo()), |
| 118 | TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI), |
| 119 | State(nullptr) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 120 | /* Collect a bitset of all registers that are only broken if they |
| 121 | are on the critical path. */ |
| 122 | for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) { |
| 123 | BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]); |
| 124 | if (CriticalPathSet.none()) |
| 125 | CriticalPathSet = CPSet; |
| 126 | else |
| 127 | CriticalPathSet |= CPSet; |
| 128 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 129 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 130 | DEBUG(dbgs() << "AntiDep Critical-Path Registers:"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 131 | DEBUG(for (int r = CriticalPathSet.find_first(); r != -1; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 132 | r = CriticalPathSet.find_next(r)) |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 133 | dbgs() << " " << TRI->getName(r)); |
| 134 | DEBUG(dbgs() << '\n'); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() { |
| 138 | delete State; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 142 | assert(!State); |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 143 | State = new AggressiveAntiDepState(TRI->getNumRegs(), BB); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 144 | |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 145 | bool IsReturnBlock = (!BB->empty() && BB->back().isReturn()); |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 146 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 147 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 148 | |
Jakob Stoklund Olesen | c338679 | 2013-02-05 18:21:52 +0000 | [diff] [blame] | 149 | // Examine the live-in regs of all successors. |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 150 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 151 | SE = BB->succ_end(); SI != SE; ++SI) |
| 152 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 153 | E = (*SI)->livein_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 154 | for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) { |
| 155 | unsigned Reg = *AI; |
Jakob Stoklund Olesen | be1c8d3 | 2010-12-14 23:23:15 +0000 | [diff] [blame] | 156 | State->UnionGroups(Reg, 0); |
| 157 | KillIndices[Reg] = BB->size(); |
| 158 | DefIndices[Reg] = ~0u; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 162 | // Mark live-out callee-saved registers. In a return block this is |
| 163 | // all callee-saved registers. In non-return this is any |
| 164 | // callee-saved register that is not saved in the prolog. |
| 165 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 166 | BitVector Pristine = MFI->getPristineRegs(BB); |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 167 | for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 168 | unsigned Reg = *I; |
| 169 | if (!IsReturnBlock && !Pristine.test(Reg)) continue; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 170 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
| 171 | unsigned AliasReg = *AI; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 172 | State->UnionGroups(AliasReg, 0); |
| 173 | KillIndices[AliasReg] = BB->size(); |
| 174 | DefIndices[AliasReg] = ~0u; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void AggressiveAntiDepBreaker::FinishBlock() { |
| 180 | delete State; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 181 | State = nullptr; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 185 | unsigned InsertPosIndex) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 186 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 187 | |
David Goodwin | faa7660 | 2009-10-29 23:30:59 +0000 | [diff] [blame] | 188 | std::set<unsigned> PassthruRegs; |
| 189 | GetPassthruRegs(MI, PassthruRegs); |
| 190 | PrescanInstruction(MI, Count, PassthruRegs); |
| 191 | ScanInstruction(MI, Count); |
| 192 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 193 | DEBUG(dbgs() << "Observe: "); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 194 | DEBUG(MI->dump()); |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 195 | DEBUG(dbgs() << "\tRegs:"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 196 | |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 197 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 198 | for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 199 | // If Reg is current live, then mark that it can't be renamed as |
| 200 | // we don't know the extent of its live-range anymore (now that it |
| 201 | // has been scheduled). If it is not live but was defined in the |
| 202 | // previous schedule region, then set its def index to the most |
| 203 | // conservative location (i.e. the beginning of the previous |
| 204 | // schedule region). |
| 205 | if (State->IsLive(Reg)) { |
| 206 | DEBUG(if (State->GetGroup(Reg) != 0) |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 207 | dbgs() << " " << TRI->getName(Reg) << "=g" << |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 208 | State->GetGroup(Reg) << "->g0(region live-out)"); |
| 209 | State->UnionGroups(Reg, 0); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 210 | } else if ((DefIndices[Reg] < InsertPosIndex) |
| 211 | && (DefIndices[Reg] >= Count)) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 212 | DefIndices[Reg] = Count; |
| 213 | } |
| 214 | } |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 215 | DEBUG(dbgs() << '\n'); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 216 | } |
| 217 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 218 | bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 219 | MachineOperand& MO) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 220 | { |
| 221 | if (!MO.isReg() || !MO.isImplicit()) |
| 222 | return false; |
| 223 | |
| 224 | unsigned Reg = MO.getReg(); |
| 225 | if (Reg == 0) |
| 226 | return false; |
| 227 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 228 | MachineOperand *Op = nullptr; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 229 | if (MO.isDef()) |
| 230 | Op = MI->findRegisterUseOperand(Reg, true); |
| 231 | else |
| 232 | Op = MI->findRegisterDefOperand(Reg); |
| 233 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 234 | return(Op && Op->isImplicit()); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI, |
| 238 | std::set<unsigned>& PassthruRegs) { |
| 239 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 240 | MachineOperand &MO = MI->getOperand(i); |
| 241 | if (!MO.isReg()) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 242 | if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) || |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 243 | IsImplicitDefUse(MI, MO)) { |
| 244 | const unsigned Reg = MO.getReg(); |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 245 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 246 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 247 | PassthruRegs.insert(*SubRegs); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 252 | /// AntiDepEdges - Return in Edges the anti- and output- dependencies |
| 253 | /// in SU that we want to consider for breaking. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 254 | static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 255 | SmallSet<unsigned, 4> RegSet; |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 256 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 257 | P != PE; ++P) { |
David Goodwin | da83f7d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 258 | if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame^] | 259 | if (RegSet.insert(P->getReg()).second) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 260 | Edges.push_back(&*P); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 265 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 266 | /// critical path. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 267 | static const SUnit *CriticalPathStep(const SUnit *SU) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 268 | const SDep *Next = nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 269 | unsigned NextDepth = 0; |
| 270 | // Find the predecessor edge with the greatest depth. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 271 | if (SU) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 272 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 273 | P != PE; ++P) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 274 | const SUnit *PredSU = P->getSUnit(); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 275 | unsigned PredLatency = P->getLatency(); |
| 276 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 277 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 278 | // other types of edges. |
| 279 | if (NextDepth < PredTotalLatency || |
| 280 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 281 | NextDepth = PredTotalLatency; |
| 282 | Next = &*P; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 287 | return (Next) ? Next->getSUnit() : nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 288 | } |
| 289 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 290 | void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx, |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 291 | const char *tag, |
| 292 | const char *header, |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 293 | const char *footer) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 294 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 295 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 296 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 297 | RegRefs = State->GetRegRefs(); |
| 298 | |
| 299 | if (!State->IsLive(Reg)) { |
| 300 | KillIndices[Reg] = KillIdx; |
| 301 | DefIndices[Reg] = ~0u; |
| 302 | RegRefs.erase(Reg); |
| 303 | State->LeaveGroup(Reg); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 304 | DEBUG(if (header) { |
| 305 | dbgs() << header << TRI->getName(Reg); header = nullptr; }); |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 306 | DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 307 | } |
| 308 | // Repeat for subregisters. |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 309 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 310 | unsigned SubregReg = *SubRegs; |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 311 | if (!State->IsLive(SubregReg)) { |
| 312 | KillIndices[SubregReg] = KillIdx; |
| 313 | DefIndices[SubregReg] = ~0u; |
| 314 | RegRefs.erase(SubregReg); |
| 315 | State->LeaveGroup(SubregReg); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 316 | DEBUG(if (header) { |
| 317 | dbgs() << header << TRI->getName(Reg); header = nullptr; }); |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 318 | DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" << |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 319 | State->GetGroup(SubregReg) << tag); |
| 320 | } |
| 321 | } |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 322 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 323 | DEBUG(if (!header && footer) dbgs() << footer); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 326 | void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI, |
| 327 | unsigned Count, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 328 | std::set<unsigned>& PassthruRegs) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 329 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 330 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 331 | RegRefs = State->GetRegRefs(); |
| 332 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 333 | // Handle dead defs by simulating a last-use of the register just |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 334 | // after the def. A dead def can occur because the def is truly |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 335 | // dead, or because only a subregister is live at the def. If we |
| 336 | // don't do this the dead def will be incorrectly merged into the |
| 337 | // previous def. |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 338 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 339 | MachineOperand &MO = MI->getOperand(i); |
| 340 | if (!MO.isReg() || !MO.isDef()) continue; |
| 341 | unsigned Reg = MO.getReg(); |
| 342 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 343 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 344 | HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 345 | } |
| 346 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 347 | DEBUG(dbgs() << "\tDef Groups:"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 348 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 349 | MachineOperand &MO = MI->getOperand(i); |
| 350 | if (!MO.isReg() || !MO.isDef()) continue; |
| 351 | unsigned Reg = MO.getReg(); |
| 352 | if (Reg == 0) continue; |
| 353 | |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 354 | DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 355 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 356 | // If MI's defs have a special allocation requirement, don't allow |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 357 | // any def registers to be changed. Also assume all registers |
| 358 | // defined in a call must not be changed (ABI). |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 359 | if (MI->isCall() || MI->hasExtraDefRegAllocReq() || |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 360 | TII->isPredicated(MI)) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 361 | DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 362 | State->UnionGroups(Reg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Any aliased that are live at this point are completely or |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 366 | // partially defined here, so group those aliases with Reg. |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 367 | for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) { |
| 368 | unsigned AliasReg = *AI; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 369 | if (State->IsLive(AliasReg)) { |
| 370 | State->UnionGroups(Reg, AliasReg); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 371 | DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " << |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 372 | TRI->getName(AliasReg) << ")"); |
| 373 | } |
| 374 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 375 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 376 | // Note register reference... |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 377 | const TargetRegisterClass *RC = nullptr; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 378 | if (i < MI->getDesc().getNumOperands()) |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 379 | RC = TII->getRegClass(MI->getDesc(), i, TRI, MF); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 380 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 381 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 382 | } |
| 383 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 384 | DEBUG(dbgs() << '\n'); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 385 | |
| 386 | // Scan the register defs for this instruction and update |
| 387 | // live-ranges. |
| 388 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 389 | MachineOperand &MO = MI->getOperand(i); |
| 390 | if (!MO.isReg() || !MO.isDef()) continue; |
| 391 | unsigned Reg = MO.getReg(); |
| 392 | if (Reg == 0) continue; |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 393 | // Ignore KILLs and passthru registers for liveness... |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 394 | if (MI->isKill() || (PassthruRegs.count(Reg) != 0)) |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 395 | continue; |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 396 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 397 | // Update def for Reg and aliases. |
Hal Finkel | 121caf6 | 2014-02-26 20:20:30 +0000 | [diff] [blame] | 398 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
| 399 | // We need to be careful here not to define already-live super registers. |
| 400 | // If the super register is already live, then this definition is not |
| 401 | // a definition of the whole super register (just a partial insertion |
| 402 | // into it). Earlier subregister definitions (which we've not yet visited |
| 403 | // because we're iterating bottom-up) need to be linked to the same group |
| 404 | // as this definition. |
| 405 | if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) |
| 406 | continue; |
| 407 | |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 408 | DefIndices[*AI] = Count; |
Hal Finkel | 121caf6 | 2014-02-26 20:20:30 +0000 | [diff] [blame] | 409 | } |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 410 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 414 | unsigned Count) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 415 | DEBUG(dbgs() << "\tUse Groups:"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 416 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 417 | RegRefs = State->GetRegRefs(); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 418 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 419 | // If MI's uses have special allocation requirement, don't allow |
| 420 | // any use registers to be changed. Also assume all registers |
| 421 | // used in a call must not be changed (ABI). |
| 422 | // FIXME: The issue with predicated instruction is more complex. We are being |
| 423 | // conservatively here because the kill markers cannot be trusted after |
| 424 | // if-conversion: |
| 425 | // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14] |
| 426 | // ... |
| 427 | // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395] |
| 428 | // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12] |
| 429 | // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8) |
| 430 | // |
| 431 | // The first R6 kill is not really a kill since it's killed by a predicated |
| 432 | // instruction which may not be executed. The second R6 def may or may not |
| 433 | // re-define R6 so it's not safe to change it since the last R6 use cannot be |
| 434 | // changed. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 435 | bool Special = MI->isCall() || |
| 436 | MI->hasExtraSrcRegAllocReq() || |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 437 | TII->isPredicated(MI); |
| 438 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 439 | // Scan the register uses for this instruction and update |
| 440 | // live-ranges, groups and RegRefs. |
| 441 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 442 | MachineOperand &MO = MI->getOperand(i); |
| 443 | if (!MO.isReg() || !MO.isUse()) continue; |
| 444 | unsigned Reg = MO.getReg(); |
| 445 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 446 | |
| 447 | DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << |
| 448 | State->GetGroup(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 449 | |
| 450 | // It wasn't previously live but now it is, this is a kill. Forget |
| 451 | // the previous live-range information and start a new live-range |
| 452 | // for the register. |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 453 | HandleLastUse(Reg, Count, "(last-use)"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 454 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 455 | if (Special) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 456 | DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 457 | State->UnionGroups(Reg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Note register reference... |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 461 | const TargetRegisterClass *RC = nullptr; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 462 | if (i < MI->getDesc().getNumOperands()) |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 463 | RC = TII->getRegClass(MI->getDesc(), i, TRI, MF); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 464 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 465 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 466 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 467 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 468 | DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 469 | |
| 470 | // Form a group of all defs and uses of a KILL instruction to ensure |
| 471 | // that all registers are renamed as a group. |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 472 | if (MI->isKill()) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 473 | DEBUG(dbgs() << "\tKill Group:"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 474 | |
| 475 | unsigned FirstReg = 0; |
| 476 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 477 | MachineOperand &MO = MI->getOperand(i); |
| 478 | if (!MO.isReg()) continue; |
| 479 | unsigned Reg = MO.getReg(); |
| 480 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 481 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 482 | if (FirstReg != 0) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 483 | DEBUG(dbgs() << "=" << TRI->getName(Reg)); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 484 | State->UnionGroups(FirstReg, Reg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 485 | } else { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 486 | DEBUG(dbgs() << " " << TRI->getName(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 487 | FirstReg = Reg; |
| 488 | } |
| 489 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 490 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 491 | DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) { |
| 496 | BitVector BV(TRI->getNumRegs(), false); |
| 497 | bool first = true; |
| 498 | |
| 499 | // Check all references that need rewriting for Reg. For each, use |
| 500 | // the corresponding register class to narrow the set of registers |
| 501 | // that are appropriate for renaming. |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 502 | std::pair<std::multimap<unsigned, |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 503 | AggressiveAntiDepState::RegisterReference>::iterator, |
| 504 | std::multimap<unsigned, |
| 505 | AggressiveAntiDepState::RegisterReference>::iterator> |
| 506 | Range = State->GetRegRefs().equal_range(Reg); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 507 | for (std::multimap<unsigned, |
| 508 | AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first, |
| 509 | QE = Range.second; Q != QE; ++Q) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 510 | const TargetRegisterClass *RC = Q->second.RC; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 511 | if (!RC) continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 512 | |
| 513 | BitVector RCBV = TRI->getAllocatableSet(MF, RC); |
| 514 | if (first) { |
| 515 | BV |= RCBV; |
| 516 | first = false; |
| 517 | } else { |
| 518 | BV &= RCBV; |
| 519 | } |
| 520 | |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 521 | DEBUG(dbgs() << " " << TRI->getRegClassName(RC)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 522 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 523 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 524 | return BV; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 525 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 526 | |
| 527 | bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters( |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 528 | unsigned AntiDepGroupIndex, |
| 529 | RenameOrderType& RenameOrder, |
| 530 | std::map<unsigned, unsigned> &RenameMap) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 531 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 532 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 533 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 534 | RegRefs = State->GetRegRefs(); |
| 535 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 536 | // Collect all referenced registers in the same group as |
| 537 | // AntiDepReg. These all need to be renamed together if we are to |
| 538 | // break the anti-dependence. |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 539 | std::vector<unsigned> Regs; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 540 | State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 541 | assert(Regs.size() > 0 && "Empty register group!"); |
| 542 | if (Regs.size() == 0) |
| 543 | return false; |
| 544 | |
| 545 | // Find the "superest" register in the group. At the same time, |
| 546 | // collect the BitVector of registers that can be used to rename |
| 547 | // each register. |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 548 | DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex |
| 549 | << ":\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 550 | std::map<unsigned, BitVector> RenameRegisterMap; |
| 551 | unsigned SuperReg = 0; |
| 552 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 553 | unsigned Reg = Regs[i]; |
| 554 | if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg)) |
| 555 | SuperReg = Reg; |
| 556 | |
| 557 | // If Reg has any references, then collect possible rename regs |
| 558 | if (RegRefs.count(Reg) > 0) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 559 | DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 560 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 561 | BitVector BV = GetRenameRegisters(Reg); |
| 562 | RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV)); |
| 563 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 564 | DEBUG(dbgs() << " ::"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 565 | DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r)) |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 566 | dbgs() << " " << TRI->getName(r)); |
| 567 | DEBUG(dbgs() << "\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | // All group registers should be a subreg of SuperReg. |
| 572 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 573 | unsigned Reg = Regs[i]; |
| 574 | if (Reg == SuperReg) continue; |
| 575 | bool IsSub = TRI->isSubRegister(SuperReg, Reg); |
Will Schmidt | 44ff8f0 | 2014-07-31 19:50:53 +0000 | [diff] [blame] | 576 | // FIXME: remove this once PR18663 has been properly fixed. For now, |
| 577 | // return a conservative answer: |
| 578 | // assert(IsSub && "Expecting group subregister"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 579 | if (!IsSub) |
| 580 | return false; |
| 581 | } |
| 582 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 583 | #ifndef NDEBUG |
| 584 | // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod |
| 585 | if (DebugDiv > 0) { |
| 586 | static int renamecnt = 0; |
| 587 | if (renamecnt++ % DebugDiv != DebugMod) |
| 588 | return false; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 589 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 590 | dbgs() << "*** Performing rename " << TRI->getName(SuperReg) << |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 591 | " for debug ***\n"; |
| 592 | } |
| 593 | #endif |
| 594 | |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 595 | // Check each possible rename register for SuperReg in round-robin |
| 596 | // order. If that register is available, and the corresponding |
| 597 | // registers are available for the other group subregisters, then we |
| 598 | // can use those registers to rename. |
Rafael Espindola | 871c724 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 599 | |
| 600 | // FIXME: Using getMinimalPhysRegClass is very conservative. We should |
| 601 | // check every use of the register and find the largest register class |
| 602 | // that can be used in all of them. |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 603 | const TargetRegisterClass *SuperRC = |
Rafael Espindola | 871c724 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 604 | TRI->getMinimalPhysRegClass(SuperReg, MVT::Other); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 605 | |
Jakob Stoklund Olesen | bdb55e0 | 2012-11-29 03:34:17 +0000 | [diff] [blame] | 606 | ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC); |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 607 | if (Order.empty()) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 608 | DEBUG(dbgs() << "\tEmpty Super Regclass!!\n"); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 609 | return false; |
| 610 | } |
| 611 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 612 | DEBUG(dbgs() << "\tFind Registers:"); |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 613 | |
Benjamin Kramer | 2c99e41 | 2014-10-10 15:32:50 +0000 | [diff] [blame] | 614 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size())); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 615 | |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 616 | unsigned OrigR = RenameOrder[SuperRC]; |
| 617 | unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR); |
| 618 | unsigned R = OrigR; |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 619 | do { |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 620 | if (R == 0) R = Order.size(); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 621 | --R; |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 622 | const unsigned NewSuperReg = Order[R]; |
Jim Grosbach | 944aece | 2010-09-02 17:12:55 +0000 | [diff] [blame] | 623 | // Don't consider non-allocatable registers |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 624 | if (!MRI.isAllocatable(NewSuperReg)) continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 625 | // Don't replace a register with itself. |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 626 | if (NewSuperReg == SuperReg) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 627 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 628 | DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':'); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 629 | RenameMap.clear(); |
| 630 | |
| 631 | // For each referenced group register (which must be a SuperReg or |
| 632 | // a subregister of SuperReg), find the corresponding subregister |
| 633 | // of NewSuperReg and make sure it is free to be renamed. |
| 634 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 635 | unsigned Reg = Regs[i]; |
| 636 | unsigned NewReg = 0; |
| 637 | if (Reg == SuperReg) { |
| 638 | NewReg = NewSuperReg; |
| 639 | } else { |
| 640 | unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg); |
| 641 | if (NewSubRegIdx != 0) |
| 642 | NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 643 | } |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 644 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 645 | DEBUG(dbgs() << " " << TRI->getName(NewReg)); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 646 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 647 | // Check if Reg can be renamed to NewReg. |
| 648 | BitVector BV = RenameRegisterMap[Reg]; |
| 649 | if (!BV.test(NewReg)) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 650 | DEBUG(dbgs() << "(no rename)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 651 | goto next_super_reg; |
| 652 | } |
| 653 | |
| 654 | // If NewReg is dead and NewReg's most recent def is not before |
| 655 | // Regs's kill, it's safe to replace Reg with NewReg. We |
| 656 | // must also check all aliases of NewReg, because we can't define a |
| 657 | // register when any sub or super is already live. |
| 658 | if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 659 | DEBUG(dbgs() << "(live)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 660 | goto next_super_reg; |
| 661 | } else { |
| 662 | bool found = false; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 663 | for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) { |
| 664 | unsigned AliasReg = *AI; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 665 | if (State->IsLive(AliasReg) || |
| 666 | (KillIndices[Reg] > DefIndices[AliasReg])) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 667 | DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 668 | found = true; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | if (found) |
| 673 | goto next_super_reg; |
| 674 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 675 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 676 | // Record that 'Reg' can be renamed to 'NewReg'. |
| 677 | RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 678 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 679 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 680 | // If we fall-out here, then every register in the group can be |
| 681 | // renamed, as recorded in RenameMap. |
| 682 | RenameOrder.erase(SuperRC); |
| 683 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, R)); |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 684 | DEBUG(dbgs() << "]\n"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 685 | return true; |
| 686 | |
| 687 | next_super_reg: |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 688 | DEBUG(dbgs() << ']'); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 689 | } while (R != EndR); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 690 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 691 | DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 692 | |
| 693 | // No registers are free and available! |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | /// BreakAntiDependencies - Identifiy anti-dependencies within the |
| 698 | /// ScheduleDAG and break them by renaming registers. |
| 699 | /// |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 700 | unsigned AggressiveAntiDepBreaker::BreakAntiDependencies( |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 701 | const std::vector<SUnit>& SUnits, |
| 702 | MachineBasicBlock::iterator Begin, |
| 703 | MachineBasicBlock::iterator End, |
Devang Patel | f02a376 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 704 | unsigned InsertPosIndex, |
| 705 | DbgValueVector &DbgValues) { |
| 706 | |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 707 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 708 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 709 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 710 | RegRefs = State->GetRegRefs(); |
| 711 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 712 | // The code below assumes that there is at least one instruction, |
| 713 | // so just duck out immediately if the block is empty. |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 714 | if (SUnits.empty()) return 0; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 715 | |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 716 | // For each regclass the next register to use for renaming. |
| 717 | RenameOrderType RenameOrder; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 718 | |
| 719 | // ...need a map from MI to SUnit. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 720 | std::map<MachineInstr *, const SUnit *> MISUnitMap; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 721 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 722 | const SUnit *SU = &SUnits[i]; |
| 723 | MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(), |
| 724 | SU)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 725 | } |
| 726 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 727 | // Track progress along the critical path through the SUnit graph as |
| 728 | // we walk the instructions. This is needed for regclasses that only |
| 729 | // break critical-path anti-dependencies. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 730 | const SUnit *CriticalPathSU = nullptr; |
| 731 | MachineInstr *CriticalPathMI = nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 732 | if (CriticalPathSet.any()) { |
| 733 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 734 | const SUnit *SU = &SUnits[i]; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 735 | if (!CriticalPathSU || |
| 736 | ((SU->getDepth() + SU->Latency) > |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 737 | (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) { |
| 738 | CriticalPathSU = SU; |
| 739 | } |
| 740 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 741 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 742 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 743 | } |
| 744 | |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 745 | #ifndef NDEBUG |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 746 | DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n"); |
| 747 | DEBUG(dbgs() << "Available regs:"); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 748 | for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { |
| 749 | if (!State->IsLive(Reg)) |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 750 | DEBUG(dbgs() << " " << TRI->getName(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 751 | } |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 752 | DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 753 | #endif |
| 754 | |
| 755 | // Attempt to break anti-dependence edges. Walk the instructions |
| 756 | // from the bottom up, tracking information about liveness as we go |
| 757 | // to help determine which registers are available. |
| 758 | unsigned Broken = 0; |
| 759 | unsigned Count = InsertPosIndex - 1; |
| 760 | for (MachineBasicBlock::iterator I = End, E = Begin; |
| 761 | I != E; --Count) { |
| 762 | MachineInstr *MI = --I; |
| 763 | |
Hal Finkel | 8606e3c | 2012-01-16 22:53:41 +0000 | [diff] [blame] | 764 | if (MI->isDebugValue()) |
| 765 | continue; |
| 766 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 767 | DEBUG(dbgs() << "Anti: "); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 768 | DEBUG(MI->dump()); |
| 769 | |
| 770 | std::set<unsigned> PassthruRegs; |
| 771 | GetPassthruRegs(MI, PassthruRegs); |
| 772 | |
| 773 | // Process the defs in MI... |
| 774 | PrescanInstruction(MI, Count, PassthruRegs); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 775 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 776 | // The dependence edges that represent anti- and output- |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 777 | // dependencies that are candidates for breaking. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 778 | std::vector<const SDep *> Edges; |
| 779 | const SUnit *PathSU = MISUnitMap[MI]; |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 780 | AntiDepEdges(PathSU, Edges); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 781 | |
| 782 | // If MI is not on the critical path, then we don't rename |
| 783 | // registers in the CriticalPathSet. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 784 | BitVector *ExcludeRegs = nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 785 | if (MI == CriticalPathMI) { |
| 786 | CriticalPathSU = CriticalPathStep(CriticalPathSU); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 787 | CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr; |
Hal Finkel | 6f1ff8e | 2013-09-12 04:22:31 +0000 | [diff] [blame] | 788 | } else if (CriticalPathSet.any()) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 789 | ExcludeRegs = &CriticalPathSet; |
| 790 | } |
| 791 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 792 | // Ignore KILL instructions (they form a group in ScanInstruction |
| 793 | // but don't cause any anti-dependence breaking themselves) |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 794 | if (!MI->isKill()) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 795 | // Attempt to break each anti-dependency... |
| 796 | for (unsigned i = 0, e = Edges.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 797 | const SDep *Edge = Edges[i]; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 798 | SUnit *NextSU = Edge->getSUnit(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 799 | |
David Goodwin | da83f7d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 800 | if ((Edge->getKind() != SDep::Anti) && |
| 801 | (Edge->getKind() != SDep::Output)) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 802 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 803 | unsigned AntiDepReg = Edge->getReg(); |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 804 | DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 805 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 806 | |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 807 | if (!MRI.isAllocatable(AntiDepReg)) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 808 | // Don't break anti-dependencies on non-allocatable registers. |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 809 | DEBUG(dbgs() << " (non-allocatable)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 810 | continue; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 811 | } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 812 | // Don't break anti-dependencies for critical path registers |
| 813 | // if not on the critical path |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 814 | DEBUG(dbgs() << " (not critical-path)\n"); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 815 | continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 816 | } else if (PassthruRegs.count(AntiDepReg) != 0) { |
| 817 | // If the anti-dep register liveness "passes-thru", then |
| 818 | // don't try to change it. It will be changed along with |
| 819 | // the use if required to break an earlier antidep. |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 820 | DEBUG(dbgs() << " (passthru)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 821 | continue; |
| 822 | } else { |
| 823 | // No anti-dep breaking for implicit deps |
| 824 | MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 825 | assert(AntiDepOp && "Can't find index for defined register operand"); |
| 826 | if (!AntiDepOp || AntiDepOp->isImplicit()) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 827 | DEBUG(dbgs() << " (implicit)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 828 | continue; |
| 829 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 830 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 831 | // If the SUnit has other dependencies on the SUnit that |
| 832 | // it anti-depends on, don't bother breaking the |
| 833 | // anti-dependency since those edges would prevent such |
| 834 | // units from being scheduled past each other |
| 835 | // regardless. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 836 | // |
| 837 | // Also, if there are dependencies on other SUnits with the |
| 838 | // same register as the anti-dependency, don't attempt to |
| 839 | // break it. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 840 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 841 | PE = PathSU->Preds.end(); P != PE; ++P) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 842 | if (P->getSUnit() == NextSU ? |
| 843 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 844 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
| 845 | AntiDepReg = 0; |
| 846 | break; |
| 847 | } |
| 848 | } |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 849 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 850 | PE = PathSU->Preds.end(); P != PE; ++P) { |
| 851 | if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) && |
| 852 | (P->getKind() != SDep::Output)) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 853 | DEBUG(dbgs() << " (real dependency)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 854 | AntiDepReg = 0; |
| 855 | break; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 856 | } else if ((P->getSUnit() != NextSU) && |
| 857 | (P->getKind() == SDep::Data) && |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 858 | (P->getReg() == AntiDepReg)) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 859 | DEBUG(dbgs() << " (other dependency)\n"); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 860 | AntiDepReg = 0; |
| 861 | break; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 864 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 865 | if (AntiDepReg == 0) continue; |
| 866 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 867 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 868 | assert(AntiDepReg != 0); |
| 869 | if (AntiDepReg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 870 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 871 | // Determine AntiDepReg's register group. |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 872 | const unsigned GroupIndex = State->GetGroup(AntiDepReg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 873 | if (GroupIndex == 0) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 874 | DEBUG(dbgs() << " (zero group)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 875 | continue; |
| 876 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 877 | |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 878 | DEBUG(dbgs() << '\n'); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 879 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 880 | // Look for a suitable register to use to break the anti-dependence. |
| 881 | std::map<unsigned, unsigned> RenameMap; |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 882 | if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) { |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 883 | DEBUG(dbgs() << "\tBreaking anti-dependence edge on " |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 884 | << TRI->getName(AntiDepReg) << ":"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 885 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 886 | // Handle each group register... |
| 887 | for (std::map<unsigned, unsigned>::iterator |
| 888 | S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) { |
| 889 | unsigned CurrReg = S->first; |
| 890 | unsigned NewReg = S->second; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 891 | |
| 892 | DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" << |
| 893 | TRI->getName(NewReg) << "(" << |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 894 | RegRefs.count(CurrReg) << " refs)"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 895 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 896 | // Update the references to the old register CurrReg to |
| 897 | // refer to the new register NewReg. |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 898 | std::pair<std::multimap<unsigned, |
| 899 | AggressiveAntiDepState::RegisterReference>::iterator, |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 900 | std::multimap<unsigned, |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 901 | AggressiveAntiDepState::RegisterReference>::iterator> |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 902 | Range = RegRefs.equal_range(CurrReg); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 903 | for (std::multimap<unsigned, |
| 904 | AggressiveAntiDepState::RegisterReference>::iterator |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 905 | Q = Range.first, QE = Range.second; Q != QE; ++Q) { |
| 906 | Q->second.Operand->setReg(NewReg); |
Jim Grosbach | 12ac8f0 | 2010-06-01 23:48:44 +0000 | [diff] [blame] | 907 | // If the SU for the instruction being updated has debug |
| 908 | // information related to the anti-dependency register, make |
| 909 | // sure to update that as well. |
| 910 | const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()]; |
Jim Grosbach | 8485483 | 2010-06-02 15:29:36 +0000 | [diff] [blame] | 911 | if (!SU) continue; |
Devang Patel | f02a376 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 912 | for (DbgValueVector::iterator DVI = DbgValues.begin(), |
| 913 | DVE = DbgValues.end(); DVI != DVE; ++DVI) |
| 914 | if (DVI->second == Q->second.Operand->getParent()) |
| 915 | UpdateDbgValue(DVI->first, AntiDepReg, NewReg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 916 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 917 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 918 | // We just went back in time and modified history; the |
| 919 | // liveness information for CurrReg is now inconsistent. Set |
| 920 | // the state as if it were dead. |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 921 | State->UnionGroups(NewReg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 922 | RegRefs.erase(NewReg); |
| 923 | DefIndices[NewReg] = DefIndices[CurrReg]; |
| 924 | KillIndices[NewReg] = KillIndices[CurrReg]; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 925 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 926 | State->UnionGroups(CurrReg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 927 | RegRefs.erase(CurrReg); |
| 928 | DefIndices[CurrReg] = KillIndices[CurrReg]; |
| 929 | KillIndices[CurrReg] = ~0u; |
| 930 | assert(((KillIndices[CurrReg] == ~0u) != |
| 931 | (DefIndices[CurrReg] == ~0u)) && |
| 932 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 933 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 934 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 935 | ++Broken; |
David Greene | 75a2efb | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 936 | DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | ScanInstruction(MI, Count); |
| 942 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 943 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 944 | return Broken; |
| 945 | } |