blob: 982bbab3cb280090ca92be12433c01834b89246c [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//
Dan Gohman5e76c3b2008-07-11 22:39:58 +000010// This implements the ScheduleDAG class, which is a base class used by
11// scheduling implementation classes.
Chris Lattnerd32b2362005-08-18 18:45:24 +000012//
13//===----------------------------------------------------------------------===//
14
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000015#define DEBUG_TYPE "pre-RA-sched"
Reid Spencere5530da2007-01-12 23:31:12 +000016#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000017#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000019#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng8a50f1f2008-04-03 16:36:07 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000025#include "llvm/Target/TargetLowering.h"
Evan Cheng643afa52008-02-28 07:40:24 +000026#include "llvm/ADT/Statistic.h"
Evan Cheng9e233362008-03-12 22:19:41 +000027#include "llvm/Support/CommandLine.h"
Evan Chenge165a782006-05-11 23:55:42 +000028#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000030using namespace llvm;
31
Evan Cheng643afa52008-02-28 07:40:24 +000032STATISTIC(NumCommutes, "Number of instructions commuted");
33
Evan Cheng9e233362008-03-12 22:19:41 +000034namespace {
35 static cl::opt<bool>
36 SchedLiveInCopies("schedule-livein-copies",
37 cl::desc("Schedule copies of livein registers"),
38 cl::init(false));
39}
40
Chris Lattner84bc5422007-12-31 04:13:23 +000041ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
42 const TargetMachine &tm)
Evan Cheng9e233362008-03-12 22:19:41 +000043 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +000044 TII = TM.getInstrInfo();
45 MF = &DAG.getMachineFunction();
46 TRI = TM.getRegisterInfo();
47 TLI = &DAG.getTargetLoweringInfo();
48 ConstPool = BB->getParent()->getConstantPool();
Chris Lattner84bc5422007-12-31 04:13:23 +000049}
Evan Chenga6fb1b62007-09-25 01:54:36 +000050
Evan Chenga6fb1b62007-09-25 01:54:36 +000051/// CheckForPhysRegDependency - Check if the dependency between def and use of
52/// a specified operand is a physical register dependency. If so, returns the
53/// register and the cost of copying the register.
54static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000055 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000056 const TargetInstrInfo *TII,
57 unsigned &PhysReg, int &Cost) {
58 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
59 return;
60
61 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000062 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000063 return;
64
65 unsigned ResNo = Use->getOperand(2).ResNo;
66 if (Def->isTargetOpcode()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000067 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000068 if (ResNo >= II.getNumDefs() &&
69 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000070 PhysReg = Reg;
71 const TargetRegisterClass *RC =
Evan Cheng676dd7c2008-03-11 07:19:34 +000072 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Chenga6fb1b62007-09-25 01:54:36 +000073 Cost = RC->getCopyCost();
74 }
75 }
76}
77
78SUnit *ScheduleDAG::Clone(SUnit *Old) {
79 SUnit *SU = NewSUnit(Old->Node);
Dan Gohman4c8c8302008-06-21 15:52:51 +000080 SU->OrigNode = Old->OrigNode;
Dan Gohman45f36ea2008-03-10 23:48:14 +000081 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Chenga6fb1b62007-09-25 01:54:36 +000082 SU->Latency = Old->Latency;
83 SU->isTwoAddress = Old->isTwoAddress;
84 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000085 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000086 return SU;
87}
88
Evan Chengf10c9732007-10-05 01:39:18 +000089
Evan Chenge165a782006-05-11 23:55:42 +000090/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
91/// This SUnit graph is similar to the SelectionDAG, but represents flagged
92/// together nodes with a single SUnit.
93void ScheduleDAG::BuildSchedUnits() {
94 // Reserve entries in the vector for each of the SUnits we are creating. This
95 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
96 // invalidated.
Dan Gohman3461cc92008-06-20 17:15:19 +000097 SUnits.reserve(DAG.allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +000098
Dan Gohman94d7a5f2008-06-21 19:18:17 +000099 // During scheduling, the NodeId field of SDNode is used to map SDNodes
100 // to their associated SUnits by holding SUnits table indices. A value
101 // of -1 means the SDNode does not yet have an associated SUnit.
102 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
103 E = DAG.allnodes_end(); NI != E; ++NI)
104 NI->setNodeId(-1);
105
Evan Chenge165a782006-05-11 23:55:42 +0000106 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
107 E = DAG.allnodes_end(); NI != E; ++NI) {
108 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
109 continue;
110
111 // If this node has already been processed, stop now.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000112 if (NI->getNodeId() != -1) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000113
114 SUnit *NodeSUnit = NewSUnit(NI);
115
116 // See if anything is flagged to this node, if so, add them to flagged
117 // nodes. Nodes can have at most one flag input and one flag output. Flags
118 // are required the be the last operand and result of a node.
119
120 // Scan up, adding flagged preds to FlaggedNodes.
121 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000122 if (N->getNumOperands() &&
123 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
124 do {
125 N = N->getOperand(N->getNumOperands()-1).Val;
126 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000127 assert(N->getNodeId() == -1 && "Node already inserted!");
128 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000129 } while (N->getNumOperands() &&
130 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
131 std::reverse(NodeSUnit->FlaggedNodes.begin(),
132 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000133 }
134
135 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
136 // have a user of the flag operand.
137 N = NI;
138 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
139 SDOperand FlagVal(N, N->getNumValues()-1);
140
141 // There are either zero or one users of the Flag result.
142 bool HasFlagUse = false;
143 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
144 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000145 if (FlagVal.isOperandOf(UI->getUser())) {
Evan Chenge165a782006-05-11 23:55:42 +0000146 HasFlagUse = true;
147 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000148 assert(N->getNodeId() == -1 && "Node already inserted!");
149 N->setNodeId(NodeSUnit->NodeNum);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000150 N = UI->getUser();
Evan Chenge165a782006-05-11 23:55:42 +0000151 break;
152 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000153 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000154 }
155
156 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
157 // Update the SUnit
158 NodeSUnit->Node = N;
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000159 assert(N->getNodeId() == -1 && "Node already inserted!");
160 N->setNodeId(NodeSUnit->NodeNum);
Evan Chengf10c9732007-10-05 01:39:18 +0000161
162 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000163 }
164
165 // Pass 2: add the preds, succs, etc.
166 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
167 SUnit *SU = &SUnits[su];
168 SDNode *MainNode = SU->Node;
169
170 if (MainNode->isTargetOpcode()) {
171 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000172 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000173 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000174 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000175 SU->isTwoAddress = true;
176 break;
177 }
178 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000179 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000180 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000181 }
182
183 // Find all predecessors and successors of the group.
184 // Temporarily add N to make code simpler.
185 SU->FlaggedNodes.push_back(MainNode);
186
187 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
188 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000189 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000190 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
191 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000192 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000193
194 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
195 SDNode *OpN = N->getOperand(i).Val;
196 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000197 SUnit *OpSU = &SUnits[OpN->getNodeId()];
Evan Chenge165a782006-05-11 23:55:42 +0000198 assert(OpSU && "Node has no SUnit!");
199 if (OpSU == SU) continue; // In the same group.
200
Duncan Sands83ec4b62008-06-06 12:08:01 +0000201 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000202 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
203 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000204
205 unsigned PhysReg = 0;
206 int Cost = 1;
207 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000208 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000209 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000210 }
211 }
212
213 // Remove MainNode from FlaggedNodes again.
214 SU->FlaggedNodes.pop_back();
215 }
Evan Chenge165a782006-05-11 23:55:42 +0000216}
217
Evan Chengf10c9732007-10-05 01:39:18 +0000218void ScheduleDAG::ComputeLatency(SUnit *SU) {
219 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
220
221 // Compute the latency for the node. We use the sum of the latencies for
222 // all nodes flagged together into this SUnit.
223 if (InstrItins.isEmpty()) {
224 // No latency information.
225 SU->Latency = 1;
Evan Chengc6be7772008-07-02 09:23:51 +0000226 return;
227 }
228
229 SU->Latency = 0;
230 if (SU->Node->isTargetOpcode()) {
231 unsigned SchedClass = TII->get(SU->Node->getTargetOpcode()).getSchedClass();
232 const InstrStage *S = InstrItins.begin(SchedClass);
233 const InstrStage *E = InstrItins.end(SchedClass);
234 for (; S != E; ++S)
235 SU->Latency += S->Cycles;
236 }
237 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
238 SDNode *FNode = SU->FlaggedNodes[i];
239 if (FNode->isTargetOpcode()) {
240 unsigned SchedClass = TII->get(FNode->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000241 const InstrStage *S = InstrItins.begin(SchedClass);
242 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000243 for (; S != E; ++S)
244 SU->Latency += S->Cycles;
245 }
Evan Chengf10c9732007-10-05 01:39:18 +0000246 }
247}
248
Roman Levensteind86449e2008-03-04 11:19:43 +0000249/// CalculateDepths - compute depths using algorithms for the longest
250/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000251void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000252 unsigned DAGSize = SUnits.size();
253 std::vector<unsigned> InDegree(DAGSize);
254 std::vector<SUnit*> WorkList;
255 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000256
Roman Levensteind86449e2008-03-04 11:19:43 +0000257 // Initialize the data structures
258 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
259 SUnit *SU = &SUnits[i];
260 int NodeNum = SU->NodeNum;
261 unsigned Degree = SU->Preds.size();
262 InDegree[NodeNum] = Degree;
263 SU->Depth = 0;
264
265 // Is it a node without dependencies?
266 if (Degree == 0) {
267 assert(SU->Preds.empty() && "SUnit should have no predecessors");
268 // Collect leaf nodes
269 WorkList.push_back(SU);
270 }
271 }
272
273 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000274 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000275 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000276 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000277 unsigned &SUDepth = SU->Depth;
278
279 // Use dynamic programming:
280 // When current node is being processed, all of its dependencies
281 // are already processed.
282 // So, just iterate over all predecessors and take the longest path
283 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
284 I != E; ++I) {
285 unsigned PredDepth = I->Dep->Depth;
286 if (PredDepth+1 > SUDepth) {
287 SUDepth = PredDepth + 1;
288 }
289 }
290
291 // Update InDegrees of all nodes depending on current SUnit
292 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
293 I != E; ++I) {
294 SUnit *SU = I->Dep;
295 if (!--InDegree[SU->NodeNum])
296 // If all dependencies of the node are processed already,
297 // then the longest path for the node can be computed now
298 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000299 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000300 }
Evan Chenge165a782006-05-11 23:55:42 +0000301}
Evan Cheng99126282007-07-06 01:37:28 +0000302
Roman Levensteind86449e2008-03-04 11:19:43 +0000303/// CalculateHeights - compute heights using algorithms for the longest
304/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000305void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000306 unsigned DAGSize = SUnits.size();
307 std::vector<unsigned> InDegree(DAGSize);
308 std::vector<SUnit*> WorkList;
309 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000310
Roman Levensteind86449e2008-03-04 11:19:43 +0000311 // Initialize the data structures
312 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
313 SUnit *SU = &SUnits[i];
314 int NodeNum = SU->NodeNum;
315 unsigned Degree = SU->Succs.size();
316 InDegree[NodeNum] = Degree;
317 SU->Height = 0;
318
319 // Is it a node without dependencies?
320 if (Degree == 0) {
321 assert(SU->Succs.empty() && "Something wrong");
322 assert(WorkList.empty() && "Should be empty");
323 // Collect leaf nodes
324 WorkList.push_back(SU);
325 }
326 }
327
328 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000329 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000330 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000331 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000332 unsigned &SUHeight = SU->Height;
333
334 // Use dynamic programming:
335 // When current node is being processed, all of its dependencies
336 // are already processed.
337 // So, just iterate over all successors and take the longest path
338 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
339 I != E; ++I) {
340 unsigned SuccHeight = I->Dep->Height;
341 if (SuccHeight+1 > SUHeight) {
342 SUHeight = SuccHeight + 1;
343 }
344 }
345
346 // Update InDegrees of all nodes depending on current SUnit
347 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
348 I != E; ++I) {
349 SUnit *SU = I->Dep;
350 if (!--InDegree[SU->NodeNum])
351 // If all dependencies of the node are processed already,
352 // then the longest path for the node can be computed now
353 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000354 }
355 }
Evan Chenge165a782006-05-11 23:55:42 +0000356}
357
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000358/// CountResults - The results of target nodes have register or immediate
359/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000360/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000361unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000362 unsigned N = Node->getNumValues();
363 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000364 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000365 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000366 --N; // Skip over chain result.
367 return N;
368}
369
Dan Gohman69de1932008-02-06 22:27:42 +0000370/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000371/// followed by special operands that describe memory references, then an
372/// optional chain operand, then flag operands. Compute the number of
373/// actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000374unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000375 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohmancc20cd52008-02-11 19:00:34 +0000376 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman36b5c132008-04-07 19:35:22 +0000377 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000378 return N;
379}
380
Dan Gohman42a77882008-02-16 00:36:48 +0000381/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
382/// operand
383unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000384 unsigned N = Node->getNumOperands();
385 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
386 --N;
387 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
388 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000389 return N;
390}
391
Evan Chengc6be7772008-07-02 09:23:51 +0000392/// getInstrOperandRegClass - Return register class of the operand of an
393/// instruction of the specified TargetInstrDesc.
394static const TargetRegisterClass*
395getInstrOperandRegClass(const TargetRegisterInfo *TRI,
396 const TargetInstrInfo *TII, const TargetInstrDesc &II,
397 unsigned Op) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000398 if (Op >= II.getNumOperands()) {
399 assert(II.isVariadic() && "Invalid operand # of instruction");
Jim Laskey60f09922006-07-21 20:57:35 +0000400 return NULL;
401 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000402 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattner8ca5c672008-01-07 02:39:19 +0000403 return TII->getPointerRegClass();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000404 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Jim Laskey60f09922006-07-21 20:57:35 +0000405}
406
Evan Chengc6be7772008-07-02 09:23:51 +0000407/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
408/// implicit physical register output.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000409void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
Dan Gohman4c8c8302008-06-21 15:52:51 +0000410 bool IsClone, unsigned SrcReg,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000411 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng84097472007-08-02 00:28:15 +0000412 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000413 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000414 // Just use the input register directly!
Dan Gohman6b345ee2008-07-07 17:46:23 +0000415 SDOperand Op(Node, ResNo);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000416 if (IsClone)
Dan Gohman6b345ee2008-07-07 17:46:23 +0000417 VRBaseMap.erase(Op);
418 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
Evan Cheng97e60d92008-05-14 21:08:07 +0000419 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000420 assert(isNew && "Node emitted out of order - early");
421 return;
422 }
423
424 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
425 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000426 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000427 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
428 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000429 SDNode *Use = UI->getUser();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000430 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000431 if (Use->getOpcode() == ISD::CopyToReg &&
432 Use->getOperand(2).Val == Node &&
433 Use->getOperand(2).ResNo == ResNo) {
434 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000435 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000436 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000437 Match = false;
438 } else if (DestReg != SrcReg)
439 Match = false;
440 } else {
441 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
442 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000443 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000444 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000445 MVT VT = Node->getValueType(Op.ResNo);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000446 if (VT != MVT::Other && VT != MVT::Flag)
447 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000448 }
449 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000450 MatchReg &= Match;
451 if (VRBase)
452 break;
Evan Cheng84097472007-08-02 00:28:15 +0000453 }
454
Chris Lattner02b6d252008-03-09 08:49:15 +0000455 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000456 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000457
Evan Chenga6fb1b62007-09-25 01:54:36 +0000458 // Figure out the register class to create for the destreg.
Chris Lattner02b6d252008-03-09 08:49:15 +0000459 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000460 DstRC = MRI.getRegClass(VRBase);
Chris Lattner02b6d252008-03-09 08:49:15 +0000461 } else {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000462 DstRC = TLI->getRegClassFor(Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000463 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000464
465 // If all uses are reading from the src physical register and copying the
466 // register is either impossible or very expensive, then don't create a copy.
Chris Lattner02b6d252008-03-09 08:49:15 +0000467 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000468 VRBase = SrcReg;
469 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000470 // Create the reg, emit the copy.
Evan Cheng9e233362008-03-12 22:19:41 +0000471 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattner02b6d252008-03-09 08:49:15 +0000472 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng84097472007-08-02 00:28:15 +0000473 }
Evan Cheng84097472007-08-02 00:28:15 +0000474
Dan Gohman6b345ee2008-07-07 17:46:23 +0000475 SDOperand Op(Node, ResNo);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000476 if (IsClone)
Dan Gohman6b345ee2008-07-07 17:46:23 +0000477 VRBaseMap.erase(Op);
478 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
Evan Cheng97e60d92008-05-14 21:08:07 +0000479 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000480 assert(isNew && "Node emitted out of order - early");
481}
482
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000483/// getDstOfCopyToRegUse - If the only use of the specified result number of
484/// node is a CopyToReg, return its destination register. Return 0 otherwise.
485unsigned ScheduleDAG::getDstOfOnlyCopyToRegUse(SDNode *Node,
486 unsigned ResNo) const {
487 if (!Node->hasOneUse())
488 return 0;
489
Roman Levensteindc1adac2008-04-07 10:06:32 +0000490 SDNode *Use = Node->use_begin()->getUser();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000491 if (Use->getOpcode() == ISD::CopyToReg &&
492 Use->getOperand(2).Val == Node &&
493 Use->getOperand(2).ResNo == ResNo) {
494 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
495 if (TargetRegisterInfo::isVirtualRegister(Reg))
496 return Reg;
497 }
498 return 0;
499}
500
Evan Chengda47e6e2008-03-15 00:03:38 +0000501void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000502 const TargetInstrDesc &II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000503 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000504 assert(Node->getTargetOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
505 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
506
Chris Lattner349c4952008-01-07 03:13:06 +0000507 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000508 // If the specific node value is only used by a CopyToReg and the dest reg
509 // is a vreg, use the CopyToReg'd destination register instead of creating
510 // a new vreg.
511 unsigned VRBase = 0;
512 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
513 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000514 SDNode *Use = UI->getUser();
Evan Chengaf825c82007-07-10 07:08:32 +0000515 if (Use->getOpcode() == ISD::CopyToReg &&
516 Use->getOperand(2).Val == Node &&
517 Use->getOperand(2).ResNo == i) {
518 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000519 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000520 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000521 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000522 break;
523 }
524 }
525 }
526
Evan Cheng84097472007-08-02 00:28:15 +0000527 // Create the result registers for this node and add the result regs to
528 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000529 if (VRBase == 0) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000530 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000531 assert(RC && "Isn't a register operand!");
Evan Cheng9e233362008-03-12 22:19:41 +0000532 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000533 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000534 }
535
Dan Gohman6b345ee2008-07-07 17:46:23 +0000536 SDOperand Op(Node, i);
537 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
Evan Cheng97e60d92008-05-14 21:08:07 +0000538 isNew = isNew; // Silence compiler warning.
Evan Chengaf825c82007-07-10 07:08:32 +0000539 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000540 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000541}
542
Chris Lattnerdf375062006-03-10 07:25:12 +0000543/// getVR - Return the virtual register corresponding to the specified result
544/// of the specified node.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000545unsigned ScheduleDAG::getVR(SDOperand Op,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000546 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000547 if (Op.isTargetOpcode() &&
548 Op.getTargetOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
549 // Add an IMPLICIT_DEF instruction before every use.
550 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.Val, Op.ResNo);
551 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
552 // does not include operand register class info.
553 if (!VReg) {
554 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
555 VReg = MRI.createVirtualRegister(RC);
556 }
557 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
558 return VReg;
559 }
560
Roman Levenstein9cac5252008-04-16 16:15:27 +0000561 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000562 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000563 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000564}
565
566
Chris Lattnered18b682006-02-24 18:54:03 +0000567/// AddOperand - Add the specified operand to the specified machine instr. II
568/// specifies the instruction information for the node, and IIOpNum is the
569/// operand number (in the II) that we are adding. IIOpNum and II are used for
570/// assertions only.
571void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
572 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000573 const TargetInstrDesc *II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000574 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000575 if (Op.isTargetOpcode()) {
576 // Note that this case is redundant with the final else block, but we
577 // include it because it is the most common and it makes the logic
578 // simpler here.
579 assert(Op.getValueType() != MVT::Other &&
580 Op.getValueType() != MVT::Flag &&
581 "Chain and flag operands should occur at end of operand list!");
Chris Lattnered18b682006-02-24 18:54:03 +0000582 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000583 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000584 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000585 bool isOptDef = IIOpNum < TID.getNumOperands() &&
586 TID.OpInfo[IIOpNum].isOptionalDef();
Chris Lattner8019f412007-12-30 00:41:17 +0000587 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000588
589 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000590 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerb7795802008-03-11 00:59:28 +0000591#ifndef NDEBUG
Chris Lattnered18b682006-02-24 18:54:03 +0000592 if (II) {
Chris Lattnerb7795802008-03-11 00:59:28 +0000593 // There may be no register class for this operand if it is a variadic
594 // argument (RC will be NULL in this case). In this case, we just assume
595 // the regclass is ok.
Jim Laskey60f09922006-07-21 20:57:35 +0000596 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000597 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000598 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng9e233362008-03-12 22:19:41 +0000599 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerb7795802008-03-11 00:59:28 +0000600 if (RC && VRC != RC) {
Chris Lattner01528292007-02-15 18:17:56 +0000601 cerr << "Register class of operand and regclass of use don't agree!\n";
Chris Lattner01528292007-02-15 18:17:56 +0000602 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000603 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000604 cerr << "MI = "; MI->print(cerr);
605 cerr << "VReg = " << VReg << "\n";
606 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000607 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000608 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000609 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000610 cerr << "Fatal error, aborting.\n";
611 abort();
612 }
Chris Lattnered18b682006-02-24 18:54:03 +0000613 }
Chris Lattnerb7795802008-03-11 00:59:28 +0000614#endif
Chris Lattnerfec65d52007-12-30 00:51:11 +0000615 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000616 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000617 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Chris Lattner02a260a2008-04-20 00:41:09 +0000618 ConstantFP *CFP = ConstantFP::get(F->getValueAPF());
Nate Begemane1795842008-02-14 08:57:00 +0000619 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000620 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000621 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000622 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
623 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
624 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
625 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
626 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
627 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
628 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
629 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
630 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000631 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000632 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000633 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000634 // MachineConstantPool wants an explicit alignment.
635 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000636 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000637 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000638 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000639 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000640 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000641 }
Chris Lattnered18b682006-02-24 18:54:03 +0000642 }
643
Evan Chengd6594ae2006-09-12 21:00:35 +0000644 unsigned Idx;
645 if (CP->isMachineConstantPoolEntry())
646 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
647 else
648 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000649 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
650 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
651 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000652 } else {
653 assert(Op.getValueType() != MVT::Other &&
654 Op.getValueType() != MVT::Flag &&
655 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000656 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000657 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000658
Chris Lattner02b6d252008-03-09 08:49:15 +0000659 // Verify that it is right. Note that the reg class of the physreg and the
660 // vreg don't necessarily need to match, but the target copy insertion has
661 // to be able to handle it. This handles things like copies from ST(0) to
662 // an FP vreg on x86.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000663 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000664 if (II && !II->isVariadic()) {
Chris Lattner02b6d252008-03-09 08:49:15 +0000665 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
666 "Don't have operand info for this instruction!");
Chris Lattnered18b682006-02-24 18:54:03 +0000667 }
Evan Chengc6be7772008-07-02 09:23:51 +0000668 }
Chris Lattnered18b682006-02-24 18:54:03 +0000669}
670
Dan Gohman36b5c132008-04-07 19:35:22 +0000671void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000672 MI->addMemOperand(*MF, MO);
Dan Gohman69de1932008-02-06 22:27:42 +0000673}
674
Evan Chengc6be7772008-07-02 09:23:51 +0000675/// getSubRegisterRegClass - Returns the register class of specified register
676/// class' "SubIdx"'th sub-register class.
677static const TargetRegisterClass*
678getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000679 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000680 TargetRegisterInfo::regclass_iterator I =
681 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000682 assert(I < TRC->subregclasses_end() &&
683 "Invalid subregister index for register class");
684 return *I;
685}
686
Evan Chengc6be7772008-07-02 09:23:51 +0000687/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
688/// "SubIdx"'th sub-register class is the specified register class and whose
689/// type matches the specified type.
690static const TargetRegisterClass*
691getSuperRegisterRegClass(const TargetRegisterClass *TRC,
692 unsigned SubIdx, MVT VT) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000693 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000694 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000695 E = TRC->superregclasses_end(); I != E; ++I)
696 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
697 return *I;
698 assert(false && "Couldn't find the register class");
699 return 0;
700}
701
702/// EmitSubregNode - Generate machine code for subreg nodes.
703///
704void ScheduleDAG::EmitSubregNode(SDNode *Node,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000705 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000706 unsigned VRBase = 0;
707 unsigned Opc = Node->getTargetOpcode();
Christopher Lambc9298232008-03-16 03:12:01 +0000708
709 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
710 // the CopyToReg'd destination register instead of creating a new vreg.
711 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
712 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000713 SDNode *Use = UI->getUser();
Christopher Lambc9298232008-03-16 03:12:01 +0000714 if (Use->getOpcode() == ISD::CopyToReg &&
715 Use->getOperand(2).Val == Node) {
716 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
717 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
718 VRBase = DestReg;
719 break;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000720 }
721 }
Christopher Lambc9298232008-03-16 03:12:01 +0000722 }
723
724 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000725 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000726
Christopher Lambe24f8f12007-07-26 08:12:07 +0000727 // Create the extract_subreg machine instruction.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000728 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000729
730 // Figure out the register class to create for the destreg.
731 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng9e233362008-03-12 22:19:41 +0000732 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000733 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
734
735 if (VRBase) {
736 // Grab the destination register
Evan Cheng50871242008-05-14 20:07:51 +0000737#ifndef NDEBUG
Evan Cheng9e233362008-03-12 22:19:41 +0000738 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000739 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000740 "Source subregister and destination must have the same class");
Evan Cheng50871242008-05-14 20:07:51 +0000741#endif
Christopher Lambe24f8f12007-07-26 08:12:07 +0000742 } else {
743 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000744 assert(SRC && "Couldn't find source register class");
Evan Cheng9e233362008-03-12 22:19:41 +0000745 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000746 }
747
748 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000749 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000750 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000751 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000752 BB->push_back(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000753 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
754 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000755 SDOperand N0 = Node->getOperand(0);
756 SDOperand N1 = Node->getOperand(1);
757 SDOperand N2 = Node->getOperand(2);
758 unsigned SubReg = getVR(N1, VRBaseMap);
759 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000760
Christopher Lambe24f8f12007-07-26 08:12:07 +0000761
762 // Figure out the register class to create for the destreg.
763 const TargetRegisterClass *TRC = 0;
764 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000765 TRC = MRI.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000766 } else {
Evan Chengc6be7772008-07-02 09:23:51 +0000767 TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000768 Node->getValueType(0));
769 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng9e233362008-03-12 22:19:41 +0000770 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000771 }
772
Christopher Lambc9298232008-03-16 03:12:01 +0000773 // Create the insert_subreg or subreg_to_reg machine instruction.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000774 MachineInstr *MI = BuildMI(*MF, TII->get(Opc));
Chris Lattner8019f412007-12-30 00:41:17 +0000775 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000776
Christopher Lambc9298232008-03-16 03:12:01 +0000777 // If creating a subreg_to_reg, then the first input operand
778 // is an implicit value immediate, otherwise it's a register
779 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
780 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000781 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
Christopher Lambc9298232008-03-16 03:12:01 +0000782 } else
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000783 AddOperand(MI, N0, 0, 0, VRBaseMap);
784 // Add the subregster being inserted
785 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000786 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000787 BB->push_back(MI);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000788 } else
Christopher Lambc9298232008-03-16 03:12:01 +0000789 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Christopher Lambe24f8f12007-07-26 08:12:07 +0000790
Dan Gohman6b345ee2008-07-07 17:46:23 +0000791 SDOperand Op(Node, 0);
792 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
Evan Cheng97e60d92008-05-14 21:08:07 +0000793 isNew = isNew; // Silence compiler warning.
Christopher Lambe24f8f12007-07-26 08:12:07 +0000794 assert(isNew && "Node emitted out of order - early");
795}
796
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000797/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000798///
Dan Gohman4c8c8302008-06-21 15:52:51 +0000799void ScheduleDAG::EmitNode(SDNode *Node, bool IsClone,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000800 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000801 // If machine instruction
802 if (Node->isTargetOpcode()) {
803 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000804
805 // Handle subreg insert/extract specially
806 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Christopher Lambc9298232008-03-16 03:12:01 +0000807 Opc == TargetInstrInfo::INSERT_SUBREG ||
808 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000809 EmitSubregNode(Node, VRBaseMap);
810 return;
811 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000812
813 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
814 // We want a unique VR for each IMPLICIT_DEF use.
815 return;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000816
Chris Lattner749c6f62008-01-07 07:27:27 +0000817 const TargetInstrDesc &II = TII->get(Opc);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000818 unsigned NumResults = CountResults(Node);
819 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000820 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Chris Lattner349c4952008-01-07 03:13:06 +0000821 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
822 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000823#ifndef NDEBUG
Evan Cheng50871242008-05-14 20:07:51 +0000824 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000825 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000826 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000827 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000828#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000829
830 // Create the new machine instruction.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000831 MachineInstr *MI = BuildMI(*MF, II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000832
833 // Add result register values for things that are defined by this
834 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000835 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000836 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000837
838 // Emit all of the actual operands of this instruction, adding them to the
839 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000840 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000841 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000842
Dan Gohman69de1932008-02-06 22:27:42 +0000843 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000844 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000845 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
846
Evan Cheng13d41b92006-05-12 01:58:24 +0000847 // Commute node if it has been determined to be profitable.
848 if (CommuteSet.count(Node)) {
849 MachineInstr *NewMI = TII->commuteInstruction(MI);
850 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000851 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000852 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000853 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000854 if (MI != NewMI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000855 MF->DeleteMachineInstr(MI);
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000856 MI = NewMI;
857 }
Evan Cheng643afa52008-02-28 07:40:24 +0000858 ++NumCommutes;
Evan Cheng13d41b92006-05-12 01:58:24 +0000859 }
860 }
861
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000862 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000863 // Insert this instruction into the basic block using a target
864 // specific inserter which may returns a new basic block.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000865 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000866 else
867 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000868
869 // Additional results must be an physical register def.
870 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000871 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
872 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000873 if (Node->hasAnyUseOfValue(i))
Dan Gohman4c8c8302008-06-21 15:52:51 +0000874 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000875 }
876 }
Evan Chengc6be7772008-07-02 09:23:51 +0000877 return;
878 }
879
880 switch (Node->getOpcode()) {
881 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000882#ifndef NDEBUG
Evan Chengc6be7772008-07-02 09:23:51 +0000883 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000884#endif
Evan Chengc6be7772008-07-02 09:23:51 +0000885 assert(0 && "This target-independent node should have been selected!");
886 break;
887 case ISD::EntryToken:
888 assert(0 && "EntryToken should have been excluded from the schedule!");
889 break;
890 case ISD::TokenFactor: // fall thru
Evan Chengc6be7772008-07-02 09:23:51 +0000891 break;
892 case ISD::CopyToReg: {
893 unsigned SrcReg;
894 SDOperand SrcVal = Node->getOperand(2);
895 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
896 SrcReg = R->getReg();
897 else
898 SrcReg = getVR(SrcVal, VRBaseMap);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000899
Evan Chengc6be7772008-07-02 09:23:51 +0000900 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
901 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
902 break;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000903
Evan Chengc6be7772008-07-02 09:23:51 +0000904 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
905 // Get the register classes of the src/dst.
906 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
907 SrcTRC = MRI.getRegClass(SrcReg);
908 else
909 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000910
Evan Chengc6be7772008-07-02 09:23:51 +0000911 if (TargetRegisterInfo::isVirtualRegister(DestReg))
912 DstTRC = MRI.getRegClass(DestReg);
913 else
914 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
Evan Cheng676dd7c2008-03-11 07:19:34 +0000915 Node->getOperand(1).getValueType());
Evan Chengc6be7772008-07-02 09:23:51 +0000916 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
917 break;
918 }
919 case ISD::CopyFromReg: {
920 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
921 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
922 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.
Chris Lattneracc43bf2006-01-26 23:28:04 +0000928
Evan Chengc6be7772008-07-02 09:23:51 +0000929 // Create the inline asm machine instruction.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000930 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000931
Evan Chengc6be7772008-07-02 09:23:51 +0000932 // Add the asm string as an external symbol operand.
933 const char *AsmStr =
934 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
935 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000936
Evan Chengc6be7772008-07-02 09:23:51 +0000937 // 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;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000941
Evan Chengc6be7772008-07-02 09:23:51 +0000942 MI->addOperand(MachineOperand::CreateImm(Flags));
943 ++i; // Skip the ID value.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000944
Evan Chengc6be7772008-07-02 09:23:51 +0000945 switch (Flags & 7) {
946 default: assert(0 && "Bad flags!");
Evan Chengc6be7772008-07-02 09:23:51 +0000947 case 2: // Def of register.
948 for (; NumVals; --NumVals, ++i) {
949 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
950 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerdc19b702006-02-04 02:26:14 +0000951 }
Evan Chengc6be7772008-07-02 09:23:51 +0000952 break;
Dan Gohmancd920d92008-07-02 23:23:19 +0000953 case 1: // Use of register.
954 case 3: // Immediate.
Evan Chengc6be7772008-07-02 09:23:51 +0000955 case 4: // Addressing mode.
956 // The addressing mode has been selected, just add all of the
957 // operands to the machine instruction.
958 for (; NumVals; --NumVals, ++i)
959 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
960 break;
961 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000962 }
Evan Chengc6be7772008-07-02 09:23:51 +0000963 BB->push_back(MI);
964 break;
965 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000966 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000967}
968
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000969void ScheduleDAG::EmitNoop() {
970 TII->insertNoop(*BB, BB->end());
971}
972
Chris Lattnerd9c4c452008-03-09 07:51:01 +0000973void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
974 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng42d60272007-09-26 21:36:17 +0000975 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
976 I != E; ++I) {
977 if (I->isCtrl) continue; // ignore chain preds
978 if (!I->Dep->Node) {
979 // Copy to physical register.
980 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
981 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
982 // Find the destination physical register.
983 unsigned Reg = 0;
984 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
985 EE = SU->Succs.end(); II != EE; ++II) {
986 if (I->Reg) {
987 Reg = I->Reg;
988 break;
989 }
990 }
991 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +0000992 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +0000993 SU->CopyDstRC, SU->CopySrcRC);
994 } else {
995 // Copy from physical register.
996 assert(I->Reg && "Unknown physical register!");
Evan Cheng9e233362008-03-12 22:19:41 +0000997 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Dan Gohman6b345ee2008-07-07 17:46:23 +0000998 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
Evan Cheng97e60d92008-05-14 21:08:07 +0000999 isNew = isNew; // Silence compiler warning.
Evan Cheng42d60272007-09-26 21:36:17 +00001000 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +00001001 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +00001002 SU->CopyDstRC, SU->CopySrcRC);
1003 }
1004 break;
1005 }
1006}
1007
Evan Cheng9e233362008-03-12 22:19:41 +00001008/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1009/// physical register has only a single copy use, then coalesced the copy
Evan Chengdb2d7732008-03-14 00:14:55 +00001010/// if possible.
1011void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
1012 MachineBasicBlock::iterator &InsertPos,
1013 unsigned VirtReg, unsigned PhysReg,
1014 const TargetRegisterClass *RC,
1015 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng9e233362008-03-12 22:19:41 +00001016 unsigned NumUses = 0;
1017 MachineInstr *UseMI = NULL;
1018 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1019 UE = MRI.use_end(); UI != UE; ++UI) {
1020 UseMI = &*UI;
1021 if (++NumUses > 1)
1022 break;
1023 }
1024
1025 // If the number of uses is not one, or the use is not a move instruction,
Evan Chengdb2d7732008-03-14 00:14:55 +00001026 // don't coalesce. Also, only coalesce away a virtual register to virtual
1027 // register copy.
1028 bool Coalesced = false;
Evan Cheng9e233362008-03-12 22:19:41 +00001029 unsigned SrcReg, DstReg;
Evan Chengdb2d7732008-03-14 00:14:55 +00001030 if (NumUses == 1 &&
1031 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1032 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1033 VirtReg = DstReg;
1034 Coalesced = true;
Evan Cheng9e233362008-03-12 22:19:41 +00001035 }
1036
Evan Chengdb2d7732008-03-14 00:14:55 +00001037 // Now find an ideal location to insert the copy.
1038 MachineBasicBlock::iterator Pos = InsertPos;
1039 while (Pos != MBB->begin()) {
1040 MachineInstr *PrevMI = prior(Pos);
1041 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1042 // copyRegToReg might emit multiple instructions to do a copy.
1043 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1044 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1045 // This is what the BB looks like right now:
1046 // r1024 = mov r0
1047 // ...
1048 // r1 = mov r1024
1049 //
1050 // We want to insert "r1025 = mov r1". Inserting this copy below the
1051 // move to r1024 makes it impossible for that move to be coalesced.
1052 //
1053 // r1025 = mov r1
1054 // r1024 = mov r0
1055 // ...
1056 // r1 = mov 1024
1057 // r2 = mov 1025
1058 break; // Woot! Found a good location.
1059 --Pos;
1060 }
1061
1062 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1063 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1064 if (Coalesced) {
Evan Cheng9e233362008-03-12 22:19:41 +00001065 if (&*InsertPos == UseMI) ++InsertPos;
1066 MBB->erase(UseMI);
Evan Cheng9e233362008-03-12 22:19:41 +00001067 }
Evan Cheng9e233362008-03-12 22:19:41 +00001068}
1069
1070/// EmitLiveInCopies - If this is the first basic block in the function,
1071/// and if it has live ins that need to be copied into vregs, emit the
1072/// copies into the top of the block.
1073void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chengdb2d7732008-03-14 00:14:55 +00001074 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng9e233362008-03-12 22:19:41 +00001075 MachineBasicBlock::iterator InsertPos = MBB->begin();
1076 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1077 E = MRI.livein_end(); LI != E; ++LI)
1078 if (LI->second) {
1079 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chengdb2d7732008-03-14 00:14:55 +00001080 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng9e233362008-03-12 22:19:41 +00001081 }
1082}
1083
Evan Chenge165a782006-05-11 23:55:42 +00001084/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman5e843682008-07-14 18:19:29 +00001085MachineBasicBlock *ScheduleDAG::EmitSchedule() {
Evan Cheng9e233362008-03-12 22:19:41 +00001086 bool isEntryBB = &MF->front() == BB;
1087
1088 if (isEntryBB && !SchedLiveInCopies) {
1089 // If this is the first basic block in the function, and if it has live ins
1090 // that need to be copied into vregs, emit the copies into the top of the
1091 // block before emitting the code for the block.
1092 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1093 E = MRI.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +00001094 if (LI->second) {
Evan Cheng9e233362008-03-12 22:19:41 +00001095 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +00001096 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +00001097 LI->first, RC, RC);
1098 }
Chris Lattner96645412006-05-16 06:10:58 +00001099 }
Evan Cheng9e233362008-03-12 22:19:41 +00001100
Chris Lattner96645412006-05-16 06:10:58 +00001101 // Finally, emit the code for all of the scheduled instructions.
Roman Levenstein9cac5252008-04-16 16:15:27 +00001102 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +00001103 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +00001104 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001105 SUnit *SU = Sequence[i];
1106 if (!SU) {
Evan Chenge165a782006-05-11 23:55:42 +00001107 // Null SUnit* is a noop.
1108 EmitNoop();
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001109 continue;
Evan Chenge165a782006-05-11 23:55:42 +00001110 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001111 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
Dan Gohman4c8c8302008-06-21 15:52:51 +00001112 EmitNode(SU->FlaggedNodes[j], SU->OrigNode != SU, VRBaseMap);
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001113 if (!SU->Node)
1114 EmitCrossRCCopy(SU, CopyVRBaseMap);
1115 else
Dan Gohman4c8c8302008-06-21 15:52:51 +00001116 EmitNode(SU->Node, SU->OrigNode != SU, VRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +00001117 }
Evan Cheng9e233362008-03-12 22:19:41 +00001118
1119 if (isEntryBB && SchedLiveInCopies)
1120 EmitLiveInCopies(MF->begin());
Dan Gohman5e843682008-07-14 18:19:29 +00001121
1122 return BB;
Evan Chenge165a782006-05-11 23:55:42 +00001123}
1124
1125/// dump - dump the schedule.
1126void ScheduleDAG::dumpSchedule() const {
1127 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1128 if (SUnit *SU = Sequence[i])
1129 SU->dump(&DAG);
1130 else
Bill Wendling832171c2006-12-07 20:04:42 +00001131 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +00001132 }
1133}
1134
1135
Evan Chenga9c20912006-01-21 02:32:06 +00001136/// Run - perform scheduling.
1137///
Dan Gohman5e843682008-07-14 18:19:29 +00001138void ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +00001139 Schedule();
Dan Gohman5e843682008-07-14 18:19:29 +00001140
1141 DOUT << "*** Final schedule ***\n";
1142 DEBUG(dumpSchedule());
1143 DOUT << "\n";
Chris Lattnerd32b2362005-08-18 18:45:24 +00001144}
Evan Cheng4ef10862006-01-23 07:01:07 +00001145
Evan Chenge165a782006-05-11 23:55:42 +00001146/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1147/// a group of nodes flagged together.
1148void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00001149 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +00001150 if (Node)
1151 Node->dump(G);
1152 else
1153 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +00001154 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001155 if (FlaggedNodes.size() != 0) {
1156 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +00001157 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +00001158 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +00001159 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001160 }
1161 }
1162}
Evan Cheng4ef10862006-01-23 07:01:07 +00001163
Evan Chenge165a782006-05-11 23:55:42 +00001164void SUnit::dumpAll(const SelectionDAG *G) const {
1165 dump(G);
1166
Bill Wendling832171c2006-12-07 20:04:42 +00001167 cerr << " # preds left : " << NumPredsLeft << "\n";
1168 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +00001169 cerr << " Latency : " << Latency << "\n";
1170 cerr << " Depth : " << Depth << "\n";
1171 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001172
1173 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001174 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001175 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1176 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001177 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001178 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001179 else
Bill Wendling832171c2006-12-07 20:04:42 +00001180 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001181 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1182 if (I->isSpecial)
1183 cerr << " *";
1184 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001185 }
1186 }
1187 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001188 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001189 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1190 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001191 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001192 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001193 else
Bill Wendling832171c2006-12-07 20:04:42 +00001194 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001195 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1196 if (I->isSpecial)
1197 cerr << " *";
1198 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001199 }
1200 }
Bill Wendling832171c2006-12-07 20:04:42 +00001201 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001202}