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