ScheduleDAGSDNodes.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===//
  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 implements the ScheduleDAG class, which is a base class used by
  11. // scheduling implementation classes.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "pre-RA-sched"
  15. #include "llvm/CodeGen/ScheduleDAGSDNodes.h"
  16. #include "llvm/CodeGen/SelectionDAG.h"
  17. #include "llvm/Target/TargetMachine.h"
  18. #include "llvm/Target/TargetInstrInfo.h"
  19. #include "llvm/Target/TargetRegisterInfo.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. ScheduleDAGSDNodes::ScheduleDAGSDNodes(SelectionDAG *dag, MachineBasicBlock *bb,
  24. const TargetMachine &tm)
  25. : ScheduleDAG(dag, bb, tm) {
  26. }
  27. SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
  28. SUnit *SU = NewSUnit(Old->getNode());
  29. SU->OrigNode = Old->OrigNode;
  30. SU->Latency = Old->Latency;
  31. SU->isTwoAddress = Old->isTwoAddress;
  32. SU->isCommutable = Old->isCommutable;
  33. SU->hasPhysRegDefs = Old->hasPhysRegDefs;
  34. return SU;
  35. }
  36. /// CheckForPhysRegDependency - Check if the dependency between def and use of
  37. /// a specified operand is a physical register dependency. If so, returns the
  38. /// register and the cost of copying the register.
  39. static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
  40. const TargetRegisterInfo *TRI,
  41. const TargetInstrInfo *TII,
  42. unsigned &PhysReg, int &Cost) {
  43. if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
  44. return;
  45. unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
  46. if (TargetRegisterInfo::isVirtualRegister(Reg))
  47. return;
  48. unsigned ResNo = User->getOperand(2).getResNo();
  49. if (Def->isMachineOpcode()) {
  50. const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
  51. if (ResNo >= II.getNumDefs() &&
  52. II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
  53. PhysReg = Reg;
  54. const TargetRegisterClass *RC =
  55. TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
  56. Cost = RC->getCopyCost();
  57. }
  58. }
  59. }
  60. /// BuildSchedUnits - Build SUnits from the selection dag that we are input.
  61. /// This SUnit graph is similar to the SelectionDAG, but represents flagged
  62. /// together nodes with a single SUnit.
  63. void ScheduleDAGSDNodes::BuildSchedUnits() {
  64. // Reserve entries in the vector for each of the SUnits we are creating. This
  65. // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
  66. // invalidated.
  67. SUnits.reserve(DAG->allnodes_size());
  68. // During scheduling, the NodeId field of SDNode is used to map SDNodes
  69. // to their associated SUnits by holding SUnits table indices. A value
  70. // of -1 means the SDNode does not yet have an associated SUnit.
  71. for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
  72. E = DAG->allnodes_end(); NI != E; ++NI)
  73. NI->setNodeId(-1);
  74. for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
  75. E = DAG->allnodes_end(); NI != E; ++NI) {
  76. if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
  77. continue;
  78. // If this node has already been processed, stop now.
  79. if (NI->getNodeId() != -1) continue;
  80. SUnit *NodeSUnit = NewSUnit(NI);
  81. // See if anything is flagged to this node, if so, add them to flagged
  82. // nodes. Nodes can have at most one flag input and one flag output. Flags
  83. // are required the be the last operand and result of a node.
  84. // Scan up to find flagged preds.
  85. SDNode *N = NI;
  86. if (N->getNumOperands() &&
  87. N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
  88. do {
  89. N = N->getOperand(N->getNumOperands()-1).getNode();
  90. assert(N->getNodeId() == -1 && "Node already inserted!");
  91. N->setNodeId(NodeSUnit->NodeNum);
  92. } while (N->getNumOperands() &&
  93. N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
  94. }
  95. // Scan down to find any flagged succs.
  96. N = NI;
  97. while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
  98. SDValue FlagVal(N, N->getNumValues()-1);
  99. // There are either zero or one users of the Flag result.
  100. bool HasFlagUse = false;
  101. for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
  102. UI != E; ++UI)
  103. if (FlagVal.isOperandOf(*UI)) {
  104. HasFlagUse = true;
  105. assert(N->getNodeId() == -1 && "Node already inserted!");
  106. N->setNodeId(NodeSUnit->NodeNum);
  107. N = *UI;
  108. break;
  109. }
  110. if (!HasFlagUse) break;
  111. }
  112. // If there are flag operands involved, N is now the bottom-most node
  113. // of the sequence of nodes that are flagged together.
  114. // Update the SUnit.
  115. NodeSUnit->setNode(N);
  116. assert(N->getNodeId() == -1 && "Node already inserted!");
  117. N->setNodeId(NodeSUnit->NodeNum);
  118. // Assign the Latency field of NodeSUnit using target-provided information.
  119. ComputeLatency(NodeSUnit);
  120. }
  121. // Pass 2: add the preds, succs, etc.
  122. for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
  123. SUnit *SU = &SUnits[su];
  124. SDNode *MainNode = SU->getNode();
  125. if (MainNode->isMachineOpcode()) {
  126. unsigned Opc = MainNode->getMachineOpcode();
  127. const TargetInstrDesc &TID = TII->get(Opc);
  128. for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
  129. if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
  130. SU->isTwoAddress = true;
  131. break;
  132. }
  133. }
  134. if (TID.isCommutable())
  135. SU->isCommutable = true;
  136. }
  137. // Find all predecessors and successors of the group.
  138. for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
  139. if (N->isMachineOpcode() &&
  140. TII->get(N->getMachineOpcode()).getImplicitDefs() &&
  141. CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
  142. SU->hasPhysRegDefs = true;
  143. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  144. SDNode *OpN = N->getOperand(i).getNode();
  145. if (isPassiveNode(OpN)) continue; // Not scheduled.
  146. SUnit *OpSU = &SUnits[OpN->getNodeId()];
  147. assert(OpSU && "Node has no SUnit!");
  148. if (OpSU == SU) continue; // In the same group.
  149. MVT OpVT = N->getOperand(i).getValueType();
  150. assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
  151. bool isChain = OpVT == MVT::Other;
  152. unsigned PhysReg = 0;
  153. int Cost = 1;
  154. // Determine if this is a physical register dependency.
  155. CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
  156. assert((PhysReg == 0 || !isChain) &&
  157. "Chain dependence via physreg data?");
  158. SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data,
  159. OpSU->Latency, PhysReg));
  160. }
  161. }
  162. }
  163. }
  164. void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
  165. const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
  166. // Compute the latency for the node. We use the sum of the latencies for
  167. // all nodes flagged together into this SUnit.
  168. if (InstrItins.isEmpty()) {
  169. // No latency information.
  170. SU->Latency = 1;
  171. return;
  172. }
  173. SU->Latency = 0;
  174. bool SawMachineOpcode = false;
  175. for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
  176. if (N->isMachineOpcode()) {
  177. SawMachineOpcode = true;
  178. SU->Latency +=
  179. InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass());
  180. }
  181. // Ensure that CopyToReg and similar nodes have a non-zero latency.
  182. if (!SawMachineOpcode)
  183. SU->Latency = 1;
  184. }
  185. /// CountResults - The results of target nodes have register or immediate
  186. /// operands first, then an optional chain, and optional flag operands (which do
  187. /// not go into the resulting MachineInstr).
  188. unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) {
  189. unsigned N = Node->getNumValues();
  190. while (N && Node->getValueType(N - 1) == MVT::Flag)
  191. --N;
  192. if (N && Node->getValueType(N - 1) == MVT::Other)
  193. --N; // Skip over chain result.
  194. return N;
  195. }
  196. /// CountOperands - The inputs to target nodes have any actual inputs first,
  197. /// followed by special operands that describe memory references, then an
  198. /// optional chain operand, then an optional flag operand. Compute the number
  199. /// of actual operands that will go into the resulting MachineInstr.
  200. unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) {
  201. unsigned N = ComputeMemOperandsEnd(Node);
  202. while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
  203. --N; // Ignore MEMOPERAND nodes
  204. return N;
  205. }
  206. /// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
  207. /// operand
  208. unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) {
  209. unsigned N = Node->getNumOperands();
  210. while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
  211. --N;
  212. if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
  213. --N; // Ignore chain if it exists.
  214. return N;
  215. }
  216. void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
  217. if (SU->getNode())
  218. SU->getNode()->dump(DAG);
  219. else
  220. cerr << "CROSS RC COPY ";
  221. cerr << "\n";
  222. SmallVector<SDNode *, 4> FlaggedNodes;
  223. for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
  224. FlaggedNodes.push_back(N);
  225. while (!FlaggedNodes.empty()) {
  226. cerr << " ";
  227. FlaggedNodes.back()->dump(DAG);
  228. cerr << "\n";
  229. FlaggedNodes.pop_back();
  230. }
  231. }