blob: b51c61bf6da15e5a69132d8e4f2252748bb6ce16 [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 Gohmanbcea8592009-10-10 01:32:21 +000017#include "InstrEmitter.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000018#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin71046162009-08-13 16:05:04 +000022#include "llvm/Target/TargetSubtarget.h"
Evan Chengc589e032010-01-22 03:36:51 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/Statistic.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
30
Evan Chengc589e032010-01-22 03:36:51 +000031STATISTIC(LoadsClustered, "Number of loads clustered together");
32
Dan Gohman79ce2762009-01-15 19:20:50 +000033ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
34 : ScheduleDAG(mf) {
Dan Gohman343f0c02008-11-19 23:18:57 +000035}
36
Dan Gohman47ac0f02009-02-11 04:27:20 +000037/// Run - perform scheduling.
38///
39void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
40 MachineBasicBlock::iterator insertPos) {
41 DAG = dag;
42 ScheduleDAG::Run(bb, insertPos);
43}
44
Dan Gohman343f0c02008-11-19 23:18:57 +000045SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
46 SUnit *SU = NewSUnit(Old->getNode());
47 SU->OrigNode = Old->OrigNode;
48 SU->Latency = Old->Latency;
49 SU->isTwoAddress = Old->isTwoAddress;
50 SU->isCommutable = Old->isCommutable;
51 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000052 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Evan Chenge57187c2009-01-16 20:57:18 +000053 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000054 return SU;
55}
56
57/// CheckForPhysRegDependency - Check if the dependency between def and use of
58/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000059/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000060static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
61 const TargetRegisterInfo *TRI,
62 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +000063 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +000064 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
65 return;
66
67 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
68 if (TargetRegisterInfo::isVirtualRegister(Reg))
69 return;
70
71 unsigned ResNo = User->getOperand(2).getResNo();
72 if (Def->isMachineOpcode()) {
73 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
74 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +000075 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +000076 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +000077 const TargetRegisterClass *RC =
78 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
79 Cost = RC->getCopyCost();
80 }
Dan Gohman343f0c02008-11-19 23:18:57 +000081 }
82}
83
Evan Chengc589e032010-01-22 03:36:51 +000084static void AddFlags(SDNode *N, SDValue Flag, bool AddFlag,
85 SelectionDAG *DAG) {
86 SmallVector<EVT, 4> VTs;
87 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
88 VTs.push_back(N->getValueType(i));
89 if (AddFlag)
90 VTs.push_back(MVT::Flag);
91 SmallVector<SDValue, 4> Ops;
92 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
93 Ops.push_back(N->getOperand(i));
94 if (Flag.getNode())
95 Ops.push_back(Flag);
96 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
97 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
98}
99
100/// ClusterNeighboringLoads - Force nearby loads together by "flagging" them.
101/// This function finds loads of the same base and different offsets. If the
102/// offsets are not far apart (target specific), it add MVT::Flag inputs and
103/// outputs to ensure they are scheduled together and in order. This
104/// optimization may benefit some targets by improving cache locality.
105void ScheduleDAGSDNodes::ClusterNeighboringLoads() {
106 SmallPtrSet<SDNode*, 16> Visited;
107 SmallVector<int64_t, 4> Offsets;
108 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
109 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
110 E = DAG->allnodes_end(); NI != E; ++NI) {
111 SDNode *Node = &*NI;
112 if (!Node || !Node->isMachineOpcode())
113 continue;
114
115 unsigned Opc = Node->getMachineOpcode();
116 const TargetInstrDesc &TID = TII->get(Opc);
117 if (!TID.mayLoad())
118 continue;
119
120 SDNode *Chain = 0;
121 unsigned NumOps = Node->getNumOperands();
122 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
123 Chain = Node->getOperand(NumOps-1).getNode();
124 if (!Chain)
125 continue;
126
127 // Look for other loads of the same chain. Find loads that are loading from
128 // the same base pointer and different offsets.
129 Visited.clear();
130 Offsets.clear();
131 O2SMap.clear();
132 bool Cluster = false;
133 SDNode *Base = Node;
134 int64_t BaseOffset;
135 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
136 I != E; ++I) {
137 SDNode *User = *I;
138 if (User == Node || !Visited.insert(User))
139 continue;
140 int64_t Offset1, Offset2;
141 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
142 Offset1 == Offset2)
143 // FIXME: Should be ok if they addresses are identical. But earlier
144 // optimizations really should have eliminated one of the loads.
145 continue;
146 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
147 Offsets.push_back(Offset1);
148 O2SMap.insert(std::make_pair(Offset2, User));
149 Offsets.push_back(Offset2);
150 if (Offset2 < Offset1) {
151 Base = User;
152 BaseOffset = Offset2;
153 } else {
154 BaseOffset = Offset1;
155 }
156 Cluster = true;
157 }
158
159 if (!Cluster)
160 continue;
161
162 // Sort them in increasing order.
163 std::sort(Offsets.begin(), Offsets.end());
164
165 // Check if the loads are close enough.
166 SmallVector<SDNode*, 4> Loads;
167 unsigned NumLoads = 0;
168 int64_t BaseOff = Offsets[0];
169 SDNode *BaseLoad = O2SMap[BaseOff];
170 Loads.push_back(BaseLoad);
171 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
172 int64_t Offset = Offsets[i];
173 SDNode *Load = O2SMap[Offset];
174 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,
175 NumLoads))
176 break; // Stop right here. Ignore loads that are further away.
177 Loads.push_back(Load);
178 ++NumLoads;
179 }
180
181 if (NumLoads == 0)
182 continue;
183
184 // Cluster loads by adding MVT::Flag outputs and inputs. This also
185 // ensure they are scheduled in order of increasing addresses.
186 SDNode *Lead = Loads[0];
187 AddFlags(Lead, SDValue(0,0), true, DAG);
188 SDValue InFlag = SDValue(Lead, Lead->getNumValues()-1);
189 for (unsigned i = 1, e = Loads.size(); i != e; ++i) {
190 bool OutFlag = i < e-1;
191 SDNode *Load = Loads[i];
192 AddFlags(Load, InFlag, OutFlag, DAG);
193 if (OutFlag)
194 InFlag = SDValue(Load, Load->getNumValues()-1);
195 ++LoadsClustered;
196 }
197 }
198}
199
Dan Gohman343f0c02008-11-19 23:18:57 +0000200void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000201 // During scheduling, the NodeId field of SDNode is used to map SDNodes
202 // to their associated SUnits by holding SUnits table indices. A value
203 // of -1 means the SDNode does not yet have an associated SUnit.
204 unsigned NumNodes = 0;
205 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
206 E = DAG->allnodes_end(); NI != E; ++NI) {
207 NI->setNodeId(-1);
208 ++NumNodes;
209 }
210
Dan Gohman343f0c02008-11-19 23:18:57 +0000211 // Reserve entries in the vector for each of the SUnits we are creating. This
212 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
213 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000214 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
215 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000216 SUnits.reserve(NumNodes * 2);
Dan Gohman343f0c02008-11-19 23:18:57 +0000217
Dan Gohman3f237442008-12-16 03:25:46 +0000218 // Check to see if the scheduler cares about latencies.
219 bool UnitLatencies = ForceUnitLatencies();
220
Dan Gohman343f0c02008-11-19 23:18:57 +0000221 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
222 E = DAG->allnodes_end(); NI != E; ++NI) {
223 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
224 continue;
225
226 // If this node has already been processed, stop now.
227 if (NI->getNodeId() != -1) continue;
228
229 SUnit *NodeSUnit = NewSUnit(NI);
230
231 // See if anything is flagged to this node, if so, add them to flagged
232 // nodes. Nodes can have at most one flag input and one flag output. Flags
Dan Gohmandb95fa12009-03-20 20:42:23 +0000233 // are required to be the last operand and result of a node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000234
235 // Scan up to find flagged preds.
236 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000237 while (N->getNumOperands() &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000239 N = N->getOperand(N->getNumOperands()-1).getNode();
240 assert(N->getNodeId() == -1 && "Node already inserted!");
241 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman343f0c02008-11-19 23:18:57 +0000242 }
243
244 // Scan down to find any flagged succs.
245 N = NI;
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000247 SDValue FlagVal(N, N->getNumValues()-1);
248
249 // There are either zero or one users of the Flag result.
250 bool HasFlagUse = false;
251 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
252 UI != E; ++UI)
253 if (FlagVal.isOperandOf(*UI)) {
254 HasFlagUse = true;
255 assert(N->getNodeId() == -1 && "Node already inserted!");
256 N->setNodeId(NodeSUnit->NodeNum);
257 N = *UI;
258 break;
259 }
260 if (!HasFlagUse) break;
261 }
262
263 // If there are flag operands involved, N is now the bottom-most node
264 // of the sequence of nodes that are flagged together.
265 // Update the SUnit.
266 NodeSUnit->setNode(N);
267 assert(N->getNodeId() == -1 && "Node already inserted!");
268 N->setNodeId(NodeSUnit->NodeNum);
269
Dan Gohman787782f2008-11-21 01:44:51 +0000270 // Assign the Latency field of NodeSUnit using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000271 if (UnitLatencies)
272 NodeSUnit->Latency = 1;
273 else
274 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000275 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000276}
277
278void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000279 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
280
David Goodwindc4bdcd2009-08-19 16:08:58 +0000281 // Check to see if the scheduler cares about latencies.
282 bool UnitLatencies = ForceUnitLatencies();
283
Dan Gohman343f0c02008-11-19 23:18:57 +0000284 // Pass 2: add the preds, succs, etc.
285 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
286 SUnit *SU = &SUnits[su];
287 SDNode *MainNode = SU->getNode();
288
289 if (MainNode->isMachineOpcode()) {
290 unsigned Opc = MainNode->getMachineOpcode();
291 const TargetInstrDesc &TID = TII->get(Opc);
292 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
293 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
294 SU->isTwoAddress = true;
295 break;
296 }
297 }
298 if (TID.isCommutable())
299 SU->isCommutable = true;
300 }
301
302 // Find all predecessors and successors of the group.
303 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
304 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000305 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
306 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000307 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000308 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
309 --NumUsed; // Skip over unused values at the end.
310 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000311 SU->hasPhysRegDefs = true;
312 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000313
314 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
315 SDNode *OpN = N->getOperand(i).getNode();
316 if (isPassiveNode(OpN)) continue; // Not scheduled.
317 SUnit *OpSU = &SUnits[OpN->getNodeId()];
318 assert(OpSU && "Node has no SUnit!");
319 if (OpSU == SU) continue; // In the same group.
320
Owen Andersone50ed302009-08-10 22:56:29 +0000321 EVT OpVT = N->getOperand(i).getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
323 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000324
325 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000326 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000327 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000328 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000329 assert((PhysReg == 0 || !isChain) &&
330 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000331 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
332 // emits a copy from the physical register to a virtual register unless
333 // it requires a cross class copy (cost < 0). That means we are only
334 // treating "expensive to copy" register dependency as physical register
335 // dependency. This may change in the future though.
336 if (Cost >= 0)
337 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000338
339 const SDep& dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
340 OpSU->Latency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000341 if (!isChain && !UnitLatencies) {
342 ComputeOperandLatency(OpSU, SU, (SDep &)dep);
343 ST.adjustSchedDependency(OpSU, SU, (SDep &)dep);
344 }
David Goodwin71046162009-08-13 16:05:04 +0000345
346 SU->addPred(dep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000347 }
348 }
349 }
350}
351
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000352/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
353/// are input. This SUnit graph is similar to the SelectionDAG, but
354/// excludes nodes that aren't interesting to scheduling, and represents
355/// flagged together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000356void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Chengc589e032010-01-22 03:36:51 +0000357 // Cluster loads from "near" addresses into combined SUnits.
Evan Cheng42dae2d2010-01-22 23:49:45 +0000358 ClusterNeighboringLoads();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000359 // Populate the SUnits array.
360 BuildSchedUnits();
361 // Compute all the scheduling dependencies between nodes.
362 AddSchedEdges();
363}
364
Dan Gohman343f0c02008-11-19 23:18:57 +0000365void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
366 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
367
368 // Compute the latency for the node. We use the sum of the latencies for
369 // all nodes flagged together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000370 SU->Latency = 0;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000371 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
Dan Gohman343f0c02008-11-19 23:18:57 +0000372 if (N->isMachineOpcode()) {
David Goodwindc4bdcd2009-08-19 16:08:58 +0000373 SU->Latency += InstrItins.
374 getStageLatency(TII->get(N->getMachineOpcode()).getSchedClass());
Dan Gohman343f0c02008-11-19 23:18:57 +0000375 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000376}
377
Dan Gohman343f0c02008-11-19 23:18:57 +0000378void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000379 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000380 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000381 return;
382 }
383
384 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000385 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000386 SmallVector<SDNode *, 4> FlaggedNodes;
387 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
388 FlaggedNodes.push_back(N);
389 while (!FlaggedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000390 dbgs() << " ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000391 FlaggedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000392 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000393 FlaggedNodes.pop_back();
394 }
395}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000396
397/// EmitSchedule - Emit the machine code in scheduled order.
398MachineBasicBlock *ScheduleDAGSDNodes::
399EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
400 InstrEmitter Emitter(BB, InsertPos);
401 DenseMap<SDValue, unsigned> VRBaseMap;
402 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
403 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
404 SUnit *SU = Sequence[i];
405 if (!SU) {
406 // Null SUnit* is a noop.
407 EmitNoop();
408 continue;
409 }
410
411 // For pre-regalloc scheduling, create instructions corresponding to the
412 // SDNode and any flagged SDNodes and append them to the block.
413 if (!SU->getNode()) {
414 // Emit a copy.
415 EmitPhysRegCopy(SU, CopyVRBaseMap);
416 continue;
417 }
418
419 SmallVector<SDNode *, 4> FlaggedNodes;
420 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
421 N = N->getFlaggedNode())
422 FlaggedNodes.push_back(N);
423 while (!FlaggedNodes.empty()) {
424 Emitter.EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,
425 VRBaseMap, EM);
426 FlaggedNodes.pop_back();
427 }
428 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
429 VRBaseMap, EM);
430 }
431
432 BB = Emitter.getBlock();
433 InsertPos = Emitter.getInsertPos();
434 return BB;
435}