ScheduleDAGSDNodes.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===---- ScheduleDAGSDNodes.h - SDNode Scheduling --------------*- C++ -*-===//
  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. // This file implements the ScheduleDAGSDNodes class, which implements
  10. // scheduling for an SDNode-based dependency graph.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
  14. #define LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
  15. #include "llvm/CodeGen/ISDOpcodes.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/ScheduleDAG.h"
  18. #include "llvm/CodeGen/SelectionDAGNodes.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/MachineValueType.h"
  21. #include <cassert>
  22. #include <string>
  23. #include <vector>
  24. namespace llvm {
  25. class AAResults;
  26. class InstrItineraryData;
  27. /// ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs.
  28. ///
  29. /// Edges between SUnits are initially based on edges in the SelectionDAG,
  30. /// and additional edges can be added by the schedulers as heuristics.
  31. /// SDNodes such as Constants, Registers, and a few others that are not
  32. /// interesting to schedulers are not allocated SUnits.
  33. ///
  34. /// SDNodes with MVT::Glue operands are grouped along with the flagged
  35. /// nodes into a single SUnit so that they are scheduled together.
  36. ///
  37. /// SDNode-based scheduling graphs do not use SDep::Anti or SDep::Output
  38. /// edges. Physical register dependence information is not carried in
  39. /// the DAG and must be handled explicitly by schedulers.
  40. ///
  41. class ScheduleDAGSDNodes : public ScheduleDAG {
  42. public:
  43. MachineBasicBlock *BB = nullptr;
  44. SelectionDAG *DAG = nullptr; // DAG of the current basic block
  45. const InstrItineraryData *InstrItins;
  46. /// The schedule. Null SUnit*'s represent noop instructions.
  47. std::vector<SUnit*> Sequence;
  48. explicit ScheduleDAGSDNodes(MachineFunction &mf);
  49. ~ScheduleDAGSDNodes() override = default;
  50. /// Run - perform scheduling.
  51. ///
  52. void Run(SelectionDAG *dag, MachineBasicBlock *bb);
  53. /// isPassiveNode - Return true if the node is a non-scheduled leaf.
  54. ///
  55. static bool isPassiveNode(SDNode *Node) {
  56. if (isa<ConstantSDNode>(Node)) return true;
  57. if (isa<ConstantFPSDNode>(Node)) return true;
  58. if (isa<RegisterSDNode>(Node)) return true;
  59. if (isa<RegisterMaskSDNode>(Node)) return true;
  60. if (isa<GlobalAddressSDNode>(Node)) return true;
  61. if (isa<BasicBlockSDNode>(Node)) return true;
  62. if (isa<FrameIndexSDNode>(Node)) return true;
  63. if (isa<ConstantPoolSDNode>(Node)) return true;
  64. if (isa<TargetIndexSDNode>(Node)) return true;
  65. if (isa<JumpTableSDNode>(Node)) return true;
  66. if (isa<ExternalSymbolSDNode>(Node)) return true;
  67. if (isa<MCSymbolSDNode>(Node)) return true;
  68. if (isa<BlockAddressSDNode>(Node)) return true;
  69. if (Node->getOpcode() == ISD::EntryToken ||
  70. isa<MDNodeSDNode>(Node)) return true;
  71. return false;
  72. }
  73. /// NewSUnit - Creates a new SUnit and return a ptr to it.
  74. ///
  75. SUnit *newSUnit(SDNode *N);
  76. /// Clone - Creates a clone of the specified SUnit. It does not copy the
  77. /// predecessors / successors info nor the temporary scheduling states.
  78. ///
  79. SUnit *Clone(SUnit *Old);
  80. /// BuildSchedGraph - Build the SUnit graph from the selection dag that we
  81. /// are input. This SUnit graph is similar to the SelectionDAG, but
  82. /// excludes nodes that aren't interesting to scheduling, and represents
  83. /// flagged together nodes with a single SUnit.
  84. void BuildSchedGraph(AAResults *AA);
  85. /// InitNumRegDefsLeft - Determine the # of regs defined by this node.
  86. ///
  87. void InitNumRegDefsLeft(SUnit *SU);
  88. /// computeLatency - Compute node latency.
  89. ///
  90. virtual void computeLatency(SUnit *SU);
  91. virtual void computeOperandLatency(SDNode *Def, SDNode *Use,
  92. unsigned OpIdx, SDep& dep) const;
  93. /// Schedule - Order nodes according to selected style, filling
  94. /// in the Sequence member.
  95. ///
  96. virtual void Schedule() = 0;
  97. /// VerifyScheduledSequence - Verify that all SUnits are scheduled and
  98. /// consistent with the Sequence of scheduled instructions.
  99. void VerifyScheduledSequence(bool isBottomUp);
  100. /// EmitSchedule - Insert MachineInstrs into the MachineBasicBlock
  101. /// according to the order specified in Sequence.
  102. ///
  103. virtual MachineBasicBlock*
  104. EmitSchedule(MachineBasicBlock::iterator &InsertPos);
  105. void dumpNode(const SUnit &SU) const override;
  106. void dump() const override;
  107. void dumpSchedule() const;
  108. std::string getGraphNodeLabel(const SUnit *SU) const override;
  109. std::string getDAGName() const override;
  110. virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const;
  111. /// RegDefIter - In place iteration over the values defined by an
  112. /// SUnit. This does not need copies of the iterator or any other STLisms.
  113. /// The iterator creates itself, rather than being provided by the SchedDAG.
  114. class RegDefIter {
  115. const ScheduleDAGSDNodes *SchedDAG;
  116. const SDNode *Node;
  117. unsigned DefIdx = 0;
  118. unsigned NodeNumDefs = 0;
  119. MVT ValueType;
  120. public:
  121. RegDefIter(const SUnit *SU, const ScheduleDAGSDNodes *SD);
  122. bool IsValid() const { return Node != nullptr; }
  123. MVT GetValue() const {
  124. assert(IsValid() && "bad iterator");
  125. return ValueType;
  126. }
  127. const SDNode *GetNode() const {
  128. return Node;
  129. }
  130. unsigned GetIdx() const {
  131. return DefIdx-1;
  132. }
  133. void Advance();
  134. private:
  135. void InitNodeNumDefs();
  136. };
  137. protected:
  138. /// ForceUnitLatencies - Return true if all scheduling edges should be given
  139. /// a latency value of one. The default is to return false; schedulers may
  140. /// override this as needed.
  141. virtual bool forceUnitLatencies() const { return false; }
  142. private:
  143. /// ClusterNeighboringLoads - Cluster loads from "near" addresses into
  144. /// combined SUnits.
  145. void ClusterNeighboringLoads(SDNode *Node);
  146. /// ClusterNodes - Cluster certain nodes which should be scheduled together.
  147. ///
  148. void ClusterNodes();
  149. /// BuildSchedUnits, AddSchedEdges - Helper functions for BuildSchedGraph.
  150. void BuildSchedUnits();
  151. void AddSchedEdges();
  152. void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, Register> &VRBaseMap,
  153. MachineBasicBlock::iterator InsertPos);
  154. };
  155. } // end namespace llvm
  156. #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H