blob: f144b986604d8bb640da56acc46b0aa0a2ddb9eb [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"
Reid Spencere5530da2007-01-12 23:31:12 +000017#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000020#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng8a50f1f2008-04-03 16:36:07 +000021#include "llvm/CodeGen/MachineInstrBuilder.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()) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +000045 TII = TM.getInstrInfo();
46 MF = &DAG.getMachineFunction();
47 TRI = TM.getRegisterInfo();
48 TLI = &DAG.getTargetLoweringInfo();
49 ConstPool = BB->getParent()->getConstantPool();
Chris Lattner84bc5422007-12-31 04:13:23 +000050}
Evan Chenga6fb1b62007-09-25 01:54:36 +000051
Evan Chenga6fb1b62007-09-25 01:54:36 +000052/// CheckForPhysRegDependency - Check if the dependency between def and use of
53/// a specified operand is a physical register dependency. If so, returns the
54/// register and the cost of copying the register.
55static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000056 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000057 const TargetInstrInfo *TII,
58 unsigned &PhysReg, int &Cost) {
59 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
60 return;
61
62 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000063 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000064 return;
65
66 unsigned ResNo = Use->getOperand(2).ResNo;
67 if (Def->isTargetOpcode()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000068 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000069 if (ResNo >= II.getNumDefs() &&
70 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000071 PhysReg = Reg;
72 const TargetRegisterClass *RC =
Evan Cheng676dd7c2008-03-11 07:19:34 +000073 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Chenga6fb1b62007-09-25 01:54:36 +000074 Cost = RC->getCopyCost();
75 }
76 }
77}
78
79SUnit *ScheduleDAG::Clone(SUnit *Old) {
80 SUnit *SU = NewSUnit(Old->Node);
Dan Gohman4c8c8302008-06-21 15:52:51 +000081 SU->OrigNode = Old->OrigNode;
Dan Gohman45f36ea2008-03-10 23:48:14 +000082 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Chenga6fb1b62007-09-25 01:54:36 +000083 SU->Latency = Old->Latency;
84 SU->isTwoAddress = Old->isTwoAddress;
85 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000086 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000087 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.
Dan Gohman3461cc92008-06-20 17:15:19 +000098 SUnits.reserve(DAG.allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +000099
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.
Dan Gohman4c8c8302008-06-21 15:52:51 +0000106 if (SUnitMap.count(NI)) 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);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000121 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
122 isNew = isNew;
123 assert(isNew && "Node already inserted!");
Evan Cheng3b97acd2006-08-07 22:12:12 +0000124 } while (N->getNumOperands() &&
125 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
126 std::reverse(NodeSUnit->FlaggedNodes.begin(),
127 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000128 }
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 Levensteindc1adac2008-04-07 10:06:32 +0000140 if (FlagVal.isOperandOf(UI->getUser())) {
Evan Chenge165a782006-05-11 23:55:42 +0000141 HasFlagUse = true;
142 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000143 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
144 isNew = isNew;
145 assert(isNew && "Node already inserted!");
Roman Levensteindc1adac2008-04-07 10:06:32 +0000146 N = UI->getUser();
Evan Chenge165a782006-05-11 23:55:42 +0000147 break;
148 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000149 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000150 }
151
152 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
153 // Update the SUnit
154 NodeSUnit->Node = N;
Dan Gohman4c8c8302008-06-21 15:52:51 +0000155 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
156 isNew = isNew;
157 assert(isNew && "Node already inserted!");
Evan Chengf10c9732007-10-05 01:39:18 +0000158
159 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000160 }
161
162 // Pass 2: add the preds, succs, etc.
163 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
164 SUnit *SU = &SUnits[su];
165 SDNode *MainNode = SU->Node;
166
167 if (MainNode->isTargetOpcode()) {
168 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000169 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000170 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000171 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000172 SU->isTwoAddress = true;
173 break;
174 }
175 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000176 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000177 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000178 }
179
180 // Find all predecessors and successors of the group.
181 // Temporarily add N to make code simpler.
182 SU->FlaggedNodes.push_back(MainNode);
183
184 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
185 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000186 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000187 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
188 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000189 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000190
191 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
192 SDNode *OpN = N->getOperand(i).Val;
193 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman4c8c8302008-06-21 15:52:51 +0000194 SUnit *OpSU = SUnitMap[OpN];
Evan Chenge165a782006-05-11 23:55:42 +0000195 assert(OpSU && "Node has no SUnit!");
196 if (OpSU == SU) continue; // In the same group.
197
Duncan Sands83ec4b62008-06-06 12:08:01 +0000198 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000199 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
200 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000201
202 unsigned PhysReg = 0;
203 int Cost = 1;
204 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000205 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000206 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000207 }
208 }
209
210 // Remove MainNode from FlaggedNodes again.
211 SU->FlaggedNodes.pop_back();
212 }
213
214 return;
215}
216
Evan Chengf10c9732007-10-05 01:39:18 +0000217void ScheduleDAG::ComputeLatency(SUnit *SU) {
218 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
219
220 // Compute the latency for the node. We use the sum of the latencies for
221 // all nodes flagged together into this SUnit.
222 if (InstrItins.isEmpty()) {
223 // No latency information.
224 SU->Latency = 1;
225 } else {
226 SU->Latency = 0;
227 if (SU->Node->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000228 unsigned SchedClass =
229 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000230 const InstrStage *S = InstrItins.begin(SchedClass);
231 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000232 for (; S != E; ++S)
233 SU->Latency += S->Cycles;
234 }
235 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
236 SDNode *FNode = SU->FlaggedNodes[i];
237 if (FNode->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000238 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000239 const InstrStage *S = InstrItins.begin(SchedClass);
240 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000241 for (; S != E; ++S)
242 SU->Latency += S->Cycles;
243 }
244 }
245 }
246}
247
Roman Levensteind86449e2008-03-04 11:19:43 +0000248/// CalculateDepths - compute depths using algorithms for the longest
249/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000250void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000251 unsigned DAGSize = SUnits.size();
252 std::vector<unsigned> InDegree(DAGSize);
253 std::vector<SUnit*> WorkList;
254 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000255
Roman Levensteind86449e2008-03-04 11:19:43 +0000256 // Initialize the data structures
257 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
258 SUnit *SU = &SUnits[i];
259 int NodeNum = SU->NodeNum;
260 unsigned Degree = SU->Preds.size();
261 InDegree[NodeNum] = Degree;
262 SU->Depth = 0;
263
264 // Is it a node without dependencies?
265 if (Degree == 0) {
266 assert(SU->Preds.empty() && "SUnit should have no predecessors");
267 // Collect leaf nodes
268 WorkList.push_back(SU);
269 }
270 }
271
272 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000273 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000274 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000275 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000276 unsigned &SUDepth = SU->Depth;
277
278 // Use dynamic programming:
279 // When current node is being processed, all of its dependencies
280 // are already processed.
281 // So, just iterate over all predecessors and take the longest path
282 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
283 I != E; ++I) {
284 unsigned PredDepth = I->Dep->Depth;
285 if (PredDepth+1 > SUDepth) {
286 SUDepth = PredDepth + 1;
287 }
288 }
289
290 // Update InDegrees of all nodes depending on current SUnit
291 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
292 I != E; ++I) {
293 SUnit *SU = I->Dep;
294 if (!--InDegree[SU->NodeNum])
295 // If all dependencies of the node are processed already,
296 // then the longest path for the node can be computed now
297 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000298 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000299 }
Evan Chenge165a782006-05-11 23:55:42 +0000300}
Evan Cheng99126282007-07-06 01:37:28 +0000301
Roman Levensteind86449e2008-03-04 11:19:43 +0000302/// CalculateHeights - compute heights using algorithms for the longest
303/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000304void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000305 unsigned DAGSize = SUnits.size();
306 std::vector<unsigned> InDegree(DAGSize);
307 std::vector<SUnit*> WorkList;
308 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000309
Roman Levensteind86449e2008-03-04 11:19:43 +0000310 // Initialize the data structures
311 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
312 SUnit *SU = &SUnits[i];
313 int NodeNum = SU->NodeNum;
314 unsigned Degree = SU->Succs.size();
315 InDegree[NodeNum] = Degree;
316 SU->Height = 0;
317
318 // Is it a node without dependencies?
319 if (Degree == 0) {
320 assert(SU->Succs.empty() && "Something wrong");
321 assert(WorkList.empty() && "Should be empty");
322 // Collect leaf nodes
323 WorkList.push_back(SU);
324 }
325 }
326
327 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000328 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000329 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000330 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000331 unsigned &SUHeight = SU->Height;
332
333 // Use dynamic programming:
334 // When current node is being processed, all of its dependencies
335 // are already processed.
336 // So, just iterate over all successors and take the longest path
337 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
338 I != E; ++I) {
339 unsigned SuccHeight = I->Dep->Height;
340 if (SuccHeight+1 > SUHeight) {
341 SUHeight = SuccHeight + 1;
342 }
343 }
344
345 // Update InDegrees of all nodes depending on current SUnit
346 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
347 I != E; ++I) {
348 SUnit *SU = I->Dep;
349 if (!--InDegree[SU->NodeNum])
350 // If all dependencies of the node are processed already,
351 // then the longest path for the node can be computed now
352 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000353 }
354 }
Evan Chenge165a782006-05-11 23:55:42 +0000355}
356
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000357/// CountResults - The results of target nodes have register or immediate
358/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000359/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000360unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000361 unsigned N = Node->getNumValues();
362 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000363 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000364 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000365 --N; // Skip over chain result.
366 return N;
367}
368
Dan Gohman69de1932008-02-06 22:27:42 +0000369/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000370/// followed by special operands that describe memory references, then an
371/// optional chain operand, then flag operands. Compute the number of
372/// actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000373unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000374 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohmancc20cd52008-02-11 19:00:34 +0000375 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman36b5c132008-04-07 19:35:22 +0000376 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000377 return N;
378}
379
Dan Gohman42a77882008-02-16 00:36:48 +0000380/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
381/// operand
382unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000383 unsigned N = Node->getNumOperands();
384 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
385 --N;
386 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
387 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000388 return N;
389}
390
Jim Laskey60f09922006-07-21 20:57:35 +0000391static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman6f0d0242008-02-10 18:45:23 +0000392 const TargetRegisterInfo *TRI,
Jim Laskey60f09922006-07-21 20:57:35 +0000393 const TargetInstrInfo *TII,
Chris Lattner749c6f62008-01-07 07:27:27 +0000394 const TargetInstrDesc &II,
Jim Laskey60f09922006-07-21 20:57:35 +0000395 unsigned Op) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000396 if (Op >= II.getNumOperands()) {
397 assert(II.isVariadic() && "Invalid operand # of instruction");
Jim Laskey60f09922006-07-21 20:57:35 +0000398 return NULL;
399 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000400 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattner8ca5c672008-01-07 02:39:19 +0000401 return TII->getPointerRegClass();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000402 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Jim Laskey60f09922006-07-21 20:57:35 +0000403}
404
Evan Chenga6fb1b62007-09-25 01:54:36 +0000405void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
Dan Gohman4c8c8302008-06-21 15:52:51 +0000406 bool IsClone, unsigned SrcReg,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000407 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng84097472007-08-02 00:28:15 +0000408 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000409 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000410 // Just use the input register directly!
Dan Gohman4c8c8302008-06-21 15:52:51 +0000411 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000412 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000413 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
Evan Cheng97e60d92008-05-14 21:08:07 +0000414 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000415 assert(isNew && "Node emitted out of order - early");
416 return;
417 }
418
419 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
420 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000421 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000422 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
423 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000424 SDNode *Use = UI->getUser();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000425 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000426 if (Use->getOpcode() == ISD::CopyToReg &&
427 Use->getOperand(2).Val == Node &&
428 Use->getOperand(2).ResNo == ResNo) {
429 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000430 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000431 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000432 Match = false;
433 } else if (DestReg != SrcReg)
434 Match = false;
435 } else {
436 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
437 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000438 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000439 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000440 MVT VT = Node->getValueType(Op.ResNo);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000441 if (VT != MVT::Other && VT != MVT::Flag)
442 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000443 }
444 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000445 MatchReg &= Match;
446 if (VRBase)
447 break;
Evan Cheng84097472007-08-02 00:28:15 +0000448 }
449
Chris Lattner02b6d252008-03-09 08:49:15 +0000450 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000451 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000452
Evan Chenga6fb1b62007-09-25 01:54:36 +0000453 // Figure out the register class to create for the destreg.
Chris Lattner02b6d252008-03-09 08:49:15 +0000454 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000455 DstRC = MRI.getRegClass(VRBase);
Chris Lattner02b6d252008-03-09 08:49:15 +0000456 } else {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000457 DstRC = TLI->getRegClassFor(Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000458 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000459
460 // If all uses are reading from the src physical register and copying the
461 // register is either impossible or very expensive, then don't create a copy.
Chris Lattner02b6d252008-03-09 08:49:15 +0000462 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000463 VRBase = SrcReg;
464 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000465 // Create the reg, emit the copy.
Evan Cheng9e233362008-03-12 22:19:41 +0000466 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattner02b6d252008-03-09 08:49:15 +0000467 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng84097472007-08-02 00:28:15 +0000468 }
Evan Cheng84097472007-08-02 00:28:15 +0000469
Dan Gohman4c8c8302008-06-21 15:52:51 +0000470 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000471 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000472 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000473 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000474 assert(isNew && "Node emitted out of order - early");
475}
476
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000477/// getDstOfCopyToRegUse - If the only use of the specified result number of
478/// node is a CopyToReg, return its destination register. Return 0 otherwise.
479unsigned ScheduleDAG::getDstOfOnlyCopyToRegUse(SDNode *Node,
480 unsigned ResNo) const {
481 if (!Node->hasOneUse())
482 return 0;
483
Roman Levensteindc1adac2008-04-07 10:06:32 +0000484 SDNode *Use = Node->use_begin()->getUser();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000485 if (Use->getOpcode() == ISD::CopyToReg &&
486 Use->getOperand(2).Val == Node &&
487 Use->getOperand(2).ResNo == ResNo) {
488 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
489 if (TargetRegisterInfo::isVirtualRegister(Reg))
490 return Reg;
491 }
492 return 0;
493}
494
Evan Chengda47e6e2008-03-15 00:03:38 +0000495void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000496 const TargetInstrDesc &II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000497 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000498 assert(Node->getTargetOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
499 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
500
Chris Lattner349c4952008-01-07 03:13:06 +0000501 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000502 // If the specific node value is only used by a CopyToReg and the dest reg
503 // is a vreg, use the CopyToReg'd destination register instead of creating
504 // a new vreg.
505 unsigned VRBase = 0;
506 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
507 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000508 SDNode *Use = UI->getUser();
Evan Chengaf825c82007-07-10 07:08:32 +0000509 if (Use->getOpcode() == ISD::CopyToReg &&
510 Use->getOperand(2).Val == Node &&
511 Use->getOperand(2).ResNo == i) {
512 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000513 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000514 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000515 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000516 break;
517 }
518 }
519 }
520
Evan Cheng84097472007-08-02 00:28:15 +0000521 // Create the result registers for this node and add the result regs to
522 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000523 if (VRBase == 0) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000524 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000525 assert(RC && "Isn't a register operand!");
Evan Cheng9e233362008-03-12 22:19:41 +0000526 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000527 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000528 }
529
530 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000531 isNew = isNew; // Silence compiler warning.
Evan Chengaf825c82007-07-10 07:08:32 +0000532 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000533 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000534}
535
Chris Lattnerdf375062006-03-10 07:25:12 +0000536/// getVR - Return the virtual register corresponding to the specified result
537/// of the specified node.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000538unsigned ScheduleDAG::getVR(SDOperand Op,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000539 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000540 if (Op.isTargetOpcode() &&
541 Op.getTargetOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
542 // Add an IMPLICIT_DEF instruction before every use.
543 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.Val, Op.ResNo);
544 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
545 // does not include operand register class info.
546 if (!VReg) {
547 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
548 VReg = MRI.createVirtualRegister(RC);
549 }
550 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
551 return VReg;
552 }
553
Roman Levenstein9cac5252008-04-16 16:15:27 +0000554 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000555 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000556 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000557}
558
559
Chris Lattnered18b682006-02-24 18:54:03 +0000560/// AddOperand - Add the specified operand to the specified machine instr. II
561/// specifies the instruction information for the node, and IIOpNum is the
562/// operand number (in the II) that we are adding. IIOpNum and II are used for
563/// assertions only.
564void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
565 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000566 const TargetInstrDesc *II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000567 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000568 if (Op.isTargetOpcode()) {
569 // Note that this case is redundant with the final else block, but we
570 // include it because it is the most common and it makes the logic
571 // simpler here.
572 assert(Op.getValueType() != MVT::Other &&
573 Op.getValueType() != MVT::Flag &&
574 "Chain and flag operands should occur at end of operand list!");
Chris Lattnered18b682006-02-24 18:54:03 +0000575 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000576 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000577 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000578 bool isOptDef = IIOpNum < TID.getNumOperands() &&
579 TID.OpInfo[IIOpNum].isOptionalDef();
Chris Lattner8019f412007-12-30 00:41:17 +0000580 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000581
582 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000583 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerb7795802008-03-11 00:59:28 +0000584#ifndef NDEBUG
Chris Lattnered18b682006-02-24 18:54:03 +0000585 if (II) {
Chris Lattnerb7795802008-03-11 00:59:28 +0000586 // There may be no register class for this operand if it is a variadic
587 // argument (RC will be NULL in this case). In this case, we just assume
588 // the regclass is ok.
Jim Laskey60f09922006-07-21 20:57:35 +0000589 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000590 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000591 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng9e233362008-03-12 22:19:41 +0000592 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerb7795802008-03-11 00:59:28 +0000593 if (RC && VRC != RC) {
Chris Lattner01528292007-02-15 18:17:56 +0000594 cerr << "Register class of operand and regclass of use don't agree!\n";
Chris Lattner01528292007-02-15 18:17:56 +0000595 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000596 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000597 cerr << "MI = "; MI->print(cerr);
598 cerr << "VReg = " << VReg << "\n";
599 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000600 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000601 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000602 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000603 cerr << "Fatal error, aborting.\n";
604 abort();
605 }
Chris Lattnered18b682006-02-24 18:54:03 +0000606 }
Chris Lattnerb7795802008-03-11 00:59:28 +0000607#endif
Chris Lattnerfec65d52007-12-30 00:51:11 +0000608 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000609 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000610 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Chris Lattner02a260a2008-04-20 00:41:09 +0000611 ConstantFP *CFP = ConstantFP::get(F->getValueAPF());
Nate Begemane1795842008-02-14 08:57:00 +0000612 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000613 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000614 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000615 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
616 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
617 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
618 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
619 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
620 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
621 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
622 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
623 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000624 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000625 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000626 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000627 // MachineConstantPool wants an explicit alignment.
628 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000629 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000630 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000631 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000632 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000633 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000634 }
Chris Lattnered18b682006-02-24 18:54:03 +0000635 }
636
Evan Chengd6594ae2006-09-12 21:00:35 +0000637 unsigned Idx;
638 if (CP->isMachineConstantPoolEntry())
639 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
640 else
641 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000642 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
643 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
644 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000645 } else {
646 assert(Op.getValueType() != MVT::Other &&
647 Op.getValueType() != MVT::Flag &&
648 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000649 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000650 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000651
Chris Lattner02b6d252008-03-09 08:49:15 +0000652 // Verify that it is right. Note that the reg class of the physreg and the
653 // vreg don't necessarily need to match, but the target copy insertion has
654 // to be able to handle it. This handles things like copies from ST(0) to
655 // an FP vreg on x86.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000656 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000657 if (II && !II->isVariadic()) {
Chris Lattner02b6d252008-03-09 08:49:15 +0000658 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
659 "Don't have operand info for this instruction!");
Chris Lattnered18b682006-02-24 18:54:03 +0000660 }
661 }
662
663}
664
Dan Gohman36b5c132008-04-07 19:35:22 +0000665void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +0000666 MI->addMemOperand(MO);
667}
668
Christopher Lambe24f8f12007-07-26 08:12:07 +0000669// Returns the Register Class of a subregister
670static const TargetRegisterClass *getSubRegisterRegClass(
671 const TargetRegisterClass *TRC,
672 unsigned SubIdx) {
673 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000674 TargetRegisterInfo::regclass_iterator I =
675 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000676 assert(I < TRC->subregclasses_end() &&
677 "Invalid subregister index for register class");
678 return *I;
679}
680
681static const TargetRegisterClass *getSuperregRegisterClass(
682 const TargetRegisterClass *TRC,
683 unsigned SubIdx,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000684 MVT VT) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000685 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000686 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000687 E = TRC->superregclasses_end(); I != E; ++I)
688 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
689 return *I;
690 assert(false && "Couldn't find the register class");
691 return 0;
692}
693
694/// EmitSubregNode - Generate machine code for subreg nodes.
695///
696void ScheduleDAG::EmitSubregNode(SDNode *Node,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000697 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000698 unsigned VRBase = 0;
699 unsigned Opc = Node->getTargetOpcode();
Christopher Lambc9298232008-03-16 03:12:01 +0000700
701 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
702 // the CopyToReg'd destination register instead of creating a new vreg.
703 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
704 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000705 SDNode *Use = UI->getUser();
Christopher Lambc9298232008-03-16 03:12:01 +0000706 if (Use->getOpcode() == ISD::CopyToReg &&
707 Use->getOperand(2).Val == Node) {
708 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
709 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
710 VRBase = DestReg;
711 break;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000712 }
713 }
Christopher Lambc9298232008-03-16 03:12:01 +0000714 }
715
716 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000717 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000718
Christopher Lambe24f8f12007-07-26 08:12:07 +0000719 // Create the extract_subreg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000720 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000721
722 // Figure out the register class to create for the destreg.
723 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng9e233362008-03-12 22:19:41 +0000724 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000725 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
726
727 if (VRBase) {
728 // Grab the destination register
Evan Cheng50871242008-05-14 20:07:51 +0000729#ifndef NDEBUG
Evan Cheng9e233362008-03-12 22:19:41 +0000730 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000731 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000732 "Source subregister and destination must have the same class");
Evan Cheng50871242008-05-14 20:07:51 +0000733#endif
Christopher Lambe24f8f12007-07-26 08:12:07 +0000734 } else {
735 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000736 assert(SRC && "Couldn't find source register class");
Evan Cheng9e233362008-03-12 22:19:41 +0000737 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000738 }
739
740 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000741 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000742 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000743 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000744 BB->push_back(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000745 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
746 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000747 SDOperand N0 = Node->getOperand(0);
748 SDOperand N1 = Node->getOperand(1);
749 SDOperand N2 = Node->getOperand(2);
750 unsigned SubReg = getVR(N1, VRBaseMap);
751 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000752
Christopher Lambe24f8f12007-07-26 08:12:07 +0000753
754 // Figure out the register class to create for the destreg.
755 const TargetRegisterClass *TRC = 0;
756 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000757 TRC = MRI.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000758 } else {
Evan Cheng9e233362008-03-12 22:19:41 +0000759 TRC = getSuperregRegisterClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000760 Node->getValueType(0));
761 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng9e233362008-03-12 22:19:41 +0000762 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000763 }
764
Christopher Lambc9298232008-03-16 03:12:01 +0000765 // Create the insert_subreg or subreg_to_reg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000766 MachineInstr *MI = BuildMI(TII->get(Opc));
Chris Lattner8019f412007-12-30 00:41:17 +0000767 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000768
Christopher Lambc9298232008-03-16 03:12:01 +0000769 // If creating a subreg_to_reg, then the first input operand
770 // is an implicit value immediate, otherwise it's a register
771 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
772 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000773 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
Christopher Lambc9298232008-03-16 03:12:01 +0000774 } else
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000775 AddOperand(MI, N0, 0, 0, VRBaseMap);
776 // Add the subregster being inserted
777 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000778 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000779 BB->push_back(MI);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000780 } else
Christopher Lambc9298232008-03-16 03:12:01 +0000781 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Christopher Lambe24f8f12007-07-26 08:12:07 +0000782
783 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000784 isNew = isNew; // Silence compiler warning.
Christopher Lambe24f8f12007-07-26 08:12:07 +0000785 assert(isNew && "Node emitted out of order - early");
786}
787
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000788/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000789///
Dan Gohman4c8c8302008-06-21 15:52:51 +0000790void ScheduleDAG::EmitNode(SDNode *Node, bool IsClone,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000791 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000792 // If machine instruction
793 if (Node->isTargetOpcode()) {
794 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000795
796 // Handle subreg insert/extract specially
797 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Christopher Lambc9298232008-03-16 03:12:01 +0000798 Opc == TargetInstrInfo::INSERT_SUBREG ||
799 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000800 EmitSubregNode(Node, VRBaseMap);
801 return;
802 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000803
804 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
805 // We want a unique VR for each IMPLICIT_DEF use.
806 return;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000807
Chris Lattner749c6f62008-01-07 07:27:27 +0000808 const TargetInstrDesc &II = TII->get(Opc);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000809 unsigned NumResults = CountResults(Node);
810 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000811 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Chris Lattner349c4952008-01-07 03:13:06 +0000812 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
813 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000814#ifndef NDEBUG
Evan Cheng50871242008-05-14 20:07:51 +0000815 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000816 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000817 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000818 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000819#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000820
821 // Create the new machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000822 MachineInstr *MI = BuildMI(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000823
824 // Add result register values for things that are defined by this
825 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000826 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000827 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000828
829 // Emit all of the actual operands of this instruction, adding them to the
830 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000831 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000832 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000833
Dan Gohman69de1932008-02-06 22:27:42 +0000834 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000835 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000836 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
837
Evan Cheng13d41b92006-05-12 01:58:24 +0000838 // Commute node if it has been determined to be profitable.
839 if (CommuteSet.count(Node)) {
840 MachineInstr *NewMI = TII->commuteInstruction(MI);
841 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000842 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000843 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000844 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000845 if (MI != NewMI) {
846 delete MI;
847 MI = NewMI;
848 }
Evan Cheng643afa52008-02-28 07:40:24 +0000849 ++NumCommutes;
Evan Cheng13d41b92006-05-12 01:58:24 +0000850 }
851 }
852
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000853 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000854 // Insert this instruction into the basic block using a target
855 // specific inserter which may returns a new basic block.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000856 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000857 else
858 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000859
860 // Additional results must be an physical register def.
861 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000862 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
863 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000864 if (Node->hasAnyUseOfValue(i))
Dan Gohman4c8c8302008-06-21 15:52:51 +0000865 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000866 }
867 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000868 } else {
869 switch (Node->getOpcode()) {
870 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000871#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000872 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000873#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000874 assert(0 && "This target-independent node should have been selected!");
Dan Gohman80792f32008-04-15 01:22:18 +0000875 break;
876 case ISD::EntryToken:
877 assert(0 && "EntryToken should have been excluded from the schedule!");
878 break;
879 case ISD::TokenFactor: // fall thru
Jim Laskey1ee29252007-01-26 14:34:52 +0000880 case ISD::LABEL:
Evan Chenga844bde2008-02-02 04:07:54 +0000881 case ISD::DECLARE:
Dan Gohman69de1932008-02-06 22:27:42 +0000882 case ISD::SRCVALUE:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000883 break;
884 case ISD::CopyToReg: {
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000885 unsigned SrcReg;
886 SDOperand SrcVal = Node->getOperand(2);
887 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
888 SrcReg = R->getReg();
Evan Cheng489a87c2007-01-05 20:59:06 +0000889 else
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000890 SrcReg = getVR(SrcVal, VRBaseMap);
891
Chris Lattnera4176522005-10-30 18:54:27 +0000892 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000893 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
894 break;
895
896 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
897 // Get the register classes of the src/dst.
898 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000899 SrcTRC = MRI.getRegClass(SrcReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000900 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000901 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000902
903 if (TargetRegisterInfo::isVirtualRegister(DestReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000904 DstTRC = MRI.getRegClass(DestReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000905 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000906 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
907 Node->getOperand(1).getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000908 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000909 break;
910 }
911 case ISD::CopyFromReg: {
912 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Dan Gohman4c8c8302008-06-21 15:52:51 +0000913 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000914 break;
915 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000916 case ISD::INLINEASM: {
917 unsigned NumOps = Node->getNumOperands();
918 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
919 --NumOps; // Ignore the flag operand.
920
921 // Create the inline asm machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000922 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000923
924 // Add the asm string as an external symbol operand.
925 const char *AsmStr =
926 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000927 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000928
929 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000930 for (unsigned i = 2; i != NumOps;) {
931 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000932 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000933
Chris Lattnerfec65d52007-12-30 00:51:11 +0000934 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000935 ++i; // Skip the ID value.
936
937 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000938 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000939 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000940 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000941 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000942 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000943 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000944 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000945 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000946 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000947 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000948 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000949 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000950 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000951 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000952 for (; NumVals; --NumVals, ++i) {
953 if (ConstantSDNode *CS =
954 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000955 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000956 } else if (GlobalAddressSDNode *GA =
957 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000958 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
959 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000960 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000961 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
962 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000963 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000964 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000965 break;
966 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000967 case 4: // Addressing mode.
968 // The addressing mode has been selected, just add all of the
969 // operands to the machine instruction.
970 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000971 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000972 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000973 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000974 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000975 BB->push_back(MI);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000976 break;
977 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000978 }
979 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000980}
981
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000982void ScheduleDAG::EmitNoop() {
983 TII->insertNoop(*BB, BB->end());
984}
985
Chris Lattnerd9c4c452008-03-09 07:51:01 +0000986void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
987 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng42d60272007-09-26 21:36:17 +0000988 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
989 I != E; ++I) {
990 if (I->isCtrl) continue; // ignore chain preds
991 if (!I->Dep->Node) {
992 // Copy to physical register.
993 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
994 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
995 // Find the destination physical register.
996 unsigned Reg = 0;
997 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
998 EE = SU->Succs.end(); II != EE; ++II) {
999 if (I->Reg) {
1000 Reg = I->Reg;
1001 break;
1002 }
1003 }
1004 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +00001005 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +00001006 SU->CopyDstRC, SU->CopySrcRC);
1007 } else {
1008 // Copy from physical register.
1009 assert(I->Reg && "Unknown physical register!");
Evan Cheng9e233362008-03-12 22:19:41 +00001010 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Evan Cheng42d60272007-09-26 21:36:17 +00001011 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +00001012 isNew = isNew; // Silence compiler warning.
Evan Cheng42d60272007-09-26 21:36:17 +00001013 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +00001014 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +00001015 SU->CopyDstRC, SU->CopySrcRC);
1016 }
1017 break;
1018 }
1019}
1020
Evan Cheng9e233362008-03-12 22:19:41 +00001021/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1022/// physical register has only a single copy use, then coalesced the copy
Evan Chengdb2d7732008-03-14 00:14:55 +00001023/// if possible.
1024void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
1025 MachineBasicBlock::iterator &InsertPos,
1026 unsigned VirtReg, unsigned PhysReg,
1027 const TargetRegisterClass *RC,
1028 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng9e233362008-03-12 22:19:41 +00001029 unsigned NumUses = 0;
1030 MachineInstr *UseMI = NULL;
1031 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1032 UE = MRI.use_end(); UI != UE; ++UI) {
1033 UseMI = &*UI;
1034 if (++NumUses > 1)
1035 break;
1036 }
1037
1038 // If the number of uses is not one, or the use is not a move instruction,
Evan Chengdb2d7732008-03-14 00:14:55 +00001039 // don't coalesce. Also, only coalesce away a virtual register to virtual
1040 // register copy.
1041 bool Coalesced = false;
Evan Cheng9e233362008-03-12 22:19:41 +00001042 unsigned SrcReg, DstReg;
Evan Chengdb2d7732008-03-14 00:14:55 +00001043 if (NumUses == 1 &&
1044 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1045 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1046 VirtReg = DstReg;
1047 Coalesced = true;
Evan Cheng9e233362008-03-12 22:19:41 +00001048 }
1049
Evan Chengdb2d7732008-03-14 00:14:55 +00001050 // Now find an ideal location to insert the copy.
1051 MachineBasicBlock::iterator Pos = InsertPos;
1052 while (Pos != MBB->begin()) {
1053 MachineInstr *PrevMI = prior(Pos);
1054 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1055 // copyRegToReg might emit multiple instructions to do a copy.
1056 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1057 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1058 // This is what the BB looks like right now:
1059 // r1024 = mov r0
1060 // ...
1061 // r1 = mov r1024
1062 //
1063 // We want to insert "r1025 = mov r1". Inserting this copy below the
1064 // move to r1024 makes it impossible for that move to be coalesced.
1065 //
1066 // r1025 = mov r1
1067 // r1024 = mov r0
1068 // ...
1069 // r1 = mov 1024
1070 // r2 = mov 1025
1071 break; // Woot! Found a good location.
1072 --Pos;
1073 }
1074
1075 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1076 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1077 if (Coalesced) {
Evan Cheng9e233362008-03-12 22:19:41 +00001078 if (&*InsertPos == UseMI) ++InsertPos;
1079 MBB->erase(UseMI);
Evan Cheng9e233362008-03-12 22:19:41 +00001080 }
Evan Cheng9e233362008-03-12 22:19:41 +00001081}
1082
1083/// EmitLiveInCopies - If this is the first basic block in the function,
1084/// and if it has live ins that need to be copied into vregs, emit the
1085/// copies into the top of the block.
1086void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chengdb2d7732008-03-14 00:14:55 +00001087 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng9e233362008-03-12 22:19:41 +00001088 MachineBasicBlock::iterator InsertPos = MBB->begin();
1089 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1090 E = MRI.livein_end(); LI != E; ++LI)
1091 if (LI->second) {
1092 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chengdb2d7732008-03-14 00:14:55 +00001093 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng9e233362008-03-12 22:19:41 +00001094 }
1095}
1096
Evan Chenge165a782006-05-11 23:55:42 +00001097/// EmitSchedule - Emit the machine code in scheduled order.
1098void ScheduleDAG::EmitSchedule() {
Evan Cheng9e233362008-03-12 22:19:41 +00001099 bool isEntryBB = &MF->front() == BB;
1100
1101 if (isEntryBB && !SchedLiveInCopies) {
1102 // If this is the first basic block in the function, and if it has live ins
1103 // that need to be copied into vregs, emit the copies into the top of the
1104 // block before emitting the code for the block.
1105 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1106 E = MRI.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +00001107 if (LI->second) {
Evan Cheng9e233362008-03-12 22:19:41 +00001108 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +00001109 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +00001110 LI->first, RC, RC);
1111 }
Chris Lattner96645412006-05-16 06:10:58 +00001112 }
Evan Cheng9e233362008-03-12 22:19:41 +00001113
Chris Lattner96645412006-05-16 06:10:58 +00001114 // Finally, emit the code for all of the scheduled instructions.
Roman Levenstein9cac5252008-04-16 16:15:27 +00001115 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +00001116 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +00001117 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001118 SUnit *SU = Sequence[i];
1119 if (!SU) {
Evan Chenge165a782006-05-11 23:55:42 +00001120 // Null SUnit* is a noop.
1121 EmitNoop();
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001122 continue;
Evan Chenge165a782006-05-11 23:55:42 +00001123 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001124 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
Dan Gohman4c8c8302008-06-21 15:52:51 +00001125 EmitNode(SU->FlaggedNodes[j], SU->OrigNode != SU, VRBaseMap);
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001126 if (!SU->Node)
1127 EmitCrossRCCopy(SU, CopyVRBaseMap);
1128 else
Dan Gohman4c8c8302008-06-21 15:52:51 +00001129 EmitNode(SU->Node, SU->OrigNode != SU, VRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +00001130 }
Evan Cheng9e233362008-03-12 22:19:41 +00001131
1132 if (isEntryBB && SchedLiveInCopies)
1133 EmitLiveInCopies(MF->begin());
Evan Chenge165a782006-05-11 23:55:42 +00001134}
1135
1136/// dump - dump the schedule.
1137void ScheduleDAG::dumpSchedule() const {
1138 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1139 if (SUnit *SU = Sequence[i])
1140 SU->dump(&DAG);
1141 else
Bill Wendling832171c2006-12-07 20:04:42 +00001142 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +00001143 }
1144}
1145
1146
Evan Chenga9c20912006-01-21 02:32:06 +00001147/// Run - perform scheduling.
1148///
1149MachineBasicBlock *ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +00001150 Schedule();
1151 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +00001152}
Evan Cheng4ef10862006-01-23 07:01:07 +00001153
Evan Chenge165a782006-05-11 23:55:42 +00001154/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1155/// a group of nodes flagged together.
1156void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00001157 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +00001158 if (Node)
1159 Node->dump(G);
1160 else
1161 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +00001162 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001163 if (FlaggedNodes.size() != 0) {
1164 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +00001165 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +00001166 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +00001167 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001168 }
1169 }
1170}
Evan Cheng4ef10862006-01-23 07:01:07 +00001171
Evan Chenge165a782006-05-11 23:55:42 +00001172void SUnit::dumpAll(const SelectionDAG *G) const {
1173 dump(G);
1174
Bill Wendling832171c2006-12-07 20:04:42 +00001175 cerr << " # preds left : " << NumPredsLeft << "\n";
1176 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +00001177 cerr << " Latency : " << Latency << "\n";
1178 cerr << " Depth : " << Depth << "\n";
1179 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001180
1181 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001182 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001183 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1184 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001185 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001186 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001187 else
Bill Wendling832171c2006-12-07 20:04:42 +00001188 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001189 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1190 if (I->isSpecial)
1191 cerr << " *";
1192 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001193 }
1194 }
1195 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001196 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001197 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1198 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001199 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001200 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001201 else
Bill Wendling832171c2006-12-07 20:04:42 +00001202 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001203 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1204 if (I->isSpecial)
1205 cerr << " *";
1206 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001207 }
1208 }
Bill Wendling832171c2006-12-07 20:04:42 +00001209 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001210}