ScheduleDAGSDNodes.h 6.7 KB

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