blob: b258e6eefe20958fe8516b1da5708b317517ee32 [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"
Evan Cheng1cc39842010-05-20 23:26:43 +000022#include "llvm/Target/TargetLowering.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin71046162009-08-13 16:05:04 +000024#include "llvm/Target/TargetSubtarget.h"
Evan Chengc589e032010-01-22 03:36:51 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/SmallPtrSet.h"
Evan Chengbfcb3052010-03-25 01:38:16 +000027#include "llvm/ADT/SmallSet.h"
Evan Chengc589e032010-01-22 03:36:51 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/Statistic.h"
Andrew Tricke0ef5092011-03-05 08:00:22 +000030#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34
Evan Chengc589e032010-01-22 03:36:51 +000035STATISTIC(LoadsClustered, "Number of loads clustered together");
36
Andrew Tricke0ef5092011-03-05 08:00:22 +000037// This allows latency based scheduler to notice high latency instructions
38// without a target itinerary. The choise if number here has more to do with
39// balancing scheduler heursitics than with the actual machine latency.
40static cl::opt<int> HighLatencyCycles(
41 "sched-high-latency-cycles", cl::Hidden, cl::init(10),
42 cl::desc("Roughly estimate the number of cycles that 'long latency'"
43 "instructions take for targets with no itinerary"));
44
Dan Gohman79ce2762009-01-15 19:20:50 +000045ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
Evan Cheng3ef1c872010-09-10 01:29:16 +000046 : ScheduleDAG(mf),
47 InstrItins(mf.getTarget().getInstrItineraryData()) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000048
Dan Gohman47ac0f02009-02-11 04:27:20 +000049/// Run - perform scheduling.
50///
51void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
52 MachineBasicBlock::iterator insertPos) {
53 DAG = dag;
54 ScheduleDAG::Run(bb, insertPos);
55}
56
Evan Cheng1cc39842010-05-20 23:26:43 +000057/// NewSUnit - Creates a new SUnit and return a ptr to it.
58///
59SUnit *ScheduleDAGSDNodes::NewSUnit(SDNode *N) {
60#ifndef NDEBUG
61 const SUnit *Addr = 0;
62 if (!SUnits.empty())
63 Addr = &SUnits[0];
64#endif
65 SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
66 assert((Addr == 0 || Addr == &SUnits[0]) &&
67 "SUnits std::vector reallocated on the fly!");
68 SUnits.back().OrigNode = &SUnits.back();
69 SUnit *SU = &SUnits.back();
70 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
Evan Chengc120af42010-08-10 02:39:45 +000071 if (!N ||
72 (N->isMachineOpcode() &&
73 N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
Evan Cheng046fa3f2010-05-28 23:26:21 +000074 SU->SchedulingPref = Sched::None;
75 else
76 SU->SchedulingPref = TLI.getSchedulingPreference(N);
Evan Cheng1cc39842010-05-20 23:26:43 +000077 return SU;
78}
79
Dan Gohman343f0c02008-11-19 23:18:57 +000080SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
81 SUnit *SU = NewSUnit(Old->getNode());
82 SU->OrigNode = Old->OrigNode;
83 SU->Latency = Old->Latency;
Andrew Trick54699762011-04-07 19:54:57 +000084 SU->isVRegCycle = Old->isVRegCycle;
Evan Cheng8239daf2010-11-03 00:45:17 +000085 SU->isCall = Old->isCall;
Dan Gohman343f0c02008-11-19 23:18:57 +000086 SU->isTwoAddress = Old->isTwoAddress;
87 SU->isCommutable = Old->isCommutable;
88 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000089 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Andrew Trick12f0dc62011-04-14 05:15:06 +000090 SU->isScheduleHigh = Old->isScheduleHigh;
91 SU->isScheduleLow = Old->isScheduleLow;
Evan Cheng1cc39842010-05-20 23:26:43 +000092 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000093 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000094 return SU;
95}
96
97/// CheckForPhysRegDependency - Check if the dependency between def and use of
98/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000099/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +0000100static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +0000101 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +0000102 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000103 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000104 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
105 return;
106
107 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
108 if (TargetRegisterInfo::isVirtualRegister(Reg))
109 return;
110
111 unsigned ResNo = User->getOperand(2).getResNo();
112 if (Def->isMachineOpcode()) {
113 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
114 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000115 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000116 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000117 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000118 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000119 Cost = RC->getCopyCost();
120 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000121 }
122}
123
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000124static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000125 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000126 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000127
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000128 // Don't add glue from a node to itself.
129 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000130
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000131 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000132 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000133
134 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
135 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000136
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000137 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000138 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000139
Evan Chengc589e032010-01-22 03:36:51 +0000140 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000141 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
142 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000143
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000144 if (GlueDestNode)
145 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000146
Evan Chengc589e032010-01-22 03:36:51 +0000147 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000148 MachineSDNode::mmo_iterator Begin = 0, End = 0;
149 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
150
151 // Store memory references.
152 if (MN) {
153 Begin = MN->memoperands_begin();
154 End = MN->memoperands_end();
155 }
156
Evan Chengc589e032010-01-22 03:36:51 +0000157 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000158
159 // Reset the memory references
160 if (MN)
161 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000162}
163
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000164/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000165/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000166/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000167/// outputs to ensure they are scheduled together and in order. This
168/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000169void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
170 SDNode *Chain = 0;
171 unsigned NumOps = Node->getNumOperands();
172 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
173 Chain = Node->getOperand(NumOps-1).getNode();
174 if (!Chain)
175 return;
176
177 // Look for other loads of the same chain. Find loads that are loading from
178 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000179 SmallPtrSet<SDNode*, 16> Visited;
180 SmallVector<int64_t, 4> Offsets;
181 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000182 bool Cluster = false;
183 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000184 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
185 I != E; ++I) {
186 SDNode *User = *I;
187 if (User == Node || !Visited.insert(User))
188 continue;
189 int64_t Offset1, Offset2;
190 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
191 Offset1 == Offset2)
192 // FIXME: Should be ok if they addresses are identical. But earlier
193 // optimizations really should have eliminated one of the loads.
194 continue;
195 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
196 Offsets.push_back(Offset1);
197 O2SMap.insert(std::make_pair(Offset2, User));
198 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000199 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000200 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000201 Cluster = true;
202 }
203
204 if (!Cluster)
205 return;
206
207 // Sort them in increasing order.
208 std::sort(Offsets.begin(), Offsets.end());
209
210 // Check if the loads are close enough.
211 SmallVector<SDNode*, 4> Loads;
212 unsigned NumLoads = 0;
213 int64_t BaseOff = Offsets[0];
214 SDNode *BaseLoad = O2SMap[BaseOff];
215 Loads.push_back(BaseLoad);
216 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
217 int64_t Offset = Offsets[i];
218 SDNode *Load = O2SMap[Offset];
219 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
220 break; // Stop right here. Ignore loads that are further away.
221 Loads.push_back(Load);
222 ++NumLoads;
223 }
224
225 if (NumLoads == 0)
226 return;
227
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000228 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000229 // ensure they are scheduled in order of increasing addresses.
230 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000231 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000232
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000233 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000234 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000235 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000236 SDNode *Load = Loads[I];
237
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000238 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000239
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000240 if (OutGlue)
241 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000242
Evan Cheng302ef832010-06-10 02:09:31 +0000243 ++LoadsClustered;
244 }
245}
246
247/// ClusterNodes - Cluster certain nodes which should be scheduled together.
248///
249void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000250 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
251 E = DAG->allnodes_end(); NI != E; ++NI) {
252 SDNode *Node = &*NI;
253 if (!Node || !Node->isMachineOpcode())
254 continue;
255
256 unsigned Opc = Node->getMachineOpcode();
257 const TargetInstrDesc &TID = TII->get(Opc);
Evan Cheng302ef832010-06-10 02:09:31 +0000258 if (TID.mayLoad())
259 // Cluster loads from "near" addresses into combined SUnits.
260 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000261 }
262}
263
Dan Gohman343f0c02008-11-19 23:18:57 +0000264void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000265 // During scheduling, the NodeId field of SDNode is used to map SDNodes
266 // to their associated SUnits by holding SUnits table indices. A value
267 // of -1 means the SDNode does not yet have an associated SUnit.
268 unsigned NumNodes = 0;
269 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
270 E = DAG->allnodes_end(); NI != E; ++NI) {
271 NI->setNodeId(-1);
272 ++NumNodes;
273 }
274
Dan Gohman343f0c02008-11-19 23:18:57 +0000275 // Reserve entries in the vector for each of the SUnits we are creating. This
276 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
277 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000278 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
279 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000280 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000281
Chris Lattner736a6ea2010-02-24 06:11:37 +0000282 // Add all nodes in depth first order.
283 SmallVector<SDNode*, 64> Worklist;
284 SmallPtrSet<SDNode*, 64> Visited;
285 Worklist.push_back(DAG->getRoot().getNode());
286 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000287
Chris Lattner736a6ea2010-02-24 06:11:37 +0000288 while (!Worklist.empty()) {
289 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000290
Chris Lattner736a6ea2010-02-24 06:11:37 +0000291 // Add all operands to the worklist unless they've already been added.
292 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
293 if (Visited.insert(NI->getOperand(i).getNode()))
294 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000295
Dan Gohman343f0c02008-11-19 23:18:57 +0000296 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
297 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000298
Dan Gohman343f0c02008-11-19 23:18:57 +0000299 // If this node has already been processed, stop now.
300 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000301
Dan Gohman343f0c02008-11-19 23:18:57 +0000302 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000303
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000304 // See if anything is glued to this node, if so, add them to glued
305 // nodes. Nodes can have at most one glue input and one glue output. Glue
306 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000307
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000308 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000309 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000310 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000311 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000312 N = N->getOperand(N->getNumOperands()-1).getNode();
313 assert(N->getNodeId() == -1 && "Node already inserted!");
314 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000315 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
316 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000317 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000318
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000319 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000320 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000321 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000322 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000323
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000324 // There are either zero or one users of the Glue result.
325 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000326 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000327 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000328 if (GlueVal.isOperandOf(*UI)) {
329 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000330 assert(N->getNodeId() == -1 && "Node already inserted!");
331 N->setNodeId(NodeSUnit->NodeNum);
332 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000333 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
334 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000335 break;
336 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000337 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000338 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000339
Andrew Trick12f0dc62011-04-14 05:15:06 +0000340 // Schedule zero-latency TokenFactor below any nodes that may increase the
341 // schedule height. Otherwise, ancestors of the TokenFactor may appear to
342 // have false stalls.
343 if (NI->getOpcode() == ISD::TokenFactor)
344 NodeSUnit->isScheduleLow = true;
345
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000346 // If there are glue operands involved, N is now the bottom-most node
347 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000348 // Update the SUnit.
349 NodeSUnit->setNode(N);
350 assert(N->getNodeId() == -1 && "Node already inserted!");
351 N->setNodeId(NodeSUnit->NodeNum);
352
Andrew Trick92e94662011-02-04 03:18:17 +0000353 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
354 InitNumRegDefsLeft(NodeSUnit);
355
Dan Gohman787782f2008-11-21 01:44:51 +0000356 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000357 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000358 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000359}
360
361void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000362 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
363
David Goodwindc4bdcd2009-08-19 16:08:58 +0000364 // Check to see if the scheduler cares about latencies.
365 bool UnitLatencies = ForceUnitLatencies();
366
Dan Gohman343f0c02008-11-19 23:18:57 +0000367 // Pass 2: add the preds, succs, etc.
368 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
369 SUnit *SU = &SUnits[su];
370 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000371
Dan Gohman343f0c02008-11-19 23:18:57 +0000372 if (MainNode->isMachineOpcode()) {
373 unsigned Opc = MainNode->getMachineOpcode();
374 const TargetInstrDesc &TID = TII->get(Opc);
375 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
376 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
377 SU->isTwoAddress = true;
378 break;
379 }
380 }
381 if (TID.isCommutable())
382 SU->isCommutable = true;
383 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000384
Dan Gohman343f0c02008-11-19 23:18:57 +0000385 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000386 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000387 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000388 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
389 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000390 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000391 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
392 --NumUsed; // Skip over unused values at the end.
393 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000394 SU->hasPhysRegDefs = true;
395 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000396
Dan Gohman343f0c02008-11-19 23:18:57 +0000397 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
398 SDNode *OpN = N->getOperand(i).getNode();
399 if (isPassiveNode(OpN)) continue; // Not scheduled.
400 SUnit *OpSU = &SUnits[OpN->getNodeId()];
401 assert(OpSU && "Node has no SUnit!");
402 if (OpSU == SU) continue; // In the same group.
403
Owen Andersone50ed302009-08-10 22:56:29 +0000404 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000405 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000407
408 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000409 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000410 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000411 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000412 assert((PhysReg == 0 || !isChain) &&
413 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000414 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
415 // emits a copy from the physical register to a virtual register unless
416 // it requires a cross class copy (cost < 0). That means we are only
417 // treating "expensive to copy" register dependency as physical register
418 // dependency. This may change in the future though.
419 if (Cost >= 0)
420 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000421
Evan Cheng046fa3f2010-05-28 23:26:21 +0000422 // If this is a ctrl dep, latency is 1.
Andrew Trickc558bf32011-04-12 20:14:07 +0000423 unsigned OpLatency = isChain ? 1 : OpSU->Latency;
Andrew Trick87896d92011-04-13 00:38:32 +0000424 // Special-case TokenFactor chains as zero-latency.
425 if(isChain && OpN->getOpcode() == ISD::TokenFactor)
426 OpLatency = 0;
427
Evan Cheng046fa3f2010-05-28 23:26:21 +0000428 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
429 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000430 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000431 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000432 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000433 }
David Goodwin71046162009-08-13 16:05:04 +0000434
Andrew Trick4bbf4672011-03-09 19:12:43 +0000435 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000436 // Multiple register uses are combined in the same SUnit. For example,
437 // we could have a set of glued nodes with all their defs consumed by
438 // another set of glued nodes. Register pressure tracking sees this as
439 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000440 //
441 // We can't tell (without more book-keeping) if this results from
442 // glued nodes or duplicate operands. As long as we don't reduce
443 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000444 --OpSU->NumRegDefsLeft;
445 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000446 }
447 }
448 }
449}
450
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000451/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
452/// are input. This SUnit graph is similar to the SelectionDAG, but
453/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000454/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000455void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000456 // Cluster certain nodes which should be scheduled together.
457 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000458 // Populate the SUnits array.
459 BuildSchedUnits();
460 // Compute all the scheduling dependencies between nodes.
461 AddSchedEdges();
462}
463
Andrew Trick92e94662011-02-04 03:18:17 +0000464// Initialize NumNodeDefs for the current Node's opcode.
465void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000466 // Check for phys reg copy.
467 if (!Node)
468 return;
469
Andrew Trick92e94662011-02-04 03:18:17 +0000470 if (!Node->isMachineOpcode()) {
471 if (Node->getOpcode() == ISD::CopyFromReg)
472 NodeNumDefs = 1;
473 else
474 NodeNumDefs = 0;
475 return;
476 }
477 unsigned POpc = Node->getMachineOpcode();
478 if (POpc == TargetOpcode::IMPLICIT_DEF) {
479 // No register need be allocated for this.
480 NodeNumDefs = 0;
481 return;
482 }
483 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
484 // Some instructions define regs that are not represented in the selection DAG
485 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
486 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
487 DefIdx = 0;
488}
489
490// Construct a RegDefIter for this SUnit and find the first valid value.
491ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
492 const ScheduleDAGSDNodes *SD)
493 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
494 InitNodeNumDefs();
495 Advance();
496}
497
498// Advance to the next valid value defined by the SUnit.
499void ScheduleDAGSDNodes::RegDefIter::Advance() {
500 for (;Node;) { // Visit all glued nodes.
501 for (;DefIdx < NodeNumDefs; ++DefIdx) {
502 if (!Node->hasAnyUseOfValue(DefIdx))
503 continue;
504 if (Node->isMachineOpcode() &&
505 Node->getMachineOpcode() == TargetOpcode::EXTRACT_SUBREG) {
506 // Propagate the incoming (full-register) type. I doubt it's needed.
507 ValueType = Node->getOperand(0).getValueType();
508 }
509 else {
510 ValueType = Node->getValueType(DefIdx);
511 }
512 ++DefIdx;
513 return; // Found a normal regdef.
514 }
515 Node = Node->getGluedNode();
516 if (Node == NULL) {
517 return; // No values left to visit.
518 }
519 InitNodeNumDefs();
520 }
521}
522
523void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
524 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
525 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
526 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
527 ++SU->NumRegDefsLeft;
528 }
529}
530
Dan Gohman343f0c02008-11-19 23:18:57 +0000531void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Andrew Trick87896d92011-04-13 00:38:32 +0000532 SDNode *N = SU->getNode();
533
534 // TokenFactor operands are considered zero latency, and some schedulers
535 // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
536 // whenever node latency is nonzero.
537 if (N && N->getOpcode() == ISD::TokenFactor) {
538 SU->Latency = 0;
539 return;
540 }
541
Evan Chenge1631682010-05-19 22:42:23 +0000542 // Check to see if the scheduler cares about latencies.
543 if (ForceUnitLatencies()) {
544 SU->Latency = 1;
545 return;
546 }
547
Evan Cheng3ef1c872010-09-10 01:29:16 +0000548 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000549 if (N && N->isMachineOpcode() &&
550 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000551 SU->Latency = HighLatencyCycles;
552 else
553 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000554 return;
555 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000556
Dan Gohman343f0c02008-11-19 23:18:57 +0000557 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000558 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000559 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000560 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000561 if (N->isMachineOpcode())
562 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000563}
564
Evan Cheng15a16de2010-05-20 06:13:19 +0000565void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
566 unsigned OpIdx, SDep& dep) const{
567 // Check to see if the scheduler cares about latencies.
568 if (ForceUnitLatencies())
569 return;
570
Evan Cheng15a16de2010-05-20 06:13:19 +0000571 if (dep.getKind() != SDep::Data)
572 return;
573
574 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000575 if (Use->isMachineOpcode())
576 // Adjust the use operand index by num of defs.
577 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000578 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000579 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
580 !BB->succ_empty()) {
581 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
582 if (TargetRegisterInfo::isVirtualRegister(Reg))
583 // This copy is a liveout value. It is likely coalesced, so reduce the
584 // latency so not to penalize the def.
585 // FIXME: need target specific adjustment here?
586 Latency = (Latency > 1) ? Latency - 1 : 1;
587 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000588 if (Latency >= 0)
589 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000590}
591
Dan Gohman343f0c02008-11-19 23:18:57 +0000592void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000593 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000594 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000595 return;
596 }
597
598 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000599 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000600 SmallVector<SDNode *, 4> GluedNodes;
601 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
602 GluedNodes.push_back(N);
603 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000604 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000605 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000606 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000607 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000608 }
609}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000610
Evan Chengbfcb3052010-03-25 01:38:16 +0000611namespace {
612 struct OrderSorter {
613 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
614 const std::pair<unsigned, MachineInstr*> &B) {
615 return A.first < B.first;
616 }
617 };
618}
619
Devang Patel55d20e82011-01-26 18:20:04 +0000620/// ProcessSDDbgValues - Process SDDbgValues assoicated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000621static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000622 InstrEmitter &Emitter,
623 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
624 DenseMap<SDValue, unsigned> &VRBaseMap,
625 unsigned Order) {
626 if (!N->getHasDebugValue())
627 return;
628
629 // Opportunistically insert immediate dbg_value uses, i.e. those with source
630 // order number right after the N.
631 MachineBasicBlock *BB = Emitter.getBlock();
632 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
633 SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N);
634 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
635 if (DVs[i]->isInvalidated())
636 continue;
637 unsigned DVOrder = DVs[i]->getOrder();
638 if (!Order || DVOrder == ++Order) {
639 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
640 if (DbgMI) {
641 Orders.push_back(std::make_pair(DVOrder, DbgMI));
642 BB->insert(InsertPos, DbgMI);
643 }
644 DVs[i]->setIsInvalidated();
645 }
646 }
647}
648
Evan Chengbfcb3052010-03-25 01:38:16 +0000649// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000650// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000651// instructions in the right order.
652static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
653 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000654 DenseMap<SDValue, unsigned> &VRBaseMap,
655 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
656 SmallSet<unsigned, 8> &Seen) {
657 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000658 if (!Order || !Seen.insert(Order)) {
659 // Process any valid SDDbgValues even if node does not have any order
660 // assigned.
661 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000662 return;
Devang Patel39078a82011-01-27 00:13:27 +0000663 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000664
665 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000666 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000667 // Did not insert any instruction.
668 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
669 return;
670 }
671
Dan Gohman84023e02010-07-10 09:00:22 +0000672 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000673 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000674}
675
676
Dan Gohmanbcea8592009-10-10 01:32:21 +0000677/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000678MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000679 InstrEmitter Emitter(BB, InsertPos);
680 DenseMap<SDValue, unsigned> VRBaseMap;
681 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000682 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
683 SmallSet<unsigned, 8> Seen;
684 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000685
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000686 // If this is the first BB, emit byval parameter dbg_value's.
687 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
688 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
689 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
690 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000691 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000692 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000693 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000694 }
695 }
696
Dan Gohmanbcea8592009-10-10 01:32:21 +0000697 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
698 SUnit *SU = Sequence[i];
699 if (!SU) {
700 // Null SUnit* is a noop.
701 EmitNoop();
702 continue;
703 }
704
705 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000706 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000707 if (!SU->getNode()) {
708 // Emit a copy.
709 EmitPhysRegCopy(SU, CopyVRBaseMap);
710 continue;
711 }
712
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000713 SmallVector<SDNode *, 4> GluedNodes;
714 for (SDNode *N = SU->getNode()->getGluedNode(); N;
715 N = N->getGluedNode())
716 GluedNodes.push_back(N);
717 while (!GluedNodes.empty()) {
718 SDNode *N = GluedNodes.back();
719 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000720 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000721 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000722 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000723 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000724 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000725 }
726 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000727 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000728 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000729 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000730 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000731 Seen);
732 }
733
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000734 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000735 // order sequence.
736 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000737 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000738
739 // Sort the source order instructions and use the order to insert debug
740 // values.
741 std::sort(Orders.begin(), Orders.end(), OrderSorter());
742
743 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
744 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
745 // Now emit the rest according to source order.
746 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000747 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
748 unsigned Order = Orders[i].first;
749 MachineInstr *MI = Orders[i].second;
750 // Insert all SDDbgValue's whose order(s) are before "Order".
751 if (!MI)
752 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000753 for (; DI != DE &&
754 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
755 if ((*DI)->isInvalidated())
756 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000757 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000758 if (DbgMI) {
759 if (!LastOrder)
760 // Insert to start of the BB (after PHIs).
761 BB->insert(BBBegin, DbgMI);
762 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000763 // Insert at the instruction, which may be in a different
764 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000765 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000766 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000767 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000768 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000769 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000770 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000771 }
772 // Add trailing DbgValue's before the terminator. FIXME: May want to add
773 // some of them before one or more conditional branches?
774 while (DI != DE) {
775 MachineBasicBlock *InsertBB = Emitter.getBlock();
776 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
777 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000778 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000779 if (DbgMI)
780 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000781 }
782 ++DI;
783 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000784 }
785
786 BB = Emitter.getBlock();
787 InsertPos = Emitter.getInsertPos();
788 return BB;
789}