blob: bca279a97b20f51cddbf95d502c0232999390726 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "pre-RA-sched"
Nate Begemane2ba64f2008-02-14 08:57:00 +000017#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Type.h"
19#include "llvm/CodeGen/ScheduleDAG.h"
20#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng19da42d2008-04-03 16:36:07 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetLowering.h"
Evan Cheng7f6ade32008-02-28 07:40:24 +000028#include "llvm/ADT/Statistic.h"
Evan Cheng8725a112008-03-12 22:19:41 +000029#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/MathExtras.h"
32using namespace llvm;
33
Evan Cheng7f6ade32008-02-28 07:40:24 +000034STATISTIC(NumCommutes, "Number of instructions commuted");
35
Evan Cheng8725a112008-03-12 22:19:41 +000036namespace {
37 static cl::opt<bool>
38 SchedLiveInCopies("schedule-livein-copies",
39 cl::desc("Schedule copies of livein registers"),
40 cl::init(false));
41}
42
Chris Lattner1b989192007-12-31 04:13:23 +000043ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
44 const TargetMachine &tm)
Evan Cheng8725a112008-03-12 22:19:41 +000045 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
Evan Cheng19da42d2008-04-03 16:36:07 +000046 TII = TM.getInstrInfo();
47 MF = &DAG.getMachineFunction();
48 TRI = TM.getRegisterInfo();
49 TLI = &DAG.getTargetLoweringInfo();
50 ConstPool = BB->getParent()->getConstantPool();
Chris Lattner1b989192007-12-31 04:13:23 +000051}
Evan Cheng93f143e2007-09-25 01:54:36 +000052
Evan Cheng93f143e2007-09-25 01:54:36 +000053/// CheckForPhysRegDependency - Check if the dependency between def and use of
54/// a specified operand is a physical register dependency. If so, returns the
55/// register and the cost of copying the register.
56static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman1e57df32008-02-10 18:45:23 +000057 const TargetRegisterInfo *TRI,
Evan Cheng93f143e2007-09-25 01:54:36 +000058 const TargetInstrInfo *TII,
59 unsigned &PhysReg, int &Cost) {
60 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
61 return;
62
63 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +000064 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Cheng93f143e2007-09-25 01:54:36 +000065 return;
66
67 unsigned ResNo = Use->getOperand(2).ResNo;
68 if (Def->isTargetOpcode()) {
Chris Lattner5b930372008-01-07 07:27:27 +000069 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner0c2a4f32008-01-07 03:13:06 +000070 if (ResNo >= II.getNumDefs() &&
71 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Cheng93f143e2007-09-25 01:54:36 +000072 PhysReg = Reg;
73 const TargetRegisterClass *RC =
Evan Cheng14cc83f2008-03-11 07:19:34 +000074 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Cheng93f143e2007-09-25 01:54:36 +000075 Cost = RC->getCopyCost();
76 }
77 }
78}
79
80SUnit *ScheduleDAG::Clone(SUnit *Old) {
81 SUnit *SU = NewSUnit(Old->Node);
Dan Gohmanb100d802008-03-10 23:48:14 +000082 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Cheng93f143e2007-09-25 01:54:36 +000083 SU->InstanceNo = SUnitMap[Old->Node].size();
84 SU->Latency = Old->Latency;
85 SU->isTwoAddress = Old->isTwoAddress;
86 SU->isCommutable = Old->isCommutable;
Evan Chengba597da2007-09-28 22:32:30 +000087 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Cheng93f143e2007-09-25 01:54:36 +000088 SUnitMap[Old->Node].push_back(SU);
89 return SU;
90}
91
Evan Chengdd3f8b92007-10-05 01:39:18 +000092
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
94/// This SUnit graph is similar to the SelectionDAG, but represents flagged
95/// together nodes with a single SUnit.
96void ScheduleDAG::BuildSchedUnits() {
97 // Reserve entries in the vector for each of the SUnits we are creating. This
98 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
99 // invalidated.
100 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
103 E = DAG.allnodes_end(); NI != E; ++NI) {
104 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
105 continue;
106
107 // If this node has already been processed, stop now.
Evan Cheng93f143e2007-09-25 01:54:36 +0000108 if (SUnitMap[NI].size()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 SUnit *NodeSUnit = NewSUnit(NI);
111
112 // See if anything is flagged to this node, if so, add them to flagged
113 // nodes. Nodes can have at most one flag input and one flag output. Flags
114 // are required the be the last operand and result of a node.
115
116 // Scan up, adding flagged preds to FlaggedNodes.
117 SDNode *N = NI;
118 if (N->getNumOperands() &&
119 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
120 do {
121 N = N->getOperand(N->getNumOperands()-1).Val;
122 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000123 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 } while (N->getNumOperands() &&
125 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
126 std::reverse(NodeSUnit->FlaggedNodes.begin(),
127 NodeSUnit->FlaggedNodes.end());
128 }
129
130 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
131 // have a user of the flag operand.
132 N = NI;
133 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
134 SDOperand FlagVal(N, N->getNumValues()-1);
135
136 // There are either zero or one users of the Flag result.
137 bool HasFlagUse = false;
138 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
139 UI != E; ++UI)
Roman Levenstein05650fd2008-04-07 10:06:32 +0000140 if (FlagVal.isOperandOf(UI->getUser())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 HasFlagUse = true;
142 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000143 SUnitMap[N].push_back(NodeSUnit);
Roman Levenstein05650fd2008-04-07 10:06:32 +0000144 N = UI->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 break;
146 }
147 if (!HasFlagUse) break;
148 }
149
150 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
151 // Update the SUnit
152 NodeSUnit->Node = N;
Evan Cheng93f143e2007-09-25 01:54:36 +0000153 SUnitMap[N].push_back(NodeSUnit);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000154
155 ComputeLatency(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157
158 // Pass 2: add the preds, succs, etc.
159 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
160 SUnit *SU = &SUnits[su];
161 SDNode *MainNode = SU->Node;
162
163 if (MainNode->isTargetOpcode()) {
164 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner5b930372008-01-07 07:27:27 +0000165 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000166 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000167 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 SU->isTwoAddress = true;
169 break;
170 }
171 }
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000172 if (TID.isCommutable())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 SU->isCommutable = true;
174 }
175
176 // Find all predecessors and successors of the group.
177 // Temporarily add N to make code simpler.
178 SU->FlaggedNodes.push_back(MainNode);
179
180 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
181 SDNode *N = SU->FlaggedNodes[n];
Evan Chengba597da2007-09-28 22:32:30 +0000182 if (N->isTargetOpcode() &&
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000183 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
184 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Chengba597da2007-09-28 22:32:30 +0000185 SU->hasPhysRegDefs = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
188 SDNode *OpN = N->getOperand(i).Val;
189 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Cheng93f143e2007-09-25 01:54:36 +0000190 SUnit *OpSU = SUnitMap[OpN].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 assert(OpSU && "Node has no SUnit!");
192 if (OpSU == SU) continue; // In the same group.
193
194 MVT::ValueType OpVT = N->getOperand(i).getValueType();
195 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
196 bool isChain = OpVT == MVT::Other;
Evan Cheng93f143e2007-09-25 01:54:36 +0000197
198 unsigned PhysReg = 0;
199 int Cost = 1;
200 // Determine if this is a physical register dependency.
Dan Gohman1e57df32008-02-10 18:45:23 +0000201 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Cheng93f143e2007-09-25 01:54:36 +0000202 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
204 }
205
206 // Remove MainNode from FlaggedNodes again.
207 SU->FlaggedNodes.pop_back();
208 }
209
210 return;
211}
212
Evan Chengdd3f8b92007-10-05 01:39:18 +0000213void ScheduleDAG::ComputeLatency(SUnit *SU) {
214 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
215
216 // Compute the latency for the node. We use the sum of the latencies for
217 // all nodes flagged together into this SUnit.
218 if (InstrItins.isEmpty()) {
219 // No latency information.
220 SU->Latency = 1;
221 } else {
222 SU->Latency = 0;
223 if (SU->Node->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000224 unsigned SchedClass =
225 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Dan Gohman12300e12008-03-25 21:45:14 +0000226 const InstrStage *S = InstrItins.begin(SchedClass);
227 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000228 for (; S != E; ++S)
229 SU->Latency += S->Cycles;
230 }
231 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
232 SDNode *FNode = SU->FlaggedNodes[i];
233 if (FNode->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000234 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Dan Gohman12300e12008-03-25 21:45:14 +0000235 const InstrStage *S = InstrItins.begin(SchedClass);
236 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000237 for (; S != E; ++S)
238 SU->Latency += S->Cycles;
239 }
240 }
241 }
242}
243
Roman Levenstein1db9b822008-03-04 11:19:43 +0000244/// CalculateDepths - compute depths using algorithms for the longest
245/// paths in the DAG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246void ScheduleDAG::CalculateDepths() {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000247 unsigned DAGSize = SUnits.size();
248 std::vector<unsigned> InDegree(DAGSize);
249 std::vector<SUnit*> WorkList;
250 WorkList.reserve(DAGSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
Roman Levenstein1db9b822008-03-04 11:19:43 +0000252 // Initialize the data structures
253 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
254 SUnit *SU = &SUnits[i];
255 int NodeNum = SU->NodeNum;
256 unsigned Degree = SU->Preds.size();
257 InDegree[NodeNum] = Degree;
258 SU->Depth = 0;
259
260 // Is it a node without dependencies?
261 if (Degree == 0) {
262 assert(SU->Preds.empty() && "SUnit should have no predecessors");
263 // Collect leaf nodes
264 WorkList.push_back(SU);
265 }
266 }
267
268 // Process nodes in the topological order
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 while (!WorkList.empty()) {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000270 SUnit *SU = WorkList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 WorkList.pop_back();
Roman Levenstein1db9b822008-03-04 11:19:43 +0000272 unsigned &SUDepth = SU->Depth;
273
274 // Use dynamic programming:
275 // When current node is being processed, all of its dependencies
276 // are already processed.
277 // So, just iterate over all predecessors and take the longest path
278 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
279 I != E; ++I) {
280 unsigned PredDepth = I->Dep->Depth;
281 if (PredDepth+1 > SUDepth) {
282 SUDepth = PredDepth + 1;
283 }
284 }
285
286 // Update InDegrees of all nodes depending on current SUnit
287 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
288 I != E; ++I) {
289 SUnit *SU = I->Dep;
290 if (!--InDegree[SU->NodeNum])
291 // If all dependencies of the node are processed already,
292 // then the longest path for the node can be computed now
293 WorkList.push_back(SU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 }
295 }
296}
297
Roman Levenstein1db9b822008-03-04 11:19:43 +0000298/// CalculateHeights - compute heights using algorithms for the longest
299/// paths in the DAG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300void ScheduleDAG::CalculateHeights() {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000301 unsigned DAGSize = SUnits.size();
302 std::vector<unsigned> InDegree(DAGSize);
303 std::vector<SUnit*> WorkList;
304 WorkList.reserve(DAGSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305
Roman Levenstein1db9b822008-03-04 11:19:43 +0000306 // Initialize the data structures
307 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
308 SUnit *SU = &SUnits[i];
309 int NodeNum = SU->NodeNum;
310 unsigned Degree = SU->Succs.size();
311 InDegree[NodeNum] = Degree;
312 SU->Height = 0;
313
314 // Is it a node without dependencies?
315 if (Degree == 0) {
316 assert(SU->Succs.empty() && "Something wrong");
317 assert(WorkList.empty() && "Should be empty");
318 // Collect leaf nodes
319 WorkList.push_back(SU);
320 }
321 }
322
323 // Process nodes in the topological order
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 while (!WorkList.empty()) {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000325 SUnit *SU = WorkList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 WorkList.pop_back();
Roman Levenstein1db9b822008-03-04 11:19:43 +0000327 unsigned &SUHeight = SU->Height;
328
329 // Use dynamic programming:
330 // When current node is being processed, all of its dependencies
331 // are already processed.
332 // So, just iterate over all successors and take the longest path
333 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
334 I != E; ++I) {
335 unsigned SuccHeight = I->Dep->Height;
336 if (SuccHeight+1 > SUHeight) {
337 SUHeight = SuccHeight + 1;
338 }
339 }
340
341 // Update InDegrees of all nodes depending on current SUnit
342 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
343 I != E; ++I) {
344 SUnit *SU = I->Dep;
345 if (!--InDegree[SU->NodeNum])
346 // If all dependencies of the node are processed already,
347 // then the longest path for the node can be computed now
348 WorkList.push_back(SU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 }
350 }
351}
352
353/// CountResults - The results of target nodes have register or immediate
354/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman0256f1e2008-02-11 19:00:03 +0000355/// not go into the resulting MachineInstr).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356unsigned ScheduleDAG::CountResults(SDNode *Node) {
357 unsigned N = Node->getNumValues();
358 while (N && Node->getValueType(N - 1) == MVT::Flag)
359 --N;
360 if (N && Node->getValueType(N - 1) == MVT::Other)
361 --N; // Skip over chain result.
362 return N;
363}
364
Dan Gohman12a9c082008-02-06 22:27:42 +0000365/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohmance256462008-02-16 00:36:48 +0000366/// followed by special operands that describe memory references, then an
367/// optional chain operand, then flag operands. Compute the number of
368/// actual operands that will go into the resulting MachineInstr.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohmance256462008-02-16 00:36:48 +0000370 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohman206208c2008-02-11 19:00:34 +0000371 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman1fad9e62008-04-07 19:35:22 +0000372 --N; // Ignore MEMOPERAND nodes
Dan Gohman12a9c082008-02-06 22:27:42 +0000373 return N;
374}
375
Dan Gohmance256462008-02-16 00:36:48 +0000376/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
377/// operand
378unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman12a9c082008-02-06 22:27:42 +0000379 unsigned N = Node->getNumOperands();
380 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
381 --N;
382 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
383 --N; // Ignore chain if it exists.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 return N;
385}
386
387static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman1e57df32008-02-10 18:45:23 +0000388 const TargetRegisterInfo *TRI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000390 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000392 if (Op >= II.getNumOperands()) {
393 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 return NULL;
395 }
Chris Lattner5b930372008-01-07 07:27:27 +0000396 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000397 return TII->getPointerRegClass();
Dan Gohman1e57df32008-02-10 18:45:23 +0000398 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399}
400
Evan Cheng93f143e2007-09-25 01:54:36 +0000401void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
402 unsigned InstanceNo, unsigned SrcReg,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000403 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng26639782007-08-02 00:28:15 +0000404 unsigned VRBase = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000405 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000406 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000407 if (InstanceNo > 0)
408 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng32806492008-05-14 20:26:35 +0000409#ifndef NDEBUG
Evan Cheng26639782007-08-02 00:28:15 +0000410 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
Evan Cheng32806492008-05-14 20:26:35 +0000411#else
412 VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
413#endif
Evan Cheng26639782007-08-02 00:28:15 +0000414 assert(isNew && "Node emitted out of order - early");
415 return;
416 }
417
418 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
419 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000420 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000421 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
422 UI != E; ++UI) {
Roman Levenstein05650fd2008-04-07 10:06:32 +0000423 SDNode *Use = UI->getUser();
Evan Cheng93f143e2007-09-25 01:54:36 +0000424 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000425 if (Use->getOpcode() == ISD::CopyToReg &&
426 Use->getOperand(2).Val == Node &&
427 Use->getOperand(2).ResNo == ResNo) {
428 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000429 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000430 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000431 Match = false;
432 } else if (DestReg != SrcReg)
433 Match = false;
434 } else {
435 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
436 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000437 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000438 continue;
439 MVT::ValueType VT = Node->getValueType(Op.ResNo);
440 if (VT != MVT::Other && VT != MVT::Flag)
441 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000442 }
443 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000444 MatchReg &= Match;
445 if (VRBase)
446 break;
Evan Cheng26639782007-08-02 00:28:15 +0000447 }
448
Chris Lattnere6fdb062008-03-09 08:49:15 +0000449 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000450 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattnere6fdb062008-03-09 08:49:15 +0000451
Evan Cheng93f143e2007-09-25 01:54:36 +0000452 // Figure out the register class to create for the destreg.
Chris Lattnere6fdb062008-03-09 08:49:15 +0000453 if (VRBase) {
Evan Cheng8725a112008-03-12 22:19:41 +0000454 DstRC = MRI.getRegClass(VRBase);
Chris Lattnere6fdb062008-03-09 08:49:15 +0000455 } else {
Evan Cheng19da42d2008-04-03 16:36:07 +0000456 DstRC = TLI->getRegClassFor(Node->getValueType(ResNo));
Chris Lattnere6fdb062008-03-09 08:49:15 +0000457 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000458
459 // If all uses are reading from the src physical register and copying the
460 // register is either impossible or very expensive, then don't create a copy.
Chris Lattnere6fdb062008-03-09 08:49:15 +0000461 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000462 VRBase = SrcReg;
463 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000464 // Create the reg, emit the copy.
Evan Cheng8725a112008-03-12 22:19:41 +0000465 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattnere6fdb062008-03-09 08:49:15 +0000466 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng26639782007-08-02 00:28:15 +0000467 }
Evan Cheng26639782007-08-02 00:28:15 +0000468
Evan Cheng93f143e2007-09-25 01:54:36 +0000469 if (InstanceNo > 0)
470 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng32806492008-05-14 20:26:35 +0000471#ifndef NDEBUG
Evan Cheng26639782007-08-02 00:28:15 +0000472 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
Evan Cheng32806492008-05-14 20:26:35 +0000473#else
474 VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
475#endif
Evan Cheng26639782007-08-02 00:28:15 +0000476 assert(isNew && "Node emitted out of order - early");
477}
478
Evan Cheng19da42d2008-04-03 16:36:07 +0000479/// getDstOfCopyToRegUse - If the only use of the specified result number of
480/// node is a CopyToReg, return its destination register. Return 0 otherwise.
481unsigned ScheduleDAG::getDstOfOnlyCopyToRegUse(SDNode *Node,
482 unsigned ResNo) const {
483 if (!Node->hasOneUse())
484 return 0;
485
Roman Levenstein05650fd2008-04-07 10:06:32 +0000486 SDNode *Use = Node->use_begin()->getUser();
Evan Cheng19da42d2008-04-03 16:36:07 +0000487 if (Use->getOpcode() == ISD::CopyToReg &&
488 Use->getOperand(2).Val == Node &&
489 Use->getOperand(2).ResNo == ResNo) {
490 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
491 if (TargetRegisterInfo::isVirtualRegister(Reg))
492 return Reg;
493 }
494 return 0;
495}
496
Evan Cheng3c0eda52008-03-15 00:03:38 +0000497void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Cheng19da42d2008-04-03 16:36:07 +0000498 const TargetInstrDesc &II,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000499 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng19da42d2008-04-03 16:36:07 +0000500 assert(Node->getTargetOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
501 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
502
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000503 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 // If the specific node value is only used by a CopyToReg and the dest reg
505 // is a vreg, use the CopyToReg'd destination register instead of creating
506 // a new vreg.
507 unsigned VRBase = 0;
508 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
509 UI != E; ++UI) {
Roman Levenstein05650fd2008-04-07 10:06:32 +0000510 SDNode *Use = UI->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 if (Use->getOpcode() == ISD::CopyToReg &&
512 Use->getOperand(2).Val == Node &&
513 Use->getOperand(2).ResNo == i) {
514 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000515 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000517 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 break;
519 }
520 }
521 }
522
Evan Cheng26639782007-08-02 00:28:15 +0000523 // Create the result registers for this node and add the result regs to
524 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 if (VRBase == 0) {
Evan Cheng19da42d2008-04-03 16:36:07 +0000526 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 assert(RC && "Isn't a register operand!");
Evan Cheng8725a112008-03-12 22:19:41 +0000528 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000529 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 }
531
Evan Cheng32806492008-05-14 20:26:35 +0000532#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
Evan Cheng32806492008-05-14 20:26:35 +0000534#else
535 VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
536#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 assert(isNew && "Node emitted out of order - early");
538 }
539}
540
541/// getVR - Return the virtual register corresponding to the specified result
542/// of the specified node.
Evan Cheng19da42d2008-04-03 16:36:07 +0000543unsigned ScheduleDAG::getVR(SDOperand Op,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000544 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng19da42d2008-04-03 16:36:07 +0000545 if (Op.isTargetOpcode() &&
546 Op.getTargetOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
547 // Add an IMPLICIT_DEF instruction before every use.
548 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.Val, Op.ResNo);
549 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
550 // does not include operand register class info.
551 if (!VReg) {
552 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
553 VReg = MRI.createVirtualRegister(RC);
554 }
555 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
556 return VReg;
557 }
558
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000559 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
561 return I->second;
562}
563
564
565/// AddOperand - Add the specified operand to the specified machine instr. II
566/// specifies the instruction information for the node, and IIOpNum is the
567/// operand number (in the II) that we are adding. IIOpNum and II are used for
568/// assertions only.
569void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
570 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000571 const TargetInstrDesc *II,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000572 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 if (Op.isTargetOpcode()) {
574 // Note that this case is redundant with the final else block, but we
575 // include it because it is the most common and it makes the logic
576 // simpler here.
577 assert(Op.getValueType() != MVT::Other &&
578 Op.getValueType() != MVT::Flag &&
579 "Chain and flag operands should occur at end of operand list!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 // Get/emit the operand.
581 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000582 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng19da42d2008-04-03 16:36:07 +0000583 bool isOptDef = IIOpNum < TID.getNumOperands() &&
584 TID.OpInfo[IIOpNum].isOptionalDef();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000585 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586
587 // Verify that it is right.
Dan Gohman1e57df32008-02-10 18:45:23 +0000588 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000589#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 if (II) {
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000591 // There may be no register class for this operand if it is a variadic
592 // argument (RC will be NULL in this case). In this case, we just assume
593 // the regclass is ok.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 const TargetRegisterClass *RC =
Dan Gohman1e57df32008-02-10 18:45:23 +0000595 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattner92d51282008-03-11 03:14:42 +0000596 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng8725a112008-03-12 22:19:41 +0000597 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000598 if (RC && VRC != RC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 cerr << "Register class of operand and regclass of use don't agree!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 cerr << "Operand = " << IIOpNum << "\n";
601 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
602 cerr << "MI = "; MI->print(cerr);
603 cerr << "VReg = " << VReg << "\n";
604 cerr << "VReg RegClass size = " << VRC->getSize()
605 << ", align = " << VRC->getAlignment() << "\n";
606 cerr << "Expected RegClass size = " << RC->getSize()
607 << ", align = " << RC->getAlignment() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 cerr << "Fatal error, aborting.\n";
609 abort();
610 }
611 }
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000612#endif
Chris Lattner8dfd3122007-12-30 00:51:11 +0000613 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000614 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane2ba64f2008-02-14 08:57:00 +0000615 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Chris Lattner5e0610f2008-04-20 00:41:09 +0000616 ConstantFP *CFP = ConstantFP::get(F->getValueAPF());
Nate Begemane2ba64f2008-02-14 08:57:00 +0000617 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000618 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000619 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000620 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
621 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
622 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
623 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
624 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
625 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
626 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
627 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
628 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 int Offset = CP->getOffset();
630 unsigned Align = CP->getAlignment();
631 const Type *Type = CP->getType();
632 // MachineConstantPool wants an explicit alignment.
633 if (Align == 0) {
634 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
635 if (Align == 0) {
636 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000637 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 Align = Log2_64(Align);
639 }
640 }
641
642 unsigned Idx;
643 if (CP->isMachineConstantPoolEntry())
644 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
645 else
646 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000647 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
648 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
649 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 } else {
651 assert(Op.getValueType() != MVT::Other &&
652 Op.getValueType() != MVT::Flag &&
653 "Chain and flag operands should occur at end of operand list!");
654 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000655 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656
Chris Lattnere6fdb062008-03-09 08:49:15 +0000657 // Verify that it is right. Note that the reg class of the physreg and the
658 // vreg don't necessarily need to match, but the target copy insertion has
659 // to be able to handle it. This handles things like copies from ST(0) to
660 // an FP vreg on x86.
Dan Gohman1e57df32008-02-10 18:45:23 +0000661 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattner92d51282008-03-11 03:14:42 +0000662 if (II && !II->isVariadic()) {
Chris Lattnere6fdb062008-03-09 08:49:15 +0000663 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
664 "Don't have operand info for this instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 }
666 }
667
668}
669
Dan Gohman1fad9e62008-04-07 19:35:22 +0000670void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
Dan Gohman12a9c082008-02-06 22:27:42 +0000671 MI->addMemOperand(MO);
672}
673
Christopher Lambe95328d2007-07-26 08:12:07 +0000674// Returns the Register Class of a subregister
675static const TargetRegisterClass *getSubRegisterRegClass(
676 const TargetRegisterClass *TRC,
677 unsigned SubIdx) {
678 // Pick the register class of the subregister
Dan Gohman1e57df32008-02-10 18:45:23 +0000679 TargetRegisterInfo::regclass_iterator I =
680 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe95328d2007-07-26 08:12:07 +0000681 assert(I < TRC->subregclasses_end() &&
682 "Invalid subregister index for register class");
683 return *I;
684}
685
686static const TargetRegisterClass *getSuperregRegisterClass(
687 const TargetRegisterClass *TRC,
688 unsigned SubIdx,
689 MVT::ValueType VT) {
690 // Pick the register class of the superegister for this type
Dan Gohman1e57df32008-02-10 18:45:23 +0000691 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe95328d2007-07-26 08:12:07 +0000692 E = TRC->superregclasses_end(); I != E; ++I)
693 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
694 return *I;
695 assert(false && "Couldn't find the register class");
696 return 0;
697}
698
699/// EmitSubregNode - Generate machine code for subreg nodes.
700///
701void ScheduleDAG::EmitSubregNode(SDNode *Node,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000702 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000703 unsigned VRBase = 0;
704 unsigned Opc = Node->getTargetOpcode();
Christopher Lamb76d72da2008-03-16 03:12:01 +0000705
706 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
707 // the CopyToReg'd destination register instead of creating a new vreg.
708 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
709 UI != E; ++UI) {
Roman Levenstein05650fd2008-04-07 10:06:32 +0000710 SDNode *Use = UI->getUser();
Christopher Lamb76d72da2008-03-16 03:12:01 +0000711 if (Use->getOpcode() == ISD::CopyToReg &&
712 Use->getOperand(2).Val == Node) {
713 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
714 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
715 VRBase = DestReg;
716 break;
Christopher Lambe95328d2007-07-26 08:12:07 +0000717 }
718 }
Christopher Lamb76d72da2008-03-16 03:12:01 +0000719 }
720
721 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000722 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
Christopher Lambe95328d2007-07-26 08:12:07 +0000723
Christopher Lambe95328d2007-07-26 08:12:07 +0000724 // Create the extract_subreg machine instruction.
Evan Cheng19da42d2008-04-03 16:36:07 +0000725 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Christopher Lambe95328d2007-07-26 08:12:07 +0000726
727 // Figure out the register class to create for the destreg.
728 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng8725a112008-03-12 22:19:41 +0000729 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000730 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
731
732 if (VRBase) {
733 // Grab the destination register
Evan Chengaaa364e2008-05-14 20:07:51 +0000734#ifndef NDEBUG
Evan Cheng8725a112008-03-12 22:19:41 +0000735 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000736 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe95328d2007-07-26 08:12:07 +0000737 "Source subregister and destination must have the same class");
Evan Chengaaa364e2008-05-14 20:07:51 +0000738#endif
Christopher Lambe95328d2007-07-26 08:12:07 +0000739 } else {
740 // Create the reg
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000741 assert(SRC && "Couldn't find source register class");
Evan Cheng8725a112008-03-12 22:19:41 +0000742 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000743 }
744
745 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000746 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000747 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000748 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng19da42d2008-04-03 16:36:07 +0000749 BB->push_back(MI);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000750 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
751 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambd0c8eaa2008-03-11 10:09:17 +0000752 SDOperand N0 = Node->getOperand(0);
753 SDOperand N1 = Node->getOperand(1);
754 SDOperand N2 = Node->getOperand(2);
755 unsigned SubReg = getVR(N1, VRBaseMap);
756 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe95328d2007-07-26 08:12:07 +0000757
Christopher Lambe95328d2007-07-26 08:12:07 +0000758
759 // Figure out the register class to create for the destreg.
760 const TargetRegisterClass *TRC = 0;
761 if (VRBase) {
Evan Cheng8725a112008-03-12 22:19:41 +0000762 TRC = MRI.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000763 } else {
Evan Cheng8725a112008-03-12 22:19:41 +0000764 TRC = getSuperregRegisterClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000765 Node->getValueType(0));
766 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng8725a112008-03-12 22:19:41 +0000767 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000768 }
769
Christopher Lamb76d72da2008-03-16 03:12:01 +0000770 // Create the insert_subreg or subreg_to_reg machine instruction.
Evan Cheng19da42d2008-04-03 16:36:07 +0000771 MachineInstr *MI = BuildMI(TII->get(Opc));
Chris Lattner63ab1f22007-12-30 00:41:17 +0000772 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambd0c8eaa2008-03-11 10:09:17 +0000773
Christopher Lamb76d72da2008-03-16 03:12:01 +0000774 // If creating a subreg_to_reg, then the first input operand
775 // is an implicit value immediate, otherwise it's a register
776 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
777 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Christopher Lambd0c8eaa2008-03-11 10:09:17 +0000778 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
Christopher Lamb76d72da2008-03-16 03:12:01 +0000779 } else
Christopher Lambd0c8eaa2008-03-11 10:09:17 +0000780 AddOperand(MI, N0, 0, 0, VRBaseMap);
781 // Add the subregster being inserted
782 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000783 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng19da42d2008-04-03 16:36:07 +0000784 BB->push_back(MI);
Christopher Lambe95328d2007-07-26 08:12:07 +0000785 } else
Christopher Lamb76d72da2008-03-16 03:12:01 +0000786 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Christopher Lambe95328d2007-07-26 08:12:07 +0000787
Evan Cheng32806492008-05-14 20:26:35 +0000788#ifndef NDEBUG
Christopher Lambe95328d2007-07-26 08:12:07 +0000789 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
Evan Cheng32806492008-05-14 20:26:35 +0000790#else
791 VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
792#endif
Christopher Lambe95328d2007-07-26 08:12:07 +0000793 assert(isNew && "Node emitted out of order - early");
794}
795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796/// EmitNode - Generate machine code for an node and needed dependencies.
797///
Evan Cheng93f143e2007-09-25 01:54:36 +0000798void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000799 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 // If machine instruction
801 if (Node->isTargetOpcode()) {
802 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000803
804 // Handle subreg insert/extract specially
805 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Christopher Lamb76d72da2008-03-16 03:12:01 +0000806 Opc == TargetInstrInfo::INSERT_SUBREG ||
807 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000808 EmitSubregNode(Node, VRBaseMap);
809 return;
810 }
Evan Cheng19da42d2008-04-03 16:36:07 +0000811
812 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
813 // We want a unique VR for each IMPLICIT_DEF use.
814 return;
Christopher Lambe95328d2007-07-26 08:12:07 +0000815
Chris Lattner5b930372008-01-07 07:27:27 +0000816 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 unsigned NumResults = CountResults(Node);
818 unsigned NodeOperands = CountOperands(Node);
Dan Gohmance256462008-02-16 00:36:48 +0000819 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000820 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
821 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822#ifndef NDEBUG
Evan Chengaaa364e2008-05-14 20:07:51 +0000823 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000824 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000825 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 "#operands for dag node doesn't match .td file!");
827#endif
828
829 // Create the new machine instruction.
Evan Cheng19da42d2008-04-03 16:36:07 +0000830 MachineInstr *MI = BuildMI(II);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831
832 // Add result register values for things that are defined by this
833 // instruction.
834 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000835 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836
837 // Emit all of the actual operands of this instruction, adding them to the
838 // instruction as appropriate.
839 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000840 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841
Dan Gohman12a9c082008-02-06 22:27:42 +0000842 // Emit all of the memory operands of this instruction
Dan Gohmance256462008-02-16 00:36:48 +0000843 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman12a9c082008-02-06 22:27:42 +0000844 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 // Commute node if it has been determined to be profitable.
847 if (CommuteSet.count(Node)) {
848 MachineInstr *NewMI = TII->commuteInstruction(MI);
849 if (NewMI == 0)
850 DOUT << "Sched: COMMUTING FAILED!\n";
851 else {
852 DOUT << "Sched: COMMUTED TO: " << *NewMI;
853 if (MI != NewMI) {
854 delete MI;
855 MI = NewMI;
856 }
Evan Cheng7f6ade32008-02-28 07:40:24 +0000857 ++NumCommutes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 }
859 }
860
Evan Chenga53c40a2008-02-01 09:10:45 +0000861 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng2d373922008-01-30 19:35:32 +0000862 // Insert this instruction into the basic block using a target
863 // specific inserter which may returns a new basic block.
Evan Cheng19da42d2008-04-03 16:36:07 +0000864 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Evan Cheng2d373922008-01-30 19:35:32 +0000865 else
866 BB->push_back(MI);
Evan Cheng26639782007-08-02 00:28:15 +0000867
868 // Additional results must be an physical register def.
869 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000870 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
871 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000872 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000873 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000874 }
875 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 } else {
877 switch (Node->getOpcode()) {
878 default:
879#ifndef NDEBUG
880 Node->dump(&DAG);
881#endif
882 assert(0 && "This target-independent node should have been selected!");
Dan Gohmanb7ba05e2008-04-15 01:22:18 +0000883 break;
884 case ISD::EntryToken:
885 assert(0 && "EntryToken should have been excluded from the schedule!");
886 break;
887 case ISD::TokenFactor: // fall thru
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 case ISD::LABEL:
Evan Cheng2e28d622008-02-02 04:07:54 +0000889 case ISD::DECLARE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000890 case ISD::SRCVALUE:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 break;
892 case ISD::CopyToReg: {
Chris Lattner0d128722008-03-09 09:15:31 +0000893 unsigned SrcReg;
894 SDOperand SrcVal = Node->getOperand(2);
895 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
896 SrcReg = R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 else
Chris Lattner0d128722008-03-09 09:15:31 +0000898 SrcReg = getVR(SrcVal, VRBaseMap);
899
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner0d128722008-03-09 09:15:31 +0000901 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
902 break;
903
904 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
905 // Get the register classes of the src/dst.
906 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
Evan Cheng8725a112008-03-12 22:19:41 +0000907 SrcTRC = MRI.getRegClass(SrcReg);
Chris Lattner0d128722008-03-09 09:15:31 +0000908 else
Evan Cheng14cc83f2008-03-11 07:19:34 +0000909 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattner0d128722008-03-09 09:15:31 +0000910
911 if (TargetRegisterInfo::isVirtualRegister(DestReg))
Evan Cheng8725a112008-03-12 22:19:41 +0000912 DstTRC = MRI.getRegClass(DestReg);
Chris Lattner0d128722008-03-09 09:15:31 +0000913 else
Evan Cheng14cc83f2008-03-11 07:19:34 +0000914 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
915 Node->getOperand(1).getValueType());
Chris Lattner0d128722008-03-09 09:15:31 +0000916 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 break;
918 }
919 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000921 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 break;
923 }
924 case ISD::INLINEASM: {
925 unsigned NumOps = Node->getNumOperands();
926 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
927 --NumOps; // Ignore the flag operand.
928
929 // Create the inline asm machine instruction.
Evan Cheng19da42d2008-04-03 16:36:07 +0000930 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::INLINEASM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931
932 // Add the asm string as an external symbol operand.
933 const char *AsmStr =
934 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000935 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936
937 // Add all of the operand registers to the instruction.
938 for (unsigned i = 2; i != NumOps;) {
939 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
940 unsigned NumVals = Flags >> 3;
941
Chris Lattner8dfd3122007-12-30 00:51:11 +0000942 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 ++i; // Skip the ID value.
944
945 switch (Flags & 7) {
946 default: assert(0 && "Bad flags!");
947 case 1: // Use of register.
948 for (; NumVals; --NumVals, ++i) {
949 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000950 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 }
952 break;
953 case 2: // Def of register.
954 for (; NumVals; --NumVals, ++i) {
955 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000956 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 }
958 break;
959 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000960 for (; NumVals; --NumVals, ++i) {
961 if (ConstantSDNode *CS =
962 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000963 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000964 } else if (GlobalAddressSDNode *GA =
965 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000966 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
967 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000968 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000969 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
970 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000971 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 break;
974 }
975 case 4: // Addressing mode.
976 // The addressing mode has been selected, just add all of the
977 // operands to the machine instruction.
978 for (; NumVals; --NumVals, ++i)
979 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
980 break;
981 }
982 }
Evan Cheng19da42d2008-04-03 16:36:07 +0000983 BB->push_back(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 break;
985 }
986 }
987 }
988}
989
990void ScheduleDAG::EmitNoop() {
991 TII->insertNoop(*BB, BB->end());
992}
993
Chris Lattner4e15fcc2008-03-09 07:51:01 +0000994void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
995 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng5ec4b762007-09-26 21:36:17 +0000996 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
997 I != E; ++I) {
998 if (I->isCtrl) continue; // ignore chain preds
999 if (!I->Dep->Node) {
1000 // Copy to physical register.
1001 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
1002 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
1003 // Find the destination physical register.
1004 unsigned Reg = 0;
1005 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
1006 EE = SU->Succs.end(); II != EE; ++II) {
1007 if (I->Reg) {
1008 Reg = I->Reg;
1009 break;
1010 }
1011 }
1012 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +00001013 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +00001014 SU->CopyDstRC, SU->CopySrcRC);
1015 } else {
1016 // Copy from physical register.
1017 assert(I->Reg && "Unknown physical register!");
Evan Cheng8725a112008-03-12 22:19:41 +00001018 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Evan Cheng32806492008-05-14 20:26:35 +00001019#ifndef NDEBUG
Evan Cheng5ec4b762007-09-26 21:36:17 +00001020 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
Evan Cheng32806492008-05-14 20:26:35 +00001021#else
1022 VRBaseMap.insert(std::make_pair(SU, VRBase));
1023#endif
Evan Cheng5ec4b762007-09-26 21:36:17 +00001024 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +00001025 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +00001026 SU->CopyDstRC, SU->CopySrcRC);
1027 }
1028 break;
1029 }
1030}
1031
Evan Cheng8725a112008-03-12 22:19:41 +00001032/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1033/// physical register has only a single copy use, then coalesced the copy
Evan Chenga96f9642008-03-14 00:14:55 +00001034/// if possible.
1035void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
1036 MachineBasicBlock::iterator &InsertPos,
1037 unsigned VirtReg, unsigned PhysReg,
1038 const TargetRegisterClass *RC,
1039 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng8725a112008-03-12 22:19:41 +00001040 unsigned NumUses = 0;
1041 MachineInstr *UseMI = NULL;
1042 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1043 UE = MRI.use_end(); UI != UE; ++UI) {
1044 UseMI = &*UI;
1045 if (++NumUses > 1)
1046 break;
1047 }
1048
1049 // If the number of uses is not one, or the use is not a move instruction,
Evan Chenga96f9642008-03-14 00:14:55 +00001050 // don't coalesce. Also, only coalesce away a virtual register to virtual
1051 // register copy.
1052 bool Coalesced = false;
Evan Cheng8725a112008-03-12 22:19:41 +00001053 unsigned SrcReg, DstReg;
Evan Chenga96f9642008-03-14 00:14:55 +00001054 if (NumUses == 1 &&
1055 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1056 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1057 VirtReg = DstReg;
1058 Coalesced = true;
Evan Cheng8725a112008-03-12 22:19:41 +00001059 }
1060
Evan Chenga96f9642008-03-14 00:14:55 +00001061 // Now find an ideal location to insert the copy.
1062 MachineBasicBlock::iterator Pos = InsertPos;
1063 while (Pos != MBB->begin()) {
1064 MachineInstr *PrevMI = prior(Pos);
1065 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1066 // copyRegToReg might emit multiple instructions to do a copy.
1067 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1068 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1069 // This is what the BB looks like right now:
1070 // r1024 = mov r0
1071 // ...
1072 // r1 = mov r1024
1073 //
1074 // We want to insert "r1025 = mov r1". Inserting this copy below the
1075 // move to r1024 makes it impossible for that move to be coalesced.
1076 //
1077 // r1025 = mov r1
1078 // r1024 = mov r0
1079 // ...
1080 // r1 = mov 1024
1081 // r2 = mov 1025
1082 break; // Woot! Found a good location.
1083 --Pos;
1084 }
1085
1086 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1087 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1088 if (Coalesced) {
Evan Cheng8725a112008-03-12 22:19:41 +00001089 if (&*InsertPos == UseMI) ++InsertPos;
1090 MBB->erase(UseMI);
Evan Cheng8725a112008-03-12 22:19:41 +00001091 }
Evan Cheng8725a112008-03-12 22:19:41 +00001092}
1093
1094/// EmitLiveInCopies - If this is the first basic block in the function,
1095/// and if it has live ins that need to be copied into vregs, emit the
1096/// copies into the top of the block.
1097void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chenga96f9642008-03-14 00:14:55 +00001098 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng8725a112008-03-12 22:19:41 +00001099 MachineBasicBlock::iterator InsertPos = MBB->begin();
1100 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1101 E = MRI.livein_end(); LI != E; ++LI)
1102 if (LI->second) {
1103 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chenga96f9642008-03-14 00:14:55 +00001104 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng8725a112008-03-12 22:19:41 +00001105 }
1106}
1107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108/// EmitSchedule - Emit the machine code in scheduled order.
1109void ScheduleDAG::EmitSchedule() {
Evan Cheng8725a112008-03-12 22:19:41 +00001110 bool isEntryBB = &MF->front() == BB;
1111
1112 if (isEntryBB && !SchedLiveInCopies) {
1113 // If this is the first basic block in the function, and if it has live ins
1114 // that need to be copied into vregs, emit the copies into the top of the
1115 // block before emitting the code for the block.
1116 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1117 E = MRI.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +00001118 if (LI->second) {
Evan Cheng8725a112008-03-12 22:19:41 +00001119 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng2d373922008-01-30 19:35:32 +00001120 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +00001121 LI->first, RC, RC);
1122 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 }
Evan Cheng8725a112008-03-12 22:19:41 +00001124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 // Finally, emit the code for all of the scheduled instructions.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001126 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +00001127 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Evan Cheng19da42d2008-04-03 16:36:07 +00001129 SUnit *SU = Sequence[i];
1130 if (!SU) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 // Null SUnit* is a noop.
1132 EmitNoop();
Evan Cheng19da42d2008-04-03 16:36:07 +00001133 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 }
Evan Cheng19da42d2008-04-03 16:36:07 +00001135 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
1136 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
1137 if (!SU->Node)
1138 EmitCrossRCCopy(SU, CopyVRBaseMap);
1139 else
1140 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 }
Evan Cheng8725a112008-03-12 22:19:41 +00001142
1143 if (isEntryBB && SchedLiveInCopies)
1144 EmitLiveInCopies(MF->begin());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145}
1146
1147/// dump - dump the schedule.
1148void ScheduleDAG::dumpSchedule() const {
1149 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1150 if (SUnit *SU = Sequence[i])
1151 SU->dump(&DAG);
1152 else
1153 cerr << "**** NOOP ****\n";
1154 }
1155}
1156
1157
1158/// Run - perform scheduling.
1159///
1160MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161 Schedule();
1162 return BB;
1163}
1164
1165/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1166/// a group of nodes flagged together.
1167void SUnit::dump(const SelectionDAG *G) const {
1168 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +00001169 if (Node)
1170 Node->dump(G);
1171 else
1172 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 cerr << "\n";
1174 if (FlaggedNodes.size() != 0) {
1175 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
1176 cerr << " ";
1177 FlaggedNodes[i]->dump(G);
1178 cerr << "\n";
1179 }
1180 }
1181}
1182
1183void SUnit::dumpAll(const SelectionDAG *G) const {
1184 dump(G);
1185
1186 cerr << " # preds left : " << NumPredsLeft << "\n";
1187 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 cerr << " Latency : " << Latency << "\n";
1189 cerr << " Depth : " << Depth << "\n";
1190 cerr << " Height : " << Height << "\n";
1191
1192 if (Preds.size() != 0) {
1193 cerr << " Predecessors:\n";
1194 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1195 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +00001196 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 cerr << " ch #";
1198 else
1199 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +00001200 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1201 if (I->isSpecial)
1202 cerr << " *";
1203 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 }
1205 }
1206 if (Succs.size() != 0) {
1207 cerr << " Successors:\n";
1208 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1209 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +00001210 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 cerr << " ch #";
1212 else
1213 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +00001214 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1215 if (I->isSpecial)
1216 cerr << " *";
1217 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001218 }
1219 }
1220 cerr << "\n";
1221}