blob: fe853c349f9fa440608dd87b72b80924a50c1212 [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;
Evan Cheng1cc39842010-05-20 23:26:43 +000090 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000091 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000092 return SU;
93}
94
95/// CheckForPhysRegDependency - Check if the dependency between def and use of
96/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000097/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000098static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +000099 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +0000100 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000101 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000102 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
103 return;
104
105 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
106 if (TargetRegisterInfo::isVirtualRegister(Reg))
107 return;
108
109 unsigned ResNo = User->getOperand(2).getResNo();
110 if (Def->isMachineOpcode()) {
111 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
112 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000113 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000114 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000115 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000116 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000117 Cost = RC->getCopyCost();
118 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000119 }
120}
121
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000122static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000123 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000124 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000125
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000126 // Don't add glue from a node to itself.
127 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000128
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000129 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000130 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000131
132 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
133 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000134
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000135 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000136 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000137
Evan Chengc589e032010-01-22 03:36:51 +0000138 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000139 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
140 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000141
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000142 if (GlueDestNode)
143 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000144
Evan Chengc589e032010-01-22 03:36:51 +0000145 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000146 MachineSDNode::mmo_iterator Begin = 0, End = 0;
147 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
148
149 // Store memory references.
150 if (MN) {
151 Begin = MN->memoperands_begin();
152 End = MN->memoperands_end();
153 }
154
Evan Chengc589e032010-01-22 03:36:51 +0000155 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000156
157 // Reset the memory references
158 if (MN)
159 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000160}
161
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000162/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000163/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000164/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000165/// outputs to ensure they are scheduled together and in order. This
166/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000167void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
168 SDNode *Chain = 0;
169 unsigned NumOps = Node->getNumOperands();
170 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
171 Chain = Node->getOperand(NumOps-1).getNode();
172 if (!Chain)
173 return;
174
175 // Look for other loads of the same chain. Find loads that are loading from
176 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000177 SmallPtrSet<SDNode*, 16> Visited;
178 SmallVector<int64_t, 4> Offsets;
179 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000180 bool Cluster = false;
181 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000182 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
183 I != E; ++I) {
184 SDNode *User = *I;
185 if (User == Node || !Visited.insert(User))
186 continue;
187 int64_t Offset1, Offset2;
188 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
189 Offset1 == Offset2)
190 // FIXME: Should be ok if they addresses are identical. But earlier
191 // optimizations really should have eliminated one of the loads.
192 continue;
193 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
194 Offsets.push_back(Offset1);
195 O2SMap.insert(std::make_pair(Offset2, User));
196 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000197 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000198 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000199 Cluster = true;
200 }
201
202 if (!Cluster)
203 return;
204
205 // Sort them in increasing order.
206 std::sort(Offsets.begin(), Offsets.end());
207
208 // Check if the loads are close enough.
209 SmallVector<SDNode*, 4> Loads;
210 unsigned NumLoads = 0;
211 int64_t BaseOff = Offsets[0];
212 SDNode *BaseLoad = O2SMap[BaseOff];
213 Loads.push_back(BaseLoad);
214 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
215 int64_t Offset = Offsets[i];
216 SDNode *Load = O2SMap[Offset];
217 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
218 break; // Stop right here. Ignore loads that are further away.
219 Loads.push_back(Load);
220 ++NumLoads;
221 }
222
223 if (NumLoads == 0)
224 return;
225
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000226 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000227 // ensure they are scheduled in order of increasing addresses.
228 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000229 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000230
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000231 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000232 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000233 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000234 SDNode *Load = Loads[I];
235
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000236 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000237
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000238 if (OutGlue)
239 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000240
Evan Cheng302ef832010-06-10 02:09:31 +0000241 ++LoadsClustered;
242 }
243}
244
245/// ClusterNodes - Cluster certain nodes which should be scheduled together.
246///
247void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000248 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
249 E = DAG->allnodes_end(); NI != E; ++NI) {
250 SDNode *Node = &*NI;
251 if (!Node || !Node->isMachineOpcode())
252 continue;
253
254 unsigned Opc = Node->getMachineOpcode();
255 const TargetInstrDesc &TID = TII->get(Opc);
Evan Cheng302ef832010-06-10 02:09:31 +0000256 if (TID.mayLoad())
257 // Cluster loads from "near" addresses into combined SUnits.
258 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000259 }
260}
261
Dan Gohman343f0c02008-11-19 23:18:57 +0000262void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000263 // During scheduling, the NodeId field of SDNode is used to map SDNodes
264 // to their associated SUnits by holding SUnits table indices. A value
265 // of -1 means the SDNode does not yet have an associated SUnit.
266 unsigned NumNodes = 0;
267 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
268 E = DAG->allnodes_end(); NI != E; ++NI) {
269 NI->setNodeId(-1);
270 ++NumNodes;
271 }
272
Dan Gohman343f0c02008-11-19 23:18:57 +0000273 // Reserve entries in the vector for each of the SUnits we are creating. This
274 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
275 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000276 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
277 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000278 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000279
Chris Lattner736a6ea2010-02-24 06:11:37 +0000280 // Add all nodes in depth first order.
281 SmallVector<SDNode*, 64> Worklist;
282 SmallPtrSet<SDNode*, 64> Visited;
283 Worklist.push_back(DAG->getRoot().getNode());
284 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000285
Chris Lattner736a6ea2010-02-24 06:11:37 +0000286 while (!Worklist.empty()) {
287 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000288
Chris Lattner736a6ea2010-02-24 06:11:37 +0000289 // Add all operands to the worklist unless they've already been added.
290 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
291 if (Visited.insert(NI->getOperand(i).getNode()))
292 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000293
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
295 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000296
Dan Gohman343f0c02008-11-19 23:18:57 +0000297 // If this node has already been processed, stop now.
298 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000299
Dan Gohman343f0c02008-11-19 23:18:57 +0000300 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000301
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000302 // See if anything is glued to this node, if so, add them to glued
303 // nodes. Nodes can have at most one glue input and one glue output. Glue
304 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000305
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000306 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000308 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000309 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000310 N = N->getOperand(N->getNumOperands()-1).getNode();
311 assert(N->getNodeId() == -1 && "Node already inserted!");
312 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000313 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
314 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000315 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000316
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000317 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000318 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000319 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000320 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000321
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000322 // There are either zero or one users of the Glue result.
323 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000324 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000325 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000326 if (GlueVal.isOperandOf(*UI)) {
327 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 assert(N->getNodeId() == -1 && "Node already inserted!");
329 N->setNodeId(NodeSUnit->NodeNum);
330 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000331 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
332 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000333 break;
334 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000335 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000336 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000337
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000338 // If there are glue operands involved, N is now the bottom-most node
339 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000340 // Update the SUnit.
341 NodeSUnit->setNode(N);
342 assert(N->getNodeId() == -1 && "Node already inserted!");
343 N->setNodeId(NodeSUnit->NodeNum);
344
Andrew Trick92e94662011-02-04 03:18:17 +0000345 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
346 InitNumRegDefsLeft(NodeSUnit);
347
Dan Gohman787782f2008-11-21 01:44:51 +0000348 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000349 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000350 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000351}
352
353void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000354 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
355
David Goodwindc4bdcd2009-08-19 16:08:58 +0000356 // Check to see if the scheduler cares about latencies.
357 bool UnitLatencies = ForceUnitLatencies();
358
Dan Gohman343f0c02008-11-19 23:18:57 +0000359 // Pass 2: add the preds, succs, etc.
360 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
361 SUnit *SU = &SUnits[su];
362 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000363
Dan Gohman343f0c02008-11-19 23:18:57 +0000364 if (MainNode->isMachineOpcode()) {
365 unsigned Opc = MainNode->getMachineOpcode();
366 const TargetInstrDesc &TID = TII->get(Opc);
367 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
368 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
369 SU->isTwoAddress = true;
370 break;
371 }
372 }
373 if (TID.isCommutable())
374 SU->isCommutable = true;
375 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000376
Dan Gohman343f0c02008-11-19 23:18:57 +0000377 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000378 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000379 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000380 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
381 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000382 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000383 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
384 --NumUsed; // Skip over unused values at the end.
385 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000386 SU->hasPhysRegDefs = true;
387 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000388
Dan Gohman343f0c02008-11-19 23:18:57 +0000389 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
390 SDNode *OpN = N->getOperand(i).getNode();
391 if (isPassiveNode(OpN)) continue; // Not scheduled.
392 SUnit *OpSU = &SUnits[OpN->getNodeId()];
393 assert(OpSU && "Node has no SUnit!");
394 if (OpSU == SU) continue; // In the same group.
395
Owen Andersone50ed302009-08-10 22:56:29 +0000396 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000397 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000399
400 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000401 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000402 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000403 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000404 assert((PhysReg == 0 || !isChain) &&
405 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000406 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
407 // emits a copy from the physical register to a virtual register unless
408 // it requires a cross class copy (cost < 0). That means we are only
409 // treating "expensive to copy" register dependency as physical register
410 // dependency. This may change in the future though.
411 if (Cost >= 0)
412 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000413
Evan Cheng046fa3f2010-05-28 23:26:21 +0000414 // If this is a ctrl dep, latency is 1.
Andrew Trick3eb43192011-04-12 19:54:36 +0000415 // Special-case TokenFactor chains as zero-latency.
416 unsigned OpLatency = 1;
417 if (!isChain && OpSU->Latency > 0)
418 OpLatency = OpSU->Latency;
419 else if(isChain && OpN->getOpcode() == ISD::TokenFactor)
420 OpLatency = 0;
421
Evan Cheng046fa3f2010-05-28 23:26:21 +0000422 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
423 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000424 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000425 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000426 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000427 }
David Goodwin71046162009-08-13 16:05:04 +0000428
Andrew Trick4bbf4672011-03-09 19:12:43 +0000429 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000430 // Multiple register uses are combined in the same SUnit. For example,
431 // we could have a set of glued nodes with all their defs consumed by
432 // another set of glued nodes. Register pressure tracking sees this as
433 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000434 //
435 // We can't tell (without more book-keeping) if this results from
436 // glued nodes or duplicate operands. As long as we don't reduce
437 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000438 --OpSU->NumRegDefsLeft;
439 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000440 }
441 }
442 }
443}
444
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000445/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
446/// are input. This SUnit graph is similar to the SelectionDAG, but
447/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000448/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000449void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000450 // Cluster certain nodes which should be scheduled together.
451 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000452 // Populate the SUnits array.
453 BuildSchedUnits();
454 // Compute all the scheduling dependencies between nodes.
455 AddSchedEdges();
456}
457
Andrew Trick92e94662011-02-04 03:18:17 +0000458// Initialize NumNodeDefs for the current Node's opcode.
459void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000460 // Check for phys reg copy.
461 if (!Node)
462 return;
463
Andrew Trick92e94662011-02-04 03:18:17 +0000464 if (!Node->isMachineOpcode()) {
465 if (Node->getOpcode() == ISD::CopyFromReg)
466 NodeNumDefs = 1;
467 else
468 NodeNumDefs = 0;
469 return;
470 }
471 unsigned POpc = Node->getMachineOpcode();
472 if (POpc == TargetOpcode::IMPLICIT_DEF) {
473 // No register need be allocated for this.
474 NodeNumDefs = 0;
475 return;
476 }
477 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
478 // Some instructions define regs that are not represented in the selection DAG
479 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
480 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
481 DefIdx = 0;
482}
483
484// Construct a RegDefIter for this SUnit and find the first valid value.
485ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
486 const ScheduleDAGSDNodes *SD)
487 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
488 InitNodeNumDefs();
489 Advance();
490}
491
492// Advance to the next valid value defined by the SUnit.
493void ScheduleDAGSDNodes::RegDefIter::Advance() {
494 for (;Node;) { // Visit all glued nodes.
495 for (;DefIdx < NodeNumDefs; ++DefIdx) {
496 if (!Node->hasAnyUseOfValue(DefIdx))
497 continue;
498 if (Node->isMachineOpcode() &&
499 Node->getMachineOpcode() == TargetOpcode::EXTRACT_SUBREG) {
500 // Propagate the incoming (full-register) type. I doubt it's needed.
501 ValueType = Node->getOperand(0).getValueType();
502 }
503 else {
504 ValueType = Node->getValueType(DefIdx);
505 }
506 ++DefIdx;
507 return; // Found a normal regdef.
508 }
509 Node = Node->getGluedNode();
510 if (Node == NULL) {
511 return; // No values left to visit.
512 }
513 InitNodeNumDefs();
514 }
515}
516
517void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
518 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
519 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
520 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
521 ++SU->NumRegDefsLeft;
522 }
523}
524
Dan Gohman343f0c02008-11-19 23:18:57 +0000525void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Evan Chenge1631682010-05-19 22:42:23 +0000526 // Check to see if the scheduler cares about latencies.
527 if (ForceUnitLatencies()) {
528 SU->Latency = 1;
529 return;
530 }
531
Evan Cheng3ef1c872010-09-10 01:29:16 +0000532 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000533 SDNode *N = SU->getNode();
534 if (N && N->isMachineOpcode() &&
535 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000536 SU->Latency = HighLatencyCycles;
537 else
538 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000539 return;
540 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000541
Dan Gohman343f0c02008-11-19 23:18:57 +0000542 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000543 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000544 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000545 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000546 if (N->isMachineOpcode())
547 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000548}
549
Evan Cheng15a16de2010-05-20 06:13:19 +0000550void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
551 unsigned OpIdx, SDep& dep) const{
552 // Check to see if the scheduler cares about latencies.
553 if (ForceUnitLatencies())
554 return;
555
Evan Cheng15a16de2010-05-20 06:13:19 +0000556 if (dep.getKind() != SDep::Data)
557 return;
558
559 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000560 if (Use->isMachineOpcode())
561 // Adjust the use operand index by num of defs.
562 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000563 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000564 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
565 !BB->succ_empty()) {
566 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
567 if (TargetRegisterInfo::isVirtualRegister(Reg))
568 // This copy is a liveout value. It is likely coalesced, so reduce the
569 // latency so not to penalize the def.
570 // FIXME: need target specific adjustment here?
571 Latency = (Latency > 1) ? Latency - 1 : 1;
572 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000573 if (Latency >= 0)
574 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000575}
576
Dan Gohman343f0c02008-11-19 23:18:57 +0000577void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000578 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000579 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000580 return;
581 }
582
583 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000584 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000585 SmallVector<SDNode *, 4> GluedNodes;
586 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
587 GluedNodes.push_back(N);
588 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000589 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000590 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000591 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000592 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000593 }
594}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000595
Evan Chengbfcb3052010-03-25 01:38:16 +0000596namespace {
597 struct OrderSorter {
598 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
599 const std::pair<unsigned, MachineInstr*> &B) {
600 return A.first < B.first;
601 }
602 };
603}
604
Devang Patel55d20e82011-01-26 18:20:04 +0000605/// ProcessSDDbgValues - Process SDDbgValues assoicated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000606static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000607 InstrEmitter &Emitter,
608 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
609 DenseMap<SDValue, unsigned> &VRBaseMap,
610 unsigned Order) {
611 if (!N->getHasDebugValue())
612 return;
613
614 // Opportunistically insert immediate dbg_value uses, i.e. those with source
615 // order number right after the N.
616 MachineBasicBlock *BB = Emitter.getBlock();
617 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
618 SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N);
619 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
620 if (DVs[i]->isInvalidated())
621 continue;
622 unsigned DVOrder = DVs[i]->getOrder();
623 if (!Order || DVOrder == ++Order) {
624 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
625 if (DbgMI) {
626 Orders.push_back(std::make_pair(DVOrder, DbgMI));
627 BB->insert(InsertPos, DbgMI);
628 }
629 DVs[i]->setIsInvalidated();
630 }
631 }
632}
633
Evan Chengbfcb3052010-03-25 01:38:16 +0000634// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000635// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000636// instructions in the right order.
637static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
638 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000639 DenseMap<SDValue, unsigned> &VRBaseMap,
640 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
641 SmallSet<unsigned, 8> &Seen) {
642 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000643 if (!Order || !Seen.insert(Order)) {
644 // Process any valid SDDbgValues even if node does not have any order
645 // assigned.
646 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000647 return;
Devang Patel39078a82011-01-27 00:13:27 +0000648 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000649
650 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000651 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000652 // Did not insert any instruction.
653 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
654 return;
655 }
656
Dan Gohman84023e02010-07-10 09:00:22 +0000657 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000658 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000659}
660
661
Dan Gohmanbcea8592009-10-10 01:32:21 +0000662/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000663MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000664 InstrEmitter Emitter(BB, InsertPos);
665 DenseMap<SDValue, unsigned> VRBaseMap;
666 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000667 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
668 SmallSet<unsigned, 8> Seen;
669 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000670
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000671 // If this is the first BB, emit byval parameter dbg_value's.
672 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
673 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
674 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
675 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000676 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000677 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000678 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000679 }
680 }
681
Dan Gohmanbcea8592009-10-10 01:32:21 +0000682 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
683 SUnit *SU = Sequence[i];
684 if (!SU) {
685 // Null SUnit* is a noop.
686 EmitNoop();
687 continue;
688 }
689
690 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000691 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000692 if (!SU->getNode()) {
693 // Emit a copy.
694 EmitPhysRegCopy(SU, CopyVRBaseMap);
695 continue;
696 }
697
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000698 SmallVector<SDNode *, 4> GluedNodes;
699 for (SDNode *N = SU->getNode()->getGluedNode(); N;
700 N = N->getGluedNode())
701 GluedNodes.push_back(N);
702 while (!GluedNodes.empty()) {
703 SDNode *N = GluedNodes.back();
704 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000705 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000706 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000707 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000708 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000709 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000710 }
711 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000712 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000713 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000714 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000715 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000716 Seen);
717 }
718
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000719 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000720 // order sequence.
721 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000722 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000723
724 // Sort the source order instructions and use the order to insert debug
725 // values.
726 std::sort(Orders.begin(), Orders.end(), OrderSorter());
727
728 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
729 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
730 // Now emit the rest according to source order.
731 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000732 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
733 unsigned Order = Orders[i].first;
734 MachineInstr *MI = Orders[i].second;
735 // Insert all SDDbgValue's whose order(s) are before "Order".
736 if (!MI)
737 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000738 for (; DI != DE &&
739 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
740 if ((*DI)->isInvalidated())
741 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000742 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000743 if (DbgMI) {
744 if (!LastOrder)
745 // Insert to start of the BB (after PHIs).
746 BB->insert(BBBegin, DbgMI);
747 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000748 // Insert at the instruction, which may be in a different
749 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000750 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000751 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000752 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000753 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000754 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000755 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000756 }
757 // Add trailing DbgValue's before the terminator. FIXME: May want to add
758 // some of them before one or more conditional branches?
759 while (DI != DE) {
760 MachineBasicBlock *InsertBB = Emitter.getBlock();
761 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
762 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000763 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000764 if (DbgMI)
765 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000766 }
767 ++DI;
768 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000769 }
770
771 BB = Emitter.getBlock();
772 InsertPos = Emitter.getInsertPos();
773 return BB;
774}