blob: 9c870d5e8c8181814acbc246dbb7c9532e9ebed3 [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"
Andrew Trick84b454d2012-03-07 05:21:44 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000022#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng1cc39842010-05-20 23:26:43 +000025#include "llvm/Target/TargetLowering.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000027#include "llvm/Target/TargetSubtargetInfo.h"
Evan Chengc589e032010-01-22 03:36:51 +000028#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/SmallPtrSet.h"
Evan Chengbfcb3052010-03-25 01:38:16 +000030#include "llvm/ADT/SmallSet.h"
Evan Chengc589e032010-01-22 03:36:51 +000031#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/Statistic.h"
Andrew Tricke0ef5092011-03-05 08:00:22 +000033#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
36using namespace llvm;
37
Evan Chengc589e032010-01-22 03:36:51 +000038STATISTIC(LoadsClustered, "Number of loads clustered together");
39
Andrew Tricke0ef5092011-03-05 08:00:22 +000040// This allows latency based scheduler to notice high latency instructions
41// without a target itinerary. The choise if number here has more to do with
42// balancing scheduler heursitics than with the actual machine latency.
43static cl::opt<int> HighLatencyCycles(
44 "sched-high-latency-cycles", cl::Hidden, cl::init(10),
45 cl::desc("Roughly estimate the number of cycles that 'long latency'"
46 "instructions take for targets with no itinerary"));
47
Dan Gohman79ce2762009-01-15 19:20:50 +000048ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
Evan Cheng3ef1c872010-09-10 01:29:16 +000049 : ScheduleDAG(mf),
50 InstrItins(mf.getTarget().getInstrItineraryData()) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000051
Dan Gohman47ac0f02009-02-11 04:27:20 +000052/// Run - perform scheduling.
53///
54void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
55 MachineBasicBlock::iterator insertPos) {
56 DAG = dag;
57 ScheduleDAG::Run(bb, insertPos);
58}
59
Evan Cheng1cc39842010-05-20 23:26:43 +000060/// NewSUnit - Creates a new SUnit and return a ptr to it.
61///
62SUnit *ScheduleDAGSDNodes::NewSUnit(SDNode *N) {
63#ifndef NDEBUG
64 const SUnit *Addr = 0;
65 if (!SUnits.empty())
66 Addr = &SUnits[0];
67#endif
68 SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
69 assert((Addr == 0 || Addr == &SUnits[0]) &&
70 "SUnits std::vector reallocated on the fly!");
71 SUnits.back().OrigNode = &SUnits.back();
72 SUnit *SU = &SUnits.back();
73 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
Evan Chengc120af42010-08-10 02:39:45 +000074 if (!N ||
75 (N->isMachineOpcode() &&
76 N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
Evan Cheng046fa3f2010-05-28 23:26:21 +000077 SU->SchedulingPref = Sched::None;
78 else
79 SU->SchedulingPref = TLI.getSchedulingPreference(N);
Evan Cheng1cc39842010-05-20 23:26:43 +000080 return SU;
81}
82
Dan Gohman343f0c02008-11-19 23:18:57 +000083SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
84 SUnit *SU = NewSUnit(Old->getNode());
85 SU->OrigNode = Old->OrigNode;
86 SU->Latency = Old->Latency;
Andrew Trick54699762011-04-07 19:54:57 +000087 SU->isVRegCycle = Old->isVRegCycle;
Evan Cheng8239daf2010-11-03 00:45:17 +000088 SU->isCall = Old->isCall;
Evan Cheng554daa62011-04-26 21:31:35 +000089 SU->isCallOp = Old->isCallOp;
Dan Gohman343f0c02008-11-19 23:18:57 +000090 SU->isTwoAddress = Old->isTwoAddress;
91 SU->isCommutable = Old->isCommutable;
92 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Dan Gohman39746672009-03-23 16:10:52 +000093 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
Andrew Trick12f0dc62011-04-14 05:15:06 +000094 SU->isScheduleHigh = Old->isScheduleHigh;
95 SU->isScheduleLow = Old->isScheduleLow;
Evan Cheng1cc39842010-05-20 23:26:43 +000096 SU->SchedulingPref = Old->SchedulingPref;
Evan Chenge57187c2009-01-16 20:57:18 +000097 Old->isCloned = true;
Dan Gohman343f0c02008-11-19 23:18:57 +000098 return SU;
99}
100
101/// CheckForPhysRegDependency - Check if the dependency between def and use of
102/// a specified operand is a physical register dependency. If so, returns the
Evan Chengc29a56d2009-01-12 03:19:55 +0000103/// register and the cost of copying the register.
Dan Gohman343f0c02008-11-19 23:18:57 +0000104static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Andrew Trickcd5af072011-02-03 23:00:17 +0000105 const TargetRegisterInfo *TRI,
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 const TargetInstrInfo *TII,
Evan Chengc29a56d2009-01-12 03:19:55 +0000107 unsigned &PhysReg, int &Cost) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000108 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
109 return;
110
111 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
112 if (TargetRegisterInfo::isVirtualRegister(Reg))
113 return;
114
115 unsigned ResNo = User->getOperand(2).getResNo();
116 if (Def->isMachineOpcode()) {
Evan Chenge837dea2011-06-28 19:10:37 +0000117 const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
Dan Gohman343f0c02008-11-19 23:18:57 +0000118 if (ResNo >= II.getNumDefs() &&
Evan Chengc29a56d2009-01-12 03:19:55 +0000119 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000120 PhysReg = Reg;
Evan Chengc29a56d2009-01-12 03:19:55 +0000121 const TargetRegisterClass *RC =
Rafael Espindolad31f9722010-06-29 14:02:34 +0000122 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
Evan Chengc29a56d2009-01-12 03:19:55 +0000123 Cost = RC->getCopyCost();
124 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000125 }
126}
127
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000128static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
Evan Chengc589e032010-01-22 03:36:51 +0000129 SmallVector<EVT, 4> VTs;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000130 SDNode *GlueDestNode = Glue.getNode();
Bill Wendling151d26d2010-06-23 18:16:24 +0000131
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000132 // Don't add glue from a node to itself.
133 if (GlueDestNode == N) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000134
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000135 // Don't add glue to something which already has glue.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000136 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return;
Bill Wendling10707f32010-06-24 22:00:37 +0000137
138 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
139 VTs.push_back(N->getValueType(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000140
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000141 if (AddGlue)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000142 VTs.push_back(MVT::Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000143
Evan Chengc589e032010-01-22 03:36:51 +0000144 SmallVector<SDValue, 4> Ops;
Bill Wendling10707f32010-06-24 22:00:37 +0000145 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
146 Ops.push_back(N->getOperand(I));
Bill Wendling151d26d2010-06-23 18:16:24 +0000147
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000148 if (GlueDestNode)
149 Ops.push_back(Glue);
Bill Wendling151d26d2010-06-23 18:16:24 +0000150
Evan Chengc589e032010-01-22 03:36:51 +0000151 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000152 MachineSDNode::mmo_iterator Begin = 0, End = 0;
153 MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
154
155 // Store memory references.
156 if (MN) {
157 Begin = MN->memoperands_begin();
158 End = MN->memoperands_end();
159 }
160
Evan Chengc589e032010-01-22 03:36:51 +0000161 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
Bill Wendling151d26d2010-06-23 18:16:24 +0000162
163 // Reset the memory references
164 if (MN)
165 MN->setMemRefs(Begin, End);
Evan Chengc589e032010-01-22 03:36:51 +0000166}
167
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000168/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
Evan Chengc589e032010-01-22 03:36:51 +0000169/// This function finds loads of the same base and different offsets. If the
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000170/// offsets are not far apart (target specific), it add MVT::Glue inputs and
Evan Chengc589e032010-01-22 03:36:51 +0000171/// outputs to ensure they are scheduled together and in order. This
172/// optimization may benefit some targets by improving cache locality.
Evan Cheng302ef832010-06-10 02:09:31 +0000173void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
174 SDNode *Chain = 0;
175 unsigned NumOps = Node->getNumOperands();
176 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
177 Chain = Node->getOperand(NumOps-1).getNode();
178 if (!Chain)
179 return;
180
181 // Look for other loads of the same chain. Find loads that are loading from
182 // the same base pointer and different offsets.
Evan Chengc589e032010-01-22 03:36:51 +0000183 SmallPtrSet<SDNode*, 16> Visited;
184 SmallVector<int64_t, 4> Offsets;
185 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
Evan Cheng302ef832010-06-10 02:09:31 +0000186 bool Cluster = false;
187 SDNode *Base = Node;
Evan Cheng302ef832010-06-10 02:09:31 +0000188 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
189 I != E; ++I) {
190 SDNode *User = *I;
191 if (User == Node || !Visited.insert(User))
192 continue;
193 int64_t Offset1, Offset2;
194 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
195 Offset1 == Offset2)
196 // FIXME: Should be ok if they addresses are identical. But earlier
197 // optimizations really should have eliminated one of the loads.
198 continue;
199 if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
200 Offsets.push_back(Offset1);
201 O2SMap.insert(std::make_pair(Offset2, User));
202 Offsets.push_back(Offset2);
Duncan Sandsb447c4e2010-06-25 14:48:39 +0000203 if (Offset2 < Offset1)
Evan Cheng302ef832010-06-10 02:09:31 +0000204 Base = User;
Evan Cheng302ef832010-06-10 02:09:31 +0000205 Cluster = true;
206 }
207
208 if (!Cluster)
209 return;
210
211 // Sort them in increasing order.
212 std::sort(Offsets.begin(), Offsets.end());
213
214 // Check if the loads are close enough.
215 SmallVector<SDNode*, 4> Loads;
216 unsigned NumLoads = 0;
217 int64_t BaseOff = Offsets[0];
218 SDNode *BaseLoad = O2SMap[BaseOff];
219 Loads.push_back(BaseLoad);
220 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
221 int64_t Offset = Offsets[i];
222 SDNode *Load = O2SMap[Offset];
223 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
224 break; // Stop right here. Ignore loads that are further away.
225 Loads.push_back(Load);
226 ++NumLoads;
227 }
228
229 if (NumLoads == 0)
230 return;
231
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000232 // Cluster loads by adding MVT::Glue outputs and inputs. This also
Evan Cheng302ef832010-06-10 02:09:31 +0000233 // ensure they are scheduled in order of increasing addresses.
234 SDNode *Lead = Loads[0];
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000235 AddGlue(Lead, SDValue(0, 0), true, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000236
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000237 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1);
Bill Wendling10707f32010-06-24 22:00:37 +0000238 for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000239 bool OutGlue = I < E - 1;
Bill Wendling10707f32010-06-24 22:00:37 +0000240 SDNode *Load = Loads[I];
241
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000242 AddGlue(Load, InGlue, OutGlue, DAG);
Bill Wendling151d26d2010-06-23 18:16:24 +0000243
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000244 if (OutGlue)
245 InGlue = SDValue(Load, Load->getNumValues() - 1);
Bill Wendling151d26d2010-06-23 18:16:24 +0000246
Evan Cheng302ef832010-06-10 02:09:31 +0000247 ++LoadsClustered;
248 }
249}
250
251/// ClusterNodes - Cluster certain nodes which should be scheduled together.
252///
253void ScheduleDAGSDNodes::ClusterNodes() {
Evan Chengc589e032010-01-22 03:36:51 +0000254 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
255 E = DAG->allnodes_end(); NI != E; ++NI) {
256 SDNode *Node = &*NI;
257 if (!Node || !Node->isMachineOpcode())
258 continue;
259
260 unsigned Opc = Node->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000261 const MCInstrDesc &MCID = TII->get(Opc);
262 if (MCID.mayLoad())
Evan Cheng302ef832010-06-10 02:09:31 +0000263 // Cluster loads from "near" addresses into combined SUnits.
264 ClusterNeighboringLoads(Node);
Evan Chengc589e032010-01-22 03:36:51 +0000265 }
266}
267
Dan Gohman343f0c02008-11-19 23:18:57 +0000268void ScheduleDAGSDNodes::BuildSchedUnits() {
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000269 // During scheduling, the NodeId field of SDNode is used to map SDNodes
270 // to their associated SUnits by holding SUnits table indices. A value
271 // of -1 means the SDNode does not yet have an associated SUnit.
272 unsigned NumNodes = 0;
273 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
274 E = DAG->allnodes_end(); NI != E; ++NI) {
275 NI->setNodeId(-1);
276 ++NumNodes;
277 }
278
Dan Gohman343f0c02008-11-19 23:18:57 +0000279 // Reserve entries in the vector for each of the SUnits we are creating. This
280 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
281 // invalidated.
Dan Gohman89b64bd2008-12-17 04:30:46 +0000282 // FIXME: Multiply by 2 because we may clone nodes during scheduling.
283 // This is a temporary workaround.
Dan Gohmane1dfc7d2008-12-23 17:24:50 +0000284 SUnits.reserve(NumNodes * 2);
Andrew Trickcd5af072011-02-03 23:00:17 +0000285
Chris Lattner736a6ea2010-02-24 06:11:37 +0000286 // Add all nodes in depth first order.
287 SmallVector<SDNode*, 64> Worklist;
288 SmallPtrSet<SDNode*, 64> Visited;
289 Worklist.push_back(DAG->getRoot().getNode());
290 Visited.insert(DAG->getRoot().getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000291
Evan Cheng554daa62011-04-26 21:31:35 +0000292 SmallVector<SUnit*, 8> CallSUnits;
Chris Lattner736a6ea2010-02-24 06:11:37 +0000293 while (!Worklist.empty()) {
294 SDNode *NI = Worklist.pop_back_val();
Andrew Trickcd5af072011-02-03 23:00:17 +0000295
Chris Lattner736a6ea2010-02-24 06:11:37 +0000296 // Add all operands to the worklist unless they've already been added.
297 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
298 if (Visited.insert(NI->getOperand(i).getNode()))
299 Worklist.push_back(NI->getOperand(i).getNode());
Andrew Trickcd5af072011-02-03 23:00:17 +0000300
Dan Gohman343f0c02008-11-19 23:18:57 +0000301 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
302 continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000303
Dan Gohman343f0c02008-11-19 23:18:57 +0000304 // If this node has already been processed, stop now.
305 if (NI->getNodeId() != -1) continue;
Andrew Trickcd5af072011-02-03 23:00:17 +0000306
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 SUnit *NodeSUnit = NewSUnit(NI);
Andrew Trickcd5af072011-02-03 23:00:17 +0000308
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000309 // See if anything is glued to this node, if so, add them to glued
310 // nodes. Nodes can have at most one glue input and one glue output. Glue
311 // is required to be the last operand and result of a node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000312
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000313 // Scan up to find glued preds.
Dan Gohman343f0c02008-11-19 23:18:57 +0000314 SDNode *N = NI;
Dan Gohmandb95fa12009-03-20 20:42:23 +0000315 while (N->getNumOperands() &&
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000316 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
Dan Gohmandb95fa12009-03-20 20:42:23 +0000317 N = N->getOperand(N->getNumOperands()-1).getNode();
318 assert(N->getNodeId() == -1 && "Node already inserted!");
319 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng8239daf2010-11-03 00:45:17 +0000320 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
321 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000322 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000323
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000324 // Scan down to find any glued succs.
Dan Gohman343f0c02008-11-19 23:18:57 +0000325 N = NI;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000326 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000327 SDValue GlueVal(N, N->getNumValues()-1);
Andrew Trickcd5af072011-02-03 23:00:17 +0000328
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000329 // There are either zero or one users of the Glue result.
330 bool HasGlueUse = false;
Andrew Trickcd5af072011-02-03 23:00:17 +0000331 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000332 UI != E; ++UI)
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000333 if (GlueVal.isOperandOf(*UI)) {
334 HasGlueUse = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000335 assert(N->getNodeId() == -1 && "Node already inserted!");
336 N->setNodeId(NodeSUnit->NodeNum);
337 N = *UI;
Evan Cheng8239daf2010-11-03 00:45:17 +0000338 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
339 NodeSUnit->isCall = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000340 break;
341 }
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000342 if (!HasGlueUse) break;
Dan Gohman343f0c02008-11-19 23:18:57 +0000343 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000344
Evan Cheng554daa62011-04-26 21:31:35 +0000345 if (NodeSUnit->isCall)
346 CallSUnits.push_back(NodeSUnit);
347
Andrew Trick12f0dc62011-04-14 05:15:06 +0000348 // Schedule zero-latency TokenFactor below any nodes that may increase the
349 // schedule height. Otherwise, ancestors of the TokenFactor may appear to
350 // have false stalls.
351 if (NI->getOpcode() == ISD::TokenFactor)
352 NodeSUnit->isScheduleLow = true;
353
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000354 // If there are glue operands involved, N is now the bottom-most node
355 // of the sequence of nodes that are glued together.
Dan Gohman343f0c02008-11-19 23:18:57 +0000356 // Update the SUnit.
357 NodeSUnit->setNode(N);
358 assert(N->getNodeId() == -1 && "Node already inserted!");
359 N->setNodeId(NodeSUnit->NodeNum);
360
Andrew Trick92e94662011-02-04 03:18:17 +0000361 // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
362 InitNumRegDefsLeft(NodeSUnit);
363
Dan Gohman787782f2008-11-21 01:44:51 +0000364 // Assign the Latency field of NodeSUnit using target-provided information.
Evan Chenge1631682010-05-19 22:42:23 +0000365 ComputeLatency(NodeSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000366 }
Evan Cheng554daa62011-04-26 21:31:35 +0000367
368 // Find all call operands.
369 while (!CallSUnits.empty()) {
370 SUnit *SU = CallSUnits.pop_back_val();
371 for (const SDNode *SUNode = SU->getNode(); SUNode;
372 SUNode = SUNode->getGluedNode()) {
373 if (SUNode->getOpcode() != ISD::CopyToReg)
374 continue;
375 SDNode *SrcN = SUNode->getOperand(2).getNode();
376 if (isPassiveNode(SrcN)) continue; // Not scheduled.
377 SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
378 SrcSU->isCallOp = true;
379 }
380 }
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000381}
382
383void ScheduleDAGSDNodes::AddSchedEdges() {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000384 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
David Goodwin71046162009-08-13 16:05:04 +0000385
David Goodwindc4bdcd2009-08-19 16:08:58 +0000386 // Check to see if the scheduler cares about latencies.
387 bool UnitLatencies = ForceUnitLatencies();
388
Dan Gohman343f0c02008-11-19 23:18:57 +0000389 // Pass 2: add the preds, succs, etc.
390 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
391 SUnit *SU = &SUnits[su];
392 SDNode *MainNode = SU->getNode();
Andrew Trickcd5af072011-02-03 23:00:17 +0000393
Dan Gohman343f0c02008-11-19 23:18:57 +0000394 if (MainNode->isMachineOpcode()) {
395 unsigned Opc = MainNode->getMachineOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +0000396 const MCInstrDesc &MCID = TII->get(Opc);
397 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
398 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000399 SU->isTwoAddress = true;
400 break;
401 }
402 }
Evan Chenge837dea2011-06-28 19:10:37 +0000403 if (MCID.isCommutable())
Dan Gohman343f0c02008-11-19 23:18:57 +0000404 SU->isCommutable = true;
405 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000406
Dan Gohman343f0c02008-11-19 23:18:57 +0000407 // Find all predecessors and successors of the group.
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000408 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000409 if (N->isMachineOpcode() &&
Dan Gohman39746672009-03-23 16:10:52 +0000410 TII->get(N->getMachineOpcode()).getImplicitDefs()) {
411 SU->hasPhysRegClobbers = true;
Dan Gohmanbcea8592009-10-10 01:32:21 +0000412 unsigned NumUsed = InstrEmitter::CountResults(N);
Dan Gohman8cccf0e2009-03-23 17:39:36 +0000413 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
414 --NumUsed; // Skip over unused values at the end.
415 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
Dan Gohman39746672009-03-23 16:10:52 +0000416 SU->hasPhysRegDefs = true;
417 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000418
Dan Gohman343f0c02008-11-19 23:18:57 +0000419 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
420 SDNode *OpN = N->getOperand(i).getNode();
421 if (isPassiveNode(OpN)) continue; // Not scheduled.
422 SUnit *OpSU = &SUnits[OpN->getNodeId()];
423 assert(OpSU && "Node has no SUnit!");
424 if (OpSU == SU) continue; // In the same group.
425
Owen Andersone50ed302009-08-10 22:56:29 +0000426 EVT OpVT = N->getOperand(i).getValueType();
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000427 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 bool isChain = OpVT == MVT::Other;
Dan Gohman343f0c02008-11-19 23:18:57 +0000429
430 unsigned PhysReg = 0;
Evan Chengc29a56d2009-01-12 03:19:55 +0000431 int Cost = 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000432 // Determine if this is a physical register dependency.
Evan Chengc29a56d2009-01-12 03:19:55 +0000433 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Dan Gohman54e4c362008-12-09 22:54:47 +0000434 assert((PhysReg == 0 || !isChain) &&
435 "Chain dependence via physreg data?");
Evan Chengc29a56d2009-01-12 03:19:55 +0000436 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
437 // emits a copy from the physical register to a virtual register unless
438 // it requires a cross class copy (cost < 0). That means we are only
439 // treating "expensive to copy" register dependency as physical register
440 // dependency. This may change in the future though.
Andrew Trick4cb971c2011-06-15 17:16:12 +0000441 if (Cost >= 0 && !StressSched)
Evan Chengc29a56d2009-01-12 03:19:55 +0000442 PhysReg = 0;
David Goodwin71046162009-08-13 16:05:04 +0000443
Evan Cheng046fa3f2010-05-28 23:26:21 +0000444 // If this is a ctrl dep, latency is 1.
Andrew Trickc558bf32011-04-12 20:14:07 +0000445 unsigned OpLatency = isChain ? 1 : OpSU->Latency;
Andrew Trick87896d92011-04-13 00:38:32 +0000446 // Special-case TokenFactor chains as zero-latency.
447 if(isChain && OpN->getOpcode() == ISD::TokenFactor)
448 OpLatency = 0;
449
Evan Cheng046fa3f2010-05-28 23:26:21 +0000450 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
451 OpLatency, PhysReg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000452 if (!isChain && !UnitLatencies) {
Evan Cheng15a16de2010-05-20 06:13:19 +0000453 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
Dan Gohman3fb150a2010-04-17 17:42:52 +0000454 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
David Goodwindc4bdcd2009-08-19 16:08:58 +0000455 }
David Goodwin71046162009-08-13 16:05:04 +0000456
Andrew Trick4bbf4672011-03-09 19:12:43 +0000457 if (!SU->addPred(dep) && !dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
Andrew Trick92e94662011-02-04 03:18:17 +0000458 // Multiple register uses are combined in the same SUnit. For example,
459 // we could have a set of glued nodes with all their defs consumed by
460 // another set of glued nodes. Register pressure tracking sees this as
461 // a single use, so to keep pressure balanced we reduce the defs.
Andrew Trick4bbf4672011-03-09 19:12:43 +0000462 //
463 // We can't tell (without more book-keeping) if this results from
464 // glued nodes or duplicate operands. As long as we don't reduce
465 // NumRegDefsLeft to zero, we handle the common cases well.
Andrew Trick92e94662011-02-04 03:18:17 +0000466 --OpSU->NumRegDefsLeft;
467 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000468 }
469 }
470 }
471}
472
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000473/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
474/// are input. This SUnit graph is similar to the SelectionDAG, but
475/// excludes nodes that aren't interesting to scheduling, and represents
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000476/// glued together nodes with a single SUnit.
Dan Gohman98976e42009-10-09 23:33:48 +0000477void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
Evan Cheng302ef832010-06-10 02:09:31 +0000478 // Cluster certain nodes which should be scheduled together.
479 ClusterNodes();
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000480 // Populate the SUnits array.
481 BuildSchedUnits();
482 // Compute all the scheduling dependencies between nodes.
483 AddSchedEdges();
484}
485
Andrew Trick92e94662011-02-04 03:18:17 +0000486// Initialize NumNodeDefs for the current Node's opcode.
487void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
Eric Christopher29449442011-03-08 19:35:47 +0000488 // Check for phys reg copy.
489 if (!Node)
490 return;
491
Andrew Trick92e94662011-02-04 03:18:17 +0000492 if (!Node->isMachineOpcode()) {
493 if (Node->getOpcode() == ISD::CopyFromReg)
494 NodeNumDefs = 1;
495 else
496 NodeNumDefs = 0;
497 return;
498 }
499 unsigned POpc = Node->getMachineOpcode();
500 if (POpc == TargetOpcode::IMPLICIT_DEF) {
501 // No register need be allocated for this.
502 NodeNumDefs = 0;
503 return;
504 }
505 unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
506 // Some instructions define regs that are not represented in the selection DAG
507 // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
508 NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
509 DefIdx = 0;
510}
511
512// Construct a RegDefIter for this SUnit and find the first valid value.
513ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
514 const ScheduleDAGSDNodes *SD)
515 : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
516 InitNodeNumDefs();
517 Advance();
518}
519
520// Advance to the next valid value defined by the SUnit.
521void ScheduleDAGSDNodes::RegDefIter::Advance() {
522 for (;Node;) { // Visit all glued nodes.
523 for (;DefIdx < NodeNumDefs; ++DefIdx) {
524 if (!Node->hasAnyUseOfValue(DefIdx))
525 continue;
Andrew Trick4ef4c172011-06-27 18:01:20 +0000526 ValueType = Node->getValueType(DefIdx);
Andrew Trick92e94662011-02-04 03:18:17 +0000527 ++DefIdx;
528 return; // Found a normal regdef.
529 }
530 Node = Node->getGluedNode();
531 if (Node == NULL) {
532 return; // No values left to visit.
533 }
534 InitNodeNumDefs();
535 }
536}
537
538void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
539 assert(SU->NumRegDefsLeft == 0 && "expect a new node");
540 for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
541 assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
542 ++SU->NumRegDefsLeft;
543 }
544}
545
Dan Gohman343f0c02008-11-19 23:18:57 +0000546void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
Andrew Trick87896d92011-04-13 00:38:32 +0000547 SDNode *N = SU->getNode();
548
549 // TokenFactor operands are considered zero latency, and some schedulers
550 // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
551 // whenever node latency is nonzero.
552 if (N && N->getOpcode() == ISD::TokenFactor) {
553 SU->Latency = 0;
554 return;
555 }
556
Evan Chenge1631682010-05-19 22:42:23 +0000557 // Check to see if the scheduler cares about latencies.
558 if (ForceUnitLatencies()) {
559 SU->Latency = 1;
560 return;
561 }
562
Evan Cheng3ef1c872010-09-10 01:29:16 +0000563 if (!InstrItins || InstrItins->isEmpty()) {
Andrew Trick5e84e3c2011-03-05 09:18:16 +0000564 if (N && N->isMachineOpcode() &&
565 TII->isHighLatencyDef(N->getMachineOpcode()))
Andrew Tricke0ef5092011-03-05 08:00:22 +0000566 SU->Latency = HighLatencyCycles;
567 else
568 SU->Latency = 1;
Evan Cheng15a16de2010-05-20 06:13:19 +0000569 return;
570 }
Andrew Trickcd5af072011-02-03 23:00:17 +0000571
Dan Gohman343f0c02008-11-19 23:18:57 +0000572 // Compute the latency for the node. We use the sum of the latencies for
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000573 // all nodes glued together into this SUnit.
Dan Gohman343f0c02008-11-19 23:18:57 +0000574 SU->Latency = 0;
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000575 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
Evan Cheng8239daf2010-11-03 00:45:17 +0000576 if (N->isMachineOpcode())
577 SU->Latency += TII->getInstrLatency(InstrItins, N);
Dan Gohman343f0c02008-11-19 23:18:57 +0000578}
579
Evan Cheng15a16de2010-05-20 06:13:19 +0000580void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
581 unsigned OpIdx, SDep& dep) const{
582 // Check to see if the scheduler cares about latencies.
583 if (ForceUnitLatencies())
584 return;
585
Evan Cheng15a16de2010-05-20 06:13:19 +0000586 if (dep.getKind() != SDep::Data)
587 return;
588
589 unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
Evan Cheng7e2fe912010-10-28 06:47:08 +0000590 if (Use->isMachineOpcode())
591 // Adjust the use operand index by num of defs.
592 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
Evan Chenga0792de2010-10-06 06:27:31 +0000593 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
Evan Cheng08975152010-10-29 18:09:28 +0000594 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
595 !BB->succ_empty()) {
596 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
597 if (TargetRegisterInfo::isVirtualRegister(Reg))
598 // This copy is a liveout value. It is likely coalesced, so reduce the
599 // latency so not to penalize the def.
600 // FIXME: need target specific adjustment here?
601 Latency = (Latency > 1) ? Latency - 1 : 1;
602 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000603 if (Latency >= 0)
604 dep.setLatency(Latency);
Evan Cheng15a16de2010-05-20 06:13:19 +0000605}
606
Dan Gohman343f0c02008-11-19 23:18:57 +0000607void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
Evan Chengc29a56d2009-01-12 03:19:55 +0000608 if (!SU->getNode()) {
David Greene84fa8222010-01-05 01:25:11 +0000609 dbgs() << "PHYS REG COPY\n";
Evan Chengc29a56d2009-01-12 03:19:55 +0000610 return;
611 }
612
613 SU->getNode()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000614 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000615 SmallVector<SDNode *, 4> GluedNodes;
616 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
617 GluedNodes.push_back(N);
618 while (!GluedNodes.empty()) {
David Greene84fa8222010-01-05 01:25:11 +0000619 dbgs() << " ";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000620 GluedNodes.back()->dump(DAG);
David Greene84fa8222010-01-05 01:25:11 +0000621 dbgs() << "\n";
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000622 GluedNodes.pop_back();
Dan Gohman343f0c02008-11-19 23:18:57 +0000623 }
624}
Dan Gohmanbcea8592009-10-10 01:32:21 +0000625
Andrew Trick73ba69b2012-03-07 05:21:40 +0000626void ScheduleDAGSDNodes::dumpSchedule() const {
627 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
628 if (SUnit *SU = Sequence[i])
629 SU->dump(this);
630 else
631 dbgs() << "**** NOOP ****\n";
632 }
633}
634
Andrew Trick4c727202012-03-07 05:21:36 +0000635#ifndef NDEBUG
636/// VerifyScheduledSequence - Verify that all SUnits were scheduled and that
637/// their state is consistent with the nodes listed in Sequence.
638///
639void ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) {
640 unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp);
641 unsigned Noops = 0;
642 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
643 if (!Sequence[i])
644 ++Noops;
645 assert(Sequence.size() - Noops == ScheduledNodes &&
646 "The number of nodes scheduled doesn't match the expected number!");
647}
648#endif // NDEBUG
649
Evan Chengbfcb3052010-03-25 01:38:16 +0000650namespace {
651 struct OrderSorter {
652 bool operator()(const std::pair<unsigned, MachineInstr*> &A,
653 const std::pair<unsigned, MachineInstr*> &B) {
654 return A.first < B.first;
655 }
656 };
657}
658
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000659/// ProcessSDDbgValues - Process SDDbgValues associated with this node.
Andrew Trickcd5af072011-02-03 23:00:17 +0000660static void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
Devang Patel55d20e82011-01-26 18:20:04 +0000661 InstrEmitter &Emitter,
662 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
663 DenseMap<SDValue, unsigned> &VRBaseMap,
664 unsigned Order) {
665 if (!N->getHasDebugValue())
666 return;
667
668 // Opportunistically insert immediate dbg_value uses, i.e. those with source
669 // order number right after the N.
670 MachineBasicBlock *BB = Emitter.getBlock();
671 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000672 ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N);
Devang Patel55d20e82011-01-26 18:20:04 +0000673 for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
674 if (DVs[i]->isInvalidated())
675 continue;
676 unsigned DVOrder = DVs[i]->getOrder();
677 if (!Order || DVOrder == ++Order) {
678 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
679 if (DbgMI) {
680 Orders.push_back(std::make_pair(DVOrder, DbgMI));
681 BB->insert(InsertPos, DbgMI);
682 }
683 DVs[i]->setIsInvalidated();
684 }
685 }
686}
687
Evan Chengbfcb3052010-03-25 01:38:16 +0000688// ProcessSourceNode - Process nodes with source order numbers. These are added
Jim Grosbachd27946d2010-06-30 21:27:56 +0000689// to a vector which EmitSchedule uses to determine how to insert dbg_value
Evan Chengbfcb3052010-03-25 01:38:16 +0000690// instructions in the right order.
691static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
692 InstrEmitter &Emitter,
Evan Chengbfcb3052010-03-25 01:38:16 +0000693 DenseMap<SDValue, unsigned> &VRBaseMap,
694 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
695 SmallSet<unsigned, 8> &Seen) {
696 unsigned Order = DAG->GetOrdering(N);
Devang Patel39078a82011-01-27 00:13:27 +0000697 if (!Order || !Seen.insert(Order)) {
698 // Process any valid SDDbgValues even if node does not have any order
699 // assigned.
700 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
Evan Chengbfcb3052010-03-25 01:38:16 +0000701 return;
Devang Patel39078a82011-01-27 00:13:27 +0000702 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000703
704 MachineBasicBlock *BB = Emitter.getBlock();
Dan Gohman84023e02010-07-10 09:00:22 +0000705 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
Evan Chengbfcb3052010-03-25 01:38:16 +0000706 // Did not insert any instruction.
707 Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
708 return;
709 }
710
Dan Gohman84023e02010-07-10 09:00:22 +0000711 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
Devang Patel55d20e82011-01-26 18:20:04 +0000712 ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
Evan Chengbfcb3052010-03-25 01:38:16 +0000713}
714
Andrew Trick84b454d2012-03-07 05:21:44 +0000715void ScheduleDAGSDNodes::
716EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap,
717 MachineBasicBlock::iterator InsertPos) {
718 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
719 I != E; ++I) {
720 if (I->isCtrl()) continue; // ignore chain preds
721 if (I->getSUnit()->CopyDstRC) {
722 // Copy to physical register.
723 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->getSUnit());
724 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
725 // Find the destination physical register.
726 unsigned Reg = 0;
727 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
728 EE = SU->Succs.end(); II != EE; ++II) {
729 if (II->isCtrl()) continue; // ignore chain preds
730 if (II->getReg()) {
731 Reg = II->getReg();
732 break;
733 }
734 }
735 BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg)
736 .addReg(VRI->second);
737 } else {
738 // Copy from physical register.
739 assert(I->getReg() && "Unknown physical register!");
740 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
741 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
742 (void)isNew; // Silence compiler warning.
743 assert(isNew && "Node emitted out of order - early");
744 BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase)
745 .addReg(I->getReg());
746 }
747 break;
748 }
749}
Evan Chengbfcb3052010-03-25 01:38:16 +0000750
Andrew Trick84b454d2012-03-07 05:21:44 +0000751/// EmitSchedule - Emit the machine code in scheduled order. Return the new
752/// InsertPos and MachineBasicBlock that contains this insertion
753/// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does
754/// not necessarily refer to returned BB. The emitter may split blocks.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000755MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohmanbcea8592009-10-10 01:32:21 +0000756 InstrEmitter Emitter(BB, InsertPos);
757 DenseMap<SDValue, unsigned> VRBaseMap;
758 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chengbfcb3052010-03-25 01:38:16 +0000759 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
760 SmallSet<unsigned, 8> Seen;
761 bool HasDbg = DAG->hasDebugValues();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000762
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000763 // If this is the first BB, emit byval parameter dbg_value's.
764 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
765 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
766 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
767 for (; PDI != PDE; ++PDI) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000768 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000769 if (DbgMI)
Dan Gohman84023e02010-07-10 09:00:22 +0000770 BB->insert(InsertPos, DbgMI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000771 }
772 }
773
Dan Gohmanbcea8592009-10-10 01:32:21 +0000774 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
775 SUnit *SU = Sequence[i];
776 if (!SU) {
777 // Null SUnit* is a noop.
Andrew Trick84b454d2012-03-07 05:21:44 +0000778 TII->insertNoop(*Emitter.getBlock(), InsertPos);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000779 continue;
780 }
781
782 // For pre-regalloc scheduling, create instructions corresponding to the
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000783 // SDNode and any glued SDNodes and append them to the block.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000784 if (!SU->getNode()) {
785 // Emit a copy.
Andrew Trick84b454d2012-03-07 05:21:44 +0000786 EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000787 continue;
788 }
789
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000790 SmallVector<SDNode *, 4> GluedNodes;
791 for (SDNode *N = SU->getNode()->getGluedNode(); N;
792 N = N->getGluedNode())
793 GluedNodes.push_back(N);
794 while (!GluedNodes.empty()) {
795 SDNode *N = GluedNodes.back();
796 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000797 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000798 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000799 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000800 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000801 GluedNodes.pop_back();
Dan Gohmanbcea8592009-10-10 01:32:21 +0000802 }
803 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000804 VRBaseMap);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000805 // Remember the source order of the inserted instruction.
Evan Chengbfcb3052010-03-25 01:38:16 +0000806 if (HasDbg)
Dan Gohman891ff8f2010-04-30 19:35:33 +0000807 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
Evan Chengbfcb3052010-03-25 01:38:16 +0000808 Seen);
809 }
810
Dale Johannesenfdb42fa2010-04-26 20:06:49 +0000811 // Insert all the dbg_values which have not already been inserted in source
Evan Chengbfcb3052010-03-25 01:38:16 +0000812 // order sequence.
813 if (HasDbg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000814 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
Evan Chengbfcb3052010-03-25 01:38:16 +0000815
816 // Sort the source order instructions and use the order to insert debug
817 // values.
818 std::sort(Orders.begin(), Orders.end(), OrderSorter());
819
820 SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
821 SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
822 // Now emit the rest according to source order.
823 unsigned LastOrder = 0;
Evan Chengbfcb3052010-03-25 01:38:16 +0000824 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
825 unsigned Order = Orders[i].first;
826 MachineInstr *MI = Orders[i].second;
827 // Insert all SDDbgValue's whose order(s) are before "Order".
828 if (!MI)
829 continue;
Evan Chengbfcb3052010-03-25 01:38:16 +0000830 for (; DI != DE &&
831 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
832 if ((*DI)->isInvalidated())
833 continue;
Dan Gohman891ff8f2010-04-30 19:35:33 +0000834 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000835 if (DbgMI) {
836 if (!LastOrder)
837 // Insert to start of the BB (after PHIs).
838 BB->insert(BBBegin, DbgMI);
839 else {
Dan Gohmana8dab362010-07-10 22:42:31 +0000840 // Insert at the instruction, which may be in a different
841 // block, if the block was split by a custom inserter.
Evan Cheng962021b2010-04-26 07:38:55 +0000842 MachineBasicBlock::iterator Pos = MI;
Dan Gohmana8dab362010-07-10 22:42:31 +0000843 MI->getParent()->insert(llvm::next(Pos), DbgMI);
Evan Cheng962021b2010-04-26 07:38:55 +0000844 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000845 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000846 }
Evan Chengbfcb3052010-03-25 01:38:16 +0000847 LastOrder = Order;
Evan Chengbfcb3052010-03-25 01:38:16 +0000848 }
849 // Add trailing DbgValue's before the terminator. FIXME: May want to add
850 // some of them before one or more conditional branches?
851 while (DI != DE) {
852 MachineBasicBlock *InsertBB = Emitter.getBlock();
853 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
854 if (!(*DI)->isInvalidated()) {
Dan Gohman891ff8f2010-04-30 19:35:33 +0000855 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
Evan Cheng962021b2010-04-26 07:38:55 +0000856 if (DbgMI)
857 InsertBB->insert(Pos, DbgMI);
Evan Chengbfcb3052010-03-25 01:38:16 +0000858 }
859 ++DI;
860 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000861 }
862
863 BB = Emitter.getBlock();
864 InsertPos = Emitter.getInsertPos();
865 return BB;
866}
Andrew Trick56b94c52012-03-07 00:18:22 +0000867
868/// Return the basic block label.
869std::string ScheduleDAGSDNodes::getDAGName() const {
870 return "sunit-dag." + BB->getFullName();
871}