blob: 68d0c804d8266c1e6b4844988e154c1879738ee8 [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"
Evan Chenga8efe282010-03-14 19:56:39 +000016#include "SDNodeDbgValue.h"
Dan Gohman84fbac52009-02-06 17:22:58 +000017#include "ScheduleDAGSDNodes.h"
Dan Gohmanbcea8592009-10-10 01:32:21 +000018#include "InstrEmitter.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000019#include "llvm/CodeGen/SelectionDAG.h"
20#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin71046162009-08-13 16:05:04 +000023#include "llvm/Target/TargetSubtarget.h"
Evan Chengc589e032010-01-22 03:36:51 +000024#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/SmallPtrSet.h"
Evan Chengbfcb3052010-03-25 01:38:16 +000026#include "llvm/ADT/SmallSet.h"
Evan Chengc589e032010-01-22 03:36:51 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32
Evan Chengc589e032010-01-22 03:36:51 +000033STATISTIC(LoadsClustered, "Number of loads clustered together");
34
Dan Gohman79ce2762009-01-15 19:20:50 +000035ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
36 : ScheduleDAG(mf) {
Dan Gohman343f0c02008-11-19 23:18:57 +000037}
38
Dan Gohman47ac0f02009-02-11 04:27:20 +000039/// Run - perform scheduling.
40///
41void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
42 MachineBasicBlock::iterator insertPos) {
43 DAG = dag;
44 ScheduleDAG::Run(bb, insertPos);
45}
46
Dan Gohman343f0c02008-11-19 23:18:57 +000047SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
48 SUnit *SU = NewSUnit(Old->getNode());
49 SU->OrigNode = Old->OrigNode;
50 SU->Latency = Old->Latency;
51 SU->isTwoAddress = Old->isTwoAddress;
52 SU->isCommutable = Old->isCommutable;
53 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000054 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Evan Chenge57187c2009-01-16 20:57:18 +000055 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000056 return SU;
57}
58
59/// CheckForPhysRegDependency - Check if the dependency between def and use of
60/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000061/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000062static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
63 const TargetRegisterInfo *TRI,
64 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +000065 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +000066 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
67 return;
68
69 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
70 if (TargetRegisterInfo::isVirtualRegister(Reg))
71 return;
72
73 unsigned ResNo = User->getOperand(2).getResNo();
74 if (Def->isMachineOpcode()) {
75 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
76 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +000077 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +000078 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +000079 const TargetRegisterClass *RC =
80 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
81 Cost = RC->getCopyCost();
82 }
Dan Gohman343f0c02008-11-19 23:18:57 +000083 }
84}
85
Evan Chengc589e032010-01-22 03:36:51 +000086static void AddFlags(SDNode *N, SDValue Flag, bool AddFlag,
87 SelectionDAG *DAG) {
88 SmallVector<EVT, 4> VTs;
89 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
90 VTs.push_back(N->getValueType(i));
91 if (AddFlag)
92 VTs.push_back(MVT::Flag);
93 SmallVector<SDValue, 4> Ops;
94 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
95 Ops.push_back(N->getOperand(i));
96 if (Flag.getNode())
97 Ops.push_back(Flag);
98 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
99 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
100}
101
102/// ClusterNeighboringLoads - Force nearby loads together by "flagging" them.
103/// This function finds loads of the same base and different offsets. If the
104/// offsets are not far apart (target specific), it add MVT::Flag inputs and
105/// outputs to ensure they are scheduled together and in order. This
106/// optimization may benefit some targets by improving cache locality.
107void ScheduleDAGSDNodes::ClusterNeighboringLoads() {
108 SmallPtrSet<SDNode*, 16> Visited;
109 SmallVector<int64_t, 4> Offsets;
110 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
111 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
112 E = DAG->allnodes_end(); NI != E; ++NI) {
113 SDNode *Node = &*NI;
114 if (!Node || !Node->isMachineOpcode())
115 continue;
116
117 unsigned Opc = Node->getMachineOpcode();
118 const TargetInstrDesc &TID = TII->get(Opc);
119 if (!TID.mayLoad())
120 continue;
121
122 SDNode *Chain = 0;
123 unsigned NumOps = Node->getNumOperands();
124 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
125 Chain = Node->getOperand(NumOps-1).getNode();
126 if (!Chain)
127 continue;
128
129 // Look for other loads of the same chain. Find loads that are loading from
130 // the same base pointer and different offsets.
131 Visited.clear();
132 Offsets.clear();
133 O2SMap.clear();
134 bool Cluster = false;
135 SDNode *Base = Node;
136 int64_t BaseOffset;
137 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
138 I != E; ++I) {
139 SDNode *User = *I;
140 if (User == Node || !Visited.insert(User))
141 continue;
142 int64_t Offset1, Offset2;
143 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
144 Offset1 == Offset2)
145 // FIXME: Should be ok if they addresses are identical. But earlier
146 // optimizations really should have eliminated one of the loads.
147 continue;
148 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
149 Offsets.push_back(Offset1);
150 O2SMap.insert(std::make_pair(Offset2, User));
151 Offsets.push_back(Offset2);
152 if (Offset2 < Offset1) {
153 Base = User;
154 BaseOffset = Offset2;
155 } else {
156 BaseOffset = Offset1;
157 }
158 Cluster = true;
159 }
160
161 if (!Cluster)
162 continue;
163
164 // Sort them in increasing order.
165 std::sort(Offsets.begin(), Offsets.end());
166
167 // Check if the loads are close enough.
168 SmallVector<SDNode*, 4> Loads;
169 unsigned NumLoads = 0;
170 int64_t BaseOff = Offsets[0];
171 SDNode *BaseLoad = O2SMap[BaseOff];
172 Loads.push_back(BaseLoad);
173 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
174 int64_t Offset = Offsets[i];
175 SDNode *Load = O2SMap[Offset];
176 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,
177 NumLoads))
178 break; // Stop right here. Ignore loads that are further away.
179 Loads.push_back(Load);
180 ++NumLoads;
181 }
182
183 if (NumLoads == 0)
184 continue;
185
186 // Cluster loads by adding MVT::Flag outputs and inputs. This also
187 // ensure they are scheduled in order of increasing addresses.
188 SDNode *Lead = Loads[0];
189 AddFlags(Lead, SDValue(0,0), true, DAG);
190 SDValue InFlag = SDValue(Lead, Lead->getNumValues()-1);
191 for (unsigned i = 1, e = Loads.size(); i != e; ++i) {
192 bool OutFlag = i < e-1;
193 SDNode *Load = Loads[i];
194 AddFlags(Load, InFlag, OutFlag, DAG);
195 if (OutFlag)
196 InFlag = SDValue(Load, Load->getNumValues()-1);
197 ++LoadsClustered;
198 }
199 }
200}
201
Dan Gohman343f0c02008-11-19 23:18:57 +0000202void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000203 // During scheduling, the NodeId field of SDNode is used to map SDNodes
204 // to their associated SUnits by holding SUnits table indices. A value
205 // of -1 means the SDNode does not yet have an associated SUnit.
206 unsigned NumNodes = 0;
207 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
208 E = DAG->allnodes_end(); NI != E; ++NI) {
209 NI->setNodeId(-1);
210 ++NumNodes;
211 }
212
Dan Gohman343f0c02008-11-19 23:18:57 +0000213 // Reserve entries in the vector for each of the SUnits we are creating. This
214 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
215 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000216 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
217 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000218 SUnits.reserve(NumNodes * 2);
Dan Gohman343f0c02008-11-19 23:18:57 +0000219
Chris Lattner736a6ea2010-02-24 06:11:37 +0000220 // Add all nodes in depth first order.
221 SmallVector<SDNode*, 64> Worklist;
222 SmallPtrSet<SDNode*, 64> Visited;
223 Worklist.push_back(DAG->getRoot().getNode());
224 Visited.insert(DAG->getRoot().getNode());
225
226 while (!Worklist.empty()) {
227 SDNode *NI = Worklist.pop_back_val();
228
229 // Add all operands to the worklist unless they've already been added.
230 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
231 if (Visited.insert(NI->getOperand(i).getNode()))
232 Worklist.push_back(NI->getOperand(i).getNode());
233
Dan Gohman343f0c02008-11-19 23:18:57 +0000234 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
235 continue;
236
237 // If this node has already been processed, stop now.
238 if (NI->getNodeId() != -1) continue;
239
240 SUnit *NodeSUnit = NewSUnit(NI);
241
242 // See if anything is flagged to this node, if so, add them to flagged
243 // nodes. Nodes can have at most one flag input and one flag output. Flags
Dan Gohmandb95fa12009-03-20 20:42:23 +0000244 // are required to be the last operand and result of a node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000245
246 // Scan up to find flagged preds.
247 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000248 while (N->getNumOperands() &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000250 N = N->getOperand(N->getNumOperands()-1).getNode();
251 assert(N->getNodeId() == -1 && "Node already inserted!");
252 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman343f0c02008-11-19 23:18:57 +0000253 }
254
255 // Scan down to find any flagged succs.
256 N = NI;
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000258 SDValue FlagVal(N, N->getNumValues()-1);
259
260 // There are either zero or one users of the Flag result.
261 bool HasFlagUse = false;
262 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
263 UI != E; ++UI)
264 if (FlagVal.isOperandOf(*UI)) {
265 HasFlagUse = true;
266 assert(N->getNodeId() == -1 && "Node already inserted!");
267 N->setNodeId(NodeSUnit->NodeNum);
268 N = *UI;
269 break;
270 }
271 if (!HasFlagUse) break;
272 }
273
274 // If there are flag operands involved, N is now the bottom-most node
275 // of the sequence of nodes that are flagged together.
276 // Update the SUnit.
277 NodeSUnit->setNode(N);
278 assert(N->getNodeId() == -1 && "Node already inserted!");
279 N->setNodeId(NodeSUnit->NodeNum);
280
Dan Gohman787782f2008-11-21 01:44:51 +0000281 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000282 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000283 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000284}
285
286void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000287 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
288
David Goodwindc4bdcd2009-08-19 16:08:58 +0000289 // Check to see if the scheduler cares about latencies.
290 bool UnitLatencies = ForceUnitLatencies();
291
Dan Gohman343f0c02008-11-19 23:18:57 +0000292 // Pass 2: add the preds, succs, etc.
293 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
294 SUnit *SU = &SUnits[su];
295 SDNode *MainNode = SU->getNode();
296
297 if (MainNode->isMachineOpcode()) {
298 unsigned Opc = MainNode->getMachineOpcode();
299 const TargetInstrDesc &TID = TII->get(Opc);
300 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
301 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
302 SU->isTwoAddress = true;
303 break;
304 }
305 }
306 if (TID.isCommutable())
307 SU->isCommutable = true;
308 }
309
310 // Find all predecessors and successors of the group.
311 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
312 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000313 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
314 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000315 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000316 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
317 --NumUsed; // Skip over unused values at the end.
318 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000319 SU->hasPhysRegDefs = true;
320 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000321
322 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
323 SDNode *OpN = N->getOperand(i).getNode();
324 if (isPassiveNode(OpN)) continue; // Not scheduled.
325 SUnit *OpSU = &SUnits[OpN->getNodeId()];
326 assert(OpSU && "Node has no SUnit!");
327 if (OpSU == SU) continue; // In the same group.
328
Owen Andersone50ed302009-08-10 22:56:29 +0000329 EVT OpVT = N->getOperand(i).getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
331 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000332
333 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000334 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000335 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000336 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000337 assert((PhysReg == 0 || !isChain) &&
338 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000339 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
340 // emits a copy from the physical register to a virtual register unless
341 // it requires a cross class copy (cost < 0). That means we are only
342 // treating "expensive to copy" register dependency as physical register
343 // dependency. This may change in the future though.
344 if (Cost >= 0)
345 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000346
347 const SDep& dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
348 OpSU->Latency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000349 if (!isChain && !UnitLatencies) {
Dan Gohman3fb150a2010-04-17 17:42:52 +0000350 ComputeOperandLatency(OpSU, SU, const_cast<SDep &>(dep));
351 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000352 }
David Goodwin71046162009-08-13 16:05:04 +0000353
354 SU->addPred(dep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000355 }
356 }
357 }
358}
359
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000360/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
361/// are input. This SUnit graph is similar to the SelectionDAG, but
362/// excludes nodes that aren't interesting to scheduling, and represents
363/// flagged together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000364void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Chengc589e032010-01-22 03:36:51 +0000365 // Cluster loads from "near" addresses into combined SUnits.
Evan Cheng42dae2d2010-01-22 23:49:45 +0000366 ClusterNeighboringLoads();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000367 // Populate the SUnits array.
368 BuildSchedUnits();
369 // Compute all the scheduling dependencies between nodes.
370 AddSchedEdges();
371}
372
Dan Gohman343f0c02008-11-19 23:18:57 +0000373void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Evan Chenge1631682010-05-19 22:42:23 +0000374 // Check to see if the scheduler cares about latencies.
375 if (ForceUnitLatencies()) {
376 SU->Latency = 1;
377 return;
378 }
379
Dan Gohman343f0c02008-11-19 23:18:57 +0000380 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
381
382 // Compute the latency for the node. We use the sum of the latencies for
383 // all nodes flagged together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000384 SU->Latency = 0;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000385 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
Dan Gohman343f0c02008-11-19 23:18:57 +0000386 if (N->isMachineOpcode()) {
David Goodwindc4bdcd2009-08-19 16:08:58 +0000387 SU->Latency += InstrItins.
388 getStageLatency(TII->get(N->getMachineOpcode()).getSchedClass());
Dan Gohman343f0c02008-11-19 23:18:57 +0000389 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000390}
391
Dan Gohman343f0c02008-11-19 23:18:57 +0000392void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000393 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000394 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000395 return;
396 }
397
398 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000399 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000400 SmallVector<SDNode *, 4> FlaggedNodes;
401 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
402 FlaggedNodes.push_back(N);
403 while (!FlaggedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000404 dbgs() << " ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000405 FlaggedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000406 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000407 FlaggedNodes.pop_back();
408 }
409}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000410
Evan Chengbfcb3052010-03-25 01:38:16 +0000411namespace {
412 struct OrderSorter {
413 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
414 const std::pair<unsigned, MachineInstr*> &B) {
415 return A.first < B.first;
416 }
417 };
418}
419
420// ProcessSourceNode - Process nodes with source order numbers. These are added
421// to a vector which EmitSchedule use to determine how to insert dbg_value
422// instructions in the right order.
423static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
424 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000425 DenseMap<SDValue, unsigned> &VRBaseMap,
426 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
427 SmallSet<unsigned, 8> &Seen) {
428 unsigned Order = DAG->GetOrdering(N);
429 if (!Order || !Seen.insert(Order))
430 return;
431
432 MachineBasicBlock *BB = Emitter.getBlock();
433 if (BB->empty() || BB->back().isPHI()) {
434 // Did not insert any instruction.
435 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
436 return;
437 }
438
439 Orders.push_back(std::make_pair(Order, &BB->back()));
440 if (!N->getHasDebugValue())
441 return;
442 // Opportunistically insert immediate dbg_value uses, i.e. those with source
443 // order number right after the N.
444 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
445 SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N);
446 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
447 if (DVs[i]->isInvalidated())
448 continue;
449 unsigned DVOrder = DVs[i]->getOrder();
450 if (DVOrder == ++Order) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000451 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000452 if (DbgMI) {
453 Orders.push_back(std::make_pair(DVOrder, DbgMI));
454 BB->insert(InsertPos, DbgMI);
455 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000456 DVs[i]->setIsInvalidated();
457 }
458 }
459}
460
461
Dan Gohmanbcea8592009-10-10 01:32:21 +0000462/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000463MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000464 InstrEmitter Emitter(BB, InsertPos);
465 DenseMap<SDValue, unsigned> VRBaseMap;
466 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000467 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
468 SmallSet<unsigned, 8> Seen;
469 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000470
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000471 // If this is the first BB, emit byval parameter dbg_value's.
472 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
473 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
474 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
475 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000476 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000477 if (DbgMI)
478 BB->insert(BB->end(), DbgMI);
479 }
480 }
481
Dan Gohmanbcea8592009-10-10 01:32:21 +0000482 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
483 SUnit *SU = Sequence[i];
484 if (!SU) {
485 // Null SUnit* is a noop.
486 EmitNoop();
487 continue;
488 }
489
490 // For pre-regalloc scheduling, create instructions corresponding to the
491 // SDNode and any flagged SDNodes and append them to the block.
492 if (!SU->getNode()) {
493 // Emit a copy.
494 EmitPhysRegCopy(SU, CopyVRBaseMap);
495 continue;
496 }
497
498 SmallVector<SDNode *, 4> FlaggedNodes;
499 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
500 N = N->getFlaggedNode())
501 FlaggedNodes.push_back(N);
502 while (!FlaggedNodes.empty()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000503 SDNode *N = FlaggedNodes.back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000504 Emitter.EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000505 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000506 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000507 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000508 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000509 FlaggedNodes.pop_back();
510 }
511 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000512 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000513 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000514 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000515 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000516 Seen);
517 }
518
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000519 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000520 // order sequence.
521 if (HasDbg) {
522 MachineBasicBlock::iterator BBBegin = BB->empty() ? BB->end() : BB->begin();
523 while (BBBegin != BB->end() && BBBegin->isPHI())
524 ++BBBegin;
525
526 // Sort the source order instructions and use the order to insert debug
527 // values.
528 std::sort(Orders.begin(), Orders.end(), OrderSorter());
529
530 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
531 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
532 // Now emit the rest according to source order.
533 unsigned LastOrder = 0;
534 MachineInstr *LastMI = 0;
535 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
536 unsigned Order = Orders[i].first;
537 MachineInstr *MI = Orders[i].second;
538 // Insert all SDDbgValue's whose order(s) are before "Order".
539 if (!MI)
540 continue;
541 MachineBasicBlock *MIBB = MI->getParent();
Evan Cheng4ec9bd92010-03-25 07:16:57 +0000542#ifndef NDEBUG
543 unsigned LastDIOrder = 0;
544#endif
Evan Chengbfcb3052010-03-25 01:38:16 +0000545 for (; DI != DE &&
546 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
Evan Cheng4ec9bd92010-03-25 07:16:57 +0000547#ifndef NDEBUG
548 assert((*DI)->getOrder() >= LastDIOrder &&
549 "SDDbgValue nodes must be in source order!");
550 LastDIOrder = (*DI)->getOrder();
551#endif
Evan Chengbfcb3052010-03-25 01:38:16 +0000552 if ((*DI)->isInvalidated())
553 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000554 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000555 if (DbgMI) {
556 if (!LastOrder)
557 // Insert to start of the BB (after PHIs).
558 BB->insert(BBBegin, DbgMI);
559 else {
560 MachineBasicBlock::iterator Pos = MI;
561 MIBB->insert(llvm::next(Pos), DbgMI);
562 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000563 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000564 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000565 LastOrder = Order;
566 LastMI = MI;
567 }
568 // Add trailing DbgValue's before the terminator. FIXME: May want to add
569 // some of them before one or more conditional branches?
570 while (DI != DE) {
571 MachineBasicBlock *InsertBB = Emitter.getBlock();
572 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
573 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000574 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000575 if (DbgMI)
576 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000577 }
578 ++DI;
579 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000580 }
581
582 BB = Emitter.getBlock();
583 InsertPos = Emitter.getInsertPos();
584 return BB;
585}