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