ScheduleDAGInstrs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- 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. // This file implements the ScheduleDAGInstrs class, which implements
  11. // scheduling for a MachineInstr-based dependency graph.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
  15. #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
  16. #include "llvm/ADT/SparseMultiSet.h"
  17. #include "llvm/ADT/SparseSet.h"
  18. #include "llvm/CodeGen/ScheduleDAG.h"
  19. #include "llvm/CodeGen/TargetSchedule.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Target/TargetRegisterInfo.h"
  22. namespace llvm {
  23. class MachineFrameInfo;
  24. class MachineLoopInfo;
  25. class MachineDominatorTree;
  26. class LiveIntervals;
  27. class RegPressureTracker;
  28. class PressureDiffs;
  29. /// An individual mapping from virtual register number to SUnit.
  30. struct VReg2SUnit {
  31. unsigned VirtReg;
  32. SUnit *SU;
  33. VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
  34. unsigned getSparseSetIndex() const {
  35. return TargetRegisterInfo::virtReg2Index(VirtReg);
  36. }
  37. };
  38. /// Record a physical register access.
  39. /// For non-data-dependent uses, OpIdx == -1.
  40. struct PhysRegSUOper {
  41. SUnit *SU;
  42. int OpIdx;
  43. unsigned Reg;
  44. PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
  45. unsigned getSparseSetIndex() const { return Reg; }
  46. };
  47. /// Use a SparseMultiSet to track physical registers. Storage is only
  48. /// allocated once for the pass. It can be cleared in constant time and reused
  49. /// without any frees.
  50. typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t>
  51. Reg2SUnitsMap;
  52. /// Use SparseSet as a SparseMap by relying on the fact that it never
  53. /// compares ValueT's, only unsigned keys. This allows the set to be cleared
  54. /// between scheduling regions in constant time as long as ValueT does not
  55. /// require a destructor.
  56. typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap;
  57. /// Track local uses of virtual registers. These uses are gathered by the DAG
  58. /// builder and may be consulted by the scheduler to avoid iterating an entire
  59. /// vreg use list.
  60. typedef SparseMultiSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2UseMap;
  61. /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
  62. /// MachineInstrs.
  63. class ScheduleDAGInstrs : public ScheduleDAG {
  64. protected:
  65. const MachineLoopInfo *MLI;
  66. const MachineFrameInfo *MFI;
  67. /// Live Intervals provides reaching defs in preRA scheduling.
  68. LiveIntervals *LIS;
  69. /// TargetSchedModel provides an interface to the machine model.
  70. TargetSchedModel SchedModel;
  71. /// isPostRA flag indicates vregs cannot be present.
  72. bool IsPostRA;
  73. /// True if the DAG builder should remove kill flags (in preparation for
  74. /// rescheduling).
  75. bool RemoveKillFlags;
  76. /// The standard DAG builder does not normally include terminators as DAG
  77. /// nodes because it does not create the necessary dependencies to prevent
  78. /// reordering. A specialized scheduler can override
  79. /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
  80. /// it has taken responsibility for scheduling the terminator correctly.
  81. bool CanHandleTerminators;
  82. /// State specific to the current scheduling region.
  83. /// ------------------------------------------------
  84. /// The block in which to insert instructions
  85. MachineBasicBlock *BB;
  86. /// The beginning of the range to be scheduled.
  87. MachineBasicBlock::iterator RegionBegin;
  88. /// The end of the range to be scheduled.
  89. MachineBasicBlock::iterator RegionEnd;
  90. /// Instructions in this region (distance(RegionBegin, RegionEnd)).
  91. unsigned NumRegionInstrs;
  92. /// After calling BuildSchedGraph, each machine instruction in the current
  93. /// scheduling region is mapped to an SUnit.
  94. DenseMap<MachineInstr*, SUnit*> MISUnitMap;
  95. /// After calling BuildSchedGraph, each vreg used in the scheduling region
  96. /// is mapped to a set of SUnits. These include all local vreg uses, not
  97. /// just the uses for a singly defined vreg.
  98. VReg2UseMap VRegUses;
  99. /// State internal to DAG building.
  100. /// -------------------------------
  101. /// Defs, Uses - Remember where defs and uses of each register are as we
  102. /// iterate upward through the instructions. This is allocated here instead
  103. /// of inside BuildSchedGraph to avoid the need for it to be initialized and
  104. /// destructed for each block.
  105. Reg2SUnitsMap Defs;
  106. Reg2SUnitsMap Uses;
  107. /// Track the last instruction in this region defining each virtual register.
  108. VReg2SUnitMap VRegDefs;
  109. /// PendingLoads - Remember where unknown loads are after the most recent
  110. /// unknown store, as we iterate. As with Defs and Uses, this is here
  111. /// to minimize construction/destruction.
  112. std::vector<SUnit *> PendingLoads;
  113. /// DbgValues - Remember instruction that precedes DBG_VALUE.
  114. /// These are generated by buildSchedGraph but persist so they can be
  115. /// referenced when emitting the final schedule.
  116. typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
  117. DbgValueVector;
  118. DbgValueVector DbgValues;
  119. MachineInstr *FirstDbgValue;
  120. /// Set of live physical registers for updating kill flags.
  121. BitVector LiveRegs;
  122. public:
  123. explicit ScheduleDAGInstrs(MachineFunction &mf,
  124. const MachineLoopInfo *mli,
  125. bool IsPostRAFlag,
  126. bool RemoveKillFlags = false,
  127. LiveIntervals *LIS = nullptr);
  128. ~ScheduleDAGInstrs() override {}
  129. bool isPostRA() const { return IsPostRA; }
  130. /// \brief Expose LiveIntervals for use in DAG mutators and such.
  131. LiveIntervals *getLIS() const { return LIS; }
  132. /// \brief Get the machine model for instruction scheduling.
  133. const TargetSchedModel *getSchedModel() const { return &SchedModel; }
  134. /// \brief Resolve and cache a resolved scheduling class for an SUnit.
  135. const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
  136. if (!SU->SchedClass && SchedModel.hasInstrSchedModel())
  137. SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
  138. return SU->SchedClass;
  139. }
  140. /// begin - Return an iterator to the top of the current scheduling region.
  141. MachineBasicBlock::iterator begin() const { return RegionBegin; }
  142. /// end - Return an iterator to the bottom of the current scheduling region.
  143. MachineBasicBlock::iterator end() const { return RegionEnd; }
  144. /// newSUnit - Creates a new SUnit and return a ptr to it.
  145. SUnit *newSUnit(MachineInstr *MI);
  146. /// getSUnit - Return an existing SUnit for this MI, or NULL.
  147. SUnit *getSUnit(MachineInstr *MI) const;
  148. /// startBlock - Prepare to perform scheduling in the given block.
  149. virtual void startBlock(MachineBasicBlock *BB);
  150. /// finishBlock - Clean up after scheduling in the given block.
  151. virtual void finishBlock();
  152. /// Initialize the scheduler state for the next scheduling region.
  153. virtual void enterRegion(MachineBasicBlock *bb,
  154. MachineBasicBlock::iterator begin,
  155. MachineBasicBlock::iterator end,
  156. unsigned regioninstrs);
  157. /// Notify that the scheduler has finished scheduling the current region.
  158. virtual void exitRegion();
  159. /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
  160. /// input.
  161. void buildSchedGraph(AliasAnalysis *AA,
  162. RegPressureTracker *RPTracker = nullptr,
  163. PressureDiffs *PDiffs = nullptr);
  164. /// addSchedBarrierDeps - Add dependencies from instructions in the current
  165. /// list of instructions being scheduled to scheduling barrier. We want to
  166. /// make sure instructions which define registers that are either used by
  167. /// the terminator or are live-out are properly scheduled. This is
  168. /// especially important when the definition latency of the return value(s)
  169. /// are too high to be hidden by the branch or when the liveout registers
  170. /// used by instructions in the fallthrough block.
  171. void addSchedBarrierDeps();
  172. /// schedule - Order nodes according to selected style, filling
  173. /// in the Sequence member.
  174. ///
  175. /// Typically, a scheduling algorithm will implement schedule() without
  176. /// overriding enterRegion() or exitRegion().
  177. virtual void schedule() = 0;
  178. /// finalizeSchedule - Allow targets to perform final scheduling actions at
  179. /// the level of the whole MachineFunction. By default does nothing.
  180. virtual void finalizeSchedule() {}
  181. void dumpNode(const SUnit *SU) const override;
  182. /// Return a label for a DAG node that points to an instruction.
  183. std::string getGraphNodeLabel(const SUnit *SU) const override;
  184. /// Return a label for the region of code covered by the DAG.
  185. std::string getDAGName() const override;
  186. /// \brief Fix register kill flags that scheduling has made invalid.
  187. void fixupKills(MachineBasicBlock *MBB);
  188. protected:
  189. void initSUnits();
  190. void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
  191. void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
  192. void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
  193. void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
  194. /// \brief PostRA helper for rewriting kill flags.
  195. void startBlockForKills(MachineBasicBlock *BB);
  196. /// \brief Toggle a register operand kill flag.
  197. ///
  198. /// Other adjustments may be made to the instruction if necessary. Return
  199. /// true if the operand has been deleted, false if not.
  200. bool toggleKillFlag(MachineInstr *MI, MachineOperand &MO);
  201. };
  202. /// newSUnit - Creates a new SUnit and return a ptr to it.
  203. inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
  204. #ifndef NDEBUG
  205. const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0];
  206. #endif
  207. SUnits.emplace_back(MI, (unsigned)SUnits.size());
  208. assert((Addr == nullptr || Addr == &SUnits[0]) &&
  209. "SUnits std::vector reallocated on the fly!");
  210. SUnits.back().OrigNode = &SUnits.back();
  211. return &SUnits.back();
  212. }
  213. /// getSUnit - Return an existing SUnit for this MI, or NULL.
  214. inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
  215. DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI);
  216. if (I == MISUnitMap.end())
  217. return nullptr;
  218. return I->second;
  219. }
  220. } // namespace llvm
  221. #endif