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