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