LLVM API Documentation
00001 //===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===// 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 contains the execution dependency fix pass. 00011 // 00012 // Some X86 SSE instructions like mov, and, or, xor are available in different 00013 // variants for different operand types. These variant instructions are 00014 // equivalent, but on Nehalem and newer cpus there is extra latency 00015 // transferring data between integer and floating point domains. ARM cores 00016 // have similar issues when they are configured with both VFP and NEON 00017 // pipelines. 00018 // 00019 // This pass changes the variant instructions to minimize domain crossings. 00020 // 00021 //===----------------------------------------------------------------------===// 00022 00023 #include "llvm/CodeGen/Passes.h" 00024 #include "llvm/ADT/PostOrderIterator.h" 00025 #include "llvm/CodeGen/LivePhysRegs.h" 00026 #include "llvm/CodeGen/MachineFunctionPass.h" 00027 #include "llvm/CodeGen/MachineRegisterInfo.h" 00028 #include "llvm/Support/Allocator.h" 00029 #include "llvm/Support/Debug.h" 00030 #include "llvm/Support/raw_ostream.h" 00031 #include "llvm/Target/TargetInstrInfo.h" 00032 #include "llvm/Target/TargetMachine.h" 00033 #include "llvm/Target/TargetSubtargetInfo.h" 00034 00035 using namespace llvm; 00036 00037 #define DEBUG_TYPE "execution-fix" 00038 00039 /// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track 00040 /// of execution domains. 00041 /// 00042 /// An open DomainValue represents a set of instructions that can still switch 00043 /// execution domain. Multiple registers may refer to the same open 00044 /// DomainValue - they will eventually be collapsed to the same execution 00045 /// domain. 00046 /// 00047 /// A collapsed DomainValue represents a single register that has been forced 00048 /// into one of more execution domains. There is a separate collapsed 00049 /// DomainValue for each register, but it may contain multiple execution 00050 /// domains. A register value is initially created in a single execution 00051 /// domain, but if we were forced to pay the penalty of a domain crossing, we 00052 /// keep track of the fact that the register is now available in multiple 00053 /// domains. 00054 namespace { 00055 struct DomainValue { 00056 // Basic reference counting. 00057 unsigned Refs; 00058 00059 // Bitmask of available domains. For an open DomainValue, it is the still 00060 // possible domains for collapsing. For a collapsed DomainValue it is the 00061 // domains where the register is available for free. 00062 unsigned AvailableDomains; 00063 00064 // Pointer to the next DomainValue in a chain. When two DomainValues are 00065 // merged, Victim.Next is set to point to Victor, so old DomainValue 00066 // references can be updated by following the chain. 00067 DomainValue *Next; 00068 00069 // Twiddleable instructions using or defining these registers. 00070 SmallVector<MachineInstr*, 8> Instrs; 00071 00072 // A collapsed DomainValue has no instructions to twiddle - it simply keeps 00073 // track of the domains where the registers are already available. 00074 bool isCollapsed() const { return Instrs.empty(); } 00075 00076 // Is domain available? 00077 bool hasDomain(unsigned domain) const { 00078 return AvailableDomains & (1u << domain); 00079 } 00080 00081 // Mark domain as available. 00082 void addDomain(unsigned domain) { 00083 AvailableDomains |= 1u << domain; 00084 } 00085 00086 // Restrict to a single domain available. 00087 void setSingleDomain(unsigned domain) { 00088 AvailableDomains = 1u << domain; 00089 } 00090 00091 // Return bitmask of domains that are available and in mask. 00092 unsigned getCommonDomains(unsigned mask) const { 00093 return AvailableDomains & mask; 00094 } 00095 00096 // First domain available. 00097 unsigned getFirstDomain() const { 00098 return countTrailingZeros(AvailableDomains); 00099 } 00100 00101 DomainValue() : Refs(0) { clear(); } 00102 00103 // Clear this DomainValue and point to next which has all its data. 00104 void clear() { 00105 AvailableDomains = 0; 00106 Next = nullptr; 00107 Instrs.clear(); 00108 } 00109 }; 00110 } 00111 00112 namespace { 00113 /// LiveReg - Information about a live register. 00114 struct LiveReg { 00115 /// Value currently in this register, or NULL when no value is being tracked. 00116 /// This counts as a DomainValue reference. 00117 DomainValue *Value; 00118 00119 /// Instruction that defined this register, relative to the beginning of the 00120 /// current basic block. When a LiveReg is used to represent a live-out 00121 /// register, this value is relative to the end of the basic block, so it 00122 /// will be a negative number. 00123 int Def; 00124 }; 00125 } // anonynous namespace 00126 00127 namespace { 00128 class ExeDepsFix : public MachineFunctionPass { 00129 static char ID; 00130 SpecificBumpPtrAllocator<DomainValue> Allocator; 00131 SmallVector<DomainValue*,16> Avail; 00132 00133 const TargetRegisterClass *const RC; 00134 MachineFunction *MF; 00135 const TargetInstrInfo *TII; 00136 const TargetRegisterInfo *TRI; 00137 std::vector<int> AliasMap; 00138 const unsigned NumRegs; 00139 LiveReg *LiveRegs; 00140 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap; 00141 LiveOutMap LiveOuts; 00142 00143 /// List of undefined register reads in this block in forward order. 00144 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads; 00145 00146 /// Storage for register unit liveness. 00147 LivePhysRegs LiveRegSet; 00148 00149 /// Current instruction number. 00150 /// The first instruction in each basic block is 0. 00151 int CurInstr; 00152 00153 /// True when the current block has a predecessor that hasn't been visited 00154 /// yet. 00155 bool SeenUnknownBackEdge; 00156 00157 public: 00158 ExeDepsFix(const TargetRegisterClass *rc) 00159 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {} 00160 00161 void getAnalysisUsage(AnalysisUsage &AU) const override { 00162 AU.setPreservesAll(); 00163 MachineFunctionPass::getAnalysisUsage(AU); 00164 } 00165 00166 bool runOnMachineFunction(MachineFunction &MF) override; 00167 00168 const char *getPassName() const override { 00169 return "Execution dependency fix"; 00170 } 00171 00172 private: 00173 // Register mapping. 00174 int regIndex(unsigned Reg); 00175 00176 // DomainValue allocation. 00177 DomainValue *alloc(int domain = -1); 00178 DomainValue *retain(DomainValue *DV) { 00179 if (DV) ++DV->Refs; 00180 return DV; 00181 } 00182 void release(DomainValue*); 00183 DomainValue *resolve(DomainValue*&); 00184 00185 // LiveRegs manipulations. 00186 void setLiveReg(int rx, DomainValue *DV); 00187 void kill(int rx); 00188 void force(int rx, unsigned domain); 00189 void collapse(DomainValue *dv, unsigned domain); 00190 bool merge(DomainValue *A, DomainValue *B); 00191 00192 void enterBasicBlock(MachineBasicBlock*); 00193 void leaveBasicBlock(MachineBasicBlock*); 00194 void visitInstr(MachineInstr*); 00195 void processDefs(MachineInstr*, bool Kill); 00196 void visitSoftInstr(MachineInstr*, unsigned mask); 00197 void visitHardInstr(MachineInstr*, unsigned domain); 00198 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref); 00199 void processUndefReads(MachineBasicBlock*); 00200 }; 00201 } 00202 00203 char ExeDepsFix::ID = 0; 00204 00205 /// Translate TRI register number to an index into our smaller tables of 00206 /// interesting registers. Return -1 for boring registers. 00207 int ExeDepsFix::regIndex(unsigned Reg) { 00208 assert(Reg < AliasMap.size() && "Invalid register"); 00209 return AliasMap[Reg]; 00210 } 00211 00212 DomainValue *ExeDepsFix::alloc(int domain) { 00213 DomainValue *dv = Avail.empty() ? 00214 new(Allocator.Allocate()) DomainValue : 00215 Avail.pop_back_val(); 00216 if (domain >= 0) 00217 dv->addDomain(domain); 00218 assert(dv->Refs == 0 && "Reference count wasn't cleared"); 00219 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled"); 00220 return dv; 00221 } 00222 00223 /// release - Release a reference to DV. When the last reference is released, 00224 /// collapse if needed. 00225 void ExeDepsFix::release(DomainValue *DV) { 00226 while (DV) { 00227 assert(DV->Refs && "Bad DomainValue"); 00228 if (--DV->Refs) 00229 return; 00230 00231 // There are no more DV references. Collapse any contained instructions. 00232 if (DV->AvailableDomains && !DV->isCollapsed()) 00233 collapse(DV, DV->getFirstDomain()); 00234 00235 DomainValue *Next = DV->Next; 00236 DV->clear(); 00237 Avail.push_back(DV); 00238 // Also release the next DomainValue in the chain. 00239 DV = Next; 00240 } 00241 } 00242 00243 /// resolve - Follow the chain of dead DomainValues until a live DomainValue is 00244 /// reached. Update the referenced pointer when necessary. 00245 DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) { 00246 DomainValue *DV = DVRef; 00247 if (!DV || !DV->Next) 00248 return DV; 00249 00250 // DV has a chain. Find the end. 00251 do DV = DV->Next; 00252 while (DV->Next); 00253 00254 // Update DVRef to point to DV. 00255 retain(DV); 00256 release(DVRef); 00257 DVRef = DV; 00258 return DV; 00259 } 00260 00261 /// Set LiveRegs[rx] = dv, updating reference counts. 00262 void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) { 00263 assert(unsigned(rx) < NumRegs && "Invalid index"); 00264 assert(LiveRegs && "Must enter basic block first."); 00265 00266 if (LiveRegs[rx].Value == dv) 00267 return; 00268 if (LiveRegs[rx].Value) 00269 release(LiveRegs[rx].Value); 00270 LiveRegs[rx].Value = retain(dv); 00271 } 00272 00273 // Kill register rx, recycle or collapse any DomainValue. 00274 void ExeDepsFix::kill(int rx) { 00275 assert(unsigned(rx) < NumRegs && "Invalid index"); 00276 assert(LiveRegs && "Must enter basic block first."); 00277 if (!LiveRegs[rx].Value) 00278 return; 00279 00280 release(LiveRegs[rx].Value); 00281 LiveRegs[rx].Value = nullptr; 00282 } 00283 00284 /// Force register rx into domain. 00285 void ExeDepsFix::force(int rx, unsigned domain) { 00286 assert(unsigned(rx) < NumRegs && "Invalid index"); 00287 assert(LiveRegs && "Must enter basic block first."); 00288 if (DomainValue *dv = LiveRegs[rx].Value) { 00289 if (dv->isCollapsed()) 00290 dv->addDomain(domain); 00291 else if (dv->hasDomain(domain)) 00292 collapse(dv, domain); 00293 else { 00294 // This is an incompatible open DomainValue. Collapse it to whatever and 00295 // force the new value into domain. This costs a domain crossing. 00296 collapse(dv, dv->getFirstDomain()); 00297 assert(LiveRegs[rx].Value && "Not live after collapse?"); 00298 LiveRegs[rx].Value->addDomain(domain); 00299 } 00300 } else { 00301 // Set up basic collapsed DomainValue. 00302 setLiveReg(rx, alloc(domain)); 00303 } 00304 } 00305 00306 /// Collapse open DomainValue into given domain. If there are multiple 00307 /// registers using dv, they each get a unique collapsed DomainValue. 00308 void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) { 00309 assert(dv->hasDomain(domain) && "Cannot collapse"); 00310 00311 // Collapse all the instructions. 00312 while (!dv->Instrs.empty()) 00313 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain); 00314 dv->setSingleDomain(domain); 00315 00316 // If there are multiple users, give them new, unique DomainValues. 00317 if (LiveRegs && dv->Refs > 1) 00318 for (unsigned rx = 0; rx != NumRegs; ++rx) 00319 if (LiveRegs[rx].Value == dv) 00320 setLiveReg(rx, alloc(domain)); 00321 } 00322 00323 /// Merge - All instructions and registers in B are moved to A, and B is 00324 /// released. 00325 bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) { 00326 assert(!A->isCollapsed() && "Cannot merge into collapsed"); 00327 assert(!B->isCollapsed() && "Cannot merge from collapsed"); 00328 if (A == B) 00329 return true; 00330 // Restrict to the domains that A and B have in common. 00331 unsigned common = A->getCommonDomains(B->AvailableDomains); 00332 if (!common) 00333 return false; 00334 A->AvailableDomains = common; 00335 A->Instrs.append(B->Instrs.begin(), B->Instrs.end()); 00336 00337 // Clear the old DomainValue so we won't try to swizzle instructions twice. 00338 B->clear(); 00339 // All uses of B are referred to A. 00340 B->Next = retain(A); 00341 00342 for (unsigned rx = 0; rx != NumRegs; ++rx) 00343 if (LiveRegs[rx].Value == B) 00344 setLiveReg(rx, A); 00345 return true; 00346 } 00347 00348 // enterBasicBlock - Set up LiveRegs by merging predecessor live-out values. 00349 void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) { 00350 // Detect back-edges from predecessors we haven't processed yet. 00351 SeenUnknownBackEdge = false; 00352 00353 // Reset instruction counter in each basic block. 00354 CurInstr = 0; 00355 00356 // Set up UndefReads to track undefined register reads. 00357 UndefReads.clear(); 00358 LiveRegSet.clear(); 00359 00360 // Set up LiveRegs to represent registers entering MBB. 00361 if (!LiveRegs) 00362 LiveRegs = new LiveReg[NumRegs]; 00363 00364 // Default values are 'nothing happened a long time ago'. 00365 for (unsigned rx = 0; rx != NumRegs; ++rx) { 00366 LiveRegs[rx].Value = nullptr; 00367 LiveRegs[rx].Def = -(1 << 20); 00368 } 00369 00370 // This is the entry block. 00371 if (MBB->pred_empty()) { 00372 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(), 00373 e = MBB->livein_end(); i != e; ++i) { 00374 int rx = regIndex(*i); 00375 if (rx < 0) 00376 continue; 00377 // Treat function live-ins as if they were defined just before the first 00378 // instruction. Usually, function arguments are set up immediately 00379 // before the call. 00380 LiveRegs[rx].Def = -1; 00381 } 00382 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n"); 00383 return; 00384 } 00385 00386 // Try to coalesce live-out registers from predecessors. 00387 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(), 00388 pe = MBB->pred_end(); pi != pe; ++pi) { 00389 LiveOutMap::const_iterator fi = LiveOuts.find(*pi); 00390 if (fi == LiveOuts.end()) { 00391 SeenUnknownBackEdge = true; 00392 continue; 00393 } 00394 assert(fi->second && "Can't have NULL entries"); 00395 00396 for (unsigned rx = 0; rx != NumRegs; ++rx) { 00397 // Use the most recent predecessor def for each register. 00398 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def); 00399 00400 DomainValue *pdv = resolve(fi->second[rx].Value); 00401 if (!pdv) 00402 continue; 00403 if (!LiveRegs[rx].Value) { 00404 setLiveReg(rx, pdv); 00405 continue; 00406 } 00407 00408 // We have a live DomainValue from more than one predecessor. 00409 if (LiveRegs[rx].Value->isCollapsed()) { 00410 // We are already collapsed, but predecessor is not. Force it. 00411 unsigned Domain = LiveRegs[rx].Value->getFirstDomain(); 00412 if (!pdv->isCollapsed() && pdv->hasDomain(Domain)) 00413 collapse(pdv, Domain); 00414 continue; 00415 } 00416 00417 // Currently open, merge in predecessor. 00418 if (!pdv->isCollapsed()) 00419 merge(LiveRegs[rx].Value, pdv); 00420 else 00421 force(rx, pdv->getFirstDomain()); 00422 } 00423 } 00424 DEBUG(dbgs() << "BB#" << MBB->getNumber() 00425 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n")); 00426 } 00427 00428 void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) { 00429 assert(LiveRegs && "Must enter basic block first."); 00430 // Save live registers at end of MBB - used by enterBasicBlock(). 00431 // Also use LiveOuts as a visited set to detect back-edges. 00432 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second; 00433 00434 if (First) { 00435 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to 00436 // the end of this block instead of the beginning. 00437 for (unsigned i = 0, e = NumRegs; i != e; ++i) 00438 LiveRegs[i].Def -= CurInstr; 00439 } else { 00440 // Insertion failed, this must be the second pass. 00441 // Release all the DomainValues instead of keeping them. 00442 for (unsigned i = 0, e = NumRegs; i != e; ++i) 00443 release(LiveRegs[i].Value); 00444 delete[] LiveRegs; 00445 } 00446 LiveRegs = nullptr; 00447 } 00448 00449 void ExeDepsFix::visitInstr(MachineInstr *MI) { 00450 if (MI->isDebugValue()) 00451 return; 00452 00453 // Update instructions with explicit execution domains. 00454 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI); 00455 if (DomP.first) { 00456 if (DomP.second) 00457 visitSoftInstr(MI, DomP.second); 00458 else 00459 visitHardInstr(MI, DomP.first); 00460 } 00461 00462 // Process defs to track register ages, and kill values clobbered by generic 00463 // instructions. 00464 processDefs(MI, !DomP.first); 00465 } 00466 00467 /// \brief Return true to if it makes sense to break dependence on a partial def 00468 /// or undef use. 00469 bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx, 00470 unsigned Pref) { 00471 int rx = regIndex(MI->getOperand(OpIdx).getReg()); 00472 if (rx < 0) 00473 return false; 00474 00475 unsigned Clearance = CurInstr - LiveRegs[rx].Def; 00476 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref); 00477 00478 if (Pref > Clearance) { 00479 DEBUG(dbgs() << ": Break dependency.\n"); 00480 return true; 00481 } 00482 // The current clearance seems OK, but we may be ignoring a def from a 00483 // back-edge. 00484 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) { 00485 DEBUG(dbgs() << ": OK .\n"); 00486 return false; 00487 } 00488 // A def from an unprocessed back-edge may make us break this dependency. 00489 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n"); 00490 return false; 00491 } 00492 00493 // Update def-ages for registers defined by MI. 00494 // If Kill is set, also kill off DomainValues clobbered by the defs. 00495 // 00496 // Also break dependencies on partial defs and undef uses. 00497 void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) { 00498 assert(!MI->isDebugValue() && "Won't process debug values"); 00499 00500 // Break dependence on undef uses. Do this before updating LiveRegs below. 00501 unsigned OpNum; 00502 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI); 00503 if (Pref) { 00504 if (shouldBreakDependence(MI, OpNum, Pref)) 00505 UndefReads.push_back(std::make_pair(MI, OpNum)); 00506 } 00507 const MCInstrDesc &MCID = MI->getDesc(); 00508 for (unsigned i = 0, 00509 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs(); 00510 i != e; ++i) { 00511 MachineOperand &MO = MI->getOperand(i); 00512 if (!MO.isReg()) 00513 continue; 00514 if (MO.isImplicit()) 00515 break; 00516 if (MO.isUse()) 00517 continue; 00518 int rx = regIndex(MO.getReg()); 00519 if (rx < 0) 00520 continue; 00521 00522 // This instruction explicitly defines rx. 00523 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr 00524 << '\t' << *MI); 00525 00526 // Check clearance before partial register updates. 00527 // Call breakDependence before setting LiveRegs[rx].Def. 00528 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI); 00529 if (Pref && shouldBreakDependence(MI, i, Pref)) 00530 TII->breakPartialRegDependency(MI, i, TRI); 00531 00532 // How many instructions since rx was last written? 00533 LiveRegs[rx].Def = CurInstr; 00534 00535 // Kill off domains redefined by generic instructions. 00536 if (Kill) 00537 kill(rx); 00538 } 00539 ++CurInstr; 00540 } 00541 00542 /// \break Break false dependencies on undefined register reads. 00543 /// 00544 /// Walk the block backward computing precise liveness. This is expensive, so we 00545 /// only do it on demand. Note that the occurrence of undefined register reads 00546 /// that should be broken is very rare, but when they occur we may have many in 00547 /// a single block. 00548 void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) { 00549 if (UndefReads.empty()) 00550 return; 00551 00552 // Collect this block's live out register units. 00553 LiveRegSet.init(TRI); 00554 LiveRegSet.addLiveOuts(MBB); 00555 00556 MachineInstr *UndefMI = UndefReads.back().first; 00557 unsigned OpIdx = UndefReads.back().second; 00558 00559 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend(); 00560 I != E; ++I) { 00561 // Update liveness, including the current instruction's defs. 00562 LiveRegSet.stepBackward(*I); 00563 00564 if (UndefMI == &*I) { 00565 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg())) 00566 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI); 00567 00568 UndefReads.pop_back(); 00569 if (UndefReads.empty()) 00570 return; 00571 00572 UndefMI = UndefReads.back().first; 00573 OpIdx = UndefReads.back().second; 00574 } 00575 } 00576 } 00577 00578 // A hard instruction only works in one domain. All input registers will be 00579 // forced into that domain. 00580 void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) { 00581 // Collapse all uses. 00582 for (unsigned i = mi->getDesc().getNumDefs(), 00583 e = mi->getDesc().getNumOperands(); i != e; ++i) { 00584 MachineOperand &mo = mi->getOperand(i); 00585 if (!mo.isReg()) continue; 00586 int rx = regIndex(mo.getReg()); 00587 if (rx < 0) continue; 00588 force(rx, domain); 00589 } 00590 00591 // Kill all defs and force them. 00592 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) { 00593 MachineOperand &mo = mi->getOperand(i); 00594 if (!mo.isReg()) continue; 00595 int rx = regIndex(mo.getReg()); 00596 if (rx < 0) continue; 00597 kill(rx); 00598 force(rx, domain); 00599 } 00600 } 00601 00602 // A soft instruction can be changed to work in other domains given by mask. 00603 void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) { 00604 // Bitmask of available domains for this instruction after taking collapsed 00605 // operands into account. 00606 unsigned available = mask; 00607 00608 // Scan the explicit use operands for incoming domains. 00609 SmallVector<int, 4> used; 00610 if (LiveRegs) 00611 for (unsigned i = mi->getDesc().getNumDefs(), 00612 e = mi->getDesc().getNumOperands(); i != e; ++i) { 00613 MachineOperand &mo = mi->getOperand(i); 00614 if (!mo.isReg()) continue; 00615 int rx = regIndex(mo.getReg()); 00616 if (rx < 0) continue; 00617 if (DomainValue *dv = LiveRegs[rx].Value) { 00618 // Bitmask of domains that dv and available have in common. 00619 unsigned common = dv->getCommonDomains(available); 00620 // Is it possible to use this collapsed register for free? 00621 if (dv->isCollapsed()) { 00622 // Restrict available domains to the ones in common with the operand. 00623 // If there are no common domains, we must pay the cross-domain 00624 // penalty for this operand. 00625 if (common) available = common; 00626 } else if (common) 00627 // Open DomainValue is compatible, save it for merging. 00628 used.push_back(rx); 00629 else 00630 // Open DomainValue is not compatible with instruction. It is useless 00631 // now. 00632 kill(rx); 00633 } 00634 } 00635 00636 // If the collapsed operands force a single domain, propagate the collapse. 00637 if (isPowerOf2_32(available)) { 00638 unsigned domain = countTrailingZeros(available); 00639 TII->setExecutionDomain(mi, domain); 00640 visitHardInstr(mi, domain); 00641 return; 00642 } 00643 00644 // Kill off any remaining uses that don't match available, and build a list of 00645 // incoming DomainValues that we want to merge. 00646 SmallVector<LiveReg, 4> Regs; 00647 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) { 00648 int rx = *i; 00649 const LiveReg &LR = LiveRegs[rx]; 00650 // This useless DomainValue could have been missed above. 00651 if (!LR.Value->getCommonDomains(available)) { 00652 kill(rx); 00653 continue; 00654 } 00655 // Sorted insertion. 00656 bool Inserted = false; 00657 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end(); 00658 i != e && !Inserted; ++i) { 00659 if (LR.Def < i->Def) { 00660 Inserted = true; 00661 Regs.insert(i, LR); 00662 } 00663 } 00664 if (!Inserted) 00665 Regs.push_back(LR); 00666 } 00667 00668 // doms are now sorted in order of appearance. Try to merge them all, giving 00669 // priority to the latest ones. 00670 DomainValue *dv = nullptr; 00671 while (!Regs.empty()) { 00672 if (!dv) { 00673 dv = Regs.pop_back_val().Value; 00674 // Force the first dv to match the current instruction. 00675 dv->AvailableDomains = dv->getCommonDomains(available); 00676 assert(dv->AvailableDomains && "Domain should have been filtered"); 00677 continue; 00678 } 00679 00680 DomainValue *Latest = Regs.pop_back_val().Value; 00681 // Skip already merged values. 00682 if (Latest == dv || Latest->Next) 00683 continue; 00684 if (merge(dv, Latest)) 00685 continue; 00686 00687 // If latest didn't merge, it is useless now. Kill all registers using it. 00688 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) 00689 if (LiveRegs[*i].Value == Latest) 00690 kill(*i); 00691 } 00692 00693 // dv is the DomainValue we are going to use for this instruction. 00694 if (!dv) { 00695 dv = alloc(); 00696 dv->AvailableDomains = available; 00697 } 00698 dv->Instrs.push_back(mi); 00699 00700 // Finally set all defs and non-collapsed uses to dv. We must iterate through 00701 // all the operators, including imp-def ones. 00702 for (MachineInstr::mop_iterator ii = mi->operands_begin(), 00703 ee = mi->operands_end(); 00704 ii != ee; ++ii) { 00705 MachineOperand &mo = *ii; 00706 if (!mo.isReg()) continue; 00707 int rx = regIndex(mo.getReg()); 00708 if (rx < 0) continue; 00709 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) { 00710 kill(rx); 00711 setLiveReg(rx, dv); 00712 } 00713 } 00714 } 00715 00716 bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) { 00717 MF = &mf; 00718 TII = MF->getSubtarget().getInstrInfo(); 00719 TRI = MF->getSubtarget().getRegisterInfo(); 00720 LiveRegs = nullptr; 00721 assert(NumRegs == RC->getNumRegs() && "Bad regclass"); 00722 00723 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: " 00724 << RC->getName() << " **********\n"); 00725 00726 // If no relevant registers are used in the function, we can skip it 00727 // completely. 00728 bool anyregs = false; 00729 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end(); 00730 I != E; ++I) 00731 if (MF->getRegInfo().isPhysRegUsed(*I)) { 00732 anyregs = true; 00733 break; 00734 } 00735 if (!anyregs) return false; 00736 00737 // Initialize the AliasMap on the first use. 00738 if (AliasMap.empty()) { 00739 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC, 00740 // or -1. 00741 AliasMap.resize(TRI->getNumRegs(), -1); 00742 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i) 00743 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true); 00744 AI.isValid(); ++AI) 00745 AliasMap[*AI] = i; 00746 } 00747 00748 MachineBasicBlock *Entry = MF->begin(); 00749 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry); 00750 SmallVector<MachineBasicBlock*, 16> Loops; 00751 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator 00752 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) { 00753 MachineBasicBlock *MBB = *MBBI; 00754 enterBasicBlock(MBB); 00755 if (SeenUnknownBackEdge) 00756 Loops.push_back(MBB); 00757 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; 00758 ++I) 00759 visitInstr(I); 00760 processUndefReads(MBB); 00761 leaveBasicBlock(MBB); 00762 } 00763 00764 // Visit all the loop blocks again in order to merge DomainValues from 00765 // back-edges. 00766 for (unsigned i = 0, e = Loops.size(); i != e; ++i) { 00767 MachineBasicBlock *MBB = Loops[i]; 00768 enterBasicBlock(MBB); 00769 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; 00770 ++I) 00771 if (!I->isDebugValue()) 00772 processDefs(I, false); 00773 processUndefReads(MBB); 00774 leaveBasicBlock(MBB); 00775 } 00776 00777 // Clear the LiveOuts vectors and collapse any remaining DomainValues. 00778 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator 00779 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) { 00780 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI); 00781 if (FI == LiveOuts.end() || !FI->second) 00782 continue; 00783 for (unsigned i = 0, e = NumRegs; i != e; ++i) 00784 if (FI->second[i].Value) 00785 release(FI->second[i].Value); 00786 delete[] FI->second; 00787 } 00788 LiveOuts.clear(); 00789 UndefReads.clear(); 00790 Avail.clear(); 00791 Allocator.DestroyAll(); 00792 00793 return false; 00794 } 00795 00796 FunctionPass * 00797 llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) { 00798 return new ExeDepsFix(RC); 00799 }