blob: 202f7ab4e14f3e72054ab100fb92bc4f4f255c98 [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;
Evan Cheng8239daf2010-11-03 00:45:17 +000084 SU->isCall = Old->isCall;
Dan Gohman343f0c02008-11-19 23:18:57 +000085 SU->isTwoAddress = Old->isTwoAddress;
86 SU->isCommutable = Old->isCommutable;
87 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000088 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Evan Cheng1cc39842010-05-20 23:26:43 +000089 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000090 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000091 return SU;
92}
93
94/// CheckForPhysRegDependency - Check if the dependency between def and use of
95/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +000096/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +000097static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +000098 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +000099 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000100 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000101 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
102 return;
103
104 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
105 if (TargetRegisterInfo::isVirtualRegister(Reg))
106 return;
107
108 unsigned ResNo = User->getOperand(2).getResNo();
109 if (Def->isMachineOpcode()) {
110 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
111 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000112 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000113 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000114 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000115 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000116 Cost = RC->getCopyCost();
117 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000118 }
119}
120
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000121static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000122 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000123 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000124
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000125 // Don't add glue from a node to itself.
126 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000127
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000128 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000129 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000130
131 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
132 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000133
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000134 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000135 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000136
Evan Chengc589e032010-01-22 03:36:51 +0000137 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000138 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
139 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000140
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000141 if (GlueDestNode)
142 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000143
Evan Chengc589e032010-01-22 03:36:51 +0000144 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000145 MachineSDNode::mmo_iterator Begin = 0, End = 0;
146 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
147
148 // Store memory references.
149 if (MN) {
150 Begin = MN->memoperands_begin();
151 End = MN->memoperands_end();
152 }
153
Evan Chengc589e032010-01-22 03:36:51 +0000154 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000155
156 // Reset the memory references
157 if (MN)
158 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000159}
160
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000161/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000162/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000163/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000164/// outputs to ensure they are scheduled together and in order. This
165/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000166void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
167 SDNode *Chain = 0;
168 unsigned NumOps = Node->getNumOperands();
169 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
170 Chain = Node->getOperand(NumOps-1).getNode();
171 if (!Chain)
172 return;
173
174 // Look for other loads of the same chain. Find loads that are loading from
175 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000176 SmallPtrSet<SDNode*, 16> Visited;
177 SmallVector<int64_t, 4> Offsets;
178 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000179 bool Cluster = false;
180 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000181 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
182 I != E; ++I) {
183 SDNode *User = *I;
184 if (User == Node || !Visited.insert(User))
185 continue;
186 int64_t Offset1, Offset2;
187 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
188 Offset1 == Offset2)
189 // FIXME: Should be ok if they addresses are identical. But earlier
190 // optimizations really should have eliminated one of the loads.
191 continue;
192 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
193 Offsets.push_back(Offset1);
194 O2SMap.insert(std::make_pair(Offset2, User));
195 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000196 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000197 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000198 Cluster = true;
199 }
200
201 if (!Cluster)
202 return;
203
204 // Sort them in increasing order.
205 std::sort(Offsets.begin(), Offsets.end());
206
207 // Check if the loads are close enough.
208 SmallVector<SDNode*, 4> Loads;
209 unsigned NumLoads = 0;
210 int64_t BaseOff = Offsets[0];
211 SDNode *BaseLoad = O2SMap[BaseOff];
212 Loads.push_back(BaseLoad);
213 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
214 int64_t Offset = Offsets[i];
215 SDNode *Load = O2SMap[Offset];
216 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
217 break; // Stop right here. Ignore loads that are further away.
218 Loads.push_back(Load);
219 ++NumLoads;
220 }
221
222 if (NumLoads == 0)
223 return;
224
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000225 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000226 // ensure they are scheduled in order of increasing addresses.
227 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000228 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000229
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000230 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000231 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000232 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000233 SDNode *Load = Loads[I];
234
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000235 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000236
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000237 if (OutGlue)
238 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000239
Evan Cheng302ef832010-06-10 02:09:31 +0000240 ++LoadsClustered;
241 }
242}
243
244/// ClusterNodes - Cluster certain nodes which should be scheduled together.
245///
246void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000247 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
248 E = DAG->allnodes_end(); NI != E; ++NI) {
249 SDNode *Node = &*NI;
250 if (!Node || !Node->isMachineOpcode())
251 continue;
252
253 unsigned Opc = Node->getMachineOpcode();
254 const TargetInstrDesc &TID = TII->get(Opc);
Evan Cheng302ef832010-06-10 02:09:31 +0000255 if (TID.mayLoad())
256 // Cluster loads from "near" addresses into combined SUnits.
257 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000258 }
259}
260
Dan Gohman343f0c02008-11-19 23:18:57 +0000261void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000262 // During scheduling, the NodeId field of SDNode is used to map SDNodes
263 // to their associated SUnits by holding SUnits table indices. A value
264 // of -1 means the SDNode does not yet have an associated SUnit.
265 unsigned NumNodes = 0;
266 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
267 E = DAG->allnodes_end(); NI != E; ++NI) {
268 NI->setNodeId(-1);
269 ++NumNodes;
270 }
271
Dan Gohman343f0c02008-11-19 23:18:57 +0000272 // Reserve entries in the vector for each of the SUnits we are creating. This
273 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
274 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000275 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
276 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000277 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000278
Chris Lattner736a6ea2010-02-24 06:11:37 +0000279 // Add all nodes in depth first order.
280 SmallVector<SDNode*, 64> Worklist;
281 SmallPtrSet<SDNode*, 64> Visited;
282 Worklist.push_back(DAG->getRoot().getNode());
283 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000284
Chris Lattner736a6ea2010-02-24 06:11:37 +0000285 while (!Worklist.empty()) {
286 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000287
Chris Lattner736a6ea2010-02-24 06:11:37 +0000288 // Add all operands to the worklist unless they've already been added.
289 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
290 if (Visited.insert(NI->getOperand(i).getNode()))
291 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000292
Dan Gohman343f0c02008-11-19 23:18:57 +0000293 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
294 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000295
Dan Gohman343f0c02008-11-19 23:18:57 +0000296 // If this node has already been processed, stop now.
297 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000298
Dan Gohman343f0c02008-11-19 23:18:57 +0000299 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000300
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000301 // See if anything is glued to this node, if so, add them to glued
302 // nodes. Nodes can have at most one glue input and one glue output. Glue
303 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000304
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000305 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000306 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000307 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000308 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000309 N = N->getOperand(N->getNumOperands()-1).getNode();
310 assert(N->getNodeId() == -1 && "Node already inserted!");
311 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000312 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
313 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000314 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000315
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000316 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000317 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000318 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000319 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000320
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000321 // There are either zero or one users of the Glue result.
322 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000323 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000324 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000325 if (GlueVal.isOperandOf(*UI)) {
326 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000327 assert(N->getNodeId() == -1 && "Node already inserted!");
328 N->setNodeId(NodeSUnit->NodeNum);
329 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000330 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
331 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000332 break;
333 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000334 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000335 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000336
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000337 // If there are glue operands involved, N is now the bottom-most node
338 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000339 // Update the SUnit.
340 NodeSUnit->setNode(N);
341 assert(N->getNodeId() == -1 && "Node already inserted!");
342 N->setNodeId(NodeSUnit->NodeNum);
343
Andrew Trick92e94662011-02-04 03:18:17 +0000344 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
345 InitNumRegDefsLeft(NodeSUnit);
346
Dan Gohman787782f2008-11-21 01:44:51 +0000347 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000348 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000349 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000350}
351
352void ScheduleDAGSDNodes::AddSchedEdges() {
David Goodwin71046162009-08-13 16:05:04 +0000353 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
354
David Goodwindc4bdcd2009-08-19 16:08:58 +0000355 // Check to see if the scheduler cares about latencies.
356 bool UnitLatencies = ForceUnitLatencies();
357
Dan Gohman343f0c02008-11-19 23:18:57 +0000358 // Pass 2: add the preds, succs, etc.
359 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
360 SUnit *SU = &SUnits[su];
361 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000362
Dan Gohman343f0c02008-11-19 23:18:57 +0000363 if (MainNode->isMachineOpcode()) {
364 unsigned Opc = MainNode->getMachineOpcode();
365 const TargetInstrDesc &TID = TII->get(Opc);
366 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
367 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
368 SU->isTwoAddress = true;
369 break;
370 }
371 }
372 if (TID.isCommutable())
373 SU->isCommutable = true;
374 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000375
Dan Gohman343f0c02008-11-19 23:18:57 +0000376 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000377 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000378 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000379 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
380 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000381 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000382 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
383 --NumUsed; // Skip over unused values at the end.
384 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000385 SU->hasPhysRegDefs = true;
386 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000387
Dan Gohman343f0c02008-11-19 23:18:57 +0000388 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
389 SDNode *OpN = N->getOperand(i).getNode();
390 if (isPassiveNode(OpN)) continue; // Not scheduled.
391 SUnit *OpSU = &SUnits[OpN->getNodeId()];
392 assert(OpSU && "Node has no SUnit!");
393 if (OpSU == SU) continue; // In the same group.
394
Owen Andersone50ed302009-08-10 22:56:29 +0000395 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000396 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000398
399 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000400 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000401 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000402 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000403 assert((PhysReg == 0 || !isChain) &&
404 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000405 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
406 // emits a copy from the physical register to a virtual register unless
407 // it requires a cross class copy (cost < 0). That means we are only
408 // treating "expensive to copy" register dependency as physical register
409 // dependency. This may change in the future though.
410 if (Cost >= 0)
411 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000412
Evan Cheng046fa3f2010-05-28 23:26:21 +0000413 // If this is a ctrl dep, latency is 1.
414 unsigned OpLatency = isChain ? 1 : OpSU->Latency;
415 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
416 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000417 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000418 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000419 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000420 }
David Goodwin71046162009-08-13 16:05:04 +0000421
Andrew Trick4bbf4672011-03-09 19:12:43 +0000422 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000423 // Multiple register uses are combined in the same SUnit. For example,
424 // we could have a set of glued nodes with all their defs consumed by
425 // another set of glued nodes. Register pressure tracking sees this as
426 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000427 //
428 // We can't tell (without more book-keeping) if this results from
429 // glued nodes or duplicate operands. As long as we don't reduce
430 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000431 --OpSU->NumRegDefsLeft;
432 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000433 }
434 }
435 }
436}
437
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000438/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
439/// are input. This SUnit graph is similar to the SelectionDAG, but
440/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000441/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000442void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000443 // Cluster certain nodes which should be scheduled together.
444 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000445 // Populate the SUnits array.
446 BuildSchedUnits();
447 // Compute all the scheduling dependencies between nodes.
448 AddSchedEdges();
449}
450
Andrew Trick92e94662011-02-04 03:18:17 +0000451// Initialize NumNodeDefs for the current Node's opcode.
452void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000453 // Check for phys reg copy.
454 if (!Node)
455 return;
456
Andrew Trick92e94662011-02-04 03:18:17 +0000457 if (!Node->isMachineOpcode()) {
458 if (Node->getOpcode() == ISD::CopyFromReg)
459 NodeNumDefs = 1;
460 else
461 NodeNumDefs = 0;
462 return;
463 }
464 unsigned POpc = Node->getMachineOpcode();
465 if (POpc == TargetOpcode::IMPLICIT_DEF) {
466 // No register need be allocated for this.
467 NodeNumDefs = 0;
468 return;
469 }
470 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
471 // Some instructions define regs that are not represented in the selection DAG
472 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
473 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
474 DefIdx = 0;
475}
476
477// Construct a RegDefIter for this SUnit and find the first valid value.
478ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
479 const ScheduleDAGSDNodes *SD)
480 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
481 InitNodeNumDefs();
482 Advance();
483}
484
485// Advance to the next valid value defined by the SUnit.
486void ScheduleDAGSDNodes::RegDefIter::Advance() {
487 for (;Node;) { // Visit all glued nodes.
488 for (;DefIdx < NodeNumDefs; ++DefIdx) {
489 if (!Node->hasAnyUseOfValue(DefIdx))
490 continue;
491 if (Node->isMachineOpcode() &&
492 Node->getMachineOpcode() == TargetOpcode::EXTRACT_SUBREG) {
493 // Propagate the incoming (full-register) type. I doubt it's needed.
494 ValueType = Node->getOperand(0).getValueType();
495 }
496 else {
497 ValueType = Node->getValueType(DefIdx);
498 }
499 ++DefIdx;
500 return; // Found a normal regdef.
501 }
502 Node = Node->getGluedNode();
503 if (Node == NULL) {
504 return; // No values left to visit.
505 }
506 InitNodeNumDefs();
507 }
508}
509
510void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
511 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
512 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
513 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
514 ++SU->NumRegDefsLeft;
515 }
516}
517
Dan Gohman343f0c02008-11-19 23:18:57 +0000518void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Evan Chenge1631682010-05-19 22:42:23 +0000519 // Check to see if the scheduler cares about latencies.
520 if (ForceUnitLatencies()) {
521 SU->Latency = 1;
522 return;
523 }
524
Evan Cheng3ef1c872010-09-10 01:29:16 +0000525 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000526 SDNode *N = SU->getNode();
527 if (N && N->isMachineOpcode() &&
528 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000529 SU->Latency = HighLatencyCycles;
530 else
531 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000532 return;
533 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000534
Dan Gohman343f0c02008-11-19 23:18:57 +0000535 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000536 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000537 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000538 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000539 if (N->isMachineOpcode())
540 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000541}
542
Evan Cheng15a16de2010-05-20 06:13:19 +0000543void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
544 unsigned OpIdx, SDep& dep) const{
545 // Check to see if the scheduler cares about latencies.
546 if (ForceUnitLatencies())
547 return;
548
Evan Cheng15a16de2010-05-20 06:13:19 +0000549 if (dep.getKind() != SDep::Data)
550 return;
551
552 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000553 if (Use->isMachineOpcode())
554 // Adjust the use operand index by num of defs.
555 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000556 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000557 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
558 !BB->succ_empty()) {
559 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
560 if (TargetRegisterInfo::isVirtualRegister(Reg))
561 // This copy is a liveout value. It is likely coalesced, so reduce the
562 // latency so not to penalize the def.
563 // FIXME: need target specific adjustment here?
564 Latency = (Latency > 1) ? Latency - 1 : 1;
565 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000566 if (Latency >= 0)
567 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000568}
569
Dan Gohman343f0c02008-11-19 23:18:57 +0000570void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000571 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000572 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000573 return;
574 }
575
576 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000577 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000578 SmallVector<SDNode *, 4> GluedNodes;
579 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
580 GluedNodes.push_back(N);
581 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000582 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000583 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000584 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000585 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000586 }
587}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000588
Evan Chengbfcb3052010-03-25 01:38:16 +0000589namespace {
590 struct OrderSorter {
591 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
592 const std::pair<unsigned, MachineInstr*> &B) {
593 return A.first < B.first;
594 }
595 };
596}
597
Devang Patel55d20e82011-01-26 18:20:04 +0000598/// ProcessSDDbgValues - Process SDDbgValues assoicated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000599static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000600 InstrEmitter &Emitter,
601 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
602 DenseMap<SDValue, unsigned> &VRBaseMap,
603 unsigned Order) {
604 if (!N->getHasDebugValue())
605 return;
606
607 // Opportunistically insert immediate dbg_value uses, i.e. those with source
608 // order number right after the N.
609 MachineBasicBlock *BB = Emitter.getBlock();
610 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
611 SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N);
612 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
613 if (DVs[i]->isInvalidated())
614 continue;
615 unsigned DVOrder = DVs[i]->getOrder();
616 if (!Order || DVOrder == ++Order) {
617 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
618 if (DbgMI) {
619 Orders.push_back(std::make_pair(DVOrder, DbgMI));
620 BB->insert(InsertPos, DbgMI);
621 }
622 DVs[i]->setIsInvalidated();
623 }
624 }
625}
626
Evan Chengbfcb3052010-03-25 01:38:16 +0000627// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000628// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000629// instructions in the right order.
630static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
631 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000632 DenseMap<SDValue, unsigned> &VRBaseMap,
633 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
634 SmallSet<unsigned, 8> &Seen) {
635 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000636 if (!Order || !Seen.insert(Order)) {
637 // Process any valid SDDbgValues even if node does not have any order
638 // assigned.
639 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000640 return;
Devang Patel39078a82011-01-27 00:13:27 +0000641 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000642
643 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000644 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000645 // Did not insert any instruction.
646 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
647 return;
648 }
649
Dan Gohman84023e02010-07-10 09:00:22 +0000650 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000651 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000652}
653
654
Dan Gohmanbcea8592009-10-10 01:32:21 +0000655/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000656MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000657 InstrEmitter Emitter(BB, InsertPos);
658 DenseMap<SDValue, unsigned> VRBaseMap;
659 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000660 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
661 SmallSet<unsigned, 8> Seen;
662 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000663
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000664 // If this is the first BB, emit byval parameter dbg_value's.
665 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
666 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
667 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
668 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000669 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000670 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000671 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000672 }
673 }
674
Dan Gohmanbcea8592009-10-10 01:32:21 +0000675 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
676 SUnit *SU = Sequence[i];
677 if (!SU) {
678 // Null SUnit* is a noop.
679 EmitNoop();
680 continue;
681 }
682
683 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000684 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000685 if (!SU->getNode()) {
686 // Emit a copy.
687 EmitPhysRegCopy(SU, CopyVRBaseMap);
688 continue;
689 }
690
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000691 SmallVector<SDNode *, 4> GluedNodes;
692 for (SDNode *N = SU->getNode()->getGluedNode(); N;
693 N = N->getGluedNode())
694 GluedNodes.push_back(N);
695 while (!GluedNodes.empty()) {
696 SDNode *N = GluedNodes.back();
697 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000698 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000699 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000700 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000701 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000702 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000703 }
704 Emitter.EmitNode(SU->getNode(), 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(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000709 Seen);
710 }
711
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000712 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000713 // order sequence.
714 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000715 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000716
717 // Sort the source order instructions and use the order to insert debug
718 // values.
719 std::sort(Orders.begin(), Orders.end(), OrderSorter());
720
721 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
722 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
723 // Now emit the rest according to source order.
724 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000725 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
726 unsigned Order = Orders[i].first;
727 MachineInstr *MI = Orders[i].second;
728 // Insert all SDDbgValue's whose order(s) are before "Order".
729 if (!MI)
730 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000731 for (; DI != DE &&
732 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
733 if ((*DI)->isInvalidated())
734 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000735 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000736 if (DbgMI) {
737 if (!LastOrder)
738 // Insert to start of the BB (after PHIs).
739 BB->insert(BBBegin, DbgMI);
740 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000741 // Insert at the instruction, which may be in a different
742 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000743 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000744 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000745 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000746 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000747 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000748 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000749 }
750 // Add trailing DbgValue's before the terminator. FIXME: May want to add
751 // some of them before one or more conditional branches?
752 while (DI != DE) {
753 MachineBasicBlock *InsertBB = Emitter.getBlock();
754 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
755 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000756 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000757 if (DbgMI)
758 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000759 }
760 ++DI;
761 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000762 }
763
764 BB = Emitter.getBlock();
765 InsertPos = Emitter.getInsertPos();
766 return BB;
767}