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