LLVM API Documentation
00001 //===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the AggressiveAntiDepBreaker class, which 00011 // implements register anti-dependence breaking during post-RA 00012 // scheduling. It attempts to break all anti-dependencies within a 00013 // block. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "post-RA-sched" 00018 #include "AggressiveAntiDepBreaker.h" 00019 #include "llvm/CodeGen/MachineBasicBlock.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineInstr.h" 00022 #include "llvm/CodeGen/RegisterClassInfo.h" 00023 #include "llvm/Support/CommandLine.h" 00024 #include "llvm/Support/Debug.h" 00025 #include "llvm/Support/ErrorHandling.h" 00026 #include "llvm/Support/raw_ostream.h" 00027 #include "llvm/Target/TargetInstrInfo.h" 00028 #include "llvm/Target/TargetMachine.h" 00029 #include "llvm/Target/TargetRegisterInfo.h" 00030 using namespace llvm; 00031 00032 // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod 00033 static cl::opt<int> 00034 DebugDiv("agg-antidep-debugdiv", 00035 cl::desc("Debug control for aggressive anti-dep breaker"), 00036 cl::init(0), cl::Hidden); 00037 static cl::opt<int> 00038 DebugMod("agg-antidep-debugmod", 00039 cl::desc("Debug control for aggressive anti-dep breaker"), 00040 cl::init(0), cl::Hidden); 00041 00042 AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs, 00043 MachineBasicBlock *BB) : 00044 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0), 00045 GroupNodeIndices(TargetRegs, 0), 00046 KillIndices(TargetRegs, 0), 00047 DefIndices(TargetRegs, 0) 00048 { 00049 const unsigned BBSize = BB->size(); 00050 for (unsigned i = 0; i < NumTargetRegs; ++i) { 00051 // Initialize all registers to be in their own group. Initially we 00052 // assign the register to the same-indexed GroupNode. 00053 GroupNodeIndices[i] = i; 00054 // Initialize the indices to indicate that no registers are live. 00055 KillIndices[i] = ~0u; 00056 DefIndices[i] = BBSize; 00057 } 00058 } 00059 00060 unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) { 00061 unsigned Node = GroupNodeIndices[Reg]; 00062 while (GroupNodes[Node] != Node) 00063 Node = GroupNodes[Node]; 00064 00065 return Node; 00066 } 00067 00068 void AggressiveAntiDepState::GetGroupRegs( 00069 unsigned Group, 00070 std::vector<unsigned> &Regs, 00071 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs) 00072 { 00073 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) { 00074 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0)) 00075 Regs.push_back(Reg); 00076 } 00077 } 00078 00079 unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) 00080 { 00081 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!"); 00082 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!"); 00083 00084 // find group for each register 00085 unsigned Group1 = GetGroup(Reg1); 00086 unsigned Group2 = GetGroup(Reg2); 00087 00088 // if either group is 0, then that must become the parent 00089 unsigned Parent = (Group1 == 0) ? Group1 : Group2; 00090 unsigned Other = (Parent == Group1) ? Group2 : Group1; 00091 GroupNodes.at(Other) = Parent; 00092 return Parent; 00093 } 00094 00095 unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) 00096 { 00097 // Create a new GroupNode for Reg. Reg's existing GroupNode must 00098 // stay as is because there could be other GroupNodes referring to 00099 // it. 00100 unsigned idx = GroupNodes.size(); 00101 GroupNodes.push_back(idx); 00102 GroupNodeIndices[Reg] = idx; 00103 return idx; 00104 } 00105 00106 bool AggressiveAntiDepState::IsLive(unsigned Reg) 00107 { 00108 // KillIndex must be defined and DefIndex not defined for a register 00109 // to be live. 00110 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u)); 00111 } 00112 00113 00114 00115 AggressiveAntiDepBreaker:: 00116 AggressiveAntiDepBreaker(MachineFunction& MFi, 00117 const RegisterClassInfo &RCI, 00118 TargetSubtargetInfo::RegClassVector& CriticalPathRCs) : 00119 AntiDepBreaker(), MF(MFi), 00120 MRI(MF.getRegInfo()), 00121 TII(MF.getTarget().getInstrInfo()), 00122 TRI(MF.getTarget().getRegisterInfo()), 00123 RegClassInfo(RCI), 00124 State(NULL) { 00125 /* Collect a bitset of all registers that are only broken if they 00126 are on the critical path. */ 00127 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) { 00128 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]); 00129 if (CriticalPathSet.none()) 00130 CriticalPathSet = CPSet; 00131 else 00132 CriticalPathSet |= CPSet; 00133 } 00134 00135 DEBUG(dbgs() << "AntiDep Critical-Path Registers:"); 00136 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1; 00137 r = CriticalPathSet.find_next(r)) 00138 dbgs() << " " << TRI->getName(r)); 00139 DEBUG(dbgs() << '\n'); 00140 } 00141 00142 AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() { 00143 delete State; 00144 } 00145 00146 void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) { 00147 assert(State == NULL); 00148 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB); 00149 00150 bool IsReturnBlock = (!BB->empty() && BB->back().isReturn()); 00151 std::vector<unsigned> &KillIndices = State->GetKillIndices(); 00152 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00153 00154 // Examine the live-in regs of all successors. 00155 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 00156 SE = BB->succ_end(); SI != SE; ++SI) 00157 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), 00158 E = (*SI)->livein_end(); I != E; ++I) { 00159 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) { 00160 unsigned Reg = *AI; 00161 State->UnionGroups(Reg, 0); 00162 KillIndices[Reg] = BB->size(); 00163 DefIndices[Reg] = ~0u; 00164 } 00165 } 00166 00167 // Mark live-out callee-saved registers. In a return block this is 00168 // all callee-saved registers. In non-return this is any 00169 // callee-saved register that is not saved in the prolog. 00170 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00171 BitVector Pristine = MFI->getPristineRegs(BB); 00172 for (const uint16_t *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) { 00173 unsigned Reg = *I; 00174 if (!IsReturnBlock && !Pristine.test(Reg)) continue; 00175 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 00176 unsigned AliasReg = *AI; 00177 State->UnionGroups(AliasReg, 0); 00178 KillIndices[AliasReg] = BB->size(); 00179 DefIndices[AliasReg] = ~0u; 00180 } 00181 } 00182 } 00183 00184 void AggressiveAntiDepBreaker::FinishBlock() { 00185 delete State; 00186 State = NULL; 00187 } 00188 00189 void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count, 00190 unsigned InsertPosIndex) { 00191 assert(Count < InsertPosIndex && "Instruction index out of expected range!"); 00192 00193 std::set<unsigned> PassthruRegs; 00194 GetPassthruRegs(MI, PassthruRegs); 00195 PrescanInstruction(MI, Count, PassthruRegs); 00196 ScanInstruction(MI, Count); 00197 00198 DEBUG(dbgs() << "Observe: "); 00199 DEBUG(MI->dump()); 00200 DEBUG(dbgs() << "\tRegs:"); 00201 00202 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00203 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) { 00204 // If Reg is current live, then mark that it can't be renamed as 00205 // we don't know the extent of its live-range anymore (now that it 00206 // has been scheduled). If it is not live but was defined in the 00207 // previous schedule region, then set its def index to the most 00208 // conservative location (i.e. the beginning of the previous 00209 // schedule region). 00210 if (State->IsLive(Reg)) { 00211 DEBUG(if (State->GetGroup(Reg) != 0) 00212 dbgs() << " " << TRI->getName(Reg) << "=g" << 00213 State->GetGroup(Reg) << "->g0(region live-out)"); 00214 State->UnionGroups(Reg, 0); 00215 } else if ((DefIndices[Reg] < InsertPosIndex) 00216 && (DefIndices[Reg] >= Count)) { 00217 DefIndices[Reg] = Count; 00218 } 00219 } 00220 DEBUG(dbgs() << '\n'); 00221 } 00222 00223 bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI, 00224 MachineOperand& MO) 00225 { 00226 if (!MO.isReg() || !MO.isImplicit()) 00227 return false; 00228 00229 unsigned Reg = MO.getReg(); 00230 if (Reg == 0) 00231 return false; 00232 00233 MachineOperand *Op = NULL; 00234 if (MO.isDef()) 00235 Op = MI->findRegisterUseOperand(Reg, true); 00236 else 00237 Op = MI->findRegisterDefOperand(Reg); 00238 00239 return((Op != NULL) && Op->isImplicit()); 00240 } 00241 00242 void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI, 00243 std::set<unsigned>& PassthruRegs) { 00244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00245 MachineOperand &MO = MI->getOperand(i); 00246 if (!MO.isReg()) continue; 00247 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) || 00248 IsImplicitDefUse(MI, MO)) { 00249 const unsigned Reg = MO.getReg(); 00250 PassthruRegs.insert(Reg); 00251 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) 00252 PassthruRegs.insert(*SubRegs); 00253 } 00254 } 00255 } 00256 00257 /// AntiDepEdges - Return in Edges the anti- and output- dependencies 00258 /// in SU that we want to consider for breaking. 00259 static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) { 00260 SmallSet<unsigned, 4> RegSet; 00261 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); 00262 P != PE; ++P) { 00263 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) { 00264 unsigned Reg = P->getReg(); 00265 if (RegSet.count(Reg) == 0) { 00266 Edges.push_back(&*P); 00267 RegSet.insert(Reg); 00268 } 00269 } 00270 } 00271 } 00272 00273 /// CriticalPathStep - Return the next SUnit after SU on the bottom-up 00274 /// critical path. 00275 static const SUnit *CriticalPathStep(const SUnit *SU) { 00276 const SDep *Next = 0; 00277 unsigned NextDepth = 0; 00278 // Find the predecessor edge with the greatest depth. 00279 if (SU != 0) { 00280 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); 00281 P != PE; ++P) { 00282 const SUnit *PredSU = P->getSUnit(); 00283 unsigned PredLatency = P->getLatency(); 00284 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; 00285 // In the case of a latency tie, prefer an anti-dependency edge over 00286 // other types of edges. 00287 if (NextDepth < PredTotalLatency || 00288 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { 00289 NextDepth = PredTotalLatency; 00290 Next = &*P; 00291 } 00292 } 00293 } 00294 00295 return (Next) ? Next->getSUnit() : 0; 00296 } 00297 00298 void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx, 00299 const char *tag, 00300 const char *header, 00301 const char *footer) { 00302 std::vector<unsigned> &KillIndices = State->GetKillIndices(); 00303 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00304 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& 00305 RegRefs = State->GetRegRefs(); 00306 00307 if (!State->IsLive(Reg)) { 00308 KillIndices[Reg] = KillIdx; 00309 DefIndices[Reg] = ~0u; 00310 RegRefs.erase(Reg); 00311 State->LeaveGroup(Reg); 00312 DEBUG(if (header != NULL) { 00313 dbgs() << header << TRI->getName(Reg); header = NULL; }); 00314 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag); 00315 } 00316 // Repeat for subregisters. 00317 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { 00318 unsigned SubregReg = *SubRegs; 00319 if (!State->IsLive(SubregReg)) { 00320 KillIndices[SubregReg] = KillIdx; 00321 DefIndices[SubregReg] = ~0u; 00322 RegRefs.erase(SubregReg); 00323 State->LeaveGroup(SubregReg); 00324 DEBUG(if (header != NULL) { 00325 dbgs() << header << TRI->getName(Reg); header = NULL; }); 00326 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" << 00327 State->GetGroup(SubregReg) << tag); 00328 } 00329 } 00330 00331 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer); 00332 } 00333 00334 void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI, 00335 unsigned Count, 00336 std::set<unsigned>& PassthruRegs) { 00337 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00338 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& 00339 RegRefs = State->GetRegRefs(); 00340 00341 // Handle dead defs by simulating a last-use of the register just 00342 // after the def. A dead def can occur because the def is truly 00343 // dead, or because only a subregister is live at the def. If we 00344 // don't do this the dead def will be incorrectly merged into the 00345 // previous def. 00346 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00347 MachineOperand &MO = MI->getOperand(i); 00348 if (!MO.isReg() || !MO.isDef()) continue; 00349 unsigned Reg = MO.getReg(); 00350 if (Reg == 0) continue; 00351 00352 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n"); 00353 } 00354 00355 DEBUG(dbgs() << "\tDef Groups:"); 00356 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00357 MachineOperand &MO = MI->getOperand(i); 00358 if (!MO.isReg() || !MO.isDef()) continue; 00359 unsigned Reg = MO.getReg(); 00360 if (Reg == 0) continue; 00361 00362 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg)); 00363 00364 // If MI's defs have a special allocation requirement, don't allow 00365 // any def registers to be changed. Also assume all registers 00366 // defined in a call must not be changed (ABI). 00367 if (MI->isCall() || MI->hasExtraDefRegAllocReq() || 00368 TII->isPredicated(MI)) { 00369 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); 00370 State->UnionGroups(Reg, 0); 00371 } 00372 00373 // Any aliased that are live at this point are completely or 00374 // partially defined here, so group those aliases with Reg. 00375 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) { 00376 unsigned AliasReg = *AI; 00377 if (State->IsLive(AliasReg)) { 00378 State->UnionGroups(Reg, AliasReg); 00379 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " << 00380 TRI->getName(AliasReg) << ")"); 00381 } 00382 } 00383 00384 // Note register reference... 00385 const TargetRegisterClass *RC = NULL; 00386 if (i < MI->getDesc().getNumOperands()) 00387 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF); 00388 AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; 00389 RegRefs.insert(std::make_pair(Reg, RR)); 00390 } 00391 00392 DEBUG(dbgs() << '\n'); 00393 00394 // Scan the register defs for this instruction and update 00395 // live-ranges. 00396 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00397 MachineOperand &MO = MI->getOperand(i); 00398 if (!MO.isReg() || !MO.isDef()) continue; 00399 unsigned Reg = MO.getReg(); 00400 if (Reg == 0) continue; 00401 // Ignore KILLs and passthru registers for liveness... 00402 if (MI->isKill() || (PassthruRegs.count(Reg) != 0)) 00403 continue; 00404 00405 // Update def for Reg and aliases. 00406 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 00407 DefIndices[*AI] = Count; 00408 } 00409 } 00410 00411 void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI, 00412 unsigned Count) { 00413 DEBUG(dbgs() << "\tUse Groups:"); 00414 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& 00415 RegRefs = State->GetRegRefs(); 00416 00417 // If MI's uses have special allocation requirement, don't allow 00418 // any use registers to be changed. Also assume all registers 00419 // used in a call must not be changed (ABI). 00420 // FIXME: The issue with predicated instruction is more complex. We are being 00421 // conservatively here because the kill markers cannot be trusted after 00422 // if-conversion: 00423 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14] 00424 // ... 00425 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395] 00426 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12] 00427 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8) 00428 // 00429 // The first R6 kill is not really a kill since it's killed by a predicated 00430 // instruction which may not be executed. The second R6 def may or may not 00431 // re-define R6 so it's not safe to change it since the last R6 use cannot be 00432 // changed. 00433 bool Special = MI->isCall() || 00434 MI->hasExtraSrcRegAllocReq() || 00435 TII->isPredicated(MI); 00436 00437 // Scan the register uses for this instruction and update 00438 // live-ranges, groups and RegRefs. 00439 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00440 MachineOperand &MO = MI->getOperand(i); 00441 if (!MO.isReg() || !MO.isUse()) continue; 00442 unsigned Reg = MO.getReg(); 00443 if (Reg == 0) continue; 00444 00445 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << 00446 State->GetGroup(Reg)); 00447 00448 // It wasn't previously live but now it is, this is a kill. Forget 00449 // the previous live-range information and start a new live-range 00450 // for the register. 00451 HandleLastUse(Reg, Count, "(last-use)"); 00452 00453 if (Special) { 00454 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); 00455 State->UnionGroups(Reg, 0); 00456 } 00457 00458 // Note register reference... 00459 const TargetRegisterClass *RC = NULL; 00460 if (i < MI->getDesc().getNumOperands()) 00461 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF); 00462 AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; 00463 RegRefs.insert(std::make_pair(Reg, RR)); 00464 } 00465 00466 DEBUG(dbgs() << '\n'); 00467 00468 // Form a group of all defs and uses of a KILL instruction to ensure 00469 // that all registers are renamed as a group. 00470 if (MI->isKill()) { 00471 DEBUG(dbgs() << "\tKill Group:"); 00472 00473 unsigned FirstReg = 0; 00474 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00475 MachineOperand &MO = MI->getOperand(i); 00476 if (!MO.isReg()) continue; 00477 unsigned Reg = MO.getReg(); 00478 if (Reg == 0) continue; 00479 00480 if (FirstReg != 0) { 00481 DEBUG(dbgs() << "=" << TRI->getName(Reg)); 00482 State->UnionGroups(FirstReg, Reg); 00483 } else { 00484 DEBUG(dbgs() << " " << TRI->getName(Reg)); 00485 FirstReg = Reg; 00486 } 00487 } 00488 00489 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n'); 00490 } 00491 } 00492 00493 BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) { 00494 BitVector BV(TRI->getNumRegs(), false); 00495 bool first = true; 00496 00497 // Check all references that need rewriting for Reg. For each, use 00498 // the corresponding register class to narrow the set of registers 00499 // that are appropriate for renaming. 00500 std::pair<std::multimap<unsigned, 00501 AggressiveAntiDepState::RegisterReference>::iterator, 00502 std::multimap<unsigned, 00503 AggressiveAntiDepState::RegisterReference>::iterator> 00504 Range = State->GetRegRefs().equal_range(Reg); 00505 for (std::multimap<unsigned, 00506 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first, 00507 QE = Range.second; Q != QE; ++Q) { 00508 const TargetRegisterClass *RC = Q->second.RC; 00509 if (RC == NULL) continue; 00510 00511 BitVector RCBV = TRI->getAllocatableSet(MF, RC); 00512 if (first) { 00513 BV |= RCBV; 00514 first = false; 00515 } else { 00516 BV &= RCBV; 00517 } 00518 00519 DEBUG(dbgs() << " " << RC->getName()); 00520 } 00521 00522 return BV; 00523 } 00524 00525 bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters( 00526 unsigned AntiDepGroupIndex, 00527 RenameOrderType& RenameOrder, 00528 std::map<unsigned, unsigned> &RenameMap) { 00529 std::vector<unsigned> &KillIndices = State->GetKillIndices(); 00530 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00531 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& 00532 RegRefs = State->GetRegRefs(); 00533 00534 // Collect all referenced registers in the same group as 00535 // AntiDepReg. These all need to be renamed together if we are to 00536 // break the anti-dependence. 00537 std::vector<unsigned> Regs; 00538 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs); 00539 assert(Regs.size() > 0 && "Empty register group!"); 00540 if (Regs.size() == 0) 00541 return false; 00542 00543 // Find the "superest" register in the group. At the same time, 00544 // collect the BitVector of registers that can be used to rename 00545 // each register. 00546 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex 00547 << ":\n"); 00548 std::map<unsigned, BitVector> RenameRegisterMap; 00549 unsigned SuperReg = 0; 00550 for (unsigned i = 0, e = Regs.size(); i != e; ++i) { 00551 unsigned Reg = Regs[i]; 00552 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg)) 00553 SuperReg = Reg; 00554 00555 // If Reg has any references, then collect possible rename regs 00556 if (RegRefs.count(Reg) > 0) { 00557 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":"); 00558 00559 BitVector BV = GetRenameRegisters(Reg); 00560 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV)); 00561 00562 DEBUG(dbgs() << " ::"); 00563 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r)) 00564 dbgs() << " " << TRI->getName(r)); 00565 DEBUG(dbgs() << "\n"); 00566 } 00567 } 00568 00569 // All group registers should be a subreg of SuperReg. 00570 for (unsigned i = 0, e = Regs.size(); i != e; ++i) { 00571 unsigned Reg = Regs[i]; 00572 if (Reg == SuperReg) continue; 00573 bool IsSub = TRI->isSubRegister(SuperReg, Reg); 00574 assert(IsSub && "Expecting group subregister"); 00575 if (!IsSub) 00576 return false; 00577 } 00578 00579 #ifndef NDEBUG 00580 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod 00581 if (DebugDiv > 0) { 00582 static int renamecnt = 0; 00583 if (renamecnt++ % DebugDiv != DebugMod) 00584 return false; 00585 00586 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) << 00587 " for debug ***\n"; 00588 } 00589 #endif 00590 00591 // Check each possible rename register for SuperReg in round-robin 00592 // order. If that register is available, and the corresponding 00593 // registers are available for the other group subregisters, then we 00594 // can use those registers to rename. 00595 00596 // FIXME: Using getMinimalPhysRegClass is very conservative. We should 00597 // check every use of the register and find the largest register class 00598 // that can be used in all of them. 00599 const TargetRegisterClass *SuperRC = 00600 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other); 00601 00602 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC); 00603 if (Order.empty()) { 00604 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n"); 00605 return false; 00606 } 00607 00608 DEBUG(dbgs() << "\tFind Registers:"); 00609 00610 if (RenameOrder.count(SuperRC) == 0) 00611 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size())); 00612 00613 unsigned OrigR = RenameOrder[SuperRC]; 00614 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR); 00615 unsigned R = OrigR; 00616 do { 00617 if (R == 0) R = Order.size(); 00618 --R; 00619 const unsigned NewSuperReg = Order[R]; 00620 // Don't consider non-allocatable registers 00621 if (!MRI.isAllocatable(NewSuperReg)) continue; 00622 // Don't replace a register with itself. 00623 if (NewSuperReg == SuperReg) continue; 00624 00625 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':'); 00626 RenameMap.clear(); 00627 00628 // For each referenced group register (which must be a SuperReg or 00629 // a subregister of SuperReg), find the corresponding subregister 00630 // of NewSuperReg and make sure it is free to be renamed. 00631 for (unsigned i = 0, e = Regs.size(); i != e; ++i) { 00632 unsigned Reg = Regs[i]; 00633 unsigned NewReg = 0; 00634 if (Reg == SuperReg) { 00635 NewReg = NewSuperReg; 00636 } else { 00637 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg); 00638 if (NewSubRegIdx != 0) 00639 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx); 00640 } 00641 00642 DEBUG(dbgs() << " " << TRI->getName(NewReg)); 00643 00644 // Check if Reg can be renamed to NewReg. 00645 BitVector BV = RenameRegisterMap[Reg]; 00646 if (!BV.test(NewReg)) { 00647 DEBUG(dbgs() << "(no rename)"); 00648 goto next_super_reg; 00649 } 00650 00651 // If NewReg is dead and NewReg's most recent def is not before 00652 // Regs's kill, it's safe to replace Reg with NewReg. We 00653 // must also check all aliases of NewReg, because we can't define a 00654 // register when any sub or super is already live. 00655 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) { 00656 DEBUG(dbgs() << "(live)"); 00657 goto next_super_reg; 00658 } else { 00659 bool found = false; 00660 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) { 00661 unsigned AliasReg = *AI; 00662 if (State->IsLive(AliasReg) || 00663 (KillIndices[Reg] > DefIndices[AliasReg])) { 00664 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)"); 00665 found = true; 00666 break; 00667 } 00668 } 00669 if (found) 00670 goto next_super_reg; 00671 } 00672 00673 // Record that 'Reg' can be renamed to 'NewReg'. 00674 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg)); 00675 } 00676 00677 // If we fall-out here, then every register in the group can be 00678 // renamed, as recorded in RenameMap. 00679 RenameOrder.erase(SuperRC); 00680 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R)); 00681 DEBUG(dbgs() << "]\n"); 00682 return true; 00683 00684 next_super_reg: 00685 DEBUG(dbgs() << ']'); 00686 } while (R != EndR); 00687 00688 DEBUG(dbgs() << '\n'); 00689 00690 // No registers are free and available! 00691 return false; 00692 } 00693 00694 /// BreakAntiDependencies - Identifiy anti-dependencies within the 00695 /// ScheduleDAG and break them by renaming registers. 00696 /// 00697 unsigned AggressiveAntiDepBreaker::BreakAntiDependencies( 00698 const std::vector<SUnit>& SUnits, 00699 MachineBasicBlock::iterator Begin, 00700 MachineBasicBlock::iterator End, 00701 unsigned InsertPosIndex, 00702 DbgValueVector &DbgValues) { 00703 00704 std::vector<unsigned> &KillIndices = State->GetKillIndices(); 00705 std::vector<unsigned> &DefIndices = State->GetDefIndices(); 00706 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& 00707 RegRefs = State->GetRegRefs(); 00708 00709 // The code below assumes that there is at least one instruction, 00710 // so just duck out immediately if the block is empty. 00711 if (SUnits.empty()) return 0; 00712 00713 // For each regclass the next register to use for renaming. 00714 RenameOrderType RenameOrder; 00715 00716 // ...need a map from MI to SUnit. 00717 std::map<MachineInstr *, const SUnit *> MISUnitMap; 00718 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 00719 const SUnit *SU = &SUnits[i]; 00720 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(), 00721 SU)); 00722 } 00723 00724 // Track progress along the critical path through the SUnit graph as 00725 // we walk the instructions. This is needed for regclasses that only 00726 // break critical-path anti-dependencies. 00727 const SUnit *CriticalPathSU = 0; 00728 MachineInstr *CriticalPathMI = 0; 00729 if (CriticalPathSet.any()) { 00730 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 00731 const SUnit *SU = &SUnits[i]; 00732 if (!CriticalPathSU || 00733 ((SU->getDepth() + SU->Latency) > 00734 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) { 00735 CriticalPathSU = SU; 00736 } 00737 } 00738 00739 CriticalPathMI = CriticalPathSU->getInstr(); 00740 } 00741 00742 #ifndef NDEBUG 00743 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n"); 00744 DEBUG(dbgs() << "Available regs:"); 00745 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { 00746 if (!State->IsLive(Reg)) 00747 DEBUG(dbgs() << " " << TRI->getName(Reg)); 00748 } 00749 DEBUG(dbgs() << '\n'); 00750 #endif 00751 00752 // Attempt to break anti-dependence edges. Walk the instructions 00753 // from the bottom up, tracking information about liveness as we go 00754 // to help determine which registers are available. 00755 unsigned Broken = 0; 00756 unsigned Count = InsertPosIndex - 1; 00757 for (MachineBasicBlock::iterator I = End, E = Begin; 00758 I != E; --Count) { 00759 MachineInstr *MI = --I; 00760 00761 if (MI->isDebugValue()) 00762 continue; 00763 00764 DEBUG(dbgs() << "Anti: "); 00765 DEBUG(MI->dump()); 00766 00767 std::set<unsigned> PassthruRegs; 00768 GetPassthruRegs(MI, PassthruRegs); 00769 00770 // Process the defs in MI... 00771 PrescanInstruction(MI, Count, PassthruRegs); 00772 00773 // The dependence edges that represent anti- and output- 00774 // dependencies that are candidates for breaking. 00775 std::vector<const SDep *> Edges; 00776 const SUnit *PathSU = MISUnitMap[MI]; 00777 AntiDepEdges(PathSU, Edges); 00778 00779 // If MI is not on the critical path, then we don't rename 00780 // registers in the CriticalPathSet. 00781 BitVector *ExcludeRegs = NULL; 00782 if (MI == CriticalPathMI) { 00783 CriticalPathSU = CriticalPathStep(CriticalPathSU); 00784 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0; 00785 } else { 00786 ExcludeRegs = &CriticalPathSet; 00787 } 00788 00789 // Ignore KILL instructions (they form a group in ScanInstruction 00790 // but don't cause any anti-dependence breaking themselves) 00791 if (!MI->isKill()) { 00792 // Attempt to break each anti-dependency... 00793 for (unsigned i = 0, e = Edges.size(); i != e; ++i) { 00794 const SDep *Edge = Edges[i]; 00795 SUnit *NextSU = Edge->getSUnit(); 00796 00797 if ((Edge->getKind() != SDep::Anti) && 00798 (Edge->getKind() != SDep::Output)) continue; 00799 00800 unsigned AntiDepReg = Edge->getReg(); 00801 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg)); 00802 assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); 00803 00804 if (!MRI.isAllocatable(AntiDepReg)) { 00805 // Don't break anti-dependencies on non-allocatable registers. 00806 DEBUG(dbgs() << " (non-allocatable)\n"); 00807 continue; 00808 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) { 00809 // Don't break anti-dependencies for critical path registers 00810 // if not on the critical path 00811 DEBUG(dbgs() << " (not critical-path)\n"); 00812 continue; 00813 } else if (PassthruRegs.count(AntiDepReg) != 0) { 00814 // If the anti-dep register liveness "passes-thru", then 00815 // don't try to change it. It will be changed along with 00816 // the use if required to break an earlier antidep. 00817 DEBUG(dbgs() << " (passthru)\n"); 00818 continue; 00819 } else { 00820 // No anti-dep breaking for implicit deps 00821 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg); 00822 assert(AntiDepOp != NULL && 00823 "Can't find index for defined register operand"); 00824 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) { 00825 DEBUG(dbgs() << " (implicit)\n"); 00826 continue; 00827 } 00828 00829 // If the SUnit has other dependencies on the SUnit that 00830 // it anti-depends on, don't bother breaking the 00831 // anti-dependency since those edges would prevent such 00832 // units from being scheduled past each other 00833 // regardless. 00834 // 00835 // Also, if there are dependencies on other SUnits with the 00836 // same register as the anti-dependency, don't attempt to 00837 // break it. 00838 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), 00839 PE = PathSU->Preds.end(); P != PE; ++P) { 00840 if (P->getSUnit() == NextSU ? 00841 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : 00842 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { 00843 AntiDepReg = 0; 00844 break; 00845 } 00846 } 00847 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), 00848 PE = PathSU->Preds.end(); P != PE; ++P) { 00849 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) && 00850 (P->getKind() != SDep::Output)) { 00851 DEBUG(dbgs() << " (real dependency)\n"); 00852 AntiDepReg = 0; 00853 break; 00854 } else if ((P->getSUnit() != NextSU) && 00855 (P->getKind() == SDep::Data) && 00856 (P->getReg() == AntiDepReg)) { 00857 DEBUG(dbgs() << " (other dependency)\n"); 00858 AntiDepReg = 0; 00859 break; 00860 } 00861 } 00862 00863 if (AntiDepReg == 0) continue; 00864 } 00865 00866 assert(AntiDepReg != 0); 00867 if (AntiDepReg == 0) continue; 00868 00869 // Determine AntiDepReg's register group. 00870 const unsigned GroupIndex = State->GetGroup(AntiDepReg); 00871 if (GroupIndex == 0) { 00872 DEBUG(dbgs() << " (zero group)\n"); 00873 continue; 00874 } 00875 00876 DEBUG(dbgs() << '\n'); 00877 00878 // Look for a suitable register to use to break the anti-dependence. 00879 std::map<unsigned, unsigned> RenameMap; 00880 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) { 00881 DEBUG(dbgs() << "\tBreaking anti-dependence edge on " 00882 << TRI->getName(AntiDepReg) << ":"); 00883 00884 // Handle each group register... 00885 for (std::map<unsigned, unsigned>::iterator 00886 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) { 00887 unsigned CurrReg = S->first; 00888 unsigned NewReg = S->second; 00889 00890 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" << 00891 TRI->getName(NewReg) << "(" << 00892 RegRefs.count(CurrReg) << " refs)"); 00893 00894 // Update the references to the old register CurrReg to 00895 // refer to the new register NewReg. 00896 std::pair<std::multimap<unsigned, 00897 AggressiveAntiDepState::RegisterReference>::iterator, 00898 std::multimap<unsigned, 00899 AggressiveAntiDepState::RegisterReference>::iterator> 00900 Range = RegRefs.equal_range(CurrReg); 00901 for (std::multimap<unsigned, 00902 AggressiveAntiDepState::RegisterReference>::iterator 00903 Q = Range.first, QE = Range.second; Q != QE; ++Q) { 00904 Q->second.Operand->setReg(NewReg); 00905 // If the SU for the instruction being updated has debug 00906 // information related to the anti-dependency register, make 00907 // sure to update that as well. 00908 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()]; 00909 if (!SU) continue; 00910 for (DbgValueVector::iterator DVI = DbgValues.begin(), 00911 DVE = DbgValues.end(); DVI != DVE; ++DVI) 00912 if (DVI->second == Q->second.Operand->getParent()) 00913 UpdateDbgValue(DVI->first, AntiDepReg, NewReg); 00914 } 00915 00916 // We just went back in time and modified history; the 00917 // liveness information for CurrReg is now inconsistent. Set 00918 // the state as if it were dead. 00919 State->UnionGroups(NewReg, 0); 00920 RegRefs.erase(NewReg); 00921 DefIndices[NewReg] = DefIndices[CurrReg]; 00922 KillIndices[NewReg] = KillIndices[CurrReg]; 00923 00924 State->UnionGroups(CurrReg, 0); 00925 RegRefs.erase(CurrReg); 00926 DefIndices[CurrReg] = KillIndices[CurrReg]; 00927 KillIndices[CurrReg] = ~0u; 00928 assert(((KillIndices[CurrReg] == ~0u) != 00929 (DefIndices[CurrReg] == ~0u)) && 00930 "Kill and Def maps aren't consistent for AntiDepReg!"); 00931 } 00932 00933 ++Broken; 00934 DEBUG(dbgs() << '\n'); 00935 } 00936 } 00937 } 00938 00939 ScanInstruction(MI, Count); 00940 } 00941 00942 return Broken; 00943 }