blob: 967a8cef8e15d525f7b28d0be5a24206d041c643 [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"
Dan Gohman84fbac52009-02-06 17:22:58 +000016#include "ScheduleDAGSDNodes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000017#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
Dan Gohman79ce2762009-01-15 19:20:50 +000025ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
26 : ScheduleDAG(mf) {
Dan Gohman343f0c02008-11-19 23:18:57 +000027}
28
Dan Gohman47ac0f02009-02-11 04:27:20 +000029/// Run - perform scheduling.
30///
31void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
32 MachineBasicBlock::iterator insertPos) {
33 DAG = dag;
34 ScheduleDAG::Run(bb, insertPos);
35}
36
Dan Gohman343f0c02008-11-19 23:18:57 +000037SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
38 SUnit *SU = NewSUnit(Old->getNode());
39 SU->OrigNode = Old->OrigNode;
40 SU->Latency = Old->Latency;
41 SU->isTwoAddress = Old->isTwoAddress;
42 SU->isCommutable = Old->isCommutable;
43 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenge57187c2009-01-16 20:57:18 +000044 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000045 return SU;
46}
47
48/// CheckForPhysRegDependency - Check if the dependency between def and use of
49/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000050/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000051static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
52 const TargetRegisterInfo *TRI,
53 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +000054 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +000055 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
56 return;
57
58 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
59 if (TargetRegisterInfo::isVirtualRegister(Reg))
60 return;
61
62 unsigned ResNo = User->getOperand(2).getResNo();
63 if (Def->isMachineOpcode()) {
64 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
65 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +000066 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +000067 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +000068 const TargetRegisterClass *RC =
69 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
70 Cost = RC->getCopyCost();
71 }
Dan Gohman343f0c02008-11-19 23:18:57 +000072 }
73}
74
Dan Gohman343f0c02008-11-19 23:18:57 +000075void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000076 // During scheduling, the NodeId field of SDNode is used to map SDNodes
77 // to their associated SUnits by holding SUnits table indices. A value
78 // of -1 means the SDNode does not yet have an associated SUnit.
79 unsigned NumNodes = 0;
80 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
81 E = DAG->allnodes_end(); NI != E; ++NI) {
82 NI->setNodeId(-1);
83 ++NumNodes;
84 }
85
Dan Gohman343f0c02008-11-19 23:18:57 +000086 // Reserve entries in the vector for each of the SUnits we are creating. This
87 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
88 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +000089 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
90 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000091 SUnits.reserve(NumNodes * 2);
Dan Gohman343f0c02008-11-19 23:18:57 +000092
Dan Gohman3f237442008-12-16 03:25:46 +000093 // Check to see if the scheduler cares about latencies.
94 bool UnitLatencies = ForceUnitLatencies();
95
Dan Gohman343f0c02008-11-19 23:18:57 +000096 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
97 E = DAG->allnodes_end(); NI != E; ++NI) {
98 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
99 continue;
100
101 // If this node has already been processed, stop now.
102 if (NI->getNodeId() != -1) continue;
103
104 SUnit *NodeSUnit = NewSUnit(NI);
105
106 // See if anything is flagged to this node, if so, add them to flagged
107 // nodes. Nodes can have at most one flag input and one flag output. Flags
Dan Gohmandb95fa12009-03-20 20:42:23 +0000108 // are required to be the last operand and result of a node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000109
110 // Scan up to find flagged preds.
111 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000112 while (N->getNumOperands() &&
113 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
114 N = N->getOperand(N->getNumOperands()-1).getNode();
115 assert(N->getNodeId() == -1 && "Node already inserted!");
116 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman343f0c02008-11-19 23:18:57 +0000117 }
118
119 // Scan down to find any flagged succs.
120 N = NI;
121 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
122 SDValue FlagVal(N, N->getNumValues()-1);
123
124 // There are either zero or one users of the Flag result.
125 bool HasFlagUse = false;
126 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
127 UI != E; ++UI)
128 if (FlagVal.isOperandOf(*UI)) {
129 HasFlagUse = true;
130 assert(N->getNodeId() == -1 && "Node already inserted!");
131 N->setNodeId(NodeSUnit->NodeNum);
132 N = *UI;
133 break;
134 }
135 if (!HasFlagUse) break;
136 }
137
138 // If there are flag operands involved, N is now the bottom-most node
139 // of the sequence of nodes that are flagged together.
140 // Update the SUnit.
141 NodeSUnit->setNode(N);
142 assert(N->getNodeId() == -1 && "Node already inserted!");
143 N->setNodeId(NodeSUnit->NodeNum);
144
Dan Gohman787782f2008-11-21 01:44:51 +0000145 // Assign the Latency field of NodeSUnit using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000146 if (UnitLatencies)
147 NodeSUnit->Latency = 1;
148 else
149 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000150 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000151}
152
153void ScheduleDAGSDNodes::AddSchedEdges() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000154 // Pass 2: add the preds, succs, etc.
155 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
156 SUnit *SU = &SUnits[su];
157 SDNode *MainNode = SU->getNode();
158
159 if (MainNode->isMachineOpcode()) {
160 unsigned Opc = MainNode->getMachineOpcode();
161 const TargetInstrDesc &TID = TII->get(Opc);
162 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
163 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
164 SU->isTwoAddress = true;
165 break;
166 }
167 }
168 if (TID.isCommutable())
169 SU->isCommutable = true;
170 }
171
172 // Find all predecessors and successors of the group.
173 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
174 if (N->isMachineOpcode() &&
175 TII->get(N->getMachineOpcode()).getImplicitDefs() &&
176 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
177 SU->hasPhysRegDefs = true;
178
179 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
180 SDNode *OpN = N->getOperand(i).getNode();
181 if (isPassiveNode(OpN)) continue; // Not scheduled.
182 SUnit *OpSU = &SUnits[OpN->getNodeId()];
183 assert(OpSU && "Node has no SUnit!");
184 if (OpSU == SU) continue; // In the same group.
185
186 MVT OpVT = N->getOperand(i).getValueType();
187 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
188 bool isChain = OpVT == MVT::Other;
189
190 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000191 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000192 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000193 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000194 assert((PhysReg == 0 || !isChain) &&
195 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000196 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
197 // emits a copy from the physical register to a virtual register unless
198 // it requires a cross class copy (cost < 0). That means we are only
199 // treating "expensive to copy" register dependency as physical register
200 // dependency. This may change in the future though.
201 if (Cost >= 0)
202 PhysReg = 0;
Dan Gohman54e4c362008-12-09 22:54:47 +0000203 SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data,
204 OpSU->Latency, PhysReg));
Dan Gohman343f0c02008-11-19 23:18:57 +0000205 }
206 }
207 }
208}
209
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000210/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
211/// are input. This SUnit graph is similar to the SelectionDAG, but
212/// excludes nodes that aren't interesting to scheduling, and represents
213/// flagged together nodes with a single SUnit.
214void ScheduleDAGSDNodes::BuildSchedGraph() {
215 // Populate the SUnits array.
216 BuildSchedUnits();
217 // Compute all the scheduling dependencies between nodes.
218 AddSchedEdges();
219}
220
Dan Gohman343f0c02008-11-19 23:18:57 +0000221void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
222 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
223
224 // Compute the latency for the node. We use the sum of the latencies for
225 // all nodes flagged together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000226 SU->Latency = 0;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000227 bool SawMachineOpcode = false;
228 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
Dan Gohman343f0c02008-11-19 23:18:57 +0000229 if (N->isMachineOpcode()) {
Dan Gohmanc8c28272008-11-21 00:12:10 +0000230 SawMachineOpcode = true;
231 SU->Latency +=
232 InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass());
Dan Gohman343f0c02008-11-19 23:18:57 +0000233 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000234}
235
236/// CountResults - The results of target nodes have register or immediate
237/// operands first, then an optional chain, and optional flag operands (which do
238/// not go into the resulting MachineInstr).
239unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) {
240 unsigned N = Node->getNumValues();
241 while (N && Node->getValueType(N - 1) == MVT::Flag)
242 --N;
243 if (N && Node->getValueType(N - 1) == MVT::Other)
244 --N; // Skip over chain result.
245 return N;
246}
247
248/// CountOperands - The inputs to target nodes have any actual inputs first,
249/// followed by special operands that describe memory references, then an
250/// optional chain operand, then an optional flag operand. Compute the number
251/// of actual operands that will go into the resulting MachineInstr.
252unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) {
253 unsigned N = ComputeMemOperandsEnd(Node);
254 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
255 --N; // Ignore MEMOPERAND nodes
256 return N;
257}
258
259/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
260/// operand
261unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) {
262 unsigned N = Node->getNumOperands();
263 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
264 --N;
265 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
266 --N; // Ignore chain if it exists.
267 return N;
268}
269
270
271void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000272 if (!SU->getNode()) {
273 cerr << "PHYS REG COPY\n";
274 return;
275 }
276
277 SU->getNode()->dump(DAG);
Dan Gohman343f0c02008-11-19 23:18:57 +0000278 cerr << "\n";
279 SmallVector<SDNode *, 4> FlaggedNodes;
280 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
281 FlaggedNodes.push_back(N);
282 while (!FlaggedNodes.empty()) {
283 cerr << " ";
284 FlaggedNodes.back()->dump(DAG);
285 cerr << "\n";
286 FlaggedNodes.pop_back();
287 }
288}