blob: 4a7920acedba4894f0eb0ec40784ff52372b9bae [file] [log] [blame]
Dan Gohman343f0c02008-11-19 23:18:57 +00001//===--- 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
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"
23using namespace llvm;
24
25ScheduleDAGSDNodes::ScheduleDAGSDNodes(SelectionDAG *dag, MachineBasicBlock *bb,
26 const TargetMachine &tm)
27 : ScheduleDAG(dag, bb, tm) {
28}
29
30SUnit *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;
37 return SU;
38}
39
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.
43static 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)
48 return;
49
50 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
51 if (TargetRegisterInfo::isVirtualRegister(Reg))
52 return;
53
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) {
59 PhysReg = Reg;
60 const TargetRegisterClass *RC =
61 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
62 Cost = RC->getCopyCost();
63 }
64 }
65}
66
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.
70void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000071 // 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) {
77 NI->setNodeId(-1);
78 ++NumNodes;
79 }
80
Dan Gohman343f0c02008-11-19 23:18:57 +000081 // 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
83 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +000084 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
85 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000086 SUnits.reserve(NumNodes * 2);
Dan Gohman343f0c02008-11-19 23:18:57 +000087
Dan Gohman3f237442008-12-16 03:25:46 +000088 // Check to see if the scheduler cares about latencies.
89 bool UnitLatencies = ForceUnitLatencies();
90
Dan Gohman343f0c02008-11-19 23:18:57 +000091 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.
94 continue;
95
96 // If this node has already been processed, stop now.
97 if (NI->getNodeId() != -1) continue;
98
99 SUnit *NodeSUnit = NewSUnit(NI);
100
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.
104
105 // Scan up to find flagged preds.
106 SDNode *N = NI;
107 if (N->getNumOperands() &&
108 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
109 do {
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);
115 }
116
117 // Scan down to find any flagged succs.
118 N = NI;
119 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
120 SDValue FlagVal(N, N->getNumValues()-1);
121
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();
125 UI != E; ++UI)
126 if (FlagVal.isOperandOf(*UI)) {
127 HasFlagUse = true;
128 assert(N->getNodeId() == -1 && "Node already inserted!");
129 N->setNodeId(NodeSUnit->NodeNum);
130 N = *UI;
131 break;
132 }
133 if (!HasFlagUse) break;
134 }
135
136 // If there are flag operands involved, N is now the bottom-most node
137 // of the sequence of nodes that are flagged together.
138 // Update the SUnit.
139 NodeSUnit->setNode(N);
140 assert(N->getNodeId() == -1 && "Node already inserted!");
141 N->setNodeId(NodeSUnit->NodeNum);
142
Dan Gohman787782f2008-11-21 01:44:51 +0000143 // Assign the Latency field of NodeSUnit using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000144 if (UnitLatencies)
145 NodeSUnit->Latency = 1;
146 else
147 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000148 }
149
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();
154
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;
161 break;
162 }
163 }
164 if (TID.isCommutable())
165 SU->isCommutable = true;
166 }
167
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;
174
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.
181
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;
185
186 unsigned PhysReg = 0;
187 int Cost = 1;
188 // Determine if this is a physical register dependency.
189 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000190 assert((PhysReg == 0 || !isChain) &&
191 "Chain dependence via physreg data?");
192 SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data,
193 OpSU->Latency, PhysReg));
Dan Gohman343f0c02008-11-19 23:18:57 +0000194 }
195 }
196 }
197}
198
199void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
200 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
201
202 // Compute the latency for the node. We use the sum of the latencies for
203 // all nodes flagged together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000204 SU->Latency = 0;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000205 bool SawMachineOpcode = false;
206 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
Dan Gohman343f0c02008-11-19 23:18:57 +0000207 if (N->isMachineOpcode()) {
Dan Gohmanc8c28272008-11-21 00:12:10 +0000208 SawMachineOpcode = true;
209 SU->Latency +=
210 InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass());
Dan Gohman343f0c02008-11-19 23:18:57 +0000211 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000212}
213
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).
217unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) {
218 unsigned N = Node->getNumValues();
219 while (N && Node->getValueType(N - 1) == MVT::Flag)
220 --N;
221 if (N && Node->getValueType(N - 1) == MVT::Other)
222 --N; // Skip over chain result.
223 return N;
224}
225
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.
230unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) {
231 unsigned N = ComputeMemOperandsEnd(Node);
232 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
233 --N; // Ignore MEMOPERAND nodes
234 return N;
235}
236
237/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
238/// operand
239unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) {
240 unsigned N = Node->getNumOperands();
241 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
242 --N;
243 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
244 --N; // Ignore chain if it exists.
245 return N;
246}
247
248
249void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
250 if (SU->getNode())
251 SU->getNode()->dump(DAG);
252 else
253 cerr << "CROSS RC COPY ";
254 cerr << "\n";
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()) {
259 cerr << " ";
260 FlaggedNodes.back()->dump(DAG);
261 cerr << "\n";
262 FlaggedNodes.pop_back();
263 }
264}