blob: 3e2101aab8144e65164dc007d7fd47efeceedf55 [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"
David Goodwin71046162009-08-13 16:05:04 +000021#include "llvm/Target/TargetSubtarget.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Dan Gohman79ce2762009-01-15 19:20:50 +000026ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
27 : ScheduleDAG(mf) {
Dan Gohman343f0c02008-11-19 23:18:57 +000028}
29
Dan Gohman47ac0f02009-02-11 04:27:20 +000030/// Run - perform scheduling.
31///
32void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
33 MachineBasicBlock::iterator insertPos) {
34 DAG = dag;
35 ScheduleDAG::Run(bb, insertPos);
36}
37
Dan Gohman343f0c02008-11-19 23:18:57 +000038SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
39 SUnit *SU = NewSUnit(Old->getNode());
40 SU->OrigNode = Old->OrigNode;
41 SU->Latency = Old->Latency;
42 SU->isTwoAddress = Old->isTwoAddress;
43 SU->isCommutable = Old->isCommutable;
44 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000045 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Evan Chenge57187c2009-01-16 20:57:18 +000046 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000047 return SU;
48}
49
50/// CheckForPhysRegDependency - Check if the dependency between def and use of
51/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000052/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000053static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
54 const TargetRegisterInfo *TRI,
55 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +000056 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +000057 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
58 return;
59
60 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
61 if (TargetRegisterInfo::isVirtualRegister(Reg))
62 return;
63
64 unsigned ResNo = User->getOperand(2).getResNo();
65 if (Def->isMachineOpcode()) {
66 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
67 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +000068 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +000069 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +000070 const TargetRegisterClass *RC =
71 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
72 Cost = RC->getCopyCost();
73 }
Dan Gohman343f0c02008-11-19 23:18:57 +000074 }
75}
76
Dan Gohman343f0c02008-11-19 23:18:57 +000077void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000078 // During scheduling, the NodeId field of SDNode is used to map SDNodes
79 // to their associated SUnits by holding SUnits table indices. A value
80 // of -1 means the SDNode does not yet have an associated SUnit.
81 unsigned NumNodes = 0;
82 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
83 E = DAG->allnodes_end(); NI != E; ++NI) {
84 NI->setNodeId(-1);
85 ++NumNodes;
86 }
87
Dan Gohman343f0c02008-11-19 23:18:57 +000088 // Reserve entries in the vector for each of the SUnits we are creating. This
89 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
90 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +000091 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
92 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +000093 SUnits.reserve(NumNodes * 2);
Dan Gohman343f0c02008-11-19 23:18:57 +000094
Dan Gohman3f237442008-12-16 03:25:46 +000095 // Check to see if the scheduler cares about latencies.
96 bool UnitLatencies = ForceUnitLatencies();
97
Dan Gohman343f0c02008-11-19 23:18:57 +000098 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
99 E = DAG->allnodes_end(); NI != E; ++NI) {
100 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
101 continue;
102
103 // If this node has already been processed, stop now.
104 if (NI->getNodeId() != -1) continue;
105
106 SUnit *NodeSUnit = NewSUnit(NI);
107
108 // See if anything is flagged to this node, if so, add them to flagged
109 // nodes. Nodes can have at most one flag input and one flag output. Flags
Dan Gohmandb95fa12009-03-20 20:42:23 +0000110 // are required to be the last operand and result of a node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000111
112 // Scan up to find flagged preds.
113 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000114 while (N->getNumOperands() &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000116 N = N->getOperand(N->getNumOperands()-1).getNode();
117 assert(N->getNodeId() == -1 && "Node already inserted!");
118 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman343f0c02008-11-19 23:18:57 +0000119 }
120
121 // Scan down to find any flagged succs.
122 N = NI;
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000124 SDValue FlagVal(N, N->getNumValues()-1);
125
126 // There are either zero or one users of the Flag result.
127 bool HasFlagUse = false;
128 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
129 UI != E; ++UI)
130 if (FlagVal.isOperandOf(*UI)) {
131 HasFlagUse = true;
132 assert(N->getNodeId() == -1 && "Node already inserted!");
133 N->setNodeId(NodeSUnit->NodeNum);
134 N = *UI;
135 break;
136 }
137 if (!HasFlagUse) break;
138 }
139
140 // If there are flag operands involved, N is now the bottom-most node
141 // of the sequence of nodes that are flagged together.
142 // Update the SUnit.
143 NodeSUnit->setNode(N);
144 assert(N->getNodeId() == -1 && "Node already inserted!");
145 N->setNodeId(NodeSUnit->NodeNum);
146
Dan Gohman787782f2008-11-21 01:44:51 +0000147 // Assign the Latency field of NodeSUnit using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000148 if (UnitLatencies)
149 NodeSUnit->Latency = 1;
150 else
151 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000152 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000153}
154
155void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000156 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
157
David Goodwindc4bdcd2009-08-19 16:08:58 +0000158 // Check to see if the scheduler cares about latencies.
159 bool UnitLatencies = ForceUnitLatencies();
160
Dan Gohman343f0c02008-11-19 23:18:57 +0000161 // Pass 2: add the preds, succs, etc.
162 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
163 SUnit *SU = &SUnits[su];
164 SDNode *MainNode = SU->getNode();
165
166 if (MainNode->isMachineOpcode()) {
167 unsigned Opc = MainNode->getMachineOpcode();
168 const TargetInstrDesc &TID = TII->get(Opc);
169 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
170 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
171 SU->isTwoAddress = true;
172 break;
173 }
174 }
175 if (TID.isCommutable())
176 SU->isCommutable = true;
177 }
178
179 // Find all predecessors and successors of the group.
180 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
181 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000182 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
183 SU->hasPhysRegClobbers = true;
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000184 unsigned NumUsed = CountResults(N);
185 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
186 --NumUsed; // Skip over unused values at the end.
187 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000188 SU->hasPhysRegDefs = true;
189 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000190
191 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
192 SDNode *OpN = N->getOperand(i).getNode();
193 if (isPassiveNode(OpN)) continue; // Not scheduled.
194 SUnit *OpSU = &SUnits[OpN->getNodeId()];
195 assert(OpSU && "Node has no SUnit!");
196 if (OpSU == SU) continue; // In the same group.
197
Owen Andersone50ed302009-08-10 22:56:29 +0000198 EVT OpVT = N->getOperand(i).getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
200 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000201
202 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000203 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000204 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000205 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000206 assert((PhysReg == 0 || !isChain) &&
207 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000208 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
209 // emits a copy from the physical register to a virtual register unless
210 // it requires a cross class copy (cost < 0). That means we are only
211 // treating "expensive to copy" register dependency as physical register
212 // dependency. This may change in the future though.
213 if (Cost >= 0)
214 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000215
216 const SDep& dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
217 OpSU->Latency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000218 if (!isChain && !UnitLatencies) {
219 ComputeOperandLatency(OpSU, SU, (SDep &)dep);
220 ST.adjustSchedDependency(OpSU, SU, (SDep &)dep);
221 }
David Goodwin71046162009-08-13 16:05:04 +0000222
223 SU->addPred(dep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000224 }
225 }
226 }
227}
228
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000229/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
230/// are input. This SUnit graph is similar to the SelectionDAG, but
231/// excludes nodes that aren't interesting to scheduling, and represents
232/// flagged together nodes with a single SUnit.
233void ScheduleDAGSDNodes::BuildSchedGraph() {
234 // Populate the SUnits array.
235 BuildSchedUnits();
236 // Compute all the scheduling dependencies between nodes.
237 AddSchedEdges();
238}
239
Dan Gohman343f0c02008-11-19 23:18:57 +0000240void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
241 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
242
243 // Compute the latency for the node. We use the sum of the latencies for
244 // all nodes flagged together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000245 SU->Latency = 0;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000246 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
Dan Gohman343f0c02008-11-19 23:18:57 +0000247 if (N->isMachineOpcode()) {
David Goodwindc4bdcd2009-08-19 16:08:58 +0000248 SU->Latency += InstrItins.
249 getStageLatency(TII->get(N->getMachineOpcode()).getSchedClass());
Dan Gohman343f0c02008-11-19 23:18:57 +0000250 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000251}
252
253/// CountResults - The results of target nodes have register or immediate
254/// operands first, then an optional chain, and optional flag operands (which do
255/// not go into the resulting MachineInstr).
256unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) {
257 unsigned N = Node->getNumValues();
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 while (N && Node->getValueType(N - 1) == MVT::Flag)
Dan Gohman343f0c02008-11-19 23:18:57 +0000259 --N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 if (N && Node->getValueType(N - 1) == MVT::Other)
Dan Gohman343f0c02008-11-19 23:18:57 +0000261 --N; // Skip over chain result.
262 return N;
263}
264
265/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000266/// followed by an optional chain operand, then an optional flag operand.
267/// Compute the number of actual operands that will go into the resulting
268/// MachineInstr.
Dan Gohman343f0c02008-11-19 23:18:57 +0000269unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000270 unsigned N = Node->getNumOperands();
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Dan Gohman343f0c02008-11-19 23:18:57 +0000272 --N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Dan Gohman343f0c02008-11-19 23:18:57 +0000274 --N; // Ignore chain if it exists.
275 return N;
276}
277
Dan Gohman343f0c02008-11-19 23:18:57 +0000278void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000279 if (!SU->getNode()) {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000280 errs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000281 return;
282 }
283
284 SU->getNode()->dump(DAG);
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000285 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000286 SmallVector<SDNode *, 4> FlaggedNodes;
287 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
288 FlaggedNodes.push_back(N);
289 while (!FlaggedNodes.empty()) {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000290 errs() << " ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000291 FlaggedNodes.back()->dump(DAG);
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000292 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000293 FlaggedNodes.pop_back();
294 }
295}