blob: ca70adafc08b4f6580f0608836fb7e667eac9b6d [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;
Evan Cheng554daa62011-04-26 21:31:35 +000086 SU->isCallOp = Old->isCallOp;
Dan Gohman343f0c02008-11-19 23:18:57 +000087 SU->isTwoAddress = Old->isTwoAddress;
88 SU->isCommutable = Old->isCommutable;
89 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000090 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Andrew Trick12f0dc62011-04-14 05:15:06 +000091 SU->isScheduleHigh = Old->isScheduleHigh;
92 SU->isScheduleLow = Old->isScheduleLow;
Evan Cheng1cc39842010-05-20 23:26:43 +000093 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000094 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000095 return SU;
96}
97
98/// CheckForPhysRegDependency - Check if the dependency between def and use of
99/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +0000100/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +0000101static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +0000102 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +0000103 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000104 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000105 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
106 return;
107
108 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
109 if (TargetRegisterInfo::isVirtualRegister(Reg))
110 return;
111
112 unsigned ResNo = User->getOperand(2).getResNo();
113 if (Def->isMachineOpcode()) {
Evan Chenge837dea2011-06-28 19:10:37 +0000114 const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
Dan Gohman343f0c02008-11-19 23:18:57 +0000115 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000116 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000117 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000118 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000119 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000120 Cost = RC->getCopyCost();
121 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000122 }
123}
124
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000125static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000126 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000127 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000128
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000129 // Don't add glue from a node to itself.
130 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000131
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000132 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000133 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000134
135 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
136 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000137
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000138 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000139 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000140
Evan Chengc589e032010-01-22 03:36:51 +0000141 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000142 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
143 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000144
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000145 if (GlueDestNode)
146 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000147
Evan Chengc589e032010-01-22 03:36:51 +0000148 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000149 MachineSDNode::mmo_iterator Begin = 0, End = 0;
150 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
151
152 // Store memory references.
153 if (MN) {
154 Begin = MN->memoperands_begin();
155 End = MN->memoperands_end();
156 }
157
Evan Chengc589e032010-01-22 03:36:51 +0000158 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000159
160 // Reset the memory references
161 if (MN)
162 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000163}
164
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000165/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000166/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000167/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000168/// outputs to ensure they are scheduled together and in order. This
169/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000170void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
171 SDNode *Chain = 0;
172 unsigned NumOps = Node->getNumOperands();
173 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
174 Chain = Node->getOperand(NumOps-1).getNode();
175 if (!Chain)
176 return;
177
178 // Look for other loads of the same chain. Find loads that are loading from
179 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000180 SmallPtrSet<SDNode*, 16> Visited;
181 SmallVector<int64_t, 4> Offsets;
182 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000183 bool Cluster = false;
184 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000185 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
186 I != E; ++I) {
187 SDNode *User = *I;
188 if (User == Node || !Visited.insert(User))
189 continue;
190 int64_t Offset1, Offset2;
191 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
192 Offset1 == Offset2)
193 // FIXME: Should be ok if they addresses are identical. But earlier
194 // optimizations really should have eliminated one of the loads.
195 continue;
196 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
197 Offsets.push_back(Offset1);
198 O2SMap.insert(std::make_pair(Offset2, User));
199 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000200 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000201 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000202 Cluster = true;
203 }
204
205 if (!Cluster)
206 return;
207
208 // Sort them in increasing order.
209 std::sort(Offsets.begin(), Offsets.end());
210
211 // Check if the loads are close enough.
212 SmallVector<SDNode*, 4> Loads;
213 unsigned NumLoads = 0;
214 int64_t BaseOff = Offsets[0];
215 SDNode *BaseLoad = O2SMap[BaseOff];
216 Loads.push_back(BaseLoad);
217 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
218 int64_t Offset = Offsets[i];
219 SDNode *Load = O2SMap[Offset];
220 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
221 break; // Stop right here. Ignore loads that are further away.
222 Loads.push_back(Load);
223 ++NumLoads;
224 }
225
226 if (NumLoads == 0)
227 return;
228
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000229 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000230 // ensure they are scheduled in order of increasing addresses.
231 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000232 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000233
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000234 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000235 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000236 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000237 SDNode *Load = Loads[I];
238
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000239 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000240
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000241 if (OutGlue)
242 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000243
Evan Cheng302ef832010-06-10 02:09:31 +0000244 ++LoadsClustered;
245 }
246}
247
248/// ClusterNodes - Cluster certain nodes which should be scheduled together.
249///
250void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000251 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
252 E = DAG->allnodes_end(); NI != E; ++NI) {
253 SDNode *Node = &*NI;
254 if (!Node || !Node->isMachineOpcode())
255 continue;
256
257 unsigned Opc = Node->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000258 const MCInstrDesc &MCID = TII->get(Opc);
259 if (MCID.mayLoad())
Evan Cheng302ef832010-06-10 02:09:31 +0000260 // Cluster loads from "near" addresses into combined SUnits.
261 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000262 }
263}
264
Dan Gohman343f0c02008-11-19 23:18:57 +0000265void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000266 // During scheduling, the NodeId field of SDNode is used to map SDNodes
267 // to their associated SUnits by holding SUnits table indices. A value
268 // of -1 means the SDNode does not yet have an associated SUnit.
269 unsigned NumNodes = 0;
270 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
271 E = DAG->allnodes_end(); NI != E; ++NI) {
272 NI->setNodeId(-1);
273 ++NumNodes;
274 }
275
Dan Gohman343f0c02008-11-19 23:18:57 +0000276 // Reserve entries in the vector for each of the SUnits we are creating. This
277 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
278 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000279 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
280 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000281 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000282
Chris Lattner736a6ea2010-02-24 06:11:37 +0000283 // Add all nodes in depth first order.
284 SmallVector<SDNode*, 64> Worklist;
285 SmallPtrSet<SDNode*, 64> Visited;
286 Worklist.push_back(DAG->getRoot().getNode());
287 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000288
Evan Cheng554daa62011-04-26 21:31:35 +0000289 SmallVector<SUnit*, 8> CallSUnits;
Chris Lattner736a6ea2010-02-24 06:11:37 +0000290 while (!Worklist.empty()) {
291 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000292
Chris Lattner736a6ea2010-02-24 06:11:37 +0000293 // Add all operands to the worklist unless they've already been added.
294 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
295 if (Visited.insert(NI->getOperand(i).getNode()))
296 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000297
Dan Gohman343f0c02008-11-19 23:18:57 +0000298 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
299 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000300
Dan Gohman343f0c02008-11-19 23:18:57 +0000301 // If this node has already been processed, stop now.
302 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000303
Dan Gohman343f0c02008-11-19 23:18:57 +0000304 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000305
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000306 // See if anything is glued to this node, if so, add them to glued
307 // nodes. Nodes can have at most one glue input and one glue output. Glue
308 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000309
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000310 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000311 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000312 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000313 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000314 N = N->getOperand(N->getNumOperands()-1).getNode();
315 assert(N->getNodeId() == -1 && "Node already inserted!");
316 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000317 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
318 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000319 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000320
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000321 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000322 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000323 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000324 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000325
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000326 // There are either zero or one users of the Glue result.
327 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000328 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000329 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000330 if (GlueVal.isOperandOf(*UI)) {
331 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000332 assert(N->getNodeId() == -1 && "Node already inserted!");
333 N->setNodeId(NodeSUnit->NodeNum);
334 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000335 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
336 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000337 break;
338 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000339 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000340 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000341
Evan Cheng554daa62011-04-26 21:31:35 +0000342 if (NodeSUnit->isCall)
343 CallSUnits.push_back(NodeSUnit);
344
Andrew Trick12f0dc62011-04-14 05:15:06 +0000345 // Schedule zero-latency TokenFactor below any nodes that may increase the
346 // schedule height. Otherwise, ancestors of the TokenFactor may appear to
347 // have false stalls.
348 if (NI->getOpcode() == ISD::TokenFactor)
349 NodeSUnit->isScheduleLow = true;
350
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000351 // If there are glue operands involved, N is now the bottom-most node
352 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000353 // Update the SUnit.
354 NodeSUnit->setNode(N);
355 assert(N->getNodeId() == -1 && "Node already inserted!");
356 N->setNodeId(NodeSUnit->NodeNum);
357
Andrew Trick92e94662011-02-04 03:18:17 +0000358 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
359 InitNumRegDefsLeft(NodeSUnit);
360
Dan Gohman787782f2008-11-21 01:44:51 +0000361 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000362 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000363 }
Evan Cheng554daa62011-04-26 21:31:35 +0000364
365 // Find all call operands.
366 while (!CallSUnits.empty()) {
367 SUnit *SU = CallSUnits.pop_back_val();
368 for (const SDNode *SUNode = SU->getNode(); SUNode;
369 SUNode = SUNode->getGluedNode()) {
370 if (SUNode->getOpcode() != ISD::CopyToReg)
371 continue;
372 SDNode *SrcN = SUNode->getOperand(2).getNode();
373 if (isPassiveNode(SrcN)) continue; // Not scheduled.
374 SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
375 SrcSU->isCallOp = true;
376 }
377 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000378}
379
380void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000381 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
382
David Goodwindc4bdcd2009-08-19 16:08:58 +0000383 // Check to see if the scheduler cares about latencies.
384 bool UnitLatencies = ForceUnitLatencies();
385
Dan Gohman343f0c02008-11-19 23:18:57 +0000386 // Pass 2: add the preds, succs, etc.
387 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
388 SUnit *SU = &SUnits[su];
389 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000390
Dan Gohman343f0c02008-11-19 23:18:57 +0000391 if (MainNode->isMachineOpcode()) {
392 unsigned Opc = MainNode->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000393 const MCInstrDesc &MCID = TII->get(Opc);
394 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
395 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000396 SU->isTwoAddress = true;
397 break;
398 }
399 }
Evan Chenge837dea2011-06-28 19:10:37 +0000400 if (MCID.isCommutable())
Dan Gohman343f0c02008-11-19 23:18:57 +0000401 SU->isCommutable = true;
402 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000403
Dan Gohman343f0c02008-11-19 23:18:57 +0000404 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000405 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000406 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000407 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
408 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000409 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000410 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
411 --NumUsed; // Skip over unused values at the end.
412 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000413 SU->hasPhysRegDefs = true;
414 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000415
Dan Gohman343f0c02008-11-19 23:18:57 +0000416 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
417 SDNode *OpN = N->getOperand(i).getNode();
418 if (isPassiveNode(OpN)) continue; // Not scheduled.
419 SUnit *OpSU = &SUnits[OpN->getNodeId()];
420 assert(OpSU && "Node has no SUnit!");
421 if (OpSU == SU) continue; // In the same group.
422
Owen Andersone50ed302009-08-10 22:56:29 +0000423 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000424 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000426
427 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000428 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000429 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000430 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000431 assert((PhysReg == 0 || !isChain) &&
432 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000433 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
434 // emits a copy from the physical register to a virtual register unless
435 // it requires a cross class copy (cost < 0). That means we are only
436 // treating "expensive to copy" register dependency as physical register
437 // dependency. This may change in the future though.
Andrew Trick4cb971c2011-06-15 17:16:12 +0000438 if (Cost >= 0 && !StressSched)
Evan Chengc29a56d2009-01-12 03:19:55 +0000439 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000440
Evan Cheng046fa3f2010-05-28 23:26:21 +0000441 // If this is a ctrl dep, latency is 1.
Andrew Trickc558bf32011-04-12 20:14:07 +0000442 unsigned OpLatency = isChain ? 1 : OpSU->Latency;
Andrew Trick87896d92011-04-13 00:38:32 +0000443 // Special-case TokenFactor chains as zero-latency.
444 if(isChain && OpN->getOpcode() == ISD::TokenFactor)
445 OpLatency = 0;
446
Evan Cheng046fa3f2010-05-28 23:26:21 +0000447 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
448 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000449 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000450 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000451 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000452 }
David Goodwin71046162009-08-13 16:05:04 +0000453
Andrew Trick4bbf4672011-03-09 19:12:43 +0000454 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000455 // Multiple register uses are combined in the same SUnit. For example,
456 // we could have a set of glued nodes with all their defs consumed by
457 // another set of glued nodes. Register pressure tracking sees this as
458 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000459 //
460 // We can't tell (without more book-keeping) if this results from
461 // glued nodes or duplicate operands. As long as we don't reduce
462 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000463 --OpSU->NumRegDefsLeft;
464 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000465 }
466 }
467 }
468}
469
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000470/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
471/// are input. This SUnit graph is similar to the SelectionDAG, but
472/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000473/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000474void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000475 // Cluster certain nodes which should be scheduled together.
476 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000477 // Populate the SUnits array.
478 BuildSchedUnits();
479 // Compute all the scheduling dependencies between nodes.
480 AddSchedEdges();
481}
482
Andrew Trick92e94662011-02-04 03:18:17 +0000483// Initialize NumNodeDefs for the current Node's opcode.
484void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000485 // Check for phys reg copy.
486 if (!Node)
487 return;
488
Andrew Trick92e94662011-02-04 03:18:17 +0000489 if (!Node->isMachineOpcode()) {
490 if (Node->getOpcode() == ISD::CopyFromReg)
491 NodeNumDefs = 1;
492 else
493 NodeNumDefs = 0;
494 return;
495 }
496 unsigned POpc = Node->getMachineOpcode();
497 if (POpc == TargetOpcode::IMPLICIT_DEF) {
498 // No register need be allocated for this.
499 NodeNumDefs = 0;
500 return;
501 }
502 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
503 // Some instructions define regs that are not represented in the selection DAG
504 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
505 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
506 DefIdx = 0;
507}
508
509// Construct a RegDefIter for this SUnit and find the first valid value.
510ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
511 const ScheduleDAGSDNodes *SD)
512 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
513 InitNodeNumDefs();
514 Advance();
515}
516
517// Advance to the next valid value defined by the SUnit.
518void ScheduleDAGSDNodes::RegDefIter::Advance() {
519 for (;Node;) { // Visit all glued nodes.
520 for (;DefIdx < NodeNumDefs; ++DefIdx) {
521 if (!Node->hasAnyUseOfValue(DefIdx))
522 continue;
Andrew Trick4ef4c172011-06-27 18:01:20 +0000523 ValueType = Node->getValueType(DefIdx);
Andrew Trick92e94662011-02-04 03:18:17 +0000524 ++DefIdx;
525 return; // Found a normal regdef.
526 }
527 Node = Node->getGluedNode();
528 if (Node == NULL) {
529 return; // No values left to visit.
530 }
531 InitNodeNumDefs();
532 }
533}
534
535void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
536 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
537 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
538 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
539 ++SU->NumRegDefsLeft;
540 }
541}
542
Dan Gohman343f0c02008-11-19 23:18:57 +0000543void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Andrew Trick87896d92011-04-13 00:38:32 +0000544 SDNode *N = SU->getNode();
545
546 // TokenFactor operands are considered zero latency, and some schedulers
547 // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
548 // whenever node latency is nonzero.
549 if (N && N->getOpcode() == ISD::TokenFactor) {
550 SU->Latency = 0;
551 return;
552 }
553
Evan Chenge1631682010-05-19 22:42:23 +0000554 // Check to see if the scheduler cares about latencies.
555 if (ForceUnitLatencies()) {
556 SU->Latency = 1;
557 return;
558 }
559
Evan Cheng3ef1c872010-09-10 01:29:16 +0000560 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000561 if (N && N->isMachineOpcode() &&
562 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000563 SU->Latency = HighLatencyCycles;
564 else
565 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000566 return;
567 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000568
Dan Gohman343f0c02008-11-19 23:18:57 +0000569 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000570 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000571 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000572 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000573 if (N->isMachineOpcode())
574 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000575}
576
Evan Cheng15a16de2010-05-20 06:13:19 +0000577void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
578 unsigned OpIdx, SDep& dep) const{
579 // Check to see if the scheduler cares about latencies.
580 if (ForceUnitLatencies())
581 return;
582
Evan Cheng15a16de2010-05-20 06:13:19 +0000583 if (dep.getKind() != SDep::Data)
584 return;
585
586 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000587 if (Use->isMachineOpcode())
588 // Adjust the use operand index by num of defs.
589 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000590 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000591 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
592 !BB->succ_empty()) {
593 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
594 if (TargetRegisterInfo::isVirtualRegister(Reg))
595 // This copy is a liveout value. It is likely coalesced, so reduce the
596 // latency so not to penalize the def.
597 // FIXME: need target specific adjustment here?
598 Latency = (Latency > 1) ? Latency - 1 : 1;
599 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000600 if (Latency >= 0)
601 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000602}
603
Dan Gohman343f0c02008-11-19 23:18:57 +0000604void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000605 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000606 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000607 return;
608 }
609
610 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000611 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000612 SmallVector<SDNode *, 4> GluedNodes;
613 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
614 GluedNodes.push_back(N);
615 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000616 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000617 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000618 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000619 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000620 }
621}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000622
Evan Chengbfcb3052010-03-25 01:38:16 +0000623namespace {
624 struct OrderSorter {
625 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
626 const std::pair<unsigned, MachineInstr*> &B) {
627 return A.first < B.first;
628 }
629 };
630}
631
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000632/// ProcessSDDbgValues - Process SDDbgValues associated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000633static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000634 InstrEmitter &Emitter,
635 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
636 DenseMap<SDValue, unsigned> &VRBaseMap,
637 unsigned Order) {
638 if (!N->getHasDebugValue())
639 return;
640
641 // Opportunistically insert immediate dbg_value uses, i.e. those with source
642 // order number right after the N.
643 MachineBasicBlock *BB = Emitter.getBlock();
644 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000645 ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N);
Devang Patel55d20e82011-01-26 18:20:04 +0000646 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
647 if (DVs[i]->isInvalidated())
648 continue;
649 unsigned DVOrder = DVs[i]->getOrder();
650 if (!Order || DVOrder == ++Order) {
651 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
652 if (DbgMI) {
653 Orders.push_back(std::make_pair(DVOrder, DbgMI));
654 BB->insert(InsertPos, DbgMI);
655 }
656 DVs[i]->setIsInvalidated();
657 }
658 }
659}
660
Evan Chengbfcb3052010-03-25 01:38:16 +0000661// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000662// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000663// instructions in the right order.
664static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
665 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000666 DenseMap<SDValue, unsigned> &VRBaseMap,
667 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
668 SmallSet<unsigned, 8> &Seen) {
669 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000670 if (!Order || !Seen.insert(Order)) {
671 // Process any valid SDDbgValues even if node does not have any order
672 // assigned.
673 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000674 return;
Devang Patel39078a82011-01-27 00:13:27 +0000675 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000676
677 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000678 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000679 // Did not insert any instruction.
680 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
681 return;
682 }
683
Dan Gohman84023e02010-07-10 09:00:22 +0000684 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000685 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000686}
687
688
Dan Gohmanbcea8592009-10-10 01:32:21 +0000689/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000690MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000691 InstrEmitter Emitter(BB, InsertPos);
692 DenseMap<SDValue, unsigned> VRBaseMap;
693 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000694 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
695 SmallSet<unsigned, 8> Seen;
696 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000697
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000698 // If this is the first BB, emit byval parameter dbg_value's.
699 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
700 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
701 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
702 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000703 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000704 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000705 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000706 }
707 }
708
Dan Gohmanbcea8592009-10-10 01:32:21 +0000709 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
710 SUnit *SU = Sequence[i];
711 if (!SU) {
712 // Null SUnit* is a noop.
713 EmitNoop();
714 continue;
715 }
716
717 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000718 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000719 if (!SU->getNode()) {
720 // Emit a copy.
721 EmitPhysRegCopy(SU, CopyVRBaseMap);
722 continue;
723 }
724
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000725 SmallVector<SDNode *, 4> GluedNodes;
726 for (SDNode *N = SU->getNode()->getGluedNode(); N;
727 N = N->getGluedNode())
728 GluedNodes.push_back(N);
729 while (!GluedNodes.empty()) {
730 SDNode *N = GluedNodes.back();
731 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000732 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000733 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000734 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000735 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000736 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000737 }
738 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000739 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000740 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000741 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000742 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000743 Seen);
744 }
745
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000746 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000747 // order sequence.
748 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000749 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000750
751 // Sort the source order instructions and use the order to insert debug
752 // values.
753 std::sort(Orders.begin(), Orders.end(), OrderSorter());
754
755 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
756 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
757 // Now emit the rest according to source order.
758 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000759 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
760 unsigned Order = Orders[i].first;
761 MachineInstr *MI = Orders[i].second;
762 // Insert all SDDbgValue's whose order(s) are before "Order".
763 if (!MI)
764 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000765 for (; DI != DE &&
766 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
767 if ((*DI)->isInvalidated())
768 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000769 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000770 if (DbgMI) {
771 if (!LastOrder)
772 // Insert to start of the BB (after PHIs).
773 BB->insert(BBBegin, DbgMI);
774 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000775 // Insert at the instruction, which may be in a different
776 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000777 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000778 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000779 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000780 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000781 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000782 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000783 }
784 // Add trailing DbgValue's before the terminator. FIXME: May want to add
785 // some of them before one or more conditional branches?
786 while (DI != DE) {
787 MachineBasicBlock *InsertBB = Emitter.getBlock();
788 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
789 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000790 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000791 if (DbgMI)
792 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000793 }
794 ++DI;
795 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000796 }
797
798 BB = Emitter.getBlock();
799 InsertPos = Emitter.getInsertPos();
800 return BB;
801}