blob: 45237756a8d0e4f6cfecbf943dcd82a61aa149e1 [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
Chris Lattnerd32b2362005-08-18 18:45:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerd32b2362005-08-18 18:45:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskeye6b90fb2005-09-26 21:57:04 +000010// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
Chris Lattnerd32b2362005-08-18 18:45:24 +000013//
14//===----------------------------------------------------------------------===//
15
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000016#define DEBUG_TYPE "pre-RA-sched"
Nate Begemane1795842008-02-14 08:57:00 +000017#include "llvm/Constants.h"
Reid Spencere5530da2007-01-12 23:31:12 +000018#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000026#include "llvm/Target/TargetLowering.h"
Evan Cheng643afa52008-02-28 07:40:24 +000027#include "llvm/ADT/Statistic.h"
Evan Cheng9e233362008-03-12 22:19:41 +000028#include "llvm/Support/CommandLine.h"
Evan Chenge165a782006-05-11 23:55:42 +000029#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000030#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000031using namespace llvm;
32
Evan Cheng643afa52008-02-28 07:40:24 +000033STATISTIC(NumCommutes, "Number of instructions commuted");
34
Evan Cheng9e233362008-03-12 22:19:41 +000035namespace {
36 static cl::opt<bool>
37 SchedLiveInCopies("schedule-livein-copies",
38 cl::desc("Schedule copies of livein registers"),
39 cl::init(false));
40}
41
Chris Lattner84bc5422007-12-31 04:13:23 +000042ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
43 const TargetMachine &tm)
Evan Cheng9e233362008-03-12 22:19:41 +000044 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
Chris Lattner84bc5422007-12-31 04:13:23 +000045 TII = TM.getInstrInfo();
Evan Cheng6b2cf282008-01-30 19:35:32 +000046 MF = &DAG.getMachineFunction();
Dan Gohman6f0d0242008-02-10 18:45:23 +000047 TRI = TM.getRegisterInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +000048 ConstPool = BB->getParent()->getConstantPool();
49}
Evan Chenga6fb1b62007-09-25 01:54:36 +000050
Evan Chenga6fb1b62007-09-25 01:54:36 +000051/// CheckForPhysRegDependency - Check if the dependency between def and use of
52/// a specified operand is a physical register dependency. If so, returns the
53/// register and the cost of copying the register.
54static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000055 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000056 const TargetInstrInfo *TII,
57 unsigned &PhysReg, int &Cost) {
58 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
59 return;
60
61 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000062 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000063 return;
64
65 unsigned ResNo = Use->getOperand(2).ResNo;
66 if (Def->isTargetOpcode()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000067 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000068 if (ResNo >= II.getNumDefs() &&
69 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000070 PhysReg = Reg;
71 const TargetRegisterClass *RC =
Evan Cheng676dd7c2008-03-11 07:19:34 +000072 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Chenga6fb1b62007-09-25 01:54:36 +000073 Cost = RC->getCopyCost();
74 }
75 }
76}
77
78SUnit *ScheduleDAG::Clone(SUnit *Old) {
79 SUnit *SU = NewSUnit(Old->Node);
Dan Gohman45f36ea2008-03-10 23:48:14 +000080 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Chenga6fb1b62007-09-25 01:54:36 +000081 SU->InstanceNo = SUnitMap[Old->Node].size();
82 SU->Latency = Old->Latency;
83 SU->isTwoAddress = Old->isTwoAddress;
84 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000085 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000086 SUnitMap[Old->Node].push_back(SU);
87 return SU;
88}
89
Evan Chengf10c9732007-10-05 01:39:18 +000090
Evan Chenge165a782006-05-11 23:55:42 +000091/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
92/// This SUnit graph is similar to the SelectionDAG, but represents flagged
93/// together nodes with a single SUnit.
94void ScheduleDAG::BuildSchedUnits() {
95 // Reserve entries in the vector for each of the SUnits we are creating. This
96 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
97 // invalidated.
98 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
99
Evan Chenge165a782006-05-11 23:55:42 +0000100 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
101 E = DAG.allnodes_end(); NI != E; ++NI) {
102 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
103 continue;
104
105 // If this node has already been processed, stop now.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000106 if (SUnitMap[NI].size()) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000107
108 SUnit *NodeSUnit = NewSUnit(NI);
109
110 // See if anything is flagged to this node, if so, add them to flagged
111 // nodes. Nodes can have at most one flag input and one flag output. Flags
112 // are required the be the last operand and result of a node.
113
114 // Scan up, adding flagged preds to FlaggedNodes.
115 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000116 if (N->getNumOperands() &&
117 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
118 do {
119 N = N->getOperand(N->getNumOperands()-1).Val;
120 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000121 SUnitMap[N].push_back(NodeSUnit);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000122 } while (N->getNumOperands() &&
123 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
124 std::reverse(NodeSUnit->FlaggedNodes.begin(),
125 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000126 }
127
128 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
129 // have a user of the flag operand.
130 N = NI;
131 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
132 SDOperand FlagVal(N, N->getNumValues()-1);
133
134 // There are either zero or one users of the Flag result.
135 bool HasFlagUse = false;
136 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
137 UI != E; ++UI)
Evan Cheng917be682008-03-04 00:41:45 +0000138 if (FlagVal.isOperandOf(*UI)) {
Evan Chenge165a782006-05-11 23:55:42 +0000139 HasFlagUse = true;
140 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000141 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000142 N = *UI;
143 break;
144 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000145 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000146 }
147
148 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
149 // Update the SUnit
150 NodeSUnit->Node = N;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000151 SUnitMap[N].push_back(NodeSUnit);
Evan Chengf10c9732007-10-05 01:39:18 +0000152
153 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000154 }
155
156 // Pass 2: add the preds, succs, etc.
157 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
158 SUnit *SU = &SUnits[su];
159 SDNode *MainNode = SU->Node;
160
161 if (MainNode->isTargetOpcode()) {
162 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000163 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000164 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000165 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000166 SU->isTwoAddress = true;
167 break;
168 }
169 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000170 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000171 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000172 }
173
174 // Find all predecessors and successors of the group.
175 // Temporarily add N to make code simpler.
176 SU->FlaggedNodes.push_back(MainNode);
177
178 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
179 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000180 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000181 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
182 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000183 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000184
185 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
186 SDNode *OpN = N->getOperand(i).Val;
187 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000188 SUnit *OpSU = SUnitMap[OpN].front();
Evan Chenge165a782006-05-11 23:55:42 +0000189 assert(OpSU && "Node has no SUnit!");
190 if (OpSU == SU) continue; // In the same group.
191
192 MVT::ValueType OpVT = N->getOperand(i).getValueType();
193 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
194 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000195
196 unsigned PhysReg = 0;
197 int Cost = 1;
198 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000199 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000200 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000201 }
202 }
203
204 // Remove MainNode from FlaggedNodes again.
205 SU->FlaggedNodes.pop_back();
206 }
207
208 return;
209}
210
Evan Chengf10c9732007-10-05 01:39:18 +0000211void ScheduleDAG::ComputeLatency(SUnit *SU) {
212 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
213
214 // Compute the latency for the node. We use the sum of the latencies for
215 // all nodes flagged together into this SUnit.
216 if (InstrItins.isEmpty()) {
217 // No latency information.
218 SU->Latency = 1;
219 } else {
220 SU->Latency = 0;
221 if (SU->Node->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000222 unsigned SchedClass =
223 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Evan Chengf10c9732007-10-05 01:39:18 +0000224 InstrStage *S = InstrItins.begin(SchedClass);
225 InstrStage *E = InstrItins.end(SchedClass);
226 for (; S != E; ++S)
227 SU->Latency += S->Cycles;
228 }
229 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
230 SDNode *FNode = SU->FlaggedNodes[i];
231 if (FNode->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000232 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Evan Chengf10c9732007-10-05 01:39:18 +0000233 InstrStage *S = InstrItins.begin(SchedClass);
234 InstrStage *E = InstrItins.end(SchedClass);
235 for (; S != E; ++S)
236 SU->Latency += S->Cycles;
237 }
238 }
239 }
240}
241
Roman Levensteind86449e2008-03-04 11:19:43 +0000242/// CalculateDepths - compute depths using algorithms for the longest
243/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000244void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000245 unsigned DAGSize = SUnits.size();
246 std::vector<unsigned> InDegree(DAGSize);
247 std::vector<SUnit*> WorkList;
248 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000249
Roman Levensteind86449e2008-03-04 11:19:43 +0000250 // Initialize the data structures
251 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
252 SUnit *SU = &SUnits[i];
253 int NodeNum = SU->NodeNum;
254 unsigned Degree = SU->Preds.size();
255 InDegree[NodeNum] = Degree;
256 SU->Depth = 0;
257
258 // Is it a node without dependencies?
259 if (Degree == 0) {
260 assert(SU->Preds.empty() && "SUnit should have no predecessors");
261 // Collect leaf nodes
262 WorkList.push_back(SU);
263 }
264 }
265
266 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000267 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000268 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000269 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000270 unsigned &SUDepth = SU->Depth;
271
272 // Use dynamic programming:
273 // When current node is being processed, all of its dependencies
274 // are already processed.
275 // So, just iterate over all predecessors and take the longest path
276 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
277 I != E; ++I) {
278 unsigned PredDepth = I->Dep->Depth;
279 if (PredDepth+1 > SUDepth) {
280 SUDepth = PredDepth + 1;
281 }
282 }
283
284 // Update InDegrees of all nodes depending on current SUnit
285 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
286 I != E; ++I) {
287 SUnit *SU = I->Dep;
288 if (!--InDegree[SU->NodeNum])
289 // If all dependencies of the node are processed already,
290 // then the longest path for the node can be computed now
291 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000292 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000293 }
Evan Chenge165a782006-05-11 23:55:42 +0000294}
Evan Cheng99126282007-07-06 01:37:28 +0000295
Roman Levensteind86449e2008-03-04 11:19:43 +0000296/// CalculateHeights - compute heights using algorithms for the longest
297/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000298void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000299 unsigned DAGSize = SUnits.size();
300 std::vector<unsigned> InDegree(DAGSize);
301 std::vector<SUnit*> WorkList;
302 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000303
Roman Levensteind86449e2008-03-04 11:19:43 +0000304 // Initialize the data structures
305 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
306 SUnit *SU = &SUnits[i];
307 int NodeNum = SU->NodeNum;
308 unsigned Degree = SU->Succs.size();
309 InDegree[NodeNum] = Degree;
310 SU->Height = 0;
311
312 // Is it a node without dependencies?
313 if (Degree == 0) {
314 assert(SU->Succs.empty() && "Something wrong");
315 assert(WorkList.empty() && "Should be empty");
316 // Collect leaf nodes
317 WorkList.push_back(SU);
318 }
319 }
320
321 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000322 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000323 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000324 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000325 unsigned &SUHeight = SU->Height;
326
327 // Use dynamic programming:
328 // When current node is being processed, all of its dependencies
329 // are already processed.
330 // So, just iterate over all successors and take the longest path
331 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
332 I != E; ++I) {
333 unsigned SuccHeight = I->Dep->Height;
334 if (SuccHeight+1 > SUHeight) {
335 SUHeight = SuccHeight + 1;
336 }
337 }
338
339 // Update InDegrees of all nodes depending on current SUnit
340 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
341 I != E; ++I) {
342 SUnit *SU = I->Dep;
343 if (!--InDegree[SU->NodeNum])
344 // If all dependencies of the node are processed already,
345 // then the longest path for the node can be computed now
346 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000347 }
348 }
Evan Chenge165a782006-05-11 23:55:42 +0000349}
350
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000351/// CountResults - The results of target nodes have register or immediate
352/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000353/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000354unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000355 unsigned N = Node->getNumValues();
356 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000357 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000358 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000359 --N; // Skip over chain result.
360 return N;
361}
362
Dan Gohman69de1932008-02-06 22:27:42 +0000363/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000364/// followed by special operands that describe memory references, then an
365/// optional chain operand, then flag operands. Compute the number of
366/// actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000367unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000368 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohmancc20cd52008-02-11 19:00:34 +0000369 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman69de1932008-02-06 22:27:42 +0000370 --N; // Ignore MemOperand nodes
371 return N;
372}
373
Dan Gohman42a77882008-02-16 00:36:48 +0000374/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
375/// operand
376unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000377 unsigned N = Node->getNumOperands();
378 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
379 --N;
380 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
381 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000382 return N;
383}
384
Jim Laskey60f09922006-07-21 20:57:35 +0000385static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman6f0d0242008-02-10 18:45:23 +0000386 const TargetRegisterInfo *TRI,
Jim Laskey60f09922006-07-21 20:57:35 +0000387 const TargetInstrInfo *TII,
Chris Lattner749c6f62008-01-07 07:27:27 +0000388 const TargetInstrDesc &II,
Jim Laskey60f09922006-07-21 20:57:35 +0000389 unsigned Op) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000390 if (Op >= II.getNumOperands()) {
391 assert(II.isVariadic() && "Invalid operand # of instruction");
Jim Laskey60f09922006-07-21 20:57:35 +0000392 return NULL;
393 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000394 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattner8ca5c672008-01-07 02:39:19 +0000395 return TII->getPointerRegClass();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000396 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Jim Laskey60f09922006-07-21 20:57:35 +0000397}
398
Evan Chenga6fb1b62007-09-25 01:54:36 +0000399void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
400 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng84097472007-08-02 00:28:15 +0000401 DenseMap<SDOperand, unsigned> &VRBaseMap) {
402 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000403 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000404 // Just use the input register directly!
Evan Chenga6fb1b62007-09-25 01:54:36 +0000405 if (InstanceNo > 0)
406 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000407 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
408 assert(isNew && "Node emitted out of order - early");
409 return;
410 }
411
412 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
413 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000414 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000415 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
416 UI != E; ++UI) {
417 SDNode *Use = *UI;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000418 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000419 if (Use->getOpcode() == ISD::CopyToReg &&
420 Use->getOperand(2).Val == Node &&
421 Use->getOperand(2).ResNo == ResNo) {
422 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000423 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000424 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000425 Match = false;
426 } else if (DestReg != SrcReg)
427 Match = false;
428 } else {
429 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
430 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000431 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000432 continue;
433 MVT::ValueType VT = Node->getValueType(Op.ResNo);
434 if (VT != MVT::Other && VT != MVT::Flag)
435 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000436 }
437 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000438 MatchReg &= Match;
439 if (VRBase)
440 break;
Evan Cheng84097472007-08-02 00:28:15 +0000441 }
442
Chris Lattner02b6d252008-03-09 08:49:15 +0000443 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000444 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000445
Evan Chenga6fb1b62007-09-25 01:54:36 +0000446 // Figure out the register class to create for the destreg.
Chris Lattner02b6d252008-03-09 08:49:15 +0000447 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000448 DstRC = MRI.getRegClass(VRBase);
Chris Lattner02b6d252008-03-09 08:49:15 +0000449 } else {
450 DstRC = DAG.getTargetLoweringInfo()
451 .getRegClassFor(Node->getValueType(ResNo));
452 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000453
454 // If all uses are reading from the src physical register and copying the
455 // register is either impossible or very expensive, then don't create a copy.
Chris Lattner02b6d252008-03-09 08:49:15 +0000456 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000457 VRBase = SrcReg;
458 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000459 // Create the reg, emit the copy.
Evan Cheng9e233362008-03-12 22:19:41 +0000460 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattner02b6d252008-03-09 08:49:15 +0000461 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng84097472007-08-02 00:28:15 +0000462 }
Evan Cheng84097472007-08-02 00:28:15 +0000463
Evan Chenga6fb1b62007-09-25 01:54:36 +0000464 if (InstanceNo > 0)
465 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000466 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
467 assert(isNew && "Node emitted out of order - early");
468}
469
470void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
471 MachineInstr *MI,
Chris Lattner749c6f62008-01-07 07:27:27 +0000472 const TargetInstrDesc &II,
Evan Cheng84097472007-08-02 00:28:15 +0000473 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner349c4952008-01-07 03:13:06 +0000474 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000475 // If the specific node value is only used by a CopyToReg and the dest reg
476 // is a vreg, use the CopyToReg'd destination register instead of creating
477 // a new vreg.
478 unsigned VRBase = 0;
479 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
480 UI != E; ++UI) {
481 SDNode *Use = *UI;
482 if (Use->getOpcode() == ISD::CopyToReg &&
483 Use->getOperand(2).Val == Node &&
484 Use->getOperand(2).ResNo == i) {
485 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000486 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000487 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000488 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000489 break;
490 }
491 }
492 }
493
Evan Cheng84097472007-08-02 00:28:15 +0000494 // Create the result registers for this node and add the result regs to
495 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000496 if (VRBase == 0) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000497 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000498 assert(RC && "Isn't a register operand!");
Evan Cheng9e233362008-03-12 22:19:41 +0000499 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000500 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000501 }
502
503 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
504 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000505 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000506}
507
Chris Lattnerdf375062006-03-10 07:25:12 +0000508/// getVR - Return the virtual register corresponding to the specified result
509/// of the specified node.
Evan Chengaf825c82007-07-10 07:08:32 +0000510static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
511 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000512 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000513 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000514}
515
516
Chris Lattnered18b682006-02-24 18:54:03 +0000517/// AddOperand - Add the specified operand to the specified machine instr. II
518/// specifies the instruction information for the node, and IIOpNum is the
519/// operand number (in the II) that we are adding. IIOpNum and II are used for
520/// assertions only.
521void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
522 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000523 const TargetInstrDesc *II,
Evan Chengaf825c82007-07-10 07:08:32 +0000524 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000525 if (Op.isTargetOpcode()) {
526 // Note that this case is redundant with the final else block, but we
527 // include it because it is the most common and it makes the logic
528 // simpler here.
529 assert(Op.getValueType() != MVT::Other &&
530 Op.getValueType() != MVT::Flag &&
531 "Chain and flag operands should occur at end of operand list!");
532
533 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000534 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000535 const TargetInstrDesc &TID = MI->getDesc();
536 bool isOptDef = (IIOpNum < TID.getNumOperands())
537 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner8019f412007-12-30 00:41:17 +0000538 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000539
540 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000541 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerb7795802008-03-11 00:59:28 +0000542#ifndef NDEBUG
Chris Lattnered18b682006-02-24 18:54:03 +0000543 if (II) {
Chris Lattnerb7795802008-03-11 00:59:28 +0000544 // There may be no register class for this operand if it is a variadic
545 // argument (RC will be NULL in this case). In this case, we just assume
546 // the regclass is ok.
Jim Laskey60f09922006-07-21 20:57:35 +0000547 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000548 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000549 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng9e233362008-03-12 22:19:41 +0000550 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerb7795802008-03-11 00:59:28 +0000551 if (RC && VRC != RC) {
Chris Lattner01528292007-02-15 18:17:56 +0000552 cerr << "Register class of operand and regclass of use don't agree!\n";
Chris Lattner01528292007-02-15 18:17:56 +0000553 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000554 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000555 cerr << "MI = "; MI->print(cerr);
556 cerr << "VReg = " << VReg << "\n";
557 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000558 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000559 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000560 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000561 cerr << "Fatal error, aborting.\n";
562 abort();
563 }
Chris Lattnered18b682006-02-24 18:54:03 +0000564 }
Chris Lattnerb7795802008-03-11 00:59:28 +0000565#endif
Chris Lattnerfec65d52007-12-30 00:51:11 +0000566 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000567 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000568 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
569 const Type *FType = MVT::getTypeForValueType(Op.getValueType());
570 ConstantFP *CFP = ConstantFP::get(FType, F->getValueAPF());
571 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000572 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000573 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000574 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
575 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
576 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
577 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
578 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
579 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
580 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
581 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
582 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000583 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000584 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000585 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000586 // MachineConstantPool wants an explicit alignment.
587 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000588 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000589 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000590 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000591 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000592 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000593 }
Chris Lattnered18b682006-02-24 18:54:03 +0000594 }
595
Evan Chengd6594ae2006-09-12 21:00:35 +0000596 unsigned Idx;
597 if (CP->isMachineConstantPoolEntry())
598 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
599 else
600 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000601 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
602 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
603 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000604 } else {
605 assert(Op.getValueType() != MVT::Other &&
606 Op.getValueType() != MVT::Flag &&
607 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000608 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000609 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000610
Chris Lattner02b6d252008-03-09 08:49:15 +0000611 // Verify that it is right. Note that the reg class of the physreg and the
612 // vreg don't necessarily need to match, but the target copy insertion has
613 // to be able to handle it. This handles things like copies from ST(0) to
614 // an FP vreg on x86.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000615 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000616 if (II && !II->isVariadic()) {
Chris Lattner02b6d252008-03-09 08:49:15 +0000617 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
618 "Don't have operand info for this instruction!");
Chris Lattnered18b682006-02-24 18:54:03 +0000619 }
620 }
621
622}
623
Dan Gohman69de1932008-02-06 22:27:42 +0000624void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MemOperand &MO) {
625 MI->addMemOperand(MO);
626}
627
Christopher Lambe24f8f12007-07-26 08:12:07 +0000628// Returns the Register Class of a subregister
629static const TargetRegisterClass *getSubRegisterRegClass(
630 const TargetRegisterClass *TRC,
631 unsigned SubIdx) {
632 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000633 TargetRegisterInfo::regclass_iterator I =
634 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000635 assert(I < TRC->subregclasses_end() &&
636 "Invalid subregister index for register class");
637 return *I;
638}
639
640static const TargetRegisterClass *getSuperregRegisterClass(
641 const TargetRegisterClass *TRC,
642 unsigned SubIdx,
643 MVT::ValueType VT) {
644 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000645 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000646 E = TRC->superregclasses_end(); I != E; ++I)
647 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
648 return *I;
649 assert(false && "Couldn't find the register class");
650 return 0;
651}
652
653/// EmitSubregNode - Generate machine code for subreg nodes.
654///
655void ScheduleDAG::EmitSubregNode(SDNode *Node,
656 DenseMap<SDOperand, unsigned> &VRBaseMap) {
657 unsigned VRBase = 0;
658 unsigned Opc = Node->getTargetOpcode();
659 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
660 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
661 // the CopyToReg'd destination register instead of creating a new vreg.
662 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
663 UI != E; ++UI) {
664 SDNode *Use = *UI;
665 if (Use->getOpcode() == ISD::CopyToReg &&
666 Use->getOperand(2).Val == Node) {
667 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000668 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000669 VRBase = DestReg;
670 break;
671 }
672 }
673 }
674
675 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
676
677 // TODO: If the node is a use of a CopyFromReg from a physical register
678 // fold the extract into the copy now
679
Christopher Lambe24f8f12007-07-26 08:12:07 +0000680 // Create the extract_subreg machine instruction.
681 MachineInstr *MI =
682 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
683
684 // Figure out the register class to create for the destreg.
685 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng9e233362008-03-12 22:19:41 +0000686 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000687 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
688
689 if (VRBase) {
690 // Grab the destination register
Evan Cheng9e233362008-03-12 22:19:41 +0000691 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000692 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000693 "Source subregister and destination must have the same class");
694 } else {
695 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000696 assert(SRC && "Couldn't find source register class");
Evan Cheng9e233362008-03-12 22:19:41 +0000697 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000698 }
699
700 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000701 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000702 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000703 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000704
705 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000706 SDOperand N0 = Node->getOperand(0);
707 SDOperand N1 = Node->getOperand(1);
708 SDOperand N2 = Node->getOperand(2);
709 unsigned SubReg = getVR(N1, VRBaseMap);
710 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000711
Chris Lattner534bcfb2007-12-31 04:16:08 +0000712 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe24f8f12007-07-26 08:12:07 +0000713 // to allow coalescing in the allocator
714
715 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
716 // the CopyToReg'd destination register instead of creating a new vreg.
717 // If the CopyToReg'd destination register is physical, then fold the
718 // insert into the copy
719 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
720 UI != E; ++UI) {
721 SDNode *Use = *UI;
722 if (Use->getOpcode() == ISD::CopyToReg &&
723 Use->getOperand(2).Val == Node) {
724 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000725 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000726 VRBase = DestReg;
727 break;
728 }
729 }
730 }
731
732 // Create the insert_subreg machine instruction.
733 MachineInstr *MI =
734 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
735
736 // Figure out the register class to create for the destreg.
737 const TargetRegisterClass *TRC = 0;
738 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000739 TRC = MRI.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000740 } else {
Evan Cheng9e233362008-03-12 22:19:41 +0000741 TRC = getSuperregRegisterClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000742 Node->getValueType(0));
743 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng9e233362008-03-12 22:19:41 +0000744 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000745 }
746
Chris Lattner8019f412007-12-30 00:41:17 +0000747 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000748
749 // If N0 is a constant then it indicates the insert is being done
750 // into a target specific constant value, not a register.
751 if (const ConstantSDNode *SD = dyn_cast<ConstantSDNode>(N0))
752 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
753 else
754 AddOperand(MI, N0, 0, 0, VRBaseMap);
755 // Add the subregster being inserted
756 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000757 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000758 } else
759 assert(0 && "Node is not a subreg insert or extract");
760
761 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
762 assert(isNew && "Node emitted out of order - early");
763}
764
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000765/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000766///
Evan Chenga6fb1b62007-09-25 01:54:36 +0000767void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Evan Chengaf825c82007-07-10 07:08:32 +0000768 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000769 // If machine instruction
770 if (Node->isTargetOpcode()) {
771 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000772
773 // Handle subreg insert/extract specially
774 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
775 Opc == TargetInstrInfo::INSERT_SUBREG) {
776 EmitSubregNode(Node, VRBaseMap);
777 return;
778 }
779
Chris Lattner749c6f62008-01-07 07:27:27 +0000780 const TargetInstrDesc &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000781
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000782 unsigned NumResults = CountResults(Node);
783 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000784 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000785 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000786 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
787 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000788#ifndef NDEBUG
Chris Lattner349c4952008-01-07 03:13:06 +0000789 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000790 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000791 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000792#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000793
794 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000795 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000796
797 // Add result register values for things that are defined by this
798 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000799 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000800 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000801
802 // Emit all of the actual operands of this instruction, adding them to the
803 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000804 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000805 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000806
Dan Gohman69de1932008-02-06 22:27:42 +0000807 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000808 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000809 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
810
Evan Cheng13d41b92006-05-12 01:58:24 +0000811 // Commute node if it has been determined to be profitable.
812 if (CommuteSet.count(Node)) {
813 MachineInstr *NewMI = TII->commuteInstruction(MI);
814 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000815 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000816 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000817 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000818 if (MI != NewMI) {
819 delete MI;
820 MI = NewMI;
821 }
Evan Cheng643afa52008-02-28 07:40:24 +0000822 ++NumCommutes;
Evan Cheng13d41b92006-05-12 01:58:24 +0000823 }
824 }
825
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000826 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000827 // Insert this instruction into the basic block using a target
828 // specific inserter which may returns a new basic block.
Evan Chengff9b3732008-01-30 18:18:23 +0000829 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000830 else
831 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000832
833 // Additional results must be an physical register def.
834 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000835 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
836 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000837 if (Node->hasAnyUseOfValue(i))
Evan Chenga6fb1b62007-09-25 01:54:36 +0000838 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000839 }
840 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000841 } else {
842 switch (Node->getOpcode()) {
843 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000844#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000845 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000846#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000847 assert(0 && "This target-independent node should have been selected!");
848 case ISD::EntryToken: // fall thru
849 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000850 case ISD::LABEL:
Evan Chenga844bde2008-02-02 04:07:54 +0000851 case ISD::DECLARE:
Dan Gohman69de1932008-02-06 22:27:42 +0000852 case ISD::SRCVALUE:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000853 break;
854 case ISD::CopyToReg: {
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000855 unsigned SrcReg;
856 SDOperand SrcVal = Node->getOperand(2);
857 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
858 SrcReg = R->getReg();
Evan Cheng489a87c2007-01-05 20:59:06 +0000859 else
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000860 SrcReg = getVR(SrcVal, VRBaseMap);
861
Chris Lattnera4176522005-10-30 18:54:27 +0000862 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000863 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
864 break;
865
866 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
867 // Get the register classes of the src/dst.
868 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000869 SrcTRC = MRI.getRegClass(SrcReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000870 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000871 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000872
873 if (TargetRegisterInfo::isVirtualRegister(DestReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000874 DstTRC = MRI.getRegClass(DestReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000875 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000876 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
877 Node->getOperand(1).getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000878 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000879 break;
880 }
881 case ISD::CopyFromReg: {
882 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000883 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000884 break;
885 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000886 case ISD::INLINEASM: {
887 unsigned NumOps = Node->getNumOperands();
888 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
889 --NumOps; // Ignore the flag operand.
890
891 // Create the inline asm machine instruction.
892 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000893 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000894
895 // Add the asm string as an external symbol operand.
896 const char *AsmStr =
897 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000898 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000899
900 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000901 for (unsigned i = 2; i != NumOps;) {
902 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000903 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000904
Chris Lattnerfec65d52007-12-30 00:51:11 +0000905 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000906 ++i; // Skip the ID value.
907
908 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000909 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000910 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000911 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000912 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000913 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000914 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000915 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000916 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000917 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000918 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000919 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000920 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000921 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000922 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000923 for (; NumVals; --NumVals, ++i) {
924 if (ConstantSDNode *CS =
925 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000926 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000927 } else if (GlobalAddressSDNode *GA =
928 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000929 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
930 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000931 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000932 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
933 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000934 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000935 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000936 break;
937 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000938 case 4: // Addressing mode.
939 // The addressing mode has been selected, just add all of the
940 // operands to the machine instruction.
941 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000942 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000943 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000944 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000945 }
946 break;
947 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000948 }
949 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000950}
951
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000952void ScheduleDAG::EmitNoop() {
953 TII->insertNoop(*BB, BB->end());
954}
955
Chris Lattnerd9c4c452008-03-09 07:51:01 +0000956void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
957 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng42d60272007-09-26 21:36:17 +0000958 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
959 I != E; ++I) {
960 if (I->isCtrl) continue; // ignore chain preds
961 if (!I->Dep->Node) {
962 // Copy to physical register.
963 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
964 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
965 // Find the destination physical register.
966 unsigned Reg = 0;
967 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
968 EE = SU->Succs.end(); II != EE; ++II) {
969 if (I->Reg) {
970 Reg = I->Reg;
971 break;
972 }
973 }
974 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +0000975 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +0000976 SU->CopyDstRC, SU->CopySrcRC);
977 } else {
978 // Copy from physical register.
979 assert(I->Reg && "Unknown physical register!");
Evan Cheng9e233362008-03-12 22:19:41 +0000980 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Evan Cheng42d60272007-09-26 21:36:17 +0000981 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
982 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +0000983 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +0000984 SU->CopyDstRC, SU->CopySrcRC);
985 }
986 break;
987 }
988}
989
Evan Cheng9e233362008-03-12 22:19:41 +0000990/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
991/// physical register has only a single copy use, then coalesced the copy
Evan Chengdb2d7732008-03-14 00:14:55 +0000992/// if possible.
993void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
994 MachineBasicBlock::iterator &InsertPos,
995 unsigned VirtReg, unsigned PhysReg,
996 const TargetRegisterClass *RC,
997 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng9e233362008-03-12 22:19:41 +0000998 unsigned NumUses = 0;
999 MachineInstr *UseMI = NULL;
1000 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1001 UE = MRI.use_end(); UI != UE; ++UI) {
1002 UseMI = &*UI;
1003 if (++NumUses > 1)
1004 break;
1005 }
1006
1007 // If the number of uses is not one, or the use is not a move instruction,
Evan Chengdb2d7732008-03-14 00:14:55 +00001008 // don't coalesce. Also, only coalesce away a virtual register to virtual
1009 // register copy.
1010 bool Coalesced = false;
Evan Cheng9e233362008-03-12 22:19:41 +00001011 unsigned SrcReg, DstReg;
Evan Chengdb2d7732008-03-14 00:14:55 +00001012 if (NumUses == 1 &&
1013 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1014 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1015 VirtReg = DstReg;
1016 Coalesced = true;
Evan Cheng9e233362008-03-12 22:19:41 +00001017 }
1018
Evan Chengdb2d7732008-03-14 00:14:55 +00001019 // Now find an ideal location to insert the copy.
1020 MachineBasicBlock::iterator Pos = InsertPos;
1021 while (Pos != MBB->begin()) {
1022 MachineInstr *PrevMI = prior(Pos);
1023 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1024 // copyRegToReg might emit multiple instructions to do a copy.
1025 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1026 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1027 // This is what the BB looks like right now:
1028 // r1024 = mov r0
1029 // ...
1030 // r1 = mov r1024
1031 //
1032 // We want to insert "r1025 = mov r1". Inserting this copy below the
1033 // move to r1024 makes it impossible for that move to be coalesced.
1034 //
1035 // r1025 = mov r1
1036 // r1024 = mov r0
1037 // ...
1038 // r1 = mov 1024
1039 // r2 = mov 1025
1040 break; // Woot! Found a good location.
1041 --Pos;
1042 }
1043
1044 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1045 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1046 if (Coalesced) {
Evan Cheng9e233362008-03-12 22:19:41 +00001047 if (&*InsertPos == UseMI) ++InsertPos;
1048 MBB->erase(UseMI);
Evan Cheng9e233362008-03-12 22:19:41 +00001049 }
Evan Cheng9e233362008-03-12 22:19:41 +00001050}
1051
1052/// EmitLiveInCopies - If this is the first basic block in the function,
1053/// and if it has live ins that need to be copied into vregs, emit the
1054/// copies into the top of the block.
1055void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chengdb2d7732008-03-14 00:14:55 +00001056 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng9e233362008-03-12 22:19:41 +00001057 MachineBasicBlock::iterator InsertPos = MBB->begin();
1058 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1059 E = MRI.livein_end(); LI != E; ++LI)
1060 if (LI->second) {
1061 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chengdb2d7732008-03-14 00:14:55 +00001062 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng9e233362008-03-12 22:19:41 +00001063 }
1064}
1065
Evan Chenge165a782006-05-11 23:55:42 +00001066/// EmitSchedule - Emit the machine code in scheduled order.
1067void ScheduleDAG::EmitSchedule() {
Evan Cheng9e233362008-03-12 22:19:41 +00001068 bool isEntryBB = &MF->front() == BB;
1069
1070 if (isEntryBB && !SchedLiveInCopies) {
1071 // If this is the first basic block in the function, and if it has live ins
1072 // that need to be copied into vregs, emit the copies into the top of the
1073 // block before emitting the code for the block.
1074 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1075 E = MRI.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +00001076 if (LI->second) {
Evan Cheng9e233362008-03-12 22:19:41 +00001077 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +00001078 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +00001079 LI->first, RC, RC);
1080 }
Chris Lattner96645412006-05-16 06:10:58 +00001081 }
Evan Cheng9e233362008-03-12 22:19:41 +00001082
Chris Lattner96645412006-05-16 06:10:58 +00001083 // Finally, emit the code for all of the scheduled instructions.
Evan Chengaf825c82007-07-10 07:08:32 +00001084 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +00001085 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +00001086 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1087 if (SUnit *SU = Sequence[i]) {
Evan Chenga6fb1b62007-09-25 01:54:36 +00001088 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
1089 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng42d60272007-09-26 21:36:17 +00001090 if (SU->Node)
1091 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
1092 else
1093 EmitCrossRCCopy(SU, CopyVRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +00001094 } else {
1095 // Null SUnit* is a noop.
1096 EmitNoop();
1097 }
1098 }
Evan Cheng9e233362008-03-12 22:19:41 +00001099
1100 if (isEntryBB && SchedLiveInCopies)
1101 EmitLiveInCopies(MF->begin());
Evan Chenge165a782006-05-11 23:55:42 +00001102}
1103
1104/// dump - dump the schedule.
1105void ScheduleDAG::dumpSchedule() const {
1106 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1107 if (SUnit *SU = Sequence[i])
1108 SU->dump(&DAG);
1109 else
Bill Wendling832171c2006-12-07 20:04:42 +00001110 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +00001111 }
1112}
1113
1114
Evan Chenga9c20912006-01-21 02:32:06 +00001115/// Run - perform scheduling.
1116///
1117MachineBasicBlock *ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +00001118 Schedule();
1119 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +00001120}
Evan Cheng4ef10862006-01-23 07:01:07 +00001121
Evan Chenge165a782006-05-11 23:55:42 +00001122/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1123/// a group of nodes flagged together.
1124void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00001125 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +00001126 if (Node)
1127 Node->dump(G);
1128 else
1129 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +00001130 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001131 if (FlaggedNodes.size() != 0) {
1132 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +00001133 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +00001134 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +00001135 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001136 }
1137 }
1138}
Evan Cheng4ef10862006-01-23 07:01:07 +00001139
Evan Chenge165a782006-05-11 23:55:42 +00001140void SUnit::dumpAll(const SelectionDAG *G) const {
1141 dump(G);
1142
Bill Wendling832171c2006-12-07 20:04:42 +00001143 cerr << " # preds left : " << NumPredsLeft << "\n";
1144 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +00001145 cerr << " Latency : " << Latency << "\n";
1146 cerr << " Depth : " << Depth << "\n";
1147 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001148
1149 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001150 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001151 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1152 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001153 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001154 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001155 else
Bill Wendling832171c2006-12-07 20:04:42 +00001156 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001157 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1158 if (I->isSpecial)
1159 cerr << " *";
1160 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001161 }
1162 }
1163 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001164 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001165 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1166 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001167 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001168 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001169 else
Bill Wendling832171c2006-12-07 20:04:42 +00001170 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001171 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1172 if (I->isSpecial)
1173 cerr << " *";
1174 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001175 }
1176 }
Bill Wendling832171c2006-12-07 20:04:42 +00001177 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001178}