MachinePipeliner.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MachinePipeliner.h - Machine Software Pipeliner Pass -------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // An implementation of the Swing Modulo Scheduling (SMS) software pipeliner.
  15. //
  16. // Software pipelining (SWP) is an instruction scheduling technique for loops
  17. // that overlap loop iterations and exploits ILP via a compiler transformation.
  18. //
  19. // Swing Modulo Scheduling is an implementation of software pipelining
  20. // that generates schedules that are near optimal in terms of initiation
  21. // interval, register requirements, and stage count. See the papers:
  22. //
  23. // "Swing Modulo Scheduling: A Lifetime-Sensitive Approach", by J. Llosa,
  24. // A. Gonzalez, E. Ayguade, and M. Valero. In PACT '96 Proceedings of the 1996
  25. // Conference on Parallel Architectures and Compilation Techiniques.
  26. //
  27. // "Lifetime-Sensitive Modulo Scheduling in a Production Environment", by J.
  28. // Llosa, E. Ayguade, A. Gonzalez, M. Valero, and J. Eckhardt. In IEEE
  29. // Transactions on Computers, Vol. 50, No. 3, 2001.
  30. //
  31. // "An Implementation of Swing Modulo Scheduling With Extensions for
  32. // Superblocks", by T. Lattner, Master's Thesis, University of Illinois at
  33. // Urbana-Champaign, 2005.
  34. //
  35. //
  36. // The SMS algorithm consists of three main steps after computing the minimal
  37. // initiation interval (MII).
  38. // 1) Analyze the dependence graph and compute information about each
  39. // instruction in the graph.
  40. // 2) Order the nodes (instructions) by priority based upon the heuristics
  41. // described in the algorithm.
  42. // 3) Attempt to schedule the nodes in the specified order using the MII.
  43. //
  44. //===----------------------------------------------------------------------===//
  45. #ifndef LLVM_CODEGEN_MACHINEPIPELINER_H
  46. #define LLVM_CODEGEN_MACHINEPIPELINER_H
  47. #include "llvm/CodeGen/MachineDominators.h"
  48. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  49. #include "llvm/CodeGen/RegisterClassInfo.h"
  50. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  51. #include "llvm/CodeGen/TargetInstrInfo.h"
  52. #include "llvm/InitializePasses.h"
  53. namespace llvm {
  54. class AAResults;
  55. class NodeSet;
  56. class SMSchedule;
  57. extern cl::opt<bool> SwpEnableCopyToPhi;
  58. /// The main class in the implementation of the target independent
  59. /// software pipeliner pass.
  60. class MachinePipeliner : public MachineFunctionPass {
  61. public:
  62. MachineFunction *MF = nullptr;
  63. MachineOptimizationRemarkEmitter *ORE = nullptr;
  64. const MachineLoopInfo *MLI = nullptr;
  65. const MachineDominatorTree *MDT = nullptr;
  66. const InstrItineraryData *InstrItins;
  67. const TargetInstrInfo *TII = nullptr;
  68. RegisterClassInfo RegClassInfo;
  69. bool disabledByPragma = false;
  70. unsigned II_setByPragma = 0;
  71. #ifndef NDEBUG
  72. static int NumTries;
  73. #endif
  74. /// Cache the target analysis information about the loop.
  75. struct LoopInfo {
  76. MachineBasicBlock *TBB = nullptr;
  77. MachineBasicBlock *FBB = nullptr;
  78. SmallVector<MachineOperand, 4> BrCond;
  79. MachineInstr *LoopInductionVar = nullptr;
  80. MachineInstr *LoopCompare = nullptr;
  81. };
  82. LoopInfo LI;
  83. static char ID;
  84. MachinePipeliner() : MachineFunctionPass(ID) {
  85. initializeMachinePipelinerPass(*PassRegistry::getPassRegistry());
  86. }
  87. bool runOnMachineFunction(MachineFunction &MF) override;
  88. void getAnalysisUsage(AnalysisUsage &AU) const override;
  89. private:
  90. void preprocessPhiNodes(MachineBasicBlock &B);
  91. bool canPipelineLoop(MachineLoop &L);
  92. bool scheduleLoop(MachineLoop &L);
  93. bool swingModuloScheduler(MachineLoop &L);
  94. void setPragmaPipelineOptions(MachineLoop &L);
  95. };
  96. /// This class builds the dependence graph for the instructions in a loop,
  97. /// and attempts to schedule the instructions using the SMS algorithm.
  98. class SwingSchedulerDAG : public ScheduleDAGInstrs {
  99. MachinePipeliner &Pass;
  100. /// The minimum initiation interval between iterations for this schedule.
  101. unsigned MII = 0;
  102. /// The maximum initiation interval between iterations for this schedule.
  103. unsigned MAX_II = 0;
  104. /// Set to true if a valid pipelined schedule is found for the loop.
  105. bool Scheduled = false;
  106. MachineLoop &Loop;
  107. LiveIntervals &LIS;
  108. const RegisterClassInfo &RegClassInfo;
  109. unsigned II_setByPragma = 0;
  110. /// A toplogical ordering of the SUnits, which is needed for changing
  111. /// dependences and iterating over the SUnits.
  112. ScheduleDAGTopologicalSort Topo;
  113. struct NodeInfo {
  114. int ASAP = 0;
  115. int ALAP = 0;
  116. int ZeroLatencyDepth = 0;
  117. int ZeroLatencyHeight = 0;
  118. NodeInfo() = default;
  119. };
  120. /// Computed properties for each node in the graph.
  121. std::vector<NodeInfo> ScheduleInfo;
  122. enum OrderKind { BottomUp = 0, TopDown = 1 };
  123. /// Computed node ordering for scheduling.
  124. SetVector<SUnit *> NodeOrder;
  125. using NodeSetType = SmallVector<NodeSet, 8>;
  126. using ValueMapTy = DenseMap<unsigned, unsigned>;
  127. using MBBVectorTy = SmallVectorImpl<MachineBasicBlock *>;
  128. using InstrMapTy = DenseMap<MachineInstr *, MachineInstr *>;
  129. /// Instructions to change when emitting the final schedule.
  130. DenseMap<SUnit *, std::pair<unsigned, int64_t>> InstrChanges;
  131. /// We may create a new instruction, so remember it because it
  132. /// must be deleted when the pass is finished.
  133. DenseMap<MachineInstr*, MachineInstr *> NewMIs;
  134. /// Ordered list of DAG postprocessing steps.
  135. std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
  136. /// Helper class to implement Johnson's circuit finding algorithm.
  137. class Circuits {
  138. std::vector<SUnit> &SUnits;
  139. SetVector<SUnit *> Stack;
  140. BitVector Blocked;
  141. SmallVector<SmallPtrSet<SUnit *, 4>, 10> B;
  142. SmallVector<SmallVector<int, 4>, 16> AdjK;
  143. // Node to Index from ScheduleDAGTopologicalSort
  144. std::vector<int> *Node2Idx;
  145. unsigned NumPaths;
  146. static unsigned MaxPaths;
  147. public:
  148. Circuits(std::vector<SUnit> &SUs, ScheduleDAGTopologicalSort &Topo)
  149. : SUnits(SUs), Blocked(SUs.size()), B(SUs.size()), AdjK(SUs.size()) {
  150. Node2Idx = new std::vector<int>(SUs.size());
  151. unsigned Idx = 0;
  152. for (const auto &NodeNum : Topo)
  153. Node2Idx->at(NodeNum) = Idx++;
  154. }
  155. ~Circuits() { delete Node2Idx; }
  156. /// Reset the data structures used in the circuit algorithm.
  157. void reset() {
  158. Stack.clear();
  159. Blocked.reset();
  160. B.assign(SUnits.size(), SmallPtrSet<SUnit *, 4>());
  161. NumPaths = 0;
  162. }
  163. void createAdjacencyStructure(SwingSchedulerDAG *DAG);
  164. bool circuit(int V, int S, NodeSetType &NodeSets, bool HasBackedge = false);
  165. void unblock(int U);
  166. };
  167. struct CopyToPhiMutation : public ScheduleDAGMutation {
  168. void apply(ScheduleDAGInstrs *DAG) override;
  169. };
  170. public:
  171. SwingSchedulerDAG(MachinePipeliner &P, MachineLoop &L, LiveIntervals &lis,
  172. const RegisterClassInfo &rci, unsigned II)
  173. : ScheduleDAGInstrs(*P.MF, P.MLI, false), Pass(P), Loop(L), LIS(lis),
  174. RegClassInfo(rci), II_setByPragma(II), Topo(SUnits, &ExitSU) {
  175. P.MF->getSubtarget().getSMSMutations(Mutations);
  176. if (SwpEnableCopyToPhi)
  177. Mutations.push_back(std::make_unique<CopyToPhiMutation>());
  178. }
  179. void schedule() override;
  180. void finishBlock() override;
  181. /// Return true if the loop kernel has been scheduled.
  182. bool hasNewSchedule() { return Scheduled; }
  183. /// Return the earliest time an instruction may be scheduled.
  184. int getASAP(SUnit *Node) { return ScheduleInfo[Node->NodeNum].ASAP; }
  185. /// Return the latest time an instruction my be scheduled.
  186. int getALAP(SUnit *Node) { return ScheduleInfo[Node->NodeNum].ALAP; }
  187. /// The mobility function, which the number of slots in which
  188. /// an instruction may be scheduled.
  189. int getMOV(SUnit *Node) { return getALAP(Node) - getASAP(Node); }
  190. /// The depth, in the dependence graph, for a node.
  191. unsigned getDepth(SUnit *Node) { return Node->getDepth(); }
  192. /// The maximum unweighted length of a path from an arbitrary node to the
  193. /// given node in which each edge has latency 0
  194. int getZeroLatencyDepth(SUnit *Node) {
  195. return ScheduleInfo[Node->NodeNum].ZeroLatencyDepth;
  196. }
  197. /// The height, in the dependence graph, for a node.
  198. unsigned getHeight(SUnit *Node) { return Node->getHeight(); }
  199. /// The maximum unweighted length of a path from the given node to an
  200. /// arbitrary node in which each edge has latency 0
  201. int getZeroLatencyHeight(SUnit *Node) {
  202. return ScheduleInfo[Node->NodeNum].ZeroLatencyHeight;
  203. }
  204. /// Return true if the dependence is a back-edge in the data dependence graph.
  205. /// Since the DAG doesn't contain cycles, we represent a cycle in the graph
  206. /// using an anti dependence from a Phi to an instruction.
  207. bool isBackedge(SUnit *Source, const SDep &Dep) {
  208. if (Dep.getKind() != SDep::Anti)
  209. return false;
  210. return Source->getInstr()->isPHI() || Dep.getSUnit()->getInstr()->isPHI();
  211. }
  212. bool isLoopCarriedDep(SUnit *Source, const SDep &Dep, bool isSucc = true);
  213. /// The distance function, which indicates that operation V of iteration I
  214. /// depends on operations U of iteration I-distance.
  215. unsigned getDistance(SUnit *U, SUnit *V, const SDep &Dep) {
  216. // Instructions that feed a Phi have a distance of 1. Computing larger
  217. // values for arrays requires data dependence information.
  218. if (V->getInstr()->isPHI() && Dep.getKind() == SDep::Anti)
  219. return 1;
  220. return 0;
  221. }
  222. void applyInstrChange(MachineInstr *MI, SMSchedule &Schedule);
  223. void fixupRegisterOverlaps(std::deque<SUnit *> &Instrs);
  224. /// Return the new base register that was stored away for the changed
  225. /// instruction.
  226. unsigned getInstrBaseReg(SUnit *SU) {
  227. DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
  228. InstrChanges.find(SU);
  229. if (It != InstrChanges.end())
  230. return It->second.first;
  231. return 0;
  232. }
  233. void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
  234. Mutations.push_back(std::move(Mutation));
  235. }
  236. static bool classof(const ScheduleDAGInstrs *DAG) { return true; }
  237. private:
  238. void addLoopCarriedDependences(AAResults *AA);
  239. void updatePhiDependences();
  240. void changeDependences();
  241. unsigned calculateResMII();
  242. unsigned calculateRecMII(NodeSetType &RecNodeSets);
  243. void findCircuits(NodeSetType &NodeSets);
  244. void fuseRecs(NodeSetType &NodeSets);
  245. void removeDuplicateNodes(NodeSetType &NodeSets);
  246. void computeNodeFunctions(NodeSetType &NodeSets);
  247. void registerPressureFilter(NodeSetType &NodeSets);
  248. void colocateNodeSets(NodeSetType &NodeSets);
  249. void checkNodeSets(NodeSetType &NodeSets);
  250. void groupRemainingNodes(NodeSetType &NodeSets);
  251. void addConnectedNodes(SUnit *SU, NodeSet &NewSet,
  252. SetVector<SUnit *> &NodesAdded);
  253. void computeNodeOrder(NodeSetType &NodeSets);
  254. void checkValidNodeOrder(const NodeSetType &Circuits) const;
  255. bool schedulePipeline(SMSchedule &Schedule);
  256. bool computeDelta(MachineInstr &MI, unsigned &Delta);
  257. MachineInstr *findDefInLoop(Register Reg);
  258. bool canUseLastOffsetValue(MachineInstr *MI, unsigned &BasePos,
  259. unsigned &OffsetPos, unsigned &NewBase,
  260. int64_t &NewOffset);
  261. void postprocessDAG();
  262. /// Set the Minimum Initiation Interval for this schedule attempt.
  263. void setMII(unsigned ResMII, unsigned RecMII);
  264. /// Set the Maximum Initiation Interval for this schedule attempt.
  265. void setMAX_II();
  266. };
  267. /// A NodeSet contains a set of SUnit DAG nodes with additional information
  268. /// that assigns a priority to the set.
  269. class NodeSet {
  270. SetVector<SUnit *> Nodes;
  271. bool HasRecurrence = false;
  272. unsigned RecMII = 0;
  273. int MaxMOV = 0;
  274. unsigned MaxDepth = 0;
  275. unsigned Colocate = 0;
  276. SUnit *ExceedPressure = nullptr;
  277. unsigned Latency = 0;
  278. public:
  279. using iterator = SetVector<SUnit *>::const_iterator;
  280. NodeSet() = default;
  281. NodeSet(iterator S, iterator E) : Nodes(S, E), HasRecurrence(true) {
  282. Latency = 0;
  283. for (unsigned i = 0, e = Nodes.size(); i < e; ++i) {
  284. DenseMap<SUnit *, unsigned> SuccSUnitLatency;
  285. for (const SDep &Succ : Nodes[i]->Succs) {
  286. auto SuccSUnit = Succ.getSUnit();
  287. if (!Nodes.count(SuccSUnit))
  288. continue;
  289. unsigned CurLatency = Succ.getLatency();
  290. unsigned MaxLatency = 0;
  291. if (SuccSUnitLatency.count(SuccSUnit))
  292. MaxLatency = SuccSUnitLatency[SuccSUnit];
  293. if (CurLatency > MaxLatency)
  294. SuccSUnitLatency[SuccSUnit] = CurLatency;
  295. }
  296. for (auto SUnitLatency : SuccSUnitLatency)
  297. Latency += SUnitLatency.second;
  298. }
  299. }
  300. bool insert(SUnit *SU) { return Nodes.insert(SU); }
  301. void insert(iterator S, iterator E) { Nodes.insert(S, E); }
  302. template <typename UnaryPredicate> bool remove_if(UnaryPredicate P) {
  303. return Nodes.remove_if(P);
  304. }
  305. unsigned count(SUnit *SU) const { return Nodes.count(SU); }
  306. bool hasRecurrence() { return HasRecurrence; };
  307. unsigned size() const { return Nodes.size(); }
  308. bool empty() const { return Nodes.empty(); }
  309. SUnit *getNode(unsigned i) const { return Nodes[i]; };
  310. void setRecMII(unsigned mii) { RecMII = mii; };
  311. void setColocate(unsigned c) { Colocate = c; };
  312. void setExceedPressure(SUnit *SU) { ExceedPressure = SU; }
  313. bool isExceedSU(SUnit *SU) { return ExceedPressure == SU; }
  314. int compareRecMII(NodeSet &RHS) { return RecMII - RHS.RecMII; }
  315. int getRecMII() { return RecMII; }
  316. /// Summarize node functions for the entire node set.
  317. void computeNodeSetInfo(SwingSchedulerDAG *SSD) {
  318. for (SUnit *SU : *this) {
  319. MaxMOV = std::max(MaxMOV, SSD->getMOV(SU));
  320. MaxDepth = std::max(MaxDepth, SSD->getDepth(SU));
  321. }
  322. }
  323. unsigned getLatency() { return Latency; }
  324. unsigned getMaxDepth() { return MaxDepth; }
  325. void clear() {
  326. Nodes.clear();
  327. RecMII = 0;
  328. HasRecurrence = false;
  329. MaxMOV = 0;
  330. MaxDepth = 0;
  331. Colocate = 0;
  332. ExceedPressure = nullptr;
  333. }
  334. operator SetVector<SUnit *> &() { return Nodes; }
  335. /// Sort the node sets by importance. First, rank them by recurrence MII,
  336. /// then by mobility (least mobile done first), and finally by depth.
  337. /// Each node set may contain a colocate value which is used as the first
  338. /// tie breaker, if it's set.
  339. bool operator>(const NodeSet &RHS) const {
  340. if (RecMII == RHS.RecMII) {
  341. if (Colocate != 0 && RHS.Colocate != 0 && Colocate != RHS.Colocate)
  342. return Colocate < RHS.Colocate;
  343. if (MaxMOV == RHS.MaxMOV)
  344. return MaxDepth > RHS.MaxDepth;
  345. return MaxMOV < RHS.MaxMOV;
  346. }
  347. return RecMII > RHS.RecMII;
  348. }
  349. bool operator==(const NodeSet &RHS) const {
  350. return RecMII == RHS.RecMII && MaxMOV == RHS.MaxMOV &&
  351. MaxDepth == RHS.MaxDepth;
  352. }
  353. bool operator!=(const NodeSet &RHS) const { return !operator==(RHS); }
  354. iterator begin() { return Nodes.begin(); }
  355. iterator end() { return Nodes.end(); }
  356. void print(raw_ostream &os) const;
  357. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  358. LLVM_DUMP_METHOD void dump() const;
  359. #endif
  360. };
  361. // 16 was selected based on the number of ProcResource kinds for all
  362. // existing Subtargets, so that SmallVector don't need to resize too often.
  363. static const int DefaultProcResSize = 16;
  364. class ResourceManager {
  365. private:
  366. const MCSubtargetInfo *STI;
  367. const MCSchedModel &SM;
  368. const bool UseDFA;
  369. std::unique_ptr<DFAPacketizer> DFAResources;
  370. /// Each processor resource is associated with a so-called processor resource
  371. /// mask. This vector allows to correlate processor resource IDs with
  372. /// processor resource masks. There is exactly one element per each processor
  373. /// resource declared by the scheduling model.
  374. llvm::SmallVector<uint64_t, DefaultProcResSize> ProcResourceMasks;
  375. llvm::SmallVector<uint64_t, DefaultProcResSize> ProcResourceCount;
  376. public:
  377. ResourceManager(const TargetSubtargetInfo *ST)
  378. : STI(ST), SM(ST->getSchedModel()), UseDFA(ST->useDFAforSMS()),
  379. ProcResourceMasks(SM.getNumProcResourceKinds(), 0),
  380. ProcResourceCount(SM.getNumProcResourceKinds(), 0) {
  381. if (UseDFA)
  382. DFAResources.reset(ST->getInstrInfo()->CreateTargetScheduleState(*ST));
  383. initProcResourceVectors(SM, ProcResourceMasks);
  384. }
  385. void initProcResourceVectors(const MCSchedModel &SM,
  386. SmallVectorImpl<uint64_t> &Masks);
  387. /// Check if the resources occupied by a MCInstrDesc are available in
  388. /// the current state.
  389. bool canReserveResources(const MCInstrDesc *MID) const;
  390. /// Reserve the resources occupied by a MCInstrDesc and change the current
  391. /// state to reflect that change.
  392. void reserveResources(const MCInstrDesc *MID);
  393. /// Check if the resources occupied by a machine instruction are available
  394. /// in the current state.
  395. bool canReserveResources(const MachineInstr &MI) const;
  396. /// Reserve the resources occupied by a machine instruction and change the
  397. /// current state to reflect that change.
  398. void reserveResources(const MachineInstr &MI);
  399. /// Reset the state
  400. void clearResources();
  401. };
  402. /// This class represents the scheduled code. The main data structure is a
  403. /// map from scheduled cycle to instructions. During scheduling, the
  404. /// data structure explicitly represents all stages/iterations. When
  405. /// the algorithm finshes, the schedule is collapsed into a single stage,
  406. /// which represents instructions from different loop iterations.
  407. ///
  408. /// The SMS algorithm allows negative values for cycles, so the first cycle
  409. /// in the schedule is the smallest cycle value.
  410. class SMSchedule {
  411. private:
  412. /// Map from execution cycle to instructions.
  413. DenseMap<int, std::deque<SUnit *>> ScheduledInstrs;
  414. /// Map from instruction to execution cycle.
  415. std::map<SUnit *, int> InstrToCycle;
  416. /// Keep track of the first cycle value in the schedule. It starts
  417. /// as zero, but the algorithm allows negative values.
  418. int FirstCycle = 0;
  419. /// Keep track of the last cycle value in the schedule.
  420. int LastCycle = 0;
  421. /// The initiation interval (II) for the schedule.
  422. int InitiationInterval = 0;
  423. /// Target machine information.
  424. const TargetSubtargetInfo &ST;
  425. /// Virtual register information.
  426. MachineRegisterInfo &MRI;
  427. ResourceManager ProcItinResources;
  428. public:
  429. SMSchedule(MachineFunction *mf)
  430. : ST(mf->getSubtarget()), MRI(mf->getRegInfo()), ProcItinResources(&ST) {}
  431. void reset() {
  432. ScheduledInstrs.clear();
  433. InstrToCycle.clear();
  434. FirstCycle = 0;
  435. LastCycle = 0;
  436. InitiationInterval = 0;
  437. }
  438. /// Set the initiation interval for this schedule.
  439. void setInitiationInterval(int ii) { InitiationInterval = ii; }
  440. /// Return the initiation interval for this schedule.
  441. int getInitiationInterval() const { return InitiationInterval; }
  442. /// Return the first cycle in the completed schedule. This
  443. /// can be a negative value.
  444. int getFirstCycle() const { return FirstCycle; }
  445. /// Return the last cycle in the finalized schedule.
  446. int getFinalCycle() const { return FirstCycle + InitiationInterval - 1; }
  447. /// Return the cycle of the earliest scheduled instruction in the dependence
  448. /// chain.
  449. int earliestCycleInChain(const SDep &Dep);
  450. /// Return the cycle of the latest scheduled instruction in the dependence
  451. /// chain.
  452. int latestCycleInChain(const SDep &Dep);
  453. void computeStart(SUnit *SU, int *MaxEarlyStart, int *MinLateStart,
  454. int *MinEnd, int *MaxStart, int II, SwingSchedulerDAG *DAG);
  455. bool insert(SUnit *SU, int StartCycle, int EndCycle, int II);
  456. /// Iterators for the cycle to instruction map.
  457. using sched_iterator = DenseMap<int, std::deque<SUnit *>>::iterator;
  458. using const_sched_iterator =
  459. DenseMap<int, std::deque<SUnit *>>::const_iterator;
  460. /// Return true if the instruction is scheduled at the specified stage.
  461. bool isScheduledAtStage(SUnit *SU, unsigned StageNum) {
  462. return (stageScheduled(SU) == (int)StageNum);
  463. }
  464. /// Return the stage for a scheduled instruction. Return -1 if
  465. /// the instruction has not been scheduled.
  466. int stageScheduled(SUnit *SU) const {
  467. std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SU);
  468. if (it == InstrToCycle.end())
  469. return -1;
  470. return (it->second - FirstCycle) / InitiationInterval;
  471. }
  472. /// Return the cycle for a scheduled instruction. This function normalizes
  473. /// the first cycle to be 0.
  474. unsigned cycleScheduled(SUnit *SU) const {
  475. std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SU);
  476. assert(it != InstrToCycle.end() && "Instruction hasn't been scheduled.");
  477. return (it->second - FirstCycle) % InitiationInterval;
  478. }
  479. /// Return the maximum stage count needed for this schedule.
  480. unsigned getMaxStageCount() {
  481. return (LastCycle - FirstCycle) / InitiationInterval;
  482. }
  483. /// Return the instructions that are scheduled at the specified cycle.
  484. std::deque<SUnit *> &getInstructions(int cycle) {
  485. return ScheduledInstrs[cycle];
  486. }
  487. bool isValidSchedule(SwingSchedulerDAG *SSD);
  488. void finalizeSchedule(SwingSchedulerDAG *SSD);
  489. void orderDependence(SwingSchedulerDAG *SSD, SUnit *SU,
  490. std::deque<SUnit *> &Insts);
  491. bool isLoopCarried(SwingSchedulerDAG *SSD, MachineInstr &Phi);
  492. bool isLoopCarriedDefOfUse(SwingSchedulerDAG *SSD, MachineInstr *Def,
  493. MachineOperand &MO);
  494. void print(raw_ostream &os) const;
  495. void dump() const;
  496. };
  497. } // end namespace llvm
  498. #endif // LLVM_CODEGEN_MACHINEPIPELINER_H
  499. #ifdef __GNUC__
  500. #pragma GCC diagnostic pop
  501. #endif