1 //===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the ScheduleDAG class, which is a base class used by
11 // scheduling implementation classes.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "pre-RA-sched"
16 #include "llvm/CodeGen/ScheduleDAGSDNodes.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
25 ScheduleDAGSDNodes::ScheduleDAGSDNodes(SelectionDAG *dag, MachineBasicBlock *bb,
26 const TargetMachine &tm)
27 : ScheduleDAG(dag, bb, tm) {
30 SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
31 SUnit *SU = NewSUnit(Old->getNode());
32 SU->OrigNode = Old->OrigNode;
33 SU->Latency = Old->Latency;
34 SU->isTwoAddress = Old->isTwoAddress;
35 SU->isCommutable = Old->isCommutable;
36 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
40 /// CheckForPhysRegDependency - Check if the dependency between def and use of
41 /// a specified operand is a physical register dependency. If so, returns the
42 /// register and the cost of copying the register.
43 static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
44 const TargetRegisterInfo *TRI,
45 const TargetInstrInfo *TII,
46 unsigned &PhysReg, int &Cost) {
47 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
50 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
51 if (TargetRegisterInfo::isVirtualRegister(Reg))
54 unsigned ResNo = User->getOperand(2).getResNo();
55 if (Def->isMachineOpcode()) {
56 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
57 if (ResNo >= II.getNumDefs() &&
58 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
60 const TargetRegisterClass *RC =
61 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
62 Cost = RC->getCopyCost();
67 /// BuildSchedUnits - Build SUnits from the selection dag that we are input.
68 /// This SUnit graph is similar to the SelectionDAG, but represents flagged
69 /// together nodes with a single SUnit.
70 void ScheduleDAGSDNodes::BuildSchedUnits() {
71 // During scheduling, the NodeId field of SDNode is used to map SDNodes
72 // to their associated SUnits by holding SUnits table indices. A value
73 // of -1 means the SDNode does not yet have an associated SUnit.
74 unsigned NumNodes = 0;
75 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
76 E = DAG->allnodes_end(); NI != E; ++NI) {
81 // Reserve entries in the vector for each of the SUnits we are creating. This
82 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
84 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
85 // This is a temporary workaround.
86 SUnits.reserve(NumNodes * 2);
88 // Check to see if the scheduler cares about latencies.
89 bool UnitLatencies = ForceUnitLatencies();
91 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
92 E = DAG->allnodes_end(); NI != E; ++NI) {
93 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
96 // If this node has already been processed, stop now.
97 if (NI->getNodeId() != -1) continue;
99 SUnit *NodeSUnit = NewSUnit(NI);
101 // See if anything is flagged to this node, if so, add them to flagged
102 // nodes. Nodes can have at most one flag input and one flag output. Flags
103 // are required the be the last operand and result of a node.
105 // Scan up to find flagged preds.
107 if (N->getNumOperands() &&
108 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
110 N = N->getOperand(N->getNumOperands()-1).getNode();
111 assert(N->getNodeId() == -1 && "Node already inserted!");
112 N->setNodeId(NodeSUnit->NodeNum);
113 } while (N->getNumOperands() &&
114 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
117 // Scan down to find any flagged succs.
119 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
120 SDValue FlagVal(N, N->getNumValues()-1);
122 // There are either zero or one users of the Flag result.
123 bool HasFlagUse = false;
124 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
126 if (FlagVal.isOperandOf(*UI)) {
128 assert(N->getNodeId() == -1 && "Node already inserted!");
129 N->setNodeId(NodeSUnit->NodeNum);
133 if (!HasFlagUse) break;
136 // If there are flag operands involved, N is now the bottom-most node
137 // of the sequence of nodes that are flagged together.
139 NodeSUnit->setNode(N);
140 assert(N->getNodeId() == -1 && "Node already inserted!");
141 N->setNodeId(NodeSUnit->NodeNum);
143 // Assign the Latency field of NodeSUnit using target-provided information.
145 NodeSUnit->Latency = 1;
147 ComputeLatency(NodeSUnit);
150 // Pass 2: add the preds, succs, etc.
151 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
152 SUnit *SU = &SUnits[su];
153 SDNode *MainNode = SU->getNode();
155 if (MainNode->isMachineOpcode()) {
156 unsigned Opc = MainNode->getMachineOpcode();
157 const TargetInstrDesc &TID = TII->get(Opc);
158 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
159 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
160 SU->isTwoAddress = true;
164 if (TID.isCommutable())
165 SU->isCommutable = true;
168 // Find all predecessors and successors of the group.
169 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
170 if (N->isMachineOpcode() &&
171 TII->get(N->getMachineOpcode()).getImplicitDefs() &&
172 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
173 SU->hasPhysRegDefs = true;
175 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
176 SDNode *OpN = N->getOperand(i).getNode();
177 if (isPassiveNode(OpN)) continue; // Not scheduled.
178 SUnit *OpSU = &SUnits[OpN->getNodeId()];
179 assert(OpSU && "Node has no SUnit!");
180 if (OpSU == SU) continue; // In the same group.
182 MVT OpVT = N->getOperand(i).getValueType();
183 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
184 bool isChain = OpVT == MVT::Other;
186 unsigned PhysReg = 0;
188 // Determine if this is a physical register dependency.
189 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
190 assert((PhysReg == 0 || !isChain) &&
191 "Chain dependence via physreg data?");
192 SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data,
193 OpSU->Latency, PhysReg));
199 void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
200 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
202 // Compute the latency for the node. We use the sum of the latencies for
203 // all nodes flagged together into this SUnit.
205 bool SawMachineOpcode = false;
206 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
207 if (N->isMachineOpcode()) {
208 SawMachineOpcode = true;
210 InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass());
214 /// CountResults - The results of target nodes have register or immediate
215 /// operands first, then an optional chain, and optional flag operands (which do
216 /// not go into the resulting MachineInstr).
217 unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) {
218 unsigned N = Node->getNumValues();
219 while (N && Node->getValueType(N - 1) == MVT::Flag)
221 if (N && Node->getValueType(N - 1) == MVT::Other)
222 --N; // Skip over chain result.
226 /// CountOperands - The inputs to target nodes have any actual inputs first,
227 /// followed by special operands that describe memory references, then an
228 /// optional chain operand, then an optional flag operand. Compute the number
229 /// of actual operands that will go into the resulting MachineInstr.
230 unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) {
231 unsigned N = ComputeMemOperandsEnd(Node);
232 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
233 --N; // Ignore MEMOPERAND nodes
237 /// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
239 unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) {
240 unsigned N = Node->getNumOperands();
241 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
243 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
244 --N; // Ignore chain if it exists.
249 void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
251 SU->getNode()->dump(DAG);
253 cerr << "CROSS RC COPY ";
255 SmallVector<SDNode *, 4> FlaggedNodes;
256 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
257 FlaggedNodes.push_back(N);
258 while (!FlaggedNodes.empty()) {
260 FlaggedNodes.back()->dump(DAG);
262 FlaggedNodes.pop_back();