blob: 1e5146ed31d77e1db50c31eb1922652e7d6dafdc [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"
Evan Chengab8be962011-06-29 01:14:12 +000020#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng1cc39842010-05-20 23:26:43 +000023#include "llvm/Target/TargetLowering.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000024#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000025#include "llvm/Target/TargetSubtargetInfo.h"
Evan Chengc589e032010-01-22 03:36:51 +000026#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/SmallPtrSet.h"
Evan Chengbfcb3052010-03-25 01:38:16 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengc589e032010-01-22 03:36:51 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/Statistic.h"
Andrew Tricke0ef5092011-03-05 08:00:22 +000031#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34using namespace llvm;
35
Evan Chengc589e032010-01-22 03:36:51 +000036STATISTIC(LoadsClustered, "Number of loads clustered together");
37
Andrew Tricke0ef5092011-03-05 08:00:22 +000038// This allows latency based scheduler to notice high latency instructions
39// without a target itinerary. The choise if number here has more to do with
40// balancing scheduler heursitics than with the actual machine latency.
41static cl::opt<int> HighLatencyCycles(
42 "sched-high-latency-cycles", cl::Hidden, cl::init(10),
43 cl::desc("Roughly estimate the number of cycles that 'long latency'"
44 "instructions take for targets with no itinerary"));
45
Dan Gohman79ce2762009-01-15 19:20:50 +000046ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
Evan Cheng3ef1c872010-09-10 01:29:16 +000047 : ScheduleDAG(mf),
48 InstrItins(mf.getTarget().getInstrItineraryData()) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000049
Dan Gohman47ac0f02009-02-11 04:27:20 +000050/// Run - perform scheduling.
51///
52void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
53 MachineBasicBlock::iterator insertPos) {
54 DAG = dag;
55 ScheduleDAG::Run(bb, insertPos);
56}
57
Evan Cheng1cc39842010-05-20 23:26:43 +000058/// NewSUnit - Creates a new SUnit and return a ptr to it.
59///
60SUnit *ScheduleDAGSDNodes::NewSUnit(SDNode *N) {
61#ifndef NDEBUG
62 const SUnit *Addr = 0;
63 if (!SUnits.empty())
64 Addr = &SUnits[0];
65#endif
66 SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
67 assert((Addr == 0 || Addr == &SUnits[0]) &&
68 "SUnits std::vector reallocated on the fly!");
69 SUnits.back().OrigNode = &SUnits.back();
70 SUnit *SU = &SUnits.back();
71 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
Evan Chengc120af42010-08-10 02:39:45 +000072 if (!N ||
73 (N->isMachineOpcode() &&
74 N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
Evan Cheng046fa3f2010-05-28 23:26:21 +000075 SU->SchedulingPref = Sched::None;
76 else
77 SU->SchedulingPref = TLI.getSchedulingPreference(N);
Evan Cheng1cc39842010-05-20 23:26:43 +000078 return SU;
79}
80
Dan Gohman343f0c02008-11-19 23:18:57 +000081SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
82 SUnit *SU = NewSUnit(Old->getNode());
83 SU->OrigNode = Old->OrigNode;
84 SU->Latency = Old->Latency;
Andrew Trick54699762011-04-07 19:54:57 +000085 SU->isVRegCycle = Old->isVRegCycle;
Evan Cheng8239daf2010-11-03 00:45:17 +000086 SU->isCall = Old->isCall;
Evan Cheng554daa62011-04-26 21:31:35 +000087 SU->isCallOp = Old->isCallOp;
Dan Gohman343f0c02008-11-19 23:18:57 +000088 SU->isTwoAddress = Old->isTwoAddress;
89 SU->isCommutable = Old->isCommutable;
90 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000091 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Andrew Trick12f0dc62011-04-14 05:15:06 +000092 SU->isScheduleHigh = Old->isScheduleHigh;
93 SU->isScheduleLow = Old->isScheduleLow;
Evan Cheng1cc39842010-05-20 23:26:43 +000094 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000095 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000096 return SU;
97}
98
99/// CheckForPhysRegDependency - Check if the dependency between def and use of
100/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +0000101/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +0000102static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +0000103 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +0000104 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000105 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
107 return;
108
109 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
110 if (TargetRegisterInfo::isVirtualRegister(Reg))
111 return;
112
113 unsigned ResNo = User->getOperand(2).getResNo();
114 if (Def->isMachineOpcode()) {
Evan Chenge837dea2011-06-28 19:10:37 +0000115 const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
Dan Gohman343f0c02008-11-19 23:18:57 +0000116 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000117 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000118 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000119 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000120 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000121 Cost = RC->getCopyCost();
122 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000123 }
124}
125
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000126static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000127 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000128 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000129
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000130 // Don't add glue from a node to itself.
131 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000132
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000133 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000134 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000135
136 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
137 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000138
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000139 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000140 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000141
Evan Chengc589e032010-01-22 03:36:51 +0000142 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000143 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
144 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000145
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000146 if (GlueDestNode)
147 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000148
Evan Chengc589e032010-01-22 03:36:51 +0000149 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000150 MachineSDNode::mmo_iterator Begin = 0, End = 0;
151 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
152
153 // Store memory references.
154 if (MN) {
155 Begin = MN->memoperands_begin();
156 End = MN->memoperands_end();
157 }
158
Evan Chengc589e032010-01-22 03:36:51 +0000159 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000160
161 // Reset the memory references
162 if (MN)
163 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000164}
165
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000166/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000167/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000168/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000169/// outputs to ensure they are scheduled together and in order. This
170/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000171void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
172 SDNode *Chain = 0;
173 unsigned NumOps = Node->getNumOperands();
174 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
175 Chain = Node->getOperand(NumOps-1).getNode();
176 if (!Chain)
177 return;
178
179 // Look for other loads of the same chain. Find loads that are loading from
180 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000181 SmallPtrSet<SDNode*, 16> Visited;
182 SmallVector<int64_t, 4> Offsets;
183 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000184 bool Cluster = false;
185 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000186 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
187 I != E; ++I) {
188 SDNode *User = *I;
189 if (User == Node || !Visited.insert(User))
190 continue;
191 int64_t Offset1, Offset2;
192 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
193 Offset1 == Offset2)
194 // FIXME: Should be ok if they addresses are identical. But earlier
195 // optimizations really should have eliminated one of the loads.
196 continue;
197 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
198 Offsets.push_back(Offset1);
199 O2SMap.insert(std::make_pair(Offset2, User));
200 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000201 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000202 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000203 Cluster = true;
204 }
205
206 if (!Cluster)
207 return;
208
209 // Sort them in increasing order.
210 std::sort(Offsets.begin(), Offsets.end());
211
212 // Check if the loads are close enough.
213 SmallVector<SDNode*, 4> Loads;
214 unsigned NumLoads = 0;
215 int64_t BaseOff = Offsets[0];
216 SDNode *BaseLoad = O2SMap[BaseOff];
217 Loads.push_back(BaseLoad);
218 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
219 int64_t Offset = Offsets[i];
220 SDNode *Load = O2SMap[Offset];
221 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
222 break; // Stop right here. Ignore loads that are further away.
223 Loads.push_back(Load);
224 ++NumLoads;
225 }
226
227 if (NumLoads == 0)
228 return;
229
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000230 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000231 // ensure they are scheduled in order of increasing addresses.
232 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000233 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000234
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000235 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000236 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000237 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000238 SDNode *Load = Loads[I];
239
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000240 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000241
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000242 if (OutGlue)
243 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000244
Evan Cheng302ef832010-06-10 02:09:31 +0000245 ++LoadsClustered;
246 }
247}
248
249/// ClusterNodes - Cluster certain nodes which should be scheduled together.
250///
251void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000252 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
253 E = DAG->allnodes_end(); NI != E; ++NI) {
254 SDNode *Node = &*NI;
255 if (!Node || !Node->isMachineOpcode())
256 continue;
257
258 unsigned Opc = Node->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000259 const MCInstrDesc &MCID = TII->get(Opc);
260 if (MCID.mayLoad())
Evan Cheng302ef832010-06-10 02:09:31 +0000261 // Cluster loads from "near" addresses into combined SUnits.
262 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000263 }
264}
265
Dan Gohman343f0c02008-11-19 23:18:57 +0000266void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000267 // During scheduling, the NodeId field of SDNode is used to map SDNodes
268 // to their associated SUnits by holding SUnits table indices. A value
269 // of -1 means the SDNode does not yet have an associated SUnit.
270 unsigned NumNodes = 0;
271 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
272 E = DAG->allnodes_end(); NI != E; ++NI) {
273 NI->setNodeId(-1);
274 ++NumNodes;
275 }
276
Dan Gohman343f0c02008-11-19 23:18:57 +0000277 // Reserve entries in the vector for each of the SUnits we are creating. This
278 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
279 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000280 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
281 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000282 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000283
Chris Lattner736a6ea2010-02-24 06:11:37 +0000284 // Add all nodes in depth first order.
285 SmallVector<SDNode*, 64> Worklist;
286 SmallPtrSet<SDNode*, 64> Visited;
287 Worklist.push_back(DAG->getRoot().getNode());
288 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000289
Evan Cheng554daa62011-04-26 21:31:35 +0000290 SmallVector<SUnit*, 8> CallSUnits;
Chris Lattner736a6ea2010-02-24 06:11:37 +0000291 while (!Worklist.empty()) {
292 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000293
Chris Lattner736a6ea2010-02-24 06:11:37 +0000294 // Add all operands to the worklist unless they've already been added.
295 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
296 if (Visited.insert(NI->getOperand(i).getNode()))
297 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000298
Dan Gohman343f0c02008-11-19 23:18:57 +0000299 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
300 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000301
Dan Gohman343f0c02008-11-19 23:18:57 +0000302 // If this node has already been processed, stop now.
303 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000304
Dan Gohman343f0c02008-11-19 23:18:57 +0000305 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000306
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000307 // See if anything is glued to this node, if so, add them to glued
308 // nodes. Nodes can have at most one glue input and one glue output. Glue
309 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000310
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000311 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000312 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000313 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000314 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000315 N = N->getOperand(N->getNumOperands()-1).getNode();
316 assert(N->getNodeId() == -1 && "Node already inserted!");
317 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000318 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
319 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000320 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000321
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000322 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000323 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000324 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000325 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000326
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000327 // There are either zero or one users of the Glue result.
328 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000329 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000330 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000331 if (GlueVal.isOperandOf(*UI)) {
332 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000333 assert(N->getNodeId() == -1 && "Node already inserted!");
334 N->setNodeId(NodeSUnit->NodeNum);
335 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000336 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
337 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000338 break;
339 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000340 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000341 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000342
Evan Cheng554daa62011-04-26 21:31:35 +0000343 if (NodeSUnit->isCall)
344 CallSUnits.push_back(NodeSUnit);
345
Andrew Trick12f0dc62011-04-14 05:15:06 +0000346 // Schedule zero-latency TokenFactor below any nodes that may increase the
347 // schedule height. Otherwise, ancestors of the TokenFactor may appear to
348 // have false stalls.
349 if (NI->getOpcode() == ISD::TokenFactor)
350 NodeSUnit->isScheduleLow = true;
351
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000352 // If there are glue operands involved, N is now the bottom-most node
353 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000354 // Update the SUnit.
355 NodeSUnit->setNode(N);
356 assert(N->getNodeId() == -1 && "Node already inserted!");
357 N->setNodeId(NodeSUnit->NodeNum);
358
Andrew Trick92e94662011-02-04 03:18:17 +0000359 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
360 InitNumRegDefsLeft(NodeSUnit);
361
Dan Gohman787782f2008-11-21 01:44:51 +0000362 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000363 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000364 }
Evan Cheng554daa62011-04-26 21:31:35 +0000365
366 // Find all call operands.
367 while (!CallSUnits.empty()) {
368 SUnit *SU = CallSUnits.pop_back_val();
369 for (const SDNode *SUNode = SU->getNode(); SUNode;
370 SUNode = SUNode->getGluedNode()) {
371 if (SUNode->getOpcode() != ISD::CopyToReg)
372 continue;
373 SDNode *SrcN = SUNode->getOperand(2).getNode();
374 if (isPassiveNode(SrcN)) continue; // Not scheduled.
375 SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
376 SrcSU->isCallOp = true;
377 }
378 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000379}
380
381void ScheduleDAGSDNodes::AddSchedEdges() {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000382 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
David Goodwin71046162009-08-13 16:05:04 +0000383
David Goodwindc4bdcd2009-08-19 16:08:58 +0000384 // Check to see if the scheduler cares about latencies.
385 bool UnitLatencies = ForceUnitLatencies();
386
Dan Gohman343f0c02008-11-19 23:18:57 +0000387 // Pass 2: add the preds, succs, etc.
388 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
389 SUnit *SU = &SUnits[su];
390 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000391
Dan Gohman343f0c02008-11-19 23:18:57 +0000392 if (MainNode->isMachineOpcode()) {
393 unsigned Opc = MainNode->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000394 const MCInstrDesc &MCID = TII->get(Opc);
395 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
396 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000397 SU->isTwoAddress = true;
398 break;
399 }
400 }
Evan Chenge837dea2011-06-28 19:10:37 +0000401 if (MCID.isCommutable())
Dan Gohman343f0c02008-11-19 23:18:57 +0000402 SU->isCommutable = true;
403 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000404
Dan Gohman343f0c02008-11-19 23:18:57 +0000405 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000406 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000407 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000408 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
409 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000410 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000411 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
412 --NumUsed; // Skip over unused values at the end.
413 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000414 SU->hasPhysRegDefs = true;
415 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000416
Dan Gohman343f0c02008-11-19 23:18:57 +0000417 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
418 SDNode *OpN = N->getOperand(i).getNode();
419 if (isPassiveNode(OpN)) continue; // Not scheduled.
420 SUnit *OpSU = &SUnits[OpN->getNodeId()];
421 assert(OpSU && "Node has no SUnit!");
422 if (OpSU == SU) continue; // In the same group.
423
Owen Andersone50ed302009-08-10 22:56:29 +0000424 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000425 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000427
428 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000429 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000430 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000431 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000432 assert((PhysReg == 0 || !isChain) &&
433 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000434 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
435 // emits a copy from the physical register to a virtual register unless
436 // it requires a cross class copy (cost < 0). That means we are only
437 // treating "expensive to copy" register dependency as physical register
438 // dependency. This may change in the future though.
Andrew Trick4cb971c2011-06-15 17:16:12 +0000439 if (Cost >= 0 && !StressSched)
Evan Chengc29a56d2009-01-12 03:19:55 +0000440 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000441
Evan Cheng046fa3f2010-05-28 23:26:21 +0000442 // If this is a ctrl dep, latency is 1.
Andrew Trickc558bf32011-04-12 20:14:07 +0000443 unsigned OpLatency = isChain ? 1 : OpSU->Latency;
Andrew Trick87896d92011-04-13 00:38:32 +0000444 // Special-case TokenFactor chains as zero-latency.
445 if(isChain && OpN->getOpcode() == ISD::TokenFactor)
446 OpLatency = 0;
447
Evan Cheng046fa3f2010-05-28 23:26:21 +0000448 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
449 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000450 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000451 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000452 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000453 }
David Goodwin71046162009-08-13 16:05:04 +0000454
Andrew Trick4bbf4672011-03-09 19:12:43 +0000455 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000456 // Multiple register uses are combined in the same SUnit. For example,
457 // we could have a set of glued nodes with all their defs consumed by
458 // another set of glued nodes. Register pressure tracking sees this as
459 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000460 //
461 // We can't tell (without more book-keeping) if this results from
462 // glued nodes or duplicate operands. As long as we don't reduce
463 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000464 --OpSU->NumRegDefsLeft;
465 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000466 }
467 }
468 }
469}
470
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000471/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
472/// are input. This SUnit graph is similar to the SelectionDAG, but
473/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000474/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000475void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000476 // Cluster certain nodes which should be scheduled together.
477 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000478 // Populate the SUnits array.
479 BuildSchedUnits();
480 // Compute all the scheduling dependencies between nodes.
481 AddSchedEdges();
482}
483
Andrew Trick92e94662011-02-04 03:18:17 +0000484// Initialize NumNodeDefs for the current Node's opcode.
485void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000486 // Check for phys reg copy.
487 if (!Node)
488 return;
489
Andrew Trick92e94662011-02-04 03:18:17 +0000490 if (!Node->isMachineOpcode()) {
491 if (Node->getOpcode() == ISD::CopyFromReg)
492 NodeNumDefs = 1;
493 else
494 NodeNumDefs = 0;
495 return;
496 }
497 unsigned POpc = Node->getMachineOpcode();
498 if (POpc == TargetOpcode::IMPLICIT_DEF) {
499 // No register need be allocated for this.
500 NodeNumDefs = 0;
501 return;
502 }
503 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
504 // Some instructions define regs that are not represented in the selection DAG
505 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
506 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
507 DefIdx = 0;
508}
509
510// Construct a RegDefIter for this SUnit and find the first valid value.
511ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
512 const ScheduleDAGSDNodes *SD)
513 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
514 InitNodeNumDefs();
515 Advance();
516}
517
518// Advance to the next valid value defined by the SUnit.
519void ScheduleDAGSDNodes::RegDefIter::Advance() {
520 for (;Node;) { // Visit all glued nodes.
521 for (;DefIdx < NodeNumDefs; ++DefIdx) {
522 if (!Node->hasAnyUseOfValue(DefIdx))
523 continue;
Andrew Trick4ef4c172011-06-27 18:01:20 +0000524 ValueType = Node->getValueType(DefIdx);
Andrew Trick92e94662011-02-04 03:18:17 +0000525 ++DefIdx;
526 return; // Found a normal regdef.
527 }
528 Node = Node->getGluedNode();
529 if (Node == NULL) {
530 return; // No values left to visit.
531 }
532 InitNodeNumDefs();
533 }
534}
535
536void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
537 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
538 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
539 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
540 ++SU->NumRegDefsLeft;
541 }
542}
543
Dan Gohman343f0c02008-11-19 23:18:57 +0000544void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Andrew Trick87896d92011-04-13 00:38:32 +0000545 SDNode *N = SU->getNode();
546
547 // TokenFactor operands are considered zero latency, and some schedulers
548 // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
549 // whenever node latency is nonzero.
550 if (N && N->getOpcode() == ISD::TokenFactor) {
551 SU->Latency = 0;
552 return;
553 }
554
Evan Chenge1631682010-05-19 22:42:23 +0000555 // Check to see if the scheduler cares about latencies.
556 if (ForceUnitLatencies()) {
557 SU->Latency = 1;
558 return;
559 }
560
Evan Cheng3ef1c872010-09-10 01:29:16 +0000561 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000562 if (N && N->isMachineOpcode() &&
563 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000564 SU->Latency = HighLatencyCycles;
565 else
566 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000567 return;
568 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000569
Dan Gohman343f0c02008-11-19 23:18:57 +0000570 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000571 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000572 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000573 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000574 if (N->isMachineOpcode())
575 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000576}
577
Evan Cheng15a16de2010-05-20 06:13:19 +0000578void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
579 unsigned OpIdx, SDep& dep) const{
580 // Check to see if the scheduler cares about latencies.
581 if (ForceUnitLatencies())
582 return;
583
Evan Cheng15a16de2010-05-20 06:13:19 +0000584 if (dep.getKind() != SDep::Data)
585 return;
586
587 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000588 if (Use->isMachineOpcode())
589 // Adjust the use operand index by num of defs.
590 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000591 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000592 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
593 !BB->succ_empty()) {
594 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
595 if (TargetRegisterInfo::isVirtualRegister(Reg))
596 // This copy is a liveout value. It is likely coalesced, so reduce the
597 // latency so not to penalize the def.
598 // FIXME: need target specific adjustment here?
599 Latency = (Latency > 1) ? Latency - 1 : 1;
600 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000601 if (Latency >= 0)
602 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000603}
604
Dan Gohman343f0c02008-11-19 23:18:57 +0000605void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000606 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000607 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000608 return;
609 }
610
611 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000612 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000613 SmallVector<SDNode *, 4> GluedNodes;
614 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
615 GluedNodes.push_back(N);
616 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000617 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000618 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000619 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000620 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000621 }
622}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000623
Andrew Trick4c727202012-03-07 05:21:36 +0000624#ifndef NDEBUG
625/// VerifyScheduledSequence - Verify that all SUnits were scheduled and that
626/// their state is consistent with the nodes listed in Sequence.
627///
628void ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) {
629 unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp);
630 unsigned Noops = 0;
631 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
632 if (!Sequence[i])
633 ++Noops;
634 assert(Sequence.size() - Noops == ScheduledNodes &&
635 "The number of nodes scheduled doesn't match the expected number!");
636}
637#endif // NDEBUG
638
Evan Chengbfcb3052010-03-25 01:38:16 +0000639namespace {
640 struct OrderSorter {
641 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
642 const std::pair<unsigned, MachineInstr*> &B) {
643 return A.first < B.first;
644 }
645 };
646}
647
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000648/// ProcessSDDbgValues - Process SDDbgValues associated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000649static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000650 InstrEmitter &Emitter,
651 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
652 DenseMap<SDValue, unsigned> &VRBaseMap,
653 unsigned Order) {
654 if (!N->getHasDebugValue())
655 return;
656
657 // Opportunistically insert immediate dbg_value uses, i.e. those with source
658 // order number right after the N.
659 MachineBasicBlock *BB = Emitter.getBlock();
660 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000661 ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N);
Devang Patel55d20e82011-01-26 18:20:04 +0000662 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
663 if (DVs[i]->isInvalidated())
664 continue;
665 unsigned DVOrder = DVs[i]->getOrder();
666 if (!Order || DVOrder == ++Order) {
667 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
668 if (DbgMI) {
669 Orders.push_back(std::make_pair(DVOrder, DbgMI));
670 BB->insert(InsertPos, DbgMI);
671 }
672 DVs[i]->setIsInvalidated();
673 }
674 }
675}
676
Evan Chengbfcb3052010-03-25 01:38:16 +0000677// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000678// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000679// instructions in the right order.
680static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
681 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000682 DenseMap<SDValue, unsigned> &VRBaseMap,
683 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
684 SmallSet<unsigned, 8> &Seen) {
685 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000686 if (!Order || !Seen.insert(Order)) {
687 // Process any valid SDDbgValues even if node does not have any order
688 // assigned.
689 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000690 return;
Devang Patel39078a82011-01-27 00:13:27 +0000691 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000692
693 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000694 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000695 // Did not insert any instruction.
696 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
697 return;
698 }
699
Dan Gohman84023e02010-07-10 09:00:22 +0000700 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000701 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000702}
703
704
Dan Gohmanbcea8592009-10-10 01:32:21 +0000705/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000706MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000707 InstrEmitter Emitter(BB, InsertPos);
708 DenseMap<SDValue, unsigned> VRBaseMap;
709 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000710 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
711 SmallSet<unsigned, 8> Seen;
712 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000713
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000714 // If this is the first BB, emit byval parameter dbg_value's.
715 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
716 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
717 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
718 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000719 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000720 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000721 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000722 }
723 }
724
Dan Gohmanbcea8592009-10-10 01:32:21 +0000725 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
726 SUnit *SU = Sequence[i];
727 if (!SU) {
728 // Null SUnit* is a noop.
729 EmitNoop();
730 continue;
731 }
732
733 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000734 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000735 if (!SU->getNode()) {
736 // Emit a copy.
737 EmitPhysRegCopy(SU, CopyVRBaseMap);
738 continue;
739 }
740
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000741 SmallVector<SDNode *, 4> GluedNodes;
742 for (SDNode *N = SU->getNode()->getGluedNode(); N;
743 N = N->getGluedNode())
744 GluedNodes.push_back(N);
745 while (!GluedNodes.empty()) {
746 SDNode *N = GluedNodes.back();
747 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000748 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000749 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000750 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000751 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000752 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000753 }
754 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000755 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000756 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000757 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000758 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000759 Seen);
760 }
761
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000762 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000763 // order sequence.
764 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000765 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000766
767 // Sort the source order instructions and use the order to insert debug
768 // values.
769 std::sort(Orders.begin(), Orders.end(), OrderSorter());
770
771 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
772 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
773 // Now emit the rest according to source order.
774 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000775 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
776 unsigned Order = Orders[i].first;
777 MachineInstr *MI = Orders[i].second;
778 // Insert all SDDbgValue's whose order(s) are before "Order".
779 if (!MI)
780 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000781 for (; DI != DE &&
782 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
783 if ((*DI)->isInvalidated())
784 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000785 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000786 if (DbgMI) {
787 if (!LastOrder)
788 // Insert to start of the BB (after PHIs).
789 BB->insert(BBBegin, DbgMI);
790 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000791 // Insert at the instruction, which may be in a different
792 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000793 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000794 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000795 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000796 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000797 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000798 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000799 }
800 // Add trailing DbgValue's before the terminator. FIXME: May want to add
801 // some of them before one or more conditional branches?
802 while (DI != DE) {
803 MachineBasicBlock *InsertBB = Emitter.getBlock();
804 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
805 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000806 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000807 if (DbgMI)
808 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000809 }
810 ++DI;
811 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000812 }
813
814 BB = Emitter.getBlock();
815 InsertPos = Emitter.getInsertPos();
816 return BB;
817}
Andrew Trick56b94c52012-03-07 00:18:22 +0000818
819/// Return the basic block label.
820std::string ScheduleDAGSDNodes::getDAGName() const {
821 return "sunit-dag." + BB->getFullName();
822}