1 //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file This implements the ScheduleDAGInstrs class, which implements
10 /// re-scheduling of MachineInstrs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
15 #include "llvm/ADT/IntEqClasses.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/SparseSet.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/CodeGen/LiveIntervals.h"
24 #include "llvm/CodeGen/LivePhysRegs.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBundle.h"
30 #include "llvm/CodeGen/MachineMemOperand.h"
31 #include "llvm/CodeGen/MachineOperand.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/PseudoSourceValue.h"
34 #include "llvm/CodeGen/RegisterPressure.h"
35 #include "llvm/CodeGen/ScheduleDAG.h"
36 #include "llvm/CodeGen/ScheduleDFS.h"
37 #include "llvm/CodeGen/SlotIndexes.h"
38 #include "llvm/CodeGen/TargetRegisterInfo.h"
39 #include "llvm/CodeGen/TargetSubtargetInfo.h"
40 #include "llvm/Config/llvm-config.h"
41 #include "llvm/IR/Constants.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/Operator.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/MC/LaneBitmask.h"
49 #include "llvm/MC/MCRegisterInfo.h"
50 #include "llvm/Support/Casting.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/Format.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include <algorithm>
58 #include <cassert>
59 #include <iterator>
60 #include <string>
61 #include <utility>
62 #include <vector>
63 
64 using namespace llvm;
65 
66 #define DEBUG_TYPE "machine-scheduler"
67 
68 static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden,
69     cl::ZeroOrMore, cl::init(false),
70     cl::desc("Enable use of AA during MI DAG construction"));
71 
72 static cl::opt<bool> UseTBAA("use-tbaa-in-sched-mi", cl::Hidden,
73     cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction"));
74 
75 // Note: the two options below might be used in tuning compile time vs
76 // output quality. Setting HugeRegion so large that it will never be
77 // reached means best-effort, but may be slow.
78 
79 // When Stores and Loads maps (or NonAliasStores and NonAliasLoads)
80 // together hold this many SUs, a reduction of maps will be done.
81 static cl::opt<unsigned> HugeRegion("dag-maps-huge-region", cl::Hidden,
82     cl::init(1000), cl::desc("The limit to use while constructing the DAG "
83                              "prior to scheduling, at which point a trade-off "
84                              "is made to avoid excessive compile time."));
85 
86 static cl::opt<unsigned> ReductionSize(
87     "dag-maps-reduction-size", cl::Hidden,
88     cl::desc("A huge scheduling region will have maps reduced by this many "
89              "nodes at a time. Defaults to HugeRegion / 2."));
90 
getReductionSize()91 static unsigned getReductionSize() {
92   // Always reduce a huge region with half of the elements, except
93   // when user sets this number explicitly.
94   if (ReductionSize.getNumOccurrences() == 0)
95     return HugeRegion / 2;
96   return ReductionSize;
97 }
98 
dumpSUList(ScheduleDAGInstrs::SUList & L)99 static void dumpSUList(ScheduleDAGInstrs::SUList &L) {
100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
101   dbgs() << "{ ";
102   for (const SUnit *su : L) {
103     dbgs() << "SU(" << su->NodeNum << ")";
104     if (su != L.back())
105       dbgs() << ", ";
106   }
107   dbgs() << "}\n";
108 #endif
109 }
110 
ScheduleDAGInstrs(MachineFunction & mf,const MachineLoopInfo * mli,bool RemoveKillFlags)111 ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
112                                      const MachineLoopInfo *mli,
113                                      bool RemoveKillFlags)
114     : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()),
115       RemoveKillFlags(RemoveKillFlags),
116       UnknownValue(UndefValue::get(
117                              Type::getVoidTy(mf.getFunction().getContext()))), Topo(SUnits, &ExitSU) {
118   DbgValues.clear();
119 
120   const TargetSubtargetInfo &ST = mf.getSubtarget();
121   SchedModel.init(&ST);
122 }
123 
124 /// If this machine instr has memory reference information and it can be
125 /// tracked to a normal reference to a known object, return the Value
126 /// for that object. This function returns false the memory location is
127 /// unknown or may alias anything.
getUnderlyingObjectsForInstr(const MachineInstr * MI,const MachineFrameInfo & MFI,UnderlyingObjectsVector & Objects,const DataLayout & DL)128 static bool getUnderlyingObjectsForInstr(const MachineInstr *MI,
129                                          const MachineFrameInfo &MFI,
130                                          UnderlyingObjectsVector &Objects,
131                                          const DataLayout &DL) {
132   auto allMMOsOkay = [&]() {
133     for (const MachineMemOperand *MMO : MI->memoperands()) {
134       // TODO: Figure out whether isAtomic is really necessary (see D57601).
135       if (MMO->isVolatile() || MMO->isAtomic())
136         return false;
137 
138       if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) {
139         // Function that contain tail calls don't have unique PseudoSourceValue
140         // objects. Two PseudoSourceValues might refer to the same or
141         // overlapping locations. The client code calling this function assumes
142         // this is not the case. So return a conservative answer of no known
143         // object.
144         if (MFI.hasTailCall())
145           return false;
146 
147         // For now, ignore PseudoSourceValues which may alias LLVM IR values
148         // because the code that uses this function has no way to cope with
149         // such aliases.
150         if (PSV->isAliased(&MFI))
151           return false;
152 
153         bool MayAlias = PSV->mayAlias(&MFI);
154         Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias));
155       } else if (const Value *V = MMO->getValue()) {
156         SmallVector<Value *, 4> Objs;
157         if (!getUnderlyingObjectsForCodeGen(V, Objs))
158           return false;
159 
160         for (Value *V : Objs) {
161           assert(isIdentifiedObject(V));
162           Objects.push_back(UnderlyingObjectsVector::value_type(V, true));
163         }
164       } else
165         return false;
166     }
167     return true;
168   };
169 
170   if (!allMMOsOkay()) {
171     Objects.clear();
172     return false;
173   }
174 
175   return true;
176 }
177 
startBlock(MachineBasicBlock * bb)178 void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) {
179   BB = bb;
180 }
181 
finishBlock()182 void ScheduleDAGInstrs::finishBlock() {
183   // Subclasses should no longer refer to the old block.
184   BB = nullptr;
185 }
186 
enterRegion(MachineBasicBlock * bb,MachineBasicBlock::iterator begin,MachineBasicBlock::iterator end,unsigned regioninstrs)187 void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
188                                     MachineBasicBlock::iterator begin,
189                                     MachineBasicBlock::iterator end,
190                                     unsigned regioninstrs) {
191   assert(bb == BB && "startBlock should set BB");
192   RegionBegin = begin;
193   RegionEnd = end;
194   NumRegionInstrs = regioninstrs;
195 }
196 
exitRegion()197 void ScheduleDAGInstrs::exitRegion() {
198   // Nothing to do.
199 }
200 
addSchedBarrierDeps()201 void ScheduleDAGInstrs::addSchedBarrierDeps() {
202   MachineInstr *ExitMI =
203       RegionEnd != BB->end()
204           ? &*skipDebugInstructionsBackward(RegionEnd, RegionBegin)
205           : nullptr;
206   ExitSU.setInstr(ExitMI);
207   // Add dependencies on the defs and uses of the instruction.
208   if (ExitMI) {
209     for (const MachineOperand &MO : ExitMI->operands()) {
210       if (!MO.isReg() || MO.isDef()) continue;
211       Register Reg = MO.getReg();
212       if (Register::isPhysicalRegister(Reg)) {
213         Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg));
214       } else if (Register::isVirtualRegister(Reg) && MO.readsReg()) {
215         addVRegUseDeps(&ExitSU, ExitMI->getOperandNo(&MO));
216       }
217     }
218   }
219   if (!ExitMI || (!ExitMI->isCall() && !ExitMI->isBarrier())) {
220     // For others, e.g. fallthrough, conditional branch, assume the exit
221     // uses all the registers that are livein to the successor blocks.
222     for (const MachineBasicBlock *Succ : BB->successors()) {
223       for (const auto &LI : Succ->liveins()) {
224         if (!Uses.contains(LI.PhysReg))
225           Uses.insert(PhysRegSUOper(&ExitSU, -1, LI.PhysReg));
226       }
227     }
228   }
229 }
230 
231 /// MO is an operand of SU's instruction that defines a physical register. Adds
232 /// data dependencies from SU to any uses of the physical register.
addPhysRegDataDeps(SUnit * SU,unsigned OperIdx)233 void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) {
234   const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx);
235   assert(MO.isDef() && "expect physreg def");
236 
237   // Ask the target if address-backscheduling is desirable, and if so how much.
238   const TargetSubtargetInfo &ST = MF.getSubtarget();
239 
240   // Only use any non-zero latency for real defs/uses, in contrast to
241   // "fake" operands added by regalloc.
242   const MCInstrDesc *DefMIDesc = &SU->getInstr()->getDesc();
243   bool ImplicitPseudoDef = (OperIdx >= DefMIDesc->getNumOperands() &&
244                             !DefMIDesc->hasImplicitDefOfPhysReg(MO.getReg()));
245   for (MCRegAliasIterator Alias(MO.getReg(), TRI, true);
246        Alias.isValid(); ++Alias) {
247     for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) {
248       SUnit *UseSU = I->SU;
249       if (UseSU == SU)
250         continue;
251 
252       // Adjust the dependence latency using operand def/use information,
253       // then allow the target to perform its own adjustments.
254       int UseOp = I->OpIdx;
255       MachineInstr *RegUse = nullptr;
256       SDep Dep;
257       if (UseOp < 0)
258         Dep = SDep(SU, SDep::Artificial);
259       else {
260         // Set the hasPhysRegDefs only for physreg defs that have a use within
261         // the scheduling region.
262         SU->hasPhysRegDefs = true;
263         Dep = SDep(SU, SDep::Data, *Alias);
264         RegUse = UseSU->getInstr();
265       }
266       const MCInstrDesc *UseMIDesc =
267           (RegUse ? &UseSU->getInstr()->getDesc() : nullptr);
268       bool ImplicitPseudoUse =
269           (UseMIDesc && UseOp >= ((int)UseMIDesc->getNumOperands()) &&
270            !UseMIDesc->hasImplicitUseOfPhysReg(*Alias));
271       if (!ImplicitPseudoDef && !ImplicitPseudoUse) {
272         Dep.setLatency(SchedModel.computeOperandLatency(SU->getInstr(), OperIdx,
273                                                         RegUse, UseOp));
274         ST.adjustSchedDependency(SU, OperIdx, UseSU, UseOp, Dep);
275       } else {
276         Dep.setLatency(0);
277         // FIXME: We could always let target to adjustSchedDependency(), and
278         // remove this condition, but that currently asserts in Hexagon BE.
279         if (SU->getInstr()->isBundle() || (RegUse && RegUse->isBundle()))
280           ST.adjustSchedDependency(SU, OperIdx, UseSU, UseOp, Dep);
281       }
282 
283       UseSU->addPred(Dep);
284     }
285   }
286 }
287 
288 /// Adds register dependencies (data, anti, and output) from this SUnit
289 /// to following instructions in the same scheduling region that depend the
290 /// physical register referenced at OperIdx.
addPhysRegDeps(SUnit * SU,unsigned OperIdx)291 void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
292   MachineInstr *MI = SU->getInstr();
293   MachineOperand &MO = MI->getOperand(OperIdx);
294   Register Reg = MO.getReg();
295   // We do not need to track any dependencies for constant registers.
296   if (MRI.isConstantPhysReg(Reg))
297     return;
298 
299   const TargetSubtargetInfo &ST = MF.getSubtarget();
300 
301   // Optionally add output and anti dependencies. For anti
302   // dependencies we use a latency of 0 because for a multi-issue
303   // target we want to allow the defining instruction to issue
304   // in the same cycle as the using instruction.
305   // TODO: Using a latency of 1 here for output dependencies assumes
306   //       there's no cost for reusing registers.
307   SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
308   for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
309     if (!Defs.contains(*Alias))
310       continue;
311     for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) {
312       SUnit *DefSU = I->SU;
313       if (DefSU == &ExitSU)
314         continue;
315       if (DefSU != SU &&
316           (Kind != SDep::Output || !MO.isDead() ||
317            !DefSU->getInstr()->registerDefIsDead(*Alias))) {
318         SDep Dep(SU, Kind, /*Reg=*/*Alias);
319         if (Kind != SDep::Anti)
320           Dep.setLatency(
321             SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
322         ST.adjustSchedDependency(SU, OperIdx, DefSU, I->OpIdx, Dep);
323         DefSU->addPred(Dep);
324       }
325     }
326   }
327 
328   if (!MO.isDef()) {
329     SU->hasPhysRegUses = true;
330     // Either insert a new Reg2SUnits entry with an empty SUnits list, or
331     // retrieve the existing SUnits list for this register's uses.
332     // Push this SUnit on the use list.
333     Uses.insert(PhysRegSUOper(SU, OperIdx, Reg));
334     if (RemoveKillFlags)
335       MO.setIsKill(false);
336   } else {
337     addPhysRegDataDeps(SU, OperIdx);
338 
339     // Clear previous uses and defs of this register and its subergisters.
340     for (MCSubRegIterator SubReg(Reg, TRI, true); SubReg.isValid(); ++SubReg) {
341       if (Uses.contains(*SubReg))
342         Uses.eraseAll(*SubReg);
343       if (!MO.isDead())
344         Defs.eraseAll(*SubReg);
345     }
346     if (MO.isDead() && SU->isCall) {
347       // Calls will not be reordered because of chain dependencies (see
348       // below). Since call operands are dead, calls may continue to be added
349       // to the DefList making dependence checking quadratic in the size of
350       // the block. Instead, we leave only one call at the back of the
351       // DefList.
352       Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg);
353       Reg2SUnitsMap::iterator B = P.first;
354       Reg2SUnitsMap::iterator I = P.second;
355       for (bool isBegin = I == B; !isBegin; /* empty */) {
356         isBegin = (--I) == B;
357         if (!I->SU->isCall)
358           break;
359         I = Defs.erase(I);
360       }
361     }
362 
363     // Defs are pushed in the order they are visited and never reordered.
364     Defs.insert(PhysRegSUOper(SU, OperIdx, Reg));
365   }
366 }
367 
getLaneMaskForMO(const MachineOperand & MO) const368 LaneBitmask ScheduleDAGInstrs::getLaneMaskForMO(const MachineOperand &MO) const
369 {
370   Register Reg = MO.getReg();
371   // No point in tracking lanemasks if we don't have interesting subregisters.
372   const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
373   if (!RC.HasDisjunctSubRegs)
374     return LaneBitmask::getAll();
375 
376   unsigned SubReg = MO.getSubReg();
377   if (SubReg == 0)
378     return RC.getLaneMask();
379   return TRI->getSubRegIndexLaneMask(SubReg);
380 }
381 
deadDefHasNoUse(const MachineOperand & MO)382 bool ScheduleDAGInstrs::deadDefHasNoUse(const MachineOperand &MO) {
383   auto RegUse = CurrentVRegUses.find(MO.getReg());
384   if (RegUse == CurrentVRegUses.end())
385     return true;
386   return (RegUse->LaneMask & getLaneMaskForMO(MO)).none();
387 }
388 
389 /// Adds register output and data dependencies from this SUnit to instructions
390 /// that occur later in the same scheduling region if they read from or write to
391 /// the virtual register defined at OperIdx.
392 ///
393 /// TODO: Hoist loop induction variable increments. This has to be
394 /// reevaluated. Generally, IV scheduling should be done before coalescing.
addVRegDefDeps(SUnit * SU,unsigned OperIdx)395 void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
396   MachineInstr *MI = SU->getInstr();
397   MachineOperand &MO = MI->getOperand(OperIdx);
398   Register Reg = MO.getReg();
399 
400   LaneBitmask DefLaneMask;
401   LaneBitmask KillLaneMask;
402   if (TrackLaneMasks) {
403     bool IsKill = MO.getSubReg() == 0 || MO.isUndef();
404     DefLaneMask = getLaneMaskForMO(MO);
405     // If we have a <read-undef> flag, none of the lane values comes from an
406     // earlier instruction.
407     KillLaneMask = IsKill ? LaneBitmask::getAll() : DefLaneMask;
408 
409     if (MO.getSubReg() != 0 && MO.isUndef()) {
410       // There may be other subregister defs on the same instruction of the same
411       // register in later operands. The lanes of other defs will now be live
412       // after this instruction, so these should not be treated as killed by the
413       // instruction even though they appear to be killed in this one operand.
414       for (int I = OperIdx + 1, E = MI->getNumOperands(); I != E; ++I) {
415         const MachineOperand &OtherMO = MI->getOperand(I);
416         if (OtherMO.isReg() && OtherMO.isDef() && OtherMO.getReg() == Reg)
417           KillLaneMask &= ~getLaneMaskForMO(OtherMO);
418       }
419     }
420 
421     // Clear undef flag, we'll re-add it later once we know which subregister
422     // Def is first.
423     MO.setIsUndef(false);
424   } else {
425     DefLaneMask = LaneBitmask::getAll();
426     KillLaneMask = LaneBitmask::getAll();
427   }
428 
429   if (MO.isDead()) {
430     assert(deadDefHasNoUse(MO) && "Dead defs should have no uses");
431   } else {
432     // Add data dependence to all uses we found so far.
433     const TargetSubtargetInfo &ST = MF.getSubtarget();
434     for (VReg2SUnitOperIdxMultiMap::iterator I = CurrentVRegUses.find(Reg),
435          E = CurrentVRegUses.end(); I != E; /*empty*/) {
436       LaneBitmask LaneMask = I->LaneMask;
437       // Ignore uses of other lanes.
438       if ((LaneMask & KillLaneMask).none()) {
439         ++I;
440         continue;
441       }
442 
443       if ((LaneMask & DefLaneMask).any()) {
444         SUnit *UseSU = I->SU;
445         MachineInstr *Use = UseSU->getInstr();
446         SDep Dep(SU, SDep::Data, Reg);
447         Dep.setLatency(SchedModel.computeOperandLatency(MI, OperIdx, Use,
448                                                         I->OperandIndex));
449         ST.adjustSchedDependency(SU, OperIdx, UseSU, I->OperandIndex, Dep);
450         UseSU->addPred(Dep);
451       }
452 
453       LaneMask &= ~KillLaneMask;
454       // If we found a Def for all lanes of this use, remove it from the list.
455       if (LaneMask.any()) {
456         I->LaneMask = LaneMask;
457         ++I;
458       } else
459         I = CurrentVRegUses.erase(I);
460     }
461   }
462 
463   // Shortcut: Singly defined vregs do not have output/anti dependencies.
464   if (MRI.hasOneDef(Reg))
465     return;
466 
467   // Add output dependence to the next nearest defs of this vreg.
468   //
469   // Unless this definition is dead, the output dependence should be
470   // transitively redundant with antidependencies from this definition's
471   // uses. We're conservative for now until we have a way to guarantee the uses
472   // are not eliminated sometime during scheduling. The output dependence edge
473   // is also useful if output latency exceeds def-use latency.
474   LaneBitmask LaneMask = DefLaneMask;
475   for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
476                                      CurrentVRegDefs.end())) {
477     // Ignore defs for other lanes.
478     if ((V2SU.LaneMask & LaneMask).none())
479       continue;
480     // Add an output dependence.
481     SUnit *DefSU = V2SU.SU;
482     // Ignore additional defs of the same lanes in one instruction. This can
483     // happen because lanemasks are shared for targets with too many
484     // subregisters. We also use some representration tricks/hacks where we
485     // add super-register defs/uses, to imply that although we only access parts
486     // of the reg we care about the full one.
487     if (DefSU == SU)
488       continue;
489     SDep Dep(SU, SDep::Output, Reg);
490     Dep.setLatency(
491       SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
492     DefSU->addPred(Dep);
493 
494     // Update current definition. This can get tricky if the def was about a
495     // bigger lanemask before. We then have to shrink it and create a new
496     // VReg2SUnit for the non-overlapping part.
497     LaneBitmask OverlapMask = V2SU.LaneMask & LaneMask;
498     LaneBitmask NonOverlapMask = V2SU.LaneMask & ~LaneMask;
499     V2SU.SU = SU;
500     V2SU.LaneMask = OverlapMask;
501     if (NonOverlapMask.any())
502       CurrentVRegDefs.insert(VReg2SUnit(Reg, NonOverlapMask, DefSU));
503   }
504   // If there was no CurrentVRegDefs entry for some lanes yet, create one.
505   if (LaneMask.any())
506     CurrentVRegDefs.insert(VReg2SUnit(Reg, LaneMask, SU));
507 }
508 
509 /// Adds a register data dependency if the instruction that defines the
510 /// virtual register used at OperIdx is mapped to an SUnit. Add a register
511 /// antidependency from this SUnit to instructions that occur later in the same
512 /// scheduling region if they write the virtual register.
513 ///
514 /// TODO: Handle ExitSU "uses" properly.
addVRegUseDeps(SUnit * SU,unsigned OperIdx)515 void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
516   const MachineInstr *MI = SU->getInstr();
517   assert(!MI->isDebugInstr());
518 
519   const MachineOperand &MO = MI->getOperand(OperIdx);
520   Register Reg = MO.getReg();
521 
522   // Remember the use. Data dependencies will be added when we find the def.
523   LaneBitmask LaneMask = TrackLaneMasks ? getLaneMaskForMO(MO)
524                                         : LaneBitmask::getAll();
525   CurrentVRegUses.insert(VReg2SUnitOperIdx(Reg, LaneMask, OperIdx, SU));
526 
527   // Add antidependences to the following defs of the vreg.
528   for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
529                                      CurrentVRegDefs.end())) {
530     // Ignore defs for unrelated lanes.
531     LaneBitmask PrevDefLaneMask = V2SU.LaneMask;
532     if ((PrevDefLaneMask & LaneMask).none())
533       continue;
534     if (V2SU.SU == SU)
535       continue;
536 
537     V2SU.SU->addPred(SDep(SU, SDep::Anti, Reg));
538   }
539 }
540 
541 /// Returns true if MI is an instruction we are unable to reason about
542 /// (like a call or something with unmodeled side effects).
isGlobalMemoryObject(AAResults * AA,MachineInstr * MI)543 static inline bool isGlobalMemoryObject(AAResults *AA, MachineInstr *MI) {
544   return MI->isCall() || MI->hasUnmodeledSideEffects() ||
545          (MI->hasOrderedMemoryRef() && !MI->isDereferenceableInvariantLoad(AA));
546 }
547 
addChainDependency(SUnit * SUa,SUnit * SUb,unsigned Latency)548 void ScheduleDAGInstrs::addChainDependency (SUnit *SUa, SUnit *SUb,
549                                             unsigned Latency) {
550   if (SUa->getInstr()->mayAlias(AAForDep, *SUb->getInstr(), UseTBAA)) {
551     SDep Dep(SUa, SDep::MayAliasMem);
552     Dep.setLatency(Latency);
553     SUb->addPred(Dep);
554   }
555 }
556 
557 /// Creates an SUnit for each real instruction, numbered in top-down
558 /// topological order. The instruction order A < B, implies that no edge exists
559 /// from B to A.
560 ///
561 /// Map each real instruction to its SUnit.
562 ///
563 /// After initSUnits, the SUnits vector cannot be resized and the scheduler may
564 /// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
565 /// instead of pointers.
566 ///
567 /// MachineScheduler relies on initSUnits numbering the nodes by their order in
568 /// the original instruction list.
initSUnits()569 void ScheduleDAGInstrs::initSUnits() {
570   // We'll be allocating one SUnit for each real instruction in the region,
571   // which is contained within a basic block.
572   SUnits.reserve(NumRegionInstrs);
573 
574   for (MachineInstr &MI : make_range(RegionBegin, RegionEnd)) {
575     if (MI.isDebugInstr())
576       continue;
577 
578     SUnit *SU = newSUnit(&MI);
579     MISUnitMap[&MI] = SU;
580 
581     SU->isCall = MI.isCall();
582     SU->isCommutable = MI.isCommutable();
583 
584     // Assign the Latency field of SU using target-provided information.
585     SU->Latency = SchedModel.computeInstrLatency(SU->getInstr());
586 
587     // If this SUnit uses a reserved or unbuffered resource, mark it as such.
588     //
589     // Reserved resources block an instruction from issuing and stall the
590     // entire pipeline. These are identified by BufferSize=0.
591     //
592     // Unbuffered resources prevent execution of subsequent instructions that
593     // require the same resources. This is used for in-order execution pipelines
594     // within an out-of-order core. These are identified by BufferSize=1.
595     if (SchedModel.hasInstrSchedModel()) {
596       const MCSchedClassDesc *SC = getSchedClass(SU);
597       for (const MCWriteProcResEntry &PRE :
598            make_range(SchedModel.getWriteProcResBegin(SC),
599                       SchedModel.getWriteProcResEnd(SC))) {
600         switch (SchedModel.getProcResource(PRE.ProcResourceIdx)->BufferSize) {
601         case 0:
602           SU->hasReservedResource = true;
603           break;
604         case 1:
605           SU->isUnbuffered = true;
606           break;
607         default:
608           break;
609         }
610       }
611     }
612   }
613 }
614 
615 class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
616   /// Current total number of SUs in map.
617   unsigned NumNodes = 0;
618 
619   /// 1 for loads, 0 for stores. (see comment in SUList)
620   unsigned TrueMemOrderLatency;
621 
622 public:
Value2SUsMap(unsigned lat=0)623   Value2SUsMap(unsigned lat = 0) : TrueMemOrderLatency(lat) {}
624 
625   /// To keep NumNodes up to date, insert() is used instead of
626   /// this operator w/ push_back().
operator [](const SUList & Key)627   ValueType &operator[](const SUList &Key) {
628     llvm_unreachable("Don't use. Use insert() instead."); };
629 
630   /// Adds SU to the SUList of V. If Map grows huge, reduce its size by calling
631   /// reduce().
insert(SUnit * SU,ValueType V)632   void inline insert(SUnit *SU, ValueType V) {
633     MapVector::operator[](V).push_back(SU);
634     NumNodes++;
635   }
636 
637   /// Clears the list of SUs mapped to V.
clearList(ValueType V)638   void inline clearList(ValueType V) {
639     iterator Itr = find(V);
640     if (Itr != end()) {
641       assert(NumNodes >= Itr->second.size());
642       NumNodes -= Itr->second.size();
643 
644       Itr->second.clear();
645     }
646   }
647 
648   /// Clears map from all contents.
clear()649   void clear() {
650     MapVector<ValueType, SUList>::clear();
651     NumNodes = 0;
652   }
653 
size() const654   unsigned inline size() const { return NumNodes; }
655 
656   /// Counts the number of SUs in this map after a reduction.
reComputeSize()657   void reComputeSize() {
658     NumNodes = 0;
659     for (auto &I : *this)
660       NumNodes += I.second.size();
661   }
662 
getTrueMemOrderLatency() const663   unsigned inline getTrueMemOrderLatency() const {
664     return TrueMemOrderLatency;
665   }
666 
667   void dump();
668 };
669 
addChainDependencies(SUnit * SU,Value2SUsMap & Val2SUsMap)670 void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
671                                              Value2SUsMap &Val2SUsMap) {
672   for (auto &I : Val2SUsMap)
673     addChainDependencies(SU, I.second,
674                          Val2SUsMap.getTrueMemOrderLatency());
675 }
676 
addChainDependencies(SUnit * SU,Value2SUsMap & Val2SUsMap,ValueType V)677 void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
678                                              Value2SUsMap &Val2SUsMap,
679                                              ValueType V) {
680   Value2SUsMap::iterator Itr = Val2SUsMap.find(V);
681   if (Itr != Val2SUsMap.end())
682     addChainDependencies(SU, Itr->second,
683                          Val2SUsMap.getTrueMemOrderLatency());
684 }
685 
addBarrierChain(Value2SUsMap & map)686 void ScheduleDAGInstrs::addBarrierChain(Value2SUsMap &map) {
687   assert(BarrierChain != nullptr);
688 
689   for (auto &I : map) {
690     SUList &sus = I.second;
691     for (auto *SU : sus)
692       SU->addPredBarrier(BarrierChain);
693   }
694   map.clear();
695 }
696 
insertBarrierChain(Value2SUsMap & map)697 void ScheduleDAGInstrs::insertBarrierChain(Value2SUsMap &map) {
698   assert(BarrierChain != nullptr);
699 
700   // Go through all lists of SUs.
701   for (Value2SUsMap::iterator I = map.begin(), EE = map.end(); I != EE;) {
702     Value2SUsMap::iterator CurrItr = I++;
703     SUList &sus = CurrItr->second;
704     SUList::iterator SUItr = sus.begin(), SUEE = sus.end();
705     for (; SUItr != SUEE; ++SUItr) {
706       // Stop on BarrierChain or any instruction above it.
707       if ((*SUItr)->NodeNum <= BarrierChain->NodeNum)
708         break;
709 
710       (*SUItr)->addPredBarrier(BarrierChain);
711     }
712 
713     // Remove also the BarrierChain from list if present.
714     if (SUItr != SUEE && *SUItr == BarrierChain)
715       SUItr++;
716 
717     // Remove all SUs that are now successors of BarrierChain.
718     if (SUItr != sus.begin())
719       sus.erase(sus.begin(), SUItr);
720   }
721 
722   // Remove all entries with empty su lists.
723   map.remove_if([&](std::pair<ValueType, SUList> &mapEntry) {
724       return (mapEntry.second.empty()); });
725 
726   // Recompute the size of the map (NumNodes).
727   map.reComputeSize();
728 }
729 
buildSchedGraph(AAResults * AA,RegPressureTracker * RPTracker,PressureDiffs * PDiffs,LiveIntervals * LIS,bool TrackLaneMasks)730 void ScheduleDAGInstrs::buildSchedGraph(AAResults *AA,
731                                         RegPressureTracker *RPTracker,
732                                         PressureDiffs *PDiffs,
733                                         LiveIntervals *LIS,
734                                         bool TrackLaneMasks) {
735   const TargetSubtargetInfo &ST = MF.getSubtarget();
736   bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI
737                                                        : ST.useAA();
738   AAForDep = UseAA ? AA : nullptr;
739 
740   BarrierChain = nullptr;
741 
742   this->TrackLaneMasks = TrackLaneMasks;
743   MISUnitMap.clear();
744   ScheduleDAG::clearDAG();
745 
746   // Create an SUnit for each real instruction.
747   initSUnits();
748 
749   if (PDiffs)
750     PDiffs->init(SUnits.size());
751 
752   // We build scheduling units by walking a block's instruction list
753   // from bottom to top.
754 
755   // Each MIs' memory operand(s) is analyzed to a list of underlying
756   // objects. The SU is then inserted in the SUList(s) mapped from the
757   // Value(s). Each Value thus gets mapped to lists of SUs depending
758   // on it, stores and loads kept separately. Two SUs are trivially
759   // non-aliasing if they both depend on only identified Values and do
760   // not share any common Value.
761   Value2SUsMap Stores, Loads(1 /*TrueMemOrderLatency*/);
762 
763   // Certain memory accesses are known to not alias any SU in Stores
764   // or Loads, and have therefore their own 'NonAlias'
765   // domain. E.g. spill / reload instructions never alias LLVM I/R
766   // Values. It would be nice to assume that this type of memory
767   // accesses always have a proper memory operand modelling, and are
768   // therefore never unanalyzable, but this is conservatively not
769   // done.
770   Value2SUsMap NonAliasStores, NonAliasLoads(1 /*TrueMemOrderLatency*/);
771 
772   // Track all instructions that may raise floating-point exceptions.
773   // These do not depend on one other (or normal loads or stores), but
774   // must not be rescheduled across global barriers.  Note that we don't
775   // really need a "map" here since we don't track those MIs by value;
776   // using the same Value2SUsMap data type here is simply a matter of
777   // convenience.
778   Value2SUsMap FPExceptions;
779 
780   // Remove any stale debug info; sometimes BuildSchedGraph is called again
781   // without emitting the info from the previous call.
782   DbgValues.clear();
783   FirstDbgValue = nullptr;
784 
785   assert(Defs.empty() && Uses.empty() &&
786          "Only BuildGraph should update Defs/Uses");
787   Defs.setUniverse(TRI->getNumRegs());
788   Uses.setUniverse(TRI->getNumRegs());
789 
790   assert(CurrentVRegDefs.empty() && "nobody else should use CurrentVRegDefs");
791   assert(CurrentVRegUses.empty() && "nobody else should use CurrentVRegUses");
792   unsigned NumVirtRegs = MRI.getNumVirtRegs();
793   CurrentVRegDefs.setUniverse(NumVirtRegs);
794   CurrentVRegUses.setUniverse(NumVirtRegs);
795 
796   // Model data dependencies between instructions being scheduled and the
797   // ExitSU.
798   addSchedBarrierDeps();
799 
800   // Walk the list of instructions, from bottom moving up.
801   MachineInstr *DbgMI = nullptr;
802   for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
803        MII != MIE; --MII) {
804     MachineInstr &MI = *std::prev(MII);
805     if (DbgMI) {
806       DbgValues.push_back(std::make_pair(DbgMI, &MI));
807       DbgMI = nullptr;
808     }
809 
810     if (MI.isDebugValue() || MI.isDebugRef()) {
811       DbgMI = &MI;
812       continue;
813     }
814     if (MI.isDebugLabel())
815       continue;
816 
817     SUnit *SU = MISUnitMap[&MI];
818     assert(SU && "No SUnit mapped to this MI");
819 
820     if (RPTracker) {
821       RegisterOperands RegOpers;
822       RegOpers.collect(MI, *TRI, MRI, TrackLaneMasks, false);
823       if (TrackLaneMasks) {
824         SlotIndex SlotIdx = LIS->getInstructionIndex(MI);
825         RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx);
826       }
827       if (PDiffs != nullptr)
828         PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI);
829 
830       if (RPTracker->getPos() == RegionEnd || &*RPTracker->getPos() != &MI)
831         RPTracker->recedeSkipDebugValues();
832       assert(&*RPTracker->getPos() == &MI && "RPTracker in sync");
833       RPTracker->recede(RegOpers);
834     }
835 
836     assert(
837         (CanHandleTerminators || (!MI.isTerminator() && !MI.isPosition())) &&
838         "Cannot schedule terminators or labels!");
839 
840     // Add register-based dependencies (data, anti, and output).
841     // For some instructions (calls, returns, inline-asm, etc.) there can
842     // be explicit uses and implicit defs, in which case the use will appear
843     // on the operand list before the def. Do two passes over the operand
844     // list to make sure that defs are processed before any uses.
845     bool HasVRegDef = false;
846     for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
847       const MachineOperand &MO = MI.getOperand(j);
848       if (!MO.isReg() || !MO.isDef())
849         continue;
850       Register Reg = MO.getReg();
851       if (Register::isPhysicalRegister(Reg)) {
852         addPhysRegDeps(SU, j);
853       } else if (Register::isVirtualRegister(Reg)) {
854         HasVRegDef = true;
855         addVRegDefDeps(SU, j);
856       }
857     }
858     // Now process all uses.
859     for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
860       const MachineOperand &MO = MI.getOperand(j);
861       // Only look at use operands.
862       // We do not need to check for MO.readsReg() here because subsequent
863       // subregister defs will get output dependence edges and need no
864       // additional use dependencies.
865       if (!MO.isReg() || !MO.isUse())
866         continue;
867       Register Reg = MO.getReg();
868       if (Register::isPhysicalRegister(Reg)) {
869         addPhysRegDeps(SU, j);
870       } else if (Register::isVirtualRegister(Reg) && MO.readsReg()) {
871         addVRegUseDeps(SU, j);
872       }
873     }
874 
875     // If we haven't seen any uses in this scheduling region, create a
876     // dependence edge to ExitSU to model the live-out latency. This is required
877     // for vreg defs with no in-region use, and prefetches with no vreg def.
878     //
879     // FIXME: NumDataSuccs would be more precise than NumSuccs here. This
880     // check currently relies on being called before adding chain deps.
881     if (SU->NumSuccs == 0 && SU->Latency > 1 && (HasVRegDef || MI.mayLoad())) {
882       SDep Dep(SU, SDep::Artificial);
883       Dep.setLatency(SU->Latency - 1);
884       ExitSU.addPred(Dep);
885     }
886 
887     // Add memory dependencies (Note: isStoreToStackSlot and
888     // isLoadFromStackSLot are not usable after stack slots are lowered to
889     // actual addresses).
890 
891     // This is a barrier event that acts as a pivotal node in the DAG.
892     if (isGlobalMemoryObject(AA, &MI)) {
893 
894       // Become the barrier chain.
895       if (BarrierChain)
896         BarrierChain->addPredBarrier(SU);
897       BarrierChain = SU;
898 
899       LLVM_DEBUG(dbgs() << "Global memory object and new barrier chain: SU("
900                         << BarrierChain->NodeNum << ").\n";);
901 
902       // Add dependencies against everything below it and clear maps.
903       addBarrierChain(Stores);
904       addBarrierChain(Loads);
905       addBarrierChain(NonAliasStores);
906       addBarrierChain(NonAliasLoads);
907       addBarrierChain(FPExceptions);
908 
909       continue;
910     }
911 
912     // Instructions that may raise FP exceptions may not be moved
913     // across any global barriers.
914     if (MI.mayRaiseFPException()) {
915       if (BarrierChain)
916         BarrierChain->addPredBarrier(SU);
917 
918       FPExceptions.insert(SU, UnknownValue);
919 
920       if (FPExceptions.size() >= HugeRegion) {
921         LLVM_DEBUG(dbgs() << "Reducing FPExceptions map.\n";);
922         Value2SUsMap empty;
923         reduceHugeMemNodeMaps(FPExceptions, empty, getReductionSize());
924       }
925     }
926 
927     // If it's not a store or a variant load, we're done.
928     if (!MI.mayStore() &&
929         !(MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA)))
930       continue;
931 
932     // Always add dependecy edge to BarrierChain if present.
933     if (BarrierChain)
934       BarrierChain->addPredBarrier(SU);
935 
936     // Find the underlying objects for MI. The Objs vector is either
937     // empty, or filled with the Values of memory locations which this
938     // SU depends on.
939     UnderlyingObjectsVector Objs;
940     bool ObjsFound = getUnderlyingObjectsForInstr(&MI, MFI, Objs,
941                                                   MF.getDataLayout());
942 
943     if (MI.mayStore()) {
944       if (!ObjsFound) {
945         // An unknown store depends on all stores and loads.
946         addChainDependencies(SU, Stores);
947         addChainDependencies(SU, NonAliasStores);
948         addChainDependencies(SU, Loads);
949         addChainDependencies(SU, NonAliasLoads);
950 
951         // Map this store to 'UnknownValue'.
952         Stores.insert(SU, UnknownValue);
953       } else {
954         // Add precise dependencies against all previously seen memory
955         // accesses mapped to the same Value(s).
956         for (const UnderlyingObject &UnderlObj : Objs) {
957           ValueType V = UnderlObj.getValue();
958           bool ThisMayAlias = UnderlObj.mayAlias();
959 
960           // Add dependencies to previous stores and loads mapped to V.
961           addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
962           addChainDependencies(SU, (ThisMayAlias ? Loads : NonAliasLoads), V);
963         }
964         // Update the store map after all chains have been added to avoid adding
965         // self-loop edge if multiple underlying objects are present.
966         for (const UnderlyingObject &UnderlObj : Objs) {
967           ValueType V = UnderlObj.getValue();
968           bool ThisMayAlias = UnderlObj.mayAlias();
969 
970           // Map this store to V.
971           (ThisMayAlias ? Stores : NonAliasStores).insert(SU, V);
972         }
973         // The store may have dependencies to unanalyzable loads and
974         // stores.
975         addChainDependencies(SU, Loads, UnknownValue);
976         addChainDependencies(SU, Stores, UnknownValue);
977       }
978     } else { // SU is a load.
979       if (!ObjsFound) {
980         // An unknown load depends on all stores.
981         addChainDependencies(SU, Stores);
982         addChainDependencies(SU, NonAliasStores);
983 
984         Loads.insert(SU, UnknownValue);
985       } else {
986         for (const UnderlyingObject &UnderlObj : Objs) {
987           ValueType V = UnderlObj.getValue();
988           bool ThisMayAlias = UnderlObj.mayAlias();
989 
990           // Add precise dependencies against all previously seen stores
991           // mapping to the same Value(s).
992           addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
993 
994           // Map this load to V.
995           (ThisMayAlias ? Loads : NonAliasLoads).insert(SU, V);
996         }
997         // The load may have dependencies to unanalyzable stores.
998         addChainDependencies(SU, Stores, UnknownValue);
999       }
1000     }
1001 
1002     // Reduce maps if they grow huge.
1003     if (Stores.size() + Loads.size() >= HugeRegion) {
1004       LLVM_DEBUG(dbgs() << "Reducing Stores and Loads maps.\n";);
1005       reduceHugeMemNodeMaps(Stores, Loads, getReductionSize());
1006     }
1007     if (NonAliasStores.size() + NonAliasLoads.size() >= HugeRegion) {
1008       LLVM_DEBUG(
1009           dbgs() << "Reducing NonAliasStores and NonAliasLoads maps.\n";);
1010       reduceHugeMemNodeMaps(NonAliasStores, NonAliasLoads, getReductionSize());
1011     }
1012   }
1013 
1014   if (DbgMI)
1015     FirstDbgValue = DbgMI;
1016 
1017   Defs.clear();
1018   Uses.clear();
1019   CurrentVRegDefs.clear();
1020   CurrentVRegUses.clear();
1021 
1022   Topo.MarkDirty();
1023 }
1024 
operator <<(raw_ostream & OS,const PseudoSourceValue * PSV)1025 raw_ostream &llvm::operator<<(raw_ostream &OS, const PseudoSourceValue* PSV) {
1026   PSV->printCustom(OS);
1027   return OS;
1028 }
1029 
dump()1030 void ScheduleDAGInstrs::Value2SUsMap::dump() {
1031   for (auto &Itr : *this) {
1032     if (Itr.first.is<const Value*>()) {
1033       const Value *V = Itr.first.get<const Value*>();
1034       if (isa<UndefValue>(V))
1035         dbgs() << "Unknown";
1036       else
1037         V->printAsOperand(dbgs());
1038     }
1039     else if (Itr.first.is<const PseudoSourceValue*>())
1040       dbgs() <<  Itr.first.get<const PseudoSourceValue*>();
1041     else
1042       llvm_unreachable("Unknown Value type.");
1043 
1044     dbgs() << " : ";
1045     dumpSUList(Itr.second);
1046   }
1047 }
1048 
reduceHugeMemNodeMaps(Value2SUsMap & stores,Value2SUsMap & loads,unsigned N)1049 void ScheduleDAGInstrs::reduceHugeMemNodeMaps(Value2SUsMap &stores,
1050                                               Value2SUsMap &loads, unsigned N) {
1051   LLVM_DEBUG(dbgs() << "Before reduction:\nStoring SUnits:\n"; stores.dump();
1052              dbgs() << "Loading SUnits:\n"; loads.dump());
1053 
1054   // Insert all SU's NodeNums into a vector and sort it.
1055   std::vector<unsigned> NodeNums;
1056   NodeNums.reserve(stores.size() + loads.size());
1057   for (auto &I : stores)
1058     for (auto *SU : I.second)
1059       NodeNums.push_back(SU->NodeNum);
1060   for (auto &I : loads)
1061     for (auto *SU : I.second)
1062       NodeNums.push_back(SU->NodeNum);
1063   llvm::sort(NodeNums);
1064 
1065   // The N last elements in NodeNums will be removed, and the SU with
1066   // the lowest NodeNum of them will become the new BarrierChain to
1067   // let the not yet seen SUs have a dependency to the removed SUs.
1068   assert(N <= NodeNums.size());
1069   SUnit *newBarrierChain = &SUnits[*(NodeNums.end() - N)];
1070   if (BarrierChain) {
1071     // The aliasing and non-aliasing maps reduce independently of each
1072     // other, but share a common BarrierChain. Check if the
1073     // newBarrierChain is above the former one. If it is not, it may
1074     // introduce a loop to use newBarrierChain, so keep the old one.
1075     if (newBarrierChain->NodeNum < BarrierChain->NodeNum) {
1076       BarrierChain->addPredBarrier(newBarrierChain);
1077       BarrierChain = newBarrierChain;
1078       LLVM_DEBUG(dbgs() << "Inserting new barrier chain: SU("
1079                         << BarrierChain->NodeNum << ").\n";);
1080     }
1081     else
1082       LLVM_DEBUG(dbgs() << "Keeping old barrier chain: SU("
1083                         << BarrierChain->NodeNum << ").\n";);
1084   }
1085   else
1086     BarrierChain = newBarrierChain;
1087 
1088   insertBarrierChain(stores);
1089   insertBarrierChain(loads);
1090 
1091   LLVM_DEBUG(dbgs() << "After reduction:\nStoring SUnits:\n"; stores.dump();
1092              dbgs() << "Loading SUnits:\n"; loads.dump());
1093 }
1094 
toggleKills(const MachineRegisterInfo & MRI,LivePhysRegs & LiveRegs,MachineInstr & MI,bool addToLiveRegs)1095 static void toggleKills(const MachineRegisterInfo &MRI, LivePhysRegs &LiveRegs,
1096                         MachineInstr &MI, bool addToLiveRegs) {
1097   for (MachineOperand &MO : MI.operands()) {
1098     if (!MO.isReg() || !MO.readsReg())
1099       continue;
1100     Register Reg = MO.getReg();
1101     if (!Reg)
1102       continue;
1103 
1104     // Things that are available after the instruction are killed by it.
1105     bool IsKill = LiveRegs.available(MRI, Reg);
1106     MO.setIsKill(IsKill);
1107     if (addToLiveRegs)
1108       LiveRegs.addReg(Reg);
1109   }
1110 }
1111 
fixupKills(MachineBasicBlock & MBB)1112 void ScheduleDAGInstrs::fixupKills(MachineBasicBlock &MBB) {
1113   LLVM_DEBUG(dbgs() << "Fixup kills for " << printMBBReference(MBB) << '\n');
1114 
1115   LiveRegs.init(*TRI);
1116   LiveRegs.addLiveOuts(MBB);
1117 
1118   // Examine block from end to start...
1119   for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
1120     if (MI.isDebugInstr())
1121       continue;
1122 
1123     // Update liveness.  Registers that are defed but not used in this
1124     // instruction are now dead. Mark register and all subregs as they
1125     // are completely defined.
1126     for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
1127       const MachineOperand &MO = *O;
1128       if (MO.isReg()) {
1129         if (!MO.isDef())
1130           continue;
1131         Register Reg = MO.getReg();
1132         if (!Reg)
1133           continue;
1134         LiveRegs.removeReg(Reg);
1135       } else if (MO.isRegMask()) {
1136         LiveRegs.removeRegsInMask(MO);
1137       }
1138     }
1139 
1140     // If there is a bundle header fix it up first.
1141     if (!MI.isBundled()) {
1142       toggleKills(MRI, LiveRegs, MI, true);
1143     } else {
1144       MachineBasicBlock::instr_iterator Bundle = MI.getIterator();
1145       if (MI.isBundle())
1146         toggleKills(MRI, LiveRegs, MI, false);
1147 
1148       // Some targets make the (questionable) assumtion that the instructions
1149       // inside the bundle are ordered and consequently only the last use of
1150       // a register inside the bundle can kill it.
1151       MachineBasicBlock::instr_iterator I = std::next(Bundle);
1152       while (I->isBundledWithSucc())
1153         ++I;
1154       do {
1155         if (!I->isDebugInstr())
1156           toggleKills(MRI, LiveRegs, *I, true);
1157         --I;
1158       } while (I != Bundle);
1159     }
1160   }
1161 }
1162 
dumpNode(const SUnit & SU) const1163 void ScheduleDAGInstrs::dumpNode(const SUnit &SU) const {
1164 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1165   dumpNodeName(SU);
1166   dbgs() << ": ";
1167   SU.getInstr()->dump();
1168 #endif
1169 }
1170 
dump() const1171 void ScheduleDAGInstrs::dump() const {
1172 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1173   if (EntrySU.getInstr() != nullptr)
1174     dumpNodeAll(EntrySU);
1175   for (const SUnit &SU : SUnits)
1176     dumpNodeAll(SU);
1177   if (ExitSU.getInstr() != nullptr)
1178     dumpNodeAll(ExitSU);
1179 #endif
1180 }
1181 
getGraphNodeLabel(const SUnit * SU) const1182 std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
1183   std::string s;
1184   raw_string_ostream oss(s);
1185   if (SU == &EntrySU)
1186     oss << "<entry>";
1187   else if (SU == &ExitSU)
1188     oss << "<exit>";
1189   else
1190     SU->getInstr()->print(oss, /*IsStandalone=*/true);
1191   return oss.str();
1192 }
1193 
1194 /// Return the basic block label. It is not necessarilly unique because a block
1195 /// contains multiple scheduling regions. But it is fine for visualization.
getDAGName() const1196 std::string ScheduleDAGInstrs::getDAGName() const {
1197   return "dag." + BB->getFullName();
1198 }
1199 
canAddEdge(SUnit * SuccSU,SUnit * PredSU)1200 bool ScheduleDAGInstrs::canAddEdge(SUnit *SuccSU, SUnit *PredSU) {
1201   return SuccSU == &ExitSU || !Topo.IsReachable(PredSU, SuccSU);
1202 }
1203 
addEdge(SUnit * SuccSU,const SDep & PredDep)1204 bool ScheduleDAGInstrs::addEdge(SUnit *SuccSU, const SDep &PredDep) {
1205   if (SuccSU != &ExitSU) {
1206     // Do not use WillCreateCycle, it assumes SD scheduling.
1207     // If Pred is reachable from Succ, then the edge creates a cycle.
1208     if (Topo.IsReachable(PredDep.getSUnit(), SuccSU))
1209       return false;
1210     Topo.AddPredQueued(SuccSU, PredDep.getSUnit());
1211   }
1212   SuccSU->addPred(PredDep, /*Required=*/!PredDep.isArtificial());
1213   // Return true regardless of whether a new edge needed to be inserted.
1214   return true;
1215 }
1216 
1217 //===----------------------------------------------------------------------===//
1218 // SchedDFSResult Implementation
1219 //===----------------------------------------------------------------------===//
1220 
1221 namespace llvm {
1222 
1223 /// Internal state used to compute SchedDFSResult.
1224 class SchedDFSImpl {
1225   SchedDFSResult &R;
1226 
1227   /// Join DAG nodes into equivalence classes by their subtree.
1228   IntEqClasses SubtreeClasses;
1229   /// List PredSU, SuccSU pairs that represent data edges between subtrees.
1230   std::vector<std::pair<const SUnit *, const SUnit*>> ConnectionPairs;
1231 
1232   struct RootData {
1233     unsigned NodeID;
1234     unsigned ParentNodeID;  ///< Parent node (member of the parent subtree).
1235     unsigned SubInstrCount = 0; ///< Instr count in this tree only, not
1236                                 /// children.
1237 
RootDatallvm::SchedDFSImpl::RootData1238     RootData(unsigned id): NodeID(id),
1239                            ParentNodeID(SchedDFSResult::InvalidSubtreeID) {}
1240 
getSparseSetIndexllvm::SchedDFSImpl::RootData1241     unsigned getSparseSetIndex() const { return NodeID; }
1242   };
1243 
1244   SparseSet<RootData> RootSet;
1245 
1246 public:
SchedDFSImpl(SchedDFSResult & r)1247   SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) {
1248     RootSet.setUniverse(R.DFSNodeData.size());
1249   }
1250 
1251   /// Returns true if this node been visited by the DFS traversal.
1252   ///
1253   /// During visitPostorderNode the Node's SubtreeID is assigned to the Node
1254   /// ID. Later, SubtreeID is updated but remains valid.
isVisited(const SUnit * SU) const1255   bool isVisited(const SUnit *SU) const {
1256     return R.DFSNodeData[SU->NodeNum].SubtreeID
1257       != SchedDFSResult::InvalidSubtreeID;
1258   }
1259 
1260   /// Initializes this node's instruction count. We don't need to flag the node
1261   /// visited until visitPostorder because the DAG cannot have cycles.
visitPreorder(const SUnit * SU)1262   void visitPreorder(const SUnit *SU) {
1263     R.DFSNodeData[SU->NodeNum].InstrCount =
1264       SU->getInstr()->isTransient() ? 0 : 1;
1265   }
1266 
1267   /// Called once for each node after all predecessors are visited. Revisit this
1268   /// node's predecessors and potentially join them now that we know the ILP of
1269   /// the other predecessors.
visitPostorderNode(const SUnit * SU)1270   void visitPostorderNode(const SUnit *SU) {
1271     // Mark this node as the root of a subtree. It may be joined with its
1272     // successors later.
1273     R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum;
1274     RootData RData(SU->NodeNum);
1275     RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1;
1276 
1277     // If any predecessors are still in their own subtree, they either cannot be
1278     // joined or are large enough to remain separate. If this parent node's
1279     // total instruction count is not greater than a child subtree by at least
1280     // the subtree limit, then try to join it now since splitting subtrees is
1281     // only useful if multiple high-pressure paths are possible.
1282     unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount;
1283     for (const SDep &PredDep : SU->Preds) {
1284       if (PredDep.getKind() != SDep::Data)
1285         continue;
1286       unsigned PredNum = PredDep.getSUnit()->NodeNum;
1287       if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit)
1288         joinPredSubtree(PredDep, SU, /*CheckLimit=*/false);
1289 
1290       // Either link or merge the TreeData entry from the child to the parent.
1291       if (R.DFSNodeData[PredNum].SubtreeID == PredNum) {
1292         // If the predecessor's parent is invalid, this is a tree edge and the
1293         // current node is the parent.
1294         if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID)
1295           RootSet[PredNum].ParentNodeID = SU->NodeNum;
1296       }
1297       else if (RootSet.count(PredNum)) {
1298         // The predecessor is not a root, but is still in the root set. This
1299         // must be the new parent that it was just joined to. Note that
1300         // RootSet[PredNum].ParentNodeID may either be invalid or may still be
1301         // set to the original parent.
1302         RData.SubInstrCount += RootSet[PredNum].SubInstrCount;
1303         RootSet.erase(PredNum);
1304       }
1305     }
1306     RootSet[SU->NodeNum] = RData;
1307   }
1308 
1309   /// Called once for each tree edge after calling visitPostOrderNode on
1310   /// the predecessor. Increment the parent node's instruction count and
1311   /// preemptively join this subtree to its parent's if it is small enough.
visitPostorderEdge(const SDep & PredDep,const SUnit * Succ)1312   void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) {
1313     R.DFSNodeData[Succ->NodeNum].InstrCount
1314       += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount;
1315     joinPredSubtree(PredDep, Succ);
1316   }
1317 
1318   /// Adds a connection for cross edges.
visitCrossEdge(const SDep & PredDep,const SUnit * Succ)1319   void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) {
1320     ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ));
1321   }
1322 
1323   /// Sets each node's subtree ID to the representative ID and record
1324   /// connections between trees.
finalize()1325   void finalize() {
1326     SubtreeClasses.compress();
1327     R.DFSTreeData.resize(SubtreeClasses.getNumClasses());
1328     assert(SubtreeClasses.getNumClasses() == RootSet.size()
1329            && "number of roots should match trees");
1330     for (const RootData &Root : RootSet) {
1331       unsigned TreeID = SubtreeClasses[Root.NodeID];
1332       if (Root.ParentNodeID != SchedDFSResult::InvalidSubtreeID)
1333         R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[Root.ParentNodeID];
1334       R.DFSTreeData[TreeID].SubInstrCount = Root.SubInstrCount;
1335       // Note that SubInstrCount may be greater than InstrCount if we joined
1336       // subtrees across a cross edge. InstrCount will be attributed to the
1337       // original parent, while SubInstrCount will be attributed to the joined
1338       // parent.
1339     }
1340     R.SubtreeConnections.resize(SubtreeClasses.getNumClasses());
1341     R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses());
1342     LLVM_DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n");
1343     for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) {
1344       R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx];
1345       LLVM_DEBUG(dbgs() << "  SU(" << Idx << ") in tree "
1346                         << R.DFSNodeData[Idx].SubtreeID << '\n');
1347     }
1348     for (const std::pair<const SUnit*, const SUnit*> &P : ConnectionPairs) {
1349       unsigned PredTree = SubtreeClasses[P.first->NodeNum];
1350       unsigned SuccTree = SubtreeClasses[P.second->NodeNum];
1351       if (PredTree == SuccTree)
1352         continue;
1353       unsigned Depth = P.first->getDepth();
1354       addConnection(PredTree, SuccTree, Depth);
1355       addConnection(SuccTree, PredTree, Depth);
1356     }
1357   }
1358 
1359 protected:
1360   /// Joins the predecessor subtree with the successor that is its DFS parent.
1361   /// Applies some heuristics before joining.
joinPredSubtree(const SDep & PredDep,const SUnit * Succ,bool CheckLimit=true)1362   bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ,
1363                        bool CheckLimit = true) {
1364     assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges");
1365 
1366     // Check if the predecessor is already joined.
1367     const SUnit *PredSU = PredDep.getSUnit();
1368     unsigned PredNum = PredSU->NodeNum;
1369     if (R.DFSNodeData[PredNum].SubtreeID != PredNum)
1370       return false;
1371 
1372     // Four is the magic number of successors before a node is considered a
1373     // pinch point.
1374     unsigned NumDataSucs = 0;
1375     for (const SDep &SuccDep : PredSU->Succs) {
1376       if (SuccDep.getKind() == SDep::Data) {
1377         if (++NumDataSucs >= 4)
1378           return false;
1379       }
1380     }
1381     if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit)
1382       return false;
1383     R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum;
1384     SubtreeClasses.join(Succ->NodeNum, PredNum);
1385     return true;
1386   }
1387 
1388   /// Called by finalize() to record a connection between trees.
addConnection(unsigned FromTree,unsigned ToTree,unsigned Depth)1389   void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) {
1390     if (!Depth)
1391       return;
1392 
1393     do {
1394       SmallVectorImpl<SchedDFSResult::Connection> &Connections =
1395         R.SubtreeConnections[FromTree];
1396       for (SchedDFSResult::Connection &C : Connections) {
1397         if (C.TreeID == ToTree) {
1398           C.Level = std::max(C.Level, Depth);
1399           return;
1400         }
1401       }
1402       Connections.push_back(SchedDFSResult::Connection(ToTree, Depth));
1403       FromTree = R.DFSTreeData[FromTree].ParentTreeID;
1404     } while (FromTree != SchedDFSResult::InvalidSubtreeID);
1405   }
1406 };
1407 
1408 } // end namespace llvm
1409 
1410 namespace {
1411 
1412 /// Manage the stack used by a reverse depth-first search over the DAG.
1413 class SchedDAGReverseDFS {
1414   std::vector<std::pair<const SUnit *, SUnit::const_pred_iterator>> DFSStack;
1415 
1416 public:
isComplete() const1417   bool isComplete() const { return DFSStack.empty(); }
1418 
follow(const SUnit * SU)1419   void follow(const SUnit *SU) {
1420     DFSStack.push_back(std::make_pair(SU, SU->Preds.begin()));
1421   }
advance()1422   void advance() { ++DFSStack.back().second; }
1423 
backtrack()1424   const SDep *backtrack() {
1425     DFSStack.pop_back();
1426     return DFSStack.empty() ? nullptr : std::prev(DFSStack.back().second);
1427   }
1428 
getCurr() const1429   const SUnit *getCurr() const { return DFSStack.back().first; }
1430 
getPred() const1431   SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; }
1432 
getPredEnd() const1433   SUnit::const_pred_iterator getPredEnd() const {
1434     return getCurr()->Preds.end();
1435   }
1436 };
1437 
1438 } // end anonymous namespace
1439 
hasDataSucc(const SUnit * SU)1440 static bool hasDataSucc(const SUnit *SU) {
1441   for (const SDep &SuccDep : SU->Succs) {
1442     if (SuccDep.getKind() == SDep::Data &&
1443         !SuccDep.getSUnit()->isBoundaryNode())
1444       return true;
1445   }
1446   return false;
1447 }
1448 
1449 /// Computes an ILP metric for all nodes in the subDAG reachable via depth-first
1450 /// search from this root.
compute(ArrayRef<SUnit> SUnits)1451 void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) {
1452   if (!IsBottomUp)
1453     llvm_unreachable("Top-down ILP metric is unimplemented");
1454 
1455   SchedDFSImpl Impl(*this);
1456   for (const SUnit &SU : SUnits) {
1457     if (Impl.isVisited(&SU) || hasDataSucc(&SU))
1458       continue;
1459 
1460     SchedDAGReverseDFS DFS;
1461     Impl.visitPreorder(&SU);
1462     DFS.follow(&SU);
1463     while (true) {
1464       // Traverse the leftmost path as far as possible.
1465       while (DFS.getPred() != DFS.getPredEnd()) {
1466         const SDep &PredDep = *DFS.getPred();
1467         DFS.advance();
1468         // Ignore non-data edges.
1469         if (PredDep.getKind() != SDep::Data
1470             || PredDep.getSUnit()->isBoundaryNode()) {
1471           continue;
1472         }
1473         // An already visited edge is a cross edge, assuming an acyclic DAG.
1474         if (Impl.isVisited(PredDep.getSUnit())) {
1475           Impl.visitCrossEdge(PredDep, DFS.getCurr());
1476           continue;
1477         }
1478         Impl.visitPreorder(PredDep.getSUnit());
1479         DFS.follow(PredDep.getSUnit());
1480       }
1481       // Visit the top of the stack in postorder and backtrack.
1482       const SUnit *Child = DFS.getCurr();
1483       const SDep *PredDep = DFS.backtrack();
1484       Impl.visitPostorderNode(Child);
1485       if (PredDep)
1486         Impl.visitPostorderEdge(*PredDep, DFS.getCurr());
1487       if (DFS.isComplete())
1488         break;
1489     }
1490   }
1491   Impl.finalize();
1492 }
1493 
1494 /// The root of the given SubtreeID was just scheduled. For all subtrees
1495 /// connected to this tree, record the depth of the connection so that the
1496 /// nearest connected subtrees can be prioritized.
scheduleTree(unsigned SubtreeID)1497 void SchedDFSResult::scheduleTree(unsigned SubtreeID) {
1498   for (const Connection &C : SubtreeConnections[SubtreeID]) {
1499     SubtreeConnectLevels[C.TreeID] =
1500       std::max(SubtreeConnectLevels[C.TreeID], C.Level);
1501     LLVM_DEBUG(dbgs() << "  Tree: " << C.TreeID << " @"
1502                       << SubtreeConnectLevels[C.TreeID] << '\n');
1503   }
1504 }
1505 
1506 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
print(raw_ostream & OS) const1507 LLVM_DUMP_METHOD void ILPValue::print(raw_ostream &OS) const {
1508   OS << InstrCount << " / " << Length << " = ";
1509   if (!Length)
1510     OS << "BADILP";
1511   else
1512     OS << format("%g", ((double)InstrCount / Length));
1513 }
1514 
dump() const1515 LLVM_DUMP_METHOD void ILPValue::dump() const {
1516   dbgs() << *this << '\n';
1517 }
1518 
1519 namespace llvm {
1520 
1521 LLVM_DUMP_METHOD
operator <<(raw_ostream & OS,const ILPValue & Val)1522 raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) {
1523   Val.print(OS);
1524   return OS;
1525 }
1526 
1527 } // end namespace llvm
1528 
1529 #endif
1530