1 //===- ExecutionDepsFix.cpp - Fix execution domain issues ----*- C++ -*-===//
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 #include "llvm/CodeGen/ExecutionDomainFix.h"
11 #include "llvm/ADT/PostOrderIterator.h"
12 #include "llvm/CodeGen/MachineRegisterInfo.h"
13 #include "llvm/CodeGen/TargetInstrInfo.h"
14 
15 using namespace llvm;
16 
17 #define DEBUG_TYPE "execution-deps-fix"
18 
19 char ReachingDefAnalysis::ID = 0;
20 INITIALIZE_PASS(ReachingDefAnalysis, "reaching-deps-analysis",
21                 "ReachingDefAnalysis", false, true)
22 
23 char BreakFalseDeps::ID = 0;
24 INITIALIZE_PASS_BEGIN(BreakFalseDeps, "break-false-deps", "BreakFalseDeps",
25                       false, false)
26 INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
27 INITIALIZE_PASS_END(BreakFalseDeps, "break-false-deps", "BreakFalseDeps", false,
28                     false)
29 
30 iterator_range<SmallVectorImpl<int>::const_iterator>
31 ExecutionDomainFix::regIndices(unsigned Reg) const {
32   assert(Reg < AliasMap.size() && "Invalid register");
33   const auto &Entry = AliasMap[Reg];
34   return make_range(Entry.begin(), Entry.end());
35 }
36 
37 DomainValue *ExecutionDomainFix::alloc(int domain) {
38   DomainValue *dv = Avail.empty() ? new (Allocator.Allocate()) DomainValue
39                                   : Avail.pop_back_val();
40   if (domain >= 0)
41     dv->addDomain(domain);
42   assert(dv->Refs == 0 && "Reference count wasn't cleared");
43   assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
44   return dv;
45 }
46 
47 void ExecutionDomainFix::release(DomainValue *DV) {
48   while (DV) {
49     assert(DV->Refs && "Bad DomainValue");
50     if (--DV->Refs)
51       return;
52 
53     // There are no more DV references. Collapse any contained instructions.
54     if (DV->AvailableDomains && !DV->isCollapsed())
55       collapse(DV, DV->getFirstDomain());
56 
57     DomainValue *Next = DV->Next;
58     DV->clear();
59     Avail.push_back(DV);
60     // Also release the next DomainValue in the chain.
61     DV = Next;
62   }
63 }
64 
65 DomainValue *ExecutionDomainFix::resolve(DomainValue *&DVRef) {
66   DomainValue *DV = DVRef;
67   if (!DV || !DV->Next)
68     return DV;
69 
70   // DV has a chain. Find the end.
71   do
72     DV = DV->Next;
73   while (DV->Next);
74 
75   // Update DVRef to point to DV.
76   retain(DV);
77   release(DVRef);
78   DVRef = DV;
79   return DV;
80 }
81 
82 void ExecutionDomainFix::setLiveReg(int rx, DomainValue *dv) {
83   assert(unsigned(rx) < NumRegs && "Invalid index");
84   assert(!LiveRegs.empty() && "Must enter basic block first.");
85 
86   if (LiveRegs[rx] == dv)
87     return;
88   if (LiveRegs[rx])
89     release(LiveRegs[rx]);
90   LiveRegs[rx] = retain(dv);
91 }
92 
93 void ExecutionDomainFix::kill(int rx) {
94   assert(unsigned(rx) < NumRegs && "Invalid index");
95   assert(!LiveRegs.empty() && "Must enter basic block first.");
96   if (!LiveRegs[rx])
97     return;
98 
99   release(LiveRegs[rx]);
100   LiveRegs[rx] = nullptr;
101 }
102 
103 void ExecutionDomainFix::force(int rx, unsigned domain) {
104   assert(unsigned(rx) < NumRegs && "Invalid index");
105   assert(!LiveRegs.empty() && "Must enter basic block first.");
106   if (DomainValue *dv = LiveRegs[rx]) {
107     if (dv->isCollapsed())
108       dv->addDomain(domain);
109     else if (dv->hasDomain(domain))
110       collapse(dv, domain);
111     else {
112       // This is an incompatible open DomainValue. Collapse it to whatever and
113       // force the new value into domain. This costs a domain crossing.
114       collapse(dv, dv->getFirstDomain());
115       assert(LiveRegs[rx] && "Not live after collapse?");
116       LiveRegs[rx]->addDomain(domain);
117     }
118   } else {
119     // Set up basic collapsed DomainValue.
120     setLiveReg(rx, alloc(domain));
121   }
122 }
123 
124 void ExecutionDomainFix::collapse(DomainValue *dv, unsigned domain) {
125   assert(dv->hasDomain(domain) && "Cannot collapse");
126 
127   // Collapse all the instructions.
128   while (!dv->Instrs.empty())
129     TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
130   dv->setSingleDomain(domain);
131 
132   // If there are multiple users, give them new, unique DomainValues.
133   if (!LiveRegs.empty() && dv->Refs > 1)
134     for (unsigned rx = 0; rx != NumRegs; ++rx)
135       if (LiveRegs[rx] == dv)
136         setLiveReg(rx, alloc(domain));
137 }
138 
139 bool ExecutionDomainFix::merge(DomainValue *A, DomainValue *B) {
140   assert(!A->isCollapsed() && "Cannot merge into collapsed");
141   assert(!B->isCollapsed() && "Cannot merge from collapsed");
142   if (A == B)
143     return true;
144   // Restrict to the domains that A and B have in common.
145   unsigned common = A->getCommonDomains(B->AvailableDomains);
146   if (!common)
147     return false;
148   A->AvailableDomains = common;
149   A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
150 
151   // Clear the old DomainValue so we won't try to swizzle instructions twice.
152   B->clear();
153   // All uses of B are referred to A.
154   B->Next = retain(A);
155 
156   for (unsigned rx = 0; rx != NumRegs; ++rx) {
157     assert(!LiveRegs.empty() && "no space allocated for live registers");
158     if (LiveRegs[rx] == B)
159       setLiveReg(rx, A);
160   }
161   return true;
162 }
163 
164 void ReachingDefAnalysis::enterBasicBlock(
165     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
166 
167   MachineBasicBlock *MBB = TraversedMBB.MBB;
168   int MBBNumber = MBB->getNumber();
169   assert(MBBNumber < MBBReachingDefs.size() &&
170          "Unexpected basic block number.");
171   MBBReachingDefs[MBBNumber].resize(NumRegUnits);
172 
173   // Reset instruction counter in each basic block.
174   CurInstr = 0;
175 
176   // Set up LiveRegs to represent registers entering MBB.
177   // Default values are 'nothing happened a long time ago'.
178   if (LiveRegs.empty())
179     LiveRegs.assign(NumRegUnits, ReachingDedDefaultVal);
180 
181   // This is the entry block.
182   if (MBB->pred_empty()) {
183     for (const auto &LI : MBB->liveins()) {
184       for (MCRegUnitIterator Unit(LI.PhysReg, TRI); Unit.isValid(); ++Unit) {
185         // Treat function live-ins as if they were defined just before the first
186         // instruction.  Usually, function arguments are set up immediately
187         // before the call.
188         LiveRegs[*Unit] = -1;
189         MBBReachingDefs[MBBNumber][*Unit].push_back(LiveRegs[*Unit]);
190       }
191     }
192     DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
193     return;
194   }
195 
196   // Try to coalesce live-out registers from predecessors.
197   for (MachineBasicBlock *pred : MBB->predecessors()) {
198     assert(pred->getNumber() < MBBOutRegsInfos.size() &&
199            "Should have pre-allocated MBBInfos for all MBBs");
200     const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
201     // Incoming is null if this is a backedge from a BB
202     // we haven't processed yet
203     if (Incoming.empty())
204       continue;
205 
206     for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
207       // Use the most recent predecessor def for each register.
208       LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
209       if ((LiveRegs[Unit] != ReachingDedDefaultVal))
210         MBBReachingDefs[MBBNumber][Unit].push_back(LiveRegs[Unit]);
211     }
212   }
213 
214   DEBUG(dbgs() << printMBBReference(*MBB)
215                << (!TraversedMBB.IsDone ? ": incomplete\n"
216                                         : ": all preds known\n"));
217 }
218 
219 void ExecutionDomainFix::enterBasicBlock(
220     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
221 
222   MachineBasicBlock *MBB = TraversedMBB.MBB;
223 
224   // Set up LiveRegs to represent registers entering MBB.
225   // Set default domain values to 'no domain' (nullptr)
226   if (LiveRegs.empty())
227     LiveRegs.assign(NumRegs, nullptr);
228 
229   // This is the entry block.
230   if (MBB->pred_empty()) {
231     DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
232     return;
233   }
234 
235   // Try to coalesce live-out registers from predecessors.
236   for (MachineBasicBlock *pred : MBB->predecessors()) {
237     assert(pred->getNumber() < MBBOutRegsInfos.size() &&
238            "Should have pre-allocated MBBInfos for all MBBs");
239     LiveRegsDVInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
240     // Incoming is null if this is a backedge from a BB
241     // we haven't processed yet
242     if (Incoming.empty())
243       continue;
244 
245     for (unsigned rx = 0; rx != NumRegs; ++rx) {
246       DomainValue *pdv = resolve(Incoming[rx]);
247       if (!pdv)
248         continue;
249       if (!LiveRegs[rx]) {
250         setLiveReg(rx, pdv);
251         continue;
252       }
253 
254       // We have a live DomainValue from more than one predecessor.
255       if (LiveRegs[rx]->isCollapsed()) {
256         // We are already collapsed, but predecessor is not. Force it.
257         unsigned Domain = LiveRegs[rx]->getFirstDomain();
258         if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
259           collapse(pdv, Domain);
260         continue;
261       }
262 
263       // Currently open, merge in predecessor.
264       if (!pdv->isCollapsed())
265         merge(LiveRegs[rx], pdv);
266       else
267         force(rx, pdv->getFirstDomain());
268     }
269   }
270   DEBUG(dbgs() << printMBBReference(*MBB)
271                << (!TraversedMBB.IsDone ? ": incomplete\n"
272                                         : ": all preds known\n"));
273 }
274 
275 void ReachingDefAnalysis::leaveBasicBlock(
276     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
277   assert(!LiveRegs.empty() && "Must enter basic block first.");
278   int MBBNumber = TraversedMBB.MBB->getNumber();
279   assert(MBBNumber < MBBOutRegsInfos.size() &&
280          "Unexpected basic block number.");
281   // Save register clearances at end of MBB - used by enterBasicBlock().
282   MBBOutRegsInfos[MBBNumber] = LiveRegs;
283 
284   // While processing the basic block, we kept `Def` relative to the start
285   // of the basic block for convenience. However, future use of this information
286   // only cares about the clearance from the end of the block, so adjust
287   // everything to be relative to the end of the basic block.
288   for (int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
289     OutLiveReg -= CurInstr;
290   LiveRegs.clear();
291 }
292 
293 void ExecutionDomainFix::leaveBasicBlock(
294     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
295   assert(!LiveRegs.empty() && "Must enter basic block first.");
296   int MBBNumber = TraversedMBB.MBB->getNumber();
297   assert(MBBNumber < MBBOutRegsInfos.size() &&
298          "Unexpected basic block number.");
299   // Save register clearances at end of MBB - used by enterBasicBlock().
300   for (DomainValue *OldLiveReg : MBBOutRegsInfos[MBBNumber]) {
301     release(OldLiveReg);
302   }
303   MBBOutRegsInfos[MBBNumber] = LiveRegs;
304   LiveRegs.clear();
305 }
306 
307 bool ExecutionDomainFix::visitInstr(MachineInstr *MI) {
308   // Update instructions with explicit execution domains.
309   std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
310   if (DomP.first) {
311     if (DomP.second)
312       visitSoftInstr(MI, DomP.second);
313     else
314       visitHardInstr(MI, DomP.first);
315   }
316 
317   return !DomP.first;
318 }
319 
320 bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
321                                               unsigned Pref) {
322   MachineOperand &MO = MI->getOperand(OpIdx);
323   assert(MO.isUndef() && "Expected undef machine operand");
324 
325   unsigned OriginalReg = MO.getReg();
326 
327   // Update only undef operands that have reg units that are mapped to one root.
328   for (MCRegUnitIterator Unit(OriginalReg, TRI); Unit.isValid(); ++Unit) {
329     unsigned NumRoots = 0;
330     for (MCRegUnitRootIterator Root(*Unit, TRI); Root.isValid(); ++Root) {
331       NumRoots++;
332       if (NumRoots > 1)
333         return false;
334     }
335   }
336 
337   // Get the undef operand's register class
338   const TargetRegisterClass *OpRC =
339       TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
340 
341   // If the instruction has a true dependency, we can hide the false depdency
342   // behind it.
343   for (MachineOperand &CurrMO : MI->operands()) {
344     if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
345         !OpRC->contains(CurrMO.getReg()))
346       continue;
347     // We found a true dependency - replace the undef register with the true
348     // dependency.
349     MO.setReg(CurrMO.getReg());
350     return true;
351   }
352 
353   // Go over all registers in the register class and find the register with
354   // max clearance or clearance higher than Pref.
355   unsigned MaxClearance = 0;
356   unsigned MaxClearanceReg = OriginalReg;
357   ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
358   for (MCPhysReg Reg : Order) {
359     unsigned Clearance = RDA->getClearance(MI, Reg);
360     if (Clearance <= MaxClearance)
361       continue;
362     MaxClearance = Clearance;
363     MaxClearanceReg = Reg;
364 
365     if (MaxClearance > Pref)
366       break;
367   }
368 
369   // Update the operand if we found a register with better clearance.
370   if (MaxClearanceReg != OriginalReg)
371     MO.setReg(MaxClearanceReg);
372 
373   return false;
374 }
375 
376 bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
377                                            unsigned Pref) {
378   unsigned reg = MI->getOperand(OpIdx).getReg();
379   unsigned Clearance = RDA->getClearance(MI, reg);
380   DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
381 
382   if (Pref > Clearance) {
383     DEBUG(dbgs() << ": Break dependency.\n");
384     return true;
385   }
386   DEBUG(dbgs() << ": OK .\n");
387   return false;
388 }
389 
390 void ExecutionDomainFix::processDefs(MachineInstr *MI, bool Kill) {
391   assert(!MI->isDebugValue() && "Won't process debug values");
392   const MCInstrDesc &MCID = MI->getDesc();
393   for (unsigned i = 0,
394                 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
395        i != e; ++i) {
396     MachineOperand &MO = MI->getOperand(i);
397     if (!MO.isReg())
398       continue;
399     if (MO.isUse())
400       continue;
401     for (int rx : regIndices(MO.getReg())) {
402       // This instruction explicitly defines rx.
403       DEBUG(dbgs() << printReg(RC->getRegister(rx), TRI) << ":\t" << *MI);
404 
405       // Kill off domains redefined by generic instructions.
406       if (Kill)
407         kill(rx);
408     }
409   }
410 }
411 
412 void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
413   assert(!MI->isDebugValue() && "Won't process debug values");
414 
415   int MBBNumber = MI->getParent()->getNumber();
416   assert(MBBNumber < MBBReachingDefs.size() &&
417          "Unexpected basic block number.");
418   const MCInstrDesc &MCID = MI->getDesc();
419   for (unsigned i = 0,
420                 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
421        i != e; ++i) {
422     MachineOperand &MO = MI->getOperand(i);
423     if (!MO.isReg() || !MO.getReg())
424       continue;
425     if (MO.isUse())
426       continue;
427     for (MCRegUnitIterator Unit(MO.getReg(), TRI); Unit.isValid(); ++Unit) {
428       // This instruction explicitly defines the current reg unit.
429       DEBUG(dbgs() << printReg(MO.getReg(), TRI) << ":\t" << CurInstr << '\t'
430                    << *MI);
431 
432       // How many instructions since this reg unit was last written?
433       LiveRegs[*Unit] = CurInstr;
434       MBBReachingDefs[MBBNumber][*Unit].push_back(CurInstr);
435     }
436   }
437   InstIds[MI] = CurInstr;
438   ++CurInstr;
439 }
440 
441 void BreakFalseDeps::processDefs(MachineInstr *MI) {
442   assert(!MI->isDebugValue() && "Won't process debug values");
443 
444   // Break dependence on undef uses. Do this before updating LiveRegs below.
445   unsigned OpNum;
446   unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
447   if (Pref) {
448     bool HadTrueDependency = pickBestRegisterForUndef(MI, OpNum, Pref);
449     // We don't need to bother trying to break a dependency if this
450     // instruction has a true dependency on that register through another
451     // operand - we'll have to wait for it to be available regardless.
452     if (!HadTrueDependency && shouldBreakDependence(MI, OpNum, Pref))
453       UndefReads.push_back(std::make_pair(MI, OpNum));
454   }
455 
456   const MCInstrDesc &MCID = MI->getDesc();
457   for (unsigned i = 0,
458                 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
459        i != e; ++i) {
460     MachineOperand &MO = MI->getOperand(i);
461     if (!MO.isReg() || !MO.getReg())
462       continue;
463     if (MO.isUse())
464       continue;
465     // Check clearance before partial register updates.
466     unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
467     if (Pref && shouldBreakDependence(MI, i, Pref))
468       TII->breakPartialRegDependency(*MI, i, TRI);
469   }
470 }
471 
472 void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
473   if (UndefReads.empty())
474     return;
475 
476   // Collect this block's live out register units.
477   LiveRegSet.init(*TRI);
478   // We do not need to care about pristine registers as they are just preserved
479   // but not actually used in the function.
480   LiveRegSet.addLiveOutsNoPristines(*MBB);
481 
482   MachineInstr *UndefMI = UndefReads.back().first;
483   unsigned OpIdx = UndefReads.back().second;
484 
485   for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
486     // Update liveness, including the current instruction's defs.
487     LiveRegSet.stepBackward(I);
488 
489     if (UndefMI == &I) {
490       if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
491         TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
492 
493       UndefReads.pop_back();
494       if (UndefReads.empty())
495         return;
496 
497       UndefMI = UndefReads.back().first;
498       OpIdx = UndefReads.back().second;
499     }
500   }
501 }
502 
503 void ExecutionDomainFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
504   // Collapse all uses.
505   for (unsigned i = mi->getDesc().getNumDefs(),
506                 e = mi->getDesc().getNumOperands();
507        i != e; ++i) {
508     MachineOperand &mo = mi->getOperand(i);
509     if (!mo.isReg())
510       continue;
511     for (int rx : regIndices(mo.getReg())) {
512       force(rx, domain);
513     }
514   }
515 
516   // Kill all defs and force them.
517   for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
518     MachineOperand &mo = mi->getOperand(i);
519     if (!mo.isReg())
520       continue;
521     for (int rx : regIndices(mo.getReg())) {
522       kill(rx);
523       force(rx, domain);
524     }
525   }
526 }
527 
528 void ExecutionDomainFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
529   // Bitmask of available domains for this instruction after taking collapsed
530   // operands into account.
531   unsigned available = mask;
532 
533   // Scan the explicit use operands for incoming domains.
534   SmallVector<int, 4> used;
535   if (!LiveRegs.empty())
536     for (unsigned i = mi->getDesc().getNumDefs(),
537                   e = mi->getDesc().getNumOperands();
538          i != e; ++i) {
539       MachineOperand &mo = mi->getOperand(i);
540       if (!mo.isReg())
541         continue;
542       for (int rx : regIndices(mo.getReg())) {
543         DomainValue *dv = LiveRegs[rx];
544         if (dv == nullptr)
545           continue;
546         // Bitmask of domains that dv and available have in common.
547         unsigned common = dv->getCommonDomains(available);
548         // Is it possible to use this collapsed register for free?
549         if (dv->isCollapsed()) {
550           // Restrict available domains to the ones in common with the operand.
551           // If there are no common domains, we must pay the cross-domain
552           // penalty for this operand.
553           if (common)
554             available = common;
555         } else if (common)
556           // Open DomainValue is compatible, save it for merging.
557           used.push_back(rx);
558         else
559           // Open DomainValue is not compatible with instruction. It is useless
560           // now.
561           kill(rx);
562       }
563     }
564 
565   // If the collapsed operands force a single domain, propagate the collapse.
566   if (isPowerOf2_32(available)) {
567     unsigned domain = countTrailingZeros(available);
568     TII->setExecutionDomain(*mi, domain);
569     visitHardInstr(mi, domain);
570     return;
571   }
572 
573   // Kill off any remaining uses that don't match available, and build a list of
574   // incoming DomainValues that we want to merge.
575   SmallVector<int, 4> Regs;
576   for (int rx : used) {
577     assert(!LiveRegs.empty() && "no space allocated for live registers");
578     DomainValue *&LR = LiveRegs[rx];
579     // This useless DomainValue could have been missed above.
580     if (!LR->getCommonDomains(available)) {
581       kill(rx);
582       continue;
583     }
584     // Sorted insertion.
585     // Enables giving priority to the latest domains during merging.
586     auto I = std::upper_bound(
587         Regs.begin(), Regs.end(), rx, [&](int LHS, const int RHS) {
588           return RDA->getReachingDef(mi, RC->getRegister(LHS)) <
589                  RDA->getReachingDef(mi, RC->getRegister(RHS));
590         });
591     Regs.insert(I, rx);
592   }
593 
594   // doms are now sorted in order of appearance. Try to merge them all, giving
595   // priority to the latest ones.
596   DomainValue *dv = nullptr;
597   while (!Regs.empty()) {
598     if (!dv) {
599       dv = LiveRegs[Regs.pop_back_val()];
600       // Force the first dv to match the current instruction.
601       dv->AvailableDomains = dv->getCommonDomains(available);
602       assert(dv->AvailableDomains && "Domain should have been filtered");
603       continue;
604     }
605 
606     DomainValue *Latest = LiveRegs[Regs.pop_back_val()];
607     // Skip already merged values.
608     if (Latest == dv || Latest->Next)
609       continue;
610     if (merge(dv, Latest))
611       continue;
612 
613     // If latest didn't merge, it is useless now. Kill all registers using it.
614     for (int i : used) {
615       assert(!LiveRegs.empty() && "no space allocated for live registers");
616       if (LiveRegs[i] == Latest)
617         kill(i);
618     }
619   }
620 
621   // dv is the DomainValue we are going to use for this instruction.
622   if (!dv) {
623     dv = alloc();
624     dv->AvailableDomains = available;
625   }
626   dv->Instrs.push_back(mi);
627 
628   // Finally set all defs and non-collapsed uses to dv. We must iterate through
629   // all the operators, including imp-def ones.
630   for (MachineOperand &mo : mi->operands()) {
631     if (!mo.isReg())
632       continue;
633     for (int rx : regIndices(mo.getReg())) {
634       if (!LiveRegs[rx] || (mo.isDef() && LiveRegs[rx] != dv)) {
635         kill(rx);
636         setLiveReg(rx, dv);
637       }
638     }
639   }
640 }
641 
642 void ExecutionDomainFix::processBasicBlock(
643     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
644   enterBasicBlock(TraversedMBB);
645   // If this block is not done, it makes little sense to make any decisions
646   // based on clearance information. We need to make a second pass anyway,
647   // and by then we'll have better information, so we can avoid doing the work
648   // to try and break dependencies now.
649   for (MachineInstr &MI : *TraversedMBB.MBB) {
650     if (!MI.isDebugValue()) {
651       bool Kill = false;
652       if (TraversedMBB.PrimaryPass)
653         Kill = visitInstr(&MI);
654       processDefs(&MI, Kill);
655     }
656   }
657   leaveBasicBlock(TraversedMBB);
658 }
659 
660 void ReachingDefAnalysis::processBasicBlock(
661     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
662   enterBasicBlock(TraversedMBB);
663   for (MachineInstr &MI : *TraversedMBB.MBB) {
664     if (!MI.isDebugValue())
665       processDefs(&MI);
666   }
667   leaveBasicBlock(TraversedMBB);
668 }
669 
670 void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
671   UndefReads.clear();
672   // If this block is not done, it makes little sense to make any decisions
673   // based on clearance information. We need to make a second pass anyway,
674   // and by then we'll have better information, so we can avoid doing the work
675   // to try and break dependencies now.
676   for (MachineInstr &MI : *MBB) {
677     if (!MI.isDebugValue())
678       processDefs(&MI);
679   }
680   processUndefReads(MBB);
681 }
682 
683 bool LoopTraversal::isBlockDone(MachineBasicBlock *MBB) {
684   int MBBNumber = MBB->getNumber();
685   assert(MBBNumber < MBBInfos.size() && "Unexpected basic block number.");
686   return MBBInfos[MBBNumber].PrimaryCompleted &&
687          MBBInfos[MBBNumber].IncomingCompleted ==
688              MBBInfos[MBBNumber].PrimaryIncoming &&
689          MBBInfos[MBBNumber].IncomingProcessed == MBB->pred_size();
690 }
691 
692 LoopTraversal::TraversalOrder LoopTraversal::traverse(MachineFunction &MF) {
693   // Initialize the MMBInfos
694   MBBInfos.assign(MF.getNumBlockIDs(), MBBInfo());
695 
696   MachineBasicBlock *Entry = &*MF.begin();
697   ReversePostOrderTraversal<MachineBasicBlock *> RPOT(Entry);
698   SmallVector<MachineBasicBlock *, 4> Workqueue;
699   SmallVector<TraversedMBBInfo, 4> MBBTraversalOrder;
700   for (MachineBasicBlock *MBB : RPOT) {
701     // N.B: IncomingProcessed and IncomingCompleted were already updated while
702     // processing this block's predecessors.
703     int MBBNumber = MBB->getNumber();
704     assert(MBBNumber < MBBInfos.size() && "Unexpected basic block number.");
705     MBBInfos[MBBNumber].PrimaryCompleted = true;
706     MBBInfos[MBBNumber].PrimaryIncoming = MBBInfos[MBBNumber].IncomingProcessed;
707     bool Primary = true;
708     Workqueue.push_back(MBB);
709     while (!Workqueue.empty()) {
710       MachineBasicBlock *ActiveMBB = &*Workqueue.back();
711       Workqueue.pop_back();
712       bool Done = isBlockDone(ActiveMBB);
713       MBBTraversalOrder.push_back(TraversedMBBInfo(ActiveMBB, Primary, Done));
714       for (MachineBasicBlock *Succ : ActiveMBB->successors()) {
715         int SuccNumber = Succ->getNumber();
716         assert(SuccNumber < MBBInfos.size() &&
717                "Unexpected basic block number.");
718         if (!isBlockDone(Succ)) {
719           if (Primary)
720             MBBInfos[SuccNumber].IncomingProcessed++;
721           if (Done)
722             MBBInfos[SuccNumber].IncomingCompleted++;
723           if (isBlockDone(Succ))
724             Workqueue.push_back(Succ);
725         }
726       }
727       Primary = false;
728     }
729   }
730 
731   // We need to go through again and finalize any blocks that are not done yet.
732   // This is possible if blocks have dead predecessors, so we didn't visit them
733   // above.
734   for (MachineBasicBlock *MBB : RPOT) {
735     if (!isBlockDone(MBB))
736       MBBTraversalOrder.push_back(TraversedMBBInfo(MBB, false, true));
737     // Don't update successors here. We'll get to them anyway through this
738     // loop.
739   }
740 
741   MBBInfos.clear();
742 
743   return MBBTraversalOrder;
744 }
745 
746 bool ExecutionDomainFix::runOnMachineFunction(MachineFunction &mf) {
747   if (skipFunction(mf.getFunction()))
748     return false;
749   MF = &mf;
750   TII = MF->getSubtarget().getInstrInfo();
751   TRI = MF->getSubtarget().getRegisterInfo();
752   LiveRegs.clear();
753   assert(NumRegs == RC->getNumRegs() && "Bad regclass");
754 
755   DEBUG(dbgs() << "********** FIX EXECUTION DOMAIN: "
756                << TRI->getRegClassName(RC) << " **********\n");
757 
758   // If no relevant registers are used in the function, we can skip it
759   // completely.
760   bool anyregs = false;
761   const MachineRegisterInfo &MRI = mf.getRegInfo();
762   for (unsigned Reg : *RC) {
763     if (MRI.isPhysRegUsed(Reg)) {
764       anyregs = true;
765       break;
766     }
767   }
768   if (!anyregs)
769     return false;
770 
771   RDA = &getAnalysis<ReachingDefAnalysis>();
772 
773   // Initialize the AliasMap on the first use.
774   if (AliasMap.empty()) {
775     // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
776     // therefore the LiveRegs array.
777     AliasMap.resize(TRI->getNumRegs());
778     for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
779       for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true); AI.isValid();
780            ++AI)
781         AliasMap[*AI].push_back(i);
782   }
783 
784   // Initialize the MBBOutRegsInfos
785   MBBOutRegsInfos.resize(mf.getNumBlockIDs());
786 
787   // Traverse the basic blocks.
788   LoopTraversal Traversal;
789   LoopTraversal::TraversalOrder TraversedMBBOrder = Traversal.traverse(mf);
790   for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder) {
791     processBasicBlock(TraversedMBB);
792   }
793 
794   for (LiveRegsDVInfo OutLiveRegs : MBBOutRegsInfos) {
795     for (DomainValue *OutLiveReg : OutLiveRegs) {
796       if (OutLiveReg)
797         release(OutLiveReg);
798     }
799   }
800   MBBOutRegsInfos.clear();
801   Avail.clear();
802   Allocator.DestroyAll();
803 
804   return false;
805 }
806 
807 bool ReachingDefAnalysis::runOnMachineFunction(MachineFunction &mf) {
808   if (skipFunction(mf.getFunction()))
809     return false;
810   MF = &mf;
811   TII = MF->getSubtarget().getInstrInfo();
812   TRI = MF->getSubtarget().getRegisterInfo();
813 
814   LiveRegs.clear();
815   NumRegUnits = TRI->getNumRegUnits();
816 
817   MBBReachingDefs.resize(mf.getNumBlockIDs());
818 
819   DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
820 
821   // Initialize the MBBOutRegsInfos
822   MBBOutRegsInfos.resize(mf.getNumBlockIDs());
823 
824   // Traverse the basic blocks.
825   LoopTraversal Traversal;
826   LoopTraversal::TraversalOrder TraversedMBBOrder = Traversal.traverse(mf);
827   for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder) {
828     processBasicBlock(TraversedMBB);
829   }
830 
831   // Sorting all reaching defs found for a ceartin reg unit in a given BB.
832   for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
833     for (MBBRegUnitDefs &RegUnitDefs : MBBDefs)
834       std::sort(RegUnitDefs.begin(), RegUnitDefs.end());
835   }
836 
837   return false;
838 }
839 
840 void ReachingDefAnalysis::releaseMemory() {
841   // Clear the internal vectors.
842   MBBOutRegsInfos.clear();
843   MBBReachingDefs.clear();
844   InstIds.clear();
845 }
846 
847 bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
848   if (skipFunction(mf.getFunction()))
849     return false;
850   MF = &mf;
851   TII = MF->getSubtarget().getInstrInfo();
852   TRI = MF->getSubtarget().getRegisterInfo();
853   RDA = &getAnalysis<ReachingDefAnalysis>();
854 
855   RegClassInfo.runOnMachineFunction(mf);
856 
857   DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
858 
859   // Traverse the basic blocks.
860   for (MachineBasicBlock &MBB : mf) {
861     processBasicBlock(&MBB);
862   }
863 
864   return false;
865 }
866 
867 int ReachingDefAnalysis::getReachingDef(MachineInstr *MI, int PhysReg) {
868   assert(InstIds.count(MI) && "Unexpected machine instuction.");
869   int InstId = InstIds[MI];
870   int DefRes = ReachingDedDefaultVal;
871   int MBBNumber = MI->getParent()->getNumber();
872   assert(MBBNumber < MBBReachingDefs.size() &&
873          "Unexpected basic block number.");
874   int LatestDef = ReachingDedDefaultVal;
875   for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
876     for (int Def : MBBReachingDefs[MBBNumber][*Unit]) {
877       if (Def >= InstId)
878         break;
879       DefRes = Def;
880     }
881     LatestDef = std::max(LatestDef, DefRes);
882   }
883   return LatestDef;
884 }
885 
886 int ReachingDefAnalysis::getClearance(MachineInstr *MI, MCPhysReg PhysReg) {
887   assert(InstIds.count(MI) && "Unexpected machine instuction.");
888   return InstIds[MI] - getReachingDef(MI, PhysReg);
889 }
890